Annotation of elwix/files/sqlite/dist/sqlite3.c, revision 1.2
1.2 ! misho 1: /******************************************************************************
! 2: ** This file is an amalgamation of many separate C source files from SQLite
! 3: ** version 3.7.10. 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 following macros are used to cast pointers to integers and
! 321: ** integers to pointers. The way you do this varies from one compiler
! 322: ** to the next, so we have developed the following set of #if statements
! 323: ** to generate appropriate macros for a wide range of compilers.
! 324: **
! 325: ** The correct "ANSI" way to do this is to use the intptr_t type.
! 326: ** Unfortunately, that typedef is not available on all compilers, or
! 327: ** if it is available, it requires an #include of specific headers
! 328: ** that vary from one machine to the next.
! 329: **
! 330: ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
! 331: ** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
! 332: ** So we have to define the macros in different ways depending on the
! 333: ** compiler.
! 334: */
! 335: #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
! 336: # define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
! 337: # define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
! 338: #elif !defined(__GNUC__) /* Works for compilers other than LLVM */
! 339: # define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X])
! 340: # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
! 341: #elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
! 342: # define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
! 343: # define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
! 344: #else /* Generates a warning - but it always works */
! 345: # define SQLITE_INT_TO_PTR(X) ((void*)(X))
! 346: # define SQLITE_PTR_TO_INT(X) ((int)(X))
! 347: #endif
! 348:
! 349: /*
! 350: ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
! 351: ** 0 means mutexes are permanently disable and the library is never
! 352: ** threadsafe. 1 means the library is serialized which is the highest
! 353: ** level of threadsafety. 2 means the libary is multithreaded - multiple
! 354: ** threads can use SQLite as long as no two threads try to use the same
! 355: ** database connection at the same time.
! 356: **
! 357: ** Older versions of SQLite used an optional THREADSAFE macro.
! 358: ** We support that for legacy.
! 359: */
! 360: #if !defined(SQLITE_THREADSAFE)
! 361: #if defined(THREADSAFE)
! 362: # define SQLITE_THREADSAFE THREADSAFE
! 363: #else
! 364: # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
! 365: #endif
! 366: #endif
! 367:
! 368: /*
! 369: ** Powersafe overwrite is on by default. But can be turned off using
! 370: ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
! 371: */
! 372: #ifndef SQLITE_POWERSAFE_OVERWRITE
! 373: # define SQLITE_POWERSAFE_OVERWRITE 1
! 374: #endif
! 375:
! 376: /*
! 377: ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
! 378: ** It determines whether or not the features related to
! 379: ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
! 380: ** be overridden at runtime using the sqlite3_config() API.
! 381: */
! 382: #if !defined(SQLITE_DEFAULT_MEMSTATUS)
! 383: # define SQLITE_DEFAULT_MEMSTATUS 1
! 384: #endif
! 385:
! 386: /*
! 387: ** Exactly one of the following macros must be defined in order to
! 388: ** specify which memory allocation subsystem to use.
! 389: **
! 390: ** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
! 391: ** SQLITE_WIN32_MALLOC // Use Win32 native heap API
! 392: ** SQLITE_MEMDEBUG // Debugging version of system malloc()
! 393: **
! 394: ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
! 395: ** assert() macro is enabled, each call into the Win32 native heap subsystem
! 396: ** will cause HeapValidate to be called. If heap validation should fail, an
! 397: ** assertion will be triggered.
! 398: **
! 399: ** (Historical note: There used to be several other options, but we've
! 400: ** pared it down to just these three.)
! 401: **
! 402: ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
! 403: ** the default.
! 404: */
! 405: #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
! 406: # error "At most one of the following compile-time configuration options\
! 407: is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
! 408: #endif
! 409: #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0
! 410: # define SQLITE_SYSTEM_MALLOC 1
! 411: #endif
! 412:
! 413: /*
! 414: ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
! 415: ** sizes of memory allocations below this value where possible.
! 416: */
! 417: #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
! 418: # define SQLITE_MALLOC_SOFT_LIMIT 1024
! 419: #endif
! 420:
! 421: /*
! 422: ** We need to define _XOPEN_SOURCE as follows in order to enable
! 423: ** recursive mutexes on most Unix systems. But Mac OS X is different.
! 424: ** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
! 425: ** so it is omitted there. See ticket #2673.
! 426: **
! 427: ** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
! 428: ** implemented on some systems. So we avoid defining it at all
! 429: ** if it is already defined or if it is unneeded because we are
! 430: ** not doing a threadsafe build. Ticket #2681.
! 431: **
! 432: ** See also ticket #2741.
! 433: */
! 434: #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE
! 435: # define _XOPEN_SOURCE 500 /* Needed to enable pthread recursive mutexes */
! 436: #endif
! 437:
! 438: /*
! 439: ** The TCL headers are only needed when compiling the TCL bindings.
! 440: */
! 441: #if defined(SQLITE_TCL) || defined(TCLSH)
! 442: # include <tcl.h>
! 443: #endif
! 444:
! 445: /*
! 446: ** Many people are failing to set -DNDEBUG=1 when compiling SQLite.
! 447: ** Setting NDEBUG makes the code smaller and run faster. So the following
! 448: ** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1
! 449: ** option is set. Thus NDEBUG becomes an opt-in rather than an opt-out
! 450: ** feature.
! 451: */
! 452: #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
! 453: # define NDEBUG 1
! 454: #endif
! 455:
! 456: /*
! 457: ** The testcase() macro is used to aid in coverage testing. When
! 458: ** doing coverage testing, the condition inside the argument to
! 459: ** testcase() must be evaluated both true and false in order to
! 460: ** get full branch coverage. The testcase() macro is inserted
! 461: ** to help ensure adequate test coverage in places where simple
! 462: ** condition/decision coverage is inadequate. For example, testcase()
! 463: ** can be used to make sure boundary values are tested. For
! 464: ** bitmask tests, testcase() can be used to make sure each bit
! 465: ** is significant and used at least once. On switch statements
! 466: ** where multiple cases go to the same block of code, testcase()
! 467: ** can insure that all cases are evaluated.
! 468: **
! 469: */
! 470: #ifdef SQLITE_COVERAGE_TEST
! 471: SQLITE_PRIVATE void sqlite3Coverage(int);
! 472: # define testcase(X) if( X ){ sqlite3Coverage(__LINE__); }
! 473: #else
! 474: # define testcase(X)
! 475: #endif
! 476:
! 477: /*
! 478: ** The TESTONLY macro is used to enclose variable declarations or
! 479: ** other bits of code that are needed to support the arguments
! 480: ** within testcase() and assert() macros.
! 481: */
! 482: #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
! 483: # define TESTONLY(X) X
! 484: #else
! 485: # define TESTONLY(X)
! 486: #endif
! 487:
! 488: /*
! 489: ** Sometimes we need a small amount of code such as a variable initialization
! 490: ** to setup for a later assert() statement. We do not want this code to
! 491: ** appear when assert() is disabled. The following macro is therefore
! 492: ** used to contain that setup code. The "VVA" acronym stands for
! 493: ** "Verification, Validation, and Accreditation". In other words, the
! 494: ** code within VVA_ONLY() will only run during verification processes.
! 495: */
! 496: #ifndef NDEBUG
! 497: # define VVA_ONLY(X) X
! 498: #else
! 499: # define VVA_ONLY(X)
! 500: #endif
! 501:
! 502: /*
! 503: ** The ALWAYS and NEVER macros surround boolean expressions which
! 504: ** are intended to always be true or false, respectively. Such
! 505: ** expressions could be omitted from the code completely. But they
! 506: ** are included in a few cases in order to enhance the resilience
! 507: ** of SQLite to unexpected behavior - to make the code "self-healing"
! 508: ** or "ductile" rather than being "brittle" and crashing at the first
! 509: ** hint of unplanned behavior.
! 510: **
! 511: ** In other words, ALWAYS and NEVER are added for defensive code.
! 512: **
! 513: ** When doing coverage testing ALWAYS and NEVER are hard-coded to
! 514: ** be true and false so that the unreachable code then specify will
! 515: ** not be counted as untested code.
! 516: */
! 517: #if defined(SQLITE_COVERAGE_TEST)
! 518: # define ALWAYS(X) (1)
! 519: # define NEVER(X) (0)
! 520: #elif !defined(NDEBUG)
! 521: # define ALWAYS(X) ((X)?1:(assert(0),0))
! 522: # define NEVER(X) ((X)?(assert(0),1):0)
! 523: #else
! 524: # define ALWAYS(X) (X)
! 525: # define NEVER(X) (X)
! 526: #endif
! 527:
! 528: /*
! 529: ** Return true (non-zero) if the input is a integer that is too large
! 530: ** to fit in 32-bits. This macro is used inside of various testcase()
! 531: ** macros to verify that we have tested SQLite for large-file support.
! 532: */
! 533: #define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0)
! 534:
! 535: /*
! 536: ** The macro unlikely() is a hint that surrounds a boolean
! 537: ** expression that is usually false. Macro likely() surrounds
! 538: ** a boolean expression that is usually true. GCC is able to
! 539: ** use these hints to generate better code, sometimes.
! 540: */
! 541: #if defined(__GNUC__) && 0
! 542: # define likely(X) __builtin_expect((X),1)
! 543: # define unlikely(X) __builtin_expect((X),0)
! 544: #else
! 545: # define likely(X) !!(X)
! 546: # define unlikely(X) !!(X)
! 547: #endif
! 548:
! 549: /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
! 550: /************** Begin file sqlite3.h *****************************************/
! 551: /*
! 552: ** 2001 September 15
! 553: **
! 554: ** The author disclaims copyright to this source code. In place of
! 555: ** a legal notice, here is a blessing:
! 556: **
! 557: ** May you do good and not evil.
! 558: ** May you find forgiveness for yourself and forgive others.
! 559: ** May you share freely, never taking more than you give.
! 560: **
! 561: *************************************************************************
! 562: ** This header file defines the interface that the SQLite library
! 563: ** presents to client programs. If a C-function, structure, datatype,
! 564: ** or constant definition does not appear in this file, then it is
! 565: ** not a published API of SQLite, is subject to change without
! 566: ** notice, and should not be referenced by programs that use SQLite.
! 567: **
! 568: ** Some of the definitions that are in this file are marked as
! 569: ** "experimental". Experimental interfaces are normally new
! 570: ** features recently added to SQLite. We do not anticipate changes
! 571: ** to experimental interfaces but reserve the right to make minor changes
! 572: ** if experience from use "in the wild" suggest such changes are prudent.
! 573: **
! 574: ** The official C-language API documentation for SQLite is derived
! 575: ** from comments in this file. This file is the authoritative source
! 576: ** on how SQLite interfaces are suppose to operate.
! 577: **
! 578: ** The name of this file under configuration management is "sqlite.h.in".
! 579: ** The makefile makes some minor changes to this file (such as inserting
! 580: ** the version number) and changes its name to "sqlite3.h" as
! 581: ** part of the build process.
! 582: */
! 583: #ifndef _SQLITE3_H_
! 584: #define _SQLITE3_H_
! 585: #include <stdarg.h> /* Needed for the definition of va_list */
! 586:
! 587: /*
! 588: ** Make sure we can call this stuff from C++.
! 589: */
! 590: #if 0
! 591: extern "C" {
! 592: #endif
! 593:
! 594:
! 595: /*
! 596: ** Add the ability to override 'extern'
! 597: */
! 598: #ifndef SQLITE_EXTERN
! 599: # define SQLITE_EXTERN extern
! 600: #endif
! 601:
! 602: #ifndef SQLITE_API
! 603: # define SQLITE_API
! 604: #endif
! 605:
! 606:
! 607: /*
! 608: ** These no-op macros are used in front of interfaces to mark those
! 609: ** interfaces as either deprecated or experimental. New applications
! 610: ** should not use deprecated interfaces - they are support for backwards
! 611: ** compatibility only. Application writers should be aware that
! 612: ** experimental interfaces are subject to change in point releases.
! 613: **
! 614: ** These macros used to resolve to various kinds of compiler magic that
! 615: ** would generate warning messages when they were used. But that
! 616: ** compiler magic ended up generating such a flurry of bug reports
! 617: ** that we have taken it all out and gone back to using simple
! 618: ** noop macros.
! 619: */
! 620: #define SQLITE_DEPRECATED
! 621: #define SQLITE_EXPERIMENTAL
! 622:
! 623: /*
! 624: ** Ensure these symbols were not defined by some previous header file.
! 625: */
! 626: #ifdef SQLITE_VERSION
! 627: # undef SQLITE_VERSION
! 628: #endif
! 629: #ifdef SQLITE_VERSION_NUMBER
! 630: # undef SQLITE_VERSION_NUMBER
! 631: #endif
! 632:
! 633: /*
! 634: ** CAPI3REF: Compile-Time Library Version Numbers
! 635: **
! 636: ** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
! 637: ** evaluates to a string literal that is the SQLite version in the
! 638: ** format "X.Y.Z" where X is the major version number (always 3 for
! 639: ** SQLite3) and Y is the minor version number and Z is the release number.)^
! 640: ** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
! 641: ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
! 642: ** numbers used in [SQLITE_VERSION].)^
! 643: ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
! 644: ** be larger than the release from which it is derived. Either Y will
! 645: ** be held constant and Z will be incremented or else Y will be incremented
! 646: ** and Z will be reset to zero.
! 647: **
! 648: ** Since version 3.6.18, SQLite source code has been stored in the
! 649: ** <a href="http://www.fossil-scm.org/">Fossil configuration management
! 650: ** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
! 651: ** a string which identifies a particular check-in of SQLite
! 652: ** within its configuration management system. ^The SQLITE_SOURCE_ID
! 653: ** string contains the date and time of the check-in (UTC) and an SHA1
! 654: ** hash of the entire source tree.
! 655: **
! 656: ** See also: [sqlite3_libversion()],
! 657: ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
! 658: ** [sqlite_version()] and [sqlite_source_id()].
! 659: */
! 660: #define SQLITE_VERSION "3.7.10"
! 661: #define SQLITE_VERSION_NUMBER 3007010
! 662: #define SQLITE_SOURCE_ID "2012-01-16 13:28:40 ebd01a8deffb5024a5d7494eef800d2366d97204"
! 663:
! 664: /*
! 665: ** CAPI3REF: Run-Time Library Version Numbers
! 666: ** KEYWORDS: sqlite3_version, sqlite3_sourceid
! 667: **
! 668: ** These interfaces provide the same information as the [SQLITE_VERSION],
! 669: ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
! 670: ** but are associated with the library instead of the header file. ^(Cautious
! 671: ** programmers might include assert() statements in their application to
! 672: ** verify that values returned by these interfaces match the macros in
! 673: ** the header, and thus insure that the application is
! 674: ** compiled with matching library and header files.
! 675: **
! 676: ** <blockquote><pre>
! 677: ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
! 678: ** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
! 679: ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
! 680: ** </pre></blockquote>)^
! 681: **
! 682: ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
! 683: ** macro. ^The sqlite3_libversion() function returns a pointer to the
! 684: ** to the sqlite3_version[] string constant. The sqlite3_libversion()
! 685: ** function is provided for use in DLLs since DLL users usually do not have
! 686: ** direct access to string constants within the DLL. ^The
! 687: ** sqlite3_libversion_number() function returns an integer equal to
! 688: ** [SQLITE_VERSION_NUMBER]. ^The sqlite3_sourceid() function returns
! 689: ** a pointer to a string constant whose value is the same as the
! 690: ** [SQLITE_SOURCE_ID] C preprocessor macro.
! 691: **
! 692: ** See also: [sqlite_version()] and [sqlite_source_id()].
! 693: */
! 694: SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
! 695: SQLITE_API const char *sqlite3_libversion(void);
! 696: SQLITE_API const char *sqlite3_sourceid(void);
! 697: SQLITE_API int sqlite3_libversion_number(void);
! 698:
! 699: /*
! 700: ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
! 701: **
! 702: ** ^The sqlite3_compileoption_used() function returns 0 or 1
! 703: ** indicating whether the specified option was defined at
! 704: ** compile time. ^The SQLITE_ prefix may be omitted from the
! 705: ** option name passed to sqlite3_compileoption_used().
! 706: **
! 707: ** ^The sqlite3_compileoption_get() function allows iterating
! 708: ** over the list of options that were defined at compile time by
! 709: ** returning the N-th compile time option string. ^If N is out of range,
! 710: ** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
! 711: ** prefix is omitted from any strings returned by
! 712: ** sqlite3_compileoption_get().
! 713: **
! 714: ** ^Support for the diagnostic functions sqlite3_compileoption_used()
! 715: ** and sqlite3_compileoption_get() may be omitted by specifying the
! 716: ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
! 717: **
! 718: ** See also: SQL functions [sqlite_compileoption_used()] and
! 719: ** [sqlite_compileoption_get()] and the [compile_options pragma].
! 720: */
! 721: #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
! 722: SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
! 723: SQLITE_API const char *sqlite3_compileoption_get(int N);
! 724: #endif
! 725:
! 726: /*
! 727: ** CAPI3REF: Test To See If The Library Is Threadsafe
! 728: **
! 729: ** ^The sqlite3_threadsafe() function returns zero if and only if
! 730: ** SQLite was compiled with mutexing code omitted due to the
! 731: ** [SQLITE_THREADSAFE] compile-time option being set to 0.
! 732: **
! 733: ** SQLite can be compiled with or without mutexes. When
! 734: ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
! 735: ** are enabled and SQLite is threadsafe. When the
! 736: ** [SQLITE_THREADSAFE] macro is 0,
! 737: ** the mutexes are omitted. Without the mutexes, it is not safe
! 738: ** to use SQLite concurrently from more than one thread.
! 739: **
! 740: ** Enabling mutexes incurs a measurable performance penalty.
! 741: ** So if speed is of utmost importance, it makes sense to disable
! 742: ** the mutexes. But for maximum safety, mutexes should be enabled.
! 743: ** ^The default behavior is for mutexes to be enabled.
! 744: **
! 745: ** This interface can be used by an application to make sure that the
! 746: ** version of SQLite that it is linking against was compiled with
! 747: ** the desired setting of the [SQLITE_THREADSAFE] macro.
! 748: **
! 749: ** This interface only reports on the compile-time mutex setting
! 750: ** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
! 751: ** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
! 752: ** can be fully or partially disabled using a call to [sqlite3_config()]
! 753: ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
! 754: ** or [SQLITE_CONFIG_MUTEX]. ^(The return value of the
! 755: ** sqlite3_threadsafe() function shows only the compile-time setting of
! 756: ** thread safety, not any run-time changes to that setting made by
! 757: ** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
! 758: ** is unchanged by calls to sqlite3_config().)^
! 759: **
! 760: ** See the [threading mode] documentation for additional information.
! 761: */
! 762: SQLITE_API int sqlite3_threadsafe(void);
! 763:
! 764: /*
! 765: ** CAPI3REF: Database Connection Handle
! 766: ** KEYWORDS: {database connection} {database connections}
! 767: **
! 768: ** Each open SQLite database is represented by a pointer to an instance of
! 769: ** the opaque structure named "sqlite3". It is useful to think of an sqlite3
! 770: ** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
! 771: ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
! 772: ** is its destructor. There are many other interfaces (such as
! 773: ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
! 774: ** [sqlite3_busy_timeout()] to name but three) that are methods on an
! 775: ** sqlite3 object.
! 776: */
! 777: typedef struct sqlite3 sqlite3;
! 778:
! 779: /*
! 780: ** CAPI3REF: 64-Bit Integer Types
! 781: ** KEYWORDS: sqlite_int64 sqlite_uint64
! 782: **
! 783: ** Because there is no cross-platform way to specify 64-bit integer types
! 784: ** SQLite includes typedefs for 64-bit signed and unsigned integers.
! 785: **
! 786: ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
! 787: ** The sqlite_int64 and sqlite_uint64 types are supported for backwards
! 788: ** compatibility only.
! 789: **
! 790: ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
! 791: ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
! 792: ** sqlite3_uint64 and sqlite_uint64 types can store integer values
! 793: ** between 0 and +18446744073709551615 inclusive.
! 794: */
! 795: #ifdef SQLITE_INT64_TYPE
! 796: typedef SQLITE_INT64_TYPE sqlite_int64;
! 797: typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
! 798: #elif defined(_MSC_VER) || defined(__BORLANDC__)
! 799: typedef __int64 sqlite_int64;
! 800: typedef unsigned __int64 sqlite_uint64;
! 801: #else
! 802: typedef long long int sqlite_int64;
! 803: typedef unsigned long long int sqlite_uint64;
! 804: #endif
! 805: typedef sqlite_int64 sqlite3_int64;
! 806: typedef sqlite_uint64 sqlite3_uint64;
! 807:
! 808: /*
! 809: ** If compiling for a processor that lacks floating point support,
! 810: ** substitute integer for floating-point.
! 811: */
! 812: #ifdef SQLITE_OMIT_FLOATING_POINT
! 813: # define double sqlite3_int64
! 814: #endif
! 815:
! 816: /*
! 817: ** CAPI3REF: Closing A Database Connection
! 818: **
! 819: ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
! 820: ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
! 821: ** successfully destroyed and all associated resources are deallocated.
! 822: **
! 823: ** Applications must [sqlite3_finalize | finalize] all [prepared statements]
! 824: ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
! 825: ** the [sqlite3] object prior to attempting to close the object. ^If
! 826: ** sqlite3_close() is called on a [database connection] that still has
! 827: ** outstanding [prepared statements] or [BLOB handles], then it returns
! 828: ** SQLITE_BUSY.
! 829: **
! 830: ** ^If [sqlite3_close()] is invoked while a transaction is open,
! 831: ** the transaction is automatically rolled back.
! 832: **
! 833: ** The C parameter to [sqlite3_close(C)] must be either a NULL
! 834: ** pointer or an [sqlite3] object pointer obtained
! 835: ** from [sqlite3_open()], [sqlite3_open16()], or
! 836: ** [sqlite3_open_v2()], and not previously closed.
! 837: ** ^Calling sqlite3_close() with a NULL pointer argument is a
! 838: ** harmless no-op.
! 839: */
! 840: SQLITE_API int sqlite3_close(sqlite3 *);
! 841:
! 842: /*
! 843: ** The type for a callback function.
! 844: ** This is legacy and deprecated. It is included for historical
! 845: ** compatibility and is not documented.
! 846: */
! 847: typedef int (*sqlite3_callback)(void*,int,char**, char**);
! 848:
! 849: /*
! 850: ** CAPI3REF: One-Step Query Execution Interface
! 851: **
! 852: ** The sqlite3_exec() interface is a convenience wrapper around
! 853: ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
! 854: ** that allows an application to run multiple statements of SQL
! 855: ** without having to use a lot of C code.
! 856: **
! 857: ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
! 858: ** semicolon-separate SQL statements passed into its 2nd argument,
! 859: ** in the context of the [database connection] passed in as its 1st
! 860: ** argument. ^If the callback function of the 3rd argument to
! 861: ** sqlite3_exec() is not NULL, then it is invoked for each result row
! 862: ** coming out of the evaluated SQL statements. ^The 4th argument to
! 863: ** sqlite3_exec() is relayed through to the 1st argument of each
! 864: ** callback invocation. ^If the callback pointer to sqlite3_exec()
! 865: ** is NULL, then no callback is ever invoked and result rows are
! 866: ** ignored.
! 867: **
! 868: ** ^If an error occurs while evaluating the SQL statements passed into
! 869: ** sqlite3_exec(), then execution of the current statement stops and
! 870: ** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
! 871: ** is not NULL then any error message is written into memory obtained
! 872: ** from [sqlite3_malloc()] and passed back through the 5th parameter.
! 873: ** To avoid memory leaks, the application should invoke [sqlite3_free()]
! 874: ** on error message strings returned through the 5th parameter of
! 875: ** of sqlite3_exec() after the error message string is no longer needed.
! 876: ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
! 877: ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
! 878: ** NULL before returning.
! 879: **
! 880: ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
! 881: ** routine returns SQLITE_ABORT without invoking the callback again and
! 882: ** without running any subsequent SQL statements.
! 883: **
! 884: ** ^The 2nd argument to the sqlite3_exec() callback function is the
! 885: ** number of columns in the result. ^The 3rd argument to the sqlite3_exec()
! 886: ** callback is an array of pointers to strings obtained as if from
! 887: ** [sqlite3_column_text()], one for each column. ^If an element of a
! 888: ** result row is NULL then the corresponding string pointer for the
! 889: ** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
! 890: ** sqlite3_exec() callback is an array of pointers to strings where each
! 891: ** entry represents the name of corresponding result column as obtained
! 892: ** from [sqlite3_column_name()].
! 893: **
! 894: ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
! 895: ** to an empty string, or a pointer that contains only whitespace and/or
! 896: ** SQL comments, then no SQL statements are evaluated and the database
! 897: ** is not changed.
! 898: **
! 899: ** Restrictions:
! 900: **
! 901: ** <ul>
! 902: ** <li> The application must insure that the 1st parameter to sqlite3_exec()
! 903: ** is a valid and open [database connection].
! 904: ** <li> The application must not close [database connection] specified by
! 905: ** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
! 906: ** <li> The application must not modify the SQL statement text passed into
! 907: ** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
! 908: ** </ul>
! 909: */
! 910: SQLITE_API int sqlite3_exec(
! 911: sqlite3*, /* An open database */
! 912: const char *sql, /* SQL to be evaluated */
! 913: int (*callback)(void*,int,char**,char**), /* Callback function */
! 914: void *, /* 1st argument to callback */
! 915: char **errmsg /* Error msg written here */
! 916: );
! 917:
! 918: /*
! 919: ** CAPI3REF: Result Codes
! 920: ** KEYWORDS: SQLITE_OK {error code} {error codes}
! 921: ** KEYWORDS: {result code} {result codes}
! 922: **
! 923: ** Many SQLite functions return an integer result code from the set shown
! 924: ** here in order to indicate success or failure.
! 925: **
! 926: ** New error codes may be added in future versions of SQLite.
! 927: **
! 928: ** See also: [SQLITE_IOERR_READ | extended result codes],
! 929: ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
! 930: */
! 931: #define SQLITE_OK 0 /* Successful result */
! 932: /* beginning-of-error-codes */
! 933: #define SQLITE_ERROR 1 /* SQL error or missing database */
! 934: #define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
! 935: #define SQLITE_PERM 3 /* Access permission denied */
! 936: #define SQLITE_ABORT 4 /* Callback routine requested an abort */
! 937: #define SQLITE_BUSY 5 /* The database file is locked */
! 938: #define SQLITE_LOCKED 6 /* A table in the database is locked */
! 939: #define SQLITE_NOMEM 7 /* A malloc() failed */
! 940: #define SQLITE_READONLY 8 /* Attempt to write a readonly database */
! 941: #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
! 942: #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
! 943: #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
! 944: #define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
! 945: #define SQLITE_FULL 13 /* Insertion failed because database is full */
! 946: #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
! 947: #define SQLITE_PROTOCOL 15 /* Database lock protocol error */
! 948: #define SQLITE_EMPTY 16 /* Database is empty */
! 949: #define SQLITE_SCHEMA 17 /* The database schema changed */
! 950: #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
! 951: #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
! 952: #define SQLITE_MISMATCH 20 /* Data type mismatch */
! 953: #define SQLITE_MISUSE 21 /* Library used incorrectly */
! 954: #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
! 955: #define SQLITE_AUTH 23 /* Authorization denied */
! 956: #define SQLITE_FORMAT 24 /* Auxiliary database format error */
! 957: #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
! 958: #define SQLITE_NOTADB 26 /* File opened that is not a database file */
! 959: #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
! 960: #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
! 961: /* end-of-error-codes */
! 962:
! 963: /*
! 964: ** CAPI3REF: Extended Result Codes
! 965: ** KEYWORDS: {extended error code} {extended error codes}
! 966: ** KEYWORDS: {extended result code} {extended result codes}
! 967: **
! 968: ** In its default configuration, SQLite API routines return one of 26 integer
! 969: ** [SQLITE_OK | result codes]. However, experience has shown that many of
! 970: ** these result codes are too coarse-grained. They do not provide as
! 971: ** much information about problems as programmers might like. In an effort to
! 972: ** address this, newer versions of SQLite (version 3.3.8 and later) include
! 973: ** support for additional result codes that provide more detailed information
! 974: ** about errors. The extended result codes are enabled or disabled
! 975: ** on a per database connection basis using the
! 976: ** [sqlite3_extended_result_codes()] API.
! 977: **
! 978: ** Some of the available extended result codes are listed here.
! 979: ** One may expect the number of extended result codes will be expand
! 980: ** over time. Software that uses extended result codes should expect
! 981: ** to see new result codes in future releases of SQLite.
! 982: **
! 983: ** The SQLITE_OK result code will never be extended. It will always
! 984: ** be exactly zero.
! 985: */
! 986: #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
! 987: #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
! 988: #define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
! 989: #define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
! 990: #define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8))
! 991: #define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8))
! 992: #define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8))
! 993: #define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8))
! 994: #define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8))
! 995: #define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8))
! 996: #define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8))
! 997: #define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8))
! 998: #define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
! 999: #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
! 1000: #define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
! 1001: #define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
! 1002: #define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
! 1003: #define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8))
! 1004: #define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
! 1005: #define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
! 1006: #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
! 1007: #define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
! 1008: #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
! 1009: #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
! 1010: #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
! 1011: #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
! 1012: #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
! 1013: #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
! 1014:
! 1015: /*
! 1016: ** CAPI3REF: Flags For File Open Operations
! 1017: **
! 1018: ** These bit values are intended for use in the
! 1019: ** 3rd parameter to the [sqlite3_open_v2()] interface and
! 1020: ** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
! 1021: */
! 1022: #define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */
! 1023: #define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
! 1024: #define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */
! 1025: #define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */
! 1026: #define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */
! 1027: #define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */
! 1028: #define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */
! 1029: #define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */
! 1030: #define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */
! 1031: #define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */
! 1032: #define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */
! 1033: #define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */
! 1034: #define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */
! 1035: #define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */
! 1036: #define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */
! 1037: #define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */
! 1038: #define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */
! 1039: #define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */
! 1040: #define SQLITE_OPEN_WAL 0x00080000 /* VFS only */
! 1041:
! 1042: /* Reserved: 0x00F00000 */
! 1043:
! 1044: /*
! 1045: ** CAPI3REF: Device Characteristics
! 1046: **
! 1047: ** The xDeviceCharacteristics method of the [sqlite3_io_methods]
! 1048: ** object returns an integer which is a vector of the these
! 1049: ** bit values expressing I/O characteristics of the mass storage
! 1050: ** device that holds the file that the [sqlite3_io_methods]
! 1051: ** refers to.
! 1052: **
! 1053: ** The SQLITE_IOCAP_ATOMIC property means that all writes of
! 1054: ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
! 1055: ** mean that writes of blocks that are nnn bytes in size and
! 1056: ** are aligned to an address which is an integer multiple of
! 1057: ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
! 1058: ** that when data is appended to a file, the data is appended
! 1059: ** first then the size of the file is extended, never the other
! 1060: ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
! 1061: ** information is written to disk in the same order as calls
! 1062: ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
! 1063: ** after reboot following a crash or power loss, the only bytes in a
! 1064: ** file that were written at the application level might have changed
! 1065: ** and that adjacent bytes, even bytes within the same sector are
! 1066: ** guaranteed to be unchanged.
! 1067: */
! 1068: #define SQLITE_IOCAP_ATOMIC 0x00000001
! 1069: #define SQLITE_IOCAP_ATOMIC512 0x00000002
! 1070: #define SQLITE_IOCAP_ATOMIC1K 0x00000004
! 1071: #define SQLITE_IOCAP_ATOMIC2K 0x00000008
! 1072: #define SQLITE_IOCAP_ATOMIC4K 0x00000010
! 1073: #define SQLITE_IOCAP_ATOMIC8K 0x00000020
! 1074: #define SQLITE_IOCAP_ATOMIC16K 0x00000040
! 1075: #define SQLITE_IOCAP_ATOMIC32K 0x00000080
! 1076: #define SQLITE_IOCAP_ATOMIC64K 0x00000100
! 1077: #define SQLITE_IOCAP_SAFE_APPEND 0x00000200
! 1078: #define SQLITE_IOCAP_SEQUENTIAL 0x00000400
! 1079: #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
! 1080: #define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
! 1081:
! 1082: /*
! 1083: ** CAPI3REF: File Locking Levels
! 1084: **
! 1085: ** SQLite uses one of these integer values as the second
! 1086: ** argument to calls it makes to the xLock() and xUnlock() methods
! 1087: ** of an [sqlite3_io_methods] object.
! 1088: */
! 1089: #define SQLITE_LOCK_NONE 0
! 1090: #define SQLITE_LOCK_SHARED 1
! 1091: #define SQLITE_LOCK_RESERVED 2
! 1092: #define SQLITE_LOCK_PENDING 3
! 1093: #define SQLITE_LOCK_EXCLUSIVE 4
! 1094:
! 1095: /*
! 1096: ** CAPI3REF: Synchronization Type Flags
! 1097: **
! 1098: ** When SQLite invokes the xSync() method of an
! 1099: ** [sqlite3_io_methods] object it uses a combination of
! 1100: ** these integer values as the second argument.
! 1101: **
! 1102: ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
! 1103: ** sync operation only needs to flush data to mass storage. Inode
! 1104: ** information need not be flushed. If the lower four bits of the flag
! 1105: ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
! 1106: ** If the lower four bits equal SQLITE_SYNC_FULL, that means
! 1107: ** to use Mac OS X style fullsync instead of fsync().
! 1108: **
! 1109: ** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
! 1110: ** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
! 1111: ** settings. The [synchronous pragma] determines when calls to the
! 1112: ** xSync VFS method occur and applies uniformly across all platforms.
! 1113: ** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
! 1114: ** energetic or rigorous or forceful the sync operations are and
! 1115: ** only make a difference on Mac OSX for the default SQLite code.
! 1116: ** (Third-party VFS implementations might also make the distinction
! 1117: ** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
! 1118: ** operating systems natively supported by SQLite, only Mac OSX
! 1119: ** cares about the difference.)
! 1120: */
! 1121: #define SQLITE_SYNC_NORMAL 0x00002
! 1122: #define SQLITE_SYNC_FULL 0x00003
! 1123: #define SQLITE_SYNC_DATAONLY 0x00010
! 1124:
! 1125: /*
! 1126: ** CAPI3REF: OS Interface Open File Handle
! 1127: **
! 1128: ** An [sqlite3_file] object represents an open file in the
! 1129: ** [sqlite3_vfs | OS interface layer]. Individual OS interface
! 1130: ** implementations will
! 1131: ** want to subclass this object by appending additional fields
! 1132: ** for their own use. The pMethods entry is a pointer to an
! 1133: ** [sqlite3_io_methods] object that defines methods for performing
! 1134: ** I/O operations on the open file.
! 1135: */
! 1136: typedef struct sqlite3_file sqlite3_file;
! 1137: struct sqlite3_file {
! 1138: const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
! 1139: };
! 1140:
! 1141: /*
! 1142: ** CAPI3REF: OS Interface File Virtual Methods Object
! 1143: **
! 1144: ** Every file opened by the [sqlite3_vfs.xOpen] method populates an
! 1145: ** [sqlite3_file] object (or, more commonly, a subclass of the
! 1146: ** [sqlite3_file] object) with a pointer to an instance of this object.
! 1147: ** This object defines the methods used to perform various operations
! 1148: ** against the open file represented by the [sqlite3_file] object.
! 1149: **
! 1150: ** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
! 1151: ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
! 1152: ** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The
! 1153: ** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
! 1154: ** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
! 1155: ** to NULL.
! 1156: **
! 1157: ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
! 1158: ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
! 1159: ** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
! 1160: ** flag may be ORed in to indicate that only the data of the file
! 1161: ** and not its inode needs to be synced.
! 1162: **
! 1163: ** The integer values to xLock() and xUnlock() are one of
! 1164: ** <ul>
! 1165: ** <li> [SQLITE_LOCK_NONE],
! 1166: ** <li> [SQLITE_LOCK_SHARED],
! 1167: ** <li> [SQLITE_LOCK_RESERVED],
! 1168: ** <li> [SQLITE_LOCK_PENDING], or
! 1169: ** <li> [SQLITE_LOCK_EXCLUSIVE].
! 1170: ** </ul>
! 1171: ** xLock() increases the lock. xUnlock() decreases the lock.
! 1172: ** The xCheckReservedLock() method checks whether any database connection,
! 1173: ** either in this process or in some other process, is holding a RESERVED,
! 1174: ** PENDING, or EXCLUSIVE lock on the file. It returns true
! 1175: ** if such a lock exists and false otherwise.
! 1176: **
! 1177: ** The xFileControl() method is a generic interface that allows custom
! 1178: ** VFS implementations to directly control an open file using the
! 1179: ** [sqlite3_file_control()] interface. The second "op" argument is an
! 1180: ** integer opcode. The third argument is a generic pointer intended to
! 1181: ** point to a structure that may contain arguments or space in which to
! 1182: ** write return values. Potential uses for xFileControl() might be
! 1183: ** functions to enable blocking locks with timeouts, to change the
! 1184: ** locking strategy (for example to use dot-file locks), to inquire
! 1185: ** about the status of a lock, or to break stale locks. The SQLite
! 1186: ** core reserves all opcodes less than 100 for its own use.
! 1187: ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
! 1188: ** Applications that define a custom xFileControl method should use opcodes
! 1189: ** greater than 100 to avoid conflicts. VFS implementations should
! 1190: ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
! 1191: ** recognize.
! 1192: **
! 1193: ** The xSectorSize() method returns the sector size of the
! 1194: ** device that underlies the file. The sector size is the
! 1195: ** minimum write that can be performed without disturbing
! 1196: ** other bytes in the file. The xDeviceCharacteristics()
! 1197: ** method returns a bit vector describing behaviors of the
! 1198: ** underlying device:
! 1199: **
! 1200: ** <ul>
! 1201: ** <li> [SQLITE_IOCAP_ATOMIC]
! 1202: ** <li> [SQLITE_IOCAP_ATOMIC512]
! 1203: ** <li> [SQLITE_IOCAP_ATOMIC1K]
! 1204: ** <li> [SQLITE_IOCAP_ATOMIC2K]
! 1205: ** <li> [SQLITE_IOCAP_ATOMIC4K]
! 1206: ** <li> [SQLITE_IOCAP_ATOMIC8K]
! 1207: ** <li> [SQLITE_IOCAP_ATOMIC16K]
! 1208: ** <li> [SQLITE_IOCAP_ATOMIC32K]
! 1209: ** <li> [SQLITE_IOCAP_ATOMIC64K]
! 1210: ** <li> [SQLITE_IOCAP_SAFE_APPEND]
! 1211: ** <li> [SQLITE_IOCAP_SEQUENTIAL]
! 1212: ** </ul>
! 1213: **
! 1214: ** The SQLITE_IOCAP_ATOMIC property means that all writes of
! 1215: ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
! 1216: ** mean that writes of blocks that are nnn bytes in size and
! 1217: ** are aligned to an address which is an integer multiple of
! 1218: ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
! 1219: ** that when data is appended to a file, the data is appended
! 1220: ** first then the size of the file is extended, never the other
! 1221: ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
! 1222: ** information is written to disk in the same order as calls
! 1223: ** to xWrite().
! 1224: **
! 1225: ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
! 1226: ** in the unread portions of the buffer with zeros. A VFS that
! 1227: ** fails to zero-fill short reads might seem to work. However,
! 1228: ** failure to zero-fill short reads will eventually lead to
! 1229: ** database corruption.
! 1230: */
! 1231: typedef struct sqlite3_io_methods sqlite3_io_methods;
! 1232: struct sqlite3_io_methods {
! 1233: int iVersion;
! 1234: int (*xClose)(sqlite3_file*);
! 1235: int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
! 1236: int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
! 1237: int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
! 1238: int (*xSync)(sqlite3_file*, int flags);
! 1239: int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
! 1240: int (*xLock)(sqlite3_file*, int);
! 1241: int (*xUnlock)(sqlite3_file*, int);
! 1242: int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
! 1243: int (*xFileControl)(sqlite3_file*, int op, void *pArg);
! 1244: int (*xSectorSize)(sqlite3_file*);
! 1245: int (*xDeviceCharacteristics)(sqlite3_file*);
! 1246: /* Methods above are valid for version 1 */
! 1247: int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
! 1248: int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
! 1249: void (*xShmBarrier)(sqlite3_file*);
! 1250: int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
! 1251: /* Methods above are valid for version 2 */
! 1252: /* Additional methods may be added in future releases */
! 1253: };
! 1254:
! 1255: /*
! 1256: ** CAPI3REF: Standard File Control Opcodes
! 1257: **
! 1258: ** These integer constants are opcodes for the xFileControl method
! 1259: ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
! 1260: ** interface.
! 1261: **
! 1262: ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
! 1263: ** opcode causes the xFileControl method to write the current state of
! 1264: ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
! 1265: ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
! 1266: ** into an integer that the pArg argument points to. This capability
! 1267: ** is used during testing and only needs to be supported when SQLITE_TEST
! 1268: ** is defined.
! 1269: **
! 1270: ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
! 1271: ** layer a hint of how large the database file will grow to be during the
! 1272: ** current transaction. This hint is not guaranteed to be accurate but it
! 1273: ** is often close. The underlying VFS might choose to preallocate database
! 1274: ** file space based on this hint in order to help writes to the database
! 1275: ** file run faster.
! 1276: **
! 1277: ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
! 1278: ** extends and truncates the database file in chunks of a size specified
! 1279: ** by the user. The fourth argument to [sqlite3_file_control()] should
! 1280: ** point to an integer (type int) containing the new chunk-size to use
! 1281: ** for the nominated database. Allocating database file space in large
! 1282: ** chunks (say 1MB at a time), may reduce file-system fragmentation and
! 1283: ** improve performance on some systems.
! 1284: **
! 1285: ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
! 1286: ** to the [sqlite3_file] object associated with a particular database
! 1287: ** connection. See the [sqlite3_file_control()] documentation for
! 1288: ** additional information.
! 1289: **
! 1290: ** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
! 1291: ** SQLite and sent to all VFSes in place of a call to the xSync method
! 1292: ** when the database connection has [PRAGMA synchronous] set to OFF.)^
! 1293: ** Some specialized VFSes need this signal in order to operate correctly
! 1294: ** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most
! 1295: ** VFSes do not need this signal and should silently ignore this opcode.
! 1296: ** Applications should not call [sqlite3_file_control()] with this
! 1297: ** opcode as doing so may disrupt the operation of the specialized VFSes
! 1298: ** that do require it.
! 1299: **
! 1300: ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
! 1301: ** retry counts and intervals for certain disk I/O operations for the
! 1302: ** windows [VFS] in order to provide robustness in the presence of
! 1303: ** anti-virus programs. By default, the windows VFS will retry file read,
! 1304: ** file write, and file delete operations up to 10 times, with a delay
! 1305: ** of 25 milliseconds before the first retry and with the delay increasing
! 1306: ** by an additional 25 milliseconds with each subsequent retry. This
! 1307: ** opcode allows these two values (10 retries and 25 milliseconds of delay)
! 1308: ** to be adjusted. The values are changed for all database connections
! 1309: ** within the same process. The argument is a pointer to an array of two
! 1310: ** integers where the first integer i the new retry count and the second
! 1311: ** integer is the delay. If either integer is negative, then the setting
! 1312: ** is not changed but instead the prior value of that setting is written
! 1313: ** into the array entry, allowing the current retry settings to be
! 1314: ** interrogated. The zDbName parameter is ignored.
! 1315: **
! 1316: ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
! 1317: ** persistent [WAL | Write AHead Log] setting. By default, the auxiliary
! 1318: ** write ahead log and shared memory files used for transaction control
! 1319: ** are automatically deleted when the latest connection to the database
! 1320: ** closes. Setting persistent WAL mode causes those files to persist after
! 1321: ** close. Persisting the files is useful when other processes that do not
! 1322: ** have write permission on the directory containing the database file want
! 1323: ** to read the database file, as the WAL and shared memory files must exist
! 1324: ** in order for the database to be readable. The fourth parameter to
! 1325: ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
! 1326: ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
! 1327: ** WAL mode. If the integer is -1, then it is overwritten with the current
! 1328: ** WAL persistence setting.
! 1329: **
! 1330: ** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
! 1331: ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
! 1332: ** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
! 1333: ** xDeviceCharacteristics methods. The fourth parameter to
! 1334: ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
! 1335: ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
! 1336: ** mode. If the integer is -1, then it is overwritten with the current
! 1337: ** zero-damage mode setting.
! 1338: **
! 1339: ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
! 1340: ** a write transaction to indicate that, unless it is rolled back for some
! 1341: ** reason, the entire database file will be overwritten by the current
! 1342: ** transaction. This is used by VACUUM operations.
! 1343: **
! 1344: ** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
! 1345: ** all [VFSes] in the VFS stack. The names are of all VFS shims and the
! 1346: ** final bottom-level VFS are written into memory obtained from
! 1347: ** [sqlite3_malloc()] and the result is stored in the char* variable
! 1348: ** that the fourth parameter of [sqlite3_file_control()] points to.
! 1349: ** The caller is responsible for freeing the memory when done. As with
! 1350: ** all file-control actions, there is no guarantee that this will actually
! 1351: ** do anything. Callers should initialize the char* variable to a NULL
! 1352: ** pointer in case this file-control is not implemented. This file-control
! 1353: ** is intended for diagnostic use only.
! 1354: */
! 1355: #define SQLITE_FCNTL_LOCKSTATE 1
! 1356: #define SQLITE_GET_LOCKPROXYFILE 2
! 1357: #define SQLITE_SET_LOCKPROXYFILE 3
! 1358: #define SQLITE_LAST_ERRNO 4
! 1359: #define SQLITE_FCNTL_SIZE_HINT 5
! 1360: #define SQLITE_FCNTL_CHUNK_SIZE 6
! 1361: #define SQLITE_FCNTL_FILE_POINTER 7
! 1362: #define SQLITE_FCNTL_SYNC_OMITTED 8
! 1363: #define SQLITE_FCNTL_WIN32_AV_RETRY 9
! 1364: #define SQLITE_FCNTL_PERSIST_WAL 10
! 1365: #define SQLITE_FCNTL_OVERWRITE 11
! 1366: #define SQLITE_FCNTL_VFSNAME 12
! 1367: #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
! 1368:
! 1369: /*
! 1370: ** CAPI3REF: Mutex Handle
! 1371: **
! 1372: ** The mutex module within SQLite defines [sqlite3_mutex] to be an
! 1373: ** abstract type for a mutex object. The SQLite core never looks
! 1374: ** at the internal representation of an [sqlite3_mutex]. It only
! 1375: ** deals with pointers to the [sqlite3_mutex] object.
! 1376: **
! 1377: ** Mutexes are created using [sqlite3_mutex_alloc()].
! 1378: */
! 1379: typedef struct sqlite3_mutex sqlite3_mutex;
! 1380:
! 1381: /*
! 1382: ** CAPI3REF: OS Interface Object
! 1383: **
! 1384: ** An instance of the sqlite3_vfs object defines the interface between
! 1385: ** the SQLite core and the underlying operating system. The "vfs"
! 1386: ** in the name of the object stands for "virtual file system". See
! 1387: ** the [VFS | VFS documentation] for further information.
! 1388: **
! 1389: ** The value of the iVersion field is initially 1 but may be larger in
! 1390: ** future versions of SQLite. Additional fields may be appended to this
! 1391: ** object when the iVersion value is increased. Note that the structure
! 1392: ** of the sqlite3_vfs object changes in the transaction between
! 1393: ** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
! 1394: ** modified.
! 1395: **
! 1396: ** The szOsFile field is the size of the subclassed [sqlite3_file]
! 1397: ** structure used by this VFS. mxPathname is the maximum length of
! 1398: ** a pathname in this VFS.
! 1399: **
! 1400: ** Registered sqlite3_vfs objects are kept on a linked list formed by
! 1401: ** the pNext pointer. The [sqlite3_vfs_register()]
! 1402: ** and [sqlite3_vfs_unregister()] interfaces manage this list
! 1403: ** in a thread-safe way. The [sqlite3_vfs_find()] interface
! 1404: ** searches the list. Neither the application code nor the VFS
! 1405: ** implementation should use the pNext pointer.
! 1406: **
! 1407: ** The pNext field is the only field in the sqlite3_vfs
! 1408: ** structure that SQLite will ever modify. SQLite will only access
! 1409: ** or modify this field while holding a particular static mutex.
! 1410: ** The application should never modify anything within the sqlite3_vfs
! 1411: ** object once the object has been registered.
! 1412: **
! 1413: ** The zName field holds the name of the VFS module. The name must
! 1414: ** be unique across all VFS modules.
! 1415: **
! 1416: ** [[sqlite3_vfs.xOpen]]
! 1417: ** ^SQLite guarantees that the zFilename parameter to xOpen
! 1418: ** is either a NULL pointer or string obtained
! 1419: ** from xFullPathname() with an optional suffix added.
! 1420: ** ^If a suffix is added to the zFilename parameter, it will
! 1421: ** consist of a single "-" character followed by no more than
! 1422: ** 11 alphanumeric and/or "-" characters.
! 1423: ** ^SQLite further guarantees that
! 1424: ** the string will be valid and unchanged until xClose() is
! 1425: ** called. Because of the previous sentence,
! 1426: ** the [sqlite3_file] can safely store a pointer to the
! 1427: ** filename if it needs to remember the filename for some reason.
! 1428: ** If the zFilename parameter to xOpen is a NULL pointer then xOpen
! 1429: ** must invent its own temporary name for the file. ^Whenever the
! 1430: ** xFilename parameter is NULL it will also be the case that the
! 1431: ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
! 1432: **
! 1433: ** The flags argument to xOpen() includes all bits set in
! 1434: ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
! 1435: ** or [sqlite3_open16()] is used, then flags includes at least
! 1436: ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
! 1437: ** If xOpen() opens a file read-only then it sets *pOutFlags to
! 1438: ** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set.
! 1439: **
! 1440: ** ^(SQLite will also add one of the following flags to the xOpen()
! 1441: ** call, depending on the object being opened:
! 1442: **
! 1443: ** <ul>
! 1444: ** <li> [SQLITE_OPEN_MAIN_DB]
! 1445: ** <li> [SQLITE_OPEN_MAIN_JOURNAL]
! 1446: ** <li> [SQLITE_OPEN_TEMP_DB]
! 1447: ** <li> [SQLITE_OPEN_TEMP_JOURNAL]
! 1448: ** <li> [SQLITE_OPEN_TRANSIENT_DB]
! 1449: ** <li> [SQLITE_OPEN_SUBJOURNAL]
! 1450: ** <li> [SQLITE_OPEN_MASTER_JOURNAL]
! 1451: ** <li> [SQLITE_OPEN_WAL]
! 1452: ** </ul>)^
! 1453: **
! 1454: ** The file I/O implementation can use the object type flags to
! 1455: ** change the way it deals with files. For example, an application
! 1456: ** that does not care about crash recovery or rollback might make
! 1457: ** the open of a journal file a no-op. Writes to this journal would
! 1458: ** also be no-ops, and any attempt to read the journal would return
! 1459: ** SQLITE_IOERR. Or the implementation might recognize that a database
! 1460: ** file will be doing page-aligned sector reads and writes in a random
! 1461: ** order and set up its I/O subsystem accordingly.
! 1462: **
! 1463: ** SQLite might also add one of the following flags to the xOpen method:
! 1464: **
! 1465: ** <ul>
! 1466: ** <li> [SQLITE_OPEN_DELETEONCLOSE]
! 1467: ** <li> [SQLITE_OPEN_EXCLUSIVE]
! 1468: ** </ul>
! 1469: **
! 1470: ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
! 1471: ** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE]
! 1472: ** will be set for TEMP databases and their journals, transient
! 1473: ** databases, and subjournals.
! 1474: **
! 1475: ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
! 1476: ** with the [SQLITE_OPEN_CREATE] flag, which are both directly
! 1477: ** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
! 1478: ** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
! 1479: ** SQLITE_OPEN_CREATE, is used to indicate that file should always
! 1480: ** be created, and that it is an error if it already exists.
! 1481: ** It is <i>not</i> used to indicate the file should be opened
! 1482: ** for exclusive access.
! 1483: **
! 1484: ** ^At least szOsFile bytes of memory are allocated by SQLite
! 1485: ** to hold the [sqlite3_file] structure passed as the third
! 1486: ** argument to xOpen. The xOpen method does not have to
! 1487: ** allocate the structure; it should just fill it in. Note that
! 1488: ** the xOpen method must set the sqlite3_file.pMethods to either
! 1489: ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
! 1490: ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
! 1491: ** element will be valid after xOpen returns regardless of the success
! 1492: ** or failure of the xOpen call.
! 1493: **
! 1494: ** [[sqlite3_vfs.xAccess]]
! 1495: ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
! 1496: ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
! 1497: ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
! 1498: ** to test whether a file is at least readable. The file can be a
! 1499: ** directory.
! 1500: **
! 1501: ** ^SQLite will always allocate at least mxPathname+1 bytes for the
! 1502: ** output buffer xFullPathname. The exact size of the output buffer
! 1503: ** is also passed as a parameter to both methods. If the output buffer
! 1504: ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
! 1505: ** handled as a fatal error by SQLite, vfs implementations should endeavor
! 1506: ** to prevent this by setting mxPathname to a sufficiently large value.
! 1507: **
! 1508: ** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
! 1509: ** interfaces are not strictly a part of the filesystem, but they are
! 1510: ** included in the VFS structure for completeness.
! 1511: ** The xRandomness() function attempts to return nBytes bytes
! 1512: ** of good-quality randomness into zOut. The return value is
! 1513: ** the actual number of bytes of randomness obtained.
! 1514: ** The xSleep() method causes the calling thread to sleep for at
! 1515: ** least the number of microseconds given. ^The xCurrentTime()
! 1516: ** method returns a Julian Day Number for the current date and time as
! 1517: ** a floating point value.
! 1518: ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
! 1519: ** Day Number multiplied by 86400000 (the number of milliseconds in
! 1520: ** a 24-hour day).
! 1521: ** ^SQLite will use the xCurrentTimeInt64() method to get the current
! 1522: ** date and time if that method is available (if iVersion is 2 or
! 1523: ** greater and the function pointer is not NULL) and will fall back
! 1524: ** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
! 1525: **
! 1526: ** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
! 1527: ** are not used by the SQLite core. These optional interfaces are provided
! 1528: ** by some VFSes to facilitate testing of the VFS code. By overriding
! 1529: ** system calls with functions under its control, a test program can
! 1530: ** simulate faults and error conditions that would otherwise be difficult
! 1531: ** or impossible to induce. The set of system calls that can be overridden
! 1532: ** varies from one VFS to another, and from one version of the same VFS to the
! 1533: ** next. Applications that use these interfaces must be prepared for any
! 1534: ** or all of these interfaces to be NULL or for their behavior to change
! 1535: ** from one release to the next. Applications must not attempt to access
! 1536: ** any of these methods if the iVersion of the VFS is less than 3.
! 1537: */
! 1538: typedef struct sqlite3_vfs sqlite3_vfs;
! 1539: typedef void (*sqlite3_syscall_ptr)(void);
! 1540: struct sqlite3_vfs {
! 1541: int iVersion; /* Structure version number (currently 3) */
! 1542: int szOsFile; /* Size of subclassed sqlite3_file */
! 1543: int mxPathname; /* Maximum file pathname length */
! 1544: sqlite3_vfs *pNext; /* Next registered VFS */
! 1545: const char *zName; /* Name of this virtual file system */
! 1546: void *pAppData; /* Pointer to application-specific data */
! 1547: int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
! 1548: int flags, int *pOutFlags);
! 1549: int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
! 1550: int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
! 1551: int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
! 1552: void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
! 1553: void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
! 1554: void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
! 1555: void (*xDlClose)(sqlite3_vfs*, void*);
! 1556: int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
! 1557: int (*xSleep)(sqlite3_vfs*, int microseconds);
! 1558: int (*xCurrentTime)(sqlite3_vfs*, double*);
! 1559: int (*xGetLastError)(sqlite3_vfs*, int, char *);
! 1560: /*
! 1561: ** The methods above are in version 1 of the sqlite_vfs object
! 1562: ** definition. Those that follow are added in version 2 or later
! 1563: */
! 1564: int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
! 1565: /*
! 1566: ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
! 1567: ** Those below are for version 3 and greater.
! 1568: */
! 1569: int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
! 1570: sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
! 1571: const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
! 1572: /*
! 1573: ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
! 1574: ** New fields may be appended in figure versions. The iVersion
! 1575: ** value will increment whenever this happens.
! 1576: */
! 1577: };
! 1578:
! 1579: /*
! 1580: ** CAPI3REF: Flags for the xAccess VFS method
! 1581: **
! 1582: ** These integer constants can be used as the third parameter to
! 1583: ** the xAccess method of an [sqlite3_vfs] object. They determine
! 1584: ** what kind of permissions the xAccess method is looking for.
! 1585: ** With SQLITE_ACCESS_EXISTS, the xAccess method
! 1586: ** simply checks whether the file exists.
! 1587: ** With SQLITE_ACCESS_READWRITE, the xAccess method
! 1588: ** checks whether the named directory is both readable and writable
! 1589: ** (in other words, if files can be added, removed, and renamed within
! 1590: ** the directory).
! 1591: ** The SQLITE_ACCESS_READWRITE constant is currently used only by the
! 1592: ** [temp_store_directory pragma], though this could change in a future
! 1593: ** release of SQLite.
! 1594: ** With SQLITE_ACCESS_READ, the xAccess method
! 1595: ** checks whether the file is readable. The SQLITE_ACCESS_READ constant is
! 1596: ** currently unused, though it might be used in a future release of
! 1597: ** SQLite.
! 1598: */
! 1599: #define SQLITE_ACCESS_EXISTS 0
! 1600: #define SQLITE_ACCESS_READWRITE 1 /* Used by PRAGMA temp_store_directory */
! 1601: #define SQLITE_ACCESS_READ 2 /* Unused */
! 1602:
! 1603: /*
! 1604: ** CAPI3REF: Flags for the xShmLock VFS method
! 1605: **
! 1606: ** These integer constants define the various locking operations
! 1607: ** allowed by the xShmLock method of [sqlite3_io_methods]. The
! 1608: ** following are the only legal combinations of flags to the
! 1609: ** xShmLock method:
! 1610: **
! 1611: ** <ul>
! 1612: ** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
! 1613: ** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
! 1614: ** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
! 1615: ** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
! 1616: ** </ul>
! 1617: **
! 1618: ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
! 1619: ** was given no the corresponding lock.
! 1620: **
! 1621: ** The xShmLock method can transition between unlocked and SHARED or
! 1622: ** between unlocked and EXCLUSIVE. It cannot transition between SHARED
! 1623: ** and EXCLUSIVE.
! 1624: */
! 1625: #define SQLITE_SHM_UNLOCK 1
! 1626: #define SQLITE_SHM_LOCK 2
! 1627: #define SQLITE_SHM_SHARED 4
! 1628: #define SQLITE_SHM_EXCLUSIVE 8
! 1629:
! 1630: /*
! 1631: ** CAPI3REF: Maximum xShmLock index
! 1632: **
! 1633: ** The xShmLock method on [sqlite3_io_methods] may use values
! 1634: ** between 0 and this upper bound as its "offset" argument.
! 1635: ** The SQLite core will never attempt to acquire or release a
! 1636: ** lock outside of this range
! 1637: */
! 1638: #define SQLITE_SHM_NLOCK 8
! 1639:
! 1640:
! 1641: /*
! 1642: ** CAPI3REF: Initialize The SQLite Library
! 1643: **
! 1644: ** ^The sqlite3_initialize() routine initializes the
! 1645: ** SQLite library. ^The sqlite3_shutdown() routine
! 1646: ** deallocates any resources that were allocated by sqlite3_initialize().
! 1647: ** These routines are designed to aid in process initialization and
! 1648: ** shutdown on embedded systems. Workstation applications using
! 1649: ** SQLite normally do not need to invoke either of these routines.
! 1650: **
! 1651: ** A call to sqlite3_initialize() is an "effective" call if it is
! 1652: ** the first time sqlite3_initialize() is invoked during the lifetime of
! 1653: ** the process, or if it is the first time sqlite3_initialize() is invoked
! 1654: ** following a call to sqlite3_shutdown(). ^(Only an effective call
! 1655: ** of sqlite3_initialize() does any initialization. All other calls
! 1656: ** are harmless no-ops.)^
! 1657: **
! 1658: ** A call to sqlite3_shutdown() is an "effective" call if it is the first
! 1659: ** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only
! 1660: ** an effective call to sqlite3_shutdown() does any deinitialization.
! 1661: ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
! 1662: **
! 1663: ** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
! 1664: ** is not. The sqlite3_shutdown() interface must only be called from a
! 1665: ** single thread. All open [database connections] must be closed and all
! 1666: ** other SQLite resources must be deallocated prior to invoking
! 1667: ** sqlite3_shutdown().
! 1668: **
! 1669: ** Among other things, ^sqlite3_initialize() will invoke
! 1670: ** sqlite3_os_init(). Similarly, ^sqlite3_shutdown()
! 1671: ** will invoke sqlite3_os_end().
! 1672: **
! 1673: ** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
! 1674: ** ^If for some reason, sqlite3_initialize() is unable to initialize
! 1675: ** the library (perhaps it is unable to allocate a needed resource such
! 1676: ** as a mutex) it returns an [error code] other than [SQLITE_OK].
! 1677: **
! 1678: ** ^The sqlite3_initialize() routine is called internally by many other
! 1679: ** SQLite interfaces so that an application usually does not need to
! 1680: ** invoke sqlite3_initialize() directly. For example, [sqlite3_open()]
! 1681: ** calls sqlite3_initialize() so the SQLite library will be automatically
! 1682: ** initialized when [sqlite3_open()] is called if it has not be initialized
! 1683: ** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
! 1684: ** compile-time option, then the automatic calls to sqlite3_initialize()
! 1685: ** are omitted and the application must call sqlite3_initialize() directly
! 1686: ** prior to using any other SQLite interface. For maximum portability,
! 1687: ** it is recommended that applications always invoke sqlite3_initialize()
! 1688: ** directly prior to using any other SQLite interface. Future releases
! 1689: ** of SQLite may require this. In other words, the behavior exhibited
! 1690: ** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
! 1691: ** default behavior in some future release of SQLite.
! 1692: **
! 1693: ** The sqlite3_os_init() routine does operating-system specific
! 1694: ** initialization of the SQLite library. The sqlite3_os_end()
! 1695: ** routine undoes the effect of sqlite3_os_init(). Typical tasks
! 1696: ** performed by these routines include allocation or deallocation
! 1697: ** of static resources, initialization of global variables,
! 1698: ** setting up a default [sqlite3_vfs] module, or setting up
! 1699: ** a default configuration using [sqlite3_config()].
! 1700: **
! 1701: ** The application should never invoke either sqlite3_os_init()
! 1702: ** or sqlite3_os_end() directly. The application should only invoke
! 1703: ** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init()
! 1704: ** interface is called automatically by sqlite3_initialize() and
! 1705: ** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate
! 1706: ** implementations for sqlite3_os_init() and sqlite3_os_end()
! 1707: ** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
! 1708: ** When [custom builds | built for other platforms]
! 1709: ** (using the [SQLITE_OS_OTHER=1] compile-time
! 1710: ** option) the application must supply a suitable implementation for
! 1711: ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
! 1712: ** implementation of sqlite3_os_init() or sqlite3_os_end()
! 1713: ** must return [SQLITE_OK] on success and some other [error code] upon
! 1714: ** failure.
! 1715: */
! 1716: SQLITE_API int sqlite3_initialize(void);
! 1717: SQLITE_API int sqlite3_shutdown(void);
! 1718: SQLITE_API int sqlite3_os_init(void);
! 1719: SQLITE_API int sqlite3_os_end(void);
! 1720:
! 1721: /*
! 1722: ** CAPI3REF: Configuring The SQLite Library
! 1723: **
! 1724: ** The sqlite3_config() interface is used to make global configuration
! 1725: ** changes to SQLite in order to tune SQLite to the specific needs of
! 1726: ** the application. The default configuration is recommended for most
! 1727: ** applications and so this routine is usually not necessary. It is
! 1728: ** provided to support rare applications with unusual needs.
! 1729: **
! 1730: ** The sqlite3_config() interface is not threadsafe. The application
! 1731: ** must insure that no other SQLite interfaces are invoked by other
! 1732: ** threads while sqlite3_config() is running. Furthermore, sqlite3_config()
! 1733: ** may only be invoked prior to library initialization using
! 1734: ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
! 1735: ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
! 1736: ** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
! 1737: ** Note, however, that ^sqlite3_config() can be called as part of the
! 1738: ** implementation of an application-defined [sqlite3_os_init()].
! 1739: **
! 1740: ** The first argument to sqlite3_config() is an integer
! 1741: ** [configuration option] that determines
! 1742: ** what property of SQLite is to be configured. Subsequent arguments
! 1743: ** vary depending on the [configuration option]
! 1744: ** in the first argument.
! 1745: **
! 1746: ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
! 1747: ** ^If the option is unknown or SQLite is unable to set the option
! 1748: ** then this routine returns a non-zero [error code].
! 1749: */
! 1750: SQLITE_API int sqlite3_config(int, ...);
! 1751:
! 1752: /*
! 1753: ** CAPI3REF: Configure database connections
! 1754: **
! 1755: ** The sqlite3_db_config() interface is used to make configuration
! 1756: ** changes to a [database connection]. The interface is similar to
! 1757: ** [sqlite3_config()] except that the changes apply to a single
! 1758: ** [database connection] (specified in the first argument).
! 1759: **
! 1760: ** The second argument to sqlite3_db_config(D,V,...) is the
! 1761: ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
! 1762: ** that indicates what aspect of the [database connection] is being configured.
! 1763: ** Subsequent arguments vary depending on the configuration verb.
! 1764: **
! 1765: ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
! 1766: ** the call is considered successful.
! 1767: */
! 1768: SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
! 1769:
! 1770: /*
! 1771: ** CAPI3REF: Memory Allocation Routines
! 1772: **
! 1773: ** An instance of this object defines the interface between SQLite
! 1774: ** and low-level memory allocation routines.
! 1775: **
! 1776: ** This object is used in only one place in the SQLite interface.
! 1777: ** A pointer to an instance of this object is the argument to
! 1778: ** [sqlite3_config()] when the configuration option is
! 1779: ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
! 1780: ** By creating an instance of this object
! 1781: ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
! 1782: ** during configuration, an application can specify an alternative
! 1783: ** memory allocation subsystem for SQLite to use for all of its
! 1784: ** dynamic memory needs.
! 1785: **
! 1786: ** Note that SQLite comes with several [built-in memory allocators]
! 1787: ** that are perfectly adequate for the overwhelming majority of applications
! 1788: ** and that this object is only useful to a tiny minority of applications
! 1789: ** with specialized memory allocation requirements. This object is
! 1790: ** also used during testing of SQLite in order to specify an alternative
! 1791: ** memory allocator that simulates memory out-of-memory conditions in
! 1792: ** order to verify that SQLite recovers gracefully from such
! 1793: ** conditions.
! 1794: **
! 1795: ** The xMalloc, xRealloc, and xFree methods must work like the
! 1796: ** malloc(), realloc() and free() functions from the standard C library.
! 1797: ** ^SQLite guarantees that the second argument to
! 1798: ** xRealloc is always a value returned by a prior call to xRoundup.
! 1799: **
! 1800: ** xSize should return the allocated size of a memory allocation
! 1801: ** previously obtained from xMalloc or xRealloc. The allocated size
! 1802: ** is always at least as big as the requested size but may be larger.
! 1803: **
! 1804: ** The xRoundup method returns what would be the allocated size of
! 1805: ** a memory allocation given a particular requested size. Most memory
! 1806: ** allocators round up memory allocations at least to the next multiple
! 1807: ** of 8. Some allocators round up to a larger multiple or to a power of 2.
! 1808: ** Every memory allocation request coming in through [sqlite3_malloc()]
! 1809: ** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
! 1810: ** that causes the corresponding memory allocation to fail.
! 1811: **
! 1812: ** The xInit method initializes the memory allocator. (For example,
! 1813: ** it might allocate any require mutexes or initialize internal data
! 1814: ** structures. The xShutdown method is invoked (indirectly) by
! 1815: ** [sqlite3_shutdown()] and should deallocate any resources acquired
! 1816: ** by xInit. The pAppData pointer is used as the only parameter to
! 1817: ** xInit and xShutdown.
! 1818: **
! 1819: ** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
! 1820: ** the xInit method, so the xInit method need not be threadsafe. The
! 1821: ** xShutdown method is only called from [sqlite3_shutdown()] so it does
! 1822: ** not need to be threadsafe either. For all other methods, SQLite
! 1823: ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
! 1824: ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
! 1825: ** it is by default) and so the methods are automatically serialized.
! 1826: ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
! 1827: ** methods must be threadsafe or else make their own arrangements for
! 1828: ** serialization.
! 1829: **
! 1830: ** SQLite will never invoke xInit() more than once without an intervening
! 1831: ** call to xShutdown().
! 1832: */
! 1833: typedef struct sqlite3_mem_methods sqlite3_mem_methods;
! 1834: struct sqlite3_mem_methods {
! 1835: void *(*xMalloc)(int); /* Memory allocation function */
! 1836: void (*xFree)(void*); /* Free a prior allocation */
! 1837: void *(*xRealloc)(void*,int); /* Resize an allocation */
! 1838: int (*xSize)(void*); /* Return the size of an allocation */
! 1839: int (*xRoundup)(int); /* Round up request size to allocation size */
! 1840: int (*xInit)(void*); /* Initialize the memory allocator */
! 1841: void (*xShutdown)(void*); /* Deinitialize the memory allocator */
! 1842: void *pAppData; /* Argument to xInit() and xShutdown() */
! 1843: };
! 1844:
! 1845: /*
! 1846: ** CAPI3REF: Configuration Options
! 1847: ** KEYWORDS: {configuration option}
! 1848: **
! 1849: ** These constants are the available integer configuration options that
! 1850: ** can be passed as the first argument to the [sqlite3_config()] interface.
! 1851: **
! 1852: ** New configuration options may be added in future releases of SQLite.
! 1853: ** Existing configuration options might be discontinued. Applications
! 1854: ** should check the return code from [sqlite3_config()] to make sure that
! 1855: ** the call worked. The [sqlite3_config()] interface will return a
! 1856: ** non-zero [error code] if a discontinued or unsupported configuration option
! 1857: ** is invoked.
! 1858: **
! 1859: ** <dl>
! 1860: ** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
! 1861: ** <dd>There are no arguments to this option. ^This option sets the
! 1862: ** [threading mode] to Single-thread. In other words, it disables
! 1863: ** all mutexing and puts SQLite into a mode where it can only be used
! 1864: ** by a single thread. ^If SQLite is compiled with
! 1865: ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
! 1866: ** it is not possible to change the [threading mode] from its default
! 1867: ** value of Single-thread and so [sqlite3_config()] will return
! 1868: ** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
! 1869: ** configuration option.</dd>
! 1870: **
! 1871: ** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
! 1872: ** <dd>There are no arguments to this option. ^This option sets the
! 1873: ** [threading mode] to Multi-thread. In other words, it disables
! 1874: ** mutexing on [database connection] and [prepared statement] objects.
! 1875: ** The application is responsible for serializing access to
! 1876: ** [database connections] and [prepared statements]. But other mutexes
! 1877: ** are enabled so that SQLite will be safe to use in a multi-threaded
! 1878: ** environment as long as no two threads attempt to use the same
! 1879: ** [database connection] at the same time. ^If SQLite is compiled with
! 1880: ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
! 1881: ** it is not possible to set the Multi-thread [threading mode] and
! 1882: ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
! 1883: ** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
! 1884: **
! 1885: ** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
! 1886: ** <dd>There are no arguments to this option. ^This option sets the
! 1887: ** [threading mode] to Serialized. In other words, this option enables
! 1888: ** all mutexes including the recursive
! 1889: ** mutexes on [database connection] and [prepared statement] objects.
! 1890: ** In this mode (which is the default when SQLite is compiled with
! 1891: ** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
! 1892: ** to [database connections] and [prepared statements] so that the
! 1893: ** application is free to use the same [database connection] or the
! 1894: ** same [prepared statement] in different threads at the same time.
! 1895: ** ^If SQLite is compiled with
! 1896: ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
! 1897: ** it is not possible to set the Serialized [threading mode] and
! 1898: ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
! 1899: ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
! 1900: **
! 1901: ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
! 1902: ** <dd> ^(This option takes a single argument which is a pointer to an
! 1903: ** instance of the [sqlite3_mem_methods] structure. The argument specifies
! 1904: ** alternative low-level memory allocation routines to be used in place of
! 1905: ** the memory allocation routines built into SQLite.)^ ^SQLite makes
! 1906: ** its own private copy of the content of the [sqlite3_mem_methods] structure
! 1907: ** before the [sqlite3_config()] call returns.</dd>
! 1908: **
! 1909: ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
! 1910: ** <dd> ^(This option takes a single argument which is a pointer to an
! 1911: ** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods]
! 1912: ** structure is filled with the currently defined memory allocation routines.)^
! 1913: ** This option can be used to overload the default memory allocation
! 1914: ** routines with a wrapper that simulations memory allocation failure or
! 1915: ** tracks memory usage, for example. </dd>
! 1916: **
! 1917: ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
! 1918: ** <dd> ^This option takes single argument of type int, interpreted as a
! 1919: ** boolean, which enables or disables the collection of memory allocation
! 1920: ** statistics. ^(When memory allocation statistics are disabled, the
! 1921: ** following SQLite interfaces become non-operational:
! 1922: ** <ul>
! 1923: ** <li> [sqlite3_memory_used()]
! 1924: ** <li> [sqlite3_memory_highwater()]
! 1925: ** <li> [sqlite3_soft_heap_limit64()]
! 1926: ** <li> [sqlite3_status()]
! 1927: ** </ul>)^
! 1928: ** ^Memory allocation statistics are enabled by default unless SQLite is
! 1929: ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
! 1930: ** allocation statistics are disabled by default.
! 1931: ** </dd>
! 1932: **
! 1933: ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
! 1934: ** <dd> ^This option specifies a static memory buffer that SQLite can use for
! 1935: ** scratch memory. There are three arguments: A pointer an 8-byte
! 1936: ** aligned memory buffer from which the scratch allocations will be
! 1937: ** drawn, the size of each scratch allocation (sz),
! 1938: ** and the maximum number of scratch allocations (N). The sz
! 1939: ** argument must be a multiple of 16.
! 1940: ** The first argument must be a pointer to an 8-byte aligned buffer
! 1941: ** of at least sz*N bytes of memory.
! 1942: ** ^SQLite will use no more than two scratch buffers per thread. So
! 1943: ** N should be set to twice the expected maximum number of threads.
! 1944: ** ^SQLite will never require a scratch buffer that is more than 6
! 1945: ** times the database page size. ^If SQLite needs needs additional
! 1946: ** scratch memory beyond what is provided by this configuration option, then
! 1947: ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
! 1948: **
! 1949: ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
! 1950: ** <dd> ^This option specifies a static memory buffer that SQLite can use for
! 1951: ** the database page cache with the default page cache implementation.
! 1952: ** This configuration should not be used if an application-define page
! 1953: ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
! 1954: ** There are three arguments to this option: A pointer to 8-byte aligned
! 1955: ** memory, the size of each page buffer (sz), and the number of pages (N).
! 1956: ** The sz argument should be the size of the largest database page
! 1957: ** (a power of two between 512 and 32768) plus a little extra for each
! 1958: ** page header. ^The page header size is 20 to 40 bytes depending on
! 1959: ** the host architecture. ^It is harmless, apart from the wasted memory,
! 1960: ** to make sz a little too large. The first
! 1961: ** argument should point to an allocation of at least sz*N bytes of memory.
! 1962: ** ^SQLite will use the memory provided by the first argument to satisfy its
! 1963: ** memory needs for the first N pages that it adds to cache. ^If additional
! 1964: ** page cache memory is needed beyond what is provided by this option, then
! 1965: ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
! 1966: ** The pointer in the first argument must
! 1967: ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
! 1968: ** will be undefined.</dd>
! 1969: **
! 1970: ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
! 1971: ** <dd> ^This option specifies a static memory buffer that SQLite will use
! 1972: ** for all of its dynamic memory allocation needs beyond those provided
! 1973: ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
! 1974: ** There are three arguments: An 8-byte aligned pointer to the memory,
! 1975: ** the number of bytes in the memory buffer, and the minimum allocation size.
! 1976: ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
! 1977: ** to using its default memory allocator (the system malloc() implementation),
! 1978: ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
! 1979: ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
! 1980: ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
! 1981: ** allocator is engaged to handle all of SQLites memory allocation needs.
! 1982: ** The first pointer (the memory pointer) must be aligned to an 8-byte
! 1983: ** boundary or subsequent behavior of SQLite will be undefined.
! 1984: ** The minimum allocation size is capped at 2**12. Reasonable values
! 1985: ** for the minimum allocation size are 2**5 through 2**8.</dd>
! 1986: **
! 1987: ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
! 1988: ** <dd> ^(This option takes a single argument which is a pointer to an
! 1989: ** instance of the [sqlite3_mutex_methods] structure. The argument specifies
! 1990: ** alternative low-level mutex routines to be used in place
! 1991: ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the
! 1992: ** content of the [sqlite3_mutex_methods] structure before the call to
! 1993: ** [sqlite3_config()] returns. ^If SQLite is compiled with
! 1994: ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
! 1995: ** the entire mutexing subsystem is omitted from the build and hence calls to
! 1996: ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
! 1997: ** return [SQLITE_ERROR].</dd>
! 1998: **
! 1999: ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
! 2000: ** <dd> ^(This option takes a single argument which is a pointer to an
! 2001: ** instance of the [sqlite3_mutex_methods] structure. The
! 2002: ** [sqlite3_mutex_methods]
! 2003: ** structure is filled with the currently defined mutex routines.)^
! 2004: ** This option can be used to overload the default mutex allocation
! 2005: ** routines with a wrapper used to track mutex usage for performance
! 2006: ** profiling or testing, for example. ^If SQLite is compiled with
! 2007: ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
! 2008: ** the entire mutexing subsystem is omitted from the build and hence calls to
! 2009: ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
! 2010: ** return [SQLITE_ERROR].</dd>
! 2011: **
! 2012: ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
! 2013: ** <dd> ^(This option takes two arguments that determine the default
! 2014: ** memory allocation for the lookaside memory allocator on each
! 2015: ** [database connection]. The first argument is the
! 2016: ** size of each lookaside buffer slot and the second is the number of
! 2017: ** slots allocated to each database connection.)^ ^(This option sets the
! 2018: ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
! 2019: ** verb to [sqlite3_db_config()] can be used to change the lookaside
! 2020: ** configuration on individual connections.)^ </dd>
! 2021: **
! 2022: ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
! 2023: ** <dd> ^(This option takes a single argument which is a pointer to
! 2024: ** an [sqlite3_pcache_methods2] object. This object specifies the interface
! 2025: ** to a custom page cache implementation.)^ ^SQLite makes a copy of the
! 2026: ** object and uses it for page cache memory allocations.</dd>
! 2027: **
! 2028: ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
! 2029: ** <dd> ^(This option takes a single argument which is a pointer to an
! 2030: ** [sqlite3_pcache_methods2] object. SQLite copies of the current
! 2031: ** page cache implementation into that object.)^ </dd>
! 2032: **
! 2033: ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
! 2034: ** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
! 2035: ** function with a call signature of void(*)(void*,int,const char*),
! 2036: ** and a pointer to void. ^If the function pointer is not NULL, it is
! 2037: ** invoked by [sqlite3_log()] to process each logging event. ^If the
! 2038: ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
! 2039: ** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
! 2040: ** passed through as the first parameter to the application-defined logger
! 2041: ** function whenever that function is invoked. ^The second parameter to
! 2042: ** the logger function is a copy of the first parameter to the corresponding
! 2043: ** [sqlite3_log()] call and is intended to be a [result code] or an
! 2044: ** [extended result code]. ^The third parameter passed to the logger is
! 2045: ** log message after formatting via [sqlite3_snprintf()].
! 2046: ** The SQLite logging interface is not reentrant; the logger function
! 2047: ** supplied by the application must not invoke any SQLite interface.
! 2048: ** In a multi-threaded application, the application-defined logger
! 2049: ** function must be threadsafe. </dd>
! 2050: **
! 2051: ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
! 2052: ** <dd> This option takes a single argument of type int. If non-zero, then
! 2053: ** URI handling is globally enabled. If the parameter is zero, then URI handling
! 2054: ** is globally disabled. If URI handling is globally enabled, all filenames
! 2055: ** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
! 2056: ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
! 2057: ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
! 2058: ** connection is opened. If it is globally disabled, filenames are
! 2059: ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
! 2060: ** database connection is opened. By default, URI handling is globally
! 2061: ** disabled. The default value may be changed by compiling with the
! 2062: ** [SQLITE_USE_URI] symbol defined.
! 2063: **
! 2064: ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
! 2065: ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
! 2066: ** <dd> These options are obsolete and should not be used by new code.
! 2067: ** They are retained for backwards compatibility but are now no-ops.
! 2068: ** </dl>
! 2069: */
! 2070: #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
! 2071: #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
! 2072: #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
! 2073: #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
! 2074: #define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */
! 2075: #define SQLITE_CONFIG_SCRATCH 6 /* void*, int sz, int N */
! 2076: #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
! 2077: #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
! 2078: #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
! 2079: #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
! 2080: #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
! 2081: /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
! 2082: #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
! 2083: #define SQLITE_CONFIG_PCACHE 14 /* no-op */
! 2084: #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
! 2085: #define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
! 2086: #define SQLITE_CONFIG_URI 17 /* int */
! 2087: #define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
! 2088: #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
! 2089:
! 2090: /*
! 2091: ** CAPI3REF: Database Connection Configuration Options
! 2092: **
! 2093: ** These constants are the available integer configuration options that
! 2094: ** can be passed as the second argument to the [sqlite3_db_config()] interface.
! 2095: **
! 2096: ** New configuration options may be added in future releases of SQLite.
! 2097: ** Existing configuration options might be discontinued. Applications
! 2098: ** should check the return code from [sqlite3_db_config()] to make sure that
! 2099: ** the call worked. ^The [sqlite3_db_config()] interface will return a
! 2100: ** non-zero [error code] if a discontinued or unsupported configuration option
! 2101: ** is invoked.
! 2102: **
! 2103: ** <dl>
! 2104: ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
! 2105: ** <dd> ^This option takes three additional arguments that determine the
! 2106: ** [lookaside memory allocator] configuration for the [database connection].
! 2107: ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
! 2108: ** pointer to a memory buffer to use for lookaside memory.
! 2109: ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
! 2110: ** may be NULL in which case SQLite will allocate the
! 2111: ** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
! 2112: ** size of each lookaside buffer slot. ^The third argument is the number of
! 2113: ** slots. The size of the buffer in the first argument must be greater than
! 2114: ** or equal to the product of the second and third arguments. The buffer
! 2115: ** must be aligned to an 8-byte boundary. ^If the second argument to
! 2116: ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
! 2117: ** rounded down to the next smaller multiple of 8. ^(The lookaside memory
! 2118: ** configuration for a database connection can only be changed when that
! 2119: ** connection is not currently using lookaside memory, or in other words
! 2120: ** when the "current value" returned by
! 2121: ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
! 2122: ** Any attempt to change the lookaside memory configuration when lookaside
! 2123: ** memory is in use leaves the configuration unchanged and returns
! 2124: ** [SQLITE_BUSY].)^</dd>
! 2125: **
! 2126: ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
! 2127: ** <dd> ^This option is used to enable or disable the enforcement of
! 2128: ** [foreign key constraints]. There should be two additional arguments.
! 2129: ** The first argument is an integer which is 0 to disable FK enforcement,
! 2130: ** positive to enable FK enforcement or negative to leave FK enforcement
! 2131: ** unchanged. The second parameter is a pointer to an integer into which
! 2132: ** is written 0 or 1 to indicate whether FK enforcement is off or on
! 2133: ** following this call. The second parameter may be a NULL pointer, in
! 2134: ** which case the FK enforcement setting is not reported back. </dd>
! 2135: **
! 2136: ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
! 2137: ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
! 2138: ** There should be two additional arguments.
! 2139: ** The first argument is an integer which is 0 to disable triggers,
! 2140: ** positive to enable triggers or negative to leave the setting unchanged.
! 2141: ** The second parameter is a pointer to an integer into which
! 2142: ** is written 0 or 1 to indicate whether triggers are disabled or enabled
! 2143: ** following this call. The second parameter may be a NULL pointer, in
! 2144: ** which case the trigger setting is not reported back. </dd>
! 2145: **
! 2146: ** </dl>
! 2147: */
! 2148: #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
! 2149: #define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
! 2150: #define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */
! 2151:
! 2152:
! 2153: /*
! 2154: ** CAPI3REF: Enable Or Disable Extended Result Codes
! 2155: **
! 2156: ** ^The sqlite3_extended_result_codes() routine enables or disables the
! 2157: ** [extended result codes] feature of SQLite. ^The extended result
! 2158: ** codes are disabled by default for historical compatibility.
! 2159: */
! 2160: SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
! 2161:
! 2162: /*
! 2163: ** CAPI3REF: Last Insert Rowid
! 2164: **
! 2165: ** ^Each entry in an SQLite table has a unique 64-bit signed
! 2166: ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
! 2167: ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
! 2168: ** names are not also used by explicitly declared columns. ^If
! 2169: ** the table has a column of type [INTEGER PRIMARY KEY] then that column
! 2170: ** is another alias for the rowid.
! 2171: **
! 2172: ** ^This routine returns the [rowid] of the most recent
! 2173: ** successful [INSERT] into the database from the [database connection]
! 2174: ** in the first argument. ^As of SQLite version 3.7.7, this routines
! 2175: ** records the last insert rowid of both ordinary tables and [virtual tables].
! 2176: ** ^If no successful [INSERT]s
! 2177: ** have ever occurred on that database connection, zero is returned.
! 2178: **
! 2179: ** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
! 2180: ** method, then this routine will return the [rowid] of the inserted
! 2181: ** row as long as the trigger or virtual table method is running.
! 2182: ** But once the trigger or virtual table method ends, the value returned
! 2183: ** by this routine reverts to what it was before the trigger or virtual
! 2184: ** table method began.)^
! 2185: **
! 2186: ** ^An [INSERT] that fails due to a constraint violation is not a
! 2187: ** successful [INSERT] and does not change the value returned by this
! 2188: ** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
! 2189: ** and INSERT OR ABORT make no changes to the return value of this
! 2190: ** routine when their insertion fails. ^(When INSERT OR REPLACE
! 2191: ** encounters a constraint violation, it does not fail. The
! 2192: ** INSERT continues to completion after deleting rows that caused
! 2193: ** the constraint problem so INSERT OR REPLACE will always change
! 2194: ** the return value of this interface.)^
! 2195: **
! 2196: ** ^For the purposes of this routine, an [INSERT] is considered to
! 2197: ** be successful even if it is subsequently rolled back.
! 2198: **
! 2199: ** This function is accessible to SQL statements via the
! 2200: ** [last_insert_rowid() SQL function].
! 2201: **
! 2202: ** If a separate thread performs a new [INSERT] on the same
! 2203: ** database connection while the [sqlite3_last_insert_rowid()]
! 2204: ** function is running and thus changes the last insert [rowid],
! 2205: ** then the value returned by [sqlite3_last_insert_rowid()] is
! 2206: ** unpredictable and might not equal either the old or the new
! 2207: ** last insert [rowid].
! 2208: */
! 2209: SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
! 2210:
! 2211: /*
! 2212: ** CAPI3REF: Count The Number Of Rows Modified
! 2213: **
! 2214: ** ^This function returns the number of database rows that were changed
! 2215: ** or inserted or deleted by the most recently completed SQL statement
! 2216: ** on the [database connection] specified by the first parameter.
! 2217: ** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
! 2218: ** or [DELETE] statement are counted. Auxiliary changes caused by
! 2219: ** triggers or [foreign key actions] are not counted.)^ Use the
! 2220: ** [sqlite3_total_changes()] function to find the total number of changes
! 2221: ** including changes caused by triggers and foreign key actions.
! 2222: **
! 2223: ** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
! 2224: ** are not counted. Only real table changes are counted.
! 2225: **
! 2226: ** ^(A "row change" is a change to a single row of a single table
! 2227: ** caused by an INSERT, DELETE, or UPDATE statement. Rows that
! 2228: ** are changed as side effects of [REPLACE] constraint resolution,
! 2229: ** rollback, ABORT processing, [DROP TABLE], or by any other
! 2230: ** mechanisms do not count as direct row changes.)^
! 2231: **
! 2232: ** A "trigger context" is a scope of execution that begins and
! 2233: ** ends with the script of a [CREATE TRIGGER | trigger].
! 2234: ** Most SQL statements are
! 2235: ** evaluated outside of any trigger. This is the "top level"
! 2236: ** trigger context. If a trigger fires from the top level, a
! 2237: ** new trigger context is entered for the duration of that one
! 2238: ** trigger. Subtriggers create subcontexts for their duration.
! 2239: **
! 2240: ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
! 2241: ** not create a new trigger context.
! 2242: **
! 2243: ** ^This function returns the number of direct row changes in the
! 2244: ** most recent INSERT, UPDATE, or DELETE statement within the same
! 2245: ** trigger context.
! 2246: **
! 2247: ** ^Thus, when called from the top level, this function returns the
! 2248: ** number of changes in the most recent INSERT, UPDATE, or DELETE
! 2249: ** that also occurred at the top level. ^(Within the body of a trigger,
! 2250: ** the sqlite3_changes() interface can be called to find the number of
! 2251: ** changes in the most recently completed INSERT, UPDATE, or DELETE
! 2252: ** statement within the body of the same trigger.
! 2253: ** However, the number returned does not include changes
! 2254: ** caused by subtriggers since those have their own context.)^
! 2255: **
! 2256: ** See also the [sqlite3_total_changes()] interface, the
! 2257: ** [count_changes pragma], and the [changes() SQL function].
! 2258: **
! 2259: ** If a separate thread makes changes on the same database connection
! 2260: ** while [sqlite3_changes()] is running then the value returned
! 2261: ** is unpredictable and not meaningful.
! 2262: */
! 2263: SQLITE_API int sqlite3_changes(sqlite3*);
! 2264:
! 2265: /*
! 2266: ** CAPI3REF: Total Number Of Rows Modified
! 2267: **
! 2268: ** ^This function returns the number of row changes caused by [INSERT],
! 2269: ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
! 2270: ** ^(The count returned by sqlite3_total_changes() includes all changes
! 2271: ** from all [CREATE TRIGGER | trigger] contexts and changes made by
! 2272: ** [foreign key actions]. However,
! 2273: ** the count does not include changes used to implement [REPLACE] constraints,
! 2274: ** do rollbacks or ABORT processing, or [DROP TABLE] processing. The
! 2275: ** count does not include rows of views that fire an [INSTEAD OF trigger],
! 2276: ** though if the INSTEAD OF trigger makes changes of its own, those changes
! 2277: ** are counted.)^
! 2278: ** ^The sqlite3_total_changes() function counts the changes as soon as
! 2279: ** the statement that makes them is completed (when the statement handle
! 2280: ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
! 2281: **
! 2282: ** See also the [sqlite3_changes()] interface, the
! 2283: ** [count_changes pragma], and the [total_changes() SQL function].
! 2284: **
! 2285: ** If a separate thread makes changes on the same database connection
! 2286: ** while [sqlite3_total_changes()] is running then the value
! 2287: ** returned is unpredictable and not meaningful.
! 2288: */
! 2289: SQLITE_API int sqlite3_total_changes(sqlite3*);
! 2290:
! 2291: /*
! 2292: ** CAPI3REF: Interrupt A Long-Running Query
! 2293: **
! 2294: ** ^This function causes any pending database operation to abort and
! 2295: ** return at its earliest opportunity. This routine is typically
! 2296: ** called in response to a user action such as pressing "Cancel"
! 2297: ** or Ctrl-C where the user wants a long query operation to halt
! 2298: ** immediately.
! 2299: **
! 2300: ** ^It is safe to call this routine from a thread different from the
! 2301: ** thread that is currently running the database operation. But it
! 2302: ** is not safe to call this routine with a [database connection] that
! 2303: ** is closed or might close before sqlite3_interrupt() returns.
! 2304: **
! 2305: ** ^If an SQL operation is very nearly finished at the time when
! 2306: ** sqlite3_interrupt() is called, then it might not have an opportunity
! 2307: ** to be interrupted and might continue to completion.
! 2308: **
! 2309: ** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
! 2310: ** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
! 2311: ** that is inside an explicit transaction, then the entire transaction
! 2312: ** will be rolled back automatically.
! 2313: **
! 2314: ** ^The sqlite3_interrupt(D) call is in effect until all currently running
! 2315: ** SQL statements on [database connection] D complete. ^Any new SQL statements
! 2316: ** that are started after the sqlite3_interrupt() call and before the
! 2317: ** running statements reaches zero are interrupted as if they had been
! 2318: ** running prior to the sqlite3_interrupt() call. ^New SQL statements
! 2319: ** that are started after the running statement count reaches zero are
! 2320: ** not effected by the sqlite3_interrupt().
! 2321: ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
! 2322: ** SQL statements is a no-op and has no effect on SQL statements
! 2323: ** that are started after the sqlite3_interrupt() call returns.
! 2324: **
! 2325: ** If the database connection closes while [sqlite3_interrupt()]
! 2326: ** is running then bad things will likely happen.
! 2327: */
! 2328: SQLITE_API void sqlite3_interrupt(sqlite3*);
! 2329:
! 2330: /*
! 2331: ** CAPI3REF: Determine If An SQL Statement Is Complete
! 2332: **
! 2333: ** These routines are useful during command-line input to determine if the
! 2334: ** currently entered text seems to form a complete SQL statement or
! 2335: ** if additional input is needed before sending the text into
! 2336: ** SQLite for parsing. ^These routines return 1 if the input string
! 2337: ** appears to be a complete SQL statement. ^A statement is judged to be
! 2338: ** complete if it ends with a semicolon token and is not a prefix of a
! 2339: ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
! 2340: ** string literals or quoted identifier names or comments are not
! 2341: ** independent tokens (they are part of the token in which they are
! 2342: ** embedded) and thus do not count as a statement terminator. ^Whitespace
! 2343: ** and comments that follow the final semicolon are ignored.
! 2344: **
! 2345: ** ^These routines return 0 if the statement is incomplete. ^If a
! 2346: ** memory allocation fails, then SQLITE_NOMEM is returned.
! 2347: **
! 2348: ** ^These routines do not parse the SQL statements thus
! 2349: ** will not detect syntactically incorrect SQL.
! 2350: **
! 2351: ** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
! 2352: ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
! 2353: ** automatically by sqlite3_complete16(). If that initialization fails,
! 2354: ** then the return value from sqlite3_complete16() will be non-zero
! 2355: ** regardless of whether or not the input SQL is complete.)^
! 2356: **
! 2357: ** The input to [sqlite3_complete()] must be a zero-terminated
! 2358: ** UTF-8 string.
! 2359: **
! 2360: ** The input to [sqlite3_complete16()] must be a zero-terminated
! 2361: ** UTF-16 string in native byte order.
! 2362: */
! 2363: SQLITE_API int sqlite3_complete(const char *sql);
! 2364: SQLITE_API int sqlite3_complete16(const void *sql);
! 2365:
! 2366: /*
! 2367: ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
! 2368: **
! 2369: ** ^This routine sets a callback function that might be invoked whenever
! 2370: ** an attempt is made to open a database table that another thread
! 2371: ** or process has locked.
! 2372: **
! 2373: ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
! 2374: ** is returned immediately upon encountering the lock. ^If the busy callback
! 2375: ** is not NULL, then the callback might be invoked with two arguments.
! 2376: **
! 2377: ** ^The first argument to the busy handler is a copy of the void* pointer which
! 2378: ** is the third argument to sqlite3_busy_handler(). ^The second argument to
! 2379: ** the busy handler callback is the number of times that the busy handler has
! 2380: ** been invoked for this locking event. ^If the
! 2381: ** busy callback returns 0, then no additional attempts are made to
! 2382: ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
! 2383: ** ^If the callback returns non-zero, then another attempt
! 2384: ** is made to open the database for reading and the cycle repeats.
! 2385: **
! 2386: ** The presence of a busy handler does not guarantee that it will be invoked
! 2387: ** when there is lock contention. ^If SQLite determines that invoking the busy
! 2388: ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
! 2389: ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
! 2390: ** Consider a scenario where one process is holding a read lock that
! 2391: ** it is trying to promote to a reserved lock and
! 2392: ** a second process is holding a reserved lock that it is trying
! 2393: ** to promote to an exclusive lock. The first process cannot proceed
! 2394: ** because it is blocked by the second and the second process cannot
! 2395: ** proceed because it is blocked by the first. If both processes
! 2396: ** invoke the busy handlers, neither will make any progress. Therefore,
! 2397: ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
! 2398: ** will induce the first process to release its read lock and allow
! 2399: ** the second process to proceed.
! 2400: **
! 2401: ** ^The default busy callback is NULL.
! 2402: **
! 2403: ** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
! 2404: ** when SQLite is in the middle of a large transaction where all the
! 2405: ** changes will not fit into the in-memory cache. SQLite will
! 2406: ** already hold a RESERVED lock on the database file, but it needs
! 2407: ** to promote this lock to EXCLUSIVE so that it can spill cache
! 2408: ** pages into the database file without harm to concurrent
! 2409: ** readers. ^If it is unable to promote the lock, then the in-memory
! 2410: ** cache will be left in an inconsistent state and so the error
! 2411: ** code is promoted from the relatively benign [SQLITE_BUSY] to
! 2412: ** the more severe [SQLITE_IOERR_BLOCKED]. ^This error code promotion
! 2413: ** forces an automatic rollback of the changes. See the
! 2414: ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
! 2415: ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
! 2416: ** this is important.
! 2417: **
! 2418: ** ^(There can only be a single busy handler defined for each
! 2419: ** [database connection]. Setting a new busy handler clears any
! 2420: ** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
! 2421: ** will also set or clear the busy handler.
! 2422: **
! 2423: ** The busy callback should not take any actions which modify the
! 2424: ** database connection that invoked the busy handler. Any such actions
! 2425: ** result in undefined behavior.
! 2426: **
! 2427: ** A busy handler must not close the database connection
! 2428: ** or [prepared statement] that invoked the busy handler.
! 2429: */
! 2430: SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
! 2431:
! 2432: /*
! 2433: ** CAPI3REF: Set A Busy Timeout
! 2434: **
! 2435: ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
! 2436: ** for a specified amount of time when a table is locked. ^The handler
! 2437: ** will sleep multiple times until at least "ms" milliseconds of sleeping
! 2438: ** have accumulated. ^After at least "ms" milliseconds of sleeping,
! 2439: ** the handler returns 0 which causes [sqlite3_step()] to return
! 2440: ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
! 2441: **
! 2442: ** ^Calling this routine with an argument less than or equal to zero
! 2443: ** turns off all busy handlers.
! 2444: **
! 2445: ** ^(There can only be a single busy handler for a particular
! 2446: ** [database connection] any any given moment. If another busy handler
! 2447: ** was defined (using [sqlite3_busy_handler()]) prior to calling
! 2448: ** this routine, that other busy handler is cleared.)^
! 2449: */
! 2450: SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
! 2451:
! 2452: /*
! 2453: ** CAPI3REF: Convenience Routines For Running Queries
! 2454: **
! 2455: ** This is a legacy interface that is preserved for backwards compatibility.
! 2456: ** Use of this interface is not recommended.
! 2457: **
! 2458: ** Definition: A <b>result table</b> is memory data structure created by the
! 2459: ** [sqlite3_get_table()] interface. A result table records the
! 2460: ** complete query results from one or more queries.
! 2461: **
! 2462: ** The table conceptually has a number of rows and columns. But
! 2463: ** these numbers are not part of the result table itself. These
! 2464: ** numbers are obtained separately. Let N be the number of rows
! 2465: ** and M be the number of columns.
! 2466: **
! 2467: ** A result table is an array of pointers to zero-terminated UTF-8 strings.
! 2468: ** There are (N+1)*M elements in the array. The first M pointers point
! 2469: ** to zero-terminated strings that contain the names of the columns.
! 2470: ** The remaining entries all point to query results. NULL values result
! 2471: ** in NULL pointers. All other values are in their UTF-8 zero-terminated
! 2472: ** string representation as returned by [sqlite3_column_text()].
! 2473: **
! 2474: ** A result table might consist of one or more memory allocations.
! 2475: ** It is not safe to pass a result table directly to [sqlite3_free()].
! 2476: ** A result table should be deallocated using [sqlite3_free_table()].
! 2477: **
! 2478: ** ^(As an example of the result table format, suppose a query result
! 2479: ** is as follows:
! 2480: **
! 2481: ** <blockquote><pre>
! 2482: ** Name | Age
! 2483: ** -----------------------
! 2484: ** Alice | 43
! 2485: ** Bob | 28
! 2486: ** Cindy | 21
! 2487: ** </pre></blockquote>
! 2488: **
! 2489: ** There are two column (M==2) and three rows (N==3). Thus the
! 2490: ** result table has 8 entries. Suppose the result table is stored
! 2491: ** in an array names azResult. Then azResult holds this content:
! 2492: **
! 2493: ** <blockquote><pre>
! 2494: ** azResult[0] = "Name";
! 2495: ** azResult[1] = "Age";
! 2496: ** azResult[2] = "Alice";
! 2497: ** azResult[3] = "43";
! 2498: ** azResult[4] = "Bob";
! 2499: ** azResult[5] = "28";
! 2500: ** azResult[6] = "Cindy";
! 2501: ** azResult[7] = "21";
! 2502: ** </pre></blockquote>)^
! 2503: **
! 2504: ** ^The sqlite3_get_table() function evaluates one or more
! 2505: ** semicolon-separated SQL statements in the zero-terminated UTF-8
! 2506: ** string of its 2nd parameter and returns a result table to the
! 2507: ** pointer given in its 3rd parameter.
! 2508: **
! 2509: ** After the application has finished with the result from sqlite3_get_table(),
! 2510: ** it must pass the result table pointer to sqlite3_free_table() in order to
! 2511: ** release the memory that was malloced. Because of the way the
! 2512: ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
! 2513: ** function must not try to call [sqlite3_free()] directly. Only
! 2514: ** [sqlite3_free_table()] is able to release the memory properly and safely.
! 2515: **
! 2516: ** The sqlite3_get_table() interface is implemented as a wrapper around
! 2517: ** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access
! 2518: ** to any internal data structures of SQLite. It uses only the public
! 2519: ** interface defined here. As a consequence, errors that occur in the
! 2520: ** wrapper layer outside of the internal [sqlite3_exec()] call are not
! 2521: ** reflected in subsequent calls to [sqlite3_errcode()] or
! 2522: ** [sqlite3_errmsg()].
! 2523: */
! 2524: SQLITE_API int sqlite3_get_table(
! 2525: sqlite3 *db, /* An open database */
! 2526: const char *zSql, /* SQL to be evaluated */
! 2527: char ***pazResult, /* Results of the query */
! 2528: int *pnRow, /* Number of result rows written here */
! 2529: int *pnColumn, /* Number of result columns written here */
! 2530: char **pzErrmsg /* Error msg written here */
! 2531: );
! 2532: SQLITE_API void sqlite3_free_table(char **result);
! 2533:
! 2534: /*
! 2535: ** CAPI3REF: Formatted String Printing Functions
! 2536: **
! 2537: ** These routines are work-alikes of the "printf()" family of functions
! 2538: ** from the standard C library.
! 2539: **
! 2540: ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
! 2541: ** results into memory obtained from [sqlite3_malloc()].
! 2542: ** The strings returned by these two routines should be
! 2543: ** released by [sqlite3_free()]. ^Both routines return a
! 2544: ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
! 2545: ** memory to hold the resulting string.
! 2546: **
! 2547: ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
! 2548: ** the standard C library. The result is written into the
! 2549: ** buffer supplied as the second parameter whose size is given by
! 2550: ** the first parameter. Note that the order of the
! 2551: ** first two parameters is reversed from snprintf().)^ This is an
! 2552: ** historical accident that cannot be fixed without breaking
! 2553: ** backwards compatibility. ^(Note also that sqlite3_snprintf()
! 2554: ** returns a pointer to its buffer instead of the number of
! 2555: ** characters actually written into the buffer.)^ We admit that
! 2556: ** the number of characters written would be a more useful return
! 2557: ** value but we cannot change the implementation of sqlite3_snprintf()
! 2558: ** now without breaking compatibility.
! 2559: **
! 2560: ** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
! 2561: ** guarantees that the buffer is always zero-terminated. ^The first
! 2562: ** parameter "n" is the total size of the buffer, including space for
! 2563: ** the zero terminator. So the longest string that can be completely
! 2564: ** written will be n-1 characters.
! 2565: **
! 2566: ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
! 2567: **
! 2568: ** These routines all implement some additional formatting
! 2569: ** options that are useful for constructing SQL statements.
! 2570: ** All of the usual printf() formatting options apply. In addition, there
! 2571: ** is are "%q", "%Q", and "%z" options.
! 2572: **
! 2573: ** ^(The %q option works like %s in that it substitutes a nul-terminated
! 2574: ** string from the argument list. But %q also doubles every '\'' character.
! 2575: ** %q is designed for use inside a string literal.)^ By doubling each '\''
! 2576: ** character it escapes that character and allows it to be inserted into
! 2577: ** the string.
! 2578: **
! 2579: ** For example, assume the string variable zText contains text as follows:
! 2580: **
! 2581: ** <blockquote><pre>
! 2582: ** char *zText = "It's a happy day!";
! 2583: ** </pre></blockquote>
! 2584: **
! 2585: ** One can use this text in an SQL statement as follows:
! 2586: **
! 2587: ** <blockquote><pre>
! 2588: ** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
! 2589: ** sqlite3_exec(db, zSQL, 0, 0, 0);
! 2590: ** sqlite3_free(zSQL);
! 2591: ** </pre></blockquote>
! 2592: **
! 2593: ** Because the %q format string is used, the '\'' character in zText
! 2594: ** is escaped and the SQL generated is as follows:
! 2595: **
! 2596: ** <blockquote><pre>
! 2597: ** INSERT INTO table1 VALUES('It''s a happy day!')
! 2598: ** </pre></blockquote>
! 2599: **
! 2600: ** This is correct. Had we used %s instead of %q, the generated SQL
! 2601: ** would have looked like this:
! 2602: **
! 2603: ** <blockquote><pre>
! 2604: ** INSERT INTO table1 VALUES('It's a happy day!');
! 2605: ** </pre></blockquote>
! 2606: **
! 2607: ** This second example is an SQL syntax error. As a general rule you should
! 2608: ** always use %q instead of %s when inserting text into a string literal.
! 2609: **
! 2610: ** ^(The %Q option works like %q except it also adds single quotes around
! 2611: ** the outside of the total string. Additionally, if the parameter in the
! 2612: ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
! 2613: ** single quotes).)^ So, for example, one could say:
! 2614: **
! 2615: ** <blockquote><pre>
! 2616: ** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
! 2617: ** sqlite3_exec(db, zSQL, 0, 0, 0);
! 2618: ** sqlite3_free(zSQL);
! 2619: ** </pre></blockquote>
! 2620: **
! 2621: ** The code above will render a correct SQL statement in the zSQL
! 2622: ** variable even if the zText variable is a NULL pointer.
! 2623: **
! 2624: ** ^(The "%z" formatting option works like "%s" but with the
! 2625: ** addition that after the string has been read and copied into
! 2626: ** the result, [sqlite3_free()] is called on the input string.)^
! 2627: */
! 2628: SQLITE_API char *sqlite3_mprintf(const char*,...);
! 2629: SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
! 2630: SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
! 2631: SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
! 2632:
! 2633: /*
! 2634: ** CAPI3REF: Memory Allocation Subsystem
! 2635: **
! 2636: ** The SQLite core uses these three routines for all of its own
! 2637: ** internal memory allocation needs. "Core" in the previous sentence
! 2638: ** does not include operating-system specific VFS implementation. The
! 2639: ** Windows VFS uses native malloc() and free() for some operations.
! 2640: **
! 2641: ** ^The sqlite3_malloc() routine returns a pointer to a block
! 2642: ** of memory at least N bytes in length, where N is the parameter.
! 2643: ** ^If sqlite3_malloc() is unable to obtain sufficient free
! 2644: ** memory, it returns a NULL pointer. ^If the parameter N to
! 2645: ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
! 2646: ** a NULL pointer.
! 2647: **
! 2648: ** ^Calling sqlite3_free() with a pointer previously returned
! 2649: ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
! 2650: ** that it might be reused. ^The sqlite3_free() routine is
! 2651: ** a no-op if is called with a NULL pointer. Passing a NULL pointer
! 2652: ** to sqlite3_free() is harmless. After being freed, memory
! 2653: ** should neither be read nor written. Even reading previously freed
! 2654: ** memory might result in a segmentation fault or other severe error.
! 2655: ** Memory corruption, a segmentation fault, or other severe error
! 2656: ** might result if sqlite3_free() is called with a non-NULL pointer that
! 2657: ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
! 2658: **
! 2659: ** ^(The sqlite3_realloc() interface attempts to resize a
! 2660: ** prior memory allocation to be at least N bytes, where N is the
! 2661: ** second parameter. The memory allocation to be resized is the first
! 2662: ** parameter.)^ ^ If the first parameter to sqlite3_realloc()
! 2663: ** is a NULL pointer then its behavior is identical to calling
! 2664: ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
! 2665: ** ^If the second parameter to sqlite3_realloc() is zero or
! 2666: ** negative then the behavior is exactly the same as calling
! 2667: ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
! 2668: ** ^sqlite3_realloc() returns a pointer to a memory allocation
! 2669: ** of at least N bytes in size or NULL if sufficient memory is unavailable.
! 2670: ** ^If M is the size of the prior allocation, then min(N,M) bytes
! 2671: ** of the prior allocation are copied into the beginning of buffer returned
! 2672: ** by sqlite3_realloc() and the prior allocation is freed.
! 2673: ** ^If sqlite3_realloc() returns NULL, then the prior allocation
! 2674: ** is not freed.
! 2675: **
! 2676: ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
! 2677: ** is always aligned to at least an 8 byte boundary, or to a
! 2678: ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
! 2679: ** option is used.
! 2680: **
! 2681: ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
! 2682: ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
! 2683: ** implementation of these routines to be omitted. That capability
! 2684: ** is no longer provided. Only built-in memory allocators can be used.
! 2685: **
! 2686: ** The Windows OS interface layer calls
! 2687: ** the system malloc() and free() directly when converting
! 2688: ** filenames between the UTF-8 encoding used by SQLite
! 2689: ** and whatever filename encoding is used by the particular Windows
! 2690: ** installation. Memory allocation errors are detected, but
! 2691: ** they are reported back as [SQLITE_CANTOPEN] or
! 2692: ** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
! 2693: **
! 2694: ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
! 2695: ** must be either NULL or else pointers obtained from a prior
! 2696: ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
! 2697: ** not yet been released.
! 2698: **
! 2699: ** The application must not read or write any part of
! 2700: ** a block of memory after it has been released using
! 2701: ** [sqlite3_free()] or [sqlite3_realloc()].
! 2702: */
! 2703: SQLITE_API void *sqlite3_malloc(int);
! 2704: SQLITE_API void *sqlite3_realloc(void*, int);
! 2705: SQLITE_API void sqlite3_free(void*);
! 2706:
! 2707: /*
! 2708: ** CAPI3REF: Memory Allocator Statistics
! 2709: **
! 2710: ** SQLite provides these two interfaces for reporting on the status
! 2711: ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
! 2712: ** routines, which form the built-in memory allocation subsystem.
! 2713: **
! 2714: ** ^The [sqlite3_memory_used()] routine returns the number of bytes
! 2715: ** of memory currently outstanding (malloced but not freed).
! 2716: ** ^The [sqlite3_memory_highwater()] routine returns the maximum
! 2717: ** value of [sqlite3_memory_used()] since the high-water mark
! 2718: ** was last reset. ^The values returned by [sqlite3_memory_used()] and
! 2719: ** [sqlite3_memory_highwater()] include any overhead
! 2720: ** added by SQLite in its implementation of [sqlite3_malloc()],
! 2721: ** but not overhead added by the any underlying system library
! 2722: ** routines that [sqlite3_malloc()] may call.
! 2723: **
! 2724: ** ^The memory high-water mark is reset to the current value of
! 2725: ** [sqlite3_memory_used()] if and only if the parameter to
! 2726: ** [sqlite3_memory_highwater()] is true. ^The value returned
! 2727: ** by [sqlite3_memory_highwater(1)] is the high-water mark
! 2728: ** prior to the reset.
! 2729: */
! 2730: SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
! 2731: SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
! 2732:
! 2733: /*
! 2734: ** CAPI3REF: Pseudo-Random Number Generator
! 2735: **
! 2736: ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
! 2737: ** select random [ROWID | ROWIDs] when inserting new records into a table that
! 2738: ** already uses the largest possible [ROWID]. The PRNG is also used for
! 2739: ** the build-in random() and randomblob() SQL functions. This interface allows
! 2740: ** applications to access the same PRNG for other purposes.
! 2741: **
! 2742: ** ^A call to this routine stores N bytes of randomness into buffer P.
! 2743: **
! 2744: ** ^The first time this routine is invoked (either internally or by
! 2745: ** the application) the PRNG is seeded using randomness obtained
! 2746: ** from the xRandomness method of the default [sqlite3_vfs] object.
! 2747: ** ^On all subsequent invocations, the pseudo-randomness is generated
! 2748: ** internally and without recourse to the [sqlite3_vfs] xRandomness
! 2749: ** method.
! 2750: */
! 2751: SQLITE_API void sqlite3_randomness(int N, void *P);
! 2752:
! 2753: /*
! 2754: ** CAPI3REF: Compile-Time Authorization Callbacks
! 2755: **
! 2756: ** ^This routine registers an authorizer callback with a particular
! 2757: ** [database connection], supplied in the first argument.
! 2758: ** ^The authorizer callback is invoked as SQL statements are being compiled
! 2759: ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
! 2760: ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. ^At various
! 2761: ** points during the compilation process, as logic is being created
! 2762: ** to perform various actions, the authorizer callback is invoked to
! 2763: ** see if those actions are allowed. ^The authorizer callback should
! 2764: ** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
! 2765: ** specific action but allow the SQL statement to continue to be
! 2766: ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
! 2767: ** rejected with an error. ^If the authorizer callback returns
! 2768: ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
! 2769: ** then the [sqlite3_prepare_v2()] or equivalent call that triggered
! 2770: ** the authorizer will fail with an error message.
! 2771: **
! 2772: ** When the callback returns [SQLITE_OK], that means the operation
! 2773: ** requested is ok. ^When the callback returns [SQLITE_DENY], the
! 2774: ** [sqlite3_prepare_v2()] or equivalent call that triggered the
! 2775: ** authorizer will fail with an error message explaining that
! 2776: ** access is denied.
! 2777: **
! 2778: ** ^The first parameter to the authorizer callback is a copy of the third
! 2779: ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
! 2780: ** to the callback is an integer [SQLITE_COPY | action code] that specifies
! 2781: ** the particular action to be authorized. ^The third through sixth parameters
! 2782: ** to the callback are zero-terminated strings that contain additional
! 2783: ** details about the action to be authorized.
! 2784: **
! 2785: ** ^If the action code is [SQLITE_READ]
! 2786: ** and the callback returns [SQLITE_IGNORE] then the
! 2787: ** [prepared statement] statement is constructed to substitute
! 2788: ** a NULL value in place of the table column that would have
! 2789: ** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
! 2790: ** return can be used to deny an untrusted user access to individual
! 2791: ** columns of a table.
! 2792: ** ^If the action code is [SQLITE_DELETE] and the callback returns
! 2793: ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
! 2794: ** [truncate optimization] is disabled and all rows are deleted individually.
! 2795: **
! 2796: ** An authorizer is used when [sqlite3_prepare | preparing]
! 2797: ** SQL statements from an untrusted source, to ensure that the SQL statements
! 2798: ** do not try to access data they are not allowed to see, or that they do not
! 2799: ** try to execute malicious statements that damage the database. For
! 2800: ** example, an application may allow a user to enter arbitrary
! 2801: ** SQL queries for evaluation by a database. But the application does
! 2802: ** not want the user to be able to make arbitrary changes to the
! 2803: ** database. An authorizer could then be put in place while the
! 2804: ** user-entered SQL is being [sqlite3_prepare | prepared] that
! 2805: ** disallows everything except [SELECT] statements.
! 2806: **
! 2807: ** Applications that need to process SQL from untrusted sources
! 2808: ** might also consider lowering resource limits using [sqlite3_limit()]
! 2809: ** and limiting database size using the [max_page_count] [PRAGMA]
! 2810: ** in addition to using an authorizer.
! 2811: **
! 2812: ** ^(Only a single authorizer can be in place on a database connection
! 2813: ** at a time. Each call to sqlite3_set_authorizer overrides the
! 2814: ** previous call.)^ ^Disable the authorizer by installing a NULL callback.
! 2815: ** The authorizer is disabled by default.
! 2816: **
! 2817: ** The authorizer callback must not do anything that will modify
! 2818: ** the database connection that invoked the authorizer callback.
! 2819: ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
! 2820: ** database connections for the meaning of "modify" in this paragraph.
! 2821: **
! 2822: ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
! 2823: ** statement might be re-prepared during [sqlite3_step()] due to a
! 2824: ** schema change. Hence, the application should ensure that the
! 2825: ** correct authorizer callback remains in place during the [sqlite3_step()].
! 2826: **
! 2827: ** ^Note that the authorizer callback is invoked only during
! 2828: ** [sqlite3_prepare()] or its variants. Authorization is not
! 2829: ** performed during statement evaluation in [sqlite3_step()], unless
! 2830: ** as stated in the previous paragraph, sqlite3_step() invokes
! 2831: ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
! 2832: */
! 2833: SQLITE_API int sqlite3_set_authorizer(
! 2834: sqlite3*,
! 2835: int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
! 2836: void *pUserData
! 2837: );
! 2838:
! 2839: /*
! 2840: ** CAPI3REF: Authorizer Return Codes
! 2841: **
! 2842: ** The [sqlite3_set_authorizer | authorizer callback function] must
! 2843: ** return either [SQLITE_OK] or one of these two constants in order
! 2844: ** to signal SQLite whether or not the action is permitted. See the
! 2845: ** [sqlite3_set_authorizer | authorizer documentation] for additional
! 2846: ** information.
! 2847: **
! 2848: ** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
! 2849: ** from the [sqlite3_vtab_on_conflict()] interface.
! 2850: */
! 2851: #define SQLITE_DENY 1 /* Abort the SQL statement with an error */
! 2852: #define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
! 2853:
! 2854: /*
! 2855: ** CAPI3REF: Authorizer Action Codes
! 2856: **
! 2857: ** The [sqlite3_set_authorizer()] interface registers a callback function
! 2858: ** that is invoked to authorize certain SQL statement actions. The
! 2859: ** second parameter to the callback is an integer code that specifies
! 2860: ** what action is being authorized. These are the integer action codes that
! 2861: ** the authorizer callback may be passed.
! 2862: **
! 2863: ** These action code values signify what kind of operation is to be
! 2864: ** authorized. The 3rd and 4th parameters to the authorization
! 2865: ** callback function will be parameters or NULL depending on which of these
! 2866: ** codes is used as the second parameter. ^(The 5th parameter to the
! 2867: ** authorizer callback is the name of the database ("main", "temp",
! 2868: ** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback
! 2869: ** is the name of the inner-most trigger or view that is responsible for
! 2870: ** the access attempt or NULL if this access attempt is directly from
! 2871: ** top-level SQL code.
! 2872: */
! 2873: /******************************************* 3rd ************ 4th ***********/
! 2874: #define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
! 2875: #define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
! 2876: #define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
! 2877: #define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
! 2878: #define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
! 2879: #define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
! 2880: #define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
! 2881: #define SQLITE_CREATE_VIEW 8 /* View Name NULL */
! 2882: #define SQLITE_DELETE 9 /* Table Name NULL */
! 2883: #define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
! 2884: #define SQLITE_DROP_TABLE 11 /* Table Name NULL */
! 2885: #define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
! 2886: #define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
! 2887: #define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
! 2888: #define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
! 2889: #define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
! 2890: #define SQLITE_DROP_VIEW 17 /* View Name NULL */
! 2891: #define SQLITE_INSERT 18 /* Table Name NULL */
! 2892: #define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
! 2893: #define SQLITE_READ 20 /* Table Name Column Name */
! 2894: #define SQLITE_SELECT 21 /* NULL NULL */
! 2895: #define SQLITE_TRANSACTION 22 /* Operation NULL */
! 2896: #define SQLITE_UPDATE 23 /* Table Name Column Name */
! 2897: #define SQLITE_ATTACH 24 /* Filename NULL */
! 2898: #define SQLITE_DETACH 25 /* Database Name NULL */
! 2899: #define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
! 2900: #define SQLITE_REINDEX 27 /* Index Name NULL */
! 2901: #define SQLITE_ANALYZE 28 /* Table Name NULL */
! 2902: #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
! 2903: #define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
! 2904: #define SQLITE_FUNCTION 31 /* NULL Function Name */
! 2905: #define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */
! 2906: #define SQLITE_COPY 0 /* No longer used */
! 2907:
! 2908: /*
! 2909: ** CAPI3REF: Tracing And Profiling Functions
! 2910: **
! 2911: ** These routines register callback functions that can be used for
! 2912: ** tracing and profiling the execution of SQL statements.
! 2913: **
! 2914: ** ^The callback function registered by sqlite3_trace() is invoked at
! 2915: ** various times when an SQL statement is being run by [sqlite3_step()].
! 2916: ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
! 2917: ** SQL statement text as the statement first begins executing.
! 2918: ** ^(Additional sqlite3_trace() callbacks might occur
! 2919: ** as each triggered subprogram is entered. The callbacks for triggers
! 2920: ** contain a UTF-8 SQL comment that identifies the trigger.)^
! 2921: **
! 2922: ** ^The callback function registered by sqlite3_profile() is invoked
! 2923: ** as each SQL statement finishes. ^The profile callback contains
! 2924: ** the original statement text and an estimate of wall-clock time
! 2925: ** of how long that statement took to run. ^The profile callback
! 2926: ** time is in units of nanoseconds, however the current implementation
! 2927: ** is only capable of millisecond resolution so the six least significant
! 2928: ** digits in the time are meaningless. Future versions of SQLite
! 2929: ** might provide greater resolution on the profiler callback. The
! 2930: ** sqlite3_profile() function is considered experimental and is
! 2931: ** subject to change in future versions of SQLite.
! 2932: */
! 2933: SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
! 2934: SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
! 2935: void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
! 2936:
! 2937: /*
! 2938: ** CAPI3REF: Query Progress Callbacks
! 2939: **
! 2940: ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
! 2941: ** function X to be invoked periodically during long running calls to
! 2942: ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
! 2943: ** database connection D. An example use for this
! 2944: ** interface is to keep a GUI updated during a large query.
! 2945: **
! 2946: ** ^The parameter P is passed through as the only parameter to the
! 2947: ** callback function X. ^The parameter N is the number of
! 2948: ** [virtual machine instructions] that are evaluated between successive
! 2949: ** invocations of the callback X.
! 2950: **
! 2951: ** ^Only a single progress handler may be defined at one time per
! 2952: ** [database connection]; setting a new progress handler cancels the
! 2953: ** old one. ^Setting parameter X to NULL disables the progress handler.
! 2954: ** ^The progress handler is also disabled by setting N to a value less
! 2955: ** than 1.
! 2956: **
! 2957: ** ^If the progress callback returns non-zero, the operation is
! 2958: ** interrupted. This feature can be used to implement a
! 2959: ** "Cancel" button on a GUI progress dialog box.
! 2960: **
! 2961: ** The progress handler callback must not do anything that will modify
! 2962: ** the database connection that invoked the progress handler.
! 2963: ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
! 2964: ** database connections for the meaning of "modify" in this paragraph.
! 2965: **
! 2966: */
! 2967: SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
! 2968:
! 2969: /*
! 2970: ** CAPI3REF: Opening A New Database Connection
! 2971: **
! 2972: ** ^These routines open an SQLite database file as specified by the
! 2973: ** filename argument. ^The filename argument is interpreted as UTF-8 for
! 2974: ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
! 2975: ** order for sqlite3_open16(). ^(A [database connection] handle is usually
! 2976: ** returned in *ppDb, even if an error occurs. The only exception is that
! 2977: ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
! 2978: ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
! 2979: ** object.)^ ^(If the database is opened (and/or created) successfully, then
! 2980: ** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
! 2981: ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
! 2982: ** an English language description of the error following a failure of any
! 2983: ** of the sqlite3_open() routines.
! 2984: **
! 2985: ** ^The default encoding for the database will be UTF-8 if
! 2986: ** sqlite3_open() or sqlite3_open_v2() is called and
! 2987: ** UTF-16 in the native byte order if sqlite3_open16() is used.
! 2988: **
! 2989: ** Whether or not an error occurs when it is opened, resources
! 2990: ** associated with the [database connection] handle should be released by
! 2991: ** passing it to [sqlite3_close()] when it is no longer required.
! 2992: **
! 2993: ** The sqlite3_open_v2() interface works like sqlite3_open()
! 2994: ** except that it accepts two additional parameters for additional control
! 2995: ** over the new database connection. ^(The flags parameter to
! 2996: ** sqlite3_open_v2() can take one of
! 2997: ** the following three values, optionally combined with the
! 2998: ** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
! 2999: ** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
! 3000: **
! 3001: ** <dl>
! 3002: ** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
! 3003: ** <dd>The database is opened in read-only mode. If the database does not
! 3004: ** already exist, an error is returned.</dd>)^
! 3005: **
! 3006: ** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
! 3007: ** <dd>The database is opened for reading and writing if possible, or reading
! 3008: ** only if the file is write protected by the operating system. In either
! 3009: ** case the database must already exist, otherwise an error is returned.</dd>)^
! 3010: **
! 3011: ** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
! 3012: ** <dd>The database is opened for reading and writing, and is created if
! 3013: ** it does not already exist. This is the behavior that is always used for
! 3014: ** sqlite3_open() and sqlite3_open16().</dd>)^
! 3015: ** </dl>
! 3016: **
! 3017: ** If the 3rd parameter to sqlite3_open_v2() is not one of the
! 3018: ** combinations shown above optionally combined with other
! 3019: ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
! 3020: ** then the behavior is undefined.
! 3021: **
! 3022: ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
! 3023: ** opens in the multi-thread [threading mode] as long as the single-thread
! 3024: ** mode has not been set at compile-time or start-time. ^If the
! 3025: ** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
! 3026: ** in the serialized [threading mode] unless single-thread was
! 3027: ** previously selected at compile-time or start-time.
! 3028: ** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
! 3029: ** eligible to use [shared cache mode], regardless of whether or not shared
! 3030: ** cache is enabled using [sqlite3_enable_shared_cache()]. ^The
! 3031: ** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
! 3032: ** participate in [shared cache mode] even if it is enabled.
! 3033: **
! 3034: ** ^The fourth parameter to sqlite3_open_v2() is the name of the
! 3035: ** [sqlite3_vfs] object that defines the operating system interface that
! 3036: ** the new database connection should use. ^If the fourth parameter is
! 3037: ** a NULL pointer then the default [sqlite3_vfs] object is used.
! 3038: **
! 3039: ** ^If the filename is ":memory:", then a private, temporary in-memory database
! 3040: ** is created for the connection. ^This in-memory database will vanish when
! 3041: ** the database connection is closed. Future versions of SQLite might
! 3042: ** make use of additional special filenames that begin with the ":" character.
! 3043: ** It is recommended that when a database filename actually does begin with
! 3044: ** a ":" character you should prefix the filename with a pathname such as
! 3045: ** "./" to avoid ambiguity.
! 3046: **
! 3047: ** ^If the filename is an empty string, then a private, temporary
! 3048: ** on-disk database will be created. ^This private database will be
! 3049: ** automatically deleted as soon as the database connection is closed.
! 3050: **
! 3051: ** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
! 3052: **
! 3053: ** ^If [URI filename] interpretation is enabled, and the filename argument
! 3054: ** begins with "file:", then the filename is interpreted as a URI. ^URI
! 3055: ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
! 3056: ** set in the fourth argument to sqlite3_open_v2(), or if it has
! 3057: ** been enabled globally using the [SQLITE_CONFIG_URI] option with the
! 3058: ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
! 3059: ** As of SQLite version 3.7.7, URI filename interpretation is turned off
! 3060: ** by default, but future releases of SQLite might enable URI filename
! 3061: ** interpretation by default. See "[URI filenames]" for additional
! 3062: ** information.
! 3063: **
! 3064: ** URI filenames are parsed according to RFC 3986. ^If the URI contains an
! 3065: ** authority, then it must be either an empty string or the string
! 3066: ** "localhost". ^If the authority is not an empty string or "localhost", an
! 3067: ** error is returned to the caller. ^The fragment component of a URI, if
! 3068: ** present, is ignored.
! 3069: **
! 3070: ** ^SQLite uses the path component of the URI as the name of the disk file
! 3071: ** which contains the database. ^If the path begins with a '/' character,
! 3072: ** then it is interpreted as an absolute path. ^If the path does not begin
! 3073: ** with a '/' (meaning that the authority section is omitted from the URI)
! 3074: ** then the path is interpreted as a relative path.
! 3075: ** ^On windows, the first component of an absolute path
! 3076: ** is a drive specification (e.g. "C:").
! 3077: **
! 3078: ** [[core URI query parameters]]
! 3079: ** The query component of a URI may contain parameters that are interpreted
! 3080: ** either by SQLite itself, or by a [VFS | custom VFS implementation].
! 3081: ** SQLite interprets the following three query parameters:
! 3082: **
! 3083: ** <ul>
! 3084: ** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
! 3085: ** a VFS object that provides the operating system interface that should
! 3086: ** be used to access the database file on disk. ^If this option is set to
! 3087: ** an empty string the default VFS object is used. ^Specifying an unknown
! 3088: ** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
! 3089: ** present, then the VFS specified by the option takes precedence over
! 3090: ** the value passed as the fourth parameter to sqlite3_open_v2().
! 3091: **
! 3092: ** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw" or
! 3093: ** "rwc". Attempting to set it to any other value is an error)^.
! 3094: ** ^If "ro" is specified, then the database is opened for read-only
! 3095: ** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
! 3096: ** third argument to sqlite3_prepare_v2(). ^If the mode option is set to
! 3097: ** "rw", then the database is opened for read-write (but not create)
! 3098: ** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
! 3099: ** been set. ^Value "rwc" is equivalent to setting both
! 3100: ** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If sqlite3_open_v2() is
! 3101: ** used, it is an error to specify a value for the mode parameter that is
! 3102: ** less restrictive than that specified by the flags passed as the third
! 3103: ** parameter.
! 3104: **
! 3105: ** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
! 3106: ** "private". ^Setting it to "shared" is equivalent to setting the
! 3107: ** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
! 3108: ** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
! 3109: ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
! 3110: ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
! 3111: ** a URI filename, its value overrides any behaviour requested by setting
! 3112: ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
! 3113: ** </ul>
! 3114: **
! 3115: ** ^Specifying an unknown parameter in the query component of a URI is not an
! 3116: ** error. Future versions of SQLite might understand additional query
! 3117: ** parameters. See "[query parameters with special meaning to SQLite]" for
! 3118: ** additional information.
! 3119: **
! 3120: ** [[URI filename examples]] <h3>URI filename examples</h3>
! 3121: **
! 3122: ** <table border="1" align=center cellpadding=5>
! 3123: ** <tr><th> URI filenames <th> Results
! 3124: ** <tr><td> file:data.db <td>
! 3125: ** Open the file "data.db" in the current directory.
! 3126: ** <tr><td> file:/home/fred/data.db<br>
! 3127: ** file:///home/fred/data.db <br>
! 3128: ** file://localhost/home/fred/data.db <br> <td>
! 3129: ** Open the database file "/home/fred/data.db".
! 3130: ** <tr><td> file://darkstar/home/fred/data.db <td>
! 3131: ** An error. "darkstar" is not a recognized authority.
! 3132: ** <tr><td style="white-space:nowrap">
! 3133: ** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
! 3134: ** <td> Windows only: Open the file "data.db" on fred's desktop on drive
! 3135: ** C:. Note that the %20 escaping in this example is not strictly
! 3136: ** necessary - space characters can be used literally
! 3137: ** in URI filenames.
! 3138: ** <tr><td> file:data.db?mode=ro&cache=private <td>
! 3139: ** Open file "data.db" in the current directory for read-only access.
! 3140: ** Regardless of whether or not shared-cache mode is enabled by
! 3141: ** default, use a private cache.
! 3142: ** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
! 3143: ** Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
! 3144: ** <tr><td> file:data.db?mode=readonly <td>
! 3145: ** An error. "readonly" is not a valid option for the "mode" parameter.
! 3146: ** </table>
! 3147: **
! 3148: ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
! 3149: ** query components of a URI. A hexadecimal escape sequence consists of a
! 3150: ** percent sign - "%" - followed by exactly two hexadecimal digits
! 3151: ** specifying an octet value. ^Before the path or query components of a
! 3152: ** URI filename are interpreted, they are encoded using UTF-8 and all
! 3153: ** hexadecimal escape sequences replaced by a single byte containing the
! 3154: ** corresponding octet. If this process generates an invalid UTF-8 encoding,
! 3155: ** the results are undefined.
! 3156: **
! 3157: ** <b>Note to Windows users:</b> The encoding used for the filename argument
! 3158: ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
! 3159: ** codepage is currently defined. Filenames containing international
! 3160: ** characters must be converted to UTF-8 prior to passing them into
! 3161: ** sqlite3_open() or sqlite3_open_v2().
! 3162: */
! 3163: SQLITE_API int sqlite3_open(
! 3164: const char *filename, /* Database filename (UTF-8) */
! 3165: sqlite3 **ppDb /* OUT: SQLite db handle */
! 3166: );
! 3167: SQLITE_API int sqlite3_open16(
! 3168: const void *filename, /* Database filename (UTF-16) */
! 3169: sqlite3 **ppDb /* OUT: SQLite db handle */
! 3170: );
! 3171: SQLITE_API int sqlite3_open_v2(
! 3172: const char *filename, /* Database filename (UTF-8) */
! 3173: sqlite3 **ppDb, /* OUT: SQLite db handle */
! 3174: int flags, /* Flags */
! 3175: const char *zVfs /* Name of VFS module to use */
! 3176: );
! 3177:
! 3178: /*
! 3179: ** CAPI3REF: Obtain Values For URI Parameters
! 3180: **
! 3181: ** These are utility routines, useful to VFS implementations, that check
! 3182: ** to see if a database file was a URI that contained a specific query
! 3183: ** parameter, and if so obtains the value of that query parameter.
! 3184: **
! 3185: ** If F is the database filename pointer passed into the xOpen() method of
! 3186: ** a VFS implementation when the flags parameter to xOpen() has one or
! 3187: ** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
! 3188: ** P is the name of the query parameter, then
! 3189: ** sqlite3_uri_parameter(F,P) returns the value of the P
! 3190: ** parameter if it exists or a NULL pointer if P does not appear as a
! 3191: ** query parameter on F. If P is a query parameter of F
! 3192: ** has no explicit value, then sqlite3_uri_parameter(F,P) returns
! 3193: ** a pointer to an empty string.
! 3194: **
! 3195: ** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
! 3196: ** parameter and returns true (1) or false (0) according to the value
! 3197: ** of P. The value of P is true if it is "yes" or "true" or "on" or
! 3198: ** a non-zero number and is false otherwise. If P is not a query parameter
! 3199: ** on F then sqlite3_uri_boolean(F,P,B) returns (B!=0).
! 3200: **
! 3201: ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
! 3202: ** 64-bit signed integer and returns that integer, or D if P does not
! 3203: ** exist. If the value of P is something other than an integer, then
! 3204: ** zero is returned.
! 3205: **
! 3206: ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
! 3207: ** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
! 3208: ** is not a database file pathname pointer that SQLite passed into the xOpen
! 3209: ** VFS method, then the behavior of this routine is undefined and probably
! 3210: ** undesirable.
! 3211: */
! 3212: SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
! 3213: SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
! 3214: SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
! 3215:
! 3216:
! 3217: /*
! 3218: ** CAPI3REF: Error Codes And Messages
! 3219: **
! 3220: ** ^The sqlite3_errcode() interface returns the numeric [result code] or
! 3221: ** [extended result code] for the most recent failed sqlite3_* API call
! 3222: ** associated with a [database connection]. If a prior API call failed
! 3223: ** but the most recent API call succeeded, the return value from
! 3224: ** sqlite3_errcode() is undefined. ^The sqlite3_extended_errcode()
! 3225: ** interface is the same except that it always returns the
! 3226: ** [extended result code] even when extended result codes are
! 3227: ** disabled.
! 3228: **
! 3229: ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
! 3230: ** text that describes the error, as either UTF-8 or UTF-16 respectively.
! 3231: ** ^(Memory to hold the error message string is managed internally.
! 3232: ** The application does not need to worry about freeing the result.
! 3233: ** However, the error string might be overwritten or deallocated by
! 3234: ** subsequent calls to other SQLite interface functions.)^
! 3235: **
! 3236: ** When the serialized [threading mode] is in use, it might be the
! 3237: ** case that a second error occurs on a separate thread in between
! 3238: ** the time of the first error and the call to these interfaces.
! 3239: ** When that happens, the second error will be reported since these
! 3240: ** interfaces always report the most recent result. To avoid
! 3241: ** this, each thread can obtain exclusive use of the [database connection] D
! 3242: ** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
! 3243: ** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
! 3244: ** all calls to the interfaces listed here are completed.
! 3245: **
! 3246: ** If an interface fails with SQLITE_MISUSE, that means the interface
! 3247: ** was invoked incorrectly by the application. In that case, the
! 3248: ** error code and message may or may not be set.
! 3249: */
! 3250: SQLITE_API int sqlite3_errcode(sqlite3 *db);
! 3251: SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
! 3252: SQLITE_API const char *sqlite3_errmsg(sqlite3*);
! 3253: SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
! 3254:
! 3255: /*
! 3256: ** CAPI3REF: SQL Statement Object
! 3257: ** KEYWORDS: {prepared statement} {prepared statements}
! 3258: **
! 3259: ** An instance of this object represents a single SQL statement.
! 3260: ** This object is variously known as a "prepared statement" or a
! 3261: ** "compiled SQL statement" or simply as a "statement".
! 3262: **
! 3263: ** The life of a statement object goes something like this:
! 3264: **
! 3265: ** <ol>
! 3266: ** <li> Create the object using [sqlite3_prepare_v2()] or a related
! 3267: ** function.
! 3268: ** <li> Bind values to [host parameters] using the sqlite3_bind_*()
! 3269: ** interfaces.
! 3270: ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
! 3271: ** <li> Reset the statement using [sqlite3_reset()] then go back
! 3272: ** to step 2. Do this zero or more times.
! 3273: ** <li> Destroy the object using [sqlite3_finalize()].
! 3274: ** </ol>
! 3275: **
! 3276: ** Refer to documentation on individual methods above for additional
! 3277: ** information.
! 3278: */
! 3279: typedef struct sqlite3_stmt sqlite3_stmt;
! 3280:
! 3281: /*
! 3282: ** CAPI3REF: Run-time Limits
! 3283: **
! 3284: ** ^(This interface allows the size of various constructs to be limited
! 3285: ** on a connection by connection basis. The first parameter is the
! 3286: ** [database connection] whose limit is to be set or queried. The
! 3287: ** second parameter is one of the [limit categories] that define a
! 3288: ** class of constructs to be size limited. The third parameter is the
! 3289: ** new limit for that construct.)^
! 3290: **
! 3291: ** ^If the new limit is a negative number, the limit is unchanged.
! 3292: ** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
! 3293: ** [limits | hard upper bound]
! 3294: ** set at compile-time by a C preprocessor macro called
! 3295: ** [limits | SQLITE_MAX_<i>NAME</i>].
! 3296: ** (The "_LIMIT_" in the name is changed to "_MAX_".))^
! 3297: ** ^Attempts to increase a limit above its hard upper bound are
! 3298: ** silently truncated to the hard upper bound.
! 3299: **
! 3300: ** ^Regardless of whether or not the limit was changed, the
! 3301: ** [sqlite3_limit()] interface returns the prior value of the limit.
! 3302: ** ^Hence, to find the current value of a limit without changing it,
! 3303: ** simply invoke this interface with the third parameter set to -1.
! 3304: **
! 3305: ** Run-time limits are intended for use in applications that manage
! 3306: ** both their own internal database and also databases that are controlled
! 3307: ** by untrusted external sources. An example application might be a
! 3308: ** web browser that has its own databases for storing history and
! 3309: ** separate databases controlled by JavaScript applications downloaded
! 3310: ** off the Internet. The internal databases can be given the
! 3311: ** large, default limits. Databases managed by external sources can
! 3312: ** be given much smaller limits designed to prevent a denial of service
! 3313: ** attack. Developers might also want to use the [sqlite3_set_authorizer()]
! 3314: ** interface to further control untrusted SQL. The size of the database
! 3315: ** created by an untrusted script can be contained using the
! 3316: ** [max_page_count] [PRAGMA].
! 3317: **
! 3318: ** New run-time limit categories may be added in future releases.
! 3319: */
! 3320: SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
! 3321:
! 3322: /*
! 3323: ** CAPI3REF: Run-Time Limit Categories
! 3324: ** KEYWORDS: {limit category} {*limit categories}
! 3325: **
! 3326: ** These constants define various performance limits
! 3327: ** that can be lowered at run-time using [sqlite3_limit()].
! 3328: ** The synopsis of the meanings of the various limits is shown below.
! 3329: ** Additional information is available at [limits | Limits in SQLite].
! 3330: **
! 3331: ** <dl>
! 3332: ** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
! 3333: ** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
! 3334: **
! 3335: ** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
! 3336: ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
! 3337: **
! 3338: ** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
! 3339: ** <dd>The maximum number of columns in a table definition or in the
! 3340: ** result set of a [SELECT] or the maximum number of columns in an index
! 3341: ** or in an ORDER BY or GROUP BY clause.</dd>)^
! 3342: **
! 3343: ** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
! 3344: ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
! 3345: **
! 3346: ** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
! 3347: ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
! 3348: **
! 3349: ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
! 3350: ** <dd>The maximum number of instructions in a virtual machine program
! 3351: ** used to implement an SQL statement. This limit is not currently
! 3352: ** enforced, though that might be added in some future release of
! 3353: ** SQLite.</dd>)^
! 3354: **
! 3355: ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
! 3356: ** <dd>The maximum number of arguments on a function.</dd>)^
! 3357: **
! 3358: ** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
! 3359: ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
! 3360: **
! 3361: ** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
! 3362: ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
! 3363: ** <dd>The maximum length of the pattern argument to the [LIKE] or
! 3364: ** [GLOB] operators.</dd>)^
! 3365: **
! 3366: ** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
! 3367: ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
! 3368: ** <dd>The maximum index number of any [parameter] in an SQL statement.)^
! 3369: **
! 3370: ** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
! 3371: ** <dd>The maximum depth of recursion for triggers.</dd>)^
! 3372: ** </dl>
! 3373: */
! 3374: #define SQLITE_LIMIT_LENGTH 0
! 3375: #define SQLITE_LIMIT_SQL_LENGTH 1
! 3376: #define SQLITE_LIMIT_COLUMN 2
! 3377: #define SQLITE_LIMIT_EXPR_DEPTH 3
! 3378: #define SQLITE_LIMIT_COMPOUND_SELECT 4
! 3379: #define SQLITE_LIMIT_VDBE_OP 5
! 3380: #define SQLITE_LIMIT_FUNCTION_ARG 6
! 3381: #define SQLITE_LIMIT_ATTACHED 7
! 3382: #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8
! 3383: #define SQLITE_LIMIT_VARIABLE_NUMBER 9
! 3384: #define SQLITE_LIMIT_TRIGGER_DEPTH 10
! 3385:
! 3386: /*
! 3387: ** CAPI3REF: Compiling An SQL Statement
! 3388: ** KEYWORDS: {SQL statement compiler}
! 3389: **
! 3390: ** To execute an SQL query, it must first be compiled into a byte-code
! 3391: ** program using one of these routines.
! 3392: **
! 3393: ** The first argument, "db", is a [database connection] obtained from a
! 3394: ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
! 3395: ** [sqlite3_open16()]. The database connection must not have been closed.
! 3396: **
! 3397: ** The second argument, "zSql", is the statement to be compiled, encoded
! 3398: ** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2()
! 3399: ** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
! 3400: ** use UTF-16.
! 3401: **
! 3402: ** ^If the nByte argument is less than zero, then zSql is read up to the
! 3403: ** first zero terminator. ^If nByte is non-negative, then it is the maximum
! 3404: ** number of bytes read from zSql. ^When nByte is non-negative, the
! 3405: ** zSql string ends at either the first '\000' or '\u0000' character or
! 3406: ** the nByte-th byte, whichever comes first. If the caller knows
! 3407: ** that the supplied string is nul-terminated, then there is a small
! 3408: ** performance advantage to be gained by passing an nByte parameter that
! 3409: ** is equal to the number of bytes in the input string <i>including</i>
! 3410: ** the nul-terminator bytes as this saves SQLite from having to
! 3411: ** make a copy of the input string.
! 3412: **
! 3413: ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
! 3414: ** past the end of the first SQL statement in zSql. These routines only
! 3415: ** compile the first statement in zSql, so *pzTail is left pointing to
! 3416: ** what remains uncompiled.
! 3417: **
! 3418: ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
! 3419: ** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set
! 3420: ** to NULL. ^If the input text contains no SQL (if the input is an empty
! 3421: ** string or a comment) then *ppStmt is set to NULL.
! 3422: ** The calling procedure is responsible for deleting the compiled
! 3423: ** SQL statement using [sqlite3_finalize()] after it has finished with it.
! 3424: ** ppStmt may not be NULL.
! 3425: **
! 3426: ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
! 3427: ** otherwise an [error code] is returned.
! 3428: **
! 3429: ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
! 3430: ** recommended for all new programs. The two older interfaces are retained
! 3431: ** for backwards compatibility, but their use is discouraged.
! 3432: ** ^In the "v2" interfaces, the prepared statement
! 3433: ** that is returned (the [sqlite3_stmt] object) contains a copy of the
! 3434: ** original SQL text. This causes the [sqlite3_step()] interface to
! 3435: ** behave differently in three ways:
! 3436: **
! 3437: ** <ol>
! 3438: ** <li>
! 3439: ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
! 3440: ** always used to do, [sqlite3_step()] will automatically recompile the SQL
! 3441: ** statement and try to run it again.
! 3442: ** </li>
! 3443: **
! 3444: ** <li>
! 3445: ** ^When an error occurs, [sqlite3_step()] will return one of the detailed
! 3446: ** [error codes] or [extended error codes]. ^The legacy behavior was that
! 3447: ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
! 3448: ** and the application would have to make a second call to [sqlite3_reset()]
! 3449: ** in order to find the underlying cause of the problem. With the "v2" prepare
! 3450: ** interfaces, the underlying reason for the error is returned immediately.
! 3451: ** </li>
! 3452: **
! 3453: ** <li>
! 3454: ** ^If the specific value bound to [parameter | host parameter] in the
! 3455: ** WHERE clause might influence the choice of query plan for a statement,
! 3456: ** then the statement will be automatically recompiled, as if there had been
! 3457: ** a schema change, on the first [sqlite3_step()] call following any change
! 3458: ** to the [sqlite3_bind_text | bindings] of that [parameter].
! 3459: ** ^The specific value of WHERE-clause [parameter] might influence the
! 3460: ** choice of query plan if the parameter is the left-hand side of a [LIKE]
! 3461: ** or [GLOB] operator or if the parameter is compared to an indexed column
! 3462: ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
! 3463: ** the
! 3464: ** </li>
! 3465: ** </ol>
! 3466: */
! 3467: SQLITE_API int sqlite3_prepare(
! 3468: sqlite3 *db, /* Database handle */
! 3469: const char *zSql, /* SQL statement, UTF-8 encoded */
! 3470: int nByte, /* Maximum length of zSql in bytes. */
! 3471: sqlite3_stmt **ppStmt, /* OUT: Statement handle */
! 3472: const char **pzTail /* OUT: Pointer to unused portion of zSql */
! 3473: );
! 3474: SQLITE_API int sqlite3_prepare_v2(
! 3475: sqlite3 *db, /* Database handle */
! 3476: const char *zSql, /* SQL statement, UTF-8 encoded */
! 3477: int nByte, /* Maximum length of zSql in bytes. */
! 3478: sqlite3_stmt **ppStmt, /* OUT: Statement handle */
! 3479: const char **pzTail /* OUT: Pointer to unused portion of zSql */
! 3480: );
! 3481: SQLITE_API int sqlite3_prepare16(
! 3482: sqlite3 *db, /* Database handle */
! 3483: const void *zSql, /* SQL statement, UTF-16 encoded */
! 3484: int nByte, /* Maximum length of zSql in bytes. */
! 3485: sqlite3_stmt **ppStmt, /* OUT: Statement handle */
! 3486: const void **pzTail /* OUT: Pointer to unused portion of zSql */
! 3487: );
! 3488: SQLITE_API int sqlite3_prepare16_v2(
! 3489: sqlite3 *db, /* Database handle */
! 3490: const void *zSql, /* SQL statement, UTF-16 encoded */
! 3491: int nByte, /* Maximum length of zSql in bytes. */
! 3492: sqlite3_stmt **ppStmt, /* OUT: Statement handle */
! 3493: const void **pzTail /* OUT: Pointer to unused portion of zSql */
! 3494: );
! 3495:
! 3496: /*
! 3497: ** CAPI3REF: Retrieving Statement SQL
! 3498: **
! 3499: ** ^This interface can be used to retrieve a saved copy of the original
! 3500: ** SQL text used to create a [prepared statement] if that statement was
! 3501: ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
! 3502: */
! 3503: SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
! 3504:
! 3505: /*
! 3506: ** CAPI3REF: Determine If An SQL Statement Writes The Database
! 3507: **
! 3508: ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
! 3509: ** and only if the [prepared statement] X makes no direct changes to
! 3510: ** the content of the database file.
! 3511: **
! 3512: ** Note that [application-defined SQL functions] or
! 3513: ** [virtual tables] might change the database indirectly as a side effect.
! 3514: ** ^(For example, if an application defines a function "eval()" that
! 3515: ** calls [sqlite3_exec()], then the following SQL statement would
! 3516: ** change the database file through side-effects:
! 3517: **
! 3518: ** <blockquote><pre>
! 3519: ** SELECT eval('DELETE FROM t1') FROM t2;
! 3520: ** </pre></blockquote>
! 3521: **
! 3522: ** But because the [SELECT] statement does not change the database file
! 3523: ** directly, sqlite3_stmt_readonly() would still return true.)^
! 3524: **
! 3525: ** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
! 3526: ** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
! 3527: ** since the statements themselves do not actually modify the database but
! 3528: ** rather they control the timing of when other statements modify the
! 3529: ** database. ^The [ATTACH] and [DETACH] statements also cause
! 3530: ** sqlite3_stmt_readonly() to return true since, while those statements
! 3531: ** change the configuration of a database connection, they do not make
! 3532: ** changes to the content of the database files on disk.
! 3533: */
! 3534: SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
! 3535:
! 3536: /*
! 3537: ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
! 3538: **
! 3539: ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
! 3540: ** [prepared statement] S has been stepped at least once using
! 3541: ** [sqlite3_step(S)] but has not run to completion and/or has not
! 3542: ** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
! 3543: ** interface returns false if S is a NULL pointer. If S is not a
! 3544: ** NULL pointer and is not a pointer to a valid [prepared statement]
! 3545: ** object, then the behavior is undefined and probably undesirable.
! 3546: **
! 3547: ** This interface can be used in combination [sqlite3_next_stmt()]
! 3548: ** to locate all prepared statements associated with a database
! 3549: ** connection that are in need of being reset. This can be used,
! 3550: ** for example, in diagnostic routines to search for prepared
! 3551: ** statements that are holding a transaction open.
! 3552: */
! 3553: SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
! 3554:
! 3555: /*
! 3556: ** CAPI3REF: Dynamically Typed Value Object
! 3557: ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
! 3558: **
! 3559: ** SQLite uses the sqlite3_value object to represent all values
! 3560: ** that can be stored in a database table. SQLite uses dynamic typing
! 3561: ** for the values it stores. ^Values stored in sqlite3_value objects
! 3562: ** can be integers, floating point values, strings, BLOBs, or NULL.
! 3563: **
! 3564: ** An sqlite3_value object may be either "protected" or "unprotected".
! 3565: ** Some interfaces require a protected sqlite3_value. Other interfaces
! 3566: ** will accept either a protected or an unprotected sqlite3_value.
! 3567: ** Every interface that accepts sqlite3_value arguments specifies
! 3568: ** whether or not it requires a protected sqlite3_value.
! 3569: **
! 3570: ** The terms "protected" and "unprotected" refer to whether or not
! 3571: ** a mutex is held. An internal mutex is held for a protected
! 3572: ** sqlite3_value object but no mutex is held for an unprotected
! 3573: ** sqlite3_value object. If SQLite is compiled to be single-threaded
! 3574: ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
! 3575: ** or if SQLite is run in one of reduced mutex modes
! 3576: ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
! 3577: ** then there is no distinction between protected and unprotected
! 3578: ** sqlite3_value objects and they can be used interchangeably. However,
! 3579: ** for maximum code portability it is recommended that applications
! 3580: ** still make the distinction between protected and unprotected
! 3581: ** sqlite3_value objects even when not strictly required.
! 3582: **
! 3583: ** ^The sqlite3_value objects that are passed as parameters into the
! 3584: ** implementation of [application-defined SQL functions] are protected.
! 3585: ** ^The sqlite3_value object returned by
! 3586: ** [sqlite3_column_value()] is unprotected.
! 3587: ** Unprotected sqlite3_value objects may only be used with
! 3588: ** [sqlite3_result_value()] and [sqlite3_bind_value()].
! 3589: ** The [sqlite3_value_blob | sqlite3_value_type()] family of
! 3590: ** interfaces require protected sqlite3_value objects.
! 3591: */
! 3592: typedef struct Mem sqlite3_value;
! 3593:
! 3594: /*
! 3595: ** CAPI3REF: SQL Function Context Object
! 3596: **
! 3597: ** The context in which an SQL function executes is stored in an
! 3598: ** sqlite3_context object. ^A pointer to an sqlite3_context object
! 3599: ** is always first parameter to [application-defined SQL functions].
! 3600: ** The application-defined SQL function implementation will pass this
! 3601: ** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
! 3602: ** [sqlite3_aggregate_context()], [sqlite3_user_data()],
! 3603: ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
! 3604: ** and/or [sqlite3_set_auxdata()].
! 3605: */
! 3606: typedef struct sqlite3_context sqlite3_context;
! 3607:
! 3608: /*
! 3609: ** CAPI3REF: Binding Values To Prepared Statements
! 3610: ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
! 3611: ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
! 3612: **
! 3613: ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
! 3614: ** literals may be replaced by a [parameter] that matches one of following
! 3615: ** templates:
! 3616: **
! 3617: ** <ul>
! 3618: ** <li> ?
! 3619: ** <li> ?NNN
! 3620: ** <li> :VVV
! 3621: ** <li> @VVV
! 3622: ** <li> $VVV
! 3623: ** </ul>
! 3624: **
! 3625: ** In the templates above, NNN represents an integer literal,
! 3626: ** and VVV represents an alphanumeric identifier.)^ ^The values of these
! 3627: ** parameters (also called "host parameter names" or "SQL parameters")
! 3628: ** can be set using the sqlite3_bind_*() routines defined here.
! 3629: **
! 3630: ** ^The first argument to the sqlite3_bind_*() routines is always
! 3631: ** a pointer to the [sqlite3_stmt] object returned from
! 3632: ** [sqlite3_prepare_v2()] or its variants.
! 3633: **
! 3634: ** ^The second argument is the index of the SQL parameter to be set.
! 3635: ** ^The leftmost SQL parameter has an index of 1. ^When the same named
! 3636: ** SQL parameter is used more than once, second and subsequent
! 3637: ** occurrences have the same index as the first occurrence.
! 3638: ** ^The index for named parameters can be looked up using the
! 3639: ** [sqlite3_bind_parameter_index()] API if desired. ^The index
! 3640: ** for "?NNN" parameters is the value of NNN.
! 3641: ** ^The NNN value must be between 1 and the [sqlite3_limit()]
! 3642: ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
! 3643: **
! 3644: ** ^The third argument is the value to bind to the parameter.
! 3645: **
! 3646: ** ^(In those routines that have a fourth argument, its value is the
! 3647: ** number of bytes in the parameter. To be clear: the value is the
! 3648: ** number of <u>bytes</u> in the value, not the number of characters.)^
! 3649: ** ^If the fourth parameter is negative, the length of the string is
! 3650: ** the number of bytes up to the first zero terminator.
! 3651: ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
! 3652: ** or sqlite3_bind_text16() then that parameter must be the byte offset
! 3653: ** where the NUL terminator would occur assuming the string were NUL
! 3654: ** terminated. If any NUL characters occur at byte offsets less than
! 3655: ** the value of the fourth parameter then the resulting string value will
! 3656: ** contain embedded NULs. The result of expressions involving strings
! 3657: ** with embedded NULs is undefined.
! 3658: **
! 3659: ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
! 3660: ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
! 3661: ** string after SQLite has finished with it. ^The destructor is called
! 3662: ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
! 3663: ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.
! 3664: ** ^If the fifth argument is
! 3665: ** the special value [SQLITE_STATIC], then SQLite assumes that the
! 3666: ** information is in static, unmanaged space and does not need to be freed.
! 3667: ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
! 3668: ** SQLite makes its own private copy of the data immediately, before
! 3669: ** the sqlite3_bind_*() routine returns.
! 3670: **
! 3671: ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
! 3672: ** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
! 3673: ** (just an integer to hold its size) while it is being processed.
! 3674: ** Zeroblobs are intended to serve as placeholders for BLOBs whose
! 3675: ** content is later written using
! 3676: ** [sqlite3_blob_open | incremental BLOB I/O] routines.
! 3677: ** ^A negative value for the zeroblob results in a zero-length BLOB.
! 3678: **
! 3679: ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
! 3680: ** for the [prepared statement] or with a prepared statement for which
! 3681: ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
! 3682: ** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_()
! 3683: ** routine is passed a [prepared statement] that has been finalized, the
! 3684: ** result is undefined and probably harmful.
! 3685: **
! 3686: ** ^Bindings are not cleared by the [sqlite3_reset()] routine.
! 3687: ** ^Unbound parameters are interpreted as NULL.
! 3688: **
! 3689: ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
! 3690: ** [error code] if anything goes wrong.
! 3691: ** ^[SQLITE_RANGE] is returned if the parameter
! 3692: ** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails.
! 3693: **
! 3694: ** See also: [sqlite3_bind_parameter_count()],
! 3695: ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
! 3696: */
! 3697: SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
! 3698: SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
! 3699: SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
! 3700: SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
! 3701: SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
! 3702: SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
! 3703: SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
! 3704: SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
! 3705: SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
! 3706:
! 3707: /*
! 3708: ** CAPI3REF: Number Of SQL Parameters
! 3709: **
! 3710: ** ^This routine can be used to find the number of [SQL parameters]
! 3711: ** in a [prepared statement]. SQL parameters are tokens of the
! 3712: ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
! 3713: ** placeholders for values that are [sqlite3_bind_blob | bound]
! 3714: ** to the parameters at a later time.
! 3715: **
! 3716: ** ^(This routine actually returns the index of the largest (rightmost)
! 3717: ** parameter. For all forms except ?NNN, this will correspond to the
! 3718: ** number of unique parameters. If parameters of the ?NNN form are used,
! 3719: ** there may be gaps in the list.)^
! 3720: **
! 3721: ** See also: [sqlite3_bind_blob|sqlite3_bind()],
! 3722: ** [sqlite3_bind_parameter_name()], and
! 3723: ** [sqlite3_bind_parameter_index()].
! 3724: */
! 3725: SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
! 3726:
! 3727: /*
! 3728: ** CAPI3REF: Name Of A Host Parameter
! 3729: **
! 3730: ** ^The sqlite3_bind_parameter_name(P,N) interface returns
! 3731: ** the name of the N-th [SQL parameter] in the [prepared statement] P.
! 3732: ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
! 3733: ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
! 3734: ** respectively.
! 3735: ** In other words, the initial ":" or "$" or "@" or "?"
! 3736: ** is included as part of the name.)^
! 3737: ** ^Parameters of the form "?" without a following integer have no name
! 3738: ** and are referred to as "nameless" or "anonymous parameters".
! 3739: **
! 3740: ** ^The first host parameter has an index of 1, not 0.
! 3741: **
! 3742: ** ^If the value N is out of range or if the N-th parameter is
! 3743: ** nameless, then NULL is returned. ^The returned string is
! 3744: ** always in UTF-8 encoding even if the named parameter was
! 3745: ** originally specified as UTF-16 in [sqlite3_prepare16()] or
! 3746: ** [sqlite3_prepare16_v2()].
! 3747: **
! 3748: ** See also: [sqlite3_bind_blob|sqlite3_bind()],
! 3749: ** [sqlite3_bind_parameter_count()], and
! 3750: ** [sqlite3_bind_parameter_index()].
! 3751: */
! 3752: SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
! 3753:
! 3754: /*
! 3755: ** CAPI3REF: Index Of A Parameter With A Given Name
! 3756: **
! 3757: ** ^Return the index of an SQL parameter given its name. ^The
! 3758: ** index value returned is suitable for use as the second
! 3759: ** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
! 3760: ** is returned if no matching parameter is found. ^The parameter
! 3761: ** name must be given in UTF-8 even if the original statement
! 3762: ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
! 3763: **
! 3764: ** See also: [sqlite3_bind_blob|sqlite3_bind()],
! 3765: ** [sqlite3_bind_parameter_count()], and
! 3766: ** [sqlite3_bind_parameter_index()].
! 3767: */
! 3768: SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
! 3769:
! 3770: /*
! 3771: ** CAPI3REF: Reset All Bindings On A Prepared Statement
! 3772: **
! 3773: ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
! 3774: ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
! 3775: ** ^Use this routine to reset all host parameters to NULL.
! 3776: */
! 3777: SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
! 3778:
! 3779: /*
! 3780: ** CAPI3REF: Number Of Columns In A Result Set
! 3781: **
! 3782: ** ^Return the number of columns in the result set returned by the
! 3783: ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
! 3784: ** statement that does not return data (for example an [UPDATE]).
! 3785: **
! 3786: ** See also: [sqlite3_data_count()]
! 3787: */
! 3788: SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
! 3789:
! 3790: /*
! 3791: ** CAPI3REF: Column Names In A Result Set
! 3792: **
! 3793: ** ^These routines return the name assigned to a particular column
! 3794: ** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
! 3795: ** interface returns a pointer to a zero-terminated UTF-8 string
! 3796: ** and sqlite3_column_name16() returns a pointer to a zero-terminated
! 3797: ** UTF-16 string. ^The first parameter is the [prepared statement]
! 3798: ** that implements the [SELECT] statement. ^The second parameter is the
! 3799: ** column number. ^The leftmost column is number 0.
! 3800: **
! 3801: ** ^The returned string pointer is valid until either the [prepared statement]
! 3802: ** is destroyed by [sqlite3_finalize()] or until the statement is automatically
! 3803: ** reprepared by the first call to [sqlite3_step()] for a particular run
! 3804: ** or until the next call to
! 3805: ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
! 3806: **
! 3807: ** ^If sqlite3_malloc() fails during the processing of either routine
! 3808: ** (for example during a conversion from UTF-8 to UTF-16) then a
! 3809: ** NULL pointer is returned.
! 3810: **
! 3811: ** ^The name of a result column is the value of the "AS" clause for
! 3812: ** that column, if there is an AS clause. If there is no AS clause
! 3813: ** then the name of the column is unspecified and may change from
! 3814: ** one release of SQLite to the next.
! 3815: */
! 3816: SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
! 3817: SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
! 3818:
! 3819: /*
! 3820: ** CAPI3REF: Source Of Data In A Query Result
! 3821: **
! 3822: ** ^These routines provide a means to determine the database, table, and
! 3823: ** table column that is the origin of a particular result column in
! 3824: ** [SELECT] statement.
! 3825: ** ^The name of the database or table or column can be returned as
! 3826: ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
! 3827: ** the database name, the _table_ routines return the table name, and
! 3828: ** the origin_ routines return the column name.
! 3829: ** ^The returned string is valid until the [prepared statement] is destroyed
! 3830: ** using [sqlite3_finalize()] or until the statement is automatically
! 3831: ** reprepared by the first call to [sqlite3_step()] for a particular run
! 3832: ** or until the same information is requested
! 3833: ** again in a different encoding.
! 3834: **
! 3835: ** ^The names returned are the original un-aliased names of the
! 3836: ** database, table, and column.
! 3837: **
! 3838: ** ^The first argument to these interfaces is a [prepared statement].
! 3839: ** ^These functions return information about the Nth result column returned by
! 3840: ** the statement, where N is the second function argument.
! 3841: ** ^The left-most column is column 0 for these routines.
! 3842: **
! 3843: ** ^If the Nth column returned by the statement is an expression or
! 3844: ** subquery and is not a column value, then all of these functions return
! 3845: ** NULL. ^These routine might also return NULL if a memory allocation error
! 3846: ** occurs. ^Otherwise, they return the name of the attached database, table,
! 3847: ** or column that query result column was extracted from.
! 3848: **
! 3849: ** ^As with all other SQLite APIs, those whose names end with "16" return
! 3850: ** UTF-16 encoded strings and the other functions return UTF-8.
! 3851: **
! 3852: ** ^These APIs are only available if the library was compiled with the
! 3853: ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
! 3854: **
! 3855: ** If two or more threads call one or more of these routines against the same
! 3856: ** prepared statement and column at the same time then the results are
! 3857: ** undefined.
! 3858: **
! 3859: ** If two or more threads call one or more
! 3860: ** [sqlite3_column_database_name | column metadata interfaces]
! 3861: ** for the same [prepared statement] and result column
! 3862: ** at the same time then the results are undefined.
! 3863: */
! 3864: SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
! 3865: SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
! 3866: SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
! 3867: SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
! 3868: SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
! 3869: SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
! 3870:
! 3871: /*
! 3872: ** CAPI3REF: Declared Datatype Of A Query Result
! 3873: **
! 3874: ** ^(The first parameter is a [prepared statement].
! 3875: ** If this statement is a [SELECT] statement and the Nth column of the
! 3876: ** returned result set of that [SELECT] is a table column (not an
! 3877: ** expression or subquery) then the declared type of the table
! 3878: ** column is returned.)^ ^If the Nth column of the result set is an
! 3879: ** expression or subquery, then a NULL pointer is returned.
! 3880: ** ^The returned string is always UTF-8 encoded.
! 3881: **
! 3882: ** ^(For example, given the database schema:
! 3883: **
! 3884: ** CREATE TABLE t1(c1 VARIANT);
! 3885: **
! 3886: ** and the following statement to be compiled:
! 3887: **
! 3888: ** SELECT c1 + 1, c1 FROM t1;
! 3889: **
! 3890: ** this routine would return the string "VARIANT" for the second result
! 3891: ** column (i==1), and a NULL pointer for the first result column (i==0).)^
! 3892: **
! 3893: ** ^SQLite uses dynamic run-time typing. ^So just because a column
! 3894: ** is declared to contain a particular type does not mean that the
! 3895: ** data stored in that column is of the declared type. SQLite is
! 3896: ** strongly typed, but the typing is dynamic not static. ^Type
! 3897: ** is associated with individual values, not with the containers
! 3898: ** used to hold those values.
! 3899: */
! 3900: SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
! 3901: SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
! 3902:
! 3903: /*
! 3904: ** CAPI3REF: Evaluate An SQL Statement
! 3905: **
! 3906: ** After a [prepared statement] has been prepared using either
! 3907: ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
! 3908: ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
! 3909: ** must be called one or more times to evaluate the statement.
! 3910: **
! 3911: ** The details of the behavior of the sqlite3_step() interface depend
! 3912: ** on whether the statement was prepared using the newer "v2" interface
! 3913: ** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
! 3914: ** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
! 3915: ** new "v2" interface is recommended for new applications but the legacy
! 3916: ** interface will continue to be supported.
! 3917: **
! 3918: ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
! 3919: ** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
! 3920: ** ^With the "v2" interface, any of the other [result codes] or
! 3921: ** [extended result codes] might be returned as well.
! 3922: **
! 3923: ** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
! 3924: ** database locks it needs to do its job. ^If the statement is a [COMMIT]
! 3925: ** or occurs outside of an explicit transaction, then you can retry the
! 3926: ** statement. If the statement is not a [COMMIT] and occurs within an
! 3927: ** explicit transaction then you should rollback the transaction before
! 3928: ** continuing.
! 3929: **
! 3930: ** ^[SQLITE_DONE] means that the statement has finished executing
! 3931: ** successfully. sqlite3_step() should not be called again on this virtual
! 3932: ** machine without first calling [sqlite3_reset()] to reset the virtual
! 3933: ** machine back to its initial state.
! 3934: **
! 3935: ** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
! 3936: ** is returned each time a new row of data is ready for processing by the
! 3937: ** caller. The values may be accessed using the [column access functions].
! 3938: ** sqlite3_step() is called again to retrieve the next row of data.
! 3939: **
! 3940: ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
! 3941: ** violation) has occurred. sqlite3_step() should not be called again on
! 3942: ** the VM. More information may be found by calling [sqlite3_errmsg()].
! 3943: ** ^With the legacy interface, a more specific error code (for example,
! 3944: ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
! 3945: ** can be obtained by calling [sqlite3_reset()] on the
! 3946: ** [prepared statement]. ^In the "v2" interface,
! 3947: ** the more specific error code is returned directly by sqlite3_step().
! 3948: **
! 3949: ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
! 3950: ** Perhaps it was called on a [prepared statement] that has
! 3951: ** already been [sqlite3_finalize | finalized] or on one that had
! 3952: ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
! 3953: ** be the case that the same database connection is being used by two or
! 3954: ** more threads at the same moment in time.
! 3955: **
! 3956: ** For all versions of SQLite up to and including 3.6.23.1, a call to
! 3957: ** [sqlite3_reset()] was required after sqlite3_step() returned anything
! 3958: ** other than [SQLITE_ROW] before any subsequent invocation of
! 3959: ** sqlite3_step(). Failure to reset the prepared statement using
! 3960: ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
! 3961: ** sqlite3_step(). But after version 3.6.23.1, sqlite3_step() began
! 3962: ** calling [sqlite3_reset()] automatically in this circumstance rather
! 3963: ** than returning [SQLITE_MISUSE]. This is not considered a compatibility
! 3964: ** break because any application that ever receives an SQLITE_MISUSE error
! 3965: ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
! 3966: ** can be used to restore the legacy behavior.
! 3967: **
! 3968: ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
! 3969: ** API always returns a generic error code, [SQLITE_ERROR], following any
! 3970: ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
! 3971: ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
! 3972: ** specific [error codes] that better describes the error.
! 3973: ** We admit that this is a goofy design. The problem has been fixed
! 3974: ** with the "v2" interface. If you prepare all of your SQL statements
! 3975: ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
! 3976: ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
! 3977: ** then the more specific [error codes] are returned directly
! 3978: ** by sqlite3_step(). The use of the "v2" interface is recommended.
! 3979: */
! 3980: SQLITE_API int sqlite3_step(sqlite3_stmt*);
! 3981:
! 3982: /*
! 3983: ** CAPI3REF: Number of columns in a result set
! 3984: **
! 3985: ** ^The sqlite3_data_count(P) interface returns the number of columns in the
! 3986: ** current row of the result set of [prepared statement] P.
! 3987: ** ^If prepared statement P does not have results ready to return
! 3988: ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
! 3989: ** interfaces) then sqlite3_data_count(P) returns 0.
! 3990: ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
! 3991: ** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
! 3992: ** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
! 3993: ** will return non-zero if previous call to [sqlite3_step](P) returned
! 3994: ** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
! 3995: ** where it always returns zero since each step of that multi-step
! 3996: ** pragma returns 0 columns of data.
! 3997: **
! 3998: ** See also: [sqlite3_column_count()]
! 3999: */
! 4000: SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
! 4001:
! 4002: /*
! 4003: ** CAPI3REF: Fundamental Datatypes
! 4004: ** KEYWORDS: SQLITE_TEXT
! 4005: **
! 4006: ** ^(Every value in SQLite has one of five fundamental datatypes:
! 4007: **
! 4008: ** <ul>
! 4009: ** <li> 64-bit signed integer
! 4010: ** <li> 64-bit IEEE floating point number
! 4011: ** <li> string
! 4012: ** <li> BLOB
! 4013: ** <li> NULL
! 4014: ** </ul>)^
! 4015: **
! 4016: ** These constants are codes for each of those types.
! 4017: **
! 4018: ** Note that the SQLITE_TEXT constant was also used in SQLite version 2
! 4019: ** for a completely different meaning. Software that links against both
! 4020: ** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
! 4021: ** SQLITE_TEXT.
! 4022: */
! 4023: #define SQLITE_INTEGER 1
! 4024: #define SQLITE_FLOAT 2
! 4025: #define SQLITE_BLOB 4
! 4026: #define SQLITE_NULL 5
! 4027: #ifdef SQLITE_TEXT
! 4028: # undef SQLITE_TEXT
! 4029: #else
! 4030: # define SQLITE_TEXT 3
! 4031: #endif
! 4032: #define SQLITE3_TEXT 3
! 4033:
! 4034: /*
! 4035: ** CAPI3REF: Result Values From A Query
! 4036: ** KEYWORDS: {column access functions}
! 4037: **
! 4038: ** These routines form the "result set" interface.
! 4039: **
! 4040: ** ^These routines return information about a single column of the current
! 4041: ** result row of a query. ^In every case the first argument is a pointer
! 4042: ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
! 4043: ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
! 4044: ** and the second argument is the index of the column for which information
! 4045: ** should be returned. ^The leftmost column of the result set has the index 0.
! 4046: ** ^The number of columns in the result can be determined using
! 4047: ** [sqlite3_column_count()].
! 4048: **
! 4049: ** If the SQL statement does not currently point to a valid row, or if the
! 4050: ** column index is out of range, the result is undefined.
! 4051: ** These routines may only be called when the most recent call to
! 4052: ** [sqlite3_step()] has returned [SQLITE_ROW] and neither
! 4053: ** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
! 4054: ** If any of these routines are called after [sqlite3_reset()] or
! 4055: ** [sqlite3_finalize()] or after [sqlite3_step()] has returned
! 4056: ** something other than [SQLITE_ROW], the results are undefined.
! 4057: ** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
! 4058: ** are called from a different thread while any of these routines
! 4059: ** are pending, then the results are undefined.
! 4060: **
! 4061: ** ^The sqlite3_column_type() routine returns the
! 4062: ** [SQLITE_INTEGER | datatype code] for the initial data type
! 4063: ** of the result column. ^The returned value is one of [SQLITE_INTEGER],
! 4064: ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value
! 4065: ** returned by sqlite3_column_type() is only meaningful if no type
! 4066: ** conversions have occurred as described below. After a type conversion,
! 4067: ** the value returned by sqlite3_column_type() is undefined. Future
! 4068: ** versions of SQLite may change the behavior of sqlite3_column_type()
! 4069: ** following a type conversion.
! 4070: **
! 4071: ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
! 4072: ** routine returns the number of bytes in that BLOB or string.
! 4073: ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
! 4074: ** the string to UTF-8 and then returns the number of bytes.
! 4075: ** ^If the result is a numeric value then sqlite3_column_bytes() uses
! 4076: ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
! 4077: ** the number of bytes in that string.
! 4078: ** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
! 4079: **
! 4080: ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
! 4081: ** routine returns the number of bytes in that BLOB or string.
! 4082: ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
! 4083: ** the string to UTF-16 and then returns the number of bytes.
! 4084: ** ^If the result is a numeric value then sqlite3_column_bytes16() uses
! 4085: ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
! 4086: ** the number of bytes in that string.
! 4087: ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
! 4088: **
! 4089: ** ^The values returned by [sqlite3_column_bytes()] and
! 4090: ** [sqlite3_column_bytes16()] do not include the zero terminators at the end
! 4091: ** of the string. ^For clarity: the values returned by
! 4092: ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
! 4093: ** bytes in the string, not the number of characters.
! 4094: **
! 4095: ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
! 4096: ** even empty strings, are always zero-terminated. ^The return
! 4097: ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
! 4098: **
! 4099: ** ^The object returned by [sqlite3_column_value()] is an
! 4100: ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
! 4101: ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
! 4102: ** If the [unprotected sqlite3_value] object returned by
! 4103: ** [sqlite3_column_value()] is used in any other way, including calls
! 4104: ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
! 4105: ** or [sqlite3_value_bytes()], then the behavior is undefined.
! 4106: **
! 4107: ** These routines attempt to convert the value where appropriate. ^For
! 4108: ** example, if the internal representation is FLOAT and a text result
! 4109: ** is requested, [sqlite3_snprintf()] is used internally to perform the
! 4110: ** conversion automatically. ^(The following table details the conversions
! 4111: ** that are applied:
! 4112: **
! 4113: ** <blockquote>
! 4114: ** <table border="1">
! 4115: ** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
! 4116: **
! 4117: ** <tr><td> NULL <td> INTEGER <td> Result is 0
! 4118: ** <tr><td> NULL <td> FLOAT <td> Result is 0.0
! 4119: ** <tr><td> NULL <td> TEXT <td> Result is NULL pointer
! 4120: ** <tr><td> NULL <td> BLOB <td> Result is NULL pointer
! 4121: ** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
! 4122: ** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
! 4123: ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
! 4124: ** <tr><td> FLOAT <td> INTEGER <td> Convert from float to integer
! 4125: ** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
! 4126: ** <tr><td> FLOAT <td> BLOB <td> Same as FLOAT->TEXT
! 4127: ** <tr><td> TEXT <td> INTEGER <td> Use atoi()
! 4128: ** <tr><td> TEXT <td> FLOAT <td> Use atof()
! 4129: ** <tr><td> TEXT <td> BLOB <td> No change
! 4130: ** <tr><td> BLOB <td> INTEGER <td> Convert to TEXT then use atoi()
! 4131: ** <tr><td> BLOB <td> FLOAT <td> Convert to TEXT then use atof()
! 4132: ** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
! 4133: ** </table>
! 4134: ** </blockquote>)^
! 4135: **
! 4136: ** The table above makes reference to standard C library functions atoi()
! 4137: ** and atof(). SQLite does not really use these functions. It has its
! 4138: ** own equivalent internal routines. The atoi() and atof() names are
! 4139: ** used in the table for brevity and because they are familiar to most
! 4140: ** C programmers.
! 4141: **
! 4142: ** Note that when type conversions occur, pointers returned by prior
! 4143: ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
! 4144: ** sqlite3_column_text16() may be invalidated.
! 4145: ** Type conversions and pointer invalidations might occur
! 4146: ** in the following cases:
! 4147: **
! 4148: ** <ul>
! 4149: ** <li> The initial content is a BLOB and sqlite3_column_text() or
! 4150: ** sqlite3_column_text16() is called. A zero-terminator might
! 4151: ** need to be added to the string.</li>
! 4152: ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
! 4153: ** sqlite3_column_text16() is called. The content must be converted
! 4154: ** to UTF-16.</li>
! 4155: ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
! 4156: ** sqlite3_column_text() is called. The content must be converted
! 4157: ** to UTF-8.</li>
! 4158: ** </ul>
! 4159: **
! 4160: ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
! 4161: ** not invalidate a prior pointer, though of course the content of the buffer
! 4162: ** that the prior pointer references will have been modified. Other kinds
! 4163: ** of conversion are done in place when it is possible, but sometimes they
! 4164: ** are not possible and in those cases prior pointers are invalidated.
! 4165: **
! 4166: ** The safest and easiest to remember policy is to invoke these routines
! 4167: ** in one of the following ways:
! 4168: **
! 4169: ** <ul>
! 4170: ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
! 4171: ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
! 4172: ** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
! 4173: ** </ul>
! 4174: **
! 4175: ** In other words, you should call sqlite3_column_text(),
! 4176: ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
! 4177: ** into the desired format, then invoke sqlite3_column_bytes() or
! 4178: ** sqlite3_column_bytes16() to find the size of the result. Do not mix calls
! 4179: ** to sqlite3_column_text() or sqlite3_column_blob() with calls to
! 4180: ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
! 4181: ** with calls to sqlite3_column_bytes().
! 4182: **
! 4183: ** ^The pointers returned are valid until a type conversion occurs as
! 4184: ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
! 4185: ** [sqlite3_finalize()] is called. ^The memory space used to hold strings
! 4186: ** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
! 4187: ** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
! 4188: ** [sqlite3_free()].
! 4189: **
! 4190: ** ^(If a memory allocation error occurs during the evaluation of any
! 4191: ** of these routines, a default value is returned. The default value
! 4192: ** is either the integer 0, the floating point number 0.0, or a NULL
! 4193: ** pointer. Subsequent calls to [sqlite3_errcode()] will return
! 4194: ** [SQLITE_NOMEM].)^
! 4195: */
! 4196: SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
! 4197: SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
! 4198: SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
! 4199: SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
! 4200: SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
! 4201: SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
! 4202: SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
! 4203: SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
! 4204: SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
! 4205: SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
! 4206:
! 4207: /*
! 4208: ** CAPI3REF: Destroy A Prepared Statement Object
! 4209: **
! 4210: ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
! 4211: ** ^If the most recent evaluation of the statement encountered no errors
! 4212: ** or if the statement is never been evaluated, then sqlite3_finalize() returns
! 4213: ** SQLITE_OK. ^If the most recent evaluation of statement S failed, then
! 4214: ** sqlite3_finalize(S) returns the appropriate [error code] or
! 4215: ** [extended error code].
! 4216: **
! 4217: ** ^The sqlite3_finalize(S) routine can be called at any point during
! 4218: ** the life cycle of [prepared statement] S:
! 4219: ** before statement S is ever evaluated, after
! 4220: ** one or more calls to [sqlite3_reset()], or after any call
! 4221: ** to [sqlite3_step()] regardless of whether or not the statement has
! 4222: ** completed execution.
! 4223: **
! 4224: ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
! 4225: **
! 4226: ** The application must finalize every [prepared statement] in order to avoid
! 4227: ** resource leaks. It is a grievous error for the application to try to use
! 4228: ** a prepared statement after it has been finalized. Any use of a prepared
! 4229: ** statement after it has been finalized can result in undefined and
! 4230: ** undesirable behavior such as segfaults and heap corruption.
! 4231: */
! 4232: SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
! 4233:
! 4234: /*
! 4235: ** CAPI3REF: Reset A Prepared Statement Object
! 4236: **
! 4237: ** The sqlite3_reset() function is called to reset a [prepared statement]
! 4238: ** object back to its initial state, ready to be re-executed.
! 4239: ** ^Any SQL statement variables that had values bound to them using
! 4240: ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
! 4241: ** Use [sqlite3_clear_bindings()] to reset the bindings.
! 4242: **
! 4243: ** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
! 4244: ** back to the beginning of its program.
! 4245: **
! 4246: ** ^If the most recent call to [sqlite3_step(S)] for the
! 4247: ** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
! 4248: ** or if [sqlite3_step(S)] has never before been called on S,
! 4249: ** then [sqlite3_reset(S)] returns [SQLITE_OK].
! 4250: **
! 4251: ** ^If the most recent call to [sqlite3_step(S)] for the
! 4252: ** [prepared statement] S indicated an error, then
! 4253: ** [sqlite3_reset(S)] returns an appropriate [error code].
! 4254: **
! 4255: ** ^The [sqlite3_reset(S)] interface does not change the values
! 4256: ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
! 4257: */
! 4258: SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
! 4259:
! 4260: /*
! 4261: ** CAPI3REF: Create Or Redefine SQL Functions
! 4262: ** KEYWORDS: {function creation routines}
! 4263: ** KEYWORDS: {application-defined SQL function}
! 4264: ** KEYWORDS: {application-defined SQL functions}
! 4265: **
! 4266: ** ^These functions (collectively known as "function creation routines")
! 4267: ** are used to add SQL functions or aggregates or to redefine the behavior
! 4268: ** of existing SQL functions or aggregates. The only differences between
! 4269: ** these routines are the text encoding expected for
! 4270: ** the second parameter (the name of the function being created)
! 4271: ** and the presence or absence of a destructor callback for
! 4272: ** the application data pointer.
! 4273: **
! 4274: ** ^The first parameter is the [database connection] to which the SQL
! 4275: ** function is to be added. ^If an application uses more than one database
! 4276: ** connection then application-defined SQL functions must be added
! 4277: ** to each database connection separately.
! 4278: **
! 4279: ** ^The second parameter is the name of the SQL function to be created or
! 4280: ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
! 4281: ** representation, exclusive of the zero-terminator. ^Note that the name
! 4282: ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
! 4283: ** ^Any attempt to create a function with a longer name
! 4284: ** will result in [SQLITE_MISUSE] being returned.
! 4285: **
! 4286: ** ^The third parameter (nArg)
! 4287: ** is the number of arguments that the SQL function or
! 4288: ** aggregate takes. ^If this parameter is -1, then the SQL function or
! 4289: ** aggregate may take any number of arguments between 0 and the limit
! 4290: ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third
! 4291: ** parameter is less than -1 or greater than 127 then the behavior is
! 4292: ** undefined.
! 4293: **
! 4294: ** ^The fourth parameter, eTextRep, specifies what
! 4295: ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
! 4296: ** its parameters. Every SQL function implementation must be able to work
! 4297: ** with UTF-8, UTF-16le, or UTF-16be. But some implementations may be
! 4298: ** more efficient with one encoding than another. ^An application may
! 4299: ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
! 4300: ** times with the same function but with different values of eTextRep.
! 4301: ** ^When multiple implementations of the same function are available, SQLite
! 4302: ** will pick the one that involves the least amount of data conversion.
! 4303: ** If there is only a single implementation which does not care what text
! 4304: ** encoding is used, then the fourth argument should be [SQLITE_ANY].
! 4305: **
! 4306: ** ^(The fifth parameter is an arbitrary pointer. The implementation of the
! 4307: ** function can gain access to this pointer using [sqlite3_user_data()].)^
! 4308: **
! 4309: ** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
! 4310: ** pointers to C-language functions that implement the SQL function or
! 4311: ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
! 4312: ** callback only; NULL pointers must be passed as the xStep and xFinal
! 4313: ** parameters. ^An aggregate SQL function requires an implementation of xStep
! 4314: ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
! 4315: ** SQL function or aggregate, pass NULL pointers for all three function
! 4316: ** callbacks.
! 4317: **
! 4318: ** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
! 4319: ** then it is destructor for the application data pointer.
! 4320: ** The destructor is invoked when the function is deleted, either by being
! 4321: ** overloaded or when the database connection closes.)^
! 4322: ** ^The destructor is also invoked if the call to
! 4323: ** sqlite3_create_function_v2() fails.
! 4324: ** ^When the destructor callback of the tenth parameter is invoked, it
! 4325: ** is passed a single argument which is a copy of the application data
! 4326: ** pointer which was the fifth parameter to sqlite3_create_function_v2().
! 4327: **
! 4328: ** ^It is permitted to register multiple implementations of the same
! 4329: ** functions with the same name but with either differing numbers of
! 4330: ** arguments or differing preferred text encodings. ^SQLite will use
! 4331: ** the implementation that most closely matches the way in which the
! 4332: ** SQL function is used. ^A function implementation with a non-negative
! 4333: ** nArg parameter is a better match than a function implementation with
! 4334: ** a negative nArg. ^A function where the preferred text encoding
! 4335: ** matches the database encoding is a better
! 4336: ** match than a function where the encoding is different.
! 4337: ** ^A function where the encoding difference is between UTF16le and UTF16be
! 4338: ** is a closer match than a function where the encoding difference is
! 4339: ** between UTF8 and UTF16.
! 4340: **
! 4341: ** ^Built-in functions may be overloaded by new application-defined functions.
! 4342: **
! 4343: ** ^An application-defined function is permitted to call other
! 4344: ** SQLite interfaces. However, such calls must not
! 4345: ** close the database connection nor finalize or reset the prepared
! 4346: ** statement in which the function is running.
! 4347: */
! 4348: SQLITE_API int sqlite3_create_function(
! 4349: sqlite3 *db,
! 4350: const char *zFunctionName,
! 4351: int nArg,
! 4352: int eTextRep,
! 4353: void *pApp,
! 4354: void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
! 4355: void (*xStep)(sqlite3_context*,int,sqlite3_value**),
! 4356: void (*xFinal)(sqlite3_context*)
! 4357: );
! 4358: SQLITE_API int sqlite3_create_function16(
! 4359: sqlite3 *db,
! 4360: const void *zFunctionName,
! 4361: int nArg,
! 4362: int eTextRep,
! 4363: void *pApp,
! 4364: void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
! 4365: void (*xStep)(sqlite3_context*,int,sqlite3_value**),
! 4366: void (*xFinal)(sqlite3_context*)
! 4367: );
! 4368: SQLITE_API int sqlite3_create_function_v2(
! 4369: sqlite3 *db,
! 4370: const char *zFunctionName,
! 4371: int nArg,
! 4372: int eTextRep,
! 4373: void *pApp,
! 4374: void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
! 4375: void (*xStep)(sqlite3_context*,int,sqlite3_value**),
! 4376: void (*xFinal)(sqlite3_context*),
! 4377: void(*xDestroy)(void*)
! 4378: );
! 4379:
! 4380: /*
! 4381: ** CAPI3REF: Text Encodings
! 4382: **
! 4383: ** These constant define integer codes that represent the various
! 4384: ** text encodings supported by SQLite.
! 4385: */
! 4386: #define SQLITE_UTF8 1
! 4387: #define SQLITE_UTF16LE 2
! 4388: #define SQLITE_UTF16BE 3
! 4389: #define SQLITE_UTF16 4 /* Use native byte order */
! 4390: #define SQLITE_ANY 5 /* sqlite3_create_function only */
! 4391: #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
! 4392:
! 4393: /*
! 4394: ** CAPI3REF: Deprecated Functions
! 4395: ** DEPRECATED
! 4396: **
! 4397: ** These functions are [deprecated]. In order to maintain
! 4398: ** backwards compatibility with older code, these functions continue
! 4399: ** to be supported. However, new applications should avoid
! 4400: ** the use of these functions. To help encourage people to avoid
! 4401: ** using these functions, we are not going to tell you what they do.
! 4402: */
! 4403: #ifndef SQLITE_OMIT_DEPRECATED
! 4404: SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
! 4405: SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
! 4406: SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
! 4407: SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
! 4408: SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
! 4409: SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64);
! 4410: #endif
! 4411:
! 4412: /*
! 4413: ** CAPI3REF: Obtaining SQL Function Parameter Values
! 4414: **
! 4415: ** The C-language implementation of SQL functions and aggregates uses
! 4416: ** this set of interface routines to access the parameter values on
! 4417: ** the function or aggregate.
! 4418: **
! 4419: ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
! 4420: ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
! 4421: ** define callbacks that implement the SQL functions and aggregates.
! 4422: ** The 3rd parameter to these callbacks is an array of pointers to
! 4423: ** [protected sqlite3_value] objects. There is one [sqlite3_value] object for
! 4424: ** each parameter to the SQL function. These routines are used to
! 4425: ** extract values from the [sqlite3_value] objects.
! 4426: **
! 4427: ** These routines work only with [protected sqlite3_value] objects.
! 4428: ** Any attempt to use these routines on an [unprotected sqlite3_value]
! 4429: ** object results in undefined behavior.
! 4430: **
! 4431: ** ^These routines work just like the corresponding [column access functions]
! 4432: ** except that these routines take a single [protected sqlite3_value] object
! 4433: ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
! 4434: **
! 4435: ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
! 4436: ** in the native byte-order of the host machine. ^The
! 4437: ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
! 4438: ** extract UTF-16 strings as big-endian and little-endian respectively.
! 4439: **
! 4440: ** ^(The sqlite3_value_numeric_type() interface attempts to apply
! 4441: ** numeric affinity to the value. This means that an attempt is
! 4442: ** made to convert the value to an integer or floating point. If
! 4443: ** such a conversion is possible without loss of information (in other
! 4444: ** words, if the value is a string that looks like a number)
! 4445: ** then the conversion is performed. Otherwise no conversion occurs.
! 4446: ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
! 4447: **
! 4448: ** Please pay particular attention to the fact that the pointer returned
! 4449: ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
! 4450: ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
! 4451: ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
! 4452: ** or [sqlite3_value_text16()].
! 4453: **
! 4454: ** These routines must be called from the same thread as
! 4455: ** the SQL function that supplied the [sqlite3_value*] parameters.
! 4456: */
! 4457: SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
! 4458: SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
! 4459: SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
! 4460: SQLITE_API double sqlite3_value_double(sqlite3_value*);
! 4461: SQLITE_API int sqlite3_value_int(sqlite3_value*);
! 4462: SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
! 4463: SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
! 4464: SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
! 4465: SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
! 4466: SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
! 4467: SQLITE_API int sqlite3_value_type(sqlite3_value*);
! 4468: SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
! 4469:
! 4470: /*
! 4471: ** CAPI3REF: Obtain Aggregate Function Context
! 4472: **
! 4473: ** Implementations of aggregate SQL functions use this
! 4474: ** routine to allocate memory for storing their state.
! 4475: **
! 4476: ** ^The first time the sqlite3_aggregate_context(C,N) routine is called
! 4477: ** for a particular aggregate function, SQLite
! 4478: ** allocates N of memory, zeroes out that memory, and returns a pointer
! 4479: ** to the new memory. ^On second and subsequent calls to
! 4480: ** sqlite3_aggregate_context() for the same aggregate function instance,
! 4481: ** the same buffer is returned. Sqlite3_aggregate_context() is normally
! 4482: ** called once for each invocation of the xStep callback and then one
! 4483: ** last time when the xFinal callback is invoked. ^(When no rows match
! 4484: ** an aggregate query, the xStep() callback of the aggregate function
! 4485: ** implementation is never called and xFinal() is called exactly once.
! 4486: ** In those cases, sqlite3_aggregate_context() might be called for the
! 4487: ** first time from within xFinal().)^
! 4488: **
! 4489: ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer if N is
! 4490: ** less than or equal to zero or if a memory allocate error occurs.
! 4491: **
! 4492: ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
! 4493: ** determined by the N parameter on first successful call. Changing the
! 4494: ** value of N in subsequent call to sqlite3_aggregate_context() within
! 4495: ** the same aggregate function instance will not resize the memory
! 4496: ** allocation.)^
! 4497: **
! 4498: ** ^SQLite automatically frees the memory allocated by
! 4499: ** sqlite3_aggregate_context() when the aggregate query concludes.
! 4500: **
! 4501: ** The first parameter must be a copy of the
! 4502: ** [sqlite3_context | SQL function context] that is the first parameter
! 4503: ** to the xStep or xFinal callback routine that implements the aggregate
! 4504: ** function.
! 4505: **
! 4506: ** This routine must be called from the same thread in which
! 4507: ** the aggregate SQL function is running.
! 4508: */
! 4509: SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
! 4510:
! 4511: /*
! 4512: ** CAPI3REF: User Data For Functions
! 4513: **
! 4514: ** ^The sqlite3_user_data() interface returns a copy of
! 4515: ** the pointer that was the pUserData parameter (the 5th parameter)
! 4516: ** of the [sqlite3_create_function()]
! 4517: ** and [sqlite3_create_function16()] routines that originally
! 4518: ** registered the application defined function.
! 4519: **
! 4520: ** This routine must be called from the same thread in which
! 4521: ** the application-defined function is running.
! 4522: */
! 4523: SQLITE_API void *sqlite3_user_data(sqlite3_context*);
! 4524:
! 4525: /*
! 4526: ** CAPI3REF: Database Connection For Functions
! 4527: **
! 4528: ** ^The sqlite3_context_db_handle() interface returns a copy of
! 4529: ** the pointer to the [database connection] (the 1st parameter)
! 4530: ** of the [sqlite3_create_function()]
! 4531: ** and [sqlite3_create_function16()] routines that originally
! 4532: ** registered the application defined function.
! 4533: */
! 4534: SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
! 4535:
! 4536: /*
! 4537: ** CAPI3REF: Function Auxiliary Data
! 4538: **
! 4539: ** The following two functions may be used by scalar SQL functions to
! 4540: ** associate metadata with argument values. If the same value is passed to
! 4541: ** multiple invocations of the same SQL function during query execution, under
! 4542: ** some circumstances the associated metadata may be preserved. This may
! 4543: ** be used, for example, to add a regular-expression matching scalar
! 4544: ** function. The compiled version of the regular expression is stored as
! 4545: ** metadata associated with the SQL value passed as the regular expression
! 4546: ** pattern. The compiled regular expression can be reused on multiple
! 4547: ** invocations of the same function so that the original pattern string
! 4548: ** does not need to be recompiled on each invocation.
! 4549: **
! 4550: ** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
! 4551: ** associated by the sqlite3_set_auxdata() function with the Nth argument
! 4552: ** value to the application-defined function. ^If no metadata has been ever
! 4553: ** been set for the Nth argument of the function, or if the corresponding
! 4554: ** function parameter has changed since the meta-data was set,
! 4555: ** then sqlite3_get_auxdata() returns a NULL pointer.
! 4556: **
! 4557: ** ^The sqlite3_set_auxdata() interface saves the metadata
! 4558: ** pointed to by its 3rd parameter as the metadata for the N-th
! 4559: ** argument of the application-defined function. Subsequent
! 4560: ** calls to sqlite3_get_auxdata() might return this data, if it has
! 4561: ** not been destroyed.
! 4562: ** ^If it is not NULL, SQLite will invoke the destructor
! 4563: ** function given by the 4th parameter to sqlite3_set_auxdata() on
! 4564: ** the metadata when the corresponding function parameter changes
! 4565: ** or when the SQL statement completes, whichever comes first.
! 4566: **
! 4567: ** SQLite is free to call the destructor and drop metadata on any
! 4568: ** parameter of any function at any time. ^The only guarantee is that
! 4569: ** the destructor will be called before the metadata is dropped.
! 4570: **
! 4571: ** ^(In practice, metadata is preserved between function calls for
! 4572: ** expressions that are constant at compile time. This includes literal
! 4573: ** values and [parameters].)^
! 4574: **
! 4575: ** These routines must be called from the same thread in which
! 4576: ** the SQL function is running.
! 4577: */
! 4578: SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
! 4579: SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
! 4580:
! 4581:
! 4582: /*
! 4583: ** CAPI3REF: Constants Defining Special Destructor Behavior
! 4584: **
! 4585: ** These are special values for the destructor that is passed in as the
! 4586: ** final argument to routines like [sqlite3_result_blob()]. ^If the destructor
! 4587: ** argument is SQLITE_STATIC, it means that the content pointer is constant
! 4588: ** and will never change. It does not need to be destroyed. ^The
! 4589: ** SQLITE_TRANSIENT value means that the content will likely change in
! 4590: ** the near future and that SQLite should make its own private copy of
! 4591: ** the content before returning.
! 4592: **
! 4593: ** The typedef is necessary to work around problems in certain
! 4594: ** C++ compilers. See ticket #2191.
! 4595: */
! 4596: typedef void (*sqlite3_destructor_type)(void*);
! 4597: #define SQLITE_STATIC ((sqlite3_destructor_type)0)
! 4598: #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
! 4599:
! 4600: /*
! 4601: ** CAPI3REF: Setting The Result Of An SQL Function
! 4602: **
! 4603: ** These routines are used by the xFunc or xFinal callbacks that
! 4604: ** implement SQL functions and aggregates. See
! 4605: ** [sqlite3_create_function()] and [sqlite3_create_function16()]
! 4606: ** for additional information.
! 4607: **
! 4608: ** These functions work very much like the [parameter binding] family of
! 4609: ** functions used to bind values to host parameters in prepared statements.
! 4610: ** Refer to the [SQL parameter] documentation for additional information.
! 4611: **
! 4612: ** ^The sqlite3_result_blob() interface sets the result from
! 4613: ** an application-defined function to be the BLOB whose content is pointed
! 4614: ** to by the second parameter and which is N bytes long where N is the
! 4615: ** third parameter.
! 4616: **
! 4617: ** ^The sqlite3_result_zeroblob() interfaces set the result of
! 4618: ** the application-defined function to be a BLOB containing all zero
! 4619: ** bytes and N bytes in size, where N is the value of the 2nd parameter.
! 4620: **
! 4621: ** ^The sqlite3_result_double() interface sets the result from
! 4622: ** an application-defined function to be a floating point value specified
! 4623: ** by its 2nd argument.
! 4624: **
! 4625: ** ^The sqlite3_result_error() and sqlite3_result_error16() functions
! 4626: ** cause the implemented SQL function to throw an exception.
! 4627: ** ^SQLite uses the string pointed to by the
! 4628: ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
! 4629: ** as the text of an error message. ^SQLite interprets the error
! 4630: ** message string from sqlite3_result_error() as UTF-8. ^SQLite
! 4631: ** interprets the string from sqlite3_result_error16() as UTF-16 in native
! 4632: ** byte order. ^If the third parameter to sqlite3_result_error()
! 4633: ** or sqlite3_result_error16() is negative then SQLite takes as the error
! 4634: ** message all text up through the first zero character.
! 4635: ** ^If the third parameter to sqlite3_result_error() or
! 4636: ** sqlite3_result_error16() is non-negative then SQLite takes that many
! 4637: ** bytes (not characters) from the 2nd parameter as the error message.
! 4638: ** ^The sqlite3_result_error() and sqlite3_result_error16()
! 4639: ** routines make a private copy of the error message text before
! 4640: ** they return. Hence, the calling function can deallocate or
! 4641: ** modify the text after they return without harm.
! 4642: ** ^The sqlite3_result_error_code() function changes the error code
! 4643: ** returned by SQLite as a result of an error in a function. ^By default,
! 4644: ** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
! 4645: ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
! 4646: **
! 4647: ** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
! 4648: ** indicating that a string or BLOB is too long to represent.
! 4649: **
! 4650: ** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
! 4651: ** indicating that a memory allocation failed.
! 4652: **
! 4653: ** ^The sqlite3_result_int() interface sets the return value
! 4654: ** of the application-defined function to be the 32-bit signed integer
! 4655: ** value given in the 2nd argument.
! 4656: ** ^The sqlite3_result_int64() interface sets the return value
! 4657: ** of the application-defined function to be the 64-bit signed integer
! 4658: ** value given in the 2nd argument.
! 4659: **
! 4660: ** ^The sqlite3_result_null() interface sets the return value
! 4661: ** of the application-defined function to be NULL.
! 4662: **
! 4663: ** ^The sqlite3_result_text(), sqlite3_result_text16(),
! 4664: ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
! 4665: ** set the return value of the application-defined function to be
! 4666: ** a text string which is represented as UTF-8, UTF-16 native byte order,
! 4667: ** UTF-16 little endian, or UTF-16 big endian, respectively.
! 4668: ** ^SQLite takes the text result from the application from
! 4669: ** the 2nd parameter of the sqlite3_result_text* interfaces.
! 4670: ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
! 4671: ** is negative, then SQLite takes result text from the 2nd parameter
! 4672: ** through the first zero character.
! 4673: ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
! 4674: ** is non-negative, then as many bytes (not characters) of the text
! 4675: ** pointed to by the 2nd parameter are taken as the application-defined
! 4676: ** function result. If the 3rd parameter is non-negative, then it
! 4677: ** must be the byte offset into the string where the NUL terminator would
! 4678: ** appear if the string where NUL terminated. If any NUL characters occur
! 4679: ** in the string at a byte offset that is less than the value of the 3rd
! 4680: ** parameter, then the resulting string will contain embedded NULs and the
! 4681: ** result of expressions operating on strings with embedded NULs is undefined.
! 4682: ** ^If the 4th parameter to the sqlite3_result_text* interfaces
! 4683: ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
! 4684: ** function as the destructor on the text or BLOB result when it has
! 4685: ** finished using that result.
! 4686: ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
! 4687: ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
! 4688: ** assumes that the text or BLOB result is in constant space and does not
! 4689: ** copy the content of the parameter nor call a destructor on the content
! 4690: ** when it has finished using that result.
! 4691: ** ^If the 4th parameter to the sqlite3_result_text* interfaces
! 4692: ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
! 4693: ** then SQLite makes a copy of the result into space obtained from
! 4694: ** from [sqlite3_malloc()] before it returns.
! 4695: **
! 4696: ** ^The sqlite3_result_value() interface sets the result of
! 4697: ** the application-defined function to be a copy the
! 4698: ** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
! 4699: ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
! 4700: ** so that the [sqlite3_value] specified in the parameter may change or
! 4701: ** be deallocated after sqlite3_result_value() returns without harm.
! 4702: ** ^A [protected sqlite3_value] object may always be used where an
! 4703: ** [unprotected sqlite3_value] object is required, so either
! 4704: ** kind of [sqlite3_value] object can be used with this interface.
! 4705: **
! 4706: ** If these routines are called from within the different thread
! 4707: ** than the one containing the application-defined function that received
! 4708: ** the [sqlite3_context] pointer, the results are undefined.
! 4709: */
! 4710: SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
! 4711: SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
! 4712: SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
! 4713: SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
! 4714: SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
! 4715: SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
! 4716: SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
! 4717: SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
! 4718: SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
! 4719: SQLITE_API void sqlite3_result_null(sqlite3_context*);
! 4720: SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
! 4721: SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
! 4722: SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
! 4723: SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
! 4724: SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
! 4725: SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
! 4726:
! 4727: /*
! 4728: ** CAPI3REF: Define New Collating Sequences
! 4729: **
! 4730: ** ^These functions add, remove, or modify a [collation] associated
! 4731: ** with the [database connection] specified as the first argument.
! 4732: **
! 4733: ** ^The name of the collation is a UTF-8 string
! 4734: ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
! 4735: ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
! 4736: ** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
! 4737: ** considered to be the same name.
! 4738: **
! 4739: ** ^(The third argument (eTextRep) must be one of the constants:
! 4740: ** <ul>
! 4741: ** <li> [SQLITE_UTF8],
! 4742: ** <li> [SQLITE_UTF16LE],
! 4743: ** <li> [SQLITE_UTF16BE],
! 4744: ** <li> [SQLITE_UTF16], or
! 4745: ** <li> [SQLITE_UTF16_ALIGNED].
! 4746: ** </ul>)^
! 4747: ** ^The eTextRep argument determines the encoding of strings passed
! 4748: ** to the collating function callback, xCallback.
! 4749: ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
! 4750: ** force strings to be UTF16 with native byte order.
! 4751: ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
! 4752: ** on an even byte address.
! 4753: **
! 4754: ** ^The fourth argument, pArg, is an application data pointer that is passed
! 4755: ** through as the first argument to the collating function callback.
! 4756: **
! 4757: ** ^The fifth argument, xCallback, is a pointer to the collating function.
! 4758: ** ^Multiple collating functions can be registered using the same name but
! 4759: ** with different eTextRep parameters and SQLite will use whichever
! 4760: ** function requires the least amount of data transformation.
! 4761: ** ^If the xCallback argument is NULL then the collating function is
! 4762: ** deleted. ^When all collating functions having the same name are deleted,
! 4763: ** that collation is no longer usable.
! 4764: **
! 4765: ** ^The collating function callback is invoked with a copy of the pArg
! 4766: ** application data pointer and with two strings in the encoding specified
! 4767: ** by the eTextRep argument. The collating function must return an
! 4768: ** integer that is negative, zero, or positive
! 4769: ** if the first string is less than, equal to, or greater than the second,
! 4770: ** respectively. A collating function must always return the same answer
! 4771: ** given the same inputs. If two or more collating functions are registered
! 4772: ** to the same collation name (using different eTextRep values) then all
! 4773: ** must give an equivalent answer when invoked with equivalent strings.
! 4774: ** The collating function must obey the following properties for all
! 4775: ** strings A, B, and C:
! 4776: **
! 4777: ** <ol>
! 4778: ** <li> If A==B then B==A.
! 4779: ** <li> If A==B and B==C then A==C.
! 4780: ** <li> If A<B THEN B>A.
! 4781: ** <li> If A<B and B<C then A<C.
! 4782: ** </ol>
! 4783: **
! 4784: ** If a collating function fails any of the above constraints and that
! 4785: ** collating function is registered and used, then the behavior of SQLite
! 4786: ** is undefined.
! 4787: **
! 4788: ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
! 4789: ** with the addition that the xDestroy callback is invoked on pArg when
! 4790: ** the collating function is deleted.
! 4791: ** ^Collating functions are deleted when they are overridden by later
! 4792: ** calls to the collation creation functions or when the
! 4793: ** [database connection] is closed using [sqlite3_close()].
! 4794: **
! 4795: ** ^The xDestroy callback is <u>not</u> called if the
! 4796: ** sqlite3_create_collation_v2() function fails. Applications that invoke
! 4797: ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
! 4798: ** check the return code and dispose of the application data pointer
! 4799: ** themselves rather than expecting SQLite to deal with it for them.
! 4800: ** This is different from every other SQLite interface. The inconsistency
! 4801: ** is unfortunate but cannot be changed without breaking backwards
! 4802: ** compatibility.
! 4803: **
! 4804: ** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
! 4805: */
! 4806: SQLITE_API int sqlite3_create_collation(
! 4807: sqlite3*,
! 4808: const char *zName,
! 4809: int eTextRep,
! 4810: void *pArg,
! 4811: int(*xCompare)(void*,int,const void*,int,const void*)
! 4812: );
! 4813: SQLITE_API int sqlite3_create_collation_v2(
! 4814: sqlite3*,
! 4815: const char *zName,
! 4816: int eTextRep,
! 4817: void *pArg,
! 4818: int(*xCompare)(void*,int,const void*,int,const void*),
! 4819: void(*xDestroy)(void*)
! 4820: );
! 4821: SQLITE_API int sqlite3_create_collation16(
! 4822: sqlite3*,
! 4823: const void *zName,
! 4824: int eTextRep,
! 4825: void *pArg,
! 4826: int(*xCompare)(void*,int,const void*,int,const void*)
! 4827: );
! 4828:
! 4829: /*
! 4830: ** CAPI3REF: Collation Needed Callbacks
! 4831: **
! 4832: ** ^To avoid having to register all collation sequences before a database
! 4833: ** can be used, a single callback function may be registered with the
! 4834: ** [database connection] to be invoked whenever an undefined collation
! 4835: ** sequence is required.
! 4836: **
! 4837: ** ^If the function is registered using the sqlite3_collation_needed() API,
! 4838: ** then it is passed the names of undefined collation sequences as strings
! 4839: ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
! 4840: ** the names are passed as UTF-16 in machine native byte order.
! 4841: ** ^A call to either function replaces the existing collation-needed callback.
! 4842: **
! 4843: ** ^(When the callback is invoked, the first argument passed is a copy
! 4844: ** of the second argument to sqlite3_collation_needed() or
! 4845: ** sqlite3_collation_needed16(). The second argument is the database
! 4846: ** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
! 4847: ** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
! 4848: ** sequence function required. The fourth parameter is the name of the
! 4849: ** required collation sequence.)^
! 4850: **
! 4851: ** The callback function should register the desired collation using
! 4852: ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
! 4853: ** [sqlite3_create_collation_v2()].
! 4854: */
! 4855: SQLITE_API int sqlite3_collation_needed(
! 4856: sqlite3*,
! 4857: void*,
! 4858: void(*)(void*,sqlite3*,int eTextRep,const char*)
! 4859: );
! 4860: SQLITE_API int sqlite3_collation_needed16(
! 4861: sqlite3*,
! 4862: void*,
! 4863: void(*)(void*,sqlite3*,int eTextRep,const void*)
! 4864: );
! 4865:
! 4866: #ifdef SQLITE_HAS_CODEC
! 4867: /*
! 4868: ** Specify the key for an encrypted database. This routine should be
! 4869: ** called right after sqlite3_open().
! 4870: **
! 4871: ** The code to implement this API is not available in the public release
! 4872: ** of SQLite.
! 4873: */
! 4874: SQLITE_API int sqlite3_key(
! 4875: sqlite3 *db, /* Database to be rekeyed */
! 4876: const void *pKey, int nKey /* The key */
! 4877: );
! 4878:
! 4879: /*
! 4880: ** Change the key on an open database. If the current database is not
! 4881: ** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the
! 4882: ** database is decrypted.
! 4883: **
! 4884: ** The code to implement this API is not available in the public release
! 4885: ** of SQLite.
! 4886: */
! 4887: SQLITE_API int sqlite3_rekey(
! 4888: sqlite3 *db, /* Database to be rekeyed */
! 4889: const void *pKey, int nKey /* The new key */
! 4890: );
! 4891:
! 4892: /*
! 4893: ** Specify the activation key for a SEE database. Unless
! 4894: ** activated, none of the SEE routines will work.
! 4895: */
! 4896: SQLITE_API void sqlite3_activate_see(
! 4897: const char *zPassPhrase /* Activation phrase */
! 4898: );
! 4899: #endif
! 4900:
! 4901: #ifdef SQLITE_ENABLE_CEROD
! 4902: /*
! 4903: ** Specify the activation key for a CEROD database. Unless
! 4904: ** activated, none of the CEROD routines will work.
! 4905: */
! 4906: SQLITE_API void sqlite3_activate_cerod(
! 4907: const char *zPassPhrase /* Activation phrase */
! 4908: );
! 4909: #endif
! 4910:
! 4911: /*
! 4912: ** CAPI3REF: Suspend Execution For A Short Time
! 4913: **
! 4914: ** The sqlite3_sleep() function causes the current thread to suspend execution
! 4915: ** for at least a number of milliseconds specified in its parameter.
! 4916: **
! 4917: ** If the operating system does not support sleep requests with
! 4918: ** millisecond time resolution, then the time will be rounded up to
! 4919: ** the nearest second. The number of milliseconds of sleep actually
! 4920: ** requested from the operating system is returned.
! 4921: **
! 4922: ** ^SQLite implements this interface by calling the xSleep()
! 4923: ** method of the default [sqlite3_vfs] object. If the xSleep() method
! 4924: ** of the default VFS is not implemented correctly, or not implemented at
! 4925: ** all, then the behavior of sqlite3_sleep() may deviate from the description
! 4926: ** in the previous paragraphs.
! 4927: */
! 4928: SQLITE_API int sqlite3_sleep(int);
! 4929:
! 4930: /*
! 4931: ** CAPI3REF: Name Of The Folder Holding Temporary Files
! 4932: **
! 4933: ** ^(If this global variable is made to point to a string which is
! 4934: ** the name of a folder (a.k.a. directory), then all temporary files
! 4935: ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
! 4936: ** will be placed in that directory.)^ ^If this variable
! 4937: ** is a NULL pointer, then SQLite performs a search for an appropriate
! 4938: ** temporary file directory.
! 4939: **
! 4940: ** It is not safe to read or modify this variable in more than one
! 4941: ** thread at a time. It is not safe to read or modify this variable
! 4942: ** if a [database connection] is being used at the same time in a separate
! 4943: ** thread.
! 4944: ** It is intended that this variable be set once
! 4945: ** as part of process initialization and before any SQLite interface
! 4946: ** routines have been called and that this variable remain unchanged
! 4947: ** thereafter.
! 4948: **
! 4949: ** ^The [temp_store_directory pragma] may modify this variable and cause
! 4950: ** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
! 4951: ** the [temp_store_directory pragma] always assumes that any string
! 4952: ** that this variable points to is held in memory obtained from
! 4953: ** [sqlite3_malloc] and the pragma may attempt to free that memory
! 4954: ** using [sqlite3_free].
! 4955: ** Hence, if this variable is modified directly, either it should be
! 4956: ** made NULL or made to point to memory obtained from [sqlite3_malloc]
! 4957: ** or else the use of the [temp_store_directory pragma] should be avoided.
! 4958: */
! 4959: SQLITE_API char *sqlite3_temp_directory;
! 4960:
! 4961: /*
! 4962: ** CAPI3REF: Test For Auto-Commit Mode
! 4963: ** KEYWORDS: {autocommit mode}
! 4964: **
! 4965: ** ^The sqlite3_get_autocommit() interface returns non-zero or
! 4966: ** zero if the given database connection is or is not in autocommit mode,
! 4967: ** respectively. ^Autocommit mode is on by default.
! 4968: ** ^Autocommit mode is disabled by a [BEGIN] statement.
! 4969: ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
! 4970: **
! 4971: ** If certain kinds of errors occur on a statement within a multi-statement
! 4972: ** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
! 4973: ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
! 4974: ** transaction might be rolled back automatically. The only way to
! 4975: ** find out whether SQLite automatically rolled back the transaction after
! 4976: ** an error is to use this function.
! 4977: **
! 4978: ** If another thread changes the autocommit status of the database
! 4979: ** connection while this routine is running, then the return value
! 4980: ** is undefined.
! 4981: */
! 4982: SQLITE_API int sqlite3_get_autocommit(sqlite3*);
! 4983:
! 4984: /*
! 4985: ** CAPI3REF: Find The Database Handle Of A Prepared Statement
! 4986: **
! 4987: ** ^The sqlite3_db_handle interface returns the [database connection] handle
! 4988: ** to which a [prepared statement] belongs. ^The [database connection]
! 4989: ** returned by sqlite3_db_handle is the same [database connection]
! 4990: ** that was the first argument
! 4991: ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
! 4992: ** create the statement in the first place.
! 4993: */
! 4994: SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
! 4995:
! 4996: /*
! 4997: ** CAPI3REF: Return The Filename For A Database Connection
! 4998: **
! 4999: ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
! 5000: ** associated with database N of connection D. ^The main database file
! 5001: ** has the name "main". If there is no attached database N on the database
! 5002: ** connection D, or if database N is a temporary or in-memory database, then
! 5003: ** a NULL pointer is returned.
! 5004: **
! 5005: ** ^The filename returned by this function is the output of the
! 5006: ** xFullPathname method of the [VFS]. ^In other words, the filename
! 5007: ** will be an absolute pathname, even if the filename used
! 5008: ** to open the database originally was a URI or relative pathname.
! 5009: */
! 5010: SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
! 5011:
! 5012: /*
! 5013: ** CAPI3REF: Find the next prepared statement
! 5014: **
! 5015: ** ^This interface returns a pointer to the next [prepared statement] after
! 5016: ** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
! 5017: ** then this interface returns a pointer to the first prepared statement
! 5018: ** associated with the database connection pDb. ^If no prepared statement
! 5019: ** satisfies the conditions of this routine, it returns NULL.
! 5020: **
! 5021: ** The [database connection] pointer D in a call to
! 5022: ** [sqlite3_next_stmt(D,S)] must refer to an open database
! 5023: ** connection and in particular must not be a NULL pointer.
! 5024: */
! 5025: SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
! 5026:
! 5027: /*
! 5028: ** CAPI3REF: Commit And Rollback Notification Callbacks
! 5029: **
! 5030: ** ^The sqlite3_commit_hook() interface registers a callback
! 5031: ** function to be invoked whenever a transaction is [COMMIT | committed].
! 5032: ** ^Any callback set by a previous call to sqlite3_commit_hook()
! 5033: ** for the same database connection is overridden.
! 5034: ** ^The sqlite3_rollback_hook() interface registers a callback
! 5035: ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
! 5036: ** ^Any callback set by a previous call to sqlite3_rollback_hook()
! 5037: ** for the same database connection is overridden.
! 5038: ** ^The pArg argument is passed through to the callback.
! 5039: ** ^If the callback on a commit hook function returns non-zero,
! 5040: ** then the commit is converted into a rollback.
! 5041: **
! 5042: ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
! 5043: ** return the P argument from the previous call of the same function
! 5044: ** on the same [database connection] D, or NULL for
! 5045: ** the first call for each function on D.
! 5046: **
! 5047: ** The commit and rollback hook callbacks are not reentrant.
! 5048: ** The callback implementation must not do anything that will modify
! 5049: ** the database connection that invoked the callback. Any actions
! 5050: ** to modify the database connection must be deferred until after the
! 5051: ** completion of the [sqlite3_step()] call that triggered the commit
! 5052: ** or rollback hook in the first place.
! 5053: ** Note that running any other SQL statements, including SELECT statements,
! 5054: ** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
! 5055: ** the database connections for the meaning of "modify" in this paragraph.
! 5056: **
! 5057: ** ^Registering a NULL function disables the callback.
! 5058: **
! 5059: ** ^When the commit hook callback routine returns zero, the [COMMIT]
! 5060: ** operation is allowed to continue normally. ^If the commit hook
! 5061: ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
! 5062: ** ^The rollback hook is invoked on a rollback that results from a commit
! 5063: ** hook returning non-zero, just as it would be with any other rollback.
! 5064: **
! 5065: ** ^For the purposes of this API, a transaction is said to have been
! 5066: ** rolled back if an explicit "ROLLBACK" statement is executed, or
! 5067: ** an error or constraint causes an implicit rollback to occur.
! 5068: ** ^The rollback callback is not invoked if a transaction is
! 5069: ** automatically rolled back because the database connection is closed.
! 5070: **
! 5071: ** See also the [sqlite3_update_hook()] interface.
! 5072: */
! 5073: SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
! 5074: SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
! 5075:
! 5076: /*
! 5077: ** CAPI3REF: Data Change Notification Callbacks
! 5078: **
! 5079: ** ^The sqlite3_update_hook() interface registers a callback function
! 5080: ** with the [database connection] identified by the first argument
! 5081: ** to be invoked whenever a row is updated, inserted or deleted.
! 5082: ** ^Any callback set by a previous call to this function
! 5083: ** for the same database connection is overridden.
! 5084: **
! 5085: ** ^The second argument is a pointer to the function to invoke when a
! 5086: ** row is updated, inserted or deleted.
! 5087: ** ^The first argument to the callback is a copy of the third argument
! 5088: ** to sqlite3_update_hook().
! 5089: ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
! 5090: ** or [SQLITE_UPDATE], depending on the operation that caused the callback
! 5091: ** to be invoked.
! 5092: ** ^The third and fourth arguments to the callback contain pointers to the
! 5093: ** database and table name containing the affected row.
! 5094: ** ^The final callback parameter is the [rowid] of the row.
! 5095: ** ^In the case of an update, this is the [rowid] after the update takes place.
! 5096: **
! 5097: ** ^(The update hook is not invoked when internal system tables are
! 5098: ** modified (i.e. sqlite_master and sqlite_sequence).)^
! 5099: **
! 5100: ** ^In the current implementation, the update hook
! 5101: ** is not invoked when duplication rows are deleted because of an
! 5102: ** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
! 5103: ** invoked when rows are deleted using the [truncate optimization].
! 5104: ** The exceptions defined in this paragraph might change in a future
! 5105: ** release of SQLite.
! 5106: **
! 5107: ** The update hook implementation must not do anything that will modify
! 5108: ** the database connection that invoked the update hook. Any actions
! 5109: ** to modify the database connection must be deferred until after the
! 5110: ** completion of the [sqlite3_step()] call that triggered the update hook.
! 5111: ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
! 5112: ** database connections for the meaning of "modify" in this paragraph.
! 5113: **
! 5114: ** ^The sqlite3_update_hook(D,C,P) function
! 5115: ** returns the P argument from the previous call
! 5116: ** on the same [database connection] D, or NULL for
! 5117: ** the first call on D.
! 5118: **
! 5119: ** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
! 5120: ** interfaces.
! 5121: */
! 5122: SQLITE_API void *sqlite3_update_hook(
! 5123: sqlite3*,
! 5124: void(*)(void *,int ,char const *,char const *,sqlite3_int64),
! 5125: void*
! 5126: );
! 5127:
! 5128: /*
! 5129: ** CAPI3REF: Enable Or Disable Shared Pager Cache
! 5130: ** KEYWORDS: {shared cache}
! 5131: **
! 5132: ** ^(This routine enables or disables the sharing of the database cache
! 5133: ** and schema data structures between [database connection | connections]
! 5134: ** to the same database. Sharing is enabled if the argument is true
! 5135: ** and disabled if the argument is false.)^
! 5136: **
! 5137: ** ^Cache sharing is enabled and disabled for an entire process.
! 5138: ** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
! 5139: ** sharing was enabled or disabled for each thread separately.
! 5140: **
! 5141: ** ^(The cache sharing mode set by this interface effects all subsequent
! 5142: ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
! 5143: ** Existing database connections continue use the sharing mode
! 5144: ** that was in effect at the time they were opened.)^
! 5145: **
! 5146: ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
! 5147: ** successfully. An [error code] is returned otherwise.)^
! 5148: **
! 5149: ** ^Shared cache is disabled by default. But this might change in
! 5150: ** future releases of SQLite. Applications that care about shared
! 5151: ** cache setting should set it explicitly.
! 5152: **
! 5153: ** See Also: [SQLite Shared-Cache Mode]
! 5154: */
! 5155: SQLITE_API int sqlite3_enable_shared_cache(int);
! 5156:
! 5157: /*
! 5158: ** CAPI3REF: Attempt To Free Heap Memory
! 5159: **
! 5160: ** ^The sqlite3_release_memory() interface attempts to free N bytes
! 5161: ** of heap memory by deallocating non-essential memory allocations
! 5162: ** held by the database library. Memory used to cache database
! 5163: ** pages to improve performance is an example of non-essential memory.
! 5164: ** ^sqlite3_release_memory() returns the number of bytes actually freed,
! 5165: ** which might be more or less than the amount requested.
! 5166: ** ^The sqlite3_release_memory() routine is a no-op returning zero
! 5167: ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
! 5168: **
! 5169: ** See also: [sqlite3_db_release_memory()]
! 5170: */
! 5171: SQLITE_API int sqlite3_release_memory(int);
! 5172:
! 5173: /*
! 5174: ** CAPI3REF: Free Memory Used By A Database Connection
! 5175: **
! 5176: ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
! 5177: ** memory as possible from database connection D. Unlike the
! 5178: ** [sqlite3_release_memory()] interface, this interface is effect even
! 5179: ** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
! 5180: ** omitted.
! 5181: **
! 5182: ** See also: [sqlite3_release_memory()]
! 5183: */
! 5184: SQLITE_API int sqlite3_db_release_memory(sqlite3*);
! 5185:
! 5186: /*
! 5187: ** CAPI3REF: Impose A Limit On Heap Size
! 5188: **
! 5189: ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
! 5190: ** soft limit on the amount of heap memory that may be allocated by SQLite.
! 5191: ** ^SQLite strives to keep heap memory utilization below the soft heap
! 5192: ** limit by reducing the number of pages held in the page cache
! 5193: ** as heap memory usages approaches the limit.
! 5194: ** ^The soft heap limit is "soft" because even though SQLite strives to stay
! 5195: ** below the limit, it will exceed the limit rather than generate
! 5196: ** an [SQLITE_NOMEM] error. In other words, the soft heap limit
! 5197: ** is advisory only.
! 5198: **
! 5199: ** ^The return value from sqlite3_soft_heap_limit64() is the size of
! 5200: ** the soft heap limit prior to the call, or negative in the case of an
! 5201: ** error. ^If the argument N is negative
! 5202: ** then no change is made to the soft heap limit. Hence, the current
! 5203: ** size of the soft heap limit can be determined by invoking
! 5204: ** sqlite3_soft_heap_limit64() with a negative argument.
! 5205: **
! 5206: ** ^If the argument N is zero then the soft heap limit is disabled.
! 5207: **
! 5208: ** ^(The soft heap limit is not enforced in the current implementation
! 5209: ** if one or more of following conditions are true:
! 5210: **
! 5211: ** <ul>
! 5212: ** <li> The soft heap limit is set to zero.
! 5213: ** <li> Memory accounting is disabled using a combination of the
! 5214: ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
! 5215: ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
! 5216: ** <li> An alternative page cache implementation is specified using
! 5217: ** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
! 5218: ** <li> The page cache allocates from its own memory pool supplied
! 5219: ** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
! 5220: ** from the heap.
! 5221: ** </ul>)^
! 5222: **
! 5223: ** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
! 5224: ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
! 5225: ** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
! 5226: ** the soft heap limit is enforced on every memory allocation. Without
! 5227: ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
! 5228: ** when memory is allocated by the page cache. Testing suggests that because
! 5229: ** the page cache is the predominate memory user in SQLite, most
! 5230: ** applications will achieve adequate soft heap limit enforcement without
! 5231: ** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
! 5232: **
! 5233: ** The circumstances under which SQLite will enforce the soft heap limit may
! 5234: ** changes in future releases of SQLite.
! 5235: */
! 5236: SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
! 5237:
! 5238: /*
! 5239: ** CAPI3REF: Deprecated Soft Heap Limit Interface
! 5240: ** DEPRECATED
! 5241: **
! 5242: ** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
! 5243: ** interface. This routine is provided for historical compatibility
! 5244: ** only. All new applications should use the
! 5245: ** [sqlite3_soft_heap_limit64()] interface rather than this one.
! 5246: */
! 5247: SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
! 5248:
! 5249:
! 5250: /*
! 5251: ** CAPI3REF: Extract Metadata About A Column Of A Table
! 5252: **
! 5253: ** ^This routine returns metadata about a specific column of a specific
! 5254: ** database table accessible using the [database connection] handle
! 5255: ** passed as the first function argument.
! 5256: **
! 5257: ** ^The column is identified by the second, third and fourth parameters to
! 5258: ** this function. ^The second parameter is either the name of the database
! 5259: ** (i.e. "main", "temp", or an attached database) containing the specified
! 5260: ** table or NULL. ^If it is NULL, then all attached databases are searched
! 5261: ** for the table using the same algorithm used by the database engine to
! 5262: ** resolve unqualified table references.
! 5263: **
! 5264: ** ^The third and fourth parameters to this function are the table and column
! 5265: ** name of the desired column, respectively. Neither of these parameters
! 5266: ** may be NULL.
! 5267: **
! 5268: ** ^Metadata is returned by writing to the memory locations passed as the 5th
! 5269: ** and subsequent parameters to this function. ^Any of these arguments may be
! 5270: ** NULL, in which case the corresponding element of metadata is omitted.
! 5271: **
! 5272: ** ^(<blockquote>
! 5273: ** <table border="1">
! 5274: ** <tr><th> Parameter <th> Output<br>Type <th> Description
! 5275: **
! 5276: ** <tr><td> 5th <td> const char* <td> Data type
! 5277: ** <tr><td> 6th <td> const char* <td> Name of default collation sequence
! 5278: ** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
! 5279: ** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
! 5280: ** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
! 5281: ** </table>
! 5282: ** </blockquote>)^
! 5283: **
! 5284: ** ^The memory pointed to by the character pointers returned for the
! 5285: ** declaration type and collation sequence is valid only until the next
! 5286: ** call to any SQLite API function.
! 5287: **
! 5288: ** ^If the specified table is actually a view, an [error code] is returned.
! 5289: **
! 5290: ** ^If the specified column is "rowid", "oid" or "_rowid_" and an
! 5291: ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
! 5292: ** parameters are set for the explicitly declared column. ^(If there is no
! 5293: ** explicitly declared [INTEGER PRIMARY KEY] column, then the output
! 5294: ** parameters are set as follows:
! 5295: **
! 5296: ** <pre>
! 5297: ** data type: "INTEGER"
! 5298: ** collation sequence: "BINARY"
! 5299: ** not null: 0
! 5300: ** primary key: 1
! 5301: ** auto increment: 0
! 5302: ** </pre>)^
! 5303: **
! 5304: ** ^(This function may load one or more schemas from database files. If an
! 5305: ** error occurs during this process, or if the requested table or column
! 5306: ** cannot be found, an [error code] is returned and an error message left
! 5307: ** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
! 5308: **
! 5309: ** ^This API is only available if the library was compiled with the
! 5310: ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
! 5311: */
! 5312: SQLITE_API int sqlite3_table_column_metadata(
! 5313: sqlite3 *db, /* Connection handle */
! 5314: const char *zDbName, /* Database name or NULL */
! 5315: const char *zTableName, /* Table name */
! 5316: const char *zColumnName, /* Column name */
! 5317: char const **pzDataType, /* OUTPUT: Declared data type */
! 5318: char const **pzCollSeq, /* OUTPUT: Collation sequence name */
! 5319: int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
! 5320: int *pPrimaryKey, /* OUTPUT: True if column part of PK */
! 5321: int *pAutoinc /* OUTPUT: True if column is auto-increment */
! 5322: );
! 5323:
! 5324: /*
! 5325: ** CAPI3REF: Load An Extension
! 5326: **
! 5327: ** ^This interface loads an SQLite extension library from the named file.
! 5328: **
! 5329: ** ^The sqlite3_load_extension() interface attempts to load an
! 5330: ** SQLite extension library contained in the file zFile.
! 5331: **
! 5332: ** ^The entry point is zProc.
! 5333: ** ^zProc may be 0, in which case the name of the entry point
! 5334: ** defaults to "sqlite3_extension_init".
! 5335: ** ^The sqlite3_load_extension() interface returns
! 5336: ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
! 5337: ** ^If an error occurs and pzErrMsg is not 0, then the
! 5338: ** [sqlite3_load_extension()] interface shall attempt to
! 5339: ** fill *pzErrMsg with error message text stored in memory
! 5340: ** obtained from [sqlite3_malloc()]. The calling function
! 5341: ** should free this memory by calling [sqlite3_free()].
! 5342: **
! 5343: ** ^Extension loading must be enabled using
! 5344: ** [sqlite3_enable_load_extension()] prior to calling this API,
! 5345: ** otherwise an error will be returned.
! 5346: **
! 5347: ** See also the [load_extension() SQL function].
! 5348: */
! 5349: SQLITE_API int sqlite3_load_extension(
! 5350: sqlite3 *db, /* Load the extension into this database connection */
! 5351: const char *zFile, /* Name of the shared library containing extension */
! 5352: const char *zProc, /* Entry point. Derived from zFile if 0 */
! 5353: char **pzErrMsg /* Put error message here if not 0 */
! 5354: );
! 5355:
! 5356: /*
! 5357: ** CAPI3REF: Enable Or Disable Extension Loading
! 5358: **
! 5359: ** ^So as not to open security holes in older applications that are
! 5360: ** unprepared to deal with extension loading, and as a means of disabling
! 5361: ** extension loading while evaluating user-entered SQL, the following API
! 5362: ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
! 5363: **
! 5364: ** ^Extension loading is off by default. See ticket #1863.
! 5365: ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
! 5366: ** to turn extension loading on and call it with onoff==0 to turn
! 5367: ** it back off again.
! 5368: */
! 5369: SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
! 5370:
! 5371: /*
! 5372: ** CAPI3REF: Automatically Load Statically Linked Extensions
! 5373: **
! 5374: ** ^This interface causes the xEntryPoint() function to be invoked for
! 5375: ** each new [database connection] that is created. The idea here is that
! 5376: ** xEntryPoint() is the entry point for a statically linked SQLite extension
! 5377: ** that is to be automatically loaded into all new database connections.
! 5378: **
! 5379: ** ^(Even though the function prototype shows that xEntryPoint() takes
! 5380: ** no arguments and returns void, SQLite invokes xEntryPoint() with three
! 5381: ** arguments and expects and integer result as if the signature of the
! 5382: ** entry point where as follows:
! 5383: **
! 5384: ** <blockquote><pre>
! 5385: ** int xEntryPoint(
! 5386: ** sqlite3 *db,
! 5387: ** const char **pzErrMsg,
! 5388: ** const struct sqlite3_api_routines *pThunk
! 5389: ** );
! 5390: ** </pre></blockquote>)^
! 5391: **
! 5392: ** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
! 5393: ** point to an appropriate error message (obtained from [sqlite3_mprintf()])
! 5394: ** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg
! 5395: ** is NULL before calling the xEntryPoint(). ^SQLite will invoke
! 5396: ** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any
! 5397: ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
! 5398: ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
! 5399: **
! 5400: ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
! 5401: ** on the list of automatic extensions is a harmless no-op. ^No entry point
! 5402: ** will be called more than once for each database connection that is opened.
! 5403: **
! 5404: ** See also: [sqlite3_reset_auto_extension()].
! 5405: */
! 5406: SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
! 5407:
! 5408: /*
! 5409: ** CAPI3REF: Reset Automatic Extension Loading
! 5410: **
! 5411: ** ^This interface disables all automatic extensions previously
! 5412: ** registered using [sqlite3_auto_extension()].
! 5413: */
! 5414: SQLITE_API void sqlite3_reset_auto_extension(void);
! 5415:
! 5416: /*
! 5417: ** The interface to the virtual-table mechanism is currently considered
! 5418: ** to be experimental. The interface might change in incompatible ways.
! 5419: ** If this is a problem for you, do not use the interface at this time.
! 5420: **
! 5421: ** When the virtual-table mechanism stabilizes, we will declare the
! 5422: ** interface fixed, support it indefinitely, and remove this comment.
! 5423: */
! 5424:
! 5425: /*
! 5426: ** Structures used by the virtual table interface
! 5427: */
! 5428: typedef struct sqlite3_vtab sqlite3_vtab;
! 5429: typedef struct sqlite3_index_info sqlite3_index_info;
! 5430: typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
! 5431: typedef struct sqlite3_module sqlite3_module;
! 5432:
! 5433: /*
! 5434: ** CAPI3REF: Virtual Table Object
! 5435: ** KEYWORDS: sqlite3_module {virtual table module}
! 5436: **
! 5437: ** This structure, sometimes called a "virtual table module",
! 5438: ** defines the implementation of a [virtual tables].
! 5439: ** This structure consists mostly of methods for the module.
! 5440: **
! 5441: ** ^A virtual table module is created by filling in a persistent
! 5442: ** instance of this structure and passing a pointer to that instance
! 5443: ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
! 5444: ** ^The registration remains valid until it is replaced by a different
! 5445: ** module or until the [database connection] closes. The content
! 5446: ** of this structure must not change while it is registered with
! 5447: ** any database connection.
! 5448: */
! 5449: struct sqlite3_module {
! 5450: int iVersion;
! 5451: int (*xCreate)(sqlite3*, void *pAux,
! 5452: int argc, const char *const*argv,
! 5453: sqlite3_vtab **ppVTab, char**);
! 5454: int (*xConnect)(sqlite3*, void *pAux,
! 5455: int argc, const char *const*argv,
! 5456: sqlite3_vtab **ppVTab, char**);
! 5457: int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
! 5458: int (*xDisconnect)(sqlite3_vtab *pVTab);
! 5459: int (*xDestroy)(sqlite3_vtab *pVTab);
! 5460: int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
! 5461: int (*xClose)(sqlite3_vtab_cursor*);
! 5462: int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
! 5463: int argc, sqlite3_value **argv);
! 5464: int (*xNext)(sqlite3_vtab_cursor*);
! 5465: int (*xEof)(sqlite3_vtab_cursor*);
! 5466: int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
! 5467: int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
! 5468: int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
! 5469: int (*xBegin)(sqlite3_vtab *pVTab);
! 5470: int (*xSync)(sqlite3_vtab *pVTab);
! 5471: int (*xCommit)(sqlite3_vtab *pVTab);
! 5472: int (*xRollback)(sqlite3_vtab *pVTab);
! 5473: int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
! 5474: void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
! 5475: void **ppArg);
! 5476: int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
! 5477: /* The methods above are in version 1 of the sqlite_module object. Those
! 5478: ** below are for version 2 and greater. */
! 5479: int (*xSavepoint)(sqlite3_vtab *pVTab, int);
! 5480: int (*xRelease)(sqlite3_vtab *pVTab, int);
! 5481: int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
! 5482: };
! 5483:
! 5484: /*
! 5485: ** CAPI3REF: Virtual Table Indexing Information
! 5486: ** KEYWORDS: sqlite3_index_info
! 5487: **
! 5488: ** The sqlite3_index_info structure and its substructures is used as part
! 5489: ** of the [virtual table] interface to
! 5490: ** pass information into and receive the reply from the [xBestIndex]
! 5491: ** method of a [virtual table module]. The fields under **Inputs** are the
! 5492: ** inputs to xBestIndex and are read-only. xBestIndex inserts its
! 5493: ** results into the **Outputs** fields.
! 5494: **
! 5495: ** ^(The aConstraint[] array records WHERE clause constraints of the form:
! 5496: **
! 5497: ** <blockquote>column OP expr</blockquote>
! 5498: **
! 5499: ** where OP is =, <, <=, >, or >=.)^ ^(The particular operator is
! 5500: ** stored in aConstraint[].op using one of the
! 5501: ** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
! 5502: ** ^(The index of the column is stored in
! 5503: ** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the
! 5504: ** expr on the right-hand side can be evaluated (and thus the constraint
! 5505: ** is usable) and false if it cannot.)^
! 5506: **
! 5507: ** ^The optimizer automatically inverts terms of the form "expr OP column"
! 5508: ** and makes other simplifications to the WHERE clause in an attempt to
! 5509: ** get as many WHERE clause terms into the form shown above as possible.
! 5510: ** ^The aConstraint[] array only reports WHERE clause terms that are
! 5511: ** relevant to the particular virtual table being queried.
! 5512: **
! 5513: ** ^Information about the ORDER BY clause is stored in aOrderBy[].
! 5514: ** ^Each term of aOrderBy records a column of the ORDER BY clause.
! 5515: **
! 5516: ** The [xBestIndex] method must fill aConstraintUsage[] with information
! 5517: ** about what parameters to pass to xFilter. ^If argvIndex>0 then
! 5518: ** the right-hand side of the corresponding aConstraint[] is evaluated
! 5519: ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
! 5520: ** is true, then the constraint is assumed to be fully handled by the
! 5521: ** virtual table and is not checked again by SQLite.)^
! 5522: **
! 5523: ** ^The idxNum and idxPtr values are recorded and passed into the
! 5524: ** [xFilter] method.
! 5525: ** ^[sqlite3_free()] is used to free idxPtr if and only if
! 5526: ** needToFreeIdxPtr is true.
! 5527: **
! 5528: ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
! 5529: ** the correct order to satisfy the ORDER BY clause so that no separate
! 5530: ** sorting step is required.
! 5531: **
! 5532: ** ^The estimatedCost value is an estimate of the cost of doing the
! 5533: ** particular lookup. A full scan of a table with N entries should have
! 5534: ** a cost of N. A binary search of a table of N entries should have a
! 5535: ** cost of approximately log(N).
! 5536: */
! 5537: struct sqlite3_index_info {
! 5538: /* Inputs */
! 5539: int nConstraint; /* Number of entries in aConstraint */
! 5540: struct sqlite3_index_constraint {
! 5541: int iColumn; /* Column on left-hand side of constraint */
! 5542: unsigned char op; /* Constraint operator */
! 5543: unsigned char usable; /* True if this constraint is usable */
! 5544: int iTermOffset; /* Used internally - xBestIndex should ignore */
! 5545: } *aConstraint; /* Table of WHERE clause constraints */
! 5546: int nOrderBy; /* Number of terms in the ORDER BY clause */
! 5547: struct sqlite3_index_orderby {
! 5548: int iColumn; /* Column number */
! 5549: unsigned char desc; /* True for DESC. False for ASC. */
! 5550: } *aOrderBy; /* The ORDER BY clause */
! 5551: /* Outputs */
! 5552: struct sqlite3_index_constraint_usage {
! 5553: int argvIndex; /* if >0, constraint is part of argv to xFilter */
! 5554: unsigned char omit; /* Do not code a test for this constraint */
! 5555: } *aConstraintUsage;
! 5556: int idxNum; /* Number used to identify the index */
! 5557: char *idxStr; /* String, possibly obtained from sqlite3_malloc */
! 5558: int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
! 5559: int orderByConsumed; /* True if output is already ordered */
! 5560: double estimatedCost; /* Estimated cost of using this index */
! 5561: };
! 5562:
! 5563: /*
! 5564: ** CAPI3REF: Virtual Table Constraint Operator Codes
! 5565: **
! 5566: ** These macros defined the allowed values for the
! 5567: ** [sqlite3_index_info].aConstraint[].op field. Each value represents
! 5568: ** an operator that is part of a constraint term in the wHERE clause of
! 5569: ** a query that uses a [virtual table].
! 5570: */
! 5571: #define SQLITE_INDEX_CONSTRAINT_EQ 2
! 5572: #define SQLITE_INDEX_CONSTRAINT_GT 4
! 5573: #define SQLITE_INDEX_CONSTRAINT_LE 8
! 5574: #define SQLITE_INDEX_CONSTRAINT_LT 16
! 5575: #define SQLITE_INDEX_CONSTRAINT_GE 32
! 5576: #define SQLITE_INDEX_CONSTRAINT_MATCH 64
! 5577:
! 5578: /*
! 5579: ** CAPI3REF: Register A Virtual Table Implementation
! 5580: **
! 5581: ** ^These routines are used to register a new [virtual table module] name.
! 5582: ** ^Module names must be registered before
! 5583: ** creating a new [virtual table] using the module and before using a
! 5584: ** preexisting [virtual table] for the module.
! 5585: **
! 5586: ** ^The module name is registered on the [database connection] specified
! 5587: ** by the first parameter. ^The name of the module is given by the
! 5588: ** second parameter. ^The third parameter is a pointer to
! 5589: ** the implementation of the [virtual table module]. ^The fourth
! 5590: ** parameter is an arbitrary client data pointer that is passed through
! 5591: ** into the [xCreate] and [xConnect] methods of the virtual table module
! 5592: ** when a new virtual table is be being created or reinitialized.
! 5593: **
! 5594: ** ^The sqlite3_create_module_v2() interface has a fifth parameter which
! 5595: ** is a pointer to a destructor for the pClientData. ^SQLite will
! 5596: ** invoke the destructor function (if it is not NULL) when SQLite
! 5597: ** no longer needs the pClientData pointer. ^The destructor will also
! 5598: ** be invoked if the call to sqlite3_create_module_v2() fails.
! 5599: ** ^The sqlite3_create_module()
! 5600: ** interface is equivalent to sqlite3_create_module_v2() with a NULL
! 5601: ** destructor.
! 5602: */
! 5603: SQLITE_API int sqlite3_create_module(
! 5604: sqlite3 *db, /* SQLite connection to register module with */
! 5605: const char *zName, /* Name of the module */
! 5606: const sqlite3_module *p, /* Methods for the module */
! 5607: void *pClientData /* Client data for xCreate/xConnect */
! 5608: );
! 5609: SQLITE_API int sqlite3_create_module_v2(
! 5610: sqlite3 *db, /* SQLite connection to register module with */
! 5611: const char *zName, /* Name of the module */
! 5612: const sqlite3_module *p, /* Methods for the module */
! 5613: void *pClientData, /* Client data for xCreate/xConnect */
! 5614: void(*xDestroy)(void*) /* Module destructor function */
! 5615: );
! 5616:
! 5617: /*
! 5618: ** CAPI3REF: Virtual Table Instance Object
! 5619: ** KEYWORDS: sqlite3_vtab
! 5620: **
! 5621: ** Every [virtual table module] implementation uses a subclass
! 5622: ** of this object to describe a particular instance
! 5623: ** of the [virtual table]. Each subclass will
! 5624: ** be tailored to the specific needs of the module implementation.
! 5625: ** The purpose of this superclass is to define certain fields that are
! 5626: ** common to all module implementations.
! 5627: **
! 5628: ** ^Virtual tables methods can set an error message by assigning a
! 5629: ** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should
! 5630: ** take care that any prior string is freed by a call to [sqlite3_free()]
! 5631: ** prior to assigning a new string to zErrMsg. ^After the error message
! 5632: ** is delivered up to the client application, the string will be automatically
! 5633: ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
! 5634: */
! 5635: struct sqlite3_vtab {
! 5636: const sqlite3_module *pModule; /* The module for this virtual table */
! 5637: int nRef; /* NO LONGER USED */
! 5638: char *zErrMsg; /* Error message from sqlite3_mprintf() */
! 5639: /* Virtual table implementations will typically add additional fields */
! 5640: };
! 5641:
! 5642: /*
! 5643: ** CAPI3REF: Virtual Table Cursor Object
! 5644: ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
! 5645: **
! 5646: ** Every [virtual table module] implementation uses a subclass of the
! 5647: ** following structure to describe cursors that point into the
! 5648: ** [virtual table] and are used
! 5649: ** to loop through the virtual table. Cursors are created using the
! 5650: ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
! 5651: ** by the [sqlite3_module.xClose | xClose] method. Cursors are used
! 5652: ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
! 5653: ** of the module. Each module implementation will define
! 5654: ** the content of a cursor structure to suit its own needs.
! 5655: **
! 5656: ** This superclass exists in order to define fields of the cursor that
! 5657: ** are common to all implementations.
! 5658: */
! 5659: struct sqlite3_vtab_cursor {
! 5660: sqlite3_vtab *pVtab; /* Virtual table of this cursor */
! 5661: /* Virtual table implementations will typically add additional fields */
! 5662: };
! 5663:
! 5664: /*
! 5665: ** CAPI3REF: Declare The Schema Of A Virtual Table
! 5666: **
! 5667: ** ^The [xCreate] and [xConnect] methods of a
! 5668: ** [virtual table module] call this interface
! 5669: ** to declare the format (the names and datatypes of the columns) of
! 5670: ** the virtual tables they implement.
! 5671: */
! 5672: SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
! 5673:
! 5674: /*
! 5675: ** CAPI3REF: Overload A Function For A Virtual Table
! 5676: **
! 5677: ** ^(Virtual tables can provide alternative implementations of functions
! 5678: ** using the [xFindFunction] method of the [virtual table module].
! 5679: ** But global versions of those functions
! 5680: ** must exist in order to be overloaded.)^
! 5681: **
! 5682: ** ^(This API makes sure a global version of a function with a particular
! 5683: ** name and number of parameters exists. If no such function exists
! 5684: ** before this API is called, a new function is created.)^ ^The implementation
! 5685: ** of the new function always causes an exception to be thrown. So
! 5686: ** the new function is not good for anything by itself. Its only
! 5687: ** purpose is to be a placeholder function that can be overloaded
! 5688: ** by a [virtual table].
! 5689: */
! 5690: SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
! 5691:
! 5692: /*
! 5693: ** The interface to the virtual-table mechanism defined above (back up
! 5694: ** to a comment remarkably similar to this one) is currently considered
! 5695: ** to be experimental. The interface might change in incompatible ways.
! 5696: ** If this is a problem for you, do not use the interface at this time.
! 5697: **
! 5698: ** When the virtual-table mechanism stabilizes, we will declare the
! 5699: ** interface fixed, support it indefinitely, and remove this comment.
! 5700: */
! 5701:
! 5702: /*
! 5703: ** CAPI3REF: A Handle To An Open BLOB
! 5704: ** KEYWORDS: {BLOB handle} {BLOB handles}
! 5705: **
! 5706: ** An instance of this object represents an open BLOB on which
! 5707: ** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
! 5708: ** ^Objects of this type are created by [sqlite3_blob_open()]
! 5709: ** and destroyed by [sqlite3_blob_close()].
! 5710: ** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
! 5711: ** can be used to read or write small subsections of the BLOB.
! 5712: ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
! 5713: */
! 5714: typedef struct sqlite3_blob sqlite3_blob;
! 5715:
! 5716: /*
! 5717: ** CAPI3REF: Open A BLOB For Incremental I/O
! 5718: **
! 5719: ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
! 5720: ** in row iRow, column zColumn, table zTable in database zDb;
! 5721: ** in other words, the same BLOB that would be selected by:
! 5722: **
! 5723: ** <pre>
! 5724: ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
! 5725: ** </pre>)^
! 5726: **
! 5727: ** ^If the flags parameter is non-zero, then the BLOB is opened for read
! 5728: ** and write access. ^If it is zero, the BLOB is opened for read access.
! 5729: ** ^It is not possible to open a column that is part of an index or primary
! 5730: ** key for writing. ^If [foreign key constraints] are enabled, it is
! 5731: ** not possible to open a column that is part of a [child key] for writing.
! 5732: **
! 5733: ** ^Note that the database name is not the filename that contains
! 5734: ** the database but rather the symbolic name of the database that
! 5735: ** appears after the AS keyword when the database is connected using [ATTACH].
! 5736: ** ^For the main database file, the database name is "main".
! 5737: ** ^For TEMP tables, the database name is "temp".
! 5738: **
! 5739: ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
! 5740: ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
! 5741: ** to be a null pointer.)^
! 5742: ** ^This function sets the [database connection] error code and message
! 5743: ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
! 5744: ** functions. ^Note that the *ppBlob variable is always initialized in a
! 5745: ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
! 5746: ** regardless of the success or failure of this routine.
! 5747: **
! 5748: ** ^(If the row that a BLOB handle points to is modified by an
! 5749: ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
! 5750: ** then the BLOB handle is marked as "expired".
! 5751: ** This is true if any column of the row is changed, even a column
! 5752: ** other than the one the BLOB handle is open on.)^
! 5753: ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
! 5754: ** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
! 5755: ** ^(Changes written into a BLOB prior to the BLOB expiring are not
! 5756: ** rolled back by the expiration of the BLOB. Such changes will eventually
! 5757: ** commit if the transaction continues to completion.)^
! 5758: **
! 5759: ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
! 5760: ** the opened blob. ^The size of a blob may not be changed by this
! 5761: ** interface. Use the [UPDATE] SQL command to change the size of a
! 5762: ** blob.
! 5763: **
! 5764: ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
! 5765: ** and the built-in [zeroblob] SQL function can be used, if desired,
! 5766: ** to create an empty, zero-filled blob in which to read or write using
! 5767: ** this interface.
! 5768: **
! 5769: ** To avoid a resource leak, every open [BLOB handle] should eventually
! 5770: ** be released by a call to [sqlite3_blob_close()].
! 5771: */
! 5772: SQLITE_API int sqlite3_blob_open(
! 5773: sqlite3*,
! 5774: const char *zDb,
! 5775: const char *zTable,
! 5776: const char *zColumn,
! 5777: sqlite3_int64 iRow,
! 5778: int flags,
! 5779: sqlite3_blob **ppBlob
! 5780: );
! 5781:
! 5782: /*
! 5783: ** CAPI3REF: Move a BLOB Handle to a New Row
! 5784: **
! 5785: ** ^This function is used to move an existing blob handle so that it points
! 5786: ** to a different row of the same database table. ^The new row is identified
! 5787: ** by the rowid value passed as the second argument. Only the row can be
! 5788: ** changed. ^The database, table and column on which the blob handle is open
! 5789: ** remain the same. Moving an existing blob handle to a new row can be
! 5790: ** faster than closing the existing handle and opening a new one.
! 5791: **
! 5792: ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
! 5793: ** it must exist and there must be either a blob or text value stored in
! 5794: ** the nominated column.)^ ^If the new row is not present in the table, or if
! 5795: ** it does not contain a blob or text value, or if another error occurs, an
! 5796: ** SQLite error code is returned and the blob handle is considered aborted.
! 5797: ** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
! 5798: ** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
! 5799: ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
! 5800: ** always returns zero.
! 5801: **
! 5802: ** ^This function sets the database handle error code and message.
! 5803: */
! 5804: SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
! 5805:
! 5806: /*
! 5807: ** CAPI3REF: Close A BLOB Handle
! 5808: **
! 5809: ** ^Closes an open [BLOB handle].
! 5810: **
! 5811: ** ^Closing a BLOB shall cause the current transaction to commit
! 5812: ** if there are no other BLOBs, no pending prepared statements, and the
! 5813: ** database connection is in [autocommit mode].
! 5814: ** ^If any writes were made to the BLOB, they might be held in cache
! 5815: ** until the close operation if they will fit.
! 5816: **
! 5817: ** ^(Closing the BLOB often forces the changes
! 5818: ** out to disk and so if any I/O errors occur, they will likely occur
! 5819: ** at the time when the BLOB is closed. Any errors that occur during
! 5820: ** closing are reported as a non-zero return value.)^
! 5821: **
! 5822: ** ^(The BLOB is closed unconditionally. Even if this routine returns
! 5823: ** an error code, the BLOB is still closed.)^
! 5824: **
! 5825: ** ^Calling this routine with a null pointer (such as would be returned
! 5826: ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
! 5827: */
! 5828: SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
! 5829:
! 5830: /*
! 5831: ** CAPI3REF: Return The Size Of An Open BLOB
! 5832: **
! 5833: ** ^Returns the size in bytes of the BLOB accessible via the
! 5834: ** successfully opened [BLOB handle] in its only argument. ^The
! 5835: ** incremental blob I/O routines can only read or overwriting existing
! 5836: ** blob content; they cannot change the size of a blob.
! 5837: **
! 5838: ** This routine only works on a [BLOB handle] which has been created
! 5839: ** by a prior successful call to [sqlite3_blob_open()] and which has not
! 5840: ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
! 5841: ** to this routine results in undefined and probably undesirable behavior.
! 5842: */
! 5843: SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
! 5844:
! 5845: /*
! 5846: ** CAPI3REF: Read Data From A BLOB Incrementally
! 5847: **
! 5848: ** ^(This function is used to read data from an open [BLOB handle] into a
! 5849: ** caller-supplied buffer. N bytes of data are copied into buffer Z
! 5850: ** from the open BLOB, starting at offset iOffset.)^
! 5851: **
! 5852: ** ^If offset iOffset is less than N bytes from the end of the BLOB,
! 5853: ** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is
! 5854: ** less than zero, [SQLITE_ERROR] is returned and no data is read.
! 5855: ** ^The size of the blob (and hence the maximum value of N+iOffset)
! 5856: ** can be determined using the [sqlite3_blob_bytes()] interface.
! 5857: **
! 5858: ** ^An attempt to read from an expired [BLOB handle] fails with an
! 5859: ** error code of [SQLITE_ABORT].
! 5860: **
! 5861: ** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
! 5862: ** Otherwise, an [error code] or an [extended error code] is returned.)^
! 5863: **
! 5864: ** This routine only works on a [BLOB handle] which has been created
! 5865: ** by a prior successful call to [sqlite3_blob_open()] and which has not
! 5866: ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
! 5867: ** to this routine results in undefined and probably undesirable behavior.
! 5868: **
! 5869: ** See also: [sqlite3_blob_write()].
! 5870: */
! 5871: SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
! 5872:
! 5873: /*
! 5874: ** CAPI3REF: Write Data Into A BLOB Incrementally
! 5875: **
! 5876: ** ^This function is used to write data into an open [BLOB handle] from a
! 5877: ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
! 5878: ** into the open BLOB, starting at offset iOffset.
! 5879: **
! 5880: ** ^If the [BLOB handle] passed as the first argument was not opened for
! 5881: ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
! 5882: ** this function returns [SQLITE_READONLY].
! 5883: **
! 5884: ** ^This function may only modify the contents of the BLOB; it is
! 5885: ** not possible to increase the size of a BLOB using this API.
! 5886: ** ^If offset iOffset is less than N bytes from the end of the BLOB,
! 5887: ** [SQLITE_ERROR] is returned and no data is written. ^If N is
! 5888: ** less than zero [SQLITE_ERROR] is returned and no data is written.
! 5889: ** The size of the BLOB (and hence the maximum value of N+iOffset)
! 5890: ** can be determined using the [sqlite3_blob_bytes()] interface.
! 5891: **
! 5892: ** ^An attempt to write to an expired [BLOB handle] fails with an
! 5893: ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
! 5894: ** before the [BLOB handle] expired are not rolled back by the
! 5895: ** expiration of the handle, though of course those changes might
! 5896: ** have been overwritten by the statement that expired the BLOB handle
! 5897: ** or by other independent statements.
! 5898: **
! 5899: ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
! 5900: ** Otherwise, an [error code] or an [extended error code] is returned.)^
! 5901: **
! 5902: ** This routine only works on a [BLOB handle] which has been created
! 5903: ** by a prior successful call to [sqlite3_blob_open()] and which has not
! 5904: ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
! 5905: ** to this routine results in undefined and probably undesirable behavior.
! 5906: **
! 5907: ** See also: [sqlite3_blob_read()].
! 5908: */
! 5909: SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
! 5910:
! 5911: /*
! 5912: ** CAPI3REF: Virtual File System Objects
! 5913: **
! 5914: ** A virtual filesystem (VFS) is an [sqlite3_vfs] object
! 5915: ** that SQLite uses to interact
! 5916: ** with the underlying operating system. Most SQLite builds come with a
! 5917: ** single default VFS that is appropriate for the host computer.
! 5918: ** New VFSes can be registered and existing VFSes can be unregistered.
! 5919: ** The following interfaces are provided.
! 5920: **
! 5921: ** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
! 5922: ** ^Names are case sensitive.
! 5923: ** ^Names are zero-terminated UTF-8 strings.
! 5924: ** ^If there is no match, a NULL pointer is returned.
! 5925: ** ^If zVfsName is NULL then the default VFS is returned.
! 5926: **
! 5927: ** ^New VFSes are registered with sqlite3_vfs_register().
! 5928: ** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
! 5929: ** ^The same VFS can be registered multiple times without injury.
! 5930: ** ^To make an existing VFS into the default VFS, register it again
! 5931: ** with the makeDflt flag set. If two different VFSes with the
! 5932: ** same name are registered, the behavior is undefined. If a
! 5933: ** VFS is registered with a name that is NULL or an empty string,
! 5934: ** then the behavior is undefined.
! 5935: **
! 5936: ** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
! 5937: ** ^(If the default VFS is unregistered, another VFS is chosen as
! 5938: ** the default. The choice for the new VFS is arbitrary.)^
! 5939: */
! 5940: SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
! 5941: SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
! 5942: SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
! 5943:
! 5944: /*
! 5945: ** CAPI3REF: Mutexes
! 5946: **
! 5947: ** The SQLite core uses these routines for thread
! 5948: ** synchronization. Though they are intended for internal
! 5949: ** use by SQLite, code that links against SQLite is
! 5950: ** permitted to use any of these routines.
! 5951: **
! 5952: ** The SQLite source code contains multiple implementations
! 5953: ** of these mutex routines. An appropriate implementation
! 5954: ** is selected automatically at compile-time. ^(The following
! 5955: ** implementations are available in the SQLite core:
! 5956: **
! 5957: ** <ul>
! 5958: ** <li> SQLITE_MUTEX_OS2
! 5959: ** <li> SQLITE_MUTEX_PTHREADS
! 5960: ** <li> SQLITE_MUTEX_W32
! 5961: ** <li> SQLITE_MUTEX_NOOP
! 5962: ** </ul>)^
! 5963: **
! 5964: ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
! 5965: ** that does no real locking and is appropriate for use in
! 5966: ** a single-threaded application. ^The SQLITE_MUTEX_OS2,
! 5967: ** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
! 5968: ** are appropriate for use on OS/2, Unix, and Windows.
! 5969: **
! 5970: ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
! 5971: ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
! 5972: ** implementation is included with the library. In this case the
! 5973: ** application must supply a custom mutex implementation using the
! 5974: ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
! 5975: ** before calling sqlite3_initialize() or any other public sqlite3_
! 5976: ** function that calls sqlite3_initialize().)^
! 5977: **
! 5978: ** ^The sqlite3_mutex_alloc() routine allocates a new
! 5979: ** mutex and returns a pointer to it. ^If it returns NULL
! 5980: ** that means that a mutex could not be allocated. ^SQLite
! 5981: ** will unwind its stack and return an error. ^(The argument
! 5982: ** to sqlite3_mutex_alloc() is one of these integer constants:
! 5983: **
! 5984: ** <ul>
! 5985: ** <li> SQLITE_MUTEX_FAST
! 5986: ** <li> SQLITE_MUTEX_RECURSIVE
! 5987: ** <li> SQLITE_MUTEX_STATIC_MASTER
! 5988: ** <li> SQLITE_MUTEX_STATIC_MEM
! 5989: ** <li> SQLITE_MUTEX_STATIC_MEM2
! 5990: ** <li> SQLITE_MUTEX_STATIC_PRNG
! 5991: ** <li> SQLITE_MUTEX_STATIC_LRU
! 5992: ** <li> SQLITE_MUTEX_STATIC_LRU2
! 5993: ** </ul>)^
! 5994: **
! 5995: ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
! 5996: ** cause sqlite3_mutex_alloc() to create
! 5997: ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
! 5998: ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
! 5999: ** The mutex implementation does not need to make a distinction
! 6000: ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
! 6001: ** not want to. ^SQLite will only request a recursive mutex in
! 6002: ** cases where it really needs one. ^If a faster non-recursive mutex
! 6003: ** implementation is available on the host platform, the mutex subsystem
! 6004: ** might return such a mutex in response to SQLITE_MUTEX_FAST.
! 6005: **
! 6006: ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
! 6007: ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
! 6008: ** a pointer to a static preexisting mutex. ^Six static mutexes are
! 6009: ** used by the current version of SQLite. Future versions of SQLite
! 6010: ** may add additional static mutexes. Static mutexes are for internal
! 6011: ** use by SQLite only. Applications that use SQLite mutexes should
! 6012: ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
! 6013: ** SQLITE_MUTEX_RECURSIVE.
! 6014: **
! 6015: ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
! 6016: ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
! 6017: ** returns a different mutex on every call. ^But for the static
! 6018: ** mutex types, the same mutex is returned on every call that has
! 6019: ** the same type number.
! 6020: **
! 6021: ** ^The sqlite3_mutex_free() routine deallocates a previously
! 6022: ** allocated dynamic mutex. ^SQLite is careful to deallocate every
! 6023: ** dynamic mutex that it allocates. The dynamic mutexes must not be in
! 6024: ** use when they are deallocated. Attempting to deallocate a static
! 6025: ** mutex results in undefined behavior. ^SQLite never deallocates
! 6026: ** a static mutex.
! 6027: **
! 6028: ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
! 6029: ** to enter a mutex. ^If another thread is already within the mutex,
! 6030: ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
! 6031: ** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
! 6032: ** upon successful entry. ^(Mutexes created using
! 6033: ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
! 6034: ** In such cases the,
! 6035: ** mutex must be exited an equal number of times before another thread
! 6036: ** can enter.)^ ^(If the same thread tries to enter any other
! 6037: ** kind of mutex more than once, the behavior is undefined.
! 6038: ** SQLite will never exhibit
! 6039: ** such behavior in its own use of mutexes.)^
! 6040: **
! 6041: ** ^(Some systems (for example, Windows 95) do not support the operation
! 6042: ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
! 6043: ** will always return SQLITE_BUSY. The SQLite core only ever uses
! 6044: ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
! 6045: **
! 6046: ** ^The sqlite3_mutex_leave() routine exits a mutex that was
! 6047: ** previously entered by the same thread. ^(The behavior
! 6048: ** is undefined if the mutex is not currently entered by the
! 6049: ** calling thread or is not currently allocated. SQLite will
! 6050: ** never do either.)^
! 6051: **
! 6052: ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
! 6053: ** sqlite3_mutex_leave() is a NULL pointer, then all three routines
! 6054: ** behave as no-ops.
! 6055: **
! 6056: ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
! 6057: */
! 6058: SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
! 6059: SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
! 6060: SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
! 6061: SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
! 6062: SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
! 6063:
! 6064: /*
! 6065: ** CAPI3REF: Mutex Methods Object
! 6066: **
! 6067: ** An instance of this structure defines the low-level routines
! 6068: ** used to allocate and use mutexes.
! 6069: **
! 6070: ** Usually, the default mutex implementations provided by SQLite are
! 6071: ** sufficient, however the user has the option of substituting a custom
! 6072: ** implementation for specialized deployments or systems for which SQLite
! 6073: ** does not provide a suitable implementation. In this case, the user
! 6074: ** creates and populates an instance of this structure to pass
! 6075: ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
! 6076: ** Additionally, an instance of this structure can be used as an
! 6077: ** output variable when querying the system for the current mutex
! 6078: ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
! 6079: **
! 6080: ** ^The xMutexInit method defined by this structure is invoked as
! 6081: ** part of system initialization by the sqlite3_initialize() function.
! 6082: ** ^The xMutexInit routine is called by SQLite exactly once for each
! 6083: ** effective call to [sqlite3_initialize()].
! 6084: **
! 6085: ** ^The xMutexEnd method defined by this structure is invoked as
! 6086: ** part of system shutdown by the sqlite3_shutdown() function. The
! 6087: ** implementation of this method is expected to release all outstanding
! 6088: ** resources obtained by the mutex methods implementation, especially
! 6089: ** those obtained by the xMutexInit method. ^The xMutexEnd()
! 6090: ** interface is invoked exactly once for each call to [sqlite3_shutdown()].
! 6091: **
! 6092: ** ^(The remaining seven methods defined by this structure (xMutexAlloc,
! 6093: ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
! 6094: ** xMutexNotheld) implement the following interfaces (respectively):
! 6095: **
! 6096: ** <ul>
! 6097: ** <li> [sqlite3_mutex_alloc()] </li>
! 6098: ** <li> [sqlite3_mutex_free()] </li>
! 6099: ** <li> [sqlite3_mutex_enter()] </li>
! 6100: ** <li> [sqlite3_mutex_try()] </li>
! 6101: ** <li> [sqlite3_mutex_leave()] </li>
! 6102: ** <li> [sqlite3_mutex_held()] </li>
! 6103: ** <li> [sqlite3_mutex_notheld()] </li>
! 6104: ** </ul>)^
! 6105: **
! 6106: ** The only difference is that the public sqlite3_XXX functions enumerated
! 6107: ** above silently ignore any invocations that pass a NULL pointer instead
! 6108: ** of a valid mutex handle. The implementations of the methods defined
! 6109: ** by this structure are not required to handle this case, the results
! 6110: ** of passing a NULL pointer instead of a valid mutex handle are undefined
! 6111: ** (i.e. it is acceptable to provide an implementation that segfaults if
! 6112: ** it is passed a NULL pointer).
! 6113: **
! 6114: ** The xMutexInit() method must be threadsafe. ^It must be harmless to
! 6115: ** invoke xMutexInit() multiple times within the same process and without
! 6116: ** intervening calls to xMutexEnd(). Second and subsequent calls to
! 6117: ** xMutexInit() must be no-ops.
! 6118: **
! 6119: ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
! 6120: ** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory
! 6121: ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
! 6122: ** memory allocation for a fast or recursive mutex.
! 6123: **
! 6124: ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
! 6125: ** called, but only if the prior call to xMutexInit returned SQLITE_OK.
! 6126: ** If xMutexInit fails in any way, it is expected to clean up after itself
! 6127: ** prior to returning.
! 6128: */
! 6129: typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
! 6130: struct sqlite3_mutex_methods {
! 6131: int (*xMutexInit)(void);
! 6132: int (*xMutexEnd)(void);
! 6133: sqlite3_mutex *(*xMutexAlloc)(int);
! 6134: void (*xMutexFree)(sqlite3_mutex *);
! 6135: void (*xMutexEnter)(sqlite3_mutex *);
! 6136: int (*xMutexTry)(sqlite3_mutex *);
! 6137: void (*xMutexLeave)(sqlite3_mutex *);
! 6138: int (*xMutexHeld)(sqlite3_mutex *);
! 6139: int (*xMutexNotheld)(sqlite3_mutex *);
! 6140: };
! 6141:
! 6142: /*
! 6143: ** CAPI3REF: Mutex Verification Routines
! 6144: **
! 6145: ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
! 6146: ** are intended for use inside assert() statements. ^The SQLite core
! 6147: ** never uses these routines except inside an assert() and applications
! 6148: ** are advised to follow the lead of the core. ^The SQLite core only
! 6149: ** provides implementations for these routines when it is compiled
! 6150: ** with the SQLITE_DEBUG flag. ^External mutex implementations
! 6151: ** are only required to provide these routines if SQLITE_DEBUG is
! 6152: ** defined and if NDEBUG is not defined.
! 6153: **
! 6154: ** ^These routines should return true if the mutex in their argument
! 6155: ** is held or not held, respectively, by the calling thread.
! 6156: **
! 6157: ** ^The implementation is not required to provide versions of these
! 6158: ** routines that actually work. If the implementation does not provide working
! 6159: ** versions of these routines, it should at least provide stubs that always
! 6160: ** return true so that one does not get spurious assertion failures.
! 6161: **
! 6162: ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
! 6163: ** the routine should return 1. This seems counter-intuitive since
! 6164: ** clearly the mutex cannot be held if it does not exist. But
! 6165: ** the reason the mutex does not exist is because the build is not
! 6166: ** using mutexes. And we do not want the assert() containing the
! 6167: ** call to sqlite3_mutex_held() to fail, so a non-zero return is
! 6168: ** the appropriate thing to do. ^The sqlite3_mutex_notheld()
! 6169: ** interface should also return 1 when given a NULL pointer.
! 6170: */
! 6171: #ifndef NDEBUG
! 6172: SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
! 6173: SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
! 6174: #endif
! 6175:
! 6176: /*
! 6177: ** CAPI3REF: Mutex Types
! 6178: **
! 6179: ** The [sqlite3_mutex_alloc()] interface takes a single argument
! 6180: ** which is one of these integer constants.
! 6181: **
! 6182: ** The set of static mutexes may change from one SQLite release to the
! 6183: ** next. Applications that override the built-in mutex logic must be
! 6184: ** prepared to accommodate additional static mutexes.
! 6185: */
! 6186: #define SQLITE_MUTEX_FAST 0
! 6187: #define SQLITE_MUTEX_RECURSIVE 1
! 6188: #define SQLITE_MUTEX_STATIC_MASTER 2
! 6189: #define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
! 6190: #define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
! 6191: #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
! 6192: #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
! 6193: #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
! 6194: #define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
! 6195: #define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
! 6196:
! 6197: /*
! 6198: ** CAPI3REF: Retrieve the mutex for a database connection
! 6199: **
! 6200: ** ^This interface returns a pointer the [sqlite3_mutex] object that
! 6201: ** serializes access to the [database connection] given in the argument
! 6202: ** when the [threading mode] is Serialized.
! 6203: ** ^If the [threading mode] is Single-thread or Multi-thread then this
! 6204: ** routine returns a NULL pointer.
! 6205: */
! 6206: SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
! 6207:
! 6208: /*
! 6209: ** CAPI3REF: Low-Level Control Of Database Files
! 6210: **
! 6211: ** ^The [sqlite3_file_control()] interface makes a direct call to the
! 6212: ** xFileControl method for the [sqlite3_io_methods] object associated
! 6213: ** with a particular database identified by the second argument. ^The
! 6214: ** name of the database is "main" for the main database or "temp" for the
! 6215: ** TEMP database, or the name that appears after the AS keyword for
! 6216: ** databases that are added using the [ATTACH] SQL command.
! 6217: ** ^A NULL pointer can be used in place of "main" to refer to the
! 6218: ** main database file.
! 6219: ** ^The third and fourth parameters to this routine
! 6220: ** are passed directly through to the second and third parameters of
! 6221: ** the xFileControl method. ^The return value of the xFileControl
! 6222: ** method becomes the return value of this routine.
! 6223: **
! 6224: ** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
! 6225: ** a pointer to the underlying [sqlite3_file] object to be written into
! 6226: ** the space pointed to by the 4th parameter. ^The SQLITE_FCNTL_FILE_POINTER
! 6227: ** case is a short-circuit path which does not actually invoke the
! 6228: ** underlying sqlite3_io_methods.xFileControl method.
! 6229: **
! 6230: ** ^If the second parameter (zDbName) does not match the name of any
! 6231: ** open database file, then SQLITE_ERROR is returned. ^This error
! 6232: ** code is not remembered and will not be recalled by [sqlite3_errcode()]
! 6233: ** or [sqlite3_errmsg()]. The underlying xFileControl method might
! 6234: ** also return SQLITE_ERROR. There is no way to distinguish between
! 6235: ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
! 6236: ** xFileControl method.
! 6237: **
! 6238: ** See also: [SQLITE_FCNTL_LOCKSTATE]
! 6239: */
! 6240: SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
! 6241:
! 6242: /*
! 6243: ** CAPI3REF: Testing Interface
! 6244: **
! 6245: ** ^The sqlite3_test_control() interface is used to read out internal
! 6246: ** state of SQLite and to inject faults into SQLite for testing
! 6247: ** purposes. ^The first parameter is an operation code that determines
! 6248: ** the number, meaning, and operation of all subsequent parameters.
! 6249: **
! 6250: ** This interface is not for use by applications. It exists solely
! 6251: ** for verifying the correct operation of the SQLite library. Depending
! 6252: ** on how the SQLite library is compiled, this interface might not exist.
! 6253: **
! 6254: ** The details of the operation codes, their meanings, the parameters
! 6255: ** they take, and what they do are all subject to change without notice.
! 6256: ** Unlike most of the SQLite API, this function is not guaranteed to
! 6257: ** operate consistently from one release to the next.
! 6258: */
! 6259: SQLITE_API int sqlite3_test_control(int op, ...);
! 6260:
! 6261: /*
! 6262: ** CAPI3REF: Testing Interface Operation Codes
! 6263: **
! 6264: ** These constants are the valid operation code parameters used
! 6265: ** as the first argument to [sqlite3_test_control()].
! 6266: **
! 6267: ** These parameters and their meanings are subject to change
! 6268: ** without notice. These values are for testing purposes only.
! 6269: ** Applications should not use any of these parameters or the
! 6270: ** [sqlite3_test_control()] interface.
! 6271: */
! 6272: #define SQLITE_TESTCTRL_FIRST 5
! 6273: #define SQLITE_TESTCTRL_PRNG_SAVE 5
! 6274: #define SQLITE_TESTCTRL_PRNG_RESTORE 6
! 6275: #define SQLITE_TESTCTRL_PRNG_RESET 7
! 6276: #define SQLITE_TESTCTRL_BITVEC_TEST 8
! 6277: #define SQLITE_TESTCTRL_FAULT_INSTALL 9
! 6278: #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10
! 6279: #define SQLITE_TESTCTRL_PENDING_BYTE 11
! 6280: #define SQLITE_TESTCTRL_ASSERT 12
! 6281: #define SQLITE_TESTCTRL_ALWAYS 13
! 6282: #define SQLITE_TESTCTRL_RESERVE 14
! 6283: #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
! 6284: #define SQLITE_TESTCTRL_ISKEYWORD 16
! 6285: #define SQLITE_TESTCTRL_SCRATCHMALLOC 17
! 6286: #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
! 6287: #define SQLITE_TESTCTRL_EXPLAIN_STMT 19
! 6288: #define SQLITE_TESTCTRL_LAST 19
! 6289:
! 6290: /*
! 6291: ** CAPI3REF: SQLite Runtime Status
! 6292: **
! 6293: ** ^This interface is used to retrieve runtime status information
! 6294: ** about the performance of SQLite, and optionally to reset various
! 6295: ** highwater marks. ^The first argument is an integer code for
! 6296: ** the specific parameter to measure. ^(Recognized integer codes
! 6297: ** are of the form [status parameters | SQLITE_STATUS_...].)^
! 6298: ** ^The current value of the parameter is returned into *pCurrent.
! 6299: ** ^The highest recorded value is returned in *pHighwater. ^If the
! 6300: ** resetFlag is true, then the highest record value is reset after
! 6301: ** *pHighwater is written. ^(Some parameters do not record the highest
! 6302: ** value. For those parameters
! 6303: ** nothing is written into *pHighwater and the resetFlag is ignored.)^
! 6304: ** ^(Other parameters record only the highwater mark and not the current
! 6305: ** value. For these latter parameters nothing is written into *pCurrent.)^
! 6306: **
! 6307: ** ^The sqlite3_status() routine returns SQLITE_OK on success and a
! 6308: ** non-zero [error code] on failure.
! 6309: **
! 6310: ** This routine is threadsafe but is not atomic. This routine can be
! 6311: ** called while other threads are running the same or different SQLite
! 6312: ** interfaces. However the values returned in *pCurrent and
! 6313: ** *pHighwater reflect the status of SQLite at different points in time
! 6314: ** and it is possible that another thread might change the parameter
! 6315: ** in between the times when *pCurrent and *pHighwater are written.
! 6316: **
! 6317: ** See also: [sqlite3_db_status()]
! 6318: */
! 6319: SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
! 6320:
! 6321:
! 6322: /*
! 6323: ** CAPI3REF: Status Parameters
! 6324: ** KEYWORDS: {status parameters}
! 6325: **
! 6326: ** These integer constants designate various run-time status parameters
! 6327: ** that can be returned by [sqlite3_status()].
! 6328: **
! 6329: ** <dl>
! 6330: ** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
! 6331: ** <dd>This parameter is the current amount of memory checked out
! 6332: ** using [sqlite3_malloc()], either directly or indirectly. The
! 6333: ** figure includes calls made to [sqlite3_malloc()] by the application
! 6334: ** and internal memory usage by the SQLite library. Scratch memory
! 6335: ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
! 6336: ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
! 6337: ** this parameter. The amount returned is the sum of the allocation
! 6338: ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
! 6339: **
! 6340: ** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
! 6341: ** <dd>This parameter records the largest memory allocation request
! 6342: ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
! 6343: ** internal equivalents). Only the value returned in the
! 6344: ** *pHighwater parameter to [sqlite3_status()] is of interest.
! 6345: ** The value written into the *pCurrent parameter is undefined.</dd>)^
! 6346: **
! 6347: ** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
! 6348: ** <dd>This parameter records the number of separate memory allocations
! 6349: ** currently checked out.</dd>)^
! 6350: **
! 6351: ** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
! 6352: ** <dd>This parameter returns the number of pages used out of the
! 6353: ** [pagecache memory allocator] that was configured using
! 6354: ** [SQLITE_CONFIG_PAGECACHE]. The
! 6355: ** value returned is in pages, not in bytes.</dd>)^
! 6356: **
! 6357: ** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
! 6358: ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
! 6359: ** <dd>This parameter returns the number of bytes of page cache
! 6360: ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
! 6361: ** buffer and where forced to overflow to [sqlite3_malloc()]. The
! 6362: ** returned value includes allocations that overflowed because they
! 6363: ** where too large (they were larger than the "sz" parameter to
! 6364: ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
! 6365: ** no space was left in the page cache.</dd>)^
! 6366: **
! 6367: ** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
! 6368: ** <dd>This parameter records the largest memory allocation request
! 6369: ** handed to [pagecache memory allocator]. Only the value returned in the
! 6370: ** *pHighwater parameter to [sqlite3_status()] is of interest.
! 6371: ** The value written into the *pCurrent parameter is undefined.</dd>)^
! 6372: **
! 6373: ** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
! 6374: ** <dd>This parameter returns the number of allocations used out of the
! 6375: ** [scratch memory allocator] configured using
! 6376: ** [SQLITE_CONFIG_SCRATCH]. The value returned is in allocations, not
! 6377: ** in bytes. Since a single thread may only have one scratch allocation
! 6378: ** outstanding at time, this parameter also reports the number of threads
! 6379: ** using scratch memory at the same time.</dd>)^
! 6380: **
! 6381: ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
! 6382: ** <dd>This parameter returns the number of bytes of scratch memory
! 6383: ** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
! 6384: ** buffer and where forced to overflow to [sqlite3_malloc()]. The values
! 6385: ** returned include overflows because the requested allocation was too
! 6386: ** larger (that is, because the requested allocation was larger than the
! 6387: ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
! 6388: ** slots were available.
! 6389: ** </dd>)^
! 6390: **
! 6391: ** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
! 6392: ** <dd>This parameter records the largest memory allocation request
! 6393: ** handed to [scratch memory allocator]. Only the value returned in the
! 6394: ** *pHighwater parameter to [sqlite3_status()] is of interest.
! 6395: ** The value written into the *pCurrent parameter is undefined.</dd>)^
! 6396: **
! 6397: ** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
! 6398: ** <dd>This parameter records the deepest parser stack. It is only
! 6399: ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
! 6400: ** </dl>
! 6401: **
! 6402: ** New status parameters may be added from time to time.
! 6403: */
! 6404: #define SQLITE_STATUS_MEMORY_USED 0
! 6405: #define SQLITE_STATUS_PAGECACHE_USED 1
! 6406: #define SQLITE_STATUS_PAGECACHE_OVERFLOW 2
! 6407: #define SQLITE_STATUS_SCRATCH_USED 3
! 6408: #define SQLITE_STATUS_SCRATCH_OVERFLOW 4
! 6409: #define SQLITE_STATUS_MALLOC_SIZE 5
! 6410: #define SQLITE_STATUS_PARSER_STACK 6
! 6411: #define SQLITE_STATUS_PAGECACHE_SIZE 7
! 6412: #define SQLITE_STATUS_SCRATCH_SIZE 8
! 6413: #define SQLITE_STATUS_MALLOC_COUNT 9
! 6414:
! 6415: /*
! 6416: ** CAPI3REF: Database Connection Status
! 6417: **
! 6418: ** ^This interface is used to retrieve runtime status information
! 6419: ** about a single [database connection]. ^The first argument is the
! 6420: ** database connection object to be interrogated. ^The second argument
! 6421: ** is an integer constant, taken from the set of
! 6422: ** [SQLITE_DBSTATUS options], that
! 6423: ** determines the parameter to interrogate. The set of
! 6424: ** [SQLITE_DBSTATUS options] is likely
! 6425: ** to grow in future releases of SQLite.
! 6426: **
! 6427: ** ^The current value of the requested parameter is written into *pCur
! 6428: ** and the highest instantaneous value is written into *pHiwtr. ^If
! 6429: ** the resetFlg is true, then the highest instantaneous value is
! 6430: ** reset back down to the current value.
! 6431: **
! 6432: ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
! 6433: ** non-zero [error code] on failure.
! 6434: **
! 6435: ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
! 6436: */
! 6437: SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
! 6438:
! 6439: /*
! 6440: ** CAPI3REF: Status Parameters for database connections
! 6441: ** KEYWORDS: {SQLITE_DBSTATUS options}
! 6442: **
! 6443: ** These constants are the available integer "verbs" that can be passed as
! 6444: ** the second argument to the [sqlite3_db_status()] interface.
! 6445: **
! 6446: ** New verbs may be added in future releases of SQLite. Existing verbs
! 6447: ** might be discontinued. Applications should check the return code from
! 6448: ** [sqlite3_db_status()] to make sure that the call worked.
! 6449: ** The [sqlite3_db_status()] interface will return a non-zero error code
! 6450: ** if a discontinued or unsupported verb is invoked.
! 6451: **
! 6452: ** <dl>
! 6453: ** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
! 6454: ** <dd>This parameter returns the number of lookaside memory slots currently
! 6455: ** checked out.</dd>)^
! 6456: **
! 6457: ** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
! 6458: ** <dd>This parameter returns the number malloc attempts that were
! 6459: ** satisfied using lookaside memory. Only the high-water value is meaningful;
! 6460: ** the current value is always zero.)^
! 6461: **
! 6462: ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
! 6463: ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
! 6464: ** <dd>This parameter returns the number malloc attempts that might have
! 6465: ** been satisfied using lookaside memory but failed due to the amount of
! 6466: ** memory requested being larger than the lookaside slot size.
! 6467: ** Only the high-water value is meaningful;
! 6468: ** the current value is always zero.)^
! 6469: **
! 6470: ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
! 6471: ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
! 6472: ** <dd>This parameter returns the number malloc attempts that might have
! 6473: ** been satisfied using lookaside memory but failed due to all lookaside
! 6474: ** memory already being in use.
! 6475: ** Only the high-water value is meaningful;
! 6476: ** the current value is always zero.)^
! 6477: **
! 6478: ** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
! 6479: ** <dd>This parameter returns the approximate number of of bytes of heap
! 6480: ** memory used by all pager caches associated with the database connection.)^
! 6481: ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
! 6482: **
! 6483: ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
! 6484: ** <dd>This parameter returns the approximate number of of bytes of heap
! 6485: ** memory used to store the schema for all databases associated
! 6486: ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
! 6487: ** ^The full amount of memory used by the schemas is reported, even if the
! 6488: ** schema memory is shared with other database connections due to
! 6489: ** [shared cache mode] being enabled.
! 6490: ** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
! 6491: **
! 6492: ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
! 6493: ** <dd>This parameter returns the approximate number of of bytes of heap
! 6494: ** and lookaside memory used by all prepared statements associated with
! 6495: ** the database connection.)^
! 6496: ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
! 6497: ** </dd>
! 6498: **
! 6499: ** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
! 6500: ** <dd>This parameter returns the number of pager cache hits that have
! 6501: ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
! 6502: ** is always 0.
! 6503: ** </dd>
! 6504: **
! 6505: ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
! 6506: ** <dd>This parameter returns the number of pager cache misses that have
! 6507: ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
! 6508: ** is always 0.
! 6509: ** </dd>
! 6510: ** </dl>
! 6511: */
! 6512: #define SQLITE_DBSTATUS_LOOKASIDE_USED 0
! 6513: #define SQLITE_DBSTATUS_CACHE_USED 1
! 6514: #define SQLITE_DBSTATUS_SCHEMA_USED 2
! 6515: #define SQLITE_DBSTATUS_STMT_USED 3
! 6516: #define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
! 6517: #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
! 6518: #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
! 6519: #define SQLITE_DBSTATUS_CACHE_HIT 7
! 6520: #define SQLITE_DBSTATUS_CACHE_MISS 8
! 6521: #define SQLITE_DBSTATUS_MAX 8 /* Largest defined DBSTATUS */
! 6522:
! 6523:
! 6524: /*
! 6525: ** CAPI3REF: Prepared Statement Status
! 6526: **
! 6527: ** ^(Each prepared statement maintains various
! 6528: ** [SQLITE_STMTSTATUS counters] that measure the number
! 6529: ** of times it has performed specific operations.)^ These counters can
! 6530: ** be used to monitor the performance characteristics of the prepared
! 6531: ** statements. For example, if the number of table steps greatly exceeds
! 6532: ** the number of table searches or result rows, that would tend to indicate
! 6533: ** that the prepared statement is using a full table scan rather than
! 6534: ** an index.
! 6535: **
! 6536: ** ^(This interface is used to retrieve and reset counter values from
! 6537: ** a [prepared statement]. The first argument is the prepared statement
! 6538: ** object to be interrogated. The second argument
! 6539: ** is an integer code for a specific [SQLITE_STMTSTATUS counter]
! 6540: ** to be interrogated.)^
! 6541: ** ^The current value of the requested counter is returned.
! 6542: ** ^If the resetFlg is true, then the counter is reset to zero after this
! 6543: ** interface call returns.
! 6544: **
! 6545: ** See also: [sqlite3_status()] and [sqlite3_db_status()].
! 6546: */
! 6547: SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
! 6548:
! 6549: /*
! 6550: ** CAPI3REF: Status Parameters for prepared statements
! 6551: ** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
! 6552: **
! 6553: ** These preprocessor macros define integer codes that name counter
! 6554: ** values associated with the [sqlite3_stmt_status()] interface.
! 6555: ** The meanings of the various counters are as follows:
! 6556: **
! 6557: ** <dl>
! 6558: ** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
! 6559: ** <dd>^This is the number of times that SQLite has stepped forward in
! 6560: ** a table as part of a full table scan. Large numbers for this counter
! 6561: ** may indicate opportunities for performance improvement through
! 6562: ** careful use of indices.</dd>
! 6563: **
! 6564: ** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
! 6565: ** <dd>^This is the number of sort operations that have occurred.
! 6566: ** A non-zero value in this counter may indicate an opportunity to
! 6567: ** improvement performance through careful use of indices.</dd>
! 6568: **
! 6569: ** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
! 6570: ** <dd>^This is the number of rows inserted into transient indices that
! 6571: ** were created automatically in order to help joins run faster.
! 6572: ** A non-zero value in this counter may indicate an opportunity to
! 6573: ** improvement performance by adding permanent indices that do not
! 6574: ** need to be reinitialized each time the statement is run.</dd>
! 6575: ** </dl>
! 6576: */
! 6577: #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
! 6578: #define SQLITE_STMTSTATUS_SORT 2
! 6579: #define SQLITE_STMTSTATUS_AUTOINDEX 3
! 6580:
! 6581: /*
! 6582: ** CAPI3REF: Custom Page Cache Object
! 6583: **
! 6584: ** The sqlite3_pcache type is opaque. It is implemented by
! 6585: ** the pluggable module. The SQLite core has no knowledge of
! 6586: ** its size or internal structure and never deals with the
! 6587: ** sqlite3_pcache object except by holding and passing pointers
! 6588: ** to the object.
! 6589: **
! 6590: ** See [sqlite3_pcache_methods2] for additional information.
! 6591: */
! 6592: typedef struct sqlite3_pcache sqlite3_pcache;
! 6593:
! 6594: /*
! 6595: ** CAPI3REF: Custom Page Cache Object
! 6596: **
! 6597: ** The sqlite3_pcache_page object represents a single page in the
! 6598: ** page cache. The page cache will allocate instances of this
! 6599: ** object. Various methods of the page cache use pointers to instances
! 6600: ** of this object as parameters or as their return value.
! 6601: **
! 6602: ** See [sqlite3_pcache_methods2] for additional information.
! 6603: */
! 6604: typedef struct sqlite3_pcache_page sqlite3_pcache_page;
! 6605: struct sqlite3_pcache_page {
! 6606: void *pBuf; /* The content of the page */
! 6607: void *pExtra; /* Extra information associated with the page */
! 6608: };
! 6609:
! 6610: /*
! 6611: ** CAPI3REF: Application Defined Page Cache.
! 6612: ** KEYWORDS: {page cache}
! 6613: **
! 6614: ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
! 6615: ** register an alternative page cache implementation by passing in an
! 6616: ** instance of the sqlite3_pcache_methods2 structure.)^
! 6617: ** In many applications, most of the heap memory allocated by
! 6618: ** SQLite is used for the page cache.
! 6619: ** By implementing a
! 6620: ** custom page cache using this API, an application can better control
! 6621: ** the amount of memory consumed by SQLite, the way in which
! 6622: ** that memory is allocated and released, and the policies used to
! 6623: ** determine exactly which parts of a database file are cached and for
! 6624: ** how long.
! 6625: **
! 6626: ** The alternative page cache mechanism is an
! 6627: ** extreme measure that is only needed by the most demanding applications.
! 6628: ** The built-in page cache is recommended for most uses.
! 6629: **
! 6630: ** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
! 6631: ** internal buffer by SQLite within the call to [sqlite3_config]. Hence
! 6632: ** the application may discard the parameter after the call to
! 6633: ** [sqlite3_config()] returns.)^
! 6634: **
! 6635: ** [[the xInit() page cache method]]
! 6636: ** ^(The xInit() method is called once for each effective
! 6637: ** call to [sqlite3_initialize()])^
! 6638: ** (usually only once during the lifetime of the process). ^(The xInit()
! 6639: ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
! 6640: ** The intent of the xInit() method is to set up global data structures
! 6641: ** required by the custom page cache implementation.
! 6642: ** ^(If the xInit() method is NULL, then the
! 6643: ** built-in default page cache is used instead of the application defined
! 6644: ** page cache.)^
! 6645: **
! 6646: ** [[the xShutdown() page cache method]]
! 6647: ** ^The xShutdown() method is called by [sqlite3_shutdown()].
! 6648: ** It can be used to clean up
! 6649: ** any outstanding resources before process shutdown, if required.
! 6650: ** ^The xShutdown() method may be NULL.
! 6651: **
! 6652: ** ^SQLite automatically serializes calls to the xInit method,
! 6653: ** so the xInit method need not be threadsafe. ^The
! 6654: ** xShutdown method is only called from [sqlite3_shutdown()] so it does
! 6655: ** not need to be threadsafe either. All other methods must be threadsafe
! 6656: ** in multithreaded applications.
! 6657: **
! 6658: ** ^SQLite will never invoke xInit() more than once without an intervening
! 6659: ** call to xShutdown().
! 6660: **
! 6661: ** [[the xCreate() page cache methods]]
! 6662: ** ^SQLite invokes the xCreate() method to construct a new cache instance.
! 6663: ** SQLite will typically create one cache instance for each open database file,
! 6664: ** though this is not guaranteed. ^The
! 6665: ** first parameter, szPage, is the size in bytes of the pages that must
! 6666: ** be allocated by the cache. ^szPage will always a power of two. ^The
! 6667: ** second parameter szExtra is a number of bytes of extra storage
! 6668: ** associated with each page cache entry. ^The szExtra parameter will
! 6669: ** a number less than 250. SQLite will use the
! 6670: ** extra szExtra bytes on each page to store metadata about the underlying
! 6671: ** database page on disk. The value passed into szExtra depends
! 6672: ** on the SQLite version, the target platform, and how SQLite was compiled.
! 6673: ** ^The third argument to xCreate(), bPurgeable, is true if the cache being
! 6674: ** created will be used to cache database pages of a file stored on disk, or
! 6675: ** false if it is used for an in-memory database. The cache implementation
! 6676: ** does not have to do anything special based with the value of bPurgeable;
! 6677: ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
! 6678: ** never invoke xUnpin() except to deliberately delete a page.
! 6679: ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
! 6680: ** false will always have the "discard" flag set to true.
! 6681: ** ^Hence, a cache created with bPurgeable false will
! 6682: ** never contain any unpinned pages.
! 6683: **
! 6684: ** [[the xCachesize() page cache method]]
! 6685: ** ^(The xCachesize() method may be called at any time by SQLite to set the
! 6686: ** suggested maximum cache-size (number of pages stored by) the cache
! 6687: ** instance passed as the first argument. This is the value configured using
! 6688: ** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable
! 6689: ** parameter, the implementation is not required to do anything with this
! 6690: ** value; it is advisory only.
! 6691: **
! 6692: ** [[the xPagecount() page cache methods]]
! 6693: ** The xPagecount() method must return the number of pages currently
! 6694: ** stored in the cache, both pinned and unpinned.
! 6695: **
! 6696: ** [[the xFetch() page cache methods]]
! 6697: ** The xFetch() method locates a page in the cache and returns a pointer to
! 6698: ** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
! 6699: ** The pBuf element of the returned sqlite3_pcache_page object will be a
! 6700: ** pointer to a buffer of szPage bytes used to store the content of a
! 6701: ** single database page. The pExtra element of sqlite3_pcache_page will be
! 6702: ** a pointer to the szExtra bytes of extra storage that SQLite has requested
! 6703: ** for each entry in the page cache.
! 6704: **
! 6705: ** The page to be fetched is determined by the key. ^The minimum key value
! 6706: ** is 1. After it has been retrieved using xFetch, the page is considered
! 6707: ** to be "pinned".
! 6708: **
! 6709: ** If the requested page is already in the page cache, then the page cache
! 6710: ** implementation must return a pointer to the page buffer with its content
! 6711: ** intact. If the requested page is not already in the cache, then the
! 6712: ** cache implementation should use the value of the createFlag
! 6713: ** parameter to help it determined what action to take:
! 6714: **
! 6715: ** <table border=1 width=85% align=center>
! 6716: ** <tr><th> createFlag <th> Behaviour when page is not already in cache
! 6717: ** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
! 6718: ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
! 6719: ** Otherwise return NULL.
! 6720: ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
! 6721: ** NULL if allocating a new page is effectively impossible.
! 6722: ** </table>
! 6723: **
! 6724: ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite
! 6725: ** will only use a createFlag of 2 after a prior call with a createFlag of 1
! 6726: ** failed.)^ In between the to xFetch() calls, SQLite may
! 6727: ** attempt to unpin one or more cache pages by spilling the content of
! 6728: ** pinned pages to disk and synching the operating system disk cache.
! 6729: **
! 6730: ** [[the xUnpin() page cache method]]
! 6731: ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
! 6732: ** as its second argument. If the third parameter, discard, is non-zero,
! 6733: ** then the page must be evicted from the cache.
! 6734: ** ^If the discard parameter is
! 6735: ** zero, then the page may be discarded or retained at the discretion of
! 6736: ** page cache implementation. ^The page cache implementation
! 6737: ** may choose to evict unpinned pages at any time.
! 6738: **
! 6739: ** The cache must not perform any reference counting. A single
! 6740: ** call to xUnpin() unpins the page regardless of the number of prior calls
! 6741: ** to xFetch().
! 6742: **
! 6743: ** [[the xRekey() page cache methods]]
! 6744: ** The xRekey() method is used to change the key value associated with the
! 6745: ** page passed as the second argument. If the cache
! 6746: ** previously contains an entry associated with newKey, it must be
! 6747: ** discarded. ^Any prior cache entry associated with newKey is guaranteed not
! 6748: ** to be pinned.
! 6749: **
! 6750: ** When SQLite calls the xTruncate() method, the cache must discard all
! 6751: ** existing cache entries with page numbers (keys) greater than or equal
! 6752: ** to the value of the iLimit parameter passed to xTruncate(). If any
! 6753: ** of these pages are pinned, they are implicitly unpinned, meaning that
! 6754: ** they can be safely discarded.
! 6755: **
! 6756: ** [[the xDestroy() page cache method]]
! 6757: ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
! 6758: ** All resources associated with the specified cache should be freed. ^After
! 6759: ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
! 6760: ** handle invalid, and will not use it with any other sqlite3_pcache_methods2
! 6761: ** functions.
! 6762: **
! 6763: ** [[the xShrink() page cache method]]
! 6764: ** ^SQLite invokes the xShrink() method when it wants the page cache to
! 6765: ** free up as much of heap memory as possible. The page cache implementation
! 6766: ** is not obligated to free any memory, but well-behaved implementations should
! 6767: ** do their best.
! 6768: */
! 6769: typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
! 6770: struct sqlite3_pcache_methods2 {
! 6771: int iVersion;
! 6772: void *pArg;
! 6773: int (*xInit)(void*);
! 6774: void (*xShutdown)(void*);
! 6775: sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
! 6776: void (*xCachesize)(sqlite3_pcache*, int nCachesize);
! 6777: int (*xPagecount)(sqlite3_pcache*);
! 6778: sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
! 6779: void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
! 6780: void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
! 6781: unsigned oldKey, unsigned newKey);
! 6782: void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
! 6783: void (*xDestroy)(sqlite3_pcache*);
! 6784: void (*xShrink)(sqlite3_pcache*);
! 6785: };
! 6786:
! 6787: /*
! 6788: ** This is the obsolete pcache_methods object that has now been replaced
! 6789: ** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
! 6790: ** retained in the header file for backwards compatibility only.
! 6791: */
! 6792: typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
! 6793: struct sqlite3_pcache_methods {
! 6794: void *pArg;
! 6795: int (*xInit)(void*);
! 6796: void (*xShutdown)(void*);
! 6797: sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
! 6798: void (*xCachesize)(sqlite3_pcache*, int nCachesize);
! 6799: int (*xPagecount)(sqlite3_pcache*);
! 6800: void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
! 6801: void (*xUnpin)(sqlite3_pcache*, void*, int discard);
! 6802: void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
! 6803: void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
! 6804: void (*xDestroy)(sqlite3_pcache*);
! 6805: };
! 6806:
! 6807:
! 6808: /*
! 6809: ** CAPI3REF: Online Backup Object
! 6810: **
! 6811: ** The sqlite3_backup object records state information about an ongoing
! 6812: ** online backup operation. ^The sqlite3_backup object is created by
! 6813: ** a call to [sqlite3_backup_init()] and is destroyed by a call to
! 6814: ** [sqlite3_backup_finish()].
! 6815: **
! 6816: ** See Also: [Using the SQLite Online Backup API]
! 6817: */
! 6818: typedef struct sqlite3_backup sqlite3_backup;
! 6819:
! 6820: /*
! 6821: ** CAPI3REF: Online Backup API.
! 6822: **
! 6823: ** The backup API copies the content of one database into another.
! 6824: ** It is useful either for creating backups of databases or
! 6825: ** for copying in-memory databases to or from persistent files.
! 6826: **
! 6827: ** See Also: [Using the SQLite Online Backup API]
! 6828: **
! 6829: ** ^SQLite holds a write transaction open on the destination database file
! 6830: ** for the duration of the backup operation.
! 6831: ** ^The source database is read-locked only while it is being read;
! 6832: ** it is not locked continuously for the entire backup operation.
! 6833: ** ^Thus, the backup may be performed on a live source database without
! 6834: ** preventing other database connections from
! 6835: ** reading or writing to the source database while the backup is underway.
! 6836: **
! 6837: ** ^(To perform a backup operation:
! 6838: ** <ol>
! 6839: ** <li><b>sqlite3_backup_init()</b> is called once to initialize the
! 6840: ** backup,
! 6841: ** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
! 6842: ** the data between the two databases, and finally
! 6843: ** <li><b>sqlite3_backup_finish()</b> is called to release all resources
! 6844: ** associated with the backup operation.
! 6845: ** </ol>)^
! 6846: ** There should be exactly one call to sqlite3_backup_finish() for each
! 6847: ** successful call to sqlite3_backup_init().
! 6848: **
! 6849: ** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
! 6850: **
! 6851: ** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
! 6852: ** [database connection] associated with the destination database
! 6853: ** and the database name, respectively.
! 6854: ** ^The database name is "main" for the main database, "temp" for the
! 6855: ** temporary database, or the name specified after the AS keyword in
! 6856: ** an [ATTACH] statement for an attached database.
! 6857: ** ^The S and M arguments passed to
! 6858: ** sqlite3_backup_init(D,N,S,M) identify the [database connection]
! 6859: ** and database name of the source database, respectively.
! 6860: ** ^The source and destination [database connections] (parameters S and D)
! 6861: ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
! 6862: ** an error.
! 6863: **
! 6864: ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
! 6865: ** returned and an error code and error message are stored in the
! 6866: ** destination [database connection] D.
! 6867: ** ^The error code and message for the failed call to sqlite3_backup_init()
! 6868: ** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
! 6869: ** [sqlite3_errmsg16()] functions.
! 6870: ** ^A successful call to sqlite3_backup_init() returns a pointer to an
! 6871: ** [sqlite3_backup] object.
! 6872: ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
! 6873: ** sqlite3_backup_finish() functions to perform the specified backup
! 6874: ** operation.
! 6875: **
! 6876: ** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
! 6877: **
! 6878: ** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
! 6879: ** the source and destination databases specified by [sqlite3_backup] object B.
! 6880: ** ^If N is negative, all remaining source pages are copied.
! 6881: ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
! 6882: ** are still more pages to be copied, then the function returns [SQLITE_OK].
! 6883: ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
! 6884: ** from source to destination, then it returns [SQLITE_DONE].
! 6885: ** ^If an error occurs while running sqlite3_backup_step(B,N),
! 6886: ** then an [error code] is returned. ^As well as [SQLITE_OK] and
! 6887: ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
! 6888: ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
! 6889: ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
! 6890: **
! 6891: ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
! 6892: ** <ol>
! 6893: ** <li> the destination database was opened read-only, or
! 6894: ** <li> the destination database is using write-ahead-log journaling
! 6895: ** and the destination and source page sizes differ, or
! 6896: ** <li> the destination database is an in-memory database and the
! 6897: ** destination and source page sizes differ.
! 6898: ** </ol>)^
! 6899: **
! 6900: ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
! 6901: ** the [sqlite3_busy_handler | busy-handler function]
! 6902: ** is invoked (if one is specified). ^If the
! 6903: ** busy-handler returns non-zero before the lock is available, then
! 6904: ** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
! 6905: ** sqlite3_backup_step() can be retried later. ^If the source
! 6906: ** [database connection]
! 6907: ** is being used to write to the source database when sqlite3_backup_step()
! 6908: ** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
! 6909: ** case the call to sqlite3_backup_step() can be retried later on. ^(If
! 6910: ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
! 6911: ** [SQLITE_READONLY] is returned, then
! 6912: ** there is no point in retrying the call to sqlite3_backup_step(). These
! 6913: ** errors are considered fatal.)^ The application must accept
! 6914: ** that the backup operation has failed and pass the backup operation handle
! 6915: ** to the sqlite3_backup_finish() to release associated resources.
! 6916: **
! 6917: ** ^The first call to sqlite3_backup_step() obtains an exclusive lock
! 6918: ** on the destination file. ^The exclusive lock is not released until either
! 6919: ** sqlite3_backup_finish() is called or the backup operation is complete
! 6920: ** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to
! 6921: ** sqlite3_backup_step() obtains a [shared lock] on the source database that
! 6922: ** lasts for the duration of the sqlite3_backup_step() call.
! 6923: ** ^Because the source database is not locked between calls to
! 6924: ** sqlite3_backup_step(), the source database may be modified mid-way
! 6925: ** through the backup process. ^If the source database is modified by an
! 6926: ** external process or via a database connection other than the one being
! 6927: ** used by the backup operation, then the backup will be automatically
! 6928: ** restarted by the next call to sqlite3_backup_step(). ^If the source
! 6929: ** database is modified by the using the same database connection as is used
! 6930: ** by the backup operation, then the backup database is automatically
! 6931: ** updated at the same time.
! 6932: **
! 6933: ** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
! 6934: **
! 6935: ** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
! 6936: ** application wishes to abandon the backup operation, the application
! 6937: ** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
! 6938: ** ^The sqlite3_backup_finish() interfaces releases all
! 6939: ** resources associated with the [sqlite3_backup] object.
! 6940: ** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
! 6941: ** active write-transaction on the destination database is rolled back.
! 6942: ** The [sqlite3_backup] object is invalid
! 6943: ** and may not be used following a call to sqlite3_backup_finish().
! 6944: **
! 6945: ** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
! 6946: ** sqlite3_backup_step() errors occurred, regardless or whether or not
! 6947: ** sqlite3_backup_step() completed.
! 6948: ** ^If an out-of-memory condition or IO error occurred during any prior
! 6949: ** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
! 6950: ** sqlite3_backup_finish() returns the corresponding [error code].
! 6951: **
! 6952: ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
! 6953: ** is not a permanent error and does not affect the return value of
! 6954: ** sqlite3_backup_finish().
! 6955: **
! 6956: ** [[sqlite3_backup__remaining()]] [[sqlite3_backup_pagecount()]]
! 6957: ** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
! 6958: **
! 6959: ** ^Each call to sqlite3_backup_step() sets two values inside
! 6960: ** the [sqlite3_backup] object: the number of pages still to be backed
! 6961: ** up and the total number of pages in the source database file.
! 6962: ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
! 6963: ** retrieve these two values, respectively.
! 6964: **
! 6965: ** ^The values returned by these functions are only updated by
! 6966: ** sqlite3_backup_step(). ^If the source database is modified during a backup
! 6967: ** operation, then the values are not updated to account for any extra
! 6968: ** pages that need to be updated or the size of the source database file
! 6969: ** changing.
! 6970: **
! 6971: ** <b>Concurrent Usage of Database Handles</b>
! 6972: **
! 6973: ** ^The source [database connection] may be used by the application for other
! 6974: ** purposes while a backup operation is underway or being initialized.
! 6975: ** ^If SQLite is compiled and configured to support threadsafe database
! 6976: ** connections, then the source database connection may be used concurrently
! 6977: ** from within other threads.
! 6978: **
! 6979: ** However, the application must guarantee that the destination
! 6980: ** [database connection] is not passed to any other API (by any thread) after
! 6981: ** sqlite3_backup_init() is called and before the corresponding call to
! 6982: ** sqlite3_backup_finish(). SQLite does not currently check to see
! 6983: ** if the application incorrectly accesses the destination [database connection]
! 6984: ** and so no error code is reported, but the operations may malfunction
! 6985: ** nevertheless. Use of the destination database connection while a
! 6986: ** backup is in progress might also also cause a mutex deadlock.
! 6987: **
! 6988: ** If running in [shared cache mode], the application must
! 6989: ** guarantee that the shared cache used by the destination database
! 6990: ** is not accessed while the backup is running. In practice this means
! 6991: ** that the application must guarantee that the disk file being
! 6992: ** backed up to is not accessed by any connection within the process,
! 6993: ** not just the specific connection that was passed to sqlite3_backup_init().
! 6994: **
! 6995: ** The [sqlite3_backup] object itself is partially threadsafe. Multiple
! 6996: ** threads may safely make multiple concurrent calls to sqlite3_backup_step().
! 6997: ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
! 6998: ** APIs are not strictly speaking threadsafe. If they are invoked at the
! 6999: ** same time as another thread is invoking sqlite3_backup_step() it is
! 7000: ** possible that they return invalid values.
! 7001: */
! 7002: SQLITE_API sqlite3_backup *sqlite3_backup_init(
! 7003: sqlite3 *pDest, /* Destination database handle */
! 7004: const char *zDestName, /* Destination database name */
! 7005: sqlite3 *pSource, /* Source database handle */
! 7006: const char *zSourceName /* Source database name */
! 7007: );
! 7008: SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
! 7009: SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
! 7010: SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
! 7011: SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
! 7012:
! 7013: /*
! 7014: ** CAPI3REF: Unlock Notification
! 7015: **
! 7016: ** ^When running in shared-cache mode, a database operation may fail with
! 7017: ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
! 7018: ** individual tables within the shared-cache cannot be obtained. See
! 7019: ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
! 7020: ** ^This API may be used to register a callback that SQLite will invoke
! 7021: ** when the connection currently holding the required lock relinquishes it.
! 7022: ** ^This API is only available if the library was compiled with the
! 7023: ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
! 7024: **
! 7025: ** See Also: [Using the SQLite Unlock Notification Feature].
! 7026: **
! 7027: ** ^Shared-cache locks are released when a database connection concludes
! 7028: ** its current transaction, either by committing it or rolling it back.
! 7029: **
! 7030: ** ^When a connection (known as the blocked connection) fails to obtain a
! 7031: ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
! 7032: ** identity of the database connection (the blocking connection) that
! 7033: ** has locked the required resource is stored internally. ^After an
! 7034: ** application receives an SQLITE_LOCKED error, it may call the
! 7035: ** sqlite3_unlock_notify() method with the blocked connection handle as
! 7036: ** the first argument to register for a callback that will be invoked
! 7037: ** when the blocking connections current transaction is concluded. ^The
! 7038: ** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
! 7039: ** call that concludes the blocking connections transaction.
! 7040: **
! 7041: ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
! 7042: ** there is a chance that the blocking connection will have already
! 7043: ** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
! 7044: ** If this happens, then the specified callback is invoked immediately,
! 7045: ** from within the call to sqlite3_unlock_notify().)^
! 7046: **
! 7047: ** ^If the blocked connection is attempting to obtain a write-lock on a
! 7048: ** shared-cache table, and more than one other connection currently holds
! 7049: ** a read-lock on the same table, then SQLite arbitrarily selects one of
! 7050: ** the other connections to use as the blocking connection.
! 7051: **
! 7052: ** ^(There may be at most one unlock-notify callback registered by a
! 7053: ** blocked connection. If sqlite3_unlock_notify() is called when the
! 7054: ** blocked connection already has a registered unlock-notify callback,
! 7055: ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
! 7056: ** called with a NULL pointer as its second argument, then any existing
! 7057: ** unlock-notify callback is canceled. ^The blocked connections
! 7058: ** unlock-notify callback may also be canceled by closing the blocked
! 7059: ** connection using [sqlite3_close()].
! 7060: **
! 7061: ** The unlock-notify callback is not reentrant. If an application invokes
! 7062: ** any sqlite3_xxx API functions from within an unlock-notify callback, a
! 7063: ** crash or deadlock may be the result.
! 7064: **
! 7065: ** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
! 7066: ** returns SQLITE_OK.
! 7067: **
! 7068: ** <b>Callback Invocation Details</b>
! 7069: **
! 7070: ** When an unlock-notify callback is registered, the application provides a
! 7071: ** single void* pointer that is passed to the callback when it is invoked.
! 7072: ** However, the signature of the callback function allows SQLite to pass
! 7073: ** it an array of void* context pointers. The first argument passed to
! 7074: ** an unlock-notify callback is a pointer to an array of void* pointers,
! 7075: ** and the second is the number of entries in the array.
! 7076: **
! 7077: ** When a blocking connections transaction is concluded, there may be
! 7078: ** more than one blocked connection that has registered for an unlock-notify
! 7079: ** callback. ^If two or more such blocked connections have specified the
! 7080: ** same callback function, then instead of invoking the callback function
! 7081: ** multiple times, it is invoked once with the set of void* context pointers
! 7082: ** specified by the blocked connections bundled together into an array.
! 7083: ** This gives the application an opportunity to prioritize any actions
! 7084: ** related to the set of unblocked database connections.
! 7085: **
! 7086: ** <b>Deadlock Detection</b>
! 7087: **
! 7088: ** Assuming that after registering for an unlock-notify callback a
! 7089: ** database waits for the callback to be issued before taking any further
! 7090: ** action (a reasonable assumption), then using this API may cause the
! 7091: ** application to deadlock. For example, if connection X is waiting for
! 7092: ** connection Y's transaction to be concluded, and similarly connection
! 7093: ** Y is waiting on connection X's transaction, then neither connection
! 7094: ** will proceed and the system may remain deadlocked indefinitely.
! 7095: **
! 7096: ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
! 7097: ** detection. ^If a given call to sqlite3_unlock_notify() would put the
! 7098: ** system in a deadlocked state, then SQLITE_LOCKED is returned and no
! 7099: ** unlock-notify callback is registered. The system is said to be in
! 7100: ** a deadlocked state if connection A has registered for an unlock-notify
! 7101: ** callback on the conclusion of connection B's transaction, and connection
! 7102: ** B has itself registered for an unlock-notify callback when connection
! 7103: ** A's transaction is concluded. ^Indirect deadlock is also detected, so
! 7104: ** the system is also considered to be deadlocked if connection B has
! 7105: ** registered for an unlock-notify callback on the conclusion of connection
! 7106: ** C's transaction, where connection C is waiting on connection A. ^Any
! 7107: ** number of levels of indirection are allowed.
! 7108: **
! 7109: ** <b>The "DROP TABLE" Exception</b>
! 7110: **
! 7111: ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
! 7112: ** always appropriate to call sqlite3_unlock_notify(). There is however,
! 7113: ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
! 7114: ** SQLite checks if there are any currently executing SELECT statements
! 7115: ** that belong to the same connection. If there are, SQLITE_LOCKED is
! 7116: ** returned. In this case there is no "blocking connection", so invoking
! 7117: ** sqlite3_unlock_notify() results in the unlock-notify callback being
! 7118: ** invoked immediately. If the application then re-attempts the "DROP TABLE"
! 7119: ** or "DROP INDEX" query, an infinite loop might be the result.
! 7120: **
! 7121: ** One way around this problem is to check the extended error code returned
! 7122: ** by an sqlite3_step() call. ^(If there is a blocking connection, then the
! 7123: ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
! 7124: ** the special "DROP TABLE/INDEX" case, the extended error code is just
! 7125: ** SQLITE_LOCKED.)^
! 7126: */
! 7127: SQLITE_API int sqlite3_unlock_notify(
! 7128: sqlite3 *pBlocked, /* Waiting connection */
! 7129: void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
! 7130: void *pNotifyArg /* Argument to pass to xNotify */
! 7131: );
! 7132:
! 7133:
! 7134: /*
! 7135: ** CAPI3REF: String Comparison
! 7136: **
! 7137: ** ^The [sqlite3_strnicmp()] API allows applications and extensions to
! 7138: ** compare the contents of two buffers containing UTF-8 strings in a
! 7139: ** case-independent fashion, using the same definition of case independence
! 7140: ** that SQLite uses internally when comparing identifiers.
! 7141: */
! 7142: SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
! 7143:
! 7144: /*
! 7145: ** CAPI3REF: Error Logging Interface
! 7146: **
! 7147: ** ^The [sqlite3_log()] interface writes a message into the error log
! 7148: ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
! 7149: ** ^If logging is enabled, the zFormat string and subsequent arguments are
! 7150: ** used with [sqlite3_snprintf()] to generate the final output string.
! 7151: **
! 7152: ** The sqlite3_log() interface is intended for use by extensions such as
! 7153: ** virtual tables, collating functions, and SQL functions. While there is
! 7154: ** nothing to prevent an application from calling sqlite3_log(), doing so
! 7155: ** is considered bad form.
! 7156: **
! 7157: ** The zFormat string must not be NULL.
! 7158: **
! 7159: ** To avoid deadlocks and other threading problems, the sqlite3_log() routine
! 7160: ** will not use dynamically allocated memory. The log message is stored in
! 7161: ** a fixed-length buffer on the stack. If the log message is longer than
! 7162: ** a few hundred characters, it will be truncated to the length of the
! 7163: ** buffer.
! 7164: */
! 7165: SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
! 7166:
! 7167: /*
! 7168: ** CAPI3REF: Write-Ahead Log Commit Hook
! 7169: **
! 7170: ** ^The [sqlite3_wal_hook()] function is used to register a callback that
! 7171: ** will be invoked each time a database connection commits data to a
! 7172: ** [write-ahead log] (i.e. whenever a transaction is committed in
! 7173: ** [journal_mode | journal_mode=WAL mode]).
! 7174: **
! 7175: ** ^The callback is invoked by SQLite after the commit has taken place and
! 7176: ** the associated write-lock on the database released, so the implementation
! 7177: ** may read, write or [checkpoint] the database as required.
! 7178: **
! 7179: ** ^The first parameter passed to the callback function when it is invoked
! 7180: ** is a copy of the third parameter passed to sqlite3_wal_hook() when
! 7181: ** registering the callback. ^The second is a copy of the database handle.
! 7182: ** ^The third parameter is the name of the database that was written to -
! 7183: ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
! 7184: ** is the number of pages currently in the write-ahead log file,
! 7185: ** including those that were just committed.
! 7186: **
! 7187: ** The callback function should normally return [SQLITE_OK]. ^If an error
! 7188: ** code is returned, that error will propagate back up through the
! 7189: ** SQLite code base to cause the statement that provoked the callback
! 7190: ** to report an error, though the commit will have still occurred. If the
! 7191: ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
! 7192: ** that does not correspond to any valid SQLite error code, the results
! 7193: ** are undefined.
! 7194: **
! 7195: ** A single database handle may have at most a single write-ahead log callback
! 7196: ** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
! 7197: ** previously registered write-ahead log callback. ^Note that the
! 7198: ** [sqlite3_wal_autocheckpoint()] interface and the
! 7199: ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
! 7200: ** those overwrite any prior [sqlite3_wal_hook()] settings.
! 7201: */
! 7202: SQLITE_API void *sqlite3_wal_hook(
! 7203: sqlite3*,
! 7204: int(*)(void *,sqlite3*,const char*,int),
! 7205: void*
! 7206: );
! 7207:
! 7208: /*
! 7209: ** CAPI3REF: Configure an auto-checkpoint
! 7210: **
! 7211: ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
! 7212: ** [sqlite3_wal_hook()] that causes any database on [database connection] D
! 7213: ** to automatically [checkpoint]
! 7214: ** after committing a transaction if there are N or
! 7215: ** more frames in the [write-ahead log] file. ^Passing zero or
! 7216: ** a negative value as the nFrame parameter disables automatic
! 7217: ** checkpoints entirely.
! 7218: **
! 7219: ** ^The callback registered by this function replaces any existing callback
! 7220: ** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
! 7221: ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
! 7222: ** configured by this function.
! 7223: **
! 7224: ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
! 7225: ** from SQL.
! 7226: **
! 7227: ** ^Every new [database connection] defaults to having the auto-checkpoint
! 7228: ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
! 7229: ** pages. The use of this interface
! 7230: ** is only necessary if the default setting is found to be suboptimal
! 7231: ** for a particular application.
! 7232: */
! 7233: SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
! 7234:
! 7235: /*
! 7236: ** CAPI3REF: Checkpoint a database
! 7237: **
! 7238: ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
! 7239: ** on [database connection] D to be [checkpointed]. ^If X is NULL or an
! 7240: ** empty string, then a checkpoint is run on all databases of
! 7241: ** connection D. ^If the database connection D is not in
! 7242: ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
! 7243: **
! 7244: ** ^The [wal_checkpoint pragma] can be used to invoke this interface
! 7245: ** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
! 7246: ** [wal_autocheckpoint pragma] can be used to cause this interface to be
! 7247: ** run whenever the WAL reaches a certain size threshold.
! 7248: **
! 7249: ** See also: [sqlite3_wal_checkpoint_v2()]
! 7250: */
! 7251: SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
! 7252:
! 7253: /*
! 7254: ** CAPI3REF: Checkpoint a database
! 7255: **
! 7256: ** Run a checkpoint operation on WAL database zDb attached to database
! 7257: ** handle db. The specific operation is determined by the value of the
! 7258: ** eMode parameter:
! 7259: **
! 7260: ** <dl>
! 7261: ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
! 7262: ** Checkpoint as many frames as possible without waiting for any database
! 7263: ** readers or writers to finish. Sync the db file if all frames in the log
! 7264: ** are checkpointed. This mode is the same as calling
! 7265: ** sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
! 7266: **
! 7267: ** <dt>SQLITE_CHECKPOINT_FULL<dd>
! 7268: ** This mode blocks (calls the busy-handler callback) until there is no
! 7269: ** database writer and all readers are reading from the most recent database
! 7270: ** snapshot. It then checkpoints all frames in the log file and syncs the
! 7271: ** database file. This call blocks database writers while it is running,
! 7272: ** but not database readers.
! 7273: **
! 7274: ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
! 7275: ** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
! 7276: ** checkpointing the log file it blocks (calls the busy-handler callback)
! 7277: ** until all readers are reading from the database file only. This ensures
! 7278: ** that the next client to write to the database file restarts the log file
! 7279: ** from the beginning. This call blocks database writers while it is running,
! 7280: ** but not database readers.
! 7281: ** </dl>
! 7282: **
! 7283: ** If pnLog is not NULL, then *pnLog is set to the total number of frames in
! 7284: ** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
! 7285: ** the total number of checkpointed frames (including any that were already
! 7286: ** checkpointed when this function is called). *pnLog and *pnCkpt may be
! 7287: ** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
! 7288: ** If no values are available because of an error, they are both set to -1
! 7289: ** before returning to communicate this to the caller.
! 7290: **
! 7291: ** All calls obtain an exclusive "checkpoint" lock on the database file. If
! 7292: ** any other process is running a checkpoint operation at the same time, the
! 7293: ** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a
! 7294: ** busy-handler configured, it will not be invoked in this case.
! 7295: **
! 7296: ** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive
! 7297: ** "writer" lock on the database file. If the writer lock cannot be obtained
! 7298: ** immediately, and a busy-handler is configured, it is invoked and the writer
! 7299: ** lock retried until either the busy-handler returns 0 or the lock is
! 7300: ** successfully obtained. The busy-handler is also invoked while waiting for
! 7301: ** database readers as described above. If the busy-handler returns 0 before
! 7302: ** the writer lock is obtained or while waiting for database readers, the
! 7303: ** checkpoint operation proceeds from that point in the same way as
! 7304: ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
! 7305: ** without blocking any further. SQLITE_BUSY is returned in this case.
! 7306: **
! 7307: ** If parameter zDb is NULL or points to a zero length string, then the
! 7308: ** specified operation is attempted on all WAL databases. In this case the
! 7309: ** values written to output parameters *pnLog and *pnCkpt are undefined. If
! 7310: ** an SQLITE_BUSY error is encountered when processing one or more of the
! 7311: ** attached WAL databases, the operation is still attempted on any remaining
! 7312: ** attached databases and SQLITE_BUSY is returned to the caller. If any other
! 7313: ** error occurs while processing an attached database, processing is abandoned
! 7314: ** and the error code returned to the caller immediately. If no error
! 7315: ** (SQLITE_BUSY or otherwise) is encountered while processing the attached
! 7316: ** databases, SQLITE_OK is returned.
! 7317: **
! 7318: ** If database zDb is the name of an attached database that is not in WAL
! 7319: ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
! 7320: ** zDb is not NULL (or a zero length string) and is not the name of any
! 7321: ** attached database, SQLITE_ERROR is returned to the caller.
! 7322: */
! 7323: SQLITE_API int sqlite3_wal_checkpoint_v2(
! 7324: sqlite3 *db, /* Database handle */
! 7325: const char *zDb, /* Name of attached database (or NULL) */
! 7326: int eMode, /* SQLITE_CHECKPOINT_* value */
! 7327: int *pnLog, /* OUT: Size of WAL log in frames */
! 7328: int *pnCkpt /* OUT: Total number of frames checkpointed */
! 7329: );
! 7330:
! 7331: /*
! 7332: ** CAPI3REF: Checkpoint operation parameters
! 7333: **
! 7334: ** These constants can be used as the 3rd parameter to
! 7335: ** [sqlite3_wal_checkpoint_v2()]. See the [sqlite3_wal_checkpoint_v2()]
! 7336: ** documentation for additional information about the meaning and use of
! 7337: ** each of these values.
! 7338: */
! 7339: #define SQLITE_CHECKPOINT_PASSIVE 0
! 7340: #define SQLITE_CHECKPOINT_FULL 1
! 7341: #define SQLITE_CHECKPOINT_RESTART 2
! 7342:
! 7343: /*
! 7344: ** CAPI3REF: Virtual Table Interface Configuration
! 7345: **
! 7346: ** This function may be called by either the [xConnect] or [xCreate] method
! 7347: ** of a [virtual table] implementation to configure
! 7348: ** various facets of the virtual table interface.
! 7349: **
! 7350: ** If this interface is invoked outside the context of an xConnect or
! 7351: ** xCreate virtual table method then the behavior is undefined.
! 7352: **
! 7353: ** At present, there is only one option that may be configured using
! 7354: ** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].) Further options
! 7355: ** may be added in the future.
! 7356: */
! 7357: SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
! 7358:
! 7359: /*
! 7360: ** CAPI3REF: Virtual Table Configuration Options
! 7361: **
! 7362: ** These macros define the various options to the
! 7363: ** [sqlite3_vtab_config()] interface that [virtual table] implementations
! 7364: ** can use to customize and optimize their behavior.
! 7365: **
! 7366: ** <dl>
! 7367: ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
! 7368: ** <dd>Calls of the form
! 7369: ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
! 7370: ** where X is an integer. If X is zero, then the [virtual table] whose
! 7371: ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
! 7372: ** support constraints. In this configuration (which is the default) if
! 7373: ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
! 7374: ** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
! 7375: ** specified as part of the users SQL statement, regardless of the actual
! 7376: ** ON CONFLICT mode specified.
! 7377: **
! 7378: ** If X is non-zero, then the virtual table implementation guarantees
! 7379: ** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
! 7380: ** any modifications to internal or persistent data structures have been made.
! 7381: ** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
! 7382: ** is able to roll back a statement or database transaction, and abandon
! 7383: ** or continue processing the current SQL statement as appropriate.
! 7384: ** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
! 7385: ** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
! 7386: ** had been ABORT.
! 7387: **
! 7388: ** Virtual table implementations that are required to handle OR REPLACE
! 7389: ** must do so within the [xUpdate] method. If a call to the
! 7390: ** [sqlite3_vtab_on_conflict()] function indicates that the current ON
! 7391: ** CONFLICT policy is REPLACE, the virtual table implementation should
! 7392: ** silently replace the appropriate rows within the xUpdate callback and
! 7393: ** return SQLITE_OK. Or, if this is not possible, it may return
! 7394: ** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
! 7395: ** constraint handling.
! 7396: ** </dl>
! 7397: */
! 7398: #define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
! 7399:
! 7400: /*
! 7401: ** CAPI3REF: Determine The Virtual Table Conflict Policy
! 7402: **
! 7403: ** This function may only be called from within a call to the [xUpdate] method
! 7404: ** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
! 7405: ** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
! 7406: ** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
! 7407: ** of the SQL statement that triggered the call to the [xUpdate] method of the
! 7408: ** [virtual table].
! 7409: */
! 7410: SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
! 7411:
! 7412: /*
! 7413: ** CAPI3REF: Conflict resolution modes
! 7414: **
! 7415: ** These constants are returned by [sqlite3_vtab_on_conflict()] to
! 7416: ** inform a [virtual table] implementation what the [ON CONFLICT] mode
! 7417: ** is for the SQL statement being evaluated.
! 7418: **
! 7419: ** Note that the [SQLITE_IGNORE] constant is also used as a potential
! 7420: ** return value from the [sqlite3_set_authorizer()] callback and that
! 7421: ** [SQLITE_ABORT] is also a [result code].
! 7422: */
! 7423: #define SQLITE_ROLLBACK 1
! 7424: /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
! 7425: #define SQLITE_FAIL 3
! 7426: /* #define SQLITE_ABORT 4 // Also an error code */
! 7427: #define SQLITE_REPLACE 5
! 7428:
! 7429:
! 7430:
! 7431: /*
! 7432: ** Undo the hack that converts floating point types to integer for
! 7433: ** builds on processors without floating point support.
! 7434: */
! 7435: #ifdef SQLITE_OMIT_FLOATING_POINT
! 7436: # undef double
! 7437: #endif
! 7438:
! 7439: #if 0
! 7440: } /* End of the 'extern "C"' block */
! 7441: #endif
! 7442: #endif
! 7443:
! 7444: /*
! 7445: ** 2010 August 30
! 7446: **
! 7447: ** The author disclaims copyright to this source code. In place of
! 7448: ** a legal notice, here is a blessing:
! 7449: **
! 7450: ** May you do good and not evil.
! 7451: ** May you find forgiveness for yourself and forgive others.
! 7452: ** May you share freely, never taking more than you give.
! 7453: **
! 7454: *************************************************************************
! 7455: */
! 7456:
! 7457: #ifndef _SQLITE3RTREE_H_
! 7458: #define _SQLITE3RTREE_H_
! 7459:
! 7460:
! 7461: #if 0
! 7462: extern "C" {
! 7463: #endif
! 7464:
! 7465: typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
! 7466:
! 7467: /*
! 7468: ** Register a geometry callback named zGeom that can be used as part of an
! 7469: ** R-Tree geometry query as follows:
! 7470: **
! 7471: ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
! 7472: */
! 7473: SQLITE_API int sqlite3_rtree_geometry_callback(
! 7474: sqlite3 *db,
! 7475: const char *zGeom,
! 7476: int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
! 7477: void *pContext
! 7478: );
! 7479:
! 7480:
! 7481: /*
! 7482: ** A pointer to a structure of the following type is passed as the first
! 7483: ** argument to callbacks registered using rtree_geometry_callback().
! 7484: */
! 7485: struct sqlite3_rtree_geometry {
! 7486: void *pContext; /* Copy of pContext passed to s_r_g_c() */
! 7487: int nParam; /* Size of array aParam[] */
! 7488: double *aParam; /* Parameters passed to SQL geom function */
! 7489: void *pUser; /* Callback implementation user data */
! 7490: void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
! 7491: };
! 7492:
! 7493:
! 7494: #if 0
! 7495: } /* end of the 'extern "C"' block */
! 7496: #endif
! 7497:
! 7498: #endif /* ifndef _SQLITE3RTREE_H_ */
! 7499:
! 7500:
! 7501: /************** End of sqlite3.h *********************************************/
! 7502: /************** Continuing where we left off in sqliteInt.h ******************/
! 7503: /************** Include hash.h in the middle of sqliteInt.h ******************/
! 7504: /************** Begin file hash.h ********************************************/
! 7505: /*
! 7506: ** 2001 September 22
! 7507: **
! 7508: ** The author disclaims copyright to this source code. In place of
! 7509: ** a legal notice, here is a blessing:
! 7510: **
! 7511: ** May you do good and not evil.
! 7512: ** May you find forgiveness for yourself and forgive others.
! 7513: ** May you share freely, never taking more than you give.
! 7514: **
! 7515: *************************************************************************
! 7516: ** This is the header file for the generic hash-table implemenation
! 7517: ** used in SQLite.
! 7518: */
! 7519: #ifndef _SQLITE_HASH_H_
! 7520: #define _SQLITE_HASH_H_
! 7521:
! 7522: /* Forward declarations of structures. */
! 7523: typedef struct Hash Hash;
! 7524: typedef struct HashElem HashElem;
! 7525:
! 7526: /* A complete hash table is an instance of the following structure.
! 7527: ** The internals of this structure are intended to be opaque -- client
! 7528: ** code should not attempt to access or modify the fields of this structure
! 7529: ** directly. Change this structure only by using the routines below.
! 7530: ** However, some of the "procedures" and "functions" for modifying and
! 7531: ** accessing this structure are really macros, so we can't really make
! 7532: ** this structure opaque.
! 7533: **
! 7534: ** All elements of the hash table are on a single doubly-linked list.
! 7535: ** Hash.first points to the head of this list.
! 7536: **
! 7537: ** There are Hash.htsize buckets. Each bucket points to a spot in
! 7538: ** the global doubly-linked list. The contents of the bucket are the
! 7539: ** element pointed to plus the next _ht.count-1 elements in the list.
! 7540: **
! 7541: ** Hash.htsize and Hash.ht may be zero. In that case lookup is done
! 7542: ** by a linear search of the global list. For small tables, the
! 7543: ** Hash.ht table is never allocated because if there are few elements
! 7544: ** in the table, it is faster to do a linear search than to manage
! 7545: ** the hash table.
! 7546: */
! 7547: struct Hash {
! 7548: unsigned int htsize; /* Number of buckets in the hash table */
! 7549: unsigned int count; /* Number of entries in this table */
! 7550: HashElem *first; /* The first element of the array */
! 7551: struct _ht { /* the hash table */
! 7552: int count; /* Number of entries with this hash */
! 7553: HashElem *chain; /* Pointer to first entry with this hash */
! 7554: } *ht;
! 7555: };
! 7556:
! 7557: /* Each element in the hash table is an instance of the following
! 7558: ** structure. All elements are stored on a single doubly-linked list.
! 7559: **
! 7560: ** Again, this structure is intended to be opaque, but it can't really
! 7561: ** be opaque because it is used by macros.
! 7562: */
! 7563: struct HashElem {
! 7564: HashElem *next, *prev; /* Next and previous elements in the table */
! 7565: void *data; /* Data associated with this element */
! 7566: const char *pKey; int nKey; /* Key associated with this element */
! 7567: };
! 7568:
! 7569: /*
! 7570: ** Access routines. To delete, insert a NULL pointer.
! 7571: */
! 7572: SQLITE_PRIVATE void sqlite3HashInit(Hash*);
! 7573: SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
! 7574: SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
! 7575: SQLITE_PRIVATE void sqlite3HashClear(Hash*);
! 7576:
! 7577: /*
! 7578: ** Macros for looping over all elements of a hash table. The idiom is
! 7579: ** like this:
! 7580: **
! 7581: ** Hash h;
! 7582: ** HashElem *p;
! 7583: ** ...
! 7584: ** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
! 7585: ** SomeStructure *pData = sqliteHashData(p);
! 7586: ** // do something with pData
! 7587: ** }
! 7588: */
! 7589: #define sqliteHashFirst(H) ((H)->first)
! 7590: #define sqliteHashNext(E) ((E)->next)
! 7591: #define sqliteHashData(E) ((E)->data)
! 7592: /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
! 7593: /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
! 7594:
! 7595: /*
! 7596: ** Number of entries in a hash table
! 7597: */
! 7598: /* #define sqliteHashCount(H) ((H)->count) // NOT USED */
! 7599:
! 7600: #endif /* _SQLITE_HASH_H_ */
! 7601:
! 7602: /************** End of hash.h ************************************************/
! 7603: /************** Continuing where we left off in sqliteInt.h ******************/
! 7604: /************** Include parse.h in the middle of sqliteInt.h *****************/
! 7605: /************** Begin file parse.h *******************************************/
! 7606: #define TK_SEMI 1
! 7607: #define TK_EXPLAIN 2
! 7608: #define TK_QUERY 3
! 7609: #define TK_PLAN 4
! 7610: #define TK_BEGIN 5
! 7611: #define TK_TRANSACTION 6
! 7612: #define TK_DEFERRED 7
! 7613: #define TK_IMMEDIATE 8
! 7614: #define TK_EXCLUSIVE 9
! 7615: #define TK_COMMIT 10
! 7616: #define TK_END 11
! 7617: #define TK_ROLLBACK 12
! 7618: #define TK_SAVEPOINT 13
! 7619: #define TK_RELEASE 14
! 7620: #define TK_TO 15
! 7621: #define TK_TABLE 16
! 7622: #define TK_CREATE 17
! 7623: #define TK_IF 18
! 7624: #define TK_NOT 19
! 7625: #define TK_EXISTS 20
! 7626: #define TK_TEMP 21
! 7627: #define TK_LP 22
! 7628: #define TK_RP 23
! 7629: #define TK_AS 24
! 7630: #define TK_COMMA 25
! 7631: #define TK_ID 26
! 7632: #define TK_INDEXED 27
! 7633: #define TK_ABORT 28
! 7634: #define TK_ACTION 29
! 7635: #define TK_AFTER 30
! 7636: #define TK_ANALYZE 31
! 7637: #define TK_ASC 32
! 7638: #define TK_ATTACH 33
! 7639: #define TK_BEFORE 34
! 7640: #define TK_BY 35
! 7641: #define TK_CASCADE 36
! 7642: #define TK_CAST 37
! 7643: #define TK_COLUMNKW 38
! 7644: #define TK_CONFLICT 39
! 7645: #define TK_DATABASE 40
! 7646: #define TK_DESC 41
! 7647: #define TK_DETACH 42
! 7648: #define TK_EACH 43
! 7649: #define TK_FAIL 44
! 7650: #define TK_FOR 45
! 7651: #define TK_IGNORE 46
! 7652: #define TK_INITIALLY 47
! 7653: #define TK_INSTEAD 48
! 7654: #define TK_LIKE_KW 49
! 7655: #define TK_MATCH 50
! 7656: #define TK_NO 51
! 7657: #define TK_KEY 52
! 7658: #define TK_OF 53
! 7659: #define TK_OFFSET 54
! 7660: #define TK_PRAGMA 55
! 7661: #define TK_RAISE 56
! 7662: #define TK_REPLACE 57
! 7663: #define TK_RESTRICT 58
! 7664: #define TK_ROW 59
! 7665: #define TK_TRIGGER 60
! 7666: #define TK_VACUUM 61
! 7667: #define TK_VIEW 62
! 7668: #define TK_VIRTUAL 63
! 7669: #define TK_REINDEX 64
! 7670: #define TK_RENAME 65
! 7671: #define TK_CTIME_KW 66
! 7672: #define TK_ANY 67
! 7673: #define TK_OR 68
! 7674: #define TK_AND 69
! 7675: #define TK_IS 70
! 7676: #define TK_BETWEEN 71
! 7677: #define TK_IN 72
! 7678: #define TK_ISNULL 73
! 7679: #define TK_NOTNULL 74
! 7680: #define TK_NE 75
! 7681: #define TK_EQ 76
! 7682: #define TK_GT 77
! 7683: #define TK_LE 78
! 7684: #define TK_LT 79
! 7685: #define TK_GE 80
! 7686: #define TK_ESCAPE 81
! 7687: #define TK_BITAND 82
! 7688: #define TK_BITOR 83
! 7689: #define TK_LSHIFT 84
! 7690: #define TK_RSHIFT 85
! 7691: #define TK_PLUS 86
! 7692: #define TK_MINUS 87
! 7693: #define TK_STAR 88
! 7694: #define TK_SLASH 89
! 7695: #define TK_REM 90
! 7696: #define TK_CONCAT 91
! 7697: #define TK_COLLATE 92
! 7698: #define TK_BITNOT 93
! 7699: #define TK_STRING 94
! 7700: #define TK_JOIN_KW 95
! 7701: #define TK_CONSTRAINT 96
! 7702: #define TK_DEFAULT 97
! 7703: #define TK_NULL 98
! 7704: #define TK_PRIMARY 99
! 7705: #define TK_UNIQUE 100
! 7706: #define TK_CHECK 101
! 7707: #define TK_REFERENCES 102
! 7708: #define TK_AUTOINCR 103
! 7709: #define TK_ON 104
! 7710: #define TK_INSERT 105
! 7711: #define TK_DELETE 106
! 7712: #define TK_UPDATE 107
! 7713: #define TK_SET 108
! 7714: #define TK_DEFERRABLE 109
! 7715: #define TK_FOREIGN 110
! 7716: #define TK_DROP 111
! 7717: #define TK_UNION 112
! 7718: #define TK_ALL 113
! 7719: #define TK_EXCEPT 114
! 7720: #define TK_INTERSECT 115
! 7721: #define TK_SELECT 116
! 7722: #define TK_DISTINCT 117
! 7723: #define TK_DOT 118
! 7724: #define TK_FROM 119
! 7725: #define TK_JOIN 120
! 7726: #define TK_USING 121
! 7727: #define TK_ORDER 122
! 7728: #define TK_GROUP 123
! 7729: #define TK_HAVING 124
! 7730: #define TK_LIMIT 125
! 7731: #define TK_WHERE 126
! 7732: #define TK_INTO 127
! 7733: #define TK_VALUES 128
! 7734: #define TK_INTEGER 129
! 7735: #define TK_FLOAT 130
! 7736: #define TK_BLOB 131
! 7737: #define TK_REGISTER 132
! 7738: #define TK_VARIABLE 133
! 7739: #define TK_CASE 134
! 7740: #define TK_WHEN 135
! 7741: #define TK_THEN 136
! 7742: #define TK_ELSE 137
! 7743: #define TK_INDEX 138
! 7744: #define TK_ALTER 139
! 7745: #define TK_ADD 140
! 7746: #define TK_TO_TEXT 141
! 7747: #define TK_TO_BLOB 142
! 7748: #define TK_TO_NUMERIC 143
! 7749: #define TK_TO_INT 144
! 7750: #define TK_TO_REAL 145
! 7751: #define TK_ISNOT 146
! 7752: #define TK_END_OF_FILE 147
! 7753: #define TK_ILLEGAL 148
! 7754: #define TK_SPACE 149
! 7755: #define TK_UNCLOSED_STRING 150
! 7756: #define TK_FUNCTION 151
! 7757: #define TK_COLUMN 152
! 7758: #define TK_AGG_FUNCTION 153
! 7759: #define TK_AGG_COLUMN 154
! 7760: #define TK_CONST_FUNC 155
! 7761: #define TK_UMINUS 156
! 7762: #define TK_UPLUS 157
! 7763:
! 7764: /************** End of parse.h ***********************************************/
! 7765: /************** Continuing where we left off in sqliteInt.h ******************/
! 7766: #include <stdio.h>
! 7767: #include <stdlib.h>
! 7768: #include <string.h>
! 7769: #include <assert.h>
! 7770: #include <stddef.h>
! 7771:
! 7772: /*
! 7773: ** If compiling for a processor that lacks floating point support,
! 7774: ** substitute integer for floating-point
! 7775: */
! 7776: #ifdef SQLITE_OMIT_FLOATING_POINT
! 7777: # define double sqlite_int64
! 7778: # define float sqlite_int64
! 7779: # define LONGDOUBLE_TYPE sqlite_int64
! 7780: # ifndef SQLITE_BIG_DBL
! 7781: # define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
! 7782: # endif
! 7783: # define SQLITE_OMIT_DATETIME_FUNCS 1
! 7784: # define SQLITE_OMIT_TRACE 1
! 7785: # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
! 7786: # undef SQLITE_HAVE_ISNAN
! 7787: #else
! 7788: # ifdef __vax__
! 7789: # include <float.h>
! 7790: # define SQLITE_BIG_DBL DBL_MAX
! 7791: # define SQLITE_HUGE_DBL DBL_MAX
! 7792: # endif
! 7793: #endif
! 7794: #ifndef SQLITE_BIG_DBL
! 7795: # define SQLITE_BIG_DBL (1e99)
! 7796: #endif
! 7797: #ifndef SQLITE_HUGE_DBL
! 7798: # define SQLITE_HUGE_DBL (1.0e+308)
! 7799: #endif
! 7800:
! 7801: /*
! 7802: ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
! 7803: ** afterward. Having this macro allows us to cause the C compiler
! 7804: ** to omit code used by TEMP tables without messy #ifndef statements.
! 7805: */
! 7806: #ifdef SQLITE_OMIT_TEMPDB
! 7807: #define OMIT_TEMPDB 1
! 7808: #else
! 7809: #define OMIT_TEMPDB 0
! 7810: #endif
! 7811:
! 7812: /*
! 7813: ** The "file format" number is an integer that is incremented whenever
! 7814: ** the VDBE-level file format changes. The following macros define the
! 7815: ** the default file format for new databases and the maximum file format
! 7816: ** that the library can read.
! 7817: */
! 7818: #define SQLITE_MAX_FILE_FORMAT 4
! 7819: #ifndef SQLITE_DEFAULT_FILE_FORMAT
! 7820: # define SQLITE_DEFAULT_FILE_FORMAT 4
! 7821: #endif
! 7822:
! 7823: /*
! 7824: ** Determine whether triggers are recursive by default. This can be
! 7825: ** changed at run-time using a pragma.
! 7826: */
! 7827: #ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
! 7828: # define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
! 7829: #endif
! 7830:
! 7831: /*
! 7832: ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
! 7833: ** on the command-line
! 7834: */
! 7835: #ifndef SQLITE_TEMP_STORE
! 7836: # define SQLITE_TEMP_STORE 1
! 7837: #endif
! 7838:
! 7839: /*
! 7840: ** GCC does not define the offsetof() macro so we'll have to do it
! 7841: ** ourselves.
! 7842: */
! 7843: #ifndef offsetof
! 7844: #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
! 7845: #endif
! 7846:
! 7847: /*
! 7848: ** Check to see if this machine uses EBCDIC. (Yes, believe it or
! 7849: ** not, there are still machines out there that use EBCDIC.)
! 7850: */
! 7851: #if 'A' == '\301'
! 7852: # define SQLITE_EBCDIC 1
! 7853: #else
! 7854: # define SQLITE_ASCII 1
! 7855: #endif
! 7856:
! 7857: /*
! 7858: ** Integers of known sizes. These typedefs might change for architectures
! 7859: ** where the sizes very. Preprocessor macros are available so that the
! 7860: ** types can be conveniently redefined at compile-type. Like this:
! 7861: **
! 7862: ** cc '-DUINTPTR_TYPE=long long int' ...
! 7863: */
! 7864: #ifndef UINT32_TYPE
! 7865: # ifdef HAVE_UINT32_T
! 7866: # define UINT32_TYPE uint32_t
! 7867: # else
! 7868: # define UINT32_TYPE unsigned int
! 7869: # endif
! 7870: #endif
! 7871: #ifndef UINT16_TYPE
! 7872: # ifdef HAVE_UINT16_T
! 7873: # define UINT16_TYPE uint16_t
! 7874: # else
! 7875: # define UINT16_TYPE unsigned short int
! 7876: # endif
! 7877: #endif
! 7878: #ifndef INT16_TYPE
! 7879: # ifdef HAVE_INT16_T
! 7880: # define INT16_TYPE int16_t
! 7881: # else
! 7882: # define INT16_TYPE short int
! 7883: # endif
! 7884: #endif
! 7885: #ifndef UINT8_TYPE
! 7886: # ifdef HAVE_UINT8_T
! 7887: # define UINT8_TYPE uint8_t
! 7888: # else
! 7889: # define UINT8_TYPE unsigned char
! 7890: # endif
! 7891: #endif
! 7892: #ifndef INT8_TYPE
! 7893: # ifdef HAVE_INT8_T
! 7894: # define INT8_TYPE int8_t
! 7895: # else
! 7896: # define INT8_TYPE signed char
! 7897: # endif
! 7898: #endif
! 7899: #ifndef LONGDOUBLE_TYPE
! 7900: # define LONGDOUBLE_TYPE long double
! 7901: #endif
! 7902: typedef sqlite_int64 i64; /* 8-byte signed integer */
! 7903: typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
! 7904: typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
! 7905: typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
! 7906: typedef INT16_TYPE i16; /* 2-byte signed integer */
! 7907: typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
! 7908: typedef INT8_TYPE i8; /* 1-byte signed integer */
! 7909:
! 7910: /*
! 7911: ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
! 7912: ** that can be stored in a u32 without loss of data. The value
! 7913: ** is 0x00000000ffffffff. But because of quirks of some compilers, we
! 7914: ** have to specify the value in the less intuitive manner shown:
! 7915: */
! 7916: #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
! 7917:
! 7918: /*
! 7919: ** The datatype used to store estimates of the number of rows in a
! 7920: ** table or index. This is an unsigned integer type. For 99.9% of
! 7921: ** the world, a 32-bit integer is sufficient. But a 64-bit integer
! 7922: ** can be used at compile-time if desired.
! 7923: */
! 7924: #ifdef SQLITE_64BIT_STATS
! 7925: typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
! 7926: #else
! 7927: typedef u32 tRowcnt; /* 32-bit is the default */
! 7928: #endif
! 7929:
! 7930: /*
! 7931: ** Macros to determine whether the machine is big or little endian,
! 7932: ** evaluated at runtime.
! 7933: */
! 7934: #ifdef SQLITE_AMALGAMATION
! 7935: SQLITE_PRIVATE const int sqlite3one = 1;
! 7936: #else
! 7937: SQLITE_PRIVATE const int sqlite3one;
! 7938: #endif
! 7939: #if defined(i386) || defined(__i386__) || defined(_M_IX86)\
! 7940: || defined(__x86_64) || defined(__x86_64__)
! 7941: # define SQLITE_BIGENDIAN 0
! 7942: # define SQLITE_LITTLEENDIAN 1
! 7943: # define SQLITE_UTF16NATIVE SQLITE_UTF16LE
! 7944: #else
! 7945: # define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
! 7946: # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
! 7947: # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
! 7948: #endif
! 7949:
! 7950: /*
! 7951: ** Constants for the largest and smallest possible 64-bit signed integers.
! 7952: ** These macros are designed to work correctly on both 32-bit and 64-bit
! 7953: ** compilers.
! 7954: */
! 7955: #define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
! 7956: #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
! 7957:
! 7958: /*
! 7959: ** Round up a number to the next larger multiple of 8. This is used
! 7960: ** to force 8-byte alignment on 64-bit architectures.
! 7961: */
! 7962: #define ROUND8(x) (((x)+7)&~7)
! 7963:
! 7964: /*
! 7965: ** Round down to the nearest multiple of 8
! 7966: */
! 7967: #define ROUNDDOWN8(x) ((x)&~7)
! 7968:
! 7969: /*
! 7970: ** Assert that the pointer X is aligned to an 8-byte boundary. This
! 7971: ** macro is used only within assert() to verify that the code gets
! 7972: ** all alignment restrictions correct.
! 7973: **
! 7974: ** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
! 7975: ** underlying malloc() implemention might return us 4-byte aligned
! 7976: ** pointers. In that case, only verify 4-byte alignment.
! 7977: */
! 7978: #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
! 7979: # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
! 7980: #else
! 7981: # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
! 7982: #endif
! 7983:
! 7984:
! 7985: /*
! 7986: ** An instance of the following structure is used to store the busy-handler
! 7987: ** callback for a given sqlite handle.
! 7988: **
! 7989: ** The sqlite.busyHandler member of the sqlite struct contains the busy
! 7990: ** callback for the database handle. Each pager opened via the sqlite
! 7991: ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
! 7992: ** callback is currently invoked only from within pager.c.
! 7993: */
! 7994: typedef struct BusyHandler BusyHandler;
! 7995: struct BusyHandler {
! 7996: int (*xFunc)(void *,int); /* The busy callback */
! 7997: void *pArg; /* First arg to busy callback */
! 7998: int nBusy; /* Incremented with each busy call */
! 7999: };
! 8000:
! 8001: /*
! 8002: ** Name of the master database table. The master database table
! 8003: ** is a special table that holds the names and attributes of all
! 8004: ** user tables and indices.
! 8005: */
! 8006: #define MASTER_NAME "sqlite_master"
! 8007: #define TEMP_MASTER_NAME "sqlite_temp_master"
! 8008:
! 8009: /*
! 8010: ** The root-page of the master database table.
! 8011: */
! 8012: #define MASTER_ROOT 1
! 8013:
! 8014: /*
! 8015: ** The name of the schema table.
! 8016: */
! 8017: #define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
! 8018:
! 8019: /*
! 8020: ** A convenience macro that returns the number of elements in
! 8021: ** an array.
! 8022: */
! 8023: #define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
! 8024:
! 8025: /*
! 8026: ** The following value as a destructor means to use sqlite3DbFree().
! 8027: ** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
! 8028: */
! 8029: #define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3DbFree)
! 8030:
! 8031: /*
! 8032: ** When SQLITE_OMIT_WSD is defined, it means that the target platform does
! 8033: ** not support Writable Static Data (WSD) such as global and static variables.
! 8034: ** All variables must either be on the stack or dynamically allocated from
! 8035: ** the heap. When WSD is unsupported, the variable declarations scattered
! 8036: ** throughout the SQLite code must become constants instead. The SQLITE_WSD
! 8037: ** macro is used for this purpose. And instead of referencing the variable
! 8038: ** directly, we use its constant as a key to lookup the run-time allocated
! 8039: ** buffer that holds real variable. The constant is also the initializer
! 8040: ** for the run-time allocated buffer.
! 8041: **
! 8042: ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
! 8043: ** macros become no-ops and have zero performance impact.
! 8044: */
! 8045: #ifdef SQLITE_OMIT_WSD
! 8046: #define SQLITE_WSD const
! 8047: #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
! 8048: #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
! 8049: SQLITE_API int sqlite3_wsd_init(int N, int J);
! 8050: SQLITE_API void *sqlite3_wsd_find(void *K, int L);
! 8051: #else
! 8052: #define SQLITE_WSD
! 8053: #define GLOBAL(t,v) v
! 8054: #define sqlite3GlobalConfig sqlite3Config
! 8055: #endif
! 8056:
! 8057: /*
! 8058: ** The following macros are used to suppress compiler warnings and to
! 8059: ** make it clear to human readers when a function parameter is deliberately
! 8060: ** left unused within the body of a function. This usually happens when
! 8061: ** a function is called via a function pointer. For example the
! 8062: ** implementation of an SQL aggregate step callback may not use the
! 8063: ** parameter indicating the number of arguments passed to the aggregate,
! 8064: ** if it knows that this is enforced elsewhere.
! 8065: **
! 8066: ** When a function parameter is not used at all within the body of a function,
! 8067: ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
! 8068: ** However, these macros may also be used to suppress warnings related to
! 8069: ** parameters that may or may not be used depending on compilation options.
! 8070: ** For example those parameters only used in assert() statements. In these
! 8071: ** cases the parameters are named as per the usual conventions.
! 8072: */
! 8073: #define UNUSED_PARAMETER(x) (void)(x)
! 8074: #define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
! 8075:
! 8076: /*
! 8077: ** Forward references to structures
! 8078: */
! 8079: typedef struct AggInfo AggInfo;
! 8080: typedef struct AuthContext AuthContext;
! 8081: typedef struct AutoincInfo AutoincInfo;
! 8082: typedef struct Bitvec Bitvec;
! 8083: typedef struct CollSeq CollSeq;
! 8084: typedef struct Column Column;
! 8085: typedef struct Db Db;
! 8086: typedef struct Schema Schema;
! 8087: typedef struct Expr Expr;
! 8088: typedef struct ExprList ExprList;
! 8089: typedef struct ExprSpan ExprSpan;
! 8090: typedef struct FKey FKey;
! 8091: typedef struct FuncDestructor FuncDestructor;
! 8092: typedef struct FuncDef FuncDef;
! 8093: typedef struct FuncDefHash FuncDefHash;
! 8094: typedef struct IdList IdList;
! 8095: typedef struct Index Index;
! 8096: typedef struct IndexSample IndexSample;
! 8097: typedef struct KeyClass KeyClass;
! 8098: typedef struct KeyInfo KeyInfo;
! 8099: typedef struct Lookaside Lookaside;
! 8100: typedef struct LookasideSlot LookasideSlot;
! 8101: typedef struct Module Module;
! 8102: typedef struct NameContext NameContext;
! 8103: typedef struct Parse Parse;
! 8104: typedef struct RowSet RowSet;
! 8105: typedef struct Savepoint Savepoint;
! 8106: typedef struct Select Select;
! 8107: typedef struct SrcList SrcList;
! 8108: typedef struct StrAccum StrAccum;
! 8109: typedef struct Table Table;
! 8110: typedef struct TableLock TableLock;
! 8111: typedef struct Token Token;
! 8112: typedef struct Trigger Trigger;
! 8113: typedef struct TriggerPrg TriggerPrg;
! 8114: typedef struct TriggerStep TriggerStep;
! 8115: typedef struct UnpackedRecord UnpackedRecord;
! 8116: typedef struct VTable VTable;
! 8117: typedef struct VtabCtx VtabCtx;
! 8118: typedef struct Walker Walker;
! 8119: typedef struct WherePlan WherePlan;
! 8120: typedef struct WhereInfo WhereInfo;
! 8121: typedef struct WhereLevel WhereLevel;
! 8122:
! 8123: /*
! 8124: ** Defer sourcing vdbe.h and btree.h until after the "u8" and
! 8125: ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
! 8126: ** pointer types (i.e. FuncDef) defined above.
! 8127: */
! 8128: /************** Include btree.h in the middle of sqliteInt.h *****************/
! 8129: /************** Begin file btree.h *******************************************/
! 8130: /*
! 8131: ** 2001 September 15
! 8132: **
! 8133: ** The author disclaims copyright to this source code. In place of
! 8134: ** a legal notice, here is a blessing:
! 8135: **
! 8136: ** May you do good and not evil.
! 8137: ** May you find forgiveness for yourself and forgive others.
! 8138: ** May you share freely, never taking more than you give.
! 8139: **
! 8140: *************************************************************************
! 8141: ** This header file defines the interface that the sqlite B-Tree file
! 8142: ** subsystem. See comments in the source code for a detailed description
! 8143: ** of what each interface routine does.
! 8144: */
! 8145: #ifndef _BTREE_H_
! 8146: #define _BTREE_H_
! 8147:
! 8148: /* TODO: This definition is just included so other modules compile. It
! 8149: ** needs to be revisited.
! 8150: */
! 8151: #define SQLITE_N_BTREE_META 10
! 8152:
! 8153: /*
! 8154: ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
! 8155: ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
! 8156: */
! 8157: #ifndef SQLITE_DEFAULT_AUTOVACUUM
! 8158: #define SQLITE_DEFAULT_AUTOVACUUM 0
! 8159: #endif
! 8160:
! 8161: #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
! 8162: #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
! 8163: #define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */
! 8164:
! 8165: /*
! 8166: ** Forward declarations of structure
! 8167: */
! 8168: typedef struct Btree Btree;
! 8169: typedef struct BtCursor BtCursor;
! 8170: typedef struct BtShared BtShared;
! 8171:
! 8172:
! 8173: SQLITE_PRIVATE int sqlite3BtreeOpen(
! 8174: sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
! 8175: const char *zFilename, /* Name of database file to open */
! 8176: sqlite3 *db, /* Associated database connection */
! 8177: Btree **ppBtree, /* Return open Btree* here */
! 8178: int flags, /* Flags */
! 8179: int vfsFlags /* Flags passed through to VFS open */
! 8180: );
! 8181:
! 8182: /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
! 8183: ** following values.
! 8184: **
! 8185: ** NOTE: These values must match the corresponding PAGER_ values in
! 8186: ** pager.h.
! 8187: */
! 8188: #define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
! 8189: #define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */
! 8190: #define BTREE_MEMORY 4 /* This is an in-memory DB */
! 8191: #define BTREE_SINGLE 8 /* The file contains at most 1 b-tree */
! 8192: #define BTREE_UNORDERED 16 /* Use of a hash implementation is OK */
! 8193:
! 8194: SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
! 8195: SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
! 8196: SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
! 8197: SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
! 8198: SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
! 8199: SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
! 8200: SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
! 8201: SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
! 8202: SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
! 8203: SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
! 8204: SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
! 8205: SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
! 8206: SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
! 8207: SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
! 8208: SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
! 8209: SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
! 8210: SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
! 8211: SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
! 8212: SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
! 8213: SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
! 8214: SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
! 8215: SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
! 8216: SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
! 8217: SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
! 8218: SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
! 8219: SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
! 8220:
! 8221: SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
! 8222: SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
! 8223: SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
! 8224:
! 8225: SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
! 8226:
! 8227: /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
! 8228: ** of the flags shown below.
! 8229: **
! 8230: ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
! 8231: ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
! 8232: ** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
! 8233: ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
! 8234: ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
! 8235: ** indices.)
! 8236: */
! 8237: #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
! 8238: #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
! 8239:
! 8240: SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
! 8241: SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
! 8242: SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
! 8243:
! 8244: SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
! 8245: SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
! 8246:
! 8247: /*
! 8248: ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
! 8249: ** should be one of the following values. The integer values are assigned
! 8250: ** to constants so that the offset of the corresponding field in an
! 8251: ** SQLite database header may be found using the following formula:
! 8252: **
! 8253: ** offset = 36 + (idx * 4)
! 8254: **
! 8255: ** For example, the free-page-count field is located at byte offset 36 of
! 8256: ** the database file header. The incr-vacuum-flag field is located at
! 8257: ** byte offset 64 (== 36+4*7).
! 8258: */
! 8259: #define BTREE_FREE_PAGE_COUNT 0
! 8260: #define BTREE_SCHEMA_VERSION 1
! 8261: #define BTREE_FILE_FORMAT 2
! 8262: #define BTREE_DEFAULT_CACHE_SIZE 3
! 8263: #define BTREE_LARGEST_ROOT_PAGE 4
! 8264: #define BTREE_TEXT_ENCODING 5
! 8265: #define BTREE_USER_VERSION 6
! 8266: #define BTREE_INCR_VACUUM 7
! 8267:
! 8268: SQLITE_PRIVATE int sqlite3BtreeCursor(
! 8269: Btree*, /* BTree containing table to open */
! 8270: int iTable, /* Index of root page */
! 8271: int wrFlag, /* 1 for writing. 0 for read-only */
! 8272: struct KeyInfo*, /* First argument to compare function */
! 8273: BtCursor *pCursor /* Space to write cursor structure */
! 8274: );
! 8275: SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
! 8276: SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
! 8277:
! 8278: SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
! 8279: SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
! 8280: BtCursor*,
! 8281: UnpackedRecord *pUnKey,
! 8282: i64 intKey,
! 8283: int bias,
! 8284: int *pRes
! 8285: );
! 8286: SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
! 8287: SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
! 8288: SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
! 8289: const void *pData, int nData,
! 8290: int nZero, int bias, int seekResult);
! 8291: SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
! 8292: SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
! 8293: SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
! 8294: SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
! 8295: SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
! 8296: SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
! 8297: SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
! 8298: SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
! 8299: SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
! 8300: SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
! 8301: SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
! 8302: SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
! 8303: SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
! 8304:
! 8305: SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
! 8306: SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
! 8307:
! 8308: SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
! 8309: SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
! 8310: SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
! 8311:
! 8312: SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
! 8313:
! 8314: #ifndef NDEBUG
! 8315: SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
! 8316: #endif
! 8317:
! 8318: #ifndef SQLITE_OMIT_BTREECOUNT
! 8319: SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
! 8320: #endif
! 8321:
! 8322: #ifdef SQLITE_TEST
! 8323: SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
! 8324: SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
! 8325: #endif
! 8326:
! 8327: #ifndef SQLITE_OMIT_WAL
! 8328: SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
! 8329: #endif
! 8330:
! 8331: /*
! 8332: ** If we are not using shared cache, then there is no need to
! 8333: ** use mutexes to access the BtShared structures. So make the
! 8334: ** Enter and Leave procedures no-ops.
! 8335: */
! 8336: #ifndef SQLITE_OMIT_SHARED_CACHE
! 8337: SQLITE_PRIVATE void sqlite3BtreeEnter(Btree*);
! 8338: SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3*);
! 8339: #else
! 8340: # define sqlite3BtreeEnter(X)
! 8341: # define sqlite3BtreeEnterAll(X)
! 8342: #endif
! 8343:
! 8344: #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
! 8345: SQLITE_PRIVATE int sqlite3BtreeSharable(Btree*);
! 8346: SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*);
! 8347: SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*);
! 8348: SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*);
! 8349: SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*);
! 8350: #ifndef NDEBUG
! 8351: /* These routines are used inside assert() statements only. */
! 8352: SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*);
! 8353: SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*);
! 8354: SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
! 8355: #endif
! 8356: #else
! 8357:
! 8358: # define sqlite3BtreeSharable(X) 0
! 8359: # define sqlite3BtreeLeave(X)
! 8360: # define sqlite3BtreeEnterCursor(X)
! 8361: # define sqlite3BtreeLeaveCursor(X)
! 8362: # define sqlite3BtreeLeaveAll(X)
! 8363:
! 8364: # define sqlite3BtreeHoldsMutex(X) 1
! 8365: # define sqlite3BtreeHoldsAllMutexes(X) 1
! 8366: # define sqlite3SchemaMutexHeld(X,Y,Z) 1
! 8367: #endif
! 8368:
! 8369:
! 8370: #endif /* _BTREE_H_ */
! 8371:
! 8372: /************** End of btree.h ***********************************************/
! 8373: /************** Continuing where we left off in sqliteInt.h ******************/
! 8374: /************** Include vdbe.h in the middle of sqliteInt.h ******************/
! 8375: /************** Begin file vdbe.h ********************************************/
! 8376: /*
! 8377: ** 2001 September 15
! 8378: **
! 8379: ** The author disclaims copyright to this source code. In place of
! 8380: ** a legal notice, here is a blessing:
! 8381: **
! 8382: ** May you do good and not evil.
! 8383: ** May you find forgiveness for yourself and forgive others.
! 8384: ** May you share freely, never taking more than you give.
! 8385: **
! 8386: *************************************************************************
! 8387: ** Header file for the Virtual DataBase Engine (VDBE)
! 8388: **
! 8389: ** This header defines the interface to the virtual database engine
! 8390: ** or VDBE. The VDBE implements an abstract machine that runs a
! 8391: ** simple program to access and modify the underlying database.
! 8392: */
! 8393: #ifndef _SQLITE_VDBE_H_
! 8394: #define _SQLITE_VDBE_H_
! 8395: /* #include <stdio.h> */
! 8396:
! 8397: /*
! 8398: ** A single VDBE is an opaque structure named "Vdbe". Only routines
! 8399: ** in the source file sqliteVdbe.c are allowed to see the insides
! 8400: ** of this structure.
! 8401: */
! 8402: typedef struct Vdbe Vdbe;
! 8403:
! 8404: /*
! 8405: ** The names of the following types declared in vdbeInt.h are required
! 8406: ** for the VdbeOp definition.
! 8407: */
! 8408: typedef struct VdbeFunc VdbeFunc;
! 8409: typedef struct Mem Mem;
! 8410: typedef struct SubProgram SubProgram;
! 8411:
! 8412: /*
! 8413: ** A single instruction of the virtual machine has an opcode
! 8414: ** and as many as three operands. The instruction is recorded
! 8415: ** as an instance of the following structure:
! 8416: */
! 8417: struct VdbeOp {
! 8418: u8 opcode; /* What operation to perform */
! 8419: signed char p4type; /* One of the P4_xxx constants for p4 */
! 8420: u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */
! 8421: u8 p5; /* Fifth parameter is an unsigned character */
! 8422: int p1; /* First operand */
! 8423: int p2; /* Second parameter (often the jump destination) */
! 8424: int p3; /* The third parameter */
! 8425: union { /* fourth parameter */
! 8426: int i; /* Integer value if p4type==P4_INT32 */
! 8427: void *p; /* Generic pointer */
! 8428: char *z; /* Pointer to data for string (char array) types */
! 8429: i64 *pI64; /* Used when p4type is P4_INT64 */
! 8430: double *pReal; /* Used when p4type is P4_REAL */
! 8431: FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
! 8432: VdbeFunc *pVdbeFunc; /* Used when p4type is P4_VDBEFUNC */
! 8433: CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
! 8434: Mem *pMem; /* Used when p4type is P4_MEM */
! 8435: VTable *pVtab; /* Used when p4type is P4_VTAB */
! 8436: KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
! 8437: int *ai; /* Used when p4type is P4_INTARRAY */
! 8438: SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
! 8439: int (*xAdvance)(BtCursor *, int *);
! 8440: } p4;
! 8441: #ifdef SQLITE_DEBUG
! 8442: char *zComment; /* Comment to improve readability */
! 8443: #endif
! 8444: #ifdef VDBE_PROFILE
! 8445: int cnt; /* Number of times this instruction was executed */
! 8446: u64 cycles; /* Total time spent executing this instruction */
! 8447: #endif
! 8448: };
! 8449: typedef struct VdbeOp VdbeOp;
! 8450:
! 8451:
! 8452: /*
! 8453: ** A sub-routine used to implement a trigger program.
! 8454: */
! 8455: struct SubProgram {
! 8456: VdbeOp *aOp; /* Array of opcodes for sub-program */
! 8457: int nOp; /* Elements in aOp[] */
! 8458: int nMem; /* Number of memory cells required */
! 8459: int nCsr; /* Number of cursors required */
! 8460: int nOnce; /* Number of OP_Once instructions */
! 8461: void *token; /* id that may be used to recursive triggers */
! 8462: SubProgram *pNext; /* Next sub-program already visited */
! 8463: };
! 8464:
! 8465: /*
! 8466: ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
! 8467: ** it takes up less space.
! 8468: */
! 8469: struct VdbeOpList {
! 8470: u8 opcode; /* What operation to perform */
! 8471: signed char p1; /* First operand */
! 8472: signed char p2; /* Second parameter (often the jump destination) */
! 8473: signed char p3; /* Third parameter */
! 8474: };
! 8475: typedef struct VdbeOpList VdbeOpList;
! 8476:
! 8477: /*
! 8478: ** Allowed values of VdbeOp.p4type
! 8479: */
! 8480: #define P4_NOTUSED 0 /* The P4 parameter is not used */
! 8481: #define P4_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */
! 8482: #define P4_STATIC (-2) /* Pointer to a static string */
! 8483: #define P4_COLLSEQ (-4) /* P4 is a pointer to a CollSeq structure */
! 8484: #define P4_FUNCDEF (-5) /* P4 is a pointer to a FuncDef structure */
! 8485: #define P4_KEYINFO (-6) /* P4 is a pointer to a KeyInfo structure */
! 8486: #define P4_VDBEFUNC (-7) /* P4 is a pointer to a VdbeFunc structure */
! 8487: #define P4_MEM (-8) /* P4 is a pointer to a Mem* structure */
! 8488: #define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */
! 8489: #define P4_VTAB (-10) /* P4 is a pointer to an sqlite3_vtab structure */
! 8490: #define P4_MPRINTF (-11) /* P4 is a string obtained from sqlite3_mprintf() */
! 8491: #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
! 8492: #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
! 8493: #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
! 8494: #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
! 8495: #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
! 8496: #define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
! 8497:
! 8498: /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
! 8499: ** is made. That copy is freed when the Vdbe is finalized. But if the
! 8500: ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still
! 8501: ** gets freed when the Vdbe is finalized so it still should be obtained
! 8502: ** from a single sqliteMalloc(). But no copy is made and the calling
! 8503: ** function should *not* try to free the KeyInfo.
! 8504: */
! 8505: #define P4_KEYINFO_HANDOFF (-16)
! 8506: #define P4_KEYINFO_STATIC (-17)
! 8507:
! 8508: /*
! 8509: ** The Vdbe.aColName array contains 5n Mem structures, where n is the
! 8510: ** number of columns of data returned by the statement.
! 8511: */
! 8512: #define COLNAME_NAME 0
! 8513: #define COLNAME_DECLTYPE 1
! 8514: #define COLNAME_DATABASE 2
! 8515: #define COLNAME_TABLE 3
! 8516: #define COLNAME_COLUMN 4
! 8517: #ifdef SQLITE_ENABLE_COLUMN_METADATA
! 8518: # define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
! 8519: #else
! 8520: # ifdef SQLITE_OMIT_DECLTYPE
! 8521: # define COLNAME_N 1 /* Store only the name */
! 8522: # else
! 8523: # define COLNAME_N 2 /* Store the name and decltype */
! 8524: # endif
! 8525: #endif
! 8526:
! 8527: /*
! 8528: ** The following macro converts a relative address in the p2 field
! 8529: ** of a VdbeOp structure into a negative number so that
! 8530: ** sqlite3VdbeAddOpList() knows that the address is relative. Calling
! 8531: ** the macro again restores the address.
! 8532: */
! 8533: #define ADDR(X) (-1-(X))
! 8534:
! 8535: /*
! 8536: ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
! 8537: ** header file that defines a number for each opcode used by the VDBE.
! 8538: */
! 8539: /************** Include opcodes.h in the middle of vdbe.h ********************/
! 8540: /************** Begin file opcodes.h *****************************************/
! 8541: /* Automatically generated. Do not edit */
! 8542: /* See the mkopcodeh.awk script for details */
! 8543: #define OP_Goto 1
! 8544: #define OP_Gosub 2
! 8545: #define OP_Return 3
! 8546: #define OP_Yield 4
! 8547: #define OP_HaltIfNull 5
! 8548: #define OP_Halt 6
! 8549: #define OP_Integer 7
! 8550: #define OP_Int64 8
! 8551: #define OP_Real 130 /* same as TK_FLOAT */
! 8552: #define OP_String8 94 /* same as TK_STRING */
! 8553: #define OP_String 9
! 8554: #define OP_Null 10
! 8555: #define OP_Blob 11
! 8556: #define OP_Variable 12
! 8557: #define OP_Move 13
! 8558: #define OP_Copy 14
! 8559: #define OP_SCopy 15
! 8560: #define OP_ResultRow 16
! 8561: #define OP_Concat 91 /* same as TK_CONCAT */
! 8562: #define OP_Add 86 /* same as TK_PLUS */
! 8563: #define OP_Subtract 87 /* same as TK_MINUS */
! 8564: #define OP_Multiply 88 /* same as TK_STAR */
! 8565: #define OP_Divide 89 /* same as TK_SLASH */
! 8566: #define OP_Remainder 90 /* same as TK_REM */
! 8567: #define OP_CollSeq 17
! 8568: #define OP_Function 18
! 8569: #define OP_BitAnd 82 /* same as TK_BITAND */
! 8570: #define OP_BitOr 83 /* same as TK_BITOR */
! 8571: #define OP_ShiftLeft 84 /* same as TK_LSHIFT */
! 8572: #define OP_ShiftRight 85 /* same as TK_RSHIFT */
! 8573: #define OP_AddImm 20
! 8574: #define OP_MustBeInt 21
! 8575: #define OP_RealAffinity 22
! 8576: #define OP_ToText 141 /* same as TK_TO_TEXT */
! 8577: #define OP_ToBlob 142 /* same as TK_TO_BLOB */
! 8578: #define OP_ToNumeric 143 /* same as TK_TO_NUMERIC*/
! 8579: #define OP_ToInt 144 /* same as TK_TO_INT */
! 8580: #define OP_ToReal 145 /* same as TK_TO_REAL */
! 8581: #define OP_Eq 76 /* same as TK_EQ */
! 8582: #define OP_Ne 75 /* same as TK_NE */
! 8583: #define OP_Lt 79 /* same as TK_LT */
! 8584: #define OP_Le 78 /* same as TK_LE */
! 8585: #define OP_Gt 77 /* same as TK_GT */
! 8586: #define OP_Ge 80 /* same as TK_GE */
! 8587: #define OP_Permutation 23
! 8588: #define OP_Compare 24
! 8589: #define OP_Jump 25
! 8590: #define OP_And 69 /* same as TK_AND */
! 8591: #define OP_Or 68 /* same as TK_OR */
! 8592: #define OP_Not 19 /* same as TK_NOT */
! 8593: #define OP_BitNot 93 /* same as TK_BITNOT */
! 8594: #define OP_Once 26
! 8595: #define OP_If 27
! 8596: #define OP_IfNot 28
! 8597: #define OP_IsNull 73 /* same as TK_ISNULL */
! 8598: #define OP_NotNull 74 /* same as TK_NOTNULL */
! 8599: #define OP_Column 29
! 8600: #define OP_Affinity 30
! 8601: #define OP_MakeRecord 31
! 8602: #define OP_Count 32
! 8603: #define OP_Savepoint 33
! 8604: #define OP_AutoCommit 34
! 8605: #define OP_Transaction 35
! 8606: #define OP_ReadCookie 36
! 8607: #define OP_SetCookie 37
! 8608: #define OP_VerifyCookie 38
! 8609: #define OP_OpenRead 39
! 8610: #define OP_OpenWrite 40
! 8611: #define OP_OpenAutoindex 41
! 8612: #define OP_OpenEphemeral 42
! 8613: #define OP_SorterOpen 43
! 8614: #define OP_OpenPseudo 44
! 8615: #define OP_Close 45
! 8616: #define OP_SeekLt 46
! 8617: #define OP_SeekLe 47
! 8618: #define OP_SeekGe 48
! 8619: #define OP_SeekGt 49
! 8620: #define OP_Seek 50
! 8621: #define OP_NotFound 51
! 8622: #define OP_Found 52
! 8623: #define OP_IsUnique 53
! 8624: #define OP_NotExists 54
! 8625: #define OP_Sequence 55
! 8626: #define OP_NewRowid 56
! 8627: #define OP_Insert 57
! 8628: #define OP_InsertInt 58
! 8629: #define OP_Delete 59
! 8630: #define OP_ResetCount 60
! 8631: #define OP_SorterCompare 61
! 8632: #define OP_SorterData 62
! 8633: #define OP_RowKey 63
! 8634: #define OP_RowData 64
! 8635: #define OP_Rowid 65
! 8636: #define OP_NullRow 66
! 8637: #define OP_Last 67
! 8638: #define OP_SorterSort 70
! 8639: #define OP_Sort 71
! 8640: #define OP_Rewind 72
! 8641: #define OP_SorterNext 81
! 8642: #define OP_Prev 92
! 8643: #define OP_Next 95
! 8644: #define OP_SorterInsert 96
! 8645: #define OP_IdxInsert 97
! 8646: #define OP_IdxDelete 98
! 8647: #define OP_IdxRowid 99
! 8648: #define OP_IdxLT 100
! 8649: #define OP_IdxGE 101
! 8650: #define OP_Destroy 102
! 8651: #define OP_Clear 103
! 8652: #define OP_CreateIndex 104
! 8653: #define OP_CreateTable 105
! 8654: #define OP_ParseSchema 106
! 8655: #define OP_LoadAnalysis 107
! 8656: #define OP_DropTable 108
! 8657: #define OP_DropIndex 109
! 8658: #define OP_DropTrigger 110
! 8659: #define OP_IntegrityCk 111
! 8660: #define OP_RowSetAdd 112
! 8661: #define OP_RowSetRead 113
! 8662: #define OP_RowSetTest 114
! 8663: #define OP_Program 115
! 8664: #define OP_Param 116
! 8665: #define OP_FkCounter 117
! 8666: #define OP_FkIfZero 118
! 8667: #define OP_MemMax 119
! 8668: #define OP_IfPos 120
! 8669: #define OP_IfNeg 121
! 8670: #define OP_IfZero 122
! 8671: #define OP_AggStep 123
! 8672: #define OP_AggFinal 124
! 8673: #define OP_Checkpoint 125
! 8674: #define OP_JournalMode 126
! 8675: #define OP_Vacuum 127
! 8676: #define OP_IncrVacuum 128
! 8677: #define OP_Expire 129
! 8678: #define OP_TableLock 131
! 8679: #define OP_VBegin 132
! 8680: #define OP_VCreate 133
! 8681: #define OP_VDestroy 134
! 8682: #define OP_VOpen 135
! 8683: #define OP_VFilter 136
! 8684: #define OP_VColumn 137
! 8685: #define OP_VNext 138
! 8686: #define OP_VRename 139
! 8687: #define OP_VUpdate 140
! 8688: #define OP_Pagecount 146
! 8689: #define OP_MaxPgcnt 147
! 8690: #define OP_Trace 148
! 8691: #define OP_Noop 149
! 8692: #define OP_Explain 150
! 8693:
! 8694:
! 8695: /* Properties such as "out2" or "jump" that are specified in
! 8696: ** comments following the "case" for each opcode in the vdbe.c
! 8697: ** are encoded into bitvectors as follows:
! 8698: */
! 8699: #define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */
! 8700: #define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */
! 8701: #define OPFLG_IN1 0x0004 /* in1: P1 is an input */
! 8702: #define OPFLG_IN2 0x0008 /* in2: P2 is an input */
! 8703: #define OPFLG_IN3 0x0010 /* in3: P3 is an input */
! 8704: #define OPFLG_OUT2 0x0020 /* out2: P2 is an output */
! 8705: #define OPFLG_OUT3 0x0040 /* out3: P3 is an output */
! 8706: #define OPFLG_INITIALIZER {\
! 8707: /* 0 */ 0x00, 0x01, 0x01, 0x04, 0x04, 0x10, 0x00, 0x02,\
! 8708: /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
! 8709: /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
! 8710: /* 24 */ 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00,\
! 8711: /* 32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
! 8712: /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
! 8713: /* 48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
! 8714: /* 56 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
! 8715: /* 64 */ 0x00, 0x02, 0x00, 0x01, 0x4c, 0x4c, 0x01, 0x01,\
! 8716: /* 72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
! 8717: /* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
! 8718: /* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x01,\
! 8719: /* 96 */ 0x08, 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00,\
! 8720: /* 104 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
! 8721: /* 112 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\
! 8722: /* 120 */ 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00,\
! 8723: /* 128 */ 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
! 8724: /* 136 */ 0x01, 0x00, 0x01, 0x00, 0x00, 0x04, 0x04, 0x04,\
! 8725: /* 144 */ 0x04, 0x04, 0x02, 0x02, 0x00, 0x00, 0x00,}
! 8726:
! 8727: /************** End of opcodes.h *********************************************/
! 8728: /************** Continuing where we left off in vdbe.h ***********************/
! 8729:
! 8730: /*
! 8731: ** Prototypes for the VDBE interface. See comments on the implementation
! 8732: ** for a description of what each of these routines does.
! 8733: */
! 8734: SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
! 8735: SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
! 8736: SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
! 8737: SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
! 8738: SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
! 8739: SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
! 8740: SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
! 8741: SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
! 8742: SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
! 8743: SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
! 8744: SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
! 8745: SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
! 8746: SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
! 8747: SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
! 8748: SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
! 8749: SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
! 8750: SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
! 8751: SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
! 8752: SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
! 8753: SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
! 8754: SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
! 8755: SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*);
! 8756: SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
! 8757: SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
! 8758: SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
! 8759: SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
! 8760: #ifdef SQLITE_DEBUG
! 8761: SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *, int);
! 8762: SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe*,FILE*);
! 8763: #endif
! 8764: SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
! 8765: SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
! 8766: SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
! 8767: SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
! 8768: SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
! 8769: SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
! 8770: SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
! 8771: SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
! 8772: SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
! 8773: SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
! 8774: SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
! 8775: SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
! 8776: #ifndef SQLITE_OMIT_TRACE
! 8777: SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
! 8778: #endif
! 8779:
! 8780: SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
! 8781: SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
! 8782: SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
! 8783:
! 8784: #ifndef SQLITE_OMIT_TRIGGER
! 8785: SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
! 8786: #endif
! 8787:
! 8788:
! 8789: #ifndef NDEBUG
! 8790: SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe*, const char*, ...);
! 8791: # define VdbeComment(X) sqlite3VdbeComment X
! 8792: SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
! 8793: # define VdbeNoopComment(X) sqlite3VdbeNoopComment X
! 8794: #else
! 8795: # define VdbeComment(X)
! 8796: # define VdbeNoopComment(X)
! 8797: #endif
! 8798:
! 8799: #endif
! 8800:
! 8801: /************** End of vdbe.h ************************************************/
! 8802: /************** Continuing where we left off in sqliteInt.h ******************/
! 8803: /************** Include pager.h in the middle of sqliteInt.h *****************/
! 8804: /************** Begin file pager.h *******************************************/
! 8805: /*
! 8806: ** 2001 September 15
! 8807: **
! 8808: ** The author disclaims copyright to this source code. In place of
! 8809: ** a legal notice, here is a blessing:
! 8810: **
! 8811: ** May you do good and not evil.
! 8812: ** May you find forgiveness for yourself and forgive others.
! 8813: ** May you share freely, never taking more than you give.
! 8814: **
! 8815: *************************************************************************
! 8816: ** This header file defines the interface that the sqlite page cache
! 8817: ** subsystem. The page cache subsystem reads and writes a file a page
! 8818: ** at a time and provides a journal for rollback.
! 8819: */
! 8820:
! 8821: #ifndef _PAGER_H_
! 8822: #define _PAGER_H_
! 8823:
! 8824: /*
! 8825: ** Default maximum size for persistent journal files. A negative
! 8826: ** value means no limit. This value may be overridden using the
! 8827: ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
! 8828: */
! 8829: #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
! 8830: #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
! 8831: #endif
! 8832:
! 8833: /*
! 8834: ** The type used to represent a page number. The first page in a file
! 8835: ** is called page 1. 0 is used to represent "not a page".
! 8836: */
! 8837: typedef u32 Pgno;
! 8838:
! 8839: /*
! 8840: ** Each open file is managed by a separate instance of the "Pager" structure.
! 8841: */
! 8842: typedef struct Pager Pager;
! 8843:
! 8844: /*
! 8845: ** Handle type for pages.
! 8846: */
! 8847: typedef struct PgHdr DbPage;
! 8848:
! 8849: /*
! 8850: ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
! 8851: ** reserved for working around a windows/posix incompatibility). It is
! 8852: ** used in the journal to signify that the remainder of the journal file
! 8853: ** is devoted to storing a master journal name - there are no more pages to
! 8854: ** roll back. See comments for function writeMasterJournal() in pager.c
! 8855: ** for details.
! 8856: */
! 8857: #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
! 8858:
! 8859: /*
! 8860: ** Allowed values for the flags parameter to sqlite3PagerOpen().
! 8861: **
! 8862: ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
! 8863: */
! 8864: #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
! 8865: #define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */
! 8866: #define PAGER_MEMORY 0x0004 /* In-memory database */
! 8867:
! 8868: /*
! 8869: ** Valid values for the second argument to sqlite3PagerLockingMode().
! 8870: */
! 8871: #define PAGER_LOCKINGMODE_QUERY -1
! 8872: #define PAGER_LOCKINGMODE_NORMAL 0
! 8873: #define PAGER_LOCKINGMODE_EXCLUSIVE 1
! 8874:
! 8875: /*
! 8876: ** Numeric constants that encode the journalmode.
! 8877: */
! 8878: #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
! 8879: #define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */
! 8880: #define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */
! 8881: #define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
! 8882: #define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
! 8883: #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
! 8884: #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
! 8885:
! 8886: /*
! 8887: ** The remainder of this file contains the declarations of the functions
! 8888: ** that make up the Pager sub-system API. See source code comments for
! 8889: ** a detailed description of each routine.
! 8890: */
! 8891:
! 8892: /* Open and close a Pager connection. */
! 8893: SQLITE_PRIVATE int sqlite3PagerOpen(
! 8894: sqlite3_vfs*,
! 8895: Pager **ppPager,
! 8896: const char*,
! 8897: int,
! 8898: int,
! 8899: int,
! 8900: void(*)(DbPage*)
! 8901: );
! 8902: SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
! 8903: SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
! 8904:
! 8905: /* Functions used to configure a Pager object. */
! 8906: SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
! 8907: SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
! 8908: SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
! 8909: SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
! 8910: SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
! 8911: SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
! 8912: SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
! 8913: SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
! 8914: SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
! 8915: SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
! 8916: SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
! 8917: SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
! 8918:
! 8919: /* Functions used to obtain and release page references. */
! 8920: SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
! 8921: #define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
! 8922: SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
! 8923: SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
! 8924: SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
! 8925:
! 8926: /* Operations on page references. */
! 8927: SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
! 8928: SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
! 8929: SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
! 8930: SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
! 8931: SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
! 8932: SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
! 8933:
! 8934: /* Functions used to manage pager transactions and savepoints. */
! 8935: SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
! 8936: SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
! 8937: SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
! 8938: SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
! 8939: SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
! 8940: SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
! 8941: SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
! 8942: SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
! 8943: SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
! 8944: SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
! 8945:
! 8946: SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
! 8947: SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
! 8948: SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
! 8949: SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
! 8950: SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
! 8951:
! 8952: /* Functions used to query pager state and configuration. */
! 8953: SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
! 8954: SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
! 8955: SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
! 8956: SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*);
! 8957: SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
! 8958: SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
! 8959: SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
! 8960: SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
! 8961: SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
! 8962: SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
! 8963: SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
! 8964: SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
! 8965:
! 8966: /* Functions used to truncate the database file. */
! 8967: SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
! 8968:
! 8969: #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
! 8970: SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
! 8971: #endif
! 8972:
! 8973: /* Functions to support testing and debugging. */
! 8974: #if !defined(NDEBUG) || defined(SQLITE_TEST)
! 8975: SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
! 8976: SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
! 8977: #endif
! 8978: #ifdef SQLITE_TEST
! 8979: SQLITE_PRIVATE int *sqlite3PagerStats(Pager*);
! 8980: SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*);
! 8981: void disable_simulated_io_errors(void);
! 8982: void enable_simulated_io_errors(void);
! 8983: #else
! 8984: # define disable_simulated_io_errors()
! 8985: # define enable_simulated_io_errors()
! 8986: #endif
! 8987:
! 8988: #endif /* _PAGER_H_ */
! 8989:
! 8990: /************** End of pager.h ***********************************************/
! 8991: /************** Continuing where we left off in sqliteInt.h ******************/
! 8992: /************** Include pcache.h in the middle of sqliteInt.h ****************/
! 8993: /************** Begin file pcache.h ******************************************/
! 8994: /*
! 8995: ** 2008 August 05
! 8996: **
! 8997: ** The author disclaims copyright to this source code. In place of
! 8998: ** a legal notice, here is a blessing:
! 8999: **
! 9000: ** May you do good and not evil.
! 9001: ** May you find forgiveness for yourself and forgive others.
! 9002: ** May you share freely, never taking more than you give.
! 9003: **
! 9004: *************************************************************************
! 9005: ** This header file defines the interface that the sqlite page cache
! 9006: ** subsystem.
! 9007: */
! 9008:
! 9009: #ifndef _PCACHE_H_
! 9010:
! 9011: typedef struct PgHdr PgHdr;
! 9012: typedef struct PCache PCache;
! 9013:
! 9014: /*
! 9015: ** Every page in the cache is controlled by an instance of the following
! 9016: ** structure.
! 9017: */
! 9018: struct PgHdr {
! 9019: sqlite3_pcache_page *pPage; /* Pcache object page handle */
! 9020: void *pData; /* Page data */
! 9021: void *pExtra; /* Extra content */
! 9022: PgHdr *pDirty; /* Transient list of dirty pages */
! 9023: Pgno pgno; /* Page number for this page */
! 9024: Pager *pPager; /* The pager this page is part of */
! 9025: #ifdef SQLITE_CHECK_PAGES
! 9026: u32 pageHash; /* Hash of page content */
! 9027: #endif
! 9028: u16 flags; /* PGHDR flags defined below */
! 9029:
! 9030: /**********************************************************************
! 9031: ** Elements above are public. All that follows is private to pcache.c
! 9032: ** and should not be accessed by other modules.
! 9033: */
! 9034: i16 nRef; /* Number of users of this page */
! 9035: PCache *pCache; /* Cache that owns this page */
! 9036:
! 9037: PgHdr *pDirtyNext; /* Next element in list of dirty pages */
! 9038: PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
! 9039: };
! 9040:
! 9041: /* Bit values for PgHdr.flags */
! 9042: #define PGHDR_DIRTY 0x002 /* Page has changed */
! 9043: #define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
! 9044: ** writing this page to the database */
! 9045: #define PGHDR_NEED_READ 0x008 /* Content is unread */
! 9046: #define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
! 9047: #define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
! 9048:
! 9049: /* Initialize and shutdown the page cache subsystem */
! 9050: SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
! 9051: SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
! 9052:
! 9053: /* Page cache buffer management:
! 9054: ** These routines implement SQLITE_CONFIG_PAGECACHE.
! 9055: */
! 9056: SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
! 9057:
! 9058: /* Create a new pager cache.
! 9059: ** Under memory stress, invoke xStress to try to make pages clean.
! 9060: ** Only clean and unpinned pages can be reclaimed.
! 9061: */
! 9062: SQLITE_PRIVATE void sqlite3PcacheOpen(
! 9063: int szPage, /* Size of every page */
! 9064: int szExtra, /* Extra space associated with each page */
! 9065: int bPurgeable, /* True if pages are on backing store */
! 9066: int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
! 9067: void *pStress, /* Argument to xStress */
! 9068: PCache *pToInit /* Preallocated space for the PCache */
! 9069: );
! 9070:
! 9071: /* Modify the page-size after the cache has been created. */
! 9072: SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
! 9073:
! 9074: /* Return the size in bytes of a PCache object. Used to preallocate
! 9075: ** storage space.
! 9076: */
! 9077: SQLITE_PRIVATE int sqlite3PcacheSize(void);
! 9078:
! 9079: /* One release per successful fetch. Page is pinned until released.
! 9080: ** Reference counted.
! 9081: */
! 9082: SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
! 9083: SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
! 9084:
! 9085: SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
! 9086: SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
! 9087: SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
! 9088: SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
! 9089:
! 9090: /* Change a page number. Used by incr-vacuum. */
! 9091: SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
! 9092:
! 9093: /* Remove all pages with pgno>x. Reset the cache if x==0 */
! 9094: SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
! 9095:
! 9096: /* Get a list of all dirty pages in the cache, sorted by page number */
! 9097: SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
! 9098:
! 9099: /* Reset and close the cache object */
! 9100: SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
! 9101:
! 9102: /* Clear flags from pages of the page cache */
! 9103: SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
! 9104:
! 9105: /* Discard the contents of the cache */
! 9106: SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
! 9107:
! 9108: /* Return the total number of outstanding page references */
! 9109: SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
! 9110:
! 9111: /* Increment the reference count of an existing page */
! 9112: SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
! 9113:
! 9114: SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
! 9115:
! 9116: /* Return the total number of pages stored in the cache */
! 9117: SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
! 9118:
! 9119: #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
! 9120: /* Iterate through all dirty pages currently stored in the cache. This
! 9121: ** interface is only available if SQLITE_CHECK_PAGES is defined when the
! 9122: ** library is built.
! 9123: */
! 9124: SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
! 9125: #endif
! 9126:
! 9127: /* Set and get the suggested cache-size for the specified pager-cache.
! 9128: **
! 9129: ** If no global maximum is configured, then the system attempts to limit
! 9130: ** the total number of pages cached by purgeable pager-caches to the sum
! 9131: ** of the suggested cache-sizes.
! 9132: */
! 9133: SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
! 9134: #ifdef SQLITE_TEST
! 9135: SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
! 9136: #endif
! 9137:
! 9138: /* Free up as much memory as possible from the page cache */
! 9139: SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
! 9140:
! 9141: #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
! 9142: /* Try to return memory used by the pcache module to the main memory heap */
! 9143: SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
! 9144: #endif
! 9145:
! 9146: #ifdef SQLITE_TEST
! 9147: SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
! 9148: #endif
! 9149:
! 9150: SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
! 9151:
! 9152: #endif /* _PCACHE_H_ */
! 9153:
! 9154: /************** End of pcache.h **********************************************/
! 9155: /************** Continuing where we left off in sqliteInt.h ******************/
! 9156:
! 9157: /************** Include os.h in the middle of sqliteInt.h ********************/
! 9158: /************** Begin file os.h **********************************************/
! 9159: /*
! 9160: ** 2001 September 16
! 9161: **
! 9162: ** The author disclaims copyright to this source code. In place of
! 9163: ** a legal notice, here is a blessing:
! 9164: **
! 9165: ** May you do good and not evil.
! 9166: ** May you find forgiveness for yourself and forgive others.
! 9167: ** May you share freely, never taking more than you give.
! 9168: **
! 9169: ******************************************************************************
! 9170: **
! 9171: ** This header file (together with is companion C source-code file
! 9172: ** "os.c") attempt to abstract the underlying operating system so that
! 9173: ** the SQLite library will work on both POSIX and windows systems.
! 9174: **
! 9175: ** This header file is #include-ed by sqliteInt.h and thus ends up
! 9176: ** being included by every source file.
! 9177: */
! 9178: #ifndef _SQLITE_OS_H_
! 9179: #define _SQLITE_OS_H_
! 9180:
! 9181: /*
! 9182: ** Figure out if we are dealing with Unix, Windows, or some other
! 9183: ** operating system. After the following block of preprocess macros,
! 9184: ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER
! 9185: ** will defined to either 1 or 0. One of the four will be 1. The other
! 9186: ** three will be 0.
! 9187: */
! 9188: #if defined(SQLITE_OS_OTHER)
! 9189: # if SQLITE_OS_OTHER==1
! 9190: # undef SQLITE_OS_UNIX
! 9191: # define SQLITE_OS_UNIX 0
! 9192: # undef SQLITE_OS_WIN
! 9193: # define SQLITE_OS_WIN 0
! 9194: # undef SQLITE_OS_OS2
! 9195: # define SQLITE_OS_OS2 0
! 9196: # else
! 9197: # undef SQLITE_OS_OTHER
! 9198: # endif
! 9199: #endif
! 9200: #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
! 9201: # define SQLITE_OS_OTHER 0
! 9202: # ifndef SQLITE_OS_WIN
! 9203: # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
! 9204: # define SQLITE_OS_WIN 1
! 9205: # define SQLITE_OS_UNIX 0
! 9206: # define SQLITE_OS_OS2 0
! 9207: # elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
! 9208: # define SQLITE_OS_WIN 0
! 9209: # define SQLITE_OS_UNIX 0
! 9210: # define SQLITE_OS_OS2 1
! 9211: # else
! 9212: # define SQLITE_OS_WIN 0
! 9213: # define SQLITE_OS_UNIX 1
! 9214: # define SQLITE_OS_OS2 0
! 9215: # endif
! 9216: # else
! 9217: # define SQLITE_OS_UNIX 0
! 9218: # define SQLITE_OS_OS2 0
! 9219: # endif
! 9220: #else
! 9221: # ifndef SQLITE_OS_WIN
! 9222: # define SQLITE_OS_WIN 0
! 9223: # endif
! 9224: #endif
! 9225:
! 9226: /*
! 9227: ** Define the maximum size of a temporary filename
! 9228: */
! 9229: #if SQLITE_OS_WIN
! 9230: # include <windows.h>
! 9231: # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
! 9232: #elif SQLITE_OS_OS2
! 9233: # if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
! 9234: # include <os2safe.h> /* has to be included before os2.h for linking to work */
! 9235: # endif
! 9236: # define INCL_DOSDATETIME
! 9237: # define INCL_DOSFILEMGR
! 9238: # define INCL_DOSERRORS
! 9239: # define INCL_DOSMISC
! 9240: # define INCL_DOSPROCESS
! 9241: # define INCL_DOSMODULEMGR
! 9242: # define INCL_DOSSEMAPHORES
! 9243: # include <os2.h>
! 9244: # include <uconv.h>
! 9245: # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
! 9246: #else
! 9247: # define SQLITE_TEMPNAME_SIZE 200
! 9248: #endif
! 9249:
! 9250: /*
! 9251: ** Determine if we are dealing with Windows NT.
! 9252: */
! 9253: #if defined(_WIN32_WINNT)
! 9254: # define SQLITE_OS_WINNT 1
! 9255: #else
! 9256: # define SQLITE_OS_WINNT 0
! 9257: #endif
! 9258:
! 9259: /*
! 9260: ** Determine if we are dealing with WindowsCE - which has a much
! 9261: ** reduced API.
! 9262: */
! 9263: #if defined(_WIN32_WCE)
! 9264: # define SQLITE_OS_WINCE 1
! 9265: #else
! 9266: # define SQLITE_OS_WINCE 0
! 9267: #endif
! 9268:
! 9269: /* If the SET_FULLSYNC macro is not defined above, then make it
! 9270: ** a no-op
! 9271: */
! 9272: #ifndef SET_FULLSYNC
! 9273: # define SET_FULLSYNC(x,y)
! 9274: #endif
! 9275:
! 9276: /*
! 9277: ** The default size of a disk sector
! 9278: */
! 9279: #ifndef SQLITE_DEFAULT_SECTOR_SIZE
! 9280: # define SQLITE_DEFAULT_SECTOR_SIZE 4096
! 9281: #endif
! 9282:
! 9283: /*
! 9284: ** Temporary files are named starting with this prefix followed by 16 random
! 9285: ** alphanumeric characters, and no file extension. They are stored in the
! 9286: ** OS's standard temporary file directory, and are deleted prior to exit.
! 9287: ** If sqlite is being embedded in another program, you may wish to change the
! 9288: ** prefix to reflect your program's name, so that if your program exits
! 9289: ** prematurely, old temporary files can be easily identified. This can be done
! 9290: ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
! 9291: **
! 9292: ** 2006-10-31: The default prefix used to be "sqlite_". But then
! 9293: ** Mcafee started using SQLite in their anti-virus product and it
! 9294: ** started putting files with the "sqlite" name in the c:/temp folder.
! 9295: ** This annoyed many windows users. Those users would then do a
! 9296: ** Google search for "sqlite", find the telephone numbers of the
! 9297: ** developers and call to wake them up at night and complain.
! 9298: ** For this reason, the default name prefix is changed to be "sqlite"
! 9299: ** spelled backwards. So the temp files are still identified, but
! 9300: ** anybody smart enough to figure out the code is also likely smart
! 9301: ** enough to know that calling the developer will not help get rid
! 9302: ** of the file.
! 9303: */
! 9304: #ifndef SQLITE_TEMP_FILE_PREFIX
! 9305: # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
! 9306: #endif
! 9307:
! 9308: /*
! 9309: ** The following values may be passed as the second argument to
! 9310: ** sqlite3OsLock(). The various locks exhibit the following semantics:
! 9311: **
! 9312: ** SHARED: Any number of processes may hold a SHARED lock simultaneously.
! 9313: ** RESERVED: A single process may hold a RESERVED lock on a file at
! 9314: ** any time. Other processes may hold and obtain new SHARED locks.
! 9315: ** PENDING: A single process may hold a PENDING lock on a file at
! 9316: ** any one time. Existing SHARED locks may persist, but no new
! 9317: ** SHARED locks may be obtained by other processes.
! 9318: ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
! 9319: **
! 9320: ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
! 9321: ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
! 9322: ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
! 9323: ** sqlite3OsLock().
! 9324: */
! 9325: #define NO_LOCK 0
! 9326: #define SHARED_LOCK 1
! 9327: #define RESERVED_LOCK 2
! 9328: #define PENDING_LOCK 3
! 9329: #define EXCLUSIVE_LOCK 4
! 9330:
! 9331: /*
! 9332: ** File Locking Notes: (Mostly about windows but also some info for Unix)
! 9333: **
! 9334: ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
! 9335: ** those functions are not available. So we use only LockFile() and
! 9336: ** UnlockFile().
! 9337: **
! 9338: ** LockFile() prevents not just writing but also reading by other processes.
! 9339: ** A SHARED_LOCK is obtained by locking a single randomly-chosen
! 9340: ** byte out of a specific range of bytes. The lock byte is obtained at
! 9341: ** random so two separate readers can probably access the file at the
! 9342: ** same time, unless they are unlucky and choose the same lock byte.
! 9343: ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
! 9344: ** There can only be one writer. A RESERVED_LOCK is obtained by locking
! 9345: ** a single byte of the file that is designated as the reserved lock byte.
! 9346: ** A PENDING_LOCK is obtained by locking a designated byte different from
! 9347: ** the RESERVED_LOCK byte.
! 9348: **
! 9349: ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
! 9350: ** which means we can use reader/writer locks. When reader/writer locks
! 9351: ** are used, the lock is placed on the same range of bytes that is used
! 9352: ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
! 9353: ** will support two or more Win95 readers or two or more WinNT readers.
! 9354: ** But a single Win95 reader will lock out all WinNT readers and a single
! 9355: ** WinNT reader will lock out all other Win95 readers.
! 9356: **
! 9357: ** The following #defines specify the range of bytes used for locking.
! 9358: ** SHARED_SIZE is the number of bytes available in the pool from which
! 9359: ** a random byte is selected for a shared lock. The pool of bytes for
! 9360: ** shared locks begins at SHARED_FIRST.
! 9361: **
! 9362: ** The same locking strategy and
! 9363: ** byte ranges are used for Unix. This leaves open the possiblity of having
! 9364: ** clients on win95, winNT, and unix all talking to the same shared file
! 9365: ** and all locking correctly. To do so would require that samba (or whatever
! 9366: ** tool is being used for file sharing) implements locks correctly between
! 9367: ** windows and unix. I'm guessing that isn't likely to happen, but by
! 9368: ** using the same locking range we are at least open to the possibility.
! 9369: **
! 9370: ** Locking in windows is manditory. For this reason, we cannot store
! 9371: ** actual data in the bytes used for locking. The pager never allocates
! 9372: ** the pages involved in locking therefore. SHARED_SIZE is selected so
! 9373: ** that all locks will fit on a single page even at the minimum page size.
! 9374: ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
! 9375: ** is set high so that we don't have to allocate an unused page except
! 9376: ** for very large databases. But one should test the page skipping logic
! 9377: ** by setting PENDING_BYTE low and running the entire regression suite.
! 9378: **
! 9379: ** Changing the value of PENDING_BYTE results in a subtly incompatible
! 9380: ** file format. Depending on how it is changed, you might not notice
! 9381: ** the incompatibility right away, even running a full regression test.
! 9382: ** The default location of PENDING_BYTE is the first byte past the
! 9383: ** 1GB boundary.
! 9384: **
! 9385: */
! 9386: #ifdef SQLITE_OMIT_WSD
! 9387: # define PENDING_BYTE (0x40000000)
! 9388: #else
! 9389: # define PENDING_BYTE sqlite3PendingByte
! 9390: #endif
! 9391: #define RESERVED_BYTE (PENDING_BYTE+1)
! 9392: #define SHARED_FIRST (PENDING_BYTE+2)
! 9393: #define SHARED_SIZE 510
! 9394:
! 9395: /*
! 9396: ** Wrapper around OS specific sqlite3_os_init() function.
! 9397: */
! 9398: SQLITE_PRIVATE int sqlite3OsInit(void);
! 9399:
! 9400: /*
! 9401: ** Functions for accessing sqlite3_file methods
! 9402: */
! 9403: SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
! 9404: SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
! 9405: SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
! 9406: SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
! 9407: SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
! 9408: SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
! 9409: SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
! 9410: SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
! 9411: SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
! 9412: SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
! 9413: SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
! 9414: #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
! 9415: SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
! 9416: SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
! 9417: SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
! 9418: SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
! 9419: SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
! 9420: SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
! 9421:
! 9422:
! 9423: /*
! 9424: ** Functions for accessing sqlite3_vfs methods
! 9425: */
! 9426: SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
! 9427: SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
! 9428: SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
! 9429: SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
! 9430: #ifndef SQLITE_OMIT_LOAD_EXTENSION
! 9431: SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
! 9432: SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
! 9433: SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
! 9434: SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
! 9435: #endif /* SQLITE_OMIT_LOAD_EXTENSION */
! 9436: SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
! 9437: SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
! 9438: SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
! 9439:
! 9440: /*
! 9441: ** Convenience functions for opening and closing files using
! 9442: ** sqlite3_malloc() to obtain space for the file-handle structure.
! 9443: */
! 9444: SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
! 9445: SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
! 9446:
! 9447: #endif /* _SQLITE_OS_H_ */
! 9448:
! 9449: /************** End of os.h **************************************************/
! 9450: /************** Continuing where we left off in sqliteInt.h ******************/
! 9451: /************** Include mutex.h in the middle of sqliteInt.h *****************/
! 9452: /************** Begin file mutex.h *******************************************/
! 9453: /*
! 9454: ** 2007 August 28
! 9455: **
! 9456: ** The author disclaims copyright to this source code. In place of
! 9457: ** a legal notice, here is a blessing:
! 9458: **
! 9459: ** May you do good and not evil.
! 9460: ** May you find forgiveness for yourself and forgive others.
! 9461: ** May you share freely, never taking more than you give.
! 9462: **
! 9463: *************************************************************************
! 9464: **
! 9465: ** This file contains the common header for all mutex implementations.
! 9466: ** The sqliteInt.h header #includes this file so that it is available
! 9467: ** to all source files. We break it out in an effort to keep the code
! 9468: ** better organized.
! 9469: **
! 9470: ** NOTE: source files should *not* #include this header file directly.
! 9471: ** Source files should #include the sqliteInt.h file and let that file
! 9472: ** include this one indirectly.
! 9473: */
! 9474:
! 9475:
! 9476: /*
! 9477: ** Figure out what version of the code to use. The choices are
! 9478: **
! 9479: ** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The
! 9480: ** mutexes implemention cannot be overridden
! 9481: ** at start-time.
! 9482: **
! 9483: ** SQLITE_MUTEX_NOOP For single-threaded applications. No
! 9484: ** mutual exclusion is provided. But this
! 9485: ** implementation can be overridden at
! 9486: ** start-time.
! 9487: **
! 9488: ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
! 9489: **
! 9490: ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
! 9491: **
! 9492: ** SQLITE_MUTEX_OS2 For multi-threaded applications on OS/2.
! 9493: */
! 9494: #if !SQLITE_THREADSAFE
! 9495: # define SQLITE_MUTEX_OMIT
! 9496: #endif
! 9497: #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
! 9498: # if SQLITE_OS_UNIX
! 9499: # define SQLITE_MUTEX_PTHREADS
! 9500: # elif SQLITE_OS_WIN
! 9501: # define SQLITE_MUTEX_W32
! 9502: # elif SQLITE_OS_OS2
! 9503: # define SQLITE_MUTEX_OS2
! 9504: # else
! 9505: # define SQLITE_MUTEX_NOOP
! 9506: # endif
! 9507: #endif
! 9508:
! 9509: #ifdef SQLITE_MUTEX_OMIT
! 9510: /*
! 9511: ** If this is a no-op implementation, implement everything as macros.
! 9512: */
! 9513: #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
! 9514: #define sqlite3_mutex_free(X)
! 9515: #define sqlite3_mutex_enter(X)
! 9516: #define sqlite3_mutex_try(X) SQLITE_OK
! 9517: #define sqlite3_mutex_leave(X)
! 9518: #define sqlite3_mutex_held(X) ((void)(X),1)
! 9519: #define sqlite3_mutex_notheld(X) ((void)(X),1)
! 9520: #define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
! 9521: #define sqlite3MutexInit() SQLITE_OK
! 9522: #define sqlite3MutexEnd()
! 9523: #define MUTEX_LOGIC(X)
! 9524: #else
! 9525: #define MUTEX_LOGIC(X) X
! 9526: #endif /* defined(SQLITE_MUTEX_OMIT) */
! 9527:
! 9528: /************** End of mutex.h ***********************************************/
! 9529: /************** Continuing where we left off in sqliteInt.h ******************/
! 9530:
! 9531:
! 9532: /*
! 9533: ** Each database file to be accessed by the system is an instance
! 9534: ** of the following structure. There are normally two of these structures
! 9535: ** in the sqlite.aDb[] array. aDb[0] is the main database file and
! 9536: ** aDb[1] is the database file used to hold temporary tables. Additional
! 9537: ** databases may be attached.
! 9538: */
! 9539: struct Db {
! 9540: char *zName; /* Name of this database */
! 9541: Btree *pBt; /* The B*Tree structure for this database file */
! 9542: u8 inTrans; /* 0: not writable. 1: Transaction. 2: Checkpoint */
! 9543: u8 safety_level; /* How aggressive at syncing data to disk */
! 9544: Schema *pSchema; /* Pointer to database schema (possibly shared) */
! 9545: };
! 9546:
! 9547: /*
! 9548: ** An instance of the following structure stores a database schema.
! 9549: **
! 9550: ** Most Schema objects are associated with a Btree. The exception is
! 9551: ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
! 9552: ** In shared cache mode, a single Schema object can be shared by multiple
! 9553: ** Btrees that refer to the same underlying BtShared object.
! 9554: **
! 9555: ** Schema objects are automatically deallocated when the last Btree that
! 9556: ** references them is destroyed. The TEMP Schema is manually freed by
! 9557: ** sqlite3_close().
! 9558: *
! 9559: ** A thread must be holding a mutex on the corresponding Btree in order
! 9560: ** to access Schema content. This implies that the thread must also be
! 9561: ** holding a mutex on the sqlite3 connection pointer that owns the Btree.
! 9562: ** For a TEMP Schema, only the connection mutex is required.
! 9563: */
! 9564: struct Schema {
! 9565: int schema_cookie; /* Database schema version number for this file */
! 9566: int iGeneration; /* Generation counter. Incremented with each change */
! 9567: Hash tblHash; /* All tables indexed by name */
! 9568: Hash idxHash; /* All (named) indices indexed by name */
! 9569: Hash trigHash; /* All triggers indexed by name */
! 9570: Hash fkeyHash; /* All foreign keys by referenced table name */
! 9571: Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
! 9572: u8 file_format; /* Schema format version for this file */
! 9573: u8 enc; /* Text encoding used by this database */
! 9574: u16 flags; /* Flags associated with this schema */
! 9575: int cache_size; /* Number of pages to use in the cache */
! 9576: };
! 9577:
! 9578: /*
! 9579: ** These macros can be used to test, set, or clear bits in the
! 9580: ** Db.pSchema->flags field.
! 9581: */
! 9582: #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))==(P))
! 9583: #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))!=0)
! 9584: #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->flags|=(P)
! 9585: #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->flags&=~(P)
! 9586:
! 9587: /*
! 9588: ** Allowed values for the DB.pSchema->flags field.
! 9589: **
! 9590: ** The DB_SchemaLoaded flag is set after the database schema has been
! 9591: ** read into internal hash tables.
! 9592: **
! 9593: ** DB_UnresetViews means that one or more views have column names that
! 9594: ** have been filled out. If the schema changes, these column names might
! 9595: ** changes and so the view will need to be reset.
! 9596: */
! 9597: #define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
! 9598: #define DB_UnresetViews 0x0002 /* Some views have defined column names */
! 9599: #define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */
! 9600:
! 9601: /*
! 9602: ** The number of different kinds of things that can be limited
! 9603: ** using the sqlite3_limit() interface.
! 9604: */
! 9605: #define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
! 9606:
! 9607: /*
! 9608: ** Lookaside malloc is a set of fixed-size buffers that can be used
! 9609: ** to satisfy small transient memory allocation requests for objects
! 9610: ** associated with a particular database connection. The use of
! 9611: ** lookaside malloc provides a significant performance enhancement
! 9612: ** (approx 10%) by avoiding numerous malloc/free requests while parsing
! 9613: ** SQL statements.
! 9614: **
! 9615: ** The Lookaside structure holds configuration information about the
! 9616: ** lookaside malloc subsystem. Each available memory allocation in
! 9617: ** the lookaside subsystem is stored on a linked list of LookasideSlot
! 9618: ** objects.
! 9619: **
! 9620: ** Lookaside allocations are only allowed for objects that are associated
! 9621: ** with a particular database connection. Hence, schema information cannot
! 9622: ** be stored in lookaside because in shared cache mode the schema information
! 9623: ** is shared by multiple database connections. Therefore, while parsing
! 9624: ** schema information, the Lookaside.bEnabled flag is cleared so that
! 9625: ** lookaside allocations are not used to construct the schema objects.
! 9626: */
! 9627: struct Lookaside {
! 9628: u16 sz; /* Size of each buffer in bytes */
! 9629: u8 bEnabled; /* False to disable new lookaside allocations */
! 9630: u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
! 9631: int nOut; /* Number of buffers currently checked out */
! 9632: int mxOut; /* Highwater mark for nOut */
! 9633: int anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
! 9634: LookasideSlot *pFree; /* List of available buffers */
! 9635: void *pStart; /* First byte of available memory space */
! 9636: void *pEnd; /* First byte past end of available space */
! 9637: };
! 9638: struct LookasideSlot {
! 9639: LookasideSlot *pNext; /* Next buffer in the list of free buffers */
! 9640: };
! 9641:
! 9642: /*
! 9643: ** A hash table for function definitions.
! 9644: **
! 9645: ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
! 9646: ** Collisions are on the FuncDef.pHash chain.
! 9647: */
! 9648: struct FuncDefHash {
! 9649: FuncDef *a[23]; /* Hash table for functions */
! 9650: };
! 9651:
! 9652: /*
! 9653: ** Each database connection is an instance of the following structure.
! 9654: **
! 9655: ** The sqlite.lastRowid records the last insert rowid generated by an
! 9656: ** insert statement. Inserts on views do not affect its value. Each
! 9657: ** trigger has its own context, so that lastRowid can be updated inside
! 9658: ** triggers as usual. The previous value will be restored once the trigger
! 9659: ** exits. Upon entering a before or instead of trigger, lastRowid is no
! 9660: ** longer (since after version 2.8.12) reset to -1.
! 9661: **
! 9662: ** The sqlite.nChange does not count changes within triggers and keeps no
! 9663: ** context. It is reset at start of sqlite3_exec.
! 9664: ** The sqlite.lsChange represents the number of changes made by the last
! 9665: ** insert, update, or delete statement. It remains constant throughout the
! 9666: ** length of a statement and is then updated by OP_SetCounts. It keeps a
! 9667: ** context stack just like lastRowid so that the count of changes
! 9668: ** within a trigger is not seen outside the trigger. Changes to views do not
! 9669: ** affect the value of lsChange.
! 9670: ** The sqlite.csChange keeps track of the number of current changes (since
! 9671: ** the last statement) and is used to update sqlite_lsChange.
! 9672: **
! 9673: ** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16
! 9674: ** store the most recent error code and, if applicable, string. The
! 9675: ** internal function sqlite3Error() is used to set these variables
! 9676: ** consistently.
! 9677: */
! 9678: struct sqlite3 {
! 9679: sqlite3_vfs *pVfs; /* OS Interface */
! 9680: int nDb; /* Number of backends currently in use */
! 9681: Db *aDb; /* All backends */
! 9682: int flags; /* Miscellaneous flags. See below */
! 9683: unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
! 9684: int errCode; /* Most recent error code (SQLITE_*) */
! 9685: int errMask; /* & result codes with this before returning */
! 9686: u8 autoCommit; /* The auto-commit flag. */
! 9687: u8 temp_store; /* 1: file 2: memory 0: default */
! 9688: u8 mallocFailed; /* True if we have seen a malloc failure */
! 9689: u8 dfltLockMode; /* Default locking-mode for attached dbs */
! 9690: signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
! 9691: u8 suppressErr; /* Do not issue error messages if true */
! 9692: u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
! 9693: int nextPagesize; /* Pagesize after VACUUM if >0 */
! 9694: int nTable; /* Number of tables in the database */
! 9695: CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
! 9696: i64 lastRowid; /* ROWID of most recent insert (see above) */
! 9697: u32 magic; /* Magic number for detect library misuse */
! 9698: int nChange; /* Value returned by sqlite3_changes() */
! 9699: int nTotalChange; /* Value returned by sqlite3_total_changes() */
! 9700: sqlite3_mutex *mutex; /* Connection mutex */
! 9701: int aLimit[SQLITE_N_LIMIT]; /* Limits */
! 9702: struct sqlite3InitInfo { /* Information used during initialization */
! 9703: int iDb; /* When back is being initialized */
! 9704: int newTnum; /* Rootpage of table being initialized */
! 9705: u8 busy; /* TRUE if currently initializing */
! 9706: u8 orphanTrigger; /* Last statement is orphaned TEMP trigger */
! 9707: } init;
! 9708: int nExtension; /* Number of loaded extensions */
! 9709: void **aExtension; /* Array of shared library handles */
! 9710: struct Vdbe *pVdbe; /* List of active virtual machines */
! 9711: int activeVdbeCnt; /* Number of VDBEs currently executing */
! 9712: int writeVdbeCnt; /* Number of active VDBEs that are writing */
! 9713: int vdbeExecCnt; /* Number of nested calls to VdbeExec() */
! 9714: void (*xTrace)(void*,const char*); /* Trace function */
! 9715: void *pTraceArg; /* Argument to the trace function */
! 9716: void (*xProfile)(void*,const char*,u64); /* Profiling function */
! 9717: void *pProfileArg; /* Argument to profile function */
! 9718: void *pCommitArg; /* Argument to xCommitCallback() */
! 9719: int (*xCommitCallback)(void*); /* Invoked at every commit. */
! 9720: void *pRollbackArg; /* Argument to xRollbackCallback() */
! 9721: void (*xRollbackCallback)(void*); /* Invoked at every commit. */
! 9722: void *pUpdateArg;
! 9723: void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
! 9724: #ifndef SQLITE_OMIT_WAL
! 9725: int (*xWalCallback)(void *, sqlite3 *, const char *, int);
! 9726: void *pWalArg;
! 9727: #endif
! 9728: void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
! 9729: void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
! 9730: void *pCollNeededArg;
! 9731: sqlite3_value *pErr; /* Most recent error message */
! 9732: char *zErrMsg; /* Most recent error message (UTF-8 encoded) */
! 9733: char *zErrMsg16; /* Most recent error message (UTF-16 encoded) */
! 9734: union {
! 9735: volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
! 9736: double notUsed1; /* Spacer */
! 9737: } u1;
! 9738: Lookaside lookaside; /* Lookaside malloc configuration */
! 9739: #ifndef SQLITE_OMIT_AUTHORIZATION
! 9740: int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
! 9741: /* Access authorization function */
! 9742: void *pAuthArg; /* 1st argument to the access auth function */
! 9743: #endif
! 9744: #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
! 9745: int (*xProgress)(void *); /* The progress callback */
! 9746: void *pProgressArg; /* Argument to the progress callback */
! 9747: int nProgressOps; /* Number of opcodes for progress callback */
! 9748: #endif
! 9749: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 9750: Hash aModule; /* populated by sqlite3_create_module() */
! 9751: VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
! 9752: VTable **aVTrans; /* Virtual tables with open transactions */
! 9753: int nVTrans; /* Allocated size of aVTrans */
! 9754: VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
! 9755: #endif
! 9756: FuncDefHash aFunc; /* Hash table of connection functions */
! 9757: Hash aCollSeq; /* All collating sequences */
! 9758: BusyHandler busyHandler; /* Busy callback */
! 9759: int busyTimeout; /* Busy handler timeout, in msec */
! 9760: Db aDbStatic[2]; /* Static space for the 2 default backends */
! 9761: Savepoint *pSavepoint; /* List of active savepoints */
! 9762: int nSavepoint; /* Number of non-transaction savepoints */
! 9763: int nStatement; /* Number of nested statement-transactions */
! 9764: u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
! 9765: i64 nDeferredCons; /* Net deferred constraints this transaction. */
! 9766: int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
! 9767:
! 9768: #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
! 9769: /* The following variables are all protected by the STATIC_MASTER
! 9770: ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
! 9771: **
! 9772: ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
! 9773: ** unlock so that it can proceed.
! 9774: **
! 9775: ** When X.pBlockingConnection==Y, that means that something that X tried
! 9776: ** tried to do recently failed with an SQLITE_LOCKED error due to locks
! 9777: ** held by Y.
! 9778: */
! 9779: sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
! 9780: sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
! 9781: void *pUnlockArg; /* Argument to xUnlockNotify */
! 9782: void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
! 9783: sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
! 9784: #endif
! 9785: };
! 9786:
! 9787: /*
! 9788: ** A macro to discover the encoding of a database.
! 9789: */
! 9790: #define ENC(db) ((db)->aDb[0].pSchema->enc)
! 9791:
! 9792: /*
! 9793: ** Possible values for the sqlite3.flags.
! 9794: */
! 9795: #define SQLITE_VdbeTrace 0x00000100 /* True to trace VDBE execution */
! 9796: #define SQLITE_InternChanges 0x00000200 /* Uncommitted Hash table changes */
! 9797: #define SQLITE_FullColNames 0x00000400 /* Show full column names on SELECT */
! 9798: #define SQLITE_ShortColNames 0x00000800 /* Show short columns names */
! 9799: #define SQLITE_CountRows 0x00001000 /* Count rows changed by INSERT, */
! 9800: /* DELETE, or UPDATE and return */
! 9801: /* the count using a callback. */
! 9802: #define SQLITE_NullCallback 0x00002000 /* Invoke the callback once if the */
! 9803: /* result set is empty */
! 9804: #define SQLITE_SqlTrace 0x00004000 /* Debug print SQL as it executes */
! 9805: #define SQLITE_VdbeListing 0x00008000 /* Debug listings of VDBE programs */
! 9806: #define SQLITE_WriteSchema 0x00010000 /* OK to update SQLITE_MASTER */
! 9807: #define SQLITE_NoReadlock 0x00020000 /* Readlocks are omitted when
! 9808: ** accessing read-only databases */
! 9809: #define SQLITE_IgnoreChecks 0x00040000 /* Do not enforce check constraints */
! 9810: #define SQLITE_ReadUncommitted 0x0080000 /* For shared-cache mode */
! 9811: #define SQLITE_LegacyFileFmt 0x00100000 /* Create new databases in format 1 */
! 9812: #define SQLITE_FullFSync 0x00200000 /* Use full fsync on the backend */
! 9813: #define SQLITE_CkptFullFSync 0x00400000 /* Use full fsync for checkpoint */
! 9814: #define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */
! 9815: #define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */
! 9816: #define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */
! 9817: #define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */
! 9818: #define SQLITE_AutoIndex 0x08000000 /* Enable automatic indexes */
! 9819: #define SQLITE_PreferBuiltin 0x10000000 /* Preference to built-in funcs */
! 9820: #define SQLITE_LoadExtension 0x20000000 /* Enable load_extension */
! 9821: #define SQLITE_EnableTrigger 0x40000000 /* True to enable triggers */
! 9822:
! 9823: /*
! 9824: ** Bits of the sqlite3.flags field that are used by the
! 9825: ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
! 9826: ** These must be the low-order bits of the flags field.
! 9827: */
! 9828: #define SQLITE_QueryFlattener 0x01 /* Disable query flattening */
! 9829: #define SQLITE_ColumnCache 0x02 /* Disable the column cache */
! 9830: #define SQLITE_IndexSort 0x04 /* Disable indexes for sorting */
! 9831: #define SQLITE_IndexSearch 0x08 /* Disable indexes for searching */
! 9832: #define SQLITE_IndexCover 0x10 /* Disable index covering table */
! 9833: #define SQLITE_GroupByOrder 0x20 /* Disable GROUPBY cover of ORDERBY */
! 9834: #define SQLITE_FactorOutConst 0x40 /* Disable factoring out constants */
! 9835: #define SQLITE_IdxRealAsInt 0x80 /* Store REAL as INT in indices */
! 9836: #define SQLITE_DistinctOpt 0x80 /* DISTINCT using indexes */
! 9837: #define SQLITE_OptMask 0xff /* Mask of all disablable opts */
! 9838:
! 9839: /*
! 9840: ** Possible values for the sqlite.magic field.
! 9841: ** The numbers are obtained at random and have no special meaning, other
! 9842: ** than being distinct from one another.
! 9843: */
! 9844: #define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */
! 9845: #define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */
! 9846: #define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */
! 9847: #define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */
! 9848: #define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */
! 9849:
! 9850: /*
! 9851: ** Each SQL function is defined by an instance of the following
! 9852: ** structure. A pointer to this structure is stored in the sqlite.aFunc
! 9853: ** hash table. When multiple functions have the same name, the hash table
! 9854: ** points to a linked list of these structures.
! 9855: */
! 9856: struct FuncDef {
! 9857: i16 nArg; /* Number of arguments. -1 means unlimited */
! 9858: u8 iPrefEnc; /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
! 9859: u8 flags; /* Some combination of SQLITE_FUNC_* */
! 9860: void *pUserData; /* User data parameter */
! 9861: FuncDef *pNext; /* Next function with same name */
! 9862: void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
! 9863: void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
! 9864: void (*xFinalize)(sqlite3_context*); /* Aggregate finalizer */
! 9865: char *zName; /* SQL name of the function. */
! 9866: FuncDef *pHash; /* Next with a different name but the same hash */
! 9867: FuncDestructor *pDestructor; /* Reference counted destructor function */
! 9868: };
! 9869:
! 9870: /*
! 9871: ** This structure encapsulates a user-function destructor callback (as
! 9872: ** configured using create_function_v2()) and a reference counter. When
! 9873: ** create_function_v2() is called to create a function with a destructor,
! 9874: ** a single object of this type is allocated. FuncDestructor.nRef is set to
! 9875: ** the number of FuncDef objects created (either 1 or 3, depending on whether
! 9876: ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
! 9877: ** member of each of the new FuncDef objects is set to point to the allocated
! 9878: ** FuncDestructor.
! 9879: **
! 9880: ** Thereafter, when one of the FuncDef objects is deleted, the reference
! 9881: ** count on this object is decremented. When it reaches 0, the destructor
! 9882: ** is invoked and the FuncDestructor structure freed.
! 9883: */
! 9884: struct FuncDestructor {
! 9885: int nRef;
! 9886: void (*xDestroy)(void *);
! 9887: void *pUserData;
! 9888: };
! 9889:
! 9890: /*
! 9891: ** Possible values for FuncDef.flags
! 9892: */
! 9893: #define SQLITE_FUNC_LIKE 0x01 /* Candidate for the LIKE optimization */
! 9894: #define SQLITE_FUNC_CASE 0x02 /* Case-sensitive LIKE-type function */
! 9895: #define SQLITE_FUNC_EPHEM 0x04 /* Ephemeral. Delete with VDBE */
! 9896: #define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */
! 9897: #define SQLITE_FUNC_PRIVATE 0x10 /* Allowed for internal use only */
! 9898: #define SQLITE_FUNC_COUNT 0x20 /* Built-in count(*) aggregate */
! 9899: #define SQLITE_FUNC_COALESCE 0x40 /* Built-in coalesce() or ifnull() function */
! 9900:
! 9901: /*
! 9902: ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
! 9903: ** used to create the initializers for the FuncDef structures.
! 9904: **
! 9905: ** FUNCTION(zName, nArg, iArg, bNC, xFunc)
! 9906: ** Used to create a scalar function definition of a function zName
! 9907: ** implemented by C function xFunc that accepts nArg arguments. The
! 9908: ** value passed as iArg is cast to a (void*) and made available
! 9909: ** as the user-data (sqlite3_user_data()) for the function. If
! 9910: ** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
! 9911: **
! 9912: ** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
! 9913: ** Used to create an aggregate function definition implemented by
! 9914: ** the C functions xStep and xFinal. The first four parameters
! 9915: ** are interpreted in the same way as the first 4 parameters to
! 9916: ** FUNCTION().
! 9917: **
! 9918: ** LIKEFUNC(zName, nArg, pArg, flags)
! 9919: ** Used to create a scalar function definition of a function zName
! 9920: ** that accepts nArg arguments and is implemented by a call to C
! 9921: ** function likeFunc. Argument pArg is cast to a (void *) and made
! 9922: ** available as the function user-data (sqlite3_user_data()). The
! 9923: ** FuncDef.flags variable is set to the value passed as the flags
! 9924: ** parameter.
! 9925: */
! 9926: #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
! 9927: {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
! 9928: SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
! 9929: #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
! 9930: {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
! 9931: pArg, 0, xFunc, 0, 0, #zName, 0, 0}
! 9932: #define LIKEFUNC(zName, nArg, arg, flags) \
! 9933: {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
! 9934: #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
! 9935: {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \
! 9936: SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
! 9937:
! 9938: /*
! 9939: ** All current savepoints are stored in a linked list starting at
! 9940: ** sqlite3.pSavepoint. The first element in the list is the most recently
! 9941: ** opened savepoint. Savepoints are added to the list by the vdbe
! 9942: ** OP_Savepoint instruction.
! 9943: */
! 9944: struct Savepoint {
! 9945: char *zName; /* Savepoint name (nul-terminated) */
! 9946: i64 nDeferredCons; /* Number of deferred fk violations */
! 9947: Savepoint *pNext; /* Parent savepoint (if any) */
! 9948: };
! 9949:
! 9950: /*
! 9951: ** The following are used as the second parameter to sqlite3Savepoint(),
! 9952: ** and as the P1 argument to the OP_Savepoint instruction.
! 9953: */
! 9954: #define SAVEPOINT_BEGIN 0
! 9955: #define SAVEPOINT_RELEASE 1
! 9956: #define SAVEPOINT_ROLLBACK 2
! 9957:
! 9958:
! 9959: /*
! 9960: ** Each SQLite module (virtual table definition) is defined by an
! 9961: ** instance of the following structure, stored in the sqlite3.aModule
! 9962: ** hash table.
! 9963: */
! 9964: struct Module {
! 9965: const sqlite3_module *pModule; /* Callback pointers */
! 9966: const char *zName; /* Name passed to create_module() */
! 9967: void *pAux; /* pAux passed to create_module() */
! 9968: void (*xDestroy)(void *); /* Module destructor function */
! 9969: };
! 9970:
! 9971: /*
! 9972: ** information about each column of an SQL table is held in an instance
! 9973: ** of this structure.
! 9974: */
! 9975: struct Column {
! 9976: char *zName; /* Name of this column */
! 9977: Expr *pDflt; /* Default value of this column */
! 9978: char *zDflt; /* Original text of the default value */
! 9979: char *zType; /* Data type for this column */
! 9980: char *zColl; /* Collating sequence. If NULL, use the default */
! 9981: u8 notNull; /* True if there is a NOT NULL constraint */
! 9982: u8 isPrimKey; /* True if this column is part of the PRIMARY KEY */
! 9983: char affinity; /* One of the SQLITE_AFF_... values */
! 9984: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 9985: u8 isHidden; /* True if this column is 'hidden' */
! 9986: #endif
! 9987: };
! 9988:
! 9989: /*
! 9990: ** A "Collating Sequence" is defined by an instance of the following
! 9991: ** structure. Conceptually, a collating sequence consists of a name and
! 9992: ** a comparison routine that defines the order of that sequence.
! 9993: **
! 9994: ** There may two separate implementations of the collation function, one
! 9995: ** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
! 9996: ** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
! 9997: ** native byte order. When a collation sequence is invoked, SQLite selects
! 9998: ** the version that will require the least expensive encoding
! 9999: ** translations, if any.
! 10000: **
! 10001: ** The CollSeq.pUser member variable is an extra parameter that passed in
! 10002: ** as the first argument to the UTF-8 comparison function, xCmp.
! 10003: ** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
! 10004: ** xCmp16.
! 10005: **
! 10006: ** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
! 10007: ** collating sequence is undefined. Indices built on an undefined
! 10008: ** collating sequence may not be read or written.
! 10009: */
! 10010: struct CollSeq {
! 10011: char *zName; /* Name of the collating sequence, UTF-8 encoded */
! 10012: u8 enc; /* Text encoding handled by xCmp() */
! 10013: void *pUser; /* First argument to xCmp() */
! 10014: int (*xCmp)(void*,int, const void*, int, const void*);
! 10015: void (*xDel)(void*); /* Destructor for pUser */
! 10016: };
! 10017:
! 10018: /*
! 10019: ** A sort order can be either ASC or DESC.
! 10020: */
! 10021: #define SQLITE_SO_ASC 0 /* Sort in ascending order */
! 10022: #define SQLITE_SO_DESC 1 /* Sort in ascending order */
! 10023:
! 10024: /*
! 10025: ** Column affinity types.
! 10026: **
! 10027: ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
! 10028: ** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
! 10029: ** the speed a little by numbering the values consecutively.
! 10030: **
! 10031: ** But rather than start with 0 or 1, we begin with 'a'. That way,
! 10032: ** when multiple affinity types are concatenated into a string and
! 10033: ** used as the P4 operand, they will be more readable.
! 10034: **
! 10035: ** Note also that the numeric types are grouped together so that testing
! 10036: ** for a numeric type is a single comparison.
! 10037: */
! 10038: #define SQLITE_AFF_TEXT 'a'
! 10039: #define SQLITE_AFF_NONE 'b'
! 10040: #define SQLITE_AFF_NUMERIC 'c'
! 10041: #define SQLITE_AFF_INTEGER 'd'
! 10042: #define SQLITE_AFF_REAL 'e'
! 10043:
! 10044: #define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
! 10045:
! 10046: /*
! 10047: ** The SQLITE_AFF_MASK values masks off the significant bits of an
! 10048: ** affinity value.
! 10049: */
! 10050: #define SQLITE_AFF_MASK 0x67
! 10051:
! 10052: /*
! 10053: ** Additional bit values that can be ORed with an affinity without
! 10054: ** changing the affinity.
! 10055: */
! 10056: #define SQLITE_JUMPIFNULL 0x08 /* jumps if either operand is NULL */
! 10057: #define SQLITE_STOREP2 0x10 /* Store result in reg[P2] rather than jump */
! 10058: #define SQLITE_NULLEQ 0x80 /* NULL=NULL */
! 10059:
! 10060: /*
! 10061: ** An object of this type is created for each virtual table present in
! 10062: ** the database schema.
! 10063: **
! 10064: ** If the database schema is shared, then there is one instance of this
! 10065: ** structure for each database connection (sqlite3*) that uses the shared
! 10066: ** schema. This is because each database connection requires its own unique
! 10067: ** instance of the sqlite3_vtab* handle used to access the virtual table
! 10068: ** implementation. sqlite3_vtab* handles can not be shared between
! 10069: ** database connections, even when the rest of the in-memory database
! 10070: ** schema is shared, as the implementation often stores the database
! 10071: ** connection handle passed to it via the xConnect() or xCreate() method
! 10072: ** during initialization internally. This database connection handle may
! 10073: ** then be used by the virtual table implementation to access real tables
! 10074: ** within the database. So that they appear as part of the callers
! 10075: ** transaction, these accesses need to be made via the same database
! 10076: ** connection as that used to execute SQL operations on the virtual table.
! 10077: **
! 10078: ** All VTable objects that correspond to a single table in a shared
! 10079: ** database schema are initially stored in a linked-list pointed to by
! 10080: ** the Table.pVTable member variable of the corresponding Table object.
! 10081: ** When an sqlite3_prepare() operation is required to access the virtual
! 10082: ** table, it searches the list for the VTable that corresponds to the
! 10083: ** database connection doing the preparing so as to use the correct
! 10084: ** sqlite3_vtab* handle in the compiled query.
! 10085: **
! 10086: ** When an in-memory Table object is deleted (for example when the
! 10087: ** schema is being reloaded for some reason), the VTable objects are not
! 10088: ** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
! 10089: ** immediately. Instead, they are moved from the Table.pVTable list to
! 10090: ** another linked list headed by the sqlite3.pDisconnect member of the
! 10091: ** corresponding sqlite3 structure. They are then deleted/xDisconnected
! 10092: ** next time a statement is prepared using said sqlite3*. This is done
! 10093: ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
! 10094: ** Refer to comments above function sqlite3VtabUnlockList() for an
! 10095: ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
! 10096: ** list without holding the corresponding sqlite3.mutex mutex.
! 10097: **
! 10098: ** The memory for objects of this type is always allocated by
! 10099: ** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
! 10100: ** the first argument.
! 10101: */
! 10102: struct VTable {
! 10103: sqlite3 *db; /* Database connection associated with this table */
! 10104: Module *pMod; /* Pointer to module implementation */
! 10105: sqlite3_vtab *pVtab; /* Pointer to vtab instance */
! 10106: int nRef; /* Number of pointers to this structure */
! 10107: u8 bConstraint; /* True if constraints are supported */
! 10108: int iSavepoint; /* Depth of the SAVEPOINT stack */
! 10109: VTable *pNext; /* Next in linked list (see above) */
! 10110: };
! 10111:
! 10112: /*
! 10113: ** Each SQL table is represented in memory by an instance of the
! 10114: ** following structure.
! 10115: **
! 10116: ** Table.zName is the name of the table. The case of the original
! 10117: ** CREATE TABLE statement is stored, but case is not significant for
! 10118: ** comparisons.
! 10119: **
! 10120: ** Table.nCol is the number of columns in this table. Table.aCol is a
! 10121: ** pointer to an array of Column structures, one for each column.
! 10122: **
! 10123: ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
! 10124: ** the column that is that key. Otherwise Table.iPKey is negative. Note
! 10125: ** that the datatype of the PRIMARY KEY must be INTEGER for this field to
! 10126: ** be set. An INTEGER PRIMARY KEY is used as the rowid for each row of
! 10127: ** the table. If a table has no INTEGER PRIMARY KEY, then a random rowid
! 10128: ** is generated for each row of the table. TF_HasPrimaryKey is set if
! 10129: ** the table has any PRIMARY KEY, INTEGER or otherwise.
! 10130: **
! 10131: ** Table.tnum is the page number for the root BTree page of the table in the
! 10132: ** database file. If Table.iDb is the index of the database table backend
! 10133: ** in sqlite.aDb[]. 0 is for the main database and 1 is for the file that
! 10134: ** holds temporary tables and indices. If TF_Ephemeral is set
! 10135: ** then the table is stored in a file that is automatically deleted
! 10136: ** when the VDBE cursor to the table is closed. In this case Table.tnum
! 10137: ** refers VDBE cursor number that holds the table open, not to the root
! 10138: ** page number. Transient tables are used to hold the results of a
! 10139: ** sub-query that appears instead of a real table name in the FROM clause
! 10140: ** of a SELECT statement.
! 10141: */
! 10142: struct Table {
! 10143: char *zName; /* Name of the table or view */
! 10144: int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
! 10145: int nCol; /* Number of columns in this table */
! 10146: Column *aCol; /* Information about each column */
! 10147: Index *pIndex; /* List of SQL indexes on this table. */
! 10148: int tnum; /* Root BTree node for this table (see note above) */
! 10149: tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
! 10150: Select *pSelect; /* NULL for tables. Points to definition if a view. */
! 10151: u16 nRef; /* Number of pointers to this Table */
! 10152: u8 tabFlags; /* Mask of TF_* values */
! 10153: u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
! 10154: FKey *pFKey; /* Linked list of all foreign keys in this table */
! 10155: char *zColAff; /* String defining the affinity of each column */
! 10156: #ifndef SQLITE_OMIT_CHECK
! 10157: Expr *pCheck; /* The AND of all CHECK constraints */
! 10158: #endif
! 10159: #ifndef SQLITE_OMIT_ALTERTABLE
! 10160: int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
! 10161: #endif
! 10162: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 10163: VTable *pVTable; /* List of VTable objects. */
! 10164: int nModuleArg; /* Number of arguments to the module */
! 10165: char **azModuleArg; /* Text of all module args. [0] is module name */
! 10166: #endif
! 10167: Trigger *pTrigger; /* List of triggers stored in pSchema */
! 10168: Schema *pSchema; /* Schema that contains this table */
! 10169: Table *pNextZombie; /* Next on the Parse.pZombieTab list */
! 10170: };
! 10171:
! 10172: /*
! 10173: ** Allowed values for Tabe.tabFlags.
! 10174: */
! 10175: #define TF_Readonly 0x01 /* Read-only system table */
! 10176: #define TF_Ephemeral 0x02 /* An ephemeral table */
! 10177: #define TF_HasPrimaryKey 0x04 /* Table has a primary key */
! 10178: #define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */
! 10179: #define TF_Virtual 0x10 /* Is a virtual table */
! 10180: #define TF_NeedMetadata 0x20 /* aCol[].zType and aCol[].pColl missing */
! 10181:
! 10182:
! 10183:
! 10184: /*
! 10185: ** Test to see whether or not a table is a virtual table. This is
! 10186: ** done as a macro so that it will be optimized out when virtual
! 10187: ** table support is omitted from the build.
! 10188: */
! 10189: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 10190: # define IsVirtual(X) (((X)->tabFlags & TF_Virtual)!=0)
! 10191: # define IsHiddenColumn(X) ((X)->isHidden)
! 10192: #else
! 10193: # define IsVirtual(X) 0
! 10194: # define IsHiddenColumn(X) 0
! 10195: #endif
! 10196:
! 10197: /*
! 10198: ** Each foreign key constraint is an instance of the following structure.
! 10199: **
! 10200: ** A foreign key is associated with two tables. The "from" table is
! 10201: ** the table that contains the REFERENCES clause that creates the foreign
! 10202: ** key. The "to" table is the table that is named in the REFERENCES clause.
! 10203: ** Consider this example:
! 10204: **
! 10205: ** CREATE TABLE ex1(
! 10206: ** a INTEGER PRIMARY KEY,
! 10207: ** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
! 10208: ** );
! 10209: **
! 10210: ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
! 10211: **
! 10212: ** Each REFERENCES clause generates an instance of the following structure
! 10213: ** which is attached to the from-table. The to-table need not exist when
! 10214: ** the from-table is created. The existence of the to-table is not checked.
! 10215: */
! 10216: struct FKey {
! 10217: Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
! 10218: FKey *pNextFrom; /* Next foreign key in pFrom */
! 10219: char *zTo; /* Name of table that the key points to (aka: Parent) */
! 10220: FKey *pNextTo; /* Next foreign key on table named zTo */
! 10221: FKey *pPrevTo; /* Previous foreign key on table named zTo */
! 10222: int nCol; /* Number of columns in this key */
! 10223: /* EV: R-30323-21917 */
! 10224: u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
! 10225: u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
! 10226: Trigger *apTrigger[2]; /* Triggers for aAction[] actions */
! 10227: struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
! 10228: int iFrom; /* Index of column in pFrom */
! 10229: char *zCol; /* Name of column in zTo. If 0 use PRIMARY KEY */
! 10230: } aCol[1]; /* One entry for each of nCol column s */
! 10231: };
! 10232:
! 10233: /*
! 10234: ** SQLite supports many different ways to resolve a constraint
! 10235: ** error. ROLLBACK processing means that a constraint violation
! 10236: ** causes the operation in process to fail and for the current transaction
! 10237: ** to be rolled back. ABORT processing means the operation in process
! 10238: ** fails and any prior changes from that one operation are backed out,
! 10239: ** but the transaction is not rolled back. FAIL processing means that
! 10240: ** the operation in progress stops and returns an error code. But prior
! 10241: ** changes due to the same operation are not backed out and no rollback
! 10242: ** occurs. IGNORE means that the particular row that caused the constraint
! 10243: ** error is not inserted or updated. Processing continues and no error
! 10244: ** is returned. REPLACE means that preexisting database rows that caused
! 10245: ** a UNIQUE constraint violation are removed so that the new insert or
! 10246: ** update can proceed. Processing continues and no error is reported.
! 10247: **
! 10248: ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
! 10249: ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
! 10250: ** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
! 10251: ** key is set to NULL. CASCADE means that a DELETE or UPDATE of the
! 10252: ** referenced table row is propagated into the row that holds the
! 10253: ** foreign key.
! 10254: **
! 10255: ** The following symbolic values are used to record which type
! 10256: ** of action to take.
! 10257: */
! 10258: #define OE_None 0 /* There is no constraint to check */
! 10259: #define OE_Rollback 1 /* Fail the operation and rollback the transaction */
! 10260: #define OE_Abort 2 /* Back out changes but do no rollback transaction */
! 10261: #define OE_Fail 3 /* Stop the operation but leave all prior changes */
! 10262: #define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
! 10263: #define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
! 10264:
! 10265: #define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
! 10266: #define OE_SetNull 7 /* Set the foreign key value to NULL */
! 10267: #define OE_SetDflt 8 /* Set the foreign key value to its default */
! 10268: #define OE_Cascade 9 /* Cascade the changes */
! 10269:
! 10270: #define OE_Default 99 /* Do whatever the default action is */
! 10271:
! 10272:
! 10273: /*
! 10274: ** An instance of the following structure is passed as the first
! 10275: ** argument to sqlite3VdbeKeyCompare and is used to control the
! 10276: ** comparison of the two index keys.
! 10277: */
! 10278: struct KeyInfo {
! 10279: sqlite3 *db; /* The database connection */
! 10280: u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
! 10281: u16 nField; /* Number of entries in aColl[] */
! 10282: u8 *aSortOrder; /* Sort order for each column. May be NULL */
! 10283: CollSeq *aColl[1]; /* Collating sequence for each term of the key */
! 10284: };
! 10285:
! 10286: /*
! 10287: ** An instance of the following structure holds information about a
! 10288: ** single index record that has already been parsed out into individual
! 10289: ** values.
! 10290: **
! 10291: ** A record is an object that contains one or more fields of data.
! 10292: ** Records are used to store the content of a table row and to store
! 10293: ** the key of an index. A blob encoding of a record is created by
! 10294: ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
! 10295: ** OP_Column opcode.
! 10296: **
! 10297: ** This structure holds a record that has already been disassembled
! 10298: ** into its constituent fields.
! 10299: */
! 10300: struct UnpackedRecord {
! 10301: KeyInfo *pKeyInfo; /* Collation and sort-order information */
! 10302: u16 nField; /* Number of entries in apMem[] */
! 10303: u8 flags; /* Boolean settings. UNPACKED_... below */
! 10304: i64 rowid; /* Used by UNPACKED_PREFIX_SEARCH */
! 10305: Mem *aMem; /* Values */
! 10306: };
! 10307:
! 10308: /*
! 10309: ** Allowed values of UnpackedRecord.flags
! 10310: */
! 10311: #define UNPACKED_INCRKEY 0x01 /* Make this key an epsilon larger */
! 10312: #define UNPACKED_PREFIX_MATCH 0x02 /* A prefix match is considered OK */
! 10313: #define UNPACKED_PREFIX_SEARCH 0x04 /* Ignore final (rowid) field */
! 10314:
! 10315: /*
! 10316: ** Each SQL index is represented in memory by an
! 10317: ** instance of the following structure.
! 10318: **
! 10319: ** The columns of the table that are to be indexed are described
! 10320: ** by the aiColumn[] field of this structure. For example, suppose
! 10321: ** we have the following table and index:
! 10322: **
! 10323: ** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
! 10324: ** CREATE INDEX Ex2 ON Ex1(c3,c1);
! 10325: **
! 10326: ** In the Table structure describing Ex1, nCol==3 because there are
! 10327: ** three columns in the table. In the Index structure describing
! 10328: ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
! 10329: ** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
! 10330: ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
! 10331: ** The second column to be indexed (c1) has an index of 0 in
! 10332: ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
! 10333: **
! 10334: ** The Index.onError field determines whether or not the indexed columns
! 10335: ** must be unique and what to do if they are not. When Index.onError=OE_None,
! 10336: ** it means this is not a unique index. Otherwise it is a unique index
! 10337: ** and the value of Index.onError indicate the which conflict resolution
! 10338: ** algorithm to employ whenever an attempt is made to insert a non-unique
! 10339: ** element.
! 10340: */
! 10341: struct Index {
! 10342: char *zName; /* Name of this index */
! 10343: int nColumn; /* Number of columns in the table used by this index */
! 10344: int *aiColumn; /* Which columns are used by this index. 1st is 0 */
! 10345: tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
! 10346: Table *pTable; /* The SQL table being indexed */
! 10347: int tnum; /* Page containing root of this index in database file */
! 10348: u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
! 10349: u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
! 10350: u8 bUnordered; /* Use this index for == or IN queries only */
! 10351: char *zColAff; /* String defining the affinity of each column */
! 10352: Index *pNext; /* The next index associated with the same table */
! 10353: Schema *pSchema; /* Schema containing this index */
! 10354: u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
! 10355: char **azColl; /* Array of collation sequence names for index */
! 10356: #ifdef SQLITE_ENABLE_STAT3
! 10357: int nSample; /* Number of elements in aSample[] */
! 10358: tRowcnt avgEq; /* Average nEq value for key values not in aSample */
! 10359: IndexSample *aSample; /* Samples of the left-most key */
! 10360: #endif
! 10361: };
! 10362:
! 10363: /*
! 10364: ** Each sample stored in the sqlite_stat3 table is represented in memory
! 10365: ** using a structure of this type. See documentation at the top of the
! 10366: ** analyze.c source file for additional information.
! 10367: */
! 10368: struct IndexSample {
! 10369: union {
! 10370: char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
! 10371: double r; /* Value if eType is SQLITE_FLOAT */
! 10372: i64 i; /* Value if eType is SQLITE_INTEGER */
! 10373: } u;
! 10374: u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
! 10375: int nByte; /* Size in byte of text or blob. */
! 10376: tRowcnt nEq; /* Est. number of rows where the key equals this sample */
! 10377: tRowcnt nLt; /* Est. number of rows where key is less than this sample */
! 10378: tRowcnt nDLt; /* Est. number of distinct keys less than this sample */
! 10379: };
! 10380:
! 10381: /*
! 10382: ** Each token coming out of the lexer is an instance of
! 10383: ** this structure. Tokens are also used as part of an expression.
! 10384: **
! 10385: ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
! 10386: ** may contain random values. Do not make any assumptions about Token.dyn
! 10387: ** and Token.n when Token.z==0.
! 10388: */
! 10389: struct Token {
! 10390: const char *z; /* Text of the token. Not NULL-terminated! */
! 10391: unsigned int n; /* Number of characters in this token */
! 10392: };
! 10393:
! 10394: /*
! 10395: ** An instance of this structure contains information needed to generate
! 10396: ** code for a SELECT that contains aggregate functions.
! 10397: **
! 10398: ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
! 10399: ** pointer to this structure. The Expr.iColumn field is the index in
! 10400: ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
! 10401: ** code for that node.
! 10402: **
! 10403: ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
! 10404: ** original Select structure that describes the SELECT statement. These
! 10405: ** fields do not need to be freed when deallocating the AggInfo structure.
! 10406: */
! 10407: struct AggInfo {
! 10408: u8 directMode; /* Direct rendering mode means take data directly
! 10409: ** from source tables rather than from accumulators */
! 10410: u8 useSortingIdx; /* In direct mode, reference the sorting index rather
! 10411: ** than the source table */
! 10412: int sortingIdx; /* Cursor number of the sorting index */
! 10413: int sortingIdxPTab; /* Cursor number of pseudo-table */
! 10414: ExprList *pGroupBy; /* The group by clause */
! 10415: int nSortingColumn; /* Number of columns in the sorting index */
! 10416: struct AggInfo_col { /* For each column used in source tables */
! 10417: Table *pTab; /* Source table */
! 10418: int iTable; /* Cursor number of the source table */
! 10419: int iColumn; /* Column number within the source table */
! 10420: int iSorterColumn; /* Column number in the sorting index */
! 10421: int iMem; /* Memory location that acts as accumulator */
! 10422: Expr *pExpr; /* The original expression */
! 10423: } *aCol;
! 10424: int nColumn; /* Number of used entries in aCol[] */
! 10425: int nColumnAlloc; /* Number of slots allocated for aCol[] */
! 10426: int nAccumulator; /* Number of columns that show through to the output.
! 10427: ** Additional columns are used only as parameters to
! 10428: ** aggregate functions */
! 10429: struct AggInfo_func { /* For each aggregate function */
! 10430: Expr *pExpr; /* Expression encoding the function */
! 10431: FuncDef *pFunc; /* The aggregate function implementation */
! 10432: int iMem; /* Memory location that acts as accumulator */
! 10433: int iDistinct; /* Ephemeral table used to enforce DISTINCT */
! 10434: } *aFunc;
! 10435: int nFunc; /* Number of entries in aFunc[] */
! 10436: int nFuncAlloc; /* Number of slots allocated for aFunc[] */
! 10437: };
! 10438:
! 10439: /*
! 10440: ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
! 10441: ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
! 10442: ** than 32767 we have to make it 32-bit. 16-bit is preferred because
! 10443: ** it uses less memory in the Expr object, which is a big memory user
! 10444: ** in systems with lots of prepared statements. And few applications
! 10445: ** need more than about 10 or 20 variables. But some extreme users want
! 10446: ** to have prepared statements with over 32767 variables, and for them
! 10447: ** the option is available (at compile-time).
! 10448: */
! 10449: #if SQLITE_MAX_VARIABLE_NUMBER<=32767
! 10450: typedef i16 ynVar;
! 10451: #else
! 10452: typedef int ynVar;
! 10453: #endif
! 10454:
! 10455: /*
! 10456: ** Each node of an expression in the parse tree is an instance
! 10457: ** of this structure.
! 10458: **
! 10459: ** Expr.op is the opcode. The integer parser token codes are reused
! 10460: ** as opcodes here. For example, the parser defines TK_GE to be an integer
! 10461: ** code representing the ">=" operator. This same integer code is reused
! 10462: ** to represent the greater-than-or-equal-to operator in the expression
! 10463: ** tree.
! 10464: **
! 10465: ** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
! 10466: ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
! 10467: ** the expression is a variable (TK_VARIABLE), then Expr.token contains the
! 10468: ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
! 10469: ** then Expr.token contains the name of the function.
! 10470: **
! 10471: ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
! 10472: ** binary operator. Either or both may be NULL.
! 10473: **
! 10474: ** Expr.x.pList is a list of arguments if the expression is an SQL function,
! 10475: ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
! 10476: ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
! 10477: ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
! 10478: ** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
! 10479: ** valid.
! 10480: **
! 10481: ** An expression of the form ID or ID.ID refers to a column in a table.
! 10482: ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
! 10483: ** the integer cursor number of a VDBE cursor pointing to that table and
! 10484: ** Expr.iColumn is the column number for the specific column. If the
! 10485: ** expression is used as a result in an aggregate SELECT, then the
! 10486: ** value is also stored in the Expr.iAgg column in the aggregate so that
! 10487: ** it can be accessed after all aggregates are computed.
! 10488: **
! 10489: ** If the expression is an unbound variable marker (a question mark
! 10490: ** character '?' in the original SQL) then the Expr.iTable holds the index
! 10491: ** number for that variable.
! 10492: **
! 10493: ** If the expression is a subquery then Expr.iColumn holds an integer
! 10494: ** register number containing the result of the subquery. If the
! 10495: ** subquery gives a constant result, then iTable is -1. If the subquery
! 10496: ** gives a different answer at different times during statement processing
! 10497: ** then iTable is the address of a subroutine that computes the subquery.
! 10498: **
! 10499: ** If the Expr is of type OP_Column, and the table it is selecting from
! 10500: ** is a disk table or the "old.*" pseudo-table, then pTab points to the
! 10501: ** corresponding table definition.
! 10502: **
! 10503: ** ALLOCATION NOTES:
! 10504: **
! 10505: ** Expr objects can use a lot of memory space in database schema. To
! 10506: ** help reduce memory requirements, sometimes an Expr object will be
! 10507: ** truncated. And to reduce the number of memory allocations, sometimes
! 10508: ** two or more Expr objects will be stored in a single memory allocation,
! 10509: ** together with Expr.zToken strings.
! 10510: **
! 10511: ** If the EP_Reduced and EP_TokenOnly flags are set when
! 10512: ** an Expr object is truncated. When EP_Reduced is set, then all
! 10513: ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
! 10514: ** are contained within the same memory allocation. Note, however, that
! 10515: ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
! 10516: ** allocated, regardless of whether or not EP_Reduced is set.
! 10517: */
! 10518: struct Expr {
! 10519: u8 op; /* Operation performed by this node */
! 10520: char affinity; /* The affinity of the column or 0 if not a column */
! 10521: u16 flags; /* Various flags. EP_* See below */
! 10522: union {
! 10523: char *zToken; /* Token value. Zero terminated and dequoted */
! 10524: int iValue; /* Non-negative integer value if EP_IntValue */
! 10525: } u;
! 10526:
! 10527: /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
! 10528: ** space is allocated for the fields below this point. An attempt to
! 10529: ** access them will result in a segfault or malfunction.
! 10530: *********************************************************************/
! 10531:
! 10532: Expr *pLeft; /* Left subnode */
! 10533: Expr *pRight; /* Right subnode */
! 10534: union {
! 10535: ExprList *pList; /* Function arguments or in "<expr> IN (<expr-list)" */
! 10536: Select *pSelect; /* Used for sub-selects and "<expr> IN (<select>)" */
! 10537: } x;
! 10538: CollSeq *pColl; /* The collation type of the column or 0 */
! 10539:
! 10540: /* If the EP_Reduced flag is set in the Expr.flags mask, then no
! 10541: ** space is allocated for the fields below this point. An attempt to
! 10542: ** access them will result in a segfault or malfunction.
! 10543: *********************************************************************/
! 10544:
! 10545: int iTable; /* TK_COLUMN: cursor number of table holding column
! 10546: ** TK_REGISTER: register number
! 10547: ** TK_TRIGGER: 1 -> new, 0 -> old */
! 10548: ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
! 10549: ** TK_VARIABLE: variable number (always >= 1). */
! 10550: i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
! 10551: i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
! 10552: u8 flags2; /* Second set of flags. EP2_... */
! 10553: u8 op2; /* If a TK_REGISTER, the original value of Expr.op */
! 10554: AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
! 10555: Table *pTab; /* Table for TK_COLUMN expressions. */
! 10556: #if SQLITE_MAX_EXPR_DEPTH>0
! 10557: int nHeight; /* Height of the tree headed by this node */
! 10558: #endif
! 10559: };
! 10560:
! 10561: /*
! 10562: ** The following are the meanings of bits in the Expr.flags field.
! 10563: */
! 10564: #define EP_FromJoin 0x0001 /* Originated in ON or USING clause of a join */
! 10565: #define EP_Agg 0x0002 /* Contains one or more aggregate functions */
! 10566: #define EP_Resolved 0x0004 /* IDs have been resolved to COLUMNs */
! 10567: #define EP_Error 0x0008 /* Expression contains one or more errors */
! 10568: #define EP_Distinct 0x0010 /* Aggregate function with DISTINCT keyword */
! 10569: #define EP_VarSelect 0x0020 /* pSelect is correlated, not constant */
! 10570: #define EP_DblQuoted 0x0040 /* token.z was originally in "..." */
! 10571: #define EP_InfixFunc 0x0080 /* True for an infix function: LIKE, GLOB, etc */
! 10572: #define EP_ExpCollate 0x0100 /* Collating sequence specified explicitly */
! 10573: #define EP_FixedDest 0x0200 /* Result needed in a specific register */
! 10574: #define EP_IntValue 0x0400 /* Integer value contained in u.iValue */
! 10575: #define EP_xIsSelect 0x0800 /* x.pSelect is valid (otherwise x.pList is) */
! 10576: #define EP_Hint 0x1000 /* Optimizer hint. Not required for correctness */
! 10577: #define EP_Reduced 0x2000 /* Expr struct is EXPR_REDUCEDSIZE bytes only */
! 10578: #define EP_TokenOnly 0x4000 /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
! 10579: #define EP_Static 0x8000 /* Held in memory not obtained from malloc() */
! 10580:
! 10581: /*
! 10582: ** The following are the meanings of bits in the Expr.flags2 field.
! 10583: */
! 10584: #define EP2_MallocedToken 0x0001 /* Need to sqlite3DbFree() Expr.zToken */
! 10585: #define EP2_Irreducible 0x0002 /* Cannot EXPRDUP_REDUCE this Expr */
! 10586:
! 10587: /*
! 10588: ** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
! 10589: ** flag on an expression structure. This flag is used for VV&A only. The
! 10590: ** routine is implemented as a macro that only works when in debugging mode,
! 10591: ** so as not to burden production code.
! 10592: */
! 10593: #ifdef SQLITE_DEBUG
! 10594: # define ExprSetIrreducible(X) (X)->flags2 |= EP2_Irreducible
! 10595: #else
! 10596: # define ExprSetIrreducible(X)
! 10597: #endif
! 10598:
! 10599: /*
! 10600: ** These macros can be used to test, set, or clear bits in the
! 10601: ** Expr.flags field.
! 10602: */
! 10603: #define ExprHasProperty(E,P) (((E)->flags&(P))==(P))
! 10604: #define ExprHasAnyProperty(E,P) (((E)->flags&(P))!=0)
! 10605: #define ExprSetProperty(E,P) (E)->flags|=(P)
! 10606: #define ExprClearProperty(E,P) (E)->flags&=~(P)
! 10607:
! 10608: /*
! 10609: ** Macros to determine the number of bytes required by a normal Expr
! 10610: ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
! 10611: ** and an Expr struct with the EP_TokenOnly flag set.
! 10612: */
! 10613: #define EXPR_FULLSIZE sizeof(Expr) /* Full size */
! 10614: #define EXPR_REDUCEDSIZE offsetof(Expr,iTable) /* Common features */
! 10615: #define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */
! 10616:
! 10617: /*
! 10618: ** Flags passed to the sqlite3ExprDup() function. See the header comment
! 10619: ** above sqlite3ExprDup() for details.
! 10620: */
! 10621: #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
! 10622:
! 10623: /*
! 10624: ** A list of expressions. Each expression may optionally have a
! 10625: ** name. An expr/name combination can be used in several ways, such
! 10626: ** as the list of "expr AS ID" fields following a "SELECT" or in the
! 10627: ** list of "ID = expr" items in an UPDATE. A list of expressions can
! 10628: ** also be used as the argument to a function, in which case the a.zName
! 10629: ** field is not used.
! 10630: */
! 10631: struct ExprList {
! 10632: int nExpr; /* Number of expressions on the list */
! 10633: int nAlloc; /* Number of entries allocated below */
! 10634: int iECursor; /* VDBE Cursor associated with this ExprList */
! 10635: struct ExprList_item {
! 10636: Expr *pExpr; /* The list of expressions */
! 10637: char *zName; /* Token associated with this expression */
! 10638: char *zSpan; /* Original text of the expression */
! 10639: u8 sortOrder; /* 1 for DESC or 0 for ASC */
! 10640: u8 done; /* A flag to indicate when processing is finished */
! 10641: u16 iOrderByCol; /* For ORDER BY, column number in result set */
! 10642: u16 iAlias; /* Index into Parse.aAlias[] for zName */
! 10643: } *a; /* One entry for each expression */
! 10644: };
! 10645:
! 10646: /*
! 10647: ** An instance of this structure is used by the parser to record both
! 10648: ** the parse tree for an expression and the span of input text for an
! 10649: ** expression.
! 10650: */
! 10651: struct ExprSpan {
! 10652: Expr *pExpr; /* The expression parse tree */
! 10653: const char *zStart; /* First character of input text */
! 10654: const char *zEnd; /* One character past the end of input text */
! 10655: };
! 10656:
! 10657: /*
! 10658: ** An instance of this structure can hold a simple list of identifiers,
! 10659: ** such as the list "a,b,c" in the following statements:
! 10660: **
! 10661: ** INSERT INTO t(a,b,c) VALUES ...;
! 10662: ** CREATE INDEX idx ON t(a,b,c);
! 10663: ** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
! 10664: **
! 10665: ** The IdList.a.idx field is used when the IdList represents the list of
! 10666: ** column names after a table name in an INSERT statement. In the statement
! 10667: **
! 10668: ** INSERT INTO t(a,b,c) ...
! 10669: **
! 10670: ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
! 10671: */
! 10672: struct IdList {
! 10673: struct IdList_item {
! 10674: char *zName; /* Name of the identifier */
! 10675: int idx; /* Index in some Table.aCol[] of a column named zName */
! 10676: } *a;
! 10677: int nId; /* Number of identifiers on the list */
! 10678: int nAlloc; /* Number of entries allocated for a[] below */
! 10679: };
! 10680:
! 10681: /*
! 10682: ** The bitmask datatype defined below is used for various optimizations.
! 10683: **
! 10684: ** Changing this from a 64-bit to a 32-bit type limits the number of
! 10685: ** tables in a join to 32 instead of 64. But it also reduces the size
! 10686: ** of the library by 738 bytes on ix86.
! 10687: */
! 10688: typedef u64 Bitmask;
! 10689:
! 10690: /*
! 10691: ** The number of bits in a Bitmask. "BMS" means "BitMask Size".
! 10692: */
! 10693: #define BMS ((int)(sizeof(Bitmask)*8))
! 10694:
! 10695: /*
! 10696: ** The following structure describes the FROM clause of a SELECT statement.
! 10697: ** Each table or subquery in the FROM clause is a separate element of
! 10698: ** the SrcList.a[] array.
! 10699: **
! 10700: ** With the addition of multiple database support, the following structure
! 10701: ** can also be used to describe a particular table such as the table that
! 10702: ** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
! 10703: ** such a table must be a simple name: ID. But in SQLite, the table can
! 10704: ** now be identified by a database name, a dot, then the table name: ID.ID.
! 10705: **
! 10706: ** The jointype starts out showing the join type between the current table
! 10707: ** and the next table on the list. The parser builds the list this way.
! 10708: ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
! 10709: ** jointype expresses the join between the table and the previous table.
! 10710: **
! 10711: ** In the colUsed field, the high-order bit (bit 63) is set if the table
! 10712: ** contains more than 63 columns and the 64-th or later column is used.
! 10713: */
! 10714: struct SrcList {
! 10715: i16 nSrc; /* Number of tables or subqueries in the FROM clause */
! 10716: i16 nAlloc; /* Number of entries allocated in a[] below */
! 10717: struct SrcList_item {
! 10718: char *zDatabase; /* Name of database holding this table */
! 10719: char *zName; /* Name of the table */
! 10720: char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
! 10721: Table *pTab; /* An SQL table corresponding to zName */
! 10722: Select *pSelect; /* A SELECT statement used in place of a table name */
! 10723: int addrFillSub; /* Address of subroutine to manifest a subquery */
! 10724: int regReturn; /* Register holding return address of addrFillSub */
! 10725: u8 jointype; /* Type of join between this able and the previous */
! 10726: u8 notIndexed; /* True if there is a NOT INDEXED clause */
! 10727: u8 isCorrelated; /* True if sub-query is correlated */
! 10728: #ifndef SQLITE_OMIT_EXPLAIN
! 10729: u8 iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */
! 10730: #endif
! 10731: int iCursor; /* The VDBE cursor number used to access this table */
! 10732: Expr *pOn; /* The ON clause of a join */
! 10733: IdList *pUsing; /* The USING clause of a join */
! 10734: Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
! 10735: char *zIndex; /* Identifier from "INDEXED BY <zIndex>" clause */
! 10736: Index *pIndex; /* Index structure corresponding to zIndex, if any */
! 10737: } a[1]; /* One entry for each identifier on the list */
! 10738: };
! 10739:
! 10740: /*
! 10741: ** Permitted values of the SrcList.a.jointype field
! 10742: */
! 10743: #define JT_INNER 0x0001 /* Any kind of inner or cross join */
! 10744: #define JT_CROSS 0x0002 /* Explicit use of the CROSS keyword */
! 10745: #define JT_NATURAL 0x0004 /* True for a "natural" join */
! 10746: #define JT_LEFT 0x0008 /* Left outer join */
! 10747: #define JT_RIGHT 0x0010 /* Right outer join */
! 10748: #define JT_OUTER 0x0020 /* The "OUTER" keyword is present */
! 10749: #define JT_ERROR 0x0040 /* unknown or unsupported join type */
! 10750:
! 10751:
! 10752: /*
! 10753: ** A WherePlan object holds information that describes a lookup
! 10754: ** strategy.
! 10755: **
! 10756: ** This object is intended to be opaque outside of the where.c module.
! 10757: ** It is included here only so that that compiler will know how big it
! 10758: ** is. None of the fields in this object should be used outside of
! 10759: ** the where.c module.
! 10760: **
! 10761: ** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
! 10762: ** pTerm is only used when wsFlags&WHERE_MULTI_OR is true. And pVtabIdx
! 10763: ** is only used when wsFlags&WHERE_VIRTUALTABLE is true. It is never the
! 10764: ** case that more than one of these conditions is true.
! 10765: */
! 10766: struct WherePlan {
! 10767: u32 wsFlags; /* WHERE_* flags that describe the strategy */
! 10768: u32 nEq; /* Number of == constraints */
! 10769: double nRow; /* Estimated number of rows (for EQP) */
! 10770: union {
! 10771: Index *pIdx; /* Index when WHERE_INDEXED is true */
! 10772: struct WhereTerm *pTerm; /* WHERE clause term for OR-search */
! 10773: sqlite3_index_info *pVtabIdx; /* Virtual table index to use */
! 10774: } u;
! 10775: };
! 10776:
! 10777: /*
! 10778: ** For each nested loop in a WHERE clause implementation, the WhereInfo
! 10779: ** structure contains a single instance of this structure. This structure
! 10780: ** is intended to be private the the where.c module and should not be
! 10781: ** access or modified by other modules.
! 10782: **
! 10783: ** The pIdxInfo field is used to help pick the best index on a
! 10784: ** virtual table. The pIdxInfo pointer contains indexing
! 10785: ** information for the i-th table in the FROM clause before reordering.
! 10786: ** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
! 10787: ** All other information in the i-th WhereLevel object for the i-th table
! 10788: ** after FROM clause ordering.
! 10789: */
! 10790: struct WhereLevel {
! 10791: WherePlan plan; /* query plan for this element of the FROM clause */
! 10792: int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
! 10793: int iTabCur; /* The VDBE cursor used to access the table */
! 10794: int iIdxCur; /* The VDBE cursor used to access pIdx */
! 10795: int addrBrk; /* Jump here to break out of the loop */
! 10796: int addrNxt; /* Jump here to start the next IN combination */
! 10797: int addrCont; /* Jump here to continue with the next loop cycle */
! 10798: int addrFirst; /* First instruction of interior of the loop */
! 10799: u8 iFrom; /* Which entry in the FROM clause */
! 10800: u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */
! 10801: int p1, p2; /* Operands of the opcode used to ends the loop */
! 10802: union { /* Information that depends on plan.wsFlags */
! 10803: struct {
! 10804: int nIn; /* Number of entries in aInLoop[] */
! 10805: struct InLoop {
! 10806: int iCur; /* The VDBE cursor used by this IN operator */
! 10807: int addrInTop; /* Top of the IN loop */
! 10808: } *aInLoop; /* Information about each nested IN operator */
! 10809: } in; /* Used when plan.wsFlags&WHERE_IN_ABLE */
! 10810: } u;
! 10811:
! 10812: /* The following field is really not part of the current level. But
! 10813: ** we need a place to cache virtual table index information for each
! 10814: ** virtual table in the FROM clause and the WhereLevel structure is
! 10815: ** a convenient place since there is one WhereLevel for each FROM clause
! 10816: ** element.
! 10817: */
! 10818: sqlite3_index_info *pIdxInfo; /* Index info for n-th source table */
! 10819: };
! 10820:
! 10821: /*
! 10822: ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
! 10823: ** and the WhereInfo.wctrlFlags member.
! 10824: */
! 10825: #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
! 10826: #define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
! 10827: #define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
! 10828: #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
! 10829: #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */
! 10830: #define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */
! 10831: #define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */
! 10832: #define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */
! 10833: #define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */
! 10834:
! 10835: /*
! 10836: ** The WHERE clause processing routine has two halves. The
! 10837: ** first part does the start of the WHERE loop and the second
! 10838: ** half does the tail of the WHERE loop. An instance of
! 10839: ** this structure is returned by the first half and passed
! 10840: ** into the second half to give some continuity.
! 10841: */
! 10842: struct WhereInfo {
! 10843: Parse *pParse; /* Parsing and code generating context */
! 10844: u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
! 10845: u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE or DELETE */
! 10846: u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
! 10847: u8 eDistinct;
! 10848: SrcList *pTabList; /* List of tables in the join */
! 10849: int iTop; /* The very beginning of the WHERE loop */
! 10850: int iContinue; /* Jump here to continue with next record */
! 10851: int iBreak; /* Jump here to break out of the loop */
! 10852: int nLevel; /* Number of nested loop */
! 10853: struct WhereClause *pWC; /* Decomposition of the WHERE clause */
! 10854: double savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
! 10855: double nRowOut; /* Estimated number of output rows */
! 10856: WhereLevel a[1]; /* Information about each nest loop in WHERE */
! 10857: };
! 10858:
! 10859: #define WHERE_DISTINCT_UNIQUE 1
! 10860: #define WHERE_DISTINCT_ORDERED 2
! 10861:
! 10862: /*
! 10863: ** A NameContext defines a context in which to resolve table and column
! 10864: ** names. The context consists of a list of tables (the pSrcList) field and
! 10865: ** a list of named expression (pEList). The named expression list may
! 10866: ** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
! 10867: ** to the table being operated on by INSERT, UPDATE, or DELETE. The
! 10868: ** pEList corresponds to the result set of a SELECT and is NULL for
! 10869: ** other statements.
! 10870: **
! 10871: ** NameContexts can be nested. When resolving names, the inner-most
! 10872: ** context is searched first. If no match is found, the next outer
! 10873: ** context is checked. If there is still no match, the next context
! 10874: ** is checked. This process continues until either a match is found
! 10875: ** or all contexts are check. When a match is found, the nRef member of
! 10876: ** the context containing the match is incremented.
! 10877: **
! 10878: ** Each subquery gets a new NameContext. The pNext field points to the
! 10879: ** NameContext in the parent query. Thus the process of scanning the
! 10880: ** NameContext list corresponds to searching through successively outer
! 10881: ** subqueries looking for a match.
! 10882: */
! 10883: struct NameContext {
! 10884: Parse *pParse; /* The parser */
! 10885: SrcList *pSrcList; /* One or more tables used to resolve names */
! 10886: ExprList *pEList; /* Optional list of named expressions */
! 10887: int nRef; /* Number of names resolved by this context */
! 10888: int nErr; /* Number of errors encountered while resolving names */
! 10889: u8 allowAgg; /* Aggregate functions allowed here */
! 10890: u8 hasAgg; /* True if aggregates are seen */
! 10891: u8 isCheck; /* True if resolving names in a CHECK constraint */
! 10892: int nDepth; /* Depth of subquery recursion. 1 for no recursion */
! 10893: AggInfo *pAggInfo; /* Information about aggregates at this level */
! 10894: NameContext *pNext; /* Next outer name context. NULL for outermost */
! 10895: };
! 10896:
! 10897: /*
! 10898: ** An instance of the following structure contains all information
! 10899: ** needed to generate code for a single SELECT statement.
! 10900: **
! 10901: ** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0.
! 10902: ** If there is a LIMIT clause, the parser sets nLimit to the value of the
! 10903: ** limit and nOffset to the value of the offset (or 0 if there is not
! 10904: ** offset). But later on, nLimit and nOffset become the memory locations
! 10905: ** in the VDBE that record the limit and offset counters.
! 10906: **
! 10907: ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
! 10908: ** These addresses must be stored so that we can go back and fill in
! 10909: ** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
! 10910: ** the number of columns in P2 can be computed at the same time
! 10911: ** as the OP_OpenEphm instruction is coded because not
! 10912: ** enough information about the compound query is known at that point.
! 10913: ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
! 10914: ** for the result set. The KeyInfo for addrOpenTran[2] contains collating
! 10915: ** sequences for the ORDER BY clause.
! 10916: */
! 10917: struct Select {
! 10918: ExprList *pEList; /* The fields of the result */
! 10919: u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
! 10920: char affinity; /* MakeRecord with this affinity for SRT_Set */
! 10921: u16 selFlags; /* Various SF_* values */
! 10922: SrcList *pSrc; /* The FROM clause */
! 10923: Expr *pWhere; /* The WHERE clause */
! 10924: ExprList *pGroupBy; /* The GROUP BY clause */
! 10925: Expr *pHaving; /* The HAVING clause */
! 10926: ExprList *pOrderBy; /* The ORDER BY clause */
! 10927: Select *pPrior; /* Prior select in a compound select statement */
! 10928: Select *pNext; /* Next select to the left in a compound */
! 10929: Select *pRightmost; /* Right-most select in a compound select statement */
! 10930: Expr *pLimit; /* LIMIT expression. NULL means not used. */
! 10931: Expr *pOffset; /* OFFSET expression. NULL means not used. */
! 10932: int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
! 10933: int addrOpenEphm[3]; /* OP_OpenEphem opcodes related to this select */
! 10934: double nSelectRow; /* Estimated number of result rows */
! 10935: };
! 10936:
! 10937: /*
! 10938: ** Allowed values for Select.selFlags. The "SF" prefix stands for
! 10939: ** "Select Flag".
! 10940: */
! 10941: #define SF_Distinct 0x01 /* Output should be DISTINCT */
! 10942: #define SF_Resolved 0x02 /* Identifiers have been resolved */
! 10943: #define SF_Aggregate 0x04 /* Contains aggregate functions */
! 10944: #define SF_UsesEphemeral 0x08 /* Uses the OpenEphemeral opcode */
! 10945: #define SF_Expanded 0x10 /* sqlite3SelectExpand() called on this */
! 10946: #define SF_HasTypeInfo 0x20 /* FROM subqueries have Table metadata */
! 10947: #define SF_UseSorter 0x40 /* Sort using a sorter */
! 10948:
! 10949:
! 10950: /*
! 10951: ** The results of a select can be distributed in several ways. The
! 10952: ** "SRT" prefix means "SELECT Result Type".
! 10953: */
! 10954: #define SRT_Union 1 /* Store result as keys in an index */
! 10955: #define SRT_Except 2 /* Remove result from a UNION index */
! 10956: #define SRT_Exists 3 /* Store 1 if the result is not empty */
! 10957: #define SRT_Discard 4 /* Do not save the results anywhere */
! 10958:
! 10959: /* The ORDER BY clause is ignored for all of the above */
! 10960: #define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
! 10961:
! 10962: #define SRT_Output 5 /* Output each row of result */
! 10963: #define SRT_Mem 6 /* Store result in a memory cell */
! 10964: #define SRT_Set 7 /* Store results as keys in an index */
! 10965: #define SRT_Table 8 /* Store result as data with an automatic rowid */
! 10966: #define SRT_EphemTab 9 /* Create transient tab and store like SRT_Table */
! 10967: #define SRT_Coroutine 10 /* Generate a single row of result */
! 10968:
! 10969: /*
! 10970: ** A structure used to customize the behavior of sqlite3Select(). See
! 10971: ** comments above sqlite3Select() for details.
! 10972: */
! 10973: typedef struct SelectDest SelectDest;
! 10974: struct SelectDest {
! 10975: u8 eDest; /* How to dispose of the results */
! 10976: u8 affinity; /* Affinity used when eDest==SRT_Set */
! 10977: int iParm; /* A parameter used by the eDest disposal method */
! 10978: int iMem; /* Base register where results are written */
! 10979: int nMem; /* Number of registers allocated */
! 10980: };
! 10981:
! 10982: /*
! 10983: ** During code generation of statements that do inserts into AUTOINCREMENT
! 10984: ** tables, the following information is attached to the Table.u.autoInc.p
! 10985: ** pointer of each autoincrement table to record some side information that
! 10986: ** the code generator needs. We have to keep per-table autoincrement
! 10987: ** information in case inserts are down within triggers. Triggers do not
! 10988: ** normally coordinate their activities, but we do need to coordinate the
! 10989: ** loading and saving of autoincrement information.
! 10990: */
! 10991: struct AutoincInfo {
! 10992: AutoincInfo *pNext; /* Next info block in a list of them all */
! 10993: Table *pTab; /* Table this info block refers to */
! 10994: int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
! 10995: int regCtr; /* Memory register holding the rowid counter */
! 10996: };
! 10997:
! 10998: /*
! 10999: ** Size of the column cache
! 11000: */
! 11001: #ifndef SQLITE_N_COLCACHE
! 11002: # define SQLITE_N_COLCACHE 10
! 11003: #endif
! 11004:
! 11005: /*
! 11006: ** At least one instance of the following structure is created for each
! 11007: ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
! 11008: ** statement. All such objects are stored in the linked list headed at
! 11009: ** Parse.pTriggerPrg and deleted once statement compilation has been
! 11010: ** completed.
! 11011: **
! 11012: ** A Vdbe sub-program that implements the body and WHEN clause of trigger
! 11013: ** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
! 11014: ** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
! 11015: ** The Parse.pTriggerPrg list never contains two entries with the same
! 11016: ** values for both pTrigger and orconf.
! 11017: **
! 11018: ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
! 11019: ** accessed (or set to 0 for triggers fired as a result of INSERT
! 11020: ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
! 11021: ** a mask of new.* columns used by the program.
! 11022: */
! 11023: struct TriggerPrg {
! 11024: Trigger *pTrigger; /* Trigger this program was coded from */
! 11025: int orconf; /* Default ON CONFLICT policy */
! 11026: SubProgram *pProgram; /* Program implementing pTrigger/orconf */
! 11027: u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
! 11028: TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
! 11029: };
! 11030:
! 11031: /*
! 11032: ** The yDbMask datatype for the bitmask of all attached databases.
! 11033: */
! 11034: #if SQLITE_MAX_ATTACHED>30
! 11035: typedef sqlite3_uint64 yDbMask;
! 11036: #else
! 11037: typedef unsigned int yDbMask;
! 11038: #endif
! 11039:
! 11040: /*
! 11041: ** An SQL parser context. A copy of this structure is passed through
! 11042: ** the parser and down into all the parser action routine in order to
! 11043: ** carry around information that is global to the entire parse.
! 11044: **
! 11045: ** The structure is divided into two parts. When the parser and code
! 11046: ** generate call themselves recursively, the first part of the structure
! 11047: ** is constant but the second part is reset at the beginning and end of
! 11048: ** each recursion.
! 11049: **
! 11050: ** The nTableLock and aTableLock variables are only used if the shared-cache
! 11051: ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
! 11052: ** used to store the set of table-locks required by the statement being
! 11053: ** compiled. Function sqlite3TableLock() is used to add entries to the
! 11054: ** list.
! 11055: */
! 11056: struct Parse {
! 11057: sqlite3 *db; /* The main database structure */
! 11058: int rc; /* Return code from execution */
! 11059: char *zErrMsg; /* An error message */
! 11060: Vdbe *pVdbe; /* An engine for executing database bytecode */
! 11061: u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
! 11062: u8 checkSchema; /* Causes schema cookie check after an error */
! 11063: u8 nested; /* Number of nested calls to the parser/code generator */
! 11064: u8 nTempReg; /* Number of temporary registers in aTempReg[] */
! 11065: u8 nTempInUse; /* Number of aTempReg[] currently checked out */
! 11066: int aTempReg[8]; /* Holding area for temporary registers */
! 11067: int nRangeReg; /* Size of the temporary register block */
! 11068: int iRangeReg; /* First register in temporary register block */
! 11069: int nErr; /* Number of errors seen */
! 11070: int nTab; /* Number of previously allocated VDBE cursors */
! 11071: int nMem; /* Number of memory cells used so far */
! 11072: int nSet; /* Number of sets used so far */
! 11073: int nOnce; /* Number of OP_Once instructions so far */
! 11074: int ckBase; /* Base register of data during check constraints */
! 11075: int iCacheLevel; /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
! 11076: int iCacheCnt; /* Counter used to generate aColCache[].lru values */
! 11077: u8 nColCache; /* Number of entries in aColCache[] */
! 11078: u8 iColCache; /* Next entry in aColCache[] to replace */
! 11079: struct yColCache {
! 11080: int iTable; /* Table cursor number */
! 11081: int iColumn; /* Table column number */
! 11082: u8 tempReg; /* iReg is a temp register that needs to be freed */
! 11083: int iLevel; /* Nesting level */
! 11084: int iReg; /* Reg with value of this column. 0 means none. */
! 11085: int lru; /* Least recently used entry has the smallest value */
! 11086: } aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */
! 11087: yDbMask writeMask; /* Start a write transaction on these databases */
! 11088: yDbMask cookieMask; /* Bitmask of schema verified databases */
! 11089: u8 isMultiWrite; /* True if statement may affect/insert multiple rows */
! 11090: u8 mayAbort; /* True if statement may throw an ABORT exception */
! 11091: int cookieGoto; /* Address of OP_Goto to cookie verifier subroutine */
! 11092: int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */
! 11093: #ifndef SQLITE_OMIT_SHARED_CACHE
! 11094: int nTableLock; /* Number of locks in aTableLock */
! 11095: TableLock *aTableLock; /* Required table locks for shared-cache mode */
! 11096: #endif
! 11097: int regRowid; /* Register holding rowid of CREATE TABLE entry */
! 11098: int regRoot; /* Register holding root page number for new objects */
! 11099: AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
! 11100: int nMaxArg; /* Max args passed to user function by sub-program */
! 11101:
! 11102: /* Information used while coding trigger programs. */
! 11103: Parse *pToplevel; /* Parse structure for main program (or NULL) */
! 11104: Table *pTriggerTab; /* Table triggers are being coded for */
! 11105: u32 oldmask; /* Mask of old.* columns referenced */
! 11106: u32 newmask; /* Mask of new.* columns referenced */
! 11107: u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
! 11108: u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
! 11109: u8 disableTriggers; /* True to disable triggers */
! 11110: double nQueryLoop; /* Estimated number of iterations of a query */
! 11111:
! 11112: /* Above is constant between recursions. Below is reset before and after
! 11113: ** each recursion */
! 11114:
! 11115: int nVar; /* Number of '?' variables seen in the SQL so far */
! 11116: int nzVar; /* Number of available slots in azVar[] */
! 11117: char **azVar; /* Pointers to names of parameters */
! 11118: Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
! 11119: int nAlias; /* Number of aliased result set columns */
! 11120: int *aAlias; /* Register used to hold aliased result */
! 11121: u8 explain; /* True if the EXPLAIN flag is found on the query */
! 11122: Token sNameToken; /* Token with unqualified schema object name */
! 11123: Token sLastToken; /* The last token parsed */
! 11124: const char *zTail; /* All SQL text past the last semicolon parsed */
! 11125: Table *pNewTable; /* A table being constructed by CREATE TABLE */
! 11126: Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
! 11127: const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
! 11128: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 11129: Token sArg; /* Complete text of a module argument */
! 11130: u8 declareVtab; /* True if inside sqlite3_declare_vtab() */
! 11131: int nVtabLock; /* Number of virtual tables to lock */
! 11132: Table **apVtabLock; /* Pointer to virtual tables needing locking */
! 11133: #endif
! 11134: int nHeight; /* Expression tree height of current sub-select */
! 11135: Table *pZombieTab; /* List of Table objects to delete after code gen */
! 11136: TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
! 11137:
! 11138: #ifndef SQLITE_OMIT_EXPLAIN
! 11139: int iSelectId;
! 11140: int iNextSelectId;
! 11141: #endif
! 11142: };
! 11143:
! 11144: #ifdef SQLITE_OMIT_VIRTUALTABLE
! 11145: #define IN_DECLARE_VTAB 0
! 11146: #else
! 11147: #define IN_DECLARE_VTAB (pParse->declareVtab)
! 11148: #endif
! 11149:
! 11150: /*
! 11151: ** An instance of the following structure can be declared on a stack and used
! 11152: ** to save the Parse.zAuthContext value so that it can be restored later.
! 11153: */
! 11154: struct AuthContext {
! 11155: const char *zAuthContext; /* Put saved Parse.zAuthContext here */
! 11156: Parse *pParse; /* The Parse structure */
! 11157: };
! 11158:
! 11159: /*
! 11160: ** Bitfield flags for P5 value in OP_Insert and OP_Delete
! 11161: */
! 11162: #define OPFLAG_NCHANGE 0x01 /* Set to update db->nChange */
! 11163: #define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */
! 11164: #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
! 11165: #define OPFLAG_APPEND 0x08 /* This is likely to be an append */
! 11166: #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
! 11167: #define OPFLAG_CLEARCACHE 0x20 /* Clear pseudo-table cache in OP_Column */
! 11168:
! 11169: /*
! 11170: * Each trigger present in the database schema is stored as an instance of
! 11171: * struct Trigger.
! 11172: *
! 11173: * Pointers to instances of struct Trigger are stored in two ways.
! 11174: * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
! 11175: * database). This allows Trigger structures to be retrieved by name.
! 11176: * 2. All triggers associated with a single table form a linked list, using the
! 11177: * pNext member of struct Trigger. A pointer to the first element of the
! 11178: * linked list is stored as the "pTrigger" member of the associated
! 11179: * struct Table.
! 11180: *
! 11181: * The "step_list" member points to the first element of a linked list
! 11182: * containing the SQL statements specified as the trigger program.
! 11183: */
! 11184: struct Trigger {
! 11185: char *zName; /* The name of the trigger */
! 11186: char *table; /* The table or view to which the trigger applies */
! 11187: u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
! 11188: u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
! 11189: Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
! 11190: IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
! 11191: the <column-list> is stored here */
! 11192: Schema *pSchema; /* Schema containing the trigger */
! 11193: Schema *pTabSchema; /* Schema containing the table */
! 11194: TriggerStep *step_list; /* Link list of trigger program steps */
! 11195: Trigger *pNext; /* Next trigger associated with the table */
! 11196: };
! 11197:
! 11198: /*
! 11199: ** A trigger is either a BEFORE or an AFTER trigger. The following constants
! 11200: ** determine which.
! 11201: **
! 11202: ** If there are multiple triggers, you might of some BEFORE and some AFTER.
! 11203: ** In that cases, the constants below can be ORed together.
! 11204: */
! 11205: #define TRIGGER_BEFORE 1
! 11206: #define TRIGGER_AFTER 2
! 11207:
! 11208: /*
! 11209: * An instance of struct TriggerStep is used to store a single SQL statement
! 11210: * that is a part of a trigger-program.
! 11211: *
! 11212: * Instances of struct TriggerStep are stored in a singly linked list (linked
! 11213: * using the "pNext" member) referenced by the "step_list" member of the
! 11214: * associated struct Trigger instance. The first element of the linked list is
! 11215: * the first step of the trigger-program.
! 11216: *
! 11217: * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
! 11218: * "SELECT" statement. The meanings of the other members is determined by the
! 11219: * value of "op" as follows:
! 11220: *
! 11221: * (op == TK_INSERT)
! 11222: * orconf -> stores the ON CONFLICT algorithm
! 11223: * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
! 11224: * this stores a pointer to the SELECT statement. Otherwise NULL.
! 11225: * target -> A token holding the quoted name of the table to insert into.
! 11226: * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
! 11227: * this stores values to be inserted. Otherwise NULL.
! 11228: * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
! 11229: * statement, then this stores the column-names to be
! 11230: * inserted into.
! 11231: *
! 11232: * (op == TK_DELETE)
! 11233: * target -> A token holding the quoted name of the table to delete from.
! 11234: * pWhere -> The WHERE clause of the DELETE statement if one is specified.
! 11235: * Otherwise NULL.
! 11236: *
! 11237: * (op == TK_UPDATE)
! 11238: * target -> A token holding the quoted name of the table to update rows of.
! 11239: * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
! 11240: * Otherwise NULL.
! 11241: * pExprList -> A list of the columns to update and the expressions to update
! 11242: * them to. See sqlite3Update() documentation of "pChanges"
! 11243: * argument.
! 11244: *
! 11245: */
! 11246: struct TriggerStep {
! 11247: u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
! 11248: u8 orconf; /* OE_Rollback etc. */
! 11249: Trigger *pTrig; /* The trigger that this step is a part of */
! 11250: Select *pSelect; /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
! 11251: Token target; /* Target table for DELETE, UPDATE, INSERT */
! 11252: Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
! 11253: ExprList *pExprList; /* SET clause for UPDATE. VALUES clause for INSERT */
! 11254: IdList *pIdList; /* Column names for INSERT */
! 11255: TriggerStep *pNext; /* Next in the link-list */
! 11256: TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
! 11257: };
! 11258:
! 11259: /*
! 11260: ** The following structure contains information used by the sqliteFix...
! 11261: ** routines as they walk the parse tree to make database references
! 11262: ** explicit.
! 11263: */
! 11264: typedef struct DbFixer DbFixer;
! 11265: struct DbFixer {
! 11266: Parse *pParse; /* The parsing context. Error messages written here */
! 11267: const char *zDb; /* Make sure all objects are contained in this database */
! 11268: const char *zType; /* Type of the container - used for error messages */
! 11269: const Token *pName; /* Name of the container - used for error messages */
! 11270: };
! 11271:
! 11272: /*
! 11273: ** An objected used to accumulate the text of a string where we
! 11274: ** do not necessarily know how big the string will be in the end.
! 11275: */
! 11276: struct StrAccum {
! 11277: sqlite3 *db; /* Optional database for lookaside. Can be NULL */
! 11278: char *zBase; /* A base allocation. Not from malloc. */
! 11279: char *zText; /* The string collected so far */
! 11280: int nChar; /* Length of the string so far */
! 11281: int nAlloc; /* Amount of space allocated in zText */
! 11282: int mxAlloc; /* Maximum allowed string length */
! 11283: u8 mallocFailed; /* Becomes true if any memory allocation fails */
! 11284: u8 useMalloc; /* 0: none, 1: sqlite3DbMalloc, 2: sqlite3_malloc */
! 11285: u8 tooBig; /* Becomes true if string size exceeds limits */
! 11286: };
! 11287:
! 11288: /*
! 11289: ** A pointer to this structure is used to communicate information
! 11290: ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
! 11291: */
! 11292: typedef struct {
! 11293: sqlite3 *db; /* The database being initialized */
! 11294: int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
! 11295: char **pzErrMsg; /* Error message stored here */
! 11296: int rc; /* Result code stored here */
! 11297: } InitData;
! 11298:
! 11299: /*
! 11300: ** Structure containing global configuration data for the SQLite library.
! 11301: **
! 11302: ** This structure also contains some state information.
! 11303: */
! 11304: struct Sqlite3Config {
! 11305: int bMemstat; /* True to enable memory status */
! 11306: int bCoreMutex; /* True to enable core mutexing */
! 11307: int bFullMutex; /* True to enable full mutexing */
! 11308: int bOpenUri; /* True to interpret filenames as URIs */
! 11309: int mxStrlen; /* Maximum string length */
! 11310: int szLookaside; /* Default lookaside buffer size */
! 11311: int nLookaside; /* Default lookaside buffer count */
! 11312: sqlite3_mem_methods m; /* Low-level memory allocation interface */
! 11313: sqlite3_mutex_methods mutex; /* Low-level mutex interface */
! 11314: sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
! 11315: void *pHeap; /* Heap storage space */
! 11316: int nHeap; /* Size of pHeap[] */
! 11317: int mnReq, mxReq; /* Min and max heap requests sizes */
! 11318: void *pScratch; /* Scratch memory */
! 11319: int szScratch; /* Size of each scratch buffer */
! 11320: int nScratch; /* Number of scratch buffers */
! 11321: void *pPage; /* Page cache memory */
! 11322: int szPage; /* Size of each page in pPage[] */
! 11323: int nPage; /* Number of pages in pPage[] */
! 11324: int mxParserStack; /* maximum depth of the parser stack */
! 11325: int sharedCacheEnabled; /* true if shared-cache mode enabled */
! 11326: /* The above might be initialized to non-zero. The following need to always
! 11327: ** initially be zero, however. */
! 11328: int isInit; /* True after initialization has finished */
! 11329: int inProgress; /* True while initialization in progress */
! 11330: int isMutexInit; /* True after mutexes are initialized */
! 11331: int isMallocInit; /* True after malloc is initialized */
! 11332: int isPCacheInit; /* True after malloc is initialized */
! 11333: sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
! 11334: int nRefInitMutex; /* Number of users of pInitMutex */
! 11335: void (*xLog)(void*,int,const char*); /* Function for logging */
! 11336: void *pLogArg; /* First argument to xLog() */
! 11337: int bLocaltimeFault; /* True to fail localtime() calls */
! 11338: };
! 11339:
! 11340: /*
! 11341: ** Context pointer passed down through the tree-walk.
! 11342: */
! 11343: struct Walker {
! 11344: int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
! 11345: int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
! 11346: Parse *pParse; /* Parser context. */
! 11347: union { /* Extra data for callback */
! 11348: NameContext *pNC; /* Naming context */
! 11349: int i; /* Integer value */
! 11350: } u;
! 11351: };
! 11352:
! 11353: /* Forward declarations */
! 11354: SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
! 11355: SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
! 11356: SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
! 11357: SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
! 11358: SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
! 11359:
! 11360: /*
! 11361: ** Return code from the parse-tree walking primitives and their
! 11362: ** callbacks.
! 11363: */
! 11364: #define WRC_Continue 0 /* Continue down into children */
! 11365: #define WRC_Prune 1 /* Omit children but continue walking siblings */
! 11366: #define WRC_Abort 2 /* Abandon the tree walk */
! 11367:
! 11368: /*
! 11369: ** Assuming zIn points to the first byte of a UTF-8 character,
! 11370: ** advance zIn to point to the first byte of the next UTF-8 character.
! 11371: */
! 11372: #define SQLITE_SKIP_UTF8(zIn) { \
! 11373: if( (*(zIn++))>=0xc0 ){ \
! 11374: while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
! 11375: } \
! 11376: }
! 11377:
! 11378: /*
! 11379: ** The SQLITE_*_BKPT macros are substitutes for the error codes with
! 11380: ** the same name but without the _BKPT suffix. These macros invoke
! 11381: ** routines that report the line-number on which the error originated
! 11382: ** using sqlite3_log(). The routines also provide a convenient place
! 11383: ** to set a debugger breakpoint.
! 11384: */
! 11385: SQLITE_PRIVATE int sqlite3CorruptError(int);
! 11386: SQLITE_PRIVATE int sqlite3MisuseError(int);
! 11387: SQLITE_PRIVATE int sqlite3CantopenError(int);
! 11388: #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
! 11389: #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
! 11390: #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
! 11391:
! 11392:
! 11393: /*
! 11394: ** FTS4 is really an extension for FTS3. It is enabled using the
! 11395: ** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
! 11396: ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
! 11397: */
! 11398: #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
! 11399: # define SQLITE_ENABLE_FTS3
! 11400: #endif
! 11401:
! 11402: /*
! 11403: ** The ctype.h header is needed for non-ASCII systems. It is also
! 11404: ** needed by FTS3 when FTS3 is included in the amalgamation.
! 11405: */
! 11406: #if !defined(SQLITE_ASCII) || \
! 11407: (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
! 11408: # include <ctype.h>
! 11409: #endif
! 11410:
! 11411: /*
! 11412: ** The following macros mimic the standard library functions toupper(),
! 11413: ** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
! 11414: ** sqlite versions only work for ASCII characters, regardless of locale.
! 11415: */
! 11416: #ifdef SQLITE_ASCII
! 11417: # define sqlite3Toupper(x) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
! 11418: # define sqlite3Isspace(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
! 11419: # define sqlite3Isalnum(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
! 11420: # define sqlite3Isalpha(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
! 11421: # define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
! 11422: # define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
! 11423: # define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)])
! 11424: #else
! 11425: # define sqlite3Toupper(x) toupper((unsigned char)(x))
! 11426: # define sqlite3Isspace(x) isspace((unsigned char)(x))
! 11427: # define sqlite3Isalnum(x) isalnum((unsigned char)(x))
! 11428: # define sqlite3Isalpha(x) isalpha((unsigned char)(x))
! 11429: # define sqlite3Isdigit(x) isdigit((unsigned char)(x))
! 11430: # define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
! 11431: # define sqlite3Tolower(x) tolower((unsigned char)(x))
! 11432: #endif
! 11433:
! 11434: /*
! 11435: ** Internal function prototypes
! 11436: */
! 11437: SQLITE_PRIVATE int sqlite3StrICmp(const char *, const char *);
! 11438: SQLITE_PRIVATE int sqlite3Strlen30(const char*);
! 11439: #define sqlite3StrNICmp sqlite3_strnicmp
! 11440:
! 11441: SQLITE_PRIVATE int sqlite3MallocInit(void);
! 11442: SQLITE_PRIVATE void sqlite3MallocEnd(void);
! 11443: SQLITE_PRIVATE void *sqlite3Malloc(int);
! 11444: SQLITE_PRIVATE void *sqlite3MallocZero(int);
! 11445: SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
! 11446: SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
! 11447: SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
! 11448: SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
! 11449: SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
! 11450: SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
! 11451: SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
! 11452: SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
! 11453: SQLITE_PRIVATE int sqlite3MallocSize(void*);
! 11454: SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
! 11455: SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
! 11456: SQLITE_PRIVATE void sqlite3ScratchFree(void*);
! 11457: SQLITE_PRIVATE void *sqlite3PageMalloc(int);
! 11458: SQLITE_PRIVATE void sqlite3PageFree(void*);
! 11459: SQLITE_PRIVATE void sqlite3MemSetDefault(void);
! 11460: SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
! 11461: SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
! 11462:
! 11463: /*
! 11464: ** On systems with ample stack space and that support alloca(), make
! 11465: ** use of alloca() to obtain space for large automatic objects. By default,
! 11466: ** obtain space from malloc().
! 11467: **
! 11468: ** The alloca() routine never returns NULL. This will cause code paths
! 11469: ** that deal with sqlite3StackAlloc() failures to be unreachable.
! 11470: */
! 11471: #ifdef SQLITE_USE_ALLOCA
! 11472: # define sqlite3StackAllocRaw(D,N) alloca(N)
! 11473: # define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N)
! 11474: # define sqlite3StackFree(D,P)
! 11475: #else
! 11476: # define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
! 11477: # define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N)
! 11478: # define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
! 11479: #endif
! 11480:
! 11481: #ifdef SQLITE_ENABLE_MEMSYS3
! 11482: SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
! 11483: #endif
! 11484: #ifdef SQLITE_ENABLE_MEMSYS5
! 11485: SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
! 11486: #endif
! 11487:
! 11488:
! 11489: #ifndef SQLITE_MUTEX_OMIT
! 11490: SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
! 11491: SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void);
! 11492: SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
! 11493: SQLITE_PRIVATE int sqlite3MutexInit(void);
! 11494: SQLITE_PRIVATE int sqlite3MutexEnd(void);
! 11495: #endif
! 11496:
! 11497: SQLITE_PRIVATE int sqlite3StatusValue(int);
! 11498: SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
! 11499: SQLITE_PRIVATE void sqlite3StatusSet(int, int);
! 11500:
! 11501: #ifndef SQLITE_OMIT_FLOATING_POINT
! 11502: SQLITE_PRIVATE int sqlite3IsNaN(double);
! 11503: #else
! 11504: # define sqlite3IsNaN(X) 0
! 11505: #endif
! 11506:
! 11507: SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
! 11508: #ifndef SQLITE_OMIT_TRACE
! 11509: SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
! 11510: #endif
! 11511: SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
! 11512: SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
! 11513: SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
! 11514: #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
! 11515: SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
! 11516: #endif
! 11517: #if defined(SQLITE_TEST)
! 11518: SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
! 11519: #endif
! 11520:
! 11521: /* Output formatting for SQLITE_TESTCTRL_EXPLAIN */
! 11522: #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
! 11523: SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe*);
! 11524: SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe*, const char*, ...);
! 11525: SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe*);
! 11526: SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe*);
! 11527: SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe*);
! 11528: SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe*);
! 11529: SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe*, Select*);
! 11530: SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe*, Expr*);
! 11531: SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe*, ExprList*);
! 11532: SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe*);
! 11533: #else
! 11534: # define sqlite3ExplainBegin(X)
! 11535: # define sqlite3ExplainSelect(A,B)
! 11536: # define sqlite3ExplainExpr(A,B)
! 11537: # define sqlite3ExplainExprList(A,B)
! 11538: # define sqlite3ExplainFinish(X)
! 11539: # define sqlite3VdbeExplanation(X) 0
! 11540: #endif
! 11541:
! 11542:
! 11543: SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
! 11544: SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
! 11545: SQLITE_PRIVATE int sqlite3Dequote(char*);
! 11546: SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
! 11547: SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
! 11548: SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
! 11549: SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
! 11550: SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
! 11551: SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
! 11552: SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
! 11553: SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
! 11554: SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
! 11555: SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
! 11556: SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
! 11557: SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
! 11558: SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
! 11559: SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
! 11560: SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
! 11561: SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
! 11562: SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
! 11563: SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
! 11564: SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
! 11565: SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
! 11566: SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
! 11567: SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
! 11568: SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
! 11569: SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3*, int);
! 11570: SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
! 11571: SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
! 11572: SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
! 11573: SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
! 11574: SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
! 11575: SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
! 11576: SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
! 11577: SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
! 11578: SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
! 11579: SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
! 11580: SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
! 11581: SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
! 11582: SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
! 11583: SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
! 11584: sqlite3_vfs**,char**,char **);
! 11585: SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
! 11586:
! 11587: SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
! 11588: SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
! 11589: SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
! 11590: SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
! 11591: SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
! 11592: SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
! 11593: SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
! 11594:
! 11595: SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
! 11596: SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
! 11597: SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
! 11598: SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
! 11599: SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
! 11600:
! 11601: SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
! 11602:
! 11603: #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
! 11604: SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*);
! 11605: #else
! 11606: # define sqlite3ViewGetColumnNames(A,B) 0
! 11607: #endif
! 11608:
! 11609: SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
! 11610: SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
! 11611: SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
! 11612: #ifndef SQLITE_OMIT_AUTOINCREMENT
! 11613: SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
! 11614: SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
! 11615: #else
! 11616: # define sqlite3AutoincrementBegin(X)
! 11617: # define sqlite3AutoincrementEnd(X)
! 11618: #endif
! 11619: SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
! 11620: SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
! 11621: SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
! 11622: SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
! 11623: SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
! 11624: SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
! 11625: SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
! 11626: Token*, Select*, Expr*, IdList*);
! 11627: SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
! 11628: SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
! 11629: SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
! 11630: SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
! 11631: SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
! 11632: SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
! 11633: SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
! 11634: Token*, int, int);
! 11635: SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
! 11636: SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
! 11637: SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
! 11638: Expr*,ExprList*,int,Expr*,Expr*);
! 11639: SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
! 11640: SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
! 11641: SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
! 11642: SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
! 11643: #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
! 11644: SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
! 11645: #endif
! 11646: SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
! 11647: SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
! 11648: SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**,ExprList*,u16);
! 11649: SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
! 11650: SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int);
! 11651: SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
! 11652: SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
! 11653: SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
! 11654: SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
! 11655: SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
! 11656: SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
! 11657: SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
! 11658: SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
! 11659: SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
! 11660: SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
! 11661: SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
! 11662: SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
! 11663: SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
! 11664: SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*);
! 11665: SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
! 11666: SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
! 11667: SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
! 11668: SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
! 11669: SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
! 11670: SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
! 11671: SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
! 11672: SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
! 11673: SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
! 11674: SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
! 11675: SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
! 11676: SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
! 11677: SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*);
! 11678: SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
! 11679: SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
! 11680: SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
! 11681: SQLITE_PRIVATE void sqlite3PrngSaveState(void);
! 11682: SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
! 11683: SQLITE_PRIVATE void sqlite3PrngResetState(void);
! 11684: SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*);
! 11685: SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
! 11686: SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
! 11687: SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
! 11688: SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
! 11689: SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
! 11690: SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
! 11691: SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
! 11692: SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
! 11693: SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
! 11694: SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
! 11695: SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
! 11696: SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
! 11697: SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
! 11698: SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
! 11699: SQLITE_PRIVATE int sqlite3IsRowid(const char*);
! 11700: SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
! 11701: SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
! 11702: SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
! 11703: SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
! 11704: int*,int,int,int,int,int*);
! 11705: SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
! 11706: SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
! 11707: SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
! 11708: SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
! 11709: SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
! 11710: SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, char*, int);
! 11711: SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
! 11712: SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
! 11713: SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
! 11714: SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
! 11715: SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
! 11716: SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
! 11717: SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
! 11718: SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
! 11719: SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
! 11720: SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
! 11721: SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
! 11722: SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
! 11723: SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
! 11724:
! 11725: #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
! 11726: SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
! 11727: #endif
! 11728:
! 11729: #ifndef SQLITE_OMIT_TRIGGER
! 11730: SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
! 11731: Expr*,int, int);
! 11732: SQLITE_PRIVATE void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
! 11733: SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int);
! 11734: SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*);
! 11735: SQLITE_PRIVATE Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
! 11736: SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *, Table *);
! 11737: SQLITE_PRIVATE void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
! 11738: int, int, int);
! 11739: SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
! 11740: void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
! 11741: SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
! 11742: SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
! 11743: SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
! 11744: ExprList*,Select*,u8);
! 11745: SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
! 11746: SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
! 11747: SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*);
! 11748: SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
! 11749: SQLITE_PRIVATE u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
! 11750: # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
! 11751: #else
! 11752: # define sqlite3TriggersExist(B,C,D,E,F) 0
! 11753: # define sqlite3DeleteTrigger(A,B)
! 11754: # define sqlite3DropTriggerPtr(A,B)
! 11755: # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
! 11756: # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
! 11757: # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
! 11758: # define sqlite3TriggerList(X, Y) 0
! 11759: # define sqlite3ParseToplevel(p) p
! 11760: # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
! 11761: #endif
! 11762:
! 11763: SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
! 11764: SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
! 11765: SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
! 11766: #ifndef SQLITE_OMIT_AUTHORIZATION
! 11767: SQLITE_PRIVATE void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
! 11768: SQLITE_PRIVATE int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
! 11769: SQLITE_PRIVATE void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
! 11770: SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext*);
! 11771: SQLITE_PRIVATE int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
! 11772: #else
! 11773: # define sqlite3AuthRead(a,b,c,d)
! 11774: # define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
! 11775: # define sqlite3AuthContextPush(a,b,c)
! 11776: # define sqlite3AuthContextPop(a) ((void)(a))
! 11777: #endif
! 11778: SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
! 11779: SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
! 11780: SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
! 11781: SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
! 11782: SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
! 11783: SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
! 11784: SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
! 11785: SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
! 11786: SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
! 11787: SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
! 11788: SQLITE_PRIVATE int sqlite3Atoi(const char*);
! 11789: SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
! 11790: SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
! 11791: SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8*, const u8**);
! 11792:
! 11793: /*
! 11794: ** Routines to read and write variable-length integers. These used to
! 11795: ** be defined locally, but now we use the varint routines in the util.c
! 11796: ** file. Code should use the MACRO forms below, as the Varint32 versions
! 11797: ** are coded to assume the single byte case is already handled (which
! 11798: ** the MACRO form does).
! 11799: */
! 11800: SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
! 11801: SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
! 11802: SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
! 11803: SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
! 11804: SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
! 11805:
! 11806: /*
! 11807: ** The header of a record consists of a sequence variable-length integers.
! 11808: ** These integers are almost always small and are encoded as a single byte.
! 11809: ** The following macros take advantage this fact to provide a fast encode
! 11810: ** and decode of the integers in a record header. It is faster for the common
! 11811: ** case where the integer is a single byte. It is a little slower when the
! 11812: ** integer is two or more bytes. But overall it is faster.
! 11813: **
! 11814: ** The following expressions are equivalent:
! 11815: **
! 11816: ** x = sqlite3GetVarint32( A, &B );
! 11817: ** x = sqlite3PutVarint32( A, B );
! 11818: **
! 11819: ** x = getVarint32( A, B );
! 11820: ** x = putVarint32( A, B );
! 11821: **
! 11822: */
! 11823: #define getVarint32(A,B) (u8)((*(A)<(u8)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
! 11824: #define putVarint32(A,B) (u8)(((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
! 11825: #define getVarint sqlite3GetVarint
! 11826: #define putVarint sqlite3PutVarint
! 11827:
! 11828:
! 11829: SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
! 11830: SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
! 11831: SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
! 11832: SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
! 11833: SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
! 11834: SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
! 11835: SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
! 11836: SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
! 11837: SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
! 11838: SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
! 11839: SQLITE_PRIVATE const char *sqlite3ErrStr(int);
! 11840: SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
! 11841: SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
! 11842: SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
! 11843: SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
! 11844: SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr*, CollSeq*);
! 11845: SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*);
! 11846: SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
! 11847: SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
! 11848: SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
! 11849: SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
! 11850: SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
! 11851: SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
! 11852: SQLITE_PRIVATE int sqlite3AbsInt32(int);
! 11853: #ifdef SQLITE_ENABLE_8_3_NAMES
! 11854: SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
! 11855: #else
! 11856: # define sqlite3FileSuffix3(X,Y)
! 11857: #endif
! 11858: SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z);
! 11859:
! 11860: SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
! 11861: SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
! 11862: SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
! 11863: void(*)(void*));
! 11864: SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
! 11865: SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
! 11866: SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
! 11867: #ifdef SQLITE_ENABLE_STAT3
! 11868: SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
! 11869: #endif
! 11870: SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
! 11871: SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
! 11872: #ifndef SQLITE_AMALGAMATION
! 11873: SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
! 11874: SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
! 11875: SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
! 11876: SQLITE_PRIVATE const Token sqlite3IntTokens[];
! 11877: SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
! 11878: SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
! 11879: #ifndef SQLITE_OMIT_WSD
! 11880: SQLITE_PRIVATE int sqlite3PendingByte;
! 11881: #endif
! 11882: #endif
! 11883: SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
! 11884: SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
! 11885: SQLITE_PRIVATE void sqlite3AlterFunctions(void);
! 11886: SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
! 11887: SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
! 11888: SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
! 11889: SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
! 11890: SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
! 11891: SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
! 11892: SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
! 11893: SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
! 11894: SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
! 11895: SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
! 11896: SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
! 11897: SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
! 11898: SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(sqlite3*, u8, CollSeq *, const char*);
! 11899: SQLITE_PRIVATE char sqlite3AffinityType(const char*);
! 11900: SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
! 11901: SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
! 11902: SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
! 11903: SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
! 11904: SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
! 11905: SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
! 11906: SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
! 11907: SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
! 11908: SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
! 11909: SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
! 11910: SQLITE_PRIVATE void sqlite3SchemaClear(void *);
! 11911: SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
! 11912: SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
! 11913: SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
! 11914: SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
! 11915: void (*)(sqlite3_context*,int,sqlite3_value **),
! 11916: void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
! 11917: FuncDestructor *pDestructor
! 11918: );
! 11919: SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
! 11920: SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
! 11921:
! 11922: SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
! 11923: SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
! 11924: SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
! 11925: SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
! 11926: SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
! 11927: SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
! 11928: SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
! 11929:
! 11930: SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
! 11931: SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
! 11932:
! 11933: /*
! 11934: ** The interface to the LEMON-generated parser
! 11935: */
! 11936: SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
! 11937: SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
! 11938: SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
! 11939: #ifdef YYTRACKMAXSTACKDEPTH
! 11940: SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
! 11941: #endif
! 11942:
! 11943: SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
! 11944: #ifndef SQLITE_OMIT_LOAD_EXTENSION
! 11945: SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3*);
! 11946: #else
! 11947: # define sqlite3CloseExtensions(X)
! 11948: #endif
! 11949:
! 11950: #ifndef SQLITE_OMIT_SHARED_CACHE
! 11951: SQLITE_PRIVATE void sqlite3TableLock(Parse *, int, int, u8, const char *);
! 11952: #else
! 11953: #define sqlite3TableLock(v,w,x,y,z)
! 11954: #endif
! 11955:
! 11956: #ifdef SQLITE_TEST
! 11957: SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char*);
! 11958: #endif
! 11959:
! 11960: #ifdef SQLITE_OMIT_VIRTUALTABLE
! 11961: # define sqlite3VtabClear(Y)
! 11962: # define sqlite3VtabSync(X,Y) SQLITE_OK
! 11963: # define sqlite3VtabRollback(X)
! 11964: # define sqlite3VtabCommit(X)
! 11965: # define sqlite3VtabInSync(db) 0
! 11966: # define sqlite3VtabLock(X)
! 11967: # define sqlite3VtabUnlock(X)
! 11968: # define sqlite3VtabUnlockList(X)
! 11969: # define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
! 11970: # define sqlite3GetVTable(X,Y) ((VTable*)0)
! 11971: #else
! 11972: SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*);
! 11973: SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **);
! 11974: SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
! 11975: SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
! 11976: SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
! 11977: SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
! 11978: SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
! 11979: SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
! 11980: SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
! 11981: # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
! 11982: #endif
! 11983: SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
! 11984: SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
! 11985: SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
! 11986: SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
! 11987: SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
! 11988: SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
! 11989: SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
! 11990: SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
! 11991: SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
! 11992: SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
! 11993: SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
! 11994: SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
! 11995: SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
! 11996: SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
! 11997: SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
! 11998: SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
! 11999: SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
! 12000: SQLITE_PRIVATE const char *sqlite3JournalModename(int);
! 12001: SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
! 12002: SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
! 12003:
! 12004: /* Declarations for functions in fkey.c. All of these are replaced by
! 12005: ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
! 12006: ** key functionality is available. If OMIT_TRIGGER is defined but
! 12007: ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
! 12008: ** this case foreign keys are parsed, but no other functionality is
! 12009: ** provided (enforcement of FK constraints requires the triggers sub-system).
! 12010: */
! 12011: #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
! 12012: SQLITE_PRIVATE void sqlite3FkCheck(Parse*, Table*, int, int);
! 12013: SQLITE_PRIVATE void sqlite3FkDropTable(Parse*, SrcList *, Table*);
! 12014: SQLITE_PRIVATE void sqlite3FkActions(Parse*, Table*, ExprList*, int);
! 12015: SQLITE_PRIVATE int sqlite3FkRequired(Parse*, Table*, int*, int);
! 12016: SQLITE_PRIVATE u32 sqlite3FkOldmask(Parse*, Table*);
! 12017: SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *);
! 12018: #else
! 12019: #define sqlite3FkActions(a,b,c,d)
! 12020: #define sqlite3FkCheck(a,b,c,d)
! 12021: #define sqlite3FkDropTable(a,b,c)
! 12022: #define sqlite3FkOldmask(a,b) 0
! 12023: #define sqlite3FkRequired(a,b,c,d) 0
! 12024: #endif
! 12025: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 12026: SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*);
! 12027: #else
! 12028: #define sqlite3FkDelete(a,b)
! 12029: #endif
! 12030:
! 12031:
! 12032: /*
! 12033: ** Available fault injectors. Should be numbered beginning with 0.
! 12034: */
! 12035: #define SQLITE_FAULTINJECTOR_MALLOC 0
! 12036: #define SQLITE_FAULTINJECTOR_COUNT 1
! 12037:
! 12038: /*
! 12039: ** The interface to the code in fault.c used for identifying "benign"
! 12040: ** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
! 12041: ** is not defined.
! 12042: */
! 12043: #ifndef SQLITE_OMIT_BUILTIN_TEST
! 12044: SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void);
! 12045: SQLITE_PRIVATE void sqlite3EndBenignMalloc(void);
! 12046: #else
! 12047: #define sqlite3BeginBenignMalloc()
! 12048: #define sqlite3EndBenignMalloc()
! 12049: #endif
! 12050:
! 12051: #define IN_INDEX_ROWID 1
! 12052: #define IN_INDEX_EPH 2
! 12053: #define IN_INDEX_INDEX 3
! 12054: SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
! 12055:
! 12056: #ifdef SQLITE_ENABLE_ATOMIC_WRITE
! 12057: SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
! 12058: SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
! 12059: SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *);
! 12060: #else
! 12061: #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
! 12062: #endif
! 12063:
! 12064: SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
! 12065: SQLITE_PRIVATE int sqlite3MemJournalSize(void);
! 12066: SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
! 12067:
! 12068: #if SQLITE_MAX_EXPR_DEPTH>0
! 12069: SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
! 12070: SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *);
! 12071: SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse*, int);
! 12072: #else
! 12073: #define sqlite3ExprSetHeight(x,y)
! 12074: #define sqlite3SelectExprHeight(x) 0
! 12075: #define sqlite3ExprCheckHeight(x,y)
! 12076: #endif
! 12077:
! 12078: SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
! 12079: SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
! 12080:
! 12081: #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
! 12082: SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
! 12083: SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db);
! 12084: SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db);
! 12085: #else
! 12086: #define sqlite3ConnectionBlocked(x,y)
! 12087: #define sqlite3ConnectionUnlocked(x)
! 12088: #define sqlite3ConnectionClosed(x)
! 12089: #endif
! 12090:
! 12091: #ifdef SQLITE_DEBUG
! 12092: SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *);
! 12093: #endif
! 12094:
! 12095: /*
! 12096: ** If the SQLITE_ENABLE IOTRACE exists then the global variable
! 12097: ** sqlite3IoTrace is a pointer to a printf-like routine used to
! 12098: ** print I/O tracing messages.
! 12099: */
! 12100: #ifdef SQLITE_ENABLE_IOTRACE
! 12101: # define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
! 12102: SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe*);
! 12103: SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
! 12104: #else
! 12105: # define IOTRACE(A)
! 12106: # define sqlite3VdbeIOTraceSql(X)
! 12107: #endif
! 12108:
! 12109: /*
! 12110: ** These routines are available for the mem2.c debugging memory allocator
! 12111: ** only. They are used to verify that different "types" of memory
! 12112: ** allocations are properly tracked by the system.
! 12113: **
! 12114: ** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
! 12115: ** the MEMTYPE_* macros defined below. The type must be a bitmask with
! 12116: ** a single bit set.
! 12117: **
! 12118: ** sqlite3MemdebugHasType() returns true if any of the bits in its second
! 12119: ** argument match the type set by the previous sqlite3MemdebugSetType().
! 12120: ** sqlite3MemdebugHasType() is intended for use inside assert() statements.
! 12121: **
! 12122: ** sqlite3MemdebugNoType() returns true if none of the bits in its second
! 12123: ** argument match the type set by the previous sqlite3MemdebugSetType().
! 12124: **
! 12125: ** Perhaps the most important point is the difference between MEMTYPE_HEAP
! 12126: ** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
! 12127: ** it might have been allocated by lookaside, except the allocation was
! 12128: ** too large or lookaside was already full. It is important to verify
! 12129: ** that allocations that might have been satisfied by lookaside are not
! 12130: ** passed back to non-lookaside free() routines. Asserts such as the
! 12131: ** example above are placed on the non-lookaside free() routines to verify
! 12132: ** this constraint.
! 12133: **
! 12134: ** All of this is no-op for a production build. It only comes into
! 12135: ** play when the SQLITE_MEMDEBUG compile-time option is used.
! 12136: */
! 12137: #ifdef SQLITE_MEMDEBUG
! 12138: SQLITE_PRIVATE void sqlite3MemdebugSetType(void*,u8);
! 12139: SQLITE_PRIVATE int sqlite3MemdebugHasType(void*,u8);
! 12140: SQLITE_PRIVATE int sqlite3MemdebugNoType(void*,u8);
! 12141: #else
! 12142: # define sqlite3MemdebugSetType(X,Y) /* no-op */
! 12143: # define sqlite3MemdebugHasType(X,Y) 1
! 12144: # define sqlite3MemdebugNoType(X,Y) 1
! 12145: #endif
! 12146: #define MEMTYPE_HEAP 0x01 /* General heap allocations */
! 12147: #define MEMTYPE_LOOKASIDE 0x02 /* Might have been lookaside memory */
! 12148: #define MEMTYPE_SCRATCH 0x04 /* Scratch allocations */
! 12149: #define MEMTYPE_PCACHE 0x08 /* Page cache allocations */
! 12150: #define MEMTYPE_DB 0x10 /* Uses sqlite3DbMalloc, not sqlite_malloc */
! 12151:
! 12152: #endif /* _SQLITEINT_H_ */
! 12153:
! 12154: /************** End of sqliteInt.h *******************************************/
! 12155: /************** Begin file global.c ******************************************/
! 12156: /*
! 12157: ** 2008 June 13
! 12158: **
! 12159: ** The author disclaims copyright to this source code. In place of
! 12160: ** a legal notice, here is a blessing:
! 12161: **
! 12162: ** May you do good and not evil.
! 12163: ** May you find forgiveness for yourself and forgive others.
! 12164: ** May you share freely, never taking more than you give.
! 12165: **
! 12166: *************************************************************************
! 12167: **
! 12168: ** This file contains definitions of global variables and contants.
! 12169: */
! 12170:
! 12171: /* An array to map all upper-case characters into their corresponding
! 12172: ** lower-case character.
! 12173: **
! 12174: ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
! 12175: ** handle case conversions for the UTF character set since the tables
! 12176: ** involved are nearly as big or bigger than SQLite itself.
! 12177: */
! 12178: SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
! 12179: #ifdef SQLITE_ASCII
! 12180: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
! 12181: 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
! 12182: 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
! 12183: 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
! 12184: 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
! 12185: 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
! 12186: 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
! 12187: 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
! 12188: 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
! 12189: 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
! 12190: 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
! 12191: 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
! 12192: 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
! 12193: 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
! 12194: 252,253,254,255
! 12195: #endif
! 12196: #ifdef SQLITE_EBCDIC
! 12197: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
! 12198: 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
! 12199: 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
! 12200: 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
! 12201: 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
! 12202: 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
! 12203: 96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
! 12204: 112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
! 12205: 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
! 12206: 144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
! 12207: 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
! 12208: 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
! 12209: 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
! 12210: 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
! 12211: 224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
! 12212: 239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
! 12213: #endif
! 12214: };
! 12215:
! 12216: /*
! 12217: ** The following 256 byte lookup table is used to support SQLites built-in
! 12218: ** equivalents to the following standard library functions:
! 12219: **
! 12220: ** isspace() 0x01
! 12221: ** isalpha() 0x02
! 12222: ** isdigit() 0x04
! 12223: ** isalnum() 0x06
! 12224: ** isxdigit() 0x08
! 12225: ** toupper() 0x20
! 12226: ** SQLite identifier character 0x40
! 12227: **
! 12228: ** Bit 0x20 is set if the mapped character requires translation to upper
! 12229: ** case. i.e. if the character is a lower-case ASCII character.
! 12230: ** If x is a lower-case ASCII character, then its upper-case equivalent
! 12231: ** is (x - 0x20). Therefore toupper() can be implemented as:
! 12232: **
! 12233: ** (x & ~(map[x]&0x20))
! 12234: **
! 12235: ** Standard function tolower() is implemented using the sqlite3UpperToLower[]
! 12236: ** array. tolower() is used more often than toupper() by SQLite.
! 12237: **
! 12238: ** Bit 0x40 is set if the character non-alphanumeric and can be used in an
! 12239: ** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any
! 12240: ** non-ASCII UTF character. Hence the test for whether or not a character is
! 12241: ** part of an identifier is 0x46.
! 12242: **
! 12243: ** SQLite's versions are identical to the standard versions assuming a
! 12244: ** locale of "C". They are implemented as macros in sqliteInt.h.
! 12245: */
! 12246: #ifdef SQLITE_ASCII
! 12247: SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
! 12248: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */
! 12249: 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */
! 12250: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */
! 12251: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */
! 12252: 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, /* 20..27 !"#$%&' */
! 12253: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
! 12254: 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */
! 12255: 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */
! 12256:
! 12257: 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */
! 12258: 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */
! 12259: 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */
! 12260: 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */
! 12261: 0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */
! 12262: 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */
! 12263: 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */
! 12264: 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */
! 12265:
! 12266: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */
! 12267: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */
! 12268: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */
! 12269: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */
! 12270: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */
! 12271: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */
! 12272: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */
! 12273: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */
! 12274:
! 12275: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */
! 12276: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */
! 12277: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */
! 12278: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */
! 12279: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */
! 12280: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */
! 12281: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
! 12282: 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
! 12283: };
! 12284: #endif
! 12285:
! 12286: #ifndef SQLITE_USE_URI
! 12287: # define SQLITE_USE_URI 0
! 12288: #endif
! 12289:
! 12290: /*
! 12291: ** The following singleton contains the global configuration for
! 12292: ** the SQLite library.
! 12293: */
! 12294: SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
! 12295: SQLITE_DEFAULT_MEMSTATUS, /* bMemstat */
! 12296: 1, /* bCoreMutex */
! 12297: SQLITE_THREADSAFE==1, /* bFullMutex */
! 12298: SQLITE_USE_URI, /* bOpenUri */
! 12299: 0x7ffffffe, /* mxStrlen */
! 12300: 128, /* szLookaside */
! 12301: 500, /* nLookaside */
! 12302: {0,0,0,0,0,0,0,0}, /* m */
! 12303: {0,0,0,0,0,0,0,0,0}, /* mutex */
! 12304: {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
! 12305: (void*)0, /* pHeap */
! 12306: 0, /* nHeap */
! 12307: 0, 0, /* mnHeap, mxHeap */
! 12308: (void*)0, /* pScratch */
! 12309: 0, /* szScratch */
! 12310: 0, /* nScratch */
! 12311: (void*)0, /* pPage */
! 12312: 0, /* szPage */
! 12313: 0, /* nPage */
! 12314: 0, /* mxParserStack */
! 12315: 0, /* sharedCacheEnabled */
! 12316: /* All the rest should always be initialized to zero */
! 12317: 0, /* isInit */
! 12318: 0, /* inProgress */
! 12319: 0, /* isMutexInit */
! 12320: 0, /* isMallocInit */
! 12321: 0, /* isPCacheInit */
! 12322: 0, /* pInitMutex */
! 12323: 0, /* nRefInitMutex */
! 12324: 0, /* xLog */
! 12325: 0, /* pLogArg */
! 12326: 0, /* bLocaltimeFault */
! 12327: };
! 12328:
! 12329:
! 12330: /*
! 12331: ** Hash table for global functions - functions common to all
! 12332: ** database connections. After initialization, this table is
! 12333: ** read-only.
! 12334: */
! 12335: SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
! 12336:
! 12337: /*
! 12338: ** Constant tokens for values 0 and 1.
! 12339: */
! 12340: SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
! 12341: { "0", 1 },
! 12342: { "1", 1 }
! 12343: };
! 12344:
! 12345:
! 12346: /*
! 12347: ** The value of the "pending" byte must be 0x40000000 (1 byte past the
! 12348: ** 1-gibabyte boundary) in a compatible database. SQLite never uses
! 12349: ** the database page that contains the pending byte. It never attempts
! 12350: ** to read or write that page. The pending byte page is set assign
! 12351: ** for use by the VFS layers as space for managing file locks.
! 12352: **
! 12353: ** During testing, it is often desirable to move the pending byte to
! 12354: ** a different position in the file. This allows code that has to
! 12355: ** deal with the pending byte to run on files that are much smaller
! 12356: ** than 1 GiB. The sqlite3_test_control() interface can be used to
! 12357: ** move the pending byte.
! 12358: **
! 12359: ** IMPORTANT: Changing the pending byte to any value other than
! 12360: ** 0x40000000 results in an incompatible database file format!
! 12361: ** Changing the pending byte during operating results in undefined
! 12362: ** and dileterious behavior.
! 12363: */
! 12364: #ifndef SQLITE_OMIT_WSD
! 12365: SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
! 12366: #endif
! 12367:
! 12368: /*
! 12369: ** Properties of opcodes. The OPFLG_INITIALIZER macro is
! 12370: ** created by mkopcodeh.awk during compilation. Data is obtained
! 12371: ** from the comments following the "case OP_xxxx:" statements in
! 12372: ** the vdbe.c file.
! 12373: */
! 12374: SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
! 12375:
! 12376: /************** End of global.c **********************************************/
! 12377: /************** Begin file ctime.c *******************************************/
! 12378: /*
! 12379: ** 2010 February 23
! 12380: **
! 12381: ** The author disclaims copyright to this source code. In place of
! 12382: ** a legal notice, here is a blessing:
! 12383: **
! 12384: ** May you do good and not evil.
! 12385: ** May you find forgiveness for yourself and forgive others.
! 12386: ** May you share freely, never taking more than you give.
! 12387: **
! 12388: *************************************************************************
! 12389: **
! 12390: ** This file implements routines used to report what compile-time options
! 12391: ** SQLite was built with.
! 12392: */
! 12393:
! 12394: #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
! 12395:
! 12396:
! 12397: /*
! 12398: ** An array of names of all compile-time options. This array should
! 12399: ** be sorted A-Z.
! 12400: **
! 12401: ** This array looks large, but in a typical installation actually uses
! 12402: ** only a handful of compile-time options, so most times this array is usually
! 12403: ** rather short and uses little memory space.
! 12404: */
! 12405: static const char * const azCompileOpt[] = {
! 12406:
! 12407: /* These macros are provided to "stringify" the value of the define
! 12408: ** for those options in which the value is meaningful. */
! 12409: #define CTIMEOPT_VAL_(opt) #opt
! 12410: #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
! 12411:
! 12412: #ifdef SQLITE_32BIT_ROWID
! 12413: "32BIT_ROWID",
! 12414: #endif
! 12415: #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
! 12416: "4_BYTE_ALIGNED_MALLOC",
! 12417: #endif
! 12418: #ifdef SQLITE_CASE_SENSITIVE_LIKE
! 12419: "CASE_SENSITIVE_LIKE",
! 12420: #endif
! 12421: #ifdef SQLITE_CHECK_PAGES
! 12422: "CHECK_PAGES",
! 12423: #endif
! 12424: #ifdef SQLITE_COVERAGE_TEST
! 12425: "COVERAGE_TEST",
! 12426: #endif
! 12427: #ifdef SQLITE_DEBUG
! 12428: "DEBUG",
! 12429: #endif
! 12430: #ifdef SQLITE_DEFAULT_LOCKING_MODE
! 12431: "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
! 12432: #endif
! 12433: #ifdef SQLITE_DISABLE_DIRSYNC
! 12434: "DISABLE_DIRSYNC",
! 12435: #endif
! 12436: #ifdef SQLITE_DISABLE_LFS
! 12437: "DISABLE_LFS",
! 12438: #endif
! 12439: #ifdef SQLITE_ENABLE_ATOMIC_WRITE
! 12440: "ENABLE_ATOMIC_WRITE",
! 12441: #endif
! 12442: #ifdef SQLITE_ENABLE_CEROD
! 12443: "ENABLE_CEROD",
! 12444: #endif
! 12445: #ifdef SQLITE_ENABLE_COLUMN_METADATA
! 12446: "ENABLE_COLUMN_METADATA",
! 12447: #endif
! 12448: #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
! 12449: "ENABLE_EXPENSIVE_ASSERT",
! 12450: #endif
! 12451: #ifdef SQLITE_ENABLE_FTS1
! 12452: "ENABLE_FTS1",
! 12453: #endif
! 12454: #ifdef SQLITE_ENABLE_FTS2
! 12455: "ENABLE_FTS2",
! 12456: #endif
! 12457: #ifdef SQLITE_ENABLE_FTS3
! 12458: "ENABLE_FTS3",
! 12459: #endif
! 12460: #ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
! 12461: "ENABLE_FTS3_PARENTHESIS",
! 12462: #endif
! 12463: #ifdef SQLITE_ENABLE_FTS4
! 12464: "ENABLE_FTS4",
! 12465: #endif
! 12466: #ifdef SQLITE_ENABLE_ICU
! 12467: "ENABLE_ICU",
! 12468: #endif
! 12469: #ifdef SQLITE_ENABLE_IOTRACE
! 12470: "ENABLE_IOTRACE",
! 12471: #endif
! 12472: #ifdef SQLITE_ENABLE_LOAD_EXTENSION
! 12473: "ENABLE_LOAD_EXTENSION",
! 12474: #endif
! 12475: #ifdef SQLITE_ENABLE_LOCKING_STYLE
! 12476: "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
! 12477: #endif
! 12478: #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
! 12479: "ENABLE_MEMORY_MANAGEMENT",
! 12480: #endif
! 12481: #ifdef SQLITE_ENABLE_MEMSYS3
! 12482: "ENABLE_MEMSYS3",
! 12483: #endif
! 12484: #ifdef SQLITE_ENABLE_MEMSYS5
! 12485: "ENABLE_MEMSYS5",
! 12486: #endif
! 12487: #ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
! 12488: "ENABLE_OVERSIZE_CELL_CHECK",
! 12489: #endif
! 12490: #ifdef SQLITE_ENABLE_RTREE
! 12491: "ENABLE_RTREE",
! 12492: #endif
! 12493: #ifdef SQLITE_ENABLE_STAT3
! 12494: "ENABLE_STAT3",
! 12495: #endif
! 12496: #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
! 12497: "ENABLE_UNLOCK_NOTIFY",
! 12498: #endif
! 12499: #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
! 12500: "ENABLE_UPDATE_DELETE_LIMIT",
! 12501: #endif
! 12502: #ifdef SQLITE_HAS_CODEC
! 12503: "HAS_CODEC",
! 12504: #endif
! 12505: #ifdef SQLITE_HAVE_ISNAN
! 12506: "HAVE_ISNAN",
! 12507: #endif
! 12508: #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
! 12509: "HOMEGROWN_RECURSIVE_MUTEX",
! 12510: #endif
! 12511: #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
! 12512: "IGNORE_AFP_LOCK_ERRORS",
! 12513: #endif
! 12514: #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
! 12515: "IGNORE_FLOCK_LOCK_ERRORS",
! 12516: #endif
! 12517: #ifdef SQLITE_INT64_TYPE
! 12518: "INT64_TYPE",
! 12519: #endif
! 12520: #ifdef SQLITE_LOCK_TRACE
! 12521: "LOCK_TRACE",
! 12522: #endif
! 12523: #ifdef SQLITE_MAX_SCHEMA_RETRY
! 12524: "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
! 12525: #endif
! 12526: #ifdef SQLITE_MEMDEBUG
! 12527: "MEMDEBUG",
! 12528: #endif
! 12529: #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
! 12530: "MIXED_ENDIAN_64BIT_FLOAT",
! 12531: #endif
! 12532: #ifdef SQLITE_NO_SYNC
! 12533: "NO_SYNC",
! 12534: #endif
! 12535: #ifdef SQLITE_OMIT_ALTERTABLE
! 12536: "OMIT_ALTERTABLE",
! 12537: #endif
! 12538: #ifdef SQLITE_OMIT_ANALYZE
! 12539: "OMIT_ANALYZE",
! 12540: #endif
! 12541: #ifdef SQLITE_OMIT_ATTACH
! 12542: "OMIT_ATTACH",
! 12543: #endif
! 12544: #ifdef SQLITE_OMIT_AUTHORIZATION
! 12545: "OMIT_AUTHORIZATION",
! 12546: #endif
! 12547: #ifdef SQLITE_OMIT_AUTOINCREMENT
! 12548: "OMIT_AUTOINCREMENT",
! 12549: #endif
! 12550: #ifdef SQLITE_OMIT_AUTOINIT
! 12551: "OMIT_AUTOINIT",
! 12552: #endif
! 12553: #ifdef SQLITE_OMIT_AUTOMATIC_INDEX
! 12554: "OMIT_AUTOMATIC_INDEX",
! 12555: #endif
! 12556: #ifdef SQLITE_OMIT_AUTORESET
! 12557: "OMIT_AUTORESET",
! 12558: #endif
! 12559: #ifdef SQLITE_OMIT_AUTOVACUUM
! 12560: "OMIT_AUTOVACUUM",
! 12561: #endif
! 12562: #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
! 12563: "OMIT_BETWEEN_OPTIMIZATION",
! 12564: #endif
! 12565: #ifdef SQLITE_OMIT_BLOB_LITERAL
! 12566: "OMIT_BLOB_LITERAL",
! 12567: #endif
! 12568: #ifdef SQLITE_OMIT_BTREECOUNT
! 12569: "OMIT_BTREECOUNT",
! 12570: #endif
! 12571: #ifdef SQLITE_OMIT_BUILTIN_TEST
! 12572: "OMIT_BUILTIN_TEST",
! 12573: #endif
! 12574: #ifdef SQLITE_OMIT_CAST
! 12575: "OMIT_CAST",
! 12576: #endif
! 12577: #ifdef SQLITE_OMIT_CHECK
! 12578: "OMIT_CHECK",
! 12579: #endif
! 12580: /* // redundant
! 12581: ** #ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
! 12582: ** "OMIT_COMPILEOPTION_DIAGS",
! 12583: ** #endif
! 12584: */
! 12585: #ifdef SQLITE_OMIT_COMPLETE
! 12586: "OMIT_COMPLETE",
! 12587: #endif
! 12588: #ifdef SQLITE_OMIT_COMPOUND_SELECT
! 12589: "OMIT_COMPOUND_SELECT",
! 12590: #endif
! 12591: #ifdef SQLITE_OMIT_DATETIME_FUNCS
! 12592: "OMIT_DATETIME_FUNCS",
! 12593: #endif
! 12594: #ifdef SQLITE_OMIT_DECLTYPE
! 12595: "OMIT_DECLTYPE",
! 12596: #endif
! 12597: #ifdef SQLITE_OMIT_DEPRECATED
! 12598: "OMIT_DEPRECATED",
! 12599: #endif
! 12600: #ifdef SQLITE_OMIT_DISKIO
! 12601: "OMIT_DISKIO",
! 12602: #endif
! 12603: #ifdef SQLITE_OMIT_EXPLAIN
! 12604: "OMIT_EXPLAIN",
! 12605: #endif
! 12606: #ifdef SQLITE_OMIT_FLAG_PRAGMAS
! 12607: "OMIT_FLAG_PRAGMAS",
! 12608: #endif
! 12609: #ifdef SQLITE_OMIT_FLOATING_POINT
! 12610: "OMIT_FLOATING_POINT",
! 12611: #endif
! 12612: #ifdef SQLITE_OMIT_FOREIGN_KEY
! 12613: "OMIT_FOREIGN_KEY",
! 12614: #endif
! 12615: #ifdef SQLITE_OMIT_GET_TABLE
! 12616: "OMIT_GET_TABLE",
! 12617: #endif
! 12618: #ifdef SQLITE_OMIT_INCRBLOB
! 12619: "OMIT_INCRBLOB",
! 12620: #endif
! 12621: #ifdef SQLITE_OMIT_INTEGRITY_CHECK
! 12622: "OMIT_INTEGRITY_CHECK",
! 12623: #endif
! 12624: #ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
! 12625: "OMIT_LIKE_OPTIMIZATION",
! 12626: #endif
! 12627: #ifdef SQLITE_OMIT_LOAD_EXTENSION
! 12628: "OMIT_LOAD_EXTENSION",
! 12629: #endif
! 12630: #ifdef SQLITE_OMIT_LOCALTIME
! 12631: "OMIT_LOCALTIME",
! 12632: #endif
! 12633: #ifdef SQLITE_OMIT_LOOKASIDE
! 12634: "OMIT_LOOKASIDE",
! 12635: #endif
! 12636: #ifdef SQLITE_OMIT_MEMORYDB
! 12637: "OMIT_MEMORYDB",
! 12638: #endif
! 12639: #ifdef SQLITE_OMIT_MERGE_SORT
! 12640: "OMIT_MERGE_SORT",
! 12641: #endif
! 12642: #ifdef SQLITE_OMIT_OR_OPTIMIZATION
! 12643: "OMIT_OR_OPTIMIZATION",
! 12644: #endif
! 12645: #ifdef SQLITE_OMIT_PAGER_PRAGMAS
! 12646: "OMIT_PAGER_PRAGMAS",
! 12647: #endif
! 12648: #ifdef SQLITE_OMIT_PRAGMA
! 12649: "OMIT_PRAGMA",
! 12650: #endif
! 12651: #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
! 12652: "OMIT_PROGRESS_CALLBACK",
! 12653: #endif
! 12654: #ifdef SQLITE_OMIT_QUICKBALANCE
! 12655: "OMIT_QUICKBALANCE",
! 12656: #endif
! 12657: #ifdef SQLITE_OMIT_REINDEX
! 12658: "OMIT_REINDEX",
! 12659: #endif
! 12660: #ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
! 12661: "OMIT_SCHEMA_PRAGMAS",
! 12662: #endif
! 12663: #ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
! 12664: "OMIT_SCHEMA_VERSION_PRAGMAS",
! 12665: #endif
! 12666: #ifdef SQLITE_OMIT_SHARED_CACHE
! 12667: "OMIT_SHARED_CACHE",
! 12668: #endif
! 12669: #ifdef SQLITE_OMIT_SUBQUERY
! 12670: "OMIT_SUBQUERY",
! 12671: #endif
! 12672: #ifdef SQLITE_OMIT_TCL_VARIABLE
! 12673: "OMIT_TCL_VARIABLE",
! 12674: #endif
! 12675: #ifdef SQLITE_OMIT_TEMPDB
! 12676: "OMIT_TEMPDB",
! 12677: #endif
! 12678: #ifdef SQLITE_OMIT_TRACE
! 12679: "OMIT_TRACE",
! 12680: #endif
! 12681: #ifdef SQLITE_OMIT_TRIGGER
! 12682: "OMIT_TRIGGER",
! 12683: #endif
! 12684: #ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
! 12685: "OMIT_TRUNCATE_OPTIMIZATION",
! 12686: #endif
! 12687: #ifdef SQLITE_OMIT_UTF16
! 12688: "OMIT_UTF16",
! 12689: #endif
! 12690: #ifdef SQLITE_OMIT_VACUUM
! 12691: "OMIT_VACUUM",
! 12692: #endif
! 12693: #ifdef SQLITE_OMIT_VIEW
! 12694: "OMIT_VIEW",
! 12695: #endif
! 12696: #ifdef SQLITE_OMIT_VIRTUALTABLE
! 12697: "OMIT_VIRTUALTABLE",
! 12698: #endif
! 12699: #ifdef SQLITE_OMIT_WAL
! 12700: "OMIT_WAL",
! 12701: #endif
! 12702: #ifdef SQLITE_OMIT_WSD
! 12703: "OMIT_WSD",
! 12704: #endif
! 12705: #ifdef SQLITE_OMIT_XFER_OPT
! 12706: "OMIT_XFER_OPT",
! 12707: #endif
! 12708: #ifdef SQLITE_PERFORMANCE_TRACE
! 12709: "PERFORMANCE_TRACE",
! 12710: #endif
! 12711: #ifdef SQLITE_PROXY_DEBUG
! 12712: "PROXY_DEBUG",
! 12713: #endif
! 12714: #ifdef SQLITE_SECURE_DELETE
! 12715: "SECURE_DELETE",
! 12716: #endif
! 12717: #ifdef SQLITE_SMALL_STACK
! 12718: "SMALL_STACK",
! 12719: #endif
! 12720: #ifdef SQLITE_SOUNDEX
! 12721: "SOUNDEX",
! 12722: #endif
! 12723: #ifdef SQLITE_TCL
! 12724: "TCL",
! 12725: #endif
! 12726: #ifdef SQLITE_TEMP_STORE
! 12727: "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
! 12728: #endif
! 12729: #ifdef SQLITE_TEST
! 12730: "TEST",
! 12731: #endif
! 12732: #ifdef SQLITE_THREADSAFE
! 12733: "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
! 12734: #endif
! 12735: #ifdef SQLITE_USE_ALLOCA
! 12736: "USE_ALLOCA",
! 12737: #endif
! 12738: #ifdef SQLITE_ZERO_MALLOC
! 12739: "ZERO_MALLOC"
! 12740: #endif
! 12741: };
! 12742:
! 12743: /*
! 12744: ** Given the name of a compile-time option, return true if that option
! 12745: ** was used and false if not.
! 12746: **
! 12747: ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
! 12748: ** is not required for a match.
! 12749: */
! 12750: SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
! 12751: int i, n;
! 12752: if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
! 12753: n = sqlite3Strlen30(zOptName);
! 12754:
! 12755: /* Since ArraySize(azCompileOpt) is normally in single digits, a
! 12756: ** linear search is adequate. No need for a binary search. */
! 12757: for(i=0; i<ArraySize(azCompileOpt); i++){
! 12758: if( (sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0)
! 12759: && ( (azCompileOpt[i][n]==0) || (azCompileOpt[i][n]=='=') ) ) return 1;
! 12760: }
! 12761: return 0;
! 12762: }
! 12763:
! 12764: /*
! 12765: ** Return the N-th compile-time option string. If N is out of range,
! 12766: ** return a NULL pointer.
! 12767: */
! 12768: SQLITE_API const char *sqlite3_compileoption_get(int N){
! 12769: if( N>=0 && N<ArraySize(azCompileOpt) ){
! 12770: return azCompileOpt[N];
! 12771: }
! 12772: return 0;
! 12773: }
! 12774:
! 12775: #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
! 12776:
! 12777: /************** End of ctime.c ***********************************************/
! 12778: /************** Begin file status.c ******************************************/
! 12779: /*
! 12780: ** 2008 June 18
! 12781: **
! 12782: ** The author disclaims copyright to this source code. In place of
! 12783: ** a legal notice, here is a blessing:
! 12784: **
! 12785: ** May you do good and not evil.
! 12786: ** May you find forgiveness for yourself and forgive others.
! 12787: ** May you share freely, never taking more than you give.
! 12788: **
! 12789: *************************************************************************
! 12790: **
! 12791: ** This module implements the sqlite3_status() interface and related
! 12792: ** functionality.
! 12793: */
! 12794: /************** Include vdbeInt.h in the middle of status.c ******************/
! 12795: /************** Begin file vdbeInt.h *****************************************/
! 12796: /*
! 12797: ** 2003 September 6
! 12798: **
! 12799: ** The author disclaims copyright to this source code. In place of
! 12800: ** a legal notice, here is a blessing:
! 12801: **
! 12802: ** May you do good and not evil.
! 12803: ** May you find forgiveness for yourself and forgive others.
! 12804: ** May you share freely, never taking more than you give.
! 12805: **
! 12806: *************************************************************************
! 12807: ** This is the header file for information that is private to the
! 12808: ** VDBE. This information used to all be at the top of the single
! 12809: ** source code file "vdbe.c". When that file became too big (over
! 12810: ** 6000 lines long) it was split up into several smaller files and
! 12811: ** this header information was factored out.
! 12812: */
! 12813: #ifndef _VDBEINT_H_
! 12814: #define _VDBEINT_H_
! 12815:
! 12816: /*
! 12817: ** SQL is translated into a sequence of instructions to be
! 12818: ** executed by a virtual machine. Each instruction is an instance
! 12819: ** of the following structure.
! 12820: */
! 12821: typedef struct VdbeOp Op;
! 12822:
! 12823: /*
! 12824: ** Boolean values
! 12825: */
! 12826: typedef unsigned char Bool;
! 12827:
! 12828: /* Opaque type used by code in vdbesort.c */
! 12829: typedef struct VdbeSorter VdbeSorter;
! 12830:
! 12831: /* Opaque type used by the explainer */
! 12832: typedef struct Explain Explain;
! 12833:
! 12834: /*
! 12835: ** A cursor is a pointer into a single BTree within a database file.
! 12836: ** The cursor can seek to a BTree entry with a particular key, or
! 12837: ** loop over all entries of the Btree. You can also insert new BTree
! 12838: ** entries or retrieve the key or data from the entry that the cursor
! 12839: ** is currently pointing to.
! 12840: **
! 12841: ** Every cursor that the virtual machine has open is represented by an
! 12842: ** instance of the following structure.
! 12843: */
! 12844: struct VdbeCursor {
! 12845: BtCursor *pCursor; /* The cursor structure of the backend */
! 12846: Btree *pBt; /* Separate file holding temporary table */
! 12847: KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
! 12848: int iDb; /* Index of cursor database in db->aDb[] (or -1) */
! 12849: int pseudoTableReg; /* Register holding pseudotable content. */
! 12850: int nField; /* Number of fields in the header */
! 12851: Bool zeroed; /* True if zeroed out and ready for reuse */
! 12852: Bool rowidIsValid; /* True if lastRowid is valid */
! 12853: Bool atFirst; /* True if pointing to first entry */
! 12854: Bool useRandomRowid; /* Generate new record numbers semi-randomly */
! 12855: Bool nullRow; /* True if pointing to a row with no data */
! 12856: Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
! 12857: Bool isTable; /* True if a table requiring integer keys */
! 12858: Bool isIndex; /* True if an index containing keys only - no data */
! 12859: Bool isOrdered; /* True if the underlying table is BTREE_UNORDERED */
! 12860: Bool isSorter; /* True if a new-style sorter */
! 12861: sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
! 12862: const sqlite3_module *pModule; /* Module for cursor pVtabCursor */
! 12863: i64 seqCount; /* Sequence counter */
! 12864: i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
! 12865: i64 lastRowid; /* Last rowid from a Next or NextIdx operation */
! 12866: VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */
! 12867:
! 12868: /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or
! 12869: ** OP_IsUnique opcode on this cursor. */
! 12870: int seekResult;
! 12871:
! 12872: /* Cached information about the header for the data record that the
! 12873: ** cursor is currently pointing to. Only valid if cacheStatus matches
! 12874: ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
! 12875: ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
! 12876: ** the cache is out of date.
! 12877: **
! 12878: ** aRow might point to (ephemeral) data for the current row, or it might
! 12879: ** be NULL.
! 12880: */
! 12881: u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */
! 12882: int payloadSize; /* Total number of bytes in the record */
! 12883: u32 *aType; /* Type values for all entries in the record */
! 12884: u32 *aOffset; /* Cached offsets to the start of each columns data */
! 12885: u8 *aRow; /* Data for the current row, if all on one page */
! 12886: };
! 12887: typedef struct VdbeCursor VdbeCursor;
! 12888:
! 12889: /*
! 12890: ** When a sub-program is executed (OP_Program), a structure of this type
! 12891: ** is allocated to store the current value of the program counter, as
! 12892: ** well as the current memory cell array and various other frame specific
! 12893: ** values stored in the Vdbe struct. When the sub-program is finished,
! 12894: ** these values are copied back to the Vdbe from the VdbeFrame structure,
! 12895: ** restoring the state of the VM to as it was before the sub-program
! 12896: ** began executing.
! 12897: **
! 12898: ** The memory for a VdbeFrame object is allocated and managed by a memory
! 12899: ** cell in the parent (calling) frame. When the memory cell is deleted or
! 12900: ** overwritten, the VdbeFrame object is not freed immediately. Instead, it
! 12901: ** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
! 12902: ** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
! 12903: ** this instead of deleting the VdbeFrame immediately is to avoid recursive
! 12904: ** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
! 12905: ** child frame are released.
! 12906: **
! 12907: ** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
! 12908: ** set to NULL if the currently executing frame is the main program.
! 12909: */
! 12910: typedef struct VdbeFrame VdbeFrame;
! 12911: struct VdbeFrame {
! 12912: Vdbe *v; /* VM this frame belongs to */
! 12913: int pc; /* Program Counter in parent (calling) frame */
! 12914: Op *aOp; /* Program instructions for parent frame */
! 12915: int nOp; /* Size of aOp array */
! 12916: Mem *aMem; /* Array of memory cells for parent frame */
! 12917: int nMem; /* Number of entries in aMem */
! 12918: u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */
! 12919: int nOnceFlag; /* Number of entries in aOnceFlag */
! 12920: VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
! 12921: u16 nCursor; /* Number of entries in apCsr */
! 12922: void *token; /* Copy of SubProgram.token */
! 12923: int nChildMem; /* Number of memory cells for child frame */
! 12924: int nChildCsr; /* Number of cursors for child frame */
! 12925: i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
! 12926: int nChange; /* Statement changes (Vdbe.nChanges) */
! 12927: VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
! 12928: };
! 12929:
! 12930: #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
! 12931:
! 12932: /*
! 12933: ** A value for VdbeCursor.cacheValid that means the cache is always invalid.
! 12934: */
! 12935: #define CACHE_STALE 0
! 12936:
! 12937: /*
! 12938: ** Internally, the vdbe manipulates nearly all SQL values as Mem
! 12939: ** structures. Each Mem struct may cache multiple representations (string,
! 12940: ** integer etc.) of the same value.
! 12941: */
! 12942: struct Mem {
! 12943: sqlite3 *db; /* The associated database connection */
! 12944: char *z; /* String or BLOB value */
! 12945: double r; /* Real value */
! 12946: union {
! 12947: i64 i; /* Integer value used when MEM_Int is set in flags */
! 12948: int nZero; /* Used when bit MEM_Zero is set in flags */
! 12949: FuncDef *pDef; /* Used only when flags==MEM_Agg */
! 12950: RowSet *pRowSet; /* Used only when flags==MEM_RowSet */
! 12951: VdbeFrame *pFrame; /* Used when flags==MEM_Frame */
! 12952: } u;
! 12953: int n; /* Number of characters in string value, excluding '\0' */
! 12954: u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
! 12955: u8 type; /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
! 12956: u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
! 12957: #ifdef SQLITE_DEBUG
! 12958: Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
! 12959: void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */
! 12960: #endif
! 12961: void (*xDel)(void *); /* If not null, call this function to delete Mem.z */
! 12962: char *zMalloc; /* Dynamic buffer allocated by sqlite3_malloc() */
! 12963: };
! 12964:
! 12965: /* One or more of the following flags are set to indicate the validOK
! 12966: ** representations of the value stored in the Mem struct.
! 12967: **
! 12968: ** If the MEM_Null flag is set, then the value is an SQL NULL value.
! 12969: ** No other flags may be set in this case.
! 12970: **
! 12971: ** If the MEM_Str flag is set then Mem.z points at a string representation.
! 12972: ** Usually this is encoded in the same unicode encoding as the main
! 12973: ** database (see below for exceptions). If the MEM_Term flag is also
! 12974: ** set, then the string is nul terminated. The MEM_Int and MEM_Real
! 12975: ** flags may coexist with the MEM_Str flag.
! 12976: */
! 12977: #define MEM_Null 0x0001 /* Value is NULL */
! 12978: #define MEM_Str 0x0002 /* Value is a string */
! 12979: #define MEM_Int 0x0004 /* Value is an integer */
! 12980: #define MEM_Real 0x0008 /* Value is a real number */
! 12981: #define MEM_Blob 0x0010 /* Value is a BLOB */
! 12982: #define MEM_RowSet 0x0020 /* Value is a RowSet object */
! 12983: #define MEM_Frame 0x0040 /* Value is a VdbeFrame object */
! 12984: #define MEM_Invalid 0x0080 /* Value is undefined */
! 12985: #define MEM_TypeMask 0x00ff /* Mask of type bits */
! 12986:
! 12987: /* Whenever Mem contains a valid string or blob representation, one of
! 12988: ** the following flags must be set to determine the memory management
! 12989: ** policy for Mem.z. The MEM_Term flag tells us whether or not the
! 12990: ** string is \000 or \u0000 terminated
! 12991: */
! 12992: #define MEM_Term 0x0200 /* String rep is nul terminated */
! 12993: #define MEM_Dyn 0x0400 /* Need to call sqliteFree() on Mem.z */
! 12994: #define MEM_Static 0x0800 /* Mem.z points to a static string */
! 12995: #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
! 12996: #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
! 12997: #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
! 12998: #ifdef SQLITE_OMIT_INCRBLOB
! 12999: #undef MEM_Zero
! 13000: #define MEM_Zero 0x0000
! 13001: #endif
! 13002:
! 13003: /*
! 13004: ** Clear any existing type flags from a Mem and replace them with f
! 13005: */
! 13006: #define MemSetTypeFlag(p, f) \
! 13007: ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
! 13008:
! 13009: /*
! 13010: ** Return true if a memory cell is not marked as invalid. This macro
! 13011: ** is for use inside assert() statements only.
! 13012: */
! 13013: #ifdef SQLITE_DEBUG
! 13014: #define memIsValid(M) ((M)->flags & MEM_Invalid)==0
! 13015: #endif
! 13016:
! 13017:
! 13018: /* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
! 13019: ** additional information about auxiliary information bound to arguments
! 13020: ** of the function. This is used to implement the sqlite3_get_auxdata()
! 13021: ** and sqlite3_set_auxdata() APIs. The "auxdata" is some auxiliary data
! 13022: ** that can be associated with a constant argument to a function. This
! 13023: ** allows functions such as "regexp" to compile their constant regular
! 13024: ** expression argument once and reused the compiled code for multiple
! 13025: ** invocations.
! 13026: */
! 13027: struct VdbeFunc {
! 13028: FuncDef *pFunc; /* The definition of the function */
! 13029: int nAux; /* Number of entries allocated for apAux[] */
! 13030: struct AuxData {
! 13031: void *pAux; /* Aux data for the i-th argument */
! 13032: void (*xDelete)(void *); /* Destructor for the aux data */
! 13033: } apAux[1]; /* One slot for each function argument */
! 13034: };
! 13035:
! 13036: /*
! 13037: ** The "context" argument for a installable function. A pointer to an
! 13038: ** instance of this structure is the first argument to the routines used
! 13039: ** implement the SQL functions.
! 13040: **
! 13041: ** There is a typedef for this structure in sqlite.h. So all routines,
! 13042: ** even the public interface to SQLite, can use a pointer to this structure.
! 13043: ** But this file is the only place where the internal details of this
! 13044: ** structure are known.
! 13045: **
! 13046: ** This structure is defined inside of vdbeInt.h because it uses substructures
! 13047: ** (Mem) which are only defined there.
! 13048: */
! 13049: struct sqlite3_context {
! 13050: FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */
! 13051: VdbeFunc *pVdbeFunc; /* Auxilary data, if created. */
! 13052: Mem s; /* The return value is stored here */
! 13053: Mem *pMem; /* Memory cell used to store aggregate context */
! 13054: int isError; /* Error code returned by the function. */
! 13055: CollSeq *pColl; /* Collating sequence */
! 13056: };
! 13057:
! 13058: /*
! 13059: ** An Explain object accumulates indented output which is helpful
! 13060: ** in describing recursive data structures.
! 13061: */
! 13062: struct Explain {
! 13063: Vdbe *pVdbe; /* Attach the explanation to this Vdbe */
! 13064: StrAccum str; /* The string being accumulated */
! 13065: int nIndent; /* Number of elements in aIndent */
! 13066: u16 aIndent[100]; /* Levels of indentation */
! 13067: char zBase[100]; /* Initial space */
! 13068: };
! 13069:
! 13070: /*
! 13071: ** An instance of the virtual machine. This structure contains the complete
! 13072: ** state of the virtual machine.
! 13073: **
! 13074: ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
! 13075: ** is really a pointer to an instance of this structure.
! 13076: **
! 13077: ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
! 13078: ** any virtual table method invocations made by the vdbe program. It is
! 13079: ** set to 2 for xDestroy method calls and 1 for all other methods. This
! 13080: ** variable is used for two purposes: to allow xDestroy methods to execute
! 13081: ** "DROP TABLE" statements and to prevent some nasty side effects of
! 13082: ** malloc failure when SQLite is invoked recursively by a virtual table
! 13083: ** method function.
! 13084: */
! 13085: struct Vdbe {
! 13086: sqlite3 *db; /* The database connection that owns this statement */
! 13087: Op *aOp; /* Space to hold the virtual machine's program */
! 13088: Mem *aMem; /* The memory locations */
! 13089: Mem **apArg; /* Arguments to currently executing user function */
! 13090: Mem *aColName; /* Column names to return */
! 13091: Mem *pResultSet; /* Pointer to an array of results */
! 13092: int nMem; /* Number of memory locations currently allocated */
! 13093: int nOp; /* Number of instructions in the program */
! 13094: int nOpAlloc; /* Number of slots allocated for aOp[] */
! 13095: int nLabel; /* Number of labels used */
! 13096: int nLabelAlloc; /* Number of slots allocated in aLabel[] */
! 13097: int *aLabel; /* Space to hold the labels */
! 13098: u16 nResColumn; /* Number of columns in one row of the result set */
! 13099: u16 nCursor; /* Number of slots in apCsr[] */
! 13100: u32 magic; /* Magic number for sanity checking */
! 13101: char *zErrMsg; /* Error message written here */
! 13102: Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
! 13103: VdbeCursor **apCsr; /* One element of this array for each open cursor */
! 13104: Mem *aVar; /* Values for the OP_Variable opcode. */
! 13105: char **azVar; /* Name of variables */
! 13106: ynVar nVar; /* Number of entries in aVar[] */
! 13107: ynVar nzVar; /* Number of entries in azVar[] */
! 13108: u32 cacheCtr; /* VdbeCursor row cache generation counter */
! 13109: int pc; /* The program counter */
! 13110: int rc; /* Value to return */
! 13111: u8 errorAction; /* Recovery action to do in case of an error */
! 13112: u8 explain; /* True if EXPLAIN present on SQL command */
! 13113: u8 changeCntOn; /* True to update the change-counter */
! 13114: u8 expired; /* True if the VM needs to be recompiled */
! 13115: u8 runOnlyOnce; /* Automatically expire on reset */
! 13116: u8 minWriteFileFormat; /* Minimum file format for writable database files */
! 13117: u8 inVtabMethod; /* See comments above */
! 13118: u8 usesStmtJournal; /* True if uses a statement journal */
! 13119: u8 readOnly; /* True for read-only statements */
! 13120: u8 isPrepareV2; /* True if prepared with prepare_v2() */
! 13121: int nChange; /* Number of db changes made since last reset */
! 13122: yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
! 13123: yDbMask lockMask; /* Subset of btreeMask that requires a lock */
! 13124: int iStatement; /* Statement number (or 0 if has not opened stmt) */
! 13125: int aCounter[3]; /* Counters used by sqlite3_stmt_status() */
! 13126: #ifndef SQLITE_OMIT_TRACE
! 13127: i64 startTime; /* Time when query started - used for profiling */
! 13128: #endif
! 13129: i64 nFkConstraint; /* Number of imm. FK constraints this VM */
! 13130: i64 nStmtDefCons; /* Number of def. constraints when stmt started */
! 13131: char *zSql; /* Text of the SQL statement that generated this */
! 13132: void *pFree; /* Free this when deleting the vdbe */
! 13133: #ifdef SQLITE_DEBUG
! 13134: FILE *trace; /* Write an execution trace here, if not NULL */
! 13135: #endif
! 13136: #ifdef SQLITE_ENABLE_TREE_EXPLAIN
! 13137: Explain *pExplain; /* The explainer */
! 13138: char *zExplain; /* Explanation of data structures */
! 13139: #endif
! 13140: VdbeFrame *pFrame; /* Parent frame */
! 13141: VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
! 13142: int nFrame; /* Number of frames in pFrame list */
! 13143: u32 expmask; /* Binding to these vars invalidates VM */
! 13144: SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
! 13145: int nOnceFlag; /* Size of array aOnceFlag[] */
! 13146: u8 *aOnceFlag; /* Flags for OP_Once */
! 13147: };
! 13148:
! 13149: /*
! 13150: ** The following are allowed values for Vdbe.magic
! 13151: */
! 13152: #define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */
! 13153: #define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */
! 13154: #define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */
! 13155: #define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */
! 13156:
! 13157: /*
! 13158: ** Function prototypes
! 13159: */
! 13160: SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
! 13161: void sqliteVdbePopStack(Vdbe*,int);
! 13162: SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
! 13163: #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
! 13164: SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
! 13165: #endif
! 13166: SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
! 13167: SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
! 13168: SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
! 13169: SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
! 13170: SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
! 13171:
! 13172: int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
! 13173: SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
! 13174: SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
! 13175: SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
! 13176: SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
! 13177: SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
! 13178: SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
! 13179: SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
! 13180: SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
! 13181: SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
! 13182: SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
! 13183: SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
! 13184: SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
! 13185: SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
! 13186: SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
! 13187: #ifdef SQLITE_OMIT_FLOATING_POINT
! 13188: # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
! 13189: #else
! 13190: SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double);
! 13191: #endif
! 13192: SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
! 13193: SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
! 13194: SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
! 13195: SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
! 13196: SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int);
! 13197: SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
! 13198: SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
! 13199: SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
! 13200: SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
! 13201: SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
! 13202: SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
! 13203: SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
! 13204: SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
! 13205: SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
! 13206: #define VdbeMemRelease(X) \
! 13207: if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
! 13208: sqlite3VdbeMemReleaseExternal(X);
! 13209: SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
! 13210: SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
! 13211: SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
! 13212: SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
! 13213: SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
! 13214: SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
! 13215: SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
! 13216: SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
! 13217:
! 13218: #ifdef SQLITE_OMIT_MERGE_SORT
! 13219: # define sqlite3VdbeSorterInit(Y,Z) SQLITE_OK
! 13220: # define sqlite3VdbeSorterWrite(X,Y,Z) SQLITE_OK
! 13221: # define sqlite3VdbeSorterClose(Y,Z)
! 13222: # define sqlite3VdbeSorterRowkey(Y,Z) SQLITE_OK
! 13223: # define sqlite3VdbeSorterRewind(X,Y,Z) SQLITE_OK
! 13224: # define sqlite3VdbeSorterNext(X,Y,Z) SQLITE_OK
! 13225: # define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
! 13226: #else
! 13227: SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
! 13228: SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
! 13229: SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
! 13230: SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
! 13231: SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
! 13232: SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, Mem *);
! 13233: SQLITE_PRIVATE int sqlite3VdbeSorterCompare(VdbeCursor *, Mem *, int *);
! 13234: #endif
! 13235:
! 13236: #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
! 13237: SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
! 13238: SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
! 13239: #else
! 13240: # define sqlite3VdbeEnter(X)
! 13241: # define sqlite3VdbeLeave(X)
! 13242: #endif
! 13243:
! 13244: #ifdef SQLITE_DEBUG
! 13245: SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
! 13246: #endif
! 13247:
! 13248: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 13249: SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
! 13250: #else
! 13251: # define sqlite3VdbeCheckFk(p,i) 0
! 13252: #endif
! 13253:
! 13254: SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
! 13255: #ifdef SQLITE_DEBUG
! 13256: SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*);
! 13257: SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
! 13258: #endif
! 13259: SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
! 13260:
! 13261: #ifndef SQLITE_OMIT_INCRBLOB
! 13262: SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *);
! 13263: #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
! 13264: #else
! 13265: #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
! 13266: #define ExpandBlob(P) SQLITE_OK
! 13267: #endif
! 13268:
! 13269: #endif /* !defined(_VDBEINT_H_) */
! 13270:
! 13271: /************** End of vdbeInt.h *********************************************/
! 13272: /************** Continuing where we left off in status.c *********************/
! 13273:
! 13274: /*
! 13275: ** Variables in which to record status information.
! 13276: */
! 13277: typedef struct sqlite3StatType sqlite3StatType;
! 13278: static SQLITE_WSD struct sqlite3StatType {
! 13279: int nowValue[10]; /* Current value */
! 13280: int mxValue[10]; /* Maximum value */
! 13281: } sqlite3Stat = { {0,}, {0,} };
! 13282:
! 13283:
! 13284: /* The "wsdStat" macro will resolve to the status information
! 13285: ** state vector. If writable static data is unsupported on the target,
! 13286: ** we have to locate the state vector at run-time. In the more common
! 13287: ** case where writable static data is supported, wsdStat can refer directly
! 13288: ** to the "sqlite3Stat" state vector declared above.
! 13289: */
! 13290: #ifdef SQLITE_OMIT_WSD
! 13291: # define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
! 13292: # define wsdStat x[0]
! 13293: #else
! 13294: # define wsdStatInit
! 13295: # define wsdStat sqlite3Stat
! 13296: #endif
! 13297:
! 13298: /*
! 13299: ** Return the current value of a status parameter.
! 13300: */
! 13301: SQLITE_PRIVATE int sqlite3StatusValue(int op){
! 13302: wsdStatInit;
! 13303: assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
! 13304: return wsdStat.nowValue[op];
! 13305: }
! 13306:
! 13307: /*
! 13308: ** Add N to the value of a status record. It is assumed that the
! 13309: ** caller holds appropriate locks.
! 13310: */
! 13311: SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
! 13312: wsdStatInit;
! 13313: assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
! 13314: wsdStat.nowValue[op] += N;
! 13315: if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
! 13316: wsdStat.mxValue[op] = wsdStat.nowValue[op];
! 13317: }
! 13318: }
! 13319:
! 13320: /*
! 13321: ** Set the value of a status to X.
! 13322: */
! 13323: SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
! 13324: wsdStatInit;
! 13325: assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
! 13326: wsdStat.nowValue[op] = X;
! 13327: if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
! 13328: wsdStat.mxValue[op] = wsdStat.nowValue[op];
! 13329: }
! 13330: }
! 13331:
! 13332: /*
! 13333: ** Query status information.
! 13334: **
! 13335: ** This implementation assumes that reading or writing an aligned
! 13336: ** 32-bit integer is an atomic operation. If that assumption is not true,
! 13337: ** then this routine is not threadsafe.
! 13338: */
! 13339: SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
! 13340: wsdStatInit;
! 13341: if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
! 13342: return SQLITE_MISUSE_BKPT;
! 13343: }
! 13344: *pCurrent = wsdStat.nowValue[op];
! 13345: *pHighwater = wsdStat.mxValue[op];
! 13346: if( resetFlag ){
! 13347: wsdStat.mxValue[op] = wsdStat.nowValue[op];
! 13348: }
! 13349: return SQLITE_OK;
! 13350: }
! 13351:
! 13352: /*
! 13353: ** Query status information for a single database connection
! 13354: */
! 13355: SQLITE_API int sqlite3_db_status(
! 13356: sqlite3 *db, /* The database connection whose status is desired */
! 13357: int op, /* Status verb */
! 13358: int *pCurrent, /* Write current value here */
! 13359: int *pHighwater, /* Write high-water mark here */
! 13360: int resetFlag /* Reset high-water mark if true */
! 13361: ){
! 13362: int rc = SQLITE_OK; /* Return code */
! 13363: sqlite3_mutex_enter(db->mutex);
! 13364: switch( op ){
! 13365: case SQLITE_DBSTATUS_LOOKASIDE_USED: {
! 13366: *pCurrent = db->lookaside.nOut;
! 13367: *pHighwater = db->lookaside.mxOut;
! 13368: if( resetFlag ){
! 13369: db->lookaside.mxOut = db->lookaside.nOut;
! 13370: }
! 13371: break;
! 13372: }
! 13373:
! 13374: case SQLITE_DBSTATUS_LOOKASIDE_HIT:
! 13375: case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
! 13376: case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
! 13377: testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
! 13378: testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
! 13379: testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
! 13380: assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
! 13381: assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
! 13382: *pCurrent = 0;
! 13383: *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
! 13384: if( resetFlag ){
! 13385: db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
! 13386: }
! 13387: break;
! 13388: }
! 13389:
! 13390: /*
! 13391: ** Return an approximation for the amount of memory currently used
! 13392: ** by all pagers associated with the given database connection. The
! 13393: ** highwater mark is meaningless and is returned as zero.
! 13394: */
! 13395: case SQLITE_DBSTATUS_CACHE_USED: {
! 13396: int totalUsed = 0;
! 13397: int i;
! 13398: sqlite3BtreeEnterAll(db);
! 13399: for(i=0; i<db->nDb; i++){
! 13400: Btree *pBt = db->aDb[i].pBt;
! 13401: if( pBt ){
! 13402: Pager *pPager = sqlite3BtreePager(pBt);
! 13403: totalUsed += sqlite3PagerMemUsed(pPager);
! 13404: }
! 13405: }
! 13406: sqlite3BtreeLeaveAll(db);
! 13407: *pCurrent = totalUsed;
! 13408: *pHighwater = 0;
! 13409: break;
! 13410: }
! 13411:
! 13412: /*
! 13413: ** *pCurrent gets an accurate estimate of the amount of memory used
! 13414: ** to store the schema for all databases (main, temp, and any ATTACHed
! 13415: ** databases. *pHighwater is set to zero.
! 13416: */
! 13417: case SQLITE_DBSTATUS_SCHEMA_USED: {
! 13418: int i; /* Used to iterate through schemas */
! 13419: int nByte = 0; /* Used to accumulate return value */
! 13420:
! 13421: sqlite3BtreeEnterAll(db);
! 13422: db->pnBytesFreed = &nByte;
! 13423: for(i=0; i<db->nDb; i++){
! 13424: Schema *pSchema = db->aDb[i].pSchema;
! 13425: if( ALWAYS(pSchema!=0) ){
! 13426: HashElem *p;
! 13427:
! 13428: nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
! 13429: pSchema->tblHash.count
! 13430: + pSchema->trigHash.count
! 13431: + pSchema->idxHash.count
! 13432: + pSchema->fkeyHash.count
! 13433: );
! 13434: nByte += sqlite3MallocSize(pSchema->tblHash.ht);
! 13435: nByte += sqlite3MallocSize(pSchema->trigHash.ht);
! 13436: nByte += sqlite3MallocSize(pSchema->idxHash.ht);
! 13437: nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
! 13438:
! 13439: for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
! 13440: sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
! 13441: }
! 13442: for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
! 13443: sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
! 13444: }
! 13445: }
! 13446: }
! 13447: db->pnBytesFreed = 0;
! 13448: sqlite3BtreeLeaveAll(db);
! 13449:
! 13450: *pHighwater = 0;
! 13451: *pCurrent = nByte;
! 13452: break;
! 13453: }
! 13454:
! 13455: /*
! 13456: ** *pCurrent gets an accurate estimate of the amount of memory used
! 13457: ** to store all prepared statements.
! 13458: ** *pHighwater is set to zero.
! 13459: */
! 13460: case SQLITE_DBSTATUS_STMT_USED: {
! 13461: struct Vdbe *pVdbe; /* Used to iterate through VMs */
! 13462: int nByte = 0; /* Used to accumulate return value */
! 13463:
! 13464: db->pnBytesFreed = &nByte;
! 13465: for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
! 13466: sqlite3VdbeDeleteObject(db, pVdbe);
! 13467: }
! 13468: db->pnBytesFreed = 0;
! 13469:
! 13470: *pHighwater = 0;
! 13471: *pCurrent = nByte;
! 13472:
! 13473: break;
! 13474: }
! 13475:
! 13476: /*
! 13477: ** Set *pCurrent to the total cache hits or misses encountered by all
! 13478: ** pagers the database handle is connected to. *pHighwater is always set
! 13479: ** to zero.
! 13480: */
! 13481: case SQLITE_DBSTATUS_CACHE_HIT:
! 13482: case SQLITE_DBSTATUS_CACHE_MISS: {
! 13483: int i;
! 13484: int nRet = 0;
! 13485: assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
! 13486:
! 13487: for(i=0; i<db->nDb; i++){
! 13488: if( db->aDb[i].pBt ){
! 13489: Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
! 13490: sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
! 13491: }
! 13492: }
! 13493: *pHighwater = 0;
! 13494: *pCurrent = nRet;
! 13495: break;
! 13496: }
! 13497:
! 13498: default: {
! 13499: rc = SQLITE_ERROR;
! 13500: }
! 13501: }
! 13502: sqlite3_mutex_leave(db->mutex);
! 13503: return rc;
! 13504: }
! 13505:
! 13506: /************** End of status.c **********************************************/
! 13507: /************** Begin file date.c ********************************************/
! 13508: /*
! 13509: ** 2003 October 31
! 13510: **
! 13511: ** The author disclaims copyright to this source code. In place of
! 13512: ** a legal notice, here is a blessing:
! 13513: **
! 13514: ** May you do good and not evil.
! 13515: ** May you find forgiveness for yourself and forgive others.
! 13516: ** May you share freely, never taking more than you give.
! 13517: **
! 13518: *************************************************************************
! 13519: ** This file contains the C functions that implement date and time
! 13520: ** functions for SQLite.
! 13521: **
! 13522: ** There is only one exported symbol in this file - the function
! 13523: ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
! 13524: ** All other code has file scope.
! 13525: **
! 13526: ** SQLite processes all times and dates as Julian Day numbers. The
! 13527: ** dates and times are stored as the number of days since noon
! 13528: ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
! 13529: ** calendar system.
! 13530: **
! 13531: ** 1970-01-01 00:00:00 is JD 2440587.5
! 13532: ** 2000-01-01 00:00:00 is JD 2451544.5
! 13533: **
! 13534: ** This implemention requires years to be expressed as a 4-digit number
! 13535: ** which means that only dates between 0000-01-01 and 9999-12-31 can
! 13536: ** be represented, even though julian day numbers allow a much wider
! 13537: ** range of dates.
! 13538: **
! 13539: ** The Gregorian calendar system is used for all dates and times,
! 13540: ** even those that predate the Gregorian calendar. Historians usually
! 13541: ** use the Julian calendar for dates prior to 1582-10-15 and for some
! 13542: ** dates afterwards, depending on locale. Beware of this difference.
! 13543: **
! 13544: ** The conversion algorithms are implemented based on descriptions
! 13545: ** in the following text:
! 13546: **
! 13547: ** Jean Meeus
! 13548: ** Astronomical Algorithms, 2nd Edition, 1998
! 13549: ** ISBM 0-943396-61-1
! 13550: ** Willmann-Bell, Inc
! 13551: ** Richmond, Virginia (USA)
! 13552: */
! 13553: /* #include <stdlib.h> */
! 13554: /* #include <assert.h> */
! 13555: #include <time.h>
! 13556:
! 13557: #ifndef SQLITE_OMIT_DATETIME_FUNCS
! 13558:
! 13559:
! 13560: /*
! 13561: ** A structure for holding a single date and time.
! 13562: */
! 13563: typedef struct DateTime DateTime;
! 13564: struct DateTime {
! 13565: sqlite3_int64 iJD; /* The julian day number times 86400000 */
! 13566: int Y, M, D; /* Year, month, and day */
! 13567: int h, m; /* Hour and minutes */
! 13568: int tz; /* Timezone offset in minutes */
! 13569: double s; /* Seconds */
! 13570: char validYMD; /* True (1) if Y,M,D are valid */
! 13571: char validHMS; /* True (1) if h,m,s are valid */
! 13572: char validJD; /* True (1) if iJD is valid */
! 13573: char validTZ; /* True (1) if tz is valid */
! 13574: };
! 13575:
! 13576:
! 13577: /*
! 13578: ** Convert zDate into one or more integers. Additional arguments
! 13579: ** come in groups of 5 as follows:
! 13580: **
! 13581: ** N number of digits in the integer
! 13582: ** min minimum allowed value of the integer
! 13583: ** max maximum allowed value of the integer
! 13584: ** nextC first character after the integer
! 13585: ** pVal where to write the integers value.
! 13586: **
! 13587: ** Conversions continue until one with nextC==0 is encountered.
! 13588: ** The function returns the number of successful conversions.
! 13589: */
! 13590: static int getDigits(const char *zDate, ...){
! 13591: va_list ap;
! 13592: int val;
! 13593: int N;
! 13594: int min;
! 13595: int max;
! 13596: int nextC;
! 13597: int *pVal;
! 13598: int cnt = 0;
! 13599: va_start(ap, zDate);
! 13600: do{
! 13601: N = va_arg(ap, int);
! 13602: min = va_arg(ap, int);
! 13603: max = va_arg(ap, int);
! 13604: nextC = va_arg(ap, int);
! 13605: pVal = va_arg(ap, int*);
! 13606: val = 0;
! 13607: while( N-- ){
! 13608: if( !sqlite3Isdigit(*zDate) ){
! 13609: goto end_getDigits;
! 13610: }
! 13611: val = val*10 + *zDate - '0';
! 13612: zDate++;
! 13613: }
! 13614: if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
! 13615: goto end_getDigits;
! 13616: }
! 13617: *pVal = val;
! 13618: zDate++;
! 13619: cnt++;
! 13620: }while( nextC );
! 13621: end_getDigits:
! 13622: va_end(ap);
! 13623: return cnt;
! 13624: }
! 13625:
! 13626: /*
! 13627: ** Parse a timezone extension on the end of a date-time.
! 13628: ** The extension is of the form:
! 13629: **
! 13630: ** (+/-)HH:MM
! 13631: **
! 13632: ** Or the "zulu" notation:
! 13633: **
! 13634: ** Z
! 13635: **
! 13636: ** If the parse is successful, write the number of minutes
! 13637: ** of change in p->tz and return 0. If a parser error occurs,
! 13638: ** return non-zero.
! 13639: **
! 13640: ** A missing specifier is not considered an error.
! 13641: */
! 13642: static int parseTimezone(const char *zDate, DateTime *p){
! 13643: int sgn = 0;
! 13644: int nHr, nMn;
! 13645: int c;
! 13646: while( sqlite3Isspace(*zDate) ){ zDate++; }
! 13647: p->tz = 0;
! 13648: c = *zDate;
! 13649: if( c=='-' ){
! 13650: sgn = -1;
! 13651: }else if( c=='+' ){
! 13652: sgn = +1;
! 13653: }else if( c=='Z' || c=='z' ){
! 13654: zDate++;
! 13655: goto zulu_time;
! 13656: }else{
! 13657: return c!=0;
! 13658: }
! 13659: zDate++;
! 13660: if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
! 13661: return 1;
! 13662: }
! 13663: zDate += 5;
! 13664: p->tz = sgn*(nMn + nHr*60);
! 13665: zulu_time:
! 13666: while( sqlite3Isspace(*zDate) ){ zDate++; }
! 13667: return *zDate!=0;
! 13668: }
! 13669:
! 13670: /*
! 13671: ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
! 13672: ** The HH, MM, and SS must each be exactly 2 digits. The
! 13673: ** fractional seconds FFFF can be one or more digits.
! 13674: **
! 13675: ** Return 1 if there is a parsing error and 0 on success.
! 13676: */
! 13677: static int parseHhMmSs(const char *zDate, DateTime *p){
! 13678: int h, m, s;
! 13679: double ms = 0.0;
! 13680: if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
! 13681: return 1;
! 13682: }
! 13683: zDate += 5;
! 13684: if( *zDate==':' ){
! 13685: zDate++;
! 13686: if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
! 13687: return 1;
! 13688: }
! 13689: zDate += 2;
! 13690: if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
! 13691: double rScale = 1.0;
! 13692: zDate++;
! 13693: while( sqlite3Isdigit(*zDate) ){
! 13694: ms = ms*10.0 + *zDate - '0';
! 13695: rScale *= 10.0;
! 13696: zDate++;
! 13697: }
! 13698: ms /= rScale;
! 13699: }
! 13700: }else{
! 13701: s = 0;
! 13702: }
! 13703: p->validJD = 0;
! 13704: p->validHMS = 1;
! 13705: p->h = h;
! 13706: p->m = m;
! 13707: p->s = s + ms;
! 13708: if( parseTimezone(zDate, p) ) return 1;
! 13709: p->validTZ = (p->tz!=0)?1:0;
! 13710: return 0;
! 13711: }
! 13712:
! 13713: /*
! 13714: ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
! 13715: ** that the YYYY-MM-DD is according to the Gregorian calendar.
! 13716: **
! 13717: ** Reference: Meeus page 61
! 13718: */
! 13719: static void computeJD(DateTime *p){
! 13720: int Y, M, D, A, B, X1, X2;
! 13721:
! 13722: if( p->validJD ) return;
! 13723: if( p->validYMD ){
! 13724: Y = p->Y;
! 13725: M = p->M;
! 13726: D = p->D;
! 13727: }else{
! 13728: Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
! 13729: M = 1;
! 13730: D = 1;
! 13731: }
! 13732: if( M<=2 ){
! 13733: Y--;
! 13734: M += 12;
! 13735: }
! 13736: A = Y/100;
! 13737: B = 2 - A + (A/4);
! 13738: X1 = 36525*(Y+4716)/100;
! 13739: X2 = 306001*(M+1)/10000;
! 13740: p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
! 13741: p->validJD = 1;
! 13742: if( p->validHMS ){
! 13743: p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
! 13744: if( p->validTZ ){
! 13745: p->iJD -= p->tz*60000;
! 13746: p->validYMD = 0;
! 13747: p->validHMS = 0;
! 13748: p->validTZ = 0;
! 13749: }
! 13750: }
! 13751: }
! 13752:
! 13753: /*
! 13754: ** Parse dates of the form
! 13755: **
! 13756: ** YYYY-MM-DD HH:MM:SS.FFF
! 13757: ** YYYY-MM-DD HH:MM:SS
! 13758: ** YYYY-MM-DD HH:MM
! 13759: ** YYYY-MM-DD
! 13760: **
! 13761: ** Write the result into the DateTime structure and return 0
! 13762: ** on success and 1 if the input string is not a well-formed
! 13763: ** date.
! 13764: */
! 13765: static int parseYyyyMmDd(const char *zDate, DateTime *p){
! 13766: int Y, M, D, neg;
! 13767:
! 13768: if( zDate[0]=='-' ){
! 13769: zDate++;
! 13770: neg = 1;
! 13771: }else{
! 13772: neg = 0;
! 13773: }
! 13774: if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
! 13775: return 1;
! 13776: }
! 13777: zDate += 10;
! 13778: while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
! 13779: if( parseHhMmSs(zDate, p)==0 ){
! 13780: /* We got the time */
! 13781: }else if( *zDate==0 ){
! 13782: p->validHMS = 0;
! 13783: }else{
! 13784: return 1;
! 13785: }
! 13786: p->validJD = 0;
! 13787: p->validYMD = 1;
! 13788: p->Y = neg ? -Y : Y;
! 13789: p->M = M;
! 13790: p->D = D;
! 13791: if( p->validTZ ){
! 13792: computeJD(p);
! 13793: }
! 13794: return 0;
! 13795: }
! 13796:
! 13797: /*
! 13798: ** Set the time to the current time reported by the VFS.
! 13799: **
! 13800: ** Return the number of errors.
! 13801: */
! 13802: static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
! 13803: sqlite3 *db = sqlite3_context_db_handle(context);
! 13804: if( sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD)==SQLITE_OK ){
! 13805: p->validJD = 1;
! 13806: return 0;
! 13807: }else{
! 13808: return 1;
! 13809: }
! 13810: }
! 13811:
! 13812: /*
! 13813: ** Attempt to parse the given string into a Julian Day Number. Return
! 13814: ** the number of errors.
! 13815: **
! 13816: ** The following are acceptable forms for the input string:
! 13817: **
! 13818: ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
! 13819: ** DDDD.DD
! 13820: ** now
! 13821: **
! 13822: ** In the first form, the +/-HH:MM is always optional. The fractional
! 13823: ** seconds extension (the ".FFF") is optional. The seconds portion
! 13824: ** (":SS.FFF") is option. The year and date can be omitted as long
! 13825: ** as there is a time string. The time string can be omitted as long
! 13826: ** as there is a year and date.
! 13827: */
! 13828: static int parseDateOrTime(
! 13829: sqlite3_context *context,
! 13830: const char *zDate,
! 13831: DateTime *p
! 13832: ){
! 13833: double r;
! 13834: if( parseYyyyMmDd(zDate,p)==0 ){
! 13835: return 0;
! 13836: }else if( parseHhMmSs(zDate, p)==0 ){
! 13837: return 0;
! 13838: }else if( sqlite3StrICmp(zDate,"now")==0){
! 13839: return setDateTimeToCurrent(context, p);
! 13840: }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
! 13841: p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
! 13842: p->validJD = 1;
! 13843: return 0;
! 13844: }
! 13845: return 1;
! 13846: }
! 13847:
! 13848: /*
! 13849: ** Compute the Year, Month, and Day from the julian day number.
! 13850: */
! 13851: static void computeYMD(DateTime *p){
! 13852: int Z, A, B, C, D, E, X1;
! 13853: if( p->validYMD ) return;
! 13854: if( !p->validJD ){
! 13855: p->Y = 2000;
! 13856: p->M = 1;
! 13857: p->D = 1;
! 13858: }else{
! 13859: Z = (int)((p->iJD + 43200000)/86400000);
! 13860: A = (int)((Z - 1867216.25)/36524.25);
! 13861: A = Z + 1 + A - (A/4);
! 13862: B = A + 1524;
! 13863: C = (int)((B - 122.1)/365.25);
! 13864: D = (36525*C)/100;
! 13865: E = (int)((B-D)/30.6001);
! 13866: X1 = (int)(30.6001*E);
! 13867: p->D = B - D - X1;
! 13868: p->M = E<14 ? E-1 : E-13;
! 13869: p->Y = p->M>2 ? C - 4716 : C - 4715;
! 13870: }
! 13871: p->validYMD = 1;
! 13872: }
! 13873:
! 13874: /*
! 13875: ** Compute the Hour, Minute, and Seconds from the julian day number.
! 13876: */
! 13877: static void computeHMS(DateTime *p){
! 13878: int s;
! 13879: if( p->validHMS ) return;
! 13880: computeJD(p);
! 13881: s = (int)((p->iJD + 43200000) % 86400000);
! 13882: p->s = s/1000.0;
! 13883: s = (int)p->s;
! 13884: p->s -= s;
! 13885: p->h = s/3600;
! 13886: s -= p->h*3600;
! 13887: p->m = s/60;
! 13888: p->s += s - p->m*60;
! 13889: p->validHMS = 1;
! 13890: }
! 13891:
! 13892: /*
! 13893: ** Compute both YMD and HMS
! 13894: */
! 13895: static void computeYMD_HMS(DateTime *p){
! 13896: computeYMD(p);
! 13897: computeHMS(p);
! 13898: }
! 13899:
! 13900: /*
! 13901: ** Clear the YMD and HMS and the TZ
! 13902: */
! 13903: static void clearYMD_HMS_TZ(DateTime *p){
! 13904: p->validYMD = 0;
! 13905: p->validHMS = 0;
! 13906: p->validTZ = 0;
! 13907: }
! 13908:
! 13909: /*
! 13910: ** On recent Windows platforms, the localtime_s() function is available
! 13911: ** as part of the "Secure CRT". It is essentially equivalent to
! 13912: ** localtime_r() available under most POSIX platforms, except that the
! 13913: ** order of the parameters is reversed.
! 13914: **
! 13915: ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
! 13916: **
! 13917: ** If the user has not indicated to use localtime_r() or localtime_s()
! 13918: ** already, check for an MSVC build environment that provides
! 13919: ** localtime_s().
! 13920: */
! 13921: #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
! 13922: defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
! 13923: #define HAVE_LOCALTIME_S 1
! 13924: #endif
! 13925:
! 13926: #ifndef SQLITE_OMIT_LOCALTIME
! 13927: /*
! 13928: ** The following routine implements the rough equivalent of localtime_r()
! 13929: ** using whatever operating-system specific localtime facility that
! 13930: ** is available. This routine returns 0 on success and
! 13931: ** non-zero on any kind of error.
! 13932: **
! 13933: ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
! 13934: ** routine will always fail.
! 13935: */
! 13936: static int osLocaltime(time_t *t, struct tm *pTm){
! 13937: int rc;
! 13938: #if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \
! 13939: && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S)
! 13940: struct tm *pX;
! 13941: #if SQLITE_THREADSAFE>0
! 13942: sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
! 13943: #endif
! 13944: sqlite3_mutex_enter(mutex);
! 13945: pX = localtime(t);
! 13946: #ifndef SQLITE_OMIT_BUILTIN_TEST
! 13947: if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
! 13948: #endif
! 13949: if( pX ) *pTm = *pX;
! 13950: sqlite3_mutex_leave(mutex);
! 13951: rc = pX==0;
! 13952: #else
! 13953: #ifndef SQLITE_OMIT_BUILTIN_TEST
! 13954: if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
! 13955: #endif
! 13956: #if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R
! 13957: rc = localtime_r(t, pTm)==0;
! 13958: #else
! 13959: rc = localtime_s(pTm, t);
! 13960: #endif /* HAVE_LOCALTIME_R */
! 13961: #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
! 13962: return rc;
! 13963: }
! 13964: #endif /* SQLITE_OMIT_LOCALTIME */
! 13965:
! 13966:
! 13967: #ifndef SQLITE_OMIT_LOCALTIME
! 13968: /*
! 13969: ** Compute the difference (in milliseconds) between localtime and UTC
! 13970: ** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
! 13971: ** return this value and set *pRc to SQLITE_OK.
! 13972: **
! 13973: ** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
! 13974: ** is undefined in this case.
! 13975: */
! 13976: static sqlite3_int64 localtimeOffset(
! 13977: DateTime *p, /* Date at which to calculate offset */
! 13978: sqlite3_context *pCtx, /* Write error here if one occurs */
! 13979: int *pRc /* OUT: Error code. SQLITE_OK or ERROR */
! 13980: ){
! 13981: DateTime x, y;
! 13982: time_t t;
! 13983: struct tm sLocal;
! 13984:
! 13985: /* Initialize the contents of sLocal to avoid a compiler warning. */
! 13986: memset(&sLocal, 0, sizeof(sLocal));
! 13987:
! 13988: x = *p;
! 13989: computeYMD_HMS(&x);
! 13990: if( x.Y<1971 || x.Y>=2038 ){
! 13991: x.Y = 2000;
! 13992: x.M = 1;
! 13993: x.D = 1;
! 13994: x.h = 0;
! 13995: x.m = 0;
! 13996: x.s = 0.0;
! 13997: } else {
! 13998: int s = (int)(x.s + 0.5);
! 13999: x.s = s;
! 14000: }
! 14001: x.tz = 0;
! 14002: x.validJD = 0;
! 14003: computeJD(&x);
! 14004: t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
! 14005: if( osLocaltime(&t, &sLocal) ){
! 14006: sqlite3_result_error(pCtx, "local time unavailable", -1);
! 14007: *pRc = SQLITE_ERROR;
! 14008: return 0;
! 14009: }
! 14010: y.Y = sLocal.tm_year + 1900;
! 14011: y.M = sLocal.tm_mon + 1;
! 14012: y.D = sLocal.tm_mday;
! 14013: y.h = sLocal.tm_hour;
! 14014: y.m = sLocal.tm_min;
! 14015: y.s = sLocal.tm_sec;
! 14016: y.validYMD = 1;
! 14017: y.validHMS = 1;
! 14018: y.validJD = 0;
! 14019: y.validTZ = 0;
! 14020: computeJD(&y);
! 14021: *pRc = SQLITE_OK;
! 14022: return y.iJD - x.iJD;
! 14023: }
! 14024: #endif /* SQLITE_OMIT_LOCALTIME */
! 14025:
! 14026: /*
! 14027: ** Process a modifier to a date-time stamp. The modifiers are
! 14028: ** as follows:
! 14029: **
! 14030: ** NNN days
! 14031: ** NNN hours
! 14032: ** NNN minutes
! 14033: ** NNN.NNNN seconds
! 14034: ** NNN months
! 14035: ** NNN years
! 14036: ** start of month
! 14037: ** start of year
! 14038: ** start of week
! 14039: ** start of day
! 14040: ** weekday N
! 14041: ** unixepoch
! 14042: ** localtime
! 14043: ** utc
! 14044: **
! 14045: ** Return 0 on success and 1 if there is any kind of error. If the error
! 14046: ** is in a system call (i.e. localtime()), then an error message is written
! 14047: ** to context pCtx. If the error is an unrecognized modifier, no error is
! 14048: ** written to pCtx.
! 14049: */
! 14050: static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
! 14051: int rc = 1;
! 14052: int n;
! 14053: double r;
! 14054: char *z, zBuf[30];
! 14055: z = zBuf;
! 14056: for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
! 14057: z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
! 14058: }
! 14059: z[n] = 0;
! 14060: switch( z[0] ){
! 14061: #ifndef SQLITE_OMIT_LOCALTIME
! 14062: case 'l': {
! 14063: /* localtime
! 14064: **
! 14065: ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
! 14066: ** show local time.
! 14067: */
! 14068: if( strcmp(z, "localtime")==0 ){
! 14069: computeJD(p);
! 14070: p->iJD += localtimeOffset(p, pCtx, &rc);
! 14071: clearYMD_HMS_TZ(p);
! 14072: }
! 14073: break;
! 14074: }
! 14075: #endif
! 14076: case 'u': {
! 14077: /*
! 14078: ** unixepoch
! 14079: **
! 14080: ** Treat the current value of p->iJD as the number of
! 14081: ** seconds since 1970. Convert to a real julian day number.
! 14082: */
! 14083: if( strcmp(z, "unixepoch")==0 && p->validJD ){
! 14084: p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
! 14085: clearYMD_HMS_TZ(p);
! 14086: rc = 0;
! 14087: }
! 14088: #ifndef SQLITE_OMIT_LOCALTIME
! 14089: else if( strcmp(z, "utc")==0 ){
! 14090: sqlite3_int64 c1;
! 14091: computeJD(p);
! 14092: c1 = localtimeOffset(p, pCtx, &rc);
! 14093: if( rc==SQLITE_OK ){
! 14094: p->iJD -= c1;
! 14095: clearYMD_HMS_TZ(p);
! 14096: p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
! 14097: }
! 14098: }
! 14099: #endif
! 14100: break;
! 14101: }
! 14102: case 'w': {
! 14103: /*
! 14104: ** weekday N
! 14105: **
! 14106: ** Move the date to the same time on the next occurrence of
! 14107: ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
! 14108: ** date is already on the appropriate weekday, this is a no-op.
! 14109: */
! 14110: if( strncmp(z, "weekday ", 8)==0
! 14111: && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
! 14112: && (n=(int)r)==r && n>=0 && r<7 ){
! 14113: sqlite3_int64 Z;
! 14114: computeYMD_HMS(p);
! 14115: p->validTZ = 0;
! 14116: p->validJD = 0;
! 14117: computeJD(p);
! 14118: Z = ((p->iJD + 129600000)/86400000) % 7;
! 14119: if( Z>n ) Z -= 7;
! 14120: p->iJD += (n - Z)*86400000;
! 14121: clearYMD_HMS_TZ(p);
! 14122: rc = 0;
! 14123: }
! 14124: break;
! 14125: }
! 14126: case 's': {
! 14127: /*
! 14128: ** start of TTTTT
! 14129: **
! 14130: ** Move the date backwards to the beginning of the current day,
! 14131: ** or month or year.
! 14132: */
! 14133: if( strncmp(z, "start of ", 9)!=0 ) break;
! 14134: z += 9;
! 14135: computeYMD(p);
! 14136: p->validHMS = 1;
! 14137: p->h = p->m = 0;
! 14138: p->s = 0.0;
! 14139: p->validTZ = 0;
! 14140: p->validJD = 0;
! 14141: if( strcmp(z,"month")==0 ){
! 14142: p->D = 1;
! 14143: rc = 0;
! 14144: }else if( strcmp(z,"year")==0 ){
! 14145: computeYMD(p);
! 14146: p->M = 1;
! 14147: p->D = 1;
! 14148: rc = 0;
! 14149: }else if( strcmp(z,"day")==0 ){
! 14150: rc = 0;
! 14151: }
! 14152: break;
! 14153: }
! 14154: case '+':
! 14155: case '-':
! 14156: case '0':
! 14157: case '1':
! 14158: case '2':
! 14159: case '3':
! 14160: case '4':
! 14161: case '5':
! 14162: case '6':
! 14163: case '7':
! 14164: case '8':
! 14165: case '9': {
! 14166: double rRounder;
! 14167: for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
! 14168: if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
! 14169: rc = 1;
! 14170: break;
! 14171: }
! 14172: if( z[n]==':' ){
! 14173: /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
! 14174: ** specified number of hours, minutes, seconds, and fractional seconds
! 14175: ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
! 14176: ** omitted.
! 14177: */
! 14178: const char *z2 = z;
! 14179: DateTime tx;
! 14180: sqlite3_int64 day;
! 14181: if( !sqlite3Isdigit(*z2) ) z2++;
! 14182: memset(&tx, 0, sizeof(tx));
! 14183: if( parseHhMmSs(z2, &tx) ) break;
! 14184: computeJD(&tx);
! 14185: tx.iJD -= 43200000;
! 14186: day = tx.iJD/86400000;
! 14187: tx.iJD -= day*86400000;
! 14188: if( z[0]=='-' ) tx.iJD = -tx.iJD;
! 14189: computeJD(p);
! 14190: clearYMD_HMS_TZ(p);
! 14191: p->iJD += tx.iJD;
! 14192: rc = 0;
! 14193: break;
! 14194: }
! 14195: z += n;
! 14196: while( sqlite3Isspace(*z) ) z++;
! 14197: n = sqlite3Strlen30(z);
! 14198: if( n>10 || n<3 ) break;
! 14199: if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
! 14200: computeJD(p);
! 14201: rc = 0;
! 14202: rRounder = r<0 ? -0.5 : +0.5;
! 14203: if( n==3 && strcmp(z,"day")==0 ){
! 14204: p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
! 14205: }else if( n==4 && strcmp(z,"hour")==0 ){
! 14206: p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
! 14207: }else if( n==6 && strcmp(z,"minute")==0 ){
! 14208: p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
! 14209: }else if( n==6 && strcmp(z,"second")==0 ){
! 14210: p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
! 14211: }else if( n==5 && strcmp(z,"month")==0 ){
! 14212: int x, y;
! 14213: computeYMD_HMS(p);
! 14214: p->M += (int)r;
! 14215: x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
! 14216: p->Y += x;
! 14217: p->M -= x*12;
! 14218: p->validJD = 0;
! 14219: computeJD(p);
! 14220: y = (int)r;
! 14221: if( y!=r ){
! 14222: p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
! 14223: }
! 14224: }else if( n==4 && strcmp(z,"year")==0 ){
! 14225: int y = (int)r;
! 14226: computeYMD_HMS(p);
! 14227: p->Y += y;
! 14228: p->validJD = 0;
! 14229: computeJD(p);
! 14230: if( y!=r ){
! 14231: p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
! 14232: }
! 14233: }else{
! 14234: rc = 1;
! 14235: }
! 14236: clearYMD_HMS_TZ(p);
! 14237: break;
! 14238: }
! 14239: default: {
! 14240: break;
! 14241: }
! 14242: }
! 14243: return rc;
! 14244: }
! 14245:
! 14246: /*
! 14247: ** Process time function arguments. argv[0] is a date-time stamp.
! 14248: ** argv[1] and following are modifiers. Parse them all and write
! 14249: ** the resulting time into the DateTime structure p. Return 0
! 14250: ** on success and 1 if there are any errors.
! 14251: **
! 14252: ** If there are zero parameters (if even argv[0] is undefined)
! 14253: ** then assume a default value of "now" for argv[0].
! 14254: */
! 14255: static int isDate(
! 14256: sqlite3_context *context,
! 14257: int argc,
! 14258: sqlite3_value **argv,
! 14259: DateTime *p
! 14260: ){
! 14261: int i;
! 14262: const unsigned char *z;
! 14263: int eType;
! 14264: memset(p, 0, sizeof(*p));
! 14265: if( argc==0 ){
! 14266: return setDateTimeToCurrent(context, p);
! 14267: }
! 14268: if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
! 14269: || eType==SQLITE_INTEGER ){
! 14270: p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
! 14271: p->validJD = 1;
! 14272: }else{
! 14273: z = sqlite3_value_text(argv[0]);
! 14274: if( !z || parseDateOrTime(context, (char*)z, p) ){
! 14275: return 1;
! 14276: }
! 14277: }
! 14278: for(i=1; i<argc; i++){
! 14279: z = sqlite3_value_text(argv[i]);
! 14280: if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
! 14281: }
! 14282: return 0;
! 14283: }
! 14284:
! 14285:
! 14286: /*
! 14287: ** The following routines implement the various date and time functions
! 14288: ** of SQLite.
! 14289: */
! 14290:
! 14291: /*
! 14292: ** julianday( TIMESTRING, MOD, MOD, ...)
! 14293: **
! 14294: ** Return the julian day number of the date specified in the arguments
! 14295: */
! 14296: static void juliandayFunc(
! 14297: sqlite3_context *context,
! 14298: int argc,
! 14299: sqlite3_value **argv
! 14300: ){
! 14301: DateTime x;
! 14302: if( isDate(context, argc, argv, &x)==0 ){
! 14303: computeJD(&x);
! 14304: sqlite3_result_double(context, x.iJD/86400000.0);
! 14305: }
! 14306: }
! 14307:
! 14308: /*
! 14309: ** datetime( TIMESTRING, MOD, MOD, ...)
! 14310: **
! 14311: ** Return YYYY-MM-DD HH:MM:SS
! 14312: */
! 14313: static void datetimeFunc(
! 14314: sqlite3_context *context,
! 14315: int argc,
! 14316: sqlite3_value **argv
! 14317: ){
! 14318: DateTime x;
! 14319: if( isDate(context, argc, argv, &x)==0 ){
! 14320: char zBuf[100];
! 14321: computeYMD_HMS(&x);
! 14322: sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
! 14323: x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
! 14324: sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
! 14325: }
! 14326: }
! 14327:
! 14328: /*
! 14329: ** time( TIMESTRING, MOD, MOD, ...)
! 14330: **
! 14331: ** Return HH:MM:SS
! 14332: */
! 14333: static void timeFunc(
! 14334: sqlite3_context *context,
! 14335: int argc,
! 14336: sqlite3_value **argv
! 14337: ){
! 14338: DateTime x;
! 14339: if( isDate(context, argc, argv, &x)==0 ){
! 14340: char zBuf[100];
! 14341: computeHMS(&x);
! 14342: sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
! 14343: sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
! 14344: }
! 14345: }
! 14346:
! 14347: /*
! 14348: ** date( TIMESTRING, MOD, MOD, ...)
! 14349: **
! 14350: ** Return YYYY-MM-DD
! 14351: */
! 14352: static void dateFunc(
! 14353: sqlite3_context *context,
! 14354: int argc,
! 14355: sqlite3_value **argv
! 14356: ){
! 14357: DateTime x;
! 14358: if( isDate(context, argc, argv, &x)==0 ){
! 14359: char zBuf[100];
! 14360: computeYMD(&x);
! 14361: sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
! 14362: sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
! 14363: }
! 14364: }
! 14365:
! 14366: /*
! 14367: ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
! 14368: **
! 14369: ** Return a string described by FORMAT. Conversions as follows:
! 14370: **
! 14371: ** %d day of month
! 14372: ** %f ** fractional seconds SS.SSS
! 14373: ** %H hour 00-24
! 14374: ** %j day of year 000-366
! 14375: ** %J ** Julian day number
! 14376: ** %m month 01-12
! 14377: ** %M minute 00-59
! 14378: ** %s seconds since 1970-01-01
! 14379: ** %S seconds 00-59
! 14380: ** %w day of week 0-6 sunday==0
! 14381: ** %W week of year 00-53
! 14382: ** %Y year 0000-9999
! 14383: ** %% %
! 14384: */
! 14385: static void strftimeFunc(
! 14386: sqlite3_context *context,
! 14387: int argc,
! 14388: sqlite3_value **argv
! 14389: ){
! 14390: DateTime x;
! 14391: u64 n;
! 14392: size_t i,j;
! 14393: char *z;
! 14394: sqlite3 *db;
! 14395: const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
! 14396: char zBuf[100];
! 14397: if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
! 14398: db = sqlite3_context_db_handle(context);
! 14399: for(i=0, n=1; zFmt[i]; i++, n++){
! 14400: if( zFmt[i]=='%' ){
! 14401: switch( zFmt[i+1] ){
! 14402: case 'd':
! 14403: case 'H':
! 14404: case 'm':
! 14405: case 'M':
! 14406: case 'S':
! 14407: case 'W':
! 14408: n++;
! 14409: /* fall thru */
! 14410: case 'w':
! 14411: case '%':
! 14412: break;
! 14413: case 'f':
! 14414: n += 8;
! 14415: break;
! 14416: case 'j':
! 14417: n += 3;
! 14418: break;
! 14419: case 'Y':
! 14420: n += 8;
! 14421: break;
! 14422: case 's':
! 14423: case 'J':
! 14424: n += 50;
! 14425: break;
! 14426: default:
! 14427: return; /* ERROR. return a NULL */
! 14428: }
! 14429: i++;
! 14430: }
! 14431: }
! 14432: testcase( n==sizeof(zBuf)-1 );
! 14433: testcase( n==sizeof(zBuf) );
! 14434: testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
! 14435: testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
! 14436: if( n<sizeof(zBuf) ){
! 14437: z = zBuf;
! 14438: }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
! 14439: sqlite3_result_error_toobig(context);
! 14440: return;
! 14441: }else{
! 14442: z = sqlite3DbMallocRaw(db, (int)n);
! 14443: if( z==0 ){
! 14444: sqlite3_result_error_nomem(context);
! 14445: return;
! 14446: }
! 14447: }
! 14448: computeJD(&x);
! 14449: computeYMD_HMS(&x);
! 14450: for(i=j=0; zFmt[i]; i++){
! 14451: if( zFmt[i]!='%' ){
! 14452: z[j++] = zFmt[i];
! 14453: }else{
! 14454: i++;
! 14455: switch( zFmt[i] ){
! 14456: case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
! 14457: case 'f': {
! 14458: double s = x.s;
! 14459: if( s>59.999 ) s = 59.999;
! 14460: sqlite3_snprintf(7, &z[j],"%06.3f", s);
! 14461: j += sqlite3Strlen30(&z[j]);
! 14462: break;
! 14463: }
! 14464: case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
! 14465: case 'W': /* Fall thru */
! 14466: case 'j': {
! 14467: int nDay; /* Number of days since 1st day of year */
! 14468: DateTime y = x;
! 14469: y.validJD = 0;
! 14470: y.M = 1;
! 14471: y.D = 1;
! 14472: computeJD(&y);
! 14473: nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
! 14474: if( zFmt[i]=='W' ){
! 14475: int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
! 14476: wd = (int)(((x.iJD+43200000)/86400000)%7);
! 14477: sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
! 14478: j += 2;
! 14479: }else{
! 14480: sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
! 14481: j += 3;
! 14482: }
! 14483: break;
! 14484: }
! 14485: case 'J': {
! 14486: sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
! 14487: j+=sqlite3Strlen30(&z[j]);
! 14488: break;
! 14489: }
! 14490: case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
! 14491: case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
! 14492: case 's': {
! 14493: sqlite3_snprintf(30,&z[j],"%lld",
! 14494: (i64)(x.iJD/1000 - 21086676*(i64)10000));
! 14495: j += sqlite3Strlen30(&z[j]);
! 14496: break;
! 14497: }
! 14498: case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
! 14499: case 'w': {
! 14500: z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
! 14501: break;
! 14502: }
! 14503: case 'Y': {
! 14504: sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
! 14505: break;
! 14506: }
! 14507: default: z[j++] = '%'; break;
! 14508: }
! 14509: }
! 14510: }
! 14511: z[j] = 0;
! 14512: sqlite3_result_text(context, z, -1,
! 14513: z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
! 14514: }
! 14515:
! 14516: /*
! 14517: ** current_time()
! 14518: **
! 14519: ** This function returns the same value as time('now').
! 14520: */
! 14521: static void ctimeFunc(
! 14522: sqlite3_context *context,
! 14523: int NotUsed,
! 14524: sqlite3_value **NotUsed2
! 14525: ){
! 14526: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 14527: timeFunc(context, 0, 0);
! 14528: }
! 14529:
! 14530: /*
! 14531: ** current_date()
! 14532: **
! 14533: ** This function returns the same value as date('now').
! 14534: */
! 14535: static void cdateFunc(
! 14536: sqlite3_context *context,
! 14537: int NotUsed,
! 14538: sqlite3_value **NotUsed2
! 14539: ){
! 14540: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 14541: dateFunc(context, 0, 0);
! 14542: }
! 14543:
! 14544: /*
! 14545: ** current_timestamp()
! 14546: **
! 14547: ** This function returns the same value as datetime('now').
! 14548: */
! 14549: static void ctimestampFunc(
! 14550: sqlite3_context *context,
! 14551: int NotUsed,
! 14552: sqlite3_value **NotUsed2
! 14553: ){
! 14554: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 14555: datetimeFunc(context, 0, 0);
! 14556: }
! 14557: #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
! 14558:
! 14559: #ifdef SQLITE_OMIT_DATETIME_FUNCS
! 14560: /*
! 14561: ** If the library is compiled to omit the full-scale date and time
! 14562: ** handling (to get a smaller binary), the following minimal version
! 14563: ** of the functions current_time(), current_date() and current_timestamp()
! 14564: ** are included instead. This is to support column declarations that
! 14565: ** include "DEFAULT CURRENT_TIME" etc.
! 14566: **
! 14567: ** This function uses the C-library functions time(), gmtime()
! 14568: ** and strftime(). The format string to pass to strftime() is supplied
! 14569: ** as the user-data for the function.
! 14570: */
! 14571: static void currentTimeFunc(
! 14572: sqlite3_context *context,
! 14573: int argc,
! 14574: sqlite3_value **argv
! 14575: ){
! 14576: time_t t;
! 14577: char *zFormat = (char *)sqlite3_user_data(context);
! 14578: sqlite3 *db;
! 14579: sqlite3_int64 iT;
! 14580: struct tm *pTm;
! 14581: struct tm sNow;
! 14582: char zBuf[20];
! 14583:
! 14584: UNUSED_PARAMETER(argc);
! 14585: UNUSED_PARAMETER(argv);
! 14586:
! 14587: db = sqlite3_context_db_handle(context);
! 14588: if( sqlite3OsCurrentTimeInt64(db->pVfs, &iT) ) return;
! 14589: t = iT/1000 - 10000*(sqlite3_int64)21086676;
! 14590: #ifdef HAVE_GMTIME_R
! 14591: pTm = gmtime_r(&t, &sNow);
! 14592: #else
! 14593: sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 14594: pTm = gmtime(&t);
! 14595: if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
! 14596: sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 14597: #endif
! 14598: if( pTm ){
! 14599: strftime(zBuf, 20, zFormat, &sNow);
! 14600: sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
! 14601: }
! 14602: }
! 14603: #endif
! 14604:
! 14605: /*
! 14606: ** This function registered all of the above C functions as SQL
! 14607: ** functions. This should be the only routine in this file with
! 14608: ** external linkage.
! 14609: */
! 14610: SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
! 14611: static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
! 14612: #ifndef SQLITE_OMIT_DATETIME_FUNCS
! 14613: FUNCTION(julianday, -1, 0, 0, juliandayFunc ),
! 14614: FUNCTION(date, -1, 0, 0, dateFunc ),
! 14615: FUNCTION(time, -1, 0, 0, timeFunc ),
! 14616: FUNCTION(datetime, -1, 0, 0, datetimeFunc ),
! 14617: FUNCTION(strftime, -1, 0, 0, strftimeFunc ),
! 14618: FUNCTION(current_time, 0, 0, 0, ctimeFunc ),
! 14619: FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
! 14620: FUNCTION(current_date, 0, 0, 0, cdateFunc ),
! 14621: #else
! 14622: STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc),
! 14623: STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc),
! 14624: STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
! 14625: #endif
! 14626: };
! 14627: int i;
! 14628: FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
! 14629: FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
! 14630:
! 14631: for(i=0; i<ArraySize(aDateTimeFuncs); i++){
! 14632: sqlite3FuncDefInsert(pHash, &aFunc[i]);
! 14633: }
! 14634: }
! 14635:
! 14636: /************** End of date.c ************************************************/
! 14637: /************** Begin file os.c **********************************************/
! 14638: /*
! 14639: ** 2005 November 29
! 14640: **
! 14641: ** The author disclaims copyright to this source code. In place of
! 14642: ** a legal notice, here is a blessing:
! 14643: **
! 14644: ** May you do good and not evil.
! 14645: ** May you find forgiveness for yourself and forgive others.
! 14646: ** May you share freely, never taking more than you give.
! 14647: **
! 14648: ******************************************************************************
! 14649: **
! 14650: ** This file contains OS interface code that is common to all
! 14651: ** architectures.
! 14652: */
! 14653: #define _SQLITE_OS_C_ 1
! 14654: #undef _SQLITE_OS_C_
! 14655:
! 14656: /*
! 14657: ** The default SQLite sqlite3_vfs implementations do not allocate
! 14658: ** memory (actually, os_unix.c allocates a small amount of memory
! 14659: ** from within OsOpen()), but some third-party implementations may.
! 14660: ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
! 14661: ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
! 14662: **
! 14663: ** The following functions are instrumented for malloc() failure
! 14664: ** testing:
! 14665: **
! 14666: ** sqlite3OsRead()
! 14667: ** sqlite3OsWrite()
! 14668: ** sqlite3OsSync()
! 14669: ** sqlite3OsFileSize()
! 14670: ** sqlite3OsLock()
! 14671: ** sqlite3OsCheckReservedLock()
! 14672: ** sqlite3OsFileControl()
! 14673: ** sqlite3OsShmMap()
! 14674: ** sqlite3OsOpen()
! 14675: ** sqlite3OsDelete()
! 14676: ** sqlite3OsAccess()
! 14677: ** sqlite3OsFullPathname()
! 14678: **
! 14679: */
! 14680: #if defined(SQLITE_TEST)
! 14681: SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
! 14682: #define DO_OS_MALLOC_TEST(x) \
! 14683: if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) { \
! 14684: void *pTstAlloc = sqlite3Malloc(10); \
! 14685: if (!pTstAlloc) return SQLITE_IOERR_NOMEM; \
! 14686: sqlite3_free(pTstAlloc); \
! 14687: }
! 14688: #else
! 14689: #define DO_OS_MALLOC_TEST(x)
! 14690: #endif
! 14691:
! 14692: /*
! 14693: ** The following routines are convenience wrappers around methods
! 14694: ** of the sqlite3_file object. This is mostly just syntactic sugar. All
! 14695: ** of this would be completely automatic if SQLite were coded using
! 14696: ** C++ instead of plain old C.
! 14697: */
! 14698: SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
! 14699: int rc = SQLITE_OK;
! 14700: if( pId->pMethods ){
! 14701: rc = pId->pMethods->xClose(pId);
! 14702: pId->pMethods = 0;
! 14703: }
! 14704: return rc;
! 14705: }
! 14706: SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
! 14707: DO_OS_MALLOC_TEST(id);
! 14708: return id->pMethods->xRead(id, pBuf, amt, offset);
! 14709: }
! 14710: SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
! 14711: DO_OS_MALLOC_TEST(id);
! 14712: return id->pMethods->xWrite(id, pBuf, amt, offset);
! 14713: }
! 14714: SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
! 14715: return id->pMethods->xTruncate(id, size);
! 14716: }
! 14717: SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
! 14718: DO_OS_MALLOC_TEST(id);
! 14719: return id->pMethods->xSync(id, flags);
! 14720: }
! 14721: SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
! 14722: DO_OS_MALLOC_TEST(id);
! 14723: return id->pMethods->xFileSize(id, pSize);
! 14724: }
! 14725: SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
! 14726: DO_OS_MALLOC_TEST(id);
! 14727: return id->pMethods->xLock(id, lockType);
! 14728: }
! 14729: SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
! 14730: return id->pMethods->xUnlock(id, lockType);
! 14731: }
! 14732: SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
! 14733: DO_OS_MALLOC_TEST(id);
! 14734: return id->pMethods->xCheckReservedLock(id, pResOut);
! 14735: }
! 14736:
! 14737: /*
! 14738: ** Use sqlite3OsFileControl() when we are doing something that might fail
! 14739: ** and we need to know about the failures. Use sqlite3OsFileControlHint()
! 14740: ** when simply tossing information over the wall to the VFS and we do not
! 14741: ** really care if the VFS receives and understands the information since it
! 14742: ** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
! 14743: ** routine has no return value since the return value would be meaningless.
! 14744: */
! 14745: SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
! 14746: DO_OS_MALLOC_TEST(id);
! 14747: return id->pMethods->xFileControl(id, op, pArg);
! 14748: }
! 14749: SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
! 14750: (void)id->pMethods->xFileControl(id, op, pArg);
! 14751: }
! 14752:
! 14753: SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
! 14754: int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
! 14755: return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
! 14756: }
! 14757: SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
! 14758: return id->pMethods->xDeviceCharacteristics(id);
! 14759: }
! 14760: SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
! 14761: return id->pMethods->xShmLock(id, offset, n, flags);
! 14762: }
! 14763: SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
! 14764: id->pMethods->xShmBarrier(id);
! 14765: }
! 14766: SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
! 14767: return id->pMethods->xShmUnmap(id, deleteFlag);
! 14768: }
! 14769: SQLITE_PRIVATE int sqlite3OsShmMap(
! 14770: sqlite3_file *id, /* Database file handle */
! 14771: int iPage,
! 14772: int pgsz,
! 14773: int bExtend, /* True to extend file if necessary */
! 14774: void volatile **pp /* OUT: Pointer to mapping */
! 14775: ){
! 14776: DO_OS_MALLOC_TEST(id);
! 14777: return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
! 14778: }
! 14779:
! 14780: /*
! 14781: ** The next group of routines are convenience wrappers around the
! 14782: ** VFS methods.
! 14783: */
! 14784: SQLITE_PRIVATE int sqlite3OsOpen(
! 14785: sqlite3_vfs *pVfs,
! 14786: const char *zPath,
! 14787: sqlite3_file *pFile,
! 14788: int flags,
! 14789: int *pFlagsOut
! 14790: ){
! 14791: int rc;
! 14792: DO_OS_MALLOC_TEST(0);
! 14793: /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
! 14794: ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
! 14795: ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
! 14796: ** reaching the VFS. */
! 14797: rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
! 14798: assert( rc==SQLITE_OK || pFile->pMethods==0 );
! 14799: return rc;
! 14800: }
! 14801: SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
! 14802: DO_OS_MALLOC_TEST(0);
! 14803: assert( dirSync==0 || dirSync==1 );
! 14804: return pVfs->xDelete(pVfs, zPath, dirSync);
! 14805: }
! 14806: SQLITE_PRIVATE int sqlite3OsAccess(
! 14807: sqlite3_vfs *pVfs,
! 14808: const char *zPath,
! 14809: int flags,
! 14810: int *pResOut
! 14811: ){
! 14812: DO_OS_MALLOC_TEST(0);
! 14813: return pVfs->xAccess(pVfs, zPath, flags, pResOut);
! 14814: }
! 14815: SQLITE_PRIVATE int sqlite3OsFullPathname(
! 14816: sqlite3_vfs *pVfs,
! 14817: const char *zPath,
! 14818: int nPathOut,
! 14819: char *zPathOut
! 14820: ){
! 14821: DO_OS_MALLOC_TEST(0);
! 14822: zPathOut[0] = 0;
! 14823: return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
! 14824: }
! 14825: #ifndef SQLITE_OMIT_LOAD_EXTENSION
! 14826: SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
! 14827: return pVfs->xDlOpen(pVfs, zPath);
! 14828: }
! 14829: SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
! 14830: pVfs->xDlError(pVfs, nByte, zBufOut);
! 14831: }
! 14832: SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
! 14833: return pVfs->xDlSym(pVfs, pHdle, zSym);
! 14834: }
! 14835: SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
! 14836: pVfs->xDlClose(pVfs, pHandle);
! 14837: }
! 14838: #endif /* SQLITE_OMIT_LOAD_EXTENSION */
! 14839: SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
! 14840: return pVfs->xRandomness(pVfs, nByte, zBufOut);
! 14841: }
! 14842: SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
! 14843: return pVfs->xSleep(pVfs, nMicro);
! 14844: }
! 14845: SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
! 14846: int rc;
! 14847: /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
! 14848: ** method to get the current date and time if that method is available
! 14849: ** (if iVersion is 2 or greater and the function pointer is not NULL) and
! 14850: ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
! 14851: ** unavailable.
! 14852: */
! 14853: if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
! 14854: rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
! 14855: }else{
! 14856: double r;
! 14857: rc = pVfs->xCurrentTime(pVfs, &r);
! 14858: *pTimeOut = (sqlite3_int64)(r*86400000.0);
! 14859: }
! 14860: return rc;
! 14861: }
! 14862:
! 14863: SQLITE_PRIVATE int sqlite3OsOpenMalloc(
! 14864: sqlite3_vfs *pVfs,
! 14865: const char *zFile,
! 14866: sqlite3_file **ppFile,
! 14867: int flags,
! 14868: int *pOutFlags
! 14869: ){
! 14870: int rc = SQLITE_NOMEM;
! 14871: sqlite3_file *pFile;
! 14872: pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
! 14873: if( pFile ){
! 14874: rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
! 14875: if( rc!=SQLITE_OK ){
! 14876: sqlite3_free(pFile);
! 14877: }else{
! 14878: *ppFile = pFile;
! 14879: }
! 14880: }
! 14881: return rc;
! 14882: }
! 14883: SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
! 14884: int rc = SQLITE_OK;
! 14885: assert( pFile );
! 14886: rc = sqlite3OsClose(pFile);
! 14887: sqlite3_free(pFile);
! 14888: return rc;
! 14889: }
! 14890:
! 14891: /*
! 14892: ** This function is a wrapper around the OS specific implementation of
! 14893: ** sqlite3_os_init(). The purpose of the wrapper is to provide the
! 14894: ** ability to simulate a malloc failure, so that the handling of an
! 14895: ** error in sqlite3_os_init() by the upper layers can be tested.
! 14896: */
! 14897: SQLITE_PRIVATE int sqlite3OsInit(void){
! 14898: void *p = sqlite3_malloc(10);
! 14899: if( p==0 ) return SQLITE_NOMEM;
! 14900: sqlite3_free(p);
! 14901: return sqlite3_os_init();
! 14902: }
! 14903:
! 14904: /*
! 14905: ** The list of all registered VFS implementations.
! 14906: */
! 14907: static sqlite3_vfs * SQLITE_WSD vfsList = 0;
! 14908: #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
! 14909:
! 14910: /*
! 14911: ** Locate a VFS by name. If no name is given, simply return the
! 14912: ** first VFS on the list.
! 14913: */
! 14914: SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
! 14915: sqlite3_vfs *pVfs = 0;
! 14916: #if SQLITE_THREADSAFE
! 14917: sqlite3_mutex *mutex;
! 14918: #endif
! 14919: #ifndef SQLITE_OMIT_AUTOINIT
! 14920: int rc = sqlite3_initialize();
! 14921: if( rc ) return 0;
! 14922: #endif
! 14923: #if SQLITE_THREADSAFE
! 14924: mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
! 14925: #endif
! 14926: sqlite3_mutex_enter(mutex);
! 14927: for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
! 14928: if( zVfs==0 ) break;
! 14929: if( strcmp(zVfs, pVfs->zName)==0 ) break;
! 14930: }
! 14931: sqlite3_mutex_leave(mutex);
! 14932: return pVfs;
! 14933: }
! 14934:
! 14935: /*
! 14936: ** Unlink a VFS from the linked list
! 14937: */
! 14938: static void vfsUnlink(sqlite3_vfs *pVfs){
! 14939: assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
! 14940: if( pVfs==0 ){
! 14941: /* No-op */
! 14942: }else if( vfsList==pVfs ){
! 14943: vfsList = pVfs->pNext;
! 14944: }else if( vfsList ){
! 14945: sqlite3_vfs *p = vfsList;
! 14946: while( p->pNext && p->pNext!=pVfs ){
! 14947: p = p->pNext;
! 14948: }
! 14949: if( p->pNext==pVfs ){
! 14950: p->pNext = pVfs->pNext;
! 14951: }
! 14952: }
! 14953: }
! 14954:
! 14955: /*
! 14956: ** Register a VFS with the system. It is harmless to register the same
! 14957: ** VFS multiple times. The new VFS becomes the default if makeDflt is
! 14958: ** true.
! 14959: */
! 14960: SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
! 14961: MUTEX_LOGIC(sqlite3_mutex *mutex;)
! 14962: #ifndef SQLITE_OMIT_AUTOINIT
! 14963: int rc = sqlite3_initialize();
! 14964: if( rc ) return rc;
! 14965: #endif
! 14966: MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
! 14967: sqlite3_mutex_enter(mutex);
! 14968: vfsUnlink(pVfs);
! 14969: if( makeDflt || vfsList==0 ){
! 14970: pVfs->pNext = vfsList;
! 14971: vfsList = pVfs;
! 14972: }else{
! 14973: pVfs->pNext = vfsList->pNext;
! 14974: vfsList->pNext = pVfs;
! 14975: }
! 14976: assert(vfsList);
! 14977: sqlite3_mutex_leave(mutex);
! 14978: return SQLITE_OK;
! 14979: }
! 14980:
! 14981: /*
! 14982: ** Unregister a VFS so that it is no longer accessible.
! 14983: */
! 14984: SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
! 14985: #if SQLITE_THREADSAFE
! 14986: sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
! 14987: #endif
! 14988: sqlite3_mutex_enter(mutex);
! 14989: vfsUnlink(pVfs);
! 14990: sqlite3_mutex_leave(mutex);
! 14991: return SQLITE_OK;
! 14992: }
! 14993:
! 14994: /************** End of os.c **************************************************/
! 14995: /************** Begin file fault.c *******************************************/
! 14996: /*
! 14997: ** 2008 Jan 22
! 14998: **
! 14999: ** The author disclaims copyright to this source code. In place of
! 15000: ** a legal notice, here is a blessing:
! 15001: **
! 15002: ** May you do good and not evil.
! 15003: ** May you find forgiveness for yourself and forgive others.
! 15004: ** May you share freely, never taking more than you give.
! 15005: **
! 15006: *************************************************************************
! 15007: **
! 15008: ** This file contains code to support the concept of "benign"
! 15009: ** malloc failures (when the xMalloc() or xRealloc() method of the
! 15010: ** sqlite3_mem_methods structure fails to allocate a block of memory
! 15011: ** and returns 0).
! 15012: **
! 15013: ** Most malloc failures are non-benign. After they occur, SQLite
! 15014: ** abandons the current operation and returns an error code (usually
! 15015: ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
! 15016: ** fatal. For example, if a malloc fails while resizing a hash table, this
! 15017: ** is completely recoverable simply by not carrying out the resize. The
! 15018: ** hash table will continue to function normally. So a malloc failure
! 15019: ** during a hash table resize is a benign fault.
! 15020: */
! 15021:
! 15022:
! 15023: #ifndef SQLITE_OMIT_BUILTIN_TEST
! 15024:
! 15025: /*
! 15026: ** Global variables.
! 15027: */
! 15028: typedef struct BenignMallocHooks BenignMallocHooks;
! 15029: static SQLITE_WSD struct BenignMallocHooks {
! 15030: void (*xBenignBegin)(void);
! 15031: void (*xBenignEnd)(void);
! 15032: } sqlite3Hooks = { 0, 0 };
! 15033:
! 15034: /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
! 15035: ** structure. If writable static data is unsupported on the target,
! 15036: ** we have to locate the state vector at run-time. In the more common
! 15037: ** case where writable static data is supported, wsdHooks can refer directly
! 15038: ** to the "sqlite3Hooks" state vector declared above.
! 15039: */
! 15040: #ifdef SQLITE_OMIT_WSD
! 15041: # define wsdHooksInit \
! 15042: BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
! 15043: # define wsdHooks x[0]
! 15044: #else
! 15045: # define wsdHooksInit
! 15046: # define wsdHooks sqlite3Hooks
! 15047: #endif
! 15048:
! 15049:
! 15050: /*
! 15051: ** Register hooks to call when sqlite3BeginBenignMalloc() and
! 15052: ** sqlite3EndBenignMalloc() are called, respectively.
! 15053: */
! 15054: SQLITE_PRIVATE void sqlite3BenignMallocHooks(
! 15055: void (*xBenignBegin)(void),
! 15056: void (*xBenignEnd)(void)
! 15057: ){
! 15058: wsdHooksInit;
! 15059: wsdHooks.xBenignBegin = xBenignBegin;
! 15060: wsdHooks.xBenignEnd = xBenignEnd;
! 15061: }
! 15062:
! 15063: /*
! 15064: ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
! 15065: ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
! 15066: ** indicates that subsequent malloc failures are non-benign.
! 15067: */
! 15068: SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
! 15069: wsdHooksInit;
! 15070: if( wsdHooks.xBenignBegin ){
! 15071: wsdHooks.xBenignBegin();
! 15072: }
! 15073: }
! 15074: SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
! 15075: wsdHooksInit;
! 15076: if( wsdHooks.xBenignEnd ){
! 15077: wsdHooks.xBenignEnd();
! 15078: }
! 15079: }
! 15080:
! 15081: #endif /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
! 15082:
! 15083: /************** End of fault.c ***********************************************/
! 15084: /************** Begin file mem0.c ********************************************/
! 15085: /*
! 15086: ** 2008 October 28
! 15087: **
! 15088: ** The author disclaims copyright to this source code. In place of
! 15089: ** a legal notice, here is a blessing:
! 15090: **
! 15091: ** May you do good and not evil.
! 15092: ** May you find forgiveness for yourself and forgive others.
! 15093: ** May you share freely, never taking more than you give.
! 15094: **
! 15095: *************************************************************************
! 15096: **
! 15097: ** This file contains a no-op memory allocation drivers for use when
! 15098: ** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
! 15099: ** here always fail. SQLite will not operate with these drivers. These
! 15100: ** are merely placeholders. Real drivers must be substituted using
! 15101: ** sqlite3_config() before SQLite will operate.
! 15102: */
! 15103:
! 15104: /*
! 15105: ** This version of the memory allocator is the default. It is
! 15106: ** used when no other memory allocator is specified using compile-time
! 15107: ** macros.
! 15108: */
! 15109: #ifdef SQLITE_ZERO_MALLOC
! 15110:
! 15111: /*
! 15112: ** No-op versions of all memory allocation routines
! 15113: */
! 15114: static void *sqlite3MemMalloc(int nByte){ return 0; }
! 15115: static void sqlite3MemFree(void *pPrior){ return; }
! 15116: static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
! 15117: static int sqlite3MemSize(void *pPrior){ return 0; }
! 15118: static int sqlite3MemRoundup(int n){ return n; }
! 15119: static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
! 15120: static void sqlite3MemShutdown(void *NotUsed){ return; }
! 15121:
! 15122: /*
! 15123: ** This routine is the only routine in this file with external linkage.
! 15124: **
! 15125: ** Populate the low-level memory allocation function pointers in
! 15126: ** sqlite3GlobalConfig.m with pointers to the routines in this file.
! 15127: */
! 15128: SQLITE_PRIVATE void sqlite3MemSetDefault(void){
! 15129: static const sqlite3_mem_methods defaultMethods = {
! 15130: sqlite3MemMalloc,
! 15131: sqlite3MemFree,
! 15132: sqlite3MemRealloc,
! 15133: sqlite3MemSize,
! 15134: sqlite3MemRoundup,
! 15135: sqlite3MemInit,
! 15136: sqlite3MemShutdown,
! 15137: 0
! 15138: };
! 15139: sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
! 15140: }
! 15141:
! 15142: #endif /* SQLITE_ZERO_MALLOC */
! 15143:
! 15144: /************** End of mem0.c ************************************************/
! 15145: /************** Begin file mem1.c ********************************************/
! 15146: /*
! 15147: ** 2007 August 14
! 15148: **
! 15149: ** The author disclaims copyright to this source code. In place of
! 15150: ** a legal notice, here is a blessing:
! 15151: **
! 15152: ** May you do good and not evil.
! 15153: ** May you find forgiveness for yourself and forgive others.
! 15154: ** May you share freely, never taking more than you give.
! 15155: **
! 15156: *************************************************************************
! 15157: **
! 15158: ** This file contains low-level memory allocation drivers for when
! 15159: ** SQLite will use the standard C-library malloc/realloc/free interface
! 15160: ** to obtain the memory it needs.
! 15161: **
! 15162: ** This file contains implementations of the low-level memory allocation
! 15163: ** routines specified in the sqlite3_mem_methods object.
! 15164: */
! 15165:
! 15166: /*
! 15167: ** This version of the memory allocator is the default. It is
! 15168: ** used when no other memory allocator is specified using compile-time
! 15169: ** macros.
! 15170: */
! 15171: #ifdef SQLITE_SYSTEM_MALLOC
! 15172:
! 15173: /*
! 15174: ** Windows systems have malloc_usable_size() but it is called _msize()
! 15175: */
! 15176: #if !defined(HAVE_MALLOC_USABLE_SIZE) && SQLITE_OS_WIN
! 15177: # define HAVE_MALLOC_USABLE_SIZE 1
! 15178: # define malloc_usable_size _msize
! 15179: #endif
! 15180:
! 15181: #if defined(__APPLE__)
! 15182:
! 15183: /*
! 15184: ** Use the zone allocator available on apple products
! 15185: */
! 15186: #include <sys/sysctl.h>
! 15187: #include <malloc/malloc.h>
! 15188: #include <libkern/OSAtomic.h>
! 15189: static malloc_zone_t* _sqliteZone_;
! 15190: #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
! 15191: #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
! 15192: #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
! 15193: #define SQLITE_MALLOCSIZE(x) \
! 15194: (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
! 15195:
! 15196: #else /* if not __APPLE__ */
! 15197:
! 15198: /*
! 15199: ** Use standard C library malloc and free on non-Apple systems.
! 15200: */
! 15201: #define SQLITE_MALLOC(x) malloc(x)
! 15202: #define SQLITE_FREE(x) free(x)
! 15203: #define SQLITE_REALLOC(x,y) realloc((x),(y))
! 15204:
! 15205: #ifdef HAVE_MALLOC_USABLE_SIZE
! 15206: #include <malloc.h>
! 15207: #define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
! 15208: #else
! 15209: #undef SQLITE_MALLOCSIZE
! 15210: #endif
! 15211:
! 15212: #endif /* __APPLE__ or not __APPLE__ */
! 15213:
! 15214: /*
! 15215: ** Like malloc(), but remember the size of the allocation
! 15216: ** so that we can find it later using sqlite3MemSize().
! 15217: **
! 15218: ** For this low-level routine, we are guaranteed that nByte>0 because
! 15219: ** cases of nByte<=0 will be intercepted and dealt with by higher level
! 15220: ** routines.
! 15221: */
! 15222: static void *sqlite3MemMalloc(int nByte){
! 15223: #ifdef SQLITE_MALLOCSIZE
! 15224: void *p = SQLITE_MALLOC( nByte );
! 15225: if( p==0 ){
! 15226: testcase( sqlite3GlobalConfig.xLog!=0 );
! 15227: sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
! 15228: }
! 15229: return p;
! 15230: #else
! 15231: sqlite3_int64 *p;
! 15232: assert( nByte>0 );
! 15233: nByte = ROUND8(nByte);
! 15234: p = SQLITE_MALLOC( nByte+8 );
! 15235: if( p ){
! 15236: p[0] = nByte;
! 15237: p++;
! 15238: }else{
! 15239: testcase( sqlite3GlobalConfig.xLog!=0 );
! 15240: sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
! 15241: }
! 15242: return (void *)p;
! 15243: #endif
! 15244: }
! 15245:
! 15246: /*
! 15247: ** Like free() but works for allocations obtained from sqlite3MemMalloc()
! 15248: ** or sqlite3MemRealloc().
! 15249: **
! 15250: ** For this low-level routine, we already know that pPrior!=0 since
! 15251: ** cases where pPrior==0 will have been intecepted and dealt with
! 15252: ** by higher-level routines.
! 15253: */
! 15254: static void sqlite3MemFree(void *pPrior){
! 15255: #ifdef SQLITE_MALLOCSIZE
! 15256: SQLITE_FREE(pPrior);
! 15257: #else
! 15258: sqlite3_int64 *p = (sqlite3_int64*)pPrior;
! 15259: assert( pPrior!=0 );
! 15260: p--;
! 15261: SQLITE_FREE(p);
! 15262: #endif
! 15263: }
! 15264:
! 15265: /*
! 15266: ** Report the allocated size of a prior return from xMalloc()
! 15267: ** or xRealloc().
! 15268: */
! 15269: static int sqlite3MemSize(void *pPrior){
! 15270: #ifdef SQLITE_MALLOCSIZE
! 15271: return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
! 15272: #else
! 15273: sqlite3_int64 *p;
! 15274: if( pPrior==0 ) return 0;
! 15275: p = (sqlite3_int64*)pPrior;
! 15276: p--;
! 15277: return (int)p[0];
! 15278: #endif
! 15279: }
! 15280:
! 15281: /*
! 15282: ** Like realloc(). Resize an allocation previously obtained from
! 15283: ** sqlite3MemMalloc().
! 15284: **
! 15285: ** For this low-level interface, we know that pPrior!=0. Cases where
! 15286: ** pPrior==0 while have been intercepted by higher-level routine and
! 15287: ** redirected to xMalloc. Similarly, we know that nByte>0 becauses
! 15288: ** cases where nByte<=0 will have been intercepted by higher-level
! 15289: ** routines and redirected to xFree.
! 15290: */
! 15291: static void *sqlite3MemRealloc(void *pPrior, int nByte){
! 15292: #ifdef SQLITE_MALLOCSIZE
! 15293: void *p = SQLITE_REALLOC(pPrior, nByte);
! 15294: if( p==0 ){
! 15295: testcase( sqlite3GlobalConfig.xLog!=0 );
! 15296: sqlite3_log(SQLITE_NOMEM,
! 15297: "failed memory resize %u to %u bytes",
! 15298: SQLITE_MALLOCSIZE(pPrior), nByte);
! 15299: }
! 15300: return p;
! 15301: #else
! 15302: sqlite3_int64 *p = (sqlite3_int64*)pPrior;
! 15303: assert( pPrior!=0 && nByte>0 );
! 15304: assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
! 15305: p--;
! 15306: p = SQLITE_REALLOC(p, nByte+8 );
! 15307: if( p ){
! 15308: p[0] = nByte;
! 15309: p++;
! 15310: }else{
! 15311: testcase( sqlite3GlobalConfig.xLog!=0 );
! 15312: sqlite3_log(SQLITE_NOMEM,
! 15313: "failed memory resize %u to %u bytes",
! 15314: sqlite3MemSize(pPrior), nByte);
! 15315: }
! 15316: return (void*)p;
! 15317: #endif
! 15318: }
! 15319:
! 15320: /*
! 15321: ** Round up a request size to the next valid allocation size.
! 15322: */
! 15323: static int sqlite3MemRoundup(int n){
! 15324: return ROUND8(n);
! 15325: }
! 15326:
! 15327: /*
! 15328: ** Initialize this module.
! 15329: */
! 15330: static int sqlite3MemInit(void *NotUsed){
! 15331: #if defined(__APPLE__)
! 15332: int cpuCount;
! 15333: size_t len;
! 15334: if( _sqliteZone_ ){
! 15335: return SQLITE_OK;
! 15336: }
! 15337: len = sizeof(cpuCount);
! 15338: /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
! 15339: sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
! 15340: if( cpuCount>1 ){
! 15341: /* defer MT decisions to system malloc */
! 15342: _sqliteZone_ = malloc_default_zone();
! 15343: }else{
! 15344: /* only 1 core, use our own zone to contention over global locks,
! 15345: ** e.g. we have our own dedicated locks */
! 15346: bool success;
! 15347: malloc_zone_t* newzone = malloc_create_zone(4096, 0);
! 15348: malloc_set_zone_name(newzone, "Sqlite_Heap");
! 15349: do{
! 15350: success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone,
! 15351: (void * volatile *)&_sqliteZone_);
! 15352: }while(!_sqliteZone_);
! 15353: if( !success ){
! 15354: /* somebody registered a zone first */
! 15355: malloc_destroy_zone(newzone);
! 15356: }
! 15357: }
! 15358: #endif
! 15359: UNUSED_PARAMETER(NotUsed);
! 15360: return SQLITE_OK;
! 15361: }
! 15362:
! 15363: /*
! 15364: ** Deinitialize this module.
! 15365: */
! 15366: static void sqlite3MemShutdown(void *NotUsed){
! 15367: UNUSED_PARAMETER(NotUsed);
! 15368: return;
! 15369: }
! 15370:
! 15371: /*
! 15372: ** This routine is the only routine in this file with external linkage.
! 15373: **
! 15374: ** Populate the low-level memory allocation function pointers in
! 15375: ** sqlite3GlobalConfig.m with pointers to the routines in this file.
! 15376: */
! 15377: SQLITE_PRIVATE void sqlite3MemSetDefault(void){
! 15378: static const sqlite3_mem_methods defaultMethods = {
! 15379: sqlite3MemMalloc,
! 15380: sqlite3MemFree,
! 15381: sqlite3MemRealloc,
! 15382: sqlite3MemSize,
! 15383: sqlite3MemRoundup,
! 15384: sqlite3MemInit,
! 15385: sqlite3MemShutdown,
! 15386: 0
! 15387: };
! 15388: sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
! 15389: }
! 15390:
! 15391: #endif /* SQLITE_SYSTEM_MALLOC */
! 15392:
! 15393: /************** End of mem1.c ************************************************/
! 15394: /************** Begin file mem2.c ********************************************/
! 15395: /*
! 15396: ** 2007 August 15
! 15397: **
! 15398: ** The author disclaims copyright to this source code. In place of
! 15399: ** a legal notice, here is a blessing:
! 15400: **
! 15401: ** May you do good and not evil.
! 15402: ** May you find forgiveness for yourself and forgive others.
! 15403: ** May you share freely, never taking more than you give.
! 15404: **
! 15405: *************************************************************************
! 15406: **
! 15407: ** This file contains low-level memory allocation drivers for when
! 15408: ** SQLite will use the standard C-library malloc/realloc/free interface
! 15409: ** to obtain the memory it needs while adding lots of additional debugging
! 15410: ** information to each allocation in order to help detect and fix memory
! 15411: ** leaks and memory usage errors.
! 15412: **
! 15413: ** This file contains implementations of the low-level memory allocation
! 15414: ** routines specified in the sqlite3_mem_methods object.
! 15415: */
! 15416:
! 15417: /*
! 15418: ** This version of the memory allocator is used only if the
! 15419: ** SQLITE_MEMDEBUG macro is defined
! 15420: */
! 15421: #ifdef SQLITE_MEMDEBUG
! 15422:
! 15423: /*
! 15424: ** The backtrace functionality is only available with GLIBC
! 15425: */
! 15426: #ifdef __GLIBC__
! 15427: extern int backtrace(void**,int);
! 15428: extern void backtrace_symbols_fd(void*const*,int,int);
! 15429: #else
! 15430: # define backtrace(A,B) 1
! 15431: # define backtrace_symbols_fd(A,B,C)
! 15432: #endif
! 15433: /* #include <stdio.h> */
! 15434:
! 15435: /*
! 15436: ** Each memory allocation looks like this:
! 15437: **
! 15438: ** ------------------------------------------------------------------------
! 15439: ** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
! 15440: ** ------------------------------------------------------------------------
! 15441: **
! 15442: ** The application code sees only a pointer to the allocation. We have
! 15443: ** to back up from the allocation pointer to find the MemBlockHdr. The
! 15444: ** MemBlockHdr tells us the size of the allocation and the number of
! 15445: ** backtrace pointers. There is also a guard word at the end of the
! 15446: ** MemBlockHdr.
! 15447: */
! 15448: struct MemBlockHdr {
! 15449: i64 iSize; /* Size of this allocation */
! 15450: struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
! 15451: char nBacktrace; /* Number of backtraces on this alloc */
! 15452: char nBacktraceSlots; /* Available backtrace slots */
! 15453: u8 nTitle; /* Bytes of title; includes '\0' */
! 15454: u8 eType; /* Allocation type code */
! 15455: int iForeGuard; /* Guard word for sanity */
! 15456: };
! 15457:
! 15458: /*
! 15459: ** Guard words
! 15460: */
! 15461: #define FOREGUARD 0x80F5E153
! 15462: #define REARGUARD 0xE4676B53
! 15463:
! 15464: /*
! 15465: ** Number of malloc size increments to track.
! 15466: */
! 15467: #define NCSIZE 1000
! 15468:
! 15469: /*
! 15470: ** All of the static variables used by this module are collected
! 15471: ** into a single structure named "mem". This is to keep the
! 15472: ** static variables organized and to reduce namespace pollution
! 15473: ** when this module is combined with other in the amalgamation.
! 15474: */
! 15475: static struct {
! 15476:
! 15477: /*
! 15478: ** Mutex to control access to the memory allocation subsystem.
! 15479: */
! 15480: sqlite3_mutex *mutex;
! 15481:
! 15482: /*
! 15483: ** Head and tail of a linked list of all outstanding allocations
! 15484: */
! 15485: struct MemBlockHdr *pFirst;
! 15486: struct MemBlockHdr *pLast;
! 15487:
! 15488: /*
! 15489: ** The number of levels of backtrace to save in new allocations.
! 15490: */
! 15491: int nBacktrace;
! 15492: void (*xBacktrace)(int, int, void **);
! 15493:
! 15494: /*
! 15495: ** Title text to insert in front of each block
! 15496: */
! 15497: int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
! 15498: char zTitle[100]; /* The title text */
! 15499:
! 15500: /*
! 15501: ** sqlite3MallocDisallow() increments the following counter.
! 15502: ** sqlite3MallocAllow() decrements it.
! 15503: */
! 15504: int disallow; /* Do not allow memory allocation */
! 15505:
! 15506: /*
! 15507: ** Gather statistics on the sizes of memory allocations.
! 15508: ** nAlloc[i] is the number of allocation attempts of i*8
! 15509: ** bytes. i==NCSIZE is the number of allocation attempts for
! 15510: ** sizes more than NCSIZE*8 bytes.
! 15511: */
! 15512: int nAlloc[NCSIZE]; /* Total number of allocations */
! 15513: int nCurrent[NCSIZE]; /* Current number of allocations */
! 15514: int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
! 15515:
! 15516: } mem;
! 15517:
! 15518:
! 15519: /*
! 15520: ** Adjust memory usage statistics
! 15521: */
! 15522: static void adjustStats(int iSize, int increment){
! 15523: int i = ROUND8(iSize)/8;
! 15524: if( i>NCSIZE-1 ){
! 15525: i = NCSIZE - 1;
! 15526: }
! 15527: if( increment>0 ){
! 15528: mem.nAlloc[i]++;
! 15529: mem.nCurrent[i]++;
! 15530: if( mem.nCurrent[i]>mem.mxCurrent[i] ){
! 15531: mem.mxCurrent[i] = mem.nCurrent[i];
! 15532: }
! 15533: }else{
! 15534: mem.nCurrent[i]--;
! 15535: assert( mem.nCurrent[i]>=0 );
! 15536: }
! 15537: }
! 15538:
! 15539: /*
! 15540: ** Given an allocation, find the MemBlockHdr for that allocation.
! 15541: **
! 15542: ** This routine checks the guards at either end of the allocation and
! 15543: ** if they are incorrect it asserts.
! 15544: */
! 15545: static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
! 15546: struct MemBlockHdr *p;
! 15547: int *pInt;
! 15548: u8 *pU8;
! 15549: int nReserve;
! 15550:
! 15551: p = (struct MemBlockHdr*)pAllocation;
! 15552: p--;
! 15553: assert( p->iForeGuard==(int)FOREGUARD );
! 15554: nReserve = ROUND8(p->iSize);
! 15555: pInt = (int*)pAllocation;
! 15556: pU8 = (u8*)pAllocation;
! 15557: assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
! 15558: /* This checks any of the "extra" bytes allocated due
! 15559: ** to rounding up to an 8 byte boundary to ensure
! 15560: ** they haven't been overwritten.
! 15561: */
! 15562: while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
! 15563: return p;
! 15564: }
! 15565:
! 15566: /*
! 15567: ** Return the number of bytes currently allocated at address p.
! 15568: */
! 15569: static int sqlite3MemSize(void *p){
! 15570: struct MemBlockHdr *pHdr;
! 15571: if( !p ){
! 15572: return 0;
! 15573: }
! 15574: pHdr = sqlite3MemsysGetHeader(p);
! 15575: return pHdr->iSize;
! 15576: }
! 15577:
! 15578: /*
! 15579: ** Initialize the memory allocation subsystem.
! 15580: */
! 15581: static int sqlite3MemInit(void *NotUsed){
! 15582: UNUSED_PARAMETER(NotUsed);
! 15583: assert( (sizeof(struct MemBlockHdr)&7) == 0 );
! 15584: if( !sqlite3GlobalConfig.bMemstat ){
! 15585: /* If memory status is enabled, then the malloc.c wrapper will already
! 15586: ** hold the STATIC_MEM mutex when the routines here are invoked. */
! 15587: mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
! 15588: }
! 15589: return SQLITE_OK;
! 15590: }
! 15591:
! 15592: /*
! 15593: ** Deinitialize the memory allocation subsystem.
! 15594: */
! 15595: static void sqlite3MemShutdown(void *NotUsed){
! 15596: UNUSED_PARAMETER(NotUsed);
! 15597: mem.mutex = 0;
! 15598: }
! 15599:
! 15600: /*
! 15601: ** Round up a request size to the next valid allocation size.
! 15602: */
! 15603: static int sqlite3MemRoundup(int n){
! 15604: return ROUND8(n);
! 15605: }
! 15606:
! 15607: /*
! 15608: ** Fill a buffer with pseudo-random bytes. This is used to preset
! 15609: ** the content of a new memory allocation to unpredictable values and
! 15610: ** to clear the content of a freed allocation to unpredictable values.
! 15611: */
! 15612: static void randomFill(char *pBuf, int nByte){
! 15613: unsigned int x, y, r;
! 15614: x = SQLITE_PTR_TO_INT(pBuf);
! 15615: y = nByte | 1;
! 15616: while( nByte >= 4 ){
! 15617: x = (x>>1) ^ (-(x&1) & 0xd0000001);
! 15618: y = y*1103515245 + 12345;
! 15619: r = x ^ y;
! 15620: *(int*)pBuf = r;
! 15621: pBuf += 4;
! 15622: nByte -= 4;
! 15623: }
! 15624: while( nByte-- > 0 ){
! 15625: x = (x>>1) ^ (-(x&1) & 0xd0000001);
! 15626: y = y*1103515245 + 12345;
! 15627: r = x ^ y;
! 15628: *(pBuf++) = r & 0xff;
! 15629: }
! 15630: }
! 15631:
! 15632: /*
! 15633: ** Allocate nByte bytes of memory.
! 15634: */
! 15635: static void *sqlite3MemMalloc(int nByte){
! 15636: struct MemBlockHdr *pHdr;
! 15637: void **pBt;
! 15638: char *z;
! 15639: int *pInt;
! 15640: void *p = 0;
! 15641: int totalSize;
! 15642: int nReserve;
! 15643: sqlite3_mutex_enter(mem.mutex);
! 15644: assert( mem.disallow==0 );
! 15645: nReserve = ROUND8(nByte);
! 15646: totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
! 15647: mem.nBacktrace*sizeof(void*) + mem.nTitle;
! 15648: p = malloc(totalSize);
! 15649: if( p ){
! 15650: z = p;
! 15651: pBt = (void**)&z[mem.nTitle];
! 15652: pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
! 15653: pHdr->pNext = 0;
! 15654: pHdr->pPrev = mem.pLast;
! 15655: if( mem.pLast ){
! 15656: mem.pLast->pNext = pHdr;
! 15657: }else{
! 15658: mem.pFirst = pHdr;
! 15659: }
! 15660: mem.pLast = pHdr;
! 15661: pHdr->iForeGuard = FOREGUARD;
! 15662: pHdr->eType = MEMTYPE_HEAP;
! 15663: pHdr->nBacktraceSlots = mem.nBacktrace;
! 15664: pHdr->nTitle = mem.nTitle;
! 15665: if( mem.nBacktrace ){
! 15666: void *aAddr[40];
! 15667: pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
! 15668: memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
! 15669: assert(pBt[0]);
! 15670: if( mem.xBacktrace ){
! 15671: mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
! 15672: }
! 15673: }else{
! 15674: pHdr->nBacktrace = 0;
! 15675: }
! 15676: if( mem.nTitle ){
! 15677: memcpy(z, mem.zTitle, mem.nTitle);
! 15678: }
! 15679: pHdr->iSize = nByte;
! 15680: adjustStats(nByte, +1);
! 15681: pInt = (int*)&pHdr[1];
! 15682: pInt[nReserve/sizeof(int)] = REARGUARD;
! 15683: randomFill((char*)pInt, nByte);
! 15684: memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
! 15685: p = (void*)pInt;
! 15686: }
! 15687: sqlite3_mutex_leave(mem.mutex);
! 15688: return p;
! 15689: }
! 15690:
! 15691: /*
! 15692: ** Free memory.
! 15693: */
! 15694: static void sqlite3MemFree(void *pPrior){
! 15695: struct MemBlockHdr *pHdr;
! 15696: void **pBt;
! 15697: char *z;
! 15698: assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
! 15699: || mem.mutex!=0 );
! 15700: pHdr = sqlite3MemsysGetHeader(pPrior);
! 15701: pBt = (void**)pHdr;
! 15702: pBt -= pHdr->nBacktraceSlots;
! 15703: sqlite3_mutex_enter(mem.mutex);
! 15704: if( pHdr->pPrev ){
! 15705: assert( pHdr->pPrev->pNext==pHdr );
! 15706: pHdr->pPrev->pNext = pHdr->pNext;
! 15707: }else{
! 15708: assert( mem.pFirst==pHdr );
! 15709: mem.pFirst = pHdr->pNext;
! 15710: }
! 15711: if( pHdr->pNext ){
! 15712: assert( pHdr->pNext->pPrev==pHdr );
! 15713: pHdr->pNext->pPrev = pHdr->pPrev;
! 15714: }else{
! 15715: assert( mem.pLast==pHdr );
! 15716: mem.pLast = pHdr->pPrev;
! 15717: }
! 15718: z = (char*)pBt;
! 15719: z -= pHdr->nTitle;
! 15720: adjustStats(pHdr->iSize, -1);
! 15721: randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
! 15722: pHdr->iSize + sizeof(int) + pHdr->nTitle);
! 15723: free(z);
! 15724: sqlite3_mutex_leave(mem.mutex);
! 15725: }
! 15726:
! 15727: /*
! 15728: ** Change the size of an existing memory allocation.
! 15729: **
! 15730: ** For this debugging implementation, we *always* make a copy of the
! 15731: ** allocation into a new place in memory. In this way, if the
! 15732: ** higher level code is using pointer to the old allocation, it is
! 15733: ** much more likely to break and we are much more liking to find
! 15734: ** the error.
! 15735: */
! 15736: static void *sqlite3MemRealloc(void *pPrior, int nByte){
! 15737: struct MemBlockHdr *pOldHdr;
! 15738: void *pNew;
! 15739: assert( mem.disallow==0 );
! 15740: assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
! 15741: pOldHdr = sqlite3MemsysGetHeader(pPrior);
! 15742: pNew = sqlite3MemMalloc(nByte);
! 15743: if( pNew ){
! 15744: memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
! 15745: if( nByte>pOldHdr->iSize ){
! 15746: randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
! 15747: }
! 15748: sqlite3MemFree(pPrior);
! 15749: }
! 15750: return pNew;
! 15751: }
! 15752:
! 15753: /*
! 15754: ** Populate the low-level memory allocation function pointers in
! 15755: ** sqlite3GlobalConfig.m with pointers to the routines in this file.
! 15756: */
! 15757: SQLITE_PRIVATE void sqlite3MemSetDefault(void){
! 15758: static const sqlite3_mem_methods defaultMethods = {
! 15759: sqlite3MemMalloc,
! 15760: sqlite3MemFree,
! 15761: sqlite3MemRealloc,
! 15762: sqlite3MemSize,
! 15763: sqlite3MemRoundup,
! 15764: sqlite3MemInit,
! 15765: sqlite3MemShutdown,
! 15766: 0
! 15767: };
! 15768: sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
! 15769: }
! 15770:
! 15771: /*
! 15772: ** Set the "type" of an allocation.
! 15773: */
! 15774: SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
! 15775: if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
! 15776: struct MemBlockHdr *pHdr;
! 15777: pHdr = sqlite3MemsysGetHeader(p);
! 15778: assert( pHdr->iForeGuard==FOREGUARD );
! 15779: pHdr->eType = eType;
! 15780: }
! 15781: }
! 15782:
! 15783: /*
! 15784: ** Return TRUE if the mask of type in eType matches the type of the
! 15785: ** allocation p. Also return true if p==NULL.
! 15786: **
! 15787: ** This routine is designed for use within an assert() statement, to
! 15788: ** verify the type of an allocation. For example:
! 15789: **
! 15790: ** assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
! 15791: */
! 15792: SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
! 15793: int rc = 1;
! 15794: if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
! 15795: struct MemBlockHdr *pHdr;
! 15796: pHdr = sqlite3MemsysGetHeader(p);
! 15797: assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
! 15798: if( (pHdr->eType&eType)==0 ){
! 15799: rc = 0;
! 15800: }
! 15801: }
! 15802: return rc;
! 15803: }
! 15804:
! 15805: /*
! 15806: ** Return TRUE if the mask of type in eType matches no bits of the type of the
! 15807: ** allocation p. Also return true if p==NULL.
! 15808: **
! 15809: ** This routine is designed for use within an assert() statement, to
! 15810: ** verify the type of an allocation. For example:
! 15811: **
! 15812: ** assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
! 15813: */
! 15814: SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
! 15815: int rc = 1;
! 15816: if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
! 15817: struct MemBlockHdr *pHdr;
! 15818: pHdr = sqlite3MemsysGetHeader(p);
! 15819: assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
! 15820: if( (pHdr->eType&eType)!=0 ){
! 15821: rc = 0;
! 15822: }
! 15823: }
! 15824: return rc;
! 15825: }
! 15826:
! 15827: /*
! 15828: ** Set the number of backtrace levels kept for each allocation.
! 15829: ** A value of zero turns off backtracing. The number is always rounded
! 15830: ** up to a multiple of 2.
! 15831: */
! 15832: SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
! 15833: if( depth<0 ){ depth = 0; }
! 15834: if( depth>20 ){ depth = 20; }
! 15835: depth = (depth+1)&0xfe;
! 15836: mem.nBacktrace = depth;
! 15837: }
! 15838:
! 15839: SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
! 15840: mem.xBacktrace = xBacktrace;
! 15841: }
! 15842:
! 15843: /*
! 15844: ** Set the title string for subsequent allocations.
! 15845: */
! 15846: SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
! 15847: unsigned int n = sqlite3Strlen30(zTitle) + 1;
! 15848: sqlite3_mutex_enter(mem.mutex);
! 15849: if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
! 15850: memcpy(mem.zTitle, zTitle, n);
! 15851: mem.zTitle[n] = 0;
! 15852: mem.nTitle = ROUND8(n);
! 15853: sqlite3_mutex_leave(mem.mutex);
! 15854: }
! 15855:
! 15856: SQLITE_PRIVATE void sqlite3MemdebugSync(){
! 15857: struct MemBlockHdr *pHdr;
! 15858: for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
! 15859: void **pBt = (void**)pHdr;
! 15860: pBt -= pHdr->nBacktraceSlots;
! 15861: mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
! 15862: }
! 15863: }
! 15864:
! 15865: /*
! 15866: ** Open the file indicated and write a log of all unfreed memory
! 15867: ** allocations into that log.
! 15868: */
! 15869: SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
! 15870: FILE *out;
! 15871: struct MemBlockHdr *pHdr;
! 15872: void **pBt;
! 15873: int i;
! 15874: out = fopen(zFilename, "w");
! 15875: if( out==0 ){
! 15876: fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
! 15877: zFilename);
! 15878: return;
! 15879: }
! 15880: for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
! 15881: char *z = (char*)pHdr;
! 15882: z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
! 15883: fprintf(out, "**** %lld bytes at %p from %s ****\n",
! 15884: pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
! 15885: if( pHdr->nBacktrace ){
! 15886: fflush(out);
! 15887: pBt = (void**)pHdr;
! 15888: pBt -= pHdr->nBacktraceSlots;
! 15889: backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
! 15890: fprintf(out, "\n");
! 15891: }
! 15892: }
! 15893: fprintf(out, "COUNTS:\n");
! 15894: for(i=0; i<NCSIZE-1; i++){
! 15895: if( mem.nAlloc[i] ){
! 15896: fprintf(out, " %5d: %10d %10d %10d\n",
! 15897: i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
! 15898: }
! 15899: }
! 15900: if( mem.nAlloc[NCSIZE-1] ){
! 15901: fprintf(out, " %5d: %10d %10d %10d\n",
! 15902: NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
! 15903: mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
! 15904: }
! 15905: fclose(out);
! 15906: }
! 15907:
! 15908: /*
! 15909: ** Return the number of times sqlite3MemMalloc() has been called.
! 15910: */
! 15911: SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
! 15912: int i;
! 15913: int nTotal = 0;
! 15914: for(i=0; i<NCSIZE; i++){
! 15915: nTotal += mem.nAlloc[i];
! 15916: }
! 15917: return nTotal;
! 15918: }
! 15919:
! 15920:
! 15921: #endif /* SQLITE_MEMDEBUG */
! 15922:
! 15923: /************** End of mem2.c ************************************************/
! 15924: /************** Begin file mem3.c ********************************************/
! 15925: /*
! 15926: ** 2007 October 14
! 15927: **
! 15928: ** The author disclaims copyright to this source code. In place of
! 15929: ** a legal notice, here is a blessing:
! 15930: **
! 15931: ** May you do good and not evil.
! 15932: ** May you find forgiveness for yourself and forgive others.
! 15933: ** May you share freely, never taking more than you give.
! 15934: **
! 15935: *************************************************************************
! 15936: ** This file contains the C functions that implement a memory
! 15937: ** allocation subsystem for use by SQLite.
! 15938: **
! 15939: ** This version of the memory allocation subsystem omits all
! 15940: ** use of malloc(). The SQLite user supplies a block of memory
! 15941: ** before calling sqlite3_initialize() from which allocations
! 15942: ** are made and returned by the xMalloc() and xRealloc()
! 15943: ** implementations. Once sqlite3_initialize() has been called,
! 15944: ** the amount of memory available to SQLite is fixed and cannot
! 15945: ** be changed.
! 15946: **
! 15947: ** This version of the memory allocation subsystem is included
! 15948: ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
! 15949: */
! 15950:
! 15951: /*
! 15952: ** This version of the memory allocator is only built into the library
! 15953: ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
! 15954: ** mean that the library will use a memory-pool by default, just that
! 15955: ** it is available. The mempool allocator is activated by calling
! 15956: ** sqlite3_config().
! 15957: */
! 15958: #ifdef SQLITE_ENABLE_MEMSYS3
! 15959:
! 15960: /*
! 15961: ** Maximum size (in Mem3Blocks) of a "small" chunk.
! 15962: */
! 15963: #define MX_SMALL 10
! 15964:
! 15965:
! 15966: /*
! 15967: ** Number of freelist hash slots
! 15968: */
! 15969: #define N_HASH 61
! 15970:
! 15971: /*
! 15972: ** A memory allocation (also called a "chunk") consists of two or
! 15973: ** more blocks where each block is 8 bytes. The first 8 bytes are
! 15974: ** a header that is not returned to the user.
! 15975: **
! 15976: ** A chunk is two or more blocks that is either checked out or
! 15977: ** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
! 15978: ** size of the allocation in blocks if the allocation is free.
! 15979: ** The u.hdr.size4x&1 bit is true if the chunk is checked out and
! 15980: ** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
! 15981: ** is true if the previous chunk is checked out and false if the
! 15982: ** previous chunk is free. The u.hdr.prevSize field is the size of
! 15983: ** the previous chunk in blocks if the previous chunk is on the
! 15984: ** freelist. If the previous chunk is checked out, then
! 15985: ** u.hdr.prevSize can be part of the data for that chunk and should
! 15986: ** not be read or written.
! 15987: **
! 15988: ** We often identify a chunk by its index in mem3.aPool[]. When
! 15989: ** this is done, the chunk index refers to the second block of
! 15990: ** the chunk. In this way, the first chunk has an index of 1.
! 15991: ** A chunk index of 0 means "no such chunk" and is the equivalent
! 15992: ** of a NULL pointer.
! 15993: **
! 15994: ** The second block of free chunks is of the form u.list. The
! 15995: ** two fields form a double-linked list of chunks of related sizes.
! 15996: ** Pointers to the head of the list are stored in mem3.aiSmall[]
! 15997: ** for smaller chunks and mem3.aiHash[] for larger chunks.
! 15998: **
! 15999: ** The second block of a chunk is user data if the chunk is checked
! 16000: ** out. If a chunk is checked out, the user data may extend into
! 16001: ** the u.hdr.prevSize value of the following chunk.
! 16002: */
! 16003: typedef struct Mem3Block Mem3Block;
! 16004: struct Mem3Block {
! 16005: union {
! 16006: struct {
! 16007: u32 prevSize; /* Size of previous chunk in Mem3Block elements */
! 16008: u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
! 16009: } hdr;
! 16010: struct {
! 16011: u32 next; /* Index in mem3.aPool[] of next free chunk */
! 16012: u32 prev; /* Index in mem3.aPool[] of previous free chunk */
! 16013: } list;
! 16014: } u;
! 16015: };
! 16016:
! 16017: /*
! 16018: ** All of the static variables used by this module are collected
! 16019: ** into a single structure named "mem3". This is to keep the
! 16020: ** static variables organized and to reduce namespace pollution
! 16021: ** when this module is combined with other in the amalgamation.
! 16022: */
! 16023: static SQLITE_WSD struct Mem3Global {
! 16024: /*
! 16025: ** Memory available for allocation. nPool is the size of the array
! 16026: ** (in Mem3Blocks) pointed to by aPool less 2.
! 16027: */
! 16028: u32 nPool;
! 16029: Mem3Block *aPool;
! 16030:
! 16031: /*
! 16032: ** True if we are evaluating an out-of-memory callback.
! 16033: */
! 16034: int alarmBusy;
! 16035:
! 16036: /*
! 16037: ** Mutex to control access to the memory allocation subsystem.
! 16038: */
! 16039: sqlite3_mutex *mutex;
! 16040:
! 16041: /*
! 16042: ** The minimum amount of free space that we have seen.
! 16043: */
! 16044: u32 mnMaster;
! 16045:
! 16046: /*
! 16047: ** iMaster is the index of the master chunk. Most new allocations
! 16048: ** occur off of this chunk. szMaster is the size (in Mem3Blocks)
! 16049: ** of the current master. iMaster is 0 if there is not master chunk.
! 16050: ** The master chunk is not in either the aiHash[] or aiSmall[].
! 16051: */
! 16052: u32 iMaster;
! 16053: u32 szMaster;
! 16054:
! 16055: /*
! 16056: ** Array of lists of free blocks according to the block size
! 16057: ** for smaller chunks, or a hash on the block size for larger
! 16058: ** chunks.
! 16059: */
! 16060: u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
! 16061: u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
! 16062: } mem3 = { 97535575 };
! 16063:
! 16064: #define mem3 GLOBAL(struct Mem3Global, mem3)
! 16065:
! 16066: /*
! 16067: ** Unlink the chunk at mem3.aPool[i] from list it is currently
! 16068: ** on. *pRoot is the list that i is a member of.
! 16069: */
! 16070: static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
! 16071: u32 next = mem3.aPool[i].u.list.next;
! 16072: u32 prev = mem3.aPool[i].u.list.prev;
! 16073: assert( sqlite3_mutex_held(mem3.mutex) );
! 16074: if( prev==0 ){
! 16075: *pRoot = next;
! 16076: }else{
! 16077: mem3.aPool[prev].u.list.next = next;
! 16078: }
! 16079: if( next ){
! 16080: mem3.aPool[next].u.list.prev = prev;
! 16081: }
! 16082: mem3.aPool[i].u.list.next = 0;
! 16083: mem3.aPool[i].u.list.prev = 0;
! 16084: }
! 16085:
! 16086: /*
! 16087: ** Unlink the chunk at index i from
! 16088: ** whatever list is currently a member of.
! 16089: */
! 16090: static void memsys3Unlink(u32 i){
! 16091: u32 size, hash;
! 16092: assert( sqlite3_mutex_held(mem3.mutex) );
! 16093: assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
! 16094: assert( i>=1 );
! 16095: size = mem3.aPool[i-1].u.hdr.size4x/4;
! 16096: assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
! 16097: assert( size>=2 );
! 16098: if( size <= MX_SMALL ){
! 16099: memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
! 16100: }else{
! 16101: hash = size % N_HASH;
! 16102: memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
! 16103: }
! 16104: }
! 16105:
! 16106: /*
! 16107: ** Link the chunk at mem3.aPool[i] so that is on the list rooted
! 16108: ** at *pRoot.
! 16109: */
! 16110: static void memsys3LinkIntoList(u32 i, u32 *pRoot){
! 16111: assert( sqlite3_mutex_held(mem3.mutex) );
! 16112: mem3.aPool[i].u.list.next = *pRoot;
! 16113: mem3.aPool[i].u.list.prev = 0;
! 16114: if( *pRoot ){
! 16115: mem3.aPool[*pRoot].u.list.prev = i;
! 16116: }
! 16117: *pRoot = i;
! 16118: }
! 16119:
! 16120: /*
! 16121: ** Link the chunk at index i into either the appropriate
! 16122: ** small chunk list, or into the large chunk hash table.
! 16123: */
! 16124: static void memsys3Link(u32 i){
! 16125: u32 size, hash;
! 16126: assert( sqlite3_mutex_held(mem3.mutex) );
! 16127: assert( i>=1 );
! 16128: assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
! 16129: size = mem3.aPool[i-1].u.hdr.size4x/4;
! 16130: assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
! 16131: assert( size>=2 );
! 16132: if( size <= MX_SMALL ){
! 16133: memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
! 16134: }else{
! 16135: hash = size % N_HASH;
! 16136: memsys3LinkIntoList(i, &mem3.aiHash[hash]);
! 16137: }
! 16138: }
! 16139:
! 16140: /*
! 16141: ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
! 16142: ** will already be held (obtained by code in malloc.c) if
! 16143: ** sqlite3GlobalConfig.bMemStat is true.
! 16144: */
! 16145: static void memsys3Enter(void){
! 16146: if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
! 16147: mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
! 16148: }
! 16149: sqlite3_mutex_enter(mem3.mutex);
! 16150: }
! 16151: static void memsys3Leave(void){
! 16152: sqlite3_mutex_leave(mem3.mutex);
! 16153: }
! 16154:
! 16155: /*
! 16156: ** Called when we are unable to satisfy an allocation of nBytes.
! 16157: */
! 16158: static void memsys3OutOfMemory(int nByte){
! 16159: if( !mem3.alarmBusy ){
! 16160: mem3.alarmBusy = 1;
! 16161: assert( sqlite3_mutex_held(mem3.mutex) );
! 16162: sqlite3_mutex_leave(mem3.mutex);
! 16163: sqlite3_release_memory(nByte);
! 16164: sqlite3_mutex_enter(mem3.mutex);
! 16165: mem3.alarmBusy = 0;
! 16166: }
! 16167: }
! 16168:
! 16169:
! 16170: /*
! 16171: ** Chunk i is a free chunk that has been unlinked. Adjust its
! 16172: ** size parameters for check-out and return a pointer to the
! 16173: ** user portion of the chunk.
! 16174: */
! 16175: static void *memsys3Checkout(u32 i, u32 nBlock){
! 16176: u32 x;
! 16177: assert( sqlite3_mutex_held(mem3.mutex) );
! 16178: assert( i>=1 );
! 16179: assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
! 16180: assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
! 16181: x = mem3.aPool[i-1].u.hdr.size4x;
! 16182: mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
! 16183: mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
! 16184: mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
! 16185: return &mem3.aPool[i];
! 16186: }
! 16187:
! 16188: /*
! 16189: ** Carve a piece off of the end of the mem3.iMaster free chunk.
! 16190: ** Return a pointer to the new allocation. Or, if the master chunk
! 16191: ** is not large enough, return 0.
! 16192: */
! 16193: static void *memsys3FromMaster(u32 nBlock){
! 16194: assert( sqlite3_mutex_held(mem3.mutex) );
! 16195: assert( mem3.szMaster>=nBlock );
! 16196: if( nBlock>=mem3.szMaster-1 ){
! 16197: /* Use the entire master */
! 16198: void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
! 16199: mem3.iMaster = 0;
! 16200: mem3.szMaster = 0;
! 16201: mem3.mnMaster = 0;
! 16202: return p;
! 16203: }else{
! 16204: /* Split the master block. Return the tail. */
! 16205: u32 newi, x;
! 16206: newi = mem3.iMaster + mem3.szMaster - nBlock;
! 16207: assert( newi > mem3.iMaster+1 );
! 16208: mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
! 16209: mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
! 16210: mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
! 16211: mem3.szMaster -= nBlock;
! 16212: mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
! 16213: x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
! 16214: mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
! 16215: if( mem3.szMaster < mem3.mnMaster ){
! 16216: mem3.mnMaster = mem3.szMaster;
! 16217: }
! 16218: return (void*)&mem3.aPool[newi];
! 16219: }
! 16220: }
! 16221:
! 16222: /*
! 16223: ** *pRoot is the head of a list of free chunks of the same size
! 16224: ** or same size hash. In other words, *pRoot is an entry in either
! 16225: ** mem3.aiSmall[] or mem3.aiHash[].
! 16226: **
! 16227: ** This routine examines all entries on the given list and tries
! 16228: ** to coalesce each entries with adjacent free chunks.
! 16229: **
! 16230: ** If it sees a chunk that is larger than mem3.iMaster, it replaces
! 16231: ** the current mem3.iMaster with the new larger chunk. In order for
! 16232: ** this mem3.iMaster replacement to work, the master chunk must be
! 16233: ** linked into the hash tables. That is not the normal state of
! 16234: ** affairs, of course. The calling routine must link the master
! 16235: ** chunk before invoking this routine, then must unlink the (possibly
! 16236: ** changed) master chunk once this routine has finished.
! 16237: */
! 16238: static void memsys3Merge(u32 *pRoot){
! 16239: u32 iNext, prev, size, i, x;
! 16240:
! 16241: assert( sqlite3_mutex_held(mem3.mutex) );
! 16242: for(i=*pRoot; i>0; i=iNext){
! 16243: iNext = mem3.aPool[i].u.list.next;
! 16244: size = mem3.aPool[i-1].u.hdr.size4x;
! 16245: assert( (size&1)==0 );
! 16246: if( (size&2)==0 ){
! 16247: memsys3UnlinkFromList(i, pRoot);
! 16248: assert( i > mem3.aPool[i-1].u.hdr.prevSize );
! 16249: prev = i - mem3.aPool[i-1].u.hdr.prevSize;
! 16250: if( prev==iNext ){
! 16251: iNext = mem3.aPool[prev].u.list.next;
! 16252: }
! 16253: memsys3Unlink(prev);
! 16254: size = i + size/4 - prev;
! 16255: x = mem3.aPool[prev-1].u.hdr.size4x & 2;
! 16256: mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
! 16257: mem3.aPool[prev+size-1].u.hdr.prevSize = size;
! 16258: memsys3Link(prev);
! 16259: i = prev;
! 16260: }else{
! 16261: size /= 4;
! 16262: }
! 16263: if( size>mem3.szMaster ){
! 16264: mem3.iMaster = i;
! 16265: mem3.szMaster = size;
! 16266: }
! 16267: }
! 16268: }
! 16269:
! 16270: /*
! 16271: ** Return a block of memory of at least nBytes in size.
! 16272: ** Return NULL if unable.
! 16273: **
! 16274: ** This function assumes that the necessary mutexes, if any, are
! 16275: ** already held by the caller. Hence "Unsafe".
! 16276: */
! 16277: static void *memsys3MallocUnsafe(int nByte){
! 16278: u32 i;
! 16279: u32 nBlock;
! 16280: u32 toFree;
! 16281:
! 16282: assert( sqlite3_mutex_held(mem3.mutex) );
! 16283: assert( sizeof(Mem3Block)==8 );
! 16284: if( nByte<=12 ){
! 16285: nBlock = 2;
! 16286: }else{
! 16287: nBlock = (nByte + 11)/8;
! 16288: }
! 16289: assert( nBlock>=2 );
! 16290:
! 16291: /* STEP 1:
! 16292: ** Look for an entry of the correct size in either the small
! 16293: ** chunk table or in the large chunk hash table. This is
! 16294: ** successful most of the time (about 9 times out of 10).
! 16295: */
! 16296: if( nBlock <= MX_SMALL ){
! 16297: i = mem3.aiSmall[nBlock-2];
! 16298: if( i>0 ){
! 16299: memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
! 16300: return memsys3Checkout(i, nBlock);
! 16301: }
! 16302: }else{
! 16303: int hash = nBlock % N_HASH;
! 16304: for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
! 16305: if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
! 16306: memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
! 16307: return memsys3Checkout(i, nBlock);
! 16308: }
! 16309: }
! 16310: }
! 16311:
! 16312: /* STEP 2:
! 16313: ** Try to satisfy the allocation by carving a piece off of the end
! 16314: ** of the master chunk. This step usually works if step 1 fails.
! 16315: */
! 16316: if( mem3.szMaster>=nBlock ){
! 16317: return memsys3FromMaster(nBlock);
! 16318: }
! 16319:
! 16320:
! 16321: /* STEP 3:
! 16322: ** Loop through the entire memory pool. Coalesce adjacent free
! 16323: ** chunks. Recompute the master chunk as the largest free chunk.
! 16324: ** Then try again to satisfy the allocation by carving a piece off
! 16325: ** of the end of the master chunk. This step happens very
! 16326: ** rarely (we hope!)
! 16327: */
! 16328: for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
! 16329: memsys3OutOfMemory(toFree);
! 16330: if( mem3.iMaster ){
! 16331: memsys3Link(mem3.iMaster);
! 16332: mem3.iMaster = 0;
! 16333: mem3.szMaster = 0;
! 16334: }
! 16335: for(i=0; i<N_HASH; i++){
! 16336: memsys3Merge(&mem3.aiHash[i]);
! 16337: }
! 16338: for(i=0; i<MX_SMALL-1; i++){
! 16339: memsys3Merge(&mem3.aiSmall[i]);
! 16340: }
! 16341: if( mem3.szMaster ){
! 16342: memsys3Unlink(mem3.iMaster);
! 16343: if( mem3.szMaster>=nBlock ){
! 16344: return memsys3FromMaster(nBlock);
! 16345: }
! 16346: }
! 16347: }
! 16348:
! 16349: /* If none of the above worked, then we fail. */
! 16350: return 0;
! 16351: }
! 16352:
! 16353: /*
! 16354: ** Free an outstanding memory allocation.
! 16355: **
! 16356: ** This function assumes that the necessary mutexes, if any, are
! 16357: ** already held by the caller. Hence "Unsafe".
! 16358: */
! 16359: static void memsys3FreeUnsafe(void *pOld){
! 16360: Mem3Block *p = (Mem3Block*)pOld;
! 16361: int i;
! 16362: u32 size, x;
! 16363: assert( sqlite3_mutex_held(mem3.mutex) );
! 16364: assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
! 16365: i = p - mem3.aPool;
! 16366: assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
! 16367: size = mem3.aPool[i-1].u.hdr.size4x/4;
! 16368: assert( i+size<=mem3.nPool+1 );
! 16369: mem3.aPool[i-1].u.hdr.size4x &= ~1;
! 16370: mem3.aPool[i+size-1].u.hdr.prevSize = size;
! 16371: mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
! 16372: memsys3Link(i);
! 16373:
! 16374: /* Try to expand the master using the newly freed chunk */
! 16375: if( mem3.iMaster ){
! 16376: while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
! 16377: size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
! 16378: mem3.iMaster -= size;
! 16379: mem3.szMaster += size;
! 16380: memsys3Unlink(mem3.iMaster);
! 16381: x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
! 16382: mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
! 16383: mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
! 16384: }
! 16385: x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
! 16386: while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
! 16387: memsys3Unlink(mem3.iMaster+mem3.szMaster);
! 16388: mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
! 16389: mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
! 16390: mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
! 16391: }
! 16392: }
! 16393: }
! 16394:
! 16395: /*
! 16396: ** Return the size of an outstanding allocation, in bytes. The
! 16397: ** size returned omits the 8-byte header overhead. This only
! 16398: ** works for chunks that are currently checked out.
! 16399: */
! 16400: static int memsys3Size(void *p){
! 16401: Mem3Block *pBlock;
! 16402: if( p==0 ) return 0;
! 16403: pBlock = (Mem3Block*)p;
! 16404: assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
! 16405: return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
! 16406: }
! 16407:
! 16408: /*
! 16409: ** Round up a request size to the next valid allocation size.
! 16410: */
! 16411: static int memsys3Roundup(int n){
! 16412: if( n<=12 ){
! 16413: return 12;
! 16414: }else{
! 16415: return ((n+11)&~7) - 4;
! 16416: }
! 16417: }
! 16418:
! 16419: /*
! 16420: ** Allocate nBytes of memory.
! 16421: */
! 16422: static void *memsys3Malloc(int nBytes){
! 16423: sqlite3_int64 *p;
! 16424: assert( nBytes>0 ); /* malloc.c filters out 0 byte requests */
! 16425: memsys3Enter();
! 16426: p = memsys3MallocUnsafe(nBytes);
! 16427: memsys3Leave();
! 16428: return (void*)p;
! 16429: }
! 16430:
! 16431: /*
! 16432: ** Free memory.
! 16433: */
! 16434: static void memsys3Free(void *pPrior){
! 16435: assert( pPrior );
! 16436: memsys3Enter();
! 16437: memsys3FreeUnsafe(pPrior);
! 16438: memsys3Leave();
! 16439: }
! 16440:
! 16441: /*
! 16442: ** Change the size of an existing memory allocation
! 16443: */
! 16444: static void *memsys3Realloc(void *pPrior, int nBytes){
! 16445: int nOld;
! 16446: void *p;
! 16447: if( pPrior==0 ){
! 16448: return sqlite3_malloc(nBytes);
! 16449: }
! 16450: if( nBytes<=0 ){
! 16451: sqlite3_free(pPrior);
! 16452: return 0;
! 16453: }
! 16454: nOld = memsys3Size(pPrior);
! 16455: if( nBytes<=nOld && nBytes>=nOld-128 ){
! 16456: return pPrior;
! 16457: }
! 16458: memsys3Enter();
! 16459: p = memsys3MallocUnsafe(nBytes);
! 16460: if( p ){
! 16461: if( nOld<nBytes ){
! 16462: memcpy(p, pPrior, nOld);
! 16463: }else{
! 16464: memcpy(p, pPrior, nBytes);
! 16465: }
! 16466: memsys3FreeUnsafe(pPrior);
! 16467: }
! 16468: memsys3Leave();
! 16469: return p;
! 16470: }
! 16471:
! 16472: /*
! 16473: ** Initialize this module.
! 16474: */
! 16475: static int memsys3Init(void *NotUsed){
! 16476: UNUSED_PARAMETER(NotUsed);
! 16477: if( !sqlite3GlobalConfig.pHeap ){
! 16478: return SQLITE_ERROR;
! 16479: }
! 16480:
! 16481: /* Store a pointer to the memory block in global structure mem3. */
! 16482: assert( sizeof(Mem3Block)==8 );
! 16483: mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
! 16484: mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
! 16485:
! 16486: /* Initialize the master block. */
! 16487: mem3.szMaster = mem3.nPool;
! 16488: mem3.mnMaster = mem3.szMaster;
! 16489: mem3.iMaster = 1;
! 16490: mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
! 16491: mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
! 16492: mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
! 16493:
! 16494: return SQLITE_OK;
! 16495: }
! 16496:
! 16497: /*
! 16498: ** Deinitialize this module.
! 16499: */
! 16500: static void memsys3Shutdown(void *NotUsed){
! 16501: UNUSED_PARAMETER(NotUsed);
! 16502: mem3.mutex = 0;
! 16503: return;
! 16504: }
! 16505:
! 16506:
! 16507:
! 16508: /*
! 16509: ** Open the file indicated and write a log of all unfreed memory
! 16510: ** allocations into that log.
! 16511: */
! 16512: SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
! 16513: #ifdef SQLITE_DEBUG
! 16514: FILE *out;
! 16515: u32 i, j;
! 16516: u32 size;
! 16517: if( zFilename==0 || zFilename[0]==0 ){
! 16518: out = stdout;
! 16519: }else{
! 16520: out = fopen(zFilename, "w");
! 16521: if( out==0 ){
! 16522: fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
! 16523: zFilename);
! 16524: return;
! 16525: }
! 16526: }
! 16527: memsys3Enter();
! 16528: fprintf(out, "CHUNKS:\n");
! 16529: for(i=1; i<=mem3.nPool; i+=size/4){
! 16530: size = mem3.aPool[i-1].u.hdr.size4x;
! 16531: if( size/4<=1 ){
! 16532: fprintf(out, "%p size error\n", &mem3.aPool[i]);
! 16533: assert( 0 );
! 16534: break;
! 16535: }
! 16536: if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
! 16537: fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
! 16538: assert( 0 );
! 16539: break;
! 16540: }
! 16541: if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
! 16542: fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
! 16543: assert( 0 );
! 16544: break;
! 16545: }
! 16546: if( size&1 ){
! 16547: fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
! 16548: }else{
! 16549: fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
! 16550: i==mem3.iMaster ? " **master**" : "");
! 16551: }
! 16552: }
! 16553: for(i=0; i<MX_SMALL-1; i++){
! 16554: if( mem3.aiSmall[i]==0 ) continue;
! 16555: fprintf(out, "small(%2d):", i);
! 16556: for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
! 16557: fprintf(out, " %p(%d)", &mem3.aPool[j],
! 16558: (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
! 16559: }
! 16560: fprintf(out, "\n");
! 16561: }
! 16562: for(i=0; i<N_HASH; i++){
! 16563: if( mem3.aiHash[i]==0 ) continue;
! 16564: fprintf(out, "hash(%2d):", i);
! 16565: for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
! 16566: fprintf(out, " %p(%d)", &mem3.aPool[j],
! 16567: (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
! 16568: }
! 16569: fprintf(out, "\n");
! 16570: }
! 16571: fprintf(out, "master=%d\n", mem3.iMaster);
! 16572: fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
! 16573: fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
! 16574: sqlite3_mutex_leave(mem3.mutex);
! 16575: if( out==stdout ){
! 16576: fflush(stdout);
! 16577: }else{
! 16578: fclose(out);
! 16579: }
! 16580: #else
! 16581: UNUSED_PARAMETER(zFilename);
! 16582: #endif
! 16583: }
! 16584:
! 16585: /*
! 16586: ** This routine is the only routine in this file with external
! 16587: ** linkage.
! 16588: **
! 16589: ** Populate the low-level memory allocation function pointers in
! 16590: ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
! 16591: ** arguments specify the block of memory to manage.
! 16592: **
! 16593: ** This routine is only called by sqlite3_config(), and therefore
! 16594: ** is not required to be threadsafe (it is not).
! 16595: */
! 16596: SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
! 16597: static const sqlite3_mem_methods mempoolMethods = {
! 16598: memsys3Malloc,
! 16599: memsys3Free,
! 16600: memsys3Realloc,
! 16601: memsys3Size,
! 16602: memsys3Roundup,
! 16603: memsys3Init,
! 16604: memsys3Shutdown,
! 16605: 0
! 16606: };
! 16607: return &mempoolMethods;
! 16608: }
! 16609:
! 16610: #endif /* SQLITE_ENABLE_MEMSYS3 */
! 16611:
! 16612: /************** End of mem3.c ************************************************/
! 16613: /************** Begin file mem5.c ********************************************/
! 16614: /*
! 16615: ** 2007 October 14
! 16616: **
! 16617: ** The author disclaims copyright to this source code. In place of
! 16618: ** a legal notice, here is a blessing:
! 16619: **
! 16620: ** May you do good and not evil.
! 16621: ** May you find forgiveness for yourself and forgive others.
! 16622: ** May you share freely, never taking more than you give.
! 16623: **
! 16624: *************************************************************************
! 16625: ** This file contains the C functions that implement a memory
! 16626: ** allocation subsystem for use by SQLite.
! 16627: **
! 16628: ** This version of the memory allocation subsystem omits all
! 16629: ** use of malloc(). The application gives SQLite a block of memory
! 16630: ** before calling sqlite3_initialize() from which allocations
! 16631: ** are made and returned by the xMalloc() and xRealloc()
! 16632: ** implementations. Once sqlite3_initialize() has been called,
! 16633: ** the amount of memory available to SQLite is fixed and cannot
! 16634: ** be changed.
! 16635: **
! 16636: ** This version of the memory allocation subsystem is included
! 16637: ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
! 16638: **
! 16639: ** This memory allocator uses the following algorithm:
! 16640: **
! 16641: ** 1. All memory allocations sizes are rounded up to a power of 2.
! 16642: **
! 16643: ** 2. If two adjacent free blocks are the halves of a larger block,
! 16644: ** then the two blocks are coalesed into the single larger block.
! 16645: **
! 16646: ** 3. New memory is allocated from the first available free block.
! 16647: **
! 16648: ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
! 16649: ** Concerning Dynamic Storage Allocation". Journal of the Association for
! 16650: ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
! 16651: **
! 16652: ** Let n be the size of the largest allocation divided by the minimum
! 16653: ** allocation size (after rounding all sizes up to a power of 2.) Let M
! 16654: ** be the maximum amount of memory ever outstanding at one time. Let
! 16655: ** N be the total amount of memory available for allocation. Robson
! 16656: ** proved that this memory allocator will never breakdown due to
! 16657: ** fragmentation as long as the following constraint holds:
! 16658: **
! 16659: ** N >= M*(1 + log2(n)/2) - n + 1
! 16660: **
! 16661: ** The sqlite3_status() logic tracks the maximum values of n and M so
! 16662: ** that an application can, at any time, verify this constraint.
! 16663: */
! 16664:
! 16665: /*
! 16666: ** This version of the memory allocator is used only when
! 16667: ** SQLITE_ENABLE_MEMSYS5 is defined.
! 16668: */
! 16669: #ifdef SQLITE_ENABLE_MEMSYS5
! 16670:
! 16671: /*
! 16672: ** A minimum allocation is an instance of the following structure.
! 16673: ** Larger allocations are an array of these structures where the
! 16674: ** size of the array is a power of 2.
! 16675: **
! 16676: ** The size of this object must be a power of two. That fact is
! 16677: ** verified in memsys5Init().
! 16678: */
! 16679: typedef struct Mem5Link Mem5Link;
! 16680: struct Mem5Link {
! 16681: int next; /* Index of next free chunk */
! 16682: int prev; /* Index of previous free chunk */
! 16683: };
! 16684:
! 16685: /*
! 16686: ** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
! 16687: ** mem5.szAtom is always at least 8 and 32-bit integers are used,
! 16688: ** it is not actually possible to reach this limit.
! 16689: */
! 16690: #define LOGMAX 30
! 16691:
! 16692: /*
! 16693: ** Masks used for mem5.aCtrl[] elements.
! 16694: */
! 16695: #define CTRL_LOGSIZE 0x1f /* Log2 Size of this block */
! 16696: #define CTRL_FREE 0x20 /* True if not checked out */
! 16697:
! 16698: /*
! 16699: ** All of the static variables used by this module are collected
! 16700: ** into a single structure named "mem5". This is to keep the
! 16701: ** static variables organized and to reduce namespace pollution
! 16702: ** when this module is combined with other in the amalgamation.
! 16703: */
! 16704: static SQLITE_WSD struct Mem5Global {
! 16705: /*
! 16706: ** Memory available for allocation
! 16707: */
! 16708: int szAtom; /* Smallest possible allocation in bytes */
! 16709: int nBlock; /* Number of szAtom sized blocks in zPool */
! 16710: u8 *zPool; /* Memory available to be allocated */
! 16711:
! 16712: /*
! 16713: ** Mutex to control access to the memory allocation subsystem.
! 16714: */
! 16715: sqlite3_mutex *mutex;
! 16716:
! 16717: /*
! 16718: ** Performance statistics
! 16719: */
! 16720: u64 nAlloc; /* Total number of calls to malloc */
! 16721: u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
! 16722: u64 totalExcess; /* Total internal fragmentation */
! 16723: u32 currentOut; /* Current checkout, including internal fragmentation */
! 16724: u32 currentCount; /* Current number of distinct checkouts */
! 16725: u32 maxOut; /* Maximum instantaneous currentOut */
! 16726: u32 maxCount; /* Maximum instantaneous currentCount */
! 16727: u32 maxRequest; /* Largest allocation (exclusive of internal frag) */
! 16728:
! 16729: /*
! 16730: ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
! 16731: ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2.
! 16732: ** and so forth.
! 16733: */
! 16734: int aiFreelist[LOGMAX+1];
! 16735:
! 16736: /*
! 16737: ** Space for tracking which blocks are checked out and the size
! 16738: ** of each block. One byte per block.
! 16739: */
! 16740: u8 *aCtrl;
! 16741:
! 16742: } mem5;
! 16743:
! 16744: /*
! 16745: ** Access the static variable through a macro for SQLITE_OMIT_WSD
! 16746: */
! 16747: #define mem5 GLOBAL(struct Mem5Global, mem5)
! 16748:
! 16749: /*
! 16750: ** Assuming mem5.zPool is divided up into an array of Mem5Link
! 16751: ** structures, return a pointer to the idx-th such lik.
! 16752: */
! 16753: #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
! 16754:
! 16755: /*
! 16756: ** Unlink the chunk at mem5.aPool[i] from list it is currently
! 16757: ** on. It should be found on mem5.aiFreelist[iLogsize].
! 16758: */
! 16759: static void memsys5Unlink(int i, int iLogsize){
! 16760: int next, prev;
! 16761: assert( i>=0 && i<mem5.nBlock );
! 16762: assert( iLogsize>=0 && iLogsize<=LOGMAX );
! 16763: assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
! 16764:
! 16765: next = MEM5LINK(i)->next;
! 16766: prev = MEM5LINK(i)->prev;
! 16767: if( prev<0 ){
! 16768: mem5.aiFreelist[iLogsize] = next;
! 16769: }else{
! 16770: MEM5LINK(prev)->next = next;
! 16771: }
! 16772: if( next>=0 ){
! 16773: MEM5LINK(next)->prev = prev;
! 16774: }
! 16775: }
! 16776:
! 16777: /*
! 16778: ** Link the chunk at mem5.aPool[i] so that is on the iLogsize
! 16779: ** free list.
! 16780: */
! 16781: static void memsys5Link(int i, int iLogsize){
! 16782: int x;
! 16783: assert( sqlite3_mutex_held(mem5.mutex) );
! 16784: assert( i>=0 && i<mem5.nBlock );
! 16785: assert( iLogsize>=0 && iLogsize<=LOGMAX );
! 16786: assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
! 16787:
! 16788: x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
! 16789: MEM5LINK(i)->prev = -1;
! 16790: if( x>=0 ){
! 16791: assert( x<mem5.nBlock );
! 16792: MEM5LINK(x)->prev = i;
! 16793: }
! 16794: mem5.aiFreelist[iLogsize] = i;
! 16795: }
! 16796:
! 16797: /*
! 16798: ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
! 16799: ** will already be held (obtained by code in malloc.c) if
! 16800: ** sqlite3GlobalConfig.bMemStat is true.
! 16801: */
! 16802: static void memsys5Enter(void){
! 16803: sqlite3_mutex_enter(mem5.mutex);
! 16804: }
! 16805: static void memsys5Leave(void){
! 16806: sqlite3_mutex_leave(mem5.mutex);
! 16807: }
! 16808:
! 16809: /*
! 16810: ** Return the size of an outstanding allocation, in bytes. The
! 16811: ** size returned omits the 8-byte header overhead. This only
! 16812: ** works for chunks that are currently checked out.
! 16813: */
! 16814: static int memsys5Size(void *p){
! 16815: int iSize = 0;
! 16816: if( p ){
! 16817: int i = ((u8 *)p-mem5.zPool)/mem5.szAtom;
! 16818: assert( i>=0 && i<mem5.nBlock );
! 16819: iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
! 16820: }
! 16821: return iSize;
! 16822: }
! 16823:
! 16824: /*
! 16825: ** Find the first entry on the freelist iLogsize. Unlink that
! 16826: ** entry and return its index.
! 16827: */
! 16828: static int memsys5UnlinkFirst(int iLogsize){
! 16829: int i;
! 16830: int iFirst;
! 16831:
! 16832: assert( iLogsize>=0 && iLogsize<=LOGMAX );
! 16833: i = iFirst = mem5.aiFreelist[iLogsize];
! 16834: assert( iFirst>=0 );
! 16835: while( i>0 ){
! 16836: if( i<iFirst ) iFirst = i;
! 16837: i = MEM5LINK(i)->next;
! 16838: }
! 16839: memsys5Unlink(iFirst, iLogsize);
! 16840: return iFirst;
! 16841: }
! 16842:
! 16843: /*
! 16844: ** Return a block of memory of at least nBytes in size.
! 16845: ** Return NULL if unable. Return NULL if nBytes==0.
! 16846: **
! 16847: ** The caller guarantees that nByte positive.
! 16848: **
! 16849: ** The caller has obtained a mutex prior to invoking this
! 16850: ** routine so there is never any chance that two or more
! 16851: ** threads can be in this routine at the same time.
! 16852: */
! 16853: static void *memsys5MallocUnsafe(int nByte){
! 16854: int i; /* Index of a mem5.aPool[] slot */
! 16855: int iBin; /* Index into mem5.aiFreelist[] */
! 16856: int iFullSz; /* Size of allocation rounded up to power of 2 */
! 16857: int iLogsize; /* Log2 of iFullSz/POW2_MIN */
! 16858:
! 16859: /* nByte must be a positive */
! 16860: assert( nByte>0 );
! 16861:
! 16862: /* Keep track of the maximum allocation request. Even unfulfilled
! 16863: ** requests are counted */
! 16864: if( (u32)nByte>mem5.maxRequest ){
! 16865: mem5.maxRequest = nByte;
! 16866: }
! 16867:
! 16868: /* Abort if the requested allocation size is larger than the largest
! 16869: ** power of two that we can represent using 32-bit signed integers.
! 16870: */
! 16871: if( nByte > 0x40000000 ){
! 16872: return 0;
! 16873: }
! 16874:
! 16875: /* Round nByte up to the next valid power of two */
! 16876: for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
! 16877:
! 16878: /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
! 16879: ** block. If not, then split a block of the next larger power of
! 16880: ** two in order to create a new free block of size iLogsize.
! 16881: */
! 16882: for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
! 16883: if( iBin>LOGMAX ){
! 16884: testcase( sqlite3GlobalConfig.xLog!=0 );
! 16885: sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
! 16886: return 0;
! 16887: }
! 16888: i = memsys5UnlinkFirst(iBin);
! 16889: while( iBin>iLogsize ){
! 16890: int newSize;
! 16891:
! 16892: iBin--;
! 16893: newSize = 1 << iBin;
! 16894: mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
! 16895: memsys5Link(i+newSize, iBin);
! 16896: }
! 16897: mem5.aCtrl[i] = iLogsize;
! 16898:
! 16899: /* Update allocator performance statistics. */
! 16900: mem5.nAlloc++;
! 16901: mem5.totalAlloc += iFullSz;
! 16902: mem5.totalExcess += iFullSz - nByte;
! 16903: mem5.currentCount++;
! 16904: mem5.currentOut += iFullSz;
! 16905: if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
! 16906: if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
! 16907:
! 16908: /* Return a pointer to the allocated memory. */
! 16909: return (void*)&mem5.zPool[i*mem5.szAtom];
! 16910: }
! 16911:
! 16912: /*
! 16913: ** Free an outstanding memory allocation.
! 16914: */
! 16915: static void memsys5FreeUnsafe(void *pOld){
! 16916: u32 size, iLogsize;
! 16917: int iBlock;
! 16918:
! 16919: /* Set iBlock to the index of the block pointed to by pOld in
! 16920: ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
! 16921: */
! 16922: iBlock = ((u8 *)pOld-mem5.zPool)/mem5.szAtom;
! 16923:
! 16924: /* Check that the pointer pOld points to a valid, non-free block. */
! 16925: assert( iBlock>=0 && iBlock<mem5.nBlock );
! 16926: assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
! 16927: assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
! 16928:
! 16929: iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
! 16930: size = 1<<iLogsize;
! 16931: assert( iBlock+size-1<(u32)mem5.nBlock );
! 16932:
! 16933: mem5.aCtrl[iBlock] |= CTRL_FREE;
! 16934: mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
! 16935: assert( mem5.currentCount>0 );
! 16936: assert( mem5.currentOut>=(size*mem5.szAtom) );
! 16937: mem5.currentCount--;
! 16938: mem5.currentOut -= size*mem5.szAtom;
! 16939: assert( mem5.currentOut>0 || mem5.currentCount==0 );
! 16940: assert( mem5.currentCount>0 || mem5.currentOut==0 );
! 16941:
! 16942: mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
! 16943: while( ALWAYS(iLogsize<LOGMAX) ){
! 16944: int iBuddy;
! 16945: if( (iBlock>>iLogsize) & 1 ){
! 16946: iBuddy = iBlock - size;
! 16947: }else{
! 16948: iBuddy = iBlock + size;
! 16949: }
! 16950: assert( iBuddy>=0 );
! 16951: if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
! 16952: if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
! 16953: memsys5Unlink(iBuddy, iLogsize);
! 16954: iLogsize++;
! 16955: if( iBuddy<iBlock ){
! 16956: mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
! 16957: mem5.aCtrl[iBlock] = 0;
! 16958: iBlock = iBuddy;
! 16959: }else{
! 16960: mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
! 16961: mem5.aCtrl[iBuddy] = 0;
! 16962: }
! 16963: size *= 2;
! 16964: }
! 16965: memsys5Link(iBlock, iLogsize);
! 16966: }
! 16967:
! 16968: /*
! 16969: ** Allocate nBytes of memory
! 16970: */
! 16971: static void *memsys5Malloc(int nBytes){
! 16972: sqlite3_int64 *p = 0;
! 16973: if( nBytes>0 ){
! 16974: memsys5Enter();
! 16975: p = memsys5MallocUnsafe(nBytes);
! 16976: memsys5Leave();
! 16977: }
! 16978: return (void*)p;
! 16979: }
! 16980:
! 16981: /*
! 16982: ** Free memory.
! 16983: **
! 16984: ** The outer layer memory allocator prevents this routine from
! 16985: ** being called with pPrior==0.
! 16986: */
! 16987: static void memsys5Free(void *pPrior){
! 16988: assert( pPrior!=0 );
! 16989: memsys5Enter();
! 16990: memsys5FreeUnsafe(pPrior);
! 16991: memsys5Leave();
! 16992: }
! 16993:
! 16994: /*
! 16995: ** Change the size of an existing memory allocation.
! 16996: **
! 16997: ** The outer layer memory allocator prevents this routine from
! 16998: ** being called with pPrior==0.
! 16999: **
! 17000: ** nBytes is always a value obtained from a prior call to
! 17001: ** memsys5Round(). Hence nBytes is always a non-negative power
! 17002: ** of two. If nBytes==0 that means that an oversize allocation
! 17003: ** (an allocation larger than 0x40000000) was requested and this
! 17004: ** routine should return 0 without freeing pPrior.
! 17005: */
! 17006: static void *memsys5Realloc(void *pPrior, int nBytes){
! 17007: int nOld;
! 17008: void *p;
! 17009: assert( pPrior!=0 );
! 17010: assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */
! 17011: assert( nBytes>=0 );
! 17012: if( nBytes==0 ){
! 17013: return 0;
! 17014: }
! 17015: nOld = memsys5Size(pPrior);
! 17016: if( nBytes<=nOld ){
! 17017: return pPrior;
! 17018: }
! 17019: memsys5Enter();
! 17020: p = memsys5MallocUnsafe(nBytes);
! 17021: if( p ){
! 17022: memcpy(p, pPrior, nOld);
! 17023: memsys5FreeUnsafe(pPrior);
! 17024: }
! 17025: memsys5Leave();
! 17026: return p;
! 17027: }
! 17028:
! 17029: /*
! 17030: ** Round up a request size to the next valid allocation size. If
! 17031: ** the allocation is too large to be handled by this allocation system,
! 17032: ** return 0.
! 17033: **
! 17034: ** All allocations must be a power of two and must be expressed by a
! 17035: ** 32-bit signed integer. Hence the largest allocation is 0x40000000
! 17036: ** or 1073741824 bytes.
! 17037: */
! 17038: static int memsys5Roundup(int n){
! 17039: int iFullSz;
! 17040: if( n > 0x40000000 ) return 0;
! 17041: for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
! 17042: return iFullSz;
! 17043: }
! 17044:
! 17045: /*
! 17046: ** Return the ceiling of the logarithm base 2 of iValue.
! 17047: **
! 17048: ** Examples: memsys5Log(1) -> 0
! 17049: ** memsys5Log(2) -> 1
! 17050: ** memsys5Log(4) -> 2
! 17051: ** memsys5Log(5) -> 3
! 17052: ** memsys5Log(8) -> 3
! 17053: ** memsys5Log(9) -> 4
! 17054: */
! 17055: static int memsys5Log(int iValue){
! 17056: int iLog;
! 17057: for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
! 17058: return iLog;
! 17059: }
! 17060:
! 17061: /*
! 17062: ** Initialize the memory allocator.
! 17063: **
! 17064: ** This routine is not threadsafe. The caller must be holding a mutex
! 17065: ** to prevent multiple threads from entering at the same time.
! 17066: */
! 17067: static int memsys5Init(void *NotUsed){
! 17068: int ii; /* Loop counter */
! 17069: int nByte; /* Number of bytes of memory available to this allocator */
! 17070: u8 *zByte; /* Memory usable by this allocator */
! 17071: int nMinLog; /* Log base 2 of minimum allocation size in bytes */
! 17072: int iOffset; /* An offset into mem5.aCtrl[] */
! 17073:
! 17074: UNUSED_PARAMETER(NotUsed);
! 17075:
! 17076: /* For the purposes of this routine, disable the mutex */
! 17077: mem5.mutex = 0;
! 17078:
! 17079: /* The size of a Mem5Link object must be a power of two. Verify that
! 17080: ** this is case.
! 17081: */
! 17082: assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
! 17083:
! 17084: nByte = sqlite3GlobalConfig.nHeap;
! 17085: zByte = (u8*)sqlite3GlobalConfig.pHeap;
! 17086: assert( zByte!=0 ); /* sqlite3_config() does not allow otherwise */
! 17087:
! 17088: /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
! 17089: nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
! 17090: mem5.szAtom = (1<<nMinLog);
! 17091: while( (int)sizeof(Mem5Link)>mem5.szAtom ){
! 17092: mem5.szAtom = mem5.szAtom << 1;
! 17093: }
! 17094:
! 17095: mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
! 17096: mem5.zPool = zByte;
! 17097: mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
! 17098:
! 17099: for(ii=0; ii<=LOGMAX; ii++){
! 17100: mem5.aiFreelist[ii] = -1;
! 17101: }
! 17102:
! 17103: iOffset = 0;
! 17104: for(ii=LOGMAX; ii>=0; ii--){
! 17105: int nAlloc = (1<<ii);
! 17106: if( (iOffset+nAlloc)<=mem5.nBlock ){
! 17107: mem5.aCtrl[iOffset] = ii | CTRL_FREE;
! 17108: memsys5Link(iOffset, ii);
! 17109: iOffset += nAlloc;
! 17110: }
! 17111: assert((iOffset+nAlloc)>mem5.nBlock);
! 17112: }
! 17113:
! 17114: /* If a mutex is required for normal operation, allocate one */
! 17115: if( sqlite3GlobalConfig.bMemstat==0 ){
! 17116: mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
! 17117: }
! 17118:
! 17119: return SQLITE_OK;
! 17120: }
! 17121:
! 17122: /*
! 17123: ** Deinitialize this module.
! 17124: */
! 17125: static void memsys5Shutdown(void *NotUsed){
! 17126: UNUSED_PARAMETER(NotUsed);
! 17127: mem5.mutex = 0;
! 17128: return;
! 17129: }
! 17130:
! 17131: #ifdef SQLITE_TEST
! 17132: /*
! 17133: ** Open the file indicated and write a log of all unfreed memory
! 17134: ** allocations into that log.
! 17135: */
! 17136: SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
! 17137: FILE *out;
! 17138: int i, j, n;
! 17139: int nMinLog;
! 17140:
! 17141: if( zFilename==0 || zFilename[0]==0 ){
! 17142: out = stdout;
! 17143: }else{
! 17144: out = fopen(zFilename, "w");
! 17145: if( out==0 ){
! 17146: fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
! 17147: zFilename);
! 17148: return;
! 17149: }
! 17150: }
! 17151: memsys5Enter();
! 17152: nMinLog = memsys5Log(mem5.szAtom);
! 17153: for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
! 17154: for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
! 17155: fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
! 17156: }
! 17157: fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
! 17158: fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
! 17159: fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
! 17160: fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
! 17161: fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
! 17162: fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
! 17163: fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
! 17164: fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
! 17165: memsys5Leave();
! 17166: if( out==stdout ){
! 17167: fflush(stdout);
! 17168: }else{
! 17169: fclose(out);
! 17170: }
! 17171: }
! 17172: #endif
! 17173:
! 17174: /*
! 17175: ** This routine is the only routine in this file with external
! 17176: ** linkage. It returns a pointer to a static sqlite3_mem_methods
! 17177: ** struct populated with the memsys5 methods.
! 17178: */
! 17179: SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
! 17180: static const sqlite3_mem_methods memsys5Methods = {
! 17181: memsys5Malloc,
! 17182: memsys5Free,
! 17183: memsys5Realloc,
! 17184: memsys5Size,
! 17185: memsys5Roundup,
! 17186: memsys5Init,
! 17187: memsys5Shutdown,
! 17188: 0
! 17189: };
! 17190: return &memsys5Methods;
! 17191: }
! 17192:
! 17193: #endif /* SQLITE_ENABLE_MEMSYS5 */
! 17194:
! 17195: /************** End of mem5.c ************************************************/
! 17196: /************** Begin file mutex.c *******************************************/
! 17197: /*
! 17198: ** 2007 August 14
! 17199: **
! 17200: ** The author disclaims copyright to this source code. In place of
! 17201: ** a legal notice, here is a blessing:
! 17202: **
! 17203: ** May you do good and not evil.
! 17204: ** May you find forgiveness for yourself and forgive others.
! 17205: ** May you share freely, never taking more than you give.
! 17206: **
! 17207: *************************************************************************
! 17208: ** This file contains the C functions that implement mutexes.
! 17209: **
! 17210: ** This file contains code that is common across all mutex implementations.
! 17211: */
! 17212:
! 17213: #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
! 17214: /*
! 17215: ** For debugging purposes, record when the mutex subsystem is initialized
! 17216: ** and uninitialized so that we can assert() if there is an attempt to
! 17217: ** allocate a mutex while the system is uninitialized.
! 17218: */
! 17219: static SQLITE_WSD int mutexIsInit = 0;
! 17220: #endif /* SQLITE_DEBUG */
! 17221:
! 17222:
! 17223: #ifndef SQLITE_MUTEX_OMIT
! 17224: /*
! 17225: ** Initialize the mutex system.
! 17226: */
! 17227: SQLITE_PRIVATE int sqlite3MutexInit(void){
! 17228: int rc = SQLITE_OK;
! 17229: if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
! 17230: /* If the xMutexAlloc method has not been set, then the user did not
! 17231: ** install a mutex implementation via sqlite3_config() prior to
! 17232: ** sqlite3_initialize() being called. This block copies pointers to
! 17233: ** the default implementation into the sqlite3GlobalConfig structure.
! 17234: */
! 17235: sqlite3_mutex_methods const *pFrom;
! 17236: sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
! 17237:
! 17238: if( sqlite3GlobalConfig.bCoreMutex ){
! 17239: pFrom = sqlite3DefaultMutex();
! 17240: }else{
! 17241: pFrom = sqlite3NoopMutex();
! 17242: }
! 17243: memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
! 17244: memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
! 17245: sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
! 17246: pTo->xMutexAlloc = pFrom->xMutexAlloc;
! 17247: }
! 17248: rc = sqlite3GlobalConfig.mutex.xMutexInit();
! 17249:
! 17250: #ifdef SQLITE_DEBUG
! 17251: GLOBAL(int, mutexIsInit) = 1;
! 17252: #endif
! 17253:
! 17254: return rc;
! 17255: }
! 17256:
! 17257: /*
! 17258: ** Shutdown the mutex system. This call frees resources allocated by
! 17259: ** sqlite3MutexInit().
! 17260: */
! 17261: SQLITE_PRIVATE int sqlite3MutexEnd(void){
! 17262: int rc = SQLITE_OK;
! 17263: if( sqlite3GlobalConfig.mutex.xMutexEnd ){
! 17264: rc = sqlite3GlobalConfig.mutex.xMutexEnd();
! 17265: }
! 17266:
! 17267: #ifdef SQLITE_DEBUG
! 17268: GLOBAL(int, mutexIsInit) = 0;
! 17269: #endif
! 17270:
! 17271: return rc;
! 17272: }
! 17273:
! 17274: /*
! 17275: ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
! 17276: */
! 17277: SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
! 17278: #ifndef SQLITE_OMIT_AUTOINIT
! 17279: if( sqlite3_initialize() ) return 0;
! 17280: #endif
! 17281: return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
! 17282: }
! 17283:
! 17284: SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
! 17285: if( !sqlite3GlobalConfig.bCoreMutex ){
! 17286: return 0;
! 17287: }
! 17288: assert( GLOBAL(int, mutexIsInit) );
! 17289: return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
! 17290: }
! 17291:
! 17292: /*
! 17293: ** Free a dynamic mutex.
! 17294: */
! 17295: SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
! 17296: if( p ){
! 17297: sqlite3GlobalConfig.mutex.xMutexFree(p);
! 17298: }
! 17299: }
! 17300:
! 17301: /*
! 17302: ** Obtain the mutex p. If some other thread already has the mutex, block
! 17303: ** until it can be obtained.
! 17304: */
! 17305: SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
! 17306: if( p ){
! 17307: sqlite3GlobalConfig.mutex.xMutexEnter(p);
! 17308: }
! 17309: }
! 17310:
! 17311: /*
! 17312: ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
! 17313: ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
! 17314: */
! 17315: SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
! 17316: int rc = SQLITE_OK;
! 17317: if( p ){
! 17318: return sqlite3GlobalConfig.mutex.xMutexTry(p);
! 17319: }
! 17320: return rc;
! 17321: }
! 17322:
! 17323: /*
! 17324: ** The sqlite3_mutex_leave() routine exits a mutex that was previously
! 17325: ** entered by the same thread. The behavior is undefined if the mutex
! 17326: ** is not currently entered. If a NULL pointer is passed as an argument
! 17327: ** this function is a no-op.
! 17328: */
! 17329: SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
! 17330: if( p ){
! 17331: sqlite3GlobalConfig.mutex.xMutexLeave(p);
! 17332: }
! 17333: }
! 17334:
! 17335: #ifndef NDEBUG
! 17336: /*
! 17337: ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
! 17338: ** intended for use inside assert() statements.
! 17339: */
! 17340: SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
! 17341: return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
! 17342: }
! 17343: SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
! 17344: return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
! 17345: }
! 17346: #endif
! 17347:
! 17348: #endif /* !defined(SQLITE_MUTEX_OMIT) */
! 17349:
! 17350: /************** End of mutex.c ***********************************************/
! 17351: /************** Begin file mutex_noop.c **************************************/
! 17352: /*
! 17353: ** 2008 October 07
! 17354: **
! 17355: ** The author disclaims copyright to this source code. In place of
! 17356: ** a legal notice, here is a blessing:
! 17357: **
! 17358: ** May you do good and not evil.
! 17359: ** May you find forgiveness for yourself and forgive others.
! 17360: ** May you share freely, never taking more than you give.
! 17361: **
! 17362: *************************************************************************
! 17363: ** This file contains the C functions that implement mutexes.
! 17364: **
! 17365: ** This implementation in this file does not provide any mutual
! 17366: ** exclusion and is thus suitable for use only in applications
! 17367: ** that use SQLite in a single thread. The routines defined
! 17368: ** here are place-holders. Applications can substitute working
! 17369: ** mutex routines at start-time using the
! 17370: **
! 17371: ** sqlite3_config(SQLITE_CONFIG_MUTEX,...)
! 17372: **
! 17373: ** interface.
! 17374: **
! 17375: ** If compiled with SQLITE_DEBUG, then additional logic is inserted
! 17376: ** that does error checking on mutexes to make sure they are being
! 17377: ** called correctly.
! 17378: */
! 17379:
! 17380: #ifndef SQLITE_MUTEX_OMIT
! 17381:
! 17382: #ifndef SQLITE_DEBUG
! 17383: /*
! 17384: ** Stub routines for all mutex methods.
! 17385: **
! 17386: ** This routines provide no mutual exclusion or error checking.
! 17387: */
! 17388: static int noopMutexInit(void){ return SQLITE_OK; }
! 17389: static int noopMutexEnd(void){ return SQLITE_OK; }
! 17390: static sqlite3_mutex *noopMutexAlloc(int id){
! 17391: UNUSED_PARAMETER(id);
! 17392: return (sqlite3_mutex*)8;
! 17393: }
! 17394: static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
! 17395: static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
! 17396: static int noopMutexTry(sqlite3_mutex *p){
! 17397: UNUSED_PARAMETER(p);
! 17398: return SQLITE_OK;
! 17399: }
! 17400: static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
! 17401:
! 17402: SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
! 17403: static const sqlite3_mutex_methods sMutex = {
! 17404: noopMutexInit,
! 17405: noopMutexEnd,
! 17406: noopMutexAlloc,
! 17407: noopMutexFree,
! 17408: noopMutexEnter,
! 17409: noopMutexTry,
! 17410: noopMutexLeave,
! 17411:
! 17412: 0,
! 17413: 0,
! 17414: };
! 17415:
! 17416: return &sMutex;
! 17417: }
! 17418: #endif /* !SQLITE_DEBUG */
! 17419:
! 17420: #ifdef SQLITE_DEBUG
! 17421: /*
! 17422: ** In this implementation, error checking is provided for testing
! 17423: ** and debugging purposes. The mutexes still do not provide any
! 17424: ** mutual exclusion.
! 17425: */
! 17426:
! 17427: /*
! 17428: ** The mutex object
! 17429: */
! 17430: typedef struct sqlite3_debug_mutex {
! 17431: int id; /* The mutex type */
! 17432: int cnt; /* Number of entries without a matching leave */
! 17433: } sqlite3_debug_mutex;
! 17434:
! 17435: /*
! 17436: ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
! 17437: ** intended for use inside assert() statements.
! 17438: */
! 17439: static int debugMutexHeld(sqlite3_mutex *pX){
! 17440: sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
! 17441: return p==0 || p->cnt>0;
! 17442: }
! 17443: static int debugMutexNotheld(sqlite3_mutex *pX){
! 17444: sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
! 17445: return p==0 || p->cnt==0;
! 17446: }
! 17447:
! 17448: /*
! 17449: ** Initialize and deinitialize the mutex subsystem.
! 17450: */
! 17451: static int debugMutexInit(void){ return SQLITE_OK; }
! 17452: static int debugMutexEnd(void){ return SQLITE_OK; }
! 17453:
! 17454: /*
! 17455: ** The sqlite3_mutex_alloc() routine allocates a new
! 17456: ** mutex and returns a pointer to it. If it returns NULL
! 17457: ** that means that a mutex could not be allocated.
! 17458: */
! 17459: static sqlite3_mutex *debugMutexAlloc(int id){
! 17460: static sqlite3_debug_mutex aStatic[6];
! 17461: sqlite3_debug_mutex *pNew = 0;
! 17462: switch( id ){
! 17463: case SQLITE_MUTEX_FAST:
! 17464: case SQLITE_MUTEX_RECURSIVE: {
! 17465: pNew = sqlite3Malloc(sizeof(*pNew));
! 17466: if( pNew ){
! 17467: pNew->id = id;
! 17468: pNew->cnt = 0;
! 17469: }
! 17470: break;
! 17471: }
! 17472: default: {
! 17473: assert( id-2 >= 0 );
! 17474: assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
! 17475: pNew = &aStatic[id-2];
! 17476: pNew->id = id;
! 17477: break;
! 17478: }
! 17479: }
! 17480: return (sqlite3_mutex*)pNew;
! 17481: }
! 17482:
! 17483: /*
! 17484: ** This routine deallocates a previously allocated mutex.
! 17485: */
! 17486: static void debugMutexFree(sqlite3_mutex *pX){
! 17487: sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
! 17488: assert( p->cnt==0 );
! 17489: assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
! 17490: sqlite3_free(p);
! 17491: }
! 17492:
! 17493: /*
! 17494: ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
! 17495: ** to enter a mutex. If another thread is already within the mutex,
! 17496: ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
! 17497: ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
! 17498: ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
! 17499: ** be entered multiple times by the same thread. In such cases the,
! 17500: ** mutex must be exited an equal number of times before another thread
! 17501: ** can enter. If the same thread tries to enter any other kind of mutex
! 17502: ** more than once, the behavior is undefined.
! 17503: */
! 17504: static void debugMutexEnter(sqlite3_mutex *pX){
! 17505: sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
! 17506: assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
! 17507: p->cnt++;
! 17508: }
! 17509: static int debugMutexTry(sqlite3_mutex *pX){
! 17510: sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
! 17511: assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
! 17512: p->cnt++;
! 17513: return SQLITE_OK;
! 17514: }
! 17515:
! 17516: /*
! 17517: ** The sqlite3_mutex_leave() routine exits a mutex that was
! 17518: ** previously entered by the same thread. The behavior
! 17519: ** is undefined if the mutex is not currently entered or
! 17520: ** is not currently allocated. SQLite will never do either.
! 17521: */
! 17522: static void debugMutexLeave(sqlite3_mutex *pX){
! 17523: sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
! 17524: assert( debugMutexHeld(pX) );
! 17525: p->cnt--;
! 17526: assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
! 17527: }
! 17528:
! 17529: SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
! 17530: static const sqlite3_mutex_methods sMutex = {
! 17531: debugMutexInit,
! 17532: debugMutexEnd,
! 17533: debugMutexAlloc,
! 17534: debugMutexFree,
! 17535: debugMutexEnter,
! 17536: debugMutexTry,
! 17537: debugMutexLeave,
! 17538:
! 17539: debugMutexHeld,
! 17540: debugMutexNotheld
! 17541: };
! 17542:
! 17543: return &sMutex;
! 17544: }
! 17545: #endif /* SQLITE_DEBUG */
! 17546:
! 17547: /*
! 17548: ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
! 17549: ** is used regardless of the run-time threadsafety setting.
! 17550: */
! 17551: #ifdef SQLITE_MUTEX_NOOP
! 17552: SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
! 17553: return sqlite3NoopMutex();
! 17554: }
! 17555: #endif /* defined(SQLITE_MUTEX_NOOP) */
! 17556: #endif /* !defined(SQLITE_MUTEX_OMIT) */
! 17557:
! 17558: /************** End of mutex_noop.c ******************************************/
! 17559: /************** Begin file mutex_os2.c ***************************************/
! 17560: /*
! 17561: ** 2007 August 28
! 17562: **
! 17563: ** The author disclaims copyright to this source code. In place of
! 17564: ** a legal notice, here is a blessing:
! 17565: **
! 17566: ** May you do good and not evil.
! 17567: ** May you find forgiveness for yourself and forgive others.
! 17568: ** May you share freely, never taking more than you give.
! 17569: **
! 17570: *************************************************************************
! 17571: ** This file contains the C functions that implement mutexes for OS/2
! 17572: */
! 17573:
! 17574: /*
! 17575: ** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
! 17576: ** See the mutex.h file for details.
! 17577: */
! 17578: #ifdef SQLITE_MUTEX_OS2
! 17579:
! 17580: /********************** OS/2 Mutex Implementation **********************
! 17581: **
! 17582: ** This implementation of mutexes is built using the OS/2 API.
! 17583: */
! 17584:
! 17585: /*
! 17586: ** The mutex object
! 17587: ** Each recursive mutex is an instance of the following structure.
! 17588: */
! 17589: struct sqlite3_mutex {
! 17590: HMTX mutex; /* Mutex controlling the lock */
! 17591: int id; /* Mutex type */
! 17592: #ifdef SQLITE_DEBUG
! 17593: int trace; /* True to trace changes */
! 17594: #endif
! 17595: };
! 17596:
! 17597: #ifdef SQLITE_DEBUG
! 17598: #define SQLITE3_MUTEX_INITIALIZER { 0, 0, 0 }
! 17599: #else
! 17600: #define SQLITE3_MUTEX_INITIALIZER { 0, 0 }
! 17601: #endif
! 17602:
! 17603: /*
! 17604: ** Initialize and deinitialize the mutex subsystem.
! 17605: */
! 17606: static int os2MutexInit(void){ return SQLITE_OK; }
! 17607: static int os2MutexEnd(void){ return SQLITE_OK; }
! 17608:
! 17609: /*
! 17610: ** The sqlite3_mutex_alloc() routine allocates a new
! 17611: ** mutex and returns a pointer to it. If it returns NULL
! 17612: ** that means that a mutex could not be allocated.
! 17613: ** SQLite will unwind its stack and return an error. The argument
! 17614: ** to sqlite3_mutex_alloc() is one of these integer constants:
! 17615: **
! 17616: ** <ul>
! 17617: ** <li> SQLITE_MUTEX_FAST
! 17618: ** <li> SQLITE_MUTEX_RECURSIVE
! 17619: ** <li> SQLITE_MUTEX_STATIC_MASTER
! 17620: ** <li> SQLITE_MUTEX_STATIC_MEM
! 17621: ** <li> SQLITE_MUTEX_STATIC_MEM2
! 17622: ** <li> SQLITE_MUTEX_STATIC_PRNG
! 17623: ** <li> SQLITE_MUTEX_STATIC_LRU
! 17624: ** <li> SQLITE_MUTEX_STATIC_LRU2
! 17625: ** </ul>
! 17626: **
! 17627: ** The first two constants cause sqlite3_mutex_alloc() to create
! 17628: ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
! 17629: ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
! 17630: ** The mutex implementation does not need to make a distinction
! 17631: ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
! 17632: ** not want to. But SQLite will only request a recursive mutex in
! 17633: ** cases where it really needs one. If a faster non-recursive mutex
! 17634: ** implementation is available on the host platform, the mutex subsystem
! 17635: ** might return such a mutex in response to SQLITE_MUTEX_FAST.
! 17636: **
! 17637: ** The other allowed parameters to sqlite3_mutex_alloc() each return
! 17638: ** a pointer to a static preexisting mutex. Six static mutexes are
! 17639: ** used by the current version of SQLite. Future versions of SQLite
! 17640: ** may add additional static mutexes. Static mutexes are for internal
! 17641: ** use by SQLite only. Applications that use SQLite mutexes should
! 17642: ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
! 17643: ** SQLITE_MUTEX_RECURSIVE.
! 17644: **
! 17645: ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
! 17646: ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
! 17647: ** returns a different mutex on every call. But for the static
! 17648: ** mutex types, the same mutex is returned on every call that has
! 17649: ** the same type number.
! 17650: */
! 17651: static sqlite3_mutex *os2MutexAlloc(int iType){
! 17652: sqlite3_mutex *p = NULL;
! 17653: switch( iType ){
! 17654: case SQLITE_MUTEX_FAST:
! 17655: case SQLITE_MUTEX_RECURSIVE: {
! 17656: p = sqlite3MallocZero( sizeof(*p) );
! 17657: if( p ){
! 17658: p->id = iType;
! 17659: if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){
! 17660: sqlite3_free( p );
! 17661: p = NULL;
! 17662: }
! 17663: }
! 17664: break;
! 17665: }
! 17666: default: {
! 17667: static volatile int isInit = 0;
! 17668: static sqlite3_mutex staticMutexes[6] = {
! 17669: SQLITE3_MUTEX_INITIALIZER,
! 17670: SQLITE3_MUTEX_INITIALIZER,
! 17671: SQLITE3_MUTEX_INITIALIZER,
! 17672: SQLITE3_MUTEX_INITIALIZER,
! 17673: SQLITE3_MUTEX_INITIALIZER,
! 17674: SQLITE3_MUTEX_INITIALIZER,
! 17675: };
! 17676: if ( !isInit ){
! 17677: APIRET rc;
! 17678: PTIB ptib;
! 17679: PPIB ppib;
! 17680: HMTX mutex;
! 17681: char name[32];
! 17682: DosGetInfoBlocks( &ptib, &ppib );
! 17683: sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x",
! 17684: ppib->pib_ulpid );
! 17685: while( !isInit ){
! 17686: mutex = 0;
! 17687: rc = DosCreateMutexSem( name, &mutex, 0, FALSE);
! 17688: if( rc == NO_ERROR ){
! 17689: unsigned int i;
! 17690: if( !isInit ){
! 17691: for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){
! 17692: DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE );
! 17693: }
! 17694: isInit = 1;
! 17695: }
! 17696: DosCloseMutexSem( mutex );
! 17697: }else if( rc == ERROR_DUPLICATE_NAME ){
! 17698: DosSleep( 1 );
! 17699: }else{
! 17700: return p;
! 17701: }
! 17702: }
! 17703: }
! 17704: assert( iType-2 >= 0 );
! 17705: assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
! 17706: p = &staticMutexes[iType-2];
! 17707: p->id = iType;
! 17708: break;
! 17709: }
! 17710: }
! 17711: return p;
! 17712: }
! 17713:
! 17714:
! 17715: /*
! 17716: ** This routine deallocates a previously allocated mutex.
! 17717: ** SQLite is careful to deallocate every mutex that it allocates.
! 17718: */
! 17719: static void os2MutexFree(sqlite3_mutex *p){
! 17720: #ifdef SQLITE_DEBUG
! 17721: TID tid;
! 17722: PID pid;
! 17723: ULONG ulCount;
! 17724: DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
! 17725: assert( ulCount==0 );
! 17726: assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
! 17727: #endif
! 17728: DosCloseMutexSem( p->mutex );
! 17729: sqlite3_free( p );
! 17730: }
! 17731:
! 17732: #ifdef SQLITE_DEBUG
! 17733: /*
! 17734: ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
! 17735: ** intended for use inside assert() statements.
! 17736: */
! 17737: static int os2MutexHeld(sqlite3_mutex *p){
! 17738: TID tid;
! 17739: PID pid;
! 17740: ULONG ulCount;
! 17741: PTIB ptib;
! 17742: DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
! 17743: if( ulCount==0 || ( ulCount>1 && p->id!=SQLITE_MUTEX_RECURSIVE ) )
! 17744: return 0;
! 17745: DosGetInfoBlocks(&ptib, NULL);
! 17746: return tid==ptib->tib_ptib2->tib2_ultid;
! 17747: }
! 17748: static int os2MutexNotheld(sqlite3_mutex *p){
! 17749: TID tid;
! 17750: PID pid;
! 17751: ULONG ulCount;
! 17752: PTIB ptib;
! 17753: DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
! 17754: if( ulCount==0 )
! 17755: return 1;
! 17756: DosGetInfoBlocks(&ptib, NULL);
! 17757: return tid!=ptib->tib_ptib2->tib2_ultid;
! 17758: }
! 17759: static void os2MutexTrace(sqlite3_mutex *p, char *pAction){
! 17760: TID tid;
! 17761: PID pid;
! 17762: ULONG ulCount;
! 17763: DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
! 17764: printf("%s mutex %p (%d) with nRef=%ld\n", pAction, (void*)p, p->trace, ulCount);
! 17765: }
! 17766: #endif
! 17767:
! 17768: /*
! 17769: ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
! 17770: ** to enter a mutex. If another thread is already within the mutex,
! 17771: ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
! 17772: ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
! 17773: ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
! 17774: ** be entered multiple times by the same thread. In such cases the,
! 17775: ** mutex must be exited an equal number of times before another thread
! 17776: ** can enter. If the same thread tries to enter any other kind of mutex
! 17777: ** more than once, the behavior is undefined.
! 17778: */
! 17779: static void os2MutexEnter(sqlite3_mutex *p){
! 17780: assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
! 17781: DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
! 17782: #ifdef SQLITE_DEBUG
! 17783: if( p->trace ) os2MutexTrace(p, "enter");
! 17784: #endif
! 17785: }
! 17786: static int os2MutexTry(sqlite3_mutex *p){
! 17787: int rc = SQLITE_BUSY;
! 17788: assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
! 17789: if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR ) {
! 17790: rc = SQLITE_OK;
! 17791: #ifdef SQLITE_DEBUG
! 17792: if( p->trace ) os2MutexTrace(p, "try");
! 17793: #endif
! 17794: }
! 17795: return rc;
! 17796: }
! 17797:
! 17798: /*
! 17799: ** The sqlite3_mutex_leave() routine exits a mutex that was
! 17800: ** previously entered by the same thread. The behavior
! 17801: ** is undefined if the mutex is not currently entered or
! 17802: ** is not currently allocated. SQLite will never do either.
! 17803: */
! 17804: static void os2MutexLeave(sqlite3_mutex *p){
! 17805: assert( os2MutexHeld(p) );
! 17806: DosReleaseMutexSem(p->mutex);
! 17807: #ifdef SQLITE_DEBUG
! 17808: if( p->trace ) os2MutexTrace(p, "leave");
! 17809: #endif
! 17810: }
! 17811:
! 17812: SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
! 17813: static const sqlite3_mutex_methods sMutex = {
! 17814: os2MutexInit,
! 17815: os2MutexEnd,
! 17816: os2MutexAlloc,
! 17817: os2MutexFree,
! 17818: os2MutexEnter,
! 17819: os2MutexTry,
! 17820: os2MutexLeave,
! 17821: #ifdef SQLITE_DEBUG
! 17822: os2MutexHeld,
! 17823: os2MutexNotheld
! 17824: #else
! 17825: 0,
! 17826: 0
! 17827: #endif
! 17828: };
! 17829:
! 17830: return &sMutex;
! 17831: }
! 17832: #endif /* SQLITE_MUTEX_OS2 */
! 17833:
! 17834: /************** End of mutex_os2.c *******************************************/
! 17835: /************** Begin file mutex_unix.c **************************************/
! 17836: /*
! 17837: ** 2007 August 28
! 17838: **
! 17839: ** The author disclaims copyright to this source code. In place of
! 17840: ** a legal notice, here is a blessing:
! 17841: **
! 17842: ** May you do good and not evil.
! 17843: ** May you find forgiveness for yourself and forgive others.
! 17844: ** May you share freely, never taking more than you give.
! 17845: **
! 17846: *************************************************************************
! 17847: ** This file contains the C functions that implement mutexes for pthreads
! 17848: */
! 17849:
! 17850: /*
! 17851: ** The code in this file is only used if we are compiling threadsafe
! 17852: ** under unix with pthreads.
! 17853: **
! 17854: ** Note that this implementation requires a version of pthreads that
! 17855: ** supports recursive mutexes.
! 17856: */
! 17857: #ifdef SQLITE_MUTEX_PTHREADS
! 17858:
! 17859: #include <pthread.h>
! 17860:
! 17861: /*
! 17862: ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
! 17863: ** are necessary under two condidtions: (1) Debug builds and (2) using
! 17864: ** home-grown mutexes. Encapsulate these conditions into a single #define.
! 17865: */
! 17866: #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
! 17867: # define SQLITE_MUTEX_NREF 1
! 17868: #else
! 17869: # define SQLITE_MUTEX_NREF 0
! 17870: #endif
! 17871:
! 17872: /*
! 17873: ** Each recursive mutex is an instance of the following structure.
! 17874: */
! 17875: struct sqlite3_mutex {
! 17876: pthread_mutex_t mutex; /* Mutex controlling the lock */
! 17877: #if SQLITE_MUTEX_NREF
! 17878: int id; /* Mutex type */
! 17879: volatile int nRef; /* Number of entrances */
! 17880: volatile pthread_t owner; /* Thread that is within this mutex */
! 17881: int trace; /* True to trace changes */
! 17882: #endif
! 17883: };
! 17884: #if SQLITE_MUTEX_NREF
! 17885: #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
! 17886: #else
! 17887: #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
! 17888: #endif
! 17889:
! 17890: /*
! 17891: ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
! 17892: ** intended for use only inside assert() statements. On some platforms,
! 17893: ** there might be race conditions that can cause these routines to
! 17894: ** deliver incorrect results. In particular, if pthread_equal() is
! 17895: ** not an atomic operation, then these routines might delivery
! 17896: ** incorrect results. On most platforms, pthread_equal() is a
! 17897: ** comparison of two integers and is therefore atomic. But we are
! 17898: ** told that HPUX is not such a platform. If so, then these routines
! 17899: ** will not always work correctly on HPUX.
! 17900: **
! 17901: ** On those platforms where pthread_equal() is not atomic, SQLite
! 17902: ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
! 17903: ** make sure no assert() statements are evaluated and hence these
! 17904: ** routines are never called.
! 17905: */
! 17906: #if !defined(NDEBUG) || defined(SQLITE_DEBUG)
! 17907: static int pthreadMutexHeld(sqlite3_mutex *p){
! 17908: return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
! 17909: }
! 17910: static int pthreadMutexNotheld(sqlite3_mutex *p){
! 17911: return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
! 17912: }
! 17913: #endif
! 17914:
! 17915: /*
! 17916: ** Initialize and deinitialize the mutex subsystem.
! 17917: */
! 17918: static int pthreadMutexInit(void){ return SQLITE_OK; }
! 17919: static int pthreadMutexEnd(void){ return SQLITE_OK; }
! 17920:
! 17921: /*
! 17922: ** The sqlite3_mutex_alloc() routine allocates a new
! 17923: ** mutex and returns a pointer to it. If it returns NULL
! 17924: ** that means that a mutex could not be allocated. SQLite
! 17925: ** will unwind its stack and return an error. The argument
! 17926: ** to sqlite3_mutex_alloc() is one of these integer constants:
! 17927: **
! 17928: ** <ul>
! 17929: ** <li> SQLITE_MUTEX_FAST
! 17930: ** <li> SQLITE_MUTEX_RECURSIVE
! 17931: ** <li> SQLITE_MUTEX_STATIC_MASTER
! 17932: ** <li> SQLITE_MUTEX_STATIC_MEM
! 17933: ** <li> SQLITE_MUTEX_STATIC_MEM2
! 17934: ** <li> SQLITE_MUTEX_STATIC_PRNG
! 17935: ** <li> SQLITE_MUTEX_STATIC_LRU
! 17936: ** <li> SQLITE_MUTEX_STATIC_PMEM
! 17937: ** </ul>
! 17938: **
! 17939: ** The first two constants cause sqlite3_mutex_alloc() to create
! 17940: ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
! 17941: ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
! 17942: ** The mutex implementation does not need to make a distinction
! 17943: ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
! 17944: ** not want to. But SQLite will only request a recursive mutex in
! 17945: ** cases where it really needs one. If a faster non-recursive mutex
! 17946: ** implementation is available on the host platform, the mutex subsystem
! 17947: ** might return such a mutex in response to SQLITE_MUTEX_FAST.
! 17948: **
! 17949: ** The other allowed parameters to sqlite3_mutex_alloc() each return
! 17950: ** a pointer to a static preexisting mutex. Six static mutexes are
! 17951: ** used by the current version of SQLite. Future versions of SQLite
! 17952: ** may add additional static mutexes. Static mutexes are for internal
! 17953: ** use by SQLite only. Applications that use SQLite mutexes should
! 17954: ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
! 17955: ** SQLITE_MUTEX_RECURSIVE.
! 17956: **
! 17957: ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
! 17958: ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
! 17959: ** returns a different mutex on every call. But for the static
! 17960: ** mutex types, the same mutex is returned on every call that has
! 17961: ** the same type number.
! 17962: */
! 17963: static sqlite3_mutex *pthreadMutexAlloc(int iType){
! 17964: static sqlite3_mutex staticMutexes[] = {
! 17965: SQLITE3_MUTEX_INITIALIZER,
! 17966: SQLITE3_MUTEX_INITIALIZER,
! 17967: SQLITE3_MUTEX_INITIALIZER,
! 17968: SQLITE3_MUTEX_INITIALIZER,
! 17969: SQLITE3_MUTEX_INITIALIZER,
! 17970: SQLITE3_MUTEX_INITIALIZER
! 17971: };
! 17972: sqlite3_mutex *p;
! 17973: switch( iType ){
! 17974: case SQLITE_MUTEX_RECURSIVE: {
! 17975: p = sqlite3MallocZero( sizeof(*p) );
! 17976: if( p ){
! 17977: #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
! 17978: /* If recursive mutexes are not available, we will have to
! 17979: ** build our own. See below. */
! 17980: pthread_mutex_init(&p->mutex, 0);
! 17981: #else
! 17982: /* Use a recursive mutex if it is available */
! 17983: pthread_mutexattr_t recursiveAttr;
! 17984: pthread_mutexattr_init(&recursiveAttr);
! 17985: pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
! 17986: pthread_mutex_init(&p->mutex, &recursiveAttr);
! 17987: pthread_mutexattr_destroy(&recursiveAttr);
! 17988: #endif
! 17989: #if SQLITE_MUTEX_NREF
! 17990: p->id = iType;
! 17991: #endif
! 17992: }
! 17993: break;
! 17994: }
! 17995: case SQLITE_MUTEX_FAST: {
! 17996: p = sqlite3MallocZero( sizeof(*p) );
! 17997: if( p ){
! 17998: #if SQLITE_MUTEX_NREF
! 17999: p->id = iType;
! 18000: #endif
! 18001: pthread_mutex_init(&p->mutex, 0);
! 18002: }
! 18003: break;
! 18004: }
! 18005: default: {
! 18006: assert( iType-2 >= 0 );
! 18007: assert( iType-2 < ArraySize(staticMutexes) );
! 18008: p = &staticMutexes[iType-2];
! 18009: #if SQLITE_MUTEX_NREF
! 18010: p->id = iType;
! 18011: #endif
! 18012: break;
! 18013: }
! 18014: }
! 18015: return p;
! 18016: }
! 18017:
! 18018:
! 18019: /*
! 18020: ** This routine deallocates a previously
! 18021: ** allocated mutex. SQLite is careful to deallocate every
! 18022: ** mutex that it allocates.
! 18023: */
! 18024: static void pthreadMutexFree(sqlite3_mutex *p){
! 18025: assert( p->nRef==0 );
! 18026: assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
! 18027: pthread_mutex_destroy(&p->mutex);
! 18028: sqlite3_free(p);
! 18029: }
! 18030:
! 18031: /*
! 18032: ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
! 18033: ** to enter a mutex. If another thread is already within the mutex,
! 18034: ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
! 18035: ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
! 18036: ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
! 18037: ** be entered multiple times by the same thread. In such cases the,
! 18038: ** mutex must be exited an equal number of times before another thread
! 18039: ** can enter. If the same thread tries to enter any other kind of mutex
! 18040: ** more than once, the behavior is undefined.
! 18041: */
! 18042: static void pthreadMutexEnter(sqlite3_mutex *p){
! 18043: assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
! 18044:
! 18045: #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
! 18046: /* If recursive mutexes are not available, then we have to grow
! 18047: ** our own. This implementation assumes that pthread_equal()
! 18048: ** is atomic - that it cannot be deceived into thinking self
! 18049: ** and p->owner are equal if p->owner changes between two values
! 18050: ** that are not equal to self while the comparison is taking place.
! 18051: ** This implementation also assumes a coherent cache - that
! 18052: ** separate processes cannot read different values from the same
! 18053: ** address at the same time. If either of these two conditions
! 18054: ** are not met, then the mutexes will fail and problems will result.
! 18055: */
! 18056: {
! 18057: pthread_t self = pthread_self();
! 18058: if( p->nRef>0 && pthread_equal(p->owner, self) ){
! 18059: p->nRef++;
! 18060: }else{
! 18061: pthread_mutex_lock(&p->mutex);
! 18062: assert( p->nRef==0 );
! 18063: p->owner = self;
! 18064: p->nRef = 1;
! 18065: }
! 18066: }
! 18067: #else
! 18068: /* Use the built-in recursive mutexes if they are available.
! 18069: */
! 18070: pthread_mutex_lock(&p->mutex);
! 18071: #if SQLITE_MUTEX_NREF
! 18072: assert( p->nRef>0 || p->owner==0 );
! 18073: p->owner = pthread_self();
! 18074: p->nRef++;
! 18075: #endif
! 18076: #endif
! 18077:
! 18078: #ifdef SQLITE_DEBUG
! 18079: if( p->trace ){
! 18080: printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
! 18081: }
! 18082: #endif
! 18083: }
! 18084: static int pthreadMutexTry(sqlite3_mutex *p){
! 18085: int rc;
! 18086: assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
! 18087:
! 18088: #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
! 18089: /* If recursive mutexes are not available, then we have to grow
! 18090: ** our own. This implementation assumes that pthread_equal()
! 18091: ** is atomic - that it cannot be deceived into thinking self
! 18092: ** and p->owner are equal if p->owner changes between two values
! 18093: ** that are not equal to self while the comparison is taking place.
! 18094: ** This implementation also assumes a coherent cache - that
! 18095: ** separate processes cannot read different values from the same
! 18096: ** address at the same time. If either of these two conditions
! 18097: ** are not met, then the mutexes will fail and problems will result.
! 18098: */
! 18099: {
! 18100: pthread_t self = pthread_self();
! 18101: if( p->nRef>0 && pthread_equal(p->owner, self) ){
! 18102: p->nRef++;
! 18103: rc = SQLITE_OK;
! 18104: }else if( pthread_mutex_trylock(&p->mutex)==0 ){
! 18105: assert( p->nRef==0 );
! 18106: p->owner = self;
! 18107: p->nRef = 1;
! 18108: rc = SQLITE_OK;
! 18109: }else{
! 18110: rc = SQLITE_BUSY;
! 18111: }
! 18112: }
! 18113: #else
! 18114: /* Use the built-in recursive mutexes if they are available.
! 18115: */
! 18116: if( pthread_mutex_trylock(&p->mutex)==0 ){
! 18117: #if SQLITE_MUTEX_NREF
! 18118: p->owner = pthread_self();
! 18119: p->nRef++;
! 18120: #endif
! 18121: rc = SQLITE_OK;
! 18122: }else{
! 18123: rc = SQLITE_BUSY;
! 18124: }
! 18125: #endif
! 18126:
! 18127: #ifdef SQLITE_DEBUG
! 18128: if( rc==SQLITE_OK && p->trace ){
! 18129: printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
! 18130: }
! 18131: #endif
! 18132: return rc;
! 18133: }
! 18134:
! 18135: /*
! 18136: ** The sqlite3_mutex_leave() routine exits a mutex that was
! 18137: ** previously entered by the same thread. The behavior
! 18138: ** is undefined if the mutex is not currently entered or
! 18139: ** is not currently allocated. SQLite will never do either.
! 18140: */
! 18141: static void pthreadMutexLeave(sqlite3_mutex *p){
! 18142: assert( pthreadMutexHeld(p) );
! 18143: #if SQLITE_MUTEX_NREF
! 18144: p->nRef--;
! 18145: if( p->nRef==0 ) p->owner = 0;
! 18146: #endif
! 18147: assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
! 18148:
! 18149: #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
! 18150: if( p->nRef==0 ){
! 18151: pthread_mutex_unlock(&p->mutex);
! 18152: }
! 18153: #else
! 18154: pthread_mutex_unlock(&p->mutex);
! 18155: #endif
! 18156:
! 18157: #ifdef SQLITE_DEBUG
! 18158: if( p->trace ){
! 18159: printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
! 18160: }
! 18161: #endif
! 18162: }
! 18163:
! 18164: SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
! 18165: static const sqlite3_mutex_methods sMutex = {
! 18166: pthreadMutexInit,
! 18167: pthreadMutexEnd,
! 18168: pthreadMutexAlloc,
! 18169: pthreadMutexFree,
! 18170: pthreadMutexEnter,
! 18171: pthreadMutexTry,
! 18172: pthreadMutexLeave,
! 18173: #ifdef SQLITE_DEBUG
! 18174: pthreadMutexHeld,
! 18175: pthreadMutexNotheld
! 18176: #else
! 18177: 0,
! 18178: 0
! 18179: #endif
! 18180: };
! 18181:
! 18182: return &sMutex;
! 18183: }
! 18184:
! 18185: #endif /* SQLITE_MUTEX_PTHREADS */
! 18186:
! 18187: /************** End of mutex_unix.c ******************************************/
! 18188: /************** Begin file mutex_w32.c ***************************************/
! 18189: /*
! 18190: ** 2007 August 14
! 18191: **
! 18192: ** The author disclaims copyright to this source code. In place of
! 18193: ** a legal notice, here is a blessing:
! 18194: **
! 18195: ** May you do good and not evil.
! 18196: ** May you find forgiveness for yourself and forgive others.
! 18197: ** May you share freely, never taking more than you give.
! 18198: **
! 18199: *************************************************************************
! 18200: ** This file contains the C functions that implement mutexes for win32
! 18201: */
! 18202:
! 18203: /*
! 18204: ** The code in this file is only used if we are compiling multithreaded
! 18205: ** on a win32 system.
! 18206: */
! 18207: #ifdef SQLITE_MUTEX_W32
! 18208:
! 18209: /*
! 18210: ** Each recursive mutex is an instance of the following structure.
! 18211: */
! 18212: struct sqlite3_mutex {
! 18213: CRITICAL_SECTION mutex; /* Mutex controlling the lock */
! 18214: int id; /* Mutex type */
! 18215: #ifdef SQLITE_DEBUG
! 18216: volatile int nRef; /* Number of enterances */
! 18217: volatile DWORD owner; /* Thread holding this mutex */
! 18218: int trace; /* True to trace changes */
! 18219: #endif
! 18220: };
! 18221: #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
! 18222: #ifdef SQLITE_DEBUG
! 18223: #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
! 18224: #else
! 18225: #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
! 18226: #endif
! 18227:
! 18228: /*
! 18229: ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
! 18230: ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
! 18231: **
! 18232: ** Here is an interesting observation: Win95, Win98, and WinME lack
! 18233: ** the LockFileEx() API. But we can still statically link against that
! 18234: ** API as long as we don't call it win running Win95/98/ME. A call to
! 18235: ** this routine is used to determine if the host is Win95/98/ME or
! 18236: ** WinNT/2K/XP so that we will know whether or not we can safely call
! 18237: ** the LockFileEx() API.
! 18238: **
! 18239: ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
! 18240: ** which is only available if your application was compiled with
! 18241: ** _WIN32_WINNT defined to a value >= 0x0400. Currently, the only
! 18242: ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
! 18243: ** this out as well.
! 18244: */
! 18245: #if 0
! 18246: #if SQLITE_OS_WINCE
! 18247: # define mutexIsNT() (1)
! 18248: #else
! 18249: static int mutexIsNT(void){
! 18250: static int osType = 0;
! 18251: if( osType==0 ){
! 18252: OSVERSIONINFO sInfo;
! 18253: sInfo.dwOSVersionInfoSize = sizeof(sInfo);
! 18254: GetVersionEx(&sInfo);
! 18255: osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
! 18256: }
! 18257: return osType==2;
! 18258: }
! 18259: #endif /* SQLITE_OS_WINCE */
! 18260: #endif
! 18261:
! 18262: #ifdef SQLITE_DEBUG
! 18263: /*
! 18264: ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
! 18265: ** intended for use only inside assert() statements.
! 18266: */
! 18267: static int winMutexHeld(sqlite3_mutex *p){
! 18268: return p->nRef!=0 && p->owner==GetCurrentThreadId();
! 18269: }
! 18270: static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
! 18271: return p->nRef==0 || p->owner!=tid;
! 18272: }
! 18273: static int winMutexNotheld(sqlite3_mutex *p){
! 18274: DWORD tid = GetCurrentThreadId();
! 18275: return winMutexNotheld2(p, tid);
! 18276: }
! 18277: #endif
! 18278:
! 18279:
! 18280: /*
! 18281: ** Initialize and deinitialize the mutex subsystem.
! 18282: */
! 18283: static sqlite3_mutex winMutex_staticMutexes[6] = {
! 18284: SQLITE3_MUTEX_INITIALIZER,
! 18285: SQLITE3_MUTEX_INITIALIZER,
! 18286: SQLITE3_MUTEX_INITIALIZER,
! 18287: SQLITE3_MUTEX_INITIALIZER,
! 18288: SQLITE3_MUTEX_INITIALIZER,
! 18289: SQLITE3_MUTEX_INITIALIZER
! 18290: };
! 18291: static int winMutex_isInit = 0;
! 18292: /* As winMutexInit() and winMutexEnd() are called as part
! 18293: ** of the sqlite3_initialize and sqlite3_shutdown()
! 18294: ** processing, the "interlocked" magic is probably not
! 18295: ** strictly necessary.
! 18296: */
! 18297: static long winMutex_lock = 0;
! 18298:
! 18299: static int winMutexInit(void){
! 18300: /* The first to increment to 1 does actual initialization */
! 18301: if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
! 18302: int i;
! 18303: for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
! 18304: InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
! 18305: }
! 18306: winMutex_isInit = 1;
! 18307: }else{
! 18308: /* Someone else is in the process of initing the static mutexes */
! 18309: while( !winMutex_isInit ){
! 18310: Sleep(1);
! 18311: }
! 18312: }
! 18313: return SQLITE_OK;
! 18314: }
! 18315:
! 18316: static int winMutexEnd(void){
! 18317: /* The first to decrement to 0 does actual shutdown
! 18318: ** (which should be the last to shutdown.) */
! 18319: if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
! 18320: if( winMutex_isInit==1 ){
! 18321: int i;
! 18322: for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
! 18323: DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
! 18324: }
! 18325: winMutex_isInit = 0;
! 18326: }
! 18327: }
! 18328: return SQLITE_OK;
! 18329: }
! 18330:
! 18331: /*
! 18332: ** The sqlite3_mutex_alloc() routine allocates a new
! 18333: ** mutex and returns a pointer to it. If it returns NULL
! 18334: ** that means that a mutex could not be allocated. SQLite
! 18335: ** will unwind its stack and return an error. The argument
! 18336: ** to sqlite3_mutex_alloc() is one of these integer constants:
! 18337: **
! 18338: ** <ul>
! 18339: ** <li> SQLITE_MUTEX_FAST
! 18340: ** <li> SQLITE_MUTEX_RECURSIVE
! 18341: ** <li> SQLITE_MUTEX_STATIC_MASTER
! 18342: ** <li> SQLITE_MUTEX_STATIC_MEM
! 18343: ** <li> SQLITE_MUTEX_STATIC_MEM2
! 18344: ** <li> SQLITE_MUTEX_STATIC_PRNG
! 18345: ** <li> SQLITE_MUTEX_STATIC_LRU
! 18346: ** <li> SQLITE_MUTEX_STATIC_PMEM
! 18347: ** </ul>
! 18348: **
! 18349: ** The first two constants cause sqlite3_mutex_alloc() to create
! 18350: ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
! 18351: ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
! 18352: ** The mutex implementation does not need to make a distinction
! 18353: ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
! 18354: ** not want to. But SQLite will only request a recursive mutex in
! 18355: ** cases where it really needs one. If a faster non-recursive mutex
! 18356: ** implementation is available on the host platform, the mutex subsystem
! 18357: ** might return such a mutex in response to SQLITE_MUTEX_FAST.
! 18358: **
! 18359: ** The other allowed parameters to sqlite3_mutex_alloc() each return
! 18360: ** a pointer to a static preexisting mutex. Six static mutexes are
! 18361: ** used by the current version of SQLite. Future versions of SQLite
! 18362: ** may add additional static mutexes. Static mutexes are for internal
! 18363: ** use by SQLite only. Applications that use SQLite mutexes should
! 18364: ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
! 18365: ** SQLITE_MUTEX_RECURSIVE.
! 18366: **
! 18367: ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
! 18368: ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
! 18369: ** returns a different mutex on every call. But for the static
! 18370: ** mutex types, the same mutex is returned on every call that has
! 18371: ** the same type number.
! 18372: */
! 18373: static sqlite3_mutex *winMutexAlloc(int iType){
! 18374: sqlite3_mutex *p;
! 18375:
! 18376: switch( iType ){
! 18377: case SQLITE_MUTEX_FAST:
! 18378: case SQLITE_MUTEX_RECURSIVE: {
! 18379: p = sqlite3MallocZero( sizeof(*p) );
! 18380: if( p ){
! 18381: #ifdef SQLITE_DEBUG
! 18382: p->id = iType;
! 18383: #endif
! 18384: InitializeCriticalSection(&p->mutex);
! 18385: }
! 18386: break;
! 18387: }
! 18388: default: {
! 18389: assert( winMutex_isInit==1 );
! 18390: assert( iType-2 >= 0 );
! 18391: assert( iType-2 < ArraySize(winMutex_staticMutexes) );
! 18392: p = &winMutex_staticMutexes[iType-2];
! 18393: #ifdef SQLITE_DEBUG
! 18394: p->id = iType;
! 18395: #endif
! 18396: break;
! 18397: }
! 18398: }
! 18399: return p;
! 18400: }
! 18401:
! 18402:
! 18403: /*
! 18404: ** This routine deallocates a previously
! 18405: ** allocated mutex. SQLite is careful to deallocate every
! 18406: ** mutex that it allocates.
! 18407: */
! 18408: static void winMutexFree(sqlite3_mutex *p){
! 18409: assert( p );
! 18410: assert( p->nRef==0 && p->owner==0 );
! 18411: assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
! 18412: DeleteCriticalSection(&p->mutex);
! 18413: sqlite3_free(p);
! 18414: }
! 18415:
! 18416: /*
! 18417: ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
! 18418: ** to enter a mutex. If another thread is already within the mutex,
! 18419: ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
! 18420: ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
! 18421: ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
! 18422: ** be entered multiple times by the same thread. In such cases the,
! 18423: ** mutex must be exited an equal number of times before another thread
! 18424: ** can enter. If the same thread tries to enter any other kind of mutex
! 18425: ** more than once, the behavior is undefined.
! 18426: */
! 18427: static void winMutexEnter(sqlite3_mutex *p){
! 18428: #ifdef SQLITE_DEBUG
! 18429: DWORD tid = GetCurrentThreadId();
! 18430: assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
! 18431: #endif
! 18432: EnterCriticalSection(&p->mutex);
! 18433: #ifdef SQLITE_DEBUG
! 18434: assert( p->nRef>0 || p->owner==0 );
! 18435: p->owner = tid;
! 18436: p->nRef++;
! 18437: if( p->trace ){
! 18438: printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
! 18439: }
! 18440: #endif
! 18441: }
! 18442: static int winMutexTry(sqlite3_mutex *p){
! 18443: #ifndef NDEBUG
! 18444: DWORD tid = GetCurrentThreadId();
! 18445: #endif
! 18446: int rc = SQLITE_BUSY;
! 18447: assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
! 18448: /*
! 18449: ** The sqlite3_mutex_try() routine is very rarely used, and when it
! 18450: ** is used it is merely an optimization. So it is OK for it to always
! 18451: ** fail.
! 18452: **
! 18453: ** The TryEnterCriticalSection() interface is only available on WinNT.
! 18454: ** And some windows compilers complain if you try to use it without
! 18455: ** first doing some #defines that prevent SQLite from building on Win98.
! 18456: ** For that reason, we will omit this optimization for now. See
! 18457: ** ticket #2685.
! 18458: */
! 18459: #if 0
! 18460: if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
! 18461: p->owner = tid;
! 18462: p->nRef++;
! 18463: rc = SQLITE_OK;
! 18464: }
! 18465: #else
! 18466: UNUSED_PARAMETER(p);
! 18467: #endif
! 18468: #ifdef SQLITE_DEBUG
! 18469: if( rc==SQLITE_OK && p->trace ){
! 18470: printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
! 18471: }
! 18472: #endif
! 18473: return rc;
! 18474: }
! 18475:
! 18476: /*
! 18477: ** The sqlite3_mutex_leave() routine exits a mutex that was
! 18478: ** previously entered by the same thread. The behavior
! 18479: ** is undefined if the mutex is not currently entered or
! 18480: ** is not currently allocated. SQLite will never do either.
! 18481: */
! 18482: static void winMutexLeave(sqlite3_mutex *p){
! 18483: #ifndef NDEBUG
! 18484: DWORD tid = GetCurrentThreadId();
! 18485: assert( p->nRef>0 );
! 18486: assert( p->owner==tid );
! 18487: p->nRef--;
! 18488: if( p->nRef==0 ) p->owner = 0;
! 18489: assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
! 18490: #endif
! 18491: LeaveCriticalSection(&p->mutex);
! 18492: #ifdef SQLITE_DEBUG
! 18493: if( p->trace ){
! 18494: printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
! 18495: }
! 18496: #endif
! 18497: }
! 18498:
! 18499: SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
! 18500: static const sqlite3_mutex_methods sMutex = {
! 18501: winMutexInit,
! 18502: winMutexEnd,
! 18503: winMutexAlloc,
! 18504: winMutexFree,
! 18505: winMutexEnter,
! 18506: winMutexTry,
! 18507: winMutexLeave,
! 18508: #ifdef SQLITE_DEBUG
! 18509: winMutexHeld,
! 18510: winMutexNotheld
! 18511: #else
! 18512: 0,
! 18513: 0
! 18514: #endif
! 18515: };
! 18516:
! 18517: return &sMutex;
! 18518: }
! 18519: #endif /* SQLITE_MUTEX_W32 */
! 18520:
! 18521: /************** End of mutex_w32.c *******************************************/
! 18522: /************** Begin file malloc.c ******************************************/
! 18523: /*
! 18524: ** 2001 September 15
! 18525: **
! 18526: ** The author disclaims copyright to this source code. In place of
! 18527: ** a legal notice, here is a blessing:
! 18528: **
! 18529: ** May you do good and not evil.
! 18530: ** May you find forgiveness for yourself and forgive others.
! 18531: ** May you share freely, never taking more than you give.
! 18532: **
! 18533: *************************************************************************
! 18534: **
! 18535: ** Memory allocation functions used throughout sqlite.
! 18536: */
! 18537: /* #include <stdarg.h> */
! 18538:
! 18539: /*
! 18540: ** Attempt to release up to n bytes of non-essential memory currently
! 18541: ** held by SQLite. An example of non-essential memory is memory used to
! 18542: ** cache database pages that are not currently in use.
! 18543: */
! 18544: SQLITE_API int sqlite3_release_memory(int n){
! 18545: #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
! 18546: return sqlite3PcacheReleaseMemory(n);
! 18547: #else
! 18548: /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
! 18549: ** is a no-op returning zero if SQLite is not compiled with
! 18550: ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
! 18551: UNUSED_PARAMETER(n);
! 18552: return 0;
! 18553: #endif
! 18554: }
! 18555:
! 18556: /*
! 18557: ** An instance of the following object records the location of
! 18558: ** each unused scratch buffer.
! 18559: */
! 18560: typedef struct ScratchFreeslot {
! 18561: struct ScratchFreeslot *pNext; /* Next unused scratch buffer */
! 18562: } ScratchFreeslot;
! 18563:
! 18564: /*
! 18565: ** State information local to the memory allocation subsystem.
! 18566: */
! 18567: static SQLITE_WSD struct Mem0Global {
! 18568: sqlite3_mutex *mutex; /* Mutex to serialize access */
! 18569:
! 18570: /*
! 18571: ** The alarm callback and its arguments. The mem0.mutex lock will
! 18572: ** be held while the callback is running. Recursive calls into
! 18573: ** the memory subsystem are allowed, but no new callbacks will be
! 18574: ** issued.
! 18575: */
! 18576: sqlite3_int64 alarmThreshold;
! 18577: void (*alarmCallback)(void*, sqlite3_int64,int);
! 18578: void *alarmArg;
! 18579:
! 18580: /*
! 18581: ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
! 18582: ** (so that a range test can be used to determine if an allocation
! 18583: ** being freed came from pScratch) and a pointer to the list of
! 18584: ** unused scratch allocations.
! 18585: */
! 18586: void *pScratchEnd;
! 18587: ScratchFreeslot *pScratchFree;
! 18588: u32 nScratchFree;
! 18589:
! 18590: /*
! 18591: ** True if heap is nearly "full" where "full" is defined by the
! 18592: ** sqlite3_soft_heap_limit() setting.
! 18593: */
! 18594: int nearlyFull;
! 18595: } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
! 18596:
! 18597: #define mem0 GLOBAL(struct Mem0Global, mem0)
! 18598:
! 18599: /*
! 18600: ** This routine runs when the memory allocator sees that the
! 18601: ** total memory allocation is about to exceed the soft heap
! 18602: ** limit.
! 18603: */
! 18604: static void softHeapLimitEnforcer(
! 18605: void *NotUsed,
! 18606: sqlite3_int64 NotUsed2,
! 18607: int allocSize
! 18608: ){
! 18609: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 18610: sqlite3_release_memory(allocSize);
! 18611: }
! 18612:
! 18613: /*
! 18614: ** Change the alarm callback
! 18615: */
! 18616: static int sqlite3MemoryAlarm(
! 18617: void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
! 18618: void *pArg,
! 18619: sqlite3_int64 iThreshold
! 18620: ){
! 18621: int nUsed;
! 18622: sqlite3_mutex_enter(mem0.mutex);
! 18623: mem0.alarmCallback = xCallback;
! 18624: mem0.alarmArg = pArg;
! 18625: mem0.alarmThreshold = iThreshold;
! 18626: nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
! 18627: mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
! 18628: sqlite3_mutex_leave(mem0.mutex);
! 18629: return SQLITE_OK;
! 18630: }
! 18631:
! 18632: #ifndef SQLITE_OMIT_DEPRECATED
! 18633: /*
! 18634: ** Deprecated external interface. Internal/core SQLite code
! 18635: ** should call sqlite3MemoryAlarm.
! 18636: */
! 18637: SQLITE_API int sqlite3_memory_alarm(
! 18638: void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
! 18639: void *pArg,
! 18640: sqlite3_int64 iThreshold
! 18641: ){
! 18642: return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
! 18643: }
! 18644: #endif
! 18645:
! 18646: /*
! 18647: ** Set the soft heap-size limit for the library. Passing a zero or
! 18648: ** negative value indicates no limit.
! 18649: */
! 18650: SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
! 18651: sqlite3_int64 priorLimit;
! 18652: sqlite3_int64 excess;
! 18653: #ifndef SQLITE_OMIT_AUTOINIT
! 18654: int rc = sqlite3_initialize();
! 18655: if( rc ) return -1;
! 18656: #endif
! 18657: sqlite3_mutex_enter(mem0.mutex);
! 18658: priorLimit = mem0.alarmThreshold;
! 18659: sqlite3_mutex_leave(mem0.mutex);
! 18660: if( n<0 ) return priorLimit;
! 18661: if( n>0 ){
! 18662: sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
! 18663: }else{
! 18664: sqlite3MemoryAlarm(0, 0, 0);
! 18665: }
! 18666: excess = sqlite3_memory_used() - n;
! 18667: if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
! 18668: return priorLimit;
! 18669: }
! 18670: SQLITE_API void sqlite3_soft_heap_limit(int n){
! 18671: if( n<0 ) n = 0;
! 18672: sqlite3_soft_heap_limit64(n);
! 18673: }
! 18674:
! 18675: /*
! 18676: ** Initialize the memory allocation subsystem.
! 18677: */
! 18678: SQLITE_PRIVATE int sqlite3MallocInit(void){
! 18679: if( sqlite3GlobalConfig.m.xMalloc==0 ){
! 18680: sqlite3MemSetDefault();
! 18681: }
! 18682: memset(&mem0, 0, sizeof(mem0));
! 18683: if( sqlite3GlobalConfig.bCoreMutex ){
! 18684: mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
! 18685: }
! 18686: if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
! 18687: && sqlite3GlobalConfig.nScratch>0 ){
! 18688: int i, n, sz;
! 18689: ScratchFreeslot *pSlot;
! 18690: sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
! 18691: sqlite3GlobalConfig.szScratch = sz;
! 18692: pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
! 18693: n = sqlite3GlobalConfig.nScratch;
! 18694: mem0.pScratchFree = pSlot;
! 18695: mem0.nScratchFree = n;
! 18696: for(i=0; i<n-1; i++){
! 18697: pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
! 18698: pSlot = pSlot->pNext;
! 18699: }
! 18700: pSlot->pNext = 0;
! 18701: mem0.pScratchEnd = (void*)&pSlot[1];
! 18702: }else{
! 18703: mem0.pScratchEnd = 0;
! 18704: sqlite3GlobalConfig.pScratch = 0;
! 18705: sqlite3GlobalConfig.szScratch = 0;
! 18706: sqlite3GlobalConfig.nScratch = 0;
! 18707: }
! 18708: if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
! 18709: || sqlite3GlobalConfig.nPage<1 ){
! 18710: sqlite3GlobalConfig.pPage = 0;
! 18711: sqlite3GlobalConfig.szPage = 0;
! 18712: sqlite3GlobalConfig.nPage = 0;
! 18713: }
! 18714: return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
! 18715: }
! 18716:
! 18717: /*
! 18718: ** Return true if the heap is currently under memory pressure - in other
! 18719: ** words if the amount of heap used is close to the limit set by
! 18720: ** sqlite3_soft_heap_limit().
! 18721: */
! 18722: SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
! 18723: return mem0.nearlyFull;
! 18724: }
! 18725:
! 18726: /*
! 18727: ** Deinitialize the memory allocation subsystem.
! 18728: */
! 18729: SQLITE_PRIVATE void sqlite3MallocEnd(void){
! 18730: if( sqlite3GlobalConfig.m.xShutdown ){
! 18731: sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
! 18732: }
! 18733: memset(&mem0, 0, sizeof(mem0));
! 18734: }
! 18735:
! 18736: /*
! 18737: ** Return the amount of memory currently checked out.
! 18738: */
! 18739: SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
! 18740: int n, mx;
! 18741: sqlite3_int64 res;
! 18742: sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
! 18743: res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
! 18744: return res;
! 18745: }
! 18746:
! 18747: /*
! 18748: ** Return the maximum amount of memory that has ever been
! 18749: ** checked out since either the beginning of this process
! 18750: ** or since the most recent reset.
! 18751: */
! 18752: SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
! 18753: int n, mx;
! 18754: sqlite3_int64 res;
! 18755: sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
! 18756: res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
! 18757: return res;
! 18758: }
! 18759:
! 18760: /*
! 18761: ** Trigger the alarm
! 18762: */
! 18763: static void sqlite3MallocAlarm(int nByte){
! 18764: void (*xCallback)(void*,sqlite3_int64,int);
! 18765: sqlite3_int64 nowUsed;
! 18766: void *pArg;
! 18767: if( mem0.alarmCallback==0 ) return;
! 18768: xCallback = mem0.alarmCallback;
! 18769: nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
! 18770: pArg = mem0.alarmArg;
! 18771: mem0.alarmCallback = 0;
! 18772: sqlite3_mutex_leave(mem0.mutex);
! 18773: xCallback(pArg, nowUsed, nByte);
! 18774: sqlite3_mutex_enter(mem0.mutex);
! 18775: mem0.alarmCallback = xCallback;
! 18776: mem0.alarmArg = pArg;
! 18777: }
! 18778:
! 18779: /*
! 18780: ** Do a memory allocation with statistics and alarms. Assume the
! 18781: ** lock is already held.
! 18782: */
! 18783: static int mallocWithAlarm(int n, void **pp){
! 18784: int nFull;
! 18785: void *p;
! 18786: assert( sqlite3_mutex_held(mem0.mutex) );
! 18787: nFull = sqlite3GlobalConfig.m.xRoundup(n);
! 18788: sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
! 18789: if( mem0.alarmCallback!=0 ){
! 18790: int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
! 18791: if( nUsed >= mem0.alarmThreshold - nFull ){
! 18792: mem0.nearlyFull = 1;
! 18793: sqlite3MallocAlarm(nFull);
! 18794: }else{
! 18795: mem0.nearlyFull = 0;
! 18796: }
! 18797: }
! 18798: p = sqlite3GlobalConfig.m.xMalloc(nFull);
! 18799: #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
! 18800: if( p==0 && mem0.alarmCallback ){
! 18801: sqlite3MallocAlarm(nFull);
! 18802: p = sqlite3GlobalConfig.m.xMalloc(nFull);
! 18803: }
! 18804: #endif
! 18805: if( p ){
! 18806: nFull = sqlite3MallocSize(p);
! 18807: sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
! 18808: sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
! 18809: }
! 18810: *pp = p;
! 18811: return nFull;
! 18812: }
! 18813:
! 18814: /*
! 18815: ** Allocate memory. This routine is like sqlite3_malloc() except that it
! 18816: ** assumes the memory subsystem has already been initialized.
! 18817: */
! 18818: SQLITE_PRIVATE void *sqlite3Malloc(int n){
! 18819: void *p;
! 18820: if( n<=0 /* IMP: R-65312-04917 */
! 18821: || n>=0x7fffff00
! 18822: ){
! 18823: /* A memory allocation of a number of bytes which is near the maximum
! 18824: ** signed integer value might cause an integer overflow inside of the
! 18825: ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
! 18826: ** 255 bytes of overhead. SQLite itself will never use anything near
! 18827: ** this amount. The only way to reach the limit is with sqlite3_malloc() */
! 18828: p = 0;
! 18829: }else if( sqlite3GlobalConfig.bMemstat ){
! 18830: sqlite3_mutex_enter(mem0.mutex);
! 18831: mallocWithAlarm(n, &p);
! 18832: sqlite3_mutex_leave(mem0.mutex);
! 18833: }else{
! 18834: p = sqlite3GlobalConfig.m.xMalloc(n);
! 18835: }
! 18836: assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */
! 18837: return p;
! 18838: }
! 18839:
! 18840: /*
! 18841: ** This version of the memory allocation is for use by the application.
! 18842: ** First make sure the memory subsystem is initialized, then do the
! 18843: ** allocation.
! 18844: */
! 18845: SQLITE_API void *sqlite3_malloc(int n){
! 18846: #ifndef SQLITE_OMIT_AUTOINIT
! 18847: if( sqlite3_initialize() ) return 0;
! 18848: #endif
! 18849: return sqlite3Malloc(n);
! 18850: }
! 18851:
! 18852: /*
! 18853: ** Each thread may only have a single outstanding allocation from
! 18854: ** xScratchMalloc(). We verify this constraint in the single-threaded
! 18855: ** case by setting scratchAllocOut to 1 when an allocation
! 18856: ** is outstanding clearing it when the allocation is freed.
! 18857: */
! 18858: #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
! 18859: static int scratchAllocOut = 0;
! 18860: #endif
! 18861:
! 18862:
! 18863: /*
! 18864: ** Allocate memory that is to be used and released right away.
! 18865: ** This routine is similar to alloca() in that it is not intended
! 18866: ** for situations where the memory might be held long-term. This
! 18867: ** routine is intended to get memory to old large transient data
! 18868: ** structures that would not normally fit on the stack of an
! 18869: ** embedded processor.
! 18870: */
! 18871: SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
! 18872: void *p;
! 18873: assert( n>0 );
! 18874:
! 18875: sqlite3_mutex_enter(mem0.mutex);
! 18876: if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
! 18877: p = mem0.pScratchFree;
! 18878: mem0.pScratchFree = mem0.pScratchFree->pNext;
! 18879: mem0.nScratchFree--;
! 18880: sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
! 18881: sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
! 18882: sqlite3_mutex_leave(mem0.mutex);
! 18883: }else{
! 18884: if( sqlite3GlobalConfig.bMemstat ){
! 18885: sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
! 18886: n = mallocWithAlarm(n, &p);
! 18887: if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
! 18888: sqlite3_mutex_leave(mem0.mutex);
! 18889: }else{
! 18890: sqlite3_mutex_leave(mem0.mutex);
! 18891: p = sqlite3GlobalConfig.m.xMalloc(n);
! 18892: }
! 18893: sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
! 18894: }
! 18895: assert( sqlite3_mutex_notheld(mem0.mutex) );
! 18896:
! 18897:
! 18898: #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
! 18899: /* Verify that no more than two scratch allocations per thread
! 18900: ** are outstanding at one time. (This is only checked in the
! 18901: ** single-threaded case since checking in the multi-threaded case
! 18902: ** would be much more complicated.) */
! 18903: assert( scratchAllocOut<=1 );
! 18904: if( p ) scratchAllocOut++;
! 18905: #endif
! 18906:
! 18907: return p;
! 18908: }
! 18909: SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
! 18910: if( p ){
! 18911:
! 18912: #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
! 18913: /* Verify that no more than two scratch allocation per thread
! 18914: ** is outstanding at one time. (This is only checked in the
! 18915: ** single-threaded case since checking in the multi-threaded case
! 18916: ** would be much more complicated.) */
! 18917: assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
! 18918: scratchAllocOut--;
! 18919: #endif
! 18920:
! 18921: if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
! 18922: /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
! 18923: ScratchFreeslot *pSlot;
! 18924: pSlot = (ScratchFreeslot*)p;
! 18925: sqlite3_mutex_enter(mem0.mutex);
! 18926: pSlot->pNext = mem0.pScratchFree;
! 18927: mem0.pScratchFree = pSlot;
! 18928: mem0.nScratchFree++;
! 18929: assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
! 18930: sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
! 18931: sqlite3_mutex_leave(mem0.mutex);
! 18932: }else{
! 18933: /* Release memory back to the heap */
! 18934: assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
! 18935: assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
! 18936: sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
! 18937: if( sqlite3GlobalConfig.bMemstat ){
! 18938: int iSize = sqlite3MallocSize(p);
! 18939: sqlite3_mutex_enter(mem0.mutex);
! 18940: sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
! 18941: sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
! 18942: sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
! 18943: sqlite3GlobalConfig.m.xFree(p);
! 18944: sqlite3_mutex_leave(mem0.mutex);
! 18945: }else{
! 18946: sqlite3GlobalConfig.m.xFree(p);
! 18947: }
! 18948: }
! 18949: }
! 18950: }
! 18951:
! 18952: /*
! 18953: ** TRUE if p is a lookaside memory allocation from db
! 18954: */
! 18955: #ifndef SQLITE_OMIT_LOOKASIDE
! 18956: static int isLookaside(sqlite3 *db, void *p){
! 18957: return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
! 18958: }
! 18959: #else
! 18960: #define isLookaside(A,B) 0
! 18961: #endif
! 18962:
! 18963: /*
! 18964: ** Return the size of a memory allocation previously obtained from
! 18965: ** sqlite3Malloc() or sqlite3_malloc().
! 18966: */
! 18967: SQLITE_PRIVATE int sqlite3MallocSize(void *p){
! 18968: assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
! 18969: assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
! 18970: return sqlite3GlobalConfig.m.xSize(p);
! 18971: }
! 18972: SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
! 18973: assert( db==0 || sqlite3_mutex_held(db->mutex) );
! 18974: if( db && isLookaside(db, p) ){
! 18975: return db->lookaside.sz;
! 18976: }else{
! 18977: assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
! 18978: assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
! 18979: assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
! 18980: return sqlite3GlobalConfig.m.xSize(p);
! 18981: }
! 18982: }
! 18983:
! 18984: /*
! 18985: ** Free memory previously obtained from sqlite3Malloc().
! 18986: */
! 18987: SQLITE_API void sqlite3_free(void *p){
! 18988: if( p==0 ) return; /* IMP: R-49053-54554 */
! 18989: assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
! 18990: assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
! 18991: if( sqlite3GlobalConfig.bMemstat ){
! 18992: sqlite3_mutex_enter(mem0.mutex);
! 18993: sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
! 18994: sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
! 18995: sqlite3GlobalConfig.m.xFree(p);
! 18996: sqlite3_mutex_leave(mem0.mutex);
! 18997: }else{
! 18998: sqlite3GlobalConfig.m.xFree(p);
! 18999: }
! 19000: }
! 19001:
! 19002: /*
! 19003: ** Free memory that might be associated with a particular database
! 19004: ** connection.
! 19005: */
! 19006: SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
! 19007: assert( db==0 || sqlite3_mutex_held(db->mutex) );
! 19008: if( db ){
! 19009: if( db->pnBytesFreed ){
! 19010: *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
! 19011: return;
! 19012: }
! 19013: if( isLookaside(db, p) ){
! 19014: LookasideSlot *pBuf = (LookasideSlot*)p;
! 19015: pBuf->pNext = db->lookaside.pFree;
! 19016: db->lookaside.pFree = pBuf;
! 19017: db->lookaside.nOut--;
! 19018: return;
! 19019: }
! 19020: }
! 19021: assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
! 19022: assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
! 19023: assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
! 19024: sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
! 19025: sqlite3_free(p);
! 19026: }
! 19027:
! 19028: /*
! 19029: ** Change the size of an existing memory allocation
! 19030: */
! 19031: SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){
! 19032: int nOld, nNew, nDiff;
! 19033: void *pNew;
! 19034: if( pOld==0 ){
! 19035: return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
! 19036: }
! 19037: if( nBytes<=0 ){
! 19038: sqlite3_free(pOld); /* IMP: R-31593-10574 */
! 19039: return 0;
! 19040: }
! 19041: if( nBytes>=0x7fffff00 ){
! 19042: /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
! 19043: return 0;
! 19044: }
! 19045: nOld = sqlite3MallocSize(pOld);
! 19046: /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
! 19047: ** argument to xRealloc is always a value returned by a prior call to
! 19048: ** xRoundup. */
! 19049: nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
! 19050: if( nOld==nNew ){
! 19051: pNew = pOld;
! 19052: }else if( sqlite3GlobalConfig.bMemstat ){
! 19053: sqlite3_mutex_enter(mem0.mutex);
! 19054: sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
! 19055: nDiff = nNew - nOld;
! 19056: if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
! 19057: mem0.alarmThreshold-nDiff ){
! 19058: sqlite3MallocAlarm(nDiff);
! 19059: }
! 19060: assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
! 19061: assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
! 19062: pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
! 19063: if( pNew==0 && mem0.alarmCallback ){
! 19064: sqlite3MallocAlarm(nBytes);
! 19065: pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
! 19066: }
! 19067: if( pNew ){
! 19068: nNew = sqlite3MallocSize(pNew);
! 19069: sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
! 19070: }
! 19071: sqlite3_mutex_leave(mem0.mutex);
! 19072: }else{
! 19073: pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
! 19074: }
! 19075: assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
! 19076: return pNew;
! 19077: }
! 19078:
! 19079: /*
! 19080: ** The public interface to sqlite3Realloc. Make sure that the memory
! 19081: ** subsystem is initialized prior to invoking sqliteRealloc.
! 19082: */
! 19083: SQLITE_API void *sqlite3_realloc(void *pOld, int n){
! 19084: #ifndef SQLITE_OMIT_AUTOINIT
! 19085: if( sqlite3_initialize() ) return 0;
! 19086: #endif
! 19087: return sqlite3Realloc(pOld, n);
! 19088: }
! 19089:
! 19090:
! 19091: /*
! 19092: ** Allocate and zero memory.
! 19093: */
! 19094: SQLITE_PRIVATE void *sqlite3MallocZero(int n){
! 19095: void *p = sqlite3Malloc(n);
! 19096: if( p ){
! 19097: memset(p, 0, n);
! 19098: }
! 19099: return p;
! 19100: }
! 19101:
! 19102: /*
! 19103: ** Allocate and zero memory. If the allocation fails, make
! 19104: ** the mallocFailed flag in the connection pointer.
! 19105: */
! 19106: SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, int n){
! 19107: void *p = sqlite3DbMallocRaw(db, n);
! 19108: if( p ){
! 19109: memset(p, 0, n);
! 19110: }
! 19111: return p;
! 19112: }
! 19113:
! 19114: /*
! 19115: ** Allocate and zero memory. If the allocation fails, make
! 19116: ** the mallocFailed flag in the connection pointer.
! 19117: **
! 19118: ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
! 19119: ** failure on the same database connection) then always return 0.
! 19120: ** Hence for a particular database connection, once malloc starts
! 19121: ** failing, it fails consistently until mallocFailed is reset.
! 19122: ** This is an important assumption. There are many places in the
! 19123: ** code that do things like this:
! 19124: **
! 19125: ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
! 19126: ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
! 19127: ** if( b ) a[10] = 9;
! 19128: **
! 19129: ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
! 19130: ** that all prior mallocs (ex: "a") worked too.
! 19131: */
! 19132: SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){
! 19133: void *p;
! 19134: assert( db==0 || sqlite3_mutex_held(db->mutex) );
! 19135: assert( db==0 || db->pnBytesFreed==0 );
! 19136: #ifndef SQLITE_OMIT_LOOKASIDE
! 19137: if( db ){
! 19138: LookasideSlot *pBuf;
! 19139: if( db->mallocFailed ){
! 19140: return 0;
! 19141: }
! 19142: if( db->lookaside.bEnabled ){
! 19143: if( n>db->lookaside.sz ){
! 19144: db->lookaside.anStat[1]++;
! 19145: }else if( (pBuf = db->lookaside.pFree)==0 ){
! 19146: db->lookaside.anStat[2]++;
! 19147: }else{
! 19148: db->lookaside.pFree = pBuf->pNext;
! 19149: db->lookaside.nOut++;
! 19150: db->lookaside.anStat[0]++;
! 19151: if( db->lookaside.nOut>db->lookaside.mxOut ){
! 19152: db->lookaside.mxOut = db->lookaside.nOut;
! 19153: }
! 19154: return (void*)pBuf;
! 19155: }
! 19156: }
! 19157: }
! 19158: #else
! 19159: if( db && db->mallocFailed ){
! 19160: return 0;
! 19161: }
! 19162: #endif
! 19163: p = sqlite3Malloc(n);
! 19164: if( !p && db ){
! 19165: db->mallocFailed = 1;
! 19166: }
! 19167: sqlite3MemdebugSetType(p, MEMTYPE_DB |
! 19168: ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
! 19169: return p;
! 19170: }
! 19171:
! 19172: /*
! 19173: ** Resize the block of memory pointed to by p to n bytes. If the
! 19174: ** resize fails, set the mallocFailed flag in the connection object.
! 19175: */
! 19176: SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
! 19177: void *pNew = 0;
! 19178: assert( db!=0 );
! 19179: assert( sqlite3_mutex_held(db->mutex) );
! 19180: if( db->mallocFailed==0 ){
! 19181: if( p==0 ){
! 19182: return sqlite3DbMallocRaw(db, n);
! 19183: }
! 19184: if( isLookaside(db, p) ){
! 19185: if( n<=db->lookaside.sz ){
! 19186: return p;
! 19187: }
! 19188: pNew = sqlite3DbMallocRaw(db, n);
! 19189: if( pNew ){
! 19190: memcpy(pNew, p, db->lookaside.sz);
! 19191: sqlite3DbFree(db, p);
! 19192: }
! 19193: }else{
! 19194: assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
! 19195: assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
! 19196: sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
! 19197: pNew = sqlite3_realloc(p, n);
! 19198: if( !pNew ){
! 19199: sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
! 19200: db->mallocFailed = 1;
! 19201: }
! 19202: sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
! 19203: (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
! 19204: }
! 19205: }
! 19206: return pNew;
! 19207: }
! 19208:
! 19209: /*
! 19210: ** Attempt to reallocate p. If the reallocation fails, then free p
! 19211: ** and set the mallocFailed flag in the database connection.
! 19212: */
! 19213: SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
! 19214: void *pNew;
! 19215: pNew = sqlite3DbRealloc(db, p, n);
! 19216: if( !pNew ){
! 19217: sqlite3DbFree(db, p);
! 19218: }
! 19219: return pNew;
! 19220: }
! 19221:
! 19222: /*
! 19223: ** Make a copy of a string in memory obtained from sqliteMalloc(). These
! 19224: ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
! 19225: ** is because when memory debugging is turned on, these two functions are
! 19226: ** called via macros that record the current file and line number in the
! 19227: ** ThreadData structure.
! 19228: */
! 19229: SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
! 19230: char *zNew;
! 19231: size_t n;
! 19232: if( z==0 ){
! 19233: return 0;
! 19234: }
! 19235: n = sqlite3Strlen30(z) + 1;
! 19236: assert( (n&0x7fffffff)==n );
! 19237: zNew = sqlite3DbMallocRaw(db, (int)n);
! 19238: if( zNew ){
! 19239: memcpy(zNew, z, n);
! 19240: }
! 19241: return zNew;
! 19242: }
! 19243: SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
! 19244: char *zNew;
! 19245: if( z==0 ){
! 19246: return 0;
! 19247: }
! 19248: assert( (n&0x7fffffff)==n );
! 19249: zNew = sqlite3DbMallocRaw(db, n+1);
! 19250: if( zNew ){
! 19251: memcpy(zNew, z, n);
! 19252: zNew[n] = 0;
! 19253: }
! 19254: return zNew;
! 19255: }
! 19256:
! 19257: /*
! 19258: ** Create a string from the zFromat argument and the va_list that follows.
! 19259: ** Store the string in memory obtained from sqliteMalloc() and make *pz
! 19260: ** point to that string.
! 19261: */
! 19262: SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
! 19263: va_list ap;
! 19264: char *z;
! 19265:
! 19266: va_start(ap, zFormat);
! 19267: z = sqlite3VMPrintf(db, zFormat, ap);
! 19268: va_end(ap);
! 19269: sqlite3DbFree(db, *pz);
! 19270: *pz = z;
! 19271: }
! 19272:
! 19273:
! 19274: /*
! 19275: ** This function must be called before exiting any API function (i.e.
! 19276: ** returning control to the user) that has called sqlite3_malloc or
! 19277: ** sqlite3_realloc.
! 19278: **
! 19279: ** The returned value is normally a copy of the second argument to this
! 19280: ** function. However, if a malloc() failure has occurred since the previous
! 19281: ** invocation SQLITE_NOMEM is returned instead.
! 19282: **
! 19283: ** If the first argument, db, is not NULL and a malloc() error has occurred,
! 19284: ** then the connection error-code (the value returned by sqlite3_errcode())
! 19285: ** is set to SQLITE_NOMEM.
! 19286: */
! 19287: SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
! 19288: /* If the db handle is not NULL, then we must hold the connection handle
! 19289: ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
! 19290: ** is unsafe, as is the call to sqlite3Error().
! 19291: */
! 19292: assert( !db || sqlite3_mutex_held(db->mutex) );
! 19293: if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
! 19294: sqlite3Error(db, SQLITE_NOMEM, 0);
! 19295: db->mallocFailed = 0;
! 19296: rc = SQLITE_NOMEM;
! 19297: }
! 19298: return rc & (db ? db->errMask : 0xff);
! 19299: }
! 19300:
! 19301: /************** End of malloc.c **********************************************/
! 19302: /************** Begin file printf.c ******************************************/
! 19303: /*
! 19304: ** The "printf" code that follows dates from the 1980's. It is in
! 19305: ** the public domain. The original comments are included here for
! 19306: ** completeness. They are very out-of-date but might be useful as
! 19307: ** an historical reference. Most of the "enhancements" have been backed
! 19308: ** out so that the functionality is now the same as standard printf().
! 19309: **
! 19310: **************************************************************************
! 19311: **
! 19312: ** This file contains code for a set of "printf"-like routines. These
! 19313: ** routines format strings much like the printf() from the standard C
! 19314: ** library, though the implementation here has enhancements to support
! 19315: ** SQLlite.
! 19316: */
! 19317:
! 19318: /*
! 19319: ** Conversion types fall into various categories as defined by the
! 19320: ** following enumeration.
! 19321: */
! 19322: #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */
! 19323: #define etFLOAT 2 /* Floating point. %f */
! 19324: #define etEXP 3 /* Exponentional notation. %e and %E */
! 19325: #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */
! 19326: #define etSIZE 5 /* Return number of characters processed so far. %n */
! 19327: #define etSTRING 6 /* Strings. %s */
! 19328: #define etDYNSTRING 7 /* Dynamically allocated strings. %z */
! 19329: #define etPERCENT 8 /* Percent symbol. %% */
! 19330: #define etCHARX 9 /* Characters. %c */
! 19331: /* The rest are extensions, not normally found in printf() */
! 19332: #define etSQLESCAPE 10 /* Strings with '\'' doubled. %q */
! 19333: #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
! 19334: NULL pointers replaced by SQL NULL. %Q */
! 19335: #define etTOKEN 12 /* a pointer to a Token structure */
! 19336: #define etSRCLIST 13 /* a pointer to a SrcList */
! 19337: #define etPOINTER 14 /* The %p conversion */
! 19338: #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
! 19339: #define etORDINAL 16 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
! 19340:
! 19341: #define etINVALID 0 /* Any unrecognized conversion type */
! 19342:
! 19343:
! 19344: /*
! 19345: ** An "etByte" is an 8-bit unsigned value.
! 19346: */
! 19347: typedef unsigned char etByte;
! 19348:
! 19349: /*
! 19350: ** Each builtin conversion character (ex: the 'd' in "%d") is described
! 19351: ** by an instance of the following structure
! 19352: */
! 19353: typedef struct et_info { /* Information about each format field */
! 19354: char fmttype; /* The format field code letter */
! 19355: etByte base; /* The base for radix conversion */
! 19356: etByte flags; /* One or more of FLAG_ constants below */
! 19357: etByte type; /* Conversion paradigm */
! 19358: etByte charset; /* Offset into aDigits[] of the digits string */
! 19359: etByte prefix; /* Offset into aPrefix[] of the prefix string */
! 19360: } et_info;
! 19361:
! 19362: /*
! 19363: ** Allowed values for et_info.flags
! 19364: */
! 19365: #define FLAG_SIGNED 1 /* True if the value to convert is signed */
! 19366: #define FLAG_INTERN 2 /* True if for internal use only */
! 19367: #define FLAG_STRING 4 /* Allow infinity precision */
! 19368:
! 19369:
! 19370: /*
! 19371: ** The following table is searched linearly, so it is good to put the
! 19372: ** most frequently used conversion types first.
! 19373: */
! 19374: static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
! 19375: static const char aPrefix[] = "-x0\000X0";
! 19376: static const et_info fmtinfo[] = {
! 19377: { 'd', 10, 1, etRADIX, 0, 0 },
! 19378: { 's', 0, 4, etSTRING, 0, 0 },
! 19379: { 'g', 0, 1, etGENERIC, 30, 0 },
! 19380: { 'z', 0, 4, etDYNSTRING, 0, 0 },
! 19381: { 'q', 0, 4, etSQLESCAPE, 0, 0 },
! 19382: { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
! 19383: { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
! 19384: { 'c', 0, 0, etCHARX, 0, 0 },
! 19385: { 'o', 8, 0, etRADIX, 0, 2 },
! 19386: { 'u', 10, 0, etRADIX, 0, 0 },
! 19387: { 'x', 16, 0, etRADIX, 16, 1 },
! 19388: { 'X', 16, 0, etRADIX, 0, 4 },
! 19389: #ifndef SQLITE_OMIT_FLOATING_POINT
! 19390: { 'f', 0, 1, etFLOAT, 0, 0 },
! 19391: { 'e', 0, 1, etEXP, 30, 0 },
! 19392: { 'E', 0, 1, etEXP, 14, 0 },
! 19393: { 'G', 0, 1, etGENERIC, 14, 0 },
! 19394: #endif
! 19395: { 'i', 10, 1, etRADIX, 0, 0 },
! 19396: { 'n', 0, 0, etSIZE, 0, 0 },
! 19397: { '%', 0, 0, etPERCENT, 0, 0 },
! 19398: { 'p', 16, 0, etPOINTER, 0, 1 },
! 19399:
! 19400: /* All the rest have the FLAG_INTERN bit set and are thus for internal
! 19401: ** use only */
! 19402: { 'T', 0, 2, etTOKEN, 0, 0 },
! 19403: { 'S', 0, 2, etSRCLIST, 0, 0 },
! 19404: { 'r', 10, 3, etORDINAL, 0, 0 },
! 19405: };
! 19406:
! 19407: /*
! 19408: ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
! 19409: ** conversions will work.
! 19410: */
! 19411: #ifndef SQLITE_OMIT_FLOATING_POINT
! 19412: /*
! 19413: ** "*val" is a double such that 0.1 <= *val < 10.0
! 19414: ** Return the ascii code for the leading digit of *val, then
! 19415: ** multiply "*val" by 10.0 to renormalize.
! 19416: **
! 19417: ** Example:
! 19418: ** input: *val = 3.14159
! 19419: ** output: *val = 1.4159 function return = '3'
! 19420: **
! 19421: ** The counter *cnt is incremented each time. After counter exceeds
! 19422: ** 16 (the number of significant digits in a 64-bit float) '0' is
! 19423: ** always returned.
! 19424: */
! 19425: static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
! 19426: int digit;
! 19427: LONGDOUBLE_TYPE d;
! 19428: if( (*cnt)++ >= 16 ) return '0';
! 19429: digit = (int)*val;
! 19430: d = digit;
! 19431: digit += '0';
! 19432: *val = (*val - d)*10.0;
! 19433: return (char)digit;
! 19434: }
! 19435: #endif /* SQLITE_OMIT_FLOATING_POINT */
! 19436:
! 19437: /*
! 19438: ** Append N space characters to the given string buffer.
! 19439: */
! 19440: SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *pAccum, int N){
! 19441: static const char zSpaces[] = " ";
! 19442: while( N>=(int)sizeof(zSpaces)-1 ){
! 19443: sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
! 19444: N -= sizeof(zSpaces)-1;
! 19445: }
! 19446: if( N>0 ){
! 19447: sqlite3StrAccumAppend(pAccum, zSpaces, N);
! 19448: }
! 19449: }
! 19450:
! 19451: /*
! 19452: ** On machines with a small stack size, you can redefine the
! 19453: ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
! 19454: */
! 19455: #ifndef SQLITE_PRINT_BUF_SIZE
! 19456: # define SQLITE_PRINT_BUF_SIZE 70
! 19457: #endif
! 19458: #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
! 19459:
! 19460: /*
! 19461: ** Render a string given by "fmt" into the StrAccum object.
! 19462: */
! 19463: SQLITE_PRIVATE void sqlite3VXPrintf(
! 19464: StrAccum *pAccum, /* Accumulate results here */
! 19465: int useExtended, /* Allow extended %-conversions */
! 19466: const char *fmt, /* Format string */
! 19467: va_list ap /* arguments */
! 19468: ){
! 19469: int c; /* Next character in the format string */
! 19470: char *bufpt; /* Pointer to the conversion buffer */
! 19471: int precision; /* Precision of the current field */
! 19472: int length; /* Length of the field */
! 19473: int idx; /* A general purpose loop counter */
! 19474: int width; /* Width of the current field */
! 19475: etByte flag_leftjustify; /* True if "-" flag is present */
! 19476: etByte flag_plussign; /* True if "+" flag is present */
! 19477: etByte flag_blanksign; /* True if " " flag is present */
! 19478: etByte flag_alternateform; /* True if "#" flag is present */
! 19479: etByte flag_altform2; /* True if "!" flag is present */
! 19480: etByte flag_zeropad; /* True if field width constant starts with zero */
! 19481: etByte flag_long; /* True if "l" flag is present */
! 19482: etByte flag_longlong; /* True if the "ll" flag is present */
! 19483: etByte done; /* Loop termination flag */
! 19484: etByte xtype = 0; /* Conversion paradigm */
! 19485: char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
! 19486: sqlite_uint64 longvalue; /* Value for integer types */
! 19487: LONGDOUBLE_TYPE realvalue; /* Value for real types */
! 19488: const et_info *infop; /* Pointer to the appropriate info structure */
! 19489: char *zOut; /* Rendering buffer */
! 19490: int nOut; /* Size of the rendering buffer */
! 19491: char *zExtra; /* Malloced memory used by some conversion */
! 19492: #ifndef SQLITE_OMIT_FLOATING_POINT
! 19493: int exp, e2; /* exponent of real numbers */
! 19494: int nsd; /* Number of significant digits returned */
! 19495: double rounder; /* Used for rounding floating point values */
! 19496: etByte flag_dp; /* True if decimal point should be shown */
! 19497: etByte flag_rtz; /* True if trailing zeros should be removed */
! 19498: #endif
! 19499: char buf[etBUFSIZE]; /* Conversion buffer */
! 19500:
! 19501: bufpt = 0;
! 19502: for(; (c=(*fmt))!=0; ++fmt){
! 19503: if( c!='%' ){
! 19504: int amt;
! 19505: bufpt = (char *)fmt;
! 19506: amt = 1;
! 19507: while( (c=(*++fmt))!='%' && c!=0 ) amt++;
! 19508: sqlite3StrAccumAppend(pAccum, bufpt, amt);
! 19509: if( c==0 ) break;
! 19510: }
! 19511: if( (c=(*++fmt))==0 ){
! 19512: sqlite3StrAccumAppend(pAccum, "%", 1);
! 19513: break;
! 19514: }
! 19515: /* Find out what flags are present */
! 19516: flag_leftjustify = flag_plussign = flag_blanksign =
! 19517: flag_alternateform = flag_altform2 = flag_zeropad = 0;
! 19518: done = 0;
! 19519: do{
! 19520: switch( c ){
! 19521: case '-': flag_leftjustify = 1; break;
! 19522: case '+': flag_plussign = 1; break;
! 19523: case ' ': flag_blanksign = 1; break;
! 19524: case '#': flag_alternateform = 1; break;
! 19525: case '!': flag_altform2 = 1; break;
! 19526: case '0': flag_zeropad = 1; break;
! 19527: default: done = 1; break;
! 19528: }
! 19529: }while( !done && (c=(*++fmt))!=0 );
! 19530: /* Get the field width */
! 19531: width = 0;
! 19532: if( c=='*' ){
! 19533: width = va_arg(ap,int);
! 19534: if( width<0 ){
! 19535: flag_leftjustify = 1;
! 19536: width = -width;
! 19537: }
! 19538: c = *++fmt;
! 19539: }else{
! 19540: while( c>='0' && c<='9' ){
! 19541: width = width*10 + c - '0';
! 19542: c = *++fmt;
! 19543: }
! 19544: }
! 19545: /* Get the precision */
! 19546: if( c=='.' ){
! 19547: precision = 0;
! 19548: c = *++fmt;
! 19549: if( c=='*' ){
! 19550: precision = va_arg(ap,int);
! 19551: if( precision<0 ) precision = -precision;
! 19552: c = *++fmt;
! 19553: }else{
! 19554: while( c>='0' && c<='9' ){
! 19555: precision = precision*10 + c - '0';
! 19556: c = *++fmt;
! 19557: }
! 19558: }
! 19559: }else{
! 19560: precision = -1;
! 19561: }
! 19562: /* Get the conversion type modifier */
! 19563: if( c=='l' ){
! 19564: flag_long = 1;
! 19565: c = *++fmt;
! 19566: if( c=='l' ){
! 19567: flag_longlong = 1;
! 19568: c = *++fmt;
! 19569: }else{
! 19570: flag_longlong = 0;
! 19571: }
! 19572: }else{
! 19573: flag_long = flag_longlong = 0;
! 19574: }
! 19575: /* Fetch the info entry for the field */
! 19576: infop = &fmtinfo[0];
! 19577: xtype = etINVALID;
! 19578: for(idx=0; idx<ArraySize(fmtinfo); idx++){
! 19579: if( c==fmtinfo[idx].fmttype ){
! 19580: infop = &fmtinfo[idx];
! 19581: if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
! 19582: xtype = infop->type;
! 19583: }else{
! 19584: return;
! 19585: }
! 19586: break;
! 19587: }
! 19588: }
! 19589: zExtra = 0;
! 19590:
! 19591: /*
! 19592: ** At this point, variables are initialized as follows:
! 19593: **
! 19594: ** flag_alternateform TRUE if a '#' is present.
! 19595: ** flag_altform2 TRUE if a '!' is present.
! 19596: ** flag_plussign TRUE if a '+' is present.
! 19597: ** flag_leftjustify TRUE if a '-' is present or if the
! 19598: ** field width was negative.
! 19599: ** flag_zeropad TRUE if the width began with 0.
! 19600: ** flag_long TRUE if the letter 'l' (ell) prefixed
! 19601: ** the conversion character.
! 19602: ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
! 19603: ** the conversion character.
! 19604: ** flag_blanksign TRUE if a ' ' is present.
! 19605: ** width The specified field width. This is
! 19606: ** always non-negative. Zero is the default.
! 19607: ** precision The specified precision. The default
! 19608: ** is -1.
! 19609: ** xtype The class of the conversion.
! 19610: ** infop Pointer to the appropriate info struct.
! 19611: */
! 19612: switch( xtype ){
! 19613: case etPOINTER:
! 19614: flag_longlong = sizeof(char*)==sizeof(i64);
! 19615: flag_long = sizeof(char*)==sizeof(long int);
! 19616: /* Fall through into the next case */
! 19617: case etORDINAL:
! 19618: case etRADIX:
! 19619: if( infop->flags & FLAG_SIGNED ){
! 19620: i64 v;
! 19621: if( flag_longlong ){
! 19622: v = va_arg(ap,i64);
! 19623: }else if( flag_long ){
! 19624: v = va_arg(ap,long int);
! 19625: }else{
! 19626: v = va_arg(ap,int);
! 19627: }
! 19628: if( v<0 ){
! 19629: if( v==SMALLEST_INT64 ){
! 19630: longvalue = ((u64)1)<<63;
! 19631: }else{
! 19632: longvalue = -v;
! 19633: }
! 19634: prefix = '-';
! 19635: }else{
! 19636: longvalue = v;
! 19637: if( flag_plussign ) prefix = '+';
! 19638: else if( flag_blanksign ) prefix = ' ';
! 19639: else prefix = 0;
! 19640: }
! 19641: }else{
! 19642: if( flag_longlong ){
! 19643: longvalue = va_arg(ap,u64);
! 19644: }else if( flag_long ){
! 19645: longvalue = va_arg(ap,unsigned long int);
! 19646: }else{
! 19647: longvalue = va_arg(ap,unsigned int);
! 19648: }
! 19649: prefix = 0;
! 19650: }
! 19651: if( longvalue==0 ) flag_alternateform = 0;
! 19652: if( flag_zeropad && precision<width-(prefix!=0) ){
! 19653: precision = width-(prefix!=0);
! 19654: }
! 19655: if( precision<etBUFSIZE-10 ){
! 19656: nOut = etBUFSIZE;
! 19657: zOut = buf;
! 19658: }else{
! 19659: nOut = precision + 10;
! 19660: zOut = zExtra = sqlite3Malloc( nOut );
! 19661: if( zOut==0 ){
! 19662: pAccum->mallocFailed = 1;
! 19663: return;
! 19664: }
! 19665: }
! 19666: bufpt = &zOut[nOut-1];
! 19667: if( xtype==etORDINAL ){
! 19668: static const char zOrd[] = "thstndrd";
! 19669: int x = (int)(longvalue % 10);
! 19670: if( x>=4 || (longvalue/10)%10==1 ){
! 19671: x = 0;
! 19672: }
! 19673: *(--bufpt) = zOrd[x*2+1];
! 19674: *(--bufpt) = zOrd[x*2];
! 19675: }
! 19676: {
! 19677: register const char *cset; /* Use registers for speed */
! 19678: register int base;
! 19679: cset = &aDigits[infop->charset];
! 19680: base = infop->base;
! 19681: do{ /* Convert to ascii */
! 19682: *(--bufpt) = cset[longvalue%base];
! 19683: longvalue = longvalue/base;
! 19684: }while( longvalue>0 );
! 19685: }
! 19686: length = (int)(&zOut[nOut-1]-bufpt);
! 19687: for(idx=precision-length; idx>0; idx--){
! 19688: *(--bufpt) = '0'; /* Zero pad */
! 19689: }
! 19690: if( prefix ) *(--bufpt) = prefix; /* Add sign */
! 19691: if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
! 19692: const char *pre;
! 19693: char x;
! 19694: pre = &aPrefix[infop->prefix];
! 19695: for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
! 19696: }
! 19697: length = (int)(&zOut[nOut-1]-bufpt);
! 19698: break;
! 19699: case etFLOAT:
! 19700: case etEXP:
! 19701: case etGENERIC:
! 19702: realvalue = va_arg(ap,double);
! 19703: #ifdef SQLITE_OMIT_FLOATING_POINT
! 19704: length = 0;
! 19705: #else
! 19706: if( precision<0 ) precision = 6; /* Set default precision */
! 19707: if( realvalue<0.0 ){
! 19708: realvalue = -realvalue;
! 19709: prefix = '-';
! 19710: }else{
! 19711: if( flag_plussign ) prefix = '+';
! 19712: else if( flag_blanksign ) prefix = ' ';
! 19713: else prefix = 0;
! 19714: }
! 19715: if( xtype==etGENERIC && precision>0 ) precision--;
! 19716: #if 0
! 19717: /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
! 19718: for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
! 19719: #else
! 19720: /* It makes more sense to use 0.5 */
! 19721: for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
! 19722: #endif
! 19723: if( xtype==etFLOAT ) realvalue += rounder;
! 19724: /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
! 19725: exp = 0;
! 19726: if( sqlite3IsNaN((double)realvalue) ){
! 19727: bufpt = "NaN";
! 19728: length = 3;
! 19729: break;
! 19730: }
! 19731: if( realvalue>0.0 ){
! 19732: while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
! 19733: while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
! 19734: while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
! 19735: while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
! 19736: while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
! 19737: if( exp>350 ){
! 19738: if( prefix=='-' ){
! 19739: bufpt = "-Inf";
! 19740: }else if( prefix=='+' ){
! 19741: bufpt = "+Inf";
! 19742: }else{
! 19743: bufpt = "Inf";
! 19744: }
! 19745: length = sqlite3Strlen30(bufpt);
! 19746: break;
! 19747: }
! 19748: }
! 19749: bufpt = buf;
! 19750: /*
! 19751: ** If the field type is etGENERIC, then convert to either etEXP
! 19752: ** or etFLOAT, as appropriate.
! 19753: */
! 19754: if( xtype!=etFLOAT ){
! 19755: realvalue += rounder;
! 19756: if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
! 19757: }
! 19758: if( xtype==etGENERIC ){
! 19759: flag_rtz = !flag_alternateform;
! 19760: if( exp<-4 || exp>precision ){
! 19761: xtype = etEXP;
! 19762: }else{
! 19763: precision = precision - exp;
! 19764: xtype = etFLOAT;
! 19765: }
! 19766: }else{
! 19767: flag_rtz = 0;
! 19768: }
! 19769: if( xtype==etEXP ){
! 19770: e2 = 0;
! 19771: }else{
! 19772: e2 = exp;
! 19773: }
! 19774: if( e2+precision+width > etBUFSIZE - 15 ){
! 19775: bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 );
! 19776: if( bufpt==0 ){
! 19777: pAccum->mallocFailed = 1;
! 19778: return;
! 19779: }
! 19780: }
! 19781: zOut = bufpt;
! 19782: nsd = 0;
! 19783: flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
! 19784: /* The sign in front of the number */
! 19785: if( prefix ){
! 19786: *(bufpt++) = prefix;
! 19787: }
! 19788: /* Digits prior to the decimal point */
! 19789: if( e2<0 ){
! 19790: *(bufpt++) = '0';
! 19791: }else{
! 19792: for(; e2>=0; e2--){
! 19793: *(bufpt++) = et_getdigit(&realvalue,&nsd);
! 19794: }
! 19795: }
! 19796: /* The decimal point */
! 19797: if( flag_dp ){
! 19798: *(bufpt++) = '.';
! 19799: }
! 19800: /* "0" digits after the decimal point but before the first
! 19801: ** significant digit of the number */
! 19802: for(e2++; e2<0; precision--, e2++){
! 19803: assert( precision>0 );
! 19804: *(bufpt++) = '0';
! 19805: }
! 19806: /* Significant digits after the decimal point */
! 19807: while( (precision--)>0 ){
! 19808: *(bufpt++) = et_getdigit(&realvalue,&nsd);
! 19809: }
! 19810: /* Remove trailing zeros and the "." if no digits follow the "." */
! 19811: if( flag_rtz && flag_dp ){
! 19812: while( bufpt[-1]=='0' ) *(--bufpt) = 0;
! 19813: assert( bufpt>zOut );
! 19814: if( bufpt[-1]=='.' ){
! 19815: if( flag_altform2 ){
! 19816: *(bufpt++) = '0';
! 19817: }else{
! 19818: *(--bufpt) = 0;
! 19819: }
! 19820: }
! 19821: }
! 19822: /* Add the "eNNN" suffix */
! 19823: if( xtype==etEXP ){
! 19824: *(bufpt++) = aDigits[infop->charset];
! 19825: if( exp<0 ){
! 19826: *(bufpt++) = '-'; exp = -exp;
! 19827: }else{
! 19828: *(bufpt++) = '+';
! 19829: }
! 19830: if( exp>=100 ){
! 19831: *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
! 19832: exp %= 100;
! 19833: }
! 19834: *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
! 19835: *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
! 19836: }
! 19837: *bufpt = 0;
! 19838:
! 19839: /* The converted number is in buf[] and zero terminated. Output it.
! 19840: ** Note that the number is in the usual order, not reversed as with
! 19841: ** integer conversions. */
! 19842: length = (int)(bufpt-zOut);
! 19843: bufpt = zOut;
! 19844:
! 19845: /* Special case: Add leading zeros if the flag_zeropad flag is
! 19846: ** set and we are not left justified */
! 19847: if( flag_zeropad && !flag_leftjustify && length < width){
! 19848: int i;
! 19849: int nPad = width - length;
! 19850: for(i=width; i>=nPad; i--){
! 19851: bufpt[i] = bufpt[i-nPad];
! 19852: }
! 19853: i = prefix!=0;
! 19854: while( nPad-- ) bufpt[i++] = '0';
! 19855: length = width;
! 19856: }
! 19857: #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
! 19858: break;
! 19859: case etSIZE:
! 19860: *(va_arg(ap,int*)) = pAccum->nChar;
! 19861: length = width = 0;
! 19862: break;
! 19863: case etPERCENT:
! 19864: buf[0] = '%';
! 19865: bufpt = buf;
! 19866: length = 1;
! 19867: break;
! 19868: case etCHARX:
! 19869: c = va_arg(ap,int);
! 19870: buf[0] = (char)c;
! 19871: if( precision>=0 ){
! 19872: for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
! 19873: length = precision;
! 19874: }else{
! 19875: length =1;
! 19876: }
! 19877: bufpt = buf;
! 19878: break;
! 19879: case etSTRING:
! 19880: case etDYNSTRING:
! 19881: bufpt = va_arg(ap,char*);
! 19882: if( bufpt==0 ){
! 19883: bufpt = "";
! 19884: }else if( xtype==etDYNSTRING ){
! 19885: zExtra = bufpt;
! 19886: }
! 19887: if( precision>=0 ){
! 19888: for(length=0; length<precision && bufpt[length]; length++){}
! 19889: }else{
! 19890: length = sqlite3Strlen30(bufpt);
! 19891: }
! 19892: break;
! 19893: case etSQLESCAPE:
! 19894: case etSQLESCAPE2:
! 19895: case etSQLESCAPE3: {
! 19896: int i, j, k, n, isnull;
! 19897: int needQuote;
! 19898: char ch;
! 19899: char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
! 19900: char *escarg = va_arg(ap,char*);
! 19901: isnull = escarg==0;
! 19902: if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
! 19903: k = precision;
! 19904: for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
! 19905: if( ch==q ) n++;
! 19906: }
! 19907: needQuote = !isnull && xtype==etSQLESCAPE2;
! 19908: n += i + 1 + needQuote*2;
! 19909: if( n>etBUFSIZE ){
! 19910: bufpt = zExtra = sqlite3Malloc( n );
! 19911: if( bufpt==0 ){
! 19912: pAccum->mallocFailed = 1;
! 19913: return;
! 19914: }
! 19915: }else{
! 19916: bufpt = buf;
! 19917: }
! 19918: j = 0;
! 19919: if( needQuote ) bufpt[j++] = q;
! 19920: k = i;
! 19921: for(i=0; i<k; i++){
! 19922: bufpt[j++] = ch = escarg[i];
! 19923: if( ch==q ) bufpt[j++] = ch;
! 19924: }
! 19925: if( needQuote ) bufpt[j++] = q;
! 19926: bufpt[j] = 0;
! 19927: length = j;
! 19928: /* The precision in %q and %Q means how many input characters to
! 19929: ** consume, not the length of the output...
! 19930: ** if( precision>=0 && precision<length ) length = precision; */
! 19931: break;
! 19932: }
! 19933: case etTOKEN: {
! 19934: Token *pToken = va_arg(ap, Token*);
! 19935: if( pToken ){
! 19936: sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
! 19937: }
! 19938: length = width = 0;
! 19939: break;
! 19940: }
! 19941: case etSRCLIST: {
! 19942: SrcList *pSrc = va_arg(ap, SrcList*);
! 19943: int k = va_arg(ap, int);
! 19944: struct SrcList_item *pItem = &pSrc->a[k];
! 19945: assert( k>=0 && k<pSrc->nSrc );
! 19946: if( pItem->zDatabase ){
! 19947: sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
! 19948: sqlite3StrAccumAppend(pAccum, ".", 1);
! 19949: }
! 19950: sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
! 19951: length = width = 0;
! 19952: break;
! 19953: }
! 19954: default: {
! 19955: assert( xtype==etINVALID );
! 19956: return;
! 19957: }
! 19958: }/* End switch over the format type */
! 19959: /*
! 19960: ** The text of the conversion is pointed to by "bufpt" and is
! 19961: ** "length" characters long. The field width is "width". Do
! 19962: ** the output.
! 19963: */
! 19964: if( !flag_leftjustify ){
! 19965: register int nspace;
! 19966: nspace = width-length;
! 19967: if( nspace>0 ){
! 19968: sqlite3AppendSpace(pAccum, nspace);
! 19969: }
! 19970: }
! 19971: if( length>0 ){
! 19972: sqlite3StrAccumAppend(pAccum, bufpt, length);
! 19973: }
! 19974: if( flag_leftjustify ){
! 19975: register int nspace;
! 19976: nspace = width-length;
! 19977: if( nspace>0 ){
! 19978: sqlite3AppendSpace(pAccum, nspace);
! 19979: }
! 19980: }
! 19981: sqlite3_free(zExtra);
! 19982: }/* End for loop over the format string */
! 19983: } /* End of function */
! 19984:
! 19985: /*
! 19986: ** Append N bytes of text from z to the StrAccum object.
! 19987: */
! 19988: SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
! 19989: assert( z!=0 || N==0 );
! 19990: if( p->tooBig | p->mallocFailed ){
! 19991: testcase(p->tooBig);
! 19992: testcase(p->mallocFailed);
! 19993: return;
! 19994: }
! 19995: assert( p->zText!=0 || p->nChar==0 );
! 19996: if( N<0 ){
! 19997: N = sqlite3Strlen30(z);
! 19998: }
! 19999: if( N==0 || NEVER(z==0) ){
! 20000: return;
! 20001: }
! 20002: if( p->nChar+N >= p->nAlloc ){
! 20003: char *zNew;
! 20004: if( !p->useMalloc ){
! 20005: p->tooBig = 1;
! 20006: N = p->nAlloc - p->nChar - 1;
! 20007: if( N<=0 ){
! 20008: return;
! 20009: }
! 20010: }else{
! 20011: char *zOld = (p->zText==p->zBase ? 0 : p->zText);
! 20012: i64 szNew = p->nChar;
! 20013: szNew += N + 1;
! 20014: if( szNew > p->mxAlloc ){
! 20015: sqlite3StrAccumReset(p);
! 20016: p->tooBig = 1;
! 20017: return;
! 20018: }else{
! 20019: p->nAlloc = (int)szNew;
! 20020: }
! 20021: if( p->useMalloc==1 ){
! 20022: zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
! 20023: }else{
! 20024: zNew = sqlite3_realloc(zOld, p->nAlloc);
! 20025: }
! 20026: if( zNew ){
! 20027: if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
! 20028: p->zText = zNew;
! 20029: }else{
! 20030: p->mallocFailed = 1;
! 20031: sqlite3StrAccumReset(p);
! 20032: return;
! 20033: }
! 20034: }
! 20035: }
! 20036: assert( p->zText );
! 20037: memcpy(&p->zText[p->nChar], z, N);
! 20038: p->nChar += N;
! 20039: }
! 20040:
! 20041: /*
! 20042: ** Finish off a string by making sure it is zero-terminated.
! 20043: ** Return a pointer to the resulting string. Return a NULL
! 20044: ** pointer if any kind of error was encountered.
! 20045: */
! 20046: SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
! 20047: if( p->zText ){
! 20048: p->zText[p->nChar] = 0;
! 20049: if( p->useMalloc && p->zText==p->zBase ){
! 20050: if( p->useMalloc==1 ){
! 20051: p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
! 20052: }else{
! 20053: p->zText = sqlite3_malloc(p->nChar+1);
! 20054: }
! 20055: if( p->zText ){
! 20056: memcpy(p->zText, p->zBase, p->nChar+1);
! 20057: }else{
! 20058: p->mallocFailed = 1;
! 20059: }
! 20060: }
! 20061: }
! 20062: return p->zText;
! 20063: }
! 20064:
! 20065: /*
! 20066: ** Reset an StrAccum string. Reclaim all malloced memory.
! 20067: */
! 20068: SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
! 20069: if( p->zText!=p->zBase ){
! 20070: if( p->useMalloc==1 ){
! 20071: sqlite3DbFree(p->db, p->zText);
! 20072: }else{
! 20073: sqlite3_free(p->zText);
! 20074: }
! 20075: }
! 20076: p->zText = 0;
! 20077: }
! 20078:
! 20079: /*
! 20080: ** Initialize a string accumulator
! 20081: */
! 20082: SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
! 20083: p->zText = p->zBase = zBase;
! 20084: p->db = 0;
! 20085: p->nChar = 0;
! 20086: p->nAlloc = n;
! 20087: p->mxAlloc = mx;
! 20088: p->useMalloc = 1;
! 20089: p->tooBig = 0;
! 20090: p->mallocFailed = 0;
! 20091: }
! 20092:
! 20093: /*
! 20094: ** Print into memory obtained from sqliteMalloc(). Use the internal
! 20095: ** %-conversion extensions.
! 20096: */
! 20097: SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
! 20098: char *z;
! 20099: char zBase[SQLITE_PRINT_BUF_SIZE];
! 20100: StrAccum acc;
! 20101: assert( db!=0 );
! 20102: sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
! 20103: db->aLimit[SQLITE_LIMIT_LENGTH]);
! 20104: acc.db = db;
! 20105: sqlite3VXPrintf(&acc, 1, zFormat, ap);
! 20106: z = sqlite3StrAccumFinish(&acc);
! 20107: if( acc.mallocFailed ){
! 20108: db->mallocFailed = 1;
! 20109: }
! 20110: return z;
! 20111: }
! 20112:
! 20113: /*
! 20114: ** Print into memory obtained from sqliteMalloc(). Use the internal
! 20115: ** %-conversion extensions.
! 20116: */
! 20117: SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
! 20118: va_list ap;
! 20119: char *z;
! 20120: va_start(ap, zFormat);
! 20121: z = sqlite3VMPrintf(db, zFormat, ap);
! 20122: va_end(ap);
! 20123: return z;
! 20124: }
! 20125:
! 20126: /*
! 20127: ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
! 20128: ** the string and before returnning. This routine is intended to be used
! 20129: ** to modify an existing string. For example:
! 20130: **
! 20131: ** x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
! 20132: **
! 20133: */
! 20134: SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
! 20135: va_list ap;
! 20136: char *z;
! 20137: va_start(ap, zFormat);
! 20138: z = sqlite3VMPrintf(db, zFormat, ap);
! 20139: va_end(ap);
! 20140: sqlite3DbFree(db, zStr);
! 20141: return z;
! 20142: }
! 20143:
! 20144: /*
! 20145: ** Print into memory obtained from sqlite3_malloc(). Omit the internal
! 20146: ** %-conversion extensions.
! 20147: */
! 20148: SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
! 20149: char *z;
! 20150: char zBase[SQLITE_PRINT_BUF_SIZE];
! 20151: StrAccum acc;
! 20152: #ifndef SQLITE_OMIT_AUTOINIT
! 20153: if( sqlite3_initialize() ) return 0;
! 20154: #endif
! 20155: sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
! 20156: acc.useMalloc = 2;
! 20157: sqlite3VXPrintf(&acc, 0, zFormat, ap);
! 20158: z = sqlite3StrAccumFinish(&acc);
! 20159: return z;
! 20160: }
! 20161:
! 20162: /*
! 20163: ** Print into memory obtained from sqlite3_malloc()(). Omit the internal
! 20164: ** %-conversion extensions.
! 20165: */
! 20166: SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
! 20167: va_list ap;
! 20168: char *z;
! 20169: #ifndef SQLITE_OMIT_AUTOINIT
! 20170: if( sqlite3_initialize() ) return 0;
! 20171: #endif
! 20172: va_start(ap, zFormat);
! 20173: z = sqlite3_vmprintf(zFormat, ap);
! 20174: va_end(ap);
! 20175: return z;
! 20176: }
! 20177:
! 20178: /*
! 20179: ** sqlite3_snprintf() works like snprintf() except that it ignores the
! 20180: ** current locale settings. This is important for SQLite because we
! 20181: ** are not able to use a "," as the decimal point in place of "." as
! 20182: ** specified by some locales.
! 20183: **
! 20184: ** Oops: The first two arguments of sqlite3_snprintf() are backwards
! 20185: ** from the snprintf() standard. Unfortunately, it is too late to change
! 20186: ** this without breaking compatibility, so we just have to live with the
! 20187: ** mistake.
! 20188: **
! 20189: ** sqlite3_vsnprintf() is the varargs version.
! 20190: */
! 20191: SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
! 20192: StrAccum acc;
! 20193: if( n<=0 ) return zBuf;
! 20194: sqlite3StrAccumInit(&acc, zBuf, n, 0);
! 20195: acc.useMalloc = 0;
! 20196: sqlite3VXPrintf(&acc, 0, zFormat, ap);
! 20197: return sqlite3StrAccumFinish(&acc);
! 20198: }
! 20199: SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
! 20200: char *z;
! 20201: va_list ap;
! 20202: va_start(ap,zFormat);
! 20203: z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
! 20204: va_end(ap);
! 20205: return z;
! 20206: }
! 20207:
! 20208: /*
! 20209: ** This is the routine that actually formats the sqlite3_log() message.
! 20210: ** We house it in a separate routine from sqlite3_log() to avoid using
! 20211: ** stack space on small-stack systems when logging is disabled.
! 20212: **
! 20213: ** sqlite3_log() must render into a static buffer. It cannot dynamically
! 20214: ** allocate memory because it might be called while the memory allocator
! 20215: ** mutex is held.
! 20216: */
! 20217: static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
! 20218: StrAccum acc; /* String accumulator */
! 20219: char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
! 20220:
! 20221: sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
! 20222: acc.useMalloc = 0;
! 20223: sqlite3VXPrintf(&acc, 0, zFormat, ap);
! 20224: sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
! 20225: sqlite3StrAccumFinish(&acc));
! 20226: }
! 20227:
! 20228: /*
! 20229: ** Format and write a message to the log if logging is enabled.
! 20230: */
! 20231: SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
! 20232: va_list ap; /* Vararg list */
! 20233: if( sqlite3GlobalConfig.xLog ){
! 20234: va_start(ap, zFormat);
! 20235: renderLogMsg(iErrCode, zFormat, ap);
! 20236: va_end(ap);
! 20237: }
! 20238: }
! 20239:
! 20240: #if defined(SQLITE_DEBUG)
! 20241: /*
! 20242: ** A version of printf() that understands %lld. Used for debugging.
! 20243: ** The printf() built into some versions of windows does not understand %lld
! 20244: ** and segfaults if you give it a long long int.
! 20245: */
! 20246: SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
! 20247: va_list ap;
! 20248: StrAccum acc;
! 20249: char zBuf[500];
! 20250: sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
! 20251: acc.useMalloc = 0;
! 20252: va_start(ap,zFormat);
! 20253: sqlite3VXPrintf(&acc, 0, zFormat, ap);
! 20254: va_end(ap);
! 20255: sqlite3StrAccumFinish(&acc);
! 20256: fprintf(stdout,"%s", zBuf);
! 20257: fflush(stdout);
! 20258: }
! 20259: #endif
! 20260:
! 20261: #ifndef SQLITE_OMIT_TRACE
! 20262: /*
! 20263: ** variable-argument wrapper around sqlite3VXPrintf().
! 20264: */
! 20265: SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
! 20266: va_list ap;
! 20267: va_start(ap,zFormat);
! 20268: sqlite3VXPrintf(p, 1, zFormat, ap);
! 20269: va_end(ap);
! 20270: }
! 20271: #endif
! 20272:
! 20273: /************** End of printf.c **********************************************/
! 20274: /************** Begin file random.c ******************************************/
! 20275: /*
! 20276: ** 2001 September 15
! 20277: **
! 20278: ** The author disclaims copyright to this source code. In place of
! 20279: ** a legal notice, here is a blessing:
! 20280: **
! 20281: ** May you do good and not evil.
! 20282: ** May you find forgiveness for yourself and forgive others.
! 20283: ** May you share freely, never taking more than you give.
! 20284: **
! 20285: *************************************************************************
! 20286: ** This file contains code to implement a pseudo-random number
! 20287: ** generator (PRNG) for SQLite.
! 20288: **
! 20289: ** Random numbers are used by some of the database backends in order
! 20290: ** to generate random integer keys for tables or random filenames.
! 20291: */
! 20292:
! 20293:
! 20294: /* All threads share a single random number generator.
! 20295: ** This structure is the current state of the generator.
! 20296: */
! 20297: static SQLITE_WSD struct sqlite3PrngType {
! 20298: unsigned char isInit; /* True if initialized */
! 20299: unsigned char i, j; /* State variables */
! 20300: unsigned char s[256]; /* State variables */
! 20301: } sqlite3Prng;
! 20302:
! 20303: /*
! 20304: ** Get a single 8-bit random value from the RC4 PRNG. The Mutex
! 20305: ** must be held while executing this routine.
! 20306: **
! 20307: ** Why not just use a library random generator like lrand48() for this?
! 20308: ** Because the OP_NewRowid opcode in the VDBE depends on having a very
! 20309: ** good source of random numbers. The lrand48() library function may
! 20310: ** well be good enough. But maybe not. Or maybe lrand48() has some
! 20311: ** subtle problems on some systems that could cause problems. It is hard
! 20312: ** to know. To minimize the risk of problems due to bad lrand48()
! 20313: ** implementations, SQLite uses this random number generator based
! 20314: ** on RC4, which we know works very well.
! 20315: **
! 20316: ** (Later): Actually, OP_NewRowid does not depend on a good source of
! 20317: ** randomness any more. But we will leave this code in all the same.
! 20318: */
! 20319: static u8 randomByte(void){
! 20320: unsigned char t;
! 20321:
! 20322:
! 20323: /* The "wsdPrng" macro will resolve to the pseudo-random number generator
! 20324: ** state vector. If writable static data is unsupported on the target,
! 20325: ** we have to locate the state vector at run-time. In the more common
! 20326: ** case where writable static data is supported, wsdPrng can refer directly
! 20327: ** to the "sqlite3Prng" state vector declared above.
! 20328: */
! 20329: #ifdef SQLITE_OMIT_WSD
! 20330: struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
! 20331: # define wsdPrng p[0]
! 20332: #else
! 20333: # define wsdPrng sqlite3Prng
! 20334: #endif
! 20335:
! 20336:
! 20337: /* Initialize the state of the random number generator once,
! 20338: ** the first time this routine is called. The seed value does
! 20339: ** not need to contain a lot of randomness since we are not
! 20340: ** trying to do secure encryption or anything like that...
! 20341: **
! 20342: ** Nothing in this file or anywhere else in SQLite does any kind of
! 20343: ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
! 20344: ** number generator) not as an encryption device.
! 20345: */
! 20346: if( !wsdPrng.isInit ){
! 20347: int i;
! 20348: char k[256];
! 20349: wsdPrng.j = 0;
! 20350: wsdPrng.i = 0;
! 20351: sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
! 20352: for(i=0; i<256; i++){
! 20353: wsdPrng.s[i] = (u8)i;
! 20354: }
! 20355: for(i=0; i<256; i++){
! 20356: wsdPrng.j += wsdPrng.s[i] + k[i];
! 20357: t = wsdPrng.s[wsdPrng.j];
! 20358: wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
! 20359: wsdPrng.s[i] = t;
! 20360: }
! 20361: wsdPrng.isInit = 1;
! 20362: }
! 20363:
! 20364: /* Generate and return single random byte
! 20365: */
! 20366: wsdPrng.i++;
! 20367: t = wsdPrng.s[wsdPrng.i];
! 20368: wsdPrng.j += t;
! 20369: wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
! 20370: wsdPrng.s[wsdPrng.j] = t;
! 20371: t += wsdPrng.s[wsdPrng.i];
! 20372: return wsdPrng.s[t];
! 20373: }
! 20374:
! 20375: /*
! 20376: ** Return N random bytes.
! 20377: */
! 20378: SQLITE_API void sqlite3_randomness(int N, void *pBuf){
! 20379: unsigned char *zBuf = pBuf;
! 20380: #if SQLITE_THREADSAFE
! 20381: sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
! 20382: #endif
! 20383: sqlite3_mutex_enter(mutex);
! 20384: while( N-- ){
! 20385: *(zBuf++) = randomByte();
! 20386: }
! 20387: sqlite3_mutex_leave(mutex);
! 20388: }
! 20389:
! 20390: #ifndef SQLITE_OMIT_BUILTIN_TEST
! 20391: /*
! 20392: ** For testing purposes, we sometimes want to preserve the state of
! 20393: ** PRNG and restore the PRNG to its saved state at a later time, or
! 20394: ** to reset the PRNG to its initial state. These routines accomplish
! 20395: ** those tasks.
! 20396: **
! 20397: ** The sqlite3_test_control() interface calls these routines to
! 20398: ** control the PRNG.
! 20399: */
! 20400: static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
! 20401: SQLITE_PRIVATE void sqlite3PrngSaveState(void){
! 20402: memcpy(
! 20403: &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
! 20404: &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
! 20405: sizeof(sqlite3Prng)
! 20406: );
! 20407: }
! 20408: SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
! 20409: memcpy(
! 20410: &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
! 20411: &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
! 20412: sizeof(sqlite3Prng)
! 20413: );
! 20414: }
! 20415: SQLITE_PRIVATE void sqlite3PrngResetState(void){
! 20416: GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
! 20417: }
! 20418: #endif /* SQLITE_OMIT_BUILTIN_TEST */
! 20419:
! 20420: /************** End of random.c **********************************************/
! 20421: /************** Begin file utf.c *********************************************/
! 20422: /*
! 20423: ** 2004 April 13
! 20424: **
! 20425: ** The author disclaims copyright to this source code. In place of
! 20426: ** a legal notice, here is a blessing:
! 20427: **
! 20428: ** May you do good and not evil.
! 20429: ** May you find forgiveness for yourself and forgive others.
! 20430: ** May you share freely, never taking more than you give.
! 20431: **
! 20432: *************************************************************************
! 20433: ** This file contains routines used to translate between UTF-8,
! 20434: ** UTF-16, UTF-16BE, and UTF-16LE.
! 20435: **
! 20436: ** Notes on UTF-8:
! 20437: **
! 20438: ** Byte-0 Byte-1 Byte-2 Byte-3 Value
! 20439: ** 0xxxxxxx 00000000 00000000 0xxxxxxx
! 20440: ** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
! 20441: ** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
! 20442: ** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
! 20443: **
! 20444: **
! 20445: ** Notes on UTF-16: (with wwww+1==uuuuu)
! 20446: **
! 20447: ** Word-0 Word-1 Value
! 20448: ** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
! 20449: ** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
! 20450: **
! 20451: **
! 20452: ** BOM or Byte Order Mark:
! 20453: ** 0xff 0xfe little-endian utf-16 follows
! 20454: ** 0xfe 0xff big-endian utf-16 follows
! 20455: **
! 20456: */
! 20457: /* #include <assert.h> */
! 20458:
! 20459: #ifndef SQLITE_AMALGAMATION
! 20460: /*
! 20461: ** The following constant value is used by the SQLITE_BIGENDIAN and
! 20462: ** SQLITE_LITTLEENDIAN macros.
! 20463: */
! 20464: SQLITE_PRIVATE const int sqlite3one = 1;
! 20465: #endif /* SQLITE_AMALGAMATION */
! 20466:
! 20467: /*
! 20468: ** This lookup table is used to help decode the first byte of
! 20469: ** a multi-byte UTF8 character.
! 20470: */
! 20471: static const unsigned char sqlite3Utf8Trans1[] = {
! 20472: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
! 20473: 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
! 20474: 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
! 20475: 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
! 20476: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
! 20477: 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
! 20478: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
! 20479: 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
! 20480: };
! 20481:
! 20482:
! 20483: #define WRITE_UTF8(zOut, c) { \
! 20484: if( c<0x00080 ){ \
! 20485: *zOut++ = (u8)(c&0xFF); \
! 20486: } \
! 20487: else if( c<0x00800 ){ \
! 20488: *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
! 20489: *zOut++ = 0x80 + (u8)(c & 0x3F); \
! 20490: } \
! 20491: else if( c<0x10000 ){ \
! 20492: *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
! 20493: *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
! 20494: *zOut++ = 0x80 + (u8)(c & 0x3F); \
! 20495: }else{ \
! 20496: *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
! 20497: *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
! 20498: *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
! 20499: *zOut++ = 0x80 + (u8)(c & 0x3F); \
! 20500: } \
! 20501: }
! 20502:
! 20503: #define WRITE_UTF16LE(zOut, c) { \
! 20504: if( c<=0xFFFF ){ \
! 20505: *zOut++ = (u8)(c&0x00FF); \
! 20506: *zOut++ = (u8)((c>>8)&0x00FF); \
! 20507: }else{ \
! 20508: *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
! 20509: *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
! 20510: *zOut++ = (u8)(c&0x00FF); \
! 20511: *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
! 20512: } \
! 20513: }
! 20514:
! 20515: #define WRITE_UTF16BE(zOut, c) { \
! 20516: if( c<=0xFFFF ){ \
! 20517: *zOut++ = (u8)((c>>8)&0x00FF); \
! 20518: *zOut++ = (u8)(c&0x00FF); \
! 20519: }else{ \
! 20520: *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
! 20521: *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
! 20522: *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
! 20523: *zOut++ = (u8)(c&0x00FF); \
! 20524: } \
! 20525: }
! 20526:
! 20527: #define READ_UTF16LE(zIn, TERM, c){ \
! 20528: c = (*zIn++); \
! 20529: c += ((*zIn++)<<8); \
! 20530: if( c>=0xD800 && c<0xE000 && TERM ){ \
! 20531: int c2 = (*zIn++); \
! 20532: c2 += ((*zIn++)<<8); \
! 20533: c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
! 20534: } \
! 20535: }
! 20536:
! 20537: #define READ_UTF16BE(zIn, TERM, c){ \
! 20538: c = ((*zIn++)<<8); \
! 20539: c += (*zIn++); \
! 20540: if( c>=0xD800 && c<0xE000 && TERM ){ \
! 20541: int c2 = ((*zIn++)<<8); \
! 20542: c2 += (*zIn++); \
! 20543: c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
! 20544: } \
! 20545: }
! 20546:
! 20547: /*
! 20548: ** Translate a single UTF-8 character. Return the unicode value.
! 20549: **
! 20550: ** During translation, assume that the byte that zTerm points
! 20551: ** is a 0x00.
! 20552: **
! 20553: ** Write a pointer to the next unread byte back into *pzNext.
! 20554: **
! 20555: ** Notes On Invalid UTF-8:
! 20556: **
! 20557: ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
! 20558: ** be encoded as a multi-byte character. Any multi-byte character that
! 20559: ** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
! 20560: **
! 20561: ** * This routine never allows a UTF16 surrogate value to be encoded.
! 20562: ** If a multi-byte character attempts to encode a value between
! 20563: ** 0xd800 and 0xe000 then it is rendered as 0xfffd.
! 20564: **
! 20565: ** * Bytes in the range of 0x80 through 0xbf which occur as the first
! 20566: ** byte of a character are interpreted as single-byte characters
! 20567: ** and rendered as themselves even though they are technically
! 20568: ** invalid characters.
! 20569: **
! 20570: ** * This routine accepts an infinite number of different UTF8 encodings
! 20571: ** for unicode values 0x80 and greater. It do not change over-length
! 20572: ** encodings to 0xfffd as some systems recommend.
! 20573: */
! 20574: #define READ_UTF8(zIn, zTerm, c) \
! 20575: c = *(zIn++); \
! 20576: if( c>=0xc0 ){ \
! 20577: c = sqlite3Utf8Trans1[c-0xc0]; \
! 20578: while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
! 20579: c = (c<<6) + (0x3f & *(zIn++)); \
! 20580: } \
! 20581: if( c<0x80 \
! 20582: || (c&0xFFFFF800)==0xD800 \
! 20583: || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
! 20584: }
! 20585: SQLITE_PRIVATE u32 sqlite3Utf8Read(
! 20586: const unsigned char *zIn, /* First byte of UTF-8 character */
! 20587: const unsigned char **pzNext /* Write first byte past UTF-8 char here */
! 20588: ){
! 20589: unsigned int c;
! 20590:
! 20591: /* Same as READ_UTF8() above but without the zTerm parameter.
! 20592: ** For this routine, we assume the UTF8 string is always zero-terminated.
! 20593: */
! 20594: c = *(zIn++);
! 20595: if( c>=0xc0 ){
! 20596: c = sqlite3Utf8Trans1[c-0xc0];
! 20597: while( (*zIn & 0xc0)==0x80 ){
! 20598: c = (c<<6) + (0x3f & *(zIn++));
! 20599: }
! 20600: if( c<0x80
! 20601: || (c&0xFFFFF800)==0xD800
! 20602: || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
! 20603: }
! 20604: *pzNext = zIn;
! 20605: return c;
! 20606: }
! 20607:
! 20608:
! 20609:
! 20610:
! 20611: /*
! 20612: ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
! 20613: ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
! 20614: */
! 20615: /* #define TRANSLATE_TRACE 1 */
! 20616:
! 20617: #ifndef SQLITE_OMIT_UTF16
! 20618: /*
! 20619: ** This routine transforms the internal text encoding used by pMem to
! 20620: ** desiredEnc. It is an error if the string is already of the desired
! 20621: ** encoding, or if *pMem does not contain a string value.
! 20622: */
! 20623: SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
! 20624: int len; /* Maximum length of output string in bytes */
! 20625: unsigned char *zOut; /* Output buffer */
! 20626: unsigned char *zIn; /* Input iterator */
! 20627: unsigned char *zTerm; /* End of input */
! 20628: unsigned char *z; /* Output iterator */
! 20629: unsigned int c;
! 20630:
! 20631: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 20632: assert( pMem->flags&MEM_Str );
! 20633: assert( pMem->enc!=desiredEnc );
! 20634: assert( pMem->enc!=0 );
! 20635: assert( pMem->n>=0 );
! 20636:
! 20637: #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
! 20638: {
! 20639: char zBuf[100];
! 20640: sqlite3VdbeMemPrettyPrint(pMem, zBuf);
! 20641: fprintf(stderr, "INPUT: %s\n", zBuf);
! 20642: }
! 20643: #endif
! 20644:
! 20645: /* If the translation is between UTF-16 little and big endian, then
! 20646: ** all that is required is to swap the byte order. This case is handled
! 20647: ** differently from the others.
! 20648: */
! 20649: if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
! 20650: u8 temp;
! 20651: int rc;
! 20652: rc = sqlite3VdbeMemMakeWriteable(pMem);
! 20653: if( rc!=SQLITE_OK ){
! 20654: assert( rc==SQLITE_NOMEM );
! 20655: return SQLITE_NOMEM;
! 20656: }
! 20657: zIn = (u8*)pMem->z;
! 20658: zTerm = &zIn[pMem->n&~1];
! 20659: while( zIn<zTerm ){
! 20660: temp = *zIn;
! 20661: *zIn = *(zIn+1);
! 20662: zIn++;
! 20663: *zIn++ = temp;
! 20664: }
! 20665: pMem->enc = desiredEnc;
! 20666: goto translate_out;
! 20667: }
! 20668:
! 20669: /* Set len to the maximum number of bytes required in the output buffer. */
! 20670: if( desiredEnc==SQLITE_UTF8 ){
! 20671: /* When converting from UTF-16, the maximum growth results from
! 20672: ** translating a 2-byte character to a 4-byte UTF-8 character.
! 20673: ** A single byte is required for the output string
! 20674: ** nul-terminator.
! 20675: */
! 20676: pMem->n &= ~1;
! 20677: len = pMem->n * 2 + 1;
! 20678: }else{
! 20679: /* When converting from UTF-8 to UTF-16 the maximum growth is caused
! 20680: ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
! 20681: ** character. Two bytes are required in the output buffer for the
! 20682: ** nul-terminator.
! 20683: */
! 20684: len = pMem->n * 2 + 2;
! 20685: }
! 20686:
! 20687: /* Set zIn to point at the start of the input buffer and zTerm to point 1
! 20688: ** byte past the end.
! 20689: **
! 20690: ** Variable zOut is set to point at the output buffer, space obtained
! 20691: ** from sqlite3_malloc().
! 20692: */
! 20693: zIn = (u8*)pMem->z;
! 20694: zTerm = &zIn[pMem->n];
! 20695: zOut = sqlite3DbMallocRaw(pMem->db, len);
! 20696: if( !zOut ){
! 20697: return SQLITE_NOMEM;
! 20698: }
! 20699: z = zOut;
! 20700:
! 20701: if( pMem->enc==SQLITE_UTF8 ){
! 20702: if( desiredEnc==SQLITE_UTF16LE ){
! 20703: /* UTF-8 -> UTF-16 Little-endian */
! 20704: while( zIn<zTerm ){
! 20705: /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
! 20706: READ_UTF8(zIn, zTerm, c);
! 20707: WRITE_UTF16LE(z, c);
! 20708: }
! 20709: }else{
! 20710: assert( desiredEnc==SQLITE_UTF16BE );
! 20711: /* UTF-8 -> UTF-16 Big-endian */
! 20712: while( zIn<zTerm ){
! 20713: /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
! 20714: READ_UTF8(zIn, zTerm, c);
! 20715: WRITE_UTF16BE(z, c);
! 20716: }
! 20717: }
! 20718: pMem->n = (int)(z - zOut);
! 20719: *z++ = 0;
! 20720: }else{
! 20721: assert( desiredEnc==SQLITE_UTF8 );
! 20722: if( pMem->enc==SQLITE_UTF16LE ){
! 20723: /* UTF-16 Little-endian -> UTF-8 */
! 20724: while( zIn<zTerm ){
! 20725: READ_UTF16LE(zIn, zIn<zTerm, c);
! 20726: WRITE_UTF8(z, c);
! 20727: }
! 20728: }else{
! 20729: /* UTF-16 Big-endian -> UTF-8 */
! 20730: while( zIn<zTerm ){
! 20731: READ_UTF16BE(zIn, zIn<zTerm, c);
! 20732: WRITE_UTF8(z, c);
! 20733: }
! 20734: }
! 20735: pMem->n = (int)(z - zOut);
! 20736: }
! 20737: *z = 0;
! 20738: assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
! 20739:
! 20740: sqlite3VdbeMemRelease(pMem);
! 20741: pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
! 20742: pMem->enc = desiredEnc;
! 20743: pMem->flags |= (MEM_Term|MEM_Dyn);
! 20744: pMem->z = (char*)zOut;
! 20745: pMem->zMalloc = pMem->z;
! 20746:
! 20747: translate_out:
! 20748: #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
! 20749: {
! 20750: char zBuf[100];
! 20751: sqlite3VdbeMemPrettyPrint(pMem, zBuf);
! 20752: fprintf(stderr, "OUTPUT: %s\n", zBuf);
! 20753: }
! 20754: #endif
! 20755: return SQLITE_OK;
! 20756: }
! 20757:
! 20758: /*
! 20759: ** This routine checks for a byte-order mark at the beginning of the
! 20760: ** UTF-16 string stored in *pMem. If one is present, it is removed and
! 20761: ** the encoding of the Mem adjusted. This routine does not do any
! 20762: ** byte-swapping, it just sets Mem.enc appropriately.
! 20763: **
! 20764: ** The allocation (static, dynamic etc.) and encoding of the Mem may be
! 20765: ** changed by this function.
! 20766: */
! 20767: SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
! 20768: int rc = SQLITE_OK;
! 20769: u8 bom = 0;
! 20770:
! 20771: assert( pMem->n>=0 );
! 20772: if( pMem->n>1 ){
! 20773: u8 b1 = *(u8 *)pMem->z;
! 20774: u8 b2 = *(((u8 *)pMem->z) + 1);
! 20775: if( b1==0xFE && b2==0xFF ){
! 20776: bom = SQLITE_UTF16BE;
! 20777: }
! 20778: if( b1==0xFF && b2==0xFE ){
! 20779: bom = SQLITE_UTF16LE;
! 20780: }
! 20781: }
! 20782:
! 20783: if( bom ){
! 20784: rc = sqlite3VdbeMemMakeWriteable(pMem);
! 20785: if( rc==SQLITE_OK ){
! 20786: pMem->n -= 2;
! 20787: memmove(pMem->z, &pMem->z[2], pMem->n);
! 20788: pMem->z[pMem->n] = '\0';
! 20789: pMem->z[pMem->n+1] = '\0';
! 20790: pMem->flags |= MEM_Term;
! 20791: pMem->enc = bom;
! 20792: }
! 20793: }
! 20794: return rc;
! 20795: }
! 20796: #endif /* SQLITE_OMIT_UTF16 */
! 20797:
! 20798: /*
! 20799: ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
! 20800: ** return the number of unicode characters in pZ up to (but not including)
! 20801: ** the first 0x00 byte. If nByte is not less than zero, return the
! 20802: ** number of unicode characters in the first nByte of pZ (or up to
! 20803: ** the first 0x00, whichever comes first).
! 20804: */
! 20805: SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
! 20806: int r = 0;
! 20807: const u8 *z = (const u8*)zIn;
! 20808: const u8 *zTerm;
! 20809: if( nByte>=0 ){
! 20810: zTerm = &z[nByte];
! 20811: }else{
! 20812: zTerm = (const u8*)(-1);
! 20813: }
! 20814: assert( z<=zTerm );
! 20815: while( *z!=0 && z<zTerm ){
! 20816: SQLITE_SKIP_UTF8(z);
! 20817: r++;
! 20818: }
! 20819: return r;
! 20820: }
! 20821:
! 20822: /* This test function is not currently used by the automated test-suite.
! 20823: ** Hence it is only available in debug builds.
! 20824: */
! 20825: #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
! 20826: /*
! 20827: ** Translate UTF-8 to UTF-8.
! 20828: **
! 20829: ** This has the effect of making sure that the string is well-formed
! 20830: ** UTF-8. Miscoded characters are removed.
! 20831: **
! 20832: ** The translation is done in-place and aborted if the output
! 20833: ** overruns the input.
! 20834: */
! 20835: SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
! 20836: unsigned char *zOut = zIn;
! 20837: unsigned char *zStart = zIn;
! 20838: u32 c;
! 20839:
! 20840: while( zIn[0] && zOut<=zIn ){
! 20841: c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
! 20842: if( c!=0xfffd ){
! 20843: WRITE_UTF8(zOut, c);
! 20844: }
! 20845: }
! 20846: *zOut = 0;
! 20847: return (int)(zOut - zStart);
! 20848: }
! 20849: #endif
! 20850:
! 20851: #ifndef SQLITE_OMIT_UTF16
! 20852: /*
! 20853: ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
! 20854: ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
! 20855: ** be freed by the calling function.
! 20856: **
! 20857: ** NULL is returned if there is an allocation error.
! 20858: */
! 20859: SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
! 20860: Mem m;
! 20861: memset(&m, 0, sizeof(m));
! 20862: m.db = db;
! 20863: sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
! 20864: sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
! 20865: if( db->mallocFailed ){
! 20866: sqlite3VdbeMemRelease(&m);
! 20867: m.z = 0;
! 20868: }
! 20869: assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
! 20870: assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
! 20871: assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
! 20872: assert( m.z || db->mallocFailed );
! 20873: return m.z;
! 20874: }
! 20875:
! 20876: /*
! 20877: ** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
! 20878: ** enc. A pointer to the new string is returned, and the value of *pnOut
! 20879: ** is set to the length of the returned string in bytes. The call should
! 20880: ** arrange to call sqlite3DbFree() on the returned pointer when it is
! 20881: ** no longer required.
! 20882: **
! 20883: ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
! 20884: ** flag set.
! 20885: */
! 20886: #ifdef SQLITE_ENABLE_STAT3
! 20887: SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
! 20888: Mem m;
! 20889: memset(&m, 0, sizeof(m));
! 20890: m.db = db;
! 20891: sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
! 20892: if( sqlite3VdbeMemTranslate(&m, enc) ){
! 20893: assert( db->mallocFailed );
! 20894: return 0;
! 20895: }
! 20896: assert( m.z==m.zMalloc );
! 20897: *pnOut = m.n;
! 20898: return m.z;
! 20899: }
! 20900: #endif
! 20901:
! 20902: /*
! 20903: ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
! 20904: ** Return the number of bytes in the first nChar unicode characters
! 20905: ** in pZ. nChar must be non-negative.
! 20906: */
! 20907: SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
! 20908: int c;
! 20909: unsigned char const *z = zIn;
! 20910: int n = 0;
! 20911:
! 20912: if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
! 20913: while( n<nChar ){
! 20914: READ_UTF16BE(z, 1, c);
! 20915: n++;
! 20916: }
! 20917: }else{
! 20918: while( n<nChar ){
! 20919: READ_UTF16LE(z, 1, c);
! 20920: n++;
! 20921: }
! 20922: }
! 20923: return (int)(z-(unsigned char const *)zIn);
! 20924: }
! 20925:
! 20926: #if defined(SQLITE_TEST)
! 20927: /*
! 20928: ** This routine is called from the TCL test function "translate_selftest".
! 20929: ** It checks that the primitives for serializing and deserializing
! 20930: ** characters in each encoding are inverses of each other.
! 20931: */
! 20932: SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
! 20933: unsigned int i, t;
! 20934: unsigned char zBuf[20];
! 20935: unsigned char *z;
! 20936: int n;
! 20937: unsigned int c;
! 20938:
! 20939: for(i=0; i<0x00110000; i++){
! 20940: z = zBuf;
! 20941: WRITE_UTF8(z, i);
! 20942: n = (int)(z-zBuf);
! 20943: assert( n>0 && n<=4 );
! 20944: z[0] = 0;
! 20945: z = zBuf;
! 20946: c = sqlite3Utf8Read(z, (const u8**)&z);
! 20947: t = i;
! 20948: if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
! 20949: if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
! 20950: assert( c==t );
! 20951: assert( (z-zBuf)==n );
! 20952: }
! 20953: for(i=0; i<0x00110000; i++){
! 20954: if( i>=0xD800 && i<0xE000 ) continue;
! 20955: z = zBuf;
! 20956: WRITE_UTF16LE(z, i);
! 20957: n = (int)(z-zBuf);
! 20958: assert( n>0 && n<=4 );
! 20959: z[0] = 0;
! 20960: z = zBuf;
! 20961: READ_UTF16LE(z, 1, c);
! 20962: assert( c==i );
! 20963: assert( (z-zBuf)==n );
! 20964: }
! 20965: for(i=0; i<0x00110000; i++){
! 20966: if( i>=0xD800 && i<0xE000 ) continue;
! 20967: z = zBuf;
! 20968: WRITE_UTF16BE(z, i);
! 20969: n = (int)(z-zBuf);
! 20970: assert( n>0 && n<=4 );
! 20971: z[0] = 0;
! 20972: z = zBuf;
! 20973: READ_UTF16BE(z, 1, c);
! 20974: assert( c==i );
! 20975: assert( (z-zBuf)==n );
! 20976: }
! 20977: }
! 20978: #endif /* SQLITE_TEST */
! 20979: #endif /* SQLITE_OMIT_UTF16 */
! 20980:
! 20981: /************** End of utf.c *************************************************/
! 20982: /************** Begin file util.c ********************************************/
! 20983: /*
! 20984: ** 2001 September 15
! 20985: **
! 20986: ** The author disclaims copyright to this source code. In place of
! 20987: ** a legal notice, here is a blessing:
! 20988: **
! 20989: ** May you do good and not evil.
! 20990: ** May you find forgiveness for yourself and forgive others.
! 20991: ** May you share freely, never taking more than you give.
! 20992: **
! 20993: *************************************************************************
! 20994: ** Utility functions used throughout sqlite.
! 20995: **
! 20996: ** This file contains functions for allocating memory, comparing
! 20997: ** strings, and stuff like that.
! 20998: **
! 20999: */
! 21000: /* #include <stdarg.h> */
! 21001: #ifdef SQLITE_HAVE_ISNAN
! 21002: # include <math.h>
! 21003: #endif
! 21004:
! 21005: /*
! 21006: ** Routine needed to support the testcase() macro.
! 21007: */
! 21008: #ifdef SQLITE_COVERAGE_TEST
! 21009: SQLITE_PRIVATE void sqlite3Coverage(int x){
! 21010: static unsigned dummy = 0;
! 21011: dummy += (unsigned)x;
! 21012: }
! 21013: #endif
! 21014:
! 21015: #ifndef SQLITE_OMIT_FLOATING_POINT
! 21016: /*
! 21017: ** Return true if the floating point value is Not a Number (NaN).
! 21018: **
! 21019: ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
! 21020: ** Otherwise, we have our own implementation that works on most systems.
! 21021: */
! 21022: SQLITE_PRIVATE int sqlite3IsNaN(double x){
! 21023: int rc; /* The value return */
! 21024: #if !defined(SQLITE_HAVE_ISNAN)
! 21025: /*
! 21026: ** Systems that support the isnan() library function should probably
! 21027: ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have
! 21028: ** found that many systems do not have a working isnan() function so
! 21029: ** this implementation is provided as an alternative.
! 21030: **
! 21031: ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
! 21032: ** On the other hand, the use of -ffast-math comes with the following
! 21033: ** warning:
! 21034: **
! 21035: ** This option [-ffast-math] should never be turned on by any
! 21036: ** -O option since it can result in incorrect output for programs
! 21037: ** which depend on an exact implementation of IEEE or ISO
! 21038: ** rules/specifications for math functions.
! 21039: **
! 21040: ** Under MSVC, this NaN test may fail if compiled with a floating-
! 21041: ** point precision mode other than /fp:precise. From the MSDN
! 21042: ** documentation:
! 21043: **
! 21044: ** The compiler [with /fp:precise] will properly handle comparisons
! 21045: ** involving NaN. For example, x != x evaluates to true if x is NaN
! 21046: ** ...
! 21047: */
! 21048: #ifdef __FAST_MATH__
! 21049: # error SQLite will not work correctly with the -ffast-math option of GCC.
! 21050: #endif
! 21051: volatile double y = x;
! 21052: volatile double z = y;
! 21053: rc = (y!=z);
! 21054: #else /* if defined(SQLITE_HAVE_ISNAN) */
! 21055: rc = isnan(x);
! 21056: #endif /* SQLITE_HAVE_ISNAN */
! 21057: testcase( rc );
! 21058: return rc;
! 21059: }
! 21060: #endif /* SQLITE_OMIT_FLOATING_POINT */
! 21061:
! 21062: /*
! 21063: ** Compute a string length that is limited to what can be stored in
! 21064: ** lower 30 bits of a 32-bit signed integer.
! 21065: **
! 21066: ** The value returned will never be negative. Nor will it ever be greater
! 21067: ** than the actual length of the string. For very long strings (greater
! 21068: ** than 1GiB) the value returned might be less than the true string length.
! 21069: */
! 21070: SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
! 21071: const char *z2 = z;
! 21072: if( z==0 ) return 0;
! 21073: while( *z2 ){ z2++; }
! 21074: return 0x3fffffff & (int)(z2 - z);
! 21075: }
! 21076:
! 21077: /*
! 21078: ** Set the most recent error code and error string for the sqlite
! 21079: ** handle "db". The error code is set to "err_code".
! 21080: **
! 21081: ** If it is not NULL, string zFormat specifies the format of the
! 21082: ** error string in the style of the printf functions: The following
! 21083: ** format characters are allowed:
! 21084: **
! 21085: ** %s Insert a string
! 21086: ** %z A string that should be freed after use
! 21087: ** %d Insert an integer
! 21088: ** %T Insert a token
! 21089: ** %S Insert the first element of a SrcList
! 21090: **
! 21091: ** zFormat and any string tokens that follow it are assumed to be
! 21092: ** encoded in UTF-8.
! 21093: **
! 21094: ** To clear the most recent error for sqlite handle "db", sqlite3Error
! 21095: ** should be called with err_code set to SQLITE_OK and zFormat set
! 21096: ** to NULL.
! 21097: */
! 21098: SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
! 21099: if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
! 21100: db->errCode = err_code;
! 21101: if( zFormat ){
! 21102: char *z;
! 21103: va_list ap;
! 21104: va_start(ap, zFormat);
! 21105: z = sqlite3VMPrintf(db, zFormat, ap);
! 21106: va_end(ap);
! 21107: sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
! 21108: }else{
! 21109: sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
! 21110: }
! 21111: }
! 21112: }
! 21113:
! 21114: /*
! 21115: ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
! 21116: ** The following formatting characters are allowed:
! 21117: **
! 21118: ** %s Insert a string
! 21119: ** %z A string that should be freed after use
! 21120: ** %d Insert an integer
! 21121: ** %T Insert a token
! 21122: ** %S Insert the first element of a SrcList
! 21123: **
! 21124: ** This function should be used to report any error that occurs whilst
! 21125: ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
! 21126: ** last thing the sqlite3_prepare() function does is copy the error
! 21127: ** stored by this function into the database handle using sqlite3Error().
! 21128: ** Function sqlite3Error() should be used during statement execution
! 21129: ** (sqlite3_step() etc.).
! 21130: */
! 21131: SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
! 21132: char *zMsg;
! 21133: va_list ap;
! 21134: sqlite3 *db = pParse->db;
! 21135: va_start(ap, zFormat);
! 21136: zMsg = sqlite3VMPrintf(db, zFormat, ap);
! 21137: va_end(ap);
! 21138: if( db->suppressErr ){
! 21139: sqlite3DbFree(db, zMsg);
! 21140: }else{
! 21141: pParse->nErr++;
! 21142: sqlite3DbFree(db, pParse->zErrMsg);
! 21143: pParse->zErrMsg = zMsg;
! 21144: pParse->rc = SQLITE_ERROR;
! 21145: }
! 21146: }
! 21147:
! 21148: /*
! 21149: ** Convert an SQL-style quoted string into a normal string by removing
! 21150: ** the quote characters. The conversion is done in-place. If the
! 21151: ** input does not begin with a quote character, then this routine
! 21152: ** is a no-op.
! 21153: **
! 21154: ** The input string must be zero-terminated. A new zero-terminator
! 21155: ** is added to the dequoted string.
! 21156: **
! 21157: ** The return value is -1 if no dequoting occurs or the length of the
! 21158: ** dequoted string, exclusive of the zero terminator, if dequoting does
! 21159: ** occur.
! 21160: **
! 21161: ** 2002-Feb-14: This routine is extended to remove MS-Access style
! 21162: ** brackets from around identifers. For example: "[a-b-c]" becomes
! 21163: ** "a-b-c".
! 21164: */
! 21165: SQLITE_PRIVATE int sqlite3Dequote(char *z){
! 21166: char quote;
! 21167: int i, j;
! 21168: if( z==0 ) return -1;
! 21169: quote = z[0];
! 21170: switch( quote ){
! 21171: case '\'': break;
! 21172: case '"': break;
! 21173: case '`': break; /* For MySQL compatibility */
! 21174: case '[': quote = ']'; break; /* For MS SqlServer compatibility */
! 21175: default: return -1;
! 21176: }
! 21177: for(i=1, j=0; ALWAYS(z[i]); i++){
! 21178: if( z[i]==quote ){
! 21179: if( z[i+1]==quote ){
! 21180: z[j++] = quote;
! 21181: i++;
! 21182: }else{
! 21183: break;
! 21184: }
! 21185: }else{
! 21186: z[j++] = z[i];
! 21187: }
! 21188: }
! 21189: z[j] = 0;
! 21190: return j;
! 21191: }
! 21192:
! 21193: /* Convenient short-hand */
! 21194: #define UpperToLower sqlite3UpperToLower
! 21195:
! 21196: /*
! 21197: ** Some systems have stricmp(). Others have strcasecmp(). Because
! 21198: ** there is no consistency, we will define our own.
! 21199: **
! 21200: ** IMPLEMENTATION-OF: R-20522-24639 The sqlite3_strnicmp() API allows
! 21201: ** applications and extensions to compare the contents of two buffers
! 21202: ** containing UTF-8 strings in a case-independent fashion, using the same
! 21203: ** definition of case independence that SQLite uses internally when
! 21204: ** comparing identifiers.
! 21205: */
! 21206: SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
! 21207: register unsigned char *a, *b;
! 21208: a = (unsigned char *)zLeft;
! 21209: b = (unsigned char *)zRight;
! 21210: while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
! 21211: return UpperToLower[*a] - UpperToLower[*b];
! 21212: }
! 21213: SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
! 21214: register unsigned char *a, *b;
! 21215: a = (unsigned char *)zLeft;
! 21216: b = (unsigned char *)zRight;
! 21217: while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
! 21218: return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
! 21219: }
! 21220:
! 21221: /*
! 21222: ** The string z[] is an text representation of a real number.
! 21223: ** Convert this string to a double and write it into *pResult.
! 21224: **
! 21225: ** The string z[] is length bytes in length (bytes, not characters) and
! 21226: ** uses the encoding enc. The string is not necessarily zero-terminated.
! 21227: **
! 21228: ** Return TRUE if the result is a valid real number (or integer) and FALSE
! 21229: ** if the string is empty or contains extraneous text. Valid numbers
! 21230: ** are in one of these formats:
! 21231: **
! 21232: ** [+-]digits[E[+-]digits]
! 21233: ** [+-]digits.[digits][E[+-]digits]
! 21234: ** [+-].digits[E[+-]digits]
! 21235: **
! 21236: ** Leading and trailing whitespace is ignored for the purpose of determining
! 21237: ** validity.
! 21238: **
! 21239: ** If some prefix of the input string is a valid number, this routine
! 21240: ** returns FALSE but it still converts the prefix and writes the result
! 21241: ** into *pResult.
! 21242: */
! 21243: SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
! 21244: #ifndef SQLITE_OMIT_FLOATING_POINT
! 21245: int incr = (enc==SQLITE_UTF8?1:2);
! 21246: const char *zEnd = z + length;
! 21247: /* sign * significand * (10 ^ (esign * exponent)) */
! 21248: int sign = 1; /* sign of significand */
! 21249: i64 s = 0; /* significand */
! 21250: int d = 0; /* adjust exponent for shifting decimal point */
! 21251: int esign = 1; /* sign of exponent */
! 21252: int e = 0; /* exponent */
! 21253: int eValid = 1; /* True exponent is either not used or is well-formed */
! 21254: double result;
! 21255: int nDigits = 0;
! 21256:
! 21257: *pResult = 0.0; /* Default return value, in case of an error */
! 21258:
! 21259: if( enc==SQLITE_UTF16BE ) z++;
! 21260:
! 21261: /* skip leading spaces */
! 21262: while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
! 21263: if( z>=zEnd ) return 0;
! 21264:
! 21265: /* get sign of significand */
! 21266: if( *z=='-' ){
! 21267: sign = -1;
! 21268: z+=incr;
! 21269: }else if( *z=='+' ){
! 21270: z+=incr;
! 21271: }
! 21272:
! 21273: /* skip leading zeroes */
! 21274: while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
! 21275:
! 21276: /* copy max significant digits to significand */
! 21277: while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
! 21278: s = s*10 + (*z - '0');
! 21279: z+=incr, nDigits++;
! 21280: }
! 21281:
! 21282: /* skip non-significant significand digits
! 21283: ** (increase exponent by d to shift decimal left) */
! 21284: while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
! 21285: if( z>=zEnd ) goto do_atof_calc;
! 21286:
! 21287: /* if decimal point is present */
! 21288: if( *z=='.' ){
! 21289: z+=incr;
! 21290: /* copy digits from after decimal to significand
! 21291: ** (decrease exponent by d to shift decimal right) */
! 21292: while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
! 21293: s = s*10 + (*z - '0');
! 21294: z+=incr, nDigits++, d--;
! 21295: }
! 21296: /* skip non-significant digits */
! 21297: while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
! 21298: }
! 21299: if( z>=zEnd ) goto do_atof_calc;
! 21300:
! 21301: /* if exponent is present */
! 21302: if( *z=='e' || *z=='E' ){
! 21303: z+=incr;
! 21304: eValid = 0;
! 21305: if( z>=zEnd ) goto do_atof_calc;
! 21306: /* get sign of exponent */
! 21307: if( *z=='-' ){
! 21308: esign = -1;
! 21309: z+=incr;
! 21310: }else if( *z=='+' ){
! 21311: z+=incr;
! 21312: }
! 21313: /* copy digits to exponent */
! 21314: while( z<zEnd && sqlite3Isdigit(*z) ){
! 21315: e = e<10000 ? (e*10 + (*z - '0')) : 10000;
! 21316: z+=incr;
! 21317: eValid = 1;
! 21318: }
! 21319: }
! 21320:
! 21321: /* skip trailing spaces */
! 21322: if( nDigits && eValid ){
! 21323: while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
! 21324: }
! 21325:
! 21326: do_atof_calc:
! 21327: /* adjust exponent by d, and update sign */
! 21328: e = (e*esign) + d;
! 21329: if( e<0 ) {
! 21330: esign = -1;
! 21331: e *= -1;
! 21332: } else {
! 21333: esign = 1;
! 21334: }
! 21335:
! 21336: /* if 0 significand */
! 21337: if( !s ) {
! 21338: /* In the IEEE 754 standard, zero is signed.
! 21339: ** Add the sign if we've seen at least one digit */
! 21340: result = (sign<0 && nDigits) ? -(double)0 : (double)0;
! 21341: } else {
! 21342: /* attempt to reduce exponent */
! 21343: if( esign>0 ){
! 21344: while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
! 21345: }else{
! 21346: while( !(s%10) && e>0 ) e--,s/=10;
! 21347: }
! 21348:
! 21349: /* adjust the sign of significand */
! 21350: s = sign<0 ? -s : s;
! 21351:
! 21352: /* if exponent, scale significand as appropriate
! 21353: ** and store in result. */
! 21354: if( e ){
! 21355: double scale = 1.0;
! 21356: #ifndef __vax__
! 21357: /* attempt to handle extremely small/large numbers better */
! 21358: if( e>307 && e<342 ){
! 21359: while( e%308 ) { scale *= 1.0e+1; e -= 1; }
! 21360: if( esign<0 ){
! 21361: result = s / scale;
! 21362: result /= SQLITE_HUGE_DBL;
! 21363: }else{
! 21364: result = s * scale;
! 21365: result *= SQLITE_HUGE_DBL;
! 21366: }
! 21367: }else if( e>=342 ){
! 21368: if( esign<0 ){
! 21369: result = 0.0*s;
! 21370: }else{
! 21371: result = 1e308*1e308*s; /* Infinity */
! 21372: }
! 21373: }else
! 21374: #endif
! 21375: {
! 21376: /* 1.0e+22 is the largest power of 10 than can be
! 21377: ** represented exactly. */
! 21378: while( e%22 ) { scale *= 1.0e+1; e -= 1; }
! 21379: while( e>0 ) { scale *= 1.0e+22; e -= 22; }
! 21380: if( esign<0 ){
! 21381: result = s / scale;
! 21382: }else{
! 21383: result = s * scale;
! 21384: }
! 21385: }
! 21386: } else {
! 21387: result = (double)s;
! 21388: }
! 21389: }
! 21390:
! 21391: /* store the result */
! 21392: *pResult = result;
! 21393:
! 21394: /* return true if number and no extra non-whitespace chracters after */
! 21395: return z>=zEnd && nDigits>0 && eValid;
! 21396: #else
! 21397: return !sqlite3Atoi64(z, pResult, length, enc);
! 21398: #endif /* SQLITE_OMIT_FLOATING_POINT */
! 21399: }
! 21400:
! 21401: /*
! 21402: ** Compare the 19-character string zNum against the text representation
! 21403: ** value 2^63: 9223372036854775808. Return negative, zero, or positive
! 21404: ** if zNum is less than, equal to, or greater than the string.
! 21405: ** Note that zNum must contain exactly 19 characters.
! 21406: **
! 21407: ** Unlike memcmp() this routine is guaranteed to return the difference
! 21408: ** in the values of the last digit if the only difference is in the
! 21409: ** last digit. So, for example,
! 21410: **
! 21411: ** compare2pow63("9223372036854775800", 1)
! 21412: **
! 21413: ** will return -8.
! 21414: */
! 21415: static int compare2pow63(const char *zNum, int incr){
! 21416: int c = 0;
! 21417: int i;
! 21418: /* 012345678901234567 */
! 21419: const char *pow63 = "922337203685477580";
! 21420: for(i=0; c==0 && i<18; i++){
! 21421: c = (zNum[i*incr]-pow63[i])*10;
! 21422: }
! 21423: if( c==0 ){
! 21424: c = zNum[18*incr] - '8';
! 21425: testcase( c==(-1) );
! 21426: testcase( c==0 );
! 21427: testcase( c==(+1) );
! 21428: }
! 21429: return c;
! 21430: }
! 21431:
! 21432:
! 21433: /*
! 21434: ** Convert zNum to a 64-bit signed integer.
! 21435: **
! 21436: ** If the zNum value is representable as a 64-bit twos-complement
! 21437: ** integer, then write that value into *pNum and return 0.
! 21438: **
! 21439: ** If zNum is exactly 9223372036854665808, return 2. This special
! 21440: ** case is broken out because while 9223372036854665808 cannot be a
! 21441: ** signed 64-bit integer, its negative -9223372036854665808 can be.
! 21442: **
! 21443: ** If zNum is too big for a 64-bit integer and is not
! 21444: ** 9223372036854665808 then return 1.
! 21445: **
! 21446: ** length is the number of bytes in the string (bytes, not characters).
! 21447: ** The string is not necessarily zero-terminated. The encoding is
! 21448: ** given by enc.
! 21449: */
! 21450: SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
! 21451: int incr = (enc==SQLITE_UTF8?1:2);
! 21452: u64 u = 0;
! 21453: int neg = 0; /* assume positive */
! 21454: int i;
! 21455: int c = 0;
! 21456: const char *zStart;
! 21457: const char *zEnd = zNum + length;
! 21458: if( enc==SQLITE_UTF16BE ) zNum++;
! 21459: while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
! 21460: if( zNum<zEnd ){
! 21461: if( *zNum=='-' ){
! 21462: neg = 1;
! 21463: zNum+=incr;
! 21464: }else if( *zNum=='+' ){
! 21465: zNum+=incr;
! 21466: }
! 21467: }
! 21468: zStart = zNum;
! 21469: while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
! 21470: for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
! 21471: u = u*10 + c - '0';
! 21472: }
! 21473: if( u>LARGEST_INT64 ){
! 21474: *pNum = SMALLEST_INT64;
! 21475: }else if( neg ){
! 21476: *pNum = -(i64)u;
! 21477: }else{
! 21478: *pNum = (i64)u;
! 21479: }
! 21480: testcase( i==18 );
! 21481: testcase( i==19 );
! 21482: testcase( i==20 );
! 21483: if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
! 21484: /* zNum is empty or contains non-numeric text or is longer
! 21485: ** than 19 digits (thus guaranteeing that it is too large) */
! 21486: return 1;
! 21487: }else if( i<19*incr ){
! 21488: /* Less than 19 digits, so we know that it fits in 64 bits */
! 21489: assert( u<=LARGEST_INT64 );
! 21490: return 0;
! 21491: }else{
! 21492: /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
! 21493: c = compare2pow63(zNum, incr);
! 21494: if( c<0 ){
! 21495: /* zNum is less than 9223372036854775808 so it fits */
! 21496: assert( u<=LARGEST_INT64 );
! 21497: return 0;
! 21498: }else if( c>0 ){
! 21499: /* zNum is greater than 9223372036854775808 so it overflows */
! 21500: return 1;
! 21501: }else{
! 21502: /* zNum is exactly 9223372036854775808. Fits if negative. The
! 21503: ** special case 2 overflow if positive */
! 21504: assert( u-1==LARGEST_INT64 );
! 21505: assert( (*pNum)==SMALLEST_INT64 );
! 21506: return neg ? 0 : 2;
! 21507: }
! 21508: }
! 21509: }
! 21510:
! 21511: /*
! 21512: ** If zNum represents an integer that will fit in 32-bits, then set
! 21513: ** *pValue to that integer and return true. Otherwise return false.
! 21514: **
! 21515: ** Any non-numeric characters that following zNum are ignored.
! 21516: ** This is different from sqlite3Atoi64() which requires the
! 21517: ** input number to be zero-terminated.
! 21518: */
! 21519: SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
! 21520: sqlite_int64 v = 0;
! 21521: int i, c;
! 21522: int neg = 0;
! 21523: if( zNum[0]=='-' ){
! 21524: neg = 1;
! 21525: zNum++;
! 21526: }else if( zNum[0]=='+' ){
! 21527: zNum++;
! 21528: }
! 21529: while( zNum[0]=='0' ) zNum++;
! 21530: for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
! 21531: v = v*10 + c;
! 21532: }
! 21533:
! 21534: /* The longest decimal representation of a 32 bit integer is 10 digits:
! 21535: **
! 21536: ** 1234567890
! 21537: ** 2^31 -> 2147483648
! 21538: */
! 21539: testcase( i==10 );
! 21540: if( i>10 ){
! 21541: return 0;
! 21542: }
! 21543: testcase( v-neg==2147483647 );
! 21544: if( v-neg>2147483647 ){
! 21545: return 0;
! 21546: }
! 21547: if( neg ){
! 21548: v = -v;
! 21549: }
! 21550: *pValue = (int)v;
! 21551: return 1;
! 21552: }
! 21553:
! 21554: /*
! 21555: ** Return a 32-bit integer value extracted from a string. If the
! 21556: ** string is not an integer, just return 0.
! 21557: */
! 21558: SQLITE_PRIVATE int sqlite3Atoi(const char *z){
! 21559: int x = 0;
! 21560: if( z ) sqlite3GetInt32(z, &x);
! 21561: return x;
! 21562: }
! 21563:
! 21564: /*
! 21565: ** The variable-length integer encoding is as follows:
! 21566: **
! 21567: ** KEY:
! 21568: ** A = 0xxxxxxx 7 bits of data and one flag bit
! 21569: ** B = 1xxxxxxx 7 bits of data and one flag bit
! 21570: ** C = xxxxxxxx 8 bits of data
! 21571: **
! 21572: ** 7 bits - A
! 21573: ** 14 bits - BA
! 21574: ** 21 bits - BBA
! 21575: ** 28 bits - BBBA
! 21576: ** 35 bits - BBBBA
! 21577: ** 42 bits - BBBBBA
! 21578: ** 49 bits - BBBBBBA
! 21579: ** 56 bits - BBBBBBBA
! 21580: ** 64 bits - BBBBBBBBC
! 21581: */
! 21582:
! 21583: /*
! 21584: ** Write a 64-bit variable-length integer to memory starting at p[0].
! 21585: ** The length of data write will be between 1 and 9 bytes. The number
! 21586: ** of bytes written is returned.
! 21587: **
! 21588: ** A variable-length integer consists of the lower 7 bits of each byte
! 21589: ** for all bytes that have the 8th bit set and one byte with the 8th
! 21590: ** bit clear. Except, if we get to the 9th byte, it stores the full
! 21591: ** 8 bits and is the last byte.
! 21592: */
! 21593: SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
! 21594: int i, j, n;
! 21595: u8 buf[10];
! 21596: if( v & (((u64)0xff000000)<<32) ){
! 21597: p[8] = (u8)v;
! 21598: v >>= 8;
! 21599: for(i=7; i>=0; i--){
! 21600: p[i] = (u8)((v & 0x7f) | 0x80);
! 21601: v >>= 7;
! 21602: }
! 21603: return 9;
! 21604: }
! 21605: n = 0;
! 21606: do{
! 21607: buf[n++] = (u8)((v & 0x7f) | 0x80);
! 21608: v >>= 7;
! 21609: }while( v!=0 );
! 21610: buf[0] &= 0x7f;
! 21611: assert( n<=9 );
! 21612: for(i=0, j=n-1; j>=0; j--, i++){
! 21613: p[i] = buf[j];
! 21614: }
! 21615: return n;
! 21616: }
! 21617:
! 21618: /*
! 21619: ** This routine is a faster version of sqlite3PutVarint() that only
! 21620: ** works for 32-bit positive integers and which is optimized for
! 21621: ** the common case of small integers. A MACRO version, putVarint32,
! 21622: ** is provided which inlines the single-byte case. All code should use
! 21623: ** the MACRO version as this function assumes the single-byte case has
! 21624: ** already been handled.
! 21625: */
! 21626: SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char *p, u32 v){
! 21627: #ifndef putVarint32
! 21628: if( (v & ~0x7f)==0 ){
! 21629: p[0] = v;
! 21630: return 1;
! 21631: }
! 21632: #endif
! 21633: if( (v & ~0x3fff)==0 ){
! 21634: p[0] = (u8)((v>>7) | 0x80);
! 21635: p[1] = (u8)(v & 0x7f);
! 21636: return 2;
! 21637: }
! 21638: return sqlite3PutVarint(p, v);
! 21639: }
! 21640:
! 21641: /*
! 21642: ** Bitmasks used by sqlite3GetVarint(). These precomputed constants
! 21643: ** are defined here rather than simply putting the constant expressions
! 21644: ** inline in order to work around bugs in the RVT compiler.
! 21645: **
! 21646: ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
! 21647: **
! 21648: ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
! 21649: */
! 21650: #define SLOT_2_0 0x001fc07f
! 21651: #define SLOT_4_2_0 0xf01fc07f
! 21652:
! 21653:
! 21654: /*
! 21655: ** Read a 64-bit variable-length integer from memory starting at p[0].
! 21656: ** Return the number of bytes read. The value is stored in *v.
! 21657: */
! 21658: SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
! 21659: u32 a,b,s;
! 21660:
! 21661: a = *p;
! 21662: /* a: p0 (unmasked) */
! 21663: if (!(a&0x80))
! 21664: {
! 21665: *v = a;
! 21666: return 1;
! 21667: }
! 21668:
! 21669: p++;
! 21670: b = *p;
! 21671: /* b: p1 (unmasked) */
! 21672: if (!(b&0x80))
! 21673: {
! 21674: a &= 0x7f;
! 21675: a = a<<7;
! 21676: a |= b;
! 21677: *v = a;
! 21678: return 2;
! 21679: }
! 21680:
! 21681: /* Verify that constants are precomputed correctly */
! 21682: assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
! 21683: assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
! 21684:
! 21685: p++;
! 21686: a = a<<14;
! 21687: a |= *p;
! 21688: /* a: p0<<14 | p2 (unmasked) */
! 21689: if (!(a&0x80))
! 21690: {
! 21691: a &= SLOT_2_0;
! 21692: b &= 0x7f;
! 21693: b = b<<7;
! 21694: a |= b;
! 21695: *v = a;
! 21696: return 3;
! 21697: }
! 21698:
! 21699: /* CSE1 from below */
! 21700: a &= SLOT_2_0;
! 21701: p++;
! 21702: b = b<<14;
! 21703: b |= *p;
! 21704: /* b: p1<<14 | p3 (unmasked) */
! 21705: if (!(b&0x80))
! 21706: {
! 21707: b &= SLOT_2_0;
! 21708: /* moved CSE1 up */
! 21709: /* a &= (0x7f<<14)|(0x7f); */
! 21710: a = a<<7;
! 21711: a |= b;
! 21712: *v = a;
! 21713: return 4;
! 21714: }
! 21715:
! 21716: /* a: p0<<14 | p2 (masked) */
! 21717: /* b: p1<<14 | p3 (unmasked) */
! 21718: /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
! 21719: /* moved CSE1 up */
! 21720: /* a &= (0x7f<<14)|(0x7f); */
! 21721: b &= SLOT_2_0;
! 21722: s = a;
! 21723: /* s: p0<<14 | p2 (masked) */
! 21724:
! 21725: p++;
! 21726: a = a<<14;
! 21727: a |= *p;
! 21728: /* a: p0<<28 | p2<<14 | p4 (unmasked) */
! 21729: if (!(a&0x80))
! 21730: {
! 21731: /* we can skip these cause they were (effectively) done above in calc'ing s */
! 21732: /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
! 21733: /* b &= (0x7f<<14)|(0x7f); */
! 21734: b = b<<7;
! 21735: a |= b;
! 21736: s = s>>18;
! 21737: *v = ((u64)s)<<32 | a;
! 21738: return 5;
! 21739: }
! 21740:
! 21741: /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
! 21742: s = s<<7;
! 21743: s |= b;
! 21744: /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
! 21745:
! 21746: p++;
! 21747: b = b<<14;
! 21748: b |= *p;
! 21749: /* b: p1<<28 | p3<<14 | p5 (unmasked) */
! 21750: if (!(b&0x80))
! 21751: {
! 21752: /* we can skip this cause it was (effectively) done above in calc'ing s */
! 21753: /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
! 21754: a &= SLOT_2_0;
! 21755: a = a<<7;
! 21756: a |= b;
! 21757: s = s>>18;
! 21758: *v = ((u64)s)<<32 | a;
! 21759: return 6;
! 21760: }
! 21761:
! 21762: p++;
! 21763: a = a<<14;
! 21764: a |= *p;
! 21765: /* a: p2<<28 | p4<<14 | p6 (unmasked) */
! 21766: if (!(a&0x80))
! 21767: {
! 21768: a &= SLOT_4_2_0;
! 21769: b &= SLOT_2_0;
! 21770: b = b<<7;
! 21771: a |= b;
! 21772: s = s>>11;
! 21773: *v = ((u64)s)<<32 | a;
! 21774: return 7;
! 21775: }
! 21776:
! 21777: /* CSE2 from below */
! 21778: a &= SLOT_2_0;
! 21779: p++;
! 21780: b = b<<14;
! 21781: b |= *p;
! 21782: /* b: p3<<28 | p5<<14 | p7 (unmasked) */
! 21783: if (!(b&0x80))
! 21784: {
! 21785: b &= SLOT_4_2_0;
! 21786: /* moved CSE2 up */
! 21787: /* a &= (0x7f<<14)|(0x7f); */
! 21788: a = a<<7;
! 21789: a |= b;
! 21790: s = s>>4;
! 21791: *v = ((u64)s)<<32 | a;
! 21792: return 8;
! 21793: }
! 21794:
! 21795: p++;
! 21796: a = a<<15;
! 21797: a |= *p;
! 21798: /* a: p4<<29 | p6<<15 | p8 (unmasked) */
! 21799:
! 21800: /* moved CSE2 up */
! 21801: /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
! 21802: b &= SLOT_2_0;
! 21803: b = b<<8;
! 21804: a |= b;
! 21805:
! 21806: s = s<<4;
! 21807: b = p[-4];
! 21808: b &= 0x7f;
! 21809: b = b>>3;
! 21810: s |= b;
! 21811:
! 21812: *v = ((u64)s)<<32 | a;
! 21813:
! 21814: return 9;
! 21815: }
! 21816:
! 21817: /*
! 21818: ** Read a 32-bit variable-length integer from memory starting at p[0].
! 21819: ** Return the number of bytes read. The value is stored in *v.
! 21820: **
! 21821: ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
! 21822: ** integer, then set *v to 0xffffffff.
! 21823: **
! 21824: ** A MACRO version, getVarint32, is provided which inlines the
! 21825: ** single-byte case. All code should use the MACRO version as
! 21826: ** this function assumes the single-byte case has already been handled.
! 21827: */
! 21828: SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
! 21829: u32 a,b;
! 21830:
! 21831: /* The 1-byte case. Overwhelmingly the most common. Handled inline
! 21832: ** by the getVarin32() macro */
! 21833: a = *p;
! 21834: /* a: p0 (unmasked) */
! 21835: #ifndef getVarint32
! 21836: if (!(a&0x80))
! 21837: {
! 21838: /* Values between 0 and 127 */
! 21839: *v = a;
! 21840: return 1;
! 21841: }
! 21842: #endif
! 21843:
! 21844: /* The 2-byte case */
! 21845: p++;
! 21846: b = *p;
! 21847: /* b: p1 (unmasked) */
! 21848: if (!(b&0x80))
! 21849: {
! 21850: /* Values between 128 and 16383 */
! 21851: a &= 0x7f;
! 21852: a = a<<7;
! 21853: *v = a | b;
! 21854: return 2;
! 21855: }
! 21856:
! 21857: /* The 3-byte case */
! 21858: p++;
! 21859: a = a<<14;
! 21860: a |= *p;
! 21861: /* a: p0<<14 | p2 (unmasked) */
! 21862: if (!(a&0x80))
! 21863: {
! 21864: /* Values between 16384 and 2097151 */
! 21865: a &= (0x7f<<14)|(0x7f);
! 21866: b &= 0x7f;
! 21867: b = b<<7;
! 21868: *v = a | b;
! 21869: return 3;
! 21870: }
! 21871:
! 21872: /* A 32-bit varint is used to store size information in btrees.
! 21873: ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
! 21874: ** A 3-byte varint is sufficient, for example, to record the size
! 21875: ** of a 1048569-byte BLOB or string.
! 21876: **
! 21877: ** We only unroll the first 1-, 2-, and 3- byte cases. The very
! 21878: ** rare larger cases can be handled by the slower 64-bit varint
! 21879: ** routine.
! 21880: */
! 21881: #if 1
! 21882: {
! 21883: u64 v64;
! 21884: u8 n;
! 21885:
! 21886: p -= 2;
! 21887: n = sqlite3GetVarint(p, &v64);
! 21888: assert( n>3 && n<=9 );
! 21889: if( (v64 & SQLITE_MAX_U32)!=v64 ){
! 21890: *v = 0xffffffff;
! 21891: }else{
! 21892: *v = (u32)v64;
! 21893: }
! 21894: return n;
! 21895: }
! 21896:
! 21897: #else
! 21898: /* For following code (kept for historical record only) shows an
! 21899: ** unrolling for the 3- and 4-byte varint cases. This code is
! 21900: ** slightly faster, but it is also larger and much harder to test.
! 21901: */
! 21902: p++;
! 21903: b = b<<14;
! 21904: b |= *p;
! 21905: /* b: p1<<14 | p3 (unmasked) */
! 21906: if (!(b&0x80))
! 21907: {
! 21908: /* Values between 2097152 and 268435455 */
! 21909: b &= (0x7f<<14)|(0x7f);
! 21910: a &= (0x7f<<14)|(0x7f);
! 21911: a = a<<7;
! 21912: *v = a | b;
! 21913: return 4;
! 21914: }
! 21915:
! 21916: p++;
! 21917: a = a<<14;
! 21918: a |= *p;
! 21919: /* a: p0<<28 | p2<<14 | p4 (unmasked) */
! 21920: if (!(a&0x80))
! 21921: {
! 21922: /* Values between 268435456 and 34359738367 */
! 21923: a &= SLOT_4_2_0;
! 21924: b &= SLOT_4_2_0;
! 21925: b = b<<7;
! 21926: *v = a | b;
! 21927: return 5;
! 21928: }
! 21929:
! 21930: /* We can only reach this point when reading a corrupt database
! 21931: ** file. In that case we are not in any hurry. Use the (relatively
! 21932: ** slow) general-purpose sqlite3GetVarint() routine to extract the
! 21933: ** value. */
! 21934: {
! 21935: u64 v64;
! 21936: u8 n;
! 21937:
! 21938: p -= 4;
! 21939: n = sqlite3GetVarint(p, &v64);
! 21940: assert( n>5 && n<=9 );
! 21941: *v = (u32)v64;
! 21942: return n;
! 21943: }
! 21944: #endif
! 21945: }
! 21946:
! 21947: /*
! 21948: ** Return the number of bytes that will be needed to store the given
! 21949: ** 64-bit integer.
! 21950: */
! 21951: SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
! 21952: int i = 0;
! 21953: do{
! 21954: i++;
! 21955: v >>= 7;
! 21956: }while( v!=0 && ALWAYS(i<9) );
! 21957: return i;
! 21958: }
! 21959:
! 21960:
! 21961: /*
! 21962: ** Read or write a four-byte big-endian integer value.
! 21963: */
! 21964: SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
! 21965: return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
! 21966: }
! 21967: SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
! 21968: p[0] = (u8)(v>>24);
! 21969: p[1] = (u8)(v>>16);
! 21970: p[2] = (u8)(v>>8);
! 21971: p[3] = (u8)v;
! 21972: }
! 21973:
! 21974:
! 21975:
! 21976: /*
! 21977: ** Translate a single byte of Hex into an integer.
! 21978: ** This routine only works if h really is a valid hexadecimal
! 21979: ** character: 0..9a..fA..F
! 21980: */
! 21981: SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
! 21982: assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
! 21983: #ifdef SQLITE_ASCII
! 21984: h += 9*(1&(h>>6));
! 21985: #endif
! 21986: #ifdef SQLITE_EBCDIC
! 21987: h += 9*(1&~(h>>4));
! 21988: #endif
! 21989: return (u8)(h & 0xf);
! 21990: }
! 21991:
! 21992: #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
! 21993: /*
! 21994: ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
! 21995: ** value. Return a pointer to its binary value. Space to hold the
! 21996: ** binary value has been obtained from malloc and must be freed by
! 21997: ** the calling routine.
! 21998: */
! 21999: SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
! 22000: char *zBlob;
! 22001: int i;
! 22002:
! 22003: zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
! 22004: n--;
! 22005: if( zBlob ){
! 22006: for(i=0; i<n; i+=2){
! 22007: zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
! 22008: }
! 22009: zBlob[i/2] = 0;
! 22010: }
! 22011: return zBlob;
! 22012: }
! 22013: #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
! 22014:
! 22015: /*
! 22016: ** Log an error that is an API call on a connection pointer that should
! 22017: ** not have been used. The "type" of connection pointer is given as the
! 22018: ** argument. The zType is a word like "NULL" or "closed" or "invalid".
! 22019: */
! 22020: static void logBadConnection(const char *zType){
! 22021: sqlite3_log(SQLITE_MISUSE,
! 22022: "API call with %s database connection pointer",
! 22023: zType
! 22024: );
! 22025: }
! 22026:
! 22027: /*
! 22028: ** Check to make sure we have a valid db pointer. This test is not
! 22029: ** foolproof but it does provide some measure of protection against
! 22030: ** misuse of the interface such as passing in db pointers that are
! 22031: ** NULL or which have been previously closed. If this routine returns
! 22032: ** 1 it means that the db pointer is valid and 0 if it should not be
! 22033: ** dereferenced for any reason. The calling function should invoke
! 22034: ** SQLITE_MISUSE immediately.
! 22035: **
! 22036: ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
! 22037: ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
! 22038: ** open properly and is not fit for general use but which can be
! 22039: ** used as an argument to sqlite3_errmsg() or sqlite3_close().
! 22040: */
! 22041: SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
! 22042: u32 magic;
! 22043: if( db==0 ){
! 22044: logBadConnection("NULL");
! 22045: return 0;
! 22046: }
! 22047: magic = db->magic;
! 22048: if( magic!=SQLITE_MAGIC_OPEN ){
! 22049: if( sqlite3SafetyCheckSickOrOk(db) ){
! 22050: testcase( sqlite3GlobalConfig.xLog!=0 );
! 22051: logBadConnection("unopened");
! 22052: }
! 22053: return 0;
! 22054: }else{
! 22055: return 1;
! 22056: }
! 22057: }
! 22058: SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
! 22059: u32 magic;
! 22060: magic = db->magic;
! 22061: if( magic!=SQLITE_MAGIC_SICK &&
! 22062: magic!=SQLITE_MAGIC_OPEN &&
! 22063: magic!=SQLITE_MAGIC_BUSY ){
! 22064: testcase( sqlite3GlobalConfig.xLog!=0 );
! 22065: logBadConnection("invalid");
! 22066: return 0;
! 22067: }else{
! 22068: return 1;
! 22069: }
! 22070: }
! 22071:
! 22072: /*
! 22073: ** Attempt to add, substract, or multiply the 64-bit signed value iB against
! 22074: ** the other 64-bit signed integer at *pA and store the result in *pA.
! 22075: ** Return 0 on success. Or if the operation would have resulted in an
! 22076: ** overflow, leave *pA unchanged and return 1.
! 22077: */
! 22078: SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
! 22079: i64 iA = *pA;
! 22080: testcase( iA==0 ); testcase( iA==1 );
! 22081: testcase( iB==-1 ); testcase( iB==0 );
! 22082: if( iB>=0 ){
! 22083: testcase( iA>0 && LARGEST_INT64 - iA == iB );
! 22084: testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
! 22085: if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
! 22086: *pA += iB;
! 22087: }else{
! 22088: testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
! 22089: testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
! 22090: if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
! 22091: *pA += iB;
! 22092: }
! 22093: return 0;
! 22094: }
! 22095: SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
! 22096: testcase( iB==SMALLEST_INT64+1 );
! 22097: if( iB==SMALLEST_INT64 ){
! 22098: testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
! 22099: if( (*pA)>=0 ) return 1;
! 22100: *pA -= iB;
! 22101: return 0;
! 22102: }else{
! 22103: return sqlite3AddInt64(pA, -iB);
! 22104: }
! 22105: }
! 22106: #define TWOPOWER32 (((i64)1)<<32)
! 22107: #define TWOPOWER31 (((i64)1)<<31)
! 22108: SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
! 22109: i64 iA = *pA;
! 22110: i64 iA1, iA0, iB1, iB0, r;
! 22111:
! 22112: iA1 = iA/TWOPOWER32;
! 22113: iA0 = iA % TWOPOWER32;
! 22114: iB1 = iB/TWOPOWER32;
! 22115: iB0 = iB % TWOPOWER32;
! 22116: if( iA1*iB1 != 0 ) return 1;
! 22117: assert( iA1*iB0==0 || iA0*iB1==0 );
! 22118: r = iA1*iB0 + iA0*iB1;
! 22119: testcase( r==(-TWOPOWER31)-1 );
! 22120: testcase( r==(-TWOPOWER31) );
! 22121: testcase( r==TWOPOWER31 );
! 22122: testcase( r==TWOPOWER31-1 );
! 22123: if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
! 22124: r *= TWOPOWER32;
! 22125: if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
! 22126: *pA = r;
! 22127: return 0;
! 22128: }
! 22129:
! 22130: /*
! 22131: ** Compute the absolute value of a 32-bit signed integer, of possible. Or
! 22132: ** if the integer has a value of -2147483648, return +2147483647
! 22133: */
! 22134: SQLITE_PRIVATE int sqlite3AbsInt32(int x){
! 22135: if( x>=0 ) return x;
! 22136: if( x==(int)0x80000000 ) return 0x7fffffff;
! 22137: return -x;
! 22138: }
! 22139:
! 22140: #ifdef SQLITE_ENABLE_8_3_NAMES
! 22141: /*
! 22142: ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
! 22143: ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
! 22144: ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
! 22145: ** three characters, then shorten the suffix on z[] to be the last three
! 22146: ** characters of the original suffix.
! 22147: **
! 22148: ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
! 22149: ** do the suffix shortening regardless of URI parameter.
! 22150: **
! 22151: ** Examples:
! 22152: **
! 22153: ** test.db-journal => test.nal
! 22154: ** test.db-wal => test.wal
! 22155: ** test.db-shm => test.shm
! 22156: ** test.db-mj7f3319fa => test.9fa
! 22157: */
! 22158: SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
! 22159: #if SQLITE_ENABLE_8_3_NAMES<2
! 22160: if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
! 22161: #endif
! 22162: {
! 22163: int i, sz;
! 22164: sz = sqlite3Strlen30(z);
! 22165: for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
! 22166: if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
! 22167: }
! 22168: }
! 22169: #endif
! 22170:
! 22171: /************** End of util.c ************************************************/
! 22172: /************** Begin file hash.c ********************************************/
! 22173: /*
! 22174: ** 2001 September 22
! 22175: **
! 22176: ** The author disclaims copyright to this source code. In place of
! 22177: ** a legal notice, here is a blessing:
! 22178: **
! 22179: ** May you do good and not evil.
! 22180: ** May you find forgiveness for yourself and forgive others.
! 22181: ** May you share freely, never taking more than you give.
! 22182: **
! 22183: *************************************************************************
! 22184: ** This is the implementation of generic hash-tables
! 22185: ** used in SQLite.
! 22186: */
! 22187: /* #include <assert.h> */
! 22188:
! 22189: /* Turn bulk memory into a hash table object by initializing the
! 22190: ** fields of the Hash structure.
! 22191: **
! 22192: ** "pNew" is a pointer to the hash table that is to be initialized.
! 22193: */
! 22194: SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
! 22195: assert( pNew!=0 );
! 22196: pNew->first = 0;
! 22197: pNew->count = 0;
! 22198: pNew->htsize = 0;
! 22199: pNew->ht = 0;
! 22200: }
! 22201:
! 22202: /* Remove all entries from a hash table. Reclaim all memory.
! 22203: ** Call this routine to delete a hash table or to reset a hash table
! 22204: ** to the empty state.
! 22205: */
! 22206: SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
! 22207: HashElem *elem; /* For looping over all elements of the table */
! 22208:
! 22209: assert( pH!=0 );
! 22210: elem = pH->first;
! 22211: pH->first = 0;
! 22212: sqlite3_free(pH->ht);
! 22213: pH->ht = 0;
! 22214: pH->htsize = 0;
! 22215: while( elem ){
! 22216: HashElem *next_elem = elem->next;
! 22217: sqlite3_free(elem);
! 22218: elem = next_elem;
! 22219: }
! 22220: pH->count = 0;
! 22221: }
! 22222:
! 22223: /*
! 22224: ** The hashing function.
! 22225: */
! 22226: static unsigned int strHash(const char *z, int nKey){
! 22227: int h = 0;
! 22228: assert( nKey>=0 );
! 22229: while( nKey > 0 ){
! 22230: h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
! 22231: nKey--;
! 22232: }
! 22233: return h;
! 22234: }
! 22235:
! 22236:
! 22237: /* Link pNew element into the hash table pH. If pEntry!=0 then also
! 22238: ** insert pNew into the pEntry hash bucket.
! 22239: */
! 22240: static void insertElement(
! 22241: Hash *pH, /* The complete hash table */
! 22242: struct _ht *pEntry, /* The entry into which pNew is inserted */
! 22243: HashElem *pNew /* The element to be inserted */
! 22244: ){
! 22245: HashElem *pHead; /* First element already in pEntry */
! 22246: if( pEntry ){
! 22247: pHead = pEntry->count ? pEntry->chain : 0;
! 22248: pEntry->count++;
! 22249: pEntry->chain = pNew;
! 22250: }else{
! 22251: pHead = 0;
! 22252: }
! 22253: if( pHead ){
! 22254: pNew->next = pHead;
! 22255: pNew->prev = pHead->prev;
! 22256: if( pHead->prev ){ pHead->prev->next = pNew; }
! 22257: else { pH->first = pNew; }
! 22258: pHead->prev = pNew;
! 22259: }else{
! 22260: pNew->next = pH->first;
! 22261: if( pH->first ){ pH->first->prev = pNew; }
! 22262: pNew->prev = 0;
! 22263: pH->first = pNew;
! 22264: }
! 22265: }
! 22266:
! 22267:
! 22268: /* Resize the hash table so that it cantains "new_size" buckets.
! 22269: **
! 22270: ** The hash table might fail to resize if sqlite3_malloc() fails or
! 22271: ** if the new size is the same as the prior size.
! 22272: ** Return TRUE if the resize occurs and false if not.
! 22273: */
! 22274: static int rehash(Hash *pH, unsigned int new_size){
! 22275: struct _ht *new_ht; /* The new hash table */
! 22276: HashElem *elem, *next_elem; /* For looping over existing elements */
! 22277:
! 22278: #if SQLITE_MALLOC_SOFT_LIMIT>0
! 22279: if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
! 22280: new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
! 22281: }
! 22282: if( new_size==pH->htsize ) return 0;
! 22283: #endif
! 22284:
! 22285: /* The inability to allocates space for a larger hash table is
! 22286: ** a performance hit but it is not a fatal error. So mark the
! 22287: ** allocation as a benign.
! 22288: */
! 22289: sqlite3BeginBenignMalloc();
! 22290: new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
! 22291: sqlite3EndBenignMalloc();
! 22292:
! 22293: if( new_ht==0 ) return 0;
! 22294: sqlite3_free(pH->ht);
! 22295: pH->ht = new_ht;
! 22296: pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
! 22297: memset(new_ht, 0, new_size*sizeof(struct _ht));
! 22298: for(elem=pH->first, pH->first=0; elem; elem = next_elem){
! 22299: unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
! 22300: next_elem = elem->next;
! 22301: insertElement(pH, &new_ht[h], elem);
! 22302: }
! 22303: return 1;
! 22304: }
! 22305:
! 22306: /* This function (for internal use only) locates an element in an
! 22307: ** hash table that matches the given key. The hash for this key has
! 22308: ** already been computed and is passed as the 4th parameter.
! 22309: */
! 22310: static HashElem *findElementGivenHash(
! 22311: const Hash *pH, /* The pH to be searched */
! 22312: const char *pKey, /* The key we are searching for */
! 22313: int nKey, /* Bytes in key (not counting zero terminator) */
! 22314: unsigned int h /* The hash for this key. */
! 22315: ){
! 22316: HashElem *elem; /* Used to loop thru the element list */
! 22317: int count; /* Number of elements left to test */
! 22318:
! 22319: if( pH->ht ){
! 22320: struct _ht *pEntry = &pH->ht[h];
! 22321: elem = pEntry->chain;
! 22322: count = pEntry->count;
! 22323: }else{
! 22324: elem = pH->first;
! 22325: count = pH->count;
! 22326: }
! 22327: while( count-- && ALWAYS(elem) ){
! 22328: if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){
! 22329: return elem;
! 22330: }
! 22331: elem = elem->next;
! 22332: }
! 22333: return 0;
! 22334: }
! 22335:
! 22336: /* Remove a single entry from the hash table given a pointer to that
! 22337: ** element and a hash on the element's key.
! 22338: */
! 22339: static void removeElementGivenHash(
! 22340: Hash *pH, /* The pH containing "elem" */
! 22341: HashElem* elem, /* The element to be removed from the pH */
! 22342: unsigned int h /* Hash value for the element */
! 22343: ){
! 22344: struct _ht *pEntry;
! 22345: if( elem->prev ){
! 22346: elem->prev->next = elem->next;
! 22347: }else{
! 22348: pH->first = elem->next;
! 22349: }
! 22350: if( elem->next ){
! 22351: elem->next->prev = elem->prev;
! 22352: }
! 22353: if( pH->ht ){
! 22354: pEntry = &pH->ht[h];
! 22355: if( pEntry->chain==elem ){
! 22356: pEntry->chain = elem->next;
! 22357: }
! 22358: pEntry->count--;
! 22359: assert( pEntry->count>=0 );
! 22360: }
! 22361: sqlite3_free( elem );
! 22362: pH->count--;
! 22363: if( pH->count<=0 ){
! 22364: assert( pH->first==0 );
! 22365: assert( pH->count==0 );
! 22366: sqlite3HashClear(pH);
! 22367: }
! 22368: }
! 22369:
! 22370: /* Attempt to locate an element of the hash table pH with a key
! 22371: ** that matches pKey,nKey. Return the data for this element if it is
! 22372: ** found, or NULL if there is no match.
! 22373: */
! 22374: SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
! 22375: HashElem *elem; /* The element that matches key */
! 22376: unsigned int h; /* A hash on key */
! 22377:
! 22378: assert( pH!=0 );
! 22379: assert( pKey!=0 );
! 22380: assert( nKey>=0 );
! 22381: if( pH->ht ){
! 22382: h = strHash(pKey, nKey) % pH->htsize;
! 22383: }else{
! 22384: h = 0;
! 22385: }
! 22386: elem = findElementGivenHash(pH, pKey, nKey, h);
! 22387: return elem ? elem->data : 0;
! 22388: }
! 22389:
! 22390: /* Insert an element into the hash table pH. The key is pKey,nKey
! 22391: ** and the data is "data".
! 22392: **
! 22393: ** If no element exists with a matching key, then a new
! 22394: ** element is created and NULL is returned.
! 22395: **
! 22396: ** If another element already exists with the same key, then the
! 22397: ** new data replaces the old data and the old data is returned.
! 22398: ** The key is not copied in this instance. If a malloc fails, then
! 22399: ** the new data is returned and the hash table is unchanged.
! 22400: **
! 22401: ** If the "data" parameter to this function is NULL, then the
! 22402: ** element corresponding to "key" is removed from the hash table.
! 22403: */
! 22404: SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
! 22405: unsigned int h; /* the hash of the key modulo hash table size */
! 22406: HashElem *elem; /* Used to loop thru the element list */
! 22407: HashElem *new_elem; /* New element added to the pH */
! 22408:
! 22409: assert( pH!=0 );
! 22410: assert( pKey!=0 );
! 22411: assert( nKey>=0 );
! 22412: if( pH->htsize ){
! 22413: h = strHash(pKey, nKey) % pH->htsize;
! 22414: }else{
! 22415: h = 0;
! 22416: }
! 22417: elem = findElementGivenHash(pH,pKey,nKey,h);
! 22418: if( elem ){
! 22419: void *old_data = elem->data;
! 22420: if( data==0 ){
! 22421: removeElementGivenHash(pH,elem,h);
! 22422: }else{
! 22423: elem->data = data;
! 22424: elem->pKey = pKey;
! 22425: assert(nKey==elem->nKey);
! 22426: }
! 22427: return old_data;
! 22428: }
! 22429: if( data==0 ) return 0;
! 22430: new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
! 22431: if( new_elem==0 ) return data;
! 22432: new_elem->pKey = pKey;
! 22433: new_elem->nKey = nKey;
! 22434: new_elem->data = data;
! 22435: pH->count++;
! 22436: if( pH->count>=10 && pH->count > 2*pH->htsize ){
! 22437: if( rehash(pH, pH->count*2) ){
! 22438: assert( pH->htsize>0 );
! 22439: h = strHash(pKey, nKey) % pH->htsize;
! 22440: }
! 22441: }
! 22442: if( pH->ht ){
! 22443: insertElement(pH, &pH->ht[h], new_elem);
! 22444: }else{
! 22445: insertElement(pH, 0, new_elem);
! 22446: }
! 22447: return 0;
! 22448: }
! 22449:
! 22450: /************** End of hash.c ************************************************/
! 22451: /************** Begin file opcodes.c *****************************************/
! 22452: /* Automatically generated. Do not edit */
! 22453: /* See the mkopcodec.awk script for details. */
! 22454: #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
! 22455: SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
! 22456: static const char *const azName[] = { "?",
! 22457: /* 1 */ "Goto",
! 22458: /* 2 */ "Gosub",
! 22459: /* 3 */ "Return",
! 22460: /* 4 */ "Yield",
! 22461: /* 5 */ "HaltIfNull",
! 22462: /* 6 */ "Halt",
! 22463: /* 7 */ "Integer",
! 22464: /* 8 */ "Int64",
! 22465: /* 9 */ "String",
! 22466: /* 10 */ "Null",
! 22467: /* 11 */ "Blob",
! 22468: /* 12 */ "Variable",
! 22469: /* 13 */ "Move",
! 22470: /* 14 */ "Copy",
! 22471: /* 15 */ "SCopy",
! 22472: /* 16 */ "ResultRow",
! 22473: /* 17 */ "CollSeq",
! 22474: /* 18 */ "Function",
! 22475: /* 19 */ "Not",
! 22476: /* 20 */ "AddImm",
! 22477: /* 21 */ "MustBeInt",
! 22478: /* 22 */ "RealAffinity",
! 22479: /* 23 */ "Permutation",
! 22480: /* 24 */ "Compare",
! 22481: /* 25 */ "Jump",
! 22482: /* 26 */ "Once",
! 22483: /* 27 */ "If",
! 22484: /* 28 */ "IfNot",
! 22485: /* 29 */ "Column",
! 22486: /* 30 */ "Affinity",
! 22487: /* 31 */ "MakeRecord",
! 22488: /* 32 */ "Count",
! 22489: /* 33 */ "Savepoint",
! 22490: /* 34 */ "AutoCommit",
! 22491: /* 35 */ "Transaction",
! 22492: /* 36 */ "ReadCookie",
! 22493: /* 37 */ "SetCookie",
! 22494: /* 38 */ "VerifyCookie",
! 22495: /* 39 */ "OpenRead",
! 22496: /* 40 */ "OpenWrite",
! 22497: /* 41 */ "OpenAutoindex",
! 22498: /* 42 */ "OpenEphemeral",
! 22499: /* 43 */ "SorterOpen",
! 22500: /* 44 */ "OpenPseudo",
! 22501: /* 45 */ "Close",
! 22502: /* 46 */ "SeekLt",
! 22503: /* 47 */ "SeekLe",
! 22504: /* 48 */ "SeekGe",
! 22505: /* 49 */ "SeekGt",
! 22506: /* 50 */ "Seek",
! 22507: /* 51 */ "NotFound",
! 22508: /* 52 */ "Found",
! 22509: /* 53 */ "IsUnique",
! 22510: /* 54 */ "NotExists",
! 22511: /* 55 */ "Sequence",
! 22512: /* 56 */ "NewRowid",
! 22513: /* 57 */ "Insert",
! 22514: /* 58 */ "InsertInt",
! 22515: /* 59 */ "Delete",
! 22516: /* 60 */ "ResetCount",
! 22517: /* 61 */ "SorterCompare",
! 22518: /* 62 */ "SorterData",
! 22519: /* 63 */ "RowKey",
! 22520: /* 64 */ "RowData",
! 22521: /* 65 */ "Rowid",
! 22522: /* 66 */ "NullRow",
! 22523: /* 67 */ "Last",
! 22524: /* 68 */ "Or",
! 22525: /* 69 */ "And",
! 22526: /* 70 */ "SorterSort",
! 22527: /* 71 */ "Sort",
! 22528: /* 72 */ "Rewind",
! 22529: /* 73 */ "IsNull",
! 22530: /* 74 */ "NotNull",
! 22531: /* 75 */ "Ne",
! 22532: /* 76 */ "Eq",
! 22533: /* 77 */ "Gt",
! 22534: /* 78 */ "Le",
! 22535: /* 79 */ "Lt",
! 22536: /* 80 */ "Ge",
! 22537: /* 81 */ "SorterNext",
! 22538: /* 82 */ "BitAnd",
! 22539: /* 83 */ "BitOr",
! 22540: /* 84 */ "ShiftLeft",
! 22541: /* 85 */ "ShiftRight",
! 22542: /* 86 */ "Add",
! 22543: /* 87 */ "Subtract",
! 22544: /* 88 */ "Multiply",
! 22545: /* 89 */ "Divide",
! 22546: /* 90 */ "Remainder",
! 22547: /* 91 */ "Concat",
! 22548: /* 92 */ "Prev",
! 22549: /* 93 */ "BitNot",
! 22550: /* 94 */ "String8",
! 22551: /* 95 */ "Next",
! 22552: /* 96 */ "SorterInsert",
! 22553: /* 97 */ "IdxInsert",
! 22554: /* 98 */ "IdxDelete",
! 22555: /* 99 */ "IdxRowid",
! 22556: /* 100 */ "IdxLT",
! 22557: /* 101 */ "IdxGE",
! 22558: /* 102 */ "Destroy",
! 22559: /* 103 */ "Clear",
! 22560: /* 104 */ "CreateIndex",
! 22561: /* 105 */ "CreateTable",
! 22562: /* 106 */ "ParseSchema",
! 22563: /* 107 */ "LoadAnalysis",
! 22564: /* 108 */ "DropTable",
! 22565: /* 109 */ "DropIndex",
! 22566: /* 110 */ "DropTrigger",
! 22567: /* 111 */ "IntegrityCk",
! 22568: /* 112 */ "RowSetAdd",
! 22569: /* 113 */ "RowSetRead",
! 22570: /* 114 */ "RowSetTest",
! 22571: /* 115 */ "Program",
! 22572: /* 116 */ "Param",
! 22573: /* 117 */ "FkCounter",
! 22574: /* 118 */ "FkIfZero",
! 22575: /* 119 */ "MemMax",
! 22576: /* 120 */ "IfPos",
! 22577: /* 121 */ "IfNeg",
! 22578: /* 122 */ "IfZero",
! 22579: /* 123 */ "AggStep",
! 22580: /* 124 */ "AggFinal",
! 22581: /* 125 */ "Checkpoint",
! 22582: /* 126 */ "JournalMode",
! 22583: /* 127 */ "Vacuum",
! 22584: /* 128 */ "IncrVacuum",
! 22585: /* 129 */ "Expire",
! 22586: /* 130 */ "Real",
! 22587: /* 131 */ "TableLock",
! 22588: /* 132 */ "VBegin",
! 22589: /* 133 */ "VCreate",
! 22590: /* 134 */ "VDestroy",
! 22591: /* 135 */ "VOpen",
! 22592: /* 136 */ "VFilter",
! 22593: /* 137 */ "VColumn",
! 22594: /* 138 */ "VNext",
! 22595: /* 139 */ "VRename",
! 22596: /* 140 */ "VUpdate",
! 22597: /* 141 */ "ToText",
! 22598: /* 142 */ "ToBlob",
! 22599: /* 143 */ "ToNumeric",
! 22600: /* 144 */ "ToInt",
! 22601: /* 145 */ "ToReal",
! 22602: /* 146 */ "Pagecount",
! 22603: /* 147 */ "MaxPgcnt",
! 22604: /* 148 */ "Trace",
! 22605: /* 149 */ "Noop",
! 22606: /* 150 */ "Explain",
! 22607: };
! 22608: return azName[i];
! 22609: }
! 22610: #endif
! 22611:
! 22612: /************** End of opcodes.c *********************************************/
! 22613: /************** Begin file os_os2.c ******************************************/
! 22614: /*
! 22615: ** 2006 Feb 14
! 22616: **
! 22617: ** The author disclaims copyright to this source code. In place of
! 22618: ** a legal notice, here is a blessing:
! 22619: **
! 22620: ** May you do good and not evil.
! 22621: ** May you find forgiveness for yourself and forgive others.
! 22622: ** May you share freely, never taking more than you give.
! 22623: **
! 22624: ******************************************************************************
! 22625: **
! 22626: ** This file contains code that is specific to OS/2.
! 22627: */
! 22628:
! 22629:
! 22630: #if SQLITE_OS_OS2
! 22631:
! 22632: /*
! 22633: ** A Note About Memory Allocation:
! 22634: **
! 22635: ** This driver uses malloc()/free() directly rather than going through
! 22636: ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free(). Those wrappers
! 22637: ** are designed for use on embedded systems where memory is scarce and
! 22638: ** malloc failures happen frequently. OS/2 does not typically run on
! 22639: ** embedded systems, and when it does the developers normally have bigger
! 22640: ** problems to worry about than running out of memory. So there is not
! 22641: ** a compelling need to use the wrappers.
! 22642: **
! 22643: ** But there is a good reason to not use the wrappers. If we use the
! 22644: ** wrappers then we will get simulated malloc() failures within this
! 22645: ** driver. And that causes all kinds of problems for our tests. We
! 22646: ** could enhance SQLite to deal with simulated malloc failures within
! 22647: ** the OS driver, but the code to deal with those failure would not
! 22648: ** be exercised on Linux (which does not need to malloc() in the driver)
! 22649: ** and so we would have difficulty writing coverage tests for that
! 22650: ** code. Better to leave the code out, we think.
! 22651: **
! 22652: ** The point of this discussion is as follows: When creating a new
! 22653: ** OS layer for an embedded system, if you use this file as an example,
! 22654: ** avoid the use of malloc()/free(). Those routines work ok on OS/2
! 22655: ** desktops but not so well in embedded systems.
! 22656: */
! 22657:
! 22658: /*
! 22659: ** Macros used to determine whether or not to use threads.
! 22660: */
! 22661: #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
! 22662: # define SQLITE_OS2_THREADS 1
! 22663: #endif
! 22664:
! 22665: /*
! 22666: ** Include code that is common to all os_*.c files
! 22667: */
! 22668: /************** Include os_common.h in the middle of os_os2.c ****************/
! 22669: /************** Begin file os_common.h ***************************************/
! 22670: /*
! 22671: ** 2004 May 22
! 22672: **
! 22673: ** The author disclaims copyright to this source code. In place of
! 22674: ** a legal notice, here is a blessing:
! 22675: **
! 22676: ** May you do good and not evil.
! 22677: ** May you find forgiveness for yourself and forgive others.
! 22678: ** May you share freely, never taking more than you give.
! 22679: **
! 22680: ******************************************************************************
! 22681: **
! 22682: ** This file contains macros and a little bit of code that is common to
! 22683: ** all of the platform-specific files (os_*.c) and is #included into those
! 22684: ** files.
! 22685: **
! 22686: ** This file should be #included by the os_*.c files only. It is not a
! 22687: ** general purpose header file.
! 22688: */
! 22689: #ifndef _OS_COMMON_H_
! 22690: #define _OS_COMMON_H_
! 22691:
! 22692: /*
! 22693: ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
! 22694: ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
! 22695: ** switch. The following code should catch this problem at compile-time.
! 22696: */
! 22697: #ifdef MEMORY_DEBUG
! 22698: # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
! 22699: #endif
! 22700:
! 22701: #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
! 22702: # ifndef SQLITE_DEBUG_OS_TRACE
! 22703: # define SQLITE_DEBUG_OS_TRACE 0
! 22704: # endif
! 22705: int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
! 22706: # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
! 22707: #else
! 22708: # define OSTRACE(X)
! 22709: #endif
! 22710:
! 22711: /*
! 22712: ** Macros for performance tracing. Normally turned off. Only works
! 22713: ** on i486 hardware.
! 22714: */
! 22715: #ifdef SQLITE_PERFORMANCE_TRACE
! 22716:
! 22717: /*
! 22718: ** hwtime.h contains inline assembler code for implementing
! 22719: ** high-performance timing routines.
! 22720: */
! 22721: /************** Include hwtime.h in the middle of os_common.h ****************/
! 22722: /************** Begin file hwtime.h ******************************************/
! 22723: /*
! 22724: ** 2008 May 27
! 22725: **
! 22726: ** The author disclaims copyright to this source code. In place of
! 22727: ** a legal notice, here is a blessing:
! 22728: **
! 22729: ** May you do good and not evil.
! 22730: ** May you find forgiveness for yourself and forgive others.
! 22731: ** May you share freely, never taking more than you give.
! 22732: **
! 22733: ******************************************************************************
! 22734: **
! 22735: ** This file contains inline asm code for retrieving "high-performance"
! 22736: ** counters for x86 class CPUs.
! 22737: */
! 22738: #ifndef _HWTIME_H_
! 22739: #define _HWTIME_H_
! 22740:
! 22741: /*
! 22742: ** The following routine only works on pentium-class (or newer) processors.
! 22743: ** It uses the RDTSC opcode to read the cycle count value out of the
! 22744: ** processor and returns that value. This can be used for high-res
! 22745: ** profiling.
! 22746: */
! 22747: #if (defined(__GNUC__) || defined(_MSC_VER)) && \
! 22748: (defined(i386) || defined(__i386__) || defined(_M_IX86))
! 22749:
! 22750: #if defined(__GNUC__)
! 22751:
! 22752: __inline__ sqlite_uint64 sqlite3Hwtime(void){
! 22753: unsigned int lo, hi;
! 22754: __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
! 22755: return (sqlite_uint64)hi << 32 | lo;
! 22756: }
! 22757:
! 22758: #elif defined(_MSC_VER)
! 22759:
! 22760: __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
! 22761: __asm {
! 22762: rdtsc
! 22763: ret ; return value at EDX:EAX
! 22764: }
! 22765: }
! 22766:
! 22767: #endif
! 22768:
! 22769: #elif (defined(__GNUC__) && defined(__x86_64__))
! 22770:
! 22771: __inline__ sqlite_uint64 sqlite3Hwtime(void){
! 22772: unsigned long val;
! 22773: __asm__ __volatile__ ("rdtsc" : "=A" (val));
! 22774: return val;
! 22775: }
! 22776:
! 22777: #elif (defined(__GNUC__) && defined(__ppc__))
! 22778:
! 22779: __inline__ sqlite_uint64 sqlite3Hwtime(void){
! 22780: unsigned long long retval;
! 22781: unsigned long junk;
! 22782: __asm__ __volatile__ ("\n\
! 22783: 1: mftbu %1\n\
! 22784: mftb %L0\n\
! 22785: mftbu %0\n\
! 22786: cmpw %0,%1\n\
! 22787: bne 1b"
! 22788: : "=r" (retval), "=r" (junk));
! 22789: return retval;
! 22790: }
! 22791:
! 22792: #else
! 22793:
! 22794: #error Need implementation of sqlite3Hwtime() for your platform.
! 22795:
! 22796: /*
! 22797: ** To compile without implementing sqlite3Hwtime() for your platform,
! 22798: ** you can remove the above #error and use the following
! 22799: ** stub function. You will lose timing support for many
! 22800: ** of the debugging and testing utilities, but it should at
! 22801: ** least compile and run.
! 22802: */
! 22803: SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
! 22804:
! 22805: #endif
! 22806:
! 22807: #endif /* !defined(_HWTIME_H_) */
! 22808:
! 22809: /************** End of hwtime.h **********************************************/
! 22810: /************** Continuing where we left off in os_common.h ******************/
! 22811:
! 22812: static sqlite_uint64 g_start;
! 22813: static sqlite_uint64 g_elapsed;
! 22814: #define TIMER_START g_start=sqlite3Hwtime()
! 22815: #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
! 22816: #define TIMER_ELAPSED g_elapsed
! 22817: #else
! 22818: #define TIMER_START
! 22819: #define TIMER_END
! 22820: #define TIMER_ELAPSED ((sqlite_uint64)0)
! 22821: #endif
! 22822:
! 22823: /*
! 22824: ** If we compile with the SQLITE_TEST macro set, then the following block
! 22825: ** of code will give us the ability to simulate a disk I/O error. This
! 22826: ** is used for testing the I/O recovery logic.
! 22827: */
! 22828: #ifdef SQLITE_TEST
! 22829: SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
! 22830: SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
! 22831: SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
! 22832: SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
! 22833: SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
! 22834: SQLITE_API int sqlite3_diskfull_pending = 0;
! 22835: SQLITE_API int sqlite3_diskfull = 0;
! 22836: #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
! 22837: #define SimulateIOError(CODE) \
! 22838: if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
! 22839: || sqlite3_io_error_pending-- == 1 ) \
! 22840: { local_ioerr(); CODE; }
! 22841: static void local_ioerr(){
! 22842: IOTRACE(("IOERR\n"));
! 22843: sqlite3_io_error_hit++;
! 22844: if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
! 22845: }
! 22846: #define SimulateDiskfullError(CODE) \
! 22847: if( sqlite3_diskfull_pending ){ \
! 22848: if( sqlite3_diskfull_pending == 1 ){ \
! 22849: local_ioerr(); \
! 22850: sqlite3_diskfull = 1; \
! 22851: sqlite3_io_error_hit = 1; \
! 22852: CODE; \
! 22853: }else{ \
! 22854: sqlite3_diskfull_pending--; \
! 22855: } \
! 22856: }
! 22857: #else
! 22858: #define SimulateIOErrorBenign(X)
! 22859: #define SimulateIOError(A)
! 22860: #define SimulateDiskfullError(A)
! 22861: #endif
! 22862:
! 22863: /*
! 22864: ** When testing, keep a count of the number of open files.
! 22865: */
! 22866: #ifdef SQLITE_TEST
! 22867: SQLITE_API int sqlite3_open_file_count = 0;
! 22868: #define OpenCounter(X) sqlite3_open_file_count+=(X)
! 22869: #else
! 22870: #define OpenCounter(X)
! 22871: #endif
! 22872:
! 22873: #endif /* !defined(_OS_COMMON_H_) */
! 22874:
! 22875: /************** End of os_common.h *******************************************/
! 22876: /************** Continuing where we left off in os_os2.c *********************/
! 22877:
! 22878: /* Forward references */
! 22879: typedef struct os2File os2File; /* The file structure */
! 22880: typedef struct os2ShmNode os2ShmNode; /* A shared descritive memory node */
! 22881: typedef struct os2ShmLink os2ShmLink; /* A connection to shared-memory */
! 22882:
! 22883: /*
! 22884: ** The os2File structure is subclass of sqlite3_file specific for the OS/2
! 22885: ** protability layer.
! 22886: */
! 22887: struct os2File {
! 22888: const sqlite3_io_methods *pMethod; /* Always the first entry */
! 22889: HFILE h; /* Handle for accessing the file */
! 22890: int flags; /* Flags provided to os2Open() */
! 22891: int locktype; /* Type of lock currently held on this file */
! 22892: int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
! 22893: char *zFullPathCp; /* Full path name of this file */
! 22894: os2ShmLink *pShmLink; /* Instance of shared memory on this file */
! 22895: };
! 22896:
! 22897: #define LOCK_TIMEOUT 10L /* the default locking timeout */
! 22898:
! 22899: /*
! 22900: ** Missing from some versions of the OS/2 toolkit -
! 22901: ** used to allocate from high memory if possible
! 22902: */
! 22903: #ifndef OBJ_ANY
! 22904: # define OBJ_ANY 0x00000400
! 22905: #endif
! 22906:
! 22907: /*****************************************************************************
! 22908: ** The next group of routines implement the I/O methods specified
! 22909: ** by the sqlite3_io_methods object.
! 22910: ******************************************************************************/
! 22911:
! 22912: /*
! 22913: ** Close a file.
! 22914: */
! 22915: static int os2Close( sqlite3_file *id ){
! 22916: APIRET rc;
! 22917: os2File *pFile = (os2File*)id;
! 22918:
! 22919: assert( id!=0 );
! 22920: OSTRACE(( "CLOSE %d (%s)\n", pFile->h, pFile->zFullPathCp ));
! 22921:
! 22922: rc = DosClose( pFile->h );
! 22923:
! 22924: if( pFile->flags & SQLITE_OPEN_DELETEONCLOSE )
! 22925: DosForceDelete( (PSZ)pFile->zFullPathCp );
! 22926:
! 22927: free( pFile->zFullPathCp );
! 22928: pFile->zFullPathCp = NULL;
! 22929: pFile->locktype = NO_LOCK;
! 22930: pFile->h = (HFILE)-1;
! 22931: pFile->flags = 0;
! 22932:
! 22933: OpenCounter( -1 );
! 22934: return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
! 22935: }
! 22936:
! 22937: /*
! 22938: ** Read data from a file into a buffer. Return SQLITE_OK if all
! 22939: ** bytes were read successfully and SQLITE_IOERR if anything goes
! 22940: ** wrong.
! 22941: */
! 22942: static int os2Read(
! 22943: sqlite3_file *id, /* File to read from */
! 22944: void *pBuf, /* Write content into this buffer */
! 22945: int amt, /* Number of bytes to read */
! 22946: sqlite3_int64 offset /* Begin reading at this offset */
! 22947: ){
! 22948: ULONG fileLocation = 0L;
! 22949: ULONG got;
! 22950: os2File *pFile = (os2File*)id;
! 22951: assert( id!=0 );
! 22952: SimulateIOError( return SQLITE_IOERR_READ );
! 22953: OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
! 22954: if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
! 22955: return SQLITE_IOERR;
! 22956: }
! 22957: if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
! 22958: return SQLITE_IOERR_READ;
! 22959: }
! 22960: if( got == (ULONG)amt )
! 22961: return SQLITE_OK;
! 22962: else {
! 22963: /* Unread portions of the input buffer must be zero-filled */
! 22964: memset(&((char*)pBuf)[got], 0, amt-got);
! 22965: return SQLITE_IOERR_SHORT_READ;
! 22966: }
! 22967: }
! 22968:
! 22969: /*
! 22970: ** Write data from a buffer into a file. Return SQLITE_OK on success
! 22971: ** or some other error code on failure.
! 22972: */
! 22973: static int os2Write(
! 22974: sqlite3_file *id, /* File to write into */
! 22975: const void *pBuf, /* The bytes to be written */
! 22976: int amt, /* Number of bytes to write */
! 22977: sqlite3_int64 offset /* Offset into the file to begin writing at */
! 22978: ){
! 22979: ULONG fileLocation = 0L;
! 22980: APIRET rc = NO_ERROR;
! 22981: ULONG wrote;
! 22982: os2File *pFile = (os2File*)id;
! 22983: assert( id!=0 );
! 22984: SimulateIOError( return SQLITE_IOERR_WRITE );
! 22985: SimulateDiskfullError( return SQLITE_FULL );
! 22986: OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
! 22987: if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
! 22988: return SQLITE_IOERR;
! 22989: }
! 22990: assert( amt>0 );
! 22991: while( amt > 0 &&
! 22992: ( rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote ) ) == NO_ERROR &&
! 22993: wrote > 0
! 22994: ){
! 22995: amt -= wrote;
! 22996: pBuf = &((char*)pBuf)[wrote];
! 22997: }
! 22998:
! 22999: return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
! 23000: }
! 23001:
! 23002: /*
! 23003: ** Truncate an open file to a specified size
! 23004: */
! 23005: static int os2Truncate( sqlite3_file *id, i64 nByte ){
! 23006: APIRET rc;
! 23007: os2File *pFile = (os2File*)id;
! 23008: assert( id!=0 );
! 23009: OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
! 23010: SimulateIOError( return SQLITE_IOERR_TRUNCATE );
! 23011:
! 23012: /* If the user has configured a chunk-size for this file, truncate the
! 23013: ** file so that it consists of an integer number of chunks (i.e. the
! 23014: ** actual file size after the operation may be larger than the requested
! 23015: ** size).
! 23016: */
! 23017: if( pFile->szChunk ){
! 23018: nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
! 23019: }
! 23020:
! 23021: rc = DosSetFileSize( pFile->h, nByte );
! 23022: return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
! 23023: }
! 23024:
! 23025: #ifdef SQLITE_TEST
! 23026: /*
! 23027: ** Count the number of fullsyncs and normal syncs. This is used to test
! 23028: ** that syncs and fullsyncs are occuring at the right times.
! 23029: */
! 23030: SQLITE_API int sqlite3_sync_count = 0;
! 23031: SQLITE_API int sqlite3_fullsync_count = 0;
! 23032: #endif
! 23033:
! 23034: /*
! 23035: ** Make sure all writes to a particular file are committed to disk.
! 23036: */
! 23037: static int os2Sync( sqlite3_file *id, int flags ){
! 23038: os2File *pFile = (os2File*)id;
! 23039: OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
! 23040: #ifdef SQLITE_TEST
! 23041: if( flags & SQLITE_SYNC_FULL){
! 23042: sqlite3_fullsync_count++;
! 23043: }
! 23044: sqlite3_sync_count++;
! 23045: #endif
! 23046: /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
! 23047: ** no-op
! 23048: */
! 23049: #ifdef SQLITE_NO_SYNC
! 23050: UNUSED_PARAMETER(pFile);
! 23051: return SQLITE_OK;
! 23052: #else
! 23053: return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
! 23054: #endif
! 23055: }
! 23056:
! 23057: /*
! 23058: ** Determine the current size of a file in bytes
! 23059: */
! 23060: static int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
! 23061: APIRET rc = NO_ERROR;
! 23062: FILESTATUS3 fsts3FileInfo;
! 23063: memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
! 23064: assert( id!=0 );
! 23065: SimulateIOError( return SQLITE_IOERR_FSTAT );
! 23066: rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
! 23067: if( rc == NO_ERROR ){
! 23068: *pSize = fsts3FileInfo.cbFile;
! 23069: return SQLITE_OK;
! 23070: }else{
! 23071: return SQLITE_IOERR_FSTAT;
! 23072: }
! 23073: }
! 23074:
! 23075: /*
! 23076: ** Acquire a reader lock.
! 23077: */
! 23078: static int getReadLock( os2File *pFile ){
! 23079: FILELOCK LockArea,
! 23080: UnlockArea;
! 23081: APIRET res;
! 23082: memset(&LockArea, 0, sizeof(LockArea));
! 23083: memset(&UnlockArea, 0, sizeof(UnlockArea));
! 23084: LockArea.lOffset = SHARED_FIRST;
! 23085: LockArea.lRange = SHARED_SIZE;
! 23086: UnlockArea.lOffset = 0L;
! 23087: UnlockArea.lRange = 0L;
! 23088: res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
! 23089: OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
! 23090: return res;
! 23091: }
! 23092:
! 23093: /*
! 23094: ** Undo a readlock
! 23095: */
! 23096: static int unlockReadLock( os2File *id ){
! 23097: FILELOCK LockArea,
! 23098: UnlockArea;
! 23099: APIRET res;
! 23100: memset(&LockArea, 0, sizeof(LockArea));
! 23101: memset(&UnlockArea, 0, sizeof(UnlockArea));
! 23102: LockArea.lOffset = 0L;
! 23103: LockArea.lRange = 0L;
! 23104: UnlockArea.lOffset = SHARED_FIRST;
! 23105: UnlockArea.lRange = SHARED_SIZE;
! 23106: res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
! 23107: OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
! 23108: return res;
! 23109: }
! 23110:
! 23111: /*
! 23112: ** Lock the file with the lock specified by parameter locktype - one
! 23113: ** of the following:
! 23114: **
! 23115: ** (1) SHARED_LOCK
! 23116: ** (2) RESERVED_LOCK
! 23117: ** (3) PENDING_LOCK
! 23118: ** (4) EXCLUSIVE_LOCK
! 23119: **
! 23120: ** Sometimes when requesting one lock state, additional lock states
! 23121: ** are inserted in between. The locking might fail on one of the later
! 23122: ** transitions leaving the lock state different from what it started but
! 23123: ** still short of its goal. The following chart shows the allowed
! 23124: ** transitions and the inserted intermediate states:
! 23125: **
! 23126: ** UNLOCKED -> SHARED
! 23127: ** SHARED -> RESERVED
! 23128: ** SHARED -> (PENDING) -> EXCLUSIVE
! 23129: ** RESERVED -> (PENDING) -> EXCLUSIVE
! 23130: ** PENDING -> EXCLUSIVE
! 23131: **
! 23132: ** This routine will only increase a lock. The os2Unlock() routine
! 23133: ** erases all locks at once and returns us immediately to locking level 0.
! 23134: ** It is not possible to lower the locking level one step at a time. You
! 23135: ** must go straight to locking level 0.
! 23136: */
! 23137: static int os2Lock( sqlite3_file *id, int locktype ){
! 23138: int rc = SQLITE_OK; /* Return code from subroutines */
! 23139: APIRET res = NO_ERROR; /* Result of an OS/2 lock call */
! 23140: int newLocktype; /* Set pFile->locktype to this value before exiting */
! 23141: int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
! 23142: FILELOCK LockArea,
! 23143: UnlockArea;
! 23144: os2File *pFile = (os2File*)id;
! 23145: memset(&LockArea, 0, sizeof(LockArea));
! 23146: memset(&UnlockArea, 0, sizeof(UnlockArea));
! 23147: assert( pFile!=0 );
! 23148: OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
! 23149:
! 23150: /* If there is already a lock of this type or more restrictive on the
! 23151: ** os2File, do nothing. Don't use the end_lock: exit path, as
! 23152: ** sqlite3_mutex_enter() hasn't been called yet.
! 23153: */
! 23154: if( pFile->locktype>=locktype ){
! 23155: OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
! 23156: return SQLITE_OK;
! 23157: }
! 23158:
! 23159: /* Make sure the locking sequence is correct
! 23160: */
! 23161: assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
! 23162: assert( locktype!=PENDING_LOCK );
! 23163: assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
! 23164:
! 23165: /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
! 23166: ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
! 23167: ** the PENDING_LOCK byte is temporary.
! 23168: */
! 23169: newLocktype = pFile->locktype;
! 23170: if( pFile->locktype==NO_LOCK
! 23171: || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
! 23172: ){
! 23173: LockArea.lOffset = PENDING_BYTE;
! 23174: LockArea.lRange = 1L;
! 23175: UnlockArea.lOffset = 0L;
! 23176: UnlockArea.lRange = 0L;
! 23177:
! 23178: /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
! 23179: res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
! 23180: if( res == NO_ERROR ){
! 23181: gotPendingLock = 1;
! 23182: OSTRACE(( "LOCK %d pending lock boolean set. res=%d\n", pFile->h, res ));
! 23183: }
! 23184: }
! 23185:
! 23186: /* Acquire a shared lock
! 23187: */
! 23188: if( locktype==SHARED_LOCK && res == NO_ERROR ){
! 23189: assert( pFile->locktype==NO_LOCK );
! 23190: res = getReadLock(pFile);
! 23191: if( res == NO_ERROR ){
! 23192: newLocktype = SHARED_LOCK;
! 23193: }
! 23194: OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
! 23195: }
! 23196:
! 23197: /* Acquire a RESERVED lock
! 23198: */
! 23199: if( locktype==RESERVED_LOCK && res == NO_ERROR ){
! 23200: assert( pFile->locktype==SHARED_LOCK );
! 23201: LockArea.lOffset = RESERVED_BYTE;
! 23202: LockArea.lRange = 1L;
! 23203: UnlockArea.lOffset = 0L;
! 23204: UnlockArea.lRange = 0L;
! 23205: res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
! 23206: if( res == NO_ERROR ){
! 23207: newLocktype = RESERVED_LOCK;
! 23208: }
! 23209: OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
! 23210: }
! 23211:
! 23212: /* Acquire a PENDING lock
! 23213: */
! 23214: if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
! 23215: newLocktype = PENDING_LOCK;
! 23216: gotPendingLock = 0;
! 23217: OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
! 23218: pFile->h ));
! 23219: }
! 23220:
! 23221: /* Acquire an EXCLUSIVE lock
! 23222: */
! 23223: if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
! 23224: assert( pFile->locktype>=SHARED_LOCK );
! 23225: res = unlockReadLock(pFile);
! 23226: OSTRACE(( "unreadlock = %d\n", res ));
! 23227: LockArea.lOffset = SHARED_FIRST;
! 23228: LockArea.lRange = SHARED_SIZE;
! 23229: UnlockArea.lOffset = 0L;
! 23230: UnlockArea.lRange = 0L;
! 23231: res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
! 23232: if( res == NO_ERROR ){
! 23233: newLocktype = EXCLUSIVE_LOCK;
! 23234: }else{
! 23235: OSTRACE(( "OS/2 error-code = %d\n", res ));
! 23236: getReadLock(pFile);
! 23237: }
! 23238: OSTRACE(( "LOCK %d acquire exclusive lock. res=%d\n", pFile->h, res ));
! 23239: }
! 23240:
! 23241: /* If we are holding a PENDING lock that ought to be released, then
! 23242: ** release it now.
! 23243: */
! 23244: if( gotPendingLock && locktype==SHARED_LOCK ){
! 23245: int r;
! 23246: LockArea.lOffset = 0L;
! 23247: LockArea.lRange = 0L;
! 23248: UnlockArea.lOffset = PENDING_BYTE;
! 23249: UnlockArea.lRange = 1L;
! 23250: r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
! 23251: OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
! 23252: }
! 23253:
! 23254: /* Update the state of the lock has held in the file descriptor then
! 23255: ** return the appropriate result code.
! 23256: */
! 23257: if( res == NO_ERROR ){
! 23258: rc = SQLITE_OK;
! 23259: }else{
! 23260: OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
! 23261: locktype, newLocktype ));
! 23262: rc = SQLITE_BUSY;
! 23263: }
! 23264: pFile->locktype = newLocktype;
! 23265: OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
! 23266: return rc;
! 23267: }
! 23268:
! 23269: /*
! 23270: ** This routine checks if there is a RESERVED lock held on the specified
! 23271: ** file by this or any other process. If such a lock is held, return
! 23272: ** non-zero, otherwise zero.
! 23273: */
! 23274: static int os2CheckReservedLock( sqlite3_file *id, int *pOut ){
! 23275: int r = 0;
! 23276: os2File *pFile = (os2File*)id;
! 23277: assert( pFile!=0 );
! 23278: if( pFile->locktype>=RESERVED_LOCK ){
! 23279: r = 1;
! 23280: OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
! 23281: }else{
! 23282: FILELOCK LockArea,
! 23283: UnlockArea;
! 23284: APIRET rc = NO_ERROR;
! 23285: memset(&LockArea, 0, sizeof(LockArea));
! 23286: memset(&UnlockArea, 0, sizeof(UnlockArea));
! 23287: LockArea.lOffset = RESERVED_BYTE;
! 23288: LockArea.lRange = 1L;
! 23289: UnlockArea.lOffset = 0L;
! 23290: UnlockArea.lRange = 0L;
! 23291: rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
! 23292: OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
! 23293: if( rc == NO_ERROR ){
! 23294: APIRET rcu = NO_ERROR; /* return code for unlocking */
! 23295: LockArea.lOffset = 0L;
! 23296: LockArea.lRange = 0L;
! 23297: UnlockArea.lOffset = RESERVED_BYTE;
! 23298: UnlockArea.lRange = 1L;
! 23299: rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
! 23300: OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
! 23301: }
! 23302: r = !(rc == NO_ERROR);
! 23303: OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
! 23304: }
! 23305: *pOut = r;
! 23306: return SQLITE_OK;
! 23307: }
! 23308:
! 23309: /*
! 23310: ** Lower the locking level on file descriptor id to locktype. locktype
! 23311: ** must be either NO_LOCK or SHARED_LOCK.
! 23312: **
! 23313: ** If the locking level of the file descriptor is already at or below
! 23314: ** the requested locking level, this routine is a no-op.
! 23315: **
! 23316: ** It is not possible for this routine to fail if the second argument
! 23317: ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
! 23318: ** might return SQLITE_IOERR;
! 23319: */
! 23320: static int os2Unlock( sqlite3_file *id, int locktype ){
! 23321: int type;
! 23322: os2File *pFile = (os2File*)id;
! 23323: APIRET rc = SQLITE_OK;
! 23324: APIRET res = NO_ERROR;
! 23325: FILELOCK LockArea,
! 23326: UnlockArea;
! 23327: memset(&LockArea, 0, sizeof(LockArea));
! 23328: memset(&UnlockArea, 0, sizeof(UnlockArea));
! 23329: assert( pFile!=0 );
! 23330: assert( locktype<=SHARED_LOCK );
! 23331: OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
! 23332: type = pFile->locktype;
! 23333: if( type>=EXCLUSIVE_LOCK ){
! 23334: LockArea.lOffset = 0L;
! 23335: LockArea.lRange = 0L;
! 23336: UnlockArea.lOffset = SHARED_FIRST;
! 23337: UnlockArea.lRange = SHARED_SIZE;
! 23338: res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
! 23339: OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
! 23340: if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
! 23341: /* This should never happen. We should always be able to
! 23342: ** reacquire the read lock */
! 23343: OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
! 23344: rc = SQLITE_IOERR_UNLOCK;
! 23345: }
! 23346: }
! 23347: if( type>=RESERVED_LOCK ){
! 23348: LockArea.lOffset = 0L;
! 23349: LockArea.lRange = 0L;
! 23350: UnlockArea.lOffset = RESERVED_BYTE;
! 23351: UnlockArea.lRange = 1L;
! 23352: res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
! 23353: OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
! 23354: }
! 23355: if( locktype==NO_LOCK && type>=SHARED_LOCK ){
! 23356: res = unlockReadLock(pFile);
! 23357: OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
! 23358: pFile->h, type, locktype, res ));
! 23359: }
! 23360: if( type>=PENDING_LOCK ){
! 23361: LockArea.lOffset = 0L;
! 23362: LockArea.lRange = 0L;
! 23363: UnlockArea.lOffset = PENDING_BYTE;
! 23364: UnlockArea.lRange = 1L;
! 23365: res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
! 23366: OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
! 23367: }
! 23368: pFile->locktype = locktype;
! 23369: OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
! 23370: return rc;
! 23371: }
! 23372:
! 23373: /*
! 23374: ** Control and query of the open file handle.
! 23375: */
! 23376: static int os2FileControl(sqlite3_file *id, int op, void *pArg){
! 23377: switch( op ){
! 23378: case SQLITE_FCNTL_LOCKSTATE: {
! 23379: *(int*)pArg = ((os2File*)id)->locktype;
! 23380: OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
! 23381: ((os2File*)id)->h, ((os2File*)id)->locktype ));
! 23382: return SQLITE_OK;
! 23383: }
! 23384: case SQLITE_FCNTL_CHUNK_SIZE: {
! 23385: ((os2File*)id)->szChunk = *(int*)pArg;
! 23386: return SQLITE_OK;
! 23387: }
! 23388: case SQLITE_FCNTL_SIZE_HINT: {
! 23389: sqlite3_int64 sz = *(sqlite3_int64*)pArg;
! 23390: SimulateIOErrorBenign(1);
! 23391: os2Truncate(id, sz);
! 23392: SimulateIOErrorBenign(0);
! 23393: return SQLITE_OK;
! 23394: }
! 23395: case SQLITE_FCNTL_SYNC_OMITTED: {
! 23396: return SQLITE_OK;
! 23397: }
! 23398: }
! 23399: return SQLITE_NOTFOUND;
! 23400: }
! 23401:
! 23402: /*
! 23403: ** Return the sector size in bytes of the underlying block device for
! 23404: ** the specified file. This is almost always 512 bytes, but may be
! 23405: ** larger for some devices.
! 23406: **
! 23407: ** SQLite code assumes this function cannot fail. It also assumes that
! 23408: ** if two files are created in the same file-system directory (i.e.
! 23409: ** a database and its journal file) that the sector size will be the
! 23410: ** same for both.
! 23411: */
! 23412: static int os2SectorSize(sqlite3_file *id){
! 23413: UNUSED_PARAMETER(id);
! 23414: return SQLITE_DEFAULT_SECTOR_SIZE;
! 23415: }
! 23416:
! 23417: /*
! 23418: ** Return a vector of device characteristics.
! 23419: */
! 23420: static int os2DeviceCharacteristics(sqlite3_file *id){
! 23421: UNUSED_PARAMETER(id);
! 23422: return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
! 23423: }
! 23424:
! 23425:
! 23426: /*
! 23427: ** Character set conversion objects used by conversion routines.
! 23428: */
! 23429: static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
! 23430: static UconvObject uclCp = NULL; /* convert between local codepage and UCS-2 */
! 23431:
! 23432: /*
! 23433: ** Helper function to initialize the conversion objects from and to UTF-8.
! 23434: */
! 23435: static void initUconvObjects( void ){
! 23436: if( UniCreateUconvObject( UTF_8, &ucUtf8 ) != ULS_SUCCESS )
! 23437: ucUtf8 = NULL;
! 23438: if ( UniCreateUconvObject( (UniChar *)L"@path=yes", &uclCp ) != ULS_SUCCESS )
! 23439: uclCp = NULL;
! 23440: }
! 23441:
! 23442: /*
! 23443: ** Helper function to free the conversion objects from and to UTF-8.
! 23444: */
! 23445: static void freeUconvObjects( void ){
! 23446: if ( ucUtf8 )
! 23447: UniFreeUconvObject( ucUtf8 );
! 23448: if ( uclCp )
! 23449: UniFreeUconvObject( uclCp );
! 23450: ucUtf8 = NULL;
! 23451: uclCp = NULL;
! 23452: }
! 23453:
! 23454: /*
! 23455: ** Helper function to convert UTF-8 filenames to local OS/2 codepage.
! 23456: ** The two-step process: first convert the incoming UTF-8 string
! 23457: ** into UCS-2 and then from UCS-2 to the current codepage.
! 23458: ** The returned char pointer has to be freed.
! 23459: */
! 23460: static char *convertUtf8PathToCp( const char *in ){
! 23461: UniChar tempPath[CCHMAXPATH];
! 23462: char *out = (char *)calloc( CCHMAXPATH, 1 );
! 23463:
! 23464: if( !out )
! 23465: return NULL;
! 23466:
! 23467: if( !ucUtf8 || !uclCp )
! 23468: initUconvObjects();
! 23469:
! 23470: /* determine string for the conversion of UTF-8 which is CP1208 */
! 23471: if( UniStrToUcs( ucUtf8, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
! 23472: return out; /* if conversion fails, return the empty string */
! 23473:
! 23474: /* conversion for current codepage which can be used for paths */
! 23475: UniStrFromUcs( uclCp, out, tempPath, CCHMAXPATH );
! 23476:
! 23477: return out;
! 23478: }
! 23479:
! 23480: /*
! 23481: ** Helper function to convert filenames from local codepage to UTF-8.
! 23482: ** The two-step process: first convert the incoming codepage-specific
! 23483: ** string into UCS-2 and then from UCS-2 to the codepage of UTF-8.
! 23484: ** The returned char pointer has to be freed.
! 23485: **
! 23486: ** This function is non-static to be able to use this in shell.c and
! 23487: ** similar applications that take command line arguments.
! 23488: */
! 23489: char *convertCpPathToUtf8( const char *in ){
! 23490: UniChar tempPath[CCHMAXPATH];
! 23491: char *out = (char *)calloc( CCHMAXPATH, 1 );
! 23492:
! 23493: if( !out )
! 23494: return NULL;
! 23495:
! 23496: if( !ucUtf8 || !uclCp )
! 23497: initUconvObjects();
! 23498:
! 23499: /* conversion for current codepage which can be used for paths */
! 23500: if( UniStrToUcs( uclCp, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
! 23501: return out; /* if conversion fails, return the empty string */
! 23502:
! 23503: /* determine string for the conversion of UTF-8 which is CP1208 */
! 23504: UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
! 23505:
! 23506: return out;
! 23507: }
! 23508:
! 23509:
! 23510: #ifndef SQLITE_OMIT_WAL
! 23511:
! 23512: /*
! 23513: ** Use main database file for interprocess locking. If un-defined
! 23514: ** a separate file is created for this purpose. The file will be
! 23515: ** used only to set file locks. There will be no data written to it.
! 23516: */
! 23517: #define SQLITE_OS2_NO_WAL_LOCK_FILE
! 23518:
! 23519: #if 0
! 23520: static void _ERR_TRACE( const char *fmt, ... ) {
! 23521: va_list ap;
! 23522: va_start(ap, fmt);
! 23523: vfprintf(stderr, fmt, ap);
! 23524: fflush(stderr);
! 23525: }
! 23526: #define ERR_TRACE(rc, msg) \
! 23527: if( (rc) != SQLITE_OK ) _ERR_TRACE msg;
! 23528: #else
! 23529: #define ERR_TRACE(rc, msg)
! 23530: #endif
! 23531:
! 23532: /*
! 23533: ** Helper functions to obtain and relinquish the global mutex. The
! 23534: ** global mutex is used to protect os2ShmNodeList.
! 23535: **
! 23536: ** Function os2ShmMutexHeld() is used to assert() that the global mutex
! 23537: ** is held when required. This function is only used as part of assert()
! 23538: ** statements. e.g.
! 23539: **
! 23540: ** os2ShmEnterMutex()
! 23541: ** assert( os2ShmMutexHeld() );
! 23542: ** os2ShmLeaveMutex()
! 23543: */
! 23544: static void os2ShmEnterMutex(void){
! 23545: sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 23546: }
! 23547: static void os2ShmLeaveMutex(void){
! 23548: sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 23549: }
! 23550: #ifdef SQLITE_DEBUG
! 23551: static int os2ShmMutexHeld(void) {
! 23552: return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 23553: }
! 23554: int GetCurrentProcessId(void) {
! 23555: PPIB pib;
! 23556: DosGetInfoBlocks(NULL, &pib);
! 23557: return (int)pib->pib_ulpid;
! 23558: }
! 23559: #endif
! 23560:
! 23561: /*
! 23562: ** Object used to represent a the shared memory area for a single log file.
! 23563: ** When multiple threads all reference the same log-summary, each thread has
! 23564: ** its own os2File object, but they all point to a single instance of this
! 23565: ** object. In other words, each log-summary is opened only once per process.
! 23566: **
! 23567: ** os2ShmMutexHeld() must be true when creating or destroying
! 23568: ** this object or while reading or writing the following fields:
! 23569: **
! 23570: ** nRef
! 23571: ** pNext
! 23572: **
! 23573: ** The following fields are read-only after the object is created:
! 23574: **
! 23575: ** szRegion
! 23576: ** hLockFile
! 23577: ** shmBaseName
! 23578: **
! 23579: ** Either os2ShmNode.mutex must be held or os2ShmNode.nRef==0 and
! 23580: ** os2ShmMutexHeld() is true when reading or writing any other field
! 23581: ** in this structure.
! 23582: **
! 23583: */
! 23584: struct os2ShmNode {
! 23585: sqlite3_mutex *mutex; /* Mutex to access this object */
! 23586: os2ShmNode *pNext; /* Next in list of all os2ShmNode objects */
! 23587:
! 23588: int szRegion; /* Size of shared-memory regions */
! 23589:
! 23590: int nRegion; /* Size of array apRegion */
! 23591: void **apRegion; /* Array of pointers to shared-memory regions */
! 23592:
! 23593: int nRef; /* Number of os2ShmLink objects pointing to this */
! 23594: os2ShmLink *pFirst; /* First os2ShmLink object pointing to this */
! 23595:
! 23596: HFILE hLockFile; /* File used for inter-process memory locking */
! 23597: char shmBaseName[1]; /* Name of the memory object !!! must last !!! */
! 23598: };
! 23599:
! 23600:
! 23601: /*
! 23602: ** Structure used internally by this VFS to record the state of an
! 23603: ** open shared memory connection.
! 23604: **
! 23605: ** The following fields are initialized when this object is created and
! 23606: ** are read-only thereafter:
! 23607: **
! 23608: ** os2Shm.pShmNode
! 23609: ** os2Shm.id
! 23610: **
! 23611: ** All other fields are read/write. The os2Shm.pShmNode->mutex must be held
! 23612: ** while accessing any read/write fields.
! 23613: */
! 23614: struct os2ShmLink {
! 23615: os2ShmNode *pShmNode; /* The underlying os2ShmNode object */
! 23616: os2ShmLink *pNext; /* Next os2Shm with the same os2ShmNode */
! 23617: u32 sharedMask; /* Mask of shared locks held */
! 23618: u32 exclMask; /* Mask of exclusive locks held */
! 23619: #ifdef SQLITE_DEBUG
! 23620: u8 id; /* Id of this connection with its os2ShmNode */
! 23621: #endif
! 23622: };
! 23623:
! 23624:
! 23625: /*
! 23626: ** A global list of all os2ShmNode objects.
! 23627: **
! 23628: ** The os2ShmMutexHeld() must be true while reading or writing this list.
! 23629: */
! 23630: static os2ShmNode *os2ShmNodeList = NULL;
! 23631:
! 23632: /*
! 23633: ** Constants used for locking
! 23634: */
! 23635: #ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
! 23636: #define OS2_SHM_BASE (PENDING_BYTE + 0x10000) /* first lock byte */
! 23637: #else
! 23638: #define OS2_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
! 23639: #endif
! 23640:
! 23641: #define OS2_SHM_DMS (OS2_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
! 23642:
! 23643: /*
! 23644: ** Apply advisory locks for all n bytes beginning at ofst.
! 23645: */
! 23646: #define _SHM_UNLCK 1 /* no lock */
! 23647: #define _SHM_RDLCK 2 /* shared lock, no wait */
! 23648: #define _SHM_WRLCK 3 /* exlusive lock, no wait */
! 23649: #define _SHM_WRLCK_WAIT 4 /* exclusive lock, wait */
! 23650: static int os2ShmSystemLock(
! 23651: os2ShmNode *pNode, /* Apply locks to this open shared-memory segment */
! 23652: int lockType, /* _SHM_UNLCK, _SHM_RDLCK, _SHM_WRLCK or _SHM_WRLCK_WAIT */
! 23653: int ofst, /* Offset to first byte to be locked/unlocked */
! 23654: int nByte /* Number of bytes to lock or unlock */
! 23655: ){
! 23656: APIRET rc;
! 23657: FILELOCK area;
! 23658: ULONG mode, timeout;
! 23659:
! 23660: /* Access to the os2ShmNode object is serialized by the caller */
! 23661: assert( sqlite3_mutex_held(pNode->mutex) || pNode->nRef==0 );
! 23662:
! 23663: mode = 1; /* shared lock */
! 23664: timeout = 0; /* no wait */
! 23665: area.lOffset = ofst;
! 23666: area.lRange = nByte;
! 23667:
! 23668: switch( lockType ) {
! 23669: case _SHM_WRLCK_WAIT:
! 23670: timeout = (ULONG)-1; /* wait forever */
! 23671: case _SHM_WRLCK:
! 23672: mode = 0; /* exclusive lock */
! 23673: case _SHM_RDLCK:
! 23674: rc = DosSetFileLocks(pNode->hLockFile,
! 23675: NULL, &area, timeout, mode);
! 23676: break;
! 23677: /* case _SHM_UNLCK: */
! 23678: default:
! 23679: rc = DosSetFileLocks(pNode->hLockFile,
! 23680: &area, NULL, 0, 0);
! 23681: break;
! 23682: }
! 23683:
! 23684: OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
! 23685: pNode->hLockFile,
! 23686: rc==SQLITE_OK ? "ok" : "failed",
! 23687: lockType==_SHM_UNLCK ? "Unlock" : "Lock",
! 23688: rc));
! 23689:
! 23690: ERR_TRACE(rc, ("os2ShmSystemLock: %d %s\n", rc, pNode->shmBaseName))
! 23691:
! 23692: return ( rc == 0 ) ? SQLITE_OK : SQLITE_BUSY;
! 23693: }
! 23694:
! 23695: /*
! 23696: ** Find an os2ShmNode in global list or allocate a new one, if not found.
! 23697: **
! 23698: ** This is not a VFS shared-memory method; it is a utility function called
! 23699: ** by VFS shared-memory methods.
! 23700: */
! 23701: static int os2OpenSharedMemory( os2File *fd, int szRegion ) {
! 23702: os2ShmLink *pLink;
! 23703: os2ShmNode *pNode;
! 23704: int cbShmName, rc = SQLITE_OK;
! 23705: char shmName[CCHMAXPATH + 30];
! 23706: #ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
! 23707: ULONG action;
! 23708: #endif
! 23709:
! 23710: /* We need some additional space at the end to append the region number */
! 23711: cbShmName = sprintf(shmName, "\\SHAREMEM\\%s", fd->zFullPathCp );
! 23712: if( cbShmName >= CCHMAXPATH-8 )
! 23713: return SQLITE_IOERR_SHMOPEN;
! 23714:
! 23715: /* Replace colon in file name to form a valid shared memory name */
! 23716: shmName[10+1] = '!';
! 23717:
! 23718: /* Allocate link object (we free it later in case of failure) */
! 23719: pLink = sqlite3_malloc( sizeof(*pLink) );
! 23720: if( !pLink )
! 23721: return SQLITE_NOMEM;
! 23722:
! 23723: /* Access node list */
! 23724: os2ShmEnterMutex();
! 23725:
! 23726: /* Find node by it's shared memory base name */
! 23727: for( pNode = os2ShmNodeList;
! 23728: pNode && stricmp(shmName, pNode->shmBaseName) != 0;
! 23729: pNode = pNode->pNext ) ;
! 23730:
! 23731: /* Not found: allocate a new node */
! 23732: if( !pNode ) {
! 23733: pNode = sqlite3_malloc( sizeof(*pNode) + cbShmName );
! 23734: if( pNode ) {
! 23735: memset(pNode, 0, sizeof(*pNode) );
! 23736: pNode->szRegion = szRegion;
! 23737: pNode->hLockFile = (HFILE)-1;
! 23738: strcpy(pNode->shmBaseName, shmName);
! 23739:
! 23740: #ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
! 23741: if( DosDupHandle(fd->h, &pNode->hLockFile) != 0 ) {
! 23742: #else
! 23743: sprintf(shmName, "%s-lck", fd->zFullPathCp);
! 23744: if( DosOpen((PSZ)shmName, &pNode->hLockFile, &action, 0, FILE_NORMAL,
! 23745: OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
! 23746: OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE |
! 23747: OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR,
! 23748: NULL) != 0 ) {
! 23749: #endif
! 23750: sqlite3_free(pNode);
! 23751: rc = SQLITE_IOERR;
! 23752: } else {
! 23753: pNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
! 23754: if( !pNode->mutex ) {
! 23755: sqlite3_free(pNode);
! 23756: rc = SQLITE_NOMEM;
! 23757: }
! 23758: }
! 23759: } else {
! 23760: rc = SQLITE_NOMEM;
! 23761: }
! 23762:
! 23763: if( rc == SQLITE_OK ) {
! 23764: pNode->pNext = os2ShmNodeList;
! 23765: os2ShmNodeList = pNode;
! 23766: } else {
! 23767: pNode = NULL;
! 23768: }
! 23769: } else if( pNode->szRegion != szRegion ) {
! 23770: rc = SQLITE_IOERR_SHMSIZE;
! 23771: pNode = NULL;
! 23772: }
! 23773:
! 23774: if( pNode ) {
! 23775: sqlite3_mutex_enter(pNode->mutex);
! 23776:
! 23777: memset(pLink, 0, sizeof(*pLink));
! 23778:
! 23779: pLink->pShmNode = pNode;
! 23780: pLink->pNext = pNode->pFirst;
! 23781: pNode->pFirst = pLink;
! 23782: pNode->nRef++;
! 23783:
! 23784: fd->pShmLink = pLink;
! 23785:
! 23786: sqlite3_mutex_leave(pNode->mutex);
! 23787:
! 23788: } else {
! 23789: /* Error occured. Free our link object. */
! 23790: sqlite3_free(pLink);
! 23791: }
! 23792:
! 23793: os2ShmLeaveMutex();
! 23794:
! 23795: ERR_TRACE(rc, ("os2OpenSharedMemory: %d %s\n", rc, fd->zFullPathCp))
! 23796:
! 23797: return rc;
! 23798: }
! 23799:
! 23800: /*
! 23801: ** Purge the os2ShmNodeList list of all entries with nRef==0.
! 23802: **
! 23803: ** This is not a VFS shared-memory method; it is a utility function called
! 23804: ** by VFS shared-memory methods.
! 23805: */
! 23806: static void os2PurgeShmNodes( int deleteFlag ) {
! 23807: os2ShmNode *pNode;
! 23808: os2ShmNode **ppNode;
! 23809:
! 23810: os2ShmEnterMutex();
! 23811:
! 23812: ppNode = &os2ShmNodeList;
! 23813:
! 23814: while( *ppNode ) {
! 23815: pNode = *ppNode;
! 23816:
! 23817: if( pNode->nRef == 0 ) {
! 23818: *ppNode = pNode->pNext;
! 23819:
! 23820: if( pNode->apRegion ) {
! 23821: /* Prevent other processes from resizing the shared memory */
! 23822: os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
! 23823:
! 23824: while( pNode->nRegion-- ) {
! 23825: #ifdef SQLITE_DEBUG
! 23826: int rc =
! 23827: #endif
! 23828: DosFreeMem(pNode->apRegion[pNode->nRegion]);
! 23829:
! 23830: OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
! 23831: (int)GetCurrentProcessId(), pNode->nRegion,
! 23832: rc == 0 ? "ok" : "failed"));
! 23833: }
! 23834:
! 23835: /* Allow other processes to resize the shared memory */
! 23836: os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
! 23837:
! 23838: sqlite3_free(pNode->apRegion);
! 23839: }
! 23840:
! 23841: DosClose(pNode->hLockFile);
! 23842:
! 23843: #ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
! 23844: if( deleteFlag ) {
! 23845: char fileName[CCHMAXPATH];
! 23846: /* Skip "\\SHAREMEM\\" */
! 23847: sprintf(fileName, "%s-lck", pNode->shmBaseName + 10);
! 23848: /* restore colon */
! 23849: fileName[1] = ':';
! 23850:
! 23851: DosForceDelete(fileName);
! 23852: }
! 23853: #endif
! 23854:
! 23855: sqlite3_mutex_free(pNode->mutex);
! 23856:
! 23857: sqlite3_free(pNode);
! 23858:
! 23859: } else {
! 23860: ppNode = &pNode->pNext;
! 23861: }
! 23862: }
! 23863:
! 23864: os2ShmLeaveMutex();
! 23865: }
! 23866:
! 23867: /*
! 23868: ** This function is called to obtain a pointer to region iRegion of the
! 23869: ** shared-memory associated with the database file id. Shared-memory regions
! 23870: ** are numbered starting from zero. Each shared-memory region is szRegion
! 23871: ** bytes in size.
! 23872: **
! 23873: ** If an error occurs, an error code is returned and *pp is set to NULL.
! 23874: **
! 23875: ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
! 23876: ** region has not been allocated (by any client, including one running in a
! 23877: ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
! 23878: ** bExtend is non-zero and the requested shared-memory region has not yet
! 23879: ** been allocated, it is allocated by this function.
! 23880: **
! 23881: ** If the shared-memory region has already been allocated or is allocated by
! 23882: ** this call as described above, then it is mapped into this processes
! 23883: ** address space (if it is not already), *pp is set to point to the mapped
! 23884: ** memory and SQLITE_OK returned.
! 23885: */
! 23886: static int os2ShmMap(
! 23887: sqlite3_file *id, /* Handle open on database file */
! 23888: int iRegion, /* Region to retrieve */
! 23889: int szRegion, /* Size of regions */
! 23890: int bExtend, /* True to extend block if necessary */
! 23891: void volatile **pp /* OUT: Mapped memory */
! 23892: ){
! 23893: PVOID pvTemp;
! 23894: void **apRegion;
! 23895: os2ShmNode *pNode;
! 23896: int n, rc = SQLITE_OK;
! 23897: char shmName[CCHMAXPATH];
! 23898: os2File *pFile = (os2File*)id;
! 23899:
! 23900: *pp = NULL;
! 23901:
! 23902: if( !pFile->pShmLink )
! 23903: rc = os2OpenSharedMemory( pFile, szRegion );
! 23904:
! 23905: if( rc == SQLITE_OK ) {
! 23906: pNode = pFile->pShmLink->pShmNode ;
! 23907:
! 23908: sqlite3_mutex_enter(pNode->mutex);
! 23909:
! 23910: assert( szRegion==pNode->szRegion );
! 23911:
! 23912: /* Unmapped region ? */
! 23913: if( iRegion >= pNode->nRegion ) {
! 23914: /* Prevent other processes from resizing the shared memory */
! 23915: os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
! 23916:
! 23917: apRegion = sqlite3_realloc(
! 23918: pNode->apRegion, (iRegion + 1) * sizeof(apRegion[0]));
! 23919:
! 23920: if( apRegion ) {
! 23921: pNode->apRegion = apRegion;
! 23922:
! 23923: while( pNode->nRegion <= iRegion ) {
! 23924: sprintf(shmName, "%s-%u",
! 23925: pNode->shmBaseName, pNode->nRegion);
! 23926:
! 23927: if( DosGetNamedSharedMem(&pvTemp, (PSZ)shmName,
! 23928: PAG_READ | PAG_WRITE) != NO_ERROR ) {
! 23929: if( !bExtend )
! 23930: break;
! 23931:
! 23932: if( DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
! 23933: PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY) != NO_ERROR &&
! 23934: DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
! 23935: PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR ) {
! 23936: rc = SQLITE_NOMEM;
! 23937: break;
! 23938: }
! 23939: }
! 23940:
! 23941: apRegion[pNode->nRegion++] = pvTemp;
! 23942: }
! 23943:
! 23944: /* zero out remaining entries */
! 23945: for( n = pNode->nRegion; n <= iRegion; n++ )
! 23946: pNode->apRegion[n] = NULL;
! 23947:
! 23948: /* Return this region (maybe zero) */
! 23949: *pp = pNode->apRegion[iRegion];
! 23950: } else {
! 23951: rc = SQLITE_NOMEM;
! 23952: }
! 23953:
! 23954: /* Allow other processes to resize the shared memory */
! 23955: os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
! 23956:
! 23957: } else {
! 23958: /* Region has been mapped previously */
! 23959: *pp = pNode->apRegion[iRegion];
! 23960: }
! 23961:
! 23962: sqlite3_mutex_leave(pNode->mutex);
! 23963: }
! 23964:
! 23965: ERR_TRACE(rc, ("os2ShmMap: %s iRgn = %d, szRgn = %d, bExt = %d : %d\n",
! 23966: pFile->zFullPathCp, iRegion, szRegion, bExtend, rc))
! 23967:
! 23968: return rc;
! 23969: }
! 23970:
! 23971: /*
! 23972: ** Close a connection to shared-memory. Delete the underlying
! 23973: ** storage if deleteFlag is true.
! 23974: **
! 23975: ** If there is no shared memory associated with the connection then this
! 23976: ** routine is a harmless no-op.
! 23977: */
! 23978: static int os2ShmUnmap(
! 23979: sqlite3_file *id, /* The underlying database file */
! 23980: int deleteFlag /* Delete shared-memory if true */
! 23981: ){
! 23982: os2File *pFile = (os2File*)id;
! 23983: os2ShmLink *pLink = pFile->pShmLink;
! 23984:
! 23985: if( pLink ) {
! 23986: int nRef = -1;
! 23987: os2ShmLink **ppLink;
! 23988: os2ShmNode *pNode = pLink->pShmNode;
! 23989:
! 23990: sqlite3_mutex_enter(pNode->mutex);
! 23991:
! 23992: for( ppLink = &pNode->pFirst;
! 23993: *ppLink && *ppLink != pLink;
! 23994: ppLink = &(*ppLink)->pNext ) ;
! 23995:
! 23996: assert(*ppLink);
! 23997:
! 23998: if( *ppLink ) {
! 23999: *ppLink = pLink->pNext;
! 24000: nRef = --pNode->nRef;
! 24001: } else {
! 24002: ERR_TRACE(1, ("os2ShmUnmap: link not found ! %s\n",
! 24003: pNode->shmBaseName))
! 24004: }
! 24005:
! 24006: pFile->pShmLink = NULL;
! 24007: sqlite3_free(pLink);
! 24008:
! 24009: sqlite3_mutex_leave(pNode->mutex);
! 24010:
! 24011: if( nRef == 0 )
! 24012: os2PurgeShmNodes( deleteFlag );
! 24013: }
! 24014:
! 24015: return SQLITE_OK;
! 24016: }
! 24017:
! 24018: /*
! 24019: ** Change the lock state for a shared-memory segment.
! 24020: **
! 24021: ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
! 24022: ** different here than in posix. In xShmLock(), one can go from unlocked
! 24023: ** to shared and back or from unlocked to exclusive and back. But one may
! 24024: ** not go from shared to exclusive or from exclusive to shared.
! 24025: */
! 24026: static int os2ShmLock(
! 24027: sqlite3_file *id, /* Database file holding the shared memory */
! 24028: int ofst, /* First lock to acquire or release */
! 24029: int n, /* Number of locks to acquire or release */
! 24030: int flags /* What to do with the lock */
! 24031: ){
! 24032: u32 mask; /* Mask of locks to take or release */
! 24033: int rc = SQLITE_OK; /* Result code */
! 24034: os2File *pFile = (os2File*)id;
! 24035: os2ShmLink *p = pFile->pShmLink; /* The shared memory being locked */
! 24036: os2ShmLink *pX; /* For looping over all siblings */
! 24037: os2ShmNode *pShmNode = p->pShmNode; /* Our node */
! 24038:
! 24039: assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
! 24040: assert( n>=1 );
! 24041: assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
! 24042: || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
! 24043: || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
! 24044: || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
! 24045: assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
! 24046:
! 24047: mask = (u32)((1U<<(ofst+n)) - (1U<<ofst));
! 24048: assert( n>1 || mask==(1<<ofst) );
! 24049:
! 24050:
! 24051: sqlite3_mutex_enter(pShmNode->mutex);
! 24052:
! 24053: if( flags & SQLITE_SHM_UNLOCK ){
! 24054: u32 allMask = 0; /* Mask of locks held by siblings */
! 24055:
! 24056: /* See if any siblings hold this same lock */
! 24057: for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
! 24058: if( pX==p ) continue;
! 24059: assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
! 24060: allMask |= pX->sharedMask;
! 24061: }
! 24062:
! 24063: /* Unlock the system-level locks */
! 24064: if( (mask & allMask)==0 ){
! 24065: rc = os2ShmSystemLock(pShmNode, _SHM_UNLCK, ofst+OS2_SHM_BASE, n);
! 24066: }else{
! 24067: rc = SQLITE_OK;
! 24068: }
! 24069:
! 24070: /* Undo the local locks */
! 24071: if( rc==SQLITE_OK ){
! 24072: p->exclMask &= ~mask;
! 24073: p->sharedMask &= ~mask;
! 24074: }
! 24075: }else if( flags & SQLITE_SHM_SHARED ){
! 24076: u32 allShared = 0; /* Union of locks held by connections other than "p" */
! 24077:
! 24078: /* Find out which shared locks are already held by sibling connections.
! 24079: ** If any sibling already holds an exclusive lock, go ahead and return
! 24080: ** SQLITE_BUSY.
! 24081: */
! 24082: for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
! 24083: if( (pX->exclMask & mask)!=0 ){
! 24084: rc = SQLITE_BUSY;
! 24085: break;
! 24086: }
! 24087: allShared |= pX->sharedMask;
! 24088: }
! 24089:
! 24090: /* Get shared locks at the system level, if necessary */
! 24091: if( rc==SQLITE_OK ){
! 24092: if( (allShared & mask)==0 ){
! 24093: rc = os2ShmSystemLock(pShmNode, _SHM_RDLCK, ofst+OS2_SHM_BASE, n);
! 24094: }else{
! 24095: rc = SQLITE_OK;
! 24096: }
! 24097: }
! 24098:
! 24099: /* Get the local shared locks */
! 24100: if( rc==SQLITE_OK ){
! 24101: p->sharedMask |= mask;
! 24102: }
! 24103: }else{
! 24104: /* Make sure no sibling connections hold locks that will block this
! 24105: ** lock. If any do, return SQLITE_BUSY right away.
! 24106: */
! 24107: for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
! 24108: if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
! 24109: rc = SQLITE_BUSY;
! 24110: break;
! 24111: }
! 24112: }
! 24113:
! 24114: /* Get the exclusive locks at the system level. Then if successful
! 24115: ** also mark the local connection as being locked.
! 24116: */
! 24117: if( rc==SQLITE_OK ){
! 24118: rc = os2ShmSystemLock(pShmNode, _SHM_WRLCK, ofst+OS2_SHM_BASE, n);
! 24119: if( rc==SQLITE_OK ){
! 24120: assert( (p->sharedMask & mask)==0 );
! 24121: p->exclMask |= mask;
! 24122: }
! 24123: }
! 24124: }
! 24125:
! 24126: sqlite3_mutex_leave(pShmNode->mutex);
! 24127:
! 24128: OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
! 24129: p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
! 24130: rc ? "failed" : "ok"));
! 24131:
! 24132: ERR_TRACE(rc, ("os2ShmLock: ofst = %d, n = %d, flags = 0x%x -> %d \n",
! 24133: ofst, n, flags, rc))
! 24134:
! 24135: return rc;
! 24136: }
! 24137:
! 24138: /*
! 24139: ** Implement a memory barrier or memory fence on shared memory.
! 24140: **
! 24141: ** All loads and stores begun before the barrier must complete before
! 24142: ** any load or store begun after the barrier.
! 24143: */
! 24144: static void os2ShmBarrier(
! 24145: sqlite3_file *id /* Database file holding the shared memory */
! 24146: ){
! 24147: UNUSED_PARAMETER(id);
! 24148: os2ShmEnterMutex();
! 24149: os2ShmLeaveMutex();
! 24150: }
! 24151:
! 24152: #else
! 24153: # define os2ShmMap 0
! 24154: # define os2ShmLock 0
! 24155: # define os2ShmBarrier 0
! 24156: # define os2ShmUnmap 0
! 24157: #endif /* #ifndef SQLITE_OMIT_WAL */
! 24158:
! 24159:
! 24160: /*
! 24161: ** This vector defines all the methods that can operate on an
! 24162: ** sqlite3_file for os2.
! 24163: */
! 24164: static const sqlite3_io_methods os2IoMethod = {
! 24165: 2, /* iVersion */
! 24166: os2Close, /* xClose */
! 24167: os2Read, /* xRead */
! 24168: os2Write, /* xWrite */
! 24169: os2Truncate, /* xTruncate */
! 24170: os2Sync, /* xSync */
! 24171: os2FileSize, /* xFileSize */
! 24172: os2Lock, /* xLock */
! 24173: os2Unlock, /* xUnlock */
! 24174: os2CheckReservedLock, /* xCheckReservedLock */
! 24175: os2FileControl, /* xFileControl */
! 24176: os2SectorSize, /* xSectorSize */
! 24177: os2DeviceCharacteristics, /* xDeviceCharacteristics */
! 24178: os2ShmMap, /* xShmMap */
! 24179: os2ShmLock, /* xShmLock */
! 24180: os2ShmBarrier, /* xShmBarrier */
! 24181: os2ShmUnmap /* xShmUnmap */
! 24182: };
! 24183:
! 24184:
! 24185: /***************************************************************************
! 24186: ** Here ends the I/O methods that form the sqlite3_io_methods object.
! 24187: **
! 24188: ** The next block of code implements the VFS methods.
! 24189: ****************************************************************************/
! 24190:
! 24191: /*
! 24192: ** Create a temporary file name in zBuf. zBuf must be big enough to
! 24193: ** hold at pVfs->mxPathname characters.
! 24194: */
! 24195: static int getTempname(int nBuf, char *zBuf ){
! 24196: static const char zChars[] =
! 24197: "abcdefghijklmnopqrstuvwxyz"
! 24198: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
! 24199: "0123456789";
! 24200: int i, j;
! 24201: PSZ zTempPathCp;
! 24202: char zTempPath[CCHMAXPATH];
! 24203: ULONG ulDriveNum, ulDriveMap;
! 24204:
! 24205: /* It's odd to simulate an io-error here, but really this is just
! 24206: ** using the io-error infrastructure to test that SQLite handles this
! 24207: ** function failing.
! 24208: */
! 24209: SimulateIOError( return SQLITE_IOERR );
! 24210:
! 24211: if( sqlite3_temp_directory ) {
! 24212: sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", sqlite3_temp_directory);
! 24213: } else if( DosScanEnv( (PSZ)"TEMP", &zTempPathCp ) == NO_ERROR ||
! 24214: DosScanEnv( (PSZ)"TMP", &zTempPathCp ) == NO_ERROR ||
! 24215: DosScanEnv( (PSZ)"TMPDIR", &zTempPathCp ) == NO_ERROR ) {
! 24216: char *zTempPathUTF = convertCpPathToUtf8( (char *)zTempPathCp );
! 24217: sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", zTempPathUTF);
! 24218: free( zTempPathUTF );
! 24219: } else if( DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ) == NO_ERROR ) {
! 24220: zTempPath[0] = (char)('A' + ulDriveNum - 1);
! 24221: zTempPath[1] = ':';
! 24222: zTempPath[2] = '\0';
! 24223: } else {
! 24224: zTempPath[0] = '\0';
! 24225: }
! 24226:
! 24227: /* Strip off a trailing slashes or backslashes, otherwise we would get *
! 24228: * multiple (back)slashes which causes DosOpen() to fail. *
! 24229: * Trailing spaces are not allowed, either. */
! 24230: j = sqlite3Strlen30(zTempPath);
! 24231: while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' ||
! 24232: zTempPath[j-1] == ' ' ) ){
! 24233: j--;
! 24234: }
! 24235: zTempPath[j] = '\0';
! 24236:
! 24237: /* We use 20 bytes to randomize the name */
! 24238: sqlite3_snprintf(nBuf-22, zBuf,
! 24239: "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
! 24240: j = sqlite3Strlen30(zBuf);
! 24241: sqlite3_randomness( 20, &zBuf[j] );
! 24242: for( i = 0; i < 20; i++, j++ ){
! 24243: zBuf[j] = zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
! 24244: }
! 24245: zBuf[j] = 0;
! 24246:
! 24247: OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
! 24248: return SQLITE_OK;
! 24249: }
! 24250:
! 24251:
! 24252: /*
! 24253: ** Turn a relative pathname into a full pathname. Write the full
! 24254: ** pathname into zFull[]. zFull[] will be at least pVfs->mxPathname
! 24255: ** bytes in size.
! 24256: */
! 24257: static int os2FullPathname(
! 24258: sqlite3_vfs *pVfs, /* Pointer to vfs object */
! 24259: const char *zRelative, /* Possibly relative input path */
! 24260: int nFull, /* Size of output buffer in bytes */
! 24261: char *zFull /* Output buffer */
! 24262: ){
! 24263: char *zRelativeCp = convertUtf8PathToCp( zRelative );
! 24264: char zFullCp[CCHMAXPATH] = "\0";
! 24265: char *zFullUTF;
! 24266: APIRET rc = DosQueryPathInfo( (PSZ)zRelativeCp, FIL_QUERYFULLNAME,
! 24267: zFullCp, CCHMAXPATH );
! 24268: free( zRelativeCp );
! 24269: zFullUTF = convertCpPathToUtf8( zFullCp );
! 24270: sqlite3_snprintf( nFull, zFull, zFullUTF );
! 24271: free( zFullUTF );
! 24272: return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
! 24273: }
! 24274:
! 24275:
! 24276: /*
! 24277: ** Open a file.
! 24278: */
! 24279: static int os2Open(
! 24280: sqlite3_vfs *pVfs, /* Not used */
! 24281: const char *zName, /* Name of the file (UTF-8) */
! 24282: sqlite3_file *id, /* Write the SQLite file handle here */
! 24283: int flags, /* Open mode flags */
! 24284: int *pOutFlags /* Status return flags */
! 24285: ){
! 24286: HFILE h;
! 24287: ULONG ulOpenFlags = 0;
! 24288: ULONG ulOpenMode = 0;
! 24289: ULONG ulAction = 0;
! 24290: ULONG rc;
! 24291: os2File *pFile = (os2File*)id;
! 24292: const char *zUtf8Name = zName;
! 24293: char *zNameCp;
! 24294: char zTmpname[CCHMAXPATH];
! 24295:
! 24296: int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
! 24297: int isCreate = (flags & SQLITE_OPEN_CREATE);
! 24298: int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
! 24299: #ifndef NDEBUG
! 24300: int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
! 24301: int isReadonly = (flags & SQLITE_OPEN_READONLY);
! 24302: int eType = (flags & 0xFFFFFF00);
! 24303: int isOpenJournal = (isCreate && (
! 24304: eType==SQLITE_OPEN_MASTER_JOURNAL
! 24305: || eType==SQLITE_OPEN_MAIN_JOURNAL
! 24306: || eType==SQLITE_OPEN_WAL
! 24307: ));
! 24308: #endif
! 24309:
! 24310: UNUSED_PARAMETER(pVfs);
! 24311: assert( id!=0 );
! 24312:
! 24313: /* Check the following statements are true:
! 24314: **
! 24315: ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
! 24316: ** (b) if CREATE is set, then READWRITE must also be set, and
! 24317: ** (c) if EXCLUSIVE is set, then CREATE must also be set.
! 24318: ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
! 24319: */
! 24320: assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
! 24321: assert(isCreate==0 || isReadWrite);
! 24322: assert(isExclusive==0 || isCreate);
! 24323: assert(isDelete==0 || isCreate);
! 24324:
! 24325: /* The main DB, main journal, WAL file and master journal are never
! 24326: ** automatically deleted. Nor are they ever temporary files. */
! 24327: assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
! 24328: assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
! 24329: assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
! 24330: assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
! 24331:
! 24332: /* Assert that the upper layer has set one of the "file-type" flags. */
! 24333: assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
! 24334: || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
! 24335: || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
! 24336: || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
! 24337: );
! 24338:
! 24339: memset( pFile, 0, sizeof(*pFile) );
! 24340: pFile->h = (HFILE)-1;
! 24341:
! 24342: /* If the second argument to this function is NULL, generate a
! 24343: ** temporary file name to use
! 24344: */
! 24345: if( !zUtf8Name ){
! 24346: assert(isDelete && !isOpenJournal);
! 24347: rc = getTempname(CCHMAXPATH, zTmpname);
! 24348: if( rc!=SQLITE_OK ){
! 24349: return rc;
! 24350: }
! 24351: zUtf8Name = zTmpname;
! 24352: }
! 24353:
! 24354: if( isReadWrite ){
! 24355: ulOpenMode |= OPEN_ACCESS_READWRITE;
! 24356: }else{
! 24357: ulOpenMode |= OPEN_ACCESS_READONLY;
! 24358: }
! 24359:
! 24360: /* Open in random access mode for possibly better speed. Allow full
! 24361: ** sharing because file locks will provide exclusive access when needed.
! 24362: ** The handle should not be inherited by child processes and we don't
! 24363: ** want popups from the critical error handler.
! 24364: */
! 24365: ulOpenMode |= OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE |
! 24366: OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR;
! 24367:
! 24368: /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
! 24369: ** created. SQLite doesn't use it to indicate "exclusive access"
! 24370: ** as it is usually understood.
! 24371: */
! 24372: if( isExclusive ){
! 24373: /* Creates a new file, only if it does not already exist. */
! 24374: /* If the file exists, it fails. */
! 24375: ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
! 24376: }else if( isCreate ){
! 24377: /* Open existing file, or create if it doesn't exist */
! 24378: ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
! 24379: }else{
! 24380: /* Opens a file, only if it exists. */
! 24381: ulOpenFlags |= OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
! 24382: }
! 24383:
! 24384: zNameCp = convertUtf8PathToCp( zUtf8Name );
! 24385: rc = DosOpen( (PSZ)zNameCp,
! 24386: &h,
! 24387: &ulAction,
! 24388: 0L,
! 24389: FILE_NORMAL,
! 24390: ulOpenFlags,
! 24391: ulOpenMode,
! 24392: (PEAOP2)NULL );
! 24393: free( zNameCp );
! 24394:
! 24395: if( rc != NO_ERROR ){
! 24396: OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
! 24397: rc, zUtf8Name, ulAction, ulOpenFlags, ulOpenMode ));
! 24398:
! 24399: if( isReadWrite ){
! 24400: return os2Open( pVfs, zName, id,
! 24401: ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
! 24402: pOutFlags );
! 24403: }else{
! 24404: return SQLITE_CANTOPEN;
! 24405: }
! 24406: }
! 24407:
! 24408: if( pOutFlags ){
! 24409: *pOutFlags = isReadWrite ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
! 24410: }
! 24411:
! 24412: os2FullPathname( pVfs, zUtf8Name, sizeof( zTmpname ), zTmpname );
! 24413: pFile->zFullPathCp = convertUtf8PathToCp( zTmpname );
! 24414: pFile->pMethod = &os2IoMethod;
! 24415: pFile->flags = flags;
! 24416: pFile->h = h;
! 24417:
! 24418: OpenCounter(+1);
! 24419: OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
! 24420: return SQLITE_OK;
! 24421: }
! 24422:
! 24423: /*
! 24424: ** Delete the named file.
! 24425: */
! 24426: static int os2Delete(
! 24427: sqlite3_vfs *pVfs, /* Not used on os2 */
! 24428: const char *zFilename, /* Name of file to delete */
! 24429: int syncDir /* Not used on os2 */
! 24430: ){
! 24431: APIRET rc;
! 24432: char *zFilenameCp;
! 24433: SimulateIOError( return SQLITE_IOERR_DELETE );
! 24434: zFilenameCp = convertUtf8PathToCp( zFilename );
! 24435: rc = DosDelete( (PSZ)zFilenameCp );
! 24436: free( zFilenameCp );
! 24437: OSTRACE(( "DELETE \"%s\"\n", zFilename ));
! 24438: return (rc == NO_ERROR ||
! 24439: rc == ERROR_FILE_NOT_FOUND ||
! 24440: rc == ERROR_PATH_NOT_FOUND ) ? SQLITE_OK : SQLITE_IOERR_DELETE;
! 24441: }
! 24442:
! 24443: /*
! 24444: ** Check the existance and status of a file.
! 24445: */
! 24446: static int os2Access(
! 24447: sqlite3_vfs *pVfs, /* Not used on os2 */
! 24448: const char *zFilename, /* Name of file to check */
! 24449: int flags, /* Type of test to make on this file */
! 24450: int *pOut /* Write results here */
! 24451: ){
! 24452: APIRET rc;
! 24453: FILESTATUS3 fsts3ConfigInfo;
! 24454: char *zFilenameCp;
! 24455:
! 24456: UNUSED_PARAMETER(pVfs);
! 24457: SimulateIOError( return SQLITE_IOERR_ACCESS; );
! 24458:
! 24459: zFilenameCp = convertUtf8PathToCp( zFilename );
! 24460: rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
! 24461: &fsts3ConfigInfo, sizeof(FILESTATUS3) );
! 24462: free( zFilenameCp );
! 24463: OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
! 24464: fsts3ConfigInfo.attrFile, flags, rc ));
! 24465:
! 24466: switch( flags ){
! 24467: case SQLITE_ACCESS_EXISTS:
! 24468: /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
! 24469: ** as if it does not exist.
! 24470: */
! 24471: if( fsts3ConfigInfo.cbFile == 0 )
! 24472: rc = ERROR_FILE_NOT_FOUND;
! 24473: break;
! 24474: case SQLITE_ACCESS_READ:
! 24475: break;
! 24476: case SQLITE_ACCESS_READWRITE:
! 24477: if( fsts3ConfigInfo.attrFile & FILE_READONLY )
! 24478: rc = ERROR_ACCESS_DENIED;
! 24479: break;
! 24480: default:
! 24481: rc = ERROR_FILE_NOT_FOUND;
! 24482: assert( !"Invalid flags argument" );
! 24483: }
! 24484:
! 24485: *pOut = (rc == NO_ERROR);
! 24486: OSTRACE(( "ACCESS %s flags %d: rc=%d\n", zFilename, flags, *pOut ));
! 24487:
! 24488: return SQLITE_OK;
! 24489: }
! 24490:
! 24491:
! 24492: #ifndef SQLITE_OMIT_LOAD_EXTENSION
! 24493: /*
! 24494: ** Interfaces for opening a shared library, finding entry points
! 24495: ** within the shared library, and closing the shared library.
! 24496: */
! 24497: /*
! 24498: ** Interfaces for opening a shared library, finding entry points
! 24499: ** within the shared library, and closing the shared library.
! 24500: */
! 24501: static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
! 24502: HMODULE hmod;
! 24503: APIRET rc;
! 24504: char *zFilenameCp = convertUtf8PathToCp(zFilename);
! 24505: rc = DosLoadModule(NULL, 0, (PSZ)zFilenameCp, &hmod);
! 24506: free(zFilenameCp);
! 24507: return rc != NO_ERROR ? 0 : (void*)hmod;
! 24508: }
! 24509: /*
! 24510: ** A no-op since the error code is returned on the DosLoadModule call.
! 24511: ** os2Dlopen returns zero if DosLoadModule is not successful.
! 24512: */
! 24513: static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
! 24514: /* no-op */
! 24515: }
! 24516: static void (*os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
! 24517: PFN pfn;
! 24518: APIRET rc;
! 24519: rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)zSymbol, &pfn);
! 24520: if( rc != NO_ERROR ){
! 24521: /* if the symbol itself was not found, search again for the same
! 24522: * symbol with an extra underscore, that might be needed depending
! 24523: * on the calling convention */
! 24524: char _zSymbol[256] = "_";
! 24525: strncat(_zSymbol, zSymbol, 254);
! 24526: rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)_zSymbol, &pfn);
! 24527: }
! 24528: return rc != NO_ERROR ? 0 : (void(*)(void))pfn;
! 24529: }
! 24530: static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
! 24531: DosFreeModule((HMODULE)pHandle);
! 24532: }
! 24533: #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
! 24534: #define os2DlOpen 0
! 24535: #define os2DlError 0
! 24536: #define os2DlSym 0
! 24537: #define os2DlClose 0
! 24538: #endif
! 24539:
! 24540:
! 24541: /*
! 24542: ** Write up to nBuf bytes of randomness into zBuf.
! 24543: */
! 24544: static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
! 24545: int n = 0;
! 24546: #if defined(SQLITE_TEST)
! 24547: n = nBuf;
! 24548: memset(zBuf, 0, nBuf);
! 24549: #else
! 24550: int i;
! 24551: PPIB ppib;
! 24552: PTIB ptib;
! 24553: DATETIME dt;
! 24554: static unsigned c = 0;
! 24555: /* Ordered by variation probability */
! 24556: static ULONG svIdx[6] = { QSV_MS_COUNT, QSV_TIME_LOW,
! 24557: QSV_MAXPRMEM, QSV_MAXSHMEM,
! 24558: QSV_TOTAVAILMEM, QSV_TOTRESMEM };
! 24559:
! 24560: /* 8 bytes; timezone and weekday don't increase the randomness much */
! 24561: if( (int)sizeof(dt)-3 <= nBuf - n ){
! 24562: c += 0x0100;
! 24563: DosGetDateTime(&dt);
! 24564: dt.year = (USHORT)((dt.year - 1900) | c);
! 24565: memcpy(&zBuf[n], &dt, sizeof(dt)-3);
! 24566: n += sizeof(dt)-3;
! 24567: }
! 24568:
! 24569: /* 4 bytes; PIDs and TIDs are 16 bit internally, so combine them */
! 24570: if( (int)sizeof(ULONG) <= nBuf - n ){
! 24571: DosGetInfoBlocks(&ptib, &ppib);
! 24572: *(PULONG)&zBuf[n] = MAKELONG(ppib->pib_ulpid,
! 24573: ptib->tib_ptib2->tib2_ultid);
! 24574: n += sizeof(ULONG);
! 24575: }
! 24576:
! 24577: /* Up to 6 * 4 bytes; variables depend on the system state */
! 24578: for( i = 0; i < 6 && (int)sizeof(ULONG) <= nBuf - n; i++ ){
! 24579: DosQuerySysInfo(svIdx[i], svIdx[i],
! 24580: (PULONG)&zBuf[n], sizeof(ULONG));
! 24581: n += sizeof(ULONG);
! 24582: }
! 24583: #endif
! 24584:
! 24585: return n;
! 24586: }
! 24587:
! 24588: /*
! 24589: ** Sleep for a little while. Return the amount of time slept.
! 24590: ** The argument is the number of microseconds we want to sleep.
! 24591: ** The return value is the number of microseconds of sleep actually
! 24592: ** requested from the underlying operating system, a number which
! 24593: ** might be greater than or equal to the argument, but not less
! 24594: ** than the argument.
! 24595: */
! 24596: static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
! 24597: DosSleep( (microsec/1000) );
! 24598: return microsec;
! 24599: }
! 24600:
! 24601: /*
! 24602: ** The following variable, if set to a non-zero value, becomes the result
! 24603: ** returned from sqlite3OsCurrentTime(). This is used for testing.
! 24604: */
! 24605: #ifdef SQLITE_TEST
! 24606: SQLITE_API int sqlite3_current_time = 0;
! 24607: #endif
! 24608:
! 24609: /*
! 24610: ** Find the current time (in Universal Coordinated Time). Write into *piNow
! 24611: ** the current time and date as a Julian Day number times 86_400_000. In
! 24612: ** other words, write into *piNow the number of milliseconds since the Julian
! 24613: ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
! 24614: ** proleptic Gregorian calendar.
! 24615: **
! 24616: ** On success, return 0. Return 1 if the time and date cannot be found.
! 24617: */
! 24618: static int os2CurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
! 24619: #ifdef SQLITE_TEST
! 24620: static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
! 24621: #endif
! 24622: int year, month, datepart, timepart;
! 24623:
! 24624: DATETIME dt;
! 24625: DosGetDateTime( &dt );
! 24626:
! 24627: year = dt.year;
! 24628: month = dt.month;
! 24629:
! 24630: /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
! 24631: ** http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c
! 24632: ** Calculate the Julian days
! 24633: */
! 24634: datepart = (int)dt.day - 32076 +
! 24635: 1461*(year + 4800 + (month - 14)/12)/4 +
! 24636: 367*(month - 2 - (month - 14)/12*12)/12 -
! 24637: 3*((year + 4900 + (month - 14)/12)/100)/4;
! 24638:
! 24639: /* Time in milliseconds, hours to noon added */
! 24640: timepart = 12*3600*1000 + dt.hundredths*10 + dt.seconds*1000 +
! 24641: ((int)dt.minutes + dt.timezone)*60*1000 + dt.hours*3600*1000;
! 24642:
! 24643: *piNow = (sqlite3_int64)datepart*86400*1000 + timepart;
! 24644:
! 24645: #ifdef SQLITE_TEST
! 24646: if( sqlite3_current_time ){
! 24647: *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
! 24648: }
! 24649: #endif
! 24650:
! 24651: UNUSED_PARAMETER(pVfs);
! 24652: return 0;
! 24653: }
! 24654:
! 24655: /*
! 24656: ** Find the current time (in Universal Coordinated Time). Write the
! 24657: ** current time and date as a Julian Day number into *prNow and
! 24658: ** return 0. Return 1 if the time and date cannot be found.
! 24659: */
! 24660: static int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
! 24661: int rc;
! 24662: sqlite3_int64 i;
! 24663: rc = os2CurrentTimeInt64(pVfs, &i);
! 24664: if( !rc ){
! 24665: *prNow = i/86400000.0;
! 24666: }
! 24667: return rc;
! 24668: }
! 24669:
! 24670: /*
! 24671: ** The idea is that this function works like a combination of
! 24672: ** GetLastError() and FormatMessage() on windows (or errno and
! 24673: ** strerror_r() on unix). After an error is returned by an OS
! 24674: ** function, SQLite calls this function with zBuf pointing to
! 24675: ** a buffer of nBuf bytes. The OS layer should populate the
! 24676: ** buffer with a nul-terminated UTF-8 encoded error message
! 24677: ** describing the last IO error to have occurred within the calling
! 24678: ** thread.
! 24679: **
! 24680: ** If the error message is too large for the supplied buffer,
! 24681: ** it should be truncated. The return value of xGetLastError
! 24682: ** is zero if the error message fits in the buffer, or non-zero
! 24683: ** otherwise (if the message was truncated). If non-zero is returned,
! 24684: ** then it is not necessary to include the nul-terminator character
! 24685: ** in the output buffer.
! 24686: **
! 24687: ** Not supplying an error message will have no adverse effect
! 24688: ** on SQLite. It is fine to have an implementation that never
! 24689: ** returns an error message:
! 24690: **
! 24691: ** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
! 24692: ** assert(zBuf[0]=='\0');
! 24693: ** return 0;
! 24694: ** }
! 24695: **
! 24696: ** However if an error message is supplied, it will be incorporated
! 24697: ** by sqlite into the error message available to the user using
! 24698: ** sqlite3_errmsg(), possibly making IO errors easier to debug.
! 24699: */
! 24700: static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
! 24701: assert(zBuf[0]=='\0');
! 24702: return 0;
! 24703: }
! 24704:
! 24705: /*
! 24706: ** Initialize and deinitialize the operating system interface.
! 24707: */
! 24708: SQLITE_API int sqlite3_os_init(void){
! 24709: static sqlite3_vfs os2Vfs = {
! 24710: 3, /* iVersion */
! 24711: sizeof(os2File), /* szOsFile */
! 24712: CCHMAXPATH, /* mxPathname */
! 24713: 0, /* pNext */
! 24714: "os2", /* zName */
! 24715: 0, /* pAppData */
! 24716:
! 24717: os2Open, /* xOpen */
! 24718: os2Delete, /* xDelete */
! 24719: os2Access, /* xAccess */
! 24720: os2FullPathname, /* xFullPathname */
! 24721: os2DlOpen, /* xDlOpen */
! 24722: os2DlError, /* xDlError */
! 24723: os2DlSym, /* xDlSym */
! 24724: os2DlClose, /* xDlClose */
! 24725: os2Randomness, /* xRandomness */
! 24726: os2Sleep, /* xSleep */
! 24727: os2CurrentTime, /* xCurrentTime */
! 24728: os2GetLastError, /* xGetLastError */
! 24729: os2CurrentTimeInt64, /* xCurrentTimeInt64 */
! 24730: 0, /* xSetSystemCall */
! 24731: 0, /* xGetSystemCall */
! 24732: 0 /* xNextSystemCall */
! 24733: };
! 24734: sqlite3_vfs_register(&os2Vfs, 1);
! 24735: initUconvObjects();
! 24736: /* sqlite3OSTrace = 1; */
! 24737: return SQLITE_OK;
! 24738: }
! 24739: SQLITE_API int sqlite3_os_end(void){
! 24740: freeUconvObjects();
! 24741: return SQLITE_OK;
! 24742: }
! 24743:
! 24744: #endif /* SQLITE_OS_OS2 */
! 24745:
! 24746: /************** End of os_os2.c **********************************************/
! 24747: /************** Begin file os_unix.c *****************************************/
! 24748: /*
! 24749: ** 2004 May 22
! 24750: **
! 24751: ** The author disclaims copyright to this source code. In place of
! 24752: ** a legal notice, here is a blessing:
! 24753: **
! 24754: ** May you do good and not evil.
! 24755: ** May you find forgiveness for yourself and forgive others.
! 24756: ** May you share freely, never taking more than you give.
! 24757: **
! 24758: ******************************************************************************
! 24759: **
! 24760: ** This file contains the VFS implementation for unix-like operating systems
! 24761: ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
! 24762: **
! 24763: ** There are actually several different VFS implementations in this file.
! 24764: ** The differences are in the way that file locking is done. The default
! 24765: ** implementation uses Posix Advisory Locks. Alternative implementations
! 24766: ** use flock(), dot-files, various proprietary locking schemas, or simply
! 24767: ** skip locking all together.
! 24768: **
! 24769: ** This source file is organized into divisions where the logic for various
! 24770: ** subfunctions is contained within the appropriate division. PLEASE
! 24771: ** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
! 24772: ** in the correct division and should be clearly labeled.
! 24773: **
! 24774: ** The layout of divisions is as follows:
! 24775: **
! 24776: ** * General-purpose declarations and utility functions.
! 24777: ** * Unique file ID logic used by VxWorks.
! 24778: ** * Various locking primitive implementations (all except proxy locking):
! 24779: ** + for Posix Advisory Locks
! 24780: ** + for no-op locks
! 24781: ** + for dot-file locks
! 24782: ** + for flock() locking
! 24783: ** + for named semaphore locks (VxWorks only)
! 24784: ** + for AFP filesystem locks (MacOSX only)
! 24785: ** * sqlite3_file methods not associated with locking.
! 24786: ** * Definitions of sqlite3_io_methods objects for all locking
! 24787: ** methods plus "finder" functions for each locking method.
! 24788: ** * sqlite3_vfs method implementations.
! 24789: ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
! 24790: ** * Definitions of sqlite3_vfs objects for all locking methods
! 24791: ** plus implementations of sqlite3_os_init() and sqlite3_os_end().
! 24792: */
! 24793: #if SQLITE_OS_UNIX /* This file is used on unix only */
! 24794:
! 24795: /*
! 24796: ** There are various methods for file locking used for concurrency
! 24797: ** control:
! 24798: **
! 24799: ** 1. POSIX locking (the default),
! 24800: ** 2. No locking,
! 24801: ** 3. Dot-file locking,
! 24802: ** 4. flock() locking,
! 24803: ** 5. AFP locking (OSX only),
! 24804: ** 6. Named POSIX semaphores (VXWorks only),
! 24805: ** 7. proxy locking. (OSX only)
! 24806: **
! 24807: ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
! 24808: ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
! 24809: ** selection of the appropriate locking style based on the filesystem
! 24810: ** where the database is located.
! 24811: */
! 24812: #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
! 24813: # if defined(__APPLE__)
! 24814: # define SQLITE_ENABLE_LOCKING_STYLE 1
! 24815: # else
! 24816: # define SQLITE_ENABLE_LOCKING_STYLE 0
! 24817: # endif
! 24818: #endif
! 24819:
! 24820: /*
! 24821: ** Define the OS_VXWORKS pre-processor macro to 1 if building on
! 24822: ** vxworks, or 0 otherwise.
! 24823: */
! 24824: #ifndef OS_VXWORKS
! 24825: # if defined(__RTP__) || defined(_WRS_KERNEL)
! 24826: # define OS_VXWORKS 1
! 24827: # else
! 24828: # define OS_VXWORKS 0
! 24829: # endif
! 24830: #endif
! 24831:
! 24832: /*
! 24833: ** These #defines should enable >2GB file support on Posix if the
! 24834: ** underlying operating system supports it. If the OS lacks
! 24835: ** large file support, these should be no-ops.
! 24836: **
! 24837: ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
! 24838: ** on the compiler command line. This is necessary if you are compiling
! 24839: ** on a recent machine (ex: RedHat 7.2) but you want your code to work
! 24840: ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
! 24841: ** without this option, LFS is enable. But LFS does not exist in the kernel
! 24842: ** in RedHat 6.0, so the code won't work. Hence, for maximum binary
! 24843: ** portability you should omit LFS.
! 24844: **
! 24845: ** The previous paragraph was written in 2005. (This paragraph is written
! 24846: ** on 2008-11-28.) These days, all Linux kernels support large files, so
! 24847: ** you should probably leave LFS enabled. But some embedded platforms might
! 24848: ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
! 24849: */
! 24850: #ifndef SQLITE_DISABLE_LFS
! 24851: # define _LARGE_FILE 1
! 24852: # ifndef _FILE_OFFSET_BITS
! 24853: # define _FILE_OFFSET_BITS 64
! 24854: # endif
! 24855: # define _LARGEFILE_SOURCE 1
! 24856: #endif
! 24857:
! 24858: /*
! 24859: ** standard include files.
! 24860: */
! 24861: #include <sys/types.h>
! 24862: #include <sys/stat.h>
! 24863: #include <fcntl.h>
! 24864: #include <unistd.h>
! 24865: /* #include <time.h> */
! 24866: #include <sys/time.h>
! 24867: #include <errno.h>
! 24868: #ifndef SQLITE_OMIT_WAL
! 24869: #include <sys/mman.h>
! 24870: #endif
! 24871:
! 24872:
! 24873: #if SQLITE_ENABLE_LOCKING_STYLE
! 24874: # include <sys/ioctl.h>
! 24875: # if OS_VXWORKS
! 24876: # include <semaphore.h>
! 24877: # include <limits.h>
! 24878: # else
! 24879: # include <sys/file.h>
! 24880: # include <sys/param.h>
! 24881: # endif
! 24882: #endif /* SQLITE_ENABLE_LOCKING_STYLE */
! 24883:
! 24884: #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
! 24885: # include <sys/mount.h>
! 24886: #endif
! 24887:
! 24888: #ifdef HAVE_UTIME
! 24889: # include <utime.h>
! 24890: #endif
! 24891:
! 24892: /*
! 24893: ** Allowed values of unixFile.fsFlags
! 24894: */
! 24895: #define SQLITE_FSFLAGS_IS_MSDOS 0x1
! 24896:
! 24897: /*
! 24898: ** If we are to be thread-safe, include the pthreads header and define
! 24899: ** the SQLITE_UNIX_THREADS macro.
! 24900: */
! 24901: #if SQLITE_THREADSAFE
! 24902: /* # include <pthread.h> */
! 24903: # define SQLITE_UNIX_THREADS 1
! 24904: #endif
! 24905:
! 24906: /*
! 24907: ** Default permissions when creating a new file
! 24908: */
! 24909: #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
! 24910: # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
! 24911: #endif
! 24912:
! 24913: /*
! 24914: ** Default permissions when creating auto proxy dir
! 24915: */
! 24916: #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
! 24917: # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
! 24918: #endif
! 24919:
! 24920: /*
! 24921: ** Maximum supported path-length.
! 24922: */
! 24923: #define MAX_PATHNAME 512
! 24924:
! 24925: /*
! 24926: ** Only set the lastErrno if the error code is a real error and not
! 24927: ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
! 24928: */
! 24929: #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
! 24930:
! 24931: /* Forward references */
! 24932: typedef struct unixShm unixShm; /* Connection shared memory */
! 24933: typedef struct unixShmNode unixShmNode; /* Shared memory instance */
! 24934: typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
! 24935: typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
! 24936:
! 24937: /*
! 24938: ** Sometimes, after a file handle is closed by SQLite, the file descriptor
! 24939: ** cannot be closed immediately. In these cases, instances of the following
! 24940: ** structure are used to store the file descriptor while waiting for an
! 24941: ** opportunity to either close or reuse it.
! 24942: */
! 24943: struct UnixUnusedFd {
! 24944: int fd; /* File descriptor to close */
! 24945: int flags; /* Flags this file descriptor was opened with */
! 24946: UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
! 24947: };
! 24948:
! 24949: /*
! 24950: ** The unixFile structure is subclass of sqlite3_file specific to the unix
! 24951: ** VFS implementations.
! 24952: */
! 24953: typedef struct unixFile unixFile;
! 24954: struct unixFile {
! 24955: sqlite3_io_methods const *pMethod; /* Always the first entry */
! 24956: sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
! 24957: unixInodeInfo *pInode; /* Info about locks on this inode */
! 24958: int h; /* The file descriptor */
! 24959: unsigned char eFileLock; /* The type of lock held on this fd */
! 24960: unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
! 24961: int lastErrno; /* The unix errno from last I/O error */
! 24962: void *lockingContext; /* Locking style specific state */
! 24963: UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
! 24964: const char *zPath; /* Name of the file */
! 24965: unixShm *pShm; /* Shared memory segment information */
! 24966: int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
! 24967: #if SQLITE_ENABLE_LOCKING_STYLE
! 24968: int openFlags; /* The flags specified at open() */
! 24969: #endif
! 24970: #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
! 24971: unsigned fsFlags; /* cached details from statfs() */
! 24972: #endif
! 24973: #if OS_VXWORKS
! 24974: struct vxworksFileId *pId; /* Unique file ID */
! 24975: #endif
! 24976: #ifndef NDEBUG
! 24977: /* The next group of variables are used to track whether or not the
! 24978: ** transaction counter in bytes 24-27 of database files are updated
! 24979: ** whenever any part of the database changes. An assertion fault will
! 24980: ** occur if a file is updated without also updating the transaction
! 24981: ** counter. This test is made to avoid new problems similar to the
! 24982: ** one described by ticket #3584.
! 24983: */
! 24984: unsigned char transCntrChng; /* True if the transaction counter changed */
! 24985: unsigned char dbUpdate; /* True if any part of database file changed */
! 24986: unsigned char inNormalWrite; /* True if in a normal write operation */
! 24987: #endif
! 24988: #ifdef SQLITE_TEST
! 24989: /* In test mode, increase the size of this structure a bit so that
! 24990: ** it is larger than the struct CrashFile defined in test6.c.
! 24991: */
! 24992: char aPadding[32];
! 24993: #endif
! 24994: };
! 24995:
! 24996: /*
! 24997: ** Allowed values for the unixFile.ctrlFlags bitmask:
! 24998: */
! 24999: #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
! 25000: #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
! 25001: #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
! 25002: #ifndef SQLITE_DISABLE_DIRSYNC
! 25003: # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
! 25004: #else
! 25005: # define UNIXFILE_DIRSYNC 0x00
! 25006: #endif
! 25007: #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
! 25008: #define UNIXFILE_DELETE 0x20 /* Delete on close */
! 25009: #define UNIXFILE_URI 0x40 /* Filename might have query parameters */
! 25010: #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
! 25011:
! 25012: /*
! 25013: ** Include code that is common to all os_*.c files
! 25014: */
! 25015: /************** Include os_common.h in the middle of os_unix.c ***************/
! 25016: /************** Begin file os_common.h ***************************************/
! 25017: /*
! 25018: ** 2004 May 22
! 25019: **
! 25020: ** The author disclaims copyright to this source code. In place of
! 25021: ** a legal notice, here is a blessing:
! 25022: **
! 25023: ** May you do good and not evil.
! 25024: ** May you find forgiveness for yourself and forgive others.
! 25025: ** May you share freely, never taking more than you give.
! 25026: **
! 25027: ******************************************************************************
! 25028: **
! 25029: ** This file contains macros and a little bit of code that is common to
! 25030: ** all of the platform-specific files (os_*.c) and is #included into those
! 25031: ** files.
! 25032: **
! 25033: ** This file should be #included by the os_*.c files only. It is not a
! 25034: ** general purpose header file.
! 25035: */
! 25036: #ifndef _OS_COMMON_H_
! 25037: #define _OS_COMMON_H_
! 25038:
! 25039: /*
! 25040: ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
! 25041: ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
! 25042: ** switch. The following code should catch this problem at compile-time.
! 25043: */
! 25044: #ifdef MEMORY_DEBUG
! 25045: # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
! 25046: #endif
! 25047:
! 25048: #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
! 25049: # ifndef SQLITE_DEBUG_OS_TRACE
! 25050: # define SQLITE_DEBUG_OS_TRACE 0
! 25051: # endif
! 25052: int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
! 25053: # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
! 25054: #else
! 25055: # define OSTRACE(X)
! 25056: #endif
! 25057:
! 25058: /*
! 25059: ** Macros for performance tracing. Normally turned off. Only works
! 25060: ** on i486 hardware.
! 25061: */
! 25062: #ifdef SQLITE_PERFORMANCE_TRACE
! 25063:
! 25064: /*
! 25065: ** hwtime.h contains inline assembler code for implementing
! 25066: ** high-performance timing routines.
! 25067: */
! 25068: /************** Include hwtime.h in the middle of os_common.h ****************/
! 25069: /************** Begin file hwtime.h ******************************************/
! 25070: /*
! 25071: ** 2008 May 27
! 25072: **
! 25073: ** The author disclaims copyright to this source code. In place of
! 25074: ** a legal notice, here is a blessing:
! 25075: **
! 25076: ** May you do good and not evil.
! 25077: ** May you find forgiveness for yourself and forgive others.
! 25078: ** May you share freely, never taking more than you give.
! 25079: **
! 25080: ******************************************************************************
! 25081: **
! 25082: ** This file contains inline asm code for retrieving "high-performance"
! 25083: ** counters for x86 class CPUs.
! 25084: */
! 25085: #ifndef _HWTIME_H_
! 25086: #define _HWTIME_H_
! 25087:
! 25088: /*
! 25089: ** The following routine only works on pentium-class (or newer) processors.
! 25090: ** It uses the RDTSC opcode to read the cycle count value out of the
! 25091: ** processor and returns that value. This can be used for high-res
! 25092: ** profiling.
! 25093: */
! 25094: #if (defined(__GNUC__) || defined(_MSC_VER)) && \
! 25095: (defined(i386) || defined(__i386__) || defined(_M_IX86))
! 25096:
! 25097: #if defined(__GNUC__)
! 25098:
! 25099: __inline__ sqlite_uint64 sqlite3Hwtime(void){
! 25100: unsigned int lo, hi;
! 25101: __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
! 25102: return (sqlite_uint64)hi << 32 | lo;
! 25103: }
! 25104:
! 25105: #elif defined(_MSC_VER)
! 25106:
! 25107: __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
! 25108: __asm {
! 25109: rdtsc
! 25110: ret ; return value at EDX:EAX
! 25111: }
! 25112: }
! 25113:
! 25114: #endif
! 25115:
! 25116: #elif (defined(__GNUC__) && defined(__x86_64__))
! 25117:
! 25118: __inline__ sqlite_uint64 sqlite3Hwtime(void){
! 25119: unsigned long val;
! 25120: __asm__ __volatile__ ("rdtsc" : "=A" (val));
! 25121: return val;
! 25122: }
! 25123:
! 25124: #elif (defined(__GNUC__) && defined(__ppc__))
! 25125:
! 25126: __inline__ sqlite_uint64 sqlite3Hwtime(void){
! 25127: unsigned long long retval;
! 25128: unsigned long junk;
! 25129: __asm__ __volatile__ ("\n\
! 25130: 1: mftbu %1\n\
! 25131: mftb %L0\n\
! 25132: mftbu %0\n\
! 25133: cmpw %0,%1\n\
! 25134: bne 1b"
! 25135: : "=r" (retval), "=r" (junk));
! 25136: return retval;
! 25137: }
! 25138:
! 25139: #else
! 25140:
! 25141: #error Need implementation of sqlite3Hwtime() for your platform.
! 25142:
! 25143: /*
! 25144: ** To compile without implementing sqlite3Hwtime() for your platform,
! 25145: ** you can remove the above #error and use the following
! 25146: ** stub function. You will lose timing support for many
! 25147: ** of the debugging and testing utilities, but it should at
! 25148: ** least compile and run.
! 25149: */
! 25150: SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
! 25151:
! 25152: #endif
! 25153:
! 25154: #endif /* !defined(_HWTIME_H_) */
! 25155:
! 25156: /************** End of hwtime.h **********************************************/
! 25157: /************** Continuing where we left off in os_common.h ******************/
! 25158:
! 25159: static sqlite_uint64 g_start;
! 25160: static sqlite_uint64 g_elapsed;
! 25161: #define TIMER_START g_start=sqlite3Hwtime()
! 25162: #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
! 25163: #define TIMER_ELAPSED g_elapsed
! 25164: #else
! 25165: #define TIMER_START
! 25166: #define TIMER_END
! 25167: #define TIMER_ELAPSED ((sqlite_uint64)0)
! 25168: #endif
! 25169:
! 25170: /*
! 25171: ** If we compile with the SQLITE_TEST macro set, then the following block
! 25172: ** of code will give us the ability to simulate a disk I/O error. This
! 25173: ** is used for testing the I/O recovery logic.
! 25174: */
! 25175: #ifdef SQLITE_TEST
! 25176: SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
! 25177: SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
! 25178: SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
! 25179: SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
! 25180: SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
! 25181: SQLITE_API int sqlite3_diskfull_pending = 0;
! 25182: SQLITE_API int sqlite3_diskfull = 0;
! 25183: #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
! 25184: #define SimulateIOError(CODE) \
! 25185: if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
! 25186: || sqlite3_io_error_pending-- == 1 ) \
! 25187: { local_ioerr(); CODE; }
! 25188: static void local_ioerr(){
! 25189: IOTRACE(("IOERR\n"));
! 25190: sqlite3_io_error_hit++;
! 25191: if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
! 25192: }
! 25193: #define SimulateDiskfullError(CODE) \
! 25194: if( sqlite3_diskfull_pending ){ \
! 25195: if( sqlite3_diskfull_pending == 1 ){ \
! 25196: local_ioerr(); \
! 25197: sqlite3_diskfull = 1; \
! 25198: sqlite3_io_error_hit = 1; \
! 25199: CODE; \
! 25200: }else{ \
! 25201: sqlite3_diskfull_pending--; \
! 25202: } \
! 25203: }
! 25204: #else
! 25205: #define SimulateIOErrorBenign(X)
! 25206: #define SimulateIOError(A)
! 25207: #define SimulateDiskfullError(A)
! 25208: #endif
! 25209:
! 25210: /*
! 25211: ** When testing, keep a count of the number of open files.
! 25212: */
! 25213: #ifdef SQLITE_TEST
! 25214: SQLITE_API int sqlite3_open_file_count = 0;
! 25215: #define OpenCounter(X) sqlite3_open_file_count+=(X)
! 25216: #else
! 25217: #define OpenCounter(X)
! 25218: #endif
! 25219:
! 25220: #endif /* !defined(_OS_COMMON_H_) */
! 25221:
! 25222: /************** End of os_common.h *******************************************/
! 25223: /************** Continuing where we left off in os_unix.c ********************/
! 25224:
! 25225: /*
! 25226: ** Define various macros that are missing from some systems.
! 25227: */
! 25228: #ifndef O_LARGEFILE
! 25229: # define O_LARGEFILE 0
! 25230: #endif
! 25231: #ifdef SQLITE_DISABLE_LFS
! 25232: # undef O_LARGEFILE
! 25233: # define O_LARGEFILE 0
! 25234: #endif
! 25235: #ifndef O_NOFOLLOW
! 25236: # define O_NOFOLLOW 0
! 25237: #endif
! 25238: #ifndef O_BINARY
! 25239: # define O_BINARY 0
! 25240: #endif
! 25241:
! 25242: /*
! 25243: ** The threadid macro resolves to the thread-id or to 0. Used for
! 25244: ** testing and debugging only.
! 25245: */
! 25246: #if SQLITE_THREADSAFE
! 25247: #define threadid pthread_self()
! 25248: #else
! 25249: #define threadid 0
! 25250: #endif
! 25251:
! 25252: /*
! 25253: ** Different Unix systems declare open() in different ways. Same use
! 25254: ** open(const char*,int,mode_t). Others use open(const char*,int,...).
! 25255: ** The difference is important when using a pointer to the function.
! 25256: **
! 25257: ** The safest way to deal with the problem is to always use this wrapper
! 25258: ** which always has the same well-defined interface.
! 25259: */
! 25260: static int posixOpen(const char *zFile, int flags, int mode){
! 25261: return open(zFile, flags, mode);
! 25262: }
! 25263:
! 25264: /* Forward reference */
! 25265: static int openDirectory(const char*, int*);
! 25266:
! 25267: /*
! 25268: ** Many system calls are accessed through pointer-to-functions so that
! 25269: ** they may be overridden at runtime to facilitate fault injection during
! 25270: ** testing and sandboxing. The following array holds the names and pointers
! 25271: ** to all overrideable system calls.
! 25272: */
! 25273: static struct unix_syscall {
! 25274: const char *zName; /* Name of the sytem call */
! 25275: sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
! 25276: sqlite3_syscall_ptr pDefault; /* Default value */
! 25277: } aSyscall[] = {
! 25278: { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
! 25279: #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
! 25280:
! 25281: { "close", (sqlite3_syscall_ptr)close, 0 },
! 25282: #define osClose ((int(*)(int))aSyscall[1].pCurrent)
! 25283:
! 25284: { "access", (sqlite3_syscall_ptr)access, 0 },
! 25285: #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent)
! 25286:
! 25287: { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
! 25288: #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
! 25289:
! 25290: { "stat", (sqlite3_syscall_ptr)stat, 0 },
! 25291: #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
! 25292:
! 25293: /*
! 25294: ** The DJGPP compiler environment looks mostly like Unix, but it
! 25295: ** lacks the fcntl() system call. So redefine fcntl() to be something
! 25296: ** that always succeeds. This means that locking does not occur under
! 25297: ** DJGPP. But it is DOS - what did you expect?
! 25298: */
! 25299: #ifdef __DJGPP__
! 25300: { "fstat", 0, 0 },
! 25301: #define osFstat(a,b,c) 0
! 25302: #else
! 25303: { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
! 25304: #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
! 25305: #endif
! 25306:
! 25307: { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
! 25308: #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
! 25309:
! 25310: { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
! 25311: #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
! 25312:
! 25313: { "read", (sqlite3_syscall_ptr)read, 0 },
! 25314: #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
! 25315:
! 25316: #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
! 25317: { "pread", (sqlite3_syscall_ptr)pread, 0 },
! 25318: #else
! 25319: { "pread", (sqlite3_syscall_ptr)0, 0 },
! 25320: #endif
! 25321: #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
! 25322:
! 25323: #if defined(USE_PREAD64)
! 25324: { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
! 25325: #else
! 25326: { "pread64", (sqlite3_syscall_ptr)0, 0 },
! 25327: #endif
! 25328: #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
! 25329:
! 25330: { "write", (sqlite3_syscall_ptr)write, 0 },
! 25331: #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
! 25332:
! 25333: #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
! 25334: { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
! 25335: #else
! 25336: { "pwrite", (sqlite3_syscall_ptr)0, 0 },
! 25337: #endif
! 25338: #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
! 25339: aSyscall[12].pCurrent)
! 25340:
! 25341: #if defined(USE_PREAD64)
! 25342: { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
! 25343: #else
! 25344: { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
! 25345: #endif
! 25346: #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\
! 25347: aSyscall[13].pCurrent)
! 25348:
! 25349: #if SQLITE_ENABLE_LOCKING_STYLE
! 25350: { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
! 25351: #else
! 25352: { "fchmod", (sqlite3_syscall_ptr)0, 0 },
! 25353: #endif
! 25354: #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
! 25355:
! 25356: #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
! 25357: { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
! 25358: #else
! 25359: { "fallocate", (sqlite3_syscall_ptr)0, 0 },
! 25360: #endif
! 25361: #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
! 25362:
! 25363: { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
! 25364: #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
! 25365:
! 25366: { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
! 25367: #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
! 25368:
! 25369: { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
! 25370: #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
! 25371:
! 25372: { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
! 25373: #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
! 25374:
! 25375: }; /* End of the overrideable system calls */
! 25376:
! 25377: /*
! 25378: ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
! 25379: ** "unix" VFSes. Return SQLITE_OK opon successfully updating the
! 25380: ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
! 25381: ** system call named zName.
! 25382: */
! 25383: static int unixSetSystemCall(
! 25384: sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
! 25385: const char *zName, /* Name of system call to override */
! 25386: sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
! 25387: ){
! 25388: unsigned int i;
! 25389: int rc = SQLITE_NOTFOUND;
! 25390:
! 25391: UNUSED_PARAMETER(pNotUsed);
! 25392: if( zName==0 ){
! 25393: /* If no zName is given, restore all system calls to their default
! 25394: ** settings and return NULL
! 25395: */
! 25396: rc = SQLITE_OK;
! 25397: for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
! 25398: if( aSyscall[i].pDefault ){
! 25399: aSyscall[i].pCurrent = aSyscall[i].pDefault;
! 25400: }
! 25401: }
! 25402: }else{
! 25403: /* If zName is specified, operate on only the one system call
! 25404: ** specified.
! 25405: */
! 25406: for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
! 25407: if( strcmp(zName, aSyscall[i].zName)==0 ){
! 25408: if( aSyscall[i].pDefault==0 ){
! 25409: aSyscall[i].pDefault = aSyscall[i].pCurrent;
! 25410: }
! 25411: rc = SQLITE_OK;
! 25412: if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
! 25413: aSyscall[i].pCurrent = pNewFunc;
! 25414: break;
! 25415: }
! 25416: }
! 25417: }
! 25418: return rc;
! 25419: }
! 25420:
! 25421: /*
! 25422: ** Return the value of a system call. Return NULL if zName is not a
! 25423: ** recognized system call name. NULL is also returned if the system call
! 25424: ** is currently undefined.
! 25425: */
! 25426: static sqlite3_syscall_ptr unixGetSystemCall(
! 25427: sqlite3_vfs *pNotUsed,
! 25428: const char *zName
! 25429: ){
! 25430: unsigned int i;
! 25431:
! 25432: UNUSED_PARAMETER(pNotUsed);
! 25433: for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
! 25434: if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
! 25435: }
! 25436: return 0;
! 25437: }
! 25438:
! 25439: /*
! 25440: ** Return the name of the first system call after zName. If zName==NULL
! 25441: ** then return the name of the first system call. Return NULL if zName
! 25442: ** is the last system call or if zName is not the name of a valid
! 25443: ** system call.
! 25444: */
! 25445: static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
! 25446: int i = -1;
! 25447:
! 25448: UNUSED_PARAMETER(p);
! 25449: if( zName ){
! 25450: for(i=0; i<ArraySize(aSyscall)-1; i++){
! 25451: if( strcmp(zName, aSyscall[i].zName)==0 ) break;
! 25452: }
! 25453: }
! 25454: for(i++; i<ArraySize(aSyscall); i++){
! 25455: if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
! 25456: }
! 25457: return 0;
! 25458: }
! 25459:
! 25460: /*
! 25461: ** Retry open() calls that fail due to EINTR
! 25462: */
! 25463: static int robust_open(const char *z, int f, int m){
! 25464: int rc;
! 25465: do{ rc = osOpen(z,f,m); }while( rc<0 && errno==EINTR );
! 25466: return rc;
! 25467: }
! 25468:
! 25469: /*
! 25470: ** Helper functions to obtain and relinquish the global mutex. The
! 25471: ** global mutex is used to protect the unixInodeInfo and
! 25472: ** vxworksFileId objects used by this file, all of which may be
! 25473: ** shared by multiple threads.
! 25474: **
! 25475: ** Function unixMutexHeld() is used to assert() that the global mutex
! 25476: ** is held when required. This function is only used as part of assert()
! 25477: ** statements. e.g.
! 25478: **
! 25479: ** unixEnterMutex()
! 25480: ** assert( unixMutexHeld() );
! 25481: ** unixEnterLeave()
! 25482: */
! 25483: static void unixEnterMutex(void){
! 25484: sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 25485: }
! 25486: static void unixLeaveMutex(void){
! 25487: sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 25488: }
! 25489: #ifdef SQLITE_DEBUG
! 25490: static int unixMutexHeld(void) {
! 25491: return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 25492: }
! 25493: #endif
! 25494:
! 25495:
! 25496: #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
! 25497: /*
! 25498: ** Helper function for printing out trace information from debugging
! 25499: ** binaries. This returns the string represetation of the supplied
! 25500: ** integer lock-type.
! 25501: */
! 25502: static const char *azFileLock(int eFileLock){
! 25503: switch( eFileLock ){
! 25504: case NO_LOCK: return "NONE";
! 25505: case SHARED_LOCK: return "SHARED";
! 25506: case RESERVED_LOCK: return "RESERVED";
! 25507: case PENDING_LOCK: return "PENDING";
! 25508: case EXCLUSIVE_LOCK: return "EXCLUSIVE";
! 25509: }
! 25510: return "ERROR";
! 25511: }
! 25512: #endif
! 25513:
! 25514: #ifdef SQLITE_LOCK_TRACE
! 25515: /*
! 25516: ** Print out information about all locking operations.
! 25517: **
! 25518: ** This routine is used for troubleshooting locks on multithreaded
! 25519: ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
! 25520: ** command-line option on the compiler. This code is normally
! 25521: ** turned off.
! 25522: */
! 25523: static int lockTrace(int fd, int op, struct flock *p){
! 25524: char *zOpName, *zType;
! 25525: int s;
! 25526: int savedErrno;
! 25527: if( op==F_GETLK ){
! 25528: zOpName = "GETLK";
! 25529: }else if( op==F_SETLK ){
! 25530: zOpName = "SETLK";
! 25531: }else{
! 25532: s = osFcntl(fd, op, p);
! 25533: sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
! 25534: return s;
! 25535: }
! 25536: if( p->l_type==F_RDLCK ){
! 25537: zType = "RDLCK";
! 25538: }else if( p->l_type==F_WRLCK ){
! 25539: zType = "WRLCK";
! 25540: }else if( p->l_type==F_UNLCK ){
! 25541: zType = "UNLCK";
! 25542: }else{
! 25543: assert( 0 );
! 25544: }
! 25545: assert( p->l_whence==SEEK_SET );
! 25546: s = osFcntl(fd, op, p);
! 25547: savedErrno = errno;
! 25548: sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
! 25549: threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
! 25550: (int)p->l_pid, s);
! 25551: if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
! 25552: struct flock l2;
! 25553: l2 = *p;
! 25554: osFcntl(fd, F_GETLK, &l2);
! 25555: if( l2.l_type==F_RDLCK ){
! 25556: zType = "RDLCK";
! 25557: }else if( l2.l_type==F_WRLCK ){
! 25558: zType = "WRLCK";
! 25559: }else if( l2.l_type==F_UNLCK ){
! 25560: zType = "UNLCK";
! 25561: }else{
! 25562: assert( 0 );
! 25563: }
! 25564: sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
! 25565: zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
! 25566: }
! 25567: errno = savedErrno;
! 25568: return s;
! 25569: }
! 25570: #undef osFcntl
! 25571: #define osFcntl lockTrace
! 25572: #endif /* SQLITE_LOCK_TRACE */
! 25573:
! 25574: /*
! 25575: ** Retry ftruncate() calls that fail due to EINTR
! 25576: */
! 25577: static int robust_ftruncate(int h, sqlite3_int64 sz){
! 25578: int rc;
! 25579: do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
! 25580: return rc;
! 25581: }
! 25582:
! 25583: /*
! 25584: ** This routine translates a standard POSIX errno code into something
! 25585: ** useful to the clients of the sqlite3 functions. Specifically, it is
! 25586: ** intended to translate a variety of "try again" errors into SQLITE_BUSY
! 25587: ** and a variety of "please close the file descriptor NOW" errors into
! 25588: ** SQLITE_IOERR
! 25589: **
! 25590: ** Errors during initialization of locks, or file system support for locks,
! 25591: ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
! 25592: */
! 25593: static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
! 25594: switch (posixError) {
! 25595: #if 0
! 25596: /* At one point this code was not commented out. In theory, this branch
! 25597: ** should never be hit, as this function should only be called after
! 25598: ** a locking-related function (i.e. fcntl()) has returned non-zero with
! 25599: ** the value of errno as the first argument. Since a system call has failed,
! 25600: ** errno should be non-zero.
! 25601: **
! 25602: ** Despite this, if errno really is zero, we still don't want to return
! 25603: ** SQLITE_OK. The system call failed, and *some* SQLite error should be
! 25604: ** propagated back to the caller. Commenting this branch out means errno==0
! 25605: ** will be handled by the "default:" case below.
! 25606: */
! 25607: case 0:
! 25608: return SQLITE_OK;
! 25609: #endif
! 25610:
! 25611: case EAGAIN:
! 25612: case ETIMEDOUT:
! 25613: case EBUSY:
! 25614: case EINTR:
! 25615: case ENOLCK:
! 25616: /* random NFS retry error, unless during file system support
! 25617: * introspection, in which it actually means what it says */
! 25618: return SQLITE_BUSY;
! 25619:
! 25620: case EACCES:
! 25621: /* EACCES is like EAGAIN during locking operations, but not any other time*/
! 25622: if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
! 25623: (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
! 25624: (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
! 25625: (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
! 25626: return SQLITE_BUSY;
! 25627: }
! 25628: /* else fall through */
! 25629: case EPERM:
! 25630: return SQLITE_PERM;
! 25631:
! 25632: /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
! 25633: ** this module never makes such a call. And the code in SQLite itself
! 25634: ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
! 25635: ** this case is also commented out. If the system does set errno to EDEADLK,
! 25636: ** the default SQLITE_IOERR_XXX code will be returned. */
! 25637: #if 0
! 25638: case EDEADLK:
! 25639: return SQLITE_IOERR_BLOCKED;
! 25640: #endif
! 25641:
! 25642: #if EOPNOTSUPP!=ENOTSUP
! 25643: case EOPNOTSUPP:
! 25644: /* something went terribly awry, unless during file system support
! 25645: * introspection, in which it actually means what it says */
! 25646: #endif
! 25647: #ifdef ENOTSUP
! 25648: case ENOTSUP:
! 25649: /* invalid fd, unless during file system support introspection, in which
! 25650: * it actually means what it says */
! 25651: #endif
! 25652: case EIO:
! 25653: case EBADF:
! 25654: case EINVAL:
! 25655: case ENOTCONN:
! 25656: case ENODEV:
! 25657: case ENXIO:
! 25658: case ENOENT:
! 25659: #ifdef ESTALE /* ESTALE is not defined on Interix systems */
! 25660: case ESTALE:
! 25661: #endif
! 25662: case ENOSYS:
! 25663: /* these should force the client to close the file and reconnect */
! 25664:
! 25665: default:
! 25666: return sqliteIOErr;
! 25667: }
! 25668: }
! 25669:
! 25670:
! 25671:
! 25672: /******************************************************************************
! 25673: ****************** Begin Unique File ID Utility Used By VxWorks ***************
! 25674: **
! 25675: ** On most versions of unix, we can get a unique ID for a file by concatenating
! 25676: ** the device number and the inode number. But this does not work on VxWorks.
! 25677: ** On VxWorks, a unique file id must be based on the canonical filename.
! 25678: **
! 25679: ** A pointer to an instance of the following structure can be used as a
! 25680: ** unique file ID in VxWorks. Each instance of this structure contains
! 25681: ** a copy of the canonical filename. There is also a reference count.
! 25682: ** The structure is reclaimed when the number of pointers to it drops to
! 25683: ** zero.
! 25684: **
! 25685: ** There are never very many files open at one time and lookups are not
! 25686: ** a performance-critical path, so it is sufficient to put these
! 25687: ** structures on a linked list.
! 25688: */
! 25689: struct vxworksFileId {
! 25690: struct vxworksFileId *pNext; /* Next in a list of them all */
! 25691: int nRef; /* Number of references to this one */
! 25692: int nName; /* Length of the zCanonicalName[] string */
! 25693: char *zCanonicalName; /* Canonical filename */
! 25694: };
! 25695:
! 25696: #if OS_VXWORKS
! 25697: /*
! 25698: ** All unique filenames are held on a linked list headed by this
! 25699: ** variable:
! 25700: */
! 25701: static struct vxworksFileId *vxworksFileList = 0;
! 25702:
! 25703: /*
! 25704: ** Simplify a filename into its canonical form
! 25705: ** by making the following changes:
! 25706: **
! 25707: ** * removing any trailing and duplicate /
! 25708: ** * convert /./ into just /
! 25709: ** * convert /A/../ where A is any simple name into just /
! 25710: **
! 25711: ** Changes are made in-place. Return the new name length.
! 25712: **
! 25713: ** The original filename is in z[0..n-1]. Return the number of
! 25714: ** characters in the simplified name.
! 25715: */
! 25716: static int vxworksSimplifyName(char *z, int n){
! 25717: int i, j;
! 25718: while( n>1 && z[n-1]=='/' ){ n--; }
! 25719: for(i=j=0; i<n; i++){
! 25720: if( z[i]=='/' ){
! 25721: if( z[i+1]=='/' ) continue;
! 25722: if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
! 25723: i += 1;
! 25724: continue;
! 25725: }
! 25726: if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
! 25727: while( j>0 && z[j-1]!='/' ){ j--; }
! 25728: if( j>0 ){ j--; }
! 25729: i += 2;
! 25730: continue;
! 25731: }
! 25732: }
! 25733: z[j++] = z[i];
! 25734: }
! 25735: z[j] = 0;
! 25736: return j;
! 25737: }
! 25738:
! 25739: /*
! 25740: ** Find a unique file ID for the given absolute pathname. Return
! 25741: ** a pointer to the vxworksFileId object. This pointer is the unique
! 25742: ** file ID.
! 25743: **
! 25744: ** The nRef field of the vxworksFileId object is incremented before
! 25745: ** the object is returned. A new vxworksFileId object is created
! 25746: ** and added to the global list if necessary.
! 25747: **
! 25748: ** If a memory allocation error occurs, return NULL.
! 25749: */
! 25750: static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
! 25751: struct vxworksFileId *pNew; /* search key and new file ID */
! 25752: struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
! 25753: int n; /* Length of zAbsoluteName string */
! 25754:
! 25755: assert( zAbsoluteName[0]=='/' );
! 25756: n = (int)strlen(zAbsoluteName);
! 25757: pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
! 25758: if( pNew==0 ) return 0;
! 25759: pNew->zCanonicalName = (char*)&pNew[1];
! 25760: memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
! 25761: n = vxworksSimplifyName(pNew->zCanonicalName, n);
! 25762:
! 25763: /* Search for an existing entry that matching the canonical name.
! 25764: ** If found, increment the reference count and return a pointer to
! 25765: ** the existing file ID.
! 25766: */
! 25767: unixEnterMutex();
! 25768: for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
! 25769: if( pCandidate->nName==n
! 25770: && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
! 25771: ){
! 25772: sqlite3_free(pNew);
! 25773: pCandidate->nRef++;
! 25774: unixLeaveMutex();
! 25775: return pCandidate;
! 25776: }
! 25777: }
! 25778:
! 25779: /* No match was found. We will make a new file ID */
! 25780: pNew->nRef = 1;
! 25781: pNew->nName = n;
! 25782: pNew->pNext = vxworksFileList;
! 25783: vxworksFileList = pNew;
! 25784: unixLeaveMutex();
! 25785: return pNew;
! 25786: }
! 25787:
! 25788: /*
! 25789: ** Decrement the reference count on a vxworksFileId object. Free
! 25790: ** the object when the reference count reaches zero.
! 25791: */
! 25792: static void vxworksReleaseFileId(struct vxworksFileId *pId){
! 25793: unixEnterMutex();
! 25794: assert( pId->nRef>0 );
! 25795: pId->nRef--;
! 25796: if( pId->nRef==0 ){
! 25797: struct vxworksFileId **pp;
! 25798: for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
! 25799: assert( *pp==pId );
! 25800: *pp = pId->pNext;
! 25801: sqlite3_free(pId);
! 25802: }
! 25803: unixLeaveMutex();
! 25804: }
! 25805: #endif /* OS_VXWORKS */
! 25806: /*************** End of Unique File ID Utility Used By VxWorks ****************
! 25807: ******************************************************************************/
! 25808:
! 25809:
! 25810: /******************************************************************************
! 25811: *************************** Posix Advisory Locking ****************************
! 25812: **
! 25813: ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
! 25814: ** section 6.5.2.2 lines 483 through 490 specify that when a process
! 25815: ** sets or clears a lock, that operation overrides any prior locks set
! 25816: ** by the same process. It does not explicitly say so, but this implies
! 25817: ** that it overrides locks set by the same process using a different
! 25818: ** file descriptor. Consider this test case:
! 25819: **
! 25820: ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
! 25821: ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
! 25822: **
! 25823: ** Suppose ./file1 and ./file2 are really the same file (because
! 25824: ** one is a hard or symbolic link to the other) then if you set
! 25825: ** an exclusive lock on fd1, then try to get an exclusive lock
! 25826: ** on fd2, it works. I would have expected the second lock to
! 25827: ** fail since there was already a lock on the file due to fd1.
! 25828: ** But not so. Since both locks came from the same process, the
! 25829: ** second overrides the first, even though they were on different
! 25830: ** file descriptors opened on different file names.
! 25831: **
! 25832: ** This means that we cannot use POSIX locks to synchronize file access
! 25833: ** among competing threads of the same process. POSIX locks will work fine
! 25834: ** to synchronize access for threads in separate processes, but not
! 25835: ** threads within the same process.
! 25836: **
! 25837: ** To work around the problem, SQLite has to manage file locks internally
! 25838: ** on its own. Whenever a new database is opened, we have to find the
! 25839: ** specific inode of the database file (the inode is determined by the
! 25840: ** st_dev and st_ino fields of the stat structure that fstat() fills in)
! 25841: ** and check for locks already existing on that inode. When locks are
! 25842: ** created or removed, we have to look at our own internal record of the
! 25843: ** locks to see if another thread has previously set a lock on that same
! 25844: ** inode.
! 25845: **
! 25846: ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
! 25847: ** For VxWorks, we have to use the alternative unique ID system based on
! 25848: ** canonical filename and implemented in the previous division.)
! 25849: **
! 25850: ** The sqlite3_file structure for POSIX is no longer just an integer file
! 25851: ** descriptor. It is now a structure that holds the integer file
! 25852: ** descriptor and a pointer to a structure that describes the internal
! 25853: ** locks on the corresponding inode. There is one locking structure
! 25854: ** per inode, so if the same inode is opened twice, both unixFile structures
! 25855: ** point to the same locking structure. The locking structure keeps
! 25856: ** a reference count (so we will know when to delete it) and a "cnt"
! 25857: ** field that tells us its internal lock status. cnt==0 means the
! 25858: ** file is unlocked. cnt==-1 means the file has an exclusive lock.
! 25859: ** cnt>0 means there are cnt shared locks on the file.
! 25860: **
! 25861: ** Any attempt to lock or unlock a file first checks the locking
! 25862: ** structure. The fcntl() system call is only invoked to set a
! 25863: ** POSIX lock if the internal lock structure transitions between
! 25864: ** a locked and an unlocked state.
! 25865: **
! 25866: ** But wait: there are yet more problems with POSIX advisory locks.
! 25867: **
! 25868: ** If you close a file descriptor that points to a file that has locks,
! 25869: ** all locks on that file that are owned by the current process are
! 25870: ** released. To work around this problem, each unixInodeInfo object
! 25871: ** maintains a count of the number of pending locks on tha inode.
! 25872: ** When an attempt is made to close an unixFile, if there are
! 25873: ** other unixFile open on the same inode that are holding locks, the call
! 25874: ** to close() the file descriptor is deferred until all of the locks clear.
! 25875: ** The unixInodeInfo structure keeps a list of file descriptors that need to
! 25876: ** be closed and that list is walked (and cleared) when the last lock
! 25877: ** clears.
! 25878: **
! 25879: ** Yet another problem: LinuxThreads do not play well with posix locks.
! 25880: **
! 25881: ** Many older versions of linux use the LinuxThreads library which is
! 25882: ** not posix compliant. Under LinuxThreads, a lock created by thread
! 25883: ** A cannot be modified or overridden by a different thread B.
! 25884: ** Only thread A can modify the lock. Locking behavior is correct
! 25885: ** if the appliation uses the newer Native Posix Thread Library (NPTL)
! 25886: ** on linux - with NPTL a lock created by thread A can override locks
! 25887: ** in thread B. But there is no way to know at compile-time which
! 25888: ** threading library is being used. So there is no way to know at
! 25889: ** compile-time whether or not thread A can override locks on thread B.
! 25890: ** One has to do a run-time check to discover the behavior of the
! 25891: ** current process.
! 25892: **
! 25893: ** SQLite used to support LinuxThreads. But support for LinuxThreads
! 25894: ** was dropped beginning with version 3.7.0. SQLite will still work with
! 25895: ** LinuxThreads provided that (1) there is no more than one connection
! 25896: ** per database file in the same process and (2) database connections
! 25897: ** do not move across threads.
! 25898: */
! 25899:
! 25900: /*
! 25901: ** An instance of the following structure serves as the key used
! 25902: ** to locate a particular unixInodeInfo object.
! 25903: */
! 25904: struct unixFileId {
! 25905: dev_t dev; /* Device number */
! 25906: #if OS_VXWORKS
! 25907: struct vxworksFileId *pId; /* Unique file ID for vxworks. */
! 25908: #else
! 25909: ino_t ino; /* Inode number */
! 25910: #endif
! 25911: };
! 25912:
! 25913: /*
! 25914: ** An instance of the following structure is allocated for each open
! 25915: ** inode. Or, on LinuxThreads, there is one of these structures for
! 25916: ** each inode opened by each thread.
! 25917: **
! 25918: ** A single inode can have multiple file descriptors, so each unixFile
! 25919: ** structure contains a pointer to an instance of this object and this
! 25920: ** object keeps a count of the number of unixFile pointing to it.
! 25921: */
! 25922: struct unixInodeInfo {
! 25923: struct unixFileId fileId; /* The lookup key */
! 25924: int nShared; /* Number of SHARED locks held */
! 25925: unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
! 25926: unsigned char bProcessLock; /* An exclusive process lock is held */
! 25927: int nRef; /* Number of pointers to this structure */
! 25928: unixShmNode *pShmNode; /* Shared memory associated with this inode */
! 25929: int nLock; /* Number of outstanding file locks */
! 25930: UnixUnusedFd *pUnused; /* Unused file descriptors to close */
! 25931: unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
! 25932: unixInodeInfo *pPrev; /* .... doubly linked */
! 25933: #if SQLITE_ENABLE_LOCKING_STYLE
! 25934: unsigned long long sharedByte; /* for AFP simulated shared lock */
! 25935: #endif
! 25936: #if OS_VXWORKS
! 25937: sem_t *pSem; /* Named POSIX semaphore */
! 25938: char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
! 25939: #endif
! 25940: };
! 25941:
! 25942: /*
! 25943: ** A lists of all unixInodeInfo objects.
! 25944: */
! 25945: static unixInodeInfo *inodeList = 0;
! 25946:
! 25947: /*
! 25948: **
! 25949: ** This function - unixLogError_x(), is only ever called via the macro
! 25950: ** unixLogError().
! 25951: **
! 25952: ** It is invoked after an error occurs in an OS function and errno has been
! 25953: ** set. It logs a message using sqlite3_log() containing the current value of
! 25954: ** errno and, if possible, the human-readable equivalent from strerror() or
! 25955: ** strerror_r().
! 25956: **
! 25957: ** The first argument passed to the macro should be the error code that
! 25958: ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
! 25959: ** The two subsequent arguments should be the name of the OS function that
! 25960: ** failed (e.g. "unlink", "open") and the the associated file-system path,
! 25961: ** if any.
! 25962: */
! 25963: #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__)
! 25964: static int unixLogErrorAtLine(
! 25965: int errcode, /* SQLite error code */
! 25966: const char *zFunc, /* Name of OS function that failed */
! 25967: const char *zPath, /* File path associated with error */
! 25968: int iLine /* Source line number where error occurred */
! 25969: ){
! 25970: char *zErr; /* Message from strerror() or equivalent */
! 25971: int iErrno = errno; /* Saved syscall error number */
! 25972:
! 25973: /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
! 25974: ** the strerror() function to obtain the human-readable error message
! 25975: ** equivalent to errno. Otherwise, use strerror_r().
! 25976: */
! 25977: #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
! 25978: char aErr[80];
! 25979: memset(aErr, 0, sizeof(aErr));
! 25980: zErr = aErr;
! 25981:
! 25982: /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
! 25983: ** assume that the system provides the the GNU version of strerror_r() that
! 25984: ** returns a pointer to a buffer containing the error message. That pointer
! 25985: ** may point to aErr[], or it may point to some static storage somewhere.
! 25986: ** Otherwise, assume that the system provides the POSIX version of
! 25987: ** strerror_r(), which always writes an error message into aErr[].
! 25988: **
! 25989: ** If the code incorrectly assumes that it is the POSIX version that is
! 25990: ** available, the error message will often be an empty string. Not a
! 25991: ** huge problem. Incorrectly concluding that the GNU version is available
! 25992: ** could lead to a segfault though.
! 25993: */
! 25994: #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
! 25995: zErr =
! 25996: # endif
! 25997: strerror_r(iErrno, aErr, sizeof(aErr)-1);
! 25998:
! 25999: #elif SQLITE_THREADSAFE
! 26000: /* This is a threadsafe build, but strerror_r() is not available. */
! 26001: zErr = "";
! 26002: #else
! 26003: /* Non-threadsafe build, use strerror(). */
! 26004: zErr = strerror(iErrno);
! 26005: #endif
! 26006:
! 26007: assert( errcode!=SQLITE_OK );
! 26008: if( zPath==0 ) zPath = "";
! 26009: sqlite3_log(errcode,
! 26010: "os_unix.c:%d: (%d) %s(%s) - %s",
! 26011: iLine, iErrno, zFunc, zPath, zErr
! 26012: );
! 26013:
! 26014: return errcode;
! 26015: }
! 26016:
! 26017: /*
! 26018: ** Close a file descriptor.
! 26019: **
! 26020: ** We assume that close() almost always works, since it is only in a
! 26021: ** very sick application or on a very sick platform that it might fail.
! 26022: ** If it does fail, simply leak the file descriptor, but do log the
! 26023: ** error.
! 26024: **
! 26025: ** Note that it is not safe to retry close() after EINTR since the
! 26026: ** file descriptor might have already been reused by another thread.
! 26027: ** So we don't even try to recover from an EINTR. Just log the error
! 26028: ** and move on.
! 26029: */
! 26030: static void robust_close(unixFile *pFile, int h, int lineno){
! 26031: if( osClose(h) ){
! 26032: unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
! 26033: pFile ? pFile->zPath : 0, lineno);
! 26034: }
! 26035: }
! 26036:
! 26037: /*
! 26038: ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
! 26039: */
! 26040: static void closePendingFds(unixFile *pFile){
! 26041: unixInodeInfo *pInode = pFile->pInode;
! 26042: UnixUnusedFd *p;
! 26043: UnixUnusedFd *pNext;
! 26044: for(p=pInode->pUnused; p; p=pNext){
! 26045: pNext = p->pNext;
! 26046: robust_close(pFile, p->fd, __LINE__);
! 26047: sqlite3_free(p);
! 26048: }
! 26049: pInode->pUnused = 0;
! 26050: }
! 26051:
! 26052: /*
! 26053: ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
! 26054: **
! 26055: ** The mutex entered using the unixEnterMutex() function must be held
! 26056: ** when this function is called.
! 26057: */
! 26058: static void releaseInodeInfo(unixFile *pFile){
! 26059: unixInodeInfo *pInode = pFile->pInode;
! 26060: assert( unixMutexHeld() );
! 26061: if( ALWAYS(pInode) ){
! 26062: pInode->nRef--;
! 26063: if( pInode->nRef==0 ){
! 26064: assert( pInode->pShmNode==0 );
! 26065: closePendingFds(pFile);
! 26066: if( pInode->pPrev ){
! 26067: assert( pInode->pPrev->pNext==pInode );
! 26068: pInode->pPrev->pNext = pInode->pNext;
! 26069: }else{
! 26070: assert( inodeList==pInode );
! 26071: inodeList = pInode->pNext;
! 26072: }
! 26073: if( pInode->pNext ){
! 26074: assert( pInode->pNext->pPrev==pInode );
! 26075: pInode->pNext->pPrev = pInode->pPrev;
! 26076: }
! 26077: sqlite3_free(pInode);
! 26078: }
! 26079: }
! 26080: }
! 26081:
! 26082: /*
! 26083: ** Given a file descriptor, locate the unixInodeInfo object that
! 26084: ** describes that file descriptor. Create a new one if necessary. The
! 26085: ** return value might be uninitialized if an error occurs.
! 26086: **
! 26087: ** The mutex entered using the unixEnterMutex() function must be held
! 26088: ** when this function is called.
! 26089: **
! 26090: ** Return an appropriate error code.
! 26091: */
! 26092: static int findInodeInfo(
! 26093: unixFile *pFile, /* Unix file with file desc used in the key */
! 26094: unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
! 26095: ){
! 26096: int rc; /* System call return code */
! 26097: int fd; /* The file descriptor for pFile */
! 26098: struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
! 26099: struct stat statbuf; /* Low-level file information */
! 26100: unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
! 26101:
! 26102: assert( unixMutexHeld() );
! 26103:
! 26104: /* Get low-level information about the file that we can used to
! 26105: ** create a unique name for the file.
! 26106: */
! 26107: fd = pFile->h;
! 26108: rc = osFstat(fd, &statbuf);
! 26109: if( rc!=0 ){
! 26110: pFile->lastErrno = errno;
! 26111: #ifdef EOVERFLOW
! 26112: if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
! 26113: #endif
! 26114: return SQLITE_IOERR;
! 26115: }
! 26116:
! 26117: #ifdef __APPLE__
! 26118: /* On OS X on an msdos filesystem, the inode number is reported
! 26119: ** incorrectly for zero-size files. See ticket #3260. To work
! 26120: ** around this problem (we consider it a bug in OS X, not SQLite)
! 26121: ** we always increase the file size to 1 by writing a single byte
! 26122: ** prior to accessing the inode number. The one byte written is
! 26123: ** an ASCII 'S' character which also happens to be the first byte
! 26124: ** in the header of every SQLite database. In this way, if there
! 26125: ** is a race condition such that another thread has already populated
! 26126: ** the first page of the database, no damage is done.
! 26127: */
! 26128: if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
! 26129: do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
! 26130: if( rc!=1 ){
! 26131: pFile->lastErrno = errno;
! 26132: return SQLITE_IOERR;
! 26133: }
! 26134: rc = osFstat(fd, &statbuf);
! 26135: if( rc!=0 ){
! 26136: pFile->lastErrno = errno;
! 26137: return SQLITE_IOERR;
! 26138: }
! 26139: }
! 26140: #endif
! 26141:
! 26142: memset(&fileId, 0, sizeof(fileId));
! 26143: fileId.dev = statbuf.st_dev;
! 26144: #if OS_VXWORKS
! 26145: fileId.pId = pFile->pId;
! 26146: #else
! 26147: fileId.ino = statbuf.st_ino;
! 26148: #endif
! 26149: pInode = inodeList;
! 26150: while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
! 26151: pInode = pInode->pNext;
! 26152: }
! 26153: if( pInode==0 ){
! 26154: pInode = sqlite3_malloc( sizeof(*pInode) );
! 26155: if( pInode==0 ){
! 26156: return SQLITE_NOMEM;
! 26157: }
! 26158: memset(pInode, 0, sizeof(*pInode));
! 26159: memcpy(&pInode->fileId, &fileId, sizeof(fileId));
! 26160: pInode->nRef = 1;
! 26161: pInode->pNext = inodeList;
! 26162: pInode->pPrev = 0;
! 26163: if( inodeList ) inodeList->pPrev = pInode;
! 26164: inodeList = pInode;
! 26165: }else{
! 26166: pInode->nRef++;
! 26167: }
! 26168: *ppInode = pInode;
! 26169: return SQLITE_OK;
! 26170: }
! 26171:
! 26172:
! 26173: /*
! 26174: ** This routine checks if there is a RESERVED lock held on the specified
! 26175: ** file by this or any other process. If such a lock is held, set *pResOut
! 26176: ** to a non-zero value otherwise *pResOut is set to zero. The return value
! 26177: ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
! 26178: */
! 26179: static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
! 26180: int rc = SQLITE_OK;
! 26181: int reserved = 0;
! 26182: unixFile *pFile = (unixFile*)id;
! 26183:
! 26184: SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
! 26185:
! 26186: assert( pFile );
! 26187: unixEnterMutex(); /* Because pFile->pInode is shared across threads */
! 26188:
! 26189: /* Check if a thread in this process holds such a lock */
! 26190: if( pFile->pInode->eFileLock>SHARED_LOCK ){
! 26191: reserved = 1;
! 26192: }
! 26193:
! 26194: /* Otherwise see if some other process holds it.
! 26195: */
! 26196: #ifndef __DJGPP__
! 26197: if( !reserved && !pFile->pInode->bProcessLock ){
! 26198: struct flock lock;
! 26199: lock.l_whence = SEEK_SET;
! 26200: lock.l_start = RESERVED_BYTE;
! 26201: lock.l_len = 1;
! 26202: lock.l_type = F_WRLCK;
! 26203: if( osFcntl(pFile->h, F_GETLK, &lock) ){
! 26204: rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
! 26205: pFile->lastErrno = errno;
! 26206: } else if( lock.l_type!=F_UNLCK ){
! 26207: reserved = 1;
! 26208: }
! 26209: }
! 26210: #endif
! 26211:
! 26212: unixLeaveMutex();
! 26213: OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
! 26214:
! 26215: *pResOut = reserved;
! 26216: return rc;
! 26217: }
! 26218:
! 26219: /*
! 26220: ** Attempt to set a system-lock on the file pFile. The lock is
! 26221: ** described by pLock.
! 26222: **
! 26223: ** If the pFile was opened read/write from unix-excl, then the only lock
! 26224: ** ever obtained is an exclusive lock, and it is obtained exactly once
! 26225: ** the first time any lock is attempted. All subsequent system locking
! 26226: ** operations become no-ops. Locking operations still happen internally,
! 26227: ** in order to coordinate access between separate database connections
! 26228: ** within this process, but all of that is handled in memory and the
! 26229: ** operating system does not participate.
! 26230: **
! 26231: ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
! 26232: ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
! 26233: ** and is read-only.
! 26234: **
! 26235: ** Zero is returned if the call completes successfully, or -1 if a call
! 26236: ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
! 26237: */
! 26238: static int unixFileLock(unixFile *pFile, struct flock *pLock){
! 26239: int rc;
! 26240: unixInodeInfo *pInode = pFile->pInode;
! 26241: assert( unixMutexHeld() );
! 26242: assert( pInode!=0 );
! 26243: if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
! 26244: && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
! 26245: ){
! 26246: if( pInode->bProcessLock==0 ){
! 26247: struct flock lock;
! 26248: assert( pInode->nLock==0 );
! 26249: lock.l_whence = SEEK_SET;
! 26250: lock.l_start = SHARED_FIRST;
! 26251: lock.l_len = SHARED_SIZE;
! 26252: lock.l_type = F_WRLCK;
! 26253: rc = osFcntl(pFile->h, F_SETLK, &lock);
! 26254: if( rc<0 ) return rc;
! 26255: pInode->bProcessLock = 1;
! 26256: pInode->nLock++;
! 26257: }else{
! 26258: rc = 0;
! 26259: }
! 26260: }else{
! 26261: rc = osFcntl(pFile->h, F_SETLK, pLock);
! 26262: }
! 26263: return rc;
! 26264: }
! 26265:
! 26266: /*
! 26267: ** Lock the file with the lock specified by parameter eFileLock - one
! 26268: ** of the following:
! 26269: **
! 26270: ** (1) SHARED_LOCK
! 26271: ** (2) RESERVED_LOCK
! 26272: ** (3) PENDING_LOCK
! 26273: ** (4) EXCLUSIVE_LOCK
! 26274: **
! 26275: ** Sometimes when requesting one lock state, additional lock states
! 26276: ** are inserted in between. The locking might fail on one of the later
! 26277: ** transitions leaving the lock state different from what it started but
! 26278: ** still short of its goal. The following chart shows the allowed
! 26279: ** transitions and the inserted intermediate states:
! 26280: **
! 26281: ** UNLOCKED -> SHARED
! 26282: ** SHARED -> RESERVED
! 26283: ** SHARED -> (PENDING) -> EXCLUSIVE
! 26284: ** RESERVED -> (PENDING) -> EXCLUSIVE
! 26285: ** PENDING -> EXCLUSIVE
! 26286: **
! 26287: ** This routine will only increase a lock. Use the sqlite3OsUnlock()
! 26288: ** routine to lower a locking level.
! 26289: */
! 26290: static int unixLock(sqlite3_file *id, int eFileLock){
! 26291: /* The following describes the implementation of the various locks and
! 26292: ** lock transitions in terms of the POSIX advisory shared and exclusive
! 26293: ** lock primitives (called read-locks and write-locks below, to avoid
! 26294: ** confusion with SQLite lock names). The algorithms are complicated
! 26295: ** slightly in order to be compatible with windows systems simultaneously
! 26296: ** accessing the same database file, in case that is ever required.
! 26297: **
! 26298: ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
! 26299: ** byte', each single bytes at well known offsets, and the 'shared byte
! 26300: ** range', a range of 510 bytes at a well known offset.
! 26301: **
! 26302: ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
! 26303: ** byte'. If this is successful, a random byte from the 'shared byte
! 26304: ** range' is read-locked and the lock on the 'pending byte' released.
! 26305: **
! 26306: ** A process may only obtain a RESERVED lock after it has a SHARED lock.
! 26307: ** A RESERVED lock is implemented by grabbing a write-lock on the
! 26308: ** 'reserved byte'.
! 26309: **
! 26310: ** A process may only obtain a PENDING lock after it has obtained a
! 26311: ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
! 26312: ** on the 'pending byte'. This ensures that no new SHARED locks can be
! 26313: ** obtained, but existing SHARED locks are allowed to persist. A process
! 26314: ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
! 26315: ** This property is used by the algorithm for rolling back a journal file
! 26316: ** after a crash.
! 26317: **
! 26318: ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
! 26319: ** implemented by obtaining a write-lock on the entire 'shared byte
! 26320: ** range'. Since all other locks require a read-lock on one of the bytes
! 26321: ** within this range, this ensures that no other locks are held on the
! 26322: ** database.
! 26323: **
! 26324: ** The reason a single byte cannot be used instead of the 'shared byte
! 26325: ** range' is that some versions of windows do not support read-locks. By
! 26326: ** locking a random byte from a range, concurrent SHARED locks may exist
! 26327: ** even if the locking primitive used is always a write-lock.
! 26328: */
! 26329: int rc = SQLITE_OK;
! 26330: unixFile *pFile = (unixFile*)id;
! 26331: unixInodeInfo *pInode;
! 26332: struct flock lock;
! 26333: int tErrno = 0;
! 26334:
! 26335: assert( pFile );
! 26336: OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
! 26337: azFileLock(eFileLock), azFileLock(pFile->eFileLock),
! 26338: azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid()));
! 26339:
! 26340: /* If there is already a lock of this type or more restrictive on the
! 26341: ** unixFile, do nothing. Don't use the end_lock: exit path, as
! 26342: ** unixEnterMutex() hasn't been called yet.
! 26343: */
! 26344: if( pFile->eFileLock>=eFileLock ){
! 26345: OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
! 26346: azFileLock(eFileLock)));
! 26347: return SQLITE_OK;
! 26348: }
! 26349:
! 26350: /* Make sure the locking sequence is correct.
! 26351: ** (1) We never move from unlocked to anything higher than shared lock.
! 26352: ** (2) SQLite never explicitly requests a pendig lock.
! 26353: ** (3) A shared lock is always held when a reserve lock is requested.
! 26354: */
! 26355: assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
! 26356: assert( eFileLock!=PENDING_LOCK );
! 26357: assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
! 26358:
! 26359: /* This mutex is needed because pFile->pInode is shared across threads
! 26360: */
! 26361: unixEnterMutex();
! 26362: pInode = pFile->pInode;
! 26363:
! 26364: /* If some thread using this PID has a lock via a different unixFile*
! 26365: ** handle that precludes the requested lock, return BUSY.
! 26366: */
! 26367: if( (pFile->eFileLock!=pInode->eFileLock &&
! 26368: (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
! 26369: ){
! 26370: rc = SQLITE_BUSY;
! 26371: goto end_lock;
! 26372: }
! 26373:
! 26374: /* If a SHARED lock is requested, and some thread using this PID already
! 26375: ** has a SHARED or RESERVED lock, then increment reference counts and
! 26376: ** return SQLITE_OK.
! 26377: */
! 26378: if( eFileLock==SHARED_LOCK &&
! 26379: (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
! 26380: assert( eFileLock==SHARED_LOCK );
! 26381: assert( pFile->eFileLock==0 );
! 26382: assert( pInode->nShared>0 );
! 26383: pFile->eFileLock = SHARED_LOCK;
! 26384: pInode->nShared++;
! 26385: pInode->nLock++;
! 26386: goto end_lock;
! 26387: }
! 26388:
! 26389:
! 26390: /* A PENDING lock is needed before acquiring a SHARED lock and before
! 26391: ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
! 26392: ** be released.
! 26393: */
! 26394: lock.l_len = 1L;
! 26395: lock.l_whence = SEEK_SET;
! 26396: if( eFileLock==SHARED_LOCK
! 26397: || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
! 26398: ){
! 26399: lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
! 26400: lock.l_start = PENDING_BYTE;
! 26401: if( unixFileLock(pFile, &lock) ){
! 26402: tErrno = errno;
! 26403: rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
! 26404: if( rc!=SQLITE_BUSY ){
! 26405: pFile->lastErrno = tErrno;
! 26406: }
! 26407: goto end_lock;
! 26408: }
! 26409: }
! 26410:
! 26411:
! 26412: /* If control gets to this point, then actually go ahead and make
! 26413: ** operating system calls for the specified lock.
! 26414: */
! 26415: if( eFileLock==SHARED_LOCK ){
! 26416: assert( pInode->nShared==0 );
! 26417: assert( pInode->eFileLock==0 );
! 26418: assert( rc==SQLITE_OK );
! 26419:
! 26420: /* Now get the read-lock */
! 26421: lock.l_start = SHARED_FIRST;
! 26422: lock.l_len = SHARED_SIZE;
! 26423: if( unixFileLock(pFile, &lock) ){
! 26424: tErrno = errno;
! 26425: rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
! 26426: }
! 26427:
! 26428: /* Drop the temporary PENDING lock */
! 26429: lock.l_start = PENDING_BYTE;
! 26430: lock.l_len = 1L;
! 26431: lock.l_type = F_UNLCK;
! 26432: if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
! 26433: /* This could happen with a network mount */
! 26434: tErrno = errno;
! 26435: rc = SQLITE_IOERR_UNLOCK;
! 26436: }
! 26437:
! 26438: if( rc ){
! 26439: if( rc!=SQLITE_BUSY ){
! 26440: pFile->lastErrno = tErrno;
! 26441: }
! 26442: goto end_lock;
! 26443: }else{
! 26444: pFile->eFileLock = SHARED_LOCK;
! 26445: pInode->nLock++;
! 26446: pInode->nShared = 1;
! 26447: }
! 26448: }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
! 26449: /* We are trying for an exclusive lock but another thread in this
! 26450: ** same process is still holding a shared lock. */
! 26451: rc = SQLITE_BUSY;
! 26452: }else{
! 26453: /* The request was for a RESERVED or EXCLUSIVE lock. It is
! 26454: ** assumed that there is a SHARED or greater lock on the file
! 26455: ** already.
! 26456: */
! 26457: assert( 0!=pFile->eFileLock );
! 26458: lock.l_type = F_WRLCK;
! 26459:
! 26460: assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
! 26461: if( eFileLock==RESERVED_LOCK ){
! 26462: lock.l_start = RESERVED_BYTE;
! 26463: lock.l_len = 1L;
! 26464: }else{
! 26465: lock.l_start = SHARED_FIRST;
! 26466: lock.l_len = SHARED_SIZE;
! 26467: }
! 26468:
! 26469: if( unixFileLock(pFile, &lock) ){
! 26470: tErrno = errno;
! 26471: rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
! 26472: if( rc!=SQLITE_BUSY ){
! 26473: pFile->lastErrno = tErrno;
! 26474: }
! 26475: }
! 26476: }
! 26477:
! 26478:
! 26479: #ifndef NDEBUG
! 26480: /* Set up the transaction-counter change checking flags when
! 26481: ** transitioning from a SHARED to a RESERVED lock. The change
! 26482: ** from SHARED to RESERVED marks the beginning of a normal
! 26483: ** write operation (not a hot journal rollback).
! 26484: */
! 26485: if( rc==SQLITE_OK
! 26486: && pFile->eFileLock<=SHARED_LOCK
! 26487: && eFileLock==RESERVED_LOCK
! 26488: ){
! 26489: pFile->transCntrChng = 0;
! 26490: pFile->dbUpdate = 0;
! 26491: pFile->inNormalWrite = 1;
! 26492: }
! 26493: #endif
! 26494:
! 26495:
! 26496: if( rc==SQLITE_OK ){
! 26497: pFile->eFileLock = eFileLock;
! 26498: pInode->eFileLock = eFileLock;
! 26499: }else if( eFileLock==EXCLUSIVE_LOCK ){
! 26500: pFile->eFileLock = PENDING_LOCK;
! 26501: pInode->eFileLock = PENDING_LOCK;
! 26502: }
! 26503:
! 26504: end_lock:
! 26505: unixLeaveMutex();
! 26506: OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
! 26507: rc==SQLITE_OK ? "ok" : "failed"));
! 26508: return rc;
! 26509: }
! 26510:
! 26511: /*
! 26512: ** Add the file descriptor used by file handle pFile to the corresponding
! 26513: ** pUnused list.
! 26514: */
! 26515: static void setPendingFd(unixFile *pFile){
! 26516: unixInodeInfo *pInode = pFile->pInode;
! 26517: UnixUnusedFd *p = pFile->pUnused;
! 26518: p->pNext = pInode->pUnused;
! 26519: pInode->pUnused = p;
! 26520: pFile->h = -1;
! 26521: pFile->pUnused = 0;
! 26522: }
! 26523:
! 26524: /*
! 26525: ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
! 26526: ** must be either NO_LOCK or SHARED_LOCK.
! 26527: **
! 26528: ** If the locking level of the file descriptor is already at or below
! 26529: ** the requested locking level, this routine is a no-op.
! 26530: **
! 26531: ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
! 26532: ** the byte range is divided into 2 parts and the first part is unlocked then
! 26533: ** set to a read lock, then the other part is simply unlocked. This works
! 26534: ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
! 26535: ** remove the write lock on a region when a read lock is set.
! 26536: */
! 26537: static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
! 26538: unixFile *pFile = (unixFile*)id;
! 26539: unixInodeInfo *pInode;
! 26540: struct flock lock;
! 26541: int rc = SQLITE_OK;
! 26542:
! 26543: assert( pFile );
! 26544: OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
! 26545: pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
! 26546: getpid()));
! 26547:
! 26548: assert( eFileLock<=SHARED_LOCK );
! 26549: if( pFile->eFileLock<=eFileLock ){
! 26550: return SQLITE_OK;
! 26551: }
! 26552: unixEnterMutex();
! 26553: pInode = pFile->pInode;
! 26554: assert( pInode->nShared!=0 );
! 26555: if( pFile->eFileLock>SHARED_LOCK ){
! 26556: assert( pInode->eFileLock==pFile->eFileLock );
! 26557:
! 26558: #ifndef NDEBUG
! 26559: /* When reducing a lock such that other processes can start
! 26560: ** reading the database file again, make sure that the
! 26561: ** transaction counter was updated if any part of the database
! 26562: ** file changed. If the transaction counter is not updated,
! 26563: ** other connections to the same file might not realize that
! 26564: ** the file has changed and hence might not know to flush their
! 26565: ** cache. The use of a stale cache can lead to database corruption.
! 26566: */
! 26567: pFile->inNormalWrite = 0;
! 26568: #endif
! 26569:
! 26570: /* downgrading to a shared lock on NFS involves clearing the write lock
! 26571: ** before establishing the readlock - to avoid a race condition we downgrade
! 26572: ** the lock in 2 blocks, so that part of the range will be covered by a
! 26573: ** write lock until the rest is covered by a read lock:
! 26574: ** 1: [WWWWW]
! 26575: ** 2: [....W]
! 26576: ** 3: [RRRRW]
! 26577: ** 4: [RRRR.]
! 26578: */
! 26579: if( eFileLock==SHARED_LOCK ){
! 26580:
! 26581: #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
! 26582: (void)handleNFSUnlock;
! 26583: assert( handleNFSUnlock==0 );
! 26584: #endif
! 26585: #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
! 26586: if( handleNFSUnlock ){
! 26587: int tErrno; /* Error code from system call errors */
! 26588: off_t divSize = SHARED_SIZE - 1;
! 26589:
! 26590: lock.l_type = F_UNLCK;
! 26591: lock.l_whence = SEEK_SET;
! 26592: lock.l_start = SHARED_FIRST;
! 26593: lock.l_len = divSize;
! 26594: if( unixFileLock(pFile, &lock)==(-1) ){
! 26595: tErrno = errno;
! 26596: rc = SQLITE_IOERR_UNLOCK;
! 26597: if( IS_LOCK_ERROR(rc) ){
! 26598: pFile->lastErrno = tErrno;
! 26599: }
! 26600: goto end_unlock;
! 26601: }
! 26602: lock.l_type = F_RDLCK;
! 26603: lock.l_whence = SEEK_SET;
! 26604: lock.l_start = SHARED_FIRST;
! 26605: lock.l_len = divSize;
! 26606: if( unixFileLock(pFile, &lock)==(-1) ){
! 26607: tErrno = errno;
! 26608: rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
! 26609: if( IS_LOCK_ERROR(rc) ){
! 26610: pFile->lastErrno = tErrno;
! 26611: }
! 26612: goto end_unlock;
! 26613: }
! 26614: lock.l_type = F_UNLCK;
! 26615: lock.l_whence = SEEK_SET;
! 26616: lock.l_start = SHARED_FIRST+divSize;
! 26617: lock.l_len = SHARED_SIZE-divSize;
! 26618: if( unixFileLock(pFile, &lock)==(-1) ){
! 26619: tErrno = errno;
! 26620: rc = SQLITE_IOERR_UNLOCK;
! 26621: if( IS_LOCK_ERROR(rc) ){
! 26622: pFile->lastErrno = tErrno;
! 26623: }
! 26624: goto end_unlock;
! 26625: }
! 26626: }else
! 26627: #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
! 26628: {
! 26629: lock.l_type = F_RDLCK;
! 26630: lock.l_whence = SEEK_SET;
! 26631: lock.l_start = SHARED_FIRST;
! 26632: lock.l_len = SHARED_SIZE;
! 26633: if( unixFileLock(pFile, &lock) ){
! 26634: /* In theory, the call to unixFileLock() cannot fail because another
! 26635: ** process is holding an incompatible lock. If it does, this
! 26636: ** indicates that the other process is not following the locking
! 26637: ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
! 26638: ** SQLITE_BUSY would confuse the upper layer (in practice it causes
! 26639: ** an assert to fail). */
! 26640: rc = SQLITE_IOERR_RDLOCK;
! 26641: pFile->lastErrno = errno;
! 26642: goto end_unlock;
! 26643: }
! 26644: }
! 26645: }
! 26646: lock.l_type = F_UNLCK;
! 26647: lock.l_whence = SEEK_SET;
! 26648: lock.l_start = PENDING_BYTE;
! 26649: lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
! 26650: if( unixFileLock(pFile, &lock)==0 ){
! 26651: pInode->eFileLock = SHARED_LOCK;
! 26652: }else{
! 26653: rc = SQLITE_IOERR_UNLOCK;
! 26654: pFile->lastErrno = errno;
! 26655: goto end_unlock;
! 26656: }
! 26657: }
! 26658: if( eFileLock==NO_LOCK ){
! 26659: /* Decrement the shared lock counter. Release the lock using an
! 26660: ** OS call only when all threads in this same process have released
! 26661: ** the lock.
! 26662: */
! 26663: pInode->nShared--;
! 26664: if( pInode->nShared==0 ){
! 26665: lock.l_type = F_UNLCK;
! 26666: lock.l_whence = SEEK_SET;
! 26667: lock.l_start = lock.l_len = 0L;
! 26668: if( unixFileLock(pFile, &lock)==0 ){
! 26669: pInode->eFileLock = NO_LOCK;
! 26670: }else{
! 26671: rc = SQLITE_IOERR_UNLOCK;
! 26672: pFile->lastErrno = errno;
! 26673: pInode->eFileLock = NO_LOCK;
! 26674: pFile->eFileLock = NO_LOCK;
! 26675: }
! 26676: }
! 26677:
! 26678: /* Decrement the count of locks against this same file. When the
! 26679: ** count reaches zero, close any other file descriptors whose close
! 26680: ** was deferred because of outstanding locks.
! 26681: */
! 26682: pInode->nLock--;
! 26683: assert( pInode->nLock>=0 );
! 26684: if( pInode->nLock==0 ){
! 26685: closePendingFds(pFile);
! 26686: }
! 26687: }
! 26688:
! 26689: end_unlock:
! 26690: unixLeaveMutex();
! 26691: if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
! 26692: return rc;
! 26693: }
! 26694:
! 26695: /*
! 26696: ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
! 26697: ** must be either NO_LOCK or SHARED_LOCK.
! 26698: **
! 26699: ** If the locking level of the file descriptor is already at or below
! 26700: ** the requested locking level, this routine is a no-op.
! 26701: */
! 26702: static int unixUnlock(sqlite3_file *id, int eFileLock){
! 26703: return posixUnlock(id, eFileLock, 0);
! 26704: }
! 26705:
! 26706: /*
! 26707: ** This function performs the parts of the "close file" operation
! 26708: ** common to all locking schemes. It closes the directory and file
! 26709: ** handles, if they are valid, and sets all fields of the unixFile
! 26710: ** structure to 0.
! 26711: **
! 26712: ** It is *not* necessary to hold the mutex when this routine is called,
! 26713: ** even on VxWorks. A mutex will be acquired on VxWorks by the
! 26714: ** vxworksReleaseFileId() routine.
! 26715: */
! 26716: static int closeUnixFile(sqlite3_file *id){
! 26717: unixFile *pFile = (unixFile*)id;
! 26718: if( pFile->h>=0 ){
! 26719: robust_close(pFile, pFile->h, __LINE__);
! 26720: pFile->h = -1;
! 26721: }
! 26722: #if OS_VXWORKS
! 26723: if( pFile->pId ){
! 26724: if( pFile->ctrlFlags & UNIXFILE_DELETE ){
! 26725: osUnlink(pFile->pId->zCanonicalName);
! 26726: }
! 26727: vxworksReleaseFileId(pFile->pId);
! 26728: pFile->pId = 0;
! 26729: }
! 26730: #endif
! 26731: OSTRACE(("CLOSE %-3d\n", pFile->h));
! 26732: OpenCounter(-1);
! 26733: sqlite3_free(pFile->pUnused);
! 26734: memset(pFile, 0, sizeof(unixFile));
! 26735: return SQLITE_OK;
! 26736: }
! 26737:
! 26738: /*
! 26739: ** Close a file.
! 26740: */
! 26741: static int unixClose(sqlite3_file *id){
! 26742: int rc = SQLITE_OK;
! 26743: unixFile *pFile = (unixFile *)id;
! 26744: unixUnlock(id, NO_LOCK);
! 26745: unixEnterMutex();
! 26746:
! 26747: /* unixFile.pInode is always valid here. Otherwise, a different close
! 26748: ** routine (e.g. nolockClose()) would be called instead.
! 26749: */
! 26750: assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
! 26751: if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
! 26752: /* If there are outstanding locks, do not actually close the file just
! 26753: ** yet because that would clear those locks. Instead, add the file
! 26754: ** descriptor to pInode->pUnused list. It will be automatically closed
! 26755: ** when the last lock is cleared.
! 26756: */
! 26757: setPendingFd(pFile);
! 26758: }
! 26759: releaseInodeInfo(pFile);
! 26760: rc = closeUnixFile(id);
! 26761: unixLeaveMutex();
! 26762: return rc;
! 26763: }
! 26764:
! 26765: /************** End of the posix advisory lock implementation *****************
! 26766: ******************************************************************************/
! 26767:
! 26768: /******************************************************************************
! 26769: ****************************** No-op Locking **********************************
! 26770: **
! 26771: ** Of the various locking implementations available, this is by far the
! 26772: ** simplest: locking is ignored. No attempt is made to lock the database
! 26773: ** file for reading or writing.
! 26774: **
! 26775: ** This locking mode is appropriate for use on read-only databases
! 26776: ** (ex: databases that are burned into CD-ROM, for example.) It can
! 26777: ** also be used if the application employs some external mechanism to
! 26778: ** prevent simultaneous access of the same database by two or more
! 26779: ** database connections. But there is a serious risk of database
! 26780: ** corruption if this locking mode is used in situations where multiple
! 26781: ** database connections are accessing the same database file at the same
! 26782: ** time and one or more of those connections are writing.
! 26783: */
! 26784:
! 26785: static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
! 26786: UNUSED_PARAMETER(NotUsed);
! 26787: *pResOut = 0;
! 26788: return SQLITE_OK;
! 26789: }
! 26790: static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
! 26791: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 26792: return SQLITE_OK;
! 26793: }
! 26794: static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
! 26795: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 26796: return SQLITE_OK;
! 26797: }
! 26798:
! 26799: /*
! 26800: ** Close the file.
! 26801: */
! 26802: static int nolockClose(sqlite3_file *id) {
! 26803: return closeUnixFile(id);
! 26804: }
! 26805:
! 26806: /******************* End of the no-op lock implementation *********************
! 26807: ******************************************************************************/
! 26808:
! 26809: /******************************************************************************
! 26810: ************************* Begin dot-file Locking ******************************
! 26811: **
! 26812: ** The dotfile locking implementation uses the existance of separate lock
! 26813: ** files (really a directory) to control access to the database. This works
! 26814: ** on just about every filesystem imaginable. But there are serious downsides:
! 26815: **
! 26816: ** (1) There is zero concurrency. A single reader blocks all other
! 26817: ** connections from reading or writing the database.
! 26818: **
! 26819: ** (2) An application crash or power loss can leave stale lock files
! 26820: ** sitting around that need to be cleared manually.
! 26821: **
! 26822: ** Nevertheless, a dotlock is an appropriate locking mode for use if no
! 26823: ** other locking strategy is available.
! 26824: **
! 26825: ** Dotfile locking works by creating a subdirectory in the same directory as
! 26826: ** the database and with the same name but with a ".lock" extension added.
! 26827: ** The existance of a lock directory implies an EXCLUSIVE lock. All other
! 26828: ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
! 26829: */
! 26830:
! 26831: /*
! 26832: ** The file suffix added to the data base filename in order to create the
! 26833: ** lock directory.
! 26834: */
! 26835: #define DOTLOCK_SUFFIX ".lock"
! 26836:
! 26837: /*
! 26838: ** This routine checks if there is a RESERVED lock held on the specified
! 26839: ** file by this or any other process. If such a lock is held, set *pResOut
! 26840: ** to a non-zero value otherwise *pResOut is set to zero. The return value
! 26841: ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
! 26842: **
! 26843: ** In dotfile locking, either a lock exists or it does not. So in this
! 26844: ** variation of CheckReservedLock(), *pResOut is set to true if any lock
! 26845: ** is held on the file and false if the file is unlocked.
! 26846: */
! 26847: static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
! 26848: int rc = SQLITE_OK;
! 26849: int reserved = 0;
! 26850: unixFile *pFile = (unixFile*)id;
! 26851:
! 26852: SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
! 26853:
! 26854: assert( pFile );
! 26855:
! 26856: /* Check if a thread in this process holds such a lock */
! 26857: if( pFile->eFileLock>SHARED_LOCK ){
! 26858: /* Either this connection or some other connection in the same process
! 26859: ** holds a lock on the file. No need to check further. */
! 26860: reserved = 1;
! 26861: }else{
! 26862: /* The lock is held if and only if the lockfile exists */
! 26863: const char *zLockFile = (const char*)pFile->lockingContext;
! 26864: reserved = osAccess(zLockFile, 0)==0;
! 26865: }
! 26866: OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
! 26867: *pResOut = reserved;
! 26868: return rc;
! 26869: }
! 26870:
! 26871: /*
! 26872: ** Lock the file with the lock specified by parameter eFileLock - one
! 26873: ** of the following:
! 26874: **
! 26875: ** (1) SHARED_LOCK
! 26876: ** (2) RESERVED_LOCK
! 26877: ** (3) PENDING_LOCK
! 26878: ** (4) EXCLUSIVE_LOCK
! 26879: **
! 26880: ** Sometimes when requesting one lock state, additional lock states
! 26881: ** are inserted in between. The locking might fail on one of the later
! 26882: ** transitions leaving the lock state different from what it started but
! 26883: ** still short of its goal. The following chart shows the allowed
! 26884: ** transitions and the inserted intermediate states:
! 26885: **
! 26886: ** UNLOCKED -> SHARED
! 26887: ** SHARED -> RESERVED
! 26888: ** SHARED -> (PENDING) -> EXCLUSIVE
! 26889: ** RESERVED -> (PENDING) -> EXCLUSIVE
! 26890: ** PENDING -> EXCLUSIVE
! 26891: **
! 26892: ** This routine will only increase a lock. Use the sqlite3OsUnlock()
! 26893: ** routine to lower a locking level.
! 26894: **
! 26895: ** With dotfile locking, we really only support state (4): EXCLUSIVE.
! 26896: ** But we track the other locking levels internally.
! 26897: */
! 26898: static int dotlockLock(sqlite3_file *id, int eFileLock) {
! 26899: unixFile *pFile = (unixFile*)id;
! 26900: char *zLockFile = (char *)pFile->lockingContext;
! 26901: int rc = SQLITE_OK;
! 26902:
! 26903:
! 26904: /* If we have any lock, then the lock file already exists. All we have
! 26905: ** to do is adjust our internal record of the lock level.
! 26906: */
! 26907: if( pFile->eFileLock > NO_LOCK ){
! 26908: pFile->eFileLock = eFileLock;
! 26909: /* Always update the timestamp on the old file */
! 26910: #ifdef HAVE_UTIME
! 26911: utime(zLockFile, NULL);
! 26912: #else
! 26913: utimes(zLockFile, NULL);
! 26914: #endif
! 26915: return SQLITE_OK;
! 26916: }
! 26917:
! 26918: /* grab an exclusive lock */
! 26919: rc = osMkdir(zLockFile, 0777);
! 26920: if( rc<0 ){
! 26921: /* failed to open/create the lock directory */
! 26922: int tErrno = errno;
! 26923: if( EEXIST == tErrno ){
! 26924: rc = SQLITE_BUSY;
! 26925: } else {
! 26926: rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
! 26927: if( IS_LOCK_ERROR(rc) ){
! 26928: pFile->lastErrno = tErrno;
! 26929: }
! 26930: }
! 26931: return rc;
! 26932: }
! 26933:
! 26934: /* got it, set the type and return ok */
! 26935: pFile->eFileLock = eFileLock;
! 26936: return rc;
! 26937: }
! 26938:
! 26939: /*
! 26940: ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
! 26941: ** must be either NO_LOCK or SHARED_LOCK.
! 26942: **
! 26943: ** If the locking level of the file descriptor is already at or below
! 26944: ** the requested locking level, this routine is a no-op.
! 26945: **
! 26946: ** When the locking level reaches NO_LOCK, delete the lock file.
! 26947: */
! 26948: static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
! 26949: unixFile *pFile = (unixFile*)id;
! 26950: char *zLockFile = (char *)pFile->lockingContext;
! 26951: int rc;
! 26952:
! 26953: assert( pFile );
! 26954: OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
! 26955: pFile->eFileLock, getpid()));
! 26956: assert( eFileLock<=SHARED_LOCK );
! 26957:
! 26958: /* no-op if possible */
! 26959: if( pFile->eFileLock==eFileLock ){
! 26960: return SQLITE_OK;
! 26961: }
! 26962:
! 26963: /* To downgrade to shared, simply update our internal notion of the
! 26964: ** lock state. No need to mess with the file on disk.
! 26965: */
! 26966: if( eFileLock==SHARED_LOCK ){
! 26967: pFile->eFileLock = SHARED_LOCK;
! 26968: return SQLITE_OK;
! 26969: }
! 26970:
! 26971: /* To fully unlock the database, delete the lock file */
! 26972: assert( eFileLock==NO_LOCK );
! 26973: rc = osRmdir(zLockFile);
! 26974: if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile);
! 26975: if( rc<0 ){
! 26976: int tErrno = errno;
! 26977: rc = 0;
! 26978: if( ENOENT != tErrno ){
! 26979: rc = SQLITE_IOERR_UNLOCK;
! 26980: }
! 26981: if( IS_LOCK_ERROR(rc) ){
! 26982: pFile->lastErrno = tErrno;
! 26983: }
! 26984: return rc;
! 26985: }
! 26986: pFile->eFileLock = NO_LOCK;
! 26987: return SQLITE_OK;
! 26988: }
! 26989:
! 26990: /*
! 26991: ** Close a file. Make sure the lock has been released before closing.
! 26992: */
! 26993: static int dotlockClose(sqlite3_file *id) {
! 26994: int rc;
! 26995: if( id ){
! 26996: unixFile *pFile = (unixFile*)id;
! 26997: dotlockUnlock(id, NO_LOCK);
! 26998: sqlite3_free(pFile->lockingContext);
! 26999: }
! 27000: rc = closeUnixFile(id);
! 27001: return rc;
! 27002: }
! 27003: /****************** End of the dot-file lock implementation *******************
! 27004: ******************************************************************************/
! 27005:
! 27006: /******************************************************************************
! 27007: ************************** Begin flock Locking ********************************
! 27008: **
! 27009: ** Use the flock() system call to do file locking.
! 27010: **
! 27011: ** flock() locking is like dot-file locking in that the various
! 27012: ** fine-grain locking levels supported by SQLite are collapsed into
! 27013: ** a single exclusive lock. In other words, SHARED, RESERVED, and
! 27014: ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
! 27015: ** still works when you do this, but concurrency is reduced since
! 27016: ** only a single process can be reading the database at a time.
! 27017: **
! 27018: ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
! 27019: ** compiling for VXWORKS.
! 27020: */
! 27021: #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
! 27022:
! 27023: /*
! 27024: ** Retry flock() calls that fail with EINTR
! 27025: */
! 27026: #ifdef EINTR
! 27027: static int robust_flock(int fd, int op){
! 27028: int rc;
! 27029: do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
! 27030: return rc;
! 27031: }
! 27032: #else
! 27033: # define robust_flock(a,b) flock(a,b)
! 27034: #endif
! 27035:
! 27036:
! 27037: /*
! 27038: ** This routine checks if there is a RESERVED lock held on the specified
! 27039: ** file by this or any other process. If such a lock is held, set *pResOut
! 27040: ** to a non-zero value otherwise *pResOut is set to zero. The return value
! 27041: ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
! 27042: */
! 27043: static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
! 27044: int rc = SQLITE_OK;
! 27045: int reserved = 0;
! 27046: unixFile *pFile = (unixFile*)id;
! 27047:
! 27048: SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
! 27049:
! 27050: assert( pFile );
! 27051:
! 27052: /* Check if a thread in this process holds such a lock */
! 27053: if( pFile->eFileLock>SHARED_LOCK ){
! 27054: reserved = 1;
! 27055: }
! 27056:
! 27057: /* Otherwise see if some other process holds it. */
! 27058: if( !reserved ){
! 27059: /* attempt to get the lock */
! 27060: int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
! 27061: if( !lrc ){
! 27062: /* got the lock, unlock it */
! 27063: lrc = robust_flock(pFile->h, LOCK_UN);
! 27064: if ( lrc ) {
! 27065: int tErrno = errno;
! 27066: /* unlock failed with an error */
! 27067: lrc = SQLITE_IOERR_UNLOCK;
! 27068: if( IS_LOCK_ERROR(lrc) ){
! 27069: pFile->lastErrno = tErrno;
! 27070: rc = lrc;
! 27071: }
! 27072: }
! 27073: } else {
! 27074: int tErrno = errno;
! 27075: reserved = 1;
! 27076: /* someone else might have it reserved */
! 27077: lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
! 27078: if( IS_LOCK_ERROR(lrc) ){
! 27079: pFile->lastErrno = tErrno;
! 27080: rc = lrc;
! 27081: }
! 27082: }
! 27083: }
! 27084: OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
! 27085:
! 27086: #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
! 27087: if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
! 27088: rc = SQLITE_OK;
! 27089: reserved=1;
! 27090: }
! 27091: #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
! 27092: *pResOut = reserved;
! 27093: return rc;
! 27094: }
! 27095:
! 27096: /*
! 27097: ** Lock the file with the lock specified by parameter eFileLock - one
! 27098: ** of the following:
! 27099: **
! 27100: ** (1) SHARED_LOCK
! 27101: ** (2) RESERVED_LOCK
! 27102: ** (3) PENDING_LOCK
! 27103: ** (4) EXCLUSIVE_LOCK
! 27104: **
! 27105: ** Sometimes when requesting one lock state, additional lock states
! 27106: ** are inserted in between. The locking might fail on one of the later
! 27107: ** transitions leaving the lock state different from what it started but
! 27108: ** still short of its goal. The following chart shows the allowed
! 27109: ** transitions and the inserted intermediate states:
! 27110: **
! 27111: ** UNLOCKED -> SHARED
! 27112: ** SHARED -> RESERVED
! 27113: ** SHARED -> (PENDING) -> EXCLUSIVE
! 27114: ** RESERVED -> (PENDING) -> EXCLUSIVE
! 27115: ** PENDING -> EXCLUSIVE
! 27116: **
! 27117: ** flock() only really support EXCLUSIVE locks. We track intermediate
! 27118: ** lock states in the sqlite3_file structure, but all locks SHARED or
! 27119: ** above are really EXCLUSIVE locks and exclude all other processes from
! 27120: ** access the file.
! 27121: **
! 27122: ** This routine will only increase a lock. Use the sqlite3OsUnlock()
! 27123: ** routine to lower a locking level.
! 27124: */
! 27125: static int flockLock(sqlite3_file *id, int eFileLock) {
! 27126: int rc = SQLITE_OK;
! 27127: unixFile *pFile = (unixFile*)id;
! 27128:
! 27129: assert( pFile );
! 27130:
! 27131: /* if we already have a lock, it is exclusive.
! 27132: ** Just adjust level and punt on outta here. */
! 27133: if (pFile->eFileLock > NO_LOCK) {
! 27134: pFile->eFileLock = eFileLock;
! 27135: return SQLITE_OK;
! 27136: }
! 27137:
! 27138: /* grab an exclusive lock */
! 27139:
! 27140: if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
! 27141: int tErrno = errno;
! 27142: /* didn't get, must be busy */
! 27143: rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
! 27144: if( IS_LOCK_ERROR(rc) ){
! 27145: pFile->lastErrno = tErrno;
! 27146: }
! 27147: } else {
! 27148: /* got it, set the type and return ok */
! 27149: pFile->eFileLock = eFileLock;
! 27150: }
! 27151: OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
! 27152: rc==SQLITE_OK ? "ok" : "failed"));
! 27153: #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
! 27154: if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
! 27155: rc = SQLITE_BUSY;
! 27156: }
! 27157: #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
! 27158: return rc;
! 27159: }
! 27160:
! 27161:
! 27162: /*
! 27163: ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
! 27164: ** must be either NO_LOCK or SHARED_LOCK.
! 27165: **
! 27166: ** If the locking level of the file descriptor is already at or below
! 27167: ** the requested locking level, this routine is a no-op.
! 27168: */
! 27169: static int flockUnlock(sqlite3_file *id, int eFileLock) {
! 27170: unixFile *pFile = (unixFile*)id;
! 27171:
! 27172: assert( pFile );
! 27173: OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
! 27174: pFile->eFileLock, getpid()));
! 27175: assert( eFileLock<=SHARED_LOCK );
! 27176:
! 27177: /* no-op if possible */
! 27178: if( pFile->eFileLock==eFileLock ){
! 27179: return SQLITE_OK;
! 27180: }
! 27181:
! 27182: /* shared can just be set because we always have an exclusive */
! 27183: if (eFileLock==SHARED_LOCK) {
! 27184: pFile->eFileLock = eFileLock;
! 27185: return SQLITE_OK;
! 27186: }
! 27187:
! 27188: /* no, really, unlock. */
! 27189: if( robust_flock(pFile->h, LOCK_UN) ){
! 27190: #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
! 27191: return SQLITE_OK;
! 27192: #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
! 27193: return SQLITE_IOERR_UNLOCK;
! 27194: }else{
! 27195: pFile->eFileLock = NO_LOCK;
! 27196: return SQLITE_OK;
! 27197: }
! 27198: }
! 27199:
! 27200: /*
! 27201: ** Close a file.
! 27202: */
! 27203: static int flockClose(sqlite3_file *id) {
! 27204: if( id ){
! 27205: flockUnlock(id, NO_LOCK);
! 27206: }
! 27207: return closeUnixFile(id);
! 27208: }
! 27209:
! 27210: #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
! 27211:
! 27212: /******************* End of the flock lock implementation *********************
! 27213: ******************************************************************************/
! 27214:
! 27215: /******************************************************************************
! 27216: ************************ Begin Named Semaphore Locking ************************
! 27217: **
! 27218: ** Named semaphore locking is only supported on VxWorks.
! 27219: **
! 27220: ** Semaphore locking is like dot-lock and flock in that it really only
! 27221: ** supports EXCLUSIVE locking. Only a single process can read or write
! 27222: ** the database file at a time. This reduces potential concurrency, but
! 27223: ** makes the lock implementation much easier.
! 27224: */
! 27225: #if OS_VXWORKS
! 27226:
! 27227: /*
! 27228: ** This routine checks if there is a RESERVED lock held on the specified
! 27229: ** file by this or any other process. If such a lock is held, set *pResOut
! 27230: ** to a non-zero value otherwise *pResOut is set to zero. The return value
! 27231: ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
! 27232: */
! 27233: static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
! 27234: int rc = SQLITE_OK;
! 27235: int reserved = 0;
! 27236: unixFile *pFile = (unixFile*)id;
! 27237:
! 27238: SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
! 27239:
! 27240: assert( pFile );
! 27241:
! 27242: /* Check if a thread in this process holds such a lock */
! 27243: if( pFile->eFileLock>SHARED_LOCK ){
! 27244: reserved = 1;
! 27245: }
! 27246:
! 27247: /* Otherwise see if some other process holds it. */
! 27248: if( !reserved ){
! 27249: sem_t *pSem = pFile->pInode->pSem;
! 27250: struct stat statBuf;
! 27251:
! 27252: if( sem_trywait(pSem)==-1 ){
! 27253: int tErrno = errno;
! 27254: if( EAGAIN != tErrno ){
! 27255: rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
! 27256: pFile->lastErrno = tErrno;
! 27257: } else {
! 27258: /* someone else has the lock when we are in NO_LOCK */
! 27259: reserved = (pFile->eFileLock < SHARED_LOCK);
! 27260: }
! 27261: }else{
! 27262: /* we could have it if we want it */
! 27263: sem_post(pSem);
! 27264: }
! 27265: }
! 27266: OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
! 27267:
! 27268: *pResOut = reserved;
! 27269: return rc;
! 27270: }
! 27271:
! 27272: /*
! 27273: ** Lock the file with the lock specified by parameter eFileLock - one
! 27274: ** of the following:
! 27275: **
! 27276: ** (1) SHARED_LOCK
! 27277: ** (2) RESERVED_LOCK
! 27278: ** (3) PENDING_LOCK
! 27279: ** (4) EXCLUSIVE_LOCK
! 27280: **
! 27281: ** Sometimes when requesting one lock state, additional lock states
! 27282: ** are inserted in between. The locking might fail on one of the later
! 27283: ** transitions leaving the lock state different from what it started but
! 27284: ** still short of its goal. The following chart shows the allowed
! 27285: ** transitions and the inserted intermediate states:
! 27286: **
! 27287: ** UNLOCKED -> SHARED
! 27288: ** SHARED -> RESERVED
! 27289: ** SHARED -> (PENDING) -> EXCLUSIVE
! 27290: ** RESERVED -> (PENDING) -> EXCLUSIVE
! 27291: ** PENDING -> EXCLUSIVE
! 27292: **
! 27293: ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
! 27294: ** lock states in the sqlite3_file structure, but all locks SHARED or
! 27295: ** above are really EXCLUSIVE locks and exclude all other processes from
! 27296: ** access the file.
! 27297: **
! 27298: ** This routine will only increase a lock. Use the sqlite3OsUnlock()
! 27299: ** routine to lower a locking level.
! 27300: */
! 27301: static int semLock(sqlite3_file *id, int eFileLock) {
! 27302: unixFile *pFile = (unixFile*)id;
! 27303: int fd;
! 27304: sem_t *pSem = pFile->pInode->pSem;
! 27305: int rc = SQLITE_OK;
! 27306:
! 27307: /* if we already have a lock, it is exclusive.
! 27308: ** Just adjust level and punt on outta here. */
! 27309: if (pFile->eFileLock > NO_LOCK) {
! 27310: pFile->eFileLock = eFileLock;
! 27311: rc = SQLITE_OK;
! 27312: goto sem_end_lock;
! 27313: }
! 27314:
! 27315: /* lock semaphore now but bail out when already locked. */
! 27316: if( sem_trywait(pSem)==-1 ){
! 27317: rc = SQLITE_BUSY;
! 27318: goto sem_end_lock;
! 27319: }
! 27320:
! 27321: /* got it, set the type and return ok */
! 27322: pFile->eFileLock = eFileLock;
! 27323:
! 27324: sem_end_lock:
! 27325: return rc;
! 27326: }
! 27327:
! 27328: /*
! 27329: ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
! 27330: ** must be either NO_LOCK or SHARED_LOCK.
! 27331: **
! 27332: ** If the locking level of the file descriptor is already at or below
! 27333: ** the requested locking level, this routine is a no-op.
! 27334: */
! 27335: static int semUnlock(sqlite3_file *id, int eFileLock) {
! 27336: unixFile *pFile = (unixFile*)id;
! 27337: sem_t *pSem = pFile->pInode->pSem;
! 27338:
! 27339: assert( pFile );
! 27340: assert( pSem );
! 27341: OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
! 27342: pFile->eFileLock, getpid()));
! 27343: assert( eFileLock<=SHARED_LOCK );
! 27344:
! 27345: /* no-op if possible */
! 27346: if( pFile->eFileLock==eFileLock ){
! 27347: return SQLITE_OK;
! 27348: }
! 27349:
! 27350: /* shared can just be set because we always have an exclusive */
! 27351: if (eFileLock==SHARED_LOCK) {
! 27352: pFile->eFileLock = eFileLock;
! 27353: return SQLITE_OK;
! 27354: }
! 27355:
! 27356: /* no, really unlock. */
! 27357: if ( sem_post(pSem)==-1 ) {
! 27358: int rc, tErrno = errno;
! 27359: rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
! 27360: if( IS_LOCK_ERROR(rc) ){
! 27361: pFile->lastErrno = tErrno;
! 27362: }
! 27363: return rc;
! 27364: }
! 27365: pFile->eFileLock = NO_LOCK;
! 27366: return SQLITE_OK;
! 27367: }
! 27368:
! 27369: /*
! 27370: ** Close a file.
! 27371: */
! 27372: static int semClose(sqlite3_file *id) {
! 27373: if( id ){
! 27374: unixFile *pFile = (unixFile*)id;
! 27375: semUnlock(id, NO_LOCK);
! 27376: assert( pFile );
! 27377: unixEnterMutex();
! 27378: releaseInodeInfo(pFile);
! 27379: unixLeaveMutex();
! 27380: closeUnixFile(id);
! 27381: }
! 27382: return SQLITE_OK;
! 27383: }
! 27384:
! 27385: #endif /* OS_VXWORKS */
! 27386: /*
! 27387: ** Named semaphore locking is only available on VxWorks.
! 27388: **
! 27389: *************** End of the named semaphore lock implementation ****************
! 27390: ******************************************************************************/
! 27391:
! 27392:
! 27393: /******************************************************************************
! 27394: *************************** Begin AFP Locking *********************************
! 27395: **
! 27396: ** AFP is the Apple Filing Protocol. AFP is a network filesystem found
! 27397: ** on Apple Macintosh computers - both OS9 and OSX.
! 27398: **
! 27399: ** Third-party implementations of AFP are available. But this code here
! 27400: ** only works on OSX.
! 27401: */
! 27402:
! 27403: #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
! 27404: /*
! 27405: ** The afpLockingContext structure contains all afp lock specific state
! 27406: */
! 27407: typedef struct afpLockingContext afpLockingContext;
! 27408: struct afpLockingContext {
! 27409: int reserved;
! 27410: const char *dbPath; /* Name of the open file */
! 27411: };
! 27412:
! 27413: struct ByteRangeLockPB2
! 27414: {
! 27415: unsigned long long offset; /* offset to first byte to lock */
! 27416: unsigned long long length; /* nbr of bytes to lock */
! 27417: unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
! 27418: unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
! 27419: unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
! 27420: int fd; /* file desc to assoc this lock with */
! 27421: };
! 27422:
! 27423: #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
! 27424:
! 27425: /*
! 27426: ** This is a utility for setting or clearing a bit-range lock on an
! 27427: ** AFP filesystem.
! 27428: **
! 27429: ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
! 27430: */
! 27431: static int afpSetLock(
! 27432: const char *path, /* Name of the file to be locked or unlocked */
! 27433: unixFile *pFile, /* Open file descriptor on path */
! 27434: unsigned long long offset, /* First byte to be locked */
! 27435: unsigned long long length, /* Number of bytes to lock */
! 27436: int setLockFlag /* True to set lock. False to clear lock */
! 27437: ){
! 27438: struct ByteRangeLockPB2 pb;
! 27439: int err;
! 27440:
! 27441: pb.unLockFlag = setLockFlag ? 0 : 1;
! 27442: pb.startEndFlag = 0;
! 27443: pb.offset = offset;
! 27444: pb.length = length;
! 27445: pb.fd = pFile->h;
! 27446:
! 27447: OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
! 27448: (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
! 27449: offset, length));
! 27450: err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
! 27451: if ( err==-1 ) {
! 27452: int rc;
! 27453: int tErrno = errno;
! 27454: OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
! 27455: path, tErrno, strerror(tErrno)));
! 27456: #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
! 27457: rc = SQLITE_BUSY;
! 27458: #else
! 27459: rc = sqliteErrorFromPosixError(tErrno,
! 27460: setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
! 27461: #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
! 27462: if( IS_LOCK_ERROR(rc) ){
! 27463: pFile->lastErrno = tErrno;
! 27464: }
! 27465: return rc;
! 27466: } else {
! 27467: return SQLITE_OK;
! 27468: }
! 27469: }
! 27470:
! 27471: /*
! 27472: ** This routine checks if there is a RESERVED lock held on the specified
! 27473: ** file by this or any other process. If such a lock is held, set *pResOut
! 27474: ** to a non-zero value otherwise *pResOut is set to zero. The return value
! 27475: ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
! 27476: */
! 27477: static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
! 27478: int rc = SQLITE_OK;
! 27479: int reserved = 0;
! 27480: unixFile *pFile = (unixFile*)id;
! 27481: afpLockingContext *context;
! 27482:
! 27483: SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
! 27484:
! 27485: assert( pFile );
! 27486: context = (afpLockingContext *) pFile->lockingContext;
! 27487: if( context->reserved ){
! 27488: *pResOut = 1;
! 27489: return SQLITE_OK;
! 27490: }
! 27491: unixEnterMutex(); /* Because pFile->pInode is shared across threads */
! 27492:
! 27493: /* Check if a thread in this process holds such a lock */
! 27494: if( pFile->pInode->eFileLock>SHARED_LOCK ){
! 27495: reserved = 1;
! 27496: }
! 27497:
! 27498: /* Otherwise see if some other process holds it.
! 27499: */
! 27500: if( !reserved ){
! 27501: /* lock the RESERVED byte */
! 27502: int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
! 27503: if( SQLITE_OK==lrc ){
! 27504: /* if we succeeded in taking the reserved lock, unlock it to restore
! 27505: ** the original state */
! 27506: lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
! 27507: } else {
! 27508: /* if we failed to get the lock then someone else must have it */
! 27509: reserved = 1;
! 27510: }
! 27511: if( IS_LOCK_ERROR(lrc) ){
! 27512: rc=lrc;
! 27513: }
! 27514: }
! 27515:
! 27516: unixLeaveMutex();
! 27517: OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
! 27518:
! 27519: *pResOut = reserved;
! 27520: return rc;
! 27521: }
! 27522:
! 27523: /*
! 27524: ** Lock the file with the lock specified by parameter eFileLock - one
! 27525: ** of the following:
! 27526: **
! 27527: ** (1) SHARED_LOCK
! 27528: ** (2) RESERVED_LOCK
! 27529: ** (3) PENDING_LOCK
! 27530: ** (4) EXCLUSIVE_LOCK
! 27531: **
! 27532: ** Sometimes when requesting one lock state, additional lock states
! 27533: ** are inserted in between. The locking might fail on one of the later
! 27534: ** transitions leaving the lock state different from what it started but
! 27535: ** still short of its goal. The following chart shows the allowed
! 27536: ** transitions and the inserted intermediate states:
! 27537: **
! 27538: ** UNLOCKED -> SHARED
! 27539: ** SHARED -> RESERVED
! 27540: ** SHARED -> (PENDING) -> EXCLUSIVE
! 27541: ** RESERVED -> (PENDING) -> EXCLUSIVE
! 27542: ** PENDING -> EXCLUSIVE
! 27543: **
! 27544: ** This routine will only increase a lock. Use the sqlite3OsUnlock()
! 27545: ** routine to lower a locking level.
! 27546: */
! 27547: static int afpLock(sqlite3_file *id, int eFileLock){
! 27548: int rc = SQLITE_OK;
! 27549: unixFile *pFile = (unixFile*)id;
! 27550: unixInodeInfo *pInode = pFile->pInode;
! 27551: afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
! 27552:
! 27553: assert( pFile );
! 27554: OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
! 27555: azFileLock(eFileLock), azFileLock(pFile->eFileLock),
! 27556: azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
! 27557:
! 27558: /* If there is already a lock of this type or more restrictive on the
! 27559: ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
! 27560: ** unixEnterMutex() hasn't been called yet.
! 27561: */
! 27562: if( pFile->eFileLock>=eFileLock ){
! 27563: OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
! 27564: azFileLock(eFileLock)));
! 27565: return SQLITE_OK;
! 27566: }
! 27567:
! 27568: /* Make sure the locking sequence is correct
! 27569: ** (1) We never move from unlocked to anything higher than shared lock.
! 27570: ** (2) SQLite never explicitly requests a pendig lock.
! 27571: ** (3) A shared lock is always held when a reserve lock is requested.
! 27572: */
! 27573: assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
! 27574: assert( eFileLock!=PENDING_LOCK );
! 27575: assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
! 27576:
! 27577: /* This mutex is needed because pFile->pInode is shared across threads
! 27578: */
! 27579: unixEnterMutex();
! 27580: pInode = pFile->pInode;
! 27581:
! 27582: /* If some thread using this PID has a lock via a different unixFile*
! 27583: ** handle that precludes the requested lock, return BUSY.
! 27584: */
! 27585: if( (pFile->eFileLock!=pInode->eFileLock &&
! 27586: (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
! 27587: ){
! 27588: rc = SQLITE_BUSY;
! 27589: goto afp_end_lock;
! 27590: }
! 27591:
! 27592: /* If a SHARED lock is requested, and some thread using this PID already
! 27593: ** has a SHARED or RESERVED lock, then increment reference counts and
! 27594: ** return SQLITE_OK.
! 27595: */
! 27596: if( eFileLock==SHARED_LOCK &&
! 27597: (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
! 27598: assert( eFileLock==SHARED_LOCK );
! 27599: assert( pFile->eFileLock==0 );
! 27600: assert( pInode->nShared>0 );
! 27601: pFile->eFileLock = SHARED_LOCK;
! 27602: pInode->nShared++;
! 27603: pInode->nLock++;
! 27604: goto afp_end_lock;
! 27605: }
! 27606:
! 27607: /* A PENDING lock is needed before acquiring a SHARED lock and before
! 27608: ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
! 27609: ** be released.
! 27610: */
! 27611: if( eFileLock==SHARED_LOCK
! 27612: || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
! 27613: ){
! 27614: int failed;
! 27615: failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
! 27616: if (failed) {
! 27617: rc = failed;
! 27618: goto afp_end_lock;
! 27619: }
! 27620: }
! 27621:
! 27622: /* If control gets to this point, then actually go ahead and make
! 27623: ** operating system calls for the specified lock.
! 27624: */
! 27625: if( eFileLock==SHARED_LOCK ){
! 27626: int lrc1, lrc2, lrc1Errno = 0;
! 27627: long lk, mask;
! 27628:
! 27629: assert( pInode->nShared==0 );
! 27630: assert( pInode->eFileLock==0 );
! 27631:
! 27632: mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
! 27633: /* Now get the read-lock SHARED_LOCK */
! 27634: /* note that the quality of the randomness doesn't matter that much */
! 27635: lk = random();
! 27636: pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
! 27637: lrc1 = afpSetLock(context->dbPath, pFile,
! 27638: SHARED_FIRST+pInode->sharedByte, 1, 1);
! 27639: if( IS_LOCK_ERROR(lrc1) ){
! 27640: lrc1Errno = pFile->lastErrno;
! 27641: }
! 27642: /* Drop the temporary PENDING lock */
! 27643: lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
! 27644:
! 27645: if( IS_LOCK_ERROR(lrc1) ) {
! 27646: pFile->lastErrno = lrc1Errno;
! 27647: rc = lrc1;
! 27648: goto afp_end_lock;
! 27649: } else if( IS_LOCK_ERROR(lrc2) ){
! 27650: rc = lrc2;
! 27651: goto afp_end_lock;
! 27652: } else if( lrc1 != SQLITE_OK ) {
! 27653: rc = lrc1;
! 27654: } else {
! 27655: pFile->eFileLock = SHARED_LOCK;
! 27656: pInode->nLock++;
! 27657: pInode->nShared = 1;
! 27658: }
! 27659: }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
! 27660: /* We are trying for an exclusive lock but another thread in this
! 27661: ** same process is still holding a shared lock. */
! 27662: rc = SQLITE_BUSY;
! 27663: }else{
! 27664: /* The request was for a RESERVED or EXCLUSIVE lock. It is
! 27665: ** assumed that there is a SHARED or greater lock on the file
! 27666: ** already.
! 27667: */
! 27668: int failed = 0;
! 27669: assert( 0!=pFile->eFileLock );
! 27670: if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
! 27671: /* Acquire a RESERVED lock */
! 27672: failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
! 27673: if( !failed ){
! 27674: context->reserved = 1;
! 27675: }
! 27676: }
! 27677: if (!failed && eFileLock == EXCLUSIVE_LOCK) {
! 27678: /* Acquire an EXCLUSIVE lock */
! 27679:
! 27680: /* Remove the shared lock before trying the range. we'll need to
! 27681: ** reestablish the shared lock if we can't get the afpUnlock
! 27682: */
! 27683: if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
! 27684: pInode->sharedByte, 1, 0)) ){
! 27685: int failed2 = SQLITE_OK;
! 27686: /* now attemmpt to get the exclusive lock range */
! 27687: failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
! 27688: SHARED_SIZE, 1);
! 27689: if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
! 27690: SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
! 27691: /* Can't reestablish the shared lock. Sqlite can't deal, this is
! 27692: ** a critical I/O error
! 27693: */
! 27694: rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
! 27695: SQLITE_IOERR_LOCK;
! 27696: goto afp_end_lock;
! 27697: }
! 27698: }else{
! 27699: rc = failed;
! 27700: }
! 27701: }
! 27702: if( failed ){
! 27703: rc = failed;
! 27704: }
! 27705: }
! 27706:
! 27707: if( rc==SQLITE_OK ){
! 27708: pFile->eFileLock = eFileLock;
! 27709: pInode->eFileLock = eFileLock;
! 27710: }else if( eFileLock==EXCLUSIVE_LOCK ){
! 27711: pFile->eFileLock = PENDING_LOCK;
! 27712: pInode->eFileLock = PENDING_LOCK;
! 27713: }
! 27714:
! 27715: afp_end_lock:
! 27716: unixLeaveMutex();
! 27717: OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
! 27718: rc==SQLITE_OK ? "ok" : "failed"));
! 27719: return rc;
! 27720: }
! 27721:
! 27722: /*
! 27723: ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
! 27724: ** must be either NO_LOCK or SHARED_LOCK.
! 27725: **
! 27726: ** If the locking level of the file descriptor is already at or below
! 27727: ** the requested locking level, this routine is a no-op.
! 27728: */
! 27729: static int afpUnlock(sqlite3_file *id, int eFileLock) {
! 27730: int rc = SQLITE_OK;
! 27731: unixFile *pFile = (unixFile*)id;
! 27732: unixInodeInfo *pInode;
! 27733: afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
! 27734: int skipShared = 0;
! 27735: #ifdef SQLITE_TEST
! 27736: int h = pFile->h;
! 27737: #endif
! 27738:
! 27739: assert( pFile );
! 27740: OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
! 27741: pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
! 27742: getpid()));
! 27743:
! 27744: assert( eFileLock<=SHARED_LOCK );
! 27745: if( pFile->eFileLock<=eFileLock ){
! 27746: return SQLITE_OK;
! 27747: }
! 27748: unixEnterMutex();
! 27749: pInode = pFile->pInode;
! 27750: assert( pInode->nShared!=0 );
! 27751: if( pFile->eFileLock>SHARED_LOCK ){
! 27752: assert( pInode->eFileLock==pFile->eFileLock );
! 27753: SimulateIOErrorBenign(1);
! 27754: SimulateIOError( h=(-1) )
! 27755: SimulateIOErrorBenign(0);
! 27756:
! 27757: #ifndef NDEBUG
! 27758: /* When reducing a lock such that other processes can start
! 27759: ** reading the database file again, make sure that the
! 27760: ** transaction counter was updated if any part of the database
! 27761: ** file changed. If the transaction counter is not updated,
! 27762: ** other connections to the same file might not realize that
! 27763: ** the file has changed and hence might not know to flush their
! 27764: ** cache. The use of a stale cache can lead to database corruption.
! 27765: */
! 27766: assert( pFile->inNormalWrite==0
! 27767: || pFile->dbUpdate==0
! 27768: || pFile->transCntrChng==1 );
! 27769: pFile->inNormalWrite = 0;
! 27770: #endif
! 27771:
! 27772: if( pFile->eFileLock==EXCLUSIVE_LOCK ){
! 27773: rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
! 27774: if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
! 27775: /* only re-establish the shared lock if necessary */
! 27776: int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
! 27777: rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
! 27778: } else {
! 27779: skipShared = 1;
! 27780: }
! 27781: }
! 27782: if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
! 27783: rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
! 27784: }
! 27785: if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
! 27786: rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
! 27787: if( !rc ){
! 27788: context->reserved = 0;
! 27789: }
! 27790: }
! 27791: if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
! 27792: pInode->eFileLock = SHARED_LOCK;
! 27793: }
! 27794: }
! 27795: if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
! 27796:
! 27797: /* Decrement the shared lock counter. Release the lock using an
! 27798: ** OS call only when all threads in this same process have released
! 27799: ** the lock.
! 27800: */
! 27801: unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
! 27802: pInode->nShared--;
! 27803: if( pInode->nShared==0 ){
! 27804: SimulateIOErrorBenign(1);
! 27805: SimulateIOError( h=(-1) )
! 27806: SimulateIOErrorBenign(0);
! 27807: if( !skipShared ){
! 27808: rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
! 27809: }
! 27810: if( !rc ){
! 27811: pInode->eFileLock = NO_LOCK;
! 27812: pFile->eFileLock = NO_LOCK;
! 27813: }
! 27814: }
! 27815: if( rc==SQLITE_OK ){
! 27816: pInode->nLock--;
! 27817: assert( pInode->nLock>=0 );
! 27818: if( pInode->nLock==0 ){
! 27819: closePendingFds(pFile);
! 27820: }
! 27821: }
! 27822: }
! 27823:
! 27824: unixLeaveMutex();
! 27825: if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
! 27826: return rc;
! 27827: }
! 27828:
! 27829: /*
! 27830: ** Close a file & cleanup AFP specific locking context
! 27831: */
! 27832: static int afpClose(sqlite3_file *id) {
! 27833: int rc = SQLITE_OK;
! 27834: if( id ){
! 27835: unixFile *pFile = (unixFile*)id;
! 27836: afpUnlock(id, NO_LOCK);
! 27837: unixEnterMutex();
! 27838: if( pFile->pInode && pFile->pInode->nLock ){
! 27839: /* If there are outstanding locks, do not actually close the file just
! 27840: ** yet because that would clear those locks. Instead, add the file
! 27841: ** descriptor to pInode->aPending. It will be automatically closed when
! 27842: ** the last lock is cleared.
! 27843: */
! 27844: setPendingFd(pFile);
! 27845: }
! 27846: releaseInodeInfo(pFile);
! 27847: sqlite3_free(pFile->lockingContext);
! 27848: rc = closeUnixFile(id);
! 27849: unixLeaveMutex();
! 27850: }
! 27851: return rc;
! 27852: }
! 27853:
! 27854: #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
! 27855: /*
! 27856: ** The code above is the AFP lock implementation. The code is specific
! 27857: ** to MacOSX and does not work on other unix platforms. No alternative
! 27858: ** is available. If you don't compile for a mac, then the "unix-afp"
! 27859: ** VFS is not available.
! 27860: **
! 27861: ********************* End of the AFP lock implementation **********************
! 27862: ******************************************************************************/
! 27863:
! 27864: /******************************************************************************
! 27865: *************************** Begin NFS Locking ********************************/
! 27866:
! 27867: #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
! 27868: /*
! 27869: ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
! 27870: ** must be either NO_LOCK or SHARED_LOCK.
! 27871: **
! 27872: ** If the locking level of the file descriptor is already at or below
! 27873: ** the requested locking level, this routine is a no-op.
! 27874: */
! 27875: static int nfsUnlock(sqlite3_file *id, int eFileLock){
! 27876: return posixUnlock(id, eFileLock, 1);
! 27877: }
! 27878:
! 27879: #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
! 27880: /*
! 27881: ** The code above is the NFS lock implementation. The code is specific
! 27882: ** to MacOSX and does not work on other unix platforms. No alternative
! 27883: ** is available.
! 27884: **
! 27885: ********************* End of the NFS lock implementation **********************
! 27886: ******************************************************************************/
! 27887:
! 27888: /******************************************************************************
! 27889: **************** Non-locking sqlite3_file methods *****************************
! 27890: **
! 27891: ** The next division contains implementations for all methods of the
! 27892: ** sqlite3_file object other than the locking methods. The locking
! 27893: ** methods were defined in divisions above (one locking method per
! 27894: ** division). Those methods that are common to all locking modes
! 27895: ** are gather together into this division.
! 27896: */
! 27897:
! 27898: /*
! 27899: ** Seek to the offset passed as the second argument, then read cnt
! 27900: ** bytes into pBuf. Return the number of bytes actually read.
! 27901: **
! 27902: ** NB: If you define USE_PREAD or USE_PREAD64, then it might also
! 27903: ** be necessary to define _XOPEN_SOURCE to be 500. This varies from
! 27904: ** one system to another. Since SQLite does not define USE_PREAD
! 27905: ** any any form by default, we will not attempt to define _XOPEN_SOURCE.
! 27906: ** See tickets #2741 and #2681.
! 27907: **
! 27908: ** To avoid stomping the errno value on a failed read the lastErrno value
! 27909: ** is set before returning.
! 27910: */
! 27911: static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
! 27912: int got;
! 27913: int prior = 0;
! 27914: #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
! 27915: i64 newOffset;
! 27916: #endif
! 27917: TIMER_START;
! 27918: do{
! 27919: #if defined(USE_PREAD)
! 27920: got = osPread(id->h, pBuf, cnt, offset);
! 27921: SimulateIOError( got = -1 );
! 27922: #elif defined(USE_PREAD64)
! 27923: got = osPread64(id->h, pBuf, cnt, offset);
! 27924: SimulateIOError( got = -1 );
! 27925: #else
! 27926: newOffset = lseek(id->h, offset, SEEK_SET);
! 27927: SimulateIOError( newOffset-- );
! 27928: if( newOffset!=offset ){
! 27929: if( newOffset == -1 ){
! 27930: ((unixFile*)id)->lastErrno = errno;
! 27931: }else{
! 27932: ((unixFile*)id)->lastErrno = 0;
! 27933: }
! 27934: return -1;
! 27935: }
! 27936: got = osRead(id->h, pBuf, cnt);
! 27937: #endif
! 27938: if( got==cnt ) break;
! 27939: if( got<0 ){
! 27940: if( errno==EINTR ){ got = 1; continue; }
! 27941: prior = 0;
! 27942: ((unixFile*)id)->lastErrno = errno;
! 27943: break;
! 27944: }else if( got>0 ){
! 27945: cnt -= got;
! 27946: offset += got;
! 27947: prior += got;
! 27948: pBuf = (void*)(got + (char*)pBuf);
! 27949: }
! 27950: }while( got>0 );
! 27951: TIMER_END;
! 27952: OSTRACE(("READ %-3d %5d %7lld %llu\n",
! 27953: id->h, got+prior, offset-prior, TIMER_ELAPSED));
! 27954: return got+prior;
! 27955: }
! 27956:
! 27957: /*
! 27958: ** Read data from a file into a buffer. Return SQLITE_OK if all
! 27959: ** bytes were read successfully and SQLITE_IOERR if anything goes
! 27960: ** wrong.
! 27961: */
! 27962: static int unixRead(
! 27963: sqlite3_file *id,
! 27964: void *pBuf,
! 27965: int amt,
! 27966: sqlite3_int64 offset
! 27967: ){
! 27968: unixFile *pFile = (unixFile *)id;
! 27969: int got;
! 27970: assert( id );
! 27971:
! 27972: /* If this is a database file (not a journal, master-journal or temp
! 27973: ** file), the bytes in the locking range should never be read or written. */
! 27974: #if 0
! 27975: assert( pFile->pUnused==0
! 27976: || offset>=PENDING_BYTE+512
! 27977: || offset+amt<=PENDING_BYTE
! 27978: );
! 27979: #endif
! 27980:
! 27981: got = seekAndRead(pFile, offset, pBuf, amt);
! 27982: if( got==amt ){
! 27983: return SQLITE_OK;
! 27984: }else if( got<0 ){
! 27985: /* lastErrno set by seekAndRead */
! 27986: return SQLITE_IOERR_READ;
! 27987: }else{
! 27988: pFile->lastErrno = 0; /* not a system error */
! 27989: /* Unread parts of the buffer must be zero-filled */
! 27990: memset(&((char*)pBuf)[got], 0, amt-got);
! 27991: return SQLITE_IOERR_SHORT_READ;
! 27992: }
! 27993: }
! 27994:
! 27995: /*
! 27996: ** Seek to the offset in id->offset then read cnt bytes into pBuf.
! 27997: ** Return the number of bytes actually read. Update the offset.
! 27998: **
! 27999: ** To avoid stomping the errno value on a failed write the lastErrno value
! 28000: ** is set before returning.
! 28001: */
! 28002: static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
! 28003: int got;
! 28004: #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
! 28005: i64 newOffset;
! 28006: #endif
! 28007: TIMER_START;
! 28008: #if defined(USE_PREAD)
! 28009: do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
! 28010: #elif defined(USE_PREAD64)
! 28011: do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
! 28012: #else
! 28013: do{
! 28014: newOffset = lseek(id->h, offset, SEEK_SET);
! 28015: SimulateIOError( newOffset-- );
! 28016: if( newOffset!=offset ){
! 28017: if( newOffset == -1 ){
! 28018: ((unixFile*)id)->lastErrno = errno;
! 28019: }else{
! 28020: ((unixFile*)id)->lastErrno = 0;
! 28021: }
! 28022: return -1;
! 28023: }
! 28024: got = osWrite(id->h, pBuf, cnt);
! 28025: }while( got<0 && errno==EINTR );
! 28026: #endif
! 28027: TIMER_END;
! 28028: if( got<0 ){
! 28029: ((unixFile*)id)->lastErrno = errno;
! 28030: }
! 28031:
! 28032: OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
! 28033: return got;
! 28034: }
! 28035:
! 28036:
! 28037: /*
! 28038: ** Write data from a buffer into a file. Return SQLITE_OK on success
! 28039: ** or some other error code on failure.
! 28040: */
! 28041: static int unixWrite(
! 28042: sqlite3_file *id,
! 28043: const void *pBuf,
! 28044: int amt,
! 28045: sqlite3_int64 offset
! 28046: ){
! 28047: unixFile *pFile = (unixFile*)id;
! 28048: int wrote = 0;
! 28049: assert( id );
! 28050: assert( amt>0 );
! 28051:
! 28052: /* If this is a database file (not a journal, master-journal or temp
! 28053: ** file), the bytes in the locking range should never be read or written. */
! 28054: #if 0
! 28055: assert( pFile->pUnused==0
! 28056: || offset>=PENDING_BYTE+512
! 28057: || offset+amt<=PENDING_BYTE
! 28058: );
! 28059: #endif
! 28060:
! 28061: #ifndef NDEBUG
! 28062: /* If we are doing a normal write to a database file (as opposed to
! 28063: ** doing a hot-journal rollback or a write to some file other than a
! 28064: ** normal database file) then record the fact that the database
! 28065: ** has changed. If the transaction counter is modified, record that
! 28066: ** fact too.
! 28067: */
! 28068: if( pFile->inNormalWrite ){
! 28069: pFile->dbUpdate = 1; /* The database has been modified */
! 28070: if( offset<=24 && offset+amt>=27 ){
! 28071: int rc;
! 28072: char oldCntr[4];
! 28073: SimulateIOErrorBenign(1);
! 28074: rc = seekAndRead(pFile, 24, oldCntr, 4);
! 28075: SimulateIOErrorBenign(0);
! 28076: if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
! 28077: pFile->transCntrChng = 1; /* The transaction counter has changed */
! 28078: }
! 28079: }
! 28080: }
! 28081: #endif
! 28082:
! 28083: while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
! 28084: amt -= wrote;
! 28085: offset += wrote;
! 28086: pBuf = &((char*)pBuf)[wrote];
! 28087: }
! 28088: SimulateIOError(( wrote=(-1), amt=1 ));
! 28089: SimulateDiskfullError(( wrote=0, amt=1 ));
! 28090:
! 28091: if( amt>0 ){
! 28092: if( wrote<0 && pFile->lastErrno!=ENOSPC ){
! 28093: /* lastErrno set by seekAndWrite */
! 28094: return SQLITE_IOERR_WRITE;
! 28095: }else{
! 28096: pFile->lastErrno = 0; /* not a system error */
! 28097: return SQLITE_FULL;
! 28098: }
! 28099: }
! 28100:
! 28101: return SQLITE_OK;
! 28102: }
! 28103:
! 28104: #ifdef SQLITE_TEST
! 28105: /*
! 28106: ** Count the number of fullsyncs and normal syncs. This is used to test
! 28107: ** that syncs and fullsyncs are occurring at the right times.
! 28108: */
! 28109: SQLITE_API int sqlite3_sync_count = 0;
! 28110: SQLITE_API int sqlite3_fullsync_count = 0;
! 28111: #endif
! 28112:
! 28113: /*
! 28114: ** We do not trust systems to provide a working fdatasync(). Some do.
! 28115: ** Others do no. To be safe, we will stick with the (slightly slower)
! 28116: ** fsync(). If you know that your system does support fdatasync() correctly,
! 28117: ** then simply compile with -Dfdatasync=fdatasync
! 28118: */
! 28119: #if !defined(fdatasync)
! 28120: # define fdatasync fsync
! 28121: #endif
! 28122:
! 28123: /*
! 28124: ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
! 28125: ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
! 28126: ** only available on Mac OS X. But that could change.
! 28127: */
! 28128: #ifdef F_FULLFSYNC
! 28129: # define HAVE_FULLFSYNC 1
! 28130: #else
! 28131: # define HAVE_FULLFSYNC 0
! 28132: #endif
! 28133:
! 28134:
! 28135: /*
! 28136: ** The fsync() system call does not work as advertised on many
! 28137: ** unix systems. The following procedure is an attempt to make
! 28138: ** it work better.
! 28139: **
! 28140: ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
! 28141: ** for testing when we want to run through the test suite quickly.
! 28142: ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
! 28143: ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
! 28144: ** or power failure will likely corrupt the database file.
! 28145: **
! 28146: ** SQLite sets the dataOnly flag if the size of the file is unchanged.
! 28147: ** The idea behind dataOnly is that it should only write the file content
! 28148: ** to disk, not the inode. We only set dataOnly if the file size is
! 28149: ** unchanged since the file size is part of the inode. However,
! 28150: ** Ted Ts'o tells us that fdatasync() will also write the inode if the
! 28151: ** file size has changed. The only real difference between fdatasync()
! 28152: ** and fsync(), Ted tells us, is that fdatasync() will not flush the
! 28153: ** inode if the mtime or owner or other inode attributes have changed.
! 28154: ** We only care about the file size, not the other file attributes, so
! 28155: ** as far as SQLite is concerned, an fdatasync() is always adequate.
! 28156: ** So, we always use fdatasync() if it is available, regardless of
! 28157: ** the value of the dataOnly flag.
! 28158: */
! 28159: static int full_fsync(int fd, int fullSync, int dataOnly){
! 28160: int rc;
! 28161:
! 28162: /* The following "ifdef/elif/else/" block has the same structure as
! 28163: ** the one below. It is replicated here solely to avoid cluttering
! 28164: ** up the real code with the UNUSED_PARAMETER() macros.
! 28165: */
! 28166: #ifdef SQLITE_NO_SYNC
! 28167: UNUSED_PARAMETER(fd);
! 28168: UNUSED_PARAMETER(fullSync);
! 28169: UNUSED_PARAMETER(dataOnly);
! 28170: #elif HAVE_FULLFSYNC
! 28171: UNUSED_PARAMETER(dataOnly);
! 28172: #else
! 28173: UNUSED_PARAMETER(fullSync);
! 28174: UNUSED_PARAMETER(dataOnly);
! 28175: #endif
! 28176:
! 28177: /* Record the number of times that we do a normal fsync() and
! 28178: ** FULLSYNC. This is used during testing to verify that this procedure
! 28179: ** gets called with the correct arguments.
! 28180: */
! 28181: #ifdef SQLITE_TEST
! 28182: if( fullSync ) sqlite3_fullsync_count++;
! 28183: sqlite3_sync_count++;
! 28184: #endif
! 28185:
! 28186: /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
! 28187: ** no-op
! 28188: */
! 28189: #ifdef SQLITE_NO_SYNC
! 28190: rc = SQLITE_OK;
! 28191: #elif HAVE_FULLFSYNC
! 28192: if( fullSync ){
! 28193: rc = osFcntl(fd, F_FULLFSYNC, 0);
! 28194: }else{
! 28195: rc = 1;
! 28196: }
! 28197: /* If the FULLFSYNC failed, fall back to attempting an fsync().
! 28198: ** It shouldn't be possible for fullfsync to fail on the local
! 28199: ** file system (on OSX), so failure indicates that FULLFSYNC
! 28200: ** isn't supported for this file system. So, attempt an fsync
! 28201: ** and (for now) ignore the overhead of a superfluous fcntl call.
! 28202: ** It'd be better to detect fullfsync support once and avoid
! 28203: ** the fcntl call every time sync is called.
! 28204: */
! 28205: if( rc ) rc = fsync(fd);
! 28206:
! 28207: #elif defined(__APPLE__)
! 28208: /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
! 28209: ** so currently we default to the macro that redefines fdatasync to fsync
! 28210: */
! 28211: rc = fsync(fd);
! 28212: #else
! 28213: rc = fdatasync(fd);
! 28214: #if OS_VXWORKS
! 28215: if( rc==-1 && errno==ENOTSUP ){
! 28216: rc = fsync(fd);
! 28217: }
! 28218: #endif /* OS_VXWORKS */
! 28219: #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
! 28220:
! 28221: if( OS_VXWORKS && rc!= -1 ){
! 28222: rc = 0;
! 28223: }
! 28224: return rc;
! 28225: }
! 28226:
! 28227: /*
! 28228: ** Open a file descriptor to the directory containing file zFilename.
! 28229: ** If successful, *pFd is set to the opened file descriptor and
! 28230: ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
! 28231: ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
! 28232: ** value.
! 28233: **
! 28234: ** The directory file descriptor is used for only one thing - to
! 28235: ** fsync() a directory to make sure file creation and deletion events
! 28236: ** are flushed to disk. Such fsyncs are not needed on newer
! 28237: ** journaling filesystems, but are required on older filesystems.
! 28238: **
! 28239: ** This routine can be overridden using the xSetSysCall interface.
! 28240: ** The ability to override this routine was added in support of the
! 28241: ** chromium sandbox. Opening a directory is a security risk (we are
! 28242: ** told) so making it overrideable allows the chromium sandbox to
! 28243: ** replace this routine with a harmless no-op. To make this routine
! 28244: ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
! 28245: ** *pFd set to a negative number.
! 28246: **
! 28247: ** If SQLITE_OK is returned, the caller is responsible for closing
! 28248: ** the file descriptor *pFd using close().
! 28249: */
! 28250: static int openDirectory(const char *zFilename, int *pFd){
! 28251: int ii;
! 28252: int fd = -1;
! 28253: char zDirname[MAX_PATHNAME+1];
! 28254:
! 28255: sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
! 28256: for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
! 28257: if( ii>0 ){
! 28258: zDirname[ii] = '\0';
! 28259: fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
! 28260: if( fd>=0 ){
! 28261: #ifdef FD_CLOEXEC
! 28262: osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
! 28263: #endif
! 28264: OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
! 28265: }
! 28266: }
! 28267: *pFd = fd;
! 28268: return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
! 28269: }
! 28270:
! 28271: /*
! 28272: ** Make sure all writes to a particular file are committed to disk.
! 28273: **
! 28274: ** If dataOnly==0 then both the file itself and its metadata (file
! 28275: ** size, access time, etc) are synced. If dataOnly!=0 then only the
! 28276: ** file data is synced.
! 28277: **
! 28278: ** Under Unix, also make sure that the directory entry for the file
! 28279: ** has been created by fsync-ing the directory that contains the file.
! 28280: ** If we do not do this and we encounter a power failure, the directory
! 28281: ** entry for the journal might not exist after we reboot. The next
! 28282: ** SQLite to access the file will not know that the journal exists (because
! 28283: ** the directory entry for the journal was never created) and the transaction
! 28284: ** will not roll back - possibly leading to database corruption.
! 28285: */
! 28286: static int unixSync(sqlite3_file *id, int flags){
! 28287: int rc;
! 28288: unixFile *pFile = (unixFile*)id;
! 28289:
! 28290: int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
! 28291: int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
! 28292:
! 28293: /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
! 28294: assert((flags&0x0F)==SQLITE_SYNC_NORMAL
! 28295: || (flags&0x0F)==SQLITE_SYNC_FULL
! 28296: );
! 28297:
! 28298: /* Unix cannot, but some systems may return SQLITE_FULL from here. This
! 28299: ** line is to test that doing so does not cause any problems.
! 28300: */
! 28301: SimulateDiskfullError( return SQLITE_FULL );
! 28302:
! 28303: assert( pFile );
! 28304: OSTRACE(("SYNC %-3d\n", pFile->h));
! 28305: rc = full_fsync(pFile->h, isFullsync, isDataOnly);
! 28306: SimulateIOError( rc=1 );
! 28307: if( rc ){
! 28308: pFile->lastErrno = errno;
! 28309: return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
! 28310: }
! 28311:
! 28312: /* Also fsync the directory containing the file if the DIRSYNC flag
! 28313: ** is set. This is a one-time occurrance. Many systems (examples: AIX)
! 28314: ** are unable to fsync a directory, so ignore errors on the fsync.
! 28315: */
! 28316: if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
! 28317: int dirfd;
! 28318: OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
! 28319: HAVE_FULLFSYNC, isFullsync));
! 28320: rc = osOpenDirectory(pFile->zPath, &dirfd);
! 28321: if( rc==SQLITE_OK && dirfd>=0 ){
! 28322: full_fsync(dirfd, 0, 0);
! 28323: robust_close(pFile, dirfd, __LINE__);
! 28324: }else if( rc==SQLITE_CANTOPEN ){
! 28325: rc = SQLITE_OK;
! 28326: }
! 28327: pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
! 28328: }
! 28329: return rc;
! 28330: }
! 28331:
! 28332: /*
! 28333: ** Truncate an open file to a specified size
! 28334: */
! 28335: static int unixTruncate(sqlite3_file *id, i64 nByte){
! 28336: unixFile *pFile = (unixFile *)id;
! 28337: int rc;
! 28338: assert( pFile );
! 28339: SimulateIOError( return SQLITE_IOERR_TRUNCATE );
! 28340:
! 28341: /* If the user has configured a chunk-size for this file, truncate the
! 28342: ** file so that it consists of an integer number of chunks (i.e. the
! 28343: ** actual file size after the operation may be larger than the requested
! 28344: ** size).
! 28345: */
! 28346: if( pFile->szChunk ){
! 28347: nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
! 28348: }
! 28349:
! 28350: rc = robust_ftruncate(pFile->h, (off_t)nByte);
! 28351: if( rc ){
! 28352: pFile->lastErrno = errno;
! 28353: return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
! 28354: }else{
! 28355: #ifndef NDEBUG
! 28356: /* If we are doing a normal write to a database file (as opposed to
! 28357: ** doing a hot-journal rollback or a write to some file other than a
! 28358: ** normal database file) and we truncate the file to zero length,
! 28359: ** that effectively updates the change counter. This might happen
! 28360: ** when restoring a database using the backup API from a zero-length
! 28361: ** source.
! 28362: */
! 28363: if( pFile->inNormalWrite && nByte==0 ){
! 28364: pFile->transCntrChng = 1;
! 28365: }
! 28366: #endif
! 28367:
! 28368: return SQLITE_OK;
! 28369: }
! 28370: }
! 28371:
! 28372: /*
! 28373: ** Determine the current size of a file in bytes
! 28374: */
! 28375: static int unixFileSize(sqlite3_file *id, i64 *pSize){
! 28376: int rc;
! 28377: struct stat buf;
! 28378: assert( id );
! 28379: rc = osFstat(((unixFile*)id)->h, &buf);
! 28380: SimulateIOError( rc=1 );
! 28381: if( rc!=0 ){
! 28382: ((unixFile*)id)->lastErrno = errno;
! 28383: return SQLITE_IOERR_FSTAT;
! 28384: }
! 28385: *pSize = buf.st_size;
! 28386:
! 28387: /* When opening a zero-size database, the findInodeInfo() procedure
! 28388: ** writes a single byte into that file in order to work around a bug
! 28389: ** in the OS-X msdos filesystem. In order to avoid problems with upper
! 28390: ** layers, we need to report this file size as zero even though it is
! 28391: ** really 1. Ticket #3260.
! 28392: */
! 28393: if( *pSize==1 ) *pSize = 0;
! 28394:
! 28395:
! 28396: return SQLITE_OK;
! 28397: }
! 28398:
! 28399: #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
! 28400: /*
! 28401: ** Handler for proxy-locking file-control verbs. Defined below in the
! 28402: ** proxying locking division.
! 28403: */
! 28404: static int proxyFileControl(sqlite3_file*,int,void*);
! 28405: #endif
! 28406:
! 28407: /*
! 28408: ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
! 28409: ** file-control operation. Enlarge the database to nBytes in size
! 28410: ** (rounded up to the next chunk-size). If the database is already
! 28411: ** nBytes or larger, this routine is a no-op.
! 28412: */
! 28413: static int fcntlSizeHint(unixFile *pFile, i64 nByte){
! 28414: if( pFile->szChunk>0 ){
! 28415: i64 nSize; /* Required file size */
! 28416: struct stat buf; /* Used to hold return values of fstat() */
! 28417:
! 28418: if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
! 28419:
! 28420: nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
! 28421: if( nSize>(i64)buf.st_size ){
! 28422:
! 28423: #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
! 28424: /* The code below is handling the return value of osFallocate()
! 28425: ** correctly. posix_fallocate() is defined to "returns zero on success,
! 28426: ** or an error number on failure". See the manpage for details. */
! 28427: int err;
! 28428: do{
! 28429: err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
! 28430: }while( err==EINTR );
! 28431: if( err ) return SQLITE_IOERR_WRITE;
! 28432: #else
! 28433: /* If the OS does not have posix_fallocate(), fake it. First use
! 28434: ** ftruncate() to set the file size, then write a single byte to
! 28435: ** the last byte in each block within the extended region. This
! 28436: ** is the same technique used by glibc to implement posix_fallocate()
! 28437: ** on systems that do not have a real fallocate() system call.
! 28438: */
! 28439: int nBlk = buf.st_blksize; /* File-system block size */
! 28440: i64 iWrite; /* Next offset to write to */
! 28441:
! 28442: if( robust_ftruncate(pFile->h, nSize) ){
! 28443: pFile->lastErrno = errno;
! 28444: return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
! 28445: }
! 28446: iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
! 28447: while( iWrite<nSize ){
! 28448: int nWrite = seekAndWrite(pFile, iWrite, "", 1);
! 28449: if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
! 28450: iWrite += nBlk;
! 28451: }
! 28452: #endif
! 28453: }
! 28454: }
! 28455:
! 28456: return SQLITE_OK;
! 28457: }
! 28458:
! 28459: /*
! 28460: ** If *pArg is inititially negative then this is a query. Set *pArg to
! 28461: ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
! 28462: **
! 28463: ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
! 28464: */
! 28465: static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
! 28466: if( *pArg<0 ){
! 28467: *pArg = (pFile->ctrlFlags & mask)!=0;
! 28468: }else if( (*pArg)==0 ){
! 28469: pFile->ctrlFlags &= ~mask;
! 28470: }else{
! 28471: pFile->ctrlFlags |= mask;
! 28472: }
! 28473: }
! 28474:
! 28475: /*
! 28476: ** Information and control of an open file handle.
! 28477: */
! 28478: static int unixFileControl(sqlite3_file *id, int op, void *pArg){
! 28479: unixFile *pFile = (unixFile*)id;
! 28480: switch( op ){
! 28481: case SQLITE_FCNTL_LOCKSTATE: {
! 28482: *(int*)pArg = pFile->eFileLock;
! 28483: return SQLITE_OK;
! 28484: }
! 28485: case SQLITE_LAST_ERRNO: {
! 28486: *(int*)pArg = pFile->lastErrno;
! 28487: return SQLITE_OK;
! 28488: }
! 28489: case SQLITE_FCNTL_CHUNK_SIZE: {
! 28490: pFile->szChunk = *(int *)pArg;
! 28491: return SQLITE_OK;
! 28492: }
! 28493: case SQLITE_FCNTL_SIZE_HINT: {
! 28494: int rc;
! 28495: SimulateIOErrorBenign(1);
! 28496: rc = fcntlSizeHint(pFile, *(i64 *)pArg);
! 28497: SimulateIOErrorBenign(0);
! 28498: return rc;
! 28499: }
! 28500: case SQLITE_FCNTL_PERSIST_WAL: {
! 28501: unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
! 28502: return SQLITE_OK;
! 28503: }
! 28504: case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
! 28505: unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
! 28506: return SQLITE_OK;
! 28507: }
! 28508: case SQLITE_FCNTL_VFSNAME: {
! 28509: *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
! 28510: return SQLITE_OK;
! 28511: }
! 28512: #ifndef NDEBUG
! 28513: /* The pager calls this method to signal that it has done
! 28514: ** a rollback and that the database is therefore unchanged and
! 28515: ** it hence it is OK for the transaction change counter to be
! 28516: ** unchanged.
! 28517: */
! 28518: case SQLITE_FCNTL_DB_UNCHANGED: {
! 28519: ((unixFile*)id)->dbUpdate = 0;
! 28520: return SQLITE_OK;
! 28521: }
! 28522: #endif
! 28523: #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
! 28524: case SQLITE_SET_LOCKPROXYFILE:
! 28525: case SQLITE_GET_LOCKPROXYFILE: {
! 28526: return proxyFileControl(id,op,pArg);
! 28527: }
! 28528: #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
! 28529: }
! 28530: return SQLITE_NOTFOUND;
! 28531: }
! 28532:
! 28533: /*
! 28534: ** Return the sector size in bytes of the underlying block device for
! 28535: ** the specified file. This is almost always 512 bytes, but may be
! 28536: ** larger for some devices.
! 28537: **
! 28538: ** SQLite code assumes this function cannot fail. It also assumes that
! 28539: ** if two files are created in the same file-system directory (i.e.
! 28540: ** a database and its journal file) that the sector size will be the
! 28541: ** same for both.
! 28542: */
! 28543: static int unixSectorSize(sqlite3_file *pFile){
! 28544: (void)pFile;
! 28545: return SQLITE_DEFAULT_SECTOR_SIZE;
! 28546: }
! 28547:
! 28548: /*
! 28549: ** Return the device characteristics for the file.
! 28550: **
! 28551: ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
! 28552: ** However, that choice is contraversial since technically the underlying
! 28553: ** file system does not always provide powersafe overwrites. (In other
! 28554: ** words, after a power-loss event, parts of the file that were never
! 28555: ** written might end up being altered.) However, non-PSOW behavior is very,
! 28556: ** very rare. And asserting PSOW makes a large reduction in the amount
! 28557: ** of required I/O for journaling, since a lot of padding is eliminated.
! 28558: ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
! 28559: ** available to turn it off and URI query parameter available to turn it off.
! 28560: */
! 28561: static int unixDeviceCharacteristics(sqlite3_file *id){
! 28562: unixFile *p = (unixFile*)id;
! 28563: if( p->ctrlFlags & UNIXFILE_PSOW ){
! 28564: return SQLITE_IOCAP_POWERSAFE_OVERWRITE;
! 28565: }else{
! 28566: return 0;
! 28567: }
! 28568: }
! 28569:
! 28570: #ifndef SQLITE_OMIT_WAL
! 28571:
! 28572:
! 28573: /*
! 28574: ** Object used to represent an shared memory buffer.
! 28575: **
! 28576: ** When multiple threads all reference the same wal-index, each thread
! 28577: ** has its own unixShm object, but they all point to a single instance
! 28578: ** of this unixShmNode object. In other words, each wal-index is opened
! 28579: ** only once per process.
! 28580: **
! 28581: ** Each unixShmNode object is connected to a single unixInodeInfo object.
! 28582: ** We could coalesce this object into unixInodeInfo, but that would mean
! 28583: ** every open file that does not use shared memory (in other words, most
! 28584: ** open files) would have to carry around this extra information. So
! 28585: ** the unixInodeInfo object contains a pointer to this unixShmNode object
! 28586: ** and the unixShmNode object is created only when needed.
! 28587: **
! 28588: ** unixMutexHeld() must be true when creating or destroying
! 28589: ** this object or while reading or writing the following fields:
! 28590: **
! 28591: ** nRef
! 28592: **
! 28593: ** The following fields are read-only after the object is created:
! 28594: **
! 28595: ** fid
! 28596: ** zFilename
! 28597: **
! 28598: ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
! 28599: ** unixMutexHeld() is true when reading or writing any other field
! 28600: ** in this structure.
! 28601: */
! 28602: struct unixShmNode {
! 28603: unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
! 28604: sqlite3_mutex *mutex; /* Mutex to access this object */
! 28605: char *zFilename; /* Name of the mmapped file */
! 28606: int h; /* Open file descriptor */
! 28607: int szRegion; /* Size of shared-memory regions */
! 28608: u16 nRegion; /* Size of array apRegion */
! 28609: u8 isReadonly; /* True if read-only */
! 28610: char **apRegion; /* Array of mapped shared-memory regions */
! 28611: int nRef; /* Number of unixShm objects pointing to this */
! 28612: unixShm *pFirst; /* All unixShm objects pointing to this */
! 28613: #ifdef SQLITE_DEBUG
! 28614: u8 exclMask; /* Mask of exclusive locks held */
! 28615: u8 sharedMask; /* Mask of shared locks held */
! 28616: u8 nextShmId; /* Next available unixShm.id value */
! 28617: #endif
! 28618: };
! 28619:
! 28620: /*
! 28621: ** Structure used internally by this VFS to record the state of an
! 28622: ** open shared memory connection.
! 28623: **
! 28624: ** The following fields are initialized when this object is created and
! 28625: ** are read-only thereafter:
! 28626: **
! 28627: ** unixShm.pFile
! 28628: ** unixShm.id
! 28629: **
! 28630: ** All other fields are read/write. The unixShm.pFile->mutex must be held
! 28631: ** while accessing any read/write fields.
! 28632: */
! 28633: struct unixShm {
! 28634: unixShmNode *pShmNode; /* The underlying unixShmNode object */
! 28635: unixShm *pNext; /* Next unixShm with the same unixShmNode */
! 28636: u8 hasMutex; /* True if holding the unixShmNode mutex */
! 28637: u8 id; /* Id of this connection within its unixShmNode */
! 28638: u16 sharedMask; /* Mask of shared locks held */
! 28639: u16 exclMask; /* Mask of exclusive locks held */
! 28640: };
! 28641:
! 28642: /*
! 28643: ** Constants used for locking
! 28644: */
! 28645: #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
! 28646: #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
! 28647:
! 28648: /*
! 28649: ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
! 28650: **
! 28651: ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
! 28652: ** otherwise.
! 28653: */
! 28654: static int unixShmSystemLock(
! 28655: unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
! 28656: int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
! 28657: int ofst, /* First byte of the locking range */
! 28658: int n /* Number of bytes to lock */
! 28659: ){
! 28660: struct flock f; /* The posix advisory locking structure */
! 28661: int rc = SQLITE_OK; /* Result code form fcntl() */
! 28662:
! 28663: /* Access to the unixShmNode object is serialized by the caller */
! 28664: assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
! 28665:
! 28666: /* Shared locks never span more than one byte */
! 28667: assert( n==1 || lockType!=F_RDLCK );
! 28668:
! 28669: /* Locks are within range */
! 28670: assert( n>=1 && n<SQLITE_SHM_NLOCK );
! 28671:
! 28672: if( pShmNode->h>=0 ){
! 28673: /* Initialize the locking parameters */
! 28674: memset(&f, 0, sizeof(f));
! 28675: f.l_type = lockType;
! 28676: f.l_whence = SEEK_SET;
! 28677: f.l_start = ofst;
! 28678: f.l_len = n;
! 28679:
! 28680: rc = osFcntl(pShmNode->h, F_SETLK, &f);
! 28681: rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
! 28682: }
! 28683:
! 28684: /* Update the global lock state and do debug tracing */
! 28685: #ifdef SQLITE_DEBUG
! 28686: { u16 mask;
! 28687: OSTRACE(("SHM-LOCK "));
! 28688: mask = (1<<(ofst+n)) - (1<<ofst);
! 28689: if( rc==SQLITE_OK ){
! 28690: if( lockType==F_UNLCK ){
! 28691: OSTRACE(("unlock %d ok", ofst));
! 28692: pShmNode->exclMask &= ~mask;
! 28693: pShmNode->sharedMask &= ~mask;
! 28694: }else if( lockType==F_RDLCK ){
! 28695: OSTRACE(("read-lock %d ok", ofst));
! 28696: pShmNode->exclMask &= ~mask;
! 28697: pShmNode->sharedMask |= mask;
! 28698: }else{
! 28699: assert( lockType==F_WRLCK );
! 28700: OSTRACE(("write-lock %d ok", ofst));
! 28701: pShmNode->exclMask |= mask;
! 28702: pShmNode->sharedMask &= ~mask;
! 28703: }
! 28704: }else{
! 28705: if( lockType==F_UNLCK ){
! 28706: OSTRACE(("unlock %d failed", ofst));
! 28707: }else if( lockType==F_RDLCK ){
! 28708: OSTRACE(("read-lock failed"));
! 28709: }else{
! 28710: assert( lockType==F_WRLCK );
! 28711: OSTRACE(("write-lock %d failed", ofst));
! 28712: }
! 28713: }
! 28714: OSTRACE((" - afterwards %03x,%03x\n",
! 28715: pShmNode->sharedMask, pShmNode->exclMask));
! 28716: }
! 28717: #endif
! 28718:
! 28719: return rc;
! 28720: }
! 28721:
! 28722:
! 28723: /*
! 28724: ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
! 28725: **
! 28726: ** This is not a VFS shared-memory method; it is a utility function called
! 28727: ** by VFS shared-memory methods.
! 28728: */
! 28729: static void unixShmPurge(unixFile *pFd){
! 28730: unixShmNode *p = pFd->pInode->pShmNode;
! 28731: assert( unixMutexHeld() );
! 28732: if( p && p->nRef==0 ){
! 28733: int i;
! 28734: assert( p->pInode==pFd->pInode );
! 28735: sqlite3_mutex_free(p->mutex);
! 28736: for(i=0; i<p->nRegion; i++){
! 28737: if( p->h>=0 ){
! 28738: munmap(p->apRegion[i], p->szRegion);
! 28739: }else{
! 28740: sqlite3_free(p->apRegion[i]);
! 28741: }
! 28742: }
! 28743: sqlite3_free(p->apRegion);
! 28744: if( p->h>=0 ){
! 28745: robust_close(pFd, p->h, __LINE__);
! 28746: p->h = -1;
! 28747: }
! 28748: p->pInode->pShmNode = 0;
! 28749: sqlite3_free(p);
! 28750: }
! 28751: }
! 28752:
! 28753: /*
! 28754: ** Open a shared-memory area associated with open database file pDbFd.
! 28755: ** This particular implementation uses mmapped files.
! 28756: **
! 28757: ** The file used to implement shared-memory is in the same directory
! 28758: ** as the open database file and has the same name as the open database
! 28759: ** file with the "-shm" suffix added. For example, if the database file
! 28760: ** is "/home/user1/config.db" then the file that is created and mmapped
! 28761: ** for shared memory will be called "/home/user1/config.db-shm".
! 28762: **
! 28763: ** Another approach to is to use files in /dev/shm or /dev/tmp or an
! 28764: ** some other tmpfs mount. But if a file in a different directory
! 28765: ** from the database file is used, then differing access permissions
! 28766: ** or a chroot() might cause two different processes on the same
! 28767: ** database to end up using different files for shared memory -
! 28768: ** meaning that their memory would not really be shared - resulting
! 28769: ** in database corruption. Nevertheless, this tmpfs file usage
! 28770: ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
! 28771: ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
! 28772: ** option results in an incompatible build of SQLite; builds of SQLite
! 28773: ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
! 28774: ** same database file at the same time, database corruption will likely
! 28775: ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
! 28776: ** "unsupported" and may go away in a future SQLite release.
! 28777: **
! 28778: ** When opening a new shared-memory file, if no other instances of that
! 28779: ** file are currently open, in this process or in other processes, then
! 28780: ** the file must be truncated to zero length or have its header cleared.
! 28781: **
! 28782: ** If the original database file (pDbFd) is using the "unix-excl" VFS
! 28783: ** that means that an exclusive lock is held on the database file and
! 28784: ** that no other processes are able to read or write the database. In
! 28785: ** that case, we do not really need shared memory. No shared memory
! 28786: ** file is created. The shared memory will be simulated with heap memory.
! 28787: */
! 28788: static int unixOpenSharedMemory(unixFile *pDbFd){
! 28789: struct unixShm *p = 0; /* The connection to be opened */
! 28790: struct unixShmNode *pShmNode; /* The underlying mmapped file */
! 28791: int rc; /* Result code */
! 28792: unixInodeInfo *pInode; /* The inode of fd */
! 28793: char *zShmFilename; /* Name of the file used for SHM */
! 28794: int nShmFilename; /* Size of the SHM filename in bytes */
! 28795:
! 28796: /* Allocate space for the new unixShm object. */
! 28797: p = sqlite3_malloc( sizeof(*p) );
! 28798: if( p==0 ) return SQLITE_NOMEM;
! 28799: memset(p, 0, sizeof(*p));
! 28800: assert( pDbFd->pShm==0 );
! 28801:
! 28802: /* Check to see if a unixShmNode object already exists. Reuse an existing
! 28803: ** one if present. Create a new one if necessary.
! 28804: */
! 28805: unixEnterMutex();
! 28806: pInode = pDbFd->pInode;
! 28807: pShmNode = pInode->pShmNode;
! 28808: if( pShmNode==0 ){
! 28809: struct stat sStat; /* fstat() info for database file */
! 28810:
! 28811: /* Call fstat() to figure out the permissions on the database file. If
! 28812: ** a new *-shm file is created, an attempt will be made to create it
! 28813: ** with the same permissions. The actual permissions the file is created
! 28814: ** with are subject to the current umask setting.
! 28815: */
! 28816: if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
! 28817: rc = SQLITE_IOERR_FSTAT;
! 28818: goto shm_open_err;
! 28819: }
! 28820:
! 28821: #ifdef SQLITE_SHM_DIRECTORY
! 28822: nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
! 28823: #else
! 28824: nShmFilename = 6 + (int)strlen(pDbFd->zPath);
! 28825: #endif
! 28826: pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
! 28827: if( pShmNode==0 ){
! 28828: rc = SQLITE_NOMEM;
! 28829: goto shm_open_err;
! 28830: }
! 28831: memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
! 28832: zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
! 28833: #ifdef SQLITE_SHM_DIRECTORY
! 28834: sqlite3_snprintf(nShmFilename, zShmFilename,
! 28835: SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
! 28836: (u32)sStat.st_ino, (u32)sStat.st_dev);
! 28837: #else
! 28838: sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath);
! 28839: sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
! 28840: #endif
! 28841: pShmNode->h = -1;
! 28842: pDbFd->pInode->pShmNode = pShmNode;
! 28843: pShmNode->pInode = pDbFd->pInode;
! 28844: pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
! 28845: if( pShmNode->mutex==0 ){
! 28846: rc = SQLITE_NOMEM;
! 28847: goto shm_open_err;
! 28848: }
! 28849:
! 28850: if( pInode->bProcessLock==0 ){
! 28851: int openFlags = O_RDWR | O_CREAT;
! 28852: if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
! 28853: openFlags = O_RDONLY;
! 28854: pShmNode->isReadonly = 1;
! 28855: }
! 28856: pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
! 28857: if( pShmNode->h<0 ){
! 28858: if( pShmNode->h<0 ){
! 28859: rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
! 28860: goto shm_open_err;
! 28861: }
! 28862: }
! 28863:
! 28864: /* Check to see if another process is holding the dead-man switch.
! 28865: ** If not, truncate the file to zero length.
! 28866: */
! 28867: rc = SQLITE_OK;
! 28868: if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
! 28869: if( robust_ftruncate(pShmNode->h, 0) ){
! 28870: rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
! 28871: }
! 28872: }
! 28873: if( rc==SQLITE_OK ){
! 28874: rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
! 28875: }
! 28876: if( rc ) goto shm_open_err;
! 28877: }
! 28878: }
! 28879:
! 28880: /* Make the new connection a child of the unixShmNode */
! 28881: p->pShmNode = pShmNode;
! 28882: #ifdef SQLITE_DEBUG
! 28883: p->id = pShmNode->nextShmId++;
! 28884: #endif
! 28885: pShmNode->nRef++;
! 28886: pDbFd->pShm = p;
! 28887: unixLeaveMutex();
! 28888:
! 28889: /* The reference count on pShmNode has already been incremented under
! 28890: ** the cover of the unixEnterMutex() mutex and the pointer from the
! 28891: ** new (struct unixShm) object to the pShmNode has been set. All that is
! 28892: ** left to do is to link the new object into the linked list starting
! 28893: ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
! 28894: ** mutex.
! 28895: */
! 28896: sqlite3_mutex_enter(pShmNode->mutex);
! 28897: p->pNext = pShmNode->pFirst;
! 28898: pShmNode->pFirst = p;
! 28899: sqlite3_mutex_leave(pShmNode->mutex);
! 28900: return SQLITE_OK;
! 28901:
! 28902: /* Jump here on any error */
! 28903: shm_open_err:
! 28904: unixShmPurge(pDbFd); /* This call frees pShmNode if required */
! 28905: sqlite3_free(p);
! 28906: unixLeaveMutex();
! 28907: return rc;
! 28908: }
! 28909:
! 28910: /*
! 28911: ** This function is called to obtain a pointer to region iRegion of the
! 28912: ** shared-memory associated with the database file fd. Shared-memory regions
! 28913: ** are numbered starting from zero. Each shared-memory region is szRegion
! 28914: ** bytes in size.
! 28915: **
! 28916: ** If an error occurs, an error code is returned and *pp is set to NULL.
! 28917: **
! 28918: ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
! 28919: ** region has not been allocated (by any client, including one running in a
! 28920: ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
! 28921: ** bExtend is non-zero and the requested shared-memory region has not yet
! 28922: ** been allocated, it is allocated by this function.
! 28923: **
! 28924: ** If the shared-memory region has already been allocated or is allocated by
! 28925: ** this call as described above, then it is mapped into this processes
! 28926: ** address space (if it is not already), *pp is set to point to the mapped
! 28927: ** memory and SQLITE_OK returned.
! 28928: */
! 28929: static int unixShmMap(
! 28930: sqlite3_file *fd, /* Handle open on database file */
! 28931: int iRegion, /* Region to retrieve */
! 28932: int szRegion, /* Size of regions */
! 28933: int bExtend, /* True to extend file if necessary */
! 28934: void volatile **pp /* OUT: Mapped memory */
! 28935: ){
! 28936: unixFile *pDbFd = (unixFile*)fd;
! 28937: unixShm *p;
! 28938: unixShmNode *pShmNode;
! 28939: int rc = SQLITE_OK;
! 28940:
! 28941: /* If the shared-memory file has not yet been opened, open it now. */
! 28942: if( pDbFd->pShm==0 ){
! 28943: rc = unixOpenSharedMemory(pDbFd);
! 28944: if( rc!=SQLITE_OK ) return rc;
! 28945: }
! 28946:
! 28947: p = pDbFd->pShm;
! 28948: pShmNode = p->pShmNode;
! 28949: sqlite3_mutex_enter(pShmNode->mutex);
! 28950: assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
! 28951: assert( pShmNode->pInode==pDbFd->pInode );
! 28952: assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
! 28953: assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
! 28954:
! 28955: if( pShmNode->nRegion<=iRegion ){
! 28956: char **apNew; /* New apRegion[] array */
! 28957: int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
! 28958: struct stat sStat; /* Used by fstat() */
! 28959:
! 28960: pShmNode->szRegion = szRegion;
! 28961:
! 28962: if( pShmNode->h>=0 ){
! 28963: /* The requested region is not mapped into this processes address space.
! 28964: ** Check to see if it has been allocated (i.e. if the wal-index file is
! 28965: ** large enough to contain the requested region).
! 28966: */
! 28967: if( osFstat(pShmNode->h, &sStat) ){
! 28968: rc = SQLITE_IOERR_SHMSIZE;
! 28969: goto shmpage_out;
! 28970: }
! 28971:
! 28972: if( sStat.st_size<nByte ){
! 28973: /* The requested memory region does not exist. If bExtend is set to
! 28974: ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
! 28975: **
! 28976: ** Alternatively, if bExtend is true, use ftruncate() to allocate
! 28977: ** the requested memory region.
! 28978: */
! 28979: if( !bExtend ) goto shmpage_out;
! 28980: if( robust_ftruncate(pShmNode->h, nByte) ){
! 28981: rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate",
! 28982: pShmNode->zFilename);
! 28983: goto shmpage_out;
! 28984: }
! 28985: }
! 28986: }
! 28987:
! 28988: /* Map the requested memory region into this processes address space. */
! 28989: apNew = (char **)sqlite3_realloc(
! 28990: pShmNode->apRegion, (iRegion+1)*sizeof(char *)
! 28991: );
! 28992: if( !apNew ){
! 28993: rc = SQLITE_IOERR_NOMEM;
! 28994: goto shmpage_out;
! 28995: }
! 28996: pShmNode->apRegion = apNew;
! 28997: while(pShmNode->nRegion<=iRegion){
! 28998: void *pMem;
! 28999: if( pShmNode->h>=0 ){
! 29000: pMem = mmap(0, szRegion,
! 29001: pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
! 29002: MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion
! 29003: );
! 29004: if( pMem==MAP_FAILED ){
! 29005: rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
! 29006: goto shmpage_out;
! 29007: }
! 29008: }else{
! 29009: pMem = sqlite3_malloc(szRegion);
! 29010: if( pMem==0 ){
! 29011: rc = SQLITE_NOMEM;
! 29012: goto shmpage_out;
! 29013: }
! 29014: memset(pMem, 0, szRegion);
! 29015: }
! 29016: pShmNode->apRegion[pShmNode->nRegion] = pMem;
! 29017: pShmNode->nRegion++;
! 29018: }
! 29019: }
! 29020:
! 29021: shmpage_out:
! 29022: if( pShmNode->nRegion>iRegion ){
! 29023: *pp = pShmNode->apRegion[iRegion];
! 29024: }else{
! 29025: *pp = 0;
! 29026: }
! 29027: if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
! 29028: sqlite3_mutex_leave(pShmNode->mutex);
! 29029: return rc;
! 29030: }
! 29031:
! 29032: /*
! 29033: ** Change the lock state for a shared-memory segment.
! 29034: **
! 29035: ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
! 29036: ** different here than in posix. In xShmLock(), one can go from unlocked
! 29037: ** to shared and back or from unlocked to exclusive and back. But one may
! 29038: ** not go from shared to exclusive or from exclusive to shared.
! 29039: */
! 29040: static int unixShmLock(
! 29041: sqlite3_file *fd, /* Database file holding the shared memory */
! 29042: int ofst, /* First lock to acquire or release */
! 29043: int n, /* Number of locks to acquire or release */
! 29044: int flags /* What to do with the lock */
! 29045: ){
! 29046: unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
! 29047: unixShm *p = pDbFd->pShm; /* The shared memory being locked */
! 29048: unixShm *pX; /* For looping over all siblings */
! 29049: unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
! 29050: int rc = SQLITE_OK; /* Result code */
! 29051: u16 mask; /* Mask of locks to take or release */
! 29052:
! 29053: assert( pShmNode==pDbFd->pInode->pShmNode );
! 29054: assert( pShmNode->pInode==pDbFd->pInode );
! 29055: assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
! 29056: assert( n>=1 );
! 29057: assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
! 29058: || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
! 29059: || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
! 29060: || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
! 29061: assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
! 29062: assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
! 29063: assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
! 29064:
! 29065: mask = (1<<(ofst+n)) - (1<<ofst);
! 29066: assert( n>1 || mask==(1<<ofst) );
! 29067: sqlite3_mutex_enter(pShmNode->mutex);
! 29068: if( flags & SQLITE_SHM_UNLOCK ){
! 29069: u16 allMask = 0; /* Mask of locks held by siblings */
! 29070:
! 29071: /* See if any siblings hold this same lock */
! 29072: for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
! 29073: if( pX==p ) continue;
! 29074: assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
! 29075: allMask |= pX->sharedMask;
! 29076: }
! 29077:
! 29078: /* Unlock the system-level locks */
! 29079: if( (mask & allMask)==0 ){
! 29080: rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
! 29081: }else{
! 29082: rc = SQLITE_OK;
! 29083: }
! 29084:
! 29085: /* Undo the local locks */
! 29086: if( rc==SQLITE_OK ){
! 29087: p->exclMask &= ~mask;
! 29088: p->sharedMask &= ~mask;
! 29089: }
! 29090: }else if( flags & SQLITE_SHM_SHARED ){
! 29091: u16 allShared = 0; /* Union of locks held by connections other than "p" */
! 29092:
! 29093: /* Find out which shared locks are already held by sibling connections.
! 29094: ** If any sibling already holds an exclusive lock, go ahead and return
! 29095: ** SQLITE_BUSY.
! 29096: */
! 29097: for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
! 29098: if( (pX->exclMask & mask)!=0 ){
! 29099: rc = SQLITE_BUSY;
! 29100: break;
! 29101: }
! 29102: allShared |= pX->sharedMask;
! 29103: }
! 29104:
! 29105: /* Get shared locks at the system level, if necessary */
! 29106: if( rc==SQLITE_OK ){
! 29107: if( (allShared & mask)==0 ){
! 29108: rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
! 29109: }else{
! 29110: rc = SQLITE_OK;
! 29111: }
! 29112: }
! 29113:
! 29114: /* Get the local shared locks */
! 29115: if( rc==SQLITE_OK ){
! 29116: p->sharedMask |= mask;
! 29117: }
! 29118: }else{
! 29119: /* Make sure no sibling connections hold locks that will block this
! 29120: ** lock. If any do, return SQLITE_BUSY right away.
! 29121: */
! 29122: for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
! 29123: if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
! 29124: rc = SQLITE_BUSY;
! 29125: break;
! 29126: }
! 29127: }
! 29128:
! 29129: /* Get the exclusive locks at the system level. Then if successful
! 29130: ** also mark the local connection as being locked.
! 29131: */
! 29132: if( rc==SQLITE_OK ){
! 29133: rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
! 29134: if( rc==SQLITE_OK ){
! 29135: assert( (p->sharedMask & mask)==0 );
! 29136: p->exclMask |= mask;
! 29137: }
! 29138: }
! 29139: }
! 29140: sqlite3_mutex_leave(pShmNode->mutex);
! 29141: OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
! 29142: p->id, getpid(), p->sharedMask, p->exclMask));
! 29143: return rc;
! 29144: }
! 29145:
! 29146: /*
! 29147: ** Implement a memory barrier or memory fence on shared memory.
! 29148: **
! 29149: ** All loads and stores begun before the barrier must complete before
! 29150: ** any load or store begun after the barrier.
! 29151: */
! 29152: static void unixShmBarrier(
! 29153: sqlite3_file *fd /* Database file holding the shared memory */
! 29154: ){
! 29155: UNUSED_PARAMETER(fd);
! 29156: unixEnterMutex();
! 29157: unixLeaveMutex();
! 29158: }
! 29159:
! 29160: /*
! 29161: ** Close a connection to shared-memory. Delete the underlying
! 29162: ** storage if deleteFlag is true.
! 29163: **
! 29164: ** If there is no shared memory associated with the connection then this
! 29165: ** routine is a harmless no-op.
! 29166: */
! 29167: static int unixShmUnmap(
! 29168: sqlite3_file *fd, /* The underlying database file */
! 29169: int deleteFlag /* Delete shared-memory if true */
! 29170: ){
! 29171: unixShm *p; /* The connection to be closed */
! 29172: unixShmNode *pShmNode; /* The underlying shared-memory file */
! 29173: unixShm **pp; /* For looping over sibling connections */
! 29174: unixFile *pDbFd; /* The underlying database file */
! 29175:
! 29176: pDbFd = (unixFile*)fd;
! 29177: p = pDbFd->pShm;
! 29178: if( p==0 ) return SQLITE_OK;
! 29179: pShmNode = p->pShmNode;
! 29180:
! 29181: assert( pShmNode==pDbFd->pInode->pShmNode );
! 29182: assert( pShmNode->pInode==pDbFd->pInode );
! 29183:
! 29184: /* Remove connection p from the set of connections associated
! 29185: ** with pShmNode */
! 29186: sqlite3_mutex_enter(pShmNode->mutex);
! 29187: for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
! 29188: *pp = p->pNext;
! 29189:
! 29190: /* Free the connection p */
! 29191: sqlite3_free(p);
! 29192: pDbFd->pShm = 0;
! 29193: sqlite3_mutex_leave(pShmNode->mutex);
! 29194:
! 29195: /* If pShmNode->nRef has reached 0, then close the underlying
! 29196: ** shared-memory file, too */
! 29197: unixEnterMutex();
! 29198: assert( pShmNode->nRef>0 );
! 29199: pShmNode->nRef--;
! 29200: if( pShmNode->nRef==0 ){
! 29201: if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
! 29202: unixShmPurge(pDbFd);
! 29203: }
! 29204: unixLeaveMutex();
! 29205:
! 29206: return SQLITE_OK;
! 29207: }
! 29208:
! 29209:
! 29210: #else
! 29211: # define unixShmMap 0
! 29212: # define unixShmLock 0
! 29213: # define unixShmBarrier 0
! 29214: # define unixShmUnmap 0
! 29215: #endif /* #ifndef SQLITE_OMIT_WAL */
! 29216:
! 29217: /*
! 29218: ** Here ends the implementation of all sqlite3_file methods.
! 29219: **
! 29220: ********************** End sqlite3_file Methods *******************************
! 29221: ******************************************************************************/
! 29222:
! 29223: /*
! 29224: ** This division contains definitions of sqlite3_io_methods objects that
! 29225: ** implement various file locking strategies. It also contains definitions
! 29226: ** of "finder" functions. A finder-function is used to locate the appropriate
! 29227: ** sqlite3_io_methods object for a particular database file. The pAppData
! 29228: ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
! 29229: ** the correct finder-function for that VFS.
! 29230: **
! 29231: ** Most finder functions return a pointer to a fixed sqlite3_io_methods
! 29232: ** object. The only interesting finder-function is autolockIoFinder, which
! 29233: ** looks at the filesystem type and tries to guess the best locking
! 29234: ** strategy from that.
! 29235: **
! 29236: ** For finder-funtion F, two objects are created:
! 29237: **
! 29238: ** (1) The real finder-function named "FImpt()".
! 29239: **
! 29240: ** (2) A constant pointer to this function named just "F".
! 29241: **
! 29242: **
! 29243: ** A pointer to the F pointer is used as the pAppData value for VFS
! 29244: ** objects. We have to do this instead of letting pAppData point
! 29245: ** directly at the finder-function since C90 rules prevent a void*
! 29246: ** from be cast into a function pointer.
! 29247: **
! 29248: **
! 29249: ** Each instance of this macro generates two objects:
! 29250: **
! 29251: ** * A constant sqlite3_io_methods object call METHOD that has locking
! 29252: ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
! 29253: **
! 29254: ** * An I/O method finder function called FINDER that returns a pointer
! 29255: ** to the METHOD object in the previous bullet.
! 29256: */
! 29257: #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \
! 29258: static const sqlite3_io_methods METHOD = { \
! 29259: VERSION, /* iVersion */ \
! 29260: CLOSE, /* xClose */ \
! 29261: unixRead, /* xRead */ \
! 29262: unixWrite, /* xWrite */ \
! 29263: unixTruncate, /* xTruncate */ \
! 29264: unixSync, /* xSync */ \
! 29265: unixFileSize, /* xFileSize */ \
! 29266: LOCK, /* xLock */ \
! 29267: UNLOCK, /* xUnlock */ \
! 29268: CKLOCK, /* xCheckReservedLock */ \
! 29269: unixFileControl, /* xFileControl */ \
! 29270: unixSectorSize, /* xSectorSize */ \
! 29271: unixDeviceCharacteristics, /* xDeviceCapabilities */ \
! 29272: unixShmMap, /* xShmMap */ \
! 29273: unixShmLock, /* xShmLock */ \
! 29274: unixShmBarrier, /* xShmBarrier */ \
! 29275: unixShmUnmap /* xShmUnmap */ \
! 29276: }; \
! 29277: static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
! 29278: UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
! 29279: return &METHOD; \
! 29280: } \
! 29281: static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
! 29282: = FINDER##Impl;
! 29283:
! 29284: /*
! 29285: ** Here are all of the sqlite3_io_methods objects for each of the
! 29286: ** locking strategies. Functions that return pointers to these methods
! 29287: ** are also created.
! 29288: */
! 29289: IOMETHODS(
! 29290: posixIoFinder, /* Finder function name */
! 29291: posixIoMethods, /* sqlite3_io_methods object name */
! 29292: 2, /* shared memory is enabled */
! 29293: unixClose, /* xClose method */
! 29294: unixLock, /* xLock method */
! 29295: unixUnlock, /* xUnlock method */
! 29296: unixCheckReservedLock /* xCheckReservedLock method */
! 29297: )
! 29298: IOMETHODS(
! 29299: nolockIoFinder, /* Finder function name */
! 29300: nolockIoMethods, /* sqlite3_io_methods object name */
! 29301: 1, /* shared memory is disabled */
! 29302: nolockClose, /* xClose method */
! 29303: nolockLock, /* xLock method */
! 29304: nolockUnlock, /* xUnlock method */
! 29305: nolockCheckReservedLock /* xCheckReservedLock method */
! 29306: )
! 29307: IOMETHODS(
! 29308: dotlockIoFinder, /* Finder function name */
! 29309: dotlockIoMethods, /* sqlite3_io_methods object name */
! 29310: 1, /* shared memory is disabled */
! 29311: dotlockClose, /* xClose method */
! 29312: dotlockLock, /* xLock method */
! 29313: dotlockUnlock, /* xUnlock method */
! 29314: dotlockCheckReservedLock /* xCheckReservedLock method */
! 29315: )
! 29316:
! 29317: #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
! 29318: IOMETHODS(
! 29319: flockIoFinder, /* Finder function name */
! 29320: flockIoMethods, /* sqlite3_io_methods object name */
! 29321: 1, /* shared memory is disabled */
! 29322: flockClose, /* xClose method */
! 29323: flockLock, /* xLock method */
! 29324: flockUnlock, /* xUnlock method */
! 29325: flockCheckReservedLock /* xCheckReservedLock method */
! 29326: )
! 29327: #endif
! 29328:
! 29329: #if OS_VXWORKS
! 29330: IOMETHODS(
! 29331: semIoFinder, /* Finder function name */
! 29332: semIoMethods, /* sqlite3_io_methods object name */
! 29333: 1, /* shared memory is disabled */
! 29334: semClose, /* xClose method */
! 29335: semLock, /* xLock method */
! 29336: semUnlock, /* xUnlock method */
! 29337: semCheckReservedLock /* xCheckReservedLock method */
! 29338: )
! 29339: #endif
! 29340:
! 29341: #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
! 29342: IOMETHODS(
! 29343: afpIoFinder, /* Finder function name */
! 29344: afpIoMethods, /* sqlite3_io_methods object name */
! 29345: 1, /* shared memory is disabled */
! 29346: afpClose, /* xClose method */
! 29347: afpLock, /* xLock method */
! 29348: afpUnlock, /* xUnlock method */
! 29349: afpCheckReservedLock /* xCheckReservedLock method */
! 29350: )
! 29351: #endif
! 29352:
! 29353: /*
! 29354: ** The proxy locking method is a "super-method" in the sense that it
! 29355: ** opens secondary file descriptors for the conch and lock files and
! 29356: ** it uses proxy, dot-file, AFP, and flock() locking methods on those
! 29357: ** secondary files. For this reason, the division that implements
! 29358: ** proxy locking is located much further down in the file. But we need
! 29359: ** to go ahead and define the sqlite3_io_methods and finder function
! 29360: ** for proxy locking here. So we forward declare the I/O methods.
! 29361: */
! 29362: #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
! 29363: static int proxyClose(sqlite3_file*);
! 29364: static int proxyLock(sqlite3_file*, int);
! 29365: static int proxyUnlock(sqlite3_file*, int);
! 29366: static int proxyCheckReservedLock(sqlite3_file*, int*);
! 29367: IOMETHODS(
! 29368: proxyIoFinder, /* Finder function name */
! 29369: proxyIoMethods, /* sqlite3_io_methods object name */
! 29370: 1, /* shared memory is disabled */
! 29371: proxyClose, /* xClose method */
! 29372: proxyLock, /* xLock method */
! 29373: proxyUnlock, /* xUnlock method */
! 29374: proxyCheckReservedLock /* xCheckReservedLock method */
! 29375: )
! 29376: #endif
! 29377:
! 29378: /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
! 29379: #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
! 29380: IOMETHODS(
! 29381: nfsIoFinder, /* Finder function name */
! 29382: nfsIoMethods, /* sqlite3_io_methods object name */
! 29383: 1, /* shared memory is disabled */
! 29384: unixClose, /* xClose method */
! 29385: unixLock, /* xLock method */
! 29386: nfsUnlock, /* xUnlock method */
! 29387: unixCheckReservedLock /* xCheckReservedLock method */
! 29388: )
! 29389: #endif
! 29390:
! 29391: #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
! 29392: /*
! 29393: ** This "finder" function attempts to determine the best locking strategy
! 29394: ** for the database file "filePath". It then returns the sqlite3_io_methods
! 29395: ** object that implements that strategy.
! 29396: **
! 29397: ** This is for MacOSX only.
! 29398: */
! 29399: static const sqlite3_io_methods *autolockIoFinderImpl(
! 29400: const char *filePath, /* name of the database file */
! 29401: unixFile *pNew /* open file object for the database file */
! 29402: ){
! 29403: static const struct Mapping {
! 29404: const char *zFilesystem; /* Filesystem type name */
! 29405: const sqlite3_io_methods *pMethods; /* Appropriate locking method */
! 29406: } aMap[] = {
! 29407: { "hfs", &posixIoMethods },
! 29408: { "ufs", &posixIoMethods },
! 29409: { "afpfs", &afpIoMethods },
! 29410: { "smbfs", &afpIoMethods },
! 29411: { "webdav", &nolockIoMethods },
! 29412: { 0, 0 }
! 29413: };
! 29414: int i;
! 29415: struct statfs fsInfo;
! 29416: struct flock lockInfo;
! 29417:
! 29418: if( !filePath ){
! 29419: /* If filePath==NULL that means we are dealing with a transient file
! 29420: ** that does not need to be locked. */
! 29421: return &nolockIoMethods;
! 29422: }
! 29423: if( statfs(filePath, &fsInfo) != -1 ){
! 29424: if( fsInfo.f_flags & MNT_RDONLY ){
! 29425: return &nolockIoMethods;
! 29426: }
! 29427: for(i=0; aMap[i].zFilesystem; i++){
! 29428: if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
! 29429: return aMap[i].pMethods;
! 29430: }
! 29431: }
! 29432: }
! 29433:
! 29434: /* Default case. Handles, amongst others, "nfs".
! 29435: ** Test byte-range lock using fcntl(). If the call succeeds,
! 29436: ** assume that the file-system supports POSIX style locks.
! 29437: */
! 29438: lockInfo.l_len = 1;
! 29439: lockInfo.l_start = 0;
! 29440: lockInfo.l_whence = SEEK_SET;
! 29441: lockInfo.l_type = F_RDLCK;
! 29442: if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
! 29443: if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
! 29444: return &nfsIoMethods;
! 29445: } else {
! 29446: return &posixIoMethods;
! 29447: }
! 29448: }else{
! 29449: return &dotlockIoMethods;
! 29450: }
! 29451: }
! 29452: static const sqlite3_io_methods
! 29453: *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
! 29454:
! 29455: #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
! 29456:
! 29457: #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
! 29458: /*
! 29459: ** This "finder" function attempts to determine the best locking strategy
! 29460: ** for the database file "filePath". It then returns the sqlite3_io_methods
! 29461: ** object that implements that strategy.
! 29462: **
! 29463: ** This is for VXWorks only.
! 29464: */
! 29465: static const sqlite3_io_methods *autolockIoFinderImpl(
! 29466: const char *filePath, /* name of the database file */
! 29467: unixFile *pNew /* the open file object */
! 29468: ){
! 29469: struct flock lockInfo;
! 29470:
! 29471: if( !filePath ){
! 29472: /* If filePath==NULL that means we are dealing with a transient file
! 29473: ** that does not need to be locked. */
! 29474: return &nolockIoMethods;
! 29475: }
! 29476:
! 29477: /* Test if fcntl() is supported and use POSIX style locks.
! 29478: ** Otherwise fall back to the named semaphore method.
! 29479: */
! 29480: lockInfo.l_len = 1;
! 29481: lockInfo.l_start = 0;
! 29482: lockInfo.l_whence = SEEK_SET;
! 29483: lockInfo.l_type = F_RDLCK;
! 29484: if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
! 29485: return &posixIoMethods;
! 29486: }else{
! 29487: return &semIoMethods;
! 29488: }
! 29489: }
! 29490: static const sqlite3_io_methods
! 29491: *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
! 29492:
! 29493: #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
! 29494:
! 29495: /*
! 29496: ** An abstract type for a pointer to a IO method finder function:
! 29497: */
! 29498: typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
! 29499:
! 29500:
! 29501: /****************************************************************************
! 29502: **************************** sqlite3_vfs methods ****************************
! 29503: **
! 29504: ** This division contains the implementation of methods on the
! 29505: ** sqlite3_vfs object.
! 29506: */
! 29507:
! 29508: /*
! 29509: ** Initialize the contents of the unixFile structure pointed to by pId.
! 29510: */
! 29511: static int fillInUnixFile(
! 29512: sqlite3_vfs *pVfs, /* Pointer to vfs object */
! 29513: int h, /* Open file descriptor of file being opened */
! 29514: sqlite3_file *pId, /* Write to the unixFile structure here */
! 29515: const char *zFilename, /* Name of the file being opened */
! 29516: int ctrlFlags /* Zero or more UNIXFILE_* values */
! 29517: ){
! 29518: const sqlite3_io_methods *pLockingStyle;
! 29519: unixFile *pNew = (unixFile *)pId;
! 29520: int rc = SQLITE_OK;
! 29521:
! 29522: assert( pNew->pInode==NULL );
! 29523:
! 29524: /* Usually the path zFilename should not be a relative pathname. The
! 29525: ** exception is when opening the proxy "conch" file in builds that
! 29526: ** include the special Apple locking styles.
! 29527: */
! 29528: #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
! 29529: assert( zFilename==0 || zFilename[0]=='/'
! 29530: || pVfs->pAppData==(void*)&autolockIoFinder );
! 29531: #else
! 29532: assert( zFilename==0 || zFilename[0]=='/' );
! 29533: #endif
! 29534:
! 29535: /* No locking occurs in temporary files */
! 29536: assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
! 29537:
! 29538: OSTRACE(("OPEN %-3d %s\n", h, zFilename));
! 29539: pNew->h = h;
! 29540: pNew->pVfs = pVfs;
! 29541: pNew->zPath = zFilename;
! 29542: pNew->ctrlFlags = (u8)ctrlFlags;
! 29543: if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
! 29544: "psow", SQLITE_POWERSAFE_OVERWRITE) ){
! 29545: pNew->ctrlFlags |= UNIXFILE_PSOW;
! 29546: }
! 29547: if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
! 29548: pNew->ctrlFlags |= UNIXFILE_EXCL;
! 29549: }
! 29550:
! 29551: #if OS_VXWORKS
! 29552: pNew->pId = vxworksFindFileId(zFilename);
! 29553: if( pNew->pId==0 ){
! 29554: ctrlFlags |= UNIXFILE_NOLOCK;
! 29555: rc = SQLITE_NOMEM;
! 29556: }
! 29557: #endif
! 29558:
! 29559: if( ctrlFlags & UNIXFILE_NOLOCK ){
! 29560: pLockingStyle = &nolockIoMethods;
! 29561: }else{
! 29562: pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
! 29563: #if SQLITE_ENABLE_LOCKING_STYLE
! 29564: /* Cache zFilename in the locking context (AFP and dotlock override) for
! 29565: ** proxyLock activation is possible (remote proxy is based on db name)
! 29566: ** zFilename remains valid until file is closed, to support */
! 29567: pNew->lockingContext = (void*)zFilename;
! 29568: #endif
! 29569: }
! 29570:
! 29571: if( pLockingStyle == &posixIoMethods
! 29572: #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
! 29573: || pLockingStyle == &nfsIoMethods
! 29574: #endif
! 29575: ){
! 29576: unixEnterMutex();
! 29577: rc = findInodeInfo(pNew, &pNew->pInode);
! 29578: if( rc!=SQLITE_OK ){
! 29579: /* If an error occured in findInodeInfo(), close the file descriptor
! 29580: ** immediately, before releasing the mutex. findInodeInfo() may fail
! 29581: ** in two scenarios:
! 29582: **
! 29583: ** (a) A call to fstat() failed.
! 29584: ** (b) A malloc failed.
! 29585: **
! 29586: ** Scenario (b) may only occur if the process is holding no other
! 29587: ** file descriptors open on the same file. If there were other file
! 29588: ** descriptors on this file, then no malloc would be required by
! 29589: ** findInodeInfo(). If this is the case, it is quite safe to close
! 29590: ** handle h - as it is guaranteed that no posix locks will be released
! 29591: ** by doing so.
! 29592: **
! 29593: ** If scenario (a) caused the error then things are not so safe. The
! 29594: ** implicit assumption here is that if fstat() fails, things are in
! 29595: ** such bad shape that dropping a lock or two doesn't matter much.
! 29596: */
! 29597: robust_close(pNew, h, __LINE__);
! 29598: h = -1;
! 29599: }
! 29600: unixLeaveMutex();
! 29601: }
! 29602:
! 29603: #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
! 29604: else if( pLockingStyle == &afpIoMethods ){
! 29605: /* AFP locking uses the file path so it needs to be included in
! 29606: ** the afpLockingContext.
! 29607: */
! 29608: afpLockingContext *pCtx;
! 29609: pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
! 29610: if( pCtx==0 ){
! 29611: rc = SQLITE_NOMEM;
! 29612: }else{
! 29613: /* NB: zFilename exists and remains valid until the file is closed
! 29614: ** according to requirement F11141. So we do not need to make a
! 29615: ** copy of the filename. */
! 29616: pCtx->dbPath = zFilename;
! 29617: pCtx->reserved = 0;
! 29618: srandomdev();
! 29619: unixEnterMutex();
! 29620: rc = findInodeInfo(pNew, &pNew->pInode);
! 29621: if( rc!=SQLITE_OK ){
! 29622: sqlite3_free(pNew->lockingContext);
! 29623: robust_close(pNew, h, __LINE__);
! 29624: h = -1;
! 29625: }
! 29626: unixLeaveMutex();
! 29627: }
! 29628: }
! 29629: #endif
! 29630:
! 29631: else if( pLockingStyle == &dotlockIoMethods ){
! 29632: /* Dotfile locking uses the file path so it needs to be included in
! 29633: ** the dotlockLockingContext
! 29634: */
! 29635: char *zLockFile;
! 29636: int nFilename;
! 29637: assert( zFilename!=0 );
! 29638: nFilename = (int)strlen(zFilename) + 6;
! 29639: zLockFile = (char *)sqlite3_malloc(nFilename);
! 29640: if( zLockFile==0 ){
! 29641: rc = SQLITE_NOMEM;
! 29642: }else{
! 29643: sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
! 29644: }
! 29645: pNew->lockingContext = zLockFile;
! 29646: }
! 29647:
! 29648: #if OS_VXWORKS
! 29649: else if( pLockingStyle == &semIoMethods ){
! 29650: /* Named semaphore locking uses the file path so it needs to be
! 29651: ** included in the semLockingContext
! 29652: */
! 29653: unixEnterMutex();
! 29654: rc = findInodeInfo(pNew, &pNew->pInode);
! 29655: if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
! 29656: char *zSemName = pNew->pInode->aSemName;
! 29657: int n;
! 29658: sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
! 29659: pNew->pId->zCanonicalName);
! 29660: for( n=1; zSemName[n]; n++ )
! 29661: if( zSemName[n]=='/' ) zSemName[n] = '_';
! 29662: pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
! 29663: if( pNew->pInode->pSem == SEM_FAILED ){
! 29664: rc = SQLITE_NOMEM;
! 29665: pNew->pInode->aSemName[0] = '\0';
! 29666: }
! 29667: }
! 29668: unixLeaveMutex();
! 29669: }
! 29670: #endif
! 29671:
! 29672: pNew->lastErrno = 0;
! 29673: #if OS_VXWORKS
! 29674: if( rc!=SQLITE_OK ){
! 29675: if( h>=0 ) robust_close(pNew, h, __LINE__);
! 29676: h = -1;
! 29677: osUnlink(zFilename);
! 29678: isDelete = 0;
! 29679: }
! 29680: if( isDelete ) pNew->ctrlFlags |= UNIXFILE_DELETE;
! 29681: #endif
! 29682: if( rc!=SQLITE_OK ){
! 29683: if( h>=0 ) robust_close(pNew, h, __LINE__);
! 29684: }else{
! 29685: pNew->pMethod = pLockingStyle;
! 29686: OpenCounter(+1);
! 29687: }
! 29688: return rc;
! 29689: }
! 29690:
! 29691: /*
! 29692: ** Return the name of a directory in which to put temporary files.
! 29693: ** If no suitable temporary file directory can be found, return NULL.
! 29694: */
! 29695: static const char *unixTempFileDir(void){
! 29696: static const char *azDirs[] = {
! 29697: 0,
! 29698: 0,
! 29699: "/var/tmp",
! 29700: "/usr/tmp",
! 29701: "/tmp",
! 29702: 0 /* List terminator */
! 29703: };
! 29704: unsigned int i;
! 29705: struct stat buf;
! 29706: const char *zDir = 0;
! 29707:
! 29708: azDirs[0] = sqlite3_temp_directory;
! 29709: if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
! 29710: for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
! 29711: if( zDir==0 ) continue;
! 29712: if( osStat(zDir, &buf) ) continue;
! 29713: if( !S_ISDIR(buf.st_mode) ) continue;
! 29714: if( osAccess(zDir, 07) ) continue;
! 29715: break;
! 29716: }
! 29717: return zDir;
! 29718: }
! 29719:
! 29720: /*
! 29721: ** Create a temporary file name in zBuf. zBuf must be allocated
! 29722: ** by the calling process and must be big enough to hold at least
! 29723: ** pVfs->mxPathname bytes.
! 29724: */
! 29725: static int unixGetTempname(int nBuf, char *zBuf){
! 29726: static const unsigned char zChars[] =
! 29727: "abcdefghijklmnopqrstuvwxyz"
! 29728: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
! 29729: "0123456789";
! 29730: unsigned int i, j;
! 29731: const char *zDir;
! 29732:
! 29733: /* It's odd to simulate an io-error here, but really this is just
! 29734: ** using the io-error infrastructure to test that SQLite handles this
! 29735: ** function failing.
! 29736: */
! 29737: SimulateIOError( return SQLITE_IOERR );
! 29738:
! 29739: zDir = unixTempFileDir();
! 29740: if( zDir==0 ) zDir = ".";
! 29741:
! 29742: /* Check that the output buffer is large enough for the temporary file
! 29743: ** name. If it is not, return SQLITE_ERROR.
! 29744: */
! 29745: if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
! 29746: return SQLITE_ERROR;
! 29747: }
! 29748:
! 29749: do{
! 29750: sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
! 29751: j = (int)strlen(zBuf);
! 29752: sqlite3_randomness(15, &zBuf[j]);
! 29753: for(i=0; i<15; i++, j++){
! 29754: zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
! 29755: }
! 29756: zBuf[j] = 0;
! 29757: zBuf[j+1] = 0;
! 29758: }while( osAccess(zBuf,0)==0 );
! 29759: return SQLITE_OK;
! 29760: }
! 29761:
! 29762: #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
! 29763: /*
! 29764: ** Routine to transform a unixFile into a proxy-locking unixFile.
! 29765: ** Implementation in the proxy-lock division, but used by unixOpen()
! 29766: ** if SQLITE_PREFER_PROXY_LOCKING is defined.
! 29767: */
! 29768: static int proxyTransformUnixFile(unixFile*, const char*);
! 29769: #endif
! 29770:
! 29771: /*
! 29772: ** Search for an unused file descriptor that was opened on the database
! 29773: ** file (not a journal or master-journal file) identified by pathname
! 29774: ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
! 29775: ** argument to this function.
! 29776: **
! 29777: ** Such a file descriptor may exist if a database connection was closed
! 29778: ** but the associated file descriptor could not be closed because some
! 29779: ** other file descriptor open on the same file is holding a file-lock.
! 29780: ** Refer to comments in the unixClose() function and the lengthy comment
! 29781: ** describing "Posix Advisory Locking" at the start of this file for
! 29782: ** further details. Also, ticket #4018.
! 29783: **
! 29784: ** If a suitable file descriptor is found, then it is returned. If no
! 29785: ** such file descriptor is located, -1 is returned.
! 29786: */
! 29787: static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
! 29788: UnixUnusedFd *pUnused = 0;
! 29789:
! 29790: /* Do not search for an unused file descriptor on vxworks. Not because
! 29791: ** vxworks would not benefit from the change (it might, we're not sure),
! 29792: ** but because no way to test it is currently available. It is better
! 29793: ** not to risk breaking vxworks support for the sake of such an obscure
! 29794: ** feature. */
! 29795: #if !OS_VXWORKS
! 29796: struct stat sStat; /* Results of stat() call */
! 29797:
! 29798: /* A stat() call may fail for various reasons. If this happens, it is
! 29799: ** almost certain that an open() call on the same path will also fail.
! 29800: ** For this reason, if an error occurs in the stat() call here, it is
! 29801: ** ignored and -1 is returned. The caller will try to open a new file
! 29802: ** descriptor on the same path, fail, and return an error to SQLite.
! 29803: **
! 29804: ** Even if a subsequent open() call does succeed, the consequences of
! 29805: ** not searching for a resusable file descriptor are not dire. */
! 29806: if( 0==osStat(zPath, &sStat) ){
! 29807: unixInodeInfo *pInode;
! 29808:
! 29809: unixEnterMutex();
! 29810: pInode = inodeList;
! 29811: while( pInode && (pInode->fileId.dev!=sStat.st_dev
! 29812: || pInode->fileId.ino!=sStat.st_ino) ){
! 29813: pInode = pInode->pNext;
! 29814: }
! 29815: if( pInode ){
! 29816: UnixUnusedFd **pp;
! 29817: for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
! 29818: pUnused = *pp;
! 29819: if( pUnused ){
! 29820: *pp = pUnused->pNext;
! 29821: }
! 29822: }
! 29823: unixLeaveMutex();
! 29824: }
! 29825: #endif /* if !OS_VXWORKS */
! 29826: return pUnused;
! 29827: }
! 29828:
! 29829: /*
! 29830: ** This function is called by unixOpen() to determine the unix permissions
! 29831: ** to create new files with. If no error occurs, then SQLITE_OK is returned
! 29832: ** and a value suitable for passing as the third argument to open(2) is
! 29833: ** written to *pMode. If an IO error occurs, an SQLite error code is
! 29834: ** returned and the value of *pMode is not modified.
! 29835: **
! 29836: ** If the file being opened is a temporary file, it is always created with
! 29837: ** the octal permissions 0600 (read/writable by owner only). If the file
! 29838: ** is a database or master journal file, it is created with the permissions
! 29839: ** mask SQLITE_DEFAULT_FILE_PERMISSIONS.
! 29840: **
! 29841: ** Finally, if the file being opened is a WAL or regular journal file, then
! 29842: ** this function queries the file-system for the permissions on the
! 29843: ** corresponding database file and sets *pMode to this value. Whenever
! 29844: ** possible, WAL and journal files are created using the same permissions
! 29845: ** as the associated database file.
! 29846: **
! 29847: ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
! 29848: ** original filename is unavailable. But 8_3_NAMES is only used for
! 29849: ** FAT filesystems and permissions do not matter there, so just use
! 29850: ** the default permissions.
! 29851: */
! 29852: static int findCreateFileMode(
! 29853: const char *zPath, /* Path of file (possibly) being created */
! 29854: int flags, /* Flags passed as 4th argument to xOpen() */
! 29855: mode_t *pMode /* OUT: Permissions to open file with */
! 29856: ){
! 29857: int rc = SQLITE_OK; /* Return Code */
! 29858: *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS;
! 29859: if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
! 29860: char zDb[MAX_PATHNAME+1]; /* Database file path */
! 29861: int nDb; /* Number of valid bytes in zDb */
! 29862: struct stat sStat; /* Output of stat() on database file */
! 29863:
! 29864: /* zPath is a path to a WAL or journal file. The following block derives
! 29865: ** the path to the associated database file from zPath. This block handles
! 29866: ** the following naming conventions:
! 29867: **
! 29868: ** "<path to db>-journal"
! 29869: ** "<path to db>-wal"
! 29870: ** "<path to db>-journalNN"
! 29871: ** "<path to db>-walNN"
! 29872: **
! 29873: ** where NN is a decimal number. The NN naming schemes are
! 29874: ** used by the test_multiplex.c module.
! 29875: */
! 29876: nDb = sqlite3Strlen30(zPath) - 1;
! 29877: #ifdef SQLITE_ENABLE_8_3_NAMES
! 29878: while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
! 29879: if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
! 29880: #else
! 29881: while( zPath[nDb]!='-' ){
! 29882: assert( nDb>0 );
! 29883: assert( zPath[nDb]!='\n' );
! 29884: nDb--;
! 29885: }
! 29886: #endif
! 29887: memcpy(zDb, zPath, nDb);
! 29888: zDb[nDb] = '\0';
! 29889:
! 29890: if( 0==osStat(zDb, &sStat) ){
! 29891: *pMode = sStat.st_mode & 0777;
! 29892: }else{
! 29893: rc = SQLITE_IOERR_FSTAT;
! 29894: }
! 29895: }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
! 29896: *pMode = 0600;
! 29897: }
! 29898: return rc;
! 29899: }
! 29900:
! 29901: /*
! 29902: ** Open the file zPath.
! 29903: **
! 29904: ** Previously, the SQLite OS layer used three functions in place of this
! 29905: ** one:
! 29906: **
! 29907: ** sqlite3OsOpenReadWrite();
! 29908: ** sqlite3OsOpenReadOnly();
! 29909: ** sqlite3OsOpenExclusive();
! 29910: **
! 29911: ** These calls correspond to the following combinations of flags:
! 29912: **
! 29913: ** ReadWrite() -> (READWRITE | CREATE)
! 29914: ** ReadOnly() -> (READONLY)
! 29915: ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
! 29916: **
! 29917: ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
! 29918: ** true, the file was configured to be automatically deleted when the
! 29919: ** file handle closed. To achieve the same effect using this new
! 29920: ** interface, add the DELETEONCLOSE flag to those specified above for
! 29921: ** OpenExclusive().
! 29922: */
! 29923: static int unixOpen(
! 29924: sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
! 29925: const char *zPath, /* Pathname of file to be opened */
! 29926: sqlite3_file *pFile, /* The file descriptor to be filled in */
! 29927: int flags, /* Input flags to control the opening */
! 29928: int *pOutFlags /* Output flags returned to SQLite core */
! 29929: ){
! 29930: unixFile *p = (unixFile *)pFile;
! 29931: int fd = -1; /* File descriptor returned by open() */
! 29932: int openFlags = 0; /* Flags to pass to open() */
! 29933: int eType = flags&0xFFFFFF00; /* Type of file to open */
! 29934: int noLock; /* True to omit locking primitives */
! 29935: int rc = SQLITE_OK; /* Function Return Code */
! 29936: int ctrlFlags = 0; /* UNIXFILE_* flags */
! 29937:
! 29938: int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
! 29939: int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
! 29940: int isCreate = (flags & SQLITE_OPEN_CREATE);
! 29941: int isReadonly = (flags & SQLITE_OPEN_READONLY);
! 29942: int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
! 29943: #if SQLITE_ENABLE_LOCKING_STYLE
! 29944: int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
! 29945: #endif
! 29946: #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
! 29947: struct statfs fsInfo;
! 29948: #endif
! 29949:
! 29950: /* If creating a master or main-file journal, this function will open
! 29951: ** a file-descriptor on the directory too. The first time unixSync()
! 29952: ** is called the directory file descriptor will be fsync()ed and close()d.
! 29953: */
! 29954: int syncDir = (isCreate && (
! 29955: eType==SQLITE_OPEN_MASTER_JOURNAL
! 29956: || eType==SQLITE_OPEN_MAIN_JOURNAL
! 29957: || eType==SQLITE_OPEN_WAL
! 29958: ));
! 29959:
! 29960: /* If argument zPath is a NULL pointer, this function is required to open
! 29961: ** a temporary file. Use this buffer to store the file name in.
! 29962: */
! 29963: char zTmpname[MAX_PATHNAME+2];
! 29964: const char *zName = zPath;
! 29965:
! 29966: /* Check the following statements are true:
! 29967: **
! 29968: ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
! 29969: ** (b) if CREATE is set, then READWRITE must also be set, and
! 29970: ** (c) if EXCLUSIVE is set, then CREATE must also be set.
! 29971: ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
! 29972: */
! 29973: assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
! 29974: assert(isCreate==0 || isReadWrite);
! 29975: assert(isExclusive==0 || isCreate);
! 29976: assert(isDelete==0 || isCreate);
! 29977:
! 29978: /* The main DB, main journal, WAL file and master journal are never
! 29979: ** automatically deleted. Nor are they ever temporary files. */
! 29980: assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
! 29981: assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
! 29982: assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
! 29983: assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
! 29984:
! 29985: /* Assert that the upper layer has set one of the "file-type" flags. */
! 29986: assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
! 29987: || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
! 29988: || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
! 29989: || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
! 29990: );
! 29991:
! 29992: memset(p, 0, sizeof(unixFile));
! 29993:
! 29994: if( eType==SQLITE_OPEN_MAIN_DB ){
! 29995: UnixUnusedFd *pUnused;
! 29996: pUnused = findReusableFd(zName, flags);
! 29997: if( pUnused ){
! 29998: fd = pUnused->fd;
! 29999: }else{
! 30000: pUnused = sqlite3_malloc(sizeof(*pUnused));
! 30001: if( !pUnused ){
! 30002: return SQLITE_NOMEM;
! 30003: }
! 30004: }
! 30005: p->pUnused = pUnused;
! 30006:
! 30007: /* Database filenames are double-zero terminated if they are not
! 30008: ** URIs with parameters. Hence, they can always be passed into
! 30009: ** sqlite3_uri_parameter(). */
! 30010: assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
! 30011:
! 30012: }else if( !zName ){
! 30013: /* If zName is NULL, the upper layer is requesting a temp file. */
! 30014: assert(isDelete && !syncDir);
! 30015: rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
! 30016: if( rc!=SQLITE_OK ){
! 30017: return rc;
! 30018: }
! 30019: zName = zTmpname;
! 30020:
! 30021: /* Generated temporary filenames are always double-zero terminated
! 30022: ** for use by sqlite3_uri_parameter(). */
! 30023: assert( zName[strlen(zName)+1]==0 );
! 30024: }
! 30025:
! 30026: /* Determine the value of the flags parameter passed to POSIX function
! 30027: ** open(). These must be calculated even if open() is not called, as
! 30028: ** they may be stored as part of the file handle and used by the
! 30029: ** 'conch file' locking functions later on. */
! 30030: if( isReadonly ) openFlags |= O_RDONLY;
! 30031: if( isReadWrite ) openFlags |= O_RDWR;
! 30032: if( isCreate ) openFlags |= O_CREAT;
! 30033: if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
! 30034: openFlags |= (O_LARGEFILE|O_BINARY);
! 30035:
! 30036: if( fd<0 ){
! 30037: mode_t openMode; /* Permissions to create file with */
! 30038: rc = findCreateFileMode(zName, flags, &openMode);
! 30039: if( rc!=SQLITE_OK ){
! 30040: assert( !p->pUnused );
! 30041: assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
! 30042: return rc;
! 30043: }
! 30044: fd = robust_open(zName, openFlags, openMode);
! 30045: OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
! 30046: if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
! 30047: /* Failed to open the file for read/write access. Try read-only. */
! 30048: flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
! 30049: openFlags &= ~(O_RDWR|O_CREAT);
! 30050: flags |= SQLITE_OPEN_READONLY;
! 30051: openFlags |= O_RDONLY;
! 30052: isReadonly = 1;
! 30053: fd = robust_open(zName, openFlags, openMode);
! 30054: }
! 30055: if( fd<0 ){
! 30056: rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
! 30057: goto open_finished;
! 30058: }
! 30059: }
! 30060: assert( fd>=0 );
! 30061: if( pOutFlags ){
! 30062: *pOutFlags = flags;
! 30063: }
! 30064:
! 30065: if( p->pUnused ){
! 30066: p->pUnused->fd = fd;
! 30067: p->pUnused->flags = flags;
! 30068: }
! 30069:
! 30070: if( isDelete ){
! 30071: #if OS_VXWORKS
! 30072: zPath = zName;
! 30073: #else
! 30074: osUnlink(zName);
! 30075: #endif
! 30076: }
! 30077: #if SQLITE_ENABLE_LOCKING_STYLE
! 30078: else{
! 30079: p->openFlags = openFlags;
! 30080: }
! 30081: #endif
! 30082:
! 30083: #ifdef FD_CLOEXEC
! 30084: osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
! 30085: #endif
! 30086:
! 30087: noLock = eType!=SQLITE_OPEN_MAIN_DB;
! 30088:
! 30089:
! 30090: #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
! 30091: if( fstatfs(fd, &fsInfo) == -1 ){
! 30092: ((unixFile*)pFile)->lastErrno = errno;
! 30093: robust_close(p, fd, __LINE__);
! 30094: return SQLITE_IOERR_ACCESS;
! 30095: }
! 30096: if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
! 30097: ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
! 30098: }
! 30099: #endif
! 30100:
! 30101: /* Set up appropriate ctrlFlags */
! 30102: if( isDelete ) ctrlFlags |= UNIXFILE_DELETE;
! 30103: if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY;
! 30104: if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK;
! 30105: if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC;
! 30106: if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
! 30107:
! 30108: #if SQLITE_ENABLE_LOCKING_STYLE
! 30109: #if SQLITE_PREFER_PROXY_LOCKING
! 30110: isAutoProxy = 1;
! 30111: #endif
! 30112: if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
! 30113: char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
! 30114: int useProxy = 0;
! 30115:
! 30116: /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
! 30117: ** never use proxy, NULL means use proxy for non-local files only. */
! 30118: if( envforce!=NULL ){
! 30119: useProxy = atoi(envforce)>0;
! 30120: }else{
! 30121: if( statfs(zPath, &fsInfo) == -1 ){
! 30122: /* In theory, the close(fd) call is sub-optimal. If the file opened
! 30123: ** with fd is a database file, and there are other connections open
! 30124: ** on that file that are currently holding advisory locks on it,
! 30125: ** then the call to close() will cancel those locks. In practice,
! 30126: ** we're assuming that statfs() doesn't fail very often. At least
! 30127: ** not while other file descriptors opened by the same process on
! 30128: ** the same file are working. */
! 30129: p->lastErrno = errno;
! 30130: robust_close(p, fd, __LINE__);
! 30131: rc = SQLITE_IOERR_ACCESS;
! 30132: goto open_finished;
! 30133: }
! 30134: useProxy = !(fsInfo.f_flags&MNT_LOCAL);
! 30135: }
! 30136: if( useProxy ){
! 30137: rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
! 30138: if( rc==SQLITE_OK ){
! 30139: rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
! 30140: if( rc!=SQLITE_OK ){
! 30141: /* Use unixClose to clean up the resources added in fillInUnixFile
! 30142: ** and clear all the structure's references. Specifically,
! 30143: ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
! 30144: */
! 30145: unixClose(pFile);
! 30146: return rc;
! 30147: }
! 30148: }
! 30149: goto open_finished;
! 30150: }
! 30151: }
! 30152: #endif
! 30153:
! 30154: rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
! 30155:
! 30156: open_finished:
! 30157: if( rc!=SQLITE_OK ){
! 30158: sqlite3_free(p->pUnused);
! 30159: }
! 30160: return rc;
! 30161: }
! 30162:
! 30163:
! 30164: /*
! 30165: ** Delete the file at zPath. If the dirSync argument is true, fsync()
! 30166: ** the directory after deleting the file.
! 30167: */
! 30168: static int unixDelete(
! 30169: sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
! 30170: const char *zPath, /* Name of file to be deleted */
! 30171: int dirSync /* If true, fsync() directory after deleting file */
! 30172: ){
! 30173: int rc = SQLITE_OK;
! 30174: UNUSED_PARAMETER(NotUsed);
! 30175: SimulateIOError(return SQLITE_IOERR_DELETE);
! 30176: if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
! 30177: return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
! 30178: }
! 30179: #ifndef SQLITE_DISABLE_DIRSYNC
! 30180: if( (dirSync & 1)!=0 ){
! 30181: int fd;
! 30182: rc = osOpenDirectory(zPath, &fd);
! 30183: if( rc==SQLITE_OK ){
! 30184: #if OS_VXWORKS
! 30185: if( fsync(fd)==-1 )
! 30186: #else
! 30187: if( fsync(fd) )
! 30188: #endif
! 30189: {
! 30190: rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
! 30191: }
! 30192: robust_close(0, fd, __LINE__);
! 30193: }else if( rc==SQLITE_CANTOPEN ){
! 30194: rc = SQLITE_OK;
! 30195: }
! 30196: }
! 30197: #endif
! 30198: return rc;
! 30199: }
! 30200:
! 30201: /*
! 30202: ** Test the existance of or access permissions of file zPath. The
! 30203: ** test performed depends on the value of flags:
! 30204: **
! 30205: ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
! 30206: ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
! 30207: ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
! 30208: **
! 30209: ** Otherwise return 0.
! 30210: */
! 30211: static int unixAccess(
! 30212: sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
! 30213: const char *zPath, /* Path of the file to examine */
! 30214: int flags, /* What do we want to learn about the zPath file? */
! 30215: int *pResOut /* Write result boolean here */
! 30216: ){
! 30217: int amode = 0;
! 30218: UNUSED_PARAMETER(NotUsed);
! 30219: SimulateIOError( return SQLITE_IOERR_ACCESS; );
! 30220: switch( flags ){
! 30221: case SQLITE_ACCESS_EXISTS:
! 30222: amode = F_OK;
! 30223: break;
! 30224: case SQLITE_ACCESS_READWRITE:
! 30225: amode = W_OK|R_OK;
! 30226: break;
! 30227: case SQLITE_ACCESS_READ:
! 30228: amode = R_OK;
! 30229: break;
! 30230:
! 30231: default:
! 30232: assert(!"Invalid flags argument");
! 30233: }
! 30234: *pResOut = (osAccess(zPath, amode)==0);
! 30235: if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
! 30236: struct stat buf;
! 30237: if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
! 30238: *pResOut = 0;
! 30239: }
! 30240: }
! 30241: return SQLITE_OK;
! 30242: }
! 30243:
! 30244:
! 30245: /*
! 30246: ** Turn a relative pathname into a full pathname. The relative path
! 30247: ** is stored as a nul-terminated string in the buffer pointed to by
! 30248: ** zPath.
! 30249: **
! 30250: ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
! 30251: ** (in this case, MAX_PATHNAME bytes). The full-path is written to
! 30252: ** this buffer before returning.
! 30253: */
! 30254: static int unixFullPathname(
! 30255: sqlite3_vfs *pVfs, /* Pointer to vfs object */
! 30256: const char *zPath, /* Possibly relative input path */
! 30257: int nOut, /* Size of output buffer in bytes */
! 30258: char *zOut /* Output buffer */
! 30259: ){
! 30260:
! 30261: /* It's odd to simulate an io-error here, but really this is just
! 30262: ** using the io-error infrastructure to test that SQLite handles this
! 30263: ** function failing. This function could fail if, for example, the
! 30264: ** current working directory has been unlinked.
! 30265: */
! 30266: SimulateIOError( return SQLITE_ERROR );
! 30267:
! 30268: assert( pVfs->mxPathname==MAX_PATHNAME );
! 30269: UNUSED_PARAMETER(pVfs);
! 30270:
! 30271: zOut[nOut-1] = '\0';
! 30272: if( zPath[0]=='/' ){
! 30273: sqlite3_snprintf(nOut, zOut, "%s", zPath);
! 30274: }else{
! 30275: int nCwd;
! 30276: if( osGetcwd(zOut, nOut-1)==0 ){
! 30277: return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
! 30278: }
! 30279: nCwd = (int)strlen(zOut);
! 30280: sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
! 30281: }
! 30282: return SQLITE_OK;
! 30283: }
! 30284:
! 30285:
! 30286: #ifndef SQLITE_OMIT_LOAD_EXTENSION
! 30287: /*
! 30288: ** Interfaces for opening a shared library, finding entry points
! 30289: ** within the shared library, and closing the shared library.
! 30290: */
! 30291: #include <dlfcn.h>
! 30292: static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
! 30293: UNUSED_PARAMETER(NotUsed);
! 30294: return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
! 30295: }
! 30296:
! 30297: /*
! 30298: ** SQLite calls this function immediately after a call to unixDlSym() or
! 30299: ** unixDlOpen() fails (returns a null pointer). If a more detailed error
! 30300: ** message is available, it is written to zBufOut. If no error message
! 30301: ** is available, zBufOut is left unmodified and SQLite uses a default
! 30302: ** error message.
! 30303: */
! 30304: static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
! 30305: const char *zErr;
! 30306: UNUSED_PARAMETER(NotUsed);
! 30307: unixEnterMutex();
! 30308: zErr = dlerror();
! 30309: if( zErr ){
! 30310: sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
! 30311: }
! 30312: unixLeaveMutex();
! 30313: }
! 30314: static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
! 30315: /*
! 30316: ** GCC with -pedantic-errors says that C90 does not allow a void* to be
! 30317: ** cast into a pointer to a function. And yet the library dlsym() routine
! 30318: ** returns a void* which is really a pointer to a function. So how do we
! 30319: ** use dlsym() with -pedantic-errors?
! 30320: **
! 30321: ** Variable x below is defined to be a pointer to a function taking
! 30322: ** parameters void* and const char* and returning a pointer to a function.
! 30323: ** We initialize x by assigning it a pointer to the dlsym() function.
! 30324: ** (That assignment requires a cast.) Then we call the function that
! 30325: ** x points to.
! 30326: **
! 30327: ** This work-around is unlikely to work correctly on any system where
! 30328: ** you really cannot cast a function pointer into void*. But then, on the
! 30329: ** other hand, dlsym() will not work on such a system either, so we have
! 30330: ** not really lost anything.
! 30331: */
! 30332: void (*(*x)(void*,const char*))(void);
! 30333: UNUSED_PARAMETER(NotUsed);
! 30334: x = (void(*(*)(void*,const char*))(void))dlsym;
! 30335: return (*x)(p, zSym);
! 30336: }
! 30337: static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
! 30338: UNUSED_PARAMETER(NotUsed);
! 30339: dlclose(pHandle);
! 30340: }
! 30341: #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
! 30342: #define unixDlOpen 0
! 30343: #define unixDlError 0
! 30344: #define unixDlSym 0
! 30345: #define unixDlClose 0
! 30346: #endif
! 30347:
! 30348: /*
! 30349: ** Write nBuf bytes of random data to the supplied buffer zBuf.
! 30350: */
! 30351: static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
! 30352: UNUSED_PARAMETER(NotUsed);
! 30353: assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
! 30354:
! 30355: /* We have to initialize zBuf to prevent valgrind from reporting
! 30356: ** errors. The reports issued by valgrind are incorrect - we would
! 30357: ** prefer that the randomness be increased by making use of the
! 30358: ** uninitialized space in zBuf - but valgrind errors tend to worry
! 30359: ** some users. Rather than argue, it seems easier just to initialize
! 30360: ** the whole array and silence valgrind, even if that means less randomness
! 30361: ** in the random seed.
! 30362: **
! 30363: ** When testing, initializing zBuf[] to zero is all we do. That means
! 30364: ** that we always use the same random number sequence. This makes the
! 30365: ** tests repeatable.
! 30366: */
! 30367: memset(zBuf, 0, nBuf);
! 30368: #if !defined(SQLITE_TEST)
! 30369: {
! 30370: int pid, fd;
! 30371: fd = robust_open("/dev/urandom", O_RDONLY, 0);
! 30372: if( fd<0 ){
! 30373: time_t t;
! 30374: time(&t);
! 30375: memcpy(zBuf, &t, sizeof(t));
! 30376: pid = getpid();
! 30377: memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
! 30378: assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
! 30379: nBuf = sizeof(t) + sizeof(pid);
! 30380: }else{
! 30381: do{ nBuf = osRead(fd, zBuf, nBuf); }while( nBuf<0 && errno==EINTR );
! 30382: robust_close(0, fd, __LINE__);
! 30383: }
! 30384: }
! 30385: #endif
! 30386: return nBuf;
! 30387: }
! 30388:
! 30389:
! 30390: /*
! 30391: ** Sleep for a little while. Return the amount of time slept.
! 30392: ** The argument is the number of microseconds we want to sleep.
! 30393: ** The return value is the number of microseconds of sleep actually
! 30394: ** requested from the underlying operating system, a number which
! 30395: ** might be greater than or equal to the argument, but not less
! 30396: ** than the argument.
! 30397: */
! 30398: static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
! 30399: #if OS_VXWORKS
! 30400: struct timespec sp;
! 30401:
! 30402: sp.tv_sec = microseconds / 1000000;
! 30403: sp.tv_nsec = (microseconds % 1000000) * 1000;
! 30404: nanosleep(&sp, NULL);
! 30405: UNUSED_PARAMETER(NotUsed);
! 30406: return microseconds;
! 30407: #elif defined(HAVE_USLEEP) && HAVE_USLEEP
! 30408: usleep(microseconds);
! 30409: UNUSED_PARAMETER(NotUsed);
! 30410: return microseconds;
! 30411: #else
! 30412: int seconds = (microseconds+999999)/1000000;
! 30413: sleep(seconds);
! 30414: UNUSED_PARAMETER(NotUsed);
! 30415: return seconds*1000000;
! 30416: #endif
! 30417: }
! 30418:
! 30419: /*
! 30420: ** The following variable, if set to a non-zero value, is interpreted as
! 30421: ** the number of seconds since 1970 and is used to set the result of
! 30422: ** sqlite3OsCurrentTime() during testing.
! 30423: */
! 30424: #ifdef SQLITE_TEST
! 30425: SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
! 30426: #endif
! 30427:
! 30428: /*
! 30429: ** Find the current time (in Universal Coordinated Time). Write into *piNow
! 30430: ** the current time and date as a Julian Day number times 86_400_000. In
! 30431: ** other words, write into *piNow the number of milliseconds since the Julian
! 30432: ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
! 30433: ** proleptic Gregorian calendar.
! 30434: **
! 30435: ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
! 30436: ** cannot be found.
! 30437: */
! 30438: static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
! 30439: static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
! 30440: int rc = SQLITE_OK;
! 30441: #if defined(NO_GETTOD)
! 30442: time_t t;
! 30443: time(&t);
! 30444: *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
! 30445: #elif OS_VXWORKS
! 30446: struct timespec sNow;
! 30447: clock_gettime(CLOCK_REALTIME, &sNow);
! 30448: *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
! 30449: #else
! 30450: struct timeval sNow;
! 30451: if( gettimeofday(&sNow, 0)==0 ){
! 30452: *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
! 30453: }else{
! 30454: rc = SQLITE_ERROR;
! 30455: }
! 30456: #endif
! 30457:
! 30458: #ifdef SQLITE_TEST
! 30459: if( sqlite3_current_time ){
! 30460: *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
! 30461: }
! 30462: #endif
! 30463: UNUSED_PARAMETER(NotUsed);
! 30464: return rc;
! 30465: }
! 30466:
! 30467: /*
! 30468: ** Find the current time (in Universal Coordinated Time). Write the
! 30469: ** current time and date as a Julian Day number into *prNow and
! 30470: ** return 0. Return 1 if the time and date cannot be found.
! 30471: */
! 30472: static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
! 30473: sqlite3_int64 i = 0;
! 30474: int rc;
! 30475: UNUSED_PARAMETER(NotUsed);
! 30476: rc = unixCurrentTimeInt64(0, &i);
! 30477: *prNow = i/86400000.0;
! 30478: return rc;
! 30479: }
! 30480:
! 30481: /*
! 30482: ** We added the xGetLastError() method with the intention of providing
! 30483: ** better low-level error messages when operating-system problems come up
! 30484: ** during SQLite operation. But so far, none of that has been implemented
! 30485: ** in the core. So this routine is never called. For now, it is merely
! 30486: ** a place-holder.
! 30487: */
! 30488: static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
! 30489: UNUSED_PARAMETER(NotUsed);
! 30490: UNUSED_PARAMETER(NotUsed2);
! 30491: UNUSED_PARAMETER(NotUsed3);
! 30492: return 0;
! 30493: }
! 30494:
! 30495:
! 30496: /*
! 30497: ************************ End of sqlite3_vfs methods ***************************
! 30498: ******************************************************************************/
! 30499:
! 30500: /******************************************************************************
! 30501: ************************** Begin Proxy Locking ********************************
! 30502: **
! 30503: ** Proxy locking is a "uber-locking-method" in this sense: It uses the
! 30504: ** other locking methods on secondary lock files. Proxy locking is a
! 30505: ** meta-layer over top of the primitive locking implemented above. For
! 30506: ** this reason, the division that implements of proxy locking is deferred
! 30507: ** until late in the file (here) after all of the other I/O methods have
! 30508: ** been defined - so that the primitive locking methods are available
! 30509: ** as services to help with the implementation of proxy locking.
! 30510: **
! 30511: ****
! 30512: **
! 30513: ** The default locking schemes in SQLite use byte-range locks on the
! 30514: ** database file to coordinate safe, concurrent access by multiple readers
! 30515: ** and writers [http://sqlite.org/lockingv3.html]. The five file locking
! 30516: ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
! 30517: ** as POSIX read & write locks over fixed set of locations (via fsctl),
! 30518: ** on AFP and SMB only exclusive byte-range locks are available via fsctl
! 30519: ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
! 30520: ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
! 30521: ** address in the shared range is taken for a SHARED lock, the entire
! 30522: ** shared range is taken for an EXCLUSIVE lock):
! 30523: **
! 30524: ** PENDING_BYTE 0x40000000
! 30525: ** RESERVED_BYTE 0x40000001
! 30526: ** SHARED_RANGE 0x40000002 -> 0x40000200
! 30527: **
! 30528: ** This works well on the local file system, but shows a nearly 100x
! 30529: ** slowdown in read performance on AFP because the AFP client disables
! 30530: ** the read cache when byte-range locks are present. Enabling the read
! 30531: ** cache exposes a cache coherency problem that is present on all OS X
! 30532: ** supported network file systems. NFS and AFP both observe the
! 30533: ** close-to-open semantics for ensuring cache coherency
! 30534: ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
! 30535: ** address the requirements for concurrent database access by multiple
! 30536: ** readers and writers
! 30537: ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
! 30538: **
! 30539: ** To address the performance and cache coherency issues, proxy file locking
! 30540: ** changes the way database access is controlled by limiting access to a
! 30541: ** single host at a time and moving file locks off of the database file
! 30542: ** and onto a proxy file on the local file system.
! 30543: **
! 30544: **
! 30545: ** Using proxy locks
! 30546: ** -----------------
! 30547: **
! 30548: ** C APIs
! 30549: **
! 30550: ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
! 30551: ** <proxy_path> | ":auto:");
! 30552: ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
! 30553: **
! 30554: **
! 30555: ** SQL pragmas
! 30556: **
! 30557: ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
! 30558: ** PRAGMA [database.]lock_proxy_file
! 30559: **
! 30560: ** Specifying ":auto:" means that if there is a conch file with a matching
! 30561: ** host ID in it, the proxy path in the conch file will be used, otherwise
! 30562: ** a proxy path based on the user's temp dir
! 30563: ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
! 30564: ** actual proxy file name is generated from the name and path of the
! 30565: ** database file. For example:
! 30566: **
! 30567: ** For database path "/Users/me/foo.db"
! 30568: ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
! 30569: **
! 30570: ** Once a lock proxy is configured for a database connection, it can not
! 30571: ** be removed, however it may be switched to a different proxy path via
! 30572: ** the above APIs (assuming the conch file is not being held by another
! 30573: ** connection or process).
! 30574: **
! 30575: **
! 30576: ** How proxy locking works
! 30577: ** -----------------------
! 30578: **
! 30579: ** Proxy file locking relies primarily on two new supporting files:
! 30580: **
! 30581: ** * conch file to limit access to the database file to a single host
! 30582: ** at a time
! 30583: **
! 30584: ** * proxy file to act as a proxy for the advisory locks normally
! 30585: ** taken on the database
! 30586: **
! 30587: ** The conch file - to use a proxy file, sqlite must first "hold the conch"
! 30588: ** by taking an sqlite-style shared lock on the conch file, reading the
! 30589: ** contents and comparing the host's unique host ID (see below) and lock
! 30590: ** proxy path against the values stored in the conch. The conch file is
! 30591: ** stored in the same directory as the database file and the file name
! 30592: ** is patterned after the database file name as ".<databasename>-conch".
! 30593: ** If the conch file does not exist, or it's contents do not match the
! 30594: ** host ID and/or proxy path, then the lock is escalated to an exclusive
! 30595: ** lock and the conch file contents is updated with the host ID and proxy
! 30596: ** path and the lock is downgraded to a shared lock again. If the conch
! 30597: ** is held by another process (with a shared lock), the exclusive lock
! 30598: ** will fail and SQLITE_BUSY is returned.
! 30599: **
! 30600: ** The proxy file - a single-byte file used for all advisory file locks
! 30601: ** normally taken on the database file. This allows for safe sharing
! 30602: ** of the database file for multiple readers and writers on the same
! 30603: ** host (the conch ensures that they all use the same local lock file).
! 30604: **
! 30605: ** Requesting the lock proxy does not immediately take the conch, it is
! 30606: ** only taken when the first request to lock database file is made.
! 30607: ** This matches the semantics of the traditional locking behavior, where
! 30608: ** opening a connection to a database file does not take a lock on it.
! 30609: ** The shared lock and an open file descriptor are maintained until
! 30610: ** the connection to the database is closed.
! 30611: **
! 30612: ** The proxy file and the lock file are never deleted so they only need
! 30613: ** to be created the first time they are used.
! 30614: **
! 30615: ** Configuration options
! 30616: ** ---------------------
! 30617: **
! 30618: ** SQLITE_PREFER_PROXY_LOCKING
! 30619: **
! 30620: ** Database files accessed on non-local file systems are
! 30621: ** automatically configured for proxy locking, lock files are
! 30622: ** named automatically using the same logic as
! 30623: ** PRAGMA lock_proxy_file=":auto:"
! 30624: **
! 30625: ** SQLITE_PROXY_DEBUG
! 30626: **
! 30627: ** Enables the logging of error messages during host id file
! 30628: ** retrieval and creation
! 30629: **
! 30630: ** LOCKPROXYDIR
! 30631: **
! 30632: ** Overrides the default directory used for lock proxy files that
! 30633: ** are named automatically via the ":auto:" setting
! 30634: **
! 30635: ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
! 30636: **
! 30637: ** Permissions to use when creating a directory for storing the
! 30638: ** lock proxy files, only used when LOCKPROXYDIR is not set.
! 30639: **
! 30640: **
! 30641: ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
! 30642: ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
! 30643: ** force proxy locking to be used for every database file opened, and 0
! 30644: ** will force automatic proxy locking to be disabled for all database
! 30645: ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
! 30646: ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
! 30647: */
! 30648:
! 30649: /*
! 30650: ** Proxy locking is only available on MacOSX
! 30651: */
! 30652: #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
! 30653:
! 30654: /*
! 30655: ** The proxyLockingContext has the path and file structures for the remote
! 30656: ** and local proxy files in it
! 30657: */
! 30658: typedef struct proxyLockingContext proxyLockingContext;
! 30659: struct proxyLockingContext {
! 30660: unixFile *conchFile; /* Open conch file */
! 30661: char *conchFilePath; /* Name of the conch file */
! 30662: unixFile *lockProxy; /* Open proxy lock file */
! 30663: char *lockProxyPath; /* Name of the proxy lock file */
! 30664: char *dbPath; /* Name of the open file */
! 30665: int conchHeld; /* 1 if the conch is held, -1 if lockless */
! 30666: void *oldLockingContext; /* Original lockingcontext to restore on close */
! 30667: sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
! 30668: };
! 30669:
! 30670: /*
! 30671: ** The proxy lock file path for the database at dbPath is written into lPath,
! 30672: ** which must point to valid, writable memory large enough for a maxLen length
! 30673: ** file path.
! 30674: */
! 30675: static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
! 30676: int len;
! 30677: int dbLen;
! 30678: int i;
! 30679:
! 30680: #ifdef LOCKPROXYDIR
! 30681: len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
! 30682: #else
! 30683: # ifdef _CS_DARWIN_USER_TEMP_DIR
! 30684: {
! 30685: if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
! 30686: OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
! 30687: lPath, errno, getpid()));
! 30688: return SQLITE_IOERR_LOCK;
! 30689: }
! 30690: len = strlcat(lPath, "sqliteplocks", maxLen);
! 30691: }
! 30692: # else
! 30693: len = strlcpy(lPath, "/tmp/", maxLen);
! 30694: # endif
! 30695: #endif
! 30696:
! 30697: if( lPath[len-1]!='/' ){
! 30698: len = strlcat(lPath, "/", maxLen);
! 30699: }
! 30700:
! 30701: /* transform the db path to a unique cache name */
! 30702: dbLen = (int)strlen(dbPath);
! 30703: for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
! 30704: char c = dbPath[i];
! 30705: lPath[i+len] = (c=='/')?'_':c;
! 30706: }
! 30707: lPath[i+len]='\0';
! 30708: strlcat(lPath, ":auto:", maxLen);
! 30709: OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid()));
! 30710: return SQLITE_OK;
! 30711: }
! 30712:
! 30713: /*
! 30714: ** Creates the lock file and any missing directories in lockPath
! 30715: */
! 30716: static int proxyCreateLockPath(const char *lockPath){
! 30717: int i, len;
! 30718: char buf[MAXPATHLEN];
! 30719: int start = 0;
! 30720:
! 30721: assert(lockPath!=NULL);
! 30722: /* try to create all the intermediate directories */
! 30723: len = (int)strlen(lockPath);
! 30724: buf[0] = lockPath[0];
! 30725: for( i=1; i<len; i++ ){
! 30726: if( lockPath[i] == '/' && (i - start > 0) ){
! 30727: /* only mkdir if leaf dir != "." or "/" or ".." */
! 30728: if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
! 30729: || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
! 30730: buf[i]='\0';
! 30731: if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
! 30732: int err=errno;
! 30733: if( err!=EEXIST ) {
! 30734: OSTRACE(("CREATELOCKPATH FAILED creating %s, "
! 30735: "'%s' proxy lock path=%s pid=%d\n",
! 30736: buf, strerror(err), lockPath, getpid()));
! 30737: return err;
! 30738: }
! 30739: }
! 30740: }
! 30741: start=i+1;
! 30742: }
! 30743: buf[i] = lockPath[i];
! 30744: }
! 30745: OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid()));
! 30746: return 0;
! 30747: }
! 30748:
! 30749: /*
! 30750: ** Create a new VFS file descriptor (stored in memory obtained from
! 30751: ** sqlite3_malloc) and open the file named "path" in the file descriptor.
! 30752: **
! 30753: ** The caller is responsible not only for closing the file descriptor
! 30754: ** but also for freeing the memory associated with the file descriptor.
! 30755: */
! 30756: static int proxyCreateUnixFile(
! 30757: const char *path, /* path for the new unixFile */
! 30758: unixFile **ppFile, /* unixFile created and returned by ref */
! 30759: int islockfile /* if non zero missing dirs will be created */
! 30760: ) {
! 30761: int fd = -1;
! 30762: unixFile *pNew;
! 30763: int rc = SQLITE_OK;
! 30764: int openFlags = O_RDWR | O_CREAT;
! 30765: sqlite3_vfs dummyVfs;
! 30766: int terrno = 0;
! 30767: UnixUnusedFd *pUnused = NULL;
! 30768:
! 30769: /* 1. first try to open/create the file
! 30770: ** 2. if that fails, and this is a lock file (not-conch), try creating
! 30771: ** the parent directories and then try again.
! 30772: ** 3. if that fails, try to open the file read-only
! 30773: ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
! 30774: */
! 30775: pUnused = findReusableFd(path, openFlags);
! 30776: if( pUnused ){
! 30777: fd = pUnused->fd;
! 30778: }else{
! 30779: pUnused = sqlite3_malloc(sizeof(*pUnused));
! 30780: if( !pUnused ){
! 30781: return SQLITE_NOMEM;
! 30782: }
! 30783: }
! 30784: if( fd<0 ){
! 30785: fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
! 30786: terrno = errno;
! 30787: if( fd<0 && errno==ENOENT && islockfile ){
! 30788: if( proxyCreateLockPath(path) == SQLITE_OK ){
! 30789: fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
! 30790: }
! 30791: }
! 30792: }
! 30793: if( fd<0 ){
! 30794: openFlags = O_RDONLY;
! 30795: fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
! 30796: terrno = errno;
! 30797: }
! 30798: if( fd<0 ){
! 30799: if( islockfile ){
! 30800: return SQLITE_BUSY;
! 30801: }
! 30802: switch (terrno) {
! 30803: case EACCES:
! 30804: return SQLITE_PERM;
! 30805: case EIO:
! 30806: return SQLITE_IOERR_LOCK; /* even though it is the conch */
! 30807: default:
! 30808: return SQLITE_CANTOPEN_BKPT;
! 30809: }
! 30810: }
! 30811:
! 30812: pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
! 30813: if( pNew==NULL ){
! 30814: rc = SQLITE_NOMEM;
! 30815: goto end_create_proxy;
! 30816: }
! 30817: memset(pNew, 0, sizeof(unixFile));
! 30818: pNew->openFlags = openFlags;
! 30819: memset(&dummyVfs, 0, sizeof(dummyVfs));
! 30820: dummyVfs.pAppData = (void*)&autolockIoFinder;
! 30821: dummyVfs.zName = "dummy";
! 30822: pUnused->fd = fd;
! 30823: pUnused->flags = openFlags;
! 30824: pNew->pUnused = pUnused;
! 30825:
! 30826: rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
! 30827: if( rc==SQLITE_OK ){
! 30828: *ppFile = pNew;
! 30829: return SQLITE_OK;
! 30830: }
! 30831: end_create_proxy:
! 30832: robust_close(pNew, fd, __LINE__);
! 30833: sqlite3_free(pNew);
! 30834: sqlite3_free(pUnused);
! 30835: return rc;
! 30836: }
! 30837:
! 30838: #ifdef SQLITE_TEST
! 30839: /* simulate multiple hosts by creating unique hostid file paths */
! 30840: SQLITE_API int sqlite3_hostid_num = 0;
! 30841: #endif
! 30842:
! 30843: #define PROXY_HOSTIDLEN 16 /* conch file host id length */
! 30844:
! 30845: /* Not always defined in the headers as it ought to be */
! 30846: extern int gethostuuid(uuid_t id, const struct timespec *wait);
! 30847:
! 30848: /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
! 30849: ** bytes of writable memory.
! 30850: */
! 30851: static int proxyGetHostID(unsigned char *pHostID, int *pError){
! 30852: assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
! 30853: memset(pHostID, 0, PROXY_HOSTIDLEN);
! 30854: #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
! 30855: && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
! 30856: {
! 30857: static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
! 30858: if( gethostuuid(pHostID, &timeout) ){
! 30859: int err = errno;
! 30860: if( pError ){
! 30861: *pError = err;
! 30862: }
! 30863: return SQLITE_IOERR;
! 30864: }
! 30865: }
! 30866: #else
! 30867: UNUSED_PARAMETER(pError);
! 30868: #endif
! 30869: #ifdef SQLITE_TEST
! 30870: /* simulate multiple hosts by creating unique hostid file paths */
! 30871: if( sqlite3_hostid_num != 0){
! 30872: pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
! 30873: }
! 30874: #endif
! 30875:
! 30876: return SQLITE_OK;
! 30877: }
! 30878:
! 30879: /* The conch file contains the header, host id and lock file path
! 30880: */
! 30881: #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
! 30882: #define PROXY_HEADERLEN 1 /* conch file header length */
! 30883: #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
! 30884: #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
! 30885:
! 30886: /*
! 30887: ** Takes an open conch file, copies the contents to a new path and then moves
! 30888: ** it back. The newly created file's file descriptor is assigned to the
! 30889: ** conch file structure and finally the original conch file descriptor is
! 30890: ** closed. Returns zero if successful.
! 30891: */
! 30892: static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
! 30893: proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
! 30894: unixFile *conchFile = pCtx->conchFile;
! 30895: char tPath[MAXPATHLEN];
! 30896: char buf[PROXY_MAXCONCHLEN];
! 30897: char *cPath = pCtx->conchFilePath;
! 30898: size_t readLen = 0;
! 30899: size_t pathLen = 0;
! 30900: char errmsg[64] = "";
! 30901: int fd = -1;
! 30902: int rc = -1;
! 30903: UNUSED_PARAMETER(myHostID);
! 30904:
! 30905: /* create a new path by replace the trailing '-conch' with '-break' */
! 30906: pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
! 30907: if( pathLen>MAXPATHLEN || pathLen<6 ||
! 30908: (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
! 30909: sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
! 30910: goto end_breaklock;
! 30911: }
! 30912: /* read the conch content */
! 30913: readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
! 30914: if( readLen<PROXY_PATHINDEX ){
! 30915: sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
! 30916: goto end_breaklock;
! 30917: }
! 30918: /* write it out to the temporary break file */
! 30919: fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL),
! 30920: SQLITE_DEFAULT_FILE_PERMISSIONS);
! 30921: if( fd<0 ){
! 30922: sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
! 30923: goto end_breaklock;
! 30924: }
! 30925: if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
! 30926: sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
! 30927: goto end_breaklock;
! 30928: }
! 30929: if( rename(tPath, cPath) ){
! 30930: sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
! 30931: goto end_breaklock;
! 30932: }
! 30933: rc = 0;
! 30934: fprintf(stderr, "broke stale lock on %s\n", cPath);
! 30935: robust_close(pFile, conchFile->h, __LINE__);
! 30936: conchFile->h = fd;
! 30937: conchFile->openFlags = O_RDWR | O_CREAT;
! 30938:
! 30939: end_breaklock:
! 30940: if( rc ){
! 30941: if( fd>=0 ){
! 30942: osUnlink(tPath);
! 30943: robust_close(pFile, fd, __LINE__);
! 30944: }
! 30945: fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
! 30946: }
! 30947: return rc;
! 30948: }
! 30949:
! 30950: /* Take the requested lock on the conch file and break a stale lock if the
! 30951: ** host id matches.
! 30952: */
! 30953: static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
! 30954: proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
! 30955: unixFile *conchFile = pCtx->conchFile;
! 30956: int rc = SQLITE_OK;
! 30957: int nTries = 0;
! 30958: struct timespec conchModTime;
! 30959:
! 30960: memset(&conchModTime, 0, sizeof(conchModTime));
! 30961: do {
! 30962: rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
! 30963: nTries ++;
! 30964: if( rc==SQLITE_BUSY ){
! 30965: /* If the lock failed (busy):
! 30966: * 1st try: get the mod time of the conch, wait 0.5s and try again.
! 30967: * 2nd try: fail if the mod time changed or host id is different, wait
! 30968: * 10 sec and try again
! 30969: * 3rd try: break the lock unless the mod time has changed.
! 30970: */
! 30971: struct stat buf;
! 30972: if( osFstat(conchFile->h, &buf) ){
! 30973: pFile->lastErrno = errno;
! 30974: return SQLITE_IOERR_LOCK;
! 30975: }
! 30976:
! 30977: if( nTries==1 ){
! 30978: conchModTime = buf.st_mtimespec;
! 30979: usleep(500000); /* wait 0.5 sec and try the lock again*/
! 30980: continue;
! 30981: }
! 30982:
! 30983: assert( nTries>1 );
! 30984: if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
! 30985: conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
! 30986: return SQLITE_BUSY;
! 30987: }
! 30988:
! 30989: if( nTries==2 ){
! 30990: char tBuf[PROXY_MAXCONCHLEN];
! 30991: int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
! 30992: if( len<0 ){
! 30993: pFile->lastErrno = errno;
! 30994: return SQLITE_IOERR_LOCK;
! 30995: }
! 30996: if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
! 30997: /* don't break the lock if the host id doesn't match */
! 30998: if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
! 30999: return SQLITE_BUSY;
! 31000: }
! 31001: }else{
! 31002: /* don't break the lock on short read or a version mismatch */
! 31003: return SQLITE_BUSY;
! 31004: }
! 31005: usleep(10000000); /* wait 10 sec and try the lock again */
! 31006: continue;
! 31007: }
! 31008:
! 31009: assert( nTries==3 );
! 31010: if( 0==proxyBreakConchLock(pFile, myHostID) ){
! 31011: rc = SQLITE_OK;
! 31012: if( lockType==EXCLUSIVE_LOCK ){
! 31013: rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
! 31014: }
! 31015: if( !rc ){
! 31016: rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
! 31017: }
! 31018: }
! 31019: }
! 31020: } while( rc==SQLITE_BUSY && nTries<3 );
! 31021:
! 31022: return rc;
! 31023: }
! 31024:
! 31025: /* Takes the conch by taking a shared lock and read the contents conch, if
! 31026: ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
! 31027: ** lockPath means that the lockPath in the conch file will be used if the
! 31028: ** host IDs match, or a new lock path will be generated automatically
! 31029: ** and written to the conch file.
! 31030: */
! 31031: static int proxyTakeConch(unixFile *pFile){
! 31032: proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
! 31033:
! 31034: if( pCtx->conchHeld!=0 ){
! 31035: return SQLITE_OK;
! 31036: }else{
! 31037: unixFile *conchFile = pCtx->conchFile;
! 31038: uuid_t myHostID;
! 31039: int pError = 0;
! 31040: char readBuf[PROXY_MAXCONCHLEN];
! 31041: char lockPath[MAXPATHLEN];
! 31042: char *tempLockPath = NULL;
! 31043: int rc = SQLITE_OK;
! 31044: int createConch = 0;
! 31045: int hostIdMatch = 0;
! 31046: int readLen = 0;
! 31047: int tryOldLockPath = 0;
! 31048: int forceNewLockPath = 0;
! 31049:
! 31050: OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
! 31051: (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
! 31052:
! 31053: rc = proxyGetHostID(myHostID, &pError);
! 31054: if( (rc&0xff)==SQLITE_IOERR ){
! 31055: pFile->lastErrno = pError;
! 31056: goto end_takeconch;
! 31057: }
! 31058: rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
! 31059: if( rc!=SQLITE_OK ){
! 31060: goto end_takeconch;
! 31061: }
! 31062: /* read the existing conch file */
! 31063: readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
! 31064: if( readLen<0 ){
! 31065: /* I/O error: lastErrno set by seekAndRead */
! 31066: pFile->lastErrno = conchFile->lastErrno;
! 31067: rc = SQLITE_IOERR_READ;
! 31068: goto end_takeconch;
! 31069: }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
! 31070: readBuf[0]!=(char)PROXY_CONCHVERSION ){
! 31071: /* a short read or version format mismatch means we need to create a new
! 31072: ** conch file.
! 31073: */
! 31074: createConch = 1;
! 31075: }
! 31076: /* if the host id matches and the lock path already exists in the conch
! 31077: ** we'll try to use the path there, if we can't open that path, we'll
! 31078: ** retry with a new auto-generated path
! 31079: */
! 31080: do { /* in case we need to try again for an :auto: named lock file */
! 31081:
! 31082: if( !createConch && !forceNewLockPath ){
! 31083: hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
! 31084: PROXY_HOSTIDLEN);
! 31085: /* if the conch has data compare the contents */
! 31086: if( !pCtx->lockProxyPath ){
! 31087: /* for auto-named local lock file, just check the host ID and we'll
! 31088: ** use the local lock file path that's already in there
! 31089: */
! 31090: if( hostIdMatch ){
! 31091: size_t pathLen = (readLen - PROXY_PATHINDEX);
! 31092:
! 31093: if( pathLen>=MAXPATHLEN ){
! 31094: pathLen=MAXPATHLEN-1;
! 31095: }
! 31096: memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
! 31097: lockPath[pathLen] = 0;
! 31098: tempLockPath = lockPath;
! 31099: tryOldLockPath = 1;
! 31100: /* create a copy of the lock path if the conch is taken */
! 31101: goto end_takeconch;
! 31102: }
! 31103: }else if( hostIdMatch
! 31104: && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
! 31105: readLen-PROXY_PATHINDEX)
! 31106: ){
! 31107: /* conch host and lock path match */
! 31108: goto end_takeconch;
! 31109: }
! 31110: }
! 31111:
! 31112: /* if the conch isn't writable and doesn't match, we can't take it */
! 31113: if( (conchFile->openFlags&O_RDWR) == 0 ){
! 31114: rc = SQLITE_BUSY;
! 31115: goto end_takeconch;
! 31116: }
! 31117:
! 31118: /* either the conch didn't match or we need to create a new one */
! 31119: if( !pCtx->lockProxyPath ){
! 31120: proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
! 31121: tempLockPath = lockPath;
! 31122: /* create a copy of the lock path _only_ if the conch is taken */
! 31123: }
! 31124:
! 31125: /* update conch with host and path (this will fail if other process
! 31126: ** has a shared lock already), if the host id matches, use the big
! 31127: ** stick.
! 31128: */
! 31129: futimes(conchFile->h, NULL);
! 31130: if( hostIdMatch && !createConch ){
! 31131: if( conchFile->pInode && conchFile->pInode->nShared>1 ){
! 31132: /* We are trying for an exclusive lock but another thread in this
! 31133: ** same process is still holding a shared lock. */
! 31134: rc = SQLITE_BUSY;
! 31135: } else {
! 31136: rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
! 31137: }
! 31138: }else{
! 31139: rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
! 31140: }
! 31141: if( rc==SQLITE_OK ){
! 31142: char writeBuffer[PROXY_MAXCONCHLEN];
! 31143: int writeSize = 0;
! 31144:
! 31145: writeBuffer[0] = (char)PROXY_CONCHVERSION;
! 31146: memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
! 31147: if( pCtx->lockProxyPath!=NULL ){
! 31148: strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN);
! 31149: }else{
! 31150: strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
! 31151: }
! 31152: writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
! 31153: robust_ftruncate(conchFile->h, writeSize);
! 31154: rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
! 31155: fsync(conchFile->h);
! 31156: /* If we created a new conch file (not just updated the contents of a
! 31157: ** valid conch file), try to match the permissions of the database
! 31158: */
! 31159: if( rc==SQLITE_OK && createConch ){
! 31160: struct stat buf;
! 31161: int err = osFstat(pFile->h, &buf);
! 31162: if( err==0 ){
! 31163: mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
! 31164: S_IROTH|S_IWOTH);
! 31165: /* try to match the database file R/W permissions, ignore failure */
! 31166: #ifndef SQLITE_PROXY_DEBUG
! 31167: osFchmod(conchFile->h, cmode);
! 31168: #else
! 31169: do{
! 31170: rc = osFchmod(conchFile->h, cmode);
! 31171: }while( rc==(-1) && errno==EINTR );
! 31172: if( rc!=0 ){
! 31173: int code = errno;
! 31174: fprintf(stderr, "fchmod %o FAILED with %d %s\n",
! 31175: cmode, code, strerror(code));
! 31176: } else {
! 31177: fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
! 31178: }
! 31179: }else{
! 31180: int code = errno;
! 31181: fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
! 31182: err, code, strerror(code));
! 31183: #endif
! 31184: }
! 31185: }
! 31186: }
! 31187: conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
! 31188:
! 31189: end_takeconch:
! 31190: OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
! 31191: if( rc==SQLITE_OK && pFile->openFlags ){
! 31192: int fd;
! 31193: if( pFile->h>=0 ){
! 31194: robust_close(pFile, pFile->h, __LINE__);
! 31195: }
! 31196: pFile->h = -1;
! 31197: fd = robust_open(pCtx->dbPath, pFile->openFlags,
! 31198: SQLITE_DEFAULT_FILE_PERMISSIONS);
! 31199: OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
! 31200: if( fd>=0 ){
! 31201: pFile->h = fd;
! 31202: }else{
! 31203: rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
! 31204: during locking */
! 31205: }
! 31206: }
! 31207: if( rc==SQLITE_OK && !pCtx->lockProxy ){
! 31208: char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
! 31209: rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
! 31210: if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
! 31211: /* we couldn't create the proxy lock file with the old lock file path
! 31212: ** so try again via auto-naming
! 31213: */
! 31214: forceNewLockPath = 1;
! 31215: tryOldLockPath = 0;
! 31216: continue; /* go back to the do {} while start point, try again */
! 31217: }
! 31218: }
! 31219: if( rc==SQLITE_OK ){
! 31220: /* Need to make a copy of path if we extracted the value
! 31221: ** from the conch file or the path was allocated on the stack
! 31222: */
! 31223: if( tempLockPath ){
! 31224: pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
! 31225: if( !pCtx->lockProxyPath ){
! 31226: rc = SQLITE_NOMEM;
! 31227: }
! 31228: }
! 31229: }
! 31230: if( rc==SQLITE_OK ){
! 31231: pCtx->conchHeld = 1;
! 31232:
! 31233: if( pCtx->lockProxy->pMethod == &afpIoMethods ){
! 31234: afpLockingContext *afpCtx;
! 31235: afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
! 31236: afpCtx->dbPath = pCtx->lockProxyPath;
! 31237: }
! 31238: } else {
! 31239: conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
! 31240: }
! 31241: OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
! 31242: rc==SQLITE_OK?"ok":"failed"));
! 31243: return rc;
! 31244: } while (1); /* in case we need to retry the :auto: lock file -
! 31245: ** we should never get here except via the 'continue' call. */
! 31246: }
! 31247: }
! 31248:
! 31249: /*
! 31250: ** If pFile holds a lock on a conch file, then release that lock.
! 31251: */
! 31252: static int proxyReleaseConch(unixFile *pFile){
! 31253: int rc = SQLITE_OK; /* Subroutine return code */
! 31254: proxyLockingContext *pCtx; /* The locking context for the proxy lock */
! 31255: unixFile *conchFile; /* Name of the conch file */
! 31256:
! 31257: pCtx = (proxyLockingContext *)pFile->lockingContext;
! 31258: conchFile = pCtx->conchFile;
! 31259: OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
! 31260: (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
! 31261: getpid()));
! 31262: if( pCtx->conchHeld>0 ){
! 31263: rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
! 31264: }
! 31265: pCtx->conchHeld = 0;
! 31266: OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
! 31267: (rc==SQLITE_OK ? "ok" : "failed")));
! 31268: return rc;
! 31269: }
! 31270:
! 31271: /*
! 31272: ** Given the name of a database file, compute the name of its conch file.
! 31273: ** Store the conch filename in memory obtained from sqlite3_malloc().
! 31274: ** Make *pConchPath point to the new name. Return SQLITE_OK on success
! 31275: ** or SQLITE_NOMEM if unable to obtain memory.
! 31276: **
! 31277: ** The caller is responsible for ensuring that the allocated memory
! 31278: ** space is eventually freed.
! 31279: **
! 31280: ** *pConchPath is set to NULL if a memory allocation error occurs.
! 31281: */
! 31282: static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
! 31283: int i; /* Loop counter */
! 31284: int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
! 31285: char *conchPath; /* buffer in which to construct conch name */
! 31286:
! 31287: /* Allocate space for the conch filename and initialize the name to
! 31288: ** the name of the original database file. */
! 31289: *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
! 31290: if( conchPath==0 ){
! 31291: return SQLITE_NOMEM;
! 31292: }
! 31293: memcpy(conchPath, dbPath, len+1);
! 31294:
! 31295: /* now insert a "." before the last / character */
! 31296: for( i=(len-1); i>=0; i-- ){
! 31297: if( conchPath[i]=='/' ){
! 31298: i++;
! 31299: break;
! 31300: }
! 31301: }
! 31302: conchPath[i]='.';
! 31303: while ( i<len ){
! 31304: conchPath[i+1]=dbPath[i];
! 31305: i++;
! 31306: }
! 31307:
! 31308: /* append the "-conch" suffix to the file */
! 31309: memcpy(&conchPath[i+1], "-conch", 7);
! 31310: assert( (int)strlen(conchPath) == len+7 );
! 31311:
! 31312: return SQLITE_OK;
! 31313: }
! 31314:
! 31315:
! 31316: /* Takes a fully configured proxy locking-style unix file and switches
! 31317: ** the local lock file path
! 31318: */
! 31319: static int switchLockProxyPath(unixFile *pFile, const char *path) {
! 31320: proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
! 31321: char *oldPath = pCtx->lockProxyPath;
! 31322: int rc = SQLITE_OK;
! 31323:
! 31324: if( pFile->eFileLock!=NO_LOCK ){
! 31325: return SQLITE_BUSY;
! 31326: }
! 31327:
! 31328: /* nothing to do if the path is NULL, :auto: or matches the existing path */
! 31329: if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
! 31330: (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
! 31331: return SQLITE_OK;
! 31332: }else{
! 31333: unixFile *lockProxy = pCtx->lockProxy;
! 31334: pCtx->lockProxy=NULL;
! 31335: pCtx->conchHeld = 0;
! 31336: if( lockProxy!=NULL ){
! 31337: rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
! 31338: if( rc ) return rc;
! 31339: sqlite3_free(lockProxy);
! 31340: }
! 31341: sqlite3_free(oldPath);
! 31342: pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
! 31343: }
! 31344:
! 31345: return rc;
! 31346: }
! 31347:
! 31348: /*
! 31349: ** pFile is a file that has been opened by a prior xOpen call. dbPath
! 31350: ** is a string buffer at least MAXPATHLEN+1 characters in size.
! 31351: **
! 31352: ** This routine find the filename associated with pFile and writes it
! 31353: ** int dbPath.
! 31354: */
! 31355: static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
! 31356: #if defined(__APPLE__)
! 31357: if( pFile->pMethod == &afpIoMethods ){
! 31358: /* afp style keeps a reference to the db path in the filePath field
! 31359: ** of the struct */
! 31360: assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
! 31361: strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
! 31362: } else
! 31363: #endif
! 31364: if( pFile->pMethod == &dotlockIoMethods ){
! 31365: /* dot lock style uses the locking context to store the dot lock
! 31366: ** file path */
! 31367: int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
! 31368: memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
! 31369: }else{
! 31370: /* all other styles use the locking context to store the db file path */
! 31371: assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
! 31372: strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
! 31373: }
! 31374: return SQLITE_OK;
! 31375: }
! 31376:
! 31377: /*
! 31378: ** Takes an already filled in unix file and alters it so all file locking
! 31379: ** will be performed on the local proxy lock file. The following fields
! 31380: ** are preserved in the locking context so that they can be restored and
! 31381: ** the unix structure properly cleaned up at close time:
! 31382: ** ->lockingContext
! 31383: ** ->pMethod
! 31384: */
! 31385: static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
! 31386: proxyLockingContext *pCtx;
! 31387: char dbPath[MAXPATHLEN+1]; /* Name of the database file */
! 31388: char *lockPath=NULL;
! 31389: int rc = SQLITE_OK;
! 31390:
! 31391: if( pFile->eFileLock!=NO_LOCK ){
! 31392: return SQLITE_BUSY;
! 31393: }
! 31394: proxyGetDbPathForUnixFile(pFile, dbPath);
! 31395: if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
! 31396: lockPath=NULL;
! 31397: }else{
! 31398: lockPath=(char *)path;
! 31399: }
! 31400:
! 31401: OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
! 31402: (lockPath ? lockPath : ":auto:"), getpid()));
! 31403:
! 31404: pCtx = sqlite3_malloc( sizeof(*pCtx) );
! 31405: if( pCtx==0 ){
! 31406: return SQLITE_NOMEM;
! 31407: }
! 31408: memset(pCtx, 0, sizeof(*pCtx));
! 31409:
! 31410: rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
! 31411: if( rc==SQLITE_OK ){
! 31412: rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
! 31413: if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
! 31414: /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
! 31415: ** (c) the file system is read-only, then enable no-locking access.
! 31416: ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
! 31417: ** that openFlags will have only one of O_RDONLY or O_RDWR.
! 31418: */
! 31419: struct statfs fsInfo;
! 31420: struct stat conchInfo;
! 31421: int goLockless = 0;
! 31422:
! 31423: if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
! 31424: int err = errno;
! 31425: if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
! 31426: goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
! 31427: }
! 31428: }
! 31429: if( goLockless ){
! 31430: pCtx->conchHeld = -1; /* read only FS/ lockless */
! 31431: rc = SQLITE_OK;
! 31432: }
! 31433: }
! 31434: }
! 31435: if( rc==SQLITE_OK && lockPath ){
! 31436: pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
! 31437: }
! 31438:
! 31439: if( rc==SQLITE_OK ){
! 31440: pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
! 31441: if( pCtx->dbPath==NULL ){
! 31442: rc = SQLITE_NOMEM;
! 31443: }
! 31444: }
! 31445: if( rc==SQLITE_OK ){
! 31446: /* all memory is allocated, proxys are created and assigned,
! 31447: ** switch the locking context and pMethod then return.
! 31448: */
! 31449: pCtx->oldLockingContext = pFile->lockingContext;
! 31450: pFile->lockingContext = pCtx;
! 31451: pCtx->pOldMethod = pFile->pMethod;
! 31452: pFile->pMethod = &proxyIoMethods;
! 31453: }else{
! 31454: if( pCtx->conchFile ){
! 31455: pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
! 31456: sqlite3_free(pCtx->conchFile);
! 31457: }
! 31458: sqlite3DbFree(0, pCtx->lockProxyPath);
! 31459: sqlite3_free(pCtx->conchFilePath);
! 31460: sqlite3_free(pCtx);
! 31461: }
! 31462: OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
! 31463: (rc==SQLITE_OK ? "ok" : "failed")));
! 31464: return rc;
! 31465: }
! 31466:
! 31467:
! 31468: /*
! 31469: ** This routine handles sqlite3_file_control() calls that are specific
! 31470: ** to proxy locking.
! 31471: */
! 31472: static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
! 31473: switch( op ){
! 31474: case SQLITE_GET_LOCKPROXYFILE: {
! 31475: unixFile *pFile = (unixFile*)id;
! 31476: if( pFile->pMethod == &proxyIoMethods ){
! 31477: proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
! 31478: proxyTakeConch(pFile);
! 31479: if( pCtx->lockProxyPath ){
! 31480: *(const char **)pArg = pCtx->lockProxyPath;
! 31481: }else{
! 31482: *(const char **)pArg = ":auto: (not held)";
! 31483: }
! 31484: } else {
! 31485: *(const char **)pArg = NULL;
! 31486: }
! 31487: return SQLITE_OK;
! 31488: }
! 31489: case SQLITE_SET_LOCKPROXYFILE: {
! 31490: unixFile *pFile = (unixFile*)id;
! 31491: int rc = SQLITE_OK;
! 31492: int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
! 31493: if( pArg==NULL || (const char *)pArg==0 ){
! 31494: if( isProxyStyle ){
! 31495: /* turn off proxy locking - not supported */
! 31496: rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
! 31497: }else{
! 31498: /* turn off proxy locking - already off - NOOP */
! 31499: rc = SQLITE_OK;
! 31500: }
! 31501: }else{
! 31502: const char *proxyPath = (const char *)pArg;
! 31503: if( isProxyStyle ){
! 31504: proxyLockingContext *pCtx =
! 31505: (proxyLockingContext*)pFile->lockingContext;
! 31506: if( !strcmp(pArg, ":auto:")
! 31507: || (pCtx->lockProxyPath &&
! 31508: !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
! 31509: ){
! 31510: rc = SQLITE_OK;
! 31511: }else{
! 31512: rc = switchLockProxyPath(pFile, proxyPath);
! 31513: }
! 31514: }else{
! 31515: /* turn on proxy file locking */
! 31516: rc = proxyTransformUnixFile(pFile, proxyPath);
! 31517: }
! 31518: }
! 31519: return rc;
! 31520: }
! 31521: default: {
! 31522: assert( 0 ); /* The call assures that only valid opcodes are sent */
! 31523: }
! 31524: }
! 31525: /*NOTREACHED*/
! 31526: return SQLITE_ERROR;
! 31527: }
! 31528:
! 31529: /*
! 31530: ** Within this division (the proxying locking implementation) the procedures
! 31531: ** above this point are all utilities. The lock-related methods of the
! 31532: ** proxy-locking sqlite3_io_method object follow.
! 31533: */
! 31534:
! 31535:
! 31536: /*
! 31537: ** This routine checks if there is a RESERVED lock held on the specified
! 31538: ** file by this or any other process. If such a lock is held, set *pResOut
! 31539: ** to a non-zero value otherwise *pResOut is set to zero. The return value
! 31540: ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
! 31541: */
! 31542: static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
! 31543: unixFile *pFile = (unixFile*)id;
! 31544: int rc = proxyTakeConch(pFile);
! 31545: if( rc==SQLITE_OK ){
! 31546: proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
! 31547: if( pCtx->conchHeld>0 ){
! 31548: unixFile *proxy = pCtx->lockProxy;
! 31549: return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
! 31550: }else{ /* conchHeld < 0 is lockless */
! 31551: pResOut=0;
! 31552: }
! 31553: }
! 31554: return rc;
! 31555: }
! 31556:
! 31557: /*
! 31558: ** Lock the file with the lock specified by parameter eFileLock - one
! 31559: ** of the following:
! 31560: **
! 31561: ** (1) SHARED_LOCK
! 31562: ** (2) RESERVED_LOCK
! 31563: ** (3) PENDING_LOCK
! 31564: ** (4) EXCLUSIVE_LOCK
! 31565: **
! 31566: ** Sometimes when requesting one lock state, additional lock states
! 31567: ** are inserted in between. The locking might fail on one of the later
! 31568: ** transitions leaving the lock state different from what it started but
! 31569: ** still short of its goal. The following chart shows the allowed
! 31570: ** transitions and the inserted intermediate states:
! 31571: **
! 31572: ** UNLOCKED -> SHARED
! 31573: ** SHARED -> RESERVED
! 31574: ** SHARED -> (PENDING) -> EXCLUSIVE
! 31575: ** RESERVED -> (PENDING) -> EXCLUSIVE
! 31576: ** PENDING -> EXCLUSIVE
! 31577: **
! 31578: ** This routine will only increase a lock. Use the sqlite3OsUnlock()
! 31579: ** routine to lower a locking level.
! 31580: */
! 31581: static int proxyLock(sqlite3_file *id, int eFileLock) {
! 31582: unixFile *pFile = (unixFile*)id;
! 31583: int rc = proxyTakeConch(pFile);
! 31584: if( rc==SQLITE_OK ){
! 31585: proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
! 31586: if( pCtx->conchHeld>0 ){
! 31587: unixFile *proxy = pCtx->lockProxy;
! 31588: rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
! 31589: pFile->eFileLock = proxy->eFileLock;
! 31590: }else{
! 31591: /* conchHeld < 0 is lockless */
! 31592: }
! 31593: }
! 31594: return rc;
! 31595: }
! 31596:
! 31597:
! 31598: /*
! 31599: ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
! 31600: ** must be either NO_LOCK or SHARED_LOCK.
! 31601: **
! 31602: ** If the locking level of the file descriptor is already at or below
! 31603: ** the requested locking level, this routine is a no-op.
! 31604: */
! 31605: static int proxyUnlock(sqlite3_file *id, int eFileLock) {
! 31606: unixFile *pFile = (unixFile*)id;
! 31607: int rc = proxyTakeConch(pFile);
! 31608: if( rc==SQLITE_OK ){
! 31609: proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
! 31610: if( pCtx->conchHeld>0 ){
! 31611: unixFile *proxy = pCtx->lockProxy;
! 31612: rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
! 31613: pFile->eFileLock = proxy->eFileLock;
! 31614: }else{
! 31615: /* conchHeld < 0 is lockless */
! 31616: }
! 31617: }
! 31618: return rc;
! 31619: }
! 31620:
! 31621: /*
! 31622: ** Close a file that uses proxy locks.
! 31623: */
! 31624: static int proxyClose(sqlite3_file *id) {
! 31625: if( id ){
! 31626: unixFile *pFile = (unixFile*)id;
! 31627: proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
! 31628: unixFile *lockProxy = pCtx->lockProxy;
! 31629: unixFile *conchFile = pCtx->conchFile;
! 31630: int rc = SQLITE_OK;
! 31631:
! 31632: if( lockProxy ){
! 31633: rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
! 31634: if( rc ) return rc;
! 31635: rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
! 31636: if( rc ) return rc;
! 31637: sqlite3_free(lockProxy);
! 31638: pCtx->lockProxy = 0;
! 31639: }
! 31640: if( conchFile ){
! 31641: if( pCtx->conchHeld ){
! 31642: rc = proxyReleaseConch(pFile);
! 31643: if( rc ) return rc;
! 31644: }
! 31645: rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
! 31646: if( rc ) return rc;
! 31647: sqlite3_free(conchFile);
! 31648: }
! 31649: sqlite3DbFree(0, pCtx->lockProxyPath);
! 31650: sqlite3_free(pCtx->conchFilePath);
! 31651: sqlite3DbFree(0, pCtx->dbPath);
! 31652: /* restore the original locking context and pMethod then close it */
! 31653: pFile->lockingContext = pCtx->oldLockingContext;
! 31654: pFile->pMethod = pCtx->pOldMethod;
! 31655: sqlite3_free(pCtx);
! 31656: return pFile->pMethod->xClose(id);
! 31657: }
! 31658: return SQLITE_OK;
! 31659: }
! 31660:
! 31661:
! 31662:
! 31663: #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
! 31664: /*
! 31665: ** The proxy locking style is intended for use with AFP filesystems.
! 31666: ** And since AFP is only supported on MacOSX, the proxy locking is also
! 31667: ** restricted to MacOSX.
! 31668: **
! 31669: **
! 31670: ******************* End of the proxy lock implementation **********************
! 31671: ******************************************************************************/
! 31672:
! 31673: /*
! 31674: ** Initialize the operating system interface.
! 31675: **
! 31676: ** This routine registers all VFS implementations for unix-like operating
! 31677: ** systems. This routine, and the sqlite3_os_end() routine that follows,
! 31678: ** should be the only routines in this file that are visible from other
! 31679: ** files.
! 31680: **
! 31681: ** This routine is called once during SQLite initialization and by a
! 31682: ** single thread. The memory allocation and mutex subsystems have not
! 31683: ** necessarily been initialized when this routine is called, and so they
! 31684: ** should not be used.
! 31685: */
! 31686: SQLITE_API int sqlite3_os_init(void){
! 31687: /*
! 31688: ** The following macro defines an initializer for an sqlite3_vfs object.
! 31689: ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
! 31690: ** to the "finder" function. (pAppData is a pointer to a pointer because
! 31691: ** silly C90 rules prohibit a void* from being cast to a function pointer
! 31692: ** and so we have to go through the intermediate pointer to avoid problems
! 31693: ** when compiling with -pedantic-errors on GCC.)
! 31694: **
! 31695: ** The FINDER parameter to this macro is the name of the pointer to the
! 31696: ** finder-function. The finder-function returns a pointer to the
! 31697: ** sqlite_io_methods object that implements the desired locking
! 31698: ** behaviors. See the division above that contains the IOMETHODS
! 31699: ** macro for addition information on finder-functions.
! 31700: **
! 31701: ** Most finders simply return a pointer to a fixed sqlite3_io_methods
! 31702: ** object. But the "autolockIoFinder" available on MacOSX does a little
! 31703: ** more than that; it looks at the filesystem type that hosts the
! 31704: ** database file and tries to choose an locking method appropriate for
! 31705: ** that filesystem time.
! 31706: */
! 31707: #define UNIXVFS(VFSNAME, FINDER) { \
! 31708: 3, /* iVersion */ \
! 31709: sizeof(unixFile), /* szOsFile */ \
! 31710: MAX_PATHNAME, /* mxPathname */ \
! 31711: 0, /* pNext */ \
! 31712: VFSNAME, /* zName */ \
! 31713: (void*)&FINDER, /* pAppData */ \
! 31714: unixOpen, /* xOpen */ \
! 31715: unixDelete, /* xDelete */ \
! 31716: unixAccess, /* xAccess */ \
! 31717: unixFullPathname, /* xFullPathname */ \
! 31718: unixDlOpen, /* xDlOpen */ \
! 31719: unixDlError, /* xDlError */ \
! 31720: unixDlSym, /* xDlSym */ \
! 31721: unixDlClose, /* xDlClose */ \
! 31722: unixRandomness, /* xRandomness */ \
! 31723: unixSleep, /* xSleep */ \
! 31724: unixCurrentTime, /* xCurrentTime */ \
! 31725: unixGetLastError, /* xGetLastError */ \
! 31726: unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
! 31727: unixSetSystemCall, /* xSetSystemCall */ \
! 31728: unixGetSystemCall, /* xGetSystemCall */ \
! 31729: unixNextSystemCall, /* xNextSystemCall */ \
! 31730: }
! 31731:
! 31732: /*
! 31733: ** All default VFSes for unix are contained in the following array.
! 31734: **
! 31735: ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
! 31736: ** by the SQLite core when the VFS is registered. So the following
! 31737: ** array cannot be const.
! 31738: */
! 31739: static sqlite3_vfs aVfs[] = {
! 31740: #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
! 31741: UNIXVFS("unix", autolockIoFinder ),
! 31742: #else
! 31743: UNIXVFS("unix", posixIoFinder ),
! 31744: #endif
! 31745: UNIXVFS("unix-none", nolockIoFinder ),
! 31746: UNIXVFS("unix-dotfile", dotlockIoFinder ),
! 31747: UNIXVFS("unix-excl", posixIoFinder ),
! 31748: #if OS_VXWORKS
! 31749: UNIXVFS("unix-namedsem", semIoFinder ),
! 31750: #endif
! 31751: #if SQLITE_ENABLE_LOCKING_STYLE
! 31752: UNIXVFS("unix-posix", posixIoFinder ),
! 31753: #if !OS_VXWORKS
! 31754: UNIXVFS("unix-flock", flockIoFinder ),
! 31755: #endif
! 31756: #endif
! 31757: #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
! 31758: UNIXVFS("unix-afp", afpIoFinder ),
! 31759: UNIXVFS("unix-nfs", nfsIoFinder ),
! 31760: UNIXVFS("unix-proxy", proxyIoFinder ),
! 31761: #endif
! 31762: };
! 31763: unsigned int i; /* Loop counter */
! 31764:
! 31765: /* Double-check that the aSyscall[] array has been constructed
! 31766: ** correctly. See ticket [bb3a86e890c8e96ab] */
! 31767: assert( ArraySize(aSyscall)==20 );
! 31768:
! 31769: /* Register all VFSes defined in the aVfs[] array */
! 31770: for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
! 31771: sqlite3_vfs_register(&aVfs[i], i==0);
! 31772: }
! 31773: return SQLITE_OK;
! 31774: }
! 31775:
! 31776: /*
! 31777: ** Shutdown the operating system interface.
! 31778: **
! 31779: ** Some operating systems might need to do some cleanup in this routine,
! 31780: ** to release dynamically allocated objects. But not on unix.
! 31781: ** This routine is a no-op for unix.
! 31782: */
! 31783: SQLITE_API int sqlite3_os_end(void){
! 31784: return SQLITE_OK;
! 31785: }
! 31786:
! 31787: #endif /* SQLITE_OS_UNIX */
! 31788:
! 31789: /************** End of os_unix.c *********************************************/
! 31790: /************** Begin file os_win.c ******************************************/
! 31791: /*
! 31792: ** 2004 May 22
! 31793: **
! 31794: ** The author disclaims copyright to this source code. In place of
! 31795: ** a legal notice, here is a blessing:
! 31796: **
! 31797: ** May you do good and not evil.
! 31798: ** May you find forgiveness for yourself and forgive others.
! 31799: ** May you share freely, never taking more than you give.
! 31800: **
! 31801: ******************************************************************************
! 31802: **
! 31803: ** This file contains code that is specific to Windows.
! 31804: */
! 31805: #if SQLITE_OS_WIN /* This file is used for Windows only */
! 31806:
! 31807: #ifdef __CYGWIN__
! 31808: # include <sys/cygwin.h>
! 31809: #endif
! 31810:
! 31811: /*
! 31812: ** Include code that is common to all os_*.c files
! 31813: */
! 31814: /************** Include os_common.h in the middle of os_win.c ****************/
! 31815: /************** Begin file os_common.h ***************************************/
! 31816: /*
! 31817: ** 2004 May 22
! 31818: **
! 31819: ** The author disclaims copyright to this source code. In place of
! 31820: ** a legal notice, here is a blessing:
! 31821: **
! 31822: ** May you do good and not evil.
! 31823: ** May you find forgiveness for yourself and forgive others.
! 31824: ** May you share freely, never taking more than you give.
! 31825: **
! 31826: ******************************************************************************
! 31827: **
! 31828: ** This file contains macros and a little bit of code that is common to
! 31829: ** all of the platform-specific files (os_*.c) and is #included into those
! 31830: ** files.
! 31831: **
! 31832: ** This file should be #included by the os_*.c files only. It is not a
! 31833: ** general purpose header file.
! 31834: */
! 31835: #ifndef _OS_COMMON_H_
! 31836: #define _OS_COMMON_H_
! 31837:
! 31838: /*
! 31839: ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
! 31840: ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
! 31841: ** switch. The following code should catch this problem at compile-time.
! 31842: */
! 31843: #ifdef MEMORY_DEBUG
! 31844: # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
! 31845: #endif
! 31846:
! 31847: #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
! 31848: # ifndef SQLITE_DEBUG_OS_TRACE
! 31849: # define SQLITE_DEBUG_OS_TRACE 0
! 31850: # endif
! 31851: int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
! 31852: # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
! 31853: #else
! 31854: # define OSTRACE(X)
! 31855: #endif
! 31856:
! 31857: /*
! 31858: ** Macros for performance tracing. Normally turned off. Only works
! 31859: ** on i486 hardware.
! 31860: */
! 31861: #ifdef SQLITE_PERFORMANCE_TRACE
! 31862:
! 31863: /*
! 31864: ** hwtime.h contains inline assembler code for implementing
! 31865: ** high-performance timing routines.
! 31866: */
! 31867: /************** Include hwtime.h in the middle of os_common.h ****************/
! 31868: /************** Begin file hwtime.h ******************************************/
! 31869: /*
! 31870: ** 2008 May 27
! 31871: **
! 31872: ** The author disclaims copyright to this source code. In place of
! 31873: ** a legal notice, here is a blessing:
! 31874: **
! 31875: ** May you do good and not evil.
! 31876: ** May you find forgiveness for yourself and forgive others.
! 31877: ** May you share freely, never taking more than you give.
! 31878: **
! 31879: ******************************************************************************
! 31880: **
! 31881: ** This file contains inline asm code for retrieving "high-performance"
! 31882: ** counters for x86 class CPUs.
! 31883: */
! 31884: #ifndef _HWTIME_H_
! 31885: #define _HWTIME_H_
! 31886:
! 31887: /*
! 31888: ** The following routine only works on pentium-class (or newer) processors.
! 31889: ** It uses the RDTSC opcode to read the cycle count value out of the
! 31890: ** processor and returns that value. This can be used for high-res
! 31891: ** profiling.
! 31892: */
! 31893: #if (defined(__GNUC__) || defined(_MSC_VER)) && \
! 31894: (defined(i386) || defined(__i386__) || defined(_M_IX86))
! 31895:
! 31896: #if defined(__GNUC__)
! 31897:
! 31898: __inline__ sqlite_uint64 sqlite3Hwtime(void){
! 31899: unsigned int lo, hi;
! 31900: __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
! 31901: return (sqlite_uint64)hi << 32 | lo;
! 31902: }
! 31903:
! 31904: #elif defined(_MSC_VER)
! 31905:
! 31906: __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
! 31907: __asm {
! 31908: rdtsc
! 31909: ret ; return value at EDX:EAX
! 31910: }
! 31911: }
! 31912:
! 31913: #endif
! 31914:
! 31915: #elif (defined(__GNUC__) && defined(__x86_64__))
! 31916:
! 31917: __inline__ sqlite_uint64 sqlite3Hwtime(void){
! 31918: unsigned long val;
! 31919: __asm__ __volatile__ ("rdtsc" : "=A" (val));
! 31920: return val;
! 31921: }
! 31922:
! 31923: #elif (defined(__GNUC__) && defined(__ppc__))
! 31924:
! 31925: __inline__ sqlite_uint64 sqlite3Hwtime(void){
! 31926: unsigned long long retval;
! 31927: unsigned long junk;
! 31928: __asm__ __volatile__ ("\n\
! 31929: 1: mftbu %1\n\
! 31930: mftb %L0\n\
! 31931: mftbu %0\n\
! 31932: cmpw %0,%1\n\
! 31933: bne 1b"
! 31934: : "=r" (retval), "=r" (junk));
! 31935: return retval;
! 31936: }
! 31937:
! 31938: #else
! 31939:
! 31940: #error Need implementation of sqlite3Hwtime() for your platform.
! 31941:
! 31942: /*
! 31943: ** To compile without implementing sqlite3Hwtime() for your platform,
! 31944: ** you can remove the above #error and use the following
! 31945: ** stub function. You will lose timing support for many
! 31946: ** of the debugging and testing utilities, but it should at
! 31947: ** least compile and run.
! 31948: */
! 31949: SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
! 31950:
! 31951: #endif
! 31952:
! 31953: #endif /* !defined(_HWTIME_H_) */
! 31954:
! 31955: /************** End of hwtime.h **********************************************/
! 31956: /************** Continuing where we left off in os_common.h ******************/
! 31957:
! 31958: static sqlite_uint64 g_start;
! 31959: static sqlite_uint64 g_elapsed;
! 31960: #define TIMER_START g_start=sqlite3Hwtime()
! 31961: #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
! 31962: #define TIMER_ELAPSED g_elapsed
! 31963: #else
! 31964: #define TIMER_START
! 31965: #define TIMER_END
! 31966: #define TIMER_ELAPSED ((sqlite_uint64)0)
! 31967: #endif
! 31968:
! 31969: /*
! 31970: ** If we compile with the SQLITE_TEST macro set, then the following block
! 31971: ** of code will give us the ability to simulate a disk I/O error. This
! 31972: ** is used for testing the I/O recovery logic.
! 31973: */
! 31974: #ifdef SQLITE_TEST
! 31975: SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
! 31976: SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
! 31977: SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
! 31978: SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
! 31979: SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
! 31980: SQLITE_API int sqlite3_diskfull_pending = 0;
! 31981: SQLITE_API int sqlite3_diskfull = 0;
! 31982: #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
! 31983: #define SimulateIOError(CODE) \
! 31984: if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
! 31985: || sqlite3_io_error_pending-- == 1 ) \
! 31986: { local_ioerr(); CODE; }
! 31987: static void local_ioerr(){
! 31988: IOTRACE(("IOERR\n"));
! 31989: sqlite3_io_error_hit++;
! 31990: if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
! 31991: }
! 31992: #define SimulateDiskfullError(CODE) \
! 31993: if( sqlite3_diskfull_pending ){ \
! 31994: if( sqlite3_diskfull_pending == 1 ){ \
! 31995: local_ioerr(); \
! 31996: sqlite3_diskfull = 1; \
! 31997: sqlite3_io_error_hit = 1; \
! 31998: CODE; \
! 31999: }else{ \
! 32000: sqlite3_diskfull_pending--; \
! 32001: } \
! 32002: }
! 32003: #else
! 32004: #define SimulateIOErrorBenign(X)
! 32005: #define SimulateIOError(A)
! 32006: #define SimulateDiskfullError(A)
! 32007: #endif
! 32008:
! 32009: /*
! 32010: ** When testing, keep a count of the number of open files.
! 32011: */
! 32012: #ifdef SQLITE_TEST
! 32013: SQLITE_API int sqlite3_open_file_count = 0;
! 32014: #define OpenCounter(X) sqlite3_open_file_count+=(X)
! 32015: #else
! 32016: #define OpenCounter(X)
! 32017: #endif
! 32018:
! 32019: #endif /* !defined(_OS_COMMON_H_) */
! 32020:
! 32021: /************** End of os_common.h *******************************************/
! 32022: /************** Continuing where we left off in os_win.c *********************/
! 32023:
! 32024: /*
! 32025: ** Some Microsoft compilers lack this definition.
! 32026: */
! 32027: #ifndef INVALID_FILE_ATTRIBUTES
! 32028: # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
! 32029: #endif
! 32030:
! 32031: /* Forward references */
! 32032: typedef struct winShm winShm; /* A connection to shared-memory */
! 32033: typedef struct winShmNode winShmNode; /* A region of shared-memory */
! 32034:
! 32035: /*
! 32036: ** WinCE lacks native support for file locking so we have to fake it
! 32037: ** with some code of our own.
! 32038: */
! 32039: #if SQLITE_OS_WINCE
! 32040: typedef struct winceLock {
! 32041: int nReaders; /* Number of reader locks obtained */
! 32042: BOOL bPending; /* Indicates a pending lock has been obtained */
! 32043: BOOL bReserved; /* Indicates a reserved lock has been obtained */
! 32044: BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
! 32045: } winceLock;
! 32046: #endif
! 32047:
! 32048: /*
! 32049: ** The winFile structure is a subclass of sqlite3_file* specific to the win32
! 32050: ** portability layer.
! 32051: */
! 32052: typedef struct winFile winFile;
! 32053: struct winFile {
! 32054: const sqlite3_io_methods *pMethod; /*** Must be first ***/
! 32055: sqlite3_vfs *pVfs; /* The VFS used to open this file */
! 32056: HANDLE h; /* Handle for accessing the file */
! 32057: u8 locktype; /* Type of lock currently held on this file */
! 32058: short sharedLockByte; /* Randomly chosen byte used as a shared lock */
! 32059: u8 ctrlFlags; /* Flags. See WINFILE_* below */
! 32060: DWORD lastErrno; /* The Windows errno from the last I/O error */
! 32061: winShm *pShm; /* Instance of shared memory on this file */
! 32062: const char *zPath; /* Full pathname of this file */
! 32063: int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
! 32064: #if SQLITE_OS_WINCE
! 32065: LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
! 32066: HANDLE hMutex; /* Mutex used to control access to shared lock */
! 32067: HANDLE hShared; /* Shared memory segment used for locking */
! 32068: winceLock local; /* Locks obtained by this instance of winFile */
! 32069: winceLock *shared; /* Global shared lock memory for the file */
! 32070: #endif
! 32071: };
! 32072:
! 32073: /*
! 32074: ** Allowed values for winFile.ctrlFlags
! 32075: */
! 32076: #define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
! 32077: #define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
! 32078:
! 32079: /*
! 32080: * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
! 32081: * various Win32 API heap functions instead of our own.
! 32082: */
! 32083: #ifdef SQLITE_WIN32_MALLOC
! 32084: /*
! 32085: * The initial size of the Win32-specific heap. This value may be zero.
! 32086: */
! 32087: #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
! 32088: # define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
! 32089: (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
! 32090: #endif
! 32091:
! 32092: /*
! 32093: * The maximum size of the Win32-specific heap. This value may be zero.
! 32094: */
! 32095: #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
! 32096: # define SQLITE_WIN32_HEAP_MAX_SIZE (0)
! 32097: #endif
! 32098:
! 32099: /*
! 32100: * The extra flags to use in calls to the Win32 heap APIs. This value may be
! 32101: * zero for the default behavior.
! 32102: */
! 32103: #ifndef SQLITE_WIN32_HEAP_FLAGS
! 32104: # define SQLITE_WIN32_HEAP_FLAGS (0)
! 32105: #endif
! 32106:
! 32107: /*
! 32108: ** The winMemData structure stores information required by the Win32-specific
! 32109: ** sqlite3_mem_methods implementation.
! 32110: */
! 32111: typedef struct winMemData winMemData;
! 32112: struct winMemData {
! 32113: #ifndef NDEBUG
! 32114: u32 magic; /* Magic number to detect structure corruption. */
! 32115: #endif
! 32116: HANDLE hHeap; /* The handle to our heap. */
! 32117: BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
! 32118: };
! 32119:
! 32120: #ifndef NDEBUG
! 32121: #define WINMEM_MAGIC 0x42b2830b
! 32122: #endif
! 32123:
! 32124: static struct winMemData win_mem_data = {
! 32125: #ifndef NDEBUG
! 32126: WINMEM_MAGIC,
! 32127: #endif
! 32128: NULL, FALSE
! 32129: };
! 32130:
! 32131: #ifndef NDEBUG
! 32132: #define winMemAssertMagic() assert( win_mem_data.magic==WINMEM_MAGIC )
! 32133: #else
! 32134: #define winMemAssertMagic()
! 32135: #endif
! 32136:
! 32137: #define winMemGetHeap() win_mem_data.hHeap
! 32138:
! 32139: static void *winMemMalloc(int nBytes);
! 32140: static void winMemFree(void *pPrior);
! 32141: static void *winMemRealloc(void *pPrior, int nBytes);
! 32142: static int winMemSize(void *p);
! 32143: static int winMemRoundup(int n);
! 32144: static int winMemInit(void *pAppData);
! 32145: static void winMemShutdown(void *pAppData);
! 32146:
! 32147: SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
! 32148: #endif /* SQLITE_WIN32_MALLOC */
! 32149:
! 32150: /*
! 32151: ** The following variable is (normally) set once and never changes
! 32152: ** thereafter. It records whether the operating system is Win9x
! 32153: ** or WinNT.
! 32154: **
! 32155: ** 0: Operating system unknown.
! 32156: ** 1: Operating system is Win9x.
! 32157: ** 2: Operating system is WinNT.
! 32158: **
! 32159: ** In order to facilitate testing on a WinNT system, the test fixture
! 32160: ** can manually set this value to 1 to emulate Win98 behavior.
! 32161: */
! 32162: #ifdef SQLITE_TEST
! 32163: SQLITE_API int sqlite3_os_type = 0;
! 32164: #else
! 32165: static int sqlite3_os_type = 0;
! 32166: #endif
! 32167:
! 32168: /*
! 32169: ** Many system calls are accessed through pointer-to-functions so that
! 32170: ** they may be overridden at runtime to facilitate fault injection during
! 32171: ** testing and sandboxing. The following array holds the names and pointers
! 32172: ** to all overrideable system calls.
! 32173: */
! 32174: #if !SQLITE_OS_WINCE
! 32175: # define SQLITE_WIN32_HAS_ANSI
! 32176: #endif
! 32177:
! 32178: #if SQLITE_OS_WINCE || SQLITE_OS_WINNT
! 32179: # define SQLITE_WIN32_HAS_WIDE
! 32180: #endif
! 32181:
! 32182: #ifndef SYSCALL
! 32183: # define SYSCALL sqlite3_syscall_ptr
! 32184: #endif
! 32185:
! 32186: #if SQLITE_OS_WINCE
! 32187: /*
! 32188: ** These macros are necessary because Windows CE does not natively support the
! 32189: ** Win32 APIs LockFile, UnlockFile, and LockFileEx.
! 32190: */
! 32191:
! 32192: # define LockFile(a,b,c,d,e) winceLockFile(&a, b, c, d, e)
! 32193: # define UnlockFile(a,b,c,d,e) winceUnlockFile(&a, b, c, d, e)
! 32194: # define LockFileEx(a,b,c,d,e,f) winceLockFileEx(&a, b, c, d, e, f)
! 32195:
! 32196: /*
! 32197: ** These are the special syscall hacks for Windows CE. The locking related
! 32198: ** defines here refer to the macros defined just above.
! 32199: */
! 32200:
! 32201: # define osAreFileApisANSI() 1
! 32202: # define osLockFile LockFile
! 32203: # define osUnlockFile UnlockFile
! 32204: # define osLockFileEx LockFileEx
! 32205: #endif
! 32206:
! 32207: static struct win_syscall {
! 32208: const char *zName; /* Name of the sytem call */
! 32209: sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
! 32210: sqlite3_syscall_ptr pDefault; /* Default value */
! 32211: } aSyscall[] = {
! 32212: #if !SQLITE_OS_WINCE
! 32213: { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
! 32214:
! 32215: #define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
! 32216: #else
! 32217: { "AreFileApisANSI", (SYSCALL)0, 0 },
! 32218: #endif
! 32219:
! 32220: #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
! 32221: { "CharLowerW", (SYSCALL)CharLowerW, 0 },
! 32222: #else
! 32223: { "CharLowerW", (SYSCALL)0, 0 },
! 32224: #endif
! 32225:
! 32226: #define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
! 32227:
! 32228: #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
! 32229: { "CharUpperW", (SYSCALL)CharUpperW, 0 },
! 32230: #else
! 32231: { "CharUpperW", (SYSCALL)0, 0 },
! 32232: #endif
! 32233:
! 32234: #define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
! 32235:
! 32236: { "CloseHandle", (SYSCALL)CloseHandle, 0 },
! 32237:
! 32238: #define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
! 32239:
! 32240: #if defined(SQLITE_WIN32_HAS_ANSI)
! 32241: { "CreateFileA", (SYSCALL)CreateFileA, 0 },
! 32242: #else
! 32243: { "CreateFileA", (SYSCALL)0, 0 },
! 32244: #endif
! 32245:
! 32246: #define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
! 32247: LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
! 32248:
! 32249: #if defined(SQLITE_WIN32_HAS_WIDE)
! 32250: { "CreateFileW", (SYSCALL)CreateFileW, 0 },
! 32251: #else
! 32252: { "CreateFileW", (SYSCALL)0, 0 },
! 32253: #endif
! 32254:
! 32255: #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
! 32256: LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
! 32257:
! 32258: { "CreateFileMapping", (SYSCALL)CreateFileMapping, 0 },
! 32259:
! 32260: #define osCreateFileMapping ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
! 32261: DWORD,DWORD,DWORD,LPCTSTR))aSyscall[6].pCurrent)
! 32262:
! 32263: #if defined(SQLITE_WIN32_HAS_WIDE)
! 32264: { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
! 32265: #else
! 32266: { "CreateFileMappingW", (SYSCALL)0, 0 },
! 32267: #endif
! 32268:
! 32269: #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
! 32270: DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
! 32271:
! 32272: #if defined(SQLITE_WIN32_HAS_WIDE)
! 32273: { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
! 32274: #else
! 32275: { "CreateMutexW", (SYSCALL)0, 0 },
! 32276: #endif
! 32277:
! 32278: #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
! 32279: LPCWSTR))aSyscall[8].pCurrent)
! 32280:
! 32281: #if defined(SQLITE_WIN32_HAS_ANSI)
! 32282: { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
! 32283: #else
! 32284: { "DeleteFileA", (SYSCALL)0, 0 },
! 32285: #endif
! 32286:
! 32287: #define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
! 32288:
! 32289: #if defined(SQLITE_WIN32_HAS_WIDE)
! 32290: { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
! 32291: #else
! 32292: { "DeleteFileW", (SYSCALL)0, 0 },
! 32293: #endif
! 32294:
! 32295: #define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
! 32296:
! 32297: #if SQLITE_OS_WINCE
! 32298: { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
! 32299: #else
! 32300: { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
! 32301: #endif
! 32302:
! 32303: #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
! 32304: LPFILETIME))aSyscall[11].pCurrent)
! 32305:
! 32306: #if SQLITE_OS_WINCE
! 32307: { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
! 32308: #else
! 32309: { "FileTimeToSystemTime", (SYSCALL)0, 0 },
! 32310: #endif
! 32311:
! 32312: #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
! 32313: LPSYSTEMTIME))aSyscall[12].pCurrent)
! 32314:
! 32315: { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
! 32316:
! 32317: #define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
! 32318:
! 32319: #if defined(SQLITE_WIN32_HAS_ANSI)
! 32320: { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
! 32321: #else
! 32322: { "FormatMessageA", (SYSCALL)0, 0 },
! 32323: #endif
! 32324:
! 32325: #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
! 32326: DWORD,va_list*))aSyscall[14].pCurrent)
! 32327:
! 32328: #if defined(SQLITE_WIN32_HAS_WIDE)
! 32329: { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
! 32330: #else
! 32331: { "FormatMessageW", (SYSCALL)0, 0 },
! 32332: #endif
! 32333:
! 32334: #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
! 32335: DWORD,va_list*))aSyscall[15].pCurrent)
! 32336:
! 32337: { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
! 32338:
! 32339: #define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
! 32340:
! 32341: { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
! 32342:
! 32343: #define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
! 32344:
! 32345: #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
! 32346: { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
! 32347: #else
! 32348: { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
! 32349: #endif
! 32350:
! 32351: #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
! 32352: LPDWORD))aSyscall[18].pCurrent)
! 32353:
! 32354: #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
! 32355: { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
! 32356: #else
! 32357: { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
! 32358: #endif
! 32359:
! 32360: #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
! 32361: LPDWORD))aSyscall[19].pCurrent)
! 32362:
! 32363: #if defined(SQLITE_WIN32_HAS_ANSI)
! 32364: { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
! 32365: #else
! 32366: { "GetFileAttributesA", (SYSCALL)0, 0 },
! 32367: #endif
! 32368:
! 32369: #define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
! 32370:
! 32371: #if defined(SQLITE_WIN32_HAS_WIDE)
! 32372: { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
! 32373: #else
! 32374: { "GetFileAttributesW", (SYSCALL)0, 0 },
! 32375: #endif
! 32376:
! 32377: #define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
! 32378:
! 32379: #if defined(SQLITE_WIN32_HAS_WIDE)
! 32380: { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
! 32381: #else
! 32382: { "GetFileAttributesExW", (SYSCALL)0, 0 },
! 32383: #endif
! 32384:
! 32385: #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
! 32386: LPVOID))aSyscall[22].pCurrent)
! 32387:
! 32388: { "GetFileSize", (SYSCALL)GetFileSize, 0 },
! 32389:
! 32390: #define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
! 32391:
! 32392: #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
! 32393: { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
! 32394: #else
! 32395: { "GetFullPathNameA", (SYSCALL)0, 0 },
! 32396: #endif
! 32397:
! 32398: #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
! 32399: LPSTR*))aSyscall[24].pCurrent)
! 32400:
! 32401: #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
! 32402: { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
! 32403: #else
! 32404: { "GetFullPathNameW", (SYSCALL)0, 0 },
! 32405: #endif
! 32406:
! 32407: #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
! 32408: LPWSTR*))aSyscall[25].pCurrent)
! 32409:
! 32410: { "GetLastError", (SYSCALL)GetLastError, 0 },
! 32411:
! 32412: #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
! 32413:
! 32414: #if SQLITE_OS_WINCE
! 32415: /* The GetProcAddressA() routine is only available on Windows CE. */
! 32416: { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
! 32417: #else
! 32418: /* All other Windows platforms expect GetProcAddress() to take
! 32419: ** an ANSI string regardless of the _UNICODE setting */
! 32420: { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
! 32421: #endif
! 32422:
! 32423: #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
! 32424: LPCSTR))aSyscall[27].pCurrent)
! 32425:
! 32426: { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
! 32427:
! 32428: #define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
! 32429:
! 32430: { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
! 32431:
! 32432: #define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
! 32433:
! 32434: #if !SQLITE_OS_WINCE
! 32435: { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
! 32436: #else
! 32437: { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
! 32438: #endif
! 32439:
! 32440: #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
! 32441: LPFILETIME))aSyscall[30].pCurrent)
! 32442:
! 32443: #if defined(SQLITE_WIN32_HAS_ANSI)
! 32444: { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
! 32445: #else
! 32446: { "GetTempPathA", (SYSCALL)0, 0 },
! 32447: #endif
! 32448:
! 32449: #define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
! 32450:
! 32451: #if defined(SQLITE_WIN32_HAS_WIDE)
! 32452: { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
! 32453: #else
! 32454: { "GetTempPathW", (SYSCALL)0, 0 },
! 32455: #endif
! 32456:
! 32457: #define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
! 32458:
! 32459: { "GetTickCount", (SYSCALL)GetTickCount, 0 },
! 32460:
! 32461: #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
! 32462:
! 32463: #if defined(SQLITE_WIN32_HAS_ANSI)
! 32464: { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
! 32465: #else
! 32466: { "GetVersionExA", (SYSCALL)0, 0 },
! 32467: #endif
! 32468:
! 32469: #define osGetVersionExA ((BOOL(WINAPI*)( \
! 32470: LPOSVERSIONINFOA))aSyscall[34].pCurrent)
! 32471:
! 32472: { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
! 32473:
! 32474: #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
! 32475: SIZE_T))aSyscall[35].pCurrent)
! 32476:
! 32477: { "HeapCreate", (SYSCALL)HeapCreate, 0 },
! 32478:
! 32479: #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
! 32480: SIZE_T))aSyscall[36].pCurrent)
! 32481:
! 32482: { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
! 32483:
! 32484: #define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[37].pCurrent)
! 32485:
! 32486: { "HeapFree", (SYSCALL)HeapFree, 0 },
! 32487:
! 32488: #define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[38].pCurrent)
! 32489:
! 32490: { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
! 32491:
! 32492: #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
! 32493: SIZE_T))aSyscall[39].pCurrent)
! 32494:
! 32495: { "HeapSize", (SYSCALL)HeapSize, 0 },
! 32496:
! 32497: #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
! 32498: LPCVOID))aSyscall[40].pCurrent)
! 32499:
! 32500: { "HeapValidate", (SYSCALL)HeapValidate, 0 },
! 32501:
! 32502: #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
! 32503: LPCVOID))aSyscall[41].pCurrent)
! 32504:
! 32505: #if defined(SQLITE_WIN32_HAS_ANSI)
! 32506: { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
! 32507: #else
! 32508: { "LoadLibraryA", (SYSCALL)0, 0 },
! 32509: #endif
! 32510:
! 32511: #define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[42].pCurrent)
! 32512:
! 32513: #if defined(SQLITE_WIN32_HAS_WIDE)
! 32514: { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
! 32515: #else
! 32516: { "LoadLibraryW", (SYSCALL)0, 0 },
! 32517: #endif
! 32518:
! 32519: #define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[43].pCurrent)
! 32520:
! 32521: { "LocalFree", (SYSCALL)LocalFree, 0 },
! 32522:
! 32523: #define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[44].pCurrent)
! 32524:
! 32525: #if !SQLITE_OS_WINCE
! 32526: { "LockFile", (SYSCALL)LockFile, 0 },
! 32527:
! 32528: #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
! 32529: DWORD))aSyscall[45].pCurrent)
! 32530: #else
! 32531: { "LockFile", (SYSCALL)0, 0 },
! 32532: #endif
! 32533:
! 32534: #if !SQLITE_OS_WINCE
! 32535: { "LockFileEx", (SYSCALL)LockFileEx, 0 },
! 32536:
! 32537: #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
! 32538: LPOVERLAPPED))aSyscall[46].pCurrent)
! 32539: #else
! 32540: { "LockFileEx", (SYSCALL)0, 0 },
! 32541: #endif
! 32542:
! 32543: { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
! 32544:
! 32545: #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
! 32546: SIZE_T))aSyscall[47].pCurrent)
! 32547:
! 32548: { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
! 32549:
! 32550: #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
! 32551: int))aSyscall[48].pCurrent)
! 32552:
! 32553: { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
! 32554:
! 32555: #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
! 32556: LARGE_INTEGER*))aSyscall[49].pCurrent)
! 32557:
! 32558: { "ReadFile", (SYSCALL)ReadFile, 0 },
! 32559:
! 32560: #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
! 32561: LPOVERLAPPED))aSyscall[50].pCurrent)
! 32562:
! 32563: { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
! 32564:
! 32565: #define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[51].pCurrent)
! 32566:
! 32567: { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
! 32568:
! 32569: #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
! 32570: DWORD))aSyscall[52].pCurrent)
! 32571:
! 32572: { "Sleep", (SYSCALL)Sleep, 0 },
! 32573:
! 32574: #define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[53].pCurrent)
! 32575:
! 32576: { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
! 32577:
! 32578: #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
! 32579: LPFILETIME))aSyscall[54].pCurrent)
! 32580:
! 32581: #if !SQLITE_OS_WINCE
! 32582: { "UnlockFile", (SYSCALL)UnlockFile, 0 },
! 32583:
! 32584: #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
! 32585: DWORD))aSyscall[55].pCurrent)
! 32586: #else
! 32587: { "UnlockFile", (SYSCALL)0, 0 },
! 32588: #endif
! 32589:
! 32590: #if !SQLITE_OS_WINCE
! 32591: { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
! 32592:
! 32593: #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
! 32594: LPOVERLAPPED))aSyscall[56].pCurrent)
! 32595: #else
! 32596: { "UnlockFileEx", (SYSCALL)0, 0 },
! 32597: #endif
! 32598:
! 32599: { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
! 32600:
! 32601: #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[57].pCurrent)
! 32602:
! 32603: { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
! 32604:
! 32605: #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
! 32606: LPCSTR,LPBOOL))aSyscall[58].pCurrent)
! 32607:
! 32608: { "WriteFile", (SYSCALL)WriteFile, 0 },
! 32609:
! 32610: #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
! 32611: LPOVERLAPPED))aSyscall[59].pCurrent)
! 32612:
! 32613: }; /* End of the overrideable system calls */
! 32614:
! 32615: /*
! 32616: ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
! 32617: ** "win32" VFSes. Return SQLITE_OK opon successfully updating the
! 32618: ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
! 32619: ** system call named zName.
! 32620: */
! 32621: static int winSetSystemCall(
! 32622: sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
! 32623: const char *zName, /* Name of system call to override */
! 32624: sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
! 32625: ){
! 32626: unsigned int i;
! 32627: int rc = SQLITE_NOTFOUND;
! 32628:
! 32629: UNUSED_PARAMETER(pNotUsed);
! 32630: if( zName==0 ){
! 32631: /* If no zName is given, restore all system calls to their default
! 32632: ** settings and return NULL
! 32633: */
! 32634: rc = SQLITE_OK;
! 32635: for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
! 32636: if( aSyscall[i].pDefault ){
! 32637: aSyscall[i].pCurrent = aSyscall[i].pDefault;
! 32638: }
! 32639: }
! 32640: }else{
! 32641: /* If zName is specified, operate on only the one system call
! 32642: ** specified.
! 32643: */
! 32644: for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
! 32645: if( strcmp(zName, aSyscall[i].zName)==0 ){
! 32646: if( aSyscall[i].pDefault==0 ){
! 32647: aSyscall[i].pDefault = aSyscall[i].pCurrent;
! 32648: }
! 32649: rc = SQLITE_OK;
! 32650: if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
! 32651: aSyscall[i].pCurrent = pNewFunc;
! 32652: break;
! 32653: }
! 32654: }
! 32655: }
! 32656: return rc;
! 32657: }
! 32658:
! 32659: /*
! 32660: ** Return the value of a system call. Return NULL if zName is not a
! 32661: ** recognized system call name. NULL is also returned if the system call
! 32662: ** is currently undefined.
! 32663: */
! 32664: static sqlite3_syscall_ptr winGetSystemCall(
! 32665: sqlite3_vfs *pNotUsed,
! 32666: const char *zName
! 32667: ){
! 32668: unsigned int i;
! 32669:
! 32670: UNUSED_PARAMETER(pNotUsed);
! 32671: for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
! 32672: if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
! 32673: }
! 32674: return 0;
! 32675: }
! 32676:
! 32677: /*
! 32678: ** Return the name of the first system call after zName. If zName==NULL
! 32679: ** then return the name of the first system call. Return NULL if zName
! 32680: ** is the last system call or if zName is not the name of a valid
! 32681: ** system call.
! 32682: */
! 32683: static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
! 32684: int i = -1;
! 32685:
! 32686: UNUSED_PARAMETER(p);
! 32687: if( zName ){
! 32688: for(i=0; i<ArraySize(aSyscall)-1; i++){
! 32689: if( strcmp(zName, aSyscall[i].zName)==0 ) break;
! 32690: }
! 32691: }
! 32692: for(i++; i<ArraySize(aSyscall); i++){
! 32693: if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
! 32694: }
! 32695: return 0;
! 32696: }
! 32697:
! 32698: /*
! 32699: ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
! 32700: ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
! 32701: **
! 32702: ** Here is an interesting observation: Win95, Win98, and WinME lack
! 32703: ** the LockFileEx() API. But we can still statically link against that
! 32704: ** API as long as we don't call it when running Win95/98/ME. A call to
! 32705: ** this routine is used to determine if the host is Win95/98/ME or
! 32706: ** WinNT/2K/XP so that we will know whether or not we can safely call
! 32707: ** the LockFileEx() API.
! 32708: */
! 32709: #if SQLITE_OS_WINCE
! 32710: # define isNT() (1)
! 32711: #else
! 32712: static int isNT(void){
! 32713: if( sqlite3_os_type==0 ){
! 32714: OSVERSIONINFOA sInfo;
! 32715: sInfo.dwOSVersionInfoSize = sizeof(sInfo);
! 32716: osGetVersionExA(&sInfo);
! 32717: sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
! 32718: }
! 32719: return sqlite3_os_type==2;
! 32720: }
! 32721: #endif /* SQLITE_OS_WINCE */
! 32722:
! 32723: #ifdef SQLITE_WIN32_MALLOC
! 32724: /*
! 32725: ** Allocate nBytes of memory.
! 32726: */
! 32727: static void *winMemMalloc(int nBytes){
! 32728: HANDLE hHeap;
! 32729: void *p;
! 32730:
! 32731: winMemAssertMagic();
! 32732: hHeap = winMemGetHeap();
! 32733: assert( hHeap!=0 );
! 32734: assert( hHeap!=INVALID_HANDLE_VALUE );
! 32735: #ifdef SQLITE_WIN32_MALLOC_VALIDATE
! 32736: assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
! 32737: #endif
! 32738: assert( nBytes>=0 );
! 32739: p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
! 32740: if( !p ){
! 32741: sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p",
! 32742: nBytes, osGetLastError(), (void*)hHeap);
! 32743: }
! 32744: return p;
! 32745: }
! 32746:
! 32747: /*
! 32748: ** Free memory.
! 32749: */
! 32750: static void winMemFree(void *pPrior){
! 32751: HANDLE hHeap;
! 32752:
! 32753: winMemAssertMagic();
! 32754: hHeap = winMemGetHeap();
! 32755: assert( hHeap!=0 );
! 32756: assert( hHeap!=INVALID_HANDLE_VALUE );
! 32757: #ifdef SQLITE_WIN32_MALLOC_VALIDATE
! 32758: assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
! 32759: #endif
! 32760: if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
! 32761: if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
! 32762: sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p",
! 32763: pPrior, osGetLastError(), (void*)hHeap);
! 32764: }
! 32765: }
! 32766:
! 32767: /*
! 32768: ** Change the size of an existing memory allocation
! 32769: */
! 32770: static void *winMemRealloc(void *pPrior, int nBytes){
! 32771: HANDLE hHeap;
! 32772: void *p;
! 32773:
! 32774: winMemAssertMagic();
! 32775: hHeap = winMemGetHeap();
! 32776: assert( hHeap!=0 );
! 32777: assert( hHeap!=INVALID_HANDLE_VALUE );
! 32778: #ifdef SQLITE_WIN32_MALLOC_VALIDATE
! 32779: assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
! 32780: #endif
! 32781: assert( nBytes>=0 );
! 32782: if( !pPrior ){
! 32783: p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
! 32784: }else{
! 32785: p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
! 32786: }
! 32787: if( !p ){
! 32788: sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p",
! 32789: pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
! 32790: (void*)hHeap);
! 32791: }
! 32792: return p;
! 32793: }
! 32794:
! 32795: /*
! 32796: ** Return the size of an outstanding allocation, in bytes.
! 32797: */
! 32798: static int winMemSize(void *p){
! 32799: HANDLE hHeap;
! 32800: SIZE_T n;
! 32801:
! 32802: winMemAssertMagic();
! 32803: hHeap = winMemGetHeap();
! 32804: assert( hHeap!=0 );
! 32805: assert( hHeap!=INVALID_HANDLE_VALUE );
! 32806: #ifdef SQLITE_WIN32_MALLOC_VALIDATE
! 32807: assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
! 32808: #endif
! 32809: if( !p ) return 0;
! 32810: n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
! 32811: if( n==(SIZE_T)-1 ){
! 32812: sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p",
! 32813: p, osGetLastError(), (void*)hHeap);
! 32814: return 0;
! 32815: }
! 32816: return (int)n;
! 32817: }
! 32818:
! 32819: /*
! 32820: ** Round up a request size to the next valid allocation size.
! 32821: */
! 32822: static int winMemRoundup(int n){
! 32823: return n;
! 32824: }
! 32825:
! 32826: /*
! 32827: ** Initialize this module.
! 32828: */
! 32829: static int winMemInit(void *pAppData){
! 32830: winMemData *pWinMemData = (winMemData *)pAppData;
! 32831:
! 32832: if( !pWinMemData ) return SQLITE_ERROR;
! 32833: assert( pWinMemData->magic==WINMEM_MAGIC );
! 32834: if( !pWinMemData->hHeap ){
! 32835: pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
! 32836: SQLITE_WIN32_HEAP_INIT_SIZE,
! 32837: SQLITE_WIN32_HEAP_MAX_SIZE);
! 32838: if( !pWinMemData->hHeap ){
! 32839: sqlite3_log(SQLITE_NOMEM,
! 32840: "failed to HeapCreate (%d), flags=%u, initSize=%u, maxSize=%u",
! 32841: osGetLastError(), SQLITE_WIN32_HEAP_FLAGS,
! 32842: SQLITE_WIN32_HEAP_INIT_SIZE, SQLITE_WIN32_HEAP_MAX_SIZE);
! 32843: return SQLITE_NOMEM;
! 32844: }
! 32845: pWinMemData->bOwned = TRUE;
! 32846: }
! 32847: assert( pWinMemData->hHeap!=0 );
! 32848: assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
! 32849: #ifdef SQLITE_WIN32_MALLOC_VALIDATE
! 32850: assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
! 32851: #endif
! 32852: return SQLITE_OK;
! 32853: }
! 32854:
! 32855: /*
! 32856: ** Deinitialize this module.
! 32857: */
! 32858: static void winMemShutdown(void *pAppData){
! 32859: winMemData *pWinMemData = (winMemData *)pAppData;
! 32860:
! 32861: if( !pWinMemData ) return;
! 32862: if( pWinMemData->hHeap ){
! 32863: assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
! 32864: #ifdef SQLITE_WIN32_MALLOC_VALIDATE
! 32865: assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
! 32866: #endif
! 32867: if( pWinMemData->bOwned ){
! 32868: if( !osHeapDestroy(pWinMemData->hHeap) ){
! 32869: sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p",
! 32870: osGetLastError(), (void*)pWinMemData->hHeap);
! 32871: }
! 32872: pWinMemData->bOwned = FALSE;
! 32873: }
! 32874: pWinMemData->hHeap = NULL;
! 32875: }
! 32876: }
! 32877:
! 32878: /*
! 32879: ** Populate the low-level memory allocation function pointers in
! 32880: ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
! 32881: ** arguments specify the block of memory to manage.
! 32882: **
! 32883: ** This routine is only called by sqlite3_config(), and therefore
! 32884: ** is not required to be threadsafe (it is not).
! 32885: */
! 32886: SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
! 32887: static const sqlite3_mem_methods winMemMethods = {
! 32888: winMemMalloc,
! 32889: winMemFree,
! 32890: winMemRealloc,
! 32891: winMemSize,
! 32892: winMemRoundup,
! 32893: winMemInit,
! 32894: winMemShutdown,
! 32895: &win_mem_data
! 32896: };
! 32897: return &winMemMethods;
! 32898: }
! 32899:
! 32900: SQLITE_PRIVATE void sqlite3MemSetDefault(void){
! 32901: sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
! 32902: }
! 32903: #endif /* SQLITE_WIN32_MALLOC */
! 32904:
! 32905: /*
! 32906: ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
! 32907: **
! 32908: ** Space to hold the returned string is obtained from malloc.
! 32909: */
! 32910: static LPWSTR utf8ToUnicode(const char *zFilename){
! 32911: int nChar;
! 32912: LPWSTR zWideFilename;
! 32913:
! 32914: nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
! 32915: if( nChar==0 ){
! 32916: return 0;
! 32917: }
! 32918: zWideFilename = sqlite3_malloc( nChar*sizeof(zWideFilename[0]) );
! 32919: if( zWideFilename==0 ){
! 32920: return 0;
! 32921: }
! 32922: nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
! 32923: nChar);
! 32924: if( nChar==0 ){
! 32925: sqlite3_free(zWideFilename);
! 32926: zWideFilename = 0;
! 32927: }
! 32928: return zWideFilename;
! 32929: }
! 32930:
! 32931: /*
! 32932: ** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is
! 32933: ** obtained from sqlite3_malloc().
! 32934: */
! 32935: static char *unicodeToUtf8(LPCWSTR zWideFilename){
! 32936: int nByte;
! 32937: char *zFilename;
! 32938:
! 32939: nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
! 32940: if( nByte == 0 ){
! 32941: return 0;
! 32942: }
! 32943: zFilename = sqlite3_malloc( nByte );
! 32944: if( zFilename==0 ){
! 32945: return 0;
! 32946: }
! 32947: nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
! 32948: 0, 0);
! 32949: if( nByte == 0 ){
! 32950: sqlite3_free(zFilename);
! 32951: zFilename = 0;
! 32952: }
! 32953: return zFilename;
! 32954: }
! 32955:
! 32956: /*
! 32957: ** Convert an ANSI string to Microsoft Unicode, based on the
! 32958: ** current codepage settings for file apis.
! 32959: **
! 32960: ** Space to hold the returned string is obtained
! 32961: ** from sqlite3_malloc.
! 32962: */
! 32963: static LPWSTR mbcsToUnicode(const char *zFilename){
! 32964: int nByte;
! 32965: LPWSTR zMbcsFilename;
! 32966: int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
! 32967:
! 32968: nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
! 32969: 0)*sizeof(WCHAR);
! 32970: if( nByte==0 ){
! 32971: return 0;
! 32972: }
! 32973: zMbcsFilename = sqlite3_malloc( nByte*sizeof(zMbcsFilename[0]) );
! 32974: if( zMbcsFilename==0 ){
! 32975: return 0;
! 32976: }
! 32977: nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
! 32978: nByte);
! 32979: if( nByte==0 ){
! 32980: sqlite3_free(zMbcsFilename);
! 32981: zMbcsFilename = 0;
! 32982: }
! 32983: return zMbcsFilename;
! 32984: }
! 32985:
! 32986: /*
! 32987: ** Convert Microsoft Unicode to multi-byte character string, based on the
! 32988: ** user's ANSI codepage.
! 32989: **
! 32990: ** Space to hold the returned string is obtained from
! 32991: ** sqlite3_malloc().
! 32992: */
! 32993: static char *unicodeToMbcs(LPCWSTR zWideFilename){
! 32994: int nByte;
! 32995: char *zFilename;
! 32996: int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
! 32997:
! 32998: nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
! 32999: if( nByte == 0 ){
! 33000: return 0;
! 33001: }
! 33002: zFilename = sqlite3_malloc( nByte );
! 33003: if( zFilename==0 ){
! 33004: return 0;
! 33005: }
! 33006: nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
! 33007: nByte, 0, 0);
! 33008: if( nByte == 0 ){
! 33009: sqlite3_free(zFilename);
! 33010: zFilename = 0;
! 33011: }
! 33012: return zFilename;
! 33013: }
! 33014:
! 33015: /*
! 33016: ** Convert multibyte character string to UTF-8. Space to hold the
! 33017: ** returned string is obtained from sqlite3_malloc().
! 33018: */
! 33019: SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
! 33020: char *zFilenameUtf8;
! 33021: LPWSTR zTmpWide;
! 33022:
! 33023: zTmpWide = mbcsToUnicode(zFilename);
! 33024: if( zTmpWide==0 ){
! 33025: return 0;
! 33026: }
! 33027: zFilenameUtf8 = unicodeToUtf8(zTmpWide);
! 33028: sqlite3_free(zTmpWide);
! 33029: return zFilenameUtf8;
! 33030: }
! 33031:
! 33032: /*
! 33033: ** Convert UTF-8 to multibyte character string. Space to hold the
! 33034: ** returned string is obtained from sqlite3_malloc().
! 33035: */
! 33036: SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
! 33037: char *zFilenameMbcs;
! 33038: LPWSTR zTmpWide;
! 33039:
! 33040: zTmpWide = utf8ToUnicode(zFilename);
! 33041: if( zTmpWide==0 ){
! 33042: return 0;
! 33043: }
! 33044: zFilenameMbcs = unicodeToMbcs(zTmpWide);
! 33045: sqlite3_free(zTmpWide);
! 33046: return zFilenameMbcs;
! 33047: }
! 33048:
! 33049:
! 33050: /*
! 33051: ** The return value of getLastErrorMsg
! 33052: ** is zero if the error message fits in the buffer, or non-zero
! 33053: ** otherwise (if the message was truncated).
! 33054: */
! 33055: static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
! 33056: /* FormatMessage returns 0 on failure. Otherwise it
! 33057: ** returns the number of TCHARs written to the output
! 33058: ** buffer, excluding the terminating null char.
! 33059: */
! 33060: DWORD dwLen = 0;
! 33061: char *zOut = 0;
! 33062:
! 33063: if( isNT() ){
! 33064: LPWSTR zTempWide = NULL;
! 33065: dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
! 33066: FORMAT_MESSAGE_FROM_SYSTEM |
! 33067: FORMAT_MESSAGE_IGNORE_INSERTS,
! 33068: NULL,
! 33069: lastErrno,
! 33070: 0,
! 33071: (LPWSTR) &zTempWide,
! 33072: 0,
! 33073: 0);
! 33074: if( dwLen > 0 ){
! 33075: /* allocate a buffer and convert to UTF8 */
! 33076: sqlite3BeginBenignMalloc();
! 33077: zOut = unicodeToUtf8(zTempWide);
! 33078: sqlite3EndBenignMalloc();
! 33079: /* free the system buffer allocated by FormatMessage */
! 33080: osLocalFree(zTempWide);
! 33081: }
! 33082: /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
! 33083: ** Since the ANSI version of these Windows API do not exist for WINCE,
! 33084: ** it's important to not reference them for WINCE builds.
! 33085: */
! 33086: #if SQLITE_OS_WINCE==0
! 33087: }else{
! 33088: char *zTemp = NULL;
! 33089: dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
! 33090: FORMAT_MESSAGE_FROM_SYSTEM |
! 33091: FORMAT_MESSAGE_IGNORE_INSERTS,
! 33092: NULL,
! 33093: lastErrno,
! 33094: 0,
! 33095: (LPSTR) &zTemp,
! 33096: 0,
! 33097: 0);
! 33098: if( dwLen > 0 ){
! 33099: /* allocate a buffer and convert to UTF8 */
! 33100: sqlite3BeginBenignMalloc();
! 33101: zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
! 33102: sqlite3EndBenignMalloc();
! 33103: /* free the system buffer allocated by FormatMessage */
! 33104: osLocalFree(zTemp);
! 33105: }
! 33106: #endif
! 33107: }
! 33108: if( 0 == dwLen ){
! 33109: sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", lastErrno, lastErrno);
! 33110: }else{
! 33111: /* copy a maximum of nBuf chars to output buffer */
! 33112: sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
! 33113: /* free the UTF8 buffer */
! 33114: sqlite3_free(zOut);
! 33115: }
! 33116: return 0;
! 33117: }
! 33118:
! 33119: /*
! 33120: **
! 33121: ** This function - winLogErrorAtLine() - is only ever called via the macro
! 33122: ** winLogError().
! 33123: **
! 33124: ** This routine is invoked after an error occurs in an OS function.
! 33125: ** It logs a message using sqlite3_log() containing the current value of
! 33126: ** error code and, if possible, the human-readable equivalent from
! 33127: ** FormatMessage.
! 33128: **
! 33129: ** The first argument passed to the macro should be the error code that
! 33130: ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
! 33131: ** The two subsequent arguments should be the name of the OS function that
! 33132: ** failed and the the associated file-system path, if any.
! 33133: */
! 33134: #define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
! 33135: static int winLogErrorAtLine(
! 33136: int errcode, /* SQLite error code */
! 33137: DWORD lastErrno, /* Win32 last error */
! 33138: const char *zFunc, /* Name of OS function that failed */
! 33139: const char *zPath, /* File path associated with error */
! 33140: int iLine /* Source line number where error occurred */
! 33141: ){
! 33142: char zMsg[500]; /* Human readable error text */
! 33143: int i; /* Loop counter */
! 33144:
! 33145: zMsg[0] = 0;
! 33146: getLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
! 33147: assert( errcode!=SQLITE_OK );
! 33148: if( zPath==0 ) zPath = "";
! 33149: for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
! 33150: zMsg[i] = 0;
! 33151: sqlite3_log(errcode,
! 33152: "os_win.c:%d: (%d) %s(%s) - %s",
! 33153: iLine, lastErrno, zFunc, zPath, zMsg
! 33154: );
! 33155:
! 33156: return errcode;
! 33157: }
! 33158:
! 33159: /*
! 33160: ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
! 33161: ** will be retried following a locking error - probably caused by
! 33162: ** antivirus software. Also the initial delay before the first retry.
! 33163: ** The delay increases linearly with each retry.
! 33164: */
! 33165: #ifndef SQLITE_WIN32_IOERR_RETRY
! 33166: # define SQLITE_WIN32_IOERR_RETRY 10
! 33167: #endif
! 33168: #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
! 33169: # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
! 33170: #endif
! 33171: static int win32IoerrRetry = SQLITE_WIN32_IOERR_RETRY;
! 33172: static int win32IoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
! 33173:
! 33174: /*
! 33175: ** If a ReadFile() or WriteFile() error occurs, invoke this routine
! 33176: ** to see if it should be retried. Return TRUE to retry. Return FALSE
! 33177: ** to give up with an error.
! 33178: */
! 33179: static int retryIoerr(int *pnRetry, DWORD *pError){
! 33180: DWORD e = osGetLastError();
! 33181: if( *pnRetry>=win32IoerrRetry ){
! 33182: if( pError ){
! 33183: *pError = e;
! 33184: }
! 33185: return 0;
! 33186: }
! 33187: if( e==ERROR_ACCESS_DENIED ||
! 33188: e==ERROR_LOCK_VIOLATION ||
! 33189: e==ERROR_SHARING_VIOLATION ){
! 33190: osSleep(win32IoerrRetryDelay*(1+*pnRetry));
! 33191: ++*pnRetry;
! 33192: return 1;
! 33193: }
! 33194: if( pError ){
! 33195: *pError = e;
! 33196: }
! 33197: return 0;
! 33198: }
! 33199:
! 33200: /*
! 33201: ** Log a I/O error retry episode.
! 33202: */
! 33203: static void logIoerr(int nRetry){
! 33204: if( nRetry ){
! 33205: sqlite3_log(SQLITE_IOERR,
! 33206: "delayed %dms for lock/sharing conflict",
! 33207: win32IoerrRetryDelay*nRetry*(nRetry+1)/2
! 33208: );
! 33209: }
! 33210: }
! 33211:
! 33212: #if SQLITE_OS_WINCE
! 33213: /*************************************************************************
! 33214: ** This section contains code for WinCE only.
! 33215: */
! 33216: /*
! 33217: ** Windows CE does not have a localtime() function. So create a
! 33218: ** substitute.
! 33219: */
! 33220: /* #include <time.h> */
! 33221: struct tm *__cdecl localtime(const time_t *t)
! 33222: {
! 33223: static struct tm y;
! 33224: FILETIME uTm, lTm;
! 33225: SYSTEMTIME pTm;
! 33226: sqlite3_int64 t64;
! 33227: t64 = *t;
! 33228: t64 = (t64 + 11644473600)*10000000;
! 33229: uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
! 33230: uTm.dwHighDateTime= (DWORD)(t64 >> 32);
! 33231: osFileTimeToLocalFileTime(&uTm,&lTm);
! 33232: osFileTimeToSystemTime(&lTm,&pTm);
! 33233: y.tm_year = pTm.wYear - 1900;
! 33234: y.tm_mon = pTm.wMonth - 1;
! 33235: y.tm_wday = pTm.wDayOfWeek;
! 33236: y.tm_mday = pTm.wDay;
! 33237: y.tm_hour = pTm.wHour;
! 33238: y.tm_min = pTm.wMinute;
! 33239: y.tm_sec = pTm.wSecond;
! 33240: return &y;
! 33241: }
! 33242:
! 33243: #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
! 33244:
! 33245: /*
! 33246: ** Acquire a lock on the handle h
! 33247: */
! 33248: static void winceMutexAcquire(HANDLE h){
! 33249: DWORD dwErr;
! 33250: do {
! 33251: dwErr = WaitForSingleObject(h, INFINITE);
! 33252: } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
! 33253: }
! 33254: /*
! 33255: ** Release a lock acquired by winceMutexAcquire()
! 33256: */
! 33257: #define winceMutexRelease(h) ReleaseMutex(h)
! 33258:
! 33259: /*
! 33260: ** Create the mutex and shared memory used for locking in the file
! 33261: ** descriptor pFile
! 33262: */
! 33263: static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
! 33264: LPWSTR zTok;
! 33265: LPWSTR zName;
! 33266: BOOL bInit = TRUE;
! 33267:
! 33268: zName = utf8ToUnicode(zFilename);
! 33269: if( zName==0 ){
! 33270: /* out of memory */
! 33271: return FALSE;
! 33272: }
! 33273:
! 33274: /* Initialize the local lockdata */
! 33275: memset(&pFile->local, 0, sizeof(pFile->local));
! 33276:
! 33277: /* Replace the backslashes from the filename and lowercase it
! 33278: ** to derive a mutex name. */
! 33279: zTok = osCharLowerW(zName);
! 33280: for (;*zTok;zTok++){
! 33281: if (*zTok == '\\') *zTok = '_';
! 33282: }
! 33283:
! 33284: /* Create/open the named mutex */
! 33285: pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
! 33286: if (!pFile->hMutex){
! 33287: pFile->lastErrno = osGetLastError();
! 33288: winLogError(SQLITE_ERROR, pFile->lastErrno, "winceCreateLock1", zFilename);
! 33289: sqlite3_free(zName);
! 33290: return FALSE;
! 33291: }
! 33292:
! 33293: /* Acquire the mutex before continuing */
! 33294: winceMutexAcquire(pFile->hMutex);
! 33295:
! 33296: /* Since the names of named mutexes, semaphores, file mappings etc are
! 33297: ** case-sensitive, take advantage of that by uppercasing the mutex name
! 33298: ** and using that as the shared filemapping name.
! 33299: */
! 33300: osCharUpperW(zName);
! 33301: pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
! 33302: PAGE_READWRITE, 0, sizeof(winceLock),
! 33303: zName);
! 33304:
! 33305: /* Set a flag that indicates we're the first to create the memory so it
! 33306: ** must be zero-initialized */
! 33307: if (osGetLastError() == ERROR_ALREADY_EXISTS){
! 33308: bInit = FALSE;
! 33309: }
! 33310:
! 33311: sqlite3_free(zName);
! 33312:
! 33313: /* If we succeeded in making the shared memory handle, map it. */
! 33314: if (pFile->hShared){
! 33315: pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
! 33316: FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
! 33317: /* If mapping failed, close the shared memory handle and erase it */
! 33318: if (!pFile->shared){
! 33319: pFile->lastErrno = osGetLastError();
! 33320: winLogError(SQLITE_ERROR, pFile->lastErrno,
! 33321: "winceCreateLock2", zFilename);
! 33322: osCloseHandle(pFile->hShared);
! 33323: pFile->hShared = NULL;
! 33324: }
! 33325: }
! 33326:
! 33327: /* If shared memory could not be created, then close the mutex and fail */
! 33328: if (pFile->hShared == NULL){
! 33329: winceMutexRelease(pFile->hMutex);
! 33330: osCloseHandle(pFile->hMutex);
! 33331: pFile->hMutex = NULL;
! 33332: return FALSE;
! 33333: }
! 33334:
! 33335: /* Initialize the shared memory if we're supposed to */
! 33336: if (bInit) {
! 33337: memset(pFile->shared, 0, sizeof(winceLock));
! 33338: }
! 33339:
! 33340: winceMutexRelease(pFile->hMutex);
! 33341: return TRUE;
! 33342: }
! 33343:
! 33344: /*
! 33345: ** Destroy the part of winFile that deals with wince locks
! 33346: */
! 33347: static void winceDestroyLock(winFile *pFile){
! 33348: if (pFile->hMutex){
! 33349: /* Acquire the mutex */
! 33350: winceMutexAcquire(pFile->hMutex);
! 33351:
! 33352: /* The following blocks should probably assert in debug mode, but they
! 33353: are to cleanup in case any locks remained open */
! 33354: if (pFile->local.nReaders){
! 33355: pFile->shared->nReaders --;
! 33356: }
! 33357: if (pFile->local.bReserved){
! 33358: pFile->shared->bReserved = FALSE;
! 33359: }
! 33360: if (pFile->local.bPending){
! 33361: pFile->shared->bPending = FALSE;
! 33362: }
! 33363: if (pFile->local.bExclusive){
! 33364: pFile->shared->bExclusive = FALSE;
! 33365: }
! 33366:
! 33367: /* De-reference and close our copy of the shared memory handle */
! 33368: osUnmapViewOfFile(pFile->shared);
! 33369: osCloseHandle(pFile->hShared);
! 33370:
! 33371: /* Done with the mutex */
! 33372: winceMutexRelease(pFile->hMutex);
! 33373: osCloseHandle(pFile->hMutex);
! 33374: pFile->hMutex = NULL;
! 33375: }
! 33376: }
! 33377:
! 33378: /*
! 33379: ** An implementation of the LockFile() API of Windows for CE
! 33380: */
! 33381: static BOOL winceLockFile(
! 33382: HANDLE *phFile,
! 33383: DWORD dwFileOffsetLow,
! 33384: DWORD dwFileOffsetHigh,
! 33385: DWORD nNumberOfBytesToLockLow,
! 33386: DWORD nNumberOfBytesToLockHigh
! 33387: ){
! 33388: winFile *pFile = HANDLE_TO_WINFILE(phFile);
! 33389: BOOL bReturn = FALSE;
! 33390:
! 33391: UNUSED_PARAMETER(dwFileOffsetHigh);
! 33392: UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
! 33393:
! 33394: if (!pFile->hMutex) return TRUE;
! 33395: winceMutexAcquire(pFile->hMutex);
! 33396:
! 33397: /* Wanting an exclusive lock? */
! 33398: if (dwFileOffsetLow == (DWORD)SHARED_FIRST
! 33399: && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
! 33400: if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
! 33401: pFile->shared->bExclusive = TRUE;
! 33402: pFile->local.bExclusive = TRUE;
! 33403: bReturn = TRUE;
! 33404: }
! 33405: }
! 33406:
! 33407: /* Want a read-only lock? */
! 33408: else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
! 33409: nNumberOfBytesToLockLow == 1){
! 33410: if (pFile->shared->bExclusive == 0){
! 33411: pFile->local.nReaders ++;
! 33412: if (pFile->local.nReaders == 1){
! 33413: pFile->shared->nReaders ++;
! 33414: }
! 33415: bReturn = TRUE;
! 33416: }
! 33417: }
! 33418:
! 33419: /* Want a pending lock? */
! 33420: else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToLockLow == 1){
! 33421: /* If no pending lock has been acquired, then acquire it */
! 33422: if (pFile->shared->bPending == 0) {
! 33423: pFile->shared->bPending = TRUE;
! 33424: pFile->local.bPending = TRUE;
! 33425: bReturn = TRUE;
! 33426: }
! 33427: }
! 33428:
! 33429: /* Want a reserved lock? */
! 33430: else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
! 33431: if (pFile->shared->bReserved == 0) {
! 33432: pFile->shared->bReserved = TRUE;
! 33433: pFile->local.bReserved = TRUE;
! 33434: bReturn = TRUE;
! 33435: }
! 33436: }
! 33437:
! 33438: winceMutexRelease(pFile->hMutex);
! 33439: return bReturn;
! 33440: }
! 33441:
! 33442: /*
! 33443: ** An implementation of the UnlockFile API of Windows for CE
! 33444: */
! 33445: static BOOL winceUnlockFile(
! 33446: HANDLE *phFile,
! 33447: DWORD dwFileOffsetLow,
! 33448: DWORD dwFileOffsetHigh,
! 33449: DWORD nNumberOfBytesToUnlockLow,
! 33450: DWORD nNumberOfBytesToUnlockHigh
! 33451: ){
! 33452: winFile *pFile = HANDLE_TO_WINFILE(phFile);
! 33453: BOOL bReturn = FALSE;
! 33454:
! 33455: UNUSED_PARAMETER(dwFileOffsetHigh);
! 33456: UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
! 33457:
! 33458: if (!pFile->hMutex) return TRUE;
! 33459: winceMutexAcquire(pFile->hMutex);
! 33460:
! 33461: /* Releasing a reader lock or an exclusive lock */
! 33462: if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
! 33463: /* Did we have an exclusive lock? */
! 33464: if (pFile->local.bExclusive){
! 33465: assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
! 33466: pFile->local.bExclusive = FALSE;
! 33467: pFile->shared->bExclusive = FALSE;
! 33468: bReturn = TRUE;
! 33469: }
! 33470:
! 33471: /* Did we just have a reader lock? */
! 33472: else if (pFile->local.nReaders){
! 33473: assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE || nNumberOfBytesToUnlockLow == 1);
! 33474: pFile->local.nReaders --;
! 33475: if (pFile->local.nReaders == 0)
! 33476: {
! 33477: pFile->shared->nReaders --;
! 33478: }
! 33479: bReturn = TRUE;
! 33480: }
! 33481: }
! 33482:
! 33483: /* Releasing a pending lock */
! 33484: else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
! 33485: if (pFile->local.bPending){
! 33486: pFile->local.bPending = FALSE;
! 33487: pFile->shared->bPending = FALSE;
! 33488: bReturn = TRUE;
! 33489: }
! 33490: }
! 33491: /* Releasing a reserved lock */
! 33492: else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
! 33493: if (pFile->local.bReserved) {
! 33494: pFile->local.bReserved = FALSE;
! 33495: pFile->shared->bReserved = FALSE;
! 33496: bReturn = TRUE;
! 33497: }
! 33498: }
! 33499:
! 33500: winceMutexRelease(pFile->hMutex);
! 33501: return bReturn;
! 33502: }
! 33503:
! 33504: /*
! 33505: ** An implementation of the LockFileEx() API of Windows for CE
! 33506: */
! 33507: static BOOL winceLockFileEx(
! 33508: HANDLE *phFile,
! 33509: DWORD dwFlags,
! 33510: DWORD dwReserved,
! 33511: DWORD nNumberOfBytesToLockLow,
! 33512: DWORD nNumberOfBytesToLockHigh,
! 33513: LPOVERLAPPED lpOverlapped
! 33514: ){
! 33515: UNUSED_PARAMETER(dwReserved);
! 33516: UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
! 33517:
! 33518: /* If the caller wants a shared read lock, forward this call
! 33519: ** to winceLockFile */
! 33520: if (lpOverlapped->Offset == (DWORD)SHARED_FIRST &&
! 33521: dwFlags == 1 &&
! 33522: nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
! 33523: return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0);
! 33524: }
! 33525: return FALSE;
! 33526: }
! 33527: /*
! 33528: ** End of the special code for wince
! 33529: *****************************************************************************/
! 33530: #endif /* SQLITE_OS_WINCE */
! 33531:
! 33532: /*****************************************************************************
! 33533: ** The next group of routines implement the I/O methods specified
! 33534: ** by the sqlite3_io_methods object.
! 33535: ******************************************************************************/
! 33536:
! 33537: /*
! 33538: ** Some Microsoft compilers lack this definition.
! 33539: */
! 33540: #ifndef INVALID_SET_FILE_POINTER
! 33541: # define INVALID_SET_FILE_POINTER ((DWORD)-1)
! 33542: #endif
! 33543:
! 33544: /*
! 33545: ** Move the current position of the file handle passed as the first
! 33546: ** argument to offset iOffset within the file. If successful, return 0.
! 33547: ** Otherwise, set pFile->lastErrno and return non-zero.
! 33548: */
! 33549: static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
! 33550: LONG upperBits; /* Most sig. 32 bits of new offset */
! 33551: LONG lowerBits; /* Least sig. 32 bits of new offset */
! 33552: DWORD dwRet; /* Value returned by SetFilePointer() */
! 33553: DWORD lastErrno; /* Value returned by GetLastError() */
! 33554:
! 33555: upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
! 33556: lowerBits = (LONG)(iOffset & 0xffffffff);
! 33557:
! 33558: /* API oddity: If successful, SetFilePointer() returns a dword
! 33559: ** containing the lower 32-bits of the new file-offset. Or, if it fails,
! 33560: ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
! 33561: ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
! 33562: ** whether an error has actually occured, it is also necessary to call
! 33563: ** GetLastError().
! 33564: */
! 33565: dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
! 33566:
! 33567: if( (dwRet==INVALID_SET_FILE_POINTER
! 33568: && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
! 33569: pFile->lastErrno = lastErrno;
! 33570: winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
! 33571: "seekWinFile", pFile->zPath);
! 33572: return 1;
! 33573: }
! 33574:
! 33575: return 0;
! 33576: }
! 33577:
! 33578: /*
! 33579: ** Close a file.
! 33580: **
! 33581: ** It is reported that an attempt to close a handle might sometimes
! 33582: ** fail. This is a very unreasonable result, but Windows is notorious
! 33583: ** for being unreasonable so I do not doubt that it might happen. If
! 33584: ** the close fails, we pause for 100 milliseconds and try again. As
! 33585: ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
! 33586: ** giving up and returning an error.
! 33587: */
! 33588: #define MX_CLOSE_ATTEMPT 3
! 33589: static int winClose(sqlite3_file *id){
! 33590: int rc, cnt = 0;
! 33591: winFile *pFile = (winFile*)id;
! 33592:
! 33593: assert( id!=0 );
! 33594: assert( pFile->pShm==0 );
! 33595: OSTRACE(("CLOSE %d\n", pFile->h));
! 33596: do{
! 33597: rc = osCloseHandle(pFile->h);
! 33598: /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
! 33599: }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (osSleep(100), 1) );
! 33600: #if SQLITE_OS_WINCE
! 33601: #define WINCE_DELETION_ATTEMPTS 3
! 33602: winceDestroyLock(pFile);
! 33603: if( pFile->zDeleteOnClose ){
! 33604: int cnt = 0;
! 33605: while(
! 33606: osDeleteFileW(pFile->zDeleteOnClose)==0
! 33607: && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
! 33608: && cnt++ < WINCE_DELETION_ATTEMPTS
! 33609: ){
! 33610: osSleep(100); /* Wait a little before trying again */
! 33611: }
! 33612: sqlite3_free(pFile->zDeleteOnClose);
! 33613: }
! 33614: #endif
! 33615: OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
! 33616: OpenCounter(-1);
! 33617: return rc ? SQLITE_OK
! 33618: : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
! 33619: "winClose", pFile->zPath);
! 33620: }
! 33621:
! 33622: /*
! 33623: ** Read data from a file into a buffer. Return SQLITE_OK if all
! 33624: ** bytes were read successfully and SQLITE_IOERR if anything goes
! 33625: ** wrong.
! 33626: */
! 33627: static int winRead(
! 33628: sqlite3_file *id, /* File to read from */
! 33629: void *pBuf, /* Write content into this buffer */
! 33630: int amt, /* Number of bytes to read */
! 33631: sqlite3_int64 offset /* Begin reading at this offset */
! 33632: ){
! 33633: winFile *pFile = (winFile*)id; /* file handle */
! 33634: DWORD nRead; /* Number of bytes actually read from file */
! 33635: int nRetry = 0; /* Number of retrys */
! 33636:
! 33637: assert( id!=0 );
! 33638: SimulateIOError(return SQLITE_IOERR_READ);
! 33639: OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
! 33640:
! 33641: if( seekWinFile(pFile, offset) ){
! 33642: return SQLITE_FULL;
! 33643: }
! 33644: while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
! 33645: DWORD lastErrno;
! 33646: if( retryIoerr(&nRetry, &lastErrno) ) continue;
! 33647: pFile->lastErrno = lastErrno;
! 33648: return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
! 33649: "winRead", pFile->zPath);
! 33650: }
! 33651: logIoerr(nRetry);
! 33652: if( nRead<(DWORD)amt ){
! 33653: /* Unread parts of the buffer must be zero-filled */
! 33654: memset(&((char*)pBuf)[nRead], 0, amt-nRead);
! 33655: return SQLITE_IOERR_SHORT_READ;
! 33656: }
! 33657:
! 33658: return SQLITE_OK;
! 33659: }
! 33660:
! 33661: /*
! 33662: ** Write data from a buffer into a file. Return SQLITE_OK on success
! 33663: ** or some other error code on failure.
! 33664: */
! 33665: static int winWrite(
! 33666: sqlite3_file *id, /* File to write into */
! 33667: const void *pBuf, /* The bytes to be written */
! 33668: int amt, /* Number of bytes to write */
! 33669: sqlite3_int64 offset /* Offset into the file to begin writing at */
! 33670: ){
! 33671: int rc; /* True if error has occured, else false */
! 33672: winFile *pFile = (winFile*)id; /* File handle */
! 33673: int nRetry = 0; /* Number of retries */
! 33674:
! 33675: assert( amt>0 );
! 33676: assert( pFile );
! 33677: SimulateIOError(return SQLITE_IOERR_WRITE);
! 33678: SimulateDiskfullError(return SQLITE_FULL);
! 33679:
! 33680: OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
! 33681:
! 33682: rc = seekWinFile(pFile, offset);
! 33683: if( rc==0 ){
! 33684: u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
! 33685: int nRem = amt; /* Number of bytes yet to be written */
! 33686: DWORD nWrite; /* Bytes written by each WriteFile() call */
! 33687: DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
! 33688:
! 33689: while( nRem>0 ){
! 33690: if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
! 33691: if( retryIoerr(&nRetry, &lastErrno) ) continue;
! 33692: break;
! 33693: }
! 33694: if( nWrite<=0 ) break;
! 33695: aRem += nWrite;
! 33696: nRem -= nWrite;
! 33697: }
! 33698: if( nRem>0 ){
! 33699: pFile->lastErrno = lastErrno;
! 33700: rc = 1;
! 33701: }
! 33702: }
! 33703:
! 33704: if( rc ){
! 33705: if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
! 33706: || ( pFile->lastErrno==ERROR_DISK_FULL )){
! 33707: return SQLITE_FULL;
! 33708: }
! 33709: return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
! 33710: "winWrite", pFile->zPath);
! 33711: }else{
! 33712: logIoerr(nRetry);
! 33713: }
! 33714: return SQLITE_OK;
! 33715: }
! 33716:
! 33717: /*
! 33718: ** Truncate an open file to a specified size
! 33719: */
! 33720: static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
! 33721: winFile *pFile = (winFile*)id; /* File handle object */
! 33722: int rc = SQLITE_OK; /* Return code for this function */
! 33723:
! 33724: assert( pFile );
! 33725:
! 33726: OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
! 33727: SimulateIOError(return SQLITE_IOERR_TRUNCATE);
! 33728:
! 33729: /* If the user has configured a chunk-size for this file, truncate the
! 33730: ** file so that it consists of an integer number of chunks (i.e. the
! 33731: ** actual file size after the operation may be larger than the requested
! 33732: ** size).
! 33733: */
! 33734: if( pFile->szChunk>0 ){
! 33735: nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
! 33736: }
! 33737:
! 33738: /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
! 33739: if( seekWinFile(pFile, nByte) ){
! 33740: rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
! 33741: "winTruncate1", pFile->zPath);
! 33742: }else if( 0==osSetEndOfFile(pFile->h) ){
! 33743: pFile->lastErrno = osGetLastError();
! 33744: rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
! 33745: "winTruncate2", pFile->zPath);
! 33746: }
! 33747:
! 33748: OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
! 33749: return rc;
! 33750: }
! 33751:
! 33752: #ifdef SQLITE_TEST
! 33753: /*
! 33754: ** Count the number of fullsyncs and normal syncs. This is used to test
! 33755: ** that syncs and fullsyncs are occuring at the right times.
! 33756: */
! 33757: SQLITE_API int sqlite3_sync_count = 0;
! 33758: SQLITE_API int sqlite3_fullsync_count = 0;
! 33759: #endif
! 33760:
! 33761: /*
! 33762: ** Make sure all writes to a particular file are committed to disk.
! 33763: */
! 33764: static int winSync(sqlite3_file *id, int flags){
! 33765: #ifndef SQLITE_NO_SYNC
! 33766: /*
! 33767: ** Used only when SQLITE_NO_SYNC is not defined.
! 33768: */
! 33769: BOOL rc;
! 33770: #endif
! 33771: #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
! 33772: (defined(SQLITE_TEST) && defined(SQLITE_DEBUG))
! 33773: /*
! 33774: ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
! 33775: ** OSTRACE() macros.
! 33776: */
! 33777: winFile *pFile = (winFile*)id;
! 33778: #else
! 33779: UNUSED_PARAMETER(id);
! 33780: #endif
! 33781:
! 33782: assert( pFile );
! 33783: /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
! 33784: assert((flags&0x0F)==SQLITE_SYNC_NORMAL
! 33785: || (flags&0x0F)==SQLITE_SYNC_FULL
! 33786: );
! 33787:
! 33788: OSTRACE(("SYNC %d lock=%d\n", pFile->h, pFile->locktype));
! 33789:
! 33790: /* Unix cannot, but some systems may return SQLITE_FULL from here. This
! 33791: ** line is to test that doing so does not cause any problems.
! 33792: */
! 33793: SimulateDiskfullError( return SQLITE_FULL );
! 33794:
! 33795: #ifndef SQLITE_TEST
! 33796: UNUSED_PARAMETER(flags);
! 33797: #else
! 33798: if( (flags&0x0F)==SQLITE_SYNC_FULL ){
! 33799: sqlite3_fullsync_count++;
! 33800: }
! 33801: sqlite3_sync_count++;
! 33802: #endif
! 33803:
! 33804: /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
! 33805: ** no-op
! 33806: */
! 33807: #ifdef SQLITE_NO_SYNC
! 33808: return SQLITE_OK;
! 33809: #else
! 33810: rc = osFlushFileBuffers(pFile->h);
! 33811: SimulateIOError( rc=FALSE );
! 33812: if( rc ){
! 33813: return SQLITE_OK;
! 33814: }else{
! 33815: pFile->lastErrno = osGetLastError();
! 33816: return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
! 33817: "winSync", pFile->zPath);
! 33818: }
! 33819: #endif
! 33820: }
! 33821:
! 33822: /*
! 33823: ** Determine the current size of a file in bytes
! 33824: */
! 33825: static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
! 33826: DWORD upperBits;
! 33827: DWORD lowerBits;
! 33828: winFile *pFile = (winFile*)id;
! 33829: DWORD lastErrno;
! 33830:
! 33831: assert( id!=0 );
! 33832: SimulateIOError(return SQLITE_IOERR_FSTAT);
! 33833: lowerBits = osGetFileSize(pFile->h, &upperBits);
! 33834: if( (lowerBits == INVALID_FILE_SIZE)
! 33835: && ((lastErrno = osGetLastError())!=NO_ERROR) )
! 33836: {
! 33837: pFile->lastErrno = lastErrno;
! 33838: return winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
! 33839: "winFileSize", pFile->zPath);
! 33840: }
! 33841: *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
! 33842: return SQLITE_OK;
! 33843: }
! 33844:
! 33845: /*
! 33846: ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
! 33847: */
! 33848: #ifndef LOCKFILE_FAIL_IMMEDIATELY
! 33849: # define LOCKFILE_FAIL_IMMEDIATELY 1
! 33850: #endif
! 33851:
! 33852: /*
! 33853: ** Acquire a reader lock.
! 33854: ** Different API routines are called depending on whether or not this
! 33855: ** is Win9x or WinNT.
! 33856: */
! 33857: static int getReadLock(winFile *pFile){
! 33858: int res;
! 33859: if( isNT() ){
! 33860: OVERLAPPED ovlp;
! 33861: ovlp.Offset = SHARED_FIRST;
! 33862: ovlp.OffsetHigh = 0;
! 33863: ovlp.hEvent = 0;
! 33864: res = osLockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
! 33865: 0, SHARED_SIZE, 0, &ovlp);
! 33866: /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
! 33867: */
! 33868: #if SQLITE_OS_WINCE==0
! 33869: }else{
! 33870: int lk;
! 33871: sqlite3_randomness(sizeof(lk), &lk);
! 33872: pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
! 33873: res = osLockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
! 33874: #endif
! 33875: }
! 33876: if( res == 0 ){
! 33877: pFile->lastErrno = osGetLastError();
! 33878: /* No need to log a failure to lock */
! 33879: }
! 33880: return res;
! 33881: }
! 33882:
! 33883: /*
! 33884: ** Undo a readlock
! 33885: */
! 33886: static int unlockReadLock(winFile *pFile){
! 33887: int res;
! 33888: DWORD lastErrno;
! 33889: if( isNT() ){
! 33890: res = osUnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
! 33891: /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
! 33892: */
! 33893: #if SQLITE_OS_WINCE==0
! 33894: }else{
! 33895: res = osUnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
! 33896: #endif
! 33897: }
! 33898: if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
! 33899: pFile->lastErrno = lastErrno;
! 33900: winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
! 33901: "unlockReadLock", pFile->zPath);
! 33902: }
! 33903: return res;
! 33904: }
! 33905:
! 33906: /*
! 33907: ** Lock the file with the lock specified by parameter locktype - one
! 33908: ** of the following:
! 33909: **
! 33910: ** (1) SHARED_LOCK
! 33911: ** (2) RESERVED_LOCK
! 33912: ** (3) PENDING_LOCK
! 33913: ** (4) EXCLUSIVE_LOCK
! 33914: **
! 33915: ** Sometimes when requesting one lock state, additional lock states
! 33916: ** are inserted in between. The locking might fail on one of the later
! 33917: ** transitions leaving the lock state different from what it started but
! 33918: ** still short of its goal. The following chart shows the allowed
! 33919: ** transitions and the inserted intermediate states:
! 33920: **
! 33921: ** UNLOCKED -> SHARED
! 33922: ** SHARED -> RESERVED
! 33923: ** SHARED -> (PENDING) -> EXCLUSIVE
! 33924: ** RESERVED -> (PENDING) -> EXCLUSIVE
! 33925: ** PENDING -> EXCLUSIVE
! 33926: **
! 33927: ** This routine will only increase a lock. The winUnlock() routine
! 33928: ** erases all locks at once and returns us immediately to locking level 0.
! 33929: ** It is not possible to lower the locking level one step at a time. You
! 33930: ** must go straight to locking level 0.
! 33931: */
! 33932: static int winLock(sqlite3_file *id, int locktype){
! 33933: int rc = SQLITE_OK; /* Return code from subroutines */
! 33934: int res = 1; /* Result of a Windows lock call */
! 33935: int newLocktype; /* Set pFile->locktype to this value before exiting */
! 33936: int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
! 33937: winFile *pFile = (winFile*)id;
! 33938: DWORD lastErrno = NO_ERROR;
! 33939:
! 33940: assert( id!=0 );
! 33941: OSTRACE(("LOCK %d %d was %d(%d)\n",
! 33942: pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
! 33943:
! 33944: /* If there is already a lock of this type or more restrictive on the
! 33945: ** OsFile, do nothing. Don't use the end_lock: exit path, as
! 33946: ** sqlite3OsEnterMutex() hasn't been called yet.
! 33947: */
! 33948: if( pFile->locktype>=locktype ){
! 33949: return SQLITE_OK;
! 33950: }
! 33951:
! 33952: /* Make sure the locking sequence is correct
! 33953: */
! 33954: assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
! 33955: assert( locktype!=PENDING_LOCK );
! 33956: assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
! 33957:
! 33958: /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
! 33959: ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
! 33960: ** the PENDING_LOCK byte is temporary.
! 33961: */
! 33962: newLocktype = pFile->locktype;
! 33963: if( (pFile->locktype==NO_LOCK)
! 33964: || ( (locktype==EXCLUSIVE_LOCK)
! 33965: && (pFile->locktype==RESERVED_LOCK))
! 33966: ){
! 33967: int cnt = 3;
! 33968: while( cnt-->0 && (res = osLockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
! 33969: /* Try 3 times to get the pending lock. This is needed to work
! 33970: ** around problems caused by indexing and/or anti-virus software on
! 33971: ** Windows systems.
! 33972: ** If you are using this code as a model for alternative VFSes, do not
! 33973: ** copy this retry logic. It is a hack intended for Windows only.
! 33974: */
! 33975: OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
! 33976: if( cnt ) osSleep(1);
! 33977: }
! 33978: gotPendingLock = res;
! 33979: if( !res ){
! 33980: lastErrno = osGetLastError();
! 33981: }
! 33982: }
! 33983:
! 33984: /* Acquire a shared lock
! 33985: */
! 33986: if( locktype==SHARED_LOCK && res ){
! 33987: assert( pFile->locktype==NO_LOCK );
! 33988: res = getReadLock(pFile);
! 33989: if( res ){
! 33990: newLocktype = SHARED_LOCK;
! 33991: }else{
! 33992: lastErrno = osGetLastError();
! 33993: }
! 33994: }
! 33995:
! 33996: /* Acquire a RESERVED lock
! 33997: */
! 33998: if( locktype==RESERVED_LOCK && res ){
! 33999: assert( pFile->locktype==SHARED_LOCK );
! 34000: res = osLockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
! 34001: if( res ){
! 34002: newLocktype = RESERVED_LOCK;
! 34003: }else{
! 34004: lastErrno = osGetLastError();
! 34005: }
! 34006: }
! 34007:
! 34008: /* Acquire a PENDING lock
! 34009: */
! 34010: if( locktype==EXCLUSIVE_LOCK && res ){
! 34011: newLocktype = PENDING_LOCK;
! 34012: gotPendingLock = 0;
! 34013: }
! 34014:
! 34015: /* Acquire an EXCLUSIVE lock
! 34016: */
! 34017: if( locktype==EXCLUSIVE_LOCK && res ){
! 34018: assert( pFile->locktype>=SHARED_LOCK );
! 34019: res = unlockReadLock(pFile);
! 34020: OSTRACE(("unreadlock = %d\n", res));
! 34021: res = osLockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
! 34022: if( res ){
! 34023: newLocktype = EXCLUSIVE_LOCK;
! 34024: }else{
! 34025: lastErrno = osGetLastError();
! 34026: OSTRACE(("error-code = %d\n", lastErrno));
! 34027: getReadLock(pFile);
! 34028: }
! 34029: }
! 34030:
! 34031: /* If we are holding a PENDING lock that ought to be released, then
! 34032: ** release it now.
! 34033: */
! 34034: if( gotPendingLock && locktype==SHARED_LOCK ){
! 34035: osUnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
! 34036: }
! 34037:
! 34038: /* Update the state of the lock has held in the file descriptor then
! 34039: ** return the appropriate result code.
! 34040: */
! 34041: if( res ){
! 34042: rc = SQLITE_OK;
! 34043: }else{
! 34044: OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
! 34045: locktype, newLocktype));
! 34046: pFile->lastErrno = lastErrno;
! 34047: rc = SQLITE_BUSY;
! 34048: }
! 34049: pFile->locktype = (u8)newLocktype;
! 34050: return rc;
! 34051: }
! 34052:
! 34053: /*
! 34054: ** This routine checks if there is a RESERVED lock held on the specified
! 34055: ** file by this or any other process. If such a lock is held, return
! 34056: ** non-zero, otherwise zero.
! 34057: */
! 34058: static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
! 34059: int rc;
! 34060: winFile *pFile = (winFile*)id;
! 34061:
! 34062: SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
! 34063:
! 34064: assert( id!=0 );
! 34065: if( pFile->locktype>=RESERVED_LOCK ){
! 34066: rc = 1;
! 34067: OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
! 34068: }else{
! 34069: rc = osLockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
! 34070: if( rc ){
! 34071: osUnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
! 34072: }
! 34073: rc = !rc;
! 34074: OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
! 34075: }
! 34076: *pResOut = rc;
! 34077: return SQLITE_OK;
! 34078: }
! 34079:
! 34080: /*
! 34081: ** Lower the locking level on file descriptor id to locktype. locktype
! 34082: ** must be either NO_LOCK or SHARED_LOCK.
! 34083: **
! 34084: ** If the locking level of the file descriptor is already at or below
! 34085: ** the requested locking level, this routine is a no-op.
! 34086: **
! 34087: ** It is not possible for this routine to fail if the second argument
! 34088: ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
! 34089: ** might return SQLITE_IOERR;
! 34090: */
! 34091: static int winUnlock(sqlite3_file *id, int locktype){
! 34092: int type;
! 34093: winFile *pFile = (winFile*)id;
! 34094: int rc = SQLITE_OK;
! 34095: assert( pFile!=0 );
! 34096: assert( locktype<=SHARED_LOCK );
! 34097: OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
! 34098: pFile->locktype, pFile->sharedLockByte));
! 34099: type = pFile->locktype;
! 34100: if( type>=EXCLUSIVE_LOCK ){
! 34101: osUnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
! 34102: if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
! 34103: /* This should never happen. We should always be able to
! 34104: ** reacquire the read lock */
! 34105: rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
! 34106: "winUnlock", pFile->zPath);
! 34107: }
! 34108: }
! 34109: if( type>=RESERVED_LOCK ){
! 34110: osUnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
! 34111: }
! 34112: if( locktype==NO_LOCK && type>=SHARED_LOCK ){
! 34113: unlockReadLock(pFile);
! 34114: }
! 34115: if( type>=PENDING_LOCK ){
! 34116: osUnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
! 34117: }
! 34118: pFile->locktype = (u8)locktype;
! 34119: return rc;
! 34120: }
! 34121:
! 34122: /*
! 34123: ** If *pArg is inititially negative then this is a query. Set *pArg to
! 34124: ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
! 34125: **
! 34126: ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
! 34127: */
! 34128: static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
! 34129: if( *pArg<0 ){
! 34130: *pArg = (pFile->ctrlFlags & mask)!=0;
! 34131: }else if( (*pArg)==0 ){
! 34132: pFile->ctrlFlags &= ~mask;
! 34133: }else{
! 34134: pFile->ctrlFlags |= mask;
! 34135: }
! 34136: }
! 34137:
! 34138: /*
! 34139: ** Control and query of the open file handle.
! 34140: */
! 34141: static int winFileControl(sqlite3_file *id, int op, void *pArg){
! 34142: winFile *pFile = (winFile*)id;
! 34143: switch( op ){
! 34144: case SQLITE_FCNTL_LOCKSTATE: {
! 34145: *(int*)pArg = pFile->locktype;
! 34146: return SQLITE_OK;
! 34147: }
! 34148: case SQLITE_LAST_ERRNO: {
! 34149: *(int*)pArg = (int)pFile->lastErrno;
! 34150: return SQLITE_OK;
! 34151: }
! 34152: case SQLITE_FCNTL_CHUNK_SIZE: {
! 34153: pFile->szChunk = *(int *)pArg;
! 34154: return SQLITE_OK;
! 34155: }
! 34156: case SQLITE_FCNTL_SIZE_HINT: {
! 34157: if( pFile->szChunk>0 ){
! 34158: sqlite3_int64 oldSz;
! 34159: int rc = winFileSize(id, &oldSz);
! 34160: if( rc==SQLITE_OK ){
! 34161: sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
! 34162: if( newSz>oldSz ){
! 34163: SimulateIOErrorBenign(1);
! 34164: rc = winTruncate(id, newSz);
! 34165: SimulateIOErrorBenign(0);
! 34166: }
! 34167: }
! 34168: return rc;
! 34169: }
! 34170: return SQLITE_OK;
! 34171: }
! 34172: case SQLITE_FCNTL_PERSIST_WAL: {
! 34173: winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
! 34174: return SQLITE_OK;
! 34175: }
! 34176: case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
! 34177: winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
! 34178: return SQLITE_OK;
! 34179: }
! 34180: case SQLITE_FCNTL_VFSNAME: {
! 34181: *(char**)pArg = sqlite3_mprintf("win32");
! 34182: return SQLITE_OK;
! 34183: }
! 34184: case SQLITE_FCNTL_WIN32_AV_RETRY: {
! 34185: int *a = (int*)pArg;
! 34186: if( a[0]>0 ){
! 34187: win32IoerrRetry = a[0];
! 34188: }else{
! 34189: a[0] = win32IoerrRetry;
! 34190: }
! 34191: if( a[1]>0 ){
! 34192: win32IoerrRetryDelay = a[1];
! 34193: }else{
! 34194: a[1] = win32IoerrRetryDelay;
! 34195: }
! 34196: return SQLITE_OK;
! 34197: }
! 34198: }
! 34199: return SQLITE_NOTFOUND;
! 34200: }
! 34201:
! 34202: /*
! 34203: ** Return the sector size in bytes of the underlying block device for
! 34204: ** the specified file. This is almost always 512 bytes, but may be
! 34205: ** larger for some devices.
! 34206: **
! 34207: ** SQLite code assumes this function cannot fail. It also assumes that
! 34208: ** if two files are created in the same file-system directory (i.e.
! 34209: ** a database and its journal file) that the sector size will be the
! 34210: ** same for both.
! 34211: */
! 34212: static int winSectorSize(sqlite3_file *id){
! 34213: (void)id;
! 34214: return SQLITE_DEFAULT_SECTOR_SIZE;
! 34215: }
! 34216:
! 34217: /*
! 34218: ** Return a vector of device characteristics.
! 34219: */
! 34220: static int winDeviceCharacteristics(sqlite3_file *id){
! 34221: winFile *p = (winFile*)id;
! 34222: return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
! 34223: ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
! 34224: }
! 34225:
! 34226: #ifndef SQLITE_OMIT_WAL
! 34227:
! 34228: /*
! 34229: ** Windows will only let you create file view mappings
! 34230: ** on allocation size granularity boundaries.
! 34231: ** During sqlite3_os_init() we do a GetSystemInfo()
! 34232: ** to get the granularity size.
! 34233: */
! 34234: SYSTEM_INFO winSysInfo;
! 34235:
! 34236: /*
! 34237: ** Helper functions to obtain and relinquish the global mutex. The
! 34238: ** global mutex is used to protect the winLockInfo objects used by
! 34239: ** this file, all of which may be shared by multiple threads.
! 34240: **
! 34241: ** Function winShmMutexHeld() is used to assert() that the global mutex
! 34242: ** is held when required. This function is only used as part of assert()
! 34243: ** statements. e.g.
! 34244: **
! 34245: ** winShmEnterMutex()
! 34246: ** assert( winShmMutexHeld() );
! 34247: ** winShmLeaveMutex()
! 34248: */
! 34249: static void winShmEnterMutex(void){
! 34250: sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 34251: }
! 34252: static void winShmLeaveMutex(void){
! 34253: sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 34254: }
! 34255: #ifdef SQLITE_DEBUG
! 34256: static int winShmMutexHeld(void) {
! 34257: return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 34258: }
! 34259: #endif
! 34260:
! 34261: /*
! 34262: ** Object used to represent a single file opened and mmapped to provide
! 34263: ** shared memory. When multiple threads all reference the same
! 34264: ** log-summary, each thread has its own winFile object, but they all
! 34265: ** point to a single instance of this object. In other words, each
! 34266: ** log-summary is opened only once per process.
! 34267: **
! 34268: ** winShmMutexHeld() must be true when creating or destroying
! 34269: ** this object or while reading or writing the following fields:
! 34270: **
! 34271: ** nRef
! 34272: ** pNext
! 34273: **
! 34274: ** The following fields are read-only after the object is created:
! 34275: **
! 34276: ** fid
! 34277: ** zFilename
! 34278: **
! 34279: ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
! 34280: ** winShmMutexHeld() is true when reading or writing any other field
! 34281: ** in this structure.
! 34282: **
! 34283: */
! 34284: struct winShmNode {
! 34285: sqlite3_mutex *mutex; /* Mutex to access this object */
! 34286: char *zFilename; /* Name of the file */
! 34287: winFile hFile; /* File handle from winOpen */
! 34288:
! 34289: int szRegion; /* Size of shared-memory regions */
! 34290: int nRegion; /* Size of array apRegion */
! 34291: struct ShmRegion {
! 34292: HANDLE hMap; /* File handle from CreateFileMapping */
! 34293: void *pMap;
! 34294: } *aRegion;
! 34295: DWORD lastErrno; /* The Windows errno from the last I/O error */
! 34296:
! 34297: int nRef; /* Number of winShm objects pointing to this */
! 34298: winShm *pFirst; /* All winShm objects pointing to this */
! 34299: winShmNode *pNext; /* Next in list of all winShmNode objects */
! 34300: #ifdef SQLITE_DEBUG
! 34301: u8 nextShmId; /* Next available winShm.id value */
! 34302: #endif
! 34303: };
! 34304:
! 34305: /*
! 34306: ** A global array of all winShmNode objects.
! 34307: **
! 34308: ** The winShmMutexHeld() must be true while reading or writing this list.
! 34309: */
! 34310: static winShmNode *winShmNodeList = 0;
! 34311:
! 34312: /*
! 34313: ** Structure used internally by this VFS to record the state of an
! 34314: ** open shared memory connection.
! 34315: **
! 34316: ** The following fields are initialized when this object is created and
! 34317: ** are read-only thereafter:
! 34318: **
! 34319: ** winShm.pShmNode
! 34320: ** winShm.id
! 34321: **
! 34322: ** All other fields are read/write. The winShm.pShmNode->mutex must be held
! 34323: ** while accessing any read/write fields.
! 34324: */
! 34325: struct winShm {
! 34326: winShmNode *pShmNode; /* The underlying winShmNode object */
! 34327: winShm *pNext; /* Next winShm with the same winShmNode */
! 34328: u8 hasMutex; /* True if holding the winShmNode mutex */
! 34329: u16 sharedMask; /* Mask of shared locks held */
! 34330: u16 exclMask; /* Mask of exclusive locks held */
! 34331: #ifdef SQLITE_DEBUG
! 34332: u8 id; /* Id of this connection with its winShmNode */
! 34333: #endif
! 34334: };
! 34335:
! 34336: /*
! 34337: ** Constants used for locking
! 34338: */
! 34339: #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
! 34340: #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
! 34341:
! 34342: /*
! 34343: ** Apply advisory locks for all n bytes beginning at ofst.
! 34344: */
! 34345: #define _SHM_UNLCK 1
! 34346: #define _SHM_RDLCK 2
! 34347: #define _SHM_WRLCK 3
! 34348: static int winShmSystemLock(
! 34349: winShmNode *pFile, /* Apply locks to this open shared-memory segment */
! 34350: int lockType, /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
! 34351: int ofst, /* Offset to first byte to be locked/unlocked */
! 34352: int nByte /* Number of bytes to lock or unlock */
! 34353: ){
! 34354: OVERLAPPED ovlp;
! 34355: DWORD dwFlags;
! 34356: int rc = 0; /* Result code form Lock/UnlockFileEx() */
! 34357:
! 34358: /* Access to the winShmNode object is serialized by the caller */
! 34359: assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
! 34360:
! 34361: /* Initialize the locking parameters */
! 34362: dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
! 34363: if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
! 34364:
! 34365: memset(&ovlp, 0, sizeof(OVERLAPPED));
! 34366: ovlp.Offset = ofst;
! 34367:
! 34368: /* Release/Acquire the system-level lock */
! 34369: if( lockType==_SHM_UNLCK ){
! 34370: rc = osUnlockFileEx(pFile->hFile.h, 0, nByte, 0, &ovlp);
! 34371: }else{
! 34372: rc = osLockFileEx(pFile->hFile.h, dwFlags, 0, nByte, 0, &ovlp);
! 34373: }
! 34374:
! 34375: if( rc!= 0 ){
! 34376: rc = SQLITE_OK;
! 34377: }else{
! 34378: pFile->lastErrno = osGetLastError();
! 34379: rc = SQLITE_BUSY;
! 34380: }
! 34381:
! 34382: OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
! 34383: pFile->hFile.h,
! 34384: rc==SQLITE_OK ? "ok" : "failed",
! 34385: lockType==_SHM_UNLCK ? "UnlockFileEx" : "LockFileEx",
! 34386: pFile->lastErrno));
! 34387:
! 34388: return rc;
! 34389: }
! 34390:
! 34391: /* Forward references to VFS methods */
! 34392: static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
! 34393: static int winDelete(sqlite3_vfs *,const char*,int);
! 34394:
! 34395: /*
! 34396: ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
! 34397: **
! 34398: ** This is not a VFS shared-memory method; it is a utility function called
! 34399: ** by VFS shared-memory methods.
! 34400: */
! 34401: static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
! 34402: winShmNode **pp;
! 34403: winShmNode *p;
! 34404: BOOL bRc;
! 34405: assert( winShmMutexHeld() );
! 34406: pp = &winShmNodeList;
! 34407: while( (p = *pp)!=0 ){
! 34408: if( p->nRef==0 ){
! 34409: int i;
! 34410: if( p->mutex ) sqlite3_mutex_free(p->mutex);
! 34411: for(i=0; i<p->nRegion; i++){
! 34412: bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
! 34413: OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
! 34414: (int)osGetCurrentProcessId(), i,
! 34415: bRc ? "ok" : "failed"));
! 34416: bRc = osCloseHandle(p->aRegion[i].hMap);
! 34417: OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
! 34418: (int)osGetCurrentProcessId(), i,
! 34419: bRc ? "ok" : "failed"));
! 34420: }
! 34421: if( p->hFile.h != INVALID_HANDLE_VALUE ){
! 34422: SimulateIOErrorBenign(1);
! 34423: winClose((sqlite3_file *)&p->hFile);
! 34424: SimulateIOErrorBenign(0);
! 34425: }
! 34426: if( deleteFlag ){
! 34427: SimulateIOErrorBenign(1);
! 34428: sqlite3BeginBenignMalloc();
! 34429: winDelete(pVfs, p->zFilename, 0);
! 34430: sqlite3EndBenignMalloc();
! 34431: SimulateIOErrorBenign(0);
! 34432: }
! 34433: *pp = p->pNext;
! 34434: sqlite3_free(p->aRegion);
! 34435: sqlite3_free(p);
! 34436: }else{
! 34437: pp = &p->pNext;
! 34438: }
! 34439: }
! 34440: }
! 34441:
! 34442: /*
! 34443: ** Open the shared-memory area associated with database file pDbFd.
! 34444: **
! 34445: ** When opening a new shared-memory file, if no other instances of that
! 34446: ** file are currently open, in this process or in other processes, then
! 34447: ** the file must be truncated to zero length or have its header cleared.
! 34448: */
! 34449: static int winOpenSharedMemory(winFile *pDbFd){
! 34450: struct winShm *p; /* The connection to be opened */
! 34451: struct winShmNode *pShmNode = 0; /* The underlying mmapped file */
! 34452: int rc; /* Result code */
! 34453: struct winShmNode *pNew; /* Newly allocated winShmNode */
! 34454: int nName; /* Size of zName in bytes */
! 34455:
! 34456: assert( pDbFd->pShm==0 ); /* Not previously opened */
! 34457:
! 34458: /* Allocate space for the new sqlite3_shm object. Also speculatively
! 34459: ** allocate space for a new winShmNode and filename.
! 34460: */
! 34461: p = sqlite3_malloc( sizeof(*p) );
! 34462: if( p==0 ) return SQLITE_IOERR_NOMEM;
! 34463: memset(p, 0, sizeof(*p));
! 34464: nName = sqlite3Strlen30(pDbFd->zPath);
! 34465: pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 17 );
! 34466: if( pNew==0 ){
! 34467: sqlite3_free(p);
! 34468: return SQLITE_IOERR_NOMEM;
! 34469: }
! 34470: memset(pNew, 0, sizeof(*pNew) + nName + 17);
! 34471: pNew->zFilename = (char*)&pNew[1];
! 34472: sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
! 34473: sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
! 34474:
! 34475: /* Look to see if there is an existing winShmNode that can be used.
! 34476: ** If no matching winShmNode currently exists, create a new one.
! 34477: */
! 34478: winShmEnterMutex();
! 34479: for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
! 34480: /* TBD need to come up with better match here. Perhaps
! 34481: ** use FILE_ID_BOTH_DIR_INFO Structure.
! 34482: */
! 34483: if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
! 34484: }
! 34485: if( pShmNode ){
! 34486: sqlite3_free(pNew);
! 34487: }else{
! 34488: pShmNode = pNew;
! 34489: pNew = 0;
! 34490: ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
! 34491: pShmNode->pNext = winShmNodeList;
! 34492: winShmNodeList = pShmNode;
! 34493:
! 34494: pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
! 34495: if( pShmNode->mutex==0 ){
! 34496: rc = SQLITE_IOERR_NOMEM;
! 34497: goto shm_open_err;
! 34498: }
! 34499:
! 34500: rc = winOpen(pDbFd->pVfs,
! 34501: pShmNode->zFilename, /* Name of the file (UTF-8) */
! 34502: (sqlite3_file*)&pShmNode->hFile, /* File handle here */
! 34503: SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
! 34504: 0);
! 34505: if( SQLITE_OK!=rc ){
! 34506: goto shm_open_err;
! 34507: }
! 34508:
! 34509: /* Check to see if another process is holding the dead-man switch.
! 34510: ** If not, truncate the file to zero length.
! 34511: */
! 34512: if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
! 34513: rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
! 34514: if( rc!=SQLITE_OK ){
! 34515: rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
! 34516: "winOpenShm", pDbFd->zPath);
! 34517: }
! 34518: }
! 34519: if( rc==SQLITE_OK ){
! 34520: winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
! 34521: rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
! 34522: }
! 34523: if( rc ) goto shm_open_err;
! 34524: }
! 34525:
! 34526: /* Make the new connection a child of the winShmNode */
! 34527: p->pShmNode = pShmNode;
! 34528: #ifdef SQLITE_DEBUG
! 34529: p->id = pShmNode->nextShmId++;
! 34530: #endif
! 34531: pShmNode->nRef++;
! 34532: pDbFd->pShm = p;
! 34533: winShmLeaveMutex();
! 34534:
! 34535: /* The reference count on pShmNode has already been incremented under
! 34536: ** the cover of the winShmEnterMutex() mutex and the pointer from the
! 34537: ** new (struct winShm) object to the pShmNode has been set. All that is
! 34538: ** left to do is to link the new object into the linked list starting
! 34539: ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
! 34540: ** mutex.
! 34541: */
! 34542: sqlite3_mutex_enter(pShmNode->mutex);
! 34543: p->pNext = pShmNode->pFirst;
! 34544: pShmNode->pFirst = p;
! 34545: sqlite3_mutex_leave(pShmNode->mutex);
! 34546: return SQLITE_OK;
! 34547:
! 34548: /* Jump here on any error */
! 34549: shm_open_err:
! 34550: winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
! 34551: winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
! 34552: sqlite3_free(p);
! 34553: sqlite3_free(pNew);
! 34554: winShmLeaveMutex();
! 34555: return rc;
! 34556: }
! 34557:
! 34558: /*
! 34559: ** Close a connection to shared-memory. Delete the underlying
! 34560: ** storage if deleteFlag is true.
! 34561: */
! 34562: static int winShmUnmap(
! 34563: sqlite3_file *fd, /* Database holding shared memory */
! 34564: int deleteFlag /* Delete after closing if true */
! 34565: ){
! 34566: winFile *pDbFd; /* Database holding shared-memory */
! 34567: winShm *p; /* The connection to be closed */
! 34568: winShmNode *pShmNode; /* The underlying shared-memory file */
! 34569: winShm **pp; /* For looping over sibling connections */
! 34570:
! 34571: pDbFd = (winFile*)fd;
! 34572: p = pDbFd->pShm;
! 34573: if( p==0 ) return SQLITE_OK;
! 34574: pShmNode = p->pShmNode;
! 34575:
! 34576: /* Remove connection p from the set of connections associated
! 34577: ** with pShmNode */
! 34578: sqlite3_mutex_enter(pShmNode->mutex);
! 34579: for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
! 34580: *pp = p->pNext;
! 34581:
! 34582: /* Free the connection p */
! 34583: sqlite3_free(p);
! 34584: pDbFd->pShm = 0;
! 34585: sqlite3_mutex_leave(pShmNode->mutex);
! 34586:
! 34587: /* If pShmNode->nRef has reached 0, then close the underlying
! 34588: ** shared-memory file, too */
! 34589: winShmEnterMutex();
! 34590: assert( pShmNode->nRef>0 );
! 34591: pShmNode->nRef--;
! 34592: if( pShmNode->nRef==0 ){
! 34593: winShmPurge(pDbFd->pVfs, deleteFlag);
! 34594: }
! 34595: winShmLeaveMutex();
! 34596:
! 34597: return SQLITE_OK;
! 34598: }
! 34599:
! 34600: /*
! 34601: ** Change the lock state for a shared-memory segment.
! 34602: */
! 34603: static int winShmLock(
! 34604: sqlite3_file *fd, /* Database file holding the shared memory */
! 34605: int ofst, /* First lock to acquire or release */
! 34606: int n, /* Number of locks to acquire or release */
! 34607: int flags /* What to do with the lock */
! 34608: ){
! 34609: winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
! 34610: winShm *p = pDbFd->pShm; /* The shared memory being locked */
! 34611: winShm *pX; /* For looping over all siblings */
! 34612: winShmNode *pShmNode = p->pShmNode;
! 34613: int rc = SQLITE_OK; /* Result code */
! 34614: u16 mask; /* Mask of locks to take or release */
! 34615:
! 34616: assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
! 34617: assert( n>=1 );
! 34618: assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
! 34619: || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
! 34620: || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
! 34621: || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
! 34622: assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
! 34623:
! 34624: mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
! 34625: assert( n>1 || mask==(1<<ofst) );
! 34626: sqlite3_mutex_enter(pShmNode->mutex);
! 34627: if( flags & SQLITE_SHM_UNLOCK ){
! 34628: u16 allMask = 0; /* Mask of locks held by siblings */
! 34629:
! 34630: /* See if any siblings hold this same lock */
! 34631: for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
! 34632: if( pX==p ) continue;
! 34633: assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
! 34634: allMask |= pX->sharedMask;
! 34635: }
! 34636:
! 34637: /* Unlock the system-level locks */
! 34638: if( (mask & allMask)==0 ){
! 34639: rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
! 34640: }else{
! 34641: rc = SQLITE_OK;
! 34642: }
! 34643:
! 34644: /* Undo the local locks */
! 34645: if( rc==SQLITE_OK ){
! 34646: p->exclMask &= ~mask;
! 34647: p->sharedMask &= ~mask;
! 34648: }
! 34649: }else if( flags & SQLITE_SHM_SHARED ){
! 34650: u16 allShared = 0; /* Union of locks held by connections other than "p" */
! 34651:
! 34652: /* Find out which shared locks are already held by sibling connections.
! 34653: ** If any sibling already holds an exclusive lock, go ahead and return
! 34654: ** SQLITE_BUSY.
! 34655: */
! 34656: for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
! 34657: if( (pX->exclMask & mask)!=0 ){
! 34658: rc = SQLITE_BUSY;
! 34659: break;
! 34660: }
! 34661: allShared |= pX->sharedMask;
! 34662: }
! 34663:
! 34664: /* Get shared locks at the system level, if necessary */
! 34665: if( rc==SQLITE_OK ){
! 34666: if( (allShared & mask)==0 ){
! 34667: rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
! 34668: }else{
! 34669: rc = SQLITE_OK;
! 34670: }
! 34671: }
! 34672:
! 34673: /* Get the local shared locks */
! 34674: if( rc==SQLITE_OK ){
! 34675: p->sharedMask |= mask;
! 34676: }
! 34677: }else{
! 34678: /* Make sure no sibling connections hold locks that will block this
! 34679: ** lock. If any do, return SQLITE_BUSY right away.
! 34680: */
! 34681: for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
! 34682: if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
! 34683: rc = SQLITE_BUSY;
! 34684: break;
! 34685: }
! 34686: }
! 34687:
! 34688: /* Get the exclusive locks at the system level. Then if successful
! 34689: ** also mark the local connection as being locked.
! 34690: */
! 34691: if( rc==SQLITE_OK ){
! 34692: rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
! 34693: if( rc==SQLITE_OK ){
! 34694: assert( (p->sharedMask & mask)==0 );
! 34695: p->exclMask |= mask;
! 34696: }
! 34697: }
! 34698: }
! 34699: sqlite3_mutex_leave(pShmNode->mutex);
! 34700: OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
! 34701: p->id, (int)osGetCurrentProcessId(), p->sharedMask, p->exclMask,
! 34702: rc ? "failed" : "ok"));
! 34703: return rc;
! 34704: }
! 34705:
! 34706: /*
! 34707: ** Implement a memory barrier or memory fence on shared memory.
! 34708: **
! 34709: ** All loads and stores begun before the barrier must complete before
! 34710: ** any load or store begun after the barrier.
! 34711: */
! 34712: static void winShmBarrier(
! 34713: sqlite3_file *fd /* Database holding the shared memory */
! 34714: ){
! 34715: UNUSED_PARAMETER(fd);
! 34716: /* MemoryBarrier(); // does not work -- do not know why not */
! 34717: winShmEnterMutex();
! 34718: winShmLeaveMutex();
! 34719: }
! 34720:
! 34721: /*
! 34722: ** This function is called to obtain a pointer to region iRegion of the
! 34723: ** shared-memory associated with the database file fd. Shared-memory regions
! 34724: ** are numbered starting from zero. Each shared-memory region is szRegion
! 34725: ** bytes in size.
! 34726: **
! 34727: ** If an error occurs, an error code is returned and *pp is set to NULL.
! 34728: **
! 34729: ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
! 34730: ** region has not been allocated (by any client, including one running in a
! 34731: ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
! 34732: ** isWrite is non-zero and the requested shared-memory region has not yet
! 34733: ** been allocated, it is allocated by this function.
! 34734: **
! 34735: ** If the shared-memory region has already been allocated or is allocated by
! 34736: ** this call as described above, then it is mapped into this processes
! 34737: ** address space (if it is not already), *pp is set to point to the mapped
! 34738: ** memory and SQLITE_OK returned.
! 34739: */
! 34740: static int winShmMap(
! 34741: sqlite3_file *fd, /* Handle open on database file */
! 34742: int iRegion, /* Region to retrieve */
! 34743: int szRegion, /* Size of regions */
! 34744: int isWrite, /* True to extend file if necessary */
! 34745: void volatile **pp /* OUT: Mapped memory */
! 34746: ){
! 34747: winFile *pDbFd = (winFile*)fd;
! 34748: winShm *p = pDbFd->pShm;
! 34749: winShmNode *pShmNode;
! 34750: int rc = SQLITE_OK;
! 34751:
! 34752: if( !p ){
! 34753: rc = winOpenSharedMemory(pDbFd);
! 34754: if( rc!=SQLITE_OK ) return rc;
! 34755: p = pDbFd->pShm;
! 34756: }
! 34757: pShmNode = p->pShmNode;
! 34758:
! 34759: sqlite3_mutex_enter(pShmNode->mutex);
! 34760: assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
! 34761:
! 34762: if( pShmNode->nRegion<=iRegion ){
! 34763: struct ShmRegion *apNew; /* New aRegion[] array */
! 34764: int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
! 34765: sqlite3_int64 sz; /* Current size of wal-index file */
! 34766:
! 34767: pShmNode->szRegion = szRegion;
! 34768:
! 34769: /* The requested region is not mapped into this processes address space.
! 34770: ** Check to see if it has been allocated (i.e. if the wal-index file is
! 34771: ** large enough to contain the requested region).
! 34772: */
! 34773: rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
! 34774: if( rc!=SQLITE_OK ){
! 34775: rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
! 34776: "winShmMap1", pDbFd->zPath);
! 34777: goto shmpage_out;
! 34778: }
! 34779:
! 34780: if( sz<nByte ){
! 34781: /* The requested memory region does not exist. If isWrite is set to
! 34782: ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
! 34783: **
! 34784: ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
! 34785: ** the requested memory region.
! 34786: */
! 34787: if( !isWrite ) goto shmpage_out;
! 34788: rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
! 34789: if( rc!=SQLITE_OK ){
! 34790: rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
! 34791: "winShmMap2", pDbFd->zPath);
! 34792: goto shmpage_out;
! 34793: }
! 34794: }
! 34795:
! 34796: /* Map the requested memory region into this processes address space. */
! 34797: apNew = (struct ShmRegion *)sqlite3_realloc(
! 34798: pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
! 34799: );
! 34800: if( !apNew ){
! 34801: rc = SQLITE_IOERR_NOMEM;
! 34802: goto shmpage_out;
! 34803: }
! 34804: pShmNode->aRegion = apNew;
! 34805:
! 34806: while( pShmNode->nRegion<=iRegion ){
! 34807: HANDLE hMap; /* file-mapping handle */
! 34808: void *pMap = 0; /* Mapped memory region */
! 34809:
! 34810: hMap = osCreateFileMapping(pShmNode->hFile.h,
! 34811: NULL, PAGE_READWRITE, 0, nByte, NULL
! 34812: );
! 34813: OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
! 34814: (int)osGetCurrentProcessId(), pShmNode->nRegion, nByte,
! 34815: hMap ? "ok" : "failed"));
! 34816: if( hMap ){
! 34817: int iOffset = pShmNode->nRegion*szRegion;
! 34818: int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
! 34819: pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
! 34820: 0, iOffset - iOffsetShift, szRegion + iOffsetShift
! 34821: );
! 34822: OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
! 34823: (int)osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
! 34824: szRegion, pMap ? "ok" : "failed"));
! 34825: }
! 34826: if( !pMap ){
! 34827: pShmNode->lastErrno = osGetLastError();
! 34828: rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
! 34829: "winShmMap3", pDbFd->zPath);
! 34830: if( hMap ) osCloseHandle(hMap);
! 34831: goto shmpage_out;
! 34832: }
! 34833:
! 34834: pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
! 34835: pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
! 34836: pShmNode->nRegion++;
! 34837: }
! 34838: }
! 34839:
! 34840: shmpage_out:
! 34841: if( pShmNode->nRegion>iRegion ){
! 34842: int iOffset = iRegion*szRegion;
! 34843: int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
! 34844: char *p = (char *)pShmNode->aRegion[iRegion].pMap;
! 34845: *pp = (void *)&p[iOffsetShift];
! 34846: }else{
! 34847: *pp = 0;
! 34848: }
! 34849: sqlite3_mutex_leave(pShmNode->mutex);
! 34850: return rc;
! 34851: }
! 34852:
! 34853: #else
! 34854: # define winShmMap 0
! 34855: # define winShmLock 0
! 34856: # define winShmBarrier 0
! 34857: # define winShmUnmap 0
! 34858: #endif /* #ifndef SQLITE_OMIT_WAL */
! 34859:
! 34860: /*
! 34861: ** Here ends the implementation of all sqlite3_file methods.
! 34862: **
! 34863: ********************** End sqlite3_file Methods *******************************
! 34864: ******************************************************************************/
! 34865:
! 34866: /*
! 34867: ** This vector defines all the methods that can operate on an
! 34868: ** sqlite3_file for win32.
! 34869: */
! 34870: static const sqlite3_io_methods winIoMethod = {
! 34871: 2, /* iVersion */
! 34872: winClose, /* xClose */
! 34873: winRead, /* xRead */
! 34874: winWrite, /* xWrite */
! 34875: winTruncate, /* xTruncate */
! 34876: winSync, /* xSync */
! 34877: winFileSize, /* xFileSize */
! 34878: winLock, /* xLock */
! 34879: winUnlock, /* xUnlock */
! 34880: winCheckReservedLock, /* xCheckReservedLock */
! 34881: winFileControl, /* xFileControl */
! 34882: winSectorSize, /* xSectorSize */
! 34883: winDeviceCharacteristics, /* xDeviceCharacteristics */
! 34884: winShmMap, /* xShmMap */
! 34885: winShmLock, /* xShmLock */
! 34886: winShmBarrier, /* xShmBarrier */
! 34887: winShmUnmap /* xShmUnmap */
! 34888: };
! 34889:
! 34890: /****************************************************************************
! 34891: **************************** sqlite3_vfs methods ****************************
! 34892: **
! 34893: ** This division contains the implementation of methods on the
! 34894: ** sqlite3_vfs object.
! 34895: */
! 34896:
! 34897: /*
! 34898: ** Convert a UTF-8 filename into whatever form the underlying
! 34899: ** operating system wants filenames in. Space to hold the result
! 34900: ** is obtained from malloc and must be freed by the calling
! 34901: ** function.
! 34902: */
! 34903: static void *convertUtf8Filename(const char *zFilename){
! 34904: void *zConverted = 0;
! 34905: if( isNT() ){
! 34906: zConverted = utf8ToUnicode(zFilename);
! 34907: /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
! 34908: */
! 34909: #if SQLITE_OS_WINCE==0
! 34910: }else{
! 34911: zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
! 34912: #endif
! 34913: }
! 34914: /* caller will handle out of memory */
! 34915: return zConverted;
! 34916: }
! 34917:
! 34918: /*
! 34919: ** Create a temporary file name in zBuf. zBuf must be big enough to
! 34920: ** hold at pVfs->mxPathname characters.
! 34921: */
! 34922: static int getTempname(int nBuf, char *zBuf){
! 34923: static char zChars[] =
! 34924: "abcdefghijklmnopqrstuvwxyz"
! 34925: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
! 34926: "0123456789";
! 34927: size_t i, j;
! 34928: char zTempPath[MAX_PATH+2];
! 34929:
! 34930: /* It's odd to simulate an io-error here, but really this is just
! 34931: ** using the io-error infrastructure to test that SQLite handles this
! 34932: ** function failing.
! 34933: */
! 34934: SimulateIOError( return SQLITE_IOERR );
! 34935:
! 34936: if( sqlite3_temp_directory ){
! 34937: sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
! 34938: }else if( isNT() ){
! 34939: char *zMulti;
! 34940: WCHAR zWidePath[MAX_PATH];
! 34941: osGetTempPathW(MAX_PATH-30, zWidePath);
! 34942: zMulti = unicodeToUtf8(zWidePath);
! 34943: if( zMulti ){
! 34944: sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
! 34945: sqlite3_free(zMulti);
! 34946: }else{
! 34947: return SQLITE_IOERR_NOMEM;
! 34948: }
! 34949: /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
! 34950: ** Since the ANSI version of these Windows API do not exist for WINCE,
! 34951: ** it's important to not reference them for WINCE builds.
! 34952: */
! 34953: #if SQLITE_OS_WINCE==0
! 34954: }else{
! 34955: char *zUtf8;
! 34956: char zMbcsPath[MAX_PATH];
! 34957: osGetTempPathA(MAX_PATH-30, zMbcsPath);
! 34958: zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
! 34959: if( zUtf8 ){
! 34960: sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
! 34961: sqlite3_free(zUtf8);
! 34962: }else{
! 34963: return SQLITE_IOERR_NOMEM;
! 34964: }
! 34965: #endif
! 34966: }
! 34967:
! 34968: /* Check that the output buffer is large enough for the temporary file
! 34969: ** name. If it is not, return SQLITE_ERROR.
! 34970: */
! 34971: if( (sqlite3Strlen30(zTempPath) + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
! 34972: return SQLITE_ERROR;
! 34973: }
! 34974:
! 34975: for(i=sqlite3Strlen30(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
! 34976: zTempPath[i] = 0;
! 34977:
! 34978: sqlite3_snprintf(nBuf-18, zBuf,
! 34979: "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
! 34980: j = sqlite3Strlen30(zBuf);
! 34981: sqlite3_randomness(15, &zBuf[j]);
! 34982: for(i=0; i<15; i++, j++){
! 34983: zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
! 34984: }
! 34985: zBuf[j] = 0;
! 34986: zBuf[j+1] = 0;
! 34987:
! 34988: OSTRACE(("TEMP FILENAME: %s\n", zBuf));
! 34989: return SQLITE_OK;
! 34990: }
! 34991:
! 34992: /*
! 34993: ** Open a file.
! 34994: */
! 34995: static int winOpen(
! 34996: sqlite3_vfs *pVfs, /* Not used */
! 34997: const char *zName, /* Name of the file (UTF-8) */
! 34998: sqlite3_file *id, /* Write the SQLite file handle here */
! 34999: int flags, /* Open mode flags */
! 35000: int *pOutFlags /* Status return flags */
! 35001: ){
! 35002: HANDLE h;
! 35003: DWORD lastErrno;
! 35004: DWORD dwDesiredAccess;
! 35005: DWORD dwShareMode;
! 35006: DWORD dwCreationDisposition;
! 35007: DWORD dwFlagsAndAttributes = 0;
! 35008: #if SQLITE_OS_WINCE
! 35009: int isTemp = 0;
! 35010: #endif
! 35011: winFile *pFile = (winFile*)id;
! 35012: void *zConverted; /* Filename in OS encoding */
! 35013: const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
! 35014: int cnt = 0;
! 35015:
! 35016: /* If argument zPath is a NULL pointer, this function is required to open
! 35017: ** a temporary file. Use this buffer to store the file name in.
! 35018: */
! 35019: char zTmpname[MAX_PATH+2]; /* Buffer used to create temp filename */
! 35020:
! 35021: int rc = SQLITE_OK; /* Function Return Code */
! 35022: #if !defined(NDEBUG) || SQLITE_OS_WINCE
! 35023: int eType = flags&0xFFFFFF00; /* Type of file to open */
! 35024: #endif
! 35025:
! 35026: int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
! 35027: int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
! 35028: int isCreate = (flags & SQLITE_OPEN_CREATE);
! 35029: #ifndef NDEBUG
! 35030: int isReadonly = (flags & SQLITE_OPEN_READONLY);
! 35031: #endif
! 35032: int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
! 35033:
! 35034: #ifndef NDEBUG
! 35035: int isOpenJournal = (isCreate && (
! 35036: eType==SQLITE_OPEN_MASTER_JOURNAL
! 35037: || eType==SQLITE_OPEN_MAIN_JOURNAL
! 35038: || eType==SQLITE_OPEN_WAL
! 35039: ));
! 35040: #endif
! 35041:
! 35042: /* Check the following statements are true:
! 35043: **
! 35044: ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
! 35045: ** (b) if CREATE is set, then READWRITE must also be set, and
! 35046: ** (c) if EXCLUSIVE is set, then CREATE must also be set.
! 35047: ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
! 35048: */
! 35049: assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
! 35050: assert(isCreate==0 || isReadWrite);
! 35051: assert(isExclusive==0 || isCreate);
! 35052: assert(isDelete==0 || isCreate);
! 35053:
! 35054: /* The main DB, main journal, WAL file and master journal are never
! 35055: ** automatically deleted. Nor are they ever temporary files. */
! 35056: assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
! 35057: assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
! 35058: assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
! 35059: assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
! 35060:
! 35061: /* Assert that the upper layer has set one of the "file-type" flags. */
! 35062: assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
! 35063: || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
! 35064: || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
! 35065: || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
! 35066: );
! 35067:
! 35068: assert( id!=0 );
! 35069: UNUSED_PARAMETER(pVfs);
! 35070:
! 35071: pFile->h = INVALID_HANDLE_VALUE;
! 35072:
! 35073: /* If the second argument to this function is NULL, generate a
! 35074: ** temporary file name to use
! 35075: */
! 35076: if( !zUtf8Name ){
! 35077: assert(isDelete && !isOpenJournal);
! 35078: rc = getTempname(MAX_PATH+2, zTmpname);
! 35079: if( rc!=SQLITE_OK ){
! 35080: return rc;
! 35081: }
! 35082: zUtf8Name = zTmpname;
! 35083: }
! 35084:
! 35085: /* Database filenames are double-zero terminated if they are not
! 35086: ** URIs with parameters. Hence, they can always be passed into
! 35087: ** sqlite3_uri_parameter().
! 35088: */
! 35089: assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
! 35090: zUtf8Name[strlen(zUtf8Name)+1]==0 );
! 35091:
! 35092: /* Convert the filename to the system encoding. */
! 35093: zConverted = convertUtf8Filename(zUtf8Name);
! 35094: if( zConverted==0 ){
! 35095: return SQLITE_IOERR_NOMEM;
! 35096: }
! 35097:
! 35098: if( isReadWrite ){
! 35099: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
! 35100: }else{
! 35101: dwDesiredAccess = GENERIC_READ;
! 35102: }
! 35103:
! 35104: /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
! 35105: ** created. SQLite doesn't use it to indicate "exclusive access"
! 35106: ** as it is usually understood.
! 35107: */
! 35108: if( isExclusive ){
! 35109: /* Creates a new file, only if it does not already exist. */
! 35110: /* If the file exists, it fails. */
! 35111: dwCreationDisposition = CREATE_NEW;
! 35112: }else if( isCreate ){
! 35113: /* Open existing file, or create if it doesn't exist */
! 35114: dwCreationDisposition = OPEN_ALWAYS;
! 35115: }else{
! 35116: /* Opens a file, only if it exists. */
! 35117: dwCreationDisposition = OPEN_EXISTING;
! 35118: }
! 35119:
! 35120: dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
! 35121:
! 35122: if( isDelete ){
! 35123: #if SQLITE_OS_WINCE
! 35124: dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
! 35125: isTemp = 1;
! 35126: #else
! 35127: dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
! 35128: | FILE_ATTRIBUTE_HIDDEN
! 35129: | FILE_FLAG_DELETE_ON_CLOSE;
! 35130: #endif
! 35131: }else{
! 35132: dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
! 35133: }
! 35134: /* Reports from the internet are that performance is always
! 35135: ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
! 35136: #if SQLITE_OS_WINCE
! 35137: dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
! 35138: #endif
! 35139:
! 35140: if( isNT() ){
! 35141: while( (h = osCreateFileW((LPCWSTR)zConverted,
! 35142: dwDesiredAccess,
! 35143: dwShareMode, NULL,
! 35144: dwCreationDisposition,
! 35145: dwFlagsAndAttributes,
! 35146: NULL))==INVALID_HANDLE_VALUE &&
! 35147: retryIoerr(&cnt, &lastErrno) ){}
! 35148: /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
! 35149: ** Since the ANSI version of these Windows API do not exist for WINCE,
! 35150: ** it's important to not reference them for WINCE builds.
! 35151: */
! 35152: #if SQLITE_OS_WINCE==0
! 35153: }else{
! 35154: while( (h = osCreateFileA((LPCSTR)zConverted,
! 35155: dwDesiredAccess,
! 35156: dwShareMode, NULL,
! 35157: dwCreationDisposition,
! 35158: dwFlagsAndAttributes,
! 35159: NULL))==INVALID_HANDLE_VALUE &&
! 35160: retryIoerr(&cnt, &lastErrno) ){}
! 35161: #endif
! 35162: }
! 35163:
! 35164: logIoerr(cnt);
! 35165:
! 35166: OSTRACE(("OPEN %d %s 0x%lx %s\n",
! 35167: h, zName, dwDesiredAccess,
! 35168: h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
! 35169:
! 35170: if( h==INVALID_HANDLE_VALUE ){
! 35171: pFile->lastErrno = lastErrno;
! 35172: winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
! 35173: sqlite3_free(zConverted);
! 35174: if( isReadWrite && !isExclusive ){
! 35175: return winOpen(pVfs, zName, id,
! 35176: ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
! 35177: }else{
! 35178: return SQLITE_CANTOPEN_BKPT;
! 35179: }
! 35180: }
! 35181:
! 35182: if( pOutFlags ){
! 35183: if( isReadWrite ){
! 35184: *pOutFlags = SQLITE_OPEN_READWRITE;
! 35185: }else{
! 35186: *pOutFlags = SQLITE_OPEN_READONLY;
! 35187: }
! 35188: }
! 35189:
! 35190: memset(pFile, 0, sizeof(*pFile));
! 35191: pFile->pMethod = &winIoMethod;
! 35192: pFile->h = h;
! 35193: pFile->lastErrno = NO_ERROR;
! 35194: pFile->pVfs = pVfs;
! 35195: pFile->pShm = 0;
! 35196: pFile->zPath = zName;
! 35197: if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
! 35198: pFile->ctrlFlags |= WINFILE_PSOW;
! 35199: }
! 35200:
! 35201: #if SQLITE_OS_WINCE
! 35202: if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
! 35203: && !winceCreateLock(zName, pFile)
! 35204: ){
! 35205: osCloseHandle(h);
! 35206: sqlite3_free(zConverted);
! 35207: return SQLITE_CANTOPEN_BKPT;
! 35208: }
! 35209: if( isTemp ){
! 35210: pFile->zDeleteOnClose = zConverted;
! 35211: }else
! 35212: #endif
! 35213: {
! 35214: sqlite3_free(zConverted);
! 35215: }
! 35216:
! 35217: OpenCounter(+1);
! 35218: return rc;
! 35219: }
! 35220:
! 35221: /*
! 35222: ** Delete the named file.
! 35223: **
! 35224: ** Note that Windows does not allow a file to be deleted if some other
! 35225: ** process has it open. Sometimes a virus scanner or indexing program
! 35226: ** will open a journal file shortly after it is created in order to do
! 35227: ** whatever it does. While this other process is holding the
! 35228: ** file open, we will be unable to delete it. To work around this
! 35229: ** problem, we delay 100 milliseconds and try to delete again. Up
! 35230: ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
! 35231: ** up and returning an error.
! 35232: */
! 35233: static int winDelete(
! 35234: sqlite3_vfs *pVfs, /* Not used on win32 */
! 35235: const char *zFilename, /* Name of file to delete */
! 35236: int syncDir /* Not used on win32 */
! 35237: ){
! 35238: int cnt = 0;
! 35239: int rc;
! 35240: DWORD lastErrno;
! 35241: void *zConverted;
! 35242: UNUSED_PARAMETER(pVfs);
! 35243: UNUSED_PARAMETER(syncDir);
! 35244:
! 35245: SimulateIOError(return SQLITE_IOERR_DELETE);
! 35246: zConverted = convertUtf8Filename(zFilename);
! 35247: if( zConverted==0 ){
! 35248: return SQLITE_IOERR_NOMEM;
! 35249: }
! 35250: if( isNT() ){
! 35251: rc = 1;
! 35252: while( osGetFileAttributesW(zConverted)!=INVALID_FILE_ATTRIBUTES &&
! 35253: (rc = osDeleteFileW(zConverted))==0 && retryIoerr(&cnt, &lastErrno) ){}
! 35254: rc = rc ? SQLITE_OK : SQLITE_ERROR;
! 35255: /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
! 35256: ** Since the ANSI version of these Windows API do not exist for WINCE,
! 35257: ** it's important to not reference them for WINCE builds.
! 35258: */
! 35259: #if SQLITE_OS_WINCE==0
! 35260: }else{
! 35261: rc = 1;
! 35262: while( osGetFileAttributesA(zConverted)!=INVALID_FILE_ATTRIBUTES &&
! 35263: (rc = osDeleteFileA(zConverted))==0 && retryIoerr(&cnt, &lastErrno) ){}
! 35264: rc = rc ? SQLITE_OK : SQLITE_ERROR;
! 35265: #endif
! 35266: }
! 35267: if( rc ){
! 35268: rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
! 35269: "winDelete", zFilename);
! 35270: }else{
! 35271: logIoerr(cnt);
! 35272: }
! 35273: sqlite3_free(zConverted);
! 35274: OSTRACE(("DELETE \"%s\" %s\n", zFilename, (rc ? "failed" : "ok" )));
! 35275: return rc;
! 35276: }
! 35277:
! 35278: /*
! 35279: ** Check the existance and status of a file.
! 35280: */
! 35281: static int winAccess(
! 35282: sqlite3_vfs *pVfs, /* Not used on win32 */
! 35283: const char *zFilename, /* Name of file to check */
! 35284: int flags, /* Type of test to make on this file */
! 35285: int *pResOut /* OUT: Result */
! 35286: ){
! 35287: DWORD attr;
! 35288: int rc = 0;
! 35289: DWORD lastErrno;
! 35290: void *zConverted;
! 35291: UNUSED_PARAMETER(pVfs);
! 35292:
! 35293: SimulateIOError( return SQLITE_IOERR_ACCESS; );
! 35294: zConverted = convertUtf8Filename(zFilename);
! 35295: if( zConverted==0 ){
! 35296: return SQLITE_IOERR_NOMEM;
! 35297: }
! 35298: if( isNT() ){
! 35299: int cnt = 0;
! 35300: WIN32_FILE_ATTRIBUTE_DATA sAttrData;
! 35301: memset(&sAttrData, 0, sizeof(sAttrData));
! 35302: while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
! 35303: GetFileExInfoStandard,
! 35304: &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
! 35305: if( rc ){
! 35306: /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
! 35307: ** as if it does not exist.
! 35308: */
! 35309: if( flags==SQLITE_ACCESS_EXISTS
! 35310: && sAttrData.nFileSizeHigh==0
! 35311: && sAttrData.nFileSizeLow==0 ){
! 35312: attr = INVALID_FILE_ATTRIBUTES;
! 35313: }else{
! 35314: attr = sAttrData.dwFileAttributes;
! 35315: }
! 35316: }else{
! 35317: logIoerr(cnt);
! 35318: if( lastErrno!=ERROR_FILE_NOT_FOUND ){
! 35319: winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
! 35320: sqlite3_free(zConverted);
! 35321: return SQLITE_IOERR_ACCESS;
! 35322: }else{
! 35323: attr = INVALID_FILE_ATTRIBUTES;
! 35324: }
! 35325: }
! 35326: /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
! 35327: ** Since the ANSI version of these Windows API do not exist for WINCE,
! 35328: ** it's important to not reference them for WINCE builds.
! 35329: */
! 35330: #if SQLITE_OS_WINCE==0
! 35331: }else{
! 35332: attr = osGetFileAttributesA((char*)zConverted);
! 35333: #endif
! 35334: }
! 35335: sqlite3_free(zConverted);
! 35336: switch( flags ){
! 35337: case SQLITE_ACCESS_READ:
! 35338: case SQLITE_ACCESS_EXISTS:
! 35339: rc = attr!=INVALID_FILE_ATTRIBUTES;
! 35340: break;
! 35341: case SQLITE_ACCESS_READWRITE:
! 35342: rc = attr!=INVALID_FILE_ATTRIBUTES &&
! 35343: (attr & FILE_ATTRIBUTE_READONLY)==0;
! 35344: break;
! 35345: default:
! 35346: assert(!"Invalid flags argument");
! 35347: }
! 35348: *pResOut = rc;
! 35349: return SQLITE_OK;
! 35350: }
! 35351:
! 35352:
! 35353: /*
! 35354: ** Turn a relative pathname into a full pathname. Write the full
! 35355: ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
! 35356: ** bytes in size.
! 35357: */
! 35358: static int winFullPathname(
! 35359: sqlite3_vfs *pVfs, /* Pointer to vfs object */
! 35360: const char *zRelative, /* Possibly relative input path */
! 35361: int nFull, /* Size of output buffer in bytes */
! 35362: char *zFull /* Output buffer */
! 35363: ){
! 35364:
! 35365: #if defined(__CYGWIN__)
! 35366: SimulateIOError( return SQLITE_ERROR );
! 35367: UNUSED_PARAMETER(nFull);
! 35368: cygwin_conv_to_full_win32_path(zRelative, zFull);
! 35369: return SQLITE_OK;
! 35370: #endif
! 35371:
! 35372: #if SQLITE_OS_WINCE
! 35373: SimulateIOError( return SQLITE_ERROR );
! 35374: UNUSED_PARAMETER(nFull);
! 35375: /* WinCE has no concept of a relative pathname, or so I am told. */
! 35376: sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative);
! 35377: return SQLITE_OK;
! 35378: #endif
! 35379:
! 35380: #if !SQLITE_OS_WINCE && !defined(__CYGWIN__)
! 35381: int nByte;
! 35382: void *zConverted;
! 35383: char *zOut;
! 35384:
! 35385: /* If this path name begins with "/X:", where "X" is any alphabetic
! 35386: ** character, discard the initial "/" from the pathname.
! 35387: */
! 35388: if( zRelative[0]=='/' && sqlite3Isalpha(zRelative[1]) && zRelative[2]==':' ){
! 35389: zRelative++;
! 35390: }
! 35391:
! 35392: /* It's odd to simulate an io-error here, but really this is just
! 35393: ** using the io-error infrastructure to test that SQLite handles this
! 35394: ** function failing. This function could fail if, for example, the
! 35395: ** current working directory has been unlinked.
! 35396: */
! 35397: SimulateIOError( return SQLITE_ERROR );
! 35398: UNUSED_PARAMETER(nFull);
! 35399: zConverted = convertUtf8Filename(zRelative);
! 35400: if( zConverted==0 ){
! 35401: return SQLITE_IOERR_NOMEM;
! 35402: }
! 35403: if( isNT() ){
! 35404: LPWSTR zTemp;
! 35405: nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0) + 3;
! 35406: zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
! 35407: if( zTemp==0 ){
! 35408: sqlite3_free(zConverted);
! 35409: return SQLITE_IOERR_NOMEM;
! 35410: }
! 35411: osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
! 35412: sqlite3_free(zConverted);
! 35413: zOut = unicodeToUtf8(zTemp);
! 35414: sqlite3_free(zTemp);
! 35415: /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
! 35416: ** Since the ANSI version of these Windows API do not exist for WINCE,
! 35417: ** it's important to not reference them for WINCE builds.
! 35418: */
! 35419: #if SQLITE_OS_WINCE==0
! 35420: }else{
! 35421: char *zTemp;
! 35422: nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
! 35423: zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
! 35424: if( zTemp==0 ){
! 35425: sqlite3_free(zConverted);
! 35426: return SQLITE_IOERR_NOMEM;
! 35427: }
! 35428: osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
! 35429: sqlite3_free(zConverted);
! 35430: zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
! 35431: sqlite3_free(zTemp);
! 35432: #endif
! 35433: }
! 35434: if( zOut ){
! 35435: sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
! 35436: sqlite3_free(zOut);
! 35437: return SQLITE_OK;
! 35438: }else{
! 35439: return SQLITE_IOERR_NOMEM;
! 35440: }
! 35441: #endif
! 35442: }
! 35443:
! 35444: #ifndef SQLITE_OMIT_LOAD_EXTENSION
! 35445: /*
! 35446: ** Interfaces for opening a shared library, finding entry points
! 35447: ** within the shared library, and closing the shared library.
! 35448: */
! 35449: /*
! 35450: ** Interfaces for opening a shared library, finding entry points
! 35451: ** within the shared library, and closing the shared library.
! 35452: */
! 35453: static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
! 35454: HANDLE h;
! 35455: void *zConverted = convertUtf8Filename(zFilename);
! 35456: UNUSED_PARAMETER(pVfs);
! 35457: if( zConverted==0 ){
! 35458: return 0;
! 35459: }
! 35460: if( isNT() ){
! 35461: h = osLoadLibraryW((LPCWSTR)zConverted);
! 35462: /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
! 35463: ** Since the ANSI version of these Windows API do not exist for WINCE,
! 35464: ** it's important to not reference them for WINCE builds.
! 35465: */
! 35466: #if SQLITE_OS_WINCE==0
! 35467: }else{
! 35468: h = osLoadLibraryA((char*)zConverted);
! 35469: #endif
! 35470: }
! 35471: sqlite3_free(zConverted);
! 35472: return (void*)h;
! 35473: }
! 35474: static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
! 35475: UNUSED_PARAMETER(pVfs);
! 35476: getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
! 35477: }
! 35478: static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
! 35479: UNUSED_PARAMETER(pVfs);
! 35480: return (void(*)(void))osGetProcAddressA((HANDLE)pHandle, zSymbol);
! 35481: }
! 35482: static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
! 35483: UNUSED_PARAMETER(pVfs);
! 35484: osFreeLibrary((HANDLE)pHandle);
! 35485: }
! 35486: #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
! 35487: #define winDlOpen 0
! 35488: #define winDlError 0
! 35489: #define winDlSym 0
! 35490: #define winDlClose 0
! 35491: #endif
! 35492:
! 35493:
! 35494: /*
! 35495: ** Write up to nBuf bytes of randomness into zBuf.
! 35496: */
! 35497: static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
! 35498: int n = 0;
! 35499: UNUSED_PARAMETER(pVfs);
! 35500: #if defined(SQLITE_TEST)
! 35501: n = nBuf;
! 35502: memset(zBuf, 0, nBuf);
! 35503: #else
! 35504: if( sizeof(SYSTEMTIME)<=nBuf-n ){
! 35505: SYSTEMTIME x;
! 35506: osGetSystemTime(&x);
! 35507: memcpy(&zBuf[n], &x, sizeof(x));
! 35508: n += sizeof(x);
! 35509: }
! 35510: if( sizeof(DWORD)<=nBuf-n ){
! 35511: DWORD pid = osGetCurrentProcessId();
! 35512: memcpy(&zBuf[n], &pid, sizeof(pid));
! 35513: n += sizeof(pid);
! 35514: }
! 35515: if( sizeof(DWORD)<=nBuf-n ){
! 35516: DWORD cnt = osGetTickCount();
! 35517: memcpy(&zBuf[n], &cnt, sizeof(cnt));
! 35518: n += sizeof(cnt);
! 35519: }
! 35520: if( sizeof(LARGE_INTEGER)<=nBuf-n ){
! 35521: LARGE_INTEGER i;
! 35522: osQueryPerformanceCounter(&i);
! 35523: memcpy(&zBuf[n], &i, sizeof(i));
! 35524: n += sizeof(i);
! 35525: }
! 35526: #endif
! 35527: return n;
! 35528: }
! 35529:
! 35530:
! 35531: /*
! 35532: ** Sleep for a little while. Return the amount of time slept.
! 35533: */
! 35534: static int winSleep(sqlite3_vfs *pVfs, int microsec){
! 35535: osSleep((microsec+999)/1000);
! 35536: UNUSED_PARAMETER(pVfs);
! 35537: return ((microsec+999)/1000)*1000;
! 35538: }
! 35539:
! 35540: /*
! 35541: ** The following variable, if set to a non-zero value, is interpreted as
! 35542: ** the number of seconds since 1970 and is used to set the result of
! 35543: ** sqlite3OsCurrentTime() during testing.
! 35544: */
! 35545: #ifdef SQLITE_TEST
! 35546: SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
! 35547: #endif
! 35548:
! 35549: /*
! 35550: ** Find the current time (in Universal Coordinated Time). Write into *piNow
! 35551: ** the current time and date as a Julian Day number times 86_400_000. In
! 35552: ** other words, write into *piNow the number of milliseconds since the Julian
! 35553: ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
! 35554: ** proleptic Gregorian calendar.
! 35555: **
! 35556: ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
! 35557: ** cannot be found.
! 35558: */
! 35559: static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
! 35560: /* FILETIME structure is a 64-bit value representing the number of
! 35561: 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
! 35562: */
! 35563: FILETIME ft;
! 35564: static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
! 35565: #ifdef SQLITE_TEST
! 35566: static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
! 35567: #endif
! 35568: /* 2^32 - to avoid use of LL and warnings in gcc */
! 35569: static const sqlite3_int64 max32BitValue =
! 35570: (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
! 35571:
! 35572: #if SQLITE_OS_WINCE
! 35573: SYSTEMTIME time;
! 35574: osGetSystemTime(&time);
! 35575: /* if SystemTimeToFileTime() fails, it returns zero. */
! 35576: if (!osSystemTimeToFileTime(&time,&ft)){
! 35577: return SQLITE_ERROR;
! 35578: }
! 35579: #else
! 35580: osGetSystemTimeAsFileTime( &ft );
! 35581: #endif
! 35582:
! 35583: *piNow = winFiletimeEpoch +
! 35584: ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
! 35585: (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
! 35586:
! 35587: #ifdef SQLITE_TEST
! 35588: if( sqlite3_current_time ){
! 35589: *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
! 35590: }
! 35591: #endif
! 35592: UNUSED_PARAMETER(pVfs);
! 35593: return SQLITE_OK;
! 35594: }
! 35595:
! 35596: /*
! 35597: ** Find the current time (in Universal Coordinated Time). Write the
! 35598: ** current time and date as a Julian Day number into *prNow and
! 35599: ** return 0. Return 1 if the time and date cannot be found.
! 35600: */
! 35601: static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
! 35602: int rc;
! 35603: sqlite3_int64 i;
! 35604: rc = winCurrentTimeInt64(pVfs, &i);
! 35605: if( !rc ){
! 35606: *prNow = i/86400000.0;
! 35607: }
! 35608: return rc;
! 35609: }
! 35610:
! 35611: /*
! 35612: ** The idea is that this function works like a combination of
! 35613: ** GetLastError() and FormatMessage() on Windows (or errno and
! 35614: ** strerror_r() on Unix). After an error is returned by an OS
! 35615: ** function, SQLite calls this function with zBuf pointing to
! 35616: ** a buffer of nBuf bytes. The OS layer should populate the
! 35617: ** buffer with a nul-terminated UTF-8 encoded error message
! 35618: ** describing the last IO error to have occurred within the calling
! 35619: ** thread.
! 35620: **
! 35621: ** If the error message is too large for the supplied buffer,
! 35622: ** it should be truncated. The return value of xGetLastError
! 35623: ** is zero if the error message fits in the buffer, or non-zero
! 35624: ** otherwise (if the message was truncated). If non-zero is returned,
! 35625: ** then it is not necessary to include the nul-terminator character
! 35626: ** in the output buffer.
! 35627: **
! 35628: ** Not supplying an error message will have no adverse effect
! 35629: ** on SQLite. It is fine to have an implementation that never
! 35630: ** returns an error message:
! 35631: **
! 35632: ** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
! 35633: ** assert(zBuf[0]=='\0');
! 35634: ** return 0;
! 35635: ** }
! 35636: **
! 35637: ** However if an error message is supplied, it will be incorporated
! 35638: ** by sqlite into the error message available to the user using
! 35639: ** sqlite3_errmsg(), possibly making IO errors easier to debug.
! 35640: */
! 35641: static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
! 35642: UNUSED_PARAMETER(pVfs);
! 35643: return getLastErrorMsg(osGetLastError(), nBuf, zBuf);
! 35644: }
! 35645:
! 35646: /*
! 35647: ** Initialize and deinitialize the operating system interface.
! 35648: */
! 35649: SQLITE_API int sqlite3_os_init(void){
! 35650: static sqlite3_vfs winVfs = {
! 35651: 3, /* iVersion */
! 35652: sizeof(winFile), /* szOsFile */
! 35653: MAX_PATH, /* mxPathname */
! 35654: 0, /* pNext */
! 35655: "win32", /* zName */
! 35656: 0, /* pAppData */
! 35657: winOpen, /* xOpen */
! 35658: winDelete, /* xDelete */
! 35659: winAccess, /* xAccess */
! 35660: winFullPathname, /* xFullPathname */
! 35661: winDlOpen, /* xDlOpen */
! 35662: winDlError, /* xDlError */
! 35663: winDlSym, /* xDlSym */
! 35664: winDlClose, /* xDlClose */
! 35665: winRandomness, /* xRandomness */
! 35666: winSleep, /* xSleep */
! 35667: winCurrentTime, /* xCurrentTime */
! 35668: winGetLastError, /* xGetLastError */
! 35669: winCurrentTimeInt64, /* xCurrentTimeInt64 */
! 35670: winSetSystemCall, /* xSetSystemCall */
! 35671: winGetSystemCall, /* xGetSystemCall */
! 35672: winNextSystemCall, /* xNextSystemCall */
! 35673: };
! 35674:
! 35675: /* Double-check that the aSyscall[] array has been constructed
! 35676: ** correctly. See ticket [bb3a86e890c8e96ab] */
! 35677: assert( ArraySize(aSyscall)==60 );
! 35678:
! 35679: #ifndef SQLITE_OMIT_WAL
! 35680: /* get memory map allocation granularity */
! 35681: memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
! 35682: osGetSystemInfo(&winSysInfo);
! 35683: assert(winSysInfo.dwAllocationGranularity > 0);
! 35684: #endif
! 35685:
! 35686: sqlite3_vfs_register(&winVfs, 1);
! 35687: return SQLITE_OK;
! 35688: }
! 35689:
! 35690: SQLITE_API int sqlite3_os_end(void){
! 35691: return SQLITE_OK;
! 35692: }
! 35693:
! 35694: #endif /* SQLITE_OS_WIN */
! 35695:
! 35696: /************** End of os_win.c **********************************************/
! 35697: /************** Begin file bitvec.c ******************************************/
! 35698: /*
! 35699: ** 2008 February 16
! 35700: **
! 35701: ** The author disclaims copyright to this source code. In place of
! 35702: ** a legal notice, here is a blessing:
! 35703: **
! 35704: ** May you do good and not evil.
! 35705: ** May you find forgiveness for yourself and forgive others.
! 35706: ** May you share freely, never taking more than you give.
! 35707: **
! 35708: *************************************************************************
! 35709: ** This file implements an object that represents a fixed-length
! 35710: ** bitmap. Bits are numbered starting with 1.
! 35711: **
! 35712: ** A bitmap is used to record which pages of a database file have been
! 35713: ** journalled during a transaction, or which pages have the "dont-write"
! 35714: ** property. Usually only a few pages are meet either condition.
! 35715: ** So the bitmap is usually sparse and has low cardinality.
! 35716: ** But sometimes (for example when during a DROP of a large table) most
! 35717: ** or all of the pages in a database can get journalled. In those cases,
! 35718: ** the bitmap becomes dense with high cardinality. The algorithm needs
! 35719: ** to handle both cases well.
! 35720: **
! 35721: ** The size of the bitmap is fixed when the object is created.
! 35722: **
! 35723: ** All bits are clear when the bitmap is created. Individual bits
! 35724: ** may be set or cleared one at a time.
! 35725: **
! 35726: ** Test operations are about 100 times more common that set operations.
! 35727: ** Clear operations are exceedingly rare. There are usually between
! 35728: ** 5 and 500 set operations per Bitvec object, though the number of sets can
! 35729: ** sometimes grow into tens of thousands or larger. The size of the
! 35730: ** Bitvec object is the number of pages in the database file at the
! 35731: ** start of a transaction, and is thus usually less than a few thousand,
! 35732: ** but can be as large as 2 billion for a really big database.
! 35733: */
! 35734:
! 35735: /* Size of the Bitvec structure in bytes. */
! 35736: #define BITVEC_SZ 512
! 35737:
! 35738: /* Round the union size down to the nearest pointer boundary, since that's how
! 35739: ** it will be aligned within the Bitvec struct. */
! 35740: #define BITVEC_USIZE (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
! 35741:
! 35742: /* Type of the array "element" for the bitmap representation.
! 35743: ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
! 35744: ** Setting this to the "natural word" size of your CPU may improve
! 35745: ** performance. */
! 35746: #define BITVEC_TELEM u8
! 35747: /* Size, in bits, of the bitmap element. */
! 35748: #define BITVEC_SZELEM 8
! 35749: /* Number of elements in a bitmap array. */
! 35750: #define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
! 35751: /* Number of bits in the bitmap array. */
! 35752: #define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
! 35753:
! 35754: /* Number of u32 values in hash table. */
! 35755: #define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
! 35756: /* Maximum number of entries in hash table before
! 35757: ** sub-dividing and re-hashing. */
! 35758: #define BITVEC_MXHASH (BITVEC_NINT/2)
! 35759: /* Hashing function for the aHash representation.
! 35760: ** Empirical testing showed that the *37 multiplier
! 35761: ** (an arbitrary prime)in the hash function provided
! 35762: ** no fewer collisions than the no-op *1. */
! 35763: #define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
! 35764:
! 35765: #define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
! 35766:
! 35767:
! 35768: /*
! 35769: ** A bitmap is an instance of the following structure.
! 35770: **
! 35771: ** This bitmap records the existance of zero or more bits
! 35772: ** with values between 1 and iSize, inclusive.
! 35773: **
! 35774: ** There are three possible representations of the bitmap.
! 35775: ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
! 35776: ** bitmap. The least significant bit is bit 1.
! 35777: **
! 35778: ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
! 35779: ** a hash table that will hold up to BITVEC_MXHASH distinct values.
! 35780: **
! 35781: ** Otherwise, the value i is redirected into one of BITVEC_NPTR
! 35782: ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
! 35783: ** handles up to iDivisor separate values of i. apSub[0] holds
! 35784: ** values between 1 and iDivisor. apSub[1] holds values between
! 35785: ** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
! 35786: ** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
! 35787: ** to hold deal with values between 1 and iDivisor.
! 35788: */
! 35789: struct Bitvec {
! 35790: u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
! 35791: u32 nSet; /* Number of bits that are set - only valid for aHash
! 35792: ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
! 35793: ** this would be 125. */
! 35794: u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
! 35795: /* Should >=0 for apSub element. */
! 35796: /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
! 35797: /* For a BITVEC_SZ of 512, this would be 34,359,739. */
! 35798: union {
! 35799: BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
! 35800: u32 aHash[BITVEC_NINT]; /* Hash table representation */
! 35801: Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
! 35802: } u;
! 35803: };
! 35804:
! 35805: /*
! 35806: ** Create a new bitmap object able to handle bits between 0 and iSize,
! 35807: ** inclusive. Return a pointer to the new object. Return NULL if
! 35808: ** malloc fails.
! 35809: */
! 35810: SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
! 35811: Bitvec *p;
! 35812: assert( sizeof(*p)==BITVEC_SZ );
! 35813: p = sqlite3MallocZero( sizeof(*p) );
! 35814: if( p ){
! 35815: p->iSize = iSize;
! 35816: }
! 35817: return p;
! 35818: }
! 35819:
! 35820: /*
! 35821: ** Check to see if the i-th bit is set. Return true or false.
! 35822: ** If p is NULL (if the bitmap has not been created) or if
! 35823: ** i is out of range, then return false.
! 35824: */
! 35825: SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
! 35826: if( p==0 ) return 0;
! 35827: if( i>p->iSize || i==0 ) return 0;
! 35828: i--;
! 35829: while( p->iDivisor ){
! 35830: u32 bin = i/p->iDivisor;
! 35831: i = i%p->iDivisor;
! 35832: p = p->u.apSub[bin];
! 35833: if (!p) {
! 35834: return 0;
! 35835: }
! 35836: }
! 35837: if( p->iSize<=BITVEC_NBIT ){
! 35838: return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
! 35839: } else{
! 35840: u32 h = BITVEC_HASH(i++);
! 35841: while( p->u.aHash[h] ){
! 35842: if( p->u.aHash[h]==i ) return 1;
! 35843: h = (h+1) % BITVEC_NINT;
! 35844: }
! 35845: return 0;
! 35846: }
! 35847: }
! 35848:
! 35849: /*
! 35850: ** Set the i-th bit. Return 0 on success and an error code if
! 35851: ** anything goes wrong.
! 35852: **
! 35853: ** This routine might cause sub-bitmaps to be allocated. Failing
! 35854: ** to get the memory needed to hold the sub-bitmap is the only
! 35855: ** that can go wrong with an insert, assuming p and i are valid.
! 35856: **
! 35857: ** The calling function must ensure that p is a valid Bitvec object
! 35858: ** and that the value for "i" is within range of the Bitvec object.
! 35859: ** Otherwise the behavior is undefined.
! 35860: */
! 35861: SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
! 35862: u32 h;
! 35863: if( p==0 ) return SQLITE_OK;
! 35864: assert( i>0 );
! 35865: assert( i<=p->iSize );
! 35866: i--;
! 35867: while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
! 35868: u32 bin = i/p->iDivisor;
! 35869: i = i%p->iDivisor;
! 35870: if( p->u.apSub[bin]==0 ){
! 35871: p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
! 35872: if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
! 35873: }
! 35874: p = p->u.apSub[bin];
! 35875: }
! 35876: if( p->iSize<=BITVEC_NBIT ){
! 35877: p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
! 35878: return SQLITE_OK;
! 35879: }
! 35880: h = BITVEC_HASH(i++);
! 35881: /* if there wasn't a hash collision, and this doesn't */
! 35882: /* completely fill the hash, then just add it without */
! 35883: /* worring about sub-dividing and re-hashing. */
! 35884: if( !p->u.aHash[h] ){
! 35885: if (p->nSet<(BITVEC_NINT-1)) {
! 35886: goto bitvec_set_end;
! 35887: } else {
! 35888: goto bitvec_set_rehash;
! 35889: }
! 35890: }
! 35891: /* there was a collision, check to see if it's already */
! 35892: /* in hash, if not, try to find a spot for it */
! 35893: do {
! 35894: if( p->u.aHash[h]==i ) return SQLITE_OK;
! 35895: h++;
! 35896: if( h>=BITVEC_NINT ) h = 0;
! 35897: } while( p->u.aHash[h] );
! 35898: /* we didn't find it in the hash. h points to the first */
! 35899: /* available free spot. check to see if this is going to */
! 35900: /* make our hash too "full". */
! 35901: bitvec_set_rehash:
! 35902: if( p->nSet>=BITVEC_MXHASH ){
! 35903: unsigned int j;
! 35904: int rc;
! 35905: u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
! 35906: if( aiValues==0 ){
! 35907: return SQLITE_NOMEM;
! 35908: }else{
! 35909: memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
! 35910: memset(p->u.apSub, 0, sizeof(p->u.apSub));
! 35911: p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
! 35912: rc = sqlite3BitvecSet(p, i);
! 35913: for(j=0; j<BITVEC_NINT; j++){
! 35914: if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
! 35915: }
! 35916: sqlite3StackFree(0, aiValues);
! 35917: return rc;
! 35918: }
! 35919: }
! 35920: bitvec_set_end:
! 35921: p->nSet++;
! 35922: p->u.aHash[h] = i;
! 35923: return SQLITE_OK;
! 35924: }
! 35925:
! 35926: /*
! 35927: ** Clear the i-th bit.
! 35928: **
! 35929: ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
! 35930: ** that BitvecClear can use to rebuilt its hash table.
! 35931: */
! 35932: SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
! 35933: if( p==0 ) return;
! 35934: assert( i>0 );
! 35935: i--;
! 35936: while( p->iDivisor ){
! 35937: u32 bin = i/p->iDivisor;
! 35938: i = i%p->iDivisor;
! 35939: p = p->u.apSub[bin];
! 35940: if (!p) {
! 35941: return;
! 35942: }
! 35943: }
! 35944: if( p->iSize<=BITVEC_NBIT ){
! 35945: p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
! 35946: }else{
! 35947: unsigned int j;
! 35948: u32 *aiValues = pBuf;
! 35949: memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
! 35950: memset(p->u.aHash, 0, sizeof(p->u.aHash));
! 35951: p->nSet = 0;
! 35952: for(j=0; j<BITVEC_NINT; j++){
! 35953: if( aiValues[j] && aiValues[j]!=(i+1) ){
! 35954: u32 h = BITVEC_HASH(aiValues[j]-1);
! 35955: p->nSet++;
! 35956: while( p->u.aHash[h] ){
! 35957: h++;
! 35958: if( h>=BITVEC_NINT ) h = 0;
! 35959: }
! 35960: p->u.aHash[h] = aiValues[j];
! 35961: }
! 35962: }
! 35963: }
! 35964: }
! 35965:
! 35966: /*
! 35967: ** Destroy a bitmap object. Reclaim all memory used.
! 35968: */
! 35969: SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
! 35970: if( p==0 ) return;
! 35971: if( p->iDivisor ){
! 35972: unsigned int i;
! 35973: for(i=0; i<BITVEC_NPTR; i++){
! 35974: sqlite3BitvecDestroy(p->u.apSub[i]);
! 35975: }
! 35976: }
! 35977: sqlite3_free(p);
! 35978: }
! 35979:
! 35980: /*
! 35981: ** Return the value of the iSize parameter specified when Bitvec *p
! 35982: ** was created.
! 35983: */
! 35984: SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
! 35985: return p->iSize;
! 35986: }
! 35987:
! 35988: #ifndef SQLITE_OMIT_BUILTIN_TEST
! 35989: /*
! 35990: ** Let V[] be an array of unsigned characters sufficient to hold
! 35991: ** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
! 35992: ** Then the following macros can be used to set, clear, or test
! 35993: ** individual bits within V.
! 35994: */
! 35995: #define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
! 35996: #define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
! 35997: #define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
! 35998:
! 35999: /*
! 36000: ** This routine runs an extensive test of the Bitvec code.
! 36001: **
! 36002: ** The input is an array of integers that acts as a program
! 36003: ** to test the Bitvec. The integers are opcodes followed
! 36004: ** by 0, 1, or 3 operands, depending on the opcode. Another
! 36005: ** opcode follows immediately after the last operand.
! 36006: **
! 36007: ** There are 6 opcodes numbered from 0 through 5. 0 is the
! 36008: ** "halt" opcode and causes the test to end.
! 36009: **
! 36010: ** 0 Halt and return the number of errors
! 36011: ** 1 N S X Set N bits beginning with S and incrementing by X
! 36012: ** 2 N S X Clear N bits beginning with S and incrementing by X
! 36013: ** 3 N Set N randomly chosen bits
! 36014: ** 4 N Clear N randomly chosen bits
! 36015: ** 5 N S X Set N bits from S increment X in array only, not in bitvec
! 36016: **
! 36017: ** The opcodes 1 through 4 perform set and clear operations are performed
! 36018: ** on both a Bitvec object and on a linear array of bits obtained from malloc.
! 36019: ** Opcode 5 works on the linear array only, not on the Bitvec.
! 36020: ** Opcode 5 is used to deliberately induce a fault in order to
! 36021: ** confirm that error detection works.
! 36022: **
! 36023: ** At the conclusion of the test the linear array is compared
! 36024: ** against the Bitvec object. If there are any differences,
! 36025: ** an error is returned. If they are the same, zero is returned.
! 36026: **
! 36027: ** If a memory allocation error occurs, return -1.
! 36028: */
! 36029: SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
! 36030: Bitvec *pBitvec = 0;
! 36031: unsigned char *pV = 0;
! 36032: int rc = -1;
! 36033: int i, nx, pc, op;
! 36034: void *pTmpSpace;
! 36035:
! 36036: /* Allocate the Bitvec to be tested and a linear array of
! 36037: ** bits to act as the reference */
! 36038: pBitvec = sqlite3BitvecCreate( sz );
! 36039: pV = sqlite3_malloc( (sz+7)/8 + 1 );
! 36040: pTmpSpace = sqlite3_malloc(BITVEC_SZ);
! 36041: if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
! 36042: memset(pV, 0, (sz+7)/8 + 1);
! 36043:
! 36044: /* NULL pBitvec tests */
! 36045: sqlite3BitvecSet(0, 1);
! 36046: sqlite3BitvecClear(0, 1, pTmpSpace);
! 36047:
! 36048: /* Run the program */
! 36049: pc = 0;
! 36050: while( (op = aOp[pc])!=0 ){
! 36051: switch( op ){
! 36052: case 1:
! 36053: case 2:
! 36054: case 5: {
! 36055: nx = 4;
! 36056: i = aOp[pc+2] - 1;
! 36057: aOp[pc+2] += aOp[pc+3];
! 36058: break;
! 36059: }
! 36060: case 3:
! 36061: case 4:
! 36062: default: {
! 36063: nx = 2;
! 36064: sqlite3_randomness(sizeof(i), &i);
! 36065: break;
! 36066: }
! 36067: }
! 36068: if( (--aOp[pc+1]) > 0 ) nx = 0;
! 36069: pc += nx;
! 36070: i = (i & 0x7fffffff)%sz;
! 36071: if( (op & 1)!=0 ){
! 36072: SETBIT(pV, (i+1));
! 36073: if( op!=5 ){
! 36074: if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
! 36075: }
! 36076: }else{
! 36077: CLEARBIT(pV, (i+1));
! 36078: sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
! 36079: }
! 36080: }
! 36081:
! 36082: /* Test to make sure the linear array exactly matches the
! 36083: ** Bitvec object. Start with the assumption that they do
! 36084: ** match (rc==0). Change rc to non-zero if a discrepancy
! 36085: ** is found.
! 36086: */
! 36087: rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
! 36088: + sqlite3BitvecTest(pBitvec, 0)
! 36089: + (sqlite3BitvecSize(pBitvec) - sz);
! 36090: for(i=1; i<=sz; i++){
! 36091: if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
! 36092: rc = i;
! 36093: break;
! 36094: }
! 36095: }
! 36096:
! 36097: /* Free allocated structure */
! 36098: bitvec_end:
! 36099: sqlite3_free(pTmpSpace);
! 36100: sqlite3_free(pV);
! 36101: sqlite3BitvecDestroy(pBitvec);
! 36102: return rc;
! 36103: }
! 36104: #endif /* SQLITE_OMIT_BUILTIN_TEST */
! 36105:
! 36106: /************** End of bitvec.c **********************************************/
! 36107: /************** Begin file pcache.c ******************************************/
! 36108: /*
! 36109: ** 2008 August 05
! 36110: **
! 36111: ** The author disclaims copyright to this source code. In place of
! 36112: ** a legal notice, here is a blessing:
! 36113: **
! 36114: ** May you do good and not evil.
! 36115: ** May you find forgiveness for yourself and forgive others.
! 36116: ** May you share freely, never taking more than you give.
! 36117: **
! 36118: *************************************************************************
! 36119: ** This file implements that page cache.
! 36120: */
! 36121:
! 36122: /*
! 36123: ** A complete page cache is an instance of this structure.
! 36124: */
! 36125: struct PCache {
! 36126: PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
! 36127: PgHdr *pSynced; /* Last synced page in dirty page list */
! 36128: int nRef; /* Number of referenced pages */
! 36129: int szCache; /* Configured cache size */
! 36130: int szPage; /* Size of every page in this cache */
! 36131: int szExtra; /* Size of extra space for each page */
! 36132: int bPurgeable; /* True if pages are on backing store */
! 36133: int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
! 36134: void *pStress; /* Argument to xStress */
! 36135: sqlite3_pcache *pCache; /* Pluggable cache module */
! 36136: PgHdr *pPage1; /* Reference to page 1 */
! 36137: };
! 36138:
! 36139: /*
! 36140: ** Some of the assert() macros in this code are too expensive to run
! 36141: ** even during normal debugging. Use them only rarely on long-running
! 36142: ** tests. Enable the expensive asserts using the
! 36143: ** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
! 36144: */
! 36145: #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
! 36146: # define expensive_assert(X) assert(X)
! 36147: #else
! 36148: # define expensive_assert(X)
! 36149: #endif
! 36150:
! 36151: /********************************** Linked List Management ********************/
! 36152:
! 36153: #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
! 36154: /*
! 36155: ** Check that the pCache->pSynced variable is set correctly. If it
! 36156: ** is not, either fail an assert or return zero. Otherwise, return
! 36157: ** non-zero. This is only used in debugging builds, as follows:
! 36158: **
! 36159: ** expensive_assert( pcacheCheckSynced(pCache) );
! 36160: */
! 36161: static int pcacheCheckSynced(PCache *pCache){
! 36162: PgHdr *p;
! 36163: for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
! 36164: assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
! 36165: }
! 36166: return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
! 36167: }
! 36168: #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
! 36169:
! 36170: /*
! 36171: ** Remove page pPage from the list of dirty pages.
! 36172: */
! 36173: static void pcacheRemoveFromDirtyList(PgHdr *pPage){
! 36174: PCache *p = pPage->pCache;
! 36175:
! 36176: assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
! 36177: assert( pPage->pDirtyPrev || pPage==p->pDirty );
! 36178:
! 36179: /* Update the PCache1.pSynced variable if necessary. */
! 36180: if( p->pSynced==pPage ){
! 36181: PgHdr *pSynced = pPage->pDirtyPrev;
! 36182: while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
! 36183: pSynced = pSynced->pDirtyPrev;
! 36184: }
! 36185: p->pSynced = pSynced;
! 36186: }
! 36187:
! 36188: if( pPage->pDirtyNext ){
! 36189: pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
! 36190: }else{
! 36191: assert( pPage==p->pDirtyTail );
! 36192: p->pDirtyTail = pPage->pDirtyPrev;
! 36193: }
! 36194: if( pPage->pDirtyPrev ){
! 36195: pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
! 36196: }else{
! 36197: assert( pPage==p->pDirty );
! 36198: p->pDirty = pPage->pDirtyNext;
! 36199: }
! 36200: pPage->pDirtyNext = 0;
! 36201: pPage->pDirtyPrev = 0;
! 36202:
! 36203: expensive_assert( pcacheCheckSynced(p) );
! 36204: }
! 36205:
! 36206: /*
! 36207: ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
! 36208: ** pPage).
! 36209: */
! 36210: static void pcacheAddToDirtyList(PgHdr *pPage){
! 36211: PCache *p = pPage->pCache;
! 36212:
! 36213: assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
! 36214:
! 36215: pPage->pDirtyNext = p->pDirty;
! 36216: if( pPage->pDirtyNext ){
! 36217: assert( pPage->pDirtyNext->pDirtyPrev==0 );
! 36218: pPage->pDirtyNext->pDirtyPrev = pPage;
! 36219: }
! 36220: p->pDirty = pPage;
! 36221: if( !p->pDirtyTail ){
! 36222: p->pDirtyTail = pPage;
! 36223: }
! 36224: if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
! 36225: p->pSynced = pPage;
! 36226: }
! 36227: expensive_assert( pcacheCheckSynced(p) );
! 36228: }
! 36229:
! 36230: /*
! 36231: ** Wrapper around the pluggable caches xUnpin method. If the cache is
! 36232: ** being used for an in-memory database, this function is a no-op.
! 36233: */
! 36234: static void pcacheUnpin(PgHdr *p){
! 36235: PCache *pCache = p->pCache;
! 36236: if( pCache->bPurgeable ){
! 36237: if( p->pgno==1 ){
! 36238: pCache->pPage1 = 0;
! 36239: }
! 36240: sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
! 36241: }
! 36242: }
! 36243:
! 36244: /*************************************************** General Interfaces ******
! 36245: **
! 36246: ** Initialize and shutdown the page cache subsystem. Neither of these
! 36247: ** functions are threadsafe.
! 36248: */
! 36249: SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
! 36250: if( sqlite3GlobalConfig.pcache2.xInit==0 ){
! 36251: /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
! 36252: ** built-in default page cache is used instead of the application defined
! 36253: ** page cache. */
! 36254: sqlite3PCacheSetDefault();
! 36255: }
! 36256: return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
! 36257: }
! 36258: SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
! 36259: if( sqlite3GlobalConfig.pcache2.xShutdown ){
! 36260: /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
! 36261: sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
! 36262: }
! 36263: }
! 36264:
! 36265: /*
! 36266: ** Return the size in bytes of a PCache object.
! 36267: */
! 36268: SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
! 36269:
! 36270: /*
! 36271: ** Create a new PCache object. Storage space to hold the object
! 36272: ** has already been allocated and is passed in as the p pointer.
! 36273: ** The caller discovers how much space needs to be allocated by
! 36274: ** calling sqlite3PcacheSize().
! 36275: */
! 36276: SQLITE_PRIVATE void sqlite3PcacheOpen(
! 36277: int szPage, /* Size of every page */
! 36278: int szExtra, /* Extra space associated with each page */
! 36279: int bPurgeable, /* True if pages are on backing store */
! 36280: int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
! 36281: void *pStress, /* Argument to xStress */
! 36282: PCache *p /* Preallocated space for the PCache */
! 36283: ){
! 36284: memset(p, 0, sizeof(PCache));
! 36285: p->szPage = szPage;
! 36286: p->szExtra = szExtra;
! 36287: p->bPurgeable = bPurgeable;
! 36288: p->xStress = xStress;
! 36289: p->pStress = pStress;
! 36290: p->szCache = 100;
! 36291: }
! 36292:
! 36293: /*
! 36294: ** Change the page size for PCache object. The caller must ensure that there
! 36295: ** are no outstanding page references when this function is called.
! 36296: */
! 36297: SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
! 36298: assert( pCache->nRef==0 && pCache->pDirty==0 );
! 36299: if( pCache->pCache ){
! 36300: sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
! 36301: pCache->pCache = 0;
! 36302: pCache->pPage1 = 0;
! 36303: }
! 36304: pCache->szPage = szPage;
! 36305: }
! 36306:
! 36307: /*
! 36308: ** Compute the number of pages of cache requested.
! 36309: */
! 36310: static int numberOfCachePages(PCache *p){
! 36311: if( p->szCache>=0 ){
! 36312: return p->szCache;
! 36313: }else{
! 36314: return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
! 36315: }
! 36316: }
! 36317:
! 36318: /*
! 36319: ** Try to obtain a page from the cache.
! 36320: */
! 36321: SQLITE_PRIVATE int sqlite3PcacheFetch(
! 36322: PCache *pCache, /* Obtain the page from this cache */
! 36323: Pgno pgno, /* Page number to obtain */
! 36324: int createFlag, /* If true, create page if it does not exist already */
! 36325: PgHdr **ppPage /* Write the page here */
! 36326: ){
! 36327: sqlite3_pcache_page *pPage = 0;
! 36328: PgHdr *pPgHdr = 0;
! 36329: int eCreate;
! 36330:
! 36331: assert( pCache!=0 );
! 36332: assert( createFlag==1 || createFlag==0 );
! 36333: assert( pgno>0 );
! 36334:
! 36335: /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
! 36336: ** allocate it now.
! 36337: */
! 36338: if( !pCache->pCache && createFlag ){
! 36339: sqlite3_pcache *p;
! 36340: p = sqlite3GlobalConfig.pcache2.xCreate(
! 36341: pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
! 36342: );
! 36343: if( !p ){
! 36344: return SQLITE_NOMEM;
! 36345: }
! 36346: sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
! 36347: pCache->pCache = p;
! 36348: }
! 36349:
! 36350: eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
! 36351: if( pCache->pCache ){
! 36352: pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
! 36353: }
! 36354:
! 36355: if( !pPage && eCreate==1 ){
! 36356: PgHdr *pPg;
! 36357:
! 36358: /* Find a dirty page to write-out and recycle. First try to find a
! 36359: ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
! 36360: ** cleared), but if that is not possible settle for any other
! 36361: ** unreferenced dirty page.
! 36362: */
! 36363: expensive_assert( pcacheCheckSynced(pCache) );
! 36364: for(pPg=pCache->pSynced;
! 36365: pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
! 36366: pPg=pPg->pDirtyPrev
! 36367: );
! 36368: pCache->pSynced = pPg;
! 36369: if( !pPg ){
! 36370: for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
! 36371: }
! 36372: if( pPg ){
! 36373: int rc;
! 36374: #ifdef SQLITE_LOG_CACHE_SPILL
! 36375: sqlite3_log(SQLITE_FULL,
! 36376: "spill page %d making room for %d - cache used: %d/%d",
! 36377: pPg->pgno, pgno,
! 36378: sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
! 36379: numberOfCachePages(pCache));
! 36380: #endif
! 36381: rc = pCache->xStress(pCache->pStress, pPg);
! 36382: if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
! 36383: return rc;
! 36384: }
! 36385: }
! 36386:
! 36387: pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
! 36388: }
! 36389:
! 36390: if( pPage ){
! 36391: pPgHdr = (PgHdr *)pPage->pExtra;
! 36392:
! 36393: if( !pPgHdr->pPage ){
! 36394: memset(pPgHdr, 0, sizeof(PgHdr));
! 36395: pPgHdr->pPage = pPage;
! 36396: pPgHdr->pData = pPage->pBuf;
! 36397: pPgHdr->pExtra = (void *)&pPgHdr[1];
! 36398: memset(pPgHdr->pExtra, 0, pCache->szExtra);
! 36399: pPgHdr->pCache = pCache;
! 36400: pPgHdr->pgno = pgno;
! 36401: }
! 36402: assert( pPgHdr->pCache==pCache );
! 36403: assert( pPgHdr->pgno==pgno );
! 36404: assert( pPgHdr->pData==pPage->pBuf );
! 36405: assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
! 36406:
! 36407: if( 0==pPgHdr->nRef ){
! 36408: pCache->nRef++;
! 36409: }
! 36410: pPgHdr->nRef++;
! 36411: if( pgno==1 ){
! 36412: pCache->pPage1 = pPgHdr;
! 36413: }
! 36414: }
! 36415: *ppPage = pPgHdr;
! 36416: return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
! 36417: }
! 36418:
! 36419: /*
! 36420: ** Decrement the reference count on a page. If the page is clean and the
! 36421: ** reference count drops to 0, then it is made elible for recycling.
! 36422: */
! 36423: SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
! 36424: assert( p->nRef>0 );
! 36425: p->nRef--;
! 36426: if( p->nRef==0 ){
! 36427: PCache *pCache = p->pCache;
! 36428: pCache->nRef--;
! 36429: if( (p->flags&PGHDR_DIRTY)==0 ){
! 36430: pcacheUnpin(p);
! 36431: }else{
! 36432: /* Move the page to the head of the dirty list. */
! 36433: pcacheRemoveFromDirtyList(p);
! 36434: pcacheAddToDirtyList(p);
! 36435: }
! 36436: }
! 36437: }
! 36438:
! 36439: /*
! 36440: ** Increase the reference count of a supplied page by 1.
! 36441: */
! 36442: SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
! 36443: assert(p->nRef>0);
! 36444: p->nRef++;
! 36445: }
! 36446:
! 36447: /*
! 36448: ** Drop a page from the cache. There must be exactly one reference to the
! 36449: ** page. This function deletes that reference, so after it returns the
! 36450: ** page pointed to by p is invalid.
! 36451: */
! 36452: SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
! 36453: PCache *pCache;
! 36454: assert( p->nRef==1 );
! 36455: if( p->flags&PGHDR_DIRTY ){
! 36456: pcacheRemoveFromDirtyList(p);
! 36457: }
! 36458: pCache = p->pCache;
! 36459: pCache->nRef--;
! 36460: if( p->pgno==1 ){
! 36461: pCache->pPage1 = 0;
! 36462: }
! 36463: sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
! 36464: }
! 36465:
! 36466: /*
! 36467: ** Make sure the page is marked as dirty. If it isn't dirty already,
! 36468: ** make it so.
! 36469: */
! 36470: SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
! 36471: p->flags &= ~PGHDR_DONT_WRITE;
! 36472: assert( p->nRef>0 );
! 36473: if( 0==(p->flags & PGHDR_DIRTY) ){
! 36474: p->flags |= PGHDR_DIRTY;
! 36475: pcacheAddToDirtyList( p);
! 36476: }
! 36477: }
! 36478:
! 36479: /*
! 36480: ** Make sure the page is marked as clean. If it isn't clean already,
! 36481: ** make it so.
! 36482: */
! 36483: SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
! 36484: if( (p->flags & PGHDR_DIRTY) ){
! 36485: pcacheRemoveFromDirtyList(p);
! 36486: p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
! 36487: if( p->nRef==0 ){
! 36488: pcacheUnpin(p);
! 36489: }
! 36490: }
! 36491: }
! 36492:
! 36493: /*
! 36494: ** Make every page in the cache clean.
! 36495: */
! 36496: SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
! 36497: PgHdr *p;
! 36498: while( (p = pCache->pDirty)!=0 ){
! 36499: sqlite3PcacheMakeClean(p);
! 36500: }
! 36501: }
! 36502:
! 36503: /*
! 36504: ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
! 36505: */
! 36506: SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
! 36507: PgHdr *p;
! 36508: for(p=pCache->pDirty; p; p=p->pDirtyNext){
! 36509: p->flags &= ~PGHDR_NEED_SYNC;
! 36510: }
! 36511: pCache->pSynced = pCache->pDirtyTail;
! 36512: }
! 36513:
! 36514: /*
! 36515: ** Change the page number of page p to newPgno.
! 36516: */
! 36517: SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
! 36518: PCache *pCache = p->pCache;
! 36519: assert( p->nRef>0 );
! 36520: assert( newPgno>0 );
! 36521: sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
! 36522: p->pgno = newPgno;
! 36523: if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
! 36524: pcacheRemoveFromDirtyList(p);
! 36525: pcacheAddToDirtyList(p);
! 36526: }
! 36527: }
! 36528:
! 36529: /*
! 36530: ** Drop every cache entry whose page number is greater than "pgno". The
! 36531: ** caller must ensure that there are no outstanding references to any pages
! 36532: ** other than page 1 with a page number greater than pgno.
! 36533: **
! 36534: ** If there is a reference to page 1 and the pgno parameter passed to this
! 36535: ** function is 0, then the data area associated with page 1 is zeroed, but
! 36536: ** the page object is not dropped.
! 36537: */
! 36538: SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
! 36539: if( pCache->pCache ){
! 36540: PgHdr *p;
! 36541: PgHdr *pNext;
! 36542: for(p=pCache->pDirty; p; p=pNext){
! 36543: pNext = p->pDirtyNext;
! 36544: /* This routine never gets call with a positive pgno except right
! 36545: ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
! 36546: ** it must be that pgno==0.
! 36547: */
! 36548: assert( p->pgno>0 );
! 36549: if( ALWAYS(p->pgno>pgno) ){
! 36550: assert( p->flags&PGHDR_DIRTY );
! 36551: sqlite3PcacheMakeClean(p);
! 36552: }
! 36553: }
! 36554: if( pgno==0 && pCache->pPage1 ){
! 36555: memset(pCache->pPage1->pData, 0, pCache->szPage);
! 36556: pgno = 1;
! 36557: }
! 36558: sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
! 36559: }
! 36560: }
! 36561:
! 36562: /*
! 36563: ** Close a cache.
! 36564: */
! 36565: SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
! 36566: if( pCache->pCache ){
! 36567: sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
! 36568: }
! 36569: }
! 36570:
! 36571: /*
! 36572: ** Discard the contents of the cache.
! 36573: */
! 36574: SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
! 36575: sqlite3PcacheTruncate(pCache, 0);
! 36576: }
! 36577:
! 36578: /*
! 36579: ** Merge two lists of pages connected by pDirty and in pgno order.
! 36580: ** Do not both fixing the pDirtyPrev pointers.
! 36581: */
! 36582: static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
! 36583: PgHdr result, *pTail;
! 36584: pTail = &result;
! 36585: while( pA && pB ){
! 36586: if( pA->pgno<pB->pgno ){
! 36587: pTail->pDirty = pA;
! 36588: pTail = pA;
! 36589: pA = pA->pDirty;
! 36590: }else{
! 36591: pTail->pDirty = pB;
! 36592: pTail = pB;
! 36593: pB = pB->pDirty;
! 36594: }
! 36595: }
! 36596: if( pA ){
! 36597: pTail->pDirty = pA;
! 36598: }else if( pB ){
! 36599: pTail->pDirty = pB;
! 36600: }else{
! 36601: pTail->pDirty = 0;
! 36602: }
! 36603: return result.pDirty;
! 36604: }
! 36605:
! 36606: /*
! 36607: ** Sort the list of pages in accending order by pgno. Pages are
! 36608: ** connected by pDirty pointers. The pDirtyPrev pointers are
! 36609: ** corrupted by this sort.
! 36610: **
! 36611: ** Since there cannot be more than 2^31 distinct pages in a database,
! 36612: ** there cannot be more than 31 buckets required by the merge sorter.
! 36613: ** One extra bucket is added to catch overflow in case something
! 36614: ** ever changes to make the previous sentence incorrect.
! 36615: */
! 36616: #define N_SORT_BUCKET 32
! 36617: static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
! 36618: PgHdr *a[N_SORT_BUCKET], *p;
! 36619: int i;
! 36620: memset(a, 0, sizeof(a));
! 36621: while( pIn ){
! 36622: p = pIn;
! 36623: pIn = p->pDirty;
! 36624: p->pDirty = 0;
! 36625: for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
! 36626: if( a[i]==0 ){
! 36627: a[i] = p;
! 36628: break;
! 36629: }else{
! 36630: p = pcacheMergeDirtyList(a[i], p);
! 36631: a[i] = 0;
! 36632: }
! 36633: }
! 36634: if( NEVER(i==N_SORT_BUCKET-1) ){
! 36635: /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
! 36636: ** the input list. But that is impossible.
! 36637: */
! 36638: a[i] = pcacheMergeDirtyList(a[i], p);
! 36639: }
! 36640: }
! 36641: p = a[0];
! 36642: for(i=1; i<N_SORT_BUCKET; i++){
! 36643: p = pcacheMergeDirtyList(p, a[i]);
! 36644: }
! 36645: return p;
! 36646: }
! 36647:
! 36648: /*
! 36649: ** Return a list of all dirty pages in the cache, sorted by page number.
! 36650: */
! 36651: SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
! 36652: PgHdr *p;
! 36653: for(p=pCache->pDirty; p; p=p->pDirtyNext){
! 36654: p->pDirty = p->pDirtyNext;
! 36655: }
! 36656: return pcacheSortDirtyList(pCache->pDirty);
! 36657: }
! 36658:
! 36659: /*
! 36660: ** Return the total number of referenced pages held by the cache.
! 36661: */
! 36662: SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
! 36663: return pCache->nRef;
! 36664: }
! 36665:
! 36666: /*
! 36667: ** Return the number of references to the page supplied as an argument.
! 36668: */
! 36669: SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
! 36670: return p->nRef;
! 36671: }
! 36672:
! 36673: /*
! 36674: ** Return the total number of pages in the cache.
! 36675: */
! 36676: SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
! 36677: int nPage = 0;
! 36678: if( pCache->pCache ){
! 36679: nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
! 36680: }
! 36681: return nPage;
! 36682: }
! 36683:
! 36684: #ifdef SQLITE_TEST
! 36685: /*
! 36686: ** Get the suggested cache-size value.
! 36687: */
! 36688: SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
! 36689: return numberOfCachePages(pCache);
! 36690: }
! 36691: #endif
! 36692:
! 36693: /*
! 36694: ** Set the suggested cache-size value.
! 36695: */
! 36696: SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
! 36697: pCache->szCache = mxPage;
! 36698: if( pCache->pCache ){
! 36699: sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
! 36700: numberOfCachePages(pCache));
! 36701: }
! 36702: }
! 36703:
! 36704: /*
! 36705: ** Free up as much memory as possible from the page cache.
! 36706: */
! 36707: SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
! 36708: if( pCache->pCache ){
! 36709: sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
! 36710: }
! 36711: }
! 36712:
! 36713: #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
! 36714: /*
! 36715: ** For all dirty pages currently in the cache, invoke the specified
! 36716: ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
! 36717: ** defined.
! 36718: */
! 36719: SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
! 36720: PgHdr *pDirty;
! 36721: for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
! 36722: xIter(pDirty);
! 36723: }
! 36724: }
! 36725: #endif
! 36726:
! 36727: /************** End of pcache.c **********************************************/
! 36728: /************** Begin file pcache1.c *****************************************/
! 36729: /*
! 36730: ** 2008 November 05
! 36731: **
! 36732: ** The author disclaims copyright to this source code. In place of
! 36733: ** a legal notice, here is a blessing:
! 36734: **
! 36735: ** May you do good and not evil.
! 36736: ** May you find forgiveness for yourself and forgive others.
! 36737: ** May you share freely, never taking more than you give.
! 36738: **
! 36739: *************************************************************************
! 36740: **
! 36741: ** This file implements the default page cache implementation (the
! 36742: ** sqlite3_pcache interface). It also contains part of the implementation
! 36743: ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
! 36744: ** If the default page cache implementation is overriden, then neither of
! 36745: ** these two features are available.
! 36746: */
! 36747:
! 36748:
! 36749: typedef struct PCache1 PCache1;
! 36750: typedef struct PgHdr1 PgHdr1;
! 36751: typedef struct PgFreeslot PgFreeslot;
! 36752: typedef struct PGroup PGroup;
! 36753:
! 36754: /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
! 36755: ** of one or more PCaches that are able to recycle each others unpinned
! 36756: ** pages when they are under memory pressure. A PGroup is an instance of
! 36757: ** the following object.
! 36758: **
! 36759: ** This page cache implementation works in one of two modes:
! 36760: **
! 36761: ** (1) Every PCache is the sole member of its own PGroup. There is
! 36762: ** one PGroup per PCache.
! 36763: **
! 36764: ** (2) There is a single global PGroup that all PCaches are a member
! 36765: ** of.
! 36766: **
! 36767: ** Mode 1 uses more memory (since PCache instances are not able to rob
! 36768: ** unused pages from other PCaches) but it also operates without a mutex,
! 36769: ** and is therefore often faster. Mode 2 requires a mutex in order to be
! 36770: ** threadsafe, but recycles pages more efficiently.
! 36771: **
! 36772: ** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
! 36773: ** PGroup which is the pcache1.grp global variable and its mutex is
! 36774: ** SQLITE_MUTEX_STATIC_LRU.
! 36775: */
! 36776: struct PGroup {
! 36777: sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
! 36778: unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
! 36779: unsigned int nMinPage; /* Sum of nMin for purgeable caches */
! 36780: unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
! 36781: unsigned int nCurrentPage; /* Number of purgeable pages allocated */
! 36782: PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
! 36783: };
! 36784:
! 36785: /* Each page cache is an instance of the following object. Every
! 36786: ** open database file (including each in-memory database and each
! 36787: ** temporary or transient database) has a single page cache which
! 36788: ** is an instance of this object.
! 36789: **
! 36790: ** Pointers to structures of this type are cast and returned as
! 36791: ** opaque sqlite3_pcache* handles.
! 36792: */
! 36793: struct PCache1 {
! 36794: /* Cache configuration parameters. Page size (szPage) and the purgeable
! 36795: ** flag (bPurgeable) are set when the cache is created. nMax may be
! 36796: ** modified at any time by a call to the pcache1Cachesize() method.
! 36797: ** The PGroup mutex must be held when accessing nMax.
! 36798: */
! 36799: PGroup *pGroup; /* PGroup this cache belongs to */
! 36800: int szPage; /* Size of allocated pages in bytes */
! 36801: int szExtra; /* Size of extra space in bytes */
! 36802: int bPurgeable; /* True if cache is purgeable */
! 36803: unsigned int nMin; /* Minimum number of pages reserved */
! 36804: unsigned int nMax; /* Configured "cache_size" value */
! 36805: unsigned int n90pct; /* nMax*9/10 */
! 36806:
! 36807: /* Hash table of all pages. The following variables may only be accessed
! 36808: ** when the accessor is holding the PGroup mutex.
! 36809: */
! 36810: unsigned int nRecyclable; /* Number of pages in the LRU list */
! 36811: unsigned int nPage; /* Total number of pages in apHash */
! 36812: unsigned int nHash; /* Number of slots in apHash[] */
! 36813: PgHdr1 **apHash; /* Hash table for fast lookup by key */
! 36814:
! 36815: unsigned int iMaxKey; /* Largest key seen since xTruncate() */
! 36816: };
! 36817:
! 36818: /*
! 36819: ** Each cache entry is represented by an instance of the following
! 36820: ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
! 36821: ** PgHdr1.pCache->szPage bytes is allocated directly before this structure
! 36822: ** in memory.
! 36823: */
! 36824: struct PgHdr1 {
! 36825: sqlite3_pcache_page page;
! 36826: unsigned int iKey; /* Key value (page number) */
! 36827: PgHdr1 *pNext; /* Next in hash table chain */
! 36828: PCache1 *pCache; /* Cache that currently owns this page */
! 36829: PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
! 36830: PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
! 36831: };
! 36832:
! 36833: /*
! 36834: ** Free slots in the allocator used to divide up the buffer provided using
! 36835: ** the SQLITE_CONFIG_PAGECACHE mechanism.
! 36836: */
! 36837: struct PgFreeslot {
! 36838: PgFreeslot *pNext; /* Next free slot */
! 36839: };
! 36840:
! 36841: /*
! 36842: ** Global data used by this cache.
! 36843: */
! 36844: static SQLITE_WSD struct PCacheGlobal {
! 36845: PGroup grp; /* The global PGroup for mode (2) */
! 36846:
! 36847: /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
! 36848: ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
! 36849: ** fixed at sqlite3_initialize() time and do not require mutex protection.
! 36850: ** The nFreeSlot and pFree values do require mutex protection.
! 36851: */
! 36852: int isInit; /* True if initialized */
! 36853: int szSlot; /* Size of each free slot */
! 36854: int nSlot; /* The number of pcache slots */
! 36855: int nReserve; /* Try to keep nFreeSlot above this */
! 36856: void *pStart, *pEnd; /* Bounds of pagecache malloc range */
! 36857: /* Above requires no mutex. Use mutex below for variable that follow. */
! 36858: sqlite3_mutex *mutex; /* Mutex for accessing the following: */
! 36859: int nFreeSlot; /* Number of unused pcache slots */
! 36860: PgFreeslot *pFree; /* Free page blocks */
! 36861: /* The following value requires a mutex to change. We skip the mutex on
! 36862: ** reading because (1) most platforms read a 32-bit integer atomically and
! 36863: ** (2) even if an incorrect value is read, no great harm is done since this
! 36864: ** is really just an optimization. */
! 36865: int bUnderPressure; /* True if low on PAGECACHE memory */
! 36866: } pcache1_g;
! 36867:
! 36868: /*
! 36869: ** All code in this file should access the global structure above via the
! 36870: ** alias "pcache1". This ensures that the WSD emulation is used when
! 36871: ** compiling for systems that do not support real WSD.
! 36872: */
! 36873: #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
! 36874:
! 36875: /*
! 36876: ** Macros to enter and leave the PCache LRU mutex.
! 36877: */
! 36878: #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
! 36879: #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
! 36880:
! 36881: /******************************************************************************/
! 36882: /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
! 36883:
! 36884: /*
! 36885: ** This function is called during initialization if a static buffer is
! 36886: ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
! 36887: ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
! 36888: ** enough to contain 'n' buffers of 'sz' bytes each.
! 36889: **
! 36890: ** This routine is called from sqlite3_initialize() and so it is guaranteed
! 36891: ** to be serialized already. There is no need for further mutexing.
! 36892: */
! 36893: SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
! 36894: if( pcache1.isInit ){
! 36895: PgFreeslot *p;
! 36896: sz = ROUNDDOWN8(sz);
! 36897: pcache1.szSlot = sz;
! 36898: pcache1.nSlot = pcache1.nFreeSlot = n;
! 36899: pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
! 36900: pcache1.pStart = pBuf;
! 36901: pcache1.pFree = 0;
! 36902: pcache1.bUnderPressure = 0;
! 36903: while( n-- ){
! 36904: p = (PgFreeslot*)pBuf;
! 36905: p->pNext = pcache1.pFree;
! 36906: pcache1.pFree = p;
! 36907: pBuf = (void*)&((char*)pBuf)[sz];
! 36908: }
! 36909: pcache1.pEnd = pBuf;
! 36910: }
! 36911: }
! 36912:
! 36913: /*
! 36914: ** Malloc function used within this file to allocate space from the buffer
! 36915: ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
! 36916: ** such buffer exists or there is no space left in it, this function falls
! 36917: ** back to sqlite3Malloc().
! 36918: **
! 36919: ** Multiple threads can run this routine at the same time. Global variables
! 36920: ** in pcache1 need to be protected via mutex.
! 36921: */
! 36922: static void *pcache1Alloc(int nByte){
! 36923: void *p = 0;
! 36924: assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
! 36925: sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
! 36926: if( nByte<=pcache1.szSlot ){
! 36927: sqlite3_mutex_enter(pcache1.mutex);
! 36928: p = (PgHdr1 *)pcache1.pFree;
! 36929: if( p ){
! 36930: pcache1.pFree = pcache1.pFree->pNext;
! 36931: pcache1.nFreeSlot--;
! 36932: pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
! 36933: assert( pcache1.nFreeSlot>=0 );
! 36934: sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
! 36935: }
! 36936: sqlite3_mutex_leave(pcache1.mutex);
! 36937: }
! 36938: if( p==0 ){
! 36939: /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
! 36940: ** it from sqlite3Malloc instead.
! 36941: */
! 36942: p = sqlite3Malloc(nByte);
! 36943: if( p ){
! 36944: int sz = sqlite3MallocSize(p);
! 36945: sqlite3_mutex_enter(pcache1.mutex);
! 36946: sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
! 36947: sqlite3_mutex_leave(pcache1.mutex);
! 36948: }
! 36949: sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
! 36950: }
! 36951: return p;
! 36952: }
! 36953:
! 36954: /*
! 36955: ** Free an allocated buffer obtained from pcache1Alloc().
! 36956: */
! 36957: static int pcache1Free(void *p){
! 36958: int nFreed = 0;
! 36959: if( p==0 ) return 0;
! 36960: if( p>=pcache1.pStart && p<pcache1.pEnd ){
! 36961: PgFreeslot *pSlot;
! 36962: sqlite3_mutex_enter(pcache1.mutex);
! 36963: sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
! 36964: pSlot = (PgFreeslot*)p;
! 36965: pSlot->pNext = pcache1.pFree;
! 36966: pcache1.pFree = pSlot;
! 36967: pcache1.nFreeSlot++;
! 36968: pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
! 36969: assert( pcache1.nFreeSlot<=pcache1.nSlot );
! 36970: sqlite3_mutex_leave(pcache1.mutex);
! 36971: }else{
! 36972: assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
! 36973: sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
! 36974: nFreed = sqlite3MallocSize(p);
! 36975: sqlite3_mutex_enter(pcache1.mutex);
! 36976: sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
! 36977: sqlite3_mutex_leave(pcache1.mutex);
! 36978: sqlite3_free(p);
! 36979: }
! 36980: return nFreed;
! 36981: }
! 36982:
! 36983: #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
! 36984: /*
! 36985: ** Return the size of a pcache allocation
! 36986: */
! 36987: static int pcache1MemSize(void *p){
! 36988: if( p>=pcache1.pStart && p<pcache1.pEnd ){
! 36989: return pcache1.szSlot;
! 36990: }else{
! 36991: int iSize;
! 36992: assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
! 36993: sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
! 36994: iSize = sqlite3MallocSize(p);
! 36995: sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
! 36996: return iSize;
! 36997: }
! 36998: }
! 36999: #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
! 37000:
! 37001: /*
! 37002: ** Allocate a new page object initially associated with cache pCache.
! 37003: */
! 37004: static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
! 37005: PgHdr1 *p = 0;
! 37006: void *pPg;
! 37007:
! 37008: /* The group mutex must be released before pcache1Alloc() is called. This
! 37009: ** is because it may call sqlite3_release_memory(), which assumes that
! 37010: ** this mutex is not held. */
! 37011: assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
! 37012: pcache1LeaveMutex(pCache->pGroup);
! 37013: #ifdef SQLITE_PCACHE_SEPARATE_HEADER
! 37014: pPg = pcache1Alloc(pCache->szPage);
! 37015: p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
! 37016: if( !pPg || !p ){
! 37017: pcache1Free(pPg);
! 37018: sqlite3_free(p);
! 37019: pPg = 0;
! 37020: }
! 37021: #else
! 37022: pPg = pcache1Alloc(sizeof(PgHdr1) + pCache->szPage + pCache->szExtra);
! 37023: p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
! 37024: #endif
! 37025: pcache1EnterMutex(pCache->pGroup);
! 37026:
! 37027: if( pPg ){
! 37028: p->page.pBuf = pPg;
! 37029: p->page.pExtra = &p[1];
! 37030: if( pCache->bPurgeable ){
! 37031: pCache->pGroup->nCurrentPage++;
! 37032: }
! 37033: return p;
! 37034: }
! 37035: return 0;
! 37036: }
! 37037:
! 37038: /*
! 37039: ** Free a page object allocated by pcache1AllocPage().
! 37040: **
! 37041: ** The pointer is allowed to be NULL, which is prudent. But it turns out
! 37042: ** that the current implementation happens to never call this routine
! 37043: ** with a NULL pointer, so we mark the NULL test with ALWAYS().
! 37044: */
! 37045: static void pcache1FreePage(PgHdr1 *p){
! 37046: if( ALWAYS(p) ){
! 37047: PCache1 *pCache = p->pCache;
! 37048: assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
! 37049: pcache1Free(p->page.pBuf);
! 37050: #ifdef SQLITE_PCACHE_SEPARATE_HEADER
! 37051: sqlite3_free(p);
! 37052: #endif
! 37053: if( pCache->bPurgeable ){
! 37054: pCache->pGroup->nCurrentPage--;
! 37055: }
! 37056: }
! 37057: }
! 37058:
! 37059: /*
! 37060: ** Malloc function used by SQLite to obtain space from the buffer configured
! 37061: ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
! 37062: ** exists, this function falls back to sqlite3Malloc().
! 37063: */
! 37064: SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
! 37065: return pcache1Alloc(sz);
! 37066: }
! 37067:
! 37068: /*
! 37069: ** Free an allocated buffer obtained from sqlite3PageMalloc().
! 37070: */
! 37071: SQLITE_PRIVATE void sqlite3PageFree(void *p){
! 37072: pcache1Free(p);
! 37073: }
! 37074:
! 37075:
! 37076: /*
! 37077: ** Return true if it desirable to avoid allocating a new page cache
! 37078: ** entry.
! 37079: **
! 37080: ** If memory was allocated specifically to the page cache using
! 37081: ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
! 37082: ** it is desirable to avoid allocating a new page cache entry because
! 37083: ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
! 37084: ** for all page cache needs and we should not need to spill the
! 37085: ** allocation onto the heap.
! 37086: **
! 37087: ** Or, the heap is used for all page cache memory but the heap is
! 37088: ** under memory pressure, then again it is desirable to avoid
! 37089: ** allocating a new page cache entry in order to avoid stressing
! 37090: ** the heap even further.
! 37091: */
! 37092: static int pcache1UnderMemoryPressure(PCache1 *pCache){
! 37093: if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
! 37094: return pcache1.bUnderPressure;
! 37095: }else{
! 37096: return sqlite3HeapNearlyFull();
! 37097: }
! 37098: }
! 37099:
! 37100: /******************************************************************************/
! 37101: /******** General Implementation Functions ************************************/
! 37102:
! 37103: /*
! 37104: ** This function is used to resize the hash table used by the cache passed
! 37105: ** as the first argument.
! 37106: **
! 37107: ** The PCache mutex must be held when this function is called.
! 37108: */
! 37109: static int pcache1ResizeHash(PCache1 *p){
! 37110: PgHdr1 **apNew;
! 37111: unsigned int nNew;
! 37112: unsigned int i;
! 37113:
! 37114: assert( sqlite3_mutex_held(p->pGroup->mutex) );
! 37115:
! 37116: nNew = p->nHash*2;
! 37117: if( nNew<256 ){
! 37118: nNew = 256;
! 37119: }
! 37120:
! 37121: pcache1LeaveMutex(p->pGroup);
! 37122: if( p->nHash ){ sqlite3BeginBenignMalloc(); }
! 37123: apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
! 37124: if( p->nHash ){ sqlite3EndBenignMalloc(); }
! 37125: pcache1EnterMutex(p->pGroup);
! 37126: if( apNew ){
! 37127: memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
! 37128: for(i=0; i<p->nHash; i++){
! 37129: PgHdr1 *pPage;
! 37130: PgHdr1 *pNext = p->apHash[i];
! 37131: while( (pPage = pNext)!=0 ){
! 37132: unsigned int h = pPage->iKey % nNew;
! 37133: pNext = pPage->pNext;
! 37134: pPage->pNext = apNew[h];
! 37135: apNew[h] = pPage;
! 37136: }
! 37137: }
! 37138: sqlite3_free(p->apHash);
! 37139: p->apHash = apNew;
! 37140: p->nHash = nNew;
! 37141: }
! 37142:
! 37143: return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
! 37144: }
! 37145:
! 37146: /*
! 37147: ** This function is used internally to remove the page pPage from the
! 37148: ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
! 37149: ** LRU list, then this function is a no-op.
! 37150: **
! 37151: ** The PGroup mutex must be held when this function is called.
! 37152: **
! 37153: ** If pPage is NULL then this routine is a no-op.
! 37154: */
! 37155: static void pcache1PinPage(PgHdr1 *pPage){
! 37156: PCache1 *pCache;
! 37157: PGroup *pGroup;
! 37158:
! 37159: if( pPage==0 ) return;
! 37160: pCache = pPage->pCache;
! 37161: pGroup = pCache->pGroup;
! 37162: assert( sqlite3_mutex_held(pGroup->mutex) );
! 37163: if( pPage->pLruNext || pPage==pGroup->pLruTail ){
! 37164: if( pPage->pLruPrev ){
! 37165: pPage->pLruPrev->pLruNext = pPage->pLruNext;
! 37166: }
! 37167: if( pPage->pLruNext ){
! 37168: pPage->pLruNext->pLruPrev = pPage->pLruPrev;
! 37169: }
! 37170: if( pGroup->pLruHead==pPage ){
! 37171: pGroup->pLruHead = pPage->pLruNext;
! 37172: }
! 37173: if( pGroup->pLruTail==pPage ){
! 37174: pGroup->pLruTail = pPage->pLruPrev;
! 37175: }
! 37176: pPage->pLruNext = 0;
! 37177: pPage->pLruPrev = 0;
! 37178: pPage->pCache->nRecyclable--;
! 37179: }
! 37180: }
! 37181:
! 37182:
! 37183: /*
! 37184: ** Remove the page supplied as an argument from the hash table
! 37185: ** (PCache1.apHash structure) that it is currently stored in.
! 37186: **
! 37187: ** The PGroup mutex must be held when this function is called.
! 37188: */
! 37189: static void pcache1RemoveFromHash(PgHdr1 *pPage){
! 37190: unsigned int h;
! 37191: PCache1 *pCache = pPage->pCache;
! 37192: PgHdr1 **pp;
! 37193:
! 37194: assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
! 37195: h = pPage->iKey % pCache->nHash;
! 37196: for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
! 37197: *pp = (*pp)->pNext;
! 37198:
! 37199: pCache->nPage--;
! 37200: }
! 37201:
! 37202: /*
! 37203: ** If there are currently more than nMaxPage pages allocated, try
! 37204: ** to recycle pages to reduce the number allocated to nMaxPage.
! 37205: */
! 37206: static void pcache1EnforceMaxPage(PGroup *pGroup){
! 37207: assert( sqlite3_mutex_held(pGroup->mutex) );
! 37208: while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
! 37209: PgHdr1 *p = pGroup->pLruTail;
! 37210: assert( p->pCache->pGroup==pGroup );
! 37211: pcache1PinPage(p);
! 37212: pcache1RemoveFromHash(p);
! 37213: pcache1FreePage(p);
! 37214: }
! 37215: }
! 37216:
! 37217: /*
! 37218: ** Discard all pages from cache pCache with a page number (key value)
! 37219: ** greater than or equal to iLimit. Any pinned pages that meet this
! 37220: ** criteria are unpinned before they are discarded.
! 37221: **
! 37222: ** The PCache mutex must be held when this function is called.
! 37223: */
! 37224: static void pcache1TruncateUnsafe(
! 37225: PCache1 *pCache, /* The cache to truncate */
! 37226: unsigned int iLimit /* Drop pages with this pgno or larger */
! 37227: ){
! 37228: TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */
! 37229: unsigned int h;
! 37230: assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
! 37231: for(h=0; h<pCache->nHash; h++){
! 37232: PgHdr1 **pp = &pCache->apHash[h];
! 37233: PgHdr1 *pPage;
! 37234: while( (pPage = *pp)!=0 ){
! 37235: if( pPage->iKey>=iLimit ){
! 37236: pCache->nPage--;
! 37237: *pp = pPage->pNext;
! 37238: pcache1PinPage(pPage);
! 37239: pcache1FreePage(pPage);
! 37240: }else{
! 37241: pp = &pPage->pNext;
! 37242: TESTONLY( nPage++; )
! 37243: }
! 37244: }
! 37245: }
! 37246: assert( pCache->nPage==nPage );
! 37247: }
! 37248:
! 37249: /******************************************************************************/
! 37250: /******** sqlite3_pcache Methods **********************************************/
! 37251:
! 37252: /*
! 37253: ** Implementation of the sqlite3_pcache.xInit method.
! 37254: */
! 37255: static int pcache1Init(void *NotUsed){
! 37256: UNUSED_PARAMETER(NotUsed);
! 37257: assert( pcache1.isInit==0 );
! 37258: memset(&pcache1, 0, sizeof(pcache1));
! 37259: if( sqlite3GlobalConfig.bCoreMutex ){
! 37260: pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
! 37261: pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
! 37262: }
! 37263: pcache1.grp.mxPinned = 10;
! 37264: pcache1.isInit = 1;
! 37265: return SQLITE_OK;
! 37266: }
! 37267:
! 37268: /*
! 37269: ** Implementation of the sqlite3_pcache.xShutdown method.
! 37270: ** Note that the static mutex allocated in xInit does
! 37271: ** not need to be freed.
! 37272: */
! 37273: static void pcache1Shutdown(void *NotUsed){
! 37274: UNUSED_PARAMETER(NotUsed);
! 37275: assert( pcache1.isInit!=0 );
! 37276: memset(&pcache1, 0, sizeof(pcache1));
! 37277: }
! 37278:
! 37279: /*
! 37280: ** Implementation of the sqlite3_pcache.xCreate method.
! 37281: **
! 37282: ** Allocate a new cache.
! 37283: */
! 37284: static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
! 37285: PCache1 *pCache; /* The newly created page cache */
! 37286: PGroup *pGroup; /* The group the new page cache will belong to */
! 37287: int sz; /* Bytes of memory required to allocate the new cache */
! 37288:
! 37289: /*
! 37290: ** The seperateCache variable is true if each PCache has its own private
! 37291: ** PGroup. In other words, separateCache is true for mode (1) where no
! 37292: ** mutexing is required.
! 37293: **
! 37294: ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
! 37295: **
! 37296: ** * Always use a unified cache in single-threaded applications
! 37297: **
! 37298: ** * Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
! 37299: ** use separate caches (mode-1)
! 37300: */
! 37301: #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
! 37302: const int separateCache = 0;
! 37303: #else
! 37304: int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
! 37305: #endif
! 37306:
! 37307: assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
! 37308: assert( szExtra < 300 );
! 37309:
! 37310: sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
! 37311: pCache = (PCache1 *)sqlite3_malloc(sz);
! 37312: if( pCache ){
! 37313: memset(pCache, 0, sz);
! 37314: if( separateCache ){
! 37315: pGroup = (PGroup*)&pCache[1];
! 37316: pGroup->mxPinned = 10;
! 37317: }else{
! 37318: pGroup = &pcache1.grp;
! 37319: }
! 37320: pCache->pGroup = pGroup;
! 37321: pCache->szPage = szPage;
! 37322: pCache->szExtra = szExtra;
! 37323: pCache->bPurgeable = (bPurgeable ? 1 : 0);
! 37324: if( bPurgeable ){
! 37325: pCache->nMin = 10;
! 37326: pcache1EnterMutex(pGroup);
! 37327: pGroup->nMinPage += pCache->nMin;
! 37328: pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
! 37329: pcache1LeaveMutex(pGroup);
! 37330: }
! 37331: }
! 37332: return (sqlite3_pcache *)pCache;
! 37333: }
! 37334:
! 37335: /*
! 37336: ** Implementation of the sqlite3_pcache.xCachesize method.
! 37337: **
! 37338: ** Configure the cache_size limit for a cache.
! 37339: */
! 37340: static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
! 37341: PCache1 *pCache = (PCache1 *)p;
! 37342: if( pCache->bPurgeable ){
! 37343: PGroup *pGroup = pCache->pGroup;
! 37344: pcache1EnterMutex(pGroup);
! 37345: pGroup->nMaxPage += (nMax - pCache->nMax);
! 37346: pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
! 37347: pCache->nMax = nMax;
! 37348: pCache->n90pct = pCache->nMax*9/10;
! 37349: pcache1EnforceMaxPage(pGroup);
! 37350: pcache1LeaveMutex(pGroup);
! 37351: }
! 37352: }
! 37353:
! 37354: /*
! 37355: ** Implementation of the sqlite3_pcache.xShrink method.
! 37356: **
! 37357: ** Free up as much memory as possible.
! 37358: */
! 37359: static void pcache1Shrink(sqlite3_pcache *p){
! 37360: PCache1 *pCache = (PCache1*)p;
! 37361: if( pCache->bPurgeable ){
! 37362: PGroup *pGroup = pCache->pGroup;
! 37363: int savedMaxPage;
! 37364: pcache1EnterMutex(pGroup);
! 37365: savedMaxPage = pGroup->nMaxPage;
! 37366: pGroup->nMaxPage = 0;
! 37367: pcache1EnforceMaxPage(pGroup);
! 37368: pGroup->nMaxPage = savedMaxPage;
! 37369: pcache1LeaveMutex(pGroup);
! 37370: }
! 37371: }
! 37372:
! 37373: /*
! 37374: ** Implementation of the sqlite3_pcache.xPagecount method.
! 37375: */
! 37376: static int pcache1Pagecount(sqlite3_pcache *p){
! 37377: int n;
! 37378: PCache1 *pCache = (PCache1*)p;
! 37379: pcache1EnterMutex(pCache->pGroup);
! 37380: n = pCache->nPage;
! 37381: pcache1LeaveMutex(pCache->pGroup);
! 37382: return n;
! 37383: }
! 37384:
! 37385: /*
! 37386: ** Implementation of the sqlite3_pcache.xFetch method.
! 37387: **
! 37388: ** Fetch a page by key value.
! 37389: **
! 37390: ** Whether or not a new page may be allocated by this function depends on
! 37391: ** the value of the createFlag argument. 0 means do not allocate a new
! 37392: ** page. 1 means allocate a new page if space is easily available. 2
! 37393: ** means to try really hard to allocate a new page.
! 37394: **
! 37395: ** For a non-purgeable cache (a cache used as the storage for an in-memory
! 37396: ** database) there is really no difference between createFlag 1 and 2. So
! 37397: ** the calling function (pcache.c) will never have a createFlag of 1 on
! 37398: ** a non-purgeable cache.
! 37399: **
! 37400: ** There are three different approaches to obtaining space for a page,
! 37401: ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
! 37402: **
! 37403: ** 1. Regardless of the value of createFlag, the cache is searched for a
! 37404: ** copy of the requested page. If one is found, it is returned.
! 37405: **
! 37406: ** 2. If createFlag==0 and the page is not already in the cache, NULL is
! 37407: ** returned.
! 37408: **
! 37409: ** 3. If createFlag is 1, and the page is not already in the cache, then
! 37410: ** return NULL (do not allocate a new page) if any of the following
! 37411: ** conditions are true:
! 37412: **
! 37413: ** (a) the number of pages pinned by the cache is greater than
! 37414: ** PCache1.nMax, or
! 37415: **
! 37416: ** (b) the number of pages pinned by the cache is greater than
! 37417: ** the sum of nMax for all purgeable caches, less the sum of
! 37418: ** nMin for all other purgeable caches, or
! 37419: **
! 37420: ** 4. If none of the first three conditions apply and the cache is marked
! 37421: ** as purgeable, and if one of the following is true:
! 37422: **
! 37423: ** (a) The number of pages allocated for the cache is already
! 37424: ** PCache1.nMax, or
! 37425: **
! 37426: ** (b) The number of pages allocated for all purgeable caches is
! 37427: ** already equal to or greater than the sum of nMax for all
! 37428: ** purgeable caches,
! 37429: **
! 37430: ** (c) The system is under memory pressure and wants to avoid
! 37431: ** unnecessary pages cache entry allocations
! 37432: **
! 37433: ** then attempt to recycle a page from the LRU list. If it is the right
! 37434: ** size, return the recycled buffer. Otherwise, free the buffer and
! 37435: ** proceed to step 5.
! 37436: **
! 37437: ** 5. Otherwise, allocate and return a new page buffer.
! 37438: */
! 37439: static sqlite3_pcache_page *pcache1Fetch(
! 37440: sqlite3_pcache *p,
! 37441: unsigned int iKey,
! 37442: int createFlag
! 37443: ){
! 37444: unsigned int nPinned;
! 37445: PCache1 *pCache = (PCache1 *)p;
! 37446: PGroup *pGroup;
! 37447: PgHdr1 *pPage = 0;
! 37448:
! 37449: assert( pCache->bPurgeable || createFlag!=1 );
! 37450: assert( pCache->bPurgeable || pCache->nMin==0 );
! 37451: assert( pCache->bPurgeable==0 || pCache->nMin==10 );
! 37452: assert( pCache->nMin==0 || pCache->bPurgeable );
! 37453: pcache1EnterMutex(pGroup = pCache->pGroup);
! 37454:
! 37455: /* Step 1: Search the hash table for an existing entry. */
! 37456: if( pCache->nHash>0 ){
! 37457: unsigned int h = iKey % pCache->nHash;
! 37458: for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
! 37459: }
! 37460:
! 37461: /* Step 2: Abort if no existing page is found and createFlag is 0 */
! 37462: if( pPage || createFlag==0 ){
! 37463: pcache1PinPage(pPage);
! 37464: goto fetch_out;
! 37465: }
! 37466:
! 37467: /* The pGroup local variable will normally be initialized by the
! 37468: ** pcache1EnterMutex() macro above. But if SQLITE_MUTEX_OMIT is defined,
! 37469: ** then pcache1EnterMutex() is a no-op, so we have to initialize the
! 37470: ** local variable here. Delaying the initialization of pGroup is an
! 37471: ** optimization: The common case is to exit the module before reaching
! 37472: ** this point.
! 37473: */
! 37474: #ifdef SQLITE_MUTEX_OMIT
! 37475: pGroup = pCache->pGroup;
! 37476: #endif
! 37477:
! 37478: /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
! 37479: assert( pCache->nPage >= pCache->nRecyclable );
! 37480: nPinned = pCache->nPage - pCache->nRecyclable;
! 37481: assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
! 37482: assert( pCache->n90pct == pCache->nMax*9/10 );
! 37483: if( createFlag==1 && (
! 37484: nPinned>=pGroup->mxPinned
! 37485: || nPinned>=pCache->n90pct
! 37486: || pcache1UnderMemoryPressure(pCache)
! 37487: )){
! 37488: goto fetch_out;
! 37489: }
! 37490:
! 37491: if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
! 37492: goto fetch_out;
! 37493: }
! 37494:
! 37495: /* Step 4. Try to recycle a page. */
! 37496: if( pCache->bPurgeable && pGroup->pLruTail && (
! 37497: (pCache->nPage+1>=pCache->nMax)
! 37498: || pGroup->nCurrentPage>=pGroup->nMaxPage
! 37499: || pcache1UnderMemoryPressure(pCache)
! 37500: )){
! 37501: PCache1 *pOther;
! 37502: pPage = pGroup->pLruTail;
! 37503: pcache1RemoveFromHash(pPage);
! 37504: pcache1PinPage(pPage);
! 37505: pOther = pPage->pCache;
! 37506:
! 37507: /* We want to verify that szPage and szExtra are the same for pOther
! 37508: ** and pCache. Assert that we can verify this by comparing sums. */
! 37509: assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
! 37510: assert( pCache->szExtra<512 );
! 37511: assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
! 37512: assert( pOther->szExtra<512 );
! 37513:
! 37514: if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
! 37515: pcache1FreePage(pPage);
! 37516: pPage = 0;
! 37517: }else{
! 37518: pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
! 37519: }
! 37520: }
! 37521:
! 37522: /* Step 5. If a usable page buffer has still not been found,
! 37523: ** attempt to allocate a new one.
! 37524: */
! 37525: if( !pPage ){
! 37526: if( createFlag==1 ) sqlite3BeginBenignMalloc();
! 37527: pPage = pcache1AllocPage(pCache);
! 37528: if( createFlag==1 ) sqlite3EndBenignMalloc();
! 37529: }
! 37530:
! 37531: if( pPage ){
! 37532: unsigned int h = iKey % pCache->nHash;
! 37533: pCache->nPage++;
! 37534: pPage->iKey = iKey;
! 37535: pPage->pNext = pCache->apHash[h];
! 37536: pPage->pCache = pCache;
! 37537: pPage->pLruPrev = 0;
! 37538: pPage->pLruNext = 0;
! 37539: *(void **)pPage->page.pExtra = 0;
! 37540: pCache->apHash[h] = pPage;
! 37541: }
! 37542:
! 37543: fetch_out:
! 37544: if( pPage && iKey>pCache->iMaxKey ){
! 37545: pCache->iMaxKey = iKey;
! 37546: }
! 37547: pcache1LeaveMutex(pGroup);
! 37548: return &pPage->page;
! 37549: }
! 37550:
! 37551:
! 37552: /*
! 37553: ** Implementation of the sqlite3_pcache.xUnpin method.
! 37554: **
! 37555: ** Mark a page as unpinned (eligible for asynchronous recycling).
! 37556: */
! 37557: static void pcache1Unpin(
! 37558: sqlite3_pcache *p,
! 37559: sqlite3_pcache_page *pPg,
! 37560: int reuseUnlikely
! 37561: ){
! 37562: PCache1 *pCache = (PCache1 *)p;
! 37563: PgHdr1 *pPage = (PgHdr1 *)pPg;
! 37564: PGroup *pGroup = pCache->pGroup;
! 37565:
! 37566: assert( pPage->pCache==pCache );
! 37567: pcache1EnterMutex(pGroup);
! 37568:
! 37569: /* It is an error to call this function if the page is already
! 37570: ** part of the PGroup LRU list.
! 37571: */
! 37572: assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
! 37573: assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
! 37574:
! 37575: if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
! 37576: pcache1RemoveFromHash(pPage);
! 37577: pcache1FreePage(pPage);
! 37578: }else{
! 37579: /* Add the page to the PGroup LRU list. */
! 37580: if( pGroup->pLruHead ){
! 37581: pGroup->pLruHead->pLruPrev = pPage;
! 37582: pPage->pLruNext = pGroup->pLruHead;
! 37583: pGroup->pLruHead = pPage;
! 37584: }else{
! 37585: pGroup->pLruTail = pPage;
! 37586: pGroup->pLruHead = pPage;
! 37587: }
! 37588: pCache->nRecyclable++;
! 37589: }
! 37590:
! 37591: pcache1LeaveMutex(pCache->pGroup);
! 37592: }
! 37593:
! 37594: /*
! 37595: ** Implementation of the sqlite3_pcache.xRekey method.
! 37596: */
! 37597: static void pcache1Rekey(
! 37598: sqlite3_pcache *p,
! 37599: sqlite3_pcache_page *pPg,
! 37600: unsigned int iOld,
! 37601: unsigned int iNew
! 37602: ){
! 37603: PCache1 *pCache = (PCache1 *)p;
! 37604: PgHdr1 *pPage = (PgHdr1 *)pPg;
! 37605: PgHdr1 **pp;
! 37606: unsigned int h;
! 37607: assert( pPage->iKey==iOld );
! 37608: assert( pPage->pCache==pCache );
! 37609:
! 37610: pcache1EnterMutex(pCache->pGroup);
! 37611:
! 37612: h = iOld%pCache->nHash;
! 37613: pp = &pCache->apHash[h];
! 37614: while( (*pp)!=pPage ){
! 37615: pp = &(*pp)->pNext;
! 37616: }
! 37617: *pp = pPage->pNext;
! 37618:
! 37619: h = iNew%pCache->nHash;
! 37620: pPage->iKey = iNew;
! 37621: pPage->pNext = pCache->apHash[h];
! 37622: pCache->apHash[h] = pPage;
! 37623: if( iNew>pCache->iMaxKey ){
! 37624: pCache->iMaxKey = iNew;
! 37625: }
! 37626:
! 37627: pcache1LeaveMutex(pCache->pGroup);
! 37628: }
! 37629:
! 37630: /*
! 37631: ** Implementation of the sqlite3_pcache.xTruncate method.
! 37632: **
! 37633: ** Discard all unpinned pages in the cache with a page number equal to
! 37634: ** or greater than parameter iLimit. Any pinned pages with a page number
! 37635: ** equal to or greater than iLimit are implicitly unpinned.
! 37636: */
! 37637: static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
! 37638: PCache1 *pCache = (PCache1 *)p;
! 37639: pcache1EnterMutex(pCache->pGroup);
! 37640: if( iLimit<=pCache->iMaxKey ){
! 37641: pcache1TruncateUnsafe(pCache, iLimit);
! 37642: pCache->iMaxKey = iLimit-1;
! 37643: }
! 37644: pcache1LeaveMutex(pCache->pGroup);
! 37645: }
! 37646:
! 37647: /*
! 37648: ** Implementation of the sqlite3_pcache.xDestroy method.
! 37649: **
! 37650: ** Destroy a cache allocated using pcache1Create().
! 37651: */
! 37652: static void pcache1Destroy(sqlite3_pcache *p){
! 37653: PCache1 *pCache = (PCache1 *)p;
! 37654: PGroup *pGroup = pCache->pGroup;
! 37655: assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
! 37656: pcache1EnterMutex(pGroup);
! 37657: pcache1TruncateUnsafe(pCache, 0);
! 37658: assert( pGroup->nMaxPage >= pCache->nMax );
! 37659: pGroup->nMaxPage -= pCache->nMax;
! 37660: assert( pGroup->nMinPage >= pCache->nMin );
! 37661: pGroup->nMinPage -= pCache->nMin;
! 37662: pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
! 37663: pcache1EnforceMaxPage(pGroup);
! 37664: pcache1LeaveMutex(pGroup);
! 37665: sqlite3_free(pCache->apHash);
! 37666: sqlite3_free(pCache);
! 37667: }
! 37668:
! 37669: /*
! 37670: ** This function is called during initialization (sqlite3_initialize()) to
! 37671: ** install the default pluggable cache module, assuming the user has not
! 37672: ** already provided an alternative.
! 37673: */
! 37674: SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
! 37675: static const sqlite3_pcache_methods2 defaultMethods = {
! 37676: 1, /* iVersion */
! 37677: 0, /* pArg */
! 37678: pcache1Init, /* xInit */
! 37679: pcache1Shutdown, /* xShutdown */
! 37680: pcache1Create, /* xCreate */
! 37681: pcache1Cachesize, /* xCachesize */
! 37682: pcache1Pagecount, /* xPagecount */
! 37683: pcache1Fetch, /* xFetch */
! 37684: pcache1Unpin, /* xUnpin */
! 37685: pcache1Rekey, /* xRekey */
! 37686: pcache1Truncate, /* xTruncate */
! 37687: pcache1Destroy, /* xDestroy */
! 37688: pcache1Shrink /* xShrink */
! 37689: };
! 37690: sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
! 37691: }
! 37692:
! 37693: #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
! 37694: /*
! 37695: ** This function is called to free superfluous dynamically allocated memory
! 37696: ** held by the pager system. Memory in use by any SQLite pager allocated
! 37697: ** by the current thread may be sqlite3_free()ed.
! 37698: **
! 37699: ** nReq is the number of bytes of memory required. Once this much has
! 37700: ** been released, the function returns. The return value is the total number
! 37701: ** of bytes of memory released.
! 37702: */
! 37703: SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
! 37704: int nFree = 0;
! 37705: assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
! 37706: assert( sqlite3_mutex_notheld(pcache1.mutex) );
! 37707: if( pcache1.pStart==0 ){
! 37708: PgHdr1 *p;
! 37709: pcache1EnterMutex(&pcache1.grp);
! 37710: while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
! 37711: nFree += pcache1MemSize(p->page.pBuf);
! 37712: #ifdef SQLITE_PCACHE_SEPARATE_HEADER
! 37713: nFree += sqlite3MemSize(p);
! 37714: #endif
! 37715: pcache1PinPage(p);
! 37716: pcache1RemoveFromHash(p);
! 37717: pcache1FreePage(p);
! 37718: }
! 37719: pcache1LeaveMutex(&pcache1.grp);
! 37720: }
! 37721: return nFree;
! 37722: }
! 37723: #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
! 37724:
! 37725: #ifdef SQLITE_TEST
! 37726: /*
! 37727: ** This function is used by test procedures to inspect the internal state
! 37728: ** of the global cache.
! 37729: */
! 37730: SQLITE_PRIVATE void sqlite3PcacheStats(
! 37731: int *pnCurrent, /* OUT: Total number of pages cached */
! 37732: int *pnMax, /* OUT: Global maximum cache size */
! 37733: int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
! 37734: int *pnRecyclable /* OUT: Total number of pages available for recycling */
! 37735: ){
! 37736: PgHdr1 *p;
! 37737: int nRecyclable = 0;
! 37738: for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
! 37739: nRecyclable++;
! 37740: }
! 37741: *pnCurrent = pcache1.grp.nCurrentPage;
! 37742: *pnMax = (int)pcache1.grp.nMaxPage;
! 37743: *pnMin = (int)pcache1.grp.nMinPage;
! 37744: *pnRecyclable = nRecyclable;
! 37745: }
! 37746: #endif
! 37747:
! 37748: /************** End of pcache1.c *********************************************/
! 37749: /************** Begin file rowset.c ******************************************/
! 37750: /*
! 37751: ** 2008 December 3
! 37752: **
! 37753: ** The author disclaims copyright to this source code. In place of
! 37754: ** a legal notice, here is a blessing:
! 37755: **
! 37756: ** May you do good and not evil.
! 37757: ** May you find forgiveness for yourself and forgive others.
! 37758: ** May you share freely, never taking more than you give.
! 37759: **
! 37760: *************************************************************************
! 37761: **
! 37762: ** This module implements an object we call a "RowSet".
! 37763: **
! 37764: ** The RowSet object is a collection of rowids. Rowids
! 37765: ** are inserted into the RowSet in an arbitrary order. Inserts
! 37766: ** can be intermixed with tests to see if a given rowid has been
! 37767: ** previously inserted into the RowSet.
! 37768: **
! 37769: ** After all inserts are finished, it is possible to extract the
! 37770: ** elements of the RowSet in sorted order. Once this extraction
! 37771: ** process has started, no new elements may be inserted.
! 37772: **
! 37773: ** Hence, the primitive operations for a RowSet are:
! 37774: **
! 37775: ** CREATE
! 37776: ** INSERT
! 37777: ** TEST
! 37778: ** SMALLEST
! 37779: ** DESTROY
! 37780: **
! 37781: ** The CREATE and DESTROY primitives are the constructor and destructor,
! 37782: ** obviously. The INSERT primitive adds a new element to the RowSet.
! 37783: ** TEST checks to see if an element is already in the RowSet. SMALLEST
! 37784: ** extracts the least value from the RowSet.
! 37785: **
! 37786: ** The INSERT primitive might allocate additional memory. Memory is
! 37787: ** allocated in chunks so most INSERTs do no allocation. There is an
! 37788: ** upper bound on the size of allocated memory. No memory is freed
! 37789: ** until DESTROY.
! 37790: **
! 37791: ** The TEST primitive includes a "batch" number. The TEST primitive
! 37792: ** will only see elements that were inserted before the last change
! 37793: ** in the batch number. In other words, if an INSERT occurs between
! 37794: ** two TESTs where the TESTs have the same batch nubmer, then the
! 37795: ** value added by the INSERT will not be visible to the second TEST.
! 37796: ** The initial batch number is zero, so if the very first TEST contains
! 37797: ** a non-zero batch number, it will see all prior INSERTs.
! 37798: **
! 37799: ** No INSERTs may occurs after a SMALLEST. An assertion will fail if
! 37800: ** that is attempted.
! 37801: **
! 37802: ** The cost of an INSERT is roughly constant. (Sometime new memory
! 37803: ** has to be allocated on an INSERT.) The cost of a TEST with a new
! 37804: ** batch number is O(NlogN) where N is the number of elements in the RowSet.
! 37805: ** The cost of a TEST using the same batch number is O(logN). The cost
! 37806: ** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST
! 37807: ** primitives are constant time. The cost of DESTROY is O(N).
! 37808: **
! 37809: ** There is an added cost of O(N) when switching between TEST and
! 37810: ** SMALLEST primitives.
! 37811: */
! 37812:
! 37813:
! 37814: /*
! 37815: ** Target size for allocation chunks.
! 37816: */
! 37817: #define ROWSET_ALLOCATION_SIZE 1024
! 37818:
! 37819: /*
! 37820: ** The number of rowset entries per allocation chunk.
! 37821: */
! 37822: #define ROWSET_ENTRY_PER_CHUNK \
! 37823: ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
! 37824:
! 37825: /*
! 37826: ** Each entry in a RowSet is an instance of the following object.
! 37827: */
! 37828: struct RowSetEntry {
! 37829: i64 v; /* ROWID value for this entry */
! 37830: struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
! 37831: struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
! 37832: };
! 37833:
! 37834: /*
! 37835: ** RowSetEntry objects are allocated in large chunks (instances of the
! 37836: ** following structure) to reduce memory allocation overhead. The
! 37837: ** chunks are kept on a linked list so that they can be deallocated
! 37838: ** when the RowSet is destroyed.
! 37839: */
! 37840: struct RowSetChunk {
! 37841: struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */
! 37842: struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
! 37843: };
! 37844:
! 37845: /*
! 37846: ** A RowSet in an instance of the following structure.
! 37847: **
! 37848: ** A typedef of this structure if found in sqliteInt.h.
! 37849: */
! 37850: struct RowSet {
! 37851: struct RowSetChunk *pChunk; /* List of all chunk allocations */
! 37852: sqlite3 *db; /* The database connection */
! 37853: struct RowSetEntry *pEntry; /* List of entries using pRight */
! 37854: struct RowSetEntry *pLast; /* Last entry on the pEntry list */
! 37855: struct RowSetEntry *pFresh; /* Source of new entry objects */
! 37856: struct RowSetEntry *pTree; /* Binary tree of entries */
! 37857: u16 nFresh; /* Number of objects on pFresh */
! 37858: u8 isSorted; /* True if pEntry is sorted */
! 37859: u8 iBatch; /* Current insert batch */
! 37860: };
! 37861:
! 37862: /*
! 37863: ** Turn bulk memory into a RowSet object. N bytes of memory
! 37864: ** are available at pSpace. The db pointer is used as a memory context
! 37865: ** for any subsequent allocations that need to occur.
! 37866: ** Return a pointer to the new RowSet object.
! 37867: **
! 37868: ** It must be the case that N is sufficient to make a Rowset. If not
! 37869: ** an assertion fault occurs.
! 37870: **
! 37871: ** If N is larger than the minimum, use the surplus as an initial
! 37872: ** allocation of entries available to be filled.
! 37873: */
! 37874: SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
! 37875: RowSet *p;
! 37876: assert( N >= ROUND8(sizeof(*p)) );
! 37877: p = pSpace;
! 37878: p->pChunk = 0;
! 37879: p->db = db;
! 37880: p->pEntry = 0;
! 37881: p->pLast = 0;
! 37882: p->pTree = 0;
! 37883: p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
! 37884: p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
! 37885: p->isSorted = 1;
! 37886: p->iBatch = 0;
! 37887: return p;
! 37888: }
! 37889:
! 37890: /*
! 37891: ** Deallocate all chunks from a RowSet. This frees all memory that
! 37892: ** the RowSet has allocated over its lifetime. This routine is
! 37893: ** the destructor for the RowSet.
! 37894: */
! 37895: SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
! 37896: struct RowSetChunk *pChunk, *pNextChunk;
! 37897: for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
! 37898: pNextChunk = pChunk->pNextChunk;
! 37899: sqlite3DbFree(p->db, pChunk);
! 37900: }
! 37901: p->pChunk = 0;
! 37902: p->nFresh = 0;
! 37903: p->pEntry = 0;
! 37904: p->pLast = 0;
! 37905: p->pTree = 0;
! 37906: p->isSorted = 1;
! 37907: }
! 37908:
! 37909: /*
! 37910: ** Insert a new value into a RowSet.
! 37911: **
! 37912: ** The mallocFailed flag of the database connection is set if a
! 37913: ** memory allocation fails.
! 37914: */
! 37915: SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
! 37916: struct RowSetEntry *pEntry; /* The new entry */
! 37917: struct RowSetEntry *pLast; /* The last prior entry */
! 37918: assert( p!=0 );
! 37919: if( p->nFresh==0 ){
! 37920: struct RowSetChunk *pNew;
! 37921: pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
! 37922: if( pNew==0 ){
! 37923: return;
! 37924: }
! 37925: pNew->pNextChunk = p->pChunk;
! 37926: p->pChunk = pNew;
! 37927: p->pFresh = pNew->aEntry;
! 37928: p->nFresh = ROWSET_ENTRY_PER_CHUNK;
! 37929: }
! 37930: pEntry = p->pFresh++;
! 37931: p->nFresh--;
! 37932: pEntry->v = rowid;
! 37933: pEntry->pRight = 0;
! 37934: pLast = p->pLast;
! 37935: if( pLast ){
! 37936: if( p->isSorted && rowid<=pLast->v ){
! 37937: p->isSorted = 0;
! 37938: }
! 37939: pLast->pRight = pEntry;
! 37940: }else{
! 37941: assert( p->pEntry==0 ); /* Fires if INSERT after SMALLEST */
! 37942: p->pEntry = pEntry;
! 37943: }
! 37944: p->pLast = pEntry;
! 37945: }
! 37946:
! 37947: /*
! 37948: ** Merge two lists of RowSetEntry objects. Remove duplicates.
! 37949: **
! 37950: ** The input lists are connected via pRight pointers and are
! 37951: ** assumed to each already be in sorted order.
! 37952: */
! 37953: static struct RowSetEntry *rowSetMerge(
! 37954: struct RowSetEntry *pA, /* First sorted list to be merged */
! 37955: struct RowSetEntry *pB /* Second sorted list to be merged */
! 37956: ){
! 37957: struct RowSetEntry head;
! 37958: struct RowSetEntry *pTail;
! 37959:
! 37960: pTail = &head;
! 37961: while( pA && pB ){
! 37962: assert( pA->pRight==0 || pA->v<=pA->pRight->v );
! 37963: assert( pB->pRight==0 || pB->v<=pB->pRight->v );
! 37964: if( pA->v<pB->v ){
! 37965: pTail->pRight = pA;
! 37966: pA = pA->pRight;
! 37967: pTail = pTail->pRight;
! 37968: }else if( pB->v<pA->v ){
! 37969: pTail->pRight = pB;
! 37970: pB = pB->pRight;
! 37971: pTail = pTail->pRight;
! 37972: }else{
! 37973: pA = pA->pRight;
! 37974: }
! 37975: }
! 37976: if( pA ){
! 37977: assert( pA->pRight==0 || pA->v<=pA->pRight->v );
! 37978: pTail->pRight = pA;
! 37979: }else{
! 37980: assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
! 37981: pTail->pRight = pB;
! 37982: }
! 37983: return head.pRight;
! 37984: }
! 37985:
! 37986: /*
! 37987: ** Sort all elements on the pEntry list of the RowSet into ascending order.
! 37988: */
! 37989: static void rowSetSort(RowSet *p){
! 37990: unsigned int i;
! 37991: struct RowSetEntry *pEntry;
! 37992: struct RowSetEntry *aBucket[40];
! 37993:
! 37994: assert( p->isSorted==0 );
! 37995: memset(aBucket, 0, sizeof(aBucket));
! 37996: while( p->pEntry ){
! 37997: pEntry = p->pEntry;
! 37998: p->pEntry = pEntry->pRight;
! 37999: pEntry->pRight = 0;
! 38000: for(i=0; aBucket[i]; i++){
! 38001: pEntry = rowSetMerge(aBucket[i], pEntry);
! 38002: aBucket[i] = 0;
! 38003: }
! 38004: aBucket[i] = pEntry;
! 38005: }
! 38006: pEntry = 0;
! 38007: for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
! 38008: pEntry = rowSetMerge(pEntry, aBucket[i]);
! 38009: }
! 38010: p->pEntry = pEntry;
! 38011: p->pLast = 0;
! 38012: p->isSorted = 1;
! 38013: }
! 38014:
! 38015:
! 38016: /*
! 38017: ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
! 38018: ** Convert this tree into a linked list connected by the pRight pointers
! 38019: ** and return pointers to the first and last elements of the new list.
! 38020: */
! 38021: static void rowSetTreeToList(
! 38022: struct RowSetEntry *pIn, /* Root of the input tree */
! 38023: struct RowSetEntry **ppFirst, /* Write head of the output list here */
! 38024: struct RowSetEntry **ppLast /* Write tail of the output list here */
! 38025: ){
! 38026: assert( pIn!=0 );
! 38027: if( pIn->pLeft ){
! 38028: struct RowSetEntry *p;
! 38029: rowSetTreeToList(pIn->pLeft, ppFirst, &p);
! 38030: p->pRight = pIn;
! 38031: }else{
! 38032: *ppFirst = pIn;
! 38033: }
! 38034: if( pIn->pRight ){
! 38035: rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
! 38036: }else{
! 38037: *ppLast = pIn;
! 38038: }
! 38039: assert( (*ppLast)->pRight==0 );
! 38040: }
! 38041:
! 38042:
! 38043: /*
! 38044: ** Convert a sorted list of elements (connected by pRight) into a binary
! 38045: ** tree with depth of iDepth. A depth of 1 means the tree contains a single
! 38046: ** node taken from the head of *ppList. A depth of 2 means a tree with
! 38047: ** three nodes. And so forth.
! 38048: **
! 38049: ** Use as many entries from the input list as required and update the
! 38050: ** *ppList to point to the unused elements of the list. If the input
! 38051: ** list contains too few elements, then construct an incomplete tree
! 38052: ** and leave *ppList set to NULL.
! 38053: **
! 38054: ** Return a pointer to the root of the constructed binary tree.
! 38055: */
! 38056: static struct RowSetEntry *rowSetNDeepTree(
! 38057: struct RowSetEntry **ppList,
! 38058: int iDepth
! 38059: ){
! 38060: struct RowSetEntry *p; /* Root of the new tree */
! 38061: struct RowSetEntry *pLeft; /* Left subtree */
! 38062: if( *ppList==0 ){
! 38063: return 0;
! 38064: }
! 38065: if( iDepth==1 ){
! 38066: p = *ppList;
! 38067: *ppList = p->pRight;
! 38068: p->pLeft = p->pRight = 0;
! 38069: return p;
! 38070: }
! 38071: pLeft = rowSetNDeepTree(ppList, iDepth-1);
! 38072: p = *ppList;
! 38073: if( p==0 ){
! 38074: return pLeft;
! 38075: }
! 38076: p->pLeft = pLeft;
! 38077: *ppList = p->pRight;
! 38078: p->pRight = rowSetNDeepTree(ppList, iDepth-1);
! 38079: return p;
! 38080: }
! 38081:
! 38082: /*
! 38083: ** Convert a sorted list of elements into a binary tree. Make the tree
! 38084: ** as deep as it needs to be in order to contain the entire list.
! 38085: */
! 38086: static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
! 38087: int iDepth; /* Depth of the tree so far */
! 38088: struct RowSetEntry *p; /* Current tree root */
! 38089: struct RowSetEntry *pLeft; /* Left subtree */
! 38090:
! 38091: assert( pList!=0 );
! 38092: p = pList;
! 38093: pList = p->pRight;
! 38094: p->pLeft = p->pRight = 0;
! 38095: for(iDepth=1; pList; iDepth++){
! 38096: pLeft = p;
! 38097: p = pList;
! 38098: pList = p->pRight;
! 38099: p->pLeft = pLeft;
! 38100: p->pRight = rowSetNDeepTree(&pList, iDepth);
! 38101: }
! 38102: return p;
! 38103: }
! 38104:
! 38105: /*
! 38106: ** Convert the list in p->pEntry into a sorted list if it is not
! 38107: ** sorted already. If there is a binary tree on p->pTree, then
! 38108: ** convert it into a list too and merge it into the p->pEntry list.
! 38109: */
! 38110: static void rowSetToList(RowSet *p){
! 38111: if( !p->isSorted ){
! 38112: rowSetSort(p);
! 38113: }
! 38114: if( p->pTree ){
! 38115: struct RowSetEntry *pHead, *pTail;
! 38116: rowSetTreeToList(p->pTree, &pHead, &pTail);
! 38117: p->pTree = 0;
! 38118: p->pEntry = rowSetMerge(p->pEntry, pHead);
! 38119: }
! 38120: }
! 38121:
! 38122: /*
! 38123: ** Extract the smallest element from the RowSet.
! 38124: ** Write the element into *pRowid. Return 1 on success. Return
! 38125: ** 0 if the RowSet is already empty.
! 38126: **
! 38127: ** After this routine has been called, the sqlite3RowSetInsert()
! 38128: ** routine may not be called again.
! 38129: */
! 38130: SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
! 38131: rowSetToList(p);
! 38132: if( p->pEntry ){
! 38133: *pRowid = p->pEntry->v;
! 38134: p->pEntry = p->pEntry->pRight;
! 38135: if( p->pEntry==0 ){
! 38136: sqlite3RowSetClear(p);
! 38137: }
! 38138: return 1;
! 38139: }else{
! 38140: return 0;
! 38141: }
! 38142: }
! 38143:
! 38144: /*
! 38145: ** Check to see if element iRowid was inserted into the the rowset as
! 38146: ** part of any insert batch prior to iBatch. Return 1 or 0.
! 38147: */
! 38148: SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
! 38149: struct RowSetEntry *p;
! 38150: if( iBatch!=pRowSet->iBatch ){
! 38151: if( pRowSet->pEntry ){
! 38152: rowSetToList(pRowSet);
! 38153: pRowSet->pTree = rowSetListToTree(pRowSet->pEntry);
! 38154: pRowSet->pEntry = 0;
! 38155: pRowSet->pLast = 0;
! 38156: }
! 38157: pRowSet->iBatch = iBatch;
! 38158: }
! 38159: p = pRowSet->pTree;
! 38160: while( p ){
! 38161: if( p->v<iRowid ){
! 38162: p = p->pRight;
! 38163: }else if( p->v>iRowid ){
! 38164: p = p->pLeft;
! 38165: }else{
! 38166: return 1;
! 38167: }
! 38168: }
! 38169: return 0;
! 38170: }
! 38171:
! 38172: /************** End of rowset.c **********************************************/
! 38173: /************** Begin file pager.c *******************************************/
! 38174: /*
! 38175: ** 2001 September 15
! 38176: **
! 38177: ** The author disclaims copyright to this source code. In place of
! 38178: ** a legal notice, here is a blessing:
! 38179: **
! 38180: ** May you do good and not evil.
! 38181: ** May you find forgiveness for yourself and forgive others.
! 38182: ** May you share freely, never taking more than you give.
! 38183: **
! 38184: *************************************************************************
! 38185: ** This is the implementation of the page cache subsystem or "pager".
! 38186: **
! 38187: ** The pager is used to access a database disk file. It implements
! 38188: ** atomic commit and rollback through the use of a journal file that
! 38189: ** is separate from the database file. The pager also implements file
! 38190: ** locking to prevent two processes from writing the same database
! 38191: ** file simultaneously, or one process from reading the database while
! 38192: ** another is writing.
! 38193: */
! 38194: #ifndef SQLITE_OMIT_DISKIO
! 38195: /************** Include wal.h in the middle of pager.c ***********************/
! 38196: /************** Begin file wal.h *********************************************/
! 38197: /*
! 38198: ** 2010 February 1
! 38199: **
! 38200: ** The author disclaims copyright to this source code. In place of
! 38201: ** a legal notice, here is a blessing:
! 38202: **
! 38203: ** May you do good and not evil.
! 38204: ** May you find forgiveness for yourself and forgive others.
! 38205: ** May you share freely, never taking more than you give.
! 38206: **
! 38207: *************************************************************************
! 38208: ** This header file defines the interface to the write-ahead logging
! 38209: ** system. Refer to the comments below and the header comment attached to
! 38210: ** the implementation of each function in log.c for further details.
! 38211: */
! 38212:
! 38213: #ifndef _WAL_H_
! 38214: #define _WAL_H_
! 38215:
! 38216:
! 38217: /* Additional values that can be added to the sync_flags argument of
! 38218: ** sqlite3WalFrames():
! 38219: */
! 38220: #define WAL_SYNC_TRANSACTIONS 0x20 /* Sync at the end of each transaction */
! 38221: #define SQLITE_SYNC_MASK 0x13 /* Mask off the SQLITE_SYNC_* values */
! 38222:
! 38223: #ifdef SQLITE_OMIT_WAL
! 38224: # define sqlite3WalOpen(x,y,z) 0
! 38225: # define sqlite3WalLimit(x,y)
! 38226: # define sqlite3WalClose(w,x,y,z) 0
! 38227: # define sqlite3WalBeginReadTransaction(y,z) 0
! 38228: # define sqlite3WalEndReadTransaction(z)
! 38229: # define sqlite3WalRead(v,w,x,y,z) 0
! 38230: # define sqlite3WalDbsize(y) 0
! 38231: # define sqlite3WalBeginWriteTransaction(y) 0
! 38232: # define sqlite3WalEndWriteTransaction(x) 0
! 38233: # define sqlite3WalUndo(x,y,z) 0
! 38234: # define sqlite3WalSavepoint(y,z)
! 38235: # define sqlite3WalSavepointUndo(y,z) 0
! 38236: # define sqlite3WalFrames(u,v,w,x,y,z) 0
! 38237: # define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
! 38238: # define sqlite3WalCallback(z) 0
! 38239: # define sqlite3WalExclusiveMode(y,z) 0
! 38240: # define sqlite3WalHeapMemory(z) 0
! 38241: #else
! 38242:
! 38243: #define WAL_SAVEPOINT_NDATA 4
! 38244:
! 38245: /* Connection to a write-ahead log (WAL) file.
! 38246: ** There is one object of this type for each pager.
! 38247: */
! 38248: typedef struct Wal Wal;
! 38249:
! 38250: /* Open and close a connection to a write-ahead log. */
! 38251: SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
! 38252: SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
! 38253:
! 38254: /* Set the limiting size of a WAL file. */
! 38255: SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
! 38256:
! 38257: /* Used by readers to open (lock) and close (unlock) a snapshot. A
! 38258: ** snapshot is like a read-transaction. It is the state of the database
! 38259: ** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
! 38260: ** preserves the current state even if the other threads or processes
! 38261: ** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the
! 38262: ** transaction and releases the lock.
! 38263: */
! 38264: SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
! 38265: SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
! 38266:
! 38267: /* Read a page from the write-ahead log, if it is present. */
! 38268: SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
! 38269:
! 38270: /* If the WAL is not empty, return the size of the database. */
! 38271: SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
! 38272:
! 38273: /* Obtain or release the WRITER lock. */
! 38274: SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
! 38275: SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
! 38276:
! 38277: /* Undo any frames written (but not committed) to the log */
! 38278: SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
! 38279:
! 38280: /* Return an integer that records the current (uncommitted) write
! 38281: ** position in the WAL */
! 38282: SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
! 38283:
! 38284: /* Move the write position of the WAL back to iFrame. Called in
! 38285: ** response to a ROLLBACK TO command. */
! 38286: SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
! 38287:
! 38288: /* Write a frame or frames to the log. */
! 38289: SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
! 38290:
! 38291: /* Copy pages from the log to the database file */
! 38292: SQLITE_PRIVATE int sqlite3WalCheckpoint(
! 38293: Wal *pWal, /* Write-ahead log connection */
! 38294: int eMode, /* One of PASSIVE, FULL and RESTART */
! 38295: int (*xBusy)(void*), /* Function to call when busy */
! 38296: void *pBusyArg, /* Context argument for xBusyHandler */
! 38297: int sync_flags, /* Flags to sync db file with (or 0) */
! 38298: int nBuf, /* Size of buffer nBuf */
! 38299: u8 *zBuf, /* Temporary buffer to use */
! 38300: int *pnLog, /* OUT: Number of frames in WAL */
! 38301: int *pnCkpt /* OUT: Number of backfilled frames in WAL */
! 38302: );
! 38303:
! 38304: /* Return the value to pass to a sqlite3_wal_hook callback, the
! 38305: ** number of frames in the WAL at the point of the last commit since
! 38306: ** sqlite3WalCallback() was called. If no commits have occurred since
! 38307: ** the last call, then return 0.
! 38308: */
! 38309: SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
! 38310:
! 38311: /* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
! 38312: ** by the pager layer on the database file.
! 38313: */
! 38314: SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
! 38315:
! 38316: /* Return true if the argument is non-NULL and the WAL module is using
! 38317: ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
! 38318: ** WAL module is using shared-memory, return false.
! 38319: */
! 38320: SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
! 38321:
! 38322: #endif /* ifndef SQLITE_OMIT_WAL */
! 38323: #endif /* _WAL_H_ */
! 38324:
! 38325: /************** End of wal.h *************************************************/
! 38326: /************** Continuing where we left off in pager.c **********************/
! 38327:
! 38328:
! 38329: /******************* NOTES ON THE DESIGN OF THE PAGER ************************
! 38330: **
! 38331: ** This comment block describes invariants that hold when using a rollback
! 38332: ** journal. These invariants do not apply for journal_mode=WAL,
! 38333: ** journal_mode=MEMORY, or journal_mode=OFF.
! 38334: **
! 38335: ** Within this comment block, a page is deemed to have been synced
! 38336: ** automatically as soon as it is written when PRAGMA synchronous=OFF.
! 38337: ** Otherwise, the page is not synced until the xSync method of the VFS
! 38338: ** is called successfully on the file containing the page.
! 38339: **
! 38340: ** Definition: A page of the database file is said to be "overwriteable" if
! 38341: ** one or more of the following are true about the page:
! 38342: **
! 38343: ** (a) The original content of the page as it was at the beginning of
! 38344: ** the transaction has been written into the rollback journal and
! 38345: ** synced.
! 38346: **
! 38347: ** (b) The page was a freelist leaf page at the start of the transaction.
! 38348: **
! 38349: ** (c) The page number is greater than the largest page that existed in
! 38350: ** the database file at the start of the transaction.
! 38351: **
! 38352: ** (1) A page of the database file is never overwritten unless one of the
! 38353: ** following are true:
! 38354: **
! 38355: ** (a) The page and all other pages on the same sector are overwriteable.
! 38356: **
! 38357: ** (b) The atomic page write optimization is enabled, and the entire
! 38358: ** transaction other than the update of the transaction sequence
! 38359: ** number consists of a single page change.
! 38360: **
! 38361: ** (2) The content of a page written into the rollback journal exactly matches
! 38362: ** both the content in the database when the rollback journal was written
! 38363: ** and the content in the database at the beginning of the current
! 38364: ** transaction.
! 38365: **
! 38366: ** (3) Writes to the database file are an integer multiple of the page size
! 38367: ** in length and are aligned on a page boundary.
! 38368: **
! 38369: ** (4) Reads from the database file are either aligned on a page boundary and
! 38370: ** an integer multiple of the page size in length or are taken from the
! 38371: ** first 100 bytes of the database file.
! 38372: **
! 38373: ** (5) All writes to the database file are synced prior to the rollback journal
! 38374: ** being deleted, truncated, or zeroed.
! 38375: **
! 38376: ** (6) If a master journal file is used, then all writes to the database file
! 38377: ** are synced prior to the master journal being deleted.
! 38378: **
! 38379: ** Definition: Two databases (or the same database at two points it time)
! 38380: ** are said to be "logically equivalent" if they give the same answer to
! 38381: ** all queries. Note in particular the the content of freelist leaf
! 38382: ** pages can be changed arbitarily without effecting the logical equivalence
! 38383: ** of the database.
! 38384: **
! 38385: ** (7) At any time, if any subset, including the empty set and the total set,
! 38386: ** of the unsynced changes to a rollback journal are removed and the
! 38387: ** journal is rolled back, the resulting database file will be logical
! 38388: ** equivalent to the database file at the beginning of the transaction.
! 38389: **
! 38390: ** (8) When a transaction is rolled back, the xTruncate method of the VFS
! 38391: ** is called to restore the database file to the same size it was at
! 38392: ** the beginning of the transaction. (In some VFSes, the xTruncate
! 38393: ** method is a no-op, but that does not change the fact the SQLite will
! 38394: ** invoke it.)
! 38395: **
! 38396: ** (9) Whenever the database file is modified, at least one bit in the range
! 38397: ** of bytes from 24 through 39 inclusive will be changed prior to releasing
! 38398: ** the EXCLUSIVE lock, thus signaling other connections on the same
! 38399: ** database to flush their caches.
! 38400: **
! 38401: ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
! 38402: ** than one billion transactions.
! 38403: **
! 38404: ** (11) A database file is well-formed at the beginning and at the conclusion
! 38405: ** of every transaction.
! 38406: **
! 38407: ** (12) An EXCLUSIVE lock is held on the database file when writing to
! 38408: ** the database file.
! 38409: **
! 38410: ** (13) A SHARED lock is held on the database file while reading any
! 38411: ** content out of the database file.
! 38412: **
! 38413: ******************************************************************************/
! 38414:
! 38415: /*
! 38416: ** Macros for troubleshooting. Normally turned off
! 38417: */
! 38418: #if 0
! 38419: int sqlite3PagerTrace=1; /* True to enable tracing */
! 38420: #define sqlite3DebugPrintf printf
! 38421: #define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
! 38422: #else
! 38423: #define PAGERTRACE(X)
! 38424: #endif
! 38425:
! 38426: /*
! 38427: ** The following two macros are used within the PAGERTRACE() macros above
! 38428: ** to print out file-descriptors.
! 38429: **
! 38430: ** PAGERID() takes a pointer to a Pager struct as its argument. The
! 38431: ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
! 38432: ** struct as its argument.
! 38433: */
! 38434: #define PAGERID(p) ((int)(p->fd))
! 38435: #define FILEHANDLEID(fd) ((int)fd)
! 38436:
! 38437: /*
! 38438: ** The Pager.eState variable stores the current 'state' of a pager. A
! 38439: ** pager may be in any one of the seven states shown in the following
! 38440: ** state diagram.
! 38441: **
! 38442: ** OPEN <------+------+
! 38443: ** | | |
! 38444: ** V | |
! 38445: ** +---------> READER-------+ |
! 38446: ** | | |
! 38447: ** | V |
! 38448: ** |<-------WRITER_LOCKED------> ERROR
! 38449: ** | | ^
! 38450: ** | V |
! 38451: ** |<------WRITER_CACHEMOD-------->|
! 38452: ** | | |
! 38453: ** | V |
! 38454: ** |<-------WRITER_DBMOD---------->|
! 38455: ** | | |
! 38456: ** | V |
! 38457: ** +<------WRITER_FINISHED-------->+
! 38458: **
! 38459: **
! 38460: ** List of state transitions and the C [function] that performs each:
! 38461: **
! 38462: ** OPEN -> READER [sqlite3PagerSharedLock]
! 38463: ** READER -> OPEN [pager_unlock]
! 38464: **
! 38465: ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
! 38466: ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
! 38467: ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
! 38468: ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
! 38469: ** WRITER_*** -> READER [pager_end_transaction]
! 38470: **
! 38471: ** WRITER_*** -> ERROR [pager_error]
! 38472: ** ERROR -> OPEN [pager_unlock]
! 38473: **
! 38474: **
! 38475: ** OPEN:
! 38476: **
! 38477: ** The pager starts up in this state. Nothing is guaranteed in this
! 38478: ** state - the file may or may not be locked and the database size is
! 38479: ** unknown. The database may not be read or written.
! 38480: **
! 38481: ** * No read or write transaction is active.
! 38482: ** * Any lock, or no lock at all, may be held on the database file.
! 38483: ** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
! 38484: **
! 38485: ** READER:
! 38486: **
! 38487: ** In this state all the requirements for reading the database in
! 38488: ** rollback (non-WAL) mode are met. Unless the pager is (or recently
! 38489: ** was) in exclusive-locking mode, a user-level read transaction is
! 38490: ** open. The database size is known in this state.
! 38491: **
! 38492: ** A connection running with locking_mode=normal enters this state when
! 38493: ** it opens a read-transaction on the database and returns to state
! 38494: ** OPEN after the read-transaction is completed. However a connection
! 38495: ** running in locking_mode=exclusive (including temp databases) remains in
! 38496: ** this state even after the read-transaction is closed. The only way
! 38497: ** a locking_mode=exclusive connection can transition from READER to OPEN
! 38498: ** is via the ERROR state (see below).
! 38499: **
! 38500: ** * A read transaction may be active (but a write-transaction cannot).
! 38501: ** * A SHARED or greater lock is held on the database file.
! 38502: ** * The dbSize variable may be trusted (even if a user-level read
! 38503: ** transaction is not active). The dbOrigSize and dbFileSize variables
! 38504: ** may not be trusted at this point.
! 38505: ** * If the database is a WAL database, then the WAL connection is open.
! 38506: ** * Even if a read-transaction is not open, it is guaranteed that
! 38507: ** there is no hot-journal in the file-system.
! 38508: **
! 38509: ** WRITER_LOCKED:
! 38510: **
! 38511: ** The pager moves to this state from READER when a write-transaction
! 38512: ** is first opened on the database. In WRITER_LOCKED state, all locks
! 38513: ** required to start a write-transaction are held, but no actual
! 38514: ** modifications to the cache or database have taken place.
! 38515: **
! 38516: ** In rollback mode, a RESERVED or (if the transaction was opened with
! 38517: ** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
! 38518: ** moving to this state, but the journal file is not written to or opened
! 38519: ** to in this state. If the transaction is committed or rolled back while
! 38520: ** in WRITER_LOCKED state, all that is required is to unlock the database
! 38521: ** file.
! 38522: **
! 38523: ** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
! 38524: ** If the connection is running with locking_mode=exclusive, an attempt
! 38525: ** is made to obtain an EXCLUSIVE lock on the database file.
! 38526: **
! 38527: ** * A write transaction is active.
! 38528: ** * If the connection is open in rollback-mode, a RESERVED or greater
! 38529: ** lock is held on the database file.
! 38530: ** * If the connection is open in WAL-mode, a WAL write transaction
! 38531: ** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
! 38532: ** called).
! 38533: ** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
! 38534: ** * The contents of the pager cache have not been modified.
! 38535: ** * The journal file may or may not be open.
! 38536: ** * Nothing (not even the first header) has been written to the journal.
! 38537: **
! 38538: ** WRITER_CACHEMOD:
! 38539: **
! 38540: ** A pager moves from WRITER_LOCKED state to this state when a page is
! 38541: ** first modified by the upper layer. In rollback mode the journal file
! 38542: ** is opened (if it is not already open) and a header written to the
! 38543: ** start of it. The database file on disk has not been modified.
! 38544: **
! 38545: ** * A write transaction is active.
! 38546: ** * A RESERVED or greater lock is held on the database file.
! 38547: ** * The journal file is open and the first header has been written
! 38548: ** to it, but the header has not been synced to disk.
! 38549: ** * The contents of the page cache have been modified.
! 38550: **
! 38551: ** WRITER_DBMOD:
! 38552: **
! 38553: ** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
! 38554: ** when it modifies the contents of the database file. WAL connections
! 38555: ** never enter this state (since they do not modify the database file,
! 38556: ** just the log file).
! 38557: **
! 38558: ** * A write transaction is active.
! 38559: ** * An EXCLUSIVE or greater lock is held on the database file.
! 38560: ** * The journal file is open and the first header has been written
! 38561: ** and synced to disk.
! 38562: ** * The contents of the page cache have been modified (and possibly
! 38563: ** written to disk).
! 38564: **
! 38565: ** WRITER_FINISHED:
! 38566: **
! 38567: ** It is not possible for a WAL connection to enter this state.
! 38568: **
! 38569: ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
! 38570: ** state after the entire transaction has been successfully written into the
! 38571: ** database file. In this state the transaction may be committed simply
! 38572: ** by finalizing the journal file. Once in WRITER_FINISHED state, it is
! 38573: ** not possible to modify the database further. At this point, the upper
! 38574: ** layer must either commit or rollback the transaction.
! 38575: **
! 38576: ** * A write transaction is active.
! 38577: ** * An EXCLUSIVE or greater lock is held on the database file.
! 38578: ** * All writing and syncing of journal and database data has finished.
! 38579: ** If no error occured, all that remains is to finalize the journal to
! 38580: ** commit the transaction. If an error did occur, the caller will need
! 38581: ** to rollback the transaction.
! 38582: **
! 38583: ** ERROR:
! 38584: **
! 38585: ** The ERROR state is entered when an IO or disk-full error (including
! 38586: ** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
! 38587: ** difficult to be sure that the in-memory pager state (cache contents,
! 38588: ** db size etc.) are consistent with the contents of the file-system.
! 38589: **
! 38590: ** Temporary pager files may enter the ERROR state, but in-memory pagers
! 38591: ** cannot.
! 38592: **
! 38593: ** For example, if an IO error occurs while performing a rollback,
! 38594: ** the contents of the page-cache may be left in an inconsistent state.
! 38595: ** At this point it would be dangerous to change back to READER state
! 38596: ** (as usually happens after a rollback). Any subsequent readers might
! 38597: ** report database corruption (due to the inconsistent cache), and if
! 38598: ** they upgrade to writers, they may inadvertently corrupt the database
! 38599: ** file. To avoid this hazard, the pager switches into the ERROR state
! 38600: ** instead of READER following such an error.
! 38601: **
! 38602: ** Once it has entered the ERROR state, any attempt to use the pager
! 38603: ** to read or write data returns an error. Eventually, once all
! 38604: ** outstanding transactions have been abandoned, the pager is able to
! 38605: ** transition back to OPEN state, discarding the contents of the
! 38606: ** page-cache and any other in-memory state at the same time. Everything
! 38607: ** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
! 38608: ** when a read-transaction is next opened on the pager (transitioning
! 38609: ** the pager into READER state). At that point the system has recovered
! 38610: ** from the error.
! 38611: **
! 38612: ** Specifically, the pager jumps into the ERROR state if:
! 38613: **
! 38614: ** 1. An error occurs while attempting a rollback. This happens in
! 38615: ** function sqlite3PagerRollback().
! 38616: **
! 38617: ** 2. An error occurs while attempting to finalize a journal file
! 38618: ** following a commit in function sqlite3PagerCommitPhaseTwo().
! 38619: **
! 38620: ** 3. An error occurs while attempting to write to the journal or
! 38621: ** database file in function pagerStress() in order to free up
! 38622: ** memory.
! 38623: **
! 38624: ** In other cases, the error is returned to the b-tree layer. The b-tree
! 38625: ** layer then attempts a rollback operation. If the error condition
! 38626: ** persists, the pager enters the ERROR state via condition (1) above.
! 38627: **
! 38628: ** Condition (3) is necessary because it can be triggered by a read-only
! 38629: ** statement executed within a transaction. In this case, if the error
! 38630: ** code were simply returned to the user, the b-tree layer would not
! 38631: ** automatically attempt a rollback, as it assumes that an error in a
! 38632: ** read-only statement cannot leave the pager in an internally inconsistent
! 38633: ** state.
! 38634: **
! 38635: ** * The Pager.errCode variable is set to something other than SQLITE_OK.
! 38636: ** * There are one or more outstanding references to pages (after the
! 38637: ** last reference is dropped the pager should move back to OPEN state).
! 38638: ** * The pager is not an in-memory pager.
! 38639: **
! 38640: **
! 38641: ** Notes:
! 38642: **
! 38643: ** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
! 38644: ** connection is open in WAL mode. A WAL connection is always in one
! 38645: ** of the first four states.
! 38646: **
! 38647: ** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
! 38648: ** state. There are two exceptions: immediately after exclusive-mode has
! 38649: ** been turned on (and before any read or write transactions are
! 38650: ** executed), and when the pager is leaving the "error state".
! 38651: **
! 38652: ** * See also: assert_pager_state().
! 38653: */
! 38654: #define PAGER_OPEN 0
! 38655: #define PAGER_READER 1
! 38656: #define PAGER_WRITER_LOCKED 2
! 38657: #define PAGER_WRITER_CACHEMOD 3
! 38658: #define PAGER_WRITER_DBMOD 4
! 38659: #define PAGER_WRITER_FINISHED 5
! 38660: #define PAGER_ERROR 6
! 38661:
! 38662: /*
! 38663: ** The Pager.eLock variable is almost always set to one of the
! 38664: ** following locking-states, according to the lock currently held on
! 38665: ** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
! 38666: ** This variable is kept up to date as locks are taken and released by
! 38667: ** the pagerLockDb() and pagerUnlockDb() wrappers.
! 38668: **
! 38669: ** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
! 38670: ** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
! 38671: ** the operation was successful. In these circumstances pagerLockDb() and
! 38672: ** pagerUnlockDb() take a conservative approach - eLock is always updated
! 38673: ** when unlocking the file, and only updated when locking the file if the
! 38674: ** VFS call is successful. This way, the Pager.eLock variable may be set
! 38675: ** to a less exclusive (lower) value than the lock that is actually held
! 38676: ** at the system level, but it is never set to a more exclusive value.
! 38677: **
! 38678: ** This is usually safe. If an xUnlock fails or appears to fail, there may
! 38679: ** be a few redundant xLock() calls or a lock may be held for longer than
! 38680: ** required, but nothing really goes wrong.
! 38681: **
! 38682: ** The exception is when the database file is unlocked as the pager moves
! 38683: ** from ERROR to OPEN state. At this point there may be a hot-journal file
! 38684: ** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
! 38685: ** transition, by the same pager or any other). If the call to xUnlock()
! 38686: ** fails at this point and the pager is left holding an EXCLUSIVE lock, this
! 38687: ** can confuse the call to xCheckReservedLock() call made later as part
! 38688: ** of hot-journal detection.
! 38689: **
! 38690: ** xCheckReservedLock() is defined as returning true "if there is a RESERVED
! 38691: ** lock held by this process or any others". So xCheckReservedLock may
! 38692: ** return true because the caller itself is holding an EXCLUSIVE lock (but
! 38693: ** doesn't know it because of a previous error in xUnlock). If this happens
! 38694: ** a hot-journal may be mistaken for a journal being created by an active
! 38695: ** transaction in another process, causing SQLite to read from the database
! 38696: ** without rolling it back.
! 38697: **
! 38698: ** To work around this, if a call to xUnlock() fails when unlocking the
! 38699: ** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
! 38700: ** is only changed back to a real locking state after a successful call
! 38701: ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
! 38702: ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
! 38703: ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
! 38704: ** lock on the database file before attempting to roll it back. See function
! 38705: ** PagerSharedLock() for more detail.
! 38706: **
! 38707: ** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
! 38708: ** PAGER_OPEN state.
! 38709: */
! 38710: #define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1)
! 38711:
! 38712: /*
! 38713: ** A macro used for invoking the codec if there is one
! 38714: */
! 38715: #ifdef SQLITE_HAS_CODEC
! 38716: # define CODEC1(P,D,N,X,E) \
! 38717: if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
! 38718: # define CODEC2(P,D,N,X,E,O) \
! 38719: if( P->xCodec==0 ){ O=(char*)D; }else \
! 38720: if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
! 38721: #else
! 38722: # define CODEC1(P,D,N,X,E) /* NO-OP */
! 38723: # define CODEC2(P,D,N,X,E,O) O=(char*)D
! 38724: #endif
! 38725:
! 38726: /*
! 38727: ** The maximum allowed sector size. 64KiB. If the xSectorsize() method
! 38728: ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
! 38729: ** This could conceivably cause corruption following a power failure on
! 38730: ** such a system. This is currently an undocumented limit.
! 38731: */
! 38732: #define MAX_SECTOR_SIZE 0x10000
! 38733:
! 38734: /*
! 38735: ** An instance of the following structure is allocated for each active
! 38736: ** savepoint and statement transaction in the system. All such structures
! 38737: ** are stored in the Pager.aSavepoint[] array, which is allocated and
! 38738: ** resized using sqlite3Realloc().
! 38739: **
! 38740: ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
! 38741: ** set to 0. If a journal-header is written into the main journal while
! 38742: ** the savepoint is active, then iHdrOffset is set to the byte offset
! 38743: ** immediately following the last journal record written into the main
! 38744: ** journal before the journal-header. This is required during savepoint
! 38745: ** rollback (see pagerPlaybackSavepoint()).
! 38746: */
! 38747: typedef struct PagerSavepoint PagerSavepoint;
! 38748: struct PagerSavepoint {
! 38749: i64 iOffset; /* Starting offset in main journal */
! 38750: i64 iHdrOffset; /* See above */
! 38751: Bitvec *pInSavepoint; /* Set of pages in this savepoint */
! 38752: Pgno nOrig; /* Original number of pages in file */
! 38753: Pgno iSubRec; /* Index of first record in sub-journal */
! 38754: #ifndef SQLITE_OMIT_WAL
! 38755: u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
! 38756: #endif
! 38757: };
! 38758:
! 38759: /*
! 38760: ** A open page cache is an instance of struct Pager. A description of
! 38761: ** some of the more important member variables follows:
! 38762: **
! 38763: ** eState
! 38764: **
! 38765: ** The current 'state' of the pager object. See the comment and state
! 38766: ** diagram above for a description of the pager state.
! 38767: **
! 38768: ** eLock
! 38769: **
! 38770: ** For a real on-disk database, the current lock held on the database file -
! 38771: ** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
! 38772: **
! 38773: ** For a temporary or in-memory database (neither of which require any
! 38774: ** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
! 38775: ** databases always have Pager.exclusiveMode==1, this tricks the pager
! 38776: ** logic into thinking that it already has all the locks it will ever
! 38777: ** need (and no reason to release them).
! 38778: **
! 38779: ** In some (obscure) circumstances, this variable may also be set to
! 38780: ** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
! 38781: ** details.
! 38782: **
! 38783: ** changeCountDone
! 38784: **
! 38785: ** This boolean variable is used to make sure that the change-counter
! 38786: ** (the 4-byte header field at byte offset 24 of the database file) is
! 38787: ** not updated more often than necessary.
! 38788: **
! 38789: ** It is set to true when the change-counter field is updated, which
! 38790: ** can only happen if an exclusive lock is held on the database file.
! 38791: ** It is cleared (set to false) whenever an exclusive lock is
! 38792: ** relinquished on the database file. Each time a transaction is committed,
! 38793: ** The changeCountDone flag is inspected. If it is true, the work of
! 38794: ** updating the change-counter is omitted for the current transaction.
! 38795: **
! 38796: ** This mechanism means that when running in exclusive mode, a connection
! 38797: ** need only update the change-counter once, for the first transaction
! 38798: ** committed.
! 38799: **
! 38800: ** setMaster
! 38801: **
! 38802: ** When PagerCommitPhaseOne() is called to commit a transaction, it may
! 38803: ** (or may not) specify a master-journal name to be written into the
! 38804: ** journal file before it is synced to disk.
! 38805: **
! 38806: ** Whether or not a journal file contains a master-journal pointer affects
! 38807: ** the way in which the journal file is finalized after the transaction is
! 38808: ** committed or rolled back when running in "journal_mode=PERSIST" mode.
! 38809: ** If a journal file does not contain a master-journal pointer, it is
! 38810: ** finalized by overwriting the first journal header with zeroes. If
! 38811: ** it does contain a master-journal pointer the journal file is finalized
! 38812: ** by truncating it to zero bytes, just as if the connection were
! 38813: ** running in "journal_mode=truncate" mode.
! 38814: **
! 38815: ** Journal files that contain master journal pointers cannot be finalized
! 38816: ** simply by overwriting the first journal-header with zeroes, as the
! 38817: ** master journal pointer could interfere with hot-journal rollback of any
! 38818: ** subsequently interrupted transaction that reuses the journal file.
! 38819: **
! 38820: ** The flag is cleared as soon as the journal file is finalized (either
! 38821: ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
! 38822: ** journal file from being successfully finalized, the setMaster flag
! 38823: ** is cleared anyway (and the pager will move to ERROR state).
! 38824: **
! 38825: ** doNotSpill, doNotSyncSpill
! 38826: **
! 38827: ** These two boolean variables control the behaviour of cache-spills
! 38828: ** (calls made by the pcache module to the pagerStress() routine to
! 38829: ** write cached data to the file-system in order to free up memory).
! 38830: **
! 38831: ** When doNotSpill is non-zero, writing to the database from pagerStress()
! 38832: ** is disabled altogether. This is done in a very obscure case that
! 38833: ** comes up during savepoint rollback that requires the pcache module
! 38834: ** to allocate a new page to prevent the journal file from being written
! 38835: ** while it is being traversed by code in pager_playback().
! 38836: **
! 38837: ** If doNotSyncSpill is non-zero, writing to the database from pagerStress()
! 38838: ** is permitted, but syncing the journal file is not. This flag is set
! 38839: ** by sqlite3PagerWrite() when the file-system sector-size is larger than
! 38840: ** the database page-size in order to prevent a journal sync from happening
! 38841: ** in between the journalling of two pages on the same sector.
! 38842: **
! 38843: ** subjInMemory
! 38844: **
! 38845: ** This is a boolean variable. If true, then any required sub-journal
! 38846: ** is opened as an in-memory journal file. If false, then in-memory
! 38847: ** sub-journals are only used for in-memory pager files.
! 38848: **
! 38849: ** This variable is updated by the upper layer each time a new
! 38850: ** write-transaction is opened.
! 38851: **
! 38852: ** dbSize, dbOrigSize, dbFileSize
! 38853: **
! 38854: ** Variable dbSize is set to the number of pages in the database file.
! 38855: ** It is valid in PAGER_READER and higher states (all states except for
! 38856: ** OPEN and ERROR).
! 38857: **
! 38858: ** dbSize is set based on the size of the database file, which may be
! 38859: ** larger than the size of the database (the value stored at offset
! 38860: ** 28 of the database header by the btree). If the size of the file
! 38861: ** is not an integer multiple of the page-size, the value stored in
! 38862: ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
! 38863: ** Except, any file that is greater than 0 bytes in size is considered
! 38864: ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
! 38865: ** to dbSize==1).
! 38866: **
! 38867: ** During a write-transaction, if pages with page-numbers greater than
! 38868: ** dbSize are modified in the cache, dbSize is updated accordingly.
! 38869: ** Similarly, if the database is truncated using PagerTruncateImage(),
! 38870: ** dbSize is updated.
! 38871: **
! 38872: ** Variables dbOrigSize and dbFileSize are valid in states
! 38873: ** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
! 38874: ** variable at the start of the transaction. It is used during rollback,
! 38875: ** and to determine whether or not pages need to be journalled before
! 38876: ** being modified.
! 38877: **
! 38878: ** Throughout a write-transaction, dbFileSize contains the size of
! 38879: ** the file on disk in pages. It is set to a copy of dbSize when the
! 38880: ** write-transaction is first opened, and updated when VFS calls are made
! 38881: ** to write or truncate the database file on disk.
! 38882: **
! 38883: ** The only reason the dbFileSize variable is required is to suppress
! 38884: ** unnecessary calls to xTruncate() after committing a transaction. If,
! 38885: ** when a transaction is committed, the dbFileSize variable indicates
! 38886: ** that the database file is larger than the database image (Pager.dbSize),
! 38887: ** pager_truncate() is called. The pager_truncate() call uses xFilesize()
! 38888: ** to measure the database file on disk, and then truncates it if required.
! 38889: ** dbFileSize is not used when rolling back a transaction. In this case
! 38890: ** pager_truncate() is called unconditionally (which means there may be
! 38891: ** a call to xFilesize() that is not strictly required). In either case,
! 38892: ** pager_truncate() may cause the file to become smaller or larger.
! 38893: **
! 38894: ** dbHintSize
! 38895: **
! 38896: ** The dbHintSize variable is used to limit the number of calls made to
! 38897: ** the VFS xFileControl(FCNTL_SIZE_HINT) method.
! 38898: **
! 38899: ** dbHintSize is set to a copy of the dbSize variable when a
! 38900: ** write-transaction is opened (at the same time as dbFileSize and
! 38901: ** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
! 38902: ** dbHintSize is increased to the number of pages that correspond to the
! 38903: ** size-hint passed to the method call. See pager_write_pagelist() for
! 38904: ** details.
! 38905: **
! 38906: ** errCode
! 38907: **
! 38908: ** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
! 38909: ** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
! 38910: ** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
! 38911: ** sub-codes.
! 38912: */
! 38913: struct Pager {
! 38914: sqlite3_vfs *pVfs; /* OS functions to use for IO */
! 38915: u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
! 38916: u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
! 38917: u8 useJournal; /* Use a rollback journal on this file */
! 38918: u8 noReadlock; /* Do not bother to obtain readlocks */
! 38919: u8 noSync; /* Do not sync the journal if true */
! 38920: u8 fullSync; /* Do extra syncs of the journal for robustness */
! 38921: u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
! 38922: u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */
! 38923: u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
! 38924: u8 tempFile; /* zFilename is a temporary file */
! 38925: u8 readOnly; /* True for a read-only database */
! 38926: u8 memDb; /* True to inhibit all file I/O */
! 38927:
! 38928: /**************************************************************************
! 38929: ** The following block contains those class members that change during
! 38930: ** routine opertion. Class members not in this block are either fixed
! 38931: ** when the pager is first created or else only change when there is a
! 38932: ** significant mode change (such as changing the page_size, locking_mode,
! 38933: ** or the journal_mode). From another view, these class members describe
! 38934: ** the "state" of the pager, while other class members describe the
! 38935: ** "configuration" of the pager.
! 38936: */
! 38937: u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
! 38938: u8 eLock; /* Current lock held on database file */
! 38939: u8 changeCountDone; /* Set after incrementing the change-counter */
! 38940: u8 setMaster; /* True if a m-j name has been written to jrnl */
! 38941: u8 doNotSpill; /* Do not spill the cache when non-zero */
! 38942: u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */
! 38943: u8 subjInMemory; /* True to use in-memory sub-journals */
! 38944: Pgno dbSize; /* Number of pages in the database */
! 38945: Pgno dbOrigSize; /* dbSize before the current transaction */
! 38946: Pgno dbFileSize; /* Number of pages in the database file */
! 38947: Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
! 38948: int errCode; /* One of several kinds of errors */
! 38949: int nRec; /* Pages journalled since last j-header written */
! 38950: u32 cksumInit; /* Quasi-random value added to every checksum */
! 38951: u32 nSubRec; /* Number of records written to sub-journal */
! 38952: Bitvec *pInJournal; /* One bit for each page in the database file */
! 38953: sqlite3_file *fd; /* File descriptor for database */
! 38954: sqlite3_file *jfd; /* File descriptor for main journal */
! 38955: sqlite3_file *sjfd; /* File descriptor for sub-journal */
! 38956: i64 journalOff; /* Current write offset in the journal file */
! 38957: i64 journalHdr; /* Byte offset to previous journal header */
! 38958: sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
! 38959: PagerSavepoint *aSavepoint; /* Array of active savepoints */
! 38960: int nSavepoint; /* Number of elements in aSavepoint[] */
! 38961: char dbFileVers[16]; /* Changes whenever database file changes */
! 38962: /*
! 38963: ** End of the routinely-changing class members
! 38964: ***************************************************************************/
! 38965:
! 38966: u16 nExtra; /* Add this many bytes to each in-memory page */
! 38967: i16 nReserve; /* Number of unused bytes at end of each page */
! 38968: u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
! 38969: u32 sectorSize; /* Assumed sector size during rollback */
! 38970: int pageSize; /* Number of bytes in a page */
! 38971: Pgno mxPgno; /* Maximum allowed size of the database */
! 38972: i64 journalSizeLimit; /* Size limit for persistent journal files */
! 38973: char *zFilename; /* Name of the database file */
! 38974: char *zJournal; /* Name of the journal file */
! 38975: int (*xBusyHandler)(void*); /* Function to call when busy */
! 38976: void *pBusyHandlerArg; /* Context argument for xBusyHandler */
! 38977: int nHit, nMiss; /* Total cache hits and misses */
! 38978: #ifdef SQLITE_TEST
! 38979: int nRead, nWrite; /* Database pages read/written */
! 38980: #endif
! 38981: void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
! 38982: #ifdef SQLITE_HAS_CODEC
! 38983: void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
! 38984: void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
! 38985: void (*xCodecFree)(void*); /* Destructor for the codec */
! 38986: void *pCodec; /* First argument to xCodec... methods */
! 38987: #endif
! 38988: char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
! 38989: PCache *pPCache; /* Pointer to page cache object */
! 38990: #ifndef SQLITE_OMIT_WAL
! 38991: Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
! 38992: char *zWal; /* File name for write-ahead log */
! 38993: #endif
! 38994: };
! 38995:
! 38996: /*
! 38997: ** The following global variables hold counters used for
! 38998: ** testing purposes only. These variables do not exist in
! 38999: ** a non-testing build. These variables are not thread-safe.
! 39000: */
! 39001: #ifdef SQLITE_TEST
! 39002: SQLITE_API int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
! 39003: SQLITE_API int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
! 39004: SQLITE_API int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
! 39005: # define PAGER_INCR(v) v++
! 39006: #else
! 39007: # define PAGER_INCR(v)
! 39008: #endif
! 39009:
! 39010:
! 39011:
! 39012: /*
! 39013: ** Journal files begin with the following magic string. The data
! 39014: ** was obtained from /dev/random. It is used only as a sanity check.
! 39015: **
! 39016: ** Since version 2.8.0, the journal format contains additional sanity
! 39017: ** checking information. If the power fails while the journal is being
! 39018: ** written, semi-random garbage data might appear in the journal
! 39019: ** file after power is restored. If an attempt is then made
! 39020: ** to roll the journal back, the database could be corrupted. The additional
! 39021: ** sanity checking data is an attempt to discover the garbage in the
! 39022: ** journal and ignore it.
! 39023: **
! 39024: ** The sanity checking information for the new journal format consists
! 39025: ** of a 32-bit checksum on each page of data. The checksum covers both
! 39026: ** the page number and the pPager->pageSize bytes of data for the page.
! 39027: ** This cksum is initialized to a 32-bit random value that appears in the
! 39028: ** journal file right after the header. The random initializer is important,
! 39029: ** because garbage data that appears at the end of a journal is likely
! 39030: ** data that was once in other files that have now been deleted. If the
! 39031: ** garbage data came from an obsolete journal file, the checksums might
! 39032: ** be correct. But by initializing the checksum to random value which
! 39033: ** is different for every journal, we minimize that risk.
! 39034: */
! 39035: static const unsigned char aJournalMagic[] = {
! 39036: 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
! 39037: };
! 39038:
! 39039: /*
! 39040: ** The size of the of each page record in the journal is given by
! 39041: ** the following macro.
! 39042: */
! 39043: #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
! 39044:
! 39045: /*
! 39046: ** The journal header size for this pager. This is usually the same
! 39047: ** size as a single disk sector. See also setSectorSize().
! 39048: */
! 39049: #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
! 39050:
! 39051: /*
! 39052: ** The macro MEMDB is true if we are dealing with an in-memory database.
! 39053: ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
! 39054: ** the value of MEMDB will be a constant and the compiler will optimize
! 39055: ** out code that would never execute.
! 39056: */
! 39057: #ifdef SQLITE_OMIT_MEMORYDB
! 39058: # define MEMDB 0
! 39059: #else
! 39060: # define MEMDB pPager->memDb
! 39061: #endif
! 39062:
! 39063: /*
! 39064: ** The maximum legal page number is (2^31 - 1).
! 39065: */
! 39066: #define PAGER_MAX_PGNO 2147483647
! 39067:
! 39068: /*
! 39069: ** The argument to this macro is a file descriptor (type sqlite3_file*).
! 39070: ** Return 0 if it is not open, or non-zero (but not 1) if it is.
! 39071: **
! 39072: ** This is so that expressions can be written as:
! 39073: **
! 39074: ** if( isOpen(pPager->jfd) ){ ...
! 39075: **
! 39076: ** instead of
! 39077: **
! 39078: ** if( pPager->jfd->pMethods ){ ...
! 39079: */
! 39080: #define isOpen(pFd) ((pFd)->pMethods)
! 39081:
! 39082: /*
! 39083: ** Return true if this pager uses a write-ahead log instead of the usual
! 39084: ** rollback journal. Otherwise false.
! 39085: */
! 39086: #ifndef SQLITE_OMIT_WAL
! 39087: static int pagerUseWal(Pager *pPager){
! 39088: return (pPager->pWal!=0);
! 39089: }
! 39090: #else
! 39091: # define pagerUseWal(x) 0
! 39092: # define pagerRollbackWal(x) 0
! 39093: # define pagerWalFrames(v,w,x,y) 0
! 39094: # define pagerOpenWalIfPresent(z) SQLITE_OK
! 39095: # define pagerBeginReadTransaction(z) SQLITE_OK
! 39096: #endif
! 39097:
! 39098: #ifndef NDEBUG
! 39099: /*
! 39100: ** Usage:
! 39101: **
! 39102: ** assert( assert_pager_state(pPager) );
! 39103: **
! 39104: ** This function runs many asserts to try to find inconsistencies in
! 39105: ** the internal state of the Pager object.
! 39106: */
! 39107: static int assert_pager_state(Pager *p){
! 39108: Pager *pPager = p;
! 39109:
! 39110: /* State must be valid. */
! 39111: assert( p->eState==PAGER_OPEN
! 39112: || p->eState==PAGER_READER
! 39113: || p->eState==PAGER_WRITER_LOCKED
! 39114: || p->eState==PAGER_WRITER_CACHEMOD
! 39115: || p->eState==PAGER_WRITER_DBMOD
! 39116: || p->eState==PAGER_WRITER_FINISHED
! 39117: || p->eState==PAGER_ERROR
! 39118: );
! 39119:
! 39120: /* Regardless of the current state, a temp-file connection always behaves
! 39121: ** as if it has an exclusive lock on the database file. It never updates
! 39122: ** the change-counter field, so the changeCountDone flag is always set.
! 39123: */
! 39124: assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
! 39125: assert( p->tempFile==0 || pPager->changeCountDone );
! 39126:
! 39127: /* If the useJournal flag is clear, the journal-mode must be "OFF".
! 39128: ** And if the journal-mode is "OFF", the journal file must not be open.
! 39129: */
! 39130: assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
! 39131: assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
! 39132:
! 39133: /* Check that MEMDB implies noSync. And an in-memory journal. Since
! 39134: ** this means an in-memory pager performs no IO at all, it cannot encounter
! 39135: ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
! 39136: ** a journal file. (although the in-memory journal implementation may
! 39137: ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
! 39138: ** is therefore not possible for an in-memory pager to enter the ERROR
! 39139: ** state.
! 39140: */
! 39141: if( MEMDB ){
! 39142: assert( p->noSync );
! 39143: assert( p->journalMode==PAGER_JOURNALMODE_OFF
! 39144: || p->journalMode==PAGER_JOURNALMODE_MEMORY
! 39145: );
! 39146: assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
! 39147: assert( pagerUseWal(p)==0 );
! 39148: }
! 39149:
! 39150: /* If changeCountDone is set, a RESERVED lock or greater must be held
! 39151: ** on the file.
! 39152: */
! 39153: assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
! 39154: assert( p->eLock!=PENDING_LOCK );
! 39155:
! 39156: switch( p->eState ){
! 39157: case PAGER_OPEN:
! 39158: assert( !MEMDB );
! 39159: assert( pPager->errCode==SQLITE_OK );
! 39160: assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
! 39161: break;
! 39162:
! 39163: case PAGER_READER:
! 39164: assert( pPager->errCode==SQLITE_OK );
! 39165: assert( p->eLock!=UNKNOWN_LOCK );
! 39166: assert( p->eLock>=SHARED_LOCK || p->noReadlock );
! 39167: break;
! 39168:
! 39169: case PAGER_WRITER_LOCKED:
! 39170: assert( p->eLock!=UNKNOWN_LOCK );
! 39171: assert( pPager->errCode==SQLITE_OK );
! 39172: if( !pagerUseWal(pPager) ){
! 39173: assert( p->eLock>=RESERVED_LOCK );
! 39174: }
! 39175: assert( pPager->dbSize==pPager->dbOrigSize );
! 39176: assert( pPager->dbOrigSize==pPager->dbFileSize );
! 39177: assert( pPager->dbOrigSize==pPager->dbHintSize );
! 39178: assert( pPager->setMaster==0 );
! 39179: break;
! 39180:
! 39181: case PAGER_WRITER_CACHEMOD:
! 39182: assert( p->eLock!=UNKNOWN_LOCK );
! 39183: assert( pPager->errCode==SQLITE_OK );
! 39184: if( !pagerUseWal(pPager) ){
! 39185: /* It is possible that if journal_mode=wal here that neither the
! 39186: ** journal file nor the WAL file are open. This happens during
! 39187: ** a rollback transaction that switches from journal_mode=off
! 39188: ** to journal_mode=wal.
! 39189: */
! 39190: assert( p->eLock>=RESERVED_LOCK );
! 39191: assert( isOpen(p->jfd)
! 39192: || p->journalMode==PAGER_JOURNALMODE_OFF
! 39193: || p->journalMode==PAGER_JOURNALMODE_WAL
! 39194: );
! 39195: }
! 39196: assert( pPager->dbOrigSize==pPager->dbFileSize );
! 39197: assert( pPager->dbOrigSize==pPager->dbHintSize );
! 39198: break;
! 39199:
! 39200: case PAGER_WRITER_DBMOD:
! 39201: assert( p->eLock==EXCLUSIVE_LOCK );
! 39202: assert( pPager->errCode==SQLITE_OK );
! 39203: assert( !pagerUseWal(pPager) );
! 39204: assert( p->eLock>=EXCLUSIVE_LOCK );
! 39205: assert( isOpen(p->jfd)
! 39206: || p->journalMode==PAGER_JOURNALMODE_OFF
! 39207: || p->journalMode==PAGER_JOURNALMODE_WAL
! 39208: );
! 39209: assert( pPager->dbOrigSize<=pPager->dbHintSize );
! 39210: break;
! 39211:
! 39212: case PAGER_WRITER_FINISHED:
! 39213: assert( p->eLock==EXCLUSIVE_LOCK );
! 39214: assert( pPager->errCode==SQLITE_OK );
! 39215: assert( !pagerUseWal(pPager) );
! 39216: assert( isOpen(p->jfd)
! 39217: || p->journalMode==PAGER_JOURNALMODE_OFF
! 39218: || p->journalMode==PAGER_JOURNALMODE_WAL
! 39219: );
! 39220: break;
! 39221:
! 39222: case PAGER_ERROR:
! 39223: /* There must be at least one outstanding reference to the pager if
! 39224: ** in ERROR state. Otherwise the pager should have already dropped
! 39225: ** back to OPEN state.
! 39226: */
! 39227: assert( pPager->errCode!=SQLITE_OK );
! 39228: assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
! 39229: break;
! 39230: }
! 39231:
! 39232: return 1;
! 39233: }
! 39234: #endif /* ifndef NDEBUG */
! 39235:
! 39236: #ifdef SQLITE_DEBUG
! 39237: /*
! 39238: ** Return a pointer to a human readable string in a static buffer
! 39239: ** containing the state of the Pager object passed as an argument. This
! 39240: ** is intended to be used within debuggers. For example, as an alternative
! 39241: ** to "print *pPager" in gdb:
! 39242: **
! 39243: ** (gdb) printf "%s", print_pager_state(pPager)
! 39244: */
! 39245: static char *print_pager_state(Pager *p){
! 39246: static char zRet[1024];
! 39247:
! 39248: sqlite3_snprintf(1024, zRet,
! 39249: "Filename: %s\n"
! 39250: "State: %s errCode=%d\n"
! 39251: "Lock: %s\n"
! 39252: "Locking mode: locking_mode=%s\n"
! 39253: "Journal mode: journal_mode=%s\n"
! 39254: "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
! 39255: "Journal: journalOff=%lld journalHdr=%lld\n"
! 39256: "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
! 39257: , p->zFilename
! 39258: , p->eState==PAGER_OPEN ? "OPEN" :
! 39259: p->eState==PAGER_READER ? "READER" :
! 39260: p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
! 39261: p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
! 39262: p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
! 39263: p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
! 39264: p->eState==PAGER_ERROR ? "ERROR" : "?error?"
! 39265: , (int)p->errCode
! 39266: , p->eLock==NO_LOCK ? "NO_LOCK" :
! 39267: p->eLock==RESERVED_LOCK ? "RESERVED" :
! 39268: p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
! 39269: p->eLock==SHARED_LOCK ? "SHARED" :
! 39270: p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
! 39271: , p->exclusiveMode ? "exclusive" : "normal"
! 39272: , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
! 39273: p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
! 39274: p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
! 39275: p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
! 39276: p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
! 39277: p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
! 39278: , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
! 39279: , p->journalOff, p->journalHdr
! 39280: , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
! 39281: );
! 39282:
! 39283: return zRet;
! 39284: }
! 39285: #endif
! 39286:
! 39287: /*
! 39288: ** Return true if it is necessary to write page *pPg into the sub-journal.
! 39289: ** A page needs to be written into the sub-journal if there exists one
! 39290: ** or more open savepoints for which:
! 39291: **
! 39292: ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
! 39293: ** * The bit corresponding to the page-number is not set in
! 39294: ** PagerSavepoint.pInSavepoint.
! 39295: */
! 39296: static int subjRequiresPage(PgHdr *pPg){
! 39297: Pgno pgno = pPg->pgno;
! 39298: Pager *pPager = pPg->pPager;
! 39299: int i;
! 39300: for(i=0; i<pPager->nSavepoint; i++){
! 39301: PagerSavepoint *p = &pPager->aSavepoint[i];
! 39302: if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
! 39303: return 1;
! 39304: }
! 39305: }
! 39306: return 0;
! 39307: }
! 39308:
! 39309: /*
! 39310: ** Return true if the page is already in the journal file.
! 39311: */
! 39312: static int pageInJournal(PgHdr *pPg){
! 39313: return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
! 39314: }
! 39315:
! 39316: /*
! 39317: ** Read a 32-bit integer from the given file descriptor. Store the integer
! 39318: ** that is read in *pRes. Return SQLITE_OK if everything worked, or an
! 39319: ** error code is something goes wrong.
! 39320: **
! 39321: ** All values are stored on disk as big-endian.
! 39322: */
! 39323: static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
! 39324: unsigned char ac[4];
! 39325: int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
! 39326: if( rc==SQLITE_OK ){
! 39327: *pRes = sqlite3Get4byte(ac);
! 39328: }
! 39329: return rc;
! 39330: }
! 39331:
! 39332: /*
! 39333: ** Write a 32-bit integer into a string buffer in big-endian byte order.
! 39334: */
! 39335: #define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
! 39336:
! 39337:
! 39338: /*
! 39339: ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
! 39340: ** on success or an error code is something goes wrong.
! 39341: */
! 39342: static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
! 39343: char ac[4];
! 39344: put32bits(ac, val);
! 39345: return sqlite3OsWrite(fd, ac, 4, offset);
! 39346: }
! 39347:
! 39348: /*
! 39349: ** Unlock the database file to level eLock, which must be either NO_LOCK
! 39350: ** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
! 39351: ** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
! 39352: **
! 39353: ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
! 39354: ** called, do not modify it. See the comment above the #define of
! 39355: ** UNKNOWN_LOCK for an explanation of this.
! 39356: */
! 39357: static int pagerUnlockDb(Pager *pPager, int eLock){
! 39358: int rc = SQLITE_OK;
! 39359:
! 39360: assert( !pPager->exclusiveMode || pPager->eLock==eLock );
! 39361: assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
! 39362: assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
! 39363: if( isOpen(pPager->fd) ){
! 39364: assert( pPager->eLock>=eLock );
! 39365: rc = sqlite3OsUnlock(pPager->fd, eLock);
! 39366: if( pPager->eLock!=UNKNOWN_LOCK ){
! 39367: pPager->eLock = (u8)eLock;
! 39368: }
! 39369: IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
! 39370: }
! 39371: return rc;
! 39372: }
! 39373:
! 39374: /*
! 39375: ** Lock the database file to level eLock, which must be either SHARED_LOCK,
! 39376: ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
! 39377: ** Pager.eLock variable to the new locking state.
! 39378: **
! 39379: ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
! 39380: ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
! 39381: ** See the comment above the #define of UNKNOWN_LOCK for an explanation
! 39382: ** of this.
! 39383: */
! 39384: static int pagerLockDb(Pager *pPager, int eLock){
! 39385: int rc = SQLITE_OK;
! 39386:
! 39387: assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
! 39388: if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
! 39389: rc = sqlite3OsLock(pPager->fd, eLock);
! 39390: if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
! 39391: pPager->eLock = (u8)eLock;
! 39392: IOTRACE(("LOCK %p %d\n", pPager, eLock))
! 39393: }
! 39394: }
! 39395: return rc;
! 39396: }
! 39397:
! 39398: /*
! 39399: ** This function determines whether or not the atomic-write optimization
! 39400: ** can be used with this pager. The optimization can be used if:
! 39401: **
! 39402: ** (a) the value returned by OsDeviceCharacteristics() indicates that
! 39403: ** a database page may be written atomically, and
! 39404: ** (b) the value returned by OsSectorSize() is less than or equal
! 39405: ** to the page size.
! 39406: **
! 39407: ** The optimization is also always enabled for temporary files. It is
! 39408: ** an error to call this function if pPager is opened on an in-memory
! 39409: ** database.
! 39410: **
! 39411: ** If the optimization cannot be used, 0 is returned. If it can be used,
! 39412: ** then the value returned is the size of the journal file when it
! 39413: ** contains rollback data for exactly one page.
! 39414: */
! 39415: #ifdef SQLITE_ENABLE_ATOMIC_WRITE
! 39416: static int jrnlBufferSize(Pager *pPager){
! 39417: assert( !MEMDB );
! 39418: if( !pPager->tempFile ){
! 39419: int dc; /* Device characteristics */
! 39420: int nSector; /* Sector size */
! 39421: int szPage; /* Page size */
! 39422:
! 39423: assert( isOpen(pPager->fd) );
! 39424: dc = sqlite3OsDeviceCharacteristics(pPager->fd);
! 39425: nSector = pPager->sectorSize;
! 39426: szPage = pPager->pageSize;
! 39427:
! 39428: assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
! 39429: assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
! 39430: if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
! 39431: return 0;
! 39432: }
! 39433: }
! 39434:
! 39435: return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
! 39436: }
! 39437: #endif
! 39438:
! 39439: /*
! 39440: ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
! 39441: ** on the cache using a hash function. This is used for testing
! 39442: ** and debugging only.
! 39443: */
! 39444: #ifdef SQLITE_CHECK_PAGES
! 39445: /*
! 39446: ** Return a 32-bit hash of the page data for pPage.
! 39447: */
! 39448: static u32 pager_datahash(int nByte, unsigned char *pData){
! 39449: u32 hash = 0;
! 39450: int i;
! 39451: for(i=0; i<nByte; i++){
! 39452: hash = (hash*1039) + pData[i];
! 39453: }
! 39454: return hash;
! 39455: }
! 39456: static u32 pager_pagehash(PgHdr *pPage){
! 39457: return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
! 39458: }
! 39459: static void pager_set_pagehash(PgHdr *pPage){
! 39460: pPage->pageHash = pager_pagehash(pPage);
! 39461: }
! 39462:
! 39463: /*
! 39464: ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
! 39465: ** is defined, and NDEBUG is not defined, an assert() statement checks
! 39466: ** that the page is either dirty or still matches the calculated page-hash.
! 39467: */
! 39468: #define CHECK_PAGE(x) checkPage(x)
! 39469: static void checkPage(PgHdr *pPg){
! 39470: Pager *pPager = pPg->pPager;
! 39471: assert( pPager->eState!=PAGER_ERROR );
! 39472: assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
! 39473: }
! 39474:
! 39475: #else
! 39476: #define pager_datahash(X,Y) 0
! 39477: #define pager_pagehash(X) 0
! 39478: #define pager_set_pagehash(X)
! 39479: #define CHECK_PAGE(x)
! 39480: #endif /* SQLITE_CHECK_PAGES */
! 39481:
! 39482: /*
! 39483: ** When this is called the journal file for pager pPager must be open.
! 39484: ** This function attempts to read a master journal file name from the
! 39485: ** end of the file and, if successful, copies it into memory supplied
! 39486: ** by the caller. See comments above writeMasterJournal() for the format
! 39487: ** used to store a master journal file name at the end of a journal file.
! 39488: **
! 39489: ** zMaster must point to a buffer of at least nMaster bytes allocated by
! 39490: ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
! 39491: ** enough space to write the master journal name). If the master journal
! 39492: ** name in the journal is longer than nMaster bytes (including a
! 39493: ** nul-terminator), then this is handled as if no master journal name
! 39494: ** were present in the journal.
! 39495: **
! 39496: ** If a master journal file name is present at the end of the journal
! 39497: ** file, then it is copied into the buffer pointed to by zMaster. A
! 39498: ** nul-terminator byte is appended to the buffer following the master
! 39499: ** journal file name.
! 39500: **
! 39501: ** If it is determined that no master journal file name is present
! 39502: ** zMaster[0] is set to 0 and SQLITE_OK returned.
! 39503: **
! 39504: ** If an error occurs while reading from the journal file, an SQLite
! 39505: ** error code is returned.
! 39506: */
! 39507: static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
! 39508: int rc; /* Return code */
! 39509: u32 len; /* Length in bytes of master journal name */
! 39510: i64 szJ; /* Total size in bytes of journal file pJrnl */
! 39511: u32 cksum; /* MJ checksum value read from journal */
! 39512: u32 u; /* Unsigned loop counter */
! 39513: unsigned char aMagic[8]; /* A buffer to hold the magic header */
! 39514: zMaster[0] = '\0';
! 39515:
! 39516: if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
! 39517: || szJ<16
! 39518: || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
! 39519: || len>=nMaster
! 39520: || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
! 39521: || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
! 39522: || memcmp(aMagic, aJournalMagic, 8)
! 39523: || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
! 39524: ){
! 39525: return rc;
! 39526: }
! 39527:
! 39528: /* See if the checksum matches the master journal name */
! 39529: for(u=0; u<len; u++){
! 39530: cksum -= zMaster[u];
! 39531: }
! 39532: if( cksum ){
! 39533: /* If the checksum doesn't add up, then one or more of the disk sectors
! 39534: ** containing the master journal filename is corrupted. This means
! 39535: ** definitely roll back, so just return SQLITE_OK and report a (nul)
! 39536: ** master-journal filename.
! 39537: */
! 39538: len = 0;
! 39539: }
! 39540: zMaster[len] = '\0';
! 39541:
! 39542: return SQLITE_OK;
! 39543: }
! 39544:
! 39545: /*
! 39546: ** Return the offset of the sector boundary at or immediately
! 39547: ** following the value in pPager->journalOff, assuming a sector
! 39548: ** size of pPager->sectorSize bytes.
! 39549: **
! 39550: ** i.e for a sector size of 512:
! 39551: **
! 39552: ** Pager.journalOff Return value
! 39553: ** ---------------------------------------
! 39554: ** 0 0
! 39555: ** 512 512
! 39556: ** 100 512
! 39557: ** 2000 2048
! 39558: **
! 39559: */
! 39560: static i64 journalHdrOffset(Pager *pPager){
! 39561: i64 offset = 0;
! 39562: i64 c = pPager->journalOff;
! 39563: if( c ){
! 39564: offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
! 39565: }
! 39566: assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
! 39567: assert( offset>=c );
! 39568: assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
! 39569: return offset;
! 39570: }
! 39571:
! 39572: /*
! 39573: ** The journal file must be open when this function is called.
! 39574: **
! 39575: ** This function is a no-op if the journal file has not been written to
! 39576: ** within the current transaction (i.e. if Pager.journalOff==0).
! 39577: **
! 39578: ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
! 39579: ** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
! 39580: ** zero the 28-byte header at the start of the journal file. In either case,
! 39581: ** if the pager is not in no-sync mode, sync the journal file immediately
! 39582: ** after writing or truncating it.
! 39583: **
! 39584: ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
! 39585: ** following the truncation or zeroing described above the size of the
! 39586: ** journal file in bytes is larger than this value, then truncate the
! 39587: ** journal file to Pager.journalSizeLimit bytes. The journal file does
! 39588: ** not need to be synced following this operation.
! 39589: **
! 39590: ** If an IO error occurs, abandon processing and return the IO error code.
! 39591: ** Otherwise, return SQLITE_OK.
! 39592: */
! 39593: static int zeroJournalHdr(Pager *pPager, int doTruncate){
! 39594: int rc = SQLITE_OK; /* Return code */
! 39595: assert( isOpen(pPager->jfd) );
! 39596: if( pPager->journalOff ){
! 39597: const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
! 39598:
! 39599: IOTRACE(("JZEROHDR %p\n", pPager))
! 39600: if( doTruncate || iLimit==0 ){
! 39601: rc = sqlite3OsTruncate(pPager->jfd, 0);
! 39602: }else{
! 39603: static const char zeroHdr[28] = {0};
! 39604: rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
! 39605: }
! 39606: if( rc==SQLITE_OK && !pPager->noSync ){
! 39607: rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
! 39608: }
! 39609:
! 39610: /* At this point the transaction is committed but the write lock
! 39611: ** is still held on the file. If there is a size limit configured for
! 39612: ** the persistent journal and the journal file currently consumes more
! 39613: ** space than that limit allows for, truncate it now. There is no need
! 39614: ** to sync the file following this operation.
! 39615: */
! 39616: if( rc==SQLITE_OK && iLimit>0 ){
! 39617: i64 sz;
! 39618: rc = sqlite3OsFileSize(pPager->jfd, &sz);
! 39619: if( rc==SQLITE_OK && sz>iLimit ){
! 39620: rc = sqlite3OsTruncate(pPager->jfd, iLimit);
! 39621: }
! 39622: }
! 39623: }
! 39624: return rc;
! 39625: }
! 39626:
! 39627: /*
! 39628: ** The journal file must be open when this routine is called. A journal
! 39629: ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
! 39630: ** current location.
! 39631: **
! 39632: ** The format for the journal header is as follows:
! 39633: ** - 8 bytes: Magic identifying journal format.
! 39634: ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
! 39635: ** - 4 bytes: Random number used for page hash.
! 39636: ** - 4 bytes: Initial database page count.
! 39637: ** - 4 bytes: Sector size used by the process that wrote this journal.
! 39638: ** - 4 bytes: Database page size.
! 39639: **
! 39640: ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
! 39641: */
! 39642: static int writeJournalHdr(Pager *pPager){
! 39643: int rc = SQLITE_OK; /* Return code */
! 39644: char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
! 39645: u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
! 39646: u32 nWrite; /* Bytes of header sector written */
! 39647: int ii; /* Loop counter */
! 39648:
! 39649: assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
! 39650:
! 39651: if( nHeader>JOURNAL_HDR_SZ(pPager) ){
! 39652: nHeader = JOURNAL_HDR_SZ(pPager);
! 39653: }
! 39654:
! 39655: /* If there are active savepoints and any of them were created
! 39656: ** since the most recent journal header was written, update the
! 39657: ** PagerSavepoint.iHdrOffset fields now.
! 39658: */
! 39659: for(ii=0; ii<pPager->nSavepoint; ii++){
! 39660: if( pPager->aSavepoint[ii].iHdrOffset==0 ){
! 39661: pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
! 39662: }
! 39663: }
! 39664:
! 39665: pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
! 39666:
! 39667: /*
! 39668: ** Write the nRec Field - the number of page records that follow this
! 39669: ** journal header. Normally, zero is written to this value at this time.
! 39670: ** After the records are added to the journal (and the journal synced,
! 39671: ** if in full-sync mode), the zero is overwritten with the true number
! 39672: ** of records (see syncJournal()).
! 39673: **
! 39674: ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
! 39675: ** reading the journal this value tells SQLite to assume that the
! 39676: ** rest of the journal file contains valid page records. This assumption
! 39677: ** is dangerous, as if a failure occurred whilst writing to the journal
! 39678: ** file it may contain some garbage data. There are two scenarios
! 39679: ** where this risk can be ignored:
! 39680: **
! 39681: ** * When the pager is in no-sync mode. Corruption can follow a
! 39682: ** power failure in this case anyway.
! 39683: **
! 39684: ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
! 39685: ** that garbage data is never appended to the journal file.
! 39686: */
! 39687: assert( isOpen(pPager->fd) || pPager->noSync );
! 39688: if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
! 39689: || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
! 39690: ){
! 39691: memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
! 39692: put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
! 39693: }else{
! 39694: memset(zHeader, 0, sizeof(aJournalMagic)+4);
! 39695: }
! 39696:
! 39697: /* The random check-hash initialiser */
! 39698: sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
! 39699: put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
! 39700: /* The initial database size */
! 39701: put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
! 39702: /* The assumed sector size for this process */
! 39703: put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
! 39704:
! 39705: /* The page size */
! 39706: put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
! 39707:
! 39708: /* Initializing the tail of the buffer is not necessary. Everything
! 39709: ** works find if the following memset() is omitted. But initializing
! 39710: ** the memory prevents valgrind from complaining, so we are willing to
! 39711: ** take the performance hit.
! 39712: */
! 39713: memset(&zHeader[sizeof(aJournalMagic)+20], 0,
! 39714: nHeader-(sizeof(aJournalMagic)+20));
! 39715:
! 39716: /* In theory, it is only necessary to write the 28 bytes that the
! 39717: ** journal header consumes to the journal file here. Then increment the
! 39718: ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
! 39719: ** record is written to the following sector (leaving a gap in the file
! 39720: ** that will be implicitly filled in by the OS).
! 39721: **
! 39722: ** However it has been discovered that on some systems this pattern can
! 39723: ** be significantly slower than contiguously writing data to the file,
! 39724: ** even if that means explicitly writing data to the block of
! 39725: ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
! 39726: ** is done.
! 39727: **
! 39728: ** The loop is required here in case the sector-size is larger than the
! 39729: ** database page size. Since the zHeader buffer is only Pager.pageSize
! 39730: ** bytes in size, more than one call to sqlite3OsWrite() may be required
! 39731: ** to populate the entire journal header sector.
! 39732: */
! 39733: for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
! 39734: IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
! 39735: rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
! 39736: assert( pPager->journalHdr <= pPager->journalOff );
! 39737: pPager->journalOff += nHeader;
! 39738: }
! 39739:
! 39740: return rc;
! 39741: }
! 39742:
! 39743: /*
! 39744: ** The journal file must be open when this is called. A journal header file
! 39745: ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
! 39746: ** file. The current location in the journal file is given by
! 39747: ** pPager->journalOff. See comments above function writeJournalHdr() for
! 39748: ** a description of the journal header format.
! 39749: **
! 39750: ** If the header is read successfully, *pNRec is set to the number of
! 39751: ** page records following this header and *pDbSize is set to the size of the
! 39752: ** database before the transaction began, in pages. Also, pPager->cksumInit
! 39753: ** is set to the value read from the journal header. SQLITE_OK is returned
! 39754: ** in this case.
! 39755: **
! 39756: ** If the journal header file appears to be corrupted, SQLITE_DONE is
! 39757: ** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
! 39758: ** cannot be read from the journal file an error code is returned.
! 39759: */
! 39760: static int readJournalHdr(
! 39761: Pager *pPager, /* Pager object */
! 39762: int isHot,
! 39763: i64 journalSize, /* Size of the open journal file in bytes */
! 39764: u32 *pNRec, /* OUT: Value read from the nRec field */
! 39765: u32 *pDbSize /* OUT: Value of original database size field */
! 39766: ){
! 39767: int rc; /* Return code */
! 39768: unsigned char aMagic[8]; /* A buffer to hold the magic header */
! 39769: i64 iHdrOff; /* Offset of journal header being read */
! 39770:
! 39771: assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
! 39772:
! 39773: /* Advance Pager.journalOff to the start of the next sector. If the
! 39774: ** journal file is too small for there to be a header stored at this
! 39775: ** point, return SQLITE_DONE.
! 39776: */
! 39777: pPager->journalOff = journalHdrOffset(pPager);
! 39778: if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
! 39779: return SQLITE_DONE;
! 39780: }
! 39781: iHdrOff = pPager->journalOff;
! 39782:
! 39783: /* Read in the first 8 bytes of the journal header. If they do not match
! 39784: ** the magic string found at the start of each journal header, return
! 39785: ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
! 39786: ** proceed.
! 39787: */
! 39788: if( isHot || iHdrOff!=pPager->journalHdr ){
! 39789: rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
! 39790: if( rc ){
! 39791: return rc;
! 39792: }
! 39793: if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
! 39794: return SQLITE_DONE;
! 39795: }
! 39796: }
! 39797:
! 39798: /* Read the first three 32-bit fields of the journal header: The nRec
! 39799: ** field, the checksum-initializer and the database size at the start
! 39800: ** of the transaction. Return an error code if anything goes wrong.
! 39801: */
! 39802: if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
! 39803: || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
! 39804: || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
! 39805: ){
! 39806: return rc;
! 39807: }
! 39808:
! 39809: if( pPager->journalOff==0 ){
! 39810: u32 iPageSize; /* Page-size field of journal header */
! 39811: u32 iSectorSize; /* Sector-size field of journal header */
! 39812:
! 39813: /* Read the page-size and sector-size journal header fields. */
! 39814: if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
! 39815: || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
! 39816: ){
! 39817: return rc;
! 39818: }
! 39819:
! 39820: /* Versions of SQLite prior to 3.5.8 set the page-size field of the
! 39821: ** journal header to zero. In this case, assume that the Pager.pageSize
! 39822: ** variable is already set to the correct page size.
! 39823: */
! 39824: if( iPageSize==0 ){
! 39825: iPageSize = pPager->pageSize;
! 39826: }
! 39827:
! 39828: /* Check that the values read from the page-size and sector-size fields
! 39829: ** are within range. To be 'in range', both values need to be a power
! 39830: ** of two greater than or equal to 512 or 32, and not greater than their
! 39831: ** respective compile time maximum limits.
! 39832: */
! 39833: if( iPageSize<512 || iSectorSize<32
! 39834: || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
! 39835: || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
! 39836: ){
! 39837: /* If the either the page-size or sector-size in the journal-header is
! 39838: ** invalid, then the process that wrote the journal-header must have
! 39839: ** crashed before the header was synced. In this case stop reading
! 39840: ** the journal file here.
! 39841: */
! 39842: return SQLITE_DONE;
! 39843: }
! 39844:
! 39845: /* Update the page-size to match the value read from the journal.
! 39846: ** Use a testcase() macro to make sure that malloc failure within
! 39847: ** PagerSetPagesize() is tested.
! 39848: */
! 39849: rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
! 39850: testcase( rc!=SQLITE_OK );
! 39851:
! 39852: /* Update the assumed sector-size to match the value used by
! 39853: ** the process that created this journal. If this journal was
! 39854: ** created by a process other than this one, then this routine
! 39855: ** is being called from within pager_playback(). The local value
! 39856: ** of Pager.sectorSize is restored at the end of that routine.
! 39857: */
! 39858: pPager->sectorSize = iSectorSize;
! 39859: }
! 39860:
! 39861: pPager->journalOff += JOURNAL_HDR_SZ(pPager);
! 39862: return rc;
! 39863: }
! 39864:
! 39865:
! 39866: /*
! 39867: ** Write the supplied master journal name into the journal file for pager
! 39868: ** pPager at the current location. The master journal name must be the last
! 39869: ** thing written to a journal file. If the pager is in full-sync mode, the
! 39870: ** journal file descriptor is advanced to the next sector boundary before
! 39871: ** anything is written. The format is:
! 39872: **
! 39873: ** + 4 bytes: PAGER_MJ_PGNO.
! 39874: ** + N bytes: Master journal filename in utf-8.
! 39875: ** + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
! 39876: ** + 4 bytes: Master journal name checksum.
! 39877: ** + 8 bytes: aJournalMagic[].
! 39878: **
! 39879: ** The master journal page checksum is the sum of the bytes in the master
! 39880: ** journal name, where each byte is interpreted as a signed 8-bit integer.
! 39881: **
! 39882: ** If zMaster is a NULL pointer (occurs for a single database transaction),
! 39883: ** this call is a no-op.
! 39884: */
! 39885: static int writeMasterJournal(Pager *pPager, const char *zMaster){
! 39886: int rc; /* Return code */
! 39887: int nMaster; /* Length of string zMaster */
! 39888: i64 iHdrOff; /* Offset of header in journal file */
! 39889: i64 jrnlSize; /* Size of journal file on disk */
! 39890: u32 cksum = 0; /* Checksum of string zMaster */
! 39891:
! 39892: assert( pPager->setMaster==0 );
! 39893: assert( !pagerUseWal(pPager) );
! 39894:
! 39895: if( !zMaster
! 39896: || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
! 39897: || pPager->journalMode==PAGER_JOURNALMODE_OFF
! 39898: ){
! 39899: return SQLITE_OK;
! 39900: }
! 39901: pPager->setMaster = 1;
! 39902: assert( isOpen(pPager->jfd) );
! 39903: assert( pPager->journalHdr <= pPager->journalOff );
! 39904:
! 39905: /* Calculate the length in bytes and the checksum of zMaster */
! 39906: for(nMaster=0; zMaster[nMaster]; nMaster++){
! 39907: cksum += zMaster[nMaster];
! 39908: }
! 39909:
! 39910: /* If in full-sync mode, advance to the next disk sector before writing
! 39911: ** the master journal name. This is in case the previous page written to
! 39912: ** the journal has already been synced.
! 39913: */
! 39914: if( pPager->fullSync ){
! 39915: pPager->journalOff = journalHdrOffset(pPager);
! 39916: }
! 39917: iHdrOff = pPager->journalOff;
! 39918:
! 39919: /* Write the master journal data to the end of the journal file. If
! 39920: ** an error occurs, return the error code to the caller.
! 39921: */
! 39922: if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
! 39923: || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
! 39924: || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
! 39925: || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
! 39926: || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
! 39927: ){
! 39928: return rc;
! 39929: }
! 39930: pPager->journalOff += (nMaster+20);
! 39931:
! 39932: /* If the pager is in peristent-journal mode, then the physical
! 39933: ** journal-file may extend past the end of the master-journal name
! 39934: ** and 8 bytes of magic data just written to the file. This is
! 39935: ** dangerous because the code to rollback a hot-journal file
! 39936: ** will not be able to find the master-journal name to determine
! 39937: ** whether or not the journal is hot.
! 39938: **
! 39939: ** Easiest thing to do in this scenario is to truncate the journal
! 39940: ** file to the required size.
! 39941: */
! 39942: if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
! 39943: && jrnlSize>pPager->journalOff
! 39944: ){
! 39945: rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
! 39946: }
! 39947: return rc;
! 39948: }
! 39949:
! 39950: /*
! 39951: ** Find a page in the hash table given its page number. Return
! 39952: ** a pointer to the page or NULL if the requested page is not
! 39953: ** already in memory.
! 39954: */
! 39955: static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
! 39956: PgHdr *p; /* Return value */
! 39957:
! 39958: /* It is not possible for a call to PcacheFetch() with createFlag==0 to
! 39959: ** fail, since no attempt to allocate dynamic memory will be made.
! 39960: */
! 39961: (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
! 39962: return p;
! 39963: }
! 39964:
! 39965: /*
! 39966: ** Discard the entire contents of the in-memory page-cache.
! 39967: */
! 39968: static void pager_reset(Pager *pPager){
! 39969: sqlite3BackupRestart(pPager->pBackup);
! 39970: sqlite3PcacheClear(pPager->pPCache);
! 39971: }
! 39972:
! 39973: /*
! 39974: ** Free all structures in the Pager.aSavepoint[] array and set both
! 39975: ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
! 39976: ** if it is open and the pager is not in exclusive mode.
! 39977: */
! 39978: static void releaseAllSavepoints(Pager *pPager){
! 39979: int ii; /* Iterator for looping through Pager.aSavepoint */
! 39980: for(ii=0; ii<pPager->nSavepoint; ii++){
! 39981: sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
! 39982: }
! 39983: if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
! 39984: sqlite3OsClose(pPager->sjfd);
! 39985: }
! 39986: sqlite3_free(pPager->aSavepoint);
! 39987: pPager->aSavepoint = 0;
! 39988: pPager->nSavepoint = 0;
! 39989: pPager->nSubRec = 0;
! 39990: }
! 39991:
! 39992: /*
! 39993: ** Set the bit number pgno in the PagerSavepoint.pInSavepoint
! 39994: ** bitvecs of all open savepoints. Return SQLITE_OK if successful
! 39995: ** or SQLITE_NOMEM if a malloc failure occurs.
! 39996: */
! 39997: static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
! 39998: int ii; /* Loop counter */
! 39999: int rc = SQLITE_OK; /* Result code */
! 40000:
! 40001: for(ii=0; ii<pPager->nSavepoint; ii++){
! 40002: PagerSavepoint *p = &pPager->aSavepoint[ii];
! 40003: if( pgno<=p->nOrig ){
! 40004: rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
! 40005: testcase( rc==SQLITE_NOMEM );
! 40006: assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
! 40007: }
! 40008: }
! 40009: return rc;
! 40010: }
! 40011:
! 40012: /*
! 40013: ** This function is a no-op if the pager is in exclusive mode and not
! 40014: ** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
! 40015: ** state.
! 40016: **
! 40017: ** If the pager is not in exclusive-access mode, the database file is
! 40018: ** completely unlocked. If the file is unlocked and the file-system does
! 40019: ** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
! 40020: ** closed (if it is open).
! 40021: **
! 40022: ** If the pager is in ERROR state when this function is called, the
! 40023: ** contents of the pager cache are discarded before switching back to
! 40024: ** the OPEN state. Regardless of whether the pager is in exclusive-mode
! 40025: ** or not, any journal file left in the file-system will be treated
! 40026: ** as a hot-journal and rolled back the next time a read-transaction
! 40027: ** is opened (by this or by any other connection).
! 40028: */
! 40029: static void pager_unlock(Pager *pPager){
! 40030:
! 40031: assert( pPager->eState==PAGER_READER
! 40032: || pPager->eState==PAGER_OPEN
! 40033: || pPager->eState==PAGER_ERROR
! 40034: );
! 40035:
! 40036: sqlite3BitvecDestroy(pPager->pInJournal);
! 40037: pPager->pInJournal = 0;
! 40038: releaseAllSavepoints(pPager);
! 40039:
! 40040: if( pagerUseWal(pPager) ){
! 40041: assert( !isOpen(pPager->jfd) );
! 40042: sqlite3WalEndReadTransaction(pPager->pWal);
! 40043: pPager->eState = PAGER_OPEN;
! 40044: }else if( !pPager->exclusiveMode ){
! 40045: int rc; /* Error code returned by pagerUnlockDb() */
! 40046: int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
! 40047:
! 40048: /* If the operating system support deletion of open files, then
! 40049: ** close the journal file when dropping the database lock. Otherwise
! 40050: ** another connection with journal_mode=delete might delete the file
! 40051: ** out from under us.
! 40052: */
! 40053: assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
! 40054: assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
! 40055: assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
! 40056: assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
! 40057: assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
! 40058: assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
! 40059: if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
! 40060: || 1!=(pPager->journalMode & 5)
! 40061: ){
! 40062: sqlite3OsClose(pPager->jfd);
! 40063: }
! 40064:
! 40065: /* If the pager is in the ERROR state and the call to unlock the database
! 40066: ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
! 40067: ** above the #define for UNKNOWN_LOCK for an explanation of why this
! 40068: ** is necessary.
! 40069: */
! 40070: rc = pagerUnlockDb(pPager, NO_LOCK);
! 40071: if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
! 40072: pPager->eLock = UNKNOWN_LOCK;
! 40073: }
! 40074:
! 40075: /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
! 40076: ** without clearing the error code. This is intentional - the error
! 40077: ** code is cleared and the cache reset in the block below.
! 40078: */
! 40079: assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
! 40080: pPager->changeCountDone = 0;
! 40081: pPager->eState = PAGER_OPEN;
! 40082: }
! 40083:
! 40084: /* If Pager.errCode is set, the contents of the pager cache cannot be
! 40085: ** trusted. Now that there are no outstanding references to the pager,
! 40086: ** it can safely move back to PAGER_OPEN state. This happens in both
! 40087: ** normal and exclusive-locking mode.
! 40088: */
! 40089: if( pPager->errCode ){
! 40090: assert( !MEMDB );
! 40091: pager_reset(pPager);
! 40092: pPager->changeCountDone = pPager->tempFile;
! 40093: pPager->eState = PAGER_OPEN;
! 40094: pPager->errCode = SQLITE_OK;
! 40095: }
! 40096:
! 40097: pPager->journalOff = 0;
! 40098: pPager->journalHdr = 0;
! 40099: pPager->setMaster = 0;
! 40100: }
! 40101:
! 40102: /*
! 40103: ** This function is called whenever an IOERR or FULL error that requires
! 40104: ** the pager to transition into the ERROR state may ahve occurred.
! 40105: ** The first argument is a pointer to the pager structure, the second
! 40106: ** the error-code about to be returned by a pager API function. The
! 40107: ** value returned is a copy of the second argument to this function.
! 40108: **
! 40109: ** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
! 40110: ** IOERR sub-codes, the pager enters the ERROR state and the error code
! 40111: ** is stored in Pager.errCode. While the pager remains in the ERROR state,
! 40112: ** all major API calls on the Pager will immediately return Pager.errCode.
! 40113: **
! 40114: ** The ERROR state indicates that the contents of the pager-cache
! 40115: ** cannot be trusted. This state can be cleared by completely discarding
! 40116: ** the contents of the pager-cache. If a transaction was active when
! 40117: ** the persistent error occurred, then the rollback journal may need
! 40118: ** to be replayed to restore the contents of the database file (as if
! 40119: ** it were a hot-journal).
! 40120: */
! 40121: static int pager_error(Pager *pPager, int rc){
! 40122: int rc2 = rc & 0xff;
! 40123: assert( rc==SQLITE_OK || !MEMDB );
! 40124: assert(
! 40125: pPager->errCode==SQLITE_FULL ||
! 40126: pPager->errCode==SQLITE_OK ||
! 40127: (pPager->errCode & 0xff)==SQLITE_IOERR
! 40128: );
! 40129: if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
! 40130: pPager->errCode = rc;
! 40131: pPager->eState = PAGER_ERROR;
! 40132: }
! 40133: return rc;
! 40134: }
! 40135:
! 40136: /*
! 40137: ** This routine ends a transaction. A transaction is usually ended by
! 40138: ** either a COMMIT or a ROLLBACK operation. This routine may be called
! 40139: ** after rollback of a hot-journal, or if an error occurs while opening
! 40140: ** the journal file or writing the very first journal-header of a
! 40141: ** database transaction.
! 40142: **
! 40143: ** This routine is never called in PAGER_ERROR state. If it is called
! 40144: ** in PAGER_NONE or PAGER_SHARED state and the lock held is less
! 40145: ** exclusive than a RESERVED lock, it is a no-op.
! 40146: **
! 40147: ** Otherwise, any active savepoints are released.
! 40148: **
! 40149: ** If the journal file is open, then it is "finalized". Once a journal
! 40150: ** file has been finalized it is not possible to use it to roll back a
! 40151: ** transaction. Nor will it be considered to be a hot-journal by this
! 40152: ** or any other database connection. Exactly how a journal is finalized
! 40153: ** depends on whether or not the pager is running in exclusive mode and
! 40154: ** the current journal-mode (Pager.journalMode value), as follows:
! 40155: **
! 40156: ** journalMode==MEMORY
! 40157: ** Journal file descriptor is simply closed. This destroys an
! 40158: ** in-memory journal.
! 40159: **
! 40160: ** journalMode==TRUNCATE
! 40161: ** Journal file is truncated to zero bytes in size.
! 40162: **
! 40163: ** journalMode==PERSIST
! 40164: ** The first 28 bytes of the journal file are zeroed. This invalidates
! 40165: ** the first journal header in the file, and hence the entire journal
! 40166: ** file. An invalid journal file cannot be rolled back.
! 40167: **
! 40168: ** journalMode==DELETE
! 40169: ** The journal file is closed and deleted using sqlite3OsDelete().
! 40170: **
! 40171: ** If the pager is running in exclusive mode, this method of finalizing
! 40172: ** the journal file is never used. Instead, if the journalMode is
! 40173: ** DELETE and the pager is in exclusive mode, the method described under
! 40174: ** journalMode==PERSIST is used instead.
! 40175: **
! 40176: ** After the journal is finalized, the pager moves to PAGER_READER state.
! 40177: ** If running in non-exclusive rollback mode, the lock on the file is
! 40178: ** downgraded to a SHARED_LOCK.
! 40179: **
! 40180: ** SQLITE_OK is returned if no error occurs. If an error occurs during
! 40181: ** any of the IO operations to finalize the journal file or unlock the
! 40182: ** database then the IO error code is returned to the user. If the
! 40183: ** operation to finalize the journal file fails, then the code still
! 40184: ** tries to unlock the database file if not in exclusive mode. If the
! 40185: ** unlock operation fails as well, then the first error code related
! 40186: ** to the first error encountered (the journal finalization one) is
! 40187: ** returned.
! 40188: */
! 40189: static int pager_end_transaction(Pager *pPager, int hasMaster){
! 40190: int rc = SQLITE_OK; /* Error code from journal finalization operation */
! 40191: int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
! 40192:
! 40193: /* Do nothing if the pager does not have an open write transaction
! 40194: ** or at least a RESERVED lock. This function may be called when there
! 40195: ** is no write-transaction active but a RESERVED or greater lock is
! 40196: ** held under two circumstances:
! 40197: **
! 40198: ** 1. After a successful hot-journal rollback, it is called with
! 40199: ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
! 40200: **
! 40201: ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
! 40202: ** lock switches back to locking_mode=normal and then executes a
! 40203: ** read-transaction, this function is called with eState==PAGER_READER
! 40204: ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
! 40205: */
! 40206: assert( assert_pager_state(pPager) );
! 40207: assert( pPager->eState!=PAGER_ERROR );
! 40208: if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
! 40209: return SQLITE_OK;
! 40210: }
! 40211:
! 40212: releaseAllSavepoints(pPager);
! 40213: assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
! 40214: if( isOpen(pPager->jfd) ){
! 40215: assert( !pagerUseWal(pPager) );
! 40216:
! 40217: /* Finalize the journal file. */
! 40218: if( sqlite3IsMemJournal(pPager->jfd) ){
! 40219: assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
! 40220: sqlite3OsClose(pPager->jfd);
! 40221: }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
! 40222: if( pPager->journalOff==0 ){
! 40223: rc = SQLITE_OK;
! 40224: }else{
! 40225: rc = sqlite3OsTruncate(pPager->jfd, 0);
! 40226: }
! 40227: pPager->journalOff = 0;
! 40228: }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
! 40229: || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
! 40230: ){
! 40231: rc = zeroJournalHdr(pPager, hasMaster);
! 40232: pPager->journalOff = 0;
! 40233: }else{
! 40234: /* This branch may be executed with Pager.journalMode==MEMORY if
! 40235: ** a hot-journal was just rolled back. In this case the journal
! 40236: ** file should be closed and deleted. If this connection writes to
! 40237: ** the database file, it will do so using an in-memory journal.
! 40238: */
! 40239: assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
! 40240: || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
! 40241: || pPager->journalMode==PAGER_JOURNALMODE_WAL
! 40242: );
! 40243: sqlite3OsClose(pPager->jfd);
! 40244: if( !pPager->tempFile ){
! 40245: rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
! 40246: }
! 40247: }
! 40248: }
! 40249:
! 40250: #ifdef SQLITE_CHECK_PAGES
! 40251: sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
! 40252: if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
! 40253: PgHdr *p = pager_lookup(pPager, 1);
! 40254: if( p ){
! 40255: p->pageHash = 0;
! 40256: sqlite3PagerUnref(p);
! 40257: }
! 40258: }
! 40259: #endif
! 40260:
! 40261: sqlite3BitvecDestroy(pPager->pInJournal);
! 40262: pPager->pInJournal = 0;
! 40263: pPager->nRec = 0;
! 40264: sqlite3PcacheCleanAll(pPager->pPCache);
! 40265: sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
! 40266:
! 40267: if( pagerUseWal(pPager) ){
! 40268: /* Drop the WAL write-lock, if any. Also, if the connection was in
! 40269: ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
! 40270: ** lock held on the database file.
! 40271: */
! 40272: rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
! 40273: assert( rc2==SQLITE_OK );
! 40274: }
! 40275: if( !pPager->exclusiveMode
! 40276: && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
! 40277: ){
! 40278: rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
! 40279: pPager->changeCountDone = 0;
! 40280: }
! 40281: pPager->eState = PAGER_READER;
! 40282: pPager->setMaster = 0;
! 40283:
! 40284: return (rc==SQLITE_OK?rc2:rc);
! 40285: }
! 40286:
! 40287: /*
! 40288: ** Execute a rollback if a transaction is active and unlock the
! 40289: ** database file.
! 40290: **
! 40291: ** If the pager has already entered the ERROR state, do not attempt
! 40292: ** the rollback at this time. Instead, pager_unlock() is called. The
! 40293: ** call to pager_unlock() will discard all in-memory pages, unlock
! 40294: ** the database file and move the pager back to OPEN state. If this
! 40295: ** means that there is a hot-journal left in the file-system, the next
! 40296: ** connection to obtain a shared lock on the pager (which may be this one)
! 40297: ** will roll it back.
! 40298: **
! 40299: ** If the pager has not already entered the ERROR state, but an IO or
! 40300: ** malloc error occurs during a rollback, then this will itself cause
! 40301: ** the pager to enter the ERROR state. Which will be cleared by the
! 40302: ** call to pager_unlock(), as described above.
! 40303: */
! 40304: static void pagerUnlockAndRollback(Pager *pPager){
! 40305: if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
! 40306: assert( assert_pager_state(pPager) );
! 40307: if( pPager->eState>=PAGER_WRITER_LOCKED ){
! 40308: sqlite3BeginBenignMalloc();
! 40309: sqlite3PagerRollback(pPager);
! 40310: sqlite3EndBenignMalloc();
! 40311: }else if( !pPager->exclusiveMode ){
! 40312: assert( pPager->eState==PAGER_READER );
! 40313: pager_end_transaction(pPager, 0);
! 40314: }
! 40315: }
! 40316: pager_unlock(pPager);
! 40317: }
! 40318:
! 40319: /*
! 40320: ** Parameter aData must point to a buffer of pPager->pageSize bytes
! 40321: ** of data. Compute and return a checksum based ont the contents of the
! 40322: ** page of data and the current value of pPager->cksumInit.
! 40323: **
! 40324: ** This is not a real checksum. It is really just the sum of the
! 40325: ** random initial value (pPager->cksumInit) and every 200th byte
! 40326: ** of the page data, starting with byte offset (pPager->pageSize%200).
! 40327: ** Each byte is interpreted as an 8-bit unsigned integer.
! 40328: **
! 40329: ** Changing the formula used to compute this checksum results in an
! 40330: ** incompatible journal file format.
! 40331: **
! 40332: ** If journal corruption occurs due to a power failure, the most likely
! 40333: ** scenario is that one end or the other of the record will be changed.
! 40334: ** It is much less likely that the two ends of the journal record will be
! 40335: ** correct and the middle be corrupt. Thus, this "checksum" scheme,
! 40336: ** though fast and simple, catches the mostly likely kind of corruption.
! 40337: */
! 40338: static u32 pager_cksum(Pager *pPager, const u8 *aData){
! 40339: u32 cksum = pPager->cksumInit; /* Checksum value to return */
! 40340: int i = pPager->pageSize-200; /* Loop counter */
! 40341: while( i>0 ){
! 40342: cksum += aData[i];
! 40343: i -= 200;
! 40344: }
! 40345: return cksum;
! 40346: }
! 40347:
! 40348: /*
! 40349: ** Report the current page size and number of reserved bytes back
! 40350: ** to the codec.
! 40351: */
! 40352: #ifdef SQLITE_HAS_CODEC
! 40353: static void pagerReportSize(Pager *pPager){
! 40354: if( pPager->xCodecSizeChng ){
! 40355: pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
! 40356: (int)pPager->nReserve);
! 40357: }
! 40358: }
! 40359: #else
! 40360: # define pagerReportSize(X) /* No-op if we do not support a codec */
! 40361: #endif
! 40362:
! 40363: /*
! 40364: ** Read a single page from either the journal file (if isMainJrnl==1) or
! 40365: ** from the sub-journal (if isMainJrnl==0) and playback that page.
! 40366: ** The page begins at offset *pOffset into the file. The *pOffset
! 40367: ** value is increased to the start of the next page in the journal.
! 40368: **
! 40369: ** The main rollback journal uses checksums - the statement journal does
! 40370: ** not.
! 40371: **
! 40372: ** If the page number of the page record read from the (sub-)journal file
! 40373: ** is greater than the current value of Pager.dbSize, then playback is
! 40374: ** skipped and SQLITE_OK is returned.
! 40375: **
! 40376: ** If pDone is not NULL, then it is a record of pages that have already
! 40377: ** been played back. If the page at *pOffset has already been played back
! 40378: ** (if the corresponding pDone bit is set) then skip the playback.
! 40379: ** Make sure the pDone bit corresponding to the *pOffset page is set
! 40380: ** prior to returning.
! 40381: **
! 40382: ** If the page record is successfully read from the (sub-)journal file
! 40383: ** and played back, then SQLITE_OK is returned. If an IO error occurs
! 40384: ** while reading the record from the (sub-)journal file or while writing
! 40385: ** to the database file, then the IO error code is returned. If data
! 40386: ** is successfully read from the (sub-)journal file but appears to be
! 40387: ** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
! 40388: ** two circumstances:
! 40389: **
! 40390: ** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
! 40391: ** * If the record is being rolled back from the main journal file
! 40392: ** and the checksum field does not match the record content.
! 40393: **
! 40394: ** Neither of these two scenarios are possible during a savepoint rollback.
! 40395: **
! 40396: ** If this is a savepoint rollback, then memory may have to be dynamically
! 40397: ** allocated by this function. If this is the case and an allocation fails,
! 40398: ** SQLITE_NOMEM is returned.
! 40399: */
! 40400: static int pager_playback_one_page(
! 40401: Pager *pPager, /* The pager being played back */
! 40402: i64 *pOffset, /* Offset of record to playback */
! 40403: Bitvec *pDone, /* Bitvec of pages already played back */
! 40404: int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
! 40405: int isSavepnt /* True for a savepoint rollback */
! 40406: ){
! 40407: int rc;
! 40408: PgHdr *pPg; /* An existing page in the cache */
! 40409: Pgno pgno; /* The page number of a page in journal */
! 40410: u32 cksum; /* Checksum used for sanity checking */
! 40411: char *aData; /* Temporary storage for the page */
! 40412: sqlite3_file *jfd; /* The file descriptor for the journal file */
! 40413: int isSynced; /* True if journal page is synced */
! 40414:
! 40415: assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
! 40416: assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
! 40417: assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
! 40418: assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
! 40419:
! 40420: aData = pPager->pTmpSpace;
! 40421: assert( aData ); /* Temp storage must have already been allocated */
! 40422: assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
! 40423:
! 40424: /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
! 40425: ** or savepoint rollback done at the request of the caller) or this is
! 40426: ** a hot-journal rollback. If it is a hot-journal rollback, the pager
! 40427: ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
! 40428: ** only reads from the main journal, not the sub-journal.
! 40429: */
! 40430: assert( pPager->eState>=PAGER_WRITER_CACHEMOD
! 40431: || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
! 40432: );
! 40433: assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
! 40434:
! 40435: /* Read the page number and page data from the journal or sub-journal
! 40436: ** file. Return an error code to the caller if an IO error occurs.
! 40437: */
! 40438: jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
! 40439: rc = read32bits(jfd, *pOffset, &pgno);
! 40440: if( rc!=SQLITE_OK ) return rc;
! 40441: rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
! 40442: if( rc!=SQLITE_OK ) return rc;
! 40443: *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
! 40444:
! 40445: /* Sanity checking on the page. This is more important that I originally
! 40446: ** thought. If a power failure occurs while the journal is being written,
! 40447: ** it could cause invalid data to be written into the journal. We need to
! 40448: ** detect this invalid data (with high probability) and ignore it.
! 40449: */
! 40450: if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
! 40451: assert( !isSavepnt );
! 40452: return SQLITE_DONE;
! 40453: }
! 40454: if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
! 40455: return SQLITE_OK;
! 40456: }
! 40457: if( isMainJrnl ){
! 40458: rc = read32bits(jfd, (*pOffset)-4, &cksum);
! 40459: if( rc ) return rc;
! 40460: if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
! 40461: return SQLITE_DONE;
! 40462: }
! 40463: }
! 40464:
! 40465: /* If this page has already been played by before during the current
! 40466: ** rollback, then don't bother to play it back again.
! 40467: */
! 40468: if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
! 40469: return rc;
! 40470: }
! 40471:
! 40472: /* When playing back page 1, restore the nReserve setting
! 40473: */
! 40474: if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
! 40475: pPager->nReserve = ((u8*)aData)[20];
! 40476: pagerReportSize(pPager);
! 40477: }
! 40478:
! 40479: /* If the pager is in CACHEMOD state, then there must be a copy of this
! 40480: ** page in the pager cache. In this case just update the pager cache,
! 40481: ** not the database file. The page is left marked dirty in this case.
! 40482: **
! 40483: ** An exception to the above rule: If the database is in no-sync mode
! 40484: ** and a page is moved during an incremental vacuum then the page may
! 40485: ** not be in the pager cache. Later: if a malloc() or IO error occurs
! 40486: ** during a Movepage() call, then the page may not be in the cache
! 40487: ** either. So the condition described in the above paragraph is not
! 40488: ** assert()able.
! 40489: **
! 40490: ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
! 40491: ** pager cache if it exists and the main file. The page is then marked
! 40492: ** not dirty. Since this code is only executed in PAGER_OPEN state for
! 40493: ** a hot-journal rollback, it is guaranteed that the page-cache is empty
! 40494: ** if the pager is in OPEN state.
! 40495: **
! 40496: ** Ticket #1171: The statement journal might contain page content that is
! 40497: ** different from the page content at the start of the transaction.
! 40498: ** This occurs when a page is changed prior to the start of a statement
! 40499: ** then changed again within the statement. When rolling back such a
! 40500: ** statement we must not write to the original database unless we know
! 40501: ** for certain that original page contents are synced into the main rollback
! 40502: ** journal. Otherwise, a power loss might leave modified data in the
! 40503: ** database file without an entry in the rollback journal that can
! 40504: ** restore the database to its original form. Two conditions must be
! 40505: ** met before writing to the database files. (1) the database must be
! 40506: ** locked. (2) we know that the original page content is fully synced
! 40507: ** in the main journal either because the page is not in cache or else
! 40508: ** the page is marked as needSync==0.
! 40509: **
! 40510: ** 2008-04-14: When attempting to vacuum a corrupt database file, it
! 40511: ** is possible to fail a statement on a database that does not yet exist.
! 40512: ** Do not attempt to write if database file has never been opened.
! 40513: */
! 40514: if( pagerUseWal(pPager) ){
! 40515: pPg = 0;
! 40516: }else{
! 40517: pPg = pager_lookup(pPager, pgno);
! 40518: }
! 40519: assert( pPg || !MEMDB );
! 40520: assert( pPager->eState!=PAGER_OPEN || pPg==0 );
! 40521: PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
! 40522: PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
! 40523: (isMainJrnl?"main-journal":"sub-journal")
! 40524: ));
! 40525: if( isMainJrnl ){
! 40526: isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
! 40527: }else{
! 40528: isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
! 40529: }
! 40530: if( isOpen(pPager->fd)
! 40531: && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
! 40532: && isSynced
! 40533: ){
! 40534: i64 ofst = (pgno-1)*(i64)pPager->pageSize;
! 40535: testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
! 40536: assert( !pagerUseWal(pPager) );
! 40537: rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
! 40538: if( pgno>pPager->dbFileSize ){
! 40539: pPager->dbFileSize = pgno;
! 40540: }
! 40541: if( pPager->pBackup ){
! 40542: CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
! 40543: sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
! 40544: CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
! 40545: }
! 40546: }else if( !isMainJrnl && pPg==0 ){
! 40547: /* If this is a rollback of a savepoint and data was not written to
! 40548: ** the database and the page is not in-memory, there is a potential
! 40549: ** problem. When the page is next fetched by the b-tree layer, it
! 40550: ** will be read from the database file, which may or may not be
! 40551: ** current.
! 40552: **
! 40553: ** There are a couple of different ways this can happen. All are quite
! 40554: ** obscure. When running in synchronous mode, this can only happen
! 40555: ** if the page is on the free-list at the start of the transaction, then
! 40556: ** populated, then moved using sqlite3PagerMovepage().
! 40557: **
! 40558: ** The solution is to add an in-memory page to the cache containing
! 40559: ** the data just read from the sub-journal. Mark the page as dirty
! 40560: ** and if the pager requires a journal-sync, then mark the page as
! 40561: ** requiring a journal-sync before it is written.
! 40562: */
! 40563: assert( isSavepnt );
! 40564: assert( pPager->doNotSpill==0 );
! 40565: pPager->doNotSpill++;
! 40566: rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
! 40567: assert( pPager->doNotSpill==1 );
! 40568: pPager->doNotSpill--;
! 40569: if( rc!=SQLITE_OK ) return rc;
! 40570: pPg->flags &= ~PGHDR_NEED_READ;
! 40571: sqlite3PcacheMakeDirty(pPg);
! 40572: }
! 40573: if( pPg ){
! 40574: /* No page should ever be explicitly rolled back that is in use, except
! 40575: ** for page 1 which is held in use in order to keep the lock on the
! 40576: ** database active. However such a page may be rolled back as a result
! 40577: ** of an internal error resulting in an automatic call to
! 40578: ** sqlite3PagerRollback().
! 40579: */
! 40580: void *pData;
! 40581: pData = pPg->pData;
! 40582: memcpy(pData, (u8*)aData, pPager->pageSize);
! 40583: pPager->xReiniter(pPg);
! 40584: if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
! 40585: /* If the contents of this page were just restored from the main
! 40586: ** journal file, then its content must be as they were when the
! 40587: ** transaction was first opened. In this case we can mark the page
! 40588: ** as clean, since there will be no need to write it out to the
! 40589: ** database.
! 40590: **
! 40591: ** There is one exception to this rule. If the page is being rolled
! 40592: ** back as part of a savepoint (or statement) rollback from an
! 40593: ** unsynced portion of the main journal file, then it is not safe
! 40594: ** to mark the page as clean. This is because marking the page as
! 40595: ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
! 40596: ** already in the journal file (recorded in Pager.pInJournal) and
! 40597: ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
! 40598: ** again within this transaction, it will be marked as dirty but
! 40599: ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
! 40600: ** be written out into the database file before its journal file
! 40601: ** segment is synced. If a crash occurs during or following this,
! 40602: ** database corruption may ensue.
! 40603: */
! 40604: assert( !pagerUseWal(pPager) );
! 40605: sqlite3PcacheMakeClean(pPg);
! 40606: }
! 40607: pager_set_pagehash(pPg);
! 40608:
! 40609: /* If this was page 1, then restore the value of Pager.dbFileVers.
! 40610: ** Do this before any decoding. */
! 40611: if( pgno==1 ){
! 40612: memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
! 40613: }
! 40614:
! 40615: /* Decode the page just read from disk */
! 40616: CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
! 40617: sqlite3PcacheRelease(pPg);
! 40618: }
! 40619: return rc;
! 40620: }
! 40621:
! 40622: /*
! 40623: ** Parameter zMaster is the name of a master journal file. A single journal
! 40624: ** file that referred to the master journal file has just been rolled back.
! 40625: ** This routine checks if it is possible to delete the master journal file,
! 40626: ** and does so if it is.
! 40627: **
! 40628: ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
! 40629: ** available for use within this function.
! 40630: **
! 40631: ** When a master journal file is created, it is populated with the names
! 40632: ** of all of its child journals, one after another, formatted as utf-8
! 40633: ** encoded text. The end of each child journal file is marked with a
! 40634: ** nul-terminator byte (0x00). i.e. the entire contents of a master journal
! 40635: ** file for a transaction involving two databases might be:
! 40636: **
! 40637: ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
! 40638: **
! 40639: ** A master journal file may only be deleted once all of its child
! 40640: ** journals have been rolled back.
! 40641: **
! 40642: ** This function reads the contents of the master-journal file into
! 40643: ** memory and loops through each of the child journal names. For
! 40644: ** each child journal, it checks if:
! 40645: **
! 40646: ** * if the child journal exists, and if so
! 40647: ** * if the child journal contains a reference to master journal
! 40648: ** file zMaster
! 40649: **
! 40650: ** If a child journal can be found that matches both of the criteria
! 40651: ** above, this function returns without doing anything. Otherwise, if
! 40652: ** no such child journal can be found, file zMaster is deleted from
! 40653: ** the file-system using sqlite3OsDelete().
! 40654: **
! 40655: ** If an IO error within this function, an error code is returned. This
! 40656: ** function allocates memory by calling sqlite3Malloc(). If an allocation
! 40657: ** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
! 40658: ** occur, SQLITE_OK is returned.
! 40659: **
! 40660: ** TODO: This function allocates a single block of memory to load
! 40661: ** the entire contents of the master journal file. This could be
! 40662: ** a couple of kilobytes or so - potentially larger than the page
! 40663: ** size.
! 40664: */
! 40665: static int pager_delmaster(Pager *pPager, const char *zMaster){
! 40666: sqlite3_vfs *pVfs = pPager->pVfs;
! 40667: int rc; /* Return code */
! 40668: sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */
! 40669: sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
! 40670: char *zMasterJournal = 0; /* Contents of master journal file */
! 40671: i64 nMasterJournal; /* Size of master journal file */
! 40672: char *zJournal; /* Pointer to one journal within MJ file */
! 40673: char *zMasterPtr; /* Space to hold MJ filename from a journal file */
! 40674: int nMasterPtr; /* Amount of space allocated to zMasterPtr[] */
! 40675:
! 40676: /* Allocate space for both the pJournal and pMaster file descriptors.
! 40677: ** If successful, open the master journal file for reading.
! 40678: */
! 40679: pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
! 40680: pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
! 40681: if( !pMaster ){
! 40682: rc = SQLITE_NOMEM;
! 40683: }else{
! 40684: const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
! 40685: rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
! 40686: }
! 40687: if( rc!=SQLITE_OK ) goto delmaster_out;
! 40688:
! 40689: /* Load the entire master journal file into space obtained from
! 40690: ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain
! 40691: ** sufficient space (in zMasterPtr) to hold the names of master
! 40692: ** journal files extracted from regular rollback-journals.
! 40693: */
! 40694: rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
! 40695: if( rc!=SQLITE_OK ) goto delmaster_out;
! 40696: nMasterPtr = pVfs->mxPathname+1;
! 40697: zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
! 40698: if( !zMasterJournal ){
! 40699: rc = SQLITE_NOMEM;
! 40700: goto delmaster_out;
! 40701: }
! 40702: zMasterPtr = &zMasterJournal[nMasterJournal+1];
! 40703: rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
! 40704: if( rc!=SQLITE_OK ) goto delmaster_out;
! 40705: zMasterJournal[nMasterJournal] = 0;
! 40706:
! 40707: zJournal = zMasterJournal;
! 40708: while( (zJournal-zMasterJournal)<nMasterJournal ){
! 40709: int exists;
! 40710: rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
! 40711: if( rc!=SQLITE_OK ){
! 40712: goto delmaster_out;
! 40713: }
! 40714: if( exists ){
! 40715: /* One of the journals pointed to by the master journal exists.
! 40716: ** Open it and check if it points at the master journal. If
! 40717: ** so, return without deleting the master journal file.
! 40718: */
! 40719: int c;
! 40720: int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
! 40721: rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
! 40722: if( rc!=SQLITE_OK ){
! 40723: goto delmaster_out;
! 40724: }
! 40725:
! 40726: rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
! 40727: sqlite3OsClose(pJournal);
! 40728: if( rc!=SQLITE_OK ){
! 40729: goto delmaster_out;
! 40730: }
! 40731:
! 40732: c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
! 40733: if( c ){
! 40734: /* We have a match. Do not delete the master journal file. */
! 40735: goto delmaster_out;
! 40736: }
! 40737: }
! 40738: zJournal += (sqlite3Strlen30(zJournal)+1);
! 40739: }
! 40740:
! 40741: sqlite3OsClose(pMaster);
! 40742: rc = sqlite3OsDelete(pVfs, zMaster, 0);
! 40743:
! 40744: delmaster_out:
! 40745: sqlite3_free(zMasterJournal);
! 40746: if( pMaster ){
! 40747: sqlite3OsClose(pMaster);
! 40748: assert( !isOpen(pJournal) );
! 40749: sqlite3_free(pMaster);
! 40750: }
! 40751: return rc;
! 40752: }
! 40753:
! 40754:
! 40755: /*
! 40756: ** This function is used to change the actual size of the database
! 40757: ** file in the file-system. This only happens when committing a transaction,
! 40758: ** or rolling back a transaction (including rolling back a hot-journal).
! 40759: **
! 40760: ** If the main database file is not open, or the pager is not in either
! 40761: ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
! 40762: ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
! 40763: ** If the file on disk is currently larger than nPage pages, then use the VFS
! 40764: ** xTruncate() method to truncate it.
! 40765: **
! 40766: ** Or, it might might be the case that the file on disk is smaller than
! 40767: ** nPage pages. Some operating system implementations can get confused if
! 40768: ** you try to truncate a file to some size that is larger than it
! 40769: ** currently is, so detect this case and write a single zero byte to
! 40770: ** the end of the new file instead.
! 40771: **
! 40772: ** If successful, return SQLITE_OK. If an IO error occurs while modifying
! 40773: ** the database file, return the error code to the caller.
! 40774: */
! 40775: static int pager_truncate(Pager *pPager, Pgno nPage){
! 40776: int rc = SQLITE_OK;
! 40777: assert( pPager->eState!=PAGER_ERROR );
! 40778: assert( pPager->eState!=PAGER_READER );
! 40779:
! 40780: if( isOpen(pPager->fd)
! 40781: && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
! 40782: ){
! 40783: i64 currentSize, newSize;
! 40784: int szPage = pPager->pageSize;
! 40785: assert( pPager->eLock==EXCLUSIVE_LOCK );
! 40786: /* TODO: Is it safe to use Pager.dbFileSize here? */
! 40787: rc = sqlite3OsFileSize(pPager->fd, ¤tSize);
! 40788: newSize = szPage*(i64)nPage;
! 40789: if( rc==SQLITE_OK && currentSize!=newSize ){
! 40790: if( currentSize>newSize ){
! 40791: rc = sqlite3OsTruncate(pPager->fd, newSize);
! 40792: }else if( (currentSize+szPage)<=newSize ){
! 40793: char *pTmp = pPager->pTmpSpace;
! 40794: memset(pTmp, 0, szPage);
! 40795: testcase( (newSize-szPage) == currentSize );
! 40796: testcase( (newSize-szPage) > currentSize );
! 40797: rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
! 40798: }
! 40799: if( rc==SQLITE_OK ){
! 40800: pPager->dbFileSize = nPage;
! 40801: }
! 40802: }
! 40803: }
! 40804: return rc;
! 40805: }
! 40806:
! 40807: /*
! 40808: ** Set the value of the Pager.sectorSize variable for the given
! 40809: ** pager based on the value returned by the xSectorSize method
! 40810: ** of the open database file. The sector size will be used used
! 40811: ** to determine the size and alignment of journal header and
! 40812: ** master journal pointers within created journal files.
! 40813: **
! 40814: ** For temporary files the effective sector size is always 512 bytes.
! 40815: **
! 40816: ** Otherwise, for non-temporary files, the effective sector size is
! 40817: ** the value returned by the xSectorSize() method rounded up to 32 if
! 40818: ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
! 40819: ** is greater than MAX_SECTOR_SIZE.
! 40820: **
! 40821: ** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
! 40822: ** the effective sector size to its minimum value (512). The purpose of
! 40823: ** pPager->sectorSize is to define the "blast radius" of bytes that
! 40824: ** might change if a crash occurs while writing to a single byte in
! 40825: ** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
! 40826: ** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
! 40827: ** size. For backwards compatibility of the rollback journal file format,
! 40828: ** we cannot reduce the effective sector size below 512.
! 40829: */
! 40830: static void setSectorSize(Pager *pPager){
! 40831: assert( isOpen(pPager->fd) || pPager->tempFile );
! 40832:
! 40833: if( pPager->tempFile
! 40834: || (sqlite3OsDeviceCharacteristics(pPager->fd) &
! 40835: SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
! 40836: ){
! 40837: /* Sector size doesn't matter for temporary files. Also, the file
! 40838: ** may not have been opened yet, in which case the OsSectorSize()
! 40839: ** call will segfault. */
! 40840: pPager->sectorSize = 512;
! 40841: }else{
! 40842: pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
! 40843: if( pPager->sectorSize<32 ){
! 40844: pPager->sectorSize = 512;
! 40845: }
! 40846: if( pPager->sectorSize>MAX_SECTOR_SIZE ){
! 40847: assert( MAX_SECTOR_SIZE>=512 );
! 40848: pPager->sectorSize = MAX_SECTOR_SIZE;
! 40849: }
! 40850: }
! 40851: }
! 40852:
! 40853: /*
! 40854: ** Playback the journal and thus restore the database file to
! 40855: ** the state it was in before we started making changes.
! 40856: **
! 40857: ** The journal file format is as follows:
! 40858: **
! 40859: ** (1) 8 byte prefix. A copy of aJournalMagic[].
! 40860: ** (2) 4 byte big-endian integer which is the number of valid page records
! 40861: ** in the journal. If this value is 0xffffffff, then compute the
! 40862: ** number of page records from the journal size.
! 40863: ** (3) 4 byte big-endian integer which is the initial value for the
! 40864: ** sanity checksum.
! 40865: ** (4) 4 byte integer which is the number of pages to truncate the
! 40866: ** database to during a rollback.
! 40867: ** (5) 4 byte big-endian integer which is the sector size. The header
! 40868: ** is this many bytes in size.
! 40869: ** (6) 4 byte big-endian integer which is the page size.
! 40870: ** (7) zero padding out to the next sector size.
! 40871: ** (8) Zero or more pages instances, each as follows:
! 40872: ** + 4 byte page number.
! 40873: ** + pPager->pageSize bytes of data.
! 40874: ** + 4 byte checksum
! 40875: **
! 40876: ** When we speak of the journal header, we mean the first 7 items above.
! 40877: ** Each entry in the journal is an instance of the 8th item.
! 40878: **
! 40879: ** Call the value from the second bullet "nRec". nRec is the number of
! 40880: ** valid page entries in the journal. In most cases, you can compute the
! 40881: ** value of nRec from the size of the journal file. But if a power
! 40882: ** failure occurred while the journal was being written, it could be the
! 40883: ** case that the size of the journal file had already been increased but
! 40884: ** the extra entries had not yet made it safely to disk. In such a case,
! 40885: ** the value of nRec computed from the file size would be too large. For
! 40886: ** that reason, we always use the nRec value in the header.
! 40887: **
! 40888: ** If the nRec value is 0xffffffff it means that nRec should be computed
! 40889: ** from the file size. This value is used when the user selects the
! 40890: ** no-sync option for the journal. A power failure could lead to corruption
! 40891: ** in this case. But for things like temporary table (which will be
! 40892: ** deleted when the power is restored) we don't care.
! 40893: **
! 40894: ** If the file opened as the journal file is not a well-formed
! 40895: ** journal file then all pages up to the first corrupted page are rolled
! 40896: ** back (or no pages if the journal header is corrupted). The journal file
! 40897: ** is then deleted and SQLITE_OK returned, just as if no corruption had
! 40898: ** been encountered.
! 40899: **
! 40900: ** If an I/O or malloc() error occurs, the journal-file is not deleted
! 40901: ** and an error code is returned.
! 40902: **
! 40903: ** The isHot parameter indicates that we are trying to rollback a journal
! 40904: ** that might be a hot journal. Or, it could be that the journal is
! 40905: ** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
! 40906: ** If the journal really is hot, reset the pager cache prior rolling
! 40907: ** back any content. If the journal is merely persistent, no reset is
! 40908: ** needed.
! 40909: */
! 40910: static int pager_playback(Pager *pPager, int isHot){
! 40911: sqlite3_vfs *pVfs = pPager->pVfs;
! 40912: i64 szJ; /* Size of the journal file in bytes */
! 40913: u32 nRec; /* Number of Records in the journal */
! 40914: u32 u; /* Unsigned loop counter */
! 40915: Pgno mxPg = 0; /* Size of the original file in pages */
! 40916: int rc; /* Result code of a subroutine */
! 40917: int res = 1; /* Value returned by sqlite3OsAccess() */
! 40918: char *zMaster = 0; /* Name of master journal file if any */
! 40919: int needPagerReset; /* True to reset page prior to first page rollback */
! 40920:
! 40921: /* Figure out how many records are in the journal. Abort early if
! 40922: ** the journal is empty.
! 40923: */
! 40924: assert( isOpen(pPager->jfd) );
! 40925: rc = sqlite3OsFileSize(pPager->jfd, &szJ);
! 40926: if( rc!=SQLITE_OK ){
! 40927: goto end_playback;
! 40928: }
! 40929:
! 40930: /* Read the master journal name from the journal, if it is present.
! 40931: ** If a master journal file name is specified, but the file is not
! 40932: ** present on disk, then the journal is not hot and does not need to be
! 40933: ** played back.
! 40934: **
! 40935: ** TODO: Technically the following is an error because it assumes that
! 40936: ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
! 40937: ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
! 40938: ** mxPathname is 512, which is the same as the minimum allowable value
! 40939: ** for pageSize.
! 40940: */
! 40941: zMaster = pPager->pTmpSpace;
! 40942: rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
! 40943: if( rc==SQLITE_OK && zMaster[0] ){
! 40944: rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
! 40945: }
! 40946: zMaster = 0;
! 40947: if( rc!=SQLITE_OK || !res ){
! 40948: goto end_playback;
! 40949: }
! 40950: pPager->journalOff = 0;
! 40951: needPagerReset = isHot;
! 40952:
! 40953: /* This loop terminates either when a readJournalHdr() or
! 40954: ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
! 40955: ** occurs.
! 40956: */
! 40957: while( 1 ){
! 40958: /* Read the next journal header from the journal file. If there are
! 40959: ** not enough bytes left in the journal file for a complete header, or
! 40960: ** it is corrupted, then a process must have failed while writing it.
! 40961: ** This indicates nothing more needs to be rolled back.
! 40962: */
! 40963: rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
! 40964: if( rc!=SQLITE_OK ){
! 40965: if( rc==SQLITE_DONE ){
! 40966: rc = SQLITE_OK;
! 40967: }
! 40968: goto end_playback;
! 40969: }
! 40970:
! 40971: /* If nRec is 0xffffffff, then this journal was created by a process
! 40972: ** working in no-sync mode. This means that the rest of the journal
! 40973: ** file consists of pages, there are no more journal headers. Compute
! 40974: ** the value of nRec based on this assumption.
! 40975: */
! 40976: if( nRec==0xffffffff ){
! 40977: assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
! 40978: nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
! 40979: }
! 40980:
! 40981: /* If nRec is 0 and this rollback is of a transaction created by this
! 40982: ** process and if this is the final header in the journal, then it means
! 40983: ** that this part of the journal was being filled but has not yet been
! 40984: ** synced to disk. Compute the number of pages based on the remaining
! 40985: ** size of the file.
! 40986: **
! 40987: ** The third term of the test was added to fix ticket #2565.
! 40988: ** When rolling back a hot journal, nRec==0 always means that the next
! 40989: ** chunk of the journal contains zero pages to be rolled back. But
! 40990: ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
! 40991: ** the journal, it means that the journal might contain additional
! 40992: ** pages that need to be rolled back and that the number of pages
! 40993: ** should be computed based on the journal file size.
! 40994: */
! 40995: if( nRec==0 && !isHot &&
! 40996: pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
! 40997: nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
! 40998: }
! 40999:
! 41000: /* If this is the first header read from the journal, truncate the
! 41001: ** database file back to its original size.
! 41002: */
! 41003: if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
! 41004: rc = pager_truncate(pPager, mxPg);
! 41005: if( rc!=SQLITE_OK ){
! 41006: goto end_playback;
! 41007: }
! 41008: pPager->dbSize = mxPg;
! 41009: }
! 41010:
! 41011: /* Copy original pages out of the journal and back into the
! 41012: ** database file and/or page cache.
! 41013: */
! 41014: for(u=0; u<nRec; u++){
! 41015: if( needPagerReset ){
! 41016: pager_reset(pPager);
! 41017: needPagerReset = 0;
! 41018: }
! 41019: rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
! 41020: if( rc!=SQLITE_OK ){
! 41021: if( rc==SQLITE_DONE ){
! 41022: pPager->journalOff = szJ;
! 41023: break;
! 41024: }else if( rc==SQLITE_IOERR_SHORT_READ ){
! 41025: /* If the journal has been truncated, simply stop reading and
! 41026: ** processing the journal. This might happen if the journal was
! 41027: ** not completely written and synced prior to a crash. In that
! 41028: ** case, the database should have never been written in the
! 41029: ** first place so it is OK to simply abandon the rollback. */
! 41030: rc = SQLITE_OK;
! 41031: goto end_playback;
! 41032: }else{
! 41033: /* If we are unable to rollback, quit and return the error
! 41034: ** code. This will cause the pager to enter the error state
! 41035: ** so that no further harm will be done. Perhaps the next
! 41036: ** process to come along will be able to rollback the database.
! 41037: */
! 41038: goto end_playback;
! 41039: }
! 41040: }
! 41041: }
! 41042: }
! 41043: /*NOTREACHED*/
! 41044: assert( 0 );
! 41045:
! 41046: end_playback:
! 41047: /* Following a rollback, the database file should be back in its original
! 41048: ** state prior to the start of the transaction, so invoke the
! 41049: ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
! 41050: ** assertion that the transaction counter was modified.
! 41051: */
! 41052: #ifdef SQLITE_DEBUG
! 41053: if( pPager->fd->pMethods ){
! 41054: sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
! 41055: }
! 41056: #endif
! 41057:
! 41058: /* If this playback is happening automatically as a result of an IO or
! 41059: ** malloc error that occurred after the change-counter was updated but
! 41060: ** before the transaction was committed, then the change-counter
! 41061: ** modification may just have been reverted. If this happens in exclusive
! 41062: ** mode, then subsequent transactions performed by the connection will not
! 41063: ** update the change-counter at all. This may lead to cache inconsistency
! 41064: ** problems for other processes at some point in the future. So, just
! 41065: ** in case this has happened, clear the changeCountDone flag now.
! 41066: */
! 41067: pPager->changeCountDone = pPager->tempFile;
! 41068:
! 41069: if( rc==SQLITE_OK ){
! 41070: zMaster = pPager->pTmpSpace;
! 41071: rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
! 41072: testcase( rc!=SQLITE_OK );
! 41073: }
! 41074: if( rc==SQLITE_OK
! 41075: && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
! 41076: ){
! 41077: rc = sqlite3PagerSync(pPager);
! 41078: }
! 41079: if( rc==SQLITE_OK ){
! 41080: rc = pager_end_transaction(pPager, zMaster[0]!='\0');
! 41081: testcase( rc!=SQLITE_OK );
! 41082: }
! 41083: if( rc==SQLITE_OK && zMaster[0] && res ){
! 41084: /* If there was a master journal and this routine will return success,
! 41085: ** see if it is possible to delete the master journal.
! 41086: */
! 41087: rc = pager_delmaster(pPager, zMaster);
! 41088: testcase( rc!=SQLITE_OK );
! 41089: }
! 41090:
! 41091: /* The Pager.sectorSize variable may have been updated while rolling
! 41092: ** back a journal created by a process with a different sector size
! 41093: ** value. Reset it to the correct value for this process.
! 41094: */
! 41095: setSectorSize(pPager);
! 41096: return rc;
! 41097: }
! 41098:
! 41099:
! 41100: /*
! 41101: ** Read the content for page pPg out of the database file and into
! 41102: ** pPg->pData. A shared lock or greater must be held on the database
! 41103: ** file before this function is called.
! 41104: **
! 41105: ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
! 41106: ** the value read from the database file.
! 41107: **
! 41108: ** If an IO error occurs, then the IO error is returned to the caller.
! 41109: ** Otherwise, SQLITE_OK is returned.
! 41110: */
! 41111: static int readDbPage(PgHdr *pPg){
! 41112: Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
! 41113: Pgno pgno = pPg->pgno; /* Page number to read */
! 41114: int rc = SQLITE_OK; /* Return code */
! 41115: int isInWal = 0; /* True if page is in log file */
! 41116: int pgsz = pPager->pageSize; /* Number of bytes to read */
! 41117:
! 41118: assert( pPager->eState>=PAGER_READER && !MEMDB );
! 41119: assert( isOpen(pPager->fd) );
! 41120:
! 41121: if( NEVER(!isOpen(pPager->fd)) ){
! 41122: assert( pPager->tempFile );
! 41123: memset(pPg->pData, 0, pPager->pageSize);
! 41124: return SQLITE_OK;
! 41125: }
! 41126:
! 41127: if( pagerUseWal(pPager) ){
! 41128: /* Try to pull the page from the write-ahead log. */
! 41129: rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
! 41130: }
! 41131: if( rc==SQLITE_OK && !isInWal ){
! 41132: i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
! 41133: rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
! 41134: if( rc==SQLITE_IOERR_SHORT_READ ){
! 41135: rc = SQLITE_OK;
! 41136: }
! 41137: }
! 41138:
! 41139: if( pgno==1 ){
! 41140: if( rc ){
! 41141: /* If the read is unsuccessful, set the dbFileVers[] to something
! 41142: ** that will never be a valid file version. dbFileVers[] is a copy
! 41143: ** of bytes 24..39 of the database. Bytes 28..31 should always be
! 41144: ** zero or the size of the database in page. Bytes 32..35 and 35..39
! 41145: ** should be page numbers which are never 0xffffffff. So filling
! 41146: ** pPager->dbFileVers[] with all 0xff bytes should suffice.
! 41147: **
! 41148: ** For an encrypted database, the situation is more complex: bytes
! 41149: ** 24..39 of the database are white noise. But the probability of
! 41150: ** white noising equaling 16 bytes of 0xff is vanishingly small so
! 41151: ** we should still be ok.
! 41152: */
! 41153: memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
! 41154: }else{
! 41155: u8 *dbFileVers = &((u8*)pPg->pData)[24];
! 41156: memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
! 41157: }
! 41158: }
! 41159: CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
! 41160:
! 41161: PAGER_INCR(sqlite3_pager_readdb_count);
! 41162: PAGER_INCR(pPager->nRead);
! 41163: IOTRACE(("PGIN %p %d\n", pPager, pgno));
! 41164: PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
! 41165: PAGERID(pPager), pgno, pager_pagehash(pPg)));
! 41166:
! 41167: return rc;
! 41168: }
! 41169:
! 41170: /*
! 41171: ** Update the value of the change-counter at offsets 24 and 92 in
! 41172: ** the header and the sqlite version number at offset 96.
! 41173: **
! 41174: ** This is an unconditional update. See also the pager_incr_changecounter()
! 41175: ** routine which only updates the change-counter if the update is actually
! 41176: ** needed, as determined by the pPager->changeCountDone state variable.
! 41177: */
! 41178: static void pager_write_changecounter(PgHdr *pPg){
! 41179: u32 change_counter;
! 41180:
! 41181: /* Increment the value just read and write it back to byte 24. */
! 41182: change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
! 41183: put32bits(((char*)pPg->pData)+24, change_counter);
! 41184:
! 41185: /* Also store the SQLite version number in bytes 96..99 and in
! 41186: ** bytes 92..95 store the change counter for which the version number
! 41187: ** is valid. */
! 41188: put32bits(((char*)pPg->pData)+92, change_counter);
! 41189: put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
! 41190: }
! 41191:
! 41192: #ifndef SQLITE_OMIT_WAL
! 41193: /*
! 41194: ** This function is invoked once for each page that has already been
! 41195: ** written into the log file when a WAL transaction is rolled back.
! 41196: ** Parameter iPg is the page number of said page. The pCtx argument
! 41197: ** is actually a pointer to the Pager structure.
! 41198: **
! 41199: ** If page iPg is present in the cache, and has no outstanding references,
! 41200: ** it is discarded. Otherwise, if there are one or more outstanding
! 41201: ** references, the page content is reloaded from the database. If the
! 41202: ** attempt to reload content from the database is required and fails,
! 41203: ** return an SQLite error code. Otherwise, SQLITE_OK.
! 41204: */
! 41205: static int pagerUndoCallback(void *pCtx, Pgno iPg){
! 41206: int rc = SQLITE_OK;
! 41207: Pager *pPager = (Pager *)pCtx;
! 41208: PgHdr *pPg;
! 41209:
! 41210: pPg = sqlite3PagerLookup(pPager, iPg);
! 41211: if( pPg ){
! 41212: if( sqlite3PcachePageRefcount(pPg)==1 ){
! 41213: sqlite3PcacheDrop(pPg);
! 41214: }else{
! 41215: rc = readDbPage(pPg);
! 41216: if( rc==SQLITE_OK ){
! 41217: pPager->xReiniter(pPg);
! 41218: }
! 41219: sqlite3PagerUnref(pPg);
! 41220: }
! 41221: }
! 41222:
! 41223: /* Normally, if a transaction is rolled back, any backup processes are
! 41224: ** updated as data is copied out of the rollback journal and into the
! 41225: ** database. This is not generally possible with a WAL database, as
! 41226: ** rollback involves simply truncating the log file. Therefore, if one
! 41227: ** or more frames have already been written to the log (and therefore
! 41228: ** also copied into the backup databases) as part of this transaction,
! 41229: ** the backups must be restarted.
! 41230: */
! 41231: sqlite3BackupRestart(pPager->pBackup);
! 41232:
! 41233: return rc;
! 41234: }
! 41235:
! 41236: /*
! 41237: ** This function is called to rollback a transaction on a WAL database.
! 41238: */
! 41239: static int pagerRollbackWal(Pager *pPager){
! 41240: int rc; /* Return Code */
! 41241: PgHdr *pList; /* List of dirty pages to revert */
! 41242:
! 41243: /* For all pages in the cache that are currently dirty or have already
! 41244: ** been written (but not committed) to the log file, do one of the
! 41245: ** following:
! 41246: **
! 41247: ** + Discard the cached page (if refcount==0), or
! 41248: ** + Reload page content from the database (if refcount>0).
! 41249: */
! 41250: pPager->dbSize = pPager->dbOrigSize;
! 41251: rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
! 41252: pList = sqlite3PcacheDirtyList(pPager->pPCache);
! 41253: while( pList && rc==SQLITE_OK ){
! 41254: PgHdr *pNext = pList->pDirty;
! 41255: rc = pagerUndoCallback((void *)pPager, pList->pgno);
! 41256: pList = pNext;
! 41257: }
! 41258:
! 41259: return rc;
! 41260: }
! 41261:
! 41262: /*
! 41263: ** This function is a wrapper around sqlite3WalFrames(). As well as logging
! 41264: ** the contents of the list of pages headed by pList (connected by pDirty),
! 41265: ** this function notifies any active backup processes that the pages have
! 41266: ** changed.
! 41267: **
! 41268: ** The list of pages passed into this routine is always sorted by page number.
! 41269: ** Hence, if page 1 appears anywhere on the list, it will be the first page.
! 41270: */
! 41271: static int pagerWalFrames(
! 41272: Pager *pPager, /* Pager object */
! 41273: PgHdr *pList, /* List of frames to log */
! 41274: Pgno nTruncate, /* Database size after this commit */
! 41275: int isCommit /* True if this is a commit */
! 41276: ){
! 41277: int rc; /* Return code */
! 41278: #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
! 41279: PgHdr *p; /* For looping over pages */
! 41280: #endif
! 41281:
! 41282: assert( pPager->pWal );
! 41283: assert( pList );
! 41284: #ifdef SQLITE_DEBUG
! 41285: /* Verify that the page list is in accending order */
! 41286: for(p=pList; p && p->pDirty; p=p->pDirty){
! 41287: assert( p->pgno < p->pDirty->pgno );
! 41288: }
! 41289: #endif
! 41290:
! 41291: if( isCommit ){
! 41292: /* If a WAL transaction is being committed, there is no point in writing
! 41293: ** any pages with page numbers greater than nTruncate into the WAL file.
! 41294: ** They will never be read by any client. So remove them from the pDirty
! 41295: ** list here. */
! 41296: PgHdr *p;
! 41297: PgHdr **ppNext = &pList;
! 41298: for(p=pList; (*ppNext = p); p=p->pDirty){
! 41299: if( p->pgno<=nTruncate ) ppNext = &p->pDirty;
! 41300: }
! 41301: assert( pList );
! 41302: }
! 41303:
! 41304: if( pList->pgno==1 ) pager_write_changecounter(pList);
! 41305: rc = sqlite3WalFrames(pPager->pWal,
! 41306: pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
! 41307: );
! 41308: if( rc==SQLITE_OK && pPager->pBackup ){
! 41309: PgHdr *p;
! 41310: for(p=pList; p; p=p->pDirty){
! 41311: sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
! 41312: }
! 41313: }
! 41314:
! 41315: #ifdef SQLITE_CHECK_PAGES
! 41316: pList = sqlite3PcacheDirtyList(pPager->pPCache);
! 41317: for(p=pList; p; p=p->pDirty){
! 41318: pager_set_pagehash(p);
! 41319: }
! 41320: #endif
! 41321:
! 41322: return rc;
! 41323: }
! 41324:
! 41325: /*
! 41326: ** Begin a read transaction on the WAL.
! 41327: **
! 41328: ** This routine used to be called "pagerOpenSnapshot()" because it essentially
! 41329: ** makes a snapshot of the database at the current point in time and preserves
! 41330: ** that snapshot for use by the reader in spite of concurrently changes by
! 41331: ** other writers or checkpointers.
! 41332: */
! 41333: static int pagerBeginReadTransaction(Pager *pPager){
! 41334: int rc; /* Return code */
! 41335: int changed = 0; /* True if cache must be reset */
! 41336:
! 41337: assert( pagerUseWal(pPager) );
! 41338: assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
! 41339:
! 41340: /* sqlite3WalEndReadTransaction() was not called for the previous
! 41341: ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
! 41342: ** are in locking_mode=NORMAL and EndRead() was previously called,
! 41343: ** the duplicate call is harmless.
! 41344: */
! 41345: sqlite3WalEndReadTransaction(pPager->pWal);
! 41346:
! 41347: rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
! 41348: if( rc!=SQLITE_OK || changed ){
! 41349: pager_reset(pPager);
! 41350: }
! 41351:
! 41352: return rc;
! 41353: }
! 41354: #endif
! 41355:
! 41356: /*
! 41357: ** This function is called as part of the transition from PAGER_OPEN
! 41358: ** to PAGER_READER state to determine the size of the database file
! 41359: ** in pages (assuming the page size currently stored in Pager.pageSize).
! 41360: **
! 41361: ** If no error occurs, SQLITE_OK is returned and the size of the database
! 41362: ** in pages is stored in *pnPage. Otherwise, an error code (perhaps
! 41363: ** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
! 41364: */
! 41365: static int pagerPagecount(Pager *pPager, Pgno *pnPage){
! 41366: Pgno nPage; /* Value to return via *pnPage */
! 41367:
! 41368: /* Query the WAL sub-system for the database size. The WalDbsize()
! 41369: ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
! 41370: ** if the database size is not available. The database size is not
! 41371: ** available from the WAL sub-system if the log file is empty or
! 41372: ** contains no valid committed transactions.
! 41373: */
! 41374: assert( pPager->eState==PAGER_OPEN );
! 41375: assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
! 41376: nPage = sqlite3WalDbsize(pPager->pWal);
! 41377:
! 41378: /* If the database size was not available from the WAL sub-system,
! 41379: ** determine it based on the size of the database file. If the size
! 41380: ** of the database file is not an integer multiple of the page-size,
! 41381: ** round down to the nearest page. Except, any file larger than 0
! 41382: ** bytes in size is considered to contain at least one page.
! 41383: */
! 41384: if( nPage==0 ){
! 41385: i64 n = 0; /* Size of db file in bytes */
! 41386: assert( isOpen(pPager->fd) || pPager->tempFile );
! 41387: if( isOpen(pPager->fd) ){
! 41388: int rc = sqlite3OsFileSize(pPager->fd, &n);
! 41389: if( rc!=SQLITE_OK ){
! 41390: return rc;
! 41391: }
! 41392: }
! 41393: nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
! 41394: }
! 41395:
! 41396: /* If the current number of pages in the file is greater than the
! 41397: ** configured maximum pager number, increase the allowed limit so
! 41398: ** that the file can be read.
! 41399: */
! 41400: if( nPage>pPager->mxPgno ){
! 41401: pPager->mxPgno = (Pgno)nPage;
! 41402: }
! 41403:
! 41404: *pnPage = nPage;
! 41405: return SQLITE_OK;
! 41406: }
! 41407:
! 41408: #ifndef SQLITE_OMIT_WAL
! 41409: /*
! 41410: ** Check if the *-wal file that corresponds to the database opened by pPager
! 41411: ** exists if the database is not empy, or verify that the *-wal file does
! 41412: ** not exist (by deleting it) if the database file is empty.
! 41413: **
! 41414: ** If the database is not empty and the *-wal file exists, open the pager
! 41415: ** in WAL mode. If the database is empty or if no *-wal file exists and
! 41416: ** if no error occurs, make sure Pager.journalMode is not set to
! 41417: ** PAGER_JOURNALMODE_WAL.
! 41418: **
! 41419: ** Return SQLITE_OK or an error code.
! 41420: **
! 41421: ** The caller must hold a SHARED lock on the database file to call this
! 41422: ** function. Because an EXCLUSIVE lock on the db file is required to delete
! 41423: ** a WAL on a none-empty database, this ensures there is no race condition
! 41424: ** between the xAccess() below and an xDelete() being executed by some
! 41425: ** other connection.
! 41426: */
! 41427: static int pagerOpenWalIfPresent(Pager *pPager){
! 41428: int rc = SQLITE_OK;
! 41429: assert( pPager->eState==PAGER_OPEN );
! 41430: assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
! 41431:
! 41432: if( !pPager->tempFile ){
! 41433: int isWal; /* True if WAL file exists */
! 41434: Pgno nPage; /* Size of the database file */
! 41435:
! 41436: rc = pagerPagecount(pPager, &nPage);
! 41437: if( rc ) return rc;
! 41438: if( nPage==0 ){
! 41439: rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
! 41440: isWal = 0;
! 41441: }else{
! 41442: rc = sqlite3OsAccess(
! 41443: pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
! 41444: );
! 41445: }
! 41446: if( rc==SQLITE_OK ){
! 41447: if( isWal ){
! 41448: testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
! 41449: rc = sqlite3PagerOpenWal(pPager, 0);
! 41450: }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
! 41451: pPager->journalMode = PAGER_JOURNALMODE_DELETE;
! 41452: }
! 41453: }
! 41454: }
! 41455: return rc;
! 41456: }
! 41457: #endif
! 41458:
! 41459: /*
! 41460: ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
! 41461: ** the entire master journal file. The case pSavepoint==NULL occurs when
! 41462: ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
! 41463: ** savepoint.
! 41464: **
! 41465: ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
! 41466: ** being rolled back), then the rollback consists of up to three stages,
! 41467: ** performed in the order specified:
! 41468: **
! 41469: ** * Pages are played back from the main journal starting at byte
! 41470: ** offset PagerSavepoint.iOffset and continuing to
! 41471: ** PagerSavepoint.iHdrOffset, or to the end of the main journal
! 41472: ** file if PagerSavepoint.iHdrOffset is zero.
! 41473: **
! 41474: ** * If PagerSavepoint.iHdrOffset is not zero, then pages are played
! 41475: ** back starting from the journal header immediately following
! 41476: ** PagerSavepoint.iHdrOffset to the end of the main journal file.
! 41477: **
! 41478: ** * Pages are then played back from the sub-journal file, starting
! 41479: ** with the PagerSavepoint.iSubRec and continuing to the end of
! 41480: ** the journal file.
! 41481: **
! 41482: ** Throughout the rollback process, each time a page is rolled back, the
! 41483: ** corresponding bit is set in a bitvec structure (variable pDone in the
! 41484: ** implementation below). This is used to ensure that a page is only
! 41485: ** rolled back the first time it is encountered in either journal.
! 41486: **
! 41487: ** If pSavepoint is NULL, then pages are only played back from the main
! 41488: ** journal file. There is no need for a bitvec in this case.
! 41489: **
! 41490: ** In either case, before playback commences the Pager.dbSize variable
! 41491: ** is reset to the value that it held at the start of the savepoint
! 41492: ** (or transaction). No page with a page-number greater than this value
! 41493: ** is played back. If one is encountered it is simply skipped.
! 41494: */
! 41495: static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
! 41496: i64 szJ; /* Effective size of the main journal */
! 41497: i64 iHdrOff; /* End of first segment of main-journal records */
! 41498: int rc = SQLITE_OK; /* Return code */
! 41499: Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
! 41500:
! 41501: assert( pPager->eState!=PAGER_ERROR );
! 41502: assert( pPager->eState>=PAGER_WRITER_LOCKED );
! 41503:
! 41504: /* Allocate a bitvec to use to store the set of pages rolled back */
! 41505: if( pSavepoint ){
! 41506: pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
! 41507: if( !pDone ){
! 41508: return SQLITE_NOMEM;
! 41509: }
! 41510: }
! 41511:
! 41512: /* Set the database size back to the value it was before the savepoint
! 41513: ** being reverted was opened.
! 41514: */
! 41515: pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
! 41516: pPager->changeCountDone = pPager->tempFile;
! 41517:
! 41518: if( !pSavepoint && pagerUseWal(pPager) ){
! 41519: return pagerRollbackWal(pPager);
! 41520: }
! 41521:
! 41522: /* Use pPager->journalOff as the effective size of the main rollback
! 41523: ** journal. The actual file might be larger than this in
! 41524: ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
! 41525: ** past pPager->journalOff is off-limits to us.
! 41526: */
! 41527: szJ = pPager->journalOff;
! 41528: assert( pagerUseWal(pPager)==0 || szJ==0 );
! 41529:
! 41530: /* Begin by rolling back records from the main journal starting at
! 41531: ** PagerSavepoint.iOffset and continuing to the next journal header.
! 41532: ** There might be records in the main journal that have a page number
! 41533: ** greater than the current database size (pPager->dbSize) but those
! 41534: ** will be skipped automatically. Pages are added to pDone as they
! 41535: ** are played back.
! 41536: */
! 41537: if( pSavepoint && !pagerUseWal(pPager) ){
! 41538: iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
! 41539: pPager->journalOff = pSavepoint->iOffset;
! 41540: while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
! 41541: rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
! 41542: }
! 41543: assert( rc!=SQLITE_DONE );
! 41544: }else{
! 41545: pPager->journalOff = 0;
! 41546: }
! 41547:
! 41548: /* Continue rolling back records out of the main journal starting at
! 41549: ** the first journal header seen and continuing until the effective end
! 41550: ** of the main journal file. Continue to skip out-of-range pages and
! 41551: ** continue adding pages rolled back to pDone.
! 41552: */
! 41553: while( rc==SQLITE_OK && pPager->journalOff<szJ ){
! 41554: u32 ii; /* Loop counter */
! 41555: u32 nJRec = 0; /* Number of Journal Records */
! 41556: u32 dummy;
! 41557: rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
! 41558: assert( rc!=SQLITE_DONE );
! 41559:
! 41560: /*
! 41561: ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
! 41562: ** test is related to ticket #2565. See the discussion in the
! 41563: ** pager_playback() function for additional information.
! 41564: */
! 41565: if( nJRec==0
! 41566: && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
! 41567: ){
! 41568: nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
! 41569: }
! 41570: for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
! 41571: rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
! 41572: }
! 41573: assert( rc!=SQLITE_DONE );
! 41574: }
! 41575: assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
! 41576:
! 41577: /* Finally, rollback pages from the sub-journal. Page that were
! 41578: ** previously rolled back out of the main journal (and are hence in pDone)
! 41579: ** will be skipped. Out-of-range pages are also skipped.
! 41580: */
! 41581: if( pSavepoint ){
! 41582: u32 ii; /* Loop counter */
! 41583: i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
! 41584:
! 41585: if( pagerUseWal(pPager) ){
! 41586: rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
! 41587: }
! 41588: for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
! 41589: assert( offset==(i64)ii*(4+pPager->pageSize) );
! 41590: rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
! 41591: }
! 41592: assert( rc!=SQLITE_DONE );
! 41593: }
! 41594:
! 41595: sqlite3BitvecDestroy(pDone);
! 41596: if( rc==SQLITE_OK ){
! 41597: pPager->journalOff = szJ;
! 41598: }
! 41599:
! 41600: return rc;
! 41601: }
! 41602:
! 41603: /*
! 41604: ** Change the maximum number of in-memory pages that are allowed.
! 41605: */
! 41606: SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
! 41607: sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
! 41608: }
! 41609:
! 41610: /*
! 41611: ** Free as much memory as possible from the pager.
! 41612: */
! 41613: SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
! 41614: sqlite3PcacheShrink(pPager->pPCache);
! 41615: }
! 41616:
! 41617: /*
! 41618: ** Adjust the robustness of the database to damage due to OS crashes
! 41619: ** or power failures by changing the number of syncs()s when writing
! 41620: ** the rollback journal. There are three levels:
! 41621: **
! 41622: ** OFF sqlite3OsSync() is never called. This is the default
! 41623: ** for temporary and transient files.
! 41624: **
! 41625: ** NORMAL The journal is synced once before writes begin on the
! 41626: ** database. This is normally adequate protection, but
! 41627: ** it is theoretically possible, though very unlikely,
! 41628: ** that an inopertune power failure could leave the journal
! 41629: ** in a state which would cause damage to the database
! 41630: ** when it is rolled back.
! 41631: **
! 41632: ** FULL The journal is synced twice before writes begin on the
! 41633: ** database (with some additional information - the nRec field
! 41634: ** of the journal header - being written in between the two
! 41635: ** syncs). If we assume that writing a
! 41636: ** single disk sector is atomic, then this mode provides
! 41637: ** assurance that the journal will not be corrupted to the
! 41638: ** point of causing damage to the database during rollback.
! 41639: **
! 41640: ** The above is for a rollback-journal mode. For WAL mode, OFF continues
! 41641: ** to mean that no syncs ever occur. NORMAL means that the WAL is synced
! 41642: ** prior to the start of checkpoint and that the database file is synced
! 41643: ** at the conclusion of the checkpoint if the entire content of the WAL
! 41644: ** was written back into the database. But no sync operations occur for
! 41645: ** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL
! 41646: ** file is synced following each commit operation, in addition to the
! 41647: ** syncs associated with NORMAL.
! 41648: **
! 41649: ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The
! 41650: ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
! 41651: ** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an
! 41652: ** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL
! 41653: ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the
! 41654: ** synchronous=FULL versus synchronous=NORMAL setting determines when
! 41655: ** the xSync primitive is called and is relevant to all platforms.
! 41656: **
! 41657: ** Numeric values associated with these states are OFF==1, NORMAL=2,
! 41658: ** and FULL=3.
! 41659: */
! 41660: #ifndef SQLITE_OMIT_PAGER_PRAGMAS
! 41661: SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(
! 41662: Pager *pPager, /* The pager to set safety level for */
! 41663: int level, /* PRAGMA synchronous. 1=OFF, 2=NORMAL, 3=FULL */
! 41664: int bFullFsync, /* PRAGMA fullfsync */
! 41665: int bCkptFullFsync /* PRAGMA checkpoint_fullfsync */
! 41666: ){
! 41667: assert( level>=1 && level<=3 );
! 41668: pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
! 41669: pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
! 41670: if( pPager->noSync ){
! 41671: pPager->syncFlags = 0;
! 41672: pPager->ckptSyncFlags = 0;
! 41673: }else if( bFullFsync ){
! 41674: pPager->syncFlags = SQLITE_SYNC_FULL;
! 41675: pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
! 41676: }else if( bCkptFullFsync ){
! 41677: pPager->syncFlags = SQLITE_SYNC_NORMAL;
! 41678: pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
! 41679: }else{
! 41680: pPager->syncFlags = SQLITE_SYNC_NORMAL;
! 41681: pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
! 41682: }
! 41683: pPager->walSyncFlags = pPager->syncFlags;
! 41684: if( pPager->fullSync ){
! 41685: pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
! 41686: }
! 41687: }
! 41688: #endif
! 41689:
! 41690: /*
! 41691: ** The following global variable is incremented whenever the library
! 41692: ** attempts to open a temporary file. This information is used for
! 41693: ** testing and analysis only.
! 41694: */
! 41695: #ifdef SQLITE_TEST
! 41696: SQLITE_API int sqlite3_opentemp_count = 0;
! 41697: #endif
! 41698:
! 41699: /*
! 41700: ** Open a temporary file.
! 41701: **
! 41702: ** Write the file descriptor into *pFile. Return SQLITE_OK on success
! 41703: ** or some other error code if we fail. The OS will automatically
! 41704: ** delete the temporary file when it is closed.
! 41705: **
! 41706: ** The flags passed to the VFS layer xOpen() call are those specified
! 41707: ** by parameter vfsFlags ORed with the following:
! 41708: **
! 41709: ** SQLITE_OPEN_READWRITE
! 41710: ** SQLITE_OPEN_CREATE
! 41711: ** SQLITE_OPEN_EXCLUSIVE
! 41712: ** SQLITE_OPEN_DELETEONCLOSE
! 41713: */
! 41714: static int pagerOpentemp(
! 41715: Pager *pPager, /* The pager object */
! 41716: sqlite3_file *pFile, /* Write the file descriptor here */
! 41717: int vfsFlags /* Flags passed through to the VFS */
! 41718: ){
! 41719: int rc; /* Return code */
! 41720:
! 41721: #ifdef SQLITE_TEST
! 41722: sqlite3_opentemp_count++; /* Used for testing and analysis only */
! 41723: #endif
! 41724:
! 41725: vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
! 41726: SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
! 41727: rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
! 41728: assert( rc!=SQLITE_OK || isOpen(pFile) );
! 41729: return rc;
! 41730: }
! 41731:
! 41732: /*
! 41733: ** Set the busy handler function.
! 41734: **
! 41735: ** The pager invokes the busy-handler if sqlite3OsLock() returns
! 41736: ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
! 41737: ** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
! 41738: ** lock. It does *not* invoke the busy handler when upgrading from
! 41739: ** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
! 41740: ** (which occurs during hot-journal rollback). Summary:
! 41741: **
! 41742: ** Transition | Invokes xBusyHandler
! 41743: ** --------------------------------------------------------
! 41744: ** NO_LOCK -> SHARED_LOCK | Yes
! 41745: ** SHARED_LOCK -> RESERVED_LOCK | No
! 41746: ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
! 41747: ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
! 41748: **
! 41749: ** If the busy-handler callback returns non-zero, the lock is
! 41750: ** retried. If it returns zero, then the SQLITE_BUSY error is
! 41751: ** returned to the caller of the pager API function.
! 41752: */
! 41753: SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
! 41754: Pager *pPager, /* Pager object */
! 41755: int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
! 41756: void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
! 41757: ){
! 41758: pPager->xBusyHandler = xBusyHandler;
! 41759: pPager->pBusyHandlerArg = pBusyHandlerArg;
! 41760: }
! 41761:
! 41762: /*
! 41763: ** Change the page size used by the Pager object. The new page size
! 41764: ** is passed in *pPageSize.
! 41765: **
! 41766: ** If the pager is in the error state when this function is called, it
! 41767: ** is a no-op. The value returned is the error state error code (i.e.
! 41768: ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
! 41769: **
! 41770: ** Otherwise, if all of the following are true:
! 41771: **
! 41772: ** * the new page size (value of *pPageSize) is valid (a power
! 41773: ** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
! 41774: **
! 41775: ** * there are no outstanding page references, and
! 41776: **
! 41777: ** * the database is either not an in-memory database or it is
! 41778: ** an in-memory database that currently consists of zero pages.
! 41779: **
! 41780: ** then the pager object page size is set to *pPageSize.
! 41781: **
! 41782: ** If the page size is changed, then this function uses sqlite3PagerMalloc()
! 41783: ** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
! 41784: ** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
! 41785: ** In all other cases, SQLITE_OK is returned.
! 41786: **
! 41787: ** If the page size is not changed, either because one of the enumerated
! 41788: ** conditions above is not true, the pager was in error state when this
! 41789: ** function was called, or because the memory allocation attempt failed,
! 41790: ** then *pPageSize is set to the old, retained page size before returning.
! 41791: */
! 41792: SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
! 41793: int rc = SQLITE_OK;
! 41794:
! 41795: /* It is not possible to do a full assert_pager_state() here, as this
! 41796: ** function may be called from within PagerOpen(), before the state
! 41797: ** of the Pager object is internally consistent.
! 41798: **
! 41799: ** At one point this function returned an error if the pager was in
! 41800: ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
! 41801: ** there is at least one outstanding page reference, this function
! 41802: ** is a no-op for that case anyhow.
! 41803: */
! 41804:
! 41805: u32 pageSize = *pPageSize;
! 41806: assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
! 41807: if( (pPager->memDb==0 || pPager->dbSize==0)
! 41808: && sqlite3PcacheRefCount(pPager->pPCache)==0
! 41809: && pageSize && pageSize!=(u32)pPager->pageSize
! 41810: ){
! 41811: char *pNew = NULL; /* New temp space */
! 41812: i64 nByte = 0;
! 41813:
! 41814: if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
! 41815: rc = sqlite3OsFileSize(pPager->fd, &nByte);
! 41816: }
! 41817: if( rc==SQLITE_OK ){
! 41818: pNew = (char *)sqlite3PageMalloc(pageSize);
! 41819: if( !pNew ) rc = SQLITE_NOMEM;
! 41820: }
! 41821:
! 41822: if( rc==SQLITE_OK ){
! 41823: pager_reset(pPager);
! 41824: pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
! 41825: pPager->pageSize = pageSize;
! 41826: sqlite3PageFree(pPager->pTmpSpace);
! 41827: pPager->pTmpSpace = pNew;
! 41828: sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
! 41829: }
! 41830: }
! 41831:
! 41832: *pPageSize = pPager->pageSize;
! 41833: if( rc==SQLITE_OK ){
! 41834: if( nReserve<0 ) nReserve = pPager->nReserve;
! 41835: assert( nReserve>=0 && nReserve<1000 );
! 41836: pPager->nReserve = (i16)nReserve;
! 41837: pagerReportSize(pPager);
! 41838: }
! 41839: return rc;
! 41840: }
! 41841:
! 41842: /*
! 41843: ** Return a pointer to the "temporary page" buffer held internally
! 41844: ** by the pager. This is a buffer that is big enough to hold the
! 41845: ** entire content of a database page. This buffer is used internally
! 41846: ** during rollback and will be overwritten whenever a rollback
! 41847: ** occurs. But other modules are free to use it too, as long as
! 41848: ** no rollbacks are happening.
! 41849: */
! 41850: SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
! 41851: return pPager->pTmpSpace;
! 41852: }
! 41853:
! 41854: /*
! 41855: ** Attempt to set the maximum database page count if mxPage is positive.
! 41856: ** Make no changes if mxPage is zero or negative. And never reduce the
! 41857: ** maximum page count below the current size of the database.
! 41858: **
! 41859: ** Regardless of mxPage, return the current maximum page count.
! 41860: */
! 41861: SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
! 41862: if( mxPage>0 ){
! 41863: pPager->mxPgno = mxPage;
! 41864: }
! 41865: assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
! 41866: assert( pPager->mxPgno>=pPager->dbSize ); /* OP_MaxPgcnt enforces this */
! 41867: return pPager->mxPgno;
! 41868: }
! 41869:
! 41870: /*
! 41871: ** The following set of routines are used to disable the simulated
! 41872: ** I/O error mechanism. These routines are used to avoid simulated
! 41873: ** errors in places where we do not care about errors.
! 41874: **
! 41875: ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
! 41876: ** and generate no code.
! 41877: */
! 41878: #ifdef SQLITE_TEST
! 41879: SQLITE_API extern int sqlite3_io_error_pending;
! 41880: SQLITE_API extern int sqlite3_io_error_hit;
! 41881: static int saved_cnt;
! 41882: void disable_simulated_io_errors(void){
! 41883: saved_cnt = sqlite3_io_error_pending;
! 41884: sqlite3_io_error_pending = -1;
! 41885: }
! 41886: void enable_simulated_io_errors(void){
! 41887: sqlite3_io_error_pending = saved_cnt;
! 41888: }
! 41889: #else
! 41890: # define disable_simulated_io_errors()
! 41891: # define enable_simulated_io_errors()
! 41892: #endif
! 41893:
! 41894: /*
! 41895: ** Read the first N bytes from the beginning of the file into memory
! 41896: ** that pDest points to.
! 41897: **
! 41898: ** If the pager was opened on a transient file (zFilename==""), or
! 41899: ** opened on a file less than N bytes in size, the output buffer is
! 41900: ** zeroed and SQLITE_OK returned. The rationale for this is that this
! 41901: ** function is used to read database headers, and a new transient or
! 41902: ** zero sized database has a header than consists entirely of zeroes.
! 41903: **
! 41904: ** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
! 41905: ** the error code is returned to the caller and the contents of the
! 41906: ** output buffer undefined.
! 41907: */
! 41908: SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
! 41909: int rc = SQLITE_OK;
! 41910: memset(pDest, 0, N);
! 41911: assert( isOpen(pPager->fd) || pPager->tempFile );
! 41912:
! 41913: /* This routine is only called by btree immediately after creating
! 41914: ** the Pager object. There has not been an opportunity to transition
! 41915: ** to WAL mode yet.
! 41916: */
! 41917: assert( !pagerUseWal(pPager) );
! 41918:
! 41919: if( isOpen(pPager->fd) ){
! 41920: IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
! 41921: rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
! 41922: if( rc==SQLITE_IOERR_SHORT_READ ){
! 41923: rc = SQLITE_OK;
! 41924: }
! 41925: }
! 41926: return rc;
! 41927: }
! 41928:
! 41929: /*
! 41930: ** This function may only be called when a read-transaction is open on
! 41931: ** the pager. It returns the total number of pages in the database.
! 41932: **
! 41933: ** However, if the file is between 1 and <page-size> bytes in size, then
! 41934: ** this is considered a 1 page file.
! 41935: */
! 41936: SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
! 41937: assert( pPager->eState>=PAGER_READER );
! 41938: assert( pPager->eState!=PAGER_WRITER_FINISHED );
! 41939: *pnPage = (int)pPager->dbSize;
! 41940: }
! 41941:
! 41942:
! 41943: /*
! 41944: ** Try to obtain a lock of type locktype on the database file. If
! 41945: ** a similar or greater lock is already held, this function is a no-op
! 41946: ** (returning SQLITE_OK immediately).
! 41947: **
! 41948: ** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
! 41949: ** the busy callback if the lock is currently not available. Repeat
! 41950: ** until the busy callback returns false or until the attempt to
! 41951: ** obtain the lock succeeds.
! 41952: **
! 41953: ** Return SQLITE_OK on success and an error code if we cannot obtain
! 41954: ** the lock. If the lock is obtained successfully, set the Pager.state
! 41955: ** variable to locktype before returning.
! 41956: */
! 41957: static int pager_wait_on_lock(Pager *pPager, int locktype){
! 41958: int rc; /* Return code */
! 41959:
! 41960: /* Check that this is either a no-op (because the requested lock is
! 41961: ** already held, or one of the transistions that the busy-handler
! 41962: ** may be invoked during, according to the comment above
! 41963: ** sqlite3PagerSetBusyhandler().
! 41964: */
! 41965: assert( (pPager->eLock>=locktype)
! 41966: || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
! 41967: || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
! 41968: );
! 41969:
! 41970: do {
! 41971: rc = pagerLockDb(pPager, locktype);
! 41972: }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
! 41973: return rc;
! 41974: }
! 41975:
! 41976: /*
! 41977: ** Function assertTruncateConstraint(pPager) checks that one of the
! 41978: ** following is true for all dirty pages currently in the page-cache:
! 41979: **
! 41980: ** a) The page number is less than or equal to the size of the
! 41981: ** current database image, in pages, OR
! 41982: **
! 41983: ** b) if the page content were written at this time, it would not
! 41984: ** be necessary to write the current content out to the sub-journal
! 41985: ** (as determined by function subjRequiresPage()).
! 41986: **
! 41987: ** If the condition asserted by this function were not true, and the
! 41988: ** dirty page were to be discarded from the cache via the pagerStress()
! 41989: ** routine, pagerStress() would not write the current page content to
! 41990: ** the database file. If a savepoint transaction were rolled back after
! 41991: ** this happened, the correct behaviour would be to restore the current
! 41992: ** content of the page. However, since this content is not present in either
! 41993: ** the database file or the portion of the rollback journal and
! 41994: ** sub-journal rolled back the content could not be restored and the
! 41995: ** database image would become corrupt. It is therefore fortunate that
! 41996: ** this circumstance cannot arise.
! 41997: */
! 41998: #if defined(SQLITE_DEBUG)
! 41999: static void assertTruncateConstraintCb(PgHdr *pPg){
! 42000: assert( pPg->flags&PGHDR_DIRTY );
! 42001: assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
! 42002: }
! 42003: static void assertTruncateConstraint(Pager *pPager){
! 42004: sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
! 42005: }
! 42006: #else
! 42007: # define assertTruncateConstraint(pPager)
! 42008: #endif
! 42009:
! 42010: /*
! 42011: ** Truncate the in-memory database file image to nPage pages. This
! 42012: ** function does not actually modify the database file on disk. It
! 42013: ** just sets the internal state of the pager object so that the
! 42014: ** truncation will be done when the current transaction is committed.
! 42015: */
! 42016: SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
! 42017: assert( pPager->dbSize>=nPage );
! 42018: assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
! 42019: pPager->dbSize = nPage;
! 42020: assertTruncateConstraint(pPager);
! 42021: }
! 42022:
! 42023:
! 42024: /*
! 42025: ** This function is called before attempting a hot-journal rollback. It
! 42026: ** syncs the journal file to disk, then sets pPager->journalHdr to the
! 42027: ** size of the journal file so that the pager_playback() routine knows
! 42028: ** that the entire journal file has been synced.
! 42029: **
! 42030: ** Syncing a hot-journal to disk before attempting to roll it back ensures
! 42031: ** that if a power-failure occurs during the rollback, the process that
! 42032: ** attempts rollback following system recovery sees the same journal
! 42033: ** content as this process.
! 42034: **
! 42035: ** If everything goes as planned, SQLITE_OK is returned. Otherwise,
! 42036: ** an SQLite error code.
! 42037: */
! 42038: static int pagerSyncHotJournal(Pager *pPager){
! 42039: int rc = SQLITE_OK;
! 42040: if( !pPager->noSync ){
! 42041: rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
! 42042: }
! 42043: if( rc==SQLITE_OK ){
! 42044: rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
! 42045: }
! 42046: return rc;
! 42047: }
! 42048:
! 42049: /*
! 42050: ** Shutdown the page cache. Free all memory and close all files.
! 42051: **
! 42052: ** If a transaction was in progress when this routine is called, that
! 42053: ** transaction is rolled back. All outstanding pages are invalidated
! 42054: ** and their memory is freed. Any attempt to use a page associated
! 42055: ** with this page cache after this function returns will likely
! 42056: ** result in a coredump.
! 42057: **
! 42058: ** This function always succeeds. If a transaction is active an attempt
! 42059: ** is made to roll it back. If an error occurs during the rollback
! 42060: ** a hot journal may be left in the filesystem but no error is returned
! 42061: ** to the caller.
! 42062: */
! 42063: SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
! 42064: u8 *pTmp = (u8 *)pPager->pTmpSpace;
! 42065:
! 42066: assert( assert_pager_state(pPager) );
! 42067: disable_simulated_io_errors();
! 42068: sqlite3BeginBenignMalloc();
! 42069: /* pPager->errCode = 0; */
! 42070: pPager->exclusiveMode = 0;
! 42071: #ifndef SQLITE_OMIT_WAL
! 42072: sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
! 42073: pPager->pWal = 0;
! 42074: #endif
! 42075: pager_reset(pPager);
! 42076: if( MEMDB ){
! 42077: pager_unlock(pPager);
! 42078: }else{
! 42079: /* If it is open, sync the journal file before calling UnlockAndRollback.
! 42080: ** If this is not done, then an unsynced portion of the open journal
! 42081: ** file may be played back into the database. If a power failure occurs
! 42082: ** while this is happening, the database could become corrupt.
! 42083: **
! 42084: ** If an error occurs while trying to sync the journal, shift the pager
! 42085: ** into the ERROR state. This causes UnlockAndRollback to unlock the
! 42086: ** database and close the journal file without attempting to roll it
! 42087: ** back or finalize it. The next database user will have to do hot-journal
! 42088: ** rollback before accessing the database file.
! 42089: */
! 42090: if( isOpen(pPager->jfd) ){
! 42091: pager_error(pPager, pagerSyncHotJournal(pPager));
! 42092: }
! 42093: pagerUnlockAndRollback(pPager);
! 42094: }
! 42095: sqlite3EndBenignMalloc();
! 42096: enable_simulated_io_errors();
! 42097: PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
! 42098: IOTRACE(("CLOSE %p\n", pPager))
! 42099: sqlite3OsClose(pPager->jfd);
! 42100: sqlite3OsClose(pPager->fd);
! 42101: sqlite3PageFree(pTmp);
! 42102: sqlite3PcacheClose(pPager->pPCache);
! 42103:
! 42104: #ifdef SQLITE_HAS_CODEC
! 42105: if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
! 42106: #endif
! 42107:
! 42108: assert( !pPager->aSavepoint && !pPager->pInJournal );
! 42109: assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
! 42110:
! 42111: sqlite3_free(pPager);
! 42112: return SQLITE_OK;
! 42113: }
! 42114:
! 42115: #if !defined(NDEBUG) || defined(SQLITE_TEST)
! 42116: /*
! 42117: ** Return the page number for page pPg.
! 42118: */
! 42119: SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
! 42120: return pPg->pgno;
! 42121: }
! 42122: #endif
! 42123:
! 42124: /*
! 42125: ** Increment the reference count for page pPg.
! 42126: */
! 42127: SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
! 42128: sqlite3PcacheRef(pPg);
! 42129: }
! 42130:
! 42131: /*
! 42132: ** Sync the journal. In other words, make sure all the pages that have
! 42133: ** been written to the journal have actually reached the surface of the
! 42134: ** disk and can be restored in the event of a hot-journal rollback.
! 42135: **
! 42136: ** If the Pager.noSync flag is set, then this function is a no-op.
! 42137: ** Otherwise, the actions required depend on the journal-mode and the
! 42138: ** device characteristics of the the file-system, as follows:
! 42139: **
! 42140: ** * If the journal file is an in-memory journal file, no action need
! 42141: ** be taken.
! 42142: **
! 42143: ** * Otherwise, if the device does not support the SAFE_APPEND property,
! 42144: ** then the nRec field of the most recently written journal header
! 42145: ** is updated to contain the number of journal records that have
! 42146: ** been written following it. If the pager is operating in full-sync
! 42147: ** mode, then the journal file is synced before this field is updated.
! 42148: **
! 42149: ** * If the device does not support the SEQUENTIAL property, then
! 42150: ** journal file is synced.
! 42151: **
! 42152: ** Or, in pseudo-code:
! 42153: **
! 42154: ** if( NOT <in-memory journal> ){
! 42155: ** if( NOT SAFE_APPEND ){
! 42156: ** if( <full-sync mode> ) xSync(<journal file>);
! 42157: ** <update nRec field>
! 42158: ** }
! 42159: ** if( NOT SEQUENTIAL ) xSync(<journal file>);
! 42160: ** }
! 42161: **
! 42162: ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
! 42163: ** page currently held in memory before returning SQLITE_OK. If an IO
! 42164: ** error is encountered, then the IO error code is returned to the caller.
! 42165: */
! 42166: static int syncJournal(Pager *pPager, int newHdr){
! 42167: int rc; /* Return code */
! 42168:
! 42169: assert( pPager->eState==PAGER_WRITER_CACHEMOD
! 42170: || pPager->eState==PAGER_WRITER_DBMOD
! 42171: );
! 42172: assert( assert_pager_state(pPager) );
! 42173: assert( !pagerUseWal(pPager) );
! 42174:
! 42175: rc = sqlite3PagerExclusiveLock(pPager);
! 42176: if( rc!=SQLITE_OK ) return rc;
! 42177:
! 42178: if( !pPager->noSync ){
! 42179: assert( !pPager->tempFile );
! 42180: if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
! 42181: const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
! 42182: assert( isOpen(pPager->jfd) );
! 42183:
! 42184: if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
! 42185: /* This block deals with an obscure problem. If the last connection
! 42186: ** that wrote to this database was operating in persistent-journal
! 42187: ** mode, then the journal file may at this point actually be larger
! 42188: ** than Pager.journalOff bytes. If the next thing in the journal
! 42189: ** file happens to be a journal-header (written as part of the
! 42190: ** previous connection's transaction), and a crash or power-failure
! 42191: ** occurs after nRec is updated but before this connection writes
! 42192: ** anything else to the journal file (or commits/rolls back its
! 42193: ** transaction), then SQLite may become confused when doing the
! 42194: ** hot-journal rollback following recovery. It may roll back all
! 42195: ** of this connections data, then proceed to rolling back the old,
! 42196: ** out-of-date data that follows it. Database corruption.
! 42197: **
! 42198: ** To work around this, if the journal file does appear to contain
! 42199: ** a valid header following Pager.journalOff, then write a 0x00
! 42200: ** byte to the start of it to prevent it from being recognized.
! 42201: **
! 42202: ** Variable iNextHdrOffset is set to the offset at which this
! 42203: ** problematic header will occur, if it exists. aMagic is used
! 42204: ** as a temporary buffer to inspect the first couple of bytes of
! 42205: ** the potential journal header.
! 42206: */
! 42207: i64 iNextHdrOffset;
! 42208: u8 aMagic[8];
! 42209: u8 zHeader[sizeof(aJournalMagic)+4];
! 42210:
! 42211: memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
! 42212: put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
! 42213:
! 42214: iNextHdrOffset = journalHdrOffset(pPager);
! 42215: rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
! 42216: if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
! 42217: static const u8 zerobyte = 0;
! 42218: rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
! 42219: }
! 42220: if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
! 42221: return rc;
! 42222: }
! 42223:
! 42224: /* Write the nRec value into the journal file header. If in
! 42225: ** full-synchronous mode, sync the journal first. This ensures that
! 42226: ** all data has really hit the disk before nRec is updated to mark
! 42227: ** it as a candidate for rollback.
! 42228: **
! 42229: ** This is not required if the persistent media supports the
! 42230: ** SAFE_APPEND property. Because in this case it is not possible
! 42231: ** for garbage data to be appended to the file, the nRec field
! 42232: ** is populated with 0xFFFFFFFF when the journal header is written
! 42233: ** and never needs to be updated.
! 42234: */
! 42235: if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
! 42236: PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
! 42237: IOTRACE(("JSYNC %p\n", pPager))
! 42238: rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
! 42239: if( rc!=SQLITE_OK ) return rc;
! 42240: }
! 42241: IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
! 42242: rc = sqlite3OsWrite(
! 42243: pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
! 42244: );
! 42245: if( rc!=SQLITE_OK ) return rc;
! 42246: }
! 42247: if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
! 42248: PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
! 42249: IOTRACE(("JSYNC %p\n", pPager))
! 42250: rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
! 42251: (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
! 42252: );
! 42253: if( rc!=SQLITE_OK ) return rc;
! 42254: }
! 42255:
! 42256: pPager->journalHdr = pPager->journalOff;
! 42257: if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
! 42258: pPager->nRec = 0;
! 42259: rc = writeJournalHdr(pPager);
! 42260: if( rc!=SQLITE_OK ) return rc;
! 42261: }
! 42262: }else{
! 42263: pPager->journalHdr = pPager->journalOff;
! 42264: }
! 42265: }
! 42266:
! 42267: /* Unless the pager is in noSync mode, the journal file was just
! 42268: ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
! 42269: ** all pages.
! 42270: */
! 42271: sqlite3PcacheClearSyncFlags(pPager->pPCache);
! 42272: pPager->eState = PAGER_WRITER_DBMOD;
! 42273: assert( assert_pager_state(pPager) );
! 42274: return SQLITE_OK;
! 42275: }
! 42276:
! 42277: /*
! 42278: ** The argument is the first in a linked list of dirty pages connected
! 42279: ** by the PgHdr.pDirty pointer. This function writes each one of the
! 42280: ** in-memory pages in the list to the database file. The argument may
! 42281: ** be NULL, representing an empty list. In this case this function is
! 42282: ** a no-op.
! 42283: **
! 42284: ** The pager must hold at least a RESERVED lock when this function
! 42285: ** is called. Before writing anything to the database file, this lock
! 42286: ** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
! 42287: ** SQLITE_BUSY is returned and no data is written to the database file.
! 42288: **
! 42289: ** If the pager is a temp-file pager and the actual file-system file
! 42290: ** is not yet open, it is created and opened before any data is
! 42291: ** written out.
! 42292: **
! 42293: ** Once the lock has been upgraded and, if necessary, the file opened,
! 42294: ** the pages are written out to the database file in list order. Writing
! 42295: ** a page is skipped if it meets either of the following criteria:
! 42296: **
! 42297: ** * The page number is greater than Pager.dbSize, or
! 42298: ** * The PGHDR_DONT_WRITE flag is set on the page.
! 42299: **
! 42300: ** If writing out a page causes the database file to grow, Pager.dbFileSize
! 42301: ** is updated accordingly. If page 1 is written out, then the value cached
! 42302: ** in Pager.dbFileVers[] is updated to match the new value stored in
! 42303: ** the database file.
! 42304: **
! 42305: ** If everything is successful, SQLITE_OK is returned. If an IO error
! 42306: ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
! 42307: ** be obtained, SQLITE_BUSY is returned.
! 42308: */
! 42309: static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
! 42310: int rc = SQLITE_OK; /* Return code */
! 42311:
! 42312: /* This function is only called for rollback pagers in WRITER_DBMOD state. */
! 42313: assert( !pagerUseWal(pPager) );
! 42314: assert( pPager->eState==PAGER_WRITER_DBMOD );
! 42315: assert( pPager->eLock==EXCLUSIVE_LOCK );
! 42316:
! 42317: /* If the file is a temp-file has not yet been opened, open it now. It
! 42318: ** is not possible for rc to be other than SQLITE_OK if this branch
! 42319: ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
! 42320: */
! 42321: if( !isOpen(pPager->fd) ){
! 42322: assert( pPager->tempFile && rc==SQLITE_OK );
! 42323: rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
! 42324: }
! 42325:
! 42326: /* Before the first write, give the VFS a hint of what the final
! 42327: ** file size will be.
! 42328: */
! 42329: assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
! 42330: if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
! 42331: sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
! 42332: sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
! 42333: pPager->dbHintSize = pPager->dbSize;
! 42334: }
! 42335:
! 42336: while( rc==SQLITE_OK && pList ){
! 42337: Pgno pgno = pList->pgno;
! 42338:
! 42339: /* If there are dirty pages in the page cache with page numbers greater
! 42340: ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
! 42341: ** make the file smaller (presumably by auto-vacuum code). Do not write
! 42342: ** any such pages to the file.
! 42343: **
! 42344: ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
! 42345: ** set (set by sqlite3PagerDontWrite()).
! 42346: */
! 42347: if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
! 42348: i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
! 42349: char *pData; /* Data to write */
! 42350:
! 42351: assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
! 42352: if( pList->pgno==1 ) pager_write_changecounter(pList);
! 42353:
! 42354: /* Encode the database */
! 42355: CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
! 42356:
! 42357: /* Write out the page data. */
! 42358: rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
! 42359:
! 42360: /* If page 1 was just written, update Pager.dbFileVers to match
! 42361: ** the value now stored in the database file. If writing this
! 42362: ** page caused the database file to grow, update dbFileSize.
! 42363: */
! 42364: if( pgno==1 ){
! 42365: memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
! 42366: }
! 42367: if( pgno>pPager->dbFileSize ){
! 42368: pPager->dbFileSize = pgno;
! 42369: }
! 42370:
! 42371: /* Update any backup objects copying the contents of this pager. */
! 42372: sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
! 42373:
! 42374: PAGERTRACE(("STORE %d page %d hash(%08x)\n",
! 42375: PAGERID(pPager), pgno, pager_pagehash(pList)));
! 42376: IOTRACE(("PGOUT %p %d\n", pPager, pgno));
! 42377: PAGER_INCR(sqlite3_pager_writedb_count);
! 42378: PAGER_INCR(pPager->nWrite);
! 42379: }else{
! 42380: PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
! 42381: }
! 42382: pager_set_pagehash(pList);
! 42383: pList = pList->pDirty;
! 42384: }
! 42385:
! 42386: return rc;
! 42387: }
! 42388:
! 42389: /*
! 42390: ** Ensure that the sub-journal file is open. If it is already open, this
! 42391: ** function is a no-op.
! 42392: **
! 42393: ** SQLITE_OK is returned if everything goes according to plan. An
! 42394: ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
! 42395: ** fails.
! 42396: */
! 42397: static int openSubJournal(Pager *pPager){
! 42398: int rc = SQLITE_OK;
! 42399: if( !isOpen(pPager->sjfd) ){
! 42400: if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
! 42401: sqlite3MemJournalOpen(pPager->sjfd);
! 42402: }else{
! 42403: rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
! 42404: }
! 42405: }
! 42406: return rc;
! 42407: }
! 42408:
! 42409: /*
! 42410: ** Append a record of the current state of page pPg to the sub-journal.
! 42411: ** It is the callers responsibility to use subjRequiresPage() to check
! 42412: ** that it is really required before calling this function.
! 42413: **
! 42414: ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
! 42415: ** for all open savepoints before returning.
! 42416: **
! 42417: ** This function returns SQLITE_OK if everything is successful, an IO
! 42418: ** error code if the attempt to write to the sub-journal fails, or
! 42419: ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
! 42420: ** bitvec.
! 42421: */
! 42422: static int subjournalPage(PgHdr *pPg){
! 42423: int rc = SQLITE_OK;
! 42424: Pager *pPager = pPg->pPager;
! 42425: if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
! 42426:
! 42427: /* Open the sub-journal, if it has not already been opened */
! 42428: assert( pPager->useJournal );
! 42429: assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
! 42430: assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
! 42431: assert( pagerUseWal(pPager)
! 42432: || pageInJournal(pPg)
! 42433: || pPg->pgno>pPager->dbOrigSize
! 42434: );
! 42435: rc = openSubJournal(pPager);
! 42436:
! 42437: /* If the sub-journal was opened successfully (or was already open),
! 42438: ** write the journal record into the file. */
! 42439: if( rc==SQLITE_OK ){
! 42440: void *pData = pPg->pData;
! 42441: i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
! 42442: char *pData2;
! 42443:
! 42444: CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
! 42445: PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
! 42446: rc = write32bits(pPager->sjfd, offset, pPg->pgno);
! 42447: if( rc==SQLITE_OK ){
! 42448: rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
! 42449: }
! 42450: }
! 42451: }
! 42452: if( rc==SQLITE_OK ){
! 42453: pPager->nSubRec++;
! 42454: assert( pPager->nSavepoint>0 );
! 42455: rc = addToSavepointBitvecs(pPager, pPg->pgno);
! 42456: }
! 42457: return rc;
! 42458: }
! 42459:
! 42460: /*
! 42461: ** This function is called by the pcache layer when it has reached some
! 42462: ** soft memory limit. The first argument is a pointer to a Pager object
! 42463: ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
! 42464: ** database). The second argument is a reference to a page that is
! 42465: ** currently dirty but has no outstanding references. The page
! 42466: ** is always associated with the Pager object passed as the first
! 42467: ** argument.
! 42468: **
! 42469: ** The job of this function is to make pPg clean by writing its contents
! 42470: ** out to the database file, if possible. This may involve syncing the
! 42471: ** journal file.
! 42472: **
! 42473: ** If successful, sqlite3PcacheMakeClean() is called on the page and
! 42474: ** SQLITE_OK returned. If an IO error occurs while trying to make the
! 42475: ** page clean, the IO error code is returned. If the page cannot be
! 42476: ** made clean for some other reason, but no error occurs, then SQLITE_OK
! 42477: ** is returned by sqlite3PcacheMakeClean() is not called.
! 42478: */
! 42479: static int pagerStress(void *p, PgHdr *pPg){
! 42480: Pager *pPager = (Pager *)p;
! 42481: int rc = SQLITE_OK;
! 42482:
! 42483: assert( pPg->pPager==pPager );
! 42484: assert( pPg->flags&PGHDR_DIRTY );
! 42485:
! 42486: /* The doNotSyncSpill flag is set during times when doing a sync of
! 42487: ** journal (and adding a new header) is not allowed. This occurs
! 42488: ** during calls to sqlite3PagerWrite() while trying to journal multiple
! 42489: ** pages belonging to the same sector.
! 42490: **
! 42491: ** The doNotSpill flag inhibits all cache spilling regardless of whether
! 42492: ** or not a sync is required. This is set during a rollback.
! 42493: **
! 42494: ** Spilling is also prohibited when in an error state since that could
! 42495: ** lead to database corruption. In the current implementaton it
! 42496: ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
! 42497: ** while in the error state, hence it is impossible for this routine to
! 42498: ** be called in the error state. Nevertheless, we include a NEVER()
! 42499: ** test for the error state as a safeguard against future changes.
! 42500: */
! 42501: if( NEVER(pPager->errCode) ) return SQLITE_OK;
! 42502: if( pPager->doNotSpill ) return SQLITE_OK;
! 42503: if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
! 42504: return SQLITE_OK;
! 42505: }
! 42506:
! 42507: pPg->pDirty = 0;
! 42508: if( pagerUseWal(pPager) ){
! 42509: /* Write a single frame for this page to the log. */
! 42510: if( subjRequiresPage(pPg) ){
! 42511: rc = subjournalPage(pPg);
! 42512: }
! 42513: if( rc==SQLITE_OK ){
! 42514: rc = pagerWalFrames(pPager, pPg, 0, 0);
! 42515: }
! 42516: }else{
! 42517:
! 42518: /* Sync the journal file if required. */
! 42519: if( pPg->flags&PGHDR_NEED_SYNC
! 42520: || pPager->eState==PAGER_WRITER_CACHEMOD
! 42521: ){
! 42522: rc = syncJournal(pPager, 1);
! 42523: }
! 42524:
! 42525: /* If the page number of this page is larger than the current size of
! 42526: ** the database image, it may need to be written to the sub-journal.
! 42527: ** This is because the call to pager_write_pagelist() below will not
! 42528: ** actually write data to the file in this case.
! 42529: **
! 42530: ** Consider the following sequence of events:
! 42531: **
! 42532: ** BEGIN;
! 42533: ** <journal page X>
! 42534: ** <modify page X>
! 42535: ** SAVEPOINT sp;
! 42536: ** <shrink database file to Y pages>
! 42537: ** pagerStress(page X)
! 42538: ** ROLLBACK TO sp;
! 42539: **
! 42540: ** If (X>Y), then when pagerStress is called page X will not be written
! 42541: ** out to the database file, but will be dropped from the cache. Then,
! 42542: ** following the "ROLLBACK TO sp" statement, reading page X will read
! 42543: ** data from the database file. This will be the copy of page X as it
! 42544: ** was when the transaction started, not as it was when "SAVEPOINT sp"
! 42545: ** was executed.
! 42546: **
! 42547: ** The solution is to write the current data for page X into the
! 42548: ** sub-journal file now (if it is not already there), so that it will
! 42549: ** be restored to its current value when the "ROLLBACK TO sp" is
! 42550: ** executed.
! 42551: */
! 42552: if( NEVER(
! 42553: rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
! 42554: ) ){
! 42555: rc = subjournalPage(pPg);
! 42556: }
! 42557:
! 42558: /* Write the contents of the page out to the database file. */
! 42559: if( rc==SQLITE_OK ){
! 42560: assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
! 42561: rc = pager_write_pagelist(pPager, pPg);
! 42562: }
! 42563: }
! 42564:
! 42565: /* Mark the page as clean. */
! 42566: if( rc==SQLITE_OK ){
! 42567: PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
! 42568: sqlite3PcacheMakeClean(pPg);
! 42569: }
! 42570:
! 42571: return pager_error(pPager, rc);
! 42572: }
! 42573:
! 42574:
! 42575: /*
! 42576: ** Allocate and initialize a new Pager object and put a pointer to it
! 42577: ** in *ppPager. The pager should eventually be freed by passing it
! 42578: ** to sqlite3PagerClose().
! 42579: **
! 42580: ** The zFilename argument is the path to the database file to open.
! 42581: ** If zFilename is NULL then a randomly-named temporary file is created
! 42582: ** and used as the file to be cached. Temporary files are be deleted
! 42583: ** automatically when they are closed. If zFilename is ":memory:" then
! 42584: ** all information is held in cache. It is never written to disk.
! 42585: ** This can be used to implement an in-memory database.
! 42586: **
! 42587: ** The nExtra parameter specifies the number of bytes of space allocated
! 42588: ** along with each page reference. This space is available to the user
! 42589: ** via the sqlite3PagerGetExtra() API.
! 42590: **
! 42591: ** The flags argument is used to specify properties that affect the
! 42592: ** operation of the pager. It should be passed some bitwise combination
! 42593: ** of the PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK flags.
! 42594: **
! 42595: ** The vfsFlags parameter is a bitmask to pass to the flags parameter
! 42596: ** of the xOpen() method of the supplied VFS when opening files.
! 42597: **
! 42598: ** If the pager object is allocated and the specified file opened
! 42599: ** successfully, SQLITE_OK is returned and *ppPager set to point to
! 42600: ** the new pager object. If an error occurs, *ppPager is set to NULL
! 42601: ** and error code returned. This function may return SQLITE_NOMEM
! 42602: ** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
! 42603: ** various SQLITE_IO_XXX errors.
! 42604: */
! 42605: SQLITE_PRIVATE int sqlite3PagerOpen(
! 42606: sqlite3_vfs *pVfs, /* The virtual file system to use */
! 42607: Pager **ppPager, /* OUT: Return the Pager structure here */
! 42608: const char *zFilename, /* Name of the database file to open */
! 42609: int nExtra, /* Extra bytes append to each in-memory page */
! 42610: int flags, /* flags controlling this file */
! 42611: int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */
! 42612: void (*xReinit)(DbPage*) /* Function to reinitialize pages */
! 42613: ){
! 42614: u8 *pPtr;
! 42615: Pager *pPager = 0; /* Pager object to allocate and return */
! 42616: int rc = SQLITE_OK; /* Return code */
! 42617: int tempFile = 0; /* True for temp files (incl. in-memory files) */
! 42618: int memDb = 0; /* True if this is an in-memory file */
! 42619: int readOnly = 0; /* True if this is a read-only file */
! 42620: int journalFileSize; /* Bytes to allocate for each journal fd */
! 42621: char *zPathname = 0; /* Full path to database file */
! 42622: int nPathname = 0; /* Number of bytes in zPathname */
! 42623: int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
! 42624: int noReadlock = (flags & PAGER_NO_READLOCK)!=0; /* True to omit read-lock */
! 42625: int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
! 42626: u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
! 42627: const char *zUri = 0; /* URI args to copy */
! 42628: int nUri = 0; /* Number of bytes of URI args at *zUri */
! 42629:
! 42630: /* Figure out how much space is required for each journal file-handle
! 42631: ** (there are two of them, the main journal and the sub-journal). This
! 42632: ** is the maximum space required for an in-memory journal file handle
! 42633: ** and a regular journal file-handle. Note that a "regular journal-handle"
! 42634: ** may be a wrapper capable of caching the first portion of the journal
! 42635: ** file in memory to implement the atomic-write optimization (see
! 42636: ** source file journal.c).
! 42637: */
! 42638: if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
! 42639: journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
! 42640: }else{
! 42641: journalFileSize = ROUND8(sqlite3MemJournalSize());
! 42642: }
! 42643:
! 42644: /* Set the output variable to NULL in case an error occurs. */
! 42645: *ppPager = 0;
! 42646:
! 42647: #ifndef SQLITE_OMIT_MEMORYDB
! 42648: if( flags & PAGER_MEMORY ){
! 42649: memDb = 1;
! 42650: zFilename = 0;
! 42651: }
! 42652: #endif
! 42653:
! 42654: /* Compute and store the full pathname in an allocated buffer pointed
! 42655: ** to by zPathname, length nPathname. Or, if this is a temporary file,
! 42656: ** leave both nPathname and zPathname set to 0.
! 42657: */
! 42658: if( zFilename && zFilename[0] ){
! 42659: const char *z;
! 42660: nPathname = pVfs->mxPathname+1;
! 42661: zPathname = sqlite3Malloc(nPathname*2);
! 42662: if( zPathname==0 ){
! 42663: return SQLITE_NOMEM;
! 42664: }
! 42665: zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
! 42666: rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
! 42667: nPathname = sqlite3Strlen30(zPathname);
! 42668: z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
! 42669: while( *z ){
! 42670: z += sqlite3Strlen30(z)+1;
! 42671: z += sqlite3Strlen30(z)+1;
! 42672: }
! 42673: nUri = (int)(&z[1] - zUri);
! 42674: assert( nUri>=0 );
! 42675: if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
! 42676: /* This branch is taken when the journal path required by
! 42677: ** the database being opened will be more than pVfs->mxPathname
! 42678: ** bytes in length. This means the database cannot be opened,
! 42679: ** as it will not be possible to open the journal file or even
! 42680: ** check for a hot-journal before reading.
! 42681: */
! 42682: rc = SQLITE_CANTOPEN_BKPT;
! 42683: }
! 42684: if( rc!=SQLITE_OK ){
! 42685: sqlite3_free(zPathname);
! 42686: return rc;
! 42687: }
! 42688: }
! 42689:
! 42690: /* Allocate memory for the Pager structure, PCache object, the
! 42691: ** three file descriptors, the database file name and the journal
! 42692: ** file name. The layout in memory is as follows:
! 42693: **
! 42694: ** Pager object (sizeof(Pager) bytes)
! 42695: ** PCache object (sqlite3PcacheSize() bytes)
! 42696: ** Database file handle (pVfs->szOsFile bytes)
! 42697: ** Sub-journal file handle (journalFileSize bytes)
! 42698: ** Main journal file handle (journalFileSize bytes)
! 42699: ** Database file name (nPathname+1 bytes)
! 42700: ** Journal file name (nPathname+8+1 bytes)
! 42701: */
! 42702: pPtr = (u8 *)sqlite3MallocZero(
! 42703: ROUND8(sizeof(*pPager)) + /* Pager structure */
! 42704: ROUND8(pcacheSize) + /* PCache object */
! 42705: ROUND8(pVfs->szOsFile) + /* The main db file */
! 42706: journalFileSize * 2 + /* The two journal files */
! 42707: nPathname + 1 + nUri + /* zFilename */
! 42708: nPathname + 8 + 2 /* zJournal */
! 42709: #ifndef SQLITE_OMIT_WAL
! 42710: + nPathname + 4 + 2 /* zWal */
! 42711: #endif
! 42712: );
! 42713: assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
! 42714: if( !pPtr ){
! 42715: sqlite3_free(zPathname);
! 42716: return SQLITE_NOMEM;
! 42717: }
! 42718: pPager = (Pager*)(pPtr);
! 42719: pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
! 42720: pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
! 42721: pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
! 42722: pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize);
! 42723: pPager->zFilename = (char*)(pPtr += journalFileSize);
! 42724: assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
! 42725:
! 42726: /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
! 42727: if( zPathname ){
! 42728: assert( nPathname>0 );
! 42729: pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri);
! 42730: memcpy(pPager->zFilename, zPathname, nPathname);
! 42731: memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
! 42732: memcpy(pPager->zJournal, zPathname, nPathname);
! 42733: memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+1);
! 42734: sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
! 42735: #ifndef SQLITE_OMIT_WAL
! 42736: pPager->zWal = &pPager->zJournal[nPathname+8+1];
! 42737: memcpy(pPager->zWal, zPathname, nPathname);
! 42738: memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
! 42739: sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
! 42740: #endif
! 42741: sqlite3_free(zPathname);
! 42742: }
! 42743: pPager->pVfs = pVfs;
! 42744: pPager->vfsFlags = vfsFlags;
! 42745:
! 42746: /* Open the pager file.
! 42747: */
! 42748: if( zFilename && zFilename[0] ){
! 42749: int fout = 0; /* VFS flags returned by xOpen() */
! 42750: rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
! 42751: assert( !memDb );
! 42752: readOnly = (fout&SQLITE_OPEN_READONLY);
! 42753:
! 42754: /* If the file was successfully opened for read/write access,
! 42755: ** choose a default page size in case we have to create the
! 42756: ** database file. The default page size is the maximum of:
! 42757: **
! 42758: ** + SQLITE_DEFAULT_PAGE_SIZE,
! 42759: ** + The value returned by sqlite3OsSectorSize()
! 42760: ** + The largest page size that can be written atomically.
! 42761: */
! 42762: if( rc==SQLITE_OK && !readOnly ){
! 42763: setSectorSize(pPager);
! 42764: assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
! 42765: if( szPageDflt<pPager->sectorSize ){
! 42766: if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
! 42767: szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
! 42768: }else{
! 42769: szPageDflt = (u32)pPager->sectorSize;
! 42770: }
! 42771: }
! 42772: #ifdef SQLITE_ENABLE_ATOMIC_WRITE
! 42773: {
! 42774: int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
! 42775: int ii;
! 42776: assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
! 42777: assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
! 42778: assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
! 42779: for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
! 42780: if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
! 42781: szPageDflt = ii;
! 42782: }
! 42783: }
! 42784: }
! 42785: #endif
! 42786: }
! 42787: }else{
! 42788: /* If a temporary file is requested, it is not opened immediately.
! 42789: ** In this case we accept the default page size and delay actually
! 42790: ** opening the file until the first call to OsWrite().
! 42791: **
! 42792: ** This branch is also run for an in-memory database. An in-memory
! 42793: ** database is the same as a temp-file that is never written out to
! 42794: ** disk and uses an in-memory rollback journal.
! 42795: */
! 42796: tempFile = 1;
! 42797: pPager->eState = PAGER_READER;
! 42798: pPager->eLock = EXCLUSIVE_LOCK;
! 42799: readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
! 42800: }
! 42801:
! 42802: /* The following call to PagerSetPagesize() serves to set the value of
! 42803: ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
! 42804: */
! 42805: if( rc==SQLITE_OK ){
! 42806: assert( pPager->memDb==0 );
! 42807: rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
! 42808: testcase( rc!=SQLITE_OK );
! 42809: }
! 42810:
! 42811: /* If an error occurred in either of the blocks above, free the
! 42812: ** Pager structure and close the file.
! 42813: */
! 42814: if( rc!=SQLITE_OK ){
! 42815: assert( !pPager->pTmpSpace );
! 42816: sqlite3OsClose(pPager->fd);
! 42817: sqlite3_free(pPager);
! 42818: return rc;
! 42819: }
! 42820:
! 42821: /* Initialize the PCache object. */
! 42822: assert( nExtra<1000 );
! 42823: nExtra = ROUND8(nExtra);
! 42824: sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
! 42825: !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
! 42826:
! 42827: PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
! 42828: IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
! 42829:
! 42830: pPager->useJournal = (u8)useJournal;
! 42831: pPager->noReadlock = (noReadlock && readOnly) ?1:0;
! 42832: /* pPager->stmtOpen = 0; */
! 42833: /* pPager->stmtInUse = 0; */
! 42834: /* pPager->nRef = 0; */
! 42835: /* pPager->stmtSize = 0; */
! 42836: /* pPager->stmtJSize = 0; */
! 42837: /* pPager->nPage = 0; */
! 42838: pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
! 42839: /* pPager->state = PAGER_UNLOCK; */
! 42840: #if 0
! 42841: assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
! 42842: #endif
! 42843: /* pPager->errMask = 0; */
! 42844: pPager->tempFile = (u8)tempFile;
! 42845: assert( tempFile==PAGER_LOCKINGMODE_NORMAL
! 42846: || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
! 42847: assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
! 42848: pPager->exclusiveMode = (u8)tempFile;
! 42849: pPager->changeCountDone = pPager->tempFile;
! 42850: pPager->memDb = (u8)memDb;
! 42851: pPager->readOnly = (u8)readOnly;
! 42852: assert( useJournal || pPager->tempFile );
! 42853: pPager->noSync = pPager->tempFile;
! 42854: if( pPager->noSync ){
! 42855: assert( pPager->fullSync==0 );
! 42856: assert( pPager->syncFlags==0 );
! 42857: assert( pPager->walSyncFlags==0 );
! 42858: assert( pPager->ckptSyncFlags==0 );
! 42859: }else{
! 42860: pPager->fullSync = 1;
! 42861: pPager->syncFlags = SQLITE_SYNC_NORMAL;
! 42862: pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
! 42863: pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
! 42864: }
! 42865: /* pPager->pFirst = 0; */
! 42866: /* pPager->pFirstSynced = 0; */
! 42867: /* pPager->pLast = 0; */
! 42868: pPager->nExtra = (u16)nExtra;
! 42869: pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
! 42870: assert( isOpen(pPager->fd) || tempFile );
! 42871: setSectorSize(pPager);
! 42872: if( !useJournal ){
! 42873: pPager->journalMode = PAGER_JOURNALMODE_OFF;
! 42874: }else if( memDb ){
! 42875: pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
! 42876: }
! 42877: /* pPager->xBusyHandler = 0; */
! 42878: /* pPager->pBusyHandlerArg = 0; */
! 42879: pPager->xReiniter = xReinit;
! 42880: /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
! 42881:
! 42882: *ppPager = pPager;
! 42883: return SQLITE_OK;
! 42884: }
! 42885:
! 42886:
! 42887:
! 42888: /*
! 42889: ** This function is called after transitioning from PAGER_UNLOCK to
! 42890: ** PAGER_SHARED state. It tests if there is a hot journal present in
! 42891: ** the file-system for the given pager. A hot journal is one that
! 42892: ** needs to be played back. According to this function, a hot-journal
! 42893: ** file exists if the following criteria are met:
! 42894: **
! 42895: ** * The journal file exists in the file system, and
! 42896: ** * No process holds a RESERVED or greater lock on the database file, and
! 42897: ** * The database file itself is greater than 0 bytes in size, and
! 42898: ** * The first byte of the journal file exists and is not 0x00.
! 42899: **
! 42900: ** If the current size of the database file is 0 but a journal file
! 42901: ** exists, that is probably an old journal left over from a prior
! 42902: ** database with the same name. In this case the journal file is
! 42903: ** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
! 42904: ** is returned.
! 42905: **
! 42906: ** This routine does not check if there is a master journal filename
! 42907: ** at the end of the file. If there is, and that master journal file
! 42908: ** does not exist, then the journal file is not really hot. In this
! 42909: ** case this routine will return a false-positive. The pager_playback()
! 42910: ** routine will discover that the journal file is not really hot and
! 42911: ** will not roll it back.
! 42912: **
! 42913: ** If a hot-journal file is found to exist, *pExists is set to 1 and
! 42914: ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
! 42915: ** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
! 42916: ** to determine whether or not a hot-journal file exists, the IO error
! 42917: ** code is returned and the value of *pExists is undefined.
! 42918: */
! 42919: static int hasHotJournal(Pager *pPager, int *pExists){
! 42920: sqlite3_vfs * const pVfs = pPager->pVfs;
! 42921: int rc = SQLITE_OK; /* Return code */
! 42922: int exists = 1; /* True if a journal file is present */
! 42923: int jrnlOpen = !!isOpen(pPager->jfd);
! 42924:
! 42925: assert( pPager->useJournal );
! 42926: assert( isOpen(pPager->fd) );
! 42927: assert( pPager->eState==PAGER_OPEN );
! 42928:
! 42929: assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
! 42930: SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
! 42931: ));
! 42932:
! 42933: *pExists = 0;
! 42934: if( !jrnlOpen ){
! 42935: rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
! 42936: }
! 42937: if( rc==SQLITE_OK && exists ){
! 42938: int locked = 0; /* True if some process holds a RESERVED lock */
! 42939:
! 42940: /* Race condition here: Another process might have been holding the
! 42941: ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
! 42942: ** call above, but then delete the journal and drop the lock before
! 42943: ** we get to the following sqlite3OsCheckReservedLock() call. If that
! 42944: ** is the case, this routine might think there is a hot journal when
! 42945: ** in fact there is none. This results in a false-positive which will
! 42946: ** be dealt with by the playback routine. Ticket #3883.
! 42947: */
! 42948: rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
! 42949: if( rc==SQLITE_OK && !locked ){
! 42950: Pgno nPage; /* Number of pages in database file */
! 42951:
! 42952: /* Check the size of the database file. If it consists of 0 pages,
! 42953: ** then delete the journal file. See the header comment above for
! 42954: ** the reasoning here. Delete the obsolete journal file under
! 42955: ** a RESERVED lock to avoid race conditions and to avoid violating
! 42956: ** [H33020].
! 42957: */
! 42958: rc = pagerPagecount(pPager, &nPage);
! 42959: if( rc==SQLITE_OK ){
! 42960: if( nPage==0 ){
! 42961: sqlite3BeginBenignMalloc();
! 42962: if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
! 42963: sqlite3OsDelete(pVfs, pPager->zJournal, 0);
! 42964: if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
! 42965: }
! 42966: sqlite3EndBenignMalloc();
! 42967: }else{
! 42968: /* The journal file exists and no other connection has a reserved
! 42969: ** or greater lock on the database file. Now check that there is
! 42970: ** at least one non-zero bytes at the start of the journal file.
! 42971: ** If there is, then we consider this journal to be hot. If not,
! 42972: ** it can be ignored.
! 42973: */
! 42974: if( !jrnlOpen ){
! 42975: int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
! 42976: rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
! 42977: }
! 42978: if( rc==SQLITE_OK ){
! 42979: u8 first = 0;
! 42980: rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
! 42981: if( rc==SQLITE_IOERR_SHORT_READ ){
! 42982: rc = SQLITE_OK;
! 42983: }
! 42984: if( !jrnlOpen ){
! 42985: sqlite3OsClose(pPager->jfd);
! 42986: }
! 42987: *pExists = (first!=0);
! 42988: }else if( rc==SQLITE_CANTOPEN ){
! 42989: /* If we cannot open the rollback journal file in order to see if
! 42990: ** its has a zero header, that might be due to an I/O error, or
! 42991: ** it might be due to the race condition described above and in
! 42992: ** ticket #3883. Either way, assume that the journal is hot.
! 42993: ** This might be a false positive. But if it is, then the
! 42994: ** automatic journal playback and recovery mechanism will deal
! 42995: ** with it under an EXCLUSIVE lock where we do not need to
! 42996: ** worry so much with race conditions.
! 42997: */
! 42998: *pExists = 1;
! 42999: rc = SQLITE_OK;
! 43000: }
! 43001: }
! 43002: }
! 43003: }
! 43004: }
! 43005:
! 43006: return rc;
! 43007: }
! 43008:
! 43009: /*
! 43010: ** This function is called to obtain a shared lock on the database file.
! 43011: ** It is illegal to call sqlite3PagerAcquire() until after this function
! 43012: ** has been successfully called. If a shared-lock is already held when
! 43013: ** this function is called, it is a no-op.
! 43014: **
! 43015: ** The following operations are also performed by this function.
! 43016: **
! 43017: ** 1) If the pager is currently in PAGER_OPEN state (no lock held
! 43018: ** on the database file), then an attempt is made to obtain a
! 43019: ** SHARED lock on the database file. Immediately after obtaining
! 43020: ** the SHARED lock, the file-system is checked for a hot-journal,
! 43021: ** which is played back if present. Following any hot-journal
! 43022: ** rollback, the contents of the cache are validated by checking
! 43023: ** the 'change-counter' field of the database file header and
! 43024: ** discarded if they are found to be invalid.
! 43025: **
! 43026: ** 2) If the pager is running in exclusive-mode, and there are currently
! 43027: ** no outstanding references to any pages, and is in the error state,
! 43028: ** then an attempt is made to clear the error state by discarding
! 43029: ** the contents of the page cache and rolling back any open journal
! 43030: ** file.
! 43031: **
! 43032: ** If everything is successful, SQLITE_OK is returned. If an IO error
! 43033: ** occurs while locking the database, checking for a hot-journal file or
! 43034: ** rolling back a journal file, the IO error code is returned.
! 43035: */
! 43036: SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
! 43037: int rc = SQLITE_OK; /* Return code */
! 43038:
! 43039: /* This routine is only called from b-tree and only when there are no
! 43040: ** outstanding pages. This implies that the pager state should either
! 43041: ** be OPEN or READER. READER is only possible if the pager is or was in
! 43042: ** exclusive access mode.
! 43043: */
! 43044: assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
! 43045: assert( assert_pager_state(pPager) );
! 43046: assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
! 43047: if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
! 43048:
! 43049: if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
! 43050: int bHotJournal = 1; /* True if there exists a hot journal-file */
! 43051:
! 43052: assert( !MEMDB );
! 43053: assert( pPager->noReadlock==0 || pPager->readOnly );
! 43054:
! 43055: if( pPager->noReadlock==0 ){
! 43056: rc = pager_wait_on_lock(pPager, SHARED_LOCK);
! 43057: if( rc!=SQLITE_OK ){
! 43058: assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
! 43059: goto failed;
! 43060: }
! 43061: }
! 43062:
! 43063: /* If a journal file exists, and there is no RESERVED lock on the
! 43064: ** database file, then it either needs to be played back or deleted.
! 43065: */
! 43066: if( pPager->eLock<=SHARED_LOCK ){
! 43067: rc = hasHotJournal(pPager, &bHotJournal);
! 43068: }
! 43069: if( rc!=SQLITE_OK ){
! 43070: goto failed;
! 43071: }
! 43072: if( bHotJournal ){
! 43073: /* Get an EXCLUSIVE lock on the database file. At this point it is
! 43074: ** important that a RESERVED lock is not obtained on the way to the
! 43075: ** EXCLUSIVE lock. If it were, another process might open the
! 43076: ** database file, detect the RESERVED lock, and conclude that the
! 43077: ** database is safe to read while this process is still rolling the
! 43078: ** hot-journal back.
! 43079: **
! 43080: ** Because the intermediate RESERVED lock is not requested, any
! 43081: ** other process attempting to access the database file will get to
! 43082: ** this point in the code and fail to obtain its own EXCLUSIVE lock
! 43083: ** on the database file.
! 43084: **
! 43085: ** Unless the pager is in locking_mode=exclusive mode, the lock is
! 43086: ** downgraded to SHARED_LOCK before this function returns.
! 43087: */
! 43088: rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
! 43089: if( rc!=SQLITE_OK ){
! 43090: goto failed;
! 43091: }
! 43092:
! 43093: /* If it is not already open and the file exists on disk, open the
! 43094: ** journal for read/write access. Write access is required because
! 43095: ** in exclusive-access mode the file descriptor will be kept open
! 43096: ** and possibly used for a transaction later on. Also, write-access
! 43097: ** is usually required to finalize the journal in journal_mode=persist
! 43098: ** mode (and also for journal_mode=truncate on some systems).
! 43099: **
! 43100: ** If the journal does not exist, it usually means that some
! 43101: ** other connection managed to get in and roll it back before
! 43102: ** this connection obtained the exclusive lock above. Or, it
! 43103: ** may mean that the pager was in the error-state when this
! 43104: ** function was called and the journal file does not exist.
! 43105: */
! 43106: if( !isOpen(pPager->jfd) ){
! 43107: sqlite3_vfs * const pVfs = pPager->pVfs;
! 43108: int bExists; /* True if journal file exists */
! 43109: rc = sqlite3OsAccess(
! 43110: pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
! 43111: if( rc==SQLITE_OK && bExists ){
! 43112: int fout = 0;
! 43113: int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
! 43114: assert( !pPager->tempFile );
! 43115: rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
! 43116: assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
! 43117: if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
! 43118: rc = SQLITE_CANTOPEN_BKPT;
! 43119: sqlite3OsClose(pPager->jfd);
! 43120: }
! 43121: }
! 43122: }
! 43123:
! 43124: /* Playback and delete the journal. Drop the database write
! 43125: ** lock and reacquire the read lock. Purge the cache before
! 43126: ** playing back the hot-journal so that we don't end up with
! 43127: ** an inconsistent cache. Sync the hot journal before playing
! 43128: ** it back since the process that crashed and left the hot journal
! 43129: ** probably did not sync it and we are required to always sync
! 43130: ** the journal before playing it back.
! 43131: */
! 43132: if( isOpen(pPager->jfd) ){
! 43133: assert( rc==SQLITE_OK );
! 43134: rc = pagerSyncHotJournal(pPager);
! 43135: if( rc==SQLITE_OK ){
! 43136: rc = pager_playback(pPager, 1);
! 43137: pPager->eState = PAGER_OPEN;
! 43138: }
! 43139: }else if( !pPager->exclusiveMode ){
! 43140: pagerUnlockDb(pPager, SHARED_LOCK);
! 43141: }
! 43142:
! 43143: if( rc!=SQLITE_OK ){
! 43144: /* This branch is taken if an error occurs while trying to open
! 43145: ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
! 43146: ** pager_unlock() routine will be called before returning to unlock
! 43147: ** the file. If the unlock attempt fails, then Pager.eLock must be
! 43148: ** set to UNKNOWN_LOCK (see the comment above the #define for
! 43149: ** UNKNOWN_LOCK above for an explanation).
! 43150: **
! 43151: ** In order to get pager_unlock() to do this, set Pager.eState to
! 43152: ** PAGER_ERROR now. This is not actually counted as a transition
! 43153: ** to ERROR state in the state diagram at the top of this file,
! 43154: ** since we know that the same call to pager_unlock() will very
! 43155: ** shortly transition the pager object to the OPEN state. Calling
! 43156: ** assert_pager_state() would fail now, as it should not be possible
! 43157: ** to be in ERROR state when there are zero outstanding page
! 43158: ** references.
! 43159: */
! 43160: pager_error(pPager, rc);
! 43161: goto failed;
! 43162: }
! 43163:
! 43164: assert( pPager->eState==PAGER_OPEN );
! 43165: assert( (pPager->eLock==SHARED_LOCK)
! 43166: || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
! 43167: );
! 43168: }
! 43169:
! 43170: if( !pPager->tempFile
! 43171: && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0)
! 43172: ){
! 43173: /* The shared-lock has just been acquired on the database file
! 43174: ** and there are already pages in the cache (from a previous
! 43175: ** read or write transaction). Check to see if the database
! 43176: ** has been modified. If the database has changed, flush the
! 43177: ** cache.
! 43178: **
! 43179: ** Database changes is detected by looking at 15 bytes beginning
! 43180: ** at offset 24 into the file. The first 4 of these 16 bytes are
! 43181: ** a 32-bit counter that is incremented with each change. The
! 43182: ** other bytes change randomly with each file change when
! 43183: ** a codec is in use.
! 43184: **
! 43185: ** There is a vanishingly small chance that a change will not be
! 43186: ** detected. The chance of an undetected change is so small that
! 43187: ** it can be neglected.
! 43188: */
! 43189: Pgno nPage = 0;
! 43190: char dbFileVers[sizeof(pPager->dbFileVers)];
! 43191:
! 43192: rc = pagerPagecount(pPager, &nPage);
! 43193: if( rc ) goto failed;
! 43194:
! 43195: if( nPage>0 ){
! 43196: IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
! 43197: rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
! 43198: if( rc!=SQLITE_OK ){
! 43199: goto failed;
! 43200: }
! 43201: }else{
! 43202: memset(dbFileVers, 0, sizeof(dbFileVers));
! 43203: }
! 43204:
! 43205: if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
! 43206: pager_reset(pPager);
! 43207: }
! 43208: }
! 43209:
! 43210: /* If there is a WAL file in the file-system, open this database in WAL
! 43211: ** mode. Otherwise, the following function call is a no-op.
! 43212: */
! 43213: rc = pagerOpenWalIfPresent(pPager);
! 43214: #ifndef SQLITE_OMIT_WAL
! 43215: assert( pPager->pWal==0 || rc==SQLITE_OK );
! 43216: #endif
! 43217: }
! 43218:
! 43219: if( pagerUseWal(pPager) ){
! 43220: assert( rc==SQLITE_OK );
! 43221: rc = pagerBeginReadTransaction(pPager);
! 43222: }
! 43223:
! 43224: if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
! 43225: rc = pagerPagecount(pPager, &pPager->dbSize);
! 43226: }
! 43227:
! 43228: failed:
! 43229: if( rc!=SQLITE_OK ){
! 43230: assert( !MEMDB );
! 43231: pager_unlock(pPager);
! 43232: assert( pPager->eState==PAGER_OPEN );
! 43233: }else{
! 43234: pPager->eState = PAGER_READER;
! 43235: }
! 43236: return rc;
! 43237: }
! 43238:
! 43239: /*
! 43240: ** If the reference count has reached zero, rollback any active
! 43241: ** transaction and unlock the pager.
! 43242: **
! 43243: ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
! 43244: ** the rollback journal, the unlock is not performed and there is
! 43245: ** nothing to rollback, so this routine is a no-op.
! 43246: */
! 43247: static void pagerUnlockIfUnused(Pager *pPager){
! 43248: if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
! 43249: pagerUnlockAndRollback(pPager);
! 43250: }
! 43251: }
! 43252:
! 43253: /*
! 43254: ** Acquire a reference to page number pgno in pager pPager (a page
! 43255: ** reference has type DbPage*). If the requested reference is
! 43256: ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
! 43257: **
! 43258: ** If the requested page is already in the cache, it is returned.
! 43259: ** Otherwise, a new page object is allocated and populated with data
! 43260: ** read from the database file. In some cases, the pcache module may
! 43261: ** choose not to allocate a new page object and may reuse an existing
! 43262: ** object with no outstanding references.
! 43263: **
! 43264: ** The extra data appended to a page is always initialized to zeros the
! 43265: ** first time a page is loaded into memory. If the page requested is
! 43266: ** already in the cache when this function is called, then the extra
! 43267: ** data is left as it was when the page object was last used.
! 43268: **
! 43269: ** If the database image is smaller than the requested page or if a
! 43270: ** non-zero value is passed as the noContent parameter and the
! 43271: ** requested page is not already stored in the cache, then no
! 43272: ** actual disk read occurs. In this case the memory image of the
! 43273: ** page is initialized to all zeros.
! 43274: **
! 43275: ** If noContent is true, it means that we do not care about the contents
! 43276: ** of the page. This occurs in two seperate scenarios:
! 43277: **
! 43278: ** a) When reading a free-list leaf page from the database, and
! 43279: **
! 43280: ** b) When a savepoint is being rolled back and we need to load
! 43281: ** a new page into the cache to be filled with the data read
! 43282: ** from the savepoint journal.
! 43283: **
! 43284: ** If noContent is true, then the data returned is zeroed instead of
! 43285: ** being read from the database. Additionally, the bits corresponding
! 43286: ** to pgno in Pager.pInJournal (bitvec of pages already written to the
! 43287: ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
! 43288: ** savepoints are set. This means if the page is made writable at any
! 43289: ** point in the future, using a call to sqlite3PagerWrite(), its contents
! 43290: ** will not be journaled. This saves IO.
! 43291: **
! 43292: ** The acquisition might fail for several reasons. In all cases,
! 43293: ** an appropriate error code is returned and *ppPage is set to NULL.
! 43294: **
! 43295: ** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
! 43296: ** to find a page in the in-memory cache first. If the page is not already
! 43297: ** in memory, this routine goes to disk to read it in whereas Lookup()
! 43298: ** just returns 0. This routine acquires a read-lock the first time it
! 43299: ** has to go to disk, and could also playback an old journal if necessary.
! 43300: ** Since Lookup() never goes to disk, it never has to deal with locks
! 43301: ** or journal files.
! 43302: */
! 43303: SQLITE_PRIVATE int sqlite3PagerAcquire(
! 43304: Pager *pPager, /* The pager open on the database file */
! 43305: Pgno pgno, /* Page number to fetch */
! 43306: DbPage **ppPage, /* Write a pointer to the page here */
! 43307: int noContent /* Do not bother reading content from disk if true */
! 43308: ){
! 43309: int rc;
! 43310: PgHdr *pPg;
! 43311:
! 43312: assert( pPager->eState>=PAGER_READER );
! 43313: assert( assert_pager_state(pPager) );
! 43314:
! 43315: if( pgno==0 ){
! 43316: return SQLITE_CORRUPT_BKPT;
! 43317: }
! 43318:
! 43319: /* If the pager is in the error state, return an error immediately.
! 43320: ** Otherwise, request the page from the PCache layer. */
! 43321: if( pPager->errCode!=SQLITE_OK ){
! 43322: rc = pPager->errCode;
! 43323: }else{
! 43324: rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
! 43325: }
! 43326:
! 43327: if( rc!=SQLITE_OK ){
! 43328: /* Either the call to sqlite3PcacheFetch() returned an error or the
! 43329: ** pager was already in the error-state when this function was called.
! 43330: ** Set pPg to 0 and jump to the exception handler. */
! 43331: pPg = 0;
! 43332: goto pager_acquire_err;
! 43333: }
! 43334: assert( (*ppPage)->pgno==pgno );
! 43335: assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
! 43336:
! 43337: if( (*ppPage)->pPager && !noContent ){
! 43338: /* In this case the pcache already contains an initialized copy of
! 43339: ** the page. Return without further ado. */
! 43340: assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
! 43341: pPager->nHit++;
! 43342: return SQLITE_OK;
! 43343:
! 43344: }else{
! 43345: /* The pager cache has created a new page. Its content needs to
! 43346: ** be initialized. */
! 43347:
! 43348: pPg = *ppPage;
! 43349: pPg->pPager = pPager;
! 43350:
! 43351: /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
! 43352: ** number greater than this, or the unused locking-page, is requested. */
! 43353: if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
! 43354: rc = SQLITE_CORRUPT_BKPT;
! 43355: goto pager_acquire_err;
! 43356: }
! 43357:
! 43358: if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
! 43359: if( pgno>pPager->mxPgno ){
! 43360: rc = SQLITE_FULL;
! 43361: goto pager_acquire_err;
! 43362: }
! 43363: if( noContent ){
! 43364: /* Failure to set the bits in the InJournal bit-vectors is benign.
! 43365: ** It merely means that we might do some extra work to journal a
! 43366: ** page that does not need to be journaled. Nevertheless, be sure
! 43367: ** to test the case where a malloc error occurs while trying to set
! 43368: ** a bit in a bit vector.
! 43369: */
! 43370: sqlite3BeginBenignMalloc();
! 43371: if( pgno<=pPager->dbOrigSize ){
! 43372: TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
! 43373: testcase( rc==SQLITE_NOMEM );
! 43374: }
! 43375: TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
! 43376: testcase( rc==SQLITE_NOMEM );
! 43377: sqlite3EndBenignMalloc();
! 43378: }
! 43379: memset(pPg->pData, 0, pPager->pageSize);
! 43380: IOTRACE(("ZERO %p %d\n", pPager, pgno));
! 43381: }else{
! 43382: assert( pPg->pPager==pPager );
! 43383: pPager->nMiss++;
! 43384: rc = readDbPage(pPg);
! 43385: if( rc!=SQLITE_OK ){
! 43386: goto pager_acquire_err;
! 43387: }
! 43388: }
! 43389: pager_set_pagehash(pPg);
! 43390: }
! 43391:
! 43392: return SQLITE_OK;
! 43393:
! 43394: pager_acquire_err:
! 43395: assert( rc!=SQLITE_OK );
! 43396: if( pPg ){
! 43397: sqlite3PcacheDrop(pPg);
! 43398: }
! 43399: pagerUnlockIfUnused(pPager);
! 43400:
! 43401: *ppPage = 0;
! 43402: return rc;
! 43403: }
! 43404:
! 43405: /*
! 43406: ** Acquire a page if it is already in the in-memory cache. Do
! 43407: ** not read the page from disk. Return a pointer to the page,
! 43408: ** or 0 if the page is not in cache.
! 43409: **
! 43410: ** See also sqlite3PagerGet(). The difference between this routine
! 43411: ** and sqlite3PagerGet() is that _get() will go to the disk and read
! 43412: ** in the page if the page is not already in cache. This routine
! 43413: ** returns NULL if the page is not in cache or if a disk I/O error
! 43414: ** has ever happened.
! 43415: */
! 43416: SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
! 43417: PgHdr *pPg = 0;
! 43418: assert( pPager!=0 );
! 43419: assert( pgno!=0 );
! 43420: assert( pPager->pPCache!=0 );
! 43421: assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
! 43422: sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
! 43423: return pPg;
! 43424: }
! 43425:
! 43426: /*
! 43427: ** Release a page reference.
! 43428: **
! 43429: ** If the number of references to the page drop to zero, then the
! 43430: ** page is added to the LRU list. When all references to all pages
! 43431: ** are released, a rollback occurs and the lock on the database is
! 43432: ** removed.
! 43433: */
! 43434: SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
! 43435: if( pPg ){
! 43436: Pager *pPager = pPg->pPager;
! 43437: sqlite3PcacheRelease(pPg);
! 43438: pagerUnlockIfUnused(pPager);
! 43439: }
! 43440: }
! 43441:
! 43442: /*
! 43443: ** This function is called at the start of every write transaction.
! 43444: ** There must already be a RESERVED or EXCLUSIVE lock on the database
! 43445: ** file when this routine is called.
! 43446: **
! 43447: ** Open the journal file for pager pPager and write a journal header
! 43448: ** to the start of it. If there are active savepoints, open the sub-journal
! 43449: ** as well. This function is only used when the journal file is being
! 43450: ** opened to write a rollback log for a transaction. It is not used
! 43451: ** when opening a hot journal file to roll it back.
! 43452: **
! 43453: ** If the journal file is already open (as it may be in exclusive mode),
! 43454: ** then this function just writes a journal header to the start of the
! 43455: ** already open file.
! 43456: **
! 43457: ** Whether or not the journal file is opened by this function, the
! 43458: ** Pager.pInJournal bitvec structure is allocated.
! 43459: **
! 43460: ** Return SQLITE_OK if everything is successful. Otherwise, return
! 43461: ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
! 43462: ** an IO error code if opening or writing the journal file fails.
! 43463: */
! 43464: static int pager_open_journal(Pager *pPager){
! 43465: int rc = SQLITE_OK; /* Return code */
! 43466: sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
! 43467:
! 43468: assert( pPager->eState==PAGER_WRITER_LOCKED );
! 43469: assert( assert_pager_state(pPager) );
! 43470: assert( pPager->pInJournal==0 );
! 43471:
! 43472: /* If already in the error state, this function is a no-op. But on
! 43473: ** the other hand, this routine is never called if we are already in
! 43474: ** an error state. */
! 43475: if( NEVER(pPager->errCode) ) return pPager->errCode;
! 43476:
! 43477: if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
! 43478: pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
! 43479: if( pPager->pInJournal==0 ){
! 43480: return SQLITE_NOMEM;
! 43481: }
! 43482:
! 43483: /* Open the journal file if it is not already open. */
! 43484: if( !isOpen(pPager->jfd) ){
! 43485: if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
! 43486: sqlite3MemJournalOpen(pPager->jfd);
! 43487: }else{
! 43488: const int flags = /* VFS flags to open journal file */
! 43489: SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
! 43490: (pPager->tempFile ?
! 43491: (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
! 43492: (SQLITE_OPEN_MAIN_JOURNAL)
! 43493: );
! 43494: #ifdef SQLITE_ENABLE_ATOMIC_WRITE
! 43495: rc = sqlite3JournalOpen(
! 43496: pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
! 43497: );
! 43498: #else
! 43499: rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
! 43500: #endif
! 43501: }
! 43502: assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
! 43503: }
! 43504:
! 43505:
! 43506: /* Write the first journal header to the journal file and open
! 43507: ** the sub-journal if necessary.
! 43508: */
! 43509: if( rc==SQLITE_OK ){
! 43510: /* TODO: Check if all of these are really required. */
! 43511: pPager->nRec = 0;
! 43512: pPager->journalOff = 0;
! 43513: pPager->setMaster = 0;
! 43514: pPager->journalHdr = 0;
! 43515: rc = writeJournalHdr(pPager);
! 43516: }
! 43517: }
! 43518:
! 43519: if( rc!=SQLITE_OK ){
! 43520: sqlite3BitvecDestroy(pPager->pInJournal);
! 43521: pPager->pInJournal = 0;
! 43522: }else{
! 43523: assert( pPager->eState==PAGER_WRITER_LOCKED );
! 43524: pPager->eState = PAGER_WRITER_CACHEMOD;
! 43525: }
! 43526:
! 43527: return rc;
! 43528: }
! 43529:
! 43530: /*
! 43531: ** Begin a write-transaction on the specified pager object. If a
! 43532: ** write-transaction has already been opened, this function is a no-op.
! 43533: **
! 43534: ** If the exFlag argument is false, then acquire at least a RESERVED
! 43535: ** lock on the database file. If exFlag is true, then acquire at least
! 43536: ** an EXCLUSIVE lock. If such a lock is already held, no locking
! 43537: ** functions need be called.
! 43538: **
! 43539: ** If the subjInMemory argument is non-zero, then any sub-journal opened
! 43540: ** within this transaction will be opened as an in-memory file. This
! 43541: ** has no effect if the sub-journal is already opened (as it may be when
! 43542: ** running in exclusive mode) or if the transaction does not require a
! 43543: ** sub-journal. If the subjInMemory argument is zero, then any required
! 43544: ** sub-journal is implemented in-memory if pPager is an in-memory database,
! 43545: ** or using a temporary file otherwise.
! 43546: */
! 43547: SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
! 43548: int rc = SQLITE_OK;
! 43549:
! 43550: if( pPager->errCode ) return pPager->errCode;
! 43551: assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
! 43552: pPager->subjInMemory = (u8)subjInMemory;
! 43553:
! 43554: if( ALWAYS(pPager->eState==PAGER_READER) ){
! 43555: assert( pPager->pInJournal==0 );
! 43556:
! 43557: if( pagerUseWal(pPager) ){
! 43558: /* If the pager is configured to use locking_mode=exclusive, and an
! 43559: ** exclusive lock on the database is not already held, obtain it now.
! 43560: */
! 43561: if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
! 43562: rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
! 43563: if( rc!=SQLITE_OK ){
! 43564: return rc;
! 43565: }
! 43566: sqlite3WalExclusiveMode(pPager->pWal, 1);
! 43567: }
! 43568:
! 43569: /* Grab the write lock on the log file. If successful, upgrade to
! 43570: ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
! 43571: ** The busy-handler is not invoked if another connection already
! 43572: ** holds the write-lock. If possible, the upper layer will call it.
! 43573: */
! 43574: rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
! 43575: }else{
! 43576: /* Obtain a RESERVED lock on the database file. If the exFlag parameter
! 43577: ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
! 43578: ** busy-handler callback can be used when upgrading to the EXCLUSIVE
! 43579: ** lock, but not when obtaining the RESERVED lock.
! 43580: */
! 43581: rc = pagerLockDb(pPager, RESERVED_LOCK);
! 43582: if( rc==SQLITE_OK && exFlag ){
! 43583: rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
! 43584: }
! 43585: }
! 43586:
! 43587: if( rc==SQLITE_OK ){
! 43588: /* Change to WRITER_LOCKED state.
! 43589: **
! 43590: ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
! 43591: ** when it has an open transaction, but never to DBMOD or FINISHED.
! 43592: ** This is because in those states the code to roll back savepoint
! 43593: ** transactions may copy data from the sub-journal into the database
! 43594: ** file as well as into the page cache. Which would be incorrect in
! 43595: ** WAL mode.
! 43596: */
! 43597: pPager->eState = PAGER_WRITER_LOCKED;
! 43598: pPager->dbHintSize = pPager->dbSize;
! 43599: pPager->dbFileSize = pPager->dbSize;
! 43600: pPager->dbOrigSize = pPager->dbSize;
! 43601: pPager->journalOff = 0;
! 43602: }
! 43603:
! 43604: assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
! 43605: assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
! 43606: assert( assert_pager_state(pPager) );
! 43607: }
! 43608:
! 43609: PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
! 43610: return rc;
! 43611: }
! 43612:
! 43613: /*
! 43614: ** Mark a single data page as writeable. The page is written into the
! 43615: ** main journal or sub-journal as required. If the page is written into
! 43616: ** one of the journals, the corresponding bit is set in the
! 43617: ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
! 43618: ** of any open savepoints as appropriate.
! 43619: */
! 43620: static int pager_write(PgHdr *pPg){
! 43621: void *pData = pPg->pData;
! 43622: Pager *pPager = pPg->pPager;
! 43623: int rc = SQLITE_OK;
! 43624:
! 43625: /* This routine is not called unless a write-transaction has already
! 43626: ** been started. The journal file may or may not be open at this point.
! 43627: ** It is never called in the ERROR state.
! 43628: */
! 43629: assert( pPager->eState==PAGER_WRITER_LOCKED
! 43630: || pPager->eState==PAGER_WRITER_CACHEMOD
! 43631: || pPager->eState==PAGER_WRITER_DBMOD
! 43632: );
! 43633: assert( assert_pager_state(pPager) );
! 43634:
! 43635: /* If an error has been previously detected, report the same error
! 43636: ** again. This should not happen, but the check provides robustness. */
! 43637: if( NEVER(pPager->errCode) ) return pPager->errCode;
! 43638:
! 43639: /* Higher-level routines never call this function if database is not
! 43640: ** writable. But check anyway, just for robustness. */
! 43641: if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
! 43642:
! 43643: CHECK_PAGE(pPg);
! 43644:
! 43645: /* The journal file needs to be opened. Higher level routines have already
! 43646: ** obtained the necessary locks to begin the write-transaction, but the
! 43647: ** rollback journal might not yet be open. Open it now if this is the case.
! 43648: **
! 43649: ** This is done before calling sqlite3PcacheMakeDirty() on the page.
! 43650: ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
! 43651: ** an error might occur and the pager would end up in WRITER_LOCKED state
! 43652: ** with pages marked as dirty in the cache.
! 43653: */
! 43654: if( pPager->eState==PAGER_WRITER_LOCKED ){
! 43655: rc = pager_open_journal(pPager);
! 43656: if( rc!=SQLITE_OK ) return rc;
! 43657: }
! 43658: assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
! 43659: assert( assert_pager_state(pPager) );
! 43660:
! 43661: /* Mark the page as dirty. If the page has already been written
! 43662: ** to the journal then we can return right away.
! 43663: */
! 43664: sqlite3PcacheMakeDirty(pPg);
! 43665: if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
! 43666: assert( !pagerUseWal(pPager) );
! 43667: }else{
! 43668:
! 43669: /* The transaction journal now exists and we have a RESERVED or an
! 43670: ** EXCLUSIVE lock on the main database file. Write the current page to
! 43671: ** the transaction journal if it is not there already.
! 43672: */
! 43673: if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
! 43674: assert( pagerUseWal(pPager)==0 );
! 43675: if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
! 43676: u32 cksum;
! 43677: char *pData2;
! 43678: i64 iOff = pPager->journalOff;
! 43679:
! 43680: /* We should never write to the journal file the page that
! 43681: ** contains the database locks. The following assert verifies
! 43682: ** that we do not. */
! 43683: assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
! 43684:
! 43685: assert( pPager->journalHdr<=pPager->journalOff );
! 43686: CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
! 43687: cksum = pager_cksum(pPager, (u8*)pData2);
! 43688:
! 43689: /* Even if an IO or diskfull error occurs while journalling the
! 43690: ** page in the block above, set the need-sync flag for the page.
! 43691: ** Otherwise, when the transaction is rolled back, the logic in
! 43692: ** playback_one_page() will think that the page needs to be restored
! 43693: ** in the database file. And if an IO error occurs while doing so,
! 43694: ** then corruption may follow.
! 43695: */
! 43696: pPg->flags |= PGHDR_NEED_SYNC;
! 43697:
! 43698: rc = write32bits(pPager->jfd, iOff, pPg->pgno);
! 43699: if( rc!=SQLITE_OK ) return rc;
! 43700: rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
! 43701: if( rc!=SQLITE_OK ) return rc;
! 43702: rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
! 43703: if( rc!=SQLITE_OK ) return rc;
! 43704:
! 43705: IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
! 43706: pPager->journalOff, pPager->pageSize));
! 43707: PAGER_INCR(sqlite3_pager_writej_count);
! 43708: PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
! 43709: PAGERID(pPager), pPg->pgno,
! 43710: ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
! 43711:
! 43712: pPager->journalOff += 8 + pPager->pageSize;
! 43713: pPager->nRec++;
! 43714: assert( pPager->pInJournal!=0 );
! 43715: rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
! 43716: testcase( rc==SQLITE_NOMEM );
! 43717: assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
! 43718: rc |= addToSavepointBitvecs(pPager, pPg->pgno);
! 43719: if( rc!=SQLITE_OK ){
! 43720: assert( rc==SQLITE_NOMEM );
! 43721: return rc;
! 43722: }
! 43723: }else{
! 43724: if( pPager->eState!=PAGER_WRITER_DBMOD ){
! 43725: pPg->flags |= PGHDR_NEED_SYNC;
! 43726: }
! 43727: PAGERTRACE(("APPEND %d page %d needSync=%d\n",
! 43728: PAGERID(pPager), pPg->pgno,
! 43729: ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
! 43730: }
! 43731: }
! 43732:
! 43733: /* If the statement journal is open and the page is not in it,
! 43734: ** then write the current page to the statement journal. Note that
! 43735: ** the statement journal format differs from the standard journal format
! 43736: ** in that it omits the checksums and the header.
! 43737: */
! 43738: if( subjRequiresPage(pPg) ){
! 43739: rc = subjournalPage(pPg);
! 43740: }
! 43741: }
! 43742:
! 43743: /* Update the database size and return.
! 43744: */
! 43745: if( pPager->dbSize<pPg->pgno ){
! 43746: pPager->dbSize = pPg->pgno;
! 43747: }
! 43748: return rc;
! 43749: }
! 43750:
! 43751: /*
! 43752: ** Mark a data page as writeable. This routine must be called before
! 43753: ** making changes to a page. The caller must check the return value
! 43754: ** of this function and be careful not to change any page data unless
! 43755: ** this routine returns SQLITE_OK.
! 43756: **
! 43757: ** The difference between this function and pager_write() is that this
! 43758: ** function also deals with the special case where 2 or more pages
! 43759: ** fit on a single disk sector. In this case all co-resident pages
! 43760: ** must have been written to the journal file before returning.
! 43761: **
! 43762: ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
! 43763: ** as appropriate. Otherwise, SQLITE_OK.
! 43764: */
! 43765: SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){
! 43766: int rc = SQLITE_OK;
! 43767:
! 43768: PgHdr *pPg = pDbPage;
! 43769: Pager *pPager = pPg->pPager;
! 43770: Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
! 43771:
! 43772: assert( pPager->eState>=PAGER_WRITER_LOCKED );
! 43773: assert( pPager->eState!=PAGER_ERROR );
! 43774: assert( assert_pager_state(pPager) );
! 43775:
! 43776: if( nPagePerSector>1 ){
! 43777: Pgno nPageCount; /* Total number of pages in database file */
! 43778: Pgno pg1; /* First page of the sector pPg is located on. */
! 43779: int nPage = 0; /* Number of pages starting at pg1 to journal */
! 43780: int ii; /* Loop counter */
! 43781: int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
! 43782:
! 43783: /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
! 43784: ** a journal header to be written between the pages journaled by
! 43785: ** this function.
! 43786: */
! 43787: assert( !MEMDB );
! 43788: assert( pPager->doNotSyncSpill==0 );
! 43789: pPager->doNotSyncSpill++;
! 43790:
! 43791: /* This trick assumes that both the page-size and sector-size are
! 43792: ** an integer power of 2. It sets variable pg1 to the identifier
! 43793: ** of the first page of the sector pPg is located on.
! 43794: */
! 43795: pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
! 43796:
! 43797: nPageCount = pPager->dbSize;
! 43798: if( pPg->pgno>nPageCount ){
! 43799: nPage = (pPg->pgno - pg1)+1;
! 43800: }else if( (pg1+nPagePerSector-1)>nPageCount ){
! 43801: nPage = nPageCount+1-pg1;
! 43802: }else{
! 43803: nPage = nPagePerSector;
! 43804: }
! 43805: assert(nPage>0);
! 43806: assert(pg1<=pPg->pgno);
! 43807: assert((pg1+nPage)>pPg->pgno);
! 43808:
! 43809: for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
! 43810: Pgno pg = pg1+ii;
! 43811: PgHdr *pPage;
! 43812: if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
! 43813: if( pg!=PAGER_MJ_PGNO(pPager) ){
! 43814: rc = sqlite3PagerGet(pPager, pg, &pPage);
! 43815: if( rc==SQLITE_OK ){
! 43816: rc = pager_write(pPage);
! 43817: if( pPage->flags&PGHDR_NEED_SYNC ){
! 43818: needSync = 1;
! 43819: }
! 43820: sqlite3PagerUnref(pPage);
! 43821: }
! 43822: }
! 43823: }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
! 43824: if( pPage->flags&PGHDR_NEED_SYNC ){
! 43825: needSync = 1;
! 43826: }
! 43827: sqlite3PagerUnref(pPage);
! 43828: }
! 43829: }
! 43830:
! 43831: /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
! 43832: ** starting at pg1, then it needs to be set for all of them. Because
! 43833: ** writing to any of these nPage pages may damage the others, the
! 43834: ** journal file must contain sync()ed copies of all of them
! 43835: ** before any of them can be written out to the database file.
! 43836: */
! 43837: if( rc==SQLITE_OK && needSync ){
! 43838: assert( !MEMDB );
! 43839: for(ii=0; ii<nPage; ii++){
! 43840: PgHdr *pPage = pager_lookup(pPager, pg1+ii);
! 43841: if( pPage ){
! 43842: pPage->flags |= PGHDR_NEED_SYNC;
! 43843: sqlite3PagerUnref(pPage);
! 43844: }
! 43845: }
! 43846: }
! 43847:
! 43848: assert( pPager->doNotSyncSpill==1 );
! 43849: pPager->doNotSyncSpill--;
! 43850: }else{
! 43851: rc = pager_write(pDbPage);
! 43852: }
! 43853: return rc;
! 43854: }
! 43855:
! 43856: /*
! 43857: ** Return TRUE if the page given in the argument was previously passed
! 43858: ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
! 43859: ** to change the content of the page.
! 43860: */
! 43861: #ifndef NDEBUG
! 43862: SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
! 43863: return pPg->flags&PGHDR_DIRTY;
! 43864: }
! 43865: #endif
! 43866:
! 43867: /*
! 43868: ** A call to this routine tells the pager that it is not necessary to
! 43869: ** write the information on page pPg back to the disk, even though
! 43870: ** that page might be marked as dirty. This happens, for example, when
! 43871: ** the page has been added as a leaf of the freelist and so its
! 43872: ** content no longer matters.
! 43873: **
! 43874: ** The overlying software layer calls this routine when all of the data
! 43875: ** on the given page is unused. The pager marks the page as clean so
! 43876: ** that it does not get written to disk.
! 43877: **
! 43878: ** Tests show that this optimization can quadruple the speed of large
! 43879: ** DELETE operations.
! 43880: */
! 43881: SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
! 43882: Pager *pPager = pPg->pPager;
! 43883: if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
! 43884: PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
! 43885: IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
! 43886: pPg->flags |= PGHDR_DONT_WRITE;
! 43887: pager_set_pagehash(pPg);
! 43888: }
! 43889: }
! 43890:
! 43891: /*
! 43892: ** This routine is called to increment the value of the database file
! 43893: ** change-counter, stored as a 4-byte big-endian integer starting at
! 43894: ** byte offset 24 of the pager file. The secondary change counter at
! 43895: ** 92 is also updated, as is the SQLite version number at offset 96.
! 43896: **
! 43897: ** But this only happens if the pPager->changeCountDone flag is false.
! 43898: ** To avoid excess churning of page 1, the update only happens once.
! 43899: ** See also the pager_write_changecounter() routine that does an
! 43900: ** unconditional update of the change counters.
! 43901: **
! 43902: ** If the isDirectMode flag is zero, then this is done by calling
! 43903: ** sqlite3PagerWrite() on page 1, then modifying the contents of the
! 43904: ** page data. In this case the file will be updated when the current
! 43905: ** transaction is committed.
! 43906: **
! 43907: ** The isDirectMode flag may only be non-zero if the library was compiled
! 43908: ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
! 43909: ** if isDirect is non-zero, then the database file is updated directly
! 43910: ** by writing an updated version of page 1 using a call to the
! 43911: ** sqlite3OsWrite() function.
! 43912: */
! 43913: static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
! 43914: int rc = SQLITE_OK;
! 43915:
! 43916: assert( pPager->eState==PAGER_WRITER_CACHEMOD
! 43917: || pPager->eState==PAGER_WRITER_DBMOD
! 43918: );
! 43919: assert( assert_pager_state(pPager) );
! 43920:
! 43921: /* Declare and initialize constant integer 'isDirect'. If the
! 43922: ** atomic-write optimization is enabled in this build, then isDirect
! 43923: ** is initialized to the value passed as the isDirectMode parameter
! 43924: ** to this function. Otherwise, it is always set to zero.
! 43925: **
! 43926: ** The idea is that if the atomic-write optimization is not
! 43927: ** enabled at compile time, the compiler can omit the tests of
! 43928: ** 'isDirect' below, as well as the block enclosed in the
! 43929: ** "if( isDirect )" condition.
! 43930: */
! 43931: #ifndef SQLITE_ENABLE_ATOMIC_WRITE
! 43932: # define DIRECT_MODE 0
! 43933: assert( isDirectMode==0 );
! 43934: UNUSED_PARAMETER(isDirectMode);
! 43935: #else
! 43936: # define DIRECT_MODE isDirectMode
! 43937: #endif
! 43938:
! 43939: if( !pPager->changeCountDone && pPager->dbSize>0 ){
! 43940: PgHdr *pPgHdr; /* Reference to page 1 */
! 43941:
! 43942: assert( !pPager->tempFile && isOpen(pPager->fd) );
! 43943:
! 43944: /* Open page 1 of the file for writing. */
! 43945: rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
! 43946: assert( pPgHdr==0 || rc==SQLITE_OK );
! 43947:
! 43948: /* If page one was fetched successfully, and this function is not
! 43949: ** operating in direct-mode, make page 1 writable. When not in
! 43950: ** direct mode, page 1 is always held in cache and hence the PagerGet()
! 43951: ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
! 43952: */
! 43953: if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
! 43954: rc = sqlite3PagerWrite(pPgHdr);
! 43955: }
! 43956:
! 43957: if( rc==SQLITE_OK ){
! 43958: /* Actually do the update of the change counter */
! 43959: pager_write_changecounter(pPgHdr);
! 43960:
! 43961: /* If running in direct mode, write the contents of page 1 to the file. */
! 43962: if( DIRECT_MODE ){
! 43963: const void *zBuf;
! 43964: assert( pPager->dbFileSize>0 );
! 43965: CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
! 43966: if( rc==SQLITE_OK ){
! 43967: rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
! 43968: }
! 43969: if( rc==SQLITE_OK ){
! 43970: pPager->changeCountDone = 1;
! 43971: }
! 43972: }else{
! 43973: pPager->changeCountDone = 1;
! 43974: }
! 43975: }
! 43976:
! 43977: /* Release the page reference. */
! 43978: sqlite3PagerUnref(pPgHdr);
! 43979: }
! 43980: return rc;
! 43981: }
! 43982:
! 43983: /*
! 43984: ** Sync the database file to disk. This is a no-op for in-memory databases
! 43985: ** or pages with the Pager.noSync flag set.
! 43986: **
! 43987: ** If successful, or if called on a pager for which it is a no-op, this
! 43988: ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
! 43989: */
! 43990: SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
! 43991: int rc = SQLITE_OK;
! 43992: if( !pPager->noSync ){
! 43993: assert( !MEMDB );
! 43994: rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
! 43995: }else if( isOpen(pPager->fd) ){
! 43996: assert( !MEMDB );
! 43997: rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, 0);
! 43998: if( rc==SQLITE_NOTFOUND ){
! 43999: rc = SQLITE_OK;
! 44000: }
! 44001: }
! 44002: return rc;
! 44003: }
! 44004:
! 44005: /*
! 44006: ** This function may only be called while a write-transaction is active in
! 44007: ** rollback. If the connection is in WAL mode, this call is a no-op.
! 44008: ** Otherwise, if the connection does not already have an EXCLUSIVE lock on
! 44009: ** the database file, an attempt is made to obtain one.
! 44010: **
! 44011: ** If the EXCLUSIVE lock is already held or the attempt to obtain it is
! 44012: ** successful, or the connection is in WAL mode, SQLITE_OK is returned.
! 44013: ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
! 44014: ** returned.
! 44015: */
! 44016: SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
! 44017: int rc = SQLITE_OK;
! 44018: assert( pPager->eState==PAGER_WRITER_CACHEMOD
! 44019: || pPager->eState==PAGER_WRITER_DBMOD
! 44020: || pPager->eState==PAGER_WRITER_LOCKED
! 44021: );
! 44022: assert( assert_pager_state(pPager) );
! 44023: if( 0==pagerUseWal(pPager) ){
! 44024: rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
! 44025: }
! 44026: return rc;
! 44027: }
! 44028:
! 44029: /*
! 44030: ** Sync the database file for the pager pPager. zMaster points to the name
! 44031: ** of a master journal file that should be written into the individual
! 44032: ** journal file. zMaster may be NULL, which is interpreted as no master
! 44033: ** journal (a single database transaction).
! 44034: **
! 44035: ** This routine ensures that:
! 44036: **
! 44037: ** * The database file change-counter is updated,
! 44038: ** * the journal is synced (unless the atomic-write optimization is used),
! 44039: ** * all dirty pages are written to the database file,
! 44040: ** * the database file is truncated (if required), and
! 44041: ** * the database file synced.
! 44042: **
! 44043: ** The only thing that remains to commit the transaction is to finalize
! 44044: ** (delete, truncate or zero the first part of) the journal file (or
! 44045: ** delete the master journal file if specified).
! 44046: **
! 44047: ** Note that if zMaster==NULL, this does not overwrite a previous value
! 44048: ** passed to an sqlite3PagerCommitPhaseOne() call.
! 44049: **
! 44050: ** If the final parameter - noSync - is true, then the database file itself
! 44051: ** is not synced. The caller must call sqlite3PagerSync() directly to
! 44052: ** sync the database file before calling CommitPhaseTwo() to delete the
! 44053: ** journal file in this case.
! 44054: */
! 44055: SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
! 44056: Pager *pPager, /* Pager object */
! 44057: const char *zMaster, /* If not NULL, the master journal name */
! 44058: int noSync /* True to omit the xSync on the db file */
! 44059: ){
! 44060: int rc = SQLITE_OK; /* Return code */
! 44061:
! 44062: assert( pPager->eState==PAGER_WRITER_LOCKED
! 44063: || pPager->eState==PAGER_WRITER_CACHEMOD
! 44064: || pPager->eState==PAGER_WRITER_DBMOD
! 44065: || pPager->eState==PAGER_ERROR
! 44066: );
! 44067: assert( assert_pager_state(pPager) );
! 44068:
! 44069: /* If a prior error occurred, report that error again. */
! 44070: if( NEVER(pPager->errCode) ) return pPager->errCode;
! 44071:
! 44072: PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
! 44073: pPager->zFilename, zMaster, pPager->dbSize));
! 44074:
! 44075: /* If no database changes have been made, return early. */
! 44076: if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
! 44077:
! 44078: if( MEMDB ){
! 44079: /* If this is an in-memory db, or no pages have been written to, or this
! 44080: ** function has already been called, it is mostly a no-op. However, any
! 44081: ** backup in progress needs to be restarted.
! 44082: */
! 44083: sqlite3BackupRestart(pPager->pBackup);
! 44084: }else{
! 44085: if( pagerUseWal(pPager) ){
! 44086: PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
! 44087: PgHdr *pPageOne = 0;
! 44088: if( pList==0 ){
! 44089: /* Must have at least one page for the WAL commit flag.
! 44090: ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
! 44091: rc = sqlite3PagerGet(pPager, 1, &pPageOne);
! 44092: pList = pPageOne;
! 44093: pList->pDirty = 0;
! 44094: }
! 44095: assert( rc==SQLITE_OK );
! 44096: if( ALWAYS(pList) ){
! 44097: rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
! 44098: }
! 44099: sqlite3PagerUnref(pPageOne);
! 44100: if( rc==SQLITE_OK ){
! 44101: sqlite3PcacheCleanAll(pPager->pPCache);
! 44102: }
! 44103: }else{
! 44104: /* The following block updates the change-counter. Exactly how it
! 44105: ** does this depends on whether or not the atomic-update optimization
! 44106: ** was enabled at compile time, and if this transaction meets the
! 44107: ** runtime criteria to use the operation:
! 44108: **
! 44109: ** * The file-system supports the atomic-write property for
! 44110: ** blocks of size page-size, and
! 44111: ** * This commit is not part of a multi-file transaction, and
! 44112: ** * Exactly one page has been modified and store in the journal file.
! 44113: **
! 44114: ** If the optimization was not enabled at compile time, then the
! 44115: ** pager_incr_changecounter() function is called to update the change
! 44116: ** counter in 'indirect-mode'. If the optimization is compiled in but
! 44117: ** is not applicable to this transaction, call sqlite3JournalCreate()
! 44118: ** to make sure the journal file has actually been created, then call
! 44119: ** pager_incr_changecounter() to update the change-counter in indirect
! 44120: ** mode.
! 44121: **
! 44122: ** Otherwise, if the optimization is both enabled and applicable,
! 44123: ** then call pager_incr_changecounter() to update the change-counter
! 44124: ** in 'direct' mode. In this case the journal file will never be
! 44125: ** created for this transaction.
! 44126: */
! 44127: #ifdef SQLITE_ENABLE_ATOMIC_WRITE
! 44128: PgHdr *pPg;
! 44129: assert( isOpen(pPager->jfd)
! 44130: || pPager->journalMode==PAGER_JOURNALMODE_OFF
! 44131: || pPager->journalMode==PAGER_JOURNALMODE_WAL
! 44132: );
! 44133: if( !zMaster && isOpen(pPager->jfd)
! 44134: && pPager->journalOff==jrnlBufferSize(pPager)
! 44135: && pPager->dbSize>=pPager->dbOrigSize
! 44136: && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
! 44137: ){
! 44138: /* Update the db file change counter via the direct-write method. The
! 44139: ** following call will modify the in-memory representation of page 1
! 44140: ** to include the updated change counter and then write page 1
! 44141: ** directly to the database file. Because of the atomic-write
! 44142: ** property of the host file-system, this is safe.
! 44143: */
! 44144: rc = pager_incr_changecounter(pPager, 1);
! 44145: }else{
! 44146: rc = sqlite3JournalCreate(pPager->jfd);
! 44147: if( rc==SQLITE_OK ){
! 44148: rc = pager_incr_changecounter(pPager, 0);
! 44149: }
! 44150: }
! 44151: #else
! 44152: rc = pager_incr_changecounter(pPager, 0);
! 44153: #endif
! 44154: if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
! 44155:
! 44156: /* If this transaction has made the database smaller, then all pages
! 44157: ** being discarded by the truncation must be written to the journal
! 44158: ** file. This can only happen in auto-vacuum mode.
! 44159: **
! 44160: ** Before reading the pages with page numbers larger than the
! 44161: ** current value of Pager.dbSize, set dbSize back to the value
! 44162: ** that it took at the start of the transaction. Otherwise, the
! 44163: ** calls to sqlite3PagerGet() return zeroed pages instead of
! 44164: ** reading data from the database file.
! 44165: */
! 44166: #ifndef SQLITE_OMIT_AUTOVACUUM
! 44167: if( pPager->dbSize<pPager->dbOrigSize
! 44168: && pPager->journalMode!=PAGER_JOURNALMODE_OFF
! 44169: ){
! 44170: Pgno i; /* Iterator variable */
! 44171: const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
! 44172: const Pgno dbSize = pPager->dbSize; /* Database image size */
! 44173: pPager->dbSize = pPager->dbOrigSize;
! 44174: for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
! 44175: if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
! 44176: PgHdr *pPage; /* Page to journal */
! 44177: rc = sqlite3PagerGet(pPager, i, &pPage);
! 44178: if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
! 44179: rc = sqlite3PagerWrite(pPage);
! 44180: sqlite3PagerUnref(pPage);
! 44181: if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
! 44182: }
! 44183: }
! 44184: pPager->dbSize = dbSize;
! 44185: }
! 44186: #endif
! 44187:
! 44188: /* Write the master journal name into the journal file. If a master
! 44189: ** journal file name has already been written to the journal file,
! 44190: ** or if zMaster is NULL (no master journal), then this call is a no-op.
! 44191: */
! 44192: rc = writeMasterJournal(pPager, zMaster);
! 44193: if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
! 44194:
! 44195: /* Sync the journal file and write all dirty pages to the database.
! 44196: ** If the atomic-update optimization is being used, this sync will not
! 44197: ** create the journal file or perform any real IO.
! 44198: **
! 44199: ** Because the change-counter page was just modified, unless the
! 44200: ** atomic-update optimization is used it is almost certain that the
! 44201: ** journal requires a sync here. However, in locking_mode=exclusive
! 44202: ** on a system under memory pressure it is just possible that this is
! 44203: ** not the case. In this case it is likely enough that the redundant
! 44204: ** xSync() call will be changed to a no-op by the OS anyhow.
! 44205: */
! 44206: rc = syncJournal(pPager, 0);
! 44207: if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
! 44208:
! 44209: rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
! 44210: if( rc!=SQLITE_OK ){
! 44211: assert( rc!=SQLITE_IOERR_BLOCKED );
! 44212: goto commit_phase_one_exit;
! 44213: }
! 44214: sqlite3PcacheCleanAll(pPager->pPCache);
! 44215:
! 44216: /* If the file on disk is not the same size as the database image,
! 44217: ** then use pager_truncate to grow or shrink the file here.
! 44218: */
! 44219: if( pPager->dbSize!=pPager->dbFileSize ){
! 44220: Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
! 44221: assert( pPager->eState==PAGER_WRITER_DBMOD );
! 44222: rc = pager_truncate(pPager, nNew);
! 44223: if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
! 44224: }
! 44225:
! 44226: /* Finally, sync the database file. */
! 44227: if( !noSync ){
! 44228: rc = sqlite3PagerSync(pPager);
! 44229: }
! 44230: IOTRACE(("DBSYNC %p\n", pPager))
! 44231: }
! 44232: }
! 44233:
! 44234: commit_phase_one_exit:
! 44235: if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
! 44236: pPager->eState = PAGER_WRITER_FINISHED;
! 44237: }
! 44238: return rc;
! 44239: }
! 44240:
! 44241:
! 44242: /*
! 44243: ** When this function is called, the database file has been completely
! 44244: ** updated to reflect the changes made by the current transaction and
! 44245: ** synced to disk. The journal file still exists in the file-system
! 44246: ** though, and if a failure occurs at this point it will eventually
! 44247: ** be used as a hot-journal and the current transaction rolled back.
! 44248: **
! 44249: ** This function finalizes the journal file, either by deleting,
! 44250: ** truncating or partially zeroing it, so that it cannot be used
! 44251: ** for hot-journal rollback. Once this is done the transaction is
! 44252: ** irrevocably committed.
! 44253: **
! 44254: ** If an error occurs, an IO error code is returned and the pager
! 44255: ** moves into the error state. Otherwise, SQLITE_OK is returned.
! 44256: */
! 44257: SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
! 44258: int rc = SQLITE_OK; /* Return code */
! 44259:
! 44260: /* This routine should not be called if a prior error has occurred.
! 44261: ** But if (due to a coding error elsewhere in the system) it does get
! 44262: ** called, just return the same error code without doing anything. */
! 44263: if( NEVER(pPager->errCode) ) return pPager->errCode;
! 44264:
! 44265: assert( pPager->eState==PAGER_WRITER_LOCKED
! 44266: || pPager->eState==PAGER_WRITER_FINISHED
! 44267: || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
! 44268: );
! 44269: assert( assert_pager_state(pPager) );
! 44270:
! 44271: /* An optimization. If the database was not actually modified during
! 44272: ** this transaction, the pager is running in exclusive-mode and is
! 44273: ** using persistent journals, then this function is a no-op.
! 44274: **
! 44275: ** The start of the journal file currently contains a single journal
! 44276: ** header with the nRec field set to 0. If such a journal is used as
! 44277: ** a hot-journal during hot-journal rollback, 0 changes will be made
! 44278: ** to the database file. So there is no need to zero the journal
! 44279: ** header. Since the pager is in exclusive mode, there is no need
! 44280: ** to drop any locks either.
! 44281: */
! 44282: if( pPager->eState==PAGER_WRITER_LOCKED
! 44283: && pPager->exclusiveMode
! 44284: && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
! 44285: ){
! 44286: assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
! 44287: pPager->eState = PAGER_READER;
! 44288: return SQLITE_OK;
! 44289: }
! 44290:
! 44291: PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
! 44292: rc = pager_end_transaction(pPager, pPager->setMaster);
! 44293: return pager_error(pPager, rc);
! 44294: }
! 44295:
! 44296: /*
! 44297: ** If a write transaction is open, then all changes made within the
! 44298: ** transaction are reverted and the current write-transaction is closed.
! 44299: ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
! 44300: ** state if an error occurs.
! 44301: **
! 44302: ** If the pager is already in PAGER_ERROR state when this function is called,
! 44303: ** it returns Pager.errCode immediately. No work is performed in this case.
! 44304: **
! 44305: ** Otherwise, in rollback mode, this function performs two functions:
! 44306: **
! 44307: ** 1) It rolls back the journal file, restoring all database file and
! 44308: ** in-memory cache pages to the state they were in when the transaction
! 44309: ** was opened, and
! 44310: **
! 44311: ** 2) It finalizes the journal file, so that it is not used for hot
! 44312: ** rollback at any point in the future.
! 44313: **
! 44314: ** Finalization of the journal file (task 2) is only performed if the
! 44315: ** rollback is successful.
! 44316: **
! 44317: ** In WAL mode, all cache-entries containing data modified within the
! 44318: ** current transaction are either expelled from the cache or reverted to
! 44319: ** their pre-transaction state by re-reading data from the database or
! 44320: ** WAL files. The WAL transaction is then closed.
! 44321: */
! 44322: SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
! 44323: int rc = SQLITE_OK; /* Return code */
! 44324: PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
! 44325:
! 44326: /* PagerRollback() is a no-op if called in READER or OPEN state. If
! 44327: ** the pager is already in the ERROR state, the rollback is not
! 44328: ** attempted here. Instead, the error code is returned to the caller.
! 44329: */
! 44330: assert( assert_pager_state(pPager) );
! 44331: if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
! 44332: if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
! 44333:
! 44334: if( pagerUseWal(pPager) ){
! 44335: int rc2;
! 44336: rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
! 44337: rc2 = pager_end_transaction(pPager, pPager->setMaster);
! 44338: if( rc==SQLITE_OK ) rc = rc2;
! 44339: }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
! 44340: int eState = pPager->eState;
! 44341: rc = pager_end_transaction(pPager, 0);
! 44342: if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
! 44343: /* This can happen using journal_mode=off. Move the pager to the error
! 44344: ** state to indicate that the contents of the cache may not be trusted.
! 44345: ** Any active readers will get SQLITE_ABORT.
! 44346: */
! 44347: pPager->errCode = SQLITE_ABORT;
! 44348: pPager->eState = PAGER_ERROR;
! 44349: return rc;
! 44350: }
! 44351: }else{
! 44352: rc = pager_playback(pPager, 0);
! 44353: }
! 44354:
! 44355: assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
! 44356: assert( rc==SQLITE_OK || rc==SQLITE_FULL
! 44357: || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
! 44358:
! 44359: /* If an error occurs during a ROLLBACK, we can no longer trust the pager
! 44360: ** cache. So call pager_error() on the way out to make any error persistent.
! 44361: */
! 44362: return pager_error(pPager, rc);
! 44363: }
! 44364:
! 44365: /*
! 44366: ** Return TRUE if the database file is opened read-only. Return FALSE
! 44367: ** if the database is (in theory) writable.
! 44368: */
! 44369: SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
! 44370: return pPager->readOnly;
! 44371: }
! 44372:
! 44373: /*
! 44374: ** Return the number of references to the pager.
! 44375: */
! 44376: SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
! 44377: return sqlite3PcacheRefCount(pPager->pPCache);
! 44378: }
! 44379:
! 44380: /*
! 44381: ** Return the approximate number of bytes of memory currently
! 44382: ** used by the pager and its associated cache.
! 44383: */
! 44384: SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
! 44385: int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
! 44386: + 5*sizeof(void*);
! 44387: return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
! 44388: + sqlite3MallocSize(pPager)
! 44389: + pPager->pageSize;
! 44390: }
! 44391:
! 44392: /*
! 44393: ** Return the number of references to the specified page.
! 44394: */
! 44395: SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
! 44396: return sqlite3PcachePageRefcount(pPage);
! 44397: }
! 44398:
! 44399: #ifdef SQLITE_TEST
! 44400: /*
! 44401: ** This routine is used for testing and analysis only.
! 44402: */
! 44403: SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
! 44404: static int a[11];
! 44405: a[0] = sqlite3PcacheRefCount(pPager->pPCache);
! 44406: a[1] = sqlite3PcachePagecount(pPager->pPCache);
! 44407: a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
! 44408: a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
! 44409: a[4] = pPager->eState;
! 44410: a[5] = pPager->errCode;
! 44411: a[6] = pPager->nHit;
! 44412: a[7] = pPager->nMiss;
! 44413: a[8] = 0; /* Used to be pPager->nOvfl */
! 44414: a[9] = pPager->nRead;
! 44415: a[10] = pPager->nWrite;
! 44416: return a;
! 44417: }
! 44418: #endif
! 44419:
! 44420: /*
! 44421: ** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
! 44422: ** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
! 44423: ** current cache hit or miss count, according to the value of eStat. If the
! 44424: ** reset parameter is non-zero, the cache hit or miss count is zeroed before
! 44425: ** returning.
! 44426: */
! 44427: SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
! 44428: int *piStat;
! 44429:
! 44430: assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
! 44431: || eStat==SQLITE_DBSTATUS_CACHE_MISS
! 44432: );
! 44433: if( eStat==SQLITE_DBSTATUS_CACHE_HIT ){
! 44434: piStat = &pPager->nHit;
! 44435: }else{
! 44436: piStat = &pPager->nMiss;
! 44437: }
! 44438:
! 44439: *pnVal += *piStat;
! 44440: if( reset ){
! 44441: *piStat = 0;
! 44442: }
! 44443: }
! 44444:
! 44445: /*
! 44446: ** Return true if this is an in-memory pager.
! 44447: */
! 44448: SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
! 44449: return MEMDB;
! 44450: }
! 44451:
! 44452: /*
! 44453: ** Check that there are at least nSavepoint savepoints open. If there are
! 44454: ** currently less than nSavepoints open, then open one or more savepoints
! 44455: ** to make up the difference. If the number of savepoints is already
! 44456: ** equal to nSavepoint, then this function is a no-op.
! 44457: **
! 44458: ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
! 44459: ** occurs while opening the sub-journal file, then an IO error code is
! 44460: ** returned. Otherwise, SQLITE_OK.
! 44461: */
! 44462: SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
! 44463: int rc = SQLITE_OK; /* Return code */
! 44464: int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
! 44465:
! 44466: assert( pPager->eState>=PAGER_WRITER_LOCKED );
! 44467: assert( assert_pager_state(pPager) );
! 44468:
! 44469: if( nSavepoint>nCurrent && pPager->useJournal ){
! 44470: int ii; /* Iterator variable */
! 44471: PagerSavepoint *aNew; /* New Pager.aSavepoint array */
! 44472:
! 44473: /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
! 44474: ** if the allocation fails. Otherwise, zero the new portion in case a
! 44475: ** malloc failure occurs while populating it in the for(...) loop below.
! 44476: */
! 44477: aNew = (PagerSavepoint *)sqlite3Realloc(
! 44478: pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
! 44479: );
! 44480: if( !aNew ){
! 44481: return SQLITE_NOMEM;
! 44482: }
! 44483: memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
! 44484: pPager->aSavepoint = aNew;
! 44485:
! 44486: /* Populate the PagerSavepoint structures just allocated. */
! 44487: for(ii=nCurrent; ii<nSavepoint; ii++){
! 44488: aNew[ii].nOrig = pPager->dbSize;
! 44489: if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
! 44490: aNew[ii].iOffset = pPager->journalOff;
! 44491: }else{
! 44492: aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
! 44493: }
! 44494: aNew[ii].iSubRec = pPager->nSubRec;
! 44495: aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
! 44496: if( !aNew[ii].pInSavepoint ){
! 44497: return SQLITE_NOMEM;
! 44498: }
! 44499: if( pagerUseWal(pPager) ){
! 44500: sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
! 44501: }
! 44502: pPager->nSavepoint = ii+1;
! 44503: }
! 44504: assert( pPager->nSavepoint==nSavepoint );
! 44505: assertTruncateConstraint(pPager);
! 44506: }
! 44507:
! 44508: return rc;
! 44509: }
! 44510:
! 44511: /*
! 44512: ** This function is called to rollback or release (commit) a savepoint.
! 44513: ** The savepoint to release or rollback need not be the most recently
! 44514: ** created savepoint.
! 44515: **
! 44516: ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
! 44517: ** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
! 44518: ** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
! 44519: ** that have occurred since the specified savepoint was created.
! 44520: **
! 44521: ** The savepoint to rollback or release is identified by parameter
! 44522: ** iSavepoint. A value of 0 means to operate on the outermost savepoint
! 44523: ** (the first created). A value of (Pager.nSavepoint-1) means operate
! 44524: ** on the most recently created savepoint. If iSavepoint is greater than
! 44525: ** (Pager.nSavepoint-1), then this function is a no-op.
! 44526: **
! 44527: ** If a negative value is passed to this function, then the current
! 44528: ** transaction is rolled back. This is different to calling
! 44529: ** sqlite3PagerRollback() because this function does not terminate
! 44530: ** the transaction or unlock the database, it just restores the
! 44531: ** contents of the database to its original state.
! 44532: **
! 44533: ** In any case, all savepoints with an index greater than iSavepoint
! 44534: ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
! 44535: ** then savepoint iSavepoint is also destroyed.
! 44536: **
! 44537: ** This function may return SQLITE_NOMEM if a memory allocation fails,
! 44538: ** or an IO error code if an IO error occurs while rolling back a
! 44539: ** savepoint. If no errors occur, SQLITE_OK is returned.
! 44540: */
! 44541: SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
! 44542: int rc = pPager->errCode; /* Return code */
! 44543:
! 44544: assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
! 44545: assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
! 44546:
! 44547: if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
! 44548: int ii; /* Iterator variable */
! 44549: int nNew; /* Number of remaining savepoints after this op. */
! 44550:
! 44551: /* Figure out how many savepoints will still be active after this
! 44552: ** operation. Store this value in nNew. Then free resources associated
! 44553: ** with any savepoints that are destroyed by this operation.
! 44554: */
! 44555: nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
! 44556: for(ii=nNew; ii<pPager->nSavepoint; ii++){
! 44557: sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
! 44558: }
! 44559: pPager->nSavepoint = nNew;
! 44560:
! 44561: /* If this is a release of the outermost savepoint, truncate
! 44562: ** the sub-journal to zero bytes in size. */
! 44563: if( op==SAVEPOINT_RELEASE ){
! 44564: if( nNew==0 && isOpen(pPager->sjfd) ){
! 44565: /* Only truncate if it is an in-memory sub-journal. */
! 44566: if( sqlite3IsMemJournal(pPager->sjfd) ){
! 44567: rc = sqlite3OsTruncate(pPager->sjfd, 0);
! 44568: assert( rc==SQLITE_OK );
! 44569: }
! 44570: pPager->nSubRec = 0;
! 44571: }
! 44572: }
! 44573: /* Else this is a rollback operation, playback the specified savepoint.
! 44574: ** If this is a temp-file, it is possible that the journal file has
! 44575: ** not yet been opened. In this case there have been no changes to
! 44576: ** the database file, so the playback operation can be skipped.
! 44577: */
! 44578: else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
! 44579: PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
! 44580: rc = pagerPlaybackSavepoint(pPager, pSavepoint);
! 44581: assert(rc!=SQLITE_DONE);
! 44582: }
! 44583: }
! 44584:
! 44585: return rc;
! 44586: }
! 44587:
! 44588: /*
! 44589: ** Return the full pathname of the database file.
! 44590: */
! 44591: SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager){
! 44592: return pPager->zFilename;
! 44593: }
! 44594:
! 44595: /*
! 44596: ** Return the VFS structure for the pager.
! 44597: */
! 44598: SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
! 44599: return pPager->pVfs;
! 44600: }
! 44601:
! 44602: /*
! 44603: ** Return the file handle for the database file associated
! 44604: ** with the pager. This might return NULL if the file has
! 44605: ** not yet been opened.
! 44606: */
! 44607: SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
! 44608: return pPager->fd;
! 44609: }
! 44610:
! 44611: /*
! 44612: ** Return the full pathname of the journal file.
! 44613: */
! 44614: SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
! 44615: return pPager->zJournal;
! 44616: }
! 44617:
! 44618: /*
! 44619: ** Return true if fsync() calls are disabled for this pager. Return FALSE
! 44620: ** if fsync()s are executed normally.
! 44621: */
! 44622: SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
! 44623: return pPager->noSync;
! 44624: }
! 44625:
! 44626: #ifdef SQLITE_HAS_CODEC
! 44627: /*
! 44628: ** Set or retrieve the codec for this pager
! 44629: */
! 44630: SQLITE_PRIVATE void sqlite3PagerSetCodec(
! 44631: Pager *pPager,
! 44632: void *(*xCodec)(void*,void*,Pgno,int),
! 44633: void (*xCodecSizeChng)(void*,int,int),
! 44634: void (*xCodecFree)(void*),
! 44635: void *pCodec
! 44636: ){
! 44637: if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
! 44638: pPager->xCodec = pPager->memDb ? 0 : xCodec;
! 44639: pPager->xCodecSizeChng = xCodecSizeChng;
! 44640: pPager->xCodecFree = xCodecFree;
! 44641: pPager->pCodec = pCodec;
! 44642: pagerReportSize(pPager);
! 44643: }
! 44644: SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
! 44645: return pPager->pCodec;
! 44646: }
! 44647: #endif
! 44648:
! 44649: #ifndef SQLITE_OMIT_AUTOVACUUM
! 44650: /*
! 44651: ** Move the page pPg to location pgno in the file.
! 44652: **
! 44653: ** There must be no references to the page previously located at
! 44654: ** pgno (which we call pPgOld) though that page is allowed to be
! 44655: ** in cache. If the page previously located at pgno is not already
! 44656: ** in the rollback journal, it is not put there by by this routine.
! 44657: **
! 44658: ** References to the page pPg remain valid. Updating any
! 44659: ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
! 44660: ** allocated along with the page) is the responsibility of the caller.
! 44661: **
! 44662: ** A transaction must be active when this routine is called. It used to be
! 44663: ** required that a statement transaction was not active, but this restriction
! 44664: ** has been removed (CREATE INDEX needs to move a page when a statement
! 44665: ** transaction is active).
! 44666: **
! 44667: ** If the fourth argument, isCommit, is non-zero, then this page is being
! 44668: ** moved as part of a database reorganization just before the transaction
! 44669: ** is being committed. In this case, it is guaranteed that the database page
! 44670: ** pPg refers to will not be written to again within this transaction.
! 44671: **
! 44672: ** This function may return SQLITE_NOMEM or an IO error code if an error
! 44673: ** occurs. Otherwise, it returns SQLITE_OK.
! 44674: */
! 44675: SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
! 44676: PgHdr *pPgOld; /* The page being overwritten. */
! 44677: Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
! 44678: int rc; /* Return code */
! 44679: Pgno origPgno; /* The original page number */
! 44680:
! 44681: assert( pPg->nRef>0 );
! 44682: assert( pPager->eState==PAGER_WRITER_CACHEMOD
! 44683: || pPager->eState==PAGER_WRITER_DBMOD
! 44684: );
! 44685: assert( assert_pager_state(pPager) );
! 44686:
! 44687: /* In order to be able to rollback, an in-memory database must journal
! 44688: ** the page we are moving from.
! 44689: */
! 44690: if( MEMDB ){
! 44691: rc = sqlite3PagerWrite(pPg);
! 44692: if( rc ) return rc;
! 44693: }
! 44694:
! 44695: /* If the page being moved is dirty and has not been saved by the latest
! 44696: ** savepoint, then save the current contents of the page into the
! 44697: ** sub-journal now. This is required to handle the following scenario:
! 44698: **
! 44699: ** BEGIN;
! 44700: ** <journal page X, then modify it in memory>
! 44701: ** SAVEPOINT one;
! 44702: ** <Move page X to location Y>
! 44703: ** ROLLBACK TO one;
! 44704: **
! 44705: ** If page X were not written to the sub-journal here, it would not
! 44706: ** be possible to restore its contents when the "ROLLBACK TO one"
! 44707: ** statement were is processed.
! 44708: **
! 44709: ** subjournalPage() may need to allocate space to store pPg->pgno into
! 44710: ** one or more savepoint bitvecs. This is the reason this function
! 44711: ** may return SQLITE_NOMEM.
! 44712: */
! 44713: if( pPg->flags&PGHDR_DIRTY
! 44714: && subjRequiresPage(pPg)
! 44715: && SQLITE_OK!=(rc = subjournalPage(pPg))
! 44716: ){
! 44717: return rc;
! 44718: }
! 44719:
! 44720: PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
! 44721: PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
! 44722: IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
! 44723:
! 44724: /* If the journal needs to be sync()ed before page pPg->pgno can
! 44725: ** be written to, store pPg->pgno in local variable needSyncPgno.
! 44726: **
! 44727: ** If the isCommit flag is set, there is no need to remember that
! 44728: ** the journal needs to be sync()ed before database page pPg->pgno
! 44729: ** can be written to. The caller has already promised not to write to it.
! 44730: */
! 44731: if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
! 44732: needSyncPgno = pPg->pgno;
! 44733: assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
! 44734: assert( pPg->flags&PGHDR_DIRTY );
! 44735: }
! 44736:
! 44737: /* If the cache contains a page with page-number pgno, remove it
! 44738: ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
! 44739: ** page pgno before the 'move' operation, it needs to be retained
! 44740: ** for the page moved there.
! 44741: */
! 44742: pPg->flags &= ~PGHDR_NEED_SYNC;
! 44743: pPgOld = pager_lookup(pPager, pgno);
! 44744: assert( !pPgOld || pPgOld->nRef==1 );
! 44745: if( pPgOld ){
! 44746: pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
! 44747: if( MEMDB ){
! 44748: /* Do not discard pages from an in-memory database since we might
! 44749: ** need to rollback later. Just move the page out of the way. */
! 44750: sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
! 44751: }else{
! 44752: sqlite3PcacheDrop(pPgOld);
! 44753: }
! 44754: }
! 44755:
! 44756: origPgno = pPg->pgno;
! 44757: sqlite3PcacheMove(pPg, pgno);
! 44758: sqlite3PcacheMakeDirty(pPg);
! 44759:
! 44760: /* For an in-memory database, make sure the original page continues
! 44761: ** to exist, in case the transaction needs to roll back. Use pPgOld
! 44762: ** as the original page since it has already been allocated.
! 44763: */
! 44764: if( MEMDB ){
! 44765: assert( pPgOld );
! 44766: sqlite3PcacheMove(pPgOld, origPgno);
! 44767: sqlite3PagerUnref(pPgOld);
! 44768: }
! 44769:
! 44770: if( needSyncPgno ){
! 44771: /* If needSyncPgno is non-zero, then the journal file needs to be
! 44772: ** sync()ed before any data is written to database file page needSyncPgno.
! 44773: ** Currently, no such page exists in the page-cache and the
! 44774: ** "is journaled" bitvec flag has been set. This needs to be remedied by
! 44775: ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
! 44776: ** flag.
! 44777: **
! 44778: ** If the attempt to load the page into the page-cache fails, (due
! 44779: ** to a malloc() or IO failure), clear the bit in the pInJournal[]
! 44780: ** array. Otherwise, if the page is loaded and written again in
! 44781: ** this transaction, it may be written to the database file before
! 44782: ** it is synced into the journal file. This way, it may end up in
! 44783: ** the journal file twice, but that is not a problem.
! 44784: */
! 44785: PgHdr *pPgHdr;
! 44786: rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
! 44787: if( rc!=SQLITE_OK ){
! 44788: if( needSyncPgno<=pPager->dbOrigSize ){
! 44789: assert( pPager->pTmpSpace!=0 );
! 44790: sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
! 44791: }
! 44792: return rc;
! 44793: }
! 44794: pPgHdr->flags |= PGHDR_NEED_SYNC;
! 44795: sqlite3PcacheMakeDirty(pPgHdr);
! 44796: sqlite3PagerUnref(pPgHdr);
! 44797: }
! 44798:
! 44799: return SQLITE_OK;
! 44800: }
! 44801: #endif
! 44802:
! 44803: /*
! 44804: ** Return a pointer to the data for the specified page.
! 44805: */
! 44806: SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
! 44807: assert( pPg->nRef>0 || pPg->pPager->memDb );
! 44808: return pPg->pData;
! 44809: }
! 44810:
! 44811: /*
! 44812: ** Return a pointer to the Pager.nExtra bytes of "extra" space
! 44813: ** allocated along with the specified page.
! 44814: */
! 44815: SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
! 44816: return pPg->pExtra;
! 44817: }
! 44818:
! 44819: /*
! 44820: ** Get/set the locking-mode for this pager. Parameter eMode must be one
! 44821: ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
! 44822: ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
! 44823: ** the locking-mode is set to the value specified.
! 44824: **
! 44825: ** The returned value is either PAGER_LOCKINGMODE_NORMAL or
! 44826: ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
! 44827: ** locking-mode.
! 44828: */
! 44829: SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
! 44830: assert( eMode==PAGER_LOCKINGMODE_QUERY
! 44831: || eMode==PAGER_LOCKINGMODE_NORMAL
! 44832: || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
! 44833: assert( PAGER_LOCKINGMODE_QUERY<0 );
! 44834: assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
! 44835: assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
! 44836: if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
! 44837: pPager->exclusiveMode = (u8)eMode;
! 44838: }
! 44839: return (int)pPager->exclusiveMode;
! 44840: }
! 44841:
! 44842: /*
! 44843: ** Set the journal-mode for this pager. Parameter eMode must be one of:
! 44844: **
! 44845: ** PAGER_JOURNALMODE_DELETE
! 44846: ** PAGER_JOURNALMODE_TRUNCATE
! 44847: ** PAGER_JOURNALMODE_PERSIST
! 44848: ** PAGER_JOURNALMODE_OFF
! 44849: ** PAGER_JOURNALMODE_MEMORY
! 44850: ** PAGER_JOURNALMODE_WAL
! 44851: **
! 44852: ** The journalmode is set to the value specified if the change is allowed.
! 44853: ** The change may be disallowed for the following reasons:
! 44854: **
! 44855: ** * An in-memory database can only have its journal_mode set to _OFF
! 44856: ** or _MEMORY.
! 44857: **
! 44858: ** * Temporary databases cannot have _WAL journalmode.
! 44859: **
! 44860: ** The returned indicate the current (possibly updated) journal-mode.
! 44861: */
! 44862: SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
! 44863: u8 eOld = pPager->journalMode; /* Prior journalmode */
! 44864:
! 44865: #ifdef SQLITE_DEBUG
! 44866: /* The print_pager_state() routine is intended to be used by the debugger
! 44867: ** only. We invoke it once here to suppress a compiler warning. */
! 44868: print_pager_state(pPager);
! 44869: #endif
! 44870:
! 44871:
! 44872: /* The eMode parameter is always valid */
! 44873: assert( eMode==PAGER_JOURNALMODE_DELETE
! 44874: || eMode==PAGER_JOURNALMODE_TRUNCATE
! 44875: || eMode==PAGER_JOURNALMODE_PERSIST
! 44876: || eMode==PAGER_JOURNALMODE_OFF
! 44877: || eMode==PAGER_JOURNALMODE_WAL
! 44878: || eMode==PAGER_JOURNALMODE_MEMORY );
! 44879:
! 44880: /* This routine is only called from the OP_JournalMode opcode, and
! 44881: ** the logic there will never allow a temporary file to be changed
! 44882: ** to WAL mode.
! 44883: */
! 44884: assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
! 44885:
! 44886: /* Do allow the journalmode of an in-memory database to be set to
! 44887: ** anything other than MEMORY or OFF
! 44888: */
! 44889: if( MEMDB ){
! 44890: assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
! 44891: if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
! 44892: eMode = eOld;
! 44893: }
! 44894: }
! 44895:
! 44896: if( eMode!=eOld ){
! 44897:
! 44898: /* Change the journal mode. */
! 44899: assert( pPager->eState!=PAGER_ERROR );
! 44900: pPager->journalMode = (u8)eMode;
! 44901:
! 44902: /* When transistioning from TRUNCATE or PERSIST to any other journal
! 44903: ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
! 44904: ** delete the journal file.
! 44905: */
! 44906: assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
! 44907: assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
! 44908: assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
! 44909: assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
! 44910: assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
! 44911: assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
! 44912:
! 44913: assert( isOpen(pPager->fd) || pPager->exclusiveMode );
! 44914: if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
! 44915:
! 44916: /* In this case we would like to delete the journal file. If it is
! 44917: ** not possible, then that is not a problem. Deleting the journal file
! 44918: ** here is an optimization only.
! 44919: **
! 44920: ** Before deleting the journal file, obtain a RESERVED lock on the
! 44921: ** database file. This ensures that the journal file is not deleted
! 44922: ** while it is in use by some other client.
! 44923: */
! 44924: sqlite3OsClose(pPager->jfd);
! 44925: if( pPager->eLock>=RESERVED_LOCK ){
! 44926: sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
! 44927: }else{
! 44928: int rc = SQLITE_OK;
! 44929: int state = pPager->eState;
! 44930: assert( state==PAGER_OPEN || state==PAGER_READER );
! 44931: if( state==PAGER_OPEN ){
! 44932: rc = sqlite3PagerSharedLock(pPager);
! 44933: }
! 44934: if( pPager->eState==PAGER_READER ){
! 44935: assert( rc==SQLITE_OK );
! 44936: rc = pagerLockDb(pPager, RESERVED_LOCK);
! 44937: }
! 44938: if( rc==SQLITE_OK ){
! 44939: sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
! 44940: }
! 44941: if( rc==SQLITE_OK && state==PAGER_READER ){
! 44942: pagerUnlockDb(pPager, SHARED_LOCK);
! 44943: }else if( state==PAGER_OPEN ){
! 44944: pager_unlock(pPager);
! 44945: }
! 44946: assert( state==pPager->eState );
! 44947: }
! 44948: }
! 44949: }
! 44950:
! 44951: /* Return the new journal mode */
! 44952: return (int)pPager->journalMode;
! 44953: }
! 44954:
! 44955: /*
! 44956: ** Return the current journal mode.
! 44957: */
! 44958: SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
! 44959: return (int)pPager->journalMode;
! 44960: }
! 44961:
! 44962: /*
! 44963: ** Return TRUE if the pager is in a state where it is OK to change the
! 44964: ** journalmode. Journalmode changes can only happen when the database
! 44965: ** is unmodified.
! 44966: */
! 44967: SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
! 44968: assert( assert_pager_state(pPager) );
! 44969: if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
! 44970: if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
! 44971: return 1;
! 44972: }
! 44973:
! 44974: /*
! 44975: ** Get/set the size-limit used for persistent journal files.
! 44976: **
! 44977: ** Setting the size limit to -1 means no limit is enforced.
! 44978: ** An attempt to set a limit smaller than -1 is a no-op.
! 44979: */
! 44980: SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
! 44981: if( iLimit>=-1 ){
! 44982: pPager->journalSizeLimit = iLimit;
! 44983: sqlite3WalLimit(pPager->pWal, iLimit);
! 44984: }
! 44985: return pPager->journalSizeLimit;
! 44986: }
! 44987:
! 44988: /*
! 44989: ** Return a pointer to the pPager->pBackup variable. The backup module
! 44990: ** in backup.c maintains the content of this variable. This module
! 44991: ** uses it opaquely as an argument to sqlite3BackupRestart() and
! 44992: ** sqlite3BackupUpdate() only.
! 44993: */
! 44994: SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
! 44995: return &pPager->pBackup;
! 44996: }
! 44997:
! 44998: #ifndef SQLITE_OMIT_VACUUM
! 44999: /*
! 45000: ** Unless this is an in-memory or temporary database, clear the pager cache.
! 45001: */
! 45002: SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
! 45003: if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
! 45004: }
! 45005: #endif
! 45006:
! 45007: #ifndef SQLITE_OMIT_WAL
! 45008: /*
! 45009: ** This function is called when the user invokes "PRAGMA wal_checkpoint",
! 45010: ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
! 45011: ** or wal_blocking_checkpoint() API functions.
! 45012: **
! 45013: ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
! 45014: */
! 45015: SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
! 45016: int rc = SQLITE_OK;
! 45017: if( pPager->pWal ){
! 45018: rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
! 45019: pPager->xBusyHandler, pPager->pBusyHandlerArg,
! 45020: pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
! 45021: pnLog, pnCkpt
! 45022: );
! 45023: }
! 45024: return rc;
! 45025: }
! 45026:
! 45027: SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
! 45028: return sqlite3WalCallback(pPager->pWal);
! 45029: }
! 45030:
! 45031: /*
! 45032: ** Return true if the underlying VFS for the given pager supports the
! 45033: ** primitives necessary for write-ahead logging.
! 45034: */
! 45035: SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
! 45036: const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
! 45037: return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
! 45038: }
! 45039:
! 45040: /*
! 45041: ** Attempt to take an exclusive lock on the database file. If a PENDING lock
! 45042: ** is obtained instead, immediately release it.
! 45043: */
! 45044: static int pagerExclusiveLock(Pager *pPager){
! 45045: int rc; /* Return code */
! 45046:
! 45047: assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
! 45048: rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
! 45049: if( rc!=SQLITE_OK ){
! 45050: /* If the attempt to grab the exclusive lock failed, release the
! 45051: ** pending lock that may have been obtained instead. */
! 45052: pagerUnlockDb(pPager, SHARED_LOCK);
! 45053: }
! 45054:
! 45055: return rc;
! 45056: }
! 45057:
! 45058: /*
! 45059: ** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
! 45060: ** exclusive-locking mode when this function is called, take an EXCLUSIVE
! 45061: ** lock on the database file and use heap-memory to store the wal-index
! 45062: ** in. Otherwise, use the normal shared-memory.
! 45063: */
! 45064: static int pagerOpenWal(Pager *pPager){
! 45065: int rc = SQLITE_OK;
! 45066:
! 45067: assert( pPager->pWal==0 && pPager->tempFile==0 );
! 45068: assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK || pPager->noReadlock);
! 45069:
! 45070: /* If the pager is already in exclusive-mode, the WAL module will use
! 45071: ** heap-memory for the wal-index instead of the VFS shared-memory
! 45072: ** implementation. Take the exclusive lock now, before opening the WAL
! 45073: ** file, to make sure this is safe.
! 45074: */
! 45075: if( pPager->exclusiveMode ){
! 45076: rc = pagerExclusiveLock(pPager);
! 45077: }
! 45078:
! 45079: /* Open the connection to the log file. If this operation fails,
! 45080: ** (e.g. due to malloc() failure), return an error code.
! 45081: */
! 45082: if( rc==SQLITE_OK ){
! 45083: rc = sqlite3WalOpen(pPager->pVfs,
! 45084: pPager->fd, pPager->zWal, pPager->exclusiveMode,
! 45085: pPager->journalSizeLimit, &pPager->pWal
! 45086: );
! 45087: }
! 45088:
! 45089: return rc;
! 45090: }
! 45091:
! 45092:
! 45093: /*
! 45094: ** The caller must be holding a SHARED lock on the database file to call
! 45095: ** this function.
! 45096: **
! 45097: ** If the pager passed as the first argument is open on a real database
! 45098: ** file (not a temp file or an in-memory database), and the WAL file
! 45099: ** is not already open, make an attempt to open it now. If successful,
! 45100: ** return SQLITE_OK. If an error occurs or the VFS used by the pager does
! 45101: ** not support the xShmXXX() methods, return an error code. *pbOpen is
! 45102: ** not modified in either case.
! 45103: **
! 45104: ** If the pager is open on a temp-file (or in-memory database), or if
! 45105: ** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
! 45106: ** without doing anything.
! 45107: */
! 45108: SQLITE_PRIVATE int sqlite3PagerOpenWal(
! 45109: Pager *pPager, /* Pager object */
! 45110: int *pbOpen /* OUT: Set to true if call is a no-op */
! 45111: ){
! 45112: int rc = SQLITE_OK; /* Return code */
! 45113:
! 45114: assert( assert_pager_state(pPager) );
! 45115: assert( pPager->eState==PAGER_OPEN || pbOpen );
! 45116: assert( pPager->eState==PAGER_READER || !pbOpen );
! 45117: assert( pbOpen==0 || *pbOpen==0 );
! 45118: assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
! 45119:
! 45120: if( !pPager->tempFile && !pPager->pWal ){
! 45121: if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
! 45122:
! 45123: /* Close any rollback journal previously open */
! 45124: sqlite3OsClose(pPager->jfd);
! 45125:
! 45126: rc = pagerOpenWal(pPager);
! 45127: if( rc==SQLITE_OK ){
! 45128: pPager->journalMode = PAGER_JOURNALMODE_WAL;
! 45129: pPager->eState = PAGER_OPEN;
! 45130: }
! 45131: }else{
! 45132: *pbOpen = 1;
! 45133: }
! 45134:
! 45135: return rc;
! 45136: }
! 45137:
! 45138: /*
! 45139: ** This function is called to close the connection to the log file prior
! 45140: ** to switching from WAL to rollback mode.
! 45141: **
! 45142: ** Before closing the log file, this function attempts to take an
! 45143: ** EXCLUSIVE lock on the database file. If this cannot be obtained, an
! 45144: ** error (SQLITE_BUSY) is returned and the log connection is not closed.
! 45145: ** If successful, the EXCLUSIVE lock is not released before returning.
! 45146: */
! 45147: SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
! 45148: int rc = SQLITE_OK;
! 45149:
! 45150: assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
! 45151:
! 45152: /* If the log file is not already open, but does exist in the file-system,
! 45153: ** it may need to be checkpointed before the connection can switch to
! 45154: ** rollback mode. Open it now so this can happen.
! 45155: */
! 45156: if( !pPager->pWal ){
! 45157: int logexists = 0;
! 45158: rc = pagerLockDb(pPager, SHARED_LOCK);
! 45159: if( rc==SQLITE_OK ){
! 45160: rc = sqlite3OsAccess(
! 45161: pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
! 45162: );
! 45163: }
! 45164: if( rc==SQLITE_OK && logexists ){
! 45165: rc = pagerOpenWal(pPager);
! 45166: }
! 45167: }
! 45168:
! 45169: /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
! 45170: ** the database file, the log and log-summary files will be deleted.
! 45171: */
! 45172: if( rc==SQLITE_OK && pPager->pWal ){
! 45173: rc = pagerExclusiveLock(pPager);
! 45174: if( rc==SQLITE_OK ){
! 45175: rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
! 45176: pPager->pageSize, (u8*)pPager->pTmpSpace);
! 45177: pPager->pWal = 0;
! 45178: }
! 45179: }
! 45180: return rc;
! 45181: }
! 45182:
! 45183: #ifdef SQLITE_HAS_CODEC
! 45184: /*
! 45185: ** This function is called by the wal module when writing page content
! 45186: ** into the log file.
! 45187: **
! 45188: ** This function returns a pointer to a buffer containing the encrypted
! 45189: ** page content. If a malloc fails, this function may return NULL.
! 45190: */
! 45191: SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
! 45192: void *aData = 0;
! 45193: CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
! 45194: return aData;
! 45195: }
! 45196: #endif /* SQLITE_HAS_CODEC */
! 45197:
! 45198: #endif /* !SQLITE_OMIT_WAL */
! 45199:
! 45200: #endif /* SQLITE_OMIT_DISKIO */
! 45201:
! 45202: /************** End of pager.c ***********************************************/
! 45203: /************** Begin file wal.c *********************************************/
! 45204: /*
! 45205: ** 2010 February 1
! 45206: **
! 45207: ** The author disclaims copyright to this source code. In place of
! 45208: ** a legal notice, here is a blessing:
! 45209: **
! 45210: ** May you do good and not evil.
! 45211: ** May you find forgiveness for yourself and forgive others.
! 45212: ** May you share freely, never taking more than you give.
! 45213: **
! 45214: *************************************************************************
! 45215: **
! 45216: ** This file contains the implementation of a write-ahead log (WAL) used in
! 45217: ** "journal_mode=WAL" mode.
! 45218: **
! 45219: ** WRITE-AHEAD LOG (WAL) FILE FORMAT
! 45220: **
! 45221: ** A WAL file consists of a header followed by zero or more "frames".
! 45222: ** Each frame records the revised content of a single page from the
! 45223: ** database file. All changes to the database are recorded by writing
! 45224: ** frames into the WAL. Transactions commit when a frame is written that
! 45225: ** contains a commit marker. A single WAL can and usually does record
! 45226: ** multiple transactions. Periodically, the content of the WAL is
! 45227: ** transferred back into the database file in an operation called a
! 45228: ** "checkpoint".
! 45229: **
! 45230: ** A single WAL file can be used multiple times. In other words, the
! 45231: ** WAL can fill up with frames and then be checkpointed and then new
! 45232: ** frames can overwrite the old ones. A WAL always grows from beginning
! 45233: ** toward the end. Checksums and counters attached to each frame are
! 45234: ** used to determine which frames within the WAL are valid and which
! 45235: ** are leftovers from prior checkpoints.
! 45236: **
! 45237: ** The WAL header is 32 bytes in size and consists of the following eight
! 45238: ** big-endian 32-bit unsigned integer values:
! 45239: **
! 45240: ** 0: Magic number. 0x377f0682 or 0x377f0683
! 45241: ** 4: File format version. Currently 3007000
! 45242: ** 8: Database page size. Example: 1024
! 45243: ** 12: Checkpoint sequence number
! 45244: ** 16: Salt-1, random integer incremented with each checkpoint
! 45245: ** 20: Salt-2, a different random integer changing with each ckpt
! 45246: ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
! 45247: ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
! 45248: **
! 45249: ** Immediately following the wal-header are zero or more frames. Each
! 45250: ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
! 45251: ** of page data. The frame-header is six big-endian 32-bit unsigned
! 45252: ** integer values, as follows:
! 45253: **
! 45254: ** 0: Page number.
! 45255: ** 4: For commit records, the size of the database image in pages
! 45256: ** after the commit. For all other records, zero.
! 45257: ** 8: Salt-1 (copied from the header)
! 45258: ** 12: Salt-2 (copied from the header)
! 45259: ** 16: Checksum-1.
! 45260: ** 20: Checksum-2.
! 45261: **
! 45262: ** A frame is considered valid if and only if the following conditions are
! 45263: ** true:
! 45264: **
! 45265: ** (1) The salt-1 and salt-2 values in the frame-header match
! 45266: ** salt values in the wal-header
! 45267: **
! 45268: ** (2) The checksum values in the final 8 bytes of the frame-header
! 45269: ** exactly match the checksum computed consecutively on the
! 45270: ** WAL header and the first 8 bytes and the content of all frames
! 45271: ** up to and including the current frame.
! 45272: **
! 45273: ** The checksum is computed using 32-bit big-endian integers if the
! 45274: ** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
! 45275: ** is computed using little-endian if the magic number is 0x377f0682.
! 45276: ** The checksum values are always stored in the frame header in a
! 45277: ** big-endian format regardless of which byte order is used to compute
! 45278: ** the checksum. The checksum is computed by interpreting the input as
! 45279: ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
! 45280: ** algorithm used for the checksum is as follows:
! 45281: **
! 45282: ** for i from 0 to n-1 step 2:
! 45283: ** s0 += x[i] + s1;
! 45284: ** s1 += x[i+1] + s0;
! 45285: ** endfor
! 45286: **
! 45287: ** Note that s0 and s1 are both weighted checksums using fibonacci weights
! 45288: ** in reverse order (the largest fibonacci weight occurs on the first element
! 45289: ** of the sequence being summed.) The s1 value spans all 32-bit
! 45290: ** terms of the sequence whereas s0 omits the final term.
! 45291: **
! 45292: ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
! 45293: ** WAL is transferred into the database, then the database is VFS.xSync-ed.
! 45294: ** The VFS.xSync operations serve as write barriers - all writes launched
! 45295: ** before the xSync must complete before any write that launches after the
! 45296: ** xSync begins.
! 45297: **
! 45298: ** After each checkpoint, the salt-1 value is incremented and the salt-2
! 45299: ** value is randomized. This prevents old and new frames in the WAL from
! 45300: ** being considered valid at the same time and being checkpointing together
! 45301: ** following a crash.
! 45302: **
! 45303: ** READER ALGORITHM
! 45304: **
! 45305: ** To read a page from the database (call it page number P), a reader
! 45306: ** first checks the WAL to see if it contains page P. If so, then the
! 45307: ** last valid instance of page P that is a followed by a commit frame
! 45308: ** or is a commit frame itself becomes the value read. If the WAL
! 45309: ** contains no copies of page P that are valid and which are a commit
! 45310: ** frame or are followed by a commit frame, then page P is read from
! 45311: ** the database file.
! 45312: **
! 45313: ** To start a read transaction, the reader records the index of the last
! 45314: ** valid frame in the WAL. The reader uses this recorded "mxFrame" value
! 45315: ** for all subsequent read operations. New transactions can be appended
! 45316: ** to the WAL, but as long as the reader uses its original mxFrame value
! 45317: ** and ignores the newly appended content, it will see a consistent snapshot
! 45318: ** of the database from a single point in time. This technique allows
! 45319: ** multiple concurrent readers to view different versions of the database
! 45320: ** content simultaneously.
! 45321: **
! 45322: ** The reader algorithm in the previous paragraphs works correctly, but
! 45323: ** because frames for page P can appear anywhere within the WAL, the
! 45324: ** reader has to scan the entire WAL looking for page P frames. If the
! 45325: ** WAL is large (multiple megabytes is typical) that scan can be slow,
! 45326: ** and read performance suffers. To overcome this problem, a separate
! 45327: ** data structure called the wal-index is maintained to expedite the
! 45328: ** search for frames of a particular page.
! 45329: **
! 45330: ** WAL-INDEX FORMAT
! 45331: **
! 45332: ** Conceptually, the wal-index is shared memory, though VFS implementations
! 45333: ** might choose to implement the wal-index using a mmapped file. Because
! 45334: ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
! 45335: ** on a network filesystem. All users of the database must be able to
! 45336: ** share memory.
! 45337: **
! 45338: ** The wal-index is transient. After a crash, the wal-index can (and should
! 45339: ** be) reconstructed from the original WAL file. In fact, the VFS is required
! 45340: ** to either truncate or zero the header of the wal-index when the last
! 45341: ** connection to it closes. Because the wal-index is transient, it can
! 45342: ** use an architecture-specific format; it does not have to be cross-platform.
! 45343: ** Hence, unlike the database and WAL file formats which store all values
! 45344: ** as big endian, the wal-index can store multi-byte values in the native
! 45345: ** byte order of the host computer.
! 45346: **
! 45347: ** The purpose of the wal-index is to answer this question quickly: Given
! 45348: ** a page number P, return the index of the last frame for page P in the WAL,
! 45349: ** or return NULL if there are no frames for page P in the WAL.
! 45350: **
! 45351: ** The wal-index consists of a header region, followed by an one or
! 45352: ** more index blocks.
! 45353: **
! 45354: ** The wal-index header contains the total number of frames within the WAL
! 45355: ** in the the mxFrame field.
! 45356: **
! 45357: ** Each index block except for the first contains information on
! 45358: ** HASHTABLE_NPAGE frames. The first index block contains information on
! 45359: ** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
! 45360: ** HASHTABLE_NPAGE are selected so that together the wal-index header and
! 45361: ** first index block are the same size as all other index blocks in the
! 45362: ** wal-index.
! 45363: **
! 45364: ** Each index block contains two sections, a page-mapping that contains the
! 45365: ** database page number associated with each wal frame, and a hash-table
! 45366: ** that allows readers to query an index block for a specific page number.
! 45367: ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
! 45368: ** for the first index block) 32-bit page numbers. The first entry in the
! 45369: ** first index-block contains the database page number corresponding to the
! 45370: ** first frame in the WAL file. The first entry in the second index block
! 45371: ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
! 45372: ** the log, and so on.
! 45373: **
! 45374: ** The last index block in a wal-index usually contains less than the full
! 45375: ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
! 45376: ** depending on the contents of the WAL file. This does not change the
! 45377: ** allocated size of the page-mapping array - the page-mapping array merely
! 45378: ** contains unused entries.
! 45379: **
! 45380: ** Even without using the hash table, the last frame for page P
! 45381: ** can be found by scanning the page-mapping sections of each index block
! 45382: ** starting with the last index block and moving toward the first, and
! 45383: ** within each index block, starting at the end and moving toward the
! 45384: ** beginning. The first entry that equals P corresponds to the frame
! 45385: ** holding the content for that page.
! 45386: **
! 45387: ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
! 45388: ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
! 45389: ** hash table for each page number in the mapping section, so the hash
! 45390: ** table is never more than half full. The expected number of collisions
! 45391: ** prior to finding a match is 1. Each entry of the hash table is an
! 45392: ** 1-based index of an entry in the mapping section of the same
! 45393: ** index block. Let K be the 1-based index of the largest entry in
! 45394: ** the mapping section. (For index blocks other than the last, K will
! 45395: ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
! 45396: ** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
! 45397: ** contain a value of 0.
! 45398: **
! 45399: ** To look for page P in the hash table, first compute a hash iKey on
! 45400: ** P as follows:
! 45401: **
! 45402: ** iKey = (P * 383) % HASHTABLE_NSLOT
! 45403: **
! 45404: ** Then start scanning entries of the hash table, starting with iKey
! 45405: ** (wrapping around to the beginning when the end of the hash table is
! 45406: ** reached) until an unused hash slot is found. Let the first unused slot
! 45407: ** be at index iUnused. (iUnused might be less than iKey if there was
! 45408: ** wrap-around.) Because the hash table is never more than half full,
! 45409: ** the search is guaranteed to eventually hit an unused entry. Let
! 45410: ** iMax be the value between iKey and iUnused, closest to iUnused,
! 45411: ** where aHash[iMax]==P. If there is no iMax entry (if there exists
! 45412: ** no hash slot such that aHash[i]==p) then page P is not in the
! 45413: ** current index block. Otherwise the iMax-th mapping entry of the
! 45414: ** current index block corresponds to the last entry that references
! 45415: ** page P.
! 45416: **
! 45417: ** A hash search begins with the last index block and moves toward the
! 45418: ** first index block, looking for entries corresponding to page P. On
! 45419: ** average, only two or three slots in each index block need to be
! 45420: ** examined in order to either find the last entry for page P, or to
! 45421: ** establish that no such entry exists in the block. Each index block
! 45422: ** holds over 4000 entries. So two or three index blocks are sufficient
! 45423: ** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
! 45424: ** comparisons (on average) suffice to either locate a frame in the
! 45425: ** WAL or to establish that the frame does not exist in the WAL. This
! 45426: ** is much faster than scanning the entire 10MB WAL.
! 45427: **
! 45428: ** Note that entries are added in order of increasing K. Hence, one
! 45429: ** reader might be using some value K0 and a second reader that started
! 45430: ** at a later time (after additional transactions were added to the WAL
! 45431: ** and to the wal-index) might be using a different value K1, where K1>K0.
! 45432: ** Both readers can use the same hash table and mapping section to get
! 45433: ** the correct result. There may be entries in the hash table with
! 45434: ** K>K0 but to the first reader, those entries will appear to be unused
! 45435: ** slots in the hash table and so the first reader will get an answer as
! 45436: ** if no values greater than K0 had ever been inserted into the hash table
! 45437: ** in the first place - which is what reader one wants. Meanwhile, the
! 45438: ** second reader using K1 will see additional values that were inserted
! 45439: ** later, which is exactly what reader two wants.
! 45440: **
! 45441: ** When a rollback occurs, the value of K is decreased. Hash table entries
! 45442: ** that correspond to frames greater than the new K value are removed
! 45443: ** from the hash table at this point.
! 45444: */
! 45445: #ifndef SQLITE_OMIT_WAL
! 45446:
! 45447:
! 45448: /*
! 45449: ** Trace output macros
! 45450: */
! 45451: #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
! 45452: SQLITE_PRIVATE int sqlite3WalTrace = 0;
! 45453: # define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
! 45454: #else
! 45455: # define WALTRACE(X)
! 45456: #endif
! 45457:
! 45458: /*
! 45459: ** The maximum (and only) versions of the wal and wal-index formats
! 45460: ** that may be interpreted by this version of SQLite.
! 45461: **
! 45462: ** If a client begins recovering a WAL file and finds that (a) the checksum
! 45463: ** values in the wal-header are correct and (b) the version field is not
! 45464: ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
! 45465: **
! 45466: ** Similarly, if a client successfully reads a wal-index header (i.e. the
! 45467: ** checksum test is successful) and finds that the version field is not
! 45468: ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
! 45469: ** returns SQLITE_CANTOPEN.
! 45470: */
! 45471: #define WAL_MAX_VERSION 3007000
! 45472: #define WALINDEX_MAX_VERSION 3007000
! 45473:
! 45474: /*
! 45475: ** Indices of various locking bytes. WAL_NREADER is the number
! 45476: ** of available reader locks and should be at least 3.
! 45477: */
! 45478: #define WAL_WRITE_LOCK 0
! 45479: #define WAL_ALL_BUT_WRITE 1
! 45480: #define WAL_CKPT_LOCK 1
! 45481: #define WAL_RECOVER_LOCK 2
! 45482: #define WAL_READ_LOCK(I) (3+(I))
! 45483: #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
! 45484:
! 45485:
! 45486: /* Object declarations */
! 45487: typedef struct WalIndexHdr WalIndexHdr;
! 45488: typedef struct WalIterator WalIterator;
! 45489: typedef struct WalCkptInfo WalCkptInfo;
! 45490:
! 45491:
! 45492: /*
! 45493: ** The following object holds a copy of the wal-index header content.
! 45494: **
! 45495: ** The actual header in the wal-index consists of two copies of this
! 45496: ** object.
! 45497: **
! 45498: ** The szPage value can be any power of 2 between 512 and 32768, inclusive.
! 45499: ** Or it can be 1 to represent a 65536-byte page. The latter case was
! 45500: ** added in 3.7.1 when support for 64K pages was added.
! 45501: */
! 45502: struct WalIndexHdr {
! 45503: u32 iVersion; /* Wal-index version */
! 45504: u32 unused; /* Unused (padding) field */
! 45505: u32 iChange; /* Counter incremented each transaction */
! 45506: u8 isInit; /* 1 when initialized */
! 45507: u8 bigEndCksum; /* True if checksums in WAL are big-endian */
! 45508: u16 szPage; /* Database page size in bytes. 1==64K */
! 45509: u32 mxFrame; /* Index of last valid frame in the WAL */
! 45510: u32 nPage; /* Size of database in pages */
! 45511: u32 aFrameCksum[2]; /* Checksum of last frame in log */
! 45512: u32 aSalt[2]; /* Two salt values copied from WAL header */
! 45513: u32 aCksum[2]; /* Checksum over all prior fields */
! 45514: };
! 45515:
! 45516: /*
! 45517: ** A copy of the following object occurs in the wal-index immediately
! 45518: ** following the second copy of the WalIndexHdr. This object stores
! 45519: ** information used by checkpoint.
! 45520: **
! 45521: ** nBackfill is the number of frames in the WAL that have been written
! 45522: ** back into the database. (We call the act of moving content from WAL to
! 45523: ** database "backfilling".) The nBackfill number is never greater than
! 45524: ** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
! 45525: ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
! 45526: ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
! 45527: ** mxFrame back to zero when the WAL is reset.
! 45528: **
! 45529: ** There is one entry in aReadMark[] for each reader lock. If a reader
! 45530: ** holds read-lock K, then the value in aReadMark[K] is no greater than
! 45531: ** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
! 45532: ** for any aReadMark[] means that entry is unused. aReadMark[0] is
! 45533: ** a special case; its value is never used and it exists as a place-holder
! 45534: ** to avoid having to offset aReadMark[] indexs by one. Readers holding
! 45535: ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
! 45536: ** directly from the database.
! 45537: **
! 45538: ** The value of aReadMark[K] may only be changed by a thread that
! 45539: ** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
! 45540: ** aReadMark[K] cannot changed while there is a reader is using that mark
! 45541: ** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
! 45542: **
! 45543: ** The checkpointer may only transfer frames from WAL to database where
! 45544: ** the frame numbers are less than or equal to every aReadMark[] that is
! 45545: ** in use (that is, every aReadMark[j] for which there is a corresponding
! 45546: ** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
! 45547: ** largest value and will increase an unused aReadMark[] to mxFrame if there
! 45548: ** is not already an aReadMark[] equal to mxFrame. The exception to the
! 45549: ** previous sentence is when nBackfill equals mxFrame (meaning that everything
! 45550: ** in the WAL has been backfilled into the database) then new readers
! 45551: ** will choose aReadMark[0] which has value 0 and hence such reader will
! 45552: ** get all their all content directly from the database file and ignore
! 45553: ** the WAL.
! 45554: **
! 45555: ** Writers normally append new frames to the end of the WAL. However,
! 45556: ** if nBackfill equals mxFrame (meaning that all WAL content has been
! 45557: ** written back into the database) and if no readers are using the WAL
! 45558: ** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
! 45559: ** the writer will first "reset" the WAL back to the beginning and start
! 45560: ** writing new content beginning at frame 1.
! 45561: **
! 45562: ** We assume that 32-bit loads are atomic and so no locks are needed in
! 45563: ** order to read from any aReadMark[] entries.
! 45564: */
! 45565: struct WalCkptInfo {
! 45566: u32 nBackfill; /* Number of WAL frames backfilled into DB */
! 45567: u32 aReadMark[WAL_NREADER]; /* Reader marks */
! 45568: };
! 45569: #define READMARK_NOT_USED 0xffffffff
! 45570:
! 45571:
! 45572: /* A block of WALINDEX_LOCK_RESERVED bytes beginning at
! 45573: ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
! 45574: ** only support mandatory file-locks, we do not read or write data
! 45575: ** from the region of the file on which locks are applied.
! 45576: */
! 45577: #define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
! 45578: #define WALINDEX_LOCK_RESERVED 16
! 45579: #define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
! 45580:
! 45581: /* Size of header before each frame in wal */
! 45582: #define WAL_FRAME_HDRSIZE 24
! 45583:
! 45584: /* Size of write ahead log header, including checksum. */
! 45585: /* #define WAL_HDRSIZE 24 */
! 45586: #define WAL_HDRSIZE 32
! 45587:
! 45588: /* WAL magic value. Either this value, or the same value with the least
! 45589: ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
! 45590: ** big-endian format in the first 4 bytes of a WAL file.
! 45591: **
! 45592: ** If the LSB is set, then the checksums for each frame within the WAL
! 45593: ** file are calculated by treating all data as an array of 32-bit
! 45594: ** big-endian words. Otherwise, they are calculated by interpreting
! 45595: ** all data as 32-bit little-endian words.
! 45596: */
! 45597: #define WAL_MAGIC 0x377f0682
! 45598:
! 45599: /*
! 45600: ** Return the offset of frame iFrame in the write-ahead log file,
! 45601: ** assuming a database page size of szPage bytes. The offset returned
! 45602: ** is to the start of the write-ahead log frame-header.
! 45603: */
! 45604: #define walFrameOffset(iFrame, szPage) ( \
! 45605: WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
! 45606: )
! 45607:
! 45608: /*
! 45609: ** An open write-ahead log file is represented by an instance of the
! 45610: ** following object.
! 45611: */
! 45612: struct Wal {
! 45613: sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
! 45614: sqlite3_file *pDbFd; /* File handle for the database file */
! 45615: sqlite3_file *pWalFd; /* File handle for WAL file */
! 45616: u32 iCallback; /* Value to pass to log callback (or 0) */
! 45617: i64 mxWalSize; /* Truncate WAL to this size upon reset */
! 45618: int nWiData; /* Size of array apWiData */
! 45619: int szFirstBlock; /* Size of first block written to WAL file */
! 45620: volatile u32 **apWiData; /* Pointer to wal-index content in memory */
! 45621: u32 szPage; /* Database page size */
! 45622: i16 readLock; /* Which read lock is being held. -1 for none */
! 45623: u8 syncFlags; /* Flags to use to sync header writes */
! 45624: u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
! 45625: u8 writeLock; /* True if in a write transaction */
! 45626: u8 ckptLock; /* True if holding a checkpoint lock */
! 45627: u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
! 45628: u8 truncateOnCommit; /* True to truncate WAL file on commit */
! 45629: u8 syncHeader; /* Fsync the WAL header if true */
! 45630: u8 padToSectorBoundary; /* Pad transactions out to the next sector */
! 45631: WalIndexHdr hdr; /* Wal-index header for current transaction */
! 45632: const char *zWalName; /* Name of WAL file */
! 45633: u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
! 45634: #ifdef SQLITE_DEBUG
! 45635: u8 lockError; /* True if a locking error has occurred */
! 45636: #endif
! 45637: };
! 45638:
! 45639: /*
! 45640: ** Candidate values for Wal.exclusiveMode.
! 45641: */
! 45642: #define WAL_NORMAL_MODE 0
! 45643: #define WAL_EXCLUSIVE_MODE 1
! 45644: #define WAL_HEAPMEMORY_MODE 2
! 45645:
! 45646: /*
! 45647: ** Possible values for WAL.readOnly
! 45648: */
! 45649: #define WAL_RDWR 0 /* Normal read/write connection */
! 45650: #define WAL_RDONLY 1 /* The WAL file is readonly */
! 45651: #define WAL_SHM_RDONLY 2 /* The SHM file is readonly */
! 45652:
! 45653: /*
! 45654: ** Each page of the wal-index mapping contains a hash-table made up of
! 45655: ** an array of HASHTABLE_NSLOT elements of the following type.
! 45656: */
! 45657: typedef u16 ht_slot;
! 45658:
! 45659: /*
! 45660: ** This structure is used to implement an iterator that loops through
! 45661: ** all frames in the WAL in database page order. Where two or more frames
! 45662: ** correspond to the same database page, the iterator visits only the
! 45663: ** frame most recently written to the WAL (in other words, the frame with
! 45664: ** the largest index).
! 45665: **
! 45666: ** The internals of this structure are only accessed by:
! 45667: **
! 45668: ** walIteratorInit() - Create a new iterator,
! 45669: ** walIteratorNext() - Step an iterator,
! 45670: ** walIteratorFree() - Free an iterator.
! 45671: **
! 45672: ** This functionality is used by the checkpoint code (see walCheckpoint()).
! 45673: */
! 45674: struct WalIterator {
! 45675: int iPrior; /* Last result returned from the iterator */
! 45676: int nSegment; /* Number of entries in aSegment[] */
! 45677: struct WalSegment {
! 45678: int iNext; /* Next slot in aIndex[] not yet returned */
! 45679: ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
! 45680: u32 *aPgno; /* Array of page numbers. */
! 45681: int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
! 45682: int iZero; /* Frame number associated with aPgno[0] */
! 45683: } aSegment[1]; /* One for every 32KB page in the wal-index */
! 45684: };
! 45685:
! 45686: /*
! 45687: ** Define the parameters of the hash tables in the wal-index file. There
! 45688: ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
! 45689: ** wal-index.
! 45690: **
! 45691: ** Changing any of these constants will alter the wal-index format and
! 45692: ** create incompatibilities.
! 45693: */
! 45694: #define HASHTABLE_NPAGE 4096 /* Must be power of 2 */
! 45695: #define HASHTABLE_HASH_1 383 /* Should be prime */
! 45696: #define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */
! 45697:
! 45698: /*
! 45699: ** The block of page numbers associated with the first hash-table in a
! 45700: ** wal-index is smaller than usual. This is so that there is a complete
! 45701: ** hash-table on each aligned 32KB page of the wal-index.
! 45702: */
! 45703: #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
! 45704:
! 45705: /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
! 45706: #define WALINDEX_PGSZ ( \
! 45707: sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
! 45708: )
! 45709:
! 45710: /*
! 45711: ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
! 45712: ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
! 45713: ** numbered from zero.
! 45714: **
! 45715: ** If this call is successful, *ppPage is set to point to the wal-index
! 45716: ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
! 45717: ** then an SQLite error code is returned and *ppPage is set to 0.
! 45718: */
! 45719: static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
! 45720: int rc = SQLITE_OK;
! 45721:
! 45722: /* Enlarge the pWal->apWiData[] array if required */
! 45723: if( pWal->nWiData<=iPage ){
! 45724: int nByte = sizeof(u32*)*(iPage+1);
! 45725: volatile u32 **apNew;
! 45726: apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
! 45727: if( !apNew ){
! 45728: *ppPage = 0;
! 45729: return SQLITE_NOMEM;
! 45730: }
! 45731: memset((void*)&apNew[pWal->nWiData], 0,
! 45732: sizeof(u32*)*(iPage+1-pWal->nWiData));
! 45733: pWal->apWiData = apNew;
! 45734: pWal->nWiData = iPage+1;
! 45735: }
! 45736:
! 45737: /* Request a pointer to the required page from the VFS */
! 45738: if( pWal->apWiData[iPage]==0 ){
! 45739: if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
! 45740: pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
! 45741: if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
! 45742: }else{
! 45743: rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
! 45744: pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
! 45745: );
! 45746: if( rc==SQLITE_READONLY ){
! 45747: pWal->readOnly |= WAL_SHM_RDONLY;
! 45748: rc = SQLITE_OK;
! 45749: }
! 45750: }
! 45751: }
! 45752:
! 45753: *ppPage = pWal->apWiData[iPage];
! 45754: assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
! 45755: return rc;
! 45756: }
! 45757:
! 45758: /*
! 45759: ** Return a pointer to the WalCkptInfo structure in the wal-index.
! 45760: */
! 45761: static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
! 45762: assert( pWal->nWiData>0 && pWal->apWiData[0] );
! 45763: return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
! 45764: }
! 45765:
! 45766: /*
! 45767: ** Return a pointer to the WalIndexHdr structure in the wal-index.
! 45768: */
! 45769: static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
! 45770: assert( pWal->nWiData>0 && pWal->apWiData[0] );
! 45771: return (volatile WalIndexHdr*)pWal->apWiData[0];
! 45772: }
! 45773:
! 45774: /*
! 45775: ** The argument to this macro must be of type u32. On a little-endian
! 45776: ** architecture, it returns the u32 value that results from interpreting
! 45777: ** the 4 bytes as a big-endian value. On a big-endian architecture, it
! 45778: ** returns the value that would be produced by intepreting the 4 bytes
! 45779: ** of the input value as a little-endian integer.
! 45780: */
! 45781: #define BYTESWAP32(x) ( \
! 45782: (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
! 45783: + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
! 45784: )
! 45785:
! 45786: /*
! 45787: ** Generate or extend an 8 byte checksum based on the data in
! 45788: ** array aByte[] and the initial values of aIn[0] and aIn[1] (or
! 45789: ** initial values of 0 and 0 if aIn==NULL).
! 45790: **
! 45791: ** The checksum is written back into aOut[] before returning.
! 45792: **
! 45793: ** nByte must be a positive multiple of 8.
! 45794: */
! 45795: static void walChecksumBytes(
! 45796: int nativeCksum, /* True for native byte-order, false for non-native */
! 45797: u8 *a, /* Content to be checksummed */
! 45798: int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
! 45799: const u32 *aIn, /* Initial checksum value input */
! 45800: u32 *aOut /* OUT: Final checksum value output */
! 45801: ){
! 45802: u32 s1, s2;
! 45803: u32 *aData = (u32 *)a;
! 45804: u32 *aEnd = (u32 *)&a[nByte];
! 45805:
! 45806: if( aIn ){
! 45807: s1 = aIn[0];
! 45808: s2 = aIn[1];
! 45809: }else{
! 45810: s1 = s2 = 0;
! 45811: }
! 45812:
! 45813: assert( nByte>=8 );
! 45814: assert( (nByte&0x00000007)==0 );
! 45815:
! 45816: if( nativeCksum ){
! 45817: do {
! 45818: s1 += *aData++ + s2;
! 45819: s2 += *aData++ + s1;
! 45820: }while( aData<aEnd );
! 45821: }else{
! 45822: do {
! 45823: s1 += BYTESWAP32(aData[0]) + s2;
! 45824: s2 += BYTESWAP32(aData[1]) + s1;
! 45825: aData += 2;
! 45826: }while( aData<aEnd );
! 45827: }
! 45828:
! 45829: aOut[0] = s1;
! 45830: aOut[1] = s2;
! 45831: }
! 45832:
! 45833: static void walShmBarrier(Wal *pWal){
! 45834: if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
! 45835: sqlite3OsShmBarrier(pWal->pDbFd);
! 45836: }
! 45837: }
! 45838:
! 45839: /*
! 45840: ** Write the header information in pWal->hdr into the wal-index.
! 45841: **
! 45842: ** The checksum on pWal->hdr is updated before it is written.
! 45843: */
! 45844: static void walIndexWriteHdr(Wal *pWal){
! 45845: volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
! 45846: const int nCksum = offsetof(WalIndexHdr, aCksum);
! 45847:
! 45848: assert( pWal->writeLock );
! 45849: pWal->hdr.isInit = 1;
! 45850: pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
! 45851: walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
! 45852: memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
! 45853: walShmBarrier(pWal);
! 45854: memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
! 45855: }
! 45856:
! 45857: /*
! 45858: ** This function encodes a single frame header and writes it to a buffer
! 45859: ** supplied by the caller. A frame-header is made up of a series of
! 45860: ** 4-byte big-endian integers, as follows:
! 45861: **
! 45862: ** 0: Page number.
! 45863: ** 4: For commit records, the size of the database image in pages
! 45864: ** after the commit. For all other records, zero.
! 45865: ** 8: Salt-1 (copied from the wal-header)
! 45866: ** 12: Salt-2 (copied from the wal-header)
! 45867: ** 16: Checksum-1.
! 45868: ** 20: Checksum-2.
! 45869: */
! 45870: static void walEncodeFrame(
! 45871: Wal *pWal, /* The write-ahead log */
! 45872: u32 iPage, /* Database page number for frame */
! 45873: u32 nTruncate, /* New db size (or 0 for non-commit frames) */
! 45874: u8 *aData, /* Pointer to page data */
! 45875: u8 *aFrame /* OUT: Write encoded frame here */
! 45876: ){
! 45877: int nativeCksum; /* True for native byte-order checksums */
! 45878: u32 *aCksum = pWal->hdr.aFrameCksum;
! 45879: assert( WAL_FRAME_HDRSIZE==24 );
! 45880: sqlite3Put4byte(&aFrame[0], iPage);
! 45881: sqlite3Put4byte(&aFrame[4], nTruncate);
! 45882: memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
! 45883:
! 45884: nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
! 45885: walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
! 45886: walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
! 45887:
! 45888: sqlite3Put4byte(&aFrame[16], aCksum[0]);
! 45889: sqlite3Put4byte(&aFrame[20], aCksum[1]);
! 45890: }
! 45891:
! 45892: /*
! 45893: ** Check to see if the frame with header in aFrame[] and content
! 45894: ** in aData[] is valid. If it is a valid frame, fill *piPage and
! 45895: ** *pnTruncate and return true. Return if the frame is not valid.
! 45896: */
! 45897: static int walDecodeFrame(
! 45898: Wal *pWal, /* The write-ahead log */
! 45899: u32 *piPage, /* OUT: Database page number for frame */
! 45900: u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
! 45901: u8 *aData, /* Pointer to page data (for checksum) */
! 45902: u8 *aFrame /* Frame data */
! 45903: ){
! 45904: int nativeCksum; /* True for native byte-order checksums */
! 45905: u32 *aCksum = pWal->hdr.aFrameCksum;
! 45906: u32 pgno; /* Page number of the frame */
! 45907: assert( WAL_FRAME_HDRSIZE==24 );
! 45908:
! 45909: /* A frame is only valid if the salt values in the frame-header
! 45910: ** match the salt values in the wal-header.
! 45911: */
! 45912: if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
! 45913: return 0;
! 45914: }
! 45915:
! 45916: /* A frame is only valid if the page number is creater than zero.
! 45917: */
! 45918: pgno = sqlite3Get4byte(&aFrame[0]);
! 45919: if( pgno==0 ){
! 45920: return 0;
! 45921: }
! 45922:
! 45923: /* A frame is only valid if a checksum of the WAL header,
! 45924: ** all prior frams, the first 16 bytes of this frame-header,
! 45925: ** and the frame-data matches the checksum in the last 8
! 45926: ** bytes of this frame-header.
! 45927: */
! 45928: nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
! 45929: walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
! 45930: walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
! 45931: if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
! 45932: || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
! 45933: ){
! 45934: /* Checksum failed. */
! 45935: return 0;
! 45936: }
! 45937:
! 45938: /* If we reach this point, the frame is valid. Return the page number
! 45939: ** and the new database size.
! 45940: */
! 45941: *piPage = pgno;
! 45942: *pnTruncate = sqlite3Get4byte(&aFrame[4]);
! 45943: return 1;
! 45944: }
! 45945:
! 45946:
! 45947: #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
! 45948: /*
! 45949: ** Names of locks. This routine is used to provide debugging output and is not
! 45950: ** a part of an ordinary build.
! 45951: */
! 45952: static const char *walLockName(int lockIdx){
! 45953: if( lockIdx==WAL_WRITE_LOCK ){
! 45954: return "WRITE-LOCK";
! 45955: }else if( lockIdx==WAL_CKPT_LOCK ){
! 45956: return "CKPT-LOCK";
! 45957: }else if( lockIdx==WAL_RECOVER_LOCK ){
! 45958: return "RECOVER-LOCK";
! 45959: }else{
! 45960: static char zName[15];
! 45961: sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
! 45962: lockIdx-WAL_READ_LOCK(0));
! 45963: return zName;
! 45964: }
! 45965: }
! 45966: #endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
! 45967:
! 45968:
! 45969: /*
! 45970: ** Set or release locks on the WAL. Locks are either shared or exclusive.
! 45971: ** A lock cannot be moved directly between shared and exclusive - it must go
! 45972: ** through the unlocked state first.
! 45973: **
! 45974: ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
! 45975: */
! 45976: static int walLockShared(Wal *pWal, int lockIdx){
! 45977: int rc;
! 45978: if( pWal->exclusiveMode ) return SQLITE_OK;
! 45979: rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
! 45980: SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
! 45981: WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
! 45982: walLockName(lockIdx), rc ? "failed" : "ok"));
! 45983: VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
! 45984: return rc;
! 45985: }
! 45986: static void walUnlockShared(Wal *pWal, int lockIdx){
! 45987: if( pWal->exclusiveMode ) return;
! 45988: (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
! 45989: SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
! 45990: WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
! 45991: }
! 45992: static int walLockExclusive(Wal *pWal, int lockIdx, int n){
! 45993: int rc;
! 45994: if( pWal->exclusiveMode ) return SQLITE_OK;
! 45995: rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
! 45996: SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
! 45997: WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
! 45998: walLockName(lockIdx), n, rc ? "failed" : "ok"));
! 45999: VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
! 46000: return rc;
! 46001: }
! 46002: static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
! 46003: if( pWal->exclusiveMode ) return;
! 46004: (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
! 46005: SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
! 46006: WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
! 46007: walLockName(lockIdx), n));
! 46008: }
! 46009:
! 46010: /*
! 46011: ** Compute a hash on a page number. The resulting hash value must land
! 46012: ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
! 46013: ** the hash to the next value in the event of a collision.
! 46014: */
! 46015: static int walHash(u32 iPage){
! 46016: assert( iPage>0 );
! 46017: assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
! 46018: return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
! 46019: }
! 46020: static int walNextHash(int iPriorHash){
! 46021: return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
! 46022: }
! 46023:
! 46024: /*
! 46025: ** Return pointers to the hash table and page number array stored on
! 46026: ** page iHash of the wal-index. The wal-index is broken into 32KB pages
! 46027: ** numbered starting from 0.
! 46028: **
! 46029: ** Set output variable *paHash to point to the start of the hash table
! 46030: ** in the wal-index file. Set *piZero to one less than the frame
! 46031: ** number of the first frame indexed by this hash table. If a
! 46032: ** slot in the hash table is set to N, it refers to frame number
! 46033: ** (*piZero+N) in the log.
! 46034: **
! 46035: ** Finally, set *paPgno so that *paPgno[1] is the page number of the
! 46036: ** first frame indexed by the hash table, frame (*piZero+1).
! 46037: */
! 46038: static int walHashGet(
! 46039: Wal *pWal, /* WAL handle */
! 46040: int iHash, /* Find the iHash'th table */
! 46041: volatile ht_slot **paHash, /* OUT: Pointer to hash index */
! 46042: volatile u32 **paPgno, /* OUT: Pointer to page number array */
! 46043: u32 *piZero /* OUT: Frame associated with *paPgno[0] */
! 46044: ){
! 46045: int rc; /* Return code */
! 46046: volatile u32 *aPgno;
! 46047:
! 46048: rc = walIndexPage(pWal, iHash, &aPgno);
! 46049: assert( rc==SQLITE_OK || iHash>0 );
! 46050:
! 46051: if( rc==SQLITE_OK ){
! 46052: u32 iZero;
! 46053: volatile ht_slot *aHash;
! 46054:
! 46055: aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
! 46056: if( iHash==0 ){
! 46057: aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
! 46058: iZero = 0;
! 46059: }else{
! 46060: iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
! 46061: }
! 46062:
! 46063: *paPgno = &aPgno[-1];
! 46064: *paHash = aHash;
! 46065: *piZero = iZero;
! 46066: }
! 46067: return rc;
! 46068: }
! 46069:
! 46070: /*
! 46071: ** Return the number of the wal-index page that contains the hash-table
! 46072: ** and page-number array that contain entries corresponding to WAL frame
! 46073: ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
! 46074: ** are numbered starting from 0.
! 46075: */
! 46076: static int walFramePage(u32 iFrame){
! 46077: int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
! 46078: assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
! 46079: && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
! 46080: && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
! 46081: && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
! 46082: && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
! 46083: );
! 46084: return iHash;
! 46085: }
! 46086:
! 46087: /*
! 46088: ** Return the page number associated with frame iFrame in this WAL.
! 46089: */
! 46090: static u32 walFramePgno(Wal *pWal, u32 iFrame){
! 46091: int iHash = walFramePage(iFrame);
! 46092: if( iHash==0 ){
! 46093: return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
! 46094: }
! 46095: return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
! 46096: }
! 46097:
! 46098: /*
! 46099: ** Remove entries from the hash table that point to WAL slots greater
! 46100: ** than pWal->hdr.mxFrame.
! 46101: **
! 46102: ** This function is called whenever pWal->hdr.mxFrame is decreased due
! 46103: ** to a rollback or savepoint.
! 46104: **
! 46105: ** At most only the hash table containing pWal->hdr.mxFrame needs to be
! 46106: ** updated. Any later hash tables will be automatically cleared when
! 46107: ** pWal->hdr.mxFrame advances to the point where those hash tables are
! 46108: ** actually needed.
! 46109: */
! 46110: static void walCleanupHash(Wal *pWal){
! 46111: volatile ht_slot *aHash = 0; /* Pointer to hash table to clear */
! 46112: volatile u32 *aPgno = 0; /* Page number array for hash table */
! 46113: u32 iZero = 0; /* frame == (aHash[x]+iZero) */
! 46114: int iLimit = 0; /* Zero values greater than this */
! 46115: int nByte; /* Number of bytes to zero in aPgno[] */
! 46116: int i; /* Used to iterate through aHash[] */
! 46117:
! 46118: assert( pWal->writeLock );
! 46119: testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
! 46120: testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
! 46121: testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
! 46122:
! 46123: if( pWal->hdr.mxFrame==0 ) return;
! 46124:
! 46125: /* Obtain pointers to the hash-table and page-number array containing
! 46126: ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
! 46127: ** that the page said hash-table and array reside on is already mapped.
! 46128: */
! 46129: assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
! 46130: assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
! 46131: walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
! 46132:
! 46133: /* Zero all hash-table entries that correspond to frame numbers greater
! 46134: ** than pWal->hdr.mxFrame.
! 46135: */
! 46136: iLimit = pWal->hdr.mxFrame - iZero;
! 46137: assert( iLimit>0 );
! 46138: for(i=0; i<HASHTABLE_NSLOT; i++){
! 46139: if( aHash[i]>iLimit ){
! 46140: aHash[i] = 0;
! 46141: }
! 46142: }
! 46143:
! 46144: /* Zero the entries in the aPgno array that correspond to frames with
! 46145: ** frame numbers greater than pWal->hdr.mxFrame.
! 46146: */
! 46147: nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
! 46148: memset((void *)&aPgno[iLimit+1], 0, nByte);
! 46149:
! 46150: #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
! 46151: /* Verify that the every entry in the mapping region is still reachable
! 46152: ** via the hash table even after the cleanup.
! 46153: */
! 46154: if( iLimit ){
! 46155: int i; /* Loop counter */
! 46156: int iKey; /* Hash key */
! 46157: for(i=1; i<=iLimit; i++){
! 46158: for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
! 46159: if( aHash[iKey]==i ) break;
! 46160: }
! 46161: assert( aHash[iKey]==i );
! 46162: }
! 46163: }
! 46164: #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
! 46165: }
! 46166:
! 46167:
! 46168: /*
! 46169: ** Set an entry in the wal-index that will map database page number
! 46170: ** pPage into WAL frame iFrame.
! 46171: */
! 46172: static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
! 46173: int rc; /* Return code */
! 46174: u32 iZero = 0; /* One less than frame number of aPgno[1] */
! 46175: volatile u32 *aPgno = 0; /* Page number array */
! 46176: volatile ht_slot *aHash = 0; /* Hash table */
! 46177:
! 46178: rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
! 46179:
! 46180: /* Assuming the wal-index file was successfully mapped, populate the
! 46181: ** page number array and hash table entry.
! 46182: */
! 46183: if( rc==SQLITE_OK ){
! 46184: int iKey; /* Hash table key */
! 46185: int idx; /* Value to write to hash-table slot */
! 46186: int nCollide; /* Number of hash collisions */
! 46187:
! 46188: idx = iFrame - iZero;
! 46189: assert( idx <= HASHTABLE_NSLOT/2 + 1 );
! 46190:
! 46191: /* If this is the first entry to be added to this hash-table, zero the
! 46192: ** entire hash table and aPgno[] array before proceding.
! 46193: */
! 46194: if( idx==1 ){
! 46195: int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
! 46196: memset((void*)&aPgno[1], 0, nByte);
! 46197: }
! 46198:
! 46199: /* If the entry in aPgno[] is already set, then the previous writer
! 46200: ** must have exited unexpectedly in the middle of a transaction (after
! 46201: ** writing one or more dirty pages to the WAL to free up memory).
! 46202: ** Remove the remnants of that writers uncommitted transaction from
! 46203: ** the hash-table before writing any new entries.
! 46204: */
! 46205: if( aPgno[idx] ){
! 46206: walCleanupHash(pWal);
! 46207: assert( !aPgno[idx] );
! 46208: }
! 46209:
! 46210: /* Write the aPgno[] array entry and the hash-table slot. */
! 46211: nCollide = idx;
! 46212: for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
! 46213: if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
! 46214: }
! 46215: aPgno[idx] = iPage;
! 46216: aHash[iKey] = (ht_slot)idx;
! 46217:
! 46218: #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
! 46219: /* Verify that the number of entries in the hash table exactly equals
! 46220: ** the number of entries in the mapping region.
! 46221: */
! 46222: {
! 46223: int i; /* Loop counter */
! 46224: int nEntry = 0; /* Number of entries in the hash table */
! 46225: for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
! 46226: assert( nEntry==idx );
! 46227: }
! 46228:
! 46229: /* Verify that the every entry in the mapping region is reachable
! 46230: ** via the hash table. This turns out to be a really, really expensive
! 46231: ** thing to check, so only do this occasionally - not on every
! 46232: ** iteration.
! 46233: */
! 46234: if( (idx&0x3ff)==0 ){
! 46235: int i; /* Loop counter */
! 46236: for(i=1; i<=idx; i++){
! 46237: for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
! 46238: if( aHash[iKey]==i ) break;
! 46239: }
! 46240: assert( aHash[iKey]==i );
! 46241: }
! 46242: }
! 46243: #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
! 46244: }
! 46245:
! 46246:
! 46247: return rc;
! 46248: }
! 46249:
! 46250:
! 46251: /*
! 46252: ** Recover the wal-index by reading the write-ahead log file.
! 46253: **
! 46254: ** This routine first tries to establish an exclusive lock on the
! 46255: ** wal-index to prevent other threads/processes from doing anything
! 46256: ** with the WAL or wal-index while recovery is running. The
! 46257: ** WAL_RECOVER_LOCK is also held so that other threads will know
! 46258: ** that this thread is running recovery. If unable to establish
! 46259: ** the necessary locks, this routine returns SQLITE_BUSY.
! 46260: */
! 46261: static int walIndexRecover(Wal *pWal){
! 46262: int rc; /* Return Code */
! 46263: i64 nSize; /* Size of log file */
! 46264: u32 aFrameCksum[2] = {0, 0};
! 46265: int iLock; /* Lock offset to lock for checkpoint */
! 46266: int nLock; /* Number of locks to hold */
! 46267:
! 46268: /* Obtain an exclusive lock on all byte in the locking range not already
! 46269: ** locked by the caller. The caller is guaranteed to have locked the
! 46270: ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
! 46271: ** If successful, the same bytes that are locked here are unlocked before
! 46272: ** this function returns.
! 46273: */
! 46274: assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
! 46275: assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
! 46276: assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
! 46277: assert( pWal->writeLock );
! 46278: iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
! 46279: nLock = SQLITE_SHM_NLOCK - iLock;
! 46280: rc = walLockExclusive(pWal, iLock, nLock);
! 46281: if( rc ){
! 46282: return rc;
! 46283: }
! 46284: WALTRACE(("WAL%p: recovery begin...\n", pWal));
! 46285:
! 46286: memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
! 46287:
! 46288: rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
! 46289: if( rc!=SQLITE_OK ){
! 46290: goto recovery_error;
! 46291: }
! 46292:
! 46293: if( nSize>WAL_HDRSIZE ){
! 46294: u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
! 46295: u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
! 46296: int szFrame; /* Number of bytes in buffer aFrame[] */
! 46297: u8 *aData; /* Pointer to data part of aFrame buffer */
! 46298: int iFrame; /* Index of last frame read */
! 46299: i64 iOffset; /* Next offset to read from log file */
! 46300: int szPage; /* Page size according to the log */
! 46301: u32 magic; /* Magic value read from WAL header */
! 46302: u32 version; /* Magic value read from WAL header */
! 46303: int isValid; /* True if this frame is valid */
! 46304:
! 46305: /* Read in the WAL header. */
! 46306: rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
! 46307: if( rc!=SQLITE_OK ){
! 46308: goto recovery_error;
! 46309: }
! 46310:
! 46311: /* If the database page size is not a power of two, or is greater than
! 46312: ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
! 46313: ** data. Similarly, if the 'magic' value is invalid, ignore the whole
! 46314: ** WAL file.
! 46315: */
! 46316: magic = sqlite3Get4byte(&aBuf[0]);
! 46317: szPage = sqlite3Get4byte(&aBuf[8]);
! 46318: if( (magic&0xFFFFFFFE)!=WAL_MAGIC
! 46319: || szPage&(szPage-1)
! 46320: || szPage>SQLITE_MAX_PAGE_SIZE
! 46321: || szPage<512
! 46322: ){
! 46323: goto finished;
! 46324: }
! 46325: pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
! 46326: pWal->szPage = szPage;
! 46327: pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
! 46328: memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
! 46329:
! 46330: /* Verify that the WAL header checksum is correct */
! 46331: walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
! 46332: aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
! 46333: );
! 46334: if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
! 46335: || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
! 46336: ){
! 46337: goto finished;
! 46338: }
! 46339:
! 46340: /* Verify that the version number on the WAL format is one that
! 46341: ** are able to understand */
! 46342: version = sqlite3Get4byte(&aBuf[4]);
! 46343: if( version!=WAL_MAX_VERSION ){
! 46344: rc = SQLITE_CANTOPEN_BKPT;
! 46345: goto finished;
! 46346: }
! 46347:
! 46348: /* Malloc a buffer to read frames into. */
! 46349: szFrame = szPage + WAL_FRAME_HDRSIZE;
! 46350: aFrame = (u8 *)sqlite3_malloc(szFrame);
! 46351: if( !aFrame ){
! 46352: rc = SQLITE_NOMEM;
! 46353: goto recovery_error;
! 46354: }
! 46355: aData = &aFrame[WAL_FRAME_HDRSIZE];
! 46356:
! 46357: /* Read all frames from the log file. */
! 46358: iFrame = 0;
! 46359: for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
! 46360: u32 pgno; /* Database page number for frame */
! 46361: u32 nTruncate; /* dbsize field from frame header */
! 46362:
! 46363: /* Read and decode the next log frame. */
! 46364: iFrame++;
! 46365: rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
! 46366: if( rc!=SQLITE_OK ) break;
! 46367: isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
! 46368: if( !isValid ) break;
! 46369: rc = walIndexAppend(pWal, iFrame, pgno);
! 46370: if( rc!=SQLITE_OK ) break;
! 46371:
! 46372: /* If nTruncate is non-zero, this is a commit record. */
! 46373: if( nTruncate ){
! 46374: pWal->hdr.mxFrame = iFrame;
! 46375: pWal->hdr.nPage = nTruncate;
! 46376: pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
! 46377: testcase( szPage<=32768 );
! 46378: testcase( szPage>=65536 );
! 46379: aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
! 46380: aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
! 46381: }
! 46382: }
! 46383:
! 46384: sqlite3_free(aFrame);
! 46385: }
! 46386:
! 46387: finished:
! 46388: if( rc==SQLITE_OK ){
! 46389: volatile WalCkptInfo *pInfo;
! 46390: int i;
! 46391: pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
! 46392: pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
! 46393: walIndexWriteHdr(pWal);
! 46394:
! 46395: /* Reset the checkpoint-header. This is safe because this thread is
! 46396: ** currently holding locks that exclude all other readers, writers and
! 46397: ** checkpointers.
! 46398: */
! 46399: pInfo = walCkptInfo(pWal);
! 46400: pInfo->nBackfill = 0;
! 46401: pInfo->aReadMark[0] = 0;
! 46402: for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
! 46403:
! 46404: /* If more than one frame was recovered from the log file, report an
! 46405: ** event via sqlite3_log(). This is to help with identifying performance
! 46406: ** problems caused by applications routinely shutting down without
! 46407: ** checkpointing the log file.
! 46408: */
! 46409: if( pWal->hdr.nPage ){
! 46410: sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s",
! 46411: pWal->hdr.nPage, pWal->zWalName
! 46412: );
! 46413: }
! 46414: }
! 46415:
! 46416: recovery_error:
! 46417: WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
! 46418: walUnlockExclusive(pWal, iLock, nLock);
! 46419: return rc;
! 46420: }
! 46421:
! 46422: /*
! 46423: ** Close an open wal-index.
! 46424: */
! 46425: static void walIndexClose(Wal *pWal, int isDelete){
! 46426: if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
! 46427: int i;
! 46428: for(i=0; i<pWal->nWiData; i++){
! 46429: sqlite3_free((void *)pWal->apWiData[i]);
! 46430: pWal->apWiData[i] = 0;
! 46431: }
! 46432: }else{
! 46433: sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
! 46434: }
! 46435: }
! 46436:
! 46437: /*
! 46438: ** Open a connection to the WAL file zWalName. The database file must
! 46439: ** already be opened on connection pDbFd. The buffer that zWalName points
! 46440: ** to must remain valid for the lifetime of the returned Wal* handle.
! 46441: **
! 46442: ** A SHARED lock should be held on the database file when this function
! 46443: ** is called. The purpose of this SHARED lock is to prevent any other
! 46444: ** client from unlinking the WAL or wal-index file. If another process
! 46445: ** were to do this just after this client opened one of these files, the
! 46446: ** system would be badly broken.
! 46447: **
! 46448: ** If the log file is successfully opened, SQLITE_OK is returned and
! 46449: ** *ppWal is set to point to a new WAL handle. If an error occurs,
! 46450: ** an SQLite error code is returned and *ppWal is left unmodified.
! 46451: */
! 46452: SQLITE_PRIVATE int sqlite3WalOpen(
! 46453: sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
! 46454: sqlite3_file *pDbFd, /* The open database file */
! 46455: const char *zWalName, /* Name of the WAL file */
! 46456: int bNoShm, /* True to run in heap-memory mode */
! 46457: i64 mxWalSize, /* Truncate WAL to this size on reset */
! 46458: Wal **ppWal /* OUT: Allocated Wal handle */
! 46459: ){
! 46460: int rc; /* Return Code */
! 46461: Wal *pRet; /* Object to allocate and return */
! 46462: int flags; /* Flags passed to OsOpen() */
! 46463:
! 46464: assert( zWalName && zWalName[0] );
! 46465: assert( pDbFd );
! 46466:
! 46467: /* In the amalgamation, the os_unix.c and os_win.c source files come before
! 46468: ** this source file. Verify that the #defines of the locking byte offsets
! 46469: ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
! 46470: */
! 46471: #ifdef WIN_SHM_BASE
! 46472: assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
! 46473: #endif
! 46474: #ifdef UNIX_SHM_BASE
! 46475: assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
! 46476: #endif
! 46477:
! 46478:
! 46479: /* Allocate an instance of struct Wal to return. */
! 46480: *ppWal = 0;
! 46481: pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
! 46482: if( !pRet ){
! 46483: return SQLITE_NOMEM;
! 46484: }
! 46485:
! 46486: pRet->pVfs = pVfs;
! 46487: pRet->pWalFd = (sqlite3_file *)&pRet[1];
! 46488: pRet->pDbFd = pDbFd;
! 46489: pRet->readLock = -1;
! 46490: pRet->mxWalSize = mxWalSize;
! 46491: pRet->zWalName = zWalName;
! 46492: pRet->syncHeader = 1;
! 46493: pRet->padToSectorBoundary = 1;
! 46494: pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
! 46495:
! 46496: /* Open file handle on the write-ahead log file. */
! 46497: flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
! 46498: rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
! 46499: if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
! 46500: pRet->readOnly = WAL_RDONLY;
! 46501: }
! 46502:
! 46503: if( rc!=SQLITE_OK ){
! 46504: walIndexClose(pRet, 0);
! 46505: sqlite3OsClose(pRet->pWalFd);
! 46506: sqlite3_free(pRet);
! 46507: }else{
! 46508: int iDC = sqlite3OsDeviceCharacteristics(pRet->pWalFd);
! 46509: if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
! 46510: if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
! 46511: pRet->padToSectorBoundary = 0;
! 46512: }
! 46513: *ppWal = pRet;
! 46514: WALTRACE(("WAL%d: opened\n", pRet));
! 46515: }
! 46516: return rc;
! 46517: }
! 46518:
! 46519: /*
! 46520: ** Change the size to which the WAL file is trucated on each reset.
! 46521: */
! 46522: SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
! 46523: if( pWal ) pWal->mxWalSize = iLimit;
! 46524: }
! 46525:
! 46526: /*
! 46527: ** Find the smallest page number out of all pages held in the WAL that
! 46528: ** has not been returned by any prior invocation of this method on the
! 46529: ** same WalIterator object. Write into *piFrame the frame index where
! 46530: ** that page was last written into the WAL. Write into *piPage the page
! 46531: ** number.
! 46532: **
! 46533: ** Return 0 on success. If there are no pages in the WAL with a page
! 46534: ** number larger than *piPage, then return 1.
! 46535: */
! 46536: static int walIteratorNext(
! 46537: WalIterator *p, /* Iterator */
! 46538: u32 *piPage, /* OUT: The page number of the next page */
! 46539: u32 *piFrame /* OUT: Wal frame index of next page */
! 46540: ){
! 46541: u32 iMin; /* Result pgno must be greater than iMin */
! 46542: u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
! 46543: int i; /* For looping through segments */
! 46544:
! 46545: iMin = p->iPrior;
! 46546: assert( iMin<0xffffffff );
! 46547: for(i=p->nSegment-1; i>=0; i--){
! 46548: struct WalSegment *pSegment = &p->aSegment[i];
! 46549: while( pSegment->iNext<pSegment->nEntry ){
! 46550: u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
! 46551: if( iPg>iMin ){
! 46552: if( iPg<iRet ){
! 46553: iRet = iPg;
! 46554: *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
! 46555: }
! 46556: break;
! 46557: }
! 46558: pSegment->iNext++;
! 46559: }
! 46560: }
! 46561:
! 46562: *piPage = p->iPrior = iRet;
! 46563: return (iRet==0xFFFFFFFF);
! 46564: }
! 46565:
! 46566: /*
! 46567: ** This function merges two sorted lists into a single sorted list.
! 46568: **
! 46569: ** aLeft[] and aRight[] are arrays of indices. The sort key is
! 46570: ** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following
! 46571: ** is guaranteed for all J<K:
! 46572: **
! 46573: ** aContent[aLeft[J]] < aContent[aLeft[K]]
! 46574: ** aContent[aRight[J]] < aContent[aRight[K]]
! 46575: **
! 46576: ** This routine overwrites aRight[] with a new (probably longer) sequence
! 46577: ** of indices such that the aRight[] contains every index that appears in
! 46578: ** either aLeft[] or the old aRight[] and such that the second condition
! 46579: ** above is still met.
! 46580: **
! 46581: ** The aContent[aLeft[X]] values will be unique for all X. And the
! 46582: ** aContent[aRight[X]] values will be unique too. But there might be
! 46583: ** one or more combinations of X and Y such that
! 46584: **
! 46585: ** aLeft[X]!=aRight[Y] && aContent[aLeft[X]] == aContent[aRight[Y]]
! 46586: **
! 46587: ** When that happens, omit the aLeft[X] and use the aRight[Y] index.
! 46588: */
! 46589: static void walMerge(
! 46590: const u32 *aContent, /* Pages in wal - keys for the sort */
! 46591: ht_slot *aLeft, /* IN: Left hand input list */
! 46592: int nLeft, /* IN: Elements in array *paLeft */
! 46593: ht_slot **paRight, /* IN/OUT: Right hand input list */
! 46594: int *pnRight, /* IN/OUT: Elements in *paRight */
! 46595: ht_slot *aTmp /* Temporary buffer */
! 46596: ){
! 46597: int iLeft = 0; /* Current index in aLeft */
! 46598: int iRight = 0; /* Current index in aRight */
! 46599: int iOut = 0; /* Current index in output buffer */
! 46600: int nRight = *pnRight;
! 46601: ht_slot *aRight = *paRight;
! 46602:
! 46603: assert( nLeft>0 && nRight>0 );
! 46604: while( iRight<nRight || iLeft<nLeft ){
! 46605: ht_slot logpage;
! 46606: Pgno dbpage;
! 46607:
! 46608: if( (iLeft<nLeft)
! 46609: && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
! 46610: ){
! 46611: logpage = aLeft[iLeft++];
! 46612: }else{
! 46613: logpage = aRight[iRight++];
! 46614: }
! 46615: dbpage = aContent[logpage];
! 46616:
! 46617: aTmp[iOut++] = logpage;
! 46618: if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
! 46619:
! 46620: assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
! 46621: assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
! 46622: }
! 46623:
! 46624: *paRight = aLeft;
! 46625: *pnRight = iOut;
! 46626: memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
! 46627: }
! 46628:
! 46629: /*
! 46630: ** Sort the elements in list aList using aContent[] as the sort key.
! 46631: ** Remove elements with duplicate keys, preferring to keep the
! 46632: ** larger aList[] values.
! 46633: **
! 46634: ** The aList[] entries are indices into aContent[]. The values in
! 46635: ** aList[] are to be sorted so that for all J<K:
! 46636: **
! 46637: ** aContent[aList[J]] < aContent[aList[K]]
! 46638: **
! 46639: ** For any X and Y such that
! 46640: **
! 46641: ** aContent[aList[X]] == aContent[aList[Y]]
! 46642: **
! 46643: ** Keep the larger of the two values aList[X] and aList[Y] and discard
! 46644: ** the smaller.
! 46645: */
! 46646: static void walMergesort(
! 46647: const u32 *aContent, /* Pages in wal */
! 46648: ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
! 46649: ht_slot *aList, /* IN/OUT: List to sort */
! 46650: int *pnList /* IN/OUT: Number of elements in aList[] */
! 46651: ){
! 46652: struct Sublist {
! 46653: int nList; /* Number of elements in aList */
! 46654: ht_slot *aList; /* Pointer to sub-list content */
! 46655: };
! 46656:
! 46657: const int nList = *pnList; /* Size of input list */
! 46658: int nMerge = 0; /* Number of elements in list aMerge */
! 46659: ht_slot *aMerge = 0; /* List to be merged */
! 46660: int iList; /* Index into input list */
! 46661: int iSub = 0; /* Index into aSub array */
! 46662: struct Sublist aSub[13]; /* Array of sub-lists */
! 46663:
! 46664: memset(aSub, 0, sizeof(aSub));
! 46665: assert( nList<=HASHTABLE_NPAGE && nList>0 );
! 46666: assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
! 46667:
! 46668: for(iList=0; iList<nList; iList++){
! 46669: nMerge = 1;
! 46670: aMerge = &aList[iList];
! 46671: for(iSub=0; iList & (1<<iSub); iSub++){
! 46672: struct Sublist *p = &aSub[iSub];
! 46673: assert( p->aList && p->nList<=(1<<iSub) );
! 46674: assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
! 46675: walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
! 46676: }
! 46677: aSub[iSub].aList = aMerge;
! 46678: aSub[iSub].nList = nMerge;
! 46679: }
! 46680:
! 46681: for(iSub++; iSub<ArraySize(aSub); iSub++){
! 46682: if( nList & (1<<iSub) ){
! 46683: struct Sublist *p = &aSub[iSub];
! 46684: assert( p->nList<=(1<<iSub) );
! 46685: assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
! 46686: walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
! 46687: }
! 46688: }
! 46689: assert( aMerge==aList );
! 46690: *pnList = nMerge;
! 46691:
! 46692: #ifdef SQLITE_DEBUG
! 46693: {
! 46694: int i;
! 46695: for(i=1; i<*pnList; i++){
! 46696: assert( aContent[aList[i]] > aContent[aList[i-1]] );
! 46697: }
! 46698: }
! 46699: #endif
! 46700: }
! 46701:
! 46702: /*
! 46703: ** Free an iterator allocated by walIteratorInit().
! 46704: */
! 46705: static void walIteratorFree(WalIterator *p){
! 46706: sqlite3ScratchFree(p);
! 46707: }
! 46708:
! 46709: /*
! 46710: ** Construct a WalInterator object that can be used to loop over all
! 46711: ** pages in the WAL in ascending order. The caller must hold the checkpoint
! 46712: ** lock.
! 46713: **
! 46714: ** On success, make *pp point to the newly allocated WalInterator object
! 46715: ** return SQLITE_OK. Otherwise, return an error code. If this routine
! 46716: ** returns an error, the value of *pp is undefined.
! 46717: **
! 46718: ** The calling routine should invoke walIteratorFree() to destroy the
! 46719: ** WalIterator object when it has finished with it.
! 46720: */
! 46721: static int walIteratorInit(Wal *pWal, WalIterator **pp){
! 46722: WalIterator *p; /* Return value */
! 46723: int nSegment; /* Number of segments to merge */
! 46724: u32 iLast; /* Last frame in log */
! 46725: int nByte; /* Number of bytes to allocate */
! 46726: int i; /* Iterator variable */
! 46727: ht_slot *aTmp; /* Temp space used by merge-sort */
! 46728: int rc = SQLITE_OK; /* Return Code */
! 46729:
! 46730: /* This routine only runs while holding the checkpoint lock. And
! 46731: ** it only runs if there is actually content in the log (mxFrame>0).
! 46732: */
! 46733: assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
! 46734: iLast = pWal->hdr.mxFrame;
! 46735:
! 46736: /* Allocate space for the WalIterator object. */
! 46737: nSegment = walFramePage(iLast) + 1;
! 46738: nByte = sizeof(WalIterator)
! 46739: + (nSegment-1)*sizeof(struct WalSegment)
! 46740: + iLast*sizeof(ht_slot);
! 46741: p = (WalIterator *)sqlite3ScratchMalloc(nByte);
! 46742: if( !p ){
! 46743: return SQLITE_NOMEM;
! 46744: }
! 46745: memset(p, 0, nByte);
! 46746: p->nSegment = nSegment;
! 46747:
! 46748: /* Allocate temporary space used by the merge-sort routine. This block
! 46749: ** of memory will be freed before this function returns.
! 46750: */
! 46751: aTmp = (ht_slot *)sqlite3ScratchMalloc(
! 46752: sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
! 46753: );
! 46754: if( !aTmp ){
! 46755: rc = SQLITE_NOMEM;
! 46756: }
! 46757:
! 46758: for(i=0; rc==SQLITE_OK && i<nSegment; i++){
! 46759: volatile ht_slot *aHash;
! 46760: u32 iZero;
! 46761: volatile u32 *aPgno;
! 46762:
! 46763: rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
! 46764: if( rc==SQLITE_OK ){
! 46765: int j; /* Counter variable */
! 46766: int nEntry; /* Number of entries in this segment */
! 46767: ht_slot *aIndex; /* Sorted index for this segment */
! 46768:
! 46769: aPgno++;
! 46770: if( (i+1)==nSegment ){
! 46771: nEntry = (int)(iLast - iZero);
! 46772: }else{
! 46773: nEntry = (int)((u32*)aHash - (u32*)aPgno);
! 46774: }
! 46775: aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
! 46776: iZero++;
! 46777:
! 46778: for(j=0; j<nEntry; j++){
! 46779: aIndex[j] = (ht_slot)j;
! 46780: }
! 46781: walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
! 46782: p->aSegment[i].iZero = iZero;
! 46783: p->aSegment[i].nEntry = nEntry;
! 46784: p->aSegment[i].aIndex = aIndex;
! 46785: p->aSegment[i].aPgno = (u32 *)aPgno;
! 46786: }
! 46787: }
! 46788: sqlite3ScratchFree(aTmp);
! 46789:
! 46790: if( rc!=SQLITE_OK ){
! 46791: walIteratorFree(p);
! 46792: }
! 46793: *pp = p;
! 46794: return rc;
! 46795: }
! 46796:
! 46797: /*
! 46798: ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
! 46799: ** n. If the attempt fails and parameter xBusy is not NULL, then it is a
! 46800: ** busy-handler function. Invoke it and retry the lock until either the
! 46801: ** lock is successfully obtained or the busy-handler returns 0.
! 46802: */
! 46803: static int walBusyLock(
! 46804: Wal *pWal, /* WAL connection */
! 46805: int (*xBusy)(void*), /* Function to call when busy */
! 46806: void *pBusyArg, /* Context argument for xBusyHandler */
! 46807: int lockIdx, /* Offset of first byte to lock */
! 46808: int n /* Number of bytes to lock */
! 46809: ){
! 46810: int rc;
! 46811: do {
! 46812: rc = walLockExclusive(pWal, lockIdx, n);
! 46813: }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
! 46814: return rc;
! 46815: }
! 46816:
! 46817: /*
! 46818: ** The cache of the wal-index header must be valid to call this function.
! 46819: ** Return the page-size in bytes used by the database.
! 46820: */
! 46821: static int walPagesize(Wal *pWal){
! 46822: return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
! 46823: }
! 46824:
! 46825: /*
! 46826: ** Copy as much content as we can from the WAL back into the database file
! 46827: ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
! 46828: **
! 46829: ** The amount of information copies from WAL to database might be limited
! 46830: ** by active readers. This routine will never overwrite a database page
! 46831: ** that a concurrent reader might be using.
! 46832: **
! 46833: ** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
! 46834: ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
! 46835: ** checkpoints are always run by a background thread or background
! 46836: ** process, foreground threads will never block on a lengthy fsync call.
! 46837: **
! 46838: ** Fsync is called on the WAL before writing content out of the WAL and
! 46839: ** into the database. This ensures that if the new content is persistent
! 46840: ** in the WAL and can be recovered following a power-loss or hard reset.
! 46841: **
! 46842: ** Fsync is also called on the database file if (and only if) the entire
! 46843: ** WAL content is copied into the database file. This second fsync makes
! 46844: ** it safe to delete the WAL since the new content will persist in the
! 46845: ** database file.
! 46846: **
! 46847: ** This routine uses and updates the nBackfill field of the wal-index header.
! 46848: ** This is the only routine tha will increase the value of nBackfill.
! 46849: ** (A WAL reset or recovery will revert nBackfill to zero, but not increase
! 46850: ** its value.)
! 46851: **
! 46852: ** The caller must be holding sufficient locks to ensure that no other
! 46853: ** checkpoint is running (in any other thread or process) at the same
! 46854: ** time.
! 46855: */
! 46856: static int walCheckpoint(
! 46857: Wal *pWal, /* Wal connection */
! 46858: int eMode, /* One of PASSIVE, FULL or RESTART */
! 46859: int (*xBusyCall)(void*), /* Function to call when busy */
! 46860: void *pBusyArg, /* Context argument for xBusyHandler */
! 46861: int sync_flags, /* Flags for OsSync() (or 0) */
! 46862: u8 *zBuf /* Temporary buffer to use */
! 46863: ){
! 46864: int rc; /* Return code */
! 46865: int szPage; /* Database page-size */
! 46866: WalIterator *pIter = 0; /* Wal iterator context */
! 46867: u32 iDbpage = 0; /* Next database page to write */
! 46868: u32 iFrame = 0; /* Wal frame containing data for iDbpage */
! 46869: u32 mxSafeFrame; /* Max frame that can be backfilled */
! 46870: u32 mxPage; /* Max database page to write */
! 46871: int i; /* Loop counter */
! 46872: volatile WalCkptInfo *pInfo; /* The checkpoint status information */
! 46873: int (*xBusy)(void*) = 0; /* Function to call when waiting for locks */
! 46874:
! 46875: szPage = walPagesize(pWal);
! 46876: testcase( szPage<=32768 );
! 46877: testcase( szPage>=65536 );
! 46878: pInfo = walCkptInfo(pWal);
! 46879: if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
! 46880:
! 46881: /* Allocate the iterator */
! 46882: rc = walIteratorInit(pWal, &pIter);
! 46883: if( rc!=SQLITE_OK ){
! 46884: return rc;
! 46885: }
! 46886: assert( pIter );
! 46887:
! 46888: if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
! 46889:
! 46890: /* Compute in mxSafeFrame the index of the last frame of the WAL that is
! 46891: ** safe to write into the database. Frames beyond mxSafeFrame might
! 46892: ** overwrite database pages that are in use by active readers and thus
! 46893: ** cannot be backfilled from the WAL.
! 46894: */
! 46895: mxSafeFrame = pWal->hdr.mxFrame;
! 46896: mxPage = pWal->hdr.nPage;
! 46897: for(i=1; i<WAL_NREADER; i++){
! 46898: u32 y = pInfo->aReadMark[i];
! 46899: if( mxSafeFrame>y ){
! 46900: assert( y<=pWal->hdr.mxFrame );
! 46901: rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
! 46902: if( rc==SQLITE_OK ){
! 46903: pInfo->aReadMark[i] = READMARK_NOT_USED;
! 46904: walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
! 46905: }else if( rc==SQLITE_BUSY ){
! 46906: mxSafeFrame = y;
! 46907: xBusy = 0;
! 46908: }else{
! 46909: goto walcheckpoint_out;
! 46910: }
! 46911: }
! 46912: }
! 46913:
! 46914: if( pInfo->nBackfill<mxSafeFrame
! 46915: && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
! 46916: ){
! 46917: i64 nSize; /* Current size of database file */
! 46918: u32 nBackfill = pInfo->nBackfill;
! 46919:
! 46920: /* Sync the WAL to disk */
! 46921: if( sync_flags ){
! 46922: rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
! 46923: }
! 46924:
! 46925: /* If the database file may grow as a result of this checkpoint, hint
! 46926: ** about the eventual size of the db file to the VFS layer.
! 46927: */
! 46928: if( rc==SQLITE_OK ){
! 46929: i64 nReq = ((i64)mxPage * szPage);
! 46930: rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
! 46931: if( rc==SQLITE_OK && nSize<nReq ){
! 46932: sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
! 46933: }
! 46934: }
! 46935:
! 46936: /* Iterate through the contents of the WAL, copying data to the db file. */
! 46937: while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
! 46938: i64 iOffset;
! 46939: assert( walFramePgno(pWal, iFrame)==iDbpage );
! 46940: if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
! 46941: iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
! 46942: /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
! 46943: rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
! 46944: if( rc!=SQLITE_OK ) break;
! 46945: iOffset = (iDbpage-1)*(i64)szPage;
! 46946: testcase( IS_BIG_INT(iOffset) );
! 46947: rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
! 46948: if( rc!=SQLITE_OK ) break;
! 46949: }
! 46950:
! 46951: /* If work was actually accomplished... */
! 46952: if( rc==SQLITE_OK ){
! 46953: if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
! 46954: i64 szDb = pWal->hdr.nPage*(i64)szPage;
! 46955: testcase( IS_BIG_INT(szDb) );
! 46956: rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
! 46957: if( rc==SQLITE_OK && sync_flags ){
! 46958: rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
! 46959: }
! 46960: }
! 46961: if( rc==SQLITE_OK ){
! 46962: pInfo->nBackfill = mxSafeFrame;
! 46963: }
! 46964: }
! 46965:
! 46966: /* Release the reader lock held while backfilling */
! 46967: walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
! 46968: }
! 46969:
! 46970: if( rc==SQLITE_BUSY ){
! 46971: /* Reset the return code so as not to report a checkpoint failure
! 46972: ** just because there are active readers. */
! 46973: rc = SQLITE_OK;
! 46974: }
! 46975:
! 46976: /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
! 46977: ** file has been copied into the database file, then block until all
! 46978: ** readers have finished using the wal file. This ensures that the next
! 46979: ** process to write to the database restarts the wal file.
! 46980: */
! 46981: if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
! 46982: assert( pWal->writeLock );
! 46983: if( pInfo->nBackfill<pWal->hdr.mxFrame ){
! 46984: rc = SQLITE_BUSY;
! 46985: }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
! 46986: assert( mxSafeFrame==pWal->hdr.mxFrame );
! 46987: rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
! 46988: if( rc==SQLITE_OK ){
! 46989: walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
! 46990: }
! 46991: }
! 46992: }
! 46993:
! 46994: walcheckpoint_out:
! 46995: walIteratorFree(pIter);
! 46996: return rc;
! 46997: }
! 46998:
! 46999: /*
! 47000: ** If the WAL file is currently larger than nMax bytes in size, truncate
! 47001: ** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
! 47002: */
! 47003: static void walLimitSize(Wal *pWal, i64 nMax){
! 47004: i64 sz;
! 47005: int rx;
! 47006: sqlite3BeginBenignMalloc();
! 47007: rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
! 47008: if( rx==SQLITE_OK && (sz > nMax ) ){
! 47009: rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
! 47010: }
! 47011: sqlite3EndBenignMalloc();
! 47012: if( rx ){
! 47013: sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
! 47014: }
! 47015: }
! 47016:
! 47017: /*
! 47018: ** Close a connection to a log file.
! 47019: */
! 47020: SQLITE_PRIVATE int sqlite3WalClose(
! 47021: Wal *pWal, /* Wal to close */
! 47022: int sync_flags, /* Flags to pass to OsSync() (or 0) */
! 47023: int nBuf,
! 47024: u8 *zBuf /* Buffer of at least nBuf bytes */
! 47025: ){
! 47026: int rc = SQLITE_OK;
! 47027: if( pWal ){
! 47028: int isDelete = 0; /* True to unlink wal and wal-index files */
! 47029:
! 47030: /* If an EXCLUSIVE lock can be obtained on the database file (using the
! 47031: ** ordinary, rollback-mode locking methods, this guarantees that the
! 47032: ** connection associated with this log file is the only connection to
! 47033: ** the database. In this case checkpoint the database and unlink both
! 47034: ** the wal and wal-index files.
! 47035: **
! 47036: ** The EXCLUSIVE lock is not released before returning.
! 47037: */
! 47038: rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
! 47039: if( rc==SQLITE_OK ){
! 47040: if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
! 47041: pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
! 47042: }
! 47043: rc = sqlite3WalCheckpoint(
! 47044: pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
! 47045: );
! 47046: if( rc==SQLITE_OK ){
! 47047: int bPersist = -1;
! 47048: sqlite3OsFileControlHint(
! 47049: pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
! 47050: );
! 47051: if( bPersist!=1 ){
! 47052: /* Try to delete the WAL file if the checkpoint completed and
! 47053: ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
! 47054: ** mode (!bPersist) */
! 47055: isDelete = 1;
! 47056: }else if( pWal->mxWalSize>=0 ){
! 47057: /* Try to truncate the WAL file to zero bytes if the checkpoint
! 47058: ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
! 47059: ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
! 47060: ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
! 47061: ** to zero bytes as truncating to the journal_size_limit might
! 47062: ** leave a corrupt WAL file on disk. */
! 47063: walLimitSize(pWal, 0);
! 47064: }
! 47065: }
! 47066: }
! 47067:
! 47068: walIndexClose(pWal, isDelete);
! 47069: sqlite3OsClose(pWal->pWalFd);
! 47070: if( isDelete ){
! 47071: sqlite3BeginBenignMalloc();
! 47072: sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
! 47073: sqlite3EndBenignMalloc();
! 47074: }
! 47075: WALTRACE(("WAL%p: closed\n", pWal));
! 47076: sqlite3_free((void *)pWal->apWiData);
! 47077: sqlite3_free(pWal);
! 47078: }
! 47079: return rc;
! 47080: }
! 47081:
! 47082: /*
! 47083: ** Try to read the wal-index header. Return 0 on success and 1 if
! 47084: ** there is a problem.
! 47085: **
! 47086: ** The wal-index is in shared memory. Another thread or process might
! 47087: ** be writing the header at the same time this procedure is trying to
! 47088: ** read it, which might result in inconsistency. A dirty read is detected
! 47089: ** by verifying that both copies of the header are the same and also by
! 47090: ** a checksum on the header.
! 47091: **
! 47092: ** If and only if the read is consistent and the header is different from
! 47093: ** pWal->hdr, then pWal->hdr is updated to the content of the new header
! 47094: ** and *pChanged is set to 1.
! 47095: **
! 47096: ** If the checksum cannot be verified return non-zero. If the header
! 47097: ** is read successfully and the checksum verified, return zero.
! 47098: */
! 47099: static int walIndexTryHdr(Wal *pWal, int *pChanged){
! 47100: u32 aCksum[2]; /* Checksum on the header content */
! 47101: WalIndexHdr h1, h2; /* Two copies of the header content */
! 47102: WalIndexHdr volatile *aHdr; /* Header in shared memory */
! 47103:
! 47104: /* The first page of the wal-index must be mapped at this point. */
! 47105: assert( pWal->nWiData>0 && pWal->apWiData[0] );
! 47106:
! 47107: /* Read the header. This might happen concurrently with a write to the
! 47108: ** same area of shared memory on a different CPU in a SMP,
! 47109: ** meaning it is possible that an inconsistent snapshot is read
! 47110: ** from the file. If this happens, return non-zero.
! 47111: **
! 47112: ** There are two copies of the header at the beginning of the wal-index.
! 47113: ** When reading, read [0] first then [1]. Writes are in the reverse order.
! 47114: ** Memory barriers are used to prevent the compiler or the hardware from
! 47115: ** reordering the reads and writes.
! 47116: */
! 47117: aHdr = walIndexHdr(pWal);
! 47118: memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
! 47119: walShmBarrier(pWal);
! 47120: memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
! 47121:
! 47122: if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
! 47123: return 1; /* Dirty read */
! 47124: }
! 47125: if( h1.isInit==0 ){
! 47126: return 1; /* Malformed header - probably all zeros */
! 47127: }
! 47128: walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
! 47129: if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
! 47130: return 1; /* Checksum does not match */
! 47131: }
! 47132:
! 47133: if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
! 47134: *pChanged = 1;
! 47135: memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
! 47136: pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
! 47137: testcase( pWal->szPage<=32768 );
! 47138: testcase( pWal->szPage>=65536 );
! 47139: }
! 47140:
! 47141: /* The header was successfully read. Return zero. */
! 47142: return 0;
! 47143: }
! 47144:
! 47145: /*
! 47146: ** Read the wal-index header from the wal-index and into pWal->hdr.
! 47147: ** If the wal-header appears to be corrupt, try to reconstruct the
! 47148: ** wal-index from the WAL before returning.
! 47149: **
! 47150: ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
! 47151: ** changed by this opertion. If pWal->hdr is unchanged, set *pChanged
! 47152: ** to 0.
! 47153: **
! 47154: ** If the wal-index header is successfully read, return SQLITE_OK.
! 47155: ** Otherwise an SQLite error code.
! 47156: */
! 47157: static int walIndexReadHdr(Wal *pWal, int *pChanged){
! 47158: int rc; /* Return code */
! 47159: int badHdr; /* True if a header read failed */
! 47160: volatile u32 *page0; /* Chunk of wal-index containing header */
! 47161:
! 47162: /* Ensure that page 0 of the wal-index (the page that contains the
! 47163: ** wal-index header) is mapped. Return early if an error occurs here.
! 47164: */
! 47165: assert( pChanged );
! 47166: rc = walIndexPage(pWal, 0, &page0);
! 47167: if( rc!=SQLITE_OK ){
! 47168: return rc;
! 47169: };
! 47170: assert( page0 || pWal->writeLock==0 );
! 47171:
! 47172: /* If the first page of the wal-index has been mapped, try to read the
! 47173: ** wal-index header immediately, without holding any lock. This usually
! 47174: ** works, but may fail if the wal-index header is corrupt or currently
! 47175: ** being modified by another thread or process.
! 47176: */
! 47177: badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
! 47178:
! 47179: /* If the first attempt failed, it might have been due to a race
! 47180: ** with a writer. So get a WRITE lock and try again.
! 47181: */
! 47182: assert( badHdr==0 || pWal->writeLock==0 );
! 47183: if( badHdr ){
! 47184: if( pWal->readOnly & WAL_SHM_RDONLY ){
! 47185: if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
! 47186: walUnlockShared(pWal, WAL_WRITE_LOCK);
! 47187: rc = SQLITE_READONLY_RECOVERY;
! 47188: }
! 47189: }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
! 47190: pWal->writeLock = 1;
! 47191: if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
! 47192: badHdr = walIndexTryHdr(pWal, pChanged);
! 47193: if( badHdr ){
! 47194: /* If the wal-index header is still malformed even while holding
! 47195: ** a WRITE lock, it can only mean that the header is corrupted and
! 47196: ** needs to be reconstructed. So run recovery to do exactly that.
! 47197: */
! 47198: rc = walIndexRecover(pWal);
! 47199: *pChanged = 1;
! 47200: }
! 47201: }
! 47202: pWal->writeLock = 0;
! 47203: walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
! 47204: }
! 47205: }
! 47206:
! 47207: /* If the header is read successfully, check the version number to make
! 47208: ** sure the wal-index was not constructed with some future format that
! 47209: ** this version of SQLite cannot understand.
! 47210: */
! 47211: if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
! 47212: rc = SQLITE_CANTOPEN_BKPT;
! 47213: }
! 47214:
! 47215: return rc;
! 47216: }
! 47217:
! 47218: /*
! 47219: ** This is the value that walTryBeginRead returns when it needs to
! 47220: ** be retried.
! 47221: */
! 47222: #define WAL_RETRY (-1)
! 47223:
! 47224: /*
! 47225: ** Attempt to start a read transaction. This might fail due to a race or
! 47226: ** other transient condition. When that happens, it returns WAL_RETRY to
! 47227: ** indicate to the caller that it is safe to retry immediately.
! 47228: **
! 47229: ** On success return SQLITE_OK. On a permanent failure (such an
! 47230: ** I/O error or an SQLITE_BUSY because another process is running
! 47231: ** recovery) return a positive error code.
! 47232: **
! 47233: ** The useWal parameter is true to force the use of the WAL and disable
! 47234: ** the case where the WAL is bypassed because it has been completely
! 47235: ** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
! 47236: ** to make a copy of the wal-index header into pWal->hdr. If the
! 47237: ** wal-index header has changed, *pChanged is set to 1 (as an indication
! 47238: ** to the caller that the local paget cache is obsolete and needs to be
! 47239: ** flushed.) When useWal==1, the wal-index header is assumed to already
! 47240: ** be loaded and the pChanged parameter is unused.
! 47241: **
! 47242: ** The caller must set the cnt parameter to the number of prior calls to
! 47243: ** this routine during the current read attempt that returned WAL_RETRY.
! 47244: ** This routine will start taking more aggressive measures to clear the
! 47245: ** race conditions after multiple WAL_RETRY returns, and after an excessive
! 47246: ** number of errors will ultimately return SQLITE_PROTOCOL. The
! 47247: ** SQLITE_PROTOCOL return indicates that some other process has gone rogue
! 47248: ** and is not honoring the locking protocol. There is a vanishingly small
! 47249: ** chance that SQLITE_PROTOCOL could be returned because of a run of really
! 47250: ** bad luck when there is lots of contention for the wal-index, but that
! 47251: ** possibility is so small that it can be safely neglected, we believe.
! 47252: **
! 47253: ** On success, this routine obtains a read lock on
! 47254: ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
! 47255: ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
! 47256: ** that means the Wal does not hold any read lock. The reader must not
! 47257: ** access any database page that is modified by a WAL frame up to and
! 47258: ** including frame number aReadMark[pWal->readLock]. The reader will
! 47259: ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
! 47260: ** Or if pWal->readLock==0, then the reader will ignore the WAL
! 47261: ** completely and get all content directly from the database file.
! 47262: ** If the useWal parameter is 1 then the WAL will never be ignored and
! 47263: ** this routine will always set pWal->readLock>0 on success.
! 47264: ** When the read transaction is completed, the caller must release the
! 47265: ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
! 47266: **
! 47267: ** This routine uses the nBackfill and aReadMark[] fields of the header
! 47268: ** to select a particular WAL_READ_LOCK() that strives to let the
! 47269: ** checkpoint process do as much work as possible. This routine might
! 47270: ** update values of the aReadMark[] array in the header, but if it does
! 47271: ** so it takes care to hold an exclusive lock on the corresponding
! 47272: ** WAL_READ_LOCK() while changing values.
! 47273: */
! 47274: static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
! 47275: volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
! 47276: u32 mxReadMark; /* Largest aReadMark[] value */
! 47277: int mxI; /* Index of largest aReadMark[] value */
! 47278: int i; /* Loop counter */
! 47279: int rc = SQLITE_OK; /* Return code */
! 47280:
! 47281: assert( pWal->readLock<0 ); /* Not currently locked */
! 47282:
! 47283: /* Take steps to avoid spinning forever if there is a protocol error.
! 47284: **
! 47285: ** Circumstances that cause a RETRY should only last for the briefest
! 47286: ** instances of time. No I/O or other system calls are done while the
! 47287: ** locks are held, so the locks should not be held for very long. But
! 47288: ** if we are unlucky, another process that is holding a lock might get
! 47289: ** paged out or take a page-fault that is time-consuming to resolve,
! 47290: ** during the few nanoseconds that it is holding the lock. In that case,
! 47291: ** it might take longer than normal for the lock to free.
! 47292: **
! 47293: ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
! 47294: ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
! 47295: ** is more of a scheduler yield than an actual delay. But on the 10th
! 47296: ** an subsequent retries, the delays start becoming longer and longer,
! 47297: ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
! 47298: ** The total delay time before giving up is less than 1 second.
! 47299: */
! 47300: if( cnt>5 ){
! 47301: int nDelay = 1; /* Pause time in microseconds */
! 47302: if( cnt>100 ){
! 47303: VVA_ONLY( pWal->lockError = 1; )
! 47304: return SQLITE_PROTOCOL;
! 47305: }
! 47306: if( cnt>=10 ) nDelay = (cnt-9)*238; /* Max delay 21ms. Total delay 996ms */
! 47307: sqlite3OsSleep(pWal->pVfs, nDelay);
! 47308: }
! 47309:
! 47310: if( !useWal ){
! 47311: rc = walIndexReadHdr(pWal, pChanged);
! 47312: if( rc==SQLITE_BUSY ){
! 47313: /* If there is not a recovery running in another thread or process
! 47314: ** then convert BUSY errors to WAL_RETRY. If recovery is known to
! 47315: ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
! 47316: ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
! 47317: ** would be technically correct. But the race is benign since with
! 47318: ** WAL_RETRY this routine will be called again and will probably be
! 47319: ** right on the second iteration.
! 47320: */
! 47321: if( pWal->apWiData[0]==0 ){
! 47322: /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
! 47323: ** We assume this is a transient condition, so return WAL_RETRY. The
! 47324: ** xShmMap() implementation used by the default unix and win32 VFS
! 47325: ** modules may return SQLITE_BUSY due to a race condition in the
! 47326: ** code that determines whether or not the shared-memory region
! 47327: ** must be zeroed before the requested page is returned.
! 47328: */
! 47329: rc = WAL_RETRY;
! 47330: }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
! 47331: walUnlockShared(pWal, WAL_RECOVER_LOCK);
! 47332: rc = WAL_RETRY;
! 47333: }else if( rc==SQLITE_BUSY ){
! 47334: rc = SQLITE_BUSY_RECOVERY;
! 47335: }
! 47336: }
! 47337: if( rc!=SQLITE_OK ){
! 47338: return rc;
! 47339: }
! 47340: }
! 47341:
! 47342: pInfo = walCkptInfo(pWal);
! 47343: if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
! 47344: /* The WAL has been completely backfilled (or it is empty).
! 47345: ** and can be safely ignored.
! 47346: */
! 47347: rc = walLockShared(pWal, WAL_READ_LOCK(0));
! 47348: walShmBarrier(pWal);
! 47349: if( rc==SQLITE_OK ){
! 47350: if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
! 47351: /* It is not safe to allow the reader to continue here if frames
! 47352: ** may have been appended to the log before READ_LOCK(0) was obtained.
! 47353: ** When holding READ_LOCK(0), the reader ignores the entire log file,
! 47354: ** which implies that the database file contains a trustworthy
! 47355: ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
! 47356: ** happening, this is usually correct.
! 47357: **
! 47358: ** However, if frames have been appended to the log (or if the log
! 47359: ** is wrapped and written for that matter) before the READ_LOCK(0)
! 47360: ** is obtained, that is not necessarily true. A checkpointer may
! 47361: ** have started to backfill the appended frames but crashed before
! 47362: ** it finished. Leaving a corrupt image in the database file.
! 47363: */
! 47364: walUnlockShared(pWal, WAL_READ_LOCK(0));
! 47365: return WAL_RETRY;
! 47366: }
! 47367: pWal->readLock = 0;
! 47368: return SQLITE_OK;
! 47369: }else if( rc!=SQLITE_BUSY ){
! 47370: return rc;
! 47371: }
! 47372: }
! 47373:
! 47374: /* If we get this far, it means that the reader will want to use
! 47375: ** the WAL to get at content from recent commits. The job now is
! 47376: ** to select one of the aReadMark[] entries that is closest to
! 47377: ** but not exceeding pWal->hdr.mxFrame and lock that entry.
! 47378: */
! 47379: mxReadMark = 0;
! 47380: mxI = 0;
! 47381: for(i=1; i<WAL_NREADER; i++){
! 47382: u32 thisMark = pInfo->aReadMark[i];
! 47383: if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
! 47384: assert( thisMark!=READMARK_NOT_USED );
! 47385: mxReadMark = thisMark;
! 47386: mxI = i;
! 47387: }
! 47388: }
! 47389: /* There was once an "if" here. The extra "{" is to preserve indentation. */
! 47390: {
! 47391: if( (pWal->readOnly & WAL_SHM_RDONLY)==0
! 47392: && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
! 47393: ){
! 47394: for(i=1; i<WAL_NREADER; i++){
! 47395: rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
! 47396: if( rc==SQLITE_OK ){
! 47397: mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
! 47398: mxI = i;
! 47399: walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
! 47400: break;
! 47401: }else if( rc!=SQLITE_BUSY ){
! 47402: return rc;
! 47403: }
! 47404: }
! 47405: }
! 47406: if( mxI==0 ){
! 47407: assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
! 47408: return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
! 47409: }
! 47410:
! 47411: rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
! 47412: if( rc ){
! 47413: return rc==SQLITE_BUSY ? WAL_RETRY : rc;
! 47414: }
! 47415: /* Now that the read-lock has been obtained, check that neither the
! 47416: ** value in the aReadMark[] array or the contents of the wal-index
! 47417: ** header have changed.
! 47418: **
! 47419: ** It is necessary to check that the wal-index header did not change
! 47420: ** between the time it was read and when the shared-lock was obtained
! 47421: ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
! 47422: ** that the log file may have been wrapped by a writer, or that frames
! 47423: ** that occur later in the log than pWal->hdr.mxFrame may have been
! 47424: ** copied into the database by a checkpointer. If either of these things
! 47425: ** happened, then reading the database with the current value of
! 47426: ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
! 47427: ** instead.
! 47428: **
! 47429: ** This does not guarantee that the copy of the wal-index header is up to
! 47430: ** date before proceeding. That would not be possible without somehow
! 47431: ** blocking writers. It only guarantees that a dangerous checkpoint or
! 47432: ** log-wrap (either of which would require an exclusive lock on
! 47433: ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
! 47434: */
! 47435: walShmBarrier(pWal);
! 47436: if( pInfo->aReadMark[mxI]!=mxReadMark
! 47437: || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
! 47438: ){
! 47439: walUnlockShared(pWal, WAL_READ_LOCK(mxI));
! 47440: return WAL_RETRY;
! 47441: }else{
! 47442: assert( mxReadMark<=pWal->hdr.mxFrame );
! 47443: pWal->readLock = (i16)mxI;
! 47444: }
! 47445: }
! 47446: return rc;
! 47447: }
! 47448:
! 47449: /*
! 47450: ** Begin a read transaction on the database.
! 47451: **
! 47452: ** This routine used to be called sqlite3OpenSnapshot() and with good reason:
! 47453: ** it takes a snapshot of the state of the WAL and wal-index for the current
! 47454: ** instant in time. The current thread will continue to use this snapshot.
! 47455: ** Other threads might append new content to the WAL and wal-index but
! 47456: ** that extra content is ignored by the current thread.
! 47457: **
! 47458: ** If the database contents have changes since the previous read
! 47459: ** transaction, then *pChanged is set to 1 before returning. The
! 47460: ** Pager layer will use this to know that is cache is stale and
! 47461: ** needs to be flushed.
! 47462: */
! 47463: SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
! 47464: int rc; /* Return code */
! 47465: int cnt = 0; /* Number of TryBeginRead attempts */
! 47466:
! 47467: do{
! 47468: rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
! 47469: }while( rc==WAL_RETRY );
! 47470: testcase( (rc&0xff)==SQLITE_BUSY );
! 47471: testcase( (rc&0xff)==SQLITE_IOERR );
! 47472: testcase( rc==SQLITE_PROTOCOL );
! 47473: testcase( rc==SQLITE_OK );
! 47474: return rc;
! 47475: }
! 47476:
! 47477: /*
! 47478: ** Finish with a read transaction. All this does is release the
! 47479: ** read-lock.
! 47480: */
! 47481: SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
! 47482: sqlite3WalEndWriteTransaction(pWal);
! 47483: if( pWal->readLock>=0 ){
! 47484: walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
! 47485: pWal->readLock = -1;
! 47486: }
! 47487: }
! 47488:
! 47489: /*
! 47490: ** Read a page from the WAL, if it is present in the WAL and if the
! 47491: ** current read transaction is configured to use the WAL.
! 47492: **
! 47493: ** The *pInWal is set to 1 if the requested page is in the WAL and
! 47494: ** has been loaded. Or *pInWal is set to 0 if the page was not in
! 47495: ** the WAL and needs to be read out of the database.
! 47496: */
! 47497: SQLITE_PRIVATE int sqlite3WalRead(
! 47498: Wal *pWal, /* WAL handle */
! 47499: Pgno pgno, /* Database page number to read data for */
! 47500: int *pInWal, /* OUT: True if data is read from WAL */
! 47501: int nOut, /* Size of buffer pOut in bytes */
! 47502: u8 *pOut /* Buffer to write page data to */
! 47503: ){
! 47504: u32 iRead = 0; /* If !=0, WAL frame to return data from */
! 47505: u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
! 47506: int iHash; /* Used to loop through N hash tables */
! 47507:
! 47508: /* This routine is only be called from within a read transaction. */
! 47509: assert( pWal->readLock>=0 || pWal->lockError );
! 47510:
! 47511: /* If the "last page" field of the wal-index header snapshot is 0, then
! 47512: ** no data will be read from the wal under any circumstances. Return early
! 47513: ** in this case as an optimization. Likewise, if pWal->readLock==0,
! 47514: ** then the WAL is ignored by the reader so return early, as if the
! 47515: ** WAL were empty.
! 47516: */
! 47517: if( iLast==0 || pWal->readLock==0 ){
! 47518: *pInWal = 0;
! 47519: return SQLITE_OK;
! 47520: }
! 47521:
! 47522: /* Search the hash table or tables for an entry matching page number
! 47523: ** pgno. Each iteration of the following for() loop searches one
! 47524: ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
! 47525: **
! 47526: ** This code might run concurrently to the code in walIndexAppend()
! 47527: ** that adds entries to the wal-index (and possibly to this hash
! 47528: ** table). This means the value just read from the hash
! 47529: ** slot (aHash[iKey]) may have been added before or after the
! 47530: ** current read transaction was opened. Values added after the
! 47531: ** read transaction was opened may have been written incorrectly -
! 47532: ** i.e. these slots may contain garbage data. However, we assume
! 47533: ** that any slots written before the current read transaction was
! 47534: ** opened remain unmodified.
! 47535: **
! 47536: ** For the reasons above, the if(...) condition featured in the inner
! 47537: ** loop of the following block is more stringent that would be required
! 47538: ** if we had exclusive access to the hash-table:
! 47539: **
! 47540: ** (aPgno[iFrame]==pgno):
! 47541: ** This condition filters out normal hash-table collisions.
! 47542: **
! 47543: ** (iFrame<=iLast):
! 47544: ** This condition filters out entries that were added to the hash
! 47545: ** table after the current read-transaction had started.
! 47546: */
! 47547: for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
! 47548: volatile ht_slot *aHash; /* Pointer to hash table */
! 47549: volatile u32 *aPgno; /* Pointer to array of page numbers */
! 47550: u32 iZero; /* Frame number corresponding to aPgno[0] */
! 47551: int iKey; /* Hash slot index */
! 47552: int nCollide; /* Number of hash collisions remaining */
! 47553: int rc; /* Error code */
! 47554:
! 47555: rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
! 47556: if( rc!=SQLITE_OK ){
! 47557: return rc;
! 47558: }
! 47559: nCollide = HASHTABLE_NSLOT;
! 47560: for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
! 47561: u32 iFrame = aHash[iKey] + iZero;
! 47562: if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
! 47563: /* assert( iFrame>iRead ); -- not true if there is corruption */
! 47564: iRead = iFrame;
! 47565: }
! 47566: if( (nCollide--)==0 ){
! 47567: return SQLITE_CORRUPT_BKPT;
! 47568: }
! 47569: }
! 47570: }
! 47571:
! 47572: #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
! 47573: /* If expensive assert() statements are available, do a linear search
! 47574: ** of the wal-index file content. Make sure the results agree with the
! 47575: ** result obtained using the hash indexes above. */
! 47576: {
! 47577: u32 iRead2 = 0;
! 47578: u32 iTest;
! 47579: for(iTest=iLast; iTest>0; iTest--){
! 47580: if( walFramePgno(pWal, iTest)==pgno ){
! 47581: iRead2 = iTest;
! 47582: break;
! 47583: }
! 47584: }
! 47585: assert( iRead==iRead2 );
! 47586: }
! 47587: #endif
! 47588:
! 47589: /* If iRead is non-zero, then it is the log frame number that contains the
! 47590: ** required page. Read and return data from the log file.
! 47591: */
! 47592: if( iRead ){
! 47593: int sz;
! 47594: i64 iOffset;
! 47595: sz = pWal->hdr.szPage;
! 47596: sz = (sz&0xfe00) + ((sz&0x0001)<<16);
! 47597: testcase( sz<=32768 );
! 47598: testcase( sz>=65536 );
! 47599: iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
! 47600: *pInWal = 1;
! 47601: /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
! 47602: return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset);
! 47603: }
! 47604:
! 47605: *pInWal = 0;
! 47606: return SQLITE_OK;
! 47607: }
! 47608:
! 47609:
! 47610: /*
! 47611: ** Return the size of the database in pages (or zero, if unknown).
! 47612: */
! 47613: SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
! 47614: if( pWal && ALWAYS(pWal->readLock>=0) ){
! 47615: return pWal->hdr.nPage;
! 47616: }
! 47617: return 0;
! 47618: }
! 47619:
! 47620:
! 47621: /*
! 47622: ** This function starts a write transaction on the WAL.
! 47623: **
! 47624: ** A read transaction must have already been started by a prior call
! 47625: ** to sqlite3WalBeginReadTransaction().
! 47626: **
! 47627: ** If another thread or process has written into the database since
! 47628: ** the read transaction was started, then it is not possible for this
! 47629: ** thread to write as doing so would cause a fork. So this routine
! 47630: ** returns SQLITE_BUSY in that case and no write transaction is started.
! 47631: **
! 47632: ** There can only be a single writer active at a time.
! 47633: */
! 47634: SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
! 47635: int rc;
! 47636:
! 47637: /* Cannot start a write transaction without first holding a read
! 47638: ** transaction. */
! 47639: assert( pWal->readLock>=0 );
! 47640:
! 47641: if( pWal->readOnly ){
! 47642: return SQLITE_READONLY;
! 47643: }
! 47644:
! 47645: /* Only one writer allowed at a time. Get the write lock. Return
! 47646: ** SQLITE_BUSY if unable.
! 47647: */
! 47648: rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
! 47649: if( rc ){
! 47650: return rc;
! 47651: }
! 47652: pWal->writeLock = 1;
! 47653:
! 47654: /* If another connection has written to the database file since the
! 47655: ** time the read transaction on this connection was started, then
! 47656: ** the write is disallowed.
! 47657: */
! 47658: if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
! 47659: walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
! 47660: pWal->writeLock = 0;
! 47661: rc = SQLITE_BUSY;
! 47662: }
! 47663:
! 47664: return rc;
! 47665: }
! 47666:
! 47667: /*
! 47668: ** End a write transaction. The commit has already been done. This
! 47669: ** routine merely releases the lock.
! 47670: */
! 47671: SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
! 47672: if( pWal->writeLock ){
! 47673: walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
! 47674: pWal->writeLock = 0;
! 47675: pWal->truncateOnCommit = 0;
! 47676: }
! 47677: return SQLITE_OK;
! 47678: }
! 47679:
! 47680: /*
! 47681: ** If any data has been written (but not committed) to the log file, this
! 47682: ** function moves the write-pointer back to the start of the transaction.
! 47683: **
! 47684: ** Additionally, the callback function is invoked for each frame written
! 47685: ** to the WAL since the start of the transaction. If the callback returns
! 47686: ** other than SQLITE_OK, it is not invoked again and the error code is
! 47687: ** returned to the caller.
! 47688: **
! 47689: ** Otherwise, if the callback function does not return an error, this
! 47690: ** function returns SQLITE_OK.
! 47691: */
! 47692: SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
! 47693: int rc = SQLITE_OK;
! 47694: if( ALWAYS(pWal->writeLock) ){
! 47695: Pgno iMax = pWal->hdr.mxFrame;
! 47696: Pgno iFrame;
! 47697:
! 47698: /* Restore the clients cache of the wal-index header to the state it
! 47699: ** was in before the client began writing to the database.
! 47700: */
! 47701: memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
! 47702:
! 47703: for(iFrame=pWal->hdr.mxFrame+1;
! 47704: ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
! 47705: iFrame++
! 47706: ){
! 47707: /* This call cannot fail. Unless the page for which the page number
! 47708: ** is passed as the second argument is (a) in the cache and
! 47709: ** (b) has an outstanding reference, then xUndo is either a no-op
! 47710: ** (if (a) is false) or simply expels the page from the cache (if (b)
! 47711: ** is false).
! 47712: **
! 47713: ** If the upper layer is doing a rollback, it is guaranteed that there
! 47714: ** are no outstanding references to any page other than page 1. And
! 47715: ** page 1 is never written to the log until the transaction is
! 47716: ** committed. As a result, the call to xUndo may not fail.
! 47717: */
! 47718: assert( walFramePgno(pWal, iFrame)!=1 );
! 47719: rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
! 47720: }
! 47721: walCleanupHash(pWal);
! 47722: }
! 47723: assert( rc==SQLITE_OK );
! 47724: return rc;
! 47725: }
! 47726:
! 47727: /*
! 47728: ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
! 47729: ** values. This function populates the array with values required to
! 47730: ** "rollback" the write position of the WAL handle back to the current
! 47731: ** point in the event of a savepoint rollback (via WalSavepointUndo()).
! 47732: */
! 47733: SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
! 47734: assert( pWal->writeLock );
! 47735: aWalData[0] = pWal->hdr.mxFrame;
! 47736: aWalData[1] = pWal->hdr.aFrameCksum[0];
! 47737: aWalData[2] = pWal->hdr.aFrameCksum[1];
! 47738: aWalData[3] = pWal->nCkpt;
! 47739: }
! 47740:
! 47741: /*
! 47742: ** Move the write position of the WAL back to the point identified by
! 47743: ** the values in the aWalData[] array. aWalData must point to an array
! 47744: ** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
! 47745: ** by a call to WalSavepoint().
! 47746: */
! 47747: SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
! 47748: int rc = SQLITE_OK;
! 47749:
! 47750: assert( pWal->writeLock );
! 47751: assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
! 47752:
! 47753: if( aWalData[3]!=pWal->nCkpt ){
! 47754: /* This savepoint was opened immediately after the write-transaction
! 47755: ** was started. Right after that, the writer decided to wrap around
! 47756: ** to the start of the log. Update the savepoint values to match.
! 47757: */
! 47758: aWalData[0] = 0;
! 47759: aWalData[3] = pWal->nCkpt;
! 47760: }
! 47761:
! 47762: if( aWalData[0]<pWal->hdr.mxFrame ){
! 47763: pWal->hdr.mxFrame = aWalData[0];
! 47764: pWal->hdr.aFrameCksum[0] = aWalData[1];
! 47765: pWal->hdr.aFrameCksum[1] = aWalData[2];
! 47766: walCleanupHash(pWal);
! 47767: }
! 47768:
! 47769: return rc;
! 47770: }
! 47771:
! 47772:
! 47773: /*
! 47774: ** This function is called just before writing a set of frames to the log
! 47775: ** file (see sqlite3WalFrames()). It checks to see if, instead of appending
! 47776: ** to the current log file, it is possible to overwrite the start of the
! 47777: ** existing log file with the new frames (i.e. "reset" the log). If so,
! 47778: ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
! 47779: ** unchanged.
! 47780: **
! 47781: ** SQLITE_OK is returned if no error is encountered (regardless of whether
! 47782: ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
! 47783: ** if an error occurs.
! 47784: */
! 47785: static int walRestartLog(Wal *pWal){
! 47786: int rc = SQLITE_OK;
! 47787: int cnt;
! 47788:
! 47789: if( pWal->readLock==0 ){
! 47790: volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
! 47791: assert( pInfo->nBackfill==pWal->hdr.mxFrame );
! 47792: if( pInfo->nBackfill>0 ){
! 47793: u32 salt1;
! 47794: sqlite3_randomness(4, &salt1);
! 47795: rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
! 47796: if( rc==SQLITE_OK ){
! 47797: /* If all readers are using WAL_READ_LOCK(0) (in other words if no
! 47798: ** readers are currently using the WAL), then the transactions
! 47799: ** frames will overwrite the start of the existing log. Update the
! 47800: ** wal-index header to reflect this.
! 47801: **
! 47802: ** In theory it would be Ok to update the cache of the header only
! 47803: ** at this point. But updating the actual wal-index header is also
! 47804: ** safe and means there is no special case for sqlite3WalUndo()
! 47805: ** to handle if this transaction is rolled back.
! 47806: */
! 47807: int i; /* Loop counter */
! 47808: u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
! 47809:
! 47810: pWal->nCkpt++;
! 47811: pWal->hdr.mxFrame = 0;
! 47812: sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
! 47813: aSalt[1] = salt1;
! 47814: walIndexWriteHdr(pWal);
! 47815: pInfo->nBackfill = 0;
! 47816: for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
! 47817: assert( pInfo->aReadMark[0]==0 );
! 47818: walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
! 47819: }else if( rc!=SQLITE_BUSY ){
! 47820: return rc;
! 47821: }
! 47822: }
! 47823: walUnlockShared(pWal, WAL_READ_LOCK(0));
! 47824: pWal->readLock = -1;
! 47825: cnt = 0;
! 47826: do{
! 47827: int notUsed;
! 47828: rc = walTryBeginRead(pWal, ¬Used, 1, ++cnt);
! 47829: }while( rc==WAL_RETRY );
! 47830: assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
! 47831: testcase( (rc&0xff)==SQLITE_IOERR );
! 47832: testcase( rc==SQLITE_PROTOCOL );
! 47833: testcase( rc==SQLITE_OK );
! 47834: }
! 47835: return rc;
! 47836: }
! 47837:
! 47838: /*
! 47839: ** Information about the current state of the WAL file and where
! 47840: ** the next fsync should occur - passed from sqlite3WalFrames() into
! 47841: ** walWriteToLog().
! 47842: */
! 47843: typedef struct WalWriter {
! 47844: Wal *pWal; /* The complete WAL information */
! 47845: sqlite3_file *pFd; /* The WAL file to which we write */
! 47846: sqlite3_int64 iSyncPoint; /* Fsync at this offset */
! 47847: int syncFlags; /* Flags for the fsync */
! 47848: int szPage; /* Size of one page */
! 47849: } WalWriter;
! 47850:
! 47851: /*
! 47852: ** Write iAmt bytes of content into the WAL file beginning at iOffset.
! 47853: ** Do a sync when crossing the p->iSyncPoint boundary.
! 47854: **
! 47855: ** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
! 47856: ** first write the part before iSyncPoint, then sync, then write the
! 47857: ** rest.
! 47858: */
! 47859: static int walWriteToLog(
! 47860: WalWriter *p, /* WAL to write to */
! 47861: void *pContent, /* Content to be written */
! 47862: int iAmt, /* Number of bytes to write */
! 47863: sqlite3_int64 iOffset /* Start writing at this offset */
! 47864: ){
! 47865: int rc;
! 47866: if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
! 47867: int iFirstAmt = (int)(p->iSyncPoint - iOffset);
! 47868: rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
! 47869: if( rc ) return rc;
! 47870: iOffset += iFirstAmt;
! 47871: iAmt -= iFirstAmt;
! 47872: pContent = (void*)(iFirstAmt + (char*)pContent);
! 47873: assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
! 47874: rc = sqlite3OsSync(p->pFd, p->syncFlags);
! 47875: if( iAmt==0 || rc ) return rc;
! 47876: }
! 47877: rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
! 47878: return rc;
! 47879: }
! 47880:
! 47881: /*
! 47882: ** Write out a single frame of the WAL
! 47883: */
! 47884: static int walWriteOneFrame(
! 47885: WalWriter *p, /* Where to write the frame */
! 47886: PgHdr *pPage, /* The page of the frame to be written */
! 47887: int nTruncate, /* The commit flag. Usually 0. >0 for commit */
! 47888: sqlite3_int64 iOffset /* Byte offset at which to write */
! 47889: ){
! 47890: int rc; /* Result code from subfunctions */
! 47891: void *pData; /* Data actually written */
! 47892: u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
! 47893: #if defined(SQLITE_HAS_CODEC)
! 47894: if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
! 47895: #else
! 47896: pData = pPage->pData;
! 47897: #endif
! 47898: walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
! 47899: rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
! 47900: if( rc ) return rc;
! 47901: /* Write the page data */
! 47902: rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
! 47903: return rc;
! 47904: }
! 47905:
! 47906: /*
! 47907: ** Write a set of frames to the log. The caller must hold the write-lock
! 47908: ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
! 47909: */
! 47910: SQLITE_PRIVATE int sqlite3WalFrames(
! 47911: Wal *pWal, /* Wal handle to write to */
! 47912: int szPage, /* Database page-size in bytes */
! 47913: PgHdr *pList, /* List of dirty pages to write */
! 47914: Pgno nTruncate, /* Database size after this commit */
! 47915: int isCommit, /* True if this is a commit */
! 47916: int sync_flags /* Flags to pass to OsSync() (or 0) */
! 47917: ){
! 47918: int rc; /* Used to catch return codes */
! 47919: u32 iFrame; /* Next frame address */
! 47920: PgHdr *p; /* Iterator to run through pList with. */
! 47921: PgHdr *pLast = 0; /* Last frame in list */
! 47922: int nExtra = 0; /* Number of extra copies of last page */
! 47923: int szFrame; /* The size of a single frame */
! 47924: i64 iOffset; /* Next byte to write in WAL file */
! 47925: WalWriter w; /* The writer */
! 47926:
! 47927: assert( pList );
! 47928: assert( pWal->writeLock );
! 47929:
! 47930: /* If this frame set completes a transaction, then nTruncate>0. If
! 47931: ** nTruncate==0 then this frame set does not complete the transaction. */
! 47932: assert( (isCommit!=0)==(nTruncate!=0) );
! 47933:
! 47934: #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
! 47935: { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
! 47936: WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
! 47937: pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
! 47938: }
! 47939: #endif
! 47940:
! 47941: /* See if it is possible to write these frames into the start of the
! 47942: ** log file, instead of appending to it at pWal->hdr.mxFrame.
! 47943: */
! 47944: if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
! 47945: return rc;
! 47946: }
! 47947:
! 47948: /* If this is the first frame written into the log, write the WAL
! 47949: ** header to the start of the WAL file. See comments at the top of
! 47950: ** this source file for a description of the WAL header format.
! 47951: */
! 47952: iFrame = pWal->hdr.mxFrame;
! 47953: if( iFrame==0 ){
! 47954: u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
! 47955: u32 aCksum[2]; /* Checksum for wal-header */
! 47956:
! 47957: sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
! 47958: sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
! 47959: sqlite3Put4byte(&aWalHdr[8], szPage);
! 47960: sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
! 47961: if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
! 47962: memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
! 47963: walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
! 47964: sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
! 47965: sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
! 47966:
! 47967: pWal->szPage = szPage;
! 47968: pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
! 47969: pWal->hdr.aFrameCksum[0] = aCksum[0];
! 47970: pWal->hdr.aFrameCksum[1] = aCksum[1];
! 47971: pWal->truncateOnCommit = 1;
! 47972:
! 47973: rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
! 47974: WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
! 47975: if( rc!=SQLITE_OK ){
! 47976: return rc;
! 47977: }
! 47978:
! 47979: /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
! 47980: ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
! 47981: ** an out-of-order write following a WAL restart could result in
! 47982: ** database corruption. See the ticket:
! 47983: **
! 47984: ** http://localhost:591/sqlite/info/ff5be73dee
! 47985: */
! 47986: if( pWal->syncHeader && sync_flags ){
! 47987: rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
! 47988: if( rc ) return rc;
! 47989: }
! 47990: }
! 47991: assert( (int)pWal->szPage==szPage );
! 47992:
! 47993: /* Setup information needed to write frames into the WAL */
! 47994: w.pWal = pWal;
! 47995: w.pFd = pWal->pWalFd;
! 47996: w.iSyncPoint = 0;
! 47997: w.syncFlags = sync_flags;
! 47998: w.szPage = szPage;
! 47999: iOffset = walFrameOffset(iFrame+1, szPage);
! 48000: szFrame = szPage + WAL_FRAME_HDRSIZE;
! 48001:
! 48002: /* Write all frames into the log file exactly once */
! 48003: for(p=pList; p; p=p->pDirty){
! 48004: int nDbSize; /* 0 normally. Positive == commit flag */
! 48005: iFrame++;
! 48006: assert( iOffset==walFrameOffset(iFrame, szPage) );
! 48007: nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
! 48008: rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
! 48009: if( rc ) return rc;
! 48010: pLast = p;
! 48011: iOffset += szFrame;
! 48012: }
! 48013:
! 48014: /* If this is the end of a transaction, then we might need to pad
! 48015: ** the transaction and/or sync the WAL file.
! 48016: **
! 48017: ** Padding and syncing only occur if this set of frames complete a
! 48018: ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
! 48019: ** or synchonous==OFF, then no padding or syncing are needed.
! 48020: **
! 48021: ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
! 48022: ** needed and only the sync is done. If padding is needed, then the
! 48023: ** final frame is repeated (with its commit mark) until the next sector
! 48024: ** boundary is crossed. Only the part of the WAL prior to the last
! 48025: ** sector boundary is synced; the part of the last frame that extends
! 48026: ** past the sector boundary is written after the sync.
! 48027: */
! 48028: if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
! 48029: if( pWal->padToSectorBoundary ){
! 48030: int sectorSize = sqlite3OsSectorSize(pWal->pWalFd);
! 48031: w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
! 48032: while( iOffset<w.iSyncPoint ){
! 48033: rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
! 48034: if( rc ) return rc;
! 48035: iOffset += szFrame;
! 48036: nExtra++;
! 48037: }
! 48038: }else{
! 48039: rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
! 48040: }
! 48041: }
! 48042:
! 48043: /* If this frame set completes the first transaction in the WAL and
! 48044: ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
! 48045: ** journal size limit, if possible.
! 48046: */
! 48047: if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
! 48048: i64 sz = pWal->mxWalSize;
! 48049: if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
! 48050: sz = walFrameOffset(iFrame+nExtra+1, szPage);
! 48051: }
! 48052: walLimitSize(pWal, sz);
! 48053: pWal->truncateOnCommit = 0;
! 48054: }
! 48055:
! 48056: /* Append data to the wal-index. It is not necessary to lock the
! 48057: ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
! 48058: ** guarantees that there are no other writers, and no data that may
! 48059: ** be in use by existing readers is being overwritten.
! 48060: */
! 48061: iFrame = pWal->hdr.mxFrame;
! 48062: for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
! 48063: iFrame++;
! 48064: rc = walIndexAppend(pWal, iFrame, p->pgno);
! 48065: }
! 48066: while( rc==SQLITE_OK && nExtra>0 ){
! 48067: iFrame++;
! 48068: nExtra--;
! 48069: rc = walIndexAppend(pWal, iFrame, pLast->pgno);
! 48070: }
! 48071:
! 48072: if( rc==SQLITE_OK ){
! 48073: /* Update the private copy of the header. */
! 48074: pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
! 48075: testcase( szPage<=32768 );
! 48076: testcase( szPage>=65536 );
! 48077: pWal->hdr.mxFrame = iFrame;
! 48078: if( isCommit ){
! 48079: pWal->hdr.iChange++;
! 48080: pWal->hdr.nPage = nTruncate;
! 48081: }
! 48082: /* If this is a commit, update the wal-index header too. */
! 48083: if( isCommit ){
! 48084: walIndexWriteHdr(pWal);
! 48085: pWal->iCallback = iFrame;
! 48086: }
! 48087: }
! 48088:
! 48089: WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
! 48090: return rc;
! 48091: }
! 48092:
! 48093: /*
! 48094: ** This routine is called to implement sqlite3_wal_checkpoint() and
! 48095: ** related interfaces.
! 48096: **
! 48097: ** Obtain a CHECKPOINT lock and then backfill as much information as
! 48098: ** we can from WAL into the database.
! 48099: **
! 48100: ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
! 48101: ** callback. In this case this function runs a blocking checkpoint.
! 48102: */
! 48103: SQLITE_PRIVATE int sqlite3WalCheckpoint(
! 48104: Wal *pWal, /* Wal connection */
! 48105: int eMode, /* PASSIVE, FULL or RESTART */
! 48106: int (*xBusy)(void*), /* Function to call when busy */
! 48107: void *pBusyArg, /* Context argument for xBusyHandler */
! 48108: int sync_flags, /* Flags to sync db file with (or 0) */
! 48109: int nBuf, /* Size of temporary buffer */
! 48110: u8 *zBuf, /* Temporary buffer to use */
! 48111: int *pnLog, /* OUT: Number of frames in WAL */
! 48112: int *pnCkpt /* OUT: Number of backfilled frames in WAL */
! 48113: ){
! 48114: int rc; /* Return code */
! 48115: int isChanged = 0; /* True if a new wal-index header is loaded */
! 48116: int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
! 48117:
! 48118: assert( pWal->ckptLock==0 );
! 48119: assert( pWal->writeLock==0 );
! 48120:
! 48121: if( pWal->readOnly ) return SQLITE_READONLY;
! 48122: WALTRACE(("WAL%p: checkpoint begins\n", pWal));
! 48123: rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
! 48124: if( rc ){
! 48125: /* Usually this is SQLITE_BUSY meaning that another thread or process
! 48126: ** is already running a checkpoint, or maybe a recovery. But it might
! 48127: ** also be SQLITE_IOERR. */
! 48128: return rc;
! 48129: }
! 48130: pWal->ckptLock = 1;
! 48131:
! 48132: /* If this is a blocking-checkpoint, then obtain the write-lock as well
! 48133: ** to prevent any writers from running while the checkpoint is underway.
! 48134: ** This has to be done before the call to walIndexReadHdr() below.
! 48135: **
! 48136: ** If the writer lock cannot be obtained, then a passive checkpoint is
! 48137: ** run instead. Since the checkpointer is not holding the writer lock,
! 48138: ** there is no point in blocking waiting for any readers. Assuming no
! 48139: ** other error occurs, this function will return SQLITE_BUSY to the caller.
! 48140: */
! 48141: if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
! 48142: rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
! 48143: if( rc==SQLITE_OK ){
! 48144: pWal->writeLock = 1;
! 48145: }else if( rc==SQLITE_BUSY ){
! 48146: eMode2 = SQLITE_CHECKPOINT_PASSIVE;
! 48147: rc = SQLITE_OK;
! 48148: }
! 48149: }
! 48150:
! 48151: /* Read the wal-index header. */
! 48152: if( rc==SQLITE_OK ){
! 48153: rc = walIndexReadHdr(pWal, &isChanged);
! 48154: }
! 48155:
! 48156: /* Copy data from the log to the database file. */
! 48157: if( rc==SQLITE_OK ){
! 48158: if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
! 48159: rc = SQLITE_CORRUPT_BKPT;
! 48160: }else{
! 48161: rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
! 48162: }
! 48163:
! 48164: /* If no error occurred, set the output variables. */
! 48165: if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
! 48166: if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
! 48167: if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
! 48168: }
! 48169: }
! 48170:
! 48171: if( isChanged ){
! 48172: /* If a new wal-index header was loaded before the checkpoint was
! 48173: ** performed, then the pager-cache associated with pWal is now
! 48174: ** out of date. So zero the cached wal-index header to ensure that
! 48175: ** next time the pager opens a snapshot on this database it knows that
! 48176: ** the cache needs to be reset.
! 48177: */
! 48178: memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
! 48179: }
! 48180:
! 48181: /* Release the locks. */
! 48182: sqlite3WalEndWriteTransaction(pWal);
! 48183: walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
! 48184: pWal->ckptLock = 0;
! 48185: WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
! 48186: return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
! 48187: }
! 48188:
! 48189: /* Return the value to pass to a sqlite3_wal_hook callback, the
! 48190: ** number of frames in the WAL at the point of the last commit since
! 48191: ** sqlite3WalCallback() was called. If no commits have occurred since
! 48192: ** the last call, then return 0.
! 48193: */
! 48194: SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
! 48195: u32 ret = 0;
! 48196: if( pWal ){
! 48197: ret = pWal->iCallback;
! 48198: pWal->iCallback = 0;
! 48199: }
! 48200: return (int)ret;
! 48201: }
! 48202:
! 48203: /*
! 48204: ** This function is called to change the WAL subsystem into or out
! 48205: ** of locking_mode=EXCLUSIVE.
! 48206: **
! 48207: ** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
! 48208: ** into locking_mode=NORMAL. This means that we must acquire a lock
! 48209: ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
! 48210: ** or if the acquisition of the lock fails, then return 0. If the
! 48211: ** transition out of exclusive-mode is successful, return 1. This
! 48212: ** operation must occur while the pager is still holding the exclusive
! 48213: ** lock on the main database file.
! 48214: **
! 48215: ** If op is one, then change from locking_mode=NORMAL into
! 48216: ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
! 48217: ** be released. Return 1 if the transition is made and 0 if the
! 48218: ** WAL is already in exclusive-locking mode - meaning that this
! 48219: ** routine is a no-op. The pager must already hold the exclusive lock
! 48220: ** on the main database file before invoking this operation.
! 48221: **
! 48222: ** If op is negative, then do a dry-run of the op==1 case but do
! 48223: ** not actually change anything. The pager uses this to see if it
! 48224: ** should acquire the database exclusive lock prior to invoking
! 48225: ** the op==1 case.
! 48226: */
! 48227: SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
! 48228: int rc;
! 48229: assert( pWal->writeLock==0 );
! 48230: assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
! 48231:
! 48232: /* pWal->readLock is usually set, but might be -1 if there was a
! 48233: ** prior error while attempting to acquire are read-lock. This cannot
! 48234: ** happen if the connection is actually in exclusive mode (as no xShmLock
! 48235: ** locks are taken in this case). Nor should the pager attempt to
! 48236: ** upgrade to exclusive-mode following such an error.
! 48237: */
! 48238: assert( pWal->readLock>=0 || pWal->lockError );
! 48239: assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
! 48240:
! 48241: if( op==0 ){
! 48242: if( pWal->exclusiveMode ){
! 48243: pWal->exclusiveMode = 0;
! 48244: if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
! 48245: pWal->exclusiveMode = 1;
! 48246: }
! 48247: rc = pWal->exclusiveMode==0;
! 48248: }else{
! 48249: /* Already in locking_mode=NORMAL */
! 48250: rc = 0;
! 48251: }
! 48252: }else if( op>0 ){
! 48253: assert( pWal->exclusiveMode==0 );
! 48254: assert( pWal->readLock>=0 );
! 48255: walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
! 48256: pWal->exclusiveMode = 1;
! 48257: rc = 1;
! 48258: }else{
! 48259: rc = pWal->exclusiveMode==0;
! 48260: }
! 48261: return rc;
! 48262: }
! 48263:
! 48264: /*
! 48265: ** Return true if the argument is non-NULL and the WAL module is using
! 48266: ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
! 48267: ** WAL module is using shared-memory, return false.
! 48268: */
! 48269: SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
! 48270: return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
! 48271: }
! 48272:
! 48273: #endif /* #ifndef SQLITE_OMIT_WAL */
! 48274:
! 48275: /************** End of wal.c *************************************************/
! 48276: /************** Begin file btmutex.c *****************************************/
! 48277: /*
! 48278: ** 2007 August 27
! 48279: **
! 48280: ** The author disclaims copyright to this source code. In place of
! 48281: ** a legal notice, here is a blessing:
! 48282: **
! 48283: ** May you do good and not evil.
! 48284: ** May you find forgiveness for yourself and forgive others.
! 48285: ** May you share freely, never taking more than you give.
! 48286: **
! 48287: *************************************************************************
! 48288: **
! 48289: ** This file contains code used to implement mutexes on Btree objects.
! 48290: ** This code really belongs in btree.c. But btree.c is getting too
! 48291: ** big and we want to break it down some. This packaged seemed like
! 48292: ** a good breakout.
! 48293: */
! 48294: /************** Include btreeInt.h in the middle of btmutex.c ****************/
! 48295: /************** Begin file btreeInt.h ****************************************/
! 48296: /*
! 48297: ** 2004 April 6
! 48298: **
! 48299: ** The author disclaims copyright to this source code. In place of
! 48300: ** a legal notice, here is a blessing:
! 48301: **
! 48302: ** May you do good and not evil.
! 48303: ** May you find forgiveness for yourself and forgive others.
! 48304: ** May you share freely, never taking more than you give.
! 48305: **
! 48306: *************************************************************************
! 48307: ** This file implements a external (disk-based) database using BTrees.
! 48308: ** For a detailed discussion of BTrees, refer to
! 48309: **
! 48310: ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
! 48311: ** "Sorting And Searching", pages 473-480. Addison-Wesley
! 48312: ** Publishing Company, Reading, Massachusetts.
! 48313: **
! 48314: ** The basic idea is that each page of the file contains N database
! 48315: ** entries and N+1 pointers to subpages.
! 48316: **
! 48317: ** ----------------------------------------------------------------
! 48318: ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
! 48319: ** ----------------------------------------------------------------
! 48320: **
! 48321: ** All of the keys on the page that Ptr(0) points to have values less
! 48322: ** than Key(0). All of the keys on page Ptr(1) and its subpages have
! 48323: ** values greater than Key(0) and less than Key(1). All of the keys
! 48324: ** on Ptr(N) and its subpages have values greater than Key(N-1). And
! 48325: ** so forth.
! 48326: **
! 48327: ** Finding a particular key requires reading O(log(M)) pages from the
! 48328: ** disk where M is the number of entries in the tree.
! 48329: **
! 48330: ** In this implementation, a single file can hold one or more separate
! 48331: ** BTrees. Each BTree is identified by the index of its root page. The
! 48332: ** key and data for any entry are combined to form the "payload". A
! 48333: ** fixed amount of payload can be carried directly on the database
! 48334: ** page. If the payload is larger than the preset amount then surplus
! 48335: ** bytes are stored on overflow pages. The payload for an entry
! 48336: ** and the preceding pointer are combined to form a "Cell". Each
! 48337: ** page has a small header which contains the Ptr(N) pointer and other
! 48338: ** information such as the size of key and data.
! 48339: **
! 48340: ** FORMAT DETAILS
! 48341: **
! 48342: ** The file is divided into pages. The first page is called page 1,
! 48343: ** the second is page 2, and so forth. A page number of zero indicates
! 48344: ** "no such page". The page size can be any power of 2 between 512 and 65536.
! 48345: ** Each page can be either a btree page, a freelist page, an overflow
! 48346: ** page, or a pointer-map page.
! 48347: **
! 48348: ** The first page is always a btree page. The first 100 bytes of the first
! 48349: ** page contain a special header (the "file header") that describes the file.
! 48350: ** The format of the file header is as follows:
! 48351: **
! 48352: ** OFFSET SIZE DESCRIPTION
! 48353: ** 0 16 Header string: "SQLite format 3\000"
! 48354: ** 16 2 Page size in bytes.
! 48355: ** 18 1 File format write version
! 48356: ** 19 1 File format read version
! 48357: ** 20 1 Bytes of unused space at the end of each page
! 48358: ** 21 1 Max embedded payload fraction
! 48359: ** 22 1 Min embedded payload fraction
! 48360: ** 23 1 Min leaf payload fraction
! 48361: ** 24 4 File change counter
! 48362: ** 28 4 Reserved for future use
! 48363: ** 32 4 First freelist page
! 48364: ** 36 4 Number of freelist pages in the file
! 48365: ** 40 60 15 4-byte meta values passed to higher layers
! 48366: **
! 48367: ** 40 4 Schema cookie
! 48368: ** 44 4 File format of schema layer
! 48369: ** 48 4 Size of page cache
! 48370: ** 52 4 Largest root-page (auto/incr_vacuum)
! 48371: ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
! 48372: ** 60 4 User version
! 48373: ** 64 4 Incremental vacuum mode
! 48374: ** 68 4 unused
! 48375: ** 72 4 unused
! 48376: ** 76 4 unused
! 48377: **
! 48378: ** All of the integer values are big-endian (most significant byte first).
! 48379: **
! 48380: ** The file change counter is incremented when the database is changed
! 48381: ** This counter allows other processes to know when the file has changed
! 48382: ** and thus when they need to flush their cache.
! 48383: **
! 48384: ** The max embedded payload fraction is the amount of the total usable
! 48385: ** space in a page that can be consumed by a single cell for standard
! 48386: ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
! 48387: ** is to limit the maximum cell size so that at least 4 cells will fit
! 48388: ** on one page. Thus the default max embedded payload fraction is 64.
! 48389: **
! 48390: ** If the payload for a cell is larger than the max payload, then extra
! 48391: ** payload is spilled to overflow pages. Once an overflow page is allocated,
! 48392: ** as many bytes as possible are moved into the overflow pages without letting
! 48393: ** the cell size drop below the min embedded payload fraction.
! 48394: **
! 48395: ** The min leaf payload fraction is like the min embedded payload fraction
! 48396: ** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
! 48397: ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
! 48398: ** not specified in the header.
! 48399: **
! 48400: ** Each btree pages is divided into three sections: The header, the
! 48401: ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
! 48402: ** file header that occurs before the page header.
! 48403: **
! 48404: ** |----------------|
! 48405: ** | file header | 100 bytes. Page 1 only.
! 48406: ** |----------------|
! 48407: ** | page header | 8 bytes for leaves. 12 bytes for interior nodes
! 48408: ** |----------------|
! 48409: ** | cell pointer | | 2 bytes per cell. Sorted order.
! 48410: ** | array | | Grows downward
! 48411: ** | | v
! 48412: ** |----------------|
! 48413: ** | unallocated |
! 48414: ** | space |
! 48415: ** |----------------| ^ Grows upwards
! 48416: ** | cell content | | Arbitrary order interspersed with freeblocks.
! 48417: ** | area | | and free space fragments.
! 48418: ** |----------------|
! 48419: **
! 48420: ** The page headers looks like this:
! 48421: **
! 48422: ** OFFSET SIZE DESCRIPTION
! 48423: ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
! 48424: ** 1 2 byte offset to the first freeblock
! 48425: ** 3 2 number of cells on this page
! 48426: ** 5 2 first byte of the cell content area
! 48427: ** 7 1 number of fragmented free bytes
! 48428: ** 8 4 Right child (the Ptr(N) value). Omitted on leaves.
! 48429: **
! 48430: ** The flags define the format of this btree page. The leaf flag means that
! 48431: ** this page has no children. The zerodata flag means that this page carries
! 48432: ** only keys and no data. The intkey flag means that the key is a integer
! 48433: ** which is stored in the key size entry of the cell header rather than in
! 48434: ** the payload area.
! 48435: **
! 48436: ** The cell pointer array begins on the first byte after the page header.
! 48437: ** The cell pointer array contains zero or more 2-byte numbers which are
! 48438: ** offsets from the beginning of the page to the cell content in the cell
! 48439: ** content area. The cell pointers occur in sorted order. The system strives
! 48440: ** to keep free space after the last cell pointer so that new cells can
! 48441: ** be easily added without having to defragment the page.
! 48442: **
! 48443: ** Cell content is stored at the very end of the page and grows toward the
! 48444: ** beginning of the page.
! 48445: **
! 48446: ** Unused space within the cell content area is collected into a linked list of
! 48447: ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset
! 48448: ** to the first freeblock is given in the header. Freeblocks occur in
! 48449: ** increasing order. Because a freeblock must be at least 4 bytes in size,
! 48450: ** any group of 3 or fewer unused bytes in the cell content area cannot
! 48451: ** exist on the freeblock chain. A group of 3 or fewer free bytes is called
! 48452: ** a fragment. The total number of bytes in all fragments is recorded.
! 48453: ** in the page header at offset 7.
! 48454: **
! 48455: ** SIZE DESCRIPTION
! 48456: ** 2 Byte offset of the next freeblock
! 48457: ** 2 Bytes in this freeblock
! 48458: **
! 48459: ** Cells are of variable length. Cells are stored in the cell content area at
! 48460: ** the end of the page. Pointers to the cells are in the cell pointer array
! 48461: ** that immediately follows the page header. Cells is not necessarily
! 48462: ** contiguous or in order, but cell pointers are contiguous and in order.
! 48463: **
! 48464: ** Cell content makes use of variable length integers. A variable
! 48465: ** length integer is 1 to 9 bytes where the lower 7 bits of each
! 48466: ** byte are used. The integer consists of all bytes that have bit 8 set and
! 48467: ** the first byte with bit 8 clear. The most significant byte of the integer
! 48468: ** appears first. A variable-length integer may not be more than 9 bytes long.
! 48469: ** As a special case, all 8 bytes of the 9th byte are used as data. This
! 48470: ** allows a 64-bit integer to be encoded in 9 bytes.
! 48471: **
! 48472: ** 0x00 becomes 0x00000000
! 48473: ** 0x7f becomes 0x0000007f
! 48474: ** 0x81 0x00 becomes 0x00000080
! 48475: ** 0x82 0x00 becomes 0x00000100
! 48476: ** 0x80 0x7f becomes 0x0000007f
! 48477: ** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678
! 48478: ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081
! 48479: **
! 48480: ** Variable length integers are used for rowids and to hold the number of
! 48481: ** bytes of key and data in a btree cell.
! 48482: **
! 48483: ** The content of a cell looks like this:
! 48484: **
! 48485: ** SIZE DESCRIPTION
! 48486: ** 4 Page number of the left child. Omitted if leaf flag is set.
! 48487: ** var Number of bytes of data. Omitted if the zerodata flag is set.
! 48488: ** var Number of bytes of key. Or the key itself if intkey flag is set.
! 48489: ** * Payload
! 48490: ** 4 First page of the overflow chain. Omitted if no overflow
! 48491: **
! 48492: ** Overflow pages form a linked list. Each page except the last is completely
! 48493: ** filled with data (pagesize - 4 bytes). The last page can have as little
! 48494: ** as 1 byte of data.
! 48495: **
! 48496: ** SIZE DESCRIPTION
! 48497: ** 4 Page number of next overflow page
! 48498: ** * Data
! 48499: **
! 48500: ** Freelist pages come in two subtypes: trunk pages and leaf pages. The
! 48501: ** file header points to the first in a linked list of trunk page. Each trunk
! 48502: ** page points to multiple leaf pages. The content of a leaf page is
! 48503: ** unspecified. A trunk page looks like this:
! 48504: **
! 48505: ** SIZE DESCRIPTION
! 48506: ** 4 Page number of next trunk page
! 48507: ** 4 Number of leaf pointers on this page
! 48508: ** * zero or more pages numbers of leaves
! 48509: */
! 48510:
! 48511:
! 48512: /* The following value is the maximum cell size assuming a maximum page
! 48513: ** size give above.
! 48514: */
! 48515: #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
! 48516:
! 48517: /* The maximum number of cells on a single page of the database. This
! 48518: ** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself
! 48519: ** plus 2 bytes for the index to the cell in the page header). Such
! 48520: ** small cells will be rare, but they are possible.
! 48521: */
! 48522: #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
! 48523:
! 48524: /* Forward declarations */
! 48525: typedef struct MemPage MemPage;
! 48526: typedef struct BtLock BtLock;
! 48527:
! 48528: /*
! 48529: ** This is a magic string that appears at the beginning of every
! 48530: ** SQLite database in order to identify the file as a real database.
! 48531: **
! 48532: ** You can change this value at compile-time by specifying a
! 48533: ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
! 48534: ** header must be exactly 16 bytes including the zero-terminator so
! 48535: ** the string itself should be 15 characters long. If you change
! 48536: ** the header, then your custom library will not be able to read
! 48537: ** databases generated by the standard tools and the standard tools
! 48538: ** will not be able to read databases created by your custom library.
! 48539: */
! 48540: #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
! 48541: # define SQLITE_FILE_HEADER "SQLite format 3"
! 48542: #endif
! 48543:
! 48544: /*
! 48545: ** Page type flags. An ORed combination of these flags appear as the
! 48546: ** first byte of on-disk image of every BTree page.
! 48547: */
! 48548: #define PTF_INTKEY 0x01
! 48549: #define PTF_ZERODATA 0x02
! 48550: #define PTF_LEAFDATA 0x04
! 48551: #define PTF_LEAF 0x08
! 48552:
! 48553: /*
! 48554: ** As each page of the file is loaded into memory, an instance of the following
! 48555: ** structure is appended and initialized to zero. This structure stores
! 48556: ** information about the page that is decoded from the raw file page.
! 48557: **
! 48558: ** The pParent field points back to the parent page. This allows us to
! 48559: ** walk up the BTree from any leaf to the root. Care must be taken to
! 48560: ** unref() the parent page pointer when this page is no longer referenced.
! 48561: ** The pageDestructor() routine handles that chore.
! 48562: **
! 48563: ** Access to all fields of this structure is controlled by the mutex
! 48564: ** stored in MemPage.pBt->mutex.
! 48565: */
! 48566: struct MemPage {
! 48567: u8 isInit; /* True if previously initialized. MUST BE FIRST! */
! 48568: u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
! 48569: u8 intKey; /* True if intkey flag is set */
! 48570: u8 leaf; /* True if leaf flag is set */
! 48571: u8 hasData; /* True if this page stores data */
! 48572: u8 hdrOffset; /* 100 for page 1. 0 otherwise */
! 48573: u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
! 48574: u8 max1bytePayload; /* min(maxLocal,127) */
! 48575: u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
! 48576: u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
! 48577: u16 cellOffset; /* Index in aData of first cell pointer */
! 48578: u16 nFree; /* Number of free bytes on the page */
! 48579: u16 nCell; /* Number of cells on this page, local and ovfl */
! 48580: u16 maskPage; /* Mask for page offset */
! 48581: struct _OvflCell { /* Cells that will not fit on aData[] */
! 48582: u8 *pCell; /* Pointers to the body of the overflow cell */
! 48583: u16 idx; /* Insert this cell before idx-th non-overflow cell */
! 48584: } aOvfl[5];
! 48585: BtShared *pBt; /* Pointer to BtShared that this page is part of */
! 48586: u8 *aData; /* Pointer to disk image of the page data */
! 48587: u8 *aDataEnd; /* One byte past the end of usable data */
! 48588: u8 *aCellIdx; /* The cell index area */
! 48589: DbPage *pDbPage; /* Pager page handle */
! 48590: Pgno pgno; /* Page number for this page */
! 48591: };
! 48592:
! 48593: /*
! 48594: ** The in-memory image of a disk page has the auxiliary information appended
! 48595: ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold
! 48596: ** that extra information.
! 48597: */
! 48598: #define EXTRA_SIZE sizeof(MemPage)
! 48599:
! 48600: /*
! 48601: ** A linked list of the following structures is stored at BtShared.pLock.
! 48602: ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
! 48603: ** is opened on the table with root page BtShared.iTable. Locks are removed
! 48604: ** from this list when a transaction is committed or rolled back, or when
! 48605: ** a btree handle is closed.
! 48606: */
! 48607: struct BtLock {
! 48608: Btree *pBtree; /* Btree handle holding this lock */
! 48609: Pgno iTable; /* Root page of table */
! 48610: u8 eLock; /* READ_LOCK or WRITE_LOCK */
! 48611: BtLock *pNext; /* Next in BtShared.pLock list */
! 48612: };
! 48613:
! 48614: /* Candidate values for BtLock.eLock */
! 48615: #define READ_LOCK 1
! 48616: #define WRITE_LOCK 2
! 48617:
! 48618: /* A Btree handle
! 48619: **
! 48620: ** A database connection contains a pointer to an instance of
! 48621: ** this object for every database file that it has open. This structure
! 48622: ** is opaque to the database connection. The database connection cannot
! 48623: ** see the internals of this structure and only deals with pointers to
! 48624: ** this structure.
! 48625: **
! 48626: ** For some database files, the same underlying database cache might be
! 48627: ** shared between multiple connections. In that case, each connection
! 48628: ** has it own instance of this object. But each instance of this object
! 48629: ** points to the same BtShared object. The database cache and the
! 48630: ** schema associated with the database file are all contained within
! 48631: ** the BtShared object.
! 48632: **
! 48633: ** All fields in this structure are accessed under sqlite3.mutex.
! 48634: ** The pBt pointer itself may not be changed while there exists cursors
! 48635: ** in the referenced BtShared that point back to this Btree since those
! 48636: ** cursors have to go through this Btree to find their BtShared and
! 48637: ** they often do so without holding sqlite3.mutex.
! 48638: */
! 48639: struct Btree {
! 48640: sqlite3 *db; /* The database connection holding this btree */
! 48641: BtShared *pBt; /* Sharable content of this btree */
! 48642: u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
! 48643: u8 sharable; /* True if we can share pBt with another db */
! 48644: u8 locked; /* True if db currently has pBt locked */
! 48645: int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
! 48646: int nBackup; /* Number of backup operations reading this btree */
! 48647: Btree *pNext; /* List of other sharable Btrees from the same db */
! 48648: Btree *pPrev; /* Back pointer of the same list */
! 48649: #ifndef SQLITE_OMIT_SHARED_CACHE
! 48650: BtLock lock; /* Object used to lock page 1 */
! 48651: #endif
! 48652: };
! 48653:
! 48654: /*
! 48655: ** Btree.inTrans may take one of the following values.
! 48656: **
! 48657: ** If the shared-data extension is enabled, there may be multiple users
! 48658: ** of the Btree structure. At most one of these may open a write transaction,
! 48659: ** but any number may have active read transactions.
! 48660: */
! 48661: #define TRANS_NONE 0
! 48662: #define TRANS_READ 1
! 48663: #define TRANS_WRITE 2
! 48664:
! 48665: /*
! 48666: ** An instance of this object represents a single database file.
! 48667: **
! 48668: ** A single database file can be in use at the same time by two
! 48669: ** or more database connections. When two or more connections are
! 48670: ** sharing the same database file, each connection has it own
! 48671: ** private Btree object for the file and each of those Btrees points
! 48672: ** to this one BtShared object. BtShared.nRef is the number of
! 48673: ** connections currently sharing this database file.
! 48674: **
! 48675: ** Fields in this structure are accessed under the BtShared.mutex
! 48676: ** mutex, except for nRef and pNext which are accessed under the
! 48677: ** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field
! 48678: ** may not be modified once it is initially set as long as nRef>0.
! 48679: ** The pSchema field may be set once under BtShared.mutex and
! 48680: ** thereafter is unchanged as long as nRef>0.
! 48681: **
! 48682: ** isPending:
! 48683: **
! 48684: ** If a BtShared client fails to obtain a write-lock on a database
! 48685: ** table (because there exists one or more read-locks on the table),
! 48686: ** the shared-cache enters 'pending-lock' state and isPending is
! 48687: ** set to true.
! 48688: **
! 48689: ** The shared-cache leaves the 'pending lock' state when either of
! 48690: ** the following occur:
! 48691: **
! 48692: ** 1) The current writer (BtShared.pWriter) concludes its transaction, OR
! 48693: ** 2) The number of locks held by other connections drops to zero.
! 48694: **
! 48695: ** while in the 'pending-lock' state, no connection may start a new
! 48696: ** transaction.
! 48697: **
! 48698: ** This feature is included to help prevent writer-starvation.
! 48699: */
! 48700: struct BtShared {
! 48701: Pager *pPager; /* The page cache */
! 48702: sqlite3 *db; /* Database connection currently using this Btree */
! 48703: BtCursor *pCursor; /* A list of all open cursors */
! 48704: MemPage *pPage1; /* First page of the database */
! 48705: u8 openFlags; /* Flags to sqlite3BtreeOpen() */
! 48706: #ifndef SQLITE_OMIT_AUTOVACUUM
! 48707: u8 autoVacuum; /* True if auto-vacuum is enabled */
! 48708: u8 incrVacuum; /* True if incr-vacuum is enabled */
! 48709: #endif
! 48710: u8 inTransaction; /* Transaction state */
! 48711: u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
! 48712: u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
! 48713: u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
! 48714: u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
! 48715: u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
! 48716: u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
! 48717: u32 pageSize; /* Total number of bytes on a page */
! 48718: u32 usableSize; /* Number of usable bytes on each page */
! 48719: int nTransaction; /* Number of open transactions (read + write) */
! 48720: u32 nPage; /* Number of pages in the database */
! 48721: void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
! 48722: void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
! 48723: sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
! 48724: Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
! 48725: #ifndef SQLITE_OMIT_SHARED_CACHE
! 48726: int nRef; /* Number of references to this structure */
! 48727: BtShared *pNext; /* Next on a list of sharable BtShared structs */
! 48728: BtLock *pLock; /* List of locks held on this shared-btree struct */
! 48729: Btree *pWriter; /* Btree with currently open write transaction */
! 48730: #endif
! 48731: u8 *pTmpSpace; /* BtShared.pageSize bytes of space for tmp use */
! 48732: };
! 48733:
! 48734: /*
! 48735: ** Allowed values for BtShared.btsFlags
! 48736: */
! 48737: #define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */
! 48738: #define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */
! 48739: #define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */
! 48740: #define BTS_INITIALLY_EMPTY 0x0008 /* Database was empty at trans start */
! 48741: #define BTS_NO_WAL 0x0010 /* Do not open write-ahead-log files */
! 48742: #define BTS_EXCLUSIVE 0x0020 /* pWriter has an exclusive lock */
! 48743: #define BTS_PENDING 0x0040 /* Waiting for read-locks to clear */
! 48744:
! 48745: /*
! 48746: ** An instance of the following structure is used to hold information
! 48747: ** about a cell. The parseCellPtr() function fills in this structure
! 48748: ** based on information extract from the raw disk page.
! 48749: */
! 48750: typedef struct CellInfo CellInfo;
! 48751: struct CellInfo {
! 48752: i64 nKey; /* The key for INTKEY tables, or number of bytes in key */
! 48753: u8 *pCell; /* Pointer to the start of cell content */
! 48754: u32 nData; /* Number of bytes of data */
! 48755: u32 nPayload; /* Total amount of payload */
! 48756: u16 nHeader; /* Size of the cell content header in bytes */
! 48757: u16 nLocal; /* Amount of payload held locally */
! 48758: u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */
! 48759: u16 nSize; /* Size of the cell content on the main b-tree page */
! 48760: };
! 48761:
! 48762: /*
! 48763: ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
! 48764: ** this will be declared corrupt. This value is calculated based on a
! 48765: ** maximum database size of 2^31 pages a minimum fanout of 2 for a
! 48766: ** root-node and 3 for all other internal nodes.
! 48767: **
! 48768: ** If a tree that appears to be taller than this is encountered, it is
! 48769: ** assumed that the database is corrupt.
! 48770: */
! 48771: #define BTCURSOR_MAX_DEPTH 20
! 48772:
! 48773: /*
! 48774: ** A cursor is a pointer to a particular entry within a particular
! 48775: ** b-tree within a database file.
! 48776: **
! 48777: ** The entry is identified by its MemPage and the index in
! 48778: ** MemPage.aCell[] of the entry.
! 48779: **
! 48780: ** A single database file can be shared by two more database connections,
! 48781: ** but cursors cannot be shared. Each cursor is associated with a
! 48782: ** particular database connection identified BtCursor.pBtree.db.
! 48783: **
! 48784: ** Fields in this structure are accessed under the BtShared.mutex
! 48785: ** found at self->pBt->mutex.
! 48786: */
! 48787: struct BtCursor {
! 48788: Btree *pBtree; /* The Btree to which this cursor belongs */
! 48789: BtShared *pBt; /* The BtShared this cursor points to */
! 48790: BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
! 48791: struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
! 48792: Pgno pgnoRoot; /* The root page of this tree */
! 48793: sqlite3_int64 cachedRowid; /* Next rowid cache. 0 means not valid */
! 48794: CellInfo info; /* A parse of the cell we are pointing at */
! 48795: i64 nKey; /* Size of pKey, or last integer key */
! 48796: void *pKey; /* Saved key that was cursor's last known position */
! 48797: int skipNext; /* Prev() is noop if negative. Next() is noop if positive */
! 48798: u8 wrFlag; /* True if writable */
! 48799: u8 atLast; /* Cursor pointing to the last entry */
! 48800: u8 validNKey; /* True if info.nKey is valid */
! 48801: u8 eState; /* One of the CURSOR_XXX constants (see below) */
! 48802: #ifndef SQLITE_OMIT_INCRBLOB
! 48803: Pgno *aOverflow; /* Cache of overflow page locations */
! 48804: u8 isIncrblobHandle; /* True if this cursor is an incr. io handle */
! 48805: #endif
! 48806: i16 iPage; /* Index of current page in apPage */
! 48807: u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */
! 48808: MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */
! 48809: };
! 48810:
! 48811: /*
! 48812: ** Potential values for BtCursor.eState.
! 48813: **
! 48814: ** CURSOR_VALID:
! 48815: ** Cursor points to a valid entry. getPayload() etc. may be called.
! 48816: **
! 48817: ** CURSOR_INVALID:
! 48818: ** Cursor does not point to a valid entry. This can happen (for example)
! 48819: ** because the table is empty or because BtreeCursorFirst() has not been
! 48820: ** called.
! 48821: **
! 48822: ** CURSOR_REQUIRESEEK:
! 48823: ** The table that this cursor was opened on still exists, but has been
! 48824: ** modified since the cursor was last used. The cursor position is saved
! 48825: ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
! 48826: ** this state, restoreCursorPosition() can be called to attempt to
! 48827: ** seek the cursor to the saved position.
! 48828: **
! 48829: ** CURSOR_FAULT:
! 48830: ** A unrecoverable error (an I/O error or a malloc failure) has occurred
! 48831: ** on a different connection that shares the BtShared cache with this
! 48832: ** cursor. The error has left the cache in an inconsistent state.
! 48833: ** Do nothing else with this cursor. Any attempt to use the cursor
! 48834: ** should return the error code stored in BtCursor.skip
! 48835: */
! 48836: #define CURSOR_INVALID 0
! 48837: #define CURSOR_VALID 1
! 48838: #define CURSOR_REQUIRESEEK 2
! 48839: #define CURSOR_FAULT 3
! 48840:
! 48841: /*
! 48842: ** The database page the PENDING_BYTE occupies. This page is never used.
! 48843: */
! 48844: # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
! 48845:
! 48846: /*
! 48847: ** These macros define the location of the pointer-map entry for a
! 48848: ** database page. The first argument to each is the number of usable
! 48849: ** bytes on each page of the database (often 1024). The second is the
! 48850: ** page number to look up in the pointer map.
! 48851: **
! 48852: ** PTRMAP_PAGENO returns the database page number of the pointer-map
! 48853: ** page that stores the required pointer. PTRMAP_PTROFFSET returns
! 48854: ** the offset of the requested map entry.
! 48855: **
! 48856: ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
! 48857: ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
! 48858: ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
! 48859: ** this test.
! 48860: */
! 48861: #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
! 48862: #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
! 48863: #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
! 48864:
! 48865: /*
! 48866: ** The pointer map is a lookup table that identifies the parent page for
! 48867: ** each child page in the database file. The parent page is the page that
! 48868: ** contains a pointer to the child. Every page in the database contains
! 48869: ** 0 or 1 parent pages. (In this context 'database page' refers
! 48870: ** to any page that is not part of the pointer map itself.) Each pointer map
! 48871: ** entry consists of a single byte 'type' and a 4 byte parent page number.
! 48872: ** The PTRMAP_XXX identifiers below are the valid types.
! 48873: **
! 48874: ** The purpose of the pointer map is to facility moving pages from one
! 48875: ** position in the file to another as part of autovacuum. When a page
! 48876: ** is moved, the pointer in its parent must be updated to point to the
! 48877: ** new location. The pointer map is used to locate the parent page quickly.
! 48878: **
! 48879: ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
! 48880: ** used in this case.
! 48881: **
! 48882: ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
! 48883: ** is not used in this case.
! 48884: **
! 48885: ** PTRMAP_OVERFLOW1: The database page is the first page in a list of
! 48886: ** overflow pages. The page number identifies the page that
! 48887: ** contains the cell with a pointer to this overflow page.
! 48888: **
! 48889: ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
! 48890: ** overflow pages. The page-number identifies the previous
! 48891: ** page in the overflow page list.
! 48892: **
! 48893: ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
! 48894: ** identifies the parent page in the btree.
! 48895: */
! 48896: #define PTRMAP_ROOTPAGE 1
! 48897: #define PTRMAP_FREEPAGE 2
! 48898: #define PTRMAP_OVERFLOW1 3
! 48899: #define PTRMAP_OVERFLOW2 4
! 48900: #define PTRMAP_BTREE 5
! 48901:
! 48902: /* A bunch of assert() statements to check the transaction state variables
! 48903: ** of handle p (type Btree*) are internally consistent.
! 48904: */
! 48905: #define btreeIntegrity(p) \
! 48906: assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
! 48907: assert( p->pBt->inTransaction>=p->inTrans );
! 48908:
! 48909:
! 48910: /*
! 48911: ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
! 48912: ** if the database supports auto-vacuum or not. Because it is used
! 48913: ** within an expression that is an argument to another macro
! 48914: ** (sqliteMallocRaw), it is not possible to use conditional compilation.
! 48915: ** So, this macro is defined instead.
! 48916: */
! 48917: #ifndef SQLITE_OMIT_AUTOVACUUM
! 48918: #define ISAUTOVACUUM (pBt->autoVacuum)
! 48919: #else
! 48920: #define ISAUTOVACUUM 0
! 48921: #endif
! 48922:
! 48923:
! 48924: /*
! 48925: ** This structure is passed around through all the sanity checking routines
! 48926: ** in order to keep track of some global state information.
! 48927: */
! 48928: typedef struct IntegrityCk IntegrityCk;
! 48929: struct IntegrityCk {
! 48930: BtShared *pBt; /* The tree being checked out */
! 48931: Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
! 48932: Pgno nPage; /* Number of pages in the database */
! 48933: int *anRef; /* Number of times each page is referenced */
! 48934: int mxErr; /* Stop accumulating errors when this reaches zero */
! 48935: int nErr; /* Number of messages written to zErrMsg so far */
! 48936: int mallocFailed; /* A memory allocation error has occurred */
! 48937: StrAccum errMsg; /* Accumulate the error message text here */
! 48938: };
! 48939:
! 48940: /*
! 48941: ** Routines to read or write a two- and four-byte big-endian integer values.
! 48942: */
! 48943: #define get2byte(x) ((x)[0]<<8 | (x)[1])
! 48944: #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
! 48945: #define get4byte sqlite3Get4byte
! 48946: #define put4byte sqlite3Put4byte
! 48947:
! 48948: /************** End of btreeInt.h ********************************************/
! 48949: /************** Continuing where we left off in btmutex.c ********************/
! 48950: #ifndef SQLITE_OMIT_SHARED_CACHE
! 48951: #if SQLITE_THREADSAFE
! 48952:
! 48953: /*
! 48954: ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
! 48955: ** set BtShared.db to the database handle associated with p and the
! 48956: ** p->locked boolean to true.
! 48957: */
! 48958: static void lockBtreeMutex(Btree *p){
! 48959: assert( p->locked==0 );
! 48960: assert( sqlite3_mutex_notheld(p->pBt->mutex) );
! 48961: assert( sqlite3_mutex_held(p->db->mutex) );
! 48962:
! 48963: sqlite3_mutex_enter(p->pBt->mutex);
! 48964: p->pBt->db = p->db;
! 48965: p->locked = 1;
! 48966: }
! 48967:
! 48968: /*
! 48969: ** Release the BtShared mutex associated with B-Tree handle p and
! 48970: ** clear the p->locked boolean.
! 48971: */
! 48972: static void unlockBtreeMutex(Btree *p){
! 48973: BtShared *pBt = p->pBt;
! 48974: assert( p->locked==1 );
! 48975: assert( sqlite3_mutex_held(pBt->mutex) );
! 48976: assert( sqlite3_mutex_held(p->db->mutex) );
! 48977: assert( p->db==pBt->db );
! 48978:
! 48979: sqlite3_mutex_leave(pBt->mutex);
! 48980: p->locked = 0;
! 48981: }
! 48982:
! 48983: /*
! 48984: ** Enter a mutex on the given BTree object.
! 48985: **
! 48986: ** If the object is not sharable, then no mutex is ever required
! 48987: ** and this routine is a no-op. The underlying mutex is non-recursive.
! 48988: ** But we keep a reference count in Btree.wantToLock so the behavior
! 48989: ** of this interface is recursive.
! 48990: **
! 48991: ** To avoid deadlocks, multiple Btrees are locked in the same order
! 48992: ** by all database connections. The p->pNext is a list of other
! 48993: ** Btrees belonging to the same database connection as the p Btree
! 48994: ** which need to be locked after p. If we cannot get a lock on
! 48995: ** p, then first unlock all of the others on p->pNext, then wait
! 48996: ** for the lock to become available on p, then relock all of the
! 48997: ** subsequent Btrees that desire a lock.
! 48998: */
! 48999: SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
! 49000: Btree *pLater;
! 49001:
! 49002: /* Some basic sanity checking on the Btree. The list of Btrees
! 49003: ** connected by pNext and pPrev should be in sorted order by
! 49004: ** Btree.pBt value. All elements of the list should belong to
! 49005: ** the same connection. Only shared Btrees are on the list. */
! 49006: assert( p->pNext==0 || p->pNext->pBt>p->pBt );
! 49007: assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
! 49008: assert( p->pNext==0 || p->pNext->db==p->db );
! 49009: assert( p->pPrev==0 || p->pPrev->db==p->db );
! 49010: assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
! 49011:
! 49012: /* Check for locking consistency */
! 49013: assert( !p->locked || p->wantToLock>0 );
! 49014: assert( p->sharable || p->wantToLock==0 );
! 49015:
! 49016: /* We should already hold a lock on the database connection */
! 49017: assert( sqlite3_mutex_held(p->db->mutex) );
! 49018:
! 49019: /* Unless the database is sharable and unlocked, then BtShared.db
! 49020: ** should already be set correctly. */
! 49021: assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
! 49022:
! 49023: if( !p->sharable ) return;
! 49024: p->wantToLock++;
! 49025: if( p->locked ) return;
! 49026:
! 49027: /* In most cases, we should be able to acquire the lock we
! 49028: ** want without having to go throught the ascending lock
! 49029: ** procedure that follows. Just be sure not to block.
! 49030: */
! 49031: if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
! 49032: p->pBt->db = p->db;
! 49033: p->locked = 1;
! 49034: return;
! 49035: }
! 49036:
! 49037: /* To avoid deadlock, first release all locks with a larger
! 49038: ** BtShared address. Then acquire our lock. Then reacquire
! 49039: ** the other BtShared locks that we used to hold in ascending
! 49040: ** order.
! 49041: */
! 49042: for(pLater=p->pNext; pLater; pLater=pLater->pNext){
! 49043: assert( pLater->sharable );
! 49044: assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
! 49045: assert( !pLater->locked || pLater->wantToLock>0 );
! 49046: if( pLater->locked ){
! 49047: unlockBtreeMutex(pLater);
! 49048: }
! 49049: }
! 49050: lockBtreeMutex(p);
! 49051: for(pLater=p->pNext; pLater; pLater=pLater->pNext){
! 49052: if( pLater->wantToLock ){
! 49053: lockBtreeMutex(pLater);
! 49054: }
! 49055: }
! 49056: }
! 49057:
! 49058: /*
! 49059: ** Exit the recursive mutex on a Btree.
! 49060: */
! 49061: SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
! 49062: if( p->sharable ){
! 49063: assert( p->wantToLock>0 );
! 49064: p->wantToLock--;
! 49065: if( p->wantToLock==0 ){
! 49066: unlockBtreeMutex(p);
! 49067: }
! 49068: }
! 49069: }
! 49070:
! 49071: #ifndef NDEBUG
! 49072: /*
! 49073: ** Return true if the BtShared mutex is held on the btree, or if the
! 49074: ** B-Tree is not marked as sharable.
! 49075: **
! 49076: ** This routine is used only from within assert() statements.
! 49077: */
! 49078: SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
! 49079: assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
! 49080: assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
! 49081: assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
! 49082: assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
! 49083:
! 49084: return (p->sharable==0 || p->locked);
! 49085: }
! 49086: #endif
! 49087:
! 49088:
! 49089: #ifndef SQLITE_OMIT_INCRBLOB
! 49090: /*
! 49091: ** Enter and leave a mutex on a Btree given a cursor owned by that
! 49092: ** Btree. These entry points are used by incremental I/O and can be
! 49093: ** omitted if that module is not used.
! 49094: */
! 49095: SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
! 49096: sqlite3BtreeEnter(pCur->pBtree);
! 49097: }
! 49098: SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
! 49099: sqlite3BtreeLeave(pCur->pBtree);
! 49100: }
! 49101: #endif /* SQLITE_OMIT_INCRBLOB */
! 49102:
! 49103:
! 49104: /*
! 49105: ** Enter the mutex on every Btree associated with a database
! 49106: ** connection. This is needed (for example) prior to parsing
! 49107: ** a statement since we will be comparing table and column names
! 49108: ** against all schemas and we do not want those schemas being
! 49109: ** reset out from under us.
! 49110: **
! 49111: ** There is a corresponding leave-all procedures.
! 49112: **
! 49113: ** Enter the mutexes in accending order by BtShared pointer address
! 49114: ** to avoid the possibility of deadlock when two threads with
! 49115: ** two or more btrees in common both try to lock all their btrees
! 49116: ** at the same instant.
! 49117: */
! 49118: SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
! 49119: int i;
! 49120: Btree *p;
! 49121: assert( sqlite3_mutex_held(db->mutex) );
! 49122: for(i=0; i<db->nDb; i++){
! 49123: p = db->aDb[i].pBt;
! 49124: if( p ) sqlite3BtreeEnter(p);
! 49125: }
! 49126: }
! 49127: SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
! 49128: int i;
! 49129: Btree *p;
! 49130: assert( sqlite3_mutex_held(db->mutex) );
! 49131: for(i=0; i<db->nDb; i++){
! 49132: p = db->aDb[i].pBt;
! 49133: if( p ) sqlite3BtreeLeave(p);
! 49134: }
! 49135: }
! 49136:
! 49137: /*
! 49138: ** Return true if a particular Btree requires a lock. Return FALSE if
! 49139: ** no lock is ever required since it is not sharable.
! 49140: */
! 49141: SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
! 49142: return p->sharable;
! 49143: }
! 49144:
! 49145: #ifndef NDEBUG
! 49146: /*
! 49147: ** Return true if the current thread holds the database connection
! 49148: ** mutex and all required BtShared mutexes.
! 49149: **
! 49150: ** This routine is used inside assert() statements only.
! 49151: */
! 49152: SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
! 49153: int i;
! 49154: if( !sqlite3_mutex_held(db->mutex) ){
! 49155: return 0;
! 49156: }
! 49157: for(i=0; i<db->nDb; i++){
! 49158: Btree *p;
! 49159: p = db->aDb[i].pBt;
! 49160: if( p && p->sharable &&
! 49161: (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
! 49162: return 0;
! 49163: }
! 49164: }
! 49165: return 1;
! 49166: }
! 49167: #endif /* NDEBUG */
! 49168:
! 49169: #ifndef NDEBUG
! 49170: /*
! 49171: ** Return true if the correct mutexes are held for accessing the
! 49172: ** db->aDb[iDb].pSchema structure. The mutexes required for schema
! 49173: ** access are:
! 49174: **
! 49175: ** (1) The mutex on db
! 49176: ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
! 49177: **
! 49178: ** If pSchema is not NULL, then iDb is computed from pSchema and
! 49179: ** db using sqlite3SchemaToIndex().
! 49180: */
! 49181: SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
! 49182: Btree *p;
! 49183: assert( db!=0 );
! 49184: if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
! 49185: assert( iDb>=0 && iDb<db->nDb );
! 49186: if( !sqlite3_mutex_held(db->mutex) ) return 0;
! 49187: if( iDb==1 ) return 1;
! 49188: p = db->aDb[iDb].pBt;
! 49189: assert( p!=0 );
! 49190: return p->sharable==0 || p->locked==1;
! 49191: }
! 49192: #endif /* NDEBUG */
! 49193:
! 49194: #else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
! 49195: /*
! 49196: ** The following are special cases for mutex enter routines for use
! 49197: ** in single threaded applications that use shared cache. Except for
! 49198: ** these two routines, all mutex operations are no-ops in that case and
! 49199: ** are null #defines in btree.h.
! 49200: **
! 49201: ** If shared cache is disabled, then all btree mutex routines, including
! 49202: ** the ones below, are no-ops and are null #defines in btree.h.
! 49203: */
! 49204:
! 49205: SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
! 49206: p->pBt->db = p->db;
! 49207: }
! 49208: SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
! 49209: int i;
! 49210: for(i=0; i<db->nDb; i++){
! 49211: Btree *p = db->aDb[i].pBt;
! 49212: if( p ){
! 49213: p->pBt->db = p->db;
! 49214: }
! 49215: }
! 49216: }
! 49217: #endif /* if SQLITE_THREADSAFE */
! 49218: #endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
! 49219:
! 49220: /************** End of btmutex.c *********************************************/
! 49221: /************** Begin file btree.c *******************************************/
! 49222: /*
! 49223: ** 2004 April 6
! 49224: **
! 49225: ** The author disclaims copyright to this source code. In place of
! 49226: ** a legal notice, here is a blessing:
! 49227: **
! 49228: ** May you do good and not evil.
! 49229: ** May you find forgiveness for yourself and forgive others.
! 49230: ** May you share freely, never taking more than you give.
! 49231: **
! 49232: *************************************************************************
! 49233: ** This file implements a external (disk-based) database using BTrees.
! 49234: ** See the header comment on "btreeInt.h" for additional information.
! 49235: ** Including a description of file format and an overview of operation.
! 49236: */
! 49237:
! 49238: /*
! 49239: ** The header string that appears at the beginning of every
! 49240: ** SQLite database.
! 49241: */
! 49242: static const char zMagicHeader[] = SQLITE_FILE_HEADER;
! 49243:
! 49244: /*
! 49245: ** Set this global variable to 1 to enable tracing using the TRACE
! 49246: ** macro.
! 49247: */
! 49248: #if 0
! 49249: int sqlite3BtreeTrace=1; /* True to enable tracing */
! 49250: # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
! 49251: #else
! 49252: # define TRACE(X)
! 49253: #endif
! 49254:
! 49255: /*
! 49256: ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
! 49257: ** But if the value is zero, make it 65536.
! 49258: **
! 49259: ** This routine is used to extract the "offset to cell content area" value
! 49260: ** from the header of a btree page. If the page size is 65536 and the page
! 49261: ** is empty, the offset should be 65536, but the 2-byte value stores zero.
! 49262: ** This routine makes the necessary adjustment to 65536.
! 49263: */
! 49264: #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
! 49265:
! 49266: #ifndef SQLITE_OMIT_SHARED_CACHE
! 49267: /*
! 49268: ** A list of BtShared objects that are eligible for participation
! 49269: ** in shared cache. This variable has file scope during normal builds,
! 49270: ** but the test harness needs to access it so we make it global for
! 49271: ** test builds.
! 49272: **
! 49273: ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
! 49274: */
! 49275: #ifdef SQLITE_TEST
! 49276: SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
! 49277: #else
! 49278: static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
! 49279: #endif
! 49280: #endif /* SQLITE_OMIT_SHARED_CACHE */
! 49281:
! 49282: #ifndef SQLITE_OMIT_SHARED_CACHE
! 49283: /*
! 49284: ** Enable or disable the shared pager and schema features.
! 49285: **
! 49286: ** This routine has no effect on existing database connections.
! 49287: ** The shared cache setting effects only future calls to
! 49288: ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
! 49289: */
! 49290: SQLITE_API int sqlite3_enable_shared_cache(int enable){
! 49291: sqlite3GlobalConfig.sharedCacheEnabled = enable;
! 49292: return SQLITE_OK;
! 49293: }
! 49294: #endif
! 49295:
! 49296:
! 49297:
! 49298: #ifdef SQLITE_OMIT_SHARED_CACHE
! 49299: /*
! 49300: ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
! 49301: ** and clearAllSharedCacheTableLocks()
! 49302: ** manipulate entries in the BtShared.pLock linked list used to store
! 49303: ** shared-cache table level locks. If the library is compiled with the
! 49304: ** shared-cache feature disabled, then there is only ever one user
! 49305: ** of each BtShared structure and so this locking is not necessary.
! 49306: ** So define the lock related functions as no-ops.
! 49307: */
! 49308: #define querySharedCacheTableLock(a,b,c) SQLITE_OK
! 49309: #define setSharedCacheTableLock(a,b,c) SQLITE_OK
! 49310: #define clearAllSharedCacheTableLocks(a)
! 49311: #define downgradeAllSharedCacheTableLocks(a)
! 49312: #define hasSharedCacheTableLock(a,b,c,d) 1
! 49313: #define hasReadConflicts(a, b) 0
! 49314: #endif
! 49315:
! 49316: #ifndef SQLITE_OMIT_SHARED_CACHE
! 49317:
! 49318: #ifdef SQLITE_DEBUG
! 49319: /*
! 49320: **** This function is only used as part of an assert() statement. ***
! 49321: **
! 49322: ** Check to see if pBtree holds the required locks to read or write to the
! 49323: ** table with root page iRoot. Return 1 if it does and 0 if not.
! 49324: **
! 49325: ** For example, when writing to a table with root-page iRoot via
! 49326: ** Btree connection pBtree:
! 49327: **
! 49328: ** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
! 49329: **
! 49330: ** When writing to an index that resides in a sharable database, the
! 49331: ** caller should have first obtained a lock specifying the root page of
! 49332: ** the corresponding table. This makes things a bit more complicated,
! 49333: ** as this module treats each table as a separate structure. To determine
! 49334: ** the table corresponding to the index being written, this
! 49335: ** function has to search through the database schema.
! 49336: **
! 49337: ** Instead of a lock on the table/index rooted at page iRoot, the caller may
! 49338: ** hold a write-lock on the schema table (root page 1). This is also
! 49339: ** acceptable.
! 49340: */
! 49341: static int hasSharedCacheTableLock(
! 49342: Btree *pBtree, /* Handle that must hold lock */
! 49343: Pgno iRoot, /* Root page of b-tree */
! 49344: int isIndex, /* True if iRoot is the root of an index b-tree */
! 49345: int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
! 49346: ){
! 49347: Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
! 49348: Pgno iTab = 0;
! 49349: BtLock *pLock;
! 49350:
! 49351: /* If this database is not shareable, or if the client is reading
! 49352: ** and has the read-uncommitted flag set, then no lock is required.
! 49353: ** Return true immediately.
! 49354: */
! 49355: if( (pBtree->sharable==0)
! 49356: || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
! 49357: ){
! 49358: return 1;
! 49359: }
! 49360:
! 49361: /* If the client is reading or writing an index and the schema is
! 49362: ** not loaded, then it is too difficult to actually check to see if
! 49363: ** the correct locks are held. So do not bother - just return true.
! 49364: ** This case does not come up very often anyhow.
! 49365: */
! 49366: if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
! 49367: return 1;
! 49368: }
! 49369:
! 49370: /* Figure out the root-page that the lock should be held on. For table
! 49371: ** b-trees, this is just the root page of the b-tree being read or
! 49372: ** written. For index b-trees, it is the root page of the associated
! 49373: ** table. */
! 49374: if( isIndex ){
! 49375: HashElem *p;
! 49376: for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
! 49377: Index *pIdx = (Index *)sqliteHashData(p);
! 49378: if( pIdx->tnum==(int)iRoot ){
! 49379: iTab = pIdx->pTable->tnum;
! 49380: }
! 49381: }
! 49382: }else{
! 49383: iTab = iRoot;
! 49384: }
! 49385:
! 49386: /* Search for the required lock. Either a write-lock on root-page iTab, a
! 49387: ** write-lock on the schema table, or (if the client is reading) a
! 49388: ** read-lock on iTab will suffice. Return 1 if any of these are found. */
! 49389: for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
! 49390: if( pLock->pBtree==pBtree
! 49391: && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
! 49392: && pLock->eLock>=eLockType
! 49393: ){
! 49394: return 1;
! 49395: }
! 49396: }
! 49397:
! 49398: /* Failed to find the required lock. */
! 49399: return 0;
! 49400: }
! 49401: #endif /* SQLITE_DEBUG */
! 49402:
! 49403: #ifdef SQLITE_DEBUG
! 49404: /*
! 49405: **** This function may be used as part of assert() statements only. ****
! 49406: **
! 49407: ** Return true if it would be illegal for pBtree to write into the
! 49408: ** table or index rooted at iRoot because other shared connections are
! 49409: ** simultaneously reading that same table or index.
! 49410: **
! 49411: ** It is illegal for pBtree to write if some other Btree object that
! 49412: ** shares the same BtShared object is currently reading or writing
! 49413: ** the iRoot table. Except, if the other Btree object has the
! 49414: ** read-uncommitted flag set, then it is OK for the other object to
! 49415: ** have a read cursor.
! 49416: **
! 49417: ** For example, before writing to any part of the table or index
! 49418: ** rooted at page iRoot, one should call:
! 49419: **
! 49420: ** assert( !hasReadConflicts(pBtree, iRoot) );
! 49421: */
! 49422: static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
! 49423: BtCursor *p;
! 49424: for(p=pBtree->pBt->pCursor; p; p=p->pNext){
! 49425: if( p->pgnoRoot==iRoot
! 49426: && p->pBtree!=pBtree
! 49427: && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
! 49428: ){
! 49429: return 1;
! 49430: }
! 49431: }
! 49432: return 0;
! 49433: }
! 49434: #endif /* #ifdef SQLITE_DEBUG */
! 49435:
! 49436: /*
! 49437: ** Query to see if Btree handle p may obtain a lock of type eLock
! 49438: ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
! 49439: ** SQLITE_OK if the lock may be obtained (by calling
! 49440: ** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
! 49441: */
! 49442: static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
! 49443: BtShared *pBt = p->pBt;
! 49444: BtLock *pIter;
! 49445:
! 49446: assert( sqlite3BtreeHoldsMutex(p) );
! 49447: assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
! 49448: assert( p->db!=0 );
! 49449: assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
! 49450:
! 49451: /* If requesting a write-lock, then the Btree must have an open write
! 49452: ** transaction on this file. And, obviously, for this to be so there
! 49453: ** must be an open write transaction on the file itself.
! 49454: */
! 49455: assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
! 49456: assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
! 49457:
! 49458: /* This routine is a no-op if the shared-cache is not enabled */
! 49459: if( !p->sharable ){
! 49460: return SQLITE_OK;
! 49461: }
! 49462:
! 49463: /* If some other connection is holding an exclusive lock, the
! 49464: ** requested lock may not be obtained.
! 49465: */
! 49466: if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
! 49467: sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
! 49468: return SQLITE_LOCKED_SHAREDCACHE;
! 49469: }
! 49470:
! 49471: for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
! 49472: /* The condition (pIter->eLock!=eLock) in the following if(...)
! 49473: ** statement is a simplification of:
! 49474: **
! 49475: ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
! 49476: **
! 49477: ** since we know that if eLock==WRITE_LOCK, then no other connection
! 49478: ** may hold a WRITE_LOCK on any table in this file (since there can
! 49479: ** only be a single writer).
! 49480: */
! 49481: assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
! 49482: assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
! 49483: if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
! 49484: sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
! 49485: if( eLock==WRITE_LOCK ){
! 49486: assert( p==pBt->pWriter );
! 49487: pBt->btsFlags |= BTS_PENDING;
! 49488: }
! 49489: return SQLITE_LOCKED_SHAREDCACHE;
! 49490: }
! 49491: }
! 49492: return SQLITE_OK;
! 49493: }
! 49494: #endif /* !SQLITE_OMIT_SHARED_CACHE */
! 49495:
! 49496: #ifndef SQLITE_OMIT_SHARED_CACHE
! 49497: /*
! 49498: ** Add a lock on the table with root-page iTable to the shared-btree used
! 49499: ** by Btree handle p. Parameter eLock must be either READ_LOCK or
! 49500: ** WRITE_LOCK.
! 49501: **
! 49502: ** This function assumes the following:
! 49503: **
! 49504: ** (a) The specified Btree object p is connected to a sharable
! 49505: ** database (one with the BtShared.sharable flag set), and
! 49506: **
! 49507: ** (b) No other Btree objects hold a lock that conflicts
! 49508: ** with the requested lock (i.e. querySharedCacheTableLock() has
! 49509: ** already been called and returned SQLITE_OK).
! 49510: **
! 49511: ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
! 49512: ** is returned if a malloc attempt fails.
! 49513: */
! 49514: static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
! 49515: BtShared *pBt = p->pBt;
! 49516: BtLock *pLock = 0;
! 49517: BtLock *pIter;
! 49518:
! 49519: assert( sqlite3BtreeHoldsMutex(p) );
! 49520: assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
! 49521: assert( p->db!=0 );
! 49522:
! 49523: /* A connection with the read-uncommitted flag set will never try to
! 49524: ** obtain a read-lock using this function. The only read-lock obtained
! 49525: ** by a connection in read-uncommitted mode is on the sqlite_master
! 49526: ** table, and that lock is obtained in BtreeBeginTrans(). */
! 49527: assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
! 49528:
! 49529: /* This function should only be called on a sharable b-tree after it
! 49530: ** has been determined that no other b-tree holds a conflicting lock. */
! 49531: assert( p->sharable );
! 49532: assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
! 49533:
! 49534: /* First search the list for an existing lock on this table. */
! 49535: for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
! 49536: if( pIter->iTable==iTable && pIter->pBtree==p ){
! 49537: pLock = pIter;
! 49538: break;
! 49539: }
! 49540: }
! 49541:
! 49542: /* If the above search did not find a BtLock struct associating Btree p
! 49543: ** with table iTable, allocate one and link it into the list.
! 49544: */
! 49545: if( !pLock ){
! 49546: pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
! 49547: if( !pLock ){
! 49548: return SQLITE_NOMEM;
! 49549: }
! 49550: pLock->iTable = iTable;
! 49551: pLock->pBtree = p;
! 49552: pLock->pNext = pBt->pLock;
! 49553: pBt->pLock = pLock;
! 49554: }
! 49555:
! 49556: /* Set the BtLock.eLock variable to the maximum of the current lock
! 49557: ** and the requested lock. This means if a write-lock was already held
! 49558: ** and a read-lock requested, we don't incorrectly downgrade the lock.
! 49559: */
! 49560: assert( WRITE_LOCK>READ_LOCK );
! 49561: if( eLock>pLock->eLock ){
! 49562: pLock->eLock = eLock;
! 49563: }
! 49564:
! 49565: return SQLITE_OK;
! 49566: }
! 49567: #endif /* !SQLITE_OMIT_SHARED_CACHE */
! 49568:
! 49569: #ifndef SQLITE_OMIT_SHARED_CACHE
! 49570: /*
! 49571: ** Release all the table locks (locks obtained via calls to
! 49572: ** the setSharedCacheTableLock() procedure) held by Btree object p.
! 49573: **
! 49574: ** This function assumes that Btree p has an open read or write
! 49575: ** transaction. If it does not, then the BTS_PENDING flag
! 49576: ** may be incorrectly cleared.
! 49577: */
! 49578: static void clearAllSharedCacheTableLocks(Btree *p){
! 49579: BtShared *pBt = p->pBt;
! 49580: BtLock **ppIter = &pBt->pLock;
! 49581:
! 49582: assert( sqlite3BtreeHoldsMutex(p) );
! 49583: assert( p->sharable || 0==*ppIter );
! 49584: assert( p->inTrans>0 );
! 49585:
! 49586: while( *ppIter ){
! 49587: BtLock *pLock = *ppIter;
! 49588: assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
! 49589: assert( pLock->pBtree->inTrans>=pLock->eLock );
! 49590: if( pLock->pBtree==p ){
! 49591: *ppIter = pLock->pNext;
! 49592: assert( pLock->iTable!=1 || pLock==&p->lock );
! 49593: if( pLock->iTable!=1 ){
! 49594: sqlite3_free(pLock);
! 49595: }
! 49596: }else{
! 49597: ppIter = &pLock->pNext;
! 49598: }
! 49599: }
! 49600:
! 49601: assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
! 49602: if( pBt->pWriter==p ){
! 49603: pBt->pWriter = 0;
! 49604: pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
! 49605: }else if( pBt->nTransaction==2 ){
! 49606: /* This function is called when Btree p is concluding its
! 49607: ** transaction. If there currently exists a writer, and p is not
! 49608: ** that writer, then the number of locks held by connections other
! 49609: ** than the writer must be about to drop to zero. In this case
! 49610: ** set the BTS_PENDING flag to 0.
! 49611: **
! 49612: ** If there is not currently a writer, then BTS_PENDING must
! 49613: ** be zero already. So this next line is harmless in that case.
! 49614: */
! 49615: pBt->btsFlags &= ~BTS_PENDING;
! 49616: }
! 49617: }
! 49618:
! 49619: /*
! 49620: ** This function changes all write-locks held by Btree p into read-locks.
! 49621: */
! 49622: static void downgradeAllSharedCacheTableLocks(Btree *p){
! 49623: BtShared *pBt = p->pBt;
! 49624: if( pBt->pWriter==p ){
! 49625: BtLock *pLock;
! 49626: pBt->pWriter = 0;
! 49627: pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
! 49628: for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
! 49629: assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
! 49630: pLock->eLock = READ_LOCK;
! 49631: }
! 49632: }
! 49633: }
! 49634:
! 49635: #endif /* SQLITE_OMIT_SHARED_CACHE */
! 49636:
! 49637: static void releasePage(MemPage *pPage); /* Forward reference */
! 49638:
! 49639: /*
! 49640: ***** This routine is used inside of assert() only ****
! 49641: **
! 49642: ** Verify that the cursor holds the mutex on its BtShared
! 49643: */
! 49644: #ifdef SQLITE_DEBUG
! 49645: static int cursorHoldsMutex(BtCursor *p){
! 49646: return sqlite3_mutex_held(p->pBt->mutex);
! 49647: }
! 49648: #endif
! 49649:
! 49650:
! 49651: #ifndef SQLITE_OMIT_INCRBLOB
! 49652: /*
! 49653: ** Invalidate the overflow page-list cache for cursor pCur, if any.
! 49654: */
! 49655: static void invalidateOverflowCache(BtCursor *pCur){
! 49656: assert( cursorHoldsMutex(pCur) );
! 49657: sqlite3_free(pCur->aOverflow);
! 49658: pCur->aOverflow = 0;
! 49659: }
! 49660:
! 49661: /*
! 49662: ** Invalidate the overflow page-list cache for all cursors opened
! 49663: ** on the shared btree structure pBt.
! 49664: */
! 49665: static void invalidateAllOverflowCache(BtShared *pBt){
! 49666: BtCursor *p;
! 49667: assert( sqlite3_mutex_held(pBt->mutex) );
! 49668: for(p=pBt->pCursor; p; p=p->pNext){
! 49669: invalidateOverflowCache(p);
! 49670: }
! 49671: }
! 49672:
! 49673: /*
! 49674: ** This function is called before modifying the contents of a table
! 49675: ** to invalidate any incrblob cursors that are open on the
! 49676: ** row or one of the rows being modified.
! 49677: **
! 49678: ** If argument isClearTable is true, then the entire contents of the
! 49679: ** table is about to be deleted. In this case invalidate all incrblob
! 49680: ** cursors open on any row within the table with root-page pgnoRoot.
! 49681: **
! 49682: ** Otherwise, if argument isClearTable is false, then the row with
! 49683: ** rowid iRow is being replaced or deleted. In this case invalidate
! 49684: ** only those incrblob cursors open on that specific row.
! 49685: */
! 49686: static void invalidateIncrblobCursors(
! 49687: Btree *pBtree, /* The database file to check */
! 49688: i64 iRow, /* The rowid that might be changing */
! 49689: int isClearTable /* True if all rows are being deleted */
! 49690: ){
! 49691: BtCursor *p;
! 49692: BtShared *pBt = pBtree->pBt;
! 49693: assert( sqlite3BtreeHoldsMutex(pBtree) );
! 49694: for(p=pBt->pCursor; p; p=p->pNext){
! 49695: if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
! 49696: p->eState = CURSOR_INVALID;
! 49697: }
! 49698: }
! 49699: }
! 49700:
! 49701: #else
! 49702: /* Stub functions when INCRBLOB is omitted */
! 49703: #define invalidateOverflowCache(x)
! 49704: #define invalidateAllOverflowCache(x)
! 49705: #define invalidateIncrblobCursors(x,y,z)
! 49706: #endif /* SQLITE_OMIT_INCRBLOB */
! 49707:
! 49708: /*
! 49709: ** Set bit pgno of the BtShared.pHasContent bitvec. This is called
! 49710: ** when a page that previously contained data becomes a free-list leaf
! 49711: ** page.
! 49712: **
! 49713: ** The BtShared.pHasContent bitvec exists to work around an obscure
! 49714: ** bug caused by the interaction of two useful IO optimizations surrounding
! 49715: ** free-list leaf pages:
! 49716: **
! 49717: ** 1) When all data is deleted from a page and the page becomes
! 49718: ** a free-list leaf page, the page is not written to the database
! 49719: ** (as free-list leaf pages contain no meaningful data). Sometimes
! 49720: ** such a page is not even journalled (as it will not be modified,
! 49721: ** why bother journalling it?).
! 49722: **
! 49723: ** 2) When a free-list leaf page is reused, its content is not read
! 49724: ** from the database or written to the journal file (why should it
! 49725: ** be, if it is not at all meaningful?).
! 49726: **
! 49727: ** By themselves, these optimizations work fine and provide a handy
! 49728: ** performance boost to bulk delete or insert operations. However, if
! 49729: ** a page is moved to the free-list and then reused within the same
! 49730: ** transaction, a problem comes up. If the page is not journalled when
! 49731: ** it is moved to the free-list and it is also not journalled when it
! 49732: ** is extracted from the free-list and reused, then the original data
! 49733: ** may be lost. In the event of a rollback, it may not be possible
! 49734: ** to restore the database to its original configuration.
! 49735: **
! 49736: ** The solution is the BtShared.pHasContent bitvec. Whenever a page is
! 49737: ** moved to become a free-list leaf page, the corresponding bit is
! 49738: ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
! 49739: ** optimization 2 above is omitted if the corresponding bit is already
! 49740: ** set in BtShared.pHasContent. The contents of the bitvec are cleared
! 49741: ** at the end of every transaction.
! 49742: */
! 49743: static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
! 49744: int rc = SQLITE_OK;
! 49745: if( !pBt->pHasContent ){
! 49746: assert( pgno<=pBt->nPage );
! 49747: pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
! 49748: if( !pBt->pHasContent ){
! 49749: rc = SQLITE_NOMEM;
! 49750: }
! 49751: }
! 49752: if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
! 49753: rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
! 49754: }
! 49755: return rc;
! 49756: }
! 49757:
! 49758: /*
! 49759: ** Query the BtShared.pHasContent vector.
! 49760: **
! 49761: ** This function is called when a free-list leaf page is removed from the
! 49762: ** free-list for reuse. It returns false if it is safe to retrieve the
! 49763: ** page from the pager layer with the 'no-content' flag set. True otherwise.
! 49764: */
! 49765: static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
! 49766: Bitvec *p = pBt->pHasContent;
! 49767: return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
! 49768: }
! 49769:
! 49770: /*
! 49771: ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
! 49772: ** invoked at the conclusion of each write-transaction.
! 49773: */
! 49774: static void btreeClearHasContent(BtShared *pBt){
! 49775: sqlite3BitvecDestroy(pBt->pHasContent);
! 49776: pBt->pHasContent = 0;
! 49777: }
! 49778:
! 49779: /*
! 49780: ** Save the current cursor position in the variables BtCursor.nKey
! 49781: ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
! 49782: **
! 49783: ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
! 49784: ** prior to calling this routine.
! 49785: */
! 49786: static int saveCursorPosition(BtCursor *pCur){
! 49787: int rc;
! 49788:
! 49789: assert( CURSOR_VALID==pCur->eState );
! 49790: assert( 0==pCur->pKey );
! 49791: assert( cursorHoldsMutex(pCur) );
! 49792:
! 49793: rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
! 49794: assert( rc==SQLITE_OK ); /* KeySize() cannot fail */
! 49795:
! 49796: /* If this is an intKey table, then the above call to BtreeKeySize()
! 49797: ** stores the integer key in pCur->nKey. In this case this value is
! 49798: ** all that is required. Otherwise, if pCur is not open on an intKey
! 49799: ** table, then malloc space for and store the pCur->nKey bytes of key
! 49800: ** data.
! 49801: */
! 49802: if( 0==pCur->apPage[0]->intKey ){
! 49803: void *pKey = sqlite3Malloc( (int)pCur->nKey );
! 49804: if( pKey ){
! 49805: rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
! 49806: if( rc==SQLITE_OK ){
! 49807: pCur->pKey = pKey;
! 49808: }else{
! 49809: sqlite3_free(pKey);
! 49810: }
! 49811: }else{
! 49812: rc = SQLITE_NOMEM;
! 49813: }
! 49814: }
! 49815: assert( !pCur->apPage[0]->intKey || !pCur->pKey );
! 49816:
! 49817: if( rc==SQLITE_OK ){
! 49818: int i;
! 49819: for(i=0; i<=pCur->iPage; i++){
! 49820: releasePage(pCur->apPage[i]);
! 49821: pCur->apPage[i] = 0;
! 49822: }
! 49823: pCur->iPage = -1;
! 49824: pCur->eState = CURSOR_REQUIRESEEK;
! 49825: }
! 49826:
! 49827: invalidateOverflowCache(pCur);
! 49828: return rc;
! 49829: }
! 49830:
! 49831: /*
! 49832: ** Save the positions of all cursors (except pExcept) that are open on
! 49833: ** the table with root-page iRoot. Usually, this is called just before cursor
! 49834: ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
! 49835: */
! 49836: static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
! 49837: BtCursor *p;
! 49838: assert( sqlite3_mutex_held(pBt->mutex) );
! 49839: assert( pExcept==0 || pExcept->pBt==pBt );
! 49840: for(p=pBt->pCursor; p; p=p->pNext){
! 49841: if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
! 49842: p->eState==CURSOR_VALID ){
! 49843: int rc = saveCursorPosition(p);
! 49844: if( SQLITE_OK!=rc ){
! 49845: return rc;
! 49846: }
! 49847: }
! 49848: }
! 49849: return SQLITE_OK;
! 49850: }
! 49851:
! 49852: /*
! 49853: ** Clear the current cursor position.
! 49854: */
! 49855: SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
! 49856: assert( cursorHoldsMutex(pCur) );
! 49857: sqlite3_free(pCur->pKey);
! 49858: pCur->pKey = 0;
! 49859: pCur->eState = CURSOR_INVALID;
! 49860: }
! 49861:
! 49862: /*
! 49863: ** In this version of BtreeMoveto, pKey is a packed index record
! 49864: ** such as is generated by the OP_MakeRecord opcode. Unpack the
! 49865: ** record and then call BtreeMovetoUnpacked() to do the work.
! 49866: */
! 49867: static int btreeMoveto(
! 49868: BtCursor *pCur, /* Cursor open on the btree to be searched */
! 49869: const void *pKey, /* Packed key if the btree is an index */
! 49870: i64 nKey, /* Integer key for tables. Size of pKey for indices */
! 49871: int bias, /* Bias search to the high end */
! 49872: int *pRes /* Write search results here */
! 49873: ){
! 49874: int rc; /* Status code */
! 49875: UnpackedRecord *pIdxKey; /* Unpacked index key */
! 49876: char aSpace[150]; /* Temp space for pIdxKey - to avoid a malloc */
! 49877: char *pFree = 0;
! 49878:
! 49879: if( pKey ){
! 49880: assert( nKey==(i64)(int)nKey );
! 49881: pIdxKey = sqlite3VdbeAllocUnpackedRecord(
! 49882: pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
! 49883: );
! 49884: if( pIdxKey==0 ) return SQLITE_NOMEM;
! 49885: sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
! 49886: }else{
! 49887: pIdxKey = 0;
! 49888: }
! 49889: rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
! 49890: if( pFree ){
! 49891: sqlite3DbFree(pCur->pKeyInfo->db, pFree);
! 49892: }
! 49893: return rc;
! 49894: }
! 49895:
! 49896: /*
! 49897: ** Restore the cursor to the position it was in (or as close to as possible)
! 49898: ** when saveCursorPosition() was called. Note that this call deletes the
! 49899: ** saved position info stored by saveCursorPosition(), so there can be
! 49900: ** at most one effective restoreCursorPosition() call after each
! 49901: ** saveCursorPosition().
! 49902: */
! 49903: static int btreeRestoreCursorPosition(BtCursor *pCur){
! 49904: int rc;
! 49905: assert( cursorHoldsMutex(pCur) );
! 49906: assert( pCur->eState>=CURSOR_REQUIRESEEK );
! 49907: if( pCur->eState==CURSOR_FAULT ){
! 49908: return pCur->skipNext;
! 49909: }
! 49910: pCur->eState = CURSOR_INVALID;
! 49911: rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
! 49912: if( rc==SQLITE_OK ){
! 49913: sqlite3_free(pCur->pKey);
! 49914: pCur->pKey = 0;
! 49915: assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
! 49916: }
! 49917: return rc;
! 49918: }
! 49919:
! 49920: #define restoreCursorPosition(p) \
! 49921: (p->eState>=CURSOR_REQUIRESEEK ? \
! 49922: btreeRestoreCursorPosition(p) : \
! 49923: SQLITE_OK)
! 49924:
! 49925: /*
! 49926: ** Determine whether or not a cursor has moved from the position it
! 49927: ** was last placed at. Cursors can move when the row they are pointing
! 49928: ** at is deleted out from under them.
! 49929: **
! 49930: ** This routine returns an error code if something goes wrong. The
! 49931: ** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
! 49932: */
! 49933: SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
! 49934: int rc;
! 49935:
! 49936: rc = restoreCursorPosition(pCur);
! 49937: if( rc ){
! 49938: *pHasMoved = 1;
! 49939: return rc;
! 49940: }
! 49941: if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){
! 49942: *pHasMoved = 1;
! 49943: }else{
! 49944: *pHasMoved = 0;
! 49945: }
! 49946: return SQLITE_OK;
! 49947: }
! 49948:
! 49949: #ifndef SQLITE_OMIT_AUTOVACUUM
! 49950: /*
! 49951: ** Given a page number of a regular database page, return the page
! 49952: ** number for the pointer-map page that contains the entry for the
! 49953: ** input page number.
! 49954: **
! 49955: ** Return 0 (not a valid page) for pgno==1 since there is
! 49956: ** no pointer map associated with page 1. The integrity_check logic
! 49957: ** requires that ptrmapPageno(*,1)!=1.
! 49958: */
! 49959: static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
! 49960: int nPagesPerMapPage;
! 49961: Pgno iPtrMap, ret;
! 49962: assert( sqlite3_mutex_held(pBt->mutex) );
! 49963: if( pgno<2 ) return 0;
! 49964: nPagesPerMapPage = (pBt->usableSize/5)+1;
! 49965: iPtrMap = (pgno-2)/nPagesPerMapPage;
! 49966: ret = (iPtrMap*nPagesPerMapPage) + 2;
! 49967: if( ret==PENDING_BYTE_PAGE(pBt) ){
! 49968: ret++;
! 49969: }
! 49970: return ret;
! 49971: }
! 49972:
! 49973: /*
! 49974: ** Write an entry into the pointer map.
! 49975: **
! 49976: ** This routine updates the pointer map entry for page number 'key'
! 49977: ** so that it maps to type 'eType' and parent page number 'pgno'.
! 49978: **
! 49979: ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
! 49980: ** a no-op. If an error occurs, the appropriate error code is written
! 49981: ** into *pRC.
! 49982: */
! 49983: static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
! 49984: DbPage *pDbPage; /* The pointer map page */
! 49985: u8 *pPtrmap; /* The pointer map data */
! 49986: Pgno iPtrmap; /* The pointer map page number */
! 49987: int offset; /* Offset in pointer map page */
! 49988: int rc; /* Return code from subfunctions */
! 49989:
! 49990: if( *pRC ) return;
! 49991:
! 49992: assert( sqlite3_mutex_held(pBt->mutex) );
! 49993: /* The master-journal page number must never be used as a pointer map page */
! 49994: assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
! 49995:
! 49996: assert( pBt->autoVacuum );
! 49997: if( key==0 ){
! 49998: *pRC = SQLITE_CORRUPT_BKPT;
! 49999: return;
! 50000: }
! 50001: iPtrmap = PTRMAP_PAGENO(pBt, key);
! 50002: rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
! 50003: if( rc!=SQLITE_OK ){
! 50004: *pRC = rc;
! 50005: return;
! 50006: }
! 50007: offset = PTRMAP_PTROFFSET(iPtrmap, key);
! 50008: if( offset<0 ){
! 50009: *pRC = SQLITE_CORRUPT_BKPT;
! 50010: goto ptrmap_exit;
! 50011: }
! 50012: assert( offset <= (int)pBt->usableSize-5 );
! 50013: pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
! 50014:
! 50015: if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
! 50016: TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
! 50017: *pRC= rc = sqlite3PagerWrite(pDbPage);
! 50018: if( rc==SQLITE_OK ){
! 50019: pPtrmap[offset] = eType;
! 50020: put4byte(&pPtrmap[offset+1], parent);
! 50021: }
! 50022: }
! 50023:
! 50024: ptrmap_exit:
! 50025: sqlite3PagerUnref(pDbPage);
! 50026: }
! 50027:
! 50028: /*
! 50029: ** Read an entry from the pointer map.
! 50030: **
! 50031: ** This routine retrieves the pointer map entry for page 'key', writing
! 50032: ** the type and parent page number to *pEType and *pPgno respectively.
! 50033: ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
! 50034: */
! 50035: static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
! 50036: DbPage *pDbPage; /* The pointer map page */
! 50037: int iPtrmap; /* Pointer map page index */
! 50038: u8 *pPtrmap; /* Pointer map page data */
! 50039: int offset; /* Offset of entry in pointer map */
! 50040: int rc;
! 50041:
! 50042: assert( sqlite3_mutex_held(pBt->mutex) );
! 50043:
! 50044: iPtrmap = PTRMAP_PAGENO(pBt, key);
! 50045: rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
! 50046: if( rc!=0 ){
! 50047: return rc;
! 50048: }
! 50049: pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
! 50050:
! 50051: offset = PTRMAP_PTROFFSET(iPtrmap, key);
! 50052: if( offset<0 ){
! 50053: sqlite3PagerUnref(pDbPage);
! 50054: return SQLITE_CORRUPT_BKPT;
! 50055: }
! 50056: assert( offset <= (int)pBt->usableSize-5 );
! 50057: assert( pEType!=0 );
! 50058: *pEType = pPtrmap[offset];
! 50059: if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
! 50060:
! 50061: sqlite3PagerUnref(pDbPage);
! 50062: if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
! 50063: return SQLITE_OK;
! 50064: }
! 50065:
! 50066: #else /* if defined SQLITE_OMIT_AUTOVACUUM */
! 50067: #define ptrmapPut(w,x,y,z,rc)
! 50068: #define ptrmapGet(w,x,y,z) SQLITE_OK
! 50069: #define ptrmapPutOvflPtr(x, y, rc)
! 50070: #endif
! 50071:
! 50072: /*
! 50073: ** Given a btree page and a cell index (0 means the first cell on
! 50074: ** the page, 1 means the second cell, and so forth) return a pointer
! 50075: ** to the cell content.
! 50076: **
! 50077: ** This routine works only for pages that do not contain overflow cells.
! 50078: */
! 50079: #define findCell(P,I) \
! 50080: ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
! 50081: #define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
! 50082:
! 50083:
! 50084: /*
! 50085: ** This a more complex version of findCell() that works for
! 50086: ** pages that do contain overflow cells.
! 50087: */
! 50088: static u8 *findOverflowCell(MemPage *pPage, int iCell){
! 50089: int i;
! 50090: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 50091: for(i=pPage->nOverflow-1; i>=0; i--){
! 50092: int k;
! 50093: struct _OvflCell *pOvfl;
! 50094: pOvfl = &pPage->aOvfl[i];
! 50095: k = pOvfl->idx;
! 50096: if( k<=iCell ){
! 50097: if( k==iCell ){
! 50098: return pOvfl->pCell;
! 50099: }
! 50100: iCell--;
! 50101: }
! 50102: }
! 50103: return findCell(pPage, iCell);
! 50104: }
! 50105:
! 50106: /*
! 50107: ** Parse a cell content block and fill in the CellInfo structure. There
! 50108: ** are two versions of this function. btreeParseCell() takes a
! 50109: ** cell index as the second argument and btreeParseCellPtr()
! 50110: ** takes a pointer to the body of the cell as its second argument.
! 50111: **
! 50112: ** Within this file, the parseCell() macro can be called instead of
! 50113: ** btreeParseCellPtr(). Using some compilers, this will be faster.
! 50114: */
! 50115: static void btreeParseCellPtr(
! 50116: MemPage *pPage, /* Page containing the cell */
! 50117: u8 *pCell, /* Pointer to the cell text. */
! 50118: CellInfo *pInfo /* Fill in this structure */
! 50119: ){
! 50120: u16 n; /* Number bytes in cell content header */
! 50121: u32 nPayload; /* Number of bytes of cell payload */
! 50122:
! 50123: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 50124:
! 50125: pInfo->pCell = pCell;
! 50126: assert( pPage->leaf==0 || pPage->leaf==1 );
! 50127: n = pPage->childPtrSize;
! 50128: assert( n==4-4*pPage->leaf );
! 50129: if( pPage->intKey ){
! 50130: if( pPage->hasData ){
! 50131: n += getVarint32(&pCell[n], nPayload);
! 50132: }else{
! 50133: nPayload = 0;
! 50134: }
! 50135: n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
! 50136: pInfo->nData = nPayload;
! 50137: }else{
! 50138: pInfo->nData = 0;
! 50139: n += getVarint32(&pCell[n], nPayload);
! 50140: pInfo->nKey = nPayload;
! 50141: }
! 50142: pInfo->nPayload = nPayload;
! 50143: pInfo->nHeader = n;
! 50144: testcase( nPayload==pPage->maxLocal );
! 50145: testcase( nPayload==pPage->maxLocal+1 );
! 50146: if( likely(nPayload<=pPage->maxLocal) ){
! 50147: /* This is the (easy) common case where the entire payload fits
! 50148: ** on the local page. No overflow is required.
! 50149: */
! 50150: if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
! 50151: pInfo->nLocal = (u16)nPayload;
! 50152: pInfo->iOverflow = 0;
! 50153: }else{
! 50154: /* If the payload will not fit completely on the local page, we have
! 50155: ** to decide how much to store locally and how much to spill onto
! 50156: ** overflow pages. The strategy is to minimize the amount of unused
! 50157: ** space on overflow pages while keeping the amount of local storage
! 50158: ** in between minLocal and maxLocal.
! 50159: **
! 50160: ** Warning: changing the way overflow payload is distributed in any
! 50161: ** way will result in an incompatible file format.
! 50162: */
! 50163: int minLocal; /* Minimum amount of payload held locally */
! 50164: int maxLocal; /* Maximum amount of payload held locally */
! 50165: int surplus; /* Overflow payload available for local storage */
! 50166:
! 50167: minLocal = pPage->minLocal;
! 50168: maxLocal = pPage->maxLocal;
! 50169: surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
! 50170: testcase( surplus==maxLocal );
! 50171: testcase( surplus==maxLocal+1 );
! 50172: if( surplus <= maxLocal ){
! 50173: pInfo->nLocal = (u16)surplus;
! 50174: }else{
! 50175: pInfo->nLocal = (u16)minLocal;
! 50176: }
! 50177: pInfo->iOverflow = (u16)(pInfo->nLocal + n);
! 50178: pInfo->nSize = pInfo->iOverflow + 4;
! 50179: }
! 50180: }
! 50181: #define parseCell(pPage, iCell, pInfo) \
! 50182: btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
! 50183: static void btreeParseCell(
! 50184: MemPage *pPage, /* Page containing the cell */
! 50185: int iCell, /* The cell index. First cell is 0 */
! 50186: CellInfo *pInfo /* Fill in this structure */
! 50187: ){
! 50188: parseCell(pPage, iCell, pInfo);
! 50189: }
! 50190:
! 50191: /*
! 50192: ** Compute the total number of bytes that a Cell needs in the cell
! 50193: ** data area of the btree-page. The return number includes the cell
! 50194: ** data header and the local payload, but not any overflow page or
! 50195: ** the space used by the cell pointer.
! 50196: */
! 50197: static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
! 50198: u8 *pIter = &pCell[pPage->childPtrSize];
! 50199: u32 nSize;
! 50200:
! 50201: #ifdef SQLITE_DEBUG
! 50202: /* The value returned by this function should always be the same as
! 50203: ** the (CellInfo.nSize) value found by doing a full parse of the
! 50204: ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
! 50205: ** this function verifies that this invariant is not violated. */
! 50206: CellInfo debuginfo;
! 50207: btreeParseCellPtr(pPage, pCell, &debuginfo);
! 50208: #endif
! 50209:
! 50210: if( pPage->intKey ){
! 50211: u8 *pEnd;
! 50212: if( pPage->hasData ){
! 50213: pIter += getVarint32(pIter, nSize);
! 50214: }else{
! 50215: nSize = 0;
! 50216: }
! 50217:
! 50218: /* pIter now points at the 64-bit integer key value, a variable length
! 50219: ** integer. The following block moves pIter to point at the first byte
! 50220: ** past the end of the key value. */
! 50221: pEnd = &pIter[9];
! 50222: while( (*pIter++)&0x80 && pIter<pEnd );
! 50223: }else{
! 50224: pIter += getVarint32(pIter, nSize);
! 50225: }
! 50226:
! 50227: testcase( nSize==pPage->maxLocal );
! 50228: testcase( nSize==pPage->maxLocal+1 );
! 50229: if( nSize>pPage->maxLocal ){
! 50230: int minLocal = pPage->minLocal;
! 50231: nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
! 50232: testcase( nSize==pPage->maxLocal );
! 50233: testcase( nSize==pPage->maxLocal+1 );
! 50234: if( nSize>pPage->maxLocal ){
! 50235: nSize = minLocal;
! 50236: }
! 50237: nSize += 4;
! 50238: }
! 50239: nSize += (u32)(pIter - pCell);
! 50240:
! 50241: /* The minimum size of any cell is 4 bytes. */
! 50242: if( nSize<4 ){
! 50243: nSize = 4;
! 50244: }
! 50245:
! 50246: assert( nSize==debuginfo.nSize );
! 50247: return (u16)nSize;
! 50248: }
! 50249:
! 50250: #ifdef SQLITE_DEBUG
! 50251: /* This variation on cellSizePtr() is used inside of assert() statements
! 50252: ** only. */
! 50253: static u16 cellSize(MemPage *pPage, int iCell){
! 50254: return cellSizePtr(pPage, findCell(pPage, iCell));
! 50255: }
! 50256: #endif
! 50257:
! 50258: #ifndef SQLITE_OMIT_AUTOVACUUM
! 50259: /*
! 50260: ** If the cell pCell, part of page pPage contains a pointer
! 50261: ** to an overflow page, insert an entry into the pointer-map
! 50262: ** for the overflow page.
! 50263: */
! 50264: static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
! 50265: CellInfo info;
! 50266: if( *pRC ) return;
! 50267: assert( pCell!=0 );
! 50268: btreeParseCellPtr(pPage, pCell, &info);
! 50269: assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
! 50270: if( info.iOverflow ){
! 50271: Pgno ovfl = get4byte(&pCell[info.iOverflow]);
! 50272: ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
! 50273: }
! 50274: }
! 50275: #endif
! 50276:
! 50277:
! 50278: /*
! 50279: ** Defragment the page given. All Cells are moved to the
! 50280: ** end of the page and all free space is collected into one
! 50281: ** big FreeBlk that occurs in between the header and cell
! 50282: ** pointer array and the cell content area.
! 50283: */
! 50284: static int defragmentPage(MemPage *pPage){
! 50285: int i; /* Loop counter */
! 50286: int pc; /* Address of a i-th cell */
! 50287: int hdr; /* Offset to the page header */
! 50288: int size; /* Size of a cell */
! 50289: int usableSize; /* Number of usable bytes on a page */
! 50290: int cellOffset; /* Offset to the cell pointer array */
! 50291: int cbrk; /* Offset to the cell content area */
! 50292: int nCell; /* Number of cells on the page */
! 50293: unsigned char *data; /* The page data */
! 50294: unsigned char *temp; /* Temp area for cell content */
! 50295: int iCellFirst; /* First allowable cell index */
! 50296: int iCellLast; /* Last possible cell index */
! 50297:
! 50298:
! 50299: assert( sqlite3PagerIswriteable(pPage->pDbPage) );
! 50300: assert( pPage->pBt!=0 );
! 50301: assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
! 50302: assert( pPage->nOverflow==0 );
! 50303: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 50304: temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
! 50305: data = pPage->aData;
! 50306: hdr = pPage->hdrOffset;
! 50307: cellOffset = pPage->cellOffset;
! 50308: nCell = pPage->nCell;
! 50309: assert( nCell==get2byte(&data[hdr+3]) );
! 50310: usableSize = pPage->pBt->usableSize;
! 50311: cbrk = get2byte(&data[hdr+5]);
! 50312: memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
! 50313: cbrk = usableSize;
! 50314: iCellFirst = cellOffset + 2*nCell;
! 50315: iCellLast = usableSize - 4;
! 50316: for(i=0; i<nCell; i++){
! 50317: u8 *pAddr; /* The i-th cell pointer */
! 50318: pAddr = &data[cellOffset + i*2];
! 50319: pc = get2byte(pAddr);
! 50320: testcase( pc==iCellFirst );
! 50321: testcase( pc==iCellLast );
! 50322: #if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
! 50323: /* These conditions have already been verified in btreeInitPage()
! 50324: ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined
! 50325: */
! 50326: if( pc<iCellFirst || pc>iCellLast ){
! 50327: return SQLITE_CORRUPT_BKPT;
! 50328: }
! 50329: #endif
! 50330: assert( pc>=iCellFirst && pc<=iCellLast );
! 50331: size = cellSizePtr(pPage, &temp[pc]);
! 50332: cbrk -= size;
! 50333: #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
! 50334: if( cbrk<iCellFirst ){
! 50335: return SQLITE_CORRUPT_BKPT;
! 50336: }
! 50337: #else
! 50338: if( cbrk<iCellFirst || pc+size>usableSize ){
! 50339: return SQLITE_CORRUPT_BKPT;
! 50340: }
! 50341: #endif
! 50342: assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
! 50343: testcase( cbrk+size==usableSize );
! 50344: testcase( pc+size==usableSize );
! 50345: memcpy(&data[cbrk], &temp[pc], size);
! 50346: put2byte(pAddr, cbrk);
! 50347: }
! 50348: assert( cbrk>=iCellFirst );
! 50349: put2byte(&data[hdr+5], cbrk);
! 50350: data[hdr+1] = 0;
! 50351: data[hdr+2] = 0;
! 50352: data[hdr+7] = 0;
! 50353: memset(&data[iCellFirst], 0, cbrk-iCellFirst);
! 50354: assert( sqlite3PagerIswriteable(pPage->pDbPage) );
! 50355: if( cbrk-iCellFirst!=pPage->nFree ){
! 50356: return SQLITE_CORRUPT_BKPT;
! 50357: }
! 50358: return SQLITE_OK;
! 50359: }
! 50360:
! 50361: /*
! 50362: ** Allocate nByte bytes of space from within the B-Tree page passed
! 50363: ** as the first argument. Write into *pIdx the index into pPage->aData[]
! 50364: ** of the first byte of allocated space. Return either SQLITE_OK or
! 50365: ** an error code (usually SQLITE_CORRUPT).
! 50366: **
! 50367: ** The caller guarantees that there is sufficient space to make the
! 50368: ** allocation. This routine might need to defragment in order to bring
! 50369: ** all the space together, however. This routine will avoid using
! 50370: ** the first two bytes past the cell pointer area since presumably this
! 50371: ** allocation is being made in order to insert a new cell, so we will
! 50372: ** also end up needing a new cell pointer.
! 50373: */
! 50374: static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
! 50375: const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
! 50376: u8 * const data = pPage->aData; /* Local cache of pPage->aData */
! 50377: int nFrag; /* Number of fragmented bytes on pPage */
! 50378: int top; /* First byte of cell content area */
! 50379: int gap; /* First byte of gap between cell pointers and cell content */
! 50380: int rc; /* Integer return code */
! 50381: int usableSize; /* Usable size of the page */
! 50382:
! 50383: assert( sqlite3PagerIswriteable(pPage->pDbPage) );
! 50384: assert( pPage->pBt );
! 50385: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 50386: assert( nByte>=0 ); /* Minimum cell size is 4 */
! 50387: assert( pPage->nFree>=nByte );
! 50388: assert( pPage->nOverflow==0 );
! 50389: usableSize = pPage->pBt->usableSize;
! 50390: assert( nByte < usableSize-8 );
! 50391:
! 50392: nFrag = data[hdr+7];
! 50393: assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
! 50394: gap = pPage->cellOffset + 2*pPage->nCell;
! 50395: top = get2byteNotZero(&data[hdr+5]);
! 50396: if( gap>top ) return SQLITE_CORRUPT_BKPT;
! 50397: testcase( gap+2==top );
! 50398: testcase( gap+1==top );
! 50399: testcase( gap==top );
! 50400:
! 50401: if( nFrag>=60 ){
! 50402: /* Always defragment highly fragmented pages */
! 50403: rc = defragmentPage(pPage);
! 50404: if( rc ) return rc;
! 50405: top = get2byteNotZero(&data[hdr+5]);
! 50406: }else if( gap+2<=top ){
! 50407: /* Search the freelist looking for a free slot big enough to satisfy
! 50408: ** the request. The allocation is made from the first free slot in
! 50409: ** the list that is large enough to accomadate it.
! 50410: */
! 50411: int pc, addr;
! 50412: for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
! 50413: int size; /* Size of the free slot */
! 50414: if( pc>usableSize-4 || pc<addr+4 ){
! 50415: return SQLITE_CORRUPT_BKPT;
! 50416: }
! 50417: size = get2byte(&data[pc+2]);
! 50418: if( size>=nByte ){
! 50419: int x = size - nByte;
! 50420: testcase( x==4 );
! 50421: testcase( x==3 );
! 50422: if( x<4 ){
! 50423: /* Remove the slot from the free-list. Update the number of
! 50424: ** fragmented bytes within the page. */
! 50425: memcpy(&data[addr], &data[pc], 2);
! 50426: data[hdr+7] = (u8)(nFrag + x);
! 50427: }else if( size+pc > usableSize ){
! 50428: return SQLITE_CORRUPT_BKPT;
! 50429: }else{
! 50430: /* The slot remains on the free-list. Reduce its size to account
! 50431: ** for the portion used by the new allocation. */
! 50432: put2byte(&data[pc+2], x);
! 50433: }
! 50434: *pIdx = pc + x;
! 50435: return SQLITE_OK;
! 50436: }
! 50437: }
! 50438: }
! 50439:
! 50440: /* Check to make sure there is enough space in the gap to satisfy
! 50441: ** the allocation. If not, defragment.
! 50442: */
! 50443: testcase( gap+2+nByte==top );
! 50444: if( gap+2+nByte>top ){
! 50445: rc = defragmentPage(pPage);
! 50446: if( rc ) return rc;
! 50447: top = get2byteNotZero(&data[hdr+5]);
! 50448: assert( gap+nByte<=top );
! 50449: }
! 50450:
! 50451:
! 50452: /* Allocate memory from the gap in between the cell pointer array
! 50453: ** and the cell content area. The btreeInitPage() call has already
! 50454: ** validated the freelist. Given that the freelist is valid, there
! 50455: ** is no way that the allocation can extend off the end of the page.
! 50456: ** The assert() below verifies the previous sentence.
! 50457: */
! 50458: top -= nByte;
! 50459: put2byte(&data[hdr+5], top);
! 50460: assert( top+nByte <= (int)pPage->pBt->usableSize );
! 50461: *pIdx = top;
! 50462: return SQLITE_OK;
! 50463: }
! 50464:
! 50465: /*
! 50466: ** Return a section of the pPage->aData to the freelist.
! 50467: ** The first byte of the new free block is pPage->aDisk[start]
! 50468: ** and the size of the block is "size" bytes.
! 50469: **
! 50470: ** Most of the effort here is involved in coalesing adjacent
! 50471: ** free blocks into a single big free block.
! 50472: */
! 50473: static int freeSpace(MemPage *pPage, int start, int size){
! 50474: int addr, pbegin, hdr;
! 50475: int iLast; /* Largest possible freeblock offset */
! 50476: unsigned char *data = pPage->aData;
! 50477:
! 50478: assert( pPage->pBt!=0 );
! 50479: assert( sqlite3PagerIswriteable(pPage->pDbPage) );
! 50480: assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
! 50481: assert( (start + size) <= (int)pPage->pBt->usableSize );
! 50482: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 50483: assert( size>=0 ); /* Minimum cell size is 4 */
! 50484:
! 50485: if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
! 50486: /* Overwrite deleted information with zeros when the secure_delete
! 50487: ** option is enabled */
! 50488: memset(&data[start], 0, size);
! 50489: }
! 50490:
! 50491: /* Add the space back into the linked list of freeblocks. Note that
! 50492: ** even though the freeblock list was checked by btreeInitPage(),
! 50493: ** btreeInitPage() did not detect overlapping cells or
! 50494: ** freeblocks that overlapped cells. Nor does it detect when the
! 50495: ** cell content area exceeds the value in the page header. If these
! 50496: ** situations arise, then subsequent insert operations might corrupt
! 50497: ** the freelist. So we do need to check for corruption while scanning
! 50498: ** the freelist.
! 50499: */
! 50500: hdr = pPage->hdrOffset;
! 50501: addr = hdr + 1;
! 50502: iLast = pPage->pBt->usableSize - 4;
! 50503: assert( start<=iLast );
! 50504: while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
! 50505: if( pbegin<addr+4 ){
! 50506: return SQLITE_CORRUPT_BKPT;
! 50507: }
! 50508: addr = pbegin;
! 50509: }
! 50510: if( pbegin>iLast ){
! 50511: return SQLITE_CORRUPT_BKPT;
! 50512: }
! 50513: assert( pbegin>addr || pbegin==0 );
! 50514: put2byte(&data[addr], start);
! 50515: put2byte(&data[start], pbegin);
! 50516: put2byte(&data[start+2], size);
! 50517: pPage->nFree = pPage->nFree + (u16)size;
! 50518:
! 50519: /* Coalesce adjacent free blocks */
! 50520: addr = hdr + 1;
! 50521: while( (pbegin = get2byte(&data[addr]))>0 ){
! 50522: int pnext, psize, x;
! 50523: assert( pbegin>addr );
! 50524: assert( pbegin <= (int)pPage->pBt->usableSize-4 );
! 50525: pnext = get2byte(&data[pbegin]);
! 50526: psize = get2byte(&data[pbegin+2]);
! 50527: if( pbegin + psize + 3 >= pnext && pnext>0 ){
! 50528: int frag = pnext - (pbegin+psize);
! 50529: if( (frag<0) || (frag>(int)data[hdr+7]) ){
! 50530: return SQLITE_CORRUPT_BKPT;
! 50531: }
! 50532: data[hdr+7] -= (u8)frag;
! 50533: x = get2byte(&data[pnext]);
! 50534: put2byte(&data[pbegin], x);
! 50535: x = pnext + get2byte(&data[pnext+2]) - pbegin;
! 50536: put2byte(&data[pbegin+2], x);
! 50537: }else{
! 50538: addr = pbegin;
! 50539: }
! 50540: }
! 50541:
! 50542: /* If the cell content area begins with a freeblock, remove it. */
! 50543: if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
! 50544: int top;
! 50545: pbegin = get2byte(&data[hdr+1]);
! 50546: memcpy(&data[hdr+1], &data[pbegin], 2);
! 50547: top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
! 50548: put2byte(&data[hdr+5], top);
! 50549: }
! 50550: assert( sqlite3PagerIswriteable(pPage->pDbPage) );
! 50551: return SQLITE_OK;
! 50552: }
! 50553:
! 50554: /*
! 50555: ** Decode the flags byte (the first byte of the header) for a page
! 50556: ** and initialize fields of the MemPage structure accordingly.
! 50557: **
! 50558: ** Only the following combinations are supported. Anything different
! 50559: ** indicates a corrupt database files:
! 50560: **
! 50561: ** PTF_ZERODATA
! 50562: ** PTF_ZERODATA | PTF_LEAF
! 50563: ** PTF_LEAFDATA | PTF_INTKEY
! 50564: ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
! 50565: */
! 50566: static int decodeFlags(MemPage *pPage, int flagByte){
! 50567: BtShared *pBt; /* A copy of pPage->pBt */
! 50568:
! 50569: assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
! 50570: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 50571: pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
! 50572: flagByte &= ~PTF_LEAF;
! 50573: pPage->childPtrSize = 4-4*pPage->leaf;
! 50574: pBt = pPage->pBt;
! 50575: if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
! 50576: pPage->intKey = 1;
! 50577: pPage->hasData = pPage->leaf;
! 50578: pPage->maxLocal = pBt->maxLeaf;
! 50579: pPage->minLocal = pBt->minLeaf;
! 50580: }else if( flagByte==PTF_ZERODATA ){
! 50581: pPage->intKey = 0;
! 50582: pPage->hasData = 0;
! 50583: pPage->maxLocal = pBt->maxLocal;
! 50584: pPage->minLocal = pBt->minLocal;
! 50585: }else{
! 50586: return SQLITE_CORRUPT_BKPT;
! 50587: }
! 50588: pPage->max1bytePayload = pBt->max1bytePayload;
! 50589: return SQLITE_OK;
! 50590: }
! 50591:
! 50592: /*
! 50593: ** Initialize the auxiliary information for a disk block.
! 50594: **
! 50595: ** Return SQLITE_OK on success. If we see that the page does
! 50596: ** not contain a well-formed database page, then return
! 50597: ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
! 50598: ** guarantee that the page is well-formed. It only shows that
! 50599: ** we failed to detect any corruption.
! 50600: */
! 50601: static int btreeInitPage(MemPage *pPage){
! 50602:
! 50603: assert( pPage->pBt!=0 );
! 50604: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 50605: assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
! 50606: assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
! 50607: assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
! 50608:
! 50609: if( !pPage->isInit ){
! 50610: u16 pc; /* Address of a freeblock within pPage->aData[] */
! 50611: u8 hdr; /* Offset to beginning of page header */
! 50612: u8 *data; /* Equal to pPage->aData */
! 50613: BtShared *pBt; /* The main btree structure */
! 50614: int usableSize; /* Amount of usable space on each page */
! 50615: u16 cellOffset; /* Offset from start of page to first cell pointer */
! 50616: int nFree; /* Number of unused bytes on the page */
! 50617: int top; /* First byte of the cell content area */
! 50618: int iCellFirst; /* First allowable cell or freeblock offset */
! 50619: int iCellLast; /* Last possible cell or freeblock offset */
! 50620:
! 50621: pBt = pPage->pBt;
! 50622:
! 50623: hdr = pPage->hdrOffset;
! 50624: data = pPage->aData;
! 50625: if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
! 50626: assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
! 50627: pPage->maskPage = (u16)(pBt->pageSize - 1);
! 50628: pPage->nOverflow = 0;
! 50629: usableSize = pBt->usableSize;
! 50630: pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
! 50631: pPage->aDataEnd = &data[usableSize];
! 50632: pPage->aCellIdx = &data[cellOffset];
! 50633: top = get2byteNotZero(&data[hdr+5]);
! 50634: pPage->nCell = get2byte(&data[hdr+3]);
! 50635: if( pPage->nCell>MX_CELL(pBt) ){
! 50636: /* To many cells for a single page. The page must be corrupt */
! 50637: return SQLITE_CORRUPT_BKPT;
! 50638: }
! 50639: testcase( pPage->nCell==MX_CELL(pBt) );
! 50640:
! 50641: /* A malformed database page might cause us to read past the end
! 50642: ** of page when parsing a cell.
! 50643: **
! 50644: ** The following block of code checks early to see if a cell extends
! 50645: ** past the end of a page boundary and causes SQLITE_CORRUPT to be
! 50646: ** returned if it does.
! 50647: */
! 50648: iCellFirst = cellOffset + 2*pPage->nCell;
! 50649: iCellLast = usableSize - 4;
! 50650: #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
! 50651: {
! 50652: int i; /* Index into the cell pointer array */
! 50653: int sz; /* Size of a cell */
! 50654:
! 50655: if( !pPage->leaf ) iCellLast--;
! 50656: for(i=0; i<pPage->nCell; i++){
! 50657: pc = get2byte(&data[cellOffset+i*2]);
! 50658: testcase( pc==iCellFirst );
! 50659: testcase( pc==iCellLast );
! 50660: if( pc<iCellFirst || pc>iCellLast ){
! 50661: return SQLITE_CORRUPT_BKPT;
! 50662: }
! 50663: sz = cellSizePtr(pPage, &data[pc]);
! 50664: testcase( pc+sz==usableSize );
! 50665: if( pc+sz>usableSize ){
! 50666: return SQLITE_CORRUPT_BKPT;
! 50667: }
! 50668: }
! 50669: if( !pPage->leaf ) iCellLast++;
! 50670: }
! 50671: #endif
! 50672:
! 50673: /* Compute the total free space on the page */
! 50674: pc = get2byte(&data[hdr+1]);
! 50675: nFree = data[hdr+7] + top;
! 50676: while( pc>0 ){
! 50677: u16 next, size;
! 50678: if( pc<iCellFirst || pc>iCellLast ){
! 50679: /* Start of free block is off the page */
! 50680: return SQLITE_CORRUPT_BKPT;
! 50681: }
! 50682: next = get2byte(&data[pc]);
! 50683: size = get2byte(&data[pc+2]);
! 50684: if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
! 50685: /* Free blocks must be in ascending order. And the last byte of
! 50686: ** the free-block must lie on the database page. */
! 50687: return SQLITE_CORRUPT_BKPT;
! 50688: }
! 50689: nFree = nFree + size;
! 50690: pc = next;
! 50691: }
! 50692:
! 50693: /* At this point, nFree contains the sum of the offset to the start
! 50694: ** of the cell-content area plus the number of free bytes within
! 50695: ** the cell-content area. If this is greater than the usable-size
! 50696: ** of the page, then the page must be corrupted. This check also
! 50697: ** serves to verify that the offset to the start of the cell-content
! 50698: ** area, according to the page header, lies within the page.
! 50699: */
! 50700: if( nFree>usableSize ){
! 50701: return SQLITE_CORRUPT_BKPT;
! 50702: }
! 50703: pPage->nFree = (u16)(nFree - iCellFirst);
! 50704: pPage->isInit = 1;
! 50705: }
! 50706: return SQLITE_OK;
! 50707: }
! 50708:
! 50709: /*
! 50710: ** Set up a raw page so that it looks like a database page holding
! 50711: ** no entries.
! 50712: */
! 50713: static void zeroPage(MemPage *pPage, int flags){
! 50714: unsigned char *data = pPage->aData;
! 50715: BtShared *pBt = pPage->pBt;
! 50716: u8 hdr = pPage->hdrOffset;
! 50717: u16 first;
! 50718:
! 50719: assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
! 50720: assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
! 50721: assert( sqlite3PagerGetData(pPage->pDbPage) == data );
! 50722: assert( sqlite3PagerIswriteable(pPage->pDbPage) );
! 50723: assert( sqlite3_mutex_held(pBt->mutex) );
! 50724: if( pBt->btsFlags & BTS_SECURE_DELETE ){
! 50725: memset(&data[hdr], 0, pBt->usableSize - hdr);
! 50726: }
! 50727: data[hdr] = (char)flags;
! 50728: first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
! 50729: memset(&data[hdr+1], 0, 4);
! 50730: data[hdr+7] = 0;
! 50731: put2byte(&data[hdr+5], pBt->usableSize);
! 50732: pPage->nFree = (u16)(pBt->usableSize - first);
! 50733: decodeFlags(pPage, flags);
! 50734: pPage->hdrOffset = hdr;
! 50735: pPage->cellOffset = first;
! 50736: pPage->aDataEnd = &data[pBt->usableSize];
! 50737: pPage->aCellIdx = &data[first];
! 50738: pPage->nOverflow = 0;
! 50739: assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
! 50740: pPage->maskPage = (u16)(pBt->pageSize - 1);
! 50741: pPage->nCell = 0;
! 50742: pPage->isInit = 1;
! 50743: }
! 50744:
! 50745:
! 50746: /*
! 50747: ** Convert a DbPage obtained from the pager into a MemPage used by
! 50748: ** the btree layer.
! 50749: */
! 50750: static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
! 50751: MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
! 50752: pPage->aData = sqlite3PagerGetData(pDbPage);
! 50753: pPage->pDbPage = pDbPage;
! 50754: pPage->pBt = pBt;
! 50755: pPage->pgno = pgno;
! 50756: pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
! 50757: return pPage;
! 50758: }
! 50759:
! 50760: /*
! 50761: ** Get a page from the pager. Initialize the MemPage.pBt and
! 50762: ** MemPage.aData elements if needed.
! 50763: **
! 50764: ** If the noContent flag is set, it means that we do not care about
! 50765: ** the content of the page at this time. So do not go to the disk
! 50766: ** to fetch the content. Just fill in the content with zeros for now.
! 50767: ** If in the future we call sqlite3PagerWrite() on this page, that
! 50768: ** means we have started to be concerned about content and the disk
! 50769: ** read should occur at that point.
! 50770: */
! 50771: static int btreeGetPage(
! 50772: BtShared *pBt, /* The btree */
! 50773: Pgno pgno, /* Number of the page to fetch */
! 50774: MemPage **ppPage, /* Return the page in this parameter */
! 50775: int noContent /* Do not load page content if true */
! 50776: ){
! 50777: int rc;
! 50778: DbPage *pDbPage;
! 50779:
! 50780: assert( sqlite3_mutex_held(pBt->mutex) );
! 50781: rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
! 50782: if( rc ) return rc;
! 50783: *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
! 50784: return SQLITE_OK;
! 50785: }
! 50786:
! 50787: /*
! 50788: ** Retrieve a page from the pager cache. If the requested page is not
! 50789: ** already in the pager cache return NULL. Initialize the MemPage.pBt and
! 50790: ** MemPage.aData elements if needed.
! 50791: */
! 50792: static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
! 50793: DbPage *pDbPage;
! 50794: assert( sqlite3_mutex_held(pBt->mutex) );
! 50795: pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
! 50796: if( pDbPage ){
! 50797: return btreePageFromDbPage(pDbPage, pgno, pBt);
! 50798: }
! 50799: return 0;
! 50800: }
! 50801:
! 50802: /*
! 50803: ** Return the size of the database file in pages. If there is any kind of
! 50804: ** error, return ((unsigned int)-1).
! 50805: */
! 50806: static Pgno btreePagecount(BtShared *pBt){
! 50807: return pBt->nPage;
! 50808: }
! 50809: SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
! 50810: assert( sqlite3BtreeHoldsMutex(p) );
! 50811: assert( ((p->pBt->nPage)&0x8000000)==0 );
! 50812: return (int)btreePagecount(p->pBt);
! 50813: }
! 50814:
! 50815: /*
! 50816: ** Get a page from the pager and initialize it. This routine is just a
! 50817: ** convenience wrapper around separate calls to btreeGetPage() and
! 50818: ** btreeInitPage().
! 50819: **
! 50820: ** If an error occurs, then the value *ppPage is set to is undefined. It
! 50821: ** may remain unchanged, or it may be set to an invalid value.
! 50822: */
! 50823: static int getAndInitPage(
! 50824: BtShared *pBt, /* The database file */
! 50825: Pgno pgno, /* Number of the page to get */
! 50826: MemPage **ppPage /* Write the page pointer here */
! 50827: ){
! 50828: int rc;
! 50829: assert( sqlite3_mutex_held(pBt->mutex) );
! 50830:
! 50831: if( pgno>btreePagecount(pBt) ){
! 50832: rc = SQLITE_CORRUPT_BKPT;
! 50833: }else{
! 50834: rc = btreeGetPage(pBt, pgno, ppPage, 0);
! 50835: if( rc==SQLITE_OK ){
! 50836: rc = btreeInitPage(*ppPage);
! 50837: if( rc!=SQLITE_OK ){
! 50838: releasePage(*ppPage);
! 50839: }
! 50840: }
! 50841: }
! 50842:
! 50843: testcase( pgno==0 );
! 50844: assert( pgno!=0 || rc==SQLITE_CORRUPT );
! 50845: return rc;
! 50846: }
! 50847:
! 50848: /*
! 50849: ** Release a MemPage. This should be called once for each prior
! 50850: ** call to btreeGetPage.
! 50851: */
! 50852: static void releasePage(MemPage *pPage){
! 50853: if( pPage ){
! 50854: assert( pPage->aData );
! 50855: assert( pPage->pBt );
! 50856: assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
! 50857: assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
! 50858: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 50859: sqlite3PagerUnref(pPage->pDbPage);
! 50860: }
! 50861: }
! 50862:
! 50863: /*
! 50864: ** During a rollback, when the pager reloads information into the cache
! 50865: ** so that the cache is restored to its original state at the start of
! 50866: ** the transaction, for each page restored this routine is called.
! 50867: **
! 50868: ** This routine needs to reset the extra data section at the end of the
! 50869: ** page to agree with the restored data.
! 50870: */
! 50871: static void pageReinit(DbPage *pData){
! 50872: MemPage *pPage;
! 50873: pPage = (MemPage *)sqlite3PagerGetExtra(pData);
! 50874: assert( sqlite3PagerPageRefcount(pData)>0 );
! 50875: if( pPage->isInit ){
! 50876: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 50877: pPage->isInit = 0;
! 50878: if( sqlite3PagerPageRefcount(pData)>1 ){
! 50879: /* pPage might not be a btree page; it might be an overflow page
! 50880: ** or ptrmap page or a free page. In those cases, the following
! 50881: ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
! 50882: ** But no harm is done by this. And it is very important that
! 50883: ** btreeInitPage() be called on every btree page so we make
! 50884: ** the call for every page that comes in for re-initing. */
! 50885: btreeInitPage(pPage);
! 50886: }
! 50887: }
! 50888: }
! 50889:
! 50890: /*
! 50891: ** Invoke the busy handler for a btree.
! 50892: */
! 50893: static int btreeInvokeBusyHandler(void *pArg){
! 50894: BtShared *pBt = (BtShared*)pArg;
! 50895: assert( pBt->db );
! 50896: assert( sqlite3_mutex_held(pBt->db->mutex) );
! 50897: return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
! 50898: }
! 50899:
! 50900: /*
! 50901: ** Open a database file.
! 50902: **
! 50903: ** zFilename is the name of the database file. If zFilename is NULL
! 50904: ** then an ephemeral database is created. The ephemeral database might
! 50905: ** be exclusively in memory, or it might use a disk-based memory cache.
! 50906: ** Either way, the ephemeral database will be automatically deleted
! 50907: ** when sqlite3BtreeClose() is called.
! 50908: **
! 50909: ** If zFilename is ":memory:" then an in-memory database is created
! 50910: ** that is automatically destroyed when it is closed.
! 50911: **
! 50912: ** The "flags" parameter is a bitmask that might contain bits
! 50913: ** BTREE_OMIT_JOURNAL and/or BTREE_NO_READLOCK. The BTREE_NO_READLOCK
! 50914: ** bit is also set if the SQLITE_NoReadlock flags is set in db->flags.
! 50915: ** These flags are passed through into sqlite3PagerOpen() and must
! 50916: ** be the same values as PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK.
! 50917: **
! 50918: ** If the database is already opened in the same database connection
! 50919: ** and we are in shared cache mode, then the open will fail with an
! 50920: ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
! 50921: ** objects in the same database connection since doing so will lead
! 50922: ** to problems with locking.
! 50923: */
! 50924: SQLITE_PRIVATE int sqlite3BtreeOpen(
! 50925: sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
! 50926: const char *zFilename, /* Name of the file containing the BTree database */
! 50927: sqlite3 *db, /* Associated database handle */
! 50928: Btree **ppBtree, /* Pointer to new Btree object written here */
! 50929: int flags, /* Options */
! 50930: int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
! 50931: ){
! 50932: BtShared *pBt = 0; /* Shared part of btree structure */
! 50933: Btree *p; /* Handle to return */
! 50934: sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
! 50935: int rc = SQLITE_OK; /* Result code from this function */
! 50936: u8 nReserve; /* Byte of unused space on each page */
! 50937: unsigned char zDbHeader[100]; /* Database header content */
! 50938:
! 50939: /* True if opening an ephemeral, temporary database */
! 50940: const int isTempDb = zFilename==0 || zFilename[0]==0;
! 50941:
! 50942: /* Set the variable isMemdb to true for an in-memory database, or
! 50943: ** false for a file-based database.
! 50944: */
! 50945: #ifdef SQLITE_OMIT_MEMORYDB
! 50946: const int isMemdb = 0;
! 50947: #else
! 50948: const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
! 50949: || (isTempDb && sqlite3TempInMemory(db));
! 50950: #endif
! 50951:
! 50952: assert( db!=0 );
! 50953: assert( pVfs!=0 );
! 50954: assert( sqlite3_mutex_held(db->mutex) );
! 50955: assert( (flags&0xff)==flags ); /* flags fit in 8 bits */
! 50956:
! 50957: /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
! 50958: assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
! 50959:
! 50960: /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
! 50961: assert( (flags & BTREE_SINGLE)==0 || isTempDb );
! 50962:
! 50963: if( db->flags & SQLITE_NoReadlock ){
! 50964: flags |= BTREE_NO_READLOCK;
! 50965: }
! 50966: if( isMemdb ){
! 50967: flags |= BTREE_MEMORY;
! 50968: }
! 50969: if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
! 50970: vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
! 50971: }
! 50972: p = sqlite3MallocZero(sizeof(Btree));
! 50973: if( !p ){
! 50974: return SQLITE_NOMEM;
! 50975: }
! 50976: p->inTrans = TRANS_NONE;
! 50977: p->db = db;
! 50978: #ifndef SQLITE_OMIT_SHARED_CACHE
! 50979: p->lock.pBtree = p;
! 50980: p->lock.iTable = 1;
! 50981: #endif
! 50982:
! 50983: #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
! 50984: /*
! 50985: ** If this Btree is a candidate for shared cache, try to find an
! 50986: ** existing BtShared object that we can share with
! 50987: */
! 50988: if( isMemdb==0 && isTempDb==0 ){
! 50989: if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
! 50990: int nFullPathname = pVfs->mxPathname+1;
! 50991: char *zFullPathname = sqlite3Malloc(nFullPathname);
! 50992: MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
! 50993: p->sharable = 1;
! 50994: if( !zFullPathname ){
! 50995: sqlite3_free(p);
! 50996: return SQLITE_NOMEM;
! 50997: }
! 50998: rc = sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
! 50999: if( rc ){
! 51000: sqlite3_free(zFullPathname);
! 51001: sqlite3_free(p);
! 51002: return rc;
! 51003: }
! 51004: #if SQLITE_THREADSAFE
! 51005: mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
! 51006: sqlite3_mutex_enter(mutexOpen);
! 51007: mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
! 51008: sqlite3_mutex_enter(mutexShared);
! 51009: #endif
! 51010: for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
! 51011: assert( pBt->nRef>0 );
! 51012: if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
! 51013: && sqlite3PagerVfs(pBt->pPager)==pVfs ){
! 51014: int iDb;
! 51015: for(iDb=db->nDb-1; iDb>=0; iDb--){
! 51016: Btree *pExisting = db->aDb[iDb].pBt;
! 51017: if( pExisting && pExisting->pBt==pBt ){
! 51018: sqlite3_mutex_leave(mutexShared);
! 51019: sqlite3_mutex_leave(mutexOpen);
! 51020: sqlite3_free(zFullPathname);
! 51021: sqlite3_free(p);
! 51022: return SQLITE_CONSTRAINT;
! 51023: }
! 51024: }
! 51025: p->pBt = pBt;
! 51026: pBt->nRef++;
! 51027: break;
! 51028: }
! 51029: }
! 51030: sqlite3_mutex_leave(mutexShared);
! 51031: sqlite3_free(zFullPathname);
! 51032: }
! 51033: #ifdef SQLITE_DEBUG
! 51034: else{
! 51035: /* In debug mode, we mark all persistent databases as sharable
! 51036: ** even when they are not. This exercises the locking code and
! 51037: ** gives more opportunity for asserts(sqlite3_mutex_held())
! 51038: ** statements to find locking problems.
! 51039: */
! 51040: p->sharable = 1;
! 51041: }
! 51042: #endif
! 51043: }
! 51044: #endif
! 51045: if( pBt==0 ){
! 51046: /*
! 51047: ** The following asserts make sure that structures used by the btree are
! 51048: ** the right size. This is to guard against size changes that result
! 51049: ** when compiling on a different architecture.
! 51050: */
! 51051: assert( sizeof(i64)==8 || sizeof(i64)==4 );
! 51052: assert( sizeof(u64)==8 || sizeof(u64)==4 );
! 51053: assert( sizeof(u32)==4 );
! 51054: assert( sizeof(u16)==2 );
! 51055: assert( sizeof(Pgno)==4 );
! 51056:
! 51057: pBt = sqlite3MallocZero( sizeof(*pBt) );
! 51058: if( pBt==0 ){
! 51059: rc = SQLITE_NOMEM;
! 51060: goto btree_open_out;
! 51061: }
! 51062: rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
! 51063: EXTRA_SIZE, flags, vfsFlags, pageReinit);
! 51064: if( rc==SQLITE_OK ){
! 51065: rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
! 51066: }
! 51067: if( rc!=SQLITE_OK ){
! 51068: goto btree_open_out;
! 51069: }
! 51070: pBt->openFlags = (u8)flags;
! 51071: pBt->db = db;
! 51072: sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
! 51073: p->pBt = pBt;
! 51074:
! 51075: pBt->pCursor = 0;
! 51076: pBt->pPage1 = 0;
! 51077: if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
! 51078: #ifdef SQLITE_SECURE_DELETE
! 51079: pBt->btsFlags |= BTS_SECURE_DELETE;
! 51080: #endif
! 51081: pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
! 51082: if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
! 51083: || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
! 51084: pBt->pageSize = 0;
! 51085: #ifndef SQLITE_OMIT_AUTOVACUUM
! 51086: /* If the magic name ":memory:" will create an in-memory database, then
! 51087: ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
! 51088: ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
! 51089: ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
! 51090: ** regular file-name. In this case the auto-vacuum applies as per normal.
! 51091: */
! 51092: if( zFilename && !isMemdb ){
! 51093: pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
! 51094: pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
! 51095: }
! 51096: #endif
! 51097: nReserve = 0;
! 51098: }else{
! 51099: nReserve = zDbHeader[20];
! 51100: pBt->btsFlags |= BTS_PAGESIZE_FIXED;
! 51101: #ifndef SQLITE_OMIT_AUTOVACUUM
! 51102: pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
! 51103: pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
! 51104: #endif
! 51105: }
! 51106: rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
! 51107: if( rc ) goto btree_open_out;
! 51108: pBt->usableSize = pBt->pageSize - nReserve;
! 51109: assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
! 51110:
! 51111: #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
! 51112: /* Add the new BtShared object to the linked list sharable BtShareds.
! 51113: */
! 51114: if( p->sharable ){
! 51115: MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
! 51116: pBt->nRef = 1;
! 51117: MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
! 51118: if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
! 51119: pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
! 51120: if( pBt->mutex==0 ){
! 51121: rc = SQLITE_NOMEM;
! 51122: db->mallocFailed = 0;
! 51123: goto btree_open_out;
! 51124: }
! 51125: }
! 51126: sqlite3_mutex_enter(mutexShared);
! 51127: pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
! 51128: GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
! 51129: sqlite3_mutex_leave(mutexShared);
! 51130: }
! 51131: #endif
! 51132: }
! 51133:
! 51134: #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
! 51135: /* If the new Btree uses a sharable pBtShared, then link the new
! 51136: ** Btree into the list of all sharable Btrees for the same connection.
! 51137: ** The list is kept in ascending order by pBt address.
! 51138: */
! 51139: if( p->sharable ){
! 51140: int i;
! 51141: Btree *pSib;
! 51142: for(i=0; i<db->nDb; i++){
! 51143: if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
! 51144: while( pSib->pPrev ){ pSib = pSib->pPrev; }
! 51145: if( p->pBt<pSib->pBt ){
! 51146: p->pNext = pSib;
! 51147: p->pPrev = 0;
! 51148: pSib->pPrev = p;
! 51149: }else{
! 51150: while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
! 51151: pSib = pSib->pNext;
! 51152: }
! 51153: p->pNext = pSib->pNext;
! 51154: p->pPrev = pSib;
! 51155: if( p->pNext ){
! 51156: p->pNext->pPrev = p;
! 51157: }
! 51158: pSib->pNext = p;
! 51159: }
! 51160: break;
! 51161: }
! 51162: }
! 51163: }
! 51164: #endif
! 51165: *ppBtree = p;
! 51166:
! 51167: btree_open_out:
! 51168: if( rc!=SQLITE_OK ){
! 51169: if( pBt && pBt->pPager ){
! 51170: sqlite3PagerClose(pBt->pPager);
! 51171: }
! 51172: sqlite3_free(pBt);
! 51173: sqlite3_free(p);
! 51174: *ppBtree = 0;
! 51175: }else{
! 51176: /* If the B-Tree was successfully opened, set the pager-cache size to the
! 51177: ** default value. Except, when opening on an existing shared pager-cache,
! 51178: ** do not change the pager-cache size.
! 51179: */
! 51180: if( sqlite3BtreeSchema(p, 0, 0)==0 ){
! 51181: sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
! 51182: }
! 51183: }
! 51184: if( mutexOpen ){
! 51185: assert( sqlite3_mutex_held(mutexOpen) );
! 51186: sqlite3_mutex_leave(mutexOpen);
! 51187: }
! 51188: return rc;
! 51189: }
! 51190:
! 51191: /*
! 51192: ** Decrement the BtShared.nRef counter. When it reaches zero,
! 51193: ** remove the BtShared structure from the sharing list. Return
! 51194: ** true if the BtShared.nRef counter reaches zero and return
! 51195: ** false if it is still positive.
! 51196: */
! 51197: static int removeFromSharingList(BtShared *pBt){
! 51198: #ifndef SQLITE_OMIT_SHARED_CACHE
! 51199: MUTEX_LOGIC( sqlite3_mutex *pMaster; )
! 51200: BtShared *pList;
! 51201: int removed = 0;
! 51202:
! 51203: assert( sqlite3_mutex_notheld(pBt->mutex) );
! 51204: MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
! 51205: sqlite3_mutex_enter(pMaster);
! 51206: pBt->nRef--;
! 51207: if( pBt->nRef<=0 ){
! 51208: if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
! 51209: GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
! 51210: }else{
! 51211: pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
! 51212: while( ALWAYS(pList) && pList->pNext!=pBt ){
! 51213: pList=pList->pNext;
! 51214: }
! 51215: if( ALWAYS(pList) ){
! 51216: pList->pNext = pBt->pNext;
! 51217: }
! 51218: }
! 51219: if( SQLITE_THREADSAFE ){
! 51220: sqlite3_mutex_free(pBt->mutex);
! 51221: }
! 51222: removed = 1;
! 51223: }
! 51224: sqlite3_mutex_leave(pMaster);
! 51225: return removed;
! 51226: #else
! 51227: return 1;
! 51228: #endif
! 51229: }
! 51230:
! 51231: /*
! 51232: ** Make sure pBt->pTmpSpace points to an allocation of
! 51233: ** MX_CELL_SIZE(pBt) bytes.
! 51234: */
! 51235: static void allocateTempSpace(BtShared *pBt){
! 51236: if( !pBt->pTmpSpace ){
! 51237: pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
! 51238: }
! 51239: }
! 51240:
! 51241: /*
! 51242: ** Free the pBt->pTmpSpace allocation
! 51243: */
! 51244: static void freeTempSpace(BtShared *pBt){
! 51245: sqlite3PageFree( pBt->pTmpSpace);
! 51246: pBt->pTmpSpace = 0;
! 51247: }
! 51248:
! 51249: /*
! 51250: ** Close an open database and invalidate all cursors.
! 51251: */
! 51252: SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
! 51253: BtShared *pBt = p->pBt;
! 51254: BtCursor *pCur;
! 51255:
! 51256: /* Close all cursors opened via this handle. */
! 51257: assert( sqlite3_mutex_held(p->db->mutex) );
! 51258: sqlite3BtreeEnter(p);
! 51259: pCur = pBt->pCursor;
! 51260: while( pCur ){
! 51261: BtCursor *pTmp = pCur;
! 51262: pCur = pCur->pNext;
! 51263: if( pTmp->pBtree==p ){
! 51264: sqlite3BtreeCloseCursor(pTmp);
! 51265: }
! 51266: }
! 51267:
! 51268: /* Rollback any active transaction and free the handle structure.
! 51269: ** The call to sqlite3BtreeRollback() drops any table-locks held by
! 51270: ** this handle.
! 51271: */
! 51272: sqlite3BtreeRollback(p);
! 51273: sqlite3BtreeLeave(p);
! 51274:
! 51275: /* If there are still other outstanding references to the shared-btree
! 51276: ** structure, return now. The remainder of this procedure cleans
! 51277: ** up the shared-btree.
! 51278: */
! 51279: assert( p->wantToLock==0 && p->locked==0 );
! 51280: if( !p->sharable || removeFromSharingList(pBt) ){
! 51281: /* The pBt is no longer on the sharing list, so we can access
! 51282: ** it without having to hold the mutex.
! 51283: **
! 51284: ** Clean out and delete the BtShared object.
! 51285: */
! 51286: assert( !pBt->pCursor );
! 51287: sqlite3PagerClose(pBt->pPager);
! 51288: if( pBt->xFreeSchema && pBt->pSchema ){
! 51289: pBt->xFreeSchema(pBt->pSchema);
! 51290: }
! 51291: sqlite3DbFree(0, pBt->pSchema);
! 51292: freeTempSpace(pBt);
! 51293: sqlite3_free(pBt);
! 51294: }
! 51295:
! 51296: #ifndef SQLITE_OMIT_SHARED_CACHE
! 51297: assert( p->wantToLock==0 );
! 51298: assert( p->locked==0 );
! 51299: if( p->pPrev ) p->pPrev->pNext = p->pNext;
! 51300: if( p->pNext ) p->pNext->pPrev = p->pPrev;
! 51301: #endif
! 51302:
! 51303: sqlite3_free(p);
! 51304: return SQLITE_OK;
! 51305: }
! 51306:
! 51307: /*
! 51308: ** Change the limit on the number of pages allowed in the cache.
! 51309: **
! 51310: ** The maximum number of cache pages is set to the absolute
! 51311: ** value of mxPage. If mxPage is negative, the pager will
! 51312: ** operate asynchronously - it will not stop to do fsync()s
! 51313: ** to insure data is written to the disk surface before
! 51314: ** continuing. Transactions still work if synchronous is off,
! 51315: ** and the database cannot be corrupted if this program
! 51316: ** crashes. But if the operating system crashes or there is
! 51317: ** an abrupt power failure when synchronous is off, the database
! 51318: ** could be left in an inconsistent and unrecoverable state.
! 51319: ** Synchronous is on by default so database corruption is not
! 51320: ** normally a worry.
! 51321: */
! 51322: SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
! 51323: BtShared *pBt = p->pBt;
! 51324: assert( sqlite3_mutex_held(p->db->mutex) );
! 51325: sqlite3BtreeEnter(p);
! 51326: sqlite3PagerSetCachesize(pBt->pPager, mxPage);
! 51327: sqlite3BtreeLeave(p);
! 51328: return SQLITE_OK;
! 51329: }
! 51330:
! 51331: /*
! 51332: ** Change the way data is synced to disk in order to increase or decrease
! 51333: ** how well the database resists damage due to OS crashes and power
! 51334: ** failures. Level 1 is the same as asynchronous (no syncs() occur and
! 51335: ** there is a high probability of damage) Level 2 is the default. There
! 51336: ** is a very low but non-zero probability of damage. Level 3 reduces the
! 51337: ** probability of damage to near zero but with a write performance reduction.
! 51338: */
! 51339: #ifndef SQLITE_OMIT_PAGER_PRAGMAS
! 51340: SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(
! 51341: Btree *p, /* The btree to set the safety level on */
! 51342: int level, /* PRAGMA synchronous. 1=OFF, 2=NORMAL, 3=FULL */
! 51343: int fullSync, /* PRAGMA fullfsync. */
! 51344: int ckptFullSync /* PRAGMA checkpoint_fullfync */
! 51345: ){
! 51346: BtShared *pBt = p->pBt;
! 51347: assert( sqlite3_mutex_held(p->db->mutex) );
! 51348: assert( level>=1 && level<=3 );
! 51349: sqlite3BtreeEnter(p);
! 51350: sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync);
! 51351: sqlite3BtreeLeave(p);
! 51352: return SQLITE_OK;
! 51353: }
! 51354: #endif
! 51355:
! 51356: /*
! 51357: ** Return TRUE if the given btree is set to safety level 1. In other
! 51358: ** words, return TRUE if no sync() occurs on the disk files.
! 51359: */
! 51360: SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
! 51361: BtShared *pBt = p->pBt;
! 51362: int rc;
! 51363: assert( sqlite3_mutex_held(p->db->mutex) );
! 51364: sqlite3BtreeEnter(p);
! 51365: assert( pBt && pBt->pPager );
! 51366: rc = sqlite3PagerNosync(pBt->pPager);
! 51367: sqlite3BtreeLeave(p);
! 51368: return rc;
! 51369: }
! 51370:
! 51371: /*
! 51372: ** Change the default pages size and the number of reserved bytes per page.
! 51373: ** Or, if the page size has already been fixed, return SQLITE_READONLY
! 51374: ** without changing anything.
! 51375: **
! 51376: ** The page size must be a power of 2 between 512 and 65536. If the page
! 51377: ** size supplied does not meet this constraint then the page size is not
! 51378: ** changed.
! 51379: **
! 51380: ** Page sizes are constrained to be a power of two so that the region
! 51381: ** of the database file used for locking (beginning at PENDING_BYTE,
! 51382: ** the first byte past the 1GB boundary, 0x40000000) needs to occur
! 51383: ** at the beginning of a page.
! 51384: **
! 51385: ** If parameter nReserve is less than zero, then the number of reserved
! 51386: ** bytes per page is left unchanged.
! 51387: **
! 51388: ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
! 51389: ** and autovacuum mode can no longer be changed.
! 51390: */
! 51391: SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
! 51392: int rc = SQLITE_OK;
! 51393: BtShared *pBt = p->pBt;
! 51394: assert( nReserve>=-1 && nReserve<=255 );
! 51395: sqlite3BtreeEnter(p);
! 51396: if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
! 51397: sqlite3BtreeLeave(p);
! 51398: return SQLITE_READONLY;
! 51399: }
! 51400: if( nReserve<0 ){
! 51401: nReserve = pBt->pageSize - pBt->usableSize;
! 51402: }
! 51403: assert( nReserve>=0 && nReserve<=255 );
! 51404: if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
! 51405: ((pageSize-1)&pageSize)==0 ){
! 51406: assert( (pageSize & 7)==0 );
! 51407: assert( !pBt->pPage1 && !pBt->pCursor );
! 51408: pBt->pageSize = (u32)pageSize;
! 51409: freeTempSpace(pBt);
! 51410: }
! 51411: rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
! 51412: pBt->usableSize = pBt->pageSize - (u16)nReserve;
! 51413: if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
! 51414: sqlite3BtreeLeave(p);
! 51415: return rc;
! 51416: }
! 51417:
! 51418: /*
! 51419: ** Return the currently defined page size
! 51420: */
! 51421: SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
! 51422: return p->pBt->pageSize;
! 51423: }
! 51424:
! 51425: #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
! 51426: /*
! 51427: ** Return the number of bytes of space at the end of every page that
! 51428: ** are intentually left unused. This is the "reserved" space that is
! 51429: ** sometimes used by extensions.
! 51430: */
! 51431: SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
! 51432: int n;
! 51433: sqlite3BtreeEnter(p);
! 51434: n = p->pBt->pageSize - p->pBt->usableSize;
! 51435: sqlite3BtreeLeave(p);
! 51436: return n;
! 51437: }
! 51438:
! 51439: /*
! 51440: ** Set the maximum page count for a database if mxPage is positive.
! 51441: ** No changes are made if mxPage is 0 or negative.
! 51442: ** Regardless of the value of mxPage, return the maximum page count.
! 51443: */
! 51444: SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
! 51445: int n;
! 51446: sqlite3BtreeEnter(p);
! 51447: n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
! 51448: sqlite3BtreeLeave(p);
! 51449: return n;
! 51450: }
! 51451:
! 51452: /*
! 51453: ** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1. If newFlag is -1,
! 51454: ** then make no changes. Always return the value of the BTS_SECURE_DELETE
! 51455: ** setting after the change.
! 51456: */
! 51457: SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
! 51458: int b;
! 51459: if( p==0 ) return 0;
! 51460: sqlite3BtreeEnter(p);
! 51461: if( newFlag>=0 ){
! 51462: p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
! 51463: if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
! 51464: }
! 51465: b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
! 51466: sqlite3BtreeLeave(p);
! 51467: return b;
! 51468: }
! 51469: #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
! 51470:
! 51471: /*
! 51472: ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
! 51473: ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
! 51474: ** is disabled. The default value for the auto-vacuum property is
! 51475: ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
! 51476: */
! 51477: SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
! 51478: #ifdef SQLITE_OMIT_AUTOVACUUM
! 51479: return SQLITE_READONLY;
! 51480: #else
! 51481: BtShared *pBt = p->pBt;
! 51482: int rc = SQLITE_OK;
! 51483: u8 av = (u8)autoVacuum;
! 51484:
! 51485: sqlite3BtreeEnter(p);
! 51486: if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
! 51487: rc = SQLITE_READONLY;
! 51488: }else{
! 51489: pBt->autoVacuum = av ?1:0;
! 51490: pBt->incrVacuum = av==2 ?1:0;
! 51491: }
! 51492: sqlite3BtreeLeave(p);
! 51493: return rc;
! 51494: #endif
! 51495: }
! 51496:
! 51497: /*
! 51498: ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
! 51499: ** enabled 1 is returned. Otherwise 0.
! 51500: */
! 51501: SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
! 51502: #ifdef SQLITE_OMIT_AUTOVACUUM
! 51503: return BTREE_AUTOVACUUM_NONE;
! 51504: #else
! 51505: int rc;
! 51506: sqlite3BtreeEnter(p);
! 51507: rc = (
! 51508: (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
! 51509: (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
! 51510: BTREE_AUTOVACUUM_INCR
! 51511: );
! 51512: sqlite3BtreeLeave(p);
! 51513: return rc;
! 51514: #endif
! 51515: }
! 51516:
! 51517:
! 51518: /*
! 51519: ** Get a reference to pPage1 of the database file. This will
! 51520: ** also acquire a readlock on that file.
! 51521: **
! 51522: ** SQLITE_OK is returned on success. If the file is not a
! 51523: ** well-formed database file, then SQLITE_CORRUPT is returned.
! 51524: ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
! 51525: ** is returned if we run out of memory.
! 51526: */
! 51527: static int lockBtree(BtShared *pBt){
! 51528: int rc; /* Result code from subfunctions */
! 51529: MemPage *pPage1; /* Page 1 of the database file */
! 51530: int nPage; /* Number of pages in the database */
! 51531: int nPageFile = 0; /* Number of pages in the database file */
! 51532: int nPageHeader; /* Number of pages in the database according to hdr */
! 51533:
! 51534: assert( sqlite3_mutex_held(pBt->mutex) );
! 51535: assert( pBt->pPage1==0 );
! 51536: rc = sqlite3PagerSharedLock(pBt->pPager);
! 51537: if( rc!=SQLITE_OK ) return rc;
! 51538: rc = btreeGetPage(pBt, 1, &pPage1, 0);
! 51539: if( rc!=SQLITE_OK ) return rc;
! 51540:
! 51541: /* Do some checking to help insure the file we opened really is
! 51542: ** a valid database file.
! 51543: */
! 51544: nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
! 51545: sqlite3PagerPagecount(pBt->pPager, &nPageFile);
! 51546: if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
! 51547: nPage = nPageFile;
! 51548: }
! 51549: if( nPage>0 ){
! 51550: u32 pageSize;
! 51551: u32 usableSize;
! 51552: u8 *page1 = pPage1->aData;
! 51553: rc = SQLITE_NOTADB;
! 51554: if( memcmp(page1, zMagicHeader, 16)!=0 ){
! 51555: goto page1_init_failed;
! 51556: }
! 51557:
! 51558: #ifdef SQLITE_OMIT_WAL
! 51559: if( page1[18]>1 ){
! 51560: pBt->btsFlags |= BTS_READ_ONLY;
! 51561: }
! 51562: if( page1[19]>1 ){
! 51563: goto page1_init_failed;
! 51564: }
! 51565: #else
! 51566: if( page1[18]>2 ){
! 51567: pBt->btsFlags |= BTS_READ_ONLY;
! 51568: }
! 51569: if( page1[19]>2 ){
! 51570: goto page1_init_failed;
! 51571: }
! 51572:
! 51573: /* If the write version is set to 2, this database should be accessed
! 51574: ** in WAL mode. If the log is not already open, open it now. Then
! 51575: ** return SQLITE_OK and return without populating BtShared.pPage1.
! 51576: ** The caller detects this and calls this function again. This is
! 51577: ** required as the version of page 1 currently in the page1 buffer
! 51578: ** may not be the latest version - there may be a newer one in the log
! 51579: ** file.
! 51580: */
! 51581: if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
! 51582: int isOpen = 0;
! 51583: rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
! 51584: if( rc!=SQLITE_OK ){
! 51585: goto page1_init_failed;
! 51586: }else if( isOpen==0 ){
! 51587: releasePage(pPage1);
! 51588: return SQLITE_OK;
! 51589: }
! 51590: rc = SQLITE_NOTADB;
! 51591: }
! 51592: #endif
! 51593:
! 51594: /* The maximum embedded fraction must be exactly 25%. And the minimum
! 51595: ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
! 51596: ** The original design allowed these amounts to vary, but as of
! 51597: ** version 3.6.0, we require them to be fixed.
! 51598: */
! 51599: if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
! 51600: goto page1_init_failed;
! 51601: }
! 51602: pageSize = (page1[16]<<8) | (page1[17]<<16);
! 51603: if( ((pageSize-1)&pageSize)!=0
! 51604: || pageSize>SQLITE_MAX_PAGE_SIZE
! 51605: || pageSize<=256
! 51606: ){
! 51607: goto page1_init_failed;
! 51608: }
! 51609: assert( (pageSize & 7)==0 );
! 51610: usableSize = pageSize - page1[20];
! 51611: if( (u32)pageSize!=pBt->pageSize ){
! 51612: /* After reading the first page of the database assuming a page size
! 51613: ** of BtShared.pageSize, we have discovered that the page-size is
! 51614: ** actually pageSize. Unlock the database, leave pBt->pPage1 at
! 51615: ** zero and return SQLITE_OK. The caller will call this function
! 51616: ** again with the correct page-size.
! 51617: */
! 51618: releasePage(pPage1);
! 51619: pBt->usableSize = usableSize;
! 51620: pBt->pageSize = pageSize;
! 51621: freeTempSpace(pBt);
! 51622: rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
! 51623: pageSize-usableSize);
! 51624: return rc;
! 51625: }
! 51626: if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
! 51627: rc = SQLITE_CORRUPT_BKPT;
! 51628: goto page1_init_failed;
! 51629: }
! 51630: if( usableSize<480 ){
! 51631: goto page1_init_failed;
! 51632: }
! 51633: pBt->pageSize = pageSize;
! 51634: pBt->usableSize = usableSize;
! 51635: #ifndef SQLITE_OMIT_AUTOVACUUM
! 51636: pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
! 51637: pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
! 51638: #endif
! 51639: }
! 51640:
! 51641: /* maxLocal is the maximum amount of payload to store locally for
! 51642: ** a cell. Make sure it is small enough so that at least minFanout
! 51643: ** cells can will fit on one page. We assume a 10-byte page header.
! 51644: ** Besides the payload, the cell must store:
! 51645: ** 2-byte pointer to the cell
! 51646: ** 4-byte child pointer
! 51647: ** 9-byte nKey value
! 51648: ** 4-byte nData value
! 51649: ** 4-byte overflow page pointer
! 51650: ** So a cell consists of a 2-byte pointer, a header which is as much as
! 51651: ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
! 51652: ** page pointer.
! 51653: */
! 51654: pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
! 51655: pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
! 51656: pBt->maxLeaf = (u16)(pBt->usableSize - 35);
! 51657: pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
! 51658: if( pBt->maxLocal>127 ){
! 51659: pBt->max1bytePayload = 127;
! 51660: }else{
! 51661: pBt->max1bytePayload = (u8)pBt->maxLocal;
! 51662: }
! 51663: assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
! 51664: pBt->pPage1 = pPage1;
! 51665: pBt->nPage = nPage;
! 51666: return SQLITE_OK;
! 51667:
! 51668: page1_init_failed:
! 51669: releasePage(pPage1);
! 51670: pBt->pPage1 = 0;
! 51671: return rc;
! 51672: }
! 51673:
! 51674: /*
! 51675: ** If there are no outstanding cursors and we are not in the middle
! 51676: ** of a transaction but there is a read lock on the database, then
! 51677: ** this routine unrefs the first page of the database file which
! 51678: ** has the effect of releasing the read lock.
! 51679: **
! 51680: ** If there is a transaction in progress, this routine is a no-op.
! 51681: */
! 51682: static void unlockBtreeIfUnused(BtShared *pBt){
! 51683: assert( sqlite3_mutex_held(pBt->mutex) );
! 51684: assert( pBt->pCursor==0 || pBt->inTransaction>TRANS_NONE );
! 51685: if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
! 51686: assert( pBt->pPage1->aData );
! 51687: assert( sqlite3PagerRefcount(pBt->pPager)==1 );
! 51688: assert( pBt->pPage1->aData );
! 51689: releasePage(pBt->pPage1);
! 51690: pBt->pPage1 = 0;
! 51691: }
! 51692: }
! 51693:
! 51694: /*
! 51695: ** If pBt points to an empty file then convert that empty file
! 51696: ** into a new empty database by initializing the first page of
! 51697: ** the database.
! 51698: */
! 51699: static int newDatabase(BtShared *pBt){
! 51700: MemPage *pP1;
! 51701: unsigned char *data;
! 51702: int rc;
! 51703:
! 51704: assert( sqlite3_mutex_held(pBt->mutex) );
! 51705: if( pBt->nPage>0 ){
! 51706: return SQLITE_OK;
! 51707: }
! 51708: pP1 = pBt->pPage1;
! 51709: assert( pP1!=0 );
! 51710: data = pP1->aData;
! 51711: rc = sqlite3PagerWrite(pP1->pDbPage);
! 51712: if( rc ) return rc;
! 51713: memcpy(data, zMagicHeader, sizeof(zMagicHeader));
! 51714: assert( sizeof(zMagicHeader)==16 );
! 51715: data[16] = (u8)((pBt->pageSize>>8)&0xff);
! 51716: data[17] = (u8)((pBt->pageSize>>16)&0xff);
! 51717: data[18] = 1;
! 51718: data[19] = 1;
! 51719: assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
! 51720: data[20] = (u8)(pBt->pageSize - pBt->usableSize);
! 51721: data[21] = 64;
! 51722: data[22] = 32;
! 51723: data[23] = 32;
! 51724: memset(&data[24], 0, 100-24);
! 51725: zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
! 51726: pBt->btsFlags |= BTS_PAGESIZE_FIXED;
! 51727: #ifndef SQLITE_OMIT_AUTOVACUUM
! 51728: assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
! 51729: assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
! 51730: put4byte(&data[36 + 4*4], pBt->autoVacuum);
! 51731: put4byte(&data[36 + 7*4], pBt->incrVacuum);
! 51732: #endif
! 51733: pBt->nPage = 1;
! 51734: data[31] = 1;
! 51735: return SQLITE_OK;
! 51736: }
! 51737:
! 51738: /*
! 51739: ** Attempt to start a new transaction. A write-transaction
! 51740: ** is started if the second argument is nonzero, otherwise a read-
! 51741: ** transaction. If the second argument is 2 or more and exclusive
! 51742: ** transaction is started, meaning that no other process is allowed
! 51743: ** to access the database. A preexisting transaction may not be
! 51744: ** upgraded to exclusive by calling this routine a second time - the
! 51745: ** exclusivity flag only works for a new transaction.
! 51746: **
! 51747: ** A write-transaction must be started before attempting any
! 51748: ** changes to the database. None of the following routines
! 51749: ** will work unless a transaction is started first:
! 51750: **
! 51751: ** sqlite3BtreeCreateTable()
! 51752: ** sqlite3BtreeCreateIndex()
! 51753: ** sqlite3BtreeClearTable()
! 51754: ** sqlite3BtreeDropTable()
! 51755: ** sqlite3BtreeInsert()
! 51756: ** sqlite3BtreeDelete()
! 51757: ** sqlite3BtreeUpdateMeta()
! 51758: **
! 51759: ** If an initial attempt to acquire the lock fails because of lock contention
! 51760: ** and the database was previously unlocked, then invoke the busy handler
! 51761: ** if there is one. But if there was previously a read-lock, do not
! 51762: ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
! 51763: ** returned when there is already a read-lock in order to avoid a deadlock.
! 51764: **
! 51765: ** Suppose there are two processes A and B. A has a read lock and B has
! 51766: ** a reserved lock. B tries to promote to exclusive but is blocked because
! 51767: ** of A's read lock. A tries to promote to reserved but is blocked by B.
! 51768: ** One or the other of the two processes must give way or there can be
! 51769: ** no progress. By returning SQLITE_BUSY and not invoking the busy callback
! 51770: ** when A already has a read lock, we encourage A to give up and let B
! 51771: ** proceed.
! 51772: */
! 51773: SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
! 51774: sqlite3 *pBlock = 0;
! 51775: BtShared *pBt = p->pBt;
! 51776: int rc = SQLITE_OK;
! 51777:
! 51778: sqlite3BtreeEnter(p);
! 51779: btreeIntegrity(p);
! 51780:
! 51781: /* If the btree is already in a write-transaction, or it
! 51782: ** is already in a read-transaction and a read-transaction
! 51783: ** is requested, this is a no-op.
! 51784: */
! 51785: if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
! 51786: goto trans_begun;
! 51787: }
! 51788:
! 51789: /* Write transactions are not possible on a read-only database */
! 51790: if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
! 51791: rc = SQLITE_READONLY;
! 51792: goto trans_begun;
! 51793: }
! 51794:
! 51795: #ifndef SQLITE_OMIT_SHARED_CACHE
! 51796: /* If another database handle has already opened a write transaction
! 51797: ** on this shared-btree structure and a second write transaction is
! 51798: ** requested, return SQLITE_LOCKED.
! 51799: */
! 51800: if( (wrflag && pBt->inTransaction==TRANS_WRITE)
! 51801: || (pBt->btsFlags & BTS_PENDING)!=0
! 51802: ){
! 51803: pBlock = pBt->pWriter->db;
! 51804: }else if( wrflag>1 ){
! 51805: BtLock *pIter;
! 51806: for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
! 51807: if( pIter->pBtree!=p ){
! 51808: pBlock = pIter->pBtree->db;
! 51809: break;
! 51810: }
! 51811: }
! 51812: }
! 51813: if( pBlock ){
! 51814: sqlite3ConnectionBlocked(p->db, pBlock);
! 51815: rc = SQLITE_LOCKED_SHAREDCACHE;
! 51816: goto trans_begun;
! 51817: }
! 51818: #endif
! 51819:
! 51820: /* Any read-only or read-write transaction implies a read-lock on
! 51821: ** page 1. So if some other shared-cache client already has a write-lock
! 51822: ** on page 1, the transaction cannot be opened. */
! 51823: rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
! 51824: if( SQLITE_OK!=rc ) goto trans_begun;
! 51825:
! 51826: pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
! 51827: if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
! 51828: do {
! 51829: /* Call lockBtree() until either pBt->pPage1 is populated or
! 51830: ** lockBtree() returns something other than SQLITE_OK. lockBtree()
! 51831: ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
! 51832: ** reading page 1 it discovers that the page-size of the database
! 51833: ** file is not pBt->pageSize. In this case lockBtree() will update
! 51834: ** pBt->pageSize to the page-size of the file on disk.
! 51835: */
! 51836: while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
! 51837:
! 51838: if( rc==SQLITE_OK && wrflag ){
! 51839: if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
! 51840: rc = SQLITE_READONLY;
! 51841: }else{
! 51842: rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
! 51843: if( rc==SQLITE_OK ){
! 51844: rc = newDatabase(pBt);
! 51845: }
! 51846: }
! 51847: }
! 51848:
! 51849: if( rc!=SQLITE_OK ){
! 51850: unlockBtreeIfUnused(pBt);
! 51851: }
! 51852: }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
! 51853: btreeInvokeBusyHandler(pBt) );
! 51854:
! 51855: if( rc==SQLITE_OK ){
! 51856: if( p->inTrans==TRANS_NONE ){
! 51857: pBt->nTransaction++;
! 51858: #ifndef SQLITE_OMIT_SHARED_CACHE
! 51859: if( p->sharable ){
! 51860: assert( p->lock.pBtree==p && p->lock.iTable==1 );
! 51861: p->lock.eLock = READ_LOCK;
! 51862: p->lock.pNext = pBt->pLock;
! 51863: pBt->pLock = &p->lock;
! 51864: }
! 51865: #endif
! 51866: }
! 51867: p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
! 51868: if( p->inTrans>pBt->inTransaction ){
! 51869: pBt->inTransaction = p->inTrans;
! 51870: }
! 51871: if( wrflag ){
! 51872: MemPage *pPage1 = pBt->pPage1;
! 51873: #ifndef SQLITE_OMIT_SHARED_CACHE
! 51874: assert( !pBt->pWriter );
! 51875: pBt->pWriter = p;
! 51876: pBt->btsFlags &= ~BTS_EXCLUSIVE;
! 51877: if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
! 51878: #endif
! 51879:
! 51880: /* If the db-size header field is incorrect (as it may be if an old
! 51881: ** client has been writing the database file), update it now. Doing
! 51882: ** this sooner rather than later means the database size can safely
! 51883: ** re-read the database size from page 1 if a savepoint or transaction
! 51884: ** rollback occurs within the transaction.
! 51885: */
! 51886: if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
! 51887: rc = sqlite3PagerWrite(pPage1->pDbPage);
! 51888: if( rc==SQLITE_OK ){
! 51889: put4byte(&pPage1->aData[28], pBt->nPage);
! 51890: }
! 51891: }
! 51892: }
! 51893: }
! 51894:
! 51895:
! 51896: trans_begun:
! 51897: if( rc==SQLITE_OK && wrflag ){
! 51898: /* This call makes sure that the pager has the correct number of
! 51899: ** open savepoints. If the second parameter is greater than 0 and
! 51900: ** the sub-journal is not already open, then it will be opened here.
! 51901: */
! 51902: rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
! 51903: }
! 51904:
! 51905: btreeIntegrity(p);
! 51906: sqlite3BtreeLeave(p);
! 51907: return rc;
! 51908: }
! 51909:
! 51910: #ifndef SQLITE_OMIT_AUTOVACUUM
! 51911:
! 51912: /*
! 51913: ** Set the pointer-map entries for all children of page pPage. Also, if
! 51914: ** pPage contains cells that point to overflow pages, set the pointer
! 51915: ** map entries for the overflow pages as well.
! 51916: */
! 51917: static int setChildPtrmaps(MemPage *pPage){
! 51918: int i; /* Counter variable */
! 51919: int nCell; /* Number of cells in page pPage */
! 51920: int rc; /* Return code */
! 51921: BtShared *pBt = pPage->pBt;
! 51922: u8 isInitOrig = pPage->isInit;
! 51923: Pgno pgno = pPage->pgno;
! 51924:
! 51925: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 51926: rc = btreeInitPage(pPage);
! 51927: if( rc!=SQLITE_OK ){
! 51928: goto set_child_ptrmaps_out;
! 51929: }
! 51930: nCell = pPage->nCell;
! 51931:
! 51932: for(i=0; i<nCell; i++){
! 51933: u8 *pCell = findCell(pPage, i);
! 51934:
! 51935: ptrmapPutOvflPtr(pPage, pCell, &rc);
! 51936:
! 51937: if( !pPage->leaf ){
! 51938: Pgno childPgno = get4byte(pCell);
! 51939: ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
! 51940: }
! 51941: }
! 51942:
! 51943: if( !pPage->leaf ){
! 51944: Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
! 51945: ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
! 51946: }
! 51947:
! 51948: set_child_ptrmaps_out:
! 51949: pPage->isInit = isInitOrig;
! 51950: return rc;
! 51951: }
! 51952:
! 51953: /*
! 51954: ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
! 51955: ** that it points to iTo. Parameter eType describes the type of pointer to
! 51956: ** be modified, as follows:
! 51957: **
! 51958: ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
! 51959: ** page of pPage.
! 51960: **
! 51961: ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
! 51962: ** page pointed to by one of the cells on pPage.
! 51963: **
! 51964: ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
! 51965: ** overflow page in the list.
! 51966: */
! 51967: static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
! 51968: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 51969: assert( sqlite3PagerIswriteable(pPage->pDbPage) );
! 51970: if( eType==PTRMAP_OVERFLOW2 ){
! 51971: /* The pointer is always the first 4 bytes of the page in this case. */
! 51972: if( get4byte(pPage->aData)!=iFrom ){
! 51973: return SQLITE_CORRUPT_BKPT;
! 51974: }
! 51975: put4byte(pPage->aData, iTo);
! 51976: }else{
! 51977: u8 isInitOrig = pPage->isInit;
! 51978: int i;
! 51979: int nCell;
! 51980:
! 51981: btreeInitPage(pPage);
! 51982: nCell = pPage->nCell;
! 51983:
! 51984: for(i=0; i<nCell; i++){
! 51985: u8 *pCell = findCell(pPage, i);
! 51986: if( eType==PTRMAP_OVERFLOW1 ){
! 51987: CellInfo info;
! 51988: btreeParseCellPtr(pPage, pCell, &info);
! 51989: if( info.iOverflow
! 51990: && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
! 51991: && iFrom==get4byte(&pCell[info.iOverflow])
! 51992: ){
! 51993: put4byte(&pCell[info.iOverflow], iTo);
! 51994: break;
! 51995: }
! 51996: }else{
! 51997: if( get4byte(pCell)==iFrom ){
! 51998: put4byte(pCell, iTo);
! 51999: break;
! 52000: }
! 52001: }
! 52002: }
! 52003:
! 52004: if( i==nCell ){
! 52005: if( eType!=PTRMAP_BTREE ||
! 52006: get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
! 52007: return SQLITE_CORRUPT_BKPT;
! 52008: }
! 52009: put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
! 52010: }
! 52011:
! 52012: pPage->isInit = isInitOrig;
! 52013: }
! 52014: return SQLITE_OK;
! 52015: }
! 52016:
! 52017:
! 52018: /*
! 52019: ** Move the open database page pDbPage to location iFreePage in the
! 52020: ** database. The pDbPage reference remains valid.
! 52021: **
! 52022: ** The isCommit flag indicates that there is no need to remember that
! 52023: ** the journal needs to be sync()ed before database page pDbPage->pgno
! 52024: ** can be written to. The caller has already promised not to write to that
! 52025: ** page.
! 52026: */
! 52027: static int relocatePage(
! 52028: BtShared *pBt, /* Btree */
! 52029: MemPage *pDbPage, /* Open page to move */
! 52030: u8 eType, /* Pointer map 'type' entry for pDbPage */
! 52031: Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
! 52032: Pgno iFreePage, /* The location to move pDbPage to */
! 52033: int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
! 52034: ){
! 52035: MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
! 52036: Pgno iDbPage = pDbPage->pgno;
! 52037: Pager *pPager = pBt->pPager;
! 52038: int rc;
! 52039:
! 52040: assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
! 52041: eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
! 52042: assert( sqlite3_mutex_held(pBt->mutex) );
! 52043: assert( pDbPage->pBt==pBt );
! 52044:
! 52045: /* Move page iDbPage from its current location to page number iFreePage */
! 52046: TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
! 52047: iDbPage, iFreePage, iPtrPage, eType));
! 52048: rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
! 52049: if( rc!=SQLITE_OK ){
! 52050: return rc;
! 52051: }
! 52052: pDbPage->pgno = iFreePage;
! 52053:
! 52054: /* If pDbPage was a btree-page, then it may have child pages and/or cells
! 52055: ** that point to overflow pages. The pointer map entries for all these
! 52056: ** pages need to be changed.
! 52057: **
! 52058: ** If pDbPage is an overflow page, then the first 4 bytes may store a
! 52059: ** pointer to a subsequent overflow page. If this is the case, then
! 52060: ** the pointer map needs to be updated for the subsequent overflow page.
! 52061: */
! 52062: if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
! 52063: rc = setChildPtrmaps(pDbPage);
! 52064: if( rc!=SQLITE_OK ){
! 52065: return rc;
! 52066: }
! 52067: }else{
! 52068: Pgno nextOvfl = get4byte(pDbPage->aData);
! 52069: if( nextOvfl!=0 ){
! 52070: ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
! 52071: if( rc!=SQLITE_OK ){
! 52072: return rc;
! 52073: }
! 52074: }
! 52075: }
! 52076:
! 52077: /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
! 52078: ** that it points at iFreePage. Also fix the pointer map entry for
! 52079: ** iPtrPage.
! 52080: */
! 52081: if( eType!=PTRMAP_ROOTPAGE ){
! 52082: rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
! 52083: if( rc!=SQLITE_OK ){
! 52084: return rc;
! 52085: }
! 52086: rc = sqlite3PagerWrite(pPtrPage->pDbPage);
! 52087: if( rc!=SQLITE_OK ){
! 52088: releasePage(pPtrPage);
! 52089: return rc;
! 52090: }
! 52091: rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
! 52092: releasePage(pPtrPage);
! 52093: if( rc==SQLITE_OK ){
! 52094: ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
! 52095: }
! 52096: }
! 52097: return rc;
! 52098: }
! 52099:
! 52100: /* Forward declaration required by incrVacuumStep(). */
! 52101: static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
! 52102:
! 52103: /*
! 52104: ** Perform a single step of an incremental-vacuum. If successful,
! 52105: ** return SQLITE_OK. If there is no work to do (and therefore no
! 52106: ** point in calling this function again), return SQLITE_DONE.
! 52107: **
! 52108: ** More specificly, this function attempts to re-organize the
! 52109: ** database so that the last page of the file currently in use
! 52110: ** is no longer in use.
! 52111: **
! 52112: ** If the nFin parameter is non-zero, this function assumes
! 52113: ** that the caller will keep calling incrVacuumStep() until
! 52114: ** it returns SQLITE_DONE or an error, and that nFin is the
! 52115: ** number of pages the database file will contain after this
! 52116: ** process is complete. If nFin is zero, it is assumed that
! 52117: ** incrVacuumStep() will be called a finite amount of times
! 52118: ** which may or may not empty the freelist. A full autovacuum
! 52119: ** has nFin>0. A "PRAGMA incremental_vacuum" has nFin==0.
! 52120: */
! 52121: static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
! 52122: Pgno nFreeList; /* Number of pages still on the free-list */
! 52123: int rc;
! 52124:
! 52125: assert( sqlite3_mutex_held(pBt->mutex) );
! 52126: assert( iLastPg>nFin );
! 52127:
! 52128: if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
! 52129: u8 eType;
! 52130: Pgno iPtrPage;
! 52131:
! 52132: nFreeList = get4byte(&pBt->pPage1->aData[36]);
! 52133: if( nFreeList==0 ){
! 52134: return SQLITE_DONE;
! 52135: }
! 52136:
! 52137: rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
! 52138: if( rc!=SQLITE_OK ){
! 52139: return rc;
! 52140: }
! 52141: if( eType==PTRMAP_ROOTPAGE ){
! 52142: return SQLITE_CORRUPT_BKPT;
! 52143: }
! 52144:
! 52145: if( eType==PTRMAP_FREEPAGE ){
! 52146: if( nFin==0 ){
! 52147: /* Remove the page from the files free-list. This is not required
! 52148: ** if nFin is non-zero. In that case, the free-list will be
! 52149: ** truncated to zero after this function returns, so it doesn't
! 52150: ** matter if it still contains some garbage entries.
! 52151: */
! 52152: Pgno iFreePg;
! 52153: MemPage *pFreePg;
! 52154: rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
! 52155: if( rc!=SQLITE_OK ){
! 52156: return rc;
! 52157: }
! 52158: assert( iFreePg==iLastPg );
! 52159: releasePage(pFreePg);
! 52160: }
! 52161: } else {
! 52162: Pgno iFreePg; /* Index of free page to move pLastPg to */
! 52163: MemPage *pLastPg;
! 52164:
! 52165: rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
! 52166: if( rc!=SQLITE_OK ){
! 52167: return rc;
! 52168: }
! 52169:
! 52170: /* If nFin is zero, this loop runs exactly once and page pLastPg
! 52171: ** is swapped with the first free page pulled off the free list.
! 52172: **
! 52173: ** On the other hand, if nFin is greater than zero, then keep
! 52174: ** looping until a free-page located within the first nFin pages
! 52175: ** of the file is found.
! 52176: */
! 52177: do {
! 52178: MemPage *pFreePg;
! 52179: rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
! 52180: if( rc!=SQLITE_OK ){
! 52181: releasePage(pLastPg);
! 52182: return rc;
! 52183: }
! 52184: releasePage(pFreePg);
! 52185: }while( nFin!=0 && iFreePg>nFin );
! 52186: assert( iFreePg<iLastPg );
! 52187:
! 52188: rc = sqlite3PagerWrite(pLastPg->pDbPage);
! 52189: if( rc==SQLITE_OK ){
! 52190: rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0);
! 52191: }
! 52192: releasePage(pLastPg);
! 52193: if( rc!=SQLITE_OK ){
! 52194: return rc;
! 52195: }
! 52196: }
! 52197: }
! 52198:
! 52199: if( nFin==0 ){
! 52200: iLastPg--;
! 52201: while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){
! 52202: if( PTRMAP_ISPAGE(pBt, iLastPg) ){
! 52203: MemPage *pPg;
! 52204: rc = btreeGetPage(pBt, iLastPg, &pPg, 0);
! 52205: if( rc!=SQLITE_OK ){
! 52206: return rc;
! 52207: }
! 52208: rc = sqlite3PagerWrite(pPg->pDbPage);
! 52209: releasePage(pPg);
! 52210: if( rc!=SQLITE_OK ){
! 52211: return rc;
! 52212: }
! 52213: }
! 52214: iLastPg--;
! 52215: }
! 52216: sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
! 52217: pBt->nPage = iLastPg;
! 52218: }
! 52219: return SQLITE_OK;
! 52220: }
! 52221:
! 52222: /*
! 52223: ** A write-transaction must be opened before calling this function.
! 52224: ** It performs a single unit of work towards an incremental vacuum.
! 52225: **
! 52226: ** If the incremental vacuum is finished after this function has run,
! 52227: ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
! 52228: ** SQLITE_OK is returned. Otherwise an SQLite error code.
! 52229: */
! 52230: SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
! 52231: int rc;
! 52232: BtShared *pBt = p->pBt;
! 52233:
! 52234: sqlite3BtreeEnter(p);
! 52235: assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
! 52236: if( !pBt->autoVacuum ){
! 52237: rc = SQLITE_DONE;
! 52238: }else{
! 52239: invalidateAllOverflowCache(pBt);
! 52240: rc = incrVacuumStep(pBt, 0, btreePagecount(pBt));
! 52241: if( rc==SQLITE_OK ){
! 52242: rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
! 52243: put4byte(&pBt->pPage1->aData[28], pBt->nPage);
! 52244: }
! 52245: }
! 52246: sqlite3BtreeLeave(p);
! 52247: return rc;
! 52248: }
! 52249:
! 52250: /*
! 52251: ** This routine is called prior to sqlite3PagerCommit when a transaction
! 52252: ** is commited for an auto-vacuum database.
! 52253: **
! 52254: ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
! 52255: ** the database file should be truncated to during the commit process.
! 52256: ** i.e. the database has been reorganized so that only the first *pnTrunc
! 52257: ** pages are in use.
! 52258: */
! 52259: static int autoVacuumCommit(BtShared *pBt){
! 52260: int rc = SQLITE_OK;
! 52261: Pager *pPager = pBt->pPager;
! 52262: VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
! 52263:
! 52264: assert( sqlite3_mutex_held(pBt->mutex) );
! 52265: invalidateAllOverflowCache(pBt);
! 52266: assert(pBt->autoVacuum);
! 52267: if( !pBt->incrVacuum ){
! 52268: Pgno nFin; /* Number of pages in database after autovacuuming */
! 52269: Pgno nFree; /* Number of pages on the freelist initially */
! 52270: Pgno nPtrmap; /* Number of PtrMap pages to be freed */
! 52271: Pgno iFree; /* The next page to be freed */
! 52272: int nEntry; /* Number of entries on one ptrmap page */
! 52273: Pgno nOrig; /* Database size before freeing */
! 52274:
! 52275: nOrig = btreePagecount(pBt);
! 52276: if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
! 52277: /* It is not possible to create a database for which the final page
! 52278: ** is either a pointer-map page or the pending-byte page. If one
! 52279: ** is encountered, this indicates corruption.
! 52280: */
! 52281: return SQLITE_CORRUPT_BKPT;
! 52282: }
! 52283:
! 52284: nFree = get4byte(&pBt->pPage1->aData[36]);
! 52285: nEntry = pBt->usableSize/5;
! 52286: nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
! 52287: nFin = nOrig - nFree - nPtrmap;
! 52288: if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
! 52289: nFin--;
! 52290: }
! 52291: while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
! 52292: nFin--;
! 52293: }
! 52294: if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
! 52295:
! 52296: for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
! 52297: rc = incrVacuumStep(pBt, nFin, iFree);
! 52298: }
! 52299: if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
! 52300: rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
! 52301: put4byte(&pBt->pPage1->aData[32], 0);
! 52302: put4byte(&pBt->pPage1->aData[36], 0);
! 52303: put4byte(&pBt->pPage1->aData[28], nFin);
! 52304: sqlite3PagerTruncateImage(pBt->pPager, nFin);
! 52305: pBt->nPage = nFin;
! 52306: }
! 52307: if( rc!=SQLITE_OK ){
! 52308: sqlite3PagerRollback(pPager);
! 52309: }
! 52310: }
! 52311:
! 52312: assert( nRef==sqlite3PagerRefcount(pPager) );
! 52313: return rc;
! 52314: }
! 52315:
! 52316: #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
! 52317: # define setChildPtrmaps(x) SQLITE_OK
! 52318: #endif
! 52319:
! 52320: /*
! 52321: ** This routine does the first phase of a two-phase commit. This routine
! 52322: ** causes a rollback journal to be created (if it does not already exist)
! 52323: ** and populated with enough information so that if a power loss occurs
! 52324: ** the database can be restored to its original state by playing back
! 52325: ** the journal. Then the contents of the journal are flushed out to
! 52326: ** the disk. After the journal is safely on oxide, the changes to the
! 52327: ** database are written into the database file and flushed to oxide.
! 52328: ** At the end of this call, the rollback journal still exists on the
! 52329: ** disk and we are still holding all locks, so the transaction has not
! 52330: ** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
! 52331: ** commit process.
! 52332: **
! 52333: ** This call is a no-op if no write-transaction is currently active on pBt.
! 52334: **
! 52335: ** Otherwise, sync the database file for the btree pBt. zMaster points to
! 52336: ** the name of a master journal file that should be written into the
! 52337: ** individual journal file, or is NULL, indicating no master journal file
! 52338: ** (single database transaction).
! 52339: **
! 52340: ** When this is called, the master journal should already have been
! 52341: ** created, populated with this journal pointer and synced to disk.
! 52342: **
! 52343: ** Once this is routine has returned, the only thing required to commit
! 52344: ** the write-transaction for this database file is to delete the journal.
! 52345: */
! 52346: SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
! 52347: int rc = SQLITE_OK;
! 52348: if( p->inTrans==TRANS_WRITE ){
! 52349: BtShared *pBt = p->pBt;
! 52350: sqlite3BtreeEnter(p);
! 52351: #ifndef SQLITE_OMIT_AUTOVACUUM
! 52352: if( pBt->autoVacuum ){
! 52353: rc = autoVacuumCommit(pBt);
! 52354: if( rc!=SQLITE_OK ){
! 52355: sqlite3BtreeLeave(p);
! 52356: return rc;
! 52357: }
! 52358: }
! 52359: #endif
! 52360: rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
! 52361: sqlite3BtreeLeave(p);
! 52362: }
! 52363: return rc;
! 52364: }
! 52365:
! 52366: /*
! 52367: ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
! 52368: ** at the conclusion of a transaction.
! 52369: */
! 52370: static void btreeEndTransaction(Btree *p){
! 52371: BtShared *pBt = p->pBt;
! 52372: assert( sqlite3BtreeHoldsMutex(p) );
! 52373:
! 52374: btreeClearHasContent(pBt);
! 52375: if( p->inTrans>TRANS_NONE && p->db->activeVdbeCnt>1 ){
! 52376: /* If there are other active statements that belong to this database
! 52377: ** handle, downgrade to a read-only transaction. The other statements
! 52378: ** may still be reading from the database. */
! 52379: downgradeAllSharedCacheTableLocks(p);
! 52380: p->inTrans = TRANS_READ;
! 52381: }else{
! 52382: /* If the handle had any kind of transaction open, decrement the
! 52383: ** transaction count of the shared btree. If the transaction count
! 52384: ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
! 52385: ** call below will unlock the pager. */
! 52386: if( p->inTrans!=TRANS_NONE ){
! 52387: clearAllSharedCacheTableLocks(p);
! 52388: pBt->nTransaction--;
! 52389: if( 0==pBt->nTransaction ){
! 52390: pBt->inTransaction = TRANS_NONE;
! 52391: }
! 52392: }
! 52393:
! 52394: /* Set the current transaction state to TRANS_NONE and unlock the
! 52395: ** pager if this call closed the only read or write transaction. */
! 52396: p->inTrans = TRANS_NONE;
! 52397: unlockBtreeIfUnused(pBt);
! 52398: }
! 52399:
! 52400: btreeIntegrity(p);
! 52401: }
! 52402:
! 52403: /*
! 52404: ** Commit the transaction currently in progress.
! 52405: **
! 52406: ** This routine implements the second phase of a 2-phase commit. The
! 52407: ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
! 52408: ** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
! 52409: ** routine did all the work of writing information out to disk and flushing the
! 52410: ** contents so that they are written onto the disk platter. All this
! 52411: ** routine has to do is delete or truncate or zero the header in the
! 52412: ** the rollback journal (which causes the transaction to commit) and
! 52413: ** drop locks.
! 52414: **
! 52415: ** Normally, if an error occurs while the pager layer is attempting to
! 52416: ** finalize the underlying journal file, this function returns an error and
! 52417: ** the upper layer will attempt a rollback. However, if the second argument
! 52418: ** is non-zero then this b-tree transaction is part of a multi-file
! 52419: ** transaction. In this case, the transaction has already been committed
! 52420: ** (by deleting a master journal file) and the caller will ignore this
! 52421: ** functions return code. So, even if an error occurs in the pager layer,
! 52422: ** reset the b-tree objects internal state to indicate that the write
! 52423: ** transaction has been closed. This is quite safe, as the pager will have
! 52424: ** transitioned to the error state.
! 52425: **
! 52426: ** This will release the write lock on the database file. If there
! 52427: ** are no active cursors, it also releases the read lock.
! 52428: */
! 52429: SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
! 52430:
! 52431: if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
! 52432: sqlite3BtreeEnter(p);
! 52433: btreeIntegrity(p);
! 52434:
! 52435: /* If the handle has a write-transaction open, commit the shared-btrees
! 52436: ** transaction and set the shared state to TRANS_READ.
! 52437: */
! 52438: if( p->inTrans==TRANS_WRITE ){
! 52439: int rc;
! 52440: BtShared *pBt = p->pBt;
! 52441: assert( pBt->inTransaction==TRANS_WRITE );
! 52442: assert( pBt->nTransaction>0 );
! 52443: rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
! 52444: if( rc!=SQLITE_OK && bCleanup==0 ){
! 52445: sqlite3BtreeLeave(p);
! 52446: return rc;
! 52447: }
! 52448: pBt->inTransaction = TRANS_READ;
! 52449: }
! 52450:
! 52451: btreeEndTransaction(p);
! 52452: sqlite3BtreeLeave(p);
! 52453: return SQLITE_OK;
! 52454: }
! 52455:
! 52456: /*
! 52457: ** Do both phases of a commit.
! 52458: */
! 52459: SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
! 52460: int rc;
! 52461: sqlite3BtreeEnter(p);
! 52462: rc = sqlite3BtreeCommitPhaseOne(p, 0);
! 52463: if( rc==SQLITE_OK ){
! 52464: rc = sqlite3BtreeCommitPhaseTwo(p, 0);
! 52465: }
! 52466: sqlite3BtreeLeave(p);
! 52467: return rc;
! 52468: }
! 52469:
! 52470: #ifndef NDEBUG
! 52471: /*
! 52472: ** Return the number of write-cursors open on this handle. This is for use
! 52473: ** in assert() expressions, so it is only compiled if NDEBUG is not
! 52474: ** defined.
! 52475: **
! 52476: ** For the purposes of this routine, a write-cursor is any cursor that
! 52477: ** is capable of writing to the databse. That means the cursor was
! 52478: ** originally opened for writing and the cursor has not be disabled
! 52479: ** by having its state changed to CURSOR_FAULT.
! 52480: */
! 52481: static int countWriteCursors(BtShared *pBt){
! 52482: BtCursor *pCur;
! 52483: int r = 0;
! 52484: for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
! 52485: if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++;
! 52486: }
! 52487: return r;
! 52488: }
! 52489: #endif
! 52490:
! 52491: /*
! 52492: ** This routine sets the state to CURSOR_FAULT and the error
! 52493: ** code to errCode for every cursor on BtShared that pBtree
! 52494: ** references.
! 52495: **
! 52496: ** Every cursor is tripped, including cursors that belong
! 52497: ** to other database connections that happen to be sharing
! 52498: ** the cache with pBtree.
! 52499: **
! 52500: ** This routine gets called when a rollback occurs.
! 52501: ** All cursors using the same cache must be tripped
! 52502: ** to prevent them from trying to use the btree after
! 52503: ** the rollback. The rollback may have deleted tables
! 52504: ** or moved root pages, so it is not sufficient to
! 52505: ** save the state of the cursor. The cursor must be
! 52506: ** invalidated.
! 52507: */
! 52508: SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
! 52509: BtCursor *p;
! 52510: sqlite3BtreeEnter(pBtree);
! 52511: for(p=pBtree->pBt->pCursor; p; p=p->pNext){
! 52512: int i;
! 52513: sqlite3BtreeClearCursor(p);
! 52514: p->eState = CURSOR_FAULT;
! 52515: p->skipNext = errCode;
! 52516: for(i=0; i<=p->iPage; i++){
! 52517: releasePage(p->apPage[i]);
! 52518: p->apPage[i] = 0;
! 52519: }
! 52520: }
! 52521: sqlite3BtreeLeave(pBtree);
! 52522: }
! 52523:
! 52524: /*
! 52525: ** Rollback the transaction in progress. All cursors will be
! 52526: ** invalided by this operation. Any attempt to use a cursor
! 52527: ** that was open at the beginning of this operation will result
! 52528: ** in an error.
! 52529: **
! 52530: ** This will release the write lock on the database file. If there
! 52531: ** are no active cursors, it also releases the read lock.
! 52532: */
! 52533: SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p){
! 52534: int rc;
! 52535: BtShared *pBt = p->pBt;
! 52536: MemPage *pPage1;
! 52537:
! 52538: sqlite3BtreeEnter(p);
! 52539: rc = saveAllCursors(pBt, 0, 0);
! 52540: #ifndef SQLITE_OMIT_SHARED_CACHE
! 52541: if( rc!=SQLITE_OK ){
! 52542: /* This is a horrible situation. An IO or malloc() error occurred whilst
! 52543: ** trying to save cursor positions. If this is an automatic rollback (as
! 52544: ** the result of a constraint, malloc() failure or IO error) then
! 52545: ** the cache may be internally inconsistent (not contain valid trees) so
! 52546: ** we cannot simply return the error to the caller. Instead, abort
! 52547: ** all queries that may be using any of the cursors that failed to save.
! 52548: */
! 52549: sqlite3BtreeTripAllCursors(p, rc);
! 52550: }
! 52551: #endif
! 52552: btreeIntegrity(p);
! 52553:
! 52554: if( p->inTrans==TRANS_WRITE ){
! 52555: int rc2;
! 52556:
! 52557: assert( TRANS_WRITE==pBt->inTransaction );
! 52558: rc2 = sqlite3PagerRollback(pBt->pPager);
! 52559: if( rc2!=SQLITE_OK ){
! 52560: rc = rc2;
! 52561: }
! 52562:
! 52563: /* The rollback may have destroyed the pPage1->aData value. So
! 52564: ** call btreeGetPage() on page 1 again to make
! 52565: ** sure pPage1->aData is set correctly. */
! 52566: if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
! 52567: int nPage = get4byte(28+(u8*)pPage1->aData);
! 52568: testcase( nPage==0 );
! 52569: if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
! 52570: testcase( pBt->nPage!=nPage );
! 52571: pBt->nPage = nPage;
! 52572: releasePage(pPage1);
! 52573: }
! 52574: assert( countWriteCursors(pBt)==0 );
! 52575: pBt->inTransaction = TRANS_READ;
! 52576: }
! 52577:
! 52578: btreeEndTransaction(p);
! 52579: sqlite3BtreeLeave(p);
! 52580: return rc;
! 52581: }
! 52582:
! 52583: /*
! 52584: ** Start a statement subtransaction. The subtransaction can can be rolled
! 52585: ** back independently of the main transaction. You must start a transaction
! 52586: ** before starting a subtransaction. The subtransaction is ended automatically
! 52587: ** if the main transaction commits or rolls back.
! 52588: **
! 52589: ** Statement subtransactions are used around individual SQL statements
! 52590: ** that are contained within a BEGIN...COMMIT block. If a constraint
! 52591: ** error occurs within the statement, the effect of that one statement
! 52592: ** can be rolled back without having to rollback the entire transaction.
! 52593: **
! 52594: ** A statement sub-transaction is implemented as an anonymous savepoint. The
! 52595: ** value passed as the second parameter is the total number of savepoints,
! 52596: ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
! 52597: ** are no active savepoints and no other statement-transactions open,
! 52598: ** iStatement is 1. This anonymous savepoint can be released or rolled back
! 52599: ** using the sqlite3BtreeSavepoint() function.
! 52600: */
! 52601: SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
! 52602: int rc;
! 52603: BtShared *pBt = p->pBt;
! 52604: sqlite3BtreeEnter(p);
! 52605: assert( p->inTrans==TRANS_WRITE );
! 52606: assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
! 52607: assert( iStatement>0 );
! 52608: assert( iStatement>p->db->nSavepoint );
! 52609: assert( pBt->inTransaction==TRANS_WRITE );
! 52610: /* At the pager level, a statement transaction is a savepoint with
! 52611: ** an index greater than all savepoints created explicitly using
! 52612: ** SQL statements. It is illegal to open, release or rollback any
! 52613: ** such savepoints while the statement transaction savepoint is active.
! 52614: */
! 52615: rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
! 52616: sqlite3BtreeLeave(p);
! 52617: return rc;
! 52618: }
! 52619:
! 52620: /*
! 52621: ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
! 52622: ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
! 52623: ** savepoint identified by parameter iSavepoint, depending on the value
! 52624: ** of op.
! 52625: **
! 52626: ** Normally, iSavepoint is greater than or equal to zero. However, if op is
! 52627: ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
! 52628: ** contents of the entire transaction are rolled back. This is different
! 52629: ** from a normal transaction rollback, as no locks are released and the
! 52630: ** transaction remains open.
! 52631: */
! 52632: SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
! 52633: int rc = SQLITE_OK;
! 52634: if( p && p->inTrans==TRANS_WRITE ){
! 52635: BtShared *pBt = p->pBt;
! 52636: assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
! 52637: assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
! 52638: sqlite3BtreeEnter(p);
! 52639: rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
! 52640: if( rc==SQLITE_OK ){
! 52641: if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
! 52642: pBt->nPage = 0;
! 52643: }
! 52644: rc = newDatabase(pBt);
! 52645: pBt->nPage = get4byte(28 + pBt->pPage1->aData);
! 52646:
! 52647: /* The database size was written into the offset 28 of the header
! 52648: ** when the transaction started, so we know that the value at offset
! 52649: ** 28 is nonzero. */
! 52650: assert( pBt->nPage>0 );
! 52651: }
! 52652: sqlite3BtreeLeave(p);
! 52653: }
! 52654: return rc;
! 52655: }
! 52656:
! 52657: /*
! 52658: ** Create a new cursor for the BTree whose root is on the page
! 52659: ** iTable. If a read-only cursor is requested, it is assumed that
! 52660: ** the caller already has at least a read-only transaction open
! 52661: ** on the database already. If a write-cursor is requested, then
! 52662: ** the caller is assumed to have an open write transaction.
! 52663: **
! 52664: ** If wrFlag==0, then the cursor can only be used for reading.
! 52665: ** If wrFlag==1, then the cursor can be used for reading or for
! 52666: ** writing if other conditions for writing are also met. These
! 52667: ** are the conditions that must be met in order for writing to
! 52668: ** be allowed:
! 52669: **
! 52670: ** 1: The cursor must have been opened with wrFlag==1
! 52671: **
! 52672: ** 2: Other database connections that share the same pager cache
! 52673: ** but which are not in the READ_UNCOMMITTED state may not have
! 52674: ** cursors open with wrFlag==0 on the same table. Otherwise
! 52675: ** the changes made by this write cursor would be visible to
! 52676: ** the read cursors in the other database connection.
! 52677: **
! 52678: ** 3: The database must be writable (not on read-only media)
! 52679: **
! 52680: ** 4: There must be an active transaction.
! 52681: **
! 52682: ** No checking is done to make sure that page iTable really is the
! 52683: ** root page of a b-tree. If it is not, then the cursor acquired
! 52684: ** will not work correctly.
! 52685: **
! 52686: ** It is assumed that the sqlite3BtreeCursorZero() has been called
! 52687: ** on pCur to initialize the memory space prior to invoking this routine.
! 52688: */
! 52689: static int btreeCursor(
! 52690: Btree *p, /* The btree */
! 52691: int iTable, /* Root page of table to open */
! 52692: int wrFlag, /* 1 to write. 0 read-only */
! 52693: struct KeyInfo *pKeyInfo, /* First arg to comparison function */
! 52694: BtCursor *pCur /* Space for new cursor */
! 52695: ){
! 52696: BtShared *pBt = p->pBt; /* Shared b-tree handle */
! 52697:
! 52698: assert( sqlite3BtreeHoldsMutex(p) );
! 52699: assert( wrFlag==0 || wrFlag==1 );
! 52700:
! 52701: /* The following assert statements verify that if this is a sharable
! 52702: ** b-tree database, the connection is holding the required table locks,
! 52703: ** and that no other connection has any open cursor that conflicts with
! 52704: ** this lock. */
! 52705: assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
! 52706: assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
! 52707:
! 52708: /* Assert that the caller has opened the required transaction. */
! 52709: assert( p->inTrans>TRANS_NONE );
! 52710: assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
! 52711: assert( pBt->pPage1 && pBt->pPage1->aData );
! 52712:
! 52713: if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
! 52714: return SQLITE_READONLY;
! 52715: }
! 52716: if( iTable==1 && btreePagecount(pBt)==0 ){
! 52717: assert( wrFlag==0 );
! 52718: iTable = 0;
! 52719: }
! 52720:
! 52721: /* Now that no other errors can occur, finish filling in the BtCursor
! 52722: ** variables and link the cursor into the BtShared list. */
! 52723: pCur->pgnoRoot = (Pgno)iTable;
! 52724: pCur->iPage = -1;
! 52725: pCur->pKeyInfo = pKeyInfo;
! 52726: pCur->pBtree = p;
! 52727: pCur->pBt = pBt;
! 52728: pCur->wrFlag = (u8)wrFlag;
! 52729: pCur->pNext = pBt->pCursor;
! 52730: if( pCur->pNext ){
! 52731: pCur->pNext->pPrev = pCur;
! 52732: }
! 52733: pBt->pCursor = pCur;
! 52734: pCur->eState = CURSOR_INVALID;
! 52735: pCur->cachedRowid = 0;
! 52736: return SQLITE_OK;
! 52737: }
! 52738: SQLITE_PRIVATE int sqlite3BtreeCursor(
! 52739: Btree *p, /* The btree */
! 52740: int iTable, /* Root page of table to open */
! 52741: int wrFlag, /* 1 to write. 0 read-only */
! 52742: struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
! 52743: BtCursor *pCur /* Write new cursor here */
! 52744: ){
! 52745: int rc;
! 52746: sqlite3BtreeEnter(p);
! 52747: rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
! 52748: sqlite3BtreeLeave(p);
! 52749: return rc;
! 52750: }
! 52751:
! 52752: /*
! 52753: ** Return the size of a BtCursor object in bytes.
! 52754: **
! 52755: ** This interfaces is needed so that users of cursors can preallocate
! 52756: ** sufficient storage to hold a cursor. The BtCursor object is opaque
! 52757: ** to users so they cannot do the sizeof() themselves - they must call
! 52758: ** this routine.
! 52759: */
! 52760: SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
! 52761: return ROUND8(sizeof(BtCursor));
! 52762: }
! 52763:
! 52764: /*
! 52765: ** Initialize memory that will be converted into a BtCursor object.
! 52766: **
! 52767: ** The simple approach here would be to memset() the entire object
! 52768: ** to zero. But it turns out that the apPage[] and aiIdx[] arrays
! 52769: ** do not need to be zeroed and they are large, so we can save a lot
! 52770: ** of run-time by skipping the initialization of those elements.
! 52771: */
! 52772: SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
! 52773: memset(p, 0, offsetof(BtCursor, iPage));
! 52774: }
! 52775:
! 52776: /*
! 52777: ** Set the cached rowid value of every cursor in the same database file
! 52778: ** as pCur and having the same root page number as pCur. The value is
! 52779: ** set to iRowid.
! 52780: **
! 52781: ** Only positive rowid values are considered valid for this cache.
! 52782: ** The cache is initialized to zero, indicating an invalid cache.
! 52783: ** A btree will work fine with zero or negative rowids. We just cannot
! 52784: ** cache zero or negative rowids, which means tables that use zero or
! 52785: ** negative rowids might run a little slower. But in practice, zero
! 52786: ** or negative rowids are very uncommon so this should not be a problem.
! 52787: */
! 52788: SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){
! 52789: BtCursor *p;
! 52790: for(p=pCur->pBt->pCursor; p; p=p->pNext){
! 52791: if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid;
! 52792: }
! 52793: assert( pCur->cachedRowid==iRowid );
! 52794: }
! 52795:
! 52796: /*
! 52797: ** Return the cached rowid for the given cursor. A negative or zero
! 52798: ** return value indicates that the rowid cache is invalid and should be
! 52799: ** ignored. If the rowid cache has never before been set, then a
! 52800: ** zero is returned.
! 52801: */
! 52802: SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){
! 52803: return pCur->cachedRowid;
! 52804: }
! 52805:
! 52806: /*
! 52807: ** Close a cursor. The read lock on the database file is released
! 52808: ** when the last cursor is closed.
! 52809: */
! 52810: SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
! 52811: Btree *pBtree = pCur->pBtree;
! 52812: if( pBtree ){
! 52813: int i;
! 52814: BtShared *pBt = pCur->pBt;
! 52815: sqlite3BtreeEnter(pBtree);
! 52816: sqlite3BtreeClearCursor(pCur);
! 52817: if( pCur->pPrev ){
! 52818: pCur->pPrev->pNext = pCur->pNext;
! 52819: }else{
! 52820: pBt->pCursor = pCur->pNext;
! 52821: }
! 52822: if( pCur->pNext ){
! 52823: pCur->pNext->pPrev = pCur->pPrev;
! 52824: }
! 52825: for(i=0; i<=pCur->iPage; i++){
! 52826: releasePage(pCur->apPage[i]);
! 52827: }
! 52828: unlockBtreeIfUnused(pBt);
! 52829: invalidateOverflowCache(pCur);
! 52830: /* sqlite3_free(pCur); */
! 52831: sqlite3BtreeLeave(pBtree);
! 52832: }
! 52833: return SQLITE_OK;
! 52834: }
! 52835:
! 52836: /*
! 52837: ** Make sure the BtCursor* given in the argument has a valid
! 52838: ** BtCursor.info structure. If it is not already valid, call
! 52839: ** btreeParseCell() to fill it in.
! 52840: **
! 52841: ** BtCursor.info is a cache of the information in the current cell.
! 52842: ** Using this cache reduces the number of calls to btreeParseCell().
! 52843: **
! 52844: ** 2007-06-25: There is a bug in some versions of MSVC that cause the
! 52845: ** compiler to crash when getCellInfo() is implemented as a macro.
! 52846: ** But there is a measureable speed advantage to using the macro on gcc
! 52847: ** (when less compiler optimizations like -Os or -O0 are used and the
! 52848: ** compiler is not doing agressive inlining.) So we use a real function
! 52849: ** for MSVC and a macro for everything else. Ticket #2457.
! 52850: */
! 52851: #ifndef NDEBUG
! 52852: static void assertCellInfo(BtCursor *pCur){
! 52853: CellInfo info;
! 52854: int iPage = pCur->iPage;
! 52855: memset(&info, 0, sizeof(info));
! 52856: btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
! 52857: assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
! 52858: }
! 52859: #else
! 52860: #define assertCellInfo(x)
! 52861: #endif
! 52862: #ifdef _MSC_VER
! 52863: /* Use a real function in MSVC to work around bugs in that compiler. */
! 52864: static void getCellInfo(BtCursor *pCur){
! 52865: if( pCur->info.nSize==0 ){
! 52866: int iPage = pCur->iPage;
! 52867: btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
! 52868: pCur->validNKey = 1;
! 52869: }else{
! 52870: assertCellInfo(pCur);
! 52871: }
! 52872: }
! 52873: #else /* if not _MSC_VER */
! 52874: /* Use a macro in all other compilers so that the function is inlined */
! 52875: #define getCellInfo(pCur) \
! 52876: if( pCur->info.nSize==0 ){ \
! 52877: int iPage = pCur->iPage; \
! 52878: btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
! 52879: pCur->validNKey = 1; \
! 52880: }else{ \
! 52881: assertCellInfo(pCur); \
! 52882: }
! 52883: #endif /* _MSC_VER */
! 52884:
! 52885: #ifndef NDEBUG /* The next routine used only within assert() statements */
! 52886: /*
! 52887: ** Return true if the given BtCursor is valid. A valid cursor is one
! 52888: ** that is currently pointing to a row in a (non-empty) table.
! 52889: ** This is a verification routine is used only within assert() statements.
! 52890: */
! 52891: SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
! 52892: return pCur && pCur->eState==CURSOR_VALID;
! 52893: }
! 52894: #endif /* NDEBUG */
! 52895:
! 52896: /*
! 52897: ** Set *pSize to the size of the buffer needed to hold the value of
! 52898: ** the key for the current entry. If the cursor is not pointing
! 52899: ** to a valid entry, *pSize is set to 0.
! 52900: **
! 52901: ** For a table with the INTKEY flag set, this routine returns the key
! 52902: ** itself, not the number of bytes in the key.
! 52903: **
! 52904: ** The caller must position the cursor prior to invoking this routine.
! 52905: **
! 52906: ** This routine cannot fail. It always returns SQLITE_OK.
! 52907: */
! 52908: SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
! 52909: assert( cursorHoldsMutex(pCur) );
! 52910: assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
! 52911: if( pCur->eState!=CURSOR_VALID ){
! 52912: *pSize = 0;
! 52913: }else{
! 52914: getCellInfo(pCur);
! 52915: *pSize = pCur->info.nKey;
! 52916: }
! 52917: return SQLITE_OK;
! 52918: }
! 52919:
! 52920: /*
! 52921: ** Set *pSize to the number of bytes of data in the entry the
! 52922: ** cursor currently points to.
! 52923: **
! 52924: ** The caller must guarantee that the cursor is pointing to a non-NULL
! 52925: ** valid entry. In other words, the calling procedure must guarantee
! 52926: ** that the cursor has Cursor.eState==CURSOR_VALID.
! 52927: **
! 52928: ** Failure is not possible. This function always returns SQLITE_OK.
! 52929: ** It might just as well be a procedure (returning void) but we continue
! 52930: ** to return an integer result code for historical reasons.
! 52931: */
! 52932: SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
! 52933: assert( cursorHoldsMutex(pCur) );
! 52934: assert( pCur->eState==CURSOR_VALID );
! 52935: getCellInfo(pCur);
! 52936: *pSize = pCur->info.nData;
! 52937: return SQLITE_OK;
! 52938: }
! 52939:
! 52940: /*
! 52941: ** Given the page number of an overflow page in the database (parameter
! 52942: ** ovfl), this function finds the page number of the next page in the
! 52943: ** linked list of overflow pages. If possible, it uses the auto-vacuum
! 52944: ** pointer-map data instead of reading the content of page ovfl to do so.
! 52945: **
! 52946: ** If an error occurs an SQLite error code is returned. Otherwise:
! 52947: **
! 52948: ** The page number of the next overflow page in the linked list is
! 52949: ** written to *pPgnoNext. If page ovfl is the last page in its linked
! 52950: ** list, *pPgnoNext is set to zero.
! 52951: **
! 52952: ** If ppPage is not NULL, and a reference to the MemPage object corresponding
! 52953: ** to page number pOvfl was obtained, then *ppPage is set to point to that
! 52954: ** reference. It is the responsibility of the caller to call releasePage()
! 52955: ** on *ppPage to free the reference. In no reference was obtained (because
! 52956: ** the pointer-map was used to obtain the value for *pPgnoNext), then
! 52957: ** *ppPage is set to zero.
! 52958: */
! 52959: static int getOverflowPage(
! 52960: BtShared *pBt, /* The database file */
! 52961: Pgno ovfl, /* Current overflow page number */
! 52962: MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
! 52963: Pgno *pPgnoNext /* OUT: Next overflow page number */
! 52964: ){
! 52965: Pgno next = 0;
! 52966: MemPage *pPage = 0;
! 52967: int rc = SQLITE_OK;
! 52968:
! 52969: assert( sqlite3_mutex_held(pBt->mutex) );
! 52970: assert(pPgnoNext);
! 52971:
! 52972: #ifndef SQLITE_OMIT_AUTOVACUUM
! 52973: /* Try to find the next page in the overflow list using the
! 52974: ** autovacuum pointer-map pages. Guess that the next page in
! 52975: ** the overflow list is page number (ovfl+1). If that guess turns
! 52976: ** out to be wrong, fall back to loading the data of page
! 52977: ** number ovfl to determine the next page number.
! 52978: */
! 52979: if( pBt->autoVacuum ){
! 52980: Pgno pgno;
! 52981: Pgno iGuess = ovfl+1;
! 52982: u8 eType;
! 52983:
! 52984: while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
! 52985: iGuess++;
! 52986: }
! 52987:
! 52988: if( iGuess<=btreePagecount(pBt) ){
! 52989: rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
! 52990: if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
! 52991: next = iGuess;
! 52992: rc = SQLITE_DONE;
! 52993: }
! 52994: }
! 52995: }
! 52996: #endif
! 52997:
! 52998: assert( next==0 || rc==SQLITE_DONE );
! 52999: if( rc==SQLITE_OK ){
! 53000: rc = btreeGetPage(pBt, ovfl, &pPage, 0);
! 53001: assert( rc==SQLITE_OK || pPage==0 );
! 53002: if( rc==SQLITE_OK ){
! 53003: next = get4byte(pPage->aData);
! 53004: }
! 53005: }
! 53006:
! 53007: *pPgnoNext = next;
! 53008: if( ppPage ){
! 53009: *ppPage = pPage;
! 53010: }else{
! 53011: releasePage(pPage);
! 53012: }
! 53013: return (rc==SQLITE_DONE ? SQLITE_OK : rc);
! 53014: }
! 53015:
! 53016: /*
! 53017: ** Copy data from a buffer to a page, or from a page to a buffer.
! 53018: **
! 53019: ** pPayload is a pointer to data stored on database page pDbPage.
! 53020: ** If argument eOp is false, then nByte bytes of data are copied
! 53021: ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
! 53022: ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
! 53023: ** of data are copied from the buffer pBuf to pPayload.
! 53024: **
! 53025: ** SQLITE_OK is returned on success, otherwise an error code.
! 53026: */
! 53027: static int copyPayload(
! 53028: void *pPayload, /* Pointer to page data */
! 53029: void *pBuf, /* Pointer to buffer */
! 53030: int nByte, /* Number of bytes to copy */
! 53031: int eOp, /* 0 -> copy from page, 1 -> copy to page */
! 53032: DbPage *pDbPage /* Page containing pPayload */
! 53033: ){
! 53034: if( eOp ){
! 53035: /* Copy data from buffer to page (a write operation) */
! 53036: int rc = sqlite3PagerWrite(pDbPage);
! 53037: if( rc!=SQLITE_OK ){
! 53038: return rc;
! 53039: }
! 53040: memcpy(pPayload, pBuf, nByte);
! 53041: }else{
! 53042: /* Copy data from page to buffer (a read operation) */
! 53043: memcpy(pBuf, pPayload, nByte);
! 53044: }
! 53045: return SQLITE_OK;
! 53046: }
! 53047:
! 53048: /*
! 53049: ** This function is used to read or overwrite payload information
! 53050: ** for the entry that the pCur cursor is pointing to. If the eOp
! 53051: ** parameter is 0, this is a read operation (data copied into
! 53052: ** buffer pBuf). If it is non-zero, a write (data copied from
! 53053: ** buffer pBuf).
! 53054: **
! 53055: ** A total of "amt" bytes are read or written beginning at "offset".
! 53056: ** Data is read to or from the buffer pBuf.
! 53057: **
! 53058: ** The content being read or written might appear on the main page
! 53059: ** or be scattered out on multiple overflow pages.
! 53060: **
! 53061: ** If the BtCursor.isIncrblobHandle flag is set, and the current
! 53062: ** cursor entry uses one or more overflow pages, this function
! 53063: ** allocates space for and lazily popluates the overflow page-list
! 53064: ** cache array (BtCursor.aOverflow). Subsequent calls use this
! 53065: ** cache to make seeking to the supplied offset more efficient.
! 53066: **
! 53067: ** Once an overflow page-list cache has been allocated, it may be
! 53068: ** invalidated if some other cursor writes to the same table, or if
! 53069: ** the cursor is moved to a different row. Additionally, in auto-vacuum
! 53070: ** mode, the following events may invalidate an overflow page-list cache.
! 53071: **
! 53072: ** * An incremental vacuum,
! 53073: ** * A commit in auto_vacuum="full" mode,
! 53074: ** * Creating a table (may require moving an overflow page).
! 53075: */
! 53076: static int accessPayload(
! 53077: BtCursor *pCur, /* Cursor pointing to entry to read from */
! 53078: u32 offset, /* Begin reading this far into payload */
! 53079: u32 amt, /* Read this many bytes */
! 53080: unsigned char *pBuf, /* Write the bytes into this buffer */
! 53081: int eOp /* zero to read. non-zero to write. */
! 53082: ){
! 53083: unsigned char *aPayload;
! 53084: int rc = SQLITE_OK;
! 53085: u32 nKey;
! 53086: int iIdx = 0;
! 53087: MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
! 53088: BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
! 53089:
! 53090: assert( pPage );
! 53091: assert( pCur->eState==CURSOR_VALID );
! 53092: assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
! 53093: assert( cursorHoldsMutex(pCur) );
! 53094:
! 53095: getCellInfo(pCur);
! 53096: aPayload = pCur->info.pCell + pCur->info.nHeader;
! 53097: nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
! 53098:
! 53099: if( NEVER(offset+amt > nKey+pCur->info.nData)
! 53100: || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
! 53101: ){
! 53102: /* Trying to read or write past the end of the data is an error */
! 53103: return SQLITE_CORRUPT_BKPT;
! 53104: }
! 53105:
! 53106: /* Check if data must be read/written to/from the btree page itself. */
! 53107: if( offset<pCur->info.nLocal ){
! 53108: int a = amt;
! 53109: if( a+offset>pCur->info.nLocal ){
! 53110: a = pCur->info.nLocal - offset;
! 53111: }
! 53112: rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
! 53113: offset = 0;
! 53114: pBuf += a;
! 53115: amt -= a;
! 53116: }else{
! 53117: offset -= pCur->info.nLocal;
! 53118: }
! 53119:
! 53120: if( rc==SQLITE_OK && amt>0 ){
! 53121: const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
! 53122: Pgno nextPage;
! 53123:
! 53124: nextPage = get4byte(&aPayload[pCur->info.nLocal]);
! 53125:
! 53126: #ifndef SQLITE_OMIT_INCRBLOB
! 53127: /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
! 53128: ** has not been allocated, allocate it now. The array is sized at
! 53129: ** one entry for each overflow page in the overflow chain. The
! 53130: ** page number of the first overflow page is stored in aOverflow[0],
! 53131: ** etc. A value of 0 in the aOverflow[] array means "not yet known"
! 53132: ** (the cache is lazily populated).
! 53133: */
! 53134: if( pCur->isIncrblobHandle && !pCur->aOverflow ){
! 53135: int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
! 53136: pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
! 53137: /* nOvfl is always positive. If it were zero, fetchPayload would have
! 53138: ** been used instead of this routine. */
! 53139: if( ALWAYS(nOvfl) && !pCur->aOverflow ){
! 53140: rc = SQLITE_NOMEM;
! 53141: }
! 53142: }
! 53143:
! 53144: /* If the overflow page-list cache has been allocated and the
! 53145: ** entry for the first required overflow page is valid, skip
! 53146: ** directly to it.
! 53147: */
! 53148: if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
! 53149: iIdx = (offset/ovflSize);
! 53150: nextPage = pCur->aOverflow[iIdx];
! 53151: offset = (offset%ovflSize);
! 53152: }
! 53153: #endif
! 53154:
! 53155: for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
! 53156:
! 53157: #ifndef SQLITE_OMIT_INCRBLOB
! 53158: /* If required, populate the overflow page-list cache. */
! 53159: if( pCur->aOverflow ){
! 53160: assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
! 53161: pCur->aOverflow[iIdx] = nextPage;
! 53162: }
! 53163: #endif
! 53164:
! 53165: if( offset>=ovflSize ){
! 53166: /* The only reason to read this page is to obtain the page
! 53167: ** number for the next page in the overflow chain. The page
! 53168: ** data is not required. So first try to lookup the overflow
! 53169: ** page-list cache, if any, then fall back to the getOverflowPage()
! 53170: ** function.
! 53171: */
! 53172: #ifndef SQLITE_OMIT_INCRBLOB
! 53173: if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
! 53174: nextPage = pCur->aOverflow[iIdx+1];
! 53175: } else
! 53176: #endif
! 53177: rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
! 53178: offset -= ovflSize;
! 53179: }else{
! 53180: /* Need to read this page properly. It contains some of the
! 53181: ** range of data that is being read (eOp==0) or written (eOp!=0).
! 53182: */
! 53183: #ifdef SQLITE_DIRECT_OVERFLOW_READ
! 53184: sqlite3_file *fd;
! 53185: #endif
! 53186: int a = amt;
! 53187: if( a + offset > ovflSize ){
! 53188: a = ovflSize - offset;
! 53189: }
! 53190:
! 53191: #ifdef SQLITE_DIRECT_OVERFLOW_READ
! 53192: /* If all the following are true:
! 53193: **
! 53194: ** 1) this is a read operation, and
! 53195: ** 2) data is required from the start of this overflow page, and
! 53196: ** 3) the database is file-backed, and
! 53197: ** 4) there is no open write-transaction, and
! 53198: ** 5) the database is not a WAL database,
! 53199: **
! 53200: ** then data can be read directly from the database file into the
! 53201: ** output buffer, bypassing the page-cache altogether. This speeds
! 53202: ** up loading large records that span many overflow pages.
! 53203: */
! 53204: if( eOp==0 /* (1) */
! 53205: && offset==0 /* (2) */
! 53206: && pBt->inTransaction==TRANS_READ /* (4) */
! 53207: && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */
! 53208: && pBt->pPage1->aData[19]==0x01 /* (5) */
! 53209: ){
! 53210: u8 aSave[4];
! 53211: u8 *aWrite = &pBuf[-4];
! 53212: memcpy(aSave, aWrite, 4);
! 53213: rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
! 53214: nextPage = get4byte(aWrite);
! 53215: memcpy(aWrite, aSave, 4);
! 53216: }else
! 53217: #endif
! 53218:
! 53219: {
! 53220: DbPage *pDbPage;
! 53221: rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
! 53222: if( rc==SQLITE_OK ){
! 53223: aPayload = sqlite3PagerGetData(pDbPage);
! 53224: nextPage = get4byte(aPayload);
! 53225: rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
! 53226: sqlite3PagerUnref(pDbPage);
! 53227: offset = 0;
! 53228: }
! 53229: }
! 53230: amt -= a;
! 53231: pBuf += a;
! 53232: }
! 53233: }
! 53234: }
! 53235:
! 53236: if( rc==SQLITE_OK && amt>0 ){
! 53237: return SQLITE_CORRUPT_BKPT;
! 53238: }
! 53239: return rc;
! 53240: }
! 53241:
! 53242: /*
! 53243: ** Read part of the key associated with cursor pCur. Exactly
! 53244: ** "amt" bytes will be transfered into pBuf[]. The transfer
! 53245: ** begins at "offset".
! 53246: **
! 53247: ** The caller must ensure that pCur is pointing to a valid row
! 53248: ** in the table.
! 53249: **
! 53250: ** Return SQLITE_OK on success or an error code if anything goes
! 53251: ** wrong. An error is returned if "offset+amt" is larger than
! 53252: ** the available payload.
! 53253: */
! 53254: SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
! 53255: assert( cursorHoldsMutex(pCur) );
! 53256: assert( pCur->eState==CURSOR_VALID );
! 53257: assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
! 53258: assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
! 53259: return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
! 53260: }
! 53261:
! 53262: /*
! 53263: ** Read part of the data associated with cursor pCur. Exactly
! 53264: ** "amt" bytes will be transfered into pBuf[]. The transfer
! 53265: ** begins at "offset".
! 53266: **
! 53267: ** Return SQLITE_OK on success or an error code if anything goes
! 53268: ** wrong. An error is returned if "offset+amt" is larger than
! 53269: ** the available payload.
! 53270: */
! 53271: SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
! 53272: int rc;
! 53273:
! 53274: #ifndef SQLITE_OMIT_INCRBLOB
! 53275: if ( pCur->eState==CURSOR_INVALID ){
! 53276: return SQLITE_ABORT;
! 53277: }
! 53278: #endif
! 53279:
! 53280: assert( cursorHoldsMutex(pCur) );
! 53281: rc = restoreCursorPosition(pCur);
! 53282: if( rc==SQLITE_OK ){
! 53283: assert( pCur->eState==CURSOR_VALID );
! 53284: assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
! 53285: assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
! 53286: rc = accessPayload(pCur, offset, amt, pBuf, 0);
! 53287: }
! 53288: return rc;
! 53289: }
! 53290:
! 53291: /*
! 53292: ** Return a pointer to payload information from the entry that the
! 53293: ** pCur cursor is pointing to. The pointer is to the beginning of
! 53294: ** the key if skipKey==0 and it points to the beginning of data if
! 53295: ** skipKey==1. The number of bytes of available key/data is written
! 53296: ** into *pAmt. If *pAmt==0, then the value returned will not be
! 53297: ** a valid pointer.
! 53298: **
! 53299: ** This routine is an optimization. It is common for the entire key
! 53300: ** and data to fit on the local page and for there to be no overflow
! 53301: ** pages. When that is so, this routine can be used to access the
! 53302: ** key and data without making a copy. If the key and/or data spills
! 53303: ** onto overflow pages, then accessPayload() must be used to reassemble
! 53304: ** the key/data and copy it into a preallocated buffer.
! 53305: **
! 53306: ** The pointer returned by this routine looks directly into the cached
! 53307: ** page of the database. The data might change or move the next time
! 53308: ** any btree routine is called.
! 53309: */
! 53310: static const unsigned char *fetchPayload(
! 53311: BtCursor *pCur, /* Cursor pointing to entry to read from */
! 53312: int *pAmt, /* Write the number of available bytes here */
! 53313: int skipKey /* read beginning at data if this is true */
! 53314: ){
! 53315: unsigned char *aPayload;
! 53316: MemPage *pPage;
! 53317: u32 nKey;
! 53318: u32 nLocal;
! 53319:
! 53320: assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
! 53321: assert( pCur->eState==CURSOR_VALID );
! 53322: assert( cursorHoldsMutex(pCur) );
! 53323: pPage = pCur->apPage[pCur->iPage];
! 53324: assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
! 53325: if( NEVER(pCur->info.nSize==0) ){
! 53326: btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
! 53327: &pCur->info);
! 53328: }
! 53329: aPayload = pCur->info.pCell;
! 53330: aPayload += pCur->info.nHeader;
! 53331: if( pPage->intKey ){
! 53332: nKey = 0;
! 53333: }else{
! 53334: nKey = (int)pCur->info.nKey;
! 53335: }
! 53336: if( skipKey ){
! 53337: aPayload += nKey;
! 53338: nLocal = pCur->info.nLocal - nKey;
! 53339: }else{
! 53340: nLocal = pCur->info.nLocal;
! 53341: assert( nLocal<=nKey );
! 53342: }
! 53343: *pAmt = nLocal;
! 53344: return aPayload;
! 53345: }
! 53346:
! 53347:
! 53348: /*
! 53349: ** For the entry that cursor pCur is point to, return as
! 53350: ** many bytes of the key or data as are available on the local
! 53351: ** b-tree page. Write the number of available bytes into *pAmt.
! 53352: **
! 53353: ** The pointer returned is ephemeral. The key/data may move
! 53354: ** or be destroyed on the next call to any Btree routine,
! 53355: ** including calls from other threads against the same cache.
! 53356: ** Hence, a mutex on the BtShared should be held prior to calling
! 53357: ** this routine.
! 53358: **
! 53359: ** These routines is used to get quick access to key and data
! 53360: ** in the common case where no overflow pages are used.
! 53361: */
! 53362: SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
! 53363: const void *p = 0;
! 53364: assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
! 53365: assert( cursorHoldsMutex(pCur) );
! 53366: if( ALWAYS(pCur->eState==CURSOR_VALID) ){
! 53367: p = (const void*)fetchPayload(pCur, pAmt, 0);
! 53368: }
! 53369: return p;
! 53370: }
! 53371: SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
! 53372: const void *p = 0;
! 53373: assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
! 53374: assert( cursorHoldsMutex(pCur) );
! 53375: if( ALWAYS(pCur->eState==CURSOR_VALID) ){
! 53376: p = (const void*)fetchPayload(pCur, pAmt, 1);
! 53377: }
! 53378: return p;
! 53379: }
! 53380:
! 53381:
! 53382: /*
! 53383: ** Move the cursor down to a new child page. The newPgno argument is the
! 53384: ** page number of the child page to move to.
! 53385: **
! 53386: ** This function returns SQLITE_CORRUPT if the page-header flags field of
! 53387: ** the new child page does not match the flags field of the parent (i.e.
! 53388: ** if an intkey page appears to be the parent of a non-intkey page, or
! 53389: ** vice-versa).
! 53390: */
! 53391: static int moveToChild(BtCursor *pCur, u32 newPgno){
! 53392: int rc;
! 53393: int i = pCur->iPage;
! 53394: MemPage *pNewPage;
! 53395: BtShared *pBt = pCur->pBt;
! 53396:
! 53397: assert( cursorHoldsMutex(pCur) );
! 53398: assert( pCur->eState==CURSOR_VALID );
! 53399: assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
! 53400: if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
! 53401: return SQLITE_CORRUPT_BKPT;
! 53402: }
! 53403: rc = getAndInitPage(pBt, newPgno, &pNewPage);
! 53404: if( rc ) return rc;
! 53405: pCur->apPage[i+1] = pNewPage;
! 53406: pCur->aiIdx[i+1] = 0;
! 53407: pCur->iPage++;
! 53408:
! 53409: pCur->info.nSize = 0;
! 53410: pCur->validNKey = 0;
! 53411: if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
! 53412: return SQLITE_CORRUPT_BKPT;
! 53413: }
! 53414: return SQLITE_OK;
! 53415: }
! 53416:
! 53417: #if 0
! 53418: /*
! 53419: ** Page pParent is an internal (non-leaf) tree page. This function
! 53420: ** asserts that page number iChild is the left-child if the iIdx'th
! 53421: ** cell in page pParent. Or, if iIdx is equal to the total number of
! 53422: ** cells in pParent, that page number iChild is the right-child of
! 53423: ** the page.
! 53424: */
! 53425: static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
! 53426: assert( iIdx<=pParent->nCell );
! 53427: if( iIdx==pParent->nCell ){
! 53428: assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
! 53429: }else{
! 53430: assert( get4byte(findCell(pParent, iIdx))==iChild );
! 53431: }
! 53432: }
! 53433: #else
! 53434: # define assertParentIndex(x,y,z)
! 53435: #endif
! 53436:
! 53437: /*
! 53438: ** Move the cursor up to the parent page.
! 53439: **
! 53440: ** pCur->idx is set to the cell index that contains the pointer
! 53441: ** to the page we are coming from. If we are coming from the
! 53442: ** right-most child page then pCur->idx is set to one more than
! 53443: ** the largest cell index.
! 53444: */
! 53445: static void moveToParent(BtCursor *pCur){
! 53446: assert( cursorHoldsMutex(pCur) );
! 53447: assert( pCur->eState==CURSOR_VALID );
! 53448: assert( pCur->iPage>0 );
! 53449: assert( pCur->apPage[pCur->iPage] );
! 53450:
! 53451: /* UPDATE: It is actually possible for the condition tested by the assert
! 53452: ** below to be untrue if the database file is corrupt. This can occur if
! 53453: ** one cursor has modified page pParent while a reference to it is held
! 53454: ** by a second cursor. Which can only happen if a single page is linked
! 53455: ** into more than one b-tree structure in a corrupt database. */
! 53456: #if 0
! 53457: assertParentIndex(
! 53458: pCur->apPage[pCur->iPage-1],
! 53459: pCur->aiIdx[pCur->iPage-1],
! 53460: pCur->apPage[pCur->iPage]->pgno
! 53461: );
! 53462: #endif
! 53463: testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
! 53464:
! 53465: releasePage(pCur->apPage[pCur->iPage]);
! 53466: pCur->iPage--;
! 53467: pCur->info.nSize = 0;
! 53468: pCur->validNKey = 0;
! 53469: }
! 53470:
! 53471: /*
! 53472: ** Move the cursor to point to the root page of its b-tree structure.
! 53473: **
! 53474: ** If the table has a virtual root page, then the cursor is moved to point
! 53475: ** to the virtual root page instead of the actual root page. A table has a
! 53476: ** virtual root page when the actual root page contains no cells and a
! 53477: ** single child page. This can only happen with the table rooted at page 1.
! 53478: **
! 53479: ** If the b-tree structure is empty, the cursor state is set to
! 53480: ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
! 53481: ** cell located on the root (or virtual root) page and the cursor state
! 53482: ** is set to CURSOR_VALID.
! 53483: **
! 53484: ** If this function returns successfully, it may be assumed that the
! 53485: ** page-header flags indicate that the [virtual] root-page is the expected
! 53486: ** kind of b-tree page (i.e. if when opening the cursor the caller did not
! 53487: ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
! 53488: ** indicating a table b-tree, or if the caller did specify a KeyInfo
! 53489: ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
! 53490: ** b-tree).
! 53491: */
! 53492: static int moveToRoot(BtCursor *pCur){
! 53493: MemPage *pRoot;
! 53494: int rc = SQLITE_OK;
! 53495: Btree *p = pCur->pBtree;
! 53496: BtShared *pBt = p->pBt;
! 53497:
! 53498: assert( cursorHoldsMutex(pCur) );
! 53499: assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
! 53500: assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
! 53501: assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
! 53502: if( pCur->eState>=CURSOR_REQUIRESEEK ){
! 53503: if( pCur->eState==CURSOR_FAULT ){
! 53504: assert( pCur->skipNext!=SQLITE_OK );
! 53505: return pCur->skipNext;
! 53506: }
! 53507: sqlite3BtreeClearCursor(pCur);
! 53508: }
! 53509:
! 53510: if( pCur->iPage>=0 ){
! 53511: int i;
! 53512: for(i=1; i<=pCur->iPage; i++){
! 53513: releasePage(pCur->apPage[i]);
! 53514: }
! 53515: pCur->iPage = 0;
! 53516: }else if( pCur->pgnoRoot==0 ){
! 53517: pCur->eState = CURSOR_INVALID;
! 53518: return SQLITE_OK;
! 53519: }else{
! 53520: rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
! 53521: if( rc!=SQLITE_OK ){
! 53522: pCur->eState = CURSOR_INVALID;
! 53523: return rc;
! 53524: }
! 53525: pCur->iPage = 0;
! 53526:
! 53527: /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
! 53528: ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
! 53529: ** NULL, the caller expects a table b-tree. If this is not the case,
! 53530: ** return an SQLITE_CORRUPT error. */
! 53531: assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 );
! 53532: if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){
! 53533: return SQLITE_CORRUPT_BKPT;
! 53534: }
! 53535: }
! 53536:
! 53537: /* Assert that the root page is of the correct type. This must be the
! 53538: ** case as the call to this function that loaded the root-page (either
! 53539: ** this call or a previous invocation) would have detected corruption
! 53540: ** if the assumption were not true, and it is not possible for the flags
! 53541: ** byte to have been modified while this cursor is holding a reference
! 53542: ** to the page. */
! 53543: pRoot = pCur->apPage[0];
! 53544: assert( pRoot->pgno==pCur->pgnoRoot );
! 53545: assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey );
! 53546:
! 53547: pCur->aiIdx[0] = 0;
! 53548: pCur->info.nSize = 0;
! 53549: pCur->atLast = 0;
! 53550: pCur->validNKey = 0;
! 53551:
! 53552: if( pRoot->nCell==0 && !pRoot->leaf ){
! 53553: Pgno subpage;
! 53554: if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
! 53555: subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
! 53556: pCur->eState = CURSOR_VALID;
! 53557: rc = moveToChild(pCur, subpage);
! 53558: }else{
! 53559: pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
! 53560: }
! 53561: return rc;
! 53562: }
! 53563:
! 53564: /*
! 53565: ** Move the cursor down to the left-most leaf entry beneath the
! 53566: ** entry to which it is currently pointing.
! 53567: **
! 53568: ** The left-most leaf is the one with the smallest key - the first
! 53569: ** in ascending order.
! 53570: */
! 53571: static int moveToLeftmost(BtCursor *pCur){
! 53572: Pgno pgno;
! 53573: int rc = SQLITE_OK;
! 53574: MemPage *pPage;
! 53575:
! 53576: assert( cursorHoldsMutex(pCur) );
! 53577: assert( pCur->eState==CURSOR_VALID );
! 53578: while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
! 53579: assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
! 53580: pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
! 53581: rc = moveToChild(pCur, pgno);
! 53582: }
! 53583: return rc;
! 53584: }
! 53585:
! 53586: /*
! 53587: ** Move the cursor down to the right-most leaf entry beneath the
! 53588: ** page to which it is currently pointing. Notice the difference
! 53589: ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
! 53590: ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
! 53591: ** finds the right-most entry beneath the *page*.
! 53592: **
! 53593: ** The right-most entry is the one with the largest key - the last
! 53594: ** key in ascending order.
! 53595: */
! 53596: static int moveToRightmost(BtCursor *pCur){
! 53597: Pgno pgno;
! 53598: int rc = SQLITE_OK;
! 53599: MemPage *pPage = 0;
! 53600:
! 53601: assert( cursorHoldsMutex(pCur) );
! 53602: assert( pCur->eState==CURSOR_VALID );
! 53603: while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
! 53604: pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
! 53605: pCur->aiIdx[pCur->iPage] = pPage->nCell;
! 53606: rc = moveToChild(pCur, pgno);
! 53607: }
! 53608: if( rc==SQLITE_OK ){
! 53609: pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
! 53610: pCur->info.nSize = 0;
! 53611: pCur->validNKey = 0;
! 53612: }
! 53613: return rc;
! 53614: }
! 53615:
! 53616: /* Move the cursor to the first entry in the table. Return SQLITE_OK
! 53617: ** on success. Set *pRes to 0 if the cursor actually points to something
! 53618: ** or set *pRes to 1 if the table is empty.
! 53619: */
! 53620: SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
! 53621: int rc;
! 53622:
! 53623: assert( cursorHoldsMutex(pCur) );
! 53624: assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
! 53625: rc = moveToRoot(pCur);
! 53626: if( rc==SQLITE_OK ){
! 53627: if( pCur->eState==CURSOR_INVALID ){
! 53628: assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
! 53629: *pRes = 1;
! 53630: }else{
! 53631: assert( pCur->apPage[pCur->iPage]->nCell>0 );
! 53632: *pRes = 0;
! 53633: rc = moveToLeftmost(pCur);
! 53634: }
! 53635: }
! 53636: return rc;
! 53637: }
! 53638:
! 53639: /* Move the cursor to the last entry in the table. Return SQLITE_OK
! 53640: ** on success. Set *pRes to 0 if the cursor actually points to something
! 53641: ** or set *pRes to 1 if the table is empty.
! 53642: */
! 53643: SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
! 53644: int rc;
! 53645:
! 53646: assert( cursorHoldsMutex(pCur) );
! 53647: assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
! 53648:
! 53649: /* If the cursor already points to the last entry, this is a no-op. */
! 53650: if( CURSOR_VALID==pCur->eState && pCur->atLast ){
! 53651: #ifdef SQLITE_DEBUG
! 53652: /* This block serves to assert() that the cursor really does point
! 53653: ** to the last entry in the b-tree. */
! 53654: int ii;
! 53655: for(ii=0; ii<pCur->iPage; ii++){
! 53656: assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
! 53657: }
! 53658: assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
! 53659: assert( pCur->apPage[pCur->iPage]->leaf );
! 53660: #endif
! 53661: return SQLITE_OK;
! 53662: }
! 53663:
! 53664: rc = moveToRoot(pCur);
! 53665: if( rc==SQLITE_OK ){
! 53666: if( CURSOR_INVALID==pCur->eState ){
! 53667: assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
! 53668: *pRes = 1;
! 53669: }else{
! 53670: assert( pCur->eState==CURSOR_VALID );
! 53671: *pRes = 0;
! 53672: rc = moveToRightmost(pCur);
! 53673: pCur->atLast = rc==SQLITE_OK ?1:0;
! 53674: }
! 53675: }
! 53676: return rc;
! 53677: }
! 53678:
! 53679: /* Move the cursor so that it points to an entry near the key
! 53680: ** specified by pIdxKey or intKey. Return a success code.
! 53681: **
! 53682: ** For INTKEY tables, the intKey parameter is used. pIdxKey
! 53683: ** must be NULL. For index tables, pIdxKey is used and intKey
! 53684: ** is ignored.
! 53685: **
! 53686: ** If an exact match is not found, then the cursor is always
! 53687: ** left pointing at a leaf page which would hold the entry if it
! 53688: ** were present. The cursor might point to an entry that comes
! 53689: ** before or after the key.
! 53690: **
! 53691: ** An integer is written into *pRes which is the result of
! 53692: ** comparing the key with the entry to which the cursor is
! 53693: ** pointing. The meaning of the integer written into
! 53694: ** *pRes is as follows:
! 53695: **
! 53696: ** *pRes<0 The cursor is left pointing at an entry that
! 53697: ** is smaller than intKey/pIdxKey or if the table is empty
! 53698: ** and the cursor is therefore left point to nothing.
! 53699: **
! 53700: ** *pRes==0 The cursor is left pointing at an entry that
! 53701: ** exactly matches intKey/pIdxKey.
! 53702: **
! 53703: ** *pRes>0 The cursor is left pointing at an entry that
! 53704: ** is larger than intKey/pIdxKey.
! 53705: **
! 53706: */
! 53707: SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
! 53708: BtCursor *pCur, /* The cursor to be moved */
! 53709: UnpackedRecord *pIdxKey, /* Unpacked index key */
! 53710: i64 intKey, /* The table key */
! 53711: int biasRight, /* If true, bias the search to the high end */
! 53712: int *pRes /* Write search results here */
! 53713: ){
! 53714: int rc;
! 53715:
! 53716: assert( cursorHoldsMutex(pCur) );
! 53717: assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
! 53718: assert( pRes );
! 53719: assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
! 53720:
! 53721: /* If the cursor is already positioned at the point we are trying
! 53722: ** to move to, then just return without doing any work */
! 53723: if( pCur->eState==CURSOR_VALID && pCur->validNKey
! 53724: && pCur->apPage[0]->intKey
! 53725: ){
! 53726: if( pCur->info.nKey==intKey ){
! 53727: *pRes = 0;
! 53728: return SQLITE_OK;
! 53729: }
! 53730: if( pCur->atLast && pCur->info.nKey<intKey ){
! 53731: *pRes = -1;
! 53732: return SQLITE_OK;
! 53733: }
! 53734: }
! 53735:
! 53736: rc = moveToRoot(pCur);
! 53737: if( rc ){
! 53738: return rc;
! 53739: }
! 53740: assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
! 53741: assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
! 53742: assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
! 53743: if( pCur->eState==CURSOR_INVALID ){
! 53744: *pRes = -1;
! 53745: assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
! 53746: return SQLITE_OK;
! 53747: }
! 53748: assert( pCur->apPage[0]->intKey || pIdxKey );
! 53749: for(;;){
! 53750: int lwr, upr, idx;
! 53751: Pgno chldPg;
! 53752: MemPage *pPage = pCur->apPage[pCur->iPage];
! 53753: int c;
! 53754:
! 53755: /* pPage->nCell must be greater than zero. If this is the root-page
! 53756: ** the cursor would have been INVALID above and this for(;;) loop
! 53757: ** not run. If this is not the root-page, then the moveToChild() routine
! 53758: ** would have already detected db corruption. Similarly, pPage must
! 53759: ** be the right kind (index or table) of b-tree page. Otherwise
! 53760: ** a moveToChild() or moveToRoot() call would have detected corruption. */
! 53761: assert( pPage->nCell>0 );
! 53762: assert( pPage->intKey==(pIdxKey==0) );
! 53763: lwr = 0;
! 53764: upr = pPage->nCell-1;
! 53765: if( biasRight ){
! 53766: pCur->aiIdx[pCur->iPage] = (u16)(idx = upr);
! 53767: }else{
! 53768: pCur->aiIdx[pCur->iPage] = (u16)(idx = (upr+lwr)/2);
! 53769: }
! 53770: for(;;){
! 53771: u8 *pCell; /* Pointer to current cell in pPage */
! 53772:
! 53773: assert( idx==pCur->aiIdx[pCur->iPage] );
! 53774: pCur->info.nSize = 0;
! 53775: pCell = findCell(pPage, idx) + pPage->childPtrSize;
! 53776: if( pPage->intKey ){
! 53777: i64 nCellKey;
! 53778: if( pPage->hasData ){
! 53779: u32 dummy;
! 53780: pCell += getVarint32(pCell, dummy);
! 53781: }
! 53782: getVarint(pCell, (u64*)&nCellKey);
! 53783: if( nCellKey==intKey ){
! 53784: c = 0;
! 53785: }else if( nCellKey<intKey ){
! 53786: c = -1;
! 53787: }else{
! 53788: assert( nCellKey>intKey );
! 53789: c = +1;
! 53790: }
! 53791: pCur->validNKey = 1;
! 53792: pCur->info.nKey = nCellKey;
! 53793: }else{
! 53794: /* The maximum supported page-size is 65536 bytes. This means that
! 53795: ** the maximum number of record bytes stored on an index B-Tree
! 53796: ** page is less than 16384 bytes and may be stored as a 2-byte
! 53797: ** varint. This information is used to attempt to avoid parsing
! 53798: ** the entire cell by checking for the cases where the record is
! 53799: ** stored entirely within the b-tree page by inspecting the first
! 53800: ** 2 bytes of the cell.
! 53801: */
! 53802: int nCell = pCell[0];
! 53803: if( nCell<=pPage->max1bytePayload
! 53804: /* && (pCell+nCell)<pPage->aDataEnd */
! 53805: ){
! 53806: /* This branch runs if the record-size field of the cell is a
! 53807: ** single byte varint and the record fits entirely on the main
! 53808: ** b-tree page. */
! 53809: testcase( pCell+nCell+1==pPage->aDataEnd );
! 53810: c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
! 53811: }else if( !(pCell[1] & 0x80)
! 53812: && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
! 53813: /* && (pCell+nCell+2)<=pPage->aDataEnd */
! 53814: ){
! 53815: /* The record-size field is a 2 byte varint and the record
! 53816: ** fits entirely on the main b-tree page. */
! 53817: testcase( pCell+nCell+2==pPage->aDataEnd );
! 53818: c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
! 53819: }else{
! 53820: /* The record flows over onto one or more overflow pages. In
! 53821: ** this case the whole cell needs to be parsed, a buffer allocated
! 53822: ** and accessPayload() used to retrieve the record into the
! 53823: ** buffer before VdbeRecordCompare() can be called. */
! 53824: void *pCellKey;
! 53825: u8 * const pCellBody = pCell - pPage->childPtrSize;
! 53826: btreeParseCellPtr(pPage, pCellBody, &pCur->info);
! 53827: nCell = (int)pCur->info.nKey;
! 53828: pCellKey = sqlite3Malloc( nCell );
! 53829: if( pCellKey==0 ){
! 53830: rc = SQLITE_NOMEM;
! 53831: goto moveto_finish;
! 53832: }
! 53833: rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
! 53834: if( rc ){
! 53835: sqlite3_free(pCellKey);
! 53836: goto moveto_finish;
! 53837: }
! 53838: c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
! 53839: sqlite3_free(pCellKey);
! 53840: }
! 53841: }
! 53842: if( c==0 ){
! 53843: if( pPage->intKey && !pPage->leaf ){
! 53844: lwr = idx;
! 53845: break;
! 53846: }else{
! 53847: *pRes = 0;
! 53848: rc = SQLITE_OK;
! 53849: goto moveto_finish;
! 53850: }
! 53851: }
! 53852: if( c<0 ){
! 53853: lwr = idx+1;
! 53854: }else{
! 53855: upr = idx-1;
! 53856: }
! 53857: if( lwr>upr ){
! 53858: break;
! 53859: }
! 53860: pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
! 53861: }
! 53862: assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
! 53863: assert( pPage->isInit );
! 53864: if( pPage->leaf ){
! 53865: chldPg = 0;
! 53866: }else if( lwr>=pPage->nCell ){
! 53867: chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
! 53868: }else{
! 53869: chldPg = get4byte(findCell(pPage, lwr));
! 53870: }
! 53871: if( chldPg==0 ){
! 53872: assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
! 53873: *pRes = c;
! 53874: rc = SQLITE_OK;
! 53875: goto moveto_finish;
! 53876: }
! 53877: pCur->aiIdx[pCur->iPage] = (u16)lwr;
! 53878: pCur->info.nSize = 0;
! 53879: pCur->validNKey = 0;
! 53880: rc = moveToChild(pCur, chldPg);
! 53881: if( rc ) goto moveto_finish;
! 53882: }
! 53883: moveto_finish:
! 53884: return rc;
! 53885: }
! 53886:
! 53887:
! 53888: /*
! 53889: ** Return TRUE if the cursor is not pointing at an entry of the table.
! 53890: **
! 53891: ** TRUE will be returned after a call to sqlite3BtreeNext() moves
! 53892: ** past the last entry in the table or sqlite3BtreePrev() moves past
! 53893: ** the first entry. TRUE is also returned if the table is empty.
! 53894: */
! 53895: SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
! 53896: /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
! 53897: ** have been deleted? This API will need to change to return an error code
! 53898: ** as well as the boolean result value.
! 53899: */
! 53900: return (CURSOR_VALID!=pCur->eState);
! 53901: }
! 53902:
! 53903: /*
! 53904: ** Advance the cursor to the next entry in the database. If
! 53905: ** successful then set *pRes=0. If the cursor
! 53906: ** was already pointing to the last entry in the database before
! 53907: ** this routine was called, then set *pRes=1.
! 53908: */
! 53909: SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
! 53910: int rc;
! 53911: int idx;
! 53912: MemPage *pPage;
! 53913:
! 53914: assert( cursorHoldsMutex(pCur) );
! 53915: rc = restoreCursorPosition(pCur);
! 53916: if( rc!=SQLITE_OK ){
! 53917: return rc;
! 53918: }
! 53919: assert( pRes!=0 );
! 53920: if( CURSOR_INVALID==pCur->eState ){
! 53921: *pRes = 1;
! 53922: return SQLITE_OK;
! 53923: }
! 53924: if( pCur->skipNext>0 ){
! 53925: pCur->skipNext = 0;
! 53926: *pRes = 0;
! 53927: return SQLITE_OK;
! 53928: }
! 53929: pCur->skipNext = 0;
! 53930:
! 53931: pPage = pCur->apPage[pCur->iPage];
! 53932: idx = ++pCur->aiIdx[pCur->iPage];
! 53933: assert( pPage->isInit );
! 53934:
! 53935: /* If the database file is corrupt, it is possible for the value of idx
! 53936: ** to be invalid here. This can only occur if a second cursor modifies
! 53937: ** the page while cursor pCur is holding a reference to it. Which can
! 53938: ** only happen if the database is corrupt in such a way as to link the
! 53939: ** page into more than one b-tree structure. */
! 53940: testcase( idx>pPage->nCell );
! 53941:
! 53942: pCur->info.nSize = 0;
! 53943: pCur->validNKey = 0;
! 53944: if( idx>=pPage->nCell ){
! 53945: if( !pPage->leaf ){
! 53946: rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
! 53947: if( rc ) return rc;
! 53948: rc = moveToLeftmost(pCur);
! 53949: *pRes = 0;
! 53950: return rc;
! 53951: }
! 53952: do{
! 53953: if( pCur->iPage==0 ){
! 53954: *pRes = 1;
! 53955: pCur->eState = CURSOR_INVALID;
! 53956: return SQLITE_OK;
! 53957: }
! 53958: moveToParent(pCur);
! 53959: pPage = pCur->apPage[pCur->iPage];
! 53960: }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
! 53961: *pRes = 0;
! 53962: if( pPage->intKey ){
! 53963: rc = sqlite3BtreeNext(pCur, pRes);
! 53964: }else{
! 53965: rc = SQLITE_OK;
! 53966: }
! 53967: return rc;
! 53968: }
! 53969: *pRes = 0;
! 53970: if( pPage->leaf ){
! 53971: return SQLITE_OK;
! 53972: }
! 53973: rc = moveToLeftmost(pCur);
! 53974: return rc;
! 53975: }
! 53976:
! 53977:
! 53978: /*
! 53979: ** Step the cursor to the back to the previous entry in the database. If
! 53980: ** successful then set *pRes=0. If the cursor
! 53981: ** was already pointing to the first entry in the database before
! 53982: ** this routine was called, then set *pRes=1.
! 53983: */
! 53984: SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
! 53985: int rc;
! 53986: MemPage *pPage;
! 53987:
! 53988: assert( cursorHoldsMutex(pCur) );
! 53989: rc = restoreCursorPosition(pCur);
! 53990: if( rc!=SQLITE_OK ){
! 53991: return rc;
! 53992: }
! 53993: pCur->atLast = 0;
! 53994: if( CURSOR_INVALID==pCur->eState ){
! 53995: *pRes = 1;
! 53996: return SQLITE_OK;
! 53997: }
! 53998: if( pCur->skipNext<0 ){
! 53999: pCur->skipNext = 0;
! 54000: *pRes = 0;
! 54001: return SQLITE_OK;
! 54002: }
! 54003: pCur->skipNext = 0;
! 54004:
! 54005: pPage = pCur->apPage[pCur->iPage];
! 54006: assert( pPage->isInit );
! 54007: if( !pPage->leaf ){
! 54008: int idx = pCur->aiIdx[pCur->iPage];
! 54009: rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
! 54010: if( rc ){
! 54011: return rc;
! 54012: }
! 54013: rc = moveToRightmost(pCur);
! 54014: }else{
! 54015: while( pCur->aiIdx[pCur->iPage]==0 ){
! 54016: if( pCur->iPage==0 ){
! 54017: pCur->eState = CURSOR_INVALID;
! 54018: *pRes = 1;
! 54019: return SQLITE_OK;
! 54020: }
! 54021: moveToParent(pCur);
! 54022: }
! 54023: pCur->info.nSize = 0;
! 54024: pCur->validNKey = 0;
! 54025:
! 54026: pCur->aiIdx[pCur->iPage]--;
! 54027: pPage = pCur->apPage[pCur->iPage];
! 54028: if( pPage->intKey && !pPage->leaf ){
! 54029: rc = sqlite3BtreePrevious(pCur, pRes);
! 54030: }else{
! 54031: rc = SQLITE_OK;
! 54032: }
! 54033: }
! 54034: *pRes = 0;
! 54035: return rc;
! 54036: }
! 54037:
! 54038: /*
! 54039: ** Allocate a new page from the database file.
! 54040: **
! 54041: ** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
! 54042: ** has already been called on the new page.) The new page has also
! 54043: ** been referenced and the calling routine is responsible for calling
! 54044: ** sqlite3PagerUnref() on the new page when it is done.
! 54045: **
! 54046: ** SQLITE_OK is returned on success. Any other return value indicates
! 54047: ** an error. *ppPage and *pPgno are undefined in the event of an error.
! 54048: ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
! 54049: **
! 54050: ** If the "nearby" parameter is not 0, then a (feeble) effort is made to
! 54051: ** locate a page close to the page number "nearby". This can be used in an
! 54052: ** attempt to keep related pages close to each other in the database file,
! 54053: ** which in turn can make database access faster.
! 54054: **
! 54055: ** If the "exact" parameter is not 0, and the page-number nearby exists
! 54056: ** anywhere on the free-list, then it is guarenteed to be returned. This
! 54057: ** is only used by auto-vacuum databases when allocating a new table.
! 54058: */
! 54059: static int allocateBtreePage(
! 54060: BtShared *pBt,
! 54061: MemPage **ppPage,
! 54062: Pgno *pPgno,
! 54063: Pgno nearby,
! 54064: u8 exact
! 54065: ){
! 54066: MemPage *pPage1;
! 54067: int rc;
! 54068: u32 n; /* Number of pages on the freelist */
! 54069: u32 k; /* Number of leaves on the trunk of the freelist */
! 54070: MemPage *pTrunk = 0;
! 54071: MemPage *pPrevTrunk = 0;
! 54072: Pgno mxPage; /* Total size of the database file */
! 54073:
! 54074: assert( sqlite3_mutex_held(pBt->mutex) );
! 54075: pPage1 = pBt->pPage1;
! 54076: mxPage = btreePagecount(pBt);
! 54077: n = get4byte(&pPage1->aData[36]);
! 54078: testcase( n==mxPage-1 );
! 54079: if( n>=mxPage ){
! 54080: return SQLITE_CORRUPT_BKPT;
! 54081: }
! 54082: if( n>0 ){
! 54083: /* There are pages on the freelist. Reuse one of those pages. */
! 54084: Pgno iTrunk;
! 54085: u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
! 54086:
! 54087: /* If the 'exact' parameter was true and a query of the pointer-map
! 54088: ** shows that the page 'nearby' is somewhere on the free-list, then
! 54089: ** the entire-list will be searched for that page.
! 54090: */
! 54091: #ifndef SQLITE_OMIT_AUTOVACUUM
! 54092: if( exact && nearby<=mxPage ){
! 54093: u8 eType;
! 54094: assert( nearby>0 );
! 54095: assert( pBt->autoVacuum );
! 54096: rc = ptrmapGet(pBt, nearby, &eType, 0);
! 54097: if( rc ) return rc;
! 54098: if( eType==PTRMAP_FREEPAGE ){
! 54099: searchList = 1;
! 54100: }
! 54101: *pPgno = nearby;
! 54102: }
! 54103: #endif
! 54104:
! 54105: /* Decrement the free-list count by 1. Set iTrunk to the index of the
! 54106: ** first free-list trunk page. iPrevTrunk is initially 1.
! 54107: */
! 54108: rc = sqlite3PagerWrite(pPage1->pDbPage);
! 54109: if( rc ) return rc;
! 54110: put4byte(&pPage1->aData[36], n-1);
! 54111:
! 54112: /* The code within this loop is run only once if the 'searchList' variable
! 54113: ** is not true. Otherwise, it runs once for each trunk-page on the
! 54114: ** free-list until the page 'nearby' is located.
! 54115: */
! 54116: do {
! 54117: pPrevTrunk = pTrunk;
! 54118: if( pPrevTrunk ){
! 54119: iTrunk = get4byte(&pPrevTrunk->aData[0]);
! 54120: }else{
! 54121: iTrunk = get4byte(&pPage1->aData[32]);
! 54122: }
! 54123: testcase( iTrunk==mxPage );
! 54124: if( iTrunk>mxPage ){
! 54125: rc = SQLITE_CORRUPT_BKPT;
! 54126: }else{
! 54127: rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
! 54128: }
! 54129: if( rc ){
! 54130: pTrunk = 0;
! 54131: goto end_allocate_page;
! 54132: }
! 54133: assert( pTrunk!=0 );
! 54134: assert( pTrunk->aData!=0 );
! 54135:
! 54136: k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
! 54137: if( k==0 && !searchList ){
! 54138: /* The trunk has no leaves and the list is not being searched.
! 54139: ** So extract the trunk page itself and use it as the newly
! 54140: ** allocated page */
! 54141: assert( pPrevTrunk==0 );
! 54142: rc = sqlite3PagerWrite(pTrunk->pDbPage);
! 54143: if( rc ){
! 54144: goto end_allocate_page;
! 54145: }
! 54146: *pPgno = iTrunk;
! 54147: memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
! 54148: *ppPage = pTrunk;
! 54149: pTrunk = 0;
! 54150: TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
! 54151: }else if( k>(u32)(pBt->usableSize/4 - 2) ){
! 54152: /* Value of k is out of range. Database corruption */
! 54153: rc = SQLITE_CORRUPT_BKPT;
! 54154: goto end_allocate_page;
! 54155: #ifndef SQLITE_OMIT_AUTOVACUUM
! 54156: }else if( searchList && nearby==iTrunk ){
! 54157: /* The list is being searched and this trunk page is the page
! 54158: ** to allocate, regardless of whether it has leaves.
! 54159: */
! 54160: assert( *pPgno==iTrunk );
! 54161: *ppPage = pTrunk;
! 54162: searchList = 0;
! 54163: rc = sqlite3PagerWrite(pTrunk->pDbPage);
! 54164: if( rc ){
! 54165: goto end_allocate_page;
! 54166: }
! 54167: if( k==0 ){
! 54168: if( !pPrevTrunk ){
! 54169: memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
! 54170: }else{
! 54171: rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
! 54172: if( rc!=SQLITE_OK ){
! 54173: goto end_allocate_page;
! 54174: }
! 54175: memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
! 54176: }
! 54177: }else{
! 54178: /* The trunk page is required by the caller but it contains
! 54179: ** pointers to free-list leaves. The first leaf becomes a trunk
! 54180: ** page in this case.
! 54181: */
! 54182: MemPage *pNewTrunk;
! 54183: Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
! 54184: if( iNewTrunk>mxPage ){
! 54185: rc = SQLITE_CORRUPT_BKPT;
! 54186: goto end_allocate_page;
! 54187: }
! 54188: testcase( iNewTrunk==mxPage );
! 54189: rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
! 54190: if( rc!=SQLITE_OK ){
! 54191: goto end_allocate_page;
! 54192: }
! 54193: rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
! 54194: if( rc!=SQLITE_OK ){
! 54195: releasePage(pNewTrunk);
! 54196: goto end_allocate_page;
! 54197: }
! 54198: memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
! 54199: put4byte(&pNewTrunk->aData[4], k-1);
! 54200: memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
! 54201: releasePage(pNewTrunk);
! 54202: if( !pPrevTrunk ){
! 54203: assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
! 54204: put4byte(&pPage1->aData[32], iNewTrunk);
! 54205: }else{
! 54206: rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
! 54207: if( rc ){
! 54208: goto end_allocate_page;
! 54209: }
! 54210: put4byte(&pPrevTrunk->aData[0], iNewTrunk);
! 54211: }
! 54212: }
! 54213: pTrunk = 0;
! 54214: TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
! 54215: #endif
! 54216: }else if( k>0 ){
! 54217: /* Extract a leaf from the trunk */
! 54218: u32 closest;
! 54219: Pgno iPage;
! 54220: unsigned char *aData = pTrunk->aData;
! 54221: if( nearby>0 ){
! 54222: u32 i;
! 54223: int dist;
! 54224: closest = 0;
! 54225: dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
! 54226: for(i=1; i<k; i++){
! 54227: int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
! 54228: if( d2<dist ){
! 54229: closest = i;
! 54230: dist = d2;
! 54231: }
! 54232: }
! 54233: }else{
! 54234: closest = 0;
! 54235: }
! 54236:
! 54237: iPage = get4byte(&aData[8+closest*4]);
! 54238: testcase( iPage==mxPage );
! 54239: if( iPage>mxPage ){
! 54240: rc = SQLITE_CORRUPT_BKPT;
! 54241: goto end_allocate_page;
! 54242: }
! 54243: testcase( iPage==mxPage );
! 54244: if( !searchList || iPage==nearby ){
! 54245: int noContent;
! 54246: *pPgno = iPage;
! 54247: TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
! 54248: ": %d more free pages\n",
! 54249: *pPgno, closest+1, k, pTrunk->pgno, n-1));
! 54250: rc = sqlite3PagerWrite(pTrunk->pDbPage);
! 54251: if( rc ) goto end_allocate_page;
! 54252: if( closest<k-1 ){
! 54253: memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
! 54254: }
! 54255: put4byte(&aData[4], k-1);
! 54256: noContent = !btreeGetHasContent(pBt, *pPgno);
! 54257: rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
! 54258: if( rc==SQLITE_OK ){
! 54259: rc = sqlite3PagerWrite((*ppPage)->pDbPage);
! 54260: if( rc!=SQLITE_OK ){
! 54261: releasePage(*ppPage);
! 54262: }
! 54263: }
! 54264: searchList = 0;
! 54265: }
! 54266: }
! 54267: releasePage(pPrevTrunk);
! 54268: pPrevTrunk = 0;
! 54269: }while( searchList );
! 54270: }else{
! 54271: /* There are no pages on the freelist, so create a new page at the
! 54272: ** end of the file */
! 54273: rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
! 54274: if( rc ) return rc;
! 54275: pBt->nPage++;
! 54276: if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
! 54277:
! 54278: #ifndef SQLITE_OMIT_AUTOVACUUM
! 54279: if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
! 54280: /* If *pPgno refers to a pointer-map page, allocate two new pages
! 54281: ** at the end of the file instead of one. The first allocated page
! 54282: ** becomes a new pointer-map page, the second is used by the caller.
! 54283: */
! 54284: MemPage *pPg = 0;
! 54285: TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
! 54286: assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
! 54287: rc = btreeGetPage(pBt, pBt->nPage, &pPg, 1);
! 54288: if( rc==SQLITE_OK ){
! 54289: rc = sqlite3PagerWrite(pPg->pDbPage);
! 54290: releasePage(pPg);
! 54291: }
! 54292: if( rc ) return rc;
! 54293: pBt->nPage++;
! 54294: if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
! 54295: }
! 54296: #endif
! 54297: put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
! 54298: *pPgno = pBt->nPage;
! 54299:
! 54300: assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
! 54301: rc = btreeGetPage(pBt, *pPgno, ppPage, 1);
! 54302: if( rc ) return rc;
! 54303: rc = sqlite3PagerWrite((*ppPage)->pDbPage);
! 54304: if( rc!=SQLITE_OK ){
! 54305: releasePage(*ppPage);
! 54306: }
! 54307: TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
! 54308: }
! 54309:
! 54310: assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
! 54311:
! 54312: end_allocate_page:
! 54313: releasePage(pTrunk);
! 54314: releasePage(pPrevTrunk);
! 54315: if( rc==SQLITE_OK ){
! 54316: if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
! 54317: releasePage(*ppPage);
! 54318: return SQLITE_CORRUPT_BKPT;
! 54319: }
! 54320: (*ppPage)->isInit = 0;
! 54321: }else{
! 54322: *ppPage = 0;
! 54323: }
! 54324: assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
! 54325: return rc;
! 54326: }
! 54327:
! 54328: /*
! 54329: ** This function is used to add page iPage to the database file free-list.
! 54330: ** It is assumed that the page is not already a part of the free-list.
! 54331: **
! 54332: ** The value passed as the second argument to this function is optional.
! 54333: ** If the caller happens to have a pointer to the MemPage object
! 54334: ** corresponding to page iPage handy, it may pass it as the second value.
! 54335: ** Otherwise, it may pass NULL.
! 54336: **
! 54337: ** If a pointer to a MemPage object is passed as the second argument,
! 54338: ** its reference count is not altered by this function.
! 54339: */
! 54340: static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
! 54341: MemPage *pTrunk = 0; /* Free-list trunk page */
! 54342: Pgno iTrunk = 0; /* Page number of free-list trunk page */
! 54343: MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
! 54344: MemPage *pPage; /* Page being freed. May be NULL. */
! 54345: int rc; /* Return Code */
! 54346: int nFree; /* Initial number of pages on free-list */
! 54347:
! 54348: assert( sqlite3_mutex_held(pBt->mutex) );
! 54349: assert( iPage>1 );
! 54350: assert( !pMemPage || pMemPage->pgno==iPage );
! 54351:
! 54352: if( pMemPage ){
! 54353: pPage = pMemPage;
! 54354: sqlite3PagerRef(pPage->pDbPage);
! 54355: }else{
! 54356: pPage = btreePageLookup(pBt, iPage);
! 54357: }
! 54358:
! 54359: /* Increment the free page count on pPage1 */
! 54360: rc = sqlite3PagerWrite(pPage1->pDbPage);
! 54361: if( rc ) goto freepage_out;
! 54362: nFree = get4byte(&pPage1->aData[36]);
! 54363: put4byte(&pPage1->aData[36], nFree+1);
! 54364:
! 54365: if( pBt->btsFlags & BTS_SECURE_DELETE ){
! 54366: /* If the secure_delete option is enabled, then
! 54367: ** always fully overwrite deleted information with zeros.
! 54368: */
! 54369: if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
! 54370: || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
! 54371: ){
! 54372: goto freepage_out;
! 54373: }
! 54374: memset(pPage->aData, 0, pPage->pBt->pageSize);
! 54375: }
! 54376:
! 54377: /* If the database supports auto-vacuum, write an entry in the pointer-map
! 54378: ** to indicate that the page is free.
! 54379: */
! 54380: if( ISAUTOVACUUM ){
! 54381: ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
! 54382: if( rc ) goto freepage_out;
! 54383: }
! 54384:
! 54385: /* Now manipulate the actual database free-list structure. There are two
! 54386: ** possibilities. If the free-list is currently empty, or if the first
! 54387: ** trunk page in the free-list is full, then this page will become a
! 54388: ** new free-list trunk page. Otherwise, it will become a leaf of the
! 54389: ** first trunk page in the current free-list. This block tests if it
! 54390: ** is possible to add the page as a new free-list leaf.
! 54391: */
! 54392: if( nFree!=0 ){
! 54393: u32 nLeaf; /* Initial number of leaf cells on trunk page */
! 54394:
! 54395: iTrunk = get4byte(&pPage1->aData[32]);
! 54396: rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
! 54397: if( rc!=SQLITE_OK ){
! 54398: goto freepage_out;
! 54399: }
! 54400:
! 54401: nLeaf = get4byte(&pTrunk->aData[4]);
! 54402: assert( pBt->usableSize>32 );
! 54403: if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
! 54404: rc = SQLITE_CORRUPT_BKPT;
! 54405: goto freepage_out;
! 54406: }
! 54407: if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
! 54408: /* In this case there is room on the trunk page to insert the page
! 54409: ** being freed as a new leaf.
! 54410: **
! 54411: ** Note that the trunk page is not really full until it contains
! 54412: ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
! 54413: ** coded. But due to a coding error in versions of SQLite prior to
! 54414: ** 3.6.0, databases with freelist trunk pages holding more than
! 54415: ** usableSize/4 - 8 entries will be reported as corrupt. In order
! 54416: ** to maintain backwards compatibility with older versions of SQLite,
! 54417: ** we will continue to restrict the number of entries to usableSize/4 - 8
! 54418: ** for now. At some point in the future (once everyone has upgraded
! 54419: ** to 3.6.0 or later) we should consider fixing the conditional above
! 54420: ** to read "usableSize/4-2" instead of "usableSize/4-8".
! 54421: */
! 54422: rc = sqlite3PagerWrite(pTrunk->pDbPage);
! 54423: if( rc==SQLITE_OK ){
! 54424: put4byte(&pTrunk->aData[4], nLeaf+1);
! 54425: put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
! 54426: if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
! 54427: sqlite3PagerDontWrite(pPage->pDbPage);
! 54428: }
! 54429: rc = btreeSetHasContent(pBt, iPage);
! 54430: }
! 54431: TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
! 54432: goto freepage_out;
! 54433: }
! 54434: }
! 54435:
! 54436: /* If control flows to this point, then it was not possible to add the
! 54437: ** the page being freed as a leaf page of the first trunk in the free-list.
! 54438: ** Possibly because the free-list is empty, or possibly because the
! 54439: ** first trunk in the free-list is full. Either way, the page being freed
! 54440: ** will become the new first trunk page in the free-list.
! 54441: */
! 54442: if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
! 54443: goto freepage_out;
! 54444: }
! 54445: rc = sqlite3PagerWrite(pPage->pDbPage);
! 54446: if( rc!=SQLITE_OK ){
! 54447: goto freepage_out;
! 54448: }
! 54449: put4byte(pPage->aData, iTrunk);
! 54450: put4byte(&pPage->aData[4], 0);
! 54451: put4byte(&pPage1->aData[32], iPage);
! 54452: TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
! 54453:
! 54454: freepage_out:
! 54455: if( pPage ){
! 54456: pPage->isInit = 0;
! 54457: }
! 54458: releasePage(pPage);
! 54459: releasePage(pTrunk);
! 54460: return rc;
! 54461: }
! 54462: static void freePage(MemPage *pPage, int *pRC){
! 54463: if( (*pRC)==SQLITE_OK ){
! 54464: *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
! 54465: }
! 54466: }
! 54467:
! 54468: /*
! 54469: ** Free any overflow pages associated with the given Cell.
! 54470: */
! 54471: static int clearCell(MemPage *pPage, unsigned char *pCell){
! 54472: BtShared *pBt = pPage->pBt;
! 54473: CellInfo info;
! 54474: Pgno ovflPgno;
! 54475: int rc;
! 54476: int nOvfl;
! 54477: u32 ovflPageSize;
! 54478:
! 54479: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 54480: btreeParseCellPtr(pPage, pCell, &info);
! 54481: if( info.iOverflow==0 ){
! 54482: return SQLITE_OK; /* No overflow pages. Return without doing anything */
! 54483: }
! 54484: if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
! 54485: return SQLITE_CORRUPT; /* Cell extends past end of page */
! 54486: }
! 54487: ovflPgno = get4byte(&pCell[info.iOverflow]);
! 54488: assert( pBt->usableSize > 4 );
! 54489: ovflPageSize = pBt->usableSize - 4;
! 54490: nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
! 54491: assert( ovflPgno==0 || nOvfl>0 );
! 54492: while( nOvfl-- ){
! 54493: Pgno iNext = 0;
! 54494: MemPage *pOvfl = 0;
! 54495: if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
! 54496: /* 0 is not a legal page number and page 1 cannot be an
! 54497: ** overflow page. Therefore if ovflPgno<2 or past the end of the
! 54498: ** file the database must be corrupt. */
! 54499: return SQLITE_CORRUPT_BKPT;
! 54500: }
! 54501: if( nOvfl ){
! 54502: rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
! 54503: if( rc ) return rc;
! 54504: }
! 54505:
! 54506: if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
! 54507: && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
! 54508: ){
! 54509: /* There is no reason any cursor should have an outstanding reference
! 54510: ** to an overflow page belonging to a cell that is being deleted/updated.
! 54511: ** So if there exists more than one reference to this page, then it
! 54512: ** must not really be an overflow page and the database must be corrupt.
! 54513: ** It is helpful to detect this before calling freePage2(), as
! 54514: ** freePage2() may zero the page contents if secure-delete mode is
! 54515: ** enabled. If this 'overflow' page happens to be a page that the
! 54516: ** caller is iterating through or using in some other way, this
! 54517: ** can be problematic.
! 54518: */
! 54519: rc = SQLITE_CORRUPT_BKPT;
! 54520: }else{
! 54521: rc = freePage2(pBt, pOvfl, ovflPgno);
! 54522: }
! 54523:
! 54524: if( pOvfl ){
! 54525: sqlite3PagerUnref(pOvfl->pDbPage);
! 54526: }
! 54527: if( rc ) return rc;
! 54528: ovflPgno = iNext;
! 54529: }
! 54530: return SQLITE_OK;
! 54531: }
! 54532:
! 54533: /*
! 54534: ** Create the byte sequence used to represent a cell on page pPage
! 54535: ** and write that byte sequence into pCell[]. Overflow pages are
! 54536: ** allocated and filled in as necessary. The calling procedure
! 54537: ** is responsible for making sure sufficient space has been allocated
! 54538: ** for pCell[].
! 54539: **
! 54540: ** Note that pCell does not necessary need to point to the pPage->aData
! 54541: ** area. pCell might point to some temporary storage. The cell will
! 54542: ** be constructed in this temporary area then copied into pPage->aData
! 54543: ** later.
! 54544: */
! 54545: static int fillInCell(
! 54546: MemPage *pPage, /* The page that contains the cell */
! 54547: unsigned char *pCell, /* Complete text of the cell */
! 54548: const void *pKey, i64 nKey, /* The key */
! 54549: const void *pData,int nData, /* The data */
! 54550: int nZero, /* Extra zero bytes to append to pData */
! 54551: int *pnSize /* Write cell size here */
! 54552: ){
! 54553: int nPayload;
! 54554: const u8 *pSrc;
! 54555: int nSrc, n, rc;
! 54556: int spaceLeft;
! 54557: MemPage *pOvfl = 0;
! 54558: MemPage *pToRelease = 0;
! 54559: unsigned char *pPrior;
! 54560: unsigned char *pPayload;
! 54561: BtShared *pBt = pPage->pBt;
! 54562: Pgno pgnoOvfl = 0;
! 54563: int nHeader;
! 54564: CellInfo info;
! 54565:
! 54566: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 54567:
! 54568: /* pPage is not necessarily writeable since pCell might be auxiliary
! 54569: ** buffer space that is separate from the pPage buffer area */
! 54570: assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
! 54571: || sqlite3PagerIswriteable(pPage->pDbPage) );
! 54572:
! 54573: /* Fill in the header. */
! 54574: nHeader = 0;
! 54575: if( !pPage->leaf ){
! 54576: nHeader += 4;
! 54577: }
! 54578: if( pPage->hasData ){
! 54579: nHeader += putVarint(&pCell[nHeader], nData+nZero);
! 54580: }else{
! 54581: nData = nZero = 0;
! 54582: }
! 54583: nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
! 54584: btreeParseCellPtr(pPage, pCell, &info);
! 54585: assert( info.nHeader==nHeader );
! 54586: assert( info.nKey==nKey );
! 54587: assert( info.nData==(u32)(nData+nZero) );
! 54588:
! 54589: /* Fill in the payload */
! 54590: nPayload = nData + nZero;
! 54591: if( pPage->intKey ){
! 54592: pSrc = pData;
! 54593: nSrc = nData;
! 54594: nData = 0;
! 54595: }else{
! 54596: if( NEVER(nKey>0x7fffffff || pKey==0) ){
! 54597: return SQLITE_CORRUPT_BKPT;
! 54598: }
! 54599: nPayload += (int)nKey;
! 54600: pSrc = pKey;
! 54601: nSrc = (int)nKey;
! 54602: }
! 54603: *pnSize = info.nSize;
! 54604: spaceLeft = info.nLocal;
! 54605: pPayload = &pCell[nHeader];
! 54606: pPrior = &pCell[info.iOverflow];
! 54607:
! 54608: while( nPayload>0 ){
! 54609: if( spaceLeft==0 ){
! 54610: #ifndef SQLITE_OMIT_AUTOVACUUM
! 54611: Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
! 54612: if( pBt->autoVacuum ){
! 54613: do{
! 54614: pgnoOvfl++;
! 54615: } while(
! 54616: PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
! 54617: );
! 54618: }
! 54619: #endif
! 54620: rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
! 54621: #ifndef SQLITE_OMIT_AUTOVACUUM
! 54622: /* If the database supports auto-vacuum, and the second or subsequent
! 54623: ** overflow page is being allocated, add an entry to the pointer-map
! 54624: ** for that page now.
! 54625: **
! 54626: ** If this is the first overflow page, then write a partial entry
! 54627: ** to the pointer-map. If we write nothing to this pointer-map slot,
! 54628: ** then the optimistic overflow chain processing in clearCell()
! 54629: ** may misinterpret the uninitialised values and delete the
! 54630: ** wrong pages from the database.
! 54631: */
! 54632: if( pBt->autoVacuum && rc==SQLITE_OK ){
! 54633: u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
! 54634: ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
! 54635: if( rc ){
! 54636: releasePage(pOvfl);
! 54637: }
! 54638: }
! 54639: #endif
! 54640: if( rc ){
! 54641: releasePage(pToRelease);
! 54642: return rc;
! 54643: }
! 54644:
! 54645: /* If pToRelease is not zero than pPrior points into the data area
! 54646: ** of pToRelease. Make sure pToRelease is still writeable. */
! 54647: assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
! 54648:
! 54649: /* If pPrior is part of the data area of pPage, then make sure pPage
! 54650: ** is still writeable */
! 54651: assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
! 54652: || sqlite3PagerIswriteable(pPage->pDbPage) );
! 54653:
! 54654: put4byte(pPrior, pgnoOvfl);
! 54655: releasePage(pToRelease);
! 54656: pToRelease = pOvfl;
! 54657: pPrior = pOvfl->aData;
! 54658: put4byte(pPrior, 0);
! 54659: pPayload = &pOvfl->aData[4];
! 54660: spaceLeft = pBt->usableSize - 4;
! 54661: }
! 54662: n = nPayload;
! 54663: if( n>spaceLeft ) n = spaceLeft;
! 54664:
! 54665: /* If pToRelease is not zero than pPayload points into the data area
! 54666: ** of pToRelease. Make sure pToRelease is still writeable. */
! 54667: assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
! 54668:
! 54669: /* If pPayload is part of the data area of pPage, then make sure pPage
! 54670: ** is still writeable */
! 54671: assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
! 54672: || sqlite3PagerIswriteable(pPage->pDbPage) );
! 54673:
! 54674: if( nSrc>0 ){
! 54675: if( n>nSrc ) n = nSrc;
! 54676: assert( pSrc );
! 54677: memcpy(pPayload, pSrc, n);
! 54678: }else{
! 54679: memset(pPayload, 0, n);
! 54680: }
! 54681: nPayload -= n;
! 54682: pPayload += n;
! 54683: pSrc += n;
! 54684: nSrc -= n;
! 54685: spaceLeft -= n;
! 54686: if( nSrc==0 ){
! 54687: nSrc = nData;
! 54688: pSrc = pData;
! 54689: }
! 54690: }
! 54691: releasePage(pToRelease);
! 54692: return SQLITE_OK;
! 54693: }
! 54694:
! 54695: /*
! 54696: ** Remove the i-th cell from pPage. This routine effects pPage only.
! 54697: ** The cell content is not freed or deallocated. It is assumed that
! 54698: ** the cell content has been copied someplace else. This routine just
! 54699: ** removes the reference to the cell from pPage.
! 54700: **
! 54701: ** "sz" must be the number of bytes in the cell.
! 54702: */
! 54703: static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
! 54704: u32 pc; /* Offset to cell content of cell being deleted */
! 54705: u8 *data; /* pPage->aData */
! 54706: u8 *ptr; /* Used to move bytes around within data[] */
! 54707: u8 *endPtr; /* End of loop */
! 54708: int rc; /* The return code */
! 54709: int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
! 54710:
! 54711: if( *pRC ) return;
! 54712:
! 54713: assert( idx>=0 && idx<pPage->nCell );
! 54714: assert( sz==cellSize(pPage, idx) );
! 54715: assert( sqlite3PagerIswriteable(pPage->pDbPage) );
! 54716: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 54717: data = pPage->aData;
! 54718: ptr = &pPage->aCellIdx[2*idx];
! 54719: pc = get2byte(ptr);
! 54720: hdr = pPage->hdrOffset;
! 54721: testcase( pc==get2byte(&data[hdr+5]) );
! 54722: testcase( pc+sz==pPage->pBt->usableSize );
! 54723: if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
! 54724: *pRC = SQLITE_CORRUPT_BKPT;
! 54725: return;
! 54726: }
! 54727: rc = freeSpace(pPage, pc, sz);
! 54728: if( rc ){
! 54729: *pRC = rc;
! 54730: return;
! 54731: }
! 54732: endPtr = &pPage->aCellIdx[2*pPage->nCell - 2];
! 54733: assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 ); /* ptr is always 2-byte aligned */
! 54734: while( ptr<endPtr ){
! 54735: *(u16*)ptr = *(u16*)&ptr[2];
! 54736: ptr += 2;
! 54737: }
! 54738: pPage->nCell--;
! 54739: put2byte(&data[hdr+3], pPage->nCell);
! 54740: pPage->nFree += 2;
! 54741: }
! 54742:
! 54743: /*
! 54744: ** Insert a new cell on pPage at cell index "i". pCell points to the
! 54745: ** content of the cell.
! 54746: **
! 54747: ** If the cell content will fit on the page, then put it there. If it
! 54748: ** will not fit, then make a copy of the cell content into pTemp if
! 54749: ** pTemp is not null. Regardless of pTemp, allocate a new entry
! 54750: ** in pPage->aOvfl[] and make it point to the cell content (either
! 54751: ** in pTemp or the original pCell) and also record its index.
! 54752: ** Allocating a new entry in pPage->aCell[] implies that
! 54753: ** pPage->nOverflow is incremented.
! 54754: **
! 54755: ** If nSkip is non-zero, then do not copy the first nSkip bytes of the
! 54756: ** cell. The caller will overwrite them after this function returns. If
! 54757: ** nSkip is non-zero, then pCell may not point to an invalid memory location
! 54758: ** (but pCell+nSkip is always valid).
! 54759: */
! 54760: static void insertCell(
! 54761: MemPage *pPage, /* Page into which we are copying */
! 54762: int i, /* New cell becomes the i-th cell of the page */
! 54763: u8 *pCell, /* Content of the new cell */
! 54764: int sz, /* Bytes of content in pCell */
! 54765: u8 *pTemp, /* Temp storage space for pCell, if needed */
! 54766: Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
! 54767: int *pRC /* Read and write return code from here */
! 54768: ){
! 54769: int idx = 0; /* Where to write new cell content in data[] */
! 54770: int j; /* Loop counter */
! 54771: int end; /* First byte past the last cell pointer in data[] */
! 54772: int ins; /* Index in data[] where new cell pointer is inserted */
! 54773: int cellOffset; /* Address of first cell pointer in data[] */
! 54774: u8 *data; /* The content of the whole page */
! 54775: u8 *ptr; /* Used for moving information around in data[] */
! 54776: u8 *endPtr; /* End of the loop */
! 54777:
! 54778: int nSkip = (iChild ? 4 : 0);
! 54779:
! 54780: if( *pRC ) return;
! 54781:
! 54782: assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
! 54783: assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
! 54784: assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
! 54785: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 54786: /* The cell should normally be sized correctly. However, when moving a
! 54787: ** malformed cell from a leaf page to an interior page, if the cell size
! 54788: ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
! 54789: ** might be less than 8 (leaf-size + pointer) on the interior node. Hence
! 54790: ** the term after the || in the following assert(). */
! 54791: assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
! 54792: if( pPage->nOverflow || sz+2>pPage->nFree ){
! 54793: if( pTemp ){
! 54794: memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
! 54795: pCell = pTemp;
! 54796: }
! 54797: if( iChild ){
! 54798: put4byte(pCell, iChild);
! 54799: }
! 54800: j = pPage->nOverflow++;
! 54801: assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
! 54802: pPage->aOvfl[j].pCell = pCell;
! 54803: pPage->aOvfl[j].idx = (u16)i;
! 54804: }else{
! 54805: int rc = sqlite3PagerWrite(pPage->pDbPage);
! 54806: if( rc!=SQLITE_OK ){
! 54807: *pRC = rc;
! 54808: return;
! 54809: }
! 54810: assert( sqlite3PagerIswriteable(pPage->pDbPage) );
! 54811: data = pPage->aData;
! 54812: cellOffset = pPage->cellOffset;
! 54813: end = cellOffset + 2*pPage->nCell;
! 54814: ins = cellOffset + 2*i;
! 54815: rc = allocateSpace(pPage, sz, &idx);
! 54816: if( rc ){ *pRC = rc; return; }
! 54817: /* The allocateSpace() routine guarantees the following two properties
! 54818: ** if it returns success */
! 54819: assert( idx >= end+2 );
! 54820: assert( idx+sz <= (int)pPage->pBt->usableSize );
! 54821: pPage->nCell++;
! 54822: pPage->nFree -= (u16)(2 + sz);
! 54823: memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
! 54824: if( iChild ){
! 54825: put4byte(&data[idx], iChild);
! 54826: }
! 54827: ptr = &data[end];
! 54828: endPtr = &data[ins];
! 54829: assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 ); /* ptr is always 2-byte aligned */
! 54830: while( ptr>endPtr ){
! 54831: *(u16*)ptr = *(u16*)&ptr[-2];
! 54832: ptr -= 2;
! 54833: }
! 54834: put2byte(&data[ins], idx);
! 54835: put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
! 54836: #ifndef SQLITE_OMIT_AUTOVACUUM
! 54837: if( pPage->pBt->autoVacuum ){
! 54838: /* The cell may contain a pointer to an overflow page. If so, write
! 54839: ** the entry for the overflow page into the pointer map.
! 54840: */
! 54841: ptrmapPutOvflPtr(pPage, pCell, pRC);
! 54842: }
! 54843: #endif
! 54844: }
! 54845: }
! 54846:
! 54847: /*
! 54848: ** Add a list of cells to a page. The page should be initially empty.
! 54849: ** The cells are guaranteed to fit on the page.
! 54850: */
! 54851: static void assemblePage(
! 54852: MemPage *pPage, /* The page to be assemblied */
! 54853: int nCell, /* The number of cells to add to this page */
! 54854: u8 **apCell, /* Pointers to cell bodies */
! 54855: u16 *aSize /* Sizes of the cells */
! 54856: ){
! 54857: int i; /* Loop counter */
! 54858: u8 *pCellptr; /* Address of next cell pointer */
! 54859: int cellbody; /* Address of next cell body */
! 54860: u8 * const data = pPage->aData; /* Pointer to data for pPage */
! 54861: const int hdr = pPage->hdrOffset; /* Offset of header on pPage */
! 54862: const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
! 54863:
! 54864: assert( pPage->nOverflow==0 );
! 54865: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 54866: assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
! 54867: && (int)MX_CELL(pPage->pBt)<=10921);
! 54868: assert( sqlite3PagerIswriteable(pPage->pDbPage) );
! 54869:
! 54870: /* Check that the page has just been zeroed by zeroPage() */
! 54871: assert( pPage->nCell==0 );
! 54872: assert( get2byteNotZero(&data[hdr+5])==nUsable );
! 54873:
! 54874: pCellptr = &pPage->aCellIdx[nCell*2];
! 54875: cellbody = nUsable;
! 54876: for(i=nCell-1; i>=0; i--){
! 54877: u16 sz = aSize[i];
! 54878: pCellptr -= 2;
! 54879: cellbody -= sz;
! 54880: put2byte(pCellptr, cellbody);
! 54881: memcpy(&data[cellbody], apCell[i], sz);
! 54882: }
! 54883: put2byte(&data[hdr+3], nCell);
! 54884: put2byte(&data[hdr+5], cellbody);
! 54885: pPage->nFree -= (nCell*2 + nUsable - cellbody);
! 54886: pPage->nCell = (u16)nCell;
! 54887: }
! 54888:
! 54889: /*
! 54890: ** The following parameters determine how many adjacent pages get involved
! 54891: ** in a balancing operation. NN is the number of neighbors on either side
! 54892: ** of the page that participate in the balancing operation. NB is the
! 54893: ** total number of pages that participate, including the target page and
! 54894: ** NN neighbors on either side.
! 54895: **
! 54896: ** The minimum value of NN is 1 (of course). Increasing NN above 1
! 54897: ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
! 54898: ** in exchange for a larger degradation in INSERT and UPDATE performance.
! 54899: ** The value of NN appears to give the best results overall.
! 54900: */
! 54901: #define NN 1 /* Number of neighbors on either side of pPage */
! 54902: #define NB (NN*2+1) /* Total pages involved in the balance */
! 54903:
! 54904:
! 54905: #ifndef SQLITE_OMIT_QUICKBALANCE
! 54906: /*
! 54907: ** This version of balance() handles the common special case where
! 54908: ** a new entry is being inserted on the extreme right-end of the
! 54909: ** tree, in other words, when the new entry will become the largest
! 54910: ** entry in the tree.
! 54911: **
! 54912: ** Instead of trying to balance the 3 right-most leaf pages, just add
! 54913: ** a new page to the right-hand side and put the one new entry in
! 54914: ** that page. This leaves the right side of the tree somewhat
! 54915: ** unbalanced. But odds are that we will be inserting new entries
! 54916: ** at the end soon afterwards so the nearly empty page will quickly
! 54917: ** fill up. On average.
! 54918: **
! 54919: ** pPage is the leaf page which is the right-most page in the tree.
! 54920: ** pParent is its parent. pPage must have a single overflow entry
! 54921: ** which is also the right-most entry on the page.
! 54922: **
! 54923: ** The pSpace buffer is used to store a temporary copy of the divider
! 54924: ** cell that will be inserted into pParent. Such a cell consists of a 4
! 54925: ** byte page number followed by a variable length integer. In other
! 54926: ** words, at most 13 bytes. Hence the pSpace buffer must be at
! 54927: ** least 13 bytes in size.
! 54928: */
! 54929: static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
! 54930: BtShared *const pBt = pPage->pBt; /* B-Tree Database */
! 54931: MemPage *pNew; /* Newly allocated page */
! 54932: int rc; /* Return Code */
! 54933: Pgno pgnoNew; /* Page number of pNew */
! 54934:
! 54935: assert( sqlite3_mutex_held(pPage->pBt->mutex) );
! 54936: assert( sqlite3PagerIswriteable(pParent->pDbPage) );
! 54937: assert( pPage->nOverflow==1 );
! 54938:
! 54939: /* This error condition is now caught prior to reaching this function */
! 54940: if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
! 54941:
! 54942: /* Allocate a new page. This page will become the right-sibling of
! 54943: ** pPage. Make the parent page writable, so that the new divider cell
! 54944: ** may be inserted. If both these operations are successful, proceed.
! 54945: */
! 54946: rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
! 54947:
! 54948: if( rc==SQLITE_OK ){
! 54949:
! 54950: u8 *pOut = &pSpace[4];
! 54951: u8 *pCell = pPage->aOvfl[0].pCell;
! 54952: u16 szCell = cellSizePtr(pPage, pCell);
! 54953: u8 *pStop;
! 54954:
! 54955: assert( sqlite3PagerIswriteable(pNew->pDbPage) );
! 54956: assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
! 54957: zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
! 54958: assemblePage(pNew, 1, &pCell, &szCell);
! 54959:
! 54960: /* If this is an auto-vacuum database, update the pointer map
! 54961: ** with entries for the new page, and any pointer from the
! 54962: ** cell on the page to an overflow page. If either of these
! 54963: ** operations fails, the return code is set, but the contents
! 54964: ** of the parent page are still manipulated by thh code below.
! 54965: ** That is Ok, at this point the parent page is guaranteed to
! 54966: ** be marked as dirty. Returning an error code will cause a
! 54967: ** rollback, undoing any changes made to the parent page.
! 54968: */
! 54969: if( ISAUTOVACUUM ){
! 54970: ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
! 54971: if( szCell>pNew->minLocal ){
! 54972: ptrmapPutOvflPtr(pNew, pCell, &rc);
! 54973: }
! 54974: }
! 54975:
! 54976: /* Create a divider cell to insert into pParent. The divider cell
! 54977: ** consists of a 4-byte page number (the page number of pPage) and
! 54978: ** a variable length key value (which must be the same value as the
! 54979: ** largest key on pPage).
! 54980: **
! 54981: ** To find the largest key value on pPage, first find the right-most
! 54982: ** cell on pPage. The first two fields of this cell are the
! 54983: ** record-length (a variable length integer at most 32-bits in size)
! 54984: ** and the key value (a variable length integer, may have any value).
! 54985: ** The first of the while(...) loops below skips over the record-length
! 54986: ** field. The second while(...) loop copies the key value from the
! 54987: ** cell on pPage into the pSpace buffer.
! 54988: */
! 54989: pCell = findCell(pPage, pPage->nCell-1);
! 54990: pStop = &pCell[9];
! 54991: while( (*(pCell++)&0x80) && pCell<pStop );
! 54992: pStop = &pCell[9];
! 54993: while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
! 54994:
! 54995: /* Insert the new divider cell into pParent. */
! 54996: insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
! 54997: 0, pPage->pgno, &rc);
! 54998:
! 54999: /* Set the right-child pointer of pParent to point to the new page. */
! 55000: put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
! 55001:
! 55002: /* Release the reference to the new page. */
! 55003: releasePage(pNew);
! 55004: }
! 55005:
! 55006: return rc;
! 55007: }
! 55008: #endif /* SQLITE_OMIT_QUICKBALANCE */
! 55009:
! 55010: #if 0
! 55011: /*
! 55012: ** This function does not contribute anything to the operation of SQLite.
! 55013: ** it is sometimes activated temporarily while debugging code responsible
! 55014: ** for setting pointer-map entries.
! 55015: */
! 55016: static int ptrmapCheckPages(MemPage **apPage, int nPage){
! 55017: int i, j;
! 55018: for(i=0; i<nPage; i++){
! 55019: Pgno n;
! 55020: u8 e;
! 55021: MemPage *pPage = apPage[i];
! 55022: BtShared *pBt = pPage->pBt;
! 55023: assert( pPage->isInit );
! 55024:
! 55025: for(j=0; j<pPage->nCell; j++){
! 55026: CellInfo info;
! 55027: u8 *z;
! 55028:
! 55029: z = findCell(pPage, j);
! 55030: btreeParseCellPtr(pPage, z, &info);
! 55031: if( info.iOverflow ){
! 55032: Pgno ovfl = get4byte(&z[info.iOverflow]);
! 55033: ptrmapGet(pBt, ovfl, &e, &n);
! 55034: assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
! 55035: }
! 55036: if( !pPage->leaf ){
! 55037: Pgno child = get4byte(z);
! 55038: ptrmapGet(pBt, child, &e, &n);
! 55039: assert( n==pPage->pgno && e==PTRMAP_BTREE );
! 55040: }
! 55041: }
! 55042: if( !pPage->leaf ){
! 55043: Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
! 55044: ptrmapGet(pBt, child, &e, &n);
! 55045: assert( n==pPage->pgno && e==PTRMAP_BTREE );
! 55046: }
! 55047: }
! 55048: return 1;
! 55049: }
! 55050: #endif
! 55051:
! 55052: /*
! 55053: ** This function is used to copy the contents of the b-tree node stored
! 55054: ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
! 55055: ** the pointer-map entries for each child page are updated so that the
! 55056: ** parent page stored in the pointer map is page pTo. If pFrom contained
! 55057: ** any cells with overflow page pointers, then the corresponding pointer
! 55058: ** map entries are also updated so that the parent page is page pTo.
! 55059: **
! 55060: ** If pFrom is currently carrying any overflow cells (entries in the
! 55061: ** MemPage.aOvfl[] array), they are not copied to pTo.
! 55062: **
! 55063: ** Before returning, page pTo is reinitialized using btreeInitPage().
! 55064: **
! 55065: ** The performance of this function is not critical. It is only used by
! 55066: ** the balance_shallower() and balance_deeper() procedures, neither of
! 55067: ** which are called often under normal circumstances.
! 55068: */
! 55069: static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
! 55070: if( (*pRC)==SQLITE_OK ){
! 55071: BtShared * const pBt = pFrom->pBt;
! 55072: u8 * const aFrom = pFrom->aData;
! 55073: u8 * const aTo = pTo->aData;
! 55074: int const iFromHdr = pFrom->hdrOffset;
! 55075: int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
! 55076: int rc;
! 55077: int iData;
! 55078:
! 55079:
! 55080: assert( pFrom->isInit );
! 55081: assert( pFrom->nFree>=iToHdr );
! 55082: assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
! 55083:
! 55084: /* Copy the b-tree node content from page pFrom to page pTo. */
! 55085: iData = get2byte(&aFrom[iFromHdr+5]);
! 55086: memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
! 55087: memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
! 55088:
! 55089: /* Reinitialize page pTo so that the contents of the MemPage structure
! 55090: ** match the new data. The initialization of pTo can actually fail under
! 55091: ** fairly obscure circumstances, even though it is a copy of initialized
! 55092: ** page pFrom.
! 55093: */
! 55094: pTo->isInit = 0;
! 55095: rc = btreeInitPage(pTo);
! 55096: if( rc!=SQLITE_OK ){
! 55097: *pRC = rc;
! 55098: return;
! 55099: }
! 55100:
! 55101: /* If this is an auto-vacuum database, update the pointer-map entries
! 55102: ** for any b-tree or overflow pages that pTo now contains the pointers to.
! 55103: */
! 55104: if( ISAUTOVACUUM ){
! 55105: *pRC = setChildPtrmaps(pTo);
! 55106: }
! 55107: }
! 55108: }
! 55109:
! 55110: /*
! 55111: ** This routine redistributes cells on the iParentIdx'th child of pParent
! 55112: ** (hereafter "the page") and up to 2 siblings so that all pages have about the
! 55113: ** same amount of free space. Usually a single sibling on either side of the
! 55114: ** page are used in the balancing, though both siblings might come from one
! 55115: ** side if the page is the first or last child of its parent. If the page
! 55116: ** has fewer than 2 siblings (something which can only happen if the page
! 55117: ** is a root page or a child of a root page) then all available siblings
! 55118: ** participate in the balancing.
! 55119: **
! 55120: ** The number of siblings of the page might be increased or decreased by
! 55121: ** one or two in an effort to keep pages nearly full but not over full.
! 55122: **
! 55123: ** Note that when this routine is called, some of the cells on the page
! 55124: ** might not actually be stored in MemPage.aData[]. This can happen
! 55125: ** if the page is overfull. This routine ensures that all cells allocated
! 55126: ** to the page and its siblings fit into MemPage.aData[] before returning.
! 55127: **
! 55128: ** In the course of balancing the page and its siblings, cells may be
! 55129: ** inserted into or removed from the parent page (pParent). Doing so
! 55130: ** may cause the parent page to become overfull or underfull. If this
! 55131: ** happens, it is the responsibility of the caller to invoke the correct
! 55132: ** balancing routine to fix this problem (see the balance() routine).
! 55133: **
! 55134: ** If this routine fails for any reason, it might leave the database
! 55135: ** in a corrupted state. So if this routine fails, the database should
! 55136: ** be rolled back.
! 55137: **
! 55138: ** The third argument to this function, aOvflSpace, is a pointer to a
! 55139: ** buffer big enough to hold one page. If while inserting cells into the parent
! 55140: ** page (pParent) the parent page becomes overfull, this buffer is
! 55141: ** used to store the parent's overflow cells. Because this function inserts
! 55142: ** a maximum of four divider cells into the parent page, and the maximum
! 55143: ** size of a cell stored within an internal node is always less than 1/4
! 55144: ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
! 55145: ** enough for all overflow cells.
! 55146: **
! 55147: ** If aOvflSpace is set to a null pointer, this function returns
! 55148: ** SQLITE_NOMEM.
! 55149: */
! 55150: static int balance_nonroot(
! 55151: MemPage *pParent, /* Parent page of siblings being balanced */
! 55152: int iParentIdx, /* Index of "the page" in pParent */
! 55153: u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
! 55154: int isRoot /* True if pParent is a root-page */
! 55155: ){
! 55156: BtShared *pBt; /* The whole database */
! 55157: int nCell = 0; /* Number of cells in apCell[] */
! 55158: int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
! 55159: int nNew = 0; /* Number of pages in apNew[] */
! 55160: int nOld; /* Number of pages in apOld[] */
! 55161: int i, j, k; /* Loop counters */
! 55162: int nxDiv; /* Next divider slot in pParent->aCell[] */
! 55163: int rc = SQLITE_OK; /* The return code */
! 55164: u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
! 55165: int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
! 55166: int usableSpace; /* Bytes in pPage beyond the header */
! 55167: int pageFlags; /* Value of pPage->aData[0] */
! 55168: int subtotal; /* Subtotal of bytes in cells on one page */
! 55169: int iSpace1 = 0; /* First unused byte of aSpace1[] */
! 55170: int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
! 55171: int szScratch; /* Size of scratch memory requested */
! 55172: MemPage *apOld[NB]; /* pPage and up to two siblings */
! 55173: MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
! 55174: MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
! 55175: u8 *pRight; /* Location in parent of right-sibling pointer */
! 55176: u8 *apDiv[NB-1]; /* Divider cells in pParent */
! 55177: int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
! 55178: int szNew[NB+2]; /* Combined size of cells place on i-th page */
! 55179: u8 **apCell = 0; /* All cells begin balanced */
! 55180: u16 *szCell; /* Local size of all cells in apCell[] */
! 55181: u8 *aSpace1; /* Space for copies of dividers cells */
! 55182: Pgno pgno; /* Temp var to store a page number in */
! 55183:
! 55184: pBt = pParent->pBt;
! 55185: assert( sqlite3_mutex_held(pBt->mutex) );
! 55186: assert( sqlite3PagerIswriteable(pParent->pDbPage) );
! 55187:
! 55188: #if 0
! 55189: TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
! 55190: #endif
! 55191:
! 55192: /* At this point pParent may have at most one overflow cell. And if
! 55193: ** this overflow cell is present, it must be the cell with
! 55194: ** index iParentIdx. This scenario comes about when this function
! 55195: ** is called (indirectly) from sqlite3BtreeDelete().
! 55196: */
! 55197: assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
! 55198: assert( pParent->nOverflow==0 || pParent->aOvfl[0].idx==iParentIdx );
! 55199:
! 55200: if( !aOvflSpace ){
! 55201: return SQLITE_NOMEM;
! 55202: }
! 55203:
! 55204: /* Find the sibling pages to balance. Also locate the cells in pParent
! 55205: ** that divide the siblings. An attempt is made to find NN siblings on
! 55206: ** either side of pPage. More siblings are taken from one side, however,
! 55207: ** if there are fewer than NN siblings on the other side. If pParent
! 55208: ** has NB or fewer children then all children of pParent are taken.
! 55209: **
! 55210: ** This loop also drops the divider cells from the parent page. This
! 55211: ** way, the remainder of the function does not have to deal with any
! 55212: ** overflow cells in the parent page, since if any existed they will
! 55213: ** have already been removed.
! 55214: */
! 55215: i = pParent->nOverflow + pParent->nCell;
! 55216: if( i<2 ){
! 55217: nxDiv = 0;
! 55218: nOld = i+1;
! 55219: }else{
! 55220: nOld = 3;
! 55221: if( iParentIdx==0 ){
! 55222: nxDiv = 0;
! 55223: }else if( iParentIdx==i ){
! 55224: nxDiv = i-2;
! 55225: }else{
! 55226: nxDiv = iParentIdx-1;
! 55227: }
! 55228: i = 2;
! 55229: }
! 55230: if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
! 55231: pRight = &pParent->aData[pParent->hdrOffset+8];
! 55232: }else{
! 55233: pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
! 55234: }
! 55235: pgno = get4byte(pRight);
! 55236: while( 1 ){
! 55237: rc = getAndInitPage(pBt, pgno, &apOld[i]);
! 55238: if( rc ){
! 55239: memset(apOld, 0, (i+1)*sizeof(MemPage*));
! 55240: goto balance_cleanup;
! 55241: }
! 55242: nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
! 55243: if( (i--)==0 ) break;
! 55244:
! 55245: if( i+nxDiv==pParent->aOvfl[0].idx && pParent->nOverflow ){
! 55246: apDiv[i] = pParent->aOvfl[0].pCell;
! 55247: pgno = get4byte(apDiv[i]);
! 55248: szNew[i] = cellSizePtr(pParent, apDiv[i]);
! 55249: pParent->nOverflow = 0;
! 55250: }else{
! 55251: apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
! 55252: pgno = get4byte(apDiv[i]);
! 55253: szNew[i] = cellSizePtr(pParent, apDiv[i]);
! 55254:
! 55255: /* Drop the cell from the parent page. apDiv[i] still points to
! 55256: ** the cell within the parent, even though it has been dropped.
! 55257: ** This is safe because dropping a cell only overwrites the first
! 55258: ** four bytes of it, and this function does not need the first
! 55259: ** four bytes of the divider cell. So the pointer is safe to use
! 55260: ** later on.
! 55261: **
! 55262: ** But not if we are in secure-delete mode. In secure-delete mode,
! 55263: ** the dropCell() routine will overwrite the entire cell with zeroes.
! 55264: ** In this case, temporarily copy the cell into the aOvflSpace[]
! 55265: ** buffer. It will be copied out again as soon as the aSpace[] buffer
! 55266: ** is allocated. */
! 55267: if( pBt->btsFlags & BTS_SECURE_DELETE ){
! 55268: int iOff;
! 55269:
! 55270: iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
! 55271: if( (iOff+szNew[i])>(int)pBt->usableSize ){
! 55272: rc = SQLITE_CORRUPT_BKPT;
! 55273: memset(apOld, 0, (i+1)*sizeof(MemPage*));
! 55274: goto balance_cleanup;
! 55275: }else{
! 55276: memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
! 55277: apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
! 55278: }
! 55279: }
! 55280: dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
! 55281: }
! 55282: }
! 55283:
! 55284: /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
! 55285: ** alignment */
! 55286: nMaxCells = (nMaxCells + 3)&~3;
! 55287:
! 55288: /*
! 55289: ** Allocate space for memory structures
! 55290: */
! 55291: k = pBt->pageSize + ROUND8(sizeof(MemPage));
! 55292: szScratch =
! 55293: nMaxCells*sizeof(u8*) /* apCell */
! 55294: + nMaxCells*sizeof(u16) /* szCell */
! 55295: + pBt->pageSize /* aSpace1 */
! 55296: + k*nOld; /* Page copies (apCopy) */
! 55297: apCell = sqlite3ScratchMalloc( szScratch );
! 55298: if( apCell==0 ){
! 55299: rc = SQLITE_NOMEM;
! 55300: goto balance_cleanup;
! 55301: }
! 55302: szCell = (u16*)&apCell[nMaxCells];
! 55303: aSpace1 = (u8*)&szCell[nMaxCells];
! 55304: assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
! 55305:
! 55306: /*
! 55307: ** Load pointers to all cells on sibling pages and the divider cells
! 55308: ** into the local apCell[] array. Make copies of the divider cells
! 55309: ** into space obtained from aSpace1[] and remove the the divider Cells
! 55310: ** from pParent.
! 55311: **
! 55312: ** If the siblings are on leaf pages, then the child pointers of the
! 55313: ** divider cells are stripped from the cells before they are copied
! 55314: ** into aSpace1[]. In this way, all cells in apCell[] are without
! 55315: ** child pointers. If siblings are not leaves, then all cell in
! 55316: ** apCell[] include child pointers. Either way, all cells in apCell[]
! 55317: ** are alike.
! 55318: **
! 55319: ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
! 55320: ** leafData: 1 if pPage holds key+data and pParent holds only keys.
! 55321: */
! 55322: leafCorrection = apOld[0]->leaf*4;
! 55323: leafData = apOld[0]->hasData;
! 55324: for(i=0; i<nOld; i++){
! 55325: int limit;
! 55326:
! 55327: /* Before doing anything else, take a copy of the i'th original sibling
! 55328: ** The rest of this function will use data from the copies rather
! 55329: ** that the original pages since the original pages will be in the
! 55330: ** process of being overwritten. */
! 55331: MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
! 55332: memcpy(pOld, apOld[i], sizeof(MemPage));
! 55333: pOld->aData = (void*)&pOld[1];
! 55334: memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
! 55335:
! 55336: limit = pOld->nCell+pOld->nOverflow;
! 55337: if( pOld->nOverflow>0 ){
! 55338: for(j=0; j<limit; j++){
! 55339: assert( nCell<nMaxCells );
! 55340: apCell[nCell] = findOverflowCell(pOld, j);
! 55341: szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
! 55342: nCell++;
! 55343: }
! 55344: }else{
! 55345: u8 *aData = pOld->aData;
! 55346: u16 maskPage = pOld->maskPage;
! 55347: u16 cellOffset = pOld->cellOffset;
! 55348: for(j=0; j<limit; j++){
! 55349: assert( nCell<nMaxCells );
! 55350: apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
! 55351: szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
! 55352: nCell++;
! 55353: }
! 55354: }
! 55355: if( i<nOld-1 && !leafData){
! 55356: u16 sz = (u16)szNew[i];
! 55357: u8 *pTemp;
! 55358: assert( nCell<nMaxCells );
! 55359: szCell[nCell] = sz;
! 55360: pTemp = &aSpace1[iSpace1];
! 55361: iSpace1 += sz;
! 55362: assert( sz<=pBt->maxLocal+23 );
! 55363: assert( iSpace1 <= (int)pBt->pageSize );
! 55364: memcpy(pTemp, apDiv[i], sz);
! 55365: apCell[nCell] = pTemp+leafCorrection;
! 55366: assert( leafCorrection==0 || leafCorrection==4 );
! 55367: szCell[nCell] = szCell[nCell] - leafCorrection;
! 55368: if( !pOld->leaf ){
! 55369: assert( leafCorrection==0 );
! 55370: assert( pOld->hdrOffset==0 );
! 55371: /* The right pointer of the child page pOld becomes the left
! 55372: ** pointer of the divider cell */
! 55373: memcpy(apCell[nCell], &pOld->aData[8], 4);
! 55374: }else{
! 55375: assert( leafCorrection==4 );
! 55376: if( szCell[nCell]<4 ){
! 55377: /* Do not allow any cells smaller than 4 bytes. */
! 55378: szCell[nCell] = 4;
! 55379: }
! 55380: }
! 55381: nCell++;
! 55382: }
! 55383: }
! 55384:
! 55385: /*
! 55386: ** Figure out the number of pages needed to hold all nCell cells.
! 55387: ** Store this number in "k". Also compute szNew[] which is the total
! 55388: ** size of all cells on the i-th page and cntNew[] which is the index
! 55389: ** in apCell[] of the cell that divides page i from page i+1.
! 55390: ** cntNew[k] should equal nCell.
! 55391: **
! 55392: ** Values computed by this block:
! 55393: **
! 55394: ** k: The total number of sibling pages
! 55395: ** szNew[i]: Spaced used on the i-th sibling page.
! 55396: ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
! 55397: ** the right of the i-th sibling page.
! 55398: ** usableSpace: Number of bytes of space available on each sibling.
! 55399: **
! 55400: */
! 55401: usableSpace = pBt->usableSize - 12 + leafCorrection;
! 55402: for(subtotal=k=i=0; i<nCell; i++){
! 55403: assert( i<nMaxCells );
! 55404: subtotal += szCell[i] + 2;
! 55405: if( subtotal > usableSpace ){
! 55406: szNew[k] = subtotal - szCell[i];
! 55407: cntNew[k] = i;
! 55408: if( leafData ){ i--; }
! 55409: subtotal = 0;
! 55410: k++;
! 55411: if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
! 55412: }
! 55413: }
! 55414: szNew[k] = subtotal;
! 55415: cntNew[k] = nCell;
! 55416: k++;
! 55417:
! 55418: /*
! 55419: ** The packing computed by the previous block is biased toward the siblings
! 55420: ** on the left side. The left siblings are always nearly full, while the
! 55421: ** right-most sibling might be nearly empty. This block of code attempts
! 55422: ** to adjust the packing of siblings to get a better balance.
! 55423: **
! 55424: ** This adjustment is more than an optimization. The packing above might
! 55425: ** be so out of balance as to be illegal. For example, the right-most
! 55426: ** sibling might be completely empty. This adjustment is not optional.
! 55427: */
! 55428: for(i=k-1; i>0; i--){
! 55429: int szRight = szNew[i]; /* Size of sibling on the right */
! 55430: int szLeft = szNew[i-1]; /* Size of sibling on the left */
! 55431: int r; /* Index of right-most cell in left sibling */
! 55432: int d; /* Index of first cell to the left of right sibling */
! 55433:
! 55434: r = cntNew[i-1] - 1;
! 55435: d = r + 1 - leafData;
! 55436: assert( d<nMaxCells );
! 55437: assert( r<nMaxCells );
! 55438: while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
! 55439: szRight += szCell[d] + 2;
! 55440: szLeft -= szCell[r] + 2;
! 55441: cntNew[i-1]--;
! 55442: r = cntNew[i-1] - 1;
! 55443: d = r + 1 - leafData;
! 55444: }
! 55445: szNew[i] = szRight;
! 55446: szNew[i-1] = szLeft;
! 55447: }
! 55448:
! 55449: /* Either we found one or more cells (cntnew[0])>0) or pPage is
! 55450: ** a virtual root page. A virtual root page is when the real root
! 55451: ** page is page 1 and we are the only child of that page.
! 55452: **
! 55453: ** UPDATE: The assert() below is not necessarily true if the database
! 55454: ** file is corrupt. The corruption will be detected and reported later
! 55455: ** in this procedure so there is no need to act upon it now.
! 55456: */
! 55457: #if 0
! 55458: assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
! 55459: #endif
! 55460:
! 55461: TRACE(("BALANCE: old: %d %d %d ",
! 55462: apOld[0]->pgno,
! 55463: nOld>=2 ? apOld[1]->pgno : 0,
! 55464: nOld>=3 ? apOld[2]->pgno : 0
! 55465: ));
! 55466:
! 55467: /*
! 55468: ** Allocate k new pages. Reuse old pages where possible.
! 55469: */
! 55470: if( apOld[0]->pgno<=1 ){
! 55471: rc = SQLITE_CORRUPT_BKPT;
! 55472: goto balance_cleanup;
! 55473: }
! 55474: pageFlags = apOld[0]->aData[0];
! 55475: for(i=0; i<k; i++){
! 55476: MemPage *pNew;
! 55477: if( i<nOld ){
! 55478: pNew = apNew[i] = apOld[i];
! 55479: apOld[i] = 0;
! 55480: rc = sqlite3PagerWrite(pNew->pDbPage);
! 55481: nNew++;
! 55482: if( rc ) goto balance_cleanup;
! 55483: }else{
! 55484: assert( i>0 );
! 55485: rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0);
! 55486: if( rc ) goto balance_cleanup;
! 55487: apNew[i] = pNew;
! 55488: nNew++;
! 55489:
! 55490: /* Set the pointer-map entry for the new sibling page. */
! 55491: if( ISAUTOVACUUM ){
! 55492: ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
! 55493: if( rc!=SQLITE_OK ){
! 55494: goto balance_cleanup;
! 55495: }
! 55496: }
! 55497: }
! 55498: }
! 55499:
! 55500: /* Free any old pages that were not reused as new pages.
! 55501: */
! 55502: while( i<nOld ){
! 55503: freePage(apOld[i], &rc);
! 55504: if( rc ) goto balance_cleanup;
! 55505: releasePage(apOld[i]);
! 55506: apOld[i] = 0;
! 55507: i++;
! 55508: }
! 55509:
! 55510: /*
! 55511: ** Put the new pages in accending order. This helps to
! 55512: ** keep entries in the disk file in order so that a scan
! 55513: ** of the table is a linear scan through the file. That
! 55514: ** in turn helps the operating system to deliver pages
! 55515: ** from the disk more rapidly.
! 55516: **
! 55517: ** An O(n^2) insertion sort algorithm is used, but since
! 55518: ** n is never more than NB (a small constant), that should
! 55519: ** not be a problem.
! 55520: **
! 55521: ** When NB==3, this one optimization makes the database
! 55522: ** about 25% faster for large insertions and deletions.
! 55523: */
! 55524: for(i=0; i<k-1; i++){
! 55525: int minV = apNew[i]->pgno;
! 55526: int minI = i;
! 55527: for(j=i+1; j<k; j++){
! 55528: if( apNew[j]->pgno<(unsigned)minV ){
! 55529: minI = j;
! 55530: minV = apNew[j]->pgno;
! 55531: }
! 55532: }
! 55533: if( minI>i ){
! 55534: MemPage *pT;
! 55535: pT = apNew[i];
! 55536: apNew[i] = apNew[minI];
! 55537: apNew[minI] = pT;
! 55538: }
! 55539: }
! 55540: TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
! 55541: apNew[0]->pgno, szNew[0],
! 55542: nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
! 55543: nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
! 55544: nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
! 55545: nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
! 55546:
! 55547: assert( sqlite3PagerIswriteable(pParent->pDbPage) );
! 55548: put4byte(pRight, apNew[nNew-1]->pgno);
! 55549:
! 55550: /*
! 55551: ** Evenly distribute the data in apCell[] across the new pages.
! 55552: ** Insert divider cells into pParent as necessary.
! 55553: */
! 55554: j = 0;
! 55555: for(i=0; i<nNew; i++){
! 55556: /* Assemble the new sibling page. */
! 55557: MemPage *pNew = apNew[i];
! 55558: assert( j<nMaxCells );
! 55559: zeroPage(pNew, pageFlags);
! 55560: assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
! 55561: assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
! 55562: assert( pNew->nOverflow==0 );
! 55563:
! 55564: j = cntNew[i];
! 55565:
! 55566: /* If the sibling page assembled above was not the right-most sibling,
! 55567: ** insert a divider cell into the parent page.
! 55568: */
! 55569: assert( i<nNew-1 || j==nCell );
! 55570: if( j<nCell ){
! 55571: u8 *pCell;
! 55572: u8 *pTemp;
! 55573: int sz;
! 55574:
! 55575: assert( j<nMaxCells );
! 55576: pCell = apCell[j];
! 55577: sz = szCell[j] + leafCorrection;
! 55578: pTemp = &aOvflSpace[iOvflSpace];
! 55579: if( !pNew->leaf ){
! 55580: memcpy(&pNew->aData[8], pCell, 4);
! 55581: }else if( leafData ){
! 55582: /* If the tree is a leaf-data tree, and the siblings are leaves,
! 55583: ** then there is no divider cell in apCell[]. Instead, the divider
! 55584: ** cell consists of the integer key for the right-most cell of
! 55585: ** the sibling-page assembled above only.
! 55586: */
! 55587: CellInfo info;
! 55588: j--;
! 55589: btreeParseCellPtr(pNew, apCell[j], &info);
! 55590: pCell = pTemp;
! 55591: sz = 4 + putVarint(&pCell[4], info.nKey);
! 55592: pTemp = 0;
! 55593: }else{
! 55594: pCell -= 4;
! 55595: /* Obscure case for non-leaf-data trees: If the cell at pCell was
! 55596: ** previously stored on a leaf node, and its reported size was 4
! 55597: ** bytes, then it may actually be smaller than this
! 55598: ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
! 55599: ** any cell). But it is important to pass the correct size to
! 55600: ** insertCell(), so reparse the cell now.
! 55601: **
! 55602: ** Note that this can never happen in an SQLite data file, as all
! 55603: ** cells are at least 4 bytes. It only happens in b-trees used
! 55604: ** to evaluate "IN (SELECT ...)" and similar clauses.
! 55605: */
! 55606: if( szCell[j]==4 ){
! 55607: assert(leafCorrection==4);
! 55608: sz = cellSizePtr(pParent, pCell);
! 55609: }
! 55610: }
! 55611: iOvflSpace += sz;
! 55612: assert( sz<=pBt->maxLocal+23 );
! 55613: assert( iOvflSpace <= (int)pBt->pageSize );
! 55614: insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
! 55615: if( rc!=SQLITE_OK ) goto balance_cleanup;
! 55616: assert( sqlite3PagerIswriteable(pParent->pDbPage) );
! 55617:
! 55618: j++;
! 55619: nxDiv++;
! 55620: }
! 55621: }
! 55622: assert( j==nCell );
! 55623: assert( nOld>0 );
! 55624: assert( nNew>0 );
! 55625: if( (pageFlags & PTF_LEAF)==0 ){
! 55626: u8 *zChild = &apCopy[nOld-1]->aData[8];
! 55627: memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
! 55628: }
! 55629:
! 55630: if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
! 55631: /* The root page of the b-tree now contains no cells. The only sibling
! 55632: ** page is the right-child of the parent. Copy the contents of the
! 55633: ** child page into the parent, decreasing the overall height of the
! 55634: ** b-tree structure by one. This is described as the "balance-shallower"
! 55635: ** sub-algorithm in some documentation.
! 55636: **
! 55637: ** If this is an auto-vacuum database, the call to copyNodeContent()
! 55638: ** sets all pointer-map entries corresponding to database image pages
! 55639: ** for which the pointer is stored within the content being copied.
! 55640: **
! 55641: ** The second assert below verifies that the child page is defragmented
! 55642: ** (it must be, as it was just reconstructed using assemblePage()). This
! 55643: ** is important if the parent page happens to be page 1 of the database
! 55644: ** image. */
! 55645: assert( nNew==1 );
! 55646: assert( apNew[0]->nFree ==
! 55647: (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
! 55648: );
! 55649: copyNodeContent(apNew[0], pParent, &rc);
! 55650: freePage(apNew[0], &rc);
! 55651: }else if( ISAUTOVACUUM ){
! 55652: /* Fix the pointer-map entries for all the cells that were shifted around.
! 55653: ** There are several different types of pointer-map entries that need to
! 55654: ** be dealt with by this routine. Some of these have been set already, but
! 55655: ** many have not. The following is a summary:
! 55656: **
! 55657: ** 1) The entries associated with new sibling pages that were not
! 55658: ** siblings when this function was called. These have already
! 55659: ** been set. We don't need to worry about old siblings that were
! 55660: ** moved to the free-list - the freePage() code has taken care
! 55661: ** of those.
! 55662: **
! 55663: ** 2) The pointer-map entries associated with the first overflow
! 55664: ** page in any overflow chains used by new divider cells. These
! 55665: ** have also already been taken care of by the insertCell() code.
! 55666: **
! 55667: ** 3) If the sibling pages are not leaves, then the child pages of
! 55668: ** cells stored on the sibling pages may need to be updated.
! 55669: **
! 55670: ** 4) If the sibling pages are not internal intkey nodes, then any
! 55671: ** overflow pages used by these cells may need to be updated
! 55672: ** (internal intkey nodes never contain pointers to overflow pages).
! 55673: **
! 55674: ** 5) If the sibling pages are not leaves, then the pointer-map
! 55675: ** entries for the right-child pages of each sibling may need
! 55676: ** to be updated.
! 55677: **
! 55678: ** Cases 1 and 2 are dealt with above by other code. The next
! 55679: ** block deals with cases 3 and 4 and the one after that, case 5. Since
! 55680: ** setting a pointer map entry is a relatively expensive operation, this
! 55681: ** code only sets pointer map entries for child or overflow pages that have
! 55682: ** actually moved between pages. */
! 55683: MemPage *pNew = apNew[0];
! 55684: MemPage *pOld = apCopy[0];
! 55685: int nOverflow = pOld->nOverflow;
! 55686: int iNextOld = pOld->nCell + nOverflow;
! 55687: int iOverflow = (nOverflow ? pOld->aOvfl[0].idx : -1);
! 55688: j = 0; /* Current 'old' sibling page */
! 55689: k = 0; /* Current 'new' sibling page */
! 55690: for(i=0; i<nCell; i++){
! 55691: int isDivider = 0;
! 55692: while( i==iNextOld ){
! 55693: /* Cell i is the cell immediately following the last cell on old
! 55694: ** sibling page j. If the siblings are not leaf pages of an
! 55695: ** intkey b-tree, then cell i was a divider cell. */
! 55696: assert( j+1 < ArraySize(apCopy) );
! 55697: pOld = apCopy[++j];
! 55698: iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
! 55699: if( pOld->nOverflow ){
! 55700: nOverflow = pOld->nOverflow;
! 55701: iOverflow = i + !leafData + pOld->aOvfl[0].idx;
! 55702: }
! 55703: isDivider = !leafData;
! 55704: }
! 55705:
! 55706: assert(nOverflow>0 || iOverflow<i );
! 55707: assert(nOverflow<2 || pOld->aOvfl[0].idx==pOld->aOvfl[1].idx-1);
! 55708: assert(nOverflow<3 || pOld->aOvfl[1].idx==pOld->aOvfl[2].idx-1);
! 55709: if( i==iOverflow ){
! 55710: isDivider = 1;
! 55711: if( (--nOverflow)>0 ){
! 55712: iOverflow++;
! 55713: }
! 55714: }
! 55715:
! 55716: if( i==cntNew[k] ){
! 55717: /* Cell i is the cell immediately following the last cell on new
! 55718: ** sibling page k. If the siblings are not leaf pages of an
! 55719: ** intkey b-tree, then cell i is a divider cell. */
! 55720: pNew = apNew[++k];
! 55721: if( !leafData ) continue;
! 55722: }
! 55723: assert( j<nOld );
! 55724: assert( k<nNew );
! 55725:
! 55726: /* If the cell was originally divider cell (and is not now) or
! 55727: ** an overflow cell, or if the cell was located on a different sibling
! 55728: ** page before the balancing, then the pointer map entries associated
! 55729: ** with any child or overflow pages need to be updated. */
! 55730: if( isDivider || pOld->pgno!=pNew->pgno ){
! 55731: if( !leafCorrection ){
! 55732: ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
! 55733: }
! 55734: if( szCell[i]>pNew->minLocal ){
! 55735: ptrmapPutOvflPtr(pNew, apCell[i], &rc);
! 55736: }
! 55737: }
! 55738: }
! 55739:
! 55740: if( !leafCorrection ){
! 55741: for(i=0; i<nNew; i++){
! 55742: u32 key = get4byte(&apNew[i]->aData[8]);
! 55743: ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
! 55744: }
! 55745: }
! 55746:
! 55747: #if 0
! 55748: /* The ptrmapCheckPages() contains assert() statements that verify that
! 55749: ** all pointer map pages are set correctly. This is helpful while
! 55750: ** debugging. This is usually disabled because a corrupt database may
! 55751: ** cause an assert() statement to fail. */
! 55752: ptrmapCheckPages(apNew, nNew);
! 55753: ptrmapCheckPages(&pParent, 1);
! 55754: #endif
! 55755: }
! 55756:
! 55757: assert( pParent->isInit );
! 55758: TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
! 55759: nOld, nNew, nCell));
! 55760:
! 55761: /*
! 55762: ** Cleanup before returning.
! 55763: */
! 55764: balance_cleanup:
! 55765: sqlite3ScratchFree(apCell);
! 55766: for(i=0; i<nOld; i++){
! 55767: releasePage(apOld[i]);
! 55768: }
! 55769: for(i=0; i<nNew; i++){
! 55770: releasePage(apNew[i]);
! 55771: }
! 55772:
! 55773: return rc;
! 55774: }
! 55775:
! 55776:
! 55777: /*
! 55778: ** This function is called when the root page of a b-tree structure is
! 55779: ** overfull (has one or more overflow pages).
! 55780: **
! 55781: ** A new child page is allocated and the contents of the current root
! 55782: ** page, including overflow cells, are copied into the child. The root
! 55783: ** page is then overwritten to make it an empty page with the right-child
! 55784: ** pointer pointing to the new page.
! 55785: **
! 55786: ** Before returning, all pointer-map entries corresponding to pages
! 55787: ** that the new child-page now contains pointers to are updated. The
! 55788: ** entry corresponding to the new right-child pointer of the root
! 55789: ** page is also updated.
! 55790: **
! 55791: ** If successful, *ppChild is set to contain a reference to the child
! 55792: ** page and SQLITE_OK is returned. In this case the caller is required
! 55793: ** to call releasePage() on *ppChild exactly once. If an error occurs,
! 55794: ** an error code is returned and *ppChild is set to 0.
! 55795: */
! 55796: static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
! 55797: int rc; /* Return value from subprocedures */
! 55798: MemPage *pChild = 0; /* Pointer to a new child page */
! 55799: Pgno pgnoChild = 0; /* Page number of the new child page */
! 55800: BtShared *pBt = pRoot->pBt; /* The BTree */
! 55801:
! 55802: assert( pRoot->nOverflow>0 );
! 55803: assert( sqlite3_mutex_held(pBt->mutex) );
! 55804:
! 55805: /* Make pRoot, the root page of the b-tree, writable. Allocate a new
! 55806: ** page that will become the new right-child of pPage. Copy the contents
! 55807: ** of the node stored on pRoot into the new child page.
! 55808: */
! 55809: rc = sqlite3PagerWrite(pRoot->pDbPage);
! 55810: if( rc==SQLITE_OK ){
! 55811: rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
! 55812: copyNodeContent(pRoot, pChild, &rc);
! 55813: if( ISAUTOVACUUM ){
! 55814: ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
! 55815: }
! 55816: }
! 55817: if( rc ){
! 55818: *ppChild = 0;
! 55819: releasePage(pChild);
! 55820: return rc;
! 55821: }
! 55822: assert( sqlite3PagerIswriteable(pChild->pDbPage) );
! 55823: assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
! 55824: assert( pChild->nCell==pRoot->nCell );
! 55825:
! 55826: TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
! 55827:
! 55828: /* Copy the overflow cells from pRoot to pChild */
! 55829: memcpy(pChild->aOvfl, pRoot->aOvfl, pRoot->nOverflow*sizeof(pRoot->aOvfl[0]));
! 55830: pChild->nOverflow = pRoot->nOverflow;
! 55831:
! 55832: /* Zero the contents of pRoot. Then install pChild as the right-child. */
! 55833: zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
! 55834: put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
! 55835:
! 55836: *ppChild = pChild;
! 55837: return SQLITE_OK;
! 55838: }
! 55839:
! 55840: /*
! 55841: ** The page that pCur currently points to has just been modified in
! 55842: ** some way. This function figures out if this modification means the
! 55843: ** tree needs to be balanced, and if so calls the appropriate balancing
! 55844: ** routine. Balancing routines are:
! 55845: **
! 55846: ** balance_quick()
! 55847: ** balance_deeper()
! 55848: ** balance_nonroot()
! 55849: */
! 55850: static int balance(BtCursor *pCur){
! 55851: int rc = SQLITE_OK;
! 55852: const int nMin = pCur->pBt->usableSize * 2 / 3;
! 55853: u8 aBalanceQuickSpace[13];
! 55854: u8 *pFree = 0;
! 55855:
! 55856: TESTONLY( int balance_quick_called = 0 );
! 55857: TESTONLY( int balance_deeper_called = 0 );
! 55858:
! 55859: do {
! 55860: int iPage = pCur->iPage;
! 55861: MemPage *pPage = pCur->apPage[iPage];
! 55862:
! 55863: if( iPage==0 ){
! 55864: if( pPage->nOverflow ){
! 55865: /* The root page of the b-tree is overfull. In this case call the
! 55866: ** balance_deeper() function to create a new child for the root-page
! 55867: ** and copy the current contents of the root-page to it. The
! 55868: ** next iteration of the do-loop will balance the child page.
! 55869: */
! 55870: assert( (balance_deeper_called++)==0 );
! 55871: rc = balance_deeper(pPage, &pCur->apPage[1]);
! 55872: if( rc==SQLITE_OK ){
! 55873: pCur->iPage = 1;
! 55874: pCur->aiIdx[0] = 0;
! 55875: pCur->aiIdx[1] = 0;
! 55876: assert( pCur->apPage[1]->nOverflow );
! 55877: }
! 55878: }else{
! 55879: break;
! 55880: }
! 55881: }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
! 55882: break;
! 55883: }else{
! 55884: MemPage * const pParent = pCur->apPage[iPage-1];
! 55885: int const iIdx = pCur->aiIdx[iPage-1];
! 55886:
! 55887: rc = sqlite3PagerWrite(pParent->pDbPage);
! 55888: if( rc==SQLITE_OK ){
! 55889: #ifndef SQLITE_OMIT_QUICKBALANCE
! 55890: if( pPage->hasData
! 55891: && pPage->nOverflow==1
! 55892: && pPage->aOvfl[0].idx==pPage->nCell
! 55893: && pParent->pgno!=1
! 55894: && pParent->nCell==iIdx
! 55895: ){
! 55896: /* Call balance_quick() to create a new sibling of pPage on which
! 55897: ** to store the overflow cell. balance_quick() inserts a new cell
! 55898: ** into pParent, which may cause pParent overflow. If this
! 55899: ** happens, the next interation of the do-loop will balance pParent
! 55900: ** use either balance_nonroot() or balance_deeper(). Until this
! 55901: ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
! 55902: ** buffer.
! 55903: **
! 55904: ** The purpose of the following assert() is to check that only a
! 55905: ** single call to balance_quick() is made for each call to this
! 55906: ** function. If this were not verified, a subtle bug involving reuse
! 55907: ** of the aBalanceQuickSpace[] might sneak in.
! 55908: */
! 55909: assert( (balance_quick_called++)==0 );
! 55910: rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
! 55911: }else
! 55912: #endif
! 55913: {
! 55914: /* In this case, call balance_nonroot() to redistribute cells
! 55915: ** between pPage and up to 2 of its sibling pages. This involves
! 55916: ** modifying the contents of pParent, which may cause pParent to
! 55917: ** become overfull or underfull. The next iteration of the do-loop
! 55918: ** will balance the parent page to correct this.
! 55919: **
! 55920: ** If the parent page becomes overfull, the overflow cell or cells
! 55921: ** are stored in the pSpace buffer allocated immediately below.
! 55922: ** A subsequent iteration of the do-loop will deal with this by
! 55923: ** calling balance_nonroot() (balance_deeper() may be called first,
! 55924: ** but it doesn't deal with overflow cells - just moves them to a
! 55925: ** different page). Once this subsequent call to balance_nonroot()
! 55926: ** has completed, it is safe to release the pSpace buffer used by
! 55927: ** the previous call, as the overflow cell data will have been
! 55928: ** copied either into the body of a database page or into the new
! 55929: ** pSpace buffer passed to the latter call to balance_nonroot().
! 55930: */
! 55931: u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
! 55932: rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1);
! 55933: if( pFree ){
! 55934: /* If pFree is not NULL, it points to the pSpace buffer used
! 55935: ** by a previous call to balance_nonroot(). Its contents are
! 55936: ** now stored either on real database pages or within the
! 55937: ** new pSpace buffer, so it may be safely freed here. */
! 55938: sqlite3PageFree(pFree);
! 55939: }
! 55940:
! 55941: /* The pSpace buffer will be freed after the next call to
! 55942: ** balance_nonroot(), or just before this function returns, whichever
! 55943: ** comes first. */
! 55944: pFree = pSpace;
! 55945: }
! 55946: }
! 55947:
! 55948: pPage->nOverflow = 0;
! 55949:
! 55950: /* The next iteration of the do-loop balances the parent page. */
! 55951: releasePage(pPage);
! 55952: pCur->iPage--;
! 55953: }
! 55954: }while( rc==SQLITE_OK );
! 55955:
! 55956: if( pFree ){
! 55957: sqlite3PageFree(pFree);
! 55958: }
! 55959: return rc;
! 55960: }
! 55961:
! 55962:
! 55963: /*
! 55964: ** Insert a new record into the BTree. The key is given by (pKey,nKey)
! 55965: ** and the data is given by (pData,nData). The cursor is used only to
! 55966: ** define what table the record should be inserted into. The cursor
! 55967: ** is left pointing at a random location.
! 55968: **
! 55969: ** For an INTKEY table, only the nKey value of the key is used. pKey is
! 55970: ** ignored. For a ZERODATA table, the pData and nData are both ignored.
! 55971: **
! 55972: ** If the seekResult parameter is non-zero, then a successful call to
! 55973: ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
! 55974: ** been performed. seekResult is the search result returned (a negative
! 55975: ** number if pCur points at an entry that is smaller than (pKey, nKey), or
! 55976: ** a positive value if pCur points at an etry that is larger than
! 55977: ** (pKey, nKey)).
! 55978: **
! 55979: ** If the seekResult parameter is non-zero, then the caller guarantees that
! 55980: ** cursor pCur is pointing at the existing copy of a row that is to be
! 55981: ** overwritten. If the seekResult parameter is 0, then cursor pCur may
! 55982: ** point to any entry or to no entry at all and so this function has to seek
! 55983: ** the cursor before the new key can be inserted.
! 55984: */
! 55985: SQLITE_PRIVATE int sqlite3BtreeInsert(
! 55986: BtCursor *pCur, /* Insert data into the table of this cursor */
! 55987: const void *pKey, i64 nKey, /* The key of the new record */
! 55988: const void *pData, int nData, /* The data of the new record */
! 55989: int nZero, /* Number of extra 0 bytes to append to data */
! 55990: int appendBias, /* True if this is likely an append */
! 55991: int seekResult /* Result of prior MovetoUnpacked() call */
! 55992: ){
! 55993: int rc;
! 55994: int loc = seekResult; /* -1: before desired location +1: after */
! 55995: int szNew = 0;
! 55996: int idx;
! 55997: MemPage *pPage;
! 55998: Btree *p = pCur->pBtree;
! 55999: BtShared *pBt = p->pBt;
! 56000: unsigned char *oldCell;
! 56001: unsigned char *newCell = 0;
! 56002:
! 56003: if( pCur->eState==CURSOR_FAULT ){
! 56004: assert( pCur->skipNext!=SQLITE_OK );
! 56005: return pCur->skipNext;
! 56006: }
! 56007:
! 56008: assert( cursorHoldsMutex(pCur) );
! 56009: assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
! 56010: && (pBt->btsFlags & BTS_READ_ONLY)==0 );
! 56011: assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
! 56012:
! 56013: /* Assert that the caller has been consistent. If this cursor was opened
! 56014: ** expecting an index b-tree, then the caller should be inserting blob
! 56015: ** keys with no associated data. If the cursor was opened expecting an
! 56016: ** intkey table, the caller should be inserting integer keys with a
! 56017: ** blob of associated data. */
! 56018: assert( (pKey==0)==(pCur->pKeyInfo==0) );
! 56019:
! 56020: /* If this is an insert into a table b-tree, invalidate any incrblob
! 56021: ** cursors open on the row being replaced (assuming this is a replace
! 56022: ** operation - if it is not, the following is a no-op). */
! 56023: if( pCur->pKeyInfo==0 ){
! 56024: invalidateIncrblobCursors(p, nKey, 0);
! 56025: }
! 56026:
! 56027: /* Save the positions of any other cursors open on this table.
! 56028: **
! 56029: ** In some cases, the call to btreeMoveto() below is a no-op. For
! 56030: ** example, when inserting data into a table with auto-generated integer
! 56031: ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
! 56032: ** integer key to use. It then calls this function to actually insert the
! 56033: ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
! 56034: ** that the cursor is already where it needs to be and returns without
! 56035: ** doing any work. To avoid thwarting these optimizations, it is important
! 56036: ** not to clear the cursor here.
! 56037: */
! 56038: rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
! 56039: if( rc ) return rc;
! 56040: if( !loc ){
! 56041: rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
! 56042: if( rc ) return rc;
! 56043: }
! 56044: assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
! 56045:
! 56046: pPage = pCur->apPage[pCur->iPage];
! 56047: assert( pPage->intKey || nKey>=0 );
! 56048: assert( pPage->leaf || !pPage->intKey );
! 56049:
! 56050: TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
! 56051: pCur->pgnoRoot, nKey, nData, pPage->pgno,
! 56052: loc==0 ? "overwrite" : "new entry"));
! 56053: assert( pPage->isInit );
! 56054: allocateTempSpace(pBt);
! 56055: newCell = pBt->pTmpSpace;
! 56056: if( newCell==0 ) return SQLITE_NOMEM;
! 56057: rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
! 56058: if( rc ) goto end_insert;
! 56059: assert( szNew==cellSizePtr(pPage, newCell) );
! 56060: assert( szNew <= MX_CELL_SIZE(pBt) );
! 56061: idx = pCur->aiIdx[pCur->iPage];
! 56062: if( loc==0 ){
! 56063: u16 szOld;
! 56064: assert( idx<pPage->nCell );
! 56065: rc = sqlite3PagerWrite(pPage->pDbPage);
! 56066: if( rc ){
! 56067: goto end_insert;
! 56068: }
! 56069: oldCell = findCell(pPage, idx);
! 56070: if( !pPage->leaf ){
! 56071: memcpy(newCell, oldCell, 4);
! 56072: }
! 56073: szOld = cellSizePtr(pPage, oldCell);
! 56074: rc = clearCell(pPage, oldCell);
! 56075: dropCell(pPage, idx, szOld, &rc);
! 56076: if( rc ) goto end_insert;
! 56077: }else if( loc<0 && pPage->nCell>0 ){
! 56078: assert( pPage->leaf );
! 56079: idx = ++pCur->aiIdx[pCur->iPage];
! 56080: }else{
! 56081: assert( pPage->leaf );
! 56082: }
! 56083: insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
! 56084: assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
! 56085:
! 56086: /* If no error has occured and pPage has an overflow cell, call balance()
! 56087: ** to redistribute the cells within the tree. Since balance() may move
! 56088: ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
! 56089: ** variables.
! 56090: **
! 56091: ** Previous versions of SQLite called moveToRoot() to move the cursor
! 56092: ** back to the root page as balance() used to invalidate the contents
! 56093: ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
! 56094: ** set the cursor state to "invalid". This makes common insert operations
! 56095: ** slightly faster.
! 56096: **
! 56097: ** There is a subtle but important optimization here too. When inserting
! 56098: ** multiple records into an intkey b-tree using a single cursor (as can
! 56099: ** happen while processing an "INSERT INTO ... SELECT" statement), it
! 56100: ** is advantageous to leave the cursor pointing to the last entry in
! 56101: ** the b-tree if possible. If the cursor is left pointing to the last
! 56102: ** entry in the table, and the next row inserted has an integer key
! 56103: ** larger than the largest existing key, it is possible to insert the
! 56104: ** row without seeking the cursor. This can be a big performance boost.
! 56105: */
! 56106: pCur->info.nSize = 0;
! 56107: pCur->validNKey = 0;
! 56108: if( rc==SQLITE_OK && pPage->nOverflow ){
! 56109: rc = balance(pCur);
! 56110:
! 56111: /* Must make sure nOverflow is reset to zero even if the balance()
! 56112: ** fails. Internal data structure corruption will result otherwise.
! 56113: ** Also, set the cursor state to invalid. This stops saveCursorPosition()
! 56114: ** from trying to save the current position of the cursor. */
! 56115: pCur->apPage[pCur->iPage]->nOverflow = 0;
! 56116: pCur->eState = CURSOR_INVALID;
! 56117: }
! 56118: assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
! 56119:
! 56120: end_insert:
! 56121: return rc;
! 56122: }
! 56123:
! 56124: /*
! 56125: ** Delete the entry that the cursor is pointing to. The cursor
! 56126: ** is left pointing at a arbitrary location.
! 56127: */
! 56128: SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
! 56129: Btree *p = pCur->pBtree;
! 56130: BtShared *pBt = p->pBt;
! 56131: int rc; /* Return code */
! 56132: MemPage *pPage; /* Page to delete cell from */
! 56133: unsigned char *pCell; /* Pointer to cell to delete */
! 56134: int iCellIdx; /* Index of cell to delete */
! 56135: int iCellDepth; /* Depth of node containing pCell */
! 56136:
! 56137: assert( cursorHoldsMutex(pCur) );
! 56138: assert( pBt->inTransaction==TRANS_WRITE );
! 56139: assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
! 56140: assert( pCur->wrFlag );
! 56141: assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
! 56142: assert( !hasReadConflicts(p, pCur->pgnoRoot) );
! 56143:
! 56144: if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
! 56145: || NEVER(pCur->eState!=CURSOR_VALID)
! 56146: ){
! 56147: return SQLITE_ERROR; /* Something has gone awry. */
! 56148: }
! 56149:
! 56150: /* If this is a delete operation to remove a row from a table b-tree,
! 56151: ** invalidate any incrblob cursors open on the row being deleted. */
! 56152: if( pCur->pKeyInfo==0 ){
! 56153: invalidateIncrblobCursors(p, pCur->info.nKey, 0);
! 56154: }
! 56155:
! 56156: iCellDepth = pCur->iPage;
! 56157: iCellIdx = pCur->aiIdx[iCellDepth];
! 56158: pPage = pCur->apPage[iCellDepth];
! 56159: pCell = findCell(pPage, iCellIdx);
! 56160:
! 56161: /* If the page containing the entry to delete is not a leaf page, move
! 56162: ** the cursor to the largest entry in the tree that is smaller than
! 56163: ** the entry being deleted. This cell will replace the cell being deleted
! 56164: ** from the internal node. The 'previous' entry is used for this instead
! 56165: ** of the 'next' entry, as the previous entry is always a part of the
! 56166: ** sub-tree headed by the child page of the cell being deleted. This makes
! 56167: ** balancing the tree following the delete operation easier. */
! 56168: if( !pPage->leaf ){
! 56169: int notUsed;
! 56170: rc = sqlite3BtreePrevious(pCur, ¬Used);
! 56171: if( rc ) return rc;
! 56172: }
! 56173:
! 56174: /* Save the positions of any other cursors open on this table before
! 56175: ** making any modifications. Make the page containing the entry to be
! 56176: ** deleted writable. Then free any overflow pages associated with the
! 56177: ** entry and finally remove the cell itself from within the page.
! 56178: */
! 56179: rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
! 56180: if( rc ) return rc;
! 56181: rc = sqlite3PagerWrite(pPage->pDbPage);
! 56182: if( rc ) return rc;
! 56183: rc = clearCell(pPage, pCell);
! 56184: dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
! 56185: if( rc ) return rc;
! 56186:
! 56187: /* If the cell deleted was not located on a leaf page, then the cursor
! 56188: ** is currently pointing to the largest entry in the sub-tree headed
! 56189: ** by the child-page of the cell that was just deleted from an internal
! 56190: ** node. The cell from the leaf node needs to be moved to the internal
! 56191: ** node to replace the deleted cell. */
! 56192: if( !pPage->leaf ){
! 56193: MemPage *pLeaf = pCur->apPage[pCur->iPage];
! 56194: int nCell;
! 56195: Pgno n = pCur->apPage[iCellDepth+1]->pgno;
! 56196: unsigned char *pTmp;
! 56197:
! 56198: pCell = findCell(pLeaf, pLeaf->nCell-1);
! 56199: nCell = cellSizePtr(pLeaf, pCell);
! 56200: assert( MX_CELL_SIZE(pBt) >= nCell );
! 56201:
! 56202: allocateTempSpace(pBt);
! 56203: pTmp = pBt->pTmpSpace;
! 56204:
! 56205: rc = sqlite3PagerWrite(pLeaf->pDbPage);
! 56206: insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
! 56207: dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
! 56208: if( rc ) return rc;
! 56209: }
! 56210:
! 56211: /* Balance the tree. If the entry deleted was located on a leaf page,
! 56212: ** then the cursor still points to that page. In this case the first
! 56213: ** call to balance() repairs the tree, and the if(...) condition is
! 56214: ** never true.
! 56215: **
! 56216: ** Otherwise, if the entry deleted was on an internal node page, then
! 56217: ** pCur is pointing to the leaf page from which a cell was removed to
! 56218: ** replace the cell deleted from the internal node. This is slightly
! 56219: ** tricky as the leaf node may be underfull, and the internal node may
! 56220: ** be either under or overfull. In this case run the balancing algorithm
! 56221: ** on the leaf node first. If the balance proceeds far enough up the
! 56222: ** tree that we can be sure that any problem in the internal node has
! 56223: ** been corrected, so be it. Otherwise, after balancing the leaf node,
! 56224: ** walk the cursor up the tree to the internal node and balance it as
! 56225: ** well. */
! 56226: rc = balance(pCur);
! 56227: if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
! 56228: while( pCur->iPage>iCellDepth ){
! 56229: releasePage(pCur->apPage[pCur->iPage--]);
! 56230: }
! 56231: rc = balance(pCur);
! 56232: }
! 56233:
! 56234: if( rc==SQLITE_OK ){
! 56235: moveToRoot(pCur);
! 56236: }
! 56237: return rc;
! 56238: }
! 56239:
! 56240: /*
! 56241: ** Create a new BTree table. Write into *piTable the page
! 56242: ** number for the root page of the new table.
! 56243: **
! 56244: ** The type of type is determined by the flags parameter. Only the
! 56245: ** following values of flags are currently in use. Other values for
! 56246: ** flags might not work:
! 56247: **
! 56248: ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
! 56249: ** BTREE_ZERODATA Used for SQL indices
! 56250: */
! 56251: static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
! 56252: BtShared *pBt = p->pBt;
! 56253: MemPage *pRoot;
! 56254: Pgno pgnoRoot;
! 56255: int rc;
! 56256: int ptfFlags; /* Page-type flage for the root page of new table */
! 56257:
! 56258: assert( sqlite3BtreeHoldsMutex(p) );
! 56259: assert( pBt->inTransaction==TRANS_WRITE );
! 56260: assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
! 56261:
! 56262: #ifdef SQLITE_OMIT_AUTOVACUUM
! 56263: rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
! 56264: if( rc ){
! 56265: return rc;
! 56266: }
! 56267: #else
! 56268: if( pBt->autoVacuum ){
! 56269: Pgno pgnoMove; /* Move a page here to make room for the root-page */
! 56270: MemPage *pPageMove; /* The page to move to. */
! 56271:
! 56272: /* Creating a new table may probably require moving an existing database
! 56273: ** to make room for the new tables root page. In case this page turns
! 56274: ** out to be an overflow page, delete all overflow page-map caches
! 56275: ** held by open cursors.
! 56276: */
! 56277: invalidateAllOverflowCache(pBt);
! 56278:
! 56279: /* Read the value of meta[3] from the database to determine where the
! 56280: ** root page of the new table should go. meta[3] is the largest root-page
! 56281: ** created so far, so the new root-page is (meta[3]+1).
! 56282: */
! 56283: sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
! 56284: pgnoRoot++;
! 56285:
! 56286: /* The new root-page may not be allocated on a pointer-map page, or the
! 56287: ** PENDING_BYTE page.
! 56288: */
! 56289: while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
! 56290: pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
! 56291: pgnoRoot++;
! 56292: }
! 56293: assert( pgnoRoot>=3 );
! 56294:
! 56295: /* Allocate a page. The page that currently resides at pgnoRoot will
! 56296: ** be moved to the allocated page (unless the allocated page happens
! 56297: ** to reside at pgnoRoot).
! 56298: */
! 56299: rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
! 56300: if( rc!=SQLITE_OK ){
! 56301: return rc;
! 56302: }
! 56303:
! 56304: if( pgnoMove!=pgnoRoot ){
! 56305: /* pgnoRoot is the page that will be used for the root-page of
! 56306: ** the new table (assuming an error did not occur). But we were
! 56307: ** allocated pgnoMove. If required (i.e. if it was not allocated
! 56308: ** by extending the file), the current page at position pgnoMove
! 56309: ** is already journaled.
! 56310: */
! 56311: u8 eType = 0;
! 56312: Pgno iPtrPage = 0;
! 56313:
! 56314: releasePage(pPageMove);
! 56315:
! 56316: /* Move the page currently at pgnoRoot to pgnoMove. */
! 56317: rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
! 56318: if( rc!=SQLITE_OK ){
! 56319: return rc;
! 56320: }
! 56321: rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
! 56322: if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
! 56323: rc = SQLITE_CORRUPT_BKPT;
! 56324: }
! 56325: if( rc!=SQLITE_OK ){
! 56326: releasePage(pRoot);
! 56327: return rc;
! 56328: }
! 56329: assert( eType!=PTRMAP_ROOTPAGE );
! 56330: assert( eType!=PTRMAP_FREEPAGE );
! 56331: rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
! 56332: releasePage(pRoot);
! 56333:
! 56334: /* Obtain the page at pgnoRoot */
! 56335: if( rc!=SQLITE_OK ){
! 56336: return rc;
! 56337: }
! 56338: rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
! 56339: if( rc!=SQLITE_OK ){
! 56340: return rc;
! 56341: }
! 56342: rc = sqlite3PagerWrite(pRoot->pDbPage);
! 56343: if( rc!=SQLITE_OK ){
! 56344: releasePage(pRoot);
! 56345: return rc;
! 56346: }
! 56347: }else{
! 56348: pRoot = pPageMove;
! 56349: }
! 56350:
! 56351: /* Update the pointer-map and meta-data with the new root-page number. */
! 56352: ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
! 56353: if( rc ){
! 56354: releasePage(pRoot);
! 56355: return rc;
! 56356: }
! 56357:
! 56358: /* When the new root page was allocated, page 1 was made writable in
! 56359: ** order either to increase the database filesize, or to decrement the
! 56360: ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
! 56361: */
! 56362: assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
! 56363: rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
! 56364: if( NEVER(rc) ){
! 56365: releasePage(pRoot);
! 56366: return rc;
! 56367: }
! 56368:
! 56369: }else{
! 56370: rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
! 56371: if( rc ) return rc;
! 56372: }
! 56373: #endif
! 56374: assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
! 56375: if( createTabFlags & BTREE_INTKEY ){
! 56376: ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
! 56377: }else{
! 56378: ptfFlags = PTF_ZERODATA | PTF_LEAF;
! 56379: }
! 56380: zeroPage(pRoot, ptfFlags);
! 56381: sqlite3PagerUnref(pRoot->pDbPage);
! 56382: assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
! 56383: *piTable = (int)pgnoRoot;
! 56384: return SQLITE_OK;
! 56385: }
! 56386: SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
! 56387: int rc;
! 56388: sqlite3BtreeEnter(p);
! 56389: rc = btreeCreateTable(p, piTable, flags);
! 56390: sqlite3BtreeLeave(p);
! 56391: return rc;
! 56392: }
! 56393:
! 56394: /*
! 56395: ** Erase the given database page and all its children. Return
! 56396: ** the page to the freelist.
! 56397: */
! 56398: static int clearDatabasePage(
! 56399: BtShared *pBt, /* The BTree that contains the table */
! 56400: Pgno pgno, /* Page number to clear */
! 56401: int freePageFlag, /* Deallocate page if true */
! 56402: int *pnChange /* Add number of Cells freed to this counter */
! 56403: ){
! 56404: MemPage *pPage;
! 56405: int rc;
! 56406: unsigned char *pCell;
! 56407: int i;
! 56408:
! 56409: assert( sqlite3_mutex_held(pBt->mutex) );
! 56410: if( pgno>btreePagecount(pBt) ){
! 56411: return SQLITE_CORRUPT_BKPT;
! 56412: }
! 56413:
! 56414: rc = getAndInitPage(pBt, pgno, &pPage);
! 56415: if( rc ) return rc;
! 56416: for(i=0; i<pPage->nCell; i++){
! 56417: pCell = findCell(pPage, i);
! 56418: if( !pPage->leaf ){
! 56419: rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
! 56420: if( rc ) goto cleardatabasepage_out;
! 56421: }
! 56422: rc = clearCell(pPage, pCell);
! 56423: if( rc ) goto cleardatabasepage_out;
! 56424: }
! 56425: if( !pPage->leaf ){
! 56426: rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
! 56427: if( rc ) goto cleardatabasepage_out;
! 56428: }else if( pnChange ){
! 56429: assert( pPage->intKey );
! 56430: *pnChange += pPage->nCell;
! 56431: }
! 56432: if( freePageFlag ){
! 56433: freePage(pPage, &rc);
! 56434: }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
! 56435: zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
! 56436: }
! 56437:
! 56438: cleardatabasepage_out:
! 56439: releasePage(pPage);
! 56440: return rc;
! 56441: }
! 56442:
! 56443: /*
! 56444: ** Delete all information from a single table in the database. iTable is
! 56445: ** the page number of the root of the table. After this routine returns,
! 56446: ** the root page is empty, but still exists.
! 56447: **
! 56448: ** This routine will fail with SQLITE_LOCKED if there are any open
! 56449: ** read cursors on the table. Open write cursors are moved to the
! 56450: ** root of the table.
! 56451: **
! 56452: ** If pnChange is not NULL, then table iTable must be an intkey table. The
! 56453: ** integer value pointed to by pnChange is incremented by the number of
! 56454: ** entries in the table.
! 56455: */
! 56456: SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
! 56457: int rc;
! 56458: BtShared *pBt = p->pBt;
! 56459: sqlite3BtreeEnter(p);
! 56460: assert( p->inTrans==TRANS_WRITE );
! 56461:
! 56462: /* Invalidate all incrblob cursors open on table iTable (assuming iTable
! 56463: ** is the root of a table b-tree - if it is not, the following call is
! 56464: ** a no-op). */
! 56465: invalidateIncrblobCursors(p, 0, 1);
! 56466:
! 56467: rc = saveAllCursors(pBt, (Pgno)iTable, 0);
! 56468: if( SQLITE_OK==rc ){
! 56469: rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
! 56470: }
! 56471: sqlite3BtreeLeave(p);
! 56472: return rc;
! 56473: }
! 56474:
! 56475: /*
! 56476: ** Erase all information in a table and add the root of the table to
! 56477: ** the freelist. Except, the root of the principle table (the one on
! 56478: ** page 1) is never added to the freelist.
! 56479: **
! 56480: ** This routine will fail with SQLITE_LOCKED if there are any open
! 56481: ** cursors on the table.
! 56482: **
! 56483: ** If AUTOVACUUM is enabled and the page at iTable is not the last
! 56484: ** root page in the database file, then the last root page
! 56485: ** in the database file is moved into the slot formerly occupied by
! 56486: ** iTable and that last slot formerly occupied by the last root page
! 56487: ** is added to the freelist instead of iTable. In this say, all
! 56488: ** root pages are kept at the beginning of the database file, which
! 56489: ** is necessary for AUTOVACUUM to work right. *piMoved is set to the
! 56490: ** page number that used to be the last root page in the file before
! 56491: ** the move. If no page gets moved, *piMoved is set to 0.
! 56492: ** The last root page is recorded in meta[3] and the value of
! 56493: ** meta[3] is updated by this procedure.
! 56494: */
! 56495: static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
! 56496: int rc;
! 56497: MemPage *pPage = 0;
! 56498: BtShared *pBt = p->pBt;
! 56499:
! 56500: assert( sqlite3BtreeHoldsMutex(p) );
! 56501: assert( p->inTrans==TRANS_WRITE );
! 56502:
! 56503: /* It is illegal to drop a table if any cursors are open on the
! 56504: ** database. This is because in auto-vacuum mode the backend may
! 56505: ** need to move another root-page to fill a gap left by the deleted
! 56506: ** root page. If an open cursor was using this page a problem would
! 56507: ** occur.
! 56508: **
! 56509: ** This error is caught long before control reaches this point.
! 56510: */
! 56511: if( NEVER(pBt->pCursor) ){
! 56512: sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
! 56513: return SQLITE_LOCKED_SHAREDCACHE;
! 56514: }
! 56515:
! 56516: rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
! 56517: if( rc ) return rc;
! 56518: rc = sqlite3BtreeClearTable(p, iTable, 0);
! 56519: if( rc ){
! 56520: releasePage(pPage);
! 56521: return rc;
! 56522: }
! 56523:
! 56524: *piMoved = 0;
! 56525:
! 56526: if( iTable>1 ){
! 56527: #ifdef SQLITE_OMIT_AUTOVACUUM
! 56528: freePage(pPage, &rc);
! 56529: releasePage(pPage);
! 56530: #else
! 56531: if( pBt->autoVacuum ){
! 56532: Pgno maxRootPgno;
! 56533: sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
! 56534:
! 56535: if( iTable==maxRootPgno ){
! 56536: /* If the table being dropped is the table with the largest root-page
! 56537: ** number in the database, put the root page on the free list.
! 56538: */
! 56539: freePage(pPage, &rc);
! 56540: releasePage(pPage);
! 56541: if( rc!=SQLITE_OK ){
! 56542: return rc;
! 56543: }
! 56544: }else{
! 56545: /* The table being dropped does not have the largest root-page
! 56546: ** number in the database. So move the page that does into the
! 56547: ** gap left by the deleted root-page.
! 56548: */
! 56549: MemPage *pMove;
! 56550: releasePage(pPage);
! 56551: rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
! 56552: if( rc!=SQLITE_OK ){
! 56553: return rc;
! 56554: }
! 56555: rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
! 56556: releasePage(pMove);
! 56557: if( rc!=SQLITE_OK ){
! 56558: return rc;
! 56559: }
! 56560: pMove = 0;
! 56561: rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
! 56562: freePage(pMove, &rc);
! 56563: releasePage(pMove);
! 56564: if( rc!=SQLITE_OK ){
! 56565: return rc;
! 56566: }
! 56567: *piMoved = maxRootPgno;
! 56568: }
! 56569:
! 56570: /* Set the new 'max-root-page' value in the database header. This
! 56571: ** is the old value less one, less one more if that happens to
! 56572: ** be a root-page number, less one again if that is the
! 56573: ** PENDING_BYTE_PAGE.
! 56574: */
! 56575: maxRootPgno--;
! 56576: while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
! 56577: || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
! 56578: maxRootPgno--;
! 56579: }
! 56580: assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
! 56581:
! 56582: rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
! 56583: }else{
! 56584: freePage(pPage, &rc);
! 56585: releasePage(pPage);
! 56586: }
! 56587: #endif
! 56588: }else{
! 56589: /* If sqlite3BtreeDropTable was called on page 1.
! 56590: ** This really never should happen except in a corrupt
! 56591: ** database.
! 56592: */
! 56593: zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
! 56594: releasePage(pPage);
! 56595: }
! 56596: return rc;
! 56597: }
! 56598: SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
! 56599: int rc;
! 56600: sqlite3BtreeEnter(p);
! 56601: rc = btreeDropTable(p, iTable, piMoved);
! 56602: sqlite3BtreeLeave(p);
! 56603: return rc;
! 56604: }
! 56605:
! 56606:
! 56607: /*
! 56608: ** This function may only be called if the b-tree connection already
! 56609: ** has a read or write transaction open on the database.
! 56610: **
! 56611: ** Read the meta-information out of a database file. Meta[0]
! 56612: ** is the number of free pages currently in the database. Meta[1]
! 56613: ** through meta[15] are available for use by higher layers. Meta[0]
! 56614: ** is read-only, the others are read/write.
! 56615: **
! 56616: ** The schema layer numbers meta values differently. At the schema
! 56617: ** layer (and the SetCookie and ReadCookie opcodes) the number of
! 56618: ** free pages is not visible. So Cookie[0] is the same as Meta[1].
! 56619: */
! 56620: SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
! 56621: BtShared *pBt = p->pBt;
! 56622:
! 56623: sqlite3BtreeEnter(p);
! 56624: assert( p->inTrans>TRANS_NONE );
! 56625: assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
! 56626: assert( pBt->pPage1 );
! 56627: assert( idx>=0 && idx<=15 );
! 56628:
! 56629: *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
! 56630:
! 56631: /* If auto-vacuum is disabled in this build and this is an auto-vacuum
! 56632: ** database, mark the database as read-only. */
! 56633: #ifdef SQLITE_OMIT_AUTOVACUUM
! 56634: if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
! 56635: pBt->btsFlags |= BTS_READ_ONLY;
! 56636: }
! 56637: #endif
! 56638:
! 56639: sqlite3BtreeLeave(p);
! 56640: }
! 56641:
! 56642: /*
! 56643: ** Write meta-information back into the database. Meta[0] is
! 56644: ** read-only and may not be written.
! 56645: */
! 56646: SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
! 56647: BtShared *pBt = p->pBt;
! 56648: unsigned char *pP1;
! 56649: int rc;
! 56650: assert( idx>=1 && idx<=15 );
! 56651: sqlite3BtreeEnter(p);
! 56652: assert( p->inTrans==TRANS_WRITE );
! 56653: assert( pBt->pPage1!=0 );
! 56654: pP1 = pBt->pPage1->aData;
! 56655: rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
! 56656: if( rc==SQLITE_OK ){
! 56657: put4byte(&pP1[36 + idx*4], iMeta);
! 56658: #ifndef SQLITE_OMIT_AUTOVACUUM
! 56659: if( idx==BTREE_INCR_VACUUM ){
! 56660: assert( pBt->autoVacuum || iMeta==0 );
! 56661: assert( iMeta==0 || iMeta==1 );
! 56662: pBt->incrVacuum = (u8)iMeta;
! 56663: }
! 56664: #endif
! 56665: }
! 56666: sqlite3BtreeLeave(p);
! 56667: return rc;
! 56668: }
! 56669:
! 56670: #ifndef SQLITE_OMIT_BTREECOUNT
! 56671: /*
! 56672: ** The first argument, pCur, is a cursor opened on some b-tree. Count the
! 56673: ** number of entries in the b-tree and write the result to *pnEntry.
! 56674: **
! 56675: ** SQLITE_OK is returned if the operation is successfully executed.
! 56676: ** Otherwise, if an error is encountered (i.e. an IO error or database
! 56677: ** corruption) an SQLite error code is returned.
! 56678: */
! 56679: SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
! 56680: i64 nEntry = 0; /* Value to return in *pnEntry */
! 56681: int rc; /* Return code */
! 56682:
! 56683: if( pCur->pgnoRoot==0 ){
! 56684: *pnEntry = 0;
! 56685: return SQLITE_OK;
! 56686: }
! 56687: rc = moveToRoot(pCur);
! 56688:
! 56689: /* Unless an error occurs, the following loop runs one iteration for each
! 56690: ** page in the B-Tree structure (not including overflow pages).
! 56691: */
! 56692: while( rc==SQLITE_OK ){
! 56693: int iIdx; /* Index of child node in parent */
! 56694: MemPage *pPage; /* Current page of the b-tree */
! 56695:
! 56696: /* If this is a leaf page or the tree is not an int-key tree, then
! 56697: ** this page contains countable entries. Increment the entry counter
! 56698: ** accordingly.
! 56699: */
! 56700: pPage = pCur->apPage[pCur->iPage];
! 56701: if( pPage->leaf || !pPage->intKey ){
! 56702: nEntry += pPage->nCell;
! 56703: }
! 56704:
! 56705: /* pPage is a leaf node. This loop navigates the cursor so that it
! 56706: ** points to the first interior cell that it points to the parent of
! 56707: ** the next page in the tree that has not yet been visited. The
! 56708: ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
! 56709: ** of the page, or to the number of cells in the page if the next page
! 56710: ** to visit is the right-child of its parent.
! 56711: **
! 56712: ** If all pages in the tree have been visited, return SQLITE_OK to the
! 56713: ** caller.
! 56714: */
! 56715: if( pPage->leaf ){
! 56716: do {
! 56717: if( pCur->iPage==0 ){
! 56718: /* All pages of the b-tree have been visited. Return successfully. */
! 56719: *pnEntry = nEntry;
! 56720: return SQLITE_OK;
! 56721: }
! 56722: moveToParent(pCur);
! 56723: }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
! 56724:
! 56725: pCur->aiIdx[pCur->iPage]++;
! 56726: pPage = pCur->apPage[pCur->iPage];
! 56727: }
! 56728:
! 56729: /* Descend to the child node of the cell that the cursor currently
! 56730: ** points at. This is the right-child if (iIdx==pPage->nCell).
! 56731: */
! 56732: iIdx = pCur->aiIdx[pCur->iPage];
! 56733: if( iIdx==pPage->nCell ){
! 56734: rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
! 56735: }else{
! 56736: rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
! 56737: }
! 56738: }
! 56739:
! 56740: /* An error has occurred. Return an error code. */
! 56741: return rc;
! 56742: }
! 56743: #endif
! 56744:
! 56745: /*
! 56746: ** Return the pager associated with a BTree. This routine is used for
! 56747: ** testing and debugging only.
! 56748: */
! 56749: SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
! 56750: return p->pBt->pPager;
! 56751: }
! 56752:
! 56753: #ifndef SQLITE_OMIT_INTEGRITY_CHECK
! 56754: /*
! 56755: ** Append a message to the error message string.
! 56756: */
! 56757: static void checkAppendMsg(
! 56758: IntegrityCk *pCheck,
! 56759: char *zMsg1,
! 56760: const char *zFormat,
! 56761: ...
! 56762: ){
! 56763: va_list ap;
! 56764: if( !pCheck->mxErr ) return;
! 56765: pCheck->mxErr--;
! 56766: pCheck->nErr++;
! 56767: va_start(ap, zFormat);
! 56768: if( pCheck->errMsg.nChar ){
! 56769: sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
! 56770: }
! 56771: if( zMsg1 ){
! 56772: sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
! 56773: }
! 56774: sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
! 56775: va_end(ap);
! 56776: if( pCheck->errMsg.mallocFailed ){
! 56777: pCheck->mallocFailed = 1;
! 56778: }
! 56779: }
! 56780: #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
! 56781:
! 56782: #ifndef SQLITE_OMIT_INTEGRITY_CHECK
! 56783: /*
! 56784: ** Add 1 to the reference count for page iPage. If this is the second
! 56785: ** reference to the page, add an error message to pCheck->zErrMsg.
! 56786: ** Return 1 if there are 2 ore more references to the page and 0 if
! 56787: ** if this is the first reference to the page.
! 56788: **
! 56789: ** Also check that the page number is in bounds.
! 56790: */
! 56791: static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
! 56792: if( iPage==0 ) return 1;
! 56793: if( iPage>pCheck->nPage ){
! 56794: checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
! 56795: return 1;
! 56796: }
! 56797: if( pCheck->anRef[iPage]==1 ){
! 56798: checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
! 56799: return 1;
! 56800: }
! 56801: return (pCheck->anRef[iPage]++)>1;
! 56802: }
! 56803:
! 56804: #ifndef SQLITE_OMIT_AUTOVACUUM
! 56805: /*
! 56806: ** Check that the entry in the pointer-map for page iChild maps to
! 56807: ** page iParent, pointer type ptrType. If not, append an error message
! 56808: ** to pCheck.
! 56809: */
! 56810: static void checkPtrmap(
! 56811: IntegrityCk *pCheck, /* Integrity check context */
! 56812: Pgno iChild, /* Child page number */
! 56813: u8 eType, /* Expected pointer map type */
! 56814: Pgno iParent, /* Expected pointer map parent page number */
! 56815: char *zContext /* Context description (used for error msg) */
! 56816: ){
! 56817: int rc;
! 56818: u8 ePtrmapType;
! 56819: Pgno iPtrmapParent;
! 56820:
! 56821: rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
! 56822: if( rc!=SQLITE_OK ){
! 56823: if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
! 56824: checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
! 56825: return;
! 56826: }
! 56827:
! 56828: if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
! 56829: checkAppendMsg(pCheck, zContext,
! 56830: "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
! 56831: iChild, eType, iParent, ePtrmapType, iPtrmapParent);
! 56832: }
! 56833: }
! 56834: #endif
! 56835:
! 56836: /*
! 56837: ** Check the integrity of the freelist or of an overflow page list.
! 56838: ** Verify that the number of pages on the list is N.
! 56839: */
! 56840: static void checkList(
! 56841: IntegrityCk *pCheck, /* Integrity checking context */
! 56842: int isFreeList, /* True for a freelist. False for overflow page list */
! 56843: int iPage, /* Page number for first page in the list */
! 56844: int N, /* Expected number of pages in the list */
! 56845: char *zContext /* Context for error messages */
! 56846: ){
! 56847: int i;
! 56848: int expected = N;
! 56849: int iFirst = iPage;
! 56850: while( N-- > 0 && pCheck->mxErr ){
! 56851: DbPage *pOvflPage;
! 56852: unsigned char *pOvflData;
! 56853: if( iPage<1 ){
! 56854: checkAppendMsg(pCheck, zContext,
! 56855: "%d of %d pages missing from overflow list starting at %d",
! 56856: N+1, expected, iFirst);
! 56857: break;
! 56858: }
! 56859: if( checkRef(pCheck, iPage, zContext) ) break;
! 56860: if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
! 56861: checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
! 56862: break;
! 56863: }
! 56864: pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
! 56865: if( isFreeList ){
! 56866: int n = get4byte(&pOvflData[4]);
! 56867: #ifndef SQLITE_OMIT_AUTOVACUUM
! 56868: if( pCheck->pBt->autoVacuum ){
! 56869: checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
! 56870: }
! 56871: #endif
! 56872: if( n>(int)pCheck->pBt->usableSize/4-2 ){
! 56873: checkAppendMsg(pCheck, zContext,
! 56874: "freelist leaf count too big on page %d", iPage);
! 56875: N--;
! 56876: }else{
! 56877: for(i=0; i<n; i++){
! 56878: Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
! 56879: #ifndef SQLITE_OMIT_AUTOVACUUM
! 56880: if( pCheck->pBt->autoVacuum ){
! 56881: checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
! 56882: }
! 56883: #endif
! 56884: checkRef(pCheck, iFreePage, zContext);
! 56885: }
! 56886: N -= n;
! 56887: }
! 56888: }
! 56889: #ifndef SQLITE_OMIT_AUTOVACUUM
! 56890: else{
! 56891: /* If this database supports auto-vacuum and iPage is not the last
! 56892: ** page in this overflow list, check that the pointer-map entry for
! 56893: ** the following page matches iPage.
! 56894: */
! 56895: if( pCheck->pBt->autoVacuum && N>0 ){
! 56896: i = get4byte(pOvflData);
! 56897: checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
! 56898: }
! 56899: }
! 56900: #endif
! 56901: iPage = get4byte(pOvflData);
! 56902: sqlite3PagerUnref(pOvflPage);
! 56903: }
! 56904: }
! 56905: #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
! 56906:
! 56907: #ifndef SQLITE_OMIT_INTEGRITY_CHECK
! 56908: /*
! 56909: ** Do various sanity checks on a single page of a tree. Return
! 56910: ** the tree depth. Root pages return 0. Parents of root pages
! 56911: ** return 1, and so forth.
! 56912: **
! 56913: ** These checks are done:
! 56914: **
! 56915: ** 1. Make sure that cells and freeblocks do not overlap
! 56916: ** but combine to completely cover the page.
! 56917: ** NO 2. Make sure cell keys are in order.
! 56918: ** NO 3. Make sure no key is less than or equal to zLowerBound.
! 56919: ** NO 4. Make sure no key is greater than or equal to zUpperBound.
! 56920: ** 5. Check the integrity of overflow pages.
! 56921: ** 6. Recursively call checkTreePage on all children.
! 56922: ** 7. Verify that the depth of all children is the same.
! 56923: ** 8. Make sure this page is at least 33% full or else it is
! 56924: ** the root of the tree.
! 56925: */
! 56926: static int checkTreePage(
! 56927: IntegrityCk *pCheck, /* Context for the sanity check */
! 56928: int iPage, /* Page number of the page to check */
! 56929: char *zParentContext, /* Parent context */
! 56930: i64 *pnParentMinKey,
! 56931: i64 *pnParentMaxKey
! 56932: ){
! 56933: MemPage *pPage;
! 56934: int i, rc, depth, d2, pgno, cnt;
! 56935: int hdr, cellStart;
! 56936: int nCell;
! 56937: u8 *data;
! 56938: BtShared *pBt;
! 56939: int usableSize;
! 56940: char zContext[100];
! 56941: char *hit = 0;
! 56942: i64 nMinKey = 0;
! 56943: i64 nMaxKey = 0;
! 56944:
! 56945: sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
! 56946:
! 56947: /* Check that the page exists
! 56948: */
! 56949: pBt = pCheck->pBt;
! 56950: usableSize = pBt->usableSize;
! 56951: if( iPage==0 ) return 0;
! 56952: if( checkRef(pCheck, iPage, zParentContext) ) return 0;
! 56953: if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
! 56954: checkAppendMsg(pCheck, zContext,
! 56955: "unable to get the page. error code=%d", rc);
! 56956: return 0;
! 56957: }
! 56958:
! 56959: /* Clear MemPage.isInit to make sure the corruption detection code in
! 56960: ** btreeInitPage() is executed. */
! 56961: pPage->isInit = 0;
! 56962: if( (rc = btreeInitPage(pPage))!=0 ){
! 56963: assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
! 56964: checkAppendMsg(pCheck, zContext,
! 56965: "btreeInitPage() returns error code %d", rc);
! 56966: releasePage(pPage);
! 56967: return 0;
! 56968: }
! 56969:
! 56970: /* Check out all the cells.
! 56971: */
! 56972: depth = 0;
! 56973: for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
! 56974: u8 *pCell;
! 56975: u32 sz;
! 56976: CellInfo info;
! 56977:
! 56978: /* Check payload overflow pages
! 56979: */
! 56980: sqlite3_snprintf(sizeof(zContext), zContext,
! 56981: "On tree page %d cell %d: ", iPage, i);
! 56982: pCell = findCell(pPage,i);
! 56983: btreeParseCellPtr(pPage, pCell, &info);
! 56984: sz = info.nData;
! 56985: if( !pPage->intKey ) sz += (int)info.nKey;
! 56986: /* For intKey pages, check that the keys are in order.
! 56987: */
! 56988: else if( i==0 ) nMinKey = nMaxKey = info.nKey;
! 56989: else{
! 56990: if( info.nKey <= nMaxKey ){
! 56991: checkAppendMsg(pCheck, zContext,
! 56992: "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
! 56993: }
! 56994: nMaxKey = info.nKey;
! 56995: }
! 56996: assert( sz==info.nPayload );
! 56997: if( (sz>info.nLocal)
! 56998: && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
! 56999: ){
! 57000: int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
! 57001: Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
! 57002: #ifndef SQLITE_OMIT_AUTOVACUUM
! 57003: if( pBt->autoVacuum ){
! 57004: checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
! 57005: }
! 57006: #endif
! 57007: checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
! 57008: }
! 57009:
! 57010: /* Check sanity of left child page.
! 57011: */
! 57012: if( !pPage->leaf ){
! 57013: pgno = get4byte(pCell);
! 57014: #ifndef SQLITE_OMIT_AUTOVACUUM
! 57015: if( pBt->autoVacuum ){
! 57016: checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
! 57017: }
! 57018: #endif
! 57019: d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
! 57020: if( i>0 && d2!=depth ){
! 57021: checkAppendMsg(pCheck, zContext, "Child page depth differs");
! 57022: }
! 57023: depth = d2;
! 57024: }
! 57025: }
! 57026:
! 57027: if( !pPage->leaf ){
! 57028: pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
! 57029: sqlite3_snprintf(sizeof(zContext), zContext,
! 57030: "On page %d at right child: ", iPage);
! 57031: #ifndef SQLITE_OMIT_AUTOVACUUM
! 57032: if( pBt->autoVacuum ){
! 57033: checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
! 57034: }
! 57035: #endif
! 57036: checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
! 57037: }
! 57038:
! 57039: /* For intKey leaf pages, check that the min/max keys are in order
! 57040: ** with any left/parent/right pages.
! 57041: */
! 57042: if( pPage->leaf && pPage->intKey ){
! 57043: /* if we are a left child page */
! 57044: if( pnParentMinKey ){
! 57045: /* if we are the left most child page */
! 57046: if( !pnParentMaxKey ){
! 57047: if( nMaxKey > *pnParentMinKey ){
! 57048: checkAppendMsg(pCheck, zContext,
! 57049: "Rowid %lld out of order (max larger than parent min of %lld)",
! 57050: nMaxKey, *pnParentMinKey);
! 57051: }
! 57052: }else{
! 57053: if( nMinKey <= *pnParentMinKey ){
! 57054: checkAppendMsg(pCheck, zContext,
! 57055: "Rowid %lld out of order (min less than parent min of %lld)",
! 57056: nMinKey, *pnParentMinKey);
! 57057: }
! 57058: if( nMaxKey > *pnParentMaxKey ){
! 57059: checkAppendMsg(pCheck, zContext,
! 57060: "Rowid %lld out of order (max larger than parent max of %lld)",
! 57061: nMaxKey, *pnParentMaxKey);
! 57062: }
! 57063: *pnParentMinKey = nMaxKey;
! 57064: }
! 57065: /* else if we're a right child page */
! 57066: } else if( pnParentMaxKey ){
! 57067: if( nMinKey <= *pnParentMaxKey ){
! 57068: checkAppendMsg(pCheck, zContext,
! 57069: "Rowid %lld out of order (min less than parent max of %lld)",
! 57070: nMinKey, *pnParentMaxKey);
! 57071: }
! 57072: }
! 57073: }
! 57074:
! 57075: /* Check for complete coverage of the page
! 57076: */
! 57077: data = pPage->aData;
! 57078: hdr = pPage->hdrOffset;
! 57079: hit = sqlite3PageMalloc( pBt->pageSize );
! 57080: if( hit==0 ){
! 57081: pCheck->mallocFailed = 1;
! 57082: }else{
! 57083: int contentOffset = get2byteNotZero(&data[hdr+5]);
! 57084: assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
! 57085: memset(hit+contentOffset, 0, usableSize-contentOffset);
! 57086: memset(hit, 1, contentOffset);
! 57087: nCell = get2byte(&data[hdr+3]);
! 57088: cellStart = hdr + 12 - 4*pPage->leaf;
! 57089: for(i=0; i<nCell; i++){
! 57090: int pc = get2byte(&data[cellStart+i*2]);
! 57091: u32 size = 65536;
! 57092: int j;
! 57093: if( pc<=usableSize-4 ){
! 57094: size = cellSizePtr(pPage, &data[pc]);
! 57095: }
! 57096: if( (int)(pc+size-1)>=usableSize ){
! 57097: checkAppendMsg(pCheck, 0,
! 57098: "Corruption detected in cell %d on page %d",i,iPage);
! 57099: }else{
! 57100: for(j=pc+size-1; j>=pc; j--) hit[j]++;
! 57101: }
! 57102: }
! 57103: i = get2byte(&data[hdr+1]);
! 57104: while( i>0 ){
! 57105: int size, j;
! 57106: assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */
! 57107: size = get2byte(&data[i+2]);
! 57108: assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */
! 57109: for(j=i+size-1; j>=i; j--) hit[j]++;
! 57110: j = get2byte(&data[i]);
! 57111: assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */
! 57112: assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */
! 57113: i = j;
! 57114: }
! 57115: for(i=cnt=0; i<usableSize; i++){
! 57116: if( hit[i]==0 ){
! 57117: cnt++;
! 57118: }else if( hit[i]>1 ){
! 57119: checkAppendMsg(pCheck, 0,
! 57120: "Multiple uses for byte %d of page %d", i, iPage);
! 57121: break;
! 57122: }
! 57123: }
! 57124: if( cnt!=data[hdr+7] ){
! 57125: checkAppendMsg(pCheck, 0,
! 57126: "Fragmentation of %d bytes reported as %d on page %d",
! 57127: cnt, data[hdr+7], iPage);
! 57128: }
! 57129: }
! 57130: sqlite3PageFree(hit);
! 57131: releasePage(pPage);
! 57132: return depth+1;
! 57133: }
! 57134: #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
! 57135:
! 57136: #ifndef SQLITE_OMIT_INTEGRITY_CHECK
! 57137: /*
! 57138: ** This routine does a complete check of the given BTree file. aRoot[] is
! 57139: ** an array of pages numbers were each page number is the root page of
! 57140: ** a table. nRoot is the number of entries in aRoot.
! 57141: **
! 57142: ** A read-only or read-write transaction must be opened before calling
! 57143: ** this function.
! 57144: **
! 57145: ** Write the number of error seen in *pnErr. Except for some memory
! 57146: ** allocation errors, an error message held in memory obtained from
! 57147: ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
! 57148: ** returned. If a memory allocation error occurs, NULL is returned.
! 57149: */
! 57150: SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
! 57151: Btree *p, /* The btree to be checked */
! 57152: int *aRoot, /* An array of root pages numbers for individual trees */
! 57153: int nRoot, /* Number of entries in aRoot[] */
! 57154: int mxErr, /* Stop reporting errors after this many */
! 57155: int *pnErr /* Write number of errors seen to this variable */
! 57156: ){
! 57157: Pgno i;
! 57158: int nRef;
! 57159: IntegrityCk sCheck;
! 57160: BtShared *pBt = p->pBt;
! 57161: char zErr[100];
! 57162:
! 57163: sqlite3BtreeEnter(p);
! 57164: assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
! 57165: nRef = sqlite3PagerRefcount(pBt->pPager);
! 57166: sCheck.pBt = pBt;
! 57167: sCheck.pPager = pBt->pPager;
! 57168: sCheck.nPage = btreePagecount(sCheck.pBt);
! 57169: sCheck.mxErr = mxErr;
! 57170: sCheck.nErr = 0;
! 57171: sCheck.mallocFailed = 0;
! 57172: *pnErr = 0;
! 57173: if( sCheck.nPage==0 ){
! 57174: sqlite3BtreeLeave(p);
! 57175: return 0;
! 57176: }
! 57177: sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
! 57178: if( !sCheck.anRef ){
! 57179: *pnErr = 1;
! 57180: sqlite3BtreeLeave(p);
! 57181: return 0;
! 57182: }
! 57183: for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
! 57184: i = PENDING_BYTE_PAGE(pBt);
! 57185: if( i<=sCheck.nPage ){
! 57186: sCheck.anRef[i] = 1;
! 57187: }
! 57188: sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
! 57189: sCheck.errMsg.useMalloc = 2;
! 57190:
! 57191: /* Check the integrity of the freelist
! 57192: */
! 57193: checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
! 57194: get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
! 57195:
! 57196: /* Check all the tables.
! 57197: */
! 57198: for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
! 57199: if( aRoot[i]==0 ) continue;
! 57200: #ifndef SQLITE_OMIT_AUTOVACUUM
! 57201: if( pBt->autoVacuum && aRoot[i]>1 ){
! 57202: checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
! 57203: }
! 57204: #endif
! 57205: checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
! 57206: }
! 57207:
! 57208: /* Make sure every page in the file is referenced
! 57209: */
! 57210: for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
! 57211: #ifdef SQLITE_OMIT_AUTOVACUUM
! 57212: if( sCheck.anRef[i]==0 ){
! 57213: checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
! 57214: }
! 57215: #else
! 57216: /* If the database supports auto-vacuum, make sure no tables contain
! 57217: ** references to pointer-map pages.
! 57218: */
! 57219: if( sCheck.anRef[i]==0 &&
! 57220: (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
! 57221: checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
! 57222: }
! 57223: if( sCheck.anRef[i]!=0 &&
! 57224: (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
! 57225: checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
! 57226: }
! 57227: #endif
! 57228: }
! 57229:
! 57230: /* Make sure this analysis did not leave any unref() pages.
! 57231: ** This is an internal consistency check; an integrity check
! 57232: ** of the integrity check.
! 57233: */
! 57234: if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
! 57235: checkAppendMsg(&sCheck, 0,
! 57236: "Outstanding page count goes from %d to %d during this analysis",
! 57237: nRef, sqlite3PagerRefcount(pBt->pPager)
! 57238: );
! 57239: }
! 57240:
! 57241: /* Clean up and report errors.
! 57242: */
! 57243: sqlite3BtreeLeave(p);
! 57244: sqlite3_free(sCheck.anRef);
! 57245: if( sCheck.mallocFailed ){
! 57246: sqlite3StrAccumReset(&sCheck.errMsg);
! 57247: *pnErr = sCheck.nErr+1;
! 57248: return 0;
! 57249: }
! 57250: *pnErr = sCheck.nErr;
! 57251: if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
! 57252: return sqlite3StrAccumFinish(&sCheck.errMsg);
! 57253: }
! 57254: #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
! 57255:
! 57256: /*
! 57257: ** Return the full pathname of the underlying database file.
! 57258: **
! 57259: ** The pager filename is invariant as long as the pager is
! 57260: ** open so it is safe to access without the BtShared mutex.
! 57261: */
! 57262: SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
! 57263: assert( p->pBt->pPager!=0 );
! 57264: return sqlite3PagerFilename(p->pBt->pPager);
! 57265: }
! 57266:
! 57267: /*
! 57268: ** Return the pathname of the journal file for this database. The return
! 57269: ** value of this routine is the same regardless of whether the journal file
! 57270: ** has been created or not.
! 57271: **
! 57272: ** The pager journal filename is invariant as long as the pager is
! 57273: ** open so it is safe to access without the BtShared mutex.
! 57274: */
! 57275: SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
! 57276: assert( p->pBt->pPager!=0 );
! 57277: return sqlite3PagerJournalname(p->pBt->pPager);
! 57278: }
! 57279:
! 57280: /*
! 57281: ** Return non-zero if a transaction is active.
! 57282: */
! 57283: SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
! 57284: assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
! 57285: return (p && (p->inTrans==TRANS_WRITE));
! 57286: }
! 57287:
! 57288: #ifndef SQLITE_OMIT_WAL
! 57289: /*
! 57290: ** Run a checkpoint on the Btree passed as the first argument.
! 57291: **
! 57292: ** Return SQLITE_LOCKED if this or any other connection has an open
! 57293: ** transaction on the shared-cache the argument Btree is connected to.
! 57294: **
! 57295: ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
! 57296: */
! 57297: SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
! 57298: int rc = SQLITE_OK;
! 57299: if( p ){
! 57300: BtShared *pBt = p->pBt;
! 57301: sqlite3BtreeEnter(p);
! 57302: if( pBt->inTransaction!=TRANS_NONE ){
! 57303: rc = SQLITE_LOCKED;
! 57304: }else{
! 57305: rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
! 57306: }
! 57307: sqlite3BtreeLeave(p);
! 57308: }
! 57309: return rc;
! 57310: }
! 57311: #endif
! 57312:
! 57313: /*
! 57314: ** Return non-zero if a read (or write) transaction is active.
! 57315: */
! 57316: SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
! 57317: assert( p );
! 57318: assert( sqlite3_mutex_held(p->db->mutex) );
! 57319: return p->inTrans!=TRANS_NONE;
! 57320: }
! 57321:
! 57322: SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
! 57323: assert( p );
! 57324: assert( sqlite3_mutex_held(p->db->mutex) );
! 57325: return p->nBackup!=0;
! 57326: }
! 57327:
! 57328: /*
! 57329: ** This function returns a pointer to a blob of memory associated with
! 57330: ** a single shared-btree. The memory is used by client code for its own
! 57331: ** purposes (for example, to store a high-level schema associated with
! 57332: ** the shared-btree). The btree layer manages reference counting issues.
! 57333: **
! 57334: ** The first time this is called on a shared-btree, nBytes bytes of memory
! 57335: ** are allocated, zeroed, and returned to the caller. For each subsequent
! 57336: ** call the nBytes parameter is ignored and a pointer to the same blob
! 57337: ** of memory returned.
! 57338: **
! 57339: ** If the nBytes parameter is 0 and the blob of memory has not yet been
! 57340: ** allocated, a null pointer is returned. If the blob has already been
! 57341: ** allocated, it is returned as normal.
! 57342: **
! 57343: ** Just before the shared-btree is closed, the function passed as the
! 57344: ** xFree argument when the memory allocation was made is invoked on the
! 57345: ** blob of allocated memory. The xFree function should not call sqlite3_free()
! 57346: ** on the memory, the btree layer does that.
! 57347: */
! 57348: SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
! 57349: BtShared *pBt = p->pBt;
! 57350: sqlite3BtreeEnter(p);
! 57351: if( !pBt->pSchema && nBytes ){
! 57352: pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
! 57353: pBt->xFreeSchema = xFree;
! 57354: }
! 57355: sqlite3BtreeLeave(p);
! 57356: return pBt->pSchema;
! 57357: }
! 57358:
! 57359: /*
! 57360: ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
! 57361: ** btree as the argument handle holds an exclusive lock on the
! 57362: ** sqlite_master table. Otherwise SQLITE_OK.
! 57363: */
! 57364: SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
! 57365: int rc;
! 57366: assert( sqlite3_mutex_held(p->db->mutex) );
! 57367: sqlite3BtreeEnter(p);
! 57368: rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
! 57369: assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
! 57370: sqlite3BtreeLeave(p);
! 57371: return rc;
! 57372: }
! 57373:
! 57374:
! 57375: #ifndef SQLITE_OMIT_SHARED_CACHE
! 57376: /*
! 57377: ** Obtain a lock on the table whose root page is iTab. The
! 57378: ** lock is a write lock if isWritelock is true or a read lock
! 57379: ** if it is false.
! 57380: */
! 57381: SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
! 57382: int rc = SQLITE_OK;
! 57383: assert( p->inTrans!=TRANS_NONE );
! 57384: if( p->sharable ){
! 57385: u8 lockType = READ_LOCK + isWriteLock;
! 57386: assert( READ_LOCK+1==WRITE_LOCK );
! 57387: assert( isWriteLock==0 || isWriteLock==1 );
! 57388:
! 57389: sqlite3BtreeEnter(p);
! 57390: rc = querySharedCacheTableLock(p, iTab, lockType);
! 57391: if( rc==SQLITE_OK ){
! 57392: rc = setSharedCacheTableLock(p, iTab, lockType);
! 57393: }
! 57394: sqlite3BtreeLeave(p);
! 57395: }
! 57396: return rc;
! 57397: }
! 57398: #endif
! 57399:
! 57400: #ifndef SQLITE_OMIT_INCRBLOB
! 57401: /*
! 57402: ** Argument pCsr must be a cursor opened for writing on an
! 57403: ** INTKEY table currently pointing at a valid table entry.
! 57404: ** This function modifies the data stored as part of that entry.
! 57405: **
! 57406: ** Only the data content may only be modified, it is not possible to
! 57407: ** change the length of the data stored. If this function is called with
! 57408: ** parameters that attempt to write past the end of the existing data,
! 57409: ** no modifications are made and SQLITE_CORRUPT is returned.
! 57410: */
! 57411: SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
! 57412: int rc;
! 57413: assert( cursorHoldsMutex(pCsr) );
! 57414: assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
! 57415: assert( pCsr->isIncrblobHandle );
! 57416:
! 57417: rc = restoreCursorPosition(pCsr);
! 57418: if( rc!=SQLITE_OK ){
! 57419: return rc;
! 57420: }
! 57421: assert( pCsr->eState!=CURSOR_REQUIRESEEK );
! 57422: if( pCsr->eState!=CURSOR_VALID ){
! 57423: return SQLITE_ABORT;
! 57424: }
! 57425:
! 57426: /* Check some assumptions:
! 57427: ** (a) the cursor is open for writing,
! 57428: ** (b) there is a read/write transaction open,
! 57429: ** (c) the connection holds a write-lock on the table (if required),
! 57430: ** (d) there are no conflicting read-locks, and
! 57431: ** (e) the cursor points at a valid row of an intKey table.
! 57432: */
! 57433: if( !pCsr->wrFlag ){
! 57434: return SQLITE_READONLY;
! 57435: }
! 57436: assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
! 57437: && pCsr->pBt->inTransaction==TRANS_WRITE );
! 57438: assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
! 57439: assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
! 57440: assert( pCsr->apPage[pCsr->iPage]->intKey );
! 57441:
! 57442: return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
! 57443: }
! 57444:
! 57445: /*
! 57446: ** Set a flag on this cursor to cache the locations of pages from the
! 57447: ** overflow list for the current row. This is used by cursors opened
! 57448: ** for incremental blob IO only.
! 57449: **
! 57450: ** This function sets a flag only. The actual page location cache
! 57451: ** (stored in BtCursor.aOverflow[]) is allocated and used by function
! 57452: ** accessPayload() (the worker function for sqlite3BtreeData() and
! 57453: ** sqlite3BtreePutData()).
! 57454: */
! 57455: SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
! 57456: assert( cursorHoldsMutex(pCur) );
! 57457: assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
! 57458: invalidateOverflowCache(pCur);
! 57459: pCur->isIncrblobHandle = 1;
! 57460: }
! 57461: #endif
! 57462:
! 57463: /*
! 57464: ** Set both the "read version" (single byte at byte offset 18) and
! 57465: ** "write version" (single byte at byte offset 19) fields in the database
! 57466: ** header to iVersion.
! 57467: */
! 57468: SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
! 57469: BtShared *pBt = pBtree->pBt;
! 57470: int rc; /* Return code */
! 57471:
! 57472: assert( iVersion==1 || iVersion==2 );
! 57473:
! 57474: /* If setting the version fields to 1, do not automatically open the
! 57475: ** WAL connection, even if the version fields are currently set to 2.
! 57476: */
! 57477: pBt->btsFlags &= ~BTS_NO_WAL;
! 57478: if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
! 57479:
! 57480: rc = sqlite3BtreeBeginTrans(pBtree, 0);
! 57481: if( rc==SQLITE_OK ){
! 57482: u8 *aData = pBt->pPage1->aData;
! 57483: if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
! 57484: rc = sqlite3BtreeBeginTrans(pBtree, 2);
! 57485: if( rc==SQLITE_OK ){
! 57486: rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
! 57487: if( rc==SQLITE_OK ){
! 57488: aData[18] = (u8)iVersion;
! 57489: aData[19] = (u8)iVersion;
! 57490: }
! 57491: }
! 57492: }
! 57493: }
! 57494:
! 57495: pBt->btsFlags &= ~BTS_NO_WAL;
! 57496: return rc;
! 57497: }
! 57498:
! 57499: /************** End of btree.c ***********************************************/
! 57500: /************** Begin file backup.c ******************************************/
! 57501: /*
! 57502: ** 2009 January 28
! 57503: **
! 57504: ** The author disclaims copyright to this source code. In place of
! 57505: ** a legal notice, here is a blessing:
! 57506: **
! 57507: ** May you do good and not evil.
! 57508: ** May you find forgiveness for yourself and forgive others.
! 57509: ** May you share freely, never taking more than you give.
! 57510: **
! 57511: *************************************************************************
! 57512: ** This file contains the implementation of the sqlite3_backup_XXX()
! 57513: ** API functions and the related features.
! 57514: */
! 57515:
! 57516: /* Macro to find the minimum of two numeric values.
! 57517: */
! 57518: #ifndef MIN
! 57519: # define MIN(x,y) ((x)<(y)?(x):(y))
! 57520: #endif
! 57521:
! 57522: /*
! 57523: ** Structure allocated for each backup operation.
! 57524: */
! 57525: struct sqlite3_backup {
! 57526: sqlite3* pDestDb; /* Destination database handle */
! 57527: Btree *pDest; /* Destination b-tree file */
! 57528: u32 iDestSchema; /* Original schema cookie in destination */
! 57529: int bDestLocked; /* True once a write-transaction is open on pDest */
! 57530:
! 57531: Pgno iNext; /* Page number of the next source page to copy */
! 57532: sqlite3* pSrcDb; /* Source database handle */
! 57533: Btree *pSrc; /* Source b-tree file */
! 57534:
! 57535: int rc; /* Backup process error code */
! 57536:
! 57537: /* These two variables are set by every call to backup_step(). They are
! 57538: ** read by calls to backup_remaining() and backup_pagecount().
! 57539: */
! 57540: Pgno nRemaining; /* Number of pages left to copy */
! 57541: Pgno nPagecount; /* Total number of pages to copy */
! 57542:
! 57543: int isAttached; /* True once backup has been registered with pager */
! 57544: sqlite3_backup *pNext; /* Next backup associated with source pager */
! 57545: };
! 57546:
! 57547: /*
! 57548: ** THREAD SAFETY NOTES:
! 57549: **
! 57550: ** Once it has been created using backup_init(), a single sqlite3_backup
! 57551: ** structure may be accessed via two groups of thread-safe entry points:
! 57552: **
! 57553: ** * Via the sqlite3_backup_XXX() API function backup_step() and
! 57554: ** backup_finish(). Both these functions obtain the source database
! 57555: ** handle mutex and the mutex associated with the source BtShared
! 57556: ** structure, in that order.
! 57557: **
! 57558: ** * Via the BackupUpdate() and BackupRestart() functions, which are
! 57559: ** invoked by the pager layer to report various state changes in
! 57560: ** the page cache associated with the source database. The mutex
! 57561: ** associated with the source database BtShared structure will always
! 57562: ** be held when either of these functions are invoked.
! 57563: **
! 57564: ** The other sqlite3_backup_XXX() API functions, backup_remaining() and
! 57565: ** backup_pagecount() are not thread-safe functions. If they are called
! 57566: ** while some other thread is calling backup_step() or backup_finish(),
! 57567: ** the values returned may be invalid. There is no way for a call to
! 57568: ** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
! 57569: ** or backup_pagecount().
! 57570: **
! 57571: ** Depending on the SQLite configuration, the database handles and/or
! 57572: ** the Btree objects may have their own mutexes that require locking.
! 57573: ** Non-sharable Btrees (in-memory databases for example), do not have
! 57574: ** associated mutexes.
! 57575: */
! 57576:
! 57577: /*
! 57578: ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
! 57579: ** in connection handle pDb. If such a database cannot be found, return
! 57580: ** a NULL pointer and write an error message to pErrorDb.
! 57581: **
! 57582: ** If the "temp" database is requested, it may need to be opened by this
! 57583: ** function. If an error occurs while doing so, return 0 and write an
! 57584: ** error message to pErrorDb.
! 57585: */
! 57586: static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
! 57587: int i = sqlite3FindDbName(pDb, zDb);
! 57588:
! 57589: if( i==1 ){
! 57590: Parse *pParse;
! 57591: int rc = 0;
! 57592: pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
! 57593: if( pParse==0 ){
! 57594: sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
! 57595: rc = SQLITE_NOMEM;
! 57596: }else{
! 57597: pParse->db = pDb;
! 57598: if( sqlite3OpenTempDatabase(pParse) ){
! 57599: sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
! 57600: rc = SQLITE_ERROR;
! 57601: }
! 57602: sqlite3DbFree(pErrorDb, pParse->zErrMsg);
! 57603: sqlite3StackFree(pErrorDb, pParse);
! 57604: }
! 57605: if( rc ){
! 57606: return 0;
! 57607: }
! 57608: }
! 57609:
! 57610: if( i<0 ){
! 57611: sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
! 57612: return 0;
! 57613: }
! 57614:
! 57615: return pDb->aDb[i].pBt;
! 57616: }
! 57617:
! 57618: /*
! 57619: ** Attempt to set the page size of the destination to match the page size
! 57620: ** of the source.
! 57621: */
! 57622: static int setDestPgsz(sqlite3_backup *p){
! 57623: int rc;
! 57624: rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
! 57625: return rc;
! 57626: }
! 57627:
! 57628: /*
! 57629: ** Create an sqlite3_backup process to copy the contents of zSrcDb from
! 57630: ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
! 57631: ** a pointer to the new sqlite3_backup object.
! 57632: **
! 57633: ** If an error occurs, NULL is returned and an error code and error message
! 57634: ** stored in database handle pDestDb.
! 57635: */
! 57636: SQLITE_API sqlite3_backup *sqlite3_backup_init(
! 57637: sqlite3* pDestDb, /* Database to write to */
! 57638: const char *zDestDb, /* Name of database within pDestDb */
! 57639: sqlite3* pSrcDb, /* Database connection to read from */
! 57640: const char *zSrcDb /* Name of database within pSrcDb */
! 57641: ){
! 57642: sqlite3_backup *p; /* Value to return */
! 57643:
! 57644: /* Lock the source database handle. The destination database
! 57645: ** handle is not locked in this routine, but it is locked in
! 57646: ** sqlite3_backup_step(). The user is required to ensure that no
! 57647: ** other thread accesses the destination handle for the duration
! 57648: ** of the backup operation. Any attempt to use the destination
! 57649: ** database connection while a backup is in progress may cause
! 57650: ** a malfunction or a deadlock.
! 57651: */
! 57652: sqlite3_mutex_enter(pSrcDb->mutex);
! 57653: sqlite3_mutex_enter(pDestDb->mutex);
! 57654:
! 57655: if( pSrcDb==pDestDb ){
! 57656: sqlite3Error(
! 57657: pDestDb, SQLITE_ERROR, "source and destination must be distinct"
! 57658: );
! 57659: p = 0;
! 57660: }else {
! 57661: /* Allocate space for a new sqlite3_backup object...
! 57662: ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
! 57663: ** call to sqlite3_backup_init() and is destroyed by a call to
! 57664: ** sqlite3_backup_finish(). */
! 57665: p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup));
! 57666: if( !p ){
! 57667: sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
! 57668: }
! 57669: }
! 57670:
! 57671: /* If the allocation succeeded, populate the new object. */
! 57672: if( p ){
! 57673: memset(p, 0, sizeof(sqlite3_backup));
! 57674: p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
! 57675: p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
! 57676: p->pDestDb = pDestDb;
! 57677: p->pSrcDb = pSrcDb;
! 57678: p->iNext = 1;
! 57679: p->isAttached = 0;
! 57680:
! 57681: if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
! 57682: /* One (or both) of the named databases did not exist or an OOM
! 57683: ** error was hit. The error has already been written into the
! 57684: ** pDestDb handle. All that is left to do here is free the
! 57685: ** sqlite3_backup structure.
! 57686: */
! 57687: sqlite3_free(p);
! 57688: p = 0;
! 57689: }
! 57690: }
! 57691: if( p ){
! 57692: p->pSrc->nBackup++;
! 57693: }
! 57694:
! 57695: sqlite3_mutex_leave(pDestDb->mutex);
! 57696: sqlite3_mutex_leave(pSrcDb->mutex);
! 57697: return p;
! 57698: }
! 57699:
! 57700: /*
! 57701: ** Argument rc is an SQLite error code. Return true if this error is
! 57702: ** considered fatal if encountered during a backup operation. All errors
! 57703: ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
! 57704: */
! 57705: static int isFatalError(int rc){
! 57706: return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
! 57707: }
! 57708:
! 57709: /*
! 57710: ** Parameter zSrcData points to a buffer containing the data for
! 57711: ** page iSrcPg from the source database. Copy this data into the
! 57712: ** destination database.
! 57713: */
! 57714: static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
! 57715: Pager * const pDestPager = sqlite3BtreePager(p->pDest);
! 57716: const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
! 57717: int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
! 57718: const int nCopy = MIN(nSrcPgsz, nDestPgsz);
! 57719: const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
! 57720: #ifdef SQLITE_HAS_CODEC
! 57721: int nSrcReserve = sqlite3BtreeGetReserve(p->pSrc);
! 57722: int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
! 57723: #endif
! 57724:
! 57725: int rc = SQLITE_OK;
! 57726: i64 iOff;
! 57727:
! 57728: assert( p->bDestLocked );
! 57729: assert( !isFatalError(p->rc) );
! 57730: assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
! 57731: assert( zSrcData );
! 57732:
! 57733: /* Catch the case where the destination is an in-memory database and the
! 57734: ** page sizes of the source and destination differ.
! 57735: */
! 57736: if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
! 57737: rc = SQLITE_READONLY;
! 57738: }
! 57739:
! 57740: #ifdef SQLITE_HAS_CODEC
! 57741: /* Backup is not possible if the page size of the destination is changing
! 57742: ** and a codec is in use.
! 57743: */
! 57744: if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
! 57745: rc = SQLITE_READONLY;
! 57746: }
! 57747:
! 57748: /* Backup is not possible if the number of bytes of reserve space differ
! 57749: ** between source and destination. If there is a difference, try to
! 57750: ** fix the destination to agree with the source. If that is not possible,
! 57751: ** then the backup cannot proceed.
! 57752: */
! 57753: if( nSrcReserve!=nDestReserve ){
! 57754: u32 newPgsz = nSrcPgsz;
! 57755: rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
! 57756: if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
! 57757: }
! 57758: #endif
! 57759:
! 57760: /* This loop runs once for each destination page spanned by the source
! 57761: ** page. For each iteration, variable iOff is set to the byte offset
! 57762: ** of the destination page.
! 57763: */
! 57764: for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
! 57765: DbPage *pDestPg = 0;
! 57766: Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
! 57767: if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
! 57768: if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
! 57769: && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
! 57770: ){
! 57771: const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
! 57772: u8 *zDestData = sqlite3PagerGetData(pDestPg);
! 57773: u8 *zOut = &zDestData[iOff%nDestPgsz];
! 57774:
! 57775: /* Copy the data from the source page into the destination page.
! 57776: ** Then clear the Btree layer MemPage.isInit flag. Both this module
! 57777: ** and the pager code use this trick (clearing the first byte
! 57778: ** of the page 'extra' space to invalidate the Btree layers
! 57779: ** cached parse of the page). MemPage.isInit is marked
! 57780: ** "MUST BE FIRST" for this purpose.
! 57781: */
! 57782: memcpy(zOut, zIn, nCopy);
! 57783: ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
! 57784: }
! 57785: sqlite3PagerUnref(pDestPg);
! 57786: }
! 57787:
! 57788: return rc;
! 57789: }
! 57790:
! 57791: /*
! 57792: ** If pFile is currently larger than iSize bytes, then truncate it to
! 57793: ** exactly iSize bytes. If pFile is not larger than iSize bytes, then
! 57794: ** this function is a no-op.
! 57795: **
! 57796: ** Return SQLITE_OK if everything is successful, or an SQLite error
! 57797: ** code if an error occurs.
! 57798: */
! 57799: static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
! 57800: i64 iCurrent;
! 57801: int rc = sqlite3OsFileSize(pFile, &iCurrent);
! 57802: if( rc==SQLITE_OK && iCurrent>iSize ){
! 57803: rc = sqlite3OsTruncate(pFile, iSize);
! 57804: }
! 57805: return rc;
! 57806: }
! 57807:
! 57808: /*
! 57809: ** Register this backup object with the associated source pager for
! 57810: ** callbacks when pages are changed or the cache invalidated.
! 57811: */
! 57812: static void attachBackupObject(sqlite3_backup *p){
! 57813: sqlite3_backup **pp;
! 57814: assert( sqlite3BtreeHoldsMutex(p->pSrc) );
! 57815: pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
! 57816: p->pNext = *pp;
! 57817: *pp = p;
! 57818: p->isAttached = 1;
! 57819: }
! 57820:
! 57821: /*
! 57822: ** Copy nPage pages from the source b-tree to the destination.
! 57823: */
! 57824: SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
! 57825: int rc;
! 57826: int destMode; /* Destination journal mode */
! 57827: int pgszSrc = 0; /* Source page size */
! 57828: int pgszDest = 0; /* Destination page size */
! 57829:
! 57830: sqlite3_mutex_enter(p->pSrcDb->mutex);
! 57831: sqlite3BtreeEnter(p->pSrc);
! 57832: if( p->pDestDb ){
! 57833: sqlite3_mutex_enter(p->pDestDb->mutex);
! 57834: }
! 57835:
! 57836: rc = p->rc;
! 57837: if( !isFatalError(rc) ){
! 57838: Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
! 57839: Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
! 57840: int ii; /* Iterator variable */
! 57841: int nSrcPage = -1; /* Size of source db in pages */
! 57842: int bCloseTrans = 0; /* True if src db requires unlocking */
! 57843:
! 57844: /* If the source pager is currently in a write-transaction, return
! 57845: ** SQLITE_BUSY immediately.
! 57846: */
! 57847: if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
! 57848: rc = SQLITE_BUSY;
! 57849: }else{
! 57850: rc = SQLITE_OK;
! 57851: }
! 57852:
! 57853: /* Lock the destination database, if it is not locked already. */
! 57854: if( SQLITE_OK==rc && p->bDestLocked==0
! 57855: && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
! 57856: ){
! 57857: p->bDestLocked = 1;
! 57858: sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
! 57859: }
! 57860:
! 57861: /* If there is no open read-transaction on the source database, open
! 57862: ** one now. If a transaction is opened here, then it will be closed
! 57863: ** before this function exits.
! 57864: */
! 57865: if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
! 57866: rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
! 57867: bCloseTrans = 1;
! 57868: }
! 57869:
! 57870: /* Do not allow backup if the destination database is in WAL mode
! 57871: ** and the page sizes are different between source and destination */
! 57872: pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
! 57873: pgszDest = sqlite3BtreeGetPageSize(p->pDest);
! 57874: destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
! 57875: if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
! 57876: rc = SQLITE_READONLY;
! 57877: }
! 57878:
! 57879: /* Now that there is a read-lock on the source database, query the
! 57880: ** source pager for the number of pages in the database.
! 57881: */
! 57882: nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
! 57883: assert( nSrcPage>=0 );
! 57884: for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
! 57885: const Pgno iSrcPg = p->iNext; /* Source page number */
! 57886: if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
! 57887: DbPage *pSrcPg; /* Source page object */
! 57888: rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
! 57889: if( rc==SQLITE_OK ){
! 57890: rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
! 57891: sqlite3PagerUnref(pSrcPg);
! 57892: }
! 57893: }
! 57894: p->iNext++;
! 57895: }
! 57896: if( rc==SQLITE_OK ){
! 57897: p->nPagecount = nSrcPage;
! 57898: p->nRemaining = nSrcPage+1-p->iNext;
! 57899: if( p->iNext>(Pgno)nSrcPage ){
! 57900: rc = SQLITE_DONE;
! 57901: }else if( !p->isAttached ){
! 57902: attachBackupObject(p);
! 57903: }
! 57904: }
! 57905:
! 57906: /* Update the schema version field in the destination database. This
! 57907: ** is to make sure that the schema-version really does change in
! 57908: ** the case where the source and destination databases have the
! 57909: ** same schema version.
! 57910: */
! 57911: if( rc==SQLITE_DONE ){
! 57912: rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
! 57913: if( rc==SQLITE_OK ){
! 57914: if( p->pDestDb ){
! 57915: sqlite3ResetInternalSchema(p->pDestDb, -1);
! 57916: }
! 57917: if( destMode==PAGER_JOURNALMODE_WAL ){
! 57918: rc = sqlite3BtreeSetVersion(p->pDest, 2);
! 57919: }
! 57920: }
! 57921: if( rc==SQLITE_OK ){
! 57922: int nDestTruncate;
! 57923: /* Set nDestTruncate to the final number of pages in the destination
! 57924: ** database. The complication here is that the destination page
! 57925: ** size may be different to the source page size.
! 57926: **
! 57927: ** If the source page size is smaller than the destination page size,
! 57928: ** round up. In this case the call to sqlite3OsTruncate() below will
! 57929: ** fix the size of the file. However it is important to call
! 57930: ** sqlite3PagerTruncateImage() here so that any pages in the
! 57931: ** destination file that lie beyond the nDestTruncate page mark are
! 57932: ** journalled by PagerCommitPhaseOne() before they are destroyed
! 57933: ** by the file truncation.
! 57934: */
! 57935: assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
! 57936: assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
! 57937: if( pgszSrc<pgszDest ){
! 57938: int ratio = pgszDest/pgszSrc;
! 57939: nDestTruncate = (nSrcPage+ratio-1)/ratio;
! 57940: if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
! 57941: nDestTruncate--;
! 57942: }
! 57943: }else{
! 57944: nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
! 57945: }
! 57946: sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
! 57947:
! 57948: if( pgszSrc<pgszDest ){
! 57949: /* If the source page-size is smaller than the destination page-size,
! 57950: ** two extra things may need to happen:
! 57951: **
! 57952: ** * The destination may need to be truncated, and
! 57953: **
! 57954: ** * Data stored on the pages immediately following the
! 57955: ** pending-byte page in the source database may need to be
! 57956: ** copied into the destination database.
! 57957: */
! 57958: const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
! 57959: sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
! 57960: i64 iOff;
! 57961: i64 iEnd;
! 57962:
! 57963: assert( pFile );
! 57964: assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
! 57965: nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
! 57966: && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
! 57967: ));
! 57968:
! 57969: /* This call ensures that all data required to recreate the original
! 57970: ** database has been stored in the journal for pDestPager and the
! 57971: ** journal synced to disk. So at this point we may safely modify
! 57972: ** the database file in any way, knowing that if a power failure
! 57973: ** occurs, the original database will be reconstructed from the
! 57974: ** journal file. */
! 57975: rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
! 57976:
! 57977: /* Write the extra pages and truncate the database file as required */
! 57978: iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
! 57979: for(
! 57980: iOff=PENDING_BYTE+pgszSrc;
! 57981: rc==SQLITE_OK && iOff<iEnd;
! 57982: iOff+=pgszSrc
! 57983: ){
! 57984: PgHdr *pSrcPg = 0;
! 57985: const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
! 57986: rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
! 57987: if( rc==SQLITE_OK ){
! 57988: u8 *zData = sqlite3PagerGetData(pSrcPg);
! 57989: rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
! 57990: }
! 57991: sqlite3PagerUnref(pSrcPg);
! 57992: }
! 57993: if( rc==SQLITE_OK ){
! 57994: rc = backupTruncateFile(pFile, iSize);
! 57995: }
! 57996:
! 57997: /* Sync the database file to disk. */
! 57998: if( rc==SQLITE_OK ){
! 57999: rc = sqlite3PagerSync(pDestPager);
! 58000: }
! 58001: }else{
! 58002: rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
! 58003: }
! 58004:
! 58005: /* Finish committing the transaction to the destination database. */
! 58006: if( SQLITE_OK==rc
! 58007: && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
! 58008: ){
! 58009: rc = SQLITE_DONE;
! 58010: }
! 58011: }
! 58012: }
! 58013:
! 58014: /* If bCloseTrans is true, then this function opened a read transaction
! 58015: ** on the source database. Close the read transaction here. There is
! 58016: ** no need to check the return values of the btree methods here, as
! 58017: ** "committing" a read-only transaction cannot fail.
! 58018: */
! 58019: if( bCloseTrans ){
! 58020: TESTONLY( int rc2 );
! 58021: TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
! 58022: TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
! 58023: assert( rc2==SQLITE_OK );
! 58024: }
! 58025:
! 58026: if( rc==SQLITE_IOERR_NOMEM ){
! 58027: rc = SQLITE_NOMEM;
! 58028: }
! 58029: p->rc = rc;
! 58030: }
! 58031: if( p->pDestDb ){
! 58032: sqlite3_mutex_leave(p->pDestDb->mutex);
! 58033: }
! 58034: sqlite3BtreeLeave(p->pSrc);
! 58035: sqlite3_mutex_leave(p->pSrcDb->mutex);
! 58036: return rc;
! 58037: }
! 58038:
! 58039: /*
! 58040: ** Release all resources associated with an sqlite3_backup* handle.
! 58041: */
! 58042: SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
! 58043: sqlite3_backup **pp; /* Ptr to head of pagers backup list */
! 58044: MUTEX_LOGIC( sqlite3_mutex *mutex; ) /* Mutex to protect source database */
! 58045: int rc; /* Value to return */
! 58046:
! 58047: /* Enter the mutexes */
! 58048: if( p==0 ) return SQLITE_OK;
! 58049: sqlite3_mutex_enter(p->pSrcDb->mutex);
! 58050: sqlite3BtreeEnter(p->pSrc);
! 58051: MUTEX_LOGIC( mutex = p->pSrcDb->mutex; )
! 58052: if( p->pDestDb ){
! 58053: sqlite3_mutex_enter(p->pDestDb->mutex);
! 58054: }
! 58055:
! 58056: /* Detach this backup from the source pager. */
! 58057: if( p->pDestDb ){
! 58058: p->pSrc->nBackup--;
! 58059: }
! 58060: if( p->isAttached ){
! 58061: pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
! 58062: while( *pp!=p ){
! 58063: pp = &(*pp)->pNext;
! 58064: }
! 58065: *pp = p->pNext;
! 58066: }
! 58067:
! 58068: /* If a transaction is still open on the Btree, roll it back. */
! 58069: sqlite3BtreeRollback(p->pDest);
! 58070:
! 58071: /* Set the error code of the destination database handle. */
! 58072: rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
! 58073: sqlite3Error(p->pDestDb, rc, 0);
! 58074:
! 58075: /* Exit the mutexes and free the backup context structure. */
! 58076: if( p->pDestDb ){
! 58077: sqlite3_mutex_leave(p->pDestDb->mutex);
! 58078: }
! 58079: sqlite3BtreeLeave(p->pSrc);
! 58080: if( p->pDestDb ){
! 58081: /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
! 58082: ** call to sqlite3_backup_init() and is destroyed by a call to
! 58083: ** sqlite3_backup_finish(). */
! 58084: sqlite3_free(p);
! 58085: }
! 58086: sqlite3_mutex_leave(mutex);
! 58087: return rc;
! 58088: }
! 58089:
! 58090: /*
! 58091: ** Return the number of pages still to be backed up as of the most recent
! 58092: ** call to sqlite3_backup_step().
! 58093: */
! 58094: SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
! 58095: return p->nRemaining;
! 58096: }
! 58097:
! 58098: /*
! 58099: ** Return the total number of pages in the source database as of the most
! 58100: ** recent call to sqlite3_backup_step().
! 58101: */
! 58102: SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
! 58103: return p->nPagecount;
! 58104: }
! 58105:
! 58106: /*
! 58107: ** This function is called after the contents of page iPage of the
! 58108: ** source database have been modified. If page iPage has already been
! 58109: ** copied into the destination database, then the data written to the
! 58110: ** destination is now invalidated. The destination copy of iPage needs
! 58111: ** to be updated with the new data before the backup operation is
! 58112: ** complete.
! 58113: **
! 58114: ** It is assumed that the mutex associated with the BtShared object
! 58115: ** corresponding to the source database is held when this function is
! 58116: ** called.
! 58117: */
! 58118: SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
! 58119: sqlite3_backup *p; /* Iterator variable */
! 58120: for(p=pBackup; p; p=p->pNext){
! 58121: assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
! 58122: if( !isFatalError(p->rc) && iPage<p->iNext ){
! 58123: /* The backup process p has already copied page iPage. But now it
! 58124: ** has been modified by a transaction on the source pager. Copy
! 58125: ** the new data into the backup.
! 58126: */
! 58127: int rc;
! 58128: assert( p->pDestDb );
! 58129: sqlite3_mutex_enter(p->pDestDb->mutex);
! 58130: rc = backupOnePage(p, iPage, aData);
! 58131: sqlite3_mutex_leave(p->pDestDb->mutex);
! 58132: assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
! 58133: if( rc!=SQLITE_OK ){
! 58134: p->rc = rc;
! 58135: }
! 58136: }
! 58137: }
! 58138: }
! 58139:
! 58140: /*
! 58141: ** Restart the backup process. This is called when the pager layer
! 58142: ** detects that the database has been modified by an external database
! 58143: ** connection. In this case there is no way of knowing which of the
! 58144: ** pages that have been copied into the destination database are still
! 58145: ** valid and which are not, so the entire process needs to be restarted.
! 58146: **
! 58147: ** It is assumed that the mutex associated with the BtShared object
! 58148: ** corresponding to the source database is held when this function is
! 58149: ** called.
! 58150: */
! 58151: SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
! 58152: sqlite3_backup *p; /* Iterator variable */
! 58153: for(p=pBackup; p; p=p->pNext){
! 58154: assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
! 58155: p->iNext = 1;
! 58156: }
! 58157: }
! 58158:
! 58159: #ifndef SQLITE_OMIT_VACUUM
! 58160: /*
! 58161: ** Copy the complete content of pBtFrom into pBtTo. A transaction
! 58162: ** must be active for both files.
! 58163: **
! 58164: ** The size of file pTo may be reduced by this operation. If anything
! 58165: ** goes wrong, the transaction on pTo is rolled back. If successful, the
! 58166: ** transaction is committed before returning.
! 58167: */
! 58168: SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
! 58169: int rc;
! 58170: sqlite3_file *pFd; /* File descriptor for database pTo */
! 58171: sqlite3_backup b;
! 58172: sqlite3BtreeEnter(pTo);
! 58173: sqlite3BtreeEnter(pFrom);
! 58174:
! 58175: assert( sqlite3BtreeIsInTrans(pTo) );
! 58176: pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
! 58177: if( pFd->pMethods ){
! 58178: i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
! 58179: rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
! 58180: if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
! 58181: if( rc ) goto copy_finished;
! 58182: }
! 58183:
! 58184: /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
! 58185: ** to 0. This is used by the implementations of sqlite3_backup_step()
! 58186: ** and sqlite3_backup_finish() to detect that they are being called
! 58187: ** from this function, not directly by the user.
! 58188: */
! 58189: memset(&b, 0, sizeof(b));
! 58190: b.pSrcDb = pFrom->db;
! 58191: b.pSrc = pFrom;
! 58192: b.pDest = pTo;
! 58193: b.iNext = 1;
! 58194:
! 58195: /* 0x7FFFFFFF is the hard limit for the number of pages in a database
! 58196: ** file. By passing this as the number of pages to copy to
! 58197: ** sqlite3_backup_step(), we can guarantee that the copy finishes
! 58198: ** within a single call (unless an error occurs). The assert() statement
! 58199: ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
! 58200: ** or an error code.
! 58201: */
! 58202: sqlite3_backup_step(&b, 0x7FFFFFFF);
! 58203: assert( b.rc!=SQLITE_OK );
! 58204: rc = sqlite3_backup_finish(&b);
! 58205: if( rc==SQLITE_OK ){
! 58206: pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
! 58207: }else{
! 58208: sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
! 58209: }
! 58210:
! 58211: assert( sqlite3BtreeIsInTrans(pTo)==0 );
! 58212: copy_finished:
! 58213: sqlite3BtreeLeave(pFrom);
! 58214: sqlite3BtreeLeave(pTo);
! 58215: return rc;
! 58216: }
! 58217: #endif /* SQLITE_OMIT_VACUUM */
! 58218:
! 58219: /************** End of backup.c **********************************************/
! 58220: /************** Begin file vdbemem.c *****************************************/
! 58221: /*
! 58222: ** 2004 May 26
! 58223: **
! 58224: ** The author disclaims copyright to this source code. In place of
! 58225: ** a legal notice, here is a blessing:
! 58226: **
! 58227: ** May you do good and not evil.
! 58228: ** May you find forgiveness for yourself and forgive others.
! 58229: ** May you share freely, never taking more than you give.
! 58230: **
! 58231: *************************************************************************
! 58232: **
! 58233: ** This file contains code use to manipulate "Mem" structure. A "Mem"
! 58234: ** stores a single value in the VDBE. Mem is an opaque structure visible
! 58235: ** only within the VDBE. Interface routines refer to a Mem using the
! 58236: ** name sqlite_value
! 58237: */
! 58238:
! 58239: /*
! 58240: ** If pMem is an object with a valid string representation, this routine
! 58241: ** ensures the internal encoding for the string representation is
! 58242: ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
! 58243: **
! 58244: ** If pMem is not a string object, or the encoding of the string
! 58245: ** representation is already stored using the requested encoding, then this
! 58246: ** routine is a no-op.
! 58247: **
! 58248: ** SQLITE_OK is returned if the conversion is successful (or not required).
! 58249: ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
! 58250: ** between formats.
! 58251: */
! 58252: SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
! 58253: int rc;
! 58254: assert( (pMem->flags&MEM_RowSet)==0 );
! 58255: assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
! 58256: || desiredEnc==SQLITE_UTF16BE );
! 58257: if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
! 58258: return SQLITE_OK;
! 58259: }
! 58260: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58261: #ifdef SQLITE_OMIT_UTF16
! 58262: return SQLITE_ERROR;
! 58263: #else
! 58264:
! 58265: /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
! 58266: ** then the encoding of the value may not have changed.
! 58267: */
! 58268: rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
! 58269: assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
! 58270: assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
! 58271: assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
! 58272: return rc;
! 58273: #endif
! 58274: }
! 58275:
! 58276: /*
! 58277: ** Make sure pMem->z points to a writable allocation of at least
! 58278: ** n bytes.
! 58279: **
! 58280: ** If the memory cell currently contains string or blob data
! 58281: ** and the third argument passed to this function is true, the
! 58282: ** current content of the cell is preserved. Otherwise, it may
! 58283: ** be discarded.
! 58284: **
! 58285: ** This function sets the MEM_Dyn flag and clears any xDel callback.
! 58286: ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is
! 58287: ** not set, Mem.n is zeroed.
! 58288: */
! 58289: SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
! 58290: assert( 1 >=
! 58291: ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
! 58292: (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) +
! 58293: ((pMem->flags&MEM_Ephem) ? 1 : 0) +
! 58294: ((pMem->flags&MEM_Static) ? 1 : 0)
! 58295: );
! 58296: assert( (pMem->flags&MEM_RowSet)==0 );
! 58297:
! 58298: if( n<32 ) n = 32;
! 58299: if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
! 58300: if( preserve && pMem->z==pMem->zMalloc ){
! 58301: pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
! 58302: preserve = 0;
! 58303: }else{
! 58304: sqlite3DbFree(pMem->db, pMem->zMalloc);
! 58305: pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
! 58306: }
! 58307: }
! 58308:
! 58309: if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
! 58310: memcpy(pMem->zMalloc, pMem->z, pMem->n);
! 58311: }
! 58312: if( pMem->flags&MEM_Dyn && pMem->xDel ){
! 58313: pMem->xDel((void *)(pMem->z));
! 58314: }
! 58315:
! 58316: pMem->z = pMem->zMalloc;
! 58317: if( pMem->z==0 ){
! 58318: pMem->flags = MEM_Null;
! 58319: }else{
! 58320: pMem->flags &= ~(MEM_Ephem|MEM_Static);
! 58321: }
! 58322: pMem->xDel = 0;
! 58323: return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
! 58324: }
! 58325:
! 58326: /*
! 58327: ** Make the given Mem object MEM_Dyn. In other words, make it so
! 58328: ** that any TEXT or BLOB content is stored in memory obtained from
! 58329: ** malloc(). In this way, we know that the memory is safe to be
! 58330: ** overwritten or altered.
! 58331: **
! 58332: ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
! 58333: */
! 58334: SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
! 58335: int f;
! 58336: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58337: assert( (pMem->flags&MEM_RowSet)==0 );
! 58338: ExpandBlob(pMem);
! 58339: f = pMem->flags;
! 58340: if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
! 58341: if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
! 58342: return SQLITE_NOMEM;
! 58343: }
! 58344: pMem->z[pMem->n] = 0;
! 58345: pMem->z[pMem->n+1] = 0;
! 58346: pMem->flags |= MEM_Term;
! 58347: #ifdef SQLITE_DEBUG
! 58348: pMem->pScopyFrom = 0;
! 58349: #endif
! 58350: }
! 58351:
! 58352: return SQLITE_OK;
! 58353: }
! 58354:
! 58355: /*
! 58356: ** If the given Mem* has a zero-filled tail, turn it into an ordinary
! 58357: ** blob stored in dynamically allocated space.
! 58358: */
! 58359: #ifndef SQLITE_OMIT_INCRBLOB
! 58360: SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
! 58361: if( pMem->flags & MEM_Zero ){
! 58362: int nByte;
! 58363: assert( pMem->flags&MEM_Blob );
! 58364: assert( (pMem->flags&MEM_RowSet)==0 );
! 58365: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58366:
! 58367: /* Set nByte to the number of bytes required to store the expanded blob. */
! 58368: nByte = pMem->n + pMem->u.nZero;
! 58369: if( nByte<=0 ){
! 58370: nByte = 1;
! 58371: }
! 58372: if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
! 58373: return SQLITE_NOMEM;
! 58374: }
! 58375:
! 58376: memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
! 58377: pMem->n += pMem->u.nZero;
! 58378: pMem->flags &= ~(MEM_Zero|MEM_Term);
! 58379: }
! 58380: return SQLITE_OK;
! 58381: }
! 58382: #endif
! 58383:
! 58384:
! 58385: /*
! 58386: ** Make sure the given Mem is \u0000 terminated.
! 58387: */
! 58388: SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
! 58389: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58390: if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
! 58391: return SQLITE_OK; /* Nothing to do */
! 58392: }
! 58393: if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
! 58394: return SQLITE_NOMEM;
! 58395: }
! 58396: pMem->z[pMem->n] = 0;
! 58397: pMem->z[pMem->n+1] = 0;
! 58398: pMem->flags |= MEM_Term;
! 58399: return SQLITE_OK;
! 58400: }
! 58401:
! 58402: /*
! 58403: ** Add MEM_Str to the set of representations for the given Mem. Numbers
! 58404: ** are converted using sqlite3_snprintf(). Converting a BLOB to a string
! 58405: ** is a no-op.
! 58406: **
! 58407: ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
! 58408: **
! 58409: ** A MEM_Null value will never be passed to this function. This function is
! 58410: ** used for converting values to text for returning to the user (i.e. via
! 58411: ** sqlite3_value_text()), or for ensuring that values to be used as btree
! 58412: ** keys are strings. In the former case a NULL pointer is returned the
! 58413: ** user and the later is an internal programming error.
! 58414: */
! 58415: SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, int enc){
! 58416: int rc = SQLITE_OK;
! 58417: int fg = pMem->flags;
! 58418: const int nByte = 32;
! 58419:
! 58420: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58421: assert( !(fg&MEM_Zero) );
! 58422: assert( !(fg&(MEM_Str|MEM_Blob)) );
! 58423: assert( fg&(MEM_Int|MEM_Real) );
! 58424: assert( (pMem->flags&MEM_RowSet)==0 );
! 58425: assert( EIGHT_BYTE_ALIGNMENT(pMem) );
! 58426:
! 58427:
! 58428: if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
! 58429: return SQLITE_NOMEM;
! 58430: }
! 58431:
! 58432: /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
! 58433: ** string representation of the value. Then, if the required encoding
! 58434: ** is UTF-16le or UTF-16be do a translation.
! 58435: **
! 58436: ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
! 58437: */
! 58438: if( fg & MEM_Int ){
! 58439: sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
! 58440: }else{
! 58441: assert( fg & MEM_Real );
! 58442: sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
! 58443: }
! 58444: pMem->n = sqlite3Strlen30(pMem->z);
! 58445: pMem->enc = SQLITE_UTF8;
! 58446: pMem->flags |= MEM_Str|MEM_Term;
! 58447: sqlite3VdbeChangeEncoding(pMem, enc);
! 58448: return rc;
! 58449: }
! 58450:
! 58451: /*
! 58452: ** Memory cell pMem contains the context of an aggregate function.
! 58453: ** This routine calls the finalize method for that function. The
! 58454: ** result of the aggregate is stored back into pMem.
! 58455: **
! 58456: ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
! 58457: ** otherwise.
! 58458: */
! 58459: SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
! 58460: int rc = SQLITE_OK;
! 58461: if( ALWAYS(pFunc && pFunc->xFinalize) ){
! 58462: sqlite3_context ctx;
! 58463: assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
! 58464: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58465: memset(&ctx, 0, sizeof(ctx));
! 58466: ctx.s.flags = MEM_Null;
! 58467: ctx.s.db = pMem->db;
! 58468: ctx.pMem = pMem;
! 58469: ctx.pFunc = pFunc;
! 58470: pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
! 58471: assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
! 58472: sqlite3DbFree(pMem->db, pMem->zMalloc);
! 58473: memcpy(pMem, &ctx.s, sizeof(ctx.s));
! 58474: rc = ctx.isError;
! 58475: }
! 58476: return rc;
! 58477: }
! 58478:
! 58479: /*
! 58480: ** If the memory cell contains a string value that must be freed by
! 58481: ** invoking an external callback, free it now. Calling this function
! 58482: ** does not free any Mem.zMalloc buffer.
! 58483: */
! 58484: SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
! 58485: assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
! 58486: if( p->flags&MEM_Agg ){
! 58487: sqlite3VdbeMemFinalize(p, p->u.pDef);
! 58488: assert( (p->flags & MEM_Agg)==0 );
! 58489: sqlite3VdbeMemRelease(p);
! 58490: }else if( p->flags&MEM_Dyn && p->xDel ){
! 58491: assert( (p->flags&MEM_RowSet)==0 );
! 58492: p->xDel((void *)p->z);
! 58493: p->xDel = 0;
! 58494: }else if( p->flags&MEM_RowSet ){
! 58495: sqlite3RowSetClear(p->u.pRowSet);
! 58496: }else if( p->flags&MEM_Frame ){
! 58497: sqlite3VdbeMemSetNull(p);
! 58498: }
! 58499: }
! 58500:
! 58501: /*
! 58502: ** Release any memory held by the Mem. This may leave the Mem in an
! 58503: ** inconsistent state, for example with (Mem.z==0) and
! 58504: ** (Mem.type==SQLITE_TEXT).
! 58505: */
! 58506: SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
! 58507: VdbeMemRelease(p);
! 58508: sqlite3DbFree(p->db, p->zMalloc);
! 58509: p->z = 0;
! 58510: p->zMalloc = 0;
! 58511: p->xDel = 0;
! 58512: }
! 58513:
! 58514: /*
! 58515: ** Convert a 64-bit IEEE double into a 64-bit signed integer.
! 58516: ** If the double is too large, return 0x8000000000000000.
! 58517: **
! 58518: ** Most systems appear to do this simply by assigning
! 58519: ** variables and without the extra range tests. But
! 58520: ** there are reports that windows throws an expection
! 58521: ** if the floating point value is out of range. (See ticket #2880.)
! 58522: ** Because we do not completely understand the problem, we will
! 58523: ** take the conservative approach and always do range tests
! 58524: ** before attempting the conversion.
! 58525: */
! 58526: static i64 doubleToInt64(double r){
! 58527: #ifdef SQLITE_OMIT_FLOATING_POINT
! 58528: /* When floating-point is omitted, double and int64 are the same thing */
! 58529: return r;
! 58530: #else
! 58531: /*
! 58532: ** Many compilers we encounter do not define constants for the
! 58533: ** minimum and maximum 64-bit integers, or they define them
! 58534: ** inconsistently. And many do not understand the "LL" notation.
! 58535: ** So we define our own static constants here using nothing
! 58536: ** larger than a 32-bit integer constant.
! 58537: */
! 58538: static const i64 maxInt = LARGEST_INT64;
! 58539: static const i64 minInt = SMALLEST_INT64;
! 58540:
! 58541: if( r<(double)minInt ){
! 58542: return minInt;
! 58543: }else if( r>(double)maxInt ){
! 58544: /* minInt is correct here - not maxInt. It turns out that assigning
! 58545: ** a very large positive number to an integer results in a very large
! 58546: ** negative integer. This makes no sense, but it is what x86 hardware
! 58547: ** does so for compatibility we will do the same in software. */
! 58548: return minInt;
! 58549: }else{
! 58550: return (i64)r;
! 58551: }
! 58552: #endif
! 58553: }
! 58554:
! 58555: /*
! 58556: ** Return some kind of integer value which is the best we can do
! 58557: ** at representing the value that *pMem describes as an integer.
! 58558: ** If pMem is an integer, then the value is exact. If pMem is
! 58559: ** a floating-point then the value returned is the integer part.
! 58560: ** If pMem is a string or blob, then we make an attempt to convert
! 58561: ** it into a integer and return that. If pMem represents an
! 58562: ** an SQL-NULL value, return 0.
! 58563: **
! 58564: ** If pMem represents a string value, its encoding might be changed.
! 58565: */
! 58566: SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
! 58567: int flags;
! 58568: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58569: assert( EIGHT_BYTE_ALIGNMENT(pMem) );
! 58570: flags = pMem->flags;
! 58571: if( flags & MEM_Int ){
! 58572: return pMem->u.i;
! 58573: }else if( flags & MEM_Real ){
! 58574: return doubleToInt64(pMem->r);
! 58575: }else if( flags & (MEM_Str|MEM_Blob) ){
! 58576: i64 value = 0;
! 58577: assert( pMem->z || pMem->n==0 );
! 58578: testcase( pMem->z==0 );
! 58579: sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
! 58580: return value;
! 58581: }else{
! 58582: return 0;
! 58583: }
! 58584: }
! 58585:
! 58586: /*
! 58587: ** Return the best representation of pMem that we can get into a
! 58588: ** double. If pMem is already a double or an integer, return its
! 58589: ** value. If it is a string or blob, try to convert it to a double.
! 58590: ** If it is a NULL, return 0.0.
! 58591: */
! 58592: SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
! 58593: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58594: assert( EIGHT_BYTE_ALIGNMENT(pMem) );
! 58595: if( pMem->flags & MEM_Real ){
! 58596: return pMem->r;
! 58597: }else if( pMem->flags & MEM_Int ){
! 58598: return (double)pMem->u.i;
! 58599: }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
! 58600: /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
! 58601: double val = (double)0;
! 58602: sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
! 58603: return val;
! 58604: }else{
! 58605: /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
! 58606: return (double)0;
! 58607: }
! 58608: }
! 58609:
! 58610: /*
! 58611: ** The MEM structure is already a MEM_Real. Try to also make it a
! 58612: ** MEM_Int if we can.
! 58613: */
! 58614: SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
! 58615: assert( pMem->flags & MEM_Real );
! 58616: assert( (pMem->flags & MEM_RowSet)==0 );
! 58617: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58618: assert( EIGHT_BYTE_ALIGNMENT(pMem) );
! 58619:
! 58620: pMem->u.i = doubleToInt64(pMem->r);
! 58621:
! 58622: /* Only mark the value as an integer if
! 58623: **
! 58624: ** (1) the round-trip conversion real->int->real is a no-op, and
! 58625: ** (2) The integer is neither the largest nor the smallest
! 58626: ** possible integer (ticket #3922)
! 58627: **
! 58628: ** The second and third terms in the following conditional enforces
! 58629: ** the second condition under the assumption that addition overflow causes
! 58630: ** values to wrap around. On x86 hardware, the third term is always
! 58631: ** true and could be omitted. But we leave it in because other
! 58632: ** architectures might behave differently.
! 58633: */
! 58634: if( pMem->r==(double)pMem->u.i && pMem->u.i>SMALLEST_INT64
! 58635: && ALWAYS(pMem->u.i<LARGEST_INT64) ){
! 58636: pMem->flags |= MEM_Int;
! 58637: }
! 58638: }
! 58639:
! 58640: /*
! 58641: ** Convert pMem to type integer. Invalidate any prior representations.
! 58642: */
! 58643: SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
! 58644: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58645: assert( (pMem->flags & MEM_RowSet)==0 );
! 58646: assert( EIGHT_BYTE_ALIGNMENT(pMem) );
! 58647:
! 58648: pMem->u.i = sqlite3VdbeIntValue(pMem);
! 58649: MemSetTypeFlag(pMem, MEM_Int);
! 58650: return SQLITE_OK;
! 58651: }
! 58652:
! 58653: /*
! 58654: ** Convert pMem so that it is of type MEM_Real.
! 58655: ** Invalidate any prior representations.
! 58656: */
! 58657: SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
! 58658: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58659: assert( EIGHT_BYTE_ALIGNMENT(pMem) );
! 58660:
! 58661: pMem->r = sqlite3VdbeRealValue(pMem);
! 58662: MemSetTypeFlag(pMem, MEM_Real);
! 58663: return SQLITE_OK;
! 58664: }
! 58665:
! 58666: /*
! 58667: ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
! 58668: ** Invalidate any prior representations.
! 58669: **
! 58670: ** Every effort is made to force the conversion, even if the input
! 58671: ** is a string that does not look completely like a number. Convert
! 58672: ** as much of the string as we can and ignore the rest.
! 58673: */
! 58674: SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
! 58675: if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
! 58676: assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
! 58677: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58678: if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
! 58679: MemSetTypeFlag(pMem, MEM_Int);
! 58680: }else{
! 58681: pMem->r = sqlite3VdbeRealValue(pMem);
! 58682: MemSetTypeFlag(pMem, MEM_Real);
! 58683: sqlite3VdbeIntegerAffinity(pMem);
! 58684: }
! 58685: }
! 58686: assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
! 58687: pMem->flags &= ~(MEM_Str|MEM_Blob);
! 58688: return SQLITE_OK;
! 58689: }
! 58690:
! 58691: /*
! 58692: ** Delete any previous value and set the value stored in *pMem to NULL.
! 58693: */
! 58694: SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
! 58695: if( pMem->flags & MEM_Frame ){
! 58696: VdbeFrame *pFrame = pMem->u.pFrame;
! 58697: pFrame->pParent = pFrame->v->pDelFrame;
! 58698: pFrame->v->pDelFrame = pFrame;
! 58699: }
! 58700: if( pMem->flags & MEM_RowSet ){
! 58701: sqlite3RowSetClear(pMem->u.pRowSet);
! 58702: }
! 58703: MemSetTypeFlag(pMem, MEM_Null);
! 58704: pMem->type = SQLITE_NULL;
! 58705: }
! 58706:
! 58707: /*
! 58708: ** Delete any previous value and set the value to be a BLOB of length
! 58709: ** n containing all zeros.
! 58710: */
! 58711: SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
! 58712: sqlite3VdbeMemRelease(pMem);
! 58713: pMem->flags = MEM_Blob|MEM_Zero;
! 58714: pMem->type = SQLITE_BLOB;
! 58715: pMem->n = 0;
! 58716: if( n<0 ) n = 0;
! 58717: pMem->u.nZero = n;
! 58718: pMem->enc = SQLITE_UTF8;
! 58719:
! 58720: #ifdef SQLITE_OMIT_INCRBLOB
! 58721: sqlite3VdbeMemGrow(pMem, n, 0);
! 58722: if( pMem->z ){
! 58723: pMem->n = n;
! 58724: memset(pMem->z, 0, n);
! 58725: }
! 58726: #endif
! 58727: }
! 58728:
! 58729: /*
! 58730: ** Delete any previous value and set the value stored in *pMem to val,
! 58731: ** manifest type INTEGER.
! 58732: */
! 58733: SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
! 58734: sqlite3VdbeMemRelease(pMem);
! 58735: pMem->u.i = val;
! 58736: pMem->flags = MEM_Int;
! 58737: pMem->type = SQLITE_INTEGER;
! 58738: }
! 58739:
! 58740: #ifndef SQLITE_OMIT_FLOATING_POINT
! 58741: /*
! 58742: ** Delete any previous value and set the value stored in *pMem to val,
! 58743: ** manifest type REAL.
! 58744: */
! 58745: SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
! 58746: if( sqlite3IsNaN(val) ){
! 58747: sqlite3VdbeMemSetNull(pMem);
! 58748: }else{
! 58749: sqlite3VdbeMemRelease(pMem);
! 58750: pMem->r = val;
! 58751: pMem->flags = MEM_Real;
! 58752: pMem->type = SQLITE_FLOAT;
! 58753: }
! 58754: }
! 58755: #endif
! 58756:
! 58757: /*
! 58758: ** Delete any previous value and set the value of pMem to be an
! 58759: ** empty boolean index.
! 58760: */
! 58761: SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
! 58762: sqlite3 *db = pMem->db;
! 58763: assert( db!=0 );
! 58764: assert( (pMem->flags & MEM_RowSet)==0 );
! 58765: sqlite3VdbeMemRelease(pMem);
! 58766: pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
! 58767: if( db->mallocFailed ){
! 58768: pMem->flags = MEM_Null;
! 58769: }else{
! 58770: assert( pMem->zMalloc );
! 58771: pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
! 58772: sqlite3DbMallocSize(db, pMem->zMalloc));
! 58773: assert( pMem->u.pRowSet!=0 );
! 58774: pMem->flags = MEM_RowSet;
! 58775: }
! 58776: }
! 58777:
! 58778: /*
! 58779: ** Return true if the Mem object contains a TEXT or BLOB that is
! 58780: ** too large - whose size exceeds SQLITE_MAX_LENGTH.
! 58781: */
! 58782: SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
! 58783: assert( p->db!=0 );
! 58784: if( p->flags & (MEM_Str|MEM_Blob) ){
! 58785: int n = p->n;
! 58786: if( p->flags & MEM_Zero ){
! 58787: n += p->u.nZero;
! 58788: }
! 58789: return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
! 58790: }
! 58791: return 0;
! 58792: }
! 58793:
! 58794: #ifdef SQLITE_DEBUG
! 58795: /*
! 58796: ** This routine prepares a memory cell for modication by breaking
! 58797: ** its link to a shallow copy and by marking any current shallow
! 58798: ** copies of this cell as invalid.
! 58799: **
! 58800: ** This is used for testing and debugging only - to make sure shallow
! 58801: ** copies are not misused.
! 58802: */
! 58803: SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
! 58804: int i;
! 58805: Mem *pX;
! 58806: for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
! 58807: if( pX->pScopyFrom==pMem ){
! 58808: pX->flags |= MEM_Invalid;
! 58809: pX->pScopyFrom = 0;
! 58810: }
! 58811: }
! 58812: pMem->pScopyFrom = 0;
! 58813: }
! 58814: #endif /* SQLITE_DEBUG */
! 58815:
! 58816: /*
! 58817: ** Size of struct Mem not including the Mem.zMalloc member.
! 58818: */
! 58819: #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
! 58820:
! 58821: /*
! 58822: ** Make an shallow copy of pFrom into pTo. Prior contents of
! 58823: ** pTo are freed. The pFrom->z field is not duplicated. If
! 58824: ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
! 58825: ** and flags gets srcType (either MEM_Ephem or MEM_Static).
! 58826: */
! 58827: SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
! 58828: assert( (pFrom->flags & MEM_RowSet)==0 );
! 58829: VdbeMemRelease(pTo);
! 58830: memcpy(pTo, pFrom, MEMCELLSIZE);
! 58831: pTo->xDel = 0;
! 58832: if( (pFrom->flags&MEM_Static)==0 ){
! 58833: pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
! 58834: assert( srcType==MEM_Ephem || srcType==MEM_Static );
! 58835: pTo->flags |= srcType;
! 58836: }
! 58837: }
! 58838:
! 58839: /*
! 58840: ** Make a full copy of pFrom into pTo. Prior contents of pTo are
! 58841: ** freed before the copy is made.
! 58842: */
! 58843: SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
! 58844: int rc = SQLITE_OK;
! 58845:
! 58846: assert( (pFrom->flags & MEM_RowSet)==0 );
! 58847: VdbeMemRelease(pTo);
! 58848: memcpy(pTo, pFrom, MEMCELLSIZE);
! 58849: pTo->flags &= ~MEM_Dyn;
! 58850:
! 58851: if( pTo->flags&(MEM_Str|MEM_Blob) ){
! 58852: if( 0==(pFrom->flags&MEM_Static) ){
! 58853: pTo->flags |= MEM_Ephem;
! 58854: rc = sqlite3VdbeMemMakeWriteable(pTo);
! 58855: }
! 58856: }
! 58857:
! 58858: return rc;
! 58859: }
! 58860:
! 58861: /*
! 58862: ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
! 58863: ** freed. If pFrom contains ephemeral data, a copy is made.
! 58864: **
! 58865: ** pFrom contains an SQL NULL when this routine returns.
! 58866: */
! 58867: SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
! 58868: assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
! 58869: assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
! 58870: assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
! 58871:
! 58872: sqlite3VdbeMemRelease(pTo);
! 58873: memcpy(pTo, pFrom, sizeof(Mem));
! 58874: pFrom->flags = MEM_Null;
! 58875: pFrom->xDel = 0;
! 58876: pFrom->zMalloc = 0;
! 58877: }
! 58878:
! 58879: /*
! 58880: ** Change the value of a Mem to be a string or a BLOB.
! 58881: **
! 58882: ** The memory management strategy depends on the value of the xDel
! 58883: ** parameter. If the value passed is SQLITE_TRANSIENT, then the
! 58884: ** string is copied into a (possibly existing) buffer managed by the
! 58885: ** Mem structure. Otherwise, any existing buffer is freed and the
! 58886: ** pointer copied.
! 58887: **
! 58888: ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
! 58889: ** size limit) then no memory allocation occurs. If the string can be
! 58890: ** stored without allocating memory, then it is. If a memory allocation
! 58891: ** is required to store the string, then value of pMem is unchanged. In
! 58892: ** either case, SQLITE_TOOBIG is returned.
! 58893: */
! 58894: SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
! 58895: Mem *pMem, /* Memory cell to set to string value */
! 58896: const char *z, /* String pointer */
! 58897: int n, /* Bytes in string, or negative */
! 58898: u8 enc, /* Encoding of z. 0 for BLOBs */
! 58899: void (*xDel)(void*) /* Destructor function */
! 58900: ){
! 58901: int nByte = n; /* New value for pMem->n */
! 58902: int iLimit; /* Maximum allowed string or blob size */
! 58903: u16 flags = 0; /* New value for pMem->flags */
! 58904:
! 58905: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
! 58906: assert( (pMem->flags & MEM_RowSet)==0 );
! 58907:
! 58908: /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
! 58909: if( !z ){
! 58910: sqlite3VdbeMemSetNull(pMem);
! 58911: return SQLITE_OK;
! 58912: }
! 58913:
! 58914: if( pMem->db ){
! 58915: iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
! 58916: }else{
! 58917: iLimit = SQLITE_MAX_LENGTH;
! 58918: }
! 58919: flags = (enc==0?MEM_Blob:MEM_Str);
! 58920: if( nByte<0 ){
! 58921: assert( enc!=0 );
! 58922: if( enc==SQLITE_UTF8 ){
! 58923: for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
! 58924: }else{
! 58925: for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
! 58926: }
! 58927: flags |= MEM_Term;
! 58928: }
! 58929:
! 58930: /* The following block sets the new values of Mem.z and Mem.xDel. It
! 58931: ** also sets a flag in local variable "flags" to indicate the memory
! 58932: ** management (one of MEM_Dyn or MEM_Static).
! 58933: */
! 58934: if( xDel==SQLITE_TRANSIENT ){
! 58935: int nAlloc = nByte;
! 58936: if( flags&MEM_Term ){
! 58937: nAlloc += (enc==SQLITE_UTF8?1:2);
! 58938: }
! 58939: if( nByte>iLimit ){
! 58940: return SQLITE_TOOBIG;
! 58941: }
! 58942: if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
! 58943: return SQLITE_NOMEM;
! 58944: }
! 58945: memcpy(pMem->z, z, nAlloc);
! 58946: }else if( xDel==SQLITE_DYNAMIC ){
! 58947: sqlite3VdbeMemRelease(pMem);
! 58948: pMem->zMalloc = pMem->z = (char *)z;
! 58949: pMem->xDel = 0;
! 58950: }else{
! 58951: sqlite3VdbeMemRelease(pMem);
! 58952: pMem->z = (char *)z;
! 58953: pMem->xDel = xDel;
! 58954: flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
! 58955: }
! 58956:
! 58957: pMem->n = nByte;
! 58958: pMem->flags = flags;
! 58959: pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
! 58960: pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
! 58961:
! 58962: #ifndef SQLITE_OMIT_UTF16
! 58963: if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
! 58964: return SQLITE_NOMEM;
! 58965: }
! 58966: #endif
! 58967:
! 58968: if( nByte>iLimit ){
! 58969: return SQLITE_TOOBIG;
! 58970: }
! 58971:
! 58972: return SQLITE_OK;
! 58973: }
! 58974:
! 58975: /*
! 58976: ** Compare the values contained by the two memory cells, returning
! 58977: ** negative, zero or positive if pMem1 is less than, equal to, or greater
! 58978: ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
! 58979: ** and reals) sorted numerically, followed by text ordered by the collating
! 58980: ** sequence pColl and finally blob's ordered by memcmp().
! 58981: **
! 58982: ** Two NULL values are considered equal by this function.
! 58983: */
! 58984: SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
! 58985: int rc;
! 58986: int f1, f2;
! 58987: int combined_flags;
! 58988:
! 58989: f1 = pMem1->flags;
! 58990: f2 = pMem2->flags;
! 58991: combined_flags = f1|f2;
! 58992: assert( (combined_flags & MEM_RowSet)==0 );
! 58993:
! 58994: /* If one value is NULL, it is less than the other. If both values
! 58995: ** are NULL, return 0.
! 58996: */
! 58997: if( combined_flags&MEM_Null ){
! 58998: return (f2&MEM_Null) - (f1&MEM_Null);
! 58999: }
! 59000:
! 59001: /* If one value is a number and the other is not, the number is less.
! 59002: ** If both are numbers, compare as reals if one is a real, or as integers
! 59003: ** if both values are integers.
! 59004: */
! 59005: if( combined_flags&(MEM_Int|MEM_Real) ){
! 59006: if( !(f1&(MEM_Int|MEM_Real)) ){
! 59007: return 1;
! 59008: }
! 59009: if( !(f2&(MEM_Int|MEM_Real)) ){
! 59010: return -1;
! 59011: }
! 59012: if( (f1 & f2 & MEM_Int)==0 ){
! 59013: double r1, r2;
! 59014: if( (f1&MEM_Real)==0 ){
! 59015: r1 = (double)pMem1->u.i;
! 59016: }else{
! 59017: r1 = pMem1->r;
! 59018: }
! 59019: if( (f2&MEM_Real)==0 ){
! 59020: r2 = (double)pMem2->u.i;
! 59021: }else{
! 59022: r2 = pMem2->r;
! 59023: }
! 59024: if( r1<r2 ) return -1;
! 59025: if( r1>r2 ) return 1;
! 59026: return 0;
! 59027: }else{
! 59028: assert( f1&MEM_Int );
! 59029: assert( f2&MEM_Int );
! 59030: if( pMem1->u.i < pMem2->u.i ) return -1;
! 59031: if( pMem1->u.i > pMem2->u.i ) return 1;
! 59032: return 0;
! 59033: }
! 59034: }
! 59035:
! 59036: /* If one value is a string and the other is a blob, the string is less.
! 59037: ** If both are strings, compare using the collating functions.
! 59038: */
! 59039: if( combined_flags&MEM_Str ){
! 59040: if( (f1 & MEM_Str)==0 ){
! 59041: return 1;
! 59042: }
! 59043: if( (f2 & MEM_Str)==0 ){
! 59044: return -1;
! 59045: }
! 59046:
! 59047: assert( pMem1->enc==pMem2->enc );
! 59048: assert( pMem1->enc==SQLITE_UTF8 ||
! 59049: pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
! 59050:
! 59051: /* The collation sequence must be defined at this point, even if
! 59052: ** the user deletes the collation sequence after the vdbe program is
! 59053: ** compiled (this was not always the case).
! 59054: */
! 59055: assert( !pColl || pColl->xCmp );
! 59056:
! 59057: if( pColl ){
! 59058: if( pMem1->enc==pColl->enc ){
! 59059: /* The strings are already in the correct encoding. Call the
! 59060: ** comparison function directly */
! 59061: return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
! 59062: }else{
! 59063: const void *v1, *v2;
! 59064: int n1, n2;
! 59065: Mem c1;
! 59066: Mem c2;
! 59067: memset(&c1, 0, sizeof(c1));
! 59068: memset(&c2, 0, sizeof(c2));
! 59069: sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
! 59070: sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
! 59071: v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
! 59072: n1 = v1==0 ? 0 : c1.n;
! 59073: v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
! 59074: n2 = v2==0 ? 0 : c2.n;
! 59075: rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
! 59076: sqlite3VdbeMemRelease(&c1);
! 59077: sqlite3VdbeMemRelease(&c2);
! 59078: return rc;
! 59079: }
! 59080: }
! 59081: /* If a NULL pointer was passed as the collate function, fall through
! 59082: ** to the blob case and use memcmp(). */
! 59083: }
! 59084:
! 59085: /* Both values must be blobs. Compare using memcmp(). */
! 59086: rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
! 59087: if( rc==0 ){
! 59088: rc = pMem1->n - pMem2->n;
! 59089: }
! 59090: return rc;
! 59091: }
! 59092:
! 59093: /*
! 59094: ** Move data out of a btree key or data field and into a Mem structure.
! 59095: ** The data or key is taken from the entry that pCur is currently pointing
! 59096: ** to. offset and amt determine what portion of the data or key to retrieve.
! 59097: ** key is true to get the key or false to get data. The result is written
! 59098: ** into the pMem element.
! 59099: **
! 59100: ** The pMem structure is assumed to be uninitialized. Any prior content
! 59101: ** is overwritten without being freed.
! 59102: **
! 59103: ** If this routine fails for any reason (malloc returns NULL or unable
! 59104: ** to read from the disk) then the pMem is left in an inconsistent state.
! 59105: */
! 59106: SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
! 59107: BtCursor *pCur, /* Cursor pointing at record to retrieve. */
! 59108: int offset, /* Offset from the start of data to return bytes from. */
! 59109: int amt, /* Number of bytes to return. */
! 59110: int key, /* If true, retrieve from the btree key, not data. */
! 59111: Mem *pMem /* OUT: Return data in this Mem structure. */
! 59112: ){
! 59113: char *zData; /* Data from the btree layer */
! 59114: int available = 0; /* Number of bytes available on the local btree page */
! 59115: int rc = SQLITE_OK; /* Return code */
! 59116:
! 59117: assert( sqlite3BtreeCursorIsValid(pCur) );
! 59118:
! 59119: /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
! 59120: ** that both the BtShared and database handle mutexes are held. */
! 59121: assert( (pMem->flags & MEM_RowSet)==0 );
! 59122: if( key ){
! 59123: zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
! 59124: }else{
! 59125: zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
! 59126: }
! 59127: assert( zData!=0 );
! 59128:
! 59129: if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
! 59130: sqlite3VdbeMemRelease(pMem);
! 59131: pMem->z = &zData[offset];
! 59132: pMem->flags = MEM_Blob|MEM_Ephem;
! 59133: }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
! 59134: pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
! 59135: pMem->enc = 0;
! 59136: pMem->type = SQLITE_BLOB;
! 59137: if( key ){
! 59138: rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
! 59139: }else{
! 59140: rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
! 59141: }
! 59142: pMem->z[amt] = 0;
! 59143: pMem->z[amt+1] = 0;
! 59144: if( rc!=SQLITE_OK ){
! 59145: sqlite3VdbeMemRelease(pMem);
! 59146: }
! 59147: }
! 59148: pMem->n = amt;
! 59149:
! 59150: return rc;
! 59151: }
! 59152:
! 59153: /* This function is only available internally, it is not part of the
! 59154: ** external API. It works in a similar way to sqlite3_value_text(),
! 59155: ** except the data returned is in the encoding specified by the second
! 59156: ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
! 59157: ** SQLITE_UTF8.
! 59158: **
! 59159: ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
! 59160: ** If that is the case, then the result must be aligned on an even byte
! 59161: ** boundary.
! 59162: */
! 59163: SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
! 59164: if( !pVal ) return 0;
! 59165:
! 59166: assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
! 59167: assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
! 59168: assert( (pVal->flags & MEM_RowSet)==0 );
! 59169:
! 59170: if( pVal->flags&MEM_Null ){
! 59171: return 0;
! 59172: }
! 59173: assert( (MEM_Blob>>3) == MEM_Str );
! 59174: pVal->flags |= (pVal->flags & MEM_Blob)>>3;
! 59175: ExpandBlob(pVal);
! 59176: if( pVal->flags&MEM_Str ){
! 59177: sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
! 59178: if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
! 59179: assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
! 59180: if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
! 59181: return 0;
! 59182: }
! 59183: }
! 59184: sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
! 59185: }else{
! 59186: assert( (pVal->flags&MEM_Blob)==0 );
! 59187: sqlite3VdbeMemStringify(pVal, enc);
! 59188: assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
! 59189: }
! 59190: assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
! 59191: || pVal->db->mallocFailed );
! 59192: if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
! 59193: return pVal->z;
! 59194: }else{
! 59195: return 0;
! 59196: }
! 59197: }
! 59198:
! 59199: /*
! 59200: ** Create a new sqlite3_value object.
! 59201: */
! 59202: SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
! 59203: Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
! 59204: if( p ){
! 59205: p->flags = MEM_Null;
! 59206: p->type = SQLITE_NULL;
! 59207: p->db = db;
! 59208: }
! 59209: return p;
! 59210: }
! 59211:
! 59212: /*
! 59213: ** Create a new sqlite3_value object, containing the value of pExpr.
! 59214: **
! 59215: ** This only works for very simple expressions that consist of one constant
! 59216: ** token (i.e. "5", "5.1", "'a string'"). If the expression can
! 59217: ** be converted directly into a value, then the value is allocated and
! 59218: ** a pointer written to *ppVal. The caller is responsible for deallocating
! 59219: ** the value by passing it to sqlite3ValueFree() later on. If the expression
! 59220: ** cannot be converted to a value, then *ppVal is set to NULL.
! 59221: */
! 59222: SQLITE_PRIVATE int sqlite3ValueFromExpr(
! 59223: sqlite3 *db, /* The database connection */
! 59224: Expr *pExpr, /* The expression to evaluate */
! 59225: u8 enc, /* Encoding to use */
! 59226: u8 affinity, /* Affinity to use */
! 59227: sqlite3_value **ppVal /* Write the new value here */
! 59228: ){
! 59229: int op;
! 59230: char *zVal = 0;
! 59231: sqlite3_value *pVal = 0;
! 59232: int negInt = 1;
! 59233: const char *zNeg = "";
! 59234:
! 59235: if( !pExpr ){
! 59236: *ppVal = 0;
! 59237: return SQLITE_OK;
! 59238: }
! 59239: op = pExpr->op;
! 59240:
! 59241: /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
! 59242: ** The ifdef here is to enable us to achieve 100% branch test coverage even
! 59243: ** when SQLITE_ENABLE_STAT3 is omitted.
! 59244: */
! 59245: #ifdef SQLITE_ENABLE_STAT3
! 59246: if( op==TK_REGISTER ) op = pExpr->op2;
! 59247: #else
! 59248: if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
! 59249: #endif
! 59250:
! 59251: /* Handle negative integers in a single step. This is needed in the
! 59252: ** case when the value is -9223372036854775808.
! 59253: */
! 59254: if( op==TK_UMINUS
! 59255: && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
! 59256: pExpr = pExpr->pLeft;
! 59257: op = pExpr->op;
! 59258: negInt = -1;
! 59259: zNeg = "-";
! 59260: }
! 59261:
! 59262: if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
! 59263: pVal = sqlite3ValueNew(db);
! 59264: if( pVal==0 ) goto no_mem;
! 59265: if( ExprHasProperty(pExpr, EP_IntValue) ){
! 59266: sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
! 59267: }else{
! 59268: zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
! 59269: if( zVal==0 ) goto no_mem;
! 59270: sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
! 59271: if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
! 59272: }
! 59273: if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
! 59274: sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
! 59275: }else{
! 59276: sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
! 59277: }
! 59278: if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
! 59279: if( enc!=SQLITE_UTF8 ){
! 59280: sqlite3VdbeChangeEncoding(pVal, enc);
! 59281: }
! 59282: }else if( op==TK_UMINUS ) {
! 59283: /* This branch happens for multiple negative signs. Ex: -(-5) */
! 59284: if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
! 59285: sqlite3VdbeMemNumerify(pVal);
! 59286: if( pVal->u.i==SMALLEST_INT64 ){
! 59287: pVal->flags &= MEM_Int;
! 59288: pVal->flags |= MEM_Real;
! 59289: pVal->r = (double)LARGEST_INT64;
! 59290: }else{
! 59291: pVal->u.i = -pVal->u.i;
! 59292: }
! 59293: pVal->r = -pVal->r;
! 59294: sqlite3ValueApplyAffinity(pVal, affinity, enc);
! 59295: }
! 59296: }else if( op==TK_NULL ){
! 59297: pVal = sqlite3ValueNew(db);
! 59298: if( pVal==0 ) goto no_mem;
! 59299: }
! 59300: #ifndef SQLITE_OMIT_BLOB_LITERAL
! 59301: else if( op==TK_BLOB ){
! 59302: int nVal;
! 59303: assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
! 59304: assert( pExpr->u.zToken[1]=='\'' );
! 59305: pVal = sqlite3ValueNew(db);
! 59306: if( !pVal ) goto no_mem;
! 59307: zVal = &pExpr->u.zToken[2];
! 59308: nVal = sqlite3Strlen30(zVal)-1;
! 59309: assert( zVal[nVal]=='\'' );
! 59310: sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
! 59311: 0, SQLITE_DYNAMIC);
! 59312: }
! 59313: #endif
! 59314:
! 59315: if( pVal ){
! 59316: sqlite3VdbeMemStoreType(pVal);
! 59317: }
! 59318: *ppVal = pVal;
! 59319: return SQLITE_OK;
! 59320:
! 59321: no_mem:
! 59322: db->mallocFailed = 1;
! 59323: sqlite3DbFree(db, zVal);
! 59324: sqlite3ValueFree(pVal);
! 59325: *ppVal = 0;
! 59326: return SQLITE_NOMEM;
! 59327: }
! 59328:
! 59329: /*
! 59330: ** Change the string value of an sqlite3_value object
! 59331: */
! 59332: SQLITE_PRIVATE void sqlite3ValueSetStr(
! 59333: sqlite3_value *v, /* Value to be set */
! 59334: int n, /* Length of string z */
! 59335: const void *z, /* Text of the new string */
! 59336: u8 enc, /* Encoding to use */
! 59337: void (*xDel)(void*) /* Destructor for the string */
! 59338: ){
! 59339: if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
! 59340: }
! 59341:
! 59342: /*
! 59343: ** Free an sqlite3_value object
! 59344: */
! 59345: SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
! 59346: if( !v ) return;
! 59347: sqlite3VdbeMemRelease((Mem *)v);
! 59348: sqlite3DbFree(((Mem*)v)->db, v);
! 59349: }
! 59350:
! 59351: /*
! 59352: ** Return the number of bytes in the sqlite3_value object assuming
! 59353: ** that it uses the encoding "enc"
! 59354: */
! 59355: SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
! 59356: Mem *p = (Mem*)pVal;
! 59357: if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
! 59358: if( p->flags & MEM_Zero ){
! 59359: return p->n + p->u.nZero;
! 59360: }else{
! 59361: return p->n;
! 59362: }
! 59363: }
! 59364: return 0;
! 59365: }
! 59366:
! 59367: /************** End of vdbemem.c *********************************************/
! 59368: /************** Begin file vdbeaux.c *****************************************/
! 59369: /*
! 59370: ** 2003 September 6
! 59371: **
! 59372: ** The author disclaims copyright to this source code. In place of
! 59373: ** a legal notice, here is a blessing:
! 59374: **
! 59375: ** May you do good and not evil.
! 59376: ** May you find forgiveness for yourself and forgive others.
! 59377: ** May you share freely, never taking more than you give.
! 59378: **
! 59379: *************************************************************************
! 59380: ** This file contains code used for creating, destroying, and populating
! 59381: ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
! 59382: ** to version 2.8.7, all this code was combined into the vdbe.c source file.
! 59383: ** But that file was getting too big so this subroutines were split out.
! 59384: */
! 59385:
! 59386:
! 59387:
! 59388: /*
! 59389: ** When debugging the code generator in a symbolic debugger, one can
! 59390: ** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed
! 59391: ** as they are added to the instruction stream.
! 59392: */
! 59393: #ifdef SQLITE_DEBUG
! 59394: SQLITE_PRIVATE int sqlite3VdbeAddopTrace = 0;
! 59395: #endif
! 59396:
! 59397:
! 59398: /*
! 59399: ** Create a new virtual database engine.
! 59400: */
! 59401: SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3 *db){
! 59402: Vdbe *p;
! 59403: p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
! 59404: if( p==0 ) return 0;
! 59405: p->db = db;
! 59406: if( db->pVdbe ){
! 59407: db->pVdbe->pPrev = p;
! 59408: }
! 59409: p->pNext = db->pVdbe;
! 59410: p->pPrev = 0;
! 59411: db->pVdbe = p;
! 59412: p->magic = VDBE_MAGIC_INIT;
! 59413: return p;
! 59414: }
! 59415:
! 59416: /*
! 59417: ** Remember the SQL string for a prepared statement.
! 59418: */
! 59419: SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
! 59420: assert( isPrepareV2==1 || isPrepareV2==0 );
! 59421: if( p==0 ) return;
! 59422: #ifdef SQLITE_OMIT_TRACE
! 59423: if( !isPrepareV2 ) return;
! 59424: #endif
! 59425: assert( p->zSql==0 );
! 59426: p->zSql = sqlite3DbStrNDup(p->db, z, n);
! 59427: p->isPrepareV2 = (u8)isPrepareV2;
! 59428: }
! 59429:
! 59430: /*
! 59431: ** Return the SQL associated with a prepared statement
! 59432: */
! 59433: SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
! 59434: Vdbe *p = (Vdbe *)pStmt;
! 59435: return (p && p->isPrepareV2) ? p->zSql : 0;
! 59436: }
! 59437:
! 59438: /*
! 59439: ** Swap all content between two VDBE structures.
! 59440: */
! 59441: SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
! 59442: Vdbe tmp, *pTmp;
! 59443: char *zTmp;
! 59444: tmp = *pA;
! 59445: *pA = *pB;
! 59446: *pB = tmp;
! 59447: pTmp = pA->pNext;
! 59448: pA->pNext = pB->pNext;
! 59449: pB->pNext = pTmp;
! 59450: pTmp = pA->pPrev;
! 59451: pA->pPrev = pB->pPrev;
! 59452: pB->pPrev = pTmp;
! 59453: zTmp = pA->zSql;
! 59454: pA->zSql = pB->zSql;
! 59455: pB->zSql = zTmp;
! 59456: pB->isPrepareV2 = pA->isPrepareV2;
! 59457: }
! 59458:
! 59459: #ifdef SQLITE_DEBUG
! 59460: /*
! 59461: ** Turn tracing on or off
! 59462: */
! 59463: SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
! 59464: p->trace = trace;
! 59465: }
! 59466: #endif
! 59467:
! 59468: /*
! 59469: ** Resize the Vdbe.aOp array so that it is at least one op larger than
! 59470: ** it was.
! 59471: **
! 59472: ** If an out-of-memory error occurs while resizing the array, return
! 59473: ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
! 59474: ** unchanged (this is so that any opcodes already allocated can be
! 59475: ** correctly deallocated along with the rest of the Vdbe).
! 59476: */
! 59477: static int growOpArray(Vdbe *p){
! 59478: VdbeOp *pNew;
! 59479: int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
! 59480: pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
! 59481: if( pNew ){
! 59482: p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
! 59483: p->aOp = pNew;
! 59484: }
! 59485: return (pNew ? SQLITE_OK : SQLITE_NOMEM);
! 59486: }
! 59487:
! 59488: /*
! 59489: ** Add a new instruction to the list of instructions current in the
! 59490: ** VDBE. Return the address of the new instruction.
! 59491: **
! 59492: ** Parameters:
! 59493: **
! 59494: ** p Pointer to the VDBE
! 59495: **
! 59496: ** op The opcode for this instruction
! 59497: **
! 59498: ** p1, p2, p3 Operands
! 59499: **
! 59500: ** Use the sqlite3VdbeResolveLabel() function to fix an address and
! 59501: ** the sqlite3VdbeChangeP4() function to change the value of the P4
! 59502: ** operand.
! 59503: */
! 59504: SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
! 59505: int i;
! 59506: VdbeOp *pOp;
! 59507:
! 59508: i = p->nOp;
! 59509: assert( p->magic==VDBE_MAGIC_INIT );
! 59510: assert( op>0 && op<0xff );
! 59511: if( p->nOpAlloc<=i ){
! 59512: if( growOpArray(p) ){
! 59513: return 1;
! 59514: }
! 59515: }
! 59516: p->nOp++;
! 59517: pOp = &p->aOp[i];
! 59518: pOp->opcode = (u8)op;
! 59519: pOp->p5 = 0;
! 59520: pOp->p1 = p1;
! 59521: pOp->p2 = p2;
! 59522: pOp->p3 = p3;
! 59523: pOp->p4.p = 0;
! 59524: pOp->p4type = P4_NOTUSED;
! 59525: #ifdef SQLITE_DEBUG
! 59526: pOp->zComment = 0;
! 59527: if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
! 59528: #endif
! 59529: #ifdef VDBE_PROFILE
! 59530: pOp->cycles = 0;
! 59531: pOp->cnt = 0;
! 59532: #endif
! 59533: return i;
! 59534: }
! 59535: SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
! 59536: return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
! 59537: }
! 59538: SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
! 59539: return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
! 59540: }
! 59541: SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
! 59542: return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
! 59543: }
! 59544:
! 59545:
! 59546: /*
! 59547: ** Add an opcode that includes the p4 value as a pointer.
! 59548: */
! 59549: SQLITE_PRIVATE int sqlite3VdbeAddOp4(
! 59550: Vdbe *p, /* Add the opcode to this VM */
! 59551: int op, /* The new opcode */
! 59552: int p1, /* The P1 operand */
! 59553: int p2, /* The P2 operand */
! 59554: int p3, /* The P3 operand */
! 59555: const char *zP4, /* The P4 operand */
! 59556: int p4type /* P4 operand type */
! 59557: ){
! 59558: int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
! 59559: sqlite3VdbeChangeP4(p, addr, zP4, p4type);
! 59560: return addr;
! 59561: }
! 59562:
! 59563: /*
! 59564: ** Add an OP_ParseSchema opcode. This routine is broken out from
! 59565: ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
! 59566: ** as having been used.
! 59567: **
! 59568: ** The zWhere string must have been obtained from sqlite3_malloc().
! 59569: ** This routine will take ownership of the allocated memory.
! 59570: */
! 59571: SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
! 59572: int j;
! 59573: int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
! 59574: sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
! 59575: for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
! 59576: }
! 59577:
! 59578: /*
! 59579: ** Add an opcode that includes the p4 value as an integer.
! 59580: */
! 59581: SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
! 59582: Vdbe *p, /* Add the opcode to this VM */
! 59583: int op, /* The new opcode */
! 59584: int p1, /* The P1 operand */
! 59585: int p2, /* The P2 operand */
! 59586: int p3, /* The P3 operand */
! 59587: int p4 /* The P4 operand as an integer */
! 59588: ){
! 59589: int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
! 59590: sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
! 59591: return addr;
! 59592: }
! 59593:
! 59594: /*
! 59595: ** Create a new symbolic label for an instruction that has yet to be
! 59596: ** coded. The symbolic label is really just a negative number. The
! 59597: ** label can be used as the P2 value of an operation. Later, when
! 59598: ** the label is resolved to a specific address, the VDBE will scan
! 59599: ** through its operation list and change all values of P2 which match
! 59600: ** the label into the resolved address.
! 59601: **
! 59602: ** The VDBE knows that a P2 value is a label because labels are
! 59603: ** always negative and P2 values are suppose to be non-negative.
! 59604: ** Hence, a negative P2 value is a label that has yet to be resolved.
! 59605: **
! 59606: ** Zero is returned if a malloc() fails.
! 59607: */
! 59608: SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *p){
! 59609: int i;
! 59610: i = p->nLabel++;
! 59611: assert( p->magic==VDBE_MAGIC_INIT );
! 59612: if( i>=p->nLabelAlloc ){
! 59613: int n = p->nLabelAlloc*2 + 5;
! 59614: p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
! 59615: n*sizeof(p->aLabel[0]));
! 59616: p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]);
! 59617: }
! 59618: if( p->aLabel ){
! 59619: p->aLabel[i] = -1;
! 59620: }
! 59621: return -1-i;
! 59622: }
! 59623:
! 59624: /*
! 59625: ** Resolve label "x" to be the address of the next instruction to
! 59626: ** be inserted. The parameter "x" must have been obtained from
! 59627: ** a prior call to sqlite3VdbeMakeLabel().
! 59628: */
! 59629: SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *p, int x){
! 59630: int j = -1-x;
! 59631: assert( p->magic==VDBE_MAGIC_INIT );
! 59632: assert( j>=0 && j<p->nLabel );
! 59633: if( p->aLabel ){
! 59634: p->aLabel[j] = p->nOp;
! 59635: }
! 59636: }
! 59637:
! 59638: /*
! 59639: ** Mark the VDBE as one that can only be run one time.
! 59640: */
! 59641: SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
! 59642: p->runOnlyOnce = 1;
! 59643: }
! 59644:
! 59645: #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
! 59646:
! 59647: /*
! 59648: ** The following type and function are used to iterate through all opcodes
! 59649: ** in a Vdbe main program and each of the sub-programs (triggers) it may
! 59650: ** invoke directly or indirectly. It should be used as follows:
! 59651: **
! 59652: ** Op *pOp;
! 59653: ** VdbeOpIter sIter;
! 59654: **
! 59655: ** memset(&sIter, 0, sizeof(sIter));
! 59656: ** sIter.v = v; // v is of type Vdbe*
! 59657: ** while( (pOp = opIterNext(&sIter)) ){
! 59658: ** // Do something with pOp
! 59659: ** }
! 59660: ** sqlite3DbFree(v->db, sIter.apSub);
! 59661: **
! 59662: */
! 59663: typedef struct VdbeOpIter VdbeOpIter;
! 59664: struct VdbeOpIter {
! 59665: Vdbe *v; /* Vdbe to iterate through the opcodes of */
! 59666: SubProgram **apSub; /* Array of subprograms */
! 59667: int nSub; /* Number of entries in apSub */
! 59668: int iAddr; /* Address of next instruction to return */
! 59669: int iSub; /* 0 = main program, 1 = first sub-program etc. */
! 59670: };
! 59671: static Op *opIterNext(VdbeOpIter *p){
! 59672: Vdbe *v = p->v;
! 59673: Op *pRet = 0;
! 59674: Op *aOp;
! 59675: int nOp;
! 59676:
! 59677: if( p->iSub<=p->nSub ){
! 59678:
! 59679: if( p->iSub==0 ){
! 59680: aOp = v->aOp;
! 59681: nOp = v->nOp;
! 59682: }else{
! 59683: aOp = p->apSub[p->iSub-1]->aOp;
! 59684: nOp = p->apSub[p->iSub-1]->nOp;
! 59685: }
! 59686: assert( p->iAddr<nOp );
! 59687:
! 59688: pRet = &aOp[p->iAddr];
! 59689: p->iAddr++;
! 59690: if( p->iAddr==nOp ){
! 59691: p->iSub++;
! 59692: p->iAddr = 0;
! 59693: }
! 59694:
! 59695: if( pRet->p4type==P4_SUBPROGRAM ){
! 59696: int nByte = (p->nSub+1)*sizeof(SubProgram*);
! 59697: int j;
! 59698: for(j=0; j<p->nSub; j++){
! 59699: if( p->apSub[j]==pRet->p4.pProgram ) break;
! 59700: }
! 59701: if( j==p->nSub ){
! 59702: p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
! 59703: if( !p->apSub ){
! 59704: pRet = 0;
! 59705: }else{
! 59706: p->apSub[p->nSub++] = pRet->p4.pProgram;
! 59707: }
! 59708: }
! 59709: }
! 59710: }
! 59711:
! 59712: return pRet;
! 59713: }
! 59714:
! 59715: /*
! 59716: ** Check if the program stored in the VM associated with pParse may
! 59717: ** throw an ABORT exception (causing the statement, but not entire transaction
! 59718: ** to be rolled back). This condition is true if the main program or any
! 59719: ** sub-programs contains any of the following:
! 59720: **
! 59721: ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
! 59722: ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
! 59723: ** * OP_Destroy
! 59724: ** * OP_VUpdate
! 59725: ** * OP_VRename
! 59726: ** * OP_FkCounter with P2==0 (immediate foreign key constraint)
! 59727: **
! 59728: ** Then check that the value of Parse.mayAbort is true if an
! 59729: ** ABORT may be thrown, or false otherwise. Return true if it does
! 59730: ** match, or false otherwise. This function is intended to be used as
! 59731: ** part of an assert statement in the compiler. Similar to:
! 59732: **
! 59733: ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
! 59734: */
! 59735: SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
! 59736: int hasAbort = 0;
! 59737: Op *pOp;
! 59738: VdbeOpIter sIter;
! 59739: memset(&sIter, 0, sizeof(sIter));
! 59740: sIter.v = v;
! 59741:
! 59742: while( (pOp = opIterNext(&sIter))!=0 ){
! 59743: int opcode = pOp->opcode;
! 59744: if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
! 59745: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 59746: || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1)
! 59747: #endif
! 59748: || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
! 59749: && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
! 59750: ){
! 59751: hasAbort = 1;
! 59752: break;
! 59753: }
! 59754: }
! 59755: sqlite3DbFree(v->db, sIter.apSub);
! 59756:
! 59757: /* Return true if hasAbort==mayAbort. Or if a malloc failure occured.
! 59758: ** If malloc failed, then the while() loop above may not have iterated
! 59759: ** through all opcodes and hasAbort may be set incorrectly. Return
! 59760: ** true for this case to prevent the assert() in the callers frame
! 59761: ** from failing. */
! 59762: return ( v->db->mallocFailed || hasAbort==mayAbort );
! 59763: }
! 59764: #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
! 59765:
! 59766: /*
! 59767: ** Loop through the program looking for P2 values that are negative
! 59768: ** on jump instructions. Each such value is a label. Resolve the
! 59769: ** label by setting the P2 value to its correct non-zero value.
! 59770: **
! 59771: ** This routine is called once after all opcodes have been inserted.
! 59772: **
! 59773: ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
! 59774: ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
! 59775: ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
! 59776: **
! 59777: ** The Op.opflags field is set on all opcodes.
! 59778: */
! 59779: static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
! 59780: int i;
! 59781: int nMaxArgs = *pMaxFuncArgs;
! 59782: Op *pOp;
! 59783: int *aLabel = p->aLabel;
! 59784: p->readOnly = 1;
! 59785: for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
! 59786: u8 opcode = pOp->opcode;
! 59787:
! 59788: pOp->opflags = sqlite3OpcodeProperty[opcode];
! 59789: if( opcode==OP_Function || opcode==OP_AggStep ){
! 59790: if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
! 59791: }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){
! 59792: p->readOnly = 0;
! 59793: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 59794: }else if( opcode==OP_VUpdate ){
! 59795: if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
! 59796: }else if( opcode==OP_VFilter ){
! 59797: int n;
! 59798: assert( p->nOp - i >= 3 );
! 59799: assert( pOp[-1].opcode==OP_Integer );
! 59800: n = pOp[-1].p1;
! 59801: if( n>nMaxArgs ) nMaxArgs = n;
! 59802: #endif
! 59803: }else if( opcode==OP_Next || opcode==OP_SorterNext ){
! 59804: pOp->p4.xAdvance = sqlite3BtreeNext;
! 59805: pOp->p4type = P4_ADVANCE;
! 59806: }else if( opcode==OP_Prev ){
! 59807: pOp->p4.xAdvance = sqlite3BtreePrevious;
! 59808: pOp->p4type = P4_ADVANCE;
! 59809: }
! 59810:
! 59811: if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
! 59812: assert( -1-pOp->p2<p->nLabel );
! 59813: pOp->p2 = aLabel[-1-pOp->p2];
! 59814: }
! 59815: }
! 59816: sqlite3DbFree(p->db, p->aLabel);
! 59817: p->aLabel = 0;
! 59818:
! 59819: *pMaxFuncArgs = nMaxArgs;
! 59820: }
! 59821:
! 59822: /*
! 59823: ** Return the address of the next instruction to be inserted.
! 59824: */
! 59825: SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
! 59826: assert( p->magic==VDBE_MAGIC_INIT );
! 59827: return p->nOp;
! 59828: }
! 59829:
! 59830: /*
! 59831: ** This function returns a pointer to the array of opcodes associated with
! 59832: ** the Vdbe passed as the first argument. It is the callers responsibility
! 59833: ** to arrange for the returned array to be eventually freed using the
! 59834: ** vdbeFreeOpArray() function.
! 59835: **
! 59836: ** Before returning, *pnOp is set to the number of entries in the returned
! 59837: ** array. Also, *pnMaxArg is set to the larger of its current value and
! 59838: ** the number of entries in the Vdbe.apArg[] array required to execute the
! 59839: ** returned program.
! 59840: */
! 59841: SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
! 59842: VdbeOp *aOp = p->aOp;
! 59843: assert( aOp && !p->db->mallocFailed );
! 59844:
! 59845: /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
! 59846: assert( p->btreeMask==0 );
! 59847:
! 59848: resolveP2Values(p, pnMaxArg);
! 59849: *pnOp = p->nOp;
! 59850: p->aOp = 0;
! 59851: return aOp;
! 59852: }
! 59853:
! 59854: /*
! 59855: ** Add a whole list of operations to the operation stack. Return the
! 59856: ** address of the first operation added.
! 59857: */
! 59858: SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
! 59859: int addr;
! 59860: assert( p->magic==VDBE_MAGIC_INIT );
! 59861: if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
! 59862: return 0;
! 59863: }
! 59864: addr = p->nOp;
! 59865: if( ALWAYS(nOp>0) ){
! 59866: int i;
! 59867: VdbeOpList const *pIn = aOp;
! 59868: for(i=0; i<nOp; i++, pIn++){
! 59869: int p2 = pIn->p2;
! 59870: VdbeOp *pOut = &p->aOp[i+addr];
! 59871: pOut->opcode = pIn->opcode;
! 59872: pOut->p1 = pIn->p1;
! 59873: if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){
! 59874: pOut->p2 = addr + ADDR(p2);
! 59875: }else{
! 59876: pOut->p2 = p2;
! 59877: }
! 59878: pOut->p3 = pIn->p3;
! 59879: pOut->p4type = P4_NOTUSED;
! 59880: pOut->p4.p = 0;
! 59881: pOut->p5 = 0;
! 59882: #ifdef SQLITE_DEBUG
! 59883: pOut->zComment = 0;
! 59884: if( sqlite3VdbeAddopTrace ){
! 59885: sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
! 59886: }
! 59887: #endif
! 59888: }
! 59889: p->nOp += nOp;
! 59890: }
! 59891: return addr;
! 59892: }
! 59893:
! 59894: /*
! 59895: ** Change the value of the P1 operand for a specific instruction.
! 59896: ** This routine is useful when a large program is loaded from a
! 59897: ** static array using sqlite3VdbeAddOpList but we want to make a
! 59898: ** few minor changes to the program.
! 59899: */
! 59900: SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
! 59901: assert( p!=0 );
! 59902: if( ((u32)p->nOp)>addr ){
! 59903: p->aOp[addr].p1 = val;
! 59904: }
! 59905: }
! 59906:
! 59907: /*
! 59908: ** Change the value of the P2 operand for a specific instruction.
! 59909: ** This routine is useful for setting a jump destination.
! 59910: */
! 59911: SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
! 59912: assert( p!=0 );
! 59913: if( ((u32)p->nOp)>addr ){
! 59914: p->aOp[addr].p2 = val;
! 59915: }
! 59916: }
! 59917:
! 59918: /*
! 59919: ** Change the value of the P3 operand for a specific instruction.
! 59920: */
! 59921: SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
! 59922: assert( p!=0 );
! 59923: if( ((u32)p->nOp)>addr ){
! 59924: p->aOp[addr].p3 = val;
! 59925: }
! 59926: }
! 59927:
! 59928: /*
! 59929: ** Change the value of the P5 operand for the most recently
! 59930: ** added operation.
! 59931: */
! 59932: SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
! 59933: assert( p!=0 );
! 59934: if( p->aOp ){
! 59935: assert( p->nOp>0 );
! 59936: p->aOp[p->nOp-1].p5 = val;
! 59937: }
! 59938: }
! 59939:
! 59940: /*
! 59941: ** Change the P2 operand of instruction addr so that it points to
! 59942: ** the address of the next instruction to be coded.
! 59943: */
! 59944: SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
! 59945: assert( addr>=0 || p->db->mallocFailed );
! 59946: if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
! 59947: }
! 59948:
! 59949:
! 59950: /*
! 59951: ** If the input FuncDef structure is ephemeral, then free it. If
! 59952: ** the FuncDef is not ephermal, then do nothing.
! 59953: */
! 59954: static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
! 59955: if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
! 59956: sqlite3DbFree(db, pDef);
! 59957: }
! 59958: }
! 59959:
! 59960: static void vdbeFreeOpArray(sqlite3 *, Op *, int);
! 59961:
! 59962: /*
! 59963: ** Delete a P4 value if necessary.
! 59964: */
! 59965: static void freeP4(sqlite3 *db, int p4type, void *p4){
! 59966: if( p4 ){
! 59967: assert( db );
! 59968: switch( p4type ){
! 59969: case P4_REAL:
! 59970: case P4_INT64:
! 59971: case P4_DYNAMIC:
! 59972: case P4_KEYINFO:
! 59973: case P4_INTARRAY:
! 59974: case P4_KEYINFO_HANDOFF: {
! 59975: sqlite3DbFree(db, p4);
! 59976: break;
! 59977: }
! 59978: case P4_MPRINTF: {
! 59979: if( db->pnBytesFreed==0 ) sqlite3_free(p4);
! 59980: break;
! 59981: }
! 59982: case P4_VDBEFUNC: {
! 59983: VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
! 59984: freeEphemeralFunction(db, pVdbeFunc->pFunc);
! 59985: if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
! 59986: sqlite3DbFree(db, pVdbeFunc);
! 59987: break;
! 59988: }
! 59989: case P4_FUNCDEF: {
! 59990: freeEphemeralFunction(db, (FuncDef*)p4);
! 59991: break;
! 59992: }
! 59993: case P4_MEM: {
! 59994: if( db->pnBytesFreed==0 ){
! 59995: sqlite3ValueFree((sqlite3_value*)p4);
! 59996: }else{
! 59997: Mem *p = (Mem*)p4;
! 59998: sqlite3DbFree(db, p->zMalloc);
! 59999: sqlite3DbFree(db, p);
! 60000: }
! 60001: break;
! 60002: }
! 60003: case P4_VTAB : {
! 60004: if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
! 60005: break;
! 60006: }
! 60007: }
! 60008: }
! 60009: }
! 60010:
! 60011: /*
! 60012: ** Free the space allocated for aOp and any p4 values allocated for the
! 60013: ** opcodes contained within. If aOp is not NULL it is assumed to contain
! 60014: ** nOp entries.
! 60015: */
! 60016: static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
! 60017: if( aOp ){
! 60018: Op *pOp;
! 60019: for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
! 60020: freeP4(db, pOp->p4type, pOp->p4.p);
! 60021: #ifdef SQLITE_DEBUG
! 60022: sqlite3DbFree(db, pOp->zComment);
! 60023: #endif
! 60024: }
! 60025: }
! 60026: sqlite3DbFree(db, aOp);
! 60027: }
! 60028:
! 60029: /*
! 60030: ** Link the SubProgram object passed as the second argument into the linked
! 60031: ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
! 60032: ** objects when the VM is no longer required.
! 60033: */
! 60034: SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
! 60035: p->pNext = pVdbe->pProgram;
! 60036: pVdbe->pProgram = p;
! 60037: }
! 60038:
! 60039: /*
! 60040: ** Change the opcode at addr into OP_Noop
! 60041: */
! 60042: SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
! 60043: if( p->aOp ){
! 60044: VdbeOp *pOp = &p->aOp[addr];
! 60045: sqlite3 *db = p->db;
! 60046: freeP4(db, pOp->p4type, pOp->p4.p);
! 60047: memset(pOp, 0, sizeof(pOp[0]));
! 60048: pOp->opcode = OP_Noop;
! 60049: }
! 60050: }
! 60051:
! 60052: /*
! 60053: ** Change the value of the P4 operand for a specific instruction.
! 60054: ** This routine is useful when a large program is loaded from a
! 60055: ** static array using sqlite3VdbeAddOpList but we want to make a
! 60056: ** few minor changes to the program.
! 60057: **
! 60058: ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
! 60059: ** the string is made into memory obtained from sqlite3_malloc().
! 60060: ** A value of n==0 means copy bytes of zP4 up to and including the
! 60061: ** first null byte. If n>0 then copy n+1 bytes of zP4.
! 60062: **
! 60063: ** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
! 60064: ** A copy is made of the KeyInfo structure into memory obtained from
! 60065: ** sqlite3_malloc, to be freed when the Vdbe is finalized.
! 60066: ** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
! 60067: ** stored in memory that the caller has obtained from sqlite3_malloc. The
! 60068: ** caller should not free the allocation, it will be freed when the Vdbe is
! 60069: ** finalized.
! 60070: **
! 60071: ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
! 60072: ** to a string or structure that is guaranteed to exist for the lifetime of
! 60073: ** the Vdbe. In these cases we can just copy the pointer.
! 60074: **
! 60075: ** If addr<0 then change P4 on the most recently inserted instruction.
! 60076: */
! 60077: SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
! 60078: Op *pOp;
! 60079: sqlite3 *db;
! 60080: assert( p!=0 );
! 60081: db = p->db;
! 60082: assert( p->magic==VDBE_MAGIC_INIT );
! 60083: if( p->aOp==0 || db->mallocFailed ){
! 60084: if ( n!=P4_KEYINFO && n!=P4_VTAB ) {
! 60085: freeP4(db, n, (void*)*(char**)&zP4);
! 60086: }
! 60087: return;
! 60088: }
! 60089: assert( p->nOp>0 );
! 60090: assert( addr<p->nOp );
! 60091: if( addr<0 ){
! 60092: addr = p->nOp - 1;
! 60093: }
! 60094: pOp = &p->aOp[addr];
! 60095: freeP4(db, pOp->p4type, pOp->p4.p);
! 60096: pOp->p4.p = 0;
! 60097: if( n==P4_INT32 ){
! 60098: /* Note: this cast is safe, because the origin data point was an int
! 60099: ** that was cast to a (const char *). */
! 60100: pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
! 60101: pOp->p4type = P4_INT32;
! 60102: }else if( zP4==0 ){
! 60103: pOp->p4.p = 0;
! 60104: pOp->p4type = P4_NOTUSED;
! 60105: }else if( n==P4_KEYINFO ){
! 60106: KeyInfo *pKeyInfo;
! 60107: int nField, nByte;
! 60108:
! 60109: nField = ((KeyInfo*)zP4)->nField;
! 60110: nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
! 60111: pKeyInfo = sqlite3DbMallocRaw(0, nByte);
! 60112: pOp->p4.pKeyInfo = pKeyInfo;
! 60113: if( pKeyInfo ){
! 60114: u8 *aSortOrder;
! 60115: memcpy((char*)pKeyInfo, zP4, nByte - nField);
! 60116: aSortOrder = pKeyInfo->aSortOrder;
! 60117: if( aSortOrder ){
! 60118: pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
! 60119: memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
! 60120: }
! 60121: pOp->p4type = P4_KEYINFO;
! 60122: }else{
! 60123: p->db->mallocFailed = 1;
! 60124: pOp->p4type = P4_NOTUSED;
! 60125: }
! 60126: }else if( n==P4_KEYINFO_HANDOFF ){
! 60127: pOp->p4.p = (void*)zP4;
! 60128: pOp->p4type = P4_KEYINFO;
! 60129: }else if( n==P4_VTAB ){
! 60130: pOp->p4.p = (void*)zP4;
! 60131: pOp->p4type = P4_VTAB;
! 60132: sqlite3VtabLock((VTable *)zP4);
! 60133: assert( ((VTable *)zP4)->db==p->db );
! 60134: }else if( n<0 ){
! 60135: pOp->p4.p = (void*)zP4;
! 60136: pOp->p4type = (signed char)n;
! 60137: }else{
! 60138: if( n==0 ) n = sqlite3Strlen30(zP4);
! 60139: pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
! 60140: pOp->p4type = P4_DYNAMIC;
! 60141: }
! 60142: }
! 60143:
! 60144: #ifndef NDEBUG
! 60145: /*
! 60146: ** Change the comment on the the most recently coded instruction. Or
! 60147: ** insert a No-op and add the comment to that new instruction. This
! 60148: ** makes the code easier to read during debugging. None of this happens
! 60149: ** in a production build.
! 60150: */
! 60151: static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
! 60152: assert( p->nOp>0 || p->aOp==0 );
! 60153: assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
! 60154: if( p->nOp ){
! 60155: assert( p->aOp );
! 60156: sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
! 60157: p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
! 60158: }
! 60159: }
! 60160: SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
! 60161: va_list ap;
! 60162: if( p ){
! 60163: va_start(ap, zFormat);
! 60164: vdbeVComment(p, zFormat, ap);
! 60165: va_end(ap);
! 60166: }
! 60167: }
! 60168: SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
! 60169: va_list ap;
! 60170: if( p ){
! 60171: sqlite3VdbeAddOp0(p, OP_Noop);
! 60172: va_start(ap, zFormat);
! 60173: vdbeVComment(p, zFormat, ap);
! 60174: va_end(ap);
! 60175: }
! 60176: }
! 60177: #endif /* NDEBUG */
! 60178:
! 60179: /*
! 60180: ** Return the opcode for a given address. If the address is -1, then
! 60181: ** return the most recently inserted opcode.
! 60182: **
! 60183: ** If a memory allocation error has occurred prior to the calling of this
! 60184: ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
! 60185: ** is readable but not writable, though it is cast to a writable value.
! 60186: ** The return of a dummy opcode allows the call to continue functioning
! 60187: ** after a OOM fault without having to check to see if the return from
! 60188: ** this routine is a valid pointer. But because the dummy.opcode is 0,
! 60189: ** dummy will never be written to. This is verified by code inspection and
! 60190: ** by running with Valgrind.
! 60191: **
! 60192: ** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called
! 60193: ** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE,
! 60194: ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
! 60195: ** a new VDBE is created. So we are free to set addr to p->nOp-1 without
! 60196: ** having to double-check to make sure that the result is non-negative. But
! 60197: ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
! 60198: ** check the value of p->nOp-1 before continuing.
! 60199: */
! 60200: SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
! 60201: /* C89 specifies that the constant "dummy" will be initialized to all
! 60202: ** zeros, which is correct. MSVC generates a warning, nevertheless. */
! 60203: static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
! 60204: assert( p->magic==VDBE_MAGIC_INIT );
! 60205: if( addr<0 ){
! 60206: #ifdef SQLITE_OMIT_TRACE
! 60207: if( p->nOp==0 ) return (VdbeOp*)&dummy;
! 60208: #endif
! 60209: addr = p->nOp - 1;
! 60210: }
! 60211: assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
! 60212: if( p->db->mallocFailed ){
! 60213: return (VdbeOp*)&dummy;
! 60214: }else{
! 60215: return &p->aOp[addr];
! 60216: }
! 60217: }
! 60218:
! 60219: #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
! 60220: || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
! 60221: /*
! 60222: ** Compute a string that describes the P4 parameter for an opcode.
! 60223: ** Use zTemp for any required temporary buffer space.
! 60224: */
! 60225: static char *displayP4(Op *pOp, char *zTemp, int nTemp){
! 60226: char *zP4 = zTemp;
! 60227: assert( nTemp>=20 );
! 60228: switch( pOp->p4type ){
! 60229: case P4_KEYINFO_STATIC:
! 60230: case P4_KEYINFO: {
! 60231: int i, j;
! 60232: KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
! 60233: sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
! 60234: i = sqlite3Strlen30(zTemp);
! 60235: for(j=0; j<pKeyInfo->nField; j++){
! 60236: CollSeq *pColl = pKeyInfo->aColl[j];
! 60237: if( pColl ){
! 60238: int n = sqlite3Strlen30(pColl->zName);
! 60239: if( i+n>nTemp-6 ){
! 60240: memcpy(&zTemp[i],",...",4);
! 60241: break;
! 60242: }
! 60243: zTemp[i++] = ',';
! 60244: if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
! 60245: zTemp[i++] = '-';
! 60246: }
! 60247: memcpy(&zTemp[i], pColl->zName,n+1);
! 60248: i += n;
! 60249: }else if( i+4<nTemp-6 ){
! 60250: memcpy(&zTemp[i],",nil",4);
! 60251: i += 4;
! 60252: }
! 60253: }
! 60254: zTemp[i++] = ')';
! 60255: zTemp[i] = 0;
! 60256: assert( i<nTemp );
! 60257: break;
! 60258: }
! 60259: case P4_COLLSEQ: {
! 60260: CollSeq *pColl = pOp->p4.pColl;
! 60261: sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
! 60262: break;
! 60263: }
! 60264: case P4_FUNCDEF: {
! 60265: FuncDef *pDef = pOp->p4.pFunc;
! 60266: sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
! 60267: break;
! 60268: }
! 60269: case P4_INT64: {
! 60270: sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
! 60271: break;
! 60272: }
! 60273: case P4_INT32: {
! 60274: sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
! 60275: break;
! 60276: }
! 60277: case P4_REAL: {
! 60278: sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
! 60279: break;
! 60280: }
! 60281: case P4_MEM: {
! 60282: Mem *pMem = pOp->p4.pMem;
! 60283: if( pMem->flags & MEM_Str ){
! 60284: zP4 = pMem->z;
! 60285: }else if( pMem->flags & MEM_Int ){
! 60286: sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
! 60287: }else if( pMem->flags & MEM_Real ){
! 60288: sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
! 60289: }else if( pMem->flags & MEM_Null ){
! 60290: sqlite3_snprintf(nTemp, zTemp, "NULL");
! 60291: }else{
! 60292: assert( pMem->flags & MEM_Blob );
! 60293: zP4 = "(blob)";
! 60294: }
! 60295: break;
! 60296: }
! 60297: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 60298: case P4_VTAB: {
! 60299: sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
! 60300: sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
! 60301: break;
! 60302: }
! 60303: #endif
! 60304: case P4_INTARRAY: {
! 60305: sqlite3_snprintf(nTemp, zTemp, "intarray");
! 60306: break;
! 60307: }
! 60308: case P4_SUBPROGRAM: {
! 60309: sqlite3_snprintf(nTemp, zTemp, "program");
! 60310: break;
! 60311: }
! 60312: case P4_ADVANCE: {
! 60313: zTemp[0] = 0;
! 60314: break;
! 60315: }
! 60316: default: {
! 60317: zP4 = pOp->p4.z;
! 60318: if( zP4==0 ){
! 60319: zP4 = zTemp;
! 60320: zTemp[0] = 0;
! 60321: }
! 60322: }
! 60323: }
! 60324: assert( zP4!=0 );
! 60325: return zP4;
! 60326: }
! 60327: #endif
! 60328:
! 60329: /*
! 60330: ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
! 60331: **
! 60332: ** The prepared statements need to know in advance the complete set of
! 60333: ** attached databases that will be use. A mask of these databases
! 60334: ** is maintained in p->btreeMask. The p->lockMask value is the subset of
! 60335: ** p->btreeMask of databases that will require a lock.
! 60336: */
! 60337: SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
! 60338: assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
! 60339: assert( i<(int)sizeof(p->btreeMask)*8 );
! 60340: p->btreeMask |= ((yDbMask)1)<<i;
! 60341: if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
! 60342: p->lockMask |= ((yDbMask)1)<<i;
! 60343: }
! 60344: }
! 60345:
! 60346: #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
! 60347: /*
! 60348: ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
! 60349: ** this routine obtains the mutex associated with each BtShared structure
! 60350: ** that may be accessed by the VM passed as an argument. In doing so it also
! 60351: ** sets the BtShared.db member of each of the BtShared structures, ensuring
! 60352: ** that the correct busy-handler callback is invoked if required.
! 60353: **
! 60354: ** If SQLite is not threadsafe but does support shared-cache mode, then
! 60355: ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
! 60356: ** of all of BtShared structures accessible via the database handle
! 60357: ** associated with the VM.
! 60358: **
! 60359: ** If SQLite is not threadsafe and does not support shared-cache mode, this
! 60360: ** function is a no-op.
! 60361: **
! 60362: ** The p->btreeMask field is a bitmask of all btrees that the prepared
! 60363: ** statement p will ever use. Let N be the number of bits in p->btreeMask
! 60364: ** corresponding to btrees that use shared cache. Then the runtime of
! 60365: ** this routine is N*N. But as N is rarely more than 1, this should not
! 60366: ** be a problem.
! 60367: */
! 60368: SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
! 60369: int i;
! 60370: yDbMask mask;
! 60371: sqlite3 *db;
! 60372: Db *aDb;
! 60373: int nDb;
! 60374: if( p->lockMask==0 ) return; /* The common case */
! 60375: db = p->db;
! 60376: aDb = db->aDb;
! 60377: nDb = db->nDb;
! 60378: for(i=0, mask=1; i<nDb; i++, mask += mask){
! 60379: if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
! 60380: sqlite3BtreeEnter(aDb[i].pBt);
! 60381: }
! 60382: }
! 60383: }
! 60384: #endif
! 60385:
! 60386: #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
! 60387: /*
! 60388: ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
! 60389: */
! 60390: SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
! 60391: int i;
! 60392: yDbMask mask;
! 60393: sqlite3 *db;
! 60394: Db *aDb;
! 60395: int nDb;
! 60396: if( p->lockMask==0 ) return; /* The common case */
! 60397: db = p->db;
! 60398: aDb = db->aDb;
! 60399: nDb = db->nDb;
! 60400: for(i=0, mask=1; i<nDb; i++, mask += mask){
! 60401: if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
! 60402: sqlite3BtreeLeave(aDb[i].pBt);
! 60403: }
! 60404: }
! 60405: }
! 60406: #endif
! 60407:
! 60408: #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
! 60409: /*
! 60410: ** Print a single opcode. This routine is used for debugging only.
! 60411: */
! 60412: SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
! 60413: char *zP4;
! 60414: char zPtr[50];
! 60415: static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
! 60416: if( pOut==0 ) pOut = stdout;
! 60417: zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
! 60418: fprintf(pOut, zFormat1, pc,
! 60419: sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
! 60420: #ifdef SQLITE_DEBUG
! 60421: pOp->zComment ? pOp->zComment : ""
! 60422: #else
! 60423: ""
! 60424: #endif
! 60425: );
! 60426: fflush(pOut);
! 60427: }
! 60428: #endif
! 60429:
! 60430: /*
! 60431: ** Release an array of N Mem elements
! 60432: */
! 60433: static void releaseMemArray(Mem *p, int N){
! 60434: if( p && N ){
! 60435: Mem *pEnd;
! 60436: sqlite3 *db = p->db;
! 60437: u8 malloc_failed = db->mallocFailed;
! 60438: if( db->pnBytesFreed ){
! 60439: for(pEnd=&p[N]; p<pEnd; p++){
! 60440: sqlite3DbFree(db, p->zMalloc);
! 60441: }
! 60442: return;
! 60443: }
! 60444: for(pEnd=&p[N]; p<pEnd; p++){
! 60445: assert( (&p[1])==pEnd || p[0].db==p[1].db );
! 60446:
! 60447: /* This block is really an inlined version of sqlite3VdbeMemRelease()
! 60448: ** that takes advantage of the fact that the memory cell value is
! 60449: ** being set to NULL after releasing any dynamic resources.
! 60450: **
! 60451: ** The justification for duplicating code is that according to
! 60452: ** callgrind, this causes a certain test case to hit the CPU 4.7
! 60453: ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
! 60454: ** sqlite3MemRelease() were called from here. With -O2, this jumps
! 60455: ** to 6.6 percent. The test case is inserting 1000 rows into a table
! 60456: ** with no indexes using a single prepared INSERT statement, bind()
! 60457: ** and reset(). Inserts are grouped into a transaction.
! 60458: */
! 60459: if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
! 60460: sqlite3VdbeMemRelease(p);
! 60461: }else if( p->zMalloc ){
! 60462: sqlite3DbFree(db, p->zMalloc);
! 60463: p->zMalloc = 0;
! 60464: }
! 60465:
! 60466: p->flags = MEM_Invalid;
! 60467: }
! 60468: db->mallocFailed = malloc_failed;
! 60469: }
! 60470: }
! 60471:
! 60472: /*
! 60473: ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
! 60474: ** allocated by the OP_Program opcode in sqlite3VdbeExec().
! 60475: */
! 60476: SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
! 60477: int i;
! 60478: Mem *aMem = VdbeFrameMem(p);
! 60479: VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
! 60480: for(i=0; i<p->nChildCsr; i++){
! 60481: sqlite3VdbeFreeCursor(p->v, apCsr[i]);
! 60482: }
! 60483: releaseMemArray(aMem, p->nChildMem);
! 60484: sqlite3DbFree(p->v->db, p);
! 60485: }
! 60486:
! 60487: #ifndef SQLITE_OMIT_EXPLAIN
! 60488: /*
! 60489: ** Give a listing of the program in the virtual machine.
! 60490: **
! 60491: ** The interface is the same as sqlite3VdbeExec(). But instead of
! 60492: ** running the code, it invokes the callback once for each instruction.
! 60493: ** This feature is used to implement "EXPLAIN".
! 60494: **
! 60495: ** When p->explain==1, each instruction is listed. When
! 60496: ** p->explain==2, only OP_Explain instructions are listed and these
! 60497: ** are shown in a different format. p->explain==2 is used to implement
! 60498: ** EXPLAIN QUERY PLAN.
! 60499: **
! 60500: ** When p->explain==1, first the main program is listed, then each of
! 60501: ** the trigger subprograms are listed one by one.
! 60502: */
! 60503: SQLITE_PRIVATE int sqlite3VdbeList(
! 60504: Vdbe *p /* The VDBE */
! 60505: ){
! 60506: int nRow; /* Stop when row count reaches this */
! 60507: int nSub = 0; /* Number of sub-vdbes seen so far */
! 60508: SubProgram **apSub = 0; /* Array of sub-vdbes */
! 60509: Mem *pSub = 0; /* Memory cell hold array of subprogs */
! 60510: sqlite3 *db = p->db; /* The database connection */
! 60511: int i; /* Loop counter */
! 60512: int rc = SQLITE_OK; /* Return code */
! 60513: Mem *pMem = &p->aMem[1]; /* First Mem of result set */
! 60514:
! 60515: assert( p->explain );
! 60516: assert( p->magic==VDBE_MAGIC_RUN );
! 60517: assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
! 60518:
! 60519: /* Even though this opcode does not use dynamic strings for
! 60520: ** the result, result columns may become dynamic if the user calls
! 60521: ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
! 60522: */
! 60523: releaseMemArray(pMem, 8);
! 60524: p->pResultSet = 0;
! 60525:
! 60526: if( p->rc==SQLITE_NOMEM ){
! 60527: /* This happens if a malloc() inside a call to sqlite3_column_text() or
! 60528: ** sqlite3_column_text16() failed. */
! 60529: db->mallocFailed = 1;
! 60530: return SQLITE_ERROR;
! 60531: }
! 60532:
! 60533: /* When the number of output rows reaches nRow, that means the
! 60534: ** listing has finished and sqlite3_step() should return SQLITE_DONE.
! 60535: ** nRow is the sum of the number of rows in the main program, plus
! 60536: ** the sum of the number of rows in all trigger subprograms encountered
! 60537: ** so far. The nRow value will increase as new trigger subprograms are
! 60538: ** encountered, but p->pc will eventually catch up to nRow.
! 60539: */
! 60540: nRow = p->nOp;
! 60541: if( p->explain==1 ){
! 60542: /* The first 8 memory cells are used for the result set. So we will
! 60543: ** commandeer the 9th cell to use as storage for an array of pointers
! 60544: ** to trigger subprograms. The VDBE is guaranteed to have at least 9
! 60545: ** cells. */
! 60546: assert( p->nMem>9 );
! 60547: pSub = &p->aMem[9];
! 60548: if( pSub->flags&MEM_Blob ){
! 60549: /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
! 60550: ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
! 60551: nSub = pSub->n/sizeof(Vdbe*);
! 60552: apSub = (SubProgram **)pSub->z;
! 60553: }
! 60554: for(i=0; i<nSub; i++){
! 60555: nRow += apSub[i]->nOp;
! 60556: }
! 60557: }
! 60558:
! 60559: do{
! 60560: i = p->pc++;
! 60561: }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
! 60562: if( i>=nRow ){
! 60563: p->rc = SQLITE_OK;
! 60564: rc = SQLITE_DONE;
! 60565: }else if( db->u1.isInterrupted ){
! 60566: p->rc = SQLITE_INTERRUPT;
! 60567: rc = SQLITE_ERROR;
! 60568: sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
! 60569: }else{
! 60570: char *z;
! 60571: Op *pOp;
! 60572: if( i<p->nOp ){
! 60573: /* The output line number is small enough that we are still in the
! 60574: ** main program. */
! 60575: pOp = &p->aOp[i];
! 60576: }else{
! 60577: /* We are currently listing subprograms. Figure out which one and
! 60578: ** pick up the appropriate opcode. */
! 60579: int j;
! 60580: i -= p->nOp;
! 60581: for(j=0; i>=apSub[j]->nOp; j++){
! 60582: i -= apSub[j]->nOp;
! 60583: }
! 60584: pOp = &apSub[j]->aOp[i];
! 60585: }
! 60586: if( p->explain==1 ){
! 60587: pMem->flags = MEM_Int;
! 60588: pMem->type = SQLITE_INTEGER;
! 60589: pMem->u.i = i; /* Program counter */
! 60590: pMem++;
! 60591:
! 60592: pMem->flags = MEM_Static|MEM_Str|MEM_Term;
! 60593: pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
! 60594: assert( pMem->z!=0 );
! 60595: pMem->n = sqlite3Strlen30(pMem->z);
! 60596: pMem->type = SQLITE_TEXT;
! 60597: pMem->enc = SQLITE_UTF8;
! 60598: pMem++;
! 60599:
! 60600: /* When an OP_Program opcode is encounter (the only opcode that has
! 60601: ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
! 60602: ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
! 60603: ** has not already been seen.
! 60604: */
! 60605: if( pOp->p4type==P4_SUBPROGRAM ){
! 60606: int nByte = (nSub+1)*sizeof(SubProgram*);
! 60607: int j;
! 60608: for(j=0; j<nSub; j++){
! 60609: if( apSub[j]==pOp->p4.pProgram ) break;
! 60610: }
! 60611: if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){
! 60612: apSub = (SubProgram **)pSub->z;
! 60613: apSub[nSub++] = pOp->p4.pProgram;
! 60614: pSub->flags |= MEM_Blob;
! 60615: pSub->n = nSub*sizeof(SubProgram*);
! 60616: }
! 60617: }
! 60618: }
! 60619:
! 60620: pMem->flags = MEM_Int;
! 60621: pMem->u.i = pOp->p1; /* P1 */
! 60622: pMem->type = SQLITE_INTEGER;
! 60623: pMem++;
! 60624:
! 60625: pMem->flags = MEM_Int;
! 60626: pMem->u.i = pOp->p2; /* P2 */
! 60627: pMem->type = SQLITE_INTEGER;
! 60628: pMem++;
! 60629:
! 60630: pMem->flags = MEM_Int;
! 60631: pMem->u.i = pOp->p3; /* P3 */
! 60632: pMem->type = SQLITE_INTEGER;
! 60633: pMem++;
! 60634:
! 60635: if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
! 60636: assert( p->db->mallocFailed );
! 60637: return SQLITE_ERROR;
! 60638: }
! 60639: pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
! 60640: z = displayP4(pOp, pMem->z, 32);
! 60641: if( z!=pMem->z ){
! 60642: sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
! 60643: }else{
! 60644: assert( pMem->z!=0 );
! 60645: pMem->n = sqlite3Strlen30(pMem->z);
! 60646: pMem->enc = SQLITE_UTF8;
! 60647: }
! 60648: pMem->type = SQLITE_TEXT;
! 60649: pMem++;
! 60650:
! 60651: if( p->explain==1 ){
! 60652: if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
! 60653: assert( p->db->mallocFailed );
! 60654: return SQLITE_ERROR;
! 60655: }
! 60656: pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
! 60657: pMem->n = 2;
! 60658: sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
! 60659: pMem->type = SQLITE_TEXT;
! 60660: pMem->enc = SQLITE_UTF8;
! 60661: pMem++;
! 60662:
! 60663: #ifdef SQLITE_DEBUG
! 60664: if( pOp->zComment ){
! 60665: pMem->flags = MEM_Str|MEM_Term;
! 60666: pMem->z = pOp->zComment;
! 60667: pMem->n = sqlite3Strlen30(pMem->z);
! 60668: pMem->enc = SQLITE_UTF8;
! 60669: pMem->type = SQLITE_TEXT;
! 60670: }else
! 60671: #endif
! 60672: {
! 60673: pMem->flags = MEM_Null; /* Comment */
! 60674: pMem->type = SQLITE_NULL;
! 60675: }
! 60676: }
! 60677:
! 60678: p->nResColumn = 8 - 4*(p->explain-1);
! 60679: p->pResultSet = &p->aMem[1];
! 60680: p->rc = SQLITE_OK;
! 60681: rc = SQLITE_ROW;
! 60682: }
! 60683: return rc;
! 60684: }
! 60685: #endif /* SQLITE_OMIT_EXPLAIN */
! 60686:
! 60687: #ifdef SQLITE_DEBUG
! 60688: /*
! 60689: ** Print the SQL that was used to generate a VDBE program.
! 60690: */
! 60691: SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
! 60692: int nOp = p->nOp;
! 60693: VdbeOp *pOp;
! 60694: if( nOp<1 ) return;
! 60695: pOp = &p->aOp[0];
! 60696: if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
! 60697: const char *z = pOp->p4.z;
! 60698: while( sqlite3Isspace(*z) ) z++;
! 60699: printf("SQL: [%s]\n", z);
! 60700: }
! 60701: }
! 60702: #endif
! 60703:
! 60704: #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
! 60705: /*
! 60706: ** Print an IOTRACE message showing SQL content.
! 60707: */
! 60708: SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
! 60709: int nOp = p->nOp;
! 60710: VdbeOp *pOp;
! 60711: if( sqlite3IoTrace==0 ) return;
! 60712: if( nOp<1 ) return;
! 60713: pOp = &p->aOp[0];
! 60714: if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
! 60715: int i, j;
! 60716: char z[1000];
! 60717: sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
! 60718: for(i=0; sqlite3Isspace(z[i]); i++){}
! 60719: for(j=0; z[i]; i++){
! 60720: if( sqlite3Isspace(z[i]) ){
! 60721: if( z[i-1]!=' ' ){
! 60722: z[j++] = ' ';
! 60723: }
! 60724: }else{
! 60725: z[j++] = z[i];
! 60726: }
! 60727: }
! 60728: z[j] = 0;
! 60729: sqlite3IoTrace("SQL %s\n", z);
! 60730: }
! 60731: }
! 60732: #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
! 60733:
! 60734: /*
! 60735: ** Allocate space from a fixed size buffer and return a pointer to
! 60736: ** that space. If insufficient space is available, return NULL.
! 60737: **
! 60738: ** The pBuf parameter is the initial value of a pointer which will
! 60739: ** receive the new memory. pBuf is normally NULL. If pBuf is not
! 60740: ** NULL, it means that memory space has already been allocated and that
! 60741: ** this routine should not allocate any new memory. When pBuf is not
! 60742: ** NULL simply return pBuf. Only allocate new memory space when pBuf
! 60743: ** is NULL.
! 60744: **
! 60745: ** nByte is the number of bytes of space needed.
! 60746: **
! 60747: ** *ppFrom points to available space and pEnd points to the end of the
! 60748: ** available space. When space is allocated, *ppFrom is advanced past
! 60749: ** the end of the allocated space.
! 60750: **
! 60751: ** *pnByte is a counter of the number of bytes of space that have failed
! 60752: ** to allocate. If there is insufficient space in *ppFrom to satisfy the
! 60753: ** request, then increment *pnByte by the amount of the request.
! 60754: */
! 60755: static void *allocSpace(
! 60756: void *pBuf, /* Where return pointer will be stored */
! 60757: int nByte, /* Number of bytes to allocate */
! 60758: u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */
! 60759: u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */
! 60760: int *pnByte /* If allocation cannot be made, increment *pnByte */
! 60761: ){
! 60762: assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
! 60763: if( pBuf ) return pBuf;
! 60764: nByte = ROUND8(nByte);
! 60765: if( &(*ppFrom)[nByte] <= pEnd ){
! 60766: pBuf = (void*)*ppFrom;
! 60767: *ppFrom += nByte;
! 60768: }else{
! 60769: *pnByte += nByte;
! 60770: }
! 60771: return pBuf;
! 60772: }
! 60773:
! 60774: /*
! 60775: ** Rewind the VDBE back to the beginning in preparation for
! 60776: ** running it.
! 60777: */
! 60778: SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
! 60779: #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
! 60780: int i;
! 60781: #endif
! 60782: assert( p!=0 );
! 60783: assert( p->magic==VDBE_MAGIC_INIT );
! 60784:
! 60785: /* There should be at least one opcode.
! 60786: */
! 60787: assert( p->nOp>0 );
! 60788:
! 60789: /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
! 60790: p->magic = VDBE_MAGIC_RUN;
! 60791:
! 60792: #ifdef SQLITE_DEBUG
! 60793: for(i=1; i<p->nMem; i++){
! 60794: assert( p->aMem[i].db==p->db );
! 60795: }
! 60796: #endif
! 60797: p->pc = -1;
! 60798: p->rc = SQLITE_OK;
! 60799: p->errorAction = OE_Abort;
! 60800: p->magic = VDBE_MAGIC_RUN;
! 60801: p->nChange = 0;
! 60802: p->cacheCtr = 1;
! 60803: p->minWriteFileFormat = 255;
! 60804: p->iStatement = 0;
! 60805: p->nFkConstraint = 0;
! 60806: #ifdef VDBE_PROFILE
! 60807: for(i=0; i<p->nOp; i++){
! 60808: p->aOp[i].cnt = 0;
! 60809: p->aOp[i].cycles = 0;
! 60810: }
! 60811: #endif
! 60812: }
! 60813:
! 60814: /*
! 60815: ** Prepare a virtual machine for execution for the first time after
! 60816: ** creating the virtual machine. This involves things such
! 60817: ** as allocating stack space and initializing the program counter.
! 60818: ** After the VDBE has be prepped, it can be executed by one or more
! 60819: ** calls to sqlite3VdbeExec().
! 60820: **
! 60821: ** This function may be called exact once on a each virtual machine.
! 60822: ** After this routine is called the VM has been "packaged" and is ready
! 60823: ** to run. After this routine is called, futher calls to
! 60824: ** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
! 60825: ** the Vdbe from the Parse object that helped generate it so that the
! 60826: ** the Vdbe becomes an independent entity and the Parse object can be
! 60827: ** destroyed.
! 60828: **
! 60829: ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
! 60830: ** to its initial state after it has been run.
! 60831: */
! 60832: SQLITE_PRIVATE void sqlite3VdbeMakeReady(
! 60833: Vdbe *p, /* The VDBE */
! 60834: Parse *pParse /* Parsing context */
! 60835: ){
! 60836: sqlite3 *db; /* The database connection */
! 60837: int nVar; /* Number of parameters */
! 60838: int nMem; /* Number of VM memory registers */
! 60839: int nCursor; /* Number of cursors required */
! 60840: int nArg; /* Number of arguments in subprograms */
! 60841: int nOnce; /* Number of OP_Once instructions */
! 60842: int n; /* Loop counter */
! 60843: u8 *zCsr; /* Memory available for allocation */
! 60844: u8 *zEnd; /* First byte past allocated memory */
! 60845: int nByte; /* How much extra memory is needed */
! 60846:
! 60847: assert( p!=0 );
! 60848: assert( p->nOp>0 );
! 60849: assert( pParse!=0 );
! 60850: assert( p->magic==VDBE_MAGIC_INIT );
! 60851: db = p->db;
! 60852: assert( db->mallocFailed==0 );
! 60853: nVar = pParse->nVar;
! 60854: nMem = pParse->nMem;
! 60855: nCursor = pParse->nTab;
! 60856: nArg = pParse->nMaxArg;
! 60857: nOnce = pParse->nOnce;
! 60858: if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
! 60859:
! 60860: /* For each cursor required, also allocate a memory cell. Memory
! 60861: ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
! 60862: ** the vdbe program. Instead they are used to allocate space for
! 60863: ** VdbeCursor/BtCursor structures. The blob of memory associated with
! 60864: ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
! 60865: ** stores the blob of memory associated with cursor 1, etc.
! 60866: **
! 60867: ** See also: allocateCursor().
! 60868: */
! 60869: nMem += nCursor;
! 60870:
! 60871: /* Allocate space for memory registers, SQL variables, VDBE cursors and
! 60872: ** an array to marshal SQL function arguments in.
! 60873: */
! 60874: zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */
! 60875: zEnd = (u8*)&p->aOp[p->nOpAlloc]; /* First byte past end of zCsr[] */
! 60876:
! 60877: resolveP2Values(p, &nArg);
! 60878: p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
! 60879: if( pParse->explain && nMem<10 ){
! 60880: nMem = 10;
! 60881: }
! 60882: memset(zCsr, 0, zEnd-zCsr);
! 60883: zCsr += (zCsr - (u8*)0)&7;
! 60884: assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
! 60885: p->expired = 0;
! 60886:
! 60887: /* Memory for registers, parameters, cursor, etc, is allocated in two
! 60888: ** passes. On the first pass, we try to reuse unused space at the
! 60889: ** end of the opcode array. If we are unable to satisfy all memory
! 60890: ** requirements by reusing the opcode array tail, then the second
! 60891: ** pass will fill in the rest using a fresh allocation.
! 60892: **
! 60893: ** This two-pass approach that reuses as much memory as possible from
! 60894: ** the leftover space at the end of the opcode array can significantly
! 60895: ** reduce the amount of memory held by a prepared statement.
! 60896: */
! 60897: do {
! 60898: nByte = 0;
! 60899: p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
! 60900: p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
! 60901: p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
! 60902: p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
! 60903: p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
! 60904: &zCsr, zEnd, &nByte);
! 60905: p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
! 60906: if( nByte ){
! 60907: p->pFree = sqlite3DbMallocZero(db, nByte);
! 60908: }
! 60909: zCsr = p->pFree;
! 60910: zEnd = &zCsr[nByte];
! 60911: }while( nByte && !db->mallocFailed );
! 60912:
! 60913: p->nCursor = (u16)nCursor;
! 60914: p->nOnceFlag = nOnce;
! 60915: if( p->aVar ){
! 60916: p->nVar = (ynVar)nVar;
! 60917: for(n=0; n<nVar; n++){
! 60918: p->aVar[n].flags = MEM_Null;
! 60919: p->aVar[n].db = db;
! 60920: }
! 60921: }
! 60922: if( p->azVar ){
! 60923: p->nzVar = pParse->nzVar;
! 60924: memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
! 60925: memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
! 60926: }
! 60927: if( p->aMem ){
! 60928: p->aMem--; /* aMem[] goes from 1..nMem */
! 60929: p->nMem = nMem; /* not from 0..nMem-1 */
! 60930: for(n=1; n<=nMem; n++){
! 60931: p->aMem[n].flags = MEM_Invalid;
! 60932: p->aMem[n].db = db;
! 60933: }
! 60934: }
! 60935: p->explain = pParse->explain;
! 60936: sqlite3VdbeRewind(p);
! 60937: }
! 60938:
! 60939: /*
! 60940: ** Close a VDBE cursor and release all the resources that cursor
! 60941: ** happens to hold.
! 60942: */
! 60943: SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
! 60944: if( pCx==0 ){
! 60945: return;
! 60946: }
! 60947: sqlite3VdbeSorterClose(p->db, pCx);
! 60948: if( pCx->pBt ){
! 60949: sqlite3BtreeClose(pCx->pBt);
! 60950: /* The pCx->pCursor will be close automatically, if it exists, by
! 60951: ** the call above. */
! 60952: }else if( pCx->pCursor ){
! 60953: sqlite3BtreeCloseCursor(pCx->pCursor);
! 60954: }
! 60955: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 60956: if( pCx->pVtabCursor ){
! 60957: sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
! 60958: const sqlite3_module *pModule = pCx->pModule;
! 60959: p->inVtabMethod = 1;
! 60960: pModule->xClose(pVtabCursor);
! 60961: p->inVtabMethod = 0;
! 60962: }
! 60963: #endif
! 60964: }
! 60965:
! 60966: /*
! 60967: ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
! 60968: ** is used, for example, when a trigger sub-program is halted to restore
! 60969: ** control to the main program.
! 60970: */
! 60971: SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
! 60972: Vdbe *v = pFrame->v;
! 60973: v->aOnceFlag = pFrame->aOnceFlag;
! 60974: v->nOnceFlag = pFrame->nOnceFlag;
! 60975: v->aOp = pFrame->aOp;
! 60976: v->nOp = pFrame->nOp;
! 60977: v->aMem = pFrame->aMem;
! 60978: v->nMem = pFrame->nMem;
! 60979: v->apCsr = pFrame->apCsr;
! 60980: v->nCursor = pFrame->nCursor;
! 60981: v->db->lastRowid = pFrame->lastRowid;
! 60982: v->nChange = pFrame->nChange;
! 60983: return pFrame->pc;
! 60984: }
! 60985:
! 60986: /*
! 60987: ** Close all cursors.
! 60988: **
! 60989: ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
! 60990: ** cell array. This is necessary as the memory cell array may contain
! 60991: ** pointers to VdbeFrame objects, which may in turn contain pointers to
! 60992: ** open cursors.
! 60993: */
! 60994: static void closeAllCursors(Vdbe *p){
! 60995: if( p->pFrame ){
! 60996: VdbeFrame *pFrame;
! 60997: for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
! 60998: sqlite3VdbeFrameRestore(pFrame);
! 60999: }
! 61000: p->pFrame = 0;
! 61001: p->nFrame = 0;
! 61002:
! 61003: if( p->apCsr ){
! 61004: int i;
! 61005: for(i=0; i<p->nCursor; i++){
! 61006: VdbeCursor *pC = p->apCsr[i];
! 61007: if( pC ){
! 61008: sqlite3VdbeFreeCursor(p, pC);
! 61009: p->apCsr[i] = 0;
! 61010: }
! 61011: }
! 61012: }
! 61013: if( p->aMem ){
! 61014: releaseMemArray(&p->aMem[1], p->nMem);
! 61015: }
! 61016: while( p->pDelFrame ){
! 61017: VdbeFrame *pDel = p->pDelFrame;
! 61018: p->pDelFrame = pDel->pParent;
! 61019: sqlite3VdbeFrameDelete(pDel);
! 61020: }
! 61021: }
! 61022:
! 61023: /*
! 61024: ** Clean up the VM after execution.
! 61025: **
! 61026: ** This routine will automatically close any cursors, lists, and/or
! 61027: ** sorters that were left open. It also deletes the values of
! 61028: ** variables in the aVar[] array.
! 61029: */
! 61030: static void Cleanup(Vdbe *p){
! 61031: sqlite3 *db = p->db;
! 61032:
! 61033: #ifdef SQLITE_DEBUG
! 61034: /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
! 61035: ** Vdbe.aMem[] arrays have already been cleaned up. */
! 61036: int i;
! 61037: if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
! 61038: if( p->aMem ){
! 61039: for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Invalid );
! 61040: }
! 61041: #endif
! 61042:
! 61043: sqlite3DbFree(db, p->zErrMsg);
! 61044: p->zErrMsg = 0;
! 61045: p->pResultSet = 0;
! 61046: }
! 61047:
! 61048: /*
! 61049: ** Set the number of result columns that will be returned by this SQL
! 61050: ** statement. This is now set at compile time, rather than during
! 61051: ** execution of the vdbe program so that sqlite3_column_count() can
! 61052: ** be called on an SQL statement before sqlite3_step().
! 61053: */
! 61054: SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
! 61055: Mem *pColName;
! 61056: int n;
! 61057: sqlite3 *db = p->db;
! 61058:
! 61059: releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
! 61060: sqlite3DbFree(db, p->aColName);
! 61061: n = nResColumn*COLNAME_N;
! 61062: p->nResColumn = (u16)nResColumn;
! 61063: p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
! 61064: if( p->aColName==0 ) return;
! 61065: while( n-- > 0 ){
! 61066: pColName->flags = MEM_Null;
! 61067: pColName->db = p->db;
! 61068: pColName++;
! 61069: }
! 61070: }
! 61071:
! 61072: /*
! 61073: ** Set the name of the idx'th column to be returned by the SQL statement.
! 61074: ** zName must be a pointer to a nul terminated string.
! 61075: **
! 61076: ** This call must be made after a call to sqlite3VdbeSetNumCols().
! 61077: **
! 61078: ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
! 61079: ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
! 61080: ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
! 61081: */
! 61082: SQLITE_PRIVATE int sqlite3VdbeSetColName(
! 61083: Vdbe *p, /* Vdbe being configured */
! 61084: int idx, /* Index of column zName applies to */
! 61085: int var, /* One of the COLNAME_* constants */
! 61086: const char *zName, /* Pointer to buffer containing name */
! 61087: void (*xDel)(void*) /* Memory management strategy for zName */
! 61088: ){
! 61089: int rc;
! 61090: Mem *pColName;
! 61091: assert( idx<p->nResColumn );
! 61092: assert( var<COLNAME_N );
! 61093: if( p->db->mallocFailed ){
! 61094: assert( !zName || xDel!=SQLITE_DYNAMIC );
! 61095: return SQLITE_NOMEM;
! 61096: }
! 61097: assert( p->aColName!=0 );
! 61098: pColName = &(p->aColName[idx+var*p->nResColumn]);
! 61099: rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
! 61100: assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
! 61101: return rc;
! 61102: }
! 61103:
! 61104: /*
! 61105: ** A read or write transaction may or may not be active on database handle
! 61106: ** db. If a transaction is active, commit it. If there is a
! 61107: ** write-transaction spanning more than one database file, this routine
! 61108: ** takes care of the master journal trickery.
! 61109: */
! 61110: static int vdbeCommit(sqlite3 *db, Vdbe *p){
! 61111: int i;
! 61112: int nTrans = 0; /* Number of databases with an active write-transaction */
! 61113: int rc = SQLITE_OK;
! 61114: int needXcommit = 0;
! 61115:
! 61116: #ifdef SQLITE_OMIT_VIRTUALTABLE
! 61117: /* With this option, sqlite3VtabSync() is defined to be simply
! 61118: ** SQLITE_OK so p is not used.
! 61119: */
! 61120: UNUSED_PARAMETER(p);
! 61121: #endif
! 61122:
! 61123: /* Before doing anything else, call the xSync() callback for any
! 61124: ** virtual module tables written in this transaction. This has to
! 61125: ** be done before determining whether a master journal file is
! 61126: ** required, as an xSync() callback may add an attached database
! 61127: ** to the transaction.
! 61128: */
! 61129: rc = sqlite3VtabSync(db, &p->zErrMsg);
! 61130:
! 61131: /* This loop determines (a) if the commit hook should be invoked and
! 61132: ** (b) how many database files have open write transactions, not
! 61133: ** including the temp database. (b) is important because if more than
! 61134: ** one database file has an open write transaction, a master journal
! 61135: ** file is required for an atomic commit.
! 61136: */
! 61137: for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
! 61138: Btree *pBt = db->aDb[i].pBt;
! 61139: if( sqlite3BtreeIsInTrans(pBt) ){
! 61140: needXcommit = 1;
! 61141: if( i!=1 ) nTrans++;
! 61142: rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
! 61143: }
! 61144: }
! 61145: if( rc!=SQLITE_OK ){
! 61146: return rc;
! 61147: }
! 61148:
! 61149: /* If there are any write-transactions at all, invoke the commit hook */
! 61150: if( needXcommit && db->xCommitCallback ){
! 61151: rc = db->xCommitCallback(db->pCommitArg);
! 61152: if( rc ){
! 61153: return SQLITE_CONSTRAINT;
! 61154: }
! 61155: }
! 61156:
! 61157: /* The simple case - no more than one database file (not counting the
! 61158: ** TEMP database) has a transaction active. There is no need for the
! 61159: ** master-journal.
! 61160: **
! 61161: ** If the return value of sqlite3BtreeGetFilename() is a zero length
! 61162: ** string, it means the main database is :memory: or a temp file. In
! 61163: ** that case we do not support atomic multi-file commits, so use the
! 61164: ** simple case then too.
! 61165: */
! 61166: if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
! 61167: || nTrans<=1
! 61168: ){
! 61169: for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
! 61170: Btree *pBt = db->aDb[i].pBt;
! 61171: if( pBt ){
! 61172: rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
! 61173: }
! 61174: }
! 61175:
! 61176: /* Do the commit only if all databases successfully complete phase 1.
! 61177: ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
! 61178: ** IO error while deleting or truncating a journal file. It is unlikely,
! 61179: ** but could happen. In this case abandon processing and return the error.
! 61180: */
! 61181: for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
! 61182: Btree *pBt = db->aDb[i].pBt;
! 61183: if( pBt ){
! 61184: rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
! 61185: }
! 61186: }
! 61187: if( rc==SQLITE_OK ){
! 61188: sqlite3VtabCommit(db);
! 61189: }
! 61190: }
! 61191:
! 61192: /* The complex case - There is a multi-file write-transaction active.
! 61193: ** This requires a master journal file to ensure the transaction is
! 61194: ** committed atomicly.
! 61195: */
! 61196: #ifndef SQLITE_OMIT_DISKIO
! 61197: else{
! 61198: sqlite3_vfs *pVfs = db->pVfs;
! 61199: int needSync = 0;
! 61200: char *zMaster = 0; /* File-name for the master journal */
! 61201: char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
! 61202: sqlite3_file *pMaster = 0;
! 61203: i64 offset = 0;
! 61204: int res;
! 61205: int retryCount = 0;
! 61206: int nMainFile;
! 61207:
! 61208: /* Select a master journal file name */
! 61209: nMainFile = sqlite3Strlen30(zMainFile);
! 61210: zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
! 61211: if( zMaster==0 ) return SQLITE_NOMEM;
! 61212: do {
! 61213: u32 iRandom;
! 61214: if( retryCount ){
! 61215: if( retryCount>100 ){
! 61216: sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
! 61217: sqlite3OsDelete(pVfs, zMaster, 0);
! 61218: break;
! 61219: }else if( retryCount==1 ){
! 61220: sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
! 61221: }
! 61222: }
! 61223: retryCount++;
! 61224: sqlite3_randomness(sizeof(iRandom), &iRandom);
! 61225: sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
! 61226: (iRandom>>8)&0xffffff, iRandom&0xff);
! 61227: /* The antipenultimate character of the master journal name must
! 61228: ** be "9" to avoid name collisions when using 8+3 filenames. */
! 61229: assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
! 61230: sqlite3FileSuffix3(zMainFile, zMaster);
! 61231: rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
! 61232: }while( rc==SQLITE_OK && res );
! 61233: if( rc==SQLITE_OK ){
! 61234: /* Open the master journal. */
! 61235: rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
! 61236: SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
! 61237: SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
! 61238: );
! 61239: }
! 61240: if( rc!=SQLITE_OK ){
! 61241: sqlite3DbFree(db, zMaster);
! 61242: return rc;
! 61243: }
! 61244:
! 61245: /* Write the name of each database file in the transaction into the new
! 61246: ** master journal file. If an error occurs at this point close
! 61247: ** and delete the master journal file. All the individual journal files
! 61248: ** still have 'null' as the master journal pointer, so they will roll
! 61249: ** back independently if a failure occurs.
! 61250: */
! 61251: for(i=0; i<db->nDb; i++){
! 61252: Btree *pBt = db->aDb[i].pBt;
! 61253: if( sqlite3BtreeIsInTrans(pBt) ){
! 61254: char const *zFile = sqlite3BtreeGetJournalname(pBt);
! 61255: if( zFile==0 ){
! 61256: continue; /* Ignore TEMP and :memory: databases */
! 61257: }
! 61258: assert( zFile[0]!=0 );
! 61259: if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
! 61260: needSync = 1;
! 61261: }
! 61262: rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
! 61263: offset += sqlite3Strlen30(zFile)+1;
! 61264: if( rc!=SQLITE_OK ){
! 61265: sqlite3OsCloseFree(pMaster);
! 61266: sqlite3OsDelete(pVfs, zMaster, 0);
! 61267: sqlite3DbFree(db, zMaster);
! 61268: return rc;
! 61269: }
! 61270: }
! 61271: }
! 61272:
! 61273: /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
! 61274: ** flag is set this is not required.
! 61275: */
! 61276: if( needSync
! 61277: && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
! 61278: && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
! 61279: ){
! 61280: sqlite3OsCloseFree(pMaster);
! 61281: sqlite3OsDelete(pVfs, zMaster, 0);
! 61282: sqlite3DbFree(db, zMaster);
! 61283: return rc;
! 61284: }
! 61285:
! 61286: /* Sync all the db files involved in the transaction. The same call
! 61287: ** sets the master journal pointer in each individual journal. If
! 61288: ** an error occurs here, do not delete the master journal file.
! 61289: **
! 61290: ** If the error occurs during the first call to
! 61291: ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
! 61292: ** master journal file will be orphaned. But we cannot delete it,
! 61293: ** in case the master journal file name was written into the journal
! 61294: ** file before the failure occurred.
! 61295: */
! 61296: for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
! 61297: Btree *pBt = db->aDb[i].pBt;
! 61298: if( pBt ){
! 61299: rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
! 61300: }
! 61301: }
! 61302: sqlite3OsCloseFree(pMaster);
! 61303: assert( rc!=SQLITE_BUSY );
! 61304: if( rc!=SQLITE_OK ){
! 61305: sqlite3DbFree(db, zMaster);
! 61306: return rc;
! 61307: }
! 61308:
! 61309: /* Delete the master journal file. This commits the transaction. After
! 61310: ** doing this the directory is synced again before any individual
! 61311: ** transaction files are deleted.
! 61312: */
! 61313: rc = sqlite3OsDelete(pVfs, zMaster, 1);
! 61314: sqlite3DbFree(db, zMaster);
! 61315: zMaster = 0;
! 61316: if( rc ){
! 61317: return rc;
! 61318: }
! 61319:
! 61320: /* All files and directories have already been synced, so the following
! 61321: ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
! 61322: ** deleting or truncating journals. If something goes wrong while
! 61323: ** this is happening we don't really care. The integrity of the
! 61324: ** transaction is already guaranteed, but some stray 'cold' journals
! 61325: ** may be lying around. Returning an error code won't help matters.
! 61326: */
! 61327: disable_simulated_io_errors();
! 61328: sqlite3BeginBenignMalloc();
! 61329: for(i=0; i<db->nDb; i++){
! 61330: Btree *pBt = db->aDb[i].pBt;
! 61331: if( pBt ){
! 61332: sqlite3BtreeCommitPhaseTwo(pBt, 1);
! 61333: }
! 61334: }
! 61335: sqlite3EndBenignMalloc();
! 61336: enable_simulated_io_errors();
! 61337:
! 61338: sqlite3VtabCommit(db);
! 61339: }
! 61340: #endif
! 61341:
! 61342: return rc;
! 61343: }
! 61344:
! 61345: /*
! 61346: ** This routine checks that the sqlite3.activeVdbeCnt count variable
! 61347: ** matches the number of vdbe's in the list sqlite3.pVdbe that are
! 61348: ** currently active. An assertion fails if the two counts do not match.
! 61349: ** This is an internal self-check only - it is not an essential processing
! 61350: ** step.
! 61351: **
! 61352: ** This is a no-op if NDEBUG is defined.
! 61353: */
! 61354: #ifndef NDEBUG
! 61355: static void checkActiveVdbeCnt(sqlite3 *db){
! 61356: Vdbe *p;
! 61357: int cnt = 0;
! 61358: int nWrite = 0;
! 61359: p = db->pVdbe;
! 61360: while( p ){
! 61361: if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
! 61362: cnt++;
! 61363: if( p->readOnly==0 ) nWrite++;
! 61364: }
! 61365: p = p->pNext;
! 61366: }
! 61367: assert( cnt==db->activeVdbeCnt );
! 61368: assert( nWrite==db->writeVdbeCnt );
! 61369: }
! 61370: #else
! 61371: #define checkActiveVdbeCnt(x)
! 61372: #endif
! 61373:
! 61374: /*
! 61375: ** For every Btree that in database connection db which
! 61376: ** has been modified, "trip" or invalidate each cursor in
! 61377: ** that Btree might have been modified so that the cursor
! 61378: ** can never be used again. This happens when a rollback
! 61379: *** occurs. We have to trip all the other cursors, even
! 61380: ** cursor from other VMs in different database connections,
! 61381: ** so that none of them try to use the data at which they
! 61382: ** were pointing and which now may have been changed due
! 61383: ** to the rollback.
! 61384: **
! 61385: ** Remember that a rollback can delete tables complete and
! 61386: ** reorder rootpages. So it is not sufficient just to save
! 61387: ** the state of the cursor. We have to invalidate the cursor
! 61388: ** so that it is never used again.
! 61389: */
! 61390: static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
! 61391: int i;
! 61392: for(i=0; i<db->nDb; i++){
! 61393: Btree *p = db->aDb[i].pBt;
! 61394: if( p && sqlite3BtreeIsInTrans(p) ){
! 61395: sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
! 61396: }
! 61397: }
! 61398: }
! 61399:
! 61400: /*
! 61401: ** If the Vdbe passed as the first argument opened a statement-transaction,
! 61402: ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
! 61403: ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
! 61404: ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
! 61405: ** statement transaction is commtted.
! 61406: **
! 61407: ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
! 61408: ** Otherwise SQLITE_OK.
! 61409: */
! 61410: SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
! 61411: sqlite3 *const db = p->db;
! 61412: int rc = SQLITE_OK;
! 61413:
! 61414: /* If p->iStatement is greater than zero, then this Vdbe opened a
! 61415: ** statement transaction that should be closed here. The only exception
! 61416: ** is that an IO error may have occured, causing an emergency rollback.
! 61417: ** In this case (db->nStatement==0), and there is nothing to do.
! 61418: */
! 61419: if( db->nStatement && p->iStatement ){
! 61420: int i;
! 61421: const int iSavepoint = p->iStatement-1;
! 61422:
! 61423: assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
! 61424: assert( db->nStatement>0 );
! 61425: assert( p->iStatement==(db->nStatement+db->nSavepoint) );
! 61426:
! 61427: for(i=0; i<db->nDb; i++){
! 61428: int rc2 = SQLITE_OK;
! 61429: Btree *pBt = db->aDb[i].pBt;
! 61430: if( pBt ){
! 61431: if( eOp==SAVEPOINT_ROLLBACK ){
! 61432: rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
! 61433: }
! 61434: if( rc2==SQLITE_OK ){
! 61435: rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
! 61436: }
! 61437: if( rc==SQLITE_OK ){
! 61438: rc = rc2;
! 61439: }
! 61440: }
! 61441: }
! 61442: db->nStatement--;
! 61443: p->iStatement = 0;
! 61444:
! 61445: if( rc==SQLITE_OK ){
! 61446: if( eOp==SAVEPOINT_ROLLBACK ){
! 61447: rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
! 61448: }
! 61449: if( rc==SQLITE_OK ){
! 61450: rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
! 61451: }
! 61452: }
! 61453:
! 61454: /* If the statement transaction is being rolled back, also restore the
! 61455: ** database handles deferred constraint counter to the value it had when
! 61456: ** the statement transaction was opened. */
! 61457: if( eOp==SAVEPOINT_ROLLBACK ){
! 61458: db->nDeferredCons = p->nStmtDefCons;
! 61459: }
! 61460: }
! 61461: return rc;
! 61462: }
! 61463:
! 61464: /*
! 61465: ** This function is called when a transaction opened by the database
! 61466: ** handle associated with the VM passed as an argument is about to be
! 61467: ** committed. If there are outstanding deferred foreign key constraint
! 61468: ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
! 61469: **
! 61470: ** If there are outstanding FK violations and this function returns
! 61471: ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write
! 61472: ** an error message to it. Then return SQLITE_ERROR.
! 61473: */
! 61474: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 61475: SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
! 61476: sqlite3 *db = p->db;
! 61477: if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){
! 61478: p->rc = SQLITE_CONSTRAINT;
! 61479: p->errorAction = OE_Abort;
! 61480: sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
! 61481: return SQLITE_ERROR;
! 61482: }
! 61483: return SQLITE_OK;
! 61484: }
! 61485: #endif
! 61486:
! 61487: /*
! 61488: ** This routine is called the when a VDBE tries to halt. If the VDBE
! 61489: ** has made changes and is in autocommit mode, then commit those
! 61490: ** changes. If a rollback is needed, then do the rollback.
! 61491: **
! 61492: ** This routine is the only way to move the state of a VM from
! 61493: ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
! 61494: ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
! 61495: **
! 61496: ** Return an error code. If the commit could not complete because of
! 61497: ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
! 61498: ** means the close did not happen and needs to be repeated.
! 61499: */
! 61500: SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
! 61501: int rc; /* Used to store transient return codes */
! 61502: sqlite3 *db = p->db;
! 61503:
! 61504: /* This function contains the logic that determines if a statement or
! 61505: ** transaction will be committed or rolled back as a result of the
! 61506: ** execution of this virtual machine.
! 61507: **
! 61508: ** If any of the following errors occur:
! 61509: **
! 61510: ** SQLITE_NOMEM
! 61511: ** SQLITE_IOERR
! 61512: ** SQLITE_FULL
! 61513: ** SQLITE_INTERRUPT
! 61514: **
! 61515: ** Then the internal cache might have been left in an inconsistent
! 61516: ** state. We need to rollback the statement transaction, if there is
! 61517: ** one, or the complete transaction if there is no statement transaction.
! 61518: */
! 61519:
! 61520: if( p->db->mallocFailed ){
! 61521: p->rc = SQLITE_NOMEM;
! 61522: }
! 61523: if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
! 61524: closeAllCursors(p);
! 61525: if( p->magic!=VDBE_MAGIC_RUN ){
! 61526: return SQLITE_OK;
! 61527: }
! 61528: checkActiveVdbeCnt(db);
! 61529:
! 61530: /* No commit or rollback needed if the program never started */
! 61531: if( p->pc>=0 ){
! 61532: int mrc; /* Primary error code from p->rc */
! 61533: int eStatementOp = 0;
! 61534: int isSpecialError; /* Set to true if a 'special' error */
! 61535:
! 61536: /* Lock all btrees used by the statement */
! 61537: sqlite3VdbeEnter(p);
! 61538:
! 61539: /* Check for one of the special errors */
! 61540: mrc = p->rc & 0xff;
! 61541: assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
! 61542: isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
! 61543: || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
! 61544: if( isSpecialError ){
! 61545: /* If the query was read-only and the error code is SQLITE_INTERRUPT,
! 61546: ** no rollback is necessary. Otherwise, at least a savepoint
! 61547: ** transaction must be rolled back to restore the database to a
! 61548: ** consistent state.
! 61549: **
! 61550: ** Even if the statement is read-only, it is important to perform
! 61551: ** a statement or transaction rollback operation. If the error
! 61552: ** occured while writing to the journal, sub-journal or database
! 61553: ** file as part of an effort to free up cache space (see function
! 61554: ** pagerStress() in pager.c), the rollback is required to restore
! 61555: ** the pager to a consistent state.
! 61556: */
! 61557: if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
! 61558: if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
! 61559: eStatementOp = SAVEPOINT_ROLLBACK;
! 61560: }else{
! 61561: /* We are forced to roll back the active transaction. Before doing
! 61562: ** so, abort any other statements this handle currently has active.
! 61563: */
! 61564: invalidateCursorsOnModifiedBtrees(db);
! 61565: sqlite3RollbackAll(db);
! 61566: sqlite3CloseSavepoints(db);
! 61567: db->autoCommit = 1;
! 61568: }
! 61569: }
! 61570: }
! 61571:
! 61572: /* Check for immediate foreign key violations. */
! 61573: if( p->rc==SQLITE_OK ){
! 61574: sqlite3VdbeCheckFk(p, 0);
! 61575: }
! 61576:
! 61577: /* If the auto-commit flag is set and this is the only active writer
! 61578: ** VM, then we do either a commit or rollback of the current transaction.
! 61579: **
! 61580: ** Note: This block also runs if one of the special errors handled
! 61581: ** above has occurred.
! 61582: */
! 61583: if( !sqlite3VtabInSync(db)
! 61584: && db->autoCommit
! 61585: && db->writeVdbeCnt==(p->readOnly==0)
! 61586: ){
! 61587: if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
! 61588: rc = sqlite3VdbeCheckFk(p, 1);
! 61589: if( rc!=SQLITE_OK ){
! 61590: if( NEVER(p->readOnly) ){
! 61591: sqlite3VdbeLeave(p);
! 61592: return SQLITE_ERROR;
! 61593: }
! 61594: rc = SQLITE_CONSTRAINT;
! 61595: }else{
! 61596: /* The auto-commit flag is true, the vdbe program was successful
! 61597: ** or hit an 'OR FAIL' constraint and there are no deferred foreign
! 61598: ** key constraints to hold up the transaction. This means a commit
! 61599: ** is required. */
! 61600: rc = vdbeCommit(db, p);
! 61601: }
! 61602: if( rc==SQLITE_BUSY && p->readOnly ){
! 61603: sqlite3VdbeLeave(p);
! 61604: return SQLITE_BUSY;
! 61605: }else if( rc!=SQLITE_OK ){
! 61606: p->rc = rc;
! 61607: sqlite3RollbackAll(db);
! 61608: }else{
! 61609: db->nDeferredCons = 0;
! 61610: sqlite3CommitInternalChanges(db);
! 61611: }
! 61612: }else{
! 61613: sqlite3RollbackAll(db);
! 61614: }
! 61615: db->nStatement = 0;
! 61616: }else if( eStatementOp==0 ){
! 61617: if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
! 61618: eStatementOp = SAVEPOINT_RELEASE;
! 61619: }else if( p->errorAction==OE_Abort ){
! 61620: eStatementOp = SAVEPOINT_ROLLBACK;
! 61621: }else{
! 61622: invalidateCursorsOnModifiedBtrees(db);
! 61623: sqlite3RollbackAll(db);
! 61624: sqlite3CloseSavepoints(db);
! 61625: db->autoCommit = 1;
! 61626: }
! 61627: }
! 61628:
! 61629: /* If eStatementOp is non-zero, then a statement transaction needs to
! 61630: ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
! 61631: ** do so. If this operation returns an error, and the current statement
! 61632: ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
! 61633: ** current statement error code.
! 61634: */
! 61635: if( eStatementOp ){
! 61636: rc = sqlite3VdbeCloseStatement(p, eStatementOp);
! 61637: if( rc ){
! 61638: if( p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT ){
! 61639: p->rc = rc;
! 61640: sqlite3DbFree(db, p->zErrMsg);
! 61641: p->zErrMsg = 0;
! 61642: }
! 61643: invalidateCursorsOnModifiedBtrees(db);
! 61644: sqlite3RollbackAll(db);
! 61645: sqlite3CloseSavepoints(db);
! 61646: db->autoCommit = 1;
! 61647: }
! 61648: }
! 61649:
! 61650: /* If this was an INSERT, UPDATE or DELETE and no statement transaction
! 61651: ** has been rolled back, update the database connection change-counter.
! 61652: */
! 61653: if( p->changeCntOn ){
! 61654: if( eStatementOp!=SAVEPOINT_ROLLBACK ){
! 61655: sqlite3VdbeSetChanges(db, p->nChange);
! 61656: }else{
! 61657: sqlite3VdbeSetChanges(db, 0);
! 61658: }
! 61659: p->nChange = 0;
! 61660: }
! 61661:
! 61662: /* Rollback or commit any schema changes that occurred. */
! 61663: if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
! 61664: sqlite3ResetInternalSchema(db, -1);
! 61665: db->flags = (db->flags | SQLITE_InternChanges);
! 61666: }
! 61667:
! 61668: /* Release the locks */
! 61669: sqlite3VdbeLeave(p);
! 61670: }
! 61671:
! 61672: /* We have successfully halted and closed the VM. Record this fact. */
! 61673: if( p->pc>=0 ){
! 61674: db->activeVdbeCnt--;
! 61675: if( !p->readOnly ){
! 61676: db->writeVdbeCnt--;
! 61677: }
! 61678: assert( db->activeVdbeCnt>=db->writeVdbeCnt );
! 61679: }
! 61680: p->magic = VDBE_MAGIC_HALT;
! 61681: checkActiveVdbeCnt(db);
! 61682: if( p->db->mallocFailed ){
! 61683: p->rc = SQLITE_NOMEM;
! 61684: }
! 61685:
! 61686: /* If the auto-commit flag is set to true, then any locks that were held
! 61687: ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
! 61688: ** to invoke any required unlock-notify callbacks.
! 61689: */
! 61690: if( db->autoCommit ){
! 61691: sqlite3ConnectionUnlocked(db);
! 61692: }
! 61693:
! 61694: assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
! 61695: return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
! 61696: }
! 61697:
! 61698:
! 61699: /*
! 61700: ** Each VDBE holds the result of the most recent sqlite3_step() call
! 61701: ** in p->rc. This routine sets that result back to SQLITE_OK.
! 61702: */
! 61703: SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
! 61704: p->rc = SQLITE_OK;
! 61705: }
! 61706:
! 61707: /*
! 61708: ** Copy the error code and error message belonging to the VDBE passed
! 61709: ** as the first argument to its database handle (so that they will be
! 61710: ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
! 61711: **
! 61712: ** This function does not clear the VDBE error code or message, just
! 61713: ** copies them to the database handle.
! 61714: */
! 61715: SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
! 61716: sqlite3 *db = p->db;
! 61717: int rc = p->rc;
! 61718: if( p->zErrMsg ){
! 61719: u8 mallocFailed = db->mallocFailed;
! 61720: sqlite3BeginBenignMalloc();
! 61721: sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
! 61722: sqlite3EndBenignMalloc();
! 61723: db->mallocFailed = mallocFailed;
! 61724: db->errCode = rc;
! 61725: }else{
! 61726: sqlite3Error(db, rc, 0);
! 61727: }
! 61728: return rc;
! 61729: }
! 61730:
! 61731: /*
! 61732: ** Clean up a VDBE after execution but do not delete the VDBE just yet.
! 61733: ** Write any error messages into *pzErrMsg. Return the result code.
! 61734: **
! 61735: ** After this routine is run, the VDBE should be ready to be executed
! 61736: ** again.
! 61737: **
! 61738: ** To look at it another way, this routine resets the state of the
! 61739: ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
! 61740: ** VDBE_MAGIC_INIT.
! 61741: */
! 61742: SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
! 61743: sqlite3 *db;
! 61744: db = p->db;
! 61745:
! 61746: /* If the VM did not run to completion or if it encountered an
! 61747: ** error, then it might not have been halted properly. So halt
! 61748: ** it now.
! 61749: */
! 61750: sqlite3VdbeHalt(p);
! 61751:
! 61752: /* If the VDBE has be run even partially, then transfer the error code
! 61753: ** and error message from the VDBE into the main database structure. But
! 61754: ** if the VDBE has just been set to run but has not actually executed any
! 61755: ** instructions yet, leave the main database error information unchanged.
! 61756: */
! 61757: if( p->pc>=0 ){
! 61758: sqlite3VdbeTransferError(p);
! 61759: sqlite3DbFree(db, p->zErrMsg);
! 61760: p->zErrMsg = 0;
! 61761: if( p->runOnlyOnce ) p->expired = 1;
! 61762: }else if( p->rc && p->expired ){
! 61763: /* The expired flag was set on the VDBE before the first call
! 61764: ** to sqlite3_step(). For consistency (since sqlite3_step() was
! 61765: ** called), set the database error in this case as well.
! 61766: */
! 61767: sqlite3Error(db, p->rc, 0);
! 61768: sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
! 61769: sqlite3DbFree(db, p->zErrMsg);
! 61770: p->zErrMsg = 0;
! 61771: }
! 61772:
! 61773: /* Reclaim all memory used by the VDBE
! 61774: */
! 61775: Cleanup(p);
! 61776:
! 61777: /* Save profiling information from this VDBE run.
! 61778: */
! 61779: #ifdef VDBE_PROFILE
! 61780: {
! 61781: FILE *out = fopen("vdbe_profile.out", "a");
! 61782: if( out ){
! 61783: int i;
! 61784: fprintf(out, "---- ");
! 61785: for(i=0; i<p->nOp; i++){
! 61786: fprintf(out, "%02x", p->aOp[i].opcode);
! 61787: }
! 61788: fprintf(out, "\n");
! 61789: for(i=0; i<p->nOp; i++){
! 61790: fprintf(out, "%6d %10lld %8lld ",
! 61791: p->aOp[i].cnt,
! 61792: p->aOp[i].cycles,
! 61793: p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
! 61794: );
! 61795: sqlite3VdbePrintOp(out, i, &p->aOp[i]);
! 61796: }
! 61797: fclose(out);
! 61798: }
! 61799: }
! 61800: #endif
! 61801: p->magic = VDBE_MAGIC_INIT;
! 61802: return p->rc & db->errMask;
! 61803: }
! 61804:
! 61805: /*
! 61806: ** Clean up and delete a VDBE after execution. Return an integer which is
! 61807: ** the result code. Write any error message text into *pzErrMsg.
! 61808: */
! 61809: SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
! 61810: int rc = SQLITE_OK;
! 61811: if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
! 61812: rc = sqlite3VdbeReset(p);
! 61813: assert( (rc & p->db->errMask)==rc );
! 61814: }
! 61815: sqlite3VdbeDelete(p);
! 61816: return rc;
! 61817: }
! 61818:
! 61819: /*
! 61820: ** Call the destructor for each auxdata entry in pVdbeFunc for which
! 61821: ** the corresponding bit in mask is clear. Auxdata entries beyond 31
! 61822: ** are always destroyed. To destroy all auxdata entries, call this
! 61823: ** routine with mask==0.
! 61824: */
! 61825: SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
! 61826: int i;
! 61827: for(i=0; i<pVdbeFunc->nAux; i++){
! 61828: struct AuxData *pAux = &pVdbeFunc->apAux[i];
! 61829: if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){
! 61830: if( pAux->xDelete ){
! 61831: pAux->xDelete(pAux->pAux);
! 61832: }
! 61833: pAux->pAux = 0;
! 61834: }
! 61835: }
! 61836: }
! 61837:
! 61838: /*
! 61839: ** Free all memory associated with the Vdbe passed as the second argument.
! 61840: ** The difference between this function and sqlite3VdbeDelete() is that
! 61841: ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
! 61842: ** the database connection.
! 61843: */
! 61844: SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){
! 61845: SubProgram *pSub, *pNext;
! 61846: int i;
! 61847: assert( p->db==0 || p->db==db );
! 61848: releaseMemArray(p->aVar, p->nVar);
! 61849: releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
! 61850: for(pSub=p->pProgram; pSub; pSub=pNext){
! 61851: pNext = pSub->pNext;
! 61852: vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
! 61853: sqlite3DbFree(db, pSub);
! 61854: }
! 61855: for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
! 61856: vdbeFreeOpArray(db, p->aOp, p->nOp);
! 61857: sqlite3DbFree(db, p->aLabel);
! 61858: sqlite3DbFree(db, p->aColName);
! 61859: sqlite3DbFree(db, p->zSql);
! 61860: sqlite3DbFree(db, p->pFree);
! 61861: #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
! 61862: sqlite3DbFree(db, p->zExplain);
! 61863: sqlite3DbFree(db, p->pExplain);
! 61864: #endif
! 61865: sqlite3DbFree(db, p);
! 61866: }
! 61867:
! 61868: /*
! 61869: ** Delete an entire VDBE.
! 61870: */
! 61871: SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
! 61872: sqlite3 *db;
! 61873:
! 61874: if( NEVER(p==0) ) return;
! 61875: db = p->db;
! 61876: if( p->pPrev ){
! 61877: p->pPrev->pNext = p->pNext;
! 61878: }else{
! 61879: assert( db->pVdbe==p );
! 61880: db->pVdbe = p->pNext;
! 61881: }
! 61882: if( p->pNext ){
! 61883: p->pNext->pPrev = p->pPrev;
! 61884: }
! 61885: p->magic = VDBE_MAGIC_DEAD;
! 61886: p->db = 0;
! 61887: sqlite3VdbeDeleteObject(db, p);
! 61888: }
! 61889:
! 61890: /*
! 61891: ** Make sure the cursor p is ready to read or write the row to which it
! 61892: ** was last positioned. Return an error code if an OOM fault or I/O error
! 61893: ** prevents us from positioning the cursor to its correct position.
! 61894: **
! 61895: ** If a MoveTo operation is pending on the given cursor, then do that
! 61896: ** MoveTo now. If no move is pending, check to see if the row has been
! 61897: ** deleted out from under the cursor and if it has, mark the row as
! 61898: ** a NULL row.
! 61899: **
! 61900: ** If the cursor is already pointing to the correct row and that row has
! 61901: ** not been deleted out from under the cursor, then this routine is a no-op.
! 61902: */
! 61903: SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
! 61904: if( p->deferredMoveto ){
! 61905: int res, rc;
! 61906: #ifdef SQLITE_TEST
! 61907: extern int sqlite3_search_count;
! 61908: #endif
! 61909: assert( p->isTable );
! 61910: rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
! 61911: if( rc ) return rc;
! 61912: p->lastRowid = p->movetoTarget;
! 61913: if( res!=0 ) return SQLITE_CORRUPT_BKPT;
! 61914: p->rowidIsValid = 1;
! 61915: #ifdef SQLITE_TEST
! 61916: sqlite3_search_count++;
! 61917: #endif
! 61918: p->deferredMoveto = 0;
! 61919: p->cacheStatus = CACHE_STALE;
! 61920: }else if( ALWAYS(p->pCursor) ){
! 61921: int hasMoved;
! 61922: int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
! 61923: if( rc ) return rc;
! 61924: if( hasMoved ){
! 61925: p->cacheStatus = CACHE_STALE;
! 61926: p->nullRow = 1;
! 61927: }
! 61928: }
! 61929: return SQLITE_OK;
! 61930: }
! 61931:
! 61932: /*
! 61933: ** The following functions:
! 61934: **
! 61935: ** sqlite3VdbeSerialType()
! 61936: ** sqlite3VdbeSerialTypeLen()
! 61937: ** sqlite3VdbeSerialLen()
! 61938: ** sqlite3VdbeSerialPut()
! 61939: ** sqlite3VdbeSerialGet()
! 61940: **
! 61941: ** encapsulate the code that serializes values for storage in SQLite
! 61942: ** data and index records. Each serialized value consists of a
! 61943: ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
! 61944: ** integer, stored as a varint.
! 61945: **
! 61946: ** In an SQLite index record, the serial type is stored directly before
! 61947: ** the blob of data that it corresponds to. In a table record, all serial
! 61948: ** types are stored at the start of the record, and the blobs of data at
! 61949: ** the end. Hence these functions allow the caller to handle the
! 61950: ** serial-type and data blob seperately.
! 61951: **
! 61952: ** The following table describes the various storage classes for data:
! 61953: **
! 61954: ** serial type bytes of data type
! 61955: ** -------------- --------------- ---------------
! 61956: ** 0 0 NULL
! 61957: ** 1 1 signed integer
! 61958: ** 2 2 signed integer
! 61959: ** 3 3 signed integer
! 61960: ** 4 4 signed integer
! 61961: ** 5 6 signed integer
! 61962: ** 6 8 signed integer
! 61963: ** 7 8 IEEE float
! 61964: ** 8 0 Integer constant 0
! 61965: ** 9 0 Integer constant 1
! 61966: ** 10,11 reserved for expansion
! 61967: ** N>=12 and even (N-12)/2 BLOB
! 61968: ** N>=13 and odd (N-13)/2 text
! 61969: **
! 61970: ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
! 61971: ** of SQLite will not understand those serial types.
! 61972: */
! 61973:
! 61974: /*
! 61975: ** Return the serial-type for the value stored in pMem.
! 61976: */
! 61977: SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
! 61978: int flags = pMem->flags;
! 61979: int n;
! 61980:
! 61981: if( flags&MEM_Null ){
! 61982: return 0;
! 61983: }
! 61984: if( flags&MEM_Int ){
! 61985: /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
! 61986: # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
! 61987: i64 i = pMem->u.i;
! 61988: u64 u;
! 61989: if( file_format>=4 && (i&1)==i ){
! 61990: return 8+(u32)i;
! 61991: }
! 61992: if( i<0 ){
! 61993: if( i<(-MAX_6BYTE) ) return 6;
! 61994: /* Previous test prevents: u = -(-9223372036854775808) */
! 61995: u = -i;
! 61996: }else{
! 61997: u = i;
! 61998: }
! 61999: if( u<=127 ) return 1;
! 62000: if( u<=32767 ) return 2;
! 62001: if( u<=8388607 ) return 3;
! 62002: if( u<=2147483647 ) return 4;
! 62003: if( u<=MAX_6BYTE ) return 5;
! 62004: return 6;
! 62005: }
! 62006: if( flags&MEM_Real ){
! 62007: return 7;
! 62008: }
! 62009: assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
! 62010: n = pMem->n;
! 62011: if( flags & MEM_Zero ){
! 62012: n += pMem->u.nZero;
! 62013: }
! 62014: assert( n>=0 );
! 62015: return ((n*2) + 12 + ((flags&MEM_Str)!=0));
! 62016: }
! 62017:
! 62018: /*
! 62019: ** Return the length of the data corresponding to the supplied serial-type.
! 62020: */
! 62021: SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
! 62022: if( serial_type>=12 ){
! 62023: return (serial_type-12)/2;
! 62024: }else{
! 62025: static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
! 62026: return aSize[serial_type];
! 62027: }
! 62028: }
! 62029:
! 62030: /*
! 62031: ** If we are on an architecture with mixed-endian floating
! 62032: ** points (ex: ARM7) then swap the lower 4 bytes with the
! 62033: ** upper 4 bytes. Return the result.
! 62034: **
! 62035: ** For most architectures, this is a no-op.
! 62036: **
! 62037: ** (later): It is reported to me that the mixed-endian problem
! 62038: ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
! 62039: ** that early versions of GCC stored the two words of a 64-bit
! 62040: ** float in the wrong order. And that error has been propagated
! 62041: ** ever since. The blame is not necessarily with GCC, though.
! 62042: ** GCC might have just copying the problem from a prior compiler.
! 62043: ** I am also told that newer versions of GCC that follow a different
! 62044: ** ABI get the byte order right.
! 62045: **
! 62046: ** Developers using SQLite on an ARM7 should compile and run their
! 62047: ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
! 62048: ** enabled, some asserts below will ensure that the byte order of
! 62049: ** floating point values is correct.
! 62050: **
! 62051: ** (2007-08-30) Frank van Vugt has studied this problem closely
! 62052: ** and has send his findings to the SQLite developers. Frank
! 62053: ** writes that some Linux kernels offer floating point hardware
! 62054: ** emulation that uses only 32-bit mantissas instead of a full
! 62055: ** 48-bits as required by the IEEE standard. (This is the
! 62056: ** CONFIG_FPE_FASTFPE option.) On such systems, floating point
! 62057: ** byte swapping becomes very complicated. To avoid problems,
! 62058: ** the necessary byte swapping is carried out using a 64-bit integer
! 62059: ** rather than a 64-bit float. Frank assures us that the code here
! 62060: ** works for him. We, the developers, have no way to independently
! 62061: ** verify this, but Frank seems to know what he is talking about
! 62062: ** so we trust him.
! 62063: */
! 62064: #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
! 62065: static u64 floatSwap(u64 in){
! 62066: union {
! 62067: u64 r;
! 62068: u32 i[2];
! 62069: } u;
! 62070: u32 t;
! 62071:
! 62072: u.r = in;
! 62073: t = u.i[0];
! 62074: u.i[0] = u.i[1];
! 62075: u.i[1] = t;
! 62076: return u.r;
! 62077: }
! 62078: # define swapMixedEndianFloat(X) X = floatSwap(X)
! 62079: #else
! 62080: # define swapMixedEndianFloat(X)
! 62081: #endif
! 62082:
! 62083: /*
! 62084: ** Write the serialized data blob for the value stored in pMem into
! 62085: ** buf. It is assumed that the caller has allocated sufficient space.
! 62086: ** Return the number of bytes written.
! 62087: **
! 62088: ** nBuf is the amount of space left in buf[]. nBuf must always be
! 62089: ** large enough to hold the entire field. Except, if the field is
! 62090: ** a blob with a zero-filled tail, then buf[] might be just the right
! 62091: ** size to hold everything except for the zero-filled tail. If buf[]
! 62092: ** is only big enough to hold the non-zero prefix, then only write that
! 62093: ** prefix into buf[]. But if buf[] is large enough to hold both the
! 62094: ** prefix and the tail then write the prefix and set the tail to all
! 62095: ** zeros.
! 62096: **
! 62097: ** Return the number of bytes actually written into buf[]. The number
! 62098: ** of bytes in the zero-filled tail is included in the return value only
! 62099: ** if those bytes were zeroed in buf[].
! 62100: */
! 62101: SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
! 62102: u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
! 62103: u32 len;
! 62104:
! 62105: /* Integer and Real */
! 62106: if( serial_type<=7 && serial_type>0 ){
! 62107: u64 v;
! 62108: u32 i;
! 62109: if( serial_type==7 ){
! 62110: assert( sizeof(v)==sizeof(pMem->r) );
! 62111: memcpy(&v, &pMem->r, sizeof(v));
! 62112: swapMixedEndianFloat(v);
! 62113: }else{
! 62114: v = pMem->u.i;
! 62115: }
! 62116: len = i = sqlite3VdbeSerialTypeLen(serial_type);
! 62117: assert( len<=(u32)nBuf );
! 62118: while( i-- ){
! 62119: buf[i] = (u8)(v&0xFF);
! 62120: v >>= 8;
! 62121: }
! 62122: return len;
! 62123: }
! 62124:
! 62125: /* String or blob */
! 62126: if( serial_type>=12 ){
! 62127: assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
! 62128: == (int)sqlite3VdbeSerialTypeLen(serial_type) );
! 62129: assert( pMem->n<=nBuf );
! 62130: len = pMem->n;
! 62131: memcpy(buf, pMem->z, len);
! 62132: if( pMem->flags & MEM_Zero ){
! 62133: len += pMem->u.nZero;
! 62134: assert( nBuf>=0 );
! 62135: if( len > (u32)nBuf ){
! 62136: len = (u32)nBuf;
! 62137: }
! 62138: memset(&buf[pMem->n], 0, len-pMem->n);
! 62139: }
! 62140: return len;
! 62141: }
! 62142:
! 62143: /* NULL or constants 0 or 1 */
! 62144: return 0;
! 62145: }
! 62146:
! 62147: /*
! 62148: ** Deserialize the data blob pointed to by buf as serial type serial_type
! 62149: ** and store the result in pMem. Return the number of bytes read.
! 62150: */
! 62151: SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
! 62152: const unsigned char *buf, /* Buffer to deserialize from */
! 62153: u32 serial_type, /* Serial type to deserialize */
! 62154: Mem *pMem /* Memory cell to write value into */
! 62155: ){
! 62156: switch( serial_type ){
! 62157: case 10: /* Reserved for future use */
! 62158: case 11: /* Reserved for future use */
! 62159: case 0: { /* NULL */
! 62160: pMem->flags = MEM_Null;
! 62161: break;
! 62162: }
! 62163: case 1: { /* 1-byte signed integer */
! 62164: pMem->u.i = (signed char)buf[0];
! 62165: pMem->flags = MEM_Int;
! 62166: return 1;
! 62167: }
! 62168: case 2: { /* 2-byte signed integer */
! 62169: pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
! 62170: pMem->flags = MEM_Int;
! 62171: return 2;
! 62172: }
! 62173: case 3: { /* 3-byte signed integer */
! 62174: pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
! 62175: pMem->flags = MEM_Int;
! 62176: return 3;
! 62177: }
! 62178: case 4: { /* 4-byte signed integer */
! 62179: pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
! 62180: pMem->flags = MEM_Int;
! 62181: return 4;
! 62182: }
! 62183: case 5: { /* 6-byte signed integer */
! 62184: u64 x = (((signed char)buf[0])<<8) | buf[1];
! 62185: u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
! 62186: x = (x<<32) | y;
! 62187: pMem->u.i = *(i64*)&x;
! 62188: pMem->flags = MEM_Int;
! 62189: return 6;
! 62190: }
! 62191: case 6: /* 8-byte signed integer */
! 62192: case 7: { /* IEEE floating point */
! 62193: u64 x;
! 62194: u32 y;
! 62195: #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
! 62196: /* Verify that integers and floating point values use the same
! 62197: ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
! 62198: ** defined that 64-bit floating point values really are mixed
! 62199: ** endian.
! 62200: */
! 62201: static const u64 t1 = ((u64)0x3ff00000)<<32;
! 62202: static const double r1 = 1.0;
! 62203: u64 t2 = t1;
! 62204: swapMixedEndianFloat(t2);
! 62205: assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
! 62206: #endif
! 62207:
! 62208: x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
! 62209: y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
! 62210: x = (x<<32) | y;
! 62211: if( serial_type==6 ){
! 62212: pMem->u.i = *(i64*)&x;
! 62213: pMem->flags = MEM_Int;
! 62214: }else{
! 62215: assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
! 62216: swapMixedEndianFloat(x);
! 62217: memcpy(&pMem->r, &x, sizeof(x));
! 62218: pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
! 62219: }
! 62220: return 8;
! 62221: }
! 62222: case 8: /* Integer 0 */
! 62223: case 9: { /* Integer 1 */
! 62224: pMem->u.i = serial_type-8;
! 62225: pMem->flags = MEM_Int;
! 62226: return 0;
! 62227: }
! 62228: default: {
! 62229: u32 len = (serial_type-12)/2;
! 62230: pMem->z = (char *)buf;
! 62231: pMem->n = len;
! 62232: pMem->xDel = 0;
! 62233: if( serial_type&0x01 ){
! 62234: pMem->flags = MEM_Str | MEM_Ephem;
! 62235: }else{
! 62236: pMem->flags = MEM_Blob | MEM_Ephem;
! 62237: }
! 62238: return len;
! 62239: }
! 62240: }
! 62241: return 0;
! 62242: }
! 62243:
! 62244: /*
! 62245: ** This routine is used to allocate sufficient space for an UnpackedRecord
! 62246: ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
! 62247: ** the first argument is a pointer to KeyInfo structure pKeyInfo.
! 62248: **
! 62249: ** The space is either allocated using sqlite3DbMallocRaw() or from within
! 62250: ** the unaligned buffer passed via the second and third arguments (presumably
! 62251: ** stack space). If the former, then *ppFree is set to a pointer that should
! 62252: ** be eventually freed by the caller using sqlite3DbFree(). Or, if the
! 62253: ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
! 62254: ** before returning.
! 62255: **
! 62256: ** If an OOM error occurs, NULL is returned.
! 62257: */
! 62258: SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
! 62259: KeyInfo *pKeyInfo, /* Description of the record */
! 62260: char *pSpace, /* Unaligned space available */
! 62261: int szSpace, /* Size of pSpace[] in bytes */
! 62262: char **ppFree /* OUT: Caller should free this pointer */
! 62263: ){
! 62264: UnpackedRecord *p; /* Unpacked record to return */
! 62265: int nOff; /* Increment pSpace by nOff to align it */
! 62266: int nByte; /* Number of bytes required for *p */
! 62267:
! 62268: /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
! 62269: ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
! 62270: ** it by. If pSpace is already 8-byte aligned, nOff should be zero.
! 62271: */
! 62272: nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
! 62273: nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
! 62274: if( nByte>szSpace+nOff ){
! 62275: p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
! 62276: *ppFree = (char *)p;
! 62277: if( !p ) return 0;
! 62278: }else{
! 62279: p = (UnpackedRecord*)&pSpace[nOff];
! 62280: *ppFree = 0;
! 62281: }
! 62282:
! 62283: p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
! 62284: p->pKeyInfo = pKeyInfo;
! 62285: p->nField = pKeyInfo->nField + 1;
! 62286: return p;
! 62287: }
! 62288:
! 62289: /*
! 62290: ** Given the nKey-byte encoding of a record in pKey[], populate the
! 62291: ** UnpackedRecord structure indicated by the fourth argument with the
! 62292: ** contents of the decoded record.
! 62293: */
! 62294: SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
! 62295: KeyInfo *pKeyInfo, /* Information about the record format */
! 62296: int nKey, /* Size of the binary record */
! 62297: const void *pKey, /* The binary record */
! 62298: UnpackedRecord *p /* Populate this structure before returning. */
! 62299: ){
! 62300: const unsigned char *aKey = (const unsigned char *)pKey;
! 62301: int d;
! 62302: u32 idx; /* Offset in aKey[] to read from */
! 62303: u16 u; /* Unsigned loop counter */
! 62304: u32 szHdr;
! 62305: Mem *pMem = p->aMem;
! 62306:
! 62307: p->flags = 0;
! 62308: assert( EIGHT_BYTE_ALIGNMENT(pMem) );
! 62309: idx = getVarint32(aKey, szHdr);
! 62310: d = szHdr;
! 62311: u = 0;
! 62312: while( idx<szHdr && u<p->nField && d<=nKey ){
! 62313: u32 serial_type;
! 62314:
! 62315: idx += getVarint32(&aKey[idx], serial_type);
! 62316: pMem->enc = pKeyInfo->enc;
! 62317: pMem->db = pKeyInfo->db;
! 62318: /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
! 62319: pMem->zMalloc = 0;
! 62320: d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
! 62321: pMem++;
! 62322: u++;
! 62323: }
! 62324: assert( u<=pKeyInfo->nField + 1 );
! 62325: p->nField = u;
! 62326: }
! 62327:
! 62328: /*
! 62329: ** This function compares the two table rows or index records
! 62330: ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
! 62331: ** or positive integer if key1 is less than, equal to or
! 62332: ** greater than key2. The {nKey1, pKey1} key must be a blob
! 62333: ** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
! 62334: ** key must be a parsed key such as obtained from
! 62335: ** sqlite3VdbeParseRecord.
! 62336: **
! 62337: ** Key1 and Key2 do not have to contain the same number of fields.
! 62338: ** The key with fewer fields is usually compares less than the
! 62339: ** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set
! 62340: ** and the common prefixes are equal, then key1 is less than key2.
! 62341: ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
! 62342: ** equal, then the keys are considered to be equal and
! 62343: ** the parts beyond the common prefix are ignored.
! 62344: */
! 62345: SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
! 62346: int nKey1, const void *pKey1, /* Left key */
! 62347: UnpackedRecord *pPKey2 /* Right key */
! 62348: ){
! 62349: int d1; /* Offset into aKey[] of next data element */
! 62350: u32 idx1; /* Offset into aKey[] of next header element */
! 62351: u32 szHdr1; /* Number of bytes in header */
! 62352: int i = 0;
! 62353: int nField;
! 62354: int rc = 0;
! 62355: const unsigned char *aKey1 = (const unsigned char *)pKey1;
! 62356: KeyInfo *pKeyInfo;
! 62357: Mem mem1;
! 62358:
! 62359: pKeyInfo = pPKey2->pKeyInfo;
! 62360: mem1.enc = pKeyInfo->enc;
! 62361: mem1.db = pKeyInfo->db;
! 62362: /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
! 62363: VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
! 62364:
! 62365: /* Compilers may complain that mem1.u.i is potentially uninitialized.
! 62366: ** We could initialize it, as shown here, to silence those complaints.
! 62367: ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
! 62368: ** the unnecessary initialization has a measurable negative performance
! 62369: ** impact, since this routine is a very high runner. And so, we choose
! 62370: ** to ignore the compiler warnings and leave this variable uninitialized.
! 62371: */
! 62372: /* mem1.u.i = 0; // not needed, here to silence compiler warning */
! 62373:
! 62374: idx1 = getVarint32(aKey1, szHdr1);
! 62375: d1 = szHdr1;
! 62376: nField = pKeyInfo->nField;
! 62377: while( idx1<szHdr1 && i<pPKey2->nField ){
! 62378: u32 serial_type1;
! 62379:
! 62380: /* Read the serial types for the next element in each key. */
! 62381: idx1 += getVarint32( aKey1+idx1, serial_type1 );
! 62382: if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
! 62383:
! 62384: /* Extract the values to be compared.
! 62385: */
! 62386: d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
! 62387:
! 62388: /* Do the comparison
! 62389: */
! 62390: rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
! 62391: i<nField ? pKeyInfo->aColl[i] : 0);
! 62392: if( rc!=0 ){
! 62393: assert( mem1.zMalloc==0 ); /* See comment below */
! 62394:
! 62395: /* Invert the result if we are using DESC sort order. */
! 62396: if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){
! 62397: rc = -rc;
! 62398: }
! 62399:
! 62400: /* If the PREFIX_SEARCH flag is set and all fields except the final
! 62401: ** rowid field were equal, then clear the PREFIX_SEARCH flag and set
! 62402: ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
! 62403: ** This is used by the OP_IsUnique opcode.
! 62404: */
! 62405: if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
! 62406: assert( idx1==szHdr1 && rc );
! 62407: assert( mem1.flags & MEM_Int );
! 62408: pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
! 62409: pPKey2->rowid = mem1.u.i;
! 62410: }
! 62411:
! 62412: return rc;
! 62413: }
! 62414: i++;
! 62415: }
! 62416:
! 62417: /* No memory allocation is ever used on mem1. Prove this using
! 62418: ** the following assert(). If the assert() fails, it indicates a
! 62419: ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
! 62420: */
! 62421: assert( mem1.zMalloc==0 );
! 62422:
! 62423: /* rc==0 here means that one of the keys ran out of fields and
! 62424: ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
! 62425: ** flag is set, then break the tie by treating key2 as larger.
! 62426: ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
! 62427: ** are considered to be equal. Otherwise, the longer key is the
! 62428: ** larger. As it happens, the pPKey2 will always be the longer
! 62429: ** if there is a difference.
! 62430: */
! 62431: assert( rc==0 );
! 62432: if( pPKey2->flags & UNPACKED_INCRKEY ){
! 62433: rc = -1;
! 62434: }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
! 62435: /* Leave rc==0 */
! 62436: }else if( idx1<szHdr1 ){
! 62437: rc = 1;
! 62438: }
! 62439: return rc;
! 62440: }
! 62441:
! 62442:
! 62443: /*
! 62444: ** pCur points at an index entry created using the OP_MakeRecord opcode.
! 62445: ** Read the rowid (the last field in the record) and store it in *rowid.
! 62446: ** Return SQLITE_OK if everything works, or an error code otherwise.
! 62447: **
! 62448: ** pCur might be pointing to text obtained from a corrupt database file.
! 62449: ** So the content cannot be trusted. Do appropriate checks on the content.
! 62450: */
! 62451: SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
! 62452: i64 nCellKey = 0;
! 62453: int rc;
! 62454: u32 szHdr; /* Size of the header */
! 62455: u32 typeRowid; /* Serial type of the rowid */
! 62456: u32 lenRowid; /* Size of the rowid */
! 62457: Mem m, v;
! 62458:
! 62459: UNUSED_PARAMETER(db);
! 62460:
! 62461: /* Get the size of the index entry. Only indices entries of less
! 62462: ** than 2GiB are support - anything large must be database corruption.
! 62463: ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
! 62464: ** this code can safely assume that nCellKey is 32-bits
! 62465: */
! 62466: assert( sqlite3BtreeCursorIsValid(pCur) );
! 62467: VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
! 62468: assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
! 62469: assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
! 62470:
! 62471: /* Read in the complete content of the index entry */
! 62472: memset(&m, 0, sizeof(m));
! 62473: rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
! 62474: if( rc ){
! 62475: return rc;
! 62476: }
! 62477:
! 62478: /* The index entry must begin with a header size */
! 62479: (void)getVarint32((u8*)m.z, szHdr);
! 62480: testcase( szHdr==3 );
! 62481: testcase( szHdr==m.n );
! 62482: if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
! 62483: goto idx_rowid_corruption;
! 62484: }
! 62485:
! 62486: /* The last field of the index should be an integer - the ROWID.
! 62487: ** Verify that the last entry really is an integer. */
! 62488: (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
! 62489: testcase( typeRowid==1 );
! 62490: testcase( typeRowid==2 );
! 62491: testcase( typeRowid==3 );
! 62492: testcase( typeRowid==4 );
! 62493: testcase( typeRowid==5 );
! 62494: testcase( typeRowid==6 );
! 62495: testcase( typeRowid==8 );
! 62496: testcase( typeRowid==9 );
! 62497: if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
! 62498: goto idx_rowid_corruption;
! 62499: }
! 62500: lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
! 62501: testcase( (u32)m.n==szHdr+lenRowid );
! 62502: if( unlikely((u32)m.n<szHdr+lenRowid) ){
! 62503: goto idx_rowid_corruption;
! 62504: }
! 62505:
! 62506: /* Fetch the integer off the end of the index record */
! 62507: sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
! 62508: *rowid = v.u.i;
! 62509: sqlite3VdbeMemRelease(&m);
! 62510: return SQLITE_OK;
! 62511:
! 62512: /* Jump here if database corruption is detected after m has been
! 62513: ** allocated. Free the m object and return SQLITE_CORRUPT. */
! 62514: idx_rowid_corruption:
! 62515: testcase( m.zMalloc!=0 );
! 62516: sqlite3VdbeMemRelease(&m);
! 62517: return SQLITE_CORRUPT_BKPT;
! 62518: }
! 62519:
! 62520: /*
! 62521: ** Compare the key of the index entry that cursor pC is pointing to against
! 62522: ** the key string in pUnpacked. Write into *pRes a number
! 62523: ** that is negative, zero, or positive if pC is less than, equal to,
! 62524: ** or greater than pUnpacked. Return SQLITE_OK on success.
! 62525: **
! 62526: ** pUnpacked is either created without a rowid or is truncated so that it
! 62527: ** omits the rowid at the end. The rowid at the end of the index entry
! 62528: ** is ignored as well. Hence, this routine only compares the prefixes
! 62529: ** of the keys prior to the final rowid, not the entire key.
! 62530: */
! 62531: SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
! 62532: VdbeCursor *pC, /* The cursor to compare against */
! 62533: UnpackedRecord *pUnpacked, /* Unpacked version of key to compare against */
! 62534: int *res /* Write the comparison result here */
! 62535: ){
! 62536: i64 nCellKey = 0;
! 62537: int rc;
! 62538: BtCursor *pCur = pC->pCursor;
! 62539: Mem m;
! 62540:
! 62541: assert( sqlite3BtreeCursorIsValid(pCur) );
! 62542: VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
! 62543: assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
! 62544: /* nCellKey will always be between 0 and 0xffffffff because of the say
! 62545: ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
! 62546: if( nCellKey<=0 || nCellKey>0x7fffffff ){
! 62547: *res = 0;
! 62548: return SQLITE_CORRUPT_BKPT;
! 62549: }
! 62550: memset(&m, 0, sizeof(m));
! 62551: rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
! 62552: if( rc ){
! 62553: return rc;
! 62554: }
! 62555: assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
! 62556: *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
! 62557: sqlite3VdbeMemRelease(&m);
! 62558: return SQLITE_OK;
! 62559: }
! 62560:
! 62561: /*
! 62562: ** This routine sets the value to be returned by subsequent calls to
! 62563: ** sqlite3_changes() on the database handle 'db'.
! 62564: */
! 62565: SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
! 62566: assert( sqlite3_mutex_held(db->mutex) );
! 62567: db->nChange = nChange;
! 62568: db->nTotalChange += nChange;
! 62569: }
! 62570:
! 62571: /*
! 62572: ** Set a flag in the vdbe to update the change counter when it is finalised
! 62573: ** or reset.
! 62574: */
! 62575: SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
! 62576: v->changeCntOn = 1;
! 62577: }
! 62578:
! 62579: /*
! 62580: ** Mark every prepared statement associated with a database connection
! 62581: ** as expired.
! 62582: **
! 62583: ** An expired statement means that recompilation of the statement is
! 62584: ** recommend. Statements expire when things happen that make their
! 62585: ** programs obsolete. Removing user-defined functions or collating
! 62586: ** sequences, or changing an authorization function are the types of
! 62587: ** things that make prepared statements obsolete.
! 62588: */
! 62589: SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
! 62590: Vdbe *p;
! 62591: for(p = db->pVdbe; p; p=p->pNext){
! 62592: p->expired = 1;
! 62593: }
! 62594: }
! 62595:
! 62596: /*
! 62597: ** Return the database associated with the Vdbe.
! 62598: */
! 62599: SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
! 62600: return v->db;
! 62601: }
! 62602:
! 62603: /*
! 62604: ** Return a pointer to an sqlite3_value structure containing the value bound
! 62605: ** parameter iVar of VM v. Except, if the value is an SQL NULL, return
! 62606: ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
! 62607: ** constants) to the value before returning it.
! 62608: **
! 62609: ** The returned value must be freed by the caller using sqlite3ValueFree().
! 62610: */
! 62611: SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){
! 62612: assert( iVar>0 );
! 62613: if( v ){
! 62614: Mem *pMem = &v->aVar[iVar-1];
! 62615: if( 0==(pMem->flags & MEM_Null) ){
! 62616: sqlite3_value *pRet = sqlite3ValueNew(v->db);
! 62617: if( pRet ){
! 62618: sqlite3VdbeMemCopy((Mem *)pRet, pMem);
! 62619: sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
! 62620: sqlite3VdbeMemStoreType((Mem *)pRet);
! 62621: }
! 62622: return pRet;
! 62623: }
! 62624: }
! 62625: return 0;
! 62626: }
! 62627:
! 62628: /*
! 62629: ** Configure SQL variable iVar so that binding a new value to it signals
! 62630: ** to sqlite3_reoptimize() that re-preparing the statement may result
! 62631: ** in a better query plan.
! 62632: */
! 62633: SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
! 62634: assert( iVar>0 );
! 62635: if( iVar>32 ){
! 62636: v->expmask = 0xffffffff;
! 62637: }else{
! 62638: v->expmask |= ((u32)1 << (iVar-1));
! 62639: }
! 62640: }
! 62641:
! 62642: /************** End of vdbeaux.c *********************************************/
! 62643: /************** Begin file vdbeapi.c *****************************************/
! 62644: /*
! 62645: ** 2004 May 26
! 62646: **
! 62647: ** The author disclaims copyright to this source code. In place of
! 62648: ** a legal notice, here is a blessing:
! 62649: **
! 62650: ** May you do good and not evil.
! 62651: ** May you find forgiveness for yourself and forgive others.
! 62652: ** May you share freely, never taking more than you give.
! 62653: **
! 62654: *************************************************************************
! 62655: **
! 62656: ** This file contains code use to implement APIs that are part of the
! 62657: ** VDBE.
! 62658: */
! 62659:
! 62660: #ifndef SQLITE_OMIT_DEPRECATED
! 62661: /*
! 62662: ** Return TRUE (non-zero) of the statement supplied as an argument needs
! 62663: ** to be recompiled. A statement needs to be recompiled whenever the
! 62664: ** execution environment changes in a way that would alter the program
! 62665: ** that sqlite3_prepare() generates. For example, if new functions or
! 62666: ** collating sequences are registered or if an authorizer function is
! 62667: ** added or changed.
! 62668: */
! 62669: SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
! 62670: Vdbe *p = (Vdbe*)pStmt;
! 62671: return p==0 || p->expired;
! 62672: }
! 62673: #endif
! 62674:
! 62675: /*
! 62676: ** Check on a Vdbe to make sure it has not been finalized. Log
! 62677: ** an error and return true if it has been finalized (or is otherwise
! 62678: ** invalid). Return false if it is ok.
! 62679: */
! 62680: static int vdbeSafety(Vdbe *p){
! 62681: if( p->db==0 ){
! 62682: sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
! 62683: return 1;
! 62684: }else{
! 62685: return 0;
! 62686: }
! 62687: }
! 62688: static int vdbeSafetyNotNull(Vdbe *p){
! 62689: if( p==0 ){
! 62690: sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
! 62691: return 1;
! 62692: }else{
! 62693: return vdbeSafety(p);
! 62694: }
! 62695: }
! 62696:
! 62697: /*
! 62698: ** The following routine destroys a virtual machine that is created by
! 62699: ** the sqlite3_compile() routine. The integer returned is an SQLITE_
! 62700: ** success/failure code that describes the result of executing the virtual
! 62701: ** machine.
! 62702: **
! 62703: ** This routine sets the error code and string returned by
! 62704: ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
! 62705: */
! 62706: SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
! 62707: int rc;
! 62708: if( pStmt==0 ){
! 62709: /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
! 62710: ** pointer is a harmless no-op. */
! 62711: rc = SQLITE_OK;
! 62712: }else{
! 62713: Vdbe *v = (Vdbe*)pStmt;
! 62714: sqlite3 *db = v->db;
! 62715: #if SQLITE_THREADSAFE
! 62716: sqlite3_mutex *mutex;
! 62717: #endif
! 62718: if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
! 62719: #if SQLITE_THREADSAFE
! 62720: mutex = v->db->mutex;
! 62721: #endif
! 62722: sqlite3_mutex_enter(mutex);
! 62723: rc = sqlite3VdbeFinalize(v);
! 62724: rc = sqlite3ApiExit(db, rc);
! 62725: sqlite3_mutex_leave(mutex);
! 62726: }
! 62727: return rc;
! 62728: }
! 62729:
! 62730: /*
! 62731: ** Terminate the current execution of an SQL statement and reset it
! 62732: ** back to its starting state so that it can be reused. A success code from
! 62733: ** the prior execution is returned.
! 62734: **
! 62735: ** This routine sets the error code and string returned by
! 62736: ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
! 62737: */
! 62738: SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
! 62739: int rc;
! 62740: if( pStmt==0 ){
! 62741: rc = SQLITE_OK;
! 62742: }else{
! 62743: Vdbe *v = (Vdbe*)pStmt;
! 62744: sqlite3_mutex_enter(v->db->mutex);
! 62745: rc = sqlite3VdbeReset(v);
! 62746: sqlite3VdbeRewind(v);
! 62747: assert( (rc & (v->db->errMask))==rc );
! 62748: rc = sqlite3ApiExit(v->db, rc);
! 62749: sqlite3_mutex_leave(v->db->mutex);
! 62750: }
! 62751: return rc;
! 62752: }
! 62753:
! 62754: /*
! 62755: ** Set all the parameters in the compiled SQL statement to NULL.
! 62756: */
! 62757: SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
! 62758: int i;
! 62759: int rc = SQLITE_OK;
! 62760: Vdbe *p = (Vdbe*)pStmt;
! 62761: #if SQLITE_THREADSAFE
! 62762: sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
! 62763: #endif
! 62764: sqlite3_mutex_enter(mutex);
! 62765: for(i=0; i<p->nVar; i++){
! 62766: sqlite3VdbeMemRelease(&p->aVar[i]);
! 62767: p->aVar[i].flags = MEM_Null;
! 62768: }
! 62769: if( p->isPrepareV2 && p->expmask ){
! 62770: p->expired = 1;
! 62771: }
! 62772: sqlite3_mutex_leave(mutex);
! 62773: return rc;
! 62774: }
! 62775:
! 62776:
! 62777: /**************************** sqlite3_value_ *******************************
! 62778: ** The following routines extract information from a Mem or sqlite3_value
! 62779: ** structure.
! 62780: */
! 62781: SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
! 62782: Mem *p = (Mem*)pVal;
! 62783: if( p->flags & (MEM_Blob|MEM_Str) ){
! 62784: sqlite3VdbeMemExpandBlob(p);
! 62785: p->flags &= ~MEM_Str;
! 62786: p->flags |= MEM_Blob;
! 62787: return p->n ? p->z : 0;
! 62788: }else{
! 62789: return sqlite3_value_text(pVal);
! 62790: }
! 62791: }
! 62792: SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
! 62793: return sqlite3ValueBytes(pVal, SQLITE_UTF8);
! 62794: }
! 62795: SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
! 62796: return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
! 62797: }
! 62798: SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
! 62799: return sqlite3VdbeRealValue((Mem*)pVal);
! 62800: }
! 62801: SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
! 62802: return (int)sqlite3VdbeIntValue((Mem*)pVal);
! 62803: }
! 62804: SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
! 62805: return sqlite3VdbeIntValue((Mem*)pVal);
! 62806: }
! 62807: SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
! 62808: return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
! 62809: }
! 62810: #ifndef SQLITE_OMIT_UTF16
! 62811: SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
! 62812: return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
! 62813: }
! 62814: SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
! 62815: return sqlite3ValueText(pVal, SQLITE_UTF16BE);
! 62816: }
! 62817: SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
! 62818: return sqlite3ValueText(pVal, SQLITE_UTF16LE);
! 62819: }
! 62820: #endif /* SQLITE_OMIT_UTF16 */
! 62821: SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
! 62822: return pVal->type;
! 62823: }
! 62824:
! 62825: /**************************** sqlite3_result_ *******************************
! 62826: ** The following routines are used by user-defined functions to specify
! 62827: ** the function result.
! 62828: **
! 62829: ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
! 62830: ** result as a string or blob but if the string or blob is too large, it
! 62831: ** then sets the error code to SQLITE_TOOBIG
! 62832: */
! 62833: static void setResultStrOrError(
! 62834: sqlite3_context *pCtx, /* Function context */
! 62835: const char *z, /* String pointer */
! 62836: int n, /* Bytes in string, or negative */
! 62837: u8 enc, /* Encoding of z. 0 for BLOBs */
! 62838: void (*xDel)(void*) /* Destructor function */
! 62839: ){
! 62840: if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
! 62841: sqlite3_result_error_toobig(pCtx);
! 62842: }
! 62843: }
! 62844: SQLITE_API void sqlite3_result_blob(
! 62845: sqlite3_context *pCtx,
! 62846: const void *z,
! 62847: int n,
! 62848: void (*xDel)(void *)
! 62849: ){
! 62850: assert( n>=0 );
! 62851: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62852: setResultStrOrError(pCtx, z, n, 0, xDel);
! 62853: }
! 62854: SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
! 62855: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62856: sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
! 62857: }
! 62858: SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
! 62859: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62860: pCtx->isError = SQLITE_ERROR;
! 62861: sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
! 62862: }
! 62863: #ifndef SQLITE_OMIT_UTF16
! 62864: SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
! 62865: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62866: pCtx->isError = SQLITE_ERROR;
! 62867: sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
! 62868: }
! 62869: #endif
! 62870: SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
! 62871: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62872: sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
! 62873: }
! 62874: SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
! 62875: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62876: sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
! 62877: }
! 62878: SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
! 62879: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62880: sqlite3VdbeMemSetNull(&pCtx->s);
! 62881: }
! 62882: SQLITE_API void sqlite3_result_text(
! 62883: sqlite3_context *pCtx,
! 62884: const char *z,
! 62885: int n,
! 62886: void (*xDel)(void *)
! 62887: ){
! 62888: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62889: setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
! 62890: }
! 62891: #ifndef SQLITE_OMIT_UTF16
! 62892: SQLITE_API void sqlite3_result_text16(
! 62893: sqlite3_context *pCtx,
! 62894: const void *z,
! 62895: int n,
! 62896: void (*xDel)(void *)
! 62897: ){
! 62898: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62899: setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
! 62900: }
! 62901: SQLITE_API void sqlite3_result_text16be(
! 62902: sqlite3_context *pCtx,
! 62903: const void *z,
! 62904: int n,
! 62905: void (*xDel)(void *)
! 62906: ){
! 62907: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62908: setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
! 62909: }
! 62910: SQLITE_API void sqlite3_result_text16le(
! 62911: sqlite3_context *pCtx,
! 62912: const void *z,
! 62913: int n,
! 62914: void (*xDel)(void *)
! 62915: ){
! 62916: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62917: setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
! 62918: }
! 62919: #endif /* SQLITE_OMIT_UTF16 */
! 62920: SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
! 62921: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62922: sqlite3VdbeMemCopy(&pCtx->s, pValue);
! 62923: }
! 62924: SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
! 62925: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62926: sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
! 62927: }
! 62928: SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
! 62929: pCtx->isError = errCode;
! 62930: if( pCtx->s.flags & MEM_Null ){
! 62931: sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
! 62932: SQLITE_UTF8, SQLITE_STATIC);
! 62933: }
! 62934: }
! 62935:
! 62936: /* Force an SQLITE_TOOBIG error. */
! 62937: SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
! 62938: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62939: pCtx->isError = SQLITE_TOOBIG;
! 62940: sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
! 62941: SQLITE_UTF8, SQLITE_STATIC);
! 62942: }
! 62943:
! 62944: /* An SQLITE_NOMEM error. */
! 62945: SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
! 62946: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 62947: sqlite3VdbeMemSetNull(&pCtx->s);
! 62948: pCtx->isError = SQLITE_NOMEM;
! 62949: pCtx->s.db->mallocFailed = 1;
! 62950: }
! 62951:
! 62952: /*
! 62953: ** This function is called after a transaction has been committed. It
! 62954: ** invokes callbacks registered with sqlite3_wal_hook() as required.
! 62955: */
! 62956: static int doWalCallbacks(sqlite3 *db){
! 62957: int rc = SQLITE_OK;
! 62958: #ifndef SQLITE_OMIT_WAL
! 62959: int i;
! 62960: for(i=0; i<db->nDb; i++){
! 62961: Btree *pBt = db->aDb[i].pBt;
! 62962: if( pBt ){
! 62963: int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
! 62964: if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
! 62965: rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
! 62966: }
! 62967: }
! 62968: }
! 62969: #endif
! 62970: return rc;
! 62971: }
! 62972:
! 62973: /*
! 62974: ** Execute the statement pStmt, either until a row of data is ready, the
! 62975: ** statement is completely executed or an error occurs.
! 62976: **
! 62977: ** This routine implements the bulk of the logic behind the sqlite_step()
! 62978: ** API. The only thing omitted is the automatic recompile if a
! 62979: ** schema change has occurred. That detail is handled by the
! 62980: ** outer sqlite3_step() wrapper procedure.
! 62981: */
! 62982: static int sqlite3Step(Vdbe *p){
! 62983: sqlite3 *db;
! 62984: int rc;
! 62985:
! 62986: assert(p);
! 62987: if( p->magic!=VDBE_MAGIC_RUN ){
! 62988: /* We used to require that sqlite3_reset() be called before retrying
! 62989: ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
! 62990: ** with version 3.7.0, we changed this so that sqlite3_reset() would
! 62991: ** be called automatically instead of throwing the SQLITE_MISUSE error.
! 62992: ** This "automatic-reset" change is not technically an incompatibility,
! 62993: ** since any application that receives an SQLITE_MISUSE is broken by
! 62994: ** definition.
! 62995: **
! 62996: ** Nevertheless, some published applications that were originally written
! 62997: ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
! 62998: ** returns, and those were broken by the automatic-reset change. As a
! 62999: ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
! 63000: ** legacy behavior of returning SQLITE_MISUSE for cases where the
! 63001: ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
! 63002: ** or SQLITE_BUSY error.
! 63003: */
! 63004: #ifdef SQLITE_OMIT_AUTORESET
! 63005: if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
! 63006: sqlite3_reset((sqlite3_stmt*)p);
! 63007: }else{
! 63008: return SQLITE_MISUSE_BKPT;
! 63009: }
! 63010: #else
! 63011: sqlite3_reset((sqlite3_stmt*)p);
! 63012: #endif
! 63013: }
! 63014:
! 63015: /* Check that malloc() has not failed. If it has, return early. */
! 63016: db = p->db;
! 63017: if( db->mallocFailed ){
! 63018: p->rc = SQLITE_NOMEM;
! 63019: return SQLITE_NOMEM;
! 63020: }
! 63021:
! 63022: if( p->pc<=0 && p->expired ){
! 63023: p->rc = SQLITE_SCHEMA;
! 63024: rc = SQLITE_ERROR;
! 63025: goto end_of_step;
! 63026: }
! 63027: if( p->pc<0 ){
! 63028: /* If there are no other statements currently running, then
! 63029: ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
! 63030: ** from interrupting a statement that has not yet started.
! 63031: */
! 63032: if( db->activeVdbeCnt==0 ){
! 63033: db->u1.isInterrupted = 0;
! 63034: }
! 63035:
! 63036: assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
! 63037:
! 63038: #ifndef SQLITE_OMIT_TRACE
! 63039: if( db->xProfile && !db->init.busy ){
! 63040: sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
! 63041: }
! 63042: #endif
! 63043:
! 63044: db->activeVdbeCnt++;
! 63045: if( p->readOnly==0 ) db->writeVdbeCnt++;
! 63046: p->pc = 0;
! 63047: }
! 63048: #ifndef SQLITE_OMIT_EXPLAIN
! 63049: if( p->explain ){
! 63050: rc = sqlite3VdbeList(p);
! 63051: }else
! 63052: #endif /* SQLITE_OMIT_EXPLAIN */
! 63053: {
! 63054: db->vdbeExecCnt++;
! 63055: rc = sqlite3VdbeExec(p);
! 63056: db->vdbeExecCnt--;
! 63057: }
! 63058:
! 63059: #ifndef SQLITE_OMIT_TRACE
! 63060: /* Invoke the profile callback if there is one
! 63061: */
! 63062: if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
! 63063: sqlite3_int64 iNow;
! 63064: sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
! 63065: db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
! 63066: }
! 63067: #endif
! 63068:
! 63069: if( rc==SQLITE_DONE ){
! 63070: assert( p->rc==SQLITE_OK );
! 63071: p->rc = doWalCallbacks(db);
! 63072: if( p->rc!=SQLITE_OK ){
! 63073: rc = SQLITE_ERROR;
! 63074: }
! 63075: }
! 63076:
! 63077: db->errCode = rc;
! 63078: if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
! 63079: p->rc = SQLITE_NOMEM;
! 63080: }
! 63081: end_of_step:
! 63082: /* At this point local variable rc holds the value that should be
! 63083: ** returned if this statement was compiled using the legacy
! 63084: ** sqlite3_prepare() interface. According to the docs, this can only
! 63085: ** be one of the values in the first assert() below. Variable p->rc
! 63086: ** contains the value that would be returned if sqlite3_finalize()
! 63087: ** were called on statement p.
! 63088: */
! 63089: assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
! 63090: || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
! 63091: );
! 63092: assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
! 63093: if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
! 63094: /* If this statement was prepared using sqlite3_prepare_v2(), and an
! 63095: ** error has occured, then return the error code in p->rc to the
! 63096: ** caller. Set the error code in the database handle to the same value.
! 63097: */
! 63098: rc = sqlite3VdbeTransferError(p);
! 63099: }
! 63100: return (rc&db->errMask);
! 63101: }
! 63102:
! 63103: /*
! 63104: ** The maximum number of times that a statement will try to reparse
! 63105: ** itself before giving up and returning SQLITE_SCHEMA.
! 63106: */
! 63107: #ifndef SQLITE_MAX_SCHEMA_RETRY
! 63108: # define SQLITE_MAX_SCHEMA_RETRY 5
! 63109: #endif
! 63110:
! 63111: /*
! 63112: ** This is the top-level implementation of sqlite3_step(). Call
! 63113: ** sqlite3Step() to do most of the work. If a schema error occurs,
! 63114: ** call sqlite3Reprepare() and try again.
! 63115: */
! 63116: SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
! 63117: int rc = SQLITE_OK; /* Result from sqlite3Step() */
! 63118: int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */
! 63119: Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
! 63120: int cnt = 0; /* Counter to prevent infinite loop of reprepares */
! 63121: sqlite3 *db; /* The database connection */
! 63122:
! 63123: if( vdbeSafetyNotNull(v) ){
! 63124: return SQLITE_MISUSE_BKPT;
! 63125: }
! 63126: db = v->db;
! 63127: sqlite3_mutex_enter(db->mutex);
! 63128: while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
! 63129: && cnt++ < SQLITE_MAX_SCHEMA_RETRY
! 63130: && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
! 63131: sqlite3_reset(pStmt);
! 63132: assert( v->expired==0 );
! 63133: }
! 63134: if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
! 63135: /* This case occurs after failing to recompile an sql statement.
! 63136: ** The error message from the SQL compiler has already been loaded
! 63137: ** into the database handle. This block copies the error message
! 63138: ** from the database handle into the statement and sets the statement
! 63139: ** program counter to 0 to ensure that when the statement is
! 63140: ** finalized or reset the parser error message is available via
! 63141: ** sqlite3_errmsg() and sqlite3_errcode().
! 63142: */
! 63143: const char *zErr = (const char *)sqlite3_value_text(db->pErr);
! 63144: sqlite3DbFree(db, v->zErrMsg);
! 63145: if( !db->mallocFailed ){
! 63146: v->zErrMsg = sqlite3DbStrDup(db, zErr);
! 63147: v->rc = rc2;
! 63148: } else {
! 63149: v->zErrMsg = 0;
! 63150: v->rc = rc = SQLITE_NOMEM;
! 63151: }
! 63152: }
! 63153: rc = sqlite3ApiExit(db, rc);
! 63154: sqlite3_mutex_leave(db->mutex);
! 63155: return rc;
! 63156: }
! 63157:
! 63158: /*
! 63159: ** Extract the user data from a sqlite3_context structure and return a
! 63160: ** pointer to it.
! 63161: */
! 63162: SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
! 63163: assert( p && p->pFunc );
! 63164: return p->pFunc->pUserData;
! 63165: }
! 63166:
! 63167: /*
! 63168: ** Extract the user data from a sqlite3_context structure and return a
! 63169: ** pointer to it.
! 63170: **
! 63171: ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
! 63172: ** returns a copy of the pointer to the database connection (the 1st
! 63173: ** parameter) of the sqlite3_create_function() and
! 63174: ** sqlite3_create_function16() routines that originally registered the
! 63175: ** application defined function.
! 63176: */
! 63177: SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
! 63178: assert( p && p->pFunc );
! 63179: return p->s.db;
! 63180: }
! 63181:
! 63182: /*
! 63183: ** The following is the implementation of an SQL function that always
! 63184: ** fails with an error message stating that the function is used in the
! 63185: ** wrong context. The sqlite3_overload_function() API might construct
! 63186: ** SQL function that use this routine so that the functions will exist
! 63187: ** for name resolution but are actually overloaded by the xFindFunction
! 63188: ** method of virtual tables.
! 63189: */
! 63190: SQLITE_PRIVATE void sqlite3InvalidFunction(
! 63191: sqlite3_context *context, /* The function calling context */
! 63192: int NotUsed, /* Number of arguments to the function */
! 63193: sqlite3_value **NotUsed2 /* Value of each argument */
! 63194: ){
! 63195: const char *zName = context->pFunc->zName;
! 63196: char *zErr;
! 63197: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 63198: zErr = sqlite3_mprintf(
! 63199: "unable to use function %s in the requested context", zName);
! 63200: sqlite3_result_error(context, zErr, -1);
! 63201: sqlite3_free(zErr);
! 63202: }
! 63203:
! 63204: /*
! 63205: ** Allocate or return the aggregate context for a user function. A new
! 63206: ** context is allocated on the first call. Subsequent calls return the
! 63207: ** same context that was returned on prior calls.
! 63208: */
! 63209: SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
! 63210: Mem *pMem;
! 63211: assert( p && p->pFunc && p->pFunc->xStep );
! 63212: assert( sqlite3_mutex_held(p->s.db->mutex) );
! 63213: pMem = p->pMem;
! 63214: testcase( nByte<0 );
! 63215: if( (pMem->flags & MEM_Agg)==0 ){
! 63216: if( nByte<=0 ){
! 63217: sqlite3VdbeMemReleaseExternal(pMem);
! 63218: pMem->flags = MEM_Null;
! 63219: pMem->z = 0;
! 63220: }else{
! 63221: sqlite3VdbeMemGrow(pMem, nByte, 0);
! 63222: pMem->flags = MEM_Agg;
! 63223: pMem->u.pDef = p->pFunc;
! 63224: if( pMem->z ){
! 63225: memset(pMem->z, 0, nByte);
! 63226: }
! 63227: }
! 63228: }
! 63229: return (void*)pMem->z;
! 63230: }
! 63231:
! 63232: /*
! 63233: ** Return the auxilary data pointer, if any, for the iArg'th argument to
! 63234: ** the user-function defined by pCtx.
! 63235: */
! 63236: SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
! 63237: VdbeFunc *pVdbeFunc;
! 63238:
! 63239: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 63240: pVdbeFunc = pCtx->pVdbeFunc;
! 63241: if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
! 63242: return 0;
! 63243: }
! 63244: return pVdbeFunc->apAux[iArg].pAux;
! 63245: }
! 63246:
! 63247: /*
! 63248: ** Set the auxilary data pointer and delete function, for the iArg'th
! 63249: ** argument to the user-function defined by pCtx. Any previous value is
! 63250: ** deleted by calling the delete function specified when it was set.
! 63251: */
! 63252: SQLITE_API void sqlite3_set_auxdata(
! 63253: sqlite3_context *pCtx,
! 63254: int iArg,
! 63255: void *pAux,
! 63256: void (*xDelete)(void*)
! 63257: ){
! 63258: struct AuxData *pAuxData;
! 63259: VdbeFunc *pVdbeFunc;
! 63260: if( iArg<0 ) goto failed;
! 63261:
! 63262: assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
! 63263: pVdbeFunc = pCtx->pVdbeFunc;
! 63264: if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
! 63265: int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
! 63266: int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
! 63267: pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
! 63268: if( !pVdbeFunc ){
! 63269: goto failed;
! 63270: }
! 63271: pCtx->pVdbeFunc = pVdbeFunc;
! 63272: memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
! 63273: pVdbeFunc->nAux = iArg+1;
! 63274: pVdbeFunc->pFunc = pCtx->pFunc;
! 63275: }
! 63276:
! 63277: pAuxData = &pVdbeFunc->apAux[iArg];
! 63278: if( pAuxData->pAux && pAuxData->xDelete ){
! 63279: pAuxData->xDelete(pAuxData->pAux);
! 63280: }
! 63281: pAuxData->pAux = pAux;
! 63282: pAuxData->xDelete = xDelete;
! 63283: return;
! 63284:
! 63285: failed:
! 63286: if( xDelete ){
! 63287: xDelete(pAux);
! 63288: }
! 63289: }
! 63290:
! 63291: #ifndef SQLITE_OMIT_DEPRECATED
! 63292: /*
! 63293: ** Return the number of times the Step function of a aggregate has been
! 63294: ** called.
! 63295: **
! 63296: ** This function is deprecated. Do not use it for new code. It is
! 63297: ** provide only to avoid breaking legacy code. New aggregate function
! 63298: ** implementations should keep their own counts within their aggregate
! 63299: ** context.
! 63300: */
! 63301: SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
! 63302: assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
! 63303: return p->pMem->n;
! 63304: }
! 63305: #endif
! 63306:
! 63307: /*
! 63308: ** Return the number of columns in the result set for the statement pStmt.
! 63309: */
! 63310: SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
! 63311: Vdbe *pVm = (Vdbe *)pStmt;
! 63312: return pVm ? pVm->nResColumn : 0;
! 63313: }
! 63314:
! 63315: /*
! 63316: ** Return the number of values available from the current row of the
! 63317: ** currently executing statement pStmt.
! 63318: */
! 63319: SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
! 63320: Vdbe *pVm = (Vdbe *)pStmt;
! 63321: if( pVm==0 || pVm->pResultSet==0 ) return 0;
! 63322: return pVm->nResColumn;
! 63323: }
! 63324:
! 63325:
! 63326: /*
! 63327: ** Check to see if column iCol of the given statement is valid. If
! 63328: ** it is, return a pointer to the Mem for the value of that column.
! 63329: ** If iCol is not valid, return a pointer to a Mem which has a value
! 63330: ** of NULL.
! 63331: */
! 63332: static Mem *columnMem(sqlite3_stmt *pStmt, int i){
! 63333: Vdbe *pVm;
! 63334: Mem *pOut;
! 63335:
! 63336: pVm = (Vdbe *)pStmt;
! 63337: if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
! 63338: sqlite3_mutex_enter(pVm->db->mutex);
! 63339: pOut = &pVm->pResultSet[i];
! 63340: }else{
! 63341: /* If the value passed as the second argument is out of range, return
! 63342: ** a pointer to the following static Mem object which contains the
! 63343: ** value SQL NULL. Even though the Mem structure contains an element
! 63344: ** of type i64, on certain architectures (x86) with certain compiler
! 63345: ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
! 63346: ** instead of an 8-byte one. This all works fine, except that when
! 63347: ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
! 63348: ** that a Mem structure is located on an 8-byte boundary. To prevent
! 63349: ** these assert()s from failing, when building with SQLITE_DEBUG defined
! 63350: ** using gcc, we force nullMem to be 8-byte aligned using the magical
! 63351: ** __attribute__((aligned(8))) macro. */
! 63352: static const Mem nullMem
! 63353: #if defined(SQLITE_DEBUG) && defined(__GNUC__)
! 63354: __attribute__((aligned(8)))
! 63355: #endif
! 63356: = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0,
! 63357: #ifdef SQLITE_DEBUG
! 63358: 0, 0, /* pScopyFrom, pFiller */
! 63359: #endif
! 63360: 0, 0 };
! 63361:
! 63362: if( pVm && ALWAYS(pVm->db) ){
! 63363: sqlite3_mutex_enter(pVm->db->mutex);
! 63364: sqlite3Error(pVm->db, SQLITE_RANGE, 0);
! 63365: }
! 63366: pOut = (Mem*)&nullMem;
! 63367: }
! 63368: return pOut;
! 63369: }
! 63370:
! 63371: /*
! 63372: ** This function is called after invoking an sqlite3_value_XXX function on a
! 63373: ** column value (i.e. a value returned by evaluating an SQL expression in the
! 63374: ** select list of a SELECT statement) that may cause a malloc() failure. If
! 63375: ** malloc() has failed, the threads mallocFailed flag is cleared and the result
! 63376: ** code of statement pStmt set to SQLITE_NOMEM.
! 63377: **
! 63378: ** Specifically, this is called from within:
! 63379: **
! 63380: ** sqlite3_column_int()
! 63381: ** sqlite3_column_int64()
! 63382: ** sqlite3_column_text()
! 63383: ** sqlite3_column_text16()
! 63384: ** sqlite3_column_real()
! 63385: ** sqlite3_column_bytes()
! 63386: ** sqlite3_column_bytes16()
! 63387: ** sqiite3_column_blob()
! 63388: */
! 63389: static void columnMallocFailure(sqlite3_stmt *pStmt)
! 63390: {
! 63391: /* If malloc() failed during an encoding conversion within an
! 63392: ** sqlite3_column_XXX API, then set the return code of the statement to
! 63393: ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
! 63394: ** and _finalize() will return NOMEM.
! 63395: */
! 63396: Vdbe *p = (Vdbe *)pStmt;
! 63397: if( p ){
! 63398: p->rc = sqlite3ApiExit(p->db, p->rc);
! 63399: sqlite3_mutex_leave(p->db->mutex);
! 63400: }
! 63401: }
! 63402:
! 63403: /**************************** sqlite3_column_ *******************************
! 63404: ** The following routines are used to access elements of the current row
! 63405: ** in the result set.
! 63406: */
! 63407: SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
! 63408: const void *val;
! 63409: val = sqlite3_value_blob( columnMem(pStmt,i) );
! 63410: /* Even though there is no encoding conversion, value_blob() might
! 63411: ** need to call malloc() to expand the result of a zeroblob()
! 63412: ** expression.
! 63413: */
! 63414: columnMallocFailure(pStmt);
! 63415: return val;
! 63416: }
! 63417: SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
! 63418: int val = sqlite3_value_bytes( columnMem(pStmt,i) );
! 63419: columnMallocFailure(pStmt);
! 63420: return val;
! 63421: }
! 63422: SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
! 63423: int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
! 63424: columnMallocFailure(pStmt);
! 63425: return val;
! 63426: }
! 63427: SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
! 63428: double val = sqlite3_value_double( columnMem(pStmt,i) );
! 63429: columnMallocFailure(pStmt);
! 63430: return val;
! 63431: }
! 63432: SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
! 63433: int val = sqlite3_value_int( columnMem(pStmt,i) );
! 63434: columnMallocFailure(pStmt);
! 63435: return val;
! 63436: }
! 63437: SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
! 63438: sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
! 63439: columnMallocFailure(pStmt);
! 63440: return val;
! 63441: }
! 63442: SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
! 63443: const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
! 63444: columnMallocFailure(pStmt);
! 63445: return val;
! 63446: }
! 63447: SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
! 63448: Mem *pOut = columnMem(pStmt, i);
! 63449: if( pOut->flags&MEM_Static ){
! 63450: pOut->flags &= ~MEM_Static;
! 63451: pOut->flags |= MEM_Ephem;
! 63452: }
! 63453: columnMallocFailure(pStmt);
! 63454: return (sqlite3_value *)pOut;
! 63455: }
! 63456: #ifndef SQLITE_OMIT_UTF16
! 63457: SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
! 63458: const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
! 63459: columnMallocFailure(pStmt);
! 63460: return val;
! 63461: }
! 63462: #endif /* SQLITE_OMIT_UTF16 */
! 63463: SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
! 63464: int iType = sqlite3_value_type( columnMem(pStmt,i) );
! 63465: columnMallocFailure(pStmt);
! 63466: return iType;
! 63467: }
! 63468:
! 63469: /* The following function is experimental and subject to change or
! 63470: ** removal */
! 63471: /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
! 63472: ** return sqlite3_value_numeric_type( columnMem(pStmt,i) );
! 63473: **}
! 63474: */
! 63475:
! 63476: /*
! 63477: ** Convert the N-th element of pStmt->pColName[] into a string using
! 63478: ** xFunc() then return that string. If N is out of range, return 0.
! 63479: **
! 63480: ** There are up to 5 names for each column. useType determines which
! 63481: ** name is returned. Here are the names:
! 63482: **
! 63483: ** 0 The column name as it should be displayed for output
! 63484: ** 1 The datatype name for the column
! 63485: ** 2 The name of the database that the column derives from
! 63486: ** 3 The name of the table that the column derives from
! 63487: ** 4 The name of the table column that the result column derives from
! 63488: **
! 63489: ** If the result is not a simple column reference (if it is an expression
! 63490: ** or a constant) then useTypes 2, 3, and 4 return NULL.
! 63491: */
! 63492: static const void *columnName(
! 63493: sqlite3_stmt *pStmt,
! 63494: int N,
! 63495: const void *(*xFunc)(Mem*),
! 63496: int useType
! 63497: ){
! 63498: const void *ret = 0;
! 63499: Vdbe *p = (Vdbe *)pStmt;
! 63500: int n;
! 63501: sqlite3 *db = p->db;
! 63502:
! 63503: assert( db!=0 );
! 63504: n = sqlite3_column_count(pStmt);
! 63505: if( N<n && N>=0 ){
! 63506: N += useType*n;
! 63507: sqlite3_mutex_enter(db->mutex);
! 63508: assert( db->mallocFailed==0 );
! 63509: ret = xFunc(&p->aColName[N]);
! 63510: /* A malloc may have failed inside of the xFunc() call. If this
! 63511: ** is the case, clear the mallocFailed flag and return NULL.
! 63512: */
! 63513: if( db->mallocFailed ){
! 63514: db->mallocFailed = 0;
! 63515: ret = 0;
! 63516: }
! 63517: sqlite3_mutex_leave(db->mutex);
! 63518: }
! 63519: return ret;
! 63520: }
! 63521:
! 63522: /*
! 63523: ** Return the name of the Nth column of the result set returned by SQL
! 63524: ** statement pStmt.
! 63525: */
! 63526: SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
! 63527: return columnName(
! 63528: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
! 63529: }
! 63530: #ifndef SQLITE_OMIT_UTF16
! 63531: SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
! 63532: return columnName(
! 63533: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
! 63534: }
! 63535: #endif
! 63536:
! 63537: /*
! 63538: ** Constraint: If you have ENABLE_COLUMN_METADATA then you must
! 63539: ** not define OMIT_DECLTYPE.
! 63540: */
! 63541: #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
! 63542: # error "Must not define both SQLITE_OMIT_DECLTYPE \
! 63543: and SQLITE_ENABLE_COLUMN_METADATA"
! 63544: #endif
! 63545:
! 63546: #ifndef SQLITE_OMIT_DECLTYPE
! 63547: /*
! 63548: ** Return the column declaration type (if applicable) of the 'i'th column
! 63549: ** of the result set of SQL statement pStmt.
! 63550: */
! 63551: SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
! 63552: return columnName(
! 63553: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
! 63554: }
! 63555: #ifndef SQLITE_OMIT_UTF16
! 63556: SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
! 63557: return columnName(
! 63558: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
! 63559: }
! 63560: #endif /* SQLITE_OMIT_UTF16 */
! 63561: #endif /* SQLITE_OMIT_DECLTYPE */
! 63562:
! 63563: #ifdef SQLITE_ENABLE_COLUMN_METADATA
! 63564: /*
! 63565: ** Return the name of the database from which a result column derives.
! 63566: ** NULL is returned if the result column is an expression or constant or
! 63567: ** anything else which is not an unabiguous reference to a database column.
! 63568: */
! 63569: SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
! 63570: return columnName(
! 63571: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
! 63572: }
! 63573: #ifndef SQLITE_OMIT_UTF16
! 63574: SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
! 63575: return columnName(
! 63576: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
! 63577: }
! 63578: #endif /* SQLITE_OMIT_UTF16 */
! 63579:
! 63580: /*
! 63581: ** Return the name of the table from which a result column derives.
! 63582: ** NULL is returned if the result column is an expression or constant or
! 63583: ** anything else which is not an unabiguous reference to a database column.
! 63584: */
! 63585: SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
! 63586: return columnName(
! 63587: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
! 63588: }
! 63589: #ifndef SQLITE_OMIT_UTF16
! 63590: SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
! 63591: return columnName(
! 63592: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
! 63593: }
! 63594: #endif /* SQLITE_OMIT_UTF16 */
! 63595:
! 63596: /*
! 63597: ** Return the name of the table column from which a result column derives.
! 63598: ** NULL is returned if the result column is an expression or constant or
! 63599: ** anything else which is not an unabiguous reference to a database column.
! 63600: */
! 63601: SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
! 63602: return columnName(
! 63603: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
! 63604: }
! 63605: #ifndef SQLITE_OMIT_UTF16
! 63606: SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
! 63607: return columnName(
! 63608: pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
! 63609: }
! 63610: #endif /* SQLITE_OMIT_UTF16 */
! 63611: #endif /* SQLITE_ENABLE_COLUMN_METADATA */
! 63612:
! 63613:
! 63614: /******************************* sqlite3_bind_ ***************************
! 63615: **
! 63616: ** Routines used to attach values to wildcards in a compiled SQL statement.
! 63617: */
! 63618: /*
! 63619: ** Unbind the value bound to variable i in virtual machine p. This is the
! 63620: ** the same as binding a NULL value to the column. If the "i" parameter is
! 63621: ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
! 63622: **
! 63623: ** A successful evaluation of this routine acquires the mutex on p.
! 63624: ** the mutex is released if any kind of error occurs.
! 63625: **
! 63626: ** The error code stored in database p->db is overwritten with the return
! 63627: ** value in any case.
! 63628: */
! 63629: static int vdbeUnbind(Vdbe *p, int i){
! 63630: Mem *pVar;
! 63631: if( vdbeSafetyNotNull(p) ){
! 63632: return SQLITE_MISUSE_BKPT;
! 63633: }
! 63634: sqlite3_mutex_enter(p->db->mutex);
! 63635: if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
! 63636: sqlite3Error(p->db, SQLITE_MISUSE, 0);
! 63637: sqlite3_mutex_leave(p->db->mutex);
! 63638: sqlite3_log(SQLITE_MISUSE,
! 63639: "bind on a busy prepared statement: [%s]", p->zSql);
! 63640: return SQLITE_MISUSE_BKPT;
! 63641: }
! 63642: if( i<1 || i>p->nVar ){
! 63643: sqlite3Error(p->db, SQLITE_RANGE, 0);
! 63644: sqlite3_mutex_leave(p->db->mutex);
! 63645: return SQLITE_RANGE;
! 63646: }
! 63647: i--;
! 63648: pVar = &p->aVar[i];
! 63649: sqlite3VdbeMemRelease(pVar);
! 63650: pVar->flags = MEM_Null;
! 63651: sqlite3Error(p->db, SQLITE_OK, 0);
! 63652:
! 63653: /* If the bit corresponding to this variable in Vdbe.expmask is set, then
! 63654: ** binding a new value to this variable invalidates the current query plan.
! 63655: **
! 63656: ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
! 63657: ** parameter in the WHERE clause might influence the choice of query plan
! 63658: ** for a statement, then the statement will be automatically recompiled,
! 63659: ** as if there had been a schema change, on the first sqlite3_step() call
! 63660: ** following any change to the bindings of that parameter.
! 63661: */
! 63662: if( p->isPrepareV2 &&
! 63663: ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
! 63664: ){
! 63665: p->expired = 1;
! 63666: }
! 63667: return SQLITE_OK;
! 63668: }
! 63669:
! 63670: /*
! 63671: ** Bind a text or BLOB value.
! 63672: */
! 63673: static int bindText(
! 63674: sqlite3_stmt *pStmt, /* The statement to bind against */
! 63675: int i, /* Index of the parameter to bind */
! 63676: const void *zData, /* Pointer to the data to be bound */
! 63677: int nData, /* Number of bytes of data to be bound */
! 63678: void (*xDel)(void*), /* Destructor for the data */
! 63679: u8 encoding /* Encoding for the data */
! 63680: ){
! 63681: Vdbe *p = (Vdbe *)pStmt;
! 63682: Mem *pVar;
! 63683: int rc;
! 63684:
! 63685: rc = vdbeUnbind(p, i);
! 63686: if( rc==SQLITE_OK ){
! 63687: if( zData!=0 ){
! 63688: pVar = &p->aVar[i-1];
! 63689: rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
! 63690: if( rc==SQLITE_OK && encoding!=0 ){
! 63691: rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
! 63692: }
! 63693: sqlite3Error(p->db, rc, 0);
! 63694: rc = sqlite3ApiExit(p->db, rc);
! 63695: }
! 63696: sqlite3_mutex_leave(p->db->mutex);
! 63697: }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
! 63698: xDel((void*)zData);
! 63699: }
! 63700: return rc;
! 63701: }
! 63702:
! 63703:
! 63704: /*
! 63705: ** Bind a blob value to an SQL statement variable.
! 63706: */
! 63707: SQLITE_API int sqlite3_bind_blob(
! 63708: sqlite3_stmt *pStmt,
! 63709: int i,
! 63710: const void *zData,
! 63711: int nData,
! 63712: void (*xDel)(void*)
! 63713: ){
! 63714: return bindText(pStmt, i, zData, nData, xDel, 0);
! 63715: }
! 63716: SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
! 63717: int rc;
! 63718: Vdbe *p = (Vdbe *)pStmt;
! 63719: rc = vdbeUnbind(p, i);
! 63720: if( rc==SQLITE_OK ){
! 63721: sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
! 63722: sqlite3_mutex_leave(p->db->mutex);
! 63723: }
! 63724: return rc;
! 63725: }
! 63726: SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
! 63727: return sqlite3_bind_int64(p, i, (i64)iValue);
! 63728: }
! 63729: SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
! 63730: int rc;
! 63731: Vdbe *p = (Vdbe *)pStmt;
! 63732: rc = vdbeUnbind(p, i);
! 63733: if( rc==SQLITE_OK ){
! 63734: sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
! 63735: sqlite3_mutex_leave(p->db->mutex);
! 63736: }
! 63737: return rc;
! 63738: }
! 63739: SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
! 63740: int rc;
! 63741: Vdbe *p = (Vdbe*)pStmt;
! 63742: rc = vdbeUnbind(p, i);
! 63743: if( rc==SQLITE_OK ){
! 63744: sqlite3_mutex_leave(p->db->mutex);
! 63745: }
! 63746: return rc;
! 63747: }
! 63748: SQLITE_API int sqlite3_bind_text(
! 63749: sqlite3_stmt *pStmt,
! 63750: int i,
! 63751: const char *zData,
! 63752: int nData,
! 63753: void (*xDel)(void*)
! 63754: ){
! 63755: return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
! 63756: }
! 63757: #ifndef SQLITE_OMIT_UTF16
! 63758: SQLITE_API int sqlite3_bind_text16(
! 63759: sqlite3_stmt *pStmt,
! 63760: int i,
! 63761: const void *zData,
! 63762: int nData,
! 63763: void (*xDel)(void*)
! 63764: ){
! 63765: return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
! 63766: }
! 63767: #endif /* SQLITE_OMIT_UTF16 */
! 63768: SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
! 63769: int rc;
! 63770: switch( pValue->type ){
! 63771: case SQLITE_INTEGER: {
! 63772: rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
! 63773: break;
! 63774: }
! 63775: case SQLITE_FLOAT: {
! 63776: rc = sqlite3_bind_double(pStmt, i, pValue->r);
! 63777: break;
! 63778: }
! 63779: case SQLITE_BLOB: {
! 63780: if( pValue->flags & MEM_Zero ){
! 63781: rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
! 63782: }else{
! 63783: rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
! 63784: }
! 63785: break;
! 63786: }
! 63787: case SQLITE_TEXT: {
! 63788: rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
! 63789: pValue->enc);
! 63790: break;
! 63791: }
! 63792: default: {
! 63793: rc = sqlite3_bind_null(pStmt, i);
! 63794: break;
! 63795: }
! 63796: }
! 63797: return rc;
! 63798: }
! 63799: SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
! 63800: int rc;
! 63801: Vdbe *p = (Vdbe *)pStmt;
! 63802: rc = vdbeUnbind(p, i);
! 63803: if( rc==SQLITE_OK ){
! 63804: sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
! 63805: sqlite3_mutex_leave(p->db->mutex);
! 63806: }
! 63807: return rc;
! 63808: }
! 63809:
! 63810: /*
! 63811: ** Return the number of wildcards that can be potentially bound to.
! 63812: ** This routine is added to support DBD::SQLite.
! 63813: */
! 63814: SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
! 63815: Vdbe *p = (Vdbe*)pStmt;
! 63816: return p ? p->nVar : 0;
! 63817: }
! 63818:
! 63819: /*
! 63820: ** Return the name of a wildcard parameter. Return NULL if the index
! 63821: ** is out of range or if the wildcard is unnamed.
! 63822: **
! 63823: ** The result is always UTF-8.
! 63824: */
! 63825: SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
! 63826: Vdbe *p = (Vdbe*)pStmt;
! 63827: if( p==0 || i<1 || i>p->nzVar ){
! 63828: return 0;
! 63829: }
! 63830: return p->azVar[i-1];
! 63831: }
! 63832:
! 63833: /*
! 63834: ** Given a wildcard parameter name, return the index of the variable
! 63835: ** with that name. If there is no variable with the given name,
! 63836: ** return 0.
! 63837: */
! 63838: SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
! 63839: int i;
! 63840: if( p==0 ){
! 63841: return 0;
! 63842: }
! 63843: if( zName ){
! 63844: for(i=0; i<p->nzVar; i++){
! 63845: const char *z = p->azVar[i];
! 63846: if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){
! 63847: return i+1;
! 63848: }
! 63849: }
! 63850: }
! 63851: return 0;
! 63852: }
! 63853: SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
! 63854: return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
! 63855: }
! 63856:
! 63857: /*
! 63858: ** Transfer all bindings from the first statement over to the second.
! 63859: */
! 63860: SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
! 63861: Vdbe *pFrom = (Vdbe*)pFromStmt;
! 63862: Vdbe *pTo = (Vdbe*)pToStmt;
! 63863: int i;
! 63864: assert( pTo->db==pFrom->db );
! 63865: assert( pTo->nVar==pFrom->nVar );
! 63866: sqlite3_mutex_enter(pTo->db->mutex);
! 63867: for(i=0; i<pFrom->nVar; i++){
! 63868: sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
! 63869: }
! 63870: sqlite3_mutex_leave(pTo->db->mutex);
! 63871: return SQLITE_OK;
! 63872: }
! 63873:
! 63874: #ifndef SQLITE_OMIT_DEPRECATED
! 63875: /*
! 63876: ** Deprecated external interface. Internal/core SQLite code
! 63877: ** should call sqlite3TransferBindings.
! 63878: **
! 63879: ** Is is misuse to call this routine with statements from different
! 63880: ** database connections. But as this is a deprecated interface, we
! 63881: ** will not bother to check for that condition.
! 63882: **
! 63883: ** If the two statements contain a different number of bindings, then
! 63884: ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
! 63885: ** SQLITE_OK is returned.
! 63886: */
! 63887: SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
! 63888: Vdbe *pFrom = (Vdbe*)pFromStmt;
! 63889: Vdbe *pTo = (Vdbe*)pToStmt;
! 63890: if( pFrom->nVar!=pTo->nVar ){
! 63891: return SQLITE_ERROR;
! 63892: }
! 63893: if( pTo->isPrepareV2 && pTo->expmask ){
! 63894: pTo->expired = 1;
! 63895: }
! 63896: if( pFrom->isPrepareV2 && pFrom->expmask ){
! 63897: pFrom->expired = 1;
! 63898: }
! 63899: return sqlite3TransferBindings(pFromStmt, pToStmt);
! 63900: }
! 63901: #endif
! 63902:
! 63903: /*
! 63904: ** Return the sqlite3* database handle to which the prepared statement given
! 63905: ** in the argument belongs. This is the same database handle that was
! 63906: ** the first argument to the sqlite3_prepare() that was used to create
! 63907: ** the statement in the first place.
! 63908: */
! 63909: SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
! 63910: return pStmt ? ((Vdbe*)pStmt)->db : 0;
! 63911: }
! 63912:
! 63913: /*
! 63914: ** Return true if the prepared statement is guaranteed to not modify the
! 63915: ** database.
! 63916: */
! 63917: SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
! 63918: return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
! 63919: }
! 63920:
! 63921: /*
! 63922: ** Return true if the prepared statement is in need of being reset.
! 63923: */
! 63924: SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
! 63925: Vdbe *v = (Vdbe*)pStmt;
! 63926: return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
! 63927: }
! 63928:
! 63929: /*
! 63930: ** Return a pointer to the next prepared statement after pStmt associated
! 63931: ** with database connection pDb. If pStmt is NULL, return the first
! 63932: ** prepared statement for the database connection. Return NULL if there
! 63933: ** are no more.
! 63934: */
! 63935: SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
! 63936: sqlite3_stmt *pNext;
! 63937: sqlite3_mutex_enter(pDb->mutex);
! 63938: if( pStmt==0 ){
! 63939: pNext = (sqlite3_stmt*)pDb->pVdbe;
! 63940: }else{
! 63941: pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
! 63942: }
! 63943: sqlite3_mutex_leave(pDb->mutex);
! 63944: return pNext;
! 63945: }
! 63946:
! 63947: /*
! 63948: ** Return the value of a status counter for a prepared statement
! 63949: */
! 63950: SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
! 63951: Vdbe *pVdbe = (Vdbe*)pStmt;
! 63952: int v = pVdbe->aCounter[op-1];
! 63953: if( resetFlag ) pVdbe->aCounter[op-1] = 0;
! 63954: return v;
! 63955: }
! 63956:
! 63957: /************** End of vdbeapi.c *********************************************/
! 63958: /************** Begin file vdbetrace.c ***************************************/
! 63959: /*
! 63960: ** 2009 November 25
! 63961: **
! 63962: ** The author disclaims copyright to this source code. In place of
! 63963: ** a legal notice, here is a blessing:
! 63964: **
! 63965: ** May you do good and not evil.
! 63966: ** May you find forgiveness for yourself and forgive others.
! 63967: ** May you share freely, never taking more than you give.
! 63968: **
! 63969: *************************************************************************
! 63970: **
! 63971: ** This file contains code used to insert the values of host parameters
! 63972: ** (aka "wildcards") into the SQL text output by sqlite3_trace().
! 63973: **
! 63974: ** The Vdbe parse-tree explainer is also found here.
! 63975: */
! 63976:
! 63977: #ifndef SQLITE_OMIT_TRACE
! 63978:
! 63979: /*
! 63980: ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
! 63981: ** bytes in this text up to but excluding the first character in
! 63982: ** a host parameter. If the text contains no host parameters, return
! 63983: ** the total number of bytes in the text.
! 63984: */
! 63985: static int findNextHostParameter(const char *zSql, int *pnToken){
! 63986: int tokenType;
! 63987: int nTotal = 0;
! 63988: int n;
! 63989:
! 63990: *pnToken = 0;
! 63991: while( zSql[0] ){
! 63992: n = sqlite3GetToken((u8*)zSql, &tokenType);
! 63993: assert( n>0 && tokenType!=TK_ILLEGAL );
! 63994: if( tokenType==TK_VARIABLE ){
! 63995: *pnToken = n;
! 63996: break;
! 63997: }
! 63998: nTotal += n;
! 63999: zSql += n;
! 64000: }
! 64001: return nTotal;
! 64002: }
! 64003:
! 64004: /*
! 64005: ** This function returns a pointer to a nul-terminated string in memory
! 64006: ** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
! 64007: ** string contains a copy of zRawSql but with host parameters expanded to
! 64008: ** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1,
! 64009: ** then the returned string holds a copy of zRawSql with "-- " prepended
! 64010: ** to each line of text.
! 64011: **
! 64012: ** The calling function is responsible for making sure the memory returned
! 64013: ** is eventually freed.
! 64014: **
! 64015: ** ALGORITHM: Scan the input string looking for host parameters in any of
! 64016: ** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
! 64017: ** string literals, quoted identifier names, and comments. For text forms,
! 64018: ** the host parameter index is found by scanning the perpared
! 64019: ** statement for the corresponding OP_Variable opcode. Once the host
! 64020: ** parameter index is known, locate the value in p->aVar[]. Then render
! 64021: ** the value as a literal in place of the host parameter name.
! 64022: */
! 64023: SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
! 64024: Vdbe *p, /* The prepared statement being evaluated */
! 64025: const char *zRawSql /* Raw text of the SQL statement */
! 64026: ){
! 64027: sqlite3 *db; /* The database connection */
! 64028: int idx = 0; /* Index of a host parameter */
! 64029: int nextIndex = 1; /* Index of next ? host parameter */
! 64030: int n; /* Length of a token prefix */
! 64031: int nToken; /* Length of the parameter token */
! 64032: int i; /* Loop counter */
! 64033: Mem *pVar; /* Value of a host parameter */
! 64034: StrAccum out; /* Accumulate the output here */
! 64035: char zBase[100]; /* Initial working space */
! 64036:
! 64037: db = p->db;
! 64038: sqlite3StrAccumInit(&out, zBase, sizeof(zBase),
! 64039: db->aLimit[SQLITE_LIMIT_LENGTH]);
! 64040: out.db = db;
! 64041: if( db->vdbeExecCnt>1 ){
! 64042: while( *zRawSql ){
! 64043: const char *zStart = zRawSql;
! 64044: while( *(zRawSql++)!='\n' && *zRawSql );
! 64045: sqlite3StrAccumAppend(&out, "-- ", 3);
! 64046: sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
! 64047: }
! 64048: }else{
! 64049: while( zRawSql[0] ){
! 64050: n = findNextHostParameter(zRawSql, &nToken);
! 64051: assert( n>0 );
! 64052: sqlite3StrAccumAppend(&out, zRawSql, n);
! 64053: zRawSql += n;
! 64054: assert( zRawSql[0] || nToken==0 );
! 64055: if( nToken==0 ) break;
! 64056: if( zRawSql[0]=='?' ){
! 64057: if( nToken>1 ){
! 64058: assert( sqlite3Isdigit(zRawSql[1]) );
! 64059: sqlite3GetInt32(&zRawSql[1], &idx);
! 64060: }else{
! 64061: idx = nextIndex;
! 64062: }
! 64063: }else{
! 64064: assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
! 64065: testcase( zRawSql[0]==':' );
! 64066: testcase( zRawSql[0]=='$' );
! 64067: testcase( zRawSql[0]=='@' );
! 64068: idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
! 64069: assert( idx>0 );
! 64070: }
! 64071: zRawSql += nToken;
! 64072: nextIndex = idx + 1;
! 64073: assert( idx>0 && idx<=p->nVar );
! 64074: pVar = &p->aVar[idx-1];
! 64075: if( pVar->flags & MEM_Null ){
! 64076: sqlite3StrAccumAppend(&out, "NULL", 4);
! 64077: }else if( pVar->flags & MEM_Int ){
! 64078: sqlite3XPrintf(&out, "%lld", pVar->u.i);
! 64079: }else if( pVar->flags & MEM_Real ){
! 64080: sqlite3XPrintf(&out, "%!.15g", pVar->r);
! 64081: }else if( pVar->flags & MEM_Str ){
! 64082: #ifndef SQLITE_OMIT_UTF16
! 64083: u8 enc = ENC(db);
! 64084: if( enc!=SQLITE_UTF8 ){
! 64085: Mem utf8;
! 64086: memset(&utf8, 0, sizeof(utf8));
! 64087: utf8.db = db;
! 64088: sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
! 64089: sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
! 64090: sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
! 64091: sqlite3VdbeMemRelease(&utf8);
! 64092: }else
! 64093: #endif
! 64094: {
! 64095: sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
! 64096: }
! 64097: }else if( pVar->flags & MEM_Zero ){
! 64098: sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
! 64099: }else{
! 64100: assert( pVar->flags & MEM_Blob );
! 64101: sqlite3StrAccumAppend(&out, "x'", 2);
! 64102: for(i=0; i<pVar->n; i++){
! 64103: sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
! 64104: }
! 64105: sqlite3StrAccumAppend(&out, "'", 1);
! 64106: }
! 64107: }
! 64108: }
! 64109: return sqlite3StrAccumFinish(&out);
! 64110: }
! 64111:
! 64112: #endif /* #ifndef SQLITE_OMIT_TRACE */
! 64113:
! 64114: /*****************************************************************************
! 64115: ** The following code implements the data-structure explaining logic
! 64116: ** for the Vdbe.
! 64117: */
! 64118:
! 64119: #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
! 64120:
! 64121: /*
! 64122: ** Allocate a new Explain object
! 64123: */
! 64124: SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe *pVdbe){
! 64125: if( pVdbe ){
! 64126: sqlite3BeginBenignMalloc();
! 64127: Explain *p = sqlite3_malloc( sizeof(Explain) );
! 64128: if( p ){
! 64129: memset(p, 0, sizeof(*p));
! 64130: p->pVdbe = pVdbe;
! 64131: sqlite3_free(pVdbe->pExplain);
! 64132: pVdbe->pExplain = p;
! 64133: sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
! 64134: SQLITE_MAX_LENGTH);
! 64135: p->str.useMalloc = 2;
! 64136: }else{
! 64137: sqlite3EndBenignMalloc();
! 64138: }
! 64139: }
! 64140: }
! 64141:
! 64142: /*
! 64143: ** Return true if the Explain ends with a new-line.
! 64144: */
! 64145: static int endsWithNL(Explain *p){
! 64146: return p && p->str.zText && p->str.nChar
! 64147: && p->str.zText[p->str.nChar-1]=='\n';
! 64148: }
! 64149:
! 64150: /*
! 64151: ** Append text to the indentation
! 64152: */
! 64153: SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){
! 64154: Explain *p;
! 64155: if( pVdbe && (p = pVdbe->pExplain)!=0 ){
! 64156: va_list ap;
! 64157: if( p->nIndent && endsWithNL(p) ){
! 64158: int n = p->nIndent;
! 64159: if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent);
! 64160: sqlite3AppendSpace(&p->str, p->aIndent[n-1]);
! 64161: }
! 64162: va_start(ap, zFormat);
! 64163: sqlite3VXPrintf(&p->str, 1, zFormat, ap);
! 64164: va_end(ap);
! 64165: }
! 64166: }
! 64167:
! 64168: /*
! 64169: ** Append a '\n' if there is not already one.
! 64170: */
! 64171: SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe *pVdbe){
! 64172: Explain *p;
! 64173: if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){
! 64174: sqlite3StrAccumAppend(&p->str, "\n", 1);
! 64175: }
! 64176: }
! 64177:
! 64178: /*
! 64179: ** Push a new indentation level. Subsequent lines will be indented
! 64180: ** so that they begin at the current cursor position.
! 64181: */
! 64182: SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe *pVdbe){
! 64183: Explain *p;
! 64184: if( pVdbe && (p = pVdbe->pExplain)!=0 ){
! 64185: if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){
! 64186: const char *z = p->str.zText;
! 64187: int i = p->str.nChar-1;
! 64188: int x;
! 64189: while( i>=0 && z[i]!='\n' ){ i--; }
! 64190: x = (p->str.nChar - 1) - i;
! 64191: if( p->nIndent && x<p->aIndent[p->nIndent-1] ){
! 64192: x = p->aIndent[p->nIndent-1];
! 64193: }
! 64194: p->aIndent[p->nIndent] = x;
! 64195: }
! 64196: p->nIndent++;
! 64197: }
! 64198: }
! 64199:
! 64200: /*
! 64201: ** Pop the indentation stack by one level.
! 64202: */
! 64203: SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe *p){
! 64204: if( p && p->pExplain ) p->pExplain->nIndent--;
! 64205: }
! 64206:
! 64207: /*
! 64208: ** Free the indentation structure
! 64209: */
! 64210: SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe *pVdbe){
! 64211: if( pVdbe && pVdbe->pExplain ){
! 64212: sqlite3_free(pVdbe->zExplain);
! 64213: sqlite3ExplainNL(pVdbe);
! 64214: pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str);
! 64215: sqlite3_free(pVdbe->pExplain);
! 64216: pVdbe->pExplain = 0;
! 64217: sqlite3EndBenignMalloc();
! 64218: }
! 64219: }
! 64220:
! 64221: /*
! 64222: ** Return the explanation of a virtual machine.
! 64223: */
! 64224: SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe *pVdbe){
! 64225: return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0;
! 64226: }
! 64227: #endif /* defined(SQLITE_DEBUG) */
! 64228:
! 64229: /************** End of vdbetrace.c *******************************************/
! 64230: /************** Begin file vdbe.c ********************************************/
! 64231: /*
! 64232: ** 2001 September 15
! 64233: **
! 64234: ** The author disclaims copyright to this source code. In place of
! 64235: ** a legal notice, here is a blessing:
! 64236: **
! 64237: ** May you do good and not evil.
! 64238: ** May you find forgiveness for yourself and forgive others.
! 64239: ** May you share freely, never taking more than you give.
! 64240: **
! 64241: *************************************************************************
! 64242: ** The code in this file implements execution method of the
! 64243: ** Virtual Database Engine (VDBE). A separate file ("vdbeaux.c")
! 64244: ** handles housekeeping details such as creating and deleting
! 64245: ** VDBE instances. This file is solely interested in executing
! 64246: ** the VDBE program.
! 64247: **
! 64248: ** In the external interface, an "sqlite3_stmt*" is an opaque pointer
! 64249: ** to a VDBE.
! 64250: **
! 64251: ** The SQL parser generates a program which is then executed by
! 64252: ** the VDBE to do the work of the SQL statement. VDBE programs are
! 64253: ** similar in form to assembly language. The program consists of
! 64254: ** a linear sequence of operations. Each operation has an opcode
! 64255: ** and 5 operands. Operands P1, P2, and P3 are integers. Operand P4
! 64256: ** is a null-terminated string. Operand P5 is an unsigned character.
! 64257: ** Few opcodes use all 5 operands.
! 64258: **
! 64259: ** Computation results are stored on a set of registers numbered beginning
! 64260: ** with 1 and going up to Vdbe.nMem. Each register can store
! 64261: ** either an integer, a null-terminated string, a floating point
! 64262: ** number, or the SQL "NULL" value. An implicit conversion from one
! 64263: ** type to the other occurs as necessary.
! 64264: **
! 64265: ** Most of the code in this file is taken up by the sqlite3VdbeExec()
! 64266: ** function which does the work of interpreting a VDBE program.
! 64267: ** But other routines are also provided to help in building up
! 64268: ** a program instruction by instruction.
! 64269: **
! 64270: ** Various scripts scan this source file in order to generate HTML
! 64271: ** documentation, headers files, or other derived files. The formatting
! 64272: ** of the code in this file is, therefore, important. See other comments
! 64273: ** in this file for details. If in doubt, do not deviate from existing
! 64274: ** commenting and indentation practices when changing or adding code.
! 64275: */
! 64276:
! 64277: /*
! 64278: ** Invoke this macro on memory cells just prior to changing the
! 64279: ** value of the cell. This macro verifies that shallow copies are
! 64280: ** not misused.
! 64281: */
! 64282: #ifdef SQLITE_DEBUG
! 64283: # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
! 64284: #else
! 64285: # define memAboutToChange(P,M)
! 64286: #endif
! 64287:
! 64288: /*
! 64289: ** The following global variable is incremented every time a cursor
! 64290: ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
! 64291: ** procedures use this information to make sure that indices are
! 64292: ** working correctly. This variable has no function other than to
! 64293: ** help verify the correct operation of the library.
! 64294: */
! 64295: #ifdef SQLITE_TEST
! 64296: SQLITE_API int sqlite3_search_count = 0;
! 64297: #endif
! 64298:
! 64299: /*
! 64300: ** When this global variable is positive, it gets decremented once before
! 64301: ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
! 64302: ** field of the sqlite3 structure is set in order to simulate an interrupt.
! 64303: **
! 64304: ** This facility is used for testing purposes only. It does not function
! 64305: ** in an ordinary build.
! 64306: */
! 64307: #ifdef SQLITE_TEST
! 64308: SQLITE_API int sqlite3_interrupt_count = 0;
! 64309: #endif
! 64310:
! 64311: /*
! 64312: ** The next global variable is incremented each type the OP_Sort opcode
! 64313: ** is executed. The test procedures use this information to make sure that
! 64314: ** sorting is occurring or not occurring at appropriate times. This variable
! 64315: ** has no function other than to help verify the correct operation of the
! 64316: ** library.
! 64317: */
! 64318: #ifdef SQLITE_TEST
! 64319: SQLITE_API int sqlite3_sort_count = 0;
! 64320: #endif
! 64321:
! 64322: /*
! 64323: ** The next global variable records the size of the largest MEM_Blob
! 64324: ** or MEM_Str that has been used by a VDBE opcode. The test procedures
! 64325: ** use this information to make sure that the zero-blob functionality
! 64326: ** is working correctly. This variable has no function other than to
! 64327: ** help verify the correct operation of the library.
! 64328: */
! 64329: #ifdef SQLITE_TEST
! 64330: SQLITE_API int sqlite3_max_blobsize = 0;
! 64331: static void updateMaxBlobsize(Mem *p){
! 64332: if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
! 64333: sqlite3_max_blobsize = p->n;
! 64334: }
! 64335: }
! 64336: #endif
! 64337:
! 64338: /*
! 64339: ** The next global variable is incremented each type the OP_Found opcode
! 64340: ** is executed. This is used to test whether or not the foreign key
! 64341: ** operation implemented using OP_FkIsZero is working. This variable
! 64342: ** has no function other than to help verify the correct operation of the
! 64343: ** library.
! 64344: */
! 64345: #ifdef SQLITE_TEST
! 64346: SQLITE_API int sqlite3_found_count = 0;
! 64347: #endif
! 64348:
! 64349: /*
! 64350: ** Test a register to see if it exceeds the current maximum blob size.
! 64351: ** If it does, record the new maximum blob size.
! 64352: */
! 64353: #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
! 64354: # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
! 64355: #else
! 64356: # define UPDATE_MAX_BLOBSIZE(P)
! 64357: #endif
! 64358:
! 64359: /*
! 64360: ** Convert the given register into a string if it isn't one
! 64361: ** already. Return non-zero if a malloc() fails.
! 64362: */
! 64363: #define Stringify(P, enc) \
! 64364: if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
! 64365: { goto no_mem; }
! 64366:
! 64367: /*
! 64368: ** An ephemeral string value (signified by the MEM_Ephem flag) contains
! 64369: ** a pointer to a dynamically allocated string where some other entity
! 64370: ** is responsible for deallocating that string. Because the register
! 64371: ** does not control the string, it might be deleted without the register
! 64372: ** knowing it.
! 64373: **
! 64374: ** This routine converts an ephemeral string into a dynamically allocated
! 64375: ** string that the register itself controls. In other words, it
! 64376: ** converts an MEM_Ephem string into an MEM_Dyn string.
! 64377: */
! 64378: #define Deephemeralize(P) \
! 64379: if( ((P)->flags&MEM_Ephem)!=0 \
! 64380: && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
! 64381:
! 64382: /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
! 64383: #ifdef SQLITE_OMIT_MERGE_SORT
! 64384: # define isSorter(x) 0
! 64385: #else
! 64386: # define isSorter(x) ((x)->pSorter!=0)
! 64387: #endif
! 64388:
! 64389: /*
! 64390: ** Argument pMem points at a register that will be passed to a
! 64391: ** user-defined function or returned to the user as the result of a query.
! 64392: ** This routine sets the pMem->type variable used by the sqlite3_value_*()
! 64393: ** routines.
! 64394: */
! 64395: SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem){
! 64396: int flags = pMem->flags;
! 64397: if( flags & MEM_Null ){
! 64398: pMem->type = SQLITE_NULL;
! 64399: }
! 64400: else if( flags & MEM_Int ){
! 64401: pMem->type = SQLITE_INTEGER;
! 64402: }
! 64403: else if( flags & MEM_Real ){
! 64404: pMem->type = SQLITE_FLOAT;
! 64405: }
! 64406: else if( flags & MEM_Str ){
! 64407: pMem->type = SQLITE_TEXT;
! 64408: }else{
! 64409: pMem->type = SQLITE_BLOB;
! 64410: }
! 64411: }
! 64412:
! 64413: /*
! 64414: ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
! 64415: ** if we run out of memory.
! 64416: */
! 64417: static VdbeCursor *allocateCursor(
! 64418: Vdbe *p, /* The virtual machine */
! 64419: int iCur, /* Index of the new VdbeCursor */
! 64420: int nField, /* Number of fields in the table or index */
! 64421: int iDb, /* Database the cursor belongs to, or -1 */
! 64422: int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */
! 64423: ){
! 64424: /* Find the memory cell that will be used to store the blob of memory
! 64425: ** required for this VdbeCursor structure. It is convenient to use a
! 64426: ** vdbe memory cell to manage the memory allocation required for a
! 64427: ** VdbeCursor structure for the following reasons:
! 64428: **
! 64429: ** * Sometimes cursor numbers are used for a couple of different
! 64430: ** purposes in a vdbe program. The different uses might require
! 64431: ** different sized allocations. Memory cells provide growable
! 64432: ** allocations.
! 64433: **
! 64434: ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
! 64435: ** be freed lazily via the sqlite3_release_memory() API. This
! 64436: ** minimizes the number of malloc calls made by the system.
! 64437: **
! 64438: ** Memory cells for cursors are allocated at the top of the address
! 64439: ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
! 64440: ** cursor 1 is managed by memory cell (p->nMem-1), etc.
! 64441: */
! 64442: Mem *pMem = &p->aMem[p->nMem-iCur];
! 64443:
! 64444: int nByte;
! 64445: VdbeCursor *pCx = 0;
! 64446: nByte =
! 64447: ROUND8(sizeof(VdbeCursor)) +
! 64448: (isBtreeCursor?sqlite3BtreeCursorSize():0) +
! 64449: 2*nField*sizeof(u32);
! 64450:
! 64451: assert( iCur<p->nCursor );
! 64452: if( p->apCsr[iCur] ){
! 64453: sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
! 64454: p->apCsr[iCur] = 0;
! 64455: }
! 64456: if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
! 64457: p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
! 64458: memset(pCx, 0, sizeof(VdbeCursor));
! 64459: pCx->iDb = iDb;
! 64460: pCx->nField = nField;
! 64461: if( nField ){
! 64462: pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
! 64463: }
! 64464: if( isBtreeCursor ){
! 64465: pCx->pCursor = (BtCursor*)
! 64466: &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
! 64467: sqlite3BtreeCursorZero(pCx->pCursor);
! 64468: }
! 64469: }
! 64470: return pCx;
! 64471: }
! 64472:
! 64473: /*
! 64474: ** Try to convert a value into a numeric representation if we can
! 64475: ** do so without loss of information. In other words, if the string
! 64476: ** looks like a number, convert it into a number. If it does not
! 64477: ** look like a number, leave it alone.
! 64478: */
! 64479: static void applyNumericAffinity(Mem *pRec){
! 64480: if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
! 64481: double rValue;
! 64482: i64 iValue;
! 64483: u8 enc = pRec->enc;
! 64484: if( (pRec->flags&MEM_Str)==0 ) return;
! 64485: if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
! 64486: if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
! 64487: pRec->u.i = iValue;
! 64488: pRec->flags |= MEM_Int;
! 64489: }else{
! 64490: pRec->r = rValue;
! 64491: pRec->flags |= MEM_Real;
! 64492: }
! 64493: }
! 64494: }
! 64495:
! 64496: /*
! 64497: ** Processing is determine by the affinity parameter:
! 64498: **
! 64499: ** SQLITE_AFF_INTEGER:
! 64500: ** SQLITE_AFF_REAL:
! 64501: ** SQLITE_AFF_NUMERIC:
! 64502: ** Try to convert pRec to an integer representation or a
! 64503: ** floating-point representation if an integer representation
! 64504: ** is not possible. Note that the integer representation is
! 64505: ** always preferred, even if the affinity is REAL, because
! 64506: ** an integer representation is more space efficient on disk.
! 64507: **
! 64508: ** SQLITE_AFF_TEXT:
! 64509: ** Convert pRec to a text representation.
! 64510: **
! 64511: ** SQLITE_AFF_NONE:
! 64512: ** No-op. pRec is unchanged.
! 64513: */
! 64514: static void applyAffinity(
! 64515: Mem *pRec, /* The value to apply affinity to */
! 64516: char affinity, /* The affinity to be applied */
! 64517: u8 enc /* Use this text encoding */
! 64518: ){
! 64519: if( affinity==SQLITE_AFF_TEXT ){
! 64520: /* Only attempt the conversion to TEXT if there is an integer or real
! 64521: ** representation (blob and NULL do not get converted) but no string
! 64522: ** representation.
! 64523: */
! 64524: if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
! 64525: sqlite3VdbeMemStringify(pRec, enc);
! 64526: }
! 64527: pRec->flags &= ~(MEM_Real|MEM_Int);
! 64528: }else if( affinity!=SQLITE_AFF_NONE ){
! 64529: assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
! 64530: || affinity==SQLITE_AFF_NUMERIC );
! 64531: applyNumericAffinity(pRec);
! 64532: if( pRec->flags & MEM_Real ){
! 64533: sqlite3VdbeIntegerAffinity(pRec);
! 64534: }
! 64535: }
! 64536: }
! 64537:
! 64538: /*
! 64539: ** Try to convert the type of a function argument or a result column
! 64540: ** into a numeric representation. Use either INTEGER or REAL whichever
! 64541: ** is appropriate. But only do the conversion if it is possible without
! 64542: ** loss of information and return the revised type of the argument.
! 64543: */
! 64544: SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
! 64545: Mem *pMem = (Mem*)pVal;
! 64546: if( pMem->type==SQLITE_TEXT ){
! 64547: applyNumericAffinity(pMem);
! 64548: sqlite3VdbeMemStoreType(pMem);
! 64549: }
! 64550: return pMem->type;
! 64551: }
! 64552:
! 64553: /*
! 64554: ** Exported version of applyAffinity(). This one works on sqlite3_value*,
! 64555: ** not the internal Mem* type.
! 64556: */
! 64557: SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
! 64558: sqlite3_value *pVal,
! 64559: u8 affinity,
! 64560: u8 enc
! 64561: ){
! 64562: applyAffinity((Mem *)pVal, affinity, enc);
! 64563: }
! 64564:
! 64565: #ifdef SQLITE_DEBUG
! 64566: /*
! 64567: ** Write a nice string representation of the contents of cell pMem
! 64568: ** into buffer zBuf, length nBuf.
! 64569: */
! 64570: SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
! 64571: char *zCsr = zBuf;
! 64572: int f = pMem->flags;
! 64573:
! 64574: static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
! 64575:
! 64576: if( f&MEM_Blob ){
! 64577: int i;
! 64578: char c;
! 64579: if( f & MEM_Dyn ){
! 64580: c = 'z';
! 64581: assert( (f & (MEM_Static|MEM_Ephem))==0 );
! 64582: }else if( f & MEM_Static ){
! 64583: c = 't';
! 64584: assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
! 64585: }else if( f & MEM_Ephem ){
! 64586: c = 'e';
! 64587: assert( (f & (MEM_Static|MEM_Dyn))==0 );
! 64588: }else{
! 64589: c = 's';
! 64590: }
! 64591:
! 64592: sqlite3_snprintf(100, zCsr, "%c", c);
! 64593: zCsr += sqlite3Strlen30(zCsr);
! 64594: sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
! 64595: zCsr += sqlite3Strlen30(zCsr);
! 64596: for(i=0; i<16 && i<pMem->n; i++){
! 64597: sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
! 64598: zCsr += sqlite3Strlen30(zCsr);
! 64599: }
! 64600: for(i=0; i<16 && i<pMem->n; i++){
! 64601: char z = pMem->z[i];
! 64602: if( z<32 || z>126 ) *zCsr++ = '.';
! 64603: else *zCsr++ = z;
! 64604: }
! 64605:
! 64606: sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
! 64607: zCsr += sqlite3Strlen30(zCsr);
! 64608: if( f & MEM_Zero ){
! 64609: sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
! 64610: zCsr += sqlite3Strlen30(zCsr);
! 64611: }
! 64612: *zCsr = '\0';
! 64613: }else if( f & MEM_Str ){
! 64614: int j, k;
! 64615: zBuf[0] = ' ';
! 64616: if( f & MEM_Dyn ){
! 64617: zBuf[1] = 'z';
! 64618: assert( (f & (MEM_Static|MEM_Ephem))==0 );
! 64619: }else if( f & MEM_Static ){
! 64620: zBuf[1] = 't';
! 64621: assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
! 64622: }else if( f & MEM_Ephem ){
! 64623: zBuf[1] = 'e';
! 64624: assert( (f & (MEM_Static|MEM_Dyn))==0 );
! 64625: }else{
! 64626: zBuf[1] = 's';
! 64627: }
! 64628: k = 2;
! 64629: sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
! 64630: k += sqlite3Strlen30(&zBuf[k]);
! 64631: zBuf[k++] = '[';
! 64632: for(j=0; j<15 && j<pMem->n; j++){
! 64633: u8 c = pMem->z[j];
! 64634: if( c>=0x20 && c<0x7f ){
! 64635: zBuf[k++] = c;
! 64636: }else{
! 64637: zBuf[k++] = '.';
! 64638: }
! 64639: }
! 64640: zBuf[k++] = ']';
! 64641: sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
! 64642: k += sqlite3Strlen30(&zBuf[k]);
! 64643: zBuf[k++] = 0;
! 64644: }
! 64645: }
! 64646: #endif
! 64647:
! 64648: #ifdef SQLITE_DEBUG
! 64649: /*
! 64650: ** Print the value of a register for tracing purposes:
! 64651: */
! 64652: static void memTracePrint(FILE *out, Mem *p){
! 64653: if( p->flags & MEM_Null ){
! 64654: fprintf(out, " NULL");
! 64655: }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
! 64656: fprintf(out, " si:%lld", p->u.i);
! 64657: }else if( p->flags & MEM_Int ){
! 64658: fprintf(out, " i:%lld", p->u.i);
! 64659: #ifndef SQLITE_OMIT_FLOATING_POINT
! 64660: }else if( p->flags & MEM_Real ){
! 64661: fprintf(out, " r:%g", p->r);
! 64662: #endif
! 64663: }else if( p->flags & MEM_RowSet ){
! 64664: fprintf(out, " (rowset)");
! 64665: }else{
! 64666: char zBuf[200];
! 64667: sqlite3VdbeMemPrettyPrint(p, zBuf);
! 64668: fprintf(out, " ");
! 64669: fprintf(out, "%s", zBuf);
! 64670: }
! 64671: }
! 64672: static void registerTrace(FILE *out, int iReg, Mem *p){
! 64673: fprintf(out, "REG[%d] = ", iReg);
! 64674: memTracePrint(out, p);
! 64675: fprintf(out, "\n");
! 64676: }
! 64677: #endif
! 64678:
! 64679: #ifdef SQLITE_DEBUG
! 64680: # define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
! 64681: #else
! 64682: # define REGISTER_TRACE(R,M)
! 64683: #endif
! 64684:
! 64685:
! 64686: #ifdef VDBE_PROFILE
! 64687:
! 64688: /*
! 64689: ** hwtime.h contains inline assembler code for implementing
! 64690: ** high-performance timing routines.
! 64691: */
! 64692: /************** Include hwtime.h in the middle of vdbe.c *********************/
! 64693: /************** Begin file hwtime.h ******************************************/
! 64694: /*
! 64695: ** 2008 May 27
! 64696: **
! 64697: ** The author disclaims copyright to this source code. In place of
! 64698: ** a legal notice, here is a blessing:
! 64699: **
! 64700: ** May you do good and not evil.
! 64701: ** May you find forgiveness for yourself and forgive others.
! 64702: ** May you share freely, never taking more than you give.
! 64703: **
! 64704: ******************************************************************************
! 64705: **
! 64706: ** This file contains inline asm code for retrieving "high-performance"
! 64707: ** counters for x86 class CPUs.
! 64708: */
! 64709: #ifndef _HWTIME_H_
! 64710: #define _HWTIME_H_
! 64711:
! 64712: /*
! 64713: ** The following routine only works on pentium-class (or newer) processors.
! 64714: ** It uses the RDTSC opcode to read the cycle count value out of the
! 64715: ** processor and returns that value. This can be used for high-res
! 64716: ** profiling.
! 64717: */
! 64718: #if (defined(__GNUC__) || defined(_MSC_VER)) && \
! 64719: (defined(i386) || defined(__i386__) || defined(_M_IX86))
! 64720:
! 64721: #if defined(__GNUC__)
! 64722:
! 64723: __inline__ sqlite_uint64 sqlite3Hwtime(void){
! 64724: unsigned int lo, hi;
! 64725: __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
! 64726: return (sqlite_uint64)hi << 32 | lo;
! 64727: }
! 64728:
! 64729: #elif defined(_MSC_VER)
! 64730:
! 64731: __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
! 64732: __asm {
! 64733: rdtsc
! 64734: ret ; return value at EDX:EAX
! 64735: }
! 64736: }
! 64737:
! 64738: #endif
! 64739:
! 64740: #elif (defined(__GNUC__) && defined(__x86_64__))
! 64741:
! 64742: __inline__ sqlite_uint64 sqlite3Hwtime(void){
! 64743: unsigned long val;
! 64744: __asm__ __volatile__ ("rdtsc" : "=A" (val));
! 64745: return val;
! 64746: }
! 64747:
! 64748: #elif (defined(__GNUC__) && defined(__ppc__))
! 64749:
! 64750: __inline__ sqlite_uint64 sqlite3Hwtime(void){
! 64751: unsigned long long retval;
! 64752: unsigned long junk;
! 64753: __asm__ __volatile__ ("\n\
! 64754: 1: mftbu %1\n\
! 64755: mftb %L0\n\
! 64756: mftbu %0\n\
! 64757: cmpw %0,%1\n\
! 64758: bne 1b"
! 64759: : "=r" (retval), "=r" (junk));
! 64760: return retval;
! 64761: }
! 64762:
! 64763: #else
! 64764:
! 64765: #error Need implementation of sqlite3Hwtime() for your platform.
! 64766:
! 64767: /*
! 64768: ** To compile without implementing sqlite3Hwtime() for your platform,
! 64769: ** you can remove the above #error and use the following
! 64770: ** stub function. You will lose timing support for many
! 64771: ** of the debugging and testing utilities, but it should at
! 64772: ** least compile and run.
! 64773: */
! 64774: SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
! 64775:
! 64776: #endif
! 64777:
! 64778: #endif /* !defined(_HWTIME_H_) */
! 64779:
! 64780: /************** End of hwtime.h **********************************************/
! 64781: /************** Continuing where we left off in vdbe.c ***********************/
! 64782:
! 64783: #endif
! 64784:
! 64785: /*
! 64786: ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
! 64787: ** sqlite3_interrupt() routine has been called. If it has been, then
! 64788: ** processing of the VDBE program is interrupted.
! 64789: **
! 64790: ** This macro added to every instruction that does a jump in order to
! 64791: ** implement a loop. This test used to be on every single instruction,
! 64792: ** but that meant we more testing than we needed. By only testing the
! 64793: ** flag on jump instructions, we get a (small) speed improvement.
! 64794: */
! 64795: #define CHECK_FOR_INTERRUPT \
! 64796: if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
! 64797:
! 64798:
! 64799: #ifndef NDEBUG
! 64800: /*
! 64801: ** This function is only called from within an assert() expression. It
! 64802: ** checks that the sqlite3.nTransaction variable is correctly set to
! 64803: ** the number of non-transaction savepoints currently in the
! 64804: ** linked list starting at sqlite3.pSavepoint.
! 64805: **
! 64806: ** Usage:
! 64807: **
! 64808: ** assert( checkSavepointCount(db) );
! 64809: */
! 64810: static int checkSavepointCount(sqlite3 *db){
! 64811: int n = 0;
! 64812: Savepoint *p;
! 64813: for(p=db->pSavepoint; p; p=p->pNext) n++;
! 64814: assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
! 64815: return 1;
! 64816: }
! 64817: #endif
! 64818:
! 64819: /*
! 64820: ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
! 64821: ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
! 64822: ** in memory obtained from sqlite3DbMalloc).
! 64823: */
! 64824: static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
! 64825: sqlite3 *db = p->db;
! 64826: sqlite3DbFree(db, p->zErrMsg);
! 64827: p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
! 64828: sqlite3_free(pVtab->zErrMsg);
! 64829: pVtab->zErrMsg = 0;
! 64830: }
! 64831:
! 64832:
! 64833: /*
! 64834: ** Execute as much of a VDBE program as we can then return.
! 64835: **
! 64836: ** sqlite3VdbeMakeReady() must be called before this routine in order to
! 64837: ** close the program with a final OP_Halt and to set up the callbacks
! 64838: ** and the error message pointer.
! 64839: **
! 64840: ** Whenever a row or result data is available, this routine will either
! 64841: ** invoke the result callback (if there is one) or return with
! 64842: ** SQLITE_ROW.
! 64843: **
! 64844: ** If an attempt is made to open a locked database, then this routine
! 64845: ** will either invoke the busy callback (if there is one) or it will
! 64846: ** return SQLITE_BUSY.
! 64847: **
! 64848: ** If an error occurs, an error message is written to memory obtained
! 64849: ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
! 64850: ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
! 64851: **
! 64852: ** If the callback ever returns non-zero, then the program exits
! 64853: ** immediately. There will be no error message but the p->rc field is
! 64854: ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
! 64855: **
! 64856: ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
! 64857: ** routine to return SQLITE_ERROR.
! 64858: **
! 64859: ** Other fatal errors return SQLITE_ERROR.
! 64860: **
! 64861: ** After this routine has finished, sqlite3VdbeFinalize() should be
! 64862: ** used to clean up the mess that was left behind.
! 64863: */
! 64864: SQLITE_PRIVATE int sqlite3VdbeExec(
! 64865: Vdbe *p /* The VDBE */
! 64866: ){
! 64867: int pc=0; /* The program counter */
! 64868: Op *aOp = p->aOp; /* Copy of p->aOp */
! 64869: Op *pOp; /* Current operation */
! 64870: int rc = SQLITE_OK; /* Value to return */
! 64871: sqlite3 *db = p->db; /* The database */
! 64872: u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
! 64873: u8 encoding = ENC(db); /* The database encoding */
! 64874: #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
! 64875: int checkProgress; /* True if progress callbacks are enabled */
! 64876: int nProgressOps = 0; /* Opcodes executed since progress callback. */
! 64877: #endif
! 64878: Mem *aMem = p->aMem; /* Copy of p->aMem */
! 64879: Mem *pIn1 = 0; /* 1st input operand */
! 64880: Mem *pIn2 = 0; /* 2nd input operand */
! 64881: Mem *pIn3 = 0; /* 3rd input operand */
! 64882: Mem *pOut = 0; /* Output operand */
! 64883: int iCompare = 0; /* Result of last OP_Compare operation */
! 64884: int *aPermute = 0; /* Permutation of columns for OP_Compare */
! 64885: i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
! 64886: #ifdef VDBE_PROFILE
! 64887: u64 start; /* CPU clock count at start of opcode */
! 64888: int origPc; /* Program counter at start of opcode */
! 64889: #endif
! 64890: /********************************************************************
! 64891: ** Automatically generated code
! 64892: **
! 64893: ** The following union is automatically generated by the
! 64894: ** vdbe-compress.tcl script. The purpose of this union is to
! 64895: ** reduce the amount of stack space required by this function.
! 64896: ** See comments in the vdbe-compress.tcl script for details.
! 64897: */
! 64898: union vdbeExecUnion {
! 64899: struct OP_Yield_stack_vars {
! 64900: int pcDest;
! 64901: } aa;
! 64902: struct OP_Null_stack_vars {
! 64903: int cnt;
! 64904: } ab;
! 64905: struct OP_Variable_stack_vars {
! 64906: Mem *pVar; /* Value being transferred */
! 64907: } ac;
! 64908: struct OP_Move_stack_vars {
! 64909: char *zMalloc; /* Holding variable for allocated memory */
! 64910: int n; /* Number of registers left to copy */
! 64911: int p1; /* Register to copy from */
! 64912: int p2; /* Register to copy to */
! 64913: } ad;
! 64914: struct OP_ResultRow_stack_vars {
! 64915: Mem *pMem;
! 64916: int i;
! 64917: } ae;
! 64918: struct OP_Concat_stack_vars {
! 64919: i64 nByte;
! 64920: } af;
! 64921: struct OP_Remainder_stack_vars {
! 64922: int flags; /* Combined MEM_* flags from both inputs */
! 64923: i64 iA; /* Integer value of left operand */
! 64924: i64 iB; /* Integer value of right operand */
! 64925: double rA; /* Real value of left operand */
! 64926: double rB; /* Real value of right operand */
! 64927: } ag;
! 64928: struct OP_Function_stack_vars {
! 64929: int i;
! 64930: Mem *pArg;
! 64931: sqlite3_context ctx;
! 64932: sqlite3_value **apVal;
! 64933: int n;
! 64934: } ah;
! 64935: struct OP_ShiftRight_stack_vars {
! 64936: i64 iA;
! 64937: u64 uA;
! 64938: i64 iB;
! 64939: u8 op;
! 64940: } ai;
! 64941: struct OP_Ge_stack_vars {
! 64942: int res; /* Result of the comparison of pIn1 against pIn3 */
! 64943: char affinity; /* Affinity to use for comparison */
! 64944: u16 flags1; /* Copy of initial value of pIn1->flags */
! 64945: u16 flags3; /* Copy of initial value of pIn3->flags */
! 64946: } aj;
! 64947: struct OP_Compare_stack_vars {
! 64948: int n;
! 64949: int i;
! 64950: int p1;
! 64951: int p2;
! 64952: const KeyInfo *pKeyInfo;
! 64953: int idx;
! 64954: CollSeq *pColl; /* Collating sequence to use on this term */
! 64955: int bRev; /* True for DESCENDING sort order */
! 64956: } ak;
! 64957: struct OP_Or_stack_vars {
! 64958: int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
! 64959: int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
! 64960: } al;
! 64961: struct OP_IfNot_stack_vars {
! 64962: int c;
! 64963: } am;
! 64964: struct OP_Column_stack_vars {
! 64965: u32 payloadSize; /* Number of bytes in the record */
! 64966: i64 payloadSize64; /* Number of bytes in the record */
! 64967: int p1; /* P1 value of the opcode */
! 64968: int p2; /* column number to retrieve */
! 64969: VdbeCursor *pC; /* The VDBE cursor */
! 64970: char *zRec; /* Pointer to complete record-data */
! 64971: BtCursor *pCrsr; /* The BTree cursor */
! 64972: u32 *aType; /* aType[i] holds the numeric type of the i-th column */
! 64973: u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
! 64974: int nField; /* number of fields in the record */
! 64975: int len; /* The length of the serialized data for the column */
! 64976: int i; /* Loop counter */
! 64977: char *zData; /* Part of the record being decoded */
! 64978: Mem *pDest; /* Where to write the extracted value */
! 64979: Mem sMem; /* For storing the record being decoded */
! 64980: u8 *zIdx; /* Index into header */
! 64981: u8 *zEndHdr; /* Pointer to first byte after the header */
! 64982: u32 offset; /* Offset into the data */
! 64983: u32 szField; /* Number of bytes in the content of a field */
! 64984: int szHdr; /* Size of the header size field at start of record */
! 64985: int avail; /* Number of bytes of available data */
! 64986: u32 t; /* A type code from the record header */
! 64987: Mem *pReg; /* PseudoTable input register */
! 64988: } an;
! 64989: struct OP_Affinity_stack_vars {
! 64990: const char *zAffinity; /* The affinity to be applied */
! 64991: char cAff; /* A single character of affinity */
! 64992: } ao;
! 64993: struct OP_MakeRecord_stack_vars {
! 64994: u8 *zNewRecord; /* A buffer to hold the data for the new record */
! 64995: Mem *pRec; /* The new record */
! 64996: u64 nData; /* Number of bytes of data space */
! 64997: int nHdr; /* Number of bytes of header space */
! 64998: i64 nByte; /* Data space required for this record */
! 64999: int nZero; /* Number of zero bytes at the end of the record */
! 65000: int nVarint; /* Number of bytes in a varint */
! 65001: u32 serial_type; /* Type field */
! 65002: Mem *pData0; /* First field to be combined into the record */
! 65003: Mem *pLast; /* Last field of the record */
! 65004: int nField; /* Number of fields in the record */
! 65005: char *zAffinity; /* The affinity string for the record */
! 65006: int file_format; /* File format to use for encoding */
! 65007: int i; /* Space used in zNewRecord[] */
! 65008: int len; /* Length of a field */
! 65009: } ap;
! 65010: struct OP_Count_stack_vars {
! 65011: i64 nEntry;
! 65012: BtCursor *pCrsr;
! 65013: } aq;
! 65014: struct OP_Savepoint_stack_vars {
! 65015: int p1; /* Value of P1 operand */
! 65016: char *zName; /* Name of savepoint */
! 65017: int nName;
! 65018: Savepoint *pNew;
! 65019: Savepoint *pSavepoint;
! 65020: Savepoint *pTmp;
! 65021: int iSavepoint;
! 65022: int ii;
! 65023: } ar;
! 65024: struct OP_AutoCommit_stack_vars {
! 65025: int desiredAutoCommit;
! 65026: int iRollback;
! 65027: int turnOnAC;
! 65028: } as;
! 65029: struct OP_Transaction_stack_vars {
! 65030: Btree *pBt;
! 65031: } at;
! 65032: struct OP_ReadCookie_stack_vars {
! 65033: int iMeta;
! 65034: int iDb;
! 65035: int iCookie;
! 65036: } au;
! 65037: struct OP_SetCookie_stack_vars {
! 65038: Db *pDb;
! 65039: } av;
! 65040: struct OP_VerifyCookie_stack_vars {
! 65041: int iMeta;
! 65042: int iGen;
! 65043: Btree *pBt;
! 65044: } aw;
! 65045: struct OP_OpenWrite_stack_vars {
! 65046: int nField;
! 65047: KeyInfo *pKeyInfo;
! 65048: int p2;
! 65049: int iDb;
! 65050: int wrFlag;
! 65051: Btree *pX;
! 65052: VdbeCursor *pCur;
! 65053: Db *pDb;
! 65054: } ax;
! 65055: struct OP_OpenEphemeral_stack_vars {
! 65056: VdbeCursor *pCx;
! 65057: } ay;
! 65058: struct OP_SorterOpen_stack_vars {
! 65059: VdbeCursor *pCx;
! 65060: } az;
! 65061: struct OP_OpenPseudo_stack_vars {
! 65062: VdbeCursor *pCx;
! 65063: } ba;
! 65064: struct OP_SeekGt_stack_vars {
! 65065: int res;
! 65066: int oc;
! 65067: VdbeCursor *pC;
! 65068: UnpackedRecord r;
! 65069: int nField;
! 65070: i64 iKey; /* The rowid we are to seek to */
! 65071: } bb;
! 65072: struct OP_Seek_stack_vars {
! 65073: VdbeCursor *pC;
! 65074: } bc;
! 65075: struct OP_Found_stack_vars {
! 65076: int alreadyExists;
! 65077: VdbeCursor *pC;
! 65078: int res;
! 65079: char *pFree;
! 65080: UnpackedRecord *pIdxKey;
! 65081: UnpackedRecord r;
! 65082: char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
! 65083: } bd;
! 65084: struct OP_IsUnique_stack_vars {
! 65085: u16 ii;
! 65086: VdbeCursor *pCx;
! 65087: BtCursor *pCrsr;
! 65088: u16 nField;
! 65089: Mem *aMx;
! 65090: UnpackedRecord r; /* B-Tree index search key */
! 65091: i64 R; /* Rowid stored in register P3 */
! 65092: } be;
! 65093: struct OP_NotExists_stack_vars {
! 65094: VdbeCursor *pC;
! 65095: BtCursor *pCrsr;
! 65096: int res;
! 65097: u64 iKey;
! 65098: } bf;
! 65099: struct OP_NewRowid_stack_vars {
! 65100: i64 v; /* The new rowid */
! 65101: VdbeCursor *pC; /* Cursor of table to get the new rowid */
! 65102: int res; /* Result of an sqlite3BtreeLast() */
! 65103: int cnt; /* Counter to limit the number of searches */
! 65104: Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
! 65105: VdbeFrame *pFrame; /* Root frame of VDBE */
! 65106: } bg;
! 65107: struct OP_InsertInt_stack_vars {
! 65108: Mem *pData; /* MEM cell holding data for the record to be inserted */
! 65109: Mem *pKey; /* MEM cell holding key for the record */
! 65110: i64 iKey; /* The integer ROWID or key for the record to be inserted */
! 65111: VdbeCursor *pC; /* Cursor to table into which insert is written */
! 65112: int nZero; /* Number of zero-bytes to append */
! 65113: int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
! 65114: const char *zDb; /* database name - used by the update hook */
! 65115: const char *zTbl; /* Table name - used by the opdate hook */
! 65116: int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
! 65117: } bh;
! 65118: struct OP_Delete_stack_vars {
! 65119: i64 iKey;
! 65120: VdbeCursor *pC;
! 65121: } bi;
! 65122: struct OP_SorterCompare_stack_vars {
! 65123: VdbeCursor *pC;
! 65124: int res;
! 65125: } bj;
! 65126: struct OP_SorterData_stack_vars {
! 65127: VdbeCursor *pC;
! 65128: } bk;
! 65129: struct OP_RowData_stack_vars {
! 65130: VdbeCursor *pC;
! 65131: BtCursor *pCrsr;
! 65132: u32 n;
! 65133: i64 n64;
! 65134: } bl;
! 65135: struct OP_Rowid_stack_vars {
! 65136: VdbeCursor *pC;
! 65137: i64 v;
! 65138: sqlite3_vtab *pVtab;
! 65139: const sqlite3_module *pModule;
! 65140: } bm;
! 65141: struct OP_NullRow_stack_vars {
! 65142: VdbeCursor *pC;
! 65143: } bn;
! 65144: struct OP_Last_stack_vars {
! 65145: VdbeCursor *pC;
! 65146: BtCursor *pCrsr;
! 65147: int res;
! 65148: } bo;
! 65149: struct OP_Rewind_stack_vars {
! 65150: VdbeCursor *pC;
! 65151: BtCursor *pCrsr;
! 65152: int res;
! 65153: } bp;
! 65154: struct OP_Next_stack_vars {
! 65155: VdbeCursor *pC;
! 65156: int res;
! 65157: } bq;
! 65158: struct OP_IdxInsert_stack_vars {
! 65159: VdbeCursor *pC;
! 65160: BtCursor *pCrsr;
! 65161: int nKey;
! 65162: const char *zKey;
! 65163: } br;
! 65164: struct OP_IdxDelete_stack_vars {
! 65165: VdbeCursor *pC;
! 65166: BtCursor *pCrsr;
! 65167: int res;
! 65168: UnpackedRecord r;
! 65169: } bs;
! 65170: struct OP_IdxRowid_stack_vars {
! 65171: BtCursor *pCrsr;
! 65172: VdbeCursor *pC;
! 65173: i64 rowid;
! 65174: } bt;
! 65175: struct OP_IdxGE_stack_vars {
! 65176: VdbeCursor *pC;
! 65177: int res;
! 65178: UnpackedRecord r;
! 65179: } bu;
! 65180: struct OP_Destroy_stack_vars {
! 65181: int iMoved;
! 65182: int iCnt;
! 65183: Vdbe *pVdbe;
! 65184: int iDb;
! 65185: } bv;
! 65186: struct OP_Clear_stack_vars {
! 65187: int nChange;
! 65188: } bw;
! 65189: struct OP_CreateTable_stack_vars {
! 65190: int pgno;
! 65191: int flags;
! 65192: Db *pDb;
! 65193: } bx;
! 65194: struct OP_ParseSchema_stack_vars {
! 65195: int iDb;
! 65196: const char *zMaster;
! 65197: char *zSql;
! 65198: InitData initData;
! 65199: } by;
! 65200: struct OP_IntegrityCk_stack_vars {
! 65201: int nRoot; /* Number of tables to check. (Number of root pages.) */
! 65202: int *aRoot; /* Array of rootpage numbers for tables to be checked */
! 65203: int j; /* Loop counter */
! 65204: int nErr; /* Number of errors reported */
! 65205: char *z; /* Text of the error report */
! 65206: Mem *pnErr; /* Register keeping track of errors remaining */
! 65207: } bz;
! 65208: struct OP_RowSetRead_stack_vars {
! 65209: i64 val;
! 65210: } ca;
! 65211: struct OP_RowSetTest_stack_vars {
! 65212: int iSet;
! 65213: int exists;
! 65214: } cb;
! 65215: struct OP_Program_stack_vars {
! 65216: int nMem; /* Number of memory registers for sub-program */
! 65217: int nByte; /* Bytes of runtime space required for sub-program */
! 65218: Mem *pRt; /* Register to allocate runtime space */
! 65219: Mem *pMem; /* Used to iterate through memory cells */
! 65220: Mem *pEnd; /* Last memory cell in new array */
! 65221: VdbeFrame *pFrame; /* New vdbe frame to execute in */
! 65222: SubProgram *pProgram; /* Sub-program to execute */
! 65223: void *t; /* Token identifying trigger */
! 65224: } cc;
! 65225: struct OP_Param_stack_vars {
! 65226: VdbeFrame *pFrame;
! 65227: Mem *pIn;
! 65228: } cd;
! 65229: struct OP_MemMax_stack_vars {
! 65230: Mem *pIn1;
! 65231: VdbeFrame *pFrame;
! 65232: } ce;
! 65233: struct OP_AggStep_stack_vars {
! 65234: int n;
! 65235: int i;
! 65236: Mem *pMem;
! 65237: Mem *pRec;
! 65238: sqlite3_context ctx;
! 65239: sqlite3_value **apVal;
! 65240: } cf;
! 65241: struct OP_AggFinal_stack_vars {
! 65242: Mem *pMem;
! 65243: } cg;
! 65244: struct OP_Checkpoint_stack_vars {
! 65245: int i; /* Loop counter */
! 65246: int aRes[3]; /* Results */
! 65247: Mem *pMem; /* Write results here */
! 65248: } ch;
! 65249: struct OP_JournalMode_stack_vars {
! 65250: Btree *pBt; /* Btree to change journal mode of */
! 65251: Pager *pPager; /* Pager associated with pBt */
! 65252: int eNew; /* New journal mode */
! 65253: int eOld; /* The old journal mode */
! 65254: const char *zFilename; /* Name of database file for pPager */
! 65255: } ci;
! 65256: struct OP_IncrVacuum_stack_vars {
! 65257: Btree *pBt;
! 65258: } cj;
! 65259: struct OP_VBegin_stack_vars {
! 65260: VTable *pVTab;
! 65261: } ck;
! 65262: struct OP_VOpen_stack_vars {
! 65263: VdbeCursor *pCur;
! 65264: sqlite3_vtab_cursor *pVtabCursor;
! 65265: sqlite3_vtab *pVtab;
! 65266: sqlite3_module *pModule;
! 65267: } cl;
! 65268: struct OP_VFilter_stack_vars {
! 65269: int nArg;
! 65270: int iQuery;
! 65271: const sqlite3_module *pModule;
! 65272: Mem *pQuery;
! 65273: Mem *pArgc;
! 65274: sqlite3_vtab_cursor *pVtabCursor;
! 65275: sqlite3_vtab *pVtab;
! 65276: VdbeCursor *pCur;
! 65277: int res;
! 65278: int i;
! 65279: Mem **apArg;
! 65280: } cm;
! 65281: struct OP_VColumn_stack_vars {
! 65282: sqlite3_vtab *pVtab;
! 65283: const sqlite3_module *pModule;
! 65284: Mem *pDest;
! 65285: sqlite3_context sContext;
! 65286: } cn;
! 65287: struct OP_VNext_stack_vars {
! 65288: sqlite3_vtab *pVtab;
! 65289: const sqlite3_module *pModule;
! 65290: int res;
! 65291: VdbeCursor *pCur;
! 65292: } co;
! 65293: struct OP_VRename_stack_vars {
! 65294: sqlite3_vtab *pVtab;
! 65295: Mem *pName;
! 65296: } cp;
! 65297: struct OP_VUpdate_stack_vars {
! 65298: sqlite3_vtab *pVtab;
! 65299: sqlite3_module *pModule;
! 65300: int nArg;
! 65301: int i;
! 65302: sqlite_int64 rowid;
! 65303: Mem **apArg;
! 65304: Mem *pX;
! 65305: } cq;
! 65306: struct OP_Trace_stack_vars {
! 65307: char *zTrace;
! 65308: char *z;
! 65309: } cr;
! 65310: } u;
! 65311: /* End automatically generated code
! 65312: ********************************************************************/
! 65313:
! 65314: assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
! 65315: sqlite3VdbeEnter(p);
! 65316: if( p->rc==SQLITE_NOMEM ){
! 65317: /* This happens if a malloc() inside a call to sqlite3_column_text() or
! 65318: ** sqlite3_column_text16() failed. */
! 65319: goto no_mem;
! 65320: }
! 65321: assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
! 65322: p->rc = SQLITE_OK;
! 65323: assert( p->explain==0 );
! 65324: p->pResultSet = 0;
! 65325: db->busyHandler.nBusy = 0;
! 65326: CHECK_FOR_INTERRUPT;
! 65327: sqlite3VdbeIOTraceSql(p);
! 65328: #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
! 65329: checkProgress = db->xProgress!=0;
! 65330: #endif
! 65331: #ifdef SQLITE_DEBUG
! 65332: sqlite3BeginBenignMalloc();
! 65333: if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){
! 65334: int i;
! 65335: printf("VDBE Program Listing:\n");
! 65336: sqlite3VdbePrintSql(p);
! 65337: for(i=0; i<p->nOp; i++){
! 65338: sqlite3VdbePrintOp(stdout, i, &aOp[i]);
! 65339: }
! 65340: }
! 65341: sqlite3EndBenignMalloc();
! 65342: #endif
! 65343: for(pc=p->pc; rc==SQLITE_OK; pc++){
! 65344: assert( pc>=0 && pc<p->nOp );
! 65345: if( db->mallocFailed ) goto no_mem;
! 65346: #ifdef VDBE_PROFILE
! 65347: origPc = pc;
! 65348: start = sqlite3Hwtime();
! 65349: #endif
! 65350: pOp = &aOp[pc];
! 65351:
! 65352: /* Only allow tracing if SQLITE_DEBUG is defined.
! 65353: */
! 65354: #ifdef SQLITE_DEBUG
! 65355: if( p->trace ){
! 65356: if( pc==0 ){
! 65357: printf("VDBE Execution Trace:\n");
! 65358: sqlite3VdbePrintSql(p);
! 65359: }
! 65360: sqlite3VdbePrintOp(p->trace, pc, pOp);
! 65361: }
! 65362: #endif
! 65363:
! 65364:
! 65365: /* Check to see if we need to simulate an interrupt. This only happens
! 65366: ** if we have a special test build.
! 65367: */
! 65368: #ifdef SQLITE_TEST
! 65369: if( sqlite3_interrupt_count>0 ){
! 65370: sqlite3_interrupt_count--;
! 65371: if( sqlite3_interrupt_count==0 ){
! 65372: sqlite3_interrupt(db);
! 65373: }
! 65374: }
! 65375: #endif
! 65376:
! 65377: #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
! 65378: /* Call the progress callback if it is configured and the required number
! 65379: ** of VDBE ops have been executed (either since this invocation of
! 65380: ** sqlite3VdbeExec() or since last time the progress callback was called).
! 65381: ** If the progress callback returns non-zero, exit the virtual machine with
! 65382: ** a return code SQLITE_ABORT.
! 65383: */
! 65384: if( checkProgress ){
! 65385: if( db->nProgressOps==nProgressOps ){
! 65386: int prc;
! 65387: prc = db->xProgress(db->pProgressArg);
! 65388: if( prc!=0 ){
! 65389: rc = SQLITE_INTERRUPT;
! 65390: goto vdbe_error_halt;
! 65391: }
! 65392: nProgressOps = 0;
! 65393: }
! 65394: nProgressOps++;
! 65395: }
! 65396: #endif
! 65397:
! 65398: /* On any opcode with the "out2-prerelase" tag, free any
! 65399: ** external allocations out of mem[p2] and set mem[p2] to be
! 65400: ** an undefined integer. Opcodes will either fill in the integer
! 65401: ** value or convert mem[p2] to a different type.
! 65402: */
! 65403: assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
! 65404: if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
! 65405: assert( pOp->p2>0 );
! 65406: assert( pOp->p2<=p->nMem );
! 65407: pOut = &aMem[pOp->p2];
! 65408: memAboutToChange(p, pOut);
! 65409: VdbeMemRelease(pOut);
! 65410: pOut->flags = MEM_Int;
! 65411: }
! 65412:
! 65413: /* Sanity checking on other operands */
! 65414: #ifdef SQLITE_DEBUG
! 65415: if( (pOp->opflags & OPFLG_IN1)!=0 ){
! 65416: assert( pOp->p1>0 );
! 65417: assert( pOp->p1<=p->nMem );
! 65418: assert( memIsValid(&aMem[pOp->p1]) );
! 65419: REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
! 65420: }
! 65421: if( (pOp->opflags & OPFLG_IN2)!=0 ){
! 65422: assert( pOp->p2>0 );
! 65423: assert( pOp->p2<=p->nMem );
! 65424: assert( memIsValid(&aMem[pOp->p2]) );
! 65425: REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
! 65426: }
! 65427: if( (pOp->opflags & OPFLG_IN3)!=0 ){
! 65428: assert( pOp->p3>0 );
! 65429: assert( pOp->p3<=p->nMem );
! 65430: assert( memIsValid(&aMem[pOp->p3]) );
! 65431: REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
! 65432: }
! 65433: if( (pOp->opflags & OPFLG_OUT2)!=0 ){
! 65434: assert( pOp->p2>0 );
! 65435: assert( pOp->p2<=p->nMem );
! 65436: memAboutToChange(p, &aMem[pOp->p2]);
! 65437: }
! 65438: if( (pOp->opflags & OPFLG_OUT3)!=0 ){
! 65439: assert( pOp->p3>0 );
! 65440: assert( pOp->p3<=p->nMem );
! 65441: memAboutToChange(p, &aMem[pOp->p3]);
! 65442: }
! 65443: #endif
! 65444:
! 65445: switch( pOp->opcode ){
! 65446:
! 65447: /*****************************************************************************
! 65448: ** What follows is a massive switch statement where each case implements a
! 65449: ** separate instruction in the virtual machine. If we follow the usual
! 65450: ** indentation conventions, each case should be indented by 6 spaces. But
! 65451: ** that is a lot of wasted space on the left margin. So the code within
! 65452: ** the switch statement will break with convention and be flush-left. Another
! 65453: ** big comment (similar to this one) will mark the point in the code where
! 65454: ** we transition back to normal indentation.
! 65455: **
! 65456: ** The formatting of each case is important. The makefile for SQLite
! 65457: ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
! 65458: ** file looking for lines that begin with "case OP_". The opcodes.h files
! 65459: ** will be filled with #defines that give unique integer values to each
! 65460: ** opcode and the opcodes.c file is filled with an array of strings where
! 65461: ** each string is the symbolic name for the corresponding opcode. If the
! 65462: ** case statement is followed by a comment of the form "/# same as ... #/"
! 65463: ** that comment is used to determine the particular value of the opcode.
! 65464: **
! 65465: ** Other keywords in the comment that follows each case are used to
! 65466: ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
! 65467: ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
! 65468: ** the mkopcodeh.awk script for additional information.
! 65469: **
! 65470: ** Documentation about VDBE opcodes is generated by scanning this file
! 65471: ** for lines of that contain "Opcode:". That line and all subsequent
! 65472: ** comment lines are used in the generation of the opcode.html documentation
! 65473: ** file.
! 65474: **
! 65475: ** SUMMARY:
! 65476: **
! 65477: ** Formatting is important to scripts that scan this file.
! 65478: ** Do not deviate from the formatting style currently in use.
! 65479: **
! 65480: *****************************************************************************/
! 65481:
! 65482: /* Opcode: Goto * P2 * * *
! 65483: **
! 65484: ** An unconditional jump to address P2.
! 65485: ** The next instruction executed will be
! 65486: ** the one at index P2 from the beginning of
! 65487: ** the program.
! 65488: */
! 65489: case OP_Goto: { /* jump */
! 65490: CHECK_FOR_INTERRUPT;
! 65491: pc = pOp->p2 - 1;
! 65492: break;
! 65493: }
! 65494:
! 65495: /* Opcode: Gosub P1 P2 * * *
! 65496: **
! 65497: ** Write the current address onto register P1
! 65498: ** and then jump to address P2.
! 65499: */
! 65500: case OP_Gosub: { /* jump */
! 65501: assert( pOp->p1>0 && pOp->p1<=p->nMem );
! 65502: pIn1 = &aMem[pOp->p1];
! 65503: assert( (pIn1->flags & MEM_Dyn)==0 );
! 65504: memAboutToChange(p, pIn1);
! 65505: pIn1->flags = MEM_Int;
! 65506: pIn1->u.i = pc;
! 65507: REGISTER_TRACE(pOp->p1, pIn1);
! 65508: pc = pOp->p2 - 1;
! 65509: break;
! 65510: }
! 65511:
! 65512: /* Opcode: Return P1 * * * *
! 65513: **
! 65514: ** Jump to the next instruction after the address in register P1.
! 65515: */
! 65516: case OP_Return: { /* in1 */
! 65517: pIn1 = &aMem[pOp->p1];
! 65518: assert( pIn1->flags & MEM_Int );
! 65519: pc = (int)pIn1->u.i;
! 65520: break;
! 65521: }
! 65522:
! 65523: /* Opcode: Yield P1 * * * *
! 65524: **
! 65525: ** Swap the program counter with the value in register P1.
! 65526: */
! 65527: case OP_Yield: { /* in1 */
! 65528: #if 0 /* local variables moved into u.aa */
! 65529: int pcDest;
! 65530: #endif /* local variables moved into u.aa */
! 65531: pIn1 = &aMem[pOp->p1];
! 65532: assert( (pIn1->flags & MEM_Dyn)==0 );
! 65533: pIn1->flags = MEM_Int;
! 65534: u.aa.pcDest = (int)pIn1->u.i;
! 65535: pIn1->u.i = pc;
! 65536: REGISTER_TRACE(pOp->p1, pIn1);
! 65537: pc = u.aa.pcDest;
! 65538: break;
! 65539: }
! 65540:
! 65541: /* Opcode: HaltIfNull P1 P2 P3 P4 *
! 65542: **
! 65543: ** Check the value in register P3. If it is NULL then Halt using
! 65544: ** parameter P1, P2, and P4 as if this were a Halt instruction. If the
! 65545: ** value in register P3 is not NULL, then this routine is a no-op.
! 65546: */
! 65547: case OP_HaltIfNull: { /* in3 */
! 65548: pIn3 = &aMem[pOp->p3];
! 65549: if( (pIn3->flags & MEM_Null)==0 ) break;
! 65550: /* Fall through into OP_Halt */
! 65551: }
! 65552:
! 65553: /* Opcode: Halt P1 P2 * P4 *
! 65554: **
! 65555: ** Exit immediately. All open cursors, etc are closed
! 65556: ** automatically.
! 65557: **
! 65558: ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
! 65559: ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
! 65560: ** For errors, it can be some other value. If P1!=0 then P2 will determine
! 65561: ** whether or not to rollback the current transaction. Do not rollback
! 65562: ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
! 65563: ** then back out all changes that have occurred during this execution of the
! 65564: ** VDBE, but do not rollback the transaction.
! 65565: **
! 65566: ** If P4 is not null then it is an error message string.
! 65567: **
! 65568: ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
! 65569: ** every program. So a jump past the last instruction of the program
! 65570: ** is the same as executing Halt.
! 65571: */
! 65572: case OP_Halt: {
! 65573: if( pOp->p1==SQLITE_OK && p->pFrame ){
! 65574: /* Halt the sub-program. Return control to the parent frame. */
! 65575: VdbeFrame *pFrame = p->pFrame;
! 65576: p->pFrame = pFrame->pParent;
! 65577: p->nFrame--;
! 65578: sqlite3VdbeSetChanges(db, p->nChange);
! 65579: pc = sqlite3VdbeFrameRestore(pFrame);
! 65580: lastRowid = db->lastRowid;
! 65581: if( pOp->p2==OE_Ignore ){
! 65582: /* Instruction pc is the OP_Program that invoked the sub-program
! 65583: ** currently being halted. If the p2 instruction of this OP_Halt
! 65584: ** instruction is set to OE_Ignore, then the sub-program is throwing
! 65585: ** an IGNORE exception. In this case jump to the address specified
! 65586: ** as the p2 of the calling OP_Program. */
! 65587: pc = p->aOp[pc].p2-1;
! 65588: }
! 65589: aOp = p->aOp;
! 65590: aMem = p->aMem;
! 65591: break;
! 65592: }
! 65593:
! 65594: p->rc = pOp->p1;
! 65595: p->errorAction = (u8)pOp->p2;
! 65596: p->pc = pc;
! 65597: if( pOp->p4.z ){
! 65598: assert( p->rc!=SQLITE_OK );
! 65599: sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
! 65600: testcase( sqlite3GlobalConfig.xLog!=0 );
! 65601: sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
! 65602: }else if( p->rc ){
! 65603: testcase( sqlite3GlobalConfig.xLog!=0 );
! 65604: sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
! 65605: }
! 65606: rc = sqlite3VdbeHalt(p);
! 65607: assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
! 65608: if( rc==SQLITE_BUSY ){
! 65609: p->rc = rc = SQLITE_BUSY;
! 65610: }else{
! 65611: assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
! 65612: assert( rc==SQLITE_OK || db->nDeferredCons>0 );
! 65613: rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
! 65614: }
! 65615: goto vdbe_return;
! 65616: }
! 65617:
! 65618: /* Opcode: Integer P1 P2 * * *
! 65619: **
! 65620: ** The 32-bit integer value P1 is written into register P2.
! 65621: */
! 65622: case OP_Integer: { /* out2-prerelease */
! 65623: pOut->u.i = pOp->p1;
! 65624: break;
! 65625: }
! 65626:
! 65627: /* Opcode: Int64 * P2 * P4 *
! 65628: **
! 65629: ** P4 is a pointer to a 64-bit integer value.
! 65630: ** Write that value into register P2.
! 65631: */
! 65632: case OP_Int64: { /* out2-prerelease */
! 65633: assert( pOp->p4.pI64!=0 );
! 65634: pOut->u.i = *pOp->p4.pI64;
! 65635: break;
! 65636: }
! 65637:
! 65638: #ifndef SQLITE_OMIT_FLOATING_POINT
! 65639: /* Opcode: Real * P2 * P4 *
! 65640: **
! 65641: ** P4 is a pointer to a 64-bit floating point value.
! 65642: ** Write that value into register P2.
! 65643: */
! 65644: case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
! 65645: pOut->flags = MEM_Real;
! 65646: assert( !sqlite3IsNaN(*pOp->p4.pReal) );
! 65647: pOut->r = *pOp->p4.pReal;
! 65648: break;
! 65649: }
! 65650: #endif
! 65651:
! 65652: /* Opcode: String8 * P2 * P4 *
! 65653: **
! 65654: ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
! 65655: ** into an OP_String before it is executed for the first time.
! 65656: */
! 65657: case OP_String8: { /* same as TK_STRING, out2-prerelease */
! 65658: assert( pOp->p4.z!=0 );
! 65659: pOp->opcode = OP_String;
! 65660: pOp->p1 = sqlite3Strlen30(pOp->p4.z);
! 65661:
! 65662: #ifndef SQLITE_OMIT_UTF16
! 65663: if( encoding!=SQLITE_UTF8 ){
! 65664: rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
! 65665: if( rc==SQLITE_TOOBIG ) goto too_big;
! 65666: if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
! 65667: assert( pOut->zMalloc==pOut->z );
! 65668: assert( pOut->flags & MEM_Dyn );
! 65669: pOut->zMalloc = 0;
! 65670: pOut->flags |= MEM_Static;
! 65671: pOut->flags &= ~MEM_Dyn;
! 65672: if( pOp->p4type==P4_DYNAMIC ){
! 65673: sqlite3DbFree(db, pOp->p4.z);
! 65674: }
! 65675: pOp->p4type = P4_DYNAMIC;
! 65676: pOp->p4.z = pOut->z;
! 65677: pOp->p1 = pOut->n;
! 65678: }
! 65679: #endif
! 65680: if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
! 65681: goto too_big;
! 65682: }
! 65683: /* Fall through to the next case, OP_String */
! 65684: }
! 65685:
! 65686: /* Opcode: String P1 P2 * P4 *
! 65687: **
! 65688: ** The string value P4 of length P1 (bytes) is stored in register P2.
! 65689: */
! 65690: case OP_String: { /* out2-prerelease */
! 65691: assert( pOp->p4.z!=0 );
! 65692: pOut->flags = MEM_Str|MEM_Static|MEM_Term;
! 65693: pOut->z = pOp->p4.z;
! 65694: pOut->n = pOp->p1;
! 65695: pOut->enc = encoding;
! 65696: UPDATE_MAX_BLOBSIZE(pOut);
! 65697: break;
! 65698: }
! 65699:
! 65700: /* Opcode: Null * P2 P3 * *
! 65701: **
! 65702: ** Write a NULL into registers P2. If P3 greater than P2, then also write
! 65703: ** NULL into register P3 and ever register in between P2 and P3. If P3
! 65704: ** is less than P2 (typically P3 is zero) then only register P2 is
! 65705: ** set to NULL
! 65706: */
! 65707: case OP_Null: { /* out2-prerelease */
! 65708: #if 0 /* local variables moved into u.ab */
! 65709: int cnt;
! 65710: #endif /* local variables moved into u.ab */
! 65711: u.ab.cnt = pOp->p3-pOp->p2;
! 65712: assert( pOp->p3<=p->nMem );
! 65713: pOut->flags = MEM_Null;
! 65714: while( u.ab.cnt>0 ){
! 65715: pOut++;
! 65716: memAboutToChange(p, pOut);
! 65717: VdbeMemRelease(pOut);
! 65718: pOut->flags = MEM_Null;
! 65719: u.ab.cnt--;
! 65720: }
! 65721: break;
! 65722: }
! 65723:
! 65724:
! 65725: /* Opcode: Blob P1 P2 * P4
! 65726: **
! 65727: ** P4 points to a blob of data P1 bytes long. Store this
! 65728: ** blob in register P2.
! 65729: */
! 65730: case OP_Blob: { /* out2-prerelease */
! 65731: assert( pOp->p1 <= SQLITE_MAX_LENGTH );
! 65732: sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
! 65733: pOut->enc = encoding;
! 65734: UPDATE_MAX_BLOBSIZE(pOut);
! 65735: break;
! 65736: }
! 65737:
! 65738: /* Opcode: Variable P1 P2 * P4 *
! 65739: **
! 65740: ** Transfer the values of bound parameter P1 into register P2
! 65741: **
! 65742: ** If the parameter is named, then its name appears in P4 and P3==1.
! 65743: ** The P4 value is used by sqlite3_bind_parameter_name().
! 65744: */
! 65745: case OP_Variable: { /* out2-prerelease */
! 65746: #if 0 /* local variables moved into u.ac */
! 65747: Mem *pVar; /* Value being transferred */
! 65748: #endif /* local variables moved into u.ac */
! 65749:
! 65750: assert( pOp->p1>0 && pOp->p1<=p->nVar );
! 65751: assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
! 65752: u.ac.pVar = &p->aVar[pOp->p1 - 1];
! 65753: if( sqlite3VdbeMemTooBig(u.ac.pVar) ){
! 65754: goto too_big;
! 65755: }
! 65756: sqlite3VdbeMemShallowCopy(pOut, u.ac.pVar, MEM_Static);
! 65757: UPDATE_MAX_BLOBSIZE(pOut);
! 65758: break;
! 65759: }
! 65760:
! 65761: /* Opcode: Move P1 P2 P3 * *
! 65762: **
! 65763: ** Move the values in register P1..P1+P3-1 over into
! 65764: ** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
! 65765: ** left holding a NULL. It is an error for register ranges
! 65766: ** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
! 65767: */
! 65768: case OP_Move: {
! 65769: #if 0 /* local variables moved into u.ad */
! 65770: char *zMalloc; /* Holding variable for allocated memory */
! 65771: int n; /* Number of registers left to copy */
! 65772: int p1; /* Register to copy from */
! 65773: int p2; /* Register to copy to */
! 65774: #endif /* local variables moved into u.ad */
! 65775:
! 65776: u.ad.n = pOp->p3;
! 65777: u.ad.p1 = pOp->p1;
! 65778: u.ad.p2 = pOp->p2;
! 65779: assert( u.ad.n>0 && u.ad.p1>0 && u.ad.p2>0 );
! 65780: assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
! 65781:
! 65782: pIn1 = &aMem[u.ad.p1];
! 65783: pOut = &aMem[u.ad.p2];
! 65784: while( u.ad.n-- ){
! 65785: assert( pOut<=&aMem[p->nMem] );
! 65786: assert( pIn1<=&aMem[p->nMem] );
! 65787: assert( memIsValid(pIn1) );
! 65788: memAboutToChange(p, pOut);
! 65789: u.ad.zMalloc = pOut->zMalloc;
! 65790: pOut->zMalloc = 0;
! 65791: sqlite3VdbeMemMove(pOut, pIn1);
! 65792: #ifdef SQLITE_DEBUG
! 65793: if( pOut->pScopyFrom>=&aMem[u.ad.p1] && pOut->pScopyFrom<&aMem[u.ad.p1+pOp->p3] ){
! 65794: pOut->pScopyFrom += u.ad.p1 - pOp->p2;
! 65795: }
! 65796: #endif
! 65797: pIn1->zMalloc = u.ad.zMalloc;
! 65798: REGISTER_TRACE(u.ad.p2++, pOut);
! 65799: pIn1++;
! 65800: pOut++;
! 65801: }
! 65802: break;
! 65803: }
! 65804:
! 65805: /* Opcode: Copy P1 P2 * * *
! 65806: **
! 65807: ** Make a copy of register P1 into register P2.
! 65808: **
! 65809: ** This instruction makes a deep copy of the value. A duplicate
! 65810: ** is made of any string or blob constant. See also OP_SCopy.
! 65811: */
! 65812: case OP_Copy: { /* in1, out2 */
! 65813: pIn1 = &aMem[pOp->p1];
! 65814: pOut = &aMem[pOp->p2];
! 65815: assert( pOut!=pIn1 );
! 65816: sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
! 65817: Deephemeralize(pOut);
! 65818: REGISTER_TRACE(pOp->p2, pOut);
! 65819: break;
! 65820: }
! 65821:
! 65822: /* Opcode: SCopy P1 P2 * * *
! 65823: **
! 65824: ** Make a shallow copy of register P1 into register P2.
! 65825: **
! 65826: ** This instruction makes a shallow copy of the value. If the value
! 65827: ** is a string or blob, then the copy is only a pointer to the
! 65828: ** original and hence if the original changes so will the copy.
! 65829: ** Worse, if the original is deallocated, the copy becomes invalid.
! 65830: ** Thus the program must guarantee that the original will not change
! 65831: ** during the lifetime of the copy. Use OP_Copy to make a complete
! 65832: ** copy.
! 65833: */
! 65834: case OP_SCopy: { /* in1, out2 */
! 65835: pIn1 = &aMem[pOp->p1];
! 65836: pOut = &aMem[pOp->p2];
! 65837: assert( pOut!=pIn1 );
! 65838: sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
! 65839: #ifdef SQLITE_DEBUG
! 65840: if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
! 65841: #endif
! 65842: REGISTER_TRACE(pOp->p2, pOut);
! 65843: break;
! 65844: }
! 65845:
! 65846: /* Opcode: ResultRow P1 P2 * * *
! 65847: **
! 65848: ** The registers P1 through P1+P2-1 contain a single row of
! 65849: ** results. This opcode causes the sqlite3_step() call to terminate
! 65850: ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
! 65851: ** structure to provide access to the top P1 values as the result
! 65852: ** row.
! 65853: */
! 65854: case OP_ResultRow: {
! 65855: #if 0 /* local variables moved into u.ae */
! 65856: Mem *pMem;
! 65857: int i;
! 65858: #endif /* local variables moved into u.ae */
! 65859: assert( p->nResColumn==pOp->p2 );
! 65860: assert( pOp->p1>0 );
! 65861: assert( pOp->p1+pOp->p2<=p->nMem+1 );
! 65862:
! 65863: /* If this statement has violated immediate foreign key constraints, do
! 65864: ** not return the number of rows modified. And do not RELEASE the statement
! 65865: ** transaction. It needs to be rolled back. */
! 65866: if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
! 65867: assert( db->flags&SQLITE_CountRows );
! 65868: assert( p->usesStmtJournal );
! 65869: break;
! 65870: }
! 65871:
! 65872: /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
! 65873: ** DML statements invoke this opcode to return the number of rows
! 65874: ** modified to the user. This is the only way that a VM that
! 65875: ** opens a statement transaction may invoke this opcode.
! 65876: **
! 65877: ** In case this is such a statement, close any statement transaction
! 65878: ** opened by this VM before returning control to the user. This is to
! 65879: ** ensure that statement-transactions are always nested, not overlapping.
! 65880: ** If the open statement-transaction is not closed here, then the user
! 65881: ** may step another VM that opens its own statement transaction. This
! 65882: ** may lead to overlapping statement transactions.
! 65883: **
! 65884: ** The statement transaction is never a top-level transaction. Hence
! 65885: ** the RELEASE call below can never fail.
! 65886: */
! 65887: assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
! 65888: rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
! 65889: if( NEVER(rc!=SQLITE_OK) ){
! 65890: break;
! 65891: }
! 65892:
! 65893: /* Invalidate all ephemeral cursor row caches */
! 65894: p->cacheCtr = (p->cacheCtr + 2)|1;
! 65895:
! 65896: /* Make sure the results of the current row are \000 terminated
! 65897: ** and have an assigned type. The results are de-ephemeralized as
! 65898: ** a side effect.
! 65899: */
! 65900: u.ae.pMem = p->pResultSet = &aMem[pOp->p1];
! 65901: for(u.ae.i=0; u.ae.i<pOp->p2; u.ae.i++){
! 65902: assert( memIsValid(&u.ae.pMem[u.ae.i]) );
! 65903: Deephemeralize(&u.ae.pMem[u.ae.i]);
! 65904: assert( (u.ae.pMem[u.ae.i].flags & MEM_Ephem)==0
! 65905: || (u.ae.pMem[u.ae.i].flags & (MEM_Str|MEM_Blob))==0 );
! 65906: sqlite3VdbeMemNulTerminate(&u.ae.pMem[u.ae.i]);
! 65907: sqlite3VdbeMemStoreType(&u.ae.pMem[u.ae.i]);
! 65908: REGISTER_TRACE(pOp->p1+u.ae.i, &u.ae.pMem[u.ae.i]);
! 65909: }
! 65910: if( db->mallocFailed ) goto no_mem;
! 65911:
! 65912: /* Return SQLITE_ROW
! 65913: */
! 65914: p->pc = pc + 1;
! 65915: rc = SQLITE_ROW;
! 65916: goto vdbe_return;
! 65917: }
! 65918:
! 65919: /* Opcode: Concat P1 P2 P3 * *
! 65920: **
! 65921: ** Add the text in register P1 onto the end of the text in
! 65922: ** register P2 and store the result in register P3.
! 65923: ** If either the P1 or P2 text are NULL then store NULL in P3.
! 65924: **
! 65925: ** P3 = P2 || P1
! 65926: **
! 65927: ** It is illegal for P1 and P3 to be the same register. Sometimes,
! 65928: ** if P3 is the same register as P2, the implementation is able
! 65929: ** to avoid a memcpy().
! 65930: */
! 65931: case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
! 65932: #if 0 /* local variables moved into u.af */
! 65933: i64 nByte;
! 65934: #endif /* local variables moved into u.af */
! 65935:
! 65936: pIn1 = &aMem[pOp->p1];
! 65937: pIn2 = &aMem[pOp->p2];
! 65938: pOut = &aMem[pOp->p3];
! 65939: assert( pIn1!=pOut );
! 65940: if( (pIn1->flags | pIn2->flags) & MEM_Null ){
! 65941: sqlite3VdbeMemSetNull(pOut);
! 65942: break;
! 65943: }
! 65944: if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
! 65945: Stringify(pIn1, encoding);
! 65946: Stringify(pIn2, encoding);
! 65947: u.af.nByte = pIn1->n + pIn2->n;
! 65948: if( u.af.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
! 65949: goto too_big;
! 65950: }
! 65951: MemSetTypeFlag(pOut, MEM_Str);
! 65952: if( sqlite3VdbeMemGrow(pOut, (int)u.af.nByte+2, pOut==pIn2) ){
! 65953: goto no_mem;
! 65954: }
! 65955: if( pOut!=pIn2 ){
! 65956: memcpy(pOut->z, pIn2->z, pIn2->n);
! 65957: }
! 65958: memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
! 65959: pOut->z[u.af.nByte] = 0;
! 65960: pOut->z[u.af.nByte+1] = 0;
! 65961: pOut->flags |= MEM_Term;
! 65962: pOut->n = (int)u.af.nByte;
! 65963: pOut->enc = encoding;
! 65964: UPDATE_MAX_BLOBSIZE(pOut);
! 65965: break;
! 65966: }
! 65967:
! 65968: /* Opcode: Add P1 P2 P3 * *
! 65969: **
! 65970: ** Add the value in register P1 to the value in register P2
! 65971: ** and store the result in register P3.
! 65972: ** If either input is NULL, the result is NULL.
! 65973: */
! 65974: /* Opcode: Multiply P1 P2 P3 * *
! 65975: **
! 65976: **
! 65977: ** Multiply the value in register P1 by the value in register P2
! 65978: ** and store the result in register P3.
! 65979: ** If either input is NULL, the result is NULL.
! 65980: */
! 65981: /* Opcode: Subtract P1 P2 P3 * *
! 65982: **
! 65983: ** Subtract the value in register P1 from the value in register P2
! 65984: ** and store the result in register P3.
! 65985: ** If either input is NULL, the result is NULL.
! 65986: */
! 65987: /* Opcode: Divide P1 P2 P3 * *
! 65988: **
! 65989: ** Divide the value in register P1 by the value in register P2
! 65990: ** and store the result in register P3 (P3=P2/P1). If the value in
! 65991: ** register P1 is zero, then the result is NULL. If either input is
! 65992: ** NULL, the result is NULL.
! 65993: */
! 65994: /* Opcode: Remainder P1 P2 P3 * *
! 65995: **
! 65996: ** Compute the remainder after integer division of the value in
! 65997: ** register P1 by the value in register P2 and store the result in P3.
! 65998: ** If the value in register P2 is zero the result is NULL.
! 65999: ** If either operand is NULL, the result is NULL.
! 66000: */
! 66001: case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
! 66002: case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
! 66003: case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
! 66004: case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
! 66005: case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
! 66006: #if 0 /* local variables moved into u.ag */
! 66007: int flags; /* Combined MEM_* flags from both inputs */
! 66008: i64 iA; /* Integer value of left operand */
! 66009: i64 iB; /* Integer value of right operand */
! 66010: double rA; /* Real value of left operand */
! 66011: double rB; /* Real value of right operand */
! 66012: #endif /* local variables moved into u.ag */
! 66013:
! 66014: pIn1 = &aMem[pOp->p1];
! 66015: applyNumericAffinity(pIn1);
! 66016: pIn2 = &aMem[pOp->p2];
! 66017: applyNumericAffinity(pIn2);
! 66018: pOut = &aMem[pOp->p3];
! 66019: u.ag.flags = pIn1->flags | pIn2->flags;
! 66020: if( (u.ag.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
! 66021: if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
! 66022: u.ag.iA = pIn1->u.i;
! 66023: u.ag.iB = pIn2->u.i;
! 66024: switch( pOp->opcode ){
! 66025: case OP_Add: if( sqlite3AddInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
! 66026: case OP_Subtract: if( sqlite3SubInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
! 66027: case OP_Multiply: if( sqlite3MulInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
! 66028: case OP_Divide: {
! 66029: if( u.ag.iA==0 ) goto arithmetic_result_is_null;
! 66030: if( u.ag.iA==-1 && u.ag.iB==SMALLEST_INT64 ) goto fp_math;
! 66031: u.ag.iB /= u.ag.iA;
! 66032: break;
! 66033: }
! 66034: default: {
! 66035: if( u.ag.iA==0 ) goto arithmetic_result_is_null;
! 66036: if( u.ag.iA==-1 ) u.ag.iA = 1;
! 66037: u.ag.iB %= u.ag.iA;
! 66038: break;
! 66039: }
! 66040: }
! 66041: pOut->u.i = u.ag.iB;
! 66042: MemSetTypeFlag(pOut, MEM_Int);
! 66043: }else{
! 66044: fp_math:
! 66045: u.ag.rA = sqlite3VdbeRealValue(pIn1);
! 66046: u.ag.rB = sqlite3VdbeRealValue(pIn2);
! 66047: switch( pOp->opcode ){
! 66048: case OP_Add: u.ag.rB += u.ag.rA; break;
! 66049: case OP_Subtract: u.ag.rB -= u.ag.rA; break;
! 66050: case OP_Multiply: u.ag.rB *= u.ag.rA; break;
! 66051: case OP_Divide: {
! 66052: /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
! 66053: if( u.ag.rA==(double)0 ) goto arithmetic_result_is_null;
! 66054: u.ag.rB /= u.ag.rA;
! 66055: break;
! 66056: }
! 66057: default: {
! 66058: u.ag.iA = (i64)u.ag.rA;
! 66059: u.ag.iB = (i64)u.ag.rB;
! 66060: if( u.ag.iA==0 ) goto arithmetic_result_is_null;
! 66061: if( u.ag.iA==-1 ) u.ag.iA = 1;
! 66062: u.ag.rB = (double)(u.ag.iB % u.ag.iA);
! 66063: break;
! 66064: }
! 66065: }
! 66066: #ifdef SQLITE_OMIT_FLOATING_POINT
! 66067: pOut->u.i = u.ag.rB;
! 66068: MemSetTypeFlag(pOut, MEM_Int);
! 66069: #else
! 66070: if( sqlite3IsNaN(u.ag.rB) ){
! 66071: goto arithmetic_result_is_null;
! 66072: }
! 66073: pOut->r = u.ag.rB;
! 66074: MemSetTypeFlag(pOut, MEM_Real);
! 66075: if( (u.ag.flags & MEM_Real)==0 ){
! 66076: sqlite3VdbeIntegerAffinity(pOut);
! 66077: }
! 66078: #endif
! 66079: }
! 66080: break;
! 66081:
! 66082: arithmetic_result_is_null:
! 66083: sqlite3VdbeMemSetNull(pOut);
! 66084: break;
! 66085: }
! 66086:
! 66087: /* Opcode: CollSeq * * P4
! 66088: **
! 66089: ** P4 is a pointer to a CollSeq struct. If the next call to a user function
! 66090: ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
! 66091: ** be returned. This is used by the built-in min(), max() and nullif()
! 66092: ** functions.
! 66093: **
! 66094: ** The interface used by the implementation of the aforementioned functions
! 66095: ** to retrieve the collation sequence set by this opcode is not available
! 66096: ** publicly, only to user functions defined in func.c.
! 66097: */
! 66098: case OP_CollSeq: {
! 66099: assert( pOp->p4type==P4_COLLSEQ );
! 66100: break;
! 66101: }
! 66102:
! 66103: /* Opcode: Function P1 P2 P3 P4 P5
! 66104: **
! 66105: ** Invoke a user function (P4 is a pointer to a Function structure that
! 66106: ** defines the function) with P5 arguments taken from register P2 and
! 66107: ** successors. The result of the function is stored in register P3.
! 66108: ** Register P3 must not be one of the function inputs.
! 66109: **
! 66110: ** P1 is a 32-bit bitmask indicating whether or not each argument to the
! 66111: ** function was determined to be constant at compile time. If the first
! 66112: ** argument was constant then bit 0 of P1 is set. This is used to determine
! 66113: ** whether meta data associated with a user function argument using the
! 66114: ** sqlite3_set_auxdata() API may be safely retained until the next
! 66115: ** invocation of this opcode.
! 66116: **
! 66117: ** See also: AggStep and AggFinal
! 66118: */
! 66119: case OP_Function: {
! 66120: #if 0 /* local variables moved into u.ah */
! 66121: int i;
! 66122: Mem *pArg;
! 66123: sqlite3_context ctx;
! 66124: sqlite3_value **apVal;
! 66125: int n;
! 66126: #endif /* local variables moved into u.ah */
! 66127:
! 66128: u.ah.n = pOp->p5;
! 66129: u.ah.apVal = p->apArg;
! 66130: assert( u.ah.apVal || u.ah.n==0 );
! 66131: assert( pOp->p3>0 && pOp->p3<=p->nMem );
! 66132: pOut = &aMem[pOp->p3];
! 66133: memAboutToChange(p, pOut);
! 66134:
! 66135: assert( u.ah.n==0 || (pOp->p2>0 && pOp->p2+u.ah.n<=p->nMem+1) );
! 66136: assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ah.n );
! 66137: u.ah.pArg = &aMem[pOp->p2];
! 66138: for(u.ah.i=0; u.ah.i<u.ah.n; u.ah.i++, u.ah.pArg++){
! 66139: assert( memIsValid(u.ah.pArg) );
! 66140: u.ah.apVal[u.ah.i] = u.ah.pArg;
! 66141: Deephemeralize(u.ah.pArg);
! 66142: sqlite3VdbeMemStoreType(u.ah.pArg);
! 66143: REGISTER_TRACE(pOp->p2+u.ah.i, u.ah.pArg);
! 66144: }
! 66145:
! 66146: assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
! 66147: if( pOp->p4type==P4_FUNCDEF ){
! 66148: u.ah.ctx.pFunc = pOp->p4.pFunc;
! 66149: u.ah.ctx.pVdbeFunc = 0;
! 66150: }else{
! 66151: u.ah.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
! 66152: u.ah.ctx.pFunc = u.ah.ctx.pVdbeFunc->pFunc;
! 66153: }
! 66154:
! 66155: u.ah.ctx.s.flags = MEM_Null;
! 66156: u.ah.ctx.s.db = db;
! 66157: u.ah.ctx.s.xDel = 0;
! 66158: u.ah.ctx.s.zMalloc = 0;
! 66159:
! 66160: /* The output cell may already have a buffer allocated. Move
! 66161: ** the pointer to u.ah.ctx.s so in case the user-function can use
! 66162: ** the already allocated buffer instead of allocating a new one.
! 66163: */
! 66164: sqlite3VdbeMemMove(&u.ah.ctx.s, pOut);
! 66165: MemSetTypeFlag(&u.ah.ctx.s, MEM_Null);
! 66166:
! 66167: u.ah.ctx.isError = 0;
! 66168: if( u.ah.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
! 66169: assert( pOp>aOp );
! 66170: assert( pOp[-1].p4type==P4_COLLSEQ );
! 66171: assert( pOp[-1].opcode==OP_CollSeq );
! 66172: u.ah.ctx.pColl = pOp[-1].p4.pColl;
! 66173: }
! 66174: db->lastRowid = lastRowid;
! 66175: (*u.ah.ctx.pFunc->xFunc)(&u.ah.ctx, u.ah.n, u.ah.apVal); /* IMP: R-24505-23230 */
! 66176: lastRowid = db->lastRowid;
! 66177:
! 66178: /* If any auxiliary data functions have been called by this user function,
! 66179: ** immediately call the destructor for any non-static values.
! 66180: */
! 66181: if( u.ah.ctx.pVdbeFunc ){
! 66182: sqlite3VdbeDeleteAuxData(u.ah.ctx.pVdbeFunc, pOp->p1);
! 66183: pOp->p4.pVdbeFunc = u.ah.ctx.pVdbeFunc;
! 66184: pOp->p4type = P4_VDBEFUNC;
! 66185: }
! 66186:
! 66187: if( db->mallocFailed ){
! 66188: /* Even though a malloc() has failed, the implementation of the
! 66189: ** user function may have called an sqlite3_result_XXX() function
! 66190: ** to return a value. The following call releases any resources
! 66191: ** associated with such a value.
! 66192: */
! 66193: sqlite3VdbeMemRelease(&u.ah.ctx.s);
! 66194: goto no_mem;
! 66195: }
! 66196:
! 66197: /* If the function returned an error, throw an exception */
! 66198: if( u.ah.ctx.isError ){
! 66199: sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ah.ctx.s));
! 66200: rc = u.ah.ctx.isError;
! 66201: }
! 66202:
! 66203: /* Copy the result of the function into register P3 */
! 66204: sqlite3VdbeChangeEncoding(&u.ah.ctx.s, encoding);
! 66205: sqlite3VdbeMemMove(pOut, &u.ah.ctx.s);
! 66206: if( sqlite3VdbeMemTooBig(pOut) ){
! 66207: goto too_big;
! 66208: }
! 66209:
! 66210: #if 0
! 66211: /* The app-defined function has done something that as caused this
! 66212: ** statement to expire. (Perhaps the function called sqlite3_exec()
! 66213: ** with a CREATE TABLE statement.)
! 66214: */
! 66215: if( p->expired ) rc = SQLITE_ABORT;
! 66216: #endif
! 66217:
! 66218: REGISTER_TRACE(pOp->p3, pOut);
! 66219: UPDATE_MAX_BLOBSIZE(pOut);
! 66220: break;
! 66221: }
! 66222:
! 66223: /* Opcode: BitAnd P1 P2 P3 * *
! 66224: **
! 66225: ** Take the bit-wise AND of the values in register P1 and P2 and
! 66226: ** store the result in register P3.
! 66227: ** If either input is NULL, the result is NULL.
! 66228: */
! 66229: /* Opcode: BitOr P1 P2 P3 * *
! 66230: **
! 66231: ** Take the bit-wise OR of the values in register P1 and P2 and
! 66232: ** store the result in register P3.
! 66233: ** If either input is NULL, the result is NULL.
! 66234: */
! 66235: /* Opcode: ShiftLeft P1 P2 P3 * *
! 66236: **
! 66237: ** Shift the integer value in register P2 to the left by the
! 66238: ** number of bits specified by the integer in register P1.
! 66239: ** Store the result in register P3.
! 66240: ** If either input is NULL, the result is NULL.
! 66241: */
! 66242: /* Opcode: ShiftRight P1 P2 P3 * *
! 66243: **
! 66244: ** Shift the integer value in register P2 to the right by the
! 66245: ** number of bits specified by the integer in register P1.
! 66246: ** Store the result in register P3.
! 66247: ** If either input is NULL, the result is NULL.
! 66248: */
! 66249: case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
! 66250: case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
! 66251: case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
! 66252: case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
! 66253: #if 0 /* local variables moved into u.ai */
! 66254: i64 iA;
! 66255: u64 uA;
! 66256: i64 iB;
! 66257: u8 op;
! 66258: #endif /* local variables moved into u.ai */
! 66259:
! 66260: pIn1 = &aMem[pOp->p1];
! 66261: pIn2 = &aMem[pOp->p2];
! 66262: pOut = &aMem[pOp->p3];
! 66263: if( (pIn1->flags | pIn2->flags) & MEM_Null ){
! 66264: sqlite3VdbeMemSetNull(pOut);
! 66265: break;
! 66266: }
! 66267: u.ai.iA = sqlite3VdbeIntValue(pIn2);
! 66268: u.ai.iB = sqlite3VdbeIntValue(pIn1);
! 66269: u.ai.op = pOp->opcode;
! 66270: if( u.ai.op==OP_BitAnd ){
! 66271: u.ai.iA &= u.ai.iB;
! 66272: }else if( u.ai.op==OP_BitOr ){
! 66273: u.ai.iA |= u.ai.iB;
! 66274: }else if( u.ai.iB!=0 ){
! 66275: assert( u.ai.op==OP_ShiftRight || u.ai.op==OP_ShiftLeft );
! 66276:
! 66277: /* If shifting by a negative amount, shift in the other direction */
! 66278: if( u.ai.iB<0 ){
! 66279: assert( OP_ShiftRight==OP_ShiftLeft+1 );
! 66280: u.ai.op = 2*OP_ShiftLeft + 1 - u.ai.op;
! 66281: u.ai.iB = u.ai.iB>(-64) ? -u.ai.iB : 64;
! 66282: }
! 66283:
! 66284: if( u.ai.iB>=64 ){
! 66285: u.ai.iA = (u.ai.iA>=0 || u.ai.op==OP_ShiftLeft) ? 0 : -1;
! 66286: }else{
! 66287: memcpy(&u.ai.uA, &u.ai.iA, sizeof(u.ai.uA));
! 66288: if( u.ai.op==OP_ShiftLeft ){
! 66289: u.ai.uA <<= u.ai.iB;
! 66290: }else{
! 66291: u.ai.uA >>= u.ai.iB;
! 66292: /* Sign-extend on a right shift of a negative number */
! 66293: if( u.ai.iA<0 ) u.ai.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ai.iB);
! 66294: }
! 66295: memcpy(&u.ai.iA, &u.ai.uA, sizeof(u.ai.iA));
! 66296: }
! 66297: }
! 66298: pOut->u.i = u.ai.iA;
! 66299: MemSetTypeFlag(pOut, MEM_Int);
! 66300: break;
! 66301: }
! 66302:
! 66303: /* Opcode: AddImm P1 P2 * * *
! 66304: **
! 66305: ** Add the constant P2 to the value in register P1.
! 66306: ** The result is always an integer.
! 66307: **
! 66308: ** To force any register to be an integer, just add 0.
! 66309: */
! 66310: case OP_AddImm: { /* in1 */
! 66311: pIn1 = &aMem[pOp->p1];
! 66312: memAboutToChange(p, pIn1);
! 66313: sqlite3VdbeMemIntegerify(pIn1);
! 66314: pIn1->u.i += pOp->p2;
! 66315: break;
! 66316: }
! 66317:
! 66318: /* Opcode: MustBeInt P1 P2 * * *
! 66319: **
! 66320: ** Force the value in register P1 to be an integer. If the value
! 66321: ** in P1 is not an integer and cannot be converted into an integer
! 66322: ** without data loss, then jump immediately to P2, or if P2==0
! 66323: ** raise an SQLITE_MISMATCH exception.
! 66324: */
! 66325: case OP_MustBeInt: { /* jump, in1 */
! 66326: pIn1 = &aMem[pOp->p1];
! 66327: applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
! 66328: if( (pIn1->flags & MEM_Int)==0 ){
! 66329: if( pOp->p2==0 ){
! 66330: rc = SQLITE_MISMATCH;
! 66331: goto abort_due_to_error;
! 66332: }else{
! 66333: pc = pOp->p2 - 1;
! 66334: }
! 66335: }else{
! 66336: MemSetTypeFlag(pIn1, MEM_Int);
! 66337: }
! 66338: break;
! 66339: }
! 66340:
! 66341: #ifndef SQLITE_OMIT_FLOATING_POINT
! 66342: /* Opcode: RealAffinity P1 * * * *
! 66343: **
! 66344: ** If register P1 holds an integer convert it to a real value.
! 66345: **
! 66346: ** This opcode is used when extracting information from a column that
! 66347: ** has REAL affinity. Such column values may still be stored as
! 66348: ** integers, for space efficiency, but after extraction we want them
! 66349: ** to have only a real value.
! 66350: */
! 66351: case OP_RealAffinity: { /* in1 */
! 66352: pIn1 = &aMem[pOp->p1];
! 66353: if( pIn1->flags & MEM_Int ){
! 66354: sqlite3VdbeMemRealify(pIn1);
! 66355: }
! 66356: break;
! 66357: }
! 66358: #endif
! 66359:
! 66360: #ifndef SQLITE_OMIT_CAST
! 66361: /* Opcode: ToText P1 * * * *
! 66362: **
! 66363: ** Force the value in register P1 to be text.
! 66364: ** If the value is numeric, convert it to a string using the
! 66365: ** equivalent of printf(). Blob values are unchanged and
! 66366: ** are afterwards simply interpreted as text.
! 66367: **
! 66368: ** A NULL value is not changed by this routine. It remains NULL.
! 66369: */
! 66370: case OP_ToText: { /* same as TK_TO_TEXT, in1 */
! 66371: pIn1 = &aMem[pOp->p1];
! 66372: memAboutToChange(p, pIn1);
! 66373: if( pIn1->flags & MEM_Null ) break;
! 66374: assert( MEM_Str==(MEM_Blob>>3) );
! 66375: pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
! 66376: applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
! 66377: rc = ExpandBlob(pIn1);
! 66378: assert( pIn1->flags & MEM_Str || db->mallocFailed );
! 66379: pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
! 66380: UPDATE_MAX_BLOBSIZE(pIn1);
! 66381: break;
! 66382: }
! 66383:
! 66384: /* Opcode: ToBlob P1 * * * *
! 66385: **
! 66386: ** Force the value in register P1 to be a BLOB.
! 66387: ** If the value is numeric, convert it to a string first.
! 66388: ** Strings are simply reinterpreted as blobs with no change
! 66389: ** to the underlying data.
! 66390: **
! 66391: ** A NULL value is not changed by this routine. It remains NULL.
! 66392: */
! 66393: case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
! 66394: pIn1 = &aMem[pOp->p1];
! 66395: if( pIn1->flags & MEM_Null ) break;
! 66396: if( (pIn1->flags & MEM_Blob)==0 ){
! 66397: applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
! 66398: assert( pIn1->flags & MEM_Str || db->mallocFailed );
! 66399: MemSetTypeFlag(pIn1, MEM_Blob);
! 66400: }else{
! 66401: pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
! 66402: }
! 66403: UPDATE_MAX_BLOBSIZE(pIn1);
! 66404: break;
! 66405: }
! 66406:
! 66407: /* Opcode: ToNumeric P1 * * * *
! 66408: **
! 66409: ** Force the value in register P1 to be numeric (either an
! 66410: ** integer or a floating-point number.)
! 66411: ** If the value is text or blob, try to convert it to an using the
! 66412: ** equivalent of atoi() or atof() and store 0 if no such conversion
! 66413: ** is possible.
! 66414: **
! 66415: ** A NULL value is not changed by this routine. It remains NULL.
! 66416: */
! 66417: case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
! 66418: pIn1 = &aMem[pOp->p1];
! 66419: sqlite3VdbeMemNumerify(pIn1);
! 66420: break;
! 66421: }
! 66422: #endif /* SQLITE_OMIT_CAST */
! 66423:
! 66424: /* Opcode: ToInt P1 * * * *
! 66425: **
! 66426: ** Force the value in register P1 to be an integer. If
! 66427: ** The value is currently a real number, drop its fractional part.
! 66428: ** If the value is text or blob, try to convert it to an integer using the
! 66429: ** equivalent of atoi() and store 0 if no such conversion is possible.
! 66430: **
! 66431: ** A NULL value is not changed by this routine. It remains NULL.
! 66432: */
! 66433: case OP_ToInt: { /* same as TK_TO_INT, in1 */
! 66434: pIn1 = &aMem[pOp->p1];
! 66435: if( (pIn1->flags & MEM_Null)==0 ){
! 66436: sqlite3VdbeMemIntegerify(pIn1);
! 66437: }
! 66438: break;
! 66439: }
! 66440:
! 66441: #if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
! 66442: /* Opcode: ToReal P1 * * * *
! 66443: **
! 66444: ** Force the value in register P1 to be a floating point number.
! 66445: ** If The value is currently an integer, convert it.
! 66446: ** If the value is text or blob, try to convert it to an integer using the
! 66447: ** equivalent of atoi() and store 0.0 if no such conversion is possible.
! 66448: **
! 66449: ** A NULL value is not changed by this routine. It remains NULL.
! 66450: */
! 66451: case OP_ToReal: { /* same as TK_TO_REAL, in1 */
! 66452: pIn1 = &aMem[pOp->p1];
! 66453: memAboutToChange(p, pIn1);
! 66454: if( (pIn1->flags & MEM_Null)==0 ){
! 66455: sqlite3VdbeMemRealify(pIn1);
! 66456: }
! 66457: break;
! 66458: }
! 66459: #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
! 66460:
! 66461: /* Opcode: Lt P1 P2 P3 P4 P5
! 66462: **
! 66463: ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
! 66464: ** jump to address P2.
! 66465: **
! 66466: ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
! 66467: ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
! 66468: ** bit is clear then fall through if either operand is NULL.
! 66469: **
! 66470: ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
! 66471: ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
! 66472: ** to coerce both inputs according to this affinity before the
! 66473: ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
! 66474: ** affinity is used. Note that the affinity conversions are stored
! 66475: ** back into the input registers P1 and P3. So this opcode can cause
! 66476: ** persistent changes to registers P1 and P3.
! 66477: **
! 66478: ** Once any conversions have taken place, and neither value is NULL,
! 66479: ** the values are compared. If both values are blobs then memcmp() is
! 66480: ** used to determine the results of the comparison. If both values
! 66481: ** are text, then the appropriate collating function specified in
! 66482: ** P4 is used to do the comparison. If P4 is not specified then
! 66483: ** memcmp() is used to compare text string. If both values are
! 66484: ** numeric, then a numeric comparison is used. If the two values
! 66485: ** are of different types, then numbers are considered less than
! 66486: ** strings and strings are considered less than blobs.
! 66487: **
! 66488: ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
! 66489: ** store a boolean result (either 0, or 1, or NULL) in register P2.
! 66490: */
! 66491: /* Opcode: Ne P1 P2 P3 P4 P5
! 66492: **
! 66493: ** This works just like the Lt opcode except that the jump is taken if
! 66494: ** the operands in registers P1 and P3 are not equal. See the Lt opcode for
! 66495: ** additional information.
! 66496: **
! 66497: ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
! 66498: ** true or false and is never NULL. If both operands are NULL then the result
! 66499: ** of comparison is false. If either operand is NULL then the result is true.
! 66500: ** If neither operand is NULL the result is the same as it would be if
! 66501: ** the SQLITE_NULLEQ flag were omitted from P5.
! 66502: */
! 66503: /* Opcode: Eq P1 P2 P3 P4 P5
! 66504: **
! 66505: ** This works just like the Lt opcode except that the jump is taken if
! 66506: ** the operands in registers P1 and P3 are equal.
! 66507: ** See the Lt opcode for additional information.
! 66508: **
! 66509: ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
! 66510: ** true or false and is never NULL. If both operands are NULL then the result
! 66511: ** of comparison is true. If either operand is NULL then the result is false.
! 66512: ** If neither operand is NULL the result is the same as it would be if
! 66513: ** the SQLITE_NULLEQ flag were omitted from P5.
! 66514: */
! 66515: /* Opcode: Le P1 P2 P3 P4 P5
! 66516: **
! 66517: ** This works just like the Lt opcode except that the jump is taken if
! 66518: ** the content of register P3 is less than or equal to the content of
! 66519: ** register P1. See the Lt opcode for additional information.
! 66520: */
! 66521: /* Opcode: Gt P1 P2 P3 P4 P5
! 66522: **
! 66523: ** This works just like the Lt opcode except that the jump is taken if
! 66524: ** the content of register P3 is greater than the content of
! 66525: ** register P1. See the Lt opcode for additional information.
! 66526: */
! 66527: /* Opcode: Ge P1 P2 P3 P4 P5
! 66528: **
! 66529: ** This works just like the Lt opcode except that the jump is taken if
! 66530: ** the content of register P3 is greater than or equal to the content of
! 66531: ** register P1. See the Lt opcode for additional information.
! 66532: */
! 66533: case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
! 66534: case OP_Ne: /* same as TK_NE, jump, in1, in3 */
! 66535: case OP_Lt: /* same as TK_LT, jump, in1, in3 */
! 66536: case OP_Le: /* same as TK_LE, jump, in1, in3 */
! 66537: case OP_Gt: /* same as TK_GT, jump, in1, in3 */
! 66538: case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
! 66539: #if 0 /* local variables moved into u.aj */
! 66540: int res; /* Result of the comparison of pIn1 against pIn3 */
! 66541: char affinity; /* Affinity to use for comparison */
! 66542: u16 flags1; /* Copy of initial value of pIn1->flags */
! 66543: u16 flags3; /* Copy of initial value of pIn3->flags */
! 66544: #endif /* local variables moved into u.aj */
! 66545:
! 66546: pIn1 = &aMem[pOp->p1];
! 66547: pIn3 = &aMem[pOp->p3];
! 66548: u.aj.flags1 = pIn1->flags;
! 66549: u.aj.flags3 = pIn3->flags;
! 66550: if( (u.aj.flags1 | u.aj.flags3)&MEM_Null ){
! 66551: /* One or both operands are NULL */
! 66552: if( pOp->p5 & SQLITE_NULLEQ ){
! 66553: /* If SQLITE_NULLEQ is set (which will only happen if the operator is
! 66554: ** OP_Eq or OP_Ne) then take the jump or not depending on whether
! 66555: ** or not both operands are null.
! 66556: */
! 66557: assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
! 66558: u.aj.res = (u.aj.flags1 & u.aj.flags3 & MEM_Null)==0;
! 66559: }else{
! 66560: /* SQLITE_NULLEQ is clear and at least one operand is NULL,
! 66561: ** then the result is always NULL.
! 66562: ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
! 66563: */
! 66564: if( pOp->p5 & SQLITE_STOREP2 ){
! 66565: pOut = &aMem[pOp->p2];
! 66566: MemSetTypeFlag(pOut, MEM_Null);
! 66567: REGISTER_TRACE(pOp->p2, pOut);
! 66568: }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
! 66569: pc = pOp->p2-1;
! 66570: }
! 66571: break;
! 66572: }
! 66573: }else{
! 66574: /* Neither operand is NULL. Do a comparison. */
! 66575: u.aj.affinity = pOp->p5 & SQLITE_AFF_MASK;
! 66576: if( u.aj.affinity ){
! 66577: applyAffinity(pIn1, u.aj.affinity, encoding);
! 66578: applyAffinity(pIn3, u.aj.affinity, encoding);
! 66579: if( db->mallocFailed ) goto no_mem;
! 66580: }
! 66581:
! 66582: assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
! 66583: ExpandBlob(pIn1);
! 66584: ExpandBlob(pIn3);
! 66585: u.aj.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
! 66586: }
! 66587: switch( pOp->opcode ){
! 66588: case OP_Eq: u.aj.res = u.aj.res==0; break;
! 66589: case OP_Ne: u.aj.res = u.aj.res!=0; break;
! 66590: case OP_Lt: u.aj.res = u.aj.res<0; break;
! 66591: case OP_Le: u.aj.res = u.aj.res<=0; break;
! 66592: case OP_Gt: u.aj.res = u.aj.res>0; break;
! 66593: default: u.aj.res = u.aj.res>=0; break;
! 66594: }
! 66595:
! 66596: if( pOp->p5 & SQLITE_STOREP2 ){
! 66597: pOut = &aMem[pOp->p2];
! 66598: memAboutToChange(p, pOut);
! 66599: MemSetTypeFlag(pOut, MEM_Int);
! 66600: pOut->u.i = u.aj.res;
! 66601: REGISTER_TRACE(pOp->p2, pOut);
! 66602: }else if( u.aj.res ){
! 66603: pc = pOp->p2-1;
! 66604: }
! 66605:
! 66606: /* Undo any changes made by applyAffinity() to the input registers. */
! 66607: pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.aj.flags1&MEM_TypeMask);
! 66608: pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.aj.flags3&MEM_TypeMask);
! 66609: break;
! 66610: }
! 66611:
! 66612: /* Opcode: Permutation * * * P4 *
! 66613: **
! 66614: ** Set the permutation used by the OP_Compare operator to be the array
! 66615: ** of integers in P4.
! 66616: **
! 66617: ** The permutation is only valid until the next OP_Permutation, OP_Compare,
! 66618: ** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur
! 66619: ** immediately prior to the OP_Compare.
! 66620: */
! 66621: case OP_Permutation: {
! 66622: assert( pOp->p4type==P4_INTARRAY );
! 66623: assert( pOp->p4.ai );
! 66624: aPermute = pOp->p4.ai;
! 66625: break;
! 66626: }
! 66627:
! 66628: /* Opcode: Compare P1 P2 P3 P4 *
! 66629: **
! 66630: ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
! 66631: ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
! 66632: ** the comparison for use by the next OP_Jump instruct.
! 66633: **
! 66634: ** P4 is a KeyInfo structure that defines collating sequences and sort
! 66635: ** orders for the comparison. The permutation applies to registers
! 66636: ** only. The KeyInfo elements are used sequentially.
! 66637: **
! 66638: ** The comparison is a sort comparison, so NULLs compare equal,
! 66639: ** NULLs are less than numbers, numbers are less than strings,
! 66640: ** and strings are less than blobs.
! 66641: */
! 66642: case OP_Compare: {
! 66643: #if 0 /* local variables moved into u.ak */
! 66644: int n;
! 66645: int i;
! 66646: int p1;
! 66647: int p2;
! 66648: const KeyInfo *pKeyInfo;
! 66649: int idx;
! 66650: CollSeq *pColl; /* Collating sequence to use on this term */
! 66651: int bRev; /* True for DESCENDING sort order */
! 66652: #endif /* local variables moved into u.ak */
! 66653:
! 66654: u.ak.n = pOp->p3;
! 66655: u.ak.pKeyInfo = pOp->p4.pKeyInfo;
! 66656: assert( u.ak.n>0 );
! 66657: assert( u.ak.pKeyInfo!=0 );
! 66658: u.ak.p1 = pOp->p1;
! 66659: u.ak.p2 = pOp->p2;
! 66660: #if SQLITE_DEBUG
! 66661: if( aPermute ){
! 66662: int k, mx = 0;
! 66663: for(k=0; k<u.ak.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
! 66664: assert( u.ak.p1>0 && u.ak.p1+mx<=p->nMem+1 );
! 66665: assert( u.ak.p2>0 && u.ak.p2+mx<=p->nMem+1 );
! 66666: }else{
! 66667: assert( u.ak.p1>0 && u.ak.p1+u.ak.n<=p->nMem+1 );
! 66668: assert( u.ak.p2>0 && u.ak.p2+u.ak.n<=p->nMem+1 );
! 66669: }
! 66670: #endif /* SQLITE_DEBUG */
! 66671: for(u.ak.i=0; u.ak.i<u.ak.n; u.ak.i++){
! 66672: u.ak.idx = aPermute ? aPermute[u.ak.i] : u.ak.i;
! 66673: assert( memIsValid(&aMem[u.ak.p1+u.ak.idx]) );
! 66674: assert( memIsValid(&aMem[u.ak.p2+u.ak.idx]) );
! 66675: REGISTER_TRACE(u.ak.p1+u.ak.idx, &aMem[u.ak.p1+u.ak.idx]);
! 66676: REGISTER_TRACE(u.ak.p2+u.ak.idx, &aMem[u.ak.p2+u.ak.idx]);
! 66677: assert( u.ak.i<u.ak.pKeyInfo->nField );
! 66678: u.ak.pColl = u.ak.pKeyInfo->aColl[u.ak.i];
! 66679: u.ak.bRev = u.ak.pKeyInfo->aSortOrder[u.ak.i];
! 66680: iCompare = sqlite3MemCompare(&aMem[u.ak.p1+u.ak.idx], &aMem[u.ak.p2+u.ak.idx], u.ak.pColl);
! 66681: if( iCompare ){
! 66682: if( u.ak.bRev ) iCompare = -iCompare;
! 66683: break;
! 66684: }
! 66685: }
! 66686: aPermute = 0;
! 66687: break;
! 66688: }
! 66689:
! 66690: /* Opcode: Jump P1 P2 P3 * *
! 66691: **
! 66692: ** Jump to the instruction at address P1, P2, or P3 depending on whether
! 66693: ** in the most recent OP_Compare instruction the P1 vector was less than
! 66694: ** equal to, or greater than the P2 vector, respectively.
! 66695: */
! 66696: case OP_Jump: { /* jump */
! 66697: if( iCompare<0 ){
! 66698: pc = pOp->p1 - 1;
! 66699: }else if( iCompare==0 ){
! 66700: pc = pOp->p2 - 1;
! 66701: }else{
! 66702: pc = pOp->p3 - 1;
! 66703: }
! 66704: break;
! 66705: }
! 66706:
! 66707: /* Opcode: And P1 P2 P3 * *
! 66708: **
! 66709: ** Take the logical AND of the values in registers P1 and P2 and
! 66710: ** write the result into register P3.
! 66711: **
! 66712: ** If either P1 or P2 is 0 (false) then the result is 0 even if
! 66713: ** the other input is NULL. A NULL and true or two NULLs give
! 66714: ** a NULL output.
! 66715: */
! 66716: /* Opcode: Or P1 P2 P3 * *
! 66717: **
! 66718: ** Take the logical OR of the values in register P1 and P2 and
! 66719: ** store the answer in register P3.
! 66720: **
! 66721: ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
! 66722: ** even if the other input is NULL. A NULL and false or two NULLs
! 66723: ** give a NULL output.
! 66724: */
! 66725: case OP_And: /* same as TK_AND, in1, in2, out3 */
! 66726: case OP_Or: { /* same as TK_OR, in1, in2, out3 */
! 66727: #if 0 /* local variables moved into u.al */
! 66728: int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
! 66729: int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
! 66730: #endif /* local variables moved into u.al */
! 66731:
! 66732: pIn1 = &aMem[pOp->p1];
! 66733: if( pIn1->flags & MEM_Null ){
! 66734: u.al.v1 = 2;
! 66735: }else{
! 66736: u.al.v1 = sqlite3VdbeIntValue(pIn1)!=0;
! 66737: }
! 66738: pIn2 = &aMem[pOp->p2];
! 66739: if( pIn2->flags & MEM_Null ){
! 66740: u.al.v2 = 2;
! 66741: }else{
! 66742: u.al.v2 = sqlite3VdbeIntValue(pIn2)!=0;
! 66743: }
! 66744: if( pOp->opcode==OP_And ){
! 66745: static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
! 66746: u.al.v1 = and_logic[u.al.v1*3+u.al.v2];
! 66747: }else{
! 66748: static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
! 66749: u.al.v1 = or_logic[u.al.v1*3+u.al.v2];
! 66750: }
! 66751: pOut = &aMem[pOp->p3];
! 66752: if( u.al.v1==2 ){
! 66753: MemSetTypeFlag(pOut, MEM_Null);
! 66754: }else{
! 66755: pOut->u.i = u.al.v1;
! 66756: MemSetTypeFlag(pOut, MEM_Int);
! 66757: }
! 66758: break;
! 66759: }
! 66760:
! 66761: /* Opcode: Not P1 P2 * * *
! 66762: **
! 66763: ** Interpret the value in register P1 as a boolean value. Store the
! 66764: ** boolean complement in register P2. If the value in register P1 is
! 66765: ** NULL, then a NULL is stored in P2.
! 66766: */
! 66767: case OP_Not: { /* same as TK_NOT, in1, out2 */
! 66768: pIn1 = &aMem[pOp->p1];
! 66769: pOut = &aMem[pOp->p2];
! 66770: if( pIn1->flags & MEM_Null ){
! 66771: sqlite3VdbeMemSetNull(pOut);
! 66772: }else{
! 66773: sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
! 66774: }
! 66775: break;
! 66776: }
! 66777:
! 66778: /* Opcode: BitNot P1 P2 * * *
! 66779: **
! 66780: ** Interpret the content of register P1 as an integer. Store the
! 66781: ** ones-complement of the P1 value into register P2. If P1 holds
! 66782: ** a NULL then store a NULL in P2.
! 66783: */
! 66784: case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
! 66785: pIn1 = &aMem[pOp->p1];
! 66786: pOut = &aMem[pOp->p2];
! 66787: if( pIn1->flags & MEM_Null ){
! 66788: sqlite3VdbeMemSetNull(pOut);
! 66789: }else{
! 66790: sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
! 66791: }
! 66792: break;
! 66793: }
! 66794:
! 66795: /* Opcode: Once P1 P2 * * *
! 66796: **
! 66797: ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
! 66798: ** set the flag and fall through to the next instruction.
! 66799: **
! 66800: ** See also: JumpOnce
! 66801: */
! 66802: case OP_Once: { /* jump */
! 66803: assert( pOp->p1<p->nOnceFlag );
! 66804: if( p->aOnceFlag[pOp->p1] ){
! 66805: pc = pOp->p2-1;
! 66806: }else{
! 66807: p->aOnceFlag[pOp->p1] = 1;
! 66808: }
! 66809: break;
! 66810: }
! 66811:
! 66812: /* Opcode: If P1 P2 P3 * *
! 66813: **
! 66814: ** Jump to P2 if the value in register P1 is true. The value
! 66815: ** is considered true if it is numeric and non-zero. If the value
! 66816: ** in P1 is NULL then take the jump if P3 is non-zero.
! 66817: */
! 66818: /* Opcode: IfNot P1 P2 P3 * *
! 66819: **
! 66820: ** Jump to P2 if the value in register P1 is False. The value
! 66821: ** is considered false if it has a numeric value of zero. If the value
! 66822: ** in P1 is NULL then take the jump if P3 is zero.
! 66823: */
! 66824: case OP_If: /* jump, in1 */
! 66825: case OP_IfNot: { /* jump, in1 */
! 66826: #if 0 /* local variables moved into u.am */
! 66827: int c;
! 66828: #endif /* local variables moved into u.am */
! 66829: pIn1 = &aMem[pOp->p1];
! 66830: if( pIn1->flags & MEM_Null ){
! 66831: u.am.c = pOp->p3;
! 66832: }else{
! 66833: #ifdef SQLITE_OMIT_FLOATING_POINT
! 66834: u.am.c = sqlite3VdbeIntValue(pIn1)!=0;
! 66835: #else
! 66836: u.am.c = sqlite3VdbeRealValue(pIn1)!=0.0;
! 66837: #endif
! 66838: if( pOp->opcode==OP_IfNot ) u.am.c = !u.am.c;
! 66839: }
! 66840: if( u.am.c ){
! 66841: pc = pOp->p2-1;
! 66842: }
! 66843: break;
! 66844: }
! 66845:
! 66846: /* Opcode: IsNull P1 P2 * * *
! 66847: **
! 66848: ** Jump to P2 if the value in register P1 is NULL.
! 66849: */
! 66850: case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
! 66851: pIn1 = &aMem[pOp->p1];
! 66852: if( (pIn1->flags & MEM_Null)!=0 ){
! 66853: pc = pOp->p2 - 1;
! 66854: }
! 66855: break;
! 66856: }
! 66857:
! 66858: /* Opcode: NotNull P1 P2 * * *
! 66859: **
! 66860: ** Jump to P2 if the value in register P1 is not NULL.
! 66861: */
! 66862: case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
! 66863: pIn1 = &aMem[pOp->p1];
! 66864: if( (pIn1->flags & MEM_Null)==0 ){
! 66865: pc = pOp->p2 - 1;
! 66866: }
! 66867: break;
! 66868: }
! 66869:
! 66870: /* Opcode: Column P1 P2 P3 P4 P5
! 66871: **
! 66872: ** Interpret the data that cursor P1 points to as a structure built using
! 66873: ** the MakeRecord instruction. (See the MakeRecord opcode for additional
! 66874: ** information about the format of the data.) Extract the P2-th column
! 66875: ** from this record. If there are less that (P2+1)
! 66876: ** values in the record, extract a NULL.
! 66877: **
! 66878: ** The value extracted is stored in register P3.
! 66879: **
! 66880: ** If the column contains fewer than P2 fields, then extract a NULL. Or,
! 66881: ** if the P4 argument is a P4_MEM use the value of the P4 argument as
! 66882: ** the result.
! 66883: **
! 66884: ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
! 66885: ** then the cache of the cursor is reset prior to extracting the column.
! 66886: ** The first OP_Column against a pseudo-table after the value of the content
! 66887: ** register has changed should have this bit set.
! 66888: */
! 66889: case OP_Column: {
! 66890: #if 0 /* local variables moved into u.an */
! 66891: u32 payloadSize; /* Number of bytes in the record */
! 66892: i64 payloadSize64; /* Number of bytes in the record */
! 66893: int p1; /* P1 value of the opcode */
! 66894: int p2; /* column number to retrieve */
! 66895: VdbeCursor *pC; /* The VDBE cursor */
! 66896: char *zRec; /* Pointer to complete record-data */
! 66897: BtCursor *pCrsr; /* The BTree cursor */
! 66898: u32 *aType; /* aType[i] holds the numeric type of the i-th column */
! 66899: u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
! 66900: int nField; /* number of fields in the record */
! 66901: int len; /* The length of the serialized data for the column */
! 66902: int i; /* Loop counter */
! 66903: char *zData; /* Part of the record being decoded */
! 66904: Mem *pDest; /* Where to write the extracted value */
! 66905: Mem sMem; /* For storing the record being decoded */
! 66906: u8 *zIdx; /* Index into header */
! 66907: u8 *zEndHdr; /* Pointer to first byte after the header */
! 66908: u32 offset; /* Offset into the data */
! 66909: u32 szField; /* Number of bytes in the content of a field */
! 66910: int szHdr; /* Size of the header size field at start of record */
! 66911: int avail; /* Number of bytes of available data */
! 66912: u32 t; /* A type code from the record header */
! 66913: Mem *pReg; /* PseudoTable input register */
! 66914: #endif /* local variables moved into u.an */
! 66915:
! 66916:
! 66917: u.an.p1 = pOp->p1;
! 66918: u.an.p2 = pOp->p2;
! 66919: u.an.pC = 0;
! 66920: memset(&u.an.sMem, 0, sizeof(u.an.sMem));
! 66921: assert( u.an.p1<p->nCursor );
! 66922: assert( pOp->p3>0 && pOp->p3<=p->nMem );
! 66923: u.an.pDest = &aMem[pOp->p3];
! 66924: memAboutToChange(p, u.an.pDest);
! 66925: u.an.zRec = 0;
! 66926:
! 66927: /* This block sets the variable u.an.payloadSize to be the total number of
! 66928: ** bytes in the record.
! 66929: **
! 66930: ** u.an.zRec is set to be the complete text of the record if it is available.
! 66931: ** The complete record text is always available for pseudo-tables
! 66932: ** If the record is stored in a cursor, the complete record text
! 66933: ** might be available in the u.an.pC->aRow cache. Or it might not be.
! 66934: ** If the data is unavailable, u.an.zRec is set to NULL.
! 66935: **
! 66936: ** We also compute the number of columns in the record. For cursors,
! 66937: ** the number of columns is stored in the VdbeCursor.nField element.
! 66938: */
! 66939: u.an.pC = p->apCsr[u.an.p1];
! 66940: assert( u.an.pC!=0 );
! 66941: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 66942: assert( u.an.pC->pVtabCursor==0 );
! 66943: #endif
! 66944: u.an.pCrsr = u.an.pC->pCursor;
! 66945: if( u.an.pCrsr!=0 ){
! 66946: /* The record is stored in a B-Tree */
! 66947: rc = sqlite3VdbeCursorMoveto(u.an.pC);
! 66948: if( rc ) goto abort_due_to_error;
! 66949: if( u.an.pC->nullRow ){
! 66950: u.an.payloadSize = 0;
! 66951: }else if( u.an.pC->cacheStatus==p->cacheCtr ){
! 66952: u.an.payloadSize = u.an.pC->payloadSize;
! 66953: u.an.zRec = (char*)u.an.pC->aRow;
! 66954: }else if( u.an.pC->isIndex ){
! 66955: assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
! 66956: VVA_ONLY(rc =) sqlite3BtreeKeySize(u.an.pCrsr, &u.an.payloadSize64);
! 66957: assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
! 66958: /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
! 66959: ** payload size, so it is impossible for u.an.payloadSize64 to be
! 66960: ** larger than 32 bits. */
! 66961: assert( (u.an.payloadSize64 & SQLITE_MAX_U32)==(u64)u.an.payloadSize64 );
! 66962: u.an.payloadSize = (u32)u.an.payloadSize64;
! 66963: }else{
! 66964: assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
! 66965: VVA_ONLY(rc =) sqlite3BtreeDataSize(u.an.pCrsr, &u.an.payloadSize);
! 66966: assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
! 66967: }
! 66968: }else if( ALWAYS(u.an.pC->pseudoTableReg>0) ){
! 66969: u.an.pReg = &aMem[u.an.pC->pseudoTableReg];
! 66970: assert( u.an.pReg->flags & MEM_Blob );
! 66971: assert( memIsValid(u.an.pReg) );
! 66972: u.an.payloadSize = u.an.pReg->n;
! 66973: u.an.zRec = u.an.pReg->z;
! 66974: u.an.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
! 66975: assert( u.an.payloadSize==0 || u.an.zRec!=0 );
! 66976: }else{
! 66977: /* Consider the row to be NULL */
! 66978: u.an.payloadSize = 0;
! 66979: }
! 66980:
! 66981: /* If u.an.payloadSize is 0, then just store a NULL. This can happen because of
! 66982: ** nullRow or because of a corrupt database. */
! 66983: if( u.an.payloadSize==0 ){
! 66984: MemSetTypeFlag(u.an.pDest, MEM_Null);
! 66985: goto op_column_out;
! 66986: }
! 66987: assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
! 66988: if( u.an.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
! 66989: goto too_big;
! 66990: }
! 66991:
! 66992: u.an.nField = u.an.pC->nField;
! 66993: assert( u.an.p2<u.an.nField );
! 66994:
! 66995: /* Read and parse the table header. Store the results of the parse
! 66996: ** into the record header cache fields of the cursor.
! 66997: */
! 66998: u.an.aType = u.an.pC->aType;
! 66999: if( u.an.pC->cacheStatus==p->cacheCtr ){
! 67000: u.an.aOffset = u.an.pC->aOffset;
! 67001: }else{
! 67002: assert(u.an.aType);
! 67003: u.an.avail = 0;
! 67004: u.an.pC->aOffset = u.an.aOffset = &u.an.aType[u.an.nField];
! 67005: u.an.pC->payloadSize = u.an.payloadSize;
! 67006: u.an.pC->cacheStatus = p->cacheCtr;
! 67007:
! 67008: /* Figure out how many bytes are in the header */
! 67009: if( u.an.zRec ){
! 67010: u.an.zData = u.an.zRec;
! 67011: }else{
! 67012: if( u.an.pC->isIndex ){
! 67013: u.an.zData = (char*)sqlite3BtreeKeyFetch(u.an.pCrsr, &u.an.avail);
! 67014: }else{
! 67015: u.an.zData = (char*)sqlite3BtreeDataFetch(u.an.pCrsr, &u.an.avail);
! 67016: }
! 67017: /* If KeyFetch()/DataFetch() managed to get the entire payload,
! 67018: ** save the payload in the u.an.pC->aRow cache. That will save us from
! 67019: ** having to make additional calls to fetch the content portion of
! 67020: ** the record.
! 67021: */
! 67022: assert( u.an.avail>=0 );
! 67023: if( u.an.payloadSize <= (u32)u.an.avail ){
! 67024: u.an.zRec = u.an.zData;
! 67025: u.an.pC->aRow = (u8*)u.an.zData;
! 67026: }else{
! 67027: u.an.pC->aRow = 0;
! 67028: }
! 67029: }
! 67030: /* The following assert is true in all cases accept when
! 67031: ** the database file has been corrupted externally.
! 67032: ** assert( u.an.zRec!=0 || u.an.avail>=u.an.payloadSize || u.an.avail>=9 ); */
! 67033: u.an.szHdr = getVarint32((u8*)u.an.zData, u.an.offset);
! 67034:
! 67035: /* Make sure a corrupt database has not given us an oversize header.
! 67036: ** Do this now to avoid an oversize memory allocation.
! 67037: **
! 67038: ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
! 67039: ** types use so much data space that there can only be 4096 and 32 of
! 67040: ** them, respectively. So the maximum header length results from a
! 67041: ** 3-byte type for each of the maximum of 32768 columns plus three
! 67042: ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
! 67043: */
! 67044: if( u.an.offset > 98307 ){
! 67045: rc = SQLITE_CORRUPT_BKPT;
! 67046: goto op_column_out;
! 67047: }
! 67048:
! 67049: /* Compute in u.an.len the number of bytes of data we need to read in order
! 67050: ** to get u.an.nField type values. u.an.offset is an upper bound on this. But
! 67051: ** u.an.nField might be significantly less than the true number of columns
! 67052: ** in the table, and in that case, 5*u.an.nField+3 might be smaller than u.an.offset.
! 67053: ** We want to minimize u.an.len in order to limit the size of the memory
! 67054: ** allocation, especially if a corrupt database file has caused u.an.offset
! 67055: ** to be oversized. Offset is limited to 98307 above. But 98307 might
! 67056: ** still exceed Robson memory allocation limits on some configurations.
! 67057: ** On systems that cannot tolerate large memory allocations, u.an.nField*5+3
! 67058: ** will likely be much smaller since u.an.nField will likely be less than
! 67059: ** 20 or so. This insures that Robson memory allocation limits are
! 67060: ** not exceeded even for corrupt database files.
! 67061: */
! 67062: u.an.len = u.an.nField*5 + 3;
! 67063: if( u.an.len > (int)u.an.offset ) u.an.len = (int)u.an.offset;
! 67064:
! 67065: /* The KeyFetch() or DataFetch() above are fast and will get the entire
! 67066: ** record header in most cases. But they will fail to get the complete
! 67067: ** record header if the record header does not fit on a single page
! 67068: ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
! 67069: ** acquire the complete header text.
! 67070: */
! 67071: if( !u.an.zRec && u.an.avail<u.an.len ){
! 67072: u.an.sMem.flags = 0;
! 67073: u.an.sMem.db = 0;
! 67074: rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, 0, u.an.len, u.an.pC->isIndex, &u.an.sMem);
! 67075: if( rc!=SQLITE_OK ){
! 67076: goto op_column_out;
! 67077: }
! 67078: u.an.zData = u.an.sMem.z;
! 67079: }
! 67080: u.an.zEndHdr = (u8 *)&u.an.zData[u.an.len];
! 67081: u.an.zIdx = (u8 *)&u.an.zData[u.an.szHdr];
! 67082:
! 67083: /* Scan the header and use it to fill in the u.an.aType[] and u.an.aOffset[]
! 67084: ** arrays. u.an.aType[u.an.i] will contain the type integer for the u.an.i-th
! 67085: ** column and u.an.aOffset[u.an.i] will contain the u.an.offset from the beginning
! 67086: ** of the record to the start of the data for the u.an.i-th column
! 67087: */
! 67088: for(u.an.i=0; u.an.i<u.an.nField; u.an.i++){
! 67089: if( u.an.zIdx<u.an.zEndHdr ){
! 67090: u.an.aOffset[u.an.i] = u.an.offset;
! 67091: if( u.an.zIdx[0]<0x80 ){
! 67092: u.an.t = u.an.zIdx[0];
! 67093: u.an.zIdx++;
! 67094: }else{
! 67095: u.an.zIdx += sqlite3GetVarint32(u.an.zIdx, &u.an.t);
! 67096: }
! 67097: u.an.aType[u.an.i] = u.an.t;
! 67098: u.an.szField = sqlite3VdbeSerialTypeLen(u.an.t);
! 67099: u.an.offset += u.an.szField;
! 67100: if( u.an.offset<u.an.szField ){ /* True if u.an.offset overflows */
! 67101: u.an.zIdx = &u.an.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
! 67102: break;
! 67103: }
! 67104: }else{
! 67105: /* If u.an.i is less that u.an.nField, then there are less fields in this
! 67106: ** record than SetNumColumns indicated there are columns in the
! 67107: ** table. Set the u.an.offset for any extra columns not present in
! 67108: ** the record to 0. This tells code below to store a NULL
! 67109: ** instead of deserializing a value from the record.
! 67110: */
! 67111: u.an.aOffset[u.an.i] = 0;
! 67112: }
! 67113: }
! 67114: sqlite3VdbeMemRelease(&u.an.sMem);
! 67115: u.an.sMem.flags = MEM_Null;
! 67116:
! 67117: /* If we have read more header data than was contained in the header,
! 67118: ** or if the end of the last field appears to be past the end of the
! 67119: ** record, or if the end of the last field appears to be before the end
! 67120: ** of the record (when all fields present), then we must be dealing
! 67121: ** with a corrupt database.
! 67122: */
! 67123: if( (u.an.zIdx > u.an.zEndHdr) || (u.an.offset > u.an.payloadSize)
! 67124: || (u.an.zIdx==u.an.zEndHdr && u.an.offset!=u.an.payloadSize) ){
! 67125: rc = SQLITE_CORRUPT_BKPT;
! 67126: goto op_column_out;
! 67127: }
! 67128: }
! 67129:
! 67130: /* Get the column information. If u.an.aOffset[u.an.p2] is non-zero, then
! 67131: ** deserialize the value from the record. If u.an.aOffset[u.an.p2] is zero,
! 67132: ** then there are not enough fields in the record to satisfy the
! 67133: ** request. In this case, set the value NULL or to P4 if P4 is
! 67134: ** a pointer to a Mem object.
! 67135: */
! 67136: if( u.an.aOffset[u.an.p2] ){
! 67137: assert( rc==SQLITE_OK );
! 67138: if( u.an.zRec ){
! 67139: VdbeMemRelease(u.an.pDest);
! 67140: sqlite3VdbeSerialGet((u8 *)&u.an.zRec[u.an.aOffset[u.an.p2]], u.an.aType[u.an.p2], u.an.pDest);
! 67141: }else{
! 67142: u.an.len = sqlite3VdbeSerialTypeLen(u.an.aType[u.an.p2]);
! 67143: sqlite3VdbeMemMove(&u.an.sMem, u.an.pDest);
! 67144: rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, u.an.aOffset[u.an.p2], u.an.len, u.an.pC->isIndex, &u.an.sMem);
! 67145: if( rc!=SQLITE_OK ){
! 67146: goto op_column_out;
! 67147: }
! 67148: u.an.zData = u.an.sMem.z;
! 67149: sqlite3VdbeSerialGet((u8*)u.an.zData, u.an.aType[u.an.p2], u.an.pDest);
! 67150: }
! 67151: u.an.pDest->enc = encoding;
! 67152: }else{
! 67153: if( pOp->p4type==P4_MEM ){
! 67154: sqlite3VdbeMemShallowCopy(u.an.pDest, pOp->p4.pMem, MEM_Static);
! 67155: }else{
! 67156: MemSetTypeFlag(u.an.pDest, MEM_Null);
! 67157: }
! 67158: }
! 67159:
! 67160: /* If we dynamically allocated space to hold the data (in the
! 67161: ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
! 67162: ** dynamically allocated space over to the u.an.pDest structure.
! 67163: ** This prevents a memory copy.
! 67164: */
! 67165: if( u.an.sMem.zMalloc ){
! 67166: assert( u.an.sMem.z==u.an.sMem.zMalloc );
! 67167: assert( !(u.an.pDest->flags & MEM_Dyn) );
! 67168: assert( !(u.an.pDest->flags & (MEM_Blob|MEM_Str)) || u.an.pDest->z==u.an.sMem.z );
! 67169: u.an.pDest->flags &= ~(MEM_Ephem|MEM_Static);
! 67170: u.an.pDest->flags |= MEM_Term;
! 67171: u.an.pDest->z = u.an.sMem.z;
! 67172: u.an.pDest->zMalloc = u.an.sMem.zMalloc;
! 67173: }
! 67174:
! 67175: rc = sqlite3VdbeMemMakeWriteable(u.an.pDest);
! 67176:
! 67177: op_column_out:
! 67178: UPDATE_MAX_BLOBSIZE(u.an.pDest);
! 67179: REGISTER_TRACE(pOp->p3, u.an.pDest);
! 67180: break;
! 67181: }
! 67182:
! 67183: /* Opcode: Affinity P1 P2 * P4 *
! 67184: **
! 67185: ** Apply affinities to a range of P2 registers starting with P1.
! 67186: **
! 67187: ** P4 is a string that is P2 characters long. The nth character of the
! 67188: ** string indicates the column affinity that should be used for the nth
! 67189: ** memory cell in the range.
! 67190: */
! 67191: case OP_Affinity: {
! 67192: #if 0 /* local variables moved into u.ao */
! 67193: const char *zAffinity; /* The affinity to be applied */
! 67194: char cAff; /* A single character of affinity */
! 67195: #endif /* local variables moved into u.ao */
! 67196:
! 67197: u.ao.zAffinity = pOp->p4.z;
! 67198: assert( u.ao.zAffinity!=0 );
! 67199: assert( u.ao.zAffinity[pOp->p2]==0 );
! 67200: pIn1 = &aMem[pOp->p1];
! 67201: while( (u.ao.cAff = *(u.ao.zAffinity++))!=0 ){
! 67202: assert( pIn1 <= &p->aMem[p->nMem] );
! 67203: assert( memIsValid(pIn1) );
! 67204: ExpandBlob(pIn1);
! 67205: applyAffinity(pIn1, u.ao.cAff, encoding);
! 67206: pIn1++;
! 67207: }
! 67208: break;
! 67209: }
! 67210:
! 67211: /* Opcode: MakeRecord P1 P2 P3 P4 *
! 67212: **
! 67213: ** Convert P2 registers beginning with P1 into the [record format]
! 67214: ** use as a data record in a database table or as a key
! 67215: ** in an index. The OP_Column opcode can decode the record later.
! 67216: **
! 67217: ** P4 may be a string that is P2 characters long. The nth character of the
! 67218: ** string indicates the column affinity that should be used for the nth
! 67219: ** field of the index key.
! 67220: **
! 67221: ** The mapping from character to affinity is given by the SQLITE_AFF_
! 67222: ** macros defined in sqliteInt.h.
! 67223: **
! 67224: ** If P4 is NULL then all index fields have the affinity NONE.
! 67225: */
! 67226: case OP_MakeRecord: {
! 67227: #if 0 /* local variables moved into u.ap */
! 67228: u8 *zNewRecord; /* A buffer to hold the data for the new record */
! 67229: Mem *pRec; /* The new record */
! 67230: u64 nData; /* Number of bytes of data space */
! 67231: int nHdr; /* Number of bytes of header space */
! 67232: i64 nByte; /* Data space required for this record */
! 67233: int nZero; /* Number of zero bytes at the end of the record */
! 67234: int nVarint; /* Number of bytes in a varint */
! 67235: u32 serial_type; /* Type field */
! 67236: Mem *pData0; /* First field to be combined into the record */
! 67237: Mem *pLast; /* Last field of the record */
! 67238: int nField; /* Number of fields in the record */
! 67239: char *zAffinity; /* The affinity string for the record */
! 67240: int file_format; /* File format to use for encoding */
! 67241: int i; /* Space used in zNewRecord[] */
! 67242: int len; /* Length of a field */
! 67243: #endif /* local variables moved into u.ap */
! 67244:
! 67245: /* Assuming the record contains N fields, the record format looks
! 67246: ** like this:
! 67247: **
! 67248: ** ------------------------------------------------------------------------
! 67249: ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
! 67250: ** ------------------------------------------------------------------------
! 67251: **
! 67252: ** Data(0) is taken from register P1. Data(1) comes from register P1+1
! 67253: ** and so froth.
! 67254: **
! 67255: ** Each type field is a varint representing the serial type of the
! 67256: ** corresponding data element (see sqlite3VdbeSerialType()). The
! 67257: ** hdr-size field is also a varint which is the offset from the beginning
! 67258: ** of the record to data0.
! 67259: */
! 67260: u.ap.nData = 0; /* Number of bytes of data space */
! 67261: u.ap.nHdr = 0; /* Number of bytes of header space */
! 67262: u.ap.nZero = 0; /* Number of zero bytes at the end of the record */
! 67263: u.ap.nField = pOp->p1;
! 67264: u.ap.zAffinity = pOp->p4.z;
! 67265: assert( u.ap.nField>0 && pOp->p2>0 && pOp->p2+u.ap.nField<=p->nMem+1 );
! 67266: u.ap.pData0 = &aMem[u.ap.nField];
! 67267: u.ap.nField = pOp->p2;
! 67268: u.ap.pLast = &u.ap.pData0[u.ap.nField-1];
! 67269: u.ap.file_format = p->minWriteFileFormat;
! 67270:
! 67271: /* Identify the output register */
! 67272: assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
! 67273: pOut = &aMem[pOp->p3];
! 67274: memAboutToChange(p, pOut);
! 67275:
! 67276: /* Loop through the elements that will make up the record to figure
! 67277: ** out how much space is required for the new record.
! 67278: */
! 67279: for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
! 67280: assert( memIsValid(u.ap.pRec) );
! 67281: if( u.ap.zAffinity ){
! 67282: applyAffinity(u.ap.pRec, u.ap.zAffinity[u.ap.pRec-u.ap.pData0], encoding);
! 67283: }
! 67284: if( u.ap.pRec->flags&MEM_Zero && u.ap.pRec->n>0 ){
! 67285: sqlite3VdbeMemExpandBlob(u.ap.pRec);
! 67286: }
! 67287: u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
! 67288: u.ap.len = sqlite3VdbeSerialTypeLen(u.ap.serial_type);
! 67289: u.ap.nData += u.ap.len;
! 67290: u.ap.nHdr += sqlite3VarintLen(u.ap.serial_type);
! 67291: if( u.ap.pRec->flags & MEM_Zero ){
! 67292: /* Only pure zero-filled BLOBs can be input to this Opcode.
! 67293: ** We do not allow blobs with a prefix and a zero-filled tail. */
! 67294: u.ap.nZero += u.ap.pRec->u.nZero;
! 67295: }else if( u.ap.len ){
! 67296: u.ap.nZero = 0;
! 67297: }
! 67298: }
! 67299:
! 67300: /* Add the initial header varint and total the size */
! 67301: u.ap.nHdr += u.ap.nVarint = sqlite3VarintLen(u.ap.nHdr);
! 67302: if( u.ap.nVarint<sqlite3VarintLen(u.ap.nHdr) ){
! 67303: u.ap.nHdr++;
! 67304: }
! 67305: u.ap.nByte = u.ap.nHdr+u.ap.nData-u.ap.nZero;
! 67306: if( u.ap.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
! 67307: goto too_big;
! 67308: }
! 67309:
! 67310: /* Make sure the output register has a buffer large enough to store
! 67311: ** the new record. The output register (pOp->p3) is not allowed to
! 67312: ** be one of the input registers (because the following call to
! 67313: ** sqlite3VdbeMemGrow() could clobber the value before it is used).
! 67314: */
! 67315: if( sqlite3VdbeMemGrow(pOut, (int)u.ap.nByte, 0) ){
! 67316: goto no_mem;
! 67317: }
! 67318: u.ap.zNewRecord = (u8 *)pOut->z;
! 67319:
! 67320: /* Write the record */
! 67321: u.ap.i = putVarint32(u.ap.zNewRecord, u.ap.nHdr);
! 67322: for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
! 67323: u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
! 67324: u.ap.i += putVarint32(&u.ap.zNewRecord[u.ap.i], u.ap.serial_type); /* serial type */
! 67325: }
! 67326: for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){ /* serial data */
! 67327: u.ap.i += sqlite3VdbeSerialPut(&u.ap.zNewRecord[u.ap.i], (int)(u.ap.nByte-u.ap.i), u.ap.pRec,u.ap.file_format);
! 67328: }
! 67329: assert( u.ap.i==u.ap.nByte );
! 67330:
! 67331: assert( pOp->p3>0 && pOp->p3<=p->nMem );
! 67332: pOut->n = (int)u.ap.nByte;
! 67333: pOut->flags = MEM_Blob | MEM_Dyn;
! 67334: pOut->xDel = 0;
! 67335: if( u.ap.nZero ){
! 67336: pOut->u.nZero = u.ap.nZero;
! 67337: pOut->flags |= MEM_Zero;
! 67338: }
! 67339: pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
! 67340: REGISTER_TRACE(pOp->p3, pOut);
! 67341: UPDATE_MAX_BLOBSIZE(pOut);
! 67342: break;
! 67343: }
! 67344:
! 67345: /* Opcode: Count P1 P2 * * *
! 67346: **
! 67347: ** Store the number of entries (an integer value) in the table or index
! 67348: ** opened by cursor P1 in register P2
! 67349: */
! 67350: #ifndef SQLITE_OMIT_BTREECOUNT
! 67351: case OP_Count: { /* out2-prerelease */
! 67352: #if 0 /* local variables moved into u.aq */
! 67353: i64 nEntry;
! 67354: BtCursor *pCrsr;
! 67355: #endif /* local variables moved into u.aq */
! 67356:
! 67357: u.aq.pCrsr = p->apCsr[pOp->p1]->pCursor;
! 67358: if( ALWAYS(u.aq.pCrsr) ){
! 67359: rc = sqlite3BtreeCount(u.aq.pCrsr, &u.aq.nEntry);
! 67360: }else{
! 67361: u.aq.nEntry = 0;
! 67362: }
! 67363: pOut->u.i = u.aq.nEntry;
! 67364: break;
! 67365: }
! 67366: #endif
! 67367:
! 67368: /* Opcode: Savepoint P1 * * P4 *
! 67369: **
! 67370: ** Open, release or rollback the savepoint named by parameter P4, depending
! 67371: ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
! 67372: ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
! 67373: */
! 67374: case OP_Savepoint: {
! 67375: #if 0 /* local variables moved into u.ar */
! 67376: int p1; /* Value of P1 operand */
! 67377: char *zName; /* Name of savepoint */
! 67378: int nName;
! 67379: Savepoint *pNew;
! 67380: Savepoint *pSavepoint;
! 67381: Savepoint *pTmp;
! 67382: int iSavepoint;
! 67383: int ii;
! 67384: #endif /* local variables moved into u.ar */
! 67385:
! 67386: u.ar.p1 = pOp->p1;
! 67387: u.ar.zName = pOp->p4.z;
! 67388:
! 67389: /* Assert that the u.ar.p1 parameter is valid. Also that if there is no open
! 67390: ** transaction, then there cannot be any savepoints.
! 67391: */
! 67392: assert( db->pSavepoint==0 || db->autoCommit==0 );
! 67393: assert( u.ar.p1==SAVEPOINT_BEGIN||u.ar.p1==SAVEPOINT_RELEASE||u.ar.p1==SAVEPOINT_ROLLBACK );
! 67394: assert( db->pSavepoint || db->isTransactionSavepoint==0 );
! 67395: assert( checkSavepointCount(db) );
! 67396:
! 67397: if( u.ar.p1==SAVEPOINT_BEGIN ){
! 67398: if( db->writeVdbeCnt>0 ){
! 67399: /* A new savepoint cannot be created if there are active write
! 67400: ** statements (i.e. open read/write incremental blob handles).
! 67401: */
! 67402: sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
! 67403: "SQL statements in progress");
! 67404: rc = SQLITE_BUSY;
! 67405: }else{
! 67406: u.ar.nName = sqlite3Strlen30(u.ar.zName);
! 67407:
! 67408: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 67409: /* This call is Ok even if this savepoint is actually a transaction
! 67410: ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
! 67411: ** If this is a transaction savepoint being opened, it is guaranteed
! 67412: ** that the db->aVTrans[] array is empty. */
! 67413: assert( db->autoCommit==0 || db->nVTrans==0 );
! 67414: rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
! 67415: db->nStatement+db->nSavepoint);
! 67416: if( rc!=SQLITE_OK ) goto abort_due_to_error;
! 67417: #endif
! 67418:
! 67419: /* Create a new savepoint structure. */
! 67420: u.ar.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.ar.nName+1);
! 67421: if( u.ar.pNew ){
! 67422: u.ar.pNew->zName = (char *)&u.ar.pNew[1];
! 67423: memcpy(u.ar.pNew->zName, u.ar.zName, u.ar.nName+1);
! 67424:
! 67425: /* If there is no open transaction, then mark this as a special
! 67426: ** "transaction savepoint". */
! 67427: if( db->autoCommit ){
! 67428: db->autoCommit = 0;
! 67429: db->isTransactionSavepoint = 1;
! 67430: }else{
! 67431: db->nSavepoint++;
! 67432: }
! 67433:
! 67434: /* Link the new savepoint into the database handle's list. */
! 67435: u.ar.pNew->pNext = db->pSavepoint;
! 67436: db->pSavepoint = u.ar.pNew;
! 67437: u.ar.pNew->nDeferredCons = db->nDeferredCons;
! 67438: }
! 67439: }
! 67440: }else{
! 67441: u.ar.iSavepoint = 0;
! 67442:
! 67443: /* Find the named savepoint. If there is no such savepoint, then an
! 67444: ** an error is returned to the user. */
! 67445: for(
! 67446: u.ar.pSavepoint = db->pSavepoint;
! 67447: u.ar.pSavepoint && sqlite3StrICmp(u.ar.pSavepoint->zName, u.ar.zName);
! 67448: u.ar.pSavepoint = u.ar.pSavepoint->pNext
! 67449: ){
! 67450: u.ar.iSavepoint++;
! 67451: }
! 67452: if( !u.ar.pSavepoint ){
! 67453: sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.ar.zName);
! 67454: rc = SQLITE_ERROR;
! 67455: }else if(
! 67456: db->writeVdbeCnt>0 || (u.ar.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
! 67457: ){
! 67458: /* It is not possible to release (commit) a savepoint if there are
! 67459: ** active write statements. It is not possible to rollback a savepoint
! 67460: ** if there are any active statements at all.
! 67461: */
! 67462: sqlite3SetString(&p->zErrMsg, db,
! 67463: "cannot %s savepoint - SQL statements in progress",
! 67464: (u.ar.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
! 67465: );
! 67466: rc = SQLITE_BUSY;
! 67467: }else{
! 67468:
! 67469: /* Determine whether or not this is a transaction savepoint. If so,
! 67470: ** and this is a RELEASE command, then the current transaction
! 67471: ** is committed.
! 67472: */
! 67473: int isTransaction = u.ar.pSavepoint->pNext==0 && db->isTransactionSavepoint;
! 67474: if( isTransaction && u.ar.p1==SAVEPOINT_RELEASE ){
! 67475: if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
! 67476: goto vdbe_return;
! 67477: }
! 67478: db->autoCommit = 1;
! 67479: if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
! 67480: p->pc = pc;
! 67481: db->autoCommit = 0;
! 67482: p->rc = rc = SQLITE_BUSY;
! 67483: goto vdbe_return;
! 67484: }
! 67485: db->isTransactionSavepoint = 0;
! 67486: rc = p->rc;
! 67487: }else{
! 67488: u.ar.iSavepoint = db->nSavepoint - u.ar.iSavepoint - 1;
! 67489: for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
! 67490: rc = sqlite3BtreeSavepoint(db->aDb[u.ar.ii].pBt, u.ar.p1, u.ar.iSavepoint);
! 67491: if( rc!=SQLITE_OK ){
! 67492: goto abort_due_to_error;
! 67493: }
! 67494: }
! 67495: if( u.ar.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
! 67496: sqlite3ExpirePreparedStatements(db);
! 67497: sqlite3ResetInternalSchema(db, -1);
! 67498: db->flags = (db->flags | SQLITE_InternChanges);
! 67499: }
! 67500: }
! 67501:
! 67502: /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
! 67503: ** savepoints nested inside of the savepoint being operated on. */
! 67504: while( db->pSavepoint!=u.ar.pSavepoint ){
! 67505: u.ar.pTmp = db->pSavepoint;
! 67506: db->pSavepoint = u.ar.pTmp->pNext;
! 67507: sqlite3DbFree(db, u.ar.pTmp);
! 67508: db->nSavepoint--;
! 67509: }
! 67510:
! 67511: /* If it is a RELEASE, then destroy the savepoint being operated on
! 67512: ** too. If it is a ROLLBACK TO, then set the number of deferred
! 67513: ** constraint violations present in the database to the value stored
! 67514: ** when the savepoint was created. */
! 67515: if( u.ar.p1==SAVEPOINT_RELEASE ){
! 67516: assert( u.ar.pSavepoint==db->pSavepoint );
! 67517: db->pSavepoint = u.ar.pSavepoint->pNext;
! 67518: sqlite3DbFree(db, u.ar.pSavepoint);
! 67519: if( !isTransaction ){
! 67520: db->nSavepoint--;
! 67521: }
! 67522: }else{
! 67523: db->nDeferredCons = u.ar.pSavepoint->nDeferredCons;
! 67524: }
! 67525:
! 67526: if( !isTransaction ){
! 67527: rc = sqlite3VtabSavepoint(db, u.ar.p1, u.ar.iSavepoint);
! 67528: if( rc!=SQLITE_OK ) goto abort_due_to_error;
! 67529: }
! 67530: }
! 67531: }
! 67532:
! 67533: break;
! 67534: }
! 67535:
! 67536: /* Opcode: AutoCommit P1 P2 * * *
! 67537: **
! 67538: ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
! 67539: ** back any currently active btree transactions. If there are any active
! 67540: ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
! 67541: ** there are active writing VMs or active VMs that use shared cache.
! 67542: **
! 67543: ** This instruction causes the VM to halt.
! 67544: */
! 67545: case OP_AutoCommit: {
! 67546: #if 0 /* local variables moved into u.as */
! 67547: int desiredAutoCommit;
! 67548: int iRollback;
! 67549: int turnOnAC;
! 67550: #endif /* local variables moved into u.as */
! 67551:
! 67552: u.as.desiredAutoCommit = pOp->p1;
! 67553: u.as.iRollback = pOp->p2;
! 67554: u.as.turnOnAC = u.as.desiredAutoCommit && !db->autoCommit;
! 67555: assert( u.as.desiredAutoCommit==1 || u.as.desiredAutoCommit==0 );
! 67556: assert( u.as.desiredAutoCommit==1 || u.as.iRollback==0 );
! 67557: assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
! 67558:
! 67559: if( u.as.turnOnAC && u.as.iRollback && db->activeVdbeCnt>1 ){
! 67560: /* If this instruction implements a ROLLBACK and other VMs are
! 67561: ** still running, and a transaction is active, return an error indicating
! 67562: ** that the other VMs must complete first.
! 67563: */
! 67564: sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
! 67565: "SQL statements in progress");
! 67566: rc = SQLITE_BUSY;
! 67567: }else if( u.as.turnOnAC && !u.as.iRollback && db->writeVdbeCnt>0 ){
! 67568: /* If this instruction implements a COMMIT and other VMs are writing
! 67569: ** return an error indicating that the other VMs must complete first.
! 67570: */
! 67571: sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
! 67572: "SQL statements in progress");
! 67573: rc = SQLITE_BUSY;
! 67574: }else if( u.as.desiredAutoCommit!=db->autoCommit ){
! 67575: if( u.as.iRollback ){
! 67576: assert( u.as.desiredAutoCommit==1 );
! 67577: sqlite3RollbackAll(db);
! 67578: db->autoCommit = 1;
! 67579: }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
! 67580: goto vdbe_return;
! 67581: }else{
! 67582: db->autoCommit = (u8)u.as.desiredAutoCommit;
! 67583: if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
! 67584: p->pc = pc;
! 67585: db->autoCommit = (u8)(1-u.as.desiredAutoCommit);
! 67586: p->rc = rc = SQLITE_BUSY;
! 67587: goto vdbe_return;
! 67588: }
! 67589: }
! 67590: assert( db->nStatement==0 );
! 67591: sqlite3CloseSavepoints(db);
! 67592: if( p->rc==SQLITE_OK ){
! 67593: rc = SQLITE_DONE;
! 67594: }else{
! 67595: rc = SQLITE_ERROR;
! 67596: }
! 67597: goto vdbe_return;
! 67598: }else{
! 67599: sqlite3SetString(&p->zErrMsg, db,
! 67600: (!u.as.desiredAutoCommit)?"cannot start a transaction within a transaction":(
! 67601: (u.as.iRollback)?"cannot rollback - no transaction is active":
! 67602: "cannot commit - no transaction is active"));
! 67603:
! 67604: rc = SQLITE_ERROR;
! 67605: }
! 67606: break;
! 67607: }
! 67608:
! 67609: /* Opcode: Transaction P1 P2 * * *
! 67610: **
! 67611: ** Begin a transaction. The transaction ends when a Commit or Rollback
! 67612: ** opcode is encountered. Depending on the ON CONFLICT setting, the
! 67613: ** transaction might also be rolled back if an error is encountered.
! 67614: **
! 67615: ** P1 is the index of the database file on which the transaction is
! 67616: ** started. Index 0 is the main database file and index 1 is the
! 67617: ** file used for temporary tables. Indices of 2 or more are used for
! 67618: ** attached databases.
! 67619: **
! 67620: ** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
! 67621: ** obtained on the database file when a write-transaction is started. No
! 67622: ** other process can start another write transaction while this transaction is
! 67623: ** underway. Starting a write transaction also creates a rollback journal. A
! 67624: ** write transaction must be started before any changes can be made to the
! 67625: ** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
! 67626: ** on the file.
! 67627: **
! 67628: ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
! 67629: ** true (this flag is set if the Vdbe may modify more than one row and may
! 67630: ** throw an ABORT exception), a statement transaction may also be opened.
! 67631: ** More specifically, a statement transaction is opened iff the database
! 67632: ** connection is currently not in autocommit mode, or if there are other
! 67633: ** active statements. A statement transaction allows the affects of this
! 67634: ** VDBE to be rolled back after an error without having to roll back the
! 67635: ** entire transaction. If no error is encountered, the statement transaction
! 67636: ** will automatically commit when the VDBE halts.
! 67637: **
! 67638: ** If P2 is zero, then a read-lock is obtained on the database file.
! 67639: */
! 67640: case OP_Transaction: {
! 67641: #if 0 /* local variables moved into u.at */
! 67642: Btree *pBt;
! 67643: #endif /* local variables moved into u.at */
! 67644:
! 67645: assert( pOp->p1>=0 && pOp->p1<db->nDb );
! 67646: assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
! 67647: u.at.pBt = db->aDb[pOp->p1].pBt;
! 67648:
! 67649: if( u.at.pBt ){
! 67650: rc = sqlite3BtreeBeginTrans(u.at.pBt, pOp->p2);
! 67651: if( rc==SQLITE_BUSY ){
! 67652: p->pc = pc;
! 67653: p->rc = rc = SQLITE_BUSY;
! 67654: goto vdbe_return;
! 67655: }
! 67656: if( rc!=SQLITE_OK ){
! 67657: goto abort_due_to_error;
! 67658: }
! 67659:
! 67660: if( pOp->p2 && p->usesStmtJournal
! 67661: && (db->autoCommit==0 || db->activeVdbeCnt>1)
! 67662: ){
! 67663: assert( sqlite3BtreeIsInTrans(u.at.pBt) );
! 67664: if( p->iStatement==0 ){
! 67665: assert( db->nStatement>=0 && db->nSavepoint>=0 );
! 67666: db->nStatement++;
! 67667: p->iStatement = db->nSavepoint + db->nStatement;
! 67668: }
! 67669:
! 67670: rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
! 67671: if( rc==SQLITE_OK ){
! 67672: rc = sqlite3BtreeBeginStmt(u.at.pBt, p->iStatement);
! 67673: }
! 67674:
! 67675: /* Store the current value of the database handles deferred constraint
! 67676: ** counter. If the statement transaction needs to be rolled back,
! 67677: ** the value of this counter needs to be restored too. */
! 67678: p->nStmtDefCons = db->nDeferredCons;
! 67679: }
! 67680: }
! 67681: break;
! 67682: }
! 67683:
! 67684: /* Opcode: ReadCookie P1 P2 P3 * *
! 67685: **
! 67686: ** Read cookie number P3 from database P1 and write it into register P2.
! 67687: ** P3==1 is the schema version. P3==2 is the database format.
! 67688: ** P3==3 is the recommended pager cache size, and so forth. P1==0 is
! 67689: ** the main database file and P1==1 is the database file used to store
! 67690: ** temporary tables.
! 67691: **
! 67692: ** There must be a read-lock on the database (either a transaction
! 67693: ** must be started or there must be an open cursor) before
! 67694: ** executing this instruction.
! 67695: */
! 67696: case OP_ReadCookie: { /* out2-prerelease */
! 67697: #if 0 /* local variables moved into u.au */
! 67698: int iMeta;
! 67699: int iDb;
! 67700: int iCookie;
! 67701: #endif /* local variables moved into u.au */
! 67702:
! 67703: u.au.iDb = pOp->p1;
! 67704: u.au.iCookie = pOp->p3;
! 67705: assert( pOp->p3<SQLITE_N_BTREE_META );
! 67706: assert( u.au.iDb>=0 && u.au.iDb<db->nDb );
! 67707: assert( db->aDb[u.au.iDb].pBt!=0 );
! 67708: assert( (p->btreeMask & (((yDbMask)1)<<u.au.iDb))!=0 );
! 67709:
! 67710: sqlite3BtreeGetMeta(db->aDb[u.au.iDb].pBt, u.au.iCookie, (u32 *)&u.au.iMeta);
! 67711: pOut->u.i = u.au.iMeta;
! 67712: break;
! 67713: }
! 67714:
! 67715: /* Opcode: SetCookie P1 P2 P3 * *
! 67716: **
! 67717: ** Write the content of register P3 (interpreted as an integer)
! 67718: ** into cookie number P2 of database P1. P2==1 is the schema version.
! 67719: ** P2==2 is the database format. P2==3 is the recommended pager cache
! 67720: ** size, and so forth. P1==0 is the main database file and P1==1 is the
! 67721: ** database file used to store temporary tables.
! 67722: **
! 67723: ** A transaction must be started before executing this opcode.
! 67724: */
! 67725: case OP_SetCookie: { /* in3 */
! 67726: #if 0 /* local variables moved into u.av */
! 67727: Db *pDb;
! 67728: #endif /* local variables moved into u.av */
! 67729: assert( pOp->p2<SQLITE_N_BTREE_META );
! 67730: assert( pOp->p1>=0 && pOp->p1<db->nDb );
! 67731: assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
! 67732: u.av.pDb = &db->aDb[pOp->p1];
! 67733: assert( u.av.pDb->pBt!=0 );
! 67734: assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
! 67735: pIn3 = &aMem[pOp->p3];
! 67736: sqlite3VdbeMemIntegerify(pIn3);
! 67737: /* See note about index shifting on OP_ReadCookie */
! 67738: rc = sqlite3BtreeUpdateMeta(u.av.pDb->pBt, pOp->p2, (int)pIn3->u.i);
! 67739: if( pOp->p2==BTREE_SCHEMA_VERSION ){
! 67740: /* When the schema cookie changes, record the new cookie internally */
! 67741: u.av.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
! 67742: db->flags |= SQLITE_InternChanges;
! 67743: }else if( pOp->p2==BTREE_FILE_FORMAT ){
! 67744: /* Record changes in the file format */
! 67745: u.av.pDb->pSchema->file_format = (u8)pIn3->u.i;
! 67746: }
! 67747: if( pOp->p1==1 ){
! 67748: /* Invalidate all prepared statements whenever the TEMP database
! 67749: ** schema is changed. Ticket #1644 */
! 67750: sqlite3ExpirePreparedStatements(db);
! 67751: p->expired = 0;
! 67752: }
! 67753: break;
! 67754: }
! 67755:
! 67756: /* Opcode: VerifyCookie P1 P2 P3 * *
! 67757: **
! 67758: ** Check the value of global database parameter number 0 (the
! 67759: ** schema version) and make sure it is equal to P2 and that the
! 67760: ** generation counter on the local schema parse equals P3.
! 67761: **
! 67762: ** P1 is the database number which is 0 for the main database file
! 67763: ** and 1 for the file holding temporary tables and some higher number
! 67764: ** for auxiliary databases.
! 67765: **
! 67766: ** The cookie changes its value whenever the database schema changes.
! 67767: ** This operation is used to detect when that the cookie has changed
! 67768: ** and that the current process needs to reread the schema.
! 67769: **
! 67770: ** Either a transaction needs to have been started or an OP_Open needs
! 67771: ** to be executed (to establish a read lock) before this opcode is
! 67772: ** invoked.
! 67773: */
! 67774: case OP_VerifyCookie: {
! 67775: #if 0 /* local variables moved into u.aw */
! 67776: int iMeta;
! 67777: int iGen;
! 67778: Btree *pBt;
! 67779: #endif /* local variables moved into u.aw */
! 67780:
! 67781: assert( pOp->p1>=0 && pOp->p1<db->nDb );
! 67782: assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
! 67783: assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
! 67784: u.aw.pBt = db->aDb[pOp->p1].pBt;
! 67785: if( u.aw.pBt ){
! 67786: sqlite3BtreeGetMeta(u.aw.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.aw.iMeta);
! 67787: u.aw.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
! 67788: }else{
! 67789: u.aw.iGen = u.aw.iMeta = 0;
! 67790: }
! 67791: if( u.aw.iMeta!=pOp->p2 || u.aw.iGen!=pOp->p3 ){
! 67792: sqlite3DbFree(db, p->zErrMsg);
! 67793: p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
! 67794: /* If the schema-cookie from the database file matches the cookie
! 67795: ** stored with the in-memory representation of the schema, do
! 67796: ** not reload the schema from the database file.
! 67797: **
! 67798: ** If virtual-tables are in use, this is not just an optimization.
! 67799: ** Often, v-tables store their data in other SQLite tables, which
! 67800: ** are queried from within xNext() and other v-table methods using
! 67801: ** prepared queries. If such a query is out-of-date, we do not want to
! 67802: ** discard the database schema, as the user code implementing the
! 67803: ** v-table would have to be ready for the sqlite3_vtab structure itself
! 67804: ** to be invalidated whenever sqlite3_step() is called from within
! 67805: ** a v-table method.
! 67806: */
! 67807: if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.aw.iMeta ){
! 67808: sqlite3ResetInternalSchema(db, pOp->p1);
! 67809: }
! 67810:
! 67811: p->expired = 1;
! 67812: rc = SQLITE_SCHEMA;
! 67813: }
! 67814: break;
! 67815: }
! 67816:
! 67817: /* Opcode: OpenRead P1 P2 P3 P4 P5
! 67818: **
! 67819: ** Open a read-only cursor for the database table whose root page is
! 67820: ** P2 in a database file. The database file is determined by P3.
! 67821: ** P3==0 means the main database, P3==1 means the database used for
! 67822: ** temporary tables, and P3>1 means used the corresponding attached
! 67823: ** database. Give the new cursor an identifier of P1. The P1
! 67824: ** values need not be contiguous but all P1 values should be small integers.
! 67825: ** It is an error for P1 to be negative.
! 67826: **
! 67827: ** If P5!=0 then use the content of register P2 as the root page, not
! 67828: ** the value of P2 itself.
! 67829: **
! 67830: ** There will be a read lock on the database whenever there is an
! 67831: ** open cursor. If the database was unlocked prior to this instruction
! 67832: ** then a read lock is acquired as part of this instruction. A read
! 67833: ** lock allows other processes to read the database but prohibits
! 67834: ** any other process from modifying the database. The read lock is
! 67835: ** released when all cursors are closed. If this instruction attempts
! 67836: ** to get a read lock but fails, the script terminates with an
! 67837: ** SQLITE_BUSY error code.
! 67838: **
! 67839: ** The P4 value may be either an integer (P4_INT32) or a pointer to
! 67840: ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
! 67841: ** structure, then said structure defines the content and collating
! 67842: ** sequence of the index being opened. Otherwise, if P4 is an integer
! 67843: ** value, it is set to the number of columns in the table.
! 67844: **
! 67845: ** See also OpenWrite.
! 67846: */
! 67847: /* Opcode: OpenWrite P1 P2 P3 P4 P5
! 67848: **
! 67849: ** Open a read/write cursor named P1 on the table or index whose root
! 67850: ** page is P2. Or if P5!=0 use the content of register P2 to find the
! 67851: ** root page.
! 67852: **
! 67853: ** The P4 value may be either an integer (P4_INT32) or a pointer to
! 67854: ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
! 67855: ** structure, then said structure defines the content and collating
! 67856: ** sequence of the index being opened. Otherwise, if P4 is an integer
! 67857: ** value, it is set to the number of columns in the table, or to the
! 67858: ** largest index of any column of the table that is actually used.
! 67859: **
! 67860: ** This instruction works just like OpenRead except that it opens the cursor
! 67861: ** in read/write mode. For a given table, there can be one or more read-only
! 67862: ** cursors or a single read/write cursor but not both.
! 67863: **
! 67864: ** See also OpenRead.
! 67865: */
! 67866: case OP_OpenRead:
! 67867: case OP_OpenWrite: {
! 67868: #if 0 /* local variables moved into u.ax */
! 67869: int nField;
! 67870: KeyInfo *pKeyInfo;
! 67871: int p2;
! 67872: int iDb;
! 67873: int wrFlag;
! 67874: Btree *pX;
! 67875: VdbeCursor *pCur;
! 67876: Db *pDb;
! 67877: #endif /* local variables moved into u.ax */
! 67878:
! 67879: if( p->expired ){
! 67880: rc = SQLITE_ABORT;
! 67881: break;
! 67882: }
! 67883:
! 67884: u.ax.nField = 0;
! 67885: u.ax.pKeyInfo = 0;
! 67886: u.ax.p2 = pOp->p2;
! 67887: u.ax.iDb = pOp->p3;
! 67888: assert( u.ax.iDb>=0 && u.ax.iDb<db->nDb );
! 67889: assert( (p->btreeMask & (((yDbMask)1)<<u.ax.iDb))!=0 );
! 67890: u.ax.pDb = &db->aDb[u.ax.iDb];
! 67891: u.ax.pX = u.ax.pDb->pBt;
! 67892: assert( u.ax.pX!=0 );
! 67893: if( pOp->opcode==OP_OpenWrite ){
! 67894: u.ax.wrFlag = 1;
! 67895: assert( sqlite3SchemaMutexHeld(db, u.ax.iDb, 0) );
! 67896: if( u.ax.pDb->pSchema->file_format < p->minWriteFileFormat ){
! 67897: p->minWriteFileFormat = u.ax.pDb->pSchema->file_format;
! 67898: }
! 67899: }else{
! 67900: u.ax.wrFlag = 0;
! 67901: }
! 67902: if( pOp->p5 ){
! 67903: assert( u.ax.p2>0 );
! 67904: assert( u.ax.p2<=p->nMem );
! 67905: pIn2 = &aMem[u.ax.p2];
! 67906: assert( memIsValid(pIn2) );
! 67907: assert( (pIn2->flags & MEM_Int)!=0 );
! 67908: sqlite3VdbeMemIntegerify(pIn2);
! 67909: u.ax.p2 = (int)pIn2->u.i;
! 67910: /* The u.ax.p2 value always comes from a prior OP_CreateTable opcode and
! 67911: ** that opcode will always set the u.ax.p2 value to 2 or more or else fail.
! 67912: ** If there were a failure, the prepared statement would have halted
! 67913: ** before reaching this instruction. */
! 67914: if( NEVER(u.ax.p2<2) ) {
! 67915: rc = SQLITE_CORRUPT_BKPT;
! 67916: goto abort_due_to_error;
! 67917: }
! 67918: }
! 67919: if( pOp->p4type==P4_KEYINFO ){
! 67920: u.ax.pKeyInfo = pOp->p4.pKeyInfo;
! 67921: u.ax.pKeyInfo->enc = ENC(p->db);
! 67922: u.ax.nField = u.ax.pKeyInfo->nField+1;
! 67923: }else if( pOp->p4type==P4_INT32 ){
! 67924: u.ax.nField = pOp->p4.i;
! 67925: }
! 67926: assert( pOp->p1>=0 );
! 67927: u.ax.pCur = allocateCursor(p, pOp->p1, u.ax.nField, u.ax.iDb, 1);
! 67928: if( u.ax.pCur==0 ) goto no_mem;
! 67929: u.ax.pCur->nullRow = 1;
! 67930: u.ax.pCur->isOrdered = 1;
! 67931: rc = sqlite3BtreeCursor(u.ax.pX, u.ax.p2, u.ax.wrFlag, u.ax.pKeyInfo, u.ax.pCur->pCursor);
! 67932: u.ax.pCur->pKeyInfo = u.ax.pKeyInfo;
! 67933:
! 67934: /* Since it performs no memory allocation or IO, the only value that
! 67935: ** sqlite3BtreeCursor() may return is SQLITE_OK. */
! 67936: assert( rc==SQLITE_OK );
! 67937:
! 67938: /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
! 67939: ** SQLite used to check if the root-page flags were sane at this point
! 67940: ** and report database corruption if they were not, but this check has
! 67941: ** since moved into the btree layer. */
! 67942: u.ax.pCur->isTable = pOp->p4type!=P4_KEYINFO;
! 67943: u.ax.pCur->isIndex = !u.ax.pCur->isTable;
! 67944: break;
! 67945: }
! 67946:
! 67947: /* Opcode: OpenEphemeral P1 P2 * P4 P5
! 67948: **
! 67949: ** Open a new cursor P1 to a transient table.
! 67950: ** The cursor is always opened read/write even if
! 67951: ** the main database is read-only. The ephemeral
! 67952: ** table is deleted automatically when the cursor is closed.
! 67953: **
! 67954: ** P2 is the number of columns in the ephemeral table.
! 67955: ** The cursor points to a BTree table if P4==0 and to a BTree index
! 67956: ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
! 67957: ** that defines the format of keys in the index.
! 67958: **
! 67959: ** This opcode was once called OpenTemp. But that created
! 67960: ** confusion because the term "temp table", might refer either
! 67961: ** to a TEMP table at the SQL level, or to a table opened by
! 67962: ** this opcode. Then this opcode was call OpenVirtual. But
! 67963: ** that created confusion with the whole virtual-table idea.
! 67964: **
! 67965: ** The P5 parameter can be a mask of the BTREE_* flags defined
! 67966: ** in btree.h. These flags control aspects of the operation of
! 67967: ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
! 67968: ** added automatically.
! 67969: */
! 67970: /* Opcode: OpenAutoindex P1 P2 * P4 *
! 67971: **
! 67972: ** This opcode works the same as OP_OpenEphemeral. It has a
! 67973: ** different name to distinguish its use. Tables created using
! 67974: ** by this opcode will be used for automatically created transient
! 67975: ** indices in joins.
! 67976: */
! 67977: case OP_OpenAutoindex:
! 67978: case OP_OpenEphemeral: {
! 67979: #if 0 /* local variables moved into u.ay */
! 67980: VdbeCursor *pCx;
! 67981: #endif /* local variables moved into u.ay */
! 67982: static const int vfsFlags =
! 67983: SQLITE_OPEN_READWRITE |
! 67984: SQLITE_OPEN_CREATE |
! 67985: SQLITE_OPEN_EXCLUSIVE |
! 67986: SQLITE_OPEN_DELETEONCLOSE |
! 67987: SQLITE_OPEN_TRANSIENT_DB;
! 67988:
! 67989: assert( pOp->p1>=0 );
! 67990: u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
! 67991: if( u.ay.pCx==0 ) goto no_mem;
! 67992: u.ay.pCx->nullRow = 1;
! 67993: rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ay.pCx->pBt,
! 67994: BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
! 67995: if( rc==SQLITE_OK ){
! 67996: rc = sqlite3BtreeBeginTrans(u.ay.pCx->pBt, 1);
! 67997: }
! 67998: if( rc==SQLITE_OK ){
! 67999: /* If a transient index is required, create it by calling
! 68000: ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
! 68001: ** opening it. If a transient table is required, just use the
! 68002: ** automatically created table with root-page 1 (an BLOB_INTKEY table).
! 68003: */
! 68004: if( pOp->p4.pKeyInfo ){
! 68005: int pgno;
! 68006: assert( pOp->p4type==P4_KEYINFO );
! 68007: rc = sqlite3BtreeCreateTable(u.ay.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
! 68008: if( rc==SQLITE_OK ){
! 68009: assert( pgno==MASTER_ROOT+1 );
! 68010: rc = sqlite3BtreeCursor(u.ay.pCx->pBt, pgno, 1,
! 68011: (KeyInfo*)pOp->p4.z, u.ay.pCx->pCursor);
! 68012: u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
! 68013: u.ay.pCx->pKeyInfo->enc = ENC(p->db);
! 68014: }
! 68015: u.ay.pCx->isTable = 0;
! 68016: }else{
! 68017: rc = sqlite3BtreeCursor(u.ay.pCx->pBt, MASTER_ROOT, 1, 0, u.ay.pCx->pCursor);
! 68018: u.ay.pCx->isTable = 1;
! 68019: }
! 68020: }
! 68021: u.ay.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
! 68022: u.ay.pCx->isIndex = !u.ay.pCx->isTable;
! 68023: break;
! 68024: }
! 68025:
! 68026: /* Opcode: OpenSorter P1 P2 * P4 *
! 68027: **
! 68028: ** This opcode works like OP_OpenEphemeral except that it opens
! 68029: ** a transient index that is specifically designed to sort large
! 68030: ** tables using an external merge-sort algorithm.
! 68031: */
! 68032: case OP_SorterOpen: {
! 68033: #if 0 /* local variables moved into u.az */
! 68034: VdbeCursor *pCx;
! 68035: #endif /* local variables moved into u.az */
! 68036: #ifndef SQLITE_OMIT_MERGE_SORT
! 68037: u.az.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
! 68038: if( u.az.pCx==0 ) goto no_mem;
! 68039: u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
! 68040: u.az.pCx->pKeyInfo->enc = ENC(p->db);
! 68041: u.az.pCx->isSorter = 1;
! 68042: rc = sqlite3VdbeSorterInit(db, u.az.pCx);
! 68043: #else
! 68044: pOp->opcode = OP_OpenEphemeral;
! 68045: pc--;
! 68046: #endif
! 68047: break;
! 68048: }
! 68049:
! 68050: /* Opcode: OpenPseudo P1 P2 P3 * *
! 68051: **
! 68052: ** Open a new cursor that points to a fake table that contains a single
! 68053: ** row of data. The content of that one row in the content of memory
! 68054: ** register P2. In other words, cursor P1 becomes an alias for the
! 68055: ** MEM_Blob content contained in register P2.
! 68056: **
! 68057: ** A pseudo-table created by this opcode is used to hold a single
! 68058: ** row output from the sorter so that the row can be decomposed into
! 68059: ** individual columns using the OP_Column opcode. The OP_Column opcode
! 68060: ** is the only cursor opcode that works with a pseudo-table.
! 68061: **
! 68062: ** P3 is the number of fields in the records that will be stored by
! 68063: ** the pseudo-table.
! 68064: */
! 68065: case OP_OpenPseudo: {
! 68066: #if 0 /* local variables moved into u.ba */
! 68067: VdbeCursor *pCx;
! 68068: #endif /* local variables moved into u.ba */
! 68069:
! 68070: assert( pOp->p1>=0 );
! 68071: u.ba.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
! 68072: if( u.ba.pCx==0 ) goto no_mem;
! 68073: u.ba.pCx->nullRow = 1;
! 68074: u.ba.pCx->pseudoTableReg = pOp->p2;
! 68075: u.ba.pCx->isTable = 1;
! 68076: u.ba.pCx->isIndex = 0;
! 68077: break;
! 68078: }
! 68079:
! 68080: /* Opcode: Close P1 * * * *
! 68081: **
! 68082: ** Close a cursor previously opened as P1. If P1 is not
! 68083: ** currently open, this instruction is a no-op.
! 68084: */
! 68085: case OP_Close: {
! 68086: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 68087: sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
! 68088: p->apCsr[pOp->p1] = 0;
! 68089: break;
! 68090: }
! 68091:
! 68092: /* Opcode: SeekGe P1 P2 P3 P4 *
! 68093: **
! 68094: ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
! 68095: ** use the value in register P3 as the key. If cursor P1 refers
! 68096: ** to an SQL index, then P3 is the first in an array of P4 registers
! 68097: ** that are used as an unpacked index key.
! 68098: **
! 68099: ** Reposition cursor P1 so that it points to the smallest entry that
! 68100: ** is greater than or equal to the key value. If there are no records
! 68101: ** greater than or equal to the key and P2 is not zero, then jump to P2.
! 68102: **
! 68103: ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
! 68104: */
! 68105: /* Opcode: SeekGt P1 P2 P3 P4 *
! 68106: **
! 68107: ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
! 68108: ** use the value in register P3 as a key. If cursor P1 refers
! 68109: ** to an SQL index, then P3 is the first in an array of P4 registers
! 68110: ** that are used as an unpacked index key.
! 68111: **
! 68112: ** Reposition cursor P1 so that it points to the smallest entry that
! 68113: ** is greater than the key value. If there are no records greater than
! 68114: ** the key and P2 is not zero, then jump to P2.
! 68115: **
! 68116: ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
! 68117: */
! 68118: /* Opcode: SeekLt P1 P2 P3 P4 *
! 68119: **
! 68120: ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
! 68121: ** use the value in register P3 as a key. If cursor P1 refers
! 68122: ** to an SQL index, then P3 is the first in an array of P4 registers
! 68123: ** that are used as an unpacked index key.
! 68124: **
! 68125: ** Reposition cursor P1 so that it points to the largest entry that
! 68126: ** is less than the key value. If there are no records less than
! 68127: ** the key and P2 is not zero, then jump to P2.
! 68128: **
! 68129: ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
! 68130: */
! 68131: /* Opcode: SeekLe P1 P2 P3 P4 *
! 68132: **
! 68133: ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
! 68134: ** use the value in register P3 as a key. If cursor P1 refers
! 68135: ** to an SQL index, then P3 is the first in an array of P4 registers
! 68136: ** that are used as an unpacked index key.
! 68137: **
! 68138: ** Reposition cursor P1 so that it points to the largest entry that
! 68139: ** is less than or equal to the key value. If there are no records
! 68140: ** less than or equal to the key and P2 is not zero, then jump to P2.
! 68141: **
! 68142: ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
! 68143: */
! 68144: case OP_SeekLt: /* jump, in3 */
! 68145: case OP_SeekLe: /* jump, in3 */
! 68146: case OP_SeekGe: /* jump, in3 */
! 68147: case OP_SeekGt: { /* jump, in3 */
! 68148: #if 0 /* local variables moved into u.bb */
! 68149: int res;
! 68150: int oc;
! 68151: VdbeCursor *pC;
! 68152: UnpackedRecord r;
! 68153: int nField;
! 68154: i64 iKey; /* The rowid we are to seek to */
! 68155: #endif /* local variables moved into u.bb */
! 68156:
! 68157: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 68158: assert( pOp->p2!=0 );
! 68159: u.bb.pC = p->apCsr[pOp->p1];
! 68160: assert( u.bb.pC!=0 );
! 68161: assert( u.bb.pC->pseudoTableReg==0 );
! 68162: assert( OP_SeekLe == OP_SeekLt+1 );
! 68163: assert( OP_SeekGe == OP_SeekLt+2 );
! 68164: assert( OP_SeekGt == OP_SeekLt+3 );
! 68165: assert( u.bb.pC->isOrdered );
! 68166: if( ALWAYS(u.bb.pC->pCursor!=0) ){
! 68167: u.bb.oc = pOp->opcode;
! 68168: u.bb.pC->nullRow = 0;
! 68169: if( u.bb.pC->isTable ){
! 68170: /* The input value in P3 might be of any type: integer, real, string,
! 68171: ** blob, or NULL. But it needs to be an integer before we can do
! 68172: ** the seek, so covert it. */
! 68173: pIn3 = &aMem[pOp->p3];
! 68174: applyNumericAffinity(pIn3);
! 68175: u.bb.iKey = sqlite3VdbeIntValue(pIn3);
! 68176: u.bb.pC->rowidIsValid = 0;
! 68177:
! 68178: /* If the P3 value could not be converted into an integer without
! 68179: ** loss of information, then special processing is required... */
! 68180: if( (pIn3->flags & MEM_Int)==0 ){
! 68181: if( (pIn3->flags & MEM_Real)==0 ){
! 68182: /* If the P3 value cannot be converted into any kind of a number,
! 68183: ** then the seek is not possible, so jump to P2 */
! 68184: pc = pOp->p2 - 1;
! 68185: break;
! 68186: }
! 68187: /* If we reach this point, then the P3 value must be a floating
! 68188: ** point number. */
! 68189: assert( (pIn3->flags & MEM_Real)!=0 );
! 68190:
! 68191: if( u.bb.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bb.iKey || pIn3->r>0) ){
! 68192: /* The P3 value is too large in magnitude to be expressed as an
! 68193: ** integer. */
! 68194: u.bb.res = 1;
! 68195: if( pIn3->r<0 ){
! 68196: if( u.bb.oc>=OP_SeekGe ){ assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
! 68197: rc = sqlite3BtreeFirst(u.bb.pC->pCursor, &u.bb.res);
! 68198: if( rc!=SQLITE_OK ) goto abort_due_to_error;
! 68199: }
! 68200: }else{
! 68201: if( u.bb.oc<=OP_SeekLe ){ assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
! 68202: rc = sqlite3BtreeLast(u.bb.pC->pCursor, &u.bb.res);
! 68203: if( rc!=SQLITE_OK ) goto abort_due_to_error;
! 68204: }
! 68205: }
! 68206: if( u.bb.res ){
! 68207: pc = pOp->p2 - 1;
! 68208: }
! 68209: break;
! 68210: }else if( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekGe ){
! 68211: /* Use the ceiling() function to convert real->int */
! 68212: if( pIn3->r > (double)u.bb.iKey ) u.bb.iKey++;
! 68213: }else{
! 68214: /* Use the floor() function to convert real->int */
! 68215: assert( u.bb.oc==OP_SeekLe || u.bb.oc==OP_SeekGt );
! 68216: if( pIn3->r < (double)u.bb.iKey ) u.bb.iKey--;
! 68217: }
! 68218: }
! 68219: rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, 0, (u64)u.bb.iKey, 0, &u.bb.res);
! 68220: if( rc!=SQLITE_OK ){
! 68221: goto abort_due_to_error;
! 68222: }
! 68223: if( u.bb.res==0 ){
! 68224: u.bb.pC->rowidIsValid = 1;
! 68225: u.bb.pC->lastRowid = u.bb.iKey;
! 68226: }
! 68227: }else{
! 68228: u.bb.nField = pOp->p4.i;
! 68229: assert( pOp->p4type==P4_INT32 );
! 68230: assert( u.bb.nField>0 );
! 68231: u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo;
! 68232: u.bb.r.nField = (u16)u.bb.nField;
! 68233:
! 68234: /* The next line of code computes as follows, only faster:
! 68235: ** if( u.bb.oc==OP_SeekGt || u.bb.oc==OP_SeekLe ){
! 68236: ** u.bb.r.flags = UNPACKED_INCRKEY;
! 68237: ** }else{
! 68238: ** u.bb.r.flags = 0;
! 68239: ** }
! 68240: */
! 68241: u.bb.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bb.oc - OP_SeekLt)));
! 68242: assert( u.bb.oc!=OP_SeekGt || u.bb.r.flags==UNPACKED_INCRKEY );
! 68243: assert( u.bb.oc!=OP_SeekLe || u.bb.r.flags==UNPACKED_INCRKEY );
! 68244: assert( u.bb.oc!=OP_SeekGe || u.bb.r.flags==0 );
! 68245: assert( u.bb.oc!=OP_SeekLt || u.bb.r.flags==0 );
! 68246:
! 68247: u.bb.r.aMem = &aMem[pOp->p3];
! 68248: #ifdef SQLITE_DEBUG
! 68249: { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); }
! 68250: #endif
! 68251: ExpandBlob(u.bb.r.aMem);
! 68252: rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, &u.bb.r, 0, 0, &u.bb.res);
! 68253: if( rc!=SQLITE_OK ){
! 68254: goto abort_due_to_error;
! 68255: }
! 68256: u.bb.pC->rowidIsValid = 0;
! 68257: }
! 68258: u.bb.pC->deferredMoveto = 0;
! 68259: u.bb.pC->cacheStatus = CACHE_STALE;
! 68260: #ifdef SQLITE_TEST
! 68261: sqlite3_search_count++;
! 68262: #endif
! 68263: if( u.bb.oc>=OP_SeekGe ){ assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
! 68264: if( u.bb.res<0 || (u.bb.res==0 && u.bb.oc==OP_SeekGt) ){
! 68265: rc = sqlite3BtreeNext(u.bb.pC->pCursor, &u.bb.res);
! 68266: if( rc!=SQLITE_OK ) goto abort_due_to_error;
! 68267: u.bb.pC->rowidIsValid = 0;
! 68268: }else{
! 68269: u.bb.res = 0;
! 68270: }
! 68271: }else{
! 68272: assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
! 68273: if( u.bb.res>0 || (u.bb.res==0 && u.bb.oc==OP_SeekLt) ){
! 68274: rc = sqlite3BtreePrevious(u.bb.pC->pCursor, &u.bb.res);
! 68275: if( rc!=SQLITE_OK ) goto abort_due_to_error;
! 68276: u.bb.pC->rowidIsValid = 0;
! 68277: }else{
! 68278: /* u.bb.res might be negative because the table is empty. Check to
! 68279: ** see if this is the case.
! 68280: */
! 68281: u.bb.res = sqlite3BtreeEof(u.bb.pC->pCursor);
! 68282: }
! 68283: }
! 68284: assert( pOp->p2>0 );
! 68285: if( u.bb.res ){
! 68286: pc = pOp->p2 - 1;
! 68287: }
! 68288: }else{
! 68289: /* This happens when attempting to open the sqlite3_master table
! 68290: ** for read access returns SQLITE_EMPTY. In this case always
! 68291: ** take the jump (since there are no records in the table).
! 68292: */
! 68293: pc = pOp->p2 - 1;
! 68294: }
! 68295: break;
! 68296: }
! 68297:
! 68298: /* Opcode: Seek P1 P2 * * *
! 68299: **
! 68300: ** P1 is an open table cursor and P2 is a rowid integer. Arrange
! 68301: ** for P1 to move so that it points to the rowid given by P2.
! 68302: **
! 68303: ** This is actually a deferred seek. Nothing actually happens until
! 68304: ** the cursor is used to read a record. That way, if no reads
! 68305: ** occur, no unnecessary I/O happens.
! 68306: */
! 68307: case OP_Seek: { /* in2 */
! 68308: #if 0 /* local variables moved into u.bc */
! 68309: VdbeCursor *pC;
! 68310: #endif /* local variables moved into u.bc */
! 68311:
! 68312: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 68313: u.bc.pC = p->apCsr[pOp->p1];
! 68314: assert( u.bc.pC!=0 );
! 68315: if( ALWAYS(u.bc.pC->pCursor!=0) ){
! 68316: assert( u.bc.pC->isTable );
! 68317: u.bc.pC->nullRow = 0;
! 68318: pIn2 = &aMem[pOp->p2];
! 68319: u.bc.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
! 68320: u.bc.pC->rowidIsValid = 0;
! 68321: u.bc.pC->deferredMoveto = 1;
! 68322: }
! 68323: break;
! 68324: }
! 68325:
! 68326:
! 68327: /* Opcode: Found P1 P2 P3 P4 *
! 68328: **
! 68329: ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
! 68330: ** P4>0 then register P3 is the first of P4 registers that form an unpacked
! 68331: ** record.
! 68332: **
! 68333: ** Cursor P1 is on an index btree. If the record identified by P3 and P4
! 68334: ** is a prefix of any entry in P1 then a jump is made to P2 and
! 68335: ** P1 is left pointing at the matching entry.
! 68336: */
! 68337: /* Opcode: NotFound P1 P2 P3 P4 *
! 68338: **
! 68339: ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
! 68340: ** P4>0 then register P3 is the first of P4 registers that form an unpacked
! 68341: ** record.
! 68342: **
! 68343: ** Cursor P1 is on an index btree. If the record identified by P3 and P4
! 68344: ** is not the prefix of any entry in P1 then a jump is made to P2. If P1
! 68345: ** does contain an entry whose prefix matches the P3/P4 record then control
! 68346: ** falls through to the next instruction and P1 is left pointing at the
! 68347: ** matching entry.
! 68348: **
! 68349: ** See also: Found, NotExists, IsUnique
! 68350: */
! 68351: case OP_NotFound: /* jump, in3 */
! 68352: case OP_Found: { /* jump, in3 */
! 68353: #if 0 /* local variables moved into u.bd */
! 68354: int alreadyExists;
! 68355: VdbeCursor *pC;
! 68356: int res;
! 68357: char *pFree;
! 68358: UnpackedRecord *pIdxKey;
! 68359: UnpackedRecord r;
! 68360: char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
! 68361: #endif /* local variables moved into u.bd */
! 68362:
! 68363: #ifdef SQLITE_TEST
! 68364: sqlite3_found_count++;
! 68365: #endif
! 68366:
! 68367: u.bd.alreadyExists = 0;
! 68368: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 68369: assert( pOp->p4type==P4_INT32 );
! 68370: u.bd.pC = p->apCsr[pOp->p1];
! 68371: assert( u.bd.pC!=0 );
! 68372: pIn3 = &aMem[pOp->p3];
! 68373: if( ALWAYS(u.bd.pC->pCursor!=0) ){
! 68374:
! 68375: assert( u.bd.pC->isTable==0 );
! 68376: if( pOp->p4.i>0 ){
! 68377: u.bd.r.pKeyInfo = u.bd.pC->pKeyInfo;
! 68378: u.bd.r.nField = (u16)pOp->p4.i;
! 68379: u.bd.r.aMem = pIn3;
! 68380: #ifdef SQLITE_DEBUG
! 68381: { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
! 68382: #endif
! 68383: u.bd.r.flags = UNPACKED_PREFIX_MATCH;
! 68384: u.bd.pIdxKey = &u.bd.r;
! 68385: }else{
! 68386: u.bd.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
! 68387: u.bd.pC->pKeyInfo, u.bd.aTempRec, sizeof(u.bd.aTempRec), &u.bd.pFree
! 68388: );
! 68389: if( u.bd.pIdxKey==0 ) goto no_mem;
! 68390: assert( pIn3->flags & MEM_Blob );
! 68391: assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
! 68392: sqlite3VdbeRecordUnpack(u.bd.pC->pKeyInfo, pIn3->n, pIn3->z, u.bd.pIdxKey);
! 68393: u.bd.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
! 68394: }
! 68395: rc = sqlite3BtreeMovetoUnpacked(u.bd.pC->pCursor, u.bd.pIdxKey, 0, 0, &u.bd.res);
! 68396: if( pOp->p4.i==0 ){
! 68397: sqlite3DbFree(db, u.bd.pFree);
! 68398: }
! 68399: if( rc!=SQLITE_OK ){
! 68400: break;
! 68401: }
! 68402: u.bd.alreadyExists = (u.bd.res==0);
! 68403: u.bd.pC->deferredMoveto = 0;
! 68404: u.bd.pC->cacheStatus = CACHE_STALE;
! 68405: }
! 68406: if( pOp->opcode==OP_Found ){
! 68407: if( u.bd.alreadyExists ) pc = pOp->p2 - 1;
! 68408: }else{
! 68409: if( !u.bd.alreadyExists ) pc = pOp->p2 - 1;
! 68410: }
! 68411: break;
! 68412: }
! 68413:
! 68414: /* Opcode: IsUnique P1 P2 P3 P4 *
! 68415: **
! 68416: ** Cursor P1 is open on an index b-tree - that is to say, a btree which
! 68417: ** no data and where the key are records generated by OP_MakeRecord with
! 68418: ** the list field being the integer ROWID of the entry that the index
! 68419: ** entry refers to.
! 68420: **
! 68421: ** The P3 register contains an integer record number. Call this record
! 68422: ** number R. Register P4 is the first in a set of N contiguous registers
! 68423: ** that make up an unpacked index key that can be used with cursor P1.
! 68424: ** The value of N can be inferred from the cursor. N includes the rowid
! 68425: ** value appended to the end of the index record. This rowid value may
! 68426: ** or may not be the same as R.
! 68427: **
! 68428: ** If any of the N registers beginning with register P4 contains a NULL
! 68429: ** value, jump immediately to P2.
! 68430: **
! 68431: ** Otherwise, this instruction checks if cursor P1 contains an entry
! 68432: ** where the first (N-1) fields match but the rowid value at the end
! 68433: ** of the index entry is not R. If there is no such entry, control jumps
! 68434: ** to instruction P2. Otherwise, the rowid of the conflicting index
! 68435: ** entry is copied to register P3 and control falls through to the next
! 68436: ** instruction.
! 68437: **
! 68438: ** See also: NotFound, NotExists, Found
! 68439: */
! 68440: case OP_IsUnique: { /* jump, in3 */
! 68441: #if 0 /* local variables moved into u.be */
! 68442: u16 ii;
! 68443: VdbeCursor *pCx;
! 68444: BtCursor *pCrsr;
! 68445: u16 nField;
! 68446: Mem *aMx;
! 68447: UnpackedRecord r; /* B-Tree index search key */
! 68448: i64 R; /* Rowid stored in register P3 */
! 68449: #endif /* local variables moved into u.be */
! 68450:
! 68451: pIn3 = &aMem[pOp->p3];
! 68452: u.be.aMx = &aMem[pOp->p4.i];
! 68453: /* Assert that the values of parameters P1 and P4 are in range. */
! 68454: assert( pOp->p4type==P4_INT32 );
! 68455: assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
! 68456: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 68457:
! 68458: /* Find the index cursor. */
! 68459: u.be.pCx = p->apCsr[pOp->p1];
! 68460: assert( u.be.pCx->deferredMoveto==0 );
! 68461: u.be.pCx->seekResult = 0;
! 68462: u.be.pCx->cacheStatus = CACHE_STALE;
! 68463: u.be.pCrsr = u.be.pCx->pCursor;
! 68464:
! 68465: /* If any of the values are NULL, take the jump. */
! 68466: u.be.nField = u.be.pCx->pKeyInfo->nField;
! 68467: for(u.be.ii=0; u.be.ii<u.be.nField; u.be.ii++){
! 68468: if( u.be.aMx[u.be.ii].flags & MEM_Null ){
! 68469: pc = pOp->p2 - 1;
! 68470: u.be.pCrsr = 0;
! 68471: break;
! 68472: }
! 68473: }
! 68474: assert( (u.be.aMx[u.be.nField].flags & MEM_Null)==0 );
! 68475:
! 68476: if( u.be.pCrsr!=0 ){
! 68477: /* Populate the index search key. */
! 68478: u.be.r.pKeyInfo = u.be.pCx->pKeyInfo;
! 68479: u.be.r.nField = u.be.nField + 1;
! 68480: u.be.r.flags = UNPACKED_PREFIX_SEARCH;
! 68481: u.be.r.aMem = u.be.aMx;
! 68482: #ifdef SQLITE_DEBUG
! 68483: { int i; for(i=0; i<u.be.r.nField; i++) assert( memIsValid(&u.be.r.aMem[i]) ); }
! 68484: #endif
! 68485:
! 68486: /* Extract the value of u.be.R from register P3. */
! 68487: sqlite3VdbeMemIntegerify(pIn3);
! 68488: u.be.R = pIn3->u.i;
! 68489:
! 68490: /* Search the B-Tree index. If no conflicting record is found, jump
! 68491: ** to P2. Otherwise, copy the rowid of the conflicting record to
! 68492: ** register P3 and fall through to the next instruction. */
! 68493: rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, &u.be.r, 0, 0, &u.be.pCx->seekResult);
! 68494: if( (u.be.r.flags & UNPACKED_PREFIX_SEARCH) || u.be.r.rowid==u.be.R ){
! 68495: pc = pOp->p2 - 1;
! 68496: }else{
! 68497: pIn3->u.i = u.be.r.rowid;
! 68498: }
! 68499: }
! 68500: break;
! 68501: }
! 68502:
! 68503: /* Opcode: NotExists P1 P2 P3 * *
! 68504: **
! 68505: ** Use the content of register P3 as an integer key. If a record
! 68506: ** with that key does not exist in table of P1, then jump to P2.
! 68507: ** If the record does exist, then fall through. The cursor is left
! 68508: ** pointing to the record if it exists.
! 68509: **
! 68510: ** The difference between this operation and NotFound is that this
! 68511: ** operation assumes the key is an integer and that P1 is a table whereas
! 68512: ** NotFound assumes key is a blob constructed from MakeRecord and
! 68513: ** P1 is an index.
! 68514: **
! 68515: ** See also: Found, NotFound, IsUnique
! 68516: */
! 68517: case OP_NotExists: { /* jump, in3 */
! 68518: #if 0 /* local variables moved into u.bf */
! 68519: VdbeCursor *pC;
! 68520: BtCursor *pCrsr;
! 68521: int res;
! 68522: u64 iKey;
! 68523: #endif /* local variables moved into u.bf */
! 68524:
! 68525: pIn3 = &aMem[pOp->p3];
! 68526: assert( pIn3->flags & MEM_Int );
! 68527: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 68528: u.bf.pC = p->apCsr[pOp->p1];
! 68529: assert( u.bf.pC!=0 );
! 68530: assert( u.bf.pC->isTable );
! 68531: assert( u.bf.pC->pseudoTableReg==0 );
! 68532: u.bf.pCrsr = u.bf.pC->pCursor;
! 68533: if( ALWAYS(u.bf.pCrsr!=0) ){
! 68534: u.bf.res = 0;
! 68535: u.bf.iKey = pIn3->u.i;
! 68536: rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, 0, u.bf.iKey, 0, &u.bf.res);
! 68537: u.bf.pC->lastRowid = pIn3->u.i;
! 68538: u.bf.pC->rowidIsValid = u.bf.res==0 ?1:0;
! 68539: u.bf.pC->nullRow = 0;
! 68540: u.bf.pC->cacheStatus = CACHE_STALE;
! 68541: u.bf.pC->deferredMoveto = 0;
! 68542: if( u.bf.res!=0 ){
! 68543: pc = pOp->p2 - 1;
! 68544: assert( u.bf.pC->rowidIsValid==0 );
! 68545: }
! 68546: u.bf.pC->seekResult = u.bf.res;
! 68547: }else{
! 68548: /* This happens when an attempt to open a read cursor on the
! 68549: ** sqlite_master table returns SQLITE_EMPTY.
! 68550: */
! 68551: pc = pOp->p2 - 1;
! 68552: assert( u.bf.pC->rowidIsValid==0 );
! 68553: u.bf.pC->seekResult = 0;
! 68554: }
! 68555: break;
! 68556: }
! 68557:
! 68558: /* Opcode: Sequence P1 P2 * * *
! 68559: **
! 68560: ** Find the next available sequence number for cursor P1.
! 68561: ** Write the sequence number into register P2.
! 68562: ** The sequence number on the cursor is incremented after this
! 68563: ** instruction.
! 68564: */
! 68565: case OP_Sequence: { /* out2-prerelease */
! 68566: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 68567: assert( p->apCsr[pOp->p1]!=0 );
! 68568: pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
! 68569: break;
! 68570: }
! 68571:
! 68572:
! 68573: /* Opcode: NewRowid P1 P2 P3 * *
! 68574: **
! 68575: ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
! 68576: ** The record number is not previously used as a key in the database
! 68577: ** table that cursor P1 points to. The new record number is written
! 68578: ** written to register P2.
! 68579: **
! 68580: ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
! 68581: ** the largest previously generated record number. No new record numbers are
! 68582: ** allowed to be less than this value. When this value reaches its maximum,
! 68583: ** an SQLITE_FULL error is generated. The P3 register is updated with the '
! 68584: ** generated record number. This P3 mechanism is used to help implement the
! 68585: ** AUTOINCREMENT feature.
! 68586: */
! 68587: case OP_NewRowid: { /* out2-prerelease */
! 68588: #if 0 /* local variables moved into u.bg */
! 68589: i64 v; /* The new rowid */
! 68590: VdbeCursor *pC; /* Cursor of table to get the new rowid */
! 68591: int res; /* Result of an sqlite3BtreeLast() */
! 68592: int cnt; /* Counter to limit the number of searches */
! 68593: Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
! 68594: VdbeFrame *pFrame; /* Root frame of VDBE */
! 68595: #endif /* local variables moved into u.bg */
! 68596:
! 68597: u.bg.v = 0;
! 68598: u.bg.res = 0;
! 68599: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 68600: u.bg.pC = p->apCsr[pOp->p1];
! 68601: assert( u.bg.pC!=0 );
! 68602: if( NEVER(u.bg.pC->pCursor==0) ){
! 68603: /* The zero initialization above is all that is needed */
! 68604: }else{
! 68605: /* The next rowid or record number (different terms for the same
! 68606: ** thing) is obtained in a two-step algorithm.
! 68607: **
! 68608: ** First we attempt to find the largest existing rowid and add one
! 68609: ** to that. But if the largest existing rowid is already the maximum
! 68610: ** positive integer, we have to fall through to the second
! 68611: ** probabilistic algorithm
! 68612: **
! 68613: ** The second algorithm is to select a rowid at random and see if
! 68614: ** it already exists in the table. If it does not exist, we have
! 68615: ** succeeded. If the random rowid does exist, we select a new one
! 68616: ** and try again, up to 100 times.
! 68617: */
! 68618: assert( u.bg.pC->isTable );
! 68619:
! 68620: #ifdef SQLITE_32BIT_ROWID
! 68621: # define MAX_ROWID 0x7fffffff
! 68622: #else
! 68623: /* Some compilers complain about constants of the form 0x7fffffffffffffff.
! 68624: ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
! 68625: ** to provide the constant while making all compilers happy.
! 68626: */
! 68627: # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
! 68628: #endif
! 68629:
! 68630: if( !u.bg.pC->useRandomRowid ){
! 68631: u.bg.v = sqlite3BtreeGetCachedRowid(u.bg.pC->pCursor);
! 68632: if( u.bg.v==0 ){
! 68633: rc = sqlite3BtreeLast(u.bg.pC->pCursor, &u.bg.res);
! 68634: if( rc!=SQLITE_OK ){
! 68635: goto abort_due_to_error;
! 68636: }
! 68637: if( u.bg.res ){
! 68638: u.bg.v = 1; /* IMP: R-61914-48074 */
! 68639: }else{
! 68640: assert( sqlite3BtreeCursorIsValid(u.bg.pC->pCursor) );
! 68641: rc = sqlite3BtreeKeySize(u.bg.pC->pCursor, &u.bg.v);
! 68642: assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
! 68643: if( u.bg.v==MAX_ROWID ){
! 68644: u.bg.pC->useRandomRowid = 1;
! 68645: }else{
! 68646: u.bg.v++; /* IMP: R-29538-34987 */
! 68647: }
! 68648: }
! 68649: }
! 68650:
! 68651: #ifndef SQLITE_OMIT_AUTOINCREMENT
! 68652: if( pOp->p3 ){
! 68653: /* Assert that P3 is a valid memory cell. */
! 68654: assert( pOp->p3>0 );
! 68655: if( p->pFrame ){
! 68656: for(u.bg.pFrame=p->pFrame; u.bg.pFrame->pParent; u.bg.pFrame=u.bg.pFrame->pParent);
! 68657: /* Assert that P3 is a valid memory cell. */
! 68658: assert( pOp->p3<=u.bg.pFrame->nMem );
! 68659: u.bg.pMem = &u.bg.pFrame->aMem[pOp->p3];
! 68660: }else{
! 68661: /* Assert that P3 is a valid memory cell. */
! 68662: assert( pOp->p3<=p->nMem );
! 68663: u.bg.pMem = &aMem[pOp->p3];
! 68664: memAboutToChange(p, u.bg.pMem);
! 68665: }
! 68666: assert( memIsValid(u.bg.pMem) );
! 68667:
! 68668: REGISTER_TRACE(pOp->p3, u.bg.pMem);
! 68669: sqlite3VdbeMemIntegerify(u.bg.pMem);
! 68670: assert( (u.bg.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
! 68671: if( u.bg.pMem->u.i==MAX_ROWID || u.bg.pC->useRandomRowid ){
! 68672: rc = SQLITE_FULL; /* IMP: R-12275-61338 */
! 68673: goto abort_due_to_error;
! 68674: }
! 68675: if( u.bg.v<u.bg.pMem->u.i+1 ){
! 68676: u.bg.v = u.bg.pMem->u.i + 1;
! 68677: }
! 68678: u.bg.pMem->u.i = u.bg.v;
! 68679: }
! 68680: #endif
! 68681:
! 68682: sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, u.bg.v<MAX_ROWID ? u.bg.v+1 : 0);
! 68683: }
! 68684: if( u.bg.pC->useRandomRowid ){
! 68685: /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
! 68686: ** largest possible integer (9223372036854775807) then the database
! 68687: ** engine starts picking positive candidate ROWIDs at random until
! 68688: ** it finds one that is not previously used. */
! 68689: assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
! 68690: ** an AUTOINCREMENT table. */
! 68691: /* on the first attempt, simply do one more than previous */
! 68692: u.bg.v = lastRowid;
! 68693: u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
! 68694: u.bg.v++; /* ensure non-zero */
! 68695: u.bg.cnt = 0;
! 68696: while( ((rc = sqlite3BtreeMovetoUnpacked(u.bg.pC->pCursor, 0, (u64)u.bg.v,
! 68697: 0, &u.bg.res))==SQLITE_OK)
! 68698: && (u.bg.res==0)
! 68699: && (++u.bg.cnt<100)){
! 68700: /* collision - try another random rowid */
! 68701: sqlite3_randomness(sizeof(u.bg.v), &u.bg.v);
! 68702: if( u.bg.cnt<5 ){
! 68703: /* try "small" random rowids for the initial attempts */
! 68704: u.bg.v &= 0xffffff;
! 68705: }else{
! 68706: u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
! 68707: }
! 68708: u.bg.v++; /* ensure non-zero */
! 68709: }
! 68710: if( rc==SQLITE_OK && u.bg.res==0 ){
! 68711: rc = SQLITE_FULL; /* IMP: R-38219-53002 */
! 68712: goto abort_due_to_error;
! 68713: }
! 68714: assert( u.bg.v>0 ); /* EV: R-40812-03570 */
! 68715: }
! 68716: u.bg.pC->rowidIsValid = 0;
! 68717: u.bg.pC->deferredMoveto = 0;
! 68718: u.bg.pC->cacheStatus = CACHE_STALE;
! 68719: }
! 68720: pOut->u.i = u.bg.v;
! 68721: break;
! 68722: }
! 68723:
! 68724: /* Opcode: Insert P1 P2 P3 P4 P5
! 68725: **
! 68726: ** Write an entry into the table of cursor P1. A new entry is
! 68727: ** created if it doesn't already exist or the data for an existing
! 68728: ** entry is overwritten. The data is the value MEM_Blob stored in register
! 68729: ** number P2. The key is stored in register P3. The key must
! 68730: ** be a MEM_Int.
! 68731: **
! 68732: ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
! 68733: ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
! 68734: ** then rowid is stored for subsequent return by the
! 68735: ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
! 68736: **
! 68737: ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
! 68738: ** the last seek operation (OP_NotExists) was a success, then this
! 68739: ** operation will not attempt to find the appropriate row before doing
! 68740: ** the insert but will instead overwrite the row that the cursor is
! 68741: ** currently pointing to. Presumably, the prior OP_NotExists opcode
! 68742: ** has already positioned the cursor correctly. This is an optimization
! 68743: ** that boosts performance by avoiding redundant seeks.
! 68744: **
! 68745: ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
! 68746: ** UPDATE operation. Otherwise (if the flag is clear) then this opcode
! 68747: ** is part of an INSERT operation. The difference is only important to
! 68748: ** the update hook.
! 68749: **
! 68750: ** Parameter P4 may point to a string containing the table-name, or
! 68751: ** may be NULL. If it is not NULL, then the update-hook
! 68752: ** (sqlite3.xUpdateCallback) is invoked following a successful insert.
! 68753: **
! 68754: ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
! 68755: ** allocated, then ownership of P2 is transferred to the pseudo-cursor
! 68756: ** and register P2 becomes ephemeral. If the cursor is changed, the
! 68757: ** value of register P2 will then change. Make sure this does not
! 68758: ** cause any problems.)
! 68759: **
! 68760: ** This instruction only works on tables. The equivalent instruction
! 68761: ** for indices is OP_IdxInsert.
! 68762: */
! 68763: /* Opcode: InsertInt P1 P2 P3 P4 P5
! 68764: **
! 68765: ** This works exactly like OP_Insert except that the key is the
! 68766: ** integer value P3, not the value of the integer stored in register P3.
! 68767: */
! 68768: case OP_Insert:
! 68769: case OP_InsertInt: {
! 68770: #if 0 /* local variables moved into u.bh */
! 68771: Mem *pData; /* MEM cell holding data for the record to be inserted */
! 68772: Mem *pKey; /* MEM cell holding key for the record */
! 68773: i64 iKey; /* The integer ROWID or key for the record to be inserted */
! 68774: VdbeCursor *pC; /* Cursor to table into which insert is written */
! 68775: int nZero; /* Number of zero-bytes to append */
! 68776: int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
! 68777: const char *zDb; /* database name - used by the update hook */
! 68778: const char *zTbl; /* Table name - used by the opdate hook */
! 68779: int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
! 68780: #endif /* local variables moved into u.bh */
! 68781:
! 68782: u.bh.pData = &aMem[pOp->p2];
! 68783: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 68784: assert( memIsValid(u.bh.pData) );
! 68785: u.bh.pC = p->apCsr[pOp->p1];
! 68786: assert( u.bh.pC!=0 );
! 68787: assert( u.bh.pC->pCursor!=0 );
! 68788: assert( u.bh.pC->pseudoTableReg==0 );
! 68789: assert( u.bh.pC->isTable );
! 68790: REGISTER_TRACE(pOp->p2, u.bh.pData);
! 68791:
! 68792: if( pOp->opcode==OP_Insert ){
! 68793: u.bh.pKey = &aMem[pOp->p3];
! 68794: assert( u.bh.pKey->flags & MEM_Int );
! 68795: assert( memIsValid(u.bh.pKey) );
! 68796: REGISTER_TRACE(pOp->p3, u.bh.pKey);
! 68797: u.bh.iKey = u.bh.pKey->u.i;
! 68798: }else{
! 68799: assert( pOp->opcode==OP_InsertInt );
! 68800: u.bh.iKey = pOp->p3;
! 68801: }
! 68802:
! 68803: if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
! 68804: if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bh.iKey;
! 68805: if( u.bh.pData->flags & MEM_Null ){
! 68806: u.bh.pData->z = 0;
! 68807: u.bh.pData->n = 0;
! 68808: }else{
! 68809: assert( u.bh.pData->flags & (MEM_Blob|MEM_Str) );
! 68810: }
! 68811: u.bh.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bh.pC->seekResult : 0);
! 68812: if( u.bh.pData->flags & MEM_Zero ){
! 68813: u.bh.nZero = u.bh.pData->u.nZero;
! 68814: }else{
! 68815: u.bh.nZero = 0;
! 68816: }
! 68817: sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
! 68818: rc = sqlite3BtreeInsert(u.bh.pC->pCursor, 0, u.bh.iKey,
! 68819: u.bh.pData->z, u.bh.pData->n, u.bh.nZero,
! 68820: pOp->p5 & OPFLAG_APPEND, u.bh.seekResult
! 68821: );
! 68822: u.bh.pC->rowidIsValid = 0;
! 68823: u.bh.pC->deferredMoveto = 0;
! 68824: u.bh.pC->cacheStatus = CACHE_STALE;
! 68825:
! 68826: /* Invoke the update-hook if required. */
! 68827: if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
! 68828: u.bh.zDb = db->aDb[u.bh.pC->iDb].zName;
! 68829: u.bh.zTbl = pOp->p4.z;
! 68830: u.bh.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
! 68831: assert( u.bh.pC->isTable );
! 68832: db->xUpdateCallback(db->pUpdateArg, u.bh.op, u.bh.zDb, u.bh.zTbl, u.bh.iKey);
! 68833: assert( u.bh.pC->iDb>=0 );
! 68834: }
! 68835: break;
! 68836: }
! 68837:
! 68838: /* Opcode: Delete P1 P2 * P4 *
! 68839: **
! 68840: ** Delete the record at which the P1 cursor is currently pointing.
! 68841: **
! 68842: ** The cursor will be left pointing at either the next or the previous
! 68843: ** record in the table. If it is left pointing at the next record, then
! 68844: ** the next Next instruction will be a no-op. Hence it is OK to delete
! 68845: ** a record from within an Next loop.
! 68846: **
! 68847: ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
! 68848: ** incremented (otherwise not).
! 68849: **
! 68850: ** P1 must not be pseudo-table. It has to be a real table with
! 68851: ** multiple rows.
! 68852: **
! 68853: ** If P4 is not NULL, then it is the name of the table that P1 is
! 68854: ** pointing to. The update hook will be invoked, if it exists.
! 68855: ** If P4 is not NULL then the P1 cursor must have been positioned
! 68856: ** using OP_NotFound prior to invoking this opcode.
! 68857: */
! 68858: case OP_Delete: {
! 68859: #if 0 /* local variables moved into u.bi */
! 68860: i64 iKey;
! 68861: VdbeCursor *pC;
! 68862: #endif /* local variables moved into u.bi */
! 68863:
! 68864: u.bi.iKey = 0;
! 68865: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 68866: u.bi.pC = p->apCsr[pOp->p1];
! 68867: assert( u.bi.pC!=0 );
! 68868: assert( u.bi.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
! 68869:
! 68870: /* If the update-hook will be invoked, set u.bi.iKey to the rowid of the
! 68871: ** row being deleted.
! 68872: */
! 68873: if( db->xUpdateCallback && pOp->p4.z ){
! 68874: assert( u.bi.pC->isTable );
! 68875: assert( u.bi.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
! 68876: u.bi.iKey = u.bi.pC->lastRowid;
! 68877: }
! 68878:
! 68879: /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
! 68880: ** OP_Column on the same table without any intervening operations that
! 68881: ** might move or invalidate the cursor. Hence cursor u.bi.pC is always pointing
! 68882: ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
! 68883: ** below is always a no-op and cannot fail. We will run it anyhow, though,
! 68884: ** to guard against future changes to the code generator.
! 68885: **/
! 68886: assert( u.bi.pC->deferredMoveto==0 );
! 68887: rc = sqlite3VdbeCursorMoveto(u.bi.pC);
! 68888: if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
! 68889:
! 68890: sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
! 68891: rc = sqlite3BtreeDelete(u.bi.pC->pCursor);
! 68892: u.bi.pC->cacheStatus = CACHE_STALE;
! 68893:
! 68894: /* Invoke the update-hook if required. */
! 68895: if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
! 68896: const char *zDb = db->aDb[u.bi.pC->iDb].zName;
! 68897: const char *zTbl = pOp->p4.z;
! 68898: db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bi.iKey);
! 68899: assert( u.bi.pC->iDb>=0 );
! 68900: }
! 68901: if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
! 68902: break;
! 68903: }
! 68904: /* Opcode: ResetCount * * * * *
! 68905: **
! 68906: ** The value of the change counter is copied to the database handle
! 68907: ** change counter (returned by subsequent calls to sqlite3_changes()).
! 68908: ** Then the VMs internal change counter resets to 0.
! 68909: ** This is used by trigger programs.
! 68910: */
! 68911: case OP_ResetCount: {
! 68912: sqlite3VdbeSetChanges(db, p->nChange);
! 68913: p->nChange = 0;
! 68914: break;
! 68915: }
! 68916:
! 68917: /* Opcode: SorterCompare P1 P2 P3
! 68918: **
! 68919: ** P1 is a sorter cursor. This instruction compares the record blob in
! 68920: ** register P3 with the entry that the sorter cursor currently points to.
! 68921: ** If, excluding the rowid fields at the end, the two records are a match,
! 68922: ** fall through to the next instruction. Otherwise, jump to instruction P2.
! 68923: */
! 68924: case OP_SorterCompare: {
! 68925: #if 0 /* local variables moved into u.bj */
! 68926: VdbeCursor *pC;
! 68927: int res;
! 68928: #endif /* local variables moved into u.bj */
! 68929:
! 68930: u.bj.pC = p->apCsr[pOp->p1];
! 68931: assert( isSorter(u.bj.pC) );
! 68932: pIn3 = &aMem[pOp->p3];
! 68933: rc = sqlite3VdbeSorterCompare(u.bj.pC, pIn3, &u.bj.res);
! 68934: if( u.bj.res ){
! 68935: pc = pOp->p2-1;
! 68936: }
! 68937: break;
! 68938: };
! 68939:
! 68940: /* Opcode: SorterData P1 P2 * * *
! 68941: **
! 68942: ** Write into register P2 the current sorter data for sorter cursor P1.
! 68943: */
! 68944: case OP_SorterData: {
! 68945: #if 0 /* local variables moved into u.bk */
! 68946: VdbeCursor *pC;
! 68947: #endif /* local variables moved into u.bk */
! 68948: #ifndef SQLITE_OMIT_MERGE_SORT
! 68949: pOut = &aMem[pOp->p2];
! 68950: u.bk.pC = p->apCsr[pOp->p1];
! 68951: assert( u.bk.pC->isSorter );
! 68952: rc = sqlite3VdbeSorterRowkey(u.bk.pC, pOut);
! 68953: #else
! 68954: pOp->opcode = OP_RowKey;
! 68955: pc--;
! 68956: #endif
! 68957: break;
! 68958: }
! 68959:
! 68960: /* Opcode: RowData P1 P2 * * *
! 68961: **
! 68962: ** Write into register P2 the complete row data for cursor P1.
! 68963: ** There is no interpretation of the data.
! 68964: ** It is just copied onto the P2 register exactly as
! 68965: ** it is found in the database file.
! 68966: **
! 68967: ** If the P1 cursor must be pointing to a valid row (not a NULL row)
! 68968: ** of a real table, not a pseudo-table.
! 68969: */
! 68970: /* Opcode: RowKey P1 P2 * * *
! 68971: **
! 68972: ** Write into register P2 the complete row key for cursor P1.
! 68973: ** There is no interpretation of the data.
! 68974: ** The key is copied onto the P3 register exactly as
! 68975: ** it is found in the database file.
! 68976: **
! 68977: ** If the P1 cursor must be pointing to a valid row (not a NULL row)
! 68978: ** of a real table, not a pseudo-table.
! 68979: */
! 68980: case OP_RowKey:
! 68981: case OP_RowData: {
! 68982: #if 0 /* local variables moved into u.bl */
! 68983: VdbeCursor *pC;
! 68984: BtCursor *pCrsr;
! 68985: u32 n;
! 68986: i64 n64;
! 68987: #endif /* local variables moved into u.bl */
! 68988:
! 68989: pOut = &aMem[pOp->p2];
! 68990: memAboutToChange(p, pOut);
! 68991:
! 68992: /* Note that RowKey and RowData are really exactly the same instruction */
! 68993: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 68994: u.bl.pC = p->apCsr[pOp->p1];
! 68995: assert( u.bl.pC->isSorter==0 );
! 68996: assert( u.bl.pC->isTable || pOp->opcode!=OP_RowData );
! 68997: assert( u.bl.pC->isIndex || pOp->opcode==OP_RowData );
! 68998: assert( u.bl.pC!=0 );
! 68999: assert( u.bl.pC->nullRow==0 );
! 69000: assert( u.bl.pC->pseudoTableReg==0 );
! 69001: assert( !u.bl.pC->isSorter );
! 69002: assert( u.bl.pC->pCursor!=0 );
! 69003: u.bl.pCrsr = u.bl.pC->pCursor;
! 69004: assert( sqlite3BtreeCursorIsValid(u.bl.pCrsr) );
! 69005:
! 69006: /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
! 69007: ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
! 69008: ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
! 69009: ** a no-op and can never fail. But we leave it in place as a safety.
! 69010: */
! 69011: assert( u.bl.pC->deferredMoveto==0 );
! 69012: rc = sqlite3VdbeCursorMoveto(u.bl.pC);
! 69013: if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
! 69014:
! 69015: if( u.bl.pC->isIndex ){
! 69016: assert( !u.bl.pC->isTable );
! 69017: VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bl.pCrsr, &u.bl.n64);
! 69018: assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
! 69019: if( u.bl.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
! 69020: goto too_big;
! 69021: }
! 69022: u.bl.n = (u32)u.bl.n64;
! 69023: }else{
! 69024: VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bl.pCrsr, &u.bl.n);
! 69025: assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
! 69026: if( u.bl.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
! 69027: goto too_big;
! 69028: }
! 69029: }
! 69030: if( sqlite3VdbeMemGrow(pOut, u.bl.n, 0) ){
! 69031: goto no_mem;
! 69032: }
! 69033: pOut->n = u.bl.n;
! 69034: MemSetTypeFlag(pOut, MEM_Blob);
! 69035: if( u.bl.pC->isIndex ){
! 69036: rc = sqlite3BtreeKey(u.bl.pCrsr, 0, u.bl.n, pOut->z);
! 69037: }else{
! 69038: rc = sqlite3BtreeData(u.bl.pCrsr, 0, u.bl.n, pOut->z);
! 69039: }
! 69040: pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
! 69041: UPDATE_MAX_BLOBSIZE(pOut);
! 69042: break;
! 69043: }
! 69044:
! 69045: /* Opcode: Rowid P1 P2 * * *
! 69046: **
! 69047: ** Store in register P2 an integer which is the key of the table entry that
! 69048: ** P1 is currently point to.
! 69049: **
! 69050: ** P1 can be either an ordinary table or a virtual table. There used to
! 69051: ** be a separate OP_VRowid opcode for use with virtual tables, but this
! 69052: ** one opcode now works for both table types.
! 69053: */
! 69054: case OP_Rowid: { /* out2-prerelease */
! 69055: #if 0 /* local variables moved into u.bm */
! 69056: VdbeCursor *pC;
! 69057: i64 v;
! 69058: sqlite3_vtab *pVtab;
! 69059: const sqlite3_module *pModule;
! 69060: #endif /* local variables moved into u.bm */
! 69061:
! 69062: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 69063: u.bm.pC = p->apCsr[pOp->p1];
! 69064: assert( u.bm.pC!=0 );
! 69065: assert( u.bm.pC->pseudoTableReg==0 );
! 69066: if( u.bm.pC->nullRow ){
! 69067: pOut->flags = MEM_Null;
! 69068: break;
! 69069: }else if( u.bm.pC->deferredMoveto ){
! 69070: u.bm.v = u.bm.pC->movetoTarget;
! 69071: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 69072: }else if( u.bm.pC->pVtabCursor ){
! 69073: u.bm.pVtab = u.bm.pC->pVtabCursor->pVtab;
! 69074: u.bm.pModule = u.bm.pVtab->pModule;
! 69075: assert( u.bm.pModule->xRowid );
! 69076: rc = u.bm.pModule->xRowid(u.bm.pC->pVtabCursor, &u.bm.v);
! 69077: importVtabErrMsg(p, u.bm.pVtab);
! 69078: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 69079: }else{
! 69080: assert( u.bm.pC->pCursor!=0 );
! 69081: rc = sqlite3VdbeCursorMoveto(u.bm.pC);
! 69082: if( rc ) goto abort_due_to_error;
! 69083: if( u.bm.pC->rowidIsValid ){
! 69084: u.bm.v = u.bm.pC->lastRowid;
! 69085: }else{
! 69086: rc = sqlite3BtreeKeySize(u.bm.pC->pCursor, &u.bm.v);
! 69087: assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
! 69088: }
! 69089: }
! 69090: pOut->u.i = u.bm.v;
! 69091: break;
! 69092: }
! 69093:
! 69094: /* Opcode: NullRow P1 * * * *
! 69095: **
! 69096: ** Move the cursor P1 to a null row. Any OP_Column operations
! 69097: ** that occur while the cursor is on the null row will always
! 69098: ** write a NULL.
! 69099: */
! 69100: case OP_NullRow: {
! 69101: #if 0 /* local variables moved into u.bn */
! 69102: VdbeCursor *pC;
! 69103: #endif /* local variables moved into u.bn */
! 69104:
! 69105: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 69106: u.bn.pC = p->apCsr[pOp->p1];
! 69107: assert( u.bn.pC!=0 );
! 69108: u.bn.pC->nullRow = 1;
! 69109: u.bn.pC->rowidIsValid = 0;
! 69110: assert( u.bn.pC->pCursor || u.bn.pC->pVtabCursor );
! 69111: if( u.bn.pC->pCursor ){
! 69112: sqlite3BtreeClearCursor(u.bn.pC->pCursor);
! 69113: }
! 69114: break;
! 69115: }
! 69116:
! 69117: /* Opcode: Last P1 P2 * * *
! 69118: **
! 69119: ** The next use of the Rowid or Column or Next instruction for P1
! 69120: ** will refer to the last entry in the database table or index.
! 69121: ** If the table or index is empty and P2>0, then jump immediately to P2.
! 69122: ** If P2 is 0 or if the table or index is not empty, fall through
! 69123: ** to the following instruction.
! 69124: */
! 69125: case OP_Last: { /* jump */
! 69126: #if 0 /* local variables moved into u.bo */
! 69127: VdbeCursor *pC;
! 69128: BtCursor *pCrsr;
! 69129: int res;
! 69130: #endif /* local variables moved into u.bo */
! 69131:
! 69132: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 69133: u.bo.pC = p->apCsr[pOp->p1];
! 69134: assert( u.bo.pC!=0 );
! 69135: u.bo.pCrsr = u.bo.pC->pCursor;
! 69136: u.bo.res = 0;
! 69137: if( ALWAYS(u.bo.pCrsr!=0) ){
! 69138: rc = sqlite3BtreeLast(u.bo.pCrsr, &u.bo.res);
! 69139: }
! 69140: u.bo.pC->nullRow = (u8)u.bo.res;
! 69141: u.bo.pC->deferredMoveto = 0;
! 69142: u.bo.pC->rowidIsValid = 0;
! 69143: u.bo.pC->cacheStatus = CACHE_STALE;
! 69144: if( pOp->p2>0 && u.bo.res ){
! 69145: pc = pOp->p2 - 1;
! 69146: }
! 69147: break;
! 69148: }
! 69149:
! 69150:
! 69151: /* Opcode: Sort P1 P2 * * *
! 69152: **
! 69153: ** This opcode does exactly the same thing as OP_Rewind except that
! 69154: ** it increments an undocumented global variable used for testing.
! 69155: **
! 69156: ** Sorting is accomplished by writing records into a sorting index,
! 69157: ** then rewinding that index and playing it back from beginning to
! 69158: ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
! 69159: ** rewinding so that the global variable will be incremented and
! 69160: ** regression tests can determine whether or not the optimizer is
! 69161: ** correctly optimizing out sorts.
! 69162: */
! 69163: case OP_SorterSort: /* jump */
! 69164: #ifdef SQLITE_OMIT_MERGE_SORT
! 69165: pOp->opcode = OP_Sort;
! 69166: #endif
! 69167: case OP_Sort: { /* jump */
! 69168: #ifdef SQLITE_TEST
! 69169: sqlite3_sort_count++;
! 69170: sqlite3_search_count--;
! 69171: #endif
! 69172: p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
! 69173: /* Fall through into OP_Rewind */
! 69174: }
! 69175: /* Opcode: Rewind P1 P2 * * *
! 69176: **
! 69177: ** The next use of the Rowid or Column or Next instruction for P1
! 69178: ** will refer to the first entry in the database table or index.
! 69179: ** If the table or index is empty and P2>0, then jump immediately to P2.
! 69180: ** If P2 is 0 or if the table or index is not empty, fall through
! 69181: ** to the following instruction.
! 69182: */
! 69183: case OP_Rewind: { /* jump */
! 69184: #if 0 /* local variables moved into u.bp */
! 69185: VdbeCursor *pC;
! 69186: BtCursor *pCrsr;
! 69187: int res;
! 69188: #endif /* local variables moved into u.bp */
! 69189:
! 69190: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 69191: u.bp.pC = p->apCsr[pOp->p1];
! 69192: assert( u.bp.pC!=0 );
! 69193: assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterSort) );
! 69194: u.bp.res = 1;
! 69195: if( isSorter(u.bp.pC) ){
! 69196: rc = sqlite3VdbeSorterRewind(db, u.bp.pC, &u.bp.res);
! 69197: }else{
! 69198: u.bp.pCrsr = u.bp.pC->pCursor;
! 69199: assert( u.bp.pCrsr );
! 69200: rc = sqlite3BtreeFirst(u.bp.pCrsr, &u.bp.res);
! 69201: u.bp.pC->atFirst = u.bp.res==0 ?1:0;
! 69202: u.bp.pC->deferredMoveto = 0;
! 69203: u.bp.pC->cacheStatus = CACHE_STALE;
! 69204: u.bp.pC->rowidIsValid = 0;
! 69205: }
! 69206: u.bp.pC->nullRow = (u8)u.bp.res;
! 69207: assert( pOp->p2>0 && pOp->p2<p->nOp );
! 69208: if( u.bp.res ){
! 69209: pc = pOp->p2 - 1;
! 69210: }
! 69211: break;
! 69212: }
! 69213:
! 69214: /* Opcode: Next P1 P2 * P4 P5
! 69215: **
! 69216: ** Advance cursor P1 so that it points to the next key/data pair in its
! 69217: ** table or index. If there are no more key/value pairs then fall through
! 69218: ** to the following instruction. But if the cursor advance was successful,
! 69219: ** jump immediately to P2.
! 69220: **
! 69221: ** The P1 cursor must be for a real table, not a pseudo-table.
! 69222: **
! 69223: ** P4 is always of type P4_ADVANCE. The function pointer points to
! 69224: ** sqlite3BtreeNext().
! 69225: **
! 69226: ** If P5 is positive and the jump is taken, then event counter
! 69227: ** number P5-1 in the prepared statement is incremented.
! 69228: **
! 69229: ** See also: Prev
! 69230: */
! 69231: /* Opcode: Prev P1 P2 * * P5
! 69232: **
! 69233: ** Back up cursor P1 so that it points to the previous key/data pair in its
! 69234: ** table or index. If there is no previous key/value pairs then fall through
! 69235: ** to the following instruction. But if the cursor backup was successful,
! 69236: ** jump immediately to P2.
! 69237: **
! 69238: ** The P1 cursor must be for a real table, not a pseudo-table.
! 69239: **
! 69240: ** P4 is always of type P4_ADVANCE. The function pointer points to
! 69241: ** sqlite3BtreePrevious().
! 69242: **
! 69243: ** If P5 is positive and the jump is taken, then event counter
! 69244: ** number P5-1 in the prepared statement is incremented.
! 69245: */
! 69246: case OP_SorterNext: /* jump */
! 69247: #ifdef SQLITE_OMIT_MERGE_SORT
! 69248: pOp->opcode = OP_Next;
! 69249: #endif
! 69250: case OP_Prev: /* jump */
! 69251: case OP_Next: { /* jump */
! 69252: #if 0 /* local variables moved into u.bq */
! 69253: VdbeCursor *pC;
! 69254: int res;
! 69255: #endif /* local variables moved into u.bq */
! 69256:
! 69257: CHECK_FOR_INTERRUPT;
! 69258: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 69259: assert( pOp->p5<=ArraySize(p->aCounter) );
! 69260: u.bq.pC = p->apCsr[pOp->p1];
! 69261: if( u.bq.pC==0 ){
! 69262: break; /* See ticket #2273 */
! 69263: }
! 69264: assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterNext) );
! 69265: if( isSorter(u.bq.pC) ){
! 69266: assert( pOp->opcode==OP_SorterNext );
! 69267: rc = sqlite3VdbeSorterNext(db, u.bq.pC, &u.bq.res);
! 69268: }else{
! 69269: u.bq.res = 1;
! 69270: assert( u.bq.pC->deferredMoveto==0 );
! 69271: assert( u.bq.pC->pCursor );
! 69272: assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
! 69273: assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
! 69274: rc = pOp->p4.xAdvance(u.bq.pC->pCursor, &u.bq.res);
! 69275: }
! 69276: u.bq.pC->nullRow = (u8)u.bq.res;
! 69277: u.bq.pC->cacheStatus = CACHE_STALE;
! 69278: if( u.bq.res==0 ){
! 69279: pc = pOp->p2 - 1;
! 69280: if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
! 69281: #ifdef SQLITE_TEST
! 69282: sqlite3_search_count++;
! 69283: #endif
! 69284: }
! 69285: u.bq.pC->rowidIsValid = 0;
! 69286: break;
! 69287: }
! 69288:
! 69289: /* Opcode: IdxInsert P1 P2 P3 * P5
! 69290: **
! 69291: ** Register P2 holds an SQL index key made using the
! 69292: ** MakeRecord instructions. This opcode writes that key
! 69293: ** into the index P1. Data for the entry is nil.
! 69294: **
! 69295: ** P3 is a flag that provides a hint to the b-tree layer that this
! 69296: ** insert is likely to be an append.
! 69297: **
! 69298: ** This instruction only works for indices. The equivalent instruction
! 69299: ** for tables is OP_Insert.
! 69300: */
! 69301: case OP_SorterInsert: /* in2 */
! 69302: #ifdef SQLITE_OMIT_MERGE_SORT
! 69303: pOp->opcode = OP_IdxInsert;
! 69304: #endif
! 69305: case OP_IdxInsert: { /* in2 */
! 69306: #if 0 /* local variables moved into u.br */
! 69307: VdbeCursor *pC;
! 69308: BtCursor *pCrsr;
! 69309: int nKey;
! 69310: const char *zKey;
! 69311: #endif /* local variables moved into u.br */
! 69312:
! 69313: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 69314: u.br.pC = p->apCsr[pOp->p1];
! 69315: assert( u.br.pC!=0 );
! 69316: assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
! 69317: pIn2 = &aMem[pOp->p2];
! 69318: assert( pIn2->flags & MEM_Blob );
! 69319: u.br.pCrsr = u.br.pC->pCursor;
! 69320: if( ALWAYS(u.br.pCrsr!=0) ){
! 69321: assert( u.br.pC->isTable==0 );
! 69322: rc = ExpandBlob(pIn2);
! 69323: if( rc==SQLITE_OK ){
! 69324: if( isSorter(u.br.pC) ){
! 69325: rc = sqlite3VdbeSorterWrite(db, u.br.pC, pIn2);
! 69326: }else{
! 69327: u.br.nKey = pIn2->n;
! 69328: u.br.zKey = pIn2->z;
! 69329: rc = sqlite3BtreeInsert(u.br.pCrsr, u.br.zKey, u.br.nKey, "", 0, 0, pOp->p3,
! 69330: ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.br.pC->seekResult : 0)
! 69331: );
! 69332: assert( u.br.pC->deferredMoveto==0 );
! 69333: u.br.pC->cacheStatus = CACHE_STALE;
! 69334: }
! 69335: }
! 69336: }
! 69337: break;
! 69338: }
! 69339:
! 69340: /* Opcode: IdxDelete P1 P2 P3 * *
! 69341: **
! 69342: ** The content of P3 registers starting at register P2 form
! 69343: ** an unpacked index key. This opcode removes that entry from the
! 69344: ** index opened by cursor P1.
! 69345: */
! 69346: case OP_IdxDelete: {
! 69347: #if 0 /* local variables moved into u.bs */
! 69348: VdbeCursor *pC;
! 69349: BtCursor *pCrsr;
! 69350: int res;
! 69351: UnpackedRecord r;
! 69352: #endif /* local variables moved into u.bs */
! 69353:
! 69354: assert( pOp->p3>0 );
! 69355: assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
! 69356: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 69357: u.bs.pC = p->apCsr[pOp->p1];
! 69358: assert( u.bs.pC!=0 );
! 69359: u.bs.pCrsr = u.bs.pC->pCursor;
! 69360: if( ALWAYS(u.bs.pCrsr!=0) ){
! 69361: u.bs.r.pKeyInfo = u.bs.pC->pKeyInfo;
! 69362: u.bs.r.nField = (u16)pOp->p3;
! 69363: u.bs.r.flags = 0;
! 69364: u.bs.r.aMem = &aMem[pOp->p2];
! 69365: #ifdef SQLITE_DEBUG
! 69366: { int i; for(i=0; i<u.bs.r.nField; i++) assert( memIsValid(&u.bs.r.aMem[i]) ); }
! 69367: #endif
! 69368: rc = sqlite3BtreeMovetoUnpacked(u.bs.pCrsr, &u.bs.r, 0, 0, &u.bs.res);
! 69369: if( rc==SQLITE_OK && u.bs.res==0 ){
! 69370: rc = sqlite3BtreeDelete(u.bs.pCrsr);
! 69371: }
! 69372: assert( u.bs.pC->deferredMoveto==0 );
! 69373: u.bs.pC->cacheStatus = CACHE_STALE;
! 69374: }
! 69375: break;
! 69376: }
! 69377:
! 69378: /* Opcode: IdxRowid P1 P2 * * *
! 69379: **
! 69380: ** Write into register P2 an integer which is the last entry in the record at
! 69381: ** the end of the index key pointed to by cursor P1. This integer should be
! 69382: ** the rowid of the table entry to which this index entry points.
! 69383: **
! 69384: ** See also: Rowid, MakeRecord.
! 69385: */
! 69386: case OP_IdxRowid: { /* out2-prerelease */
! 69387: #if 0 /* local variables moved into u.bt */
! 69388: BtCursor *pCrsr;
! 69389: VdbeCursor *pC;
! 69390: i64 rowid;
! 69391: #endif /* local variables moved into u.bt */
! 69392:
! 69393: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 69394: u.bt.pC = p->apCsr[pOp->p1];
! 69395: assert( u.bt.pC!=0 );
! 69396: u.bt.pCrsr = u.bt.pC->pCursor;
! 69397: pOut->flags = MEM_Null;
! 69398: if( ALWAYS(u.bt.pCrsr!=0) ){
! 69399: rc = sqlite3VdbeCursorMoveto(u.bt.pC);
! 69400: if( NEVER(rc) ) goto abort_due_to_error;
! 69401: assert( u.bt.pC->deferredMoveto==0 );
! 69402: assert( u.bt.pC->isTable==0 );
! 69403: if( !u.bt.pC->nullRow ){
! 69404: rc = sqlite3VdbeIdxRowid(db, u.bt.pCrsr, &u.bt.rowid);
! 69405: if( rc!=SQLITE_OK ){
! 69406: goto abort_due_to_error;
! 69407: }
! 69408: pOut->u.i = u.bt.rowid;
! 69409: pOut->flags = MEM_Int;
! 69410: }
! 69411: }
! 69412: break;
! 69413: }
! 69414:
! 69415: /* Opcode: IdxGE P1 P2 P3 P4 P5
! 69416: **
! 69417: ** The P4 register values beginning with P3 form an unpacked index
! 69418: ** key that omits the ROWID. Compare this key value against the index
! 69419: ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
! 69420: **
! 69421: ** If the P1 index entry is greater than or equal to the key value
! 69422: ** then jump to P2. Otherwise fall through to the next instruction.
! 69423: **
! 69424: ** If P5 is non-zero then the key value is increased by an epsilon
! 69425: ** prior to the comparison. This make the opcode work like IdxGT except
! 69426: ** that if the key from register P3 is a prefix of the key in the cursor,
! 69427: ** the result is false whereas it would be true with IdxGT.
! 69428: */
! 69429: /* Opcode: IdxLT P1 P2 P3 P4 P5
! 69430: **
! 69431: ** The P4 register values beginning with P3 form an unpacked index
! 69432: ** key that omits the ROWID. Compare this key value against the index
! 69433: ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
! 69434: **
! 69435: ** If the P1 index entry is less than the key value then jump to P2.
! 69436: ** Otherwise fall through to the next instruction.
! 69437: **
! 69438: ** If P5 is non-zero then the key value is increased by an epsilon prior
! 69439: ** to the comparison. This makes the opcode work like IdxLE.
! 69440: */
! 69441: case OP_IdxLT: /* jump */
! 69442: case OP_IdxGE: { /* jump */
! 69443: #if 0 /* local variables moved into u.bu */
! 69444: VdbeCursor *pC;
! 69445: int res;
! 69446: UnpackedRecord r;
! 69447: #endif /* local variables moved into u.bu */
! 69448:
! 69449: assert( pOp->p1>=0 && pOp->p1<p->nCursor );
! 69450: u.bu.pC = p->apCsr[pOp->p1];
! 69451: assert( u.bu.pC!=0 );
! 69452: assert( u.bu.pC->isOrdered );
! 69453: if( ALWAYS(u.bu.pC->pCursor!=0) ){
! 69454: assert( u.bu.pC->deferredMoveto==0 );
! 69455: assert( pOp->p5==0 || pOp->p5==1 );
! 69456: assert( pOp->p4type==P4_INT32 );
! 69457: u.bu.r.pKeyInfo = u.bu.pC->pKeyInfo;
! 69458: u.bu.r.nField = (u16)pOp->p4.i;
! 69459: if( pOp->p5 ){
! 69460: u.bu.r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
! 69461: }else{
! 69462: u.bu.r.flags = UNPACKED_PREFIX_MATCH;
! 69463: }
! 69464: u.bu.r.aMem = &aMem[pOp->p3];
! 69465: #ifdef SQLITE_DEBUG
! 69466: { int i; for(i=0; i<u.bu.r.nField; i++) assert( memIsValid(&u.bu.r.aMem[i]) ); }
! 69467: #endif
! 69468: rc = sqlite3VdbeIdxKeyCompare(u.bu.pC, &u.bu.r, &u.bu.res);
! 69469: if( pOp->opcode==OP_IdxLT ){
! 69470: u.bu.res = -u.bu.res;
! 69471: }else{
! 69472: assert( pOp->opcode==OP_IdxGE );
! 69473: u.bu.res++;
! 69474: }
! 69475: if( u.bu.res>0 ){
! 69476: pc = pOp->p2 - 1 ;
! 69477: }
! 69478: }
! 69479: break;
! 69480: }
! 69481:
! 69482: /* Opcode: Destroy P1 P2 P3 * *
! 69483: **
! 69484: ** Delete an entire database table or index whose root page in the database
! 69485: ** file is given by P1.
! 69486: **
! 69487: ** The table being destroyed is in the main database file if P3==0. If
! 69488: ** P3==1 then the table to be clear is in the auxiliary database file
! 69489: ** that is used to store tables create using CREATE TEMPORARY TABLE.
! 69490: **
! 69491: ** If AUTOVACUUM is enabled then it is possible that another root page
! 69492: ** might be moved into the newly deleted root page in order to keep all
! 69493: ** root pages contiguous at the beginning of the database. The former
! 69494: ** value of the root page that moved - its value before the move occurred -
! 69495: ** is stored in register P2. If no page
! 69496: ** movement was required (because the table being dropped was already
! 69497: ** the last one in the database) then a zero is stored in register P2.
! 69498: ** If AUTOVACUUM is disabled then a zero is stored in register P2.
! 69499: **
! 69500: ** See also: Clear
! 69501: */
! 69502: case OP_Destroy: { /* out2-prerelease */
! 69503: #if 0 /* local variables moved into u.bv */
! 69504: int iMoved;
! 69505: int iCnt;
! 69506: Vdbe *pVdbe;
! 69507: int iDb;
! 69508: #endif /* local variables moved into u.bv */
! 69509: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 69510: u.bv.iCnt = 0;
! 69511: for(u.bv.pVdbe=db->pVdbe; u.bv.pVdbe; u.bv.pVdbe = u.bv.pVdbe->pNext){
! 69512: if( u.bv.pVdbe->magic==VDBE_MAGIC_RUN && u.bv.pVdbe->inVtabMethod<2 && u.bv.pVdbe->pc>=0 ){
! 69513: u.bv.iCnt++;
! 69514: }
! 69515: }
! 69516: #else
! 69517: u.bv.iCnt = db->activeVdbeCnt;
! 69518: #endif
! 69519: pOut->flags = MEM_Null;
! 69520: if( u.bv.iCnt>1 ){
! 69521: rc = SQLITE_LOCKED;
! 69522: p->errorAction = OE_Abort;
! 69523: }else{
! 69524: u.bv.iDb = pOp->p3;
! 69525: assert( u.bv.iCnt==1 );
! 69526: assert( (p->btreeMask & (((yDbMask)1)<<u.bv.iDb))!=0 );
! 69527: rc = sqlite3BtreeDropTable(db->aDb[u.bv.iDb].pBt, pOp->p1, &u.bv.iMoved);
! 69528: pOut->flags = MEM_Int;
! 69529: pOut->u.i = u.bv.iMoved;
! 69530: #ifndef SQLITE_OMIT_AUTOVACUUM
! 69531: if( rc==SQLITE_OK && u.bv.iMoved!=0 ){
! 69532: sqlite3RootPageMoved(db, u.bv.iDb, u.bv.iMoved, pOp->p1);
! 69533: /* All OP_Destroy operations occur on the same btree */
! 69534: assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bv.iDb+1 );
! 69535: resetSchemaOnFault = u.bv.iDb+1;
! 69536: }
! 69537: #endif
! 69538: }
! 69539: break;
! 69540: }
! 69541:
! 69542: /* Opcode: Clear P1 P2 P3
! 69543: **
! 69544: ** Delete all contents of the database table or index whose root page
! 69545: ** in the database file is given by P1. But, unlike Destroy, do not
! 69546: ** remove the table or index from the database file.
! 69547: **
! 69548: ** The table being clear is in the main database file if P2==0. If
! 69549: ** P2==1 then the table to be clear is in the auxiliary database file
! 69550: ** that is used to store tables create using CREATE TEMPORARY TABLE.
! 69551: **
! 69552: ** If the P3 value is non-zero, then the table referred to must be an
! 69553: ** intkey table (an SQL table, not an index). In this case the row change
! 69554: ** count is incremented by the number of rows in the table being cleared.
! 69555: ** If P3 is greater than zero, then the value stored in register P3 is
! 69556: ** also incremented by the number of rows in the table being cleared.
! 69557: **
! 69558: ** See also: Destroy
! 69559: */
! 69560: case OP_Clear: {
! 69561: #if 0 /* local variables moved into u.bw */
! 69562: int nChange;
! 69563: #endif /* local variables moved into u.bw */
! 69564:
! 69565: u.bw.nChange = 0;
! 69566: assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
! 69567: rc = sqlite3BtreeClearTable(
! 69568: db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bw.nChange : 0)
! 69569: );
! 69570: if( pOp->p3 ){
! 69571: p->nChange += u.bw.nChange;
! 69572: if( pOp->p3>0 ){
! 69573: assert( memIsValid(&aMem[pOp->p3]) );
! 69574: memAboutToChange(p, &aMem[pOp->p3]);
! 69575: aMem[pOp->p3].u.i += u.bw.nChange;
! 69576: }
! 69577: }
! 69578: break;
! 69579: }
! 69580:
! 69581: /* Opcode: CreateTable P1 P2 * * *
! 69582: **
! 69583: ** Allocate a new table in the main database file if P1==0 or in the
! 69584: ** auxiliary database file if P1==1 or in an attached database if
! 69585: ** P1>1. Write the root page number of the new table into
! 69586: ** register P2
! 69587: **
! 69588: ** The difference between a table and an index is this: A table must
! 69589: ** have a 4-byte integer key and can have arbitrary data. An index
! 69590: ** has an arbitrary key but no data.
! 69591: **
! 69592: ** See also: CreateIndex
! 69593: */
! 69594: /* Opcode: CreateIndex P1 P2 * * *
! 69595: **
! 69596: ** Allocate a new index in the main database file if P1==0 or in the
! 69597: ** auxiliary database file if P1==1 or in an attached database if
! 69598: ** P1>1. Write the root page number of the new table into
! 69599: ** register P2.
! 69600: **
! 69601: ** See documentation on OP_CreateTable for additional information.
! 69602: */
! 69603: case OP_CreateIndex: /* out2-prerelease */
! 69604: case OP_CreateTable: { /* out2-prerelease */
! 69605: #if 0 /* local variables moved into u.bx */
! 69606: int pgno;
! 69607: int flags;
! 69608: Db *pDb;
! 69609: #endif /* local variables moved into u.bx */
! 69610:
! 69611: u.bx.pgno = 0;
! 69612: assert( pOp->p1>=0 && pOp->p1<db->nDb );
! 69613: assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
! 69614: u.bx.pDb = &db->aDb[pOp->p1];
! 69615: assert( u.bx.pDb->pBt!=0 );
! 69616: if( pOp->opcode==OP_CreateTable ){
! 69617: /* u.bx.flags = BTREE_INTKEY; */
! 69618: u.bx.flags = BTREE_INTKEY;
! 69619: }else{
! 69620: u.bx.flags = BTREE_BLOBKEY;
! 69621: }
! 69622: rc = sqlite3BtreeCreateTable(u.bx.pDb->pBt, &u.bx.pgno, u.bx.flags);
! 69623: pOut->u.i = u.bx.pgno;
! 69624: break;
! 69625: }
! 69626:
! 69627: /* Opcode: ParseSchema P1 * * P4 *
! 69628: **
! 69629: ** Read and parse all entries from the SQLITE_MASTER table of database P1
! 69630: ** that match the WHERE clause P4.
! 69631: **
! 69632: ** This opcode invokes the parser to create a new virtual machine,
! 69633: ** then runs the new virtual machine. It is thus a re-entrant opcode.
! 69634: */
! 69635: case OP_ParseSchema: {
! 69636: #if 0 /* local variables moved into u.by */
! 69637: int iDb;
! 69638: const char *zMaster;
! 69639: char *zSql;
! 69640: InitData initData;
! 69641: #endif /* local variables moved into u.by */
! 69642:
! 69643: /* Any prepared statement that invokes this opcode will hold mutexes
! 69644: ** on every btree. This is a prerequisite for invoking
! 69645: ** sqlite3InitCallback().
! 69646: */
! 69647: #ifdef SQLITE_DEBUG
! 69648: for(u.by.iDb=0; u.by.iDb<db->nDb; u.by.iDb++){
! 69649: assert( u.by.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.by.iDb].pBt) );
! 69650: }
! 69651: #endif
! 69652:
! 69653: u.by.iDb = pOp->p1;
! 69654: assert( u.by.iDb>=0 && u.by.iDb<db->nDb );
! 69655: assert( DbHasProperty(db, u.by.iDb, DB_SchemaLoaded) );
! 69656: /* Used to be a conditional */ {
! 69657: u.by.zMaster = SCHEMA_TABLE(u.by.iDb);
! 69658: u.by.initData.db = db;
! 69659: u.by.initData.iDb = pOp->p1;
! 69660: u.by.initData.pzErrMsg = &p->zErrMsg;
! 69661: u.by.zSql = sqlite3MPrintf(db,
! 69662: "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
! 69663: db->aDb[u.by.iDb].zName, u.by.zMaster, pOp->p4.z);
! 69664: if( u.by.zSql==0 ){
! 69665: rc = SQLITE_NOMEM;
! 69666: }else{
! 69667: assert( db->init.busy==0 );
! 69668: db->init.busy = 1;
! 69669: u.by.initData.rc = SQLITE_OK;
! 69670: assert( !db->mallocFailed );
! 69671: rc = sqlite3_exec(db, u.by.zSql, sqlite3InitCallback, &u.by.initData, 0);
! 69672: if( rc==SQLITE_OK ) rc = u.by.initData.rc;
! 69673: sqlite3DbFree(db, u.by.zSql);
! 69674: db->init.busy = 0;
! 69675: }
! 69676: }
! 69677: if( rc==SQLITE_NOMEM ){
! 69678: goto no_mem;
! 69679: }
! 69680: break;
! 69681: }
! 69682:
! 69683: #if !defined(SQLITE_OMIT_ANALYZE)
! 69684: /* Opcode: LoadAnalysis P1 * * * *
! 69685: **
! 69686: ** Read the sqlite_stat1 table for database P1 and load the content
! 69687: ** of that table into the internal index hash table. This will cause
! 69688: ** the analysis to be used when preparing all subsequent queries.
! 69689: */
! 69690: case OP_LoadAnalysis: {
! 69691: assert( pOp->p1>=0 && pOp->p1<db->nDb );
! 69692: rc = sqlite3AnalysisLoad(db, pOp->p1);
! 69693: break;
! 69694: }
! 69695: #endif /* !defined(SQLITE_OMIT_ANALYZE) */
! 69696:
! 69697: /* Opcode: DropTable P1 * * P4 *
! 69698: **
! 69699: ** Remove the internal (in-memory) data structures that describe
! 69700: ** the table named P4 in database P1. This is called after a table
! 69701: ** is dropped in order to keep the internal representation of the
! 69702: ** schema consistent with what is on disk.
! 69703: */
! 69704: case OP_DropTable: {
! 69705: sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
! 69706: break;
! 69707: }
! 69708:
! 69709: /* Opcode: DropIndex P1 * * P4 *
! 69710: **
! 69711: ** Remove the internal (in-memory) data structures that describe
! 69712: ** the index named P4 in database P1. This is called after an index
! 69713: ** is dropped in order to keep the internal representation of the
! 69714: ** schema consistent with what is on disk.
! 69715: */
! 69716: case OP_DropIndex: {
! 69717: sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
! 69718: break;
! 69719: }
! 69720:
! 69721: /* Opcode: DropTrigger P1 * * P4 *
! 69722: **
! 69723: ** Remove the internal (in-memory) data structures that describe
! 69724: ** the trigger named P4 in database P1. This is called after a trigger
! 69725: ** is dropped in order to keep the internal representation of the
! 69726: ** schema consistent with what is on disk.
! 69727: */
! 69728: case OP_DropTrigger: {
! 69729: sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
! 69730: break;
! 69731: }
! 69732:
! 69733:
! 69734: #ifndef SQLITE_OMIT_INTEGRITY_CHECK
! 69735: /* Opcode: IntegrityCk P1 P2 P3 * P5
! 69736: **
! 69737: ** Do an analysis of the currently open database. Store in
! 69738: ** register P1 the text of an error message describing any problems.
! 69739: ** If no problems are found, store a NULL in register P1.
! 69740: **
! 69741: ** The register P3 contains the maximum number of allowed errors.
! 69742: ** At most reg(P3) errors will be reported.
! 69743: ** In other words, the analysis stops as soon as reg(P1) errors are
! 69744: ** seen. Reg(P1) is updated with the number of errors remaining.
! 69745: **
! 69746: ** The root page numbers of all tables in the database are integer
! 69747: ** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
! 69748: ** total.
! 69749: **
! 69750: ** If P5 is not zero, the check is done on the auxiliary database
! 69751: ** file, not the main database file.
! 69752: **
! 69753: ** This opcode is used to implement the integrity_check pragma.
! 69754: */
! 69755: case OP_IntegrityCk: {
! 69756: #if 0 /* local variables moved into u.bz */
! 69757: int nRoot; /* Number of tables to check. (Number of root pages.) */
! 69758: int *aRoot; /* Array of rootpage numbers for tables to be checked */
! 69759: int j; /* Loop counter */
! 69760: int nErr; /* Number of errors reported */
! 69761: char *z; /* Text of the error report */
! 69762: Mem *pnErr; /* Register keeping track of errors remaining */
! 69763: #endif /* local variables moved into u.bz */
! 69764:
! 69765: u.bz.nRoot = pOp->p2;
! 69766: assert( u.bz.nRoot>0 );
! 69767: u.bz.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bz.nRoot+1) );
! 69768: if( u.bz.aRoot==0 ) goto no_mem;
! 69769: assert( pOp->p3>0 && pOp->p3<=p->nMem );
! 69770: u.bz.pnErr = &aMem[pOp->p3];
! 69771: assert( (u.bz.pnErr->flags & MEM_Int)!=0 );
! 69772: assert( (u.bz.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
! 69773: pIn1 = &aMem[pOp->p1];
! 69774: for(u.bz.j=0; u.bz.j<u.bz.nRoot; u.bz.j++){
! 69775: u.bz.aRoot[u.bz.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bz.j]);
! 69776: }
! 69777: u.bz.aRoot[u.bz.j] = 0;
! 69778: assert( pOp->p5<db->nDb );
! 69779: assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
! 69780: u.bz.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bz.aRoot, u.bz.nRoot,
! 69781: (int)u.bz.pnErr->u.i, &u.bz.nErr);
! 69782: sqlite3DbFree(db, u.bz.aRoot);
! 69783: u.bz.pnErr->u.i -= u.bz.nErr;
! 69784: sqlite3VdbeMemSetNull(pIn1);
! 69785: if( u.bz.nErr==0 ){
! 69786: assert( u.bz.z==0 );
! 69787: }else if( u.bz.z==0 ){
! 69788: goto no_mem;
! 69789: }else{
! 69790: sqlite3VdbeMemSetStr(pIn1, u.bz.z, -1, SQLITE_UTF8, sqlite3_free);
! 69791: }
! 69792: UPDATE_MAX_BLOBSIZE(pIn1);
! 69793: sqlite3VdbeChangeEncoding(pIn1, encoding);
! 69794: break;
! 69795: }
! 69796: #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
! 69797:
! 69798: /* Opcode: RowSetAdd P1 P2 * * *
! 69799: **
! 69800: ** Insert the integer value held by register P2 into a boolean index
! 69801: ** held in register P1.
! 69802: **
! 69803: ** An assertion fails if P2 is not an integer.
! 69804: */
! 69805: case OP_RowSetAdd: { /* in1, in2 */
! 69806: pIn1 = &aMem[pOp->p1];
! 69807: pIn2 = &aMem[pOp->p2];
! 69808: assert( (pIn2->flags & MEM_Int)!=0 );
! 69809: if( (pIn1->flags & MEM_RowSet)==0 ){
! 69810: sqlite3VdbeMemSetRowSet(pIn1);
! 69811: if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
! 69812: }
! 69813: sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
! 69814: break;
! 69815: }
! 69816:
! 69817: /* Opcode: RowSetRead P1 P2 P3 * *
! 69818: **
! 69819: ** Extract the smallest value from boolean index P1 and put that value into
! 69820: ** register P3. Or, if boolean index P1 is initially empty, leave P3
! 69821: ** unchanged and jump to instruction P2.
! 69822: */
! 69823: case OP_RowSetRead: { /* jump, in1, out3 */
! 69824: #if 0 /* local variables moved into u.ca */
! 69825: i64 val;
! 69826: #endif /* local variables moved into u.ca */
! 69827: CHECK_FOR_INTERRUPT;
! 69828: pIn1 = &aMem[pOp->p1];
! 69829: if( (pIn1->flags & MEM_RowSet)==0
! 69830: || sqlite3RowSetNext(pIn1->u.pRowSet, &u.ca.val)==0
! 69831: ){
! 69832: /* The boolean index is empty */
! 69833: sqlite3VdbeMemSetNull(pIn1);
! 69834: pc = pOp->p2 - 1;
! 69835: }else{
! 69836: /* A value was pulled from the index */
! 69837: sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.ca.val);
! 69838: }
! 69839: break;
! 69840: }
! 69841:
! 69842: /* Opcode: RowSetTest P1 P2 P3 P4
! 69843: **
! 69844: ** Register P3 is assumed to hold a 64-bit integer value. If register P1
! 69845: ** contains a RowSet object and that RowSet object contains
! 69846: ** the value held in P3, jump to register P2. Otherwise, insert the
! 69847: ** integer in P3 into the RowSet and continue on to the
! 69848: ** next opcode.
! 69849: **
! 69850: ** The RowSet object is optimized for the case where successive sets
! 69851: ** of integers, where each set contains no duplicates. Each set
! 69852: ** of values is identified by a unique P4 value. The first set
! 69853: ** must have P4==0, the final set P4=-1. P4 must be either -1 or
! 69854: ** non-negative. For non-negative values of P4 only the lower 4
! 69855: ** bits are significant.
! 69856: **
! 69857: ** This allows optimizations: (a) when P4==0 there is no need to test
! 69858: ** the rowset object for P3, as it is guaranteed not to contain it,
! 69859: ** (b) when P4==-1 there is no need to insert the value, as it will
! 69860: ** never be tested for, and (c) when a value that is part of set X is
! 69861: ** inserted, there is no need to search to see if the same value was
! 69862: ** previously inserted as part of set X (only if it was previously
! 69863: ** inserted as part of some other set).
! 69864: */
! 69865: case OP_RowSetTest: { /* jump, in1, in3 */
! 69866: #if 0 /* local variables moved into u.cb */
! 69867: int iSet;
! 69868: int exists;
! 69869: #endif /* local variables moved into u.cb */
! 69870:
! 69871: pIn1 = &aMem[pOp->p1];
! 69872: pIn3 = &aMem[pOp->p3];
! 69873: u.cb.iSet = pOp->p4.i;
! 69874: assert( pIn3->flags&MEM_Int );
! 69875:
! 69876: /* If there is anything other than a rowset object in memory cell P1,
! 69877: ** delete it now and initialize P1 with an empty rowset
! 69878: */
! 69879: if( (pIn1->flags & MEM_RowSet)==0 ){
! 69880: sqlite3VdbeMemSetRowSet(pIn1);
! 69881: if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
! 69882: }
! 69883:
! 69884: assert( pOp->p4type==P4_INT32 );
! 69885: assert( u.cb.iSet==-1 || u.cb.iSet>=0 );
! 69886: if( u.cb.iSet ){
! 69887: u.cb.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
! 69888: (u8)(u.cb.iSet>=0 ? u.cb.iSet & 0xf : 0xff),
! 69889: pIn3->u.i);
! 69890: if( u.cb.exists ){
! 69891: pc = pOp->p2 - 1;
! 69892: break;
! 69893: }
! 69894: }
! 69895: if( u.cb.iSet>=0 ){
! 69896: sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
! 69897: }
! 69898: break;
! 69899: }
! 69900:
! 69901:
! 69902: #ifndef SQLITE_OMIT_TRIGGER
! 69903:
! 69904: /* Opcode: Program P1 P2 P3 P4 *
! 69905: **
! 69906: ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
! 69907: **
! 69908: ** P1 contains the address of the memory cell that contains the first memory
! 69909: ** cell in an array of values used as arguments to the sub-program. P2
! 69910: ** contains the address to jump to if the sub-program throws an IGNORE
! 69911: ** exception using the RAISE() function. Register P3 contains the address
! 69912: ** of a memory cell in this (the parent) VM that is used to allocate the
! 69913: ** memory required by the sub-vdbe at runtime.
! 69914: **
! 69915: ** P4 is a pointer to the VM containing the trigger program.
! 69916: */
! 69917: case OP_Program: { /* jump */
! 69918: #if 0 /* local variables moved into u.cc */
! 69919: int nMem; /* Number of memory registers for sub-program */
! 69920: int nByte; /* Bytes of runtime space required for sub-program */
! 69921: Mem *pRt; /* Register to allocate runtime space */
! 69922: Mem *pMem; /* Used to iterate through memory cells */
! 69923: Mem *pEnd; /* Last memory cell in new array */
! 69924: VdbeFrame *pFrame; /* New vdbe frame to execute in */
! 69925: SubProgram *pProgram; /* Sub-program to execute */
! 69926: void *t; /* Token identifying trigger */
! 69927: #endif /* local variables moved into u.cc */
! 69928:
! 69929: u.cc.pProgram = pOp->p4.pProgram;
! 69930: u.cc.pRt = &aMem[pOp->p3];
! 69931: assert( u.cc.pProgram->nOp>0 );
! 69932:
! 69933: /* If the p5 flag is clear, then recursive invocation of triggers is
! 69934: ** disabled for backwards compatibility (p5 is set if this sub-program
! 69935: ** is really a trigger, not a foreign key action, and the flag set
! 69936: ** and cleared by the "PRAGMA recursive_triggers" command is clear).
! 69937: **
! 69938: ** It is recursive invocation of triggers, at the SQL level, that is
! 69939: ** disabled. In some cases a single trigger may generate more than one
! 69940: ** SubProgram (if the trigger may be executed with more than one different
! 69941: ** ON CONFLICT algorithm). SubProgram structures associated with a
! 69942: ** single trigger all have the same value for the SubProgram.token
! 69943: ** variable. */
! 69944: if( pOp->p5 ){
! 69945: u.cc.t = u.cc.pProgram->token;
! 69946: for(u.cc.pFrame=p->pFrame; u.cc.pFrame && u.cc.pFrame->token!=u.cc.t; u.cc.pFrame=u.cc.pFrame->pParent);
! 69947: if( u.cc.pFrame ) break;
! 69948: }
! 69949:
! 69950: if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
! 69951: rc = SQLITE_ERROR;
! 69952: sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
! 69953: break;
! 69954: }
! 69955:
! 69956: /* Register u.cc.pRt is used to store the memory required to save the state
! 69957: ** of the current program, and the memory required at runtime to execute
! 69958: ** the trigger program. If this trigger has been fired before, then u.cc.pRt
! 69959: ** is already allocated. Otherwise, it must be initialized. */
! 69960: if( (u.cc.pRt->flags&MEM_Frame)==0 ){
! 69961: /* SubProgram.nMem is set to the number of memory cells used by the
! 69962: ** program stored in SubProgram.aOp. As well as these, one memory
! 69963: ** cell is required for each cursor used by the program. Set local
! 69964: ** variable u.cc.nMem (and later, VdbeFrame.nChildMem) to this value.
! 69965: */
! 69966: u.cc.nMem = u.cc.pProgram->nMem + u.cc.pProgram->nCsr;
! 69967: u.cc.nByte = ROUND8(sizeof(VdbeFrame))
! 69968: + u.cc.nMem * sizeof(Mem)
! 69969: + u.cc.pProgram->nCsr * sizeof(VdbeCursor *)
! 69970: + u.cc.pProgram->nOnce * sizeof(u8);
! 69971: u.cc.pFrame = sqlite3DbMallocZero(db, u.cc.nByte);
! 69972: if( !u.cc.pFrame ){
! 69973: goto no_mem;
! 69974: }
! 69975: sqlite3VdbeMemRelease(u.cc.pRt);
! 69976: u.cc.pRt->flags = MEM_Frame;
! 69977: u.cc.pRt->u.pFrame = u.cc.pFrame;
! 69978:
! 69979: u.cc.pFrame->v = p;
! 69980: u.cc.pFrame->nChildMem = u.cc.nMem;
! 69981: u.cc.pFrame->nChildCsr = u.cc.pProgram->nCsr;
! 69982: u.cc.pFrame->pc = pc;
! 69983: u.cc.pFrame->aMem = p->aMem;
! 69984: u.cc.pFrame->nMem = p->nMem;
! 69985: u.cc.pFrame->apCsr = p->apCsr;
! 69986: u.cc.pFrame->nCursor = p->nCursor;
! 69987: u.cc.pFrame->aOp = p->aOp;
! 69988: u.cc.pFrame->nOp = p->nOp;
! 69989: u.cc.pFrame->token = u.cc.pProgram->token;
! 69990: u.cc.pFrame->aOnceFlag = p->aOnceFlag;
! 69991: u.cc.pFrame->nOnceFlag = p->nOnceFlag;
! 69992:
! 69993: u.cc.pEnd = &VdbeFrameMem(u.cc.pFrame)[u.cc.pFrame->nChildMem];
! 69994: for(u.cc.pMem=VdbeFrameMem(u.cc.pFrame); u.cc.pMem!=u.cc.pEnd; u.cc.pMem++){
! 69995: u.cc.pMem->flags = MEM_Invalid;
! 69996: u.cc.pMem->db = db;
! 69997: }
! 69998: }else{
! 69999: u.cc.pFrame = u.cc.pRt->u.pFrame;
! 70000: assert( u.cc.pProgram->nMem+u.cc.pProgram->nCsr==u.cc.pFrame->nChildMem );
! 70001: assert( u.cc.pProgram->nCsr==u.cc.pFrame->nChildCsr );
! 70002: assert( pc==u.cc.pFrame->pc );
! 70003: }
! 70004:
! 70005: p->nFrame++;
! 70006: u.cc.pFrame->pParent = p->pFrame;
! 70007: u.cc.pFrame->lastRowid = lastRowid;
! 70008: u.cc.pFrame->nChange = p->nChange;
! 70009: p->nChange = 0;
! 70010: p->pFrame = u.cc.pFrame;
! 70011: p->aMem = aMem = &VdbeFrameMem(u.cc.pFrame)[-1];
! 70012: p->nMem = u.cc.pFrame->nChildMem;
! 70013: p->nCursor = (u16)u.cc.pFrame->nChildCsr;
! 70014: p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
! 70015: p->aOp = aOp = u.cc.pProgram->aOp;
! 70016: p->nOp = u.cc.pProgram->nOp;
! 70017: p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
! 70018: p->nOnceFlag = u.cc.pProgram->nOnce;
! 70019: p->nOp = u.cc.pProgram->nOp;
! 70020: pc = -1;
! 70021: memset(p->aOnceFlag, 0, p->nOnceFlag);
! 70022:
! 70023: break;
! 70024: }
! 70025:
! 70026: /* Opcode: Param P1 P2 * * *
! 70027: **
! 70028: ** This opcode is only ever present in sub-programs called via the
! 70029: ** OP_Program instruction. Copy a value currently stored in a memory
! 70030: ** cell of the calling (parent) frame to cell P2 in the current frames
! 70031: ** address space. This is used by trigger programs to access the new.*
! 70032: ** and old.* values.
! 70033: **
! 70034: ** The address of the cell in the parent frame is determined by adding
! 70035: ** the value of the P1 argument to the value of the P1 argument to the
! 70036: ** calling OP_Program instruction.
! 70037: */
! 70038: case OP_Param: { /* out2-prerelease */
! 70039: #if 0 /* local variables moved into u.cd */
! 70040: VdbeFrame *pFrame;
! 70041: Mem *pIn;
! 70042: #endif /* local variables moved into u.cd */
! 70043: u.cd.pFrame = p->pFrame;
! 70044: u.cd.pIn = &u.cd.pFrame->aMem[pOp->p1 + u.cd.pFrame->aOp[u.cd.pFrame->pc].p1];
! 70045: sqlite3VdbeMemShallowCopy(pOut, u.cd.pIn, MEM_Ephem);
! 70046: break;
! 70047: }
! 70048:
! 70049: #endif /* #ifndef SQLITE_OMIT_TRIGGER */
! 70050:
! 70051: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 70052: /* Opcode: FkCounter P1 P2 * * *
! 70053: **
! 70054: ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
! 70055: ** If P1 is non-zero, the database constraint counter is incremented
! 70056: ** (deferred foreign key constraints). Otherwise, if P1 is zero, the
! 70057: ** statement counter is incremented (immediate foreign key constraints).
! 70058: */
! 70059: case OP_FkCounter: {
! 70060: if( pOp->p1 ){
! 70061: db->nDeferredCons += pOp->p2;
! 70062: }else{
! 70063: p->nFkConstraint += pOp->p2;
! 70064: }
! 70065: break;
! 70066: }
! 70067:
! 70068: /* Opcode: FkIfZero P1 P2 * * *
! 70069: **
! 70070: ** This opcode tests if a foreign key constraint-counter is currently zero.
! 70071: ** If so, jump to instruction P2. Otherwise, fall through to the next
! 70072: ** instruction.
! 70073: **
! 70074: ** If P1 is non-zero, then the jump is taken if the database constraint-counter
! 70075: ** is zero (the one that counts deferred constraint violations). If P1 is
! 70076: ** zero, the jump is taken if the statement constraint-counter is zero
! 70077: ** (immediate foreign key constraint violations).
! 70078: */
! 70079: case OP_FkIfZero: { /* jump */
! 70080: if( pOp->p1 ){
! 70081: if( db->nDeferredCons==0 ) pc = pOp->p2-1;
! 70082: }else{
! 70083: if( p->nFkConstraint==0 ) pc = pOp->p2-1;
! 70084: }
! 70085: break;
! 70086: }
! 70087: #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
! 70088:
! 70089: #ifndef SQLITE_OMIT_AUTOINCREMENT
! 70090: /* Opcode: MemMax P1 P2 * * *
! 70091: **
! 70092: ** P1 is a register in the root frame of this VM (the root frame is
! 70093: ** different from the current frame if this instruction is being executed
! 70094: ** within a sub-program). Set the value of register P1 to the maximum of
! 70095: ** its current value and the value in register P2.
! 70096: **
! 70097: ** This instruction throws an error if the memory cell is not initially
! 70098: ** an integer.
! 70099: */
! 70100: case OP_MemMax: { /* in2 */
! 70101: #if 0 /* local variables moved into u.ce */
! 70102: Mem *pIn1;
! 70103: VdbeFrame *pFrame;
! 70104: #endif /* local variables moved into u.ce */
! 70105: if( p->pFrame ){
! 70106: for(u.ce.pFrame=p->pFrame; u.ce.pFrame->pParent; u.ce.pFrame=u.ce.pFrame->pParent);
! 70107: u.ce.pIn1 = &u.ce.pFrame->aMem[pOp->p1];
! 70108: }else{
! 70109: u.ce.pIn1 = &aMem[pOp->p1];
! 70110: }
! 70111: assert( memIsValid(u.ce.pIn1) );
! 70112: sqlite3VdbeMemIntegerify(u.ce.pIn1);
! 70113: pIn2 = &aMem[pOp->p2];
! 70114: sqlite3VdbeMemIntegerify(pIn2);
! 70115: if( u.ce.pIn1->u.i<pIn2->u.i){
! 70116: u.ce.pIn1->u.i = pIn2->u.i;
! 70117: }
! 70118: break;
! 70119: }
! 70120: #endif /* SQLITE_OMIT_AUTOINCREMENT */
! 70121:
! 70122: /* Opcode: IfPos P1 P2 * * *
! 70123: **
! 70124: ** If the value of register P1 is 1 or greater, jump to P2.
! 70125: **
! 70126: ** It is illegal to use this instruction on a register that does
! 70127: ** not contain an integer. An assertion fault will result if you try.
! 70128: */
! 70129: case OP_IfPos: { /* jump, in1 */
! 70130: pIn1 = &aMem[pOp->p1];
! 70131: assert( pIn1->flags&MEM_Int );
! 70132: if( pIn1->u.i>0 ){
! 70133: pc = pOp->p2 - 1;
! 70134: }
! 70135: break;
! 70136: }
! 70137:
! 70138: /* Opcode: IfNeg P1 P2 * * *
! 70139: **
! 70140: ** If the value of register P1 is less than zero, jump to P2.
! 70141: **
! 70142: ** It is illegal to use this instruction on a register that does
! 70143: ** not contain an integer. An assertion fault will result if you try.
! 70144: */
! 70145: case OP_IfNeg: { /* jump, in1 */
! 70146: pIn1 = &aMem[pOp->p1];
! 70147: assert( pIn1->flags&MEM_Int );
! 70148: if( pIn1->u.i<0 ){
! 70149: pc = pOp->p2 - 1;
! 70150: }
! 70151: break;
! 70152: }
! 70153:
! 70154: /* Opcode: IfZero P1 P2 P3 * *
! 70155: **
! 70156: ** The register P1 must contain an integer. Add literal P3 to the
! 70157: ** value in register P1. If the result is exactly 0, jump to P2.
! 70158: **
! 70159: ** It is illegal to use this instruction on a register that does
! 70160: ** not contain an integer. An assertion fault will result if you try.
! 70161: */
! 70162: case OP_IfZero: { /* jump, in1 */
! 70163: pIn1 = &aMem[pOp->p1];
! 70164: assert( pIn1->flags&MEM_Int );
! 70165: pIn1->u.i += pOp->p3;
! 70166: if( pIn1->u.i==0 ){
! 70167: pc = pOp->p2 - 1;
! 70168: }
! 70169: break;
! 70170: }
! 70171:
! 70172: /* Opcode: AggStep * P2 P3 P4 P5
! 70173: **
! 70174: ** Execute the step function for an aggregate. The
! 70175: ** function has P5 arguments. P4 is a pointer to the FuncDef
! 70176: ** structure that specifies the function. Use register
! 70177: ** P3 as the accumulator.
! 70178: **
! 70179: ** The P5 arguments are taken from register P2 and its
! 70180: ** successors.
! 70181: */
! 70182: case OP_AggStep: {
! 70183: #if 0 /* local variables moved into u.cf */
! 70184: int n;
! 70185: int i;
! 70186: Mem *pMem;
! 70187: Mem *pRec;
! 70188: sqlite3_context ctx;
! 70189: sqlite3_value **apVal;
! 70190: #endif /* local variables moved into u.cf */
! 70191:
! 70192: u.cf.n = pOp->p5;
! 70193: assert( u.cf.n>=0 );
! 70194: u.cf.pRec = &aMem[pOp->p2];
! 70195: u.cf.apVal = p->apArg;
! 70196: assert( u.cf.apVal || u.cf.n==0 );
! 70197: for(u.cf.i=0; u.cf.i<u.cf.n; u.cf.i++, u.cf.pRec++){
! 70198: assert( memIsValid(u.cf.pRec) );
! 70199: u.cf.apVal[u.cf.i] = u.cf.pRec;
! 70200: memAboutToChange(p, u.cf.pRec);
! 70201: sqlite3VdbeMemStoreType(u.cf.pRec);
! 70202: }
! 70203: u.cf.ctx.pFunc = pOp->p4.pFunc;
! 70204: assert( pOp->p3>0 && pOp->p3<=p->nMem );
! 70205: u.cf.ctx.pMem = u.cf.pMem = &aMem[pOp->p3];
! 70206: u.cf.pMem->n++;
! 70207: u.cf.ctx.s.flags = MEM_Null;
! 70208: u.cf.ctx.s.z = 0;
! 70209: u.cf.ctx.s.zMalloc = 0;
! 70210: u.cf.ctx.s.xDel = 0;
! 70211: u.cf.ctx.s.db = db;
! 70212: u.cf.ctx.isError = 0;
! 70213: u.cf.ctx.pColl = 0;
! 70214: if( u.cf.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
! 70215: assert( pOp>p->aOp );
! 70216: assert( pOp[-1].p4type==P4_COLLSEQ );
! 70217: assert( pOp[-1].opcode==OP_CollSeq );
! 70218: u.cf.ctx.pColl = pOp[-1].p4.pColl;
! 70219: }
! 70220: (u.cf.ctx.pFunc->xStep)(&u.cf.ctx, u.cf.n, u.cf.apVal); /* IMP: R-24505-23230 */
! 70221: if( u.cf.ctx.isError ){
! 70222: sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cf.ctx.s));
! 70223: rc = u.cf.ctx.isError;
! 70224: }
! 70225:
! 70226: sqlite3VdbeMemRelease(&u.cf.ctx.s);
! 70227:
! 70228: break;
! 70229: }
! 70230:
! 70231: /* Opcode: AggFinal P1 P2 * P4 *
! 70232: **
! 70233: ** Execute the finalizer function for an aggregate. P1 is
! 70234: ** the memory location that is the accumulator for the aggregate.
! 70235: **
! 70236: ** P2 is the number of arguments that the step function takes and
! 70237: ** P4 is a pointer to the FuncDef for this function. The P2
! 70238: ** argument is not used by this opcode. It is only there to disambiguate
! 70239: ** functions that can take varying numbers of arguments. The
! 70240: ** P4 argument is only needed for the degenerate case where
! 70241: ** the step function was not previously called.
! 70242: */
! 70243: case OP_AggFinal: {
! 70244: #if 0 /* local variables moved into u.cg */
! 70245: Mem *pMem;
! 70246: #endif /* local variables moved into u.cg */
! 70247: assert( pOp->p1>0 && pOp->p1<=p->nMem );
! 70248: u.cg.pMem = &aMem[pOp->p1];
! 70249: assert( (u.cg.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
! 70250: rc = sqlite3VdbeMemFinalize(u.cg.pMem, pOp->p4.pFunc);
! 70251: if( rc ){
! 70252: sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cg.pMem));
! 70253: }
! 70254: sqlite3VdbeChangeEncoding(u.cg.pMem, encoding);
! 70255: UPDATE_MAX_BLOBSIZE(u.cg.pMem);
! 70256: if( sqlite3VdbeMemTooBig(u.cg.pMem) ){
! 70257: goto too_big;
! 70258: }
! 70259: break;
! 70260: }
! 70261:
! 70262: #ifndef SQLITE_OMIT_WAL
! 70263: /* Opcode: Checkpoint P1 P2 P3 * *
! 70264: **
! 70265: ** Checkpoint database P1. This is a no-op if P1 is not currently in
! 70266: ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
! 70267: ** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
! 70268: ** SQLITE_BUSY or not, respectively. Write the number of pages in the
! 70269: ** WAL after the checkpoint into mem[P3+1] and the number of pages
! 70270: ** in the WAL that have been checkpointed after the checkpoint
! 70271: ** completes into mem[P3+2]. However on an error, mem[P3+1] and
! 70272: ** mem[P3+2] are initialized to -1.
! 70273: */
! 70274: case OP_Checkpoint: {
! 70275: #if 0 /* local variables moved into u.ch */
! 70276: int i; /* Loop counter */
! 70277: int aRes[3]; /* Results */
! 70278: Mem *pMem; /* Write results here */
! 70279: #endif /* local variables moved into u.ch */
! 70280:
! 70281: u.ch.aRes[0] = 0;
! 70282: u.ch.aRes[1] = u.ch.aRes[2] = -1;
! 70283: assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
! 70284: || pOp->p2==SQLITE_CHECKPOINT_FULL
! 70285: || pOp->p2==SQLITE_CHECKPOINT_RESTART
! 70286: );
! 70287: rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.ch.aRes[1], &u.ch.aRes[2]);
! 70288: if( rc==SQLITE_BUSY ){
! 70289: rc = SQLITE_OK;
! 70290: u.ch.aRes[0] = 1;
! 70291: }
! 70292: for(u.ch.i=0, u.ch.pMem = &aMem[pOp->p3]; u.ch.i<3; u.ch.i++, u.ch.pMem++){
! 70293: sqlite3VdbeMemSetInt64(u.ch.pMem, (i64)u.ch.aRes[u.ch.i]);
! 70294: }
! 70295: break;
! 70296: };
! 70297: #endif
! 70298:
! 70299: #ifndef SQLITE_OMIT_PRAGMA
! 70300: /* Opcode: JournalMode P1 P2 P3 * P5
! 70301: **
! 70302: ** Change the journal mode of database P1 to P3. P3 must be one of the
! 70303: ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
! 70304: ** modes (delete, truncate, persist, off and memory), this is a simple
! 70305: ** operation. No IO is required.
! 70306: **
! 70307: ** If changing into or out of WAL mode the procedure is more complicated.
! 70308: **
! 70309: ** Write a string containing the final journal-mode to register P2.
! 70310: */
! 70311: case OP_JournalMode: { /* out2-prerelease */
! 70312: #if 0 /* local variables moved into u.ci */
! 70313: Btree *pBt; /* Btree to change journal mode of */
! 70314: Pager *pPager; /* Pager associated with pBt */
! 70315: int eNew; /* New journal mode */
! 70316: int eOld; /* The old journal mode */
! 70317: const char *zFilename; /* Name of database file for pPager */
! 70318: #endif /* local variables moved into u.ci */
! 70319:
! 70320: u.ci.eNew = pOp->p3;
! 70321: assert( u.ci.eNew==PAGER_JOURNALMODE_DELETE
! 70322: || u.ci.eNew==PAGER_JOURNALMODE_TRUNCATE
! 70323: || u.ci.eNew==PAGER_JOURNALMODE_PERSIST
! 70324: || u.ci.eNew==PAGER_JOURNALMODE_OFF
! 70325: || u.ci.eNew==PAGER_JOURNALMODE_MEMORY
! 70326: || u.ci.eNew==PAGER_JOURNALMODE_WAL
! 70327: || u.ci.eNew==PAGER_JOURNALMODE_QUERY
! 70328: );
! 70329: assert( pOp->p1>=0 && pOp->p1<db->nDb );
! 70330:
! 70331: u.ci.pBt = db->aDb[pOp->p1].pBt;
! 70332: u.ci.pPager = sqlite3BtreePager(u.ci.pBt);
! 70333: u.ci.eOld = sqlite3PagerGetJournalMode(u.ci.pPager);
! 70334: if( u.ci.eNew==PAGER_JOURNALMODE_QUERY ) u.ci.eNew = u.ci.eOld;
! 70335: if( !sqlite3PagerOkToChangeJournalMode(u.ci.pPager) ) u.ci.eNew = u.ci.eOld;
! 70336:
! 70337: #ifndef SQLITE_OMIT_WAL
! 70338: u.ci.zFilename = sqlite3PagerFilename(u.ci.pPager);
! 70339:
! 70340: /* Do not allow a transition to journal_mode=WAL for a database
! 70341: ** in temporary storage or if the VFS does not support shared memory
! 70342: */
! 70343: if( u.ci.eNew==PAGER_JOURNALMODE_WAL
! 70344: && (sqlite3Strlen30(u.ci.zFilename)==0 /* Temp file */
! 70345: || !sqlite3PagerWalSupported(u.ci.pPager)) /* No shared-memory support */
! 70346: ){
! 70347: u.ci.eNew = u.ci.eOld;
! 70348: }
! 70349:
! 70350: if( (u.ci.eNew!=u.ci.eOld)
! 70351: && (u.ci.eOld==PAGER_JOURNALMODE_WAL || u.ci.eNew==PAGER_JOURNALMODE_WAL)
! 70352: ){
! 70353: if( !db->autoCommit || db->activeVdbeCnt>1 ){
! 70354: rc = SQLITE_ERROR;
! 70355: sqlite3SetString(&p->zErrMsg, db,
! 70356: "cannot change %s wal mode from within a transaction",
! 70357: (u.ci.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
! 70358: );
! 70359: break;
! 70360: }else{
! 70361:
! 70362: if( u.ci.eOld==PAGER_JOURNALMODE_WAL ){
! 70363: /* If leaving WAL mode, close the log file. If successful, the call
! 70364: ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
! 70365: ** file. An EXCLUSIVE lock may still be held on the database file
! 70366: ** after a successful return.
! 70367: */
! 70368: rc = sqlite3PagerCloseWal(u.ci.pPager);
! 70369: if( rc==SQLITE_OK ){
! 70370: sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
! 70371: }
! 70372: }else if( u.ci.eOld==PAGER_JOURNALMODE_MEMORY ){
! 70373: /* Cannot transition directly from MEMORY to WAL. Use mode OFF
! 70374: ** as an intermediate */
! 70375: sqlite3PagerSetJournalMode(u.ci.pPager, PAGER_JOURNALMODE_OFF);
! 70376: }
! 70377:
! 70378: /* Open a transaction on the database file. Regardless of the journal
! 70379: ** mode, this transaction always uses a rollback journal.
! 70380: */
! 70381: assert( sqlite3BtreeIsInTrans(u.ci.pBt)==0 );
! 70382: if( rc==SQLITE_OK ){
! 70383: rc = sqlite3BtreeSetVersion(u.ci.pBt, (u.ci.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
! 70384: }
! 70385: }
! 70386: }
! 70387: #endif /* ifndef SQLITE_OMIT_WAL */
! 70388:
! 70389: if( rc ){
! 70390: u.ci.eNew = u.ci.eOld;
! 70391: }
! 70392: u.ci.eNew = sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
! 70393:
! 70394: pOut = &aMem[pOp->p2];
! 70395: pOut->flags = MEM_Str|MEM_Static|MEM_Term;
! 70396: pOut->z = (char *)sqlite3JournalModename(u.ci.eNew);
! 70397: pOut->n = sqlite3Strlen30(pOut->z);
! 70398: pOut->enc = SQLITE_UTF8;
! 70399: sqlite3VdbeChangeEncoding(pOut, encoding);
! 70400: break;
! 70401: };
! 70402: #endif /* SQLITE_OMIT_PRAGMA */
! 70403:
! 70404: #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
! 70405: /* Opcode: Vacuum * * * * *
! 70406: **
! 70407: ** Vacuum the entire database. This opcode will cause other virtual
! 70408: ** machines to be created and run. It may not be called from within
! 70409: ** a transaction.
! 70410: */
! 70411: case OP_Vacuum: {
! 70412: rc = sqlite3RunVacuum(&p->zErrMsg, db);
! 70413: break;
! 70414: }
! 70415: #endif
! 70416:
! 70417: #if !defined(SQLITE_OMIT_AUTOVACUUM)
! 70418: /* Opcode: IncrVacuum P1 P2 * * *
! 70419: **
! 70420: ** Perform a single step of the incremental vacuum procedure on
! 70421: ** the P1 database. If the vacuum has finished, jump to instruction
! 70422: ** P2. Otherwise, fall through to the next instruction.
! 70423: */
! 70424: case OP_IncrVacuum: { /* jump */
! 70425: #if 0 /* local variables moved into u.cj */
! 70426: Btree *pBt;
! 70427: #endif /* local variables moved into u.cj */
! 70428:
! 70429: assert( pOp->p1>=0 && pOp->p1<db->nDb );
! 70430: assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
! 70431: u.cj.pBt = db->aDb[pOp->p1].pBt;
! 70432: rc = sqlite3BtreeIncrVacuum(u.cj.pBt);
! 70433: if( rc==SQLITE_DONE ){
! 70434: pc = pOp->p2 - 1;
! 70435: rc = SQLITE_OK;
! 70436: }
! 70437: break;
! 70438: }
! 70439: #endif
! 70440:
! 70441: /* Opcode: Expire P1 * * * *
! 70442: **
! 70443: ** Cause precompiled statements to become expired. An expired statement
! 70444: ** fails with an error code of SQLITE_SCHEMA if it is ever executed
! 70445: ** (via sqlite3_step()).
! 70446: **
! 70447: ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
! 70448: ** then only the currently executing statement is affected.
! 70449: */
! 70450: case OP_Expire: {
! 70451: if( !pOp->p1 ){
! 70452: sqlite3ExpirePreparedStatements(db);
! 70453: }else{
! 70454: p->expired = 1;
! 70455: }
! 70456: break;
! 70457: }
! 70458:
! 70459: #ifndef SQLITE_OMIT_SHARED_CACHE
! 70460: /* Opcode: TableLock P1 P2 P3 P4 *
! 70461: **
! 70462: ** Obtain a lock on a particular table. This instruction is only used when
! 70463: ** the shared-cache feature is enabled.
! 70464: **
! 70465: ** P1 is the index of the database in sqlite3.aDb[] of the database
! 70466: ** on which the lock is acquired. A readlock is obtained if P3==0 or
! 70467: ** a write lock if P3==1.
! 70468: **
! 70469: ** P2 contains the root-page of the table to lock.
! 70470: **
! 70471: ** P4 contains a pointer to the name of the table being locked. This is only
! 70472: ** used to generate an error message if the lock cannot be obtained.
! 70473: */
! 70474: case OP_TableLock: {
! 70475: u8 isWriteLock = (u8)pOp->p3;
! 70476: if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
! 70477: int p1 = pOp->p1;
! 70478: assert( p1>=0 && p1<db->nDb );
! 70479: assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
! 70480: assert( isWriteLock==0 || isWriteLock==1 );
! 70481: rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
! 70482: if( (rc&0xFF)==SQLITE_LOCKED ){
! 70483: const char *z = pOp->p4.z;
! 70484: sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
! 70485: }
! 70486: }
! 70487: break;
! 70488: }
! 70489: #endif /* SQLITE_OMIT_SHARED_CACHE */
! 70490:
! 70491: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 70492: /* Opcode: VBegin * * * P4 *
! 70493: **
! 70494: ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
! 70495: ** xBegin method for that table.
! 70496: **
! 70497: ** Also, whether or not P4 is set, check that this is not being called from
! 70498: ** within a callback to a virtual table xSync() method. If it is, the error
! 70499: ** code will be set to SQLITE_LOCKED.
! 70500: */
! 70501: case OP_VBegin: {
! 70502: #if 0 /* local variables moved into u.ck */
! 70503: VTable *pVTab;
! 70504: #endif /* local variables moved into u.ck */
! 70505: u.ck.pVTab = pOp->p4.pVtab;
! 70506: rc = sqlite3VtabBegin(db, u.ck.pVTab);
! 70507: if( u.ck.pVTab ) importVtabErrMsg(p, u.ck.pVTab->pVtab);
! 70508: break;
! 70509: }
! 70510: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 70511:
! 70512: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 70513: /* Opcode: VCreate P1 * * P4 *
! 70514: **
! 70515: ** P4 is the name of a virtual table in database P1. Call the xCreate method
! 70516: ** for that table.
! 70517: */
! 70518: case OP_VCreate: {
! 70519: rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
! 70520: break;
! 70521: }
! 70522: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 70523:
! 70524: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 70525: /* Opcode: VDestroy P1 * * P4 *
! 70526: **
! 70527: ** P4 is the name of a virtual table in database P1. Call the xDestroy method
! 70528: ** of that table.
! 70529: */
! 70530: case OP_VDestroy: {
! 70531: p->inVtabMethod = 2;
! 70532: rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
! 70533: p->inVtabMethod = 0;
! 70534: break;
! 70535: }
! 70536: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 70537:
! 70538: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 70539: /* Opcode: VOpen P1 * * P4 *
! 70540: **
! 70541: ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
! 70542: ** P1 is a cursor number. This opcode opens a cursor to the virtual
! 70543: ** table and stores that cursor in P1.
! 70544: */
! 70545: case OP_VOpen: {
! 70546: #if 0 /* local variables moved into u.cl */
! 70547: VdbeCursor *pCur;
! 70548: sqlite3_vtab_cursor *pVtabCursor;
! 70549: sqlite3_vtab *pVtab;
! 70550: sqlite3_module *pModule;
! 70551: #endif /* local variables moved into u.cl */
! 70552:
! 70553: u.cl.pCur = 0;
! 70554: u.cl.pVtabCursor = 0;
! 70555: u.cl.pVtab = pOp->p4.pVtab->pVtab;
! 70556: u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
! 70557: assert(u.cl.pVtab && u.cl.pModule);
! 70558: rc = u.cl.pModule->xOpen(u.cl.pVtab, &u.cl.pVtabCursor);
! 70559: importVtabErrMsg(p, u.cl.pVtab);
! 70560: if( SQLITE_OK==rc ){
! 70561: /* Initialize sqlite3_vtab_cursor base class */
! 70562: u.cl.pVtabCursor->pVtab = u.cl.pVtab;
! 70563:
! 70564: /* Initialise vdbe cursor object */
! 70565: u.cl.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
! 70566: if( u.cl.pCur ){
! 70567: u.cl.pCur->pVtabCursor = u.cl.pVtabCursor;
! 70568: u.cl.pCur->pModule = u.cl.pVtabCursor->pVtab->pModule;
! 70569: }else{
! 70570: db->mallocFailed = 1;
! 70571: u.cl.pModule->xClose(u.cl.pVtabCursor);
! 70572: }
! 70573: }
! 70574: break;
! 70575: }
! 70576: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 70577:
! 70578: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 70579: /* Opcode: VFilter P1 P2 P3 P4 *
! 70580: **
! 70581: ** P1 is a cursor opened using VOpen. P2 is an address to jump to if
! 70582: ** the filtered result set is empty.
! 70583: **
! 70584: ** P4 is either NULL or a string that was generated by the xBestIndex
! 70585: ** method of the module. The interpretation of the P4 string is left
! 70586: ** to the module implementation.
! 70587: **
! 70588: ** This opcode invokes the xFilter method on the virtual table specified
! 70589: ** by P1. The integer query plan parameter to xFilter is stored in register
! 70590: ** P3. Register P3+1 stores the argc parameter to be passed to the
! 70591: ** xFilter method. Registers P3+2..P3+1+argc are the argc
! 70592: ** additional parameters which are passed to
! 70593: ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
! 70594: **
! 70595: ** A jump is made to P2 if the result set after filtering would be empty.
! 70596: */
! 70597: case OP_VFilter: { /* jump */
! 70598: #if 0 /* local variables moved into u.cm */
! 70599: int nArg;
! 70600: int iQuery;
! 70601: const sqlite3_module *pModule;
! 70602: Mem *pQuery;
! 70603: Mem *pArgc;
! 70604: sqlite3_vtab_cursor *pVtabCursor;
! 70605: sqlite3_vtab *pVtab;
! 70606: VdbeCursor *pCur;
! 70607: int res;
! 70608: int i;
! 70609: Mem **apArg;
! 70610: #endif /* local variables moved into u.cm */
! 70611:
! 70612: u.cm.pQuery = &aMem[pOp->p3];
! 70613: u.cm.pArgc = &u.cm.pQuery[1];
! 70614: u.cm.pCur = p->apCsr[pOp->p1];
! 70615: assert( memIsValid(u.cm.pQuery) );
! 70616: REGISTER_TRACE(pOp->p3, u.cm.pQuery);
! 70617: assert( u.cm.pCur->pVtabCursor );
! 70618: u.cm.pVtabCursor = u.cm.pCur->pVtabCursor;
! 70619: u.cm.pVtab = u.cm.pVtabCursor->pVtab;
! 70620: u.cm.pModule = u.cm.pVtab->pModule;
! 70621:
! 70622: /* Grab the index number and argc parameters */
! 70623: assert( (u.cm.pQuery->flags&MEM_Int)!=0 && u.cm.pArgc->flags==MEM_Int );
! 70624: u.cm.nArg = (int)u.cm.pArgc->u.i;
! 70625: u.cm.iQuery = (int)u.cm.pQuery->u.i;
! 70626:
! 70627: /* Invoke the xFilter method */
! 70628: {
! 70629: u.cm.res = 0;
! 70630: u.cm.apArg = p->apArg;
! 70631: for(u.cm.i = 0; u.cm.i<u.cm.nArg; u.cm.i++){
! 70632: u.cm.apArg[u.cm.i] = &u.cm.pArgc[u.cm.i+1];
! 70633: sqlite3VdbeMemStoreType(u.cm.apArg[u.cm.i]);
! 70634: }
! 70635:
! 70636: p->inVtabMethod = 1;
! 70637: rc = u.cm.pModule->xFilter(u.cm.pVtabCursor, u.cm.iQuery, pOp->p4.z, u.cm.nArg, u.cm.apArg);
! 70638: p->inVtabMethod = 0;
! 70639: importVtabErrMsg(p, u.cm.pVtab);
! 70640: if( rc==SQLITE_OK ){
! 70641: u.cm.res = u.cm.pModule->xEof(u.cm.pVtabCursor);
! 70642: }
! 70643:
! 70644: if( u.cm.res ){
! 70645: pc = pOp->p2 - 1;
! 70646: }
! 70647: }
! 70648: u.cm.pCur->nullRow = 0;
! 70649:
! 70650: break;
! 70651: }
! 70652: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 70653:
! 70654: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 70655: /* Opcode: VColumn P1 P2 P3 * *
! 70656: **
! 70657: ** Store the value of the P2-th column of
! 70658: ** the row of the virtual-table that the
! 70659: ** P1 cursor is pointing to into register P3.
! 70660: */
! 70661: case OP_VColumn: {
! 70662: #if 0 /* local variables moved into u.cn */
! 70663: sqlite3_vtab *pVtab;
! 70664: const sqlite3_module *pModule;
! 70665: Mem *pDest;
! 70666: sqlite3_context sContext;
! 70667: #endif /* local variables moved into u.cn */
! 70668:
! 70669: VdbeCursor *pCur = p->apCsr[pOp->p1];
! 70670: assert( pCur->pVtabCursor );
! 70671: assert( pOp->p3>0 && pOp->p3<=p->nMem );
! 70672: u.cn.pDest = &aMem[pOp->p3];
! 70673: memAboutToChange(p, u.cn.pDest);
! 70674: if( pCur->nullRow ){
! 70675: sqlite3VdbeMemSetNull(u.cn.pDest);
! 70676: break;
! 70677: }
! 70678: u.cn.pVtab = pCur->pVtabCursor->pVtab;
! 70679: u.cn.pModule = u.cn.pVtab->pModule;
! 70680: assert( u.cn.pModule->xColumn );
! 70681: memset(&u.cn.sContext, 0, sizeof(u.cn.sContext));
! 70682:
! 70683: /* The output cell may already have a buffer allocated. Move
! 70684: ** the current contents to u.cn.sContext.s so in case the user-function
! 70685: ** can use the already allocated buffer instead of allocating a
! 70686: ** new one.
! 70687: */
! 70688: sqlite3VdbeMemMove(&u.cn.sContext.s, u.cn.pDest);
! 70689: MemSetTypeFlag(&u.cn.sContext.s, MEM_Null);
! 70690:
! 70691: rc = u.cn.pModule->xColumn(pCur->pVtabCursor, &u.cn.sContext, pOp->p2);
! 70692: importVtabErrMsg(p, u.cn.pVtab);
! 70693: if( u.cn.sContext.isError ){
! 70694: rc = u.cn.sContext.isError;
! 70695: }
! 70696:
! 70697: /* Copy the result of the function to the P3 register. We
! 70698: ** do this regardless of whether or not an error occurred to ensure any
! 70699: ** dynamic allocation in u.cn.sContext.s (a Mem struct) is released.
! 70700: */
! 70701: sqlite3VdbeChangeEncoding(&u.cn.sContext.s, encoding);
! 70702: sqlite3VdbeMemMove(u.cn.pDest, &u.cn.sContext.s);
! 70703: REGISTER_TRACE(pOp->p3, u.cn.pDest);
! 70704: UPDATE_MAX_BLOBSIZE(u.cn.pDest);
! 70705:
! 70706: if( sqlite3VdbeMemTooBig(u.cn.pDest) ){
! 70707: goto too_big;
! 70708: }
! 70709: break;
! 70710: }
! 70711: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 70712:
! 70713: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 70714: /* Opcode: VNext P1 P2 * * *
! 70715: **
! 70716: ** Advance virtual table P1 to the next row in its result set and
! 70717: ** jump to instruction P2. Or, if the virtual table has reached
! 70718: ** the end of its result set, then fall through to the next instruction.
! 70719: */
! 70720: case OP_VNext: { /* jump */
! 70721: #if 0 /* local variables moved into u.co */
! 70722: sqlite3_vtab *pVtab;
! 70723: const sqlite3_module *pModule;
! 70724: int res;
! 70725: VdbeCursor *pCur;
! 70726: #endif /* local variables moved into u.co */
! 70727:
! 70728: u.co.res = 0;
! 70729: u.co.pCur = p->apCsr[pOp->p1];
! 70730: assert( u.co.pCur->pVtabCursor );
! 70731: if( u.co.pCur->nullRow ){
! 70732: break;
! 70733: }
! 70734: u.co.pVtab = u.co.pCur->pVtabCursor->pVtab;
! 70735: u.co.pModule = u.co.pVtab->pModule;
! 70736: assert( u.co.pModule->xNext );
! 70737:
! 70738: /* Invoke the xNext() method of the module. There is no way for the
! 70739: ** underlying implementation to return an error if one occurs during
! 70740: ** xNext(). Instead, if an error occurs, true is returned (indicating that
! 70741: ** data is available) and the error code returned when xColumn or
! 70742: ** some other method is next invoked on the save virtual table cursor.
! 70743: */
! 70744: p->inVtabMethod = 1;
! 70745: rc = u.co.pModule->xNext(u.co.pCur->pVtabCursor);
! 70746: p->inVtabMethod = 0;
! 70747: importVtabErrMsg(p, u.co.pVtab);
! 70748: if( rc==SQLITE_OK ){
! 70749: u.co.res = u.co.pModule->xEof(u.co.pCur->pVtabCursor);
! 70750: }
! 70751:
! 70752: if( !u.co.res ){
! 70753: /* If there is data, jump to P2 */
! 70754: pc = pOp->p2 - 1;
! 70755: }
! 70756: break;
! 70757: }
! 70758: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 70759:
! 70760: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 70761: /* Opcode: VRename P1 * * P4 *
! 70762: **
! 70763: ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
! 70764: ** This opcode invokes the corresponding xRename method. The value
! 70765: ** in register P1 is passed as the zName argument to the xRename method.
! 70766: */
! 70767: case OP_VRename: {
! 70768: #if 0 /* local variables moved into u.cp */
! 70769: sqlite3_vtab *pVtab;
! 70770: Mem *pName;
! 70771: #endif /* local variables moved into u.cp */
! 70772:
! 70773: u.cp.pVtab = pOp->p4.pVtab->pVtab;
! 70774: u.cp.pName = &aMem[pOp->p1];
! 70775: assert( u.cp.pVtab->pModule->xRename );
! 70776: assert( memIsValid(u.cp.pName) );
! 70777: REGISTER_TRACE(pOp->p1, u.cp.pName);
! 70778: assert( u.cp.pName->flags & MEM_Str );
! 70779: testcase( u.cp.pName->enc==SQLITE_UTF8 );
! 70780: testcase( u.cp.pName->enc==SQLITE_UTF16BE );
! 70781: testcase( u.cp.pName->enc==SQLITE_UTF16LE );
! 70782: rc = sqlite3VdbeChangeEncoding(u.cp.pName, SQLITE_UTF8);
! 70783: if( rc==SQLITE_OK ){
! 70784: rc = u.cp.pVtab->pModule->xRename(u.cp.pVtab, u.cp.pName->z);
! 70785: importVtabErrMsg(p, u.cp.pVtab);
! 70786: p->expired = 0;
! 70787: }
! 70788: break;
! 70789: }
! 70790: #endif
! 70791:
! 70792: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 70793: /* Opcode: VUpdate P1 P2 P3 P4 *
! 70794: **
! 70795: ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
! 70796: ** This opcode invokes the corresponding xUpdate method. P2 values
! 70797: ** are contiguous memory cells starting at P3 to pass to the xUpdate
! 70798: ** invocation. The value in register (P3+P2-1) corresponds to the
! 70799: ** p2th element of the argv array passed to xUpdate.
! 70800: **
! 70801: ** The xUpdate method will do a DELETE or an INSERT or both.
! 70802: ** The argv[0] element (which corresponds to memory cell P3)
! 70803: ** is the rowid of a row to delete. If argv[0] is NULL then no
! 70804: ** deletion occurs. The argv[1] element is the rowid of the new
! 70805: ** row. This can be NULL to have the virtual table select the new
! 70806: ** rowid for itself. The subsequent elements in the array are
! 70807: ** the values of columns in the new row.
! 70808: **
! 70809: ** If P2==1 then no insert is performed. argv[0] is the rowid of
! 70810: ** a row to delete.
! 70811: **
! 70812: ** P1 is a boolean flag. If it is set to true and the xUpdate call
! 70813: ** is successful, then the value returned by sqlite3_last_insert_rowid()
! 70814: ** is set to the value of the rowid for the row just inserted.
! 70815: */
! 70816: case OP_VUpdate: {
! 70817: #if 0 /* local variables moved into u.cq */
! 70818: sqlite3_vtab *pVtab;
! 70819: sqlite3_module *pModule;
! 70820: int nArg;
! 70821: int i;
! 70822: sqlite_int64 rowid;
! 70823: Mem **apArg;
! 70824: Mem *pX;
! 70825: #endif /* local variables moved into u.cq */
! 70826:
! 70827: assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
! 70828: || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
! 70829: );
! 70830: u.cq.pVtab = pOp->p4.pVtab->pVtab;
! 70831: u.cq.pModule = (sqlite3_module *)u.cq.pVtab->pModule;
! 70832: u.cq.nArg = pOp->p2;
! 70833: assert( pOp->p4type==P4_VTAB );
! 70834: if( ALWAYS(u.cq.pModule->xUpdate) ){
! 70835: u8 vtabOnConflict = db->vtabOnConflict;
! 70836: u.cq.apArg = p->apArg;
! 70837: u.cq.pX = &aMem[pOp->p3];
! 70838: for(u.cq.i=0; u.cq.i<u.cq.nArg; u.cq.i++){
! 70839: assert( memIsValid(u.cq.pX) );
! 70840: memAboutToChange(p, u.cq.pX);
! 70841: sqlite3VdbeMemStoreType(u.cq.pX);
! 70842: u.cq.apArg[u.cq.i] = u.cq.pX;
! 70843: u.cq.pX++;
! 70844: }
! 70845: db->vtabOnConflict = pOp->p5;
! 70846: rc = u.cq.pModule->xUpdate(u.cq.pVtab, u.cq.nArg, u.cq.apArg, &u.cq.rowid);
! 70847: db->vtabOnConflict = vtabOnConflict;
! 70848: importVtabErrMsg(p, u.cq.pVtab);
! 70849: if( rc==SQLITE_OK && pOp->p1 ){
! 70850: assert( u.cq.nArg>1 && u.cq.apArg[0] && (u.cq.apArg[0]->flags&MEM_Null) );
! 70851: db->lastRowid = lastRowid = u.cq.rowid;
! 70852: }
! 70853: if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
! 70854: if( pOp->p5==OE_Ignore ){
! 70855: rc = SQLITE_OK;
! 70856: }else{
! 70857: p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
! 70858: }
! 70859: }else{
! 70860: p->nChange++;
! 70861: }
! 70862: }
! 70863: break;
! 70864: }
! 70865: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 70866:
! 70867: #ifndef SQLITE_OMIT_PAGER_PRAGMAS
! 70868: /* Opcode: Pagecount P1 P2 * * *
! 70869: **
! 70870: ** Write the current number of pages in database P1 to memory cell P2.
! 70871: */
! 70872: case OP_Pagecount: { /* out2-prerelease */
! 70873: pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
! 70874: break;
! 70875: }
! 70876: #endif
! 70877:
! 70878:
! 70879: #ifndef SQLITE_OMIT_PAGER_PRAGMAS
! 70880: /* Opcode: MaxPgcnt P1 P2 P3 * *
! 70881: **
! 70882: ** Try to set the maximum page count for database P1 to the value in P3.
! 70883: ** Do not let the maximum page count fall below the current page count and
! 70884: ** do not change the maximum page count value if P3==0.
! 70885: **
! 70886: ** Store the maximum page count after the change in register P2.
! 70887: */
! 70888: case OP_MaxPgcnt: { /* out2-prerelease */
! 70889: unsigned int newMax;
! 70890: Btree *pBt;
! 70891:
! 70892: pBt = db->aDb[pOp->p1].pBt;
! 70893: newMax = 0;
! 70894: if( pOp->p3 ){
! 70895: newMax = sqlite3BtreeLastPage(pBt);
! 70896: if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
! 70897: }
! 70898: pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
! 70899: break;
! 70900: }
! 70901: #endif
! 70902:
! 70903:
! 70904: #ifndef SQLITE_OMIT_TRACE
! 70905: /* Opcode: Trace * * * P4 *
! 70906: **
! 70907: ** If tracing is enabled (by the sqlite3_trace()) interface, then
! 70908: ** the UTF-8 string contained in P4 is emitted on the trace callback.
! 70909: */
! 70910: case OP_Trace: {
! 70911: #if 0 /* local variables moved into u.cr */
! 70912: char *zTrace;
! 70913: char *z;
! 70914: #endif /* local variables moved into u.cr */
! 70915:
! 70916: if( db->xTrace && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
! 70917: u.cr.z = sqlite3VdbeExpandSql(p, u.cr.zTrace);
! 70918: db->xTrace(db->pTraceArg, u.cr.z);
! 70919: sqlite3DbFree(db, u.cr.z);
! 70920: }
! 70921: #ifdef SQLITE_DEBUG
! 70922: if( (db->flags & SQLITE_SqlTrace)!=0
! 70923: && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
! 70924: ){
! 70925: sqlite3DebugPrintf("SQL-trace: %s\n", u.cr.zTrace);
! 70926: }
! 70927: #endif /* SQLITE_DEBUG */
! 70928: break;
! 70929: }
! 70930: #endif
! 70931:
! 70932:
! 70933: /* Opcode: Noop * * * * *
! 70934: **
! 70935: ** Do nothing. This instruction is often useful as a jump
! 70936: ** destination.
! 70937: */
! 70938: /*
! 70939: ** The magic Explain opcode are only inserted when explain==2 (which
! 70940: ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
! 70941: ** This opcode records information from the optimizer. It is the
! 70942: ** the same as a no-op. This opcodesnever appears in a real VM program.
! 70943: */
! 70944: default: { /* This is really OP_Noop and OP_Explain */
! 70945: assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
! 70946: break;
! 70947: }
! 70948:
! 70949: /*****************************************************************************
! 70950: ** The cases of the switch statement above this line should all be indented
! 70951: ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
! 70952: ** readability. From this point on down, the normal indentation rules are
! 70953: ** restored.
! 70954: *****************************************************************************/
! 70955: }
! 70956:
! 70957: #ifdef VDBE_PROFILE
! 70958: {
! 70959: u64 elapsed = sqlite3Hwtime() - start;
! 70960: pOp->cycles += elapsed;
! 70961: pOp->cnt++;
! 70962: #if 0
! 70963: fprintf(stdout, "%10llu ", elapsed);
! 70964: sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
! 70965: #endif
! 70966: }
! 70967: #endif
! 70968:
! 70969: /* The following code adds nothing to the actual functionality
! 70970: ** of the program. It is only here for testing and debugging.
! 70971: ** On the other hand, it does burn CPU cycles every time through
! 70972: ** the evaluator loop. So we can leave it out when NDEBUG is defined.
! 70973: */
! 70974: #ifndef NDEBUG
! 70975: assert( pc>=-1 && pc<p->nOp );
! 70976:
! 70977: #ifdef SQLITE_DEBUG
! 70978: if( p->trace ){
! 70979: if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
! 70980: if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
! 70981: registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
! 70982: }
! 70983: if( pOp->opflags & OPFLG_OUT3 ){
! 70984: registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
! 70985: }
! 70986: }
! 70987: #endif /* SQLITE_DEBUG */
! 70988: #endif /* NDEBUG */
! 70989: } /* The end of the for(;;) loop the loops through opcodes */
! 70990:
! 70991: /* If we reach this point, it means that execution is finished with
! 70992: ** an error of some kind.
! 70993: */
! 70994: vdbe_error_halt:
! 70995: assert( rc );
! 70996: p->rc = rc;
! 70997: testcase( sqlite3GlobalConfig.xLog!=0 );
! 70998: sqlite3_log(rc, "statement aborts at %d: [%s] %s",
! 70999: pc, p->zSql, p->zErrMsg);
! 71000: sqlite3VdbeHalt(p);
! 71001: if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
! 71002: rc = SQLITE_ERROR;
! 71003: if( resetSchemaOnFault>0 ){
! 71004: sqlite3ResetInternalSchema(db, resetSchemaOnFault-1);
! 71005: }
! 71006:
! 71007: /* This is the only way out of this procedure. We have to
! 71008: ** release the mutexes on btrees that were acquired at the
! 71009: ** top. */
! 71010: vdbe_return:
! 71011: db->lastRowid = lastRowid;
! 71012: sqlite3VdbeLeave(p);
! 71013: return rc;
! 71014:
! 71015: /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
! 71016: ** is encountered.
! 71017: */
! 71018: too_big:
! 71019: sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
! 71020: rc = SQLITE_TOOBIG;
! 71021: goto vdbe_error_halt;
! 71022:
! 71023: /* Jump to here if a malloc() fails.
! 71024: */
! 71025: no_mem:
! 71026: db->mallocFailed = 1;
! 71027: sqlite3SetString(&p->zErrMsg, db, "out of memory");
! 71028: rc = SQLITE_NOMEM;
! 71029: goto vdbe_error_halt;
! 71030:
! 71031: /* Jump to here for any other kind of fatal error. The "rc" variable
! 71032: ** should hold the error number.
! 71033: */
! 71034: abort_due_to_error:
! 71035: assert( p->zErrMsg==0 );
! 71036: if( db->mallocFailed ) rc = SQLITE_NOMEM;
! 71037: if( rc!=SQLITE_IOERR_NOMEM ){
! 71038: sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
! 71039: }
! 71040: goto vdbe_error_halt;
! 71041:
! 71042: /* Jump to here if the sqlite3_interrupt() API sets the interrupt
! 71043: ** flag.
! 71044: */
! 71045: abort_due_to_interrupt:
! 71046: assert( db->u1.isInterrupted );
! 71047: rc = SQLITE_INTERRUPT;
! 71048: p->rc = rc;
! 71049: sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
! 71050: goto vdbe_error_halt;
! 71051: }
! 71052:
! 71053: /************** End of vdbe.c ************************************************/
! 71054: /************** Begin file vdbeblob.c ****************************************/
! 71055: /*
! 71056: ** 2007 May 1
! 71057: **
! 71058: ** The author disclaims copyright to this source code. In place of
! 71059: ** a legal notice, here is a blessing:
! 71060: **
! 71061: ** May you do good and not evil.
! 71062: ** May you find forgiveness for yourself and forgive others.
! 71063: ** May you share freely, never taking more than you give.
! 71064: **
! 71065: *************************************************************************
! 71066: **
! 71067: ** This file contains code used to implement incremental BLOB I/O.
! 71068: */
! 71069:
! 71070:
! 71071: #ifndef SQLITE_OMIT_INCRBLOB
! 71072:
! 71073: /*
! 71074: ** Valid sqlite3_blob* handles point to Incrblob structures.
! 71075: */
! 71076: typedef struct Incrblob Incrblob;
! 71077: struct Incrblob {
! 71078: int flags; /* Copy of "flags" passed to sqlite3_blob_open() */
! 71079: int nByte; /* Size of open blob, in bytes */
! 71080: int iOffset; /* Byte offset of blob in cursor data */
! 71081: int iCol; /* Table column this handle is open on */
! 71082: BtCursor *pCsr; /* Cursor pointing at blob row */
! 71083: sqlite3_stmt *pStmt; /* Statement holding cursor open */
! 71084: sqlite3 *db; /* The associated database */
! 71085: };
! 71086:
! 71087:
! 71088: /*
! 71089: ** This function is used by both blob_open() and blob_reopen(). It seeks
! 71090: ** the b-tree cursor associated with blob handle p to point to row iRow.
! 71091: ** If successful, SQLITE_OK is returned and subsequent calls to
! 71092: ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
! 71093: **
! 71094: ** If an error occurs, or if the specified row does not exist or does not
! 71095: ** contain a value of type TEXT or BLOB in the column nominated when the
! 71096: ** blob handle was opened, then an error code is returned and *pzErr may
! 71097: ** be set to point to a buffer containing an error message. It is the
! 71098: ** responsibility of the caller to free the error message buffer using
! 71099: ** sqlite3DbFree().
! 71100: **
! 71101: ** If an error does occur, then the b-tree cursor is closed. All subsequent
! 71102: ** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
! 71103: ** immediately return SQLITE_ABORT.
! 71104: */
! 71105: static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
! 71106: int rc; /* Error code */
! 71107: char *zErr = 0; /* Error message */
! 71108: Vdbe *v = (Vdbe *)p->pStmt;
! 71109:
! 71110: /* Set the value of the SQL statements only variable to integer iRow.
! 71111: ** This is done directly instead of using sqlite3_bind_int64() to avoid
! 71112: ** triggering asserts related to mutexes.
! 71113: */
! 71114: assert( v->aVar[0].flags&MEM_Int );
! 71115: v->aVar[0].u.i = iRow;
! 71116:
! 71117: rc = sqlite3_step(p->pStmt);
! 71118: if( rc==SQLITE_ROW ){
! 71119: u32 type = v->apCsr[0]->aType[p->iCol];
! 71120: if( type<12 ){
! 71121: zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
! 71122: type==0?"null": type==7?"real": "integer"
! 71123: );
! 71124: rc = SQLITE_ERROR;
! 71125: sqlite3_finalize(p->pStmt);
! 71126: p->pStmt = 0;
! 71127: }else{
! 71128: p->iOffset = v->apCsr[0]->aOffset[p->iCol];
! 71129: p->nByte = sqlite3VdbeSerialTypeLen(type);
! 71130: p->pCsr = v->apCsr[0]->pCursor;
! 71131: sqlite3BtreeEnterCursor(p->pCsr);
! 71132: sqlite3BtreeCacheOverflow(p->pCsr);
! 71133: sqlite3BtreeLeaveCursor(p->pCsr);
! 71134: }
! 71135: }
! 71136:
! 71137: if( rc==SQLITE_ROW ){
! 71138: rc = SQLITE_OK;
! 71139: }else if( p->pStmt ){
! 71140: rc = sqlite3_finalize(p->pStmt);
! 71141: p->pStmt = 0;
! 71142: if( rc==SQLITE_OK ){
! 71143: zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
! 71144: rc = SQLITE_ERROR;
! 71145: }else{
! 71146: zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
! 71147: }
! 71148: }
! 71149:
! 71150: assert( rc!=SQLITE_OK || zErr==0 );
! 71151: assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
! 71152:
! 71153: *pzErr = zErr;
! 71154: return rc;
! 71155: }
! 71156:
! 71157: /*
! 71158: ** Open a blob handle.
! 71159: */
! 71160: SQLITE_API int sqlite3_blob_open(
! 71161: sqlite3* db, /* The database connection */
! 71162: const char *zDb, /* The attached database containing the blob */
! 71163: const char *zTable, /* The table containing the blob */
! 71164: const char *zColumn, /* The column containing the blob */
! 71165: sqlite_int64 iRow, /* The row containing the glob */
! 71166: int flags, /* True -> read/write access, false -> read-only */
! 71167: sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
! 71168: ){
! 71169: int nAttempt = 0;
! 71170: int iCol; /* Index of zColumn in row-record */
! 71171:
! 71172: /* This VDBE program seeks a btree cursor to the identified
! 71173: ** db/table/row entry. The reason for using a vdbe program instead
! 71174: ** of writing code to use the b-tree layer directly is that the
! 71175: ** vdbe program will take advantage of the various transaction,
! 71176: ** locking and error handling infrastructure built into the vdbe.
! 71177: **
! 71178: ** After seeking the cursor, the vdbe executes an OP_ResultRow.
! 71179: ** Code external to the Vdbe then "borrows" the b-tree cursor and
! 71180: ** uses it to implement the blob_read(), blob_write() and
! 71181: ** blob_bytes() functions.
! 71182: **
! 71183: ** The sqlite3_blob_close() function finalizes the vdbe program,
! 71184: ** which closes the b-tree cursor and (possibly) commits the
! 71185: ** transaction.
! 71186: */
! 71187: static const VdbeOpList openBlob[] = {
! 71188: {OP_Transaction, 0, 0, 0}, /* 0: Start a transaction */
! 71189: {OP_VerifyCookie, 0, 0, 0}, /* 1: Check the schema cookie */
! 71190: {OP_TableLock, 0, 0, 0}, /* 2: Acquire a read or write lock */
! 71191:
! 71192: /* One of the following two instructions is replaced by an OP_Noop. */
! 71193: {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */
! 71194: {OP_OpenWrite, 0, 0, 0}, /* 4: Open cursor 0 for read/write */
! 71195:
! 71196: {OP_Variable, 1, 1, 1}, /* 5: Push the rowid to the stack */
! 71197: {OP_NotExists, 0, 10, 1}, /* 6: Seek the cursor */
! 71198: {OP_Column, 0, 0, 1}, /* 7 */
! 71199: {OP_ResultRow, 1, 0, 0}, /* 8 */
! 71200: {OP_Goto, 0, 5, 0}, /* 9 */
! 71201: {OP_Close, 0, 0, 0}, /* 10 */
! 71202: {OP_Halt, 0, 0, 0}, /* 11 */
! 71203: };
! 71204:
! 71205: int rc = SQLITE_OK;
! 71206: char *zErr = 0;
! 71207: Table *pTab;
! 71208: Parse *pParse = 0;
! 71209: Incrblob *pBlob = 0;
! 71210:
! 71211: flags = !!flags; /* flags = (flags ? 1 : 0); */
! 71212: *ppBlob = 0;
! 71213:
! 71214: sqlite3_mutex_enter(db->mutex);
! 71215:
! 71216: pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
! 71217: if( !pBlob ) goto blob_open_out;
! 71218: pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
! 71219: if( !pParse ) goto blob_open_out;
! 71220:
! 71221: do {
! 71222: memset(pParse, 0, sizeof(Parse));
! 71223: pParse->db = db;
! 71224: sqlite3DbFree(db, zErr);
! 71225: zErr = 0;
! 71226:
! 71227: sqlite3BtreeEnterAll(db);
! 71228: pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
! 71229: if( pTab && IsVirtual(pTab) ){
! 71230: pTab = 0;
! 71231: sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
! 71232: }
! 71233: #ifndef SQLITE_OMIT_VIEW
! 71234: if( pTab && pTab->pSelect ){
! 71235: pTab = 0;
! 71236: sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
! 71237: }
! 71238: #endif
! 71239: if( !pTab ){
! 71240: if( pParse->zErrMsg ){
! 71241: sqlite3DbFree(db, zErr);
! 71242: zErr = pParse->zErrMsg;
! 71243: pParse->zErrMsg = 0;
! 71244: }
! 71245: rc = SQLITE_ERROR;
! 71246: sqlite3BtreeLeaveAll(db);
! 71247: goto blob_open_out;
! 71248: }
! 71249:
! 71250: /* Now search pTab for the exact column. */
! 71251: for(iCol=0; iCol<pTab->nCol; iCol++) {
! 71252: if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
! 71253: break;
! 71254: }
! 71255: }
! 71256: if( iCol==pTab->nCol ){
! 71257: sqlite3DbFree(db, zErr);
! 71258: zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
! 71259: rc = SQLITE_ERROR;
! 71260: sqlite3BtreeLeaveAll(db);
! 71261: goto blob_open_out;
! 71262: }
! 71263:
! 71264: /* If the value is being opened for writing, check that the
! 71265: ** column is not indexed, and that it is not part of a foreign key.
! 71266: ** It is against the rules to open a column to which either of these
! 71267: ** descriptions applies for writing. */
! 71268: if( flags ){
! 71269: const char *zFault = 0;
! 71270: Index *pIdx;
! 71271: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 71272: if( db->flags&SQLITE_ForeignKeys ){
! 71273: /* Check that the column is not part of an FK child key definition. It
! 71274: ** is not necessary to check if it is part of a parent key, as parent
! 71275: ** key columns must be indexed. The check below will pick up this
! 71276: ** case. */
! 71277: FKey *pFKey;
! 71278: for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
! 71279: int j;
! 71280: for(j=0; j<pFKey->nCol; j++){
! 71281: if( pFKey->aCol[j].iFrom==iCol ){
! 71282: zFault = "foreign key";
! 71283: }
! 71284: }
! 71285: }
! 71286: }
! 71287: #endif
! 71288: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
! 71289: int j;
! 71290: for(j=0; j<pIdx->nColumn; j++){
! 71291: if( pIdx->aiColumn[j]==iCol ){
! 71292: zFault = "indexed";
! 71293: }
! 71294: }
! 71295: }
! 71296: if( zFault ){
! 71297: sqlite3DbFree(db, zErr);
! 71298: zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
! 71299: rc = SQLITE_ERROR;
! 71300: sqlite3BtreeLeaveAll(db);
! 71301: goto blob_open_out;
! 71302: }
! 71303: }
! 71304:
! 71305: pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(db);
! 71306: assert( pBlob->pStmt || db->mallocFailed );
! 71307: if( pBlob->pStmt ){
! 71308: Vdbe *v = (Vdbe *)pBlob->pStmt;
! 71309: int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 71310:
! 71311: sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
! 71312:
! 71313:
! 71314: /* Configure the OP_Transaction */
! 71315: sqlite3VdbeChangeP1(v, 0, iDb);
! 71316: sqlite3VdbeChangeP2(v, 0, flags);
! 71317:
! 71318: /* Configure the OP_VerifyCookie */
! 71319: sqlite3VdbeChangeP1(v, 1, iDb);
! 71320: sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
! 71321: sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
! 71322:
! 71323: /* Make sure a mutex is held on the table to be accessed */
! 71324: sqlite3VdbeUsesBtree(v, iDb);
! 71325:
! 71326: /* Configure the OP_TableLock instruction */
! 71327: #ifdef SQLITE_OMIT_SHARED_CACHE
! 71328: sqlite3VdbeChangeToNoop(v, 2);
! 71329: #else
! 71330: sqlite3VdbeChangeP1(v, 2, iDb);
! 71331: sqlite3VdbeChangeP2(v, 2, pTab->tnum);
! 71332: sqlite3VdbeChangeP3(v, 2, flags);
! 71333: sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
! 71334: #endif
! 71335:
! 71336: /* Remove either the OP_OpenWrite or OpenRead. Set the P2
! 71337: ** parameter of the other to pTab->tnum. */
! 71338: sqlite3VdbeChangeToNoop(v, 4 - flags);
! 71339: sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
! 71340: sqlite3VdbeChangeP3(v, 3 + flags, iDb);
! 71341:
! 71342: /* Configure the number of columns. Configure the cursor to
! 71343: ** think that the table has one more column than it really
! 71344: ** does. An OP_Column to retrieve this imaginary column will
! 71345: ** always return an SQL NULL. This is useful because it means
! 71346: ** we can invoke OP_Column to fill in the vdbe cursors type
! 71347: ** and offset cache without causing any IO.
! 71348: */
! 71349: sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
! 71350: sqlite3VdbeChangeP2(v, 7, pTab->nCol);
! 71351: if( !db->mallocFailed ){
! 71352: pParse->nVar = 1;
! 71353: pParse->nMem = 1;
! 71354: pParse->nTab = 1;
! 71355: sqlite3VdbeMakeReady(v, pParse);
! 71356: }
! 71357: }
! 71358:
! 71359: pBlob->flags = flags;
! 71360: pBlob->iCol = iCol;
! 71361: pBlob->db = db;
! 71362: sqlite3BtreeLeaveAll(db);
! 71363: if( db->mallocFailed ){
! 71364: goto blob_open_out;
! 71365: }
! 71366: sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
! 71367: rc = blobSeekToRow(pBlob, iRow, &zErr);
! 71368: } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
! 71369:
! 71370: blob_open_out:
! 71371: if( rc==SQLITE_OK && db->mallocFailed==0 ){
! 71372: *ppBlob = (sqlite3_blob *)pBlob;
! 71373: }else{
! 71374: if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
! 71375: sqlite3DbFree(db, pBlob);
! 71376: }
! 71377: sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
! 71378: sqlite3DbFree(db, zErr);
! 71379: sqlite3StackFree(db, pParse);
! 71380: rc = sqlite3ApiExit(db, rc);
! 71381: sqlite3_mutex_leave(db->mutex);
! 71382: return rc;
! 71383: }
! 71384:
! 71385: /*
! 71386: ** Close a blob handle that was previously created using
! 71387: ** sqlite3_blob_open().
! 71388: */
! 71389: SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
! 71390: Incrblob *p = (Incrblob *)pBlob;
! 71391: int rc;
! 71392: sqlite3 *db;
! 71393:
! 71394: if( p ){
! 71395: db = p->db;
! 71396: sqlite3_mutex_enter(db->mutex);
! 71397: rc = sqlite3_finalize(p->pStmt);
! 71398: sqlite3DbFree(db, p);
! 71399: sqlite3_mutex_leave(db->mutex);
! 71400: }else{
! 71401: rc = SQLITE_OK;
! 71402: }
! 71403: return rc;
! 71404: }
! 71405:
! 71406: /*
! 71407: ** Perform a read or write operation on a blob
! 71408: */
! 71409: static int blobReadWrite(
! 71410: sqlite3_blob *pBlob,
! 71411: void *z,
! 71412: int n,
! 71413: int iOffset,
! 71414: int (*xCall)(BtCursor*, u32, u32, void*)
! 71415: ){
! 71416: int rc;
! 71417: Incrblob *p = (Incrblob *)pBlob;
! 71418: Vdbe *v;
! 71419: sqlite3 *db;
! 71420:
! 71421: if( p==0 ) return SQLITE_MISUSE_BKPT;
! 71422: db = p->db;
! 71423: sqlite3_mutex_enter(db->mutex);
! 71424: v = (Vdbe*)p->pStmt;
! 71425:
! 71426: if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
! 71427: /* Request is out of range. Return a transient error. */
! 71428: rc = SQLITE_ERROR;
! 71429: sqlite3Error(db, SQLITE_ERROR, 0);
! 71430: }else if( v==0 ){
! 71431: /* If there is no statement handle, then the blob-handle has
! 71432: ** already been invalidated. Return SQLITE_ABORT in this case.
! 71433: */
! 71434: rc = SQLITE_ABORT;
! 71435: }else{
! 71436: /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
! 71437: ** returned, clean-up the statement handle.
! 71438: */
! 71439: assert( db == v->db );
! 71440: sqlite3BtreeEnterCursor(p->pCsr);
! 71441: rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
! 71442: sqlite3BtreeLeaveCursor(p->pCsr);
! 71443: if( rc==SQLITE_ABORT ){
! 71444: sqlite3VdbeFinalize(v);
! 71445: p->pStmt = 0;
! 71446: }else{
! 71447: db->errCode = rc;
! 71448: v->rc = rc;
! 71449: }
! 71450: }
! 71451: rc = sqlite3ApiExit(db, rc);
! 71452: sqlite3_mutex_leave(db->mutex);
! 71453: return rc;
! 71454: }
! 71455:
! 71456: /*
! 71457: ** Read data from a blob handle.
! 71458: */
! 71459: SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
! 71460: return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
! 71461: }
! 71462:
! 71463: /*
! 71464: ** Write data to a blob handle.
! 71465: */
! 71466: SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
! 71467: return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
! 71468: }
! 71469:
! 71470: /*
! 71471: ** Query a blob handle for the size of the data.
! 71472: **
! 71473: ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
! 71474: ** so no mutex is required for access.
! 71475: */
! 71476: SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
! 71477: Incrblob *p = (Incrblob *)pBlob;
! 71478: return (p && p->pStmt) ? p->nByte : 0;
! 71479: }
! 71480:
! 71481: /*
! 71482: ** Move an existing blob handle to point to a different row of the same
! 71483: ** database table.
! 71484: **
! 71485: ** If an error occurs, or if the specified row does not exist or does not
! 71486: ** contain a blob or text value, then an error code is returned and the
! 71487: ** database handle error code and message set. If this happens, then all
! 71488: ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
! 71489: ** immediately return SQLITE_ABORT.
! 71490: */
! 71491: SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
! 71492: int rc;
! 71493: Incrblob *p = (Incrblob *)pBlob;
! 71494: sqlite3 *db;
! 71495:
! 71496: if( p==0 ) return SQLITE_MISUSE_BKPT;
! 71497: db = p->db;
! 71498: sqlite3_mutex_enter(db->mutex);
! 71499:
! 71500: if( p->pStmt==0 ){
! 71501: /* If there is no statement handle, then the blob-handle has
! 71502: ** already been invalidated. Return SQLITE_ABORT in this case.
! 71503: */
! 71504: rc = SQLITE_ABORT;
! 71505: }else{
! 71506: char *zErr;
! 71507: rc = blobSeekToRow(p, iRow, &zErr);
! 71508: if( rc!=SQLITE_OK ){
! 71509: sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
! 71510: sqlite3DbFree(db, zErr);
! 71511: }
! 71512: assert( rc!=SQLITE_SCHEMA );
! 71513: }
! 71514:
! 71515: rc = sqlite3ApiExit(db, rc);
! 71516: assert( rc==SQLITE_OK || p->pStmt==0 );
! 71517: sqlite3_mutex_leave(db->mutex);
! 71518: return rc;
! 71519: }
! 71520:
! 71521: #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
! 71522:
! 71523: /************** End of vdbeblob.c ********************************************/
! 71524: /************** Begin file vdbesort.c ****************************************/
! 71525: /*
! 71526: ** 2011 July 9
! 71527: **
! 71528: ** The author disclaims copyright to this source code. In place of
! 71529: ** a legal notice, here is a blessing:
! 71530: **
! 71531: ** May you do good and not evil.
! 71532: ** May you find forgiveness for yourself and forgive others.
! 71533: ** May you share freely, never taking more than you give.
! 71534: **
! 71535: *************************************************************************
! 71536: ** This file contains code for the VdbeSorter object, used in concert with
! 71537: ** a VdbeCursor to sort large numbers of keys (as may be required, for
! 71538: ** example, by CREATE INDEX statements on tables too large to fit in main
! 71539: ** memory).
! 71540: */
! 71541:
! 71542:
! 71543: #ifndef SQLITE_OMIT_MERGE_SORT
! 71544:
! 71545: typedef struct VdbeSorterIter VdbeSorterIter;
! 71546: typedef struct SorterRecord SorterRecord;
! 71547:
! 71548: /*
! 71549: ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
! 71550: **
! 71551: ** As keys are added to the sorter, they are written to disk in a series
! 71552: ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
! 71553: ** the same as the cache-size allowed for temporary databases. In order
! 71554: ** to allow the caller to extract keys from the sorter in sorted order,
! 71555: ** all PMAs currently stored on disk must be merged together. This comment
! 71556: ** describes the data structure used to do so. The structure supports
! 71557: ** merging any number of arrays in a single pass with no redundant comparison
! 71558: ** operations.
! 71559: **
! 71560: ** The aIter[] array contains an iterator for each of the PMAs being merged.
! 71561: ** An aIter[] iterator either points to a valid key or else is at EOF. For
! 71562: ** the purposes of the paragraphs below, we assume that the array is actually
! 71563: ** N elements in size, where N is the smallest power of 2 greater to or equal
! 71564: ** to the number of iterators being merged. The extra aIter[] elements are
! 71565: ** treated as if they are empty (always at EOF).
! 71566: **
! 71567: ** The aTree[] array is also N elements in size. The value of N is stored in
! 71568: ** the VdbeSorter.nTree variable.
! 71569: **
! 71570: ** The final (N/2) elements of aTree[] contain the results of comparing
! 71571: ** pairs of iterator keys together. Element i contains the result of
! 71572: ** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
! 71573: ** aTree element is set to the index of it.
! 71574: **
! 71575: ** For the purposes of this comparison, EOF is considered greater than any
! 71576: ** other key value. If the keys are equal (only possible with two EOF
! 71577: ** values), it doesn't matter which index is stored.
! 71578: **
! 71579: ** The (N/4) elements of aTree[] that preceed the final (N/2) described
! 71580: ** above contains the index of the smallest of each block of 4 iterators.
! 71581: ** And so on. So that aTree[1] contains the index of the iterator that
! 71582: ** currently points to the smallest key value. aTree[0] is unused.
! 71583: **
! 71584: ** Example:
! 71585: **
! 71586: ** aIter[0] -> Banana
! 71587: ** aIter[1] -> Feijoa
! 71588: ** aIter[2] -> Elderberry
! 71589: ** aIter[3] -> Currant
! 71590: ** aIter[4] -> Grapefruit
! 71591: ** aIter[5] -> Apple
! 71592: ** aIter[6] -> Durian
! 71593: ** aIter[7] -> EOF
! 71594: **
! 71595: ** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
! 71596: **
! 71597: ** The current element is "Apple" (the value of the key indicated by
! 71598: ** iterator 5). When the Next() operation is invoked, iterator 5 will
! 71599: ** be advanced to the next key in its segment. Say the next key is
! 71600: ** "Eggplant":
! 71601: **
! 71602: ** aIter[5] -> Eggplant
! 71603: **
! 71604: ** The contents of aTree[] are updated first by comparing the new iterator
! 71605: ** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
! 71606: ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
! 71607: ** The value of iterator 6 - "Durian" - is now smaller than that of iterator
! 71608: ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
! 71609: ** so the value written into element 1 of the array is 0. As follows:
! 71610: **
! 71611: ** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
! 71612: **
! 71613: ** In other words, each time we advance to the next sorter element, log2(N)
! 71614: ** key comparison operations are required, where N is the number of segments
! 71615: ** being merged (rounded up to the next power of 2).
! 71616: */
! 71617: struct VdbeSorter {
! 71618: int nInMemory; /* Current size of pRecord list as PMA */
! 71619: int nTree; /* Used size of aTree/aIter (power of 2) */
! 71620: VdbeSorterIter *aIter; /* Array of iterators to merge */
! 71621: int *aTree; /* Current state of incremental merge */
! 71622: i64 iWriteOff; /* Current write offset within file pTemp1 */
! 71623: i64 iReadOff; /* Current read offset within file pTemp1 */
! 71624: sqlite3_file *pTemp1; /* PMA file 1 */
! 71625: int nPMA; /* Number of PMAs stored in pTemp1 */
! 71626: SorterRecord *pRecord; /* Head of in-memory record list */
! 71627: int mnPmaSize; /* Minimum PMA size, in bytes */
! 71628: int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
! 71629: UnpackedRecord *pUnpacked; /* Used to unpack keys */
! 71630: };
! 71631:
! 71632: /*
! 71633: ** The following type is an iterator for a PMA. It caches the current key in
! 71634: ** variables nKey/aKey. If the iterator is at EOF, pFile==0.
! 71635: */
! 71636: struct VdbeSorterIter {
! 71637: i64 iReadOff; /* Current read offset */
! 71638: i64 iEof; /* 1 byte past EOF for this iterator */
! 71639: sqlite3_file *pFile; /* File iterator is reading from */
! 71640: int nAlloc; /* Bytes of space at aAlloc */
! 71641: u8 *aAlloc; /* Allocated space */
! 71642: int nKey; /* Number of bytes in key */
! 71643: u8 *aKey; /* Pointer to current key */
! 71644: };
! 71645:
! 71646: /*
! 71647: ** A structure to store a single record. All in-memory records are connected
! 71648: ** together into a linked list headed at VdbeSorter.pRecord using the
! 71649: ** SorterRecord.pNext pointer.
! 71650: */
! 71651: struct SorterRecord {
! 71652: void *pVal;
! 71653: int nVal;
! 71654: SorterRecord *pNext;
! 71655: };
! 71656:
! 71657: /* Minimum allowable value for the VdbeSorter.nWorking variable */
! 71658: #define SORTER_MIN_WORKING 10
! 71659:
! 71660: /* Maximum number of segments to merge in a single pass. */
! 71661: #define SORTER_MAX_MERGE_COUNT 16
! 71662:
! 71663: /*
! 71664: ** Free all memory belonging to the VdbeSorterIter object passed as the second
! 71665: ** argument. All structure fields are set to zero before returning.
! 71666: */
! 71667: static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
! 71668: sqlite3DbFree(db, pIter->aAlloc);
! 71669: memset(pIter, 0, sizeof(VdbeSorterIter));
! 71670: }
! 71671:
! 71672: /*
! 71673: ** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
! 71674: ** no error occurs, or an SQLite error code if one does.
! 71675: */
! 71676: static int vdbeSorterIterNext(
! 71677: sqlite3 *db, /* Database handle (for sqlite3DbMalloc() ) */
! 71678: VdbeSorterIter *pIter /* Iterator to advance */
! 71679: ){
! 71680: int rc; /* Return Code */
! 71681: int nRead; /* Number of bytes read */
! 71682: int nRec = 0; /* Size of record in bytes */
! 71683: int iOff = 0; /* Size of serialized size varint in bytes */
! 71684:
! 71685: assert( pIter->iEof>=pIter->iReadOff );
! 71686: if( pIter->iEof-pIter->iReadOff>5 ){
! 71687: nRead = 5;
! 71688: }else{
! 71689: nRead = (int)(pIter->iEof - pIter->iReadOff);
! 71690: }
! 71691: if( nRead<=0 ){
! 71692: /* This is an EOF condition */
! 71693: vdbeSorterIterZero(db, pIter);
! 71694: return SQLITE_OK;
! 71695: }
! 71696:
! 71697: rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
! 71698: if( rc==SQLITE_OK ){
! 71699: iOff = getVarint32(pIter->aAlloc, nRec);
! 71700: if( (iOff+nRec)>nRead ){
! 71701: int nRead2; /* Number of extra bytes to read */
! 71702: if( (iOff+nRec)>pIter->nAlloc ){
! 71703: int nNew = pIter->nAlloc*2;
! 71704: while( (iOff+nRec)>nNew ) nNew = nNew*2;
! 71705: pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
! 71706: if( !pIter->aAlloc ) return SQLITE_NOMEM;
! 71707: pIter->nAlloc = nNew;
! 71708: }
! 71709:
! 71710: nRead2 = iOff + nRec - nRead;
! 71711: rc = sqlite3OsRead(
! 71712: pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
! 71713: );
! 71714: }
! 71715: }
! 71716:
! 71717: assert( rc!=SQLITE_OK || nRec>0 );
! 71718: pIter->iReadOff += iOff+nRec;
! 71719: pIter->nKey = nRec;
! 71720: pIter->aKey = &pIter->aAlloc[iOff];
! 71721: return rc;
! 71722: }
! 71723:
! 71724: /*
! 71725: ** Write a single varint, value iVal, to file-descriptor pFile. Return
! 71726: ** SQLITE_OK if successful, or an SQLite error code if some error occurs.
! 71727: **
! 71728: ** The value of *piOffset when this function is called is used as the byte
! 71729: ** offset in file pFile to write to. Before returning, *piOffset is
! 71730: ** incremented by the number of bytes written.
! 71731: */
! 71732: static int vdbeSorterWriteVarint(
! 71733: sqlite3_file *pFile, /* File to write to */
! 71734: i64 iVal, /* Value to write as a varint */
! 71735: i64 *piOffset /* IN/OUT: Write offset in file pFile */
! 71736: ){
! 71737: u8 aVarint[9]; /* Buffer large enough for a varint */
! 71738: int nVarint; /* Number of used bytes in varint */
! 71739: int rc; /* Result of write() call */
! 71740:
! 71741: nVarint = sqlite3PutVarint(aVarint, iVal);
! 71742: rc = sqlite3OsWrite(pFile, aVarint, nVarint, *piOffset);
! 71743: *piOffset += nVarint;
! 71744:
! 71745: return rc;
! 71746: }
! 71747:
! 71748: /*
! 71749: ** Read a single varint from file-descriptor pFile. Return SQLITE_OK if
! 71750: ** successful, or an SQLite error code if some error occurs.
! 71751: **
! 71752: ** The value of *piOffset when this function is called is used as the
! 71753: ** byte offset in file pFile from whence to read the varint. If successful
! 71754: ** (i.e. if no IO error occurs), then *piOffset is set to the offset of
! 71755: ** the first byte past the end of the varint before returning. *piVal is
! 71756: ** set to the integer value read. If an error occurs, the final values of
! 71757: ** both *piOffset and *piVal are undefined.
! 71758: */
! 71759: static int vdbeSorterReadVarint(
! 71760: sqlite3_file *pFile, /* File to read from */
! 71761: i64 *piOffset, /* IN/OUT: Read offset in pFile */
! 71762: i64 *piVal /* OUT: Value read from file */
! 71763: ){
! 71764: u8 aVarint[9]; /* Buffer large enough for a varint */
! 71765: i64 iOff = *piOffset; /* Offset in file to read from */
! 71766: int rc; /* Return code */
! 71767:
! 71768: rc = sqlite3OsRead(pFile, aVarint, 9, iOff);
! 71769: if( rc==SQLITE_OK ){
! 71770: *piOffset += getVarint(aVarint, (u64 *)piVal);
! 71771: }
! 71772:
! 71773: return rc;
! 71774: }
! 71775:
! 71776: /*
! 71777: ** Initialize iterator pIter to scan through the PMA stored in file pFile
! 71778: ** starting at offset iStart and ending at offset iEof-1. This function
! 71779: ** leaves the iterator pointing to the first key in the PMA (or EOF if the
! 71780: ** PMA is empty).
! 71781: */
! 71782: static int vdbeSorterIterInit(
! 71783: sqlite3 *db, /* Database handle */
! 71784: VdbeSorter *pSorter, /* Sorter object */
! 71785: i64 iStart, /* Start offset in pFile */
! 71786: VdbeSorterIter *pIter, /* Iterator to populate */
! 71787: i64 *pnByte /* IN/OUT: Increment this value by PMA size */
! 71788: ){
! 71789: int rc;
! 71790:
! 71791: assert( pSorter->iWriteOff>iStart );
! 71792: assert( pIter->aAlloc==0 );
! 71793: pIter->pFile = pSorter->pTemp1;
! 71794: pIter->iReadOff = iStart;
! 71795: pIter->nAlloc = 128;
! 71796: pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
! 71797: if( !pIter->aAlloc ){
! 71798: rc = SQLITE_NOMEM;
! 71799: }else{
! 71800: i64 nByte; /* Total size of PMA in bytes */
! 71801: rc = vdbeSorterReadVarint(pSorter->pTemp1, &pIter->iReadOff, &nByte);
! 71802: *pnByte += nByte;
! 71803: pIter->iEof = pIter->iReadOff + nByte;
! 71804: }
! 71805: if( rc==SQLITE_OK ){
! 71806: rc = vdbeSorterIterNext(db, pIter);
! 71807: }
! 71808: return rc;
! 71809: }
! 71810:
! 71811:
! 71812: /*
! 71813: ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
! 71814: ** size nKey2 bytes). Argument pKeyInfo supplies the collation functions
! 71815: ** used by the comparison. If an error occurs, return an SQLite error code.
! 71816: ** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
! 71817: ** value, depending on whether key1 is smaller, equal to or larger than key2.
! 71818: **
! 71819: ** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
! 71820: ** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
! 71821: ** is true and key1 contains even a single NULL value, it is considered to
! 71822: ** be less than key2. Even if key2 also contains NULL values.
! 71823: **
! 71824: ** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
! 71825: ** has been allocated and contains an unpacked record that is used as key2.
! 71826: */
! 71827: static void vdbeSorterCompare(
! 71828: VdbeCursor *pCsr, /* Cursor object (for pKeyInfo) */
! 71829: int bOmitRowid, /* Ignore rowid field at end of keys */
! 71830: void *pKey1, int nKey1, /* Left side of comparison */
! 71831: void *pKey2, int nKey2, /* Right side of comparison */
! 71832: int *pRes /* OUT: Result of comparison */
! 71833: ){
! 71834: KeyInfo *pKeyInfo = pCsr->pKeyInfo;
! 71835: VdbeSorter *pSorter = pCsr->pSorter;
! 71836: UnpackedRecord *r2 = pSorter->pUnpacked;
! 71837: int i;
! 71838:
! 71839: if( pKey2 ){
! 71840: sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
! 71841: }
! 71842:
! 71843: if( bOmitRowid ){
! 71844: r2->nField = pKeyInfo->nField;
! 71845: assert( r2->nField>0 );
! 71846: for(i=0; i<r2->nField; i++){
! 71847: if( r2->aMem[i].flags & MEM_Null ){
! 71848: *pRes = -1;
! 71849: return;
! 71850: }
! 71851: }
! 71852: r2->flags |= UNPACKED_PREFIX_MATCH;
! 71853: }
! 71854:
! 71855: *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
! 71856: }
! 71857:
! 71858: /*
! 71859: ** This function is called to compare two iterator keys when merging
! 71860: ** multiple b-tree segments. Parameter iOut is the index of the aTree[]
! 71861: ** value to recalculate.
! 71862: */
! 71863: static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
! 71864: VdbeSorter *pSorter = pCsr->pSorter;
! 71865: int i1;
! 71866: int i2;
! 71867: int iRes;
! 71868: VdbeSorterIter *p1;
! 71869: VdbeSorterIter *p2;
! 71870:
! 71871: assert( iOut<pSorter->nTree && iOut>0 );
! 71872:
! 71873: if( iOut>=(pSorter->nTree/2) ){
! 71874: i1 = (iOut - pSorter->nTree/2) * 2;
! 71875: i2 = i1 + 1;
! 71876: }else{
! 71877: i1 = pSorter->aTree[iOut*2];
! 71878: i2 = pSorter->aTree[iOut*2+1];
! 71879: }
! 71880:
! 71881: p1 = &pSorter->aIter[i1];
! 71882: p2 = &pSorter->aIter[i2];
! 71883:
! 71884: if( p1->pFile==0 ){
! 71885: iRes = i2;
! 71886: }else if( p2->pFile==0 ){
! 71887: iRes = i1;
! 71888: }else{
! 71889: int res;
! 71890: assert( pCsr->pSorter->pUnpacked!=0 ); /* allocated in vdbeSorterMerge() */
! 71891: vdbeSorterCompare(
! 71892: pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
! 71893: );
! 71894: if( res<=0 ){
! 71895: iRes = i1;
! 71896: }else{
! 71897: iRes = i2;
! 71898: }
! 71899: }
! 71900:
! 71901: pSorter->aTree[iOut] = iRes;
! 71902: return SQLITE_OK;
! 71903: }
! 71904:
! 71905: /*
! 71906: ** Initialize the temporary index cursor just opened as a sorter cursor.
! 71907: */
! 71908: SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
! 71909: int pgsz; /* Page size of main database */
! 71910: int mxCache; /* Cache size */
! 71911: VdbeSorter *pSorter; /* The new sorter */
! 71912: char *d; /* Dummy */
! 71913:
! 71914: assert( pCsr->pKeyInfo && pCsr->pBt==0 );
! 71915: pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
! 71916: if( pSorter==0 ){
! 71917: return SQLITE_NOMEM;
! 71918: }
! 71919:
! 71920: pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pCsr->pKeyInfo, 0, 0, &d);
! 71921: if( pSorter->pUnpacked==0 ) return SQLITE_NOMEM;
! 71922: assert( pSorter->pUnpacked==(UnpackedRecord *)d );
! 71923:
! 71924: if( !sqlite3TempInMemory(db) ){
! 71925: pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
! 71926: pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
! 71927: mxCache = db->aDb[0].pSchema->cache_size;
! 71928: if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
! 71929: pSorter->mxPmaSize = mxCache * pgsz;
! 71930: }
! 71931:
! 71932: return SQLITE_OK;
! 71933: }
! 71934:
! 71935: /*
! 71936: ** Free the list of sorted records starting at pRecord.
! 71937: */
! 71938: static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
! 71939: SorterRecord *p;
! 71940: SorterRecord *pNext;
! 71941: for(p=pRecord; p; p=pNext){
! 71942: pNext = p->pNext;
! 71943: sqlite3DbFree(db, p);
! 71944: }
! 71945: }
! 71946:
! 71947: /*
! 71948: ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
! 71949: */
! 71950: SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
! 71951: VdbeSorter *pSorter = pCsr->pSorter;
! 71952: if( pSorter ){
! 71953: if( pSorter->aIter ){
! 71954: int i;
! 71955: for(i=0; i<pSorter->nTree; i++){
! 71956: vdbeSorterIterZero(db, &pSorter->aIter[i]);
! 71957: }
! 71958: sqlite3DbFree(db, pSorter->aIter);
! 71959: }
! 71960: if( pSorter->pTemp1 ){
! 71961: sqlite3OsCloseFree(pSorter->pTemp1);
! 71962: }
! 71963: vdbeSorterRecordFree(db, pSorter->pRecord);
! 71964: sqlite3DbFree(db, pSorter->pUnpacked);
! 71965: sqlite3DbFree(db, pSorter);
! 71966: pCsr->pSorter = 0;
! 71967: }
! 71968: }
! 71969:
! 71970: /*
! 71971: ** Allocate space for a file-handle and open a temporary file. If successful,
! 71972: ** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
! 71973: ** Otherwise, set *ppFile to 0 and return an SQLite error code.
! 71974: */
! 71975: static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
! 71976: int dummy;
! 71977: return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
! 71978: SQLITE_OPEN_TEMP_JOURNAL |
! 71979: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
! 71980: SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &dummy
! 71981: );
! 71982: }
! 71983:
! 71984: /*
! 71985: ** Merge the two sorted lists p1 and p2 into a single list.
! 71986: ** Set *ppOut to the head of the new list.
! 71987: */
! 71988: static void vdbeSorterMerge(
! 71989: VdbeCursor *pCsr, /* For pKeyInfo */
! 71990: SorterRecord *p1, /* First list to merge */
! 71991: SorterRecord *p2, /* Second list to merge */
! 71992: SorterRecord **ppOut /* OUT: Head of merged list */
! 71993: ){
! 71994: SorterRecord *pFinal = 0;
! 71995: SorterRecord **pp = &pFinal;
! 71996: void *pVal2 = p2 ? p2->pVal : 0;
! 71997:
! 71998: while( p1 && p2 ){
! 71999: int res;
! 72000: vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
! 72001: if( res<=0 ){
! 72002: *pp = p1;
! 72003: pp = &p1->pNext;
! 72004: p1 = p1->pNext;
! 72005: pVal2 = 0;
! 72006: }else{
! 72007: *pp = p2;
! 72008: pp = &p2->pNext;
! 72009: p2 = p2->pNext;
! 72010: if( p2==0 ) break;
! 72011: pVal2 = p2->pVal;
! 72012: }
! 72013: }
! 72014: *pp = p1 ? p1 : p2;
! 72015: *ppOut = pFinal;
! 72016: }
! 72017:
! 72018: /*
! 72019: ** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
! 72020: ** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
! 72021: ** occurs.
! 72022: */
! 72023: static int vdbeSorterSort(VdbeCursor *pCsr){
! 72024: int i;
! 72025: SorterRecord **aSlot;
! 72026: SorterRecord *p;
! 72027: VdbeSorter *pSorter = pCsr->pSorter;
! 72028:
! 72029: aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
! 72030: if( !aSlot ){
! 72031: return SQLITE_NOMEM;
! 72032: }
! 72033:
! 72034: p = pSorter->pRecord;
! 72035: while( p ){
! 72036: SorterRecord *pNext = p->pNext;
! 72037: p->pNext = 0;
! 72038: for(i=0; aSlot[i]; i++){
! 72039: vdbeSorterMerge(pCsr, p, aSlot[i], &p);
! 72040: aSlot[i] = 0;
! 72041: }
! 72042: aSlot[i] = p;
! 72043: p = pNext;
! 72044: }
! 72045:
! 72046: p = 0;
! 72047: for(i=0; i<64; i++){
! 72048: vdbeSorterMerge(pCsr, p, aSlot[i], &p);
! 72049: }
! 72050: pSorter->pRecord = p;
! 72051:
! 72052: sqlite3_free(aSlot);
! 72053: return SQLITE_OK;
! 72054: }
! 72055:
! 72056:
! 72057: /*
! 72058: ** Write the current contents of the in-memory linked-list to a PMA. Return
! 72059: ** SQLITE_OK if successful, or an SQLite error code otherwise.
! 72060: **
! 72061: ** The format of a PMA is:
! 72062: **
! 72063: ** * A varint. This varint contains the total number of bytes of content
! 72064: ** in the PMA (not including the varint itself).
! 72065: **
! 72066: ** * One or more records packed end-to-end in order of ascending keys.
! 72067: ** Each record consists of a varint followed by a blob of data (the
! 72068: ** key). The varint is the number of bytes in the blob of data.
! 72069: */
! 72070: static int vdbeSorterListToPMA(sqlite3 *db, VdbeCursor *pCsr){
! 72071: int rc = SQLITE_OK; /* Return code */
! 72072: VdbeSorter *pSorter = pCsr->pSorter;
! 72073:
! 72074: if( pSorter->nInMemory==0 ){
! 72075: assert( pSorter->pRecord==0 );
! 72076: return rc;
! 72077: }
! 72078:
! 72079: rc = vdbeSorterSort(pCsr);
! 72080:
! 72081: /* If the first temporary PMA file has not been opened, open it now. */
! 72082: if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
! 72083: rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
! 72084: assert( rc!=SQLITE_OK || pSorter->pTemp1 );
! 72085: assert( pSorter->iWriteOff==0 );
! 72086: assert( pSorter->nPMA==0 );
! 72087: }
! 72088:
! 72089: if( rc==SQLITE_OK ){
! 72090: i64 iOff = pSorter->iWriteOff;
! 72091: SorterRecord *p;
! 72092: SorterRecord *pNext = 0;
! 72093: static const char eightZeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
! 72094:
! 72095: pSorter->nPMA++;
! 72096: rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nInMemory, &iOff);
! 72097: for(p=pSorter->pRecord; rc==SQLITE_OK && p; p=pNext){
! 72098: pNext = p->pNext;
! 72099: rc = vdbeSorterWriteVarint(pSorter->pTemp1, p->nVal, &iOff);
! 72100:
! 72101: if( rc==SQLITE_OK ){
! 72102: rc = sqlite3OsWrite(pSorter->pTemp1, p->pVal, p->nVal, iOff);
! 72103: iOff += p->nVal;
! 72104: }
! 72105:
! 72106: sqlite3DbFree(db, p);
! 72107: }
! 72108:
! 72109: /* This assert verifies that unless an error has occurred, the size of
! 72110: ** the PMA on disk is the same as the expected size stored in
! 72111: ** pSorter->nInMemory. */
! 72112: assert( rc!=SQLITE_OK || pSorter->nInMemory==(
! 72113: iOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nInMemory)
! 72114: ));
! 72115:
! 72116: pSorter->iWriteOff = iOff;
! 72117: if( rc==SQLITE_OK ){
! 72118: /* Terminate each file with 8 extra bytes so that from any offset
! 72119: ** in the file we can always read 9 bytes without a SHORT_READ error */
! 72120: rc = sqlite3OsWrite(pSorter->pTemp1, eightZeros, 8, iOff);
! 72121: }
! 72122: pSorter->pRecord = p;
! 72123: }
! 72124:
! 72125: return rc;
! 72126: }
! 72127:
! 72128: /*
! 72129: ** Add a record to the sorter.
! 72130: */
! 72131: SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
! 72132: sqlite3 *db, /* Database handle */
! 72133: VdbeCursor *pCsr, /* Sorter cursor */
! 72134: Mem *pVal /* Memory cell containing record */
! 72135: ){
! 72136: VdbeSorter *pSorter = pCsr->pSorter;
! 72137: int rc = SQLITE_OK; /* Return Code */
! 72138: SorterRecord *pNew; /* New list element */
! 72139:
! 72140: assert( pSorter );
! 72141: pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
! 72142:
! 72143: pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
! 72144: if( pNew==0 ){
! 72145: rc = SQLITE_NOMEM;
! 72146: }else{
! 72147: pNew->pVal = (void *)&pNew[1];
! 72148: memcpy(pNew->pVal, pVal->z, pVal->n);
! 72149: pNew->nVal = pVal->n;
! 72150: pNew->pNext = pSorter->pRecord;
! 72151: pSorter->pRecord = pNew;
! 72152: }
! 72153:
! 72154: /* See if the contents of the sorter should now be written out. They
! 72155: ** are written out when either of the following are true:
! 72156: **
! 72157: ** * The total memory allocated for the in-memory list is greater
! 72158: ** than (page-size * cache-size), or
! 72159: **
! 72160: ** * The total memory allocated for the in-memory list is greater
! 72161: ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
! 72162: */
! 72163: if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
! 72164: (pSorter->nInMemory>pSorter->mxPmaSize)
! 72165: || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
! 72166: )){
! 72167: rc = vdbeSorterListToPMA(db, pCsr);
! 72168: pSorter->nInMemory = 0;
! 72169: }
! 72170:
! 72171: return rc;
! 72172: }
! 72173:
! 72174: /*
! 72175: ** Helper function for sqlite3VdbeSorterRewind().
! 72176: */
! 72177: static int vdbeSorterInitMerge(
! 72178: sqlite3 *db, /* Database handle */
! 72179: VdbeCursor *pCsr, /* Cursor handle for this sorter */
! 72180: i64 *pnByte /* Sum of bytes in all opened PMAs */
! 72181: ){
! 72182: VdbeSorter *pSorter = pCsr->pSorter;
! 72183: int rc = SQLITE_OK; /* Return code */
! 72184: int i; /* Used to iterator through aIter[] */
! 72185: i64 nByte = 0; /* Total bytes in all opened PMAs */
! 72186:
! 72187: /* Initialize the iterators. */
! 72188: for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
! 72189: VdbeSorterIter *pIter = &pSorter->aIter[i];
! 72190: rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
! 72191: pSorter->iReadOff = pIter->iEof;
! 72192: assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
! 72193: if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
! 72194: }
! 72195:
! 72196: /* Initialize the aTree[] array. */
! 72197: for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
! 72198: rc = vdbeSorterDoCompare(pCsr, i);
! 72199: }
! 72200:
! 72201: *pnByte = nByte;
! 72202: return rc;
! 72203: }
! 72204:
! 72205: /*
! 72206: ** Once the sorter has been populated, this function is called to prepare
! 72207: ** for iterating through its contents in sorted order.
! 72208: */
! 72209: SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
! 72210: VdbeSorter *pSorter = pCsr->pSorter;
! 72211: int rc; /* Return code */
! 72212: sqlite3_file *pTemp2 = 0; /* Second temp file to use */
! 72213: i64 iWrite2 = 0; /* Write offset for pTemp2 */
! 72214: int nIter; /* Number of iterators used */
! 72215: int nByte; /* Bytes of space required for aIter/aTree */
! 72216: int N = 2; /* Power of 2 >= nIter */
! 72217:
! 72218: assert( pSorter );
! 72219:
! 72220: /* If no data has been written to disk, then do not do so now. Instead,
! 72221: ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
! 72222: ** from the in-memory list. */
! 72223: if( pSorter->nPMA==0 ){
! 72224: *pbEof = !pSorter->pRecord;
! 72225: assert( pSorter->aTree==0 );
! 72226: return vdbeSorterSort(pCsr);
! 72227: }
! 72228:
! 72229: /* Write the current b-tree to a PMA. Close the b-tree cursor. */
! 72230: rc = vdbeSorterListToPMA(db, pCsr);
! 72231: if( rc!=SQLITE_OK ) return rc;
! 72232:
! 72233: /* Allocate space for aIter[] and aTree[]. */
! 72234: nIter = pSorter->nPMA;
! 72235: if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
! 72236: assert( nIter>0 );
! 72237: while( N<nIter ) N += N;
! 72238: nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
! 72239: pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
! 72240: if( !pSorter->aIter ) return SQLITE_NOMEM;
! 72241: pSorter->aTree = (int *)&pSorter->aIter[N];
! 72242: pSorter->nTree = N;
! 72243:
! 72244: do {
! 72245: int iNew; /* Index of new, merged, PMA */
! 72246:
! 72247: for(iNew=0;
! 72248: rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA;
! 72249: iNew++
! 72250: ){
! 72251: i64 nWrite; /* Number of bytes in new PMA */
! 72252:
! 72253: /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
! 72254: ** initialize an iterator for each of them and break out of the loop.
! 72255: ** These iterators will be incrementally merged as the VDBE layer calls
! 72256: ** sqlite3VdbeSorterNext().
! 72257: **
! 72258: ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
! 72259: ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
! 72260: ** are merged into a single PMA that is written to file pTemp2.
! 72261: */
! 72262: rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
! 72263: assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
! 72264: if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
! 72265: break;
! 72266: }
! 72267:
! 72268: /* Open the second temp file, if it is not already open. */
! 72269: if( pTemp2==0 ){
! 72270: assert( iWrite2==0 );
! 72271: rc = vdbeSorterOpenTempFile(db, &pTemp2);
! 72272: }
! 72273:
! 72274: if( rc==SQLITE_OK ){
! 72275: rc = vdbeSorterWriteVarint(pTemp2, nWrite, &iWrite2);
! 72276: }
! 72277:
! 72278: if( rc==SQLITE_OK ){
! 72279: int bEof = 0;
! 72280: while( rc==SQLITE_OK && bEof==0 ){
! 72281: int nToWrite;
! 72282: VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
! 72283: assert( pIter->pFile );
! 72284: nToWrite = pIter->nKey + sqlite3VarintLen(pIter->nKey);
! 72285: rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nToWrite, iWrite2);
! 72286: iWrite2 += nToWrite;
! 72287: if( rc==SQLITE_OK ){
! 72288: rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
! 72289: }
! 72290: }
! 72291: }
! 72292: }
! 72293:
! 72294: if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
! 72295: break;
! 72296: }else{
! 72297: sqlite3_file *pTmp = pSorter->pTemp1;
! 72298: pSorter->nPMA = iNew;
! 72299: pSorter->pTemp1 = pTemp2;
! 72300: pTemp2 = pTmp;
! 72301: pSorter->iWriteOff = iWrite2;
! 72302: pSorter->iReadOff = 0;
! 72303: iWrite2 = 0;
! 72304: }
! 72305: }while( rc==SQLITE_OK );
! 72306:
! 72307: if( pTemp2 ){
! 72308: sqlite3OsCloseFree(pTemp2);
! 72309: }
! 72310: *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
! 72311: return rc;
! 72312: }
! 72313:
! 72314: /*
! 72315: ** Advance to the next element in the sorter.
! 72316: */
! 72317: SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
! 72318: VdbeSorter *pSorter = pCsr->pSorter;
! 72319: int rc; /* Return code */
! 72320:
! 72321: if( pSorter->aTree ){
! 72322: int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
! 72323: int i; /* Index of aTree[] to recalculate */
! 72324:
! 72325: rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
! 72326: for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
! 72327: rc = vdbeSorterDoCompare(pCsr, i);
! 72328: }
! 72329:
! 72330: *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
! 72331: }else{
! 72332: SorterRecord *pFree = pSorter->pRecord;
! 72333: pSorter->pRecord = pFree->pNext;
! 72334: pFree->pNext = 0;
! 72335: vdbeSorterRecordFree(db, pFree);
! 72336: *pbEof = !pSorter->pRecord;
! 72337: rc = SQLITE_OK;
! 72338: }
! 72339: return rc;
! 72340: }
! 72341:
! 72342: /*
! 72343: ** Return a pointer to a buffer owned by the sorter that contains the
! 72344: ** current key.
! 72345: */
! 72346: static void *vdbeSorterRowkey(
! 72347: VdbeSorter *pSorter, /* Sorter object */
! 72348: int *pnKey /* OUT: Size of current key in bytes */
! 72349: ){
! 72350: void *pKey;
! 72351: if( pSorter->aTree ){
! 72352: VdbeSorterIter *pIter;
! 72353: pIter = &pSorter->aIter[ pSorter->aTree[1] ];
! 72354: *pnKey = pIter->nKey;
! 72355: pKey = pIter->aKey;
! 72356: }else{
! 72357: *pnKey = pSorter->pRecord->nVal;
! 72358: pKey = pSorter->pRecord->pVal;
! 72359: }
! 72360: return pKey;
! 72361: }
! 72362:
! 72363: /*
! 72364: ** Copy the current sorter key into the memory cell pOut.
! 72365: */
! 72366: SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
! 72367: VdbeSorter *pSorter = pCsr->pSorter;
! 72368: void *pKey; int nKey; /* Sorter key to copy into pOut */
! 72369:
! 72370: pKey = vdbeSorterRowkey(pSorter, &nKey);
! 72371: if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
! 72372: return SQLITE_NOMEM;
! 72373: }
! 72374: pOut->n = nKey;
! 72375: MemSetTypeFlag(pOut, MEM_Blob);
! 72376: memcpy(pOut->z, pKey, nKey);
! 72377:
! 72378: return SQLITE_OK;
! 72379: }
! 72380:
! 72381: /*
! 72382: ** Compare the key in memory cell pVal with the key that the sorter cursor
! 72383: ** passed as the first argument currently points to. For the purposes of
! 72384: ** the comparison, ignore the rowid field at the end of each record.
! 72385: **
! 72386: ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
! 72387: ** Otherwise, set *pRes to a negative, zero or positive value if the
! 72388: ** key in pVal is smaller than, equal to or larger than the current sorter
! 72389: ** key.
! 72390: */
! 72391: SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
! 72392: VdbeCursor *pCsr, /* Sorter cursor */
! 72393: Mem *pVal, /* Value to compare to current sorter key */
! 72394: int *pRes /* OUT: Result of comparison */
! 72395: ){
! 72396: VdbeSorter *pSorter = pCsr->pSorter;
! 72397: void *pKey; int nKey; /* Sorter key to compare pVal with */
! 72398:
! 72399: pKey = vdbeSorterRowkey(pSorter, &nKey);
! 72400: vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes);
! 72401: return SQLITE_OK;
! 72402: }
! 72403:
! 72404: #endif /* #ifndef SQLITE_OMIT_MERGE_SORT */
! 72405:
! 72406: /************** End of vdbesort.c ********************************************/
! 72407: /************** Begin file journal.c *****************************************/
! 72408: /*
! 72409: ** 2007 August 22
! 72410: **
! 72411: ** The author disclaims copyright to this source code. In place of
! 72412: ** a legal notice, here is a blessing:
! 72413: **
! 72414: ** May you do good and not evil.
! 72415: ** May you find forgiveness for yourself and forgive others.
! 72416: ** May you share freely, never taking more than you give.
! 72417: **
! 72418: *************************************************************************
! 72419: **
! 72420: ** This file implements a special kind of sqlite3_file object used
! 72421: ** by SQLite to create journal files if the atomic-write optimization
! 72422: ** is enabled.
! 72423: **
! 72424: ** The distinctive characteristic of this sqlite3_file is that the
! 72425: ** actual on disk file is created lazily. When the file is created,
! 72426: ** the caller specifies a buffer size for an in-memory buffer to
! 72427: ** be used to service read() and write() requests. The actual file
! 72428: ** on disk is not created or populated until either:
! 72429: **
! 72430: ** 1) The in-memory representation grows too large for the allocated
! 72431: ** buffer, or
! 72432: ** 2) The sqlite3JournalCreate() function is called.
! 72433: */
! 72434: #ifdef SQLITE_ENABLE_ATOMIC_WRITE
! 72435:
! 72436:
! 72437: /*
! 72438: ** A JournalFile object is a subclass of sqlite3_file used by
! 72439: ** as an open file handle for journal files.
! 72440: */
! 72441: struct JournalFile {
! 72442: sqlite3_io_methods *pMethod; /* I/O methods on journal files */
! 72443: int nBuf; /* Size of zBuf[] in bytes */
! 72444: char *zBuf; /* Space to buffer journal writes */
! 72445: int iSize; /* Amount of zBuf[] currently used */
! 72446: int flags; /* xOpen flags */
! 72447: sqlite3_vfs *pVfs; /* The "real" underlying VFS */
! 72448: sqlite3_file *pReal; /* The "real" underlying file descriptor */
! 72449: const char *zJournal; /* Name of the journal file */
! 72450: };
! 72451: typedef struct JournalFile JournalFile;
! 72452:
! 72453: /*
! 72454: ** If it does not already exists, create and populate the on-disk file
! 72455: ** for JournalFile p.
! 72456: */
! 72457: static int createFile(JournalFile *p){
! 72458: int rc = SQLITE_OK;
! 72459: if( !p->pReal ){
! 72460: sqlite3_file *pReal = (sqlite3_file *)&p[1];
! 72461: rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
! 72462: if( rc==SQLITE_OK ){
! 72463: p->pReal = pReal;
! 72464: if( p->iSize>0 ){
! 72465: assert(p->iSize<=p->nBuf);
! 72466: rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
! 72467: }
! 72468: }
! 72469: }
! 72470: return rc;
! 72471: }
! 72472:
! 72473: /*
! 72474: ** Close the file.
! 72475: */
! 72476: static int jrnlClose(sqlite3_file *pJfd){
! 72477: JournalFile *p = (JournalFile *)pJfd;
! 72478: if( p->pReal ){
! 72479: sqlite3OsClose(p->pReal);
! 72480: }
! 72481: sqlite3_free(p->zBuf);
! 72482: return SQLITE_OK;
! 72483: }
! 72484:
! 72485: /*
! 72486: ** Read data from the file.
! 72487: */
! 72488: static int jrnlRead(
! 72489: sqlite3_file *pJfd, /* The journal file from which to read */
! 72490: void *zBuf, /* Put the results here */
! 72491: int iAmt, /* Number of bytes to read */
! 72492: sqlite_int64 iOfst /* Begin reading at this offset */
! 72493: ){
! 72494: int rc = SQLITE_OK;
! 72495: JournalFile *p = (JournalFile *)pJfd;
! 72496: if( p->pReal ){
! 72497: rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
! 72498: }else if( (iAmt+iOfst)>p->iSize ){
! 72499: rc = SQLITE_IOERR_SHORT_READ;
! 72500: }else{
! 72501: memcpy(zBuf, &p->zBuf[iOfst], iAmt);
! 72502: }
! 72503: return rc;
! 72504: }
! 72505:
! 72506: /*
! 72507: ** Write data to the file.
! 72508: */
! 72509: static int jrnlWrite(
! 72510: sqlite3_file *pJfd, /* The journal file into which to write */
! 72511: const void *zBuf, /* Take data to be written from here */
! 72512: int iAmt, /* Number of bytes to write */
! 72513: sqlite_int64 iOfst /* Begin writing at this offset into the file */
! 72514: ){
! 72515: int rc = SQLITE_OK;
! 72516: JournalFile *p = (JournalFile *)pJfd;
! 72517: if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
! 72518: rc = createFile(p);
! 72519: }
! 72520: if( rc==SQLITE_OK ){
! 72521: if( p->pReal ){
! 72522: rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
! 72523: }else{
! 72524: memcpy(&p->zBuf[iOfst], zBuf, iAmt);
! 72525: if( p->iSize<(iOfst+iAmt) ){
! 72526: p->iSize = (iOfst+iAmt);
! 72527: }
! 72528: }
! 72529: }
! 72530: return rc;
! 72531: }
! 72532:
! 72533: /*
! 72534: ** Truncate the file.
! 72535: */
! 72536: static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
! 72537: int rc = SQLITE_OK;
! 72538: JournalFile *p = (JournalFile *)pJfd;
! 72539: if( p->pReal ){
! 72540: rc = sqlite3OsTruncate(p->pReal, size);
! 72541: }else if( size<p->iSize ){
! 72542: p->iSize = size;
! 72543: }
! 72544: return rc;
! 72545: }
! 72546:
! 72547: /*
! 72548: ** Sync the file.
! 72549: */
! 72550: static int jrnlSync(sqlite3_file *pJfd, int flags){
! 72551: int rc;
! 72552: JournalFile *p = (JournalFile *)pJfd;
! 72553: if( p->pReal ){
! 72554: rc = sqlite3OsSync(p->pReal, flags);
! 72555: }else{
! 72556: rc = SQLITE_OK;
! 72557: }
! 72558: return rc;
! 72559: }
! 72560:
! 72561: /*
! 72562: ** Query the size of the file in bytes.
! 72563: */
! 72564: static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
! 72565: int rc = SQLITE_OK;
! 72566: JournalFile *p = (JournalFile *)pJfd;
! 72567: if( p->pReal ){
! 72568: rc = sqlite3OsFileSize(p->pReal, pSize);
! 72569: }else{
! 72570: *pSize = (sqlite_int64) p->iSize;
! 72571: }
! 72572: return rc;
! 72573: }
! 72574:
! 72575: /*
! 72576: ** Table of methods for JournalFile sqlite3_file object.
! 72577: */
! 72578: static struct sqlite3_io_methods JournalFileMethods = {
! 72579: 1, /* iVersion */
! 72580: jrnlClose, /* xClose */
! 72581: jrnlRead, /* xRead */
! 72582: jrnlWrite, /* xWrite */
! 72583: jrnlTruncate, /* xTruncate */
! 72584: jrnlSync, /* xSync */
! 72585: jrnlFileSize, /* xFileSize */
! 72586: 0, /* xLock */
! 72587: 0, /* xUnlock */
! 72588: 0, /* xCheckReservedLock */
! 72589: 0, /* xFileControl */
! 72590: 0, /* xSectorSize */
! 72591: 0, /* xDeviceCharacteristics */
! 72592: 0, /* xShmMap */
! 72593: 0, /* xShmLock */
! 72594: 0, /* xShmBarrier */
! 72595: 0 /* xShmUnmap */
! 72596: };
! 72597:
! 72598: /*
! 72599: ** Open a journal file.
! 72600: */
! 72601: SQLITE_PRIVATE int sqlite3JournalOpen(
! 72602: sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
! 72603: const char *zName, /* Name of the journal file */
! 72604: sqlite3_file *pJfd, /* Preallocated, blank file handle */
! 72605: int flags, /* Opening flags */
! 72606: int nBuf /* Bytes buffered before opening the file */
! 72607: ){
! 72608: JournalFile *p = (JournalFile *)pJfd;
! 72609: memset(p, 0, sqlite3JournalSize(pVfs));
! 72610: if( nBuf>0 ){
! 72611: p->zBuf = sqlite3MallocZero(nBuf);
! 72612: if( !p->zBuf ){
! 72613: return SQLITE_NOMEM;
! 72614: }
! 72615: }else{
! 72616: return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
! 72617: }
! 72618: p->pMethod = &JournalFileMethods;
! 72619: p->nBuf = nBuf;
! 72620: p->flags = flags;
! 72621: p->zJournal = zName;
! 72622: p->pVfs = pVfs;
! 72623: return SQLITE_OK;
! 72624: }
! 72625:
! 72626: /*
! 72627: ** If the argument p points to a JournalFile structure, and the underlying
! 72628: ** file has not yet been created, create it now.
! 72629: */
! 72630: SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
! 72631: if( p->pMethods!=&JournalFileMethods ){
! 72632: return SQLITE_OK;
! 72633: }
! 72634: return createFile((JournalFile *)p);
! 72635: }
! 72636:
! 72637: /*
! 72638: ** Return the number of bytes required to store a JournalFile that uses vfs
! 72639: ** pVfs to create the underlying on-disk files.
! 72640: */
! 72641: SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
! 72642: return (pVfs->szOsFile+sizeof(JournalFile));
! 72643: }
! 72644: #endif
! 72645:
! 72646: /************** End of journal.c *********************************************/
! 72647: /************** Begin file memjournal.c **************************************/
! 72648: /*
! 72649: ** 2008 October 7
! 72650: **
! 72651: ** The author disclaims copyright to this source code. In place of
! 72652: ** a legal notice, here is a blessing:
! 72653: **
! 72654: ** May you do good and not evil.
! 72655: ** May you find forgiveness for yourself and forgive others.
! 72656: ** May you share freely, never taking more than you give.
! 72657: **
! 72658: *************************************************************************
! 72659: **
! 72660: ** This file contains code use to implement an in-memory rollback journal.
! 72661: ** The in-memory rollback journal is used to journal transactions for
! 72662: ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
! 72663: */
! 72664:
! 72665: /* Forward references to internal structures */
! 72666: typedef struct MemJournal MemJournal;
! 72667: typedef struct FilePoint FilePoint;
! 72668: typedef struct FileChunk FileChunk;
! 72669:
! 72670: /* Space to hold the rollback journal is allocated in increments of
! 72671: ** this many bytes.
! 72672: **
! 72673: ** The size chosen is a little less than a power of two. That way,
! 72674: ** the FileChunk object will have a size that almost exactly fills
! 72675: ** a power-of-two allocation. This mimimizes wasted space in power-of-two
! 72676: ** memory allocators.
! 72677: */
! 72678: #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
! 72679:
! 72680: /* Macro to find the minimum of two numeric values.
! 72681: */
! 72682: #ifndef MIN
! 72683: # define MIN(x,y) ((x)<(y)?(x):(y))
! 72684: #endif
! 72685:
! 72686: /*
! 72687: ** The rollback journal is composed of a linked list of these structures.
! 72688: */
! 72689: struct FileChunk {
! 72690: FileChunk *pNext; /* Next chunk in the journal */
! 72691: u8 zChunk[JOURNAL_CHUNKSIZE]; /* Content of this chunk */
! 72692: };
! 72693:
! 72694: /*
! 72695: ** An instance of this object serves as a cursor into the rollback journal.
! 72696: ** The cursor can be either for reading or writing.
! 72697: */
! 72698: struct FilePoint {
! 72699: sqlite3_int64 iOffset; /* Offset from the beginning of the file */
! 72700: FileChunk *pChunk; /* Specific chunk into which cursor points */
! 72701: };
! 72702:
! 72703: /*
! 72704: ** This subclass is a subclass of sqlite3_file. Each open memory-journal
! 72705: ** is an instance of this class.
! 72706: */
! 72707: struct MemJournal {
! 72708: sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
! 72709: FileChunk *pFirst; /* Head of in-memory chunk-list */
! 72710: FilePoint endpoint; /* Pointer to the end of the file */
! 72711: FilePoint readpoint; /* Pointer to the end of the last xRead() */
! 72712: };
! 72713:
! 72714: /*
! 72715: ** Read data from the in-memory journal file. This is the implementation
! 72716: ** of the sqlite3_vfs.xRead method.
! 72717: */
! 72718: static int memjrnlRead(
! 72719: sqlite3_file *pJfd, /* The journal file from which to read */
! 72720: void *zBuf, /* Put the results here */
! 72721: int iAmt, /* Number of bytes to read */
! 72722: sqlite_int64 iOfst /* Begin reading at this offset */
! 72723: ){
! 72724: MemJournal *p = (MemJournal *)pJfd;
! 72725: u8 *zOut = zBuf;
! 72726: int nRead = iAmt;
! 72727: int iChunkOffset;
! 72728: FileChunk *pChunk;
! 72729:
! 72730: /* SQLite never tries to read past the end of a rollback journal file */
! 72731: assert( iOfst+iAmt<=p->endpoint.iOffset );
! 72732:
! 72733: if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
! 72734: sqlite3_int64 iOff = 0;
! 72735: for(pChunk=p->pFirst;
! 72736: ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
! 72737: pChunk=pChunk->pNext
! 72738: ){
! 72739: iOff += JOURNAL_CHUNKSIZE;
! 72740: }
! 72741: }else{
! 72742: pChunk = p->readpoint.pChunk;
! 72743: }
! 72744:
! 72745: iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
! 72746: do {
! 72747: int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
! 72748: int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
! 72749: memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
! 72750: zOut += nCopy;
! 72751: nRead -= iSpace;
! 72752: iChunkOffset = 0;
! 72753: } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
! 72754: p->readpoint.iOffset = iOfst+iAmt;
! 72755: p->readpoint.pChunk = pChunk;
! 72756:
! 72757: return SQLITE_OK;
! 72758: }
! 72759:
! 72760: /*
! 72761: ** Write data to the file.
! 72762: */
! 72763: static int memjrnlWrite(
! 72764: sqlite3_file *pJfd, /* The journal file into which to write */
! 72765: const void *zBuf, /* Take data to be written from here */
! 72766: int iAmt, /* Number of bytes to write */
! 72767: sqlite_int64 iOfst /* Begin writing at this offset into the file */
! 72768: ){
! 72769: MemJournal *p = (MemJournal *)pJfd;
! 72770: int nWrite = iAmt;
! 72771: u8 *zWrite = (u8 *)zBuf;
! 72772:
! 72773: /* An in-memory journal file should only ever be appended to. Random
! 72774: ** access writes are not required by sqlite.
! 72775: */
! 72776: assert( iOfst==p->endpoint.iOffset );
! 72777: UNUSED_PARAMETER(iOfst);
! 72778:
! 72779: while( nWrite>0 ){
! 72780: FileChunk *pChunk = p->endpoint.pChunk;
! 72781: int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
! 72782: int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
! 72783:
! 72784: if( iChunkOffset==0 ){
! 72785: /* New chunk is required to extend the file. */
! 72786: FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
! 72787: if( !pNew ){
! 72788: return SQLITE_IOERR_NOMEM;
! 72789: }
! 72790: pNew->pNext = 0;
! 72791: if( pChunk ){
! 72792: assert( p->pFirst );
! 72793: pChunk->pNext = pNew;
! 72794: }else{
! 72795: assert( !p->pFirst );
! 72796: p->pFirst = pNew;
! 72797: }
! 72798: p->endpoint.pChunk = pNew;
! 72799: }
! 72800:
! 72801: memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
! 72802: zWrite += iSpace;
! 72803: nWrite -= iSpace;
! 72804: p->endpoint.iOffset += iSpace;
! 72805: }
! 72806:
! 72807: return SQLITE_OK;
! 72808: }
! 72809:
! 72810: /*
! 72811: ** Truncate the file.
! 72812: */
! 72813: static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
! 72814: MemJournal *p = (MemJournal *)pJfd;
! 72815: FileChunk *pChunk;
! 72816: assert(size==0);
! 72817: UNUSED_PARAMETER(size);
! 72818: pChunk = p->pFirst;
! 72819: while( pChunk ){
! 72820: FileChunk *pTmp = pChunk;
! 72821: pChunk = pChunk->pNext;
! 72822: sqlite3_free(pTmp);
! 72823: }
! 72824: sqlite3MemJournalOpen(pJfd);
! 72825: return SQLITE_OK;
! 72826: }
! 72827:
! 72828: /*
! 72829: ** Close the file.
! 72830: */
! 72831: static int memjrnlClose(sqlite3_file *pJfd){
! 72832: memjrnlTruncate(pJfd, 0);
! 72833: return SQLITE_OK;
! 72834: }
! 72835:
! 72836:
! 72837: /*
! 72838: ** Sync the file.
! 72839: **
! 72840: ** Syncing an in-memory journal is a no-op. And, in fact, this routine
! 72841: ** is never called in a working implementation. This implementation
! 72842: ** exists purely as a contingency, in case some malfunction in some other
! 72843: ** part of SQLite causes Sync to be called by mistake.
! 72844: */
! 72845: static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
! 72846: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 72847: return SQLITE_OK;
! 72848: }
! 72849:
! 72850: /*
! 72851: ** Query the size of the file in bytes.
! 72852: */
! 72853: static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
! 72854: MemJournal *p = (MemJournal *)pJfd;
! 72855: *pSize = (sqlite_int64) p->endpoint.iOffset;
! 72856: return SQLITE_OK;
! 72857: }
! 72858:
! 72859: /*
! 72860: ** Table of methods for MemJournal sqlite3_file object.
! 72861: */
! 72862: static const struct sqlite3_io_methods MemJournalMethods = {
! 72863: 1, /* iVersion */
! 72864: memjrnlClose, /* xClose */
! 72865: memjrnlRead, /* xRead */
! 72866: memjrnlWrite, /* xWrite */
! 72867: memjrnlTruncate, /* xTruncate */
! 72868: memjrnlSync, /* xSync */
! 72869: memjrnlFileSize, /* xFileSize */
! 72870: 0, /* xLock */
! 72871: 0, /* xUnlock */
! 72872: 0, /* xCheckReservedLock */
! 72873: 0, /* xFileControl */
! 72874: 0, /* xSectorSize */
! 72875: 0, /* xDeviceCharacteristics */
! 72876: 0, /* xShmMap */
! 72877: 0, /* xShmLock */
! 72878: 0, /* xShmBarrier */
! 72879: 0 /* xShmUnlock */
! 72880: };
! 72881:
! 72882: /*
! 72883: ** Open a journal file.
! 72884: */
! 72885: SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
! 72886: MemJournal *p = (MemJournal *)pJfd;
! 72887: assert( EIGHT_BYTE_ALIGNMENT(p) );
! 72888: memset(p, 0, sqlite3MemJournalSize());
! 72889: p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
! 72890: }
! 72891:
! 72892: /*
! 72893: ** Return true if the file-handle passed as an argument is
! 72894: ** an in-memory journal
! 72895: */
! 72896: SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
! 72897: return pJfd->pMethods==&MemJournalMethods;
! 72898: }
! 72899:
! 72900: /*
! 72901: ** Return the number of bytes required to store a MemJournal file descriptor.
! 72902: */
! 72903: SQLITE_PRIVATE int sqlite3MemJournalSize(void){
! 72904: return sizeof(MemJournal);
! 72905: }
! 72906:
! 72907: /************** End of memjournal.c ******************************************/
! 72908: /************** Begin file walker.c ******************************************/
! 72909: /*
! 72910: ** 2008 August 16
! 72911: **
! 72912: ** The author disclaims copyright to this source code. In place of
! 72913: ** a legal notice, here is a blessing:
! 72914: **
! 72915: ** May you do good and not evil.
! 72916: ** May you find forgiveness for yourself and forgive others.
! 72917: ** May you share freely, never taking more than you give.
! 72918: **
! 72919: *************************************************************************
! 72920: ** This file contains routines used for walking the parser tree for
! 72921: ** an SQL statement.
! 72922: */
! 72923: /* #include <stdlib.h> */
! 72924: /* #include <string.h> */
! 72925:
! 72926:
! 72927: /*
! 72928: ** Walk an expression tree. Invoke the callback once for each node
! 72929: ** of the expression, while decending. (In other words, the callback
! 72930: ** is invoked before visiting children.)
! 72931: **
! 72932: ** The return value from the callback should be one of the WRC_*
! 72933: ** constants to specify how to proceed with the walk.
! 72934: **
! 72935: ** WRC_Continue Continue descending down the tree.
! 72936: **
! 72937: ** WRC_Prune Do not descend into child nodes. But allow
! 72938: ** the walk to continue with sibling nodes.
! 72939: **
! 72940: ** WRC_Abort Do no more callbacks. Unwind the stack and
! 72941: ** return the top-level walk call.
! 72942: **
! 72943: ** The return value from this routine is WRC_Abort to abandon the tree walk
! 72944: ** and WRC_Continue to continue.
! 72945: */
! 72946: SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
! 72947: int rc;
! 72948: if( pExpr==0 ) return WRC_Continue;
! 72949: testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
! 72950: testcase( ExprHasProperty(pExpr, EP_Reduced) );
! 72951: rc = pWalker->xExprCallback(pWalker, pExpr);
! 72952: if( rc==WRC_Continue
! 72953: && !ExprHasAnyProperty(pExpr,EP_TokenOnly) ){
! 72954: if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
! 72955: if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
! 72956: if( ExprHasProperty(pExpr, EP_xIsSelect) ){
! 72957: if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
! 72958: }else{
! 72959: if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
! 72960: }
! 72961: }
! 72962: return rc & WRC_Abort;
! 72963: }
! 72964:
! 72965: /*
! 72966: ** Call sqlite3WalkExpr() for every expression in list p or until
! 72967: ** an abort request is seen.
! 72968: */
! 72969: SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
! 72970: int i;
! 72971: struct ExprList_item *pItem;
! 72972: if( p ){
! 72973: for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
! 72974: if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
! 72975: }
! 72976: }
! 72977: return WRC_Continue;
! 72978: }
! 72979:
! 72980: /*
! 72981: ** Walk all expressions associated with SELECT statement p. Do
! 72982: ** not invoke the SELECT callback on p, but do (of course) invoke
! 72983: ** any expr callbacks and SELECT callbacks that come from subqueries.
! 72984: ** Return WRC_Abort or WRC_Continue.
! 72985: */
! 72986: SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
! 72987: if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
! 72988: if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
! 72989: if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
! 72990: if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
! 72991: if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
! 72992: if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
! 72993: if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
! 72994: return WRC_Continue;
! 72995: }
! 72996:
! 72997: /*
! 72998: ** Walk the parse trees associated with all subqueries in the
! 72999: ** FROM clause of SELECT statement p. Do not invoke the select
! 73000: ** callback on p, but do invoke it on each FROM clause subquery
! 73001: ** and on any subqueries further down in the tree. Return
! 73002: ** WRC_Abort or WRC_Continue;
! 73003: */
! 73004: SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
! 73005: SrcList *pSrc;
! 73006: int i;
! 73007: struct SrcList_item *pItem;
! 73008:
! 73009: pSrc = p->pSrc;
! 73010: if( ALWAYS(pSrc) ){
! 73011: for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
! 73012: if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
! 73013: return WRC_Abort;
! 73014: }
! 73015: }
! 73016: }
! 73017: return WRC_Continue;
! 73018: }
! 73019:
! 73020: /*
! 73021: ** Call sqlite3WalkExpr() for every expression in Select statement p.
! 73022: ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
! 73023: ** on the compound select chain, p->pPrior.
! 73024: **
! 73025: ** Return WRC_Continue under normal conditions. Return WRC_Abort if
! 73026: ** there is an abort request.
! 73027: **
! 73028: ** If the Walker does not have an xSelectCallback() then this routine
! 73029: ** is a no-op returning WRC_Continue.
! 73030: */
! 73031: SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
! 73032: int rc;
! 73033: if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
! 73034: rc = WRC_Continue;
! 73035: while( p ){
! 73036: rc = pWalker->xSelectCallback(pWalker, p);
! 73037: if( rc ) break;
! 73038: if( sqlite3WalkSelectExpr(pWalker, p) ) return WRC_Abort;
! 73039: if( sqlite3WalkSelectFrom(pWalker, p) ) return WRC_Abort;
! 73040: p = p->pPrior;
! 73041: }
! 73042: return rc & WRC_Abort;
! 73043: }
! 73044:
! 73045: /************** End of walker.c **********************************************/
! 73046: /************** Begin file resolve.c *****************************************/
! 73047: /*
! 73048: ** 2008 August 18
! 73049: **
! 73050: ** The author disclaims copyright to this source code. In place of
! 73051: ** a legal notice, here is a blessing:
! 73052: **
! 73053: ** May you do good and not evil.
! 73054: ** May you find forgiveness for yourself and forgive others.
! 73055: ** May you share freely, never taking more than you give.
! 73056: **
! 73057: *************************************************************************
! 73058: **
! 73059: ** This file contains routines used for walking the parser tree and
! 73060: ** resolve all identifiers by associating them with a particular
! 73061: ** table and column.
! 73062: */
! 73063: /* #include <stdlib.h> */
! 73064: /* #include <string.h> */
! 73065:
! 73066: /*
! 73067: ** Turn the pExpr expression into an alias for the iCol-th column of the
! 73068: ** result set in pEList.
! 73069: **
! 73070: ** If the result set column is a simple column reference, then this routine
! 73071: ** makes an exact copy. But for any other kind of expression, this
! 73072: ** routine make a copy of the result set column as the argument to the
! 73073: ** TK_AS operator. The TK_AS operator causes the expression to be
! 73074: ** evaluated just once and then reused for each alias.
! 73075: **
! 73076: ** The reason for suppressing the TK_AS term when the expression is a simple
! 73077: ** column reference is so that the column reference will be recognized as
! 73078: ** usable by indices within the WHERE clause processing logic.
! 73079: **
! 73080: ** Hack: The TK_AS operator is inhibited if zType[0]=='G'. This means
! 73081: ** that in a GROUP BY clause, the expression is evaluated twice. Hence:
! 73082: **
! 73083: ** SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
! 73084: **
! 73085: ** Is equivalent to:
! 73086: **
! 73087: ** SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
! 73088: **
! 73089: ** The result of random()%5 in the GROUP BY clause is probably different
! 73090: ** from the result in the result-set. We might fix this someday. Or
! 73091: ** then again, we might not...
! 73092: */
! 73093: static void resolveAlias(
! 73094: Parse *pParse, /* Parsing context */
! 73095: ExprList *pEList, /* A result set */
! 73096: int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
! 73097: Expr *pExpr, /* Transform this into an alias to the result set */
! 73098: const char *zType /* "GROUP" or "ORDER" or "" */
! 73099: ){
! 73100: Expr *pOrig; /* The iCol-th column of the result set */
! 73101: Expr *pDup; /* Copy of pOrig */
! 73102: sqlite3 *db; /* The database connection */
! 73103:
! 73104: assert( iCol>=0 && iCol<pEList->nExpr );
! 73105: pOrig = pEList->a[iCol].pExpr;
! 73106: assert( pOrig!=0 );
! 73107: assert( pOrig->flags & EP_Resolved );
! 73108: db = pParse->db;
! 73109: if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
! 73110: pDup = sqlite3ExprDup(db, pOrig, 0);
! 73111: pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
! 73112: if( pDup==0 ) return;
! 73113: if( pEList->a[iCol].iAlias==0 ){
! 73114: pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
! 73115: }
! 73116: pDup->iTable = pEList->a[iCol].iAlias;
! 73117: }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
! 73118: pDup = sqlite3ExprDup(db, pOrig, 0);
! 73119: if( pDup==0 ) return;
! 73120: }else{
! 73121: char *zToken = pOrig->u.zToken;
! 73122: assert( zToken!=0 );
! 73123: pOrig->u.zToken = 0;
! 73124: pDup = sqlite3ExprDup(db, pOrig, 0);
! 73125: pOrig->u.zToken = zToken;
! 73126: if( pDup==0 ) return;
! 73127: assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
! 73128: pDup->flags2 |= EP2_MallocedToken;
! 73129: pDup->u.zToken = sqlite3DbStrDup(db, zToken);
! 73130: }
! 73131: if( pExpr->flags & EP_ExpCollate ){
! 73132: pDup->pColl = pExpr->pColl;
! 73133: pDup->flags |= EP_ExpCollate;
! 73134: }
! 73135:
! 73136: /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
! 73137: ** prevents ExprDelete() from deleting the Expr structure itself,
! 73138: ** allowing it to be repopulated by the memcpy() on the following line.
! 73139: */
! 73140: ExprSetProperty(pExpr, EP_Static);
! 73141: sqlite3ExprDelete(db, pExpr);
! 73142: memcpy(pExpr, pDup, sizeof(*pExpr));
! 73143: sqlite3DbFree(db, pDup);
! 73144: }
! 73145:
! 73146:
! 73147: /*
! 73148: ** Return TRUE if the name zCol occurs anywhere in the USING clause.
! 73149: **
! 73150: ** Return FALSE if the USING clause is NULL or if it does not contain
! 73151: ** zCol.
! 73152: */
! 73153: static int nameInUsingClause(IdList *pUsing, const char *zCol){
! 73154: if( pUsing ){
! 73155: int k;
! 73156: for(k=0; k<pUsing->nId; k++){
! 73157: if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
! 73158: }
! 73159: }
! 73160: return 0;
! 73161: }
! 73162:
! 73163:
! 73164: /*
! 73165: ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
! 73166: ** that name in the set of source tables in pSrcList and make the pExpr
! 73167: ** expression node refer back to that source column. The following changes
! 73168: ** are made to pExpr:
! 73169: **
! 73170: ** pExpr->iDb Set the index in db->aDb[] of the database X
! 73171: ** (even if X is implied).
! 73172: ** pExpr->iTable Set to the cursor number for the table obtained
! 73173: ** from pSrcList.
! 73174: ** pExpr->pTab Points to the Table structure of X.Y (even if
! 73175: ** X and/or Y are implied.)
! 73176: ** pExpr->iColumn Set to the column number within the table.
! 73177: ** pExpr->op Set to TK_COLUMN.
! 73178: ** pExpr->pLeft Any expression this points to is deleted
! 73179: ** pExpr->pRight Any expression this points to is deleted.
! 73180: **
! 73181: ** The zDb variable is the name of the database (the "X"). This value may be
! 73182: ** NULL meaning that name is of the form Y.Z or Z. Any available database
! 73183: ** can be used. The zTable variable is the name of the table (the "Y"). This
! 73184: ** value can be NULL if zDb is also NULL. If zTable is NULL it
! 73185: ** means that the form of the name is Z and that columns from any table
! 73186: ** can be used.
! 73187: **
! 73188: ** If the name cannot be resolved unambiguously, leave an error message
! 73189: ** in pParse and return WRC_Abort. Return WRC_Prune on success.
! 73190: */
! 73191: static int lookupName(
! 73192: Parse *pParse, /* The parsing context */
! 73193: const char *zDb, /* Name of the database containing table, or NULL */
! 73194: const char *zTab, /* Name of table containing column, or NULL */
! 73195: const char *zCol, /* Name of the column. */
! 73196: NameContext *pNC, /* The name context used to resolve the name */
! 73197: Expr *pExpr /* Make this EXPR node point to the selected column */
! 73198: ){
! 73199: int i, j; /* Loop counters */
! 73200: int cnt = 0; /* Number of matching column names */
! 73201: int cntTab = 0; /* Number of matching table names */
! 73202: sqlite3 *db = pParse->db; /* The database connection */
! 73203: struct SrcList_item *pItem; /* Use for looping over pSrcList items */
! 73204: struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
! 73205: NameContext *pTopNC = pNC; /* First namecontext in the list */
! 73206: Schema *pSchema = 0; /* Schema of the expression */
! 73207: int isTrigger = 0;
! 73208:
! 73209: assert( pNC ); /* the name context cannot be NULL. */
! 73210: assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
! 73211: assert( ~ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
! 73212:
! 73213: /* Initialize the node to no-match */
! 73214: pExpr->iTable = -1;
! 73215: pExpr->pTab = 0;
! 73216: ExprSetIrreducible(pExpr);
! 73217:
! 73218: /* Start at the inner-most context and move outward until a match is found */
! 73219: while( pNC && cnt==0 ){
! 73220: ExprList *pEList;
! 73221: SrcList *pSrcList = pNC->pSrcList;
! 73222:
! 73223: if( pSrcList ){
! 73224: for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
! 73225: Table *pTab;
! 73226: int iDb;
! 73227: Column *pCol;
! 73228:
! 73229: pTab = pItem->pTab;
! 73230: assert( pTab!=0 && pTab->zName!=0 );
! 73231: iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 73232: assert( pTab->nCol>0 );
! 73233: if( zTab ){
! 73234: if( pItem->zAlias ){
! 73235: char *zTabName = pItem->zAlias;
! 73236: if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
! 73237: }else{
! 73238: char *zTabName = pTab->zName;
! 73239: if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
! 73240: continue;
! 73241: }
! 73242: if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
! 73243: continue;
! 73244: }
! 73245: }
! 73246: }
! 73247: if( 0==(cntTab++) ){
! 73248: pExpr->iTable = pItem->iCursor;
! 73249: pExpr->pTab = pTab;
! 73250: pSchema = pTab->pSchema;
! 73251: pMatch = pItem;
! 73252: }
! 73253: for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
! 73254: if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
! 73255: /* If there has been exactly one prior match and this match
! 73256: ** is for the right-hand table of a NATURAL JOIN or is in a
! 73257: ** USING clause, then skip this match.
! 73258: */
! 73259: if( cnt==1 ){
! 73260: if( pItem->jointype & JT_NATURAL ) continue;
! 73261: if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
! 73262: }
! 73263: cnt++;
! 73264: pExpr->iTable = pItem->iCursor;
! 73265: pExpr->pTab = pTab;
! 73266: pMatch = pItem;
! 73267: pSchema = pTab->pSchema;
! 73268: /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
! 73269: pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
! 73270: break;
! 73271: }
! 73272: }
! 73273: }
! 73274: }
! 73275:
! 73276: #ifndef SQLITE_OMIT_TRIGGER
! 73277: /* If we have not already resolved the name, then maybe
! 73278: ** it is a new.* or old.* trigger argument reference
! 73279: */
! 73280: if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
! 73281: int op = pParse->eTriggerOp;
! 73282: Table *pTab = 0;
! 73283: assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
! 73284: if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
! 73285: pExpr->iTable = 1;
! 73286: pTab = pParse->pTriggerTab;
! 73287: }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
! 73288: pExpr->iTable = 0;
! 73289: pTab = pParse->pTriggerTab;
! 73290: }
! 73291:
! 73292: if( pTab ){
! 73293: int iCol;
! 73294: pSchema = pTab->pSchema;
! 73295: cntTab++;
! 73296: for(iCol=0; iCol<pTab->nCol; iCol++){
! 73297: Column *pCol = &pTab->aCol[iCol];
! 73298: if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
! 73299: if( iCol==pTab->iPKey ){
! 73300: iCol = -1;
! 73301: }
! 73302: break;
! 73303: }
! 73304: }
! 73305: if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) ){
! 73306: iCol = -1; /* IMP: R-44911-55124 */
! 73307: }
! 73308: if( iCol<pTab->nCol ){
! 73309: cnt++;
! 73310: if( iCol<0 ){
! 73311: pExpr->affinity = SQLITE_AFF_INTEGER;
! 73312: }else if( pExpr->iTable==0 ){
! 73313: testcase( iCol==31 );
! 73314: testcase( iCol==32 );
! 73315: pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
! 73316: }else{
! 73317: testcase( iCol==31 );
! 73318: testcase( iCol==32 );
! 73319: pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
! 73320: }
! 73321: pExpr->iColumn = (i16)iCol;
! 73322: pExpr->pTab = pTab;
! 73323: isTrigger = 1;
! 73324: }
! 73325: }
! 73326: }
! 73327: #endif /* !defined(SQLITE_OMIT_TRIGGER) */
! 73328:
! 73329: /*
! 73330: ** Perhaps the name is a reference to the ROWID
! 73331: */
! 73332: if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
! 73333: cnt = 1;
! 73334: pExpr->iColumn = -1; /* IMP: R-44911-55124 */
! 73335: pExpr->affinity = SQLITE_AFF_INTEGER;
! 73336: }
! 73337:
! 73338: /*
! 73339: ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
! 73340: ** might refer to an result-set alias. This happens, for example, when
! 73341: ** we are resolving names in the WHERE clause of the following command:
! 73342: **
! 73343: ** SELECT a+b AS x FROM table WHERE x<10;
! 73344: **
! 73345: ** In cases like this, replace pExpr with a copy of the expression that
! 73346: ** forms the result set entry ("a+b" in the example) and return immediately.
! 73347: ** Note that the expression in the result set should have already been
! 73348: ** resolved by the time the WHERE clause is resolved.
! 73349: */
! 73350: if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
! 73351: for(j=0; j<pEList->nExpr; j++){
! 73352: char *zAs = pEList->a[j].zName;
! 73353: if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
! 73354: Expr *pOrig;
! 73355: assert( pExpr->pLeft==0 && pExpr->pRight==0 );
! 73356: assert( pExpr->x.pList==0 );
! 73357: assert( pExpr->x.pSelect==0 );
! 73358: pOrig = pEList->a[j].pExpr;
! 73359: if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
! 73360: sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
! 73361: return WRC_Abort;
! 73362: }
! 73363: resolveAlias(pParse, pEList, j, pExpr, "");
! 73364: cnt = 1;
! 73365: pMatch = 0;
! 73366: assert( zTab==0 && zDb==0 );
! 73367: goto lookupname_end;
! 73368: }
! 73369: }
! 73370: }
! 73371:
! 73372: /* Advance to the next name context. The loop will exit when either
! 73373: ** we have a match (cnt>0) or when we run out of name contexts.
! 73374: */
! 73375: if( cnt==0 ){
! 73376: pNC = pNC->pNext;
! 73377: }
! 73378: }
! 73379:
! 73380: /*
! 73381: ** If X and Y are NULL (in other words if only the column name Z is
! 73382: ** supplied) and the value of Z is enclosed in double-quotes, then
! 73383: ** Z is a string literal if it doesn't match any column names. In that
! 73384: ** case, we need to return right away and not make any changes to
! 73385: ** pExpr.
! 73386: **
! 73387: ** Because no reference was made to outer contexts, the pNC->nRef
! 73388: ** fields are not changed in any context.
! 73389: */
! 73390: if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
! 73391: pExpr->op = TK_STRING;
! 73392: pExpr->pTab = 0;
! 73393: return WRC_Prune;
! 73394: }
! 73395:
! 73396: /*
! 73397: ** cnt==0 means there was not match. cnt>1 means there were two or
! 73398: ** more matches. Either way, we have an error.
! 73399: */
! 73400: if( cnt!=1 ){
! 73401: const char *zErr;
! 73402: zErr = cnt==0 ? "no such column" : "ambiguous column name";
! 73403: if( zDb ){
! 73404: sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
! 73405: }else if( zTab ){
! 73406: sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
! 73407: }else{
! 73408: sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
! 73409: }
! 73410: pParse->checkSchema = 1;
! 73411: pTopNC->nErr++;
! 73412: }
! 73413:
! 73414: /* If a column from a table in pSrcList is referenced, then record
! 73415: ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
! 73416: ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
! 73417: ** column number is greater than the number of bits in the bitmask
! 73418: ** then set the high-order bit of the bitmask.
! 73419: */
! 73420: if( pExpr->iColumn>=0 && pMatch!=0 ){
! 73421: int n = pExpr->iColumn;
! 73422: testcase( n==BMS-1 );
! 73423: if( n>=BMS ){
! 73424: n = BMS-1;
! 73425: }
! 73426: assert( pMatch->iCursor==pExpr->iTable );
! 73427: pMatch->colUsed |= ((Bitmask)1)<<n;
! 73428: }
! 73429:
! 73430: /* Clean up and return
! 73431: */
! 73432: sqlite3ExprDelete(db, pExpr->pLeft);
! 73433: pExpr->pLeft = 0;
! 73434: sqlite3ExprDelete(db, pExpr->pRight);
! 73435: pExpr->pRight = 0;
! 73436: pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
! 73437: lookupname_end:
! 73438: if( cnt==1 ){
! 73439: assert( pNC!=0 );
! 73440: sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
! 73441: /* Increment the nRef value on all name contexts from TopNC up to
! 73442: ** the point where the name matched. */
! 73443: for(;;){
! 73444: assert( pTopNC!=0 );
! 73445: pTopNC->nRef++;
! 73446: if( pTopNC==pNC ) break;
! 73447: pTopNC = pTopNC->pNext;
! 73448: }
! 73449: return WRC_Prune;
! 73450: } else {
! 73451: return WRC_Abort;
! 73452: }
! 73453: }
! 73454:
! 73455: /*
! 73456: ** Allocate and return a pointer to an expression to load the column iCol
! 73457: ** from datasource iSrc in SrcList pSrc.
! 73458: */
! 73459: SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
! 73460: Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
! 73461: if( p ){
! 73462: struct SrcList_item *pItem = &pSrc->a[iSrc];
! 73463: p->pTab = pItem->pTab;
! 73464: p->iTable = pItem->iCursor;
! 73465: if( p->pTab->iPKey==iCol ){
! 73466: p->iColumn = -1;
! 73467: }else{
! 73468: p->iColumn = (ynVar)iCol;
! 73469: testcase( iCol==BMS );
! 73470: testcase( iCol==BMS-1 );
! 73471: pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
! 73472: }
! 73473: ExprSetProperty(p, EP_Resolved);
! 73474: }
! 73475: return p;
! 73476: }
! 73477:
! 73478: /*
! 73479: ** This routine is callback for sqlite3WalkExpr().
! 73480: **
! 73481: ** Resolve symbolic names into TK_COLUMN operators for the current
! 73482: ** node in the expression tree. Return 0 to continue the search down
! 73483: ** the tree or 2 to abort the tree walk.
! 73484: **
! 73485: ** This routine also does error checking and name resolution for
! 73486: ** function names. The operator for aggregate functions is changed
! 73487: ** to TK_AGG_FUNCTION.
! 73488: */
! 73489: static int resolveExprStep(Walker *pWalker, Expr *pExpr){
! 73490: NameContext *pNC;
! 73491: Parse *pParse;
! 73492:
! 73493: pNC = pWalker->u.pNC;
! 73494: assert( pNC!=0 );
! 73495: pParse = pNC->pParse;
! 73496: assert( pParse==pWalker->pParse );
! 73497:
! 73498: if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
! 73499: ExprSetProperty(pExpr, EP_Resolved);
! 73500: #ifndef NDEBUG
! 73501: if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
! 73502: SrcList *pSrcList = pNC->pSrcList;
! 73503: int i;
! 73504: for(i=0; i<pNC->pSrcList->nSrc; i++){
! 73505: assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
! 73506: }
! 73507: }
! 73508: #endif
! 73509: switch( pExpr->op ){
! 73510:
! 73511: #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
! 73512: /* The special operator TK_ROW means use the rowid for the first
! 73513: ** column in the FROM clause. This is used by the LIMIT and ORDER BY
! 73514: ** clause processing on UPDATE and DELETE statements.
! 73515: */
! 73516: case TK_ROW: {
! 73517: SrcList *pSrcList = pNC->pSrcList;
! 73518: struct SrcList_item *pItem;
! 73519: assert( pSrcList && pSrcList->nSrc==1 );
! 73520: pItem = pSrcList->a;
! 73521: pExpr->op = TK_COLUMN;
! 73522: pExpr->pTab = pItem->pTab;
! 73523: pExpr->iTable = pItem->iCursor;
! 73524: pExpr->iColumn = -1;
! 73525: pExpr->affinity = SQLITE_AFF_INTEGER;
! 73526: break;
! 73527: }
! 73528: #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
! 73529:
! 73530: /* A lone identifier is the name of a column.
! 73531: */
! 73532: case TK_ID: {
! 73533: return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
! 73534: }
! 73535:
! 73536: /* A table name and column name: ID.ID
! 73537: ** Or a database, table and column: ID.ID.ID
! 73538: */
! 73539: case TK_DOT: {
! 73540: const char *zColumn;
! 73541: const char *zTable;
! 73542: const char *zDb;
! 73543: Expr *pRight;
! 73544:
! 73545: /* if( pSrcList==0 ) break; */
! 73546: pRight = pExpr->pRight;
! 73547: if( pRight->op==TK_ID ){
! 73548: zDb = 0;
! 73549: zTable = pExpr->pLeft->u.zToken;
! 73550: zColumn = pRight->u.zToken;
! 73551: }else{
! 73552: assert( pRight->op==TK_DOT );
! 73553: zDb = pExpr->pLeft->u.zToken;
! 73554: zTable = pRight->pLeft->u.zToken;
! 73555: zColumn = pRight->pRight->u.zToken;
! 73556: }
! 73557: return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
! 73558: }
! 73559:
! 73560: /* Resolve function names
! 73561: */
! 73562: case TK_CONST_FUNC:
! 73563: case TK_FUNCTION: {
! 73564: ExprList *pList = pExpr->x.pList; /* The argument list */
! 73565: int n = pList ? pList->nExpr : 0; /* Number of arguments */
! 73566: int no_such_func = 0; /* True if no such function exists */
! 73567: int wrong_num_args = 0; /* True if wrong number of arguments */
! 73568: int is_agg = 0; /* True if is an aggregate function */
! 73569: int auth; /* Authorization to use the function */
! 73570: int nId; /* Number of characters in function name */
! 73571: const char *zId; /* The function name. */
! 73572: FuncDef *pDef; /* Information about the function */
! 73573: u8 enc = ENC(pParse->db); /* The database encoding */
! 73574:
! 73575: testcase( pExpr->op==TK_CONST_FUNC );
! 73576: assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
! 73577: zId = pExpr->u.zToken;
! 73578: nId = sqlite3Strlen30(zId);
! 73579: pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
! 73580: if( pDef==0 ){
! 73581: pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
! 73582: if( pDef==0 ){
! 73583: no_such_func = 1;
! 73584: }else{
! 73585: wrong_num_args = 1;
! 73586: }
! 73587: }else{
! 73588: is_agg = pDef->xFunc==0;
! 73589: }
! 73590: #ifndef SQLITE_OMIT_AUTHORIZATION
! 73591: if( pDef ){
! 73592: auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
! 73593: if( auth!=SQLITE_OK ){
! 73594: if( auth==SQLITE_DENY ){
! 73595: sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
! 73596: pDef->zName);
! 73597: pNC->nErr++;
! 73598: }
! 73599: pExpr->op = TK_NULL;
! 73600: return WRC_Prune;
! 73601: }
! 73602: }
! 73603: #endif
! 73604: if( is_agg && !pNC->allowAgg ){
! 73605: sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
! 73606: pNC->nErr++;
! 73607: is_agg = 0;
! 73608: }else if( no_such_func ){
! 73609: sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
! 73610: pNC->nErr++;
! 73611: }else if( wrong_num_args ){
! 73612: sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
! 73613: nId, zId);
! 73614: pNC->nErr++;
! 73615: }
! 73616: if( is_agg ){
! 73617: pExpr->op = TK_AGG_FUNCTION;
! 73618: pNC->hasAgg = 1;
! 73619: }
! 73620: if( is_agg ) pNC->allowAgg = 0;
! 73621: sqlite3WalkExprList(pWalker, pList);
! 73622: if( is_agg ) pNC->allowAgg = 1;
! 73623: /* FIX ME: Compute pExpr->affinity based on the expected return
! 73624: ** type of the function
! 73625: */
! 73626: return WRC_Prune;
! 73627: }
! 73628: #ifndef SQLITE_OMIT_SUBQUERY
! 73629: case TK_SELECT:
! 73630: case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
! 73631: #endif
! 73632: case TK_IN: {
! 73633: testcase( pExpr->op==TK_IN );
! 73634: if( ExprHasProperty(pExpr, EP_xIsSelect) ){
! 73635: int nRef = pNC->nRef;
! 73636: #ifndef SQLITE_OMIT_CHECK
! 73637: if( pNC->isCheck ){
! 73638: sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
! 73639: }
! 73640: #endif
! 73641: sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
! 73642: assert( pNC->nRef>=nRef );
! 73643: if( nRef!=pNC->nRef ){
! 73644: ExprSetProperty(pExpr, EP_VarSelect);
! 73645: }
! 73646: }
! 73647: break;
! 73648: }
! 73649: #ifndef SQLITE_OMIT_CHECK
! 73650: case TK_VARIABLE: {
! 73651: if( pNC->isCheck ){
! 73652: sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
! 73653: }
! 73654: break;
! 73655: }
! 73656: #endif
! 73657: }
! 73658: return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
! 73659: }
! 73660:
! 73661: /*
! 73662: ** pEList is a list of expressions which are really the result set of the
! 73663: ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
! 73664: ** This routine checks to see if pE is a simple identifier which corresponds
! 73665: ** to the AS-name of one of the terms of the expression list. If it is,
! 73666: ** this routine return an integer between 1 and N where N is the number of
! 73667: ** elements in pEList, corresponding to the matching entry. If there is
! 73668: ** no match, or if pE is not a simple identifier, then this routine
! 73669: ** return 0.
! 73670: **
! 73671: ** pEList has been resolved. pE has not.
! 73672: */
! 73673: static int resolveAsName(
! 73674: Parse *pParse, /* Parsing context for error messages */
! 73675: ExprList *pEList, /* List of expressions to scan */
! 73676: Expr *pE /* Expression we are trying to match */
! 73677: ){
! 73678: int i; /* Loop counter */
! 73679:
! 73680: UNUSED_PARAMETER(pParse);
! 73681:
! 73682: if( pE->op==TK_ID ){
! 73683: char *zCol = pE->u.zToken;
! 73684: for(i=0; i<pEList->nExpr; i++){
! 73685: char *zAs = pEList->a[i].zName;
! 73686: if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
! 73687: return i+1;
! 73688: }
! 73689: }
! 73690: }
! 73691: return 0;
! 73692: }
! 73693:
! 73694: /*
! 73695: ** pE is a pointer to an expression which is a single term in the
! 73696: ** ORDER BY of a compound SELECT. The expression has not been
! 73697: ** name resolved.
! 73698: **
! 73699: ** At the point this routine is called, we already know that the
! 73700: ** ORDER BY term is not an integer index into the result set. That
! 73701: ** case is handled by the calling routine.
! 73702: **
! 73703: ** Attempt to match pE against result set columns in the left-most
! 73704: ** SELECT statement. Return the index i of the matching column,
! 73705: ** as an indication to the caller that it should sort by the i-th column.
! 73706: ** The left-most column is 1. In other words, the value returned is the
! 73707: ** same integer value that would be used in the SQL statement to indicate
! 73708: ** the column.
! 73709: **
! 73710: ** If there is no match, return 0. Return -1 if an error occurs.
! 73711: */
! 73712: static int resolveOrderByTermToExprList(
! 73713: Parse *pParse, /* Parsing context for error messages */
! 73714: Select *pSelect, /* The SELECT statement with the ORDER BY clause */
! 73715: Expr *pE /* The specific ORDER BY term */
! 73716: ){
! 73717: int i; /* Loop counter */
! 73718: ExprList *pEList; /* The columns of the result set */
! 73719: NameContext nc; /* Name context for resolving pE */
! 73720: sqlite3 *db; /* Database connection */
! 73721: int rc; /* Return code from subprocedures */
! 73722: u8 savedSuppErr; /* Saved value of db->suppressErr */
! 73723:
! 73724: assert( sqlite3ExprIsInteger(pE, &i)==0 );
! 73725: pEList = pSelect->pEList;
! 73726:
! 73727: /* Resolve all names in the ORDER BY term expression
! 73728: */
! 73729: memset(&nc, 0, sizeof(nc));
! 73730: nc.pParse = pParse;
! 73731: nc.pSrcList = pSelect->pSrc;
! 73732: nc.pEList = pEList;
! 73733: nc.allowAgg = 1;
! 73734: nc.nErr = 0;
! 73735: db = pParse->db;
! 73736: savedSuppErr = db->suppressErr;
! 73737: db->suppressErr = 1;
! 73738: rc = sqlite3ResolveExprNames(&nc, pE);
! 73739: db->suppressErr = savedSuppErr;
! 73740: if( rc ) return 0;
! 73741:
! 73742: /* Try to match the ORDER BY expression against an expression
! 73743: ** in the result set. Return an 1-based index of the matching
! 73744: ** result-set entry.
! 73745: */
! 73746: for(i=0; i<pEList->nExpr; i++){
! 73747: if( sqlite3ExprCompare(pEList->a[i].pExpr, pE)<2 ){
! 73748: return i+1;
! 73749: }
! 73750: }
! 73751:
! 73752: /* If no match, return 0. */
! 73753: return 0;
! 73754: }
! 73755:
! 73756: /*
! 73757: ** Generate an ORDER BY or GROUP BY term out-of-range error.
! 73758: */
! 73759: static void resolveOutOfRangeError(
! 73760: Parse *pParse, /* The error context into which to write the error */
! 73761: const char *zType, /* "ORDER" or "GROUP" */
! 73762: int i, /* The index (1-based) of the term out of range */
! 73763: int mx /* Largest permissible value of i */
! 73764: ){
! 73765: sqlite3ErrorMsg(pParse,
! 73766: "%r %s BY term out of range - should be "
! 73767: "between 1 and %d", i, zType, mx);
! 73768: }
! 73769:
! 73770: /*
! 73771: ** Analyze the ORDER BY clause in a compound SELECT statement. Modify
! 73772: ** each term of the ORDER BY clause is a constant integer between 1
! 73773: ** and N where N is the number of columns in the compound SELECT.
! 73774: **
! 73775: ** ORDER BY terms that are already an integer between 1 and N are
! 73776: ** unmodified. ORDER BY terms that are integers outside the range of
! 73777: ** 1 through N generate an error. ORDER BY terms that are expressions
! 73778: ** are matched against result set expressions of compound SELECT
! 73779: ** beginning with the left-most SELECT and working toward the right.
! 73780: ** At the first match, the ORDER BY expression is transformed into
! 73781: ** the integer column number.
! 73782: **
! 73783: ** Return the number of errors seen.
! 73784: */
! 73785: static int resolveCompoundOrderBy(
! 73786: Parse *pParse, /* Parsing context. Leave error messages here */
! 73787: Select *pSelect /* The SELECT statement containing the ORDER BY */
! 73788: ){
! 73789: int i;
! 73790: ExprList *pOrderBy;
! 73791: ExprList *pEList;
! 73792: sqlite3 *db;
! 73793: int moreToDo = 1;
! 73794:
! 73795: pOrderBy = pSelect->pOrderBy;
! 73796: if( pOrderBy==0 ) return 0;
! 73797: db = pParse->db;
! 73798: #if SQLITE_MAX_COLUMN
! 73799: if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
! 73800: sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
! 73801: return 1;
! 73802: }
! 73803: #endif
! 73804: for(i=0; i<pOrderBy->nExpr; i++){
! 73805: pOrderBy->a[i].done = 0;
! 73806: }
! 73807: pSelect->pNext = 0;
! 73808: while( pSelect->pPrior ){
! 73809: pSelect->pPrior->pNext = pSelect;
! 73810: pSelect = pSelect->pPrior;
! 73811: }
! 73812: while( pSelect && moreToDo ){
! 73813: struct ExprList_item *pItem;
! 73814: moreToDo = 0;
! 73815: pEList = pSelect->pEList;
! 73816: assert( pEList!=0 );
! 73817: for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
! 73818: int iCol = -1;
! 73819: Expr *pE, *pDup;
! 73820: if( pItem->done ) continue;
! 73821: pE = pItem->pExpr;
! 73822: if( sqlite3ExprIsInteger(pE, &iCol) ){
! 73823: if( iCol<=0 || iCol>pEList->nExpr ){
! 73824: resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
! 73825: return 1;
! 73826: }
! 73827: }else{
! 73828: iCol = resolveAsName(pParse, pEList, pE);
! 73829: if( iCol==0 ){
! 73830: pDup = sqlite3ExprDup(db, pE, 0);
! 73831: if( !db->mallocFailed ){
! 73832: assert(pDup);
! 73833: iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
! 73834: }
! 73835: sqlite3ExprDelete(db, pDup);
! 73836: }
! 73837: }
! 73838: if( iCol>0 ){
! 73839: CollSeq *pColl = pE->pColl;
! 73840: int flags = pE->flags & EP_ExpCollate;
! 73841: sqlite3ExprDelete(db, pE);
! 73842: pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
! 73843: if( pE==0 ) return 1;
! 73844: pE->pColl = pColl;
! 73845: pE->flags |= EP_IntValue | flags;
! 73846: pE->u.iValue = iCol;
! 73847: pItem->iOrderByCol = (u16)iCol;
! 73848: pItem->done = 1;
! 73849: }else{
! 73850: moreToDo = 1;
! 73851: }
! 73852: }
! 73853: pSelect = pSelect->pNext;
! 73854: }
! 73855: for(i=0; i<pOrderBy->nExpr; i++){
! 73856: if( pOrderBy->a[i].done==0 ){
! 73857: sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
! 73858: "column in the result set", i+1);
! 73859: return 1;
! 73860: }
! 73861: }
! 73862: return 0;
! 73863: }
! 73864:
! 73865: /*
! 73866: ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
! 73867: ** the SELECT statement pSelect. If any term is reference to a
! 73868: ** result set expression (as determined by the ExprList.a.iCol field)
! 73869: ** then convert that term into a copy of the corresponding result set
! 73870: ** column.
! 73871: **
! 73872: ** If any errors are detected, add an error message to pParse and
! 73873: ** return non-zero. Return zero if no errors are seen.
! 73874: */
! 73875: SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
! 73876: Parse *pParse, /* Parsing context. Leave error messages here */
! 73877: Select *pSelect, /* The SELECT statement containing the clause */
! 73878: ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
! 73879: const char *zType /* "ORDER" or "GROUP" */
! 73880: ){
! 73881: int i;
! 73882: sqlite3 *db = pParse->db;
! 73883: ExprList *pEList;
! 73884: struct ExprList_item *pItem;
! 73885:
! 73886: if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
! 73887: #if SQLITE_MAX_COLUMN
! 73888: if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
! 73889: sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
! 73890: return 1;
! 73891: }
! 73892: #endif
! 73893: pEList = pSelect->pEList;
! 73894: assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
! 73895: for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
! 73896: if( pItem->iOrderByCol ){
! 73897: if( pItem->iOrderByCol>pEList->nExpr ){
! 73898: resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
! 73899: return 1;
! 73900: }
! 73901: resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType);
! 73902: }
! 73903: }
! 73904: return 0;
! 73905: }
! 73906:
! 73907: /*
! 73908: ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
! 73909: ** The Name context of the SELECT statement is pNC. zType is either
! 73910: ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
! 73911: **
! 73912: ** This routine resolves each term of the clause into an expression.
! 73913: ** If the order-by term is an integer I between 1 and N (where N is the
! 73914: ** number of columns in the result set of the SELECT) then the expression
! 73915: ** in the resolution is a copy of the I-th result-set expression. If
! 73916: ** the order-by term is an identify that corresponds to the AS-name of
! 73917: ** a result-set expression, then the term resolves to a copy of the
! 73918: ** result-set expression. Otherwise, the expression is resolved in
! 73919: ** the usual way - using sqlite3ResolveExprNames().
! 73920: **
! 73921: ** This routine returns the number of errors. If errors occur, then
! 73922: ** an appropriate error message might be left in pParse. (OOM errors
! 73923: ** excepted.)
! 73924: */
! 73925: static int resolveOrderGroupBy(
! 73926: NameContext *pNC, /* The name context of the SELECT statement */
! 73927: Select *pSelect, /* The SELECT statement holding pOrderBy */
! 73928: ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
! 73929: const char *zType /* Either "ORDER" or "GROUP", as appropriate */
! 73930: ){
! 73931: int i; /* Loop counter */
! 73932: int iCol; /* Column number */
! 73933: struct ExprList_item *pItem; /* A term of the ORDER BY clause */
! 73934: Parse *pParse; /* Parsing context */
! 73935: int nResult; /* Number of terms in the result set */
! 73936:
! 73937: if( pOrderBy==0 ) return 0;
! 73938: nResult = pSelect->pEList->nExpr;
! 73939: pParse = pNC->pParse;
! 73940: for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
! 73941: Expr *pE = pItem->pExpr;
! 73942: iCol = resolveAsName(pParse, pSelect->pEList, pE);
! 73943: if( iCol>0 ){
! 73944: /* If an AS-name match is found, mark this ORDER BY column as being
! 73945: ** a copy of the iCol-th result-set column. The subsequent call to
! 73946: ** sqlite3ResolveOrderGroupBy() will convert the expression to a
! 73947: ** copy of the iCol-th result-set expression. */
! 73948: pItem->iOrderByCol = (u16)iCol;
! 73949: continue;
! 73950: }
! 73951: if( sqlite3ExprIsInteger(pE, &iCol) ){
! 73952: /* The ORDER BY term is an integer constant. Again, set the column
! 73953: ** number so that sqlite3ResolveOrderGroupBy() will convert the
! 73954: ** order-by term to a copy of the result-set expression */
! 73955: if( iCol<1 ){
! 73956: resolveOutOfRangeError(pParse, zType, i+1, nResult);
! 73957: return 1;
! 73958: }
! 73959: pItem->iOrderByCol = (u16)iCol;
! 73960: continue;
! 73961: }
! 73962:
! 73963: /* Otherwise, treat the ORDER BY term as an ordinary expression */
! 73964: pItem->iOrderByCol = 0;
! 73965: if( sqlite3ResolveExprNames(pNC, pE) ){
! 73966: return 1;
! 73967: }
! 73968: }
! 73969: return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
! 73970: }
! 73971:
! 73972: /*
! 73973: ** Resolve names in the SELECT statement p and all of its descendents.
! 73974: */
! 73975: static int resolveSelectStep(Walker *pWalker, Select *p){
! 73976: NameContext *pOuterNC; /* Context that contains this SELECT */
! 73977: NameContext sNC; /* Name context of this SELECT */
! 73978: int isCompound; /* True if p is a compound select */
! 73979: int nCompound; /* Number of compound terms processed so far */
! 73980: Parse *pParse; /* Parsing context */
! 73981: ExprList *pEList; /* Result set expression list */
! 73982: int i; /* Loop counter */
! 73983: ExprList *pGroupBy; /* The GROUP BY clause */
! 73984: Select *pLeftmost; /* Left-most of SELECT of a compound */
! 73985: sqlite3 *db; /* Database connection */
! 73986:
! 73987:
! 73988: assert( p!=0 );
! 73989: if( p->selFlags & SF_Resolved ){
! 73990: return WRC_Prune;
! 73991: }
! 73992: pOuterNC = pWalker->u.pNC;
! 73993: pParse = pWalker->pParse;
! 73994: db = pParse->db;
! 73995:
! 73996: /* Normally sqlite3SelectExpand() will be called first and will have
! 73997: ** already expanded this SELECT. However, if this is a subquery within
! 73998: ** an expression, sqlite3ResolveExprNames() will be called without a
! 73999: ** prior call to sqlite3SelectExpand(). When that happens, let
! 74000: ** sqlite3SelectPrep() do all of the processing for this SELECT.
! 74001: ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
! 74002: ** this routine in the correct order.
! 74003: */
! 74004: if( (p->selFlags & SF_Expanded)==0 ){
! 74005: sqlite3SelectPrep(pParse, p, pOuterNC);
! 74006: return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
! 74007: }
! 74008:
! 74009: isCompound = p->pPrior!=0;
! 74010: nCompound = 0;
! 74011: pLeftmost = p;
! 74012: while( p ){
! 74013: assert( (p->selFlags & SF_Expanded)!=0 );
! 74014: assert( (p->selFlags & SF_Resolved)==0 );
! 74015: p->selFlags |= SF_Resolved;
! 74016:
! 74017: /* Resolve the expressions in the LIMIT and OFFSET clauses. These
! 74018: ** are not allowed to refer to any names, so pass an empty NameContext.
! 74019: */
! 74020: memset(&sNC, 0, sizeof(sNC));
! 74021: sNC.pParse = pParse;
! 74022: if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
! 74023: sqlite3ResolveExprNames(&sNC, p->pOffset) ){
! 74024: return WRC_Abort;
! 74025: }
! 74026:
! 74027: /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
! 74028: ** resolve the result-set expression list.
! 74029: */
! 74030: sNC.allowAgg = 1;
! 74031: sNC.pSrcList = p->pSrc;
! 74032: sNC.pNext = pOuterNC;
! 74033:
! 74034: /* Resolve names in the result set. */
! 74035: pEList = p->pEList;
! 74036: assert( pEList!=0 );
! 74037: for(i=0; i<pEList->nExpr; i++){
! 74038: Expr *pX = pEList->a[i].pExpr;
! 74039: if( sqlite3ResolveExprNames(&sNC, pX) ){
! 74040: return WRC_Abort;
! 74041: }
! 74042: }
! 74043:
! 74044: /* Recursively resolve names in all subqueries
! 74045: */
! 74046: for(i=0; i<p->pSrc->nSrc; i++){
! 74047: struct SrcList_item *pItem = &p->pSrc->a[i];
! 74048: if( pItem->pSelect ){
! 74049: NameContext *pNC; /* Used to iterate name contexts */
! 74050: int nRef = 0; /* Refcount for pOuterNC and outer contexts */
! 74051: const char *zSavedContext = pParse->zAuthContext;
! 74052:
! 74053: /* Count the total number of references to pOuterNC and all of its
! 74054: ** parent contexts. After resolving references to expressions in
! 74055: ** pItem->pSelect, check if this value has changed. If so, then
! 74056: ** SELECT statement pItem->pSelect must be correlated. Set the
! 74057: ** pItem->isCorrelated flag if this is the case. */
! 74058: for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
! 74059:
! 74060: if( pItem->zName ) pParse->zAuthContext = pItem->zName;
! 74061: sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
! 74062: pParse->zAuthContext = zSavedContext;
! 74063: if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
! 74064:
! 74065: for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
! 74066: assert( pItem->isCorrelated==0 && nRef<=0 );
! 74067: pItem->isCorrelated = (nRef!=0);
! 74068: }
! 74069: }
! 74070:
! 74071: /* If there are no aggregate functions in the result-set, and no GROUP BY
! 74072: ** expression, do not allow aggregates in any of the other expressions.
! 74073: */
! 74074: assert( (p->selFlags & SF_Aggregate)==0 );
! 74075: pGroupBy = p->pGroupBy;
! 74076: if( pGroupBy || sNC.hasAgg ){
! 74077: p->selFlags |= SF_Aggregate;
! 74078: }else{
! 74079: sNC.allowAgg = 0;
! 74080: }
! 74081:
! 74082: /* If a HAVING clause is present, then there must be a GROUP BY clause.
! 74083: */
! 74084: if( p->pHaving && !pGroupBy ){
! 74085: sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
! 74086: return WRC_Abort;
! 74087: }
! 74088:
! 74089: /* Add the expression list to the name-context before parsing the
! 74090: ** other expressions in the SELECT statement. This is so that
! 74091: ** expressions in the WHERE clause (etc.) can refer to expressions by
! 74092: ** aliases in the result set.
! 74093: **
! 74094: ** Minor point: If this is the case, then the expression will be
! 74095: ** re-evaluated for each reference to it.
! 74096: */
! 74097: sNC.pEList = p->pEList;
! 74098: if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
! 74099: sqlite3ResolveExprNames(&sNC, p->pHaving)
! 74100: ){
! 74101: return WRC_Abort;
! 74102: }
! 74103:
! 74104: /* The ORDER BY and GROUP BY clauses may not refer to terms in
! 74105: ** outer queries
! 74106: */
! 74107: sNC.pNext = 0;
! 74108: sNC.allowAgg = 1;
! 74109:
! 74110: /* Process the ORDER BY clause for singleton SELECT statements.
! 74111: ** The ORDER BY clause for compounds SELECT statements is handled
! 74112: ** below, after all of the result-sets for all of the elements of
! 74113: ** the compound have been resolved.
! 74114: */
! 74115: if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
! 74116: return WRC_Abort;
! 74117: }
! 74118: if( db->mallocFailed ){
! 74119: return WRC_Abort;
! 74120: }
! 74121:
! 74122: /* Resolve the GROUP BY clause. At the same time, make sure
! 74123: ** the GROUP BY clause does not contain aggregate functions.
! 74124: */
! 74125: if( pGroupBy ){
! 74126: struct ExprList_item *pItem;
! 74127:
! 74128: if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
! 74129: return WRC_Abort;
! 74130: }
! 74131: for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
! 74132: if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
! 74133: sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
! 74134: "the GROUP BY clause");
! 74135: return WRC_Abort;
! 74136: }
! 74137: }
! 74138: }
! 74139:
! 74140: /* Advance to the next term of the compound
! 74141: */
! 74142: p = p->pPrior;
! 74143: nCompound++;
! 74144: }
! 74145:
! 74146: /* Resolve the ORDER BY on a compound SELECT after all terms of
! 74147: ** the compound have been resolved.
! 74148: */
! 74149: if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
! 74150: return WRC_Abort;
! 74151: }
! 74152:
! 74153: return WRC_Prune;
! 74154: }
! 74155:
! 74156: /*
! 74157: ** This routine walks an expression tree and resolves references to
! 74158: ** table columns and result-set columns. At the same time, do error
! 74159: ** checking on function usage and set a flag if any aggregate functions
! 74160: ** are seen.
! 74161: **
! 74162: ** To resolve table columns references we look for nodes (or subtrees) of the
! 74163: ** form X.Y.Z or Y.Z or just Z where
! 74164: **
! 74165: ** X: The name of a database. Ex: "main" or "temp" or
! 74166: ** the symbolic name assigned to an ATTACH-ed database.
! 74167: **
! 74168: ** Y: The name of a table in a FROM clause. Or in a trigger
! 74169: ** one of the special names "old" or "new".
! 74170: **
! 74171: ** Z: The name of a column in table Y.
! 74172: **
! 74173: ** The node at the root of the subtree is modified as follows:
! 74174: **
! 74175: ** Expr.op Changed to TK_COLUMN
! 74176: ** Expr.pTab Points to the Table object for X.Y
! 74177: ** Expr.iColumn The column index in X.Y. -1 for the rowid.
! 74178: ** Expr.iTable The VDBE cursor number for X.Y
! 74179: **
! 74180: **
! 74181: ** To resolve result-set references, look for expression nodes of the
! 74182: ** form Z (with no X and Y prefix) where the Z matches the right-hand
! 74183: ** size of an AS clause in the result-set of a SELECT. The Z expression
! 74184: ** is replaced by a copy of the left-hand side of the result-set expression.
! 74185: ** Table-name and function resolution occurs on the substituted expression
! 74186: ** tree. For example, in:
! 74187: **
! 74188: ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
! 74189: **
! 74190: ** The "x" term of the order by is replaced by "a+b" to render:
! 74191: **
! 74192: ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
! 74193: **
! 74194: ** Function calls are checked to make sure that the function is
! 74195: ** defined and that the correct number of arguments are specified.
! 74196: ** If the function is an aggregate function, then the pNC->hasAgg is
! 74197: ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
! 74198: ** If an expression contains aggregate functions then the EP_Agg
! 74199: ** property on the expression is set.
! 74200: **
! 74201: ** An error message is left in pParse if anything is amiss. The number
! 74202: ** if errors is returned.
! 74203: */
! 74204: SQLITE_PRIVATE int sqlite3ResolveExprNames(
! 74205: NameContext *pNC, /* Namespace to resolve expressions in. */
! 74206: Expr *pExpr /* The expression to be analyzed. */
! 74207: ){
! 74208: int savedHasAgg;
! 74209: Walker w;
! 74210:
! 74211: if( pExpr==0 ) return 0;
! 74212: #if SQLITE_MAX_EXPR_DEPTH>0
! 74213: {
! 74214: Parse *pParse = pNC->pParse;
! 74215: if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
! 74216: return 1;
! 74217: }
! 74218: pParse->nHeight += pExpr->nHeight;
! 74219: }
! 74220: #endif
! 74221: savedHasAgg = pNC->hasAgg;
! 74222: pNC->hasAgg = 0;
! 74223: w.xExprCallback = resolveExprStep;
! 74224: w.xSelectCallback = resolveSelectStep;
! 74225: w.pParse = pNC->pParse;
! 74226: w.u.pNC = pNC;
! 74227: sqlite3WalkExpr(&w, pExpr);
! 74228: #if SQLITE_MAX_EXPR_DEPTH>0
! 74229: pNC->pParse->nHeight -= pExpr->nHeight;
! 74230: #endif
! 74231: if( pNC->nErr>0 || w.pParse->nErr>0 ){
! 74232: ExprSetProperty(pExpr, EP_Error);
! 74233: }
! 74234: if( pNC->hasAgg ){
! 74235: ExprSetProperty(pExpr, EP_Agg);
! 74236: }else if( savedHasAgg ){
! 74237: pNC->hasAgg = 1;
! 74238: }
! 74239: return ExprHasProperty(pExpr, EP_Error);
! 74240: }
! 74241:
! 74242:
! 74243: /*
! 74244: ** Resolve all names in all expressions of a SELECT and in all
! 74245: ** decendents of the SELECT, including compounds off of p->pPrior,
! 74246: ** subqueries in expressions, and subqueries used as FROM clause
! 74247: ** terms.
! 74248: **
! 74249: ** See sqlite3ResolveExprNames() for a description of the kinds of
! 74250: ** transformations that occur.
! 74251: **
! 74252: ** All SELECT statements should have been expanded using
! 74253: ** sqlite3SelectExpand() prior to invoking this routine.
! 74254: */
! 74255: SQLITE_PRIVATE void sqlite3ResolveSelectNames(
! 74256: Parse *pParse, /* The parser context */
! 74257: Select *p, /* The SELECT statement being coded. */
! 74258: NameContext *pOuterNC /* Name context for parent SELECT statement */
! 74259: ){
! 74260: Walker w;
! 74261:
! 74262: assert( p!=0 );
! 74263: w.xExprCallback = resolveExprStep;
! 74264: w.xSelectCallback = resolveSelectStep;
! 74265: w.pParse = pParse;
! 74266: w.u.pNC = pOuterNC;
! 74267: sqlite3WalkSelect(&w, p);
! 74268: }
! 74269:
! 74270: /************** End of resolve.c *********************************************/
! 74271: /************** Begin file expr.c ********************************************/
! 74272: /*
! 74273: ** 2001 September 15
! 74274: **
! 74275: ** The author disclaims copyright to this source code. In place of
! 74276: ** a legal notice, here is a blessing:
! 74277: **
! 74278: ** May you do good and not evil.
! 74279: ** May you find forgiveness for yourself and forgive others.
! 74280: ** May you share freely, never taking more than you give.
! 74281: **
! 74282: *************************************************************************
! 74283: ** This file contains routines used for analyzing expressions and
! 74284: ** for generating VDBE code that evaluates expressions in SQLite.
! 74285: */
! 74286:
! 74287: /*
! 74288: ** Return the 'affinity' of the expression pExpr if any.
! 74289: **
! 74290: ** If pExpr is a column, a reference to a column via an 'AS' alias,
! 74291: ** or a sub-select with a column as the return value, then the
! 74292: ** affinity of that column is returned. Otherwise, 0x00 is returned,
! 74293: ** indicating no affinity for the expression.
! 74294: **
! 74295: ** i.e. the WHERE clause expresssions in the following statements all
! 74296: ** have an affinity:
! 74297: **
! 74298: ** CREATE TABLE t1(a);
! 74299: ** SELECT * FROM t1 WHERE a;
! 74300: ** SELECT a AS b FROM t1 WHERE b;
! 74301: ** SELECT * FROM t1 WHERE (select a from t1);
! 74302: */
! 74303: SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
! 74304: int op = pExpr->op;
! 74305: if( op==TK_SELECT ){
! 74306: assert( pExpr->flags&EP_xIsSelect );
! 74307: return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
! 74308: }
! 74309: #ifndef SQLITE_OMIT_CAST
! 74310: if( op==TK_CAST ){
! 74311: assert( !ExprHasProperty(pExpr, EP_IntValue) );
! 74312: return sqlite3AffinityType(pExpr->u.zToken);
! 74313: }
! 74314: #endif
! 74315: if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
! 74316: && pExpr->pTab!=0
! 74317: ){
! 74318: /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
! 74319: ** a TK_COLUMN but was previously evaluated and cached in a register */
! 74320: int j = pExpr->iColumn;
! 74321: if( j<0 ) return SQLITE_AFF_INTEGER;
! 74322: assert( pExpr->pTab && j<pExpr->pTab->nCol );
! 74323: return pExpr->pTab->aCol[j].affinity;
! 74324: }
! 74325: return pExpr->affinity;
! 74326: }
! 74327:
! 74328: /*
! 74329: ** Set the explicit collating sequence for an expression to the
! 74330: ** collating sequence supplied in the second argument.
! 74331: */
! 74332: SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr *pExpr, CollSeq *pColl){
! 74333: if( pExpr && pColl ){
! 74334: pExpr->pColl = pColl;
! 74335: pExpr->flags |= EP_ExpCollate;
! 74336: }
! 74337: return pExpr;
! 74338: }
! 74339:
! 74340: /*
! 74341: ** Set the collating sequence for expression pExpr to be the collating
! 74342: ** sequence named by pToken. Return a pointer to the revised expression.
! 74343: ** The collating sequence is marked as "explicit" using the EP_ExpCollate
! 74344: ** flag. An explicit collating sequence will override implicit
! 74345: ** collating sequences.
! 74346: */
! 74347: SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr *pExpr, Token *pCollName){
! 74348: char *zColl = 0; /* Dequoted name of collation sequence */
! 74349: CollSeq *pColl;
! 74350: sqlite3 *db = pParse->db;
! 74351: zColl = sqlite3NameFromToken(db, pCollName);
! 74352: pColl = sqlite3LocateCollSeq(pParse, zColl);
! 74353: sqlite3ExprSetColl(pExpr, pColl);
! 74354: sqlite3DbFree(db, zColl);
! 74355: return pExpr;
! 74356: }
! 74357:
! 74358: /*
! 74359: ** Return the default collation sequence for the expression pExpr. If
! 74360: ** there is no default collation type, return 0.
! 74361: */
! 74362: SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
! 74363: CollSeq *pColl = 0;
! 74364: Expr *p = pExpr;
! 74365: while( p ){
! 74366: int op;
! 74367: pColl = p->pColl;
! 74368: if( pColl ) break;
! 74369: op = p->op;
! 74370: if( p->pTab!=0 && (
! 74371: op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER
! 74372: )){
! 74373: /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
! 74374: ** a TK_COLUMN but was previously evaluated and cached in a register */
! 74375: const char *zColl;
! 74376: int j = p->iColumn;
! 74377: if( j>=0 ){
! 74378: sqlite3 *db = pParse->db;
! 74379: zColl = p->pTab->aCol[j].zColl;
! 74380: pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
! 74381: pExpr->pColl = pColl;
! 74382: }
! 74383: break;
! 74384: }
! 74385: if( op!=TK_CAST && op!=TK_UPLUS ){
! 74386: break;
! 74387: }
! 74388: p = p->pLeft;
! 74389: }
! 74390: if( sqlite3CheckCollSeq(pParse, pColl) ){
! 74391: pColl = 0;
! 74392: }
! 74393: return pColl;
! 74394: }
! 74395:
! 74396: /*
! 74397: ** pExpr is an operand of a comparison operator. aff2 is the
! 74398: ** type affinity of the other operand. This routine returns the
! 74399: ** type affinity that should be used for the comparison operator.
! 74400: */
! 74401: SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
! 74402: char aff1 = sqlite3ExprAffinity(pExpr);
! 74403: if( aff1 && aff2 ){
! 74404: /* Both sides of the comparison are columns. If one has numeric
! 74405: ** affinity, use that. Otherwise use no affinity.
! 74406: */
! 74407: if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
! 74408: return SQLITE_AFF_NUMERIC;
! 74409: }else{
! 74410: return SQLITE_AFF_NONE;
! 74411: }
! 74412: }else if( !aff1 && !aff2 ){
! 74413: /* Neither side of the comparison is a column. Compare the
! 74414: ** results directly.
! 74415: */
! 74416: return SQLITE_AFF_NONE;
! 74417: }else{
! 74418: /* One side is a column, the other is not. Use the columns affinity. */
! 74419: assert( aff1==0 || aff2==0 );
! 74420: return (aff1 + aff2);
! 74421: }
! 74422: }
! 74423:
! 74424: /*
! 74425: ** pExpr is a comparison operator. Return the type affinity that should
! 74426: ** be applied to both operands prior to doing the comparison.
! 74427: */
! 74428: static char comparisonAffinity(Expr *pExpr){
! 74429: char aff;
! 74430: assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
! 74431: pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
! 74432: pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
! 74433: assert( pExpr->pLeft );
! 74434: aff = sqlite3ExprAffinity(pExpr->pLeft);
! 74435: if( pExpr->pRight ){
! 74436: aff = sqlite3CompareAffinity(pExpr->pRight, aff);
! 74437: }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
! 74438: aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
! 74439: }else if( !aff ){
! 74440: aff = SQLITE_AFF_NONE;
! 74441: }
! 74442: return aff;
! 74443: }
! 74444:
! 74445: /*
! 74446: ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
! 74447: ** idx_affinity is the affinity of an indexed column. Return true
! 74448: ** if the index with affinity idx_affinity may be used to implement
! 74449: ** the comparison in pExpr.
! 74450: */
! 74451: SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
! 74452: char aff = comparisonAffinity(pExpr);
! 74453: switch( aff ){
! 74454: case SQLITE_AFF_NONE:
! 74455: return 1;
! 74456: case SQLITE_AFF_TEXT:
! 74457: return idx_affinity==SQLITE_AFF_TEXT;
! 74458: default:
! 74459: return sqlite3IsNumericAffinity(idx_affinity);
! 74460: }
! 74461: }
! 74462:
! 74463: /*
! 74464: ** Return the P5 value that should be used for a binary comparison
! 74465: ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
! 74466: */
! 74467: static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
! 74468: u8 aff = (char)sqlite3ExprAffinity(pExpr2);
! 74469: aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
! 74470: return aff;
! 74471: }
! 74472:
! 74473: /*
! 74474: ** Return a pointer to the collation sequence that should be used by
! 74475: ** a binary comparison operator comparing pLeft and pRight.
! 74476: **
! 74477: ** If the left hand expression has a collating sequence type, then it is
! 74478: ** used. Otherwise the collation sequence for the right hand expression
! 74479: ** is used, or the default (BINARY) if neither expression has a collating
! 74480: ** type.
! 74481: **
! 74482: ** Argument pRight (but not pLeft) may be a null pointer. In this case,
! 74483: ** it is not considered.
! 74484: */
! 74485: SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
! 74486: Parse *pParse,
! 74487: Expr *pLeft,
! 74488: Expr *pRight
! 74489: ){
! 74490: CollSeq *pColl;
! 74491: assert( pLeft );
! 74492: if( pLeft->flags & EP_ExpCollate ){
! 74493: assert( pLeft->pColl );
! 74494: pColl = pLeft->pColl;
! 74495: }else if( pRight && pRight->flags & EP_ExpCollate ){
! 74496: assert( pRight->pColl );
! 74497: pColl = pRight->pColl;
! 74498: }else{
! 74499: pColl = sqlite3ExprCollSeq(pParse, pLeft);
! 74500: if( !pColl ){
! 74501: pColl = sqlite3ExprCollSeq(pParse, pRight);
! 74502: }
! 74503: }
! 74504: return pColl;
! 74505: }
! 74506:
! 74507: /*
! 74508: ** Generate code for a comparison operator.
! 74509: */
! 74510: static int codeCompare(
! 74511: Parse *pParse, /* The parsing (and code generating) context */
! 74512: Expr *pLeft, /* The left operand */
! 74513: Expr *pRight, /* The right operand */
! 74514: int opcode, /* The comparison opcode */
! 74515: int in1, int in2, /* Register holding operands */
! 74516: int dest, /* Jump here if true. */
! 74517: int jumpIfNull /* If true, jump if either operand is NULL */
! 74518: ){
! 74519: int p5;
! 74520: int addr;
! 74521: CollSeq *p4;
! 74522:
! 74523: p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
! 74524: p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
! 74525: addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
! 74526: (void*)p4, P4_COLLSEQ);
! 74527: sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
! 74528: return addr;
! 74529: }
! 74530:
! 74531: #if SQLITE_MAX_EXPR_DEPTH>0
! 74532: /*
! 74533: ** Check that argument nHeight is less than or equal to the maximum
! 74534: ** expression depth allowed. If it is not, leave an error message in
! 74535: ** pParse.
! 74536: */
! 74537: SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
! 74538: int rc = SQLITE_OK;
! 74539: int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
! 74540: if( nHeight>mxHeight ){
! 74541: sqlite3ErrorMsg(pParse,
! 74542: "Expression tree is too large (maximum depth %d)", mxHeight
! 74543: );
! 74544: rc = SQLITE_ERROR;
! 74545: }
! 74546: return rc;
! 74547: }
! 74548:
! 74549: /* The following three functions, heightOfExpr(), heightOfExprList()
! 74550: ** and heightOfSelect(), are used to determine the maximum height
! 74551: ** of any expression tree referenced by the structure passed as the
! 74552: ** first argument.
! 74553: **
! 74554: ** If this maximum height is greater than the current value pointed
! 74555: ** to by pnHeight, the second parameter, then set *pnHeight to that
! 74556: ** value.
! 74557: */
! 74558: static void heightOfExpr(Expr *p, int *pnHeight){
! 74559: if( p ){
! 74560: if( p->nHeight>*pnHeight ){
! 74561: *pnHeight = p->nHeight;
! 74562: }
! 74563: }
! 74564: }
! 74565: static void heightOfExprList(ExprList *p, int *pnHeight){
! 74566: if( p ){
! 74567: int i;
! 74568: for(i=0; i<p->nExpr; i++){
! 74569: heightOfExpr(p->a[i].pExpr, pnHeight);
! 74570: }
! 74571: }
! 74572: }
! 74573: static void heightOfSelect(Select *p, int *pnHeight){
! 74574: if( p ){
! 74575: heightOfExpr(p->pWhere, pnHeight);
! 74576: heightOfExpr(p->pHaving, pnHeight);
! 74577: heightOfExpr(p->pLimit, pnHeight);
! 74578: heightOfExpr(p->pOffset, pnHeight);
! 74579: heightOfExprList(p->pEList, pnHeight);
! 74580: heightOfExprList(p->pGroupBy, pnHeight);
! 74581: heightOfExprList(p->pOrderBy, pnHeight);
! 74582: heightOfSelect(p->pPrior, pnHeight);
! 74583: }
! 74584: }
! 74585:
! 74586: /*
! 74587: ** Set the Expr.nHeight variable in the structure passed as an
! 74588: ** argument. An expression with no children, Expr.pList or
! 74589: ** Expr.pSelect member has a height of 1. Any other expression
! 74590: ** has a height equal to the maximum height of any other
! 74591: ** referenced Expr plus one.
! 74592: */
! 74593: static void exprSetHeight(Expr *p){
! 74594: int nHeight = 0;
! 74595: heightOfExpr(p->pLeft, &nHeight);
! 74596: heightOfExpr(p->pRight, &nHeight);
! 74597: if( ExprHasProperty(p, EP_xIsSelect) ){
! 74598: heightOfSelect(p->x.pSelect, &nHeight);
! 74599: }else{
! 74600: heightOfExprList(p->x.pList, &nHeight);
! 74601: }
! 74602: p->nHeight = nHeight + 1;
! 74603: }
! 74604:
! 74605: /*
! 74606: ** Set the Expr.nHeight variable using the exprSetHeight() function. If
! 74607: ** the height is greater than the maximum allowed expression depth,
! 74608: ** leave an error in pParse.
! 74609: */
! 74610: SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
! 74611: exprSetHeight(p);
! 74612: sqlite3ExprCheckHeight(pParse, p->nHeight);
! 74613: }
! 74614:
! 74615: /*
! 74616: ** Return the maximum height of any expression tree referenced
! 74617: ** by the select statement passed as an argument.
! 74618: */
! 74619: SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
! 74620: int nHeight = 0;
! 74621: heightOfSelect(p, &nHeight);
! 74622: return nHeight;
! 74623: }
! 74624: #else
! 74625: #define exprSetHeight(y)
! 74626: #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
! 74627:
! 74628: /*
! 74629: ** This routine is the core allocator for Expr nodes.
! 74630: **
! 74631: ** Construct a new expression node and return a pointer to it. Memory
! 74632: ** for this node and for the pToken argument is a single allocation
! 74633: ** obtained from sqlite3DbMalloc(). The calling function
! 74634: ** is responsible for making sure the node eventually gets freed.
! 74635: **
! 74636: ** If dequote is true, then the token (if it exists) is dequoted.
! 74637: ** If dequote is false, no dequoting is performance. The deQuote
! 74638: ** parameter is ignored if pToken is NULL or if the token does not
! 74639: ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
! 74640: ** then the EP_DblQuoted flag is set on the expression node.
! 74641: **
! 74642: ** Special case: If op==TK_INTEGER and pToken points to a string that
! 74643: ** can be translated into a 32-bit integer, then the token is not
! 74644: ** stored in u.zToken. Instead, the integer values is written
! 74645: ** into u.iValue and the EP_IntValue flag is set. No extra storage
! 74646: ** is allocated to hold the integer text and the dequote flag is ignored.
! 74647: */
! 74648: SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
! 74649: sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
! 74650: int op, /* Expression opcode */
! 74651: const Token *pToken, /* Token argument. Might be NULL */
! 74652: int dequote /* True to dequote */
! 74653: ){
! 74654: Expr *pNew;
! 74655: int nExtra = 0;
! 74656: int iValue = 0;
! 74657:
! 74658: if( pToken ){
! 74659: if( op!=TK_INTEGER || pToken->z==0
! 74660: || sqlite3GetInt32(pToken->z, &iValue)==0 ){
! 74661: nExtra = pToken->n+1;
! 74662: assert( iValue>=0 );
! 74663: }
! 74664: }
! 74665: pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
! 74666: if( pNew ){
! 74667: pNew->op = (u8)op;
! 74668: pNew->iAgg = -1;
! 74669: if( pToken ){
! 74670: if( nExtra==0 ){
! 74671: pNew->flags |= EP_IntValue;
! 74672: pNew->u.iValue = iValue;
! 74673: }else{
! 74674: int c;
! 74675: pNew->u.zToken = (char*)&pNew[1];
! 74676: assert( pToken->z!=0 || pToken->n==0 );
! 74677: if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
! 74678: pNew->u.zToken[pToken->n] = 0;
! 74679: if( dequote && nExtra>=3
! 74680: && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
! 74681: sqlite3Dequote(pNew->u.zToken);
! 74682: if( c=='"' ) pNew->flags |= EP_DblQuoted;
! 74683: }
! 74684: }
! 74685: }
! 74686: #if SQLITE_MAX_EXPR_DEPTH>0
! 74687: pNew->nHeight = 1;
! 74688: #endif
! 74689: }
! 74690: return pNew;
! 74691: }
! 74692:
! 74693: /*
! 74694: ** Allocate a new expression node from a zero-terminated token that has
! 74695: ** already been dequoted.
! 74696: */
! 74697: SQLITE_PRIVATE Expr *sqlite3Expr(
! 74698: sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
! 74699: int op, /* Expression opcode */
! 74700: const char *zToken /* Token argument. Might be NULL */
! 74701: ){
! 74702: Token x;
! 74703: x.z = zToken;
! 74704: x.n = zToken ? sqlite3Strlen30(zToken) : 0;
! 74705: return sqlite3ExprAlloc(db, op, &x, 0);
! 74706: }
! 74707:
! 74708: /*
! 74709: ** Attach subtrees pLeft and pRight to the Expr node pRoot.
! 74710: **
! 74711: ** If pRoot==NULL that means that a memory allocation error has occurred.
! 74712: ** In that case, delete the subtrees pLeft and pRight.
! 74713: */
! 74714: SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
! 74715: sqlite3 *db,
! 74716: Expr *pRoot,
! 74717: Expr *pLeft,
! 74718: Expr *pRight
! 74719: ){
! 74720: if( pRoot==0 ){
! 74721: assert( db->mallocFailed );
! 74722: sqlite3ExprDelete(db, pLeft);
! 74723: sqlite3ExprDelete(db, pRight);
! 74724: }else{
! 74725: if( pRight ){
! 74726: pRoot->pRight = pRight;
! 74727: if( pRight->flags & EP_ExpCollate ){
! 74728: pRoot->flags |= EP_ExpCollate;
! 74729: pRoot->pColl = pRight->pColl;
! 74730: }
! 74731: }
! 74732: if( pLeft ){
! 74733: pRoot->pLeft = pLeft;
! 74734: if( pLeft->flags & EP_ExpCollate ){
! 74735: pRoot->flags |= EP_ExpCollate;
! 74736: pRoot->pColl = pLeft->pColl;
! 74737: }
! 74738: }
! 74739: exprSetHeight(pRoot);
! 74740: }
! 74741: }
! 74742:
! 74743: /*
! 74744: ** Allocate a Expr node which joins as many as two subtrees.
! 74745: **
! 74746: ** One or both of the subtrees can be NULL. Return a pointer to the new
! 74747: ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
! 74748: ** free the subtrees and return NULL.
! 74749: */
! 74750: SQLITE_PRIVATE Expr *sqlite3PExpr(
! 74751: Parse *pParse, /* Parsing context */
! 74752: int op, /* Expression opcode */
! 74753: Expr *pLeft, /* Left operand */
! 74754: Expr *pRight, /* Right operand */
! 74755: const Token *pToken /* Argument token */
! 74756: ){
! 74757: Expr *p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
! 74758: sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
! 74759: if( p ) {
! 74760: sqlite3ExprCheckHeight(pParse, p->nHeight);
! 74761: }
! 74762: return p;
! 74763: }
! 74764:
! 74765: /*
! 74766: ** Join two expressions using an AND operator. If either expression is
! 74767: ** NULL, then just return the other expression.
! 74768: */
! 74769: SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
! 74770: if( pLeft==0 ){
! 74771: return pRight;
! 74772: }else if( pRight==0 ){
! 74773: return pLeft;
! 74774: }else{
! 74775: Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
! 74776: sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
! 74777: return pNew;
! 74778: }
! 74779: }
! 74780:
! 74781: /*
! 74782: ** Construct a new expression node for a function with multiple
! 74783: ** arguments.
! 74784: */
! 74785: SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
! 74786: Expr *pNew;
! 74787: sqlite3 *db = pParse->db;
! 74788: assert( pToken );
! 74789: pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
! 74790: if( pNew==0 ){
! 74791: sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
! 74792: return 0;
! 74793: }
! 74794: pNew->x.pList = pList;
! 74795: assert( !ExprHasProperty(pNew, EP_xIsSelect) );
! 74796: sqlite3ExprSetHeight(pParse, pNew);
! 74797: return pNew;
! 74798: }
! 74799:
! 74800: /*
! 74801: ** Assign a variable number to an expression that encodes a wildcard
! 74802: ** in the original SQL statement.
! 74803: **
! 74804: ** Wildcards consisting of a single "?" are assigned the next sequential
! 74805: ** variable number.
! 74806: **
! 74807: ** Wildcards of the form "?nnn" are assigned the number "nnn". We make
! 74808: ** sure "nnn" is not too be to avoid a denial of service attack when
! 74809: ** the SQL statement comes from an external source.
! 74810: **
! 74811: ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
! 74812: ** as the previous instance of the same wildcard. Or if this is the first
! 74813: ** instance of the wildcard, the next sequenial variable number is
! 74814: ** assigned.
! 74815: */
! 74816: SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
! 74817: sqlite3 *db = pParse->db;
! 74818: const char *z;
! 74819:
! 74820: if( pExpr==0 ) return;
! 74821: assert( !ExprHasAnyProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
! 74822: z = pExpr->u.zToken;
! 74823: assert( z!=0 );
! 74824: assert( z[0]!=0 );
! 74825: if( z[1]==0 ){
! 74826: /* Wildcard of the form "?". Assign the next variable number */
! 74827: assert( z[0]=='?' );
! 74828: pExpr->iColumn = (ynVar)(++pParse->nVar);
! 74829: }else{
! 74830: ynVar x = 0;
! 74831: u32 n = sqlite3Strlen30(z);
! 74832: if( z[0]=='?' ){
! 74833: /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
! 74834: ** use it as the variable number */
! 74835: i64 i;
! 74836: int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
! 74837: pExpr->iColumn = x = (ynVar)i;
! 74838: testcase( i==0 );
! 74839: testcase( i==1 );
! 74840: testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
! 74841: testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
! 74842: if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
! 74843: sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
! 74844: db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
! 74845: x = 0;
! 74846: }
! 74847: if( i>pParse->nVar ){
! 74848: pParse->nVar = (int)i;
! 74849: }
! 74850: }else{
! 74851: /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
! 74852: ** number as the prior appearance of the same name, or if the name
! 74853: ** has never appeared before, reuse the same variable number
! 74854: */
! 74855: ynVar i;
! 74856: for(i=0; i<pParse->nzVar; i++){
! 74857: if( pParse->azVar[i] && memcmp(pParse->azVar[i],z,n+1)==0 ){
! 74858: pExpr->iColumn = x = (ynVar)i+1;
! 74859: break;
! 74860: }
! 74861: }
! 74862: if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
! 74863: }
! 74864: if( x>0 ){
! 74865: if( x>pParse->nzVar ){
! 74866: char **a;
! 74867: a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
! 74868: if( a==0 ) return; /* Error reported through db->mallocFailed */
! 74869: pParse->azVar = a;
! 74870: memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
! 74871: pParse->nzVar = x;
! 74872: }
! 74873: if( z[0]!='?' || pParse->azVar[x-1]==0 ){
! 74874: sqlite3DbFree(db, pParse->azVar[x-1]);
! 74875: pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
! 74876: }
! 74877: }
! 74878: }
! 74879: if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
! 74880: sqlite3ErrorMsg(pParse, "too many SQL variables");
! 74881: }
! 74882: }
! 74883:
! 74884: /*
! 74885: ** Recursively delete an expression tree.
! 74886: */
! 74887: SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
! 74888: if( p==0 ) return;
! 74889: /* Sanity check: Assert that the IntValue is non-negative if it exists */
! 74890: assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
! 74891: if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
! 74892: sqlite3ExprDelete(db, p->pLeft);
! 74893: sqlite3ExprDelete(db, p->pRight);
! 74894: if( !ExprHasProperty(p, EP_Reduced) && (p->flags2 & EP2_MallocedToken)!=0 ){
! 74895: sqlite3DbFree(db, p->u.zToken);
! 74896: }
! 74897: if( ExprHasProperty(p, EP_xIsSelect) ){
! 74898: sqlite3SelectDelete(db, p->x.pSelect);
! 74899: }else{
! 74900: sqlite3ExprListDelete(db, p->x.pList);
! 74901: }
! 74902: }
! 74903: if( !ExprHasProperty(p, EP_Static) ){
! 74904: sqlite3DbFree(db, p);
! 74905: }
! 74906: }
! 74907:
! 74908: /*
! 74909: ** Return the number of bytes allocated for the expression structure
! 74910: ** passed as the first argument. This is always one of EXPR_FULLSIZE,
! 74911: ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
! 74912: */
! 74913: static int exprStructSize(Expr *p){
! 74914: if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
! 74915: if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
! 74916: return EXPR_FULLSIZE;
! 74917: }
! 74918:
! 74919: /*
! 74920: ** The dupedExpr*Size() routines each return the number of bytes required
! 74921: ** to store a copy of an expression or expression tree. They differ in
! 74922: ** how much of the tree is measured.
! 74923: **
! 74924: ** dupedExprStructSize() Size of only the Expr structure
! 74925: ** dupedExprNodeSize() Size of Expr + space for token
! 74926: ** dupedExprSize() Expr + token + subtree components
! 74927: **
! 74928: ***************************************************************************
! 74929: **
! 74930: ** The dupedExprStructSize() function returns two values OR-ed together:
! 74931: ** (1) the space required for a copy of the Expr structure only and
! 74932: ** (2) the EP_xxx flags that indicate what the structure size should be.
! 74933: ** The return values is always one of:
! 74934: **
! 74935: ** EXPR_FULLSIZE
! 74936: ** EXPR_REDUCEDSIZE | EP_Reduced
! 74937: ** EXPR_TOKENONLYSIZE | EP_TokenOnly
! 74938: **
! 74939: ** The size of the structure can be found by masking the return value
! 74940: ** of this routine with 0xfff. The flags can be found by masking the
! 74941: ** return value with EP_Reduced|EP_TokenOnly.
! 74942: **
! 74943: ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
! 74944: ** (unreduced) Expr objects as they or originally constructed by the parser.
! 74945: ** During expression analysis, extra information is computed and moved into
! 74946: ** later parts of teh Expr object and that extra information might get chopped
! 74947: ** off if the expression is reduced. Note also that it does not work to
! 74948: ** make a EXPRDUP_REDUCE copy of a reduced expression. It is only legal
! 74949: ** to reduce a pristine expression tree from the parser. The implementation
! 74950: ** of dupedExprStructSize() contain multiple assert() statements that attempt
! 74951: ** to enforce this constraint.
! 74952: */
! 74953: static int dupedExprStructSize(Expr *p, int flags){
! 74954: int nSize;
! 74955: assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
! 74956: if( 0==(flags&EXPRDUP_REDUCE) ){
! 74957: nSize = EXPR_FULLSIZE;
! 74958: }else{
! 74959: assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
! 74960: assert( !ExprHasProperty(p, EP_FromJoin) );
! 74961: assert( (p->flags2 & EP2_MallocedToken)==0 );
! 74962: assert( (p->flags2 & EP2_Irreducible)==0 );
! 74963: if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
! 74964: nSize = EXPR_REDUCEDSIZE | EP_Reduced;
! 74965: }else{
! 74966: nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
! 74967: }
! 74968: }
! 74969: return nSize;
! 74970: }
! 74971:
! 74972: /*
! 74973: ** This function returns the space in bytes required to store the copy
! 74974: ** of the Expr structure and a copy of the Expr.u.zToken string (if that
! 74975: ** string is defined.)
! 74976: */
! 74977: static int dupedExprNodeSize(Expr *p, int flags){
! 74978: int nByte = dupedExprStructSize(p, flags) & 0xfff;
! 74979: if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
! 74980: nByte += sqlite3Strlen30(p->u.zToken)+1;
! 74981: }
! 74982: return ROUND8(nByte);
! 74983: }
! 74984:
! 74985: /*
! 74986: ** Return the number of bytes required to create a duplicate of the
! 74987: ** expression passed as the first argument. The second argument is a
! 74988: ** mask containing EXPRDUP_XXX flags.
! 74989: **
! 74990: ** The value returned includes space to create a copy of the Expr struct
! 74991: ** itself and the buffer referred to by Expr.u.zToken, if any.
! 74992: **
! 74993: ** If the EXPRDUP_REDUCE flag is set, then the return value includes
! 74994: ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
! 74995: ** and Expr.pRight variables (but not for any structures pointed to or
! 74996: ** descended from the Expr.x.pList or Expr.x.pSelect variables).
! 74997: */
! 74998: static int dupedExprSize(Expr *p, int flags){
! 74999: int nByte = 0;
! 75000: if( p ){
! 75001: nByte = dupedExprNodeSize(p, flags);
! 75002: if( flags&EXPRDUP_REDUCE ){
! 75003: nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
! 75004: }
! 75005: }
! 75006: return nByte;
! 75007: }
! 75008:
! 75009: /*
! 75010: ** This function is similar to sqlite3ExprDup(), except that if pzBuffer
! 75011: ** is not NULL then *pzBuffer is assumed to point to a buffer large enough
! 75012: ** to store the copy of expression p, the copies of p->u.zToken
! 75013: ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
! 75014: ** if any. Before returning, *pzBuffer is set to the first byte passed the
! 75015: ** portion of the buffer copied into by this function.
! 75016: */
! 75017: static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
! 75018: Expr *pNew = 0; /* Value to return */
! 75019: if( p ){
! 75020: const int isReduced = (flags&EXPRDUP_REDUCE);
! 75021: u8 *zAlloc;
! 75022: u32 staticFlag = 0;
! 75023:
! 75024: assert( pzBuffer==0 || isReduced );
! 75025:
! 75026: /* Figure out where to write the new Expr structure. */
! 75027: if( pzBuffer ){
! 75028: zAlloc = *pzBuffer;
! 75029: staticFlag = EP_Static;
! 75030: }else{
! 75031: zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
! 75032: }
! 75033: pNew = (Expr *)zAlloc;
! 75034:
! 75035: if( pNew ){
! 75036: /* Set nNewSize to the size allocated for the structure pointed to
! 75037: ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
! 75038: ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
! 75039: ** by the copy of the p->u.zToken string (if any).
! 75040: */
! 75041: const unsigned nStructSize = dupedExprStructSize(p, flags);
! 75042: const int nNewSize = nStructSize & 0xfff;
! 75043: int nToken;
! 75044: if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
! 75045: nToken = sqlite3Strlen30(p->u.zToken) + 1;
! 75046: }else{
! 75047: nToken = 0;
! 75048: }
! 75049: if( isReduced ){
! 75050: assert( ExprHasProperty(p, EP_Reduced)==0 );
! 75051: memcpy(zAlloc, p, nNewSize);
! 75052: }else{
! 75053: int nSize = exprStructSize(p);
! 75054: memcpy(zAlloc, p, nSize);
! 75055: memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
! 75056: }
! 75057:
! 75058: /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
! 75059: pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
! 75060: pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
! 75061: pNew->flags |= staticFlag;
! 75062:
! 75063: /* Copy the p->u.zToken string, if any. */
! 75064: if( nToken ){
! 75065: char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
! 75066: memcpy(zToken, p->u.zToken, nToken);
! 75067: }
! 75068:
! 75069: if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
! 75070: /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
! 75071: if( ExprHasProperty(p, EP_xIsSelect) ){
! 75072: pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
! 75073: }else{
! 75074: pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
! 75075: }
! 75076: }
! 75077:
! 75078: /* Fill in pNew->pLeft and pNew->pRight. */
! 75079: if( ExprHasAnyProperty(pNew, EP_Reduced|EP_TokenOnly) ){
! 75080: zAlloc += dupedExprNodeSize(p, flags);
! 75081: if( ExprHasProperty(pNew, EP_Reduced) ){
! 75082: pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
! 75083: pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
! 75084: }
! 75085: if( pzBuffer ){
! 75086: *pzBuffer = zAlloc;
! 75087: }
! 75088: }else{
! 75089: pNew->flags2 = 0;
! 75090: if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
! 75091: pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
! 75092: pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
! 75093: }
! 75094: }
! 75095:
! 75096: }
! 75097: }
! 75098: return pNew;
! 75099: }
! 75100:
! 75101: /*
! 75102: ** The following group of routines make deep copies of expressions,
! 75103: ** expression lists, ID lists, and select statements. The copies can
! 75104: ** be deleted (by being passed to their respective ...Delete() routines)
! 75105: ** without effecting the originals.
! 75106: **
! 75107: ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
! 75108: ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
! 75109: ** by subsequent calls to sqlite*ListAppend() routines.
! 75110: **
! 75111: ** Any tables that the SrcList might point to are not duplicated.
! 75112: **
! 75113: ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
! 75114: ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
! 75115: ** truncated version of the usual Expr structure that will be stored as
! 75116: ** part of the in-memory representation of the database schema.
! 75117: */
! 75118: SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
! 75119: return exprDup(db, p, flags, 0);
! 75120: }
! 75121: SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
! 75122: ExprList *pNew;
! 75123: struct ExprList_item *pItem, *pOldItem;
! 75124: int i;
! 75125: if( p==0 ) return 0;
! 75126: pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
! 75127: if( pNew==0 ) return 0;
! 75128: pNew->iECursor = 0;
! 75129: pNew->nExpr = pNew->nAlloc = p->nExpr;
! 75130: pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
! 75131: if( pItem==0 ){
! 75132: sqlite3DbFree(db, pNew);
! 75133: return 0;
! 75134: }
! 75135: pOldItem = p->a;
! 75136: for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
! 75137: Expr *pOldExpr = pOldItem->pExpr;
! 75138: pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
! 75139: pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
! 75140: pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
! 75141: pItem->sortOrder = pOldItem->sortOrder;
! 75142: pItem->done = 0;
! 75143: pItem->iOrderByCol = pOldItem->iOrderByCol;
! 75144: pItem->iAlias = pOldItem->iAlias;
! 75145: }
! 75146: return pNew;
! 75147: }
! 75148:
! 75149: /*
! 75150: ** If cursors, triggers, views and subqueries are all omitted from
! 75151: ** the build, then none of the following routines, except for
! 75152: ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
! 75153: ** called with a NULL argument.
! 75154: */
! 75155: #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
! 75156: || !defined(SQLITE_OMIT_SUBQUERY)
! 75157: SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
! 75158: SrcList *pNew;
! 75159: int i;
! 75160: int nByte;
! 75161: if( p==0 ) return 0;
! 75162: nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
! 75163: pNew = sqlite3DbMallocRaw(db, nByte );
! 75164: if( pNew==0 ) return 0;
! 75165: pNew->nSrc = pNew->nAlloc = p->nSrc;
! 75166: for(i=0; i<p->nSrc; i++){
! 75167: struct SrcList_item *pNewItem = &pNew->a[i];
! 75168: struct SrcList_item *pOldItem = &p->a[i];
! 75169: Table *pTab;
! 75170: pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
! 75171: pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
! 75172: pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
! 75173: pNewItem->jointype = pOldItem->jointype;
! 75174: pNewItem->iCursor = pOldItem->iCursor;
! 75175: pNewItem->addrFillSub = pOldItem->addrFillSub;
! 75176: pNewItem->regReturn = pOldItem->regReturn;
! 75177: pNewItem->isCorrelated = pOldItem->isCorrelated;
! 75178: pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
! 75179: pNewItem->notIndexed = pOldItem->notIndexed;
! 75180: pNewItem->pIndex = pOldItem->pIndex;
! 75181: pTab = pNewItem->pTab = pOldItem->pTab;
! 75182: if( pTab ){
! 75183: pTab->nRef++;
! 75184: }
! 75185: pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
! 75186: pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
! 75187: pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
! 75188: pNewItem->colUsed = pOldItem->colUsed;
! 75189: }
! 75190: return pNew;
! 75191: }
! 75192: SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
! 75193: IdList *pNew;
! 75194: int i;
! 75195: if( p==0 ) return 0;
! 75196: pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
! 75197: if( pNew==0 ) return 0;
! 75198: pNew->nId = pNew->nAlloc = p->nId;
! 75199: pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
! 75200: if( pNew->a==0 ){
! 75201: sqlite3DbFree(db, pNew);
! 75202: return 0;
! 75203: }
! 75204: for(i=0; i<p->nId; i++){
! 75205: struct IdList_item *pNewItem = &pNew->a[i];
! 75206: struct IdList_item *pOldItem = &p->a[i];
! 75207: pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
! 75208: pNewItem->idx = pOldItem->idx;
! 75209: }
! 75210: return pNew;
! 75211: }
! 75212: SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
! 75213: Select *pNew, *pPrior;
! 75214: if( p==0 ) return 0;
! 75215: pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
! 75216: if( pNew==0 ) return 0;
! 75217: pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
! 75218: pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
! 75219: pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
! 75220: pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
! 75221: pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
! 75222: pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
! 75223: pNew->op = p->op;
! 75224: pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
! 75225: if( pPrior ) pPrior->pNext = pNew;
! 75226: pNew->pNext = 0;
! 75227: pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
! 75228: pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
! 75229: pNew->iLimit = 0;
! 75230: pNew->iOffset = 0;
! 75231: pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
! 75232: pNew->pRightmost = 0;
! 75233: pNew->addrOpenEphm[0] = -1;
! 75234: pNew->addrOpenEphm[1] = -1;
! 75235: pNew->addrOpenEphm[2] = -1;
! 75236: return pNew;
! 75237: }
! 75238: #else
! 75239: SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
! 75240: assert( p==0 );
! 75241: return 0;
! 75242: }
! 75243: #endif
! 75244:
! 75245:
! 75246: /*
! 75247: ** Add a new element to the end of an expression list. If pList is
! 75248: ** initially NULL, then create a new expression list.
! 75249: **
! 75250: ** If a memory allocation error occurs, the entire list is freed and
! 75251: ** NULL is returned. If non-NULL is returned, then it is guaranteed
! 75252: ** that the new entry was successfully appended.
! 75253: */
! 75254: SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
! 75255: Parse *pParse, /* Parsing context */
! 75256: ExprList *pList, /* List to which to append. Might be NULL */
! 75257: Expr *pExpr /* Expression to be appended. Might be NULL */
! 75258: ){
! 75259: sqlite3 *db = pParse->db;
! 75260: if( pList==0 ){
! 75261: pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
! 75262: if( pList==0 ){
! 75263: goto no_mem;
! 75264: }
! 75265: assert( pList->nAlloc==0 );
! 75266: }
! 75267: if( pList->nAlloc<=pList->nExpr ){
! 75268: struct ExprList_item *a;
! 75269: int n = pList->nAlloc*2 + 4;
! 75270: a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
! 75271: if( a==0 ){
! 75272: goto no_mem;
! 75273: }
! 75274: pList->a = a;
! 75275: pList->nAlloc = sqlite3DbMallocSize(db, a)/sizeof(a[0]);
! 75276: }
! 75277: assert( pList->a!=0 );
! 75278: if( 1 ){
! 75279: struct ExprList_item *pItem = &pList->a[pList->nExpr++];
! 75280: memset(pItem, 0, sizeof(*pItem));
! 75281: pItem->pExpr = pExpr;
! 75282: }
! 75283: return pList;
! 75284:
! 75285: no_mem:
! 75286: /* Avoid leaking memory if malloc has failed. */
! 75287: sqlite3ExprDelete(db, pExpr);
! 75288: sqlite3ExprListDelete(db, pList);
! 75289: return 0;
! 75290: }
! 75291:
! 75292: /*
! 75293: ** Set the ExprList.a[].zName element of the most recently added item
! 75294: ** on the expression list.
! 75295: **
! 75296: ** pList might be NULL following an OOM error. But pName should never be
! 75297: ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
! 75298: ** is set.
! 75299: */
! 75300: SQLITE_PRIVATE void sqlite3ExprListSetName(
! 75301: Parse *pParse, /* Parsing context */
! 75302: ExprList *pList, /* List to which to add the span. */
! 75303: Token *pName, /* Name to be added */
! 75304: int dequote /* True to cause the name to be dequoted */
! 75305: ){
! 75306: assert( pList!=0 || pParse->db->mallocFailed!=0 );
! 75307: if( pList ){
! 75308: struct ExprList_item *pItem;
! 75309: assert( pList->nExpr>0 );
! 75310: pItem = &pList->a[pList->nExpr-1];
! 75311: assert( pItem->zName==0 );
! 75312: pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
! 75313: if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
! 75314: }
! 75315: }
! 75316:
! 75317: /*
! 75318: ** Set the ExprList.a[].zSpan element of the most recently added item
! 75319: ** on the expression list.
! 75320: **
! 75321: ** pList might be NULL following an OOM error. But pSpan should never be
! 75322: ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
! 75323: ** is set.
! 75324: */
! 75325: SQLITE_PRIVATE void sqlite3ExprListSetSpan(
! 75326: Parse *pParse, /* Parsing context */
! 75327: ExprList *pList, /* List to which to add the span. */
! 75328: ExprSpan *pSpan /* The span to be added */
! 75329: ){
! 75330: sqlite3 *db = pParse->db;
! 75331: assert( pList!=0 || db->mallocFailed!=0 );
! 75332: if( pList ){
! 75333: struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
! 75334: assert( pList->nExpr>0 );
! 75335: assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
! 75336: sqlite3DbFree(db, pItem->zSpan);
! 75337: pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
! 75338: (int)(pSpan->zEnd - pSpan->zStart));
! 75339: }
! 75340: }
! 75341:
! 75342: /*
! 75343: ** If the expression list pEList contains more than iLimit elements,
! 75344: ** leave an error message in pParse.
! 75345: */
! 75346: SQLITE_PRIVATE void sqlite3ExprListCheckLength(
! 75347: Parse *pParse,
! 75348: ExprList *pEList,
! 75349: const char *zObject
! 75350: ){
! 75351: int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
! 75352: testcase( pEList && pEList->nExpr==mx );
! 75353: testcase( pEList && pEList->nExpr==mx+1 );
! 75354: if( pEList && pEList->nExpr>mx ){
! 75355: sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
! 75356: }
! 75357: }
! 75358:
! 75359: /*
! 75360: ** Delete an entire expression list.
! 75361: */
! 75362: SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
! 75363: int i;
! 75364: struct ExprList_item *pItem;
! 75365: if( pList==0 ) return;
! 75366: assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
! 75367: assert( pList->nExpr<=pList->nAlloc );
! 75368: for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
! 75369: sqlite3ExprDelete(db, pItem->pExpr);
! 75370: sqlite3DbFree(db, pItem->zName);
! 75371: sqlite3DbFree(db, pItem->zSpan);
! 75372: }
! 75373: sqlite3DbFree(db, pList->a);
! 75374: sqlite3DbFree(db, pList);
! 75375: }
! 75376:
! 75377: /*
! 75378: ** These routines are Walker callbacks. Walker.u.pi is a pointer
! 75379: ** to an integer. These routines are checking an expression to see
! 75380: ** if it is a constant. Set *Walker.u.pi to 0 if the expression is
! 75381: ** not constant.
! 75382: **
! 75383: ** These callback routines are used to implement the following:
! 75384: **
! 75385: ** sqlite3ExprIsConstant()
! 75386: ** sqlite3ExprIsConstantNotJoin()
! 75387: ** sqlite3ExprIsConstantOrFunction()
! 75388: **
! 75389: */
! 75390: static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
! 75391:
! 75392: /* If pWalker->u.i is 3 then any term of the expression that comes from
! 75393: ** the ON or USING clauses of a join disqualifies the expression
! 75394: ** from being considered constant. */
! 75395: if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
! 75396: pWalker->u.i = 0;
! 75397: return WRC_Abort;
! 75398: }
! 75399:
! 75400: switch( pExpr->op ){
! 75401: /* Consider functions to be constant if all their arguments are constant
! 75402: ** and pWalker->u.i==2 */
! 75403: case TK_FUNCTION:
! 75404: if( pWalker->u.i==2 ) return 0;
! 75405: /* Fall through */
! 75406: case TK_ID:
! 75407: case TK_COLUMN:
! 75408: case TK_AGG_FUNCTION:
! 75409: case TK_AGG_COLUMN:
! 75410: testcase( pExpr->op==TK_ID );
! 75411: testcase( pExpr->op==TK_COLUMN );
! 75412: testcase( pExpr->op==TK_AGG_FUNCTION );
! 75413: testcase( pExpr->op==TK_AGG_COLUMN );
! 75414: pWalker->u.i = 0;
! 75415: return WRC_Abort;
! 75416: default:
! 75417: testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
! 75418: testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
! 75419: return WRC_Continue;
! 75420: }
! 75421: }
! 75422: static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
! 75423: UNUSED_PARAMETER(NotUsed);
! 75424: pWalker->u.i = 0;
! 75425: return WRC_Abort;
! 75426: }
! 75427: static int exprIsConst(Expr *p, int initFlag){
! 75428: Walker w;
! 75429: w.u.i = initFlag;
! 75430: w.xExprCallback = exprNodeIsConstant;
! 75431: w.xSelectCallback = selectNodeIsConstant;
! 75432: sqlite3WalkExpr(&w, p);
! 75433: return w.u.i;
! 75434: }
! 75435:
! 75436: /*
! 75437: ** Walk an expression tree. Return 1 if the expression is constant
! 75438: ** and 0 if it involves variables or function calls.
! 75439: **
! 75440: ** For the purposes of this function, a double-quoted string (ex: "abc")
! 75441: ** is considered a variable but a single-quoted string (ex: 'abc') is
! 75442: ** a constant.
! 75443: */
! 75444: SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
! 75445: return exprIsConst(p, 1);
! 75446: }
! 75447:
! 75448: /*
! 75449: ** Walk an expression tree. Return 1 if the expression is constant
! 75450: ** that does no originate from the ON or USING clauses of a join.
! 75451: ** Return 0 if it involves variables or function calls or terms from
! 75452: ** an ON or USING clause.
! 75453: */
! 75454: SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
! 75455: return exprIsConst(p, 3);
! 75456: }
! 75457:
! 75458: /*
! 75459: ** Walk an expression tree. Return 1 if the expression is constant
! 75460: ** or a function call with constant arguments. Return and 0 if there
! 75461: ** are any variables.
! 75462: **
! 75463: ** For the purposes of this function, a double-quoted string (ex: "abc")
! 75464: ** is considered a variable but a single-quoted string (ex: 'abc') is
! 75465: ** a constant.
! 75466: */
! 75467: SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){
! 75468: return exprIsConst(p, 2);
! 75469: }
! 75470:
! 75471: /*
! 75472: ** If the expression p codes a constant integer that is small enough
! 75473: ** to fit in a 32-bit integer, return 1 and put the value of the integer
! 75474: ** in *pValue. If the expression is not an integer or if it is too big
! 75475: ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
! 75476: */
! 75477: SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
! 75478: int rc = 0;
! 75479:
! 75480: /* If an expression is an integer literal that fits in a signed 32-bit
! 75481: ** integer, then the EP_IntValue flag will have already been set */
! 75482: assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
! 75483: || sqlite3GetInt32(p->u.zToken, &rc)==0 );
! 75484:
! 75485: if( p->flags & EP_IntValue ){
! 75486: *pValue = p->u.iValue;
! 75487: return 1;
! 75488: }
! 75489: switch( p->op ){
! 75490: case TK_UPLUS: {
! 75491: rc = sqlite3ExprIsInteger(p->pLeft, pValue);
! 75492: break;
! 75493: }
! 75494: case TK_UMINUS: {
! 75495: int v;
! 75496: if( sqlite3ExprIsInteger(p->pLeft, &v) ){
! 75497: *pValue = -v;
! 75498: rc = 1;
! 75499: }
! 75500: break;
! 75501: }
! 75502: default: break;
! 75503: }
! 75504: return rc;
! 75505: }
! 75506:
! 75507: /*
! 75508: ** Return FALSE if there is no chance that the expression can be NULL.
! 75509: **
! 75510: ** If the expression might be NULL or if the expression is too complex
! 75511: ** to tell return TRUE.
! 75512: **
! 75513: ** This routine is used as an optimization, to skip OP_IsNull opcodes
! 75514: ** when we know that a value cannot be NULL. Hence, a false positive
! 75515: ** (returning TRUE when in fact the expression can never be NULL) might
! 75516: ** be a small performance hit but is otherwise harmless. On the other
! 75517: ** hand, a false negative (returning FALSE when the result could be NULL)
! 75518: ** will likely result in an incorrect answer. So when in doubt, return
! 75519: ** TRUE.
! 75520: */
! 75521: SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
! 75522: u8 op;
! 75523: while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
! 75524: op = p->op;
! 75525: if( op==TK_REGISTER ) op = p->op2;
! 75526: switch( op ){
! 75527: case TK_INTEGER:
! 75528: case TK_STRING:
! 75529: case TK_FLOAT:
! 75530: case TK_BLOB:
! 75531: return 0;
! 75532: default:
! 75533: return 1;
! 75534: }
! 75535: }
! 75536:
! 75537: /*
! 75538: ** Generate an OP_IsNull instruction that tests register iReg and jumps
! 75539: ** to location iDest if the value in iReg is NULL. The value in iReg
! 75540: ** was computed by pExpr. If we can look at pExpr at compile-time and
! 75541: ** determine that it can never generate a NULL, then the OP_IsNull operation
! 75542: ** can be omitted.
! 75543: */
! 75544: SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(
! 75545: Vdbe *v, /* The VDBE under construction */
! 75546: const Expr *pExpr, /* Only generate OP_IsNull if this expr can be NULL */
! 75547: int iReg, /* Test the value in this register for NULL */
! 75548: int iDest /* Jump here if the value is null */
! 75549: ){
! 75550: if( sqlite3ExprCanBeNull(pExpr) ){
! 75551: sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest);
! 75552: }
! 75553: }
! 75554:
! 75555: /*
! 75556: ** Return TRUE if the given expression is a constant which would be
! 75557: ** unchanged by OP_Affinity with the affinity given in the second
! 75558: ** argument.
! 75559: **
! 75560: ** This routine is used to determine if the OP_Affinity operation
! 75561: ** can be omitted. When in doubt return FALSE. A false negative
! 75562: ** is harmless. A false positive, however, can result in the wrong
! 75563: ** answer.
! 75564: */
! 75565: SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
! 75566: u8 op;
! 75567: if( aff==SQLITE_AFF_NONE ) return 1;
! 75568: while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
! 75569: op = p->op;
! 75570: if( op==TK_REGISTER ) op = p->op2;
! 75571: switch( op ){
! 75572: case TK_INTEGER: {
! 75573: return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
! 75574: }
! 75575: case TK_FLOAT: {
! 75576: return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
! 75577: }
! 75578: case TK_STRING: {
! 75579: return aff==SQLITE_AFF_TEXT;
! 75580: }
! 75581: case TK_BLOB: {
! 75582: return 1;
! 75583: }
! 75584: case TK_COLUMN: {
! 75585: assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
! 75586: return p->iColumn<0
! 75587: && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
! 75588: }
! 75589: default: {
! 75590: return 0;
! 75591: }
! 75592: }
! 75593: }
! 75594:
! 75595: /*
! 75596: ** Return TRUE if the given string is a row-id column name.
! 75597: */
! 75598: SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
! 75599: if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
! 75600: if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
! 75601: if( sqlite3StrICmp(z, "OID")==0 ) return 1;
! 75602: return 0;
! 75603: }
! 75604:
! 75605: /*
! 75606: ** Return true if we are able to the IN operator optimization on a
! 75607: ** query of the form
! 75608: **
! 75609: ** x IN (SELECT ...)
! 75610: **
! 75611: ** Where the SELECT... clause is as specified by the parameter to this
! 75612: ** routine.
! 75613: **
! 75614: ** The Select object passed in has already been preprocessed and no
! 75615: ** errors have been found.
! 75616: */
! 75617: #ifndef SQLITE_OMIT_SUBQUERY
! 75618: static int isCandidateForInOpt(Select *p){
! 75619: SrcList *pSrc;
! 75620: ExprList *pEList;
! 75621: Table *pTab;
! 75622: if( p==0 ) return 0; /* right-hand side of IN is SELECT */
! 75623: if( p->pPrior ) return 0; /* Not a compound SELECT */
! 75624: if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
! 75625: testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
! 75626: testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
! 75627: return 0; /* No DISTINCT keyword and no aggregate functions */
! 75628: }
! 75629: assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
! 75630: if( p->pLimit ) return 0; /* Has no LIMIT clause */
! 75631: assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */
! 75632: if( p->pWhere ) return 0; /* Has no WHERE clause */
! 75633: pSrc = p->pSrc;
! 75634: assert( pSrc!=0 );
! 75635: if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
! 75636: if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
! 75637: pTab = pSrc->a[0].pTab;
! 75638: if( NEVER(pTab==0) ) return 0;
! 75639: assert( pTab->pSelect==0 ); /* FROM clause is not a view */
! 75640: if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
! 75641: pEList = p->pEList;
! 75642: if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
! 75643: if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
! 75644: return 1;
! 75645: }
! 75646: #endif /* SQLITE_OMIT_SUBQUERY */
! 75647:
! 75648: /*
! 75649: ** Code an OP_Once instruction and allocate space for its flag. Return the
! 75650: ** address of the new instruction.
! 75651: */
! 75652: SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
! 75653: Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
! 75654: return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
! 75655: }
! 75656:
! 75657: /*
! 75658: ** This function is used by the implementation of the IN (...) operator.
! 75659: ** It's job is to find or create a b-tree structure that may be used
! 75660: ** either to test for membership of the (...) set or to iterate through
! 75661: ** its members, skipping duplicates.
! 75662: **
! 75663: ** The index of the cursor opened on the b-tree (database table, database index
! 75664: ** or ephermal table) is stored in pX->iTable before this function returns.
! 75665: ** The returned value of this function indicates the b-tree type, as follows:
! 75666: **
! 75667: ** IN_INDEX_ROWID - The cursor was opened on a database table.
! 75668: ** IN_INDEX_INDEX - The cursor was opened on a database index.
! 75669: ** IN_INDEX_EPH - The cursor was opened on a specially created and
! 75670: ** populated epheremal table.
! 75671: **
! 75672: ** An existing b-tree may only be used if the SELECT is of the simple
! 75673: ** form:
! 75674: **
! 75675: ** SELECT <column> FROM <table>
! 75676: **
! 75677: ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
! 75678: ** through the set members, skipping any duplicates. In this case an
! 75679: ** epheremal table must be used unless the selected <column> is guaranteed
! 75680: ** to be unique - either because it is an INTEGER PRIMARY KEY or it
! 75681: ** has a UNIQUE constraint or UNIQUE index.
! 75682: **
! 75683: ** If the prNotFound parameter is not 0, then the b-tree will be used
! 75684: ** for fast set membership tests. In this case an epheremal table must
! 75685: ** be used unless <column> is an INTEGER PRIMARY KEY or an index can
! 75686: ** be found with <column> as its left-most column.
! 75687: **
! 75688: ** When the b-tree is being used for membership tests, the calling function
! 75689: ** needs to know whether or not the structure contains an SQL NULL
! 75690: ** value in order to correctly evaluate expressions like "X IN (Y, Z)".
! 75691: ** If there is any chance that the (...) might contain a NULL value at
! 75692: ** runtime, then a register is allocated and the register number written
! 75693: ** to *prNotFound. If there is no chance that the (...) contains a
! 75694: ** NULL value, then *prNotFound is left unchanged.
! 75695: **
! 75696: ** If a register is allocated and its location stored in *prNotFound, then
! 75697: ** its initial value is NULL. If the (...) does not remain constant
! 75698: ** for the duration of the query (i.e. the SELECT within the (...)
! 75699: ** is a correlated subquery) then the value of the allocated register is
! 75700: ** reset to NULL each time the subquery is rerun. This allows the
! 75701: ** caller to use vdbe code equivalent to the following:
! 75702: **
! 75703: ** if( register==NULL ){
! 75704: ** has_null = <test if data structure contains null>
! 75705: ** register = 1
! 75706: ** }
! 75707: **
! 75708: ** in order to avoid running the <test if data structure contains null>
! 75709: ** test more often than is necessary.
! 75710: */
! 75711: #ifndef SQLITE_OMIT_SUBQUERY
! 75712: SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
! 75713: Select *p; /* SELECT to the right of IN operator */
! 75714: int eType = 0; /* Type of RHS table. IN_INDEX_* */
! 75715: int iTab = pParse->nTab++; /* Cursor of the RHS table */
! 75716: int mustBeUnique = (prNotFound==0); /* True if RHS must be unique */
! 75717: Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
! 75718:
! 75719: assert( pX->op==TK_IN );
! 75720:
! 75721: /* Check to see if an existing table or index can be used to
! 75722: ** satisfy the query. This is preferable to generating a new
! 75723: ** ephemeral table.
! 75724: */
! 75725: p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
! 75726: if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
! 75727: sqlite3 *db = pParse->db; /* Database connection */
! 75728: Table *pTab; /* Table <table>. */
! 75729: Expr *pExpr; /* Expression <column> */
! 75730: int iCol; /* Index of column <column> */
! 75731: int iDb; /* Database idx for pTab */
! 75732:
! 75733: assert( p ); /* Because of isCandidateForInOpt(p) */
! 75734: assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
! 75735: assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
! 75736: assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
! 75737: pTab = p->pSrc->a[0].pTab;
! 75738: pExpr = p->pEList->a[0].pExpr;
! 75739: iCol = pExpr->iColumn;
! 75740:
! 75741: /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
! 75742: iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 75743: sqlite3CodeVerifySchema(pParse, iDb);
! 75744: sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
! 75745:
! 75746: /* This function is only called from two places. In both cases the vdbe
! 75747: ** has already been allocated. So assume sqlite3GetVdbe() is always
! 75748: ** successful here.
! 75749: */
! 75750: assert(v);
! 75751: if( iCol<0 ){
! 75752: int iAddr;
! 75753:
! 75754: iAddr = sqlite3CodeOnce(pParse);
! 75755:
! 75756: sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
! 75757: eType = IN_INDEX_ROWID;
! 75758:
! 75759: sqlite3VdbeJumpHere(v, iAddr);
! 75760: }else{
! 75761: Index *pIdx; /* Iterator variable */
! 75762:
! 75763: /* The collation sequence used by the comparison. If an index is to
! 75764: ** be used in place of a temp-table, it must be ordered according
! 75765: ** to this collation sequence. */
! 75766: CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
! 75767:
! 75768: /* Check that the affinity that will be used to perform the
! 75769: ** comparison is the same as the affinity of the column. If
! 75770: ** it is not, it is not possible to use any index.
! 75771: */
! 75772: char aff = comparisonAffinity(pX);
! 75773: int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
! 75774:
! 75775: for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
! 75776: if( (pIdx->aiColumn[0]==iCol)
! 75777: && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
! 75778: && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
! 75779: ){
! 75780: int iAddr;
! 75781: char *pKey;
! 75782:
! 75783: pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
! 75784: iAddr = sqlite3CodeOnce(pParse);
! 75785:
! 75786: sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
! 75787: pKey,P4_KEYINFO_HANDOFF);
! 75788: VdbeComment((v, "%s", pIdx->zName));
! 75789: eType = IN_INDEX_INDEX;
! 75790:
! 75791: sqlite3VdbeJumpHere(v, iAddr);
! 75792: if( prNotFound && !pTab->aCol[iCol].notNull ){
! 75793: *prNotFound = ++pParse->nMem;
! 75794: sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
! 75795: }
! 75796: }
! 75797: }
! 75798: }
! 75799: }
! 75800:
! 75801: if( eType==0 ){
! 75802: /* Could not found an existing table or index to use as the RHS b-tree.
! 75803: ** We will have to generate an ephemeral table to do the job.
! 75804: */
! 75805: double savedNQueryLoop = pParse->nQueryLoop;
! 75806: int rMayHaveNull = 0;
! 75807: eType = IN_INDEX_EPH;
! 75808: if( prNotFound ){
! 75809: *prNotFound = rMayHaveNull = ++pParse->nMem;
! 75810: sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
! 75811: }else{
! 75812: testcase( pParse->nQueryLoop>(double)1 );
! 75813: pParse->nQueryLoop = (double)1;
! 75814: if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
! 75815: eType = IN_INDEX_ROWID;
! 75816: }
! 75817: }
! 75818: sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
! 75819: pParse->nQueryLoop = savedNQueryLoop;
! 75820: }else{
! 75821: pX->iTable = iTab;
! 75822: }
! 75823: return eType;
! 75824: }
! 75825: #endif
! 75826:
! 75827: /*
! 75828: ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
! 75829: ** or IN operators. Examples:
! 75830: **
! 75831: ** (SELECT a FROM b) -- subquery
! 75832: ** EXISTS (SELECT a FROM b) -- EXISTS subquery
! 75833: ** x IN (4,5,11) -- IN operator with list on right-hand side
! 75834: ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
! 75835: **
! 75836: ** The pExpr parameter describes the expression that contains the IN
! 75837: ** operator or subquery.
! 75838: **
! 75839: ** If parameter isRowid is non-zero, then expression pExpr is guaranteed
! 75840: ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
! 75841: ** to some integer key column of a table B-Tree. In this case, use an
! 75842: ** intkey B-Tree to store the set of IN(...) values instead of the usual
! 75843: ** (slower) variable length keys B-Tree.
! 75844: **
! 75845: ** If rMayHaveNull is non-zero, that means that the operation is an IN
! 75846: ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
! 75847: ** Furthermore, the IN is in a WHERE clause and that we really want
! 75848: ** to iterate over the RHS of the IN operator in order to quickly locate
! 75849: ** all corresponding LHS elements. All this routine does is initialize
! 75850: ** the register given by rMayHaveNull to NULL. Calling routines will take
! 75851: ** care of changing this register value to non-NULL if the RHS is NULL-free.
! 75852: **
! 75853: ** If rMayHaveNull is zero, that means that the subquery is being used
! 75854: ** for membership testing only. There is no need to initialize any
! 75855: ** registers to indicate the presense or absence of NULLs on the RHS.
! 75856: **
! 75857: ** For a SELECT or EXISTS operator, return the register that holds the
! 75858: ** result. For IN operators or if an error occurs, the return value is 0.
! 75859: */
! 75860: #ifndef SQLITE_OMIT_SUBQUERY
! 75861: SQLITE_PRIVATE int sqlite3CodeSubselect(
! 75862: Parse *pParse, /* Parsing context */
! 75863: Expr *pExpr, /* The IN, SELECT, or EXISTS operator */
! 75864: int rMayHaveNull, /* Register that records whether NULLs exist in RHS */
! 75865: int isRowid /* If true, LHS of IN operator is a rowid */
! 75866: ){
! 75867: int testAddr = -1; /* One-time test address */
! 75868: int rReg = 0; /* Register storing resulting */
! 75869: Vdbe *v = sqlite3GetVdbe(pParse);
! 75870: if( NEVER(v==0) ) return 0;
! 75871: sqlite3ExprCachePush(pParse);
! 75872:
! 75873: /* This code must be run in its entirety every time it is encountered
! 75874: ** if any of the following is true:
! 75875: **
! 75876: ** * The right-hand side is a correlated subquery
! 75877: ** * The right-hand side is an expression list containing variables
! 75878: ** * We are inside a trigger
! 75879: **
! 75880: ** If all of the above are false, then we can run this code just once
! 75881: ** save the results, and reuse the same result on subsequent invocations.
! 75882: */
! 75883: if( !ExprHasAnyProperty(pExpr, EP_VarSelect) ){
! 75884: testAddr = sqlite3CodeOnce(pParse);
! 75885: }
! 75886:
! 75887: #ifndef SQLITE_OMIT_EXPLAIN
! 75888: if( pParse->explain==2 ){
! 75889: char *zMsg = sqlite3MPrintf(
! 75890: pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
! 75891: pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
! 75892: );
! 75893: sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
! 75894: }
! 75895: #endif
! 75896:
! 75897: switch( pExpr->op ){
! 75898: case TK_IN: {
! 75899: char affinity; /* Affinity of the LHS of the IN */
! 75900: KeyInfo keyInfo; /* Keyinfo for the generated table */
! 75901: int addr; /* Address of OP_OpenEphemeral instruction */
! 75902: Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
! 75903:
! 75904: if( rMayHaveNull ){
! 75905: sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
! 75906: }
! 75907:
! 75908: affinity = sqlite3ExprAffinity(pLeft);
! 75909:
! 75910: /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
! 75911: ** expression it is handled the same way. An ephemeral table is
! 75912: ** filled with single-field index keys representing the results
! 75913: ** from the SELECT or the <exprlist>.
! 75914: **
! 75915: ** If the 'x' expression is a column value, or the SELECT...
! 75916: ** statement returns a column value, then the affinity of that
! 75917: ** column is used to build the index keys. If both 'x' and the
! 75918: ** SELECT... statement are columns, then numeric affinity is used
! 75919: ** if either column has NUMERIC or INTEGER affinity. If neither
! 75920: ** 'x' nor the SELECT... statement are columns, then numeric affinity
! 75921: ** is used.
! 75922: */
! 75923: pExpr->iTable = pParse->nTab++;
! 75924: addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
! 75925: if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
! 75926: memset(&keyInfo, 0, sizeof(keyInfo));
! 75927: keyInfo.nField = 1;
! 75928:
! 75929: if( ExprHasProperty(pExpr, EP_xIsSelect) ){
! 75930: /* Case 1: expr IN (SELECT ...)
! 75931: **
! 75932: ** Generate code to write the results of the select into the temporary
! 75933: ** table allocated and opened above.
! 75934: */
! 75935: SelectDest dest;
! 75936: ExprList *pEList;
! 75937:
! 75938: assert( !isRowid );
! 75939: sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
! 75940: dest.affinity = (u8)affinity;
! 75941: assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
! 75942: pExpr->x.pSelect->iLimit = 0;
! 75943: if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
! 75944: return 0;
! 75945: }
! 75946: pEList = pExpr->x.pSelect->pEList;
! 75947: if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){
! 75948: keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
! 75949: pEList->a[0].pExpr);
! 75950: }
! 75951: }else if( ALWAYS(pExpr->x.pList!=0) ){
! 75952: /* Case 2: expr IN (exprlist)
! 75953: **
! 75954: ** For each expression, build an index key from the evaluation and
! 75955: ** store it in the temporary table. If <expr> is a column, then use
! 75956: ** that columns affinity when building index keys. If <expr> is not
! 75957: ** a column, use numeric affinity.
! 75958: */
! 75959: int i;
! 75960: ExprList *pList = pExpr->x.pList;
! 75961: struct ExprList_item *pItem;
! 75962: int r1, r2, r3;
! 75963:
! 75964: if( !affinity ){
! 75965: affinity = SQLITE_AFF_NONE;
! 75966: }
! 75967: keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
! 75968:
! 75969: /* Loop through each expression in <exprlist>. */
! 75970: r1 = sqlite3GetTempReg(pParse);
! 75971: r2 = sqlite3GetTempReg(pParse);
! 75972: sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
! 75973: for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
! 75974: Expr *pE2 = pItem->pExpr;
! 75975: int iValToIns;
! 75976:
! 75977: /* If the expression is not constant then we will need to
! 75978: ** disable the test that was generated above that makes sure
! 75979: ** this code only executes once. Because for a non-constant
! 75980: ** expression we need to rerun this code each time.
! 75981: */
! 75982: if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
! 75983: sqlite3VdbeChangeToNoop(v, testAddr);
! 75984: testAddr = -1;
! 75985: }
! 75986:
! 75987: /* Evaluate the expression and insert it into the temp table */
! 75988: if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
! 75989: sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
! 75990: }else{
! 75991: r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
! 75992: if( isRowid ){
! 75993: sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
! 75994: sqlite3VdbeCurrentAddr(v)+2);
! 75995: sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
! 75996: }else{
! 75997: sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
! 75998: sqlite3ExprCacheAffinityChange(pParse, r3, 1);
! 75999: sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
! 76000: }
! 76001: }
! 76002: }
! 76003: sqlite3ReleaseTempReg(pParse, r1);
! 76004: sqlite3ReleaseTempReg(pParse, r2);
! 76005: }
! 76006: if( !isRowid ){
! 76007: sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
! 76008: }
! 76009: break;
! 76010: }
! 76011:
! 76012: case TK_EXISTS:
! 76013: case TK_SELECT:
! 76014: default: {
! 76015: /* If this has to be a scalar SELECT. Generate code to put the
! 76016: ** value of this select in a memory cell and record the number
! 76017: ** of the memory cell in iColumn. If this is an EXISTS, write
! 76018: ** an integer 0 (not exists) or 1 (exists) into a memory cell
! 76019: ** and record that memory cell in iColumn.
! 76020: */
! 76021: Select *pSel; /* SELECT statement to encode */
! 76022: SelectDest dest; /* How to deal with SELECt result */
! 76023:
! 76024: testcase( pExpr->op==TK_EXISTS );
! 76025: testcase( pExpr->op==TK_SELECT );
! 76026: assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
! 76027:
! 76028: assert( ExprHasProperty(pExpr, EP_xIsSelect) );
! 76029: pSel = pExpr->x.pSelect;
! 76030: sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
! 76031: if( pExpr->op==TK_SELECT ){
! 76032: dest.eDest = SRT_Mem;
! 76033: sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
! 76034: VdbeComment((v, "Init subquery result"));
! 76035: }else{
! 76036: dest.eDest = SRT_Exists;
! 76037: sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
! 76038: VdbeComment((v, "Init EXISTS result"));
! 76039: }
! 76040: sqlite3ExprDelete(pParse->db, pSel->pLimit);
! 76041: pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
! 76042: &sqlite3IntTokens[1]);
! 76043: pSel->iLimit = 0;
! 76044: if( sqlite3Select(pParse, pSel, &dest) ){
! 76045: return 0;
! 76046: }
! 76047: rReg = dest.iParm;
! 76048: ExprSetIrreducible(pExpr);
! 76049: break;
! 76050: }
! 76051: }
! 76052:
! 76053: if( testAddr>=0 ){
! 76054: sqlite3VdbeJumpHere(v, testAddr);
! 76055: }
! 76056: sqlite3ExprCachePop(pParse, 1);
! 76057:
! 76058: return rReg;
! 76059: }
! 76060: #endif /* SQLITE_OMIT_SUBQUERY */
! 76061:
! 76062: #ifndef SQLITE_OMIT_SUBQUERY
! 76063: /*
! 76064: ** Generate code for an IN expression.
! 76065: **
! 76066: ** x IN (SELECT ...)
! 76067: ** x IN (value, value, ...)
! 76068: **
! 76069: ** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS)
! 76070: ** is an array of zero or more values. The expression is true if the LHS is
! 76071: ** contained within the RHS. The value of the expression is unknown (NULL)
! 76072: ** if the LHS is NULL or if the LHS is not contained within the RHS and the
! 76073: ** RHS contains one or more NULL values.
! 76074: **
! 76075: ** This routine generates code will jump to destIfFalse if the LHS is not
! 76076: ** contained within the RHS. If due to NULLs we cannot determine if the LHS
! 76077: ** is contained in the RHS then jump to destIfNull. If the LHS is contained
! 76078: ** within the RHS then fall through.
! 76079: */
! 76080: static void sqlite3ExprCodeIN(
! 76081: Parse *pParse, /* Parsing and code generating context */
! 76082: Expr *pExpr, /* The IN expression */
! 76083: int destIfFalse, /* Jump here if LHS is not contained in the RHS */
! 76084: int destIfNull /* Jump here if the results are unknown due to NULLs */
! 76085: ){
! 76086: int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
! 76087: char affinity; /* Comparison affinity to use */
! 76088: int eType; /* Type of the RHS */
! 76089: int r1; /* Temporary use register */
! 76090: Vdbe *v; /* Statement under construction */
! 76091:
! 76092: /* Compute the RHS. After this step, the table with cursor
! 76093: ** pExpr->iTable will contains the values that make up the RHS.
! 76094: */
! 76095: v = pParse->pVdbe;
! 76096: assert( v!=0 ); /* OOM detected prior to this routine */
! 76097: VdbeNoopComment((v, "begin IN expr"));
! 76098: eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
! 76099:
! 76100: /* Figure out the affinity to use to create a key from the results
! 76101: ** of the expression. affinityStr stores a static string suitable for
! 76102: ** P4 of OP_MakeRecord.
! 76103: */
! 76104: affinity = comparisonAffinity(pExpr);
! 76105:
! 76106: /* Code the LHS, the <expr> from "<expr> IN (...)".
! 76107: */
! 76108: sqlite3ExprCachePush(pParse);
! 76109: r1 = sqlite3GetTempReg(pParse);
! 76110: sqlite3ExprCode(pParse, pExpr->pLeft, r1);
! 76111:
! 76112: /* If the LHS is NULL, then the result is either false or NULL depending
! 76113: ** on whether the RHS is empty or not, respectively.
! 76114: */
! 76115: if( destIfNull==destIfFalse ){
! 76116: /* Shortcut for the common case where the false and NULL outcomes are
! 76117: ** the same. */
! 76118: sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull);
! 76119: }else{
! 76120: int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
! 76121: sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
! 76122: sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
! 76123: sqlite3VdbeJumpHere(v, addr1);
! 76124: }
! 76125:
! 76126: if( eType==IN_INDEX_ROWID ){
! 76127: /* In this case, the RHS is the ROWID of table b-tree
! 76128: */
! 76129: sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse);
! 76130: sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
! 76131: }else{
! 76132: /* In this case, the RHS is an index b-tree.
! 76133: */
! 76134: sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
! 76135:
! 76136: /* If the set membership test fails, then the result of the
! 76137: ** "x IN (...)" expression must be either 0 or NULL. If the set
! 76138: ** contains no NULL values, then the result is 0. If the set
! 76139: ** contains one or more NULL values, then the result of the
! 76140: ** expression is also NULL.
! 76141: */
! 76142: if( rRhsHasNull==0 || destIfFalse==destIfNull ){
! 76143: /* This branch runs if it is known at compile time that the RHS
! 76144: ** cannot contain NULL values. This happens as the result
! 76145: ** of a "NOT NULL" constraint in the database schema.
! 76146: **
! 76147: ** Also run this branch if NULL is equivalent to FALSE
! 76148: ** for this particular IN operator.
! 76149: */
! 76150: sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
! 76151:
! 76152: }else{
! 76153: /* In this branch, the RHS of the IN might contain a NULL and
! 76154: ** the presence of a NULL on the RHS makes a difference in the
! 76155: ** outcome.
! 76156: */
! 76157: int j1, j2, j3;
! 76158:
! 76159: /* First check to see if the LHS is contained in the RHS. If so,
! 76160: ** then the presence of NULLs in the RHS does not matter, so jump
! 76161: ** over all of the code that follows.
! 76162: */
! 76163: j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
! 76164:
! 76165: /* Here we begin generating code that runs if the LHS is not
! 76166: ** contained within the RHS. Generate additional code that
! 76167: ** tests the RHS for NULLs. If the RHS contains a NULL then
! 76168: ** jump to destIfNull. If there are no NULLs in the RHS then
! 76169: ** jump to destIfFalse.
! 76170: */
! 76171: j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull);
! 76172: j3 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
! 76173: sqlite3VdbeAddOp2(v, OP_Integer, -1, rRhsHasNull);
! 76174: sqlite3VdbeJumpHere(v, j3);
! 76175: sqlite3VdbeAddOp2(v, OP_AddImm, rRhsHasNull, 1);
! 76176: sqlite3VdbeJumpHere(v, j2);
! 76177:
! 76178: /* Jump to the appropriate target depending on whether or not
! 76179: ** the RHS contains a NULL
! 76180: */
! 76181: sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull);
! 76182: sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
! 76183:
! 76184: /* The OP_Found at the top of this branch jumps here when true,
! 76185: ** causing the overall IN expression evaluation to fall through.
! 76186: */
! 76187: sqlite3VdbeJumpHere(v, j1);
! 76188: }
! 76189: }
! 76190: sqlite3ReleaseTempReg(pParse, r1);
! 76191: sqlite3ExprCachePop(pParse, 1);
! 76192: VdbeComment((v, "end IN expr"));
! 76193: }
! 76194: #endif /* SQLITE_OMIT_SUBQUERY */
! 76195:
! 76196: /*
! 76197: ** Duplicate an 8-byte value
! 76198: */
! 76199: static char *dup8bytes(Vdbe *v, const char *in){
! 76200: char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
! 76201: if( out ){
! 76202: memcpy(out, in, 8);
! 76203: }
! 76204: return out;
! 76205: }
! 76206:
! 76207: #ifndef SQLITE_OMIT_FLOATING_POINT
! 76208: /*
! 76209: ** Generate an instruction that will put the floating point
! 76210: ** value described by z[0..n-1] into register iMem.
! 76211: **
! 76212: ** The z[] string will probably not be zero-terminated. But the
! 76213: ** z[n] character is guaranteed to be something that does not look
! 76214: ** like the continuation of the number.
! 76215: */
! 76216: static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
! 76217: if( ALWAYS(z!=0) ){
! 76218: double value;
! 76219: char *zV;
! 76220: sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
! 76221: assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
! 76222: if( negateFlag ) value = -value;
! 76223: zV = dup8bytes(v, (char*)&value);
! 76224: sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
! 76225: }
! 76226: }
! 76227: #endif
! 76228:
! 76229:
! 76230: /*
! 76231: ** Generate an instruction that will put the integer describe by
! 76232: ** text z[0..n-1] into register iMem.
! 76233: **
! 76234: ** Expr.u.zToken is always UTF8 and zero-terminated.
! 76235: */
! 76236: static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
! 76237: Vdbe *v = pParse->pVdbe;
! 76238: if( pExpr->flags & EP_IntValue ){
! 76239: int i = pExpr->u.iValue;
! 76240: assert( i>=0 );
! 76241: if( negFlag ) i = -i;
! 76242: sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
! 76243: }else{
! 76244: int c;
! 76245: i64 value;
! 76246: const char *z = pExpr->u.zToken;
! 76247: assert( z!=0 );
! 76248: c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
! 76249: if( c==0 || (c==2 && negFlag) ){
! 76250: char *zV;
! 76251: if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
! 76252: zV = dup8bytes(v, (char*)&value);
! 76253: sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
! 76254: }else{
! 76255: #ifdef SQLITE_OMIT_FLOATING_POINT
! 76256: sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
! 76257: #else
! 76258: codeReal(v, z, negFlag, iMem);
! 76259: #endif
! 76260: }
! 76261: }
! 76262: }
! 76263:
! 76264: /*
! 76265: ** Clear a cache entry.
! 76266: */
! 76267: static void cacheEntryClear(Parse *pParse, struct yColCache *p){
! 76268: if( p->tempReg ){
! 76269: if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
! 76270: pParse->aTempReg[pParse->nTempReg++] = p->iReg;
! 76271: }
! 76272: p->tempReg = 0;
! 76273: }
! 76274: }
! 76275:
! 76276:
! 76277: /*
! 76278: ** Record in the column cache that a particular column from a
! 76279: ** particular table is stored in a particular register.
! 76280: */
! 76281: SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
! 76282: int i;
! 76283: int minLru;
! 76284: int idxLru;
! 76285: struct yColCache *p;
! 76286:
! 76287: assert( iReg>0 ); /* Register numbers are always positive */
! 76288: assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */
! 76289:
! 76290: /* The SQLITE_ColumnCache flag disables the column cache. This is used
! 76291: ** for testing only - to verify that SQLite always gets the same answer
! 76292: ** with and without the column cache.
! 76293: */
! 76294: if( pParse->db->flags & SQLITE_ColumnCache ) return;
! 76295:
! 76296: /* First replace any existing entry.
! 76297: **
! 76298: ** Actually, the way the column cache is currently used, we are guaranteed
! 76299: ** that the object will never already be in cache. Verify this guarantee.
! 76300: */
! 76301: #ifndef NDEBUG
! 76302: for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
! 76303: #if 0 /* This code wold remove the entry from the cache if it existed */
! 76304: if( p->iReg && p->iTable==iTab && p->iColumn==iCol ){
! 76305: cacheEntryClear(pParse, p);
! 76306: p->iLevel = pParse->iCacheLevel;
! 76307: p->iReg = iReg;
! 76308: p->lru = pParse->iCacheCnt++;
! 76309: return;
! 76310: }
! 76311: #endif
! 76312: assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
! 76313: }
! 76314: #endif
! 76315:
! 76316: /* Find an empty slot and replace it */
! 76317: for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
! 76318: if( p->iReg==0 ){
! 76319: p->iLevel = pParse->iCacheLevel;
! 76320: p->iTable = iTab;
! 76321: p->iColumn = iCol;
! 76322: p->iReg = iReg;
! 76323: p->tempReg = 0;
! 76324: p->lru = pParse->iCacheCnt++;
! 76325: return;
! 76326: }
! 76327: }
! 76328:
! 76329: /* Replace the last recently used */
! 76330: minLru = 0x7fffffff;
! 76331: idxLru = -1;
! 76332: for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
! 76333: if( p->lru<minLru ){
! 76334: idxLru = i;
! 76335: minLru = p->lru;
! 76336: }
! 76337: }
! 76338: if( ALWAYS(idxLru>=0) ){
! 76339: p = &pParse->aColCache[idxLru];
! 76340: p->iLevel = pParse->iCacheLevel;
! 76341: p->iTable = iTab;
! 76342: p->iColumn = iCol;
! 76343: p->iReg = iReg;
! 76344: p->tempReg = 0;
! 76345: p->lru = pParse->iCacheCnt++;
! 76346: return;
! 76347: }
! 76348: }
! 76349:
! 76350: /*
! 76351: ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
! 76352: ** Purge the range of registers from the column cache.
! 76353: */
! 76354: SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
! 76355: int i;
! 76356: int iLast = iReg + nReg - 1;
! 76357: struct yColCache *p;
! 76358: for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
! 76359: int r = p->iReg;
! 76360: if( r>=iReg && r<=iLast ){
! 76361: cacheEntryClear(pParse, p);
! 76362: p->iReg = 0;
! 76363: }
! 76364: }
! 76365: }
! 76366:
! 76367: /*
! 76368: ** Remember the current column cache context. Any new entries added
! 76369: ** added to the column cache after this call are removed when the
! 76370: ** corresponding pop occurs.
! 76371: */
! 76372: SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
! 76373: pParse->iCacheLevel++;
! 76374: }
! 76375:
! 76376: /*
! 76377: ** Remove from the column cache any entries that were added since the
! 76378: ** the previous N Push operations. In other words, restore the cache
! 76379: ** to the state it was in N Pushes ago.
! 76380: */
! 76381: SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){
! 76382: int i;
! 76383: struct yColCache *p;
! 76384: assert( N>0 );
! 76385: assert( pParse->iCacheLevel>=N );
! 76386: pParse->iCacheLevel -= N;
! 76387: for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
! 76388: if( p->iReg && p->iLevel>pParse->iCacheLevel ){
! 76389: cacheEntryClear(pParse, p);
! 76390: p->iReg = 0;
! 76391: }
! 76392: }
! 76393: }
! 76394:
! 76395: /*
! 76396: ** When a cached column is reused, make sure that its register is
! 76397: ** no longer available as a temp register. ticket #3879: that same
! 76398: ** register might be in the cache in multiple places, so be sure to
! 76399: ** get them all.
! 76400: */
! 76401: static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
! 76402: int i;
! 76403: struct yColCache *p;
! 76404: for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
! 76405: if( p->iReg==iReg ){
! 76406: p->tempReg = 0;
! 76407: }
! 76408: }
! 76409: }
! 76410:
! 76411: /*
! 76412: ** Generate code to extract the value of the iCol-th column of a table.
! 76413: */
! 76414: SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
! 76415: Vdbe *v, /* The VDBE under construction */
! 76416: Table *pTab, /* The table containing the value */
! 76417: int iTabCur, /* The cursor for this table */
! 76418: int iCol, /* Index of the column to extract */
! 76419: int regOut /* Extract the valud into this register */
! 76420: ){
! 76421: if( iCol<0 || iCol==pTab->iPKey ){
! 76422: sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
! 76423: }else{
! 76424: int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
! 76425: sqlite3VdbeAddOp3(v, op, iTabCur, iCol, regOut);
! 76426: }
! 76427: if( iCol>=0 ){
! 76428: sqlite3ColumnDefault(v, pTab, iCol, regOut);
! 76429: }
! 76430: }
! 76431:
! 76432: /*
! 76433: ** Generate code that will extract the iColumn-th column from
! 76434: ** table pTab and store the column value in a register. An effort
! 76435: ** is made to store the column value in register iReg, but this is
! 76436: ** not guaranteed. The location of the column value is returned.
! 76437: **
! 76438: ** There must be an open cursor to pTab in iTable when this routine
! 76439: ** is called. If iColumn<0 then code is generated that extracts the rowid.
! 76440: */
! 76441: SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
! 76442: Parse *pParse, /* Parsing and code generating context */
! 76443: Table *pTab, /* Description of the table we are reading from */
! 76444: int iColumn, /* Index of the table column */
! 76445: int iTable, /* The cursor pointing to the table */
! 76446: int iReg /* Store results here */
! 76447: ){
! 76448: Vdbe *v = pParse->pVdbe;
! 76449: int i;
! 76450: struct yColCache *p;
! 76451:
! 76452: for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
! 76453: if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
! 76454: p->lru = pParse->iCacheCnt++;
! 76455: sqlite3ExprCachePinRegister(pParse, p->iReg);
! 76456: return p->iReg;
! 76457: }
! 76458: }
! 76459: assert( v!=0 );
! 76460: sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
! 76461: sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
! 76462: return iReg;
! 76463: }
! 76464:
! 76465: /*
! 76466: ** Clear all column cache entries.
! 76467: */
! 76468: SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
! 76469: int i;
! 76470: struct yColCache *p;
! 76471:
! 76472: for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
! 76473: if( p->iReg ){
! 76474: cacheEntryClear(pParse, p);
! 76475: p->iReg = 0;
! 76476: }
! 76477: }
! 76478: }
! 76479:
! 76480: /*
! 76481: ** Record the fact that an affinity change has occurred on iCount
! 76482: ** registers starting with iStart.
! 76483: */
! 76484: SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
! 76485: sqlite3ExprCacheRemove(pParse, iStart, iCount);
! 76486: }
! 76487:
! 76488: /*
! 76489: ** Generate code to move content from registers iFrom...iFrom+nReg-1
! 76490: ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
! 76491: */
! 76492: SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
! 76493: int i;
! 76494: struct yColCache *p;
! 76495: if( NEVER(iFrom==iTo) ) return;
! 76496: sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
! 76497: for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
! 76498: int x = p->iReg;
! 76499: if( x>=iFrom && x<iFrom+nReg ){
! 76500: p->iReg += iTo-iFrom;
! 76501: }
! 76502: }
! 76503: }
! 76504:
! 76505: /*
! 76506: ** Generate code to copy content from registers iFrom...iFrom+nReg-1
! 76507: ** over to iTo..iTo+nReg-1.
! 76508: */
! 76509: SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
! 76510: int i;
! 76511: if( NEVER(iFrom==iTo) ) return;
! 76512: for(i=0; i<nReg; i++){
! 76513: sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
! 76514: }
! 76515: }
! 76516:
! 76517: #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
! 76518: /*
! 76519: ** Return true if any register in the range iFrom..iTo (inclusive)
! 76520: ** is used as part of the column cache.
! 76521: **
! 76522: ** This routine is used within assert() and testcase() macros only
! 76523: ** and does not appear in a normal build.
! 76524: */
! 76525: static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
! 76526: int i;
! 76527: struct yColCache *p;
! 76528: for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
! 76529: int r = p->iReg;
! 76530: if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/
! 76531: }
! 76532: return 0;
! 76533: }
! 76534: #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
! 76535:
! 76536: /*
! 76537: ** Generate code into the current Vdbe to evaluate the given
! 76538: ** expression. Attempt to store the results in register "target".
! 76539: ** Return the register where results are stored.
! 76540: **
! 76541: ** With this routine, there is no guarantee that results will
! 76542: ** be stored in target. The result might be stored in some other
! 76543: ** register if it is convenient to do so. The calling function
! 76544: ** must check the return code and move the results to the desired
! 76545: ** register.
! 76546: */
! 76547: SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
! 76548: Vdbe *v = pParse->pVdbe; /* The VM under construction */
! 76549: int op; /* The opcode being coded */
! 76550: int inReg = target; /* Results stored in register inReg */
! 76551: int regFree1 = 0; /* If non-zero free this temporary register */
! 76552: int regFree2 = 0; /* If non-zero free this temporary register */
! 76553: int r1, r2, r3, r4; /* Various register numbers */
! 76554: sqlite3 *db = pParse->db; /* The database connection */
! 76555:
! 76556: assert( target>0 && target<=pParse->nMem );
! 76557: if( v==0 ){
! 76558: assert( pParse->db->mallocFailed );
! 76559: return 0;
! 76560: }
! 76561:
! 76562: if( pExpr==0 ){
! 76563: op = TK_NULL;
! 76564: }else{
! 76565: op = pExpr->op;
! 76566: }
! 76567: switch( op ){
! 76568: case TK_AGG_COLUMN: {
! 76569: AggInfo *pAggInfo = pExpr->pAggInfo;
! 76570: struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
! 76571: if( !pAggInfo->directMode ){
! 76572: assert( pCol->iMem>0 );
! 76573: inReg = pCol->iMem;
! 76574: break;
! 76575: }else if( pAggInfo->useSortingIdx ){
! 76576: sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
! 76577: pCol->iSorterColumn, target);
! 76578: break;
! 76579: }
! 76580: /* Otherwise, fall thru into the TK_COLUMN case */
! 76581: }
! 76582: case TK_COLUMN: {
! 76583: if( pExpr->iTable<0 ){
! 76584: /* This only happens when coding check constraints */
! 76585: assert( pParse->ckBase>0 );
! 76586: inReg = pExpr->iColumn + pParse->ckBase;
! 76587: }else{
! 76588: inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
! 76589: pExpr->iColumn, pExpr->iTable, target);
! 76590: }
! 76591: break;
! 76592: }
! 76593: case TK_INTEGER: {
! 76594: codeInteger(pParse, pExpr, 0, target);
! 76595: break;
! 76596: }
! 76597: #ifndef SQLITE_OMIT_FLOATING_POINT
! 76598: case TK_FLOAT: {
! 76599: assert( !ExprHasProperty(pExpr, EP_IntValue) );
! 76600: codeReal(v, pExpr->u.zToken, 0, target);
! 76601: break;
! 76602: }
! 76603: #endif
! 76604: case TK_STRING: {
! 76605: assert( !ExprHasProperty(pExpr, EP_IntValue) );
! 76606: sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
! 76607: break;
! 76608: }
! 76609: case TK_NULL: {
! 76610: sqlite3VdbeAddOp2(v, OP_Null, 0, target);
! 76611: break;
! 76612: }
! 76613: #ifndef SQLITE_OMIT_BLOB_LITERAL
! 76614: case TK_BLOB: {
! 76615: int n;
! 76616: const char *z;
! 76617: char *zBlob;
! 76618: assert( !ExprHasProperty(pExpr, EP_IntValue) );
! 76619: assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
! 76620: assert( pExpr->u.zToken[1]=='\'' );
! 76621: z = &pExpr->u.zToken[2];
! 76622: n = sqlite3Strlen30(z) - 1;
! 76623: assert( z[n]=='\'' );
! 76624: zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
! 76625: sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
! 76626: break;
! 76627: }
! 76628: #endif
! 76629: case TK_VARIABLE: {
! 76630: assert( !ExprHasProperty(pExpr, EP_IntValue) );
! 76631: assert( pExpr->u.zToken!=0 );
! 76632: assert( pExpr->u.zToken[0]!=0 );
! 76633: sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
! 76634: if( pExpr->u.zToken[1]!=0 ){
! 76635: assert( pExpr->u.zToken[0]=='?'
! 76636: || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
! 76637: sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
! 76638: }
! 76639: break;
! 76640: }
! 76641: case TK_REGISTER: {
! 76642: inReg = pExpr->iTable;
! 76643: break;
! 76644: }
! 76645: case TK_AS: {
! 76646: inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
! 76647: break;
! 76648: }
! 76649: #ifndef SQLITE_OMIT_CAST
! 76650: case TK_CAST: {
! 76651: /* Expressions of the form: CAST(pLeft AS token) */
! 76652: int aff, to_op;
! 76653: inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
! 76654: assert( !ExprHasProperty(pExpr, EP_IntValue) );
! 76655: aff = sqlite3AffinityType(pExpr->u.zToken);
! 76656: to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
! 76657: assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
! 76658: assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
! 76659: assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
! 76660: assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
! 76661: assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
! 76662: testcase( to_op==OP_ToText );
! 76663: testcase( to_op==OP_ToBlob );
! 76664: testcase( to_op==OP_ToNumeric );
! 76665: testcase( to_op==OP_ToInt );
! 76666: testcase( to_op==OP_ToReal );
! 76667: if( inReg!=target ){
! 76668: sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
! 76669: inReg = target;
! 76670: }
! 76671: sqlite3VdbeAddOp1(v, to_op, inReg);
! 76672: testcase( usedAsColumnCache(pParse, inReg, inReg) );
! 76673: sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
! 76674: break;
! 76675: }
! 76676: #endif /* SQLITE_OMIT_CAST */
! 76677: case TK_LT:
! 76678: case TK_LE:
! 76679: case TK_GT:
! 76680: case TK_GE:
! 76681: case TK_NE:
! 76682: case TK_EQ: {
! 76683: assert( TK_LT==OP_Lt );
! 76684: assert( TK_LE==OP_Le );
! 76685: assert( TK_GT==OP_Gt );
! 76686: assert( TK_GE==OP_Ge );
! 76687: assert( TK_EQ==OP_Eq );
! 76688: assert( TK_NE==OP_Ne );
! 76689: testcase( op==TK_LT );
! 76690: testcase( op==TK_LE );
! 76691: testcase( op==TK_GT );
! 76692: testcase( op==TK_GE );
! 76693: testcase( op==TK_EQ );
! 76694: testcase( op==TK_NE );
! 76695: r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
! 76696: r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
! 76697: codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
! 76698: r1, r2, inReg, SQLITE_STOREP2);
! 76699: testcase( regFree1==0 );
! 76700: testcase( regFree2==0 );
! 76701: break;
! 76702: }
! 76703: case TK_IS:
! 76704: case TK_ISNOT: {
! 76705: testcase( op==TK_IS );
! 76706: testcase( op==TK_ISNOT );
! 76707: r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
! 76708: r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
! 76709: op = (op==TK_IS) ? TK_EQ : TK_NE;
! 76710: codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
! 76711: r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
! 76712: testcase( regFree1==0 );
! 76713: testcase( regFree2==0 );
! 76714: break;
! 76715: }
! 76716: case TK_AND:
! 76717: case TK_OR:
! 76718: case TK_PLUS:
! 76719: case TK_STAR:
! 76720: case TK_MINUS:
! 76721: case TK_REM:
! 76722: case TK_BITAND:
! 76723: case TK_BITOR:
! 76724: case TK_SLASH:
! 76725: case TK_LSHIFT:
! 76726: case TK_RSHIFT:
! 76727: case TK_CONCAT: {
! 76728: assert( TK_AND==OP_And );
! 76729: assert( TK_OR==OP_Or );
! 76730: assert( TK_PLUS==OP_Add );
! 76731: assert( TK_MINUS==OP_Subtract );
! 76732: assert( TK_REM==OP_Remainder );
! 76733: assert( TK_BITAND==OP_BitAnd );
! 76734: assert( TK_BITOR==OP_BitOr );
! 76735: assert( TK_SLASH==OP_Divide );
! 76736: assert( TK_LSHIFT==OP_ShiftLeft );
! 76737: assert( TK_RSHIFT==OP_ShiftRight );
! 76738: assert( TK_CONCAT==OP_Concat );
! 76739: testcase( op==TK_AND );
! 76740: testcase( op==TK_OR );
! 76741: testcase( op==TK_PLUS );
! 76742: testcase( op==TK_MINUS );
! 76743: testcase( op==TK_REM );
! 76744: testcase( op==TK_BITAND );
! 76745: testcase( op==TK_BITOR );
! 76746: testcase( op==TK_SLASH );
! 76747: testcase( op==TK_LSHIFT );
! 76748: testcase( op==TK_RSHIFT );
! 76749: testcase( op==TK_CONCAT );
! 76750: r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
! 76751: r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
! 76752: sqlite3VdbeAddOp3(v, op, r2, r1, target);
! 76753: testcase( regFree1==0 );
! 76754: testcase( regFree2==0 );
! 76755: break;
! 76756: }
! 76757: case TK_UMINUS: {
! 76758: Expr *pLeft = pExpr->pLeft;
! 76759: assert( pLeft );
! 76760: if( pLeft->op==TK_INTEGER ){
! 76761: codeInteger(pParse, pLeft, 1, target);
! 76762: #ifndef SQLITE_OMIT_FLOATING_POINT
! 76763: }else if( pLeft->op==TK_FLOAT ){
! 76764: assert( !ExprHasProperty(pExpr, EP_IntValue) );
! 76765: codeReal(v, pLeft->u.zToken, 1, target);
! 76766: #endif
! 76767: }else{
! 76768: regFree1 = r1 = sqlite3GetTempReg(pParse);
! 76769: sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
! 76770: r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2);
! 76771: sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
! 76772: testcase( regFree2==0 );
! 76773: }
! 76774: inReg = target;
! 76775: break;
! 76776: }
! 76777: case TK_BITNOT:
! 76778: case TK_NOT: {
! 76779: assert( TK_BITNOT==OP_BitNot );
! 76780: assert( TK_NOT==OP_Not );
! 76781: testcase( op==TK_BITNOT );
! 76782: testcase( op==TK_NOT );
! 76783: r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
! 76784: testcase( regFree1==0 );
! 76785: inReg = target;
! 76786: sqlite3VdbeAddOp2(v, op, r1, inReg);
! 76787: break;
! 76788: }
! 76789: case TK_ISNULL:
! 76790: case TK_NOTNULL: {
! 76791: int addr;
! 76792: assert( TK_ISNULL==OP_IsNull );
! 76793: assert( TK_NOTNULL==OP_NotNull );
! 76794: testcase( op==TK_ISNULL );
! 76795: testcase( op==TK_NOTNULL );
! 76796: sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
! 76797: r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
! 76798: testcase( regFree1==0 );
! 76799: addr = sqlite3VdbeAddOp1(v, op, r1);
! 76800: sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
! 76801: sqlite3VdbeJumpHere(v, addr);
! 76802: break;
! 76803: }
! 76804: case TK_AGG_FUNCTION: {
! 76805: AggInfo *pInfo = pExpr->pAggInfo;
! 76806: if( pInfo==0 ){
! 76807: assert( !ExprHasProperty(pExpr, EP_IntValue) );
! 76808: sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
! 76809: }else{
! 76810: inReg = pInfo->aFunc[pExpr->iAgg].iMem;
! 76811: }
! 76812: break;
! 76813: }
! 76814: case TK_CONST_FUNC:
! 76815: case TK_FUNCTION: {
! 76816: ExprList *pFarg; /* List of function arguments */
! 76817: int nFarg; /* Number of function arguments */
! 76818: FuncDef *pDef; /* The function definition object */
! 76819: int nId; /* Length of the function name in bytes */
! 76820: const char *zId; /* The function name */
! 76821: int constMask = 0; /* Mask of function arguments that are constant */
! 76822: int i; /* Loop counter */
! 76823: u8 enc = ENC(db); /* The text encoding used by this database */
! 76824: CollSeq *pColl = 0; /* A collating sequence */
! 76825:
! 76826: assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
! 76827: testcase( op==TK_CONST_FUNC );
! 76828: testcase( op==TK_FUNCTION );
! 76829: if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
! 76830: pFarg = 0;
! 76831: }else{
! 76832: pFarg = pExpr->x.pList;
! 76833: }
! 76834: nFarg = pFarg ? pFarg->nExpr : 0;
! 76835: assert( !ExprHasProperty(pExpr, EP_IntValue) );
! 76836: zId = pExpr->u.zToken;
! 76837: nId = sqlite3Strlen30(zId);
! 76838: pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
! 76839: if( pDef==0 ){
! 76840: sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
! 76841: break;
! 76842: }
! 76843:
! 76844: /* Attempt a direct implementation of the built-in COALESCE() and
! 76845: ** IFNULL() functions. This avoids unnecessary evalation of
! 76846: ** arguments past the first non-NULL argument.
! 76847: */
! 76848: if( pDef->flags & SQLITE_FUNC_COALESCE ){
! 76849: int endCoalesce = sqlite3VdbeMakeLabel(v);
! 76850: assert( nFarg>=2 );
! 76851: sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
! 76852: for(i=1; i<nFarg; i++){
! 76853: sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
! 76854: sqlite3ExprCacheRemove(pParse, target, 1);
! 76855: sqlite3ExprCachePush(pParse);
! 76856: sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
! 76857: sqlite3ExprCachePop(pParse, 1);
! 76858: }
! 76859: sqlite3VdbeResolveLabel(v, endCoalesce);
! 76860: break;
! 76861: }
! 76862:
! 76863:
! 76864: if( pFarg ){
! 76865: r1 = sqlite3GetTempRange(pParse, nFarg);
! 76866: sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */
! 76867: sqlite3ExprCodeExprList(pParse, pFarg, r1, 1);
! 76868: sqlite3ExprCachePop(pParse, 1); /* Ticket 2ea2425d34be */
! 76869: }else{
! 76870: r1 = 0;
! 76871: }
! 76872: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 76873: /* Possibly overload the function if the first argument is
! 76874: ** a virtual table column.
! 76875: **
! 76876: ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
! 76877: ** second argument, not the first, as the argument to test to
! 76878: ** see if it is a column in a virtual table. This is done because
! 76879: ** the left operand of infix functions (the operand we want to
! 76880: ** control overloading) ends up as the second argument to the
! 76881: ** function. The expression "A glob B" is equivalent to
! 76882: ** "glob(B,A). We want to use the A in "A glob B" to test
! 76883: ** for function overloading. But we use the B term in "glob(B,A)".
! 76884: */
! 76885: if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
! 76886: pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
! 76887: }else if( nFarg>0 ){
! 76888: pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
! 76889: }
! 76890: #endif
! 76891: for(i=0; i<nFarg; i++){
! 76892: if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
! 76893: constMask |= (1<<i);
! 76894: }
! 76895: if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
! 76896: pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
! 76897: }
! 76898: }
! 76899: if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){
! 76900: if( !pColl ) pColl = db->pDfltColl;
! 76901: sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
! 76902: }
! 76903: sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
! 76904: (char*)pDef, P4_FUNCDEF);
! 76905: sqlite3VdbeChangeP5(v, (u8)nFarg);
! 76906: if( nFarg ){
! 76907: sqlite3ReleaseTempRange(pParse, r1, nFarg);
! 76908: }
! 76909: break;
! 76910: }
! 76911: #ifndef SQLITE_OMIT_SUBQUERY
! 76912: case TK_EXISTS:
! 76913: case TK_SELECT: {
! 76914: testcase( op==TK_EXISTS );
! 76915: testcase( op==TK_SELECT );
! 76916: inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
! 76917: break;
! 76918: }
! 76919: case TK_IN: {
! 76920: int destIfFalse = sqlite3VdbeMakeLabel(v);
! 76921: int destIfNull = sqlite3VdbeMakeLabel(v);
! 76922: sqlite3VdbeAddOp2(v, OP_Null, 0, target);
! 76923: sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
! 76924: sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
! 76925: sqlite3VdbeResolveLabel(v, destIfFalse);
! 76926: sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
! 76927: sqlite3VdbeResolveLabel(v, destIfNull);
! 76928: break;
! 76929: }
! 76930: #endif /* SQLITE_OMIT_SUBQUERY */
! 76931:
! 76932:
! 76933: /*
! 76934: ** x BETWEEN y AND z
! 76935: **
! 76936: ** This is equivalent to
! 76937: **
! 76938: ** x>=y AND x<=z
! 76939: **
! 76940: ** X is stored in pExpr->pLeft.
! 76941: ** Y is stored in pExpr->pList->a[0].pExpr.
! 76942: ** Z is stored in pExpr->pList->a[1].pExpr.
! 76943: */
! 76944: case TK_BETWEEN: {
! 76945: Expr *pLeft = pExpr->pLeft;
! 76946: struct ExprList_item *pLItem = pExpr->x.pList->a;
! 76947: Expr *pRight = pLItem->pExpr;
! 76948:
! 76949: r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1);
! 76950: r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2);
! 76951: testcase( regFree1==0 );
! 76952: testcase( regFree2==0 );
! 76953: r3 = sqlite3GetTempReg(pParse);
! 76954: r4 = sqlite3GetTempReg(pParse);
! 76955: codeCompare(pParse, pLeft, pRight, OP_Ge,
! 76956: r1, r2, r3, SQLITE_STOREP2);
! 76957: pLItem++;
! 76958: pRight = pLItem->pExpr;
! 76959: sqlite3ReleaseTempReg(pParse, regFree2);
! 76960: r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2);
! 76961: testcase( regFree2==0 );
! 76962: codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
! 76963: sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
! 76964: sqlite3ReleaseTempReg(pParse, r3);
! 76965: sqlite3ReleaseTempReg(pParse, r4);
! 76966: break;
! 76967: }
! 76968: case TK_UPLUS: {
! 76969: inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
! 76970: break;
! 76971: }
! 76972:
! 76973: case TK_TRIGGER: {
! 76974: /* If the opcode is TK_TRIGGER, then the expression is a reference
! 76975: ** to a column in the new.* or old.* pseudo-tables available to
! 76976: ** trigger programs. In this case Expr.iTable is set to 1 for the
! 76977: ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
! 76978: ** is set to the column of the pseudo-table to read, or to -1 to
! 76979: ** read the rowid field.
! 76980: **
! 76981: ** The expression is implemented using an OP_Param opcode. The p1
! 76982: ** parameter is set to 0 for an old.rowid reference, or to (i+1)
! 76983: ** to reference another column of the old.* pseudo-table, where
! 76984: ** i is the index of the column. For a new.rowid reference, p1 is
! 76985: ** set to (n+1), where n is the number of columns in each pseudo-table.
! 76986: ** For a reference to any other column in the new.* pseudo-table, p1
! 76987: ** is set to (n+2+i), where n and i are as defined previously. For
! 76988: ** example, if the table on which triggers are being fired is
! 76989: ** declared as:
! 76990: **
! 76991: ** CREATE TABLE t1(a, b);
! 76992: **
! 76993: ** Then p1 is interpreted as follows:
! 76994: **
! 76995: ** p1==0 -> old.rowid p1==3 -> new.rowid
! 76996: ** p1==1 -> old.a p1==4 -> new.a
! 76997: ** p1==2 -> old.b p1==5 -> new.b
! 76998: */
! 76999: Table *pTab = pExpr->pTab;
! 77000: int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
! 77001:
! 77002: assert( pExpr->iTable==0 || pExpr->iTable==1 );
! 77003: assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
! 77004: assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
! 77005: assert( p1>=0 && p1<(pTab->nCol*2+2) );
! 77006:
! 77007: sqlite3VdbeAddOp2(v, OP_Param, p1, target);
! 77008: VdbeComment((v, "%s.%s -> $%d",
! 77009: (pExpr->iTable ? "new" : "old"),
! 77010: (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
! 77011: target
! 77012: ));
! 77013:
! 77014: #ifndef SQLITE_OMIT_FLOATING_POINT
! 77015: /* If the column has REAL affinity, it may currently be stored as an
! 77016: ** integer. Use OP_RealAffinity to make sure it is really real. */
! 77017: if( pExpr->iColumn>=0
! 77018: && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
! 77019: ){
! 77020: sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
! 77021: }
! 77022: #endif
! 77023: break;
! 77024: }
! 77025:
! 77026:
! 77027: /*
! 77028: ** Form A:
! 77029: ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
! 77030: **
! 77031: ** Form B:
! 77032: ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
! 77033: **
! 77034: ** Form A is can be transformed into the equivalent form B as follows:
! 77035: ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
! 77036: ** WHEN x=eN THEN rN ELSE y END
! 77037: **
! 77038: ** X (if it exists) is in pExpr->pLeft.
! 77039: ** Y is in pExpr->pRight. The Y is also optional. If there is no
! 77040: ** ELSE clause and no other term matches, then the result of the
! 77041: ** exprssion is NULL.
! 77042: ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
! 77043: **
! 77044: ** The result of the expression is the Ri for the first matching Ei,
! 77045: ** or if there is no matching Ei, the ELSE term Y, or if there is
! 77046: ** no ELSE term, NULL.
! 77047: */
! 77048: default: assert( op==TK_CASE ); {
! 77049: int endLabel; /* GOTO label for end of CASE stmt */
! 77050: int nextCase; /* GOTO label for next WHEN clause */
! 77051: int nExpr; /* 2x number of WHEN terms */
! 77052: int i; /* Loop counter */
! 77053: ExprList *pEList; /* List of WHEN terms */
! 77054: struct ExprList_item *aListelem; /* Array of WHEN terms */
! 77055: Expr opCompare; /* The X==Ei expression */
! 77056: Expr cacheX; /* Cached expression X */
! 77057: Expr *pX; /* The X expression */
! 77058: Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
! 77059: VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
! 77060:
! 77061: assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
! 77062: assert((pExpr->x.pList->nExpr % 2) == 0);
! 77063: assert(pExpr->x.pList->nExpr > 0);
! 77064: pEList = pExpr->x.pList;
! 77065: aListelem = pEList->a;
! 77066: nExpr = pEList->nExpr;
! 77067: endLabel = sqlite3VdbeMakeLabel(v);
! 77068: if( (pX = pExpr->pLeft)!=0 ){
! 77069: cacheX = *pX;
! 77070: testcase( pX->op==TK_COLUMN );
! 77071: testcase( pX->op==TK_REGISTER );
! 77072: cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, ®Free1);
! 77073: testcase( regFree1==0 );
! 77074: cacheX.op = TK_REGISTER;
! 77075: opCompare.op = TK_EQ;
! 77076: opCompare.pLeft = &cacheX;
! 77077: pTest = &opCompare;
! 77078: /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
! 77079: ** The value in regFree1 might get SCopy-ed into the file result.
! 77080: ** So make sure that the regFree1 register is not reused for other
! 77081: ** purposes and possibly overwritten. */
! 77082: regFree1 = 0;
! 77083: }
! 77084: for(i=0; i<nExpr; i=i+2){
! 77085: sqlite3ExprCachePush(pParse);
! 77086: if( pX ){
! 77087: assert( pTest!=0 );
! 77088: opCompare.pRight = aListelem[i].pExpr;
! 77089: }else{
! 77090: pTest = aListelem[i].pExpr;
! 77091: }
! 77092: nextCase = sqlite3VdbeMakeLabel(v);
! 77093: testcase( pTest->op==TK_COLUMN );
! 77094: sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
! 77095: testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
! 77096: testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
! 77097: sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
! 77098: sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
! 77099: sqlite3ExprCachePop(pParse, 1);
! 77100: sqlite3VdbeResolveLabel(v, nextCase);
! 77101: }
! 77102: if( pExpr->pRight ){
! 77103: sqlite3ExprCachePush(pParse);
! 77104: sqlite3ExprCode(pParse, pExpr->pRight, target);
! 77105: sqlite3ExprCachePop(pParse, 1);
! 77106: }else{
! 77107: sqlite3VdbeAddOp2(v, OP_Null, 0, target);
! 77108: }
! 77109: assert( db->mallocFailed || pParse->nErr>0
! 77110: || pParse->iCacheLevel==iCacheLevel );
! 77111: sqlite3VdbeResolveLabel(v, endLabel);
! 77112: break;
! 77113: }
! 77114: #ifndef SQLITE_OMIT_TRIGGER
! 77115: case TK_RAISE: {
! 77116: assert( pExpr->affinity==OE_Rollback
! 77117: || pExpr->affinity==OE_Abort
! 77118: || pExpr->affinity==OE_Fail
! 77119: || pExpr->affinity==OE_Ignore
! 77120: );
! 77121: if( !pParse->pTriggerTab ){
! 77122: sqlite3ErrorMsg(pParse,
! 77123: "RAISE() may only be used within a trigger-program");
! 77124: return 0;
! 77125: }
! 77126: if( pExpr->affinity==OE_Abort ){
! 77127: sqlite3MayAbort(pParse);
! 77128: }
! 77129: assert( !ExprHasProperty(pExpr, EP_IntValue) );
! 77130: if( pExpr->affinity==OE_Ignore ){
! 77131: sqlite3VdbeAddOp4(
! 77132: v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
! 77133: }else{
! 77134: sqlite3HaltConstraint(pParse, pExpr->affinity, pExpr->u.zToken, 0);
! 77135: }
! 77136:
! 77137: break;
! 77138: }
! 77139: #endif
! 77140: }
! 77141: sqlite3ReleaseTempReg(pParse, regFree1);
! 77142: sqlite3ReleaseTempReg(pParse, regFree2);
! 77143: return inReg;
! 77144: }
! 77145:
! 77146: /*
! 77147: ** Generate code to evaluate an expression and store the results
! 77148: ** into a register. Return the register number where the results
! 77149: ** are stored.
! 77150: **
! 77151: ** If the register is a temporary register that can be deallocated,
! 77152: ** then write its number into *pReg. If the result register is not
! 77153: ** a temporary, then set *pReg to zero.
! 77154: */
! 77155: SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
! 77156: int r1 = sqlite3GetTempReg(pParse);
! 77157: int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
! 77158: if( r2==r1 ){
! 77159: *pReg = r1;
! 77160: }else{
! 77161: sqlite3ReleaseTempReg(pParse, r1);
! 77162: *pReg = 0;
! 77163: }
! 77164: return r2;
! 77165: }
! 77166:
! 77167: /*
! 77168: ** Generate code that will evaluate expression pExpr and store the
! 77169: ** results in register target. The results are guaranteed to appear
! 77170: ** in register target.
! 77171: */
! 77172: SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
! 77173: int inReg;
! 77174:
! 77175: assert( target>0 && target<=pParse->nMem );
! 77176: if( pExpr && pExpr->op==TK_REGISTER ){
! 77177: sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
! 77178: }else{
! 77179: inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
! 77180: assert( pParse->pVdbe || pParse->db->mallocFailed );
! 77181: if( inReg!=target && pParse->pVdbe ){
! 77182: sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
! 77183: }
! 77184: }
! 77185: return target;
! 77186: }
! 77187:
! 77188: /*
! 77189: ** Generate code that evalutes the given expression and puts the result
! 77190: ** in register target.
! 77191: **
! 77192: ** Also make a copy of the expression results into another "cache" register
! 77193: ** and modify the expression so that the next time it is evaluated,
! 77194: ** the result is a copy of the cache register.
! 77195: **
! 77196: ** This routine is used for expressions that are used multiple
! 77197: ** times. They are evaluated once and the results of the expression
! 77198: ** are reused.
! 77199: */
! 77200: SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
! 77201: Vdbe *v = pParse->pVdbe;
! 77202: int inReg;
! 77203: inReg = sqlite3ExprCode(pParse, pExpr, target);
! 77204: assert( target>0 );
! 77205: /* This routine is called for terms to INSERT or UPDATE. And the only
! 77206: ** other place where expressions can be converted into TK_REGISTER is
! 77207: ** in WHERE clause processing. So as currently implemented, there is
! 77208: ** no way for a TK_REGISTER to exist here. But it seems prudent to
! 77209: ** keep the ALWAYS() in case the conditions above change with future
! 77210: ** modifications or enhancements. */
! 77211: if( ALWAYS(pExpr->op!=TK_REGISTER) ){
! 77212: int iMem;
! 77213: iMem = ++pParse->nMem;
! 77214: sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
! 77215: pExpr->iTable = iMem;
! 77216: pExpr->op2 = pExpr->op;
! 77217: pExpr->op = TK_REGISTER;
! 77218: }
! 77219: return inReg;
! 77220: }
! 77221:
! 77222: #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
! 77223: /*
! 77224: ** Generate a human-readable explanation of an expression tree.
! 77225: */
! 77226: SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
! 77227: int op; /* The opcode being coded */
! 77228: const char *zBinOp = 0; /* Binary operator */
! 77229: const char *zUniOp = 0; /* Unary operator */
! 77230: if( pExpr==0 ){
! 77231: op = TK_NULL;
! 77232: }else{
! 77233: op = pExpr->op;
! 77234: }
! 77235: switch( op ){
! 77236: case TK_AGG_COLUMN: {
! 77237: sqlite3ExplainPrintf(pOut, "AGG{%d:%d}",
! 77238: pExpr->iTable, pExpr->iColumn);
! 77239: break;
! 77240: }
! 77241: case TK_COLUMN: {
! 77242: if( pExpr->iTable<0 ){
! 77243: /* This only happens when coding check constraints */
! 77244: sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn);
! 77245: }else{
! 77246: sqlite3ExplainPrintf(pOut, "{%d:%d}",
! 77247: pExpr->iTable, pExpr->iColumn);
! 77248: }
! 77249: break;
! 77250: }
! 77251: case TK_INTEGER: {
! 77252: if( pExpr->flags & EP_IntValue ){
! 77253: sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue);
! 77254: }else{
! 77255: sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken);
! 77256: }
! 77257: break;
! 77258: }
! 77259: #ifndef SQLITE_OMIT_FLOATING_POINT
! 77260: case TK_FLOAT: {
! 77261: sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
! 77262: break;
! 77263: }
! 77264: #endif
! 77265: case TK_STRING: {
! 77266: sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken);
! 77267: break;
! 77268: }
! 77269: case TK_NULL: {
! 77270: sqlite3ExplainPrintf(pOut,"NULL");
! 77271: break;
! 77272: }
! 77273: #ifndef SQLITE_OMIT_BLOB_LITERAL
! 77274: case TK_BLOB: {
! 77275: sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
! 77276: break;
! 77277: }
! 77278: #endif
! 77279: case TK_VARIABLE: {
! 77280: sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)",
! 77281: pExpr->u.zToken, pExpr->iColumn);
! 77282: break;
! 77283: }
! 77284: case TK_REGISTER: {
! 77285: sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable);
! 77286: break;
! 77287: }
! 77288: case TK_AS: {
! 77289: sqlite3ExplainExpr(pOut, pExpr->pLeft);
! 77290: break;
! 77291: }
! 77292: #ifndef SQLITE_OMIT_CAST
! 77293: case TK_CAST: {
! 77294: /* Expressions of the form: CAST(pLeft AS token) */
! 77295: const char *zAff = "unk";
! 77296: switch( sqlite3AffinityType(pExpr->u.zToken) ){
! 77297: case SQLITE_AFF_TEXT: zAff = "TEXT"; break;
! 77298: case SQLITE_AFF_NONE: zAff = "NONE"; break;
! 77299: case SQLITE_AFF_NUMERIC: zAff = "NUMERIC"; break;
! 77300: case SQLITE_AFF_INTEGER: zAff = "INTEGER"; break;
! 77301: case SQLITE_AFF_REAL: zAff = "REAL"; break;
! 77302: }
! 77303: sqlite3ExplainPrintf(pOut, "CAST-%s(", zAff);
! 77304: sqlite3ExplainExpr(pOut, pExpr->pLeft);
! 77305: sqlite3ExplainPrintf(pOut, ")");
! 77306: break;
! 77307: }
! 77308: #endif /* SQLITE_OMIT_CAST */
! 77309: case TK_LT: zBinOp = "LT"; break;
! 77310: case TK_LE: zBinOp = "LE"; break;
! 77311: case TK_GT: zBinOp = "GT"; break;
! 77312: case TK_GE: zBinOp = "GE"; break;
! 77313: case TK_NE: zBinOp = "NE"; break;
! 77314: case TK_EQ: zBinOp = "EQ"; break;
! 77315: case TK_IS: zBinOp = "IS"; break;
! 77316: case TK_ISNOT: zBinOp = "ISNOT"; break;
! 77317: case TK_AND: zBinOp = "AND"; break;
! 77318: case TK_OR: zBinOp = "OR"; break;
! 77319: case TK_PLUS: zBinOp = "ADD"; break;
! 77320: case TK_STAR: zBinOp = "MUL"; break;
! 77321: case TK_MINUS: zBinOp = "SUB"; break;
! 77322: case TK_REM: zBinOp = "REM"; break;
! 77323: case TK_BITAND: zBinOp = "BITAND"; break;
! 77324: case TK_BITOR: zBinOp = "BITOR"; break;
! 77325: case TK_SLASH: zBinOp = "DIV"; break;
! 77326: case TK_LSHIFT: zBinOp = "LSHIFT"; break;
! 77327: case TK_RSHIFT: zBinOp = "RSHIFT"; break;
! 77328: case TK_CONCAT: zBinOp = "CONCAT"; break;
! 77329:
! 77330: case TK_UMINUS: zUniOp = "UMINUS"; break;
! 77331: case TK_UPLUS: zUniOp = "UPLUS"; break;
! 77332: case TK_BITNOT: zUniOp = "BITNOT"; break;
! 77333: case TK_NOT: zUniOp = "NOT"; break;
! 77334: case TK_ISNULL: zUniOp = "ISNULL"; break;
! 77335: case TK_NOTNULL: zUniOp = "NOTNULL"; break;
! 77336:
! 77337: case TK_AGG_FUNCTION:
! 77338: case TK_CONST_FUNC:
! 77339: case TK_FUNCTION: {
! 77340: ExprList *pFarg; /* List of function arguments */
! 77341: if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
! 77342: pFarg = 0;
! 77343: }else{
! 77344: pFarg = pExpr->x.pList;
! 77345: }
! 77346: sqlite3ExplainPrintf(pOut, "%sFUNCTION:%s(",
! 77347: op==TK_AGG_FUNCTION ? "AGG_" : "",
! 77348: pExpr->u.zToken);
! 77349: if( pFarg ){
! 77350: sqlite3ExplainExprList(pOut, pFarg);
! 77351: }
! 77352: sqlite3ExplainPrintf(pOut, ")");
! 77353: break;
! 77354: }
! 77355: #ifndef SQLITE_OMIT_SUBQUERY
! 77356: case TK_EXISTS: {
! 77357: sqlite3ExplainPrintf(pOut, "EXISTS(");
! 77358: sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
! 77359: sqlite3ExplainPrintf(pOut,")");
! 77360: break;
! 77361: }
! 77362: case TK_SELECT: {
! 77363: sqlite3ExplainPrintf(pOut, "(");
! 77364: sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
! 77365: sqlite3ExplainPrintf(pOut, ")");
! 77366: break;
! 77367: }
! 77368: case TK_IN: {
! 77369: sqlite3ExplainPrintf(pOut, "IN(");
! 77370: sqlite3ExplainExpr(pOut, pExpr->pLeft);
! 77371: sqlite3ExplainPrintf(pOut, ",");
! 77372: if( ExprHasProperty(pExpr, EP_xIsSelect) ){
! 77373: sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
! 77374: }else{
! 77375: sqlite3ExplainExprList(pOut, pExpr->x.pList);
! 77376: }
! 77377: sqlite3ExplainPrintf(pOut, ")");
! 77378: break;
! 77379: }
! 77380: #endif /* SQLITE_OMIT_SUBQUERY */
! 77381:
! 77382: /*
! 77383: ** x BETWEEN y AND z
! 77384: **
! 77385: ** This is equivalent to
! 77386: **
! 77387: ** x>=y AND x<=z
! 77388: **
! 77389: ** X is stored in pExpr->pLeft.
! 77390: ** Y is stored in pExpr->pList->a[0].pExpr.
! 77391: ** Z is stored in pExpr->pList->a[1].pExpr.
! 77392: */
! 77393: case TK_BETWEEN: {
! 77394: Expr *pX = pExpr->pLeft;
! 77395: Expr *pY = pExpr->x.pList->a[0].pExpr;
! 77396: Expr *pZ = pExpr->x.pList->a[1].pExpr;
! 77397: sqlite3ExplainPrintf(pOut, "BETWEEN(");
! 77398: sqlite3ExplainExpr(pOut, pX);
! 77399: sqlite3ExplainPrintf(pOut, ",");
! 77400: sqlite3ExplainExpr(pOut, pY);
! 77401: sqlite3ExplainPrintf(pOut, ",");
! 77402: sqlite3ExplainExpr(pOut, pZ);
! 77403: sqlite3ExplainPrintf(pOut, ")");
! 77404: break;
! 77405: }
! 77406: case TK_TRIGGER: {
! 77407: /* If the opcode is TK_TRIGGER, then the expression is a reference
! 77408: ** to a column in the new.* or old.* pseudo-tables available to
! 77409: ** trigger programs. In this case Expr.iTable is set to 1 for the
! 77410: ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
! 77411: ** is set to the column of the pseudo-table to read, or to -1 to
! 77412: ** read the rowid field.
! 77413: */
! 77414: sqlite3ExplainPrintf(pOut, "%s(%d)",
! 77415: pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
! 77416: break;
! 77417: }
! 77418: case TK_CASE: {
! 77419: sqlite3ExplainPrintf(pOut, "CASE(");
! 77420: sqlite3ExplainExpr(pOut, pExpr->pLeft);
! 77421: sqlite3ExplainPrintf(pOut, ",");
! 77422: sqlite3ExplainExprList(pOut, pExpr->x.pList);
! 77423: break;
! 77424: }
! 77425: #ifndef SQLITE_OMIT_TRIGGER
! 77426: case TK_RAISE: {
! 77427: const char *zType = "unk";
! 77428: switch( pExpr->affinity ){
! 77429: case OE_Rollback: zType = "rollback"; break;
! 77430: case OE_Abort: zType = "abort"; break;
! 77431: case OE_Fail: zType = "fail"; break;
! 77432: case OE_Ignore: zType = "ignore"; break;
! 77433: }
! 77434: sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken);
! 77435: break;
! 77436: }
! 77437: #endif
! 77438: }
! 77439: if( zBinOp ){
! 77440: sqlite3ExplainPrintf(pOut,"%s(", zBinOp);
! 77441: sqlite3ExplainExpr(pOut, pExpr->pLeft);
! 77442: sqlite3ExplainPrintf(pOut,",");
! 77443: sqlite3ExplainExpr(pOut, pExpr->pRight);
! 77444: sqlite3ExplainPrintf(pOut,")");
! 77445: }else if( zUniOp ){
! 77446: sqlite3ExplainPrintf(pOut,"%s(", zUniOp);
! 77447: sqlite3ExplainExpr(pOut, pExpr->pLeft);
! 77448: sqlite3ExplainPrintf(pOut,")");
! 77449: }
! 77450: }
! 77451: #endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
! 77452:
! 77453: #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
! 77454: /*
! 77455: ** Generate a human-readable explanation of an expression list.
! 77456: */
! 77457: SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){
! 77458: int i;
! 77459: if( pList==0 || pList->nExpr==0 ){
! 77460: sqlite3ExplainPrintf(pOut, "(empty-list)");
! 77461: return;
! 77462: }else if( pList->nExpr==1 ){
! 77463: sqlite3ExplainExpr(pOut, pList->a[0].pExpr);
! 77464: }else{
! 77465: sqlite3ExplainPush(pOut);
! 77466: for(i=0; i<pList->nExpr; i++){
! 77467: sqlite3ExplainPrintf(pOut, "item[%d] = ", i);
! 77468: sqlite3ExplainPush(pOut);
! 77469: sqlite3ExplainExpr(pOut, pList->a[i].pExpr);
! 77470: sqlite3ExplainPop(pOut);
! 77471: if( i<pList->nExpr-1 ){
! 77472: sqlite3ExplainNL(pOut);
! 77473: }
! 77474: }
! 77475: sqlite3ExplainPop(pOut);
! 77476: }
! 77477: }
! 77478: #endif /* SQLITE_DEBUG */
! 77479:
! 77480: /*
! 77481: ** Return TRUE if pExpr is an constant expression that is appropriate
! 77482: ** for factoring out of a loop. Appropriate expressions are:
! 77483: **
! 77484: ** * Any expression that evaluates to two or more opcodes.
! 77485: **
! 77486: ** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null,
! 77487: ** or OP_Variable that does not need to be placed in a
! 77488: ** specific register.
! 77489: **
! 77490: ** There is no point in factoring out single-instruction constant
! 77491: ** expressions that need to be placed in a particular register.
! 77492: ** We could factor them out, but then we would end up adding an
! 77493: ** OP_SCopy instruction to move the value into the correct register
! 77494: ** later. We might as well just use the original instruction and
! 77495: ** avoid the OP_SCopy.
! 77496: */
! 77497: static int isAppropriateForFactoring(Expr *p){
! 77498: if( !sqlite3ExprIsConstantNotJoin(p) ){
! 77499: return 0; /* Only constant expressions are appropriate for factoring */
! 77500: }
! 77501: if( (p->flags & EP_FixedDest)==0 ){
! 77502: return 1; /* Any constant without a fixed destination is appropriate */
! 77503: }
! 77504: while( p->op==TK_UPLUS ) p = p->pLeft;
! 77505: switch( p->op ){
! 77506: #ifndef SQLITE_OMIT_BLOB_LITERAL
! 77507: case TK_BLOB:
! 77508: #endif
! 77509: case TK_VARIABLE:
! 77510: case TK_INTEGER:
! 77511: case TK_FLOAT:
! 77512: case TK_NULL:
! 77513: case TK_STRING: {
! 77514: testcase( p->op==TK_BLOB );
! 77515: testcase( p->op==TK_VARIABLE );
! 77516: testcase( p->op==TK_INTEGER );
! 77517: testcase( p->op==TK_FLOAT );
! 77518: testcase( p->op==TK_NULL );
! 77519: testcase( p->op==TK_STRING );
! 77520: /* Single-instruction constants with a fixed destination are
! 77521: ** better done in-line. If we factor them, they will just end
! 77522: ** up generating an OP_SCopy to move the value to the destination
! 77523: ** register. */
! 77524: return 0;
! 77525: }
! 77526: case TK_UMINUS: {
! 77527: if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
! 77528: return 0;
! 77529: }
! 77530: break;
! 77531: }
! 77532: default: {
! 77533: break;
! 77534: }
! 77535: }
! 77536: return 1;
! 77537: }
! 77538:
! 77539: /*
! 77540: ** If pExpr is a constant expression that is appropriate for
! 77541: ** factoring out of a loop, then evaluate the expression
! 77542: ** into a register and convert the expression into a TK_REGISTER
! 77543: ** expression.
! 77544: */
! 77545: static int evalConstExpr(Walker *pWalker, Expr *pExpr){
! 77546: Parse *pParse = pWalker->pParse;
! 77547: switch( pExpr->op ){
! 77548: case TK_IN:
! 77549: case TK_REGISTER: {
! 77550: return WRC_Prune;
! 77551: }
! 77552: case TK_FUNCTION:
! 77553: case TK_AGG_FUNCTION:
! 77554: case TK_CONST_FUNC: {
! 77555: /* The arguments to a function have a fixed destination.
! 77556: ** Mark them this way to avoid generated unneeded OP_SCopy
! 77557: ** instructions.
! 77558: */
! 77559: ExprList *pList = pExpr->x.pList;
! 77560: assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
! 77561: if( pList ){
! 77562: int i = pList->nExpr;
! 77563: struct ExprList_item *pItem = pList->a;
! 77564: for(; i>0; i--, pItem++){
! 77565: if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest;
! 77566: }
! 77567: }
! 77568: break;
! 77569: }
! 77570: }
! 77571: if( isAppropriateForFactoring(pExpr) ){
! 77572: int r1 = ++pParse->nMem;
! 77573: int r2;
! 77574: r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
! 77575: if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1);
! 77576: pExpr->op2 = pExpr->op;
! 77577: pExpr->op = TK_REGISTER;
! 77578: pExpr->iTable = r2;
! 77579: return WRC_Prune;
! 77580: }
! 77581: return WRC_Continue;
! 77582: }
! 77583:
! 77584: /*
! 77585: ** Preevaluate constant subexpressions within pExpr and store the
! 77586: ** results in registers. Modify pExpr so that the constant subexpresions
! 77587: ** are TK_REGISTER opcodes that refer to the precomputed values.
! 77588: **
! 77589: ** This routine is a no-op if the jump to the cookie-check code has
! 77590: ** already occur. Since the cookie-check jump is generated prior to
! 77591: ** any other serious processing, this check ensures that there is no
! 77592: ** way to accidently bypass the constant initializations.
! 77593: **
! 77594: ** This routine is also a no-op if the SQLITE_FactorOutConst optimization
! 77595: ** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS)
! 77596: ** interface. This allows test logic to verify that the same answer is
! 77597: ** obtained for queries regardless of whether or not constants are
! 77598: ** precomputed into registers or if they are inserted in-line.
! 77599: */
! 77600: SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
! 77601: Walker w;
! 77602: if( pParse->cookieGoto ) return;
! 77603: if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return;
! 77604: w.xExprCallback = evalConstExpr;
! 77605: w.xSelectCallback = 0;
! 77606: w.pParse = pParse;
! 77607: sqlite3WalkExpr(&w, pExpr);
! 77608: }
! 77609:
! 77610:
! 77611: /*
! 77612: ** Generate code that pushes the value of every element of the given
! 77613: ** expression list into a sequence of registers beginning at target.
! 77614: **
! 77615: ** Return the number of elements evaluated.
! 77616: */
! 77617: SQLITE_PRIVATE int sqlite3ExprCodeExprList(
! 77618: Parse *pParse, /* Parsing context */
! 77619: ExprList *pList, /* The expression list to be coded */
! 77620: int target, /* Where to write results */
! 77621: int doHardCopy /* Make a hard copy of every element */
! 77622: ){
! 77623: struct ExprList_item *pItem;
! 77624: int i, n;
! 77625: assert( pList!=0 );
! 77626: assert( target>0 );
! 77627: assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
! 77628: n = pList->nExpr;
! 77629: for(pItem=pList->a, i=0; i<n; i++, pItem++){
! 77630: Expr *pExpr = pItem->pExpr;
! 77631: int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
! 77632: if( inReg!=target+i ){
! 77633: sqlite3VdbeAddOp2(pParse->pVdbe, doHardCopy ? OP_Copy : OP_SCopy,
! 77634: inReg, target+i);
! 77635: }
! 77636: }
! 77637: return n;
! 77638: }
! 77639:
! 77640: /*
! 77641: ** Generate code for a BETWEEN operator.
! 77642: **
! 77643: ** x BETWEEN y AND z
! 77644: **
! 77645: ** The above is equivalent to
! 77646: **
! 77647: ** x>=y AND x<=z
! 77648: **
! 77649: ** Code it as such, taking care to do the common subexpression
! 77650: ** elementation of x.
! 77651: */
! 77652: static void exprCodeBetween(
! 77653: Parse *pParse, /* Parsing and code generating context */
! 77654: Expr *pExpr, /* The BETWEEN expression */
! 77655: int dest, /* Jump here if the jump is taken */
! 77656: int jumpIfTrue, /* Take the jump if the BETWEEN is true */
! 77657: int jumpIfNull /* Take the jump if the BETWEEN is NULL */
! 77658: ){
! 77659: Expr exprAnd; /* The AND operator in x>=y AND x<=z */
! 77660: Expr compLeft; /* The x>=y term */
! 77661: Expr compRight; /* The x<=z term */
! 77662: Expr exprX; /* The x subexpression */
! 77663: int regFree1 = 0; /* Temporary use register */
! 77664:
! 77665: assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
! 77666: exprX = *pExpr->pLeft;
! 77667: exprAnd.op = TK_AND;
! 77668: exprAnd.pLeft = &compLeft;
! 77669: exprAnd.pRight = &compRight;
! 77670: compLeft.op = TK_GE;
! 77671: compLeft.pLeft = &exprX;
! 77672: compLeft.pRight = pExpr->x.pList->a[0].pExpr;
! 77673: compRight.op = TK_LE;
! 77674: compRight.pLeft = &exprX;
! 77675: compRight.pRight = pExpr->x.pList->a[1].pExpr;
! 77676: exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1);
! 77677: exprX.op = TK_REGISTER;
! 77678: if( jumpIfTrue ){
! 77679: sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
! 77680: }else{
! 77681: sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
! 77682: }
! 77683: sqlite3ReleaseTempReg(pParse, regFree1);
! 77684:
! 77685: /* Ensure adequate test coverage */
! 77686: testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
! 77687: testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
! 77688: testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
! 77689: testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
! 77690: testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
! 77691: testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
! 77692: testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
! 77693: testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
! 77694: }
! 77695:
! 77696: /*
! 77697: ** Generate code for a boolean expression such that a jump is made
! 77698: ** to the label "dest" if the expression is true but execution
! 77699: ** continues straight thru if the expression is false.
! 77700: **
! 77701: ** If the expression evaluates to NULL (neither true nor false), then
! 77702: ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
! 77703: **
! 77704: ** This code depends on the fact that certain token values (ex: TK_EQ)
! 77705: ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
! 77706: ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
! 77707: ** the make process cause these values to align. Assert()s in the code
! 77708: ** below verify that the numbers are aligned correctly.
! 77709: */
! 77710: SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
! 77711: Vdbe *v = pParse->pVdbe;
! 77712: int op = 0;
! 77713: int regFree1 = 0;
! 77714: int regFree2 = 0;
! 77715: int r1, r2;
! 77716:
! 77717: assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
! 77718: if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */
! 77719: if( NEVER(pExpr==0) ) return; /* No way this can happen */
! 77720: op = pExpr->op;
! 77721: switch( op ){
! 77722: case TK_AND: {
! 77723: int d2 = sqlite3VdbeMakeLabel(v);
! 77724: testcase( jumpIfNull==0 );
! 77725: sqlite3ExprCachePush(pParse);
! 77726: sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
! 77727: sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
! 77728: sqlite3VdbeResolveLabel(v, d2);
! 77729: sqlite3ExprCachePop(pParse, 1);
! 77730: break;
! 77731: }
! 77732: case TK_OR: {
! 77733: testcase( jumpIfNull==0 );
! 77734: sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
! 77735: sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
! 77736: break;
! 77737: }
! 77738: case TK_NOT: {
! 77739: testcase( jumpIfNull==0 );
! 77740: sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
! 77741: break;
! 77742: }
! 77743: case TK_LT:
! 77744: case TK_LE:
! 77745: case TK_GT:
! 77746: case TK_GE:
! 77747: case TK_NE:
! 77748: case TK_EQ: {
! 77749: assert( TK_LT==OP_Lt );
! 77750: assert( TK_LE==OP_Le );
! 77751: assert( TK_GT==OP_Gt );
! 77752: assert( TK_GE==OP_Ge );
! 77753: assert( TK_EQ==OP_Eq );
! 77754: assert( TK_NE==OP_Ne );
! 77755: testcase( op==TK_LT );
! 77756: testcase( op==TK_LE );
! 77757: testcase( op==TK_GT );
! 77758: testcase( op==TK_GE );
! 77759: testcase( op==TK_EQ );
! 77760: testcase( op==TK_NE );
! 77761: testcase( jumpIfNull==0 );
! 77762: r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
! 77763: r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
! 77764: codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
! 77765: r1, r2, dest, jumpIfNull);
! 77766: testcase( regFree1==0 );
! 77767: testcase( regFree2==0 );
! 77768: break;
! 77769: }
! 77770: case TK_IS:
! 77771: case TK_ISNOT: {
! 77772: testcase( op==TK_IS );
! 77773: testcase( op==TK_ISNOT );
! 77774: r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
! 77775: r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
! 77776: op = (op==TK_IS) ? TK_EQ : TK_NE;
! 77777: codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
! 77778: r1, r2, dest, SQLITE_NULLEQ);
! 77779: testcase( regFree1==0 );
! 77780: testcase( regFree2==0 );
! 77781: break;
! 77782: }
! 77783: case TK_ISNULL:
! 77784: case TK_NOTNULL: {
! 77785: assert( TK_ISNULL==OP_IsNull );
! 77786: assert( TK_NOTNULL==OP_NotNull );
! 77787: testcase( op==TK_ISNULL );
! 77788: testcase( op==TK_NOTNULL );
! 77789: r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
! 77790: sqlite3VdbeAddOp2(v, op, r1, dest);
! 77791: testcase( regFree1==0 );
! 77792: break;
! 77793: }
! 77794: case TK_BETWEEN: {
! 77795: testcase( jumpIfNull==0 );
! 77796: exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
! 77797: break;
! 77798: }
! 77799: #ifndef SQLITE_OMIT_SUBQUERY
! 77800: case TK_IN: {
! 77801: int destIfFalse = sqlite3VdbeMakeLabel(v);
! 77802: int destIfNull = jumpIfNull ? dest : destIfFalse;
! 77803: sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
! 77804: sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
! 77805: sqlite3VdbeResolveLabel(v, destIfFalse);
! 77806: break;
! 77807: }
! 77808: #endif
! 77809: default: {
! 77810: r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1);
! 77811: sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
! 77812: testcase( regFree1==0 );
! 77813: testcase( jumpIfNull==0 );
! 77814: break;
! 77815: }
! 77816: }
! 77817: sqlite3ReleaseTempReg(pParse, regFree1);
! 77818: sqlite3ReleaseTempReg(pParse, regFree2);
! 77819: }
! 77820:
! 77821: /*
! 77822: ** Generate code for a boolean expression such that a jump is made
! 77823: ** to the label "dest" if the expression is false but execution
! 77824: ** continues straight thru if the expression is true.
! 77825: **
! 77826: ** If the expression evaluates to NULL (neither true nor false) then
! 77827: ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
! 77828: ** is 0.
! 77829: */
! 77830: SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
! 77831: Vdbe *v = pParse->pVdbe;
! 77832: int op = 0;
! 77833: int regFree1 = 0;
! 77834: int regFree2 = 0;
! 77835: int r1, r2;
! 77836:
! 77837: assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
! 77838: if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */
! 77839: if( pExpr==0 ) return;
! 77840:
! 77841: /* The value of pExpr->op and op are related as follows:
! 77842: **
! 77843: ** pExpr->op op
! 77844: ** --------- ----------
! 77845: ** TK_ISNULL OP_NotNull
! 77846: ** TK_NOTNULL OP_IsNull
! 77847: ** TK_NE OP_Eq
! 77848: ** TK_EQ OP_Ne
! 77849: ** TK_GT OP_Le
! 77850: ** TK_LE OP_Gt
! 77851: ** TK_GE OP_Lt
! 77852: ** TK_LT OP_Ge
! 77853: **
! 77854: ** For other values of pExpr->op, op is undefined and unused.
! 77855: ** The value of TK_ and OP_ constants are arranged such that we
! 77856: ** can compute the mapping above using the following expression.
! 77857: ** Assert()s verify that the computation is correct.
! 77858: */
! 77859: op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
! 77860:
! 77861: /* Verify correct alignment of TK_ and OP_ constants
! 77862: */
! 77863: assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
! 77864: assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
! 77865: assert( pExpr->op!=TK_NE || op==OP_Eq );
! 77866: assert( pExpr->op!=TK_EQ || op==OP_Ne );
! 77867: assert( pExpr->op!=TK_LT || op==OP_Ge );
! 77868: assert( pExpr->op!=TK_LE || op==OP_Gt );
! 77869: assert( pExpr->op!=TK_GT || op==OP_Le );
! 77870: assert( pExpr->op!=TK_GE || op==OP_Lt );
! 77871:
! 77872: switch( pExpr->op ){
! 77873: case TK_AND: {
! 77874: testcase( jumpIfNull==0 );
! 77875: sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
! 77876: sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
! 77877: break;
! 77878: }
! 77879: case TK_OR: {
! 77880: int d2 = sqlite3VdbeMakeLabel(v);
! 77881: testcase( jumpIfNull==0 );
! 77882: sqlite3ExprCachePush(pParse);
! 77883: sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
! 77884: sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
! 77885: sqlite3VdbeResolveLabel(v, d2);
! 77886: sqlite3ExprCachePop(pParse, 1);
! 77887: break;
! 77888: }
! 77889: case TK_NOT: {
! 77890: testcase( jumpIfNull==0 );
! 77891: sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
! 77892: break;
! 77893: }
! 77894: case TK_LT:
! 77895: case TK_LE:
! 77896: case TK_GT:
! 77897: case TK_GE:
! 77898: case TK_NE:
! 77899: case TK_EQ: {
! 77900: testcase( op==TK_LT );
! 77901: testcase( op==TK_LE );
! 77902: testcase( op==TK_GT );
! 77903: testcase( op==TK_GE );
! 77904: testcase( op==TK_EQ );
! 77905: testcase( op==TK_NE );
! 77906: testcase( jumpIfNull==0 );
! 77907: r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
! 77908: r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
! 77909: codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
! 77910: r1, r2, dest, jumpIfNull);
! 77911: testcase( regFree1==0 );
! 77912: testcase( regFree2==0 );
! 77913: break;
! 77914: }
! 77915: case TK_IS:
! 77916: case TK_ISNOT: {
! 77917: testcase( pExpr->op==TK_IS );
! 77918: testcase( pExpr->op==TK_ISNOT );
! 77919: r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
! 77920: r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
! 77921: op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
! 77922: codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
! 77923: r1, r2, dest, SQLITE_NULLEQ);
! 77924: testcase( regFree1==0 );
! 77925: testcase( regFree2==0 );
! 77926: break;
! 77927: }
! 77928: case TK_ISNULL:
! 77929: case TK_NOTNULL: {
! 77930: testcase( op==TK_ISNULL );
! 77931: testcase( op==TK_NOTNULL );
! 77932: r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
! 77933: sqlite3VdbeAddOp2(v, op, r1, dest);
! 77934: testcase( regFree1==0 );
! 77935: break;
! 77936: }
! 77937: case TK_BETWEEN: {
! 77938: testcase( jumpIfNull==0 );
! 77939: exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
! 77940: break;
! 77941: }
! 77942: #ifndef SQLITE_OMIT_SUBQUERY
! 77943: case TK_IN: {
! 77944: if( jumpIfNull ){
! 77945: sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
! 77946: }else{
! 77947: int destIfNull = sqlite3VdbeMakeLabel(v);
! 77948: sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
! 77949: sqlite3VdbeResolveLabel(v, destIfNull);
! 77950: }
! 77951: break;
! 77952: }
! 77953: #endif
! 77954: default: {
! 77955: r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1);
! 77956: sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
! 77957: testcase( regFree1==0 );
! 77958: testcase( jumpIfNull==0 );
! 77959: break;
! 77960: }
! 77961: }
! 77962: sqlite3ReleaseTempReg(pParse, regFree1);
! 77963: sqlite3ReleaseTempReg(pParse, regFree2);
! 77964: }
! 77965:
! 77966: /*
! 77967: ** Do a deep comparison of two expression trees. Return 0 if the two
! 77968: ** expressions are completely identical. Return 1 if they differ only
! 77969: ** by a COLLATE operator at the top level. Return 2 if there are differences
! 77970: ** other than the top-level COLLATE operator.
! 77971: **
! 77972: ** Sometimes this routine will return 2 even if the two expressions
! 77973: ** really are equivalent. If we cannot prove that the expressions are
! 77974: ** identical, we return 2 just to be safe. So if this routine
! 77975: ** returns 2, then you do not really know for certain if the two
! 77976: ** expressions are the same. But if you get a 0 or 1 return, then you
! 77977: ** can be sure the expressions are the same. In the places where
! 77978: ** this routine is used, it does not hurt to get an extra 2 - that
! 77979: ** just might result in some slightly slower code. But returning
! 77980: ** an incorrect 0 or 1 could lead to a malfunction.
! 77981: */
! 77982: SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB){
! 77983: if( pA==0||pB==0 ){
! 77984: return pB==pA ? 0 : 2;
! 77985: }
! 77986: assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
! 77987: assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
! 77988: if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
! 77989: return 2;
! 77990: }
! 77991: if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
! 77992: if( pA->op!=pB->op ) return 2;
! 77993: if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
! 77994: if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
! 77995: if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
! 77996: if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
! 77997: if( ExprHasProperty(pA, EP_IntValue) ){
! 77998: if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
! 77999: return 2;
! 78000: }
! 78001: }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
! 78002: if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
! 78003: if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
! 78004: return 2;
! 78005: }
! 78006: }
! 78007: if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
! 78008: if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
! 78009: return 0;
! 78010: }
! 78011:
! 78012: /*
! 78013: ** Compare two ExprList objects. Return 0 if they are identical and
! 78014: ** non-zero if they differ in any way.
! 78015: **
! 78016: ** This routine might return non-zero for equivalent ExprLists. The
! 78017: ** only consequence will be disabled optimizations. But this routine
! 78018: ** must never return 0 if the two ExprList objects are different, or
! 78019: ** a malfunction will result.
! 78020: **
! 78021: ** Two NULL pointers are considered to be the same. But a NULL pointer
! 78022: ** always differs from a non-NULL pointer.
! 78023: */
! 78024: SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){
! 78025: int i;
! 78026: if( pA==0 && pB==0 ) return 0;
! 78027: if( pA==0 || pB==0 ) return 1;
! 78028: if( pA->nExpr!=pB->nExpr ) return 1;
! 78029: for(i=0; i<pA->nExpr; i++){
! 78030: Expr *pExprA = pA->a[i].pExpr;
! 78031: Expr *pExprB = pB->a[i].pExpr;
! 78032: if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
! 78033: if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
! 78034: }
! 78035: return 0;
! 78036: }
! 78037:
! 78038: /*
! 78039: ** Add a new element to the pAggInfo->aCol[] array. Return the index of
! 78040: ** the new element. Return a negative number if malloc fails.
! 78041: */
! 78042: static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
! 78043: int i;
! 78044: pInfo->aCol = sqlite3ArrayAllocate(
! 78045: db,
! 78046: pInfo->aCol,
! 78047: sizeof(pInfo->aCol[0]),
! 78048: 3,
! 78049: &pInfo->nColumn,
! 78050: &pInfo->nColumnAlloc,
! 78051: &i
! 78052: );
! 78053: return i;
! 78054: }
! 78055:
! 78056: /*
! 78057: ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
! 78058: ** the new element. Return a negative number if malloc fails.
! 78059: */
! 78060: static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
! 78061: int i;
! 78062: pInfo->aFunc = sqlite3ArrayAllocate(
! 78063: db,
! 78064: pInfo->aFunc,
! 78065: sizeof(pInfo->aFunc[0]),
! 78066: 3,
! 78067: &pInfo->nFunc,
! 78068: &pInfo->nFuncAlloc,
! 78069: &i
! 78070: );
! 78071: return i;
! 78072: }
! 78073:
! 78074: /*
! 78075: ** This is the xExprCallback for a tree walker. It is used to
! 78076: ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
! 78077: ** for additional information.
! 78078: */
! 78079: static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
! 78080: int i;
! 78081: NameContext *pNC = pWalker->u.pNC;
! 78082: Parse *pParse = pNC->pParse;
! 78083: SrcList *pSrcList = pNC->pSrcList;
! 78084: AggInfo *pAggInfo = pNC->pAggInfo;
! 78085:
! 78086: switch( pExpr->op ){
! 78087: case TK_AGG_COLUMN:
! 78088: case TK_COLUMN: {
! 78089: testcase( pExpr->op==TK_AGG_COLUMN );
! 78090: testcase( pExpr->op==TK_COLUMN );
! 78091: /* Check to see if the column is in one of the tables in the FROM
! 78092: ** clause of the aggregate query */
! 78093: if( ALWAYS(pSrcList!=0) ){
! 78094: struct SrcList_item *pItem = pSrcList->a;
! 78095: for(i=0; i<pSrcList->nSrc; i++, pItem++){
! 78096: struct AggInfo_col *pCol;
! 78097: assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
! 78098: if( pExpr->iTable==pItem->iCursor ){
! 78099: /* If we reach this point, it means that pExpr refers to a table
! 78100: ** that is in the FROM clause of the aggregate query.
! 78101: **
! 78102: ** Make an entry for the column in pAggInfo->aCol[] if there
! 78103: ** is not an entry there already.
! 78104: */
! 78105: int k;
! 78106: pCol = pAggInfo->aCol;
! 78107: for(k=0; k<pAggInfo->nColumn; k++, pCol++){
! 78108: if( pCol->iTable==pExpr->iTable &&
! 78109: pCol->iColumn==pExpr->iColumn ){
! 78110: break;
! 78111: }
! 78112: }
! 78113: if( (k>=pAggInfo->nColumn)
! 78114: && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
! 78115: ){
! 78116: pCol = &pAggInfo->aCol[k];
! 78117: pCol->pTab = pExpr->pTab;
! 78118: pCol->iTable = pExpr->iTable;
! 78119: pCol->iColumn = pExpr->iColumn;
! 78120: pCol->iMem = ++pParse->nMem;
! 78121: pCol->iSorterColumn = -1;
! 78122: pCol->pExpr = pExpr;
! 78123: if( pAggInfo->pGroupBy ){
! 78124: int j, n;
! 78125: ExprList *pGB = pAggInfo->pGroupBy;
! 78126: struct ExprList_item *pTerm = pGB->a;
! 78127: n = pGB->nExpr;
! 78128: for(j=0; j<n; j++, pTerm++){
! 78129: Expr *pE = pTerm->pExpr;
! 78130: if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
! 78131: pE->iColumn==pExpr->iColumn ){
! 78132: pCol->iSorterColumn = j;
! 78133: break;
! 78134: }
! 78135: }
! 78136: }
! 78137: if( pCol->iSorterColumn<0 ){
! 78138: pCol->iSorterColumn = pAggInfo->nSortingColumn++;
! 78139: }
! 78140: }
! 78141: /* There is now an entry for pExpr in pAggInfo->aCol[] (either
! 78142: ** because it was there before or because we just created it).
! 78143: ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
! 78144: ** pAggInfo->aCol[] entry.
! 78145: */
! 78146: ExprSetIrreducible(pExpr);
! 78147: pExpr->pAggInfo = pAggInfo;
! 78148: pExpr->op = TK_AGG_COLUMN;
! 78149: pExpr->iAgg = (i16)k;
! 78150: break;
! 78151: } /* endif pExpr->iTable==pItem->iCursor */
! 78152: } /* end loop over pSrcList */
! 78153: }
! 78154: return WRC_Prune;
! 78155: }
! 78156: case TK_AGG_FUNCTION: {
! 78157: /* The pNC->nDepth==0 test causes aggregate functions in subqueries
! 78158: ** to be ignored */
! 78159: if( pNC->nDepth==0 ){
! 78160: /* Check to see if pExpr is a duplicate of another aggregate
! 78161: ** function that is already in the pAggInfo structure
! 78162: */
! 78163: struct AggInfo_func *pItem = pAggInfo->aFunc;
! 78164: for(i=0; i<pAggInfo->nFunc; i++, pItem++){
! 78165: if( sqlite3ExprCompare(pItem->pExpr, pExpr)==0 ){
! 78166: break;
! 78167: }
! 78168: }
! 78169: if( i>=pAggInfo->nFunc ){
! 78170: /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
! 78171: */
! 78172: u8 enc = ENC(pParse->db);
! 78173: i = addAggInfoFunc(pParse->db, pAggInfo);
! 78174: if( i>=0 ){
! 78175: assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
! 78176: pItem = &pAggInfo->aFunc[i];
! 78177: pItem->pExpr = pExpr;
! 78178: pItem->iMem = ++pParse->nMem;
! 78179: assert( !ExprHasProperty(pExpr, EP_IntValue) );
! 78180: pItem->pFunc = sqlite3FindFunction(pParse->db,
! 78181: pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
! 78182: pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
! 78183: if( pExpr->flags & EP_Distinct ){
! 78184: pItem->iDistinct = pParse->nTab++;
! 78185: }else{
! 78186: pItem->iDistinct = -1;
! 78187: }
! 78188: }
! 78189: }
! 78190: /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
! 78191: */
! 78192: assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
! 78193: ExprSetIrreducible(pExpr);
! 78194: pExpr->iAgg = (i16)i;
! 78195: pExpr->pAggInfo = pAggInfo;
! 78196: return WRC_Prune;
! 78197: }
! 78198: }
! 78199: }
! 78200: return WRC_Continue;
! 78201: }
! 78202: static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
! 78203: NameContext *pNC = pWalker->u.pNC;
! 78204: if( pNC->nDepth==0 ){
! 78205: pNC->nDepth++;
! 78206: sqlite3WalkSelect(pWalker, pSelect);
! 78207: pNC->nDepth--;
! 78208: return WRC_Prune;
! 78209: }else{
! 78210: return WRC_Continue;
! 78211: }
! 78212: }
! 78213:
! 78214: /*
! 78215: ** Analyze the given expression looking for aggregate functions and
! 78216: ** for variables that need to be added to the pParse->aAgg[] array.
! 78217: ** Make additional entries to the pParse->aAgg[] array as necessary.
! 78218: **
! 78219: ** This routine should only be called after the expression has been
! 78220: ** analyzed by sqlite3ResolveExprNames().
! 78221: */
! 78222: SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
! 78223: Walker w;
! 78224: w.xExprCallback = analyzeAggregate;
! 78225: w.xSelectCallback = analyzeAggregatesInSelect;
! 78226: w.u.pNC = pNC;
! 78227: assert( pNC->pSrcList!=0 );
! 78228: sqlite3WalkExpr(&w, pExpr);
! 78229: }
! 78230:
! 78231: /*
! 78232: ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
! 78233: ** expression list. Return the number of errors.
! 78234: **
! 78235: ** If an error is found, the analysis is cut short.
! 78236: */
! 78237: SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
! 78238: struct ExprList_item *pItem;
! 78239: int i;
! 78240: if( pList ){
! 78241: for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
! 78242: sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
! 78243: }
! 78244: }
! 78245: }
! 78246:
! 78247: /*
! 78248: ** Allocate a single new register for use to hold some intermediate result.
! 78249: */
! 78250: SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
! 78251: if( pParse->nTempReg==0 ){
! 78252: return ++pParse->nMem;
! 78253: }
! 78254: return pParse->aTempReg[--pParse->nTempReg];
! 78255: }
! 78256:
! 78257: /*
! 78258: ** Deallocate a register, making available for reuse for some other
! 78259: ** purpose.
! 78260: **
! 78261: ** If a register is currently being used by the column cache, then
! 78262: ** the dallocation is deferred until the column cache line that uses
! 78263: ** the register becomes stale.
! 78264: */
! 78265: SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
! 78266: if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
! 78267: int i;
! 78268: struct yColCache *p;
! 78269: for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
! 78270: if( p->iReg==iReg ){
! 78271: p->tempReg = 1;
! 78272: return;
! 78273: }
! 78274: }
! 78275: pParse->aTempReg[pParse->nTempReg++] = iReg;
! 78276: }
! 78277: }
! 78278:
! 78279: /*
! 78280: ** Allocate or deallocate a block of nReg consecutive registers
! 78281: */
! 78282: SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
! 78283: int i, n;
! 78284: i = pParse->iRangeReg;
! 78285: n = pParse->nRangeReg;
! 78286: if( nReg<=n ){
! 78287: assert( !usedAsColumnCache(pParse, i, i+n-1) );
! 78288: pParse->iRangeReg += nReg;
! 78289: pParse->nRangeReg -= nReg;
! 78290: }else{
! 78291: i = pParse->nMem+1;
! 78292: pParse->nMem += nReg;
! 78293: }
! 78294: return i;
! 78295: }
! 78296: SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
! 78297: sqlite3ExprCacheRemove(pParse, iReg, nReg);
! 78298: if( nReg>pParse->nRangeReg ){
! 78299: pParse->nRangeReg = nReg;
! 78300: pParse->iRangeReg = iReg;
! 78301: }
! 78302: }
! 78303:
! 78304: /*
! 78305: ** Mark all temporary registers as being unavailable for reuse.
! 78306: */
! 78307: SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
! 78308: pParse->nTempReg = 0;
! 78309: pParse->nRangeReg = 0;
! 78310: }
! 78311:
! 78312: /************** End of expr.c ************************************************/
! 78313: /************** Begin file alter.c *******************************************/
! 78314: /*
! 78315: ** 2005 February 15
! 78316: **
! 78317: ** The author disclaims copyright to this source code. In place of
! 78318: ** a legal notice, here is a blessing:
! 78319: **
! 78320: ** May you do good and not evil.
! 78321: ** May you find forgiveness for yourself and forgive others.
! 78322: ** May you share freely, never taking more than you give.
! 78323: **
! 78324: *************************************************************************
! 78325: ** This file contains C code routines that used to generate VDBE code
! 78326: ** that implements the ALTER TABLE command.
! 78327: */
! 78328:
! 78329: /*
! 78330: ** The code in this file only exists if we are not omitting the
! 78331: ** ALTER TABLE logic from the build.
! 78332: */
! 78333: #ifndef SQLITE_OMIT_ALTERTABLE
! 78334:
! 78335:
! 78336: /*
! 78337: ** This function is used by SQL generated to implement the
! 78338: ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
! 78339: ** CREATE INDEX command. The second is a table name. The table name in
! 78340: ** the CREATE TABLE or CREATE INDEX statement is replaced with the third
! 78341: ** argument and the result returned. Examples:
! 78342: **
! 78343: ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
! 78344: ** -> 'CREATE TABLE def(a, b, c)'
! 78345: **
! 78346: ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
! 78347: ** -> 'CREATE INDEX i ON def(a, b, c)'
! 78348: */
! 78349: static void renameTableFunc(
! 78350: sqlite3_context *context,
! 78351: int NotUsed,
! 78352: sqlite3_value **argv
! 78353: ){
! 78354: unsigned char const *zSql = sqlite3_value_text(argv[0]);
! 78355: unsigned char const *zTableName = sqlite3_value_text(argv[1]);
! 78356:
! 78357: int token;
! 78358: Token tname;
! 78359: unsigned char const *zCsr = zSql;
! 78360: int len = 0;
! 78361: char *zRet;
! 78362:
! 78363: sqlite3 *db = sqlite3_context_db_handle(context);
! 78364:
! 78365: UNUSED_PARAMETER(NotUsed);
! 78366:
! 78367: /* The principle used to locate the table name in the CREATE TABLE
! 78368: ** statement is that the table name is the first non-space token that
! 78369: ** is immediately followed by a TK_LP or TK_USING token.
! 78370: */
! 78371: if( zSql ){
! 78372: do {
! 78373: if( !*zCsr ){
! 78374: /* Ran out of input before finding an opening bracket. Return NULL. */
! 78375: return;
! 78376: }
! 78377:
! 78378: /* Store the token that zCsr points to in tname. */
! 78379: tname.z = (char*)zCsr;
! 78380: tname.n = len;
! 78381:
! 78382: /* Advance zCsr to the next token. Store that token type in 'token',
! 78383: ** and its length in 'len' (to be used next iteration of this loop).
! 78384: */
! 78385: do {
! 78386: zCsr += len;
! 78387: len = sqlite3GetToken(zCsr, &token);
! 78388: } while( token==TK_SPACE );
! 78389: assert( len>0 );
! 78390: } while( token!=TK_LP && token!=TK_USING );
! 78391:
! 78392: zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql,
! 78393: zTableName, tname.z+tname.n);
! 78394: sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
! 78395: }
! 78396: }
! 78397:
! 78398: /*
! 78399: ** This C function implements an SQL user function that is used by SQL code
! 78400: ** generated by the ALTER TABLE ... RENAME command to modify the definition
! 78401: ** of any foreign key constraints that use the table being renamed as the
! 78402: ** parent table. It is passed three arguments:
! 78403: **
! 78404: ** 1) The complete text of the CREATE TABLE statement being modified,
! 78405: ** 2) The old name of the table being renamed, and
! 78406: ** 3) The new name of the table being renamed.
! 78407: **
! 78408: ** It returns the new CREATE TABLE statement. For example:
! 78409: **
! 78410: ** sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
! 78411: ** -> 'CREATE TABLE t1(a REFERENCES t3)'
! 78412: */
! 78413: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 78414: static void renameParentFunc(
! 78415: sqlite3_context *context,
! 78416: int NotUsed,
! 78417: sqlite3_value **argv
! 78418: ){
! 78419: sqlite3 *db = sqlite3_context_db_handle(context);
! 78420: char *zOutput = 0;
! 78421: char *zResult;
! 78422: unsigned char const *zInput = sqlite3_value_text(argv[0]);
! 78423: unsigned char const *zOld = sqlite3_value_text(argv[1]);
! 78424: unsigned char const *zNew = sqlite3_value_text(argv[2]);
! 78425:
! 78426: unsigned const char *z; /* Pointer to token */
! 78427: int n; /* Length of token z */
! 78428: int token; /* Type of token */
! 78429:
! 78430: UNUSED_PARAMETER(NotUsed);
! 78431: for(z=zInput; *z; z=z+n){
! 78432: n = sqlite3GetToken(z, &token);
! 78433: if( token==TK_REFERENCES ){
! 78434: char *zParent;
! 78435: do {
! 78436: z += n;
! 78437: n = sqlite3GetToken(z, &token);
! 78438: }while( token==TK_SPACE );
! 78439:
! 78440: zParent = sqlite3DbStrNDup(db, (const char *)z, n);
! 78441: if( zParent==0 ) break;
! 78442: sqlite3Dequote(zParent);
! 78443: if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
! 78444: char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"",
! 78445: (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
! 78446: );
! 78447: sqlite3DbFree(db, zOutput);
! 78448: zOutput = zOut;
! 78449: zInput = &z[n];
! 78450: }
! 78451: sqlite3DbFree(db, zParent);
! 78452: }
! 78453: }
! 78454:
! 78455: zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput),
! 78456: sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
! 78457: sqlite3DbFree(db, zOutput);
! 78458: }
! 78459: #endif
! 78460:
! 78461: #ifndef SQLITE_OMIT_TRIGGER
! 78462: /* This function is used by SQL generated to implement the
! 78463: ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
! 78464: ** statement. The second is a table name. The table name in the CREATE
! 78465: ** TRIGGER statement is replaced with the third argument and the result
! 78466: ** returned. This is analagous to renameTableFunc() above, except for CREATE
! 78467: ** TRIGGER, not CREATE INDEX and CREATE TABLE.
! 78468: */
! 78469: static void renameTriggerFunc(
! 78470: sqlite3_context *context,
! 78471: int NotUsed,
! 78472: sqlite3_value **argv
! 78473: ){
! 78474: unsigned char const *zSql = sqlite3_value_text(argv[0]);
! 78475: unsigned char const *zTableName = sqlite3_value_text(argv[1]);
! 78476:
! 78477: int token;
! 78478: Token tname;
! 78479: int dist = 3;
! 78480: unsigned char const *zCsr = zSql;
! 78481: int len = 0;
! 78482: char *zRet;
! 78483: sqlite3 *db = sqlite3_context_db_handle(context);
! 78484:
! 78485: UNUSED_PARAMETER(NotUsed);
! 78486:
! 78487: /* The principle used to locate the table name in the CREATE TRIGGER
! 78488: ** statement is that the table name is the first token that is immediatedly
! 78489: ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
! 78490: ** of TK_WHEN, TK_BEGIN or TK_FOR.
! 78491: */
! 78492: if( zSql ){
! 78493: do {
! 78494:
! 78495: if( !*zCsr ){
! 78496: /* Ran out of input before finding the table name. Return NULL. */
! 78497: return;
! 78498: }
! 78499:
! 78500: /* Store the token that zCsr points to in tname. */
! 78501: tname.z = (char*)zCsr;
! 78502: tname.n = len;
! 78503:
! 78504: /* Advance zCsr to the next token. Store that token type in 'token',
! 78505: ** and its length in 'len' (to be used next iteration of this loop).
! 78506: */
! 78507: do {
! 78508: zCsr += len;
! 78509: len = sqlite3GetToken(zCsr, &token);
! 78510: }while( token==TK_SPACE );
! 78511: assert( len>0 );
! 78512:
! 78513: /* Variable 'dist' stores the number of tokens read since the most
! 78514: ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
! 78515: ** token is read and 'dist' equals 2, the condition stated above
! 78516: ** to be met.
! 78517: **
! 78518: ** Note that ON cannot be a database, table or column name, so
! 78519: ** there is no need to worry about syntax like
! 78520: ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
! 78521: */
! 78522: dist++;
! 78523: if( token==TK_DOT || token==TK_ON ){
! 78524: dist = 0;
! 78525: }
! 78526: } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
! 78527:
! 78528: /* Variable tname now contains the token that is the old table-name
! 78529: ** in the CREATE TRIGGER statement.
! 78530: */
! 78531: zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql,
! 78532: zTableName, tname.z+tname.n);
! 78533: sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
! 78534: }
! 78535: }
! 78536: #endif /* !SQLITE_OMIT_TRIGGER */
! 78537:
! 78538: /*
! 78539: ** Register built-in functions used to help implement ALTER TABLE
! 78540: */
! 78541: SQLITE_PRIVATE void sqlite3AlterFunctions(void){
! 78542: static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
! 78543: FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc),
! 78544: #ifndef SQLITE_OMIT_TRIGGER
! 78545: FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
! 78546: #endif
! 78547: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 78548: FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc),
! 78549: #endif
! 78550: };
! 78551: int i;
! 78552: FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
! 78553: FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
! 78554:
! 78555: for(i=0; i<ArraySize(aAlterTableFuncs); i++){
! 78556: sqlite3FuncDefInsert(pHash, &aFunc[i]);
! 78557: }
! 78558: }
! 78559:
! 78560: /*
! 78561: ** This function is used to create the text of expressions of the form:
! 78562: **
! 78563: ** name=<constant1> OR name=<constant2> OR ...
! 78564: **
! 78565: ** If argument zWhere is NULL, then a pointer string containing the text
! 78566: ** "name=<constant>" is returned, where <constant> is the quoted version
! 78567: ** of the string passed as argument zConstant. The returned buffer is
! 78568: ** allocated using sqlite3DbMalloc(). It is the responsibility of the
! 78569: ** caller to ensure that it is eventually freed.
! 78570: **
! 78571: ** If argument zWhere is not NULL, then the string returned is
! 78572: ** "<where> OR name=<constant>", where <where> is the contents of zWhere.
! 78573: ** In this case zWhere is passed to sqlite3DbFree() before returning.
! 78574: **
! 78575: */
! 78576: static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
! 78577: char *zNew;
! 78578: if( !zWhere ){
! 78579: zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
! 78580: }else{
! 78581: zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
! 78582: sqlite3DbFree(db, zWhere);
! 78583: }
! 78584: return zNew;
! 78585: }
! 78586:
! 78587: #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
! 78588: /*
! 78589: ** Generate the text of a WHERE expression which can be used to select all
! 78590: ** tables that have foreign key constraints that refer to table pTab (i.e.
! 78591: ** constraints for which pTab is the parent table) from the sqlite_master
! 78592: ** table.
! 78593: */
! 78594: static char *whereForeignKeys(Parse *pParse, Table *pTab){
! 78595: FKey *p;
! 78596: char *zWhere = 0;
! 78597: for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
! 78598: zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
! 78599: }
! 78600: return zWhere;
! 78601: }
! 78602: #endif
! 78603:
! 78604: /*
! 78605: ** Generate the text of a WHERE expression which can be used to select all
! 78606: ** temporary triggers on table pTab from the sqlite_temp_master table. If
! 78607: ** table pTab has no temporary triggers, or is itself stored in the
! 78608: ** temporary database, NULL is returned.
! 78609: */
! 78610: static char *whereTempTriggers(Parse *pParse, Table *pTab){
! 78611: Trigger *pTrig;
! 78612: char *zWhere = 0;
! 78613: const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
! 78614:
! 78615: /* If the table is not located in the temp-db (in which case NULL is
! 78616: ** returned, loop through the tables list of triggers. For each trigger
! 78617: ** that is not part of the temp-db schema, add a clause to the WHERE
! 78618: ** expression being built up in zWhere.
! 78619: */
! 78620: if( pTab->pSchema!=pTempSchema ){
! 78621: sqlite3 *db = pParse->db;
! 78622: for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
! 78623: if( pTrig->pSchema==pTempSchema ){
! 78624: zWhere = whereOrName(db, zWhere, pTrig->zName);
! 78625: }
! 78626: }
! 78627: }
! 78628: if( zWhere ){
! 78629: char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
! 78630: sqlite3DbFree(pParse->db, zWhere);
! 78631: zWhere = zNew;
! 78632: }
! 78633: return zWhere;
! 78634: }
! 78635:
! 78636: /*
! 78637: ** Generate code to drop and reload the internal representation of table
! 78638: ** pTab from the database, including triggers and temporary triggers.
! 78639: ** Argument zName is the name of the table in the database schema at
! 78640: ** the time the generated code is executed. This can be different from
! 78641: ** pTab->zName if this function is being called to code part of an
! 78642: ** "ALTER TABLE RENAME TO" statement.
! 78643: */
! 78644: static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
! 78645: Vdbe *v;
! 78646: char *zWhere;
! 78647: int iDb; /* Index of database containing pTab */
! 78648: #ifndef SQLITE_OMIT_TRIGGER
! 78649: Trigger *pTrig;
! 78650: #endif
! 78651:
! 78652: v = sqlite3GetVdbe(pParse);
! 78653: if( NEVER(v==0) ) return;
! 78654: assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
! 78655: iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
! 78656: assert( iDb>=0 );
! 78657:
! 78658: #ifndef SQLITE_OMIT_TRIGGER
! 78659: /* Drop any table triggers from the internal schema. */
! 78660: for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
! 78661: int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
! 78662: assert( iTrigDb==iDb || iTrigDb==1 );
! 78663: sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
! 78664: }
! 78665: #endif
! 78666:
! 78667: /* Drop the table and index from the internal schema. */
! 78668: sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
! 78669:
! 78670: /* Reload the table, index and permanent trigger schemas. */
! 78671: zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
! 78672: if( !zWhere ) return;
! 78673: sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
! 78674:
! 78675: #ifndef SQLITE_OMIT_TRIGGER
! 78676: /* Now, if the table is not stored in the temp database, reload any temp
! 78677: ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
! 78678: */
! 78679: if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
! 78680: sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
! 78681: }
! 78682: #endif
! 78683: }
! 78684:
! 78685: /*
! 78686: ** Parameter zName is the name of a table that is about to be altered
! 78687: ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
! 78688: ** If the table is a system table, this function leaves an error message
! 78689: ** in pParse->zErr (system tables may not be altered) and returns non-zero.
! 78690: **
! 78691: ** Or, if zName is not a system table, zero is returned.
! 78692: */
! 78693: static int isSystemTable(Parse *pParse, const char *zName){
! 78694: if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
! 78695: sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
! 78696: return 1;
! 78697: }
! 78698: return 0;
! 78699: }
! 78700:
! 78701: /*
! 78702: ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
! 78703: ** command.
! 78704: */
! 78705: SQLITE_PRIVATE void sqlite3AlterRenameTable(
! 78706: Parse *pParse, /* Parser context. */
! 78707: SrcList *pSrc, /* The table to rename. */
! 78708: Token *pName /* The new table name. */
! 78709: ){
! 78710: int iDb; /* Database that contains the table */
! 78711: char *zDb; /* Name of database iDb */
! 78712: Table *pTab; /* Table being renamed */
! 78713: char *zName = 0; /* NULL-terminated version of pName */
! 78714: sqlite3 *db = pParse->db; /* Database connection */
! 78715: int nTabName; /* Number of UTF-8 characters in zTabName */
! 78716: const char *zTabName; /* Original name of the table */
! 78717: Vdbe *v;
! 78718: #ifndef SQLITE_OMIT_TRIGGER
! 78719: char *zWhere = 0; /* Where clause to locate temp triggers */
! 78720: #endif
! 78721: VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
! 78722: int savedDbFlags; /* Saved value of db->flags */
! 78723:
! 78724: savedDbFlags = db->flags;
! 78725: if( NEVER(db->mallocFailed) ) goto exit_rename_table;
! 78726: assert( pSrc->nSrc==1 );
! 78727: assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
! 78728:
! 78729: pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
! 78730: if( !pTab ) goto exit_rename_table;
! 78731: iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
! 78732: zDb = db->aDb[iDb].zName;
! 78733: db->flags |= SQLITE_PreferBuiltin;
! 78734:
! 78735: /* Get a NULL terminated version of the new table name. */
! 78736: zName = sqlite3NameFromToken(db, pName);
! 78737: if( !zName ) goto exit_rename_table;
! 78738:
! 78739: /* Check that a table or index named 'zName' does not already exist
! 78740: ** in database iDb. If so, this is an error.
! 78741: */
! 78742: if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
! 78743: sqlite3ErrorMsg(pParse,
! 78744: "there is already another table or index with this name: %s", zName);
! 78745: goto exit_rename_table;
! 78746: }
! 78747:
! 78748: /* Make sure it is not a system table being altered, or a reserved name
! 78749: ** that the table is being renamed to.
! 78750: */
! 78751: if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
! 78752: goto exit_rename_table;
! 78753: }
! 78754: if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
! 78755: exit_rename_table;
! 78756: }
! 78757:
! 78758: #ifndef SQLITE_OMIT_VIEW
! 78759: if( pTab->pSelect ){
! 78760: sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
! 78761: goto exit_rename_table;
! 78762: }
! 78763: #endif
! 78764:
! 78765: #ifndef SQLITE_OMIT_AUTHORIZATION
! 78766: /* Invoke the authorization callback. */
! 78767: if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
! 78768: goto exit_rename_table;
! 78769: }
! 78770: #endif
! 78771:
! 78772: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 78773: if( sqlite3ViewGetColumnNames(pParse, pTab) ){
! 78774: goto exit_rename_table;
! 78775: }
! 78776: if( IsVirtual(pTab) ){
! 78777: pVTab = sqlite3GetVTable(db, pTab);
! 78778: if( pVTab->pVtab->pModule->xRename==0 ){
! 78779: pVTab = 0;
! 78780: }
! 78781: }
! 78782: #endif
! 78783:
! 78784: /* Begin a transaction and code the VerifyCookie for database iDb.
! 78785: ** Then modify the schema cookie (since the ALTER TABLE modifies the
! 78786: ** schema). Open a statement transaction if the table is a virtual
! 78787: ** table.
! 78788: */
! 78789: v = sqlite3GetVdbe(pParse);
! 78790: if( v==0 ){
! 78791: goto exit_rename_table;
! 78792: }
! 78793: sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
! 78794: sqlite3ChangeCookie(pParse, iDb);
! 78795:
! 78796: /* If this is a virtual table, invoke the xRename() function if
! 78797: ** one is defined. The xRename() callback will modify the names
! 78798: ** of any resources used by the v-table implementation (including other
! 78799: ** SQLite tables) that are identified by the name of the virtual table.
! 78800: */
! 78801: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 78802: if( pVTab ){
! 78803: int i = ++pParse->nMem;
! 78804: sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
! 78805: sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
! 78806: sqlite3MayAbort(pParse);
! 78807: }
! 78808: #endif
! 78809:
! 78810: /* figure out how many UTF-8 characters are in zName */
! 78811: zTabName = pTab->zName;
! 78812: nTabName = sqlite3Utf8CharLen(zTabName, -1);
! 78813:
! 78814: #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
! 78815: if( db->flags&SQLITE_ForeignKeys ){
! 78816: /* If foreign-key support is enabled, rewrite the CREATE TABLE
! 78817: ** statements corresponding to all child tables of foreign key constraints
! 78818: ** for which the renamed table is the parent table. */
! 78819: if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
! 78820: sqlite3NestedParse(pParse,
! 78821: "UPDATE \"%w\".%s SET "
! 78822: "sql = sqlite_rename_parent(sql, %Q, %Q) "
! 78823: "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
! 78824: sqlite3DbFree(db, zWhere);
! 78825: }
! 78826: }
! 78827: #endif
! 78828:
! 78829: /* Modify the sqlite_master table to use the new table name. */
! 78830: sqlite3NestedParse(pParse,
! 78831: "UPDATE %Q.%s SET "
! 78832: #ifdef SQLITE_OMIT_TRIGGER
! 78833: "sql = sqlite_rename_table(sql, %Q), "
! 78834: #else
! 78835: "sql = CASE "
! 78836: "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
! 78837: "ELSE sqlite_rename_table(sql, %Q) END, "
! 78838: #endif
! 78839: "tbl_name = %Q, "
! 78840: "name = CASE "
! 78841: "WHEN type='table' THEN %Q "
! 78842: "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
! 78843: "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
! 78844: "ELSE name END "
! 78845: "WHERE tbl_name=%Q AND "
! 78846: "(type='table' OR type='index' OR type='trigger');",
! 78847: zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
! 78848: #ifndef SQLITE_OMIT_TRIGGER
! 78849: zName,
! 78850: #endif
! 78851: zName, nTabName, zTabName
! 78852: );
! 78853:
! 78854: #ifndef SQLITE_OMIT_AUTOINCREMENT
! 78855: /* If the sqlite_sequence table exists in this database, then update
! 78856: ** it with the new table name.
! 78857: */
! 78858: if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
! 78859: sqlite3NestedParse(pParse,
! 78860: "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
! 78861: zDb, zName, pTab->zName);
! 78862: }
! 78863: #endif
! 78864:
! 78865: #ifndef SQLITE_OMIT_TRIGGER
! 78866: /* If there are TEMP triggers on this table, modify the sqlite_temp_master
! 78867: ** table. Don't do this if the table being ALTERed is itself located in
! 78868: ** the temp database.
! 78869: */
! 78870: if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
! 78871: sqlite3NestedParse(pParse,
! 78872: "UPDATE sqlite_temp_master SET "
! 78873: "sql = sqlite_rename_trigger(sql, %Q), "
! 78874: "tbl_name = %Q "
! 78875: "WHERE %s;", zName, zName, zWhere);
! 78876: sqlite3DbFree(db, zWhere);
! 78877: }
! 78878: #endif
! 78879:
! 78880: #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
! 78881: if( db->flags&SQLITE_ForeignKeys ){
! 78882: FKey *p;
! 78883: for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
! 78884: Table *pFrom = p->pFrom;
! 78885: if( pFrom!=pTab ){
! 78886: reloadTableSchema(pParse, p->pFrom, pFrom->zName);
! 78887: }
! 78888: }
! 78889: }
! 78890: #endif
! 78891:
! 78892: /* Drop and reload the internal table schema. */
! 78893: reloadTableSchema(pParse, pTab, zName);
! 78894:
! 78895: exit_rename_table:
! 78896: sqlite3SrcListDelete(db, pSrc);
! 78897: sqlite3DbFree(db, zName);
! 78898: db->flags = savedDbFlags;
! 78899: }
! 78900:
! 78901:
! 78902: /*
! 78903: ** Generate code to make sure the file format number is at least minFormat.
! 78904: ** The generated code will increase the file format number if necessary.
! 78905: */
! 78906: SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
! 78907: Vdbe *v;
! 78908: v = sqlite3GetVdbe(pParse);
! 78909: /* The VDBE should have been allocated before this routine is called.
! 78910: ** If that allocation failed, we would have quit before reaching this
! 78911: ** point */
! 78912: if( ALWAYS(v) ){
! 78913: int r1 = sqlite3GetTempReg(pParse);
! 78914: int r2 = sqlite3GetTempReg(pParse);
! 78915: int j1;
! 78916: sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
! 78917: sqlite3VdbeUsesBtree(v, iDb);
! 78918: sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
! 78919: j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
! 78920: sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
! 78921: sqlite3VdbeJumpHere(v, j1);
! 78922: sqlite3ReleaseTempReg(pParse, r1);
! 78923: sqlite3ReleaseTempReg(pParse, r2);
! 78924: }
! 78925: }
! 78926:
! 78927: /*
! 78928: ** This function is called after an "ALTER TABLE ... ADD" statement
! 78929: ** has been parsed. Argument pColDef contains the text of the new
! 78930: ** column definition.
! 78931: **
! 78932: ** The Table structure pParse->pNewTable was extended to include
! 78933: ** the new column during parsing.
! 78934: */
! 78935: SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
! 78936: Table *pNew; /* Copy of pParse->pNewTable */
! 78937: Table *pTab; /* Table being altered */
! 78938: int iDb; /* Database number */
! 78939: const char *zDb; /* Database name */
! 78940: const char *zTab; /* Table name */
! 78941: char *zCol; /* Null-terminated column definition */
! 78942: Column *pCol; /* The new column */
! 78943: Expr *pDflt; /* Default value for the new column */
! 78944: sqlite3 *db; /* The database connection; */
! 78945:
! 78946: db = pParse->db;
! 78947: if( pParse->nErr || db->mallocFailed ) return;
! 78948: pNew = pParse->pNewTable;
! 78949: assert( pNew );
! 78950:
! 78951: assert( sqlite3BtreeHoldsAllMutexes(db) );
! 78952: iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
! 78953: zDb = db->aDb[iDb].zName;
! 78954: zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
! 78955: pCol = &pNew->aCol[pNew->nCol-1];
! 78956: pDflt = pCol->pDflt;
! 78957: pTab = sqlite3FindTable(db, zTab, zDb);
! 78958: assert( pTab );
! 78959:
! 78960: #ifndef SQLITE_OMIT_AUTHORIZATION
! 78961: /* Invoke the authorization callback. */
! 78962: if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
! 78963: return;
! 78964: }
! 78965: #endif
! 78966:
! 78967: /* If the default value for the new column was specified with a
! 78968: ** literal NULL, then set pDflt to 0. This simplifies checking
! 78969: ** for an SQL NULL default below.
! 78970: */
! 78971: if( pDflt && pDflt->op==TK_NULL ){
! 78972: pDflt = 0;
! 78973: }
! 78974:
! 78975: /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
! 78976: ** If there is a NOT NULL constraint, then the default value for the
! 78977: ** column must not be NULL.
! 78978: */
! 78979: if( pCol->isPrimKey ){
! 78980: sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
! 78981: return;
! 78982: }
! 78983: if( pNew->pIndex ){
! 78984: sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
! 78985: return;
! 78986: }
! 78987: if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
! 78988: sqlite3ErrorMsg(pParse,
! 78989: "Cannot add a REFERENCES column with non-NULL default value");
! 78990: return;
! 78991: }
! 78992: if( pCol->notNull && !pDflt ){
! 78993: sqlite3ErrorMsg(pParse,
! 78994: "Cannot add a NOT NULL column with default value NULL");
! 78995: return;
! 78996: }
! 78997:
! 78998: /* Ensure the default expression is something that sqlite3ValueFromExpr()
! 78999: ** can handle (i.e. not CURRENT_TIME etc.)
! 79000: */
! 79001: if( pDflt ){
! 79002: sqlite3_value *pVal;
! 79003: if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
! 79004: db->mallocFailed = 1;
! 79005: return;
! 79006: }
! 79007: if( !pVal ){
! 79008: sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
! 79009: return;
! 79010: }
! 79011: sqlite3ValueFree(pVal);
! 79012: }
! 79013:
! 79014: /* Modify the CREATE TABLE statement. */
! 79015: zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
! 79016: if( zCol ){
! 79017: char *zEnd = &zCol[pColDef->n-1];
! 79018: int savedDbFlags = db->flags;
! 79019: while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
! 79020: *zEnd-- = '\0';
! 79021: }
! 79022: db->flags |= SQLITE_PreferBuiltin;
! 79023: sqlite3NestedParse(pParse,
! 79024: "UPDATE \"%w\".%s SET "
! 79025: "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
! 79026: "WHERE type = 'table' AND name = %Q",
! 79027: zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
! 79028: zTab
! 79029: );
! 79030: sqlite3DbFree(db, zCol);
! 79031: db->flags = savedDbFlags;
! 79032: }
! 79033:
! 79034: /* If the default value of the new column is NULL, then set the file
! 79035: ** format to 2. If the default value of the new column is not NULL,
! 79036: ** the file format becomes 3.
! 79037: */
! 79038: sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
! 79039:
! 79040: /* Reload the schema of the modified table. */
! 79041: reloadTableSchema(pParse, pTab, pTab->zName);
! 79042: }
! 79043:
! 79044: /*
! 79045: ** This function is called by the parser after the table-name in
! 79046: ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
! 79047: ** pSrc is the full-name of the table being altered.
! 79048: **
! 79049: ** This routine makes a (partial) copy of the Table structure
! 79050: ** for the table being altered and sets Parse.pNewTable to point
! 79051: ** to it. Routines called by the parser as the column definition
! 79052: ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
! 79053: ** the copy. The copy of the Table structure is deleted by tokenize.c
! 79054: ** after parsing is finished.
! 79055: **
! 79056: ** Routine sqlite3AlterFinishAddColumn() will be called to complete
! 79057: ** coding the "ALTER TABLE ... ADD" statement.
! 79058: */
! 79059: SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
! 79060: Table *pNew;
! 79061: Table *pTab;
! 79062: Vdbe *v;
! 79063: int iDb;
! 79064: int i;
! 79065: int nAlloc;
! 79066: sqlite3 *db = pParse->db;
! 79067:
! 79068: /* Look up the table being altered. */
! 79069: assert( pParse->pNewTable==0 );
! 79070: assert( sqlite3BtreeHoldsAllMutexes(db) );
! 79071: if( db->mallocFailed ) goto exit_begin_add_column;
! 79072: pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
! 79073: if( !pTab ) goto exit_begin_add_column;
! 79074:
! 79075: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 79076: if( IsVirtual(pTab) ){
! 79077: sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
! 79078: goto exit_begin_add_column;
! 79079: }
! 79080: #endif
! 79081:
! 79082: /* Make sure this is not an attempt to ALTER a view. */
! 79083: if( pTab->pSelect ){
! 79084: sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
! 79085: goto exit_begin_add_column;
! 79086: }
! 79087: if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
! 79088: goto exit_begin_add_column;
! 79089: }
! 79090:
! 79091: assert( pTab->addColOffset>0 );
! 79092: iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 79093:
! 79094: /* Put a copy of the Table struct in Parse.pNewTable for the
! 79095: ** sqlite3AddColumn() function and friends to modify. But modify
! 79096: ** the name by adding an "sqlite_altertab_" prefix. By adding this
! 79097: ** prefix, we insure that the name will not collide with an existing
! 79098: ** table because user table are not allowed to have the "sqlite_"
! 79099: ** prefix on their name.
! 79100: */
! 79101: pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
! 79102: if( !pNew ) goto exit_begin_add_column;
! 79103: pParse->pNewTable = pNew;
! 79104: pNew->nRef = 1;
! 79105: pNew->nCol = pTab->nCol;
! 79106: assert( pNew->nCol>0 );
! 79107: nAlloc = (((pNew->nCol-1)/8)*8)+8;
! 79108: assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
! 79109: pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
! 79110: pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
! 79111: if( !pNew->aCol || !pNew->zName ){
! 79112: db->mallocFailed = 1;
! 79113: goto exit_begin_add_column;
! 79114: }
! 79115: memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
! 79116: for(i=0; i<pNew->nCol; i++){
! 79117: Column *pCol = &pNew->aCol[i];
! 79118: pCol->zName = sqlite3DbStrDup(db, pCol->zName);
! 79119: pCol->zColl = 0;
! 79120: pCol->zType = 0;
! 79121: pCol->pDflt = 0;
! 79122: pCol->zDflt = 0;
! 79123: }
! 79124: pNew->pSchema = db->aDb[iDb].pSchema;
! 79125: pNew->addColOffset = pTab->addColOffset;
! 79126: pNew->nRef = 1;
! 79127:
! 79128: /* Begin a transaction and increment the schema cookie. */
! 79129: sqlite3BeginWriteOperation(pParse, 0, iDb);
! 79130: v = sqlite3GetVdbe(pParse);
! 79131: if( !v ) goto exit_begin_add_column;
! 79132: sqlite3ChangeCookie(pParse, iDb);
! 79133:
! 79134: exit_begin_add_column:
! 79135: sqlite3SrcListDelete(db, pSrc);
! 79136: return;
! 79137: }
! 79138: #endif /* SQLITE_ALTER_TABLE */
! 79139:
! 79140: /************** End of alter.c ***********************************************/
! 79141: /************** Begin file analyze.c *****************************************/
! 79142: /*
! 79143: ** 2005 July 8
! 79144: **
! 79145: ** The author disclaims copyright to this source code. In place of
! 79146: ** a legal notice, here is a blessing:
! 79147: **
! 79148: ** May you do good and not evil.
! 79149: ** May you find forgiveness for yourself and forgive others.
! 79150: ** May you share freely, never taking more than you give.
! 79151: **
! 79152: *************************************************************************
! 79153: ** This file contains code associated with the ANALYZE command.
! 79154: **
! 79155: ** The ANALYZE command gather statistics about the content of tables
! 79156: ** and indices. These statistics are made available to the query planner
! 79157: ** to help it make better decisions about how to perform queries.
! 79158: **
! 79159: ** The following system tables are or have been supported:
! 79160: **
! 79161: ** CREATE TABLE sqlite_stat1(tbl, idx, stat);
! 79162: ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
! 79163: ** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
! 79164: **
! 79165: ** Additional tables might be added in future releases of SQLite.
! 79166: ** The sqlite_stat2 table is not created or used unless the SQLite version
! 79167: ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
! 79168: ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
! 79169: ** The sqlite_stat2 table is superceded by sqlite_stat3, which is only
! 79170: ** created and used by SQLite versions 3.7.9 and later and with
! 79171: ** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3
! 79172: ** is a superset of sqlite_stat2.
! 79173: **
! 79174: ** Format of sqlite_stat1:
! 79175: **
! 79176: ** There is normally one row per index, with the index identified by the
! 79177: ** name in the idx column. The tbl column is the name of the table to
! 79178: ** which the index belongs. In each such row, the stat column will be
! 79179: ** a string consisting of a list of integers. The first integer in this
! 79180: ** list is the number of rows in the index and in the table. The second
! 79181: ** integer is the average number of rows in the index that have the same
! 79182: ** value in the first column of the index. The third integer is the average
! 79183: ** number of rows in the index that have the same value for the first two
! 79184: ** columns. The N-th integer (for N>1) is the average number of rows in
! 79185: ** the index which have the same value for the first N-1 columns. For
! 79186: ** a K-column index, there will be K+1 integers in the stat column. If
! 79187: ** the index is unique, then the last integer will be 1.
! 79188: **
! 79189: ** The list of integers in the stat column can optionally be followed
! 79190: ** by the keyword "unordered". The "unordered" keyword, if it is present,
! 79191: ** must be separated from the last integer by a single space. If the
! 79192: ** "unordered" keyword is present, then the query planner assumes that
! 79193: ** the index is unordered and will not use the index for a range query.
! 79194: **
! 79195: ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
! 79196: ** column contains a single integer which is the (estimated) number of
! 79197: ** rows in the table identified by sqlite_stat1.tbl.
! 79198: **
! 79199: ** Format of sqlite_stat2:
! 79200: **
! 79201: ** The sqlite_stat2 is only created and is only used if SQLite is compiled
! 79202: ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
! 79203: ** 3.6.18 and 3.7.8. The "stat2" table contains additional information
! 79204: ** about the distribution of keys within an index. The index is identified by
! 79205: ** the "idx" column and the "tbl" column is the name of the table to which
! 79206: ** the index belongs. There are usually 10 rows in the sqlite_stat2
! 79207: ** table for each index.
! 79208: **
! 79209: ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
! 79210: ** inclusive are samples of the left-most key value in the index taken at
! 79211: ** evenly spaced points along the index. Let the number of samples be S
! 79212: ** (10 in the standard build) and let C be the number of rows in the index.
! 79213: ** Then the sampled rows are given by:
! 79214: **
! 79215: ** rownumber = (i*C*2 + C)/(S*2)
! 79216: **
! 79217: ** For i between 0 and S-1. Conceptually, the index space is divided into
! 79218: ** S uniform buckets and the samples are the middle row from each bucket.
! 79219: **
! 79220: ** The format for sqlite_stat2 is recorded here for legacy reference. This
! 79221: ** version of SQLite does not support sqlite_stat2. It neither reads nor
! 79222: ** writes the sqlite_stat2 table. This version of SQLite only supports
! 79223: ** sqlite_stat3.
! 79224: **
! 79225: ** Format for sqlite_stat3:
! 79226: **
! 79227: ** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is
! 79228: ** used to avoid compatibility problems.
! 79229: **
! 79230: ** The format of the sqlite_stat3 table is similar to the format of
! 79231: ** the sqlite_stat2 table. There are multiple entries for each index.
! 79232: ** The idx column names the index and the tbl column is the table of the
! 79233: ** index. If the idx and tbl columns are the same, then the sample is
! 79234: ** of the INTEGER PRIMARY KEY. The sample column is a value taken from
! 79235: ** the left-most column of the index. The nEq column is the approximate
! 79236: ** number of entires in the index whose left-most column exactly matches
! 79237: ** the sample. nLt is the approximate number of entires whose left-most
! 79238: ** column is less than the sample. The nDLt column is the approximate
! 79239: ** number of distinct left-most entries in the index that are less than
! 79240: ** the sample.
! 79241: **
! 79242: ** Future versions of SQLite might change to store a string containing
! 79243: ** multiple integers values in the nDLt column of sqlite_stat3. The first
! 79244: ** integer will be the number of prior index entires that are distinct in
! 79245: ** the left-most column. The second integer will be the number of prior index
! 79246: ** entries that are distinct in the first two columns. The third integer
! 79247: ** will be the number of prior index entries that are distinct in the first
! 79248: ** three columns. And so forth. With that extension, the nDLt field is
! 79249: ** similar in function to the sqlite_stat1.stat field.
! 79250: **
! 79251: ** There can be an arbitrary number of sqlite_stat3 entries per index.
! 79252: ** The ANALYZE command will typically generate sqlite_stat3 tables
! 79253: ** that contain between 10 and 40 samples which are distributed across
! 79254: ** the key space, though not uniformly, and which include samples with
! 79255: ** largest possible nEq values.
! 79256: */
! 79257: #ifndef SQLITE_OMIT_ANALYZE
! 79258:
! 79259: /*
! 79260: ** This routine generates code that opens the sqlite_stat1 table for
! 79261: ** writing with cursor iStatCur. If the library was built with the
! 79262: ** SQLITE_ENABLE_STAT3 macro defined, then the sqlite_stat3 table is
! 79263: ** opened for writing using cursor (iStatCur+1)
! 79264: **
! 79265: ** If the sqlite_stat1 tables does not previously exist, it is created.
! 79266: ** Similarly, if the sqlite_stat3 table does not exist and the library
! 79267: ** is compiled with SQLITE_ENABLE_STAT3 defined, it is created.
! 79268: **
! 79269: ** Argument zWhere may be a pointer to a buffer containing a table name,
! 79270: ** or it may be a NULL pointer. If it is not NULL, then all entries in
! 79271: ** the sqlite_stat1 and (if applicable) sqlite_stat3 tables associated
! 79272: ** with the named table are deleted. If zWhere==0, then code is generated
! 79273: ** to delete all stat table entries.
! 79274: */
! 79275: static void openStatTable(
! 79276: Parse *pParse, /* Parsing context */
! 79277: int iDb, /* The database we are looking in */
! 79278: int iStatCur, /* Open the sqlite_stat1 table on this cursor */
! 79279: const char *zWhere, /* Delete entries for this table or index */
! 79280: const char *zWhereType /* Either "tbl" or "idx" */
! 79281: ){
! 79282: static const struct {
! 79283: const char *zName;
! 79284: const char *zCols;
! 79285: } aTable[] = {
! 79286: { "sqlite_stat1", "tbl,idx,stat" },
! 79287: #ifdef SQLITE_ENABLE_STAT3
! 79288: { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
! 79289: #endif
! 79290: };
! 79291:
! 79292: int aRoot[] = {0, 0};
! 79293: u8 aCreateTbl[] = {0, 0};
! 79294:
! 79295: int i;
! 79296: sqlite3 *db = pParse->db;
! 79297: Db *pDb;
! 79298: Vdbe *v = sqlite3GetVdbe(pParse);
! 79299: if( v==0 ) return;
! 79300: assert( sqlite3BtreeHoldsAllMutexes(db) );
! 79301: assert( sqlite3VdbeDb(v)==db );
! 79302: pDb = &db->aDb[iDb];
! 79303:
! 79304: /* Create new statistic tables if they do not exist, or clear them
! 79305: ** if they do already exist.
! 79306: */
! 79307: for(i=0; i<ArraySize(aTable); i++){
! 79308: const char *zTab = aTable[i].zName;
! 79309: Table *pStat;
! 79310: if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
! 79311: /* The sqlite_stat[12] table does not exist. Create it. Note that a
! 79312: ** side-effect of the CREATE TABLE statement is to leave the rootpage
! 79313: ** of the new table in register pParse->regRoot. This is important
! 79314: ** because the OpenWrite opcode below will be needing it. */
! 79315: sqlite3NestedParse(pParse,
! 79316: "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
! 79317: );
! 79318: aRoot[i] = pParse->regRoot;
! 79319: aCreateTbl[i] = 1;
! 79320: }else{
! 79321: /* The table already exists. If zWhere is not NULL, delete all entries
! 79322: ** associated with the table zWhere. If zWhere is NULL, delete the
! 79323: ** entire contents of the table. */
! 79324: aRoot[i] = pStat->tnum;
! 79325: sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
! 79326: if( zWhere ){
! 79327: sqlite3NestedParse(pParse,
! 79328: "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
! 79329: );
! 79330: }else{
! 79331: /* The sqlite_stat[12] table already exists. Delete all rows. */
! 79332: sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
! 79333: }
! 79334: }
! 79335: }
! 79336:
! 79337: /* Open the sqlite_stat[13] tables for writing. */
! 79338: for(i=0; i<ArraySize(aTable); i++){
! 79339: sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
! 79340: sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
! 79341: sqlite3VdbeChangeP5(v, aCreateTbl[i]);
! 79342: }
! 79343: }
! 79344:
! 79345: /*
! 79346: ** Recommended number of samples for sqlite_stat3
! 79347: */
! 79348: #ifndef SQLITE_STAT3_SAMPLES
! 79349: # define SQLITE_STAT3_SAMPLES 24
! 79350: #endif
! 79351:
! 79352: /*
! 79353: ** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
! 79354: ** share an instance of the following structure to hold their state
! 79355: ** information.
! 79356: */
! 79357: typedef struct Stat3Accum Stat3Accum;
! 79358: struct Stat3Accum {
! 79359: tRowcnt nRow; /* Number of rows in the entire table */
! 79360: tRowcnt nPSample; /* How often to do a periodic sample */
! 79361: int iMin; /* Index of entry with minimum nEq and hash */
! 79362: int mxSample; /* Maximum number of samples to accumulate */
! 79363: int nSample; /* Current number of samples */
! 79364: u32 iPrn; /* Pseudo-random number used for sampling */
! 79365: struct Stat3Sample {
! 79366: i64 iRowid; /* Rowid in main table of the key */
! 79367: tRowcnt nEq; /* sqlite_stat3.nEq */
! 79368: tRowcnt nLt; /* sqlite_stat3.nLt */
! 79369: tRowcnt nDLt; /* sqlite_stat3.nDLt */
! 79370: u8 isPSample; /* True if a periodic sample */
! 79371: u32 iHash; /* Tiebreaker hash */
! 79372: } *a; /* An array of samples */
! 79373: };
! 79374:
! 79375: #ifdef SQLITE_ENABLE_STAT3
! 79376: /*
! 79377: ** Implementation of the stat3_init(C,S) SQL function. The two parameters
! 79378: ** are the number of rows in the table or index (C) and the number of samples
! 79379: ** to accumulate (S).
! 79380: **
! 79381: ** This routine allocates the Stat3Accum object.
! 79382: **
! 79383: ** The return value is the Stat3Accum object (P).
! 79384: */
! 79385: static void stat3Init(
! 79386: sqlite3_context *context,
! 79387: int argc,
! 79388: sqlite3_value **argv
! 79389: ){
! 79390: Stat3Accum *p;
! 79391: tRowcnt nRow;
! 79392: int mxSample;
! 79393: int n;
! 79394:
! 79395: UNUSED_PARAMETER(argc);
! 79396: nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
! 79397: mxSample = sqlite3_value_int(argv[1]);
! 79398: n = sizeof(*p) + sizeof(p->a[0])*mxSample;
! 79399: p = sqlite3_malloc( n );
! 79400: if( p==0 ){
! 79401: sqlite3_result_error_nomem(context);
! 79402: return;
! 79403: }
! 79404: memset(p, 0, n);
! 79405: p->a = (struct Stat3Sample*)&p[1];
! 79406: p->nRow = nRow;
! 79407: p->mxSample = mxSample;
! 79408: p->nPSample = p->nRow/(mxSample/3+1) + 1;
! 79409: sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
! 79410: sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
! 79411: }
! 79412: static const FuncDef stat3InitFuncdef = {
! 79413: 2, /* nArg */
! 79414: SQLITE_UTF8, /* iPrefEnc */
! 79415: 0, /* flags */
! 79416: 0, /* pUserData */
! 79417: 0, /* pNext */
! 79418: stat3Init, /* xFunc */
! 79419: 0, /* xStep */
! 79420: 0, /* xFinalize */
! 79421: "stat3_init", /* zName */
! 79422: 0, /* pHash */
! 79423: 0 /* pDestructor */
! 79424: };
! 79425:
! 79426:
! 79427: /*
! 79428: ** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The
! 79429: ** arguments describe a single key instance. This routine makes the
! 79430: ** decision about whether or not to retain this key for the sqlite_stat3
! 79431: ** table.
! 79432: **
! 79433: ** The return value is NULL.
! 79434: */
! 79435: static void stat3Push(
! 79436: sqlite3_context *context,
! 79437: int argc,
! 79438: sqlite3_value **argv
! 79439: ){
! 79440: Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
! 79441: tRowcnt nEq = sqlite3_value_int64(argv[0]);
! 79442: tRowcnt nLt = sqlite3_value_int64(argv[1]);
! 79443: tRowcnt nDLt = sqlite3_value_int64(argv[2]);
! 79444: i64 rowid = sqlite3_value_int64(argv[3]);
! 79445: u8 isPSample = 0;
! 79446: u8 doInsert = 0;
! 79447: int iMin = p->iMin;
! 79448: struct Stat3Sample *pSample;
! 79449: int i;
! 79450: u32 h;
! 79451:
! 79452: UNUSED_PARAMETER(context);
! 79453: UNUSED_PARAMETER(argc);
! 79454: if( nEq==0 ) return;
! 79455: h = p->iPrn = p->iPrn*1103515245 + 12345;
! 79456: if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
! 79457: doInsert = isPSample = 1;
! 79458: }else if( p->nSample<p->mxSample ){
! 79459: doInsert = 1;
! 79460: }else{
! 79461: if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
! 79462: doInsert = 1;
! 79463: }
! 79464: }
! 79465: if( !doInsert ) return;
! 79466: if( p->nSample==p->mxSample ){
! 79467: assert( p->nSample - iMin - 1 >= 0 );
! 79468: memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
! 79469: pSample = &p->a[p->nSample-1];
! 79470: }else{
! 79471: pSample = &p->a[p->nSample++];
! 79472: }
! 79473: pSample->iRowid = rowid;
! 79474: pSample->nEq = nEq;
! 79475: pSample->nLt = nLt;
! 79476: pSample->nDLt = nDLt;
! 79477: pSample->iHash = h;
! 79478: pSample->isPSample = isPSample;
! 79479:
! 79480: /* Find the new minimum */
! 79481: if( p->nSample==p->mxSample ){
! 79482: pSample = p->a;
! 79483: i = 0;
! 79484: while( pSample->isPSample ){
! 79485: i++;
! 79486: pSample++;
! 79487: assert( i<p->nSample );
! 79488: }
! 79489: nEq = pSample->nEq;
! 79490: h = pSample->iHash;
! 79491: iMin = i;
! 79492: for(i++, pSample++; i<p->nSample; i++, pSample++){
! 79493: if( pSample->isPSample ) continue;
! 79494: if( pSample->nEq<nEq
! 79495: || (pSample->nEq==nEq && pSample->iHash<h)
! 79496: ){
! 79497: iMin = i;
! 79498: nEq = pSample->nEq;
! 79499: h = pSample->iHash;
! 79500: }
! 79501: }
! 79502: p->iMin = iMin;
! 79503: }
! 79504: }
! 79505: static const FuncDef stat3PushFuncdef = {
! 79506: 5, /* nArg */
! 79507: SQLITE_UTF8, /* iPrefEnc */
! 79508: 0, /* flags */
! 79509: 0, /* pUserData */
! 79510: 0, /* pNext */
! 79511: stat3Push, /* xFunc */
! 79512: 0, /* xStep */
! 79513: 0, /* xFinalize */
! 79514: "stat3_push", /* zName */
! 79515: 0, /* pHash */
! 79516: 0 /* pDestructor */
! 79517: };
! 79518:
! 79519: /*
! 79520: ** Implementation of the stat3_get(P,N,...) SQL function. This routine is
! 79521: ** used to query the results. Content is returned for the Nth sqlite_stat3
! 79522: ** row where N is between 0 and S-1 and S is the number of samples. The
! 79523: ** value returned depends on the number of arguments.
! 79524: **
! 79525: ** argc==2 result: rowid
! 79526: ** argc==3 result: nEq
! 79527: ** argc==4 result: nLt
! 79528: ** argc==5 result: nDLt
! 79529: */
! 79530: static void stat3Get(
! 79531: sqlite3_context *context,
! 79532: int argc,
! 79533: sqlite3_value **argv
! 79534: ){
! 79535: int n = sqlite3_value_int(argv[1]);
! 79536: Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
! 79537:
! 79538: assert( p!=0 );
! 79539: if( p->nSample<=n ) return;
! 79540: switch( argc ){
! 79541: case 2: sqlite3_result_int64(context, p->a[n].iRowid); break;
! 79542: case 3: sqlite3_result_int64(context, p->a[n].nEq); break;
! 79543: case 4: sqlite3_result_int64(context, p->a[n].nLt); break;
! 79544: default: sqlite3_result_int64(context, p->a[n].nDLt); break;
! 79545: }
! 79546: }
! 79547: static const FuncDef stat3GetFuncdef = {
! 79548: -1, /* nArg */
! 79549: SQLITE_UTF8, /* iPrefEnc */
! 79550: 0, /* flags */
! 79551: 0, /* pUserData */
! 79552: 0, /* pNext */
! 79553: stat3Get, /* xFunc */
! 79554: 0, /* xStep */
! 79555: 0, /* xFinalize */
! 79556: "stat3_get", /* zName */
! 79557: 0, /* pHash */
! 79558: 0 /* pDestructor */
! 79559: };
! 79560: #endif /* SQLITE_ENABLE_STAT3 */
! 79561:
! 79562:
! 79563:
! 79564:
! 79565: /*
! 79566: ** Generate code to do an analysis of all indices associated with
! 79567: ** a single table.
! 79568: */
! 79569: static void analyzeOneTable(
! 79570: Parse *pParse, /* Parser context */
! 79571: Table *pTab, /* Table whose indices are to be analyzed */
! 79572: Index *pOnlyIdx, /* If not NULL, only analyze this one index */
! 79573: int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
! 79574: int iMem /* Available memory locations begin here */
! 79575: ){
! 79576: sqlite3 *db = pParse->db; /* Database handle */
! 79577: Index *pIdx; /* An index to being analyzed */
! 79578: int iIdxCur; /* Cursor open on index being analyzed */
! 79579: Vdbe *v; /* The virtual machine being built up */
! 79580: int i; /* Loop counter */
! 79581: int topOfLoop; /* The top of the loop */
! 79582: int endOfLoop; /* The end of the loop */
! 79583: int jZeroRows = -1; /* Jump from here if number of rows is zero */
! 79584: int iDb; /* Index of database containing pTab */
! 79585: int regTabname = iMem++; /* Register containing table name */
! 79586: int regIdxname = iMem++; /* Register containing index name */
! 79587: int regStat1 = iMem++; /* The stat column of sqlite_stat1 */
! 79588: #ifdef SQLITE_ENABLE_STAT3
! 79589: int regNumEq = regStat1; /* Number of instances. Same as regStat1 */
! 79590: int regNumLt = iMem++; /* Number of keys less than regSample */
! 79591: int regNumDLt = iMem++; /* Number of distinct keys less than regSample */
! 79592: int regSample = iMem++; /* The next sample value */
! 79593: int regRowid = regSample; /* Rowid of a sample */
! 79594: int regAccum = iMem++; /* Register to hold Stat3Accum object */
! 79595: int regLoop = iMem++; /* Loop counter */
! 79596: int regCount = iMem++; /* Number of rows in the table or index */
! 79597: int regTemp1 = iMem++; /* Intermediate register */
! 79598: int regTemp2 = iMem++; /* Intermediate register */
! 79599: int once = 1; /* One-time initialization */
! 79600: int shortJump = 0; /* Instruction address */
! 79601: int iTabCur = pParse->nTab++; /* Table cursor */
! 79602: #endif
! 79603: int regCol = iMem++; /* Content of a column in analyzed table */
! 79604: int regRec = iMem++; /* Register holding completed record */
! 79605: int regTemp = iMem++; /* Temporary use register */
! 79606: int regNewRowid = iMem++; /* Rowid for the inserted record */
! 79607:
! 79608:
! 79609: v = sqlite3GetVdbe(pParse);
! 79610: if( v==0 || NEVER(pTab==0) ){
! 79611: return;
! 79612: }
! 79613: if( pTab->tnum==0 ){
! 79614: /* Do not gather statistics on views or virtual tables */
! 79615: return;
! 79616: }
! 79617: if( memcmp(pTab->zName, "sqlite_", 7)==0 ){
! 79618: /* Do not gather statistics on system tables */
! 79619: return;
! 79620: }
! 79621: assert( sqlite3BtreeHoldsAllMutexes(db) );
! 79622: iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 79623: assert( iDb>=0 );
! 79624: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 79625: #ifndef SQLITE_OMIT_AUTHORIZATION
! 79626: if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
! 79627: db->aDb[iDb].zName ) ){
! 79628: return;
! 79629: }
! 79630: #endif
! 79631:
! 79632: /* Establish a read-lock on the table at the shared-cache level. */
! 79633: sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
! 79634:
! 79635: iIdxCur = pParse->nTab++;
! 79636: sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
! 79637: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
! 79638: int nCol;
! 79639: KeyInfo *pKey;
! 79640: int addrIfNot = 0; /* address of OP_IfNot */
! 79641: int *aChngAddr; /* Array of jump instruction addresses */
! 79642:
! 79643: if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
! 79644: VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
! 79645: nCol = pIdx->nColumn;
! 79646: aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
! 79647: if( aChngAddr==0 ) continue;
! 79648: pKey = sqlite3IndexKeyinfo(pParse, pIdx);
! 79649: if( iMem+1+(nCol*2)>pParse->nMem ){
! 79650: pParse->nMem = iMem+1+(nCol*2);
! 79651: }
! 79652:
! 79653: /* Open a cursor to the index to be analyzed. */
! 79654: assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
! 79655: sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
! 79656: (char *)pKey, P4_KEYINFO_HANDOFF);
! 79657: VdbeComment((v, "%s", pIdx->zName));
! 79658:
! 79659: /* Populate the register containing the index name. */
! 79660: sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
! 79661:
! 79662: #ifdef SQLITE_ENABLE_STAT3
! 79663: if( once ){
! 79664: once = 0;
! 79665: sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
! 79666: }
! 79667: sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
! 79668: sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
! 79669: sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
! 79670: sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
! 79671: sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
! 79672: sqlite3VdbeAddOp3(v, OP_Null, 0, regSample, regAccum);
! 79673: sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
! 79674: (char*)&stat3InitFuncdef, P4_FUNCDEF);
! 79675: sqlite3VdbeChangeP5(v, 2);
! 79676: #endif /* SQLITE_ENABLE_STAT3 */
! 79677:
! 79678: /* The block of memory cells initialized here is used as follows.
! 79679: **
! 79680: ** iMem:
! 79681: ** The total number of rows in the table.
! 79682: **
! 79683: ** iMem+1 .. iMem+nCol:
! 79684: ** Number of distinct entries in index considering the
! 79685: ** left-most N columns only, where N is between 1 and nCol,
! 79686: ** inclusive.
! 79687: **
! 79688: ** iMem+nCol+1 .. Mem+2*nCol:
! 79689: ** Previous value of indexed columns, from left to right.
! 79690: **
! 79691: ** Cells iMem through iMem+nCol are initialized to 0. The others are
! 79692: ** initialized to contain an SQL NULL.
! 79693: */
! 79694: for(i=0; i<=nCol; i++){
! 79695: sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
! 79696: }
! 79697: for(i=0; i<nCol; i++){
! 79698: sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
! 79699: }
! 79700:
! 79701: /* Start the analysis loop. This loop runs through all the entries in
! 79702: ** the index b-tree. */
! 79703: endOfLoop = sqlite3VdbeMakeLabel(v);
! 79704: sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
! 79705: topOfLoop = sqlite3VdbeCurrentAddr(v);
! 79706: sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */
! 79707:
! 79708: for(i=0; i<nCol; i++){
! 79709: CollSeq *pColl;
! 79710: sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
! 79711: if( i==0 ){
! 79712: /* Always record the very first row */
! 79713: addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
! 79714: }
! 79715: assert( pIdx->azColl!=0 );
! 79716: assert( pIdx->azColl[i]!=0 );
! 79717: pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
! 79718: aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
! 79719: (char*)pColl, P4_COLLSEQ);
! 79720: sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
! 79721: VdbeComment((v, "jump if column %d changed", i));
! 79722: #ifdef SQLITE_ENABLE_STAT3
! 79723: if( i==0 ){
! 79724: sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
! 79725: VdbeComment((v, "incr repeat count"));
! 79726: }
! 79727: #endif
! 79728: }
! 79729: sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
! 79730: for(i=0; i<nCol; i++){
! 79731: sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */
! 79732: if( i==0 ){
! 79733: sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */
! 79734: #ifdef SQLITE_ENABLE_STAT3
! 79735: sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
! 79736: (char*)&stat3PushFuncdef, P4_FUNCDEF);
! 79737: sqlite3VdbeChangeP5(v, 5);
! 79738: sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
! 79739: sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
! 79740: sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
! 79741: sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
! 79742: #endif
! 79743: }
! 79744: sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
! 79745: sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
! 79746: }
! 79747: sqlite3DbFree(db, aChngAddr);
! 79748:
! 79749: /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
! 79750: sqlite3VdbeResolveLabel(v, endOfLoop);
! 79751:
! 79752: sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
! 79753: sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
! 79754: #ifdef SQLITE_ENABLE_STAT3
! 79755: sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
! 79756: (char*)&stat3PushFuncdef, P4_FUNCDEF);
! 79757: sqlite3VdbeChangeP5(v, 5);
! 79758: sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
! 79759: shortJump =
! 79760: sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
! 79761: sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
! 79762: (char*)&stat3GetFuncdef, P4_FUNCDEF);
! 79763: sqlite3VdbeChangeP5(v, 2);
! 79764: sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
! 79765: sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
! 79766: sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
! 79767: sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
! 79768: sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
! 79769: (char*)&stat3GetFuncdef, P4_FUNCDEF);
! 79770: sqlite3VdbeChangeP5(v, 3);
! 79771: sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
! 79772: (char*)&stat3GetFuncdef, P4_FUNCDEF);
! 79773: sqlite3VdbeChangeP5(v, 4);
! 79774: sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
! 79775: (char*)&stat3GetFuncdef, P4_FUNCDEF);
! 79776: sqlite3VdbeChangeP5(v, 5);
! 79777: sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
! 79778: sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
! 79779: sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
! 79780: sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
! 79781: sqlite3VdbeJumpHere(v, shortJump+2);
! 79782: #endif
! 79783:
! 79784: /* Store the results in sqlite_stat1.
! 79785: **
! 79786: ** The result is a single row of the sqlite_stat1 table. The first
! 79787: ** two columns are the names of the table and index. The third column
! 79788: ** is a string composed of a list of integer statistics about the
! 79789: ** index. The first integer in the list is the total number of entries
! 79790: ** in the index. There is one additional integer in the list for each
! 79791: ** column of the table. This additional integer is a guess of how many
! 79792: ** rows of the table the index will select. If D is the count of distinct
! 79793: ** values and K is the total number of rows, then the integer is computed
! 79794: ** as:
! 79795: **
! 79796: ** I = (K+D-1)/D
! 79797: **
! 79798: ** If K==0 then no entry is made into the sqlite_stat1 table.
! 79799: ** If K>0 then it is always the case the D>0 so division by zero
! 79800: ** is never possible.
! 79801: */
! 79802: sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
! 79803: if( jZeroRows<0 ){
! 79804: jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
! 79805: }
! 79806: for(i=0; i<nCol; i++){
! 79807: sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
! 79808: sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
! 79809: sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
! 79810: sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
! 79811: sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
! 79812: sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
! 79813: sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
! 79814: }
! 79815: sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
! 79816: sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
! 79817: sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
! 79818: sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
! 79819: }
! 79820:
! 79821: /* If the table has no indices, create a single sqlite_stat1 entry
! 79822: ** containing NULL as the index name and the row count as the content.
! 79823: */
! 79824: if( pTab->pIndex==0 ){
! 79825: sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
! 79826: VdbeComment((v, "%s", pTab->zName));
! 79827: sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
! 79828: sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
! 79829: jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
! 79830: }else{
! 79831: sqlite3VdbeJumpHere(v, jZeroRows);
! 79832: jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
! 79833: }
! 79834: sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
! 79835: sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
! 79836: sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
! 79837: sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
! 79838: sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
! 79839: if( pParse->nMem<regRec ) pParse->nMem = regRec;
! 79840: sqlite3VdbeJumpHere(v, jZeroRows);
! 79841: }
! 79842:
! 79843:
! 79844: /*
! 79845: ** Generate code that will cause the most recent index analysis to
! 79846: ** be loaded into internal hash tables where is can be used.
! 79847: */
! 79848: static void loadAnalysis(Parse *pParse, int iDb){
! 79849: Vdbe *v = sqlite3GetVdbe(pParse);
! 79850: if( v ){
! 79851: sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
! 79852: }
! 79853: }
! 79854:
! 79855: /*
! 79856: ** Generate code that will do an analysis of an entire database
! 79857: */
! 79858: static void analyzeDatabase(Parse *pParse, int iDb){
! 79859: sqlite3 *db = pParse->db;
! 79860: Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
! 79861: HashElem *k;
! 79862: int iStatCur;
! 79863: int iMem;
! 79864:
! 79865: sqlite3BeginWriteOperation(pParse, 0, iDb);
! 79866: iStatCur = pParse->nTab;
! 79867: pParse->nTab += 3;
! 79868: openStatTable(pParse, iDb, iStatCur, 0, 0);
! 79869: iMem = pParse->nMem+1;
! 79870: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 79871: for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
! 79872: Table *pTab = (Table*)sqliteHashData(k);
! 79873: analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
! 79874: }
! 79875: loadAnalysis(pParse, iDb);
! 79876: }
! 79877:
! 79878: /*
! 79879: ** Generate code that will do an analysis of a single table in
! 79880: ** a database. If pOnlyIdx is not NULL then it is a single index
! 79881: ** in pTab that should be analyzed.
! 79882: */
! 79883: static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
! 79884: int iDb;
! 79885: int iStatCur;
! 79886:
! 79887: assert( pTab!=0 );
! 79888: assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
! 79889: iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
! 79890: sqlite3BeginWriteOperation(pParse, 0, iDb);
! 79891: iStatCur = pParse->nTab;
! 79892: pParse->nTab += 3;
! 79893: if( pOnlyIdx ){
! 79894: openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
! 79895: }else{
! 79896: openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
! 79897: }
! 79898: analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
! 79899: loadAnalysis(pParse, iDb);
! 79900: }
! 79901:
! 79902: /*
! 79903: ** Generate code for the ANALYZE command. The parser calls this routine
! 79904: ** when it recognizes an ANALYZE command.
! 79905: **
! 79906: ** ANALYZE -- 1
! 79907: ** ANALYZE <database> -- 2
! 79908: ** ANALYZE ?<database>.?<tablename> -- 3
! 79909: **
! 79910: ** Form 1 causes all indices in all attached databases to be analyzed.
! 79911: ** Form 2 analyzes all indices the single database named.
! 79912: ** Form 3 analyzes all indices associated with the named table.
! 79913: */
! 79914: SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
! 79915: sqlite3 *db = pParse->db;
! 79916: int iDb;
! 79917: int i;
! 79918: char *z, *zDb;
! 79919: Table *pTab;
! 79920: Index *pIdx;
! 79921: Token *pTableName;
! 79922:
! 79923: /* Read the database schema. If an error occurs, leave an error message
! 79924: ** and code in pParse and return NULL. */
! 79925: assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
! 79926: if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
! 79927: return;
! 79928: }
! 79929:
! 79930: assert( pName2!=0 || pName1==0 );
! 79931: if( pName1==0 ){
! 79932: /* Form 1: Analyze everything */
! 79933: for(i=0; i<db->nDb; i++){
! 79934: if( i==1 ) continue; /* Do not analyze the TEMP database */
! 79935: analyzeDatabase(pParse, i);
! 79936: }
! 79937: }else if( pName2->n==0 ){
! 79938: /* Form 2: Analyze the database or table named */
! 79939: iDb = sqlite3FindDb(db, pName1);
! 79940: if( iDb>=0 ){
! 79941: analyzeDatabase(pParse, iDb);
! 79942: }else{
! 79943: z = sqlite3NameFromToken(db, pName1);
! 79944: if( z ){
! 79945: if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
! 79946: analyzeTable(pParse, pIdx->pTable, pIdx);
! 79947: }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
! 79948: analyzeTable(pParse, pTab, 0);
! 79949: }
! 79950: sqlite3DbFree(db, z);
! 79951: }
! 79952: }
! 79953: }else{
! 79954: /* Form 3: Analyze the fully qualified table name */
! 79955: iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
! 79956: if( iDb>=0 ){
! 79957: zDb = db->aDb[iDb].zName;
! 79958: z = sqlite3NameFromToken(db, pTableName);
! 79959: if( z ){
! 79960: if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
! 79961: analyzeTable(pParse, pIdx->pTable, pIdx);
! 79962: }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
! 79963: analyzeTable(pParse, pTab, 0);
! 79964: }
! 79965: sqlite3DbFree(db, z);
! 79966: }
! 79967: }
! 79968: }
! 79969: }
! 79970:
! 79971: /*
! 79972: ** Used to pass information from the analyzer reader through to the
! 79973: ** callback routine.
! 79974: */
! 79975: typedef struct analysisInfo analysisInfo;
! 79976: struct analysisInfo {
! 79977: sqlite3 *db;
! 79978: const char *zDatabase;
! 79979: };
! 79980:
! 79981: /*
! 79982: ** This callback is invoked once for each index when reading the
! 79983: ** sqlite_stat1 table.
! 79984: **
! 79985: ** argv[0] = name of the table
! 79986: ** argv[1] = name of the index (might be NULL)
! 79987: ** argv[2] = results of analysis - on integer for each column
! 79988: **
! 79989: ** Entries for which argv[1]==NULL simply record the number of rows in
! 79990: ** the table.
! 79991: */
! 79992: static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
! 79993: analysisInfo *pInfo = (analysisInfo*)pData;
! 79994: Index *pIndex;
! 79995: Table *pTable;
! 79996: int i, c, n;
! 79997: tRowcnt v;
! 79998: const char *z;
! 79999:
! 80000: assert( argc==3 );
! 80001: UNUSED_PARAMETER2(NotUsed, argc);
! 80002:
! 80003: if( argv==0 || argv[0]==0 || argv[2]==0 ){
! 80004: return 0;
! 80005: }
! 80006: pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
! 80007: if( pTable==0 ){
! 80008: return 0;
! 80009: }
! 80010: if( argv[1] ){
! 80011: pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
! 80012: }else{
! 80013: pIndex = 0;
! 80014: }
! 80015: n = pIndex ? pIndex->nColumn : 0;
! 80016: z = argv[2];
! 80017: for(i=0; *z && i<=n; i++){
! 80018: v = 0;
! 80019: while( (c=z[0])>='0' && c<='9' ){
! 80020: v = v*10 + c - '0';
! 80021: z++;
! 80022: }
! 80023: if( i==0 ) pTable->nRowEst = v;
! 80024: if( pIndex==0 ) break;
! 80025: pIndex->aiRowEst[i] = v;
! 80026: if( *z==' ' ) z++;
! 80027: if( memcmp(z, "unordered", 10)==0 ){
! 80028: pIndex->bUnordered = 1;
! 80029: break;
! 80030: }
! 80031: }
! 80032: return 0;
! 80033: }
! 80034:
! 80035: /*
! 80036: ** If the Index.aSample variable is not NULL, delete the aSample[] array
! 80037: ** and its contents.
! 80038: */
! 80039: SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
! 80040: #ifdef SQLITE_ENABLE_STAT3
! 80041: if( pIdx->aSample ){
! 80042: int j;
! 80043: for(j=0; j<pIdx->nSample; j++){
! 80044: IndexSample *p = &pIdx->aSample[j];
! 80045: if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
! 80046: sqlite3DbFree(db, p->u.z);
! 80047: }
! 80048: }
! 80049: sqlite3DbFree(db, pIdx->aSample);
! 80050: }
! 80051: if( db && db->pnBytesFreed==0 ){
! 80052: pIdx->nSample = 0;
! 80053: pIdx->aSample = 0;
! 80054: }
! 80055: #else
! 80056: UNUSED_PARAMETER(db);
! 80057: UNUSED_PARAMETER(pIdx);
! 80058: #endif
! 80059: }
! 80060:
! 80061: #ifdef SQLITE_ENABLE_STAT3
! 80062: /*
! 80063: ** Load content from the sqlite_stat3 table into the Index.aSample[]
! 80064: ** arrays of all indices.
! 80065: */
! 80066: static int loadStat3(sqlite3 *db, const char *zDb){
! 80067: int rc; /* Result codes from subroutines */
! 80068: sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
! 80069: char *zSql; /* Text of the SQL statement */
! 80070: Index *pPrevIdx = 0; /* Previous index in the loop */
! 80071: int idx = 0; /* slot in pIdx->aSample[] for next sample */
! 80072: int eType; /* Datatype of a sample */
! 80073: IndexSample *pSample; /* A slot in pIdx->aSample[] */
! 80074:
! 80075: if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
! 80076: return SQLITE_OK;
! 80077: }
! 80078:
! 80079: zSql = sqlite3MPrintf(db,
! 80080: "SELECT idx,count(*) FROM %Q.sqlite_stat3"
! 80081: " GROUP BY idx", zDb);
! 80082: if( !zSql ){
! 80083: return SQLITE_NOMEM;
! 80084: }
! 80085: rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
! 80086: sqlite3DbFree(db, zSql);
! 80087: if( rc ) return rc;
! 80088:
! 80089: while( sqlite3_step(pStmt)==SQLITE_ROW ){
! 80090: char *zIndex; /* Index name */
! 80091: Index *pIdx; /* Pointer to the index object */
! 80092: int nSample; /* Number of samples */
! 80093:
! 80094: zIndex = (char *)sqlite3_column_text(pStmt, 0);
! 80095: if( zIndex==0 ) continue;
! 80096: nSample = sqlite3_column_int(pStmt, 1);
! 80097: pIdx = sqlite3FindIndex(db, zIndex, zDb);
! 80098: if( pIdx==0 ) continue;
! 80099: assert( pIdx->nSample==0 );
! 80100: pIdx->nSample = nSample;
! 80101: pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) );
! 80102: pIdx->avgEq = pIdx->aiRowEst[1];
! 80103: if( pIdx->aSample==0 ){
! 80104: db->mallocFailed = 1;
! 80105: sqlite3_finalize(pStmt);
! 80106: return SQLITE_NOMEM;
! 80107: }
! 80108: }
! 80109: rc = sqlite3_finalize(pStmt);
! 80110: if( rc ) return rc;
! 80111:
! 80112: zSql = sqlite3MPrintf(db,
! 80113: "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
! 80114: if( !zSql ){
! 80115: return SQLITE_NOMEM;
! 80116: }
! 80117: rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
! 80118: sqlite3DbFree(db, zSql);
! 80119: if( rc ) return rc;
! 80120:
! 80121: while( sqlite3_step(pStmt)==SQLITE_ROW ){
! 80122: char *zIndex; /* Index name */
! 80123: Index *pIdx; /* Pointer to the index object */
! 80124: int i; /* Loop counter */
! 80125: tRowcnt sumEq; /* Sum of the nEq values */
! 80126:
! 80127: zIndex = (char *)sqlite3_column_text(pStmt, 0);
! 80128: if( zIndex==0 ) continue;
! 80129: pIdx = sqlite3FindIndex(db, zIndex, zDb);
! 80130: if( pIdx==0 ) continue;
! 80131: if( pIdx==pPrevIdx ){
! 80132: idx++;
! 80133: }else{
! 80134: pPrevIdx = pIdx;
! 80135: idx = 0;
! 80136: }
! 80137: assert( idx<pIdx->nSample );
! 80138: pSample = &pIdx->aSample[idx];
! 80139: pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
! 80140: pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
! 80141: pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
! 80142: if( idx==pIdx->nSample-1 ){
! 80143: if( pSample->nDLt>0 ){
! 80144: for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
! 80145: pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
! 80146: }
! 80147: if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
! 80148: }
! 80149: eType = sqlite3_column_type(pStmt, 4);
! 80150: pSample->eType = (u8)eType;
! 80151: switch( eType ){
! 80152: case SQLITE_INTEGER: {
! 80153: pSample->u.i = sqlite3_column_int64(pStmt, 4);
! 80154: break;
! 80155: }
! 80156: case SQLITE_FLOAT: {
! 80157: pSample->u.r = sqlite3_column_double(pStmt, 4);
! 80158: break;
! 80159: }
! 80160: case SQLITE_NULL: {
! 80161: break;
! 80162: }
! 80163: default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
! 80164: const char *z = (const char *)(
! 80165: (eType==SQLITE_BLOB) ?
! 80166: sqlite3_column_blob(pStmt, 4):
! 80167: sqlite3_column_text(pStmt, 4)
! 80168: );
! 80169: int n = z ? sqlite3_column_bytes(pStmt, 4) : 0;
! 80170: pSample->nByte = n;
! 80171: if( n < 1){
! 80172: pSample->u.z = 0;
! 80173: }else{
! 80174: pSample->u.z = sqlite3Malloc(n);
! 80175: if( pSample->u.z==0 ){
! 80176: db->mallocFailed = 1;
! 80177: sqlite3_finalize(pStmt);
! 80178: return SQLITE_NOMEM;
! 80179: }
! 80180: memcpy(pSample->u.z, z, n);
! 80181: }
! 80182: }
! 80183: }
! 80184: }
! 80185: return sqlite3_finalize(pStmt);
! 80186: }
! 80187: #endif /* SQLITE_ENABLE_STAT3 */
! 80188:
! 80189: /*
! 80190: ** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
! 80191: ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
! 80192: ** arrays. The contents of sqlite_stat3 are used to populate the
! 80193: ** Index.aSample[] arrays.
! 80194: **
! 80195: ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
! 80196: ** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined
! 80197: ** during compilation and the sqlite_stat3 table is present, no data is
! 80198: ** read from it.
! 80199: **
! 80200: ** If SQLITE_ENABLE_STAT3 was defined during compilation and the
! 80201: ** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
! 80202: ** returned. However, in this case, data is read from the sqlite_stat1
! 80203: ** table (if it is present) before returning.
! 80204: **
! 80205: ** If an OOM error occurs, this function always sets db->mallocFailed.
! 80206: ** This means if the caller does not care about other errors, the return
! 80207: ** code may be ignored.
! 80208: */
! 80209: SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
! 80210: analysisInfo sInfo;
! 80211: HashElem *i;
! 80212: char *zSql;
! 80213: int rc;
! 80214:
! 80215: assert( iDb>=0 && iDb<db->nDb );
! 80216: assert( db->aDb[iDb].pBt!=0 );
! 80217:
! 80218: /* Clear any prior statistics */
! 80219: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 80220: for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
! 80221: Index *pIdx = sqliteHashData(i);
! 80222: sqlite3DefaultRowEst(pIdx);
! 80223: #ifdef SQLITE_ENABLE_STAT3
! 80224: sqlite3DeleteIndexSamples(db, pIdx);
! 80225: pIdx->aSample = 0;
! 80226: #endif
! 80227: }
! 80228:
! 80229: /* Check to make sure the sqlite_stat1 table exists */
! 80230: sInfo.db = db;
! 80231: sInfo.zDatabase = db->aDb[iDb].zName;
! 80232: if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
! 80233: return SQLITE_ERROR;
! 80234: }
! 80235:
! 80236: /* Load new statistics out of the sqlite_stat1 table */
! 80237: zSql = sqlite3MPrintf(db,
! 80238: "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
! 80239: if( zSql==0 ){
! 80240: rc = SQLITE_NOMEM;
! 80241: }else{
! 80242: rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
! 80243: sqlite3DbFree(db, zSql);
! 80244: }
! 80245:
! 80246:
! 80247: /* Load the statistics from the sqlite_stat3 table. */
! 80248: #ifdef SQLITE_ENABLE_STAT3
! 80249: if( rc==SQLITE_OK ){
! 80250: rc = loadStat3(db, sInfo.zDatabase);
! 80251: }
! 80252: #endif
! 80253:
! 80254: if( rc==SQLITE_NOMEM ){
! 80255: db->mallocFailed = 1;
! 80256: }
! 80257: return rc;
! 80258: }
! 80259:
! 80260:
! 80261: #endif /* SQLITE_OMIT_ANALYZE */
! 80262:
! 80263: /************** End of analyze.c *********************************************/
! 80264: /************** Begin file attach.c ******************************************/
! 80265: /*
! 80266: ** 2003 April 6
! 80267: **
! 80268: ** The author disclaims copyright to this source code. In place of
! 80269: ** a legal notice, here is a blessing:
! 80270: **
! 80271: ** May you do good and not evil.
! 80272: ** May you find forgiveness for yourself and forgive others.
! 80273: ** May you share freely, never taking more than you give.
! 80274: **
! 80275: *************************************************************************
! 80276: ** This file contains code used to implement the ATTACH and DETACH commands.
! 80277: */
! 80278:
! 80279: #ifndef SQLITE_OMIT_ATTACH
! 80280: /*
! 80281: ** Resolve an expression that was part of an ATTACH or DETACH statement. This
! 80282: ** is slightly different from resolving a normal SQL expression, because simple
! 80283: ** identifiers are treated as strings, not possible column names or aliases.
! 80284: **
! 80285: ** i.e. if the parser sees:
! 80286: **
! 80287: ** ATTACH DATABASE abc AS def
! 80288: **
! 80289: ** it treats the two expressions as literal strings 'abc' and 'def' instead of
! 80290: ** looking for columns of the same name.
! 80291: **
! 80292: ** This only applies to the root node of pExpr, so the statement:
! 80293: **
! 80294: ** ATTACH DATABASE abc||def AS 'db2'
! 80295: **
! 80296: ** will fail because neither abc or def can be resolved.
! 80297: */
! 80298: static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
! 80299: {
! 80300: int rc = SQLITE_OK;
! 80301: if( pExpr ){
! 80302: if( pExpr->op!=TK_ID ){
! 80303: rc = sqlite3ResolveExprNames(pName, pExpr);
! 80304: if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
! 80305: sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->u.zToken);
! 80306: return SQLITE_ERROR;
! 80307: }
! 80308: }else{
! 80309: pExpr->op = TK_STRING;
! 80310: }
! 80311: }
! 80312: return rc;
! 80313: }
! 80314:
! 80315: /*
! 80316: ** An SQL user-function registered to do the work of an ATTACH statement. The
! 80317: ** three arguments to the function come directly from an attach statement:
! 80318: **
! 80319: ** ATTACH DATABASE x AS y KEY z
! 80320: **
! 80321: ** SELECT sqlite_attach(x, y, z)
! 80322: **
! 80323: ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
! 80324: ** third argument.
! 80325: */
! 80326: static void attachFunc(
! 80327: sqlite3_context *context,
! 80328: int NotUsed,
! 80329: sqlite3_value **argv
! 80330: ){
! 80331: int i;
! 80332: int rc = 0;
! 80333: sqlite3 *db = sqlite3_context_db_handle(context);
! 80334: const char *zName;
! 80335: const char *zFile;
! 80336: char *zPath = 0;
! 80337: char *zErr = 0;
! 80338: unsigned int flags;
! 80339: Db *aNew;
! 80340: char *zErrDyn = 0;
! 80341: sqlite3_vfs *pVfs;
! 80342:
! 80343: UNUSED_PARAMETER(NotUsed);
! 80344:
! 80345: zFile = (const char *)sqlite3_value_text(argv[0]);
! 80346: zName = (const char *)sqlite3_value_text(argv[1]);
! 80347: if( zFile==0 ) zFile = "";
! 80348: if( zName==0 ) zName = "";
! 80349:
! 80350: /* Check for the following errors:
! 80351: **
! 80352: ** * Too many attached databases,
! 80353: ** * Transaction currently open
! 80354: ** * Specified database name already being used.
! 80355: */
! 80356: if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
! 80357: zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
! 80358: db->aLimit[SQLITE_LIMIT_ATTACHED]
! 80359: );
! 80360: goto attach_error;
! 80361: }
! 80362: if( !db->autoCommit ){
! 80363: zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
! 80364: goto attach_error;
! 80365: }
! 80366: for(i=0; i<db->nDb; i++){
! 80367: char *z = db->aDb[i].zName;
! 80368: assert( z && zName );
! 80369: if( sqlite3StrICmp(z, zName)==0 ){
! 80370: zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
! 80371: goto attach_error;
! 80372: }
! 80373: }
! 80374:
! 80375: /* Allocate the new entry in the db->aDb[] array and initialise the schema
! 80376: ** hash tables.
! 80377: */
! 80378: if( db->aDb==db->aDbStatic ){
! 80379: aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
! 80380: if( aNew==0 ) return;
! 80381: memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
! 80382: }else{
! 80383: aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
! 80384: if( aNew==0 ) return;
! 80385: }
! 80386: db->aDb = aNew;
! 80387: aNew = &db->aDb[db->nDb];
! 80388: memset(aNew, 0, sizeof(*aNew));
! 80389:
! 80390: /* Open the database file. If the btree is successfully opened, use
! 80391: ** it to obtain the database schema. At this point the schema may
! 80392: ** or may not be initialised.
! 80393: */
! 80394: flags = db->openFlags;
! 80395: rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
! 80396: if( rc!=SQLITE_OK ){
! 80397: if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
! 80398: sqlite3_result_error(context, zErr, -1);
! 80399: sqlite3_free(zErr);
! 80400: return;
! 80401: }
! 80402: assert( pVfs );
! 80403: flags |= SQLITE_OPEN_MAIN_DB;
! 80404: rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
! 80405: sqlite3_free( zPath );
! 80406: db->nDb++;
! 80407: if( rc==SQLITE_CONSTRAINT ){
! 80408: rc = SQLITE_ERROR;
! 80409: zErrDyn = sqlite3MPrintf(db, "database is already attached");
! 80410: }else if( rc==SQLITE_OK ){
! 80411: Pager *pPager;
! 80412: aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
! 80413: if( !aNew->pSchema ){
! 80414: rc = SQLITE_NOMEM;
! 80415: }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
! 80416: zErrDyn = sqlite3MPrintf(db,
! 80417: "attached databases must use the same text encoding as main database");
! 80418: rc = SQLITE_ERROR;
! 80419: }
! 80420: pPager = sqlite3BtreePager(aNew->pBt);
! 80421: sqlite3PagerLockingMode(pPager, db->dfltLockMode);
! 80422: sqlite3BtreeSecureDelete(aNew->pBt,
! 80423: sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
! 80424: }
! 80425: aNew->safety_level = 3;
! 80426: aNew->zName = sqlite3DbStrDup(db, zName);
! 80427: if( rc==SQLITE_OK && aNew->zName==0 ){
! 80428: rc = SQLITE_NOMEM;
! 80429: }
! 80430:
! 80431:
! 80432: #ifdef SQLITE_HAS_CODEC
! 80433: if( rc==SQLITE_OK ){
! 80434: extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
! 80435: extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
! 80436: int nKey;
! 80437: char *zKey;
! 80438: int t = sqlite3_value_type(argv[2]);
! 80439: switch( t ){
! 80440: case SQLITE_INTEGER:
! 80441: case SQLITE_FLOAT:
! 80442: zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
! 80443: rc = SQLITE_ERROR;
! 80444: break;
! 80445:
! 80446: case SQLITE_TEXT:
! 80447: case SQLITE_BLOB:
! 80448: nKey = sqlite3_value_bytes(argv[2]);
! 80449: zKey = (char *)sqlite3_value_blob(argv[2]);
! 80450: rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
! 80451: break;
! 80452:
! 80453: case SQLITE_NULL:
! 80454: /* No key specified. Use the key from the main database */
! 80455: sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
! 80456: if( nKey>0 || sqlite3BtreeGetReserve(db->aDb[0].pBt)>0 ){
! 80457: rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
! 80458: }
! 80459: break;
! 80460: }
! 80461: }
! 80462: #endif
! 80463:
! 80464: /* If the file was opened successfully, read the schema for the new database.
! 80465: ** If this fails, or if opening the file failed, then close the file and
! 80466: ** remove the entry from the db->aDb[] array. i.e. put everything back the way
! 80467: ** we found it.
! 80468: */
! 80469: if( rc==SQLITE_OK ){
! 80470: sqlite3BtreeEnterAll(db);
! 80471: rc = sqlite3Init(db, &zErrDyn);
! 80472: sqlite3BtreeLeaveAll(db);
! 80473: }
! 80474: if( rc ){
! 80475: int iDb = db->nDb - 1;
! 80476: assert( iDb>=2 );
! 80477: if( db->aDb[iDb].pBt ){
! 80478: sqlite3BtreeClose(db->aDb[iDb].pBt);
! 80479: db->aDb[iDb].pBt = 0;
! 80480: db->aDb[iDb].pSchema = 0;
! 80481: }
! 80482: sqlite3ResetInternalSchema(db, -1);
! 80483: db->nDb = iDb;
! 80484: if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
! 80485: db->mallocFailed = 1;
! 80486: sqlite3DbFree(db, zErrDyn);
! 80487: zErrDyn = sqlite3MPrintf(db, "out of memory");
! 80488: }else if( zErrDyn==0 ){
! 80489: zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
! 80490: }
! 80491: goto attach_error;
! 80492: }
! 80493:
! 80494: return;
! 80495:
! 80496: attach_error:
! 80497: /* Return an error if we get here */
! 80498: if( zErrDyn ){
! 80499: sqlite3_result_error(context, zErrDyn, -1);
! 80500: sqlite3DbFree(db, zErrDyn);
! 80501: }
! 80502: if( rc ) sqlite3_result_error_code(context, rc);
! 80503: }
! 80504:
! 80505: /*
! 80506: ** An SQL user-function registered to do the work of an DETACH statement. The
! 80507: ** three arguments to the function come directly from a detach statement:
! 80508: **
! 80509: ** DETACH DATABASE x
! 80510: **
! 80511: ** SELECT sqlite_detach(x)
! 80512: */
! 80513: static void detachFunc(
! 80514: sqlite3_context *context,
! 80515: int NotUsed,
! 80516: sqlite3_value **argv
! 80517: ){
! 80518: const char *zName = (const char *)sqlite3_value_text(argv[0]);
! 80519: sqlite3 *db = sqlite3_context_db_handle(context);
! 80520: int i;
! 80521: Db *pDb = 0;
! 80522: char zErr[128];
! 80523:
! 80524: UNUSED_PARAMETER(NotUsed);
! 80525:
! 80526: if( zName==0 ) zName = "";
! 80527: for(i=0; i<db->nDb; i++){
! 80528: pDb = &db->aDb[i];
! 80529: if( pDb->pBt==0 ) continue;
! 80530: if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
! 80531: }
! 80532:
! 80533: if( i>=db->nDb ){
! 80534: sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
! 80535: goto detach_error;
! 80536: }
! 80537: if( i<2 ){
! 80538: sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
! 80539: goto detach_error;
! 80540: }
! 80541: if( !db->autoCommit ){
! 80542: sqlite3_snprintf(sizeof(zErr), zErr,
! 80543: "cannot DETACH database within transaction");
! 80544: goto detach_error;
! 80545: }
! 80546: if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
! 80547: sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
! 80548: goto detach_error;
! 80549: }
! 80550:
! 80551: sqlite3BtreeClose(pDb->pBt);
! 80552: pDb->pBt = 0;
! 80553: pDb->pSchema = 0;
! 80554: sqlite3ResetInternalSchema(db, -1);
! 80555: return;
! 80556:
! 80557: detach_error:
! 80558: sqlite3_result_error(context, zErr, -1);
! 80559: }
! 80560:
! 80561: /*
! 80562: ** This procedure generates VDBE code for a single invocation of either the
! 80563: ** sqlite_detach() or sqlite_attach() SQL user functions.
! 80564: */
! 80565: static void codeAttach(
! 80566: Parse *pParse, /* The parser context */
! 80567: int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
! 80568: FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
! 80569: Expr *pAuthArg, /* Expression to pass to authorization callback */
! 80570: Expr *pFilename, /* Name of database file */
! 80571: Expr *pDbname, /* Name of the database to use internally */
! 80572: Expr *pKey /* Database key for encryption extension */
! 80573: ){
! 80574: int rc;
! 80575: NameContext sName;
! 80576: Vdbe *v;
! 80577: sqlite3* db = pParse->db;
! 80578: int regArgs;
! 80579:
! 80580: memset(&sName, 0, sizeof(NameContext));
! 80581: sName.pParse = pParse;
! 80582:
! 80583: if(
! 80584: SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
! 80585: SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
! 80586: SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
! 80587: ){
! 80588: pParse->nErr++;
! 80589: goto attach_end;
! 80590: }
! 80591:
! 80592: #ifndef SQLITE_OMIT_AUTHORIZATION
! 80593: if( pAuthArg ){
! 80594: char *zAuthArg;
! 80595: if( pAuthArg->op==TK_STRING ){
! 80596: zAuthArg = pAuthArg->u.zToken;
! 80597: }else{
! 80598: zAuthArg = 0;
! 80599: }
! 80600: rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
! 80601: if(rc!=SQLITE_OK ){
! 80602: goto attach_end;
! 80603: }
! 80604: }
! 80605: #endif /* SQLITE_OMIT_AUTHORIZATION */
! 80606:
! 80607:
! 80608: v = sqlite3GetVdbe(pParse);
! 80609: regArgs = sqlite3GetTempRange(pParse, 4);
! 80610: sqlite3ExprCode(pParse, pFilename, regArgs);
! 80611: sqlite3ExprCode(pParse, pDbname, regArgs+1);
! 80612: sqlite3ExprCode(pParse, pKey, regArgs+2);
! 80613:
! 80614: assert( v || db->mallocFailed );
! 80615: if( v ){
! 80616: sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
! 80617: assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
! 80618: sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
! 80619: sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
! 80620:
! 80621: /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
! 80622: ** statement only). For DETACH, set it to false (expire all existing
! 80623: ** statements).
! 80624: */
! 80625: sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
! 80626: }
! 80627:
! 80628: attach_end:
! 80629: sqlite3ExprDelete(db, pFilename);
! 80630: sqlite3ExprDelete(db, pDbname);
! 80631: sqlite3ExprDelete(db, pKey);
! 80632: }
! 80633:
! 80634: /*
! 80635: ** Called by the parser to compile a DETACH statement.
! 80636: **
! 80637: ** DETACH pDbname
! 80638: */
! 80639: SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
! 80640: static const FuncDef detach_func = {
! 80641: 1, /* nArg */
! 80642: SQLITE_UTF8, /* iPrefEnc */
! 80643: 0, /* flags */
! 80644: 0, /* pUserData */
! 80645: 0, /* pNext */
! 80646: detachFunc, /* xFunc */
! 80647: 0, /* xStep */
! 80648: 0, /* xFinalize */
! 80649: "sqlite_detach", /* zName */
! 80650: 0, /* pHash */
! 80651: 0 /* pDestructor */
! 80652: };
! 80653: codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
! 80654: }
! 80655:
! 80656: /*
! 80657: ** Called by the parser to compile an ATTACH statement.
! 80658: **
! 80659: ** ATTACH p AS pDbname KEY pKey
! 80660: */
! 80661: SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
! 80662: static const FuncDef attach_func = {
! 80663: 3, /* nArg */
! 80664: SQLITE_UTF8, /* iPrefEnc */
! 80665: 0, /* flags */
! 80666: 0, /* pUserData */
! 80667: 0, /* pNext */
! 80668: attachFunc, /* xFunc */
! 80669: 0, /* xStep */
! 80670: 0, /* xFinalize */
! 80671: "sqlite_attach", /* zName */
! 80672: 0, /* pHash */
! 80673: 0 /* pDestructor */
! 80674: };
! 80675: codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
! 80676: }
! 80677: #endif /* SQLITE_OMIT_ATTACH */
! 80678:
! 80679: /*
! 80680: ** Initialize a DbFixer structure. This routine must be called prior
! 80681: ** to passing the structure to one of the sqliteFixAAAA() routines below.
! 80682: **
! 80683: ** The return value indicates whether or not fixation is required. TRUE
! 80684: ** means we do need to fix the database references, FALSE means we do not.
! 80685: */
! 80686: SQLITE_PRIVATE int sqlite3FixInit(
! 80687: DbFixer *pFix, /* The fixer to be initialized */
! 80688: Parse *pParse, /* Error messages will be written here */
! 80689: int iDb, /* This is the database that must be used */
! 80690: const char *zType, /* "view", "trigger", or "index" */
! 80691: const Token *pName /* Name of the view, trigger, or index */
! 80692: ){
! 80693: sqlite3 *db;
! 80694:
! 80695: if( NEVER(iDb<0) || iDb==1 ) return 0;
! 80696: db = pParse->db;
! 80697: assert( db->nDb>iDb );
! 80698: pFix->pParse = pParse;
! 80699: pFix->zDb = db->aDb[iDb].zName;
! 80700: pFix->zType = zType;
! 80701: pFix->pName = pName;
! 80702: return 1;
! 80703: }
! 80704:
! 80705: /*
! 80706: ** The following set of routines walk through the parse tree and assign
! 80707: ** a specific database to all table references where the database name
! 80708: ** was left unspecified in the original SQL statement. The pFix structure
! 80709: ** must have been initialized by a prior call to sqlite3FixInit().
! 80710: **
! 80711: ** These routines are used to make sure that an index, trigger, or
! 80712: ** view in one database does not refer to objects in a different database.
! 80713: ** (Exception: indices, triggers, and views in the TEMP database are
! 80714: ** allowed to refer to anything.) If a reference is explicitly made
! 80715: ** to an object in a different database, an error message is added to
! 80716: ** pParse->zErrMsg and these routines return non-zero. If everything
! 80717: ** checks out, these routines return 0.
! 80718: */
! 80719: SQLITE_PRIVATE int sqlite3FixSrcList(
! 80720: DbFixer *pFix, /* Context of the fixation */
! 80721: SrcList *pList /* The Source list to check and modify */
! 80722: ){
! 80723: int i;
! 80724: const char *zDb;
! 80725: struct SrcList_item *pItem;
! 80726:
! 80727: if( NEVER(pList==0) ) return 0;
! 80728: zDb = pFix->zDb;
! 80729: for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
! 80730: if( pItem->zDatabase==0 ){
! 80731: pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
! 80732: }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
! 80733: sqlite3ErrorMsg(pFix->pParse,
! 80734: "%s %T cannot reference objects in database %s",
! 80735: pFix->zType, pFix->pName, pItem->zDatabase);
! 80736: return 1;
! 80737: }
! 80738: #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
! 80739: if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
! 80740: if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
! 80741: #endif
! 80742: }
! 80743: return 0;
! 80744: }
! 80745: #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
! 80746: SQLITE_PRIVATE int sqlite3FixSelect(
! 80747: DbFixer *pFix, /* Context of the fixation */
! 80748: Select *pSelect /* The SELECT statement to be fixed to one database */
! 80749: ){
! 80750: while( pSelect ){
! 80751: if( sqlite3FixExprList(pFix, pSelect->pEList) ){
! 80752: return 1;
! 80753: }
! 80754: if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
! 80755: return 1;
! 80756: }
! 80757: if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
! 80758: return 1;
! 80759: }
! 80760: if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
! 80761: return 1;
! 80762: }
! 80763: pSelect = pSelect->pPrior;
! 80764: }
! 80765: return 0;
! 80766: }
! 80767: SQLITE_PRIVATE int sqlite3FixExpr(
! 80768: DbFixer *pFix, /* Context of the fixation */
! 80769: Expr *pExpr /* The expression to be fixed to one database */
! 80770: ){
! 80771: while( pExpr ){
! 80772: if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ) break;
! 80773: if( ExprHasProperty(pExpr, EP_xIsSelect) ){
! 80774: if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
! 80775: }else{
! 80776: if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
! 80777: }
! 80778: if( sqlite3FixExpr(pFix, pExpr->pRight) ){
! 80779: return 1;
! 80780: }
! 80781: pExpr = pExpr->pLeft;
! 80782: }
! 80783: return 0;
! 80784: }
! 80785: SQLITE_PRIVATE int sqlite3FixExprList(
! 80786: DbFixer *pFix, /* Context of the fixation */
! 80787: ExprList *pList /* The expression to be fixed to one database */
! 80788: ){
! 80789: int i;
! 80790: struct ExprList_item *pItem;
! 80791: if( pList==0 ) return 0;
! 80792: for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
! 80793: if( sqlite3FixExpr(pFix, pItem->pExpr) ){
! 80794: return 1;
! 80795: }
! 80796: }
! 80797: return 0;
! 80798: }
! 80799: #endif
! 80800:
! 80801: #ifndef SQLITE_OMIT_TRIGGER
! 80802: SQLITE_PRIVATE int sqlite3FixTriggerStep(
! 80803: DbFixer *pFix, /* Context of the fixation */
! 80804: TriggerStep *pStep /* The trigger step be fixed to one database */
! 80805: ){
! 80806: while( pStep ){
! 80807: if( sqlite3FixSelect(pFix, pStep->pSelect) ){
! 80808: return 1;
! 80809: }
! 80810: if( sqlite3FixExpr(pFix, pStep->pWhere) ){
! 80811: return 1;
! 80812: }
! 80813: if( sqlite3FixExprList(pFix, pStep->pExprList) ){
! 80814: return 1;
! 80815: }
! 80816: pStep = pStep->pNext;
! 80817: }
! 80818: return 0;
! 80819: }
! 80820: #endif
! 80821:
! 80822: /************** End of attach.c **********************************************/
! 80823: /************** Begin file auth.c ********************************************/
! 80824: /*
! 80825: ** 2003 January 11
! 80826: **
! 80827: ** The author disclaims copyright to this source code. In place of
! 80828: ** a legal notice, here is a blessing:
! 80829: **
! 80830: ** May you do good and not evil.
! 80831: ** May you find forgiveness for yourself and forgive others.
! 80832: ** May you share freely, never taking more than you give.
! 80833: **
! 80834: *************************************************************************
! 80835: ** This file contains code used to implement the sqlite3_set_authorizer()
! 80836: ** API. This facility is an optional feature of the library. Embedded
! 80837: ** systems that do not need this facility may omit it by recompiling
! 80838: ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
! 80839: */
! 80840:
! 80841: /*
! 80842: ** All of the code in this file may be omitted by defining a single
! 80843: ** macro.
! 80844: */
! 80845: #ifndef SQLITE_OMIT_AUTHORIZATION
! 80846:
! 80847: /*
! 80848: ** Set or clear the access authorization function.
! 80849: **
! 80850: ** The access authorization function is be called during the compilation
! 80851: ** phase to verify that the user has read and/or write access permission on
! 80852: ** various fields of the database. The first argument to the auth function
! 80853: ** is a copy of the 3rd argument to this routine. The second argument
! 80854: ** to the auth function is one of these constants:
! 80855: **
! 80856: ** SQLITE_CREATE_INDEX
! 80857: ** SQLITE_CREATE_TABLE
! 80858: ** SQLITE_CREATE_TEMP_INDEX
! 80859: ** SQLITE_CREATE_TEMP_TABLE
! 80860: ** SQLITE_CREATE_TEMP_TRIGGER
! 80861: ** SQLITE_CREATE_TEMP_VIEW
! 80862: ** SQLITE_CREATE_TRIGGER
! 80863: ** SQLITE_CREATE_VIEW
! 80864: ** SQLITE_DELETE
! 80865: ** SQLITE_DROP_INDEX
! 80866: ** SQLITE_DROP_TABLE
! 80867: ** SQLITE_DROP_TEMP_INDEX
! 80868: ** SQLITE_DROP_TEMP_TABLE
! 80869: ** SQLITE_DROP_TEMP_TRIGGER
! 80870: ** SQLITE_DROP_TEMP_VIEW
! 80871: ** SQLITE_DROP_TRIGGER
! 80872: ** SQLITE_DROP_VIEW
! 80873: ** SQLITE_INSERT
! 80874: ** SQLITE_PRAGMA
! 80875: ** SQLITE_READ
! 80876: ** SQLITE_SELECT
! 80877: ** SQLITE_TRANSACTION
! 80878: ** SQLITE_UPDATE
! 80879: **
! 80880: ** The third and fourth arguments to the auth function are the name of
! 80881: ** the table and the column that are being accessed. The auth function
! 80882: ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
! 80883: ** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
! 80884: ** means that the SQL statement will never-run - the sqlite3_exec() call
! 80885: ** will return with an error. SQLITE_IGNORE means that the SQL statement
! 80886: ** should run but attempts to read the specified column will return NULL
! 80887: ** and attempts to write the column will be ignored.
! 80888: **
! 80889: ** Setting the auth function to NULL disables this hook. The default
! 80890: ** setting of the auth function is NULL.
! 80891: */
! 80892: SQLITE_API int sqlite3_set_authorizer(
! 80893: sqlite3 *db,
! 80894: int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
! 80895: void *pArg
! 80896: ){
! 80897: sqlite3_mutex_enter(db->mutex);
! 80898: db->xAuth = xAuth;
! 80899: db->pAuthArg = pArg;
! 80900: sqlite3ExpirePreparedStatements(db);
! 80901: sqlite3_mutex_leave(db->mutex);
! 80902: return SQLITE_OK;
! 80903: }
! 80904:
! 80905: /*
! 80906: ** Write an error message into pParse->zErrMsg that explains that the
! 80907: ** user-supplied authorization function returned an illegal value.
! 80908: */
! 80909: static void sqliteAuthBadReturnCode(Parse *pParse){
! 80910: sqlite3ErrorMsg(pParse, "authorizer malfunction");
! 80911: pParse->rc = SQLITE_ERROR;
! 80912: }
! 80913:
! 80914: /*
! 80915: ** Invoke the authorization callback for permission to read column zCol from
! 80916: ** table zTab in database zDb. This function assumes that an authorization
! 80917: ** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
! 80918: **
! 80919: ** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
! 80920: ** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
! 80921: ** is treated as SQLITE_DENY. In this case an error is left in pParse.
! 80922: */
! 80923: SQLITE_PRIVATE int sqlite3AuthReadCol(
! 80924: Parse *pParse, /* The parser context */
! 80925: const char *zTab, /* Table name */
! 80926: const char *zCol, /* Column name */
! 80927: int iDb /* Index of containing database. */
! 80928: ){
! 80929: sqlite3 *db = pParse->db; /* Database handle */
! 80930: char *zDb = db->aDb[iDb].zName; /* Name of attached database */
! 80931: int rc; /* Auth callback return code */
! 80932:
! 80933: rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
! 80934: if( rc==SQLITE_DENY ){
! 80935: if( db->nDb>2 || iDb!=0 ){
! 80936: sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
! 80937: }else{
! 80938: sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
! 80939: }
! 80940: pParse->rc = SQLITE_AUTH;
! 80941: }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
! 80942: sqliteAuthBadReturnCode(pParse);
! 80943: }
! 80944: return rc;
! 80945: }
! 80946:
! 80947: /*
! 80948: ** The pExpr should be a TK_COLUMN expression. The table referred to
! 80949: ** is in pTabList or else it is the NEW or OLD table of a trigger.
! 80950: ** Check to see if it is OK to read this particular column.
! 80951: **
! 80952: ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
! 80953: ** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
! 80954: ** then generate an error.
! 80955: */
! 80956: SQLITE_PRIVATE void sqlite3AuthRead(
! 80957: Parse *pParse, /* The parser context */
! 80958: Expr *pExpr, /* The expression to check authorization on */
! 80959: Schema *pSchema, /* The schema of the expression */
! 80960: SrcList *pTabList /* All table that pExpr might refer to */
! 80961: ){
! 80962: sqlite3 *db = pParse->db;
! 80963: Table *pTab = 0; /* The table being read */
! 80964: const char *zCol; /* Name of the column of the table */
! 80965: int iSrc; /* Index in pTabList->a[] of table being read */
! 80966: int iDb; /* The index of the database the expression refers to */
! 80967: int iCol; /* Index of column in table */
! 80968:
! 80969: if( db->xAuth==0 ) return;
! 80970: iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
! 80971: if( iDb<0 ){
! 80972: /* An attempt to read a column out of a subquery or other
! 80973: ** temporary table. */
! 80974: return;
! 80975: }
! 80976:
! 80977: assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
! 80978: if( pExpr->op==TK_TRIGGER ){
! 80979: pTab = pParse->pTriggerTab;
! 80980: }else{
! 80981: assert( pTabList );
! 80982: for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
! 80983: if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
! 80984: pTab = pTabList->a[iSrc].pTab;
! 80985: break;
! 80986: }
! 80987: }
! 80988: }
! 80989: iCol = pExpr->iColumn;
! 80990: if( NEVER(pTab==0) ) return;
! 80991:
! 80992: if( iCol>=0 ){
! 80993: assert( iCol<pTab->nCol );
! 80994: zCol = pTab->aCol[iCol].zName;
! 80995: }else if( pTab->iPKey>=0 ){
! 80996: assert( pTab->iPKey<pTab->nCol );
! 80997: zCol = pTab->aCol[pTab->iPKey].zName;
! 80998: }else{
! 80999: zCol = "ROWID";
! 81000: }
! 81001: assert( iDb>=0 && iDb<db->nDb );
! 81002: if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
! 81003: pExpr->op = TK_NULL;
! 81004: }
! 81005: }
! 81006:
! 81007: /*
! 81008: ** Do an authorization check using the code and arguments given. Return
! 81009: ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
! 81010: ** is returned, then the error count and error message in pParse are
! 81011: ** modified appropriately.
! 81012: */
! 81013: SQLITE_PRIVATE int sqlite3AuthCheck(
! 81014: Parse *pParse,
! 81015: int code,
! 81016: const char *zArg1,
! 81017: const char *zArg2,
! 81018: const char *zArg3
! 81019: ){
! 81020: sqlite3 *db = pParse->db;
! 81021: int rc;
! 81022:
! 81023: /* Don't do any authorization checks if the database is initialising
! 81024: ** or if the parser is being invoked from within sqlite3_declare_vtab.
! 81025: */
! 81026: if( db->init.busy || IN_DECLARE_VTAB ){
! 81027: return SQLITE_OK;
! 81028: }
! 81029:
! 81030: if( db->xAuth==0 ){
! 81031: return SQLITE_OK;
! 81032: }
! 81033: rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
! 81034: if( rc==SQLITE_DENY ){
! 81035: sqlite3ErrorMsg(pParse, "not authorized");
! 81036: pParse->rc = SQLITE_AUTH;
! 81037: }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
! 81038: rc = SQLITE_DENY;
! 81039: sqliteAuthBadReturnCode(pParse);
! 81040: }
! 81041: return rc;
! 81042: }
! 81043:
! 81044: /*
! 81045: ** Push an authorization context. After this routine is called, the
! 81046: ** zArg3 argument to authorization callbacks will be zContext until
! 81047: ** popped. Or if pParse==0, this routine is a no-op.
! 81048: */
! 81049: SQLITE_PRIVATE void sqlite3AuthContextPush(
! 81050: Parse *pParse,
! 81051: AuthContext *pContext,
! 81052: const char *zContext
! 81053: ){
! 81054: assert( pParse );
! 81055: pContext->pParse = pParse;
! 81056: pContext->zAuthContext = pParse->zAuthContext;
! 81057: pParse->zAuthContext = zContext;
! 81058: }
! 81059:
! 81060: /*
! 81061: ** Pop an authorization context that was previously pushed
! 81062: ** by sqlite3AuthContextPush
! 81063: */
! 81064: SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
! 81065: if( pContext->pParse ){
! 81066: pContext->pParse->zAuthContext = pContext->zAuthContext;
! 81067: pContext->pParse = 0;
! 81068: }
! 81069: }
! 81070:
! 81071: #endif /* SQLITE_OMIT_AUTHORIZATION */
! 81072:
! 81073: /************** End of auth.c ************************************************/
! 81074: /************** Begin file build.c *******************************************/
! 81075: /*
! 81076: ** 2001 September 15
! 81077: **
! 81078: ** The author disclaims copyright to this source code. In place of
! 81079: ** a legal notice, here is a blessing:
! 81080: **
! 81081: ** May you do good and not evil.
! 81082: ** May you find forgiveness for yourself and forgive others.
! 81083: ** May you share freely, never taking more than you give.
! 81084: **
! 81085: *************************************************************************
! 81086: ** This file contains C code routines that are called by the SQLite parser
! 81087: ** when syntax rules are reduced. The routines in this file handle the
! 81088: ** following kinds of SQL syntax:
! 81089: **
! 81090: ** CREATE TABLE
! 81091: ** DROP TABLE
! 81092: ** CREATE INDEX
! 81093: ** DROP INDEX
! 81094: ** creating ID lists
! 81095: ** BEGIN TRANSACTION
! 81096: ** COMMIT
! 81097: ** ROLLBACK
! 81098: */
! 81099:
! 81100: /*
! 81101: ** This routine is called when a new SQL statement is beginning to
! 81102: ** be parsed. Initialize the pParse structure as needed.
! 81103: */
! 81104: SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
! 81105: pParse->explain = (u8)explainFlag;
! 81106: pParse->nVar = 0;
! 81107: }
! 81108:
! 81109: #ifndef SQLITE_OMIT_SHARED_CACHE
! 81110: /*
! 81111: ** The TableLock structure is only used by the sqlite3TableLock() and
! 81112: ** codeTableLocks() functions.
! 81113: */
! 81114: struct TableLock {
! 81115: int iDb; /* The database containing the table to be locked */
! 81116: int iTab; /* The root page of the table to be locked */
! 81117: u8 isWriteLock; /* True for write lock. False for a read lock */
! 81118: const char *zName; /* Name of the table */
! 81119: };
! 81120:
! 81121: /*
! 81122: ** Record the fact that we want to lock a table at run-time.
! 81123: **
! 81124: ** The table to be locked has root page iTab and is found in database iDb.
! 81125: ** A read or a write lock can be taken depending on isWritelock.
! 81126: **
! 81127: ** This routine just records the fact that the lock is desired. The
! 81128: ** code to make the lock occur is generated by a later call to
! 81129: ** codeTableLocks() which occurs during sqlite3FinishCoding().
! 81130: */
! 81131: SQLITE_PRIVATE void sqlite3TableLock(
! 81132: Parse *pParse, /* Parsing context */
! 81133: int iDb, /* Index of the database containing the table to lock */
! 81134: int iTab, /* Root page number of the table to be locked */
! 81135: u8 isWriteLock, /* True for a write lock */
! 81136: const char *zName /* Name of the table to be locked */
! 81137: ){
! 81138: Parse *pToplevel = sqlite3ParseToplevel(pParse);
! 81139: int i;
! 81140: int nBytes;
! 81141: TableLock *p;
! 81142: assert( iDb>=0 );
! 81143:
! 81144: for(i=0; i<pToplevel->nTableLock; i++){
! 81145: p = &pToplevel->aTableLock[i];
! 81146: if( p->iDb==iDb && p->iTab==iTab ){
! 81147: p->isWriteLock = (p->isWriteLock || isWriteLock);
! 81148: return;
! 81149: }
! 81150: }
! 81151:
! 81152: nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
! 81153: pToplevel->aTableLock =
! 81154: sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
! 81155: if( pToplevel->aTableLock ){
! 81156: p = &pToplevel->aTableLock[pToplevel->nTableLock++];
! 81157: p->iDb = iDb;
! 81158: p->iTab = iTab;
! 81159: p->isWriteLock = isWriteLock;
! 81160: p->zName = zName;
! 81161: }else{
! 81162: pToplevel->nTableLock = 0;
! 81163: pToplevel->db->mallocFailed = 1;
! 81164: }
! 81165: }
! 81166:
! 81167: /*
! 81168: ** Code an OP_TableLock instruction for each table locked by the
! 81169: ** statement (configured by calls to sqlite3TableLock()).
! 81170: */
! 81171: static void codeTableLocks(Parse *pParse){
! 81172: int i;
! 81173: Vdbe *pVdbe;
! 81174:
! 81175: pVdbe = sqlite3GetVdbe(pParse);
! 81176: assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
! 81177:
! 81178: for(i=0; i<pParse->nTableLock; i++){
! 81179: TableLock *p = &pParse->aTableLock[i];
! 81180: int p1 = p->iDb;
! 81181: sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
! 81182: p->zName, P4_STATIC);
! 81183: }
! 81184: }
! 81185: #else
! 81186: #define codeTableLocks(x)
! 81187: #endif
! 81188:
! 81189: /*
! 81190: ** This routine is called after a single SQL statement has been
! 81191: ** parsed and a VDBE program to execute that statement has been
! 81192: ** prepared. This routine puts the finishing touches on the
! 81193: ** VDBE program and resets the pParse structure for the next
! 81194: ** parse.
! 81195: **
! 81196: ** Note that if an error occurred, it might be the case that
! 81197: ** no VDBE code was generated.
! 81198: */
! 81199: SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
! 81200: sqlite3 *db;
! 81201: Vdbe *v;
! 81202:
! 81203: db = pParse->db;
! 81204: if( db->mallocFailed ) return;
! 81205: if( pParse->nested ) return;
! 81206: if( pParse->nErr ) return;
! 81207:
! 81208: /* Begin by generating some termination code at the end of the
! 81209: ** vdbe program
! 81210: */
! 81211: v = sqlite3GetVdbe(pParse);
! 81212: assert( !pParse->isMultiWrite
! 81213: || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
! 81214: if( v ){
! 81215: sqlite3VdbeAddOp0(v, OP_Halt);
! 81216:
! 81217: /* The cookie mask contains one bit for each database file open.
! 81218: ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
! 81219: ** set for each database that is used. Generate code to start a
! 81220: ** transaction on each used database and to verify the schema cookie
! 81221: ** on each used database.
! 81222: */
! 81223: if( pParse->cookieGoto>0 ){
! 81224: yDbMask mask;
! 81225: int iDb;
! 81226: sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
! 81227: for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
! 81228: if( (mask & pParse->cookieMask)==0 ) continue;
! 81229: sqlite3VdbeUsesBtree(v, iDb);
! 81230: sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
! 81231: if( db->init.busy==0 ){
! 81232: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 81233: sqlite3VdbeAddOp3(v, OP_VerifyCookie,
! 81234: iDb, pParse->cookieValue[iDb],
! 81235: db->aDb[iDb].pSchema->iGeneration);
! 81236: }
! 81237: }
! 81238: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 81239: {
! 81240: int i;
! 81241: for(i=0; i<pParse->nVtabLock; i++){
! 81242: char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
! 81243: sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
! 81244: }
! 81245: pParse->nVtabLock = 0;
! 81246: }
! 81247: #endif
! 81248:
! 81249: /* Once all the cookies have been verified and transactions opened,
! 81250: ** obtain the required table-locks. This is a no-op unless the
! 81251: ** shared-cache feature is enabled.
! 81252: */
! 81253: codeTableLocks(pParse);
! 81254:
! 81255: /* Initialize any AUTOINCREMENT data structures required.
! 81256: */
! 81257: sqlite3AutoincrementBegin(pParse);
! 81258:
! 81259: /* Finally, jump back to the beginning of the executable code. */
! 81260: sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
! 81261: }
! 81262: }
! 81263:
! 81264:
! 81265: /* Get the VDBE program ready for execution
! 81266: */
! 81267: if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
! 81268: #ifdef SQLITE_DEBUG
! 81269: FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
! 81270: sqlite3VdbeTrace(v, trace);
! 81271: #endif
! 81272: assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */
! 81273: /* A minimum of one cursor is required if autoincrement is used
! 81274: * See ticket [a696379c1f08866] */
! 81275: if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
! 81276: sqlite3VdbeMakeReady(v, pParse);
! 81277: pParse->rc = SQLITE_DONE;
! 81278: pParse->colNamesSet = 0;
! 81279: }else{
! 81280: pParse->rc = SQLITE_ERROR;
! 81281: }
! 81282: pParse->nTab = 0;
! 81283: pParse->nMem = 0;
! 81284: pParse->nSet = 0;
! 81285: pParse->nVar = 0;
! 81286: pParse->cookieMask = 0;
! 81287: pParse->cookieGoto = 0;
! 81288: }
! 81289:
! 81290: /*
! 81291: ** Run the parser and code generator recursively in order to generate
! 81292: ** code for the SQL statement given onto the end of the pParse context
! 81293: ** currently under construction. When the parser is run recursively
! 81294: ** this way, the final OP_Halt is not appended and other initialization
! 81295: ** and finalization steps are omitted because those are handling by the
! 81296: ** outermost parser.
! 81297: **
! 81298: ** Not everything is nestable. This facility is designed to permit
! 81299: ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
! 81300: ** care if you decide to try to use this routine for some other purposes.
! 81301: */
! 81302: SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
! 81303: va_list ap;
! 81304: char *zSql;
! 81305: char *zErrMsg = 0;
! 81306: sqlite3 *db = pParse->db;
! 81307: # define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar))
! 81308: char saveBuf[SAVE_SZ];
! 81309:
! 81310: if( pParse->nErr ) return;
! 81311: assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
! 81312: va_start(ap, zFormat);
! 81313: zSql = sqlite3VMPrintf(db, zFormat, ap);
! 81314: va_end(ap);
! 81315: if( zSql==0 ){
! 81316: return; /* A malloc must have failed */
! 81317: }
! 81318: pParse->nested++;
! 81319: memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
! 81320: memset(&pParse->nVar, 0, SAVE_SZ);
! 81321: sqlite3RunParser(pParse, zSql, &zErrMsg);
! 81322: sqlite3DbFree(db, zErrMsg);
! 81323: sqlite3DbFree(db, zSql);
! 81324: memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
! 81325: pParse->nested--;
! 81326: }
! 81327:
! 81328: /*
! 81329: ** Locate the in-memory structure that describes a particular database
! 81330: ** table given the name of that table and (optionally) the name of the
! 81331: ** database containing the table. Return NULL if not found.
! 81332: **
! 81333: ** If zDatabase is 0, all databases are searched for the table and the
! 81334: ** first matching table is returned. (No checking for duplicate table
! 81335: ** names is done.) The search order is TEMP first, then MAIN, then any
! 81336: ** auxiliary databases added using the ATTACH command.
! 81337: **
! 81338: ** See also sqlite3LocateTable().
! 81339: */
! 81340: SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
! 81341: Table *p = 0;
! 81342: int i;
! 81343: int nName;
! 81344: assert( zName!=0 );
! 81345: nName = sqlite3Strlen30(zName);
! 81346: /* All mutexes are required for schema access. Make sure we hold them. */
! 81347: assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
! 81348: for(i=OMIT_TEMPDB; i<db->nDb; i++){
! 81349: int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
! 81350: if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
! 81351: assert( sqlite3SchemaMutexHeld(db, j, 0) );
! 81352: p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
! 81353: if( p ) break;
! 81354: }
! 81355: return p;
! 81356: }
! 81357:
! 81358: /*
! 81359: ** Locate the in-memory structure that describes a particular database
! 81360: ** table given the name of that table and (optionally) the name of the
! 81361: ** database containing the table. Return NULL if not found. Also leave an
! 81362: ** error message in pParse->zErrMsg.
! 81363: **
! 81364: ** The difference between this routine and sqlite3FindTable() is that this
! 81365: ** routine leaves an error message in pParse->zErrMsg where
! 81366: ** sqlite3FindTable() does not.
! 81367: */
! 81368: SQLITE_PRIVATE Table *sqlite3LocateTable(
! 81369: Parse *pParse, /* context in which to report errors */
! 81370: int isView, /* True if looking for a VIEW rather than a TABLE */
! 81371: const char *zName, /* Name of the table we are looking for */
! 81372: const char *zDbase /* Name of the database. Might be NULL */
! 81373: ){
! 81374: Table *p;
! 81375:
! 81376: /* Read the database schema. If an error occurs, leave an error message
! 81377: ** and code in pParse and return NULL. */
! 81378: if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
! 81379: return 0;
! 81380: }
! 81381:
! 81382: p = sqlite3FindTable(pParse->db, zName, zDbase);
! 81383: if( p==0 ){
! 81384: const char *zMsg = isView ? "no such view" : "no such table";
! 81385: if( zDbase ){
! 81386: sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
! 81387: }else{
! 81388: sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
! 81389: }
! 81390: pParse->checkSchema = 1;
! 81391: }
! 81392: return p;
! 81393: }
! 81394:
! 81395: /*
! 81396: ** Locate the in-memory structure that describes
! 81397: ** a particular index given the name of that index
! 81398: ** and the name of the database that contains the index.
! 81399: ** Return NULL if not found.
! 81400: **
! 81401: ** If zDatabase is 0, all databases are searched for the
! 81402: ** table and the first matching index is returned. (No checking
! 81403: ** for duplicate index names is done.) The search order is
! 81404: ** TEMP first, then MAIN, then any auxiliary databases added
! 81405: ** using the ATTACH command.
! 81406: */
! 81407: SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
! 81408: Index *p = 0;
! 81409: int i;
! 81410: int nName = sqlite3Strlen30(zName);
! 81411: /* All mutexes are required for schema access. Make sure we hold them. */
! 81412: assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
! 81413: for(i=OMIT_TEMPDB; i<db->nDb; i++){
! 81414: int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
! 81415: Schema *pSchema = db->aDb[j].pSchema;
! 81416: assert( pSchema );
! 81417: if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
! 81418: assert( sqlite3SchemaMutexHeld(db, j, 0) );
! 81419: p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
! 81420: if( p ) break;
! 81421: }
! 81422: return p;
! 81423: }
! 81424:
! 81425: /*
! 81426: ** Reclaim the memory used by an index
! 81427: */
! 81428: static void freeIndex(sqlite3 *db, Index *p){
! 81429: #ifndef SQLITE_OMIT_ANALYZE
! 81430: sqlite3DeleteIndexSamples(db, p);
! 81431: #endif
! 81432: sqlite3DbFree(db, p->zColAff);
! 81433: sqlite3DbFree(db, p);
! 81434: }
! 81435:
! 81436: /*
! 81437: ** For the index called zIdxName which is found in the database iDb,
! 81438: ** unlike that index from its Table then remove the index from
! 81439: ** the index hash table and free all memory structures associated
! 81440: ** with the index.
! 81441: */
! 81442: SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
! 81443: Index *pIndex;
! 81444: int len;
! 81445: Hash *pHash;
! 81446:
! 81447: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 81448: pHash = &db->aDb[iDb].pSchema->idxHash;
! 81449: len = sqlite3Strlen30(zIdxName);
! 81450: pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
! 81451: if( ALWAYS(pIndex) ){
! 81452: if( pIndex->pTable->pIndex==pIndex ){
! 81453: pIndex->pTable->pIndex = pIndex->pNext;
! 81454: }else{
! 81455: Index *p;
! 81456: /* Justification of ALWAYS(); The index must be on the list of
! 81457: ** indices. */
! 81458: p = pIndex->pTable->pIndex;
! 81459: while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
! 81460: if( ALWAYS(p && p->pNext==pIndex) ){
! 81461: p->pNext = pIndex->pNext;
! 81462: }
! 81463: }
! 81464: freeIndex(db, pIndex);
! 81465: }
! 81466: db->flags |= SQLITE_InternChanges;
! 81467: }
! 81468:
! 81469: /*
! 81470: ** Erase all schema information from the in-memory hash tables of
! 81471: ** a single database. This routine is called to reclaim memory
! 81472: ** before the database closes. It is also called during a rollback
! 81473: ** if there were schema changes during the transaction or if a
! 81474: ** schema-cookie mismatch occurs.
! 81475: **
! 81476: ** If iDb<0 then reset the internal schema tables for all database
! 81477: ** files. If iDb>=0 then reset the internal schema for only the
! 81478: ** single file indicated.
! 81479: */
! 81480: SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
! 81481: int i, j;
! 81482: assert( iDb<db->nDb );
! 81483:
! 81484: if( iDb>=0 ){
! 81485: /* Case 1: Reset the single schema identified by iDb */
! 81486: Db *pDb = &db->aDb[iDb];
! 81487: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 81488: assert( pDb->pSchema!=0 );
! 81489: sqlite3SchemaClear(pDb->pSchema);
! 81490:
! 81491: /* If any database other than TEMP is reset, then also reset TEMP
! 81492: ** since TEMP might be holding triggers that reference tables in the
! 81493: ** other database.
! 81494: */
! 81495: if( iDb!=1 ){
! 81496: pDb = &db->aDb[1];
! 81497: assert( pDb->pSchema!=0 );
! 81498: sqlite3SchemaClear(pDb->pSchema);
! 81499: }
! 81500: return;
! 81501: }
! 81502: /* Case 2 (from here to the end): Reset all schemas for all attached
! 81503: ** databases. */
! 81504: assert( iDb<0 );
! 81505: sqlite3BtreeEnterAll(db);
! 81506: for(i=0; i<db->nDb; i++){
! 81507: Db *pDb = &db->aDb[i];
! 81508: if( pDb->pSchema ){
! 81509: sqlite3SchemaClear(pDb->pSchema);
! 81510: }
! 81511: }
! 81512: db->flags &= ~SQLITE_InternChanges;
! 81513: sqlite3VtabUnlockList(db);
! 81514: sqlite3BtreeLeaveAll(db);
! 81515:
! 81516: /* If one or more of the auxiliary database files has been closed,
! 81517: ** then remove them from the auxiliary database list. We take the
! 81518: ** opportunity to do this here since we have just deleted all of the
! 81519: ** schema hash tables and therefore do not have to make any changes
! 81520: ** to any of those tables.
! 81521: */
! 81522: for(i=j=2; i<db->nDb; i++){
! 81523: struct Db *pDb = &db->aDb[i];
! 81524: if( pDb->pBt==0 ){
! 81525: sqlite3DbFree(db, pDb->zName);
! 81526: pDb->zName = 0;
! 81527: continue;
! 81528: }
! 81529: if( j<i ){
! 81530: db->aDb[j] = db->aDb[i];
! 81531: }
! 81532: j++;
! 81533: }
! 81534: memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
! 81535: db->nDb = j;
! 81536: if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
! 81537: memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
! 81538: sqlite3DbFree(db, db->aDb);
! 81539: db->aDb = db->aDbStatic;
! 81540: }
! 81541: }
! 81542:
! 81543: /*
! 81544: ** This routine is called when a commit occurs.
! 81545: */
! 81546: SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
! 81547: db->flags &= ~SQLITE_InternChanges;
! 81548: }
! 81549:
! 81550: /*
! 81551: ** Delete memory allocated for the column names of a table or view (the
! 81552: ** Table.aCol[] array).
! 81553: */
! 81554: static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
! 81555: int i;
! 81556: Column *pCol;
! 81557: assert( pTable!=0 );
! 81558: if( (pCol = pTable->aCol)!=0 ){
! 81559: for(i=0; i<pTable->nCol; i++, pCol++){
! 81560: sqlite3DbFree(db, pCol->zName);
! 81561: sqlite3ExprDelete(db, pCol->pDflt);
! 81562: sqlite3DbFree(db, pCol->zDflt);
! 81563: sqlite3DbFree(db, pCol->zType);
! 81564: sqlite3DbFree(db, pCol->zColl);
! 81565: }
! 81566: sqlite3DbFree(db, pTable->aCol);
! 81567: }
! 81568: }
! 81569:
! 81570: /*
! 81571: ** Remove the memory data structures associated with the given
! 81572: ** Table. No changes are made to disk by this routine.
! 81573: **
! 81574: ** This routine just deletes the data structure. It does not unlink
! 81575: ** the table data structure from the hash table. But it does destroy
! 81576: ** memory structures of the indices and foreign keys associated with
! 81577: ** the table.
! 81578: */
! 81579: SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
! 81580: Index *pIndex, *pNext;
! 81581:
! 81582: assert( !pTable || pTable->nRef>0 );
! 81583:
! 81584: /* Do not delete the table until the reference count reaches zero. */
! 81585: if( !pTable ) return;
! 81586: if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
! 81587:
! 81588: /* Delete all indices associated with this table. */
! 81589: for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
! 81590: pNext = pIndex->pNext;
! 81591: assert( pIndex->pSchema==pTable->pSchema );
! 81592: if( !db || db->pnBytesFreed==0 ){
! 81593: char *zName = pIndex->zName;
! 81594: TESTONLY ( Index *pOld = ) sqlite3HashInsert(
! 81595: &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
! 81596: );
! 81597: assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
! 81598: assert( pOld==pIndex || pOld==0 );
! 81599: }
! 81600: freeIndex(db, pIndex);
! 81601: }
! 81602:
! 81603: /* Delete any foreign keys attached to this table. */
! 81604: sqlite3FkDelete(db, pTable);
! 81605:
! 81606: /* Delete the Table structure itself.
! 81607: */
! 81608: sqliteDeleteColumnNames(db, pTable);
! 81609: sqlite3DbFree(db, pTable->zName);
! 81610: sqlite3DbFree(db, pTable->zColAff);
! 81611: sqlite3SelectDelete(db, pTable->pSelect);
! 81612: #ifndef SQLITE_OMIT_CHECK
! 81613: sqlite3ExprDelete(db, pTable->pCheck);
! 81614: #endif
! 81615: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 81616: sqlite3VtabClear(db, pTable);
! 81617: #endif
! 81618: sqlite3DbFree(db, pTable);
! 81619: }
! 81620:
! 81621: /*
! 81622: ** Unlink the given table from the hash tables and the delete the
! 81623: ** table structure with all its indices and foreign keys.
! 81624: */
! 81625: SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
! 81626: Table *p;
! 81627: Db *pDb;
! 81628:
! 81629: assert( db!=0 );
! 81630: assert( iDb>=0 && iDb<db->nDb );
! 81631: assert( zTabName );
! 81632: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 81633: testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
! 81634: pDb = &db->aDb[iDb];
! 81635: p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
! 81636: sqlite3Strlen30(zTabName),0);
! 81637: sqlite3DeleteTable(db, p);
! 81638: db->flags |= SQLITE_InternChanges;
! 81639: }
! 81640:
! 81641: /*
! 81642: ** Given a token, return a string that consists of the text of that
! 81643: ** token. Space to hold the returned string
! 81644: ** is obtained from sqliteMalloc() and must be freed by the calling
! 81645: ** function.
! 81646: **
! 81647: ** Any quotation marks (ex: "name", 'name', [name], or `name`) that
! 81648: ** surround the body of the token are removed.
! 81649: **
! 81650: ** Tokens are often just pointers into the original SQL text and so
! 81651: ** are not \000 terminated and are not persistent. The returned string
! 81652: ** is \000 terminated and is persistent.
! 81653: */
! 81654: SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
! 81655: char *zName;
! 81656: if( pName ){
! 81657: zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
! 81658: sqlite3Dequote(zName);
! 81659: }else{
! 81660: zName = 0;
! 81661: }
! 81662: return zName;
! 81663: }
! 81664:
! 81665: /*
! 81666: ** Open the sqlite_master table stored in database number iDb for
! 81667: ** writing. The table is opened using cursor 0.
! 81668: */
! 81669: SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
! 81670: Vdbe *v = sqlite3GetVdbe(p);
! 81671: sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
! 81672: sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb);
! 81673: sqlite3VdbeChangeP4(v, -1, (char *)5, P4_INT32); /* 5 column table */
! 81674: if( p->nTab==0 ){
! 81675: p->nTab = 1;
! 81676: }
! 81677: }
! 81678:
! 81679: /*
! 81680: ** Parameter zName points to a nul-terminated buffer containing the name
! 81681: ** of a database ("main", "temp" or the name of an attached db). This
! 81682: ** function returns the index of the named database in db->aDb[], or
! 81683: ** -1 if the named db cannot be found.
! 81684: */
! 81685: SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
! 81686: int i = -1; /* Database number */
! 81687: if( zName ){
! 81688: Db *pDb;
! 81689: int n = sqlite3Strlen30(zName);
! 81690: for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
! 81691: if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) &&
! 81692: 0==sqlite3StrICmp(pDb->zName, zName) ){
! 81693: break;
! 81694: }
! 81695: }
! 81696: }
! 81697: return i;
! 81698: }
! 81699:
! 81700: /*
! 81701: ** The token *pName contains the name of a database (either "main" or
! 81702: ** "temp" or the name of an attached db). This routine returns the
! 81703: ** index of the named database in db->aDb[], or -1 if the named db
! 81704: ** does not exist.
! 81705: */
! 81706: SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
! 81707: int i; /* Database number */
! 81708: char *zName; /* Name we are searching for */
! 81709: zName = sqlite3NameFromToken(db, pName);
! 81710: i = sqlite3FindDbName(db, zName);
! 81711: sqlite3DbFree(db, zName);
! 81712: return i;
! 81713: }
! 81714:
! 81715: /* The table or view or trigger name is passed to this routine via tokens
! 81716: ** pName1 and pName2. If the table name was fully qualified, for example:
! 81717: **
! 81718: ** CREATE TABLE xxx.yyy (...);
! 81719: **
! 81720: ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
! 81721: ** the table name is not fully qualified, i.e.:
! 81722: **
! 81723: ** CREATE TABLE yyy(...);
! 81724: **
! 81725: ** Then pName1 is set to "yyy" and pName2 is "".
! 81726: **
! 81727: ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
! 81728: ** pName2) that stores the unqualified table name. The index of the
! 81729: ** database "xxx" is returned.
! 81730: */
! 81731: SQLITE_PRIVATE int sqlite3TwoPartName(
! 81732: Parse *pParse, /* Parsing and code generating context */
! 81733: Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
! 81734: Token *pName2, /* The "yyy" in the name "xxx.yyy" */
! 81735: Token **pUnqual /* Write the unqualified object name here */
! 81736: ){
! 81737: int iDb; /* Database holding the object */
! 81738: sqlite3 *db = pParse->db;
! 81739:
! 81740: if( ALWAYS(pName2!=0) && pName2->n>0 ){
! 81741: if( db->init.busy ) {
! 81742: sqlite3ErrorMsg(pParse, "corrupt database");
! 81743: pParse->nErr++;
! 81744: return -1;
! 81745: }
! 81746: *pUnqual = pName2;
! 81747: iDb = sqlite3FindDb(db, pName1);
! 81748: if( iDb<0 ){
! 81749: sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
! 81750: pParse->nErr++;
! 81751: return -1;
! 81752: }
! 81753: }else{
! 81754: assert( db->init.iDb==0 || db->init.busy );
! 81755: iDb = db->init.iDb;
! 81756: *pUnqual = pName1;
! 81757: }
! 81758: return iDb;
! 81759: }
! 81760:
! 81761: /*
! 81762: ** This routine is used to check if the UTF-8 string zName is a legal
! 81763: ** unqualified name for a new schema object (table, index, view or
! 81764: ** trigger). All names are legal except those that begin with the string
! 81765: ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
! 81766: ** is reserved for internal use.
! 81767: */
! 81768: SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
! 81769: if( !pParse->db->init.busy && pParse->nested==0
! 81770: && (pParse->db->flags & SQLITE_WriteSchema)==0
! 81771: && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
! 81772: sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
! 81773: return SQLITE_ERROR;
! 81774: }
! 81775: return SQLITE_OK;
! 81776: }
! 81777:
! 81778: /*
! 81779: ** Begin constructing a new table representation in memory. This is
! 81780: ** the first of several action routines that get called in response
! 81781: ** to a CREATE TABLE statement. In particular, this routine is called
! 81782: ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
! 81783: ** flag is true if the table should be stored in the auxiliary database
! 81784: ** file instead of in the main database file. This is normally the case
! 81785: ** when the "TEMP" or "TEMPORARY" keyword occurs in between
! 81786: ** CREATE and TABLE.
! 81787: **
! 81788: ** The new table record is initialized and put in pParse->pNewTable.
! 81789: ** As more of the CREATE TABLE statement is parsed, additional action
! 81790: ** routines will be called to add more information to this record.
! 81791: ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
! 81792: ** is called to complete the construction of the new table record.
! 81793: */
! 81794: SQLITE_PRIVATE void sqlite3StartTable(
! 81795: Parse *pParse, /* Parser context */
! 81796: Token *pName1, /* First part of the name of the table or view */
! 81797: Token *pName2, /* Second part of the name of the table or view */
! 81798: int isTemp, /* True if this is a TEMP table */
! 81799: int isView, /* True if this is a VIEW */
! 81800: int isVirtual, /* True if this is a VIRTUAL table */
! 81801: int noErr /* Do nothing if table already exists */
! 81802: ){
! 81803: Table *pTable;
! 81804: char *zName = 0; /* The name of the new table */
! 81805: sqlite3 *db = pParse->db;
! 81806: Vdbe *v;
! 81807: int iDb; /* Database number to create the table in */
! 81808: Token *pName; /* Unqualified name of the table to create */
! 81809:
! 81810: /* The table or view name to create is passed to this routine via tokens
! 81811: ** pName1 and pName2. If the table name was fully qualified, for example:
! 81812: **
! 81813: ** CREATE TABLE xxx.yyy (...);
! 81814: **
! 81815: ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
! 81816: ** the table name is not fully qualified, i.e.:
! 81817: **
! 81818: ** CREATE TABLE yyy(...);
! 81819: **
! 81820: ** Then pName1 is set to "yyy" and pName2 is "".
! 81821: **
! 81822: ** The call below sets the pName pointer to point at the token (pName1 or
! 81823: ** pName2) that stores the unqualified table name. The variable iDb is
! 81824: ** set to the index of the database that the table or view is to be
! 81825: ** created in.
! 81826: */
! 81827: iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
! 81828: if( iDb<0 ) return;
! 81829: if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
! 81830: /* If creating a temp table, the name may not be qualified. Unless
! 81831: ** the database name is "temp" anyway. */
! 81832: sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
! 81833: return;
! 81834: }
! 81835: if( !OMIT_TEMPDB && isTemp ) iDb = 1;
! 81836:
! 81837: pParse->sNameToken = *pName;
! 81838: zName = sqlite3NameFromToken(db, pName);
! 81839: if( zName==0 ) return;
! 81840: if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
! 81841: goto begin_table_error;
! 81842: }
! 81843: if( db->init.iDb==1 ) isTemp = 1;
! 81844: #ifndef SQLITE_OMIT_AUTHORIZATION
! 81845: assert( (isTemp & 1)==isTemp );
! 81846: {
! 81847: int code;
! 81848: char *zDb = db->aDb[iDb].zName;
! 81849: if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
! 81850: goto begin_table_error;
! 81851: }
! 81852: if( isView ){
! 81853: if( !OMIT_TEMPDB && isTemp ){
! 81854: code = SQLITE_CREATE_TEMP_VIEW;
! 81855: }else{
! 81856: code = SQLITE_CREATE_VIEW;
! 81857: }
! 81858: }else{
! 81859: if( !OMIT_TEMPDB && isTemp ){
! 81860: code = SQLITE_CREATE_TEMP_TABLE;
! 81861: }else{
! 81862: code = SQLITE_CREATE_TABLE;
! 81863: }
! 81864: }
! 81865: if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
! 81866: goto begin_table_error;
! 81867: }
! 81868: }
! 81869: #endif
! 81870:
! 81871: /* Make sure the new table name does not collide with an existing
! 81872: ** index or table name in the same database. Issue an error message if
! 81873: ** it does. The exception is if the statement being parsed was passed
! 81874: ** to an sqlite3_declare_vtab() call. In that case only the column names
! 81875: ** and types will be used, so there is no need to test for namespace
! 81876: ** collisions.
! 81877: */
! 81878: if( !IN_DECLARE_VTAB ){
! 81879: char *zDb = db->aDb[iDb].zName;
! 81880: if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
! 81881: goto begin_table_error;
! 81882: }
! 81883: pTable = sqlite3FindTable(db, zName, zDb);
! 81884: if( pTable ){
! 81885: if( !noErr ){
! 81886: sqlite3ErrorMsg(pParse, "table %T already exists", pName);
! 81887: }else{
! 81888: assert( !db->init.busy );
! 81889: sqlite3CodeVerifySchema(pParse, iDb);
! 81890: }
! 81891: goto begin_table_error;
! 81892: }
! 81893: if( sqlite3FindIndex(db, zName, zDb)!=0 ){
! 81894: sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
! 81895: goto begin_table_error;
! 81896: }
! 81897: }
! 81898:
! 81899: pTable = sqlite3DbMallocZero(db, sizeof(Table));
! 81900: if( pTable==0 ){
! 81901: db->mallocFailed = 1;
! 81902: pParse->rc = SQLITE_NOMEM;
! 81903: pParse->nErr++;
! 81904: goto begin_table_error;
! 81905: }
! 81906: pTable->zName = zName;
! 81907: pTable->iPKey = -1;
! 81908: pTable->pSchema = db->aDb[iDb].pSchema;
! 81909: pTable->nRef = 1;
! 81910: pTable->nRowEst = 1000000;
! 81911: assert( pParse->pNewTable==0 );
! 81912: pParse->pNewTable = pTable;
! 81913:
! 81914: /* If this is the magic sqlite_sequence table used by autoincrement,
! 81915: ** then record a pointer to this table in the main database structure
! 81916: ** so that INSERT can find the table easily.
! 81917: */
! 81918: #ifndef SQLITE_OMIT_AUTOINCREMENT
! 81919: if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
! 81920: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 81921: pTable->pSchema->pSeqTab = pTable;
! 81922: }
! 81923: #endif
! 81924:
! 81925: /* Begin generating the code that will insert the table record into
! 81926: ** the SQLITE_MASTER table. Note in particular that we must go ahead
! 81927: ** and allocate the record number for the table entry now. Before any
! 81928: ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
! 81929: ** indices to be created and the table record must come before the
! 81930: ** indices. Hence, the record number for the table must be allocated
! 81931: ** now.
! 81932: */
! 81933: if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
! 81934: int j1;
! 81935: int fileFormat;
! 81936: int reg1, reg2, reg3;
! 81937: sqlite3BeginWriteOperation(pParse, 0, iDb);
! 81938:
! 81939: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 81940: if( isVirtual ){
! 81941: sqlite3VdbeAddOp0(v, OP_VBegin);
! 81942: }
! 81943: #endif
! 81944:
! 81945: /* If the file format and encoding in the database have not been set,
! 81946: ** set them now.
! 81947: */
! 81948: reg1 = pParse->regRowid = ++pParse->nMem;
! 81949: reg2 = pParse->regRoot = ++pParse->nMem;
! 81950: reg3 = ++pParse->nMem;
! 81951: sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
! 81952: sqlite3VdbeUsesBtree(v, iDb);
! 81953: j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
! 81954: fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
! 81955: 1 : SQLITE_MAX_FILE_FORMAT;
! 81956: sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
! 81957: sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
! 81958: sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
! 81959: sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
! 81960: sqlite3VdbeJumpHere(v, j1);
! 81961:
! 81962: /* This just creates a place-holder record in the sqlite_master table.
! 81963: ** The record created does not contain anything yet. It will be replaced
! 81964: ** by the real entry in code generated at sqlite3EndTable().
! 81965: **
! 81966: ** The rowid for the new entry is left in register pParse->regRowid.
! 81967: ** The root page number of the new table is left in reg pParse->regRoot.
! 81968: ** The rowid and root page number values are needed by the code that
! 81969: ** sqlite3EndTable will generate.
! 81970: */
! 81971: #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
! 81972: if( isView || isVirtual ){
! 81973: sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
! 81974: }else
! 81975: #endif
! 81976: {
! 81977: sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
! 81978: }
! 81979: sqlite3OpenMasterTable(pParse, iDb);
! 81980: sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
! 81981: sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
! 81982: sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
! 81983: sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
! 81984: sqlite3VdbeAddOp0(v, OP_Close);
! 81985: }
! 81986:
! 81987: /* Normal (non-error) return. */
! 81988: return;
! 81989:
! 81990: /* If an error occurs, we jump here */
! 81991: begin_table_error:
! 81992: sqlite3DbFree(db, zName);
! 81993: return;
! 81994: }
! 81995:
! 81996: /*
! 81997: ** This macro is used to compare two strings in a case-insensitive manner.
! 81998: ** It is slightly faster than calling sqlite3StrICmp() directly, but
! 81999: ** produces larger code.
! 82000: **
! 82001: ** WARNING: This macro is not compatible with the strcmp() family. It
! 82002: ** returns true if the two strings are equal, otherwise false.
! 82003: */
! 82004: #define STRICMP(x, y) (\
! 82005: sqlite3UpperToLower[*(unsigned char *)(x)]== \
! 82006: sqlite3UpperToLower[*(unsigned char *)(y)] \
! 82007: && sqlite3StrICmp((x)+1,(y)+1)==0 )
! 82008:
! 82009: /*
! 82010: ** Add a new column to the table currently being constructed.
! 82011: **
! 82012: ** The parser calls this routine once for each column declaration
! 82013: ** in a CREATE TABLE statement. sqlite3StartTable() gets called
! 82014: ** first to get things going. Then this routine is called for each
! 82015: ** column.
! 82016: */
! 82017: SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
! 82018: Table *p;
! 82019: int i;
! 82020: char *z;
! 82021: Column *pCol;
! 82022: sqlite3 *db = pParse->db;
! 82023: if( (p = pParse->pNewTable)==0 ) return;
! 82024: #if SQLITE_MAX_COLUMN
! 82025: if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
! 82026: sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
! 82027: return;
! 82028: }
! 82029: #endif
! 82030: z = sqlite3NameFromToken(db, pName);
! 82031: if( z==0 ) return;
! 82032: for(i=0; i<p->nCol; i++){
! 82033: if( STRICMP(z, p->aCol[i].zName) ){
! 82034: sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
! 82035: sqlite3DbFree(db, z);
! 82036: return;
! 82037: }
! 82038: }
! 82039: if( (p->nCol & 0x7)==0 ){
! 82040: Column *aNew;
! 82041: aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
! 82042: if( aNew==0 ){
! 82043: sqlite3DbFree(db, z);
! 82044: return;
! 82045: }
! 82046: p->aCol = aNew;
! 82047: }
! 82048: pCol = &p->aCol[p->nCol];
! 82049: memset(pCol, 0, sizeof(p->aCol[0]));
! 82050: pCol->zName = z;
! 82051:
! 82052: /* If there is no type specified, columns have the default affinity
! 82053: ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
! 82054: ** be called next to set pCol->affinity correctly.
! 82055: */
! 82056: pCol->affinity = SQLITE_AFF_NONE;
! 82057: p->nCol++;
! 82058: }
! 82059:
! 82060: /*
! 82061: ** This routine is called by the parser while in the middle of
! 82062: ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
! 82063: ** been seen on a column. This routine sets the notNull flag on
! 82064: ** the column currently under construction.
! 82065: */
! 82066: SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
! 82067: Table *p;
! 82068: p = pParse->pNewTable;
! 82069: if( p==0 || NEVER(p->nCol<1) ) return;
! 82070: p->aCol[p->nCol-1].notNull = (u8)onError;
! 82071: }
! 82072:
! 82073: /*
! 82074: ** Scan the column type name zType (length nType) and return the
! 82075: ** associated affinity type.
! 82076: **
! 82077: ** This routine does a case-independent search of zType for the
! 82078: ** substrings in the following table. If one of the substrings is
! 82079: ** found, the corresponding affinity is returned. If zType contains
! 82080: ** more than one of the substrings, entries toward the top of
! 82081: ** the table take priority. For example, if zType is 'BLOBINT',
! 82082: ** SQLITE_AFF_INTEGER is returned.
! 82083: **
! 82084: ** Substring | Affinity
! 82085: ** --------------------------------
! 82086: ** 'INT' | SQLITE_AFF_INTEGER
! 82087: ** 'CHAR' | SQLITE_AFF_TEXT
! 82088: ** 'CLOB' | SQLITE_AFF_TEXT
! 82089: ** 'TEXT' | SQLITE_AFF_TEXT
! 82090: ** 'BLOB' | SQLITE_AFF_NONE
! 82091: ** 'REAL' | SQLITE_AFF_REAL
! 82092: ** 'FLOA' | SQLITE_AFF_REAL
! 82093: ** 'DOUB' | SQLITE_AFF_REAL
! 82094: **
! 82095: ** If none of the substrings in the above table are found,
! 82096: ** SQLITE_AFF_NUMERIC is returned.
! 82097: */
! 82098: SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn){
! 82099: u32 h = 0;
! 82100: char aff = SQLITE_AFF_NUMERIC;
! 82101:
! 82102: if( zIn ) while( zIn[0] ){
! 82103: h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
! 82104: zIn++;
! 82105: if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
! 82106: aff = SQLITE_AFF_TEXT;
! 82107: }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
! 82108: aff = SQLITE_AFF_TEXT;
! 82109: }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
! 82110: aff = SQLITE_AFF_TEXT;
! 82111: }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
! 82112: && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
! 82113: aff = SQLITE_AFF_NONE;
! 82114: #ifndef SQLITE_OMIT_FLOATING_POINT
! 82115: }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
! 82116: && aff==SQLITE_AFF_NUMERIC ){
! 82117: aff = SQLITE_AFF_REAL;
! 82118: }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
! 82119: && aff==SQLITE_AFF_NUMERIC ){
! 82120: aff = SQLITE_AFF_REAL;
! 82121: }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
! 82122: && aff==SQLITE_AFF_NUMERIC ){
! 82123: aff = SQLITE_AFF_REAL;
! 82124: #endif
! 82125: }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
! 82126: aff = SQLITE_AFF_INTEGER;
! 82127: break;
! 82128: }
! 82129: }
! 82130:
! 82131: return aff;
! 82132: }
! 82133:
! 82134: /*
! 82135: ** This routine is called by the parser while in the middle of
! 82136: ** parsing a CREATE TABLE statement. The pFirst token is the first
! 82137: ** token in the sequence of tokens that describe the type of the
! 82138: ** column currently under construction. pLast is the last token
! 82139: ** in the sequence. Use this information to construct a string
! 82140: ** that contains the typename of the column and store that string
! 82141: ** in zType.
! 82142: */
! 82143: SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
! 82144: Table *p;
! 82145: Column *pCol;
! 82146:
! 82147: p = pParse->pNewTable;
! 82148: if( p==0 || NEVER(p->nCol<1) ) return;
! 82149: pCol = &p->aCol[p->nCol-1];
! 82150: assert( pCol->zType==0 );
! 82151: pCol->zType = sqlite3NameFromToken(pParse->db, pType);
! 82152: pCol->affinity = sqlite3AffinityType(pCol->zType);
! 82153: }
! 82154:
! 82155: /*
! 82156: ** The expression is the default value for the most recently added column
! 82157: ** of the table currently under construction.
! 82158: **
! 82159: ** Default value expressions must be constant. Raise an exception if this
! 82160: ** is not the case.
! 82161: **
! 82162: ** This routine is called by the parser while in the middle of
! 82163: ** parsing a CREATE TABLE statement.
! 82164: */
! 82165: SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
! 82166: Table *p;
! 82167: Column *pCol;
! 82168: sqlite3 *db = pParse->db;
! 82169: p = pParse->pNewTable;
! 82170: if( p!=0 ){
! 82171: pCol = &(p->aCol[p->nCol-1]);
! 82172: if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
! 82173: sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
! 82174: pCol->zName);
! 82175: }else{
! 82176: /* A copy of pExpr is used instead of the original, as pExpr contains
! 82177: ** tokens that point to volatile memory. The 'span' of the expression
! 82178: ** is required by pragma table_info.
! 82179: */
! 82180: sqlite3ExprDelete(db, pCol->pDflt);
! 82181: pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
! 82182: sqlite3DbFree(db, pCol->zDflt);
! 82183: pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
! 82184: (int)(pSpan->zEnd - pSpan->zStart));
! 82185: }
! 82186: }
! 82187: sqlite3ExprDelete(db, pSpan->pExpr);
! 82188: }
! 82189:
! 82190: /*
! 82191: ** Designate the PRIMARY KEY for the table. pList is a list of names
! 82192: ** of columns that form the primary key. If pList is NULL, then the
! 82193: ** most recently added column of the table is the primary key.
! 82194: **
! 82195: ** A table can have at most one primary key. If the table already has
! 82196: ** a primary key (and this is the second primary key) then create an
! 82197: ** error.
! 82198: **
! 82199: ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
! 82200: ** then we will try to use that column as the rowid. Set the Table.iPKey
! 82201: ** field of the table under construction to be the index of the
! 82202: ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
! 82203: ** no INTEGER PRIMARY KEY.
! 82204: **
! 82205: ** If the key is not an INTEGER PRIMARY KEY, then create a unique
! 82206: ** index for the key. No index is created for INTEGER PRIMARY KEYs.
! 82207: */
! 82208: SQLITE_PRIVATE void sqlite3AddPrimaryKey(
! 82209: Parse *pParse, /* Parsing context */
! 82210: ExprList *pList, /* List of field names to be indexed */
! 82211: int onError, /* What to do with a uniqueness conflict */
! 82212: int autoInc, /* True if the AUTOINCREMENT keyword is present */
! 82213: int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
! 82214: ){
! 82215: Table *pTab = pParse->pNewTable;
! 82216: char *zType = 0;
! 82217: int iCol = -1, i;
! 82218: if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
! 82219: if( pTab->tabFlags & TF_HasPrimaryKey ){
! 82220: sqlite3ErrorMsg(pParse,
! 82221: "table \"%s\" has more than one primary key", pTab->zName);
! 82222: goto primary_key_exit;
! 82223: }
! 82224: pTab->tabFlags |= TF_HasPrimaryKey;
! 82225: if( pList==0 ){
! 82226: iCol = pTab->nCol - 1;
! 82227: pTab->aCol[iCol].isPrimKey = 1;
! 82228: }else{
! 82229: for(i=0; i<pList->nExpr; i++){
! 82230: for(iCol=0; iCol<pTab->nCol; iCol++){
! 82231: if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
! 82232: break;
! 82233: }
! 82234: }
! 82235: if( iCol<pTab->nCol ){
! 82236: pTab->aCol[iCol].isPrimKey = 1;
! 82237: }
! 82238: }
! 82239: if( pList->nExpr>1 ) iCol = -1;
! 82240: }
! 82241: if( iCol>=0 && iCol<pTab->nCol ){
! 82242: zType = pTab->aCol[iCol].zType;
! 82243: }
! 82244: if( zType && sqlite3StrICmp(zType, "INTEGER")==0
! 82245: && sortOrder==SQLITE_SO_ASC ){
! 82246: pTab->iPKey = iCol;
! 82247: pTab->keyConf = (u8)onError;
! 82248: assert( autoInc==0 || autoInc==1 );
! 82249: pTab->tabFlags |= autoInc*TF_Autoincrement;
! 82250: }else if( autoInc ){
! 82251: #ifndef SQLITE_OMIT_AUTOINCREMENT
! 82252: sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
! 82253: "INTEGER PRIMARY KEY");
! 82254: #endif
! 82255: }else{
! 82256: Index *p;
! 82257: p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
! 82258: if( p ){
! 82259: p->autoIndex = 2;
! 82260: }
! 82261: pList = 0;
! 82262: }
! 82263:
! 82264: primary_key_exit:
! 82265: sqlite3ExprListDelete(pParse->db, pList);
! 82266: return;
! 82267: }
! 82268:
! 82269: /*
! 82270: ** Add a new CHECK constraint to the table currently under construction.
! 82271: */
! 82272: SQLITE_PRIVATE void sqlite3AddCheckConstraint(
! 82273: Parse *pParse, /* Parsing context */
! 82274: Expr *pCheckExpr /* The check expression */
! 82275: ){
! 82276: sqlite3 *db = pParse->db;
! 82277: #ifndef SQLITE_OMIT_CHECK
! 82278: Table *pTab = pParse->pNewTable;
! 82279: if( pTab && !IN_DECLARE_VTAB ){
! 82280: pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck, pCheckExpr);
! 82281: }else
! 82282: #endif
! 82283: {
! 82284: sqlite3ExprDelete(db, pCheckExpr);
! 82285: }
! 82286: }
! 82287:
! 82288: /*
! 82289: ** Set the collation function of the most recently parsed table column
! 82290: ** to the CollSeq given.
! 82291: */
! 82292: SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
! 82293: Table *p;
! 82294: int i;
! 82295: char *zColl; /* Dequoted name of collation sequence */
! 82296: sqlite3 *db;
! 82297:
! 82298: if( (p = pParse->pNewTable)==0 ) return;
! 82299: i = p->nCol-1;
! 82300: db = pParse->db;
! 82301: zColl = sqlite3NameFromToken(db, pToken);
! 82302: if( !zColl ) return;
! 82303:
! 82304: if( sqlite3LocateCollSeq(pParse, zColl) ){
! 82305: Index *pIdx;
! 82306: p->aCol[i].zColl = zColl;
! 82307:
! 82308: /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
! 82309: ** then an index may have been created on this column before the
! 82310: ** collation type was added. Correct this if it is the case.
! 82311: */
! 82312: for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
! 82313: assert( pIdx->nColumn==1 );
! 82314: if( pIdx->aiColumn[0]==i ){
! 82315: pIdx->azColl[0] = p->aCol[i].zColl;
! 82316: }
! 82317: }
! 82318: }else{
! 82319: sqlite3DbFree(db, zColl);
! 82320: }
! 82321: }
! 82322:
! 82323: /*
! 82324: ** This function returns the collation sequence for database native text
! 82325: ** encoding identified by the string zName, length nName.
! 82326: **
! 82327: ** If the requested collation sequence is not available, or not available
! 82328: ** in the database native encoding, the collation factory is invoked to
! 82329: ** request it. If the collation factory does not supply such a sequence,
! 82330: ** and the sequence is available in another text encoding, then that is
! 82331: ** returned instead.
! 82332: **
! 82333: ** If no versions of the requested collations sequence are available, or
! 82334: ** another error occurs, NULL is returned and an error message written into
! 82335: ** pParse.
! 82336: **
! 82337: ** This routine is a wrapper around sqlite3FindCollSeq(). This routine
! 82338: ** invokes the collation factory if the named collation cannot be found
! 82339: ** and generates an error message.
! 82340: **
! 82341: ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
! 82342: */
! 82343: SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
! 82344: sqlite3 *db = pParse->db;
! 82345: u8 enc = ENC(db);
! 82346: u8 initbusy = db->init.busy;
! 82347: CollSeq *pColl;
! 82348:
! 82349: pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
! 82350: if( !initbusy && (!pColl || !pColl->xCmp) ){
! 82351: pColl = sqlite3GetCollSeq(db, enc, pColl, zName);
! 82352: if( !pColl ){
! 82353: sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
! 82354: }
! 82355: }
! 82356:
! 82357: return pColl;
! 82358: }
! 82359:
! 82360:
! 82361: /*
! 82362: ** Generate code that will increment the schema cookie.
! 82363: **
! 82364: ** The schema cookie is used to determine when the schema for the
! 82365: ** database changes. After each schema change, the cookie value
! 82366: ** changes. When a process first reads the schema it records the
! 82367: ** cookie. Thereafter, whenever it goes to access the database,
! 82368: ** it checks the cookie to make sure the schema has not changed
! 82369: ** since it was last read.
! 82370: **
! 82371: ** This plan is not completely bullet-proof. It is possible for
! 82372: ** the schema to change multiple times and for the cookie to be
! 82373: ** set back to prior value. But schema changes are infrequent
! 82374: ** and the probability of hitting the same cookie value is only
! 82375: ** 1 chance in 2^32. So we're safe enough.
! 82376: */
! 82377: SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
! 82378: int r1 = sqlite3GetTempReg(pParse);
! 82379: sqlite3 *db = pParse->db;
! 82380: Vdbe *v = pParse->pVdbe;
! 82381: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 82382: sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
! 82383: sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
! 82384: sqlite3ReleaseTempReg(pParse, r1);
! 82385: }
! 82386:
! 82387: /*
! 82388: ** Measure the number of characters needed to output the given
! 82389: ** identifier. The number returned includes any quotes used
! 82390: ** but does not include the null terminator.
! 82391: **
! 82392: ** The estimate is conservative. It might be larger that what is
! 82393: ** really needed.
! 82394: */
! 82395: static int identLength(const char *z){
! 82396: int n;
! 82397: for(n=0; *z; n++, z++){
! 82398: if( *z=='"' ){ n++; }
! 82399: }
! 82400: return n + 2;
! 82401: }
! 82402:
! 82403: /*
! 82404: ** The first parameter is a pointer to an output buffer. The second
! 82405: ** parameter is a pointer to an integer that contains the offset at
! 82406: ** which to write into the output buffer. This function copies the
! 82407: ** nul-terminated string pointed to by the third parameter, zSignedIdent,
! 82408: ** to the specified offset in the buffer and updates *pIdx to refer
! 82409: ** to the first byte after the last byte written before returning.
! 82410: **
! 82411: ** If the string zSignedIdent consists entirely of alpha-numeric
! 82412: ** characters, does not begin with a digit and is not an SQL keyword,
! 82413: ** then it is copied to the output buffer exactly as it is. Otherwise,
! 82414: ** it is quoted using double-quotes.
! 82415: */
! 82416: static void identPut(char *z, int *pIdx, char *zSignedIdent){
! 82417: unsigned char *zIdent = (unsigned char*)zSignedIdent;
! 82418: int i, j, needQuote;
! 82419: i = *pIdx;
! 82420:
! 82421: for(j=0; zIdent[j]; j++){
! 82422: if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
! 82423: }
! 82424: needQuote = sqlite3Isdigit(zIdent[0]) || sqlite3KeywordCode(zIdent, j)!=TK_ID;
! 82425: if( !needQuote ){
! 82426: needQuote = zIdent[j];
! 82427: }
! 82428:
! 82429: if( needQuote ) z[i++] = '"';
! 82430: for(j=0; zIdent[j]; j++){
! 82431: z[i++] = zIdent[j];
! 82432: if( zIdent[j]=='"' ) z[i++] = '"';
! 82433: }
! 82434: if( needQuote ) z[i++] = '"';
! 82435: z[i] = 0;
! 82436: *pIdx = i;
! 82437: }
! 82438:
! 82439: /*
! 82440: ** Generate a CREATE TABLE statement appropriate for the given
! 82441: ** table. Memory to hold the text of the statement is obtained
! 82442: ** from sqliteMalloc() and must be freed by the calling function.
! 82443: */
! 82444: static char *createTableStmt(sqlite3 *db, Table *p){
! 82445: int i, k, n;
! 82446: char *zStmt;
! 82447: char *zSep, *zSep2, *zEnd;
! 82448: Column *pCol;
! 82449: n = 0;
! 82450: for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
! 82451: n += identLength(pCol->zName) + 5;
! 82452: }
! 82453: n += identLength(p->zName);
! 82454: if( n<50 ){
! 82455: zSep = "";
! 82456: zSep2 = ",";
! 82457: zEnd = ")";
! 82458: }else{
! 82459: zSep = "\n ";
! 82460: zSep2 = ",\n ";
! 82461: zEnd = "\n)";
! 82462: }
! 82463: n += 35 + 6*p->nCol;
! 82464: zStmt = sqlite3DbMallocRaw(0, n);
! 82465: if( zStmt==0 ){
! 82466: db->mallocFailed = 1;
! 82467: return 0;
! 82468: }
! 82469: sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
! 82470: k = sqlite3Strlen30(zStmt);
! 82471: identPut(zStmt, &k, p->zName);
! 82472: zStmt[k++] = '(';
! 82473: for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
! 82474: static const char * const azType[] = {
! 82475: /* SQLITE_AFF_TEXT */ " TEXT",
! 82476: /* SQLITE_AFF_NONE */ "",
! 82477: /* SQLITE_AFF_NUMERIC */ " NUM",
! 82478: /* SQLITE_AFF_INTEGER */ " INT",
! 82479: /* SQLITE_AFF_REAL */ " REAL"
! 82480: };
! 82481: int len;
! 82482: const char *zType;
! 82483:
! 82484: sqlite3_snprintf(n-k, &zStmt[k], zSep);
! 82485: k += sqlite3Strlen30(&zStmt[k]);
! 82486: zSep = zSep2;
! 82487: identPut(zStmt, &k, pCol->zName);
! 82488: assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
! 82489: assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
! 82490: testcase( pCol->affinity==SQLITE_AFF_TEXT );
! 82491: testcase( pCol->affinity==SQLITE_AFF_NONE );
! 82492: testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
! 82493: testcase( pCol->affinity==SQLITE_AFF_INTEGER );
! 82494: testcase( pCol->affinity==SQLITE_AFF_REAL );
! 82495:
! 82496: zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
! 82497: len = sqlite3Strlen30(zType);
! 82498: assert( pCol->affinity==SQLITE_AFF_NONE
! 82499: || pCol->affinity==sqlite3AffinityType(zType) );
! 82500: memcpy(&zStmt[k], zType, len);
! 82501: k += len;
! 82502: assert( k<=n );
! 82503: }
! 82504: sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
! 82505: return zStmt;
! 82506: }
! 82507:
! 82508: /*
! 82509: ** This routine is called to report the final ")" that terminates
! 82510: ** a CREATE TABLE statement.
! 82511: **
! 82512: ** The table structure that other action routines have been building
! 82513: ** is added to the internal hash tables, assuming no errors have
! 82514: ** occurred.
! 82515: **
! 82516: ** An entry for the table is made in the master table on disk, unless
! 82517: ** this is a temporary table or db->init.busy==1. When db->init.busy==1
! 82518: ** it means we are reading the sqlite_master table because we just
! 82519: ** connected to the database or because the sqlite_master table has
! 82520: ** recently changed, so the entry for this table already exists in
! 82521: ** the sqlite_master table. We do not want to create it again.
! 82522: **
! 82523: ** If the pSelect argument is not NULL, it means that this routine
! 82524: ** was called to create a table generated from a
! 82525: ** "CREATE TABLE ... AS SELECT ..." statement. The column names of
! 82526: ** the new table will match the result set of the SELECT.
! 82527: */
! 82528: SQLITE_PRIVATE void sqlite3EndTable(
! 82529: Parse *pParse, /* Parse context */
! 82530: Token *pCons, /* The ',' token after the last column defn. */
! 82531: Token *pEnd, /* The final ')' token in the CREATE TABLE */
! 82532: Select *pSelect /* Select from a "CREATE ... AS SELECT" */
! 82533: ){
! 82534: Table *p;
! 82535: sqlite3 *db = pParse->db;
! 82536: int iDb;
! 82537:
! 82538: if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
! 82539: return;
! 82540: }
! 82541: p = pParse->pNewTable;
! 82542: if( p==0 ) return;
! 82543:
! 82544: assert( !db->init.busy || !pSelect );
! 82545:
! 82546: iDb = sqlite3SchemaToIndex(db, p->pSchema);
! 82547:
! 82548: #ifndef SQLITE_OMIT_CHECK
! 82549: /* Resolve names in all CHECK constraint expressions.
! 82550: */
! 82551: if( p->pCheck ){
! 82552: SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
! 82553: NameContext sNC; /* Name context for pParse->pNewTable */
! 82554:
! 82555: memset(&sNC, 0, sizeof(sNC));
! 82556: memset(&sSrc, 0, sizeof(sSrc));
! 82557: sSrc.nSrc = 1;
! 82558: sSrc.a[0].zName = p->zName;
! 82559: sSrc.a[0].pTab = p;
! 82560: sSrc.a[0].iCursor = -1;
! 82561: sNC.pParse = pParse;
! 82562: sNC.pSrcList = &sSrc;
! 82563: sNC.isCheck = 1;
! 82564: if( sqlite3ResolveExprNames(&sNC, p->pCheck) ){
! 82565: return;
! 82566: }
! 82567: }
! 82568: #endif /* !defined(SQLITE_OMIT_CHECK) */
! 82569:
! 82570: /* If the db->init.busy is 1 it means we are reading the SQL off the
! 82571: ** "sqlite_master" or "sqlite_temp_master" table on the disk.
! 82572: ** So do not write to the disk again. Extract the root page number
! 82573: ** for the table from the db->init.newTnum field. (The page number
! 82574: ** should have been put there by the sqliteOpenCb routine.)
! 82575: */
! 82576: if( db->init.busy ){
! 82577: p->tnum = db->init.newTnum;
! 82578: }
! 82579:
! 82580: /* If not initializing, then create a record for the new table
! 82581: ** in the SQLITE_MASTER table of the database.
! 82582: **
! 82583: ** If this is a TEMPORARY table, write the entry into the auxiliary
! 82584: ** file instead of into the main database file.
! 82585: */
! 82586: if( !db->init.busy ){
! 82587: int n;
! 82588: Vdbe *v;
! 82589: char *zType; /* "view" or "table" */
! 82590: char *zType2; /* "VIEW" or "TABLE" */
! 82591: char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
! 82592:
! 82593: v = sqlite3GetVdbe(pParse);
! 82594: if( NEVER(v==0) ) return;
! 82595:
! 82596: sqlite3VdbeAddOp1(v, OP_Close, 0);
! 82597:
! 82598: /*
! 82599: ** Initialize zType for the new view or table.
! 82600: */
! 82601: if( p->pSelect==0 ){
! 82602: /* A regular table */
! 82603: zType = "table";
! 82604: zType2 = "TABLE";
! 82605: #ifndef SQLITE_OMIT_VIEW
! 82606: }else{
! 82607: /* A view */
! 82608: zType = "view";
! 82609: zType2 = "VIEW";
! 82610: #endif
! 82611: }
! 82612:
! 82613: /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
! 82614: ** statement to populate the new table. The root-page number for the
! 82615: ** new table is in register pParse->regRoot.
! 82616: **
! 82617: ** Once the SELECT has been coded by sqlite3Select(), it is in a
! 82618: ** suitable state to query for the column names and types to be used
! 82619: ** by the new table.
! 82620: **
! 82621: ** A shared-cache write-lock is not required to write to the new table,
! 82622: ** as a schema-lock must have already been obtained to create it. Since
! 82623: ** a schema-lock excludes all other database users, the write-lock would
! 82624: ** be redundant.
! 82625: */
! 82626: if( pSelect ){
! 82627: SelectDest dest;
! 82628: Table *pSelTab;
! 82629:
! 82630: assert(pParse->nTab==1);
! 82631: sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
! 82632: sqlite3VdbeChangeP5(v, 1);
! 82633: pParse->nTab = 2;
! 82634: sqlite3SelectDestInit(&dest, SRT_Table, 1);
! 82635: sqlite3Select(pParse, pSelect, &dest);
! 82636: sqlite3VdbeAddOp1(v, OP_Close, 1);
! 82637: if( pParse->nErr==0 ){
! 82638: pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
! 82639: if( pSelTab==0 ) return;
! 82640: assert( p->aCol==0 );
! 82641: p->nCol = pSelTab->nCol;
! 82642: p->aCol = pSelTab->aCol;
! 82643: pSelTab->nCol = 0;
! 82644: pSelTab->aCol = 0;
! 82645: sqlite3DeleteTable(db, pSelTab);
! 82646: }
! 82647: }
! 82648:
! 82649: /* Compute the complete text of the CREATE statement */
! 82650: if( pSelect ){
! 82651: zStmt = createTableStmt(db, p);
! 82652: }else{
! 82653: n = (int)(pEnd->z - pParse->sNameToken.z) + 1;
! 82654: zStmt = sqlite3MPrintf(db,
! 82655: "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
! 82656: );
! 82657: }
! 82658:
! 82659: /* A slot for the record has already been allocated in the
! 82660: ** SQLITE_MASTER table. We just need to update that slot with all
! 82661: ** the information we've collected.
! 82662: */
! 82663: sqlite3NestedParse(pParse,
! 82664: "UPDATE %Q.%s "
! 82665: "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
! 82666: "WHERE rowid=#%d",
! 82667: db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
! 82668: zType,
! 82669: p->zName,
! 82670: p->zName,
! 82671: pParse->regRoot,
! 82672: zStmt,
! 82673: pParse->regRowid
! 82674: );
! 82675: sqlite3DbFree(db, zStmt);
! 82676: sqlite3ChangeCookie(pParse, iDb);
! 82677:
! 82678: #ifndef SQLITE_OMIT_AUTOINCREMENT
! 82679: /* Check to see if we need to create an sqlite_sequence table for
! 82680: ** keeping track of autoincrement keys.
! 82681: */
! 82682: if( p->tabFlags & TF_Autoincrement ){
! 82683: Db *pDb = &db->aDb[iDb];
! 82684: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 82685: if( pDb->pSchema->pSeqTab==0 ){
! 82686: sqlite3NestedParse(pParse,
! 82687: "CREATE TABLE %Q.sqlite_sequence(name,seq)",
! 82688: pDb->zName
! 82689: );
! 82690: }
! 82691: }
! 82692: #endif
! 82693:
! 82694: /* Reparse everything to update our internal data structures */
! 82695: sqlite3VdbeAddParseSchemaOp(v, iDb,
! 82696: sqlite3MPrintf(db, "tbl_name='%q'", p->zName));
! 82697: }
! 82698:
! 82699:
! 82700: /* Add the table to the in-memory representation of the database.
! 82701: */
! 82702: if( db->init.busy ){
! 82703: Table *pOld;
! 82704: Schema *pSchema = p->pSchema;
! 82705: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 82706: pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
! 82707: sqlite3Strlen30(p->zName),p);
! 82708: if( pOld ){
! 82709: assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
! 82710: db->mallocFailed = 1;
! 82711: return;
! 82712: }
! 82713: pParse->pNewTable = 0;
! 82714: db->nTable++;
! 82715: db->flags |= SQLITE_InternChanges;
! 82716:
! 82717: #ifndef SQLITE_OMIT_ALTERTABLE
! 82718: if( !p->pSelect ){
! 82719: const char *zName = (const char *)pParse->sNameToken.z;
! 82720: int nName;
! 82721: assert( !pSelect && pCons && pEnd );
! 82722: if( pCons->z==0 ){
! 82723: pCons = pEnd;
! 82724: }
! 82725: nName = (int)((const char *)pCons->z - zName);
! 82726: p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
! 82727: }
! 82728: #endif
! 82729: }
! 82730: }
! 82731:
! 82732: #ifndef SQLITE_OMIT_VIEW
! 82733: /*
! 82734: ** The parser calls this routine in order to create a new VIEW
! 82735: */
! 82736: SQLITE_PRIVATE void sqlite3CreateView(
! 82737: Parse *pParse, /* The parsing context */
! 82738: Token *pBegin, /* The CREATE token that begins the statement */
! 82739: Token *pName1, /* The token that holds the name of the view */
! 82740: Token *pName2, /* The token that holds the name of the view */
! 82741: Select *pSelect, /* A SELECT statement that will become the new view */
! 82742: int isTemp, /* TRUE for a TEMPORARY view */
! 82743: int noErr /* Suppress error messages if VIEW already exists */
! 82744: ){
! 82745: Table *p;
! 82746: int n;
! 82747: const char *z;
! 82748: Token sEnd;
! 82749: DbFixer sFix;
! 82750: Token *pName = 0;
! 82751: int iDb;
! 82752: sqlite3 *db = pParse->db;
! 82753:
! 82754: if( pParse->nVar>0 ){
! 82755: sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
! 82756: sqlite3SelectDelete(db, pSelect);
! 82757: return;
! 82758: }
! 82759: sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
! 82760: p = pParse->pNewTable;
! 82761: if( p==0 || pParse->nErr ){
! 82762: sqlite3SelectDelete(db, pSelect);
! 82763: return;
! 82764: }
! 82765: sqlite3TwoPartName(pParse, pName1, pName2, &pName);
! 82766: iDb = sqlite3SchemaToIndex(db, p->pSchema);
! 82767: if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
! 82768: && sqlite3FixSelect(&sFix, pSelect)
! 82769: ){
! 82770: sqlite3SelectDelete(db, pSelect);
! 82771: return;
! 82772: }
! 82773:
! 82774: /* Make a copy of the entire SELECT statement that defines the view.
! 82775: ** This will force all the Expr.token.z values to be dynamically
! 82776: ** allocated rather than point to the input string - which means that
! 82777: ** they will persist after the current sqlite3_exec() call returns.
! 82778: */
! 82779: p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
! 82780: sqlite3SelectDelete(db, pSelect);
! 82781: if( db->mallocFailed ){
! 82782: return;
! 82783: }
! 82784: if( !db->init.busy ){
! 82785: sqlite3ViewGetColumnNames(pParse, p);
! 82786: }
! 82787:
! 82788: /* Locate the end of the CREATE VIEW statement. Make sEnd point to
! 82789: ** the end.
! 82790: */
! 82791: sEnd = pParse->sLastToken;
! 82792: if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
! 82793: sEnd.z += sEnd.n;
! 82794: }
! 82795: sEnd.n = 0;
! 82796: n = (int)(sEnd.z - pBegin->z);
! 82797: z = pBegin->z;
! 82798: while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
! 82799: sEnd.z = &z[n-1];
! 82800: sEnd.n = 1;
! 82801:
! 82802: /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
! 82803: sqlite3EndTable(pParse, 0, &sEnd, 0);
! 82804: return;
! 82805: }
! 82806: #endif /* SQLITE_OMIT_VIEW */
! 82807:
! 82808: #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
! 82809: /*
! 82810: ** The Table structure pTable is really a VIEW. Fill in the names of
! 82811: ** the columns of the view in the pTable structure. Return the number
! 82812: ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
! 82813: */
! 82814: SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
! 82815: Table *pSelTab; /* A fake table from which we get the result set */
! 82816: Select *pSel; /* Copy of the SELECT that implements the view */
! 82817: int nErr = 0; /* Number of errors encountered */
! 82818: int n; /* Temporarily holds the number of cursors assigned */
! 82819: sqlite3 *db = pParse->db; /* Database connection for malloc errors */
! 82820: int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
! 82821:
! 82822: assert( pTable );
! 82823:
! 82824: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 82825: if( sqlite3VtabCallConnect(pParse, pTable) ){
! 82826: return SQLITE_ERROR;
! 82827: }
! 82828: if( IsVirtual(pTable) ) return 0;
! 82829: #endif
! 82830:
! 82831: #ifndef SQLITE_OMIT_VIEW
! 82832: /* A positive nCol means the columns names for this view are
! 82833: ** already known.
! 82834: */
! 82835: if( pTable->nCol>0 ) return 0;
! 82836:
! 82837: /* A negative nCol is a special marker meaning that we are currently
! 82838: ** trying to compute the column names. If we enter this routine with
! 82839: ** a negative nCol, it means two or more views form a loop, like this:
! 82840: **
! 82841: ** CREATE VIEW one AS SELECT * FROM two;
! 82842: ** CREATE VIEW two AS SELECT * FROM one;
! 82843: **
! 82844: ** Actually, the error above is now caught prior to reaching this point.
! 82845: ** But the following test is still important as it does come up
! 82846: ** in the following:
! 82847: **
! 82848: ** CREATE TABLE main.ex1(a);
! 82849: ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
! 82850: ** SELECT * FROM temp.ex1;
! 82851: */
! 82852: if( pTable->nCol<0 ){
! 82853: sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
! 82854: return 1;
! 82855: }
! 82856: assert( pTable->nCol>=0 );
! 82857:
! 82858: /* If we get this far, it means we need to compute the table names.
! 82859: ** Note that the call to sqlite3ResultSetOfSelect() will expand any
! 82860: ** "*" elements in the results set of the view and will assign cursors
! 82861: ** to the elements of the FROM clause. But we do not want these changes
! 82862: ** to be permanent. So the computation is done on a copy of the SELECT
! 82863: ** statement that defines the view.
! 82864: */
! 82865: assert( pTable->pSelect );
! 82866: pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
! 82867: if( pSel ){
! 82868: u8 enableLookaside = db->lookaside.bEnabled;
! 82869: n = pParse->nTab;
! 82870: sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
! 82871: pTable->nCol = -1;
! 82872: db->lookaside.bEnabled = 0;
! 82873: #ifndef SQLITE_OMIT_AUTHORIZATION
! 82874: xAuth = db->xAuth;
! 82875: db->xAuth = 0;
! 82876: pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
! 82877: db->xAuth = xAuth;
! 82878: #else
! 82879: pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
! 82880: #endif
! 82881: db->lookaside.bEnabled = enableLookaside;
! 82882: pParse->nTab = n;
! 82883: if( pSelTab ){
! 82884: assert( pTable->aCol==0 );
! 82885: pTable->nCol = pSelTab->nCol;
! 82886: pTable->aCol = pSelTab->aCol;
! 82887: pSelTab->nCol = 0;
! 82888: pSelTab->aCol = 0;
! 82889: sqlite3DeleteTable(db, pSelTab);
! 82890: assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
! 82891: pTable->pSchema->flags |= DB_UnresetViews;
! 82892: }else{
! 82893: pTable->nCol = 0;
! 82894: nErr++;
! 82895: }
! 82896: sqlite3SelectDelete(db, pSel);
! 82897: } else {
! 82898: nErr++;
! 82899: }
! 82900: #endif /* SQLITE_OMIT_VIEW */
! 82901: return nErr;
! 82902: }
! 82903: #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
! 82904:
! 82905: #ifndef SQLITE_OMIT_VIEW
! 82906: /*
! 82907: ** Clear the column names from every VIEW in database idx.
! 82908: */
! 82909: static void sqliteViewResetAll(sqlite3 *db, int idx){
! 82910: HashElem *i;
! 82911: assert( sqlite3SchemaMutexHeld(db, idx, 0) );
! 82912: if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
! 82913: for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
! 82914: Table *pTab = sqliteHashData(i);
! 82915: if( pTab->pSelect ){
! 82916: sqliteDeleteColumnNames(db, pTab);
! 82917: pTab->aCol = 0;
! 82918: pTab->nCol = 0;
! 82919: }
! 82920: }
! 82921: DbClearProperty(db, idx, DB_UnresetViews);
! 82922: }
! 82923: #else
! 82924: # define sqliteViewResetAll(A,B)
! 82925: #endif /* SQLITE_OMIT_VIEW */
! 82926:
! 82927: /*
! 82928: ** This function is called by the VDBE to adjust the internal schema
! 82929: ** used by SQLite when the btree layer moves a table root page. The
! 82930: ** root-page of a table or index in database iDb has changed from iFrom
! 82931: ** to iTo.
! 82932: **
! 82933: ** Ticket #1728: The symbol table might still contain information
! 82934: ** on tables and/or indices that are the process of being deleted.
! 82935: ** If you are unlucky, one of those deleted indices or tables might
! 82936: ** have the same rootpage number as the real table or index that is
! 82937: ** being moved. So we cannot stop searching after the first match
! 82938: ** because the first match might be for one of the deleted indices
! 82939: ** or tables and not the table/index that is actually being moved.
! 82940: ** We must continue looping until all tables and indices with
! 82941: ** rootpage==iFrom have been converted to have a rootpage of iTo
! 82942: ** in order to be certain that we got the right one.
! 82943: */
! 82944: #ifndef SQLITE_OMIT_AUTOVACUUM
! 82945: SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
! 82946: HashElem *pElem;
! 82947: Hash *pHash;
! 82948: Db *pDb;
! 82949:
! 82950: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 82951: pDb = &db->aDb[iDb];
! 82952: pHash = &pDb->pSchema->tblHash;
! 82953: for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
! 82954: Table *pTab = sqliteHashData(pElem);
! 82955: if( pTab->tnum==iFrom ){
! 82956: pTab->tnum = iTo;
! 82957: }
! 82958: }
! 82959: pHash = &pDb->pSchema->idxHash;
! 82960: for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
! 82961: Index *pIdx = sqliteHashData(pElem);
! 82962: if( pIdx->tnum==iFrom ){
! 82963: pIdx->tnum = iTo;
! 82964: }
! 82965: }
! 82966: }
! 82967: #endif
! 82968:
! 82969: /*
! 82970: ** Write code to erase the table with root-page iTable from database iDb.
! 82971: ** Also write code to modify the sqlite_master table and internal schema
! 82972: ** if a root-page of another table is moved by the btree-layer whilst
! 82973: ** erasing iTable (this can happen with an auto-vacuum database).
! 82974: */
! 82975: static void destroyRootPage(Parse *pParse, int iTable, int iDb){
! 82976: Vdbe *v = sqlite3GetVdbe(pParse);
! 82977: int r1 = sqlite3GetTempReg(pParse);
! 82978: sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
! 82979: sqlite3MayAbort(pParse);
! 82980: #ifndef SQLITE_OMIT_AUTOVACUUM
! 82981: /* OP_Destroy stores an in integer r1. If this integer
! 82982: ** is non-zero, then it is the root page number of a table moved to
! 82983: ** location iTable. The following code modifies the sqlite_master table to
! 82984: ** reflect this.
! 82985: **
! 82986: ** The "#NNN" in the SQL is a special constant that means whatever value
! 82987: ** is in register NNN. See grammar rules associated with the TK_REGISTER
! 82988: ** token for additional information.
! 82989: */
! 82990: sqlite3NestedParse(pParse,
! 82991: "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
! 82992: pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
! 82993: #endif
! 82994: sqlite3ReleaseTempReg(pParse, r1);
! 82995: }
! 82996:
! 82997: /*
! 82998: ** Write VDBE code to erase table pTab and all associated indices on disk.
! 82999: ** Code to update the sqlite_master tables and internal schema definitions
! 83000: ** in case a root-page belonging to another table is moved by the btree layer
! 83001: ** is also added (this can happen with an auto-vacuum database).
! 83002: */
! 83003: static void destroyTable(Parse *pParse, Table *pTab){
! 83004: #ifdef SQLITE_OMIT_AUTOVACUUM
! 83005: Index *pIdx;
! 83006: int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
! 83007: destroyRootPage(pParse, pTab->tnum, iDb);
! 83008: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
! 83009: destroyRootPage(pParse, pIdx->tnum, iDb);
! 83010: }
! 83011: #else
! 83012: /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
! 83013: ** is not defined), then it is important to call OP_Destroy on the
! 83014: ** table and index root-pages in order, starting with the numerically
! 83015: ** largest root-page number. This guarantees that none of the root-pages
! 83016: ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
! 83017: ** following were coded:
! 83018: **
! 83019: ** OP_Destroy 4 0
! 83020: ** ...
! 83021: ** OP_Destroy 5 0
! 83022: **
! 83023: ** and root page 5 happened to be the largest root-page number in the
! 83024: ** database, then root page 5 would be moved to page 4 by the
! 83025: ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
! 83026: ** a free-list page.
! 83027: */
! 83028: int iTab = pTab->tnum;
! 83029: int iDestroyed = 0;
! 83030:
! 83031: while( 1 ){
! 83032: Index *pIdx;
! 83033: int iLargest = 0;
! 83034:
! 83035: if( iDestroyed==0 || iTab<iDestroyed ){
! 83036: iLargest = iTab;
! 83037: }
! 83038: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
! 83039: int iIdx = pIdx->tnum;
! 83040: assert( pIdx->pSchema==pTab->pSchema );
! 83041: if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
! 83042: iLargest = iIdx;
! 83043: }
! 83044: }
! 83045: if( iLargest==0 ){
! 83046: return;
! 83047: }else{
! 83048: int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
! 83049: destroyRootPage(pParse, iLargest, iDb);
! 83050: iDestroyed = iLargest;
! 83051: }
! 83052: }
! 83053: #endif
! 83054: }
! 83055:
! 83056: /*
! 83057: ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
! 83058: ** after a DROP INDEX or DROP TABLE command.
! 83059: */
! 83060: static void sqlite3ClearStatTables(
! 83061: Parse *pParse, /* The parsing context */
! 83062: int iDb, /* The database number */
! 83063: const char *zType, /* "idx" or "tbl" */
! 83064: const char *zName /* Name of index or table */
! 83065: ){
! 83066: int i;
! 83067: const char *zDbName = pParse->db->aDb[iDb].zName;
! 83068: for(i=1; i<=3; i++){
! 83069: char zTab[24];
! 83070: sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
! 83071: if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
! 83072: sqlite3NestedParse(pParse,
! 83073: "DELETE FROM %Q.%s WHERE %s=%Q",
! 83074: zDbName, zTab, zType, zName
! 83075: );
! 83076: }
! 83077: }
! 83078: }
! 83079:
! 83080: /*
! 83081: ** Generate code to drop a table.
! 83082: */
! 83083: SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
! 83084: Vdbe *v;
! 83085: sqlite3 *db = pParse->db;
! 83086: Trigger *pTrigger;
! 83087: Db *pDb = &db->aDb[iDb];
! 83088:
! 83089: v = sqlite3GetVdbe(pParse);
! 83090: assert( v!=0 );
! 83091: sqlite3BeginWriteOperation(pParse, 1, iDb);
! 83092:
! 83093: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 83094: if( IsVirtual(pTab) ){
! 83095: sqlite3VdbeAddOp0(v, OP_VBegin);
! 83096: }
! 83097: #endif
! 83098:
! 83099: /* Drop all triggers associated with the table being dropped. Code
! 83100: ** is generated to remove entries from sqlite_master and/or
! 83101: ** sqlite_temp_master if required.
! 83102: */
! 83103: pTrigger = sqlite3TriggerList(pParse, pTab);
! 83104: while( pTrigger ){
! 83105: assert( pTrigger->pSchema==pTab->pSchema ||
! 83106: pTrigger->pSchema==db->aDb[1].pSchema );
! 83107: sqlite3DropTriggerPtr(pParse, pTrigger);
! 83108: pTrigger = pTrigger->pNext;
! 83109: }
! 83110:
! 83111: #ifndef SQLITE_OMIT_AUTOINCREMENT
! 83112: /* Remove any entries of the sqlite_sequence table associated with
! 83113: ** the table being dropped. This is done before the table is dropped
! 83114: ** at the btree level, in case the sqlite_sequence table needs to
! 83115: ** move as a result of the drop (can happen in auto-vacuum mode).
! 83116: */
! 83117: if( pTab->tabFlags & TF_Autoincrement ){
! 83118: sqlite3NestedParse(pParse,
! 83119: "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
! 83120: pDb->zName, pTab->zName
! 83121: );
! 83122: }
! 83123: #endif
! 83124:
! 83125: /* Drop all SQLITE_MASTER table and index entries that refer to the
! 83126: ** table. The program name loops through the master table and deletes
! 83127: ** every row that refers to a table of the same name as the one being
! 83128: ** dropped. Triggers are handled seperately because a trigger can be
! 83129: ** created in the temp database that refers to a table in another
! 83130: ** database.
! 83131: */
! 83132: sqlite3NestedParse(pParse,
! 83133: "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
! 83134: pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
! 83135: if( !isView && !IsVirtual(pTab) ){
! 83136: destroyTable(pParse, pTab);
! 83137: }
! 83138:
! 83139: /* Remove the table entry from SQLite's internal schema and modify
! 83140: ** the schema cookie.
! 83141: */
! 83142: if( IsVirtual(pTab) ){
! 83143: sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
! 83144: }
! 83145: sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
! 83146: sqlite3ChangeCookie(pParse, iDb);
! 83147: sqliteViewResetAll(db, iDb);
! 83148: }
! 83149:
! 83150: /*
! 83151: ** This routine is called to do the work of a DROP TABLE statement.
! 83152: ** pName is the name of the table to be dropped.
! 83153: */
! 83154: SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
! 83155: Table *pTab;
! 83156: Vdbe *v;
! 83157: sqlite3 *db = pParse->db;
! 83158: int iDb;
! 83159:
! 83160: if( db->mallocFailed ){
! 83161: goto exit_drop_table;
! 83162: }
! 83163: assert( pParse->nErr==0 );
! 83164: assert( pName->nSrc==1 );
! 83165: if( noErr ) db->suppressErr++;
! 83166: pTab = sqlite3LocateTable(pParse, isView,
! 83167: pName->a[0].zName, pName->a[0].zDatabase);
! 83168: if( noErr ) db->suppressErr--;
! 83169:
! 83170: if( pTab==0 ){
! 83171: if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
! 83172: goto exit_drop_table;
! 83173: }
! 83174: iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 83175: assert( iDb>=0 && iDb<db->nDb );
! 83176:
! 83177: /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
! 83178: ** it is initialized.
! 83179: */
! 83180: if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
! 83181: goto exit_drop_table;
! 83182: }
! 83183: #ifndef SQLITE_OMIT_AUTHORIZATION
! 83184: {
! 83185: int code;
! 83186: const char *zTab = SCHEMA_TABLE(iDb);
! 83187: const char *zDb = db->aDb[iDb].zName;
! 83188: const char *zArg2 = 0;
! 83189: if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
! 83190: goto exit_drop_table;
! 83191: }
! 83192: if( isView ){
! 83193: if( !OMIT_TEMPDB && iDb==1 ){
! 83194: code = SQLITE_DROP_TEMP_VIEW;
! 83195: }else{
! 83196: code = SQLITE_DROP_VIEW;
! 83197: }
! 83198: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 83199: }else if( IsVirtual(pTab) ){
! 83200: code = SQLITE_DROP_VTABLE;
! 83201: zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
! 83202: #endif
! 83203: }else{
! 83204: if( !OMIT_TEMPDB && iDb==1 ){
! 83205: code = SQLITE_DROP_TEMP_TABLE;
! 83206: }else{
! 83207: code = SQLITE_DROP_TABLE;
! 83208: }
! 83209: }
! 83210: if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
! 83211: goto exit_drop_table;
! 83212: }
! 83213: if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
! 83214: goto exit_drop_table;
! 83215: }
! 83216: }
! 83217: #endif
! 83218: if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
! 83219: && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
! 83220: sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
! 83221: goto exit_drop_table;
! 83222: }
! 83223:
! 83224: #ifndef SQLITE_OMIT_VIEW
! 83225: /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
! 83226: ** on a table.
! 83227: */
! 83228: if( isView && pTab->pSelect==0 ){
! 83229: sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
! 83230: goto exit_drop_table;
! 83231: }
! 83232: if( !isView && pTab->pSelect ){
! 83233: sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
! 83234: goto exit_drop_table;
! 83235: }
! 83236: #endif
! 83237:
! 83238: /* Generate code to remove the table from the master table
! 83239: ** on disk.
! 83240: */
! 83241: v = sqlite3GetVdbe(pParse);
! 83242: if( v ){
! 83243: sqlite3BeginWriteOperation(pParse, 1, iDb);
! 83244: sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
! 83245: sqlite3FkDropTable(pParse, pName, pTab);
! 83246: sqlite3CodeDropTable(pParse, pTab, iDb, isView);
! 83247: }
! 83248:
! 83249: exit_drop_table:
! 83250: sqlite3SrcListDelete(db, pName);
! 83251: }
! 83252:
! 83253: /*
! 83254: ** This routine is called to create a new foreign key on the table
! 83255: ** currently under construction. pFromCol determines which columns
! 83256: ** in the current table point to the foreign key. If pFromCol==0 then
! 83257: ** connect the key to the last column inserted. pTo is the name of
! 83258: ** the table referred to. pToCol is a list of tables in the other
! 83259: ** pTo table that the foreign key points to. flags contains all
! 83260: ** information about the conflict resolution algorithms specified
! 83261: ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
! 83262: **
! 83263: ** An FKey structure is created and added to the table currently
! 83264: ** under construction in the pParse->pNewTable field.
! 83265: **
! 83266: ** The foreign key is set for IMMEDIATE processing. A subsequent call
! 83267: ** to sqlite3DeferForeignKey() might change this to DEFERRED.
! 83268: */
! 83269: SQLITE_PRIVATE void sqlite3CreateForeignKey(
! 83270: Parse *pParse, /* Parsing context */
! 83271: ExprList *pFromCol, /* Columns in this table that point to other table */
! 83272: Token *pTo, /* Name of the other table */
! 83273: ExprList *pToCol, /* Columns in the other table */
! 83274: int flags /* Conflict resolution algorithms. */
! 83275: ){
! 83276: sqlite3 *db = pParse->db;
! 83277: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 83278: FKey *pFKey = 0;
! 83279: FKey *pNextTo;
! 83280: Table *p = pParse->pNewTable;
! 83281: int nByte;
! 83282: int i;
! 83283: int nCol;
! 83284: char *z;
! 83285:
! 83286: assert( pTo!=0 );
! 83287: if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
! 83288: if( pFromCol==0 ){
! 83289: int iCol = p->nCol-1;
! 83290: if( NEVER(iCol<0) ) goto fk_end;
! 83291: if( pToCol && pToCol->nExpr!=1 ){
! 83292: sqlite3ErrorMsg(pParse, "foreign key on %s"
! 83293: " should reference only one column of table %T",
! 83294: p->aCol[iCol].zName, pTo);
! 83295: goto fk_end;
! 83296: }
! 83297: nCol = 1;
! 83298: }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
! 83299: sqlite3ErrorMsg(pParse,
! 83300: "number of columns in foreign key does not match the number of "
! 83301: "columns in the referenced table");
! 83302: goto fk_end;
! 83303: }else{
! 83304: nCol = pFromCol->nExpr;
! 83305: }
! 83306: nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
! 83307: if( pToCol ){
! 83308: for(i=0; i<pToCol->nExpr; i++){
! 83309: nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
! 83310: }
! 83311: }
! 83312: pFKey = sqlite3DbMallocZero(db, nByte );
! 83313: if( pFKey==0 ){
! 83314: goto fk_end;
! 83315: }
! 83316: pFKey->pFrom = p;
! 83317: pFKey->pNextFrom = p->pFKey;
! 83318: z = (char*)&pFKey->aCol[nCol];
! 83319: pFKey->zTo = z;
! 83320: memcpy(z, pTo->z, pTo->n);
! 83321: z[pTo->n] = 0;
! 83322: sqlite3Dequote(z);
! 83323: z += pTo->n+1;
! 83324: pFKey->nCol = nCol;
! 83325: if( pFromCol==0 ){
! 83326: pFKey->aCol[0].iFrom = p->nCol-1;
! 83327: }else{
! 83328: for(i=0; i<nCol; i++){
! 83329: int j;
! 83330: for(j=0; j<p->nCol; j++){
! 83331: if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
! 83332: pFKey->aCol[i].iFrom = j;
! 83333: break;
! 83334: }
! 83335: }
! 83336: if( j>=p->nCol ){
! 83337: sqlite3ErrorMsg(pParse,
! 83338: "unknown column \"%s\" in foreign key definition",
! 83339: pFromCol->a[i].zName);
! 83340: goto fk_end;
! 83341: }
! 83342: }
! 83343: }
! 83344: if( pToCol ){
! 83345: for(i=0; i<nCol; i++){
! 83346: int n = sqlite3Strlen30(pToCol->a[i].zName);
! 83347: pFKey->aCol[i].zCol = z;
! 83348: memcpy(z, pToCol->a[i].zName, n);
! 83349: z[n] = 0;
! 83350: z += n+1;
! 83351: }
! 83352: }
! 83353: pFKey->isDeferred = 0;
! 83354: pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
! 83355: pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
! 83356:
! 83357: assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
! 83358: pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
! 83359: pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
! 83360: );
! 83361: if( pNextTo==pFKey ){
! 83362: db->mallocFailed = 1;
! 83363: goto fk_end;
! 83364: }
! 83365: if( pNextTo ){
! 83366: assert( pNextTo->pPrevTo==0 );
! 83367: pFKey->pNextTo = pNextTo;
! 83368: pNextTo->pPrevTo = pFKey;
! 83369: }
! 83370:
! 83371: /* Link the foreign key to the table as the last step.
! 83372: */
! 83373: p->pFKey = pFKey;
! 83374: pFKey = 0;
! 83375:
! 83376: fk_end:
! 83377: sqlite3DbFree(db, pFKey);
! 83378: #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
! 83379: sqlite3ExprListDelete(db, pFromCol);
! 83380: sqlite3ExprListDelete(db, pToCol);
! 83381: }
! 83382:
! 83383: /*
! 83384: ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
! 83385: ** clause is seen as part of a foreign key definition. The isDeferred
! 83386: ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
! 83387: ** The behavior of the most recently created foreign key is adjusted
! 83388: ** accordingly.
! 83389: */
! 83390: SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
! 83391: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 83392: Table *pTab;
! 83393: FKey *pFKey;
! 83394: if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
! 83395: assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
! 83396: pFKey->isDeferred = (u8)isDeferred;
! 83397: #endif
! 83398: }
! 83399:
! 83400: /*
! 83401: ** Generate code that will erase and refill index *pIdx. This is
! 83402: ** used to initialize a newly created index or to recompute the
! 83403: ** content of an index in response to a REINDEX command.
! 83404: **
! 83405: ** if memRootPage is not negative, it means that the index is newly
! 83406: ** created. The register specified by memRootPage contains the
! 83407: ** root page number of the index. If memRootPage is negative, then
! 83408: ** the index already exists and must be cleared before being refilled and
! 83409: ** the root page number of the index is taken from pIndex->tnum.
! 83410: */
! 83411: static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
! 83412: Table *pTab = pIndex->pTable; /* The table that is indexed */
! 83413: int iTab = pParse->nTab++; /* Btree cursor used for pTab */
! 83414: int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
! 83415: int iSorter; /* Cursor opened by OpenSorter (if in use) */
! 83416: int addr1; /* Address of top of loop */
! 83417: int addr2; /* Address to jump to for next iteration */
! 83418: int tnum; /* Root page of index */
! 83419: Vdbe *v; /* Generate code into this virtual machine */
! 83420: KeyInfo *pKey; /* KeyInfo for index */
! 83421: #ifdef SQLITE_OMIT_MERGE_SORT
! 83422: int regIdxKey; /* Registers containing the index key */
! 83423: #endif
! 83424: int regRecord; /* Register holding assemblied index record */
! 83425: sqlite3 *db = pParse->db; /* The database connection */
! 83426: int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
! 83427:
! 83428: #ifndef SQLITE_OMIT_AUTHORIZATION
! 83429: if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
! 83430: db->aDb[iDb].zName ) ){
! 83431: return;
! 83432: }
! 83433: #endif
! 83434:
! 83435: /* Require a write-lock on the table to perform this operation */
! 83436: sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
! 83437:
! 83438: v = sqlite3GetVdbe(pParse);
! 83439: if( v==0 ) return;
! 83440: if( memRootPage>=0 ){
! 83441: tnum = memRootPage;
! 83442: }else{
! 83443: tnum = pIndex->tnum;
! 83444: sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
! 83445: }
! 83446: pKey = sqlite3IndexKeyinfo(pParse, pIndex);
! 83447: sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
! 83448: (char *)pKey, P4_KEYINFO_HANDOFF);
! 83449: if( memRootPage>=0 ){
! 83450: sqlite3VdbeChangeP5(v, 1);
! 83451: }
! 83452:
! 83453: #ifndef SQLITE_OMIT_MERGE_SORT
! 83454: /* Open the sorter cursor if we are to use one. */
! 83455: iSorter = pParse->nTab++;
! 83456: sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
! 83457: #else
! 83458: iSorter = iTab;
! 83459: #endif
! 83460:
! 83461: /* Open the table. Loop through all rows of the table, inserting index
! 83462: ** records into the sorter. */
! 83463: sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
! 83464: addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
! 83465: regRecord = sqlite3GetTempReg(pParse);
! 83466:
! 83467: #ifndef SQLITE_OMIT_MERGE_SORT
! 83468: sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
! 83469: sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
! 83470: sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
! 83471: sqlite3VdbeJumpHere(v, addr1);
! 83472: addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
! 83473: if( pIndex->onError!=OE_None ){
! 83474: int j2 = sqlite3VdbeCurrentAddr(v) + 3;
! 83475: sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
! 83476: addr2 = sqlite3VdbeCurrentAddr(v);
! 83477: sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
! 83478: sqlite3HaltConstraint(
! 83479: pParse, OE_Abort, "indexed columns are not unique", P4_STATIC
! 83480: );
! 83481: }else{
! 83482: addr2 = sqlite3VdbeCurrentAddr(v);
! 83483: }
! 83484: sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
! 83485: sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
! 83486: sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
! 83487: #else
! 83488: regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
! 83489: addr2 = addr1 + 1;
! 83490: if( pIndex->onError!=OE_None ){
! 83491: const int regRowid = regIdxKey + pIndex->nColumn;
! 83492: const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
! 83493: void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
! 83494:
! 83495: /* The registers accessed by the OP_IsUnique opcode were allocated
! 83496: ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
! 83497: ** call above. Just before that function was freed they were released
! 83498: ** (made available to the compiler for reuse) using
! 83499: ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique
! 83500: ** opcode use the values stored within seems dangerous. However, since
! 83501: ** we can be sure that no other temp registers have been allocated
! 83502: ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
! 83503: */
! 83504: sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
! 83505: sqlite3HaltConstraint(
! 83506: pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
! 83507: }
! 83508: sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
! 83509: sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
! 83510: #endif
! 83511: sqlite3ReleaseTempReg(pParse, regRecord);
! 83512: sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
! 83513: sqlite3VdbeJumpHere(v, addr1);
! 83514:
! 83515: sqlite3VdbeAddOp1(v, OP_Close, iTab);
! 83516: sqlite3VdbeAddOp1(v, OP_Close, iIdx);
! 83517: sqlite3VdbeAddOp1(v, OP_Close, iSorter);
! 83518: }
! 83519:
! 83520: /*
! 83521: ** Create a new index for an SQL table. pName1.pName2 is the name of the index
! 83522: ** and pTblList is the name of the table that is to be indexed. Both will
! 83523: ** be NULL for a primary key or an index that is created to satisfy a
! 83524: ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
! 83525: ** as the table to be indexed. pParse->pNewTable is a table that is
! 83526: ** currently being constructed by a CREATE TABLE statement.
! 83527: **
! 83528: ** pList is a list of columns to be indexed. pList will be NULL if this
! 83529: ** is a primary key or unique-constraint on the most recent column added
! 83530: ** to the table currently under construction.
! 83531: **
! 83532: ** If the index is created successfully, return a pointer to the new Index
! 83533: ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
! 83534: ** as the tables primary key (Index.autoIndex==2).
! 83535: */
! 83536: SQLITE_PRIVATE Index *sqlite3CreateIndex(
! 83537: Parse *pParse, /* All information about this parse */
! 83538: Token *pName1, /* First part of index name. May be NULL */
! 83539: Token *pName2, /* Second part of index name. May be NULL */
! 83540: SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
! 83541: ExprList *pList, /* A list of columns to be indexed */
! 83542: int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
! 83543: Token *pStart, /* The CREATE token that begins this statement */
! 83544: Token *pEnd, /* The ")" that closes the CREATE INDEX statement */
! 83545: int sortOrder, /* Sort order of primary key when pList==NULL */
! 83546: int ifNotExist /* Omit error if index already exists */
! 83547: ){
! 83548: Index *pRet = 0; /* Pointer to return */
! 83549: Table *pTab = 0; /* Table to be indexed */
! 83550: Index *pIndex = 0; /* The index to be created */
! 83551: char *zName = 0; /* Name of the index */
! 83552: int nName; /* Number of characters in zName */
! 83553: int i, j;
! 83554: Token nullId; /* Fake token for an empty ID list */
! 83555: DbFixer sFix; /* For assigning database names to pTable */
! 83556: int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
! 83557: sqlite3 *db = pParse->db;
! 83558: Db *pDb; /* The specific table containing the indexed database */
! 83559: int iDb; /* Index of the database that is being written */
! 83560: Token *pName = 0; /* Unqualified name of the index to create */
! 83561: struct ExprList_item *pListItem; /* For looping over pList */
! 83562: int nCol;
! 83563: int nExtra = 0;
! 83564: char *zExtra;
! 83565:
! 83566: assert( pStart==0 || pEnd!=0 ); /* pEnd must be non-NULL if pStart is */
! 83567: assert( pParse->nErr==0 ); /* Never called with prior errors */
! 83568: if( db->mallocFailed || IN_DECLARE_VTAB ){
! 83569: goto exit_create_index;
! 83570: }
! 83571: if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
! 83572: goto exit_create_index;
! 83573: }
! 83574:
! 83575: /*
! 83576: ** Find the table that is to be indexed. Return early if not found.
! 83577: */
! 83578: if( pTblName!=0 ){
! 83579:
! 83580: /* Use the two-part index name to determine the database
! 83581: ** to search for the table. 'Fix' the table name to this db
! 83582: ** before looking up the table.
! 83583: */
! 83584: assert( pName1 && pName2 );
! 83585: iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
! 83586: if( iDb<0 ) goto exit_create_index;
! 83587: assert( pName && pName->z );
! 83588:
! 83589: #ifndef SQLITE_OMIT_TEMPDB
! 83590: /* If the index name was unqualified, check if the the table
! 83591: ** is a temp table. If so, set the database to 1. Do not do this
! 83592: ** if initialising a database schema.
! 83593: */
! 83594: if( !db->init.busy ){
! 83595: pTab = sqlite3SrcListLookup(pParse, pTblName);
! 83596: if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
! 83597: iDb = 1;
! 83598: }
! 83599: }
! 83600: #endif
! 83601:
! 83602: if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
! 83603: sqlite3FixSrcList(&sFix, pTblName)
! 83604: ){
! 83605: /* Because the parser constructs pTblName from a single identifier,
! 83606: ** sqlite3FixSrcList can never fail. */
! 83607: assert(0);
! 83608: }
! 83609: pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName,
! 83610: pTblName->a[0].zDatabase);
! 83611: if( !pTab || db->mallocFailed ) goto exit_create_index;
! 83612: assert( db->aDb[iDb].pSchema==pTab->pSchema );
! 83613: }else{
! 83614: assert( pName==0 );
! 83615: assert( pStart==0 );
! 83616: pTab = pParse->pNewTable;
! 83617: if( !pTab ) goto exit_create_index;
! 83618: iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 83619: }
! 83620: pDb = &db->aDb[iDb];
! 83621:
! 83622: assert( pTab!=0 );
! 83623: assert( pParse->nErr==0 );
! 83624: if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
! 83625: && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){
! 83626: sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
! 83627: goto exit_create_index;
! 83628: }
! 83629: #ifndef SQLITE_OMIT_VIEW
! 83630: if( pTab->pSelect ){
! 83631: sqlite3ErrorMsg(pParse, "views may not be indexed");
! 83632: goto exit_create_index;
! 83633: }
! 83634: #endif
! 83635: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 83636: if( IsVirtual(pTab) ){
! 83637: sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
! 83638: goto exit_create_index;
! 83639: }
! 83640: #endif
! 83641:
! 83642: /*
! 83643: ** Find the name of the index. Make sure there is not already another
! 83644: ** index or table with the same name.
! 83645: **
! 83646: ** Exception: If we are reading the names of permanent indices from the
! 83647: ** sqlite_master table (because some other process changed the schema) and
! 83648: ** one of the index names collides with the name of a temporary table or
! 83649: ** index, then we will continue to process this index.
! 83650: **
! 83651: ** If pName==0 it means that we are
! 83652: ** dealing with a primary key or UNIQUE constraint. We have to invent our
! 83653: ** own name.
! 83654: */
! 83655: if( pName ){
! 83656: zName = sqlite3NameFromToken(db, pName);
! 83657: if( zName==0 ) goto exit_create_index;
! 83658: assert( pName->z!=0 );
! 83659: if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
! 83660: goto exit_create_index;
! 83661: }
! 83662: if( !db->init.busy ){
! 83663: if( sqlite3FindTable(db, zName, 0)!=0 ){
! 83664: sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
! 83665: goto exit_create_index;
! 83666: }
! 83667: }
! 83668: if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
! 83669: if( !ifNotExist ){
! 83670: sqlite3ErrorMsg(pParse, "index %s already exists", zName);
! 83671: }else{
! 83672: assert( !db->init.busy );
! 83673: sqlite3CodeVerifySchema(pParse, iDb);
! 83674: }
! 83675: goto exit_create_index;
! 83676: }
! 83677: }else{
! 83678: int n;
! 83679: Index *pLoop;
! 83680: for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
! 83681: zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
! 83682: if( zName==0 ){
! 83683: goto exit_create_index;
! 83684: }
! 83685: }
! 83686:
! 83687: /* Check for authorization to create an index.
! 83688: */
! 83689: #ifndef SQLITE_OMIT_AUTHORIZATION
! 83690: {
! 83691: const char *zDb = pDb->zName;
! 83692: if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
! 83693: goto exit_create_index;
! 83694: }
! 83695: i = SQLITE_CREATE_INDEX;
! 83696: if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
! 83697: if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
! 83698: goto exit_create_index;
! 83699: }
! 83700: }
! 83701: #endif
! 83702:
! 83703: /* If pList==0, it means this routine was called to make a primary
! 83704: ** key out of the last column added to the table under construction.
! 83705: ** So create a fake list to simulate this.
! 83706: */
! 83707: if( pList==0 ){
! 83708: nullId.z = pTab->aCol[pTab->nCol-1].zName;
! 83709: nullId.n = sqlite3Strlen30((char*)nullId.z);
! 83710: pList = sqlite3ExprListAppend(pParse, 0, 0);
! 83711: if( pList==0 ) goto exit_create_index;
! 83712: sqlite3ExprListSetName(pParse, pList, &nullId, 0);
! 83713: pList->a[0].sortOrder = (u8)sortOrder;
! 83714: }
! 83715:
! 83716: /* Figure out how many bytes of space are required to store explicitly
! 83717: ** specified collation sequence names.
! 83718: */
! 83719: for(i=0; i<pList->nExpr; i++){
! 83720: Expr *pExpr = pList->a[i].pExpr;
! 83721: if( pExpr ){
! 83722: CollSeq *pColl = pExpr->pColl;
! 83723: /* Either pColl!=0 or there was an OOM failure. But if an OOM
! 83724: ** failure we have quit before reaching this point. */
! 83725: if( ALWAYS(pColl) ){
! 83726: nExtra += (1 + sqlite3Strlen30(pColl->zName));
! 83727: }
! 83728: }
! 83729: }
! 83730:
! 83731: /*
! 83732: ** Allocate the index structure.
! 83733: */
! 83734: nName = sqlite3Strlen30(zName);
! 83735: nCol = pList->nExpr;
! 83736: pIndex = sqlite3DbMallocZero(db,
! 83737: ROUND8(sizeof(Index)) + /* Index structure */
! 83738: ROUND8(sizeof(tRowcnt)*(nCol+1)) + /* Index.aiRowEst */
! 83739: sizeof(char *)*nCol + /* Index.azColl */
! 83740: sizeof(int)*nCol + /* Index.aiColumn */
! 83741: sizeof(u8)*nCol + /* Index.aSortOrder */
! 83742: nName + 1 + /* Index.zName */
! 83743: nExtra /* Collation sequence names */
! 83744: );
! 83745: if( db->mallocFailed ){
! 83746: goto exit_create_index;
! 83747: }
! 83748: zExtra = (char*)pIndex;
! 83749: pIndex->aiRowEst = (tRowcnt*)&zExtra[ROUND8(sizeof(Index))];
! 83750: pIndex->azColl = (char**)
! 83751: ((char*)pIndex->aiRowEst + ROUND8(sizeof(tRowcnt)*nCol+1));
! 83752: assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
! 83753: assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
! 83754: pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
! 83755: pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
! 83756: pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
! 83757: zExtra = (char *)(&pIndex->zName[nName+1]);
! 83758: memcpy(pIndex->zName, zName, nName+1);
! 83759: pIndex->pTable = pTab;
! 83760: pIndex->nColumn = pList->nExpr;
! 83761: pIndex->onError = (u8)onError;
! 83762: pIndex->autoIndex = (u8)(pName==0);
! 83763: pIndex->pSchema = db->aDb[iDb].pSchema;
! 83764: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 83765:
! 83766: /* Check to see if we should honor DESC requests on index columns
! 83767: */
! 83768: if( pDb->pSchema->file_format>=4 ){
! 83769: sortOrderMask = -1; /* Honor DESC */
! 83770: }else{
! 83771: sortOrderMask = 0; /* Ignore DESC */
! 83772: }
! 83773:
! 83774: /* Scan the names of the columns of the table to be indexed and
! 83775: ** load the column indices into the Index structure. Report an error
! 83776: ** if any column is not found.
! 83777: **
! 83778: ** TODO: Add a test to make sure that the same column is not named
! 83779: ** more than once within the same index. Only the first instance of
! 83780: ** the column will ever be used by the optimizer. Note that using the
! 83781: ** same column more than once cannot be an error because that would
! 83782: ** break backwards compatibility - it needs to be a warning.
! 83783: */
! 83784: for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
! 83785: const char *zColName = pListItem->zName;
! 83786: Column *pTabCol;
! 83787: int requestedSortOrder;
! 83788: char *zColl; /* Collation sequence name */
! 83789:
! 83790: for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
! 83791: if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
! 83792: }
! 83793: if( j>=pTab->nCol ){
! 83794: sqlite3ErrorMsg(pParse, "table %s has no column named %s",
! 83795: pTab->zName, zColName);
! 83796: pParse->checkSchema = 1;
! 83797: goto exit_create_index;
! 83798: }
! 83799: pIndex->aiColumn[i] = j;
! 83800: /* Justification of the ALWAYS(pListItem->pExpr->pColl): Because of
! 83801: ** the way the "idxlist" non-terminal is constructed by the parser,
! 83802: ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
! 83803: ** must exist or else there must have been an OOM error. But if there
! 83804: ** was an OOM error, we would never reach this point. */
! 83805: if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){
! 83806: int nColl;
! 83807: zColl = pListItem->pExpr->pColl->zName;
! 83808: nColl = sqlite3Strlen30(zColl) + 1;
! 83809: assert( nExtra>=nColl );
! 83810: memcpy(zExtra, zColl, nColl);
! 83811: zColl = zExtra;
! 83812: zExtra += nColl;
! 83813: nExtra -= nColl;
! 83814: }else{
! 83815: zColl = pTab->aCol[j].zColl;
! 83816: if( !zColl ){
! 83817: zColl = db->pDfltColl->zName;
! 83818: }
! 83819: }
! 83820: if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
! 83821: goto exit_create_index;
! 83822: }
! 83823: pIndex->azColl[i] = zColl;
! 83824: requestedSortOrder = pListItem->sortOrder & sortOrderMask;
! 83825: pIndex->aSortOrder[i] = (u8)requestedSortOrder;
! 83826: }
! 83827: sqlite3DefaultRowEst(pIndex);
! 83828:
! 83829: if( pTab==pParse->pNewTable ){
! 83830: /* This routine has been called to create an automatic index as a
! 83831: ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
! 83832: ** a PRIMARY KEY or UNIQUE clause following the column definitions.
! 83833: ** i.e. one of:
! 83834: **
! 83835: ** CREATE TABLE t(x PRIMARY KEY, y);
! 83836: ** CREATE TABLE t(x, y, UNIQUE(x, y));
! 83837: **
! 83838: ** Either way, check to see if the table already has such an index. If
! 83839: ** so, don't bother creating this one. This only applies to
! 83840: ** automatically created indices. Users can do as they wish with
! 83841: ** explicit indices.
! 83842: **
! 83843: ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
! 83844: ** (and thus suppressing the second one) even if they have different
! 83845: ** sort orders.
! 83846: **
! 83847: ** If there are different collating sequences or if the columns of
! 83848: ** the constraint occur in different orders, then the constraints are
! 83849: ** considered distinct and both result in separate indices.
! 83850: */
! 83851: Index *pIdx;
! 83852: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
! 83853: int k;
! 83854: assert( pIdx->onError!=OE_None );
! 83855: assert( pIdx->autoIndex );
! 83856: assert( pIndex->onError!=OE_None );
! 83857:
! 83858: if( pIdx->nColumn!=pIndex->nColumn ) continue;
! 83859: for(k=0; k<pIdx->nColumn; k++){
! 83860: const char *z1;
! 83861: const char *z2;
! 83862: if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
! 83863: z1 = pIdx->azColl[k];
! 83864: z2 = pIndex->azColl[k];
! 83865: if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
! 83866: }
! 83867: if( k==pIdx->nColumn ){
! 83868: if( pIdx->onError!=pIndex->onError ){
! 83869: /* This constraint creates the same index as a previous
! 83870: ** constraint specified somewhere in the CREATE TABLE statement.
! 83871: ** However the ON CONFLICT clauses are different. If both this
! 83872: ** constraint and the previous equivalent constraint have explicit
! 83873: ** ON CONFLICT clauses this is an error. Otherwise, use the
! 83874: ** explicitly specified behaviour for the index.
! 83875: */
! 83876: if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
! 83877: sqlite3ErrorMsg(pParse,
! 83878: "conflicting ON CONFLICT clauses specified", 0);
! 83879: }
! 83880: if( pIdx->onError==OE_Default ){
! 83881: pIdx->onError = pIndex->onError;
! 83882: }
! 83883: }
! 83884: goto exit_create_index;
! 83885: }
! 83886: }
! 83887: }
! 83888:
! 83889: /* Link the new Index structure to its table and to the other
! 83890: ** in-memory database structures.
! 83891: */
! 83892: if( db->init.busy ){
! 83893: Index *p;
! 83894: assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
! 83895: p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
! 83896: pIndex->zName, sqlite3Strlen30(pIndex->zName),
! 83897: pIndex);
! 83898: if( p ){
! 83899: assert( p==pIndex ); /* Malloc must have failed */
! 83900: db->mallocFailed = 1;
! 83901: goto exit_create_index;
! 83902: }
! 83903: db->flags |= SQLITE_InternChanges;
! 83904: if( pTblName!=0 ){
! 83905: pIndex->tnum = db->init.newTnum;
! 83906: }
! 83907: }
! 83908:
! 83909: /* If the db->init.busy is 0 then create the index on disk. This
! 83910: ** involves writing the index into the master table and filling in the
! 83911: ** index with the current table contents.
! 83912: **
! 83913: ** The db->init.busy is 0 when the user first enters a CREATE INDEX
! 83914: ** command. db->init.busy is 1 when a database is opened and
! 83915: ** CREATE INDEX statements are read out of the master table. In
! 83916: ** the latter case the index already exists on disk, which is why
! 83917: ** we don't want to recreate it.
! 83918: **
! 83919: ** If pTblName==0 it means this index is generated as a primary key
! 83920: ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
! 83921: ** has just been created, it contains no data and the index initialization
! 83922: ** step can be skipped.
! 83923: */
! 83924: else{ /* if( db->init.busy==0 ) */
! 83925: Vdbe *v;
! 83926: char *zStmt;
! 83927: int iMem = ++pParse->nMem;
! 83928:
! 83929: v = sqlite3GetVdbe(pParse);
! 83930: if( v==0 ) goto exit_create_index;
! 83931:
! 83932:
! 83933: /* Create the rootpage for the index
! 83934: */
! 83935: sqlite3BeginWriteOperation(pParse, 1, iDb);
! 83936: sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
! 83937:
! 83938: /* Gather the complete text of the CREATE INDEX statement into
! 83939: ** the zStmt variable
! 83940: */
! 83941: if( pStart ){
! 83942: assert( pEnd!=0 );
! 83943: /* A named index with an explicit CREATE INDEX statement */
! 83944: zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
! 83945: onError==OE_None ? "" : " UNIQUE",
! 83946: (int)(pEnd->z - pName->z) + 1,
! 83947: pName->z);
! 83948: }else{
! 83949: /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
! 83950: /* zStmt = sqlite3MPrintf(""); */
! 83951: zStmt = 0;
! 83952: }
! 83953:
! 83954: /* Add an entry in sqlite_master for this index
! 83955: */
! 83956: sqlite3NestedParse(pParse,
! 83957: "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
! 83958: db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
! 83959: pIndex->zName,
! 83960: pTab->zName,
! 83961: iMem,
! 83962: zStmt
! 83963: );
! 83964: sqlite3DbFree(db, zStmt);
! 83965:
! 83966: /* Fill the index with data and reparse the schema. Code an OP_Expire
! 83967: ** to invalidate all pre-compiled statements.
! 83968: */
! 83969: if( pTblName ){
! 83970: sqlite3RefillIndex(pParse, pIndex, iMem);
! 83971: sqlite3ChangeCookie(pParse, iDb);
! 83972: sqlite3VdbeAddParseSchemaOp(v, iDb,
! 83973: sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
! 83974: sqlite3VdbeAddOp1(v, OP_Expire, 0);
! 83975: }
! 83976: }
! 83977:
! 83978: /* When adding an index to the list of indices for a table, make
! 83979: ** sure all indices labeled OE_Replace come after all those labeled
! 83980: ** OE_Ignore. This is necessary for the correct constraint check
! 83981: ** processing (in sqlite3GenerateConstraintChecks()) as part of
! 83982: ** UPDATE and INSERT statements.
! 83983: */
! 83984: if( db->init.busy || pTblName==0 ){
! 83985: if( onError!=OE_Replace || pTab->pIndex==0
! 83986: || pTab->pIndex->onError==OE_Replace){
! 83987: pIndex->pNext = pTab->pIndex;
! 83988: pTab->pIndex = pIndex;
! 83989: }else{
! 83990: Index *pOther = pTab->pIndex;
! 83991: while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
! 83992: pOther = pOther->pNext;
! 83993: }
! 83994: pIndex->pNext = pOther->pNext;
! 83995: pOther->pNext = pIndex;
! 83996: }
! 83997: pRet = pIndex;
! 83998: pIndex = 0;
! 83999: }
! 84000:
! 84001: /* Clean up before exiting */
! 84002: exit_create_index:
! 84003: if( pIndex ){
! 84004: sqlite3DbFree(db, pIndex->zColAff);
! 84005: sqlite3DbFree(db, pIndex);
! 84006: }
! 84007: sqlite3ExprListDelete(db, pList);
! 84008: sqlite3SrcListDelete(db, pTblName);
! 84009: sqlite3DbFree(db, zName);
! 84010: return pRet;
! 84011: }
! 84012:
! 84013: /*
! 84014: ** Fill the Index.aiRowEst[] array with default information - information
! 84015: ** to be used when we have not run the ANALYZE command.
! 84016: **
! 84017: ** aiRowEst[0] is suppose to contain the number of elements in the index.
! 84018: ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
! 84019: ** number of rows in the table that match any particular value of the
! 84020: ** first column of the index. aiRowEst[2] is an estimate of the number
! 84021: ** of rows that match any particular combiniation of the first 2 columns
! 84022: ** of the index. And so forth. It must always be the case that
! 84023: *
! 84024: ** aiRowEst[N]<=aiRowEst[N-1]
! 84025: ** aiRowEst[N]>=1
! 84026: **
! 84027: ** Apart from that, we have little to go on besides intuition as to
! 84028: ** how aiRowEst[] should be initialized. The numbers generated here
! 84029: ** are based on typical values found in actual indices.
! 84030: */
! 84031: SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
! 84032: tRowcnt *a = pIdx->aiRowEst;
! 84033: int i;
! 84034: tRowcnt n;
! 84035: assert( a!=0 );
! 84036: a[0] = pIdx->pTable->nRowEst;
! 84037: if( a[0]<10 ) a[0] = 10;
! 84038: n = 10;
! 84039: for(i=1; i<=pIdx->nColumn; i++){
! 84040: a[i] = n;
! 84041: if( n>5 ) n--;
! 84042: }
! 84043: if( pIdx->onError!=OE_None ){
! 84044: a[pIdx->nColumn] = 1;
! 84045: }
! 84046: }
! 84047:
! 84048: /*
! 84049: ** This routine will drop an existing named index. This routine
! 84050: ** implements the DROP INDEX statement.
! 84051: */
! 84052: SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
! 84053: Index *pIndex;
! 84054: Vdbe *v;
! 84055: sqlite3 *db = pParse->db;
! 84056: int iDb;
! 84057:
! 84058: assert( pParse->nErr==0 ); /* Never called with prior errors */
! 84059: if( db->mallocFailed ){
! 84060: goto exit_drop_index;
! 84061: }
! 84062: assert( pName->nSrc==1 );
! 84063: if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
! 84064: goto exit_drop_index;
! 84065: }
! 84066: pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
! 84067: if( pIndex==0 ){
! 84068: if( !ifExists ){
! 84069: sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
! 84070: }else{
! 84071: sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
! 84072: }
! 84073: pParse->checkSchema = 1;
! 84074: goto exit_drop_index;
! 84075: }
! 84076: if( pIndex->autoIndex ){
! 84077: sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
! 84078: "or PRIMARY KEY constraint cannot be dropped", 0);
! 84079: goto exit_drop_index;
! 84080: }
! 84081: iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
! 84082: #ifndef SQLITE_OMIT_AUTHORIZATION
! 84083: {
! 84084: int code = SQLITE_DROP_INDEX;
! 84085: Table *pTab = pIndex->pTable;
! 84086: const char *zDb = db->aDb[iDb].zName;
! 84087: const char *zTab = SCHEMA_TABLE(iDb);
! 84088: if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
! 84089: goto exit_drop_index;
! 84090: }
! 84091: if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
! 84092: if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
! 84093: goto exit_drop_index;
! 84094: }
! 84095: }
! 84096: #endif
! 84097:
! 84098: /* Generate code to remove the index and from the master table */
! 84099: v = sqlite3GetVdbe(pParse);
! 84100: if( v ){
! 84101: sqlite3BeginWriteOperation(pParse, 1, iDb);
! 84102: sqlite3NestedParse(pParse,
! 84103: "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
! 84104: db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
! 84105: );
! 84106: sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
! 84107: sqlite3ChangeCookie(pParse, iDb);
! 84108: destroyRootPage(pParse, pIndex->tnum, iDb);
! 84109: sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
! 84110: }
! 84111:
! 84112: exit_drop_index:
! 84113: sqlite3SrcListDelete(db, pName);
! 84114: }
! 84115:
! 84116: /*
! 84117: ** pArray is a pointer to an array of objects. Each object in the
! 84118: ** array is szEntry bytes in size. This routine allocates a new
! 84119: ** object on the end of the array.
! 84120: **
! 84121: ** *pnEntry is the number of entries already in use. *pnAlloc is
! 84122: ** the previously allocated size of the array. initSize is the
! 84123: ** suggested initial array size allocation.
! 84124: **
! 84125: ** The index of the new entry is returned in *pIdx.
! 84126: **
! 84127: ** This routine returns a pointer to the array of objects. This
! 84128: ** might be the same as the pArray parameter or it might be a different
! 84129: ** pointer if the array was resized.
! 84130: */
! 84131: SQLITE_PRIVATE void *sqlite3ArrayAllocate(
! 84132: sqlite3 *db, /* Connection to notify of malloc failures */
! 84133: void *pArray, /* Array of objects. Might be reallocated */
! 84134: int szEntry, /* Size of each object in the array */
! 84135: int initSize, /* Suggested initial allocation, in elements */
! 84136: int *pnEntry, /* Number of objects currently in use */
! 84137: int *pnAlloc, /* Current size of the allocation, in elements */
! 84138: int *pIdx /* Write the index of a new slot here */
! 84139: ){
! 84140: char *z;
! 84141: if( *pnEntry >= *pnAlloc ){
! 84142: void *pNew;
! 84143: int newSize;
! 84144: newSize = (*pnAlloc)*2 + initSize;
! 84145: pNew = sqlite3DbRealloc(db, pArray, newSize*szEntry);
! 84146: if( pNew==0 ){
! 84147: *pIdx = -1;
! 84148: return pArray;
! 84149: }
! 84150: *pnAlloc = sqlite3DbMallocSize(db, pNew)/szEntry;
! 84151: pArray = pNew;
! 84152: }
! 84153: z = (char*)pArray;
! 84154: memset(&z[*pnEntry * szEntry], 0, szEntry);
! 84155: *pIdx = *pnEntry;
! 84156: ++*pnEntry;
! 84157: return pArray;
! 84158: }
! 84159:
! 84160: /*
! 84161: ** Append a new element to the given IdList. Create a new IdList if
! 84162: ** need be.
! 84163: **
! 84164: ** A new IdList is returned, or NULL if malloc() fails.
! 84165: */
! 84166: SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
! 84167: int i;
! 84168: if( pList==0 ){
! 84169: pList = sqlite3DbMallocZero(db, sizeof(IdList) );
! 84170: if( pList==0 ) return 0;
! 84171: pList->nAlloc = 0;
! 84172: }
! 84173: pList->a = sqlite3ArrayAllocate(
! 84174: db,
! 84175: pList->a,
! 84176: sizeof(pList->a[0]),
! 84177: 5,
! 84178: &pList->nId,
! 84179: &pList->nAlloc,
! 84180: &i
! 84181: );
! 84182: if( i<0 ){
! 84183: sqlite3IdListDelete(db, pList);
! 84184: return 0;
! 84185: }
! 84186: pList->a[i].zName = sqlite3NameFromToken(db, pToken);
! 84187: return pList;
! 84188: }
! 84189:
! 84190: /*
! 84191: ** Delete an IdList.
! 84192: */
! 84193: SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
! 84194: int i;
! 84195: if( pList==0 ) return;
! 84196: for(i=0; i<pList->nId; i++){
! 84197: sqlite3DbFree(db, pList->a[i].zName);
! 84198: }
! 84199: sqlite3DbFree(db, pList->a);
! 84200: sqlite3DbFree(db, pList);
! 84201: }
! 84202:
! 84203: /*
! 84204: ** Return the index in pList of the identifier named zId. Return -1
! 84205: ** if not found.
! 84206: */
! 84207: SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
! 84208: int i;
! 84209: if( pList==0 ) return -1;
! 84210: for(i=0; i<pList->nId; i++){
! 84211: if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
! 84212: }
! 84213: return -1;
! 84214: }
! 84215:
! 84216: /*
! 84217: ** Expand the space allocated for the given SrcList object by
! 84218: ** creating nExtra new slots beginning at iStart. iStart is zero based.
! 84219: ** New slots are zeroed.
! 84220: **
! 84221: ** For example, suppose a SrcList initially contains two entries: A,B.
! 84222: ** To append 3 new entries onto the end, do this:
! 84223: **
! 84224: ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
! 84225: **
! 84226: ** After the call above it would contain: A, B, nil, nil, nil.
! 84227: ** If the iStart argument had been 1 instead of 2, then the result
! 84228: ** would have been: A, nil, nil, nil, B. To prepend the new slots,
! 84229: ** the iStart value would be 0. The result then would
! 84230: ** be: nil, nil, nil, A, B.
! 84231: **
! 84232: ** If a memory allocation fails the SrcList is unchanged. The
! 84233: ** db->mallocFailed flag will be set to true.
! 84234: */
! 84235: SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
! 84236: sqlite3 *db, /* Database connection to notify of OOM errors */
! 84237: SrcList *pSrc, /* The SrcList to be enlarged */
! 84238: int nExtra, /* Number of new slots to add to pSrc->a[] */
! 84239: int iStart /* Index in pSrc->a[] of first new slot */
! 84240: ){
! 84241: int i;
! 84242:
! 84243: /* Sanity checking on calling parameters */
! 84244: assert( iStart>=0 );
! 84245: assert( nExtra>=1 );
! 84246: assert( pSrc!=0 );
! 84247: assert( iStart<=pSrc->nSrc );
! 84248:
! 84249: /* Allocate additional space if needed */
! 84250: if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
! 84251: SrcList *pNew;
! 84252: int nAlloc = pSrc->nSrc+nExtra;
! 84253: int nGot;
! 84254: pNew = sqlite3DbRealloc(db, pSrc,
! 84255: sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
! 84256: if( pNew==0 ){
! 84257: assert( db->mallocFailed );
! 84258: return pSrc;
! 84259: }
! 84260: pSrc = pNew;
! 84261: nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
! 84262: pSrc->nAlloc = (u16)nGot;
! 84263: }
! 84264:
! 84265: /* Move existing slots that come after the newly inserted slots
! 84266: ** out of the way */
! 84267: for(i=pSrc->nSrc-1; i>=iStart; i--){
! 84268: pSrc->a[i+nExtra] = pSrc->a[i];
! 84269: }
! 84270: pSrc->nSrc += (i16)nExtra;
! 84271:
! 84272: /* Zero the newly allocated slots */
! 84273: memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
! 84274: for(i=iStart; i<iStart+nExtra; i++){
! 84275: pSrc->a[i].iCursor = -1;
! 84276: }
! 84277:
! 84278: /* Return a pointer to the enlarged SrcList */
! 84279: return pSrc;
! 84280: }
! 84281:
! 84282:
! 84283: /*
! 84284: ** Append a new table name to the given SrcList. Create a new SrcList if
! 84285: ** need be. A new entry is created in the SrcList even if pTable is NULL.
! 84286: **
! 84287: ** A SrcList is returned, or NULL if there is an OOM error. The returned
! 84288: ** SrcList might be the same as the SrcList that was input or it might be
! 84289: ** a new one. If an OOM error does occurs, then the prior value of pList
! 84290: ** that is input to this routine is automatically freed.
! 84291: **
! 84292: ** If pDatabase is not null, it means that the table has an optional
! 84293: ** database name prefix. Like this: "database.table". The pDatabase
! 84294: ** points to the table name and the pTable points to the database name.
! 84295: ** The SrcList.a[].zName field is filled with the table name which might
! 84296: ** come from pTable (if pDatabase is NULL) or from pDatabase.
! 84297: ** SrcList.a[].zDatabase is filled with the database name from pTable,
! 84298: ** or with NULL if no database is specified.
! 84299: **
! 84300: ** In other words, if call like this:
! 84301: **
! 84302: ** sqlite3SrcListAppend(D,A,B,0);
! 84303: **
! 84304: ** Then B is a table name and the database name is unspecified. If called
! 84305: ** like this:
! 84306: **
! 84307: ** sqlite3SrcListAppend(D,A,B,C);
! 84308: **
! 84309: ** Then C is the table name and B is the database name. If C is defined
! 84310: ** then so is B. In other words, we never have a case where:
! 84311: **
! 84312: ** sqlite3SrcListAppend(D,A,0,C);
! 84313: **
! 84314: ** Both pTable and pDatabase are assumed to be quoted. They are dequoted
! 84315: ** before being added to the SrcList.
! 84316: */
! 84317: SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
! 84318: sqlite3 *db, /* Connection to notify of malloc failures */
! 84319: SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
! 84320: Token *pTable, /* Table to append */
! 84321: Token *pDatabase /* Database of the table */
! 84322: ){
! 84323: struct SrcList_item *pItem;
! 84324: assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
! 84325: if( pList==0 ){
! 84326: pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
! 84327: if( pList==0 ) return 0;
! 84328: pList->nAlloc = 1;
! 84329: }
! 84330: pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
! 84331: if( db->mallocFailed ){
! 84332: sqlite3SrcListDelete(db, pList);
! 84333: return 0;
! 84334: }
! 84335: pItem = &pList->a[pList->nSrc-1];
! 84336: if( pDatabase && pDatabase->z==0 ){
! 84337: pDatabase = 0;
! 84338: }
! 84339: if( pDatabase ){
! 84340: Token *pTemp = pDatabase;
! 84341: pDatabase = pTable;
! 84342: pTable = pTemp;
! 84343: }
! 84344: pItem->zName = sqlite3NameFromToken(db, pTable);
! 84345: pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
! 84346: return pList;
! 84347: }
! 84348:
! 84349: /*
! 84350: ** Assign VdbeCursor index numbers to all tables in a SrcList
! 84351: */
! 84352: SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
! 84353: int i;
! 84354: struct SrcList_item *pItem;
! 84355: assert(pList || pParse->db->mallocFailed );
! 84356: if( pList ){
! 84357: for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
! 84358: if( pItem->iCursor>=0 ) break;
! 84359: pItem->iCursor = pParse->nTab++;
! 84360: if( pItem->pSelect ){
! 84361: sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
! 84362: }
! 84363: }
! 84364: }
! 84365: }
! 84366:
! 84367: /*
! 84368: ** Delete an entire SrcList including all its substructure.
! 84369: */
! 84370: SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
! 84371: int i;
! 84372: struct SrcList_item *pItem;
! 84373: if( pList==0 ) return;
! 84374: for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
! 84375: sqlite3DbFree(db, pItem->zDatabase);
! 84376: sqlite3DbFree(db, pItem->zName);
! 84377: sqlite3DbFree(db, pItem->zAlias);
! 84378: sqlite3DbFree(db, pItem->zIndex);
! 84379: sqlite3DeleteTable(db, pItem->pTab);
! 84380: sqlite3SelectDelete(db, pItem->pSelect);
! 84381: sqlite3ExprDelete(db, pItem->pOn);
! 84382: sqlite3IdListDelete(db, pItem->pUsing);
! 84383: }
! 84384: sqlite3DbFree(db, pList);
! 84385: }
! 84386:
! 84387: /*
! 84388: ** This routine is called by the parser to add a new term to the
! 84389: ** end of a growing FROM clause. The "p" parameter is the part of
! 84390: ** the FROM clause that has already been constructed. "p" is NULL
! 84391: ** if this is the first term of the FROM clause. pTable and pDatabase
! 84392: ** are the name of the table and database named in the FROM clause term.
! 84393: ** pDatabase is NULL if the database name qualifier is missing - the
! 84394: ** usual case. If the term has a alias, then pAlias points to the
! 84395: ** alias token. If the term is a subquery, then pSubquery is the
! 84396: ** SELECT statement that the subquery encodes. The pTable and
! 84397: ** pDatabase parameters are NULL for subqueries. The pOn and pUsing
! 84398: ** parameters are the content of the ON and USING clauses.
! 84399: **
! 84400: ** Return a new SrcList which encodes is the FROM with the new
! 84401: ** term added.
! 84402: */
! 84403: SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
! 84404: Parse *pParse, /* Parsing context */
! 84405: SrcList *p, /* The left part of the FROM clause already seen */
! 84406: Token *pTable, /* Name of the table to add to the FROM clause */
! 84407: Token *pDatabase, /* Name of the database containing pTable */
! 84408: Token *pAlias, /* The right-hand side of the AS subexpression */
! 84409: Select *pSubquery, /* A subquery used in place of a table name */
! 84410: Expr *pOn, /* The ON clause of a join */
! 84411: IdList *pUsing /* The USING clause of a join */
! 84412: ){
! 84413: struct SrcList_item *pItem;
! 84414: sqlite3 *db = pParse->db;
! 84415: if( !p && (pOn || pUsing) ){
! 84416: sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
! 84417: (pOn ? "ON" : "USING")
! 84418: );
! 84419: goto append_from_error;
! 84420: }
! 84421: p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
! 84422: if( p==0 || NEVER(p->nSrc==0) ){
! 84423: goto append_from_error;
! 84424: }
! 84425: pItem = &p->a[p->nSrc-1];
! 84426: assert( pAlias!=0 );
! 84427: if( pAlias->n ){
! 84428: pItem->zAlias = sqlite3NameFromToken(db, pAlias);
! 84429: }
! 84430: pItem->pSelect = pSubquery;
! 84431: pItem->pOn = pOn;
! 84432: pItem->pUsing = pUsing;
! 84433: return p;
! 84434:
! 84435: append_from_error:
! 84436: assert( p==0 );
! 84437: sqlite3ExprDelete(db, pOn);
! 84438: sqlite3IdListDelete(db, pUsing);
! 84439: sqlite3SelectDelete(db, pSubquery);
! 84440: return 0;
! 84441: }
! 84442:
! 84443: /*
! 84444: ** Add an INDEXED BY or NOT INDEXED clause to the most recently added
! 84445: ** element of the source-list passed as the second argument.
! 84446: */
! 84447: SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
! 84448: assert( pIndexedBy!=0 );
! 84449: if( p && ALWAYS(p->nSrc>0) ){
! 84450: struct SrcList_item *pItem = &p->a[p->nSrc-1];
! 84451: assert( pItem->notIndexed==0 && pItem->zIndex==0 );
! 84452: if( pIndexedBy->n==1 && !pIndexedBy->z ){
! 84453: /* A "NOT INDEXED" clause was supplied. See parse.y
! 84454: ** construct "indexed_opt" for details. */
! 84455: pItem->notIndexed = 1;
! 84456: }else{
! 84457: pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
! 84458: }
! 84459: }
! 84460: }
! 84461:
! 84462: /*
! 84463: ** When building up a FROM clause in the parser, the join operator
! 84464: ** is initially attached to the left operand. But the code generator
! 84465: ** expects the join operator to be on the right operand. This routine
! 84466: ** Shifts all join operators from left to right for an entire FROM
! 84467: ** clause.
! 84468: **
! 84469: ** Example: Suppose the join is like this:
! 84470: **
! 84471: ** A natural cross join B
! 84472: **
! 84473: ** The operator is "natural cross join". The A and B operands are stored
! 84474: ** in p->a[0] and p->a[1], respectively. The parser initially stores the
! 84475: ** operator with A. This routine shifts that operator over to B.
! 84476: */
! 84477: SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
! 84478: if( p ){
! 84479: int i;
! 84480: assert( p->a || p->nSrc==0 );
! 84481: for(i=p->nSrc-1; i>0; i--){
! 84482: p->a[i].jointype = p->a[i-1].jointype;
! 84483: }
! 84484: p->a[0].jointype = 0;
! 84485: }
! 84486: }
! 84487:
! 84488: /*
! 84489: ** Begin a transaction
! 84490: */
! 84491: SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
! 84492: sqlite3 *db;
! 84493: Vdbe *v;
! 84494: int i;
! 84495:
! 84496: assert( pParse!=0 );
! 84497: db = pParse->db;
! 84498: assert( db!=0 );
! 84499: /* if( db->aDb[0].pBt==0 ) return; */
! 84500: if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
! 84501: return;
! 84502: }
! 84503: v = sqlite3GetVdbe(pParse);
! 84504: if( !v ) return;
! 84505: if( type!=TK_DEFERRED ){
! 84506: for(i=0; i<db->nDb; i++){
! 84507: sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
! 84508: sqlite3VdbeUsesBtree(v, i);
! 84509: }
! 84510: }
! 84511: sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
! 84512: }
! 84513:
! 84514: /*
! 84515: ** Commit a transaction
! 84516: */
! 84517: SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
! 84518: Vdbe *v;
! 84519:
! 84520: assert( pParse!=0 );
! 84521: assert( pParse->db!=0 );
! 84522: if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
! 84523: return;
! 84524: }
! 84525: v = sqlite3GetVdbe(pParse);
! 84526: if( v ){
! 84527: sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
! 84528: }
! 84529: }
! 84530:
! 84531: /*
! 84532: ** Rollback a transaction
! 84533: */
! 84534: SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
! 84535: Vdbe *v;
! 84536:
! 84537: assert( pParse!=0 );
! 84538: assert( pParse->db!=0 );
! 84539: if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
! 84540: return;
! 84541: }
! 84542: v = sqlite3GetVdbe(pParse);
! 84543: if( v ){
! 84544: sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
! 84545: }
! 84546: }
! 84547:
! 84548: /*
! 84549: ** This function is called by the parser when it parses a command to create,
! 84550: ** release or rollback an SQL savepoint.
! 84551: */
! 84552: SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
! 84553: char *zName = sqlite3NameFromToken(pParse->db, pName);
! 84554: if( zName ){
! 84555: Vdbe *v = sqlite3GetVdbe(pParse);
! 84556: #ifndef SQLITE_OMIT_AUTHORIZATION
! 84557: static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
! 84558: assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
! 84559: #endif
! 84560: if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
! 84561: sqlite3DbFree(pParse->db, zName);
! 84562: return;
! 84563: }
! 84564: sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
! 84565: }
! 84566: }
! 84567:
! 84568: /*
! 84569: ** Make sure the TEMP database is open and available for use. Return
! 84570: ** the number of errors. Leave any error messages in the pParse structure.
! 84571: */
! 84572: SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
! 84573: sqlite3 *db = pParse->db;
! 84574: if( db->aDb[1].pBt==0 && !pParse->explain ){
! 84575: int rc;
! 84576: Btree *pBt;
! 84577: static const int flags =
! 84578: SQLITE_OPEN_READWRITE |
! 84579: SQLITE_OPEN_CREATE |
! 84580: SQLITE_OPEN_EXCLUSIVE |
! 84581: SQLITE_OPEN_DELETEONCLOSE |
! 84582: SQLITE_OPEN_TEMP_DB;
! 84583:
! 84584: rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
! 84585: if( rc!=SQLITE_OK ){
! 84586: sqlite3ErrorMsg(pParse, "unable to open a temporary database "
! 84587: "file for storing temporary tables");
! 84588: pParse->rc = rc;
! 84589: return 1;
! 84590: }
! 84591: db->aDb[1].pBt = pBt;
! 84592: assert( db->aDb[1].pSchema );
! 84593: if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
! 84594: db->mallocFailed = 1;
! 84595: return 1;
! 84596: }
! 84597: }
! 84598: return 0;
! 84599: }
! 84600:
! 84601: /*
! 84602: ** Generate VDBE code that will verify the schema cookie and start
! 84603: ** a read-transaction for all named database files.
! 84604: **
! 84605: ** It is important that all schema cookies be verified and all
! 84606: ** read transactions be started before anything else happens in
! 84607: ** the VDBE program. But this routine can be called after much other
! 84608: ** code has been generated. So here is what we do:
! 84609: **
! 84610: ** The first time this routine is called, we code an OP_Goto that
! 84611: ** will jump to a subroutine at the end of the program. Then we
! 84612: ** record every database that needs its schema verified in the
! 84613: ** pParse->cookieMask field. Later, after all other code has been
! 84614: ** generated, the subroutine that does the cookie verifications and
! 84615: ** starts the transactions will be coded and the OP_Goto P2 value
! 84616: ** will be made to point to that subroutine. The generation of the
! 84617: ** cookie verification subroutine code happens in sqlite3FinishCoding().
! 84618: **
! 84619: ** If iDb<0 then code the OP_Goto only - don't set flag to verify the
! 84620: ** schema on any databases. This can be used to position the OP_Goto
! 84621: ** early in the code, before we know if any database tables will be used.
! 84622: */
! 84623: SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
! 84624: Parse *pToplevel = sqlite3ParseToplevel(pParse);
! 84625:
! 84626: if( pToplevel->cookieGoto==0 ){
! 84627: Vdbe *v = sqlite3GetVdbe(pToplevel);
! 84628: if( v==0 ) return; /* This only happens if there was a prior error */
! 84629: pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
! 84630: }
! 84631: if( iDb>=0 ){
! 84632: sqlite3 *db = pToplevel->db;
! 84633: yDbMask mask;
! 84634:
! 84635: assert( iDb<db->nDb );
! 84636: assert( db->aDb[iDb].pBt!=0 || iDb==1 );
! 84637: assert( iDb<SQLITE_MAX_ATTACHED+2 );
! 84638: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 84639: mask = ((yDbMask)1)<<iDb;
! 84640: if( (pToplevel->cookieMask & mask)==0 ){
! 84641: pToplevel->cookieMask |= mask;
! 84642: pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
! 84643: if( !OMIT_TEMPDB && iDb==1 ){
! 84644: sqlite3OpenTempDatabase(pToplevel);
! 84645: }
! 84646: }
! 84647: }
! 84648: }
! 84649:
! 84650: /*
! 84651: ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
! 84652: ** attached database. Otherwise, invoke it for the database named zDb only.
! 84653: */
! 84654: SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
! 84655: sqlite3 *db = pParse->db;
! 84656: int i;
! 84657: for(i=0; i<db->nDb; i++){
! 84658: Db *pDb = &db->aDb[i];
! 84659: if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
! 84660: sqlite3CodeVerifySchema(pParse, i);
! 84661: }
! 84662: }
! 84663: }
! 84664:
! 84665: /*
! 84666: ** Generate VDBE code that prepares for doing an operation that
! 84667: ** might change the database.
! 84668: **
! 84669: ** This routine starts a new transaction if we are not already within
! 84670: ** a transaction. If we are already within a transaction, then a checkpoint
! 84671: ** is set if the setStatement parameter is true. A checkpoint should
! 84672: ** be set for operations that might fail (due to a constraint) part of
! 84673: ** the way through and which will need to undo some writes without having to
! 84674: ** rollback the whole transaction. For operations where all constraints
! 84675: ** can be checked before any changes are made to the database, it is never
! 84676: ** necessary to undo a write and the checkpoint should not be set.
! 84677: */
! 84678: SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
! 84679: Parse *pToplevel = sqlite3ParseToplevel(pParse);
! 84680: sqlite3CodeVerifySchema(pParse, iDb);
! 84681: pToplevel->writeMask |= ((yDbMask)1)<<iDb;
! 84682: pToplevel->isMultiWrite |= setStatement;
! 84683: }
! 84684:
! 84685: /*
! 84686: ** Indicate that the statement currently under construction might write
! 84687: ** more than one entry (example: deleting one row then inserting another,
! 84688: ** inserting multiple rows in a table, or inserting a row and index entries.)
! 84689: ** If an abort occurs after some of these writes have completed, then it will
! 84690: ** be necessary to undo the completed writes.
! 84691: */
! 84692: SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
! 84693: Parse *pToplevel = sqlite3ParseToplevel(pParse);
! 84694: pToplevel->isMultiWrite = 1;
! 84695: }
! 84696:
! 84697: /*
! 84698: ** The code generator calls this routine if is discovers that it is
! 84699: ** possible to abort a statement prior to completion. In order to
! 84700: ** perform this abort without corrupting the database, we need to make
! 84701: ** sure that the statement is protected by a statement transaction.
! 84702: **
! 84703: ** Technically, we only need to set the mayAbort flag if the
! 84704: ** isMultiWrite flag was previously set. There is a time dependency
! 84705: ** such that the abort must occur after the multiwrite. This makes
! 84706: ** some statements involving the REPLACE conflict resolution algorithm
! 84707: ** go a little faster. But taking advantage of this time dependency
! 84708: ** makes it more difficult to prove that the code is correct (in
! 84709: ** particular, it prevents us from writing an effective
! 84710: ** implementation of sqlite3AssertMayAbort()) and so we have chosen
! 84711: ** to take the safe route and skip the optimization.
! 84712: */
! 84713: SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
! 84714: Parse *pToplevel = sqlite3ParseToplevel(pParse);
! 84715: pToplevel->mayAbort = 1;
! 84716: }
! 84717:
! 84718: /*
! 84719: ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
! 84720: ** error. The onError parameter determines which (if any) of the statement
! 84721: ** and/or current transaction is rolled back.
! 84722: */
! 84723: SQLITE_PRIVATE void sqlite3HaltConstraint(Parse *pParse, int onError, char *p4, int p4type){
! 84724: Vdbe *v = sqlite3GetVdbe(pParse);
! 84725: if( onError==OE_Abort ){
! 84726: sqlite3MayAbort(pParse);
! 84727: }
! 84728: sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type);
! 84729: }
! 84730:
! 84731: /*
! 84732: ** Check to see if pIndex uses the collating sequence pColl. Return
! 84733: ** true if it does and false if it does not.
! 84734: */
! 84735: #ifndef SQLITE_OMIT_REINDEX
! 84736: static int collationMatch(const char *zColl, Index *pIndex){
! 84737: int i;
! 84738: assert( zColl!=0 );
! 84739: for(i=0; i<pIndex->nColumn; i++){
! 84740: const char *z = pIndex->azColl[i];
! 84741: assert( z!=0 );
! 84742: if( 0==sqlite3StrICmp(z, zColl) ){
! 84743: return 1;
! 84744: }
! 84745: }
! 84746: return 0;
! 84747: }
! 84748: #endif
! 84749:
! 84750: /*
! 84751: ** Recompute all indices of pTab that use the collating sequence pColl.
! 84752: ** If pColl==0 then recompute all indices of pTab.
! 84753: */
! 84754: #ifndef SQLITE_OMIT_REINDEX
! 84755: static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
! 84756: Index *pIndex; /* An index associated with pTab */
! 84757:
! 84758: for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
! 84759: if( zColl==0 || collationMatch(zColl, pIndex) ){
! 84760: int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
! 84761: sqlite3BeginWriteOperation(pParse, 0, iDb);
! 84762: sqlite3RefillIndex(pParse, pIndex, -1);
! 84763: }
! 84764: }
! 84765: }
! 84766: #endif
! 84767:
! 84768: /*
! 84769: ** Recompute all indices of all tables in all databases where the
! 84770: ** indices use the collating sequence pColl. If pColl==0 then recompute
! 84771: ** all indices everywhere.
! 84772: */
! 84773: #ifndef SQLITE_OMIT_REINDEX
! 84774: static void reindexDatabases(Parse *pParse, char const *zColl){
! 84775: Db *pDb; /* A single database */
! 84776: int iDb; /* The database index number */
! 84777: sqlite3 *db = pParse->db; /* The database connection */
! 84778: HashElem *k; /* For looping over tables in pDb */
! 84779: Table *pTab; /* A table in the database */
! 84780:
! 84781: assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */
! 84782: for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
! 84783: assert( pDb!=0 );
! 84784: for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
! 84785: pTab = (Table*)sqliteHashData(k);
! 84786: reindexTable(pParse, pTab, zColl);
! 84787: }
! 84788: }
! 84789: }
! 84790: #endif
! 84791:
! 84792: /*
! 84793: ** Generate code for the REINDEX command.
! 84794: **
! 84795: ** REINDEX -- 1
! 84796: ** REINDEX <collation> -- 2
! 84797: ** REINDEX ?<database>.?<tablename> -- 3
! 84798: ** REINDEX ?<database>.?<indexname> -- 4
! 84799: **
! 84800: ** Form 1 causes all indices in all attached databases to be rebuilt.
! 84801: ** Form 2 rebuilds all indices in all databases that use the named
! 84802: ** collating function. Forms 3 and 4 rebuild the named index or all
! 84803: ** indices associated with the named table.
! 84804: */
! 84805: #ifndef SQLITE_OMIT_REINDEX
! 84806: SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
! 84807: CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
! 84808: char *z; /* Name of a table or index */
! 84809: const char *zDb; /* Name of the database */
! 84810: Table *pTab; /* A table in the database */
! 84811: Index *pIndex; /* An index associated with pTab */
! 84812: int iDb; /* The database index number */
! 84813: sqlite3 *db = pParse->db; /* The database connection */
! 84814: Token *pObjName; /* Name of the table or index to be reindexed */
! 84815:
! 84816: /* Read the database schema. If an error occurs, leave an error message
! 84817: ** and code in pParse and return NULL. */
! 84818: if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
! 84819: return;
! 84820: }
! 84821:
! 84822: if( pName1==0 ){
! 84823: reindexDatabases(pParse, 0);
! 84824: return;
! 84825: }else if( NEVER(pName2==0) || pName2->z==0 ){
! 84826: char *zColl;
! 84827: assert( pName1->z );
! 84828: zColl = sqlite3NameFromToken(pParse->db, pName1);
! 84829: if( !zColl ) return;
! 84830: pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
! 84831: if( pColl ){
! 84832: reindexDatabases(pParse, zColl);
! 84833: sqlite3DbFree(db, zColl);
! 84834: return;
! 84835: }
! 84836: sqlite3DbFree(db, zColl);
! 84837: }
! 84838: iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
! 84839: if( iDb<0 ) return;
! 84840: z = sqlite3NameFromToken(db, pObjName);
! 84841: if( z==0 ) return;
! 84842: zDb = db->aDb[iDb].zName;
! 84843: pTab = sqlite3FindTable(db, z, zDb);
! 84844: if( pTab ){
! 84845: reindexTable(pParse, pTab, 0);
! 84846: sqlite3DbFree(db, z);
! 84847: return;
! 84848: }
! 84849: pIndex = sqlite3FindIndex(db, z, zDb);
! 84850: sqlite3DbFree(db, z);
! 84851: if( pIndex ){
! 84852: sqlite3BeginWriteOperation(pParse, 0, iDb);
! 84853: sqlite3RefillIndex(pParse, pIndex, -1);
! 84854: return;
! 84855: }
! 84856: sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
! 84857: }
! 84858: #endif
! 84859:
! 84860: /*
! 84861: ** Return a dynamicly allocated KeyInfo structure that can be used
! 84862: ** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
! 84863: **
! 84864: ** If successful, a pointer to the new structure is returned. In this case
! 84865: ** the caller is responsible for calling sqlite3DbFree(db, ) on the returned
! 84866: ** pointer. If an error occurs (out of memory or missing collation
! 84867: ** sequence), NULL is returned and the state of pParse updated to reflect
! 84868: ** the error.
! 84869: */
! 84870: SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
! 84871: int i;
! 84872: int nCol = pIdx->nColumn;
! 84873: int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
! 84874: sqlite3 *db = pParse->db;
! 84875: KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(db, nBytes);
! 84876:
! 84877: if( pKey ){
! 84878: pKey->db = pParse->db;
! 84879: pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
! 84880: assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
! 84881: for(i=0; i<nCol; i++){
! 84882: char *zColl = pIdx->azColl[i];
! 84883: assert( zColl );
! 84884: pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
! 84885: pKey->aSortOrder[i] = pIdx->aSortOrder[i];
! 84886: }
! 84887: pKey->nField = (u16)nCol;
! 84888: }
! 84889:
! 84890: if( pParse->nErr ){
! 84891: sqlite3DbFree(db, pKey);
! 84892: pKey = 0;
! 84893: }
! 84894: return pKey;
! 84895: }
! 84896:
! 84897: /************** End of build.c ***********************************************/
! 84898: /************** Begin file callback.c ****************************************/
! 84899: /*
! 84900: ** 2005 May 23
! 84901: **
! 84902: ** The author disclaims copyright to this source code. In place of
! 84903: ** a legal notice, here is a blessing:
! 84904: **
! 84905: ** May you do good and not evil.
! 84906: ** May you find forgiveness for yourself and forgive others.
! 84907: ** May you share freely, never taking more than you give.
! 84908: **
! 84909: *************************************************************************
! 84910: **
! 84911: ** This file contains functions used to access the internal hash tables
! 84912: ** of user defined functions and collation sequences.
! 84913: */
! 84914:
! 84915:
! 84916: /*
! 84917: ** Invoke the 'collation needed' callback to request a collation sequence
! 84918: ** in the encoding enc of name zName, length nName.
! 84919: */
! 84920: static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
! 84921: assert( !db->xCollNeeded || !db->xCollNeeded16 );
! 84922: if( db->xCollNeeded ){
! 84923: char *zExternal = sqlite3DbStrDup(db, zName);
! 84924: if( !zExternal ) return;
! 84925: db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
! 84926: sqlite3DbFree(db, zExternal);
! 84927: }
! 84928: #ifndef SQLITE_OMIT_UTF16
! 84929: if( db->xCollNeeded16 ){
! 84930: char const *zExternal;
! 84931: sqlite3_value *pTmp = sqlite3ValueNew(db);
! 84932: sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
! 84933: zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
! 84934: if( zExternal ){
! 84935: db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
! 84936: }
! 84937: sqlite3ValueFree(pTmp);
! 84938: }
! 84939: #endif
! 84940: }
! 84941:
! 84942: /*
! 84943: ** This routine is called if the collation factory fails to deliver a
! 84944: ** collation function in the best encoding but there may be other versions
! 84945: ** of this collation function (for other text encodings) available. Use one
! 84946: ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
! 84947: ** possible.
! 84948: */
! 84949: static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
! 84950: CollSeq *pColl2;
! 84951: char *z = pColl->zName;
! 84952: int i;
! 84953: static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
! 84954: for(i=0; i<3; i++){
! 84955: pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
! 84956: if( pColl2->xCmp!=0 ){
! 84957: memcpy(pColl, pColl2, sizeof(CollSeq));
! 84958: pColl->xDel = 0; /* Do not copy the destructor */
! 84959: return SQLITE_OK;
! 84960: }
! 84961: }
! 84962: return SQLITE_ERROR;
! 84963: }
! 84964:
! 84965: /*
! 84966: ** This function is responsible for invoking the collation factory callback
! 84967: ** or substituting a collation sequence of a different encoding when the
! 84968: ** requested collation sequence is not available in the desired encoding.
! 84969: **
! 84970: ** If it is not NULL, then pColl must point to the database native encoding
! 84971: ** collation sequence with name zName, length nName.
! 84972: **
! 84973: ** The return value is either the collation sequence to be used in database
! 84974: ** db for collation type name zName, length nName, or NULL, if no collation
! 84975: ** sequence can be found.
! 84976: **
! 84977: ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
! 84978: */
! 84979: SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
! 84980: sqlite3* db, /* The database connection */
! 84981: u8 enc, /* The desired encoding for the collating sequence */
! 84982: CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
! 84983: const char *zName /* Collating sequence name */
! 84984: ){
! 84985: CollSeq *p;
! 84986:
! 84987: p = pColl;
! 84988: if( !p ){
! 84989: p = sqlite3FindCollSeq(db, enc, zName, 0);
! 84990: }
! 84991: if( !p || !p->xCmp ){
! 84992: /* No collation sequence of this type for this encoding is registered.
! 84993: ** Call the collation factory to see if it can supply us with one.
! 84994: */
! 84995: callCollNeeded(db, enc, zName);
! 84996: p = sqlite3FindCollSeq(db, enc, zName, 0);
! 84997: }
! 84998: if( p && !p->xCmp && synthCollSeq(db, p) ){
! 84999: p = 0;
! 85000: }
! 85001: assert( !p || p->xCmp );
! 85002: return p;
! 85003: }
! 85004:
! 85005: /*
! 85006: ** This routine is called on a collation sequence before it is used to
! 85007: ** check that it is defined. An undefined collation sequence exists when
! 85008: ** a database is loaded that contains references to collation sequences
! 85009: ** that have not been defined by sqlite3_create_collation() etc.
! 85010: **
! 85011: ** If required, this routine calls the 'collation needed' callback to
! 85012: ** request a definition of the collating sequence. If this doesn't work,
! 85013: ** an equivalent collating sequence that uses a text encoding different
! 85014: ** from the main database is substituted, if one is available.
! 85015: */
! 85016: SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
! 85017: if( pColl ){
! 85018: const char *zName = pColl->zName;
! 85019: sqlite3 *db = pParse->db;
! 85020: CollSeq *p = sqlite3GetCollSeq(db, ENC(db), pColl, zName);
! 85021: if( !p ){
! 85022: sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
! 85023: pParse->nErr++;
! 85024: return SQLITE_ERROR;
! 85025: }
! 85026: assert( p==pColl );
! 85027: }
! 85028: return SQLITE_OK;
! 85029: }
! 85030:
! 85031:
! 85032:
! 85033: /*
! 85034: ** Locate and return an entry from the db.aCollSeq hash table. If the entry
! 85035: ** specified by zName and nName is not found and parameter 'create' is
! 85036: ** true, then create a new entry. Otherwise return NULL.
! 85037: **
! 85038: ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
! 85039: ** array of three CollSeq structures. The first is the collation sequence
! 85040: ** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
! 85041: **
! 85042: ** Stored immediately after the three collation sequences is a copy of
! 85043: ** the collation sequence name. A pointer to this string is stored in
! 85044: ** each collation sequence structure.
! 85045: */
! 85046: static CollSeq *findCollSeqEntry(
! 85047: sqlite3 *db, /* Database connection */
! 85048: const char *zName, /* Name of the collating sequence */
! 85049: int create /* Create a new entry if true */
! 85050: ){
! 85051: CollSeq *pColl;
! 85052: int nName = sqlite3Strlen30(zName);
! 85053: pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
! 85054:
! 85055: if( 0==pColl && create ){
! 85056: pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
! 85057: if( pColl ){
! 85058: CollSeq *pDel = 0;
! 85059: pColl[0].zName = (char*)&pColl[3];
! 85060: pColl[0].enc = SQLITE_UTF8;
! 85061: pColl[1].zName = (char*)&pColl[3];
! 85062: pColl[1].enc = SQLITE_UTF16LE;
! 85063: pColl[2].zName = (char*)&pColl[3];
! 85064: pColl[2].enc = SQLITE_UTF16BE;
! 85065: memcpy(pColl[0].zName, zName, nName);
! 85066: pColl[0].zName[nName] = 0;
! 85067: pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
! 85068:
! 85069: /* If a malloc() failure occurred in sqlite3HashInsert(), it will
! 85070: ** return the pColl pointer to be deleted (because it wasn't added
! 85071: ** to the hash table).
! 85072: */
! 85073: assert( pDel==0 || pDel==pColl );
! 85074: if( pDel!=0 ){
! 85075: db->mallocFailed = 1;
! 85076: sqlite3DbFree(db, pDel);
! 85077: pColl = 0;
! 85078: }
! 85079: }
! 85080: }
! 85081: return pColl;
! 85082: }
! 85083:
! 85084: /*
! 85085: ** Parameter zName points to a UTF-8 encoded string nName bytes long.
! 85086: ** Return the CollSeq* pointer for the collation sequence named zName
! 85087: ** for the encoding 'enc' from the database 'db'.
! 85088: **
! 85089: ** If the entry specified is not found and 'create' is true, then create a
! 85090: ** new entry. Otherwise return NULL.
! 85091: **
! 85092: ** A separate function sqlite3LocateCollSeq() is a wrapper around
! 85093: ** this routine. sqlite3LocateCollSeq() invokes the collation factory
! 85094: ** if necessary and generates an error message if the collating sequence
! 85095: ** cannot be found.
! 85096: **
! 85097: ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
! 85098: */
! 85099: SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
! 85100: sqlite3 *db,
! 85101: u8 enc,
! 85102: const char *zName,
! 85103: int create
! 85104: ){
! 85105: CollSeq *pColl;
! 85106: if( zName ){
! 85107: pColl = findCollSeqEntry(db, zName, create);
! 85108: }else{
! 85109: pColl = db->pDfltColl;
! 85110: }
! 85111: assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
! 85112: assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
! 85113: if( pColl ) pColl += enc-1;
! 85114: return pColl;
! 85115: }
! 85116:
! 85117: /* During the search for the best function definition, this procedure
! 85118: ** is called to test how well the function passed as the first argument
! 85119: ** matches the request for a function with nArg arguments in a system
! 85120: ** that uses encoding enc. The value returned indicates how well the
! 85121: ** request is matched. A higher value indicates a better match.
! 85122: **
! 85123: ** The returned value is always between 0 and 6, as follows:
! 85124: **
! 85125: ** 0: Not a match, or if nArg<0 and the function is has no implementation.
! 85126: ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
! 85127: ** encoding is requested, or vice versa.
! 85128: ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
! 85129: ** requested, or vice versa.
! 85130: ** 3: A variable arguments function using the same text encoding.
! 85131: ** 4: A function with the exact number of arguments requested that
! 85132: ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
! 85133: ** 5: A function with the exact number of arguments requested that
! 85134: ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
! 85135: ** 6: An exact match.
! 85136: **
! 85137: */
! 85138: static int matchQuality(FuncDef *p, int nArg, u8 enc){
! 85139: int match = 0;
! 85140: if( p->nArg==-1 || p->nArg==nArg
! 85141: || (nArg==-1 && (p->xFunc!=0 || p->xStep!=0))
! 85142: ){
! 85143: match = 1;
! 85144: if( p->nArg==nArg || nArg==-1 ){
! 85145: match = 4;
! 85146: }
! 85147: if( enc==p->iPrefEnc ){
! 85148: match += 2;
! 85149: }
! 85150: else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
! 85151: (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
! 85152: match += 1;
! 85153: }
! 85154: }
! 85155: return match;
! 85156: }
! 85157:
! 85158: /*
! 85159: ** Search a FuncDefHash for a function with the given name. Return
! 85160: ** a pointer to the matching FuncDef if found, or 0 if there is no match.
! 85161: */
! 85162: static FuncDef *functionSearch(
! 85163: FuncDefHash *pHash, /* Hash table to search */
! 85164: int h, /* Hash of the name */
! 85165: const char *zFunc, /* Name of function */
! 85166: int nFunc /* Number of bytes in zFunc */
! 85167: ){
! 85168: FuncDef *p;
! 85169: for(p=pHash->a[h]; p; p=p->pHash){
! 85170: if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
! 85171: return p;
! 85172: }
! 85173: }
! 85174: return 0;
! 85175: }
! 85176:
! 85177: /*
! 85178: ** Insert a new FuncDef into a FuncDefHash hash table.
! 85179: */
! 85180: SQLITE_PRIVATE void sqlite3FuncDefInsert(
! 85181: FuncDefHash *pHash, /* The hash table into which to insert */
! 85182: FuncDef *pDef /* The function definition to insert */
! 85183: ){
! 85184: FuncDef *pOther;
! 85185: int nName = sqlite3Strlen30(pDef->zName);
! 85186: u8 c1 = (u8)pDef->zName[0];
! 85187: int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
! 85188: pOther = functionSearch(pHash, h, pDef->zName, nName);
! 85189: if( pOther ){
! 85190: assert( pOther!=pDef && pOther->pNext!=pDef );
! 85191: pDef->pNext = pOther->pNext;
! 85192: pOther->pNext = pDef;
! 85193: }else{
! 85194: pDef->pNext = 0;
! 85195: pDef->pHash = pHash->a[h];
! 85196: pHash->a[h] = pDef;
! 85197: }
! 85198: }
! 85199:
! 85200:
! 85201:
! 85202: /*
! 85203: ** Locate a user function given a name, a number of arguments and a flag
! 85204: ** indicating whether the function prefers UTF-16 over UTF-8. Return a
! 85205: ** pointer to the FuncDef structure that defines that function, or return
! 85206: ** NULL if the function does not exist.
! 85207: **
! 85208: ** If the createFlag argument is true, then a new (blank) FuncDef
! 85209: ** structure is created and liked into the "db" structure if a
! 85210: ** no matching function previously existed. When createFlag is true
! 85211: ** and the nArg parameter is -1, then only a function that accepts
! 85212: ** any number of arguments will be returned.
! 85213: **
! 85214: ** If createFlag is false and nArg is -1, then the first valid
! 85215: ** function found is returned. A function is valid if either xFunc
! 85216: ** or xStep is non-zero.
! 85217: **
! 85218: ** If createFlag is false, then a function with the required name and
! 85219: ** number of arguments may be returned even if the eTextRep flag does not
! 85220: ** match that requested.
! 85221: */
! 85222: SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
! 85223: sqlite3 *db, /* An open database */
! 85224: const char *zName, /* Name of the function. Not null-terminated */
! 85225: int nName, /* Number of characters in the name */
! 85226: int nArg, /* Number of arguments. -1 means any number */
! 85227: u8 enc, /* Preferred text encoding */
! 85228: int createFlag /* Create new entry if true and does not otherwise exist */
! 85229: ){
! 85230: FuncDef *p; /* Iterator variable */
! 85231: FuncDef *pBest = 0; /* Best match found so far */
! 85232: int bestScore = 0; /* Score of best match */
! 85233: int h; /* Hash value */
! 85234:
! 85235:
! 85236: assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
! 85237: h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
! 85238:
! 85239: /* First search for a match amongst the application-defined functions.
! 85240: */
! 85241: p = functionSearch(&db->aFunc, h, zName, nName);
! 85242: while( p ){
! 85243: int score = matchQuality(p, nArg, enc);
! 85244: if( score>bestScore ){
! 85245: pBest = p;
! 85246: bestScore = score;
! 85247: }
! 85248: p = p->pNext;
! 85249: }
! 85250:
! 85251: /* If no match is found, search the built-in functions.
! 85252: **
! 85253: ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
! 85254: ** functions even if a prior app-defined function was found. And give
! 85255: ** priority to built-in functions.
! 85256: **
! 85257: ** Except, if createFlag is true, that means that we are trying to
! 85258: ** install a new function. Whatever FuncDef structure is returned it will
! 85259: ** have fields overwritten with new information appropriate for the
! 85260: ** new function. But the FuncDefs for built-in functions are read-only.
! 85261: ** So we must not search for built-ins when creating a new function.
! 85262: */
! 85263: if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
! 85264: FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
! 85265: bestScore = 0;
! 85266: p = functionSearch(pHash, h, zName, nName);
! 85267: while( p ){
! 85268: int score = matchQuality(p, nArg, enc);
! 85269: if( score>bestScore ){
! 85270: pBest = p;
! 85271: bestScore = score;
! 85272: }
! 85273: p = p->pNext;
! 85274: }
! 85275: }
! 85276:
! 85277: /* If the createFlag parameter is true and the search did not reveal an
! 85278: ** exact match for the name, number of arguments and encoding, then add a
! 85279: ** new entry to the hash table and return it.
! 85280: */
! 85281: if( createFlag && (bestScore<6 || pBest->nArg!=nArg) &&
! 85282: (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
! 85283: pBest->zName = (char *)&pBest[1];
! 85284: pBest->nArg = (u16)nArg;
! 85285: pBest->iPrefEnc = enc;
! 85286: memcpy(pBest->zName, zName, nName);
! 85287: pBest->zName[nName] = 0;
! 85288: sqlite3FuncDefInsert(&db->aFunc, pBest);
! 85289: }
! 85290:
! 85291: if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
! 85292: return pBest;
! 85293: }
! 85294: return 0;
! 85295: }
! 85296:
! 85297: /*
! 85298: ** Free all resources held by the schema structure. The void* argument points
! 85299: ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
! 85300: ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
! 85301: ** of the schema hash tables).
! 85302: **
! 85303: ** The Schema.cache_size variable is not cleared.
! 85304: */
! 85305: SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
! 85306: Hash temp1;
! 85307: Hash temp2;
! 85308: HashElem *pElem;
! 85309: Schema *pSchema = (Schema *)p;
! 85310:
! 85311: temp1 = pSchema->tblHash;
! 85312: temp2 = pSchema->trigHash;
! 85313: sqlite3HashInit(&pSchema->trigHash);
! 85314: sqlite3HashClear(&pSchema->idxHash);
! 85315: for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
! 85316: sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
! 85317: }
! 85318: sqlite3HashClear(&temp2);
! 85319: sqlite3HashInit(&pSchema->tblHash);
! 85320: for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
! 85321: Table *pTab = sqliteHashData(pElem);
! 85322: sqlite3DeleteTable(0, pTab);
! 85323: }
! 85324: sqlite3HashClear(&temp1);
! 85325: sqlite3HashClear(&pSchema->fkeyHash);
! 85326: pSchema->pSeqTab = 0;
! 85327: if( pSchema->flags & DB_SchemaLoaded ){
! 85328: pSchema->iGeneration++;
! 85329: pSchema->flags &= ~DB_SchemaLoaded;
! 85330: }
! 85331: }
! 85332:
! 85333: /*
! 85334: ** Find and return the schema associated with a BTree. Create
! 85335: ** a new one if necessary.
! 85336: */
! 85337: SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
! 85338: Schema * p;
! 85339: if( pBt ){
! 85340: p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
! 85341: }else{
! 85342: p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
! 85343: }
! 85344: if( !p ){
! 85345: db->mallocFailed = 1;
! 85346: }else if ( 0==p->file_format ){
! 85347: sqlite3HashInit(&p->tblHash);
! 85348: sqlite3HashInit(&p->idxHash);
! 85349: sqlite3HashInit(&p->trigHash);
! 85350: sqlite3HashInit(&p->fkeyHash);
! 85351: p->enc = SQLITE_UTF8;
! 85352: }
! 85353: return p;
! 85354: }
! 85355:
! 85356: /************** End of callback.c ********************************************/
! 85357: /************** Begin file delete.c ******************************************/
! 85358: /*
! 85359: ** 2001 September 15
! 85360: **
! 85361: ** The author disclaims copyright to this source code. In place of
! 85362: ** a legal notice, here is a blessing:
! 85363: **
! 85364: ** May you do good and not evil.
! 85365: ** May you find forgiveness for yourself and forgive others.
! 85366: ** May you share freely, never taking more than you give.
! 85367: **
! 85368: *************************************************************************
! 85369: ** This file contains C code routines that are called by the parser
! 85370: ** in order to generate code for DELETE FROM statements.
! 85371: */
! 85372:
! 85373: /*
! 85374: ** While a SrcList can in general represent multiple tables and subqueries
! 85375: ** (as in the FROM clause of a SELECT statement) in this case it contains
! 85376: ** the name of a single table, as one might find in an INSERT, DELETE,
! 85377: ** or UPDATE statement. Look up that table in the symbol table and
! 85378: ** return a pointer. Set an error message and return NULL if the table
! 85379: ** name is not found or if any other error occurs.
! 85380: **
! 85381: ** The following fields are initialized appropriate in pSrc:
! 85382: **
! 85383: ** pSrc->a[0].pTab Pointer to the Table object
! 85384: ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
! 85385: **
! 85386: */
! 85387: SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
! 85388: struct SrcList_item *pItem = pSrc->a;
! 85389: Table *pTab;
! 85390: assert( pItem && pSrc->nSrc==1 );
! 85391: pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
! 85392: sqlite3DeleteTable(pParse->db, pItem->pTab);
! 85393: pItem->pTab = pTab;
! 85394: if( pTab ){
! 85395: pTab->nRef++;
! 85396: }
! 85397: if( sqlite3IndexedByLookup(pParse, pItem) ){
! 85398: pTab = 0;
! 85399: }
! 85400: return pTab;
! 85401: }
! 85402:
! 85403: /*
! 85404: ** Check to make sure the given table is writable. If it is not
! 85405: ** writable, generate an error message and return 1. If it is
! 85406: ** writable return 0;
! 85407: */
! 85408: SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
! 85409: /* A table is not writable under the following circumstances:
! 85410: **
! 85411: ** 1) It is a virtual table and no implementation of the xUpdate method
! 85412: ** has been provided, or
! 85413: ** 2) It is a system table (i.e. sqlite_master), this call is not
! 85414: ** part of a nested parse and writable_schema pragma has not
! 85415: ** been specified.
! 85416: **
! 85417: ** In either case leave an error message in pParse and return non-zero.
! 85418: */
! 85419: if( ( IsVirtual(pTab)
! 85420: && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
! 85421: || ( (pTab->tabFlags & TF_Readonly)!=0
! 85422: && (pParse->db->flags & SQLITE_WriteSchema)==0
! 85423: && pParse->nested==0 )
! 85424: ){
! 85425: sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
! 85426: return 1;
! 85427: }
! 85428:
! 85429: #ifndef SQLITE_OMIT_VIEW
! 85430: if( !viewOk && pTab->pSelect ){
! 85431: sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
! 85432: return 1;
! 85433: }
! 85434: #endif
! 85435: return 0;
! 85436: }
! 85437:
! 85438:
! 85439: #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
! 85440: /*
! 85441: ** Evaluate a view and store its result in an ephemeral table. The
! 85442: ** pWhere argument is an optional WHERE clause that restricts the
! 85443: ** set of rows in the view that are to be added to the ephemeral table.
! 85444: */
! 85445: SQLITE_PRIVATE void sqlite3MaterializeView(
! 85446: Parse *pParse, /* Parsing context */
! 85447: Table *pView, /* View definition */
! 85448: Expr *pWhere, /* Optional WHERE clause to be added */
! 85449: int iCur /* Cursor number for ephemerial table */
! 85450: ){
! 85451: SelectDest dest;
! 85452: Select *pDup;
! 85453: sqlite3 *db = pParse->db;
! 85454:
! 85455: pDup = sqlite3SelectDup(db, pView->pSelect, 0);
! 85456: if( pWhere ){
! 85457: SrcList *pFrom;
! 85458:
! 85459: pWhere = sqlite3ExprDup(db, pWhere, 0);
! 85460: pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
! 85461: if( pFrom ){
! 85462: assert( pFrom->nSrc==1 );
! 85463: pFrom->a[0].zAlias = sqlite3DbStrDup(db, pView->zName);
! 85464: pFrom->a[0].pSelect = pDup;
! 85465: assert( pFrom->a[0].pOn==0 );
! 85466: assert( pFrom->a[0].pUsing==0 );
! 85467: }else{
! 85468: sqlite3SelectDelete(db, pDup);
! 85469: }
! 85470: pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
! 85471: }
! 85472: sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
! 85473: sqlite3Select(pParse, pDup, &dest);
! 85474: sqlite3SelectDelete(db, pDup);
! 85475: }
! 85476: #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
! 85477:
! 85478: #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
! 85479: /*
! 85480: ** Generate an expression tree to implement the WHERE, ORDER BY,
! 85481: ** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
! 85482: **
! 85483: ** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
! 85484: ** \__________________________/
! 85485: ** pLimitWhere (pInClause)
! 85486: */
! 85487: SQLITE_PRIVATE Expr *sqlite3LimitWhere(
! 85488: Parse *pParse, /* The parser context */
! 85489: SrcList *pSrc, /* the FROM clause -- which tables to scan */
! 85490: Expr *pWhere, /* The WHERE clause. May be null */
! 85491: ExprList *pOrderBy, /* The ORDER BY clause. May be null */
! 85492: Expr *pLimit, /* The LIMIT clause. May be null */
! 85493: Expr *pOffset, /* The OFFSET clause. May be null */
! 85494: char *zStmtType /* Either DELETE or UPDATE. For error messages. */
! 85495: ){
! 85496: Expr *pWhereRowid = NULL; /* WHERE rowid .. */
! 85497: Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */
! 85498: Expr *pSelectRowid = NULL; /* SELECT rowid ... */
! 85499: ExprList *pEList = NULL; /* Expression list contaning only pSelectRowid */
! 85500: SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */
! 85501: Select *pSelect = NULL; /* Complete SELECT tree */
! 85502:
! 85503: /* Check that there isn't an ORDER BY without a LIMIT clause.
! 85504: */
! 85505: if( pOrderBy && (pLimit == 0) ) {
! 85506: sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
! 85507: goto limit_where_cleanup_2;
! 85508: }
! 85509:
! 85510: /* We only need to generate a select expression if there
! 85511: ** is a limit/offset term to enforce.
! 85512: */
! 85513: if( pLimit == 0 ) {
! 85514: /* if pLimit is null, pOffset will always be null as well. */
! 85515: assert( pOffset == 0 );
! 85516: return pWhere;
! 85517: }
! 85518:
! 85519: /* Generate a select expression tree to enforce the limit/offset
! 85520: ** term for the DELETE or UPDATE statement. For example:
! 85521: ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
! 85522: ** becomes:
! 85523: ** DELETE FROM table_a WHERE rowid IN (
! 85524: ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
! 85525: ** );
! 85526: */
! 85527:
! 85528: pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
! 85529: if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
! 85530: pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
! 85531: if( pEList == 0 ) goto limit_where_cleanup_2;
! 85532:
! 85533: /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
! 85534: ** and the SELECT subtree. */
! 85535: pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
! 85536: if( pSelectSrc == 0 ) {
! 85537: sqlite3ExprListDelete(pParse->db, pEList);
! 85538: goto limit_where_cleanup_2;
! 85539: }
! 85540:
! 85541: /* generate the SELECT expression tree. */
! 85542: pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
! 85543: pOrderBy,0,pLimit,pOffset);
! 85544: if( pSelect == 0 ) return 0;
! 85545:
! 85546: /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
! 85547: pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
! 85548: if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
! 85549: pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
! 85550: if( pInClause == 0 ) goto limit_where_cleanup_1;
! 85551:
! 85552: pInClause->x.pSelect = pSelect;
! 85553: pInClause->flags |= EP_xIsSelect;
! 85554: sqlite3ExprSetHeight(pParse, pInClause);
! 85555: return pInClause;
! 85556:
! 85557: /* something went wrong. clean up anything allocated. */
! 85558: limit_where_cleanup_1:
! 85559: sqlite3SelectDelete(pParse->db, pSelect);
! 85560: return 0;
! 85561:
! 85562: limit_where_cleanup_2:
! 85563: sqlite3ExprDelete(pParse->db, pWhere);
! 85564: sqlite3ExprListDelete(pParse->db, pOrderBy);
! 85565: sqlite3ExprDelete(pParse->db, pLimit);
! 85566: sqlite3ExprDelete(pParse->db, pOffset);
! 85567: return 0;
! 85568: }
! 85569: #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
! 85570:
! 85571: /*
! 85572: ** Generate code for a DELETE FROM statement.
! 85573: **
! 85574: ** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
! 85575: ** \________/ \________________/
! 85576: ** pTabList pWhere
! 85577: */
! 85578: SQLITE_PRIVATE void sqlite3DeleteFrom(
! 85579: Parse *pParse, /* The parser context */
! 85580: SrcList *pTabList, /* The table from which we should delete things */
! 85581: Expr *pWhere /* The WHERE clause. May be null */
! 85582: ){
! 85583: Vdbe *v; /* The virtual database engine */
! 85584: Table *pTab; /* The table from which records will be deleted */
! 85585: const char *zDb; /* Name of database holding pTab */
! 85586: int end, addr = 0; /* A couple addresses of generated code */
! 85587: int i; /* Loop counter */
! 85588: WhereInfo *pWInfo; /* Information about the WHERE clause */
! 85589: Index *pIdx; /* For looping over indices of the table */
! 85590: int iCur; /* VDBE Cursor number for pTab */
! 85591: sqlite3 *db; /* Main database structure */
! 85592: AuthContext sContext; /* Authorization context */
! 85593: NameContext sNC; /* Name context to resolve expressions in */
! 85594: int iDb; /* Database number */
! 85595: int memCnt = -1; /* Memory cell used for change counting */
! 85596: int rcauth; /* Value returned by authorization callback */
! 85597:
! 85598: #ifndef SQLITE_OMIT_TRIGGER
! 85599: int isView; /* True if attempting to delete from a view */
! 85600: Trigger *pTrigger; /* List of table triggers, if required */
! 85601: #endif
! 85602:
! 85603: memset(&sContext, 0, sizeof(sContext));
! 85604: db = pParse->db;
! 85605: if( pParse->nErr || db->mallocFailed ){
! 85606: goto delete_from_cleanup;
! 85607: }
! 85608: assert( pTabList->nSrc==1 );
! 85609:
! 85610: /* Locate the table which we want to delete. This table has to be
! 85611: ** put in an SrcList structure because some of the subroutines we
! 85612: ** will be calling are designed to work with multiple tables and expect
! 85613: ** an SrcList* parameter instead of just a Table* parameter.
! 85614: */
! 85615: pTab = sqlite3SrcListLookup(pParse, pTabList);
! 85616: if( pTab==0 ) goto delete_from_cleanup;
! 85617:
! 85618: /* Figure out if we have any triggers and if the table being
! 85619: ** deleted from is a view
! 85620: */
! 85621: #ifndef SQLITE_OMIT_TRIGGER
! 85622: pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
! 85623: isView = pTab->pSelect!=0;
! 85624: #else
! 85625: # define pTrigger 0
! 85626: # define isView 0
! 85627: #endif
! 85628: #ifdef SQLITE_OMIT_VIEW
! 85629: # undef isView
! 85630: # define isView 0
! 85631: #endif
! 85632:
! 85633: /* If pTab is really a view, make sure it has been initialized.
! 85634: */
! 85635: if( sqlite3ViewGetColumnNames(pParse, pTab) ){
! 85636: goto delete_from_cleanup;
! 85637: }
! 85638:
! 85639: if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
! 85640: goto delete_from_cleanup;
! 85641: }
! 85642: iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 85643: assert( iDb<db->nDb );
! 85644: zDb = db->aDb[iDb].zName;
! 85645: rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
! 85646: assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
! 85647: if( rcauth==SQLITE_DENY ){
! 85648: goto delete_from_cleanup;
! 85649: }
! 85650: assert(!isView || pTrigger);
! 85651:
! 85652: /* Assign cursor number to the table and all its indices.
! 85653: */
! 85654: assert( pTabList->nSrc==1 );
! 85655: iCur = pTabList->a[0].iCursor = pParse->nTab++;
! 85656: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
! 85657: pParse->nTab++;
! 85658: }
! 85659:
! 85660: /* Start the view context
! 85661: */
! 85662: if( isView ){
! 85663: sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
! 85664: }
! 85665:
! 85666: /* Begin generating code.
! 85667: */
! 85668: v = sqlite3GetVdbe(pParse);
! 85669: if( v==0 ){
! 85670: goto delete_from_cleanup;
! 85671: }
! 85672: if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
! 85673: sqlite3BeginWriteOperation(pParse, 1, iDb);
! 85674:
! 85675: /* If we are trying to delete from a view, realize that view into
! 85676: ** a ephemeral table.
! 85677: */
! 85678: #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
! 85679: if( isView ){
! 85680: sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
! 85681: }
! 85682: #endif
! 85683:
! 85684: /* Resolve the column names in the WHERE clause.
! 85685: */
! 85686: memset(&sNC, 0, sizeof(sNC));
! 85687: sNC.pParse = pParse;
! 85688: sNC.pSrcList = pTabList;
! 85689: if( sqlite3ResolveExprNames(&sNC, pWhere) ){
! 85690: goto delete_from_cleanup;
! 85691: }
! 85692:
! 85693: /* Initialize the counter of the number of rows deleted, if
! 85694: ** we are counting rows.
! 85695: */
! 85696: if( db->flags & SQLITE_CountRows ){
! 85697: memCnt = ++pParse->nMem;
! 85698: sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
! 85699: }
! 85700:
! 85701: #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
! 85702: /* Special case: A DELETE without a WHERE clause deletes everything.
! 85703: ** It is easier just to erase the whole table. Prior to version 3.6.5,
! 85704: ** this optimization caused the row change count (the value returned by
! 85705: ** API function sqlite3_count_changes) to be set incorrectly. */
! 85706: if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab)
! 85707: && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
! 85708: ){
! 85709: assert( !isView );
! 85710: sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
! 85711: pTab->zName, P4_STATIC);
! 85712: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
! 85713: assert( pIdx->pSchema==pTab->pSchema );
! 85714: sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
! 85715: }
! 85716: }else
! 85717: #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
! 85718: /* The usual case: There is a WHERE clause so we have to scan through
! 85719: ** the table and pick which records to delete.
! 85720: */
! 85721: {
! 85722: int iRowSet = ++pParse->nMem; /* Register for rowset of rows to delete */
! 85723: int iRowid = ++pParse->nMem; /* Used for storing rowid values. */
! 85724: int regRowid; /* Actual register containing rowids */
! 85725:
! 85726: /* Collect rowids of every row to be deleted.
! 85727: */
! 85728: sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
! 85729: pWInfo = sqlite3WhereBegin(
! 85730: pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK
! 85731: );
! 85732: if( pWInfo==0 ) goto delete_from_cleanup;
! 85733: regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid);
! 85734: sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
! 85735: if( db->flags & SQLITE_CountRows ){
! 85736: sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
! 85737: }
! 85738: sqlite3WhereEnd(pWInfo);
! 85739:
! 85740: /* Delete every item whose key was written to the list during the
! 85741: ** database scan. We have to delete items after the scan is complete
! 85742: ** because deleting an item can change the scan order. */
! 85743: end = sqlite3VdbeMakeLabel(v);
! 85744:
! 85745: /* Unless this is a view, open cursors for the table we are
! 85746: ** deleting from and all its indices. If this is a view, then the
! 85747: ** only effect this statement has is to fire the INSTEAD OF
! 85748: ** triggers. */
! 85749: if( !isView ){
! 85750: sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
! 85751: }
! 85752:
! 85753: addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
! 85754:
! 85755: /* Delete the row */
! 85756: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 85757: if( IsVirtual(pTab) ){
! 85758: const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
! 85759: sqlite3VtabMakeWritable(pParse, pTab);
! 85760: sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
! 85761: sqlite3VdbeChangeP5(v, OE_Abort);
! 85762: sqlite3MayAbort(pParse);
! 85763: }else
! 85764: #endif
! 85765: {
! 85766: int count = (pParse->nested==0); /* True to count changes */
! 85767: sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, count, pTrigger, OE_Default);
! 85768: }
! 85769:
! 85770: /* End of the delete loop */
! 85771: sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
! 85772: sqlite3VdbeResolveLabel(v, end);
! 85773:
! 85774: /* Close the cursors open on the table and its indexes. */
! 85775: if( !isView && !IsVirtual(pTab) ){
! 85776: for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
! 85777: sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
! 85778: }
! 85779: sqlite3VdbeAddOp1(v, OP_Close, iCur);
! 85780: }
! 85781: }
! 85782:
! 85783: /* Update the sqlite_sequence table by storing the content of the
! 85784: ** maximum rowid counter values recorded while inserting into
! 85785: ** autoincrement tables.
! 85786: */
! 85787: if( pParse->nested==0 && pParse->pTriggerTab==0 ){
! 85788: sqlite3AutoincrementEnd(pParse);
! 85789: }
! 85790:
! 85791: /* Return the number of rows that were deleted. If this routine is
! 85792: ** generating code because of a call to sqlite3NestedParse(), do not
! 85793: ** invoke the callback function.
! 85794: */
! 85795: if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
! 85796: sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
! 85797: sqlite3VdbeSetNumCols(v, 1);
! 85798: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
! 85799: }
! 85800:
! 85801: delete_from_cleanup:
! 85802: sqlite3AuthContextPop(&sContext);
! 85803: sqlite3SrcListDelete(db, pTabList);
! 85804: sqlite3ExprDelete(db, pWhere);
! 85805: return;
! 85806: }
! 85807: /* Make sure "isView" and other macros defined above are undefined. Otherwise
! 85808: ** thely may interfere with compilation of other functions in this file
! 85809: ** (or in another file, if this file becomes part of the amalgamation). */
! 85810: #ifdef isView
! 85811: #undef isView
! 85812: #endif
! 85813: #ifdef pTrigger
! 85814: #undef pTrigger
! 85815: #endif
! 85816:
! 85817: /*
! 85818: ** This routine generates VDBE code that causes a single row of a
! 85819: ** single table to be deleted.
! 85820: **
! 85821: ** The VDBE must be in a particular state when this routine is called.
! 85822: ** These are the requirements:
! 85823: **
! 85824: ** 1. A read/write cursor pointing to pTab, the table containing the row
! 85825: ** to be deleted, must be opened as cursor number $iCur.
! 85826: **
! 85827: ** 2. Read/write cursors for all indices of pTab must be open as
! 85828: ** cursor number base+i for the i-th index.
! 85829: **
! 85830: ** 3. The record number of the row to be deleted must be stored in
! 85831: ** memory cell iRowid.
! 85832: **
! 85833: ** This routine generates code to remove both the table record and all
! 85834: ** index entries that point to that record.
! 85835: */
! 85836: SQLITE_PRIVATE void sqlite3GenerateRowDelete(
! 85837: Parse *pParse, /* Parsing context */
! 85838: Table *pTab, /* Table containing the row to be deleted */
! 85839: int iCur, /* Cursor number for the table */
! 85840: int iRowid, /* Memory cell that contains the rowid to delete */
! 85841: int count, /* If non-zero, increment the row change counter */
! 85842: Trigger *pTrigger, /* List of triggers to (potentially) fire */
! 85843: int onconf /* Default ON CONFLICT policy for triggers */
! 85844: ){
! 85845: Vdbe *v = pParse->pVdbe; /* Vdbe */
! 85846: int iOld = 0; /* First register in OLD.* array */
! 85847: int iLabel; /* Label resolved to end of generated code */
! 85848:
! 85849: /* Vdbe is guaranteed to have been allocated by this stage. */
! 85850: assert( v );
! 85851:
! 85852: /* Seek cursor iCur to the row to delete. If this row no longer exists
! 85853: ** (this can happen if a trigger program has already deleted it), do
! 85854: ** not attempt to delete it or fire any DELETE triggers. */
! 85855: iLabel = sqlite3VdbeMakeLabel(v);
! 85856: sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
! 85857:
! 85858: /* If there are any triggers to fire, allocate a range of registers to
! 85859: ** use for the old.* references in the triggers. */
! 85860: if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
! 85861: u32 mask; /* Mask of OLD.* columns in use */
! 85862: int iCol; /* Iterator used while populating OLD.* */
! 85863:
! 85864: /* TODO: Could use temporary registers here. Also could attempt to
! 85865: ** avoid copying the contents of the rowid register. */
! 85866: mask = sqlite3TriggerColmask(
! 85867: pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
! 85868: );
! 85869: mask |= sqlite3FkOldmask(pParse, pTab);
! 85870: iOld = pParse->nMem+1;
! 85871: pParse->nMem += (1 + pTab->nCol);
! 85872:
! 85873: /* Populate the OLD.* pseudo-table register array. These values will be
! 85874: ** used by any BEFORE and AFTER triggers that exist. */
! 85875: sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
! 85876: for(iCol=0; iCol<pTab->nCol; iCol++){
! 85877: if( mask==0xffffffff || mask&(1<<iCol) ){
! 85878: sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, iOld+iCol+1);
! 85879: }
! 85880: }
! 85881:
! 85882: /* Invoke BEFORE DELETE trigger programs. */
! 85883: sqlite3CodeRowTrigger(pParse, pTrigger,
! 85884: TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
! 85885: );
! 85886:
! 85887: /* Seek the cursor to the row to be deleted again. It may be that
! 85888: ** the BEFORE triggers coded above have already removed the row
! 85889: ** being deleted. Do not attempt to delete the row a second time, and
! 85890: ** do not fire AFTER triggers. */
! 85891: sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
! 85892:
! 85893: /* Do FK processing. This call checks that any FK constraints that
! 85894: ** refer to this table (i.e. constraints attached to other tables)
! 85895: ** are not violated by deleting this row. */
! 85896: sqlite3FkCheck(pParse, pTab, iOld, 0);
! 85897: }
! 85898:
! 85899: /* Delete the index and table entries. Skip this step if pTab is really
! 85900: ** a view (in which case the only effect of the DELETE statement is to
! 85901: ** fire the INSTEAD OF triggers). */
! 85902: if( pTab->pSelect==0 ){
! 85903: sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0);
! 85904: sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
! 85905: if( count ){
! 85906: sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
! 85907: }
! 85908: }
! 85909:
! 85910: /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
! 85911: ** handle rows (possibly in other tables) that refer via a foreign key
! 85912: ** to the row just deleted. */
! 85913: sqlite3FkActions(pParse, pTab, 0, iOld);
! 85914:
! 85915: /* Invoke AFTER DELETE trigger programs. */
! 85916: sqlite3CodeRowTrigger(pParse, pTrigger,
! 85917: TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
! 85918: );
! 85919:
! 85920: /* Jump here if the row had already been deleted before any BEFORE
! 85921: ** trigger programs were invoked. Or if a trigger program throws a
! 85922: ** RAISE(IGNORE) exception. */
! 85923: sqlite3VdbeResolveLabel(v, iLabel);
! 85924: }
! 85925:
! 85926: /*
! 85927: ** This routine generates VDBE code that causes the deletion of all
! 85928: ** index entries associated with a single row of a single table.
! 85929: **
! 85930: ** The VDBE must be in a particular state when this routine is called.
! 85931: ** These are the requirements:
! 85932: **
! 85933: ** 1. A read/write cursor pointing to pTab, the table containing the row
! 85934: ** to be deleted, must be opened as cursor number "iCur".
! 85935: **
! 85936: ** 2. Read/write cursors for all indices of pTab must be open as
! 85937: ** cursor number iCur+i for the i-th index.
! 85938: **
! 85939: ** 3. The "iCur" cursor must be pointing to the row that is to be
! 85940: ** deleted.
! 85941: */
! 85942: SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
! 85943: Parse *pParse, /* Parsing and code generating context */
! 85944: Table *pTab, /* Table containing the row to be deleted */
! 85945: int iCur, /* Cursor number for the table */
! 85946: int *aRegIdx /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
! 85947: ){
! 85948: int i;
! 85949: Index *pIdx;
! 85950: int r1;
! 85951:
! 85952: for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
! 85953: if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue;
! 85954: r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0);
! 85955: sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1);
! 85956: }
! 85957: }
! 85958:
! 85959: /*
! 85960: ** Generate code that will assemble an index key and put it in register
! 85961: ** regOut. The key with be for index pIdx which is an index on pTab.
! 85962: ** iCur is the index of a cursor open on the pTab table and pointing to
! 85963: ** the entry that needs indexing.
! 85964: **
! 85965: ** Return a register number which is the first in a block of
! 85966: ** registers that holds the elements of the index key. The
! 85967: ** block of registers has already been deallocated by the time
! 85968: ** this routine returns.
! 85969: */
! 85970: SQLITE_PRIVATE int sqlite3GenerateIndexKey(
! 85971: Parse *pParse, /* Parsing context */
! 85972: Index *pIdx, /* The index for which to generate a key */
! 85973: int iCur, /* Cursor number for the pIdx->pTable table */
! 85974: int regOut, /* Write the new index key to this register */
! 85975: int doMakeRec /* Run the OP_MakeRecord instruction if true */
! 85976: ){
! 85977: Vdbe *v = pParse->pVdbe;
! 85978: int j;
! 85979: Table *pTab = pIdx->pTable;
! 85980: int regBase;
! 85981: int nCol;
! 85982:
! 85983: nCol = pIdx->nColumn;
! 85984: regBase = sqlite3GetTempRange(pParse, nCol+1);
! 85985: sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol);
! 85986: for(j=0; j<nCol; j++){
! 85987: int idx = pIdx->aiColumn[j];
! 85988: if( idx==pTab->iPKey ){
! 85989: sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j);
! 85990: }else{
! 85991: sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
! 85992: sqlite3ColumnDefault(v, pTab, idx, -1);
! 85993: }
! 85994: }
! 85995: if( doMakeRec ){
! 85996: const char *zAff;
! 85997: if( pTab->pSelect || (pParse->db->flags & SQLITE_IdxRealAsInt)!=0 ){
! 85998: zAff = 0;
! 85999: }else{
! 86000: zAff = sqlite3IndexAffinityStr(v, pIdx);
! 86001: }
! 86002: sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
! 86003: sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
! 86004: }
! 86005: sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
! 86006: return regBase;
! 86007: }
! 86008:
! 86009: /************** End of delete.c **********************************************/
! 86010: /************** Begin file func.c ********************************************/
! 86011: /*
! 86012: ** 2002 February 23
! 86013: **
! 86014: ** The author disclaims copyright to this source code. In place of
! 86015: ** a legal notice, here is a blessing:
! 86016: **
! 86017: ** May you do good and not evil.
! 86018: ** May you find forgiveness for yourself and forgive others.
! 86019: ** May you share freely, never taking more than you give.
! 86020: **
! 86021: *************************************************************************
! 86022: ** This file contains the C functions that implement various SQL
! 86023: ** functions of SQLite.
! 86024: **
! 86025: ** There is only one exported symbol in this file - the function
! 86026: ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
! 86027: ** All other code has file scope.
! 86028: */
! 86029: /* #include <stdlib.h> */
! 86030: /* #include <assert.h> */
! 86031:
! 86032: /*
! 86033: ** Return the collating function associated with a function.
! 86034: */
! 86035: static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
! 86036: return context->pColl;
! 86037: }
! 86038:
! 86039: /*
! 86040: ** Implementation of the non-aggregate min() and max() functions
! 86041: */
! 86042: static void minmaxFunc(
! 86043: sqlite3_context *context,
! 86044: int argc,
! 86045: sqlite3_value **argv
! 86046: ){
! 86047: int i;
! 86048: int mask; /* 0 for min() or 0xffffffff for max() */
! 86049: int iBest;
! 86050: CollSeq *pColl;
! 86051:
! 86052: assert( argc>1 );
! 86053: mask = sqlite3_user_data(context)==0 ? 0 : -1;
! 86054: pColl = sqlite3GetFuncCollSeq(context);
! 86055: assert( pColl );
! 86056: assert( mask==-1 || mask==0 );
! 86057: iBest = 0;
! 86058: if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
! 86059: for(i=1; i<argc; i++){
! 86060: if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
! 86061: if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
! 86062: testcase( mask==0 );
! 86063: iBest = i;
! 86064: }
! 86065: }
! 86066: sqlite3_result_value(context, argv[iBest]);
! 86067: }
! 86068:
! 86069: /*
! 86070: ** Return the type of the argument.
! 86071: */
! 86072: static void typeofFunc(
! 86073: sqlite3_context *context,
! 86074: int NotUsed,
! 86075: sqlite3_value **argv
! 86076: ){
! 86077: const char *z = 0;
! 86078: UNUSED_PARAMETER(NotUsed);
! 86079: switch( sqlite3_value_type(argv[0]) ){
! 86080: case SQLITE_INTEGER: z = "integer"; break;
! 86081: case SQLITE_TEXT: z = "text"; break;
! 86082: case SQLITE_FLOAT: z = "real"; break;
! 86083: case SQLITE_BLOB: z = "blob"; break;
! 86084: default: z = "null"; break;
! 86085: }
! 86086: sqlite3_result_text(context, z, -1, SQLITE_STATIC);
! 86087: }
! 86088:
! 86089:
! 86090: /*
! 86091: ** Implementation of the length() function
! 86092: */
! 86093: static void lengthFunc(
! 86094: sqlite3_context *context,
! 86095: int argc,
! 86096: sqlite3_value **argv
! 86097: ){
! 86098: int len;
! 86099:
! 86100: assert( argc==1 );
! 86101: UNUSED_PARAMETER(argc);
! 86102: switch( sqlite3_value_type(argv[0]) ){
! 86103: case SQLITE_BLOB:
! 86104: case SQLITE_INTEGER:
! 86105: case SQLITE_FLOAT: {
! 86106: sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
! 86107: break;
! 86108: }
! 86109: case SQLITE_TEXT: {
! 86110: const unsigned char *z = sqlite3_value_text(argv[0]);
! 86111: if( z==0 ) return;
! 86112: len = 0;
! 86113: while( *z ){
! 86114: len++;
! 86115: SQLITE_SKIP_UTF8(z);
! 86116: }
! 86117: sqlite3_result_int(context, len);
! 86118: break;
! 86119: }
! 86120: default: {
! 86121: sqlite3_result_null(context);
! 86122: break;
! 86123: }
! 86124: }
! 86125: }
! 86126:
! 86127: /*
! 86128: ** Implementation of the abs() function.
! 86129: **
! 86130: ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
! 86131: ** the numeric argument X.
! 86132: */
! 86133: static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
! 86134: assert( argc==1 );
! 86135: UNUSED_PARAMETER(argc);
! 86136: switch( sqlite3_value_type(argv[0]) ){
! 86137: case SQLITE_INTEGER: {
! 86138: i64 iVal = sqlite3_value_int64(argv[0]);
! 86139: if( iVal<0 ){
! 86140: if( (iVal<<1)==0 ){
! 86141: /* IMP: R-35460-15084 If X is the integer -9223372036854775807 then
! 86142: ** abs(X) throws an integer overflow error since there is no
! 86143: ** equivalent positive 64-bit two complement value. */
! 86144: sqlite3_result_error(context, "integer overflow", -1);
! 86145: return;
! 86146: }
! 86147: iVal = -iVal;
! 86148: }
! 86149: sqlite3_result_int64(context, iVal);
! 86150: break;
! 86151: }
! 86152: case SQLITE_NULL: {
! 86153: /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
! 86154: sqlite3_result_null(context);
! 86155: break;
! 86156: }
! 86157: default: {
! 86158: /* Because sqlite3_value_double() returns 0.0 if the argument is not
! 86159: ** something that can be converted into a number, we have:
! 86160: ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that
! 86161: ** cannot be converted to a numeric value.
! 86162: */
! 86163: double rVal = sqlite3_value_double(argv[0]);
! 86164: if( rVal<0 ) rVal = -rVal;
! 86165: sqlite3_result_double(context, rVal);
! 86166: break;
! 86167: }
! 86168: }
! 86169: }
! 86170:
! 86171: /*
! 86172: ** Implementation of the substr() function.
! 86173: **
! 86174: ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
! 86175: ** p1 is 1-indexed. So substr(x,1,1) returns the first character
! 86176: ** of x. If x is text, then we actually count UTF-8 characters.
! 86177: ** If x is a blob, then we count bytes.
! 86178: **
! 86179: ** If p1 is negative, then we begin abs(p1) from the end of x[].
! 86180: **
! 86181: ** If p2 is negative, return the p2 characters preceeding p1.
! 86182: */
! 86183: static void substrFunc(
! 86184: sqlite3_context *context,
! 86185: int argc,
! 86186: sqlite3_value **argv
! 86187: ){
! 86188: const unsigned char *z;
! 86189: const unsigned char *z2;
! 86190: int len;
! 86191: int p0type;
! 86192: i64 p1, p2;
! 86193: int negP2 = 0;
! 86194:
! 86195: assert( argc==3 || argc==2 );
! 86196: if( sqlite3_value_type(argv[1])==SQLITE_NULL
! 86197: || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
! 86198: ){
! 86199: return;
! 86200: }
! 86201: p0type = sqlite3_value_type(argv[0]);
! 86202: p1 = sqlite3_value_int(argv[1]);
! 86203: if( p0type==SQLITE_BLOB ){
! 86204: len = sqlite3_value_bytes(argv[0]);
! 86205: z = sqlite3_value_blob(argv[0]);
! 86206: if( z==0 ) return;
! 86207: assert( len==sqlite3_value_bytes(argv[0]) );
! 86208: }else{
! 86209: z = sqlite3_value_text(argv[0]);
! 86210: if( z==0 ) return;
! 86211: len = 0;
! 86212: if( p1<0 ){
! 86213: for(z2=z; *z2; len++){
! 86214: SQLITE_SKIP_UTF8(z2);
! 86215: }
! 86216: }
! 86217: }
! 86218: if( argc==3 ){
! 86219: p2 = sqlite3_value_int(argv[2]);
! 86220: if( p2<0 ){
! 86221: p2 = -p2;
! 86222: negP2 = 1;
! 86223: }
! 86224: }else{
! 86225: p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
! 86226: }
! 86227: if( p1<0 ){
! 86228: p1 += len;
! 86229: if( p1<0 ){
! 86230: p2 += p1;
! 86231: if( p2<0 ) p2 = 0;
! 86232: p1 = 0;
! 86233: }
! 86234: }else if( p1>0 ){
! 86235: p1--;
! 86236: }else if( p2>0 ){
! 86237: p2--;
! 86238: }
! 86239: if( negP2 ){
! 86240: p1 -= p2;
! 86241: if( p1<0 ){
! 86242: p2 += p1;
! 86243: p1 = 0;
! 86244: }
! 86245: }
! 86246: assert( p1>=0 && p2>=0 );
! 86247: if( p0type!=SQLITE_BLOB ){
! 86248: while( *z && p1 ){
! 86249: SQLITE_SKIP_UTF8(z);
! 86250: p1--;
! 86251: }
! 86252: for(z2=z; *z2 && p2; p2--){
! 86253: SQLITE_SKIP_UTF8(z2);
! 86254: }
! 86255: sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
! 86256: }else{
! 86257: if( p1+p2>len ){
! 86258: p2 = len-p1;
! 86259: if( p2<0 ) p2 = 0;
! 86260: }
! 86261: sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
! 86262: }
! 86263: }
! 86264:
! 86265: /*
! 86266: ** Implementation of the round() function
! 86267: */
! 86268: #ifndef SQLITE_OMIT_FLOATING_POINT
! 86269: static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
! 86270: int n = 0;
! 86271: double r;
! 86272: char *zBuf;
! 86273: assert( argc==1 || argc==2 );
! 86274: if( argc==2 ){
! 86275: if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
! 86276: n = sqlite3_value_int(argv[1]);
! 86277: if( n>30 ) n = 30;
! 86278: if( n<0 ) n = 0;
! 86279: }
! 86280: if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
! 86281: r = sqlite3_value_double(argv[0]);
! 86282: /* If Y==0 and X will fit in a 64-bit int,
! 86283: ** handle the rounding directly,
! 86284: ** otherwise use printf.
! 86285: */
! 86286: if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
! 86287: r = (double)((sqlite_int64)(r+0.5));
! 86288: }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
! 86289: r = -(double)((sqlite_int64)((-r)+0.5));
! 86290: }else{
! 86291: zBuf = sqlite3_mprintf("%.*f",n,r);
! 86292: if( zBuf==0 ){
! 86293: sqlite3_result_error_nomem(context);
! 86294: return;
! 86295: }
! 86296: sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
! 86297: sqlite3_free(zBuf);
! 86298: }
! 86299: sqlite3_result_double(context, r);
! 86300: }
! 86301: #endif
! 86302:
! 86303: /*
! 86304: ** Allocate nByte bytes of space using sqlite3_malloc(). If the
! 86305: ** allocation fails, call sqlite3_result_error_nomem() to notify
! 86306: ** the database handle that malloc() has failed and return NULL.
! 86307: ** If nByte is larger than the maximum string or blob length, then
! 86308: ** raise an SQLITE_TOOBIG exception and return NULL.
! 86309: */
! 86310: static void *contextMalloc(sqlite3_context *context, i64 nByte){
! 86311: char *z;
! 86312: sqlite3 *db = sqlite3_context_db_handle(context);
! 86313: assert( nByte>0 );
! 86314: testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
! 86315: testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
! 86316: if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
! 86317: sqlite3_result_error_toobig(context);
! 86318: z = 0;
! 86319: }else{
! 86320: z = sqlite3Malloc((int)nByte);
! 86321: if( !z ){
! 86322: sqlite3_result_error_nomem(context);
! 86323: }
! 86324: }
! 86325: return z;
! 86326: }
! 86327:
! 86328: /*
! 86329: ** Implementation of the upper() and lower() SQL functions.
! 86330: */
! 86331: static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
! 86332: char *z1;
! 86333: const char *z2;
! 86334: int i, n;
! 86335: UNUSED_PARAMETER(argc);
! 86336: z2 = (char*)sqlite3_value_text(argv[0]);
! 86337: n = sqlite3_value_bytes(argv[0]);
! 86338: /* Verify that the call to _bytes() does not invalidate the _text() pointer */
! 86339: assert( z2==(char*)sqlite3_value_text(argv[0]) );
! 86340: if( z2 ){
! 86341: z1 = contextMalloc(context, ((i64)n)+1);
! 86342: if( z1 ){
! 86343: for(i=0; i<n; i++){
! 86344: z1[i] = (char)sqlite3Toupper(z2[i]);
! 86345: }
! 86346: sqlite3_result_text(context, z1, n, sqlite3_free);
! 86347: }
! 86348: }
! 86349: }
! 86350: static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
! 86351: char *z1;
! 86352: const char *z2;
! 86353: int i, n;
! 86354: UNUSED_PARAMETER(argc);
! 86355: z2 = (char*)sqlite3_value_text(argv[0]);
! 86356: n = sqlite3_value_bytes(argv[0]);
! 86357: /* Verify that the call to _bytes() does not invalidate the _text() pointer */
! 86358: assert( z2==(char*)sqlite3_value_text(argv[0]) );
! 86359: if( z2 ){
! 86360: z1 = contextMalloc(context, ((i64)n)+1);
! 86361: if( z1 ){
! 86362: for(i=0; i<n; i++){
! 86363: z1[i] = sqlite3Tolower(z2[i]);
! 86364: }
! 86365: sqlite3_result_text(context, z1, n, sqlite3_free);
! 86366: }
! 86367: }
! 86368: }
! 86369:
! 86370:
! 86371: #if 0 /* This function is never used. */
! 86372: /*
! 86373: ** The COALESCE() and IFNULL() functions used to be implemented as shown
! 86374: ** here. But now they are implemented as VDBE code so that unused arguments
! 86375: ** do not have to be computed. This legacy implementation is retained as
! 86376: ** comment.
! 86377: */
! 86378: /*
! 86379: ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
! 86380: ** All three do the same thing. They return the first non-NULL
! 86381: ** argument.
! 86382: */
! 86383: static void ifnullFunc(
! 86384: sqlite3_context *context,
! 86385: int argc,
! 86386: sqlite3_value **argv
! 86387: ){
! 86388: int i;
! 86389: for(i=0; i<argc; i++){
! 86390: if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
! 86391: sqlite3_result_value(context, argv[i]);
! 86392: break;
! 86393: }
! 86394: }
! 86395: }
! 86396: #endif /* NOT USED */
! 86397: #define ifnullFunc versionFunc /* Substitute function - never called */
! 86398:
! 86399: /*
! 86400: ** Implementation of random(). Return a random integer.
! 86401: */
! 86402: static void randomFunc(
! 86403: sqlite3_context *context,
! 86404: int NotUsed,
! 86405: sqlite3_value **NotUsed2
! 86406: ){
! 86407: sqlite_int64 r;
! 86408: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 86409: sqlite3_randomness(sizeof(r), &r);
! 86410: if( r<0 ){
! 86411: /* We need to prevent a random number of 0x8000000000000000
! 86412: ** (or -9223372036854775808) since when you do abs() of that
! 86413: ** number of you get the same value back again. To do this
! 86414: ** in a way that is testable, mask the sign bit off of negative
! 86415: ** values, resulting in a positive value. Then take the
! 86416: ** 2s complement of that positive value. The end result can
! 86417: ** therefore be no less than -9223372036854775807.
! 86418: */
! 86419: r = -(r ^ (((sqlite3_int64)1)<<63));
! 86420: }
! 86421: sqlite3_result_int64(context, r);
! 86422: }
! 86423:
! 86424: /*
! 86425: ** Implementation of randomblob(N). Return a random blob
! 86426: ** that is N bytes long.
! 86427: */
! 86428: static void randomBlob(
! 86429: sqlite3_context *context,
! 86430: int argc,
! 86431: sqlite3_value **argv
! 86432: ){
! 86433: int n;
! 86434: unsigned char *p;
! 86435: assert( argc==1 );
! 86436: UNUSED_PARAMETER(argc);
! 86437: n = sqlite3_value_int(argv[0]);
! 86438: if( n<1 ){
! 86439: n = 1;
! 86440: }
! 86441: p = contextMalloc(context, n);
! 86442: if( p ){
! 86443: sqlite3_randomness(n, p);
! 86444: sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
! 86445: }
! 86446: }
! 86447:
! 86448: /*
! 86449: ** Implementation of the last_insert_rowid() SQL function. The return
! 86450: ** value is the same as the sqlite3_last_insert_rowid() API function.
! 86451: */
! 86452: static void last_insert_rowid(
! 86453: sqlite3_context *context,
! 86454: int NotUsed,
! 86455: sqlite3_value **NotUsed2
! 86456: ){
! 86457: sqlite3 *db = sqlite3_context_db_handle(context);
! 86458: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 86459: /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
! 86460: ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
! 86461: ** function. */
! 86462: sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
! 86463: }
! 86464:
! 86465: /*
! 86466: ** Implementation of the changes() SQL function.
! 86467: **
! 86468: ** IMP: R-62073-11209 The changes() SQL function is a wrapper
! 86469: ** around the sqlite3_changes() C/C++ function and hence follows the same
! 86470: ** rules for counting changes.
! 86471: */
! 86472: static void changes(
! 86473: sqlite3_context *context,
! 86474: int NotUsed,
! 86475: sqlite3_value **NotUsed2
! 86476: ){
! 86477: sqlite3 *db = sqlite3_context_db_handle(context);
! 86478: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 86479: sqlite3_result_int(context, sqlite3_changes(db));
! 86480: }
! 86481:
! 86482: /*
! 86483: ** Implementation of the total_changes() SQL function. The return value is
! 86484: ** the same as the sqlite3_total_changes() API function.
! 86485: */
! 86486: static void total_changes(
! 86487: sqlite3_context *context,
! 86488: int NotUsed,
! 86489: sqlite3_value **NotUsed2
! 86490: ){
! 86491: sqlite3 *db = sqlite3_context_db_handle(context);
! 86492: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 86493: /* IMP: R-52756-41993 This function is a wrapper around the
! 86494: ** sqlite3_total_changes() C/C++ interface. */
! 86495: sqlite3_result_int(context, sqlite3_total_changes(db));
! 86496: }
! 86497:
! 86498: /*
! 86499: ** A structure defining how to do GLOB-style comparisons.
! 86500: */
! 86501: struct compareInfo {
! 86502: u8 matchAll;
! 86503: u8 matchOne;
! 86504: u8 matchSet;
! 86505: u8 noCase;
! 86506: };
! 86507:
! 86508: /*
! 86509: ** For LIKE and GLOB matching on EBCDIC machines, assume that every
! 86510: ** character is exactly one byte in size. Also, all characters are
! 86511: ** able to participate in upper-case-to-lower-case mappings in EBCDIC
! 86512: ** whereas only characters less than 0x80 do in ASCII.
! 86513: */
! 86514: #if defined(SQLITE_EBCDIC)
! 86515: # define sqlite3Utf8Read(A,C) (*(A++))
! 86516: # define GlogUpperToLower(A) A = sqlite3UpperToLower[A]
! 86517: #else
! 86518: # define GlogUpperToLower(A) if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
! 86519: #endif
! 86520:
! 86521: static const struct compareInfo globInfo = { '*', '?', '[', 0 };
! 86522: /* The correct SQL-92 behavior is for the LIKE operator to ignore
! 86523: ** case. Thus 'a' LIKE 'A' would be true. */
! 86524: static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
! 86525: /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
! 86526: ** is case sensitive causing 'a' LIKE 'A' to be false */
! 86527: static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
! 86528:
! 86529: /*
! 86530: ** Compare two UTF-8 strings for equality where the first string can
! 86531: ** potentially be a "glob" expression. Return true (1) if they
! 86532: ** are the same and false (0) if they are different.
! 86533: **
! 86534: ** Globbing rules:
! 86535: **
! 86536: ** '*' Matches any sequence of zero or more characters.
! 86537: **
! 86538: ** '?' Matches exactly one character.
! 86539: **
! 86540: ** [...] Matches one character from the enclosed list of
! 86541: ** characters.
! 86542: **
! 86543: ** [^...] Matches one character not in the enclosed list.
! 86544: **
! 86545: ** With the [...] and [^...] matching, a ']' character can be included
! 86546: ** in the list by making it the first character after '[' or '^'. A
! 86547: ** range of characters can be specified using '-'. Example:
! 86548: ** "[a-z]" matches any single lower-case letter. To match a '-', make
! 86549: ** it the last character in the list.
! 86550: **
! 86551: ** This routine is usually quick, but can be N**2 in the worst case.
! 86552: **
! 86553: ** Hints: to match '*' or '?', put them in "[]". Like this:
! 86554: **
! 86555: ** abc[*]xyz Matches "abc*xyz" only
! 86556: */
! 86557: static int patternCompare(
! 86558: const u8 *zPattern, /* The glob pattern */
! 86559: const u8 *zString, /* The string to compare against the glob */
! 86560: const struct compareInfo *pInfo, /* Information about how to do the compare */
! 86561: u32 esc /* The escape character */
! 86562: ){
! 86563: u32 c, c2;
! 86564: int invert;
! 86565: int seen;
! 86566: u8 matchOne = pInfo->matchOne;
! 86567: u8 matchAll = pInfo->matchAll;
! 86568: u8 matchSet = pInfo->matchSet;
! 86569: u8 noCase = pInfo->noCase;
! 86570: int prevEscape = 0; /* True if the previous character was 'escape' */
! 86571:
! 86572: while( (c = sqlite3Utf8Read(zPattern,&zPattern))!=0 ){
! 86573: if( !prevEscape && c==matchAll ){
! 86574: while( (c=sqlite3Utf8Read(zPattern,&zPattern)) == matchAll
! 86575: || c == matchOne ){
! 86576: if( c==matchOne && sqlite3Utf8Read(zString, &zString)==0 ){
! 86577: return 0;
! 86578: }
! 86579: }
! 86580: if( c==0 ){
! 86581: return 1;
! 86582: }else if( c==esc ){
! 86583: c = sqlite3Utf8Read(zPattern, &zPattern);
! 86584: if( c==0 ){
! 86585: return 0;
! 86586: }
! 86587: }else if( c==matchSet ){
! 86588: assert( esc==0 ); /* This is GLOB, not LIKE */
! 86589: assert( matchSet<0x80 ); /* '[' is a single-byte character */
! 86590: while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
! 86591: SQLITE_SKIP_UTF8(zString);
! 86592: }
! 86593: return *zString!=0;
! 86594: }
! 86595: while( (c2 = sqlite3Utf8Read(zString,&zString))!=0 ){
! 86596: if( noCase ){
! 86597: GlogUpperToLower(c2);
! 86598: GlogUpperToLower(c);
! 86599: while( c2 != 0 && c2 != c ){
! 86600: c2 = sqlite3Utf8Read(zString, &zString);
! 86601: GlogUpperToLower(c2);
! 86602: }
! 86603: }else{
! 86604: while( c2 != 0 && c2 != c ){
! 86605: c2 = sqlite3Utf8Read(zString, &zString);
! 86606: }
! 86607: }
! 86608: if( c2==0 ) return 0;
! 86609: if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
! 86610: }
! 86611: return 0;
! 86612: }else if( !prevEscape && c==matchOne ){
! 86613: if( sqlite3Utf8Read(zString, &zString)==0 ){
! 86614: return 0;
! 86615: }
! 86616: }else if( c==matchSet ){
! 86617: u32 prior_c = 0;
! 86618: assert( esc==0 ); /* This only occurs for GLOB, not LIKE */
! 86619: seen = 0;
! 86620: invert = 0;
! 86621: c = sqlite3Utf8Read(zString, &zString);
! 86622: if( c==0 ) return 0;
! 86623: c2 = sqlite3Utf8Read(zPattern, &zPattern);
! 86624: if( c2=='^' ){
! 86625: invert = 1;
! 86626: c2 = sqlite3Utf8Read(zPattern, &zPattern);
! 86627: }
! 86628: if( c2==']' ){
! 86629: if( c==']' ) seen = 1;
! 86630: c2 = sqlite3Utf8Read(zPattern, &zPattern);
! 86631: }
! 86632: while( c2 && c2!=']' ){
! 86633: if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
! 86634: c2 = sqlite3Utf8Read(zPattern, &zPattern);
! 86635: if( c>=prior_c && c<=c2 ) seen = 1;
! 86636: prior_c = 0;
! 86637: }else{
! 86638: if( c==c2 ){
! 86639: seen = 1;
! 86640: }
! 86641: prior_c = c2;
! 86642: }
! 86643: c2 = sqlite3Utf8Read(zPattern, &zPattern);
! 86644: }
! 86645: if( c2==0 || (seen ^ invert)==0 ){
! 86646: return 0;
! 86647: }
! 86648: }else if( esc==c && !prevEscape ){
! 86649: prevEscape = 1;
! 86650: }else{
! 86651: c2 = sqlite3Utf8Read(zString, &zString);
! 86652: if( noCase ){
! 86653: GlogUpperToLower(c);
! 86654: GlogUpperToLower(c2);
! 86655: }
! 86656: if( c!=c2 ){
! 86657: return 0;
! 86658: }
! 86659: prevEscape = 0;
! 86660: }
! 86661: }
! 86662: return *zString==0;
! 86663: }
! 86664:
! 86665: /*
! 86666: ** Count the number of times that the LIKE operator (or GLOB which is
! 86667: ** just a variation of LIKE) gets called. This is used for testing
! 86668: ** only.
! 86669: */
! 86670: #ifdef SQLITE_TEST
! 86671: SQLITE_API int sqlite3_like_count = 0;
! 86672: #endif
! 86673:
! 86674:
! 86675: /*
! 86676: ** Implementation of the like() SQL function. This function implements
! 86677: ** the build-in LIKE operator. The first argument to the function is the
! 86678: ** pattern and the second argument is the string. So, the SQL statements:
! 86679: **
! 86680: ** A LIKE B
! 86681: **
! 86682: ** is implemented as like(B,A).
! 86683: **
! 86684: ** This same function (with a different compareInfo structure) computes
! 86685: ** the GLOB operator.
! 86686: */
! 86687: static void likeFunc(
! 86688: sqlite3_context *context,
! 86689: int argc,
! 86690: sqlite3_value **argv
! 86691: ){
! 86692: const unsigned char *zA, *zB;
! 86693: u32 escape = 0;
! 86694: int nPat;
! 86695: sqlite3 *db = sqlite3_context_db_handle(context);
! 86696:
! 86697: zB = sqlite3_value_text(argv[0]);
! 86698: zA = sqlite3_value_text(argv[1]);
! 86699:
! 86700: /* Limit the length of the LIKE or GLOB pattern to avoid problems
! 86701: ** of deep recursion and N*N behavior in patternCompare().
! 86702: */
! 86703: nPat = sqlite3_value_bytes(argv[0]);
! 86704: testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
! 86705: testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
! 86706: if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
! 86707: sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
! 86708: return;
! 86709: }
! 86710: assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */
! 86711:
! 86712: if( argc==3 ){
! 86713: /* The escape character string must consist of a single UTF-8 character.
! 86714: ** Otherwise, return an error.
! 86715: */
! 86716: const unsigned char *zEsc = sqlite3_value_text(argv[2]);
! 86717: if( zEsc==0 ) return;
! 86718: if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
! 86719: sqlite3_result_error(context,
! 86720: "ESCAPE expression must be a single character", -1);
! 86721: return;
! 86722: }
! 86723: escape = sqlite3Utf8Read(zEsc, &zEsc);
! 86724: }
! 86725: if( zA && zB ){
! 86726: struct compareInfo *pInfo = sqlite3_user_data(context);
! 86727: #ifdef SQLITE_TEST
! 86728: sqlite3_like_count++;
! 86729: #endif
! 86730:
! 86731: sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
! 86732: }
! 86733: }
! 86734:
! 86735: /*
! 86736: ** Implementation of the NULLIF(x,y) function. The result is the first
! 86737: ** argument if the arguments are different. The result is NULL if the
! 86738: ** arguments are equal to each other.
! 86739: */
! 86740: static void nullifFunc(
! 86741: sqlite3_context *context,
! 86742: int NotUsed,
! 86743: sqlite3_value **argv
! 86744: ){
! 86745: CollSeq *pColl = sqlite3GetFuncCollSeq(context);
! 86746: UNUSED_PARAMETER(NotUsed);
! 86747: if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
! 86748: sqlite3_result_value(context, argv[0]);
! 86749: }
! 86750: }
! 86751:
! 86752: /*
! 86753: ** Implementation of the sqlite_version() function. The result is the version
! 86754: ** of the SQLite library that is running.
! 86755: */
! 86756: static void versionFunc(
! 86757: sqlite3_context *context,
! 86758: int NotUsed,
! 86759: sqlite3_value **NotUsed2
! 86760: ){
! 86761: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 86762: /* IMP: R-48699-48617 This function is an SQL wrapper around the
! 86763: ** sqlite3_libversion() C-interface. */
! 86764: sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
! 86765: }
! 86766:
! 86767: /*
! 86768: ** Implementation of the sqlite_source_id() function. The result is a string
! 86769: ** that identifies the particular version of the source code used to build
! 86770: ** SQLite.
! 86771: */
! 86772: static void sourceidFunc(
! 86773: sqlite3_context *context,
! 86774: int NotUsed,
! 86775: sqlite3_value **NotUsed2
! 86776: ){
! 86777: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 86778: /* IMP: R-24470-31136 This function is an SQL wrapper around the
! 86779: ** sqlite3_sourceid() C interface. */
! 86780: sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
! 86781: }
! 86782:
! 86783: /*
! 86784: ** Implementation of the sqlite_log() function. This is a wrapper around
! 86785: ** sqlite3_log(). The return value is NULL. The function exists purely for
! 86786: ** its side-effects.
! 86787: */
! 86788: static void errlogFunc(
! 86789: sqlite3_context *context,
! 86790: int argc,
! 86791: sqlite3_value **argv
! 86792: ){
! 86793: UNUSED_PARAMETER(argc);
! 86794: UNUSED_PARAMETER(context);
! 86795: sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
! 86796: }
! 86797:
! 86798: /*
! 86799: ** Implementation of the sqlite_compileoption_used() function.
! 86800: ** The result is an integer that identifies if the compiler option
! 86801: ** was used to build SQLite.
! 86802: */
! 86803: #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
! 86804: static void compileoptionusedFunc(
! 86805: sqlite3_context *context,
! 86806: int argc,
! 86807: sqlite3_value **argv
! 86808: ){
! 86809: const char *zOptName;
! 86810: assert( argc==1 );
! 86811: UNUSED_PARAMETER(argc);
! 86812: /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
! 86813: ** function is a wrapper around the sqlite3_compileoption_used() C/C++
! 86814: ** function.
! 86815: */
! 86816: if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
! 86817: sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
! 86818: }
! 86819: }
! 86820: #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
! 86821:
! 86822: /*
! 86823: ** Implementation of the sqlite_compileoption_get() function.
! 86824: ** The result is a string that identifies the compiler options
! 86825: ** used to build SQLite.
! 86826: */
! 86827: #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
! 86828: static void compileoptiongetFunc(
! 86829: sqlite3_context *context,
! 86830: int argc,
! 86831: sqlite3_value **argv
! 86832: ){
! 86833: int n;
! 86834: assert( argc==1 );
! 86835: UNUSED_PARAMETER(argc);
! 86836: /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
! 86837: ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
! 86838: */
! 86839: n = sqlite3_value_int(argv[0]);
! 86840: sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
! 86841: }
! 86842: #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
! 86843:
! 86844: /* Array for converting from half-bytes (nybbles) into ASCII hex
! 86845: ** digits. */
! 86846: static const char hexdigits[] = {
! 86847: '0', '1', '2', '3', '4', '5', '6', '7',
! 86848: '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
! 86849: };
! 86850:
! 86851: /*
! 86852: ** EXPERIMENTAL - This is not an official function. The interface may
! 86853: ** change. This function may disappear. Do not write code that depends
! 86854: ** on this function.
! 86855: **
! 86856: ** Implementation of the QUOTE() function. This function takes a single
! 86857: ** argument. If the argument is numeric, the return value is the same as
! 86858: ** the argument. If the argument is NULL, the return value is the string
! 86859: ** "NULL". Otherwise, the argument is enclosed in single quotes with
! 86860: ** single-quote escapes.
! 86861: */
! 86862: static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
! 86863: assert( argc==1 );
! 86864: UNUSED_PARAMETER(argc);
! 86865: switch( sqlite3_value_type(argv[0]) ){
! 86866: case SQLITE_INTEGER:
! 86867: case SQLITE_FLOAT: {
! 86868: sqlite3_result_value(context, argv[0]);
! 86869: break;
! 86870: }
! 86871: case SQLITE_BLOB: {
! 86872: char *zText = 0;
! 86873: char const *zBlob = sqlite3_value_blob(argv[0]);
! 86874: int nBlob = sqlite3_value_bytes(argv[0]);
! 86875: assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
! 86876: zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
! 86877: if( zText ){
! 86878: int i;
! 86879: for(i=0; i<nBlob; i++){
! 86880: zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
! 86881: zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
! 86882: }
! 86883: zText[(nBlob*2)+2] = '\'';
! 86884: zText[(nBlob*2)+3] = '\0';
! 86885: zText[0] = 'X';
! 86886: zText[1] = '\'';
! 86887: sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
! 86888: sqlite3_free(zText);
! 86889: }
! 86890: break;
! 86891: }
! 86892: case SQLITE_TEXT: {
! 86893: int i,j;
! 86894: u64 n;
! 86895: const unsigned char *zArg = sqlite3_value_text(argv[0]);
! 86896: char *z;
! 86897:
! 86898: if( zArg==0 ) return;
! 86899: for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
! 86900: z = contextMalloc(context, ((i64)i)+((i64)n)+3);
! 86901: if( z ){
! 86902: z[0] = '\'';
! 86903: for(i=0, j=1; zArg[i]; i++){
! 86904: z[j++] = zArg[i];
! 86905: if( zArg[i]=='\'' ){
! 86906: z[j++] = '\'';
! 86907: }
! 86908: }
! 86909: z[j++] = '\'';
! 86910: z[j] = 0;
! 86911: sqlite3_result_text(context, z, j, sqlite3_free);
! 86912: }
! 86913: break;
! 86914: }
! 86915: default: {
! 86916: assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
! 86917: sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
! 86918: break;
! 86919: }
! 86920: }
! 86921: }
! 86922:
! 86923: /*
! 86924: ** The hex() function. Interpret the argument as a blob. Return
! 86925: ** a hexadecimal rendering as text.
! 86926: */
! 86927: static void hexFunc(
! 86928: sqlite3_context *context,
! 86929: int argc,
! 86930: sqlite3_value **argv
! 86931: ){
! 86932: int i, n;
! 86933: const unsigned char *pBlob;
! 86934: char *zHex, *z;
! 86935: assert( argc==1 );
! 86936: UNUSED_PARAMETER(argc);
! 86937: pBlob = sqlite3_value_blob(argv[0]);
! 86938: n = sqlite3_value_bytes(argv[0]);
! 86939: assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
! 86940: z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
! 86941: if( zHex ){
! 86942: for(i=0; i<n; i++, pBlob++){
! 86943: unsigned char c = *pBlob;
! 86944: *(z++) = hexdigits[(c>>4)&0xf];
! 86945: *(z++) = hexdigits[c&0xf];
! 86946: }
! 86947: *z = 0;
! 86948: sqlite3_result_text(context, zHex, n*2, sqlite3_free);
! 86949: }
! 86950: }
! 86951:
! 86952: /*
! 86953: ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
! 86954: */
! 86955: static void zeroblobFunc(
! 86956: sqlite3_context *context,
! 86957: int argc,
! 86958: sqlite3_value **argv
! 86959: ){
! 86960: i64 n;
! 86961: sqlite3 *db = sqlite3_context_db_handle(context);
! 86962: assert( argc==1 );
! 86963: UNUSED_PARAMETER(argc);
! 86964: n = sqlite3_value_int64(argv[0]);
! 86965: testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
! 86966: testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
! 86967: if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
! 86968: sqlite3_result_error_toobig(context);
! 86969: }else{
! 86970: sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
! 86971: }
! 86972: }
! 86973:
! 86974: /*
! 86975: ** The replace() function. Three arguments are all strings: call
! 86976: ** them A, B, and C. The result is also a string which is derived
! 86977: ** from A by replacing every occurance of B with C. The match
! 86978: ** must be exact. Collating sequences are not used.
! 86979: */
! 86980: static void replaceFunc(
! 86981: sqlite3_context *context,
! 86982: int argc,
! 86983: sqlite3_value **argv
! 86984: ){
! 86985: const unsigned char *zStr; /* The input string A */
! 86986: const unsigned char *zPattern; /* The pattern string B */
! 86987: const unsigned char *zRep; /* The replacement string C */
! 86988: unsigned char *zOut; /* The output */
! 86989: int nStr; /* Size of zStr */
! 86990: int nPattern; /* Size of zPattern */
! 86991: int nRep; /* Size of zRep */
! 86992: i64 nOut; /* Maximum size of zOut */
! 86993: int loopLimit; /* Last zStr[] that might match zPattern[] */
! 86994: int i, j; /* Loop counters */
! 86995:
! 86996: assert( argc==3 );
! 86997: UNUSED_PARAMETER(argc);
! 86998: zStr = sqlite3_value_text(argv[0]);
! 86999: if( zStr==0 ) return;
! 87000: nStr = sqlite3_value_bytes(argv[0]);
! 87001: assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
! 87002: zPattern = sqlite3_value_text(argv[1]);
! 87003: if( zPattern==0 ){
! 87004: assert( sqlite3_value_type(argv[1])==SQLITE_NULL
! 87005: || sqlite3_context_db_handle(context)->mallocFailed );
! 87006: return;
! 87007: }
! 87008: if( zPattern[0]==0 ){
! 87009: assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
! 87010: sqlite3_result_value(context, argv[0]);
! 87011: return;
! 87012: }
! 87013: nPattern = sqlite3_value_bytes(argv[1]);
! 87014: assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */
! 87015: zRep = sqlite3_value_text(argv[2]);
! 87016: if( zRep==0 ) return;
! 87017: nRep = sqlite3_value_bytes(argv[2]);
! 87018: assert( zRep==sqlite3_value_text(argv[2]) );
! 87019: nOut = nStr + 1;
! 87020: assert( nOut<SQLITE_MAX_LENGTH );
! 87021: zOut = contextMalloc(context, (i64)nOut);
! 87022: if( zOut==0 ){
! 87023: return;
! 87024: }
! 87025: loopLimit = nStr - nPattern;
! 87026: for(i=j=0; i<=loopLimit; i++){
! 87027: if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
! 87028: zOut[j++] = zStr[i];
! 87029: }else{
! 87030: u8 *zOld;
! 87031: sqlite3 *db = sqlite3_context_db_handle(context);
! 87032: nOut += nRep - nPattern;
! 87033: testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
! 87034: testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
! 87035: if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
! 87036: sqlite3_result_error_toobig(context);
! 87037: sqlite3_free(zOut);
! 87038: return;
! 87039: }
! 87040: zOld = zOut;
! 87041: zOut = sqlite3_realloc(zOut, (int)nOut);
! 87042: if( zOut==0 ){
! 87043: sqlite3_result_error_nomem(context);
! 87044: sqlite3_free(zOld);
! 87045: return;
! 87046: }
! 87047: memcpy(&zOut[j], zRep, nRep);
! 87048: j += nRep;
! 87049: i += nPattern-1;
! 87050: }
! 87051: }
! 87052: assert( j+nStr-i+1==nOut );
! 87053: memcpy(&zOut[j], &zStr[i], nStr-i);
! 87054: j += nStr - i;
! 87055: assert( j<=nOut );
! 87056: zOut[j] = 0;
! 87057: sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
! 87058: }
! 87059:
! 87060: /*
! 87061: ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
! 87062: ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
! 87063: */
! 87064: static void trimFunc(
! 87065: sqlite3_context *context,
! 87066: int argc,
! 87067: sqlite3_value **argv
! 87068: ){
! 87069: const unsigned char *zIn; /* Input string */
! 87070: const unsigned char *zCharSet; /* Set of characters to trim */
! 87071: int nIn; /* Number of bytes in input */
! 87072: int flags; /* 1: trimleft 2: trimright 3: trim */
! 87073: int i; /* Loop counter */
! 87074: unsigned char *aLen = 0; /* Length of each character in zCharSet */
! 87075: unsigned char **azChar = 0; /* Individual characters in zCharSet */
! 87076: int nChar; /* Number of characters in zCharSet */
! 87077:
! 87078: if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
! 87079: return;
! 87080: }
! 87081: zIn = sqlite3_value_text(argv[0]);
! 87082: if( zIn==0 ) return;
! 87083: nIn = sqlite3_value_bytes(argv[0]);
! 87084: assert( zIn==sqlite3_value_text(argv[0]) );
! 87085: if( argc==1 ){
! 87086: static const unsigned char lenOne[] = { 1 };
! 87087: static unsigned char * const azOne[] = { (u8*)" " };
! 87088: nChar = 1;
! 87089: aLen = (u8*)lenOne;
! 87090: azChar = (unsigned char **)azOne;
! 87091: zCharSet = 0;
! 87092: }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
! 87093: return;
! 87094: }else{
! 87095: const unsigned char *z;
! 87096: for(z=zCharSet, nChar=0; *z; nChar++){
! 87097: SQLITE_SKIP_UTF8(z);
! 87098: }
! 87099: if( nChar>0 ){
! 87100: azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
! 87101: if( azChar==0 ){
! 87102: return;
! 87103: }
! 87104: aLen = (unsigned char*)&azChar[nChar];
! 87105: for(z=zCharSet, nChar=0; *z; nChar++){
! 87106: azChar[nChar] = (unsigned char *)z;
! 87107: SQLITE_SKIP_UTF8(z);
! 87108: aLen[nChar] = (u8)(z - azChar[nChar]);
! 87109: }
! 87110: }
! 87111: }
! 87112: if( nChar>0 ){
! 87113: flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
! 87114: if( flags & 1 ){
! 87115: while( nIn>0 ){
! 87116: int len = 0;
! 87117: for(i=0; i<nChar; i++){
! 87118: len = aLen[i];
! 87119: if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
! 87120: }
! 87121: if( i>=nChar ) break;
! 87122: zIn += len;
! 87123: nIn -= len;
! 87124: }
! 87125: }
! 87126: if( flags & 2 ){
! 87127: while( nIn>0 ){
! 87128: int len = 0;
! 87129: for(i=0; i<nChar; i++){
! 87130: len = aLen[i];
! 87131: if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
! 87132: }
! 87133: if( i>=nChar ) break;
! 87134: nIn -= len;
! 87135: }
! 87136: }
! 87137: if( zCharSet ){
! 87138: sqlite3_free(azChar);
! 87139: }
! 87140: }
! 87141: sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
! 87142: }
! 87143:
! 87144:
! 87145: /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
! 87146: ** is only available if the SQLITE_SOUNDEX compile-time option is used
! 87147: ** when SQLite is built.
! 87148: */
! 87149: #ifdef SQLITE_SOUNDEX
! 87150: /*
! 87151: ** Compute the soundex encoding of a word.
! 87152: **
! 87153: ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
! 87154: ** soundex encoding of the string X.
! 87155: */
! 87156: static void soundexFunc(
! 87157: sqlite3_context *context,
! 87158: int argc,
! 87159: sqlite3_value **argv
! 87160: ){
! 87161: char zResult[8];
! 87162: const u8 *zIn;
! 87163: int i, j;
! 87164: static const unsigned char iCode[] = {
! 87165: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
! 87166: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
! 87167: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
! 87168: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
! 87169: 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
! 87170: 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
! 87171: 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
! 87172: 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
! 87173: };
! 87174: assert( argc==1 );
! 87175: zIn = (u8*)sqlite3_value_text(argv[0]);
! 87176: if( zIn==0 ) zIn = (u8*)"";
! 87177: for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
! 87178: if( zIn[i] ){
! 87179: u8 prevcode = iCode[zIn[i]&0x7f];
! 87180: zResult[0] = sqlite3Toupper(zIn[i]);
! 87181: for(j=1; j<4 && zIn[i]; i++){
! 87182: int code = iCode[zIn[i]&0x7f];
! 87183: if( code>0 ){
! 87184: if( code!=prevcode ){
! 87185: prevcode = code;
! 87186: zResult[j++] = code + '0';
! 87187: }
! 87188: }else{
! 87189: prevcode = 0;
! 87190: }
! 87191: }
! 87192: while( j<4 ){
! 87193: zResult[j++] = '0';
! 87194: }
! 87195: zResult[j] = 0;
! 87196: sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
! 87197: }else{
! 87198: /* IMP: R-64894-50321 The string "?000" is returned if the argument
! 87199: ** is NULL or contains no ASCII alphabetic characters. */
! 87200: sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
! 87201: }
! 87202: }
! 87203: #endif /* SQLITE_SOUNDEX */
! 87204:
! 87205: #ifndef SQLITE_OMIT_LOAD_EXTENSION
! 87206: /*
! 87207: ** A function that loads a shared-library extension then returns NULL.
! 87208: */
! 87209: static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
! 87210: const char *zFile = (const char *)sqlite3_value_text(argv[0]);
! 87211: const char *zProc;
! 87212: sqlite3 *db = sqlite3_context_db_handle(context);
! 87213: char *zErrMsg = 0;
! 87214:
! 87215: if( argc==2 ){
! 87216: zProc = (const char *)sqlite3_value_text(argv[1]);
! 87217: }else{
! 87218: zProc = 0;
! 87219: }
! 87220: if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
! 87221: sqlite3_result_error(context, zErrMsg, -1);
! 87222: sqlite3_free(zErrMsg);
! 87223: }
! 87224: }
! 87225: #endif
! 87226:
! 87227:
! 87228: /*
! 87229: ** An instance of the following structure holds the context of a
! 87230: ** sum() or avg() aggregate computation.
! 87231: */
! 87232: typedef struct SumCtx SumCtx;
! 87233: struct SumCtx {
! 87234: double rSum; /* Floating point sum */
! 87235: i64 iSum; /* Integer sum */
! 87236: i64 cnt; /* Number of elements summed */
! 87237: u8 overflow; /* True if integer overflow seen */
! 87238: u8 approx; /* True if non-integer value was input to the sum */
! 87239: };
! 87240:
! 87241: /*
! 87242: ** Routines used to compute the sum, average, and total.
! 87243: **
! 87244: ** The SUM() function follows the (broken) SQL standard which means
! 87245: ** that it returns NULL if it sums over no inputs. TOTAL returns
! 87246: ** 0.0 in that case. In addition, TOTAL always returns a float where
! 87247: ** SUM might return an integer if it never encounters a floating point
! 87248: ** value. TOTAL never fails, but SUM might through an exception if
! 87249: ** it overflows an integer.
! 87250: */
! 87251: static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
! 87252: SumCtx *p;
! 87253: int type;
! 87254: assert( argc==1 );
! 87255: UNUSED_PARAMETER(argc);
! 87256: p = sqlite3_aggregate_context(context, sizeof(*p));
! 87257: type = sqlite3_value_numeric_type(argv[0]);
! 87258: if( p && type!=SQLITE_NULL ){
! 87259: p->cnt++;
! 87260: if( type==SQLITE_INTEGER ){
! 87261: i64 v = sqlite3_value_int64(argv[0]);
! 87262: p->rSum += v;
! 87263: if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
! 87264: p->overflow = 1;
! 87265: }
! 87266: }else{
! 87267: p->rSum += sqlite3_value_double(argv[0]);
! 87268: p->approx = 1;
! 87269: }
! 87270: }
! 87271: }
! 87272: static void sumFinalize(sqlite3_context *context){
! 87273: SumCtx *p;
! 87274: p = sqlite3_aggregate_context(context, 0);
! 87275: if( p && p->cnt>0 ){
! 87276: if( p->overflow ){
! 87277: sqlite3_result_error(context,"integer overflow",-1);
! 87278: }else if( p->approx ){
! 87279: sqlite3_result_double(context, p->rSum);
! 87280: }else{
! 87281: sqlite3_result_int64(context, p->iSum);
! 87282: }
! 87283: }
! 87284: }
! 87285: static void avgFinalize(sqlite3_context *context){
! 87286: SumCtx *p;
! 87287: p = sqlite3_aggregate_context(context, 0);
! 87288: if( p && p->cnt>0 ){
! 87289: sqlite3_result_double(context, p->rSum/(double)p->cnt);
! 87290: }
! 87291: }
! 87292: static void totalFinalize(sqlite3_context *context){
! 87293: SumCtx *p;
! 87294: p = sqlite3_aggregate_context(context, 0);
! 87295: /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
! 87296: sqlite3_result_double(context, p ? p->rSum : (double)0);
! 87297: }
! 87298:
! 87299: /*
! 87300: ** The following structure keeps track of state information for the
! 87301: ** count() aggregate function.
! 87302: */
! 87303: typedef struct CountCtx CountCtx;
! 87304: struct CountCtx {
! 87305: i64 n;
! 87306: };
! 87307:
! 87308: /*
! 87309: ** Routines to implement the count() aggregate function.
! 87310: */
! 87311: static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
! 87312: CountCtx *p;
! 87313: p = sqlite3_aggregate_context(context, sizeof(*p));
! 87314: if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
! 87315: p->n++;
! 87316: }
! 87317:
! 87318: #ifndef SQLITE_OMIT_DEPRECATED
! 87319: /* The sqlite3_aggregate_count() function is deprecated. But just to make
! 87320: ** sure it still operates correctly, verify that its count agrees with our
! 87321: ** internal count when using count(*) and when the total count can be
! 87322: ** expressed as a 32-bit integer. */
! 87323: assert( argc==1 || p==0 || p->n>0x7fffffff
! 87324: || p->n==sqlite3_aggregate_count(context) );
! 87325: #endif
! 87326: }
! 87327: static void countFinalize(sqlite3_context *context){
! 87328: CountCtx *p;
! 87329: p = sqlite3_aggregate_context(context, 0);
! 87330: sqlite3_result_int64(context, p ? p->n : 0);
! 87331: }
! 87332:
! 87333: /*
! 87334: ** Routines to implement min() and max() aggregate functions.
! 87335: */
! 87336: static void minmaxStep(
! 87337: sqlite3_context *context,
! 87338: int NotUsed,
! 87339: sqlite3_value **argv
! 87340: ){
! 87341: Mem *pArg = (Mem *)argv[0];
! 87342: Mem *pBest;
! 87343: UNUSED_PARAMETER(NotUsed);
! 87344:
! 87345: if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
! 87346: pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
! 87347: if( !pBest ) return;
! 87348:
! 87349: if( pBest->flags ){
! 87350: int max;
! 87351: int cmp;
! 87352: CollSeq *pColl = sqlite3GetFuncCollSeq(context);
! 87353: /* This step function is used for both the min() and max() aggregates,
! 87354: ** the only difference between the two being that the sense of the
! 87355: ** comparison is inverted. For the max() aggregate, the
! 87356: ** sqlite3_user_data() function returns (void *)-1. For min() it
! 87357: ** returns (void *)db, where db is the sqlite3* database pointer.
! 87358: ** Therefore the next statement sets variable 'max' to 1 for the max()
! 87359: ** aggregate, or 0 for min().
! 87360: */
! 87361: max = sqlite3_user_data(context)!=0;
! 87362: cmp = sqlite3MemCompare(pBest, pArg, pColl);
! 87363: if( (max && cmp<0) || (!max && cmp>0) ){
! 87364: sqlite3VdbeMemCopy(pBest, pArg);
! 87365: }
! 87366: }else{
! 87367: sqlite3VdbeMemCopy(pBest, pArg);
! 87368: }
! 87369: }
! 87370: static void minMaxFinalize(sqlite3_context *context){
! 87371: sqlite3_value *pRes;
! 87372: pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
! 87373: if( pRes ){
! 87374: if( ALWAYS(pRes->flags) ){
! 87375: sqlite3_result_value(context, pRes);
! 87376: }
! 87377: sqlite3VdbeMemRelease(pRes);
! 87378: }
! 87379: }
! 87380:
! 87381: /*
! 87382: ** group_concat(EXPR, ?SEPARATOR?)
! 87383: */
! 87384: static void groupConcatStep(
! 87385: sqlite3_context *context,
! 87386: int argc,
! 87387: sqlite3_value **argv
! 87388: ){
! 87389: const char *zVal;
! 87390: StrAccum *pAccum;
! 87391: const char *zSep;
! 87392: int nVal, nSep;
! 87393: assert( argc==1 || argc==2 );
! 87394: if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
! 87395: pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
! 87396:
! 87397: if( pAccum ){
! 87398: sqlite3 *db = sqlite3_context_db_handle(context);
! 87399: int firstTerm = pAccum->useMalloc==0;
! 87400: pAccum->useMalloc = 2;
! 87401: pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
! 87402: if( !firstTerm ){
! 87403: if( argc==2 ){
! 87404: zSep = (char*)sqlite3_value_text(argv[1]);
! 87405: nSep = sqlite3_value_bytes(argv[1]);
! 87406: }else{
! 87407: zSep = ",";
! 87408: nSep = 1;
! 87409: }
! 87410: sqlite3StrAccumAppend(pAccum, zSep, nSep);
! 87411: }
! 87412: zVal = (char*)sqlite3_value_text(argv[0]);
! 87413: nVal = sqlite3_value_bytes(argv[0]);
! 87414: sqlite3StrAccumAppend(pAccum, zVal, nVal);
! 87415: }
! 87416: }
! 87417: static void groupConcatFinalize(sqlite3_context *context){
! 87418: StrAccum *pAccum;
! 87419: pAccum = sqlite3_aggregate_context(context, 0);
! 87420: if( pAccum ){
! 87421: if( pAccum->tooBig ){
! 87422: sqlite3_result_error_toobig(context);
! 87423: }else if( pAccum->mallocFailed ){
! 87424: sqlite3_result_error_nomem(context);
! 87425: }else{
! 87426: sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
! 87427: sqlite3_free);
! 87428: }
! 87429: }
! 87430: }
! 87431:
! 87432: /*
! 87433: ** This routine does per-connection function registration. Most
! 87434: ** of the built-in functions above are part of the global function set.
! 87435: ** This routine only deals with those that are not global.
! 87436: */
! 87437: SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
! 87438: int rc = sqlite3_overload_function(db, "MATCH", 2);
! 87439: assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
! 87440: if( rc==SQLITE_NOMEM ){
! 87441: db->mallocFailed = 1;
! 87442: }
! 87443: }
! 87444:
! 87445: /*
! 87446: ** Set the LIKEOPT flag on the 2-argument function with the given name.
! 87447: */
! 87448: static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
! 87449: FuncDef *pDef;
! 87450: pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
! 87451: 2, SQLITE_UTF8, 0);
! 87452: if( ALWAYS(pDef) ){
! 87453: pDef->flags = flagVal;
! 87454: }
! 87455: }
! 87456:
! 87457: /*
! 87458: ** Register the built-in LIKE and GLOB functions. The caseSensitive
! 87459: ** parameter determines whether or not the LIKE operator is case
! 87460: ** sensitive. GLOB is always case sensitive.
! 87461: */
! 87462: SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
! 87463: struct compareInfo *pInfo;
! 87464: if( caseSensitive ){
! 87465: pInfo = (struct compareInfo*)&likeInfoAlt;
! 87466: }else{
! 87467: pInfo = (struct compareInfo*)&likeInfoNorm;
! 87468: }
! 87469: sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
! 87470: sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
! 87471: sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
! 87472: (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
! 87473: setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
! 87474: setLikeOptFlag(db, "like",
! 87475: caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
! 87476: }
! 87477:
! 87478: /*
! 87479: ** pExpr points to an expression which implements a function. If
! 87480: ** it is appropriate to apply the LIKE optimization to that function
! 87481: ** then set aWc[0] through aWc[2] to the wildcard characters and
! 87482: ** return TRUE. If the function is not a LIKE-style function then
! 87483: ** return FALSE.
! 87484: */
! 87485: SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
! 87486: FuncDef *pDef;
! 87487: if( pExpr->op!=TK_FUNCTION
! 87488: || !pExpr->x.pList
! 87489: || pExpr->x.pList->nExpr!=2
! 87490: ){
! 87491: return 0;
! 87492: }
! 87493: assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
! 87494: pDef = sqlite3FindFunction(db, pExpr->u.zToken,
! 87495: sqlite3Strlen30(pExpr->u.zToken),
! 87496: 2, SQLITE_UTF8, 0);
! 87497: if( NEVER(pDef==0) || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
! 87498: return 0;
! 87499: }
! 87500:
! 87501: /* The memcpy() statement assumes that the wildcard characters are
! 87502: ** the first three statements in the compareInfo structure. The
! 87503: ** asserts() that follow verify that assumption
! 87504: */
! 87505: memcpy(aWc, pDef->pUserData, 3);
! 87506: assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
! 87507: assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
! 87508: assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
! 87509: *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
! 87510: return 1;
! 87511: }
! 87512:
! 87513: /*
! 87514: ** All all of the FuncDef structures in the aBuiltinFunc[] array above
! 87515: ** to the global function hash table. This occurs at start-time (as
! 87516: ** a consequence of calling sqlite3_initialize()).
! 87517: **
! 87518: ** After this routine runs
! 87519: */
! 87520: SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
! 87521: /*
! 87522: ** The following array holds FuncDef structures for all of the functions
! 87523: ** defined in this file.
! 87524: **
! 87525: ** The array cannot be constant since changes are made to the
! 87526: ** FuncDef.pHash elements at start-time. The elements of this array
! 87527: ** are read-only after initialization is complete.
! 87528: */
! 87529: static SQLITE_WSD FuncDef aBuiltinFunc[] = {
! 87530: FUNCTION(ltrim, 1, 1, 0, trimFunc ),
! 87531: FUNCTION(ltrim, 2, 1, 0, trimFunc ),
! 87532: FUNCTION(rtrim, 1, 2, 0, trimFunc ),
! 87533: FUNCTION(rtrim, 2, 2, 0, trimFunc ),
! 87534: FUNCTION(trim, 1, 3, 0, trimFunc ),
! 87535: FUNCTION(trim, 2, 3, 0, trimFunc ),
! 87536: FUNCTION(min, -1, 0, 1, minmaxFunc ),
! 87537: FUNCTION(min, 0, 0, 1, 0 ),
! 87538: AGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize ),
! 87539: FUNCTION(max, -1, 1, 1, minmaxFunc ),
! 87540: FUNCTION(max, 0, 1, 1, 0 ),
! 87541: AGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize ),
! 87542: FUNCTION(typeof, 1, 0, 0, typeofFunc ),
! 87543: FUNCTION(length, 1, 0, 0, lengthFunc ),
! 87544: FUNCTION(substr, 2, 0, 0, substrFunc ),
! 87545: FUNCTION(substr, 3, 0, 0, substrFunc ),
! 87546: FUNCTION(abs, 1, 0, 0, absFunc ),
! 87547: #ifndef SQLITE_OMIT_FLOATING_POINT
! 87548: FUNCTION(round, 1, 0, 0, roundFunc ),
! 87549: FUNCTION(round, 2, 0, 0, roundFunc ),
! 87550: #endif
! 87551: FUNCTION(upper, 1, 0, 0, upperFunc ),
! 87552: FUNCTION(lower, 1, 0, 0, lowerFunc ),
! 87553: FUNCTION(coalesce, 1, 0, 0, 0 ),
! 87554: FUNCTION(coalesce, 0, 0, 0, 0 ),
! 87555: /* FUNCTION(coalesce, -1, 0, 0, ifnullFunc ), */
! 87556: {-1,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"coalesce",0,0},
! 87557: FUNCTION(hex, 1, 0, 0, hexFunc ),
! 87558: /* FUNCTION(ifnull, 2, 0, 0, ifnullFunc ), */
! 87559: {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0,0},
! 87560: FUNCTION(random, 0, 0, 0, randomFunc ),
! 87561: FUNCTION(randomblob, 1, 0, 0, randomBlob ),
! 87562: FUNCTION(nullif, 2, 0, 1, nullifFunc ),
! 87563: FUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
! 87564: FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
! 87565: FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
! 87566: #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
! 87567: FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ),
! 87568: FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
! 87569: #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
! 87570: FUNCTION(quote, 1, 0, 0, quoteFunc ),
! 87571: FUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
! 87572: FUNCTION(changes, 0, 0, 0, changes ),
! 87573: FUNCTION(total_changes, 0, 0, 0, total_changes ),
! 87574: FUNCTION(replace, 3, 0, 0, replaceFunc ),
! 87575: FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
! 87576: #ifdef SQLITE_SOUNDEX
! 87577: FUNCTION(soundex, 1, 0, 0, soundexFunc ),
! 87578: #endif
! 87579: #ifndef SQLITE_OMIT_LOAD_EXTENSION
! 87580: FUNCTION(load_extension, 1, 0, 0, loadExt ),
! 87581: FUNCTION(load_extension, 2, 0, 0, loadExt ),
! 87582: #endif
! 87583: AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ),
! 87584: AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ),
! 87585: AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ),
! 87586: /* AGGREGATE(count, 0, 0, 0, countStep, countFinalize ), */
! 87587: {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0},
! 87588: AGGREGATE(count, 1, 0, 0, countStep, countFinalize ),
! 87589: AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize),
! 87590: AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize),
! 87591:
! 87592: LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
! 87593: #ifdef SQLITE_CASE_SENSITIVE_LIKE
! 87594: LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
! 87595: LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
! 87596: #else
! 87597: LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
! 87598: LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
! 87599: #endif
! 87600: };
! 87601:
! 87602: int i;
! 87603: FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
! 87604: FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
! 87605:
! 87606: for(i=0; i<ArraySize(aBuiltinFunc); i++){
! 87607: sqlite3FuncDefInsert(pHash, &aFunc[i]);
! 87608: }
! 87609: sqlite3RegisterDateTimeFunctions();
! 87610: #ifndef SQLITE_OMIT_ALTERTABLE
! 87611: sqlite3AlterFunctions();
! 87612: #endif
! 87613: }
! 87614:
! 87615: /************** End of func.c ************************************************/
! 87616: /************** Begin file fkey.c ********************************************/
! 87617: /*
! 87618: **
! 87619: ** The author disclaims copyright to this source code. In place of
! 87620: ** a legal notice, here is a blessing:
! 87621: **
! 87622: ** May you do good and not evil.
! 87623: ** May you find forgiveness for yourself and forgive others.
! 87624: ** May you share freely, never taking more than you give.
! 87625: **
! 87626: *************************************************************************
! 87627: ** This file contains code used by the compiler to add foreign key
! 87628: ** support to compiled SQL statements.
! 87629: */
! 87630:
! 87631: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 87632: #ifndef SQLITE_OMIT_TRIGGER
! 87633:
! 87634: /*
! 87635: ** Deferred and Immediate FKs
! 87636: ** --------------------------
! 87637: **
! 87638: ** Foreign keys in SQLite come in two flavours: deferred and immediate.
! 87639: ** If an immediate foreign key constraint is violated, SQLITE_CONSTRAINT
! 87640: ** is returned and the current statement transaction rolled back. If a
! 87641: ** deferred foreign key constraint is violated, no action is taken
! 87642: ** immediately. However if the application attempts to commit the
! 87643: ** transaction before fixing the constraint violation, the attempt fails.
! 87644: **
! 87645: ** Deferred constraints are implemented using a simple counter associated
! 87646: ** with the database handle. The counter is set to zero each time a
! 87647: ** database transaction is opened. Each time a statement is executed
! 87648: ** that causes a foreign key violation, the counter is incremented. Each
! 87649: ** time a statement is executed that removes an existing violation from
! 87650: ** the database, the counter is decremented. When the transaction is
! 87651: ** committed, the commit fails if the current value of the counter is
! 87652: ** greater than zero. This scheme has two big drawbacks:
! 87653: **
! 87654: ** * When a commit fails due to a deferred foreign key constraint,
! 87655: ** there is no way to tell which foreign constraint is not satisfied,
! 87656: ** or which row it is not satisfied for.
! 87657: **
! 87658: ** * If the database contains foreign key violations when the
! 87659: ** transaction is opened, this may cause the mechanism to malfunction.
! 87660: **
! 87661: ** Despite these problems, this approach is adopted as it seems simpler
! 87662: ** than the alternatives.
! 87663: **
! 87664: ** INSERT operations:
! 87665: **
! 87666: ** I.1) For each FK for which the table is the child table, search
! 87667: ** the parent table for a match. If none is found increment the
! 87668: ** constraint counter.
! 87669: **
! 87670: ** I.2) For each FK for which the table is the parent table,
! 87671: ** search the child table for rows that correspond to the new
! 87672: ** row in the parent table. Decrement the counter for each row
! 87673: ** found (as the constraint is now satisfied).
! 87674: **
! 87675: ** DELETE operations:
! 87676: **
! 87677: ** D.1) For each FK for which the table is the child table,
! 87678: ** search the parent table for a row that corresponds to the
! 87679: ** deleted row in the child table. If such a row is not found,
! 87680: ** decrement the counter.
! 87681: **
! 87682: ** D.2) For each FK for which the table is the parent table, search
! 87683: ** the child table for rows that correspond to the deleted row
! 87684: ** in the parent table. For each found increment the counter.
! 87685: **
! 87686: ** UPDATE operations:
! 87687: **
! 87688: ** An UPDATE command requires that all 4 steps above are taken, but only
! 87689: ** for FK constraints for which the affected columns are actually
! 87690: ** modified (values must be compared at runtime).
! 87691: **
! 87692: ** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
! 87693: ** This simplifies the implementation a bit.
! 87694: **
! 87695: ** For the purposes of immediate FK constraints, the OR REPLACE conflict
! 87696: ** resolution is considered to delete rows before the new row is inserted.
! 87697: ** If a delete caused by OR REPLACE violates an FK constraint, an exception
! 87698: ** is thrown, even if the FK constraint would be satisfied after the new
! 87699: ** row is inserted.
! 87700: **
! 87701: ** Immediate constraints are usually handled similarly. The only difference
! 87702: ** is that the counter used is stored as part of each individual statement
! 87703: ** object (struct Vdbe). If, after the statement has run, its immediate
! 87704: ** constraint counter is greater than zero, it returns SQLITE_CONSTRAINT
! 87705: ** and the statement transaction is rolled back. An exception is an INSERT
! 87706: ** statement that inserts a single row only (no triggers). In this case,
! 87707: ** instead of using a counter, an exception is thrown immediately if the
! 87708: ** INSERT violates a foreign key constraint. This is necessary as such
! 87709: ** an INSERT does not open a statement transaction.
! 87710: **
! 87711: ** TODO: How should dropping a table be handled? How should renaming a
! 87712: ** table be handled?
! 87713: **
! 87714: **
! 87715: ** Query API Notes
! 87716: ** ---------------
! 87717: **
! 87718: ** Before coding an UPDATE or DELETE row operation, the code-generator
! 87719: ** for those two operations needs to know whether or not the operation
! 87720: ** requires any FK processing and, if so, which columns of the original
! 87721: ** row are required by the FK processing VDBE code (i.e. if FKs were
! 87722: ** implemented using triggers, which of the old.* columns would be
! 87723: ** accessed). No information is required by the code-generator before
! 87724: ** coding an INSERT operation. The functions used by the UPDATE/DELETE
! 87725: ** generation code to query for this information are:
! 87726: **
! 87727: ** sqlite3FkRequired() - Test to see if FK processing is required.
! 87728: ** sqlite3FkOldmask() - Query for the set of required old.* columns.
! 87729: **
! 87730: **
! 87731: ** Externally accessible module functions
! 87732: ** --------------------------------------
! 87733: **
! 87734: ** sqlite3FkCheck() - Check for foreign key violations.
! 87735: ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
! 87736: ** sqlite3FkDelete() - Delete an FKey structure.
! 87737: */
! 87738:
! 87739: /*
! 87740: ** VDBE Calling Convention
! 87741: ** -----------------------
! 87742: **
! 87743: ** Example:
! 87744: **
! 87745: ** For the following INSERT statement:
! 87746: **
! 87747: ** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
! 87748: ** INSERT INTO t1 VALUES(1, 2, 3.1);
! 87749: **
! 87750: ** Register (x): 2 (type integer)
! 87751: ** Register (x+1): 1 (type integer)
! 87752: ** Register (x+2): NULL (type NULL)
! 87753: ** Register (x+3): 3.1 (type real)
! 87754: */
! 87755:
! 87756: /*
! 87757: ** A foreign key constraint requires that the key columns in the parent
! 87758: ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
! 87759: ** Given that pParent is the parent table for foreign key constraint pFKey,
! 87760: ** search the schema a unique index on the parent key columns.
! 87761: **
! 87762: ** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
! 87763: ** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
! 87764: ** is set to point to the unique index.
! 87765: **
! 87766: ** If the parent key consists of a single column (the foreign key constraint
! 87767: ** is not a composite foreign key), output variable *paiCol is set to NULL.
! 87768: ** Otherwise, it is set to point to an allocated array of size N, where
! 87769: ** N is the number of columns in the parent key. The first element of the
! 87770: ** array is the index of the child table column that is mapped by the FK
! 87771: ** constraint to the parent table column stored in the left-most column
! 87772: ** of index *ppIdx. The second element of the array is the index of the
! 87773: ** child table column that corresponds to the second left-most column of
! 87774: ** *ppIdx, and so on.
! 87775: **
! 87776: ** If the required index cannot be found, either because:
! 87777: **
! 87778: ** 1) The named parent key columns do not exist, or
! 87779: **
! 87780: ** 2) The named parent key columns do exist, but are not subject to a
! 87781: ** UNIQUE or PRIMARY KEY constraint, or
! 87782: **
! 87783: ** 3) No parent key columns were provided explicitly as part of the
! 87784: ** foreign key definition, and the parent table does not have a
! 87785: ** PRIMARY KEY, or
! 87786: **
! 87787: ** 4) No parent key columns were provided explicitly as part of the
! 87788: ** foreign key definition, and the PRIMARY KEY of the parent table
! 87789: ** consists of a a different number of columns to the child key in
! 87790: ** the child table.
! 87791: **
! 87792: ** then non-zero is returned, and a "foreign key mismatch" error loaded
! 87793: ** into pParse. If an OOM error occurs, non-zero is returned and the
! 87794: ** pParse->db->mallocFailed flag is set.
! 87795: */
! 87796: static int locateFkeyIndex(
! 87797: Parse *pParse, /* Parse context to store any error in */
! 87798: Table *pParent, /* Parent table of FK constraint pFKey */
! 87799: FKey *pFKey, /* Foreign key to find index for */
! 87800: Index **ppIdx, /* OUT: Unique index on parent table */
! 87801: int **paiCol /* OUT: Map of index columns in pFKey */
! 87802: ){
! 87803: Index *pIdx = 0; /* Value to return via *ppIdx */
! 87804: int *aiCol = 0; /* Value to return via *paiCol */
! 87805: int nCol = pFKey->nCol; /* Number of columns in parent key */
! 87806: char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
! 87807:
! 87808: /* The caller is responsible for zeroing output parameters. */
! 87809: assert( ppIdx && *ppIdx==0 );
! 87810: assert( !paiCol || *paiCol==0 );
! 87811: assert( pParse );
! 87812:
! 87813: /* If this is a non-composite (single column) foreign key, check if it
! 87814: ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
! 87815: ** and *paiCol set to zero and return early.
! 87816: **
! 87817: ** Otherwise, for a composite foreign key (more than one column), allocate
! 87818: ** space for the aiCol array (returned via output parameter *paiCol).
! 87819: ** Non-composite foreign keys do not require the aiCol array.
! 87820: */
! 87821: if( nCol==1 ){
! 87822: /* The FK maps to the IPK if any of the following are true:
! 87823: **
! 87824: ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
! 87825: ** mapped to the primary key of table pParent, or
! 87826: ** 2) The FK is explicitly mapped to a column declared as INTEGER
! 87827: ** PRIMARY KEY.
! 87828: */
! 87829: if( pParent->iPKey>=0 ){
! 87830: if( !zKey ) return 0;
! 87831: if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
! 87832: }
! 87833: }else if( paiCol ){
! 87834: assert( nCol>1 );
! 87835: aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
! 87836: if( !aiCol ) return 1;
! 87837: *paiCol = aiCol;
! 87838: }
! 87839:
! 87840: for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
! 87841: if( pIdx->nColumn==nCol && pIdx->onError!=OE_None ){
! 87842: /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
! 87843: ** of columns. If each indexed column corresponds to a foreign key
! 87844: ** column of pFKey, then this index is a winner. */
! 87845:
! 87846: if( zKey==0 ){
! 87847: /* If zKey is NULL, then this foreign key is implicitly mapped to
! 87848: ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
! 87849: ** identified by the test (Index.autoIndex==2). */
! 87850: if( pIdx->autoIndex==2 ){
! 87851: if( aiCol ){
! 87852: int i;
! 87853: for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
! 87854: }
! 87855: break;
! 87856: }
! 87857: }else{
! 87858: /* If zKey is non-NULL, then this foreign key was declared to
! 87859: ** map to an explicit list of columns in table pParent. Check if this
! 87860: ** index matches those columns. Also, check that the index uses
! 87861: ** the default collation sequences for each column. */
! 87862: int i, j;
! 87863: for(i=0; i<nCol; i++){
! 87864: int iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
! 87865: char *zDfltColl; /* Def. collation for column */
! 87866: char *zIdxCol; /* Name of indexed column */
! 87867:
! 87868: /* If the index uses a collation sequence that is different from
! 87869: ** the default collation sequence for the column, this index is
! 87870: ** unusable. Bail out early in this case. */
! 87871: zDfltColl = pParent->aCol[iCol].zColl;
! 87872: if( !zDfltColl ){
! 87873: zDfltColl = "BINARY";
! 87874: }
! 87875: if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
! 87876:
! 87877: zIdxCol = pParent->aCol[iCol].zName;
! 87878: for(j=0; j<nCol; j++){
! 87879: if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
! 87880: if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
! 87881: break;
! 87882: }
! 87883: }
! 87884: if( j==nCol ) break;
! 87885: }
! 87886: if( i==nCol ) break; /* pIdx is usable */
! 87887: }
! 87888: }
! 87889: }
! 87890:
! 87891: if( !pIdx ){
! 87892: if( !pParse->disableTriggers ){
! 87893: sqlite3ErrorMsg(pParse, "foreign key mismatch");
! 87894: }
! 87895: sqlite3DbFree(pParse->db, aiCol);
! 87896: return 1;
! 87897: }
! 87898:
! 87899: *ppIdx = pIdx;
! 87900: return 0;
! 87901: }
! 87902:
! 87903: /*
! 87904: ** This function is called when a row is inserted into or deleted from the
! 87905: ** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
! 87906: ** on the child table of pFKey, this function is invoked twice for each row
! 87907: ** affected - once to "delete" the old row, and then again to "insert" the
! 87908: ** new row.
! 87909: **
! 87910: ** Each time it is called, this function generates VDBE code to locate the
! 87911: ** row in the parent table that corresponds to the row being inserted into
! 87912: ** or deleted from the child table. If the parent row can be found, no
! 87913: ** special action is taken. Otherwise, if the parent row can *not* be
! 87914: ** found in the parent table:
! 87915: **
! 87916: ** Operation | FK type | Action taken
! 87917: ** --------------------------------------------------------------------------
! 87918: ** INSERT immediate Increment the "immediate constraint counter".
! 87919: **
! 87920: ** DELETE immediate Decrement the "immediate constraint counter".
! 87921: **
! 87922: ** INSERT deferred Increment the "deferred constraint counter".
! 87923: **
! 87924: ** DELETE deferred Decrement the "deferred constraint counter".
! 87925: **
! 87926: ** These operations are identified in the comment at the top of this file
! 87927: ** (fkey.c) as "I.1" and "D.1".
! 87928: */
! 87929: static void fkLookupParent(
! 87930: Parse *pParse, /* Parse context */
! 87931: int iDb, /* Index of database housing pTab */
! 87932: Table *pTab, /* Parent table of FK pFKey */
! 87933: Index *pIdx, /* Unique index on parent key columns in pTab */
! 87934: FKey *pFKey, /* Foreign key constraint */
! 87935: int *aiCol, /* Map from parent key columns to child table columns */
! 87936: int regData, /* Address of array containing child table row */
! 87937: int nIncr, /* Increment constraint counter by this */
! 87938: int isIgnore /* If true, pretend pTab contains all NULL values */
! 87939: ){
! 87940: int i; /* Iterator variable */
! 87941: Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
! 87942: int iCur = pParse->nTab - 1; /* Cursor number to use */
! 87943: int iOk = sqlite3VdbeMakeLabel(v); /* jump here if parent key found */
! 87944:
! 87945: /* If nIncr is less than zero, then check at runtime if there are any
! 87946: ** outstanding constraints to resolve. If there are not, there is no need
! 87947: ** to check if deleting this row resolves any outstanding violations.
! 87948: **
! 87949: ** Check if any of the key columns in the child table row are NULL. If
! 87950: ** any are, then the constraint is considered satisfied. No need to
! 87951: ** search for a matching row in the parent table. */
! 87952: if( nIncr<0 ){
! 87953: sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
! 87954: }
! 87955: for(i=0; i<pFKey->nCol; i++){
! 87956: int iReg = aiCol[i] + regData + 1;
! 87957: sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk);
! 87958: }
! 87959:
! 87960: if( isIgnore==0 ){
! 87961: if( pIdx==0 ){
! 87962: /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
! 87963: ** column of the parent table (table pTab). */
! 87964: int iMustBeInt; /* Address of MustBeInt instruction */
! 87965: int regTemp = sqlite3GetTempReg(pParse);
! 87966:
! 87967: /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
! 87968: ** apply the affinity of the parent key). If this fails, then there
! 87969: ** is no matching parent key. Before using MustBeInt, make a copy of
! 87970: ** the value. Otherwise, the value inserted into the child key column
! 87971: ** will have INTEGER affinity applied to it, which may not be correct. */
! 87972: sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
! 87973: iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
! 87974:
! 87975: /* If the parent table is the same as the child table, and we are about
! 87976: ** to increment the constraint-counter (i.e. this is an INSERT operation),
! 87977: ** then check if the row being inserted matches itself. If so, do not
! 87978: ** increment the constraint-counter. */
! 87979: if( pTab==pFKey->pFrom && nIncr==1 ){
! 87980: sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp);
! 87981: }
! 87982:
! 87983: sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
! 87984: sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp);
! 87985: sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
! 87986: sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
! 87987: sqlite3VdbeJumpHere(v, iMustBeInt);
! 87988: sqlite3ReleaseTempReg(pParse, regTemp);
! 87989: }else{
! 87990: int nCol = pFKey->nCol;
! 87991: int regTemp = sqlite3GetTempRange(pParse, nCol);
! 87992: int regRec = sqlite3GetTempReg(pParse);
! 87993: KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
! 87994:
! 87995: sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
! 87996: sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
! 87997: for(i=0; i<nCol; i++){
! 87998: sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
! 87999: }
! 88000:
! 88001: /* If the parent table is the same as the child table, and we are about
! 88002: ** to increment the constraint-counter (i.e. this is an INSERT operation),
! 88003: ** then check if the row being inserted matches itself. If so, do not
! 88004: ** increment the constraint-counter.
! 88005: **
! 88006: ** If any of the parent-key values are NULL, then the row cannot match
! 88007: ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
! 88008: ** of the parent-key values are NULL (at this point it is known that
! 88009: ** none of the child key values are).
! 88010: */
! 88011: if( pTab==pFKey->pFrom && nIncr==1 ){
! 88012: int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
! 88013: for(i=0; i<nCol; i++){
! 88014: int iChild = aiCol[i]+1+regData;
! 88015: int iParent = pIdx->aiColumn[i]+1+regData;
! 88016: assert( aiCol[i]!=pTab->iPKey );
! 88017: if( pIdx->aiColumn[i]==pTab->iPKey ){
! 88018: /* The parent key is a composite key that includes the IPK column */
! 88019: iParent = regData;
! 88020: }
! 88021: sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
! 88022: sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
! 88023: }
! 88024: sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
! 88025: }
! 88026:
! 88027: sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
! 88028: sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
! 88029: sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0);
! 88030:
! 88031: sqlite3ReleaseTempReg(pParse, regRec);
! 88032: sqlite3ReleaseTempRange(pParse, regTemp, nCol);
! 88033: }
! 88034: }
! 88035:
! 88036: if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
! 88037: /* Special case: If this is an INSERT statement that will insert exactly
! 88038: ** one row into the table, raise a constraint immediately instead of
! 88039: ** incrementing a counter. This is necessary as the VM code is being
! 88040: ** generated for will not open a statement transaction. */
! 88041: assert( nIncr==1 );
! 88042: sqlite3HaltConstraint(
! 88043: pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
! 88044: );
! 88045: }else{
! 88046: if( nIncr>0 && pFKey->isDeferred==0 ){
! 88047: sqlite3ParseToplevel(pParse)->mayAbort = 1;
! 88048: }
! 88049: sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
! 88050: }
! 88051:
! 88052: sqlite3VdbeResolveLabel(v, iOk);
! 88053: sqlite3VdbeAddOp1(v, OP_Close, iCur);
! 88054: }
! 88055:
! 88056: /*
! 88057: ** This function is called to generate code executed when a row is deleted
! 88058: ** from the parent table of foreign key constraint pFKey and, if pFKey is
! 88059: ** deferred, when a row is inserted into the same table. When generating
! 88060: ** code for an SQL UPDATE operation, this function may be called twice -
! 88061: ** once to "delete" the old row and once to "insert" the new row.
! 88062: **
! 88063: ** The code generated by this function scans through the rows in the child
! 88064: ** table that correspond to the parent table row being deleted or inserted.
! 88065: ** For each child row found, one of the following actions is taken:
! 88066: **
! 88067: ** Operation | FK type | Action taken
! 88068: ** --------------------------------------------------------------------------
! 88069: ** DELETE immediate Increment the "immediate constraint counter".
! 88070: ** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
! 88071: ** throw a "foreign key constraint failed" exception.
! 88072: **
! 88073: ** INSERT immediate Decrement the "immediate constraint counter".
! 88074: **
! 88075: ** DELETE deferred Increment the "deferred constraint counter".
! 88076: ** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
! 88077: ** throw a "foreign key constraint failed" exception.
! 88078: **
! 88079: ** INSERT deferred Decrement the "deferred constraint counter".
! 88080: **
! 88081: ** These operations are identified in the comment at the top of this file
! 88082: ** (fkey.c) as "I.2" and "D.2".
! 88083: */
! 88084: static void fkScanChildren(
! 88085: Parse *pParse, /* Parse context */
! 88086: SrcList *pSrc, /* SrcList containing the table to scan */
! 88087: Table *pTab,
! 88088: Index *pIdx, /* Foreign key index */
! 88089: FKey *pFKey, /* Foreign key relationship */
! 88090: int *aiCol, /* Map from pIdx cols to child table cols */
! 88091: int regData, /* Referenced table data starts here */
! 88092: int nIncr /* Amount to increment deferred counter by */
! 88093: ){
! 88094: sqlite3 *db = pParse->db; /* Database handle */
! 88095: int i; /* Iterator variable */
! 88096: Expr *pWhere = 0; /* WHERE clause to scan with */
! 88097: NameContext sNameContext; /* Context used to resolve WHERE clause */
! 88098: WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */
! 88099: int iFkIfZero = 0; /* Address of OP_FkIfZero */
! 88100: Vdbe *v = sqlite3GetVdbe(pParse);
! 88101:
! 88102: assert( !pIdx || pIdx->pTable==pTab );
! 88103:
! 88104: if( nIncr<0 ){
! 88105: iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
! 88106: }
! 88107:
! 88108: /* Create an Expr object representing an SQL expression like:
! 88109: **
! 88110: ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
! 88111: **
! 88112: ** The collation sequence used for the comparison should be that of
! 88113: ** the parent key columns. The affinity of the parent key column should
! 88114: ** be applied to each child key value before the comparison takes place.
! 88115: */
! 88116: for(i=0; i<pFKey->nCol; i++){
! 88117: Expr *pLeft; /* Value from parent table row */
! 88118: Expr *pRight; /* Column ref to child table */
! 88119: Expr *pEq; /* Expression (pLeft = pRight) */
! 88120: int iCol; /* Index of column in child table */
! 88121: const char *zCol; /* Name of column in child table */
! 88122:
! 88123: pLeft = sqlite3Expr(db, TK_REGISTER, 0);
! 88124: if( pLeft ){
! 88125: /* Set the collation sequence and affinity of the LHS of each TK_EQ
! 88126: ** expression to the parent key column defaults. */
! 88127: if( pIdx ){
! 88128: Column *pCol;
! 88129: iCol = pIdx->aiColumn[i];
! 88130: pCol = &pTab->aCol[iCol];
! 88131: if( pTab->iPKey==iCol ) iCol = -1;
! 88132: pLeft->iTable = regData+iCol+1;
! 88133: pLeft->affinity = pCol->affinity;
! 88134: pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl);
! 88135: }else{
! 88136: pLeft->iTable = regData;
! 88137: pLeft->affinity = SQLITE_AFF_INTEGER;
! 88138: }
! 88139: }
! 88140: iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
! 88141: assert( iCol>=0 );
! 88142: zCol = pFKey->pFrom->aCol[iCol].zName;
! 88143: pRight = sqlite3Expr(db, TK_ID, zCol);
! 88144: pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
! 88145: pWhere = sqlite3ExprAnd(db, pWhere, pEq);
! 88146: }
! 88147:
! 88148: /* If the child table is the same as the parent table, and this scan
! 88149: ** is taking place as part of a DELETE operation (operation D.2), omit the
! 88150: ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE
! 88151: ** clause, where $rowid is the rowid of the row being deleted. */
! 88152: if( pTab==pFKey->pFrom && nIncr>0 ){
! 88153: Expr *pEq; /* Expression (pLeft = pRight) */
! 88154: Expr *pLeft; /* Value from parent table row */
! 88155: Expr *pRight; /* Column ref to child table */
! 88156: pLeft = sqlite3Expr(db, TK_REGISTER, 0);
! 88157: pRight = sqlite3Expr(db, TK_COLUMN, 0);
! 88158: if( pLeft && pRight ){
! 88159: pLeft->iTable = regData;
! 88160: pLeft->affinity = SQLITE_AFF_INTEGER;
! 88161: pRight->iTable = pSrc->a[0].iCursor;
! 88162: pRight->iColumn = -1;
! 88163: }
! 88164: pEq = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
! 88165: pWhere = sqlite3ExprAnd(db, pWhere, pEq);
! 88166: }
! 88167:
! 88168: /* Resolve the references in the WHERE clause. */
! 88169: memset(&sNameContext, 0, sizeof(NameContext));
! 88170: sNameContext.pSrcList = pSrc;
! 88171: sNameContext.pParse = pParse;
! 88172: sqlite3ResolveExprNames(&sNameContext, pWhere);
! 88173:
! 88174: /* Create VDBE to loop through the entries in pSrc that match the WHERE
! 88175: ** clause. If the constraint is not deferred, throw an exception for
! 88176: ** each row found. Otherwise, for deferred constraints, increment the
! 88177: ** deferred constraint counter by nIncr for each row selected. */
! 88178: pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0);
! 88179: if( nIncr>0 && pFKey->isDeferred==0 ){
! 88180: sqlite3ParseToplevel(pParse)->mayAbort = 1;
! 88181: }
! 88182: sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
! 88183: if( pWInfo ){
! 88184: sqlite3WhereEnd(pWInfo);
! 88185: }
! 88186:
! 88187: /* Clean up the WHERE clause constructed above. */
! 88188: sqlite3ExprDelete(db, pWhere);
! 88189: if( iFkIfZero ){
! 88190: sqlite3VdbeJumpHere(v, iFkIfZero);
! 88191: }
! 88192: }
! 88193:
! 88194: /*
! 88195: ** This function returns a pointer to the head of a linked list of FK
! 88196: ** constraints for which table pTab is the parent table. For example,
! 88197: ** given the following schema:
! 88198: **
! 88199: ** CREATE TABLE t1(a PRIMARY KEY);
! 88200: ** CREATE TABLE t2(b REFERENCES t1(a);
! 88201: **
! 88202: ** Calling this function with table "t1" as an argument returns a pointer
! 88203: ** to the FKey structure representing the foreign key constraint on table
! 88204: ** "t2". Calling this function with "t2" as the argument would return a
! 88205: ** NULL pointer (as there are no FK constraints for which t2 is the parent
! 88206: ** table).
! 88207: */
! 88208: SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
! 88209: int nName = sqlite3Strlen30(pTab->zName);
! 88210: return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
! 88211: }
! 88212:
! 88213: /*
! 88214: ** The second argument is a Trigger structure allocated by the
! 88215: ** fkActionTrigger() routine. This function deletes the Trigger structure
! 88216: ** and all of its sub-components.
! 88217: **
! 88218: ** The Trigger structure or any of its sub-components may be allocated from
! 88219: ** the lookaside buffer belonging to database handle dbMem.
! 88220: */
! 88221: static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
! 88222: if( p ){
! 88223: TriggerStep *pStep = p->step_list;
! 88224: sqlite3ExprDelete(dbMem, pStep->pWhere);
! 88225: sqlite3ExprListDelete(dbMem, pStep->pExprList);
! 88226: sqlite3SelectDelete(dbMem, pStep->pSelect);
! 88227: sqlite3ExprDelete(dbMem, p->pWhen);
! 88228: sqlite3DbFree(dbMem, p);
! 88229: }
! 88230: }
! 88231:
! 88232: /*
! 88233: ** This function is called to generate code that runs when table pTab is
! 88234: ** being dropped from the database. The SrcList passed as the second argument
! 88235: ** to this function contains a single entry guaranteed to resolve to
! 88236: ** table pTab.
! 88237: **
! 88238: ** Normally, no code is required. However, if either
! 88239: **
! 88240: ** (a) The table is the parent table of a FK constraint, or
! 88241: ** (b) The table is the child table of a deferred FK constraint and it is
! 88242: ** determined at runtime that there are outstanding deferred FK
! 88243: ** constraint violations in the database,
! 88244: **
! 88245: ** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
! 88246: ** the table from the database. Triggers are disabled while running this
! 88247: ** DELETE, but foreign key actions are not.
! 88248: */
! 88249: SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
! 88250: sqlite3 *db = pParse->db;
! 88251: if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
! 88252: int iSkip = 0;
! 88253: Vdbe *v = sqlite3GetVdbe(pParse);
! 88254:
! 88255: assert( v ); /* VDBE has already been allocated */
! 88256: if( sqlite3FkReferences(pTab)==0 ){
! 88257: /* Search for a deferred foreign key constraint for which this table
! 88258: ** is the child table. If one cannot be found, return without
! 88259: ** generating any VDBE code. If one can be found, then jump over
! 88260: ** the entire DELETE if there are no outstanding deferred constraints
! 88261: ** when this statement is run. */
! 88262: FKey *p;
! 88263: for(p=pTab->pFKey; p; p=p->pNextFrom){
! 88264: if( p->isDeferred ) break;
! 88265: }
! 88266: if( !p ) return;
! 88267: iSkip = sqlite3VdbeMakeLabel(v);
! 88268: sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip);
! 88269: }
! 88270:
! 88271: pParse->disableTriggers = 1;
! 88272: sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
! 88273: pParse->disableTriggers = 0;
! 88274:
! 88275: /* If the DELETE has generated immediate foreign key constraint
! 88276: ** violations, halt the VDBE and return an error at this point, before
! 88277: ** any modifications to the schema are made. This is because statement
! 88278: ** transactions are not able to rollback schema changes. */
! 88279: sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
! 88280: sqlite3HaltConstraint(
! 88281: pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
! 88282: );
! 88283:
! 88284: if( iSkip ){
! 88285: sqlite3VdbeResolveLabel(v, iSkip);
! 88286: }
! 88287: }
! 88288: }
! 88289:
! 88290: /*
! 88291: ** This function is called when inserting, deleting or updating a row of
! 88292: ** table pTab to generate VDBE code to perform foreign key constraint
! 88293: ** processing for the operation.
! 88294: **
! 88295: ** For a DELETE operation, parameter regOld is passed the index of the
! 88296: ** first register in an array of (pTab->nCol+1) registers containing the
! 88297: ** rowid of the row being deleted, followed by each of the column values
! 88298: ** of the row being deleted, from left to right. Parameter regNew is passed
! 88299: ** zero in this case.
! 88300: **
! 88301: ** For an INSERT operation, regOld is passed zero and regNew is passed the
! 88302: ** first register of an array of (pTab->nCol+1) registers containing the new
! 88303: ** row data.
! 88304: **
! 88305: ** For an UPDATE operation, this function is called twice. Once before
! 88306: ** the original record is deleted from the table using the calling convention
! 88307: ** described for DELETE. Then again after the original record is deleted
! 88308: ** but before the new record is inserted using the INSERT convention.
! 88309: */
! 88310: SQLITE_PRIVATE void sqlite3FkCheck(
! 88311: Parse *pParse, /* Parse context */
! 88312: Table *pTab, /* Row is being deleted from this table */
! 88313: int regOld, /* Previous row data is stored here */
! 88314: int regNew /* New row data is stored here */
! 88315: ){
! 88316: sqlite3 *db = pParse->db; /* Database handle */
! 88317: FKey *pFKey; /* Used to iterate through FKs */
! 88318: int iDb; /* Index of database containing pTab */
! 88319: const char *zDb; /* Name of database containing pTab */
! 88320: int isIgnoreErrors = pParse->disableTriggers;
! 88321:
! 88322: /* Exactly one of regOld and regNew should be non-zero. */
! 88323: assert( (regOld==0)!=(regNew==0) );
! 88324:
! 88325: /* If foreign-keys are disabled, this function is a no-op. */
! 88326: if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
! 88327:
! 88328: iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 88329: zDb = db->aDb[iDb].zName;
! 88330:
! 88331: /* Loop through all the foreign key constraints for which pTab is the
! 88332: ** child table (the table that the foreign key definition is part of). */
! 88333: for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
! 88334: Table *pTo; /* Parent table of foreign key pFKey */
! 88335: Index *pIdx = 0; /* Index on key columns in pTo */
! 88336: int *aiFree = 0;
! 88337: int *aiCol;
! 88338: int iCol;
! 88339: int i;
! 88340: int isIgnore = 0;
! 88341:
! 88342: /* Find the parent table of this foreign key. Also find a unique index
! 88343: ** on the parent key columns in the parent table. If either of these
! 88344: ** schema items cannot be located, set an error in pParse and return
! 88345: ** early. */
! 88346: if( pParse->disableTriggers ){
! 88347: pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
! 88348: }else{
! 88349: pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
! 88350: }
! 88351: if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
! 88352: assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
! 88353: if( !isIgnoreErrors || db->mallocFailed ) return;
! 88354: if( pTo==0 ){
! 88355: /* If isIgnoreErrors is true, then a table is being dropped. In this
! 88356: ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
! 88357: ** before actually dropping it in order to check FK constraints.
! 88358: ** If the parent table of an FK constraint on the current table is
! 88359: ** missing, behave as if it is empty. i.e. decrement the relevant
! 88360: ** FK counter for each row of the current table with non-NULL keys.
! 88361: */
! 88362: Vdbe *v = sqlite3GetVdbe(pParse);
! 88363: int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
! 88364: for(i=0; i<pFKey->nCol; i++){
! 88365: int iReg = pFKey->aCol[i].iFrom + regOld + 1;
! 88366: sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
! 88367: }
! 88368: sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
! 88369: }
! 88370: continue;
! 88371: }
! 88372: assert( pFKey->nCol==1 || (aiFree && pIdx) );
! 88373:
! 88374: if( aiFree ){
! 88375: aiCol = aiFree;
! 88376: }else{
! 88377: iCol = pFKey->aCol[0].iFrom;
! 88378: aiCol = &iCol;
! 88379: }
! 88380: for(i=0; i<pFKey->nCol; i++){
! 88381: if( aiCol[i]==pTab->iPKey ){
! 88382: aiCol[i] = -1;
! 88383: }
! 88384: #ifndef SQLITE_OMIT_AUTHORIZATION
! 88385: /* Request permission to read the parent key columns. If the
! 88386: ** authorization callback returns SQLITE_IGNORE, behave as if any
! 88387: ** values read from the parent table are NULL. */
! 88388: if( db->xAuth ){
! 88389: int rcauth;
! 88390: char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
! 88391: rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
! 88392: isIgnore = (rcauth==SQLITE_IGNORE);
! 88393: }
! 88394: #endif
! 88395: }
! 88396:
! 88397: /* Take a shared-cache advisory read-lock on the parent table. Allocate
! 88398: ** a cursor to use to search the unique index on the parent key columns
! 88399: ** in the parent table. */
! 88400: sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
! 88401: pParse->nTab++;
! 88402:
! 88403: if( regOld!=0 ){
! 88404: /* A row is being removed from the child table. Search for the parent.
! 88405: ** If the parent does not exist, removing the child row resolves an
! 88406: ** outstanding foreign key constraint violation. */
! 88407: fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore);
! 88408: }
! 88409: if( regNew!=0 ){
! 88410: /* A row is being added to the child table. If a parent row cannot
! 88411: ** be found, adding the child row has violated the FK constraint. */
! 88412: fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
! 88413: }
! 88414:
! 88415: sqlite3DbFree(db, aiFree);
! 88416: }
! 88417:
! 88418: /* Loop through all the foreign key constraints that refer to this table */
! 88419: for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
! 88420: Index *pIdx = 0; /* Foreign key index for pFKey */
! 88421: SrcList *pSrc;
! 88422: int *aiCol = 0;
! 88423:
! 88424: if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
! 88425: assert( regOld==0 && regNew!=0 );
! 88426: /* Inserting a single row into a parent table cannot cause an immediate
! 88427: ** foreign key violation. So do nothing in this case. */
! 88428: continue;
! 88429: }
! 88430:
! 88431: if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
! 88432: if( !isIgnoreErrors || db->mallocFailed ) return;
! 88433: continue;
! 88434: }
! 88435: assert( aiCol || pFKey->nCol==1 );
! 88436:
! 88437: /* Create a SrcList structure containing a single table (the table
! 88438: ** the foreign key that refers to this table is attached to). This
! 88439: ** is required for the sqlite3WhereXXX() interface. */
! 88440: pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
! 88441: if( pSrc ){
! 88442: struct SrcList_item *pItem = pSrc->a;
! 88443: pItem->pTab = pFKey->pFrom;
! 88444: pItem->zName = pFKey->pFrom->zName;
! 88445: pItem->pTab->nRef++;
! 88446: pItem->iCursor = pParse->nTab++;
! 88447:
! 88448: if( regNew!=0 ){
! 88449: fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
! 88450: }
! 88451: if( regOld!=0 ){
! 88452: /* If there is a RESTRICT action configured for the current operation
! 88453: ** on the parent table of this FK, then throw an exception
! 88454: ** immediately if the FK constraint is violated, even if this is a
! 88455: ** deferred trigger. That's what RESTRICT means. To defer checking
! 88456: ** the constraint, the FK should specify NO ACTION (represented
! 88457: ** using OE_None). NO ACTION is the default. */
! 88458: fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
! 88459: }
! 88460: pItem->zName = 0;
! 88461: sqlite3SrcListDelete(db, pSrc);
! 88462: }
! 88463: sqlite3DbFree(db, aiCol);
! 88464: }
! 88465: }
! 88466:
! 88467: #define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
! 88468:
! 88469: /*
! 88470: ** This function is called before generating code to update or delete a
! 88471: ** row contained in table pTab.
! 88472: */
! 88473: SQLITE_PRIVATE u32 sqlite3FkOldmask(
! 88474: Parse *pParse, /* Parse context */
! 88475: Table *pTab /* Table being modified */
! 88476: ){
! 88477: u32 mask = 0;
! 88478: if( pParse->db->flags&SQLITE_ForeignKeys ){
! 88479: FKey *p;
! 88480: int i;
! 88481: for(p=pTab->pFKey; p; p=p->pNextFrom){
! 88482: for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
! 88483: }
! 88484: for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
! 88485: Index *pIdx = 0;
! 88486: locateFkeyIndex(pParse, pTab, p, &pIdx, 0);
! 88487: if( pIdx ){
! 88488: for(i=0; i<pIdx->nColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
! 88489: }
! 88490: }
! 88491: }
! 88492: return mask;
! 88493: }
! 88494:
! 88495: /*
! 88496: ** This function is called before generating code to update or delete a
! 88497: ** row contained in table pTab. If the operation is a DELETE, then
! 88498: ** parameter aChange is passed a NULL value. For an UPDATE, aChange points
! 88499: ** to an array of size N, where N is the number of columns in table pTab.
! 88500: ** If the i'th column is not modified by the UPDATE, then the corresponding
! 88501: ** entry in the aChange[] array is set to -1. If the column is modified,
! 88502: ** the value is 0 or greater. Parameter chngRowid is set to true if the
! 88503: ** UPDATE statement modifies the rowid fields of the table.
! 88504: **
! 88505: ** If any foreign key processing will be required, this function returns
! 88506: ** true. If there is no foreign key related processing, this function
! 88507: ** returns false.
! 88508: */
! 88509: SQLITE_PRIVATE int sqlite3FkRequired(
! 88510: Parse *pParse, /* Parse context */
! 88511: Table *pTab, /* Table being modified */
! 88512: int *aChange, /* Non-NULL for UPDATE operations */
! 88513: int chngRowid /* True for UPDATE that affects rowid */
! 88514: ){
! 88515: if( pParse->db->flags&SQLITE_ForeignKeys ){
! 88516: if( !aChange ){
! 88517: /* A DELETE operation. Foreign key processing is required if the
! 88518: ** table in question is either the child or parent table for any
! 88519: ** foreign key constraint. */
! 88520: return (sqlite3FkReferences(pTab) || pTab->pFKey);
! 88521: }else{
! 88522: /* This is an UPDATE. Foreign key processing is only required if the
! 88523: ** operation modifies one or more child or parent key columns. */
! 88524: int i;
! 88525: FKey *p;
! 88526:
! 88527: /* Check if any child key columns are being modified. */
! 88528: for(p=pTab->pFKey; p; p=p->pNextFrom){
! 88529: for(i=0; i<p->nCol; i++){
! 88530: int iChildKey = p->aCol[i].iFrom;
! 88531: if( aChange[iChildKey]>=0 ) return 1;
! 88532: if( iChildKey==pTab->iPKey && chngRowid ) return 1;
! 88533: }
! 88534: }
! 88535:
! 88536: /* Check if any parent key columns are being modified. */
! 88537: for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
! 88538: for(i=0; i<p->nCol; i++){
! 88539: char *zKey = p->aCol[i].zCol;
! 88540: int iKey;
! 88541: for(iKey=0; iKey<pTab->nCol; iKey++){
! 88542: Column *pCol = &pTab->aCol[iKey];
! 88543: if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey) : pCol->isPrimKey) ){
! 88544: if( aChange[iKey]>=0 ) return 1;
! 88545: if( iKey==pTab->iPKey && chngRowid ) return 1;
! 88546: }
! 88547: }
! 88548: }
! 88549: }
! 88550: }
! 88551: }
! 88552: return 0;
! 88553: }
! 88554:
! 88555: /*
! 88556: ** This function is called when an UPDATE or DELETE operation is being
! 88557: ** compiled on table pTab, which is the parent table of foreign-key pFKey.
! 88558: ** If the current operation is an UPDATE, then the pChanges parameter is
! 88559: ** passed a pointer to the list of columns being modified. If it is a
! 88560: ** DELETE, pChanges is passed a NULL pointer.
! 88561: **
! 88562: ** It returns a pointer to a Trigger structure containing a trigger
! 88563: ** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
! 88564: ** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
! 88565: ** returned (these actions require no special handling by the triggers
! 88566: ** sub-system, code for them is created by fkScanChildren()).
! 88567: **
! 88568: ** For example, if pFKey is the foreign key and pTab is table "p" in
! 88569: ** the following schema:
! 88570: **
! 88571: ** CREATE TABLE p(pk PRIMARY KEY);
! 88572: ** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
! 88573: **
! 88574: ** then the returned trigger structure is equivalent to:
! 88575: **
! 88576: ** CREATE TRIGGER ... DELETE ON p BEGIN
! 88577: ** DELETE FROM c WHERE ck = old.pk;
! 88578: ** END;
! 88579: **
! 88580: ** The returned pointer is cached as part of the foreign key object. It
! 88581: ** is eventually freed along with the rest of the foreign key object by
! 88582: ** sqlite3FkDelete().
! 88583: */
! 88584: static Trigger *fkActionTrigger(
! 88585: Parse *pParse, /* Parse context */
! 88586: Table *pTab, /* Table being updated or deleted from */
! 88587: FKey *pFKey, /* Foreign key to get action for */
! 88588: ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
! 88589: ){
! 88590: sqlite3 *db = pParse->db; /* Database handle */
! 88591: int action; /* One of OE_None, OE_Cascade etc. */
! 88592: Trigger *pTrigger; /* Trigger definition to return */
! 88593: int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
! 88594:
! 88595: action = pFKey->aAction[iAction];
! 88596: pTrigger = pFKey->apTrigger[iAction];
! 88597:
! 88598: if( action!=OE_None && !pTrigger ){
! 88599: u8 enableLookaside; /* Copy of db->lookaside.bEnabled */
! 88600: char const *zFrom; /* Name of child table */
! 88601: int nFrom; /* Length in bytes of zFrom */
! 88602: Index *pIdx = 0; /* Parent key index for this FK */
! 88603: int *aiCol = 0; /* child table cols -> parent key cols */
! 88604: TriggerStep *pStep = 0; /* First (only) step of trigger program */
! 88605: Expr *pWhere = 0; /* WHERE clause of trigger step */
! 88606: ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */
! 88607: Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */
! 88608: int i; /* Iterator variable */
! 88609: Expr *pWhen = 0; /* WHEN clause for the trigger */
! 88610:
! 88611: if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
! 88612: assert( aiCol || pFKey->nCol==1 );
! 88613:
! 88614: for(i=0; i<pFKey->nCol; i++){
! 88615: Token tOld = { "old", 3 }; /* Literal "old" token */
! 88616: Token tNew = { "new", 3 }; /* Literal "new" token */
! 88617: Token tFromCol; /* Name of column in child table */
! 88618: Token tToCol; /* Name of column in parent table */
! 88619: int iFromCol; /* Idx of column in child table */
! 88620: Expr *pEq; /* tFromCol = OLD.tToCol */
! 88621:
! 88622: iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
! 88623: assert( iFromCol>=0 );
! 88624: tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
! 88625: tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
! 88626:
! 88627: tToCol.n = sqlite3Strlen30(tToCol.z);
! 88628: tFromCol.n = sqlite3Strlen30(tFromCol.z);
! 88629:
! 88630: /* Create the expression "OLD.zToCol = zFromCol". It is important
! 88631: ** that the "OLD.zToCol" term is on the LHS of the = operator, so
! 88632: ** that the affinity and collation sequence associated with the
! 88633: ** parent table are used for the comparison. */
! 88634: pEq = sqlite3PExpr(pParse, TK_EQ,
! 88635: sqlite3PExpr(pParse, TK_DOT,
! 88636: sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
! 88637: sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
! 88638: , 0),
! 88639: sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
! 88640: , 0);
! 88641: pWhere = sqlite3ExprAnd(db, pWhere, pEq);
! 88642:
! 88643: /* For ON UPDATE, construct the next term of the WHEN clause.
! 88644: ** The final WHEN clause will be like this:
! 88645: **
! 88646: ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
! 88647: */
! 88648: if( pChanges ){
! 88649: pEq = sqlite3PExpr(pParse, TK_IS,
! 88650: sqlite3PExpr(pParse, TK_DOT,
! 88651: sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
! 88652: sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
! 88653: 0),
! 88654: sqlite3PExpr(pParse, TK_DOT,
! 88655: sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
! 88656: sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
! 88657: 0),
! 88658: 0);
! 88659: pWhen = sqlite3ExprAnd(db, pWhen, pEq);
! 88660: }
! 88661:
! 88662: if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
! 88663: Expr *pNew;
! 88664: if( action==OE_Cascade ){
! 88665: pNew = sqlite3PExpr(pParse, TK_DOT,
! 88666: sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
! 88667: sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
! 88668: , 0);
! 88669: }else if( action==OE_SetDflt ){
! 88670: Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
! 88671: if( pDflt ){
! 88672: pNew = sqlite3ExprDup(db, pDflt, 0);
! 88673: }else{
! 88674: pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
! 88675: }
! 88676: }else{
! 88677: pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
! 88678: }
! 88679: pList = sqlite3ExprListAppend(pParse, pList, pNew);
! 88680: sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
! 88681: }
! 88682: }
! 88683: sqlite3DbFree(db, aiCol);
! 88684:
! 88685: zFrom = pFKey->pFrom->zName;
! 88686: nFrom = sqlite3Strlen30(zFrom);
! 88687:
! 88688: if( action==OE_Restrict ){
! 88689: Token tFrom;
! 88690: Expr *pRaise;
! 88691:
! 88692: tFrom.z = zFrom;
! 88693: tFrom.n = nFrom;
! 88694: pRaise = sqlite3Expr(db, TK_RAISE, "foreign key constraint failed");
! 88695: if( pRaise ){
! 88696: pRaise->affinity = OE_Abort;
! 88697: }
! 88698: pSelect = sqlite3SelectNew(pParse,
! 88699: sqlite3ExprListAppend(pParse, 0, pRaise),
! 88700: sqlite3SrcListAppend(db, 0, &tFrom, 0),
! 88701: pWhere,
! 88702: 0, 0, 0, 0, 0, 0
! 88703: );
! 88704: pWhere = 0;
! 88705: }
! 88706:
! 88707: /* Disable lookaside memory allocation */
! 88708: enableLookaside = db->lookaside.bEnabled;
! 88709: db->lookaside.bEnabled = 0;
! 88710:
! 88711: pTrigger = (Trigger *)sqlite3DbMallocZero(db,
! 88712: sizeof(Trigger) + /* struct Trigger */
! 88713: sizeof(TriggerStep) + /* Single step in trigger program */
! 88714: nFrom + 1 /* Space for pStep->target.z */
! 88715: );
! 88716: if( pTrigger ){
! 88717: pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
! 88718: pStep->target.z = (char *)&pStep[1];
! 88719: pStep->target.n = nFrom;
! 88720: memcpy((char *)pStep->target.z, zFrom, nFrom);
! 88721:
! 88722: pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
! 88723: pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
! 88724: pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
! 88725: if( pWhen ){
! 88726: pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
! 88727: pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
! 88728: }
! 88729: }
! 88730:
! 88731: /* Re-enable the lookaside buffer, if it was disabled earlier. */
! 88732: db->lookaside.bEnabled = enableLookaside;
! 88733:
! 88734: sqlite3ExprDelete(db, pWhere);
! 88735: sqlite3ExprDelete(db, pWhen);
! 88736: sqlite3ExprListDelete(db, pList);
! 88737: sqlite3SelectDelete(db, pSelect);
! 88738: if( db->mallocFailed==1 ){
! 88739: fkTriggerDelete(db, pTrigger);
! 88740: return 0;
! 88741: }
! 88742: assert( pStep!=0 );
! 88743:
! 88744: switch( action ){
! 88745: case OE_Restrict:
! 88746: pStep->op = TK_SELECT;
! 88747: break;
! 88748: case OE_Cascade:
! 88749: if( !pChanges ){
! 88750: pStep->op = TK_DELETE;
! 88751: break;
! 88752: }
! 88753: default:
! 88754: pStep->op = TK_UPDATE;
! 88755: }
! 88756: pStep->pTrig = pTrigger;
! 88757: pTrigger->pSchema = pTab->pSchema;
! 88758: pTrigger->pTabSchema = pTab->pSchema;
! 88759: pFKey->apTrigger[iAction] = pTrigger;
! 88760: pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
! 88761: }
! 88762:
! 88763: return pTrigger;
! 88764: }
! 88765:
! 88766: /*
! 88767: ** This function is called when deleting or updating a row to implement
! 88768: ** any required CASCADE, SET NULL or SET DEFAULT actions.
! 88769: */
! 88770: SQLITE_PRIVATE void sqlite3FkActions(
! 88771: Parse *pParse, /* Parse context */
! 88772: Table *pTab, /* Table being updated or deleted from */
! 88773: ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
! 88774: int regOld /* Address of array containing old row */
! 88775: ){
! 88776: /* If foreign-key support is enabled, iterate through all FKs that
! 88777: ** refer to table pTab. If there is an action associated with the FK
! 88778: ** for this operation (either update or delete), invoke the associated
! 88779: ** trigger sub-program. */
! 88780: if( pParse->db->flags&SQLITE_ForeignKeys ){
! 88781: FKey *pFKey; /* Iterator variable */
! 88782: for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
! 88783: Trigger *pAction = fkActionTrigger(pParse, pTab, pFKey, pChanges);
! 88784: if( pAction ){
! 88785: sqlite3CodeRowTriggerDirect(pParse, pAction, pTab, regOld, OE_Abort, 0);
! 88786: }
! 88787: }
! 88788: }
! 88789: }
! 88790:
! 88791: #endif /* ifndef SQLITE_OMIT_TRIGGER */
! 88792:
! 88793: /*
! 88794: ** Free all memory associated with foreign key definitions attached to
! 88795: ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
! 88796: ** hash table.
! 88797: */
! 88798: SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
! 88799: FKey *pFKey; /* Iterator variable */
! 88800: FKey *pNext; /* Copy of pFKey->pNextFrom */
! 88801:
! 88802: assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
! 88803: for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
! 88804:
! 88805: /* Remove the FK from the fkeyHash hash table. */
! 88806: if( !db || db->pnBytesFreed==0 ){
! 88807: if( pFKey->pPrevTo ){
! 88808: pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
! 88809: }else{
! 88810: void *p = (void *)pFKey->pNextTo;
! 88811: const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
! 88812: sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p);
! 88813: }
! 88814: if( pFKey->pNextTo ){
! 88815: pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
! 88816: }
! 88817: }
! 88818:
! 88819: /* EV: R-30323-21917 Each foreign key constraint in SQLite is
! 88820: ** classified as either immediate or deferred.
! 88821: */
! 88822: assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
! 88823:
! 88824: /* Delete any triggers created to implement actions for this FK. */
! 88825: #ifndef SQLITE_OMIT_TRIGGER
! 88826: fkTriggerDelete(db, pFKey->apTrigger[0]);
! 88827: fkTriggerDelete(db, pFKey->apTrigger[1]);
! 88828: #endif
! 88829:
! 88830: pNext = pFKey->pNextFrom;
! 88831: sqlite3DbFree(db, pFKey);
! 88832: }
! 88833: }
! 88834: #endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
! 88835:
! 88836: /************** End of fkey.c ************************************************/
! 88837: /************** Begin file insert.c ******************************************/
! 88838: /*
! 88839: ** 2001 September 15
! 88840: **
! 88841: ** The author disclaims copyright to this source code. In place of
! 88842: ** a legal notice, here is a blessing:
! 88843: **
! 88844: ** May you do good and not evil.
! 88845: ** May you find forgiveness for yourself and forgive others.
! 88846: ** May you share freely, never taking more than you give.
! 88847: **
! 88848: *************************************************************************
! 88849: ** This file contains C code routines that are called by the parser
! 88850: ** to handle INSERT statements in SQLite.
! 88851: */
! 88852:
! 88853: /*
! 88854: ** Generate code that will open a table for reading.
! 88855: */
! 88856: SQLITE_PRIVATE void sqlite3OpenTable(
! 88857: Parse *p, /* Generate code into this VDBE */
! 88858: int iCur, /* The cursor number of the table */
! 88859: int iDb, /* The database index in sqlite3.aDb[] */
! 88860: Table *pTab, /* The table to be opened */
! 88861: int opcode /* OP_OpenRead or OP_OpenWrite */
! 88862: ){
! 88863: Vdbe *v;
! 88864: if( IsVirtual(pTab) ) return;
! 88865: v = sqlite3GetVdbe(p);
! 88866: assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
! 88867: sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
! 88868: sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb);
! 88869: sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
! 88870: VdbeComment((v, "%s", pTab->zName));
! 88871: }
! 88872:
! 88873: /*
! 88874: ** Return a pointer to the column affinity string associated with index
! 88875: ** pIdx. A column affinity string has one character for each column in
! 88876: ** the table, according to the affinity of the column:
! 88877: **
! 88878: ** Character Column affinity
! 88879: ** ------------------------------
! 88880: ** 'a' TEXT
! 88881: ** 'b' NONE
! 88882: ** 'c' NUMERIC
! 88883: ** 'd' INTEGER
! 88884: ** 'e' REAL
! 88885: **
! 88886: ** An extra 'd' is appended to the end of the string to cover the
! 88887: ** rowid that appears as the last column in every index.
! 88888: **
! 88889: ** Memory for the buffer containing the column index affinity string
! 88890: ** is managed along with the rest of the Index structure. It will be
! 88891: ** released when sqlite3DeleteIndex() is called.
! 88892: */
! 88893: SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
! 88894: if( !pIdx->zColAff ){
! 88895: /* The first time a column affinity string for a particular index is
! 88896: ** required, it is allocated and populated here. It is then stored as
! 88897: ** a member of the Index structure for subsequent use.
! 88898: **
! 88899: ** The column affinity string will eventually be deleted by
! 88900: ** sqliteDeleteIndex() when the Index structure itself is cleaned
! 88901: ** up.
! 88902: */
! 88903: int n;
! 88904: Table *pTab = pIdx->pTable;
! 88905: sqlite3 *db = sqlite3VdbeDb(v);
! 88906: pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2);
! 88907: if( !pIdx->zColAff ){
! 88908: db->mallocFailed = 1;
! 88909: return 0;
! 88910: }
! 88911: for(n=0; n<pIdx->nColumn; n++){
! 88912: pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
! 88913: }
! 88914: pIdx->zColAff[n++] = SQLITE_AFF_INTEGER;
! 88915: pIdx->zColAff[n] = 0;
! 88916: }
! 88917:
! 88918: return pIdx->zColAff;
! 88919: }
! 88920:
! 88921: /*
! 88922: ** Set P4 of the most recently inserted opcode to a column affinity
! 88923: ** string for table pTab. A column affinity string has one character
! 88924: ** for each column indexed by the index, according to the affinity of the
! 88925: ** column:
! 88926: **
! 88927: ** Character Column affinity
! 88928: ** ------------------------------
! 88929: ** 'a' TEXT
! 88930: ** 'b' NONE
! 88931: ** 'c' NUMERIC
! 88932: ** 'd' INTEGER
! 88933: ** 'e' REAL
! 88934: */
! 88935: SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
! 88936: /* The first time a column affinity string for a particular table
! 88937: ** is required, it is allocated and populated here. It is then
! 88938: ** stored as a member of the Table structure for subsequent use.
! 88939: **
! 88940: ** The column affinity string will eventually be deleted by
! 88941: ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
! 88942: */
! 88943: if( !pTab->zColAff ){
! 88944: char *zColAff;
! 88945: int i;
! 88946: sqlite3 *db = sqlite3VdbeDb(v);
! 88947:
! 88948: zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
! 88949: if( !zColAff ){
! 88950: db->mallocFailed = 1;
! 88951: return;
! 88952: }
! 88953:
! 88954: for(i=0; i<pTab->nCol; i++){
! 88955: zColAff[i] = pTab->aCol[i].affinity;
! 88956: }
! 88957: zColAff[pTab->nCol] = '\0';
! 88958:
! 88959: pTab->zColAff = zColAff;
! 88960: }
! 88961:
! 88962: sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
! 88963: }
! 88964:
! 88965: /*
! 88966: ** Return non-zero if the table pTab in database iDb or any of its indices
! 88967: ** have been opened at any point in the VDBE program beginning at location
! 88968: ** iStartAddr throught the end of the program. This is used to see if
! 88969: ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
! 88970: ** run without using temporary table for the results of the SELECT.
! 88971: */
! 88972: static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
! 88973: Vdbe *v = sqlite3GetVdbe(p);
! 88974: int i;
! 88975: int iEnd = sqlite3VdbeCurrentAddr(v);
! 88976: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 88977: VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
! 88978: #endif
! 88979:
! 88980: for(i=iStartAddr; i<iEnd; i++){
! 88981: VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
! 88982: assert( pOp!=0 );
! 88983: if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
! 88984: Index *pIndex;
! 88985: int tnum = pOp->p2;
! 88986: if( tnum==pTab->tnum ){
! 88987: return 1;
! 88988: }
! 88989: for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
! 88990: if( tnum==pIndex->tnum ){
! 88991: return 1;
! 88992: }
! 88993: }
! 88994: }
! 88995: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 88996: if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
! 88997: assert( pOp->p4.pVtab!=0 );
! 88998: assert( pOp->p4type==P4_VTAB );
! 88999: return 1;
! 89000: }
! 89001: #endif
! 89002: }
! 89003: return 0;
! 89004: }
! 89005:
! 89006: #ifndef SQLITE_OMIT_AUTOINCREMENT
! 89007: /*
! 89008: ** Locate or create an AutoincInfo structure associated with table pTab
! 89009: ** which is in database iDb. Return the register number for the register
! 89010: ** that holds the maximum rowid.
! 89011: **
! 89012: ** There is at most one AutoincInfo structure per table even if the
! 89013: ** same table is autoincremented multiple times due to inserts within
! 89014: ** triggers. A new AutoincInfo structure is created if this is the
! 89015: ** first use of table pTab. On 2nd and subsequent uses, the original
! 89016: ** AutoincInfo structure is used.
! 89017: **
! 89018: ** Three memory locations are allocated:
! 89019: **
! 89020: ** (1) Register to hold the name of the pTab table.
! 89021: ** (2) Register to hold the maximum ROWID of pTab.
! 89022: ** (3) Register to hold the rowid in sqlite_sequence of pTab
! 89023: **
! 89024: ** The 2nd register is the one that is returned. That is all the
! 89025: ** insert routine needs to know about.
! 89026: */
! 89027: static int autoIncBegin(
! 89028: Parse *pParse, /* Parsing context */
! 89029: int iDb, /* Index of the database holding pTab */
! 89030: Table *pTab /* The table we are writing to */
! 89031: ){
! 89032: int memId = 0; /* Register holding maximum rowid */
! 89033: if( pTab->tabFlags & TF_Autoincrement ){
! 89034: Parse *pToplevel = sqlite3ParseToplevel(pParse);
! 89035: AutoincInfo *pInfo;
! 89036:
! 89037: pInfo = pToplevel->pAinc;
! 89038: while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
! 89039: if( pInfo==0 ){
! 89040: pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
! 89041: if( pInfo==0 ) return 0;
! 89042: pInfo->pNext = pToplevel->pAinc;
! 89043: pToplevel->pAinc = pInfo;
! 89044: pInfo->pTab = pTab;
! 89045: pInfo->iDb = iDb;
! 89046: pToplevel->nMem++; /* Register to hold name of table */
! 89047: pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
! 89048: pToplevel->nMem++; /* Rowid in sqlite_sequence */
! 89049: }
! 89050: memId = pInfo->regCtr;
! 89051: }
! 89052: return memId;
! 89053: }
! 89054:
! 89055: /*
! 89056: ** This routine generates code that will initialize all of the
! 89057: ** register used by the autoincrement tracker.
! 89058: */
! 89059: SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
! 89060: AutoincInfo *p; /* Information about an AUTOINCREMENT */
! 89061: sqlite3 *db = pParse->db; /* The database connection */
! 89062: Db *pDb; /* Database only autoinc table */
! 89063: int memId; /* Register holding max rowid */
! 89064: int addr; /* A VDBE address */
! 89065: Vdbe *v = pParse->pVdbe; /* VDBE under construction */
! 89066:
! 89067: /* This routine is never called during trigger-generation. It is
! 89068: ** only called from the top-level */
! 89069: assert( pParse->pTriggerTab==0 );
! 89070: assert( pParse==sqlite3ParseToplevel(pParse) );
! 89071:
! 89072: assert( v ); /* We failed long ago if this is not so */
! 89073: for(p = pParse->pAinc; p; p = p->pNext){
! 89074: pDb = &db->aDb[p->iDb];
! 89075: memId = p->regCtr;
! 89076: assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
! 89077: sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
! 89078: sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
! 89079: addr = sqlite3VdbeCurrentAddr(v);
! 89080: sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
! 89081: sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
! 89082: sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
! 89083: sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
! 89084: sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
! 89085: sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
! 89086: sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
! 89087: sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
! 89088: sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
! 89089: sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
! 89090: sqlite3VdbeAddOp0(v, OP_Close);
! 89091: }
! 89092: }
! 89093:
! 89094: /*
! 89095: ** Update the maximum rowid for an autoincrement calculation.
! 89096: **
! 89097: ** This routine should be called when the top of the stack holds a
! 89098: ** new rowid that is about to be inserted. If that new rowid is
! 89099: ** larger than the maximum rowid in the memId memory cell, then the
! 89100: ** memory cell is updated. The stack is unchanged.
! 89101: */
! 89102: static void autoIncStep(Parse *pParse, int memId, int regRowid){
! 89103: if( memId>0 ){
! 89104: sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
! 89105: }
! 89106: }
! 89107:
! 89108: /*
! 89109: ** This routine generates the code needed to write autoincrement
! 89110: ** maximum rowid values back into the sqlite_sequence register.
! 89111: ** Every statement that might do an INSERT into an autoincrement
! 89112: ** table (either directly or through triggers) needs to call this
! 89113: ** routine just before the "exit" code.
! 89114: */
! 89115: SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
! 89116: AutoincInfo *p;
! 89117: Vdbe *v = pParse->pVdbe;
! 89118: sqlite3 *db = pParse->db;
! 89119:
! 89120: assert( v );
! 89121: for(p = pParse->pAinc; p; p = p->pNext){
! 89122: Db *pDb = &db->aDb[p->iDb];
! 89123: int j1, j2, j3, j4, j5;
! 89124: int iRec;
! 89125: int memId = p->regCtr;
! 89126:
! 89127: iRec = sqlite3GetTempReg(pParse);
! 89128: assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
! 89129: sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
! 89130: j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
! 89131: j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
! 89132: j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
! 89133: j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
! 89134: sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
! 89135: sqlite3VdbeJumpHere(v, j2);
! 89136: sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
! 89137: j5 = sqlite3VdbeAddOp0(v, OP_Goto);
! 89138: sqlite3VdbeJumpHere(v, j4);
! 89139: sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
! 89140: sqlite3VdbeJumpHere(v, j1);
! 89141: sqlite3VdbeJumpHere(v, j5);
! 89142: sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
! 89143: sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
! 89144: sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
! 89145: sqlite3VdbeAddOp0(v, OP_Close);
! 89146: sqlite3ReleaseTempReg(pParse, iRec);
! 89147: }
! 89148: }
! 89149: #else
! 89150: /*
! 89151: ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
! 89152: ** above are all no-ops
! 89153: */
! 89154: # define autoIncBegin(A,B,C) (0)
! 89155: # define autoIncStep(A,B,C)
! 89156: #endif /* SQLITE_OMIT_AUTOINCREMENT */
! 89157:
! 89158:
! 89159: /* Forward declaration */
! 89160: static int xferOptimization(
! 89161: Parse *pParse, /* Parser context */
! 89162: Table *pDest, /* The table we are inserting into */
! 89163: Select *pSelect, /* A SELECT statement to use as the data source */
! 89164: int onError, /* How to handle constraint errors */
! 89165: int iDbDest /* The database of pDest */
! 89166: );
! 89167:
! 89168: /*
! 89169: ** This routine is call to handle SQL of the following forms:
! 89170: **
! 89171: ** insert into TABLE (IDLIST) values(EXPRLIST)
! 89172: ** insert into TABLE (IDLIST) select
! 89173: **
! 89174: ** The IDLIST following the table name is always optional. If omitted,
! 89175: ** then a list of all columns for the table is substituted. The IDLIST
! 89176: ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
! 89177: **
! 89178: ** The pList parameter holds EXPRLIST in the first form of the INSERT
! 89179: ** statement above, and pSelect is NULL. For the second form, pList is
! 89180: ** NULL and pSelect is a pointer to the select statement used to generate
! 89181: ** data for the insert.
! 89182: **
! 89183: ** The code generated follows one of four templates. For a simple
! 89184: ** select with data coming from a VALUES clause, the code executes
! 89185: ** once straight down through. Pseudo-code follows (we call this
! 89186: ** the "1st template"):
! 89187: **
! 89188: ** open write cursor to <table> and its indices
! 89189: ** puts VALUES clause expressions onto the stack
! 89190: ** write the resulting record into <table>
! 89191: ** cleanup
! 89192: **
! 89193: ** The three remaining templates assume the statement is of the form
! 89194: **
! 89195: ** INSERT INTO <table> SELECT ...
! 89196: **
! 89197: ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
! 89198: ** in other words if the SELECT pulls all columns from a single table
! 89199: ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
! 89200: ** if <table2> and <table1> are distinct tables but have identical
! 89201: ** schemas, including all the same indices, then a special optimization
! 89202: ** is invoked that copies raw records from <table2> over to <table1>.
! 89203: ** See the xferOptimization() function for the implementation of this
! 89204: ** template. This is the 2nd template.
! 89205: **
! 89206: ** open a write cursor to <table>
! 89207: ** open read cursor on <table2>
! 89208: ** transfer all records in <table2> over to <table>
! 89209: ** close cursors
! 89210: ** foreach index on <table>
! 89211: ** open a write cursor on the <table> index
! 89212: ** open a read cursor on the corresponding <table2> index
! 89213: ** transfer all records from the read to the write cursors
! 89214: ** close cursors
! 89215: ** end foreach
! 89216: **
! 89217: ** The 3rd template is for when the second template does not apply
! 89218: ** and the SELECT clause does not read from <table> at any time.
! 89219: ** The generated code follows this template:
! 89220: **
! 89221: ** EOF <- 0
! 89222: ** X <- A
! 89223: ** goto B
! 89224: ** A: setup for the SELECT
! 89225: ** loop over the rows in the SELECT
! 89226: ** load values into registers R..R+n
! 89227: ** yield X
! 89228: ** end loop
! 89229: ** cleanup after the SELECT
! 89230: ** EOF <- 1
! 89231: ** yield X
! 89232: ** goto A
! 89233: ** B: open write cursor to <table> and its indices
! 89234: ** C: yield X
! 89235: ** if EOF goto D
! 89236: ** insert the select result into <table> from R..R+n
! 89237: ** goto C
! 89238: ** D: cleanup
! 89239: **
! 89240: ** The 4th template is used if the insert statement takes its
! 89241: ** values from a SELECT but the data is being inserted into a table
! 89242: ** that is also read as part of the SELECT. In the third form,
! 89243: ** we have to use a intermediate table to store the results of
! 89244: ** the select. The template is like this:
! 89245: **
! 89246: ** EOF <- 0
! 89247: ** X <- A
! 89248: ** goto B
! 89249: ** A: setup for the SELECT
! 89250: ** loop over the tables in the SELECT
! 89251: ** load value into register R..R+n
! 89252: ** yield X
! 89253: ** end loop
! 89254: ** cleanup after the SELECT
! 89255: ** EOF <- 1
! 89256: ** yield X
! 89257: ** halt-error
! 89258: ** B: open temp table
! 89259: ** L: yield X
! 89260: ** if EOF goto M
! 89261: ** insert row from R..R+n into temp table
! 89262: ** goto L
! 89263: ** M: open write cursor to <table> and its indices
! 89264: ** rewind temp table
! 89265: ** C: loop over rows of intermediate table
! 89266: ** transfer values form intermediate table into <table>
! 89267: ** end loop
! 89268: ** D: cleanup
! 89269: */
! 89270: SQLITE_PRIVATE void sqlite3Insert(
! 89271: Parse *pParse, /* Parser context */
! 89272: SrcList *pTabList, /* Name of table into which we are inserting */
! 89273: ExprList *pList, /* List of values to be inserted */
! 89274: Select *pSelect, /* A SELECT statement to use as the data source */
! 89275: IdList *pColumn, /* Column names corresponding to IDLIST. */
! 89276: int onError /* How to handle constraint errors */
! 89277: ){
! 89278: sqlite3 *db; /* The main database structure */
! 89279: Table *pTab; /* The table to insert into. aka TABLE */
! 89280: char *zTab; /* Name of the table into which we are inserting */
! 89281: const char *zDb; /* Name of the database holding this table */
! 89282: int i, j, idx; /* Loop counters */
! 89283: Vdbe *v; /* Generate code into this virtual machine */
! 89284: Index *pIdx; /* For looping over indices of the table */
! 89285: int nColumn; /* Number of columns in the data */
! 89286: int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
! 89287: int baseCur = 0; /* VDBE Cursor number for pTab */
! 89288: int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
! 89289: int endOfLoop; /* Label for the end of the insertion loop */
! 89290: int useTempTable = 0; /* Store SELECT results in intermediate table */
! 89291: int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
! 89292: int addrInsTop = 0; /* Jump to label "D" */
! 89293: int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
! 89294: int addrSelect = 0; /* Address of coroutine that implements the SELECT */
! 89295: SelectDest dest; /* Destination for SELECT on rhs of INSERT */
! 89296: int iDb; /* Index of database holding TABLE */
! 89297: Db *pDb; /* The database containing table being inserted into */
! 89298: int appendFlag = 0; /* True if the insert is likely to be an append */
! 89299:
! 89300: /* Register allocations */
! 89301: int regFromSelect = 0;/* Base register for data coming from SELECT */
! 89302: int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
! 89303: int regRowCount = 0; /* Memory cell used for the row counter */
! 89304: int regIns; /* Block of regs holding rowid+data being inserted */
! 89305: int regRowid; /* registers holding insert rowid */
! 89306: int regData; /* register holding first column to insert */
! 89307: int regEof = 0; /* Register recording end of SELECT data */
! 89308: int *aRegIdx = 0; /* One register allocated to each index */
! 89309:
! 89310: #ifndef SQLITE_OMIT_TRIGGER
! 89311: int isView; /* True if attempting to insert into a view */
! 89312: Trigger *pTrigger; /* List of triggers on pTab, if required */
! 89313: int tmask; /* Mask of trigger times */
! 89314: #endif
! 89315:
! 89316: db = pParse->db;
! 89317: memset(&dest, 0, sizeof(dest));
! 89318: if( pParse->nErr || db->mallocFailed ){
! 89319: goto insert_cleanup;
! 89320: }
! 89321:
! 89322: /* Locate the table into which we will be inserting new information.
! 89323: */
! 89324: assert( pTabList->nSrc==1 );
! 89325: zTab = pTabList->a[0].zName;
! 89326: if( NEVER(zTab==0) ) goto insert_cleanup;
! 89327: pTab = sqlite3SrcListLookup(pParse, pTabList);
! 89328: if( pTab==0 ){
! 89329: goto insert_cleanup;
! 89330: }
! 89331: iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 89332: assert( iDb<db->nDb );
! 89333: pDb = &db->aDb[iDb];
! 89334: zDb = pDb->zName;
! 89335: if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
! 89336: goto insert_cleanup;
! 89337: }
! 89338:
! 89339: /* Figure out if we have any triggers and if the table being
! 89340: ** inserted into is a view
! 89341: */
! 89342: #ifndef SQLITE_OMIT_TRIGGER
! 89343: pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
! 89344: isView = pTab->pSelect!=0;
! 89345: #else
! 89346: # define pTrigger 0
! 89347: # define tmask 0
! 89348: # define isView 0
! 89349: #endif
! 89350: #ifdef SQLITE_OMIT_VIEW
! 89351: # undef isView
! 89352: # define isView 0
! 89353: #endif
! 89354: assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
! 89355:
! 89356: /* If pTab is really a view, make sure it has been initialized.
! 89357: ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual
! 89358: ** module table).
! 89359: */
! 89360: if( sqlite3ViewGetColumnNames(pParse, pTab) ){
! 89361: goto insert_cleanup;
! 89362: }
! 89363:
! 89364: /* Ensure that:
! 89365: * (a) the table is not read-only,
! 89366: * (b) that if it is a view then ON INSERT triggers exist
! 89367: */
! 89368: if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
! 89369: goto insert_cleanup;
! 89370: }
! 89371:
! 89372: /* Allocate a VDBE
! 89373: */
! 89374: v = sqlite3GetVdbe(pParse);
! 89375: if( v==0 ) goto insert_cleanup;
! 89376: if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
! 89377: sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
! 89378:
! 89379: #ifndef SQLITE_OMIT_XFER_OPT
! 89380: /* If the statement is of the form
! 89381: **
! 89382: ** INSERT INTO <table1> SELECT * FROM <table2>;
! 89383: **
! 89384: ** Then special optimizations can be applied that make the transfer
! 89385: ** very fast and which reduce fragmentation of indices.
! 89386: **
! 89387: ** This is the 2nd template.
! 89388: */
! 89389: if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
! 89390: assert( !pTrigger );
! 89391: assert( pList==0 );
! 89392: goto insert_end;
! 89393: }
! 89394: #endif /* SQLITE_OMIT_XFER_OPT */
! 89395:
! 89396: /* If this is an AUTOINCREMENT table, look up the sequence number in the
! 89397: ** sqlite_sequence table and store it in memory cell regAutoinc.
! 89398: */
! 89399: regAutoinc = autoIncBegin(pParse, iDb, pTab);
! 89400:
! 89401: /* Figure out how many columns of data are supplied. If the data
! 89402: ** is coming from a SELECT statement, then generate a co-routine that
! 89403: ** produces a single row of the SELECT on each invocation. The
! 89404: ** co-routine is the common header to the 3rd and 4th templates.
! 89405: */
! 89406: if( pSelect ){
! 89407: /* Data is coming from a SELECT. Generate code to implement that SELECT
! 89408: ** as a co-routine. The code is common to both the 3rd and 4th
! 89409: ** templates:
! 89410: **
! 89411: ** EOF <- 0
! 89412: ** X <- A
! 89413: ** goto B
! 89414: ** A: setup for the SELECT
! 89415: ** loop over the tables in the SELECT
! 89416: ** load value into register R..R+n
! 89417: ** yield X
! 89418: ** end loop
! 89419: ** cleanup after the SELECT
! 89420: ** EOF <- 1
! 89421: ** yield X
! 89422: ** halt-error
! 89423: **
! 89424: ** On each invocation of the co-routine, it puts a single row of the
! 89425: ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1.
! 89426: ** (These output registers are allocated by sqlite3Select().) When
! 89427: ** the SELECT completes, it sets the EOF flag stored in regEof.
! 89428: */
! 89429: int rc, j1;
! 89430:
! 89431: regEof = ++pParse->nMem;
! 89432: sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof); /* EOF <- 0 */
! 89433: VdbeComment((v, "SELECT eof flag"));
! 89434: sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
! 89435: addrSelect = sqlite3VdbeCurrentAddr(v)+2;
! 89436: sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm);
! 89437: j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
! 89438: VdbeComment((v, "Jump over SELECT coroutine"));
! 89439:
! 89440: /* Resolve the expressions in the SELECT statement and execute it. */
! 89441: rc = sqlite3Select(pParse, pSelect, &dest);
! 89442: assert( pParse->nErr==0 || rc );
! 89443: if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
! 89444: goto insert_cleanup;
! 89445: }
! 89446: sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof); /* EOF <- 1 */
! 89447: sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); /* yield X */
! 89448: sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
! 89449: VdbeComment((v, "End of SELECT coroutine"));
! 89450: sqlite3VdbeJumpHere(v, j1); /* label B: */
! 89451:
! 89452: regFromSelect = dest.iMem;
! 89453: assert( pSelect->pEList );
! 89454: nColumn = pSelect->pEList->nExpr;
! 89455: assert( dest.nMem==nColumn );
! 89456:
! 89457: /* Set useTempTable to TRUE if the result of the SELECT statement
! 89458: ** should be written into a temporary table (template 4). Set to
! 89459: ** FALSE if each* row of the SELECT can be written directly into
! 89460: ** the destination table (template 3).
! 89461: **
! 89462: ** A temp table must be used if the table being updated is also one
! 89463: ** of the tables being read by the SELECT statement. Also use a
! 89464: ** temp table in the case of row triggers.
! 89465: */
! 89466: if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
! 89467: useTempTable = 1;
! 89468: }
! 89469:
! 89470: if( useTempTable ){
! 89471: /* Invoke the coroutine to extract information from the SELECT
! 89472: ** and add it to a transient table srcTab. The code generated
! 89473: ** here is from the 4th template:
! 89474: **
! 89475: ** B: open temp table
! 89476: ** L: yield X
! 89477: ** if EOF goto M
! 89478: ** insert row from R..R+n into temp table
! 89479: ** goto L
! 89480: ** M: ...
! 89481: */
! 89482: int regRec; /* Register to hold packed record */
! 89483: int regTempRowid; /* Register to hold temp table ROWID */
! 89484: int addrTop; /* Label "L" */
! 89485: int addrIf; /* Address of jump to M */
! 89486:
! 89487: srcTab = pParse->nTab++;
! 89488: regRec = sqlite3GetTempReg(pParse);
! 89489: regTempRowid = sqlite3GetTempReg(pParse);
! 89490: sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
! 89491: addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
! 89492: addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
! 89493: sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
! 89494: sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
! 89495: sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
! 89496: sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
! 89497: sqlite3VdbeJumpHere(v, addrIf);
! 89498: sqlite3ReleaseTempReg(pParse, regRec);
! 89499: sqlite3ReleaseTempReg(pParse, regTempRowid);
! 89500: }
! 89501: }else{
! 89502: /* This is the case if the data for the INSERT is coming from a VALUES
! 89503: ** clause
! 89504: */
! 89505: NameContext sNC;
! 89506: memset(&sNC, 0, sizeof(sNC));
! 89507: sNC.pParse = pParse;
! 89508: srcTab = -1;
! 89509: assert( useTempTable==0 );
! 89510: nColumn = pList ? pList->nExpr : 0;
! 89511: for(i=0; i<nColumn; i++){
! 89512: if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
! 89513: goto insert_cleanup;
! 89514: }
! 89515: }
! 89516: }
! 89517:
! 89518: /* Make sure the number of columns in the source data matches the number
! 89519: ** of columns to be inserted into the table.
! 89520: */
! 89521: if( IsVirtual(pTab) ){
! 89522: for(i=0; i<pTab->nCol; i++){
! 89523: nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
! 89524: }
! 89525: }
! 89526: if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
! 89527: sqlite3ErrorMsg(pParse,
! 89528: "table %S has %d columns but %d values were supplied",
! 89529: pTabList, 0, pTab->nCol-nHidden, nColumn);
! 89530: goto insert_cleanup;
! 89531: }
! 89532: if( pColumn!=0 && nColumn!=pColumn->nId ){
! 89533: sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
! 89534: goto insert_cleanup;
! 89535: }
! 89536:
! 89537: /* If the INSERT statement included an IDLIST term, then make sure
! 89538: ** all elements of the IDLIST really are columns of the table and
! 89539: ** remember the column indices.
! 89540: **
! 89541: ** If the table has an INTEGER PRIMARY KEY column and that column
! 89542: ** is named in the IDLIST, then record in the keyColumn variable
! 89543: ** the index into IDLIST of the primary key column. keyColumn is
! 89544: ** the index of the primary key as it appears in IDLIST, not as
! 89545: ** is appears in the original table. (The index of the primary
! 89546: ** key in the original table is pTab->iPKey.)
! 89547: */
! 89548: if( pColumn ){
! 89549: for(i=0; i<pColumn->nId; i++){
! 89550: pColumn->a[i].idx = -1;
! 89551: }
! 89552: for(i=0; i<pColumn->nId; i++){
! 89553: for(j=0; j<pTab->nCol; j++){
! 89554: if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
! 89555: pColumn->a[i].idx = j;
! 89556: if( j==pTab->iPKey ){
! 89557: keyColumn = i;
! 89558: }
! 89559: break;
! 89560: }
! 89561: }
! 89562: if( j>=pTab->nCol ){
! 89563: if( sqlite3IsRowid(pColumn->a[i].zName) ){
! 89564: keyColumn = i;
! 89565: }else{
! 89566: sqlite3ErrorMsg(pParse, "table %S has no column named %s",
! 89567: pTabList, 0, pColumn->a[i].zName);
! 89568: pParse->checkSchema = 1;
! 89569: goto insert_cleanup;
! 89570: }
! 89571: }
! 89572: }
! 89573: }
! 89574:
! 89575: /* If there is no IDLIST term but the table has an integer primary
! 89576: ** key, the set the keyColumn variable to the primary key column index
! 89577: ** in the original table definition.
! 89578: */
! 89579: if( pColumn==0 && nColumn>0 ){
! 89580: keyColumn = pTab->iPKey;
! 89581: }
! 89582:
! 89583: /* Initialize the count of rows to be inserted
! 89584: */
! 89585: if( db->flags & SQLITE_CountRows ){
! 89586: regRowCount = ++pParse->nMem;
! 89587: sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
! 89588: }
! 89589:
! 89590: /* If this is not a view, open the table and and all indices */
! 89591: if( !isView ){
! 89592: int nIdx;
! 89593:
! 89594: baseCur = pParse->nTab;
! 89595: nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
! 89596: aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
! 89597: if( aRegIdx==0 ){
! 89598: goto insert_cleanup;
! 89599: }
! 89600: for(i=0; i<nIdx; i++){
! 89601: aRegIdx[i] = ++pParse->nMem;
! 89602: }
! 89603: }
! 89604:
! 89605: /* This is the top of the main insertion loop */
! 89606: if( useTempTable ){
! 89607: /* This block codes the top of loop only. The complete loop is the
! 89608: ** following pseudocode (template 4):
! 89609: **
! 89610: ** rewind temp table
! 89611: ** C: loop over rows of intermediate table
! 89612: ** transfer values form intermediate table into <table>
! 89613: ** end loop
! 89614: ** D: ...
! 89615: */
! 89616: addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
! 89617: addrCont = sqlite3VdbeCurrentAddr(v);
! 89618: }else if( pSelect ){
! 89619: /* This block codes the top of loop only. The complete loop is the
! 89620: ** following pseudocode (template 3):
! 89621: **
! 89622: ** C: yield X
! 89623: ** if EOF goto D
! 89624: ** insert the select result into <table> from R..R+n
! 89625: ** goto C
! 89626: ** D: ...
! 89627: */
! 89628: addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
! 89629: addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
! 89630: }
! 89631:
! 89632: /* Allocate registers for holding the rowid of the new row,
! 89633: ** the content of the new row, and the assemblied row record.
! 89634: */
! 89635: regRowid = regIns = pParse->nMem+1;
! 89636: pParse->nMem += pTab->nCol + 1;
! 89637: if( IsVirtual(pTab) ){
! 89638: regRowid++;
! 89639: pParse->nMem++;
! 89640: }
! 89641: regData = regRowid+1;
! 89642:
! 89643: /* Run the BEFORE and INSTEAD OF triggers, if there are any
! 89644: */
! 89645: endOfLoop = sqlite3VdbeMakeLabel(v);
! 89646: if( tmask & TRIGGER_BEFORE ){
! 89647: int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
! 89648:
! 89649: /* build the NEW.* reference row. Note that if there is an INTEGER
! 89650: ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
! 89651: ** translated into a unique ID for the row. But on a BEFORE trigger,
! 89652: ** we do not know what the unique ID will be (because the insert has
! 89653: ** not happened yet) so we substitute a rowid of -1
! 89654: */
! 89655: if( keyColumn<0 ){
! 89656: sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
! 89657: }else{
! 89658: int j1;
! 89659: if( useTempTable ){
! 89660: sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols);
! 89661: }else{
! 89662: assert( pSelect==0 ); /* Otherwise useTempTable is true */
! 89663: sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols);
! 89664: }
! 89665: j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
! 89666: sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
! 89667: sqlite3VdbeJumpHere(v, j1);
! 89668: sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
! 89669: }
! 89670:
! 89671: /* Cannot have triggers on a virtual table. If it were possible,
! 89672: ** this block would have to account for hidden column.
! 89673: */
! 89674: assert( !IsVirtual(pTab) );
! 89675:
! 89676: /* Create the new column data
! 89677: */
! 89678: for(i=0; i<pTab->nCol; i++){
! 89679: if( pColumn==0 ){
! 89680: j = i;
! 89681: }else{
! 89682: for(j=0; j<pColumn->nId; j++){
! 89683: if( pColumn->a[j].idx==i ) break;
! 89684: }
! 89685: }
! 89686: if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
! 89687: sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
! 89688: }else if( useTempTable ){
! 89689: sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
! 89690: }else{
! 89691: assert( pSelect==0 ); /* Otherwise useTempTable is true */
! 89692: sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
! 89693: }
! 89694: }
! 89695:
! 89696: /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
! 89697: ** do not attempt any conversions before assembling the record.
! 89698: ** If this is a real table, attempt conversions as required by the
! 89699: ** table column affinities.
! 89700: */
! 89701: if( !isView ){
! 89702: sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
! 89703: sqlite3TableAffinityStr(v, pTab);
! 89704: }
! 89705:
! 89706: /* Fire BEFORE or INSTEAD OF triggers */
! 89707: sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
! 89708: pTab, regCols-pTab->nCol-1, onError, endOfLoop);
! 89709:
! 89710: sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
! 89711: }
! 89712:
! 89713: /* Push the record number for the new entry onto the stack. The
! 89714: ** record number is a randomly generate integer created by NewRowid
! 89715: ** except when the table has an INTEGER PRIMARY KEY column, in which
! 89716: ** case the record number is the same as that column.
! 89717: */
! 89718: if( !isView ){
! 89719: if( IsVirtual(pTab) ){
! 89720: /* The row that the VUpdate opcode will delete: none */
! 89721: sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
! 89722: }
! 89723: if( keyColumn>=0 ){
! 89724: if( useTempTable ){
! 89725: sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
! 89726: }else if( pSelect ){
! 89727: sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
! 89728: }else{
! 89729: VdbeOp *pOp;
! 89730: sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
! 89731: pOp = sqlite3VdbeGetOp(v, -1);
! 89732: if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
! 89733: appendFlag = 1;
! 89734: pOp->opcode = OP_NewRowid;
! 89735: pOp->p1 = baseCur;
! 89736: pOp->p2 = regRowid;
! 89737: pOp->p3 = regAutoinc;
! 89738: }
! 89739: }
! 89740: /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
! 89741: ** to generate a unique primary key value.
! 89742: */
! 89743: if( !appendFlag ){
! 89744: int j1;
! 89745: if( !IsVirtual(pTab) ){
! 89746: j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
! 89747: sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
! 89748: sqlite3VdbeJumpHere(v, j1);
! 89749: }else{
! 89750: j1 = sqlite3VdbeCurrentAddr(v);
! 89751: sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
! 89752: }
! 89753: sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
! 89754: }
! 89755: }else if( IsVirtual(pTab) ){
! 89756: sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
! 89757: }else{
! 89758: sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
! 89759: appendFlag = 1;
! 89760: }
! 89761: autoIncStep(pParse, regAutoinc, regRowid);
! 89762:
! 89763: /* Push onto the stack, data for all columns of the new entry, beginning
! 89764: ** with the first column.
! 89765: */
! 89766: nHidden = 0;
! 89767: for(i=0; i<pTab->nCol; i++){
! 89768: int iRegStore = regRowid+1+i;
! 89769: if( i==pTab->iPKey ){
! 89770: /* The value of the INTEGER PRIMARY KEY column is always a NULL.
! 89771: ** Whenever this column is read, the record number will be substituted
! 89772: ** in its place. So will fill this column with a NULL to avoid
! 89773: ** taking up data space with information that will never be used. */
! 89774: sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
! 89775: continue;
! 89776: }
! 89777: if( pColumn==0 ){
! 89778: if( IsHiddenColumn(&pTab->aCol[i]) ){
! 89779: assert( IsVirtual(pTab) );
! 89780: j = -1;
! 89781: nHidden++;
! 89782: }else{
! 89783: j = i - nHidden;
! 89784: }
! 89785: }else{
! 89786: for(j=0; j<pColumn->nId; j++){
! 89787: if( pColumn->a[j].idx==i ) break;
! 89788: }
! 89789: }
! 89790: if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
! 89791: sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
! 89792: }else if( useTempTable ){
! 89793: sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
! 89794: }else if( pSelect ){
! 89795: sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
! 89796: }else{
! 89797: sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
! 89798: }
! 89799: }
! 89800:
! 89801: /* Generate code to check constraints and generate index keys and
! 89802: ** do the insertion.
! 89803: */
! 89804: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 89805: if( IsVirtual(pTab) ){
! 89806: const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
! 89807: sqlite3VtabMakeWritable(pParse, pTab);
! 89808: sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
! 89809: sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
! 89810: sqlite3MayAbort(pParse);
! 89811: }else
! 89812: #endif
! 89813: {
! 89814: int isReplace; /* Set to true if constraints may cause a replace */
! 89815: sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
! 89816: keyColumn>=0, 0, onError, endOfLoop, &isReplace
! 89817: );
! 89818: sqlite3FkCheck(pParse, pTab, 0, regIns);
! 89819: sqlite3CompleteInsertion(
! 89820: pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
! 89821: );
! 89822: }
! 89823: }
! 89824:
! 89825: /* Update the count of rows that are inserted
! 89826: */
! 89827: if( (db->flags & SQLITE_CountRows)!=0 ){
! 89828: sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
! 89829: }
! 89830:
! 89831: if( pTrigger ){
! 89832: /* Code AFTER triggers */
! 89833: sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
! 89834: pTab, regData-2-pTab->nCol, onError, endOfLoop);
! 89835: }
! 89836:
! 89837: /* The bottom of the main insertion loop, if the data source
! 89838: ** is a SELECT statement.
! 89839: */
! 89840: sqlite3VdbeResolveLabel(v, endOfLoop);
! 89841: if( useTempTable ){
! 89842: sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
! 89843: sqlite3VdbeJumpHere(v, addrInsTop);
! 89844: sqlite3VdbeAddOp1(v, OP_Close, srcTab);
! 89845: }else if( pSelect ){
! 89846: sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
! 89847: sqlite3VdbeJumpHere(v, addrInsTop);
! 89848: }
! 89849:
! 89850: if( !IsVirtual(pTab) && !isView ){
! 89851: /* Close all tables opened */
! 89852: sqlite3VdbeAddOp1(v, OP_Close, baseCur);
! 89853: for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
! 89854: sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
! 89855: }
! 89856: }
! 89857:
! 89858: insert_end:
! 89859: /* Update the sqlite_sequence table by storing the content of the
! 89860: ** maximum rowid counter values recorded while inserting into
! 89861: ** autoincrement tables.
! 89862: */
! 89863: if( pParse->nested==0 && pParse->pTriggerTab==0 ){
! 89864: sqlite3AutoincrementEnd(pParse);
! 89865: }
! 89866:
! 89867: /*
! 89868: ** Return the number of rows inserted. If this routine is
! 89869: ** generating code because of a call to sqlite3NestedParse(), do not
! 89870: ** invoke the callback function.
! 89871: */
! 89872: if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
! 89873: sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
! 89874: sqlite3VdbeSetNumCols(v, 1);
! 89875: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
! 89876: }
! 89877:
! 89878: insert_cleanup:
! 89879: sqlite3SrcListDelete(db, pTabList);
! 89880: sqlite3ExprListDelete(db, pList);
! 89881: sqlite3SelectDelete(db, pSelect);
! 89882: sqlite3IdListDelete(db, pColumn);
! 89883: sqlite3DbFree(db, aRegIdx);
! 89884: }
! 89885:
! 89886: /* Make sure "isView" and other macros defined above are undefined. Otherwise
! 89887: ** thely may interfere with compilation of other functions in this file
! 89888: ** (or in another file, if this file becomes part of the amalgamation). */
! 89889: #ifdef isView
! 89890: #undef isView
! 89891: #endif
! 89892: #ifdef pTrigger
! 89893: #undef pTrigger
! 89894: #endif
! 89895: #ifdef tmask
! 89896: #undef tmask
! 89897: #endif
! 89898:
! 89899:
! 89900: /*
! 89901: ** Generate code to do constraint checks prior to an INSERT or an UPDATE.
! 89902: **
! 89903: ** The input is a range of consecutive registers as follows:
! 89904: **
! 89905: ** 1. The rowid of the row after the update.
! 89906: **
! 89907: ** 2. The data in the first column of the entry after the update.
! 89908: **
! 89909: ** i. Data from middle columns...
! 89910: **
! 89911: ** N. The data in the last column of the entry after the update.
! 89912: **
! 89913: ** The regRowid parameter is the index of the register containing (1).
! 89914: **
! 89915: ** If isUpdate is true and rowidChng is non-zero, then rowidChng contains
! 89916: ** the address of a register containing the rowid before the update takes
! 89917: ** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate
! 89918: ** is false, indicating an INSERT statement, then a non-zero rowidChng
! 89919: ** indicates that the rowid was explicitly specified as part of the
! 89920: ** INSERT statement. If rowidChng is false, it means that the rowid is
! 89921: ** computed automatically in an insert or that the rowid value is not
! 89922: ** modified by an update.
! 89923: **
! 89924: ** The code generated by this routine store new index entries into
! 89925: ** registers identified by aRegIdx[]. No index entry is created for
! 89926: ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
! 89927: ** the same as the order of indices on the linked list of indices
! 89928: ** attached to the table.
! 89929: **
! 89930: ** This routine also generates code to check constraints. NOT NULL,
! 89931: ** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
! 89932: ** then the appropriate action is performed. There are five possible
! 89933: ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
! 89934: **
! 89935: ** Constraint type Action What Happens
! 89936: ** --------------- ---------- ----------------------------------------
! 89937: ** any ROLLBACK The current transaction is rolled back and
! 89938: ** sqlite3_exec() returns immediately with a
! 89939: ** return code of SQLITE_CONSTRAINT.
! 89940: **
! 89941: ** any ABORT Back out changes from the current command
! 89942: ** only (do not do a complete rollback) then
! 89943: ** cause sqlite3_exec() to return immediately
! 89944: ** with SQLITE_CONSTRAINT.
! 89945: **
! 89946: ** any FAIL Sqlite3_exec() returns immediately with a
! 89947: ** return code of SQLITE_CONSTRAINT. The
! 89948: ** transaction is not rolled back and any
! 89949: ** prior changes are retained.
! 89950: **
! 89951: ** any IGNORE The record number and data is popped from
! 89952: ** the stack and there is an immediate jump
! 89953: ** to label ignoreDest.
! 89954: **
! 89955: ** NOT NULL REPLACE The NULL value is replace by the default
! 89956: ** value for that column. If the default value
! 89957: ** is NULL, the action is the same as ABORT.
! 89958: **
! 89959: ** UNIQUE REPLACE The other row that conflicts with the row
! 89960: ** being inserted is removed.
! 89961: **
! 89962: ** CHECK REPLACE Illegal. The results in an exception.
! 89963: **
! 89964: ** Which action to take is determined by the overrideError parameter.
! 89965: ** Or if overrideError==OE_Default, then the pParse->onError parameter
! 89966: ** is used. Or if pParse->onError==OE_Default then the onError value
! 89967: ** for the constraint is used.
! 89968: **
! 89969: ** The calling routine must open a read/write cursor for pTab with
! 89970: ** cursor number "baseCur". All indices of pTab must also have open
! 89971: ** read/write cursors with cursor number baseCur+i for the i-th cursor.
! 89972: ** Except, if there is no possibility of a REPLACE action then
! 89973: ** cursors do not need to be open for indices where aRegIdx[i]==0.
! 89974: */
! 89975: SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
! 89976: Parse *pParse, /* The parser context */
! 89977: Table *pTab, /* the table into which we are inserting */
! 89978: int baseCur, /* Index of a read/write cursor pointing at pTab */
! 89979: int regRowid, /* Index of the range of input registers */
! 89980: int *aRegIdx, /* Register used by each index. 0 for unused indices */
! 89981: int rowidChng, /* True if the rowid might collide with existing entry */
! 89982: int isUpdate, /* True for UPDATE, False for INSERT */
! 89983: int overrideError, /* Override onError to this if not OE_Default */
! 89984: int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
! 89985: int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */
! 89986: ){
! 89987: int i; /* loop counter */
! 89988: Vdbe *v; /* VDBE under constrution */
! 89989: int nCol; /* Number of columns */
! 89990: int onError; /* Conflict resolution strategy */
! 89991: int j1; /* Addresss of jump instruction */
! 89992: int j2 = 0, j3; /* Addresses of jump instructions */
! 89993: int regData; /* Register containing first data column */
! 89994: int iCur; /* Table cursor number */
! 89995: Index *pIdx; /* Pointer to one of the indices */
! 89996: int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
! 89997: int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid;
! 89998:
! 89999: v = sqlite3GetVdbe(pParse);
! 90000: assert( v!=0 );
! 90001: assert( pTab->pSelect==0 ); /* This table is not a VIEW */
! 90002: nCol = pTab->nCol;
! 90003: regData = regRowid + 1;
! 90004:
! 90005: /* Test all NOT NULL constraints.
! 90006: */
! 90007: for(i=0; i<nCol; i++){
! 90008: if( i==pTab->iPKey ){
! 90009: continue;
! 90010: }
! 90011: onError = pTab->aCol[i].notNull;
! 90012: if( onError==OE_None ) continue;
! 90013: if( overrideError!=OE_Default ){
! 90014: onError = overrideError;
! 90015: }else if( onError==OE_Default ){
! 90016: onError = OE_Abort;
! 90017: }
! 90018: if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
! 90019: onError = OE_Abort;
! 90020: }
! 90021: assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
! 90022: || onError==OE_Ignore || onError==OE_Replace );
! 90023: switch( onError ){
! 90024: case OE_Abort:
! 90025: sqlite3MayAbort(pParse);
! 90026: case OE_Rollback:
! 90027: case OE_Fail: {
! 90028: char *zMsg;
! 90029: sqlite3VdbeAddOp3(v, OP_HaltIfNull,
! 90030: SQLITE_CONSTRAINT, onError, regData+i);
! 90031: zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
! 90032: pTab->zName, pTab->aCol[i].zName);
! 90033: sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
! 90034: break;
! 90035: }
! 90036: case OE_Ignore: {
! 90037: sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
! 90038: break;
! 90039: }
! 90040: default: {
! 90041: assert( onError==OE_Replace );
! 90042: j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
! 90043: sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
! 90044: sqlite3VdbeJumpHere(v, j1);
! 90045: break;
! 90046: }
! 90047: }
! 90048: }
! 90049:
! 90050: /* Test all CHECK constraints
! 90051: */
! 90052: #ifndef SQLITE_OMIT_CHECK
! 90053: if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
! 90054: int allOk = sqlite3VdbeMakeLabel(v);
! 90055: pParse->ckBase = regData;
! 90056: sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL);
! 90057: onError = overrideError!=OE_Default ? overrideError : OE_Abort;
! 90058: if( onError==OE_Ignore ){
! 90059: sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
! 90060: }else{
! 90061: if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
! 90062: sqlite3HaltConstraint(pParse, onError, 0, 0);
! 90063: }
! 90064: sqlite3VdbeResolveLabel(v, allOk);
! 90065: }
! 90066: #endif /* !defined(SQLITE_OMIT_CHECK) */
! 90067:
! 90068: /* If we have an INTEGER PRIMARY KEY, make sure the primary key
! 90069: ** of the new record does not previously exist. Except, if this
! 90070: ** is an UPDATE and the primary key is not changing, that is OK.
! 90071: */
! 90072: if( rowidChng ){
! 90073: onError = pTab->keyConf;
! 90074: if( overrideError!=OE_Default ){
! 90075: onError = overrideError;
! 90076: }else if( onError==OE_Default ){
! 90077: onError = OE_Abort;
! 90078: }
! 90079:
! 90080: if( isUpdate ){
! 90081: j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
! 90082: }
! 90083: j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
! 90084: switch( onError ){
! 90085: default: {
! 90086: onError = OE_Abort;
! 90087: /* Fall thru into the next case */
! 90088: }
! 90089: case OE_Rollback:
! 90090: case OE_Abort:
! 90091: case OE_Fail: {
! 90092: sqlite3HaltConstraint(
! 90093: pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
! 90094: break;
! 90095: }
! 90096: case OE_Replace: {
! 90097: /* If there are DELETE triggers on this table and the
! 90098: ** recursive-triggers flag is set, call GenerateRowDelete() to
! 90099: ** remove the conflicting row from the the table. This will fire
! 90100: ** the triggers and remove both the table and index b-tree entries.
! 90101: **
! 90102: ** Otherwise, if there are no triggers or the recursive-triggers
! 90103: ** flag is not set, but the table has one or more indexes, call
! 90104: ** GenerateRowIndexDelete(). This removes the index b-tree entries
! 90105: ** only. The table b-tree entry will be replaced by the new entry
! 90106: ** when it is inserted.
! 90107: **
! 90108: ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
! 90109: ** also invoke MultiWrite() to indicate that this VDBE may require
! 90110: ** statement rollback (if the statement is aborted after the delete
! 90111: ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
! 90112: ** but being more selective here allows statements like:
! 90113: **
! 90114: ** REPLACE INTO t(rowid) VALUES($newrowid)
! 90115: **
! 90116: ** to run without a statement journal if there are no indexes on the
! 90117: ** table.
! 90118: */
! 90119: Trigger *pTrigger = 0;
! 90120: if( pParse->db->flags&SQLITE_RecTriggers ){
! 90121: pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
! 90122: }
! 90123: if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
! 90124: sqlite3MultiWrite(pParse);
! 90125: sqlite3GenerateRowDelete(
! 90126: pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
! 90127: );
! 90128: }else if( pTab->pIndex ){
! 90129: sqlite3MultiWrite(pParse);
! 90130: sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
! 90131: }
! 90132: seenReplace = 1;
! 90133: break;
! 90134: }
! 90135: case OE_Ignore: {
! 90136: assert( seenReplace==0 );
! 90137: sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
! 90138: break;
! 90139: }
! 90140: }
! 90141: sqlite3VdbeJumpHere(v, j3);
! 90142: if( isUpdate ){
! 90143: sqlite3VdbeJumpHere(v, j2);
! 90144: }
! 90145: }
! 90146:
! 90147: /* Test all UNIQUE constraints by creating entries for each UNIQUE
! 90148: ** index and making sure that duplicate entries do not already exist.
! 90149: ** Add the new records to the indices as we go.
! 90150: */
! 90151: for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
! 90152: int regIdx;
! 90153: int regR;
! 90154:
! 90155: if( aRegIdx[iCur]==0 ) continue; /* Skip unused indices */
! 90156:
! 90157: /* Create a key for accessing the index entry */
! 90158: regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
! 90159: for(i=0; i<pIdx->nColumn; i++){
! 90160: int idx = pIdx->aiColumn[i];
! 90161: if( idx==pTab->iPKey ){
! 90162: sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
! 90163: }else{
! 90164: sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
! 90165: }
! 90166: }
! 90167: sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
! 90168: sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
! 90169: sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
! 90170: sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
! 90171:
! 90172: /* Find out what action to take in case there is an indexing conflict */
! 90173: onError = pIdx->onError;
! 90174: if( onError==OE_None ){
! 90175: sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
! 90176: continue; /* pIdx is not a UNIQUE index */
! 90177: }
! 90178: if( overrideError!=OE_Default ){
! 90179: onError = overrideError;
! 90180: }else if( onError==OE_Default ){
! 90181: onError = OE_Abort;
! 90182: }
! 90183: if( seenReplace ){
! 90184: if( onError==OE_Ignore ) onError = OE_Replace;
! 90185: else if( onError==OE_Fail ) onError = OE_Abort;
! 90186: }
! 90187:
! 90188: /* Check to see if the new index entry will be unique */
! 90189: regR = sqlite3GetTempReg(pParse);
! 90190: sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR);
! 90191: j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
! 90192: regR, SQLITE_INT_TO_PTR(regIdx),
! 90193: P4_INT32);
! 90194: sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
! 90195:
! 90196: /* Generate code that executes if the new index entry is not unique */
! 90197: assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
! 90198: || onError==OE_Ignore || onError==OE_Replace );
! 90199: switch( onError ){
! 90200: case OE_Rollback:
! 90201: case OE_Abort:
! 90202: case OE_Fail: {
! 90203: int j;
! 90204: StrAccum errMsg;
! 90205: const char *zSep;
! 90206: char *zErr;
! 90207:
! 90208: sqlite3StrAccumInit(&errMsg, 0, 0, 200);
! 90209: errMsg.db = pParse->db;
! 90210: zSep = pIdx->nColumn>1 ? "columns " : "column ";
! 90211: for(j=0; j<pIdx->nColumn; j++){
! 90212: char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
! 90213: sqlite3StrAccumAppend(&errMsg, zSep, -1);
! 90214: zSep = ", ";
! 90215: sqlite3StrAccumAppend(&errMsg, zCol, -1);
! 90216: }
! 90217: sqlite3StrAccumAppend(&errMsg,
! 90218: pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
! 90219: zErr = sqlite3StrAccumFinish(&errMsg);
! 90220: sqlite3HaltConstraint(pParse, onError, zErr, 0);
! 90221: sqlite3DbFree(errMsg.db, zErr);
! 90222: break;
! 90223: }
! 90224: case OE_Ignore: {
! 90225: assert( seenReplace==0 );
! 90226: sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
! 90227: break;
! 90228: }
! 90229: default: {
! 90230: Trigger *pTrigger = 0;
! 90231: assert( onError==OE_Replace );
! 90232: sqlite3MultiWrite(pParse);
! 90233: if( pParse->db->flags&SQLITE_RecTriggers ){
! 90234: pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
! 90235: }
! 90236: sqlite3GenerateRowDelete(
! 90237: pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
! 90238: );
! 90239: seenReplace = 1;
! 90240: break;
! 90241: }
! 90242: }
! 90243: sqlite3VdbeJumpHere(v, j3);
! 90244: sqlite3ReleaseTempReg(pParse, regR);
! 90245: }
! 90246:
! 90247: if( pbMayReplace ){
! 90248: *pbMayReplace = seenReplace;
! 90249: }
! 90250: }
! 90251:
! 90252: /*
! 90253: ** This routine generates code to finish the INSERT or UPDATE operation
! 90254: ** that was started by a prior call to sqlite3GenerateConstraintChecks.
! 90255: ** A consecutive range of registers starting at regRowid contains the
! 90256: ** rowid and the content to be inserted.
! 90257: **
! 90258: ** The arguments to this routine should be the same as the first six
! 90259: ** arguments to sqlite3GenerateConstraintChecks.
! 90260: */
! 90261: SQLITE_PRIVATE void sqlite3CompleteInsertion(
! 90262: Parse *pParse, /* The parser context */
! 90263: Table *pTab, /* the table into which we are inserting */
! 90264: int baseCur, /* Index of a read/write cursor pointing at pTab */
! 90265: int regRowid, /* Range of content */
! 90266: int *aRegIdx, /* Register used by each index. 0 for unused indices */
! 90267: int isUpdate, /* True for UPDATE, False for INSERT */
! 90268: int appendBias, /* True if this is likely to be an append */
! 90269: int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
! 90270: ){
! 90271: int i;
! 90272: Vdbe *v;
! 90273: int nIdx;
! 90274: Index *pIdx;
! 90275: u8 pik_flags;
! 90276: int regData;
! 90277: int regRec;
! 90278:
! 90279: v = sqlite3GetVdbe(pParse);
! 90280: assert( v!=0 );
! 90281: assert( pTab->pSelect==0 ); /* This table is not a VIEW */
! 90282: for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
! 90283: for(i=nIdx-1; i>=0; i--){
! 90284: if( aRegIdx[i]==0 ) continue;
! 90285: sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
! 90286: if( useSeekResult ){
! 90287: sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
! 90288: }
! 90289: }
! 90290: regData = regRowid + 1;
! 90291: regRec = sqlite3GetTempReg(pParse);
! 90292: sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
! 90293: sqlite3TableAffinityStr(v, pTab);
! 90294: sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
! 90295: if( pParse->nested ){
! 90296: pik_flags = 0;
! 90297: }else{
! 90298: pik_flags = OPFLAG_NCHANGE;
! 90299: pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
! 90300: }
! 90301: if( appendBias ){
! 90302: pik_flags |= OPFLAG_APPEND;
! 90303: }
! 90304: if( useSeekResult ){
! 90305: pik_flags |= OPFLAG_USESEEKRESULT;
! 90306: }
! 90307: sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
! 90308: if( !pParse->nested ){
! 90309: sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
! 90310: }
! 90311: sqlite3VdbeChangeP5(v, pik_flags);
! 90312: }
! 90313:
! 90314: /*
! 90315: ** Generate code that will open cursors for a table and for all
! 90316: ** indices of that table. The "baseCur" parameter is the cursor number used
! 90317: ** for the table. Indices are opened on subsequent cursors.
! 90318: **
! 90319: ** Return the number of indices on the table.
! 90320: */
! 90321: SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
! 90322: Parse *pParse, /* Parsing context */
! 90323: Table *pTab, /* Table to be opened */
! 90324: int baseCur, /* Cursor number assigned to the table */
! 90325: int op /* OP_OpenRead or OP_OpenWrite */
! 90326: ){
! 90327: int i;
! 90328: int iDb;
! 90329: Index *pIdx;
! 90330: Vdbe *v;
! 90331:
! 90332: if( IsVirtual(pTab) ) return 0;
! 90333: iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
! 90334: v = sqlite3GetVdbe(pParse);
! 90335: assert( v!=0 );
! 90336: sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
! 90337: for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
! 90338: KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
! 90339: assert( pIdx->pSchema==pTab->pSchema );
! 90340: sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
! 90341: (char*)pKey, P4_KEYINFO_HANDOFF);
! 90342: VdbeComment((v, "%s", pIdx->zName));
! 90343: }
! 90344: if( pParse->nTab<baseCur+i ){
! 90345: pParse->nTab = baseCur+i;
! 90346: }
! 90347: return i-1;
! 90348: }
! 90349:
! 90350:
! 90351: #ifdef SQLITE_TEST
! 90352: /*
! 90353: ** The following global variable is incremented whenever the
! 90354: ** transfer optimization is used. This is used for testing
! 90355: ** purposes only - to make sure the transfer optimization really
! 90356: ** is happening when it is suppose to.
! 90357: */
! 90358: SQLITE_API int sqlite3_xferopt_count;
! 90359: #endif /* SQLITE_TEST */
! 90360:
! 90361:
! 90362: #ifndef SQLITE_OMIT_XFER_OPT
! 90363: /*
! 90364: ** Check to collation names to see if they are compatible.
! 90365: */
! 90366: static int xferCompatibleCollation(const char *z1, const char *z2){
! 90367: if( z1==0 ){
! 90368: return z2==0;
! 90369: }
! 90370: if( z2==0 ){
! 90371: return 0;
! 90372: }
! 90373: return sqlite3StrICmp(z1, z2)==0;
! 90374: }
! 90375:
! 90376:
! 90377: /*
! 90378: ** Check to see if index pSrc is compatible as a source of data
! 90379: ** for index pDest in an insert transfer optimization. The rules
! 90380: ** for a compatible index:
! 90381: **
! 90382: ** * The index is over the same set of columns
! 90383: ** * The same DESC and ASC markings occurs on all columns
! 90384: ** * The same onError processing (OE_Abort, OE_Ignore, etc)
! 90385: ** * The same collating sequence on each column
! 90386: */
! 90387: static int xferCompatibleIndex(Index *pDest, Index *pSrc){
! 90388: int i;
! 90389: assert( pDest && pSrc );
! 90390: assert( pDest->pTable!=pSrc->pTable );
! 90391: if( pDest->nColumn!=pSrc->nColumn ){
! 90392: return 0; /* Different number of columns */
! 90393: }
! 90394: if( pDest->onError!=pSrc->onError ){
! 90395: return 0; /* Different conflict resolution strategies */
! 90396: }
! 90397: for(i=0; i<pSrc->nColumn; i++){
! 90398: if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
! 90399: return 0; /* Different columns indexed */
! 90400: }
! 90401: if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
! 90402: return 0; /* Different sort orders */
! 90403: }
! 90404: if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
! 90405: return 0; /* Different collating sequences */
! 90406: }
! 90407: }
! 90408:
! 90409: /* If no test above fails then the indices must be compatible */
! 90410: return 1;
! 90411: }
! 90412:
! 90413: /*
! 90414: ** Attempt the transfer optimization on INSERTs of the form
! 90415: **
! 90416: ** INSERT INTO tab1 SELECT * FROM tab2;
! 90417: **
! 90418: ** The xfer optimization transfers raw records from tab2 over to tab1.
! 90419: ** Columns are not decoded and reassemblied, which greatly improves
! 90420: ** performance. Raw index records are transferred in the same way.
! 90421: **
! 90422: ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
! 90423: ** There are lots of rules for determining compatibility - see comments
! 90424: ** embedded in the code for details.
! 90425: **
! 90426: ** This routine returns TRUE if the optimization is guaranteed to be used.
! 90427: ** Sometimes the xfer optimization will only work if the destination table
! 90428: ** is empty - a factor that can only be determined at run-time. In that
! 90429: ** case, this routine generates code for the xfer optimization but also
! 90430: ** does a test to see if the destination table is empty and jumps over the
! 90431: ** xfer optimization code if the test fails. In that case, this routine
! 90432: ** returns FALSE so that the caller will know to go ahead and generate
! 90433: ** an unoptimized transfer. This routine also returns FALSE if there
! 90434: ** is no chance that the xfer optimization can be applied.
! 90435: **
! 90436: ** This optimization is particularly useful at making VACUUM run faster.
! 90437: */
! 90438: static int xferOptimization(
! 90439: Parse *pParse, /* Parser context */
! 90440: Table *pDest, /* The table we are inserting into */
! 90441: Select *pSelect, /* A SELECT statement to use as the data source */
! 90442: int onError, /* How to handle constraint errors */
! 90443: int iDbDest /* The database of pDest */
! 90444: ){
! 90445: ExprList *pEList; /* The result set of the SELECT */
! 90446: Table *pSrc; /* The table in the FROM clause of SELECT */
! 90447: Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
! 90448: struct SrcList_item *pItem; /* An element of pSelect->pSrc */
! 90449: int i; /* Loop counter */
! 90450: int iDbSrc; /* The database of pSrc */
! 90451: int iSrc, iDest; /* Cursors from source and destination */
! 90452: int addr1, addr2; /* Loop addresses */
! 90453: int emptyDestTest; /* Address of test for empty pDest */
! 90454: int emptySrcTest; /* Address of test for empty pSrc */
! 90455: Vdbe *v; /* The VDBE we are building */
! 90456: KeyInfo *pKey; /* Key information for an index */
! 90457: int regAutoinc; /* Memory register used by AUTOINC */
! 90458: int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
! 90459: int regData, regRowid; /* Registers holding data and rowid */
! 90460:
! 90461: if( pSelect==0 ){
! 90462: return 0; /* Must be of the form INSERT INTO ... SELECT ... */
! 90463: }
! 90464: if( sqlite3TriggerList(pParse, pDest) ){
! 90465: return 0; /* tab1 must not have triggers */
! 90466: }
! 90467: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 90468: if( pDest->tabFlags & TF_Virtual ){
! 90469: return 0; /* tab1 must not be a virtual table */
! 90470: }
! 90471: #endif
! 90472: if( onError==OE_Default ){
! 90473: if( pDest->iPKey>=0 ) onError = pDest->keyConf;
! 90474: if( onError==OE_Default ) onError = OE_Abort;
! 90475: }
! 90476: assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
! 90477: if( pSelect->pSrc->nSrc!=1 ){
! 90478: return 0; /* FROM clause must have exactly one term */
! 90479: }
! 90480: if( pSelect->pSrc->a[0].pSelect ){
! 90481: return 0; /* FROM clause cannot contain a subquery */
! 90482: }
! 90483: if( pSelect->pWhere ){
! 90484: return 0; /* SELECT may not have a WHERE clause */
! 90485: }
! 90486: if( pSelect->pOrderBy ){
! 90487: return 0; /* SELECT may not have an ORDER BY clause */
! 90488: }
! 90489: /* Do not need to test for a HAVING clause. If HAVING is present but
! 90490: ** there is no ORDER BY, we will get an error. */
! 90491: if( pSelect->pGroupBy ){
! 90492: return 0; /* SELECT may not have a GROUP BY clause */
! 90493: }
! 90494: if( pSelect->pLimit ){
! 90495: return 0; /* SELECT may not have a LIMIT clause */
! 90496: }
! 90497: assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */
! 90498: if( pSelect->pPrior ){
! 90499: return 0; /* SELECT may not be a compound query */
! 90500: }
! 90501: if( pSelect->selFlags & SF_Distinct ){
! 90502: return 0; /* SELECT may not be DISTINCT */
! 90503: }
! 90504: pEList = pSelect->pEList;
! 90505: assert( pEList!=0 );
! 90506: if( pEList->nExpr!=1 ){
! 90507: return 0; /* The result set must have exactly one column */
! 90508: }
! 90509: assert( pEList->a[0].pExpr );
! 90510: if( pEList->a[0].pExpr->op!=TK_ALL ){
! 90511: return 0; /* The result set must be the special operator "*" */
! 90512: }
! 90513:
! 90514: /* At this point we have established that the statement is of the
! 90515: ** correct syntactic form to participate in this optimization. Now
! 90516: ** we have to check the semantics.
! 90517: */
! 90518: pItem = pSelect->pSrc->a;
! 90519: pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
! 90520: if( pSrc==0 ){
! 90521: return 0; /* FROM clause does not contain a real table */
! 90522: }
! 90523: if( pSrc==pDest ){
! 90524: return 0; /* tab1 and tab2 may not be the same table */
! 90525: }
! 90526: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 90527: if( pSrc->tabFlags & TF_Virtual ){
! 90528: return 0; /* tab2 must not be a virtual table */
! 90529: }
! 90530: #endif
! 90531: if( pSrc->pSelect ){
! 90532: return 0; /* tab2 may not be a view */
! 90533: }
! 90534: if( pDest->nCol!=pSrc->nCol ){
! 90535: return 0; /* Number of columns must be the same in tab1 and tab2 */
! 90536: }
! 90537: if( pDest->iPKey!=pSrc->iPKey ){
! 90538: return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
! 90539: }
! 90540: for(i=0; i<pDest->nCol; i++){
! 90541: if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
! 90542: return 0; /* Affinity must be the same on all columns */
! 90543: }
! 90544: if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
! 90545: return 0; /* Collating sequence must be the same on all columns */
! 90546: }
! 90547: if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
! 90548: return 0; /* tab2 must be NOT NULL if tab1 is */
! 90549: }
! 90550: }
! 90551: for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
! 90552: if( pDestIdx->onError!=OE_None ){
! 90553: destHasUniqueIdx = 1;
! 90554: }
! 90555: for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
! 90556: if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
! 90557: }
! 90558: if( pSrcIdx==0 ){
! 90559: return 0; /* pDestIdx has no corresponding index in pSrc */
! 90560: }
! 90561: }
! 90562: #ifndef SQLITE_OMIT_CHECK
! 90563: if( pDest->pCheck && sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
! 90564: return 0; /* Tables have different CHECK constraints. Ticket #2252 */
! 90565: }
! 90566: #endif
! 90567: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 90568: /* Disallow the transfer optimization if the destination table constains
! 90569: ** any foreign key constraints. This is more restrictive than necessary.
! 90570: ** But the main beneficiary of the transfer optimization is the VACUUM
! 90571: ** command, and the VACUUM command disables foreign key constraints. So
! 90572: ** the extra complication to make this rule less restrictive is probably
! 90573: ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
! 90574: */
! 90575: if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
! 90576: return 0;
! 90577: }
! 90578: #endif
! 90579: if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
! 90580: return 0; /* xfer opt does not play well with PRAGMA count_changes */
! 90581: }
! 90582:
! 90583: /* If we get this far, it means that the xfer optimization is at
! 90584: ** least a possibility, though it might only work if the destination
! 90585: ** table (tab1) is initially empty.
! 90586: */
! 90587: #ifdef SQLITE_TEST
! 90588: sqlite3_xferopt_count++;
! 90589: #endif
! 90590: iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
! 90591: v = sqlite3GetVdbe(pParse);
! 90592: sqlite3CodeVerifySchema(pParse, iDbSrc);
! 90593: iSrc = pParse->nTab++;
! 90594: iDest = pParse->nTab++;
! 90595: regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
! 90596: sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
! 90597: if( (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
! 90598: || destHasUniqueIdx /* (2) */
! 90599: || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */
! 90600: ){
! 90601: /* In some circumstances, we are able to run the xfer optimization
! 90602: ** only if the destination table is initially empty. This code makes
! 90603: ** that determination. Conditions under which the destination must
! 90604: ** be empty:
! 90605: **
! 90606: ** (1) There is no INTEGER PRIMARY KEY but there are indices.
! 90607: ** (If the destination is not initially empty, the rowid fields
! 90608: ** of index entries might need to change.)
! 90609: **
! 90610: ** (2) The destination has a unique index. (The xfer optimization
! 90611: ** is unable to test uniqueness.)
! 90612: **
! 90613: ** (3) onError is something other than OE_Abort and OE_Rollback.
! 90614: */
! 90615: addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
! 90616: emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
! 90617: sqlite3VdbeJumpHere(v, addr1);
! 90618: }else{
! 90619: emptyDestTest = 0;
! 90620: }
! 90621: sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
! 90622: emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
! 90623: regData = sqlite3GetTempReg(pParse);
! 90624: regRowid = sqlite3GetTempReg(pParse);
! 90625: if( pDest->iPKey>=0 ){
! 90626: addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
! 90627: addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
! 90628: sqlite3HaltConstraint(
! 90629: pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
! 90630: sqlite3VdbeJumpHere(v, addr2);
! 90631: autoIncStep(pParse, regAutoinc, regRowid);
! 90632: }else if( pDest->pIndex==0 ){
! 90633: addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
! 90634: }else{
! 90635: addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
! 90636: assert( (pDest->tabFlags & TF_Autoincrement)==0 );
! 90637: }
! 90638: sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
! 90639: sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
! 90640: sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
! 90641: sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
! 90642: sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
! 90643: for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
! 90644: for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
! 90645: if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
! 90646: }
! 90647: assert( pSrcIdx );
! 90648: sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
! 90649: sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
! 90650: pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
! 90651: sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
! 90652: (char*)pKey, P4_KEYINFO_HANDOFF);
! 90653: VdbeComment((v, "%s", pSrcIdx->zName));
! 90654: pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
! 90655: sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
! 90656: (char*)pKey, P4_KEYINFO_HANDOFF);
! 90657: VdbeComment((v, "%s", pDestIdx->zName));
! 90658: addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
! 90659: sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
! 90660: sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
! 90661: sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
! 90662: sqlite3VdbeJumpHere(v, addr1);
! 90663: }
! 90664: sqlite3VdbeJumpHere(v, emptySrcTest);
! 90665: sqlite3ReleaseTempReg(pParse, regRowid);
! 90666: sqlite3ReleaseTempReg(pParse, regData);
! 90667: sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
! 90668: sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
! 90669: if( emptyDestTest ){
! 90670: sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
! 90671: sqlite3VdbeJumpHere(v, emptyDestTest);
! 90672: sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
! 90673: return 0;
! 90674: }else{
! 90675: return 1;
! 90676: }
! 90677: }
! 90678: #endif /* SQLITE_OMIT_XFER_OPT */
! 90679:
! 90680: /************** End of insert.c **********************************************/
! 90681: /************** Begin file legacy.c ******************************************/
! 90682: /*
! 90683: ** 2001 September 15
! 90684: **
! 90685: ** The author disclaims copyright to this source code. In place of
! 90686: ** a legal notice, here is a blessing:
! 90687: **
! 90688: ** May you do good and not evil.
! 90689: ** May you find forgiveness for yourself and forgive others.
! 90690: ** May you share freely, never taking more than you give.
! 90691: **
! 90692: *************************************************************************
! 90693: ** Main file for the SQLite library. The routines in this file
! 90694: ** implement the programmer interface to the library. Routines in
! 90695: ** other files are for internal use by SQLite and should not be
! 90696: ** accessed by users of the library.
! 90697: */
! 90698:
! 90699:
! 90700: /*
! 90701: ** Execute SQL code. Return one of the SQLITE_ success/failure
! 90702: ** codes. Also write an error message into memory obtained from
! 90703: ** malloc() and make *pzErrMsg point to that message.
! 90704: **
! 90705: ** If the SQL is a query, then for each row in the query result
! 90706: ** the xCallback() function is called. pArg becomes the first
! 90707: ** argument to xCallback(). If xCallback=NULL then no callback
! 90708: ** is invoked, even for queries.
! 90709: */
! 90710: SQLITE_API int sqlite3_exec(
! 90711: sqlite3 *db, /* The database on which the SQL executes */
! 90712: const char *zSql, /* The SQL to be executed */
! 90713: sqlite3_callback xCallback, /* Invoke this callback routine */
! 90714: void *pArg, /* First argument to xCallback() */
! 90715: char **pzErrMsg /* Write error messages here */
! 90716: ){
! 90717: int rc = SQLITE_OK; /* Return code */
! 90718: const char *zLeftover; /* Tail of unprocessed SQL */
! 90719: sqlite3_stmt *pStmt = 0; /* The current SQL statement */
! 90720: char **azCols = 0; /* Names of result columns */
! 90721: int nRetry = 0; /* Number of retry attempts */
! 90722: int callbackIsInit; /* True if callback data is initialized */
! 90723:
! 90724: if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
! 90725: if( zSql==0 ) zSql = "";
! 90726:
! 90727: sqlite3_mutex_enter(db->mutex);
! 90728: sqlite3Error(db, SQLITE_OK, 0);
! 90729: while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
! 90730: int nCol;
! 90731: char **azVals = 0;
! 90732:
! 90733: pStmt = 0;
! 90734: rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
! 90735: assert( rc==SQLITE_OK || pStmt==0 );
! 90736: if( rc!=SQLITE_OK ){
! 90737: continue;
! 90738: }
! 90739: if( !pStmt ){
! 90740: /* this happens for a comment or white-space */
! 90741: zSql = zLeftover;
! 90742: continue;
! 90743: }
! 90744:
! 90745: callbackIsInit = 0;
! 90746: nCol = sqlite3_column_count(pStmt);
! 90747:
! 90748: while( 1 ){
! 90749: int i;
! 90750: rc = sqlite3_step(pStmt);
! 90751:
! 90752: /* Invoke the callback function if required */
! 90753: if( xCallback && (SQLITE_ROW==rc ||
! 90754: (SQLITE_DONE==rc && !callbackIsInit
! 90755: && db->flags&SQLITE_NullCallback)) ){
! 90756: if( !callbackIsInit ){
! 90757: azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
! 90758: if( azCols==0 ){
! 90759: goto exec_out;
! 90760: }
! 90761: for(i=0; i<nCol; i++){
! 90762: azCols[i] = (char *)sqlite3_column_name(pStmt, i);
! 90763: /* sqlite3VdbeSetColName() installs column names as UTF8
! 90764: ** strings so there is no way for sqlite3_column_name() to fail. */
! 90765: assert( azCols[i]!=0 );
! 90766: }
! 90767: callbackIsInit = 1;
! 90768: }
! 90769: if( rc==SQLITE_ROW ){
! 90770: azVals = &azCols[nCol];
! 90771: for(i=0; i<nCol; i++){
! 90772: azVals[i] = (char *)sqlite3_column_text(pStmt, i);
! 90773: if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
! 90774: db->mallocFailed = 1;
! 90775: goto exec_out;
! 90776: }
! 90777: }
! 90778: }
! 90779: if( xCallback(pArg, nCol, azVals, azCols) ){
! 90780: rc = SQLITE_ABORT;
! 90781: sqlite3VdbeFinalize((Vdbe *)pStmt);
! 90782: pStmt = 0;
! 90783: sqlite3Error(db, SQLITE_ABORT, 0);
! 90784: goto exec_out;
! 90785: }
! 90786: }
! 90787:
! 90788: if( rc!=SQLITE_ROW ){
! 90789: rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
! 90790: pStmt = 0;
! 90791: if( rc!=SQLITE_SCHEMA ){
! 90792: nRetry = 0;
! 90793: zSql = zLeftover;
! 90794: while( sqlite3Isspace(zSql[0]) ) zSql++;
! 90795: }
! 90796: break;
! 90797: }
! 90798: }
! 90799:
! 90800: sqlite3DbFree(db, azCols);
! 90801: azCols = 0;
! 90802: }
! 90803:
! 90804: exec_out:
! 90805: if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
! 90806: sqlite3DbFree(db, azCols);
! 90807:
! 90808: rc = sqlite3ApiExit(db, rc);
! 90809: if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
! 90810: int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
! 90811: *pzErrMsg = sqlite3Malloc(nErrMsg);
! 90812: if( *pzErrMsg ){
! 90813: memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
! 90814: }else{
! 90815: rc = SQLITE_NOMEM;
! 90816: sqlite3Error(db, SQLITE_NOMEM, 0);
! 90817: }
! 90818: }else if( pzErrMsg ){
! 90819: *pzErrMsg = 0;
! 90820: }
! 90821:
! 90822: assert( (rc&db->errMask)==rc );
! 90823: sqlite3_mutex_leave(db->mutex);
! 90824: return rc;
! 90825: }
! 90826:
! 90827: /************** End of legacy.c **********************************************/
! 90828: /************** Begin file loadext.c *****************************************/
! 90829: /*
! 90830: ** 2006 June 7
! 90831: **
! 90832: ** The author disclaims copyright to this source code. In place of
! 90833: ** a legal notice, here is a blessing:
! 90834: **
! 90835: ** May you do good and not evil.
! 90836: ** May you find forgiveness for yourself and forgive others.
! 90837: ** May you share freely, never taking more than you give.
! 90838: **
! 90839: *************************************************************************
! 90840: ** This file contains code used to dynamically load extensions into
! 90841: ** the SQLite library.
! 90842: */
! 90843:
! 90844: #ifndef SQLITE_CORE
! 90845: #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
! 90846: #endif
! 90847: /************** Include sqlite3ext.h in the middle of loadext.c **************/
! 90848: /************** Begin file sqlite3ext.h **************************************/
! 90849: /*
! 90850: ** 2006 June 7
! 90851: **
! 90852: ** The author disclaims copyright to this source code. In place of
! 90853: ** a legal notice, here is a blessing:
! 90854: **
! 90855: ** May you do good and not evil.
! 90856: ** May you find forgiveness for yourself and forgive others.
! 90857: ** May you share freely, never taking more than you give.
! 90858: **
! 90859: *************************************************************************
! 90860: ** This header file defines the SQLite interface for use by
! 90861: ** shared libraries that want to be imported as extensions into
! 90862: ** an SQLite instance. Shared libraries that intend to be loaded
! 90863: ** as extensions by SQLite should #include this file instead of
! 90864: ** sqlite3.h.
! 90865: */
! 90866: #ifndef _SQLITE3EXT_H_
! 90867: #define _SQLITE3EXT_H_
! 90868:
! 90869: typedef struct sqlite3_api_routines sqlite3_api_routines;
! 90870:
! 90871: /*
! 90872: ** The following structure holds pointers to all of the SQLite API
! 90873: ** routines.
! 90874: **
! 90875: ** WARNING: In order to maintain backwards compatibility, add new
! 90876: ** interfaces to the end of this structure only. If you insert new
! 90877: ** interfaces in the middle of this structure, then older different
! 90878: ** versions of SQLite will not be able to load each others' shared
! 90879: ** libraries!
! 90880: */
! 90881: struct sqlite3_api_routines {
! 90882: void * (*aggregate_context)(sqlite3_context*,int nBytes);
! 90883: int (*aggregate_count)(sqlite3_context*);
! 90884: int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
! 90885: int (*bind_double)(sqlite3_stmt*,int,double);
! 90886: int (*bind_int)(sqlite3_stmt*,int,int);
! 90887: int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
! 90888: int (*bind_null)(sqlite3_stmt*,int);
! 90889: int (*bind_parameter_count)(sqlite3_stmt*);
! 90890: int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
! 90891: const char * (*bind_parameter_name)(sqlite3_stmt*,int);
! 90892: int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
! 90893: int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
! 90894: int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
! 90895: int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
! 90896: int (*busy_timeout)(sqlite3*,int ms);
! 90897: int (*changes)(sqlite3*);
! 90898: int (*close)(sqlite3*);
! 90899: int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
! 90900: int eTextRep,const char*));
! 90901: int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
! 90902: int eTextRep,const void*));
! 90903: const void * (*column_blob)(sqlite3_stmt*,int iCol);
! 90904: int (*column_bytes)(sqlite3_stmt*,int iCol);
! 90905: int (*column_bytes16)(sqlite3_stmt*,int iCol);
! 90906: int (*column_count)(sqlite3_stmt*pStmt);
! 90907: const char * (*column_database_name)(sqlite3_stmt*,int);
! 90908: const void * (*column_database_name16)(sqlite3_stmt*,int);
! 90909: const char * (*column_decltype)(sqlite3_stmt*,int i);
! 90910: const void * (*column_decltype16)(sqlite3_stmt*,int);
! 90911: double (*column_double)(sqlite3_stmt*,int iCol);
! 90912: int (*column_int)(sqlite3_stmt*,int iCol);
! 90913: sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
! 90914: const char * (*column_name)(sqlite3_stmt*,int);
! 90915: const void * (*column_name16)(sqlite3_stmt*,int);
! 90916: const char * (*column_origin_name)(sqlite3_stmt*,int);
! 90917: const void * (*column_origin_name16)(sqlite3_stmt*,int);
! 90918: const char * (*column_table_name)(sqlite3_stmt*,int);
! 90919: const void * (*column_table_name16)(sqlite3_stmt*,int);
! 90920: const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
! 90921: const void * (*column_text16)(sqlite3_stmt*,int iCol);
! 90922: int (*column_type)(sqlite3_stmt*,int iCol);
! 90923: sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
! 90924: void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
! 90925: int (*complete)(const char*sql);
! 90926: int (*complete16)(const void*sql);
! 90927: int (*create_collation)(sqlite3*,const char*,int,void*,
! 90928: int(*)(void*,int,const void*,int,const void*));
! 90929: int (*create_collation16)(sqlite3*,const void*,int,void*,
! 90930: int(*)(void*,int,const void*,int,const void*));
! 90931: int (*create_function)(sqlite3*,const char*,int,int,void*,
! 90932: void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
! 90933: void (*xStep)(sqlite3_context*,int,sqlite3_value**),
! 90934: void (*xFinal)(sqlite3_context*));
! 90935: int (*create_function16)(sqlite3*,const void*,int,int,void*,
! 90936: void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
! 90937: void (*xStep)(sqlite3_context*,int,sqlite3_value**),
! 90938: void (*xFinal)(sqlite3_context*));
! 90939: int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
! 90940: int (*data_count)(sqlite3_stmt*pStmt);
! 90941: sqlite3 * (*db_handle)(sqlite3_stmt*);
! 90942: int (*declare_vtab)(sqlite3*,const char*);
! 90943: int (*enable_shared_cache)(int);
! 90944: int (*errcode)(sqlite3*db);
! 90945: const char * (*errmsg)(sqlite3*);
! 90946: const void * (*errmsg16)(sqlite3*);
! 90947: int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
! 90948: int (*expired)(sqlite3_stmt*);
! 90949: int (*finalize)(sqlite3_stmt*pStmt);
! 90950: void (*free)(void*);
! 90951: void (*free_table)(char**result);
! 90952: int (*get_autocommit)(sqlite3*);
! 90953: void * (*get_auxdata)(sqlite3_context*,int);
! 90954: int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
! 90955: int (*global_recover)(void);
! 90956: void (*interruptx)(sqlite3*);
! 90957: sqlite_int64 (*last_insert_rowid)(sqlite3*);
! 90958: const char * (*libversion)(void);
! 90959: int (*libversion_number)(void);
! 90960: void *(*malloc)(int);
! 90961: char * (*mprintf)(const char*,...);
! 90962: int (*open)(const char*,sqlite3**);
! 90963: int (*open16)(const void*,sqlite3**);
! 90964: int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
! 90965: int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
! 90966: void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
! 90967: void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
! 90968: void *(*realloc)(void*,int);
! 90969: int (*reset)(sqlite3_stmt*pStmt);
! 90970: void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
! 90971: void (*result_double)(sqlite3_context*,double);
! 90972: void (*result_error)(sqlite3_context*,const char*,int);
! 90973: void (*result_error16)(sqlite3_context*,const void*,int);
! 90974: void (*result_int)(sqlite3_context*,int);
! 90975: void (*result_int64)(sqlite3_context*,sqlite_int64);
! 90976: void (*result_null)(sqlite3_context*);
! 90977: void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
! 90978: void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
! 90979: void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
! 90980: void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
! 90981: void (*result_value)(sqlite3_context*,sqlite3_value*);
! 90982: void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
! 90983: int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
! 90984: const char*,const char*),void*);
! 90985: void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
! 90986: char * (*snprintf)(int,char*,const char*,...);
! 90987: int (*step)(sqlite3_stmt*);
! 90988: int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
! 90989: char const**,char const**,int*,int*,int*);
! 90990: void (*thread_cleanup)(void);
! 90991: int (*total_changes)(sqlite3*);
! 90992: void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
! 90993: int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
! 90994: void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
! 90995: sqlite_int64),void*);
! 90996: void * (*user_data)(sqlite3_context*);
! 90997: const void * (*value_blob)(sqlite3_value*);
! 90998: int (*value_bytes)(sqlite3_value*);
! 90999: int (*value_bytes16)(sqlite3_value*);
! 91000: double (*value_double)(sqlite3_value*);
! 91001: int (*value_int)(sqlite3_value*);
! 91002: sqlite_int64 (*value_int64)(sqlite3_value*);
! 91003: int (*value_numeric_type)(sqlite3_value*);
! 91004: const unsigned char * (*value_text)(sqlite3_value*);
! 91005: const void * (*value_text16)(sqlite3_value*);
! 91006: const void * (*value_text16be)(sqlite3_value*);
! 91007: const void * (*value_text16le)(sqlite3_value*);
! 91008: int (*value_type)(sqlite3_value*);
! 91009: char *(*vmprintf)(const char*,va_list);
! 91010: /* Added ??? */
! 91011: int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
! 91012: /* Added by 3.3.13 */
! 91013: int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
! 91014: int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
! 91015: int (*clear_bindings)(sqlite3_stmt*);
! 91016: /* Added by 3.4.1 */
! 91017: int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
! 91018: void (*xDestroy)(void *));
! 91019: /* Added by 3.5.0 */
! 91020: int (*bind_zeroblob)(sqlite3_stmt*,int,int);
! 91021: int (*blob_bytes)(sqlite3_blob*);
! 91022: int (*blob_close)(sqlite3_blob*);
! 91023: int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
! 91024: int,sqlite3_blob**);
! 91025: int (*blob_read)(sqlite3_blob*,void*,int,int);
! 91026: int (*blob_write)(sqlite3_blob*,const void*,int,int);
! 91027: int (*create_collation_v2)(sqlite3*,const char*,int,void*,
! 91028: int(*)(void*,int,const void*,int,const void*),
! 91029: void(*)(void*));
! 91030: int (*file_control)(sqlite3*,const char*,int,void*);
! 91031: sqlite3_int64 (*memory_highwater)(int);
! 91032: sqlite3_int64 (*memory_used)(void);
! 91033: sqlite3_mutex *(*mutex_alloc)(int);
! 91034: void (*mutex_enter)(sqlite3_mutex*);
! 91035: void (*mutex_free)(sqlite3_mutex*);
! 91036: void (*mutex_leave)(sqlite3_mutex*);
! 91037: int (*mutex_try)(sqlite3_mutex*);
! 91038: int (*open_v2)(const char*,sqlite3**,int,const char*);
! 91039: int (*release_memory)(int);
! 91040: void (*result_error_nomem)(sqlite3_context*);
! 91041: void (*result_error_toobig)(sqlite3_context*);
! 91042: int (*sleep)(int);
! 91043: void (*soft_heap_limit)(int);
! 91044: sqlite3_vfs *(*vfs_find)(const char*);
! 91045: int (*vfs_register)(sqlite3_vfs*,int);
! 91046: int (*vfs_unregister)(sqlite3_vfs*);
! 91047: int (*xthreadsafe)(void);
! 91048: void (*result_zeroblob)(sqlite3_context*,int);
! 91049: void (*result_error_code)(sqlite3_context*,int);
! 91050: int (*test_control)(int, ...);
! 91051: void (*randomness)(int,void*);
! 91052: sqlite3 *(*context_db_handle)(sqlite3_context*);
! 91053: int (*extended_result_codes)(sqlite3*,int);
! 91054: int (*limit)(sqlite3*,int,int);
! 91055: sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
! 91056: const char *(*sql)(sqlite3_stmt*);
! 91057: int (*status)(int,int*,int*,int);
! 91058: int (*backup_finish)(sqlite3_backup*);
! 91059: sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
! 91060: int (*backup_pagecount)(sqlite3_backup*);
! 91061: int (*backup_remaining)(sqlite3_backup*);
! 91062: int (*backup_step)(sqlite3_backup*,int);
! 91063: const char *(*compileoption_get)(int);
! 91064: int (*compileoption_used)(const char*);
! 91065: int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
! 91066: void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
! 91067: void (*xStep)(sqlite3_context*,int,sqlite3_value**),
! 91068: void (*xFinal)(sqlite3_context*),
! 91069: void(*xDestroy)(void*));
! 91070: int (*db_config)(sqlite3*,int,...);
! 91071: sqlite3_mutex *(*db_mutex)(sqlite3*);
! 91072: int (*db_status)(sqlite3*,int,int*,int*,int);
! 91073: int (*extended_errcode)(sqlite3*);
! 91074: void (*log)(int,const char*,...);
! 91075: sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
! 91076: const char *(*sourceid)(void);
! 91077: int (*stmt_status)(sqlite3_stmt*,int,int);
! 91078: int (*strnicmp)(const char*,const char*,int);
! 91079: int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
! 91080: int (*wal_autocheckpoint)(sqlite3*,int);
! 91081: int (*wal_checkpoint)(sqlite3*,const char*);
! 91082: void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
! 91083: int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
! 91084: int (*vtab_config)(sqlite3*,int op,...);
! 91085: int (*vtab_on_conflict)(sqlite3*);
! 91086: };
! 91087:
! 91088: /*
! 91089: ** The following macros redefine the API routines so that they are
! 91090: ** redirected throught the global sqlite3_api structure.
! 91091: **
! 91092: ** This header file is also used by the loadext.c source file
! 91093: ** (part of the main SQLite library - not an extension) so that
! 91094: ** it can get access to the sqlite3_api_routines structure
! 91095: ** definition. But the main library does not want to redefine
! 91096: ** the API. So the redefinition macros are only valid if the
! 91097: ** SQLITE_CORE macros is undefined.
! 91098: */
! 91099: #ifndef SQLITE_CORE
! 91100: #define sqlite3_aggregate_context sqlite3_api->aggregate_context
! 91101: #ifndef SQLITE_OMIT_DEPRECATED
! 91102: #define sqlite3_aggregate_count sqlite3_api->aggregate_count
! 91103: #endif
! 91104: #define sqlite3_bind_blob sqlite3_api->bind_blob
! 91105: #define sqlite3_bind_double sqlite3_api->bind_double
! 91106: #define sqlite3_bind_int sqlite3_api->bind_int
! 91107: #define sqlite3_bind_int64 sqlite3_api->bind_int64
! 91108: #define sqlite3_bind_null sqlite3_api->bind_null
! 91109: #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
! 91110: #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
! 91111: #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
! 91112: #define sqlite3_bind_text sqlite3_api->bind_text
! 91113: #define sqlite3_bind_text16 sqlite3_api->bind_text16
! 91114: #define sqlite3_bind_value sqlite3_api->bind_value
! 91115: #define sqlite3_busy_handler sqlite3_api->busy_handler
! 91116: #define sqlite3_busy_timeout sqlite3_api->busy_timeout
! 91117: #define sqlite3_changes sqlite3_api->changes
! 91118: #define sqlite3_close sqlite3_api->close
! 91119: #define sqlite3_collation_needed sqlite3_api->collation_needed
! 91120: #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
! 91121: #define sqlite3_column_blob sqlite3_api->column_blob
! 91122: #define sqlite3_column_bytes sqlite3_api->column_bytes
! 91123: #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
! 91124: #define sqlite3_column_count sqlite3_api->column_count
! 91125: #define sqlite3_column_database_name sqlite3_api->column_database_name
! 91126: #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
! 91127: #define sqlite3_column_decltype sqlite3_api->column_decltype
! 91128: #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
! 91129: #define sqlite3_column_double sqlite3_api->column_double
! 91130: #define sqlite3_column_int sqlite3_api->column_int
! 91131: #define sqlite3_column_int64 sqlite3_api->column_int64
! 91132: #define sqlite3_column_name sqlite3_api->column_name
! 91133: #define sqlite3_column_name16 sqlite3_api->column_name16
! 91134: #define sqlite3_column_origin_name sqlite3_api->column_origin_name
! 91135: #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
! 91136: #define sqlite3_column_table_name sqlite3_api->column_table_name
! 91137: #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
! 91138: #define sqlite3_column_text sqlite3_api->column_text
! 91139: #define sqlite3_column_text16 sqlite3_api->column_text16
! 91140: #define sqlite3_column_type sqlite3_api->column_type
! 91141: #define sqlite3_column_value sqlite3_api->column_value
! 91142: #define sqlite3_commit_hook sqlite3_api->commit_hook
! 91143: #define sqlite3_complete sqlite3_api->complete
! 91144: #define sqlite3_complete16 sqlite3_api->complete16
! 91145: #define sqlite3_create_collation sqlite3_api->create_collation
! 91146: #define sqlite3_create_collation16 sqlite3_api->create_collation16
! 91147: #define sqlite3_create_function sqlite3_api->create_function
! 91148: #define sqlite3_create_function16 sqlite3_api->create_function16
! 91149: #define sqlite3_create_module sqlite3_api->create_module
! 91150: #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
! 91151: #define sqlite3_data_count sqlite3_api->data_count
! 91152: #define sqlite3_db_handle sqlite3_api->db_handle
! 91153: #define sqlite3_declare_vtab sqlite3_api->declare_vtab
! 91154: #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
! 91155: #define sqlite3_errcode sqlite3_api->errcode
! 91156: #define sqlite3_errmsg sqlite3_api->errmsg
! 91157: #define sqlite3_errmsg16 sqlite3_api->errmsg16
! 91158: #define sqlite3_exec sqlite3_api->exec
! 91159: #ifndef SQLITE_OMIT_DEPRECATED
! 91160: #define sqlite3_expired sqlite3_api->expired
! 91161: #endif
! 91162: #define sqlite3_finalize sqlite3_api->finalize
! 91163: #define sqlite3_free sqlite3_api->free
! 91164: #define sqlite3_free_table sqlite3_api->free_table
! 91165: #define sqlite3_get_autocommit sqlite3_api->get_autocommit
! 91166: #define sqlite3_get_auxdata sqlite3_api->get_auxdata
! 91167: #define sqlite3_get_table sqlite3_api->get_table
! 91168: #ifndef SQLITE_OMIT_DEPRECATED
! 91169: #define sqlite3_global_recover sqlite3_api->global_recover
! 91170: #endif
! 91171: #define sqlite3_interrupt sqlite3_api->interruptx
! 91172: #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
! 91173: #define sqlite3_libversion sqlite3_api->libversion
! 91174: #define sqlite3_libversion_number sqlite3_api->libversion_number
! 91175: #define sqlite3_malloc sqlite3_api->malloc
! 91176: #define sqlite3_mprintf sqlite3_api->mprintf
! 91177: #define sqlite3_open sqlite3_api->open
! 91178: #define sqlite3_open16 sqlite3_api->open16
! 91179: #define sqlite3_prepare sqlite3_api->prepare
! 91180: #define sqlite3_prepare16 sqlite3_api->prepare16
! 91181: #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
! 91182: #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
! 91183: #define sqlite3_profile sqlite3_api->profile
! 91184: #define sqlite3_progress_handler sqlite3_api->progress_handler
! 91185: #define sqlite3_realloc sqlite3_api->realloc
! 91186: #define sqlite3_reset sqlite3_api->reset
! 91187: #define sqlite3_result_blob sqlite3_api->result_blob
! 91188: #define sqlite3_result_double sqlite3_api->result_double
! 91189: #define sqlite3_result_error sqlite3_api->result_error
! 91190: #define sqlite3_result_error16 sqlite3_api->result_error16
! 91191: #define sqlite3_result_int sqlite3_api->result_int
! 91192: #define sqlite3_result_int64 sqlite3_api->result_int64
! 91193: #define sqlite3_result_null sqlite3_api->result_null
! 91194: #define sqlite3_result_text sqlite3_api->result_text
! 91195: #define sqlite3_result_text16 sqlite3_api->result_text16
! 91196: #define sqlite3_result_text16be sqlite3_api->result_text16be
! 91197: #define sqlite3_result_text16le sqlite3_api->result_text16le
! 91198: #define sqlite3_result_value sqlite3_api->result_value
! 91199: #define sqlite3_rollback_hook sqlite3_api->rollback_hook
! 91200: #define sqlite3_set_authorizer sqlite3_api->set_authorizer
! 91201: #define sqlite3_set_auxdata sqlite3_api->set_auxdata
! 91202: #define sqlite3_snprintf sqlite3_api->snprintf
! 91203: #define sqlite3_step sqlite3_api->step
! 91204: #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
! 91205: #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
! 91206: #define sqlite3_total_changes sqlite3_api->total_changes
! 91207: #define sqlite3_trace sqlite3_api->trace
! 91208: #ifndef SQLITE_OMIT_DEPRECATED
! 91209: #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
! 91210: #endif
! 91211: #define sqlite3_update_hook sqlite3_api->update_hook
! 91212: #define sqlite3_user_data sqlite3_api->user_data
! 91213: #define sqlite3_value_blob sqlite3_api->value_blob
! 91214: #define sqlite3_value_bytes sqlite3_api->value_bytes
! 91215: #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
! 91216: #define sqlite3_value_double sqlite3_api->value_double
! 91217: #define sqlite3_value_int sqlite3_api->value_int
! 91218: #define sqlite3_value_int64 sqlite3_api->value_int64
! 91219: #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
! 91220: #define sqlite3_value_text sqlite3_api->value_text
! 91221: #define sqlite3_value_text16 sqlite3_api->value_text16
! 91222: #define sqlite3_value_text16be sqlite3_api->value_text16be
! 91223: #define sqlite3_value_text16le sqlite3_api->value_text16le
! 91224: #define sqlite3_value_type sqlite3_api->value_type
! 91225: #define sqlite3_vmprintf sqlite3_api->vmprintf
! 91226: #define sqlite3_overload_function sqlite3_api->overload_function
! 91227: #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
! 91228: #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
! 91229: #define sqlite3_clear_bindings sqlite3_api->clear_bindings
! 91230: #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
! 91231: #define sqlite3_blob_bytes sqlite3_api->blob_bytes
! 91232: #define sqlite3_blob_close sqlite3_api->blob_close
! 91233: #define sqlite3_blob_open sqlite3_api->blob_open
! 91234: #define sqlite3_blob_read sqlite3_api->blob_read
! 91235: #define sqlite3_blob_write sqlite3_api->blob_write
! 91236: #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
! 91237: #define sqlite3_file_control sqlite3_api->file_control
! 91238: #define sqlite3_memory_highwater sqlite3_api->memory_highwater
! 91239: #define sqlite3_memory_used sqlite3_api->memory_used
! 91240: #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
! 91241: #define sqlite3_mutex_enter sqlite3_api->mutex_enter
! 91242: #define sqlite3_mutex_free sqlite3_api->mutex_free
! 91243: #define sqlite3_mutex_leave sqlite3_api->mutex_leave
! 91244: #define sqlite3_mutex_try sqlite3_api->mutex_try
! 91245: #define sqlite3_open_v2 sqlite3_api->open_v2
! 91246: #define sqlite3_release_memory sqlite3_api->release_memory
! 91247: #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
! 91248: #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
! 91249: #define sqlite3_sleep sqlite3_api->sleep
! 91250: #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
! 91251: #define sqlite3_vfs_find sqlite3_api->vfs_find
! 91252: #define sqlite3_vfs_register sqlite3_api->vfs_register
! 91253: #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
! 91254: #define sqlite3_threadsafe sqlite3_api->xthreadsafe
! 91255: #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
! 91256: #define sqlite3_result_error_code sqlite3_api->result_error_code
! 91257: #define sqlite3_test_control sqlite3_api->test_control
! 91258: #define sqlite3_randomness sqlite3_api->randomness
! 91259: #define sqlite3_context_db_handle sqlite3_api->context_db_handle
! 91260: #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
! 91261: #define sqlite3_limit sqlite3_api->limit
! 91262: #define sqlite3_next_stmt sqlite3_api->next_stmt
! 91263: #define sqlite3_sql sqlite3_api->sql
! 91264: #define sqlite3_status sqlite3_api->status
! 91265: #define sqlite3_backup_finish sqlite3_api->backup_finish
! 91266: #define sqlite3_backup_init sqlite3_api->backup_init
! 91267: #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
! 91268: #define sqlite3_backup_remaining sqlite3_api->backup_remaining
! 91269: #define sqlite3_backup_step sqlite3_api->backup_step
! 91270: #define sqlite3_compileoption_get sqlite3_api->compileoption_get
! 91271: #define sqlite3_compileoption_used sqlite3_api->compileoption_used
! 91272: #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
! 91273: #define sqlite3_db_config sqlite3_api->db_config
! 91274: #define sqlite3_db_mutex sqlite3_api->db_mutex
! 91275: #define sqlite3_db_status sqlite3_api->db_status
! 91276: #define sqlite3_extended_errcode sqlite3_api->extended_errcode
! 91277: #define sqlite3_log sqlite3_api->log
! 91278: #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
! 91279: #define sqlite3_sourceid sqlite3_api->sourceid
! 91280: #define sqlite3_stmt_status sqlite3_api->stmt_status
! 91281: #define sqlite3_strnicmp sqlite3_api->strnicmp
! 91282: #define sqlite3_unlock_notify sqlite3_api->unlock_notify
! 91283: #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
! 91284: #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
! 91285: #define sqlite3_wal_hook sqlite3_api->wal_hook
! 91286: #define sqlite3_blob_reopen sqlite3_api->blob_reopen
! 91287: #define sqlite3_vtab_config sqlite3_api->vtab_config
! 91288: #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
! 91289: #endif /* SQLITE_CORE */
! 91290:
! 91291: #define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api = 0;
! 91292: #define SQLITE_EXTENSION_INIT2(v) sqlite3_api = v;
! 91293:
! 91294: #endif /* _SQLITE3EXT_H_ */
! 91295:
! 91296: /************** End of sqlite3ext.h ******************************************/
! 91297: /************** Continuing where we left off in loadext.c ********************/
! 91298: /* #include <string.h> */
! 91299:
! 91300: #ifndef SQLITE_OMIT_LOAD_EXTENSION
! 91301:
! 91302: /*
! 91303: ** Some API routines are omitted when various features are
! 91304: ** excluded from a build of SQLite. Substitute a NULL pointer
! 91305: ** for any missing APIs.
! 91306: */
! 91307: #ifndef SQLITE_ENABLE_COLUMN_METADATA
! 91308: # define sqlite3_column_database_name 0
! 91309: # define sqlite3_column_database_name16 0
! 91310: # define sqlite3_column_table_name 0
! 91311: # define sqlite3_column_table_name16 0
! 91312: # define sqlite3_column_origin_name 0
! 91313: # define sqlite3_column_origin_name16 0
! 91314: # define sqlite3_table_column_metadata 0
! 91315: #endif
! 91316:
! 91317: #ifdef SQLITE_OMIT_AUTHORIZATION
! 91318: # define sqlite3_set_authorizer 0
! 91319: #endif
! 91320:
! 91321: #ifdef SQLITE_OMIT_UTF16
! 91322: # define sqlite3_bind_text16 0
! 91323: # define sqlite3_collation_needed16 0
! 91324: # define sqlite3_column_decltype16 0
! 91325: # define sqlite3_column_name16 0
! 91326: # define sqlite3_column_text16 0
! 91327: # define sqlite3_complete16 0
! 91328: # define sqlite3_create_collation16 0
! 91329: # define sqlite3_create_function16 0
! 91330: # define sqlite3_errmsg16 0
! 91331: # define sqlite3_open16 0
! 91332: # define sqlite3_prepare16 0
! 91333: # define sqlite3_prepare16_v2 0
! 91334: # define sqlite3_result_error16 0
! 91335: # define sqlite3_result_text16 0
! 91336: # define sqlite3_result_text16be 0
! 91337: # define sqlite3_result_text16le 0
! 91338: # define sqlite3_value_text16 0
! 91339: # define sqlite3_value_text16be 0
! 91340: # define sqlite3_value_text16le 0
! 91341: # define sqlite3_column_database_name16 0
! 91342: # define sqlite3_column_table_name16 0
! 91343: # define sqlite3_column_origin_name16 0
! 91344: #endif
! 91345:
! 91346: #ifdef SQLITE_OMIT_COMPLETE
! 91347: # define sqlite3_complete 0
! 91348: # define sqlite3_complete16 0
! 91349: #endif
! 91350:
! 91351: #ifdef SQLITE_OMIT_DECLTYPE
! 91352: # define sqlite3_column_decltype16 0
! 91353: # define sqlite3_column_decltype 0
! 91354: #endif
! 91355:
! 91356: #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
! 91357: # define sqlite3_progress_handler 0
! 91358: #endif
! 91359:
! 91360: #ifdef SQLITE_OMIT_VIRTUALTABLE
! 91361: # define sqlite3_create_module 0
! 91362: # define sqlite3_create_module_v2 0
! 91363: # define sqlite3_declare_vtab 0
! 91364: # define sqlite3_vtab_config 0
! 91365: # define sqlite3_vtab_on_conflict 0
! 91366: #endif
! 91367:
! 91368: #ifdef SQLITE_OMIT_SHARED_CACHE
! 91369: # define sqlite3_enable_shared_cache 0
! 91370: #endif
! 91371:
! 91372: #ifdef SQLITE_OMIT_TRACE
! 91373: # define sqlite3_profile 0
! 91374: # define sqlite3_trace 0
! 91375: #endif
! 91376:
! 91377: #ifdef SQLITE_OMIT_GET_TABLE
! 91378: # define sqlite3_free_table 0
! 91379: # define sqlite3_get_table 0
! 91380: #endif
! 91381:
! 91382: #ifdef SQLITE_OMIT_INCRBLOB
! 91383: #define sqlite3_bind_zeroblob 0
! 91384: #define sqlite3_blob_bytes 0
! 91385: #define sqlite3_blob_close 0
! 91386: #define sqlite3_blob_open 0
! 91387: #define sqlite3_blob_read 0
! 91388: #define sqlite3_blob_write 0
! 91389: #define sqlite3_blob_reopen 0
! 91390: #endif
! 91391:
! 91392: /*
! 91393: ** The following structure contains pointers to all SQLite API routines.
! 91394: ** A pointer to this structure is passed into extensions when they are
! 91395: ** loaded so that the extension can make calls back into the SQLite
! 91396: ** library.
! 91397: **
! 91398: ** When adding new APIs, add them to the bottom of this structure
! 91399: ** in order to preserve backwards compatibility.
! 91400: **
! 91401: ** Extensions that use newer APIs should first call the
! 91402: ** sqlite3_libversion_number() to make sure that the API they
! 91403: ** intend to use is supported by the library. Extensions should
! 91404: ** also check to make sure that the pointer to the function is
! 91405: ** not NULL before calling it.
! 91406: */
! 91407: static const sqlite3_api_routines sqlite3Apis = {
! 91408: sqlite3_aggregate_context,
! 91409: #ifndef SQLITE_OMIT_DEPRECATED
! 91410: sqlite3_aggregate_count,
! 91411: #else
! 91412: 0,
! 91413: #endif
! 91414: sqlite3_bind_blob,
! 91415: sqlite3_bind_double,
! 91416: sqlite3_bind_int,
! 91417: sqlite3_bind_int64,
! 91418: sqlite3_bind_null,
! 91419: sqlite3_bind_parameter_count,
! 91420: sqlite3_bind_parameter_index,
! 91421: sqlite3_bind_parameter_name,
! 91422: sqlite3_bind_text,
! 91423: sqlite3_bind_text16,
! 91424: sqlite3_bind_value,
! 91425: sqlite3_busy_handler,
! 91426: sqlite3_busy_timeout,
! 91427: sqlite3_changes,
! 91428: sqlite3_close,
! 91429: sqlite3_collation_needed,
! 91430: sqlite3_collation_needed16,
! 91431: sqlite3_column_blob,
! 91432: sqlite3_column_bytes,
! 91433: sqlite3_column_bytes16,
! 91434: sqlite3_column_count,
! 91435: sqlite3_column_database_name,
! 91436: sqlite3_column_database_name16,
! 91437: sqlite3_column_decltype,
! 91438: sqlite3_column_decltype16,
! 91439: sqlite3_column_double,
! 91440: sqlite3_column_int,
! 91441: sqlite3_column_int64,
! 91442: sqlite3_column_name,
! 91443: sqlite3_column_name16,
! 91444: sqlite3_column_origin_name,
! 91445: sqlite3_column_origin_name16,
! 91446: sqlite3_column_table_name,
! 91447: sqlite3_column_table_name16,
! 91448: sqlite3_column_text,
! 91449: sqlite3_column_text16,
! 91450: sqlite3_column_type,
! 91451: sqlite3_column_value,
! 91452: sqlite3_commit_hook,
! 91453: sqlite3_complete,
! 91454: sqlite3_complete16,
! 91455: sqlite3_create_collation,
! 91456: sqlite3_create_collation16,
! 91457: sqlite3_create_function,
! 91458: sqlite3_create_function16,
! 91459: sqlite3_create_module,
! 91460: sqlite3_data_count,
! 91461: sqlite3_db_handle,
! 91462: sqlite3_declare_vtab,
! 91463: sqlite3_enable_shared_cache,
! 91464: sqlite3_errcode,
! 91465: sqlite3_errmsg,
! 91466: sqlite3_errmsg16,
! 91467: sqlite3_exec,
! 91468: #ifndef SQLITE_OMIT_DEPRECATED
! 91469: sqlite3_expired,
! 91470: #else
! 91471: 0,
! 91472: #endif
! 91473: sqlite3_finalize,
! 91474: sqlite3_free,
! 91475: sqlite3_free_table,
! 91476: sqlite3_get_autocommit,
! 91477: sqlite3_get_auxdata,
! 91478: sqlite3_get_table,
! 91479: 0, /* Was sqlite3_global_recover(), but that function is deprecated */
! 91480: sqlite3_interrupt,
! 91481: sqlite3_last_insert_rowid,
! 91482: sqlite3_libversion,
! 91483: sqlite3_libversion_number,
! 91484: sqlite3_malloc,
! 91485: sqlite3_mprintf,
! 91486: sqlite3_open,
! 91487: sqlite3_open16,
! 91488: sqlite3_prepare,
! 91489: sqlite3_prepare16,
! 91490: sqlite3_profile,
! 91491: sqlite3_progress_handler,
! 91492: sqlite3_realloc,
! 91493: sqlite3_reset,
! 91494: sqlite3_result_blob,
! 91495: sqlite3_result_double,
! 91496: sqlite3_result_error,
! 91497: sqlite3_result_error16,
! 91498: sqlite3_result_int,
! 91499: sqlite3_result_int64,
! 91500: sqlite3_result_null,
! 91501: sqlite3_result_text,
! 91502: sqlite3_result_text16,
! 91503: sqlite3_result_text16be,
! 91504: sqlite3_result_text16le,
! 91505: sqlite3_result_value,
! 91506: sqlite3_rollback_hook,
! 91507: sqlite3_set_authorizer,
! 91508: sqlite3_set_auxdata,
! 91509: sqlite3_snprintf,
! 91510: sqlite3_step,
! 91511: sqlite3_table_column_metadata,
! 91512: #ifndef SQLITE_OMIT_DEPRECATED
! 91513: sqlite3_thread_cleanup,
! 91514: #else
! 91515: 0,
! 91516: #endif
! 91517: sqlite3_total_changes,
! 91518: sqlite3_trace,
! 91519: #ifndef SQLITE_OMIT_DEPRECATED
! 91520: sqlite3_transfer_bindings,
! 91521: #else
! 91522: 0,
! 91523: #endif
! 91524: sqlite3_update_hook,
! 91525: sqlite3_user_data,
! 91526: sqlite3_value_blob,
! 91527: sqlite3_value_bytes,
! 91528: sqlite3_value_bytes16,
! 91529: sqlite3_value_double,
! 91530: sqlite3_value_int,
! 91531: sqlite3_value_int64,
! 91532: sqlite3_value_numeric_type,
! 91533: sqlite3_value_text,
! 91534: sqlite3_value_text16,
! 91535: sqlite3_value_text16be,
! 91536: sqlite3_value_text16le,
! 91537: sqlite3_value_type,
! 91538: sqlite3_vmprintf,
! 91539: /*
! 91540: ** The original API set ends here. All extensions can call any
! 91541: ** of the APIs above provided that the pointer is not NULL. But
! 91542: ** before calling APIs that follow, extension should check the
! 91543: ** sqlite3_libversion_number() to make sure they are dealing with
! 91544: ** a library that is new enough to support that API.
! 91545: *************************************************************************
! 91546: */
! 91547: sqlite3_overload_function,
! 91548:
! 91549: /*
! 91550: ** Added after 3.3.13
! 91551: */
! 91552: sqlite3_prepare_v2,
! 91553: sqlite3_prepare16_v2,
! 91554: sqlite3_clear_bindings,
! 91555:
! 91556: /*
! 91557: ** Added for 3.4.1
! 91558: */
! 91559: sqlite3_create_module_v2,
! 91560:
! 91561: /*
! 91562: ** Added for 3.5.0
! 91563: */
! 91564: sqlite3_bind_zeroblob,
! 91565: sqlite3_blob_bytes,
! 91566: sqlite3_blob_close,
! 91567: sqlite3_blob_open,
! 91568: sqlite3_blob_read,
! 91569: sqlite3_blob_write,
! 91570: sqlite3_create_collation_v2,
! 91571: sqlite3_file_control,
! 91572: sqlite3_memory_highwater,
! 91573: sqlite3_memory_used,
! 91574: #ifdef SQLITE_MUTEX_OMIT
! 91575: 0,
! 91576: 0,
! 91577: 0,
! 91578: 0,
! 91579: 0,
! 91580: #else
! 91581: sqlite3_mutex_alloc,
! 91582: sqlite3_mutex_enter,
! 91583: sqlite3_mutex_free,
! 91584: sqlite3_mutex_leave,
! 91585: sqlite3_mutex_try,
! 91586: #endif
! 91587: sqlite3_open_v2,
! 91588: sqlite3_release_memory,
! 91589: sqlite3_result_error_nomem,
! 91590: sqlite3_result_error_toobig,
! 91591: sqlite3_sleep,
! 91592: sqlite3_soft_heap_limit,
! 91593: sqlite3_vfs_find,
! 91594: sqlite3_vfs_register,
! 91595: sqlite3_vfs_unregister,
! 91596:
! 91597: /*
! 91598: ** Added for 3.5.8
! 91599: */
! 91600: sqlite3_threadsafe,
! 91601: sqlite3_result_zeroblob,
! 91602: sqlite3_result_error_code,
! 91603: sqlite3_test_control,
! 91604: sqlite3_randomness,
! 91605: sqlite3_context_db_handle,
! 91606:
! 91607: /*
! 91608: ** Added for 3.6.0
! 91609: */
! 91610: sqlite3_extended_result_codes,
! 91611: sqlite3_limit,
! 91612: sqlite3_next_stmt,
! 91613: sqlite3_sql,
! 91614: sqlite3_status,
! 91615:
! 91616: /*
! 91617: ** Added for 3.7.4
! 91618: */
! 91619: sqlite3_backup_finish,
! 91620: sqlite3_backup_init,
! 91621: sqlite3_backup_pagecount,
! 91622: sqlite3_backup_remaining,
! 91623: sqlite3_backup_step,
! 91624: #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
! 91625: sqlite3_compileoption_get,
! 91626: sqlite3_compileoption_used,
! 91627: #else
! 91628: 0,
! 91629: 0,
! 91630: #endif
! 91631: sqlite3_create_function_v2,
! 91632: sqlite3_db_config,
! 91633: sqlite3_db_mutex,
! 91634: sqlite3_db_status,
! 91635: sqlite3_extended_errcode,
! 91636: sqlite3_log,
! 91637: sqlite3_soft_heap_limit64,
! 91638: sqlite3_sourceid,
! 91639: sqlite3_stmt_status,
! 91640: sqlite3_strnicmp,
! 91641: #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
! 91642: sqlite3_unlock_notify,
! 91643: #else
! 91644: 0,
! 91645: #endif
! 91646: #ifndef SQLITE_OMIT_WAL
! 91647: sqlite3_wal_autocheckpoint,
! 91648: sqlite3_wal_checkpoint,
! 91649: sqlite3_wal_hook,
! 91650: #else
! 91651: 0,
! 91652: 0,
! 91653: 0,
! 91654: #endif
! 91655: sqlite3_blob_reopen,
! 91656: sqlite3_vtab_config,
! 91657: sqlite3_vtab_on_conflict,
! 91658: };
! 91659:
! 91660: /*
! 91661: ** Attempt to load an SQLite extension library contained in the file
! 91662: ** zFile. The entry point is zProc. zProc may be 0 in which case a
! 91663: ** default entry point name (sqlite3_extension_init) is used. Use
! 91664: ** of the default name is recommended.
! 91665: **
! 91666: ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
! 91667: **
! 91668: ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
! 91669: ** error message text. The calling function should free this memory
! 91670: ** by calling sqlite3DbFree(db, ).
! 91671: */
! 91672: static int sqlite3LoadExtension(
! 91673: sqlite3 *db, /* Load the extension into this database connection */
! 91674: const char *zFile, /* Name of the shared library containing extension */
! 91675: const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
! 91676: char **pzErrMsg /* Put error message here if not 0 */
! 91677: ){
! 91678: sqlite3_vfs *pVfs = db->pVfs;
! 91679: void *handle;
! 91680: int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
! 91681: char *zErrmsg = 0;
! 91682: void **aHandle;
! 91683: int nMsg = 300 + sqlite3Strlen30(zFile);
! 91684:
! 91685: if( pzErrMsg ) *pzErrMsg = 0;
! 91686:
! 91687: /* Ticket #1863. To avoid a creating security problems for older
! 91688: ** applications that relink against newer versions of SQLite, the
! 91689: ** ability to run load_extension is turned off by default. One
! 91690: ** must call sqlite3_enable_load_extension() to turn on extension
! 91691: ** loading. Otherwise you get the following error.
! 91692: */
! 91693: if( (db->flags & SQLITE_LoadExtension)==0 ){
! 91694: if( pzErrMsg ){
! 91695: *pzErrMsg = sqlite3_mprintf("not authorized");
! 91696: }
! 91697: return SQLITE_ERROR;
! 91698: }
! 91699:
! 91700: if( zProc==0 ){
! 91701: zProc = "sqlite3_extension_init";
! 91702: }
! 91703:
! 91704: handle = sqlite3OsDlOpen(pVfs, zFile);
! 91705: if( handle==0 ){
! 91706: if( pzErrMsg ){
! 91707: *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
! 91708: if( zErrmsg ){
! 91709: sqlite3_snprintf(nMsg, zErrmsg,
! 91710: "unable to open shared library [%s]", zFile);
! 91711: sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
! 91712: }
! 91713: }
! 91714: return SQLITE_ERROR;
! 91715: }
! 91716: xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
! 91717: sqlite3OsDlSym(pVfs, handle, zProc);
! 91718: if( xInit==0 ){
! 91719: if( pzErrMsg ){
! 91720: nMsg += sqlite3Strlen30(zProc);
! 91721: *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
! 91722: if( zErrmsg ){
! 91723: sqlite3_snprintf(nMsg, zErrmsg,
! 91724: "no entry point [%s] in shared library [%s]", zProc,zFile);
! 91725: sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
! 91726: }
! 91727: sqlite3OsDlClose(pVfs, handle);
! 91728: }
! 91729: return SQLITE_ERROR;
! 91730: }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
! 91731: if( pzErrMsg ){
! 91732: *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
! 91733: }
! 91734: sqlite3_free(zErrmsg);
! 91735: sqlite3OsDlClose(pVfs, handle);
! 91736: return SQLITE_ERROR;
! 91737: }
! 91738:
! 91739: /* Append the new shared library handle to the db->aExtension array. */
! 91740: aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
! 91741: if( aHandle==0 ){
! 91742: return SQLITE_NOMEM;
! 91743: }
! 91744: if( db->nExtension>0 ){
! 91745: memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
! 91746: }
! 91747: sqlite3DbFree(db, db->aExtension);
! 91748: db->aExtension = aHandle;
! 91749:
! 91750: db->aExtension[db->nExtension++] = handle;
! 91751: return SQLITE_OK;
! 91752: }
! 91753: SQLITE_API int sqlite3_load_extension(
! 91754: sqlite3 *db, /* Load the extension into this database connection */
! 91755: const char *zFile, /* Name of the shared library containing extension */
! 91756: const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
! 91757: char **pzErrMsg /* Put error message here if not 0 */
! 91758: ){
! 91759: int rc;
! 91760: sqlite3_mutex_enter(db->mutex);
! 91761: rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
! 91762: rc = sqlite3ApiExit(db, rc);
! 91763: sqlite3_mutex_leave(db->mutex);
! 91764: return rc;
! 91765: }
! 91766:
! 91767: /*
! 91768: ** Call this routine when the database connection is closing in order
! 91769: ** to clean up loaded extensions
! 91770: */
! 91771: SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
! 91772: int i;
! 91773: assert( sqlite3_mutex_held(db->mutex) );
! 91774: for(i=0; i<db->nExtension; i++){
! 91775: sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
! 91776: }
! 91777: sqlite3DbFree(db, db->aExtension);
! 91778: }
! 91779:
! 91780: /*
! 91781: ** Enable or disable extension loading. Extension loading is disabled by
! 91782: ** default so as not to open security holes in older applications.
! 91783: */
! 91784: SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
! 91785: sqlite3_mutex_enter(db->mutex);
! 91786: if( onoff ){
! 91787: db->flags |= SQLITE_LoadExtension;
! 91788: }else{
! 91789: db->flags &= ~SQLITE_LoadExtension;
! 91790: }
! 91791: sqlite3_mutex_leave(db->mutex);
! 91792: return SQLITE_OK;
! 91793: }
! 91794:
! 91795: #endif /* SQLITE_OMIT_LOAD_EXTENSION */
! 91796:
! 91797: /*
! 91798: ** The auto-extension code added regardless of whether or not extension
! 91799: ** loading is supported. We need a dummy sqlite3Apis pointer for that
! 91800: ** code if regular extension loading is not available. This is that
! 91801: ** dummy pointer.
! 91802: */
! 91803: #ifdef SQLITE_OMIT_LOAD_EXTENSION
! 91804: static const sqlite3_api_routines sqlite3Apis = { 0 };
! 91805: #endif
! 91806:
! 91807:
! 91808: /*
! 91809: ** The following object holds the list of automatically loaded
! 91810: ** extensions.
! 91811: **
! 91812: ** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER
! 91813: ** mutex must be held while accessing this list.
! 91814: */
! 91815: typedef struct sqlite3AutoExtList sqlite3AutoExtList;
! 91816: static SQLITE_WSD struct sqlite3AutoExtList {
! 91817: int nExt; /* Number of entries in aExt[] */
! 91818: void (**aExt)(void); /* Pointers to the extension init functions */
! 91819: } sqlite3Autoext = { 0, 0 };
! 91820:
! 91821: /* The "wsdAutoext" macro will resolve to the autoextension
! 91822: ** state vector. If writable static data is unsupported on the target,
! 91823: ** we have to locate the state vector at run-time. In the more common
! 91824: ** case where writable static data is supported, wsdStat can refer directly
! 91825: ** to the "sqlite3Autoext" state vector declared above.
! 91826: */
! 91827: #ifdef SQLITE_OMIT_WSD
! 91828: # define wsdAutoextInit \
! 91829: sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
! 91830: # define wsdAutoext x[0]
! 91831: #else
! 91832: # define wsdAutoextInit
! 91833: # define wsdAutoext sqlite3Autoext
! 91834: #endif
! 91835:
! 91836:
! 91837: /*
! 91838: ** Register a statically linked extension that is automatically
! 91839: ** loaded by every new database connection.
! 91840: */
! 91841: SQLITE_API int sqlite3_auto_extension(void (*xInit)(void)){
! 91842: int rc = SQLITE_OK;
! 91843: #ifndef SQLITE_OMIT_AUTOINIT
! 91844: rc = sqlite3_initialize();
! 91845: if( rc ){
! 91846: return rc;
! 91847: }else
! 91848: #endif
! 91849: {
! 91850: int i;
! 91851: #if SQLITE_THREADSAFE
! 91852: sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
! 91853: #endif
! 91854: wsdAutoextInit;
! 91855: sqlite3_mutex_enter(mutex);
! 91856: for(i=0; i<wsdAutoext.nExt; i++){
! 91857: if( wsdAutoext.aExt[i]==xInit ) break;
! 91858: }
! 91859: if( i==wsdAutoext.nExt ){
! 91860: int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
! 91861: void (**aNew)(void);
! 91862: aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
! 91863: if( aNew==0 ){
! 91864: rc = SQLITE_NOMEM;
! 91865: }else{
! 91866: wsdAutoext.aExt = aNew;
! 91867: wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
! 91868: wsdAutoext.nExt++;
! 91869: }
! 91870: }
! 91871: sqlite3_mutex_leave(mutex);
! 91872: assert( (rc&0xff)==rc );
! 91873: return rc;
! 91874: }
! 91875: }
! 91876:
! 91877: /*
! 91878: ** Reset the automatic extension loading mechanism.
! 91879: */
! 91880: SQLITE_API void sqlite3_reset_auto_extension(void){
! 91881: #ifndef SQLITE_OMIT_AUTOINIT
! 91882: if( sqlite3_initialize()==SQLITE_OK )
! 91883: #endif
! 91884: {
! 91885: #if SQLITE_THREADSAFE
! 91886: sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
! 91887: #endif
! 91888: wsdAutoextInit;
! 91889: sqlite3_mutex_enter(mutex);
! 91890: sqlite3_free(wsdAutoext.aExt);
! 91891: wsdAutoext.aExt = 0;
! 91892: wsdAutoext.nExt = 0;
! 91893: sqlite3_mutex_leave(mutex);
! 91894: }
! 91895: }
! 91896:
! 91897: /*
! 91898: ** Load all automatic extensions.
! 91899: **
! 91900: ** If anything goes wrong, set an error in the database connection.
! 91901: */
! 91902: SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
! 91903: int i;
! 91904: int go = 1;
! 91905: int rc;
! 91906: int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
! 91907:
! 91908: wsdAutoextInit;
! 91909: if( wsdAutoext.nExt==0 ){
! 91910: /* Common case: early out without every having to acquire a mutex */
! 91911: return;
! 91912: }
! 91913: for(i=0; go; i++){
! 91914: char *zErrmsg;
! 91915: #if SQLITE_THREADSAFE
! 91916: sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
! 91917: #endif
! 91918: sqlite3_mutex_enter(mutex);
! 91919: if( i>=wsdAutoext.nExt ){
! 91920: xInit = 0;
! 91921: go = 0;
! 91922: }else{
! 91923: xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
! 91924: wsdAutoext.aExt[i];
! 91925: }
! 91926: sqlite3_mutex_leave(mutex);
! 91927: zErrmsg = 0;
! 91928: if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
! 91929: sqlite3Error(db, rc,
! 91930: "automatic extension loading failed: %s", zErrmsg);
! 91931: go = 0;
! 91932: }
! 91933: sqlite3_free(zErrmsg);
! 91934: }
! 91935: }
! 91936:
! 91937: /************** End of loadext.c *********************************************/
! 91938: /************** Begin file pragma.c ******************************************/
! 91939: /*
! 91940: ** 2003 April 6
! 91941: **
! 91942: ** The author disclaims copyright to this source code. In place of
! 91943: ** a legal notice, here is a blessing:
! 91944: **
! 91945: ** May you do good and not evil.
! 91946: ** May you find forgiveness for yourself and forgive others.
! 91947: ** May you share freely, never taking more than you give.
! 91948: **
! 91949: *************************************************************************
! 91950: ** This file contains code used to implement the PRAGMA command.
! 91951: */
! 91952:
! 91953: /*
! 91954: ** Interpret the given string as a safety level. Return 0 for OFF,
! 91955: ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
! 91956: ** unrecognized string argument.
! 91957: **
! 91958: ** Note that the values returned are one less that the values that
! 91959: ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
! 91960: ** to support legacy SQL code. The safety level used to be boolean
! 91961: ** and older scripts may have used numbers 0 for OFF and 1 for ON.
! 91962: */
! 91963: static u8 getSafetyLevel(const char *z){
! 91964: /* 123456789 123456789 */
! 91965: static const char zText[] = "onoffalseyestruefull";
! 91966: static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
! 91967: static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
! 91968: static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
! 91969: int i, n;
! 91970: if( sqlite3Isdigit(*z) ){
! 91971: return (u8)sqlite3Atoi(z);
! 91972: }
! 91973: n = sqlite3Strlen30(z);
! 91974: for(i=0; i<ArraySize(iLength); i++){
! 91975: if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
! 91976: return iValue[i];
! 91977: }
! 91978: }
! 91979: return 1;
! 91980: }
! 91981:
! 91982: /*
! 91983: ** Interpret the given string as a boolean value.
! 91984: */
! 91985: SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z){
! 91986: return getSafetyLevel(z)&1;
! 91987: }
! 91988:
! 91989: /* The sqlite3GetBoolean() function is used by other modules but the
! 91990: ** remainder of this file is specific to PRAGMA processing. So omit
! 91991: ** the rest of the file if PRAGMAs are omitted from the build.
! 91992: */
! 91993: #if !defined(SQLITE_OMIT_PRAGMA)
! 91994:
! 91995: /*
! 91996: ** Interpret the given string as a locking mode value.
! 91997: */
! 91998: static int getLockingMode(const char *z){
! 91999: if( z ){
! 92000: if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
! 92001: if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
! 92002: }
! 92003: return PAGER_LOCKINGMODE_QUERY;
! 92004: }
! 92005:
! 92006: #ifndef SQLITE_OMIT_AUTOVACUUM
! 92007: /*
! 92008: ** Interpret the given string as an auto-vacuum mode value.
! 92009: **
! 92010: ** The following strings, "none", "full" and "incremental" are
! 92011: ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
! 92012: */
! 92013: static int getAutoVacuum(const char *z){
! 92014: int i;
! 92015: if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
! 92016: if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
! 92017: if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
! 92018: i = sqlite3Atoi(z);
! 92019: return (u8)((i>=0&&i<=2)?i:0);
! 92020: }
! 92021: #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
! 92022:
! 92023: #ifndef SQLITE_OMIT_PAGER_PRAGMAS
! 92024: /*
! 92025: ** Interpret the given string as a temp db location. Return 1 for file
! 92026: ** backed temporary databases, 2 for the Red-Black tree in memory database
! 92027: ** and 0 to use the compile-time default.
! 92028: */
! 92029: static int getTempStore(const char *z){
! 92030: if( z[0]>='0' && z[0]<='2' ){
! 92031: return z[0] - '0';
! 92032: }else if( sqlite3StrICmp(z, "file")==0 ){
! 92033: return 1;
! 92034: }else if( sqlite3StrICmp(z, "memory")==0 ){
! 92035: return 2;
! 92036: }else{
! 92037: return 0;
! 92038: }
! 92039: }
! 92040: #endif /* SQLITE_PAGER_PRAGMAS */
! 92041:
! 92042: #ifndef SQLITE_OMIT_PAGER_PRAGMAS
! 92043: /*
! 92044: ** Invalidate temp storage, either when the temp storage is changed
! 92045: ** from default, or when 'file' and the temp_store_directory has changed
! 92046: */
! 92047: static int invalidateTempStorage(Parse *pParse){
! 92048: sqlite3 *db = pParse->db;
! 92049: if( db->aDb[1].pBt!=0 ){
! 92050: if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
! 92051: sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
! 92052: "from within a transaction");
! 92053: return SQLITE_ERROR;
! 92054: }
! 92055: sqlite3BtreeClose(db->aDb[1].pBt);
! 92056: db->aDb[1].pBt = 0;
! 92057: sqlite3ResetInternalSchema(db, -1);
! 92058: }
! 92059: return SQLITE_OK;
! 92060: }
! 92061: #endif /* SQLITE_PAGER_PRAGMAS */
! 92062:
! 92063: #ifndef SQLITE_OMIT_PAGER_PRAGMAS
! 92064: /*
! 92065: ** If the TEMP database is open, close it and mark the database schema
! 92066: ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
! 92067: ** or DEFAULT_TEMP_STORE pragmas.
! 92068: */
! 92069: static int changeTempStorage(Parse *pParse, const char *zStorageType){
! 92070: int ts = getTempStore(zStorageType);
! 92071: sqlite3 *db = pParse->db;
! 92072: if( db->temp_store==ts ) return SQLITE_OK;
! 92073: if( invalidateTempStorage( pParse ) != SQLITE_OK ){
! 92074: return SQLITE_ERROR;
! 92075: }
! 92076: db->temp_store = (u8)ts;
! 92077: return SQLITE_OK;
! 92078: }
! 92079: #endif /* SQLITE_PAGER_PRAGMAS */
! 92080:
! 92081: /*
! 92082: ** Generate code to return a single integer value.
! 92083: */
! 92084: static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
! 92085: Vdbe *v = sqlite3GetVdbe(pParse);
! 92086: int mem = ++pParse->nMem;
! 92087: i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
! 92088: if( pI64 ){
! 92089: memcpy(pI64, &value, sizeof(value));
! 92090: }
! 92091: sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
! 92092: sqlite3VdbeSetNumCols(v, 1);
! 92093: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
! 92094: sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
! 92095: }
! 92096:
! 92097: #ifndef SQLITE_OMIT_FLAG_PRAGMAS
! 92098: /*
! 92099: ** Check to see if zRight and zLeft refer to a pragma that queries
! 92100: ** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
! 92101: ** Also, implement the pragma.
! 92102: */
! 92103: static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
! 92104: static const struct sPragmaType {
! 92105: const char *zName; /* Name of the pragma */
! 92106: int mask; /* Mask for the db->flags value */
! 92107: } aPragma[] = {
! 92108: { "full_column_names", SQLITE_FullColNames },
! 92109: { "short_column_names", SQLITE_ShortColNames },
! 92110: { "count_changes", SQLITE_CountRows },
! 92111: { "empty_result_callbacks", SQLITE_NullCallback },
! 92112: { "legacy_file_format", SQLITE_LegacyFileFmt },
! 92113: { "fullfsync", SQLITE_FullFSync },
! 92114: { "checkpoint_fullfsync", SQLITE_CkptFullFSync },
! 92115: { "reverse_unordered_selects", SQLITE_ReverseOrder },
! 92116: #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
! 92117: { "automatic_index", SQLITE_AutoIndex },
! 92118: #endif
! 92119: #ifdef SQLITE_DEBUG
! 92120: { "sql_trace", SQLITE_SqlTrace },
! 92121: { "vdbe_listing", SQLITE_VdbeListing },
! 92122: { "vdbe_trace", SQLITE_VdbeTrace },
! 92123: #endif
! 92124: #ifndef SQLITE_OMIT_CHECK
! 92125: { "ignore_check_constraints", SQLITE_IgnoreChecks },
! 92126: #endif
! 92127: /* The following is VERY experimental */
! 92128: { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
! 92129: { "omit_readlock", SQLITE_NoReadlock },
! 92130:
! 92131: /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
! 92132: ** flag if there are any active statements. */
! 92133: { "read_uncommitted", SQLITE_ReadUncommitted },
! 92134: { "recursive_triggers", SQLITE_RecTriggers },
! 92135:
! 92136: /* This flag may only be set if both foreign-key and trigger support
! 92137: ** are present in the build. */
! 92138: #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
! 92139: { "foreign_keys", SQLITE_ForeignKeys },
! 92140: #endif
! 92141: };
! 92142: int i;
! 92143: const struct sPragmaType *p;
! 92144: for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
! 92145: if( sqlite3StrICmp(zLeft, p->zName)==0 ){
! 92146: sqlite3 *db = pParse->db;
! 92147: Vdbe *v;
! 92148: v = sqlite3GetVdbe(pParse);
! 92149: assert( v!=0 ); /* Already allocated by sqlite3Pragma() */
! 92150: if( ALWAYS(v) ){
! 92151: if( zRight==0 ){
! 92152: returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
! 92153: }else{
! 92154: int mask = p->mask; /* Mask of bits to set or clear. */
! 92155: if( db->autoCommit==0 ){
! 92156: /* Foreign key support may not be enabled or disabled while not
! 92157: ** in auto-commit mode. */
! 92158: mask &= ~(SQLITE_ForeignKeys);
! 92159: }
! 92160:
! 92161: if( sqlite3GetBoolean(zRight) ){
! 92162: db->flags |= mask;
! 92163: }else{
! 92164: db->flags &= ~mask;
! 92165: }
! 92166:
! 92167: /* Many of the flag-pragmas modify the code generated by the SQL
! 92168: ** compiler (eg. count_changes). So add an opcode to expire all
! 92169: ** compiled SQL statements after modifying a pragma value.
! 92170: */
! 92171: sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
! 92172: }
! 92173: }
! 92174:
! 92175: return 1;
! 92176: }
! 92177: }
! 92178: return 0;
! 92179: }
! 92180: #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
! 92181:
! 92182: /*
! 92183: ** Return a human-readable name for a constraint resolution action.
! 92184: */
! 92185: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 92186: static const char *actionName(u8 action){
! 92187: const char *zName;
! 92188: switch( action ){
! 92189: case OE_SetNull: zName = "SET NULL"; break;
! 92190: case OE_SetDflt: zName = "SET DEFAULT"; break;
! 92191: case OE_Cascade: zName = "CASCADE"; break;
! 92192: case OE_Restrict: zName = "RESTRICT"; break;
! 92193: default: zName = "NO ACTION";
! 92194: assert( action==OE_None ); break;
! 92195: }
! 92196: return zName;
! 92197: }
! 92198: #endif
! 92199:
! 92200:
! 92201: /*
! 92202: ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
! 92203: ** defined in pager.h. This function returns the associated lowercase
! 92204: ** journal-mode name.
! 92205: */
! 92206: SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
! 92207: static char * const azModeName[] = {
! 92208: "delete", "persist", "off", "truncate", "memory"
! 92209: #ifndef SQLITE_OMIT_WAL
! 92210: , "wal"
! 92211: #endif
! 92212: };
! 92213: assert( PAGER_JOURNALMODE_DELETE==0 );
! 92214: assert( PAGER_JOURNALMODE_PERSIST==1 );
! 92215: assert( PAGER_JOURNALMODE_OFF==2 );
! 92216: assert( PAGER_JOURNALMODE_TRUNCATE==3 );
! 92217: assert( PAGER_JOURNALMODE_MEMORY==4 );
! 92218: assert( PAGER_JOURNALMODE_WAL==5 );
! 92219: assert( eMode>=0 && eMode<=ArraySize(azModeName) );
! 92220:
! 92221: if( eMode==ArraySize(azModeName) ) return 0;
! 92222: return azModeName[eMode];
! 92223: }
! 92224:
! 92225: /*
! 92226: ** Process a pragma statement.
! 92227: **
! 92228: ** Pragmas are of this form:
! 92229: **
! 92230: ** PRAGMA [database.]id [= value]
! 92231: **
! 92232: ** The identifier might also be a string. The value is a string, and
! 92233: ** identifier, or a number. If minusFlag is true, then the value is
! 92234: ** a number that was preceded by a minus sign.
! 92235: **
! 92236: ** If the left side is "database.id" then pId1 is the database name
! 92237: ** and pId2 is the id. If the left side is just "id" then pId1 is the
! 92238: ** id and pId2 is any empty string.
! 92239: */
! 92240: SQLITE_PRIVATE void sqlite3Pragma(
! 92241: Parse *pParse,
! 92242: Token *pId1, /* First part of [database.]id field */
! 92243: Token *pId2, /* Second part of [database.]id field, or NULL */
! 92244: Token *pValue, /* Token for <value>, or NULL */
! 92245: int minusFlag /* True if a '-' sign preceded <value> */
! 92246: ){
! 92247: char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
! 92248: char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
! 92249: const char *zDb = 0; /* The database name */
! 92250: Token *pId; /* Pointer to <id> token */
! 92251: int iDb; /* Database index for <database> */
! 92252: sqlite3 *db = pParse->db;
! 92253: Db *pDb;
! 92254: Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
! 92255: if( v==0 ) return;
! 92256: sqlite3VdbeRunOnlyOnce(v);
! 92257: pParse->nMem = 2;
! 92258:
! 92259: /* Interpret the [database.] part of the pragma statement. iDb is the
! 92260: ** index of the database this pragma is being applied to in db.aDb[]. */
! 92261: iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
! 92262: if( iDb<0 ) return;
! 92263: pDb = &db->aDb[iDb];
! 92264:
! 92265: /* If the temp database has been explicitly named as part of the
! 92266: ** pragma, make sure it is open.
! 92267: */
! 92268: if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
! 92269: return;
! 92270: }
! 92271:
! 92272: zLeft = sqlite3NameFromToken(db, pId);
! 92273: if( !zLeft ) return;
! 92274: if( minusFlag ){
! 92275: zRight = sqlite3MPrintf(db, "-%T", pValue);
! 92276: }else{
! 92277: zRight = sqlite3NameFromToken(db, pValue);
! 92278: }
! 92279:
! 92280: assert( pId2 );
! 92281: zDb = pId2->n>0 ? pDb->zName : 0;
! 92282: if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
! 92283: goto pragma_out;
! 92284: }
! 92285:
! 92286: #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
! 92287: /*
! 92288: ** PRAGMA [database.]default_cache_size
! 92289: ** PRAGMA [database.]default_cache_size=N
! 92290: **
! 92291: ** The first form reports the current persistent setting for the
! 92292: ** page cache size. The value returned is the maximum number of
! 92293: ** pages in the page cache. The second form sets both the current
! 92294: ** page cache size value and the persistent page cache size value
! 92295: ** stored in the database file.
! 92296: **
! 92297: ** Older versions of SQLite would set the default cache size to a
! 92298: ** negative number to indicate synchronous=OFF. These days, synchronous
! 92299: ** is always on by default regardless of the sign of the default cache
! 92300: ** size. But continue to take the absolute value of the default cache
! 92301: ** size of historical compatibility.
! 92302: */
! 92303: if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
! 92304: static const VdbeOpList getCacheSize[] = {
! 92305: { OP_Transaction, 0, 0, 0}, /* 0 */
! 92306: { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
! 92307: { OP_IfPos, 1, 7, 0},
! 92308: { OP_Integer, 0, 2, 0},
! 92309: { OP_Subtract, 1, 2, 1},
! 92310: { OP_IfPos, 1, 7, 0},
! 92311: { OP_Integer, 0, 1, 0}, /* 6 */
! 92312: { OP_ResultRow, 1, 1, 0},
! 92313: };
! 92314: int addr;
! 92315: if( sqlite3ReadSchema(pParse) ) goto pragma_out;
! 92316: sqlite3VdbeUsesBtree(v, iDb);
! 92317: if( !zRight ){
! 92318: sqlite3VdbeSetNumCols(v, 1);
! 92319: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
! 92320: pParse->nMem += 2;
! 92321: addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
! 92322: sqlite3VdbeChangeP1(v, addr, iDb);
! 92323: sqlite3VdbeChangeP1(v, addr+1, iDb);
! 92324: sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
! 92325: }else{
! 92326: int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
! 92327: sqlite3BeginWriteOperation(pParse, 0, iDb);
! 92328: sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
! 92329: sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
! 92330: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 92331: pDb->pSchema->cache_size = size;
! 92332: sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
! 92333: }
! 92334: }else
! 92335: #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
! 92336:
! 92337: #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
! 92338: /*
! 92339: ** PRAGMA [database.]page_size
! 92340: ** PRAGMA [database.]page_size=N
! 92341: **
! 92342: ** The first form reports the current setting for the
! 92343: ** database page size in bytes. The second form sets the
! 92344: ** database page size value. The value can only be set if
! 92345: ** the database has not yet been created.
! 92346: */
! 92347: if( sqlite3StrICmp(zLeft,"page_size")==0 ){
! 92348: Btree *pBt = pDb->pBt;
! 92349: assert( pBt!=0 );
! 92350: if( !zRight ){
! 92351: int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
! 92352: returnSingleInt(pParse, "page_size", size);
! 92353: }else{
! 92354: /* Malloc may fail when setting the page-size, as there is an internal
! 92355: ** buffer that the pager module resizes using sqlite3_realloc().
! 92356: */
! 92357: db->nextPagesize = sqlite3Atoi(zRight);
! 92358: if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
! 92359: db->mallocFailed = 1;
! 92360: }
! 92361: }
! 92362: }else
! 92363:
! 92364: /*
! 92365: ** PRAGMA [database.]secure_delete
! 92366: ** PRAGMA [database.]secure_delete=ON/OFF
! 92367: **
! 92368: ** The first form reports the current setting for the
! 92369: ** secure_delete flag. The second form changes the secure_delete
! 92370: ** flag setting and reports thenew value.
! 92371: */
! 92372: if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
! 92373: Btree *pBt = pDb->pBt;
! 92374: int b = -1;
! 92375: assert( pBt!=0 );
! 92376: if( zRight ){
! 92377: b = sqlite3GetBoolean(zRight);
! 92378: }
! 92379: if( pId2->n==0 && b>=0 ){
! 92380: int ii;
! 92381: for(ii=0; ii<db->nDb; ii++){
! 92382: sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
! 92383: }
! 92384: }
! 92385: b = sqlite3BtreeSecureDelete(pBt, b);
! 92386: returnSingleInt(pParse, "secure_delete", b);
! 92387: }else
! 92388:
! 92389: /*
! 92390: ** PRAGMA [database.]max_page_count
! 92391: ** PRAGMA [database.]max_page_count=N
! 92392: **
! 92393: ** The first form reports the current setting for the
! 92394: ** maximum number of pages in the database file. The
! 92395: ** second form attempts to change this setting. Both
! 92396: ** forms return the current setting.
! 92397: **
! 92398: ** The absolute value of N is used. This is undocumented and might
! 92399: ** change. The only purpose is to provide an easy way to test
! 92400: ** the sqlite3AbsInt32() function.
! 92401: **
! 92402: ** PRAGMA [database.]page_count
! 92403: **
! 92404: ** Return the number of pages in the specified database.
! 92405: */
! 92406: if( sqlite3StrICmp(zLeft,"page_count")==0
! 92407: || sqlite3StrICmp(zLeft,"max_page_count")==0
! 92408: ){
! 92409: int iReg;
! 92410: if( sqlite3ReadSchema(pParse) ) goto pragma_out;
! 92411: sqlite3CodeVerifySchema(pParse, iDb);
! 92412: iReg = ++pParse->nMem;
! 92413: if( sqlite3Tolower(zLeft[0])=='p' ){
! 92414: sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
! 92415: }else{
! 92416: sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
! 92417: sqlite3AbsInt32(sqlite3Atoi(zRight)));
! 92418: }
! 92419: sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
! 92420: sqlite3VdbeSetNumCols(v, 1);
! 92421: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
! 92422: }else
! 92423:
! 92424: /*
! 92425: ** PRAGMA [database.]locking_mode
! 92426: ** PRAGMA [database.]locking_mode = (normal|exclusive)
! 92427: */
! 92428: if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
! 92429: const char *zRet = "normal";
! 92430: int eMode = getLockingMode(zRight);
! 92431:
! 92432: if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
! 92433: /* Simple "PRAGMA locking_mode;" statement. This is a query for
! 92434: ** the current default locking mode (which may be different to
! 92435: ** the locking-mode of the main database).
! 92436: */
! 92437: eMode = db->dfltLockMode;
! 92438: }else{
! 92439: Pager *pPager;
! 92440: if( pId2->n==0 ){
! 92441: /* This indicates that no database name was specified as part
! 92442: ** of the PRAGMA command. In this case the locking-mode must be
! 92443: ** set on all attached databases, as well as the main db file.
! 92444: **
! 92445: ** Also, the sqlite3.dfltLockMode variable is set so that
! 92446: ** any subsequently attached databases also use the specified
! 92447: ** locking mode.
! 92448: */
! 92449: int ii;
! 92450: assert(pDb==&db->aDb[0]);
! 92451: for(ii=2; ii<db->nDb; ii++){
! 92452: pPager = sqlite3BtreePager(db->aDb[ii].pBt);
! 92453: sqlite3PagerLockingMode(pPager, eMode);
! 92454: }
! 92455: db->dfltLockMode = (u8)eMode;
! 92456: }
! 92457: pPager = sqlite3BtreePager(pDb->pBt);
! 92458: eMode = sqlite3PagerLockingMode(pPager, eMode);
! 92459: }
! 92460:
! 92461: assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
! 92462: if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
! 92463: zRet = "exclusive";
! 92464: }
! 92465: sqlite3VdbeSetNumCols(v, 1);
! 92466: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
! 92467: sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
! 92468: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
! 92469: }else
! 92470:
! 92471: /*
! 92472: ** PRAGMA [database.]journal_mode
! 92473: ** PRAGMA [database.]journal_mode =
! 92474: ** (delete|persist|off|truncate|memory|wal|off)
! 92475: */
! 92476: if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
! 92477: int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
! 92478: int ii; /* Loop counter */
! 92479:
! 92480: /* Force the schema to be loaded on all databases. This causes all
! 92481: ** database files to be opened and the journal_modes set. This is
! 92482: ** necessary because subsequent processing must know if the databases
! 92483: ** are in WAL mode. */
! 92484: if( sqlite3ReadSchema(pParse) ){
! 92485: goto pragma_out;
! 92486: }
! 92487:
! 92488: sqlite3VdbeSetNumCols(v, 1);
! 92489: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
! 92490:
! 92491: if( zRight==0 ){
! 92492: /* If there is no "=MODE" part of the pragma, do a query for the
! 92493: ** current mode */
! 92494: eMode = PAGER_JOURNALMODE_QUERY;
! 92495: }else{
! 92496: const char *zMode;
! 92497: int n = sqlite3Strlen30(zRight);
! 92498: for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
! 92499: if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
! 92500: }
! 92501: if( !zMode ){
! 92502: /* If the "=MODE" part does not match any known journal mode,
! 92503: ** then do a query */
! 92504: eMode = PAGER_JOURNALMODE_QUERY;
! 92505: }
! 92506: }
! 92507: if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
! 92508: /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
! 92509: iDb = 0;
! 92510: pId2->n = 1;
! 92511: }
! 92512: for(ii=db->nDb-1; ii>=0; ii--){
! 92513: if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
! 92514: sqlite3VdbeUsesBtree(v, ii);
! 92515: sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
! 92516: }
! 92517: }
! 92518: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
! 92519: }else
! 92520:
! 92521: /*
! 92522: ** PRAGMA [database.]journal_size_limit
! 92523: ** PRAGMA [database.]journal_size_limit=N
! 92524: **
! 92525: ** Get or set the size limit on rollback journal files.
! 92526: */
! 92527: if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
! 92528: Pager *pPager = sqlite3BtreePager(pDb->pBt);
! 92529: i64 iLimit = -2;
! 92530: if( zRight ){
! 92531: sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
! 92532: if( iLimit<-1 ) iLimit = -1;
! 92533: }
! 92534: iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
! 92535: returnSingleInt(pParse, "journal_size_limit", iLimit);
! 92536: }else
! 92537:
! 92538: #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
! 92539:
! 92540: /*
! 92541: ** PRAGMA [database.]auto_vacuum
! 92542: ** PRAGMA [database.]auto_vacuum=N
! 92543: **
! 92544: ** Get or set the value of the database 'auto-vacuum' parameter.
! 92545: ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
! 92546: */
! 92547: #ifndef SQLITE_OMIT_AUTOVACUUM
! 92548: if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
! 92549: Btree *pBt = pDb->pBt;
! 92550: assert( pBt!=0 );
! 92551: if( sqlite3ReadSchema(pParse) ){
! 92552: goto pragma_out;
! 92553: }
! 92554: if( !zRight ){
! 92555: int auto_vacuum;
! 92556: if( ALWAYS(pBt) ){
! 92557: auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
! 92558: }else{
! 92559: auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
! 92560: }
! 92561: returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
! 92562: }else{
! 92563: int eAuto = getAutoVacuum(zRight);
! 92564: assert( eAuto>=0 && eAuto<=2 );
! 92565: db->nextAutovac = (u8)eAuto;
! 92566: if( ALWAYS(eAuto>=0) ){
! 92567: /* Call SetAutoVacuum() to set initialize the internal auto and
! 92568: ** incr-vacuum flags. This is required in case this connection
! 92569: ** creates the database file. It is important that it is created
! 92570: ** as an auto-vacuum capable db.
! 92571: */
! 92572: int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
! 92573: if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
! 92574: /* When setting the auto_vacuum mode to either "full" or
! 92575: ** "incremental", write the value of meta[6] in the database
! 92576: ** file. Before writing to meta[6], check that meta[3] indicates
! 92577: ** that this really is an auto-vacuum capable database.
! 92578: */
! 92579: static const VdbeOpList setMeta6[] = {
! 92580: { OP_Transaction, 0, 1, 0}, /* 0 */
! 92581: { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
! 92582: { OP_If, 1, 0, 0}, /* 2 */
! 92583: { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
! 92584: { OP_Integer, 0, 1, 0}, /* 4 */
! 92585: { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
! 92586: };
! 92587: int iAddr;
! 92588: iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
! 92589: sqlite3VdbeChangeP1(v, iAddr, iDb);
! 92590: sqlite3VdbeChangeP1(v, iAddr+1, iDb);
! 92591: sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
! 92592: sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
! 92593: sqlite3VdbeChangeP1(v, iAddr+5, iDb);
! 92594: sqlite3VdbeUsesBtree(v, iDb);
! 92595: }
! 92596: }
! 92597: }
! 92598: }else
! 92599: #endif
! 92600:
! 92601: /*
! 92602: ** PRAGMA [database.]incremental_vacuum(N)
! 92603: **
! 92604: ** Do N steps of incremental vacuuming on a database.
! 92605: */
! 92606: #ifndef SQLITE_OMIT_AUTOVACUUM
! 92607: if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
! 92608: int iLimit, addr;
! 92609: if( sqlite3ReadSchema(pParse) ){
! 92610: goto pragma_out;
! 92611: }
! 92612: if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
! 92613: iLimit = 0x7fffffff;
! 92614: }
! 92615: sqlite3BeginWriteOperation(pParse, 0, iDb);
! 92616: sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
! 92617: addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
! 92618: sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
! 92619: sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
! 92620: sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
! 92621: sqlite3VdbeJumpHere(v, addr);
! 92622: }else
! 92623: #endif
! 92624:
! 92625: #ifndef SQLITE_OMIT_PAGER_PRAGMAS
! 92626: /*
! 92627: ** PRAGMA [database.]cache_size
! 92628: ** PRAGMA [database.]cache_size=N
! 92629: **
! 92630: ** The first form reports the current local setting for the
! 92631: ** page cache size. The second form sets the local
! 92632: ** page cache size value. If N is positive then that is the
! 92633: ** number of pages in the cache. If N is negative, then the
! 92634: ** number of pages is adjusted so that the cache uses -N kibibytes
! 92635: ** of memory.
! 92636: */
! 92637: if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
! 92638: if( sqlite3ReadSchema(pParse) ) goto pragma_out;
! 92639: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 92640: if( !zRight ){
! 92641: returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
! 92642: }else{
! 92643: int size = sqlite3Atoi(zRight);
! 92644: pDb->pSchema->cache_size = size;
! 92645: sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
! 92646: }
! 92647: }else
! 92648:
! 92649: /*
! 92650: ** PRAGMA temp_store
! 92651: ** PRAGMA temp_store = "default"|"memory"|"file"
! 92652: **
! 92653: ** Return or set the local value of the temp_store flag. Changing
! 92654: ** the local value does not make changes to the disk file and the default
! 92655: ** value will be restored the next time the database is opened.
! 92656: **
! 92657: ** Note that it is possible for the library compile-time options to
! 92658: ** override this setting
! 92659: */
! 92660: if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
! 92661: if( !zRight ){
! 92662: returnSingleInt(pParse, "temp_store", db->temp_store);
! 92663: }else{
! 92664: changeTempStorage(pParse, zRight);
! 92665: }
! 92666: }else
! 92667:
! 92668: /*
! 92669: ** PRAGMA temp_store_directory
! 92670: ** PRAGMA temp_store_directory = ""|"directory_name"
! 92671: **
! 92672: ** Return or set the local value of the temp_store_directory flag. Changing
! 92673: ** the value sets a specific directory to be used for temporary files.
! 92674: ** Setting to a null string reverts to the default temporary directory search.
! 92675: ** If temporary directory is changed, then invalidateTempStorage.
! 92676: **
! 92677: */
! 92678: if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
! 92679: if( !zRight ){
! 92680: if( sqlite3_temp_directory ){
! 92681: sqlite3VdbeSetNumCols(v, 1);
! 92682: sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
! 92683: "temp_store_directory", SQLITE_STATIC);
! 92684: sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
! 92685: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
! 92686: }
! 92687: }else{
! 92688: #ifndef SQLITE_OMIT_WSD
! 92689: if( zRight[0] ){
! 92690: int rc;
! 92691: int res;
! 92692: rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
! 92693: if( rc!=SQLITE_OK || res==0 ){
! 92694: sqlite3ErrorMsg(pParse, "not a writable directory");
! 92695: goto pragma_out;
! 92696: }
! 92697: }
! 92698: if( SQLITE_TEMP_STORE==0
! 92699: || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
! 92700: || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
! 92701: ){
! 92702: invalidateTempStorage(pParse);
! 92703: }
! 92704: sqlite3_free(sqlite3_temp_directory);
! 92705: if( zRight[0] ){
! 92706: sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
! 92707: }else{
! 92708: sqlite3_temp_directory = 0;
! 92709: }
! 92710: #endif /* SQLITE_OMIT_WSD */
! 92711: }
! 92712: }else
! 92713:
! 92714: #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
! 92715: # if defined(__APPLE__)
! 92716: # define SQLITE_ENABLE_LOCKING_STYLE 1
! 92717: # else
! 92718: # define SQLITE_ENABLE_LOCKING_STYLE 0
! 92719: # endif
! 92720: #endif
! 92721: #if SQLITE_ENABLE_LOCKING_STYLE
! 92722: /*
! 92723: ** PRAGMA [database.]lock_proxy_file
! 92724: ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
! 92725: **
! 92726: ** Return or set the value of the lock_proxy_file flag. Changing
! 92727: ** the value sets a specific file to be used for database access locks.
! 92728: **
! 92729: */
! 92730: if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
! 92731: if( !zRight ){
! 92732: Pager *pPager = sqlite3BtreePager(pDb->pBt);
! 92733: char *proxy_file_path = NULL;
! 92734: sqlite3_file *pFile = sqlite3PagerFile(pPager);
! 92735: sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
! 92736: &proxy_file_path);
! 92737:
! 92738: if( proxy_file_path ){
! 92739: sqlite3VdbeSetNumCols(v, 1);
! 92740: sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
! 92741: "lock_proxy_file", SQLITE_STATIC);
! 92742: sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
! 92743: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
! 92744: }
! 92745: }else{
! 92746: Pager *pPager = sqlite3BtreePager(pDb->pBt);
! 92747: sqlite3_file *pFile = sqlite3PagerFile(pPager);
! 92748: int res;
! 92749: if( zRight[0] ){
! 92750: res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
! 92751: zRight);
! 92752: } else {
! 92753: res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
! 92754: NULL);
! 92755: }
! 92756: if( res!=SQLITE_OK ){
! 92757: sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
! 92758: goto pragma_out;
! 92759: }
! 92760: }
! 92761: }else
! 92762: #endif /* SQLITE_ENABLE_LOCKING_STYLE */
! 92763:
! 92764: /*
! 92765: ** PRAGMA [database.]synchronous
! 92766: ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
! 92767: **
! 92768: ** Return or set the local value of the synchronous flag. Changing
! 92769: ** the local value does not make changes to the disk file and the
! 92770: ** default value will be restored the next time the database is
! 92771: ** opened.
! 92772: */
! 92773: if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
! 92774: if( sqlite3ReadSchema(pParse) ) goto pragma_out;
! 92775: if( !zRight ){
! 92776: returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
! 92777: }else{
! 92778: if( !db->autoCommit ){
! 92779: sqlite3ErrorMsg(pParse,
! 92780: "Safety level may not be changed inside a transaction");
! 92781: }else{
! 92782: pDb->safety_level = getSafetyLevel(zRight)+1;
! 92783: }
! 92784: }
! 92785: }else
! 92786: #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
! 92787:
! 92788: #ifndef SQLITE_OMIT_FLAG_PRAGMAS
! 92789: if( flagPragma(pParse, zLeft, zRight) ){
! 92790: /* The flagPragma() subroutine also generates any necessary code
! 92791: ** there is nothing more to do here */
! 92792: }else
! 92793: #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
! 92794:
! 92795: #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
! 92796: /*
! 92797: ** PRAGMA table_info(<table>)
! 92798: **
! 92799: ** Return a single row for each column of the named table. The columns of
! 92800: ** the returned data set are:
! 92801: **
! 92802: ** cid: Column id (numbered from left to right, starting at 0)
! 92803: ** name: Column name
! 92804: ** type: Column declaration type.
! 92805: ** notnull: True if 'NOT NULL' is part of column declaration
! 92806: ** dflt_value: The default value for the column, if any.
! 92807: */
! 92808: if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
! 92809: Table *pTab;
! 92810: if( sqlite3ReadSchema(pParse) ) goto pragma_out;
! 92811: pTab = sqlite3FindTable(db, zRight, zDb);
! 92812: if( pTab ){
! 92813: int i;
! 92814: int nHidden = 0;
! 92815: Column *pCol;
! 92816: sqlite3VdbeSetNumCols(v, 6);
! 92817: pParse->nMem = 6;
! 92818: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
! 92819: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
! 92820: sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
! 92821: sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
! 92822: sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
! 92823: sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
! 92824: sqlite3ViewGetColumnNames(pParse, pTab);
! 92825: for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
! 92826: if( IsHiddenColumn(pCol) ){
! 92827: nHidden++;
! 92828: continue;
! 92829: }
! 92830: sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
! 92831: sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
! 92832: sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
! 92833: pCol->zType ? pCol->zType : "", 0);
! 92834: sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
! 92835: if( pCol->zDflt ){
! 92836: sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
! 92837: }else{
! 92838: sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
! 92839: }
! 92840: sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
! 92841: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
! 92842: }
! 92843: }
! 92844: }else
! 92845:
! 92846: if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
! 92847: Index *pIdx;
! 92848: Table *pTab;
! 92849: if( sqlite3ReadSchema(pParse) ) goto pragma_out;
! 92850: pIdx = sqlite3FindIndex(db, zRight, zDb);
! 92851: if( pIdx ){
! 92852: int i;
! 92853: pTab = pIdx->pTable;
! 92854: sqlite3VdbeSetNumCols(v, 3);
! 92855: pParse->nMem = 3;
! 92856: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
! 92857: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
! 92858: sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
! 92859: for(i=0; i<pIdx->nColumn; i++){
! 92860: int cnum = pIdx->aiColumn[i];
! 92861: sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
! 92862: sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
! 92863: assert( pTab->nCol>cnum );
! 92864: sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
! 92865: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
! 92866: }
! 92867: }
! 92868: }else
! 92869:
! 92870: if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
! 92871: Index *pIdx;
! 92872: Table *pTab;
! 92873: if( sqlite3ReadSchema(pParse) ) goto pragma_out;
! 92874: pTab = sqlite3FindTable(db, zRight, zDb);
! 92875: if( pTab ){
! 92876: v = sqlite3GetVdbe(pParse);
! 92877: pIdx = pTab->pIndex;
! 92878: if( pIdx ){
! 92879: int i = 0;
! 92880: sqlite3VdbeSetNumCols(v, 3);
! 92881: pParse->nMem = 3;
! 92882: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
! 92883: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
! 92884: sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
! 92885: while(pIdx){
! 92886: sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
! 92887: sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
! 92888: sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
! 92889: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
! 92890: ++i;
! 92891: pIdx = pIdx->pNext;
! 92892: }
! 92893: }
! 92894: }
! 92895: }else
! 92896:
! 92897: if( sqlite3StrICmp(zLeft, "database_list")==0 ){
! 92898: int i;
! 92899: if( sqlite3ReadSchema(pParse) ) goto pragma_out;
! 92900: sqlite3VdbeSetNumCols(v, 3);
! 92901: pParse->nMem = 3;
! 92902: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
! 92903: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
! 92904: sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
! 92905: for(i=0; i<db->nDb; i++){
! 92906: if( db->aDb[i].pBt==0 ) continue;
! 92907: assert( db->aDb[i].zName!=0 );
! 92908: sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
! 92909: sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
! 92910: sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
! 92911: sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
! 92912: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
! 92913: }
! 92914: }else
! 92915:
! 92916: if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
! 92917: int i = 0;
! 92918: HashElem *p;
! 92919: sqlite3VdbeSetNumCols(v, 2);
! 92920: pParse->nMem = 2;
! 92921: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
! 92922: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
! 92923: for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
! 92924: CollSeq *pColl = (CollSeq *)sqliteHashData(p);
! 92925: sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
! 92926: sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
! 92927: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
! 92928: }
! 92929: }else
! 92930: #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
! 92931:
! 92932: #ifndef SQLITE_OMIT_FOREIGN_KEY
! 92933: if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
! 92934: FKey *pFK;
! 92935: Table *pTab;
! 92936: if( sqlite3ReadSchema(pParse) ) goto pragma_out;
! 92937: pTab = sqlite3FindTable(db, zRight, zDb);
! 92938: if( pTab ){
! 92939: v = sqlite3GetVdbe(pParse);
! 92940: pFK = pTab->pFKey;
! 92941: if( pFK ){
! 92942: int i = 0;
! 92943: sqlite3VdbeSetNumCols(v, 8);
! 92944: pParse->nMem = 8;
! 92945: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
! 92946: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
! 92947: sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
! 92948: sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
! 92949: sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
! 92950: sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
! 92951: sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
! 92952: sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
! 92953: while(pFK){
! 92954: int j;
! 92955: for(j=0; j<pFK->nCol; j++){
! 92956: char *zCol = pFK->aCol[j].zCol;
! 92957: char *zOnDelete = (char *)actionName(pFK->aAction[0]);
! 92958: char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
! 92959: sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
! 92960: sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
! 92961: sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
! 92962: sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
! 92963: pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
! 92964: sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
! 92965: sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
! 92966: sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
! 92967: sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
! 92968: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
! 92969: }
! 92970: ++i;
! 92971: pFK = pFK->pNextFrom;
! 92972: }
! 92973: }
! 92974: }
! 92975: }else
! 92976: #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
! 92977:
! 92978: #ifndef NDEBUG
! 92979: if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
! 92980: if( zRight ){
! 92981: if( sqlite3GetBoolean(zRight) ){
! 92982: sqlite3ParserTrace(stderr, "parser: ");
! 92983: }else{
! 92984: sqlite3ParserTrace(0, 0);
! 92985: }
! 92986: }
! 92987: }else
! 92988: #endif
! 92989:
! 92990: /* Reinstall the LIKE and GLOB functions. The variant of LIKE
! 92991: ** used will be case sensitive or not depending on the RHS.
! 92992: */
! 92993: if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
! 92994: if( zRight ){
! 92995: sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight));
! 92996: }
! 92997: }else
! 92998:
! 92999: #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
! 93000: # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
! 93001: #endif
! 93002:
! 93003: #ifndef SQLITE_OMIT_INTEGRITY_CHECK
! 93004: /* Pragma "quick_check" is an experimental reduced version of
! 93005: ** integrity_check designed to detect most database corruption
! 93006: ** without most of the overhead of a full integrity-check.
! 93007: */
! 93008: if( sqlite3StrICmp(zLeft, "integrity_check")==0
! 93009: || sqlite3StrICmp(zLeft, "quick_check")==0
! 93010: ){
! 93011: int i, j, addr, mxErr;
! 93012:
! 93013: /* Code that appears at the end of the integrity check. If no error
! 93014: ** messages have been generated, output OK. Otherwise output the
! 93015: ** error message
! 93016: */
! 93017: static const VdbeOpList endCode[] = {
! 93018: { OP_AddImm, 1, 0, 0}, /* 0 */
! 93019: { OP_IfNeg, 1, 0, 0}, /* 1 */
! 93020: { OP_String8, 0, 3, 0}, /* 2 */
! 93021: { OP_ResultRow, 3, 1, 0},
! 93022: };
! 93023:
! 93024: int isQuick = (sqlite3Tolower(zLeft[0])=='q');
! 93025:
! 93026: /* Initialize the VDBE program */
! 93027: if( sqlite3ReadSchema(pParse) ) goto pragma_out;
! 93028: pParse->nMem = 6;
! 93029: sqlite3VdbeSetNumCols(v, 1);
! 93030: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
! 93031:
! 93032: /* Set the maximum error count */
! 93033: mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
! 93034: if( zRight ){
! 93035: sqlite3GetInt32(zRight, &mxErr);
! 93036: if( mxErr<=0 ){
! 93037: mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
! 93038: }
! 93039: }
! 93040: sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
! 93041:
! 93042: /* Do an integrity check on each database file */
! 93043: for(i=0; i<db->nDb; i++){
! 93044: HashElem *x;
! 93045: Hash *pTbls;
! 93046: int cnt = 0;
! 93047:
! 93048: if( OMIT_TEMPDB && i==1 ) continue;
! 93049:
! 93050: sqlite3CodeVerifySchema(pParse, i);
! 93051: addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
! 93052: sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
! 93053: sqlite3VdbeJumpHere(v, addr);
! 93054:
! 93055: /* Do an integrity check of the B-Tree
! 93056: **
! 93057: ** Begin by filling registers 2, 3, ... with the root pages numbers
! 93058: ** for all tables and indices in the database.
! 93059: */
! 93060: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 93061: pTbls = &db->aDb[i].pSchema->tblHash;
! 93062: for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
! 93063: Table *pTab = sqliteHashData(x);
! 93064: Index *pIdx;
! 93065: sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
! 93066: cnt++;
! 93067: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
! 93068: sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
! 93069: cnt++;
! 93070: }
! 93071: }
! 93072:
! 93073: /* Make sure sufficient number of registers have been allocated */
! 93074: if( pParse->nMem < cnt+4 ){
! 93075: pParse->nMem = cnt+4;
! 93076: }
! 93077:
! 93078: /* Do the b-tree integrity checks */
! 93079: sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
! 93080: sqlite3VdbeChangeP5(v, (u8)i);
! 93081: addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
! 93082: sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
! 93083: sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
! 93084: P4_DYNAMIC);
! 93085: sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
! 93086: sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
! 93087: sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
! 93088: sqlite3VdbeJumpHere(v, addr);
! 93089:
! 93090: /* Make sure all the indices are constructed correctly.
! 93091: */
! 93092: for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
! 93093: Table *pTab = sqliteHashData(x);
! 93094: Index *pIdx;
! 93095: int loopTop;
! 93096:
! 93097: if( pTab->pIndex==0 ) continue;
! 93098: addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
! 93099: sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
! 93100: sqlite3VdbeJumpHere(v, addr);
! 93101: sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
! 93102: sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */
! 93103: loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
! 93104: sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */
! 93105: for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
! 93106: int jmp2;
! 93107: int r1;
! 93108: static const VdbeOpList idxErr[] = {
! 93109: { OP_AddImm, 1, -1, 0},
! 93110: { OP_String8, 0, 3, 0}, /* 1 */
! 93111: { OP_Rowid, 1, 4, 0},
! 93112: { OP_String8, 0, 5, 0}, /* 3 */
! 93113: { OP_String8, 0, 6, 0}, /* 4 */
! 93114: { OP_Concat, 4, 3, 3},
! 93115: { OP_Concat, 5, 3, 3},
! 93116: { OP_Concat, 6, 3, 3},
! 93117: { OP_ResultRow, 3, 1, 0},
! 93118: { OP_IfPos, 1, 0, 0}, /* 9 */
! 93119: { OP_Halt, 0, 0, 0},
! 93120: };
! 93121: r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
! 93122: jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
! 93123: addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
! 93124: sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
! 93125: sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
! 93126: sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
! 93127: sqlite3VdbeJumpHere(v, addr+9);
! 93128: sqlite3VdbeJumpHere(v, jmp2);
! 93129: }
! 93130: sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
! 93131: sqlite3VdbeJumpHere(v, loopTop);
! 93132: for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
! 93133: static const VdbeOpList cntIdx[] = {
! 93134: { OP_Integer, 0, 3, 0},
! 93135: { OP_Rewind, 0, 0, 0}, /* 1 */
! 93136: { OP_AddImm, 3, 1, 0},
! 93137: { OP_Next, 0, 0, 0}, /* 3 */
! 93138: { OP_Eq, 2, 0, 3}, /* 4 */
! 93139: { OP_AddImm, 1, -1, 0},
! 93140: { OP_String8, 0, 2, 0}, /* 6 */
! 93141: { OP_String8, 0, 3, 0}, /* 7 */
! 93142: { OP_Concat, 3, 2, 2},
! 93143: { OP_ResultRow, 2, 1, 0},
! 93144: };
! 93145: addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
! 93146: sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
! 93147: sqlite3VdbeJumpHere(v, addr);
! 93148: addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
! 93149: sqlite3VdbeChangeP1(v, addr+1, j+2);
! 93150: sqlite3VdbeChangeP2(v, addr+1, addr+4);
! 93151: sqlite3VdbeChangeP1(v, addr+3, j+2);
! 93152: sqlite3VdbeChangeP2(v, addr+3, addr+2);
! 93153: sqlite3VdbeJumpHere(v, addr+4);
! 93154: sqlite3VdbeChangeP4(v, addr+6,
! 93155: "wrong # of entries in index ", P4_STATIC);
! 93156: sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
! 93157: }
! 93158: }
! 93159: }
! 93160: addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
! 93161: sqlite3VdbeChangeP2(v, addr, -mxErr);
! 93162: sqlite3VdbeJumpHere(v, addr+1);
! 93163: sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
! 93164: }else
! 93165: #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
! 93166:
! 93167: #ifndef SQLITE_OMIT_UTF16
! 93168: /*
! 93169: ** PRAGMA encoding
! 93170: ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
! 93171: **
! 93172: ** In its first form, this pragma returns the encoding of the main
! 93173: ** database. If the database is not initialized, it is initialized now.
! 93174: **
! 93175: ** The second form of this pragma is a no-op if the main database file
! 93176: ** has not already been initialized. In this case it sets the default
! 93177: ** encoding that will be used for the main database file if a new file
! 93178: ** is created. If an existing main database file is opened, then the
! 93179: ** default text encoding for the existing database is used.
! 93180: **
! 93181: ** In all cases new databases created using the ATTACH command are
! 93182: ** created to use the same default text encoding as the main database. If
! 93183: ** the main database has not been initialized and/or created when ATTACH
! 93184: ** is executed, this is done before the ATTACH operation.
! 93185: **
! 93186: ** In the second form this pragma sets the text encoding to be used in
! 93187: ** new database files created using this database handle. It is only
! 93188: ** useful if invoked immediately after the main database i
! 93189: */
! 93190: if( sqlite3StrICmp(zLeft, "encoding")==0 ){
! 93191: static const struct EncName {
! 93192: char *zName;
! 93193: u8 enc;
! 93194: } encnames[] = {
! 93195: { "UTF8", SQLITE_UTF8 },
! 93196: { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
! 93197: { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
! 93198: { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
! 93199: { "UTF16le", SQLITE_UTF16LE },
! 93200: { "UTF16be", SQLITE_UTF16BE },
! 93201: { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
! 93202: { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
! 93203: { 0, 0 }
! 93204: };
! 93205: const struct EncName *pEnc;
! 93206: if( !zRight ){ /* "PRAGMA encoding" */
! 93207: if( sqlite3ReadSchema(pParse) ) goto pragma_out;
! 93208: sqlite3VdbeSetNumCols(v, 1);
! 93209: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
! 93210: sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
! 93211: assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
! 93212: assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
! 93213: assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
! 93214: sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
! 93215: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
! 93216: }else{ /* "PRAGMA encoding = XXX" */
! 93217: /* Only change the value of sqlite.enc if the database handle is not
! 93218: ** initialized. If the main database exists, the new sqlite.enc value
! 93219: ** will be overwritten when the schema is next loaded. If it does not
! 93220: ** already exists, it will be created to use the new encoding value.
! 93221: */
! 93222: if(
! 93223: !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
! 93224: DbHasProperty(db, 0, DB_Empty)
! 93225: ){
! 93226: for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
! 93227: if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
! 93228: ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
! 93229: break;
! 93230: }
! 93231: }
! 93232: if( !pEnc->zName ){
! 93233: sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
! 93234: }
! 93235: }
! 93236: }
! 93237: }else
! 93238: #endif /* SQLITE_OMIT_UTF16 */
! 93239:
! 93240: #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
! 93241: /*
! 93242: ** PRAGMA [database.]schema_version
! 93243: ** PRAGMA [database.]schema_version = <integer>
! 93244: **
! 93245: ** PRAGMA [database.]user_version
! 93246: ** PRAGMA [database.]user_version = <integer>
! 93247: **
! 93248: ** The pragma's schema_version and user_version are used to set or get
! 93249: ** the value of the schema-version and user-version, respectively. Both
! 93250: ** the schema-version and the user-version are 32-bit signed integers
! 93251: ** stored in the database header.
! 93252: **
! 93253: ** The schema-cookie is usually only manipulated internally by SQLite. It
! 93254: ** is incremented by SQLite whenever the database schema is modified (by
! 93255: ** creating or dropping a table or index). The schema version is used by
! 93256: ** SQLite each time a query is executed to ensure that the internal cache
! 93257: ** of the schema used when compiling the SQL query matches the schema of
! 93258: ** the database against which the compiled query is actually executed.
! 93259: ** Subverting this mechanism by using "PRAGMA schema_version" to modify
! 93260: ** the schema-version is potentially dangerous and may lead to program
! 93261: ** crashes or database corruption. Use with caution!
! 93262: **
! 93263: ** The user-version is not used internally by SQLite. It may be used by
! 93264: ** applications for any purpose.
! 93265: */
! 93266: if( sqlite3StrICmp(zLeft, "schema_version")==0
! 93267: || sqlite3StrICmp(zLeft, "user_version")==0
! 93268: || sqlite3StrICmp(zLeft, "freelist_count")==0
! 93269: ){
! 93270: int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
! 93271: sqlite3VdbeUsesBtree(v, iDb);
! 93272: switch( zLeft[0] ){
! 93273: case 'f': case 'F':
! 93274: iCookie = BTREE_FREE_PAGE_COUNT;
! 93275: break;
! 93276: case 's': case 'S':
! 93277: iCookie = BTREE_SCHEMA_VERSION;
! 93278: break;
! 93279: default:
! 93280: iCookie = BTREE_USER_VERSION;
! 93281: break;
! 93282: }
! 93283:
! 93284: if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
! 93285: /* Write the specified cookie value */
! 93286: static const VdbeOpList setCookie[] = {
! 93287: { OP_Transaction, 0, 1, 0}, /* 0 */
! 93288: { OP_Integer, 0, 1, 0}, /* 1 */
! 93289: { OP_SetCookie, 0, 0, 1}, /* 2 */
! 93290: };
! 93291: int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
! 93292: sqlite3VdbeChangeP1(v, addr, iDb);
! 93293: sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
! 93294: sqlite3VdbeChangeP1(v, addr+2, iDb);
! 93295: sqlite3VdbeChangeP2(v, addr+2, iCookie);
! 93296: }else{
! 93297: /* Read the specified cookie value */
! 93298: static const VdbeOpList readCookie[] = {
! 93299: { OP_Transaction, 0, 0, 0}, /* 0 */
! 93300: { OP_ReadCookie, 0, 1, 0}, /* 1 */
! 93301: { OP_ResultRow, 1, 1, 0}
! 93302: };
! 93303: int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
! 93304: sqlite3VdbeChangeP1(v, addr, iDb);
! 93305: sqlite3VdbeChangeP1(v, addr+1, iDb);
! 93306: sqlite3VdbeChangeP3(v, addr+1, iCookie);
! 93307: sqlite3VdbeSetNumCols(v, 1);
! 93308: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
! 93309: }
! 93310: }else
! 93311: #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
! 93312:
! 93313: #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
! 93314: /*
! 93315: ** PRAGMA compile_options
! 93316: **
! 93317: ** Return the names of all compile-time options used in this build,
! 93318: ** one option per row.
! 93319: */
! 93320: if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
! 93321: int i = 0;
! 93322: const char *zOpt;
! 93323: sqlite3VdbeSetNumCols(v, 1);
! 93324: pParse->nMem = 1;
! 93325: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
! 93326: while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
! 93327: sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
! 93328: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
! 93329: }
! 93330: }else
! 93331: #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
! 93332:
! 93333: #ifndef SQLITE_OMIT_WAL
! 93334: /*
! 93335: ** PRAGMA [database.]wal_checkpoint = passive|full|restart
! 93336: **
! 93337: ** Checkpoint the database.
! 93338: */
! 93339: if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
! 93340: int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
! 93341: int eMode = SQLITE_CHECKPOINT_PASSIVE;
! 93342: if( zRight ){
! 93343: if( sqlite3StrICmp(zRight, "full")==0 ){
! 93344: eMode = SQLITE_CHECKPOINT_FULL;
! 93345: }else if( sqlite3StrICmp(zRight, "restart")==0 ){
! 93346: eMode = SQLITE_CHECKPOINT_RESTART;
! 93347: }
! 93348: }
! 93349: if( sqlite3ReadSchema(pParse) ) goto pragma_out;
! 93350: sqlite3VdbeSetNumCols(v, 3);
! 93351: pParse->nMem = 3;
! 93352: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
! 93353: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
! 93354: sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
! 93355:
! 93356: sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
! 93357: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
! 93358: }else
! 93359:
! 93360: /*
! 93361: ** PRAGMA wal_autocheckpoint
! 93362: ** PRAGMA wal_autocheckpoint = N
! 93363: **
! 93364: ** Configure a database connection to automatically checkpoint a database
! 93365: ** after accumulating N frames in the log. Or query for the current value
! 93366: ** of N.
! 93367: */
! 93368: if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
! 93369: if( zRight ){
! 93370: sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
! 93371: }
! 93372: returnSingleInt(pParse, "wal_autocheckpoint",
! 93373: db->xWalCallback==sqlite3WalDefaultHook ?
! 93374: SQLITE_PTR_TO_INT(db->pWalArg) : 0);
! 93375: }else
! 93376: #endif
! 93377:
! 93378: /*
! 93379: ** PRAGMA shrink_memory
! 93380: **
! 93381: ** This pragma attempts to free as much memory as possible from the
! 93382: ** current database connection.
! 93383: */
! 93384: if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
! 93385: sqlite3_db_release_memory(db);
! 93386: }else
! 93387:
! 93388: #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
! 93389: /*
! 93390: ** Report the current state of file logs for all databases
! 93391: */
! 93392: if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
! 93393: static const char *const azLockName[] = {
! 93394: "unlocked", "shared", "reserved", "pending", "exclusive"
! 93395: };
! 93396: int i;
! 93397: sqlite3VdbeSetNumCols(v, 2);
! 93398: pParse->nMem = 2;
! 93399: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
! 93400: sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
! 93401: for(i=0; i<db->nDb; i++){
! 93402: Btree *pBt;
! 93403: Pager *pPager;
! 93404: const char *zState = "unknown";
! 93405: int j;
! 93406: if( db->aDb[i].zName==0 ) continue;
! 93407: sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
! 93408: pBt = db->aDb[i].pBt;
! 93409: if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
! 93410: zState = "closed";
! 93411: }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
! 93412: SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
! 93413: zState = azLockName[j];
! 93414: }
! 93415: sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
! 93416: sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
! 93417: }
! 93418:
! 93419: }else
! 93420: #endif
! 93421:
! 93422: #ifdef SQLITE_HAS_CODEC
! 93423: if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
! 93424: sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
! 93425: }else
! 93426: if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
! 93427: sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
! 93428: }else
! 93429: if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
! 93430: sqlite3StrICmp(zLeft, "hexrekey")==0) ){
! 93431: int i, h1, h2;
! 93432: char zKey[40];
! 93433: for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
! 93434: h1 += 9*(1&(h1>>6));
! 93435: h2 += 9*(1&(h2>>6));
! 93436: zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
! 93437: }
! 93438: if( (zLeft[3] & 0xf)==0xb ){
! 93439: sqlite3_key(db, zKey, i/2);
! 93440: }else{
! 93441: sqlite3_rekey(db, zKey, i/2);
! 93442: }
! 93443: }else
! 93444: #endif
! 93445: #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
! 93446: if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
! 93447: #ifdef SQLITE_HAS_CODEC
! 93448: if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
! 93449: sqlite3_activate_see(&zRight[4]);
! 93450: }
! 93451: #endif
! 93452: #ifdef SQLITE_ENABLE_CEROD
! 93453: if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
! 93454: sqlite3_activate_cerod(&zRight[6]);
! 93455: }
! 93456: #endif
! 93457: }else
! 93458: #endif
! 93459:
! 93460:
! 93461: {/* Empty ELSE clause */}
! 93462:
! 93463: /*
! 93464: ** Reset the safety level, in case the fullfsync flag or synchronous
! 93465: ** setting changed.
! 93466: */
! 93467: #ifndef SQLITE_OMIT_PAGER_PRAGMAS
! 93468: if( db->autoCommit ){
! 93469: sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
! 93470: (db->flags&SQLITE_FullFSync)!=0,
! 93471: (db->flags&SQLITE_CkptFullFSync)!=0);
! 93472: }
! 93473: #endif
! 93474: pragma_out:
! 93475: sqlite3DbFree(db, zLeft);
! 93476: sqlite3DbFree(db, zRight);
! 93477: }
! 93478:
! 93479: #endif /* SQLITE_OMIT_PRAGMA */
! 93480:
! 93481: /************** End of pragma.c **********************************************/
! 93482: /************** Begin file prepare.c *****************************************/
! 93483: /*
! 93484: ** 2005 May 25
! 93485: **
! 93486: ** The author disclaims copyright to this source code. In place of
! 93487: ** a legal notice, here is a blessing:
! 93488: **
! 93489: ** May you do good and not evil.
! 93490: ** May you find forgiveness for yourself and forgive others.
! 93491: ** May you share freely, never taking more than you give.
! 93492: **
! 93493: *************************************************************************
! 93494: ** This file contains the implementation of the sqlite3_prepare()
! 93495: ** interface, and routines that contribute to loading the database schema
! 93496: ** from disk.
! 93497: */
! 93498:
! 93499: /*
! 93500: ** Fill the InitData structure with an error message that indicates
! 93501: ** that the database is corrupt.
! 93502: */
! 93503: static void corruptSchema(
! 93504: InitData *pData, /* Initialization context */
! 93505: const char *zObj, /* Object being parsed at the point of error */
! 93506: const char *zExtra /* Error information */
! 93507: ){
! 93508: sqlite3 *db = pData->db;
! 93509: if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
! 93510: if( zObj==0 ) zObj = "?";
! 93511: sqlite3SetString(pData->pzErrMsg, db,
! 93512: "malformed database schema (%s)", zObj);
! 93513: if( zExtra ){
! 93514: *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg,
! 93515: "%s - %s", *pData->pzErrMsg, zExtra);
! 93516: }
! 93517: }
! 93518: pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
! 93519: }
! 93520:
! 93521: /*
! 93522: ** This is the callback routine for the code that initializes the
! 93523: ** database. See sqlite3Init() below for additional information.
! 93524: ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
! 93525: **
! 93526: ** Each callback contains the following information:
! 93527: **
! 93528: ** argv[0] = name of thing being created
! 93529: ** argv[1] = root page number for table or index. 0 for trigger or view.
! 93530: ** argv[2] = SQL text for the CREATE statement.
! 93531: **
! 93532: */
! 93533: SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
! 93534: InitData *pData = (InitData*)pInit;
! 93535: sqlite3 *db = pData->db;
! 93536: int iDb = pData->iDb;
! 93537:
! 93538: assert( argc==3 );
! 93539: UNUSED_PARAMETER2(NotUsed, argc);
! 93540: assert( sqlite3_mutex_held(db->mutex) );
! 93541: DbClearProperty(db, iDb, DB_Empty);
! 93542: if( db->mallocFailed ){
! 93543: corruptSchema(pData, argv[0], 0);
! 93544: return 1;
! 93545: }
! 93546:
! 93547: assert( iDb>=0 && iDb<db->nDb );
! 93548: if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
! 93549: if( argv[1]==0 ){
! 93550: corruptSchema(pData, argv[0], 0);
! 93551: }else if( argv[2] && argv[2][0] ){
! 93552: /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
! 93553: ** But because db->init.busy is set to 1, no VDBE code is generated
! 93554: ** or executed. All the parser does is build the internal data
! 93555: ** structures that describe the table, index, or view.
! 93556: */
! 93557: int rc;
! 93558: sqlite3_stmt *pStmt;
! 93559: TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
! 93560:
! 93561: assert( db->init.busy );
! 93562: db->init.iDb = iDb;
! 93563: db->init.newTnum = sqlite3Atoi(argv[1]);
! 93564: db->init.orphanTrigger = 0;
! 93565: TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
! 93566: rc = db->errCode;
! 93567: assert( (rc&0xFF)==(rcp&0xFF) );
! 93568: db->init.iDb = 0;
! 93569: if( SQLITE_OK!=rc ){
! 93570: if( db->init.orphanTrigger ){
! 93571: assert( iDb==1 );
! 93572: }else{
! 93573: pData->rc = rc;
! 93574: if( rc==SQLITE_NOMEM ){
! 93575: db->mallocFailed = 1;
! 93576: }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
! 93577: corruptSchema(pData, argv[0], sqlite3_errmsg(db));
! 93578: }
! 93579: }
! 93580: }
! 93581: sqlite3_finalize(pStmt);
! 93582: }else if( argv[0]==0 ){
! 93583: corruptSchema(pData, 0, 0);
! 93584: }else{
! 93585: /* If the SQL column is blank it means this is an index that
! 93586: ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
! 93587: ** constraint for a CREATE TABLE. The index should have already
! 93588: ** been created when we processed the CREATE TABLE. All we have
! 93589: ** to do here is record the root page number for that index.
! 93590: */
! 93591: Index *pIndex;
! 93592: pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
! 93593: if( pIndex==0 ){
! 93594: /* This can occur if there exists an index on a TEMP table which
! 93595: ** has the same name as another index on a permanent index. Since
! 93596: ** the permanent table is hidden by the TEMP table, we can also
! 93597: ** safely ignore the index on the permanent table.
! 93598: */
! 93599: /* Do Nothing */;
! 93600: }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
! 93601: corruptSchema(pData, argv[0], "invalid rootpage");
! 93602: }
! 93603: }
! 93604: return 0;
! 93605: }
! 93606:
! 93607: /*
! 93608: ** Attempt to read the database schema and initialize internal
! 93609: ** data structures for a single database file. The index of the
! 93610: ** database file is given by iDb. iDb==0 is used for the main
! 93611: ** database. iDb==1 should never be used. iDb>=2 is used for
! 93612: ** auxiliary databases. Return one of the SQLITE_ error codes to
! 93613: ** indicate success or failure.
! 93614: */
! 93615: static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
! 93616: int rc;
! 93617: int i;
! 93618: int size;
! 93619: Table *pTab;
! 93620: Db *pDb;
! 93621: char const *azArg[4];
! 93622: int meta[5];
! 93623: InitData initData;
! 93624: char const *zMasterSchema;
! 93625: char const *zMasterName;
! 93626: int openedTransaction = 0;
! 93627:
! 93628: /*
! 93629: ** The master database table has a structure like this
! 93630: */
! 93631: static const char master_schema[] =
! 93632: "CREATE TABLE sqlite_master(\n"
! 93633: " type text,\n"
! 93634: " name text,\n"
! 93635: " tbl_name text,\n"
! 93636: " rootpage integer,\n"
! 93637: " sql text\n"
! 93638: ")"
! 93639: ;
! 93640: #ifndef SQLITE_OMIT_TEMPDB
! 93641: static const char temp_master_schema[] =
! 93642: "CREATE TEMP TABLE sqlite_temp_master(\n"
! 93643: " type text,\n"
! 93644: " name text,\n"
! 93645: " tbl_name text,\n"
! 93646: " rootpage integer,\n"
! 93647: " sql text\n"
! 93648: ")"
! 93649: ;
! 93650: #else
! 93651: #define temp_master_schema 0
! 93652: #endif
! 93653:
! 93654: assert( iDb>=0 && iDb<db->nDb );
! 93655: assert( db->aDb[iDb].pSchema );
! 93656: assert( sqlite3_mutex_held(db->mutex) );
! 93657: assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
! 93658:
! 93659: /* zMasterSchema and zInitScript are set to point at the master schema
! 93660: ** and initialisation script appropriate for the database being
! 93661: ** initialised. zMasterName is the name of the master table.
! 93662: */
! 93663: if( !OMIT_TEMPDB && iDb==1 ){
! 93664: zMasterSchema = temp_master_schema;
! 93665: }else{
! 93666: zMasterSchema = master_schema;
! 93667: }
! 93668: zMasterName = SCHEMA_TABLE(iDb);
! 93669:
! 93670: /* Construct the schema tables. */
! 93671: azArg[0] = zMasterName;
! 93672: azArg[1] = "1";
! 93673: azArg[2] = zMasterSchema;
! 93674: azArg[3] = 0;
! 93675: initData.db = db;
! 93676: initData.iDb = iDb;
! 93677: initData.rc = SQLITE_OK;
! 93678: initData.pzErrMsg = pzErrMsg;
! 93679: sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
! 93680: if( initData.rc ){
! 93681: rc = initData.rc;
! 93682: goto error_out;
! 93683: }
! 93684: pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
! 93685: if( ALWAYS(pTab) ){
! 93686: pTab->tabFlags |= TF_Readonly;
! 93687: }
! 93688:
! 93689: /* Create a cursor to hold the database open
! 93690: */
! 93691: pDb = &db->aDb[iDb];
! 93692: if( pDb->pBt==0 ){
! 93693: if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
! 93694: DbSetProperty(db, 1, DB_SchemaLoaded);
! 93695: }
! 93696: return SQLITE_OK;
! 93697: }
! 93698:
! 93699: /* If there is not already a read-only (or read-write) transaction opened
! 93700: ** on the b-tree database, open one now. If a transaction is opened, it
! 93701: ** will be closed before this function returns. */
! 93702: sqlite3BtreeEnter(pDb->pBt);
! 93703: if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
! 93704: rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
! 93705: if( rc!=SQLITE_OK ){
! 93706: sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
! 93707: goto initone_error_out;
! 93708: }
! 93709: openedTransaction = 1;
! 93710: }
! 93711:
! 93712: /* Get the database meta information.
! 93713: **
! 93714: ** Meta values are as follows:
! 93715: ** meta[0] Schema cookie. Changes with each schema change.
! 93716: ** meta[1] File format of schema layer.
! 93717: ** meta[2] Size of the page cache.
! 93718: ** meta[3] Largest rootpage (auto/incr_vacuum mode)
! 93719: ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
! 93720: ** meta[5] User version
! 93721: ** meta[6] Incremental vacuum mode
! 93722: ** meta[7] unused
! 93723: ** meta[8] unused
! 93724: ** meta[9] unused
! 93725: **
! 93726: ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
! 93727: ** the possible values of meta[4].
! 93728: */
! 93729: for(i=0; i<ArraySize(meta); i++){
! 93730: sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
! 93731: }
! 93732: pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
! 93733:
! 93734: /* If opening a non-empty database, check the text encoding. For the
! 93735: ** main database, set sqlite3.enc to the encoding of the main database.
! 93736: ** For an attached db, it is an error if the encoding is not the same
! 93737: ** as sqlite3.enc.
! 93738: */
! 93739: if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
! 93740: if( iDb==0 ){
! 93741: u8 encoding;
! 93742: /* If opening the main database, set ENC(db). */
! 93743: encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
! 93744: if( encoding==0 ) encoding = SQLITE_UTF8;
! 93745: ENC(db) = encoding;
! 93746: db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
! 93747: }else{
! 93748: /* If opening an attached database, the encoding much match ENC(db) */
! 93749: if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
! 93750: sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
! 93751: " text encoding as main database");
! 93752: rc = SQLITE_ERROR;
! 93753: goto initone_error_out;
! 93754: }
! 93755: }
! 93756: }else{
! 93757: DbSetProperty(db, iDb, DB_Empty);
! 93758: }
! 93759: pDb->pSchema->enc = ENC(db);
! 93760:
! 93761: if( pDb->pSchema->cache_size==0 ){
! 93762: #ifndef SQLITE_OMIT_DEPRECATED
! 93763: size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
! 93764: if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
! 93765: pDb->pSchema->cache_size = size;
! 93766: #else
! 93767: pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
! 93768: #endif
! 93769: sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
! 93770: }
! 93771:
! 93772: /*
! 93773: ** file_format==1 Version 3.0.0.
! 93774: ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
! 93775: ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
! 93776: ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
! 93777: */
! 93778: pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
! 93779: if( pDb->pSchema->file_format==0 ){
! 93780: pDb->pSchema->file_format = 1;
! 93781: }
! 93782: if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
! 93783: sqlite3SetString(pzErrMsg, db, "unsupported file format");
! 93784: rc = SQLITE_ERROR;
! 93785: goto initone_error_out;
! 93786: }
! 93787:
! 93788: /* Ticket #2804: When we open a database in the newer file format,
! 93789: ** clear the legacy_file_format pragma flag so that a VACUUM will
! 93790: ** not downgrade the database and thus invalidate any descending
! 93791: ** indices that the user might have created.
! 93792: */
! 93793: if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
! 93794: db->flags &= ~SQLITE_LegacyFileFmt;
! 93795: }
! 93796:
! 93797: /* Read the schema information out of the schema tables
! 93798: */
! 93799: assert( db->init.busy );
! 93800: {
! 93801: char *zSql;
! 93802: zSql = sqlite3MPrintf(db,
! 93803: "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
! 93804: db->aDb[iDb].zName, zMasterName);
! 93805: #ifndef SQLITE_OMIT_AUTHORIZATION
! 93806: {
! 93807: int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
! 93808: xAuth = db->xAuth;
! 93809: db->xAuth = 0;
! 93810: #endif
! 93811: rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
! 93812: #ifndef SQLITE_OMIT_AUTHORIZATION
! 93813: db->xAuth = xAuth;
! 93814: }
! 93815: #endif
! 93816: if( rc==SQLITE_OK ) rc = initData.rc;
! 93817: sqlite3DbFree(db, zSql);
! 93818: #ifndef SQLITE_OMIT_ANALYZE
! 93819: if( rc==SQLITE_OK ){
! 93820: sqlite3AnalysisLoad(db, iDb);
! 93821: }
! 93822: #endif
! 93823: }
! 93824: if( db->mallocFailed ){
! 93825: rc = SQLITE_NOMEM;
! 93826: sqlite3ResetInternalSchema(db, -1);
! 93827: }
! 93828: if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
! 93829: /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
! 93830: ** the schema loaded, even if errors occurred. In this situation the
! 93831: ** current sqlite3_prepare() operation will fail, but the following one
! 93832: ** will attempt to compile the supplied statement against whatever subset
! 93833: ** of the schema was loaded before the error occurred. The primary
! 93834: ** purpose of this is to allow access to the sqlite_master table
! 93835: ** even when its contents have been corrupted.
! 93836: */
! 93837: DbSetProperty(db, iDb, DB_SchemaLoaded);
! 93838: rc = SQLITE_OK;
! 93839: }
! 93840:
! 93841: /* Jump here for an error that occurs after successfully allocating
! 93842: ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
! 93843: ** before that point, jump to error_out.
! 93844: */
! 93845: initone_error_out:
! 93846: if( openedTransaction ){
! 93847: sqlite3BtreeCommit(pDb->pBt);
! 93848: }
! 93849: sqlite3BtreeLeave(pDb->pBt);
! 93850:
! 93851: error_out:
! 93852: if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
! 93853: db->mallocFailed = 1;
! 93854: }
! 93855: return rc;
! 93856: }
! 93857:
! 93858: /*
! 93859: ** Initialize all database files - the main database file, the file
! 93860: ** used to store temporary tables, and any additional database files
! 93861: ** created using ATTACH statements. Return a success code. If an
! 93862: ** error occurs, write an error message into *pzErrMsg.
! 93863: **
! 93864: ** After a database is initialized, the DB_SchemaLoaded bit is set
! 93865: ** bit is set in the flags field of the Db structure. If the database
! 93866: ** file was of zero-length, then the DB_Empty flag is also set.
! 93867: */
! 93868: SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
! 93869: int i, rc;
! 93870: int commit_internal = !(db->flags&SQLITE_InternChanges);
! 93871:
! 93872: assert( sqlite3_mutex_held(db->mutex) );
! 93873: rc = SQLITE_OK;
! 93874: db->init.busy = 1;
! 93875: for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
! 93876: if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
! 93877: rc = sqlite3InitOne(db, i, pzErrMsg);
! 93878: if( rc ){
! 93879: sqlite3ResetInternalSchema(db, i);
! 93880: }
! 93881: }
! 93882:
! 93883: /* Once all the other databases have been initialised, load the schema
! 93884: ** for the TEMP database. This is loaded last, as the TEMP database
! 93885: ** schema may contain references to objects in other databases.
! 93886: */
! 93887: #ifndef SQLITE_OMIT_TEMPDB
! 93888: if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
! 93889: && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
! 93890: rc = sqlite3InitOne(db, 1, pzErrMsg);
! 93891: if( rc ){
! 93892: sqlite3ResetInternalSchema(db, 1);
! 93893: }
! 93894: }
! 93895: #endif
! 93896:
! 93897: db->init.busy = 0;
! 93898: if( rc==SQLITE_OK && commit_internal ){
! 93899: sqlite3CommitInternalChanges(db);
! 93900: }
! 93901:
! 93902: return rc;
! 93903: }
! 93904:
! 93905: /*
! 93906: ** This routine is a no-op if the database schema is already initialised.
! 93907: ** Otherwise, the schema is loaded. An error code is returned.
! 93908: */
! 93909: SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
! 93910: int rc = SQLITE_OK;
! 93911: sqlite3 *db = pParse->db;
! 93912: assert( sqlite3_mutex_held(db->mutex) );
! 93913: if( !db->init.busy ){
! 93914: rc = sqlite3Init(db, &pParse->zErrMsg);
! 93915: }
! 93916: if( rc!=SQLITE_OK ){
! 93917: pParse->rc = rc;
! 93918: pParse->nErr++;
! 93919: }
! 93920: return rc;
! 93921: }
! 93922:
! 93923:
! 93924: /*
! 93925: ** Check schema cookies in all databases. If any cookie is out
! 93926: ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
! 93927: ** make no changes to pParse->rc.
! 93928: */
! 93929: static void schemaIsValid(Parse *pParse){
! 93930: sqlite3 *db = pParse->db;
! 93931: int iDb;
! 93932: int rc;
! 93933: int cookie;
! 93934:
! 93935: assert( pParse->checkSchema );
! 93936: assert( sqlite3_mutex_held(db->mutex) );
! 93937: for(iDb=0; iDb<db->nDb; iDb++){
! 93938: int openedTransaction = 0; /* True if a transaction is opened */
! 93939: Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
! 93940: if( pBt==0 ) continue;
! 93941:
! 93942: /* If there is not already a read-only (or read-write) transaction opened
! 93943: ** on the b-tree database, open one now. If a transaction is opened, it
! 93944: ** will be closed immediately after reading the meta-value. */
! 93945: if( !sqlite3BtreeIsInReadTrans(pBt) ){
! 93946: rc = sqlite3BtreeBeginTrans(pBt, 0);
! 93947: if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
! 93948: db->mallocFailed = 1;
! 93949: }
! 93950: if( rc!=SQLITE_OK ) return;
! 93951: openedTransaction = 1;
! 93952: }
! 93953:
! 93954: /* Read the schema cookie from the database. If it does not match the
! 93955: ** value stored as part of the in-memory schema representation,
! 93956: ** set Parse.rc to SQLITE_SCHEMA. */
! 93957: sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
! 93958: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 93959: if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
! 93960: sqlite3ResetInternalSchema(db, iDb);
! 93961: pParse->rc = SQLITE_SCHEMA;
! 93962: }
! 93963:
! 93964: /* Close the transaction, if one was opened. */
! 93965: if( openedTransaction ){
! 93966: sqlite3BtreeCommit(pBt);
! 93967: }
! 93968: }
! 93969: }
! 93970:
! 93971: /*
! 93972: ** Convert a schema pointer into the iDb index that indicates
! 93973: ** which database file in db->aDb[] the schema refers to.
! 93974: **
! 93975: ** If the same database is attached more than once, the first
! 93976: ** attached database is returned.
! 93977: */
! 93978: SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
! 93979: int i = -1000000;
! 93980:
! 93981: /* If pSchema is NULL, then return -1000000. This happens when code in
! 93982: ** expr.c is trying to resolve a reference to a transient table (i.e. one
! 93983: ** created by a sub-select). In this case the return value of this
! 93984: ** function should never be used.
! 93985: **
! 93986: ** We return -1000000 instead of the more usual -1 simply because using
! 93987: ** -1000000 as the incorrect index into db->aDb[] is much
! 93988: ** more likely to cause a segfault than -1 (of course there are assert()
! 93989: ** statements too, but it never hurts to play the odds).
! 93990: */
! 93991: assert( sqlite3_mutex_held(db->mutex) );
! 93992: if( pSchema ){
! 93993: for(i=0; ALWAYS(i<db->nDb); i++){
! 93994: if( db->aDb[i].pSchema==pSchema ){
! 93995: break;
! 93996: }
! 93997: }
! 93998: assert( i>=0 && i<db->nDb );
! 93999: }
! 94000: return i;
! 94001: }
! 94002:
! 94003: /*
! 94004: ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
! 94005: */
! 94006: static int sqlite3Prepare(
! 94007: sqlite3 *db, /* Database handle. */
! 94008: const char *zSql, /* UTF-8 encoded SQL statement. */
! 94009: int nBytes, /* Length of zSql in bytes. */
! 94010: int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
! 94011: Vdbe *pReprepare, /* VM being reprepared */
! 94012: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
! 94013: const char **pzTail /* OUT: End of parsed string */
! 94014: ){
! 94015: Parse *pParse; /* Parsing context */
! 94016: char *zErrMsg = 0; /* Error message */
! 94017: int rc = SQLITE_OK; /* Result code */
! 94018: int i; /* Loop counter */
! 94019:
! 94020: /* Allocate the parsing context */
! 94021: pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
! 94022: if( pParse==0 ){
! 94023: rc = SQLITE_NOMEM;
! 94024: goto end_prepare;
! 94025: }
! 94026: pParse->pReprepare = pReprepare;
! 94027: assert( ppStmt && *ppStmt==0 );
! 94028: assert( !db->mallocFailed );
! 94029: assert( sqlite3_mutex_held(db->mutex) );
! 94030:
! 94031: /* Check to verify that it is possible to get a read lock on all
! 94032: ** database schemas. The inability to get a read lock indicates that
! 94033: ** some other database connection is holding a write-lock, which in
! 94034: ** turn means that the other connection has made uncommitted changes
! 94035: ** to the schema.
! 94036: **
! 94037: ** Were we to proceed and prepare the statement against the uncommitted
! 94038: ** schema changes and if those schema changes are subsequently rolled
! 94039: ** back and different changes are made in their place, then when this
! 94040: ** prepared statement goes to run the schema cookie would fail to detect
! 94041: ** the schema change. Disaster would follow.
! 94042: **
! 94043: ** This thread is currently holding mutexes on all Btrees (because
! 94044: ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
! 94045: ** is not possible for another thread to start a new schema change
! 94046: ** while this routine is running. Hence, we do not need to hold
! 94047: ** locks on the schema, we just need to make sure nobody else is
! 94048: ** holding them.
! 94049: **
! 94050: ** Note that setting READ_UNCOMMITTED overrides most lock detection,
! 94051: ** but it does *not* override schema lock detection, so this all still
! 94052: ** works even if READ_UNCOMMITTED is set.
! 94053: */
! 94054: for(i=0; i<db->nDb; i++) {
! 94055: Btree *pBt = db->aDb[i].pBt;
! 94056: if( pBt ){
! 94057: assert( sqlite3BtreeHoldsMutex(pBt) );
! 94058: rc = sqlite3BtreeSchemaLocked(pBt);
! 94059: if( rc ){
! 94060: const char *zDb = db->aDb[i].zName;
! 94061: sqlite3Error(db, rc, "database schema is locked: %s", zDb);
! 94062: testcase( db->flags & SQLITE_ReadUncommitted );
! 94063: goto end_prepare;
! 94064: }
! 94065: }
! 94066: }
! 94067:
! 94068: sqlite3VtabUnlockList(db);
! 94069:
! 94070: pParse->db = db;
! 94071: pParse->nQueryLoop = (double)1;
! 94072: if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
! 94073: char *zSqlCopy;
! 94074: int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
! 94075: testcase( nBytes==mxLen );
! 94076: testcase( nBytes==mxLen+1 );
! 94077: if( nBytes>mxLen ){
! 94078: sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
! 94079: rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
! 94080: goto end_prepare;
! 94081: }
! 94082: zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
! 94083: if( zSqlCopy ){
! 94084: sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
! 94085: sqlite3DbFree(db, zSqlCopy);
! 94086: pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
! 94087: }else{
! 94088: pParse->zTail = &zSql[nBytes];
! 94089: }
! 94090: }else{
! 94091: sqlite3RunParser(pParse, zSql, &zErrMsg);
! 94092: }
! 94093: assert( 1==(int)pParse->nQueryLoop );
! 94094:
! 94095: if( db->mallocFailed ){
! 94096: pParse->rc = SQLITE_NOMEM;
! 94097: }
! 94098: if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
! 94099: if( pParse->checkSchema ){
! 94100: schemaIsValid(pParse);
! 94101: }
! 94102: if( db->mallocFailed ){
! 94103: pParse->rc = SQLITE_NOMEM;
! 94104: }
! 94105: if( pzTail ){
! 94106: *pzTail = pParse->zTail;
! 94107: }
! 94108: rc = pParse->rc;
! 94109:
! 94110: #ifndef SQLITE_OMIT_EXPLAIN
! 94111: if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
! 94112: static const char * const azColName[] = {
! 94113: "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
! 94114: "selectid", "order", "from", "detail"
! 94115: };
! 94116: int iFirst, mx;
! 94117: if( pParse->explain==2 ){
! 94118: sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
! 94119: iFirst = 8;
! 94120: mx = 12;
! 94121: }else{
! 94122: sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
! 94123: iFirst = 0;
! 94124: mx = 8;
! 94125: }
! 94126: for(i=iFirst; i<mx; i++){
! 94127: sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
! 94128: azColName[i], SQLITE_STATIC);
! 94129: }
! 94130: }
! 94131: #endif
! 94132:
! 94133: assert( db->init.busy==0 || saveSqlFlag==0 );
! 94134: if( db->init.busy==0 ){
! 94135: Vdbe *pVdbe = pParse->pVdbe;
! 94136: sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
! 94137: }
! 94138: if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
! 94139: sqlite3VdbeFinalize(pParse->pVdbe);
! 94140: assert(!(*ppStmt));
! 94141: }else{
! 94142: *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
! 94143: }
! 94144:
! 94145: if( zErrMsg ){
! 94146: sqlite3Error(db, rc, "%s", zErrMsg);
! 94147: sqlite3DbFree(db, zErrMsg);
! 94148: }else{
! 94149: sqlite3Error(db, rc, 0);
! 94150: }
! 94151:
! 94152: /* Delete any TriggerPrg structures allocated while parsing this statement. */
! 94153: while( pParse->pTriggerPrg ){
! 94154: TriggerPrg *pT = pParse->pTriggerPrg;
! 94155: pParse->pTriggerPrg = pT->pNext;
! 94156: sqlite3DbFree(db, pT);
! 94157: }
! 94158:
! 94159: end_prepare:
! 94160:
! 94161: sqlite3StackFree(db, pParse);
! 94162: rc = sqlite3ApiExit(db, rc);
! 94163: assert( (rc&db->errMask)==rc );
! 94164: return rc;
! 94165: }
! 94166: static int sqlite3LockAndPrepare(
! 94167: sqlite3 *db, /* Database handle. */
! 94168: const char *zSql, /* UTF-8 encoded SQL statement. */
! 94169: int nBytes, /* Length of zSql in bytes. */
! 94170: int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
! 94171: Vdbe *pOld, /* VM being reprepared */
! 94172: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
! 94173: const char **pzTail /* OUT: End of parsed string */
! 94174: ){
! 94175: int rc;
! 94176: assert( ppStmt!=0 );
! 94177: *ppStmt = 0;
! 94178: if( !sqlite3SafetyCheckOk(db) ){
! 94179: return SQLITE_MISUSE_BKPT;
! 94180: }
! 94181: sqlite3_mutex_enter(db->mutex);
! 94182: sqlite3BtreeEnterAll(db);
! 94183: rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
! 94184: if( rc==SQLITE_SCHEMA ){
! 94185: sqlite3_finalize(*ppStmt);
! 94186: rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
! 94187: }
! 94188: sqlite3BtreeLeaveAll(db);
! 94189: sqlite3_mutex_leave(db->mutex);
! 94190: return rc;
! 94191: }
! 94192:
! 94193: /*
! 94194: ** Rerun the compilation of a statement after a schema change.
! 94195: **
! 94196: ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
! 94197: ** if the statement cannot be recompiled because another connection has
! 94198: ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
! 94199: ** occurs, return SQLITE_SCHEMA.
! 94200: */
! 94201: SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
! 94202: int rc;
! 94203: sqlite3_stmt *pNew;
! 94204: const char *zSql;
! 94205: sqlite3 *db;
! 94206:
! 94207: assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
! 94208: zSql = sqlite3_sql((sqlite3_stmt *)p);
! 94209: assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */
! 94210: db = sqlite3VdbeDb(p);
! 94211: assert( sqlite3_mutex_held(db->mutex) );
! 94212: rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
! 94213: if( rc ){
! 94214: if( rc==SQLITE_NOMEM ){
! 94215: db->mallocFailed = 1;
! 94216: }
! 94217: assert( pNew==0 );
! 94218: return rc;
! 94219: }else{
! 94220: assert( pNew!=0 );
! 94221: }
! 94222: sqlite3VdbeSwap((Vdbe*)pNew, p);
! 94223: sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
! 94224: sqlite3VdbeResetStepResult((Vdbe*)pNew);
! 94225: sqlite3VdbeFinalize((Vdbe*)pNew);
! 94226: return SQLITE_OK;
! 94227: }
! 94228:
! 94229:
! 94230: /*
! 94231: ** Two versions of the official API. Legacy and new use. In the legacy
! 94232: ** version, the original SQL text is not saved in the prepared statement
! 94233: ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
! 94234: ** sqlite3_step(). In the new version, the original SQL text is retained
! 94235: ** and the statement is automatically recompiled if an schema change
! 94236: ** occurs.
! 94237: */
! 94238: SQLITE_API int sqlite3_prepare(
! 94239: sqlite3 *db, /* Database handle. */
! 94240: const char *zSql, /* UTF-8 encoded SQL statement. */
! 94241: int nBytes, /* Length of zSql in bytes. */
! 94242: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
! 94243: const char **pzTail /* OUT: End of parsed string */
! 94244: ){
! 94245: int rc;
! 94246: rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
! 94247: assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
! 94248: return rc;
! 94249: }
! 94250: SQLITE_API int sqlite3_prepare_v2(
! 94251: sqlite3 *db, /* Database handle. */
! 94252: const char *zSql, /* UTF-8 encoded SQL statement. */
! 94253: int nBytes, /* Length of zSql in bytes. */
! 94254: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
! 94255: const char **pzTail /* OUT: End of parsed string */
! 94256: ){
! 94257: int rc;
! 94258: rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
! 94259: assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
! 94260: return rc;
! 94261: }
! 94262:
! 94263:
! 94264: #ifndef SQLITE_OMIT_UTF16
! 94265: /*
! 94266: ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
! 94267: */
! 94268: static int sqlite3Prepare16(
! 94269: sqlite3 *db, /* Database handle. */
! 94270: const void *zSql, /* UTF-16 encoded SQL statement. */
! 94271: int nBytes, /* Length of zSql in bytes. */
! 94272: int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */
! 94273: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
! 94274: const void **pzTail /* OUT: End of parsed string */
! 94275: ){
! 94276: /* This function currently works by first transforming the UTF-16
! 94277: ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
! 94278: ** tricky bit is figuring out the pointer to return in *pzTail.
! 94279: */
! 94280: char *zSql8;
! 94281: const char *zTail8 = 0;
! 94282: int rc = SQLITE_OK;
! 94283:
! 94284: assert( ppStmt );
! 94285: *ppStmt = 0;
! 94286: if( !sqlite3SafetyCheckOk(db) ){
! 94287: return SQLITE_MISUSE_BKPT;
! 94288: }
! 94289: sqlite3_mutex_enter(db->mutex);
! 94290: zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
! 94291: if( zSql8 ){
! 94292: rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
! 94293: }
! 94294:
! 94295: if( zTail8 && pzTail ){
! 94296: /* If sqlite3_prepare returns a tail pointer, we calculate the
! 94297: ** equivalent pointer into the UTF-16 string by counting the unicode
! 94298: ** characters between zSql8 and zTail8, and then returning a pointer
! 94299: ** the same number of characters into the UTF-16 string.
! 94300: */
! 94301: int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
! 94302: *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
! 94303: }
! 94304: sqlite3DbFree(db, zSql8);
! 94305: rc = sqlite3ApiExit(db, rc);
! 94306: sqlite3_mutex_leave(db->mutex);
! 94307: return rc;
! 94308: }
! 94309:
! 94310: /*
! 94311: ** Two versions of the official API. Legacy and new use. In the legacy
! 94312: ** version, the original SQL text is not saved in the prepared statement
! 94313: ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
! 94314: ** sqlite3_step(). In the new version, the original SQL text is retained
! 94315: ** and the statement is automatically recompiled if an schema change
! 94316: ** occurs.
! 94317: */
! 94318: SQLITE_API int sqlite3_prepare16(
! 94319: sqlite3 *db, /* Database handle. */
! 94320: const void *zSql, /* UTF-16 encoded SQL statement. */
! 94321: int nBytes, /* Length of zSql in bytes. */
! 94322: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
! 94323: const void **pzTail /* OUT: End of parsed string */
! 94324: ){
! 94325: int rc;
! 94326: rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
! 94327: assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
! 94328: return rc;
! 94329: }
! 94330: SQLITE_API int sqlite3_prepare16_v2(
! 94331: sqlite3 *db, /* Database handle. */
! 94332: const void *zSql, /* UTF-16 encoded SQL statement. */
! 94333: int nBytes, /* Length of zSql in bytes. */
! 94334: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
! 94335: const void **pzTail /* OUT: End of parsed string */
! 94336: ){
! 94337: int rc;
! 94338: rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
! 94339: assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
! 94340: return rc;
! 94341: }
! 94342:
! 94343: #endif /* SQLITE_OMIT_UTF16 */
! 94344:
! 94345: /************** End of prepare.c *********************************************/
! 94346: /************** Begin file select.c ******************************************/
! 94347: /*
! 94348: ** 2001 September 15
! 94349: **
! 94350: ** The author disclaims copyright to this source code. In place of
! 94351: ** a legal notice, here is a blessing:
! 94352: **
! 94353: ** May you do good and not evil.
! 94354: ** May you find forgiveness for yourself and forgive others.
! 94355: ** May you share freely, never taking more than you give.
! 94356: **
! 94357: *************************************************************************
! 94358: ** This file contains C code routines that are called by the parser
! 94359: ** to handle SELECT statements in SQLite.
! 94360: */
! 94361:
! 94362:
! 94363: /*
! 94364: ** Delete all the content of a Select structure but do not deallocate
! 94365: ** the select structure itself.
! 94366: */
! 94367: static void clearSelect(sqlite3 *db, Select *p){
! 94368: sqlite3ExprListDelete(db, p->pEList);
! 94369: sqlite3SrcListDelete(db, p->pSrc);
! 94370: sqlite3ExprDelete(db, p->pWhere);
! 94371: sqlite3ExprListDelete(db, p->pGroupBy);
! 94372: sqlite3ExprDelete(db, p->pHaving);
! 94373: sqlite3ExprListDelete(db, p->pOrderBy);
! 94374: sqlite3SelectDelete(db, p->pPrior);
! 94375: sqlite3ExprDelete(db, p->pLimit);
! 94376: sqlite3ExprDelete(db, p->pOffset);
! 94377: }
! 94378:
! 94379: /*
! 94380: ** Initialize a SelectDest structure.
! 94381: */
! 94382: SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
! 94383: pDest->eDest = (u8)eDest;
! 94384: pDest->iParm = iParm;
! 94385: pDest->affinity = 0;
! 94386: pDest->iMem = 0;
! 94387: pDest->nMem = 0;
! 94388: }
! 94389:
! 94390:
! 94391: /*
! 94392: ** Allocate a new Select structure and return a pointer to that
! 94393: ** structure.
! 94394: */
! 94395: SQLITE_PRIVATE Select *sqlite3SelectNew(
! 94396: Parse *pParse, /* Parsing context */
! 94397: ExprList *pEList, /* which columns to include in the result */
! 94398: SrcList *pSrc, /* the FROM clause -- which tables to scan */
! 94399: Expr *pWhere, /* the WHERE clause */
! 94400: ExprList *pGroupBy, /* the GROUP BY clause */
! 94401: Expr *pHaving, /* the HAVING clause */
! 94402: ExprList *pOrderBy, /* the ORDER BY clause */
! 94403: int isDistinct, /* true if the DISTINCT keyword is present */
! 94404: Expr *pLimit, /* LIMIT value. NULL means not used */
! 94405: Expr *pOffset /* OFFSET value. NULL means no offset */
! 94406: ){
! 94407: Select *pNew;
! 94408: Select standin;
! 94409: sqlite3 *db = pParse->db;
! 94410: pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
! 94411: assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
! 94412: if( pNew==0 ){
! 94413: assert( db->mallocFailed );
! 94414: pNew = &standin;
! 94415: memset(pNew, 0, sizeof(*pNew));
! 94416: }
! 94417: if( pEList==0 ){
! 94418: pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
! 94419: }
! 94420: pNew->pEList = pEList;
! 94421: pNew->pSrc = pSrc;
! 94422: pNew->pWhere = pWhere;
! 94423: pNew->pGroupBy = pGroupBy;
! 94424: pNew->pHaving = pHaving;
! 94425: pNew->pOrderBy = pOrderBy;
! 94426: pNew->selFlags = isDistinct ? SF_Distinct : 0;
! 94427: pNew->op = TK_SELECT;
! 94428: pNew->pLimit = pLimit;
! 94429: pNew->pOffset = pOffset;
! 94430: assert( pOffset==0 || pLimit!=0 );
! 94431: pNew->addrOpenEphm[0] = -1;
! 94432: pNew->addrOpenEphm[1] = -1;
! 94433: pNew->addrOpenEphm[2] = -1;
! 94434: if( db->mallocFailed ) {
! 94435: clearSelect(db, pNew);
! 94436: if( pNew!=&standin ) sqlite3DbFree(db, pNew);
! 94437: pNew = 0;
! 94438: }else{
! 94439: assert( pNew->pSrc!=0 || pParse->nErr>0 );
! 94440: }
! 94441: assert( pNew!=&standin );
! 94442: return pNew;
! 94443: }
! 94444:
! 94445: /*
! 94446: ** Delete the given Select structure and all of its substructures.
! 94447: */
! 94448: SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
! 94449: if( p ){
! 94450: clearSelect(db, p);
! 94451: sqlite3DbFree(db, p);
! 94452: }
! 94453: }
! 94454:
! 94455: /*
! 94456: ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
! 94457: ** type of join. Return an integer constant that expresses that type
! 94458: ** in terms of the following bit values:
! 94459: **
! 94460: ** JT_INNER
! 94461: ** JT_CROSS
! 94462: ** JT_OUTER
! 94463: ** JT_NATURAL
! 94464: ** JT_LEFT
! 94465: ** JT_RIGHT
! 94466: **
! 94467: ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
! 94468: **
! 94469: ** If an illegal or unsupported join type is seen, then still return
! 94470: ** a join type, but put an error in the pParse structure.
! 94471: */
! 94472: SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
! 94473: int jointype = 0;
! 94474: Token *apAll[3];
! 94475: Token *p;
! 94476: /* 0123456789 123456789 123456789 123 */
! 94477: static const char zKeyText[] = "naturaleftouterightfullinnercross";
! 94478: static const struct {
! 94479: u8 i; /* Beginning of keyword text in zKeyText[] */
! 94480: u8 nChar; /* Length of the keyword in characters */
! 94481: u8 code; /* Join type mask */
! 94482: } aKeyword[] = {
! 94483: /* natural */ { 0, 7, JT_NATURAL },
! 94484: /* left */ { 6, 4, JT_LEFT|JT_OUTER },
! 94485: /* outer */ { 10, 5, JT_OUTER },
! 94486: /* right */ { 14, 5, JT_RIGHT|JT_OUTER },
! 94487: /* full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
! 94488: /* inner */ { 23, 5, JT_INNER },
! 94489: /* cross */ { 28, 5, JT_INNER|JT_CROSS },
! 94490: };
! 94491: int i, j;
! 94492: apAll[0] = pA;
! 94493: apAll[1] = pB;
! 94494: apAll[2] = pC;
! 94495: for(i=0; i<3 && apAll[i]; i++){
! 94496: p = apAll[i];
! 94497: for(j=0; j<ArraySize(aKeyword); j++){
! 94498: if( p->n==aKeyword[j].nChar
! 94499: && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
! 94500: jointype |= aKeyword[j].code;
! 94501: break;
! 94502: }
! 94503: }
! 94504: testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
! 94505: if( j>=ArraySize(aKeyword) ){
! 94506: jointype |= JT_ERROR;
! 94507: break;
! 94508: }
! 94509: }
! 94510: if(
! 94511: (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
! 94512: (jointype & JT_ERROR)!=0
! 94513: ){
! 94514: const char *zSp = " ";
! 94515: assert( pB!=0 );
! 94516: if( pC==0 ){ zSp++; }
! 94517: sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
! 94518: "%T %T%s%T", pA, pB, zSp, pC);
! 94519: jointype = JT_INNER;
! 94520: }else if( (jointype & JT_OUTER)!=0
! 94521: && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
! 94522: sqlite3ErrorMsg(pParse,
! 94523: "RIGHT and FULL OUTER JOINs are not currently supported");
! 94524: jointype = JT_INNER;
! 94525: }
! 94526: return jointype;
! 94527: }
! 94528:
! 94529: /*
! 94530: ** Return the index of a column in a table. Return -1 if the column
! 94531: ** is not contained in the table.
! 94532: */
! 94533: static int columnIndex(Table *pTab, const char *zCol){
! 94534: int i;
! 94535: for(i=0; i<pTab->nCol; i++){
! 94536: if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
! 94537: }
! 94538: return -1;
! 94539: }
! 94540:
! 94541: /*
! 94542: ** Search the first N tables in pSrc, from left to right, looking for a
! 94543: ** table that has a column named zCol.
! 94544: **
! 94545: ** When found, set *piTab and *piCol to the table index and column index
! 94546: ** of the matching column and return TRUE.
! 94547: **
! 94548: ** If not found, return FALSE.
! 94549: */
! 94550: static int tableAndColumnIndex(
! 94551: SrcList *pSrc, /* Array of tables to search */
! 94552: int N, /* Number of tables in pSrc->a[] to search */
! 94553: const char *zCol, /* Name of the column we are looking for */
! 94554: int *piTab, /* Write index of pSrc->a[] here */
! 94555: int *piCol /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
! 94556: ){
! 94557: int i; /* For looping over tables in pSrc */
! 94558: int iCol; /* Index of column matching zCol */
! 94559:
! 94560: assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */
! 94561: for(i=0; i<N; i++){
! 94562: iCol = columnIndex(pSrc->a[i].pTab, zCol);
! 94563: if( iCol>=0 ){
! 94564: if( piTab ){
! 94565: *piTab = i;
! 94566: *piCol = iCol;
! 94567: }
! 94568: return 1;
! 94569: }
! 94570: }
! 94571: return 0;
! 94572: }
! 94573:
! 94574: /*
! 94575: ** This function is used to add terms implied by JOIN syntax to the
! 94576: ** WHERE clause expression of a SELECT statement. The new term, which
! 94577: ** is ANDed with the existing WHERE clause, is of the form:
! 94578: **
! 94579: ** (tab1.col1 = tab2.col2)
! 94580: **
! 94581: ** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
! 94582: ** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
! 94583: ** column iColRight of tab2.
! 94584: */
! 94585: static void addWhereTerm(
! 94586: Parse *pParse, /* Parsing context */
! 94587: SrcList *pSrc, /* List of tables in FROM clause */
! 94588: int iLeft, /* Index of first table to join in pSrc */
! 94589: int iColLeft, /* Index of column in first table */
! 94590: int iRight, /* Index of second table in pSrc */
! 94591: int iColRight, /* Index of column in second table */
! 94592: int isOuterJoin, /* True if this is an OUTER join */
! 94593: Expr **ppWhere /* IN/OUT: The WHERE clause to add to */
! 94594: ){
! 94595: sqlite3 *db = pParse->db;
! 94596: Expr *pE1;
! 94597: Expr *pE2;
! 94598: Expr *pEq;
! 94599:
! 94600: assert( iLeft<iRight );
! 94601: assert( pSrc->nSrc>iRight );
! 94602: assert( pSrc->a[iLeft].pTab );
! 94603: assert( pSrc->a[iRight].pTab );
! 94604:
! 94605: pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
! 94606: pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
! 94607:
! 94608: pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
! 94609: if( pEq && isOuterJoin ){
! 94610: ExprSetProperty(pEq, EP_FromJoin);
! 94611: assert( !ExprHasAnyProperty(pEq, EP_TokenOnly|EP_Reduced) );
! 94612: ExprSetIrreducible(pEq);
! 94613: pEq->iRightJoinTable = (i16)pE2->iTable;
! 94614: }
! 94615: *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
! 94616: }
! 94617:
! 94618: /*
! 94619: ** Set the EP_FromJoin property on all terms of the given expression.
! 94620: ** And set the Expr.iRightJoinTable to iTable for every term in the
! 94621: ** expression.
! 94622: **
! 94623: ** The EP_FromJoin property is used on terms of an expression to tell
! 94624: ** the LEFT OUTER JOIN processing logic that this term is part of the
! 94625: ** join restriction specified in the ON or USING clause and not a part
! 94626: ** of the more general WHERE clause. These terms are moved over to the
! 94627: ** WHERE clause during join processing but we need to remember that they
! 94628: ** originated in the ON or USING clause.
! 94629: **
! 94630: ** The Expr.iRightJoinTable tells the WHERE clause processing that the
! 94631: ** expression depends on table iRightJoinTable even if that table is not
! 94632: ** explicitly mentioned in the expression. That information is needed
! 94633: ** for cases like this:
! 94634: **
! 94635: ** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
! 94636: **
! 94637: ** The where clause needs to defer the handling of the t1.x=5
! 94638: ** term until after the t2 loop of the join. In that way, a
! 94639: ** NULL t2 row will be inserted whenever t1.x!=5. If we do not
! 94640: ** defer the handling of t1.x=5, it will be processed immediately
! 94641: ** after the t1 loop and rows with t1.x!=5 will never appear in
! 94642: ** the output, which is incorrect.
! 94643: */
! 94644: static void setJoinExpr(Expr *p, int iTable){
! 94645: while( p ){
! 94646: ExprSetProperty(p, EP_FromJoin);
! 94647: assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
! 94648: ExprSetIrreducible(p);
! 94649: p->iRightJoinTable = (i16)iTable;
! 94650: setJoinExpr(p->pLeft, iTable);
! 94651: p = p->pRight;
! 94652: }
! 94653: }
! 94654:
! 94655: /*
! 94656: ** This routine processes the join information for a SELECT statement.
! 94657: ** ON and USING clauses are converted into extra terms of the WHERE clause.
! 94658: ** NATURAL joins also create extra WHERE clause terms.
! 94659: **
! 94660: ** The terms of a FROM clause are contained in the Select.pSrc structure.
! 94661: ** The left most table is the first entry in Select.pSrc. The right-most
! 94662: ** table is the last entry. The join operator is held in the entry to
! 94663: ** the left. Thus entry 0 contains the join operator for the join between
! 94664: ** entries 0 and 1. Any ON or USING clauses associated with the join are
! 94665: ** also attached to the left entry.
! 94666: **
! 94667: ** This routine returns the number of errors encountered.
! 94668: */
! 94669: static int sqliteProcessJoin(Parse *pParse, Select *p){
! 94670: SrcList *pSrc; /* All tables in the FROM clause */
! 94671: int i, j; /* Loop counters */
! 94672: struct SrcList_item *pLeft; /* Left table being joined */
! 94673: struct SrcList_item *pRight; /* Right table being joined */
! 94674:
! 94675: pSrc = p->pSrc;
! 94676: pLeft = &pSrc->a[0];
! 94677: pRight = &pLeft[1];
! 94678: for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
! 94679: Table *pLeftTab = pLeft->pTab;
! 94680: Table *pRightTab = pRight->pTab;
! 94681: int isOuter;
! 94682:
! 94683: if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
! 94684: isOuter = (pRight->jointype & JT_OUTER)!=0;
! 94685:
! 94686: /* When the NATURAL keyword is present, add WHERE clause terms for
! 94687: ** every column that the two tables have in common.
! 94688: */
! 94689: if( pRight->jointype & JT_NATURAL ){
! 94690: if( pRight->pOn || pRight->pUsing ){
! 94691: sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
! 94692: "an ON or USING clause", 0);
! 94693: return 1;
! 94694: }
! 94695: for(j=0; j<pRightTab->nCol; j++){
! 94696: char *zName; /* Name of column in the right table */
! 94697: int iLeft; /* Matching left table */
! 94698: int iLeftCol; /* Matching column in the left table */
! 94699:
! 94700: zName = pRightTab->aCol[j].zName;
! 94701: if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
! 94702: addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
! 94703: isOuter, &p->pWhere);
! 94704: }
! 94705: }
! 94706: }
! 94707:
! 94708: /* Disallow both ON and USING clauses in the same join
! 94709: */
! 94710: if( pRight->pOn && pRight->pUsing ){
! 94711: sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
! 94712: "clauses in the same join");
! 94713: return 1;
! 94714: }
! 94715:
! 94716: /* Add the ON clause to the end of the WHERE clause, connected by
! 94717: ** an AND operator.
! 94718: */
! 94719: if( pRight->pOn ){
! 94720: if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
! 94721: p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
! 94722: pRight->pOn = 0;
! 94723: }
! 94724:
! 94725: /* Create extra terms on the WHERE clause for each column named
! 94726: ** in the USING clause. Example: If the two tables to be joined are
! 94727: ** A and B and the USING clause names X, Y, and Z, then add this
! 94728: ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
! 94729: ** Report an error if any column mentioned in the USING clause is
! 94730: ** not contained in both tables to be joined.
! 94731: */
! 94732: if( pRight->pUsing ){
! 94733: IdList *pList = pRight->pUsing;
! 94734: for(j=0; j<pList->nId; j++){
! 94735: char *zName; /* Name of the term in the USING clause */
! 94736: int iLeft; /* Table on the left with matching column name */
! 94737: int iLeftCol; /* Column number of matching column on the left */
! 94738: int iRightCol; /* Column number of matching column on the right */
! 94739:
! 94740: zName = pList->a[j].zName;
! 94741: iRightCol = columnIndex(pRightTab, zName);
! 94742: if( iRightCol<0
! 94743: || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
! 94744: ){
! 94745: sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
! 94746: "not present in both tables", zName);
! 94747: return 1;
! 94748: }
! 94749: addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
! 94750: isOuter, &p->pWhere);
! 94751: }
! 94752: }
! 94753: }
! 94754: return 0;
! 94755: }
! 94756:
! 94757: /*
! 94758: ** Insert code into "v" that will push the record on the top of the
! 94759: ** stack into the sorter.
! 94760: */
! 94761: static void pushOntoSorter(
! 94762: Parse *pParse, /* Parser context */
! 94763: ExprList *pOrderBy, /* The ORDER BY clause */
! 94764: Select *pSelect, /* The whole SELECT statement */
! 94765: int regData /* Register holding data to be sorted */
! 94766: ){
! 94767: Vdbe *v = pParse->pVdbe;
! 94768: int nExpr = pOrderBy->nExpr;
! 94769: int regBase = sqlite3GetTempRange(pParse, nExpr+2);
! 94770: int regRecord = sqlite3GetTempReg(pParse);
! 94771: int op;
! 94772: sqlite3ExprCacheClear(pParse);
! 94773: sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
! 94774: sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
! 94775: sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
! 94776: sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
! 94777: if( pSelect->selFlags & SF_UseSorter ){
! 94778: op = OP_SorterInsert;
! 94779: }else{
! 94780: op = OP_IdxInsert;
! 94781: }
! 94782: sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
! 94783: sqlite3ReleaseTempReg(pParse, regRecord);
! 94784: sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
! 94785: if( pSelect->iLimit ){
! 94786: int addr1, addr2;
! 94787: int iLimit;
! 94788: if( pSelect->iOffset ){
! 94789: iLimit = pSelect->iOffset+1;
! 94790: }else{
! 94791: iLimit = pSelect->iLimit;
! 94792: }
! 94793: addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit);
! 94794: sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
! 94795: addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
! 94796: sqlite3VdbeJumpHere(v, addr1);
! 94797: sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
! 94798: sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
! 94799: sqlite3VdbeJumpHere(v, addr2);
! 94800: }
! 94801: }
! 94802:
! 94803: /*
! 94804: ** Add code to implement the OFFSET
! 94805: */
! 94806: static void codeOffset(
! 94807: Vdbe *v, /* Generate code into this VM */
! 94808: Select *p, /* The SELECT statement being coded */
! 94809: int iContinue /* Jump here to skip the current record */
! 94810: ){
! 94811: if( p->iOffset && iContinue!=0 ){
! 94812: int addr;
! 94813: sqlite3VdbeAddOp2(v, OP_AddImm, p->iOffset, -1);
! 94814: addr = sqlite3VdbeAddOp1(v, OP_IfNeg, p->iOffset);
! 94815: sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
! 94816: VdbeComment((v, "skip OFFSET records"));
! 94817: sqlite3VdbeJumpHere(v, addr);
! 94818: }
! 94819: }
! 94820:
! 94821: /*
! 94822: ** Add code that will check to make sure the N registers starting at iMem
! 94823: ** form a distinct entry. iTab is a sorting index that holds previously
! 94824: ** seen combinations of the N values. A new entry is made in iTab
! 94825: ** if the current N values are new.
! 94826: **
! 94827: ** A jump to addrRepeat is made and the N+1 values are popped from the
! 94828: ** stack if the top N elements are not distinct.
! 94829: */
! 94830: static void codeDistinct(
! 94831: Parse *pParse, /* Parsing and code generating context */
! 94832: int iTab, /* A sorting index used to test for distinctness */
! 94833: int addrRepeat, /* Jump to here if not distinct */
! 94834: int N, /* Number of elements */
! 94835: int iMem /* First element */
! 94836: ){
! 94837: Vdbe *v;
! 94838: int r1;
! 94839:
! 94840: v = pParse->pVdbe;
! 94841: r1 = sqlite3GetTempReg(pParse);
! 94842: sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N);
! 94843: sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
! 94844: sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
! 94845: sqlite3ReleaseTempReg(pParse, r1);
! 94846: }
! 94847:
! 94848: #ifndef SQLITE_OMIT_SUBQUERY
! 94849: /*
! 94850: ** Generate an error message when a SELECT is used within a subexpression
! 94851: ** (example: "a IN (SELECT * FROM table)") but it has more than 1 result
! 94852: ** column. We do this in a subroutine because the error used to occur
! 94853: ** in multiple places. (The error only occurs in one place now, but we
! 94854: ** retain the subroutine to minimize code disruption.)
! 94855: */
! 94856: static int checkForMultiColumnSelectError(
! 94857: Parse *pParse, /* Parse context. */
! 94858: SelectDest *pDest, /* Destination of SELECT results */
! 94859: int nExpr /* Number of result columns returned by SELECT */
! 94860: ){
! 94861: int eDest = pDest->eDest;
! 94862: if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
! 94863: sqlite3ErrorMsg(pParse, "only a single result allowed for "
! 94864: "a SELECT that is part of an expression");
! 94865: return 1;
! 94866: }else{
! 94867: return 0;
! 94868: }
! 94869: }
! 94870: #endif
! 94871:
! 94872: /*
! 94873: ** This routine generates the code for the inside of the inner loop
! 94874: ** of a SELECT.
! 94875: **
! 94876: ** If srcTab and nColumn are both zero, then the pEList expressions
! 94877: ** are evaluated in order to get the data for this row. If nColumn>0
! 94878: ** then data is pulled from srcTab and pEList is used only to get the
! 94879: ** datatypes for each column.
! 94880: */
! 94881: static void selectInnerLoop(
! 94882: Parse *pParse, /* The parser context */
! 94883: Select *p, /* The complete select statement being coded */
! 94884: ExprList *pEList, /* List of values being extracted */
! 94885: int srcTab, /* Pull data from this table */
! 94886: int nColumn, /* Number of columns in the source table */
! 94887: ExprList *pOrderBy, /* If not NULL, sort results using this key */
! 94888: int distinct, /* If >=0, make sure results are distinct */
! 94889: SelectDest *pDest, /* How to dispose of the results */
! 94890: int iContinue, /* Jump here to continue with next row */
! 94891: int iBreak /* Jump here to break out of the inner loop */
! 94892: ){
! 94893: Vdbe *v = pParse->pVdbe;
! 94894: int i;
! 94895: int hasDistinct; /* True if the DISTINCT keyword is present */
! 94896: int regResult; /* Start of memory holding result set */
! 94897: int eDest = pDest->eDest; /* How to dispose of results */
! 94898: int iParm = pDest->iParm; /* First argument to disposal method */
! 94899: int nResultCol; /* Number of result columns */
! 94900:
! 94901: assert( v );
! 94902: if( NEVER(v==0) ) return;
! 94903: assert( pEList!=0 );
! 94904: hasDistinct = distinct>=0;
! 94905: if( pOrderBy==0 && !hasDistinct ){
! 94906: codeOffset(v, p, iContinue);
! 94907: }
! 94908:
! 94909: /* Pull the requested columns.
! 94910: */
! 94911: if( nColumn>0 ){
! 94912: nResultCol = nColumn;
! 94913: }else{
! 94914: nResultCol = pEList->nExpr;
! 94915: }
! 94916: if( pDest->iMem==0 ){
! 94917: pDest->iMem = pParse->nMem+1;
! 94918: pDest->nMem = nResultCol;
! 94919: pParse->nMem += nResultCol;
! 94920: }else{
! 94921: assert( pDest->nMem==nResultCol );
! 94922: }
! 94923: regResult = pDest->iMem;
! 94924: if( nColumn>0 ){
! 94925: for(i=0; i<nColumn; i++){
! 94926: sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
! 94927: }
! 94928: }else if( eDest!=SRT_Exists ){
! 94929: /* If the destination is an EXISTS(...) expression, the actual
! 94930: ** values returned by the SELECT are not required.
! 94931: */
! 94932: sqlite3ExprCacheClear(pParse);
! 94933: sqlite3ExprCodeExprList(pParse, pEList, regResult, eDest==SRT_Output);
! 94934: }
! 94935: nColumn = nResultCol;
! 94936:
! 94937: /* If the DISTINCT keyword was present on the SELECT statement
! 94938: ** and this row has been seen before, then do not make this row
! 94939: ** part of the result.
! 94940: */
! 94941: if( hasDistinct ){
! 94942: assert( pEList!=0 );
! 94943: assert( pEList->nExpr==nColumn );
! 94944: codeDistinct(pParse, distinct, iContinue, nColumn, regResult);
! 94945: if( pOrderBy==0 ){
! 94946: codeOffset(v, p, iContinue);
! 94947: }
! 94948: }
! 94949:
! 94950: switch( eDest ){
! 94951: /* In this mode, write each query result to the key of the temporary
! 94952: ** table iParm.
! 94953: */
! 94954: #ifndef SQLITE_OMIT_COMPOUND_SELECT
! 94955: case SRT_Union: {
! 94956: int r1;
! 94957: r1 = sqlite3GetTempReg(pParse);
! 94958: sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
! 94959: sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
! 94960: sqlite3ReleaseTempReg(pParse, r1);
! 94961: break;
! 94962: }
! 94963:
! 94964: /* Construct a record from the query result, but instead of
! 94965: ** saving that record, use it as a key to delete elements from
! 94966: ** the temporary table iParm.
! 94967: */
! 94968: case SRT_Except: {
! 94969: sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nColumn);
! 94970: break;
! 94971: }
! 94972: #endif
! 94973:
! 94974: /* Store the result as data using a unique key.
! 94975: */
! 94976: case SRT_Table:
! 94977: case SRT_EphemTab: {
! 94978: int r1 = sqlite3GetTempReg(pParse);
! 94979: testcase( eDest==SRT_Table );
! 94980: testcase( eDest==SRT_EphemTab );
! 94981: sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
! 94982: if( pOrderBy ){
! 94983: pushOntoSorter(pParse, pOrderBy, p, r1);
! 94984: }else{
! 94985: int r2 = sqlite3GetTempReg(pParse);
! 94986: sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
! 94987: sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
! 94988: sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
! 94989: sqlite3ReleaseTempReg(pParse, r2);
! 94990: }
! 94991: sqlite3ReleaseTempReg(pParse, r1);
! 94992: break;
! 94993: }
! 94994:
! 94995: #ifndef SQLITE_OMIT_SUBQUERY
! 94996: /* If we are creating a set for an "expr IN (SELECT ...)" construct,
! 94997: ** then there should be a single item on the stack. Write this
! 94998: ** item into the set table with bogus data.
! 94999: */
! 95000: case SRT_Set: {
! 95001: assert( nColumn==1 );
! 95002: p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affinity);
! 95003: if( pOrderBy ){
! 95004: /* At first glance you would think we could optimize out the
! 95005: ** ORDER BY in this case since the order of entries in the set
! 95006: ** does not matter. But there might be a LIMIT clause, in which
! 95007: ** case the order does matter */
! 95008: pushOntoSorter(pParse, pOrderBy, p, regResult);
! 95009: }else{
! 95010: int r1 = sqlite3GetTempReg(pParse);
! 95011: sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1);
! 95012: sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
! 95013: sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
! 95014: sqlite3ReleaseTempReg(pParse, r1);
! 95015: }
! 95016: break;
! 95017: }
! 95018:
! 95019: /* If any row exist in the result set, record that fact and abort.
! 95020: */
! 95021: case SRT_Exists: {
! 95022: sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
! 95023: /* The LIMIT clause will terminate the loop for us */
! 95024: break;
! 95025: }
! 95026:
! 95027: /* If this is a scalar select that is part of an expression, then
! 95028: ** store the results in the appropriate memory cell and break out
! 95029: ** of the scan loop.
! 95030: */
! 95031: case SRT_Mem: {
! 95032: assert( nColumn==1 );
! 95033: if( pOrderBy ){
! 95034: pushOntoSorter(pParse, pOrderBy, p, regResult);
! 95035: }else{
! 95036: sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
! 95037: /* The LIMIT clause will jump out of the loop for us */
! 95038: }
! 95039: break;
! 95040: }
! 95041: #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
! 95042:
! 95043: /* Send the data to the callback function or to a subroutine. In the
! 95044: ** case of a subroutine, the subroutine itself is responsible for
! 95045: ** popping the data from the stack.
! 95046: */
! 95047: case SRT_Coroutine:
! 95048: case SRT_Output: {
! 95049: testcase( eDest==SRT_Coroutine );
! 95050: testcase( eDest==SRT_Output );
! 95051: if( pOrderBy ){
! 95052: int r1 = sqlite3GetTempReg(pParse);
! 95053: sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
! 95054: pushOntoSorter(pParse, pOrderBy, p, r1);
! 95055: sqlite3ReleaseTempReg(pParse, r1);
! 95056: }else if( eDest==SRT_Coroutine ){
! 95057: sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
! 95058: }else{
! 95059: sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
! 95060: sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
! 95061: }
! 95062: break;
! 95063: }
! 95064:
! 95065: #if !defined(SQLITE_OMIT_TRIGGER)
! 95066: /* Discard the results. This is used for SELECT statements inside
! 95067: ** the body of a TRIGGER. The purpose of such selects is to call
! 95068: ** user-defined functions that have side effects. We do not care
! 95069: ** about the actual results of the select.
! 95070: */
! 95071: default: {
! 95072: assert( eDest==SRT_Discard );
! 95073: break;
! 95074: }
! 95075: #endif
! 95076: }
! 95077:
! 95078: /* Jump to the end of the loop if the LIMIT is reached. Except, if
! 95079: ** there is a sorter, in which case the sorter has already limited
! 95080: ** the output for us.
! 95081: */
! 95082: if( pOrderBy==0 && p->iLimit ){
! 95083: sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
! 95084: }
! 95085: }
! 95086:
! 95087: /*
! 95088: ** Given an expression list, generate a KeyInfo structure that records
! 95089: ** the collating sequence for each expression in that expression list.
! 95090: **
! 95091: ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
! 95092: ** KeyInfo structure is appropriate for initializing a virtual index to
! 95093: ** implement that clause. If the ExprList is the result set of a SELECT
! 95094: ** then the KeyInfo structure is appropriate for initializing a virtual
! 95095: ** index to implement a DISTINCT test.
! 95096: **
! 95097: ** Space to hold the KeyInfo structure is obtain from malloc. The calling
! 95098: ** function is responsible for seeing that this structure is eventually
! 95099: ** freed. Add the KeyInfo structure to the P4 field of an opcode using
! 95100: ** P4_KEYINFO_HANDOFF is the usual way of dealing with this.
! 95101: */
! 95102: static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
! 95103: sqlite3 *db = pParse->db;
! 95104: int nExpr;
! 95105: KeyInfo *pInfo;
! 95106: struct ExprList_item *pItem;
! 95107: int i;
! 95108:
! 95109: nExpr = pList->nExpr;
! 95110: pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
! 95111: if( pInfo ){
! 95112: pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
! 95113: pInfo->nField = (u16)nExpr;
! 95114: pInfo->enc = ENC(db);
! 95115: pInfo->db = db;
! 95116: for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
! 95117: CollSeq *pColl;
! 95118: pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
! 95119: if( !pColl ){
! 95120: pColl = db->pDfltColl;
! 95121: }
! 95122: pInfo->aColl[i] = pColl;
! 95123: pInfo->aSortOrder[i] = pItem->sortOrder;
! 95124: }
! 95125: }
! 95126: return pInfo;
! 95127: }
! 95128:
! 95129: #ifndef SQLITE_OMIT_COMPOUND_SELECT
! 95130: /*
! 95131: ** Name of the connection operator, used for error messages.
! 95132: */
! 95133: static const char *selectOpName(int id){
! 95134: char *z;
! 95135: switch( id ){
! 95136: case TK_ALL: z = "UNION ALL"; break;
! 95137: case TK_INTERSECT: z = "INTERSECT"; break;
! 95138: case TK_EXCEPT: z = "EXCEPT"; break;
! 95139: default: z = "UNION"; break;
! 95140: }
! 95141: return z;
! 95142: }
! 95143: #endif /* SQLITE_OMIT_COMPOUND_SELECT */
! 95144:
! 95145: #ifndef SQLITE_OMIT_EXPLAIN
! 95146: /*
! 95147: ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
! 95148: ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
! 95149: ** where the caption is of the form:
! 95150: **
! 95151: ** "USE TEMP B-TREE FOR xxx"
! 95152: **
! 95153: ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
! 95154: ** is determined by the zUsage argument.
! 95155: */
! 95156: static void explainTempTable(Parse *pParse, const char *zUsage){
! 95157: if( pParse->explain==2 ){
! 95158: Vdbe *v = pParse->pVdbe;
! 95159: char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
! 95160: sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
! 95161: }
! 95162: }
! 95163:
! 95164: /*
! 95165: ** Assign expression b to lvalue a. A second, no-op, version of this macro
! 95166: ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
! 95167: ** in sqlite3Select() to assign values to structure member variables that
! 95168: ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
! 95169: ** code with #ifndef directives.
! 95170: */
! 95171: # define explainSetInteger(a, b) a = b
! 95172:
! 95173: #else
! 95174: /* No-op versions of the explainXXX() functions and macros. */
! 95175: # define explainTempTable(y,z)
! 95176: # define explainSetInteger(y,z)
! 95177: #endif
! 95178:
! 95179: #if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
! 95180: /*
! 95181: ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
! 95182: ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
! 95183: ** where the caption is of one of the two forms:
! 95184: **
! 95185: ** "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
! 95186: ** "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
! 95187: **
! 95188: ** where iSub1 and iSub2 are the integers passed as the corresponding
! 95189: ** function parameters, and op is the text representation of the parameter
! 95190: ** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
! 95191: ** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is
! 95192: ** false, or the second form if it is true.
! 95193: */
! 95194: static void explainComposite(
! 95195: Parse *pParse, /* Parse context */
! 95196: int op, /* One of TK_UNION, TK_EXCEPT etc. */
! 95197: int iSub1, /* Subquery id 1 */
! 95198: int iSub2, /* Subquery id 2 */
! 95199: int bUseTmp /* True if a temp table was used */
! 95200: ){
! 95201: assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
! 95202: if( pParse->explain==2 ){
! 95203: Vdbe *v = pParse->pVdbe;
! 95204: char *zMsg = sqlite3MPrintf(
! 95205: pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
! 95206: bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
! 95207: );
! 95208: sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
! 95209: }
! 95210: }
! 95211: #else
! 95212: /* No-op versions of the explainXXX() functions and macros. */
! 95213: # define explainComposite(v,w,x,y,z)
! 95214: #endif
! 95215:
! 95216: /*
! 95217: ** If the inner loop was generated using a non-null pOrderBy argument,
! 95218: ** then the results were placed in a sorter. After the loop is terminated
! 95219: ** we need to run the sorter and output the results. The following
! 95220: ** routine generates the code needed to do that.
! 95221: */
! 95222: static void generateSortTail(
! 95223: Parse *pParse, /* Parsing context */
! 95224: Select *p, /* The SELECT statement */
! 95225: Vdbe *v, /* Generate code into this VDBE */
! 95226: int nColumn, /* Number of columns of data */
! 95227: SelectDest *pDest /* Write the sorted results here */
! 95228: ){
! 95229: int addrBreak = sqlite3VdbeMakeLabel(v); /* Jump here to exit loop */
! 95230: int addrContinue = sqlite3VdbeMakeLabel(v); /* Jump here for next cycle */
! 95231: int addr;
! 95232: int iTab;
! 95233: int pseudoTab = 0;
! 95234: ExprList *pOrderBy = p->pOrderBy;
! 95235:
! 95236: int eDest = pDest->eDest;
! 95237: int iParm = pDest->iParm;
! 95238:
! 95239: int regRow;
! 95240: int regRowid;
! 95241:
! 95242: iTab = pOrderBy->iECursor;
! 95243: regRow = sqlite3GetTempReg(pParse);
! 95244: if( eDest==SRT_Output || eDest==SRT_Coroutine ){
! 95245: pseudoTab = pParse->nTab++;
! 95246: sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
! 95247: regRowid = 0;
! 95248: }else{
! 95249: regRowid = sqlite3GetTempReg(pParse);
! 95250: }
! 95251: if( p->selFlags & SF_UseSorter ){
! 95252: int regSortOut = ++pParse->nMem;
! 95253: int ptab2 = pParse->nTab++;
! 95254: sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
! 95255: addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
! 95256: codeOffset(v, p, addrContinue);
! 95257: sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
! 95258: sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
! 95259: sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
! 95260: }else{
! 95261: addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
! 95262: codeOffset(v, p, addrContinue);
! 95263: sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
! 95264: }
! 95265: switch( eDest ){
! 95266: case SRT_Table:
! 95267: case SRT_EphemTab: {
! 95268: testcase( eDest==SRT_Table );
! 95269: testcase( eDest==SRT_EphemTab );
! 95270: sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
! 95271: sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
! 95272: sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
! 95273: break;
! 95274: }
! 95275: #ifndef SQLITE_OMIT_SUBQUERY
! 95276: case SRT_Set: {
! 95277: assert( nColumn==1 );
! 95278: sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1);
! 95279: sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
! 95280: sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
! 95281: break;
! 95282: }
! 95283: case SRT_Mem: {
! 95284: assert( nColumn==1 );
! 95285: sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
! 95286: /* The LIMIT clause will terminate the loop for us */
! 95287: break;
! 95288: }
! 95289: #endif
! 95290: default: {
! 95291: int i;
! 95292: assert( eDest==SRT_Output || eDest==SRT_Coroutine );
! 95293: testcase( eDest==SRT_Output );
! 95294: testcase( eDest==SRT_Coroutine );
! 95295: for(i=0; i<nColumn; i++){
! 95296: assert( regRow!=pDest->iMem+i );
! 95297: sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i);
! 95298: if( i==0 ){
! 95299: sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
! 95300: }
! 95301: }
! 95302: if( eDest==SRT_Output ){
! 95303: sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn);
! 95304: sqlite3ExprCacheAffinityChange(pParse, pDest->iMem, nColumn);
! 95305: }else{
! 95306: sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
! 95307: }
! 95308: break;
! 95309: }
! 95310: }
! 95311: sqlite3ReleaseTempReg(pParse, regRow);
! 95312: sqlite3ReleaseTempReg(pParse, regRowid);
! 95313:
! 95314: /* The bottom of the loop
! 95315: */
! 95316: sqlite3VdbeResolveLabel(v, addrContinue);
! 95317: if( p->selFlags & SF_UseSorter ){
! 95318: sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
! 95319: }else{
! 95320: sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
! 95321: }
! 95322: sqlite3VdbeResolveLabel(v, addrBreak);
! 95323: if( eDest==SRT_Output || eDest==SRT_Coroutine ){
! 95324: sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
! 95325: }
! 95326: }
! 95327:
! 95328: /*
! 95329: ** Return a pointer to a string containing the 'declaration type' of the
! 95330: ** expression pExpr. The string may be treated as static by the caller.
! 95331: **
! 95332: ** The declaration type is the exact datatype definition extracted from the
! 95333: ** original CREATE TABLE statement if the expression is a column. The
! 95334: ** declaration type for a ROWID field is INTEGER. Exactly when an expression
! 95335: ** is considered a column can be complex in the presence of subqueries. The
! 95336: ** result-set expression in all of the following SELECT statements is
! 95337: ** considered a column by this function.
! 95338: **
! 95339: ** SELECT col FROM tbl;
! 95340: ** SELECT (SELECT col FROM tbl;
! 95341: ** SELECT (SELECT col FROM tbl);
! 95342: ** SELECT abc FROM (SELECT col AS abc FROM tbl);
! 95343: **
! 95344: ** The declaration type for any expression other than a column is NULL.
! 95345: */
! 95346: static const char *columnType(
! 95347: NameContext *pNC,
! 95348: Expr *pExpr,
! 95349: const char **pzOriginDb,
! 95350: const char **pzOriginTab,
! 95351: const char **pzOriginCol
! 95352: ){
! 95353: char const *zType = 0;
! 95354: char const *zOriginDb = 0;
! 95355: char const *zOriginTab = 0;
! 95356: char const *zOriginCol = 0;
! 95357: int j;
! 95358: if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
! 95359:
! 95360: switch( pExpr->op ){
! 95361: case TK_AGG_COLUMN:
! 95362: case TK_COLUMN: {
! 95363: /* The expression is a column. Locate the table the column is being
! 95364: ** extracted from in NameContext.pSrcList. This table may be real
! 95365: ** database table or a subquery.
! 95366: */
! 95367: Table *pTab = 0; /* Table structure column is extracted from */
! 95368: Select *pS = 0; /* Select the column is extracted from */
! 95369: int iCol = pExpr->iColumn; /* Index of column in pTab */
! 95370: testcase( pExpr->op==TK_AGG_COLUMN );
! 95371: testcase( pExpr->op==TK_COLUMN );
! 95372: while( pNC && !pTab ){
! 95373: SrcList *pTabList = pNC->pSrcList;
! 95374: for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
! 95375: if( j<pTabList->nSrc ){
! 95376: pTab = pTabList->a[j].pTab;
! 95377: pS = pTabList->a[j].pSelect;
! 95378: }else{
! 95379: pNC = pNC->pNext;
! 95380: }
! 95381: }
! 95382:
! 95383: if( pTab==0 ){
! 95384: /* At one time, code such as "SELECT new.x" within a trigger would
! 95385: ** cause this condition to run. Since then, we have restructured how
! 95386: ** trigger code is generated and so this condition is no longer
! 95387: ** possible. However, it can still be true for statements like
! 95388: ** the following:
! 95389: **
! 95390: ** CREATE TABLE t1(col INTEGER);
! 95391: ** SELECT (SELECT t1.col) FROM FROM t1;
! 95392: **
! 95393: ** when columnType() is called on the expression "t1.col" in the
! 95394: ** sub-select. In this case, set the column type to NULL, even
! 95395: ** though it should really be "INTEGER".
! 95396: **
! 95397: ** This is not a problem, as the column type of "t1.col" is never
! 95398: ** used. When columnType() is called on the expression
! 95399: ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
! 95400: ** branch below. */
! 95401: break;
! 95402: }
! 95403:
! 95404: assert( pTab && pExpr->pTab==pTab );
! 95405: if( pS ){
! 95406: /* The "table" is actually a sub-select or a view in the FROM clause
! 95407: ** of the SELECT statement. Return the declaration type and origin
! 95408: ** data for the result-set column of the sub-select.
! 95409: */
! 95410: if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
! 95411: /* If iCol is less than zero, then the expression requests the
! 95412: ** rowid of the sub-select or view. This expression is legal (see
! 95413: ** test case misc2.2.2) - it always evaluates to NULL.
! 95414: */
! 95415: NameContext sNC;
! 95416: Expr *p = pS->pEList->a[iCol].pExpr;
! 95417: sNC.pSrcList = pS->pSrc;
! 95418: sNC.pNext = pNC;
! 95419: sNC.pParse = pNC->pParse;
! 95420: zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
! 95421: }
! 95422: }else if( ALWAYS(pTab->pSchema) ){
! 95423: /* A real table */
! 95424: assert( !pS );
! 95425: if( iCol<0 ) iCol = pTab->iPKey;
! 95426: assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
! 95427: if( iCol<0 ){
! 95428: zType = "INTEGER";
! 95429: zOriginCol = "rowid";
! 95430: }else{
! 95431: zType = pTab->aCol[iCol].zType;
! 95432: zOriginCol = pTab->aCol[iCol].zName;
! 95433: }
! 95434: zOriginTab = pTab->zName;
! 95435: if( pNC->pParse ){
! 95436: int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
! 95437: zOriginDb = pNC->pParse->db->aDb[iDb].zName;
! 95438: }
! 95439: }
! 95440: break;
! 95441: }
! 95442: #ifndef SQLITE_OMIT_SUBQUERY
! 95443: case TK_SELECT: {
! 95444: /* The expression is a sub-select. Return the declaration type and
! 95445: ** origin info for the single column in the result set of the SELECT
! 95446: ** statement.
! 95447: */
! 95448: NameContext sNC;
! 95449: Select *pS = pExpr->x.pSelect;
! 95450: Expr *p = pS->pEList->a[0].pExpr;
! 95451: assert( ExprHasProperty(pExpr, EP_xIsSelect) );
! 95452: sNC.pSrcList = pS->pSrc;
! 95453: sNC.pNext = pNC;
! 95454: sNC.pParse = pNC->pParse;
! 95455: zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
! 95456: break;
! 95457: }
! 95458: #endif
! 95459: }
! 95460:
! 95461: if( pzOriginDb ){
! 95462: assert( pzOriginTab && pzOriginCol );
! 95463: *pzOriginDb = zOriginDb;
! 95464: *pzOriginTab = zOriginTab;
! 95465: *pzOriginCol = zOriginCol;
! 95466: }
! 95467: return zType;
! 95468: }
! 95469:
! 95470: /*
! 95471: ** Generate code that will tell the VDBE the declaration types of columns
! 95472: ** in the result set.
! 95473: */
! 95474: static void generateColumnTypes(
! 95475: Parse *pParse, /* Parser context */
! 95476: SrcList *pTabList, /* List of tables */
! 95477: ExprList *pEList /* Expressions defining the result set */
! 95478: ){
! 95479: #ifndef SQLITE_OMIT_DECLTYPE
! 95480: Vdbe *v = pParse->pVdbe;
! 95481: int i;
! 95482: NameContext sNC;
! 95483: sNC.pSrcList = pTabList;
! 95484: sNC.pParse = pParse;
! 95485: for(i=0; i<pEList->nExpr; i++){
! 95486: Expr *p = pEList->a[i].pExpr;
! 95487: const char *zType;
! 95488: #ifdef SQLITE_ENABLE_COLUMN_METADATA
! 95489: const char *zOrigDb = 0;
! 95490: const char *zOrigTab = 0;
! 95491: const char *zOrigCol = 0;
! 95492: zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
! 95493:
! 95494: /* The vdbe must make its own copy of the column-type and other
! 95495: ** column specific strings, in case the schema is reset before this
! 95496: ** virtual machine is deleted.
! 95497: */
! 95498: sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
! 95499: sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
! 95500: sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
! 95501: #else
! 95502: zType = columnType(&sNC, p, 0, 0, 0);
! 95503: #endif
! 95504: sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
! 95505: }
! 95506: #endif /* SQLITE_OMIT_DECLTYPE */
! 95507: }
! 95508:
! 95509: /*
! 95510: ** Generate code that will tell the VDBE the names of columns
! 95511: ** in the result set. This information is used to provide the
! 95512: ** azCol[] values in the callback.
! 95513: */
! 95514: static void generateColumnNames(
! 95515: Parse *pParse, /* Parser context */
! 95516: SrcList *pTabList, /* List of tables */
! 95517: ExprList *pEList /* Expressions defining the result set */
! 95518: ){
! 95519: Vdbe *v = pParse->pVdbe;
! 95520: int i, j;
! 95521: sqlite3 *db = pParse->db;
! 95522: int fullNames, shortNames;
! 95523:
! 95524: #ifndef SQLITE_OMIT_EXPLAIN
! 95525: /* If this is an EXPLAIN, skip this step */
! 95526: if( pParse->explain ){
! 95527: return;
! 95528: }
! 95529: #endif
! 95530:
! 95531: if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
! 95532: pParse->colNamesSet = 1;
! 95533: fullNames = (db->flags & SQLITE_FullColNames)!=0;
! 95534: shortNames = (db->flags & SQLITE_ShortColNames)!=0;
! 95535: sqlite3VdbeSetNumCols(v, pEList->nExpr);
! 95536: for(i=0; i<pEList->nExpr; i++){
! 95537: Expr *p;
! 95538: p = pEList->a[i].pExpr;
! 95539: if( NEVER(p==0) ) continue;
! 95540: if( pEList->a[i].zName ){
! 95541: char *zName = pEList->a[i].zName;
! 95542: sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
! 95543: }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
! 95544: Table *pTab;
! 95545: char *zCol;
! 95546: int iCol = p->iColumn;
! 95547: for(j=0; ALWAYS(j<pTabList->nSrc); j++){
! 95548: if( pTabList->a[j].iCursor==p->iTable ) break;
! 95549: }
! 95550: assert( j<pTabList->nSrc );
! 95551: pTab = pTabList->a[j].pTab;
! 95552: if( iCol<0 ) iCol = pTab->iPKey;
! 95553: assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
! 95554: if( iCol<0 ){
! 95555: zCol = "rowid";
! 95556: }else{
! 95557: zCol = pTab->aCol[iCol].zName;
! 95558: }
! 95559: if( !shortNames && !fullNames ){
! 95560: sqlite3VdbeSetColName(v, i, COLNAME_NAME,
! 95561: sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
! 95562: }else if( fullNames ){
! 95563: char *zName = 0;
! 95564: zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
! 95565: sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
! 95566: }else{
! 95567: sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
! 95568: }
! 95569: }else{
! 95570: sqlite3VdbeSetColName(v, i, COLNAME_NAME,
! 95571: sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
! 95572: }
! 95573: }
! 95574: generateColumnTypes(pParse, pTabList, pEList);
! 95575: }
! 95576:
! 95577: /*
! 95578: ** Given a an expression list (which is really the list of expressions
! 95579: ** that form the result set of a SELECT statement) compute appropriate
! 95580: ** column names for a table that would hold the expression list.
! 95581: **
! 95582: ** All column names will be unique.
! 95583: **
! 95584: ** Only the column names are computed. Column.zType, Column.zColl,
! 95585: ** and other fields of Column are zeroed.
! 95586: **
! 95587: ** Return SQLITE_OK on success. If a memory allocation error occurs,
! 95588: ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
! 95589: */
! 95590: static int selectColumnsFromExprList(
! 95591: Parse *pParse, /* Parsing context */
! 95592: ExprList *pEList, /* Expr list from which to derive column names */
! 95593: int *pnCol, /* Write the number of columns here */
! 95594: Column **paCol /* Write the new column list here */
! 95595: ){
! 95596: sqlite3 *db = pParse->db; /* Database connection */
! 95597: int i, j; /* Loop counters */
! 95598: int cnt; /* Index added to make the name unique */
! 95599: Column *aCol, *pCol; /* For looping over result columns */
! 95600: int nCol; /* Number of columns in the result set */
! 95601: Expr *p; /* Expression for a single result column */
! 95602: char *zName; /* Column name */
! 95603: int nName; /* Size of name in zName[] */
! 95604:
! 95605: *pnCol = nCol = pEList->nExpr;
! 95606: aCol = *paCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
! 95607: if( aCol==0 ) return SQLITE_NOMEM;
! 95608: for(i=0, pCol=aCol; i<nCol; i++, pCol++){
! 95609: /* Get an appropriate name for the column
! 95610: */
! 95611: p = pEList->a[i].pExpr;
! 95612: assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
! 95613: || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
! 95614: if( (zName = pEList->a[i].zName)!=0 ){
! 95615: /* If the column contains an "AS <name>" phrase, use <name> as the name */
! 95616: zName = sqlite3DbStrDup(db, zName);
! 95617: }else{
! 95618: Expr *pColExpr = p; /* The expression that is the result column name */
! 95619: Table *pTab; /* Table associated with this expression */
! 95620: while( pColExpr->op==TK_DOT ){
! 95621: pColExpr = pColExpr->pRight;
! 95622: assert( pColExpr!=0 );
! 95623: }
! 95624: if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
! 95625: /* For columns use the column name name */
! 95626: int iCol = pColExpr->iColumn;
! 95627: pTab = pColExpr->pTab;
! 95628: if( iCol<0 ) iCol = pTab->iPKey;
! 95629: zName = sqlite3MPrintf(db, "%s",
! 95630: iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
! 95631: }else if( pColExpr->op==TK_ID ){
! 95632: assert( !ExprHasProperty(pColExpr, EP_IntValue) );
! 95633: zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
! 95634: }else{
! 95635: /* Use the original text of the column expression as its name */
! 95636: zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
! 95637: }
! 95638: }
! 95639: if( db->mallocFailed ){
! 95640: sqlite3DbFree(db, zName);
! 95641: break;
! 95642: }
! 95643:
! 95644: /* Make sure the column name is unique. If the name is not unique,
! 95645: ** append a integer to the name so that it becomes unique.
! 95646: */
! 95647: nName = sqlite3Strlen30(zName);
! 95648: for(j=cnt=0; j<i; j++){
! 95649: if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
! 95650: char *zNewName;
! 95651: zName[nName] = 0;
! 95652: zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
! 95653: sqlite3DbFree(db, zName);
! 95654: zName = zNewName;
! 95655: j = -1;
! 95656: if( zName==0 ) break;
! 95657: }
! 95658: }
! 95659: pCol->zName = zName;
! 95660: }
! 95661: if( db->mallocFailed ){
! 95662: for(j=0; j<i; j++){
! 95663: sqlite3DbFree(db, aCol[j].zName);
! 95664: }
! 95665: sqlite3DbFree(db, aCol);
! 95666: *paCol = 0;
! 95667: *pnCol = 0;
! 95668: return SQLITE_NOMEM;
! 95669: }
! 95670: return SQLITE_OK;
! 95671: }
! 95672:
! 95673: /*
! 95674: ** Add type and collation information to a column list based on
! 95675: ** a SELECT statement.
! 95676: **
! 95677: ** The column list presumably came from selectColumnNamesFromExprList().
! 95678: ** The column list has only names, not types or collations. This
! 95679: ** routine goes through and adds the types and collations.
! 95680: **
! 95681: ** This routine requires that all identifiers in the SELECT
! 95682: ** statement be resolved.
! 95683: */
! 95684: static void selectAddColumnTypeAndCollation(
! 95685: Parse *pParse, /* Parsing contexts */
! 95686: int nCol, /* Number of columns */
! 95687: Column *aCol, /* List of columns */
! 95688: Select *pSelect /* SELECT used to determine types and collations */
! 95689: ){
! 95690: sqlite3 *db = pParse->db;
! 95691: NameContext sNC;
! 95692: Column *pCol;
! 95693: CollSeq *pColl;
! 95694: int i;
! 95695: Expr *p;
! 95696: struct ExprList_item *a;
! 95697:
! 95698: assert( pSelect!=0 );
! 95699: assert( (pSelect->selFlags & SF_Resolved)!=0 );
! 95700: assert( nCol==pSelect->pEList->nExpr || db->mallocFailed );
! 95701: if( db->mallocFailed ) return;
! 95702: memset(&sNC, 0, sizeof(sNC));
! 95703: sNC.pSrcList = pSelect->pSrc;
! 95704: a = pSelect->pEList->a;
! 95705: for(i=0, pCol=aCol; i<nCol; i++, pCol++){
! 95706: p = a[i].pExpr;
! 95707: pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
! 95708: pCol->affinity = sqlite3ExprAffinity(p);
! 95709: if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
! 95710: pColl = sqlite3ExprCollSeq(pParse, p);
! 95711: if( pColl ){
! 95712: pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
! 95713: }
! 95714: }
! 95715: }
! 95716:
! 95717: /*
! 95718: ** Given a SELECT statement, generate a Table structure that describes
! 95719: ** the result set of that SELECT.
! 95720: */
! 95721: SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
! 95722: Table *pTab;
! 95723: sqlite3 *db = pParse->db;
! 95724: int savedFlags;
! 95725:
! 95726: savedFlags = db->flags;
! 95727: db->flags &= ~SQLITE_FullColNames;
! 95728: db->flags |= SQLITE_ShortColNames;
! 95729: sqlite3SelectPrep(pParse, pSelect, 0);
! 95730: if( pParse->nErr ) return 0;
! 95731: while( pSelect->pPrior ) pSelect = pSelect->pPrior;
! 95732: db->flags = savedFlags;
! 95733: pTab = sqlite3DbMallocZero(db, sizeof(Table) );
! 95734: if( pTab==0 ){
! 95735: return 0;
! 95736: }
! 95737: /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
! 95738: ** is disabled */
! 95739: assert( db->lookaside.bEnabled==0 );
! 95740: pTab->nRef = 1;
! 95741: pTab->zName = 0;
! 95742: pTab->nRowEst = 1000000;
! 95743: selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
! 95744: selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect);
! 95745: pTab->iPKey = -1;
! 95746: if( db->mallocFailed ){
! 95747: sqlite3DeleteTable(db, pTab);
! 95748: return 0;
! 95749: }
! 95750: return pTab;
! 95751: }
! 95752:
! 95753: /*
! 95754: ** Get a VDBE for the given parser context. Create a new one if necessary.
! 95755: ** If an error occurs, return NULL and leave a message in pParse.
! 95756: */
! 95757: SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
! 95758: Vdbe *v = pParse->pVdbe;
! 95759: if( v==0 ){
! 95760: v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
! 95761: #ifndef SQLITE_OMIT_TRACE
! 95762: if( v ){
! 95763: sqlite3VdbeAddOp0(v, OP_Trace);
! 95764: }
! 95765: #endif
! 95766: }
! 95767: return v;
! 95768: }
! 95769:
! 95770:
! 95771: /*
! 95772: ** Compute the iLimit and iOffset fields of the SELECT based on the
! 95773: ** pLimit and pOffset expressions. pLimit and pOffset hold the expressions
! 95774: ** that appear in the original SQL statement after the LIMIT and OFFSET
! 95775: ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
! 95776: ** are the integer memory register numbers for counters used to compute
! 95777: ** the limit and offset. If there is no limit and/or offset, then
! 95778: ** iLimit and iOffset are negative.
! 95779: **
! 95780: ** This routine changes the values of iLimit and iOffset only if
! 95781: ** a limit or offset is defined by pLimit and pOffset. iLimit and
! 95782: ** iOffset should have been preset to appropriate default values
! 95783: ** (usually but not always -1) prior to calling this routine.
! 95784: ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
! 95785: ** redefined. The UNION ALL operator uses this property to force
! 95786: ** the reuse of the same limit and offset registers across multiple
! 95787: ** SELECT statements.
! 95788: */
! 95789: static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
! 95790: Vdbe *v = 0;
! 95791: int iLimit = 0;
! 95792: int iOffset;
! 95793: int addr1, n;
! 95794: if( p->iLimit ) return;
! 95795:
! 95796: /*
! 95797: ** "LIMIT -1" always shows all rows. There is some
! 95798: ** contraversy about what the correct behavior should be.
! 95799: ** The current implementation interprets "LIMIT 0" to mean
! 95800: ** no rows.
! 95801: */
! 95802: sqlite3ExprCacheClear(pParse);
! 95803: assert( p->pOffset==0 || p->pLimit!=0 );
! 95804: if( p->pLimit ){
! 95805: p->iLimit = iLimit = ++pParse->nMem;
! 95806: v = sqlite3GetVdbe(pParse);
! 95807: if( NEVER(v==0) ) return; /* VDBE should have already been allocated */
! 95808: if( sqlite3ExprIsInteger(p->pLimit, &n) ){
! 95809: sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
! 95810: VdbeComment((v, "LIMIT counter"));
! 95811: if( n==0 ){
! 95812: sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
! 95813: }else{
! 95814: if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n;
! 95815: }
! 95816: }else{
! 95817: sqlite3ExprCode(pParse, p->pLimit, iLimit);
! 95818: sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
! 95819: VdbeComment((v, "LIMIT counter"));
! 95820: sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
! 95821: }
! 95822: if( p->pOffset ){
! 95823: p->iOffset = iOffset = ++pParse->nMem;
! 95824: pParse->nMem++; /* Allocate an extra register for limit+offset */
! 95825: sqlite3ExprCode(pParse, p->pOffset, iOffset);
! 95826: sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset);
! 95827: VdbeComment((v, "OFFSET counter"));
! 95828: addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset);
! 95829: sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
! 95830: sqlite3VdbeJumpHere(v, addr1);
! 95831: sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
! 95832: VdbeComment((v, "LIMIT+OFFSET"));
! 95833: addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit);
! 95834: sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
! 95835: sqlite3VdbeJumpHere(v, addr1);
! 95836: }
! 95837: }
! 95838: }
! 95839:
! 95840: #ifndef SQLITE_OMIT_COMPOUND_SELECT
! 95841: /*
! 95842: ** Return the appropriate collating sequence for the iCol-th column of
! 95843: ** the result set for the compound-select statement "p". Return NULL if
! 95844: ** the column has no default collating sequence.
! 95845: **
! 95846: ** The collating sequence for the compound select is taken from the
! 95847: ** left-most term of the select that has a collating sequence.
! 95848: */
! 95849: static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
! 95850: CollSeq *pRet;
! 95851: if( p->pPrior ){
! 95852: pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
! 95853: }else{
! 95854: pRet = 0;
! 95855: }
! 95856: assert( iCol>=0 );
! 95857: if( pRet==0 && iCol<p->pEList->nExpr ){
! 95858: pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
! 95859: }
! 95860: return pRet;
! 95861: }
! 95862: #endif /* SQLITE_OMIT_COMPOUND_SELECT */
! 95863:
! 95864: /* Forward reference */
! 95865: static int multiSelectOrderBy(
! 95866: Parse *pParse, /* Parsing context */
! 95867: Select *p, /* The right-most of SELECTs to be coded */
! 95868: SelectDest *pDest /* What to do with query results */
! 95869: );
! 95870:
! 95871:
! 95872: #ifndef SQLITE_OMIT_COMPOUND_SELECT
! 95873: /*
! 95874: ** This routine is called to process a compound query form from
! 95875: ** two or more separate queries using UNION, UNION ALL, EXCEPT, or
! 95876: ** INTERSECT
! 95877: **
! 95878: ** "p" points to the right-most of the two queries. the query on the
! 95879: ** left is p->pPrior. The left query could also be a compound query
! 95880: ** in which case this routine will be called recursively.
! 95881: **
! 95882: ** The results of the total query are to be written into a destination
! 95883: ** of type eDest with parameter iParm.
! 95884: **
! 95885: ** Example 1: Consider a three-way compound SQL statement.
! 95886: **
! 95887: ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
! 95888: **
! 95889: ** This statement is parsed up as follows:
! 95890: **
! 95891: ** SELECT c FROM t3
! 95892: ** |
! 95893: ** `-----> SELECT b FROM t2
! 95894: ** |
! 95895: ** `------> SELECT a FROM t1
! 95896: **
! 95897: ** The arrows in the diagram above represent the Select.pPrior pointer.
! 95898: ** So if this routine is called with p equal to the t3 query, then
! 95899: ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
! 95900: **
! 95901: ** Notice that because of the way SQLite parses compound SELECTs, the
! 95902: ** individual selects always group from left to right.
! 95903: */
! 95904: static int multiSelect(
! 95905: Parse *pParse, /* Parsing context */
! 95906: Select *p, /* The right-most of SELECTs to be coded */
! 95907: SelectDest *pDest /* What to do with query results */
! 95908: ){
! 95909: int rc = SQLITE_OK; /* Success code from a subroutine */
! 95910: Select *pPrior; /* Another SELECT immediately to our left */
! 95911: Vdbe *v; /* Generate code to this VDBE */
! 95912: SelectDest dest; /* Alternative data destination */
! 95913: Select *pDelete = 0; /* Chain of simple selects to delete */
! 95914: sqlite3 *db; /* Database connection */
! 95915: #ifndef SQLITE_OMIT_EXPLAIN
! 95916: int iSub1; /* EQP id of left-hand query */
! 95917: int iSub2; /* EQP id of right-hand query */
! 95918: #endif
! 95919:
! 95920: /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
! 95921: ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
! 95922: */
! 95923: assert( p && p->pPrior ); /* Calling function guarantees this much */
! 95924: db = pParse->db;
! 95925: pPrior = p->pPrior;
! 95926: assert( pPrior->pRightmost!=pPrior );
! 95927: assert( pPrior->pRightmost==p->pRightmost );
! 95928: dest = *pDest;
! 95929: if( pPrior->pOrderBy ){
! 95930: sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
! 95931: selectOpName(p->op));
! 95932: rc = 1;
! 95933: goto multi_select_end;
! 95934: }
! 95935: if( pPrior->pLimit ){
! 95936: sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
! 95937: selectOpName(p->op));
! 95938: rc = 1;
! 95939: goto multi_select_end;
! 95940: }
! 95941:
! 95942: v = sqlite3GetVdbe(pParse);
! 95943: assert( v!=0 ); /* The VDBE already created by calling function */
! 95944:
! 95945: /* Create the destination temporary table if necessary
! 95946: */
! 95947: if( dest.eDest==SRT_EphemTab ){
! 95948: assert( p->pEList );
! 95949: sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr);
! 95950: sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
! 95951: dest.eDest = SRT_Table;
! 95952: }
! 95953:
! 95954: /* Make sure all SELECTs in the statement have the same number of elements
! 95955: ** in their result sets.
! 95956: */
! 95957: assert( p->pEList && pPrior->pEList );
! 95958: if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
! 95959: sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
! 95960: " do not have the same number of result columns", selectOpName(p->op));
! 95961: rc = 1;
! 95962: goto multi_select_end;
! 95963: }
! 95964:
! 95965: /* Compound SELECTs that have an ORDER BY clause are handled separately.
! 95966: */
! 95967: if( p->pOrderBy ){
! 95968: return multiSelectOrderBy(pParse, p, pDest);
! 95969: }
! 95970:
! 95971: /* Generate code for the left and right SELECT statements.
! 95972: */
! 95973: switch( p->op ){
! 95974: case TK_ALL: {
! 95975: int addr = 0;
! 95976: int nLimit;
! 95977: assert( !pPrior->pLimit );
! 95978: pPrior->pLimit = p->pLimit;
! 95979: pPrior->pOffset = p->pOffset;
! 95980: explainSetInteger(iSub1, pParse->iNextSelectId);
! 95981: rc = sqlite3Select(pParse, pPrior, &dest);
! 95982: p->pLimit = 0;
! 95983: p->pOffset = 0;
! 95984: if( rc ){
! 95985: goto multi_select_end;
! 95986: }
! 95987: p->pPrior = 0;
! 95988: p->iLimit = pPrior->iLimit;
! 95989: p->iOffset = pPrior->iOffset;
! 95990: if( p->iLimit ){
! 95991: addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit);
! 95992: VdbeComment((v, "Jump ahead if LIMIT reached"));
! 95993: }
! 95994: explainSetInteger(iSub2, pParse->iNextSelectId);
! 95995: rc = sqlite3Select(pParse, p, &dest);
! 95996: testcase( rc!=SQLITE_OK );
! 95997: pDelete = p->pPrior;
! 95998: p->pPrior = pPrior;
! 95999: p->nSelectRow += pPrior->nSelectRow;
! 96000: if( pPrior->pLimit
! 96001: && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
! 96002: && p->nSelectRow > (double)nLimit
! 96003: ){
! 96004: p->nSelectRow = (double)nLimit;
! 96005: }
! 96006: if( addr ){
! 96007: sqlite3VdbeJumpHere(v, addr);
! 96008: }
! 96009: break;
! 96010: }
! 96011: case TK_EXCEPT:
! 96012: case TK_UNION: {
! 96013: int unionTab; /* Cursor number of the temporary table holding result */
! 96014: u8 op = 0; /* One of the SRT_ operations to apply to self */
! 96015: int priorOp; /* The SRT_ operation to apply to prior selects */
! 96016: Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
! 96017: int addr;
! 96018: SelectDest uniondest;
! 96019:
! 96020: testcase( p->op==TK_EXCEPT );
! 96021: testcase( p->op==TK_UNION );
! 96022: priorOp = SRT_Union;
! 96023: if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
! 96024: /* We can reuse a temporary table generated by a SELECT to our
! 96025: ** right.
! 96026: */
! 96027: assert( p->pRightmost!=p ); /* Can only happen for leftward elements
! 96028: ** of a 3-way or more compound */
! 96029: assert( p->pLimit==0 ); /* Not allowed on leftward elements */
! 96030: assert( p->pOffset==0 ); /* Not allowed on leftward elements */
! 96031: unionTab = dest.iParm;
! 96032: }else{
! 96033: /* We will need to create our own temporary table to hold the
! 96034: ** intermediate results.
! 96035: */
! 96036: unionTab = pParse->nTab++;
! 96037: assert( p->pOrderBy==0 );
! 96038: addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
! 96039: assert( p->addrOpenEphm[0] == -1 );
! 96040: p->addrOpenEphm[0] = addr;
! 96041: p->pRightmost->selFlags |= SF_UsesEphemeral;
! 96042: assert( p->pEList );
! 96043: }
! 96044:
! 96045: /* Code the SELECT statements to our left
! 96046: */
! 96047: assert( !pPrior->pOrderBy );
! 96048: sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
! 96049: explainSetInteger(iSub1, pParse->iNextSelectId);
! 96050: rc = sqlite3Select(pParse, pPrior, &uniondest);
! 96051: if( rc ){
! 96052: goto multi_select_end;
! 96053: }
! 96054:
! 96055: /* Code the current SELECT statement
! 96056: */
! 96057: if( p->op==TK_EXCEPT ){
! 96058: op = SRT_Except;
! 96059: }else{
! 96060: assert( p->op==TK_UNION );
! 96061: op = SRT_Union;
! 96062: }
! 96063: p->pPrior = 0;
! 96064: pLimit = p->pLimit;
! 96065: p->pLimit = 0;
! 96066: pOffset = p->pOffset;
! 96067: p->pOffset = 0;
! 96068: uniondest.eDest = op;
! 96069: explainSetInteger(iSub2, pParse->iNextSelectId);
! 96070: rc = sqlite3Select(pParse, p, &uniondest);
! 96071: testcase( rc!=SQLITE_OK );
! 96072: /* Query flattening in sqlite3Select() might refill p->pOrderBy.
! 96073: ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
! 96074: sqlite3ExprListDelete(db, p->pOrderBy);
! 96075: pDelete = p->pPrior;
! 96076: p->pPrior = pPrior;
! 96077: p->pOrderBy = 0;
! 96078: if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
! 96079: sqlite3ExprDelete(db, p->pLimit);
! 96080: p->pLimit = pLimit;
! 96081: p->pOffset = pOffset;
! 96082: p->iLimit = 0;
! 96083: p->iOffset = 0;
! 96084:
! 96085: /* Convert the data in the temporary table into whatever form
! 96086: ** it is that we currently need.
! 96087: */
! 96088: assert( unionTab==dest.iParm || dest.eDest!=priorOp );
! 96089: if( dest.eDest!=priorOp ){
! 96090: int iCont, iBreak, iStart;
! 96091: assert( p->pEList );
! 96092: if( dest.eDest==SRT_Output ){
! 96093: Select *pFirst = p;
! 96094: while( pFirst->pPrior ) pFirst = pFirst->pPrior;
! 96095: generateColumnNames(pParse, 0, pFirst->pEList);
! 96096: }
! 96097: iBreak = sqlite3VdbeMakeLabel(v);
! 96098: iCont = sqlite3VdbeMakeLabel(v);
! 96099: computeLimitRegisters(pParse, p, iBreak);
! 96100: sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
! 96101: iStart = sqlite3VdbeCurrentAddr(v);
! 96102: selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
! 96103: 0, -1, &dest, iCont, iBreak);
! 96104: sqlite3VdbeResolveLabel(v, iCont);
! 96105: sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
! 96106: sqlite3VdbeResolveLabel(v, iBreak);
! 96107: sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
! 96108: }
! 96109: break;
! 96110: }
! 96111: default: assert( p->op==TK_INTERSECT ); {
! 96112: int tab1, tab2;
! 96113: int iCont, iBreak, iStart;
! 96114: Expr *pLimit, *pOffset;
! 96115: int addr;
! 96116: SelectDest intersectdest;
! 96117: int r1;
! 96118:
! 96119: /* INTERSECT is different from the others since it requires
! 96120: ** two temporary tables. Hence it has its own case. Begin
! 96121: ** by allocating the tables we will need.
! 96122: */
! 96123: tab1 = pParse->nTab++;
! 96124: tab2 = pParse->nTab++;
! 96125: assert( p->pOrderBy==0 );
! 96126:
! 96127: addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
! 96128: assert( p->addrOpenEphm[0] == -1 );
! 96129: p->addrOpenEphm[0] = addr;
! 96130: p->pRightmost->selFlags |= SF_UsesEphemeral;
! 96131: assert( p->pEList );
! 96132:
! 96133: /* Code the SELECTs to our left into temporary table "tab1".
! 96134: */
! 96135: sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
! 96136: explainSetInteger(iSub1, pParse->iNextSelectId);
! 96137: rc = sqlite3Select(pParse, pPrior, &intersectdest);
! 96138: if( rc ){
! 96139: goto multi_select_end;
! 96140: }
! 96141:
! 96142: /* Code the current SELECT into temporary table "tab2"
! 96143: */
! 96144: addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
! 96145: assert( p->addrOpenEphm[1] == -1 );
! 96146: p->addrOpenEphm[1] = addr;
! 96147: p->pPrior = 0;
! 96148: pLimit = p->pLimit;
! 96149: p->pLimit = 0;
! 96150: pOffset = p->pOffset;
! 96151: p->pOffset = 0;
! 96152: intersectdest.iParm = tab2;
! 96153: explainSetInteger(iSub2, pParse->iNextSelectId);
! 96154: rc = sqlite3Select(pParse, p, &intersectdest);
! 96155: testcase( rc!=SQLITE_OK );
! 96156: pDelete = p->pPrior;
! 96157: p->pPrior = pPrior;
! 96158: if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
! 96159: sqlite3ExprDelete(db, p->pLimit);
! 96160: p->pLimit = pLimit;
! 96161: p->pOffset = pOffset;
! 96162:
! 96163: /* Generate code to take the intersection of the two temporary
! 96164: ** tables.
! 96165: */
! 96166: assert( p->pEList );
! 96167: if( dest.eDest==SRT_Output ){
! 96168: Select *pFirst = p;
! 96169: while( pFirst->pPrior ) pFirst = pFirst->pPrior;
! 96170: generateColumnNames(pParse, 0, pFirst->pEList);
! 96171: }
! 96172: iBreak = sqlite3VdbeMakeLabel(v);
! 96173: iCont = sqlite3VdbeMakeLabel(v);
! 96174: computeLimitRegisters(pParse, p, iBreak);
! 96175: sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
! 96176: r1 = sqlite3GetTempReg(pParse);
! 96177: iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
! 96178: sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
! 96179: sqlite3ReleaseTempReg(pParse, r1);
! 96180: selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
! 96181: 0, -1, &dest, iCont, iBreak);
! 96182: sqlite3VdbeResolveLabel(v, iCont);
! 96183: sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
! 96184: sqlite3VdbeResolveLabel(v, iBreak);
! 96185: sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
! 96186: sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
! 96187: break;
! 96188: }
! 96189: }
! 96190:
! 96191: explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
! 96192:
! 96193: /* Compute collating sequences used by
! 96194: ** temporary tables needed to implement the compound select.
! 96195: ** Attach the KeyInfo structure to all temporary tables.
! 96196: **
! 96197: ** This section is run by the right-most SELECT statement only.
! 96198: ** SELECT statements to the left always skip this part. The right-most
! 96199: ** SELECT might also skip this part if it has no ORDER BY clause and
! 96200: ** no temp tables are required.
! 96201: */
! 96202: if( p->selFlags & SF_UsesEphemeral ){
! 96203: int i; /* Loop counter */
! 96204: KeyInfo *pKeyInfo; /* Collating sequence for the result set */
! 96205: Select *pLoop; /* For looping through SELECT statements */
! 96206: CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
! 96207: int nCol; /* Number of columns in result set */
! 96208:
! 96209: assert( p->pRightmost==p );
! 96210: nCol = p->pEList->nExpr;
! 96211: pKeyInfo = sqlite3DbMallocZero(db,
! 96212: sizeof(*pKeyInfo)+nCol*(sizeof(CollSeq*) + 1));
! 96213: if( !pKeyInfo ){
! 96214: rc = SQLITE_NOMEM;
! 96215: goto multi_select_end;
! 96216: }
! 96217:
! 96218: pKeyInfo->enc = ENC(db);
! 96219: pKeyInfo->nField = (u16)nCol;
! 96220:
! 96221: for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
! 96222: *apColl = multiSelectCollSeq(pParse, p, i);
! 96223: if( 0==*apColl ){
! 96224: *apColl = db->pDfltColl;
! 96225: }
! 96226: }
! 96227:
! 96228: for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
! 96229: for(i=0; i<2; i++){
! 96230: int addr = pLoop->addrOpenEphm[i];
! 96231: if( addr<0 ){
! 96232: /* If [0] is unused then [1] is also unused. So we can
! 96233: ** always safely abort as soon as the first unused slot is found */
! 96234: assert( pLoop->addrOpenEphm[1]<0 );
! 96235: break;
! 96236: }
! 96237: sqlite3VdbeChangeP2(v, addr, nCol);
! 96238: sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO);
! 96239: pLoop->addrOpenEphm[i] = -1;
! 96240: }
! 96241: }
! 96242: sqlite3DbFree(db, pKeyInfo);
! 96243: }
! 96244:
! 96245: multi_select_end:
! 96246: pDest->iMem = dest.iMem;
! 96247: pDest->nMem = dest.nMem;
! 96248: sqlite3SelectDelete(db, pDelete);
! 96249: return rc;
! 96250: }
! 96251: #endif /* SQLITE_OMIT_COMPOUND_SELECT */
! 96252:
! 96253: /*
! 96254: ** Code an output subroutine for a coroutine implementation of a
! 96255: ** SELECT statment.
! 96256: **
! 96257: ** The data to be output is contained in pIn->iMem. There are
! 96258: ** pIn->nMem columns to be output. pDest is where the output should
! 96259: ** be sent.
! 96260: **
! 96261: ** regReturn is the number of the register holding the subroutine
! 96262: ** return address.
! 96263: **
! 96264: ** If regPrev>0 then it is the first register in a vector that
! 96265: ** records the previous output. mem[regPrev] is a flag that is false
! 96266: ** if there has been no previous output. If regPrev>0 then code is
! 96267: ** generated to suppress duplicates. pKeyInfo is used for comparing
! 96268: ** keys.
! 96269: **
! 96270: ** If the LIMIT found in p->iLimit is reached, jump immediately to
! 96271: ** iBreak.
! 96272: */
! 96273: static int generateOutputSubroutine(
! 96274: Parse *pParse, /* Parsing context */
! 96275: Select *p, /* The SELECT statement */
! 96276: SelectDest *pIn, /* Coroutine supplying data */
! 96277: SelectDest *pDest, /* Where to send the data */
! 96278: int regReturn, /* The return address register */
! 96279: int regPrev, /* Previous result register. No uniqueness if 0 */
! 96280: KeyInfo *pKeyInfo, /* For comparing with previous entry */
! 96281: int p4type, /* The p4 type for pKeyInfo */
! 96282: int iBreak /* Jump here if we hit the LIMIT */
! 96283: ){
! 96284: Vdbe *v = pParse->pVdbe;
! 96285: int iContinue;
! 96286: int addr;
! 96287:
! 96288: addr = sqlite3VdbeCurrentAddr(v);
! 96289: iContinue = sqlite3VdbeMakeLabel(v);
! 96290:
! 96291: /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
! 96292: */
! 96293: if( regPrev ){
! 96294: int j1, j2;
! 96295: j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
! 96296: j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iMem, regPrev+1, pIn->nMem,
! 96297: (char*)pKeyInfo, p4type);
! 96298: sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
! 96299: sqlite3VdbeJumpHere(v, j1);
! 96300: sqlite3ExprCodeCopy(pParse, pIn->iMem, regPrev+1, pIn->nMem);
! 96301: sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
! 96302: }
! 96303: if( pParse->db->mallocFailed ) return 0;
! 96304:
! 96305: /* Suppress the the first OFFSET entries if there is an OFFSET clause
! 96306: */
! 96307: codeOffset(v, p, iContinue);
! 96308:
! 96309: switch( pDest->eDest ){
! 96310: /* Store the result as data using a unique key.
! 96311: */
! 96312: case SRT_Table:
! 96313: case SRT_EphemTab: {
! 96314: int r1 = sqlite3GetTempReg(pParse);
! 96315: int r2 = sqlite3GetTempReg(pParse);
! 96316: testcase( pDest->eDest==SRT_Table );
! 96317: testcase( pDest->eDest==SRT_EphemTab );
! 96318: sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iMem, pIn->nMem, r1);
! 96319: sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iParm, r2);
! 96320: sqlite3VdbeAddOp3(v, OP_Insert, pDest->iParm, r1, r2);
! 96321: sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
! 96322: sqlite3ReleaseTempReg(pParse, r2);
! 96323: sqlite3ReleaseTempReg(pParse, r1);
! 96324: break;
! 96325: }
! 96326:
! 96327: #ifndef SQLITE_OMIT_SUBQUERY
! 96328: /* If we are creating a set for an "expr IN (SELECT ...)" construct,
! 96329: ** then there should be a single item on the stack. Write this
! 96330: ** item into the set table with bogus data.
! 96331: */
! 96332: case SRT_Set: {
! 96333: int r1;
! 96334: assert( pIn->nMem==1 );
! 96335: p->affinity =
! 96336: sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affinity);
! 96337: r1 = sqlite3GetTempReg(pParse);
! 96338: sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iMem, 1, r1, &p->affinity, 1);
! 96339: sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, 1);
! 96340: sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iParm, r1);
! 96341: sqlite3ReleaseTempReg(pParse, r1);
! 96342: break;
! 96343: }
! 96344:
! 96345: #if 0 /* Never occurs on an ORDER BY query */
! 96346: /* If any row exist in the result set, record that fact and abort.
! 96347: */
! 96348: case SRT_Exists: {
! 96349: sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iParm);
! 96350: /* The LIMIT clause will terminate the loop for us */
! 96351: break;
! 96352: }
! 96353: #endif
! 96354:
! 96355: /* If this is a scalar select that is part of an expression, then
! 96356: ** store the results in the appropriate memory cell and break out
! 96357: ** of the scan loop.
! 96358: */
! 96359: case SRT_Mem: {
! 96360: assert( pIn->nMem==1 );
! 96361: sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iParm, 1);
! 96362: /* The LIMIT clause will jump out of the loop for us */
! 96363: break;
! 96364: }
! 96365: #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
! 96366:
! 96367: /* The results are stored in a sequence of registers
! 96368: ** starting at pDest->iMem. Then the co-routine yields.
! 96369: */
! 96370: case SRT_Coroutine: {
! 96371: if( pDest->iMem==0 ){
! 96372: pDest->iMem = sqlite3GetTempRange(pParse, pIn->nMem);
! 96373: pDest->nMem = pIn->nMem;
! 96374: }
! 96375: sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iMem, pDest->nMem);
! 96376: sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
! 96377: break;
! 96378: }
! 96379:
! 96380: /* If none of the above, then the result destination must be
! 96381: ** SRT_Output. This routine is never called with any other
! 96382: ** destination other than the ones handled above or SRT_Output.
! 96383: **
! 96384: ** For SRT_Output, results are stored in a sequence of registers.
! 96385: ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
! 96386: ** return the next row of result.
! 96387: */
! 96388: default: {
! 96389: assert( pDest->eDest==SRT_Output );
! 96390: sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iMem, pIn->nMem);
! 96391: sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, pIn->nMem);
! 96392: break;
! 96393: }
! 96394: }
! 96395:
! 96396: /* Jump to the end of the loop if the LIMIT is reached.
! 96397: */
! 96398: if( p->iLimit ){
! 96399: sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
! 96400: }
! 96401:
! 96402: /* Generate the subroutine return
! 96403: */
! 96404: sqlite3VdbeResolveLabel(v, iContinue);
! 96405: sqlite3VdbeAddOp1(v, OP_Return, regReturn);
! 96406:
! 96407: return addr;
! 96408: }
! 96409:
! 96410: /*
! 96411: ** Alternative compound select code generator for cases when there
! 96412: ** is an ORDER BY clause.
! 96413: **
! 96414: ** We assume a query of the following form:
! 96415: **
! 96416: ** <selectA> <operator> <selectB> ORDER BY <orderbylist>
! 96417: **
! 96418: ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
! 96419: ** is to code both <selectA> and <selectB> with the ORDER BY clause as
! 96420: ** co-routines. Then run the co-routines in parallel and merge the results
! 96421: ** into the output. In addition to the two coroutines (called selectA and
! 96422: ** selectB) there are 7 subroutines:
! 96423: **
! 96424: ** outA: Move the output of the selectA coroutine into the output
! 96425: ** of the compound query.
! 96426: **
! 96427: ** outB: Move the output of the selectB coroutine into the output
! 96428: ** of the compound query. (Only generated for UNION and
! 96429: ** UNION ALL. EXCEPT and INSERTSECT never output a row that
! 96430: ** appears only in B.)
! 96431: **
! 96432: ** AltB: Called when there is data from both coroutines and A<B.
! 96433: **
! 96434: ** AeqB: Called when there is data from both coroutines and A==B.
! 96435: **
! 96436: ** AgtB: Called when there is data from both coroutines and A>B.
! 96437: **
! 96438: ** EofA: Called when data is exhausted from selectA.
! 96439: **
! 96440: ** EofB: Called when data is exhausted from selectB.
! 96441: **
! 96442: ** The implementation of the latter five subroutines depend on which
! 96443: ** <operator> is used:
! 96444: **
! 96445: **
! 96446: ** UNION ALL UNION EXCEPT INTERSECT
! 96447: ** ------------- ----------------- -------------- -----------------
! 96448: ** AltB: outA, nextA outA, nextA outA, nextA nextA
! 96449: **
! 96450: ** AeqB: outA, nextA nextA nextA outA, nextA
! 96451: **
! 96452: ** AgtB: outB, nextB outB, nextB nextB nextB
! 96453: **
! 96454: ** EofA: outB, nextB outB, nextB halt halt
! 96455: **
! 96456: ** EofB: outA, nextA outA, nextA outA, nextA halt
! 96457: **
! 96458: ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
! 96459: ** causes an immediate jump to EofA and an EOF on B following nextB causes
! 96460: ** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
! 96461: ** following nextX causes a jump to the end of the select processing.
! 96462: **
! 96463: ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
! 96464: ** within the output subroutine. The regPrev register set holds the previously
! 96465: ** output value. A comparison is made against this value and the output
! 96466: ** is skipped if the next results would be the same as the previous.
! 96467: **
! 96468: ** The implementation plan is to implement the two coroutines and seven
! 96469: ** subroutines first, then put the control logic at the bottom. Like this:
! 96470: **
! 96471: ** goto Init
! 96472: ** coA: coroutine for left query (A)
! 96473: ** coB: coroutine for right query (B)
! 96474: ** outA: output one row of A
! 96475: ** outB: output one row of B (UNION and UNION ALL only)
! 96476: ** EofA: ...
! 96477: ** EofB: ...
! 96478: ** AltB: ...
! 96479: ** AeqB: ...
! 96480: ** AgtB: ...
! 96481: ** Init: initialize coroutine registers
! 96482: ** yield coA
! 96483: ** if eof(A) goto EofA
! 96484: ** yield coB
! 96485: ** if eof(B) goto EofB
! 96486: ** Cmpr: Compare A, B
! 96487: ** Jump AltB, AeqB, AgtB
! 96488: ** End: ...
! 96489: **
! 96490: ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
! 96491: ** actually called using Gosub and they do not Return. EofA and EofB loop
! 96492: ** until all data is exhausted then jump to the "end" labe. AltB, AeqB,
! 96493: ** and AgtB jump to either L2 or to one of EofA or EofB.
! 96494: */
! 96495: #ifndef SQLITE_OMIT_COMPOUND_SELECT
! 96496: static int multiSelectOrderBy(
! 96497: Parse *pParse, /* Parsing context */
! 96498: Select *p, /* The right-most of SELECTs to be coded */
! 96499: SelectDest *pDest /* What to do with query results */
! 96500: ){
! 96501: int i, j; /* Loop counters */
! 96502: Select *pPrior; /* Another SELECT immediately to our left */
! 96503: Vdbe *v; /* Generate code to this VDBE */
! 96504: SelectDest destA; /* Destination for coroutine A */
! 96505: SelectDest destB; /* Destination for coroutine B */
! 96506: int regAddrA; /* Address register for select-A coroutine */
! 96507: int regEofA; /* Flag to indicate when select-A is complete */
! 96508: int regAddrB; /* Address register for select-B coroutine */
! 96509: int regEofB; /* Flag to indicate when select-B is complete */
! 96510: int addrSelectA; /* Address of the select-A coroutine */
! 96511: int addrSelectB; /* Address of the select-B coroutine */
! 96512: int regOutA; /* Address register for the output-A subroutine */
! 96513: int regOutB; /* Address register for the output-B subroutine */
! 96514: int addrOutA; /* Address of the output-A subroutine */
! 96515: int addrOutB = 0; /* Address of the output-B subroutine */
! 96516: int addrEofA; /* Address of the select-A-exhausted subroutine */
! 96517: int addrEofB; /* Address of the select-B-exhausted subroutine */
! 96518: int addrAltB; /* Address of the A<B subroutine */
! 96519: int addrAeqB; /* Address of the A==B subroutine */
! 96520: int addrAgtB; /* Address of the A>B subroutine */
! 96521: int regLimitA; /* Limit register for select-A */
! 96522: int regLimitB; /* Limit register for select-A */
! 96523: int regPrev; /* A range of registers to hold previous output */
! 96524: int savedLimit; /* Saved value of p->iLimit */
! 96525: int savedOffset; /* Saved value of p->iOffset */
! 96526: int labelCmpr; /* Label for the start of the merge algorithm */
! 96527: int labelEnd; /* Label for the end of the overall SELECT stmt */
! 96528: int j1; /* Jump instructions that get retargetted */
! 96529: int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
! 96530: KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
! 96531: KeyInfo *pKeyMerge; /* Comparison information for merging rows */
! 96532: sqlite3 *db; /* Database connection */
! 96533: ExprList *pOrderBy; /* The ORDER BY clause */
! 96534: int nOrderBy; /* Number of terms in the ORDER BY clause */
! 96535: int *aPermute; /* Mapping from ORDER BY terms to result set columns */
! 96536: #ifndef SQLITE_OMIT_EXPLAIN
! 96537: int iSub1; /* EQP id of left-hand query */
! 96538: int iSub2; /* EQP id of right-hand query */
! 96539: #endif
! 96540:
! 96541: assert( p->pOrderBy!=0 );
! 96542: assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
! 96543: db = pParse->db;
! 96544: v = pParse->pVdbe;
! 96545: assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */
! 96546: labelEnd = sqlite3VdbeMakeLabel(v);
! 96547: labelCmpr = sqlite3VdbeMakeLabel(v);
! 96548:
! 96549:
! 96550: /* Patch up the ORDER BY clause
! 96551: */
! 96552: op = p->op;
! 96553: pPrior = p->pPrior;
! 96554: assert( pPrior->pOrderBy==0 );
! 96555: pOrderBy = p->pOrderBy;
! 96556: assert( pOrderBy );
! 96557: nOrderBy = pOrderBy->nExpr;
! 96558:
! 96559: /* For operators other than UNION ALL we have to make sure that
! 96560: ** the ORDER BY clause covers every term of the result set. Add
! 96561: ** terms to the ORDER BY clause as necessary.
! 96562: */
! 96563: if( op!=TK_ALL ){
! 96564: for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
! 96565: struct ExprList_item *pItem;
! 96566: for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
! 96567: assert( pItem->iOrderByCol>0 );
! 96568: if( pItem->iOrderByCol==i ) break;
! 96569: }
! 96570: if( j==nOrderBy ){
! 96571: Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
! 96572: if( pNew==0 ) return SQLITE_NOMEM;
! 96573: pNew->flags |= EP_IntValue;
! 96574: pNew->u.iValue = i;
! 96575: pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
! 96576: pOrderBy->a[nOrderBy++].iOrderByCol = (u16)i;
! 96577: }
! 96578: }
! 96579: }
! 96580:
! 96581: /* Compute the comparison permutation and keyinfo that is used with
! 96582: ** the permutation used to determine if the next
! 96583: ** row of results comes from selectA or selectB. Also add explicit
! 96584: ** collations to the ORDER BY clause terms so that when the subqueries
! 96585: ** to the right and the left are evaluated, they use the correct
! 96586: ** collation.
! 96587: */
! 96588: aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
! 96589: if( aPermute ){
! 96590: struct ExprList_item *pItem;
! 96591: for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
! 96592: assert( pItem->iOrderByCol>0 && pItem->iOrderByCol<=p->pEList->nExpr );
! 96593: aPermute[i] = pItem->iOrderByCol - 1;
! 96594: }
! 96595: pKeyMerge =
! 96596: sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1));
! 96597: if( pKeyMerge ){
! 96598: pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
! 96599: pKeyMerge->nField = (u16)nOrderBy;
! 96600: pKeyMerge->enc = ENC(db);
! 96601: for(i=0; i<nOrderBy; i++){
! 96602: CollSeq *pColl;
! 96603: Expr *pTerm = pOrderBy->a[i].pExpr;
! 96604: if( pTerm->flags & EP_ExpCollate ){
! 96605: pColl = pTerm->pColl;
! 96606: }else{
! 96607: pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
! 96608: pTerm->flags |= EP_ExpCollate;
! 96609: pTerm->pColl = pColl;
! 96610: }
! 96611: pKeyMerge->aColl[i] = pColl;
! 96612: pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
! 96613: }
! 96614: }
! 96615: }else{
! 96616: pKeyMerge = 0;
! 96617: }
! 96618:
! 96619: /* Reattach the ORDER BY clause to the query.
! 96620: */
! 96621: p->pOrderBy = pOrderBy;
! 96622: pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
! 96623:
! 96624: /* Allocate a range of temporary registers and the KeyInfo needed
! 96625: ** for the logic that removes duplicate result rows when the
! 96626: ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
! 96627: */
! 96628: if( op==TK_ALL ){
! 96629: regPrev = 0;
! 96630: }else{
! 96631: int nExpr = p->pEList->nExpr;
! 96632: assert( nOrderBy>=nExpr || db->mallocFailed );
! 96633: regPrev = sqlite3GetTempRange(pParse, nExpr+1);
! 96634: sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
! 96635: pKeyDup = sqlite3DbMallocZero(db,
! 96636: sizeof(*pKeyDup) + nExpr*(sizeof(CollSeq*)+1) );
! 96637: if( pKeyDup ){
! 96638: pKeyDup->aSortOrder = (u8*)&pKeyDup->aColl[nExpr];
! 96639: pKeyDup->nField = (u16)nExpr;
! 96640: pKeyDup->enc = ENC(db);
! 96641: for(i=0; i<nExpr; i++){
! 96642: pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
! 96643: pKeyDup->aSortOrder[i] = 0;
! 96644: }
! 96645: }
! 96646: }
! 96647:
! 96648: /* Separate the left and the right query from one another
! 96649: */
! 96650: p->pPrior = 0;
! 96651: sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
! 96652: if( pPrior->pPrior==0 ){
! 96653: sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
! 96654: }
! 96655:
! 96656: /* Compute the limit registers */
! 96657: computeLimitRegisters(pParse, p, labelEnd);
! 96658: if( p->iLimit && op==TK_ALL ){
! 96659: regLimitA = ++pParse->nMem;
! 96660: regLimitB = ++pParse->nMem;
! 96661: sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
! 96662: regLimitA);
! 96663: sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
! 96664: }else{
! 96665: regLimitA = regLimitB = 0;
! 96666: }
! 96667: sqlite3ExprDelete(db, p->pLimit);
! 96668: p->pLimit = 0;
! 96669: sqlite3ExprDelete(db, p->pOffset);
! 96670: p->pOffset = 0;
! 96671:
! 96672: regAddrA = ++pParse->nMem;
! 96673: regEofA = ++pParse->nMem;
! 96674: regAddrB = ++pParse->nMem;
! 96675: regEofB = ++pParse->nMem;
! 96676: regOutA = ++pParse->nMem;
! 96677: regOutB = ++pParse->nMem;
! 96678: sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
! 96679: sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
! 96680:
! 96681: /* Jump past the various subroutines and coroutines to the main
! 96682: ** merge loop
! 96683: */
! 96684: j1 = sqlite3VdbeAddOp0(v, OP_Goto);
! 96685: addrSelectA = sqlite3VdbeCurrentAddr(v);
! 96686:
! 96687:
! 96688: /* Generate a coroutine to evaluate the SELECT statement to the
! 96689: ** left of the compound operator - the "A" select.
! 96690: */
! 96691: VdbeNoopComment((v, "Begin coroutine for left SELECT"));
! 96692: pPrior->iLimit = regLimitA;
! 96693: explainSetInteger(iSub1, pParse->iNextSelectId);
! 96694: sqlite3Select(pParse, pPrior, &destA);
! 96695: sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
! 96696: sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
! 96697: VdbeNoopComment((v, "End coroutine for left SELECT"));
! 96698:
! 96699: /* Generate a coroutine to evaluate the SELECT statement on
! 96700: ** the right - the "B" select
! 96701: */
! 96702: addrSelectB = sqlite3VdbeCurrentAddr(v);
! 96703: VdbeNoopComment((v, "Begin coroutine for right SELECT"));
! 96704: savedLimit = p->iLimit;
! 96705: savedOffset = p->iOffset;
! 96706: p->iLimit = regLimitB;
! 96707: p->iOffset = 0;
! 96708: explainSetInteger(iSub2, pParse->iNextSelectId);
! 96709: sqlite3Select(pParse, p, &destB);
! 96710: p->iLimit = savedLimit;
! 96711: p->iOffset = savedOffset;
! 96712: sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
! 96713: sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
! 96714: VdbeNoopComment((v, "End coroutine for right SELECT"));
! 96715:
! 96716: /* Generate a subroutine that outputs the current row of the A
! 96717: ** select as the next output row of the compound select.
! 96718: */
! 96719: VdbeNoopComment((v, "Output routine for A"));
! 96720: addrOutA = generateOutputSubroutine(pParse,
! 96721: p, &destA, pDest, regOutA,
! 96722: regPrev, pKeyDup, P4_KEYINFO_HANDOFF, labelEnd);
! 96723:
! 96724: /* Generate a subroutine that outputs the current row of the B
! 96725: ** select as the next output row of the compound select.
! 96726: */
! 96727: if( op==TK_ALL || op==TK_UNION ){
! 96728: VdbeNoopComment((v, "Output routine for B"));
! 96729: addrOutB = generateOutputSubroutine(pParse,
! 96730: p, &destB, pDest, regOutB,
! 96731: regPrev, pKeyDup, P4_KEYINFO_STATIC, labelEnd);
! 96732: }
! 96733:
! 96734: /* Generate a subroutine to run when the results from select A
! 96735: ** are exhausted and only data in select B remains.
! 96736: */
! 96737: VdbeNoopComment((v, "eof-A subroutine"));
! 96738: if( op==TK_EXCEPT || op==TK_INTERSECT ){
! 96739: addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
! 96740: }else{
! 96741: addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
! 96742: sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
! 96743: sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
! 96744: sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
! 96745: p->nSelectRow += pPrior->nSelectRow;
! 96746: }
! 96747:
! 96748: /* Generate a subroutine to run when the results from select B
! 96749: ** are exhausted and only data in select A remains.
! 96750: */
! 96751: if( op==TK_INTERSECT ){
! 96752: addrEofB = addrEofA;
! 96753: if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
! 96754: }else{
! 96755: VdbeNoopComment((v, "eof-B subroutine"));
! 96756: addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
! 96757: sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
! 96758: sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
! 96759: sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
! 96760: }
! 96761:
! 96762: /* Generate code to handle the case of A<B
! 96763: */
! 96764: VdbeNoopComment((v, "A-lt-B subroutine"));
! 96765: addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
! 96766: sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
! 96767: sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
! 96768: sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
! 96769:
! 96770: /* Generate code to handle the case of A==B
! 96771: */
! 96772: if( op==TK_ALL ){
! 96773: addrAeqB = addrAltB;
! 96774: }else if( op==TK_INTERSECT ){
! 96775: addrAeqB = addrAltB;
! 96776: addrAltB++;
! 96777: }else{
! 96778: VdbeNoopComment((v, "A-eq-B subroutine"));
! 96779: addrAeqB =
! 96780: sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
! 96781: sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
! 96782: sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
! 96783: }
! 96784:
! 96785: /* Generate code to handle the case of A>B
! 96786: */
! 96787: VdbeNoopComment((v, "A-gt-B subroutine"));
! 96788: addrAgtB = sqlite3VdbeCurrentAddr(v);
! 96789: if( op==TK_ALL || op==TK_UNION ){
! 96790: sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
! 96791: }
! 96792: sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
! 96793: sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
! 96794: sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
! 96795:
! 96796: /* This code runs once to initialize everything.
! 96797: */
! 96798: sqlite3VdbeJumpHere(v, j1);
! 96799: sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
! 96800: sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
! 96801: sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
! 96802: sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
! 96803: sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
! 96804: sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
! 96805:
! 96806: /* Implement the main merge loop
! 96807: */
! 96808: sqlite3VdbeResolveLabel(v, labelCmpr);
! 96809: sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
! 96810: sqlite3VdbeAddOp4(v, OP_Compare, destA.iMem, destB.iMem, nOrderBy,
! 96811: (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
! 96812: sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
! 96813:
! 96814: /* Release temporary registers
! 96815: */
! 96816: if( regPrev ){
! 96817: sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
! 96818: }
! 96819:
! 96820: /* Jump to the this point in order to terminate the query.
! 96821: */
! 96822: sqlite3VdbeResolveLabel(v, labelEnd);
! 96823:
! 96824: /* Set the number of output columns
! 96825: */
! 96826: if( pDest->eDest==SRT_Output ){
! 96827: Select *pFirst = pPrior;
! 96828: while( pFirst->pPrior ) pFirst = pFirst->pPrior;
! 96829: generateColumnNames(pParse, 0, pFirst->pEList);
! 96830: }
! 96831:
! 96832: /* Reassembly the compound query so that it will be freed correctly
! 96833: ** by the calling function */
! 96834: if( p->pPrior ){
! 96835: sqlite3SelectDelete(db, p->pPrior);
! 96836: }
! 96837: p->pPrior = pPrior;
! 96838:
! 96839: /*** TBD: Insert subroutine calls to close cursors on incomplete
! 96840: **** subqueries ****/
! 96841: explainComposite(pParse, p->op, iSub1, iSub2, 0);
! 96842: return SQLITE_OK;
! 96843: }
! 96844: #endif
! 96845:
! 96846: #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
! 96847: /* Forward Declarations */
! 96848: static void substExprList(sqlite3*, ExprList*, int, ExprList*);
! 96849: static void substSelect(sqlite3*, Select *, int, ExprList *);
! 96850:
! 96851: /*
! 96852: ** Scan through the expression pExpr. Replace every reference to
! 96853: ** a column in table number iTable with a copy of the iColumn-th
! 96854: ** entry in pEList. (But leave references to the ROWID column
! 96855: ** unchanged.)
! 96856: **
! 96857: ** This routine is part of the flattening procedure. A subquery
! 96858: ** whose result set is defined by pEList appears as entry in the
! 96859: ** FROM clause of a SELECT such that the VDBE cursor assigned to that
! 96860: ** FORM clause entry is iTable. This routine make the necessary
! 96861: ** changes to pExpr so that it refers directly to the source table
! 96862: ** of the subquery rather the result set of the subquery.
! 96863: */
! 96864: static Expr *substExpr(
! 96865: sqlite3 *db, /* Report malloc errors to this connection */
! 96866: Expr *pExpr, /* Expr in which substitution occurs */
! 96867: int iTable, /* Table to be substituted */
! 96868: ExprList *pEList /* Substitute expressions */
! 96869: ){
! 96870: if( pExpr==0 ) return 0;
! 96871: if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
! 96872: if( pExpr->iColumn<0 ){
! 96873: pExpr->op = TK_NULL;
! 96874: }else{
! 96875: Expr *pNew;
! 96876: assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
! 96877: assert( pExpr->pLeft==0 && pExpr->pRight==0 );
! 96878: pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
! 96879: if( pNew && pExpr->pColl ){
! 96880: pNew->pColl = pExpr->pColl;
! 96881: }
! 96882: sqlite3ExprDelete(db, pExpr);
! 96883: pExpr = pNew;
! 96884: }
! 96885: }else{
! 96886: pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
! 96887: pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
! 96888: if( ExprHasProperty(pExpr, EP_xIsSelect) ){
! 96889: substSelect(db, pExpr->x.pSelect, iTable, pEList);
! 96890: }else{
! 96891: substExprList(db, pExpr->x.pList, iTable, pEList);
! 96892: }
! 96893: }
! 96894: return pExpr;
! 96895: }
! 96896: static void substExprList(
! 96897: sqlite3 *db, /* Report malloc errors here */
! 96898: ExprList *pList, /* List to scan and in which to make substitutes */
! 96899: int iTable, /* Table to be substituted */
! 96900: ExprList *pEList /* Substitute values */
! 96901: ){
! 96902: int i;
! 96903: if( pList==0 ) return;
! 96904: for(i=0; i<pList->nExpr; i++){
! 96905: pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
! 96906: }
! 96907: }
! 96908: static void substSelect(
! 96909: sqlite3 *db, /* Report malloc errors here */
! 96910: Select *p, /* SELECT statement in which to make substitutions */
! 96911: int iTable, /* Table to be replaced */
! 96912: ExprList *pEList /* Substitute values */
! 96913: ){
! 96914: SrcList *pSrc;
! 96915: struct SrcList_item *pItem;
! 96916: int i;
! 96917: if( !p ) return;
! 96918: substExprList(db, p->pEList, iTable, pEList);
! 96919: substExprList(db, p->pGroupBy, iTable, pEList);
! 96920: substExprList(db, p->pOrderBy, iTable, pEList);
! 96921: p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
! 96922: p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
! 96923: substSelect(db, p->pPrior, iTable, pEList);
! 96924: pSrc = p->pSrc;
! 96925: assert( pSrc ); /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
! 96926: if( ALWAYS(pSrc) ){
! 96927: for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
! 96928: substSelect(db, pItem->pSelect, iTable, pEList);
! 96929: }
! 96930: }
! 96931: }
! 96932: #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
! 96933:
! 96934: #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
! 96935: /*
! 96936: ** This routine attempts to flatten subqueries as a performance optimization.
! 96937: ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
! 96938: **
! 96939: ** To understand the concept of flattening, consider the following
! 96940: ** query:
! 96941: **
! 96942: ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
! 96943: **
! 96944: ** The default way of implementing this query is to execute the
! 96945: ** subquery first and store the results in a temporary table, then
! 96946: ** run the outer query on that temporary table. This requires two
! 96947: ** passes over the data. Furthermore, because the temporary table
! 96948: ** has no indices, the WHERE clause on the outer query cannot be
! 96949: ** optimized.
! 96950: **
! 96951: ** This routine attempts to rewrite queries such as the above into
! 96952: ** a single flat select, like this:
! 96953: **
! 96954: ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
! 96955: **
! 96956: ** The code generated for this simpification gives the same result
! 96957: ** but only has to scan the data once. And because indices might
! 96958: ** exist on the table t1, a complete scan of the data might be
! 96959: ** avoided.
! 96960: **
! 96961: ** Flattening is only attempted if all of the following are true:
! 96962: **
! 96963: ** (1) The subquery and the outer query do not both use aggregates.
! 96964: **
! 96965: ** (2) The subquery is not an aggregate or the outer query is not a join.
! 96966: **
! 96967: ** (3) The subquery is not the right operand of a left outer join
! 96968: ** (Originally ticket #306. Strengthened by ticket #3300)
! 96969: **
! 96970: ** (4) The subquery is not DISTINCT.
! 96971: **
! 96972: ** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
! 96973: ** sub-queries that were excluded from this optimization. Restriction
! 96974: ** (4) has since been expanded to exclude all DISTINCT subqueries.
! 96975: **
! 96976: ** (6) The subquery does not use aggregates or the outer query is not
! 96977: ** DISTINCT.
! 96978: **
! 96979: ** (7) The subquery has a FROM clause. TODO: For subqueries without
! 96980: ** A FROM clause, consider adding a FROM close with the special
! 96981: ** table sqlite_once that consists of a single row containing a
! 96982: ** single NULL.
! 96983: **
! 96984: ** (8) The subquery does not use LIMIT or the outer query is not a join.
! 96985: **
! 96986: ** (9) The subquery does not use LIMIT or the outer query does not use
! 96987: ** aggregates.
! 96988: **
! 96989: ** (10) The subquery does not use aggregates or the outer query does not
! 96990: ** use LIMIT.
! 96991: **
! 96992: ** (11) The subquery and the outer query do not both have ORDER BY clauses.
! 96993: **
! 96994: ** (**) Not implemented. Subsumed into restriction (3). Was previously
! 96995: ** a separate restriction deriving from ticket #350.
! 96996: **
! 96997: ** (13) The subquery and outer query do not both use LIMIT.
! 96998: **
! 96999: ** (14) The subquery does not use OFFSET.
! 97000: **
! 97001: ** (15) The outer query is not part of a compound select or the
! 97002: ** subquery does not have a LIMIT clause.
! 97003: ** (See ticket #2339 and ticket [02a8e81d44]).
! 97004: **
! 97005: ** (16) The outer query is not an aggregate or the subquery does
! 97006: ** not contain ORDER BY. (Ticket #2942) This used to not matter
! 97007: ** until we introduced the group_concat() function.
! 97008: **
! 97009: ** (17) The sub-query is not a compound select, or it is a UNION ALL
! 97010: ** compound clause made up entirely of non-aggregate queries, and
! 97011: ** the parent query:
! 97012: **
! 97013: ** * is not itself part of a compound select,
! 97014: ** * is not an aggregate or DISTINCT query, and
! 97015: ** * is not a join
! 97016: **
! 97017: ** The parent and sub-query may contain WHERE clauses. Subject to
! 97018: ** rules (11), (13) and (14), they may also contain ORDER BY,
! 97019: ** LIMIT and OFFSET clauses. The subquery cannot use any compound
! 97020: ** operator other than UNION ALL because all the other compound
! 97021: ** operators have an implied DISTINCT which is disallowed by
! 97022: ** restriction (4).
! 97023: **
! 97024: ** (18) If the sub-query is a compound select, then all terms of the
! 97025: ** ORDER by clause of the parent must be simple references to
! 97026: ** columns of the sub-query.
! 97027: **
! 97028: ** (19) The subquery does not use LIMIT or the outer query does not
! 97029: ** have a WHERE clause.
! 97030: **
! 97031: ** (20) If the sub-query is a compound select, then it must not use
! 97032: ** an ORDER BY clause. Ticket #3773. We could relax this constraint
! 97033: ** somewhat by saying that the terms of the ORDER BY clause must
! 97034: ** appear as unmodified result columns in the outer query. But we
! 97035: ** have other optimizations in mind to deal with that case.
! 97036: **
! 97037: ** (21) The subquery does not use LIMIT or the outer query is not
! 97038: ** DISTINCT. (See ticket [752e1646fc]).
! 97039: **
! 97040: ** In this routine, the "p" parameter is a pointer to the outer query.
! 97041: ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
! 97042: ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
! 97043: **
! 97044: ** If flattening is not attempted, this routine is a no-op and returns 0.
! 97045: ** If flattening is attempted this routine returns 1.
! 97046: **
! 97047: ** All of the expression analysis must occur on both the outer query and
! 97048: ** the subquery before this routine runs.
! 97049: */
! 97050: static int flattenSubquery(
! 97051: Parse *pParse, /* Parsing context */
! 97052: Select *p, /* The parent or outer SELECT statement */
! 97053: int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
! 97054: int isAgg, /* True if outer SELECT uses aggregate functions */
! 97055: int subqueryIsAgg /* True if the subquery uses aggregate functions */
! 97056: ){
! 97057: const char *zSavedAuthContext = pParse->zAuthContext;
! 97058: Select *pParent;
! 97059: Select *pSub; /* The inner query or "subquery" */
! 97060: Select *pSub1; /* Pointer to the rightmost select in sub-query */
! 97061: SrcList *pSrc; /* The FROM clause of the outer query */
! 97062: SrcList *pSubSrc; /* The FROM clause of the subquery */
! 97063: ExprList *pList; /* The result set of the outer query */
! 97064: int iParent; /* VDBE cursor number of the pSub result set temp table */
! 97065: int i; /* Loop counter */
! 97066: Expr *pWhere; /* The WHERE clause */
! 97067: struct SrcList_item *pSubitem; /* The subquery */
! 97068: sqlite3 *db = pParse->db;
! 97069:
! 97070: /* Check to see if flattening is permitted. Return 0 if not.
! 97071: */
! 97072: assert( p!=0 );
! 97073: assert( p->pPrior==0 ); /* Unable to flatten compound queries */
! 97074: if( db->flags & SQLITE_QueryFlattener ) return 0;
! 97075: pSrc = p->pSrc;
! 97076: assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
! 97077: pSubitem = &pSrc->a[iFrom];
! 97078: iParent = pSubitem->iCursor;
! 97079: pSub = pSubitem->pSelect;
! 97080: assert( pSub!=0 );
! 97081: if( isAgg && subqueryIsAgg ) return 0; /* Restriction (1) */
! 97082: if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; /* Restriction (2) */
! 97083: pSubSrc = pSub->pSrc;
! 97084: assert( pSubSrc );
! 97085: /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
! 97086: ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
! 97087: ** because they could be computed at compile-time. But when LIMIT and OFFSET
! 97088: ** became arbitrary expressions, we were forced to add restrictions (13)
! 97089: ** and (14). */
! 97090: if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
! 97091: if( pSub->pOffset ) return 0; /* Restriction (14) */
! 97092: if( p->pRightmost && pSub->pLimit ){
! 97093: return 0; /* Restriction (15) */
! 97094: }
! 97095: if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
! 97096: if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (5) */
! 97097: if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
! 97098: return 0; /* Restrictions (8)(9) */
! 97099: }
! 97100: if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
! 97101: return 0; /* Restriction (6) */
! 97102: }
! 97103: if( p->pOrderBy && pSub->pOrderBy ){
! 97104: return 0; /* Restriction (11) */
! 97105: }
! 97106: if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
! 97107: if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
! 97108: if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
! 97109: return 0; /* Restriction (21) */
! 97110: }
! 97111:
! 97112: /* OBSOLETE COMMENT 1:
! 97113: ** Restriction 3: If the subquery is a join, make sure the subquery is
! 97114: ** not used as the right operand of an outer join. Examples of why this
! 97115: ** is not allowed:
! 97116: **
! 97117: ** t1 LEFT OUTER JOIN (t2 JOIN t3)
! 97118: **
! 97119: ** If we flatten the above, we would get
! 97120: **
! 97121: ** (t1 LEFT OUTER JOIN t2) JOIN t3
! 97122: **
! 97123: ** which is not at all the same thing.
! 97124: **
! 97125: ** OBSOLETE COMMENT 2:
! 97126: ** Restriction 12: If the subquery is the right operand of a left outer
! 97127: ** join, make sure the subquery has no WHERE clause.
! 97128: ** An examples of why this is not allowed:
! 97129: **
! 97130: ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
! 97131: **
! 97132: ** If we flatten the above, we would get
! 97133: **
! 97134: ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
! 97135: **
! 97136: ** But the t2.x>0 test will always fail on a NULL row of t2, which
! 97137: ** effectively converts the OUTER JOIN into an INNER JOIN.
! 97138: **
! 97139: ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
! 97140: ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
! 97141: ** is fraught with danger. Best to avoid the whole thing. If the
! 97142: ** subquery is the right term of a LEFT JOIN, then do not flatten.
! 97143: */
! 97144: if( (pSubitem->jointype & JT_OUTER)!=0 ){
! 97145: return 0;
! 97146: }
! 97147:
! 97148: /* Restriction 17: If the sub-query is a compound SELECT, then it must
! 97149: ** use only the UNION ALL operator. And none of the simple select queries
! 97150: ** that make up the compound SELECT are allowed to be aggregate or distinct
! 97151: ** queries.
! 97152: */
! 97153: if( pSub->pPrior ){
! 97154: if( pSub->pOrderBy ){
! 97155: return 0; /* Restriction 20 */
! 97156: }
! 97157: if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
! 97158: return 0;
! 97159: }
! 97160: for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
! 97161: testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
! 97162: testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
! 97163: assert( pSub->pSrc!=0 );
! 97164: if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
! 97165: || (pSub1->pPrior && pSub1->op!=TK_ALL)
! 97166: || pSub1->pSrc->nSrc<1
! 97167: ){
! 97168: return 0;
! 97169: }
! 97170: testcase( pSub1->pSrc->nSrc>1 );
! 97171: }
! 97172:
! 97173: /* Restriction 18. */
! 97174: if( p->pOrderBy ){
! 97175: int ii;
! 97176: for(ii=0; ii<p->pOrderBy->nExpr; ii++){
! 97177: if( p->pOrderBy->a[ii].iOrderByCol==0 ) return 0;
! 97178: }
! 97179: }
! 97180: }
! 97181:
! 97182: /***** If we reach this point, flattening is permitted. *****/
! 97183:
! 97184: /* Authorize the subquery */
! 97185: pParse->zAuthContext = pSubitem->zName;
! 97186: sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
! 97187: pParse->zAuthContext = zSavedAuthContext;
! 97188:
! 97189: /* If the sub-query is a compound SELECT statement, then (by restrictions
! 97190: ** 17 and 18 above) it must be a UNION ALL and the parent query must
! 97191: ** be of the form:
! 97192: **
! 97193: ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
! 97194: **
! 97195: ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
! 97196: ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
! 97197: ** OFFSET clauses and joins them to the left-hand-side of the original
! 97198: ** using UNION ALL operators. In this case N is the number of simple
! 97199: ** select statements in the compound sub-query.
! 97200: **
! 97201: ** Example:
! 97202: **
! 97203: ** SELECT a+1 FROM (
! 97204: ** SELECT x FROM tab
! 97205: ** UNION ALL
! 97206: ** SELECT y FROM tab
! 97207: ** UNION ALL
! 97208: ** SELECT abs(z*2) FROM tab2
! 97209: ** ) WHERE a!=5 ORDER BY 1
! 97210: **
! 97211: ** Transformed into:
! 97212: **
! 97213: ** SELECT x+1 FROM tab WHERE x+1!=5
! 97214: ** UNION ALL
! 97215: ** SELECT y+1 FROM tab WHERE y+1!=5
! 97216: ** UNION ALL
! 97217: ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
! 97218: ** ORDER BY 1
! 97219: **
! 97220: ** We call this the "compound-subquery flattening".
! 97221: */
! 97222: for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
! 97223: Select *pNew;
! 97224: ExprList *pOrderBy = p->pOrderBy;
! 97225: Expr *pLimit = p->pLimit;
! 97226: Select *pPrior = p->pPrior;
! 97227: p->pOrderBy = 0;
! 97228: p->pSrc = 0;
! 97229: p->pPrior = 0;
! 97230: p->pLimit = 0;
! 97231: pNew = sqlite3SelectDup(db, p, 0);
! 97232: p->pLimit = pLimit;
! 97233: p->pOrderBy = pOrderBy;
! 97234: p->pSrc = pSrc;
! 97235: p->op = TK_ALL;
! 97236: p->pRightmost = 0;
! 97237: if( pNew==0 ){
! 97238: pNew = pPrior;
! 97239: }else{
! 97240: pNew->pPrior = pPrior;
! 97241: pNew->pRightmost = 0;
! 97242: }
! 97243: p->pPrior = pNew;
! 97244: if( db->mallocFailed ) return 1;
! 97245: }
! 97246:
! 97247: /* Begin flattening the iFrom-th entry of the FROM clause
! 97248: ** in the outer query.
! 97249: */
! 97250: pSub = pSub1 = pSubitem->pSelect;
! 97251:
! 97252: /* Delete the transient table structure associated with the
! 97253: ** subquery
! 97254: */
! 97255: sqlite3DbFree(db, pSubitem->zDatabase);
! 97256: sqlite3DbFree(db, pSubitem->zName);
! 97257: sqlite3DbFree(db, pSubitem->zAlias);
! 97258: pSubitem->zDatabase = 0;
! 97259: pSubitem->zName = 0;
! 97260: pSubitem->zAlias = 0;
! 97261: pSubitem->pSelect = 0;
! 97262:
! 97263: /* Defer deleting the Table object associated with the
! 97264: ** subquery until code generation is
! 97265: ** complete, since there may still exist Expr.pTab entries that
! 97266: ** refer to the subquery even after flattening. Ticket #3346.
! 97267: **
! 97268: ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
! 97269: */
! 97270: if( ALWAYS(pSubitem->pTab!=0) ){
! 97271: Table *pTabToDel = pSubitem->pTab;
! 97272: if( pTabToDel->nRef==1 ){
! 97273: Parse *pToplevel = sqlite3ParseToplevel(pParse);
! 97274: pTabToDel->pNextZombie = pToplevel->pZombieTab;
! 97275: pToplevel->pZombieTab = pTabToDel;
! 97276: }else{
! 97277: pTabToDel->nRef--;
! 97278: }
! 97279: pSubitem->pTab = 0;
! 97280: }
! 97281:
! 97282: /* The following loop runs once for each term in a compound-subquery
! 97283: ** flattening (as described above). If we are doing a different kind
! 97284: ** of flattening - a flattening other than a compound-subquery flattening -
! 97285: ** then this loop only runs once.
! 97286: **
! 97287: ** This loop moves all of the FROM elements of the subquery into the
! 97288: ** the FROM clause of the outer query. Before doing this, remember
! 97289: ** the cursor number for the original outer query FROM element in
! 97290: ** iParent. The iParent cursor will never be used. Subsequent code
! 97291: ** will scan expressions looking for iParent references and replace
! 97292: ** those references with expressions that resolve to the subquery FROM
! 97293: ** elements we are now copying in.
! 97294: */
! 97295: for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
! 97296: int nSubSrc;
! 97297: u8 jointype = 0;
! 97298: pSubSrc = pSub->pSrc; /* FROM clause of subquery */
! 97299: nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
! 97300: pSrc = pParent->pSrc; /* FROM clause of the outer query */
! 97301:
! 97302: if( pSrc ){
! 97303: assert( pParent==p ); /* First time through the loop */
! 97304: jointype = pSubitem->jointype;
! 97305: }else{
! 97306: assert( pParent!=p ); /* 2nd and subsequent times through the loop */
! 97307: pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
! 97308: if( pSrc==0 ){
! 97309: assert( db->mallocFailed );
! 97310: break;
! 97311: }
! 97312: }
! 97313:
! 97314: /* The subquery uses a single slot of the FROM clause of the outer
! 97315: ** query. If the subquery has more than one element in its FROM clause,
! 97316: ** then expand the outer query to make space for it to hold all elements
! 97317: ** of the subquery.
! 97318: **
! 97319: ** Example:
! 97320: **
! 97321: ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
! 97322: **
! 97323: ** The outer query has 3 slots in its FROM clause. One slot of the
! 97324: ** outer query (the middle slot) is used by the subquery. The next
! 97325: ** block of code will expand the out query to 4 slots. The middle
! 97326: ** slot is expanded to two slots in order to make space for the
! 97327: ** two elements in the FROM clause of the subquery.
! 97328: */
! 97329: if( nSubSrc>1 ){
! 97330: pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
! 97331: if( db->mallocFailed ){
! 97332: break;
! 97333: }
! 97334: }
! 97335:
! 97336: /* Transfer the FROM clause terms from the subquery into the
! 97337: ** outer query.
! 97338: */
! 97339: for(i=0; i<nSubSrc; i++){
! 97340: sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
! 97341: pSrc->a[i+iFrom] = pSubSrc->a[i];
! 97342: memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
! 97343: }
! 97344: pSrc->a[iFrom].jointype = jointype;
! 97345:
! 97346: /* Now begin substituting subquery result set expressions for
! 97347: ** references to the iParent in the outer query.
! 97348: **
! 97349: ** Example:
! 97350: **
! 97351: ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
! 97352: ** \ \_____________ subquery __________/ /
! 97353: ** \_____________________ outer query ______________________________/
! 97354: **
! 97355: ** We look at every expression in the outer query and every place we see
! 97356: ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
! 97357: */
! 97358: pList = pParent->pEList;
! 97359: for(i=0; i<pList->nExpr; i++){
! 97360: if( pList->a[i].zName==0 ){
! 97361: const char *zSpan = pList->a[i].zSpan;
! 97362: if( ALWAYS(zSpan) ){
! 97363: pList->a[i].zName = sqlite3DbStrDup(db, zSpan);
! 97364: }
! 97365: }
! 97366: }
! 97367: substExprList(db, pParent->pEList, iParent, pSub->pEList);
! 97368: if( isAgg ){
! 97369: substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
! 97370: pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
! 97371: }
! 97372: if( pSub->pOrderBy ){
! 97373: assert( pParent->pOrderBy==0 );
! 97374: pParent->pOrderBy = pSub->pOrderBy;
! 97375: pSub->pOrderBy = 0;
! 97376: }else if( pParent->pOrderBy ){
! 97377: substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
! 97378: }
! 97379: if( pSub->pWhere ){
! 97380: pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
! 97381: }else{
! 97382: pWhere = 0;
! 97383: }
! 97384: if( subqueryIsAgg ){
! 97385: assert( pParent->pHaving==0 );
! 97386: pParent->pHaving = pParent->pWhere;
! 97387: pParent->pWhere = pWhere;
! 97388: pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
! 97389: pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving,
! 97390: sqlite3ExprDup(db, pSub->pHaving, 0));
! 97391: assert( pParent->pGroupBy==0 );
! 97392: pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
! 97393: }else{
! 97394: pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
! 97395: pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
! 97396: }
! 97397:
! 97398: /* The flattened query is distinct if either the inner or the
! 97399: ** outer query is distinct.
! 97400: */
! 97401: pParent->selFlags |= pSub->selFlags & SF_Distinct;
! 97402:
! 97403: /*
! 97404: ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
! 97405: **
! 97406: ** One is tempted to try to add a and b to combine the limits. But this
! 97407: ** does not work if either limit is negative.
! 97408: */
! 97409: if( pSub->pLimit ){
! 97410: pParent->pLimit = pSub->pLimit;
! 97411: pSub->pLimit = 0;
! 97412: }
! 97413: }
! 97414:
! 97415: /* Finially, delete what is left of the subquery and return
! 97416: ** success.
! 97417: */
! 97418: sqlite3SelectDelete(db, pSub1);
! 97419:
! 97420: return 1;
! 97421: }
! 97422: #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
! 97423:
! 97424: /*
! 97425: ** Analyze the SELECT statement passed as an argument to see if it
! 97426: ** is a min() or max() query. Return WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX if
! 97427: ** it is, or 0 otherwise. At present, a query is considered to be
! 97428: ** a min()/max() query if:
! 97429: **
! 97430: ** 1. There is a single object in the FROM clause.
! 97431: **
! 97432: ** 2. There is a single expression in the result set, and it is
! 97433: ** either min(x) or max(x), where x is a column reference.
! 97434: */
! 97435: static u8 minMaxQuery(Select *p){
! 97436: Expr *pExpr;
! 97437: ExprList *pEList = p->pEList;
! 97438:
! 97439: if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
! 97440: pExpr = pEList->a[0].pExpr;
! 97441: if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
! 97442: if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
! 97443: pEList = pExpr->x.pList;
! 97444: if( pEList==0 || pEList->nExpr!=1 ) return 0;
! 97445: if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
! 97446: assert( !ExprHasProperty(pExpr, EP_IntValue) );
! 97447: if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){
! 97448: return WHERE_ORDERBY_MIN;
! 97449: }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){
! 97450: return WHERE_ORDERBY_MAX;
! 97451: }
! 97452: return WHERE_ORDERBY_NORMAL;
! 97453: }
! 97454:
! 97455: /*
! 97456: ** The select statement passed as the first argument is an aggregate query.
! 97457: ** The second argment is the associated aggregate-info object. This
! 97458: ** function tests if the SELECT is of the form:
! 97459: **
! 97460: ** SELECT count(*) FROM <tbl>
! 97461: **
! 97462: ** where table is a database table, not a sub-select or view. If the query
! 97463: ** does match this pattern, then a pointer to the Table object representing
! 97464: ** <tbl> is returned. Otherwise, 0 is returned.
! 97465: */
! 97466: static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
! 97467: Table *pTab;
! 97468: Expr *pExpr;
! 97469:
! 97470: assert( !p->pGroupBy );
! 97471:
! 97472: if( p->pWhere || p->pEList->nExpr!=1
! 97473: || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
! 97474: ){
! 97475: return 0;
! 97476: }
! 97477: pTab = p->pSrc->a[0].pTab;
! 97478: pExpr = p->pEList->a[0].pExpr;
! 97479: assert( pTab && !pTab->pSelect && pExpr );
! 97480:
! 97481: if( IsVirtual(pTab) ) return 0;
! 97482: if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
! 97483: if( (pAggInfo->aFunc[0].pFunc->flags&SQLITE_FUNC_COUNT)==0 ) return 0;
! 97484: if( pExpr->flags&EP_Distinct ) return 0;
! 97485:
! 97486: return pTab;
! 97487: }
! 97488:
! 97489: /*
! 97490: ** If the source-list item passed as an argument was augmented with an
! 97491: ** INDEXED BY clause, then try to locate the specified index. If there
! 97492: ** was such a clause and the named index cannot be found, return
! 97493: ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
! 97494: ** pFrom->pIndex and return SQLITE_OK.
! 97495: */
! 97496: SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
! 97497: if( pFrom->pTab && pFrom->zIndex ){
! 97498: Table *pTab = pFrom->pTab;
! 97499: char *zIndex = pFrom->zIndex;
! 97500: Index *pIdx;
! 97501: for(pIdx=pTab->pIndex;
! 97502: pIdx && sqlite3StrICmp(pIdx->zName, zIndex);
! 97503: pIdx=pIdx->pNext
! 97504: );
! 97505: if( !pIdx ){
! 97506: sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
! 97507: pParse->checkSchema = 1;
! 97508: return SQLITE_ERROR;
! 97509: }
! 97510: pFrom->pIndex = pIdx;
! 97511: }
! 97512: return SQLITE_OK;
! 97513: }
! 97514:
! 97515: /*
! 97516: ** This routine is a Walker callback for "expanding" a SELECT statement.
! 97517: ** "Expanding" means to do the following:
! 97518: **
! 97519: ** (1) Make sure VDBE cursor numbers have been assigned to every
! 97520: ** element of the FROM clause.
! 97521: **
! 97522: ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
! 97523: ** defines FROM clause. When views appear in the FROM clause,
! 97524: ** fill pTabList->a[].pSelect with a copy of the SELECT statement
! 97525: ** that implements the view. A copy is made of the view's SELECT
! 97526: ** statement so that we can freely modify or delete that statement
! 97527: ** without worrying about messing up the presistent representation
! 97528: ** of the view.
! 97529: **
! 97530: ** (3) Add terms to the WHERE clause to accomodate the NATURAL keyword
! 97531: ** on joins and the ON and USING clause of joins.
! 97532: **
! 97533: ** (4) Scan the list of columns in the result set (pEList) looking
! 97534: ** for instances of the "*" operator or the TABLE.* operator.
! 97535: ** If found, expand each "*" to be every column in every table
! 97536: ** and TABLE.* to be every column in TABLE.
! 97537: **
! 97538: */
! 97539: static int selectExpander(Walker *pWalker, Select *p){
! 97540: Parse *pParse = pWalker->pParse;
! 97541: int i, j, k;
! 97542: SrcList *pTabList;
! 97543: ExprList *pEList;
! 97544: struct SrcList_item *pFrom;
! 97545: sqlite3 *db = pParse->db;
! 97546:
! 97547: if( db->mallocFailed ){
! 97548: return WRC_Abort;
! 97549: }
! 97550: if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){
! 97551: return WRC_Prune;
! 97552: }
! 97553: p->selFlags |= SF_Expanded;
! 97554: pTabList = p->pSrc;
! 97555: pEList = p->pEList;
! 97556:
! 97557: /* Make sure cursor numbers have been assigned to all entries in
! 97558: ** the FROM clause of the SELECT statement.
! 97559: */
! 97560: sqlite3SrcListAssignCursors(pParse, pTabList);
! 97561:
! 97562: /* Look up every table named in the FROM clause of the select. If
! 97563: ** an entry of the FROM clause is a subquery instead of a table or view,
! 97564: ** then create a transient table structure to describe the subquery.
! 97565: */
! 97566: for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
! 97567: Table *pTab;
! 97568: if( pFrom->pTab!=0 ){
! 97569: /* This statement has already been prepared. There is no need
! 97570: ** to go further. */
! 97571: assert( i==0 );
! 97572: return WRC_Prune;
! 97573: }
! 97574: if( pFrom->zName==0 ){
! 97575: #ifndef SQLITE_OMIT_SUBQUERY
! 97576: Select *pSel = pFrom->pSelect;
! 97577: /* A sub-query in the FROM clause of a SELECT */
! 97578: assert( pSel!=0 );
! 97579: assert( pFrom->pTab==0 );
! 97580: sqlite3WalkSelect(pWalker, pSel);
! 97581: pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
! 97582: if( pTab==0 ) return WRC_Abort;
! 97583: pTab->nRef = 1;
! 97584: pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab);
! 97585: while( pSel->pPrior ){ pSel = pSel->pPrior; }
! 97586: selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
! 97587: pTab->iPKey = -1;
! 97588: pTab->nRowEst = 1000000;
! 97589: pTab->tabFlags |= TF_Ephemeral;
! 97590: #endif
! 97591: }else{
! 97592: /* An ordinary table or view name in the FROM clause */
! 97593: assert( pFrom->pTab==0 );
! 97594: pFrom->pTab = pTab =
! 97595: sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase);
! 97596: if( pTab==0 ) return WRC_Abort;
! 97597: pTab->nRef++;
! 97598: #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
! 97599: if( pTab->pSelect || IsVirtual(pTab) ){
! 97600: /* We reach here if the named table is a really a view */
! 97601: if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
! 97602: assert( pFrom->pSelect==0 );
! 97603: pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
! 97604: sqlite3WalkSelect(pWalker, pFrom->pSelect);
! 97605: }
! 97606: #endif
! 97607: }
! 97608:
! 97609: /* Locate the index named by the INDEXED BY clause, if any. */
! 97610: if( sqlite3IndexedByLookup(pParse, pFrom) ){
! 97611: return WRC_Abort;
! 97612: }
! 97613: }
! 97614:
! 97615: /* Process NATURAL keywords, and ON and USING clauses of joins.
! 97616: */
! 97617: if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
! 97618: return WRC_Abort;
! 97619: }
! 97620:
! 97621: /* For every "*" that occurs in the column list, insert the names of
! 97622: ** all columns in all tables. And for every TABLE.* insert the names
! 97623: ** of all columns in TABLE. The parser inserted a special expression
! 97624: ** with the TK_ALL operator for each "*" that it found in the column list.
! 97625: ** The following code just has to locate the TK_ALL expressions and expand
! 97626: ** each one to the list of all columns in all tables.
! 97627: **
! 97628: ** The first loop just checks to see if there are any "*" operators
! 97629: ** that need expanding.
! 97630: */
! 97631: for(k=0; k<pEList->nExpr; k++){
! 97632: Expr *pE = pEList->a[k].pExpr;
! 97633: if( pE->op==TK_ALL ) break;
! 97634: assert( pE->op!=TK_DOT || pE->pRight!=0 );
! 97635: assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
! 97636: if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
! 97637: }
! 97638: if( k<pEList->nExpr ){
! 97639: /*
! 97640: ** If we get here it means the result set contains one or more "*"
! 97641: ** operators that need to be expanded. Loop through each expression
! 97642: ** in the result set and expand them one by one.
! 97643: */
! 97644: struct ExprList_item *a = pEList->a;
! 97645: ExprList *pNew = 0;
! 97646: int flags = pParse->db->flags;
! 97647: int longNames = (flags & SQLITE_FullColNames)!=0
! 97648: && (flags & SQLITE_ShortColNames)==0;
! 97649:
! 97650: for(k=0; k<pEList->nExpr; k++){
! 97651: Expr *pE = a[k].pExpr;
! 97652: assert( pE->op!=TK_DOT || pE->pRight!=0 );
! 97653: if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
! 97654: /* This particular expression does not need to be expanded.
! 97655: */
! 97656: pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
! 97657: if( pNew ){
! 97658: pNew->a[pNew->nExpr-1].zName = a[k].zName;
! 97659: pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
! 97660: a[k].zName = 0;
! 97661: a[k].zSpan = 0;
! 97662: }
! 97663: a[k].pExpr = 0;
! 97664: }else{
! 97665: /* This expression is a "*" or a "TABLE.*" and needs to be
! 97666: ** expanded. */
! 97667: int tableSeen = 0; /* Set to 1 when TABLE matches */
! 97668: char *zTName; /* text of name of TABLE */
! 97669: if( pE->op==TK_DOT ){
! 97670: assert( pE->pLeft!=0 );
! 97671: assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
! 97672: zTName = pE->pLeft->u.zToken;
! 97673: }else{
! 97674: zTName = 0;
! 97675: }
! 97676: for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
! 97677: Table *pTab = pFrom->pTab;
! 97678: char *zTabName = pFrom->zAlias;
! 97679: if( zTabName==0 ){
! 97680: zTabName = pTab->zName;
! 97681: }
! 97682: if( db->mallocFailed ) break;
! 97683: if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
! 97684: continue;
! 97685: }
! 97686: tableSeen = 1;
! 97687: for(j=0; j<pTab->nCol; j++){
! 97688: Expr *pExpr, *pRight;
! 97689: char *zName = pTab->aCol[j].zName;
! 97690: char *zColname; /* The computed column name */
! 97691: char *zToFree; /* Malloced string that needs to be freed */
! 97692: Token sColname; /* Computed column name as a token */
! 97693:
! 97694: /* If a column is marked as 'hidden' (currently only possible
! 97695: ** for virtual tables), do not include it in the expanded
! 97696: ** result-set list.
! 97697: */
! 97698: if( IsHiddenColumn(&pTab->aCol[j]) ){
! 97699: assert(IsVirtual(pTab));
! 97700: continue;
! 97701: }
! 97702:
! 97703: if( i>0 && zTName==0 ){
! 97704: if( (pFrom->jointype & JT_NATURAL)!=0
! 97705: && tableAndColumnIndex(pTabList, i, zName, 0, 0)
! 97706: ){
! 97707: /* In a NATURAL join, omit the join columns from the
! 97708: ** table to the right of the join */
! 97709: continue;
! 97710: }
! 97711: if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
! 97712: /* In a join with a USING clause, omit columns in the
! 97713: ** using clause from the table on the right. */
! 97714: continue;
! 97715: }
! 97716: }
! 97717: pRight = sqlite3Expr(db, TK_ID, zName);
! 97718: zColname = zName;
! 97719: zToFree = 0;
! 97720: if( longNames || pTabList->nSrc>1 ){
! 97721: Expr *pLeft;
! 97722: pLeft = sqlite3Expr(db, TK_ID, zTabName);
! 97723: pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
! 97724: if( longNames ){
! 97725: zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
! 97726: zToFree = zColname;
! 97727: }
! 97728: }else{
! 97729: pExpr = pRight;
! 97730: }
! 97731: pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
! 97732: sColname.z = zColname;
! 97733: sColname.n = sqlite3Strlen30(zColname);
! 97734: sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
! 97735: sqlite3DbFree(db, zToFree);
! 97736: }
! 97737: }
! 97738: if( !tableSeen ){
! 97739: if( zTName ){
! 97740: sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
! 97741: }else{
! 97742: sqlite3ErrorMsg(pParse, "no tables specified");
! 97743: }
! 97744: }
! 97745: }
! 97746: }
! 97747: sqlite3ExprListDelete(db, pEList);
! 97748: p->pEList = pNew;
! 97749: }
! 97750: #if SQLITE_MAX_COLUMN
! 97751: if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
! 97752: sqlite3ErrorMsg(pParse, "too many columns in result set");
! 97753: }
! 97754: #endif
! 97755: return WRC_Continue;
! 97756: }
! 97757:
! 97758: /*
! 97759: ** No-op routine for the parse-tree walker.
! 97760: **
! 97761: ** When this routine is the Walker.xExprCallback then expression trees
! 97762: ** are walked without any actions being taken at each node. Presumably,
! 97763: ** when this routine is used for Walker.xExprCallback then
! 97764: ** Walker.xSelectCallback is set to do something useful for every
! 97765: ** subquery in the parser tree.
! 97766: */
! 97767: static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
! 97768: UNUSED_PARAMETER2(NotUsed, NotUsed2);
! 97769: return WRC_Continue;
! 97770: }
! 97771:
! 97772: /*
! 97773: ** This routine "expands" a SELECT statement and all of its subqueries.
! 97774: ** For additional information on what it means to "expand" a SELECT
! 97775: ** statement, see the comment on the selectExpand worker callback above.
! 97776: **
! 97777: ** Expanding a SELECT statement is the first step in processing a
! 97778: ** SELECT statement. The SELECT statement must be expanded before
! 97779: ** name resolution is performed.
! 97780: **
! 97781: ** If anything goes wrong, an error message is written into pParse.
! 97782: ** The calling function can detect the problem by looking at pParse->nErr
! 97783: ** and/or pParse->db->mallocFailed.
! 97784: */
! 97785: static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
! 97786: Walker w;
! 97787: w.xSelectCallback = selectExpander;
! 97788: w.xExprCallback = exprWalkNoop;
! 97789: w.pParse = pParse;
! 97790: sqlite3WalkSelect(&w, pSelect);
! 97791: }
! 97792:
! 97793:
! 97794: #ifndef SQLITE_OMIT_SUBQUERY
! 97795: /*
! 97796: ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
! 97797: ** interface.
! 97798: **
! 97799: ** For each FROM-clause subquery, add Column.zType and Column.zColl
! 97800: ** information to the Table structure that represents the result set
! 97801: ** of that subquery.
! 97802: **
! 97803: ** The Table structure that represents the result set was constructed
! 97804: ** by selectExpander() but the type and collation information was omitted
! 97805: ** at that point because identifiers had not yet been resolved. This
! 97806: ** routine is called after identifier resolution.
! 97807: */
! 97808: static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
! 97809: Parse *pParse;
! 97810: int i;
! 97811: SrcList *pTabList;
! 97812: struct SrcList_item *pFrom;
! 97813:
! 97814: assert( p->selFlags & SF_Resolved );
! 97815: if( (p->selFlags & SF_HasTypeInfo)==0 ){
! 97816: p->selFlags |= SF_HasTypeInfo;
! 97817: pParse = pWalker->pParse;
! 97818: pTabList = p->pSrc;
! 97819: for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
! 97820: Table *pTab = pFrom->pTab;
! 97821: if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
! 97822: /* A sub-query in the FROM clause of a SELECT */
! 97823: Select *pSel = pFrom->pSelect;
! 97824: assert( pSel );
! 97825: while( pSel->pPrior ) pSel = pSel->pPrior;
! 97826: selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
! 97827: }
! 97828: }
! 97829: }
! 97830: return WRC_Continue;
! 97831: }
! 97832: #endif
! 97833:
! 97834:
! 97835: /*
! 97836: ** This routine adds datatype and collating sequence information to
! 97837: ** the Table structures of all FROM-clause subqueries in a
! 97838: ** SELECT statement.
! 97839: **
! 97840: ** Use this routine after name resolution.
! 97841: */
! 97842: static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
! 97843: #ifndef SQLITE_OMIT_SUBQUERY
! 97844: Walker w;
! 97845: w.xSelectCallback = selectAddSubqueryTypeInfo;
! 97846: w.xExprCallback = exprWalkNoop;
! 97847: w.pParse = pParse;
! 97848: sqlite3WalkSelect(&w, pSelect);
! 97849: #endif
! 97850: }
! 97851:
! 97852:
! 97853: /*
! 97854: ** This routine sets of a SELECT statement for processing. The
! 97855: ** following is accomplished:
! 97856: **
! 97857: ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
! 97858: ** * Ephemeral Table objects are created for all FROM-clause subqueries.
! 97859: ** * ON and USING clauses are shifted into WHERE statements
! 97860: ** * Wildcards "*" and "TABLE.*" in result sets are expanded.
! 97861: ** * Identifiers in expression are matched to tables.
! 97862: **
! 97863: ** This routine acts recursively on all subqueries within the SELECT.
! 97864: */
! 97865: SQLITE_PRIVATE void sqlite3SelectPrep(
! 97866: Parse *pParse, /* The parser context */
! 97867: Select *p, /* The SELECT statement being coded. */
! 97868: NameContext *pOuterNC /* Name context for container */
! 97869: ){
! 97870: sqlite3 *db;
! 97871: if( NEVER(p==0) ) return;
! 97872: db = pParse->db;
! 97873: if( p->selFlags & SF_HasTypeInfo ) return;
! 97874: sqlite3SelectExpand(pParse, p);
! 97875: if( pParse->nErr || db->mallocFailed ) return;
! 97876: sqlite3ResolveSelectNames(pParse, p, pOuterNC);
! 97877: if( pParse->nErr || db->mallocFailed ) return;
! 97878: sqlite3SelectAddTypeInfo(pParse, p);
! 97879: }
! 97880:
! 97881: /*
! 97882: ** Reset the aggregate accumulator.
! 97883: **
! 97884: ** The aggregate accumulator is a set of memory cells that hold
! 97885: ** intermediate results while calculating an aggregate. This
! 97886: ** routine simply stores NULLs in all of those memory cells.
! 97887: */
! 97888: static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
! 97889: Vdbe *v = pParse->pVdbe;
! 97890: int i;
! 97891: struct AggInfo_func *pFunc;
! 97892: if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
! 97893: return;
! 97894: }
! 97895: for(i=0; i<pAggInfo->nColumn; i++){
! 97896: sqlite3VdbeAddOp2(v, OP_Null, 0, pAggInfo->aCol[i].iMem);
! 97897: }
! 97898: for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
! 97899: sqlite3VdbeAddOp2(v, OP_Null, 0, pFunc->iMem);
! 97900: if( pFunc->iDistinct>=0 ){
! 97901: Expr *pE = pFunc->pExpr;
! 97902: assert( !ExprHasProperty(pE, EP_xIsSelect) );
! 97903: if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
! 97904: sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
! 97905: "argument");
! 97906: pFunc->iDistinct = -1;
! 97907: }else{
! 97908: KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList);
! 97909: sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
! 97910: (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
! 97911: }
! 97912: }
! 97913: }
! 97914: }
! 97915:
! 97916: /*
! 97917: ** Invoke the OP_AggFinalize opcode for every aggregate function
! 97918: ** in the AggInfo structure.
! 97919: */
! 97920: static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
! 97921: Vdbe *v = pParse->pVdbe;
! 97922: int i;
! 97923: struct AggInfo_func *pF;
! 97924: for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
! 97925: ExprList *pList = pF->pExpr->x.pList;
! 97926: assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
! 97927: sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
! 97928: (void*)pF->pFunc, P4_FUNCDEF);
! 97929: }
! 97930: }
! 97931:
! 97932: /*
! 97933: ** Update the accumulator memory cells for an aggregate based on
! 97934: ** the current cursor position.
! 97935: */
! 97936: static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
! 97937: Vdbe *v = pParse->pVdbe;
! 97938: int i;
! 97939: struct AggInfo_func *pF;
! 97940: struct AggInfo_col *pC;
! 97941:
! 97942: pAggInfo->directMode = 1;
! 97943: sqlite3ExprCacheClear(pParse);
! 97944: for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
! 97945: int nArg;
! 97946: int addrNext = 0;
! 97947: int regAgg;
! 97948: ExprList *pList = pF->pExpr->x.pList;
! 97949: assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
! 97950: if( pList ){
! 97951: nArg = pList->nExpr;
! 97952: regAgg = sqlite3GetTempRange(pParse, nArg);
! 97953: sqlite3ExprCodeExprList(pParse, pList, regAgg, 1);
! 97954: }else{
! 97955: nArg = 0;
! 97956: regAgg = 0;
! 97957: }
! 97958: if( pF->iDistinct>=0 ){
! 97959: addrNext = sqlite3VdbeMakeLabel(v);
! 97960: assert( nArg==1 );
! 97961: codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
! 97962: }
! 97963: if( pF->pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
! 97964: CollSeq *pColl = 0;
! 97965: struct ExprList_item *pItem;
! 97966: int j;
! 97967: assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
! 97968: for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
! 97969: pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
! 97970: }
! 97971: if( !pColl ){
! 97972: pColl = pParse->db->pDfltColl;
! 97973: }
! 97974: sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
! 97975: }
! 97976: sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
! 97977: (void*)pF->pFunc, P4_FUNCDEF);
! 97978: sqlite3VdbeChangeP5(v, (u8)nArg);
! 97979: sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
! 97980: sqlite3ReleaseTempRange(pParse, regAgg, nArg);
! 97981: if( addrNext ){
! 97982: sqlite3VdbeResolveLabel(v, addrNext);
! 97983: sqlite3ExprCacheClear(pParse);
! 97984: }
! 97985: }
! 97986:
! 97987: /* Before populating the accumulator registers, clear the column cache.
! 97988: ** Otherwise, if any of the required column values are already present
! 97989: ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
! 97990: ** to pC->iMem. But by the time the value is used, the original register
! 97991: ** may have been used, invalidating the underlying buffer holding the
! 97992: ** text or blob value. See ticket [883034dcb5].
! 97993: **
! 97994: ** Another solution would be to change the OP_SCopy used to copy cached
! 97995: ** values to an OP_Copy.
! 97996: */
! 97997: sqlite3ExprCacheClear(pParse);
! 97998: for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
! 97999: sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
! 98000: }
! 98001: pAggInfo->directMode = 0;
! 98002: sqlite3ExprCacheClear(pParse);
! 98003: }
! 98004:
! 98005: /*
! 98006: ** Add a single OP_Explain instruction to the VDBE to explain a simple
! 98007: ** count(*) query ("SELECT count(*) FROM pTab").
! 98008: */
! 98009: #ifndef SQLITE_OMIT_EXPLAIN
! 98010: static void explainSimpleCount(
! 98011: Parse *pParse, /* Parse context */
! 98012: Table *pTab, /* Table being queried */
! 98013: Index *pIdx /* Index used to optimize scan, or NULL */
! 98014: ){
! 98015: if( pParse->explain==2 ){
! 98016: char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)",
! 98017: pTab->zName,
! 98018: pIdx ? "USING COVERING INDEX " : "",
! 98019: pIdx ? pIdx->zName : "",
! 98020: pTab->nRowEst
! 98021: );
! 98022: sqlite3VdbeAddOp4(
! 98023: pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
! 98024: );
! 98025: }
! 98026: }
! 98027: #else
! 98028: # define explainSimpleCount(a,b,c)
! 98029: #endif
! 98030:
! 98031: /*
! 98032: ** Generate code for the SELECT statement given in the p argument.
! 98033: **
! 98034: ** The results are distributed in various ways depending on the
! 98035: ** contents of the SelectDest structure pointed to by argument pDest
! 98036: ** as follows:
! 98037: **
! 98038: ** pDest->eDest Result
! 98039: ** ------------ -------------------------------------------
! 98040: ** SRT_Output Generate a row of output (using the OP_ResultRow
! 98041: ** opcode) for each row in the result set.
! 98042: **
! 98043: ** SRT_Mem Only valid if the result is a single column.
! 98044: ** Store the first column of the first result row
! 98045: ** in register pDest->iParm then abandon the rest
! 98046: ** of the query. This destination implies "LIMIT 1".
! 98047: **
! 98048: ** SRT_Set The result must be a single column. Store each
! 98049: ** row of result as the key in table pDest->iParm.
! 98050: ** Apply the affinity pDest->affinity before storing
! 98051: ** results. Used to implement "IN (SELECT ...)".
! 98052: **
! 98053: ** SRT_Union Store results as a key in a temporary table pDest->iParm.
! 98054: **
! 98055: ** SRT_Except Remove results from the temporary table pDest->iParm.
! 98056: **
! 98057: ** SRT_Table Store results in temporary table pDest->iParm.
! 98058: ** This is like SRT_EphemTab except that the table
! 98059: ** is assumed to already be open.
! 98060: **
! 98061: ** SRT_EphemTab Create an temporary table pDest->iParm and store
! 98062: ** the result there. The cursor is left open after
! 98063: ** returning. This is like SRT_Table except that
! 98064: ** this destination uses OP_OpenEphemeral to create
! 98065: ** the table first.
! 98066: **
! 98067: ** SRT_Coroutine Generate a co-routine that returns a new row of
! 98068: ** results each time it is invoked. The entry point
! 98069: ** of the co-routine is stored in register pDest->iParm.
! 98070: **
! 98071: ** SRT_Exists Store a 1 in memory cell pDest->iParm if the result
! 98072: ** set is not empty.
! 98073: **
! 98074: ** SRT_Discard Throw the results away. This is used by SELECT
! 98075: ** statements within triggers whose only purpose is
! 98076: ** the side-effects of functions.
! 98077: **
! 98078: ** This routine returns the number of errors. If any errors are
! 98079: ** encountered, then an appropriate error message is left in
! 98080: ** pParse->zErrMsg.
! 98081: **
! 98082: ** This routine does NOT free the Select structure passed in. The
! 98083: ** calling function needs to do that.
! 98084: */
! 98085: SQLITE_PRIVATE int sqlite3Select(
! 98086: Parse *pParse, /* The parser context */
! 98087: Select *p, /* The SELECT statement being coded. */
! 98088: SelectDest *pDest /* What to do with the query results */
! 98089: ){
! 98090: int i, j; /* Loop counters */
! 98091: WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
! 98092: Vdbe *v; /* The virtual machine under construction */
! 98093: int isAgg; /* True for select lists like "count(*)" */
! 98094: ExprList *pEList; /* List of columns to extract. */
! 98095: SrcList *pTabList; /* List of tables to select from */
! 98096: Expr *pWhere; /* The WHERE clause. May be NULL */
! 98097: ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
! 98098: ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
! 98099: Expr *pHaving; /* The HAVING clause. May be NULL */
! 98100: int isDistinct; /* True if the DISTINCT keyword is present */
! 98101: int distinct; /* Table to use for the distinct set */
! 98102: int rc = 1; /* Value to return from this function */
! 98103: int addrSortIndex; /* Address of an OP_OpenEphemeral instruction */
! 98104: int addrDistinctIndex; /* Address of an OP_OpenEphemeral instruction */
! 98105: AggInfo sAggInfo; /* Information used by aggregate queries */
! 98106: int iEnd; /* Address of the end of the query */
! 98107: sqlite3 *db; /* The database connection */
! 98108:
! 98109: #ifndef SQLITE_OMIT_EXPLAIN
! 98110: int iRestoreSelectId = pParse->iSelectId;
! 98111: pParse->iSelectId = pParse->iNextSelectId++;
! 98112: #endif
! 98113:
! 98114: db = pParse->db;
! 98115: if( p==0 || db->mallocFailed || pParse->nErr ){
! 98116: return 1;
! 98117: }
! 98118: if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
! 98119: memset(&sAggInfo, 0, sizeof(sAggInfo));
! 98120:
! 98121: if( IgnorableOrderby(pDest) ){
! 98122: assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
! 98123: pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
! 98124: /* If ORDER BY makes no difference in the output then neither does
! 98125: ** DISTINCT so it can be removed too. */
! 98126: sqlite3ExprListDelete(db, p->pOrderBy);
! 98127: p->pOrderBy = 0;
! 98128: p->selFlags &= ~SF_Distinct;
! 98129: }
! 98130: sqlite3SelectPrep(pParse, p, 0);
! 98131: pOrderBy = p->pOrderBy;
! 98132: pTabList = p->pSrc;
! 98133: pEList = p->pEList;
! 98134: if( pParse->nErr || db->mallocFailed ){
! 98135: goto select_end;
! 98136: }
! 98137: isAgg = (p->selFlags & SF_Aggregate)!=0;
! 98138: assert( pEList!=0 );
! 98139:
! 98140: /* Begin generating code.
! 98141: */
! 98142: v = sqlite3GetVdbe(pParse);
! 98143: if( v==0 ) goto select_end;
! 98144:
! 98145: /* If writing to memory or generating a set
! 98146: ** only a single column may be output.
! 98147: */
! 98148: #ifndef SQLITE_OMIT_SUBQUERY
! 98149: if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
! 98150: goto select_end;
! 98151: }
! 98152: #endif
! 98153:
! 98154: /* Generate code for all sub-queries in the FROM clause
! 98155: */
! 98156: #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
! 98157: for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
! 98158: struct SrcList_item *pItem = &pTabList->a[i];
! 98159: SelectDest dest;
! 98160: Select *pSub = pItem->pSelect;
! 98161: int isAggSub;
! 98162:
! 98163: if( pSub==0 ) continue;
! 98164: if( pItem->addrFillSub ){
! 98165: sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
! 98166: continue;
! 98167: }
! 98168:
! 98169: /* Increment Parse.nHeight by the height of the largest expression
! 98170: ** tree refered to by this, the parent select. The child select
! 98171: ** may contain expression trees of at most
! 98172: ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
! 98173: ** more conservative than necessary, but much easier than enforcing
! 98174: ** an exact limit.
! 98175: */
! 98176: pParse->nHeight += sqlite3SelectExprHeight(p);
! 98177:
! 98178: isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
! 98179: if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
! 98180: /* This subquery can be absorbed into its parent. */
! 98181: if( isAggSub ){
! 98182: isAgg = 1;
! 98183: p->selFlags |= SF_Aggregate;
! 98184: }
! 98185: i = -1;
! 98186: }else{
! 98187: /* Generate a subroutine that will fill an ephemeral table with
! 98188: ** the content of this subquery. pItem->addrFillSub will point
! 98189: ** to the address of the generated subroutine. pItem->regReturn
! 98190: ** is a register allocated to hold the subroutine return address
! 98191: */
! 98192: int topAddr;
! 98193: int onceAddr = 0;
! 98194: int retAddr;
! 98195: assert( pItem->addrFillSub==0 );
! 98196: pItem->regReturn = ++pParse->nMem;
! 98197: topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
! 98198: pItem->addrFillSub = topAddr+1;
! 98199: VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
! 98200: if( pItem->isCorrelated==0 ){
! 98201: /* If the subquery is no correlated and if we are not inside of
! 98202: ** a trigger, then we only need to compute the value of the subquery
! 98203: ** once. */
! 98204: onceAddr = sqlite3CodeOnce(pParse);
! 98205: }
! 98206: sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
! 98207: explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
! 98208: sqlite3Select(pParse, pSub, &dest);
! 98209: pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
! 98210: if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
! 98211: retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
! 98212: VdbeComment((v, "end %s", pItem->pTab->zName));
! 98213: sqlite3VdbeChangeP1(v, topAddr, retAddr);
! 98214: sqlite3ClearTempRegCache(pParse);
! 98215: }
! 98216: if( /*pParse->nErr ||*/ db->mallocFailed ){
! 98217: goto select_end;
! 98218: }
! 98219: pParse->nHeight -= sqlite3SelectExprHeight(p);
! 98220: pTabList = p->pSrc;
! 98221: if( !IgnorableOrderby(pDest) ){
! 98222: pOrderBy = p->pOrderBy;
! 98223: }
! 98224: }
! 98225: pEList = p->pEList;
! 98226: #endif
! 98227: pWhere = p->pWhere;
! 98228: pGroupBy = p->pGroupBy;
! 98229: pHaving = p->pHaving;
! 98230: isDistinct = (p->selFlags & SF_Distinct)!=0;
! 98231:
! 98232: #ifndef SQLITE_OMIT_COMPOUND_SELECT
! 98233: /* If there is are a sequence of queries, do the earlier ones first.
! 98234: */
! 98235: if( p->pPrior ){
! 98236: if( p->pRightmost==0 ){
! 98237: Select *pLoop, *pRight = 0;
! 98238: int cnt = 0;
! 98239: int mxSelect;
! 98240: for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
! 98241: pLoop->pRightmost = p;
! 98242: pLoop->pNext = pRight;
! 98243: pRight = pLoop;
! 98244: }
! 98245: mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
! 98246: if( mxSelect && cnt>mxSelect ){
! 98247: sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
! 98248: goto select_end;
! 98249: }
! 98250: }
! 98251: rc = multiSelect(pParse, p, pDest);
! 98252: explainSetInteger(pParse->iSelectId, iRestoreSelectId);
! 98253: return rc;
! 98254: }
! 98255: #endif
! 98256:
! 98257: /* If there is both a GROUP BY and an ORDER BY clause and they are
! 98258: ** identical, then disable the ORDER BY clause since the GROUP BY
! 98259: ** will cause elements to come out in the correct order. This is
! 98260: ** an optimization - the correct answer should result regardless.
! 98261: ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
! 98262: ** to disable this optimization for testing purposes.
! 98263: */
! 98264: if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
! 98265: && (db->flags & SQLITE_GroupByOrder)==0 ){
! 98266: pOrderBy = 0;
! 98267: }
! 98268:
! 98269: /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
! 98270: ** if the select-list is the same as the ORDER BY list, then this query
! 98271: ** can be rewritten as a GROUP BY. In other words, this:
! 98272: **
! 98273: ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
! 98274: **
! 98275: ** is transformed to:
! 98276: **
! 98277: ** SELECT xyz FROM ... GROUP BY xyz
! 98278: **
! 98279: ** The second form is preferred as a single index (or temp-table) may be
! 98280: ** used for both the ORDER BY and DISTINCT processing. As originally
! 98281: ** written the query must use a temp-table for at least one of the ORDER
! 98282: ** BY and DISTINCT, and an index or separate temp-table for the other.
! 98283: */
! 98284: if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
! 98285: && sqlite3ExprListCompare(pOrderBy, p->pEList)==0
! 98286: ){
! 98287: p->selFlags &= ~SF_Distinct;
! 98288: p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
! 98289: pGroupBy = p->pGroupBy;
! 98290: pOrderBy = 0;
! 98291: }
! 98292:
! 98293: /* If there is an ORDER BY clause, then this sorting
! 98294: ** index might end up being unused if the data can be
! 98295: ** extracted in pre-sorted order. If that is the case, then the
! 98296: ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
! 98297: ** we figure out that the sorting index is not needed. The addrSortIndex
! 98298: ** variable is used to facilitate that change.
! 98299: */
! 98300: if( pOrderBy ){
! 98301: KeyInfo *pKeyInfo;
! 98302: pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
! 98303: pOrderBy->iECursor = pParse->nTab++;
! 98304: p->addrOpenEphm[2] = addrSortIndex =
! 98305: sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
! 98306: pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
! 98307: (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
! 98308: }else{
! 98309: addrSortIndex = -1;
! 98310: }
! 98311:
! 98312: /* If the output is destined for a temporary table, open that table.
! 98313: */
! 98314: if( pDest->eDest==SRT_EphemTab ){
! 98315: sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, pEList->nExpr);
! 98316: }
! 98317:
! 98318: /* Set the limiter.
! 98319: */
! 98320: iEnd = sqlite3VdbeMakeLabel(v);
! 98321: p->nSelectRow = (double)LARGEST_INT64;
! 98322: computeLimitRegisters(pParse, p, iEnd);
! 98323: if( p->iLimit==0 && addrSortIndex>=0 ){
! 98324: sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
! 98325: p->selFlags |= SF_UseSorter;
! 98326: }
! 98327:
! 98328: /* Open a virtual index to use for the distinct set.
! 98329: */
! 98330: if( p->selFlags & SF_Distinct ){
! 98331: KeyInfo *pKeyInfo;
! 98332: distinct = pParse->nTab++;
! 98333: pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
! 98334: addrDistinctIndex = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0,
! 98335: (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
! 98336: sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
! 98337: }else{
! 98338: distinct = addrDistinctIndex = -1;
! 98339: }
! 98340:
! 98341: /* Aggregate and non-aggregate queries are handled differently */
! 98342: if( !isAgg && pGroupBy==0 ){
! 98343: ExprList *pDist = (isDistinct ? p->pEList : 0);
! 98344:
! 98345: /* Begin the database scan. */
! 98346: pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, pDist, 0);
! 98347: if( pWInfo==0 ) goto select_end;
! 98348: if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
! 98349:
! 98350: /* If sorting index that was created by a prior OP_OpenEphemeral
! 98351: ** instruction ended up not being needed, then change the OP_OpenEphemeral
! 98352: ** into an OP_Noop.
! 98353: */
! 98354: if( addrSortIndex>=0 && pOrderBy==0 ){
! 98355: sqlite3VdbeChangeToNoop(v, addrSortIndex);
! 98356: p->addrOpenEphm[2] = -1;
! 98357: }
! 98358:
! 98359: if( pWInfo->eDistinct ){
! 98360: VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
! 98361:
! 98362: assert( addrDistinctIndex>=0 );
! 98363: pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
! 98364:
! 98365: assert( isDistinct );
! 98366: assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
! 98367: || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE
! 98368: );
! 98369: distinct = -1;
! 98370: if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED ){
! 98371: int iJump;
! 98372: int iExpr;
! 98373: int iFlag = ++pParse->nMem;
! 98374: int iBase = pParse->nMem+1;
! 98375: int iBase2 = iBase + pEList->nExpr;
! 98376: pParse->nMem += (pEList->nExpr*2);
! 98377:
! 98378: /* Change the OP_OpenEphemeral coded earlier to an OP_Integer. The
! 98379: ** OP_Integer initializes the "first row" flag. */
! 98380: pOp->opcode = OP_Integer;
! 98381: pOp->p1 = 1;
! 98382: pOp->p2 = iFlag;
! 98383:
! 98384: sqlite3ExprCodeExprList(pParse, pEList, iBase, 1);
! 98385: iJump = sqlite3VdbeCurrentAddr(v) + 1 + pEList->nExpr + 1 + 1;
! 98386: sqlite3VdbeAddOp2(v, OP_If, iFlag, iJump-1);
! 98387: for(iExpr=0; iExpr<pEList->nExpr; iExpr++){
! 98388: CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[iExpr].pExpr);
! 98389: sqlite3VdbeAddOp3(v, OP_Ne, iBase+iExpr, iJump, iBase2+iExpr);
! 98390: sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
! 98391: sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
! 98392: }
! 98393: sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iContinue);
! 98394:
! 98395: sqlite3VdbeAddOp2(v, OP_Integer, 0, iFlag);
! 98396: assert( sqlite3VdbeCurrentAddr(v)==iJump );
! 98397: sqlite3VdbeAddOp3(v, OP_Move, iBase, iBase2, pEList->nExpr);
! 98398: }else{
! 98399: pOp->opcode = OP_Noop;
! 98400: }
! 98401: }
! 98402:
! 98403: /* Use the standard inner loop. */
! 98404: selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, pDest,
! 98405: pWInfo->iContinue, pWInfo->iBreak);
! 98406:
! 98407: /* End the database scan loop.
! 98408: */
! 98409: sqlite3WhereEnd(pWInfo);
! 98410: }else{
! 98411: /* This is the processing for aggregate queries */
! 98412: NameContext sNC; /* Name context for processing aggregate information */
! 98413: int iAMem; /* First Mem address for storing current GROUP BY */
! 98414: int iBMem; /* First Mem address for previous GROUP BY */
! 98415: int iUseFlag; /* Mem address holding flag indicating that at least
! 98416: ** one row of the input to the aggregator has been
! 98417: ** processed */
! 98418: int iAbortFlag; /* Mem address which causes query abort if positive */
! 98419: int groupBySort; /* Rows come from source in GROUP BY order */
! 98420: int addrEnd; /* End of processing for this SELECT */
! 98421: int sortPTab = 0; /* Pseudotable used to decode sorting results */
! 98422: int sortOut = 0; /* Output register from the sorter */
! 98423:
! 98424: /* Remove any and all aliases between the result set and the
! 98425: ** GROUP BY clause.
! 98426: */
! 98427: if( pGroupBy ){
! 98428: int k; /* Loop counter */
! 98429: struct ExprList_item *pItem; /* For looping over expression in a list */
! 98430:
! 98431: for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
! 98432: pItem->iAlias = 0;
! 98433: }
! 98434: for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
! 98435: pItem->iAlias = 0;
! 98436: }
! 98437: if( p->nSelectRow>(double)100 ) p->nSelectRow = (double)100;
! 98438: }else{
! 98439: p->nSelectRow = (double)1;
! 98440: }
! 98441:
! 98442:
! 98443: /* Create a label to jump to when we want to abort the query */
! 98444: addrEnd = sqlite3VdbeMakeLabel(v);
! 98445:
! 98446: /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
! 98447: ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
! 98448: ** SELECT statement.
! 98449: */
! 98450: memset(&sNC, 0, sizeof(sNC));
! 98451: sNC.pParse = pParse;
! 98452: sNC.pSrcList = pTabList;
! 98453: sNC.pAggInfo = &sAggInfo;
! 98454: sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
! 98455: sAggInfo.pGroupBy = pGroupBy;
! 98456: sqlite3ExprAnalyzeAggList(&sNC, pEList);
! 98457: sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
! 98458: if( pHaving ){
! 98459: sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
! 98460: }
! 98461: sAggInfo.nAccumulator = sAggInfo.nColumn;
! 98462: for(i=0; i<sAggInfo.nFunc; i++){
! 98463: assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
! 98464: sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
! 98465: }
! 98466: if( db->mallocFailed ) goto select_end;
! 98467:
! 98468: /* Processing for aggregates with GROUP BY is very different and
! 98469: ** much more complex than aggregates without a GROUP BY.
! 98470: */
! 98471: if( pGroupBy ){
! 98472: KeyInfo *pKeyInfo; /* Keying information for the group by clause */
! 98473: int j1; /* A-vs-B comparision jump */
! 98474: int addrOutputRow; /* Start of subroutine that outputs a result row */
! 98475: int regOutputRow; /* Return address register for output subroutine */
! 98476: int addrSetAbort; /* Set the abort flag and return */
! 98477: int addrTopOfLoop; /* Top of the input loop */
! 98478: int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
! 98479: int addrReset; /* Subroutine for resetting the accumulator */
! 98480: int regReset; /* Return address register for reset subroutine */
! 98481:
! 98482: /* If there is a GROUP BY clause we might need a sorting index to
! 98483: ** implement it. Allocate that sorting index now. If it turns out
! 98484: ** that we do not need it after all, the OP_SorterOpen instruction
! 98485: ** will be converted into a Noop.
! 98486: */
! 98487: sAggInfo.sortingIdx = pParse->nTab++;
! 98488: pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
! 98489: addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
! 98490: sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
! 98491: 0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
! 98492:
! 98493: /* Initialize memory locations used by GROUP BY aggregate processing
! 98494: */
! 98495: iUseFlag = ++pParse->nMem;
! 98496: iAbortFlag = ++pParse->nMem;
! 98497: regOutputRow = ++pParse->nMem;
! 98498: addrOutputRow = sqlite3VdbeMakeLabel(v);
! 98499: regReset = ++pParse->nMem;
! 98500: addrReset = sqlite3VdbeMakeLabel(v);
! 98501: iAMem = pParse->nMem + 1;
! 98502: pParse->nMem += pGroupBy->nExpr;
! 98503: iBMem = pParse->nMem + 1;
! 98504: pParse->nMem += pGroupBy->nExpr;
! 98505: sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
! 98506: VdbeComment((v, "clear abort flag"));
! 98507: sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
! 98508: VdbeComment((v, "indicate accumulator empty"));
! 98509: sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
! 98510:
! 98511: /* Begin a loop that will extract all source rows in GROUP BY order.
! 98512: ** This might involve two separate loops with an OP_Sort in between, or
! 98513: ** it might be a single loop that uses an index to extract information
! 98514: ** in the right order to begin with.
! 98515: */
! 98516: sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
! 98517: pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0, 0);
! 98518: if( pWInfo==0 ) goto select_end;
! 98519: if( pGroupBy==0 ){
! 98520: /* The optimizer is able to deliver rows in group by order so
! 98521: ** we do not have to sort. The OP_OpenEphemeral table will be
! 98522: ** cancelled later because we still need to use the pKeyInfo
! 98523: */
! 98524: pGroupBy = p->pGroupBy;
! 98525: groupBySort = 0;
! 98526: }else{
! 98527: /* Rows are coming out in undetermined order. We have to push
! 98528: ** each row into a sorting index, terminate the first loop,
! 98529: ** then loop over the sorting index in order to get the output
! 98530: ** in sorted order
! 98531: */
! 98532: int regBase;
! 98533: int regRecord;
! 98534: int nCol;
! 98535: int nGroupBy;
! 98536:
! 98537: explainTempTable(pParse,
! 98538: isDistinct && !(p->selFlags&SF_Distinct)?"DISTINCT":"GROUP BY");
! 98539:
! 98540: groupBySort = 1;
! 98541: nGroupBy = pGroupBy->nExpr;
! 98542: nCol = nGroupBy + 1;
! 98543: j = nGroupBy+1;
! 98544: for(i=0; i<sAggInfo.nColumn; i++){
! 98545: if( sAggInfo.aCol[i].iSorterColumn>=j ){
! 98546: nCol++;
! 98547: j++;
! 98548: }
! 98549: }
! 98550: regBase = sqlite3GetTempRange(pParse, nCol);
! 98551: sqlite3ExprCacheClear(pParse);
! 98552: sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
! 98553: sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy);
! 98554: j = nGroupBy+1;
! 98555: for(i=0; i<sAggInfo.nColumn; i++){
! 98556: struct AggInfo_col *pCol = &sAggInfo.aCol[i];
! 98557: if( pCol->iSorterColumn>=j ){
! 98558: int r1 = j + regBase;
! 98559: int r2;
! 98560:
! 98561: r2 = sqlite3ExprCodeGetColumn(pParse,
! 98562: pCol->pTab, pCol->iColumn, pCol->iTable, r1);
! 98563: if( r1!=r2 ){
! 98564: sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
! 98565: }
! 98566: j++;
! 98567: }
! 98568: }
! 98569: regRecord = sqlite3GetTempReg(pParse);
! 98570: sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
! 98571: sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
! 98572: sqlite3ReleaseTempReg(pParse, regRecord);
! 98573: sqlite3ReleaseTempRange(pParse, regBase, nCol);
! 98574: sqlite3WhereEnd(pWInfo);
! 98575: sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
! 98576: sortOut = sqlite3GetTempReg(pParse);
! 98577: sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
! 98578: sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
! 98579: VdbeComment((v, "GROUP BY sort"));
! 98580: sAggInfo.useSortingIdx = 1;
! 98581: sqlite3ExprCacheClear(pParse);
! 98582: }
! 98583:
! 98584: /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
! 98585: ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
! 98586: ** Then compare the current GROUP BY terms against the GROUP BY terms
! 98587: ** from the previous row currently stored in a0, a1, a2...
! 98588: */
! 98589: addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
! 98590: sqlite3ExprCacheClear(pParse);
! 98591: if( groupBySort ){
! 98592: sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
! 98593: }
! 98594: for(j=0; j<pGroupBy->nExpr; j++){
! 98595: if( groupBySort ){
! 98596: sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
! 98597: if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
! 98598: }else{
! 98599: sAggInfo.directMode = 1;
! 98600: sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
! 98601: }
! 98602: }
! 98603: sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
! 98604: (char*)pKeyInfo, P4_KEYINFO);
! 98605: j1 = sqlite3VdbeCurrentAddr(v);
! 98606: sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
! 98607:
! 98608: /* Generate code that runs whenever the GROUP BY changes.
! 98609: ** Changes in the GROUP BY are detected by the previous code
! 98610: ** block. If there were no changes, this block is skipped.
! 98611: **
! 98612: ** This code copies current group by terms in b0,b1,b2,...
! 98613: ** over to a0,a1,a2. It then calls the output subroutine
! 98614: ** and resets the aggregate accumulator registers in preparation
! 98615: ** for the next GROUP BY batch.
! 98616: */
! 98617: sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
! 98618: sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
! 98619: VdbeComment((v, "output one row"));
! 98620: sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd);
! 98621: VdbeComment((v, "check abort flag"));
! 98622: sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
! 98623: VdbeComment((v, "reset accumulator"));
! 98624:
! 98625: /* Update the aggregate accumulators based on the content of
! 98626: ** the current row
! 98627: */
! 98628: sqlite3VdbeJumpHere(v, j1);
! 98629: updateAccumulator(pParse, &sAggInfo);
! 98630: sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
! 98631: VdbeComment((v, "indicate data in accumulator"));
! 98632:
! 98633: /* End of the loop
! 98634: */
! 98635: if( groupBySort ){
! 98636: sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
! 98637: }else{
! 98638: sqlite3WhereEnd(pWInfo);
! 98639: sqlite3VdbeChangeToNoop(v, addrSortingIdx);
! 98640: }
! 98641:
! 98642: /* Output the final row of result
! 98643: */
! 98644: sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
! 98645: VdbeComment((v, "output final row"));
! 98646:
! 98647: /* Jump over the subroutines
! 98648: */
! 98649: sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
! 98650:
! 98651: /* Generate a subroutine that outputs a single row of the result
! 98652: ** set. This subroutine first looks at the iUseFlag. If iUseFlag
! 98653: ** is less than or equal to zero, the subroutine is a no-op. If
! 98654: ** the processing calls for the query to abort, this subroutine
! 98655: ** increments the iAbortFlag memory location before returning in
! 98656: ** order to signal the caller to abort.
! 98657: */
! 98658: addrSetAbort = sqlite3VdbeCurrentAddr(v);
! 98659: sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
! 98660: VdbeComment((v, "set abort flag"));
! 98661: sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
! 98662: sqlite3VdbeResolveLabel(v, addrOutputRow);
! 98663: addrOutputRow = sqlite3VdbeCurrentAddr(v);
! 98664: sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
! 98665: VdbeComment((v, "Groupby result generator entry point"));
! 98666: sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
! 98667: finalizeAggFunctions(pParse, &sAggInfo);
! 98668: sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
! 98669: selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
! 98670: distinct, pDest,
! 98671: addrOutputRow+1, addrSetAbort);
! 98672: sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
! 98673: VdbeComment((v, "end groupby result generator"));
! 98674:
! 98675: /* Generate a subroutine that will reset the group-by accumulator
! 98676: */
! 98677: sqlite3VdbeResolveLabel(v, addrReset);
! 98678: resetAccumulator(pParse, &sAggInfo);
! 98679: sqlite3VdbeAddOp1(v, OP_Return, regReset);
! 98680:
! 98681: } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
! 98682: else {
! 98683: ExprList *pDel = 0;
! 98684: #ifndef SQLITE_OMIT_BTREECOUNT
! 98685: Table *pTab;
! 98686: if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
! 98687: /* If isSimpleCount() returns a pointer to a Table structure, then
! 98688: ** the SQL statement is of the form:
! 98689: **
! 98690: ** SELECT count(*) FROM <tbl>
! 98691: **
! 98692: ** where the Table structure returned represents table <tbl>.
! 98693: **
! 98694: ** This statement is so common that it is optimized specially. The
! 98695: ** OP_Count instruction is executed either on the intkey table that
! 98696: ** contains the data for table <tbl> or on one of its indexes. It
! 98697: ** is better to execute the op on an index, as indexes are almost
! 98698: ** always spread across less pages than their corresponding tables.
! 98699: */
! 98700: const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
! 98701: const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
! 98702: Index *pIdx; /* Iterator variable */
! 98703: KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
! 98704: Index *pBest = 0; /* Best index found so far */
! 98705: int iRoot = pTab->tnum; /* Root page of scanned b-tree */
! 98706:
! 98707: sqlite3CodeVerifySchema(pParse, iDb);
! 98708: sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
! 98709:
! 98710: /* Search for the index that has the least amount of columns. If
! 98711: ** there is such an index, and it has less columns than the table
! 98712: ** does, then we can assume that it consumes less space on disk and
! 98713: ** will therefore be cheaper to scan to determine the query result.
! 98714: ** In this case set iRoot to the root page number of the index b-tree
! 98715: ** and pKeyInfo to the KeyInfo structure required to navigate the
! 98716: ** index.
! 98717: **
! 98718: ** (2011-04-15) Do not do a full scan of an unordered index.
! 98719: **
! 98720: ** In practice the KeyInfo structure will not be used. It is only
! 98721: ** passed to keep OP_OpenRead happy.
! 98722: */
! 98723: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
! 98724: if( pIdx->bUnordered==0 && (!pBest || pIdx->nColumn<pBest->nColumn) ){
! 98725: pBest = pIdx;
! 98726: }
! 98727: }
! 98728: if( pBest && pBest->nColumn<pTab->nCol ){
! 98729: iRoot = pBest->tnum;
! 98730: pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
! 98731: }
! 98732:
! 98733: /* Open a read-only cursor, execute the OP_Count, close the cursor. */
! 98734: sqlite3VdbeAddOp3(v, OP_OpenRead, iCsr, iRoot, iDb);
! 98735: if( pKeyInfo ){
! 98736: sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO_HANDOFF);
! 98737: }
! 98738: sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
! 98739: sqlite3VdbeAddOp1(v, OP_Close, iCsr);
! 98740: explainSimpleCount(pParse, pTab, pBest);
! 98741: }else
! 98742: #endif /* SQLITE_OMIT_BTREECOUNT */
! 98743: {
! 98744: /* Check if the query is of one of the following forms:
! 98745: **
! 98746: ** SELECT min(x) FROM ...
! 98747: ** SELECT max(x) FROM ...
! 98748: **
! 98749: ** If it is, then ask the code in where.c to attempt to sort results
! 98750: ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause.
! 98751: ** If where.c is able to produce results sorted in this order, then
! 98752: ** add vdbe code to break out of the processing loop after the
! 98753: ** first iteration (since the first iteration of the loop is
! 98754: ** guaranteed to operate on the row with the minimum or maximum
! 98755: ** value of x, the only row required).
! 98756: **
! 98757: ** A special flag must be passed to sqlite3WhereBegin() to slightly
! 98758: ** modify behaviour as follows:
! 98759: **
! 98760: ** + If the query is a "SELECT min(x)", then the loop coded by
! 98761: ** where.c should not iterate over any values with a NULL value
! 98762: ** for x.
! 98763: **
! 98764: ** + The optimizer code in where.c (the thing that decides which
! 98765: ** index or indices to use) should place a different priority on
! 98766: ** satisfying the 'ORDER BY' clause than it does in other cases.
! 98767: ** Refer to code and comments in where.c for details.
! 98768: */
! 98769: ExprList *pMinMax = 0;
! 98770: u8 flag = minMaxQuery(p);
! 98771: if( flag ){
! 98772: assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
! 98773: pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
! 98774: pDel = pMinMax;
! 98775: if( pMinMax && !db->mallocFailed ){
! 98776: pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
! 98777: pMinMax->a[0].pExpr->op = TK_COLUMN;
! 98778: }
! 98779: }
! 98780:
! 98781: /* This case runs if the aggregate has no GROUP BY clause. The
! 98782: ** processing is much simpler since there is only a single row
! 98783: ** of output.
! 98784: */
! 98785: resetAccumulator(pParse, &sAggInfo);
! 98786: pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax, 0, flag);
! 98787: if( pWInfo==0 ){
! 98788: sqlite3ExprListDelete(db, pDel);
! 98789: goto select_end;
! 98790: }
! 98791: updateAccumulator(pParse, &sAggInfo);
! 98792: if( !pMinMax && flag ){
! 98793: sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
! 98794: VdbeComment((v, "%s() by index",
! 98795: (flag==WHERE_ORDERBY_MIN?"min":"max")));
! 98796: }
! 98797: sqlite3WhereEnd(pWInfo);
! 98798: finalizeAggFunctions(pParse, &sAggInfo);
! 98799: }
! 98800:
! 98801: pOrderBy = 0;
! 98802: sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
! 98803: selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1,
! 98804: pDest, addrEnd, addrEnd);
! 98805: sqlite3ExprListDelete(db, pDel);
! 98806: }
! 98807: sqlite3VdbeResolveLabel(v, addrEnd);
! 98808:
! 98809: } /* endif aggregate query */
! 98810:
! 98811: if( distinct>=0 ){
! 98812: explainTempTable(pParse, "DISTINCT");
! 98813: }
! 98814:
! 98815: /* If there is an ORDER BY clause, then we need to sort the results
! 98816: ** and send them to the callback one by one.
! 98817: */
! 98818: if( pOrderBy ){
! 98819: explainTempTable(pParse, "ORDER BY");
! 98820: generateSortTail(pParse, p, v, pEList->nExpr, pDest);
! 98821: }
! 98822:
! 98823: /* Jump here to skip this query
! 98824: */
! 98825: sqlite3VdbeResolveLabel(v, iEnd);
! 98826:
! 98827: /* The SELECT was successfully coded. Set the return code to 0
! 98828: ** to indicate no errors.
! 98829: */
! 98830: rc = 0;
! 98831:
! 98832: /* Control jumps to here if an error is encountered above, or upon
! 98833: ** successful coding of the SELECT.
! 98834: */
! 98835: select_end:
! 98836: explainSetInteger(pParse->iSelectId, iRestoreSelectId);
! 98837:
! 98838: /* Identify column names if results of the SELECT are to be output.
! 98839: */
! 98840: if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
! 98841: generateColumnNames(pParse, pTabList, pEList);
! 98842: }
! 98843:
! 98844: sqlite3DbFree(db, sAggInfo.aCol);
! 98845: sqlite3DbFree(db, sAggInfo.aFunc);
! 98846: return rc;
! 98847: }
! 98848:
! 98849: #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
! 98850: /*
! 98851: ** Generate a human-readable description of a the Select object.
! 98852: */
! 98853: static void explainOneSelect(Vdbe *pVdbe, Select *p){
! 98854: sqlite3ExplainPrintf(pVdbe, "SELECT ");
! 98855: if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
! 98856: if( p->selFlags & SF_Distinct ){
! 98857: sqlite3ExplainPrintf(pVdbe, "DISTINCT ");
! 98858: }
! 98859: if( p->selFlags & SF_Aggregate ){
! 98860: sqlite3ExplainPrintf(pVdbe, "agg_flag ");
! 98861: }
! 98862: sqlite3ExplainNL(pVdbe);
! 98863: sqlite3ExplainPrintf(pVdbe, " ");
! 98864: }
! 98865: sqlite3ExplainExprList(pVdbe, p->pEList);
! 98866: sqlite3ExplainNL(pVdbe);
! 98867: if( p->pSrc && p->pSrc->nSrc ){
! 98868: int i;
! 98869: sqlite3ExplainPrintf(pVdbe, "FROM ");
! 98870: sqlite3ExplainPush(pVdbe);
! 98871: for(i=0; i<p->pSrc->nSrc; i++){
! 98872: struct SrcList_item *pItem = &p->pSrc->a[i];
! 98873: sqlite3ExplainPrintf(pVdbe, "{%d,*} = ", pItem->iCursor);
! 98874: if( pItem->pSelect ){
! 98875: sqlite3ExplainSelect(pVdbe, pItem->pSelect);
! 98876: if( pItem->pTab ){
! 98877: sqlite3ExplainPrintf(pVdbe, " (tabname=%s)", pItem->pTab->zName);
! 98878: }
! 98879: }else if( pItem->zName ){
! 98880: sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName);
! 98881: }
! 98882: if( pItem->zAlias ){
! 98883: sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias);
! 98884: }
! 98885: if( pItem->jointype & JT_LEFT ){
! 98886: sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN");
! 98887: }
! 98888: sqlite3ExplainNL(pVdbe);
! 98889: }
! 98890: sqlite3ExplainPop(pVdbe);
! 98891: }
! 98892: if( p->pWhere ){
! 98893: sqlite3ExplainPrintf(pVdbe, "WHERE ");
! 98894: sqlite3ExplainExpr(pVdbe, p->pWhere);
! 98895: sqlite3ExplainNL(pVdbe);
! 98896: }
! 98897: if( p->pGroupBy ){
! 98898: sqlite3ExplainPrintf(pVdbe, "GROUPBY ");
! 98899: sqlite3ExplainExprList(pVdbe, p->pGroupBy);
! 98900: sqlite3ExplainNL(pVdbe);
! 98901: }
! 98902: if( p->pHaving ){
! 98903: sqlite3ExplainPrintf(pVdbe, "HAVING ");
! 98904: sqlite3ExplainExpr(pVdbe, p->pHaving);
! 98905: sqlite3ExplainNL(pVdbe);
! 98906: }
! 98907: if( p->pOrderBy ){
! 98908: sqlite3ExplainPrintf(pVdbe, "ORDERBY ");
! 98909: sqlite3ExplainExprList(pVdbe, p->pOrderBy);
! 98910: sqlite3ExplainNL(pVdbe);
! 98911: }
! 98912: if( p->pLimit ){
! 98913: sqlite3ExplainPrintf(pVdbe, "LIMIT ");
! 98914: sqlite3ExplainExpr(pVdbe, p->pLimit);
! 98915: sqlite3ExplainNL(pVdbe);
! 98916: }
! 98917: if( p->pOffset ){
! 98918: sqlite3ExplainPrintf(pVdbe, "OFFSET ");
! 98919: sqlite3ExplainExpr(pVdbe, p->pOffset);
! 98920: sqlite3ExplainNL(pVdbe);
! 98921: }
! 98922: }
! 98923: SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
! 98924: if( p==0 ){
! 98925: sqlite3ExplainPrintf(pVdbe, "(null-select)");
! 98926: return;
! 98927: }
! 98928: while( p->pPrior ) p = p->pPrior;
! 98929: sqlite3ExplainPush(pVdbe);
! 98930: while( p ){
! 98931: explainOneSelect(pVdbe, p);
! 98932: p = p->pNext;
! 98933: if( p==0 ) break;
! 98934: sqlite3ExplainNL(pVdbe);
! 98935: sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op));
! 98936: }
! 98937: sqlite3ExplainPrintf(pVdbe, "END");
! 98938: sqlite3ExplainPop(pVdbe);
! 98939: }
! 98940:
! 98941: /* End of the structure debug printing code
! 98942: *****************************************************************************/
! 98943: #endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
! 98944:
! 98945: /************** End of select.c **********************************************/
! 98946: /************** Begin file table.c *******************************************/
! 98947: /*
! 98948: ** 2001 September 15
! 98949: **
! 98950: ** The author disclaims copyright to this source code. In place of
! 98951: ** a legal notice, here is a blessing:
! 98952: **
! 98953: ** May you do good and not evil.
! 98954: ** May you find forgiveness for yourself and forgive others.
! 98955: ** May you share freely, never taking more than you give.
! 98956: **
! 98957: *************************************************************************
! 98958: ** This file contains the sqlite3_get_table() and sqlite3_free_table()
! 98959: ** interface routines. These are just wrappers around the main
! 98960: ** interface routine of sqlite3_exec().
! 98961: **
! 98962: ** These routines are in a separate files so that they will not be linked
! 98963: ** if they are not used.
! 98964: */
! 98965: /* #include <stdlib.h> */
! 98966: /* #include <string.h> */
! 98967:
! 98968: #ifndef SQLITE_OMIT_GET_TABLE
! 98969:
! 98970: /*
! 98971: ** This structure is used to pass data from sqlite3_get_table() through
! 98972: ** to the callback function is uses to build the result.
! 98973: */
! 98974: typedef struct TabResult {
! 98975: char **azResult; /* Accumulated output */
! 98976: char *zErrMsg; /* Error message text, if an error occurs */
! 98977: int nAlloc; /* Slots allocated for azResult[] */
! 98978: int nRow; /* Number of rows in the result */
! 98979: int nColumn; /* Number of columns in the result */
! 98980: int nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
! 98981: int rc; /* Return code from sqlite3_exec() */
! 98982: } TabResult;
! 98983:
! 98984: /*
! 98985: ** This routine is called once for each row in the result table. Its job
! 98986: ** is to fill in the TabResult structure appropriately, allocating new
! 98987: ** memory as necessary.
! 98988: */
! 98989: static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
! 98990: TabResult *p = (TabResult*)pArg; /* Result accumulator */
! 98991: int need; /* Slots needed in p->azResult[] */
! 98992: int i; /* Loop counter */
! 98993: char *z; /* A single column of result */
! 98994:
! 98995: /* Make sure there is enough space in p->azResult to hold everything
! 98996: ** we need to remember from this invocation of the callback.
! 98997: */
! 98998: if( p->nRow==0 && argv!=0 ){
! 98999: need = nCol*2;
! 99000: }else{
! 99001: need = nCol;
! 99002: }
! 99003: if( p->nData + need > p->nAlloc ){
! 99004: char **azNew;
! 99005: p->nAlloc = p->nAlloc*2 + need;
! 99006: azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
! 99007: if( azNew==0 ) goto malloc_failed;
! 99008: p->azResult = azNew;
! 99009: }
! 99010:
! 99011: /* If this is the first row, then generate an extra row containing
! 99012: ** the names of all columns.
! 99013: */
! 99014: if( p->nRow==0 ){
! 99015: p->nColumn = nCol;
! 99016: for(i=0; i<nCol; i++){
! 99017: z = sqlite3_mprintf("%s", colv[i]);
! 99018: if( z==0 ) goto malloc_failed;
! 99019: p->azResult[p->nData++] = z;
! 99020: }
! 99021: }else if( p->nColumn!=nCol ){
! 99022: sqlite3_free(p->zErrMsg);
! 99023: p->zErrMsg = sqlite3_mprintf(
! 99024: "sqlite3_get_table() called with two or more incompatible queries"
! 99025: );
! 99026: p->rc = SQLITE_ERROR;
! 99027: return 1;
! 99028: }
! 99029:
! 99030: /* Copy over the row data
! 99031: */
! 99032: if( argv!=0 ){
! 99033: for(i=0; i<nCol; i++){
! 99034: if( argv[i]==0 ){
! 99035: z = 0;
! 99036: }else{
! 99037: int n = sqlite3Strlen30(argv[i])+1;
! 99038: z = sqlite3_malloc( n );
! 99039: if( z==0 ) goto malloc_failed;
! 99040: memcpy(z, argv[i], n);
! 99041: }
! 99042: p->azResult[p->nData++] = z;
! 99043: }
! 99044: p->nRow++;
! 99045: }
! 99046: return 0;
! 99047:
! 99048: malloc_failed:
! 99049: p->rc = SQLITE_NOMEM;
! 99050: return 1;
! 99051: }
! 99052:
! 99053: /*
! 99054: ** Query the database. But instead of invoking a callback for each row,
! 99055: ** malloc() for space to hold the result and return the entire results
! 99056: ** at the conclusion of the call.
! 99057: **
! 99058: ** The result that is written to ***pazResult is held in memory obtained
! 99059: ** from malloc(). But the caller cannot free this memory directly.
! 99060: ** Instead, the entire table should be passed to sqlite3_free_table() when
! 99061: ** the calling procedure is finished using it.
! 99062: */
! 99063: SQLITE_API int sqlite3_get_table(
! 99064: sqlite3 *db, /* The database on which the SQL executes */
! 99065: const char *zSql, /* The SQL to be executed */
! 99066: char ***pazResult, /* Write the result table here */
! 99067: int *pnRow, /* Write the number of rows in the result here */
! 99068: int *pnColumn, /* Write the number of columns of result here */
! 99069: char **pzErrMsg /* Write error messages here */
! 99070: ){
! 99071: int rc;
! 99072: TabResult res;
! 99073:
! 99074: *pazResult = 0;
! 99075: if( pnColumn ) *pnColumn = 0;
! 99076: if( pnRow ) *pnRow = 0;
! 99077: if( pzErrMsg ) *pzErrMsg = 0;
! 99078: res.zErrMsg = 0;
! 99079: res.nRow = 0;
! 99080: res.nColumn = 0;
! 99081: res.nData = 1;
! 99082: res.nAlloc = 20;
! 99083: res.rc = SQLITE_OK;
! 99084: res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
! 99085: if( res.azResult==0 ){
! 99086: db->errCode = SQLITE_NOMEM;
! 99087: return SQLITE_NOMEM;
! 99088: }
! 99089: res.azResult[0] = 0;
! 99090: rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
! 99091: assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
! 99092: res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
! 99093: if( (rc&0xff)==SQLITE_ABORT ){
! 99094: sqlite3_free_table(&res.azResult[1]);
! 99095: if( res.zErrMsg ){
! 99096: if( pzErrMsg ){
! 99097: sqlite3_free(*pzErrMsg);
! 99098: *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
! 99099: }
! 99100: sqlite3_free(res.zErrMsg);
! 99101: }
! 99102: db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
! 99103: return res.rc;
! 99104: }
! 99105: sqlite3_free(res.zErrMsg);
! 99106: if( rc!=SQLITE_OK ){
! 99107: sqlite3_free_table(&res.azResult[1]);
! 99108: return rc;
! 99109: }
! 99110: if( res.nAlloc>res.nData ){
! 99111: char **azNew;
! 99112: azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
! 99113: if( azNew==0 ){
! 99114: sqlite3_free_table(&res.azResult[1]);
! 99115: db->errCode = SQLITE_NOMEM;
! 99116: return SQLITE_NOMEM;
! 99117: }
! 99118: res.azResult = azNew;
! 99119: }
! 99120: *pazResult = &res.azResult[1];
! 99121: if( pnColumn ) *pnColumn = res.nColumn;
! 99122: if( pnRow ) *pnRow = res.nRow;
! 99123: return rc;
! 99124: }
! 99125:
! 99126: /*
! 99127: ** This routine frees the space the sqlite3_get_table() malloced.
! 99128: */
! 99129: SQLITE_API void sqlite3_free_table(
! 99130: char **azResult /* Result returned from from sqlite3_get_table() */
! 99131: ){
! 99132: if( azResult ){
! 99133: int i, n;
! 99134: azResult--;
! 99135: assert( azResult!=0 );
! 99136: n = SQLITE_PTR_TO_INT(azResult[0]);
! 99137: for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
! 99138: sqlite3_free(azResult);
! 99139: }
! 99140: }
! 99141:
! 99142: #endif /* SQLITE_OMIT_GET_TABLE */
! 99143:
! 99144: /************** End of table.c ***********************************************/
! 99145: /************** Begin file trigger.c *****************************************/
! 99146: /*
! 99147: **
! 99148: ** The author disclaims copyright to this source code. In place of
! 99149: ** a legal notice, here is a blessing:
! 99150: **
! 99151: ** May you do good and not evil.
! 99152: ** May you find forgiveness for yourself and forgive others.
! 99153: ** May you share freely, never taking more than you give.
! 99154: **
! 99155: *************************************************************************
! 99156: ** This file contains the implementation for TRIGGERs
! 99157: */
! 99158:
! 99159: #ifndef SQLITE_OMIT_TRIGGER
! 99160: /*
! 99161: ** Delete a linked list of TriggerStep structures.
! 99162: */
! 99163: SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
! 99164: while( pTriggerStep ){
! 99165: TriggerStep * pTmp = pTriggerStep;
! 99166: pTriggerStep = pTriggerStep->pNext;
! 99167:
! 99168: sqlite3ExprDelete(db, pTmp->pWhere);
! 99169: sqlite3ExprListDelete(db, pTmp->pExprList);
! 99170: sqlite3SelectDelete(db, pTmp->pSelect);
! 99171: sqlite3IdListDelete(db, pTmp->pIdList);
! 99172:
! 99173: sqlite3DbFree(db, pTmp);
! 99174: }
! 99175: }
! 99176:
! 99177: /*
! 99178: ** Given table pTab, return a list of all the triggers attached to
! 99179: ** the table. The list is connected by Trigger.pNext pointers.
! 99180: **
! 99181: ** All of the triggers on pTab that are in the same database as pTab
! 99182: ** are already attached to pTab->pTrigger. But there might be additional
! 99183: ** triggers on pTab in the TEMP schema. This routine prepends all
! 99184: ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
! 99185: ** and returns the combined list.
! 99186: **
! 99187: ** To state it another way: This routine returns a list of all triggers
! 99188: ** that fire off of pTab. The list will include any TEMP triggers on
! 99189: ** pTab as well as the triggers lised in pTab->pTrigger.
! 99190: */
! 99191: SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
! 99192: Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
! 99193: Trigger *pList = 0; /* List of triggers to return */
! 99194:
! 99195: if( pParse->disableTriggers ){
! 99196: return 0;
! 99197: }
! 99198:
! 99199: if( pTmpSchema!=pTab->pSchema ){
! 99200: HashElem *p;
! 99201: assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
! 99202: for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
! 99203: Trigger *pTrig = (Trigger *)sqliteHashData(p);
! 99204: if( pTrig->pTabSchema==pTab->pSchema
! 99205: && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
! 99206: ){
! 99207: pTrig->pNext = (pList ? pList : pTab->pTrigger);
! 99208: pList = pTrig;
! 99209: }
! 99210: }
! 99211: }
! 99212:
! 99213: return (pList ? pList : pTab->pTrigger);
! 99214: }
! 99215:
! 99216: /*
! 99217: ** This is called by the parser when it sees a CREATE TRIGGER statement
! 99218: ** up to the point of the BEGIN before the trigger actions. A Trigger
! 99219: ** structure is generated based on the information available and stored
! 99220: ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
! 99221: ** sqlite3FinishTrigger() function is called to complete the trigger
! 99222: ** construction process.
! 99223: */
! 99224: SQLITE_PRIVATE void sqlite3BeginTrigger(
! 99225: Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
! 99226: Token *pName1, /* The name of the trigger */
! 99227: Token *pName2, /* The name of the trigger */
! 99228: int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
! 99229: int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
! 99230: IdList *pColumns, /* column list if this is an UPDATE OF trigger */
! 99231: SrcList *pTableName,/* The name of the table/view the trigger applies to */
! 99232: Expr *pWhen, /* WHEN clause */
! 99233: int isTemp, /* True if the TEMPORARY keyword is present */
! 99234: int noErr /* Suppress errors if the trigger already exists */
! 99235: ){
! 99236: Trigger *pTrigger = 0; /* The new trigger */
! 99237: Table *pTab; /* Table that the trigger fires off of */
! 99238: char *zName = 0; /* Name of the trigger */
! 99239: sqlite3 *db = pParse->db; /* The database connection */
! 99240: int iDb; /* The database to store the trigger in */
! 99241: Token *pName; /* The unqualified db name */
! 99242: DbFixer sFix; /* State vector for the DB fixer */
! 99243: int iTabDb; /* Index of the database holding pTab */
! 99244:
! 99245: assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
! 99246: assert( pName2!=0 );
! 99247: assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
! 99248: assert( op>0 && op<0xff );
! 99249: if( isTemp ){
! 99250: /* If TEMP was specified, then the trigger name may not be qualified. */
! 99251: if( pName2->n>0 ){
! 99252: sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
! 99253: goto trigger_cleanup;
! 99254: }
! 99255: iDb = 1;
! 99256: pName = pName1;
! 99257: }else{
! 99258: /* Figure out the db that the the trigger will be created in */
! 99259: iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
! 99260: if( iDb<0 ){
! 99261: goto trigger_cleanup;
! 99262: }
! 99263: }
! 99264: if( !pTableName || db->mallocFailed ){
! 99265: goto trigger_cleanup;
! 99266: }
! 99267:
! 99268: /* A long-standing parser bug is that this syntax was allowed:
! 99269: **
! 99270: ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
! 99271: ** ^^^^^^^^
! 99272: **
! 99273: ** To maintain backwards compatibility, ignore the database
! 99274: ** name on pTableName if we are reparsing our of SQLITE_MASTER.
! 99275: */
! 99276: if( db->init.busy && iDb!=1 ){
! 99277: sqlite3DbFree(db, pTableName->a[0].zDatabase);
! 99278: pTableName->a[0].zDatabase = 0;
! 99279: }
! 99280:
! 99281: /* If the trigger name was unqualified, and the table is a temp table,
! 99282: ** then set iDb to 1 to create the trigger in the temporary database.
! 99283: ** If sqlite3SrcListLookup() returns 0, indicating the table does not
! 99284: ** exist, the error is caught by the block below.
! 99285: */
! 99286: pTab = sqlite3SrcListLookup(pParse, pTableName);
! 99287: if( db->init.busy==0 && pName2->n==0 && pTab
! 99288: && pTab->pSchema==db->aDb[1].pSchema ){
! 99289: iDb = 1;
! 99290: }
! 99291:
! 99292: /* Ensure the table name matches database name and that the table exists */
! 99293: if( db->mallocFailed ) goto trigger_cleanup;
! 99294: assert( pTableName->nSrc==1 );
! 99295: if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
! 99296: sqlite3FixSrcList(&sFix, pTableName) ){
! 99297: goto trigger_cleanup;
! 99298: }
! 99299: pTab = sqlite3SrcListLookup(pParse, pTableName);
! 99300: if( !pTab ){
! 99301: /* The table does not exist. */
! 99302: if( db->init.iDb==1 ){
! 99303: /* Ticket #3810.
! 99304: ** Normally, whenever a table is dropped, all associated triggers are
! 99305: ** dropped too. But if a TEMP trigger is created on a non-TEMP table
! 99306: ** and the table is dropped by a different database connection, the
! 99307: ** trigger is not visible to the database connection that does the
! 99308: ** drop so the trigger cannot be dropped. This results in an
! 99309: ** "orphaned trigger" - a trigger whose associated table is missing.
! 99310: */
! 99311: db->init.orphanTrigger = 1;
! 99312: }
! 99313: goto trigger_cleanup;
! 99314: }
! 99315: if( IsVirtual(pTab) ){
! 99316: sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
! 99317: goto trigger_cleanup;
! 99318: }
! 99319:
! 99320: /* Check that the trigger name is not reserved and that no trigger of the
! 99321: ** specified name exists */
! 99322: zName = sqlite3NameFromToken(db, pName);
! 99323: if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
! 99324: goto trigger_cleanup;
! 99325: }
! 99326: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 99327: if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
! 99328: zName, sqlite3Strlen30(zName)) ){
! 99329: if( !noErr ){
! 99330: sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
! 99331: }else{
! 99332: assert( !db->init.busy );
! 99333: sqlite3CodeVerifySchema(pParse, iDb);
! 99334: }
! 99335: goto trigger_cleanup;
! 99336: }
! 99337:
! 99338: /* Do not create a trigger on a system table */
! 99339: if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
! 99340: sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
! 99341: pParse->nErr++;
! 99342: goto trigger_cleanup;
! 99343: }
! 99344:
! 99345: /* INSTEAD of triggers are only for views and views only support INSTEAD
! 99346: ** of triggers.
! 99347: */
! 99348: if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
! 99349: sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
! 99350: (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
! 99351: goto trigger_cleanup;
! 99352: }
! 99353: if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
! 99354: sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
! 99355: " trigger on table: %S", pTableName, 0);
! 99356: goto trigger_cleanup;
! 99357: }
! 99358: iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 99359:
! 99360: #ifndef SQLITE_OMIT_AUTHORIZATION
! 99361: {
! 99362: int code = SQLITE_CREATE_TRIGGER;
! 99363: const char *zDb = db->aDb[iTabDb].zName;
! 99364: const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
! 99365: if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
! 99366: if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
! 99367: goto trigger_cleanup;
! 99368: }
! 99369: if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
! 99370: goto trigger_cleanup;
! 99371: }
! 99372: }
! 99373: #endif
! 99374:
! 99375: /* INSTEAD OF triggers can only appear on views and BEFORE triggers
! 99376: ** cannot appear on views. So we might as well translate every
! 99377: ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
! 99378: ** elsewhere.
! 99379: */
! 99380: if (tr_tm == TK_INSTEAD){
! 99381: tr_tm = TK_BEFORE;
! 99382: }
! 99383:
! 99384: /* Build the Trigger object */
! 99385: pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
! 99386: if( pTrigger==0 ) goto trigger_cleanup;
! 99387: pTrigger->zName = zName;
! 99388: zName = 0;
! 99389: pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
! 99390: pTrigger->pSchema = db->aDb[iDb].pSchema;
! 99391: pTrigger->pTabSchema = pTab->pSchema;
! 99392: pTrigger->op = (u8)op;
! 99393: pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
! 99394: pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
! 99395: pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
! 99396: assert( pParse->pNewTrigger==0 );
! 99397: pParse->pNewTrigger = pTrigger;
! 99398:
! 99399: trigger_cleanup:
! 99400: sqlite3DbFree(db, zName);
! 99401: sqlite3SrcListDelete(db, pTableName);
! 99402: sqlite3IdListDelete(db, pColumns);
! 99403: sqlite3ExprDelete(db, pWhen);
! 99404: if( !pParse->pNewTrigger ){
! 99405: sqlite3DeleteTrigger(db, pTrigger);
! 99406: }else{
! 99407: assert( pParse->pNewTrigger==pTrigger );
! 99408: }
! 99409: }
! 99410:
! 99411: /*
! 99412: ** This routine is called after all of the trigger actions have been parsed
! 99413: ** in order to complete the process of building the trigger.
! 99414: */
! 99415: SQLITE_PRIVATE void sqlite3FinishTrigger(
! 99416: Parse *pParse, /* Parser context */
! 99417: TriggerStep *pStepList, /* The triggered program */
! 99418: Token *pAll /* Token that describes the complete CREATE TRIGGER */
! 99419: ){
! 99420: Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
! 99421: char *zName; /* Name of trigger */
! 99422: sqlite3 *db = pParse->db; /* The database */
! 99423: DbFixer sFix; /* Fixer object */
! 99424: int iDb; /* Database containing the trigger */
! 99425: Token nameToken; /* Trigger name for error reporting */
! 99426:
! 99427: pParse->pNewTrigger = 0;
! 99428: if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
! 99429: zName = pTrig->zName;
! 99430: iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
! 99431: pTrig->step_list = pStepList;
! 99432: while( pStepList ){
! 99433: pStepList->pTrig = pTrig;
! 99434: pStepList = pStepList->pNext;
! 99435: }
! 99436: nameToken.z = pTrig->zName;
! 99437: nameToken.n = sqlite3Strlen30(nameToken.z);
! 99438: if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken)
! 99439: && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
! 99440: goto triggerfinish_cleanup;
! 99441: }
! 99442:
! 99443: /* if we are not initializing,
! 99444: ** build the sqlite_master entry
! 99445: */
! 99446: if( !db->init.busy ){
! 99447: Vdbe *v;
! 99448: char *z;
! 99449:
! 99450: /* Make an entry in the sqlite_master table */
! 99451: v = sqlite3GetVdbe(pParse);
! 99452: if( v==0 ) goto triggerfinish_cleanup;
! 99453: sqlite3BeginWriteOperation(pParse, 0, iDb);
! 99454: z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
! 99455: sqlite3NestedParse(pParse,
! 99456: "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
! 99457: db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
! 99458: pTrig->table, z);
! 99459: sqlite3DbFree(db, z);
! 99460: sqlite3ChangeCookie(pParse, iDb);
! 99461: sqlite3VdbeAddParseSchemaOp(v, iDb,
! 99462: sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
! 99463: }
! 99464:
! 99465: if( db->init.busy ){
! 99466: Trigger *pLink = pTrig;
! 99467: Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
! 99468: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 99469: pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
! 99470: if( pTrig ){
! 99471: db->mallocFailed = 1;
! 99472: }else if( pLink->pSchema==pLink->pTabSchema ){
! 99473: Table *pTab;
! 99474: int n = sqlite3Strlen30(pLink->table);
! 99475: pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
! 99476: assert( pTab!=0 );
! 99477: pLink->pNext = pTab->pTrigger;
! 99478: pTab->pTrigger = pLink;
! 99479: }
! 99480: }
! 99481:
! 99482: triggerfinish_cleanup:
! 99483: sqlite3DeleteTrigger(db, pTrig);
! 99484: assert( !pParse->pNewTrigger );
! 99485: sqlite3DeleteTriggerStep(db, pStepList);
! 99486: }
! 99487:
! 99488: /*
! 99489: ** Turn a SELECT statement (that the pSelect parameter points to) into
! 99490: ** a trigger step. Return a pointer to a TriggerStep structure.
! 99491: **
! 99492: ** The parser calls this routine when it finds a SELECT statement in
! 99493: ** body of a TRIGGER.
! 99494: */
! 99495: SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
! 99496: TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
! 99497: if( pTriggerStep==0 ) {
! 99498: sqlite3SelectDelete(db, pSelect);
! 99499: return 0;
! 99500: }
! 99501: pTriggerStep->op = TK_SELECT;
! 99502: pTriggerStep->pSelect = pSelect;
! 99503: pTriggerStep->orconf = OE_Default;
! 99504: return pTriggerStep;
! 99505: }
! 99506:
! 99507: /*
! 99508: ** Allocate space to hold a new trigger step. The allocated space
! 99509: ** holds both the TriggerStep object and the TriggerStep.target.z string.
! 99510: **
! 99511: ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
! 99512: */
! 99513: static TriggerStep *triggerStepAllocate(
! 99514: sqlite3 *db, /* Database connection */
! 99515: u8 op, /* Trigger opcode */
! 99516: Token *pName /* The target name */
! 99517: ){
! 99518: TriggerStep *pTriggerStep;
! 99519:
! 99520: pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
! 99521: if( pTriggerStep ){
! 99522: char *z = (char*)&pTriggerStep[1];
! 99523: memcpy(z, pName->z, pName->n);
! 99524: pTriggerStep->target.z = z;
! 99525: pTriggerStep->target.n = pName->n;
! 99526: pTriggerStep->op = op;
! 99527: }
! 99528: return pTriggerStep;
! 99529: }
! 99530:
! 99531: /*
! 99532: ** Build a trigger step out of an INSERT statement. Return a pointer
! 99533: ** to the new trigger step.
! 99534: **
! 99535: ** The parser calls this routine when it sees an INSERT inside the
! 99536: ** body of a trigger.
! 99537: */
! 99538: SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
! 99539: sqlite3 *db, /* The database connection */
! 99540: Token *pTableName, /* Name of the table into which we insert */
! 99541: IdList *pColumn, /* List of columns in pTableName to insert into */
! 99542: ExprList *pEList, /* The VALUE clause: a list of values to be inserted */
! 99543: Select *pSelect, /* A SELECT statement that supplies values */
! 99544: u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
! 99545: ){
! 99546: TriggerStep *pTriggerStep;
! 99547:
! 99548: assert(pEList == 0 || pSelect == 0);
! 99549: assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
! 99550:
! 99551: pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
! 99552: if( pTriggerStep ){
! 99553: pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
! 99554: pTriggerStep->pIdList = pColumn;
! 99555: pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
! 99556: pTriggerStep->orconf = orconf;
! 99557: }else{
! 99558: sqlite3IdListDelete(db, pColumn);
! 99559: }
! 99560: sqlite3ExprListDelete(db, pEList);
! 99561: sqlite3SelectDelete(db, pSelect);
! 99562:
! 99563: return pTriggerStep;
! 99564: }
! 99565:
! 99566: /*
! 99567: ** Construct a trigger step that implements an UPDATE statement and return
! 99568: ** a pointer to that trigger step. The parser calls this routine when it
! 99569: ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
! 99570: */
! 99571: SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
! 99572: sqlite3 *db, /* The database connection */
! 99573: Token *pTableName, /* Name of the table to be updated */
! 99574: ExprList *pEList, /* The SET clause: list of column and new values */
! 99575: Expr *pWhere, /* The WHERE clause */
! 99576: u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
! 99577: ){
! 99578: TriggerStep *pTriggerStep;
! 99579:
! 99580: pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
! 99581: if( pTriggerStep ){
! 99582: pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
! 99583: pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
! 99584: pTriggerStep->orconf = orconf;
! 99585: }
! 99586: sqlite3ExprListDelete(db, pEList);
! 99587: sqlite3ExprDelete(db, pWhere);
! 99588: return pTriggerStep;
! 99589: }
! 99590:
! 99591: /*
! 99592: ** Construct a trigger step that implements a DELETE statement and return
! 99593: ** a pointer to that trigger step. The parser calls this routine when it
! 99594: ** sees a DELETE statement inside the body of a CREATE TRIGGER.
! 99595: */
! 99596: SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
! 99597: sqlite3 *db, /* Database connection */
! 99598: Token *pTableName, /* The table from which rows are deleted */
! 99599: Expr *pWhere /* The WHERE clause */
! 99600: ){
! 99601: TriggerStep *pTriggerStep;
! 99602:
! 99603: pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
! 99604: if( pTriggerStep ){
! 99605: pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
! 99606: pTriggerStep->orconf = OE_Default;
! 99607: }
! 99608: sqlite3ExprDelete(db, pWhere);
! 99609: return pTriggerStep;
! 99610: }
! 99611:
! 99612: /*
! 99613: ** Recursively delete a Trigger structure
! 99614: */
! 99615: SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
! 99616: if( pTrigger==0 ) return;
! 99617: sqlite3DeleteTriggerStep(db, pTrigger->step_list);
! 99618: sqlite3DbFree(db, pTrigger->zName);
! 99619: sqlite3DbFree(db, pTrigger->table);
! 99620: sqlite3ExprDelete(db, pTrigger->pWhen);
! 99621: sqlite3IdListDelete(db, pTrigger->pColumns);
! 99622: sqlite3DbFree(db, pTrigger);
! 99623: }
! 99624:
! 99625: /*
! 99626: ** This function is called to drop a trigger from the database schema.
! 99627: **
! 99628: ** This may be called directly from the parser and therefore identifies
! 99629: ** the trigger by name. The sqlite3DropTriggerPtr() routine does the
! 99630: ** same job as this routine except it takes a pointer to the trigger
! 99631: ** instead of the trigger name.
! 99632: **/
! 99633: SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
! 99634: Trigger *pTrigger = 0;
! 99635: int i;
! 99636: const char *zDb;
! 99637: const char *zName;
! 99638: int nName;
! 99639: sqlite3 *db = pParse->db;
! 99640:
! 99641: if( db->mallocFailed ) goto drop_trigger_cleanup;
! 99642: if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
! 99643: goto drop_trigger_cleanup;
! 99644: }
! 99645:
! 99646: assert( pName->nSrc==1 );
! 99647: zDb = pName->a[0].zDatabase;
! 99648: zName = pName->a[0].zName;
! 99649: nName = sqlite3Strlen30(zName);
! 99650: assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
! 99651: for(i=OMIT_TEMPDB; i<db->nDb; i++){
! 99652: int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
! 99653: if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
! 99654: assert( sqlite3SchemaMutexHeld(db, j, 0) );
! 99655: pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
! 99656: if( pTrigger ) break;
! 99657: }
! 99658: if( !pTrigger ){
! 99659: if( !noErr ){
! 99660: sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
! 99661: }else{
! 99662: sqlite3CodeVerifyNamedSchema(pParse, zDb);
! 99663: }
! 99664: pParse->checkSchema = 1;
! 99665: goto drop_trigger_cleanup;
! 99666: }
! 99667: sqlite3DropTriggerPtr(pParse, pTrigger);
! 99668:
! 99669: drop_trigger_cleanup:
! 99670: sqlite3SrcListDelete(db, pName);
! 99671: }
! 99672:
! 99673: /*
! 99674: ** Return a pointer to the Table structure for the table that a trigger
! 99675: ** is set on.
! 99676: */
! 99677: static Table *tableOfTrigger(Trigger *pTrigger){
! 99678: int n = sqlite3Strlen30(pTrigger->table);
! 99679: return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
! 99680: }
! 99681:
! 99682:
! 99683: /*
! 99684: ** Drop a trigger given a pointer to that trigger.
! 99685: */
! 99686: SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
! 99687: Table *pTable;
! 99688: Vdbe *v;
! 99689: sqlite3 *db = pParse->db;
! 99690: int iDb;
! 99691:
! 99692: iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
! 99693: assert( iDb>=0 && iDb<db->nDb );
! 99694: pTable = tableOfTrigger(pTrigger);
! 99695: assert( pTable );
! 99696: assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
! 99697: #ifndef SQLITE_OMIT_AUTHORIZATION
! 99698: {
! 99699: int code = SQLITE_DROP_TRIGGER;
! 99700: const char *zDb = db->aDb[iDb].zName;
! 99701: const char *zTab = SCHEMA_TABLE(iDb);
! 99702: if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
! 99703: if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
! 99704: sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
! 99705: return;
! 99706: }
! 99707: }
! 99708: #endif
! 99709:
! 99710: /* Generate code to destroy the database record of the trigger.
! 99711: */
! 99712: assert( pTable!=0 );
! 99713: if( (v = sqlite3GetVdbe(pParse))!=0 ){
! 99714: int base;
! 99715: static const VdbeOpList dropTrigger[] = {
! 99716: { OP_Rewind, 0, ADDR(9), 0},
! 99717: { OP_String8, 0, 1, 0}, /* 1 */
! 99718: { OP_Column, 0, 1, 2},
! 99719: { OP_Ne, 2, ADDR(8), 1},
! 99720: { OP_String8, 0, 1, 0}, /* 4: "trigger" */
! 99721: { OP_Column, 0, 0, 2},
! 99722: { OP_Ne, 2, ADDR(8), 1},
! 99723: { OP_Delete, 0, 0, 0},
! 99724: { OP_Next, 0, ADDR(1), 0}, /* 8 */
! 99725: };
! 99726:
! 99727: sqlite3BeginWriteOperation(pParse, 0, iDb);
! 99728: sqlite3OpenMasterTable(pParse, iDb);
! 99729: base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger);
! 99730: sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
! 99731: sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
! 99732: sqlite3ChangeCookie(pParse, iDb);
! 99733: sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
! 99734: sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
! 99735: if( pParse->nMem<3 ){
! 99736: pParse->nMem = 3;
! 99737: }
! 99738: }
! 99739: }
! 99740:
! 99741: /*
! 99742: ** Remove a trigger from the hash tables of the sqlite* pointer.
! 99743: */
! 99744: SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
! 99745: Trigger *pTrigger;
! 99746: Hash *pHash;
! 99747:
! 99748: assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
! 99749: pHash = &(db->aDb[iDb].pSchema->trigHash);
! 99750: pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
! 99751: if( ALWAYS(pTrigger) ){
! 99752: if( pTrigger->pSchema==pTrigger->pTabSchema ){
! 99753: Table *pTab = tableOfTrigger(pTrigger);
! 99754: Trigger **pp;
! 99755: for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
! 99756: *pp = (*pp)->pNext;
! 99757: }
! 99758: sqlite3DeleteTrigger(db, pTrigger);
! 99759: db->flags |= SQLITE_InternChanges;
! 99760: }
! 99761: }
! 99762:
! 99763: /*
! 99764: ** pEList is the SET clause of an UPDATE statement. Each entry
! 99765: ** in pEList is of the format <id>=<expr>. If any of the entries
! 99766: ** in pEList have an <id> which matches an identifier in pIdList,
! 99767: ** then return TRUE. If pIdList==NULL, then it is considered a
! 99768: ** wildcard that matches anything. Likewise if pEList==NULL then
! 99769: ** it matches anything so always return true. Return false only
! 99770: ** if there is no match.
! 99771: */
! 99772: static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
! 99773: int e;
! 99774: if( pIdList==0 || NEVER(pEList==0) ) return 1;
! 99775: for(e=0; e<pEList->nExpr; e++){
! 99776: if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
! 99777: }
! 99778: return 0;
! 99779: }
! 99780:
! 99781: /*
! 99782: ** Return a list of all triggers on table pTab if there exists at least
! 99783: ** one trigger that must be fired when an operation of type 'op' is
! 99784: ** performed on the table, and, if that operation is an UPDATE, if at
! 99785: ** least one of the columns in pChanges is being modified.
! 99786: */
! 99787: SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
! 99788: Parse *pParse, /* Parse context */
! 99789: Table *pTab, /* The table the contains the triggers */
! 99790: int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
! 99791: ExprList *pChanges, /* Columns that change in an UPDATE statement */
! 99792: int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
! 99793: ){
! 99794: int mask = 0;
! 99795: Trigger *pList = 0;
! 99796: Trigger *p;
! 99797:
! 99798: if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
! 99799: pList = sqlite3TriggerList(pParse, pTab);
! 99800: }
! 99801: assert( pList==0 || IsVirtual(pTab)==0 );
! 99802: for(p=pList; p; p=p->pNext){
! 99803: if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
! 99804: mask |= p->tr_tm;
! 99805: }
! 99806: }
! 99807: if( pMask ){
! 99808: *pMask = mask;
! 99809: }
! 99810: return (mask ? pList : 0);
! 99811: }
! 99812:
! 99813: /*
! 99814: ** Convert the pStep->target token into a SrcList and return a pointer
! 99815: ** to that SrcList.
! 99816: **
! 99817: ** This routine adds a specific database name, if needed, to the target when
! 99818: ** forming the SrcList. This prevents a trigger in one database from
! 99819: ** referring to a target in another database. An exception is when the
! 99820: ** trigger is in TEMP in which case it can refer to any other database it
! 99821: ** wants.
! 99822: */
! 99823: static SrcList *targetSrcList(
! 99824: Parse *pParse, /* The parsing context */
! 99825: TriggerStep *pStep /* The trigger containing the target token */
! 99826: ){
! 99827: int iDb; /* Index of the database to use */
! 99828: SrcList *pSrc; /* SrcList to be returned */
! 99829:
! 99830: pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
! 99831: if( pSrc ){
! 99832: assert( pSrc->nSrc>0 );
! 99833: assert( pSrc->a!=0 );
! 99834: iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
! 99835: if( iDb==0 || iDb>=2 ){
! 99836: sqlite3 *db = pParse->db;
! 99837: assert( iDb<pParse->db->nDb );
! 99838: pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
! 99839: }
! 99840: }
! 99841: return pSrc;
! 99842: }
! 99843:
! 99844: /*
! 99845: ** Generate VDBE code for the statements inside the body of a single
! 99846: ** trigger.
! 99847: */
! 99848: static int codeTriggerProgram(
! 99849: Parse *pParse, /* The parser context */
! 99850: TriggerStep *pStepList, /* List of statements inside the trigger body */
! 99851: int orconf /* Conflict algorithm. (OE_Abort, etc) */
! 99852: ){
! 99853: TriggerStep *pStep;
! 99854: Vdbe *v = pParse->pVdbe;
! 99855: sqlite3 *db = pParse->db;
! 99856:
! 99857: assert( pParse->pTriggerTab && pParse->pToplevel );
! 99858: assert( pStepList );
! 99859: assert( v!=0 );
! 99860: for(pStep=pStepList; pStep; pStep=pStep->pNext){
! 99861: /* Figure out the ON CONFLICT policy that will be used for this step
! 99862: ** of the trigger program. If the statement that caused this trigger
! 99863: ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
! 99864: ** the ON CONFLICT policy that was specified as part of the trigger
! 99865: ** step statement. Example:
! 99866: **
! 99867: ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
! 99868: ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
! 99869: ** END;
! 99870: **
! 99871: ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
! 99872: ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
! 99873: */
! 99874: pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
! 99875:
! 99876: switch( pStep->op ){
! 99877: case TK_UPDATE: {
! 99878: sqlite3Update(pParse,
! 99879: targetSrcList(pParse, pStep),
! 99880: sqlite3ExprListDup(db, pStep->pExprList, 0),
! 99881: sqlite3ExprDup(db, pStep->pWhere, 0),
! 99882: pParse->eOrconf
! 99883: );
! 99884: break;
! 99885: }
! 99886: case TK_INSERT: {
! 99887: sqlite3Insert(pParse,
! 99888: targetSrcList(pParse, pStep),
! 99889: sqlite3ExprListDup(db, pStep->pExprList, 0),
! 99890: sqlite3SelectDup(db, pStep->pSelect, 0),
! 99891: sqlite3IdListDup(db, pStep->pIdList),
! 99892: pParse->eOrconf
! 99893: );
! 99894: break;
! 99895: }
! 99896: case TK_DELETE: {
! 99897: sqlite3DeleteFrom(pParse,
! 99898: targetSrcList(pParse, pStep),
! 99899: sqlite3ExprDup(db, pStep->pWhere, 0)
! 99900: );
! 99901: break;
! 99902: }
! 99903: default: assert( pStep->op==TK_SELECT ); {
! 99904: SelectDest sDest;
! 99905: Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
! 99906: sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
! 99907: sqlite3Select(pParse, pSelect, &sDest);
! 99908: sqlite3SelectDelete(db, pSelect);
! 99909: break;
! 99910: }
! 99911: }
! 99912: if( pStep->op!=TK_SELECT ){
! 99913: sqlite3VdbeAddOp0(v, OP_ResetCount);
! 99914: }
! 99915: }
! 99916:
! 99917: return 0;
! 99918: }
! 99919:
! 99920: #ifdef SQLITE_DEBUG
! 99921: /*
! 99922: ** This function is used to add VdbeComment() annotations to a VDBE
! 99923: ** program. It is not used in production code, only for debugging.
! 99924: */
! 99925: static const char *onErrorText(int onError){
! 99926: switch( onError ){
! 99927: case OE_Abort: return "abort";
! 99928: case OE_Rollback: return "rollback";
! 99929: case OE_Fail: return "fail";
! 99930: case OE_Replace: return "replace";
! 99931: case OE_Ignore: return "ignore";
! 99932: case OE_Default: return "default";
! 99933: }
! 99934: return "n/a";
! 99935: }
! 99936: #endif
! 99937:
! 99938: /*
! 99939: ** Parse context structure pFrom has just been used to create a sub-vdbe
! 99940: ** (trigger program). If an error has occurred, transfer error information
! 99941: ** from pFrom to pTo.
! 99942: */
! 99943: static void transferParseError(Parse *pTo, Parse *pFrom){
! 99944: assert( pFrom->zErrMsg==0 || pFrom->nErr );
! 99945: assert( pTo->zErrMsg==0 || pTo->nErr );
! 99946: if( pTo->nErr==0 ){
! 99947: pTo->zErrMsg = pFrom->zErrMsg;
! 99948: pTo->nErr = pFrom->nErr;
! 99949: }else{
! 99950: sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
! 99951: }
! 99952: }
! 99953:
! 99954: /*
! 99955: ** Create and populate a new TriggerPrg object with a sub-program
! 99956: ** implementing trigger pTrigger with ON CONFLICT policy orconf.
! 99957: */
! 99958: static TriggerPrg *codeRowTrigger(
! 99959: Parse *pParse, /* Current parse context */
! 99960: Trigger *pTrigger, /* Trigger to code */
! 99961: Table *pTab, /* The table pTrigger is attached to */
! 99962: int orconf /* ON CONFLICT policy to code trigger program with */
! 99963: ){
! 99964: Parse *pTop = sqlite3ParseToplevel(pParse);
! 99965: sqlite3 *db = pParse->db; /* Database handle */
! 99966: TriggerPrg *pPrg; /* Value to return */
! 99967: Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
! 99968: Vdbe *v; /* Temporary VM */
! 99969: NameContext sNC; /* Name context for sub-vdbe */
! 99970: SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
! 99971: Parse *pSubParse; /* Parse context for sub-vdbe */
! 99972: int iEndTrigger = 0; /* Label to jump to if WHEN is false */
! 99973:
! 99974: assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
! 99975: assert( pTop->pVdbe );
! 99976:
! 99977: /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
! 99978: ** are freed if an error occurs, link them into the Parse.pTriggerPrg
! 99979: ** list of the top-level Parse object sooner rather than later. */
! 99980: pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
! 99981: if( !pPrg ) return 0;
! 99982: pPrg->pNext = pTop->pTriggerPrg;
! 99983: pTop->pTriggerPrg = pPrg;
! 99984: pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
! 99985: if( !pProgram ) return 0;
! 99986: sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
! 99987: pPrg->pTrigger = pTrigger;
! 99988: pPrg->orconf = orconf;
! 99989: pPrg->aColmask[0] = 0xffffffff;
! 99990: pPrg->aColmask[1] = 0xffffffff;
! 99991:
! 99992: /* Allocate and populate a new Parse context to use for coding the
! 99993: ** trigger sub-program. */
! 99994: pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
! 99995: if( !pSubParse ) return 0;
! 99996: memset(&sNC, 0, sizeof(sNC));
! 99997: sNC.pParse = pSubParse;
! 99998: pSubParse->db = db;
! 99999: pSubParse->pTriggerTab = pTab;
! 100000: pSubParse->pToplevel = pTop;
! 100001: pSubParse->zAuthContext = pTrigger->zName;
! 100002: pSubParse->eTriggerOp = pTrigger->op;
! 100003: pSubParse->nQueryLoop = pParse->nQueryLoop;
! 100004:
! 100005: v = sqlite3GetVdbe(pSubParse);
! 100006: if( v ){
! 100007: VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
! 100008: pTrigger->zName, onErrorText(orconf),
! 100009: (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
! 100010: (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
! 100011: (pTrigger->op==TK_INSERT ? "INSERT" : ""),
! 100012: (pTrigger->op==TK_DELETE ? "DELETE" : ""),
! 100013: pTab->zName
! 100014: ));
! 100015: #ifndef SQLITE_OMIT_TRACE
! 100016: sqlite3VdbeChangeP4(v, -1,
! 100017: sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
! 100018: );
! 100019: #endif
! 100020:
! 100021: /* If one was specified, code the WHEN clause. If it evaluates to false
! 100022: ** (or NULL) the sub-vdbe is immediately halted by jumping to the
! 100023: ** OP_Halt inserted at the end of the program. */
! 100024: if( pTrigger->pWhen ){
! 100025: pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
! 100026: if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
! 100027: && db->mallocFailed==0
! 100028: ){
! 100029: iEndTrigger = sqlite3VdbeMakeLabel(v);
! 100030: sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
! 100031: }
! 100032: sqlite3ExprDelete(db, pWhen);
! 100033: }
! 100034:
! 100035: /* Code the trigger program into the sub-vdbe. */
! 100036: codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
! 100037:
! 100038: /* Insert an OP_Halt at the end of the sub-program. */
! 100039: if( iEndTrigger ){
! 100040: sqlite3VdbeResolveLabel(v, iEndTrigger);
! 100041: }
! 100042: sqlite3VdbeAddOp0(v, OP_Halt);
! 100043: VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
! 100044:
! 100045: transferParseError(pParse, pSubParse);
! 100046: if( db->mallocFailed==0 ){
! 100047: pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
! 100048: }
! 100049: pProgram->nMem = pSubParse->nMem;
! 100050: pProgram->nCsr = pSubParse->nTab;
! 100051: pProgram->nOnce = pSubParse->nOnce;
! 100052: pProgram->token = (void *)pTrigger;
! 100053: pPrg->aColmask[0] = pSubParse->oldmask;
! 100054: pPrg->aColmask[1] = pSubParse->newmask;
! 100055: sqlite3VdbeDelete(v);
! 100056: }
! 100057:
! 100058: assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
! 100059: assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
! 100060: sqlite3StackFree(db, pSubParse);
! 100061:
! 100062: return pPrg;
! 100063: }
! 100064:
! 100065: /*
! 100066: ** Return a pointer to a TriggerPrg object containing the sub-program for
! 100067: ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
! 100068: ** TriggerPrg object exists, a new object is allocated and populated before
! 100069: ** being returned.
! 100070: */
! 100071: static TriggerPrg *getRowTrigger(
! 100072: Parse *pParse, /* Current parse context */
! 100073: Trigger *pTrigger, /* Trigger to code */
! 100074: Table *pTab, /* The table trigger pTrigger is attached to */
! 100075: int orconf /* ON CONFLICT algorithm. */
! 100076: ){
! 100077: Parse *pRoot = sqlite3ParseToplevel(pParse);
! 100078: TriggerPrg *pPrg;
! 100079:
! 100080: assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
! 100081:
! 100082: /* It may be that this trigger has already been coded (or is in the
! 100083: ** process of being coded). If this is the case, then an entry with
! 100084: ** a matching TriggerPrg.pTrigger field will be present somewhere
! 100085: ** in the Parse.pTriggerPrg list. Search for such an entry. */
! 100086: for(pPrg=pRoot->pTriggerPrg;
! 100087: pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
! 100088: pPrg=pPrg->pNext
! 100089: );
! 100090:
! 100091: /* If an existing TriggerPrg could not be located, create a new one. */
! 100092: if( !pPrg ){
! 100093: pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
! 100094: }
! 100095:
! 100096: return pPrg;
! 100097: }
! 100098:
! 100099: /*
! 100100: ** Generate code for the trigger program associated with trigger p on
! 100101: ** table pTab. The reg, orconf and ignoreJump parameters passed to this
! 100102: ** function are the same as those described in the header function for
! 100103: ** sqlite3CodeRowTrigger()
! 100104: */
! 100105: SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
! 100106: Parse *pParse, /* Parse context */
! 100107: Trigger *p, /* Trigger to code */
! 100108: Table *pTab, /* The table to code triggers from */
! 100109: int reg, /* Reg array containing OLD.* and NEW.* values */
! 100110: int orconf, /* ON CONFLICT policy */
! 100111: int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
! 100112: ){
! 100113: Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
! 100114: TriggerPrg *pPrg;
! 100115: pPrg = getRowTrigger(pParse, p, pTab, orconf);
! 100116: assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
! 100117:
! 100118: /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
! 100119: ** is a pointer to the sub-vdbe containing the trigger program. */
! 100120: if( pPrg ){
! 100121: int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
! 100122:
! 100123: sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
! 100124: sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
! 100125: VdbeComment(
! 100126: (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
! 100127:
! 100128: /* Set the P5 operand of the OP_Program instruction to non-zero if
! 100129: ** recursive invocation of this trigger program is disallowed. Recursive
! 100130: ** invocation is disallowed if (a) the sub-program is really a trigger,
! 100131: ** not a foreign key action, and (b) the flag to enable recursive triggers
! 100132: ** is clear. */
! 100133: sqlite3VdbeChangeP5(v, (u8)bRecursive);
! 100134: }
! 100135: }
! 100136:
! 100137: /*
! 100138: ** This is called to code the required FOR EACH ROW triggers for an operation
! 100139: ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
! 100140: ** is given by the op paramater. The tr_tm parameter determines whether the
! 100141: ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
! 100142: ** parameter pChanges is passed the list of columns being modified.
! 100143: **
! 100144: ** If there are no triggers that fire at the specified time for the specified
! 100145: ** operation on pTab, this function is a no-op.
! 100146: **
! 100147: ** The reg argument is the address of the first in an array of registers
! 100148: ** that contain the values substituted for the new.* and old.* references
! 100149: ** in the trigger program. If N is the number of columns in table pTab
! 100150: ** (a copy of pTab->nCol), then registers are populated as follows:
! 100151: **
! 100152: ** Register Contains
! 100153: ** ------------------------------------------------------
! 100154: ** reg+0 OLD.rowid
! 100155: ** reg+1 OLD.* value of left-most column of pTab
! 100156: ** ... ...
! 100157: ** reg+N OLD.* value of right-most column of pTab
! 100158: ** reg+N+1 NEW.rowid
! 100159: ** reg+N+2 OLD.* value of left-most column of pTab
! 100160: ** ... ...
! 100161: ** reg+N+N+1 NEW.* value of right-most column of pTab
! 100162: **
! 100163: ** For ON DELETE triggers, the registers containing the NEW.* values will
! 100164: ** never be accessed by the trigger program, so they are not allocated or
! 100165: ** populated by the caller (there is no data to populate them with anyway).
! 100166: ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
! 100167: ** are never accessed, and so are not allocated by the caller. So, for an
! 100168: ** ON INSERT trigger, the value passed to this function as parameter reg
! 100169: ** is not a readable register, although registers (reg+N) through
! 100170: ** (reg+N+N+1) are.
! 100171: **
! 100172: ** Parameter orconf is the default conflict resolution algorithm for the
! 100173: ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
! 100174: ** is the instruction that control should jump to if a trigger program
! 100175: ** raises an IGNORE exception.
! 100176: */
! 100177: SQLITE_PRIVATE void sqlite3CodeRowTrigger(
! 100178: Parse *pParse, /* Parse context */
! 100179: Trigger *pTrigger, /* List of triggers on table pTab */
! 100180: int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
! 100181: ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
! 100182: int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
! 100183: Table *pTab, /* The table to code triggers from */
! 100184: int reg, /* The first in an array of registers (see above) */
! 100185: int orconf, /* ON CONFLICT policy */
! 100186: int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
! 100187: ){
! 100188: Trigger *p; /* Used to iterate through pTrigger list */
! 100189:
! 100190: assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
! 100191: assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
! 100192: assert( (op==TK_UPDATE)==(pChanges!=0) );
! 100193:
! 100194: for(p=pTrigger; p; p=p->pNext){
! 100195:
! 100196: /* Sanity checking: The schema for the trigger and for the table are
! 100197: ** always defined. The trigger must be in the same schema as the table
! 100198: ** or else it must be a TEMP trigger. */
! 100199: assert( p->pSchema!=0 );
! 100200: assert( p->pTabSchema!=0 );
! 100201: assert( p->pSchema==p->pTabSchema
! 100202: || p->pSchema==pParse->db->aDb[1].pSchema );
! 100203:
! 100204: /* Determine whether we should code this trigger */
! 100205: if( p->op==op
! 100206: && p->tr_tm==tr_tm
! 100207: && checkColumnOverlap(p->pColumns, pChanges)
! 100208: ){
! 100209: sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
! 100210: }
! 100211: }
! 100212: }
! 100213:
! 100214: /*
! 100215: ** Triggers may access values stored in the old.* or new.* pseudo-table.
! 100216: ** This function returns a 32-bit bitmask indicating which columns of the
! 100217: ** old.* or new.* tables actually are used by triggers. This information
! 100218: ** may be used by the caller, for example, to avoid having to load the entire
! 100219: ** old.* record into memory when executing an UPDATE or DELETE command.
! 100220: **
! 100221: ** Bit 0 of the returned mask is set if the left-most column of the
! 100222: ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
! 100223: ** the second leftmost column value is required, and so on. If there
! 100224: ** are more than 32 columns in the table, and at least one of the columns
! 100225: ** with an index greater than 32 may be accessed, 0xffffffff is returned.
! 100226: **
! 100227: ** It is not possible to determine if the old.rowid or new.rowid column is
! 100228: ** accessed by triggers. The caller must always assume that it is.
! 100229: **
! 100230: ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
! 100231: ** applies to the old.* table. If 1, the new.* table.
! 100232: **
! 100233: ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
! 100234: ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
! 100235: ** included in the returned mask if the TRIGGER_BEFORE bit is set in the
! 100236: ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
! 100237: ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
! 100238: */
! 100239: SQLITE_PRIVATE u32 sqlite3TriggerColmask(
! 100240: Parse *pParse, /* Parse context */
! 100241: Trigger *pTrigger, /* List of triggers on table pTab */
! 100242: ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
! 100243: int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
! 100244: int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
! 100245: Table *pTab, /* The table to code triggers from */
! 100246: int orconf /* Default ON CONFLICT policy for trigger steps */
! 100247: ){
! 100248: const int op = pChanges ? TK_UPDATE : TK_DELETE;
! 100249: u32 mask = 0;
! 100250: Trigger *p;
! 100251:
! 100252: assert( isNew==1 || isNew==0 );
! 100253: for(p=pTrigger; p; p=p->pNext){
! 100254: if( p->op==op && (tr_tm&p->tr_tm)
! 100255: && checkColumnOverlap(p->pColumns,pChanges)
! 100256: ){
! 100257: TriggerPrg *pPrg;
! 100258: pPrg = getRowTrigger(pParse, p, pTab, orconf);
! 100259: if( pPrg ){
! 100260: mask |= pPrg->aColmask[isNew];
! 100261: }
! 100262: }
! 100263: }
! 100264:
! 100265: return mask;
! 100266: }
! 100267:
! 100268: #endif /* !defined(SQLITE_OMIT_TRIGGER) */
! 100269:
! 100270: /************** End of trigger.c *********************************************/
! 100271: /************** Begin file update.c ******************************************/
! 100272: /*
! 100273: ** 2001 September 15
! 100274: **
! 100275: ** The author disclaims copyright to this source code. In place of
! 100276: ** a legal notice, here is a blessing:
! 100277: **
! 100278: ** May you do good and not evil.
! 100279: ** May you find forgiveness for yourself and forgive others.
! 100280: ** May you share freely, never taking more than you give.
! 100281: **
! 100282: *************************************************************************
! 100283: ** This file contains C code routines that are called by the parser
! 100284: ** to handle UPDATE statements.
! 100285: */
! 100286:
! 100287: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 100288: /* Forward declaration */
! 100289: static void updateVirtualTable(
! 100290: Parse *pParse, /* The parsing context */
! 100291: SrcList *pSrc, /* The virtual table to be modified */
! 100292: Table *pTab, /* The virtual table */
! 100293: ExprList *pChanges, /* The columns to change in the UPDATE statement */
! 100294: Expr *pRowidExpr, /* Expression used to recompute the rowid */
! 100295: int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
! 100296: Expr *pWhere, /* WHERE clause of the UPDATE statement */
! 100297: int onError /* ON CONFLICT strategy */
! 100298: );
! 100299: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 100300:
! 100301: /*
! 100302: ** The most recently coded instruction was an OP_Column to retrieve the
! 100303: ** i-th column of table pTab. This routine sets the P4 parameter of the
! 100304: ** OP_Column to the default value, if any.
! 100305: **
! 100306: ** The default value of a column is specified by a DEFAULT clause in the
! 100307: ** column definition. This was either supplied by the user when the table
! 100308: ** was created, or added later to the table definition by an ALTER TABLE
! 100309: ** command. If the latter, then the row-records in the table btree on disk
! 100310: ** may not contain a value for the column and the default value, taken
! 100311: ** from the P4 parameter of the OP_Column instruction, is returned instead.
! 100312: ** If the former, then all row-records are guaranteed to include a value
! 100313: ** for the column and the P4 value is not required.
! 100314: **
! 100315: ** Column definitions created by an ALTER TABLE command may only have
! 100316: ** literal default values specified: a number, null or a string. (If a more
! 100317: ** complicated default expression value was provided, it is evaluated
! 100318: ** when the ALTER TABLE is executed and one of the literal values written
! 100319: ** into the sqlite_master table.)
! 100320: **
! 100321: ** Therefore, the P4 parameter is only required if the default value for
! 100322: ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
! 100323: ** function is capable of transforming these types of expressions into
! 100324: ** sqlite3_value objects.
! 100325: **
! 100326: ** If parameter iReg is not negative, code an OP_RealAffinity instruction
! 100327: ** on register iReg. This is used when an equivalent integer value is
! 100328: ** stored in place of an 8-byte floating point value in order to save
! 100329: ** space.
! 100330: */
! 100331: SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
! 100332: assert( pTab!=0 );
! 100333: if( !pTab->pSelect ){
! 100334: sqlite3_value *pValue;
! 100335: u8 enc = ENC(sqlite3VdbeDb(v));
! 100336: Column *pCol = &pTab->aCol[i];
! 100337: VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
! 100338: assert( i<pTab->nCol );
! 100339: sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
! 100340: pCol->affinity, &pValue);
! 100341: if( pValue ){
! 100342: sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
! 100343: }
! 100344: #ifndef SQLITE_OMIT_FLOATING_POINT
! 100345: if( iReg>=0 && pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
! 100346: sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
! 100347: }
! 100348: #endif
! 100349: }
! 100350: }
! 100351:
! 100352: /*
! 100353: ** Process an UPDATE statement.
! 100354: **
! 100355: ** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
! 100356: ** \_______/ \________/ \______/ \________________/
! 100357: * onError pTabList pChanges pWhere
! 100358: */
! 100359: SQLITE_PRIVATE void sqlite3Update(
! 100360: Parse *pParse, /* The parser context */
! 100361: SrcList *pTabList, /* The table in which we should change things */
! 100362: ExprList *pChanges, /* Things to be changed */
! 100363: Expr *pWhere, /* The WHERE clause. May be null */
! 100364: int onError /* How to handle constraint errors */
! 100365: ){
! 100366: int i, j; /* Loop counters */
! 100367: Table *pTab; /* The table to be updated */
! 100368: int addr = 0; /* VDBE instruction address of the start of the loop */
! 100369: WhereInfo *pWInfo; /* Information about the WHERE clause */
! 100370: Vdbe *v; /* The virtual database engine */
! 100371: Index *pIdx; /* For looping over indices */
! 100372: int nIdx; /* Number of indices that need updating */
! 100373: int iCur; /* VDBE Cursor number of pTab */
! 100374: sqlite3 *db; /* The database structure */
! 100375: int *aRegIdx = 0; /* One register assigned to each index to be updated */
! 100376: int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
! 100377: ** an expression for the i-th column of the table.
! 100378: ** aXRef[i]==-1 if the i-th column is not changed. */
! 100379: int chngRowid; /* True if the record number is being changed */
! 100380: Expr *pRowidExpr = 0; /* Expression defining the new record number */
! 100381: int openAll = 0; /* True if all indices need to be opened */
! 100382: AuthContext sContext; /* The authorization context */
! 100383: NameContext sNC; /* The name-context to resolve expressions in */
! 100384: int iDb; /* Database containing the table being updated */
! 100385: int okOnePass; /* True for one-pass algorithm without the FIFO */
! 100386: int hasFK; /* True if foreign key processing is required */
! 100387:
! 100388: #ifndef SQLITE_OMIT_TRIGGER
! 100389: int isView; /* True when updating a view (INSTEAD OF trigger) */
! 100390: Trigger *pTrigger; /* List of triggers on pTab, if required */
! 100391: int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
! 100392: #endif
! 100393: int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
! 100394:
! 100395: /* Register Allocations */
! 100396: int regRowCount = 0; /* A count of rows changed */
! 100397: int regOldRowid; /* The old rowid */
! 100398: int regNewRowid; /* The new rowid */
! 100399: int regNew; /* Content of the NEW.* table in triggers */
! 100400: int regOld = 0; /* Content of OLD.* table in triggers */
! 100401: int regRowSet = 0; /* Rowset of rows to be updated */
! 100402:
! 100403: memset(&sContext, 0, sizeof(sContext));
! 100404: db = pParse->db;
! 100405: if( pParse->nErr || db->mallocFailed ){
! 100406: goto update_cleanup;
! 100407: }
! 100408: assert( pTabList->nSrc==1 );
! 100409:
! 100410: /* Locate the table which we want to update.
! 100411: */
! 100412: pTab = sqlite3SrcListLookup(pParse, pTabList);
! 100413: if( pTab==0 ) goto update_cleanup;
! 100414: iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
! 100415:
! 100416: /* Figure out if we have any triggers and if the table being
! 100417: ** updated is a view.
! 100418: */
! 100419: #ifndef SQLITE_OMIT_TRIGGER
! 100420: pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
! 100421: isView = pTab->pSelect!=0;
! 100422: assert( pTrigger || tmask==0 );
! 100423: #else
! 100424: # define pTrigger 0
! 100425: # define isView 0
! 100426: # define tmask 0
! 100427: #endif
! 100428: #ifdef SQLITE_OMIT_VIEW
! 100429: # undef isView
! 100430: # define isView 0
! 100431: #endif
! 100432:
! 100433: if( sqlite3ViewGetColumnNames(pParse, pTab) ){
! 100434: goto update_cleanup;
! 100435: }
! 100436: if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
! 100437: goto update_cleanup;
! 100438: }
! 100439: aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
! 100440: if( aXRef==0 ) goto update_cleanup;
! 100441: for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
! 100442:
! 100443: /* Allocate a cursors for the main database table and for all indices.
! 100444: ** The index cursors might not be used, but if they are used they
! 100445: ** need to occur right after the database cursor. So go ahead and
! 100446: ** allocate enough space, just in case.
! 100447: */
! 100448: pTabList->a[0].iCursor = iCur = pParse->nTab++;
! 100449: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
! 100450: pParse->nTab++;
! 100451: }
! 100452:
! 100453: /* Initialize the name-context */
! 100454: memset(&sNC, 0, sizeof(sNC));
! 100455: sNC.pParse = pParse;
! 100456: sNC.pSrcList = pTabList;
! 100457:
! 100458: /* Resolve the column names in all the expressions of the
! 100459: ** of the UPDATE statement. Also find the column index
! 100460: ** for each column to be updated in the pChanges array. For each
! 100461: ** column to be updated, make sure we have authorization to change
! 100462: ** that column.
! 100463: */
! 100464: chngRowid = 0;
! 100465: for(i=0; i<pChanges->nExpr; i++){
! 100466: if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
! 100467: goto update_cleanup;
! 100468: }
! 100469: for(j=0; j<pTab->nCol; j++){
! 100470: if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
! 100471: if( j==pTab->iPKey ){
! 100472: chngRowid = 1;
! 100473: pRowidExpr = pChanges->a[i].pExpr;
! 100474: }
! 100475: aXRef[j] = i;
! 100476: break;
! 100477: }
! 100478: }
! 100479: if( j>=pTab->nCol ){
! 100480: if( sqlite3IsRowid(pChanges->a[i].zName) ){
! 100481: chngRowid = 1;
! 100482: pRowidExpr = pChanges->a[i].pExpr;
! 100483: }else{
! 100484: sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
! 100485: pParse->checkSchema = 1;
! 100486: goto update_cleanup;
! 100487: }
! 100488: }
! 100489: #ifndef SQLITE_OMIT_AUTHORIZATION
! 100490: {
! 100491: int rc;
! 100492: rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
! 100493: pTab->aCol[j].zName, db->aDb[iDb].zName);
! 100494: if( rc==SQLITE_DENY ){
! 100495: goto update_cleanup;
! 100496: }else if( rc==SQLITE_IGNORE ){
! 100497: aXRef[j] = -1;
! 100498: }
! 100499: }
! 100500: #endif
! 100501: }
! 100502:
! 100503: hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid);
! 100504:
! 100505: /* Allocate memory for the array aRegIdx[]. There is one entry in the
! 100506: ** array for each index associated with table being updated. Fill in
! 100507: ** the value with a register number for indices that are to be used
! 100508: ** and with zero for unused indices.
! 100509: */
! 100510: for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
! 100511: if( nIdx>0 ){
! 100512: aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
! 100513: if( aRegIdx==0 ) goto update_cleanup;
! 100514: }
! 100515: for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
! 100516: int reg;
! 100517: if( hasFK || chngRowid ){
! 100518: reg = ++pParse->nMem;
! 100519: }else{
! 100520: reg = 0;
! 100521: for(i=0; i<pIdx->nColumn; i++){
! 100522: if( aXRef[pIdx->aiColumn[i]]>=0 ){
! 100523: reg = ++pParse->nMem;
! 100524: break;
! 100525: }
! 100526: }
! 100527: }
! 100528: aRegIdx[j] = reg;
! 100529: }
! 100530:
! 100531: /* Begin generating code. */
! 100532: v = sqlite3GetVdbe(pParse);
! 100533: if( v==0 ) goto update_cleanup;
! 100534: if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
! 100535: sqlite3BeginWriteOperation(pParse, 1, iDb);
! 100536:
! 100537: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 100538: /* Virtual tables must be handled separately */
! 100539: if( IsVirtual(pTab) ){
! 100540: updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
! 100541: pWhere, onError);
! 100542: pWhere = 0;
! 100543: pTabList = 0;
! 100544: goto update_cleanup;
! 100545: }
! 100546: #endif
! 100547:
! 100548: /* Allocate required registers. */
! 100549: regRowSet = ++pParse->nMem;
! 100550: regOldRowid = regNewRowid = ++pParse->nMem;
! 100551: if( pTrigger || hasFK ){
! 100552: regOld = pParse->nMem + 1;
! 100553: pParse->nMem += pTab->nCol;
! 100554: }
! 100555: if( chngRowid || pTrigger || hasFK ){
! 100556: regNewRowid = ++pParse->nMem;
! 100557: }
! 100558: regNew = pParse->nMem + 1;
! 100559: pParse->nMem += pTab->nCol;
! 100560:
! 100561: /* Start the view context. */
! 100562: if( isView ){
! 100563: sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
! 100564: }
! 100565:
! 100566: /* If we are trying to update a view, realize that view into
! 100567: ** a ephemeral table.
! 100568: */
! 100569: #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
! 100570: if( isView ){
! 100571: sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
! 100572: }
! 100573: #endif
! 100574:
! 100575: /* Resolve the column names in all the expressions in the
! 100576: ** WHERE clause.
! 100577: */
! 100578: if( sqlite3ResolveExprNames(&sNC, pWhere) ){
! 100579: goto update_cleanup;
! 100580: }
! 100581:
! 100582: /* Begin the database scan
! 100583: */
! 100584: sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
! 100585: pWInfo = sqlite3WhereBegin(
! 100586: pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED
! 100587: );
! 100588: if( pWInfo==0 ) goto update_cleanup;
! 100589: okOnePass = pWInfo->okOnePass;
! 100590:
! 100591: /* Remember the rowid of every item to be updated.
! 100592: */
! 100593: sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
! 100594: if( !okOnePass ){
! 100595: sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
! 100596: }
! 100597:
! 100598: /* End the database scan loop.
! 100599: */
! 100600: sqlite3WhereEnd(pWInfo);
! 100601:
! 100602: /* Initialize the count of updated rows
! 100603: */
! 100604: if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
! 100605: regRowCount = ++pParse->nMem;
! 100606: sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
! 100607: }
! 100608:
! 100609: if( !isView ){
! 100610: /*
! 100611: ** Open every index that needs updating. Note that if any
! 100612: ** index could potentially invoke a REPLACE conflict resolution
! 100613: ** action, then we need to open all indices because we might need
! 100614: ** to be deleting some records.
! 100615: */
! 100616: if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
! 100617: if( onError==OE_Replace ){
! 100618: openAll = 1;
! 100619: }else{
! 100620: openAll = 0;
! 100621: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
! 100622: if( pIdx->onError==OE_Replace ){
! 100623: openAll = 1;
! 100624: break;
! 100625: }
! 100626: }
! 100627: }
! 100628: for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
! 100629: assert( aRegIdx );
! 100630: if( openAll || aRegIdx[i]>0 ){
! 100631: KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
! 100632: sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
! 100633: (char*)pKey, P4_KEYINFO_HANDOFF);
! 100634: assert( pParse->nTab>iCur+i+1 );
! 100635: }
! 100636: }
! 100637: }
! 100638:
! 100639: /* Top of the update loop */
! 100640: if( okOnePass ){
! 100641: int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid);
! 100642: addr = sqlite3VdbeAddOp0(v, OP_Goto);
! 100643: sqlite3VdbeJumpHere(v, a1);
! 100644: }else{
! 100645: addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid);
! 100646: }
! 100647:
! 100648: /* Make cursor iCur point to the record that is being updated. If
! 100649: ** this record does not exist for some reason (deleted by a trigger,
! 100650: ** for example, then jump to the next iteration of the RowSet loop. */
! 100651: sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
! 100652:
! 100653: /* If the record number will change, set register regNewRowid to
! 100654: ** contain the new value. If the record number is not being modified,
! 100655: ** then regNewRowid is the same register as regOldRowid, which is
! 100656: ** already populated. */
! 100657: assert( chngRowid || pTrigger || hasFK || regOldRowid==regNewRowid );
! 100658: if( chngRowid ){
! 100659: sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
! 100660: sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
! 100661: }
! 100662:
! 100663: /* If there are triggers on this table, populate an array of registers
! 100664: ** with the required old.* column data. */
! 100665: if( hasFK || pTrigger ){
! 100666: u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
! 100667: oldmask |= sqlite3TriggerColmask(pParse,
! 100668: pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
! 100669: );
! 100670: for(i=0; i<pTab->nCol; i++){
! 100671: if( aXRef[i]<0 || oldmask==0xffffffff || (i<32 && (oldmask & (1<<i))) ){
! 100672: sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOld+i);
! 100673: }else{
! 100674: sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
! 100675: }
! 100676: }
! 100677: if( chngRowid==0 ){
! 100678: sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
! 100679: }
! 100680: }
! 100681:
! 100682: /* Populate the array of registers beginning at regNew with the new
! 100683: ** row data. This array is used to check constaints, create the new
! 100684: ** table and index records, and as the values for any new.* references
! 100685: ** made by triggers.
! 100686: **
! 100687: ** If there are one or more BEFORE triggers, then do not populate the
! 100688: ** registers associated with columns that are (a) not modified by
! 100689: ** this UPDATE statement and (b) not accessed by new.* references. The
! 100690: ** values for registers not modified by the UPDATE must be reloaded from
! 100691: ** the database after the BEFORE triggers are fired anyway (as the trigger
! 100692: ** may have modified them). So not loading those that are not going to
! 100693: ** be used eliminates some redundant opcodes.
! 100694: */
! 100695: newmask = sqlite3TriggerColmask(
! 100696: pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
! 100697: );
! 100698: sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);
! 100699: for(i=0; i<pTab->nCol; i++){
! 100700: if( i==pTab->iPKey ){
! 100701: /*sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);*/
! 100702: }else{
! 100703: j = aXRef[i];
! 100704: if( j>=0 ){
! 100705: sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
! 100706: }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<<i)) ){
! 100707: /* This branch loads the value of a column that will not be changed
! 100708: ** into a register. This is done if there are no BEFORE triggers, or
! 100709: ** if there are one or more BEFORE triggers that use this value via
! 100710: ** a new.* reference in a trigger program.
! 100711: */
! 100712: testcase( i==31 );
! 100713: testcase( i==32 );
! 100714: sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
! 100715: sqlite3ColumnDefault(v, pTab, i, regNew+i);
! 100716: }
! 100717: }
! 100718: }
! 100719:
! 100720: /* Fire any BEFORE UPDATE triggers. This happens before constraints are
! 100721: ** verified. One could argue that this is wrong.
! 100722: */
! 100723: if( tmask&TRIGGER_BEFORE ){
! 100724: sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol);
! 100725: sqlite3TableAffinityStr(v, pTab);
! 100726: sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
! 100727: TRIGGER_BEFORE, pTab, regOldRowid, onError, addr);
! 100728:
! 100729: /* The row-trigger may have deleted the row being updated. In this
! 100730: ** case, jump to the next row. No updates or AFTER triggers are
! 100731: ** required. This behaviour - what happens when the row being updated
! 100732: ** is deleted or renamed by a BEFORE trigger - is left undefined in the
! 100733: ** documentation.
! 100734: */
! 100735: sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
! 100736:
! 100737: /* If it did not delete it, the row-trigger may still have modified
! 100738: ** some of the columns of the row being updated. Load the values for
! 100739: ** all columns not modified by the update statement into their
! 100740: ** registers in case this has happened.
! 100741: */
! 100742: for(i=0; i<pTab->nCol; i++){
! 100743: if( aXRef[i]<0 && i!=pTab->iPKey ){
! 100744: sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
! 100745: sqlite3ColumnDefault(v, pTab, i, regNew+i);
! 100746: }
! 100747: }
! 100748: }
! 100749:
! 100750: if( !isView ){
! 100751: int j1; /* Address of jump instruction */
! 100752:
! 100753: /* Do constraint checks. */
! 100754: sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
! 100755: aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0);
! 100756:
! 100757: /* Do FK constraint checks. */
! 100758: if( hasFK ){
! 100759: sqlite3FkCheck(pParse, pTab, regOldRowid, 0);
! 100760: }
! 100761:
! 100762: /* Delete the index entries associated with the current record. */
! 100763: j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
! 100764: sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
! 100765:
! 100766: /* If changing the record number, delete the old record. */
! 100767: if( hasFK || chngRowid ){
! 100768: sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
! 100769: }
! 100770: sqlite3VdbeJumpHere(v, j1);
! 100771:
! 100772: if( hasFK ){
! 100773: sqlite3FkCheck(pParse, pTab, 0, regNewRowid);
! 100774: }
! 100775:
! 100776: /* Insert the new index entries and the new record. */
! 100777: sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, 0, 0);
! 100778:
! 100779: /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
! 100780: ** handle rows (possibly in other tables) that refer via a foreign key
! 100781: ** to the row just updated. */
! 100782: if( hasFK ){
! 100783: sqlite3FkActions(pParse, pTab, pChanges, regOldRowid);
! 100784: }
! 100785: }
! 100786:
! 100787: /* Increment the row counter
! 100788: */
! 100789: if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
! 100790: sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
! 100791: }
! 100792:
! 100793: sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
! 100794: TRIGGER_AFTER, pTab, regOldRowid, onError, addr);
! 100795:
! 100796: /* Repeat the above with the next record to be updated, until
! 100797: ** all record selected by the WHERE clause have been updated.
! 100798: */
! 100799: sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
! 100800: sqlite3VdbeJumpHere(v, addr);
! 100801:
! 100802: /* Close all tables */
! 100803: for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
! 100804: assert( aRegIdx );
! 100805: if( openAll || aRegIdx[i]>0 ){
! 100806: sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
! 100807: }
! 100808: }
! 100809: sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
! 100810:
! 100811: /* Update the sqlite_sequence table by storing the content of the
! 100812: ** maximum rowid counter values recorded while inserting into
! 100813: ** autoincrement tables.
! 100814: */
! 100815: if( pParse->nested==0 && pParse->pTriggerTab==0 ){
! 100816: sqlite3AutoincrementEnd(pParse);
! 100817: }
! 100818:
! 100819: /*
! 100820: ** Return the number of rows that were changed. If this routine is
! 100821: ** generating code because of a call to sqlite3NestedParse(), do not
! 100822: ** invoke the callback function.
! 100823: */
! 100824: if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
! 100825: sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
! 100826: sqlite3VdbeSetNumCols(v, 1);
! 100827: sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
! 100828: }
! 100829:
! 100830: update_cleanup:
! 100831: sqlite3AuthContextPop(&sContext);
! 100832: sqlite3DbFree(db, aRegIdx);
! 100833: sqlite3DbFree(db, aXRef);
! 100834: sqlite3SrcListDelete(db, pTabList);
! 100835: sqlite3ExprListDelete(db, pChanges);
! 100836: sqlite3ExprDelete(db, pWhere);
! 100837: return;
! 100838: }
! 100839: /* Make sure "isView" and other macros defined above are undefined. Otherwise
! 100840: ** thely may interfere with compilation of other functions in this file
! 100841: ** (or in another file, if this file becomes part of the amalgamation). */
! 100842: #ifdef isView
! 100843: #undef isView
! 100844: #endif
! 100845: #ifdef pTrigger
! 100846: #undef pTrigger
! 100847: #endif
! 100848:
! 100849: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 100850: /*
! 100851: ** Generate code for an UPDATE of a virtual table.
! 100852: **
! 100853: ** The strategy is that we create an ephemerial table that contains
! 100854: ** for each row to be changed:
! 100855: **
! 100856: ** (A) The original rowid of that row.
! 100857: ** (B) The revised rowid for the row. (note1)
! 100858: ** (C) The content of every column in the row.
! 100859: **
! 100860: ** Then we loop over this ephemeral table and for each row in
! 100861: ** the ephermeral table call VUpdate.
! 100862: **
! 100863: ** When finished, drop the ephemeral table.
! 100864: **
! 100865: ** (note1) Actually, if we know in advance that (A) is always the same
! 100866: ** as (B) we only store (A), then duplicate (A) when pulling
! 100867: ** it out of the ephemeral table before calling VUpdate.
! 100868: */
! 100869: static void updateVirtualTable(
! 100870: Parse *pParse, /* The parsing context */
! 100871: SrcList *pSrc, /* The virtual table to be modified */
! 100872: Table *pTab, /* The virtual table */
! 100873: ExprList *pChanges, /* The columns to change in the UPDATE statement */
! 100874: Expr *pRowid, /* Expression used to recompute the rowid */
! 100875: int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
! 100876: Expr *pWhere, /* WHERE clause of the UPDATE statement */
! 100877: int onError /* ON CONFLICT strategy */
! 100878: ){
! 100879: Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
! 100880: ExprList *pEList = 0; /* The result set of the SELECT statement */
! 100881: Select *pSelect = 0; /* The SELECT statement */
! 100882: Expr *pExpr; /* Temporary expression */
! 100883: int ephemTab; /* Table holding the result of the SELECT */
! 100884: int i; /* Loop counter */
! 100885: int addr; /* Address of top of loop */
! 100886: int iReg; /* First register in set passed to OP_VUpdate */
! 100887: sqlite3 *db = pParse->db; /* Database connection */
! 100888: const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
! 100889: SelectDest dest;
! 100890:
! 100891: /* Construct the SELECT statement that will find the new values for
! 100892: ** all updated rows.
! 100893: */
! 100894: pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
! 100895: if( pRowid ){
! 100896: pEList = sqlite3ExprListAppend(pParse, pEList,
! 100897: sqlite3ExprDup(db, pRowid, 0));
! 100898: }
! 100899: assert( pTab->iPKey<0 );
! 100900: for(i=0; i<pTab->nCol; i++){
! 100901: if( aXRef[i]>=0 ){
! 100902: pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
! 100903: }else{
! 100904: pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
! 100905: }
! 100906: pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
! 100907: }
! 100908: pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
! 100909:
! 100910: /* Create the ephemeral table into which the update results will
! 100911: ** be stored.
! 100912: */
! 100913: assert( v );
! 100914: ephemTab = pParse->nTab++;
! 100915: sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
! 100916: sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
! 100917:
! 100918: /* fill the ephemeral table
! 100919: */
! 100920: sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
! 100921: sqlite3Select(pParse, pSelect, &dest);
! 100922:
! 100923: /* Generate code to scan the ephemeral table and call VUpdate. */
! 100924: iReg = ++pParse->nMem;
! 100925: pParse->nMem += pTab->nCol+1;
! 100926: addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
! 100927: sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg);
! 100928: sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
! 100929: for(i=0; i<pTab->nCol; i++){
! 100930: sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
! 100931: }
! 100932: sqlite3VtabMakeWritable(pParse, pTab);
! 100933: sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
! 100934: sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
! 100935: sqlite3MayAbort(pParse);
! 100936: sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
! 100937: sqlite3VdbeJumpHere(v, addr);
! 100938: sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
! 100939:
! 100940: /* Cleanup */
! 100941: sqlite3SelectDelete(db, pSelect);
! 100942: }
! 100943: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 100944:
! 100945: /************** End of update.c **********************************************/
! 100946: /************** Begin file vacuum.c ******************************************/
! 100947: /*
! 100948: ** 2003 April 6
! 100949: **
! 100950: ** The author disclaims copyright to this source code. In place of
! 100951: ** a legal notice, here is a blessing:
! 100952: **
! 100953: ** May you do good and not evil.
! 100954: ** May you find forgiveness for yourself and forgive others.
! 100955: ** May you share freely, never taking more than you give.
! 100956: **
! 100957: *************************************************************************
! 100958: ** This file contains code used to implement the VACUUM command.
! 100959: **
! 100960: ** Most of the code in this file may be omitted by defining the
! 100961: ** SQLITE_OMIT_VACUUM macro.
! 100962: */
! 100963:
! 100964: #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
! 100965: /*
! 100966: ** Finalize a prepared statement. If there was an error, store the
! 100967: ** text of the error message in *pzErrMsg. Return the result code.
! 100968: */
! 100969: static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
! 100970: int rc;
! 100971: rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
! 100972: if( rc ){
! 100973: sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
! 100974: }
! 100975: return rc;
! 100976: }
! 100977:
! 100978: /*
! 100979: ** Execute zSql on database db. Return an error code.
! 100980: */
! 100981: static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
! 100982: sqlite3_stmt *pStmt;
! 100983: VVA_ONLY( int rc; )
! 100984: if( !zSql ){
! 100985: return SQLITE_NOMEM;
! 100986: }
! 100987: if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
! 100988: sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
! 100989: return sqlite3_errcode(db);
! 100990: }
! 100991: VVA_ONLY( rc = ) sqlite3_step(pStmt);
! 100992: assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
! 100993: return vacuumFinalize(db, pStmt, pzErrMsg);
! 100994: }
! 100995:
! 100996: /*
! 100997: ** Execute zSql on database db. The statement returns exactly
! 100998: ** one column. Execute this as SQL on the same database.
! 100999: */
! 101000: static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
! 101001: sqlite3_stmt *pStmt;
! 101002: int rc;
! 101003:
! 101004: rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
! 101005: if( rc!=SQLITE_OK ) return rc;
! 101006:
! 101007: while( SQLITE_ROW==sqlite3_step(pStmt) ){
! 101008: rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
! 101009: if( rc!=SQLITE_OK ){
! 101010: vacuumFinalize(db, pStmt, pzErrMsg);
! 101011: return rc;
! 101012: }
! 101013: }
! 101014:
! 101015: return vacuumFinalize(db, pStmt, pzErrMsg);
! 101016: }
! 101017:
! 101018: /*
! 101019: ** The non-standard VACUUM command is used to clean up the database,
! 101020: ** collapse free space, etc. It is modelled after the VACUUM command
! 101021: ** in PostgreSQL.
! 101022: **
! 101023: ** In version 1.0.x of SQLite, the VACUUM command would call
! 101024: ** gdbm_reorganize() on all the database tables. But beginning
! 101025: ** with 2.0.0, SQLite no longer uses GDBM so this command has
! 101026: ** become a no-op.
! 101027: */
! 101028: SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
! 101029: Vdbe *v = sqlite3GetVdbe(pParse);
! 101030: if( v ){
! 101031: sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
! 101032: }
! 101033: return;
! 101034: }
! 101035:
! 101036: /*
! 101037: ** This routine implements the OP_Vacuum opcode of the VDBE.
! 101038: */
! 101039: SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
! 101040: int rc = SQLITE_OK; /* Return code from service routines */
! 101041: Btree *pMain; /* The database being vacuumed */
! 101042: Btree *pTemp; /* The temporary database we vacuum into */
! 101043: char *zSql = 0; /* SQL statements */
! 101044: int saved_flags; /* Saved value of the db->flags */
! 101045: int saved_nChange; /* Saved value of db->nChange */
! 101046: int saved_nTotalChange; /* Saved value of db->nTotalChange */
! 101047: void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */
! 101048: Db *pDb = 0; /* Database to detach at end of vacuum */
! 101049: int isMemDb; /* True if vacuuming a :memory: database */
! 101050: int nRes; /* Bytes of reserved space at the end of each page */
! 101051: int nDb; /* Number of attached databases */
! 101052:
! 101053: if( !db->autoCommit ){
! 101054: sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
! 101055: return SQLITE_ERROR;
! 101056: }
! 101057: if( db->activeVdbeCnt>1 ){
! 101058: sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
! 101059: return SQLITE_ERROR;
! 101060: }
! 101061:
! 101062: /* Save the current value of the database flags so that it can be
! 101063: ** restored before returning. Then set the writable-schema flag, and
! 101064: ** disable CHECK and foreign key constraints. */
! 101065: saved_flags = db->flags;
! 101066: saved_nChange = db->nChange;
! 101067: saved_nTotalChange = db->nTotalChange;
! 101068: saved_xTrace = db->xTrace;
! 101069: db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
! 101070: db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
! 101071: db->xTrace = 0;
! 101072:
! 101073: pMain = db->aDb[0].pBt;
! 101074: isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
! 101075:
! 101076: /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
! 101077: ** can be set to 'off' for this file, as it is not recovered if a crash
! 101078: ** occurs anyway. The integrity of the database is maintained by a
! 101079: ** (possibly synchronous) transaction opened on the main database before
! 101080: ** sqlite3BtreeCopyFile() is called.
! 101081: **
! 101082: ** An optimisation would be to use a non-journaled pager.
! 101083: ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
! 101084: ** that actually made the VACUUM run slower. Very little journalling
! 101085: ** actually occurs when doing a vacuum since the vacuum_db is initially
! 101086: ** empty. Only the journal header is written. Apparently it takes more
! 101087: ** time to parse and run the PRAGMA to turn journalling off than it does
! 101088: ** to write the journal header file.
! 101089: */
! 101090: nDb = db->nDb;
! 101091: if( sqlite3TempInMemory(db) ){
! 101092: zSql = "ATTACH ':memory:' AS vacuum_db;";
! 101093: }else{
! 101094: zSql = "ATTACH '' AS vacuum_db;";
! 101095: }
! 101096: rc = execSql(db, pzErrMsg, zSql);
! 101097: if( db->nDb>nDb ){
! 101098: pDb = &db->aDb[db->nDb-1];
! 101099: assert( strcmp(pDb->zName,"vacuum_db")==0 );
! 101100: }
! 101101: if( rc!=SQLITE_OK ) goto end_of_vacuum;
! 101102: pTemp = db->aDb[db->nDb-1].pBt;
! 101103:
! 101104: /* The call to execSql() to attach the temp database has left the file
! 101105: ** locked (as there was more than one active statement when the transaction
! 101106: ** to read the schema was concluded. Unlock it here so that this doesn't
! 101107: ** cause problems for the call to BtreeSetPageSize() below. */
! 101108: sqlite3BtreeCommit(pTemp);
! 101109:
! 101110: nRes = sqlite3BtreeGetReserve(pMain);
! 101111:
! 101112: /* A VACUUM cannot change the pagesize of an encrypted database. */
! 101113: #ifdef SQLITE_HAS_CODEC
! 101114: if( db->nextPagesize ){
! 101115: extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
! 101116: int nKey;
! 101117: char *zKey;
! 101118: sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
! 101119: if( nKey ) db->nextPagesize = 0;
! 101120: }
! 101121: #endif
! 101122:
! 101123: /* Do not attempt to change the page size for a WAL database */
! 101124: if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
! 101125: ==PAGER_JOURNALMODE_WAL ){
! 101126: db->nextPagesize = 0;
! 101127: }
! 101128:
! 101129: if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
! 101130: || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
! 101131: || NEVER(db->mallocFailed)
! 101132: ){
! 101133: rc = SQLITE_NOMEM;
! 101134: goto end_of_vacuum;
! 101135: }
! 101136: rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
! 101137: if( rc!=SQLITE_OK ){
! 101138: goto end_of_vacuum;
! 101139: }
! 101140:
! 101141: #ifndef SQLITE_OMIT_AUTOVACUUM
! 101142: sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
! 101143: sqlite3BtreeGetAutoVacuum(pMain));
! 101144: #endif
! 101145:
! 101146: /* Begin a transaction */
! 101147: rc = execSql(db, pzErrMsg, "BEGIN EXCLUSIVE;");
! 101148: if( rc!=SQLITE_OK ) goto end_of_vacuum;
! 101149:
! 101150: /* Query the schema of the main database. Create a mirror schema
! 101151: ** in the temporary database.
! 101152: */
! 101153: rc = execExecSql(db, pzErrMsg,
! 101154: "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
! 101155: " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
! 101156: " AND rootpage>0"
! 101157: );
! 101158: if( rc!=SQLITE_OK ) goto end_of_vacuum;
! 101159: rc = execExecSql(db, pzErrMsg,
! 101160: "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
! 101161: " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
! 101162: if( rc!=SQLITE_OK ) goto end_of_vacuum;
! 101163: rc = execExecSql(db, pzErrMsg,
! 101164: "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
! 101165: " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
! 101166: if( rc!=SQLITE_OK ) goto end_of_vacuum;
! 101167:
! 101168: /* Loop through the tables in the main database. For each, do
! 101169: ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
! 101170: ** the contents to the temporary database.
! 101171: */
! 101172: rc = execExecSql(db, pzErrMsg,
! 101173: "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
! 101174: "|| ' SELECT * FROM main.' || quote(name) || ';'"
! 101175: "FROM main.sqlite_master "
! 101176: "WHERE type = 'table' AND name!='sqlite_sequence' "
! 101177: " AND rootpage>0"
! 101178: );
! 101179: if( rc!=SQLITE_OK ) goto end_of_vacuum;
! 101180:
! 101181: /* Copy over the sequence table
! 101182: */
! 101183: rc = execExecSql(db, pzErrMsg,
! 101184: "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
! 101185: "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
! 101186: );
! 101187: if( rc!=SQLITE_OK ) goto end_of_vacuum;
! 101188: rc = execExecSql(db, pzErrMsg,
! 101189: "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
! 101190: "|| ' SELECT * FROM main.' || quote(name) || ';' "
! 101191: "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
! 101192: );
! 101193: if( rc!=SQLITE_OK ) goto end_of_vacuum;
! 101194:
! 101195:
! 101196: /* Copy the triggers, views, and virtual tables from the main database
! 101197: ** over to the temporary database. None of these objects has any
! 101198: ** associated storage, so all we have to do is copy their entries
! 101199: ** from the SQLITE_MASTER table.
! 101200: */
! 101201: rc = execSql(db, pzErrMsg,
! 101202: "INSERT INTO vacuum_db.sqlite_master "
! 101203: " SELECT type, name, tbl_name, rootpage, sql"
! 101204: " FROM main.sqlite_master"
! 101205: " WHERE type='view' OR type='trigger'"
! 101206: " OR (type='table' AND rootpage=0)"
! 101207: );
! 101208: if( rc ) goto end_of_vacuum;
! 101209:
! 101210: /* At this point, there is a write transaction open on both the
! 101211: ** vacuum database and the main database. Assuming no error occurs,
! 101212: ** both transactions are closed by this block - the main database
! 101213: ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
! 101214: ** call to sqlite3BtreeCommit().
! 101215: */
! 101216: {
! 101217: u32 meta;
! 101218: int i;
! 101219:
! 101220: /* This array determines which meta meta values are preserved in the
! 101221: ** vacuum. Even entries are the meta value number and odd entries
! 101222: ** are an increment to apply to the meta value after the vacuum.
! 101223: ** The increment is used to increase the schema cookie so that other
! 101224: ** connections to the same database will know to reread the schema.
! 101225: */
! 101226: static const unsigned char aCopy[] = {
! 101227: BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */
! 101228: BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
! 101229: BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */
! 101230: BTREE_USER_VERSION, 0, /* Preserve the user version */
! 101231: };
! 101232:
! 101233: assert( 1==sqlite3BtreeIsInTrans(pTemp) );
! 101234: assert( 1==sqlite3BtreeIsInTrans(pMain) );
! 101235:
! 101236: /* Copy Btree meta values */
! 101237: for(i=0; i<ArraySize(aCopy); i+=2){
! 101238: /* GetMeta() and UpdateMeta() cannot fail in this context because
! 101239: ** we already have page 1 loaded into cache and marked dirty. */
! 101240: sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
! 101241: rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
! 101242: if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
! 101243: }
! 101244:
! 101245: rc = sqlite3BtreeCopyFile(pMain, pTemp);
! 101246: if( rc!=SQLITE_OK ) goto end_of_vacuum;
! 101247: rc = sqlite3BtreeCommit(pTemp);
! 101248: if( rc!=SQLITE_OK ) goto end_of_vacuum;
! 101249: #ifndef SQLITE_OMIT_AUTOVACUUM
! 101250: sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
! 101251: #endif
! 101252: }
! 101253:
! 101254: assert( rc==SQLITE_OK );
! 101255: rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
! 101256:
! 101257: end_of_vacuum:
! 101258: /* Restore the original value of db->flags */
! 101259: db->flags = saved_flags;
! 101260: db->nChange = saved_nChange;
! 101261: db->nTotalChange = saved_nTotalChange;
! 101262: db->xTrace = saved_xTrace;
! 101263: sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
! 101264:
! 101265: /* Currently there is an SQL level transaction open on the vacuum
! 101266: ** database. No locks are held on any other files (since the main file
! 101267: ** was committed at the btree level). So it safe to end the transaction
! 101268: ** by manually setting the autoCommit flag to true and detaching the
! 101269: ** vacuum database. The vacuum_db journal file is deleted when the pager
! 101270: ** is closed by the DETACH.
! 101271: */
! 101272: db->autoCommit = 1;
! 101273:
! 101274: if( pDb ){
! 101275: sqlite3BtreeClose(pDb->pBt);
! 101276: pDb->pBt = 0;
! 101277: pDb->pSchema = 0;
! 101278: }
! 101279:
! 101280: /* This both clears the schemas and reduces the size of the db->aDb[]
! 101281: ** array. */
! 101282: sqlite3ResetInternalSchema(db, -1);
! 101283:
! 101284: return rc;
! 101285: }
! 101286:
! 101287: #endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
! 101288:
! 101289: /************** End of vacuum.c **********************************************/
! 101290: /************** Begin file vtab.c ********************************************/
! 101291: /*
! 101292: ** 2006 June 10
! 101293: **
! 101294: ** The author disclaims copyright to this source code. In place of
! 101295: ** a legal notice, here is a blessing:
! 101296: **
! 101297: ** May you do good and not evil.
! 101298: ** May you find forgiveness for yourself and forgive others.
! 101299: ** May you share freely, never taking more than you give.
! 101300: **
! 101301: *************************************************************************
! 101302: ** This file contains code used to help implement virtual tables.
! 101303: */
! 101304: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 101305:
! 101306: /*
! 101307: ** Before a virtual table xCreate() or xConnect() method is invoked, the
! 101308: ** sqlite3.pVtabCtx member variable is set to point to an instance of
! 101309: ** this struct allocated on the stack. It is used by the implementation of
! 101310: ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
! 101311: ** are invoked only from within xCreate and xConnect methods.
! 101312: */
! 101313: struct VtabCtx {
! 101314: Table *pTab;
! 101315: VTable *pVTable;
! 101316: };
! 101317:
! 101318: /*
! 101319: ** The actual function that does the work of creating a new module.
! 101320: ** This function implements the sqlite3_create_module() and
! 101321: ** sqlite3_create_module_v2() interfaces.
! 101322: */
! 101323: static int createModule(
! 101324: sqlite3 *db, /* Database in which module is registered */
! 101325: const char *zName, /* Name assigned to this module */
! 101326: const sqlite3_module *pModule, /* The definition of the module */
! 101327: void *pAux, /* Context pointer for xCreate/xConnect */
! 101328: void (*xDestroy)(void *) /* Module destructor function */
! 101329: ){
! 101330: int rc, nName;
! 101331: Module *pMod;
! 101332:
! 101333: sqlite3_mutex_enter(db->mutex);
! 101334: nName = sqlite3Strlen30(zName);
! 101335: pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
! 101336: if( pMod ){
! 101337: Module *pDel;
! 101338: char *zCopy = (char *)(&pMod[1]);
! 101339: memcpy(zCopy, zName, nName+1);
! 101340: pMod->zName = zCopy;
! 101341: pMod->pModule = pModule;
! 101342: pMod->pAux = pAux;
! 101343: pMod->xDestroy = xDestroy;
! 101344: pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
! 101345: if( pDel && pDel->xDestroy ){
! 101346: sqlite3ResetInternalSchema(db, -1);
! 101347: pDel->xDestroy(pDel->pAux);
! 101348: }
! 101349: sqlite3DbFree(db, pDel);
! 101350: if( pDel==pMod ){
! 101351: db->mallocFailed = 1;
! 101352: }
! 101353: }else if( xDestroy ){
! 101354: xDestroy(pAux);
! 101355: }
! 101356: rc = sqlite3ApiExit(db, SQLITE_OK);
! 101357: sqlite3_mutex_leave(db->mutex);
! 101358: return rc;
! 101359: }
! 101360:
! 101361:
! 101362: /*
! 101363: ** External API function used to create a new virtual-table module.
! 101364: */
! 101365: SQLITE_API int sqlite3_create_module(
! 101366: sqlite3 *db, /* Database in which module is registered */
! 101367: const char *zName, /* Name assigned to this module */
! 101368: const sqlite3_module *pModule, /* The definition of the module */
! 101369: void *pAux /* Context pointer for xCreate/xConnect */
! 101370: ){
! 101371: return createModule(db, zName, pModule, pAux, 0);
! 101372: }
! 101373:
! 101374: /*
! 101375: ** External API function used to create a new virtual-table module.
! 101376: */
! 101377: SQLITE_API int sqlite3_create_module_v2(
! 101378: sqlite3 *db, /* Database in which module is registered */
! 101379: const char *zName, /* Name assigned to this module */
! 101380: const sqlite3_module *pModule, /* The definition of the module */
! 101381: void *pAux, /* Context pointer for xCreate/xConnect */
! 101382: void (*xDestroy)(void *) /* Module destructor function */
! 101383: ){
! 101384: return createModule(db, zName, pModule, pAux, xDestroy);
! 101385: }
! 101386:
! 101387: /*
! 101388: ** Lock the virtual table so that it cannot be disconnected.
! 101389: ** Locks nest. Every lock should have a corresponding unlock.
! 101390: ** If an unlock is omitted, resources leaks will occur.
! 101391: **
! 101392: ** If a disconnect is attempted while a virtual table is locked,
! 101393: ** the disconnect is deferred until all locks have been removed.
! 101394: */
! 101395: SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
! 101396: pVTab->nRef++;
! 101397: }
! 101398:
! 101399:
! 101400: /*
! 101401: ** pTab is a pointer to a Table structure representing a virtual-table.
! 101402: ** Return a pointer to the VTable object used by connection db to access
! 101403: ** this virtual-table, if one has been created, or NULL otherwise.
! 101404: */
! 101405: SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
! 101406: VTable *pVtab;
! 101407: assert( IsVirtual(pTab) );
! 101408: for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
! 101409: return pVtab;
! 101410: }
! 101411:
! 101412: /*
! 101413: ** Decrement the ref-count on a virtual table object. When the ref-count
! 101414: ** reaches zero, call the xDisconnect() method to delete the object.
! 101415: */
! 101416: SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
! 101417: sqlite3 *db = pVTab->db;
! 101418:
! 101419: assert( db );
! 101420: assert( pVTab->nRef>0 );
! 101421: assert( sqlite3SafetyCheckOk(db) );
! 101422:
! 101423: pVTab->nRef--;
! 101424: if( pVTab->nRef==0 ){
! 101425: sqlite3_vtab *p = pVTab->pVtab;
! 101426: if( p ){
! 101427: p->pModule->xDisconnect(p);
! 101428: }
! 101429: sqlite3DbFree(db, pVTab);
! 101430: }
! 101431: }
! 101432:
! 101433: /*
! 101434: ** Table p is a virtual table. This function moves all elements in the
! 101435: ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
! 101436: ** database connections to be disconnected at the next opportunity.
! 101437: ** Except, if argument db is not NULL, then the entry associated with
! 101438: ** connection db is left in the p->pVTable list.
! 101439: */
! 101440: static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
! 101441: VTable *pRet = 0;
! 101442: VTable *pVTable = p->pVTable;
! 101443: p->pVTable = 0;
! 101444:
! 101445: /* Assert that the mutex (if any) associated with the BtShared database
! 101446: ** that contains table p is held by the caller. See header comments
! 101447: ** above function sqlite3VtabUnlockList() for an explanation of why
! 101448: ** this makes it safe to access the sqlite3.pDisconnect list of any
! 101449: ** database connection that may have an entry in the p->pVTable list.
! 101450: */
! 101451: assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
! 101452:
! 101453: while( pVTable ){
! 101454: sqlite3 *db2 = pVTable->db;
! 101455: VTable *pNext = pVTable->pNext;
! 101456: assert( db2 );
! 101457: if( db2==db ){
! 101458: pRet = pVTable;
! 101459: p->pVTable = pRet;
! 101460: pRet->pNext = 0;
! 101461: }else{
! 101462: pVTable->pNext = db2->pDisconnect;
! 101463: db2->pDisconnect = pVTable;
! 101464: }
! 101465: pVTable = pNext;
! 101466: }
! 101467:
! 101468: assert( !db || pRet );
! 101469: return pRet;
! 101470: }
! 101471:
! 101472:
! 101473: /*
! 101474: ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
! 101475: **
! 101476: ** This function may only be called when the mutexes associated with all
! 101477: ** shared b-tree databases opened using connection db are held by the
! 101478: ** caller. This is done to protect the sqlite3.pDisconnect list. The
! 101479: ** sqlite3.pDisconnect list is accessed only as follows:
! 101480: **
! 101481: ** 1) By this function. In this case, all BtShared mutexes and the mutex
! 101482: ** associated with the database handle itself must be held.
! 101483: **
! 101484: ** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
! 101485: ** the sqlite3.pDisconnect list. In this case either the BtShared mutex
! 101486: ** associated with the database the virtual table is stored in is held
! 101487: ** or, if the virtual table is stored in a non-sharable database, then
! 101488: ** the database handle mutex is held.
! 101489: **
! 101490: ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
! 101491: ** by multiple threads. It is thread-safe.
! 101492: */
! 101493: SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
! 101494: VTable *p = db->pDisconnect;
! 101495: db->pDisconnect = 0;
! 101496:
! 101497: assert( sqlite3BtreeHoldsAllMutexes(db) );
! 101498: assert( sqlite3_mutex_held(db->mutex) );
! 101499:
! 101500: if( p ){
! 101501: sqlite3ExpirePreparedStatements(db);
! 101502: do {
! 101503: VTable *pNext = p->pNext;
! 101504: sqlite3VtabUnlock(p);
! 101505: p = pNext;
! 101506: }while( p );
! 101507: }
! 101508: }
! 101509:
! 101510: /*
! 101511: ** Clear any and all virtual-table information from the Table record.
! 101512: ** This routine is called, for example, just before deleting the Table
! 101513: ** record.
! 101514: **
! 101515: ** Since it is a virtual-table, the Table structure contains a pointer
! 101516: ** to the head of a linked list of VTable structures. Each VTable
! 101517: ** structure is associated with a single sqlite3* user of the schema.
! 101518: ** The reference count of the VTable structure associated with database
! 101519: ** connection db is decremented immediately (which may lead to the
! 101520: ** structure being xDisconnected and free). Any other VTable structures
! 101521: ** in the list are moved to the sqlite3.pDisconnect list of the associated
! 101522: ** database connection.
! 101523: */
! 101524: SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
! 101525: if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
! 101526: if( p->azModuleArg ){
! 101527: int i;
! 101528: for(i=0; i<p->nModuleArg; i++){
! 101529: sqlite3DbFree(db, p->azModuleArg[i]);
! 101530: }
! 101531: sqlite3DbFree(db, p->azModuleArg);
! 101532: }
! 101533: }
! 101534:
! 101535: /*
! 101536: ** Add a new module argument to pTable->azModuleArg[].
! 101537: ** The string is not copied - the pointer is stored. The
! 101538: ** string will be freed automatically when the table is
! 101539: ** deleted.
! 101540: */
! 101541: static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
! 101542: int i = pTable->nModuleArg++;
! 101543: int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
! 101544: char **azModuleArg;
! 101545: azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
! 101546: if( azModuleArg==0 ){
! 101547: int j;
! 101548: for(j=0; j<i; j++){
! 101549: sqlite3DbFree(db, pTable->azModuleArg[j]);
! 101550: }
! 101551: sqlite3DbFree(db, zArg);
! 101552: sqlite3DbFree(db, pTable->azModuleArg);
! 101553: pTable->nModuleArg = 0;
! 101554: }else{
! 101555: azModuleArg[i] = zArg;
! 101556: azModuleArg[i+1] = 0;
! 101557: }
! 101558: pTable->azModuleArg = azModuleArg;
! 101559: }
! 101560:
! 101561: /*
! 101562: ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
! 101563: ** statement. The module name has been parsed, but the optional list
! 101564: ** of parameters that follow the module name are still pending.
! 101565: */
! 101566: SQLITE_PRIVATE void sqlite3VtabBeginParse(
! 101567: Parse *pParse, /* Parsing context */
! 101568: Token *pName1, /* Name of new table, or database name */
! 101569: Token *pName2, /* Name of new table or NULL */
! 101570: Token *pModuleName /* Name of the module for the virtual table */
! 101571: ){
! 101572: int iDb; /* The database the table is being created in */
! 101573: Table *pTable; /* The new virtual table */
! 101574: sqlite3 *db; /* Database connection */
! 101575:
! 101576: sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
! 101577: pTable = pParse->pNewTable;
! 101578: if( pTable==0 ) return;
! 101579: assert( 0==pTable->pIndex );
! 101580:
! 101581: db = pParse->db;
! 101582: iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
! 101583: assert( iDb>=0 );
! 101584:
! 101585: pTable->tabFlags |= TF_Virtual;
! 101586: pTable->nModuleArg = 0;
! 101587: addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
! 101588: addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
! 101589: addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
! 101590: pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
! 101591:
! 101592: #ifndef SQLITE_OMIT_AUTHORIZATION
! 101593: /* Creating a virtual table invokes the authorization callback twice.
! 101594: ** The first invocation, to obtain permission to INSERT a row into the
! 101595: ** sqlite_master table, has already been made by sqlite3StartTable().
! 101596: ** The second call, to obtain permission to create the table, is made now.
! 101597: */
! 101598: if( pTable->azModuleArg ){
! 101599: sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
! 101600: pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
! 101601: }
! 101602: #endif
! 101603: }
! 101604:
! 101605: /*
! 101606: ** This routine takes the module argument that has been accumulating
! 101607: ** in pParse->zArg[] and appends it to the list of arguments on the
! 101608: ** virtual table currently under construction in pParse->pTable.
! 101609: */
! 101610: static void addArgumentToVtab(Parse *pParse){
! 101611: if( pParse->sArg.z && ALWAYS(pParse->pNewTable) ){
! 101612: const char *z = (const char*)pParse->sArg.z;
! 101613: int n = pParse->sArg.n;
! 101614: sqlite3 *db = pParse->db;
! 101615: addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
! 101616: }
! 101617: }
! 101618:
! 101619: /*
! 101620: ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
! 101621: ** has been completely parsed.
! 101622: */
! 101623: SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
! 101624: Table *pTab = pParse->pNewTable; /* The table being constructed */
! 101625: sqlite3 *db = pParse->db; /* The database connection */
! 101626:
! 101627: if( pTab==0 ) return;
! 101628: addArgumentToVtab(pParse);
! 101629: pParse->sArg.z = 0;
! 101630: if( pTab->nModuleArg<1 ) return;
! 101631:
! 101632: /* If the CREATE VIRTUAL TABLE statement is being entered for the
! 101633: ** first time (in other words if the virtual table is actually being
! 101634: ** created now instead of just being read out of sqlite_master) then
! 101635: ** do additional initialization work and store the statement text
! 101636: ** in the sqlite_master table.
! 101637: */
! 101638: if( !db->init.busy ){
! 101639: char *zStmt;
! 101640: char *zWhere;
! 101641: int iDb;
! 101642: Vdbe *v;
! 101643:
! 101644: /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
! 101645: if( pEnd ){
! 101646: pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
! 101647: }
! 101648: zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
! 101649:
! 101650: /* A slot for the record has already been allocated in the
! 101651: ** SQLITE_MASTER table. We just need to update that slot with all
! 101652: ** the information we've collected.
! 101653: **
! 101654: ** The VM register number pParse->regRowid holds the rowid of an
! 101655: ** entry in the sqlite_master table tht was created for this vtab
! 101656: ** by sqlite3StartTable().
! 101657: */
! 101658: iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 101659: sqlite3NestedParse(pParse,
! 101660: "UPDATE %Q.%s "
! 101661: "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
! 101662: "WHERE rowid=#%d",
! 101663: db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
! 101664: pTab->zName,
! 101665: pTab->zName,
! 101666: zStmt,
! 101667: pParse->regRowid
! 101668: );
! 101669: sqlite3DbFree(db, zStmt);
! 101670: v = sqlite3GetVdbe(pParse);
! 101671: sqlite3ChangeCookie(pParse, iDb);
! 101672:
! 101673: sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
! 101674: zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
! 101675: sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
! 101676: sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
! 101677: pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
! 101678: }
! 101679:
! 101680: /* If we are rereading the sqlite_master table create the in-memory
! 101681: ** record of the table. The xConnect() method is not called until
! 101682: ** the first time the virtual table is used in an SQL statement. This
! 101683: ** allows a schema that contains virtual tables to be loaded before
! 101684: ** the required virtual table implementations are registered. */
! 101685: else {
! 101686: Table *pOld;
! 101687: Schema *pSchema = pTab->pSchema;
! 101688: const char *zName = pTab->zName;
! 101689: int nName = sqlite3Strlen30(zName);
! 101690: assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
! 101691: pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
! 101692: if( pOld ){
! 101693: db->mallocFailed = 1;
! 101694: assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
! 101695: return;
! 101696: }
! 101697: pParse->pNewTable = 0;
! 101698: }
! 101699: }
! 101700:
! 101701: /*
! 101702: ** The parser calls this routine when it sees the first token
! 101703: ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
! 101704: */
! 101705: SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
! 101706: addArgumentToVtab(pParse);
! 101707: pParse->sArg.z = 0;
! 101708: pParse->sArg.n = 0;
! 101709: }
! 101710:
! 101711: /*
! 101712: ** The parser calls this routine for each token after the first token
! 101713: ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
! 101714: */
! 101715: SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
! 101716: Token *pArg = &pParse->sArg;
! 101717: if( pArg->z==0 ){
! 101718: pArg->z = p->z;
! 101719: pArg->n = p->n;
! 101720: }else{
! 101721: assert(pArg->z < p->z);
! 101722: pArg->n = (int)(&p->z[p->n] - pArg->z);
! 101723: }
! 101724: }
! 101725:
! 101726: /*
! 101727: ** Invoke a virtual table constructor (either xCreate or xConnect). The
! 101728: ** pointer to the function to invoke is passed as the fourth parameter
! 101729: ** to this procedure.
! 101730: */
! 101731: static int vtabCallConstructor(
! 101732: sqlite3 *db,
! 101733: Table *pTab,
! 101734: Module *pMod,
! 101735: int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
! 101736: char **pzErr
! 101737: ){
! 101738: VtabCtx sCtx;
! 101739: VTable *pVTable;
! 101740: int rc;
! 101741: const char *const*azArg = (const char *const*)pTab->azModuleArg;
! 101742: int nArg = pTab->nModuleArg;
! 101743: char *zErr = 0;
! 101744: char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
! 101745:
! 101746: if( !zModuleName ){
! 101747: return SQLITE_NOMEM;
! 101748: }
! 101749:
! 101750: pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
! 101751: if( !pVTable ){
! 101752: sqlite3DbFree(db, zModuleName);
! 101753: return SQLITE_NOMEM;
! 101754: }
! 101755: pVTable->db = db;
! 101756: pVTable->pMod = pMod;
! 101757:
! 101758: /* Invoke the virtual table constructor */
! 101759: assert( &db->pVtabCtx );
! 101760: assert( xConstruct );
! 101761: sCtx.pTab = pTab;
! 101762: sCtx.pVTable = pVTable;
! 101763: db->pVtabCtx = &sCtx;
! 101764: rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
! 101765: db->pVtabCtx = 0;
! 101766: if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
! 101767:
! 101768: if( SQLITE_OK!=rc ){
! 101769: if( zErr==0 ){
! 101770: *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
! 101771: }else {
! 101772: *pzErr = sqlite3MPrintf(db, "%s", zErr);
! 101773: sqlite3_free(zErr);
! 101774: }
! 101775: sqlite3DbFree(db, pVTable);
! 101776: }else if( ALWAYS(pVTable->pVtab) ){
! 101777: /* Justification of ALWAYS(): A correct vtab constructor must allocate
! 101778: ** the sqlite3_vtab object if successful. */
! 101779: pVTable->pVtab->pModule = pMod->pModule;
! 101780: pVTable->nRef = 1;
! 101781: if( sCtx.pTab ){
! 101782: const char *zFormat = "vtable constructor did not declare schema: %s";
! 101783: *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
! 101784: sqlite3VtabUnlock(pVTable);
! 101785: rc = SQLITE_ERROR;
! 101786: }else{
! 101787: int iCol;
! 101788: /* If everything went according to plan, link the new VTable structure
! 101789: ** into the linked list headed by pTab->pVTable. Then loop through the
! 101790: ** columns of the table to see if any of them contain the token "hidden".
! 101791: ** If so, set the Column.isHidden flag and remove the token from
! 101792: ** the type string. */
! 101793: pVTable->pNext = pTab->pVTable;
! 101794: pTab->pVTable = pVTable;
! 101795:
! 101796: for(iCol=0; iCol<pTab->nCol; iCol++){
! 101797: char *zType = pTab->aCol[iCol].zType;
! 101798: int nType;
! 101799: int i = 0;
! 101800: if( !zType ) continue;
! 101801: nType = sqlite3Strlen30(zType);
! 101802: if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
! 101803: for(i=0; i<nType; i++){
! 101804: if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
! 101805: && (zType[i+7]=='\0' || zType[i+7]==' ')
! 101806: ){
! 101807: i++;
! 101808: break;
! 101809: }
! 101810: }
! 101811: }
! 101812: if( i<nType ){
! 101813: int j;
! 101814: int nDel = 6 + (zType[i+6] ? 1 : 0);
! 101815: for(j=i; (j+nDel)<=nType; j++){
! 101816: zType[j] = zType[j+nDel];
! 101817: }
! 101818: if( zType[i]=='\0' && i>0 ){
! 101819: assert(zType[i-1]==' ');
! 101820: zType[i-1] = '\0';
! 101821: }
! 101822: pTab->aCol[iCol].isHidden = 1;
! 101823: }
! 101824: }
! 101825: }
! 101826: }
! 101827:
! 101828: sqlite3DbFree(db, zModuleName);
! 101829: return rc;
! 101830: }
! 101831:
! 101832: /*
! 101833: ** This function is invoked by the parser to call the xConnect() method
! 101834: ** of the virtual table pTab. If an error occurs, an error code is returned
! 101835: ** and an error left in pParse.
! 101836: **
! 101837: ** This call is a no-op if table pTab is not a virtual table.
! 101838: */
! 101839: SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
! 101840: sqlite3 *db = pParse->db;
! 101841: const char *zMod;
! 101842: Module *pMod;
! 101843: int rc;
! 101844:
! 101845: assert( pTab );
! 101846: if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
! 101847: return SQLITE_OK;
! 101848: }
! 101849:
! 101850: /* Locate the required virtual table module */
! 101851: zMod = pTab->azModuleArg[0];
! 101852: pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
! 101853:
! 101854: if( !pMod ){
! 101855: const char *zModule = pTab->azModuleArg[0];
! 101856: sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
! 101857: rc = SQLITE_ERROR;
! 101858: }else{
! 101859: char *zErr = 0;
! 101860: rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
! 101861: if( rc!=SQLITE_OK ){
! 101862: sqlite3ErrorMsg(pParse, "%s", zErr);
! 101863: }
! 101864: sqlite3DbFree(db, zErr);
! 101865: }
! 101866:
! 101867: return rc;
! 101868: }
! 101869: /*
! 101870: ** Grow the db->aVTrans[] array so that there is room for at least one
! 101871: ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
! 101872: */
! 101873: static int growVTrans(sqlite3 *db){
! 101874: const int ARRAY_INCR = 5;
! 101875:
! 101876: /* Grow the sqlite3.aVTrans array if required */
! 101877: if( (db->nVTrans%ARRAY_INCR)==0 ){
! 101878: VTable **aVTrans;
! 101879: int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
! 101880: aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
! 101881: if( !aVTrans ){
! 101882: return SQLITE_NOMEM;
! 101883: }
! 101884: memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
! 101885: db->aVTrans = aVTrans;
! 101886: }
! 101887:
! 101888: return SQLITE_OK;
! 101889: }
! 101890:
! 101891: /*
! 101892: ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
! 101893: ** have already been reserved using growVTrans().
! 101894: */
! 101895: static void addToVTrans(sqlite3 *db, VTable *pVTab){
! 101896: /* Add pVtab to the end of sqlite3.aVTrans */
! 101897: db->aVTrans[db->nVTrans++] = pVTab;
! 101898: sqlite3VtabLock(pVTab);
! 101899: }
! 101900:
! 101901: /*
! 101902: ** This function is invoked by the vdbe to call the xCreate method
! 101903: ** of the virtual table named zTab in database iDb.
! 101904: **
! 101905: ** If an error occurs, *pzErr is set to point an an English language
! 101906: ** description of the error and an SQLITE_XXX error code is returned.
! 101907: ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
! 101908: */
! 101909: SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
! 101910: int rc = SQLITE_OK;
! 101911: Table *pTab;
! 101912: Module *pMod;
! 101913: const char *zMod;
! 101914:
! 101915: pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
! 101916: assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
! 101917:
! 101918: /* Locate the required virtual table module */
! 101919: zMod = pTab->azModuleArg[0];
! 101920: pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
! 101921:
! 101922: /* If the module has been registered and includes a Create method,
! 101923: ** invoke it now. If the module has not been registered, return an
! 101924: ** error. Otherwise, do nothing.
! 101925: */
! 101926: if( !pMod ){
! 101927: *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
! 101928: rc = SQLITE_ERROR;
! 101929: }else{
! 101930: rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
! 101931: }
! 101932:
! 101933: /* Justification of ALWAYS(): The xConstructor method is required to
! 101934: ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
! 101935: if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
! 101936: rc = growVTrans(db);
! 101937: if( rc==SQLITE_OK ){
! 101938: addToVTrans(db, sqlite3GetVTable(db, pTab));
! 101939: }
! 101940: }
! 101941:
! 101942: return rc;
! 101943: }
! 101944:
! 101945: /*
! 101946: ** This function is used to set the schema of a virtual table. It is only
! 101947: ** valid to call this function from within the xCreate() or xConnect() of a
! 101948: ** virtual table module.
! 101949: */
! 101950: SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
! 101951: Parse *pParse;
! 101952:
! 101953: int rc = SQLITE_OK;
! 101954: Table *pTab;
! 101955: char *zErr = 0;
! 101956:
! 101957: sqlite3_mutex_enter(db->mutex);
! 101958: if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
! 101959: sqlite3Error(db, SQLITE_MISUSE, 0);
! 101960: sqlite3_mutex_leave(db->mutex);
! 101961: return SQLITE_MISUSE_BKPT;
! 101962: }
! 101963: assert( (pTab->tabFlags & TF_Virtual)!=0 );
! 101964:
! 101965: pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
! 101966: if( pParse==0 ){
! 101967: rc = SQLITE_NOMEM;
! 101968: }else{
! 101969: pParse->declareVtab = 1;
! 101970: pParse->db = db;
! 101971: pParse->nQueryLoop = 1;
! 101972:
! 101973: if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
! 101974: && pParse->pNewTable
! 101975: && !db->mallocFailed
! 101976: && !pParse->pNewTable->pSelect
! 101977: && (pParse->pNewTable->tabFlags & TF_Virtual)==0
! 101978: ){
! 101979: if( !pTab->aCol ){
! 101980: pTab->aCol = pParse->pNewTable->aCol;
! 101981: pTab->nCol = pParse->pNewTable->nCol;
! 101982: pParse->pNewTable->nCol = 0;
! 101983: pParse->pNewTable->aCol = 0;
! 101984: }
! 101985: db->pVtabCtx->pTab = 0;
! 101986: }else{
! 101987: sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
! 101988: sqlite3DbFree(db, zErr);
! 101989: rc = SQLITE_ERROR;
! 101990: }
! 101991: pParse->declareVtab = 0;
! 101992:
! 101993: if( pParse->pVdbe ){
! 101994: sqlite3VdbeFinalize(pParse->pVdbe);
! 101995: }
! 101996: sqlite3DeleteTable(db, pParse->pNewTable);
! 101997: sqlite3StackFree(db, pParse);
! 101998: }
! 101999:
! 102000: assert( (rc&0xff)==rc );
! 102001: rc = sqlite3ApiExit(db, rc);
! 102002: sqlite3_mutex_leave(db->mutex);
! 102003: return rc;
! 102004: }
! 102005:
! 102006: /*
! 102007: ** This function is invoked by the vdbe to call the xDestroy method
! 102008: ** of the virtual table named zTab in database iDb. This occurs
! 102009: ** when a DROP TABLE is mentioned.
! 102010: **
! 102011: ** This call is a no-op if zTab is not a virtual table.
! 102012: */
! 102013: SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
! 102014: int rc = SQLITE_OK;
! 102015: Table *pTab;
! 102016:
! 102017: pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
! 102018: if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
! 102019: VTable *p = vtabDisconnectAll(db, pTab);
! 102020:
! 102021: assert( rc==SQLITE_OK );
! 102022: rc = p->pMod->pModule->xDestroy(p->pVtab);
! 102023:
! 102024: /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
! 102025: if( rc==SQLITE_OK ){
! 102026: assert( pTab->pVTable==p && p->pNext==0 );
! 102027: p->pVtab = 0;
! 102028: pTab->pVTable = 0;
! 102029: sqlite3VtabUnlock(p);
! 102030: }
! 102031: }
! 102032:
! 102033: return rc;
! 102034: }
! 102035:
! 102036: /*
! 102037: ** This function invokes either the xRollback or xCommit method
! 102038: ** of each of the virtual tables in the sqlite3.aVTrans array. The method
! 102039: ** called is identified by the second argument, "offset", which is
! 102040: ** the offset of the method to call in the sqlite3_module structure.
! 102041: **
! 102042: ** The array is cleared after invoking the callbacks.
! 102043: */
! 102044: static void callFinaliser(sqlite3 *db, int offset){
! 102045: int i;
! 102046: if( db->aVTrans ){
! 102047: for(i=0; i<db->nVTrans; i++){
! 102048: VTable *pVTab = db->aVTrans[i];
! 102049: sqlite3_vtab *p = pVTab->pVtab;
! 102050: if( p ){
! 102051: int (*x)(sqlite3_vtab *);
! 102052: x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
! 102053: if( x ) x(p);
! 102054: }
! 102055: pVTab->iSavepoint = 0;
! 102056: sqlite3VtabUnlock(pVTab);
! 102057: }
! 102058: sqlite3DbFree(db, db->aVTrans);
! 102059: db->nVTrans = 0;
! 102060: db->aVTrans = 0;
! 102061: }
! 102062: }
! 102063:
! 102064: /*
! 102065: ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
! 102066: ** array. Return the error code for the first error that occurs, or
! 102067: ** SQLITE_OK if all xSync operations are successful.
! 102068: **
! 102069: ** Set *pzErrmsg to point to a buffer that should be released using
! 102070: ** sqlite3DbFree() containing an error message, if one is available.
! 102071: */
! 102072: SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
! 102073: int i;
! 102074: int rc = SQLITE_OK;
! 102075: VTable **aVTrans = db->aVTrans;
! 102076:
! 102077: db->aVTrans = 0;
! 102078: for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
! 102079: int (*x)(sqlite3_vtab *);
! 102080: sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
! 102081: if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
! 102082: rc = x(pVtab);
! 102083: sqlite3DbFree(db, *pzErrmsg);
! 102084: *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
! 102085: sqlite3_free(pVtab->zErrMsg);
! 102086: }
! 102087: }
! 102088: db->aVTrans = aVTrans;
! 102089: return rc;
! 102090: }
! 102091:
! 102092: /*
! 102093: ** Invoke the xRollback method of all virtual tables in the
! 102094: ** sqlite3.aVTrans array. Then clear the array itself.
! 102095: */
! 102096: SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
! 102097: callFinaliser(db, offsetof(sqlite3_module,xRollback));
! 102098: return SQLITE_OK;
! 102099: }
! 102100:
! 102101: /*
! 102102: ** Invoke the xCommit method of all virtual tables in the
! 102103: ** sqlite3.aVTrans array. Then clear the array itself.
! 102104: */
! 102105: SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
! 102106: callFinaliser(db, offsetof(sqlite3_module,xCommit));
! 102107: return SQLITE_OK;
! 102108: }
! 102109:
! 102110: /*
! 102111: ** If the virtual table pVtab supports the transaction interface
! 102112: ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
! 102113: ** not currently open, invoke the xBegin method now.
! 102114: **
! 102115: ** If the xBegin call is successful, place the sqlite3_vtab pointer
! 102116: ** in the sqlite3.aVTrans array.
! 102117: */
! 102118: SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
! 102119: int rc = SQLITE_OK;
! 102120: const sqlite3_module *pModule;
! 102121:
! 102122: /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
! 102123: ** than zero, then this function is being called from within a
! 102124: ** virtual module xSync() callback. It is illegal to write to
! 102125: ** virtual module tables in this case, so return SQLITE_LOCKED.
! 102126: */
! 102127: if( sqlite3VtabInSync(db) ){
! 102128: return SQLITE_LOCKED;
! 102129: }
! 102130: if( !pVTab ){
! 102131: return SQLITE_OK;
! 102132: }
! 102133: pModule = pVTab->pVtab->pModule;
! 102134:
! 102135: if( pModule->xBegin ){
! 102136: int i;
! 102137:
! 102138: /* If pVtab is already in the aVTrans array, return early */
! 102139: for(i=0; i<db->nVTrans; i++){
! 102140: if( db->aVTrans[i]==pVTab ){
! 102141: return SQLITE_OK;
! 102142: }
! 102143: }
! 102144:
! 102145: /* Invoke the xBegin method. If successful, add the vtab to the
! 102146: ** sqlite3.aVTrans[] array. */
! 102147: rc = growVTrans(db);
! 102148: if( rc==SQLITE_OK ){
! 102149: rc = pModule->xBegin(pVTab->pVtab);
! 102150: if( rc==SQLITE_OK ){
! 102151: addToVTrans(db, pVTab);
! 102152: }
! 102153: }
! 102154: }
! 102155: return rc;
! 102156: }
! 102157:
! 102158: /*
! 102159: ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
! 102160: ** virtual tables that currently have an open transaction. Pass iSavepoint
! 102161: ** as the second argument to the virtual table method invoked.
! 102162: **
! 102163: ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
! 102164: ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
! 102165: ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
! 102166: ** an open transaction is invoked.
! 102167: **
! 102168: ** If any virtual table method returns an error code other than SQLITE_OK,
! 102169: ** processing is abandoned and the error returned to the caller of this
! 102170: ** function immediately. If all calls to virtual table methods are successful,
! 102171: ** SQLITE_OK is returned.
! 102172: */
! 102173: SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
! 102174: int rc = SQLITE_OK;
! 102175:
! 102176: assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
! 102177: assert( iSavepoint>=0 );
! 102178: if( db->aVTrans ){
! 102179: int i;
! 102180: for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
! 102181: VTable *pVTab = db->aVTrans[i];
! 102182: const sqlite3_module *pMod = pVTab->pMod->pModule;
! 102183: if( pVTab->pVtab && pMod->iVersion>=2 ){
! 102184: int (*xMethod)(sqlite3_vtab *, int);
! 102185: switch( op ){
! 102186: case SAVEPOINT_BEGIN:
! 102187: xMethod = pMod->xSavepoint;
! 102188: pVTab->iSavepoint = iSavepoint+1;
! 102189: break;
! 102190: case SAVEPOINT_ROLLBACK:
! 102191: xMethod = pMod->xRollbackTo;
! 102192: break;
! 102193: default:
! 102194: xMethod = pMod->xRelease;
! 102195: break;
! 102196: }
! 102197: if( xMethod && pVTab->iSavepoint>iSavepoint ){
! 102198: rc = xMethod(pVTab->pVtab, iSavepoint);
! 102199: }
! 102200: }
! 102201: }
! 102202: }
! 102203: return rc;
! 102204: }
! 102205:
! 102206: /*
! 102207: ** The first parameter (pDef) is a function implementation. The
! 102208: ** second parameter (pExpr) is the first argument to this function.
! 102209: ** If pExpr is a column in a virtual table, then let the virtual
! 102210: ** table implementation have an opportunity to overload the function.
! 102211: **
! 102212: ** This routine is used to allow virtual table implementations to
! 102213: ** overload MATCH, LIKE, GLOB, and REGEXP operators.
! 102214: **
! 102215: ** Return either the pDef argument (indicating no change) or a
! 102216: ** new FuncDef structure that is marked as ephemeral using the
! 102217: ** SQLITE_FUNC_EPHEM flag.
! 102218: */
! 102219: SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
! 102220: sqlite3 *db, /* Database connection for reporting malloc problems */
! 102221: FuncDef *pDef, /* Function to possibly overload */
! 102222: int nArg, /* Number of arguments to the function */
! 102223: Expr *pExpr /* First argument to the function */
! 102224: ){
! 102225: Table *pTab;
! 102226: sqlite3_vtab *pVtab;
! 102227: sqlite3_module *pMod;
! 102228: void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
! 102229: void *pArg = 0;
! 102230: FuncDef *pNew;
! 102231: int rc = 0;
! 102232: char *zLowerName;
! 102233: unsigned char *z;
! 102234:
! 102235:
! 102236: /* Check to see the left operand is a column in a virtual table */
! 102237: if( NEVER(pExpr==0) ) return pDef;
! 102238: if( pExpr->op!=TK_COLUMN ) return pDef;
! 102239: pTab = pExpr->pTab;
! 102240: if( NEVER(pTab==0) ) return pDef;
! 102241: if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
! 102242: pVtab = sqlite3GetVTable(db, pTab)->pVtab;
! 102243: assert( pVtab!=0 );
! 102244: assert( pVtab->pModule!=0 );
! 102245: pMod = (sqlite3_module *)pVtab->pModule;
! 102246: if( pMod->xFindFunction==0 ) return pDef;
! 102247:
! 102248: /* Call the xFindFunction method on the virtual table implementation
! 102249: ** to see if the implementation wants to overload this function
! 102250: */
! 102251: zLowerName = sqlite3DbStrDup(db, pDef->zName);
! 102252: if( zLowerName ){
! 102253: for(z=(unsigned char*)zLowerName; *z; z++){
! 102254: *z = sqlite3UpperToLower[*z];
! 102255: }
! 102256: rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
! 102257: sqlite3DbFree(db, zLowerName);
! 102258: }
! 102259: if( rc==0 ){
! 102260: return pDef;
! 102261: }
! 102262:
! 102263: /* Create a new ephemeral function definition for the overloaded
! 102264: ** function */
! 102265: pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
! 102266: + sqlite3Strlen30(pDef->zName) + 1);
! 102267: if( pNew==0 ){
! 102268: return pDef;
! 102269: }
! 102270: *pNew = *pDef;
! 102271: pNew->zName = (char *)&pNew[1];
! 102272: memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
! 102273: pNew->xFunc = xFunc;
! 102274: pNew->pUserData = pArg;
! 102275: pNew->flags |= SQLITE_FUNC_EPHEM;
! 102276: return pNew;
! 102277: }
! 102278:
! 102279: /*
! 102280: ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
! 102281: ** array so that an OP_VBegin will get generated for it. Add pTab to the
! 102282: ** array if it is missing. If pTab is already in the array, this routine
! 102283: ** is a no-op.
! 102284: */
! 102285: SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
! 102286: Parse *pToplevel = sqlite3ParseToplevel(pParse);
! 102287: int i, n;
! 102288: Table **apVtabLock;
! 102289:
! 102290: assert( IsVirtual(pTab) );
! 102291: for(i=0; i<pToplevel->nVtabLock; i++){
! 102292: if( pTab==pToplevel->apVtabLock[i] ) return;
! 102293: }
! 102294: n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
! 102295: apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
! 102296: if( apVtabLock ){
! 102297: pToplevel->apVtabLock = apVtabLock;
! 102298: pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
! 102299: }else{
! 102300: pToplevel->db->mallocFailed = 1;
! 102301: }
! 102302: }
! 102303:
! 102304: /*
! 102305: ** Return the ON CONFLICT resolution mode in effect for the virtual
! 102306: ** table update operation currently in progress.
! 102307: **
! 102308: ** The results of this routine are undefined unless it is called from
! 102309: ** within an xUpdate method.
! 102310: */
! 102311: SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
! 102312: static const unsigned char aMap[] = {
! 102313: SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
! 102314: };
! 102315: assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
! 102316: assert( OE_Ignore==4 && OE_Replace==5 );
! 102317: assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
! 102318: return (int)aMap[db->vtabOnConflict-1];
! 102319: }
! 102320:
! 102321: /*
! 102322: ** Call from within the xCreate() or xConnect() methods to provide
! 102323: ** the SQLite core with additional information about the behavior
! 102324: ** of the virtual table being implemented.
! 102325: */
! 102326: SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
! 102327: va_list ap;
! 102328: int rc = SQLITE_OK;
! 102329:
! 102330: sqlite3_mutex_enter(db->mutex);
! 102331:
! 102332: va_start(ap, op);
! 102333: switch( op ){
! 102334: case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
! 102335: VtabCtx *p = db->pVtabCtx;
! 102336: if( !p ){
! 102337: rc = SQLITE_MISUSE_BKPT;
! 102338: }else{
! 102339: assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
! 102340: p->pVTable->bConstraint = (u8)va_arg(ap, int);
! 102341: }
! 102342: break;
! 102343: }
! 102344: default:
! 102345: rc = SQLITE_MISUSE_BKPT;
! 102346: break;
! 102347: }
! 102348: va_end(ap);
! 102349:
! 102350: if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
! 102351: sqlite3_mutex_leave(db->mutex);
! 102352: return rc;
! 102353: }
! 102354:
! 102355: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 102356:
! 102357: /************** End of vtab.c ************************************************/
! 102358: /************** Begin file where.c *******************************************/
! 102359: /*
! 102360: ** 2001 September 15
! 102361: **
! 102362: ** The author disclaims copyright to this source code. In place of
! 102363: ** a legal notice, here is a blessing:
! 102364: **
! 102365: ** May you do good and not evil.
! 102366: ** May you find forgiveness for yourself and forgive others.
! 102367: ** May you share freely, never taking more than you give.
! 102368: **
! 102369: *************************************************************************
! 102370: ** This module contains C code that generates VDBE code used to process
! 102371: ** the WHERE clause of SQL statements. This module is responsible for
! 102372: ** generating the code that loops through a table looking for applicable
! 102373: ** rows. Indices are selected and used to speed the search when doing
! 102374: ** so is applicable. Because this module is responsible for selecting
! 102375: ** indices, you might also think of this module as the "query optimizer".
! 102376: */
! 102377:
! 102378:
! 102379: /*
! 102380: ** Trace output macros
! 102381: */
! 102382: #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
! 102383: SQLITE_PRIVATE int sqlite3WhereTrace = 0;
! 102384: #endif
! 102385: #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
! 102386: # define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X
! 102387: #else
! 102388: # define WHERETRACE(X)
! 102389: #endif
! 102390:
! 102391: /* Forward reference
! 102392: */
! 102393: typedef struct WhereClause WhereClause;
! 102394: typedef struct WhereMaskSet WhereMaskSet;
! 102395: typedef struct WhereOrInfo WhereOrInfo;
! 102396: typedef struct WhereAndInfo WhereAndInfo;
! 102397: typedef struct WhereCost WhereCost;
! 102398:
! 102399: /*
! 102400: ** The query generator uses an array of instances of this structure to
! 102401: ** help it analyze the subexpressions of the WHERE clause. Each WHERE
! 102402: ** clause subexpression is separated from the others by AND operators,
! 102403: ** usually, or sometimes subexpressions separated by OR.
! 102404: **
! 102405: ** All WhereTerms are collected into a single WhereClause structure.
! 102406: ** The following identity holds:
! 102407: **
! 102408: ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
! 102409: **
! 102410: ** When a term is of the form:
! 102411: **
! 102412: ** X <op> <expr>
! 102413: **
! 102414: ** where X is a column name and <op> is one of certain operators,
! 102415: ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
! 102416: ** cursor number and column number for X. WhereTerm.eOperator records
! 102417: ** the <op> using a bitmask encoding defined by WO_xxx below. The
! 102418: ** use of a bitmask encoding for the operator allows us to search
! 102419: ** quickly for terms that match any of several different operators.
! 102420: **
! 102421: ** A WhereTerm might also be two or more subterms connected by OR:
! 102422: **
! 102423: ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
! 102424: **
! 102425: ** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR
! 102426: ** and the WhereTerm.u.pOrInfo field points to auxiliary information that
! 102427: ** is collected about the
! 102428: **
! 102429: ** If a term in the WHERE clause does not match either of the two previous
! 102430: ** categories, then eOperator==0. The WhereTerm.pExpr field is still set
! 102431: ** to the original subexpression content and wtFlags is set up appropriately
! 102432: ** but no other fields in the WhereTerm object are meaningful.
! 102433: **
! 102434: ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
! 102435: ** but they do so indirectly. A single WhereMaskSet structure translates
! 102436: ** cursor number into bits and the translated bit is stored in the prereq
! 102437: ** fields. The translation is used in order to maximize the number of
! 102438: ** bits that will fit in a Bitmask. The VDBE cursor numbers might be
! 102439: ** spread out over the non-negative integers. For example, the cursor
! 102440: ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
! 102441: ** translates these sparse cursor numbers into consecutive integers
! 102442: ** beginning with 0 in order to make the best possible use of the available
! 102443: ** bits in the Bitmask. So, in the example above, the cursor numbers
! 102444: ** would be mapped into integers 0 through 7.
! 102445: **
! 102446: ** The number of terms in a join is limited by the number of bits
! 102447: ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
! 102448: ** is only able to process joins with 64 or fewer tables.
! 102449: */
! 102450: typedef struct WhereTerm WhereTerm;
! 102451: struct WhereTerm {
! 102452: Expr *pExpr; /* Pointer to the subexpression that is this term */
! 102453: int iParent; /* Disable pWC->a[iParent] when this term disabled */
! 102454: int leftCursor; /* Cursor number of X in "X <op> <expr>" */
! 102455: union {
! 102456: int leftColumn; /* Column number of X in "X <op> <expr>" */
! 102457: WhereOrInfo *pOrInfo; /* Extra information if eOperator==WO_OR */
! 102458: WhereAndInfo *pAndInfo; /* Extra information if eOperator==WO_AND */
! 102459: } u;
! 102460: u16 eOperator; /* A WO_xx value describing <op> */
! 102461: u8 wtFlags; /* TERM_xxx bit flags. See below */
! 102462: u8 nChild; /* Number of children that must disable us */
! 102463: WhereClause *pWC; /* The clause this term is part of */
! 102464: Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
! 102465: Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
! 102466: };
! 102467:
! 102468: /*
! 102469: ** Allowed values of WhereTerm.wtFlags
! 102470: */
! 102471: #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */
! 102472: #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */
! 102473: #define TERM_CODED 0x04 /* This term is already coded */
! 102474: #define TERM_COPIED 0x08 /* Has a child */
! 102475: #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
! 102476: #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
! 102477: #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
! 102478: #ifdef SQLITE_ENABLE_STAT3
! 102479: # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
! 102480: #else
! 102481: # define TERM_VNULL 0x00 /* Disabled if not using stat3 */
! 102482: #endif
! 102483:
! 102484: /*
! 102485: ** An instance of the following structure holds all information about a
! 102486: ** WHERE clause. Mostly this is a container for one or more WhereTerms.
! 102487: **
! 102488: ** Explanation of pOuter: For a WHERE clause of the form
! 102489: **
! 102490: ** a AND ((b AND c) OR (d AND e)) AND f
! 102491: **
! 102492: ** There are separate WhereClause objects for the whole clause and for
! 102493: ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
! 102494: ** subclauses points to the WhereClause object for the whole clause.
! 102495: */
! 102496: struct WhereClause {
! 102497: Parse *pParse; /* The parser context */
! 102498: WhereMaskSet *pMaskSet; /* Mapping of table cursor numbers to bitmasks */
! 102499: Bitmask vmask; /* Bitmask identifying virtual table cursors */
! 102500: WhereClause *pOuter; /* Outer conjunction */
! 102501: u8 op; /* Split operator. TK_AND or TK_OR */
! 102502: u16 wctrlFlags; /* Might include WHERE_AND_ONLY */
! 102503: int nTerm; /* Number of terms */
! 102504: int nSlot; /* Number of entries in a[] */
! 102505: WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
! 102506: #if defined(SQLITE_SMALL_STACK)
! 102507: WhereTerm aStatic[1]; /* Initial static space for a[] */
! 102508: #else
! 102509: WhereTerm aStatic[8]; /* Initial static space for a[] */
! 102510: #endif
! 102511: };
! 102512:
! 102513: /*
! 102514: ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
! 102515: ** a dynamically allocated instance of the following structure.
! 102516: */
! 102517: struct WhereOrInfo {
! 102518: WhereClause wc; /* Decomposition into subterms */
! 102519: Bitmask indexable; /* Bitmask of all indexable tables in the clause */
! 102520: };
! 102521:
! 102522: /*
! 102523: ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
! 102524: ** a dynamically allocated instance of the following structure.
! 102525: */
! 102526: struct WhereAndInfo {
! 102527: WhereClause wc; /* The subexpression broken out */
! 102528: };
! 102529:
! 102530: /*
! 102531: ** An instance of the following structure keeps track of a mapping
! 102532: ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
! 102533: **
! 102534: ** The VDBE cursor numbers are small integers contained in
! 102535: ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
! 102536: ** clause, the cursor numbers might not begin with 0 and they might
! 102537: ** contain gaps in the numbering sequence. But we want to make maximum
! 102538: ** use of the bits in our bitmasks. This structure provides a mapping
! 102539: ** from the sparse cursor numbers into consecutive integers beginning
! 102540: ** with 0.
! 102541: **
! 102542: ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
! 102543: ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
! 102544: **
! 102545: ** For example, if the WHERE clause expression used these VDBE
! 102546: ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
! 102547: ** would map those cursor numbers into bits 0 through 5.
! 102548: **
! 102549: ** Note that the mapping is not necessarily ordered. In the example
! 102550: ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
! 102551: ** 57->5, 73->4. Or one of 719 other combinations might be used. It
! 102552: ** does not really matter. What is important is that sparse cursor
! 102553: ** numbers all get mapped into bit numbers that begin with 0 and contain
! 102554: ** no gaps.
! 102555: */
! 102556: struct WhereMaskSet {
! 102557: int n; /* Number of assigned cursor values */
! 102558: int ix[BMS]; /* Cursor assigned to each bit */
! 102559: };
! 102560:
! 102561: /*
! 102562: ** A WhereCost object records a lookup strategy and the estimated
! 102563: ** cost of pursuing that strategy.
! 102564: */
! 102565: struct WhereCost {
! 102566: WherePlan plan; /* The lookup strategy */
! 102567: double rCost; /* Overall cost of pursuing this search strategy */
! 102568: Bitmask used; /* Bitmask of cursors used by this plan */
! 102569: };
! 102570:
! 102571: /*
! 102572: ** Bitmasks for the operators that indices are able to exploit. An
! 102573: ** OR-ed combination of these values can be used when searching for
! 102574: ** terms in the where clause.
! 102575: */
! 102576: #define WO_IN 0x001
! 102577: #define WO_EQ 0x002
! 102578: #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
! 102579: #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
! 102580: #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
! 102581: #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
! 102582: #define WO_MATCH 0x040
! 102583: #define WO_ISNULL 0x080
! 102584: #define WO_OR 0x100 /* Two or more OR-connected terms */
! 102585: #define WO_AND 0x200 /* Two or more AND-connected terms */
! 102586: #define WO_NOOP 0x800 /* This term does not restrict search space */
! 102587:
! 102588: #define WO_ALL 0xfff /* Mask of all possible WO_* values */
! 102589: #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */
! 102590:
! 102591: /*
! 102592: ** Value for wsFlags returned by bestIndex() and stored in
! 102593: ** WhereLevel.wsFlags. These flags determine which search
! 102594: ** strategies are appropriate.
! 102595: **
! 102596: ** The least significant 12 bits is reserved as a mask for WO_ values above.
! 102597: ** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
! 102598: ** But if the table is the right table of a left join, WhereLevel.wsFlags
! 102599: ** is set to WO_IN|WO_EQ. The WhereLevel.wsFlags field can then be used as
! 102600: ** the "op" parameter to findTerm when we are resolving equality constraints.
! 102601: ** ISNULL constraints will then not be used on the right table of a left
! 102602: ** join. Tickets #2177 and #2189.
! 102603: */
! 102604: #define WHERE_ROWID_EQ 0x00001000 /* rowid=EXPR or rowid IN (...) */
! 102605: #define WHERE_ROWID_RANGE 0x00002000 /* rowid<EXPR and/or rowid>EXPR */
! 102606: #define WHERE_COLUMN_EQ 0x00010000 /* x=EXPR or x IN (...) or x IS NULL */
! 102607: #define WHERE_COLUMN_RANGE 0x00020000 /* x<EXPR and/or x>EXPR */
! 102608: #define WHERE_COLUMN_IN 0x00040000 /* x IN (...) */
! 102609: #define WHERE_COLUMN_NULL 0x00080000 /* x IS NULL */
! 102610: #define WHERE_INDEXED 0x000f0000 /* Anything that uses an index */
! 102611: #define WHERE_NOT_FULLSCAN 0x100f3000 /* Does not do a full table scan */
! 102612: #define WHERE_IN_ABLE 0x000f1000 /* Able to support an IN operator */
! 102613: #define WHERE_TOP_LIMIT 0x00100000 /* x<EXPR or x<=EXPR constraint */
! 102614: #define WHERE_BTM_LIMIT 0x00200000 /* x>EXPR or x>=EXPR constraint */
! 102615: #define WHERE_BOTH_LIMIT 0x00300000 /* Both x>EXPR and x<EXPR */
! 102616: #define WHERE_IDX_ONLY 0x00800000 /* Use index only - omit table */
! 102617: #define WHERE_ORDERBY 0x01000000 /* Output will appear in correct order */
! 102618: #define WHERE_REVERSE 0x02000000 /* Scan in reverse order */
! 102619: #define WHERE_UNIQUE 0x04000000 /* Selects no more than one row */
! 102620: #define WHERE_VIRTUALTABLE 0x08000000 /* Use virtual-table processing */
! 102621: #define WHERE_MULTI_OR 0x10000000 /* OR using multiple indices */
! 102622: #define WHERE_TEMP_INDEX 0x20000000 /* Uses an ephemeral index */
! 102623: #define WHERE_DISTINCT 0x40000000 /* Correct order for DISTINCT */
! 102624:
! 102625: /*
! 102626: ** Initialize a preallocated WhereClause structure.
! 102627: */
! 102628: static void whereClauseInit(
! 102629: WhereClause *pWC, /* The WhereClause to be initialized */
! 102630: Parse *pParse, /* The parsing context */
! 102631: WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmasks */
! 102632: u16 wctrlFlags /* Might include WHERE_AND_ONLY */
! 102633: ){
! 102634: pWC->pParse = pParse;
! 102635: pWC->pMaskSet = pMaskSet;
! 102636: pWC->pOuter = 0;
! 102637: pWC->nTerm = 0;
! 102638: pWC->nSlot = ArraySize(pWC->aStatic);
! 102639: pWC->a = pWC->aStatic;
! 102640: pWC->vmask = 0;
! 102641: pWC->wctrlFlags = wctrlFlags;
! 102642: }
! 102643:
! 102644: /* Forward reference */
! 102645: static void whereClauseClear(WhereClause*);
! 102646:
! 102647: /*
! 102648: ** Deallocate all memory associated with a WhereOrInfo object.
! 102649: */
! 102650: static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
! 102651: whereClauseClear(&p->wc);
! 102652: sqlite3DbFree(db, p);
! 102653: }
! 102654:
! 102655: /*
! 102656: ** Deallocate all memory associated with a WhereAndInfo object.
! 102657: */
! 102658: static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
! 102659: whereClauseClear(&p->wc);
! 102660: sqlite3DbFree(db, p);
! 102661: }
! 102662:
! 102663: /*
! 102664: ** Deallocate a WhereClause structure. The WhereClause structure
! 102665: ** itself is not freed. This routine is the inverse of whereClauseInit().
! 102666: */
! 102667: static void whereClauseClear(WhereClause *pWC){
! 102668: int i;
! 102669: WhereTerm *a;
! 102670: sqlite3 *db = pWC->pParse->db;
! 102671: for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
! 102672: if( a->wtFlags & TERM_DYNAMIC ){
! 102673: sqlite3ExprDelete(db, a->pExpr);
! 102674: }
! 102675: if( a->wtFlags & TERM_ORINFO ){
! 102676: whereOrInfoDelete(db, a->u.pOrInfo);
! 102677: }else if( a->wtFlags & TERM_ANDINFO ){
! 102678: whereAndInfoDelete(db, a->u.pAndInfo);
! 102679: }
! 102680: }
! 102681: if( pWC->a!=pWC->aStatic ){
! 102682: sqlite3DbFree(db, pWC->a);
! 102683: }
! 102684: }
! 102685:
! 102686: /*
! 102687: ** Add a single new WhereTerm entry to the WhereClause object pWC.
! 102688: ** The new WhereTerm object is constructed from Expr p and with wtFlags.
! 102689: ** The index in pWC->a[] of the new WhereTerm is returned on success.
! 102690: ** 0 is returned if the new WhereTerm could not be added due to a memory
! 102691: ** allocation error. The memory allocation failure will be recorded in
! 102692: ** the db->mallocFailed flag so that higher-level functions can detect it.
! 102693: **
! 102694: ** This routine will increase the size of the pWC->a[] array as necessary.
! 102695: **
! 102696: ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
! 102697: ** for freeing the expression p is assumed by the WhereClause object pWC.
! 102698: ** This is true even if this routine fails to allocate a new WhereTerm.
! 102699: **
! 102700: ** WARNING: This routine might reallocate the space used to store
! 102701: ** WhereTerms. All pointers to WhereTerms should be invalidated after
! 102702: ** calling this routine. Such pointers may be reinitialized by referencing
! 102703: ** the pWC->a[] array.
! 102704: */
! 102705: static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
! 102706: WhereTerm *pTerm;
! 102707: int idx;
! 102708: testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */
! 102709: if( pWC->nTerm>=pWC->nSlot ){
! 102710: WhereTerm *pOld = pWC->a;
! 102711: sqlite3 *db = pWC->pParse->db;
! 102712: pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
! 102713: if( pWC->a==0 ){
! 102714: if( wtFlags & TERM_DYNAMIC ){
! 102715: sqlite3ExprDelete(db, p);
! 102716: }
! 102717: pWC->a = pOld;
! 102718: return 0;
! 102719: }
! 102720: memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
! 102721: if( pOld!=pWC->aStatic ){
! 102722: sqlite3DbFree(db, pOld);
! 102723: }
! 102724: pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
! 102725: }
! 102726: pTerm = &pWC->a[idx = pWC->nTerm++];
! 102727: pTerm->pExpr = p;
! 102728: pTerm->wtFlags = wtFlags;
! 102729: pTerm->pWC = pWC;
! 102730: pTerm->iParent = -1;
! 102731: return idx;
! 102732: }
! 102733:
! 102734: /*
! 102735: ** This routine identifies subexpressions in the WHERE clause where
! 102736: ** each subexpression is separated by the AND operator or some other
! 102737: ** operator specified in the op parameter. The WhereClause structure
! 102738: ** is filled with pointers to subexpressions. For example:
! 102739: **
! 102740: ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
! 102741: ** \________/ \_______________/ \________________/
! 102742: ** slot[0] slot[1] slot[2]
! 102743: **
! 102744: ** The original WHERE clause in pExpr is unaltered. All this routine
! 102745: ** does is make slot[] entries point to substructure within pExpr.
! 102746: **
! 102747: ** In the previous sentence and in the diagram, "slot[]" refers to
! 102748: ** the WhereClause.a[] array. The slot[] array grows as needed to contain
! 102749: ** all terms of the WHERE clause.
! 102750: */
! 102751: static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){
! 102752: pWC->op = (u8)op;
! 102753: if( pExpr==0 ) return;
! 102754: if( pExpr->op!=op ){
! 102755: whereClauseInsert(pWC, pExpr, 0);
! 102756: }else{
! 102757: whereSplit(pWC, pExpr->pLeft, op);
! 102758: whereSplit(pWC, pExpr->pRight, op);
! 102759: }
! 102760: }
! 102761:
! 102762: /*
! 102763: ** Initialize an expression mask set (a WhereMaskSet object)
! 102764: */
! 102765: #define initMaskSet(P) memset(P, 0, sizeof(*P))
! 102766:
! 102767: /*
! 102768: ** Return the bitmask for the given cursor number. Return 0 if
! 102769: ** iCursor is not in the set.
! 102770: */
! 102771: static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
! 102772: int i;
! 102773: assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
! 102774: for(i=0; i<pMaskSet->n; i++){
! 102775: if( pMaskSet->ix[i]==iCursor ){
! 102776: return ((Bitmask)1)<<i;
! 102777: }
! 102778: }
! 102779: return 0;
! 102780: }
! 102781:
! 102782: /*
! 102783: ** Create a new mask for cursor iCursor.
! 102784: **
! 102785: ** There is one cursor per table in the FROM clause. The number of
! 102786: ** tables in the FROM clause is limited by a test early in the
! 102787: ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
! 102788: ** array will never overflow.
! 102789: */
! 102790: static void createMask(WhereMaskSet *pMaskSet, int iCursor){
! 102791: assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
! 102792: pMaskSet->ix[pMaskSet->n++] = iCursor;
! 102793: }
! 102794:
! 102795: /*
! 102796: ** This routine walks (recursively) an expression tree and generates
! 102797: ** a bitmask indicating which tables are used in that expression
! 102798: ** tree.
! 102799: **
! 102800: ** In order for this routine to work, the calling function must have
! 102801: ** previously invoked sqlite3ResolveExprNames() on the expression. See
! 102802: ** the header comment on that routine for additional information.
! 102803: ** The sqlite3ResolveExprNames() routines looks for column names and
! 102804: ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
! 102805: ** the VDBE cursor number of the table. This routine just has to
! 102806: ** translate the cursor numbers into bitmask values and OR all
! 102807: ** the bitmasks together.
! 102808: */
! 102809: static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
! 102810: static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
! 102811: static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
! 102812: Bitmask mask = 0;
! 102813: if( p==0 ) return 0;
! 102814: if( p->op==TK_COLUMN ){
! 102815: mask = getMask(pMaskSet, p->iTable);
! 102816: return mask;
! 102817: }
! 102818: mask = exprTableUsage(pMaskSet, p->pRight);
! 102819: mask |= exprTableUsage(pMaskSet, p->pLeft);
! 102820: if( ExprHasProperty(p, EP_xIsSelect) ){
! 102821: mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
! 102822: }else{
! 102823: mask |= exprListTableUsage(pMaskSet, p->x.pList);
! 102824: }
! 102825: return mask;
! 102826: }
! 102827: static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
! 102828: int i;
! 102829: Bitmask mask = 0;
! 102830: if( pList ){
! 102831: for(i=0; i<pList->nExpr; i++){
! 102832: mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
! 102833: }
! 102834: }
! 102835: return mask;
! 102836: }
! 102837: static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
! 102838: Bitmask mask = 0;
! 102839: while( pS ){
! 102840: SrcList *pSrc = pS->pSrc;
! 102841: mask |= exprListTableUsage(pMaskSet, pS->pEList);
! 102842: mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
! 102843: mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
! 102844: mask |= exprTableUsage(pMaskSet, pS->pWhere);
! 102845: mask |= exprTableUsage(pMaskSet, pS->pHaving);
! 102846: if( ALWAYS(pSrc!=0) ){
! 102847: int i;
! 102848: for(i=0; i<pSrc->nSrc; i++){
! 102849: mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
! 102850: mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
! 102851: }
! 102852: }
! 102853: pS = pS->pPrior;
! 102854: }
! 102855: return mask;
! 102856: }
! 102857:
! 102858: /*
! 102859: ** Return TRUE if the given operator is one of the operators that is
! 102860: ** allowed for an indexable WHERE clause term. The allowed operators are
! 102861: ** "=", "<", ">", "<=", ">=", and "IN".
! 102862: **
! 102863: ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be
! 102864: ** of one of the following forms: column = expression column > expression
! 102865: ** column >= expression column < expression column <= expression
! 102866: ** expression = column expression > column expression >= column
! 102867: ** expression < column expression <= column column IN
! 102868: ** (expression-list) column IN (subquery) column IS NULL
! 102869: */
! 102870: static int allowedOp(int op){
! 102871: assert( TK_GT>TK_EQ && TK_GT<TK_GE );
! 102872: assert( TK_LT>TK_EQ && TK_LT<TK_GE );
! 102873: assert( TK_LE>TK_EQ && TK_LE<TK_GE );
! 102874: assert( TK_GE==TK_EQ+4 );
! 102875: return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
! 102876: }
! 102877:
! 102878: /*
! 102879: ** Swap two objects of type TYPE.
! 102880: */
! 102881: #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
! 102882:
! 102883: /*
! 102884: ** Commute a comparison operator. Expressions of the form "X op Y"
! 102885: ** are converted into "Y op X".
! 102886: **
! 102887: ** If a collation sequence is associated with either the left or right
! 102888: ** side of the comparison, it remains associated with the same side after
! 102889: ** the commutation. So "Y collate NOCASE op X" becomes
! 102890: ** "X collate NOCASE op Y". This is because any collation sequence on
! 102891: ** the left hand side of a comparison overrides any collation sequence
! 102892: ** attached to the right. For the same reason the EP_ExpCollate flag
! 102893: ** is not commuted.
! 102894: */
! 102895: static void exprCommute(Parse *pParse, Expr *pExpr){
! 102896: u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
! 102897: u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
! 102898: assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
! 102899: pExpr->pRight->pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
! 102900: pExpr->pLeft->pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
! 102901: SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
! 102902: pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
! 102903: pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
! 102904: SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
! 102905: if( pExpr->op>=TK_GT ){
! 102906: assert( TK_LT==TK_GT+2 );
! 102907: assert( TK_GE==TK_LE+2 );
! 102908: assert( TK_GT>TK_EQ );
! 102909: assert( TK_GT<TK_LE );
! 102910: assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
! 102911: pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
! 102912: }
! 102913: }
! 102914:
! 102915: /*
! 102916: ** Translate from TK_xx operator to WO_xx bitmask.
! 102917: */
! 102918: static u16 operatorMask(int op){
! 102919: u16 c;
! 102920: assert( allowedOp(op) );
! 102921: if( op==TK_IN ){
! 102922: c = WO_IN;
! 102923: }else if( op==TK_ISNULL ){
! 102924: c = WO_ISNULL;
! 102925: }else{
! 102926: assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
! 102927: c = (u16)(WO_EQ<<(op-TK_EQ));
! 102928: }
! 102929: assert( op!=TK_ISNULL || c==WO_ISNULL );
! 102930: assert( op!=TK_IN || c==WO_IN );
! 102931: assert( op!=TK_EQ || c==WO_EQ );
! 102932: assert( op!=TK_LT || c==WO_LT );
! 102933: assert( op!=TK_LE || c==WO_LE );
! 102934: assert( op!=TK_GT || c==WO_GT );
! 102935: assert( op!=TK_GE || c==WO_GE );
! 102936: return c;
! 102937: }
! 102938:
! 102939: /*
! 102940: ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
! 102941: ** where X is a reference to the iColumn of table iCur and <op> is one of
! 102942: ** the WO_xx operator codes specified by the op parameter.
! 102943: ** Return a pointer to the term. Return 0 if not found.
! 102944: */
! 102945: static WhereTerm *findTerm(
! 102946: WhereClause *pWC, /* The WHERE clause to be searched */
! 102947: int iCur, /* Cursor number of LHS */
! 102948: int iColumn, /* Column number of LHS */
! 102949: Bitmask notReady, /* RHS must not overlap with this mask */
! 102950: u32 op, /* Mask of WO_xx values describing operator */
! 102951: Index *pIdx /* Must be compatible with this index, if not NULL */
! 102952: ){
! 102953: WhereTerm *pTerm;
! 102954: int k;
! 102955: assert( iCur>=0 );
! 102956: op &= WO_ALL;
! 102957: for(; pWC; pWC=pWC->pOuter){
! 102958: for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
! 102959: if( pTerm->leftCursor==iCur
! 102960: && (pTerm->prereqRight & notReady)==0
! 102961: && pTerm->u.leftColumn==iColumn
! 102962: && (pTerm->eOperator & op)!=0
! 102963: ){
! 102964: if( iColumn>=0 && pIdx && pTerm->eOperator!=WO_ISNULL ){
! 102965: Expr *pX = pTerm->pExpr;
! 102966: CollSeq *pColl;
! 102967: char idxaff;
! 102968: int j;
! 102969: Parse *pParse = pWC->pParse;
! 102970:
! 102971: idxaff = pIdx->pTable->aCol[iColumn].affinity;
! 102972: if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
! 102973:
! 102974: /* Figure out the collation sequence required from an index for
! 102975: ** it to be useful for optimising expression pX. Store this
! 102976: ** value in variable pColl.
! 102977: */
! 102978: assert(pX->pLeft);
! 102979: pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
! 102980: assert(pColl || pParse->nErr);
! 102981:
! 102982: for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
! 102983: if( NEVER(j>=pIdx->nColumn) ) return 0;
! 102984: }
! 102985: if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
! 102986: }
! 102987: return pTerm;
! 102988: }
! 102989: }
! 102990: }
! 102991: return 0;
! 102992: }
! 102993:
! 102994: /* Forward reference */
! 102995: static void exprAnalyze(SrcList*, WhereClause*, int);
! 102996:
! 102997: /*
! 102998: ** Call exprAnalyze on all terms in a WHERE clause.
! 102999: **
! 103000: **
! 103001: */
! 103002: static void exprAnalyzeAll(
! 103003: SrcList *pTabList, /* the FROM clause */
! 103004: WhereClause *pWC /* the WHERE clause to be analyzed */
! 103005: ){
! 103006: int i;
! 103007: for(i=pWC->nTerm-1; i>=0; i--){
! 103008: exprAnalyze(pTabList, pWC, i);
! 103009: }
! 103010: }
! 103011:
! 103012: #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
! 103013: /*
! 103014: ** Check to see if the given expression is a LIKE or GLOB operator that
! 103015: ** can be optimized using inequality constraints. Return TRUE if it is
! 103016: ** so and false if not.
! 103017: **
! 103018: ** In order for the operator to be optimizible, the RHS must be a string
! 103019: ** literal that does not begin with a wildcard.
! 103020: */
! 103021: static int isLikeOrGlob(
! 103022: Parse *pParse, /* Parsing and code generating context */
! 103023: Expr *pExpr, /* Test this expression */
! 103024: Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
! 103025: int *pisComplete, /* True if the only wildcard is % in the last character */
! 103026: int *pnoCase /* True if uppercase is equivalent to lowercase */
! 103027: ){
! 103028: const char *z = 0; /* String on RHS of LIKE operator */
! 103029: Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
! 103030: ExprList *pList; /* List of operands to the LIKE operator */
! 103031: int c; /* One character in z[] */
! 103032: int cnt; /* Number of non-wildcard prefix characters */
! 103033: char wc[3]; /* Wildcard characters */
! 103034: sqlite3 *db = pParse->db; /* Database connection */
! 103035: sqlite3_value *pVal = 0;
! 103036: int op; /* Opcode of pRight */
! 103037:
! 103038: if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
! 103039: return 0;
! 103040: }
! 103041: #ifdef SQLITE_EBCDIC
! 103042: if( *pnoCase ) return 0;
! 103043: #endif
! 103044: pList = pExpr->x.pList;
! 103045: pLeft = pList->a[1].pExpr;
! 103046: if( pLeft->op!=TK_COLUMN || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT ){
! 103047: /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
! 103048: ** be the name of an indexed column with TEXT affinity. */
! 103049: return 0;
! 103050: }
! 103051: assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
! 103052:
! 103053: pRight = pList->a[0].pExpr;
! 103054: op = pRight->op;
! 103055: if( op==TK_REGISTER ){
! 103056: op = pRight->op2;
! 103057: }
! 103058: if( op==TK_VARIABLE ){
! 103059: Vdbe *pReprepare = pParse->pReprepare;
! 103060: int iCol = pRight->iColumn;
! 103061: pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
! 103062: if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
! 103063: z = (char *)sqlite3_value_text(pVal);
! 103064: }
! 103065: sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
! 103066: assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
! 103067: }else if( op==TK_STRING ){
! 103068: z = pRight->u.zToken;
! 103069: }
! 103070: if( z ){
! 103071: cnt = 0;
! 103072: while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
! 103073: cnt++;
! 103074: }
! 103075: if( cnt!=0 && 255!=(u8)z[cnt-1] ){
! 103076: Expr *pPrefix;
! 103077: *pisComplete = c==wc[0] && z[cnt+1]==0;
! 103078: pPrefix = sqlite3Expr(db, TK_STRING, z);
! 103079: if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
! 103080: *ppPrefix = pPrefix;
! 103081: if( op==TK_VARIABLE ){
! 103082: Vdbe *v = pParse->pVdbe;
! 103083: sqlite3VdbeSetVarmask(v, pRight->iColumn);
! 103084: if( *pisComplete && pRight->u.zToken[1] ){
! 103085: /* If the rhs of the LIKE expression is a variable, and the current
! 103086: ** value of the variable means there is no need to invoke the LIKE
! 103087: ** function, then no OP_Variable will be added to the program.
! 103088: ** This causes problems for the sqlite3_bind_parameter_name()
! 103089: ** API. To workaround them, add a dummy OP_Variable here.
! 103090: */
! 103091: int r1 = sqlite3GetTempReg(pParse);
! 103092: sqlite3ExprCodeTarget(pParse, pRight, r1);
! 103093: sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
! 103094: sqlite3ReleaseTempReg(pParse, r1);
! 103095: }
! 103096: }
! 103097: }else{
! 103098: z = 0;
! 103099: }
! 103100: }
! 103101:
! 103102: sqlite3ValueFree(pVal);
! 103103: return (z!=0);
! 103104: }
! 103105: #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
! 103106:
! 103107:
! 103108: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 103109: /*
! 103110: ** Check to see if the given expression is of the form
! 103111: **
! 103112: ** column MATCH expr
! 103113: **
! 103114: ** If it is then return TRUE. If not, return FALSE.
! 103115: */
! 103116: static int isMatchOfColumn(
! 103117: Expr *pExpr /* Test this expression */
! 103118: ){
! 103119: ExprList *pList;
! 103120:
! 103121: if( pExpr->op!=TK_FUNCTION ){
! 103122: return 0;
! 103123: }
! 103124: if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
! 103125: return 0;
! 103126: }
! 103127: pList = pExpr->x.pList;
! 103128: if( pList->nExpr!=2 ){
! 103129: return 0;
! 103130: }
! 103131: if( pList->a[1].pExpr->op != TK_COLUMN ){
! 103132: return 0;
! 103133: }
! 103134: return 1;
! 103135: }
! 103136: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 103137:
! 103138: /*
! 103139: ** If the pBase expression originated in the ON or USING clause of
! 103140: ** a join, then transfer the appropriate markings over to derived.
! 103141: */
! 103142: static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
! 103143: pDerived->flags |= pBase->flags & EP_FromJoin;
! 103144: pDerived->iRightJoinTable = pBase->iRightJoinTable;
! 103145: }
! 103146:
! 103147: #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
! 103148: /*
! 103149: ** Analyze a term that consists of two or more OR-connected
! 103150: ** subterms. So in:
! 103151: **
! 103152: ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
! 103153: ** ^^^^^^^^^^^^^^^^^^^^
! 103154: **
! 103155: ** This routine analyzes terms such as the middle term in the above example.
! 103156: ** A WhereOrTerm object is computed and attached to the term under
! 103157: ** analysis, regardless of the outcome of the analysis. Hence:
! 103158: **
! 103159: ** WhereTerm.wtFlags |= TERM_ORINFO
! 103160: ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
! 103161: **
! 103162: ** The term being analyzed must have two or more of OR-connected subterms.
! 103163: ** A single subterm might be a set of AND-connected sub-subterms.
! 103164: ** Examples of terms under analysis:
! 103165: **
! 103166: ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
! 103167: ** (B) x=expr1 OR expr2=x OR x=expr3
! 103168: ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
! 103169: ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
! 103170: ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
! 103171: **
! 103172: ** CASE 1:
! 103173: **
! 103174: ** If all subterms are of the form T.C=expr for some single column of C
! 103175: ** a single table T (as shown in example B above) then create a new virtual
! 103176: ** term that is an equivalent IN expression. In other words, if the term
! 103177: ** being analyzed is:
! 103178: **
! 103179: ** x = expr1 OR expr2 = x OR x = expr3
! 103180: **
! 103181: ** then create a new virtual term like this:
! 103182: **
! 103183: ** x IN (expr1,expr2,expr3)
! 103184: **
! 103185: ** CASE 2:
! 103186: **
! 103187: ** If all subterms are indexable by a single table T, then set
! 103188: **
! 103189: ** WhereTerm.eOperator = WO_OR
! 103190: ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
! 103191: **
! 103192: ** A subterm is "indexable" if it is of the form
! 103193: ** "T.C <op> <expr>" where C is any column of table T and
! 103194: ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
! 103195: ** A subterm is also indexable if it is an AND of two or more
! 103196: ** subsubterms at least one of which is indexable. Indexable AND
! 103197: ** subterms have their eOperator set to WO_AND and they have
! 103198: ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
! 103199: **
! 103200: ** From another point of view, "indexable" means that the subterm could
! 103201: ** potentially be used with an index if an appropriate index exists.
! 103202: ** This analysis does not consider whether or not the index exists; that
! 103203: ** is something the bestIndex() routine will determine. This analysis
! 103204: ** only looks at whether subterms appropriate for indexing exist.
! 103205: **
! 103206: ** All examples A through E above all satisfy case 2. But if a term
! 103207: ** also statisfies case 1 (such as B) we know that the optimizer will
! 103208: ** always prefer case 1, so in that case we pretend that case 2 is not
! 103209: ** satisfied.
! 103210: **
! 103211: ** It might be the case that multiple tables are indexable. For example,
! 103212: ** (E) above is indexable on tables P, Q, and R.
! 103213: **
! 103214: ** Terms that satisfy case 2 are candidates for lookup by using
! 103215: ** separate indices to find rowids for each subterm and composing
! 103216: ** the union of all rowids using a RowSet object. This is similar
! 103217: ** to "bitmap indices" in other database engines.
! 103218: **
! 103219: ** OTHERWISE:
! 103220: **
! 103221: ** If neither case 1 nor case 2 apply, then leave the eOperator set to
! 103222: ** zero. This term is not useful for search.
! 103223: */
! 103224: static void exprAnalyzeOrTerm(
! 103225: SrcList *pSrc, /* the FROM clause */
! 103226: WhereClause *pWC, /* the complete WHERE clause */
! 103227: int idxTerm /* Index of the OR-term to be analyzed */
! 103228: ){
! 103229: Parse *pParse = pWC->pParse; /* Parser context */
! 103230: sqlite3 *db = pParse->db; /* Database connection */
! 103231: WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
! 103232: Expr *pExpr = pTerm->pExpr; /* The expression of the term */
! 103233: WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */
! 103234: int i; /* Loop counters */
! 103235: WhereClause *pOrWc; /* Breakup of pTerm into subterms */
! 103236: WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
! 103237: WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
! 103238: Bitmask chngToIN; /* Tables that might satisfy case 1 */
! 103239: Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
! 103240:
! 103241: /*
! 103242: ** Break the OR clause into its separate subterms. The subterms are
! 103243: ** stored in a WhereClause structure containing within the WhereOrInfo
! 103244: ** object that is attached to the original OR clause term.
! 103245: */
! 103246: assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
! 103247: assert( pExpr->op==TK_OR );
! 103248: pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
! 103249: if( pOrInfo==0 ) return;
! 103250: pTerm->wtFlags |= TERM_ORINFO;
! 103251: pOrWc = &pOrInfo->wc;
! 103252: whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags);
! 103253: whereSplit(pOrWc, pExpr, TK_OR);
! 103254: exprAnalyzeAll(pSrc, pOrWc);
! 103255: if( db->mallocFailed ) return;
! 103256: assert( pOrWc->nTerm>=2 );
! 103257:
! 103258: /*
! 103259: ** Compute the set of tables that might satisfy cases 1 or 2.
! 103260: */
! 103261: indexable = ~(Bitmask)0;
! 103262: chngToIN = ~(pWC->vmask);
! 103263: for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
! 103264: if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
! 103265: WhereAndInfo *pAndInfo;
! 103266: assert( pOrTerm->eOperator==0 );
! 103267: assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
! 103268: chngToIN = 0;
! 103269: pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
! 103270: if( pAndInfo ){
! 103271: WhereClause *pAndWC;
! 103272: WhereTerm *pAndTerm;
! 103273: int j;
! 103274: Bitmask b = 0;
! 103275: pOrTerm->u.pAndInfo = pAndInfo;
! 103276: pOrTerm->wtFlags |= TERM_ANDINFO;
! 103277: pOrTerm->eOperator = WO_AND;
! 103278: pAndWC = &pAndInfo->wc;
! 103279: whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags);
! 103280: whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
! 103281: exprAnalyzeAll(pSrc, pAndWC);
! 103282: pAndWC->pOuter = pWC;
! 103283: testcase( db->mallocFailed );
! 103284: if( !db->mallocFailed ){
! 103285: for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
! 103286: assert( pAndTerm->pExpr );
! 103287: if( allowedOp(pAndTerm->pExpr->op) ){
! 103288: b |= getMask(pMaskSet, pAndTerm->leftCursor);
! 103289: }
! 103290: }
! 103291: }
! 103292: indexable &= b;
! 103293: }
! 103294: }else if( pOrTerm->wtFlags & TERM_COPIED ){
! 103295: /* Skip this term for now. We revisit it when we process the
! 103296: ** corresponding TERM_VIRTUAL term */
! 103297: }else{
! 103298: Bitmask b;
! 103299: b = getMask(pMaskSet, pOrTerm->leftCursor);
! 103300: if( pOrTerm->wtFlags & TERM_VIRTUAL ){
! 103301: WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
! 103302: b |= getMask(pMaskSet, pOther->leftCursor);
! 103303: }
! 103304: indexable &= b;
! 103305: if( pOrTerm->eOperator!=WO_EQ ){
! 103306: chngToIN = 0;
! 103307: }else{
! 103308: chngToIN &= b;
! 103309: }
! 103310: }
! 103311: }
! 103312:
! 103313: /*
! 103314: ** Record the set of tables that satisfy case 2. The set might be
! 103315: ** empty.
! 103316: */
! 103317: pOrInfo->indexable = indexable;
! 103318: pTerm->eOperator = indexable==0 ? 0 : WO_OR;
! 103319:
! 103320: /*
! 103321: ** chngToIN holds a set of tables that *might* satisfy case 1. But
! 103322: ** we have to do some additional checking to see if case 1 really
! 103323: ** is satisfied.
! 103324: **
! 103325: ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
! 103326: ** that there is no possibility of transforming the OR clause into an
! 103327: ** IN operator because one or more terms in the OR clause contain
! 103328: ** something other than == on a column in the single table. The 1-bit
! 103329: ** case means that every term of the OR clause is of the form
! 103330: ** "table.column=expr" for some single table. The one bit that is set
! 103331: ** will correspond to the common table. We still need to check to make
! 103332: ** sure the same column is used on all terms. The 2-bit case is when
! 103333: ** the all terms are of the form "table1.column=table2.column". It
! 103334: ** might be possible to form an IN operator with either table1.column
! 103335: ** or table2.column as the LHS if either is common to every term of
! 103336: ** the OR clause.
! 103337: **
! 103338: ** Note that terms of the form "table.column1=table.column2" (the
! 103339: ** same table on both sizes of the ==) cannot be optimized.
! 103340: */
! 103341: if( chngToIN ){
! 103342: int okToChngToIN = 0; /* True if the conversion to IN is valid */
! 103343: int iColumn = -1; /* Column index on lhs of IN operator */
! 103344: int iCursor = -1; /* Table cursor common to all terms */
! 103345: int j = 0; /* Loop counter */
! 103346:
! 103347: /* Search for a table and column that appears on one side or the
! 103348: ** other of the == operator in every subterm. That table and column
! 103349: ** will be recorded in iCursor and iColumn. There might not be any
! 103350: ** such table and column. Set okToChngToIN if an appropriate table
! 103351: ** and column is found but leave okToChngToIN false if not found.
! 103352: */
! 103353: for(j=0; j<2 && !okToChngToIN; j++){
! 103354: pOrTerm = pOrWc->a;
! 103355: for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
! 103356: assert( pOrTerm->eOperator==WO_EQ );
! 103357: pOrTerm->wtFlags &= ~TERM_OR_OK;
! 103358: if( pOrTerm->leftCursor==iCursor ){
! 103359: /* This is the 2-bit case and we are on the second iteration and
! 103360: ** current term is from the first iteration. So skip this term. */
! 103361: assert( j==1 );
! 103362: continue;
! 103363: }
! 103364: if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
! 103365: /* This term must be of the form t1.a==t2.b where t2 is in the
! 103366: ** chngToIN set but t1 is not. This term will be either preceeded
! 103367: ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
! 103368: ** and use its inversion. */
! 103369: testcase( pOrTerm->wtFlags & TERM_COPIED );
! 103370: testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
! 103371: assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
! 103372: continue;
! 103373: }
! 103374: iColumn = pOrTerm->u.leftColumn;
! 103375: iCursor = pOrTerm->leftCursor;
! 103376: break;
! 103377: }
! 103378: if( i<0 ){
! 103379: /* No candidate table+column was found. This can only occur
! 103380: ** on the second iteration */
! 103381: assert( j==1 );
! 103382: assert( (chngToIN&(chngToIN-1))==0 );
! 103383: assert( chngToIN==getMask(pMaskSet, iCursor) );
! 103384: break;
! 103385: }
! 103386: testcase( j==1 );
! 103387:
! 103388: /* We have found a candidate table and column. Check to see if that
! 103389: ** table and column is common to every term in the OR clause */
! 103390: okToChngToIN = 1;
! 103391: for(; i>=0 && okToChngToIN; i--, pOrTerm++){
! 103392: assert( pOrTerm->eOperator==WO_EQ );
! 103393: if( pOrTerm->leftCursor!=iCursor ){
! 103394: pOrTerm->wtFlags &= ~TERM_OR_OK;
! 103395: }else if( pOrTerm->u.leftColumn!=iColumn ){
! 103396: okToChngToIN = 0;
! 103397: }else{
! 103398: int affLeft, affRight;
! 103399: /* If the right-hand side is also a column, then the affinities
! 103400: ** of both right and left sides must be such that no type
! 103401: ** conversions are required on the right. (Ticket #2249)
! 103402: */
! 103403: affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
! 103404: affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
! 103405: if( affRight!=0 && affRight!=affLeft ){
! 103406: okToChngToIN = 0;
! 103407: }else{
! 103408: pOrTerm->wtFlags |= TERM_OR_OK;
! 103409: }
! 103410: }
! 103411: }
! 103412: }
! 103413:
! 103414: /* At this point, okToChngToIN is true if original pTerm satisfies
! 103415: ** case 1. In that case, construct a new virtual term that is
! 103416: ** pTerm converted into an IN operator.
! 103417: **
! 103418: ** EV: R-00211-15100
! 103419: */
! 103420: if( okToChngToIN ){
! 103421: Expr *pDup; /* A transient duplicate expression */
! 103422: ExprList *pList = 0; /* The RHS of the IN operator */
! 103423: Expr *pLeft = 0; /* The LHS of the IN operator */
! 103424: Expr *pNew; /* The complete IN operator */
! 103425:
! 103426: for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
! 103427: if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
! 103428: assert( pOrTerm->eOperator==WO_EQ );
! 103429: assert( pOrTerm->leftCursor==iCursor );
! 103430: assert( pOrTerm->u.leftColumn==iColumn );
! 103431: pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
! 103432: pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup);
! 103433: pLeft = pOrTerm->pExpr->pLeft;
! 103434: }
! 103435: assert( pLeft!=0 );
! 103436: pDup = sqlite3ExprDup(db, pLeft, 0);
! 103437: pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
! 103438: if( pNew ){
! 103439: int idxNew;
! 103440: transferJoinMarkings(pNew, pExpr);
! 103441: assert( !ExprHasProperty(pNew, EP_xIsSelect) );
! 103442: pNew->x.pList = pList;
! 103443: idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
! 103444: testcase( idxNew==0 );
! 103445: exprAnalyze(pSrc, pWC, idxNew);
! 103446: pTerm = &pWC->a[idxTerm];
! 103447: pWC->a[idxNew].iParent = idxTerm;
! 103448: pTerm->nChild = 1;
! 103449: }else{
! 103450: sqlite3ExprListDelete(db, pList);
! 103451: }
! 103452: pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */
! 103453: }
! 103454: }
! 103455: }
! 103456: #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
! 103457:
! 103458:
! 103459: /*
! 103460: ** The input to this routine is an WhereTerm structure with only the
! 103461: ** "pExpr" field filled in. The job of this routine is to analyze the
! 103462: ** subexpression and populate all the other fields of the WhereTerm
! 103463: ** structure.
! 103464: **
! 103465: ** If the expression is of the form "<expr> <op> X" it gets commuted
! 103466: ** to the standard form of "X <op> <expr>".
! 103467: **
! 103468: ** If the expression is of the form "X <op> Y" where both X and Y are
! 103469: ** columns, then the original expression is unchanged and a new virtual
! 103470: ** term of the form "Y <op> X" is added to the WHERE clause and
! 103471: ** analyzed separately. The original term is marked with TERM_COPIED
! 103472: ** and the new term is marked with TERM_DYNAMIC (because it's pExpr
! 103473: ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
! 103474: ** is a commuted copy of a prior term.) The original term has nChild=1
! 103475: ** and the copy has idxParent set to the index of the original term.
! 103476: */
! 103477: static void exprAnalyze(
! 103478: SrcList *pSrc, /* the FROM clause */
! 103479: WhereClause *pWC, /* the WHERE clause */
! 103480: int idxTerm /* Index of the term to be analyzed */
! 103481: ){
! 103482: WhereTerm *pTerm; /* The term to be analyzed */
! 103483: WhereMaskSet *pMaskSet; /* Set of table index masks */
! 103484: Expr *pExpr; /* The expression to be analyzed */
! 103485: Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
! 103486: Bitmask prereqAll; /* Prerequesites of pExpr */
! 103487: Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
! 103488: Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
! 103489: int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
! 103490: int noCase = 0; /* LIKE/GLOB distinguishes case */
! 103491: int op; /* Top-level operator. pExpr->op */
! 103492: Parse *pParse = pWC->pParse; /* Parsing context */
! 103493: sqlite3 *db = pParse->db; /* Database connection */
! 103494:
! 103495: if( db->mallocFailed ){
! 103496: return;
! 103497: }
! 103498: pTerm = &pWC->a[idxTerm];
! 103499: pMaskSet = pWC->pMaskSet;
! 103500: pExpr = pTerm->pExpr;
! 103501: prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
! 103502: op = pExpr->op;
! 103503: if( op==TK_IN ){
! 103504: assert( pExpr->pRight==0 );
! 103505: if( ExprHasProperty(pExpr, EP_xIsSelect) ){
! 103506: pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
! 103507: }else{
! 103508: pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
! 103509: }
! 103510: }else if( op==TK_ISNULL ){
! 103511: pTerm->prereqRight = 0;
! 103512: }else{
! 103513: pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
! 103514: }
! 103515: prereqAll = exprTableUsage(pMaskSet, pExpr);
! 103516: if( ExprHasProperty(pExpr, EP_FromJoin) ){
! 103517: Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
! 103518: prereqAll |= x;
! 103519: extraRight = x-1; /* ON clause terms may not be used with an index
! 103520: ** on left table of a LEFT JOIN. Ticket #3015 */
! 103521: }
! 103522: pTerm->prereqAll = prereqAll;
! 103523: pTerm->leftCursor = -1;
! 103524: pTerm->iParent = -1;
! 103525: pTerm->eOperator = 0;
! 103526: if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
! 103527: Expr *pLeft = pExpr->pLeft;
! 103528: Expr *pRight = pExpr->pRight;
! 103529: if( pLeft->op==TK_COLUMN ){
! 103530: pTerm->leftCursor = pLeft->iTable;
! 103531: pTerm->u.leftColumn = pLeft->iColumn;
! 103532: pTerm->eOperator = operatorMask(op);
! 103533: }
! 103534: if( pRight && pRight->op==TK_COLUMN ){
! 103535: WhereTerm *pNew;
! 103536: Expr *pDup;
! 103537: if( pTerm->leftCursor>=0 ){
! 103538: int idxNew;
! 103539: pDup = sqlite3ExprDup(db, pExpr, 0);
! 103540: if( db->mallocFailed ){
! 103541: sqlite3ExprDelete(db, pDup);
! 103542: return;
! 103543: }
! 103544: idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
! 103545: if( idxNew==0 ) return;
! 103546: pNew = &pWC->a[idxNew];
! 103547: pNew->iParent = idxTerm;
! 103548: pTerm = &pWC->a[idxTerm];
! 103549: pTerm->nChild = 1;
! 103550: pTerm->wtFlags |= TERM_COPIED;
! 103551: }else{
! 103552: pDup = pExpr;
! 103553: pNew = pTerm;
! 103554: }
! 103555: exprCommute(pParse, pDup);
! 103556: pLeft = pDup->pLeft;
! 103557: pNew->leftCursor = pLeft->iTable;
! 103558: pNew->u.leftColumn = pLeft->iColumn;
! 103559: testcase( (prereqLeft | extraRight) != prereqLeft );
! 103560: pNew->prereqRight = prereqLeft | extraRight;
! 103561: pNew->prereqAll = prereqAll;
! 103562: pNew->eOperator = operatorMask(pDup->op);
! 103563: }
! 103564: }
! 103565:
! 103566: #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
! 103567: /* If a term is the BETWEEN operator, create two new virtual terms
! 103568: ** that define the range that the BETWEEN implements. For example:
! 103569: **
! 103570: ** a BETWEEN b AND c
! 103571: **
! 103572: ** is converted into:
! 103573: **
! 103574: ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
! 103575: **
! 103576: ** The two new terms are added onto the end of the WhereClause object.
! 103577: ** The new terms are "dynamic" and are children of the original BETWEEN
! 103578: ** term. That means that if the BETWEEN term is coded, the children are
! 103579: ** skipped. Or, if the children are satisfied by an index, the original
! 103580: ** BETWEEN term is skipped.
! 103581: */
! 103582: else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
! 103583: ExprList *pList = pExpr->x.pList;
! 103584: int i;
! 103585: static const u8 ops[] = {TK_GE, TK_LE};
! 103586: assert( pList!=0 );
! 103587: assert( pList->nExpr==2 );
! 103588: for(i=0; i<2; i++){
! 103589: Expr *pNewExpr;
! 103590: int idxNew;
! 103591: pNewExpr = sqlite3PExpr(pParse, ops[i],
! 103592: sqlite3ExprDup(db, pExpr->pLeft, 0),
! 103593: sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
! 103594: idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
! 103595: testcase( idxNew==0 );
! 103596: exprAnalyze(pSrc, pWC, idxNew);
! 103597: pTerm = &pWC->a[idxTerm];
! 103598: pWC->a[idxNew].iParent = idxTerm;
! 103599: }
! 103600: pTerm->nChild = 2;
! 103601: }
! 103602: #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
! 103603:
! 103604: #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
! 103605: /* Analyze a term that is composed of two or more subterms connected by
! 103606: ** an OR operator.
! 103607: */
! 103608: else if( pExpr->op==TK_OR ){
! 103609: assert( pWC->op==TK_AND );
! 103610: exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
! 103611: pTerm = &pWC->a[idxTerm];
! 103612: }
! 103613: #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
! 103614:
! 103615: #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
! 103616: /* Add constraints to reduce the search space on a LIKE or GLOB
! 103617: ** operator.
! 103618: **
! 103619: ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
! 103620: **
! 103621: ** x>='abc' AND x<'abd' AND x LIKE 'abc%'
! 103622: **
! 103623: ** The last character of the prefix "abc" is incremented to form the
! 103624: ** termination condition "abd".
! 103625: */
! 103626: if( pWC->op==TK_AND
! 103627: && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
! 103628: ){
! 103629: Expr *pLeft; /* LHS of LIKE/GLOB operator */
! 103630: Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
! 103631: Expr *pNewExpr1;
! 103632: Expr *pNewExpr2;
! 103633: int idxNew1;
! 103634: int idxNew2;
! 103635: CollSeq *pColl; /* Collating sequence to use */
! 103636:
! 103637: pLeft = pExpr->x.pList->a[1].pExpr;
! 103638: pStr2 = sqlite3ExprDup(db, pStr1, 0);
! 103639: if( !db->mallocFailed ){
! 103640: u8 c, *pC; /* Last character before the first wildcard */
! 103641: pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
! 103642: c = *pC;
! 103643: if( noCase ){
! 103644: /* The point is to increment the last character before the first
! 103645: ** wildcard. But if we increment '@', that will push it into the
! 103646: ** alphabetic range where case conversions will mess up the
! 103647: ** inequality. To avoid this, make sure to also run the full
! 103648: ** LIKE on all candidate expressions by clearing the isComplete flag
! 103649: */
! 103650: if( c=='A'-1 ) isComplete = 0; /* EV: R-64339-08207 */
! 103651:
! 103652:
! 103653: c = sqlite3UpperToLower[c];
! 103654: }
! 103655: *pC = c + 1;
! 103656: }
! 103657: pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY",0);
! 103658: pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
! 103659: sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
! 103660: pStr1, 0);
! 103661: idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
! 103662: testcase( idxNew1==0 );
! 103663: exprAnalyze(pSrc, pWC, idxNew1);
! 103664: pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
! 103665: sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
! 103666: pStr2, 0);
! 103667: idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
! 103668: testcase( idxNew2==0 );
! 103669: exprAnalyze(pSrc, pWC, idxNew2);
! 103670: pTerm = &pWC->a[idxTerm];
! 103671: if( isComplete ){
! 103672: pWC->a[idxNew1].iParent = idxTerm;
! 103673: pWC->a[idxNew2].iParent = idxTerm;
! 103674: pTerm->nChild = 2;
! 103675: }
! 103676: }
! 103677: #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
! 103678:
! 103679: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 103680: /* Add a WO_MATCH auxiliary term to the constraint set if the
! 103681: ** current expression is of the form: column MATCH expr.
! 103682: ** This information is used by the xBestIndex methods of
! 103683: ** virtual tables. The native query optimizer does not attempt
! 103684: ** to do anything with MATCH functions.
! 103685: */
! 103686: if( isMatchOfColumn(pExpr) ){
! 103687: int idxNew;
! 103688: Expr *pRight, *pLeft;
! 103689: WhereTerm *pNewTerm;
! 103690: Bitmask prereqColumn, prereqExpr;
! 103691:
! 103692: pRight = pExpr->x.pList->a[0].pExpr;
! 103693: pLeft = pExpr->x.pList->a[1].pExpr;
! 103694: prereqExpr = exprTableUsage(pMaskSet, pRight);
! 103695: prereqColumn = exprTableUsage(pMaskSet, pLeft);
! 103696: if( (prereqExpr & prereqColumn)==0 ){
! 103697: Expr *pNewExpr;
! 103698: pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
! 103699: 0, sqlite3ExprDup(db, pRight, 0), 0);
! 103700: idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
! 103701: testcase( idxNew==0 );
! 103702: pNewTerm = &pWC->a[idxNew];
! 103703: pNewTerm->prereqRight = prereqExpr;
! 103704: pNewTerm->leftCursor = pLeft->iTable;
! 103705: pNewTerm->u.leftColumn = pLeft->iColumn;
! 103706: pNewTerm->eOperator = WO_MATCH;
! 103707: pNewTerm->iParent = idxTerm;
! 103708: pTerm = &pWC->a[idxTerm];
! 103709: pTerm->nChild = 1;
! 103710: pTerm->wtFlags |= TERM_COPIED;
! 103711: pNewTerm->prereqAll = pTerm->prereqAll;
! 103712: }
! 103713: }
! 103714: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 103715:
! 103716: #ifdef SQLITE_ENABLE_STAT3
! 103717: /* When sqlite_stat3 histogram data is available an operator of the
! 103718: ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
! 103719: ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
! 103720: ** virtual term of that form.
! 103721: **
! 103722: ** Note that the virtual term must be tagged with TERM_VNULL. This
! 103723: ** TERM_VNULL tag will suppress the not-null check at the beginning
! 103724: ** of the loop. Without the TERM_VNULL flag, the not-null check at
! 103725: ** the start of the loop will prevent any results from being returned.
! 103726: */
! 103727: if( pExpr->op==TK_NOTNULL
! 103728: && pExpr->pLeft->op==TK_COLUMN
! 103729: && pExpr->pLeft->iColumn>=0
! 103730: ){
! 103731: Expr *pNewExpr;
! 103732: Expr *pLeft = pExpr->pLeft;
! 103733: int idxNew;
! 103734: WhereTerm *pNewTerm;
! 103735:
! 103736: pNewExpr = sqlite3PExpr(pParse, TK_GT,
! 103737: sqlite3ExprDup(db, pLeft, 0),
! 103738: sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
! 103739:
! 103740: idxNew = whereClauseInsert(pWC, pNewExpr,
! 103741: TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
! 103742: if( idxNew ){
! 103743: pNewTerm = &pWC->a[idxNew];
! 103744: pNewTerm->prereqRight = 0;
! 103745: pNewTerm->leftCursor = pLeft->iTable;
! 103746: pNewTerm->u.leftColumn = pLeft->iColumn;
! 103747: pNewTerm->eOperator = WO_GT;
! 103748: pNewTerm->iParent = idxTerm;
! 103749: pTerm = &pWC->a[idxTerm];
! 103750: pTerm->nChild = 1;
! 103751: pTerm->wtFlags |= TERM_COPIED;
! 103752: pNewTerm->prereqAll = pTerm->prereqAll;
! 103753: }
! 103754: }
! 103755: #endif /* SQLITE_ENABLE_STAT */
! 103756:
! 103757: /* Prevent ON clause terms of a LEFT JOIN from being used to drive
! 103758: ** an index for tables to the left of the join.
! 103759: */
! 103760: pTerm->prereqRight |= extraRight;
! 103761: }
! 103762:
! 103763: /*
! 103764: ** Return TRUE if any of the expressions in pList->a[iFirst...] contain
! 103765: ** a reference to any table other than the iBase table.
! 103766: */
! 103767: static int referencesOtherTables(
! 103768: ExprList *pList, /* Search expressions in ths list */
! 103769: WhereMaskSet *pMaskSet, /* Mapping from tables to bitmaps */
! 103770: int iFirst, /* Be searching with the iFirst-th expression */
! 103771: int iBase /* Ignore references to this table */
! 103772: ){
! 103773: Bitmask allowed = ~getMask(pMaskSet, iBase);
! 103774: while( iFirst<pList->nExpr ){
! 103775: if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
! 103776: return 1;
! 103777: }
! 103778: }
! 103779: return 0;
! 103780: }
! 103781:
! 103782: /*
! 103783: ** This function searches the expression list passed as the second argument
! 103784: ** for an expression of type TK_COLUMN that refers to the same column and
! 103785: ** uses the same collation sequence as the iCol'th column of index pIdx.
! 103786: ** Argument iBase is the cursor number used for the table that pIdx refers
! 103787: ** to.
! 103788: **
! 103789: ** If such an expression is found, its index in pList->a[] is returned. If
! 103790: ** no expression is found, -1 is returned.
! 103791: */
! 103792: static int findIndexCol(
! 103793: Parse *pParse, /* Parse context */
! 103794: ExprList *pList, /* Expression list to search */
! 103795: int iBase, /* Cursor for table associated with pIdx */
! 103796: Index *pIdx, /* Index to match column of */
! 103797: int iCol /* Column of index to match */
! 103798: ){
! 103799: int i;
! 103800: const char *zColl = pIdx->azColl[iCol];
! 103801:
! 103802: for(i=0; i<pList->nExpr; i++){
! 103803: Expr *p = pList->a[i].pExpr;
! 103804: if( p->op==TK_COLUMN
! 103805: && p->iColumn==pIdx->aiColumn[iCol]
! 103806: && p->iTable==iBase
! 103807: ){
! 103808: CollSeq *pColl = sqlite3ExprCollSeq(pParse, p);
! 103809: if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
! 103810: return i;
! 103811: }
! 103812: }
! 103813: }
! 103814:
! 103815: return -1;
! 103816: }
! 103817:
! 103818: /*
! 103819: ** This routine determines if pIdx can be used to assist in processing a
! 103820: ** DISTINCT qualifier. In other words, it tests whether or not using this
! 103821: ** index for the outer loop guarantees that rows with equal values for
! 103822: ** all expressions in the pDistinct list are delivered grouped together.
! 103823: **
! 103824: ** For example, the query
! 103825: **
! 103826: ** SELECT DISTINCT a, b, c FROM tbl WHERE a = ?
! 103827: **
! 103828: ** can benefit from any index on columns "b" and "c".
! 103829: */
! 103830: static int isDistinctIndex(
! 103831: Parse *pParse, /* Parsing context */
! 103832: WhereClause *pWC, /* The WHERE clause */
! 103833: Index *pIdx, /* The index being considered */
! 103834: int base, /* Cursor number for the table pIdx is on */
! 103835: ExprList *pDistinct, /* The DISTINCT expressions */
! 103836: int nEqCol /* Number of index columns with == */
! 103837: ){
! 103838: Bitmask mask = 0; /* Mask of unaccounted for pDistinct exprs */
! 103839: int i; /* Iterator variable */
! 103840:
! 103841: if( pIdx->zName==0 || pDistinct==0 || pDistinct->nExpr>=BMS ) return 0;
! 103842: testcase( pDistinct->nExpr==BMS-1 );
! 103843:
! 103844: /* Loop through all the expressions in the distinct list. If any of them
! 103845: ** are not simple column references, return early. Otherwise, test if the
! 103846: ** WHERE clause contains a "col=X" clause. If it does, the expression
! 103847: ** can be ignored. If it does not, and the column does not belong to the
! 103848: ** same table as index pIdx, return early. Finally, if there is no
! 103849: ** matching "col=X" expression and the column is on the same table as pIdx,
! 103850: ** set the corresponding bit in variable mask.
! 103851: */
! 103852: for(i=0; i<pDistinct->nExpr; i++){
! 103853: WhereTerm *pTerm;
! 103854: Expr *p = pDistinct->a[i].pExpr;
! 103855: if( p->op!=TK_COLUMN ) return 0;
! 103856: pTerm = findTerm(pWC, p->iTable, p->iColumn, ~(Bitmask)0, WO_EQ, 0);
! 103857: if( pTerm ){
! 103858: Expr *pX = pTerm->pExpr;
! 103859: CollSeq *p1 = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
! 103860: CollSeq *p2 = sqlite3ExprCollSeq(pParse, p);
! 103861: if( p1==p2 ) continue;
! 103862: }
! 103863: if( p->iTable!=base ) return 0;
! 103864: mask |= (((Bitmask)1) << i);
! 103865: }
! 103866:
! 103867: for(i=nEqCol; mask && i<pIdx->nColumn; i++){
! 103868: int iExpr = findIndexCol(pParse, pDistinct, base, pIdx, i);
! 103869: if( iExpr<0 ) break;
! 103870: mask &= ~(((Bitmask)1) << iExpr);
! 103871: }
! 103872:
! 103873: return (mask==0);
! 103874: }
! 103875:
! 103876:
! 103877: /*
! 103878: ** Return true if the DISTINCT expression-list passed as the third argument
! 103879: ** is redundant. A DISTINCT list is redundant if the database contains a
! 103880: ** UNIQUE index that guarantees that the result of the query will be distinct
! 103881: ** anyway.
! 103882: */
! 103883: static int isDistinctRedundant(
! 103884: Parse *pParse,
! 103885: SrcList *pTabList,
! 103886: WhereClause *pWC,
! 103887: ExprList *pDistinct
! 103888: ){
! 103889: Table *pTab;
! 103890: Index *pIdx;
! 103891: int i;
! 103892: int iBase;
! 103893:
! 103894: /* If there is more than one table or sub-select in the FROM clause of
! 103895: ** this query, then it will not be possible to show that the DISTINCT
! 103896: ** clause is redundant. */
! 103897: if( pTabList->nSrc!=1 ) return 0;
! 103898: iBase = pTabList->a[0].iCursor;
! 103899: pTab = pTabList->a[0].pTab;
! 103900:
! 103901: /* If any of the expressions is an IPK column on table iBase, then return
! 103902: ** true. Note: The (p->iTable==iBase) part of this test may be false if the
! 103903: ** current SELECT is a correlated sub-query.
! 103904: */
! 103905: for(i=0; i<pDistinct->nExpr; i++){
! 103906: Expr *p = pDistinct->a[i].pExpr;
! 103907: if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
! 103908: }
! 103909:
! 103910: /* Loop through all indices on the table, checking each to see if it makes
! 103911: ** the DISTINCT qualifier redundant. It does so if:
! 103912: **
! 103913: ** 1. The index is itself UNIQUE, and
! 103914: **
! 103915: ** 2. All of the columns in the index are either part of the pDistinct
! 103916: ** list, or else the WHERE clause contains a term of the form "col=X",
! 103917: ** where X is a constant value. The collation sequences of the
! 103918: ** comparison and select-list expressions must match those of the index.
! 103919: */
! 103920: for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
! 103921: if( pIdx->onError==OE_None ) continue;
! 103922: for(i=0; i<pIdx->nColumn; i++){
! 103923: int iCol = pIdx->aiColumn[i];
! 103924: if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx)
! 103925: && 0>findIndexCol(pParse, pDistinct, iBase, pIdx, i)
! 103926: ){
! 103927: break;
! 103928: }
! 103929: }
! 103930: if( i==pIdx->nColumn ){
! 103931: /* This index implies that the DISTINCT qualifier is redundant. */
! 103932: return 1;
! 103933: }
! 103934: }
! 103935:
! 103936: return 0;
! 103937: }
! 103938:
! 103939: /*
! 103940: ** This routine decides if pIdx can be used to satisfy the ORDER BY
! 103941: ** clause. If it can, it returns 1. If pIdx cannot satisfy the
! 103942: ** ORDER BY clause, this routine returns 0.
! 103943: **
! 103944: ** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
! 103945: ** left-most table in the FROM clause of that same SELECT statement and
! 103946: ** the table has a cursor number of "base". pIdx is an index on pTab.
! 103947: **
! 103948: ** nEqCol is the number of columns of pIdx that are used as equality
! 103949: ** constraints. Any of these columns may be missing from the ORDER BY
! 103950: ** clause and the match can still be a success.
! 103951: **
! 103952: ** All terms of the ORDER BY that match against the index must be either
! 103953: ** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE
! 103954: ** index do not need to satisfy this constraint.) The *pbRev value is
! 103955: ** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
! 103956: ** the ORDER BY clause is all ASC.
! 103957: */
! 103958: static int isSortingIndex(
! 103959: Parse *pParse, /* Parsing context */
! 103960: WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
! 103961: Index *pIdx, /* The index we are testing */
! 103962: int base, /* Cursor number for the table to be sorted */
! 103963: ExprList *pOrderBy, /* The ORDER BY clause */
! 103964: int nEqCol, /* Number of index columns with == constraints */
! 103965: int wsFlags, /* Index usages flags */
! 103966: int *pbRev /* Set to 1 if ORDER BY is DESC */
! 103967: ){
! 103968: int i, j; /* Loop counters */
! 103969: int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
! 103970: int nTerm; /* Number of ORDER BY terms */
! 103971: struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
! 103972: sqlite3 *db = pParse->db;
! 103973:
! 103974: if( !pOrderBy ) return 0;
! 103975: if( wsFlags & WHERE_COLUMN_IN ) return 0;
! 103976: if( pIdx->bUnordered ) return 0;
! 103977:
! 103978: nTerm = pOrderBy->nExpr;
! 103979: assert( nTerm>0 );
! 103980:
! 103981: /* Argument pIdx must either point to a 'real' named index structure,
! 103982: ** or an index structure allocated on the stack by bestBtreeIndex() to
! 103983: ** represent the rowid index that is part of every table. */
! 103984: assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
! 103985:
! 103986: /* Match terms of the ORDER BY clause against columns of
! 103987: ** the index.
! 103988: **
! 103989: ** Note that indices have pIdx->nColumn regular columns plus
! 103990: ** one additional column containing the rowid. The rowid column
! 103991: ** of the index is also allowed to match against the ORDER BY
! 103992: ** clause.
! 103993: */
! 103994: for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
! 103995: Expr *pExpr; /* The expression of the ORDER BY pTerm */
! 103996: CollSeq *pColl; /* The collating sequence of pExpr */
! 103997: int termSortOrder; /* Sort order for this term */
! 103998: int iColumn; /* The i-th column of the index. -1 for rowid */
! 103999: int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
! 104000: const char *zColl; /* Name of the collating sequence for i-th index term */
! 104001:
! 104002: pExpr = pTerm->pExpr;
! 104003: if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
! 104004: /* Can not use an index sort on anything that is not a column in the
! 104005: ** left-most table of the FROM clause */
! 104006: break;
! 104007: }
! 104008: pColl = sqlite3ExprCollSeq(pParse, pExpr);
! 104009: if( !pColl ){
! 104010: pColl = db->pDfltColl;
! 104011: }
! 104012: if( pIdx->zName && i<pIdx->nColumn ){
! 104013: iColumn = pIdx->aiColumn[i];
! 104014: if( iColumn==pIdx->pTable->iPKey ){
! 104015: iColumn = -1;
! 104016: }
! 104017: iSortOrder = pIdx->aSortOrder[i];
! 104018: zColl = pIdx->azColl[i];
! 104019: }else{
! 104020: iColumn = -1;
! 104021: iSortOrder = 0;
! 104022: zColl = pColl->zName;
! 104023: }
! 104024: if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
! 104025: /* Term j of the ORDER BY clause does not match column i of the index */
! 104026: if( i<nEqCol ){
! 104027: /* If an index column that is constrained by == fails to match an
! 104028: ** ORDER BY term, that is OK. Just ignore that column of the index
! 104029: */
! 104030: continue;
! 104031: }else if( i==pIdx->nColumn ){
! 104032: /* Index column i is the rowid. All other terms match. */
! 104033: break;
! 104034: }else{
! 104035: /* If an index column fails to match and is not constrained by ==
! 104036: ** then the index cannot satisfy the ORDER BY constraint.
! 104037: */
! 104038: return 0;
! 104039: }
! 104040: }
! 104041: assert( pIdx->aSortOrder!=0 || iColumn==-1 );
! 104042: assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
! 104043: assert( iSortOrder==0 || iSortOrder==1 );
! 104044: termSortOrder = iSortOrder ^ pTerm->sortOrder;
! 104045: if( i>nEqCol ){
! 104046: if( termSortOrder!=sortOrder ){
! 104047: /* Indices can only be used if all ORDER BY terms past the
! 104048: ** equality constraints are all either DESC or ASC. */
! 104049: return 0;
! 104050: }
! 104051: }else{
! 104052: sortOrder = termSortOrder;
! 104053: }
! 104054: j++;
! 104055: pTerm++;
! 104056: if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
! 104057: /* If the indexed column is the primary key and everything matches
! 104058: ** so far and none of the ORDER BY terms to the right reference other
! 104059: ** tables in the join, then we are assured that the index can be used
! 104060: ** to sort because the primary key is unique and so none of the other
! 104061: ** columns will make any difference
! 104062: */
! 104063: j = nTerm;
! 104064: }
! 104065: }
! 104066:
! 104067: *pbRev = sortOrder!=0;
! 104068: if( j>=nTerm ){
! 104069: /* All terms of the ORDER BY clause are covered by this index so
! 104070: ** this index can be used for sorting. */
! 104071: return 1;
! 104072: }
! 104073: if( pIdx->onError!=OE_None && i==pIdx->nColumn
! 104074: && (wsFlags & WHERE_COLUMN_NULL)==0
! 104075: && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
! 104076: /* All terms of this index match some prefix of the ORDER BY clause
! 104077: ** and the index is UNIQUE and no terms on the tail of the ORDER BY
! 104078: ** clause reference other tables in a join. If this is all true then
! 104079: ** the order by clause is superfluous. Not that if the matching
! 104080: ** condition is IS NULL then the result is not necessarily unique
! 104081: ** even on a UNIQUE index, so disallow those cases. */
! 104082: return 1;
! 104083: }
! 104084: return 0;
! 104085: }
! 104086:
! 104087: /*
! 104088: ** Prepare a crude estimate of the logarithm of the input value.
! 104089: ** The results need not be exact. This is only used for estimating
! 104090: ** the total cost of performing operations with O(logN) or O(NlogN)
! 104091: ** complexity. Because N is just a guess, it is no great tragedy if
! 104092: ** logN is a little off.
! 104093: */
! 104094: static double estLog(double N){
! 104095: double logN = 1;
! 104096: double x = 10;
! 104097: while( N>x ){
! 104098: logN += 1;
! 104099: x *= 10;
! 104100: }
! 104101: return logN;
! 104102: }
! 104103:
! 104104: /*
! 104105: ** Two routines for printing the content of an sqlite3_index_info
! 104106: ** structure. Used for testing and debugging only. If neither
! 104107: ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
! 104108: ** are no-ops.
! 104109: */
! 104110: #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
! 104111: static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
! 104112: int i;
! 104113: if( !sqlite3WhereTrace ) return;
! 104114: for(i=0; i<p->nConstraint; i++){
! 104115: sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
! 104116: i,
! 104117: p->aConstraint[i].iColumn,
! 104118: p->aConstraint[i].iTermOffset,
! 104119: p->aConstraint[i].op,
! 104120: p->aConstraint[i].usable);
! 104121: }
! 104122: for(i=0; i<p->nOrderBy; i++){
! 104123: sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
! 104124: i,
! 104125: p->aOrderBy[i].iColumn,
! 104126: p->aOrderBy[i].desc);
! 104127: }
! 104128: }
! 104129: static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
! 104130: int i;
! 104131: if( !sqlite3WhereTrace ) return;
! 104132: for(i=0; i<p->nConstraint; i++){
! 104133: sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
! 104134: i,
! 104135: p->aConstraintUsage[i].argvIndex,
! 104136: p->aConstraintUsage[i].omit);
! 104137: }
! 104138: sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
! 104139: sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
! 104140: sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
! 104141: sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
! 104142: }
! 104143: #else
! 104144: #define TRACE_IDX_INPUTS(A)
! 104145: #define TRACE_IDX_OUTPUTS(A)
! 104146: #endif
! 104147:
! 104148: /*
! 104149: ** Required because bestIndex() is called by bestOrClauseIndex()
! 104150: */
! 104151: static void bestIndex(
! 104152: Parse*, WhereClause*, struct SrcList_item*,
! 104153: Bitmask, Bitmask, ExprList*, WhereCost*);
! 104154:
! 104155: /*
! 104156: ** This routine attempts to find an scanning strategy that can be used
! 104157: ** to optimize an 'OR' expression that is part of a WHERE clause.
! 104158: **
! 104159: ** The table associated with FROM clause term pSrc may be either a
! 104160: ** regular B-Tree table or a virtual table.
! 104161: */
! 104162: static void bestOrClauseIndex(
! 104163: Parse *pParse, /* The parsing context */
! 104164: WhereClause *pWC, /* The WHERE clause */
! 104165: struct SrcList_item *pSrc, /* The FROM clause term to search */
! 104166: Bitmask notReady, /* Mask of cursors not available for indexing */
! 104167: Bitmask notValid, /* Cursors not available for any purpose */
! 104168: ExprList *pOrderBy, /* The ORDER BY clause */
! 104169: WhereCost *pCost /* Lowest cost query plan */
! 104170: ){
! 104171: #ifndef SQLITE_OMIT_OR_OPTIMIZATION
! 104172: const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
! 104173: const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */
! 104174: WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */
! 104175: WhereTerm *pTerm; /* A single term of the WHERE clause */
! 104176:
! 104177: /* The OR-clause optimization is disallowed if the INDEXED BY or
! 104178: ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
! 104179: if( pSrc->notIndexed || pSrc->pIndex!=0 ){
! 104180: return;
! 104181: }
! 104182: if( pWC->wctrlFlags & WHERE_AND_ONLY ){
! 104183: return;
! 104184: }
! 104185:
! 104186: /* Search the WHERE clause terms for a usable WO_OR term. */
! 104187: for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
! 104188: if( pTerm->eOperator==WO_OR
! 104189: && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
! 104190: && (pTerm->u.pOrInfo->indexable & maskSrc)!=0
! 104191: ){
! 104192: WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
! 104193: WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
! 104194: WhereTerm *pOrTerm;
! 104195: int flags = WHERE_MULTI_OR;
! 104196: double rTotal = 0;
! 104197: double nRow = 0;
! 104198: Bitmask used = 0;
! 104199:
! 104200: for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
! 104201: WhereCost sTermCost;
! 104202: WHERETRACE(("... Multi-index OR testing for term %d of %d....\n",
! 104203: (pOrTerm - pOrWC->a), (pTerm - pWC->a)
! 104204: ));
! 104205: if( pOrTerm->eOperator==WO_AND ){
! 104206: WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
! 104207: bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
! 104208: }else if( pOrTerm->leftCursor==iCur ){
! 104209: WhereClause tempWC;
! 104210: tempWC.pParse = pWC->pParse;
! 104211: tempWC.pMaskSet = pWC->pMaskSet;
! 104212: tempWC.pOuter = pWC;
! 104213: tempWC.op = TK_AND;
! 104214: tempWC.a = pOrTerm;
! 104215: tempWC.wctrlFlags = 0;
! 104216: tempWC.nTerm = 1;
! 104217: bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
! 104218: }else{
! 104219: continue;
! 104220: }
! 104221: rTotal += sTermCost.rCost;
! 104222: nRow += sTermCost.plan.nRow;
! 104223: used |= sTermCost.used;
! 104224: if( rTotal>=pCost->rCost ) break;
! 104225: }
! 104226:
! 104227: /* If there is an ORDER BY clause, increase the scan cost to account
! 104228: ** for the cost of the sort. */
! 104229: if( pOrderBy!=0 ){
! 104230: WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
! 104231: rTotal, rTotal+nRow*estLog(nRow)));
! 104232: rTotal += nRow*estLog(nRow);
! 104233: }
! 104234:
! 104235: /* If the cost of scanning using this OR term for optimization is
! 104236: ** less than the current cost stored in pCost, replace the contents
! 104237: ** of pCost. */
! 104238: WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
! 104239: if( rTotal<pCost->rCost ){
! 104240: pCost->rCost = rTotal;
! 104241: pCost->used = used;
! 104242: pCost->plan.nRow = nRow;
! 104243: pCost->plan.wsFlags = flags;
! 104244: pCost->plan.u.pTerm = pTerm;
! 104245: }
! 104246: }
! 104247: }
! 104248: #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
! 104249: }
! 104250:
! 104251: #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
! 104252: /*
! 104253: ** Return TRUE if the WHERE clause term pTerm is of a form where it
! 104254: ** could be used with an index to access pSrc, assuming an appropriate
! 104255: ** index existed.
! 104256: */
! 104257: static int termCanDriveIndex(
! 104258: WhereTerm *pTerm, /* WHERE clause term to check */
! 104259: struct SrcList_item *pSrc, /* Table we are trying to access */
! 104260: Bitmask notReady /* Tables in outer loops of the join */
! 104261: ){
! 104262: char aff;
! 104263: if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
! 104264: if( pTerm->eOperator!=WO_EQ ) return 0;
! 104265: if( (pTerm->prereqRight & notReady)!=0 ) return 0;
! 104266: aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
! 104267: if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
! 104268: return 1;
! 104269: }
! 104270: #endif
! 104271:
! 104272: #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
! 104273: /*
! 104274: ** If the query plan for pSrc specified in pCost is a full table scan
! 104275: ** and indexing is allows (if there is no NOT INDEXED clause) and it
! 104276: ** possible to construct a transient index that would perform better
! 104277: ** than a full table scan even when the cost of constructing the index
! 104278: ** is taken into account, then alter the query plan to use the
! 104279: ** transient index.
! 104280: */
! 104281: static void bestAutomaticIndex(
! 104282: Parse *pParse, /* The parsing context */
! 104283: WhereClause *pWC, /* The WHERE clause */
! 104284: struct SrcList_item *pSrc, /* The FROM clause term to search */
! 104285: Bitmask notReady, /* Mask of cursors that are not available */
! 104286: WhereCost *pCost /* Lowest cost query plan */
! 104287: ){
! 104288: double nTableRow; /* Rows in the input table */
! 104289: double logN; /* log(nTableRow) */
! 104290: double costTempIdx; /* per-query cost of the transient index */
! 104291: WhereTerm *pTerm; /* A single term of the WHERE clause */
! 104292: WhereTerm *pWCEnd; /* End of pWC->a[] */
! 104293: Table *pTable; /* Table tht might be indexed */
! 104294:
! 104295: if( pParse->nQueryLoop<=(double)1 ){
! 104296: /* There is no point in building an automatic index for a single scan */
! 104297: return;
! 104298: }
! 104299: if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
! 104300: /* Automatic indices are disabled at run-time */
! 104301: return;
! 104302: }
! 104303: if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
! 104304: /* We already have some kind of index in use for this query. */
! 104305: return;
! 104306: }
! 104307: if( pSrc->notIndexed ){
! 104308: /* The NOT INDEXED clause appears in the SQL. */
! 104309: return;
! 104310: }
! 104311: if( pSrc->isCorrelated ){
! 104312: /* The source is a correlated sub-query. No point in indexing it. */
! 104313: return;
! 104314: }
! 104315:
! 104316: assert( pParse->nQueryLoop >= (double)1 );
! 104317: pTable = pSrc->pTab;
! 104318: nTableRow = pTable->nRowEst;
! 104319: logN = estLog(nTableRow);
! 104320: costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
! 104321: if( costTempIdx>=pCost->rCost ){
! 104322: /* The cost of creating the transient table would be greater than
! 104323: ** doing the full table scan */
! 104324: return;
! 104325: }
! 104326:
! 104327: /* Search for any equality comparison term */
! 104328: pWCEnd = &pWC->a[pWC->nTerm];
! 104329: for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
! 104330: if( termCanDriveIndex(pTerm, pSrc, notReady) ){
! 104331: WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
! 104332: pCost->rCost, costTempIdx));
! 104333: pCost->rCost = costTempIdx;
! 104334: pCost->plan.nRow = logN + 1;
! 104335: pCost->plan.wsFlags = WHERE_TEMP_INDEX;
! 104336: pCost->used = pTerm->prereqRight;
! 104337: break;
! 104338: }
! 104339: }
! 104340: }
! 104341: #else
! 104342: # define bestAutomaticIndex(A,B,C,D,E) /* no-op */
! 104343: #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
! 104344:
! 104345:
! 104346: #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
! 104347: /*
! 104348: ** Generate code to construct the Index object for an automatic index
! 104349: ** and to set up the WhereLevel object pLevel so that the code generator
! 104350: ** makes use of the automatic index.
! 104351: */
! 104352: static void constructAutomaticIndex(
! 104353: Parse *pParse, /* The parsing context */
! 104354: WhereClause *pWC, /* The WHERE clause */
! 104355: struct SrcList_item *pSrc, /* The FROM clause term to get the next index */
! 104356: Bitmask notReady, /* Mask of cursors that are not available */
! 104357: WhereLevel *pLevel /* Write new index here */
! 104358: ){
! 104359: int nColumn; /* Number of columns in the constructed index */
! 104360: WhereTerm *pTerm; /* A single term of the WHERE clause */
! 104361: WhereTerm *pWCEnd; /* End of pWC->a[] */
! 104362: int nByte; /* Byte of memory needed for pIdx */
! 104363: Index *pIdx; /* Object describing the transient index */
! 104364: Vdbe *v; /* Prepared statement under construction */
! 104365: int addrInit; /* Address of the initialization bypass jump */
! 104366: Table *pTable; /* The table being indexed */
! 104367: KeyInfo *pKeyinfo; /* Key information for the index */
! 104368: int addrTop; /* Top of the index fill loop */
! 104369: int regRecord; /* Register holding an index record */
! 104370: int n; /* Column counter */
! 104371: int i; /* Loop counter */
! 104372: int mxBitCol; /* Maximum column in pSrc->colUsed */
! 104373: CollSeq *pColl; /* Collating sequence to on a column */
! 104374: Bitmask idxCols; /* Bitmap of columns used for indexing */
! 104375: Bitmask extraCols; /* Bitmap of additional columns */
! 104376:
! 104377: /* Generate code to skip over the creation and initialization of the
! 104378: ** transient index on 2nd and subsequent iterations of the loop. */
! 104379: v = pParse->pVdbe;
! 104380: assert( v!=0 );
! 104381: addrInit = sqlite3CodeOnce(pParse);
! 104382:
! 104383: /* Count the number of columns that will be added to the index
! 104384: ** and used to match WHERE clause constraints */
! 104385: nColumn = 0;
! 104386: pTable = pSrc->pTab;
! 104387: pWCEnd = &pWC->a[pWC->nTerm];
! 104388: idxCols = 0;
! 104389: for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
! 104390: if( termCanDriveIndex(pTerm, pSrc, notReady) ){
! 104391: int iCol = pTerm->u.leftColumn;
! 104392: Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
! 104393: testcase( iCol==BMS );
! 104394: testcase( iCol==BMS-1 );
! 104395: if( (idxCols & cMask)==0 ){
! 104396: nColumn++;
! 104397: idxCols |= cMask;
! 104398: }
! 104399: }
! 104400: }
! 104401: assert( nColumn>0 );
! 104402: pLevel->plan.nEq = nColumn;
! 104403:
! 104404: /* Count the number of additional columns needed to create a
! 104405: ** covering index. A "covering index" is an index that contains all
! 104406: ** columns that are needed by the query. With a covering index, the
! 104407: ** original table never needs to be accessed. Automatic indices must
! 104408: ** be a covering index because the index will not be updated if the
! 104409: ** original table changes and the index and table cannot both be used
! 104410: ** if they go out of sync.
! 104411: */
! 104412: extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1)));
! 104413: mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
! 104414: testcase( pTable->nCol==BMS-1 );
! 104415: testcase( pTable->nCol==BMS-2 );
! 104416: for(i=0; i<mxBitCol; i++){
! 104417: if( extraCols & (((Bitmask)1)<<i) ) nColumn++;
! 104418: }
! 104419: if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
! 104420: nColumn += pTable->nCol - BMS + 1;
! 104421: }
! 104422: pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
! 104423:
! 104424: /* Construct the Index object to describe this index */
! 104425: nByte = sizeof(Index);
! 104426: nByte += nColumn*sizeof(int); /* Index.aiColumn */
! 104427: nByte += nColumn*sizeof(char*); /* Index.azColl */
! 104428: nByte += nColumn; /* Index.aSortOrder */
! 104429: pIdx = sqlite3DbMallocZero(pParse->db, nByte);
! 104430: if( pIdx==0 ) return;
! 104431: pLevel->plan.u.pIdx = pIdx;
! 104432: pIdx->azColl = (char**)&pIdx[1];
! 104433: pIdx->aiColumn = (int*)&pIdx->azColl[nColumn];
! 104434: pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
! 104435: pIdx->zName = "auto-index";
! 104436: pIdx->nColumn = nColumn;
! 104437: pIdx->pTable = pTable;
! 104438: n = 0;
! 104439: idxCols = 0;
! 104440: for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
! 104441: if( termCanDriveIndex(pTerm, pSrc, notReady) ){
! 104442: int iCol = pTerm->u.leftColumn;
! 104443: Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
! 104444: if( (idxCols & cMask)==0 ){
! 104445: Expr *pX = pTerm->pExpr;
! 104446: idxCols |= cMask;
! 104447: pIdx->aiColumn[n] = pTerm->u.leftColumn;
! 104448: pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
! 104449: pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
! 104450: n++;
! 104451: }
! 104452: }
! 104453: }
! 104454: assert( (u32)n==pLevel->plan.nEq );
! 104455:
! 104456: /* Add additional columns needed to make the automatic index into
! 104457: ** a covering index */
! 104458: for(i=0; i<mxBitCol; i++){
! 104459: if( extraCols & (((Bitmask)1)<<i) ){
! 104460: pIdx->aiColumn[n] = i;
! 104461: pIdx->azColl[n] = "BINARY";
! 104462: n++;
! 104463: }
! 104464: }
! 104465: if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
! 104466: for(i=BMS-1; i<pTable->nCol; i++){
! 104467: pIdx->aiColumn[n] = i;
! 104468: pIdx->azColl[n] = "BINARY";
! 104469: n++;
! 104470: }
! 104471: }
! 104472: assert( n==nColumn );
! 104473:
! 104474: /* Create the automatic index */
! 104475: pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx);
! 104476: assert( pLevel->iIdxCur>=0 );
! 104477: sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0,
! 104478: (char*)pKeyinfo, P4_KEYINFO_HANDOFF);
! 104479: VdbeComment((v, "for %s", pTable->zName));
! 104480:
! 104481: /* Fill the automatic index with content */
! 104482: addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
! 104483: regRecord = sqlite3GetTempReg(pParse);
! 104484: sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1);
! 104485: sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
! 104486: sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
! 104487: sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
! 104488: sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
! 104489: sqlite3VdbeJumpHere(v, addrTop);
! 104490: sqlite3ReleaseTempReg(pParse, regRecord);
! 104491:
! 104492: /* Jump here when skipping the initialization */
! 104493: sqlite3VdbeJumpHere(v, addrInit);
! 104494: }
! 104495: #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
! 104496:
! 104497: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 104498: /*
! 104499: ** Allocate and populate an sqlite3_index_info structure. It is the
! 104500: ** responsibility of the caller to eventually release the structure
! 104501: ** by passing the pointer returned by this function to sqlite3_free().
! 104502: */
! 104503: static sqlite3_index_info *allocateIndexInfo(
! 104504: Parse *pParse,
! 104505: WhereClause *pWC,
! 104506: struct SrcList_item *pSrc,
! 104507: ExprList *pOrderBy
! 104508: ){
! 104509: int i, j;
! 104510: int nTerm;
! 104511: struct sqlite3_index_constraint *pIdxCons;
! 104512: struct sqlite3_index_orderby *pIdxOrderBy;
! 104513: struct sqlite3_index_constraint_usage *pUsage;
! 104514: WhereTerm *pTerm;
! 104515: int nOrderBy;
! 104516: sqlite3_index_info *pIdxInfo;
! 104517:
! 104518: WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));
! 104519:
! 104520: /* Count the number of possible WHERE clause constraints referring
! 104521: ** to this virtual table */
! 104522: for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
! 104523: if( pTerm->leftCursor != pSrc->iCursor ) continue;
! 104524: assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
! 104525: testcase( pTerm->eOperator==WO_IN );
! 104526: testcase( pTerm->eOperator==WO_ISNULL );
! 104527: if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
! 104528: if( pTerm->wtFlags & TERM_VNULL ) continue;
! 104529: nTerm++;
! 104530: }
! 104531:
! 104532: /* If the ORDER BY clause contains only columns in the current
! 104533: ** virtual table then allocate space for the aOrderBy part of
! 104534: ** the sqlite3_index_info structure.
! 104535: */
! 104536: nOrderBy = 0;
! 104537: if( pOrderBy ){
! 104538: for(i=0; i<pOrderBy->nExpr; i++){
! 104539: Expr *pExpr = pOrderBy->a[i].pExpr;
! 104540: if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
! 104541: }
! 104542: if( i==pOrderBy->nExpr ){
! 104543: nOrderBy = pOrderBy->nExpr;
! 104544: }
! 104545: }
! 104546:
! 104547: /* Allocate the sqlite3_index_info structure
! 104548: */
! 104549: pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
! 104550: + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
! 104551: + sizeof(*pIdxOrderBy)*nOrderBy );
! 104552: if( pIdxInfo==0 ){
! 104553: sqlite3ErrorMsg(pParse, "out of memory");
! 104554: /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
! 104555: return 0;
! 104556: }
! 104557:
! 104558: /* Initialize the structure. The sqlite3_index_info structure contains
! 104559: ** many fields that are declared "const" to prevent xBestIndex from
! 104560: ** changing them. We have to do some funky casting in order to
! 104561: ** initialize those fields.
! 104562: */
! 104563: pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
! 104564: pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
! 104565: pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
! 104566: *(int*)&pIdxInfo->nConstraint = nTerm;
! 104567: *(int*)&pIdxInfo->nOrderBy = nOrderBy;
! 104568: *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
! 104569: *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
! 104570: *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
! 104571: pUsage;
! 104572:
! 104573: for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
! 104574: if( pTerm->leftCursor != pSrc->iCursor ) continue;
! 104575: assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
! 104576: testcase( pTerm->eOperator==WO_IN );
! 104577: testcase( pTerm->eOperator==WO_ISNULL );
! 104578: if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
! 104579: if( pTerm->wtFlags & TERM_VNULL ) continue;
! 104580: pIdxCons[j].iColumn = pTerm->u.leftColumn;
! 104581: pIdxCons[j].iTermOffset = i;
! 104582: pIdxCons[j].op = (u8)pTerm->eOperator;
! 104583: /* The direct assignment in the previous line is possible only because
! 104584: ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
! 104585: ** following asserts verify this fact. */
! 104586: assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
! 104587: assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
! 104588: assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
! 104589: assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
! 104590: assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
! 104591: assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
! 104592: assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
! 104593: j++;
! 104594: }
! 104595: for(i=0; i<nOrderBy; i++){
! 104596: Expr *pExpr = pOrderBy->a[i].pExpr;
! 104597: pIdxOrderBy[i].iColumn = pExpr->iColumn;
! 104598: pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
! 104599: }
! 104600:
! 104601: return pIdxInfo;
! 104602: }
! 104603:
! 104604: /*
! 104605: ** The table object reference passed as the second argument to this function
! 104606: ** must represent a virtual table. This function invokes the xBestIndex()
! 104607: ** method of the virtual table with the sqlite3_index_info pointer passed
! 104608: ** as the argument.
! 104609: **
! 104610: ** If an error occurs, pParse is populated with an error message and a
! 104611: ** non-zero value is returned. Otherwise, 0 is returned and the output
! 104612: ** part of the sqlite3_index_info structure is left populated.
! 104613: **
! 104614: ** Whether or not an error is returned, it is the responsibility of the
! 104615: ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
! 104616: ** that this is required.
! 104617: */
! 104618: static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
! 104619: sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
! 104620: int i;
! 104621: int rc;
! 104622:
! 104623: WHERETRACE(("xBestIndex for %s\n", pTab->zName));
! 104624: TRACE_IDX_INPUTS(p);
! 104625: rc = pVtab->pModule->xBestIndex(pVtab, p);
! 104626: TRACE_IDX_OUTPUTS(p);
! 104627:
! 104628: if( rc!=SQLITE_OK ){
! 104629: if( rc==SQLITE_NOMEM ){
! 104630: pParse->db->mallocFailed = 1;
! 104631: }else if( !pVtab->zErrMsg ){
! 104632: sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
! 104633: }else{
! 104634: sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
! 104635: }
! 104636: }
! 104637: sqlite3_free(pVtab->zErrMsg);
! 104638: pVtab->zErrMsg = 0;
! 104639:
! 104640: for(i=0; i<p->nConstraint; i++){
! 104641: if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
! 104642: sqlite3ErrorMsg(pParse,
! 104643: "table %s: xBestIndex returned an invalid plan", pTab->zName);
! 104644: }
! 104645: }
! 104646:
! 104647: return pParse->nErr;
! 104648: }
! 104649:
! 104650:
! 104651: /*
! 104652: ** Compute the best index for a virtual table.
! 104653: **
! 104654: ** The best index is computed by the xBestIndex method of the virtual
! 104655: ** table module. This routine is really just a wrapper that sets up
! 104656: ** the sqlite3_index_info structure that is used to communicate with
! 104657: ** xBestIndex.
! 104658: **
! 104659: ** In a join, this routine might be called multiple times for the
! 104660: ** same virtual table. The sqlite3_index_info structure is created
! 104661: ** and initialized on the first invocation and reused on all subsequent
! 104662: ** invocations. The sqlite3_index_info structure is also used when
! 104663: ** code is generated to access the virtual table. The whereInfoDelete()
! 104664: ** routine takes care of freeing the sqlite3_index_info structure after
! 104665: ** everybody has finished with it.
! 104666: */
! 104667: static void bestVirtualIndex(
! 104668: Parse *pParse, /* The parsing context */
! 104669: WhereClause *pWC, /* The WHERE clause */
! 104670: struct SrcList_item *pSrc, /* The FROM clause term to search */
! 104671: Bitmask notReady, /* Mask of cursors not available for index */
! 104672: Bitmask notValid, /* Cursors not valid for any purpose */
! 104673: ExprList *pOrderBy, /* The order by clause */
! 104674: WhereCost *pCost, /* Lowest cost query plan */
! 104675: sqlite3_index_info **ppIdxInfo /* Index information passed to xBestIndex */
! 104676: ){
! 104677: Table *pTab = pSrc->pTab;
! 104678: sqlite3_index_info *pIdxInfo;
! 104679: struct sqlite3_index_constraint *pIdxCons;
! 104680: struct sqlite3_index_constraint_usage *pUsage;
! 104681: WhereTerm *pTerm;
! 104682: int i, j;
! 104683: int nOrderBy;
! 104684: double rCost;
! 104685:
! 104686: /* Make sure wsFlags is initialized to some sane value. Otherwise, if the
! 104687: ** malloc in allocateIndexInfo() fails and this function returns leaving
! 104688: ** wsFlags in an uninitialized state, the caller may behave unpredictably.
! 104689: */
! 104690: memset(pCost, 0, sizeof(*pCost));
! 104691: pCost->plan.wsFlags = WHERE_VIRTUALTABLE;
! 104692:
! 104693: /* If the sqlite3_index_info structure has not been previously
! 104694: ** allocated and initialized, then allocate and initialize it now.
! 104695: */
! 104696: pIdxInfo = *ppIdxInfo;
! 104697: if( pIdxInfo==0 ){
! 104698: *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy);
! 104699: }
! 104700: if( pIdxInfo==0 ){
! 104701: return;
! 104702: }
! 104703:
! 104704: /* At this point, the sqlite3_index_info structure that pIdxInfo points
! 104705: ** to will have been initialized, either during the current invocation or
! 104706: ** during some prior invocation. Now we just have to customize the
! 104707: ** details of pIdxInfo for the current invocation and pass it to
! 104708: ** xBestIndex.
! 104709: */
! 104710:
! 104711: /* The module name must be defined. Also, by this point there must
! 104712: ** be a pointer to an sqlite3_vtab structure. Otherwise
! 104713: ** sqlite3ViewGetColumnNames() would have picked up the error.
! 104714: */
! 104715: assert( pTab->azModuleArg && pTab->azModuleArg[0] );
! 104716: assert( sqlite3GetVTable(pParse->db, pTab) );
! 104717:
! 104718: /* Set the aConstraint[].usable fields and initialize all
! 104719: ** output variables to zero.
! 104720: **
! 104721: ** aConstraint[].usable is true for constraints where the right-hand
! 104722: ** side contains only references to tables to the left of the current
! 104723: ** table. In other words, if the constraint is of the form:
! 104724: **
! 104725: ** column = expr
! 104726: **
! 104727: ** and we are evaluating a join, then the constraint on column is
! 104728: ** only valid if all tables referenced in expr occur to the left
! 104729: ** of the table containing column.
! 104730: **
! 104731: ** The aConstraints[] array contains entries for all constraints
! 104732: ** on the current table. That way we only have to compute it once
! 104733: ** even though we might try to pick the best index multiple times.
! 104734: ** For each attempt at picking an index, the order of tables in the
! 104735: ** join might be different so we have to recompute the usable flag
! 104736: ** each time.
! 104737: */
! 104738: pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
! 104739: pUsage = pIdxInfo->aConstraintUsage;
! 104740: for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
! 104741: j = pIdxCons->iTermOffset;
! 104742: pTerm = &pWC->a[j];
! 104743: pIdxCons->usable = (pTerm->prereqRight¬Ready) ? 0 : 1;
! 104744: }
! 104745: memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
! 104746: if( pIdxInfo->needToFreeIdxStr ){
! 104747: sqlite3_free(pIdxInfo->idxStr);
! 104748: }
! 104749: pIdxInfo->idxStr = 0;
! 104750: pIdxInfo->idxNum = 0;
! 104751: pIdxInfo->needToFreeIdxStr = 0;
! 104752: pIdxInfo->orderByConsumed = 0;
! 104753: /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
! 104754: pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
! 104755: nOrderBy = pIdxInfo->nOrderBy;
! 104756: if( !pOrderBy ){
! 104757: pIdxInfo->nOrderBy = 0;
! 104758: }
! 104759:
! 104760: if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
! 104761: return;
! 104762: }
! 104763:
! 104764: pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
! 104765: for(i=0; i<pIdxInfo->nConstraint; i++){
! 104766: if( pUsage[i].argvIndex>0 ){
! 104767: pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
! 104768: }
! 104769: }
! 104770:
! 104771: /* If there is an ORDER BY clause, and the selected virtual table index
! 104772: ** does not satisfy it, increase the cost of the scan accordingly. This
! 104773: ** matches the processing for non-virtual tables in bestBtreeIndex().
! 104774: */
! 104775: rCost = pIdxInfo->estimatedCost;
! 104776: if( pOrderBy && pIdxInfo->orderByConsumed==0 ){
! 104777: rCost += estLog(rCost)*rCost;
! 104778: }
! 104779:
! 104780: /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
! 104781: ** inital value of lowestCost in this loop. If it is, then the
! 104782: ** (cost<lowestCost) test below will never be true.
! 104783: **
! 104784: ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT
! 104785: ** is defined.
! 104786: */
! 104787: if( (SQLITE_BIG_DBL/((double)2))<rCost ){
! 104788: pCost->rCost = (SQLITE_BIG_DBL/((double)2));
! 104789: }else{
! 104790: pCost->rCost = rCost;
! 104791: }
! 104792: pCost->plan.u.pVtabIdx = pIdxInfo;
! 104793: if( pIdxInfo->orderByConsumed ){
! 104794: pCost->plan.wsFlags |= WHERE_ORDERBY;
! 104795: }
! 104796: pCost->plan.nEq = 0;
! 104797: pIdxInfo->nOrderBy = nOrderBy;
! 104798:
! 104799: /* Try to find a more efficient access pattern by using multiple indexes
! 104800: ** to optimize an OR expression within the WHERE clause.
! 104801: */
! 104802: bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
! 104803: }
! 104804: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 104805:
! 104806: #ifdef SQLITE_ENABLE_STAT3
! 104807: /*
! 104808: ** Estimate the location of a particular key among all keys in an
! 104809: ** index. Store the results in aStat as follows:
! 104810: **
! 104811: ** aStat[0] Est. number of rows less than pVal
! 104812: ** aStat[1] Est. number of rows equal to pVal
! 104813: **
! 104814: ** Return SQLITE_OK on success.
! 104815: */
! 104816: static int whereKeyStats(
! 104817: Parse *pParse, /* Database connection */
! 104818: Index *pIdx, /* Index to consider domain of */
! 104819: sqlite3_value *pVal, /* Value to consider */
! 104820: int roundUp, /* Round up if true. Round down if false */
! 104821: tRowcnt *aStat /* OUT: stats written here */
! 104822: ){
! 104823: tRowcnt n;
! 104824: IndexSample *aSample;
! 104825: int i, eType;
! 104826: int isEq = 0;
! 104827: i64 v;
! 104828: double r, rS;
! 104829:
! 104830: assert( roundUp==0 || roundUp==1 );
! 104831: assert( pIdx->nSample>0 );
! 104832: if( pVal==0 ) return SQLITE_ERROR;
! 104833: n = pIdx->aiRowEst[0];
! 104834: aSample = pIdx->aSample;
! 104835: eType = sqlite3_value_type(pVal);
! 104836:
! 104837: if( eType==SQLITE_INTEGER ){
! 104838: v = sqlite3_value_int64(pVal);
! 104839: r = (i64)v;
! 104840: for(i=0; i<pIdx->nSample; i++){
! 104841: if( aSample[i].eType==SQLITE_NULL ) continue;
! 104842: if( aSample[i].eType>=SQLITE_TEXT ) break;
! 104843: if( aSample[i].eType==SQLITE_INTEGER ){
! 104844: if( aSample[i].u.i>=v ){
! 104845: isEq = aSample[i].u.i==v;
! 104846: break;
! 104847: }
! 104848: }else{
! 104849: assert( aSample[i].eType==SQLITE_FLOAT );
! 104850: if( aSample[i].u.r>=r ){
! 104851: isEq = aSample[i].u.r==r;
! 104852: break;
! 104853: }
! 104854: }
! 104855: }
! 104856: }else if( eType==SQLITE_FLOAT ){
! 104857: r = sqlite3_value_double(pVal);
! 104858: for(i=0; i<pIdx->nSample; i++){
! 104859: if( aSample[i].eType==SQLITE_NULL ) continue;
! 104860: if( aSample[i].eType>=SQLITE_TEXT ) break;
! 104861: if( aSample[i].eType==SQLITE_FLOAT ){
! 104862: rS = aSample[i].u.r;
! 104863: }else{
! 104864: rS = aSample[i].u.i;
! 104865: }
! 104866: if( rS>=r ){
! 104867: isEq = rS==r;
! 104868: break;
! 104869: }
! 104870: }
! 104871: }else if( eType==SQLITE_NULL ){
! 104872: i = 0;
! 104873: if( aSample[0].eType==SQLITE_NULL ) isEq = 1;
! 104874: }else{
! 104875: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
! 104876: for(i=0; i<pIdx->nSample; i++){
! 104877: if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
! 104878: break;
! 104879: }
! 104880: }
! 104881: if( i<pIdx->nSample ){
! 104882: sqlite3 *db = pParse->db;
! 104883: CollSeq *pColl;
! 104884: const u8 *z;
! 104885: if( eType==SQLITE_BLOB ){
! 104886: z = (const u8 *)sqlite3_value_blob(pVal);
! 104887: pColl = db->pDfltColl;
! 104888: assert( pColl->enc==SQLITE_UTF8 );
! 104889: }else{
! 104890: pColl = sqlite3GetCollSeq(db, SQLITE_UTF8, 0, *pIdx->azColl);
! 104891: if( pColl==0 ){
! 104892: sqlite3ErrorMsg(pParse, "no such collation sequence: %s",
! 104893: *pIdx->azColl);
! 104894: return SQLITE_ERROR;
! 104895: }
! 104896: z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
! 104897: if( !z ){
! 104898: return SQLITE_NOMEM;
! 104899: }
! 104900: assert( z && pColl && pColl->xCmp );
! 104901: }
! 104902: n = sqlite3ValueBytes(pVal, pColl->enc);
! 104903:
! 104904: for(; i<pIdx->nSample; i++){
! 104905: int c;
! 104906: int eSampletype = aSample[i].eType;
! 104907: if( eSampletype<eType ) continue;
! 104908: if( eSampletype!=eType ) break;
! 104909: #ifndef SQLITE_OMIT_UTF16
! 104910: if( pColl->enc!=SQLITE_UTF8 ){
! 104911: int nSample;
! 104912: char *zSample = sqlite3Utf8to16(
! 104913: db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
! 104914: );
! 104915: if( !zSample ){
! 104916: assert( db->mallocFailed );
! 104917: return SQLITE_NOMEM;
! 104918: }
! 104919: c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
! 104920: sqlite3DbFree(db, zSample);
! 104921: }else
! 104922: #endif
! 104923: {
! 104924: c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
! 104925: }
! 104926: if( c>=0 ){
! 104927: if( c==0 ) isEq = 1;
! 104928: break;
! 104929: }
! 104930: }
! 104931: }
! 104932: }
! 104933:
! 104934: /* At this point, aSample[i] is the first sample that is greater than
! 104935: ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
! 104936: ** than pVal. If aSample[i]==pVal, then isEq==1.
! 104937: */
! 104938: if( isEq ){
! 104939: assert( i<pIdx->nSample );
! 104940: aStat[0] = aSample[i].nLt;
! 104941: aStat[1] = aSample[i].nEq;
! 104942: }else{
! 104943: tRowcnt iLower, iUpper, iGap;
! 104944: if( i==0 ){
! 104945: iLower = 0;
! 104946: iUpper = aSample[0].nLt;
! 104947: }else{
! 104948: iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
! 104949: iLower = aSample[i-1].nEq + aSample[i-1].nLt;
! 104950: }
! 104951: aStat[1] = pIdx->avgEq;
! 104952: if( iLower>=iUpper ){
! 104953: iGap = 0;
! 104954: }else{
! 104955: iGap = iUpper - iLower;
! 104956: }
! 104957: if( roundUp ){
! 104958: iGap = (iGap*2)/3;
! 104959: }else{
! 104960: iGap = iGap/3;
! 104961: }
! 104962: aStat[0] = iLower + iGap;
! 104963: }
! 104964: return SQLITE_OK;
! 104965: }
! 104966: #endif /* SQLITE_ENABLE_STAT3 */
! 104967:
! 104968: /*
! 104969: ** If expression pExpr represents a literal value, set *pp to point to
! 104970: ** an sqlite3_value structure containing the same value, with affinity
! 104971: ** aff applied to it, before returning. It is the responsibility of the
! 104972: ** caller to eventually release this structure by passing it to
! 104973: ** sqlite3ValueFree().
! 104974: **
! 104975: ** If the current parse is a recompile (sqlite3Reprepare()) and pExpr
! 104976: ** is an SQL variable that currently has a non-NULL value bound to it,
! 104977: ** create an sqlite3_value structure containing this value, again with
! 104978: ** affinity aff applied to it, instead.
! 104979: **
! 104980: ** If neither of the above apply, set *pp to NULL.
! 104981: **
! 104982: ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
! 104983: */
! 104984: #ifdef SQLITE_ENABLE_STAT3
! 104985: static int valueFromExpr(
! 104986: Parse *pParse,
! 104987: Expr *pExpr,
! 104988: u8 aff,
! 104989: sqlite3_value **pp
! 104990: ){
! 104991: if( pExpr->op==TK_VARIABLE
! 104992: || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
! 104993: ){
! 104994: int iVar = pExpr->iColumn;
! 104995: sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
! 104996: *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
! 104997: return SQLITE_OK;
! 104998: }
! 104999: return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
! 105000: }
! 105001: #endif
! 105002:
! 105003: /*
! 105004: ** This function is used to estimate the number of rows that will be visited
! 105005: ** by scanning an index for a range of values. The range may have an upper
! 105006: ** bound, a lower bound, or both. The WHERE clause terms that set the upper
! 105007: ** and lower bounds are represented by pLower and pUpper respectively. For
! 105008: ** example, assuming that index p is on t1(a):
! 105009: **
! 105010: ** ... FROM t1 WHERE a > ? AND a < ? ...
! 105011: ** |_____| |_____|
! 105012: ** | |
! 105013: ** pLower pUpper
! 105014: **
! 105015: ** If either of the upper or lower bound is not present, then NULL is passed in
! 105016: ** place of the corresponding WhereTerm.
! 105017: **
! 105018: ** The nEq parameter is passed the index of the index column subject to the
! 105019: ** range constraint. Or, equivalently, the number of equality constraints
! 105020: ** optimized by the proposed index scan. For example, assuming index p is
! 105021: ** on t1(a, b), and the SQL query is:
! 105022: **
! 105023: ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
! 105024: **
! 105025: ** then nEq should be passed the value 1 (as the range restricted column,
! 105026: ** b, is the second left-most column of the index). Or, if the query is:
! 105027: **
! 105028: ** ... FROM t1 WHERE a > ? AND a < ? ...
! 105029: **
! 105030: ** then nEq should be passed 0.
! 105031: **
! 105032: ** The returned value is an integer divisor to reduce the estimated
! 105033: ** search space. A return value of 1 means that range constraints are
! 105034: ** no help at all. A return value of 2 means range constraints are
! 105035: ** expected to reduce the search space by half. And so forth...
! 105036: **
! 105037: ** In the absence of sqlite_stat3 ANALYZE data, each range inequality
! 105038: ** reduces the search space by a factor of 4. Hence a single constraint (x>?)
! 105039: ** results in a return of 4 and a range constraint (x>? AND x<?) results
! 105040: ** in a return of 16.
! 105041: */
! 105042: static int whereRangeScanEst(
! 105043: Parse *pParse, /* Parsing & code generating context */
! 105044: Index *p, /* The index containing the range-compared column; "x" */
! 105045: int nEq, /* index into p->aCol[] of the range-compared column */
! 105046: WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
! 105047: WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
! 105048: double *pRangeDiv /* OUT: Reduce search space by this divisor */
! 105049: ){
! 105050: int rc = SQLITE_OK;
! 105051:
! 105052: #ifdef SQLITE_ENABLE_STAT3
! 105053:
! 105054: if( nEq==0 && p->nSample ){
! 105055: sqlite3_value *pRangeVal;
! 105056: tRowcnt iLower = 0;
! 105057: tRowcnt iUpper = p->aiRowEst[0];
! 105058: tRowcnt a[2];
! 105059: u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
! 105060:
! 105061: if( pLower ){
! 105062: Expr *pExpr = pLower->pExpr->pRight;
! 105063: rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
! 105064: assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
! 105065: if( rc==SQLITE_OK
! 105066: && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
! 105067: ){
! 105068: iLower = a[0];
! 105069: if( pLower->eOperator==WO_GT ) iLower += a[1];
! 105070: }
! 105071: sqlite3ValueFree(pRangeVal);
! 105072: }
! 105073: if( rc==SQLITE_OK && pUpper ){
! 105074: Expr *pExpr = pUpper->pExpr->pRight;
! 105075: rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
! 105076: assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
! 105077: if( rc==SQLITE_OK
! 105078: && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
! 105079: ){
! 105080: iUpper = a[0];
! 105081: if( pUpper->eOperator==WO_LE ) iUpper += a[1];
! 105082: }
! 105083: sqlite3ValueFree(pRangeVal);
! 105084: }
! 105085: if( rc==SQLITE_OK ){
! 105086: if( iUpper<=iLower ){
! 105087: *pRangeDiv = (double)p->aiRowEst[0];
! 105088: }else{
! 105089: *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
! 105090: }
! 105091: WHERETRACE(("range scan regions: %u..%u div=%g\n",
! 105092: (u32)iLower, (u32)iUpper, *pRangeDiv));
! 105093: return SQLITE_OK;
! 105094: }
! 105095: }
! 105096: #else
! 105097: UNUSED_PARAMETER(pParse);
! 105098: UNUSED_PARAMETER(p);
! 105099: UNUSED_PARAMETER(nEq);
! 105100: #endif
! 105101: assert( pLower || pUpper );
! 105102: *pRangeDiv = (double)1;
! 105103: if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
! 105104: if( pUpper ) *pRangeDiv *= (double)4;
! 105105: return rc;
! 105106: }
! 105107:
! 105108: #ifdef SQLITE_ENABLE_STAT3
! 105109: /*
! 105110: ** Estimate the number of rows that will be returned based on
! 105111: ** an equality constraint x=VALUE and where that VALUE occurs in
! 105112: ** the histogram data. This only works when x is the left-most
! 105113: ** column of an index and sqlite_stat3 histogram data is available
! 105114: ** for that index. When pExpr==NULL that means the constraint is
! 105115: ** "x IS NULL" instead of "x=VALUE".
! 105116: **
! 105117: ** Write the estimated row count into *pnRow and return SQLITE_OK.
! 105118: ** If unable to make an estimate, leave *pnRow unchanged and return
! 105119: ** non-zero.
! 105120: **
! 105121: ** This routine can fail if it is unable to load a collating sequence
! 105122: ** required for string comparison, or if unable to allocate memory
! 105123: ** for a UTF conversion required for comparison. The error is stored
! 105124: ** in the pParse structure.
! 105125: */
! 105126: static int whereEqualScanEst(
! 105127: Parse *pParse, /* Parsing & code generating context */
! 105128: Index *p, /* The index whose left-most column is pTerm */
! 105129: Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
! 105130: double *pnRow /* Write the revised row estimate here */
! 105131: ){
! 105132: sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
! 105133: u8 aff; /* Column affinity */
! 105134: int rc; /* Subfunction return code */
! 105135: tRowcnt a[2]; /* Statistics */
! 105136:
! 105137: assert( p->aSample!=0 );
! 105138: assert( p->nSample>0 );
! 105139: aff = p->pTable->aCol[p->aiColumn[0]].affinity;
! 105140: if( pExpr ){
! 105141: rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
! 105142: if( rc ) goto whereEqualScanEst_cancel;
! 105143: }else{
! 105144: pRhs = sqlite3ValueNew(pParse->db);
! 105145: }
! 105146: if( pRhs==0 ) return SQLITE_NOTFOUND;
! 105147: rc = whereKeyStats(pParse, p, pRhs, 0, a);
! 105148: if( rc==SQLITE_OK ){
! 105149: WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
! 105150: *pnRow = a[1];
! 105151: }
! 105152: whereEqualScanEst_cancel:
! 105153: sqlite3ValueFree(pRhs);
! 105154: return rc;
! 105155: }
! 105156: #endif /* defined(SQLITE_ENABLE_STAT3) */
! 105157:
! 105158: #ifdef SQLITE_ENABLE_STAT3
! 105159: /*
! 105160: ** Estimate the number of rows that will be returned based on
! 105161: ** an IN constraint where the right-hand side of the IN operator
! 105162: ** is a list of values. Example:
! 105163: **
! 105164: ** WHERE x IN (1,2,3,4)
! 105165: **
! 105166: ** Write the estimated row count into *pnRow and return SQLITE_OK.
! 105167: ** If unable to make an estimate, leave *pnRow unchanged and return
! 105168: ** non-zero.
! 105169: **
! 105170: ** This routine can fail if it is unable to load a collating sequence
! 105171: ** required for string comparison, or if unable to allocate memory
! 105172: ** for a UTF conversion required for comparison. The error is stored
! 105173: ** in the pParse structure.
! 105174: */
! 105175: static int whereInScanEst(
! 105176: Parse *pParse, /* Parsing & code generating context */
! 105177: Index *p, /* The index whose left-most column is pTerm */
! 105178: ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
! 105179: double *pnRow /* Write the revised row estimate here */
! 105180: ){
! 105181: int rc = SQLITE_OK; /* Subfunction return code */
! 105182: double nEst; /* Number of rows for a single term */
! 105183: double nRowEst = (double)0; /* New estimate of the number of rows */
! 105184: int i; /* Loop counter */
! 105185:
! 105186: assert( p->aSample!=0 );
! 105187: for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
! 105188: nEst = p->aiRowEst[0];
! 105189: rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
! 105190: nRowEst += nEst;
! 105191: }
! 105192: if( rc==SQLITE_OK ){
! 105193: if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
! 105194: *pnRow = nRowEst;
! 105195: WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
! 105196: }
! 105197: return rc;
! 105198: }
! 105199: #endif /* defined(SQLITE_ENABLE_STAT3) */
! 105200:
! 105201:
! 105202: /*
! 105203: ** Find the best query plan for accessing a particular table. Write the
! 105204: ** best query plan and its cost into the WhereCost object supplied as the
! 105205: ** last parameter.
! 105206: **
! 105207: ** The lowest cost plan wins. The cost is an estimate of the amount of
! 105208: ** CPU and disk I/O needed to process the requested result.
! 105209: ** Factors that influence cost include:
! 105210: **
! 105211: ** * The estimated number of rows that will be retrieved. (The
! 105212: ** fewer the better.)
! 105213: **
! 105214: ** * Whether or not sorting must occur.
! 105215: **
! 105216: ** * Whether or not there must be separate lookups in the
! 105217: ** index and in the main table.
! 105218: **
! 105219: ** If there was an INDEXED BY clause (pSrc->pIndex) attached to the table in
! 105220: ** the SQL statement, then this function only considers plans using the
! 105221: ** named index. If no such plan is found, then the returned cost is
! 105222: ** SQLITE_BIG_DBL. If a plan is found that uses the named index,
! 105223: ** then the cost is calculated in the usual way.
! 105224: **
! 105225: ** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table
! 105226: ** in the SELECT statement, then no indexes are considered. However, the
! 105227: ** selected plan may still take advantage of the built-in rowid primary key
! 105228: ** index.
! 105229: */
! 105230: static void bestBtreeIndex(
! 105231: Parse *pParse, /* The parsing context */
! 105232: WhereClause *pWC, /* The WHERE clause */
! 105233: struct SrcList_item *pSrc, /* The FROM clause term to search */
! 105234: Bitmask notReady, /* Mask of cursors not available for indexing */
! 105235: Bitmask notValid, /* Cursors not available for any purpose */
! 105236: ExprList *pOrderBy, /* The ORDER BY clause */
! 105237: ExprList *pDistinct, /* The select-list if query is DISTINCT */
! 105238: WhereCost *pCost /* Lowest cost query plan */
! 105239: ){
! 105240: int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
! 105241: Index *pProbe; /* An index we are evaluating */
! 105242: Index *pIdx; /* Copy of pProbe, or zero for IPK index */
! 105243: int eqTermMask; /* Current mask of valid equality operators */
! 105244: int idxEqTermMask; /* Index mask of valid equality operators */
! 105245: Index sPk; /* A fake index object for the primary key */
! 105246: tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
! 105247: int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
! 105248: int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
! 105249:
! 105250: /* Initialize the cost to a worst-case value */
! 105251: memset(pCost, 0, sizeof(*pCost));
! 105252: pCost->rCost = SQLITE_BIG_DBL;
! 105253:
! 105254: /* If the pSrc table is the right table of a LEFT JOIN then we may not
! 105255: ** use an index to satisfy IS NULL constraints on that table. This is
! 105256: ** because columns might end up being NULL if the table does not match -
! 105257: ** a circumstance which the index cannot help us discover. Ticket #2177.
! 105258: */
! 105259: if( pSrc->jointype & JT_LEFT ){
! 105260: idxEqTermMask = WO_EQ|WO_IN;
! 105261: }else{
! 105262: idxEqTermMask = WO_EQ|WO_IN|WO_ISNULL;
! 105263: }
! 105264:
! 105265: if( pSrc->pIndex ){
! 105266: /* An INDEXED BY clause specifies a particular index to use */
! 105267: pIdx = pProbe = pSrc->pIndex;
! 105268: wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
! 105269: eqTermMask = idxEqTermMask;
! 105270: }else{
! 105271: /* There is no INDEXED BY clause. Create a fake Index object in local
! 105272: ** variable sPk to represent the rowid primary key index. Make this
! 105273: ** fake index the first in a chain of Index objects with all of the real
! 105274: ** indices to follow */
! 105275: Index *pFirst; /* First of real indices on the table */
! 105276: memset(&sPk, 0, sizeof(Index));
! 105277: sPk.nColumn = 1;
! 105278: sPk.aiColumn = &aiColumnPk;
! 105279: sPk.aiRowEst = aiRowEstPk;
! 105280: sPk.onError = OE_Replace;
! 105281: sPk.pTable = pSrc->pTab;
! 105282: aiRowEstPk[0] = pSrc->pTab->nRowEst;
! 105283: aiRowEstPk[1] = 1;
! 105284: pFirst = pSrc->pTab->pIndex;
! 105285: if( pSrc->notIndexed==0 ){
! 105286: /* The real indices of the table are only considered if the
! 105287: ** NOT INDEXED qualifier is omitted from the FROM clause */
! 105288: sPk.pNext = pFirst;
! 105289: }
! 105290: pProbe = &sPk;
! 105291: wsFlagMask = ~(
! 105292: WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
! 105293: );
! 105294: eqTermMask = WO_EQ|WO_IN;
! 105295: pIdx = 0;
! 105296: }
! 105297:
! 105298: /* Loop over all indices looking for the best one to use
! 105299: */
! 105300: for(; pProbe; pIdx=pProbe=pProbe->pNext){
! 105301: const tRowcnt * const aiRowEst = pProbe->aiRowEst;
! 105302: double cost; /* Cost of using pProbe */
! 105303: double nRow; /* Estimated number of rows in result set */
! 105304: double log10N = (double)1; /* base-10 logarithm of nRow (inexact) */
! 105305: int rev; /* True to scan in reverse order */
! 105306: int wsFlags = 0;
! 105307: Bitmask used = 0;
! 105308:
! 105309: /* The following variables are populated based on the properties of
! 105310: ** index being evaluated. They are then used to determine the expected
! 105311: ** cost and number of rows returned.
! 105312: **
! 105313: ** nEq:
! 105314: ** Number of equality terms that can be implemented using the index.
! 105315: ** In other words, the number of initial fields in the index that
! 105316: ** are used in == or IN or NOT NULL constraints of the WHERE clause.
! 105317: **
! 105318: ** nInMul:
! 105319: ** The "in-multiplier". This is an estimate of how many seek operations
! 105320: ** SQLite must perform on the index in question. For example, if the
! 105321: ** WHERE clause is:
! 105322: **
! 105323: ** WHERE a IN (1, 2, 3) AND b IN (4, 5, 6)
! 105324: **
! 105325: ** SQLite must perform 9 lookups on an index on (a, b), so nInMul is
! 105326: ** set to 9. Given the same schema and either of the following WHERE
! 105327: ** clauses:
! 105328: **
! 105329: ** WHERE a = 1
! 105330: ** WHERE a >= 2
! 105331: **
! 105332: ** nInMul is set to 1.
! 105333: **
! 105334: ** If there exists a WHERE term of the form "x IN (SELECT ...)", then
! 105335: ** the sub-select is assumed to return 25 rows for the purposes of
! 105336: ** determining nInMul.
! 105337: **
! 105338: ** bInEst:
! 105339: ** Set to true if there was at least one "x IN (SELECT ...)" term used
! 105340: ** in determining the value of nInMul. Note that the RHS of the
! 105341: ** IN operator must be a SELECT, not a value list, for this variable
! 105342: ** to be true.
! 105343: **
! 105344: ** rangeDiv:
! 105345: ** An estimate of a divisor by which to reduce the search space due
! 105346: ** to inequality constraints. In the absence of sqlite_stat3 ANALYZE
! 105347: ** data, a single inequality reduces the search space to 1/4rd its
! 105348: ** original size (rangeDiv==4). Two inequalities reduce the search
! 105349: ** space to 1/16th of its original size (rangeDiv==16).
! 105350: **
! 105351: ** bSort:
! 105352: ** Boolean. True if there is an ORDER BY clause that will require an
! 105353: ** external sort (i.e. scanning the index being evaluated will not
! 105354: ** correctly order records).
! 105355: **
! 105356: ** bLookup:
! 105357: ** Boolean. True if a table lookup is required for each index entry
! 105358: ** visited. In other words, true if this is not a covering index.
! 105359: ** This is always false for the rowid primary key index of a table.
! 105360: ** For other indexes, it is true unless all the columns of the table
! 105361: ** used by the SELECT statement are present in the index (such an
! 105362: ** index is sometimes described as a covering index).
! 105363: ** For example, given the index on (a, b), the second of the following
! 105364: ** two queries requires table b-tree lookups in order to find the value
! 105365: ** of column c, but the first does not because columns a and b are
! 105366: ** both available in the index.
! 105367: **
! 105368: ** SELECT a, b FROM tbl WHERE a = 1;
! 105369: ** SELECT a, b, c FROM tbl WHERE a = 1;
! 105370: */
! 105371: int nEq; /* Number of == or IN terms matching index */
! 105372: int bInEst = 0; /* True if "x IN (SELECT...)" seen */
! 105373: int nInMul = 1; /* Number of distinct equalities to lookup */
! 105374: double rangeDiv = (double)1; /* Estimated reduction in search space */
! 105375: int nBound = 0; /* Number of range constraints seen */
! 105376: int bSort = !!pOrderBy; /* True if external sort required */
! 105377: int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
! 105378: int bLookup = 0; /* True if not a covering index */
! 105379: WhereTerm *pTerm; /* A single term of the WHERE clause */
! 105380: #ifdef SQLITE_ENABLE_STAT3
! 105381: WhereTerm *pFirstTerm = 0; /* First term matching the index */
! 105382: #endif
! 105383:
! 105384: /* Determine the values of nEq and nInMul */
! 105385: for(nEq=0; nEq<pProbe->nColumn; nEq++){
! 105386: int j = pProbe->aiColumn[nEq];
! 105387: pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
! 105388: if( pTerm==0 ) break;
! 105389: wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
! 105390: testcase( pTerm->pWC!=pWC );
! 105391: if( pTerm->eOperator & WO_IN ){
! 105392: Expr *pExpr = pTerm->pExpr;
! 105393: wsFlags |= WHERE_COLUMN_IN;
! 105394: if( ExprHasProperty(pExpr, EP_xIsSelect) ){
! 105395: /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */
! 105396: nInMul *= 25;
! 105397: bInEst = 1;
! 105398: }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
! 105399: /* "x IN (value, value, ...)" */
! 105400: nInMul *= pExpr->x.pList->nExpr;
! 105401: }
! 105402: }else if( pTerm->eOperator & WO_ISNULL ){
! 105403: wsFlags |= WHERE_COLUMN_NULL;
! 105404: }
! 105405: #ifdef SQLITE_ENABLE_STAT3
! 105406: if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
! 105407: #endif
! 105408: used |= pTerm->prereqRight;
! 105409: }
! 105410:
! 105411: /* If the index being considered is UNIQUE, and there is an equality
! 105412: ** constraint for all columns in the index, then this search will find
! 105413: ** at most a single row. In this case set the WHERE_UNIQUE flag to
! 105414: ** indicate this to the caller.
! 105415: **
! 105416: ** Otherwise, if the search may find more than one row, test to see if
! 105417: ** there is a range constraint on indexed column (nEq+1) that can be
! 105418: ** optimized using the index.
! 105419: */
! 105420: if( nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
! 105421: testcase( wsFlags & WHERE_COLUMN_IN );
! 105422: testcase( wsFlags & WHERE_COLUMN_NULL );
! 105423: if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
! 105424: wsFlags |= WHERE_UNIQUE;
! 105425: }
! 105426: }else if( pProbe->bUnordered==0 ){
! 105427: int j = (nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[nEq]);
! 105428: if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
! 105429: WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
! 105430: WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
! 105431: whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
! 105432: if( pTop ){
! 105433: nBound = 1;
! 105434: wsFlags |= WHERE_TOP_LIMIT;
! 105435: used |= pTop->prereqRight;
! 105436: testcase( pTop->pWC!=pWC );
! 105437: }
! 105438: if( pBtm ){
! 105439: nBound++;
! 105440: wsFlags |= WHERE_BTM_LIMIT;
! 105441: used |= pBtm->prereqRight;
! 105442: testcase( pBtm->pWC!=pWC );
! 105443: }
! 105444: wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
! 105445: }
! 105446: }
! 105447:
! 105448: /* If there is an ORDER BY clause and the index being considered will
! 105449: ** naturally scan rows in the required order, set the appropriate flags
! 105450: ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
! 105451: ** will scan rows in a different order, set the bSort variable. */
! 105452: if( isSortingIndex(
! 105453: pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy, nEq, wsFlags, &rev)
! 105454: ){
! 105455: bSort = 0;
! 105456: wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
! 105457: wsFlags |= (rev ? WHERE_REVERSE : 0);
! 105458: }
! 105459:
! 105460: /* If there is a DISTINCT qualifier and this index will scan rows in
! 105461: ** order of the DISTINCT expressions, clear bDist and set the appropriate
! 105462: ** flags in wsFlags. */
! 105463: if( isDistinctIndex(pParse, pWC, pProbe, iCur, pDistinct, nEq) ){
! 105464: bDist = 0;
! 105465: wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
! 105466: }
! 105467:
! 105468: /* If currently calculating the cost of using an index (not the IPK
! 105469: ** index), determine if all required column data may be obtained without
! 105470: ** using the main table (i.e. if the index is a covering
! 105471: ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
! 105472: ** wsFlags. Otherwise, set the bLookup variable to true. */
! 105473: if( pIdx && wsFlags ){
! 105474: Bitmask m = pSrc->colUsed;
! 105475: int j;
! 105476: for(j=0; j<pIdx->nColumn; j++){
! 105477: int x = pIdx->aiColumn[j];
! 105478: if( x<BMS-1 ){
! 105479: m &= ~(((Bitmask)1)<<x);
! 105480: }
! 105481: }
! 105482: if( m==0 ){
! 105483: wsFlags |= WHERE_IDX_ONLY;
! 105484: }else{
! 105485: bLookup = 1;
! 105486: }
! 105487: }
! 105488:
! 105489: /*
! 105490: ** Estimate the number of rows of output. For an "x IN (SELECT...)"
! 105491: ** constraint, do not let the estimate exceed half the rows in the table.
! 105492: */
! 105493: nRow = (double)(aiRowEst[nEq] * nInMul);
! 105494: if( bInEst && nRow*2>aiRowEst[0] ){
! 105495: nRow = aiRowEst[0]/2;
! 105496: nInMul = (int)(nRow / aiRowEst[nEq]);
! 105497: }
! 105498:
! 105499: #ifdef SQLITE_ENABLE_STAT3
! 105500: /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
! 105501: ** and we do not think that values of x are unique and if histogram
! 105502: ** data is available for column x, then it might be possible
! 105503: ** to get a better estimate on the number of rows based on
! 105504: ** VALUE and how common that value is according to the histogram.
! 105505: */
! 105506: if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){
! 105507: assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
! 105508: if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
! 105509: testcase( pFirstTerm->eOperator==WO_EQ );
! 105510: testcase( pFirstTerm->eOperator==WO_ISNULL );
! 105511: whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
! 105512: }else if( bInEst==0 ){
! 105513: assert( pFirstTerm->eOperator==WO_IN );
! 105514: whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
! 105515: }
! 105516: }
! 105517: #endif /* SQLITE_ENABLE_STAT3 */
! 105518:
! 105519: /* Adjust the number of output rows and downward to reflect rows
! 105520: ** that are excluded by range constraints.
! 105521: */
! 105522: nRow = nRow/rangeDiv;
! 105523: if( nRow<1 ) nRow = 1;
! 105524:
! 105525: /* Experiments run on real SQLite databases show that the time needed
! 105526: ** to do a binary search to locate a row in a table or index is roughly
! 105527: ** log10(N) times the time to move from one row to the next row within
! 105528: ** a table or index. The actual times can vary, with the size of
! 105529: ** records being an important factor. Both moves and searches are
! 105530: ** slower with larger records, presumably because fewer records fit
! 105531: ** on one page and hence more pages have to be fetched.
! 105532: **
! 105533: ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
! 105534: ** not give us data on the relative sizes of table and index records.
! 105535: ** So this computation assumes table records are about twice as big
! 105536: ** as index records
! 105537: */
! 105538: if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
! 105539: /* The cost of a full table scan is a number of move operations equal
! 105540: ** to the number of rows in the table.
! 105541: **
! 105542: ** We add an additional 4x penalty to full table scans. This causes
! 105543: ** the cost function to err on the side of choosing an index over
! 105544: ** choosing a full scan. This 4x full-scan penalty is an arguable
! 105545: ** decision and one which we expect to revisit in the future. But
! 105546: ** it seems to be working well enough at the moment.
! 105547: */
! 105548: cost = aiRowEst[0]*4;
! 105549: }else{
! 105550: log10N = estLog(aiRowEst[0]);
! 105551: cost = nRow;
! 105552: if( pIdx ){
! 105553: if( bLookup ){
! 105554: /* For an index lookup followed by a table lookup:
! 105555: ** nInMul index searches to find the start of each index range
! 105556: ** + nRow steps through the index
! 105557: ** + nRow table searches to lookup the table entry using the rowid
! 105558: */
! 105559: cost += (nInMul + nRow)*log10N;
! 105560: }else{
! 105561: /* For a covering index:
! 105562: ** nInMul index searches to find the initial entry
! 105563: ** + nRow steps through the index
! 105564: */
! 105565: cost += nInMul*log10N;
! 105566: }
! 105567: }else{
! 105568: /* For a rowid primary key lookup:
! 105569: ** nInMult table searches to find the initial entry for each range
! 105570: ** + nRow steps through the table
! 105571: */
! 105572: cost += nInMul*log10N;
! 105573: }
! 105574: }
! 105575:
! 105576: /* Add in the estimated cost of sorting the result. Actual experimental
! 105577: ** measurements of sorting performance in SQLite show that sorting time
! 105578: ** adds C*N*log10(N) to the cost, where N is the number of rows to be
! 105579: ** sorted and C is a factor between 1.95 and 4.3. We will split the
! 105580: ** difference and select C of 3.0.
! 105581: */
! 105582: if( bSort ){
! 105583: cost += nRow*estLog(nRow)*3;
! 105584: }
! 105585: if( bDist ){
! 105586: cost += nRow*estLog(nRow)*3;
! 105587: }
! 105588:
! 105589: /**** Cost of using this index has now been computed ****/
! 105590:
! 105591: /* If there are additional constraints on this table that cannot
! 105592: ** be used with the current index, but which might lower the number
! 105593: ** of output rows, adjust the nRow value accordingly. This only
! 105594: ** matters if the current index is the least costly, so do not bother
! 105595: ** with this step if we already know this index will not be chosen.
! 105596: ** Also, never reduce the output row count below 2 using this step.
! 105597: **
! 105598: ** It is critical that the notValid mask be used here instead of
! 105599: ** the notReady mask. When computing an "optimal" index, the notReady
! 105600: ** mask will only have one bit set - the bit for the current table.
! 105601: ** The notValid mask, on the other hand, always has all bits set for
! 105602: ** tables that are not in outer loops. If notReady is used here instead
! 105603: ** of notValid, then a optimal index that depends on inner joins loops
! 105604: ** might be selected even when there exists an optimal index that has
! 105605: ** no such dependency.
! 105606: */
! 105607: if( nRow>2 && cost<=pCost->rCost ){
! 105608: int k; /* Loop counter */
! 105609: int nSkipEq = nEq; /* Number of == constraints to skip */
! 105610: int nSkipRange = nBound; /* Number of < constraints to skip */
! 105611: Bitmask thisTab; /* Bitmap for pSrc */
! 105612:
! 105613: thisTab = getMask(pWC->pMaskSet, iCur);
! 105614: for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
! 105615: if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
! 105616: if( (pTerm->prereqAll & notValid)!=thisTab ) continue;
! 105617: if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
! 105618: if( nSkipEq ){
! 105619: /* Ignore the first nEq equality matches since the index
! 105620: ** has already accounted for these */
! 105621: nSkipEq--;
! 105622: }else{
! 105623: /* Assume each additional equality match reduces the result
! 105624: ** set size by a factor of 10 */
! 105625: nRow /= 10;
! 105626: }
! 105627: }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
! 105628: if( nSkipRange ){
! 105629: /* Ignore the first nSkipRange range constraints since the index
! 105630: ** has already accounted for these */
! 105631: nSkipRange--;
! 105632: }else{
! 105633: /* Assume each additional range constraint reduces the result
! 105634: ** set size by a factor of 3. Indexed range constraints reduce
! 105635: ** the search space by a larger factor: 4. We make indexed range
! 105636: ** more selective intentionally because of the subjective
! 105637: ** observation that indexed range constraints really are more
! 105638: ** selective in practice, on average. */
! 105639: nRow /= 3;
! 105640: }
! 105641: }else if( pTerm->eOperator!=WO_NOOP ){
! 105642: /* Any other expression lowers the output row count by half */
! 105643: nRow /= 2;
! 105644: }
! 105645: }
! 105646: if( nRow<2 ) nRow = 2;
! 105647: }
! 105648:
! 105649:
! 105650: WHERETRACE((
! 105651: "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
! 105652: " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
! 105653: pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
! 105654: nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
! 105655: notReady, log10N, nRow, cost, used
! 105656: ));
! 105657:
! 105658: /* If this index is the best we have seen so far, then record this
! 105659: ** index and its cost in the pCost structure.
! 105660: */
! 105661: if( (!pIdx || wsFlags)
! 105662: && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->plan.nRow))
! 105663: ){
! 105664: pCost->rCost = cost;
! 105665: pCost->used = used;
! 105666: pCost->plan.nRow = nRow;
! 105667: pCost->plan.wsFlags = (wsFlags&wsFlagMask);
! 105668: pCost->plan.nEq = nEq;
! 105669: pCost->plan.u.pIdx = pIdx;
! 105670: }
! 105671:
! 105672: /* If there was an INDEXED BY clause, then only that one index is
! 105673: ** considered. */
! 105674: if( pSrc->pIndex ) break;
! 105675:
! 105676: /* Reset masks for the next index in the loop */
! 105677: wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
! 105678: eqTermMask = idxEqTermMask;
! 105679: }
! 105680:
! 105681: /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
! 105682: ** is set, then reverse the order that the index will be scanned
! 105683: ** in. This is used for application testing, to help find cases
! 105684: ** where application behaviour depends on the (undefined) order that
! 105685: ** SQLite outputs rows in in the absence of an ORDER BY clause. */
! 105686: if( !pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
! 105687: pCost->plan.wsFlags |= WHERE_REVERSE;
! 105688: }
! 105689:
! 105690: assert( pOrderBy || (pCost->plan.wsFlags&WHERE_ORDERBY)==0 );
! 105691: assert( pCost->plan.u.pIdx==0 || (pCost->plan.wsFlags&WHERE_ROWID_EQ)==0 );
! 105692: assert( pSrc->pIndex==0
! 105693: || pCost->plan.u.pIdx==0
! 105694: || pCost->plan.u.pIdx==pSrc->pIndex
! 105695: );
! 105696:
! 105697: WHERETRACE(("best index is: %s\n",
! 105698: ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" :
! 105699: pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
! 105700: ));
! 105701:
! 105702: bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
! 105703: bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
! 105704: pCost->plan.wsFlags |= eqTermMask;
! 105705: }
! 105706:
! 105707: /*
! 105708: ** Find the query plan for accessing table pSrc->pTab. Write the
! 105709: ** best query plan and its cost into the WhereCost object supplied
! 105710: ** as the last parameter. This function may calculate the cost of
! 105711: ** both real and virtual table scans.
! 105712: */
! 105713: static void bestIndex(
! 105714: Parse *pParse, /* The parsing context */
! 105715: WhereClause *pWC, /* The WHERE clause */
! 105716: struct SrcList_item *pSrc, /* The FROM clause term to search */
! 105717: Bitmask notReady, /* Mask of cursors not available for indexing */
! 105718: Bitmask notValid, /* Cursors not available for any purpose */
! 105719: ExprList *pOrderBy, /* The ORDER BY clause */
! 105720: WhereCost *pCost /* Lowest cost query plan */
! 105721: ){
! 105722: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 105723: if( IsVirtual(pSrc->pTab) ){
! 105724: sqlite3_index_info *p = 0;
! 105725: bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost,&p);
! 105726: if( p->needToFreeIdxStr ){
! 105727: sqlite3_free(p->idxStr);
! 105728: }
! 105729: sqlite3DbFree(pParse->db, p);
! 105730: }else
! 105731: #endif
! 105732: {
! 105733: bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, 0, pCost);
! 105734: }
! 105735: }
! 105736:
! 105737: /*
! 105738: ** Disable a term in the WHERE clause. Except, do not disable the term
! 105739: ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
! 105740: ** or USING clause of that join.
! 105741: **
! 105742: ** Consider the term t2.z='ok' in the following queries:
! 105743: **
! 105744: ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
! 105745: ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
! 105746: ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
! 105747: **
! 105748: ** The t2.z='ok' is disabled in the in (2) because it originates
! 105749: ** in the ON clause. The term is disabled in (3) because it is not part
! 105750: ** of a LEFT OUTER JOIN. In (1), the term is not disabled.
! 105751: **
! 105752: ** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are
! 105753: ** completely satisfied by indices.
! 105754: **
! 105755: ** Disabling a term causes that term to not be tested in the inner loop
! 105756: ** of the join. Disabling is an optimization. When terms are satisfied
! 105757: ** by indices, we disable them to prevent redundant tests in the inner
! 105758: ** loop. We would get the correct results if nothing were ever disabled,
! 105759: ** but joins might run a little slower. The trick is to disable as much
! 105760: ** as we can without disabling too much. If we disabled in (1), we'd get
! 105761: ** the wrong answer. See ticket #813.
! 105762: */
! 105763: static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
! 105764: if( pTerm
! 105765: && (pTerm->wtFlags & TERM_CODED)==0
! 105766: && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
! 105767: ){
! 105768: pTerm->wtFlags |= TERM_CODED;
! 105769: if( pTerm->iParent>=0 ){
! 105770: WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
! 105771: if( (--pOther->nChild)==0 ){
! 105772: disableTerm(pLevel, pOther);
! 105773: }
! 105774: }
! 105775: }
! 105776: }
! 105777:
! 105778: /*
! 105779: ** Code an OP_Affinity opcode to apply the column affinity string zAff
! 105780: ** to the n registers starting at base.
! 105781: **
! 105782: ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
! 105783: ** beginning and end of zAff are ignored. If all entries in zAff are
! 105784: ** SQLITE_AFF_NONE, then no code gets generated.
! 105785: **
! 105786: ** This routine makes its own copy of zAff so that the caller is free
! 105787: ** to modify zAff after this routine returns.
! 105788: */
! 105789: static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
! 105790: Vdbe *v = pParse->pVdbe;
! 105791: if( zAff==0 ){
! 105792: assert( pParse->db->mallocFailed );
! 105793: return;
! 105794: }
! 105795: assert( v!=0 );
! 105796:
! 105797: /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
! 105798: ** and end of the affinity string.
! 105799: */
! 105800: while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
! 105801: n--;
! 105802: base++;
! 105803: zAff++;
! 105804: }
! 105805: while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
! 105806: n--;
! 105807: }
! 105808:
! 105809: /* Code the OP_Affinity opcode if there is anything left to do. */
! 105810: if( n>0 ){
! 105811: sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
! 105812: sqlite3VdbeChangeP4(v, -1, zAff, n);
! 105813: sqlite3ExprCacheAffinityChange(pParse, base, n);
! 105814: }
! 105815: }
! 105816:
! 105817:
! 105818: /*
! 105819: ** Generate code for a single equality term of the WHERE clause. An equality
! 105820: ** term can be either X=expr or X IN (...). pTerm is the term to be
! 105821: ** coded.
! 105822: **
! 105823: ** The current value for the constraint is left in register iReg.
! 105824: **
! 105825: ** For a constraint of the form X=expr, the expression is evaluated and its
! 105826: ** result is left on the stack. For constraints of the form X IN (...)
! 105827: ** this routine sets up a loop that will iterate over all values of X.
! 105828: */
! 105829: static int codeEqualityTerm(
! 105830: Parse *pParse, /* The parsing context */
! 105831: WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
! 105832: WhereLevel *pLevel, /* When level of the FROM clause we are working on */
! 105833: int iTarget /* Attempt to leave results in this register */
! 105834: ){
! 105835: Expr *pX = pTerm->pExpr;
! 105836: Vdbe *v = pParse->pVdbe;
! 105837: int iReg; /* Register holding results */
! 105838:
! 105839: assert( iTarget>0 );
! 105840: if( pX->op==TK_EQ ){
! 105841: iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
! 105842: }else if( pX->op==TK_ISNULL ){
! 105843: iReg = iTarget;
! 105844: sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
! 105845: #ifndef SQLITE_OMIT_SUBQUERY
! 105846: }else{
! 105847: int eType;
! 105848: int iTab;
! 105849: struct InLoop *pIn;
! 105850:
! 105851: assert( pX->op==TK_IN );
! 105852: iReg = iTarget;
! 105853: eType = sqlite3FindInIndex(pParse, pX, 0);
! 105854: iTab = pX->iTable;
! 105855: sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
! 105856: assert( pLevel->plan.wsFlags & WHERE_IN_ABLE );
! 105857: if( pLevel->u.in.nIn==0 ){
! 105858: pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
! 105859: }
! 105860: pLevel->u.in.nIn++;
! 105861: pLevel->u.in.aInLoop =
! 105862: sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
! 105863: sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
! 105864: pIn = pLevel->u.in.aInLoop;
! 105865: if( pIn ){
! 105866: pIn += pLevel->u.in.nIn - 1;
! 105867: pIn->iCur = iTab;
! 105868: if( eType==IN_INDEX_ROWID ){
! 105869: pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
! 105870: }else{
! 105871: pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
! 105872: }
! 105873: sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
! 105874: }else{
! 105875: pLevel->u.in.nIn = 0;
! 105876: }
! 105877: #endif
! 105878: }
! 105879: disableTerm(pLevel, pTerm);
! 105880: return iReg;
! 105881: }
! 105882:
! 105883: /*
! 105884: ** Generate code that will evaluate all == and IN constraints for an
! 105885: ** index.
! 105886: **
! 105887: ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
! 105888: ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
! 105889: ** The index has as many as three equality constraints, but in this
! 105890: ** example, the third "c" value is an inequality. So only two
! 105891: ** constraints are coded. This routine will generate code to evaluate
! 105892: ** a==5 and b IN (1,2,3). The current values for a and b will be stored
! 105893: ** in consecutive registers and the index of the first register is returned.
! 105894: **
! 105895: ** In the example above nEq==2. But this subroutine works for any value
! 105896: ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
! 105897: ** The only thing it does is allocate the pLevel->iMem memory cell and
! 105898: ** compute the affinity string.
! 105899: **
! 105900: ** This routine always allocates at least one memory cell and returns
! 105901: ** the index of that memory cell. The code that
! 105902: ** calls this routine will use that memory cell to store the termination
! 105903: ** key value of the loop. If one or more IN operators appear, then
! 105904: ** this routine allocates an additional nEq memory cells for internal
! 105905: ** use.
! 105906: **
! 105907: ** Before returning, *pzAff is set to point to a buffer containing a
! 105908: ** copy of the column affinity string of the index allocated using
! 105909: ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
! 105910: ** with equality constraints that use NONE affinity are set to
! 105911: ** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
! 105912: **
! 105913: ** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
! 105914: ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
! 105915: **
! 105916: ** In the example above, the index on t1(a) has TEXT affinity. But since
! 105917: ** the right hand side of the equality constraint (t2.b) has NONE affinity,
! 105918: ** no conversion should be attempted before using a t2.b value as part of
! 105919: ** a key to search the index. Hence the first byte in the returned affinity
! 105920: ** string in this example would be set to SQLITE_AFF_NONE.
! 105921: */
! 105922: static int codeAllEqualityTerms(
! 105923: Parse *pParse, /* Parsing context */
! 105924: WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
! 105925: WhereClause *pWC, /* The WHERE clause */
! 105926: Bitmask notReady, /* Which parts of FROM have not yet been coded */
! 105927: int nExtraReg, /* Number of extra registers to allocate */
! 105928: char **pzAff /* OUT: Set to point to affinity string */
! 105929: ){
! 105930: int nEq = pLevel->plan.nEq; /* The number of == or IN constraints to code */
! 105931: Vdbe *v = pParse->pVdbe; /* The vm under construction */
! 105932: Index *pIdx; /* The index being used for this loop */
! 105933: int iCur = pLevel->iTabCur; /* The cursor of the table */
! 105934: WhereTerm *pTerm; /* A single constraint term */
! 105935: int j; /* Loop counter */
! 105936: int regBase; /* Base register */
! 105937: int nReg; /* Number of registers to allocate */
! 105938: char *zAff; /* Affinity string to return */
! 105939:
! 105940: /* This module is only called on query plans that use an index. */
! 105941: assert( pLevel->plan.wsFlags & WHERE_INDEXED );
! 105942: pIdx = pLevel->plan.u.pIdx;
! 105943:
! 105944: /* Figure out how many memory cells we will need then allocate them.
! 105945: */
! 105946: regBase = pParse->nMem + 1;
! 105947: nReg = pLevel->plan.nEq + nExtraReg;
! 105948: pParse->nMem += nReg;
! 105949:
! 105950: zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
! 105951: if( !zAff ){
! 105952: pParse->db->mallocFailed = 1;
! 105953: }
! 105954:
! 105955: /* Evaluate the equality constraints
! 105956: */
! 105957: assert( pIdx->nColumn>=nEq );
! 105958: for(j=0; j<nEq; j++){
! 105959: int r1;
! 105960: int k = pIdx->aiColumn[j];
! 105961: pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
! 105962: if( NEVER(pTerm==0) ) break;
! 105963: /* The following true for indices with redundant columns.
! 105964: ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
! 105965: testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
! 105966: testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
! 105967: r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
! 105968: if( r1!=regBase+j ){
! 105969: if( nReg==1 ){
! 105970: sqlite3ReleaseTempReg(pParse, regBase);
! 105971: regBase = r1;
! 105972: }else{
! 105973: sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
! 105974: }
! 105975: }
! 105976: testcase( pTerm->eOperator & WO_ISNULL );
! 105977: testcase( pTerm->eOperator & WO_IN );
! 105978: if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
! 105979: Expr *pRight = pTerm->pExpr->pRight;
! 105980: sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk);
! 105981: if( zAff ){
! 105982: if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
! 105983: zAff[j] = SQLITE_AFF_NONE;
! 105984: }
! 105985: if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
! 105986: zAff[j] = SQLITE_AFF_NONE;
! 105987: }
! 105988: }
! 105989: }
! 105990: }
! 105991: *pzAff = zAff;
! 105992: return regBase;
! 105993: }
! 105994:
! 105995: #ifndef SQLITE_OMIT_EXPLAIN
! 105996: /*
! 105997: ** This routine is a helper for explainIndexRange() below
! 105998: **
! 105999: ** pStr holds the text of an expression that we are building up one term
! 106000: ** at a time. This routine adds a new term to the end of the expression.
! 106001: ** Terms are separated by AND so add the "AND" text for second and subsequent
! 106002: ** terms only.
! 106003: */
! 106004: static void explainAppendTerm(
! 106005: StrAccum *pStr, /* The text expression being built */
! 106006: int iTerm, /* Index of this term. First is zero */
! 106007: const char *zColumn, /* Name of the column */
! 106008: const char *zOp /* Name of the operator */
! 106009: ){
! 106010: if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
! 106011: sqlite3StrAccumAppend(pStr, zColumn, -1);
! 106012: sqlite3StrAccumAppend(pStr, zOp, 1);
! 106013: sqlite3StrAccumAppend(pStr, "?", 1);
! 106014: }
! 106015:
! 106016: /*
! 106017: ** Argument pLevel describes a strategy for scanning table pTab. This
! 106018: ** function returns a pointer to a string buffer containing a description
! 106019: ** of the subset of table rows scanned by the strategy in the form of an
! 106020: ** SQL expression. Or, if all rows are scanned, NULL is returned.
! 106021: **
! 106022: ** For example, if the query:
! 106023: **
! 106024: ** SELECT * FROM t1 WHERE a=1 AND b>2;
! 106025: **
! 106026: ** is run and there is an index on (a, b), then this function returns a
! 106027: ** string similar to:
! 106028: **
! 106029: ** "a=? AND b>?"
! 106030: **
! 106031: ** The returned pointer points to memory obtained from sqlite3DbMalloc().
! 106032: ** It is the responsibility of the caller to free the buffer when it is
! 106033: ** no longer required.
! 106034: */
! 106035: static char *explainIndexRange(sqlite3 *db, WhereLevel *pLevel, Table *pTab){
! 106036: WherePlan *pPlan = &pLevel->plan;
! 106037: Index *pIndex = pPlan->u.pIdx;
! 106038: int nEq = pPlan->nEq;
! 106039: int i, j;
! 106040: Column *aCol = pTab->aCol;
! 106041: int *aiColumn = pIndex->aiColumn;
! 106042: StrAccum txt;
! 106043:
! 106044: if( nEq==0 && (pPlan->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
! 106045: return 0;
! 106046: }
! 106047: sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
! 106048: txt.db = db;
! 106049: sqlite3StrAccumAppend(&txt, " (", 2);
! 106050: for(i=0; i<nEq; i++){
! 106051: explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
! 106052: }
! 106053:
! 106054: j = i;
! 106055: if( pPlan->wsFlags&WHERE_BTM_LIMIT ){
! 106056: char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
! 106057: explainAppendTerm(&txt, i++, z, ">");
! 106058: }
! 106059: if( pPlan->wsFlags&WHERE_TOP_LIMIT ){
! 106060: char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
! 106061: explainAppendTerm(&txt, i, z, "<");
! 106062: }
! 106063: sqlite3StrAccumAppend(&txt, ")", 1);
! 106064: return sqlite3StrAccumFinish(&txt);
! 106065: }
! 106066:
! 106067: /*
! 106068: ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
! 106069: ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
! 106070: ** record is added to the output to describe the table scan strategy in
! 106071: ** pLevel.
! 106072: */
! 106073: static void explainOneScan(
! 106074: Parse *pParse, /* Parse context */
! 106075: SrcList *pTabList, /* Table list this loop refers to */
! 106076: WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
! 106077: int iLevel, /* Value for "level" column of output */
! 106078: int iFrom, /* Value for "from" column of output */
! 106079: u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
! 106080: ){
! 106081: if( pParse->explain==2 ){
! 106082: u32 flags = pLevel->plan.wsFlags;
! 106083: struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
! 106084: Vdbe *v = pParse->pVdbe; /* VM being constructed */
! 106085: sqlite3 *db = pParse->db; /* Database handle */
! 106086: char *zMsg; /* Text to add to EQP output */
! 106087: sqlite3_int64 nRow; /* Expected number of rows visited by scan */
! 106088: int iId = pParse->iSelectId; /* Select id (left-most output column) */
! 106089: int isSearch; /* True for a SEARCH. False for SCAN. */
! 106090:
! 106091: if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
! 106092:
! 106093: isSearch = (pLevel->plan.nEq>0)
! 106094: || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
! 106095: || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
! 106096:
! 106097: zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN");
! 106098: if( pItem->pSelect ){
! 106099: zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId);
! 106100: }else{
! 106101: zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName);
! 106102: }
! 106103:
! 106104: if( pItem->zAlias ){
! 106105: zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
! 106106: }
! 106107: if( (flags & WHERE_INDEXED)!=0 ){
! 106108: char *zWhere = explainIndexRange(db, pLevel, pItem->pTab);
! 106109: zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg,
! 106110: ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""),
! 106111: ((flags & WHERE_IDX_ONLY)?"COVERING ":""),
! 106112: ((flags & WHERE_TEMP_INDEX)?"":" "),
! 106113: ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName),
! 106114: zWhere
! 106115: );
! 106116: sqlite3DbFree(db, zWhere);
! 106117: }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
! 106118: zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
! 106119:
! 106120: if( flags&WHERE_ROWID_EQ ){
! 106121: zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
! 106122: }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
! 106123: zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
! 106124: }else if( flags&WHERE_BTM_LIMIT ){
! 106125: zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg);
! 106126: }else if( flags&WHERE_TOP_LIMIT ){
! 106127: zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg);
! 106128: }
! 106129: }
! 106130: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 106131: else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
! 106132: sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
! 106133: zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
! 106134: pVtabIdx->idxNum, pVtabIdx->idxStr);
! 106135: }
! 106136: #endif
! 106137: if( wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ){
! 106138: testcase( wctrlFlags & WHERE_ORDERBY_MIN );
! 106139: nRow = 1;
! 106140: }else{
! 106141: nRow = (sqlite3_int64)pLevel->plan.nRow;
! 106142: }
! 106143: zMsg = sqlite3MAppendf(db, zMsg, "%s (~%lld rows)", zMsg, nRow);
! 106144: sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
! 106145: }
! 106146: }
! 106147: #else
! 106148: # define explainOneScan(u,v,w,x,y,z)
! 106149: #endif /* SQLITE_OMIT_EXPLAIN */
! 106150:
! 106151:
! 106152: /*
! 106153: ** Generate code for the start of the iLevel-th loop in the WHERE clause
! 106154: ** implementation described by pWInfo.
! 106155: */
! 106156: static Bitmask codeOneLoopStart(
! 106157: WhereInfo *pWInfo, /* Complete information about the WHERE clause */
! 106158: int iLevel, /* Which level of pWInfo->a[] should be coded */
! 106159: u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */
! 106160: Bitmask notReady, /* Which tables are currently available */
! 106161: Expr *pWhere /* Complete WHERE clause */
! 106162: ){
! 106163: int j, k; /* Loop counters */
! 106164: int iCur; /* The VDBE cursor for the table */
! 106165: int addrNxt; /* Where to jump to continue with the next IN case */
! 106166: int omitTable; /* True if we use the index only */
! 106167: int bRev; /* True if we need to scan in reverse order */
! 106168: WhereLevel *pLevel; /* The where level to be coded */
! 106169: WhereClause *pWC; /* Decomposition of the entire WHERE clause */
! 106170: WhereTerm *pTerm; /* A WHERE clause term */
! 106171: Parse *pParse; /* Parsing context */
! 106172: Vdbe *v; /* The prepared stmt under constructions */
! 106173: struct SrcList_item *pTabItem; /* FROM clause term being coded */
! 106174: int addrBrk; /* Jump here to break out of the loop */
! 106175: int addrCont; /* Jump here to continue with next cycle */
! 106176: int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
! 106177: int iReleaseReg = 0; /* Temp register to free before returning */
! 106178:
! 106179: pParse = pWInfo->pParse;
! 106180: v = pParse->pVdbe;
! 106181: pWC = pWInfo->pWC;
! 106182: pLevel = &pWInfo->a[iLevel];
! 106183: pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
! 106184: iCur = pTabItem->iCursor;
! 106185: bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
! 106186: omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0
! 106187: && (wctrlFlags & WHERE_FORCE_TABLE)==0;
! 106188:
! 106189: /* Create labels for the "break" and "continue" instructions
! 106190: ** for the current loop. Jump to addrBrk to break out of a loop.
! 106191: ** Jump to cont to go immediately to the next iteration of the
! 106192: ** loop.
! 106193: **
! 106194: ** When there is an IN operator, we also have a "addrNxt" label that
! 106195: ** means to continue with the next IN value combination. When
! 106196: ** there are no IN operators in the constraints, the "addrNxt" label
! 106197: ** is the same as "addrBrk".
! 106198: */
! 106199: addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
! 106200: addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
! 106201:
! 106202: /* If this is the right table of a LEFT OUTER JOIN, allocate and
! 106203: ** initialize a memory cell that records if this table matches any
! 106204: ** row of the left table of the join.
! 106205: */
! 106206: if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
! 106207: pLevel->iLeftJoin = ++pParse->nMem;
! 106208: sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
! 106209: VdbeComment((v, "init LEFT JOIN no-match flag"));
! 106210: }
! 106211:
! 106212: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 106213: if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
! 106214: /* Case 0: The table is a virtual-table. Use the VFilter and VNext
! 106215: ** to access the data.
! 106216: */
! 106217: int iReg; /* P3 Value for OP_VFilter */
! 106218: sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
! 106219: int nConstraint = pVtabIdx->nConstraint;
! 106220: struct sqlite3_index_constraint_usage *aUsage =
! 106221: pVtabIdx->aConstraintUsage;
! 106222: const struct sqlite3_index_constraint *aConstraint =
! 106223: pVtabIdx->aConstraint;
! 106224:
! 106225: sqlite3ExprCachePush(pParse);
! 106226: iReg = sqlite3GetTempRange(pParse, nConstraint+2);
! 106227: for(j=1; j<=nConstraint; j++){
! 106228: for(k=0; k<nConstraint; k++){
! 106229: if( aUsage[k].argvIndex==j ){
! 106230: int iTerm = aConstraint[k].iTermOffset;
! 106231: sqlite3ExprCode(pParse, pWC->a[iTerm].pExpr->pRight, iReg+j+1);
! 106232: break;
! 106233: }
! 106234: }
! 106235: if( k==nConstraint ) break;
! 106236: }
! 106237: sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg);
! 106238: sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
! 106239: sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrBrk, iReg, pVtabIdx->idxStr,
! 106240: pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
! 106241: pVtabIdx->needToFreeIdxStr = 0;
! 106242: for(j=0; j<nConstraint; j++){
! 106243: if( aUsage[j].omit ){
! 106244: int iTerm = aConstraint[j].iTermOffset;
! 106245: disableTerm(pLevel, &pWC->a[iTerm]);
! 106246: }
! 106247: }
! 106248: pLevel->op = OP_VNext;
! 106249: pLevel->p1 = iCur;
! 106250: pLevel->p2 = sqlite3VdbeCurrentAddr(v);
! 106251: sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
! 106252: sqlite3ExprCachePop(pParse, 1);
! 106253: }else
! 106254: #endif /* SQLITE_OMIT_VIRTUALTABLE */
! 106255:
! 106256: if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){
! 106257: /* Case 1: We can directly reference a single row using an
! 106258: ** equality comparison against the ROWID field. Or
! 106259: ** we reference multiple rows using a "rowid IN (...)"
! 106260: ** construct.
! 106261: */
! 106262: iReleaseReg = sqlite3GetTempReg(pParse);
! 106263: pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
! 106264: assert( pTerm!=0 );
! 106265: assert( pTerm->pExpr!=0 );
! 106266: assert( pTerm->leftCursor==iCur );
! 106267: assert( omitTable==0 );
! 106268: testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
! 106269: iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg);
! 106270: addrNxt = pLevel->addrNxt;
! 106271: sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
! 106272: sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
! 106273: sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
! 106274: VdbeComment((v, "pk"));
! 106275: pLevel->op = OP_Noop;
! 106276: }else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){
! 106277: /* Case 2: We have an inequality comparison against the ROWID field.
! 106278: */
! 106279: int testOp = OP_Noop;
! 106280: int start;
! 106281: int memEndValue = 0;
! 106282: WhereTerm *pStart, *pEnd;
! 106283:
! 106284: assert( omitTable==0 );
! 106285: pStart = findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0);
! 106286: pEnd = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0);
! 106287: if( bRev ){
! 106288: pTerm = pStart;
! 106289: pStart = pEnd;
! 106290: pEnd = pTerm;
! 106291: }
! 106292: if( pStart ){
! 106293: Expr *pX; /* The expression that defines the start bound */
! 106294: int r1, rTemp; /* Registers for holding the start boundary */
! 106295:
! 106296: /* The following constant maps TK_xx codes into corresponding
! 106297: ** seek opcodes. It depends on a particular ordering of TK_xx
! 106298: */
! 106299: const u8 aMoveOp[] = {
! 106300: /* TK_GT */ OP_SeekGt,
! 106301: /* TK_LE */ OP_SeekLe,
! 106302: /* TK_LT */ OP_SeekLt,
! 106303: /* TK_GE */ OP_SeekGe
! 106304: };
! 106305: assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */
! 106306: assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */
! 106307: assert( TK_GE==TK_GT+3 ); /* ... is correcct. */
! 106308:
! 106309: testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
! 106310: pX = pStart->pExpr;
! 106311: assert( pX!=0 );
! 106312: assert( pStart->leftCursor==iCur );
! 106313: r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
! 106314: sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
! 106315: VdbeComment((v, "pk"));
! 106316: sqlite3ExprCacheAffinityChange(pParse, r1, 1);
! 106317: sqlite3ReleaseTempReg(pParse, rTemp);
! 106318: disableTerm(pLevel, pStart);
! 106319: }else{
! 106320: sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
! 106321: }
! 106322: if( pEnd ){
! 106323: Expr *pX;
! 106324: pX = pEnd->pExpr;
! 106325: assert( pX!=0 );
! 106326: assert( pEnd->leftCursor==iCur );
! 106327: testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
! 106328: memEndValue = ++pParse->nMem;
! 106329: sqlite3ExprCode(pParse, pX->pRight, memEndValue);
! 106330: if( pX->op==TK_LT || pX->op==TK_GT ){
! 106331: testOp = bRev ? OP_Le : OP_Ge;
! 106332: }else{
! 106333: testOp = bRev ? OP_Lt : OP_Gt;
! 106334: }
! 106335: disableTerm(pLevel, pEnd);
! 106336: }
! 106337: start = sqlite3VdbeCurrentAddr(v);
! 106338: pLevel->op = bRev ? OP_Prev : OP_Next;
! 106339: pLevel->p1 = iCur;
! 106340: pLevel->p2 = start;
! 106341: if( pStart==0 && pEnd==0 ){
! 106342: pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
! 106343: }else{
! 106344: assert( pLevel->p5==0 );
! 106345: }
! 106346: if( testOp!=OP_Noop ){
! 106347: iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
! 106348: sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
! 106349: sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
! 106350: sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
! 106351: sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
! 106352: }
! 106353: }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
! 106354: /* Case 3: A scan using an index.
! 106355: **
! 106356: ** The WHERE clause may contain zero or more equality
! 106357: ** terms ("==" or "IN" operators) that refer to the N
! 106358: ** left-most columns of the index. It may also contain
! 106359: ** inequality constraints (>, <, >= or <=) on the indexed
! 106360: ** column that immediately follows the N equalities. Only
! 106361: ** the right-most column can be an inequality - the rest must
! 106362: ** use the "==" and "IN" operators. For example, if the
! 106363: ** index is on (x,y,z), then the following clauses are all
! 106364: ** optimized:
! 106365: **
! 106366: ** x=5
! 106367: ** x=5 AND y=10
! 106368: ** x=5 AND y<10
! 106369: ** x=5 AND y>5 AND y<10
! 106370: ** x=5 AND y=5 AND z<=10
! 106371: **
! 106372: ** The z<10 term of the following cannot be used, only
! 106373: ** the x=5 term:
! 106374: **
! 106375: ** x=5 AND z<10
! 106376: **
! 106377: ** N may be zero if there are inequality constraints.
! 106378: ** If there are no inequality constraints, then N is at
! 106379: ** least one.
! 106380: **
! 106381: ** This case is also used when there are no WHERE clause
! 106382: ** constraints but an index is selected anyway, in order
! 106383: ** to force the output order to conform to an ORDER BY.
! 106384: */
! 106385: static const u8 aStartOp[] = {
! 106386: 0,
! 106387: 0,
! 106388: OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
! 106389: OP_Last, /* 3: (!start_constraints && startEq && bRev) */
! 106390: OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */
! 106391: OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */
! 106392: OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */
! 106393: OP_SeekLe /* 7: (start_constraints && startEq && bRev) */
! 106394: };
! 106395: static const u8 aEndOp[] = {
! 106396: OP_Noop, /* 0: (!end_constraints) */
! 106397: OP_IdxGE, /* 1: (end_constraints && !bRev) */
! 106398: OP_IdxLT /* 2: (end_constraints && bRev) */
! 106399: };
! 106400: int nEq = pLevel->plan.nEq; /* Number of == or IN terms */
! 106401: int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */
! 106402: int regBase; /* Base register holding constraint values */
! 106403: int r1; /* Temp register */
! 106404: WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
! 106405: WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
! 106406: int startEq; /* True if range start uses ==, >= or <= */
! 106407: int endEq; /* True if range end uses ==, >= or <= */
! 106408: int start_constraints; /* Start of range is constrained */
! 106409: int nConstraint; /* Number of constraint terms */
! 106410: Index *pIdx; /* The index we will be using */
! 106411: int iIdxCur; /* The VDBE cursor for the index */
! 106412: int nExtraReg = 0; /* Number of extra registers needed */
! 106413: int op; /* Instruction opcode */
! 106414: char *zStartAff; /* Affinity for start of range constraint */
! 106415: char *zEndAff; /* Affinity for end of range constraint */
! 106416:
! 106417: pIdx = pLevel->plan.u.pIdx;
! 106418: iIdxCur = pLevel->iIdxCur;
! 106419: k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]);
! 106420:
! 106421: /* If this loop satisfies a sort order (pOrderBy) request that
! 106422: ** was passed to this function to implement a "SELECT min(x) ..."
! 106423: ** query, then the caller will only allow the loop to run for
! 106424: ** a single iteration. This means that the first row returned
! 106425: ** should not have a NULL value stored in 'x'. If column 'x' is
! 106426: ** the first one after the nEq equality constraints in the index,
! 106427: ** this requires some special handling.
! 106428: */
! 106429: if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
! 106430: && (pLevel->plan.wsFlags&WHERE_ORDERBY)
! 106431: && (pIdx->nColumn>nEq)
! 106432: ){
! 106433: /* assert( pOrderBy->nExpr==1 ); */
! 106434: /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
! 106435: isMinQuery = 1;
! 106436: nExtraReg = 1;
! 106437: }
! 106438:
! 106439: /* Find any inequality constraint terms for the start and end
! 106440: ** of the range.
! 106441: */
! 106442: if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){
! 106443: pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx);
! 106444: nExtraReg = 1;
! 106445: }
! 106446: if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){
! 106447: pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx);
! 106448: nExtraReg = 1;
! 106449: }
! 106450:
! 106451: /* Generate code to evaluate all constraint terms using == or IN
! 106452: ** and store the values of those terms in an array of registers
! 106453: ** starting at regBase.
! 106454: */
! 106455: regBase = codeAllEqualityTerms(
! 106456: pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff
! 106457: );
! 106458: zEndAff = sqlite3DbStrDup(pParse->db, zStartAff);
! 106459: addrNxt = pLevel->addrNxt;
! 106460:
! 106461: /* If we are doing a reverse order scan on an ascending index, or
! 106462: ** a forward order scan on a descending index, interchange the
! 106463: ** start and end terms (pRangeStart and pRangeEnd).
! 106464: */
! 106465: if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
! 106466: || (bRev && pIdx->nColumn==nEq)
! 106467: ){
! 106468: SWAP(WhereTerm *, pRangeEnd, pRangeStart);
! 106469: }
! 106470:
! 106471: testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
! 106472: testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
! 106473: testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE );
! 106474: testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE );
! 106475: startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
! 106476: endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
! 106477: start_constraints = pRangeStart || nEq>0;
! 106478:
! 106479: /* Seek the index cursor to the start of the range. */
! 106480: nConstraint = nEq;
! 106481: if( pRangeStart ){
! 106482: Expr *pRight = pRangeStart->pExpr->pRight;
! 106483: sqlite3ExprCode(pParse, pRight, regBase+nEq);
! 106484: if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){
! 106485: sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
! 106486: }
! 106487: if( zStartAff ){
! 106488: if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
! 106489: /* Since the comparison is to be performed with no conversions
! 106490: ** applied to the operands, set the affinity to apply to pRight to
! 106491: ** SQLITE_AFF_NONE. */
! 106492: zStartAff[nEq] = SQLITE_AFF_NONE;
! 106493: }
! 106494: if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
! 106495: zStartAff[nEq] = SQLITE_AFF_NONE;
! 106496: }
! 106497: }
! 106498: nConstraint++;
! 106499: testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
! 106500: }else if( isMinQuery ){
! 106501: sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
! 106502: nConstraint++;
! 106503: startEq = 0;
! 106504: start_constraints = 1;
! 106505: }
! 106506: codeApplyAffinity(pParse, regBase, nConstraint, zStartAff);
! 106507: op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
! 106508: assert( op!=0 );
! 106509: testcase( op==OP_Rewind );
! 106510: testcase( op==OP_Last );
! 106511: testcase( op==OP_SeekGt );
! 106512: testcase( op==OP_SeekGe );
! 106513: testcase( op==OP_SeekLe );
! 106514: testcase( op==OP_SeekLt );
! 106515: sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
! 106516:
! 106517: /* Load the value for the inequality constraint at the end of the
! 106518: ** range (if any).
! 106519: */
! 106520: nConstraint = nEq;
! 106521: if( pRangeEnd ){
! 106522: Expr *pRight = pRangeEnd->pExpr->pRight;
! 106523: sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
! 106524: sqlite3ExprCode(pParse, pRight, regBase+nEq);
! 106525: if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
! 106526: sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
! 106527: }
! 106528: if( zEndAff ){
! 106529: if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
! 106530: /* Since the comparison is to be performed with no conversions
! 106531: ** applied to the operands, set the affinity to apply to pRight to
! 106532: ** SQLITE_AFF_NONE. */
! 106533: zEndAff[nEq] = SQLITE_AFF_NONE;
! 106534: }
! 106535: if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
! 106536: zEndAff[nEq] = SQLITE_AFF_NONE;
! 106537: }
! 106538: }
! 106539: codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
! 106540: nConstraint++;
! 106541: testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
! 106542: }
! 106543: sqlite3DbFree(pParse->db, zStartAff);
! 106544: sqlite3DbFree(pParse->db, zEndAff);
! 106545:
! 106546: /* Top of the loop body */
! 106547: pLevel->p2 = sqlite3VdbeCurrentAddr(v);
! 106548:
! 106549: /* Check if the index cursor is past the end of the range. */
! 106550: op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
! 106551: testcase( op==OP_Noop );
! 106552: testcase( op==OP_IdxGE );
! 106553: testcase( op==OP_IdxLT );
! 106554: if( op!=OP_Noop ){
! 106555: sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
! 106556: sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0);
! 106557: }
! 106558:
! 106559: /* If there are inequality constraints, check that the value
! 106560: ** of the table column that the inequality contrains is not NULL.
! 106561: ** If it is, jump to the next iteration of the loop.
! 106562: */
! 106563: r1 = sqlite3GetTempReg(pParse);
! 106564: testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT );
! 106565: testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT );
! 106566: if( (pLevel->plan.wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){
! 106567: sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
! 106568: sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
! 106569: }
! 106570: sqlite3ReleaseTempReg(pParse, r1);
! 106571:
! 106572: /* Seek the table cursor, if required */
! 106573: disableTerm(pLevel, pRangeStart);
! 106574: disableTerm(pLevel, pRangeEnd);
! 106575: if( !omitTable ){
! 106576: iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
! 106577: sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
! 106578: sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
! 106579: sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */
! 106580: }
! 106581:
! 106582: /* Record the instruction used to terminate the loop. Disable
! 106583: ** WHERE clause terms made redundant by the index range scan.
! 106584: */
! 106585: if( pLevel->plan.wsFlags & WHERE_UNIQUE ){
! 106586: pLevel->op = OP_Noop;
! 106587: }else if( bRev ){
! 106588: pLevel->op = OP_Prev;
! 106589: }else{
! 106590: pLevel->op = OP_Next;
! 106591: }
! 106592: pLevel->p1 = iIdxCur;
! 106593: }else
! 106594:
! 106595: #ifndef SQLITE_OMIT_OR_OPTIMIZATION
! 106596: if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
! 106597: /* Case 4: Two or more separately indexed terms connected by OR
! 106598: **
! 106599: ** Example:
! 106600: **
! 106601: ** CREATE TABLE t1(a,b,c,d);
! 106602: ** CREATE INDEX i1 ON t1(a);
! 106603: ** CREATE INDEX i2 ON t1(b);
! 106604: ** CREATE INDEX i3 ON t1(c);
! 106605: **
! 106606: ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
! 106607: **
! 106608: ** In the example, there are three indexed terms connected by OR.
! 106609: ** The top of the loop looks like this:
! 106610: **
! 106611: ** Null 1 # Zero the rowset in reg 1
! 106612: **
! 106613: ** Then, for each indexed term, the following. The arguments to
! 106614: ** RowSetTest are such that the rowid of the current row is inserted
! 106615: ** into the RowSet. If it is already present, control skips the
! 106616: ** Gosub opcode and jumps straight to the code generated by WhereEnd().
! 106617: **
! 106618: ** sqlite3WhereBegin(<term>)
! 106619: ** RowSetTest # Insert rowid into rowset
! 106620: ** Gosub 2 A
! 106621: ** sqlite3WhereEnd()
! 106622: **
! 106623: ** Following the above, code to terminate the loop. Label A, the target
! 106624: ** of the Gosub above, jumps to the instruction right after the Goto.
! 106625: **
! 106626: ** Null 1 # Zero the rowset in reg 1
! 106627: ** Goto B # The loop is finished.
! 106628: **
! 106629: ** A: <loop body> # Return data, whatever.
! 106630: **
! 106631: ** Return 2 # Jump back to the Gosub
! 106632: **
! 106633: ** B: <after the loop>
! 106634: **
! 106635: */
! 106636: WhereClause *pOrWc; /* The OR-clause broken out into subterms */
! 106637: SrcList *pOrTab; /* Shortened table list or OR-clause generation */
! 106638:
! 106639: int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
! 106640: int regRowset = 0; /* Register for RowSet object */
! 106641: int regRowid = 0; /* Register holding rowid */
! 106642: int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
! 106643: int iRetInit; /* Address of regReturn init */
! 106644: int untestedTerms = 0; /* Some terms not completely tested */
! 106645: int ii; /* Loop counter */
! 106646: Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
! 106647:
! 106648: pTerm = pLevel->plan.u.pTerm;
! 106649: assert( pTerm!=0 );
! 106650: assert( pTerm->eOperator==WO_OR );
! 106651: assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
! 106652: pOrWc = &pTerm->u.pOrInfo->wc;
! 106653: pLevel->op = OP_Return;
! 106654: pLevel->p1 = regReturn;
! 106655:
! 106656: /* Set up a new SrcList ni pOrTab containing the table being scanned
! 106657: ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
! 106658: ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
! 106659: */
! 106660: if( pWInfo->nLevel>1 ){
! 106661: int nNotReady; /* The number of notReady tables */
! 106662: struct SrcList_item *origSrc; /* Original list of tables */
! 106663: nNotReady = pWInfo->nLevel - iLevel - 1;
! 106664: pOrTab = sqlite3StackAllocRaw(pParse->db,
! 106665: sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
! 106666: if( pOrTab==0 ) return notReady;
! 106667: pOrTab->nAlloc = (i16)(nNotReady + 1);
! 106668: pOrTab->nSrc = pOrTab->nAlloc;
! 106669: memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
! 106670: origSrc = pWInfo->pTabList->a;
! 106671: for(k=1; k<=nNotReady; k++){
! 106672: memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
! 106673: }
! 106674: }else{
! 106675: pOrTab = pWInfo->pTabList;
! 106676: }
! 106677:
! 106678: /* Initialize the rowset register to contain NULL. An SQL NULL is
! 106679: ** equivalent to an empty rowset.
! 106680: **
! 106681: ** Also initialize regReturn to contain the address of the instruction
! 106682: ** immediately following the OP_Return at the bottom of the loop. This
! 106683: ** is required in a few obscure LEFT JOIN cases where control jumps
! 106684: ** over the top of the loop into the body of it. In this case the
! 106685: ** correct response for the end-of-loop code (the OP_Return) is to
! 106686: ** fall through to the next instruction, just as an OP_Next does if
! 106687: ** called on an uninitialized cursor.
! 106688: */
! 106689: if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
! 106690: regRowset = ++pParse->nMem;
! 106691: regRowid = ++pParse->nMem;
! 106692: sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
! 106693: }
! 106694: iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
! 106695:
! 106696: /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
! 106697: ** Then for every term xN, evaluate as the subexpression: xN AND z
! 106698: ** That way, terms in y that are factored into the disjunction will
! 106699: ** be picked up by the recursive calls to sqlite3WhereBegin() below.
! 106700: */
! 106701: if( pWC->nTerm>1 ){
! 106702: pAndExpr = sqlite3ExprAlloc(pParse->db, TK_AND, 0, 0);
! 106703: pAndExpr->pRight = pWhere;
! 106704: }
! 106705:
! 106706: for(ii=0; ii<pOrWc->nTerm; ii++){
! 106707: WhereTerm *pOrTerm = &pOrWc->a[ii];
! 106708: if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
! 106709: WhereInfo *pSubWInfo; /* Info for single OR-term scan */
! 106710: Expr *pOrExpr = pOrTerm->pExpr;
! 106711: if( pAndExpr ){
! 106712: pAndExpr->pLeft = pOrExpr;
! 106713: pOrExpr = pAndExpr;
! 106714: }
! 106715: /* Loop through table entries that match term pOrTerm. */
! 106716: pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
! 106717: WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
! 106718: WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
! 106719: if( pSubWInfo ){
! 106720: explainOneScan(
! 106721: pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
! 106722: );
! 106723: if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
! 106724: int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
! 106725: int r;
! 106726: r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur,
! 106727: regRowid);
! 106728: sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
! 106729: sqlite3VdbeCurrentAddr(v)+2, r, iSet);
! 106730: }
! 106731: sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
! 106732:
! 106733: /* The pSubWInfo->untestedTerms flag means that this OR term
! 106734: ** contained one or more AND term from a notReady table. The
! 106735: ** terms from the notReady table could not be tested and will
! 106736: ** need to be tested later.
! 106737: */
! 106738: if( pSubWInfo->untestedTerms ) untestedTerms = 1;
! 106739:
! 106740: /* Finish the loop through table entries that match term pOrTerm. */
! 106741: sqlite3WhereEnd(pSubWInfo);
! 106742: }
! 106743: }
! 106744: }
! 106745: sqlite3DbFree(pParse->db, pAndExpr);
! 106746: sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
! 106747: sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
! 106748: sqlite3VdbeResolveLabel(v, iLoopBody);
! 106749:
! 106750: if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
! 106751: if( !untestedTerms ) disableTerm(pLevel, pTerm);
! 106752: }else
! 106753: #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
! 106754:
! 106755: {
! 106756: /* Case 5: There is no usable index. We must do a complete
! 106757: ** scan of the entire table.
! 106758: */
! 106759: static const u8 aStep[] = { OP_Next, OP_Prev };
! 106760: static const u8 aStart[] = { OP_Rewind, OP_Last };
! 106761: assert( bRev==0 || bRev==1 );
! 106762: assert( omitTable==0 );
! 106763: pLevel->op = aStep[bRev];
! 106764: pLevel->p1 = iCur;
! 106765: pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
! 106766: pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
! 106767: }
! 106768: notReady &= ~getMask(pWC->pMaskSet, iCur);
! 106769:
! 106770: /* Insert code to test every subexpression that can be completely
! 106771: ** computed using the current set of tables.
! 106772: **
! 106773: ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
! 106774: ** the use of indices become tests that are evaluated against each row of
! 106775: ** the relevant input tables.
! 106776: */
! 106777: for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
! 106778: Expr *pE;
! 106779: testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
! 106780: testcase( pTerm->wtFlags & TERM_CODED );
! 106781: if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
! 106782: if( (pTerm->prereqAll & notReady)!=0 ){
! 106783: testcase( pWInfo->untestedTerms==0
! 106784: && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
! 106785: pWInfo->untestedTerms = 1;
! 106786: continue;
! 106787: }
! 106788: pE = pTerm->pExpr;
! 106789: assert( pE!=0 );
! 106790: if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
! 106791: continue;
! 106792: }
! 106793: sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
! 106794: pTerm->wtFlags |= TERM_CODED;
! 106795: }
! 106796:
! 106797: /* For a LEFT OUTER JOIN, generate code that will record the fact that
! 106798: ** at least one row of the right table has matched the left table.
! 106799: */
! 106800: if( pLevel->iLeftJoin ){
! 106801: pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
! 106802: sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
! 106803: VdbeComment((v, "record LEFT JOIN hit"));
! 106804: sqlite3ExprCacheClear(pParse);
! 106805: for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
! 106806: testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
! 106807: testcase( pTerm->wtFlags & TERM_CODED );
! 106808: if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
! 106809: if( (pTerm->prereqAll & notReady)!=0 ){
! 106810: assert( pWInfo->untestedTerms );
! 106811: continue;
! 106812: }
! 106813: assert( pTerm->pExpr );
! 106814: sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
! 106815: pTerm->wtFlags |= TERM_CODED;
! 106816: }
! 106817: }
! 106818: sqlite3ReleaseTempReg(pParse, iReleaseReg);
! 106819:
! 106820: return notReady;
! 106821: }
! 106822:
! 106823: #if defined(SQLITE_TEST)
! 106824: /*
! 106825: ** The following variable holds a text description of query plan generated
! 106826: ** by the most recent call to sqlite3WhereBegin(). Each call to WhereBegin
! 106827: ** overwrites the previous. This information is used for testing and
! 106828: ** analysis only.
! 106829: */
! 106830: SQLITE_API char sqlite3_query_plan[BMS*2*40]; /* Text of the join */
! 106831: static int nQPlan = 0; /* Next free slow in _query_plan[] */
! 106832:
! 106833: #endif /* SQLITE_TEST */
! 106834:
! 106835:
! 106836: /*
! 106837: ** Free a WhereInfo structure
! 106838: */
! 106839: static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
! 106840: if( ALWAYS(pWInfo) ){
! 106841: int i;
! 106842: for(i=0; i<pWInfo->nLevel; i++){
! 106843: sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
! 106844: if( pInfo ){
! 106845: /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
! 106846: if( pInfo->needToFreeIdxStr ){
! 106847: sqlite3_free(pInfo->idxStr);
! 106848: }
! 106849: sqlite3DbFree(db, pInfo);
! 106850: }
! 106851: if( pWInfo->a[i].plan.wsFlags & WHERE_TEMP_INDEX ){
! 106852: Index *pIdx = pWInfo->a[i].plan.u.pIdx;
! 106853: if( pIdx ){
! 106854: sqlite3DbFree(db, pIdx->zColAff);
! 106855: sqlite3DbFree(db, pIdx);
! 106856: }
! 106857: }
! 106858: }
! 106859: whereClauseClear(pWInfo->pWC);
! 106860: sqlite3DbFree(db, pWInfo);
! 106861: }
! 106862: }
! 106863:
! 106864:
! 106865: /*
! 106866: ** Generate the beginning of the loop used for WHERE clause processing.
! 106867: ** The return value is a pointer to an opaque structure that contains
! 106868: ** information needed to terminate the loop. Later, the calling routine
! 106869: ** should invoke sqlite3WhereEnd() with the return value of this function
! 106870: ** in order to complete the WHERE clause processing.
! 106871: **
! 106872: ** If an error occurs, this routine returns NULL.
! 106873: **
! 106874: ** The basic idea is to do a nested loop, one loop for each table in
! 106875: ** the FROM clause of a select. (INSERT and UPDATE statements are the
! 106876: ** same as a SELECT with only a single table in the FROM clause.) For
! 106877: ** example, if the SQL is this:
! 106878: **
! 106879: ** SELECT * FROM t1, t2, t3 WHERE ...;
! 106880: **
! 106881: ** Then the code generated is conceptually like the following:
! 106882: **
! 106883: ** foreach row1 in t1 do \ Code generated
! 106884: ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
! 106885: ** foreach row3 in t3 do /
! 106886: ** ...
! 106887: ** end \ Code generated
! 106888: ** end |-- by sqlite3WhereEnd()
! 106889: ** end /
! 106890: **
! 106891: ** Note that the loops might not be nested in the order in which they
! 106892: ** appear in the FROM clause if a different order is better able to make
! 106893: ** use of indices. Note also that when the IN operator appears in
! 106894: ** the WHERE clause, it might result in additional nested loops for
! 106895: ** scanning through all values on the right-hand side of the IN.
! 106896: **
! 106897: ** There are Btree cursors associated with each table. t1 uses cursor
! 106898: ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
! 106899: ** And so forth. This routine generates code to open those VDBE cursors
! 106900: ** and sqlite3WhereEnd() generates the code to close them.
! 106901: **
! 106902: ** The code that sqlite3WhereBegin() generates leaves the cursors named
! 106903: ** in pTabList pointing at their appropriate entries. The [...] code
! 106904: ** can use OP_Column and OP_Rowid opcodes on these cursors to extract
! 106905: ** data from the various tables of the loop.
! 106906: **
! 106907: ** If the WHERE clause is empty, the foreach loops must each scan their
! 106908: ** entire tables. Thus a three-way join is an O(N^3) operation. But if
! 106909: ** the tables have indices and there are terms in the WHERE clause that
! 106910: ** refer to those indices, a complete table scan can be avoided and the
! 106911: ** code will run much faster. Most of the work of this routine is checking
! 106912: ** to see if there are indices that can be used to speed up the loop.
! 106913: **
! 106914: ** Terms of the WHERE clause are also used to limit which rows actually
! 106915: ** make it to the "..." in the middle of the loop. After each "foreach",
! 106916: ** terms of the WHERE clause that use only terms in that loop and outer
! 106917: ** loops are evaluated and if false a jump is made around all subsequent
! 106918: ** inner loops (or around the "..." if the test occurs within the inner-
! 106919: ** most loop)
! 106920: **
! 106921: ** OUTER JOINS
! 106922: **
! 106923: ** An outer join of tables t1 and t2 is conceptally coded as follows:
! 106924: **
! 106925: ** foreach row1 in t1 do
! 106926: ** flag = 0
! 106927: ** foreach row2 in t2 do
! 106928: ** start:
! 106929: ** ...
! 106930: ** flag = 1
! 106931: ** end
! 106932: ** if flag==0 then
! 106933: ** move the row2 cursor to a null row
! 106934: ** goto start
! 106935: ** fi
! 106936: ** end
! 106937: **
! 106938: ** ORDER BY CLAUSE PROCESSING
! 106939: **
! 106940: ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
! 106941: ** if there is one. If there is no ORDER BY clause or if this routine
! 106942: ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
! 106943: **
! 106944: ** If an index can be used so that the natural output order of the table
! 106945: ** scan is correct for the ORDER BY clause, then that index is used and
! 106946: ** *ppOrderBy is set to NULL. This is an optimization that prevents an
! 106947: ** unnecessary sort of the result set if an index appropriate for the
! 106948: ** ORDER BY clause already exists.
! 106949: **
! 106950: ** If the where clause loops cannot be arranged to provide the correct
! 106951: ** output order, then the *ppOrderBy is unchanged.
! 106952: */
! 106953: SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
! 106954: Parse *pParse, /* The parser context */
! 106955: SrcList *pTabList, /* A list of all tables to be scanned */
! 106956: Expr *pWhere, /* The WHERE clause */
! 106957: ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
! 106958: ExprList *pDistinct, /* The select-list for DISTINCT queries - or NULL */
! 106959: u16 wctrlFlags /* One of the WHERE_* flags defined in sqliteInt.h */
! 106960: ){
! 106961: int i; /* Loop counter */
! 106962: int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
! 106963: int nTabList; /* Number of elements in pTabList */
! 106964: WhereInfo *pWInfo; /* Will become the return value of this function */
! 106965: Vdbe *v = pParse->pVdbe; /* The virtual database engine */
! 106966: Bitmask notReady; /* Cursors that are not yet positioned */
! 106967: WhereMaskSet *pMaskSet; /* The expression mask set */
! 106968: WhereClause *pWC; /* Decomposition of the WHERE clause */
! 106969: struct SrcList_item *pTabItem; /* A single entry from pTabList */
! 106970: WhereLevel *pLevel; /* A single level in the pWInfo list */
! 106971: int iFrom; /* First unused FROM clause element */
! 106972: int andFlags; /* AND-ed combination of all pWC->a[].wtFlags */
! 106973: sqlite3 *db; /* Database connection */
! 106974:
! 106975: /* The number of tables in the FROM clause is limited by the number of
! 106976: ** bits in a Bitmask
! 106977: */
! 106978: testcase( pTabList->nSrc==BMS );
! 106979: if( pTabList->nSrc>BMS ){
! 106980: sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
! 106981: return 0;
! 106982: }
! 106983:
! 106984: /* This function normally generates a nested loop for all tables in
! 106985: ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should
! 106986: ** only generate code for the first table in pTabList and assume that
! 106987: ** any cursors associated with subsequent tables are uninitialized.
! 106988: */
! 106989: nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
! 106990:
! 106991: /* Allocate and initialize the WhereInfo structure that will become the
! 106992: ** return value. A single allocation is used to store the WhereInfo
! 106993: ** struct, the contents of WhereInfo.a[], the WhereClause structure
! 106994: ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
! 106995: ** field (type Bitmask) it must be aligned on an 8-byte boundary on
! 106996: ** some architectures. Hence the ROUND8() below.
! 106997: */
! 106998: db = pParse->db;
! 106999: nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
! 107000: pWInfo = sqlite3DbMallocZero(db,
! 107001: nByteWInfo +
! 107002: sizeof(WhereClause) +
! 107003: sizeof(WhereMaskSet)
! 107004: );
! 107005: if( db->mallocFailed ){
! 107006: sqlite3DbFree(db, pWInfo);
! 107007: pWInfo = 0;
! 107008: goto whereBeginError;
! 107009: }
! 107010: pWInfo->nLevel = nTabList;
! 107011: pWInfo->pParse = pParse;
! 107012: pWInfo->pTabList = pTabList;
! 107013: pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
! 107014: pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
! 107015: pWInfo->wctrlFlags = wctrlFlags;
! 107016: pWInfo->savedNQueryLoop = pParse->nQueryLoop;
! 107017: pMaskSet = (WhereMaskSet*)&pWC[1];
! 107018:
! 107019: /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
! 107020: ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
! 107021: if( db->flags & SQLITE_DistinctOpt ) pDistinct = 0;
! 107022:
! 107023: /* Split the WHERE clause into separate subexpressions where each
! 107024: ** subexpression is separated by an AND operator.
! 107025: */
! 107026: initMaskSet(pMaskSet);
! 107027: whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags);
! 107028: sqlite3ExprCodeConstants(pParse, pWhere);
! 107029: whereSplit(pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */
! 107030:
! 107031: /* Special case: a WHERE clause that is constant. Evaluate the
! 107032: ** expression and either jump over all of the code or fall thru.
! 107033: */
! 107034: if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
! 107035: sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
! 107036: pWhere = 0;
! 107037: }
! 107038:
! 107039: /* Assign a bit from the bitmask to every term in the FROM clause.
! 107040: **
! 107041: ** When assigning bitmask values to FROM clause cursors, it must be
! 107042: ** the case that if X is the bitmask for the N-th FROM clause term then
! 107043: ** the bitmask for all FROM clause terms to the left of the N-th term
! 107044: ** is (X-1). An expression from the ON clause of a LEFT JOIN can use
! 107045: ** its Expr.iRightJoinTable value to find the bitmask of the right table
! 107046: ** of the join. Subtracting one from the right table bitmask gives a
! 107047: ** bitmask for all tables to the left of the join. Knowing the bitmask
! 107048: ** for all tables to the left of a left join is important. Ticket #3015.
! 107049: **
! 107050: ** Configure the WhereClause.vmask variable so that bits that correspond
! 107051: ** to virtual table cursors are set. This is used to selectively disable
! 107052: ** the OR-to-IN transformation in exprAnalyzeOrTerm(). It is not helpful
! 107053: ** with virtual tables.
! 107054: **
! 107055: ** Note that bitmasks are created for all pTabList->nSrc tables in
! 107056: ** pTabList, not just the first nTabList tables. nTabList is normally
! 107057: ** equal to pTabList->nSrc but might be shortened to 1 if the
! 107058: ** WHERE_ONETABLE_ONLY flag is set.
! 107059: */
! 107060: assert( pWC->vmask==0 && pMaskSet->n==0 );
! 107061: for(i=0; i<pTabList->nSrc; i++){
! 107062: createMask(pMaskSet, pTabList->a[i].iCursor);
! 107063: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 107064: if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
! 107065: pWC->vmask |= ((Bitmask)1 << i);
! 107066: }
! 107067: #endif
! 107068: }
! 107069: #ifndef NDEBUG
! 107070: {
! 107071: Bitmask toTheLeft = 0;
! 107072: for(i=0; i<pTabList->nSrc; i++){
! 107073: Bitmask m = getMask(pMaskSet, pTabList->a[i].iCursor);
! 107074: assert( (m-1)==toTheLeft );
! 107075: toTheLeft |= m;
! 107076: }
! 107077: }
! 107078: #endif
! 107079:
! 107080: /* Analyze all of the subexpressions. Note that exprAnalyze() might
! 107081: ** add new virtual terms onto the end of the WHERE clause. We do not
! 107082: ** want to analyze these virtual terms, so start analyzing at the end
! 107083: ** and work forward so that the added virtual terms are never processed.
! 107084: */
! 107085: exprAnalyzeAll(pTabList, pWC);
! 107086: if( db->mallocFailed ){
! 107087: goto whereBeginError;
! 107088: }
! 107089:
! 107090: /* Check if the DISTINCT qualifier, if there is one, is redundant.
! 107091: ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
! 107092: ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
! 107093: */
! 107094: if( pDistinct && isDistinctRedundant(pParse, pTabList, pWC, pDistinct) ){
! 107095: pDistinct = 0;
! 107096: pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
! 107097: }
! 107098:
! 107099: /* Chose the best index to use for each table in the FROM clause.
! 107100: **
! 107101: ** This loop fills in the following fields:
! 107102: **
! 107103: ** pWInfo->a[].pIdx The index to use for this level of the loop.
! 107104: ** pWInfo->a[].wsFlags WHERE_xxx flags associated with pIdx
! 107105: ** pWInfo->a[].nEq The number of == and IN constraints
! 107106: ** pWInfo->a[].iFrom Which term of the FROM clause is being coded
! 107107: ** pWInfo->a[].iTabCur The VDBE cursor for the database table
! 107108: ** pWInfo->a[].iIdxCur The VDBE cursor for the index
! 107109: ** pWInfo->a[].pTerm When wsFlags==WO_OR, the OR-clause term
! 107110: **
! 107111: ** This loop also figures out the nesting order of tables in the FROM
! 107112: ** clause.
! 107113: */
! 107114: notReady = ~(Bitmask)0;
! 107115: andFlags = ~0;
! 107116: WHERETRACE(("*** Optimizer Start ***\n"));
! 107117: for(i=iFrom=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
! 107118: WhereCost bestPlan; /* Most efficient plan seen so far */
! 107119: Index *pIdx; /* Index for FROM table at pTabItem */
! 107120: int j; /* For looping over FROM tables */
! 107121: int bestJ = -1; /* The value of j */
! 107122: Bitmask m; /* Bitmask value for j or bestJ */
! 107123: int isOptimal; /* Iterator for optimal/non-optimal search */
! 107124: int nUnconstrained; /* Number tables without INDEXED BY */
! 107125: Bitmask notIndexed; /* Mask of tables that cannot use an index */
! 107126:
! 107127: memset(&bestPlan, 0, sizeof(bestPlan));
! 107128: bestPlan.rCost = SQLITE_BIG_DBL;
! 107129: WHERETRACE(("*** Begin search for loop %d ***\n", i));
! 107130:
! 107131: /* Loop through the remaining entries in the FROM clause to find the
! 107132: ** next nested loop. The loop tests all FROM clause entries
! 107133: ** either once or twice.
! 107134: **
! 107135: ** The first test is always performed if there are two or more entries
! 107136: ** remaining and never performed if there is only one FROM clause entry
! 107137: ** to choose from. The first test looks for an "optimal" scan. In
! 107138: ** this context an optimal scan is one that uses the same strategy
! 107139: ** for the given FROM clause entry as would be selected if the entry
! 107140: ** were used as the innermost nested loop. In other words, a table
! 107141: ** is chosen such that the cost of running that table cannot be reduced
! 107142: ** by waiting for other tables to run first. This "optimal" test works
! 107143: ** by first assuming that the FROM clause is on the inner loop and finding
! 107144: ** its query plan, then checking to see if that query plan uses any
! 107145: ** other FROM clause terms that are notReady. If no notReady terms are
! 107146: ** used then the "optimal" query plan works.
! 107147: **
! 107148: ** Note that the WhereCost.nRow parameter for an optimal scan might
! 107149: ** not be as small as it would be if the table really were the innermost
! 107150: ** join. The nRow value can be reduced by WHERE clause constraints
! 107151: ** that do not use indices. But this nRow reduction only happens if the
! 107152: ** table really is the innermost join.
! 107153: **
! 107154: ** The second loop iteration is only performed if no optimal scan
! 107155: ** strategies were found by the first iteration. This second iteration
! 107156: ** is used to search for the lowest cost scan overall.
! 107157: **
! 107158: ** Previous versions of SQLite performed only the second iteration -
! 107159: ** the next outermost loop was always that with the lowest overall
! 107160: ** cost. However, this meant that SQLite could select the wrong plan
! 107161: ** for scripts such as the following:
! 107162: **
! 107163: ** CREATE TABLE t1(a, b);
! 107164: ** CREATE TABLE t2(c, d);
! 107165: ** SELECT * FROM t2, t1 WHERE t2.rowid = t1.a;
! 107166: **
! 107167: ** The best strategy is to iterate through table t1 first. However it
! 107168: ** is not possible to determine this with a simple greedy algorithm.
! 107169: ** Since the cost of a linear scan through table t2 is the same
! 107170: ** as the cost of a linear scan through table t1, a simple greedy
! 107171: ** algorithm may choose to use t2 for the outer loop, which is a much
! 107172: ** costlier approach.
! 107173: */
! 107174: nUnconstrained = 0;
! 107175: notIndexed = 0;
! 107176: for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
! 107177: Bitmask mask; /* Mask of tables not yet ready */
! 107178: for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
! 107179: int doNotReorder; /* True if this table should not be reordered */
! 107180: WhereCost sCost; /* Cost information from best[Virtual]Index() */
! 107181: ExprList *pOrderBy; /* ORDER BY clause for index to optimize */
! 107182: ExprList *pDist; /* DISTINCT clause for index to optimize */
! 107183:
! 107184: doNotReorder = (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
! 107185: if( j!=iFrom && doNotReorder ) break;
! 107186: m = getMask(pMaskSet, pTabItem->iCursor);
! 107187: if( (m & notReady)==0 ){
! 107188: if( j==iFrom ) iFrom++;
! 107189: continue;
! 107190: }
! 107191: mask = (isOptimal ? m : notReady);
! 107192: pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
! 107193: pDist = (i==0 ? pDistinct : 0);
! 107194: if( pTabItem->pIndex==0 ) nUnconstrained++;
! 107195:
! 107196: WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
! 107197: j, isOptimal));
! 107198: assert( pTabItem->pTab );
! 107199: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 107200: if( IsVirtual(pTabItem->pTab) ){
! 107201: sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo;
! 107202: bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
! 107203: &sCost, pp);
! 107204: }else
! 107205: #endif
! 107206: {
! 107207: bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
! 107208: pDist, &sCost);
! 107209: }
! 107210: assert( isOptimal || (sCost.used¬Ready)==0 );
! 107211:
! 107212: /* If an INDEXED BY clause is present, then the plan must use that
! 107213: ** index if it uses any index at all */
! 107214: assert( pTabItem->pIndex==0
! 107215: || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
! 107216: || sCost.plan.u.pIdx==pTabItem->pIndex );
! 107217:
! 107218: if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
! 107219: notIndexed |= m;
! 107220: }
! 107221:
! 107222: /* Conditions under which this table becomes the best so far:
! 107223: **
! 107224: ** (1) The table must not depend on other tables that have not
! 107225: ** yet run.
! 107226: **
! 107227: ** (2) A full-table-scan plan cannot supercede indexed plan unless
! 107228: ** the full-table-scan is an "optimal" plan as defined above.
! 107229: **
! 107230: ** (3) All tables have an INDEXED BY clause or this table lacks an
! 107231: ** INDEXED BY clause or this table uses the specific
! 107232: ** index specified by its INDEXED BY clause. This rule ensures
! 107233: ** that a best-so-far is always selected even if an impossible
! 107234: ** combination of INDEXED BY clauses are given. The error
! 107235: ** will be detected and relayed back to the application later.
! 107236: ** The NEVER() comes about because rule (2) above prevents
! 107237: ** An indexable full-table-scan from reaching rule (3).
! 107238: **
! 107239: ** (4) The plan cost must be lower than prior plans or else the
! 107240: ** cost must be the same and the number of rows must be lower.
! 107241: */
! 107242: if( (sCost.used¬Ready)==0 /* (1) */
! 107243: && (bestJ<0 || (notIndexed&m)!=0 /* (2) */
! 107244: || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
! 107245: || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
! 107246: && (nUnconstrained==0 || pTabItem->pIndex==0 /* (3) */
! 107247: || NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
! 107248: && (bestJ<0 || sCost.rCost<bestPlan.rCost /* (4) */
! 107249: || (sCost.rCost<=bestPlan.rCost
! 107250: && sCost.plan.nRow<bestPlan.plan.nRow))
! 107251: ){
! 107252: WHERETRACE(("=== table %d is best so far"
! 107253: " with cost=%g and nRow=%g\n",
! 107254: j, sCost.rCost, sCost.plan.nRow));
! 107255: bestPlan = sCost;
! 107256: bestJ = j;
! 107257: }
! 107258: if( doNotReorder ) break;
! 107259: }
! 107260: }
! 107261: assert( bestJ>=0 );
! 107262: assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
! 107263: WHERETRACE(("*** Optimizer selects table %d for loop %d"
! 107264: " with cost=%g and nRow=%g\n",
! 107265: bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
! 107266: /* The ALWAYS() that follows was added to hush up clang scan-build */
! 107267: if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 && ALWAYS(ppOrderBy) ){
! 107268: *ppOrderBy = 0;
! 107269: }
! 107270: if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
! 107271: assert( pWInfo->eDistinct==0 );
! 107272: pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
! 107273: }
! 107274: andFlags &= bestPlan.plan.wsFlags;
! 107275: pLevel->plan = bestPlan.plan;
! 107276: testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
! 107277: testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
! 107278: if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
! 107279: pLevel->iIdxCur = pParse->nTab++;
! 107280: }else{
! 107281: pLevel->iIdxCur = -1;
! 107282: }
! 107283: notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
! 107284: pLevel->iFrom = (u8)bestJ;
! 107285: if( bestPlan.plan.nRow>=(double)1 ){
! 107286: pParse->nQueryLoop *= bestPlan.plan.nRow;
! 107287: }
! 107288:
! 107289: /* Check that if the table scanned by this loop iteration had an
! 107290: ** INDEXED BY clause attached to it, that the named index is being
! 107291: ** used for the scan. If not, then query compilation has failed.
! 107292: ** Return an error.
! 107293: */
! 107294: pIdx = pTabList->a[bestJ].pIndex;
! 107295: if( pIdx ){
! 107296: if( (bestPlan.plan.wsFlags & WHERE_INDEXED)==0 ){
! 107297: sqlite3ErrorMsg(pParse, "cannot use index: %s", pIdx->zName);
! 107298: goto whereBeginError;
! 107299: }else{
! 107300: /* If an INDEXED BY clause is used, the bestIndex() function is
! 107301: ** guaranteed to find the index specified in the INDEXED BY clause
! 107302: ** if it find an index at all. */
! 107303: assert( bestPlan.plan.u.pIdx==pIdx );
! 107304: }
! 107305: }
! 107306: }
! 107307: WHERETRACE(("*** Optimizer Finished ***\n"));
! 107308: if( pParse->nErr || db->mallocFailed ){
! 107309: goto whereBeginError;
! 107310: }
! 107311:
! 107312: /* If the total query only selects a single row, then the ORDER BY
! 107313: ** clause is irrelevant.
! 107314: */
! 107315: if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
! 107316: *ppOrderBy = 0;
! 107317: }
! 107318:
! 107319: /* If the caller is an UPDATE or DELETE statement that is requesting
! 107320: ** to use a one-pass algorithm, determine if this is appropriate.
! 107321: ** The one-pass algorithm only works if the WHERE clause constraints
! 107322: ** the statement to update a single row.
! 107323: */
! 107324: assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
! 107325: if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){
! 107326: pWInfo->okOnePass = 1;
! 107327: pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY;
! 107328: }
! 107329:
! 107330: /* Open all tables in the pTabList and any indices selected for
! 107331: ** searching those tables.
! 107332: */
! 107333: sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
! 107334: notReady = ~(Bitmask)0;
! 107335: pWInfo->nRowOut = (double)1;
! 107336: for(i=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
! 107337: Table *pTab; /* Table to open */
! 107338: int iDb; /* Index of database containing table/index */
! 107339:
! 107340: pTabItem = &pTabList->a[pLevel->iFrom];
! 107341: pTab = pTabItem->pTab;
! 107342: pLevel->iTabCur = pTabItem->iCursor;
! 107343: pWInfo->nRowOut *= pLevel->plan.nRow;
! 107344: iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
! 107345: if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
! 107346: /* Do nothing */
! 107347: }else
! 107348: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 107349: if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
! 107350: const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
! 107351: int iCur = pTabItem->iCursor;
! 107352: sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
! 107353: }else
! 107354: #endif
! 107355: if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
! 107356: && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
! 107357: int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
! 107358: sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
! 107359: testcase( pTab->nCol==BMS-1 );
! 107360: testcase( pTab->nCol==BMS );
! 107361: if( !pWInfo->okOnePass && pTab->nCol<BMS ){
! 107362: Bitmask b = pTabItem->colUsed;
! 107363: int n = 0;
! 107364: for(; b; b=b>>1, n++){}
! 107365: sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
! 107366: SQLITE_INT_TO_PTR(n), P4_INT32);
! 107367: assert( n<=pTab->nCol );
! 107368: }
! 107369: }else{
! 107370: sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
! 107371: }
! 107372: #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
! 107373: if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
! 107374: constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel);
! 107375: }else
! 107376: #endif
! 107377: if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
! 107378: Index *pIx = pLevel->plan.u.pIdx;
! 107379: KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
! 107380: int iIdxCur = pLevel->iIdxCur;
! 107381: assert( pIx->pSchema==pTab->pSchema );
! 107382: assert( iIdxCur>=0 );
! 107383: sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb,
! 107384: (char*)pKey, P4_KEYINFO_HANDOFF);
! 107385: VdbeComment((v, "%s", pIx->zName));
! 107386: }
! 107387: sqlite3CodeVerifySchema(pParse, iDb);
! 107388: notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor);
! 107389: }
! 107390: pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
! 107391: if( db->mallocFailed ) goto whereBeginError;
! 107392:
! 107393: /* Generate the code to do the search. Each iteration of the for
! 107394: ** loop below generates code for a single nested loop of the VM
! 107395: ** program.
! 107396: */
! 107397: notReady = ~(Bitmask)0;
! 107398: for(i=0; i<nTabList; i++){
! 107399: pLevel = &pWInfo->a[i];
! 107400: explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
! 107401: notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady, pWhere);
! 107402: pWInfo->iContinue = pLevel->addrCont;
! 107403: }
! 107404:
! 107405: #ifdef SQLITE_TEST /* For testing and debugging use only */
! 107406: /* Record in the query plan information about the current table
! 107407: ** and the index used to access it (if any). If the table itself
! 107408: ** is not used, its name is just '{}'. If no index is used
! 107409: ** the index is listed as "{}". If the primary key is used the
! 107410: ** index name is '*'.
! 107411: */
! 107412: for(i=0; i<nTabList; i++){
! 107413: char *z;
! 107414: int n;
! 107415: pLevel = &pWInfo->a[i];
! 107416: pTabItem = &pTabList->a[pLevel->iFrom];
! 107417: z = pTabItem->zAlias;
! 107418: if( z==0 ) z = pTabItem->pTab->zName;
! 107419: n = sqlite3Strlen30(z);
! 107420: if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
! 107421: if( pLevel->plan.wsFlags & WHERE_IDX_ONLY ){
! 107422: memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
! 107423: nQPlan += 2;
! 107424: }else{
! 107425: memcpy(&sqlite3_query_plan[nQPlan], z, n);
! 107426: nQPlan += n;
! 107427: }
! 107428: sqlite3_query_plan[nQPlan++] = ' ';
! 107429: }
! 107430: testcase( pLevel->plan.wsFlags & WHERE_ROWID_EQ );
! 107431: testcase( pLevel->plan.wsFlags & WHERE_ROWID_RANGE );
! 107432: if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
! 107433: memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
! 107434: nQPlan += 2;
! 107435: }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
! 107436: n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
! 107437: if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
! 107438: memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
! 107439: nQPlan += n;
! 107440: sqlite3_query_plan[nQPlan++] = ' ';
! 107441: }
! 107442: }else{
! 107443: memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
! 107444: nQPlan += 3;
! 107445: }
! 107446: }
! 107447: while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
! 107448: sqlite3_query_plan[--nQPlan] = 0;
! 107449: }
! 107450: sqlite3_query_plan[nQPlan] = 0;
! 107451: nQPlan = 0;
! 107452: #endif /* SQLITE_TEST // Testing and debugging use only */
! 107453:
! 107454: /* Record the continuation address in the WhereInfo structure. Then
! 107455: ** clean up and return.
! 107456: */
! 107457: return pWInfo;
! 107458:
! 107459: /* Jump here if malloc fails */
! 107460: whereBeginError:
! 107461: if( pWInfo ){
! 107462: pParse->nQueryLoop = pWInfo->savedNQueryLoop;
! 107463: whereInfoFree(db, pWInfo);
! 107464: }
! 107465: return 0;
! 107466: }
! 107467:
! 107468: /*
! 107469: ** Generate the end of the WHERE loop. See comments on
! 107470: ** sqlite3WhereBegin() for additional information.
! 107471: */
! 107472: SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
! 107473: Parse *pParse = pWInfo->pParse;
! 107474: Vdbe *v = pParse->pVdbe;
! 107475: int i;
! 107476: WhereLevel *pLevel;
! 107477: SrcList *pTabList = pWInfo->pTabList;
! 107478: sqlite3 *db = pParse->db;
! 107479:
! 107480: /* Generate loop termination code.
! 107481: */
! 107482: sqlite3ExprCacheClear(pParse);
! 107483: for(i=pWInfo->nLevel-1; i>=0; i--){
! 107484: pLevel = &pWInfo->a[i];
! 107485: sqlite3VdbeResolveLabel(v, pLevel->addrCont);
! 107486: if( pLevel->op!=OP_Noop ){
! 107487: sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
! 107488: sqlite3VdbeChangeP5(v, pLevel->p5);
! 107489: }
! 107490: if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
! 107491: struct InLoop *pIn;
! 107492: int j;
! 107493: sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
! 107494: for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
! 107495: sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
! 107496: sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->addrInTop);
! 107497: sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
! 107498: }
! 107499: sqlite3DbFree(db, pLevel->u.in.aInLoop);
! 107500: }
! 107501: sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
! 107502: if( pLevel->iLeftJoin ){
! 107503: int addr;
! 107504: addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
! 107505: assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
! 107506: || (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 );
! 107507: if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){
! 107508: sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
! 107509: }
! 107510: if( pLevel->iIdxCur>=0 ){
! 107511: sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
! 107512: }
! 107513: if( pLevel->op==OP_Return ){
! 107514: sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
! 107515: }else{
! 107516: sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
! 107517: }
! 107518: sqlite3VdbeJumpHere(v, addr);
! 107519: }
! 107520: }
! 107521:
! 107522: /* The "break" point is here, just past the end of the outer loop.
! 107523: ** Set it.
! 107524: */
! 107525: sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
! 107526:
! 107527: /* Close all of the cursors that were opened by sqlite3WhereBegin.
! 107528: */
! 107529: assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
! 107530: for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
! 107531: struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
! 107532: Table *pTab = pTabItem->pTab;
! 107533: assert( pTab!=0 );
! 107534: if( (pTab->tabFlags & TF_Ephemeral)==0
! 107535: && pTab->pSelect==0
! 107536: && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
! 107537: ){
! 107538: int ws = pLevel->plan.wsFlags;
! 107539: if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
! 107540: sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
! 107541: }
! 107542: if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
! 107543: sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
! 107544: }
! 107545: }
! 107546:
! 107547: /* If this scan uses an index, make code substitutions to read data
! 107548: ** from the index in preference to the table. Sometimes, this means
! 107549: ** the table need never be read from. This is a performance boost,
! 107550: ** as the vdbe level waits until the table is read before actually
! 107551: ** seeking the table cursor to the record corresponding to the current
! 107552: ** position in the index.
! 107553: **
! 107554: ** Calls to the code generator in between sqlite3WhereBegin and
! 107555: ** sqlite3WhereEnd will have created code that references the table
! 107556: ** directly. This loop scans all that code looking for opcodes
! 107557: ** that reference the table and converts them into opcodes that
! 107558: ** reference the index.
! 107559: */
! 107560: if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 && !db->mallocFailed){
! 107561: int k, j, last;
! 107562: VdbeOp *pOp;
! 107563: Index *pIdx = pLevel->plan.u.pIdx;
! 107564:
! 107565: assert( pIdx!=0 );
! 107566: pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
! 107567: last = sqlite3VdbeCurrentAddr(v);
! 107568: for(k=pWInfo->iTop; k<last; k++, pOp++){
! 107569: if( pOp->p1!=pLevel->iTabCur ) continue;
! 107570: if( pOp->opcode==OP_Column ){
! 107571: for(j=0; j<pIdx->nColumn; j++){
! 107572: if( pOp->p2==pIdx->aiColumn[j] ){
! 107573: pOp->p2 = j;
! 107574: pOp->p1 = pLevel->iIdxCur;
! 107575: break;
! 107576: }
! 107577: }
! 107578: assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
! 107579: || j<pIdx->nColumn );
! 107580: }else if( pOp->opcode==OP_Rowid ){
! 107581: pOp->p1 = pLevel->iIdxCur;
! 107582: pOp->opcode = OP_IdxRowid;
! 107583: }
! 107584: }
! 107585: }
! 107586: }
! 107587:
! 107588: /* Final cleanup
! 107589: */
! 107590: pParse->nQueryLoop = pWInfo->savedNQueryLoop;
! 107591: whereInfoFree(db, pWInfo);
! 107592: return;
! 107593: }
! 107594:
! 107595: /************** End of where.c ***********************************************/
! 107596: /************** Begin file parse.c *******************************************/
! 107597: /* Driver template for the LEMON parser generator.
! 107598: ** The author disclaims copyright to this source code.
! 107599: **
! 107600: ** This version of "lempar.c" is modified, slightly, for use by SQLite.
! 107601: ** The only modifications are the addition of a couple of NEVER()
! 107602: ** macros to disable tests that are needed in the case of a general
! 107603: ** LALR(1) grammar but which are always false in the
! 107604: ** specific grammar used by SQLite.
! 107605: */
! 107606: /* First off, code is included that follows the "include" declaration
! 107607: ** in the input grammar file. */
! 107608: /* #include <stdio.h> */
! 107609:
! 107610:
! 107611: /*
! 107612: ** Disable all error recovery processing in the parser push-down
! 107613: ** automaton.
! 107614: */
! 107615: #define YYNOERRORRECOVERY 1
! 107616:
! 107617: /*
! 107618: ** Make yytestcase() the same as testcase()
! 107619: */
! 107620: #define yytestcase(X) testcase(X)
! 107621:
! 107622: /*
! 107623: ** An instance of this structure holds information about the
! 107624: ** LIMIT clause of a SELECT statement.
! 107625: */
! 107626: struct LimitVal {
! 107627: Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */
! 107628: Expr *pOffset; /* The OFFSET expression. NULL if there is none */
! 107629: };
! 107630:
! 107631: /*
! 107632: ** An instance of this structure is used to store the LIKE,
! 107633: ** GLOB, NOT LIKE, and NOT GLOB operators.
! 107634: */
! 107635: struct LikeOp {
! 107636: Token eOperator; /* "like" or "glob" or "regexp" */
! 107637: int not; /* True if the NOT keyword is present */
! 107638: };
! 107639:
! 107640: /*
! 107641: ** An instance of the following structure describes the event of a
! 107642: ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
! 107643: ** TK_DELETE, or TK_INSTEAD. If the event is of the form
! 107644: **
! 107645: ** UPDATE ON (a,b,c)
! 107646: **
! 107647: ** Then the "b" IdList records the list "a,b,c".
! 107648: */
! 107649: struct TrigEvent { int a; IdList * b; };
! 107650:
! 107651: /*
! 107652: ** An instance of this structure holds the ATTACH key and the key type.
! 107653: */
! 107654: struct AttachKey { int type; Token key; };
! 107655:
! 107656:
! 107657: /* This is a utility routine used to set the ExprSpan.zStart and
! 107658: ** ExprSpan.zEnd values of pOut so that the span covers the complete
! 107659: ** range of text beginning with pStart and going to the end of pEnd.
! 107660: */
! 107661: static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
! 107662: pOut->zStart = pStart->z;
! 107663: pOut->zEnd = &pEnd->z[pEnd->n];
! 107664: }
! 107665:
! 107666: /* Construct a new Expr object from a single identifier. Use the
! 107667: ** new Expr to populate pOut. Set the span of pOut to be the identifier
! 107668: ** that created the expression.
! 107669: */
! 107670: static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
! 107671: pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
! 107672: pOut->zStart = pValue->z;
! 107673: pOut->zEnd = &pValue->z[pValue->n];
! 107674: }
! 107675:
! 107676: /* This routine constructs a binary expression node out of two ExprSpan
! 107677: ** objects and uses the result to populate a new ExprSpan object.
! 107678: */
! 107679: static void spanBinaryExpr(
! 107680: ExprSpan *pOut, /* Write the result here */
! 107681: Parse *pParse, /* The parsing context. Errors accumulate here */
! 107682: int op, /* The binary operation */
! 107683: ExprSpan *pLeft, /* The left operand */
! 107684: ExprSpan *pRight /* The right operand */
! 107685: ){
! 107686: pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
! 107687: pOut->zStart = pLeft->zStart;
! 107688: pOut->zEnd = pRight->zEnd;
! 107689: }
! 107690:
! 107691: /* Construct an expression node for a unary postfix operator
! 107692: */
! 107693: static void spanUnaryPostfix(
! 107694: ExprSpan *pOut, /* Write the new expression node here */
! 107695: Parse *pParse, /* Parsing context to record errors */
! 107696: int op, /* The operator */
! 107697: ExprSpan *pOperand, /* The operand */
! 107698: Token *pPostOp /* The operand token for setting the span */
! 107699: ){
! 107700: pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
! 107701: pOut->zStart = pOperand->zStart;
! 107702: pOut->zEnd = &pPostOp->z[pPostOp->n];
! 107703: }
! 107704:
! 107705: /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
! 107706: ** unary TK_ISNULL or TK_NOTNULL expression. */
! 107707: static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
! 107708: sqlite3 *db = pParse->db;
! 107709: if( db->mallocFailed==0 && pY->op==TK_NULL ){
! 107710: pA->op = (u8)op;
! 107711: sqlite3ExprDelete(db, pA->pRight);
! 107712: pA->pRight = 0;
! 107713: }
! 107714: }
! 107715:
! 107716: /* Construct an expression node for a unary prefix operator
! 107717: */
! 107718: static void spanUnaryPrefix(
! 107719: ExprSpan *pOut, /* Write the new expression node here */
! 107720: Parse *pParse, /* Parsing context to record errors */
! 107721: int op, /* The operator */
! 107722: ExprSpan *pOperand, /* The operand */
! 107723: Token *pPreOp /* The operand token for setting the span */
! 107724: ){
! 107725: pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
! 107726: pOut->zStart = pPreOp->z;
! 107727: pOut->zEnd = pOperand->zEnd;
! 107728: }
! 107729: /* Next is all token values, in a form suitable for use by makeheaders.
! 107730: ** This section will be null unless lemon is run with the -m switch.
! 107731: */
! 107732: /*
! 107733: ** These constants (all generated automatically by the parser generator)
! 107734: ** specify the various kinds of tokens (terminals) that the parser
! 107735: ** understands.
! 107736: **
! 107737: ** Each symbol here is a terminal symbol in the grammar.
! 107738: */
! 107739: /* Make sure the INTERFACE macro is defined.
! 107740: */
! 107741: #ifndef INTERFACE
! 107742: # define INTERFACE 1
! 107743: #endif
! 107744: /* The next thing included is series of defines which control
! 107745: ** various aspects of the generated parser.
! 107746: ** YYCODETYPE is the data type used for storing terminal
! 107747: ** and nonterminal numbers. "unsigned char" is
! 107748: ** used if there are fewer than 250 terminals
! 107749: ** and nonterminals. "int" is used otherwise.
! 107750: ** YYNOCODE is a number of type YYCODETYPE which corresponds
! 107751: ** to no legal terminal or nonterminal number. This
! 107752: ** number is used to fill in empty slots of the hash
! 107753: ** table.
! 107754: ** YYFALLBACK If defined, this indicates that one or more tokens
! 107755: ** have fall-back values which should be used if the
! 107756: ** original value of the token will not parse.
! 107757: ** YYACTIONTYPE is the data type used for storing terminal
! 107758: ** and nonterminal numbers. "unsigned char" is
! 107759: ** used if there are fewer than 250 rules and
! 107760: ** states combined. "int" is used otherwise.
! 107761: ** sqlite3ParserTOKENTYPE is the data type used for minor tokens given
! 107762: ** directly to the parser from the tokenizer.
! 107763: ** YYMINORTYPE is the data type used for all minor tokens.
! 107764: ** This is typically a union of many types, one of
! 107765: ** which is sqlite3ParserTOKENTYPE. The entry in the union
! 107766: ** for base tokens is called "yy0".
! 107767: ** YYSTACKDEPTH is the maximum depth of the parser's stack. If
! 107768: ** zero the stack is dynamically sized using realloc()
! 107769: ** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument
! 107770: ** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
! 107771: ** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
! 107772: ** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
! 107773: ** YYNSTATE the combined number of states.
! 107774: ** YYNRULE the number of rules in the grammar
! 107775: ** YYERRORSYMBOL is the code number of the error symbol. If not
! 107776: ** defined, then do no error processing.
! 107777: */
! 107778: #define YYCODETYPE unsigned char
! 107779: #define YYNOCODE 253
! 107780: #define YYACTIONTYPE unsigned short int
! 107781: #define YYWILDCARD 67
! 107782: #define sqlite3ParserTOKENTYPE Token
! 107783: typedef union {
! 107784: int yyinit;
! 107785: sqlite3ParserTOKENTYPE yy0;
! 107786: int yy4;
! 107787: struct TrigEvent yy90;
! 107788: ExprSpan yy118;
! 107789: TriggerStep* yy203;
! 107790: u8 yy210;
! 107791: struct {int value; int mask;} yy215;
! 107792: SrcList* yy259;
! 107793: struct LimitVal yy292;
! 107794: Expr* yy314;
! 107795: ExprList* yy322;
! 107796: struct LikeOp yy342;
! 107797: IdList* yy384;
! 107798: Select* yy387;
! 107799: } YYMINORTYPE;
! 107800: #ifndef YYSTACKDEPTH
! 107801: #define YYSTACKDEPTH 100
! 107802: #endif
! 107803: #define sqlite3ParserARG_SDECL Parse *pParse;
! 107804: #define sqlite3ParserARG_PDECL ,Parse *pParse
! 107805: #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
! 107806: #define sqlite3ParserARG_STORE yypParser->pParse = pParse
! 107807: #define YYNSTATE 630
! 107808: #define YYNRULE 329
! 107809: #define YYFALLBACK 1
! 107810: #define YY_NO_ACTION (YYNSTATE+YYNRULE+2)
! 107811: #define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1)
! 107812: #define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
! 107813:
! 107814: /* The yyzerominor constant is used to initialize instances of
! 107815: ** YYMINORTYPE objects to zero. */
! 107816: static const YYMINORTYPE yyzerominor = { 0 };
! 107817:
! 107818: /* Define the yytestcase() macro to be a no-op if is not already defined
! 107819: ** otherwise.
! 107820: **
! 107821: ** Applications can choose to define yytestcase() in the %include section
! 107822: ** to a macro that can assist in verifying code coverage. For production
! 107823: ** code the yytestcase() macro should be turned off. But it is useful
! 107824: ** for testing.
! 107825: */
! 107826: #ifndef yytestcase
! 107827: # define yytestcase(X)
! 107828: #endif
! 107829:
! 107830:
! 107831: /* Next are the tables used to determine what action to take based on the
! 107832: ** current state and lookahead token. These tables are used to implement
! 107833: ** functions that take a state number and lookahead value and return an
! 107834: ** action integer.
! 107835: **
! 107836: ** Suppose the action integer is N. Then the action is determined as
! 107837: ** follows
! 107838: **
! 107839: ** 0 <= N < YYNSTATE Shift N. That is, push the lookahead
! 107840: ** token onto the stack and goto state N.
! 107841: **
! 107842: ** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE.
! 107843: **
! 107844: ** N == YYNSTATE+YYNRULE A syntax error has occurred.
! 107845: **
! 107846: ** N == YYNSTATE+YYNRULE+1 The parser accepts its input.
! 107847: **
! 107848: ** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused
! 107849: ** slots in the yy_action[] table.
! 107850: **
! 107851: ** The action table is constructed as a single large table named yy_action[].
! 107852: ** Given state S and lookahead X, the action is computed as
! 107853: **
! 107854: ** yy_action[ yy_shift_ofst[S] + X ]
! 107855: **
! 107856: ** If the index value yy_shift_ofst[S]+X is out of range or if the value
! 107857: ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
! 107858: ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
! 107859: ** and that yy_default[S] should be used instead.
! 107860: **
! 107861: ** The formula above is for computing the action when the lookahead is
! 107862: ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
! 107863: ** a reduce action) then the yy_reduce_ofst[] array is used in place of
! 107864: ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
! 107865: ** YY_SHIFT_USE_DFLT.
! 107866: **
! 107867: ** The following are the tables generated in this section:
! 107868: **
! 107869: ** yy_action[] A single table containing all actions.
! 107870: ** yy_lookahead[] A table containing the lookahead for each entry in
! 107871: ** yy_action. Used to detect hash collisions.
! 107872: ** yy_shift_ofst[] For each state, the offset into yy_action for
! 107873: ** shifting terminals.
! 107874: ** yy_reduce_ofst[] For each state, the offset into yy_action for
! 107875: ** shifting non-terminals after a reduce.
! 107876: ** yy_default[] Default action for each state.
! 107877: */
! 107878: #define YY_ACTTAB_COUNT (1557)
! 107879: static const YYACTIONTYPE yy_action[] = {
! 107880: /* 0 */ 313, 960, 186, 419, 2, 172, 627, 597, 55, 55,
! 107881: /* 10 */ 55, 55, 48, 53, 53, 53, 53, 52, 52, 51,
! 107882: /* 20 */ 51, 51, 50, 238, 302, 283, 623, 622, 516, 515,
! 107883: /* 30 */ 590, 584, 55, 55, 55, 55, 282, 53, 53, 53,
! 107884: /* 40 */ 53, 52, 52, 51, 51, 51, 50, 238, 6, 56,
! 107885: /* 50 */ 57, 47, 582, 581, 583, 583, 54, 54, 55, 55,
! 107886: /* 60 */ 55, 55, 608, 53, 53, 53, 53, 52, 52, 51,
! 107887: /* 70 */ 51, 51, 50, 238, 313, 597, 409, 330, 579, 579,
! 107888: /* 80 */ 32, 53, 53, 53, 53, 52, 52, 51, 51, 51,
! 107889: /* 90 */ 50, 238, 330, 217, 620, 619, 166, 411, 624, 382,
! 107890: /* 100 */ 379, 378, 7, 491, 590, 584, 200, 199, 198, 58,
! 107891: /* 110 */ 377, 300, 414, 621, 481, 66, 623, 622, 621, 580,
! 107892: /* 120 */ 254, 601, 94, 56, 57, 47, 582, 581, 583, 583,
! 107893: /* 130 */ 54, 54, 55, 55, 55, 55, 671, 53, 53, 53,
! 107894: /* 140 */ 53, 52, 52, 51, 51, 51, 50, 238, 313, 532,
! 107895: /* 150 */ 226, 506, 507, 133, 177, 139, 284, 385, 279, 384,
! 107896: /* 160 */ 169, 197, 342, 398, 251, 226, 253, 275, 388, 167,
! 107897: /* 170 */ 139, 284, 385, 279, 384, 169, 570, 236, 590, 584,
! 107898: /* 180 */ 672, 240, 275, 157, 620, 619, 554, 437, 51, 51,
! 107899: /* 190 */ 51, 50, 238, 343, 439, 553, 438, 56, 57, 47,
! 107900: /* 200 */ 582, 581, 583, 583, 54, 54, 55, 55, 55, 55,
! 107901: /* 210 */ 465, 53, 53, 53, 53, 52, 52, 51, 51, 51,
! 107902: /* 220 */ 50, 238, 313, 390, 52, 52, 51, 51, 51, 50,
! 107903: /* 230 */ 238, 391, 166, 491, 566, 382, 379, 378, 409, 440,
! 107904: /* 240 */ 579, 579, 252, 440, 607, 66, 377, 513, 621, 49,
! 107905: /* 250 */ 46, 147, 590, 584, 621, 16, 466, 189, 621, 441,
! 107906: /* 260 */ 442, 673, 526, 441, 340, 577, 595, 64, 194, 482,
! 107907: /* 270 */ 434, 56, 57, 47, 582, 581, 583, 583, 54, 54,
! 107908: /* 280 */ 55, 55, 55, 55, 30, 53, 53, 53, 53, 52,
! 107909: /* 290 */ 52, 51, 51, 51, 50, 238, 313, 593, 593, 593,
! 107910: /* 300 */ 387, 578, 606, 493, 259, 351, 258, 411, 1, 623,
! 107911: /* 310 */ 622, 496, 623, 622, 65, 240, 623, 622, 597, 443,
! 107912: /* 320 */ 237, 239, 414, 341, 237, 602, 590, 584, 18, 603,
! 107913: /* 330 */ 166, 601, 87, 382, 379, 378, 67, 623, 622, 38,
! 107914: /* 340 */ 623, 622, 176, 270, 377, 56, 57, 47, 582, 581,
! 107915: /* 350 */ 583, 583, 54, 54, 55, 55, 55, 55, 175, 53,
! 107916: /* 360 */ 53, 53, 53, 52, 52, 51, 51, 51, 50, 238,
! 107917: /* 370 */ 313, 396, 233, 411, 531, 565, 317, 620, 619, 44,
! 107918: /* 380 */ 620, 619, 240, 206, 620, 619, 597, 266, 414, 268,
! 107919: /* 390 */ 409, 597, 579, 579, 352, 184, 505, 601, 73, 533,
! 107920: /* 400 */ 590, 584, 466, 548, 190, 620, 619, 576, 620, 619,
! 107921: /* 410 */ 547, 383, 551, 35, 332, 575, 574, 600, 504, 56,
! 107922: /* 420 */ 57, 47, 582, 581, 583, 583, 54, 54, 55, 55,
! 107923: /* 430 */ 55, 55, 567, 53, 53, 53, 53, 52, 52, 51,
! 107924: /* 440 */ 51, 51, 50, 238, 313, 411, 561, 561, 528, 364,
! 107925: /* 450 */ 259, 351, 258, 183, 361, 549, 524, 374, 411, 597,
! 107926: /* 460 */ 414, 240, 560, 560, 409, 604, 579, 579, 328, 601,
! 107927: /* 470 */ 93, 623, 622, 414, 590, 584, 237, 564, 559, 559,
! 107928: /* 480 */ 520, 402, 601, 87, 409, 210, 579, 579, 168, 421,
! 107929: /* 490 */ 950, 519, 950, 56, 57, 47, 582, 581, 583, 583,
! 107930: /* 500 */ 54, 54, 55, 55, 55, 55, 192, 53, 53, 53,
! 107931: /* 510 */ 53, 52, 52, 51, 51, 51, 50, 238, 313, 600,
! 107932: /* 520 */ 293, 563, 511, 234, 357, 146, 475, 475, 367, 411,
! 107933: /* 530 */ 562, 411, 358, 542, 425, 171, 411, 215, 144, 620,
! 107934: /* 540 */ 619, 544, 318, 353, 414, 203, 414, 275, 590, 584,
! 107935: /* 550 */ 549, 414, 174, 601, 94, 601, 79, 558, 471, 61,
! 107936: /* 560 */ 601, 79, 421, 949, 350, 949, 34, 56, 57, 47,
! 107937: /* 570 */ 582, 581, 583, 583, 54, 54, 55, 55, 55, 55,
! 107938: /* 580 */ 535, 53, 53, 53, 53, 52, 52, 51, 51, 51,
! 107939: /* 590 */ 50, 238, 313, 307, 424, 394, 272, 49, 46, 147,
! 107940: /* 600 */ 349, 322, 4, 411, 491, 312, 321, 425, 568, 492,
! 107941: /* 610 */ 216, 264, 407, 575, 574, 429, 66, 549, 414, 621,
! 107942: /* 620 */ 540, 602, 590, 584, 13, 603, 621, 601, 72, 12,
! 107943: /* 630 */ 618, 617, 616, 202, 210, 621, 546, 469, 422, 319,
! 107944: /* 640 */ 148, 56, 57, 47, 582, 581, 583, 583, 54, 54,
! 107945: /* 650 */ 55, 55, 55, 55, 338, 53, 53, 53, 53, 52,
! 107946: /* 660 */ 52, 51, 51, 51, 50, 238, 313, 600, 600, 411,
! 107947: /* 670 */ 39, 21, 37, 170, 237, 875, 411, 572, 572, 201,
! 107948: /* 680 */ 144, 473, 538, 331, 414, 474, 143, 146, 630, 628,
! 107949: /* 690 */ 334, 414, 353, 601, 68, 168, 590, 584, 132, 365,
! 107950: /* 700 */ 601, 96, 307, 423, 530, 336, 49, 46, 147, 568,
! 107951: /* 710 */ 406, 216, 549, 360, 529, 56, 57, 47, 582, 581,
! 107952: /* 720 */ 583, 583, 54, 54, 55, 55, 55, 55, 411, 53,
! 107953: /* 730 */ 53, 53, 53, 52, 52, 51, 51, 51, 50, 238,
! 107954: /* 740 */ 313, 411, 605, 414, 484, 510, 172, 422, 597, 318,
! 107955: /* 750 */ 496, 485, 601, 99, 411, 142, 414, 411, 231, 411,
! 107956: /* 760 */ 540, 411, 359, 629, 2, 601, 97, 426, 308, 414,
! 107957: /* 770 */ 590, 584, 414, 20, 414, 621, 414, 621, 601, 106,
! 107958: /* 780 */ 503, 601, 105, 601, 108, 601, 109, 204, 28, 56,
! 107959: /* 790 */ 57, 47, 582, 581, 583, 583, 54, 54, 55, 55,
! 107960: /* 800 */ 55, 55, 411, 53, 53, 53, 53, 52, 52, 51,
! 107961: /* 810 */ 51, 51, 50, 238, 313, 411, 597, 414, 411, 276,
! 107962: /* 820 */ 214, 600, 411, 366, 213, 381, 601, 134, 274, 500,
! 107963: /* 830 */ 414, 167, 130, 414, 621, 411, 354, 414, 376, 601,
! 107964: /* 840 */ 135, 129, 601, 100, 590, 584, 601, 104, 522, 521,
! 107965: /* 850 */ 414, 621, 224, 273, 600, 167, 327, 282, 600, 601,
! 107966: /* 860 */ 103, 468, 521, 56, 57, 47, 582, 581, 583, 583,
! 107967: /* 870 */ 54, 54, 55, 55, 55, 55, 411, 53, 53, 53,
! 107968: /* 880 */ 53, 52, 52, 51, 51, 51, 50, 238, 313, 411,
! 107969: /* 890 */ 27, 414, 411, 375, 276, 167, 359, 544, 50, 238,
! 107970: /* 900 */ 601, 95, 128, 223, 414, 411, 165, 414, 411, 621,
! 107971: /* 910 */ 411, 621, 612, 601, 102, 372, 601, 76, 590, 584,
! 107972: /* 920 */ 414, 570, 236, 414, 470, 414, 167, 621, 188, 601,
! 107973: /* 930 */ 98, 225, 601, 138, 601, 137, 232, 56, 45, 47,
! 107974: /* 940 */ 582, 581, 583, 583, 54, 54, 55, 55, 55, 55,
! 107975: /* 950 */ 411, 53, 53, 53, 53, 52, 52, 51, 51, 51,
! 107976: /* 960 */ 50, 238, 313, 276, 276, 414, 411, 276, 544, 459,
! 107977: /* 970 */ 359, 171, 209, 479, 601, 136, 628, 334, 621, 621,
! 107978: /* 980 */ 125, 414, 621, 368, 411, 621, 257, 540, 589, 588,
! 107979: /* 990 */ 601, 75, 590, 584, 458, 446, 23, 23, 124, 414,
! 107980: /* 1000 */ 326, 325, 621, 427, 324, 309, 600, 288, 601, 92,
! 107981: /* 1010 */ 586, 585, 57, 47, 582, 581, 583, 583, 54, 54,
! 107982: /* 1020 */ 55, 55, 55, 55, 411, 53, 53, 53, 53, 52,
! 107983: /* 1030 */ 52, 51, 51, 51, 50, 238, 313, 587, 411, 414,
! 107984: /* 1040 */ 411, 207, 611, 476, 171, 472, 160, 123, 601, 91,
! 107985: /* 1050 */ 323, 261, 15, 414, 464, 414, 411, 621, 411, 354,
! 107986: /* 1060 */ 222, 411, 601, 74, 601, 90, 590, 584, 159, 264,
! 107987: /* 1070 */ 158, 414, 461, 414, 621, 600, 414, 121, 120, 25,
! 107988: /* 1080 */ 601, 89, 601, 101, 621, 601, 88, 47, 582, 581,
! 107989: /* 1090 */ 583, 583, 54, 54, 55, 55, 55, 55, 544, 53,
! 107990: /* 1100 */ 53, 53, 53, 52, 52, 51, 51, 51, 50, 238,
! 107991: /* 1110 */ 43, 405, 263, 3, 610, 264, 140, 415, 622, 24,
! 107992: /* 1120 */ 410, 11, 456, 594, 118, 155, 219, 452, 408, 621,
! 107993: /* 1130 */ 621, 621, 156, 43, 405, 621, 3, 286, 621, 113,
! 107994: /* 1140 */ 415, 622, 111, 445, 411, 400, 557, 403, 545, 10,
! 107995: /* 1150 */ 411, 408, 264, 110, 205, 436, 541, 566, 453, 414,
! 107996: /* 1160 */ 621, 621, 63, 621, 435, 414, 411, 621, 601, 94,
! 107997: /* 1170 */ 403, 621, 411, 337, 601, 86, 150, 40, 41, 534,
! 107998: /* 1180 */ 566, 414, 242, 264, 42, 413, 412, 414, 600, 595,
! 107999: /* 1190 */ 601, 85, 191, 333, 107, 451, 601, 84, 621, 539,
! 108000: /* 1200 */ 40, 41, 420, 230, 411, 149, 316, 42, 413, 412,
! 108001: /* 1210 */ 398, 127, 595, 315, 621, 399, 278, 625, 181, 414,
! 108002: /* 1220 */ 593, 593, 593, 592, 591, 14, 450, 411, 601, 71,
! 108003: /* 1230 */ 240, 621, 43, 405, 264, 3, 615, 180, 264, 415,
! 108004: /* 1240 */ 622, 614, 414, 593, 593, 593, 592, 591, 14, 621,
! 108005: /* 1250 */ 408, 601, 70, 621, 417, 33, 405, 613, 3, 411,
! 108006: /* 1260 */ 264, 411, 415, 622, 418, 626, 178, 509, 8, 403,
! 108007: /* 1270 */ 241, 416, 126, 408, 414, 621, 414, 449, 208, 566,
! 108008: /* 1280 */ 240, 221, 621, 601, 83, 601, 82, 599, 297, 277,
! 108009: /* 1290 */ 296, 30, 403, 31, 395, 264, 295, 397, 489, 40,
! 108010: /* 1300 */ 41, 411, 566, 220, 621, 294, 42, 413, 412, 271,
! 108011: /* 1310 */ 621, 595, 600, 621, 59, 60, 414, 269, 267, 623,
! 108012: /* 1320 */ 622, 36, 40, 41, 621, 601, 81, 598, 235, 42,
! 108013: /* 1330 */ 413, 412, 621, 621, 595, 265, 344, 411, 248, 556,
! 108014: /* 1340 */ 173, 185, 593, 593, 593, 592, 591, 14, 218, 29,
! 108015: /* 1350 */ 621, 543, 414, 305, 304, 303, 179, 301, 411, 566,
! 108016: /* 1360 */ 454, 601, 80, 289, 335, 593, 593, 593, 592, 591,
! 108017: /* 1370 */ 14, 411, 287, 414, 151, 392, 246, 260, 411, 196,
! 108018: /* 1380 */ 195, 523, 601, 69, 411, 245, 414, 526, 537, 285,
! 108019: /* 1390 */ 389, 595, 621, 414, 536, 601, 17, 362, 153, 414,
! 108020: /* 1400 */ 466, 463, 601, 78, 154, 414, 462, 152, 601, 77,
! 108021: /* 1410 */ 355, 255, 621, 455, 601, 9, 621, 386, 444, 517,
! 108022: /* 1420 */ 247, 621, 593, 593, 593, 621, 621, 244, 621, 243,
! 108023: /* 1430 */ 430, 518, 292, 621, 329, 621, 145, 393, 280, 513,
! 108024: /* 1440 */ 291, 131, 621, 514, 621, 621, 311, 621, 259, 346,
! 108025: /* 1450 */ 249, 621, 621, 229, 314, 621, 228, 512, 227, 240,
! 108026: /* 1460 */ 494, 488, 310, 164, 487, 486, 373, 480, 163, 262,
! 108027: /* 1470 */ 369, 371, 162, 26, 212, 478, 477, 161, 141, 363,
! 108028: /* 1480 */ 467, 122, 339, 187, 119, 348, 347, 117, 116, 115,
! 108029: /* 1490 */ 114, 112, 182, 457, 320, 22, 433, 432, 448, 19,
! 108030: /* 1500 */ 609, 431, 428, 62, 193, 596, 573, 298, 555, 552,
! 108031: /* 1510 */ 571, 404, 290, 380, 498, 510, 495, 306, 281, 499,
! 108032: /* 1520 */ 250, 5, 497, 460, 345, 447, 569, 550, 238, 299,
! 108033: /* 1530 */ 527, 525, 508, 961, 502, 501, 961, 401, 961, 211,
! 108034: /* 1540 */ 490, 356, 256, 961, 483, 961, 961, 961, 961, 961,
! 108035: /* 1550 */ 961, 961, 961, 961, 961, 961, 370,
! 108036: };
! 108037: static const YYCODETYPE yy_lookahead[] = {
! 108038: /* 0 */ 19, 142, 143, 144, 145, 24, 1, 26, 77, 78,
! 108039: /* 10 */ 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
! 108040: /* 20 */ 89, 90, 91, 92, 15, 98, 26, 27, 7, 8,
! 108041: /* 30 */ 49, 50, 77, 78, 79, 80, 109, 82, 83, 84,
! 108042: /* 40 */ 85, 86, 87, 88, 89, 90, 91, 92, 22, 68,
! 108043: /* 50 */ 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
! 108044: /* 60 */ 79, 80, 23, 82, 83, 84, 85, 86, 87, 88,
! 108045: /* 70 */ 89, 90, 91, 92, 19, 94, 112, 19, 114, 115,
! 108046: /* 80 */ 25, 82, 83, 84, 85, 86, 87, 88, 89, 90,
! 108047: /* 90 */ 91, 92, 19, 22, 94, 95, 96, 150, 150, 99,
! 108048: /* 100 */ 100, 101, 76, 150, 49, 50, 105, 106, 107, 54,
! 108049: /* 110 */ 110, 158, 165, 165, 161, 162, 26, 27, 165, 113,
! 108050: /* 120 */ 16, 174, 175, 68, 69, 70, 71, 72, 73, 74,
! 108051: /* 130 */ 75, 76, 77, 78, 79, 80, 118, 82, 83, 84,
! 108052: /* 140 */ 85, 86, 87, 88, 89, 90, 91, 92, 19, 23,
! 108053: /* 150 */ 92, 97, 98, 24, 96, 97, 98, 99, 100, 101,
! 108054: /* 160 */ 102, 25, 97, 216, 60, 92, 62, 109, 221, 25,
! 108055: /* 170 */ 97, 98, 99, 100, 101, 102, 86, 87, 49, 50,
! 108056: /* 180 */ 118, 116, 109, 25, 94, 95, 32, 97, 88, 89,
! 108057: /* 190 */ 90, 91, 92, 128, 104, 41, 106, 68, 69, 70,
! 108058: /* 200 */ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
! 108059: /* 210 */ 11, 82, 83, 84, 85, 86, 87, 88, 89, 90,
! 108060: /* 220 */ 91, 92, 19, 19, 86, 87, 88, 89, 90, 91,
! 108061: /* 230 */ 92, 27, 96, 150, 66, 99, 100, 101, 112, 150,
! 108062: /* 240 */ 114, 115, 138, 150, 161, 162, 110, 103, 165, 222,
! 108063: /* 250 */ 223, 224, 49, 50, 165, 22, 57, 24, 165, 170,
! 108064: /* 260 */ 171, 118, 94, 170, 171, 23, 98, 25, 185, 186,
! 108065: /* 270 */ 243, 68, 69, 70, 71, 72, 73, 74, 75, 76,
! 108066: /* 280 */ 77, 78, 79, 80, 126, 82, 83, 84, 85, 86,
! 108067: /* 290 */ 87, 88, 89, 90, 91, 92, 19, 129, 130, 131,
! 108068: /* 300 */ 88, 23, 172, 173, 105, 106, 107, 150, 22, 26,
! 108069: /* 310 */ 27, 181, 26, 27, 22, 116, 26, 27, 26, 230,
! 108070: /* 320 */ 231, 197, 165, 230, 231, 113, 49, 50, 204, 117,
! 108071: /* 330 */ 96, 174, 175, 99, 100, 101, 22, 26, 27, 136,
! 108072: /* 340 */ 26, 27, 118, 16, 110, 68, 69, 70, 71, 72,
! 108073: /* 350 */ 73, 74, 75, 76, 77, 78, 79, 80, 118, 82,
! 108074: /* 360 */ 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
! 108075: /* 370 */ 19, 214, 215, 150, 23, 23, 155, 94, 95, 22,
! 108076: /* 380 */ 94, 95, 116, 160, 94, 95, 94, 60, 165, 62,
! 108077: /* 390 */ 112, 26, 114, 115, 128, 23, 36, 174, 175, 88,
! 108078: /* 400 */ 49, 50, 57, 120, 22, 94, 95, 23, 94, 95,
! 108079: /* 410 */ 120, 51, 25, 136, 169, 170, 171, 194, 58, 68,
! 108080: /* 420 */ 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
! 108081: /* 430 */ 79, 80, 23, 82, 83, 84, 85, 86, 87, 88,
! 108082: /* 440 */ 89, 90, 91, 92, 19, 150, 12, 12, 23, 228,
! 108083: /* 450 */ 105, 106, 107, 23, 233, 25, 165, 19, 150, 94,
! 108084: /* 460 */ 165, 116, 28, 28, 112, 174, 114, 115, 108, 174,
! 108085: /* 470 */ 175, 26, 27, 165, 49, 50, 231, 11, 44, 44,
! 108086: /* 480 */ 46, 46, 174, 175, 112, 160, 114, 115, 50, 22,
! 108087: /* 490 */ 23, 57, 25, 68, 69, 70, 71, 72, 73, 74,
! 108088: /* 500 */ 75, 76, 77, 78, 79, 80, 119, 82, 83, 84,
! 108089: /* 510 */ 85, 86, 87, 88, 89, 90, 91, 92, 19, 194,
! 108090: /* 520 */ 225, 23, 23, 215, 19, 95, 105, 106, 107, 150,
! 108091: /* 530 */ 23, 150, 27, 23, 67, 25, 150, 206, 207, 94,
! 108092: /* 540 */ 95, 166, 104, 218, 165, 22, 165, 109, 49, 50,
! 108093: /* 550 */ 120, 165, 25, 174, 175, 174, 175, 23, 21, 234,
! 108094: /* 560 */ 174, 175, 22, 23, 239, 25, 25, 68, 69, 70,
! 108095: /* 570 */ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
! 108096: /* 580 */ 205, 82, 83, 84, 85, 86, 87, 88, 89, 90,
! 108097: /* 590 */ 91, 92, 19, 22, 23, 216, 23, 222, 223, 224,
! 108098: /* 600 */ 63, 220, 35, 150, 150, 163, 220, 67, 166, 167,
! 108099: /* 610 */ 168, 150, 169, 170, 171, 161, 162, 25, 165, 165,
! 108100: /* 620 */ 150, 113, 49, 50, 25, 117, 165, 174, 175, 35,
! 108101: /* 630 */ 7, 8, 9, 160, 160, 165, 120, 100, 67, 247,
! 108102: /* 640 */ 248, 68, 69, 70, 71, 72, 73, 74, 75, 76,
! 108103: /* 650 */ 77, 78, 79, 80, 193, 82, 83, 84, 85, 86,
! 108104: /* 660 */ 87, 88, 89, 90, 91, 92, 19, 194, 194, 150,
! 108105: /* 670 */ 135, 24, 137, 35, 231, 138, 150, 129, 130, 206,
! 108106: /* 680 */ 207, 30, 27, 213, 165, 34, 118, 95, 0, 1,
! 108107: /* 690 */ 2, 165, 218, 174, 175, 50, 49, 50, 22, 48,
! 108108: /* 700 */ 174, 175, 22, 23, 23, 244, 222, 223, 224, 166,
! 108109: /* 710 */ 167, 168, 120, 239, 23, 68, 69, 70, 71, 72,
! 108110: /* 720 */ 73, 74, 75, 76, 77, 78, 79, 80, 150, 82,
! 108111: /* 730 */ 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
! 108112: /* 740 */ 19, 150, 173, 165, 181, 182, 24, 67, 26, 104,
! 108113: /* 750 */ 181, 188, 174, 175, 150, 39, 165, 150, 52, 150,
! 108114: /* 760 */ 150, 150, 150, 144, 145, 174, 175, 249, 250, 165,
! 108115: /* 770 */ 49, 50, 165, 52, 165, 165, 165, 165, 174, 175,
! 108116: /* 780 */ 29, 174, 175, 174, 175, 174, 175, 160, 22, 68,
! 108117: /* 790 */ 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
! 108118: /* 800 */ 79, 80, 150, 82, 83, 84, 85, 86, 87, 88,
! 108119: /* 810 */ 89, 90, 91, 92, 19, 150, 94, 165, 150, 150,
! 108120: /* 820 */ 160, 194, 150, 213, 160, 52, 174, 175, 23, 23,
! 108121: /* 830 */ 165, 25, 22, 165, 165, 150, 150, 165, 52, 174,
! 108122: /* 840 */ 175, 22, 174, 175, 49, 50, 174, 175, 190, 191,
! 108123: /* 850 */ 165, 165, 240, 23, 194, 25, 187, 109, 194, 174,
! 108124: /* 860 */ 175, 190, 191, 68, 69, 70, 71, 72, 73, 74,
! 108125: /* 870 */ 75, 76, 77, 78, 79, 80, 150, 82, 83, 84,
! 108126: /* 880 */ 85, 86, 87, 88, 89, 90, 91, 92, 19, 150,
! 108127: /* 890 */ 22, 165, 150, 23, 150, 25, 150, 166, 91, 92,
! 108128: /* 900 */ 174, 175, 22, 217, 165, 150, 102, 165, 150, 165,
! 108129: /* 910 */ 150, 165, 150, 174, 175, 19, 174, 175, 49, 50,
! 108130: /* 920 */ 165, 86, 87, 165, 23, 165, 25, 165, 24, 174,
! 108131: /* 930 */ 175, 187, 174, 175, 174, 175, 205, 68, 69, 70,
! 108132: /* 940 */ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
! 108133: /* 950 */ 150, 82, 83, 84, 85, 86, 87, 88, 89, 90,
! 108134: /* 960 */ 91, 92, 19, 150, 150, 165, 150, 150, 166, 23,
! 108135: /* 970 */ 150, 25, 160, 20, 174, 175, 1, 2, 165, 165,
! 108136: /* 980 */ 104, 165, 165, 43, 150, 165, 240, 150, 49, 50,
! 108137: /* 990 */ 174, 175, 49, 50, 23, 23, 25, 25, 53, 165,
! 108138: /* 1000 */ 187, 187, 165, 23, 187, 25, 194, 205, 174, 175,
! 108139: /* 1010 */ 71, 72, 69, 70, 71, 72, 73, 74, 75, 76,
! 108140: /* 1020 */ 77, 78, 79, 80, 150, 82, 83, 84, 85, 86,
! 108141: /* 1030 */ 87, 88, 89, 90, 91, 92, 19, 98, 150, 165,
! 108142: /* 1040 */ 150, 160, 150, 59, 25, 53, 104, 22, 174, 175,
! 108143: /* 1050 */ 213, 138, 5, 165, 1, 165, 150, 165, 150, 150,
! 108144: /* 1060 */ 240, 150, 174, 175, 174, 175, 49, 50, 118, 150,
! 108145: /* 1070 */ 35, 165, 27, 165, 165, 194, 165, 108, 127, 76,
! 108146: /* 1080 */ 174, 175, 174, 175, 165, 174, 175, 70, 71, 72,
! 108147: /* 1090 */ 73, 74, 75, 76, 77, 78, 79, 80, 166, 82,
! 108148: /* 1100 */ 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
! 108149: /* 1110 */ 19, 20, 193, 22, 150, 150, 150, 26, 27, 76,
! 108150: /* 1120 */ 150, 22, 1, 150, 119, 121, 217, 20, 37, 165,
! 108151: /* 1130 */ 165, 165, 16, 19, 20, 165, 22, 205, 165, 119,
! 108152: /* 1140 */ 26, 27, 108, 128, 150, 150, 150, 56, 150, 22,
! 108153: /* 1150 */ 150, 37, 150, 127, 160, 23, 150, 66, 193, 165,
! 108154: /* 1160 */ 165, 165, 16, 165, 23, 165, 150, 165, 174, 175,
! 108155: /* 1170 */ 56, 165, 150, 65, 174, 175, 15, 86, 87, 88,
! 108156: /* 1180 */ 66, 165, 140, 150, 93, 94, 95, 165, 194, 98,
! 108157: /* 1190 */ 174, 175, 22, 3, 164, 193, 174, 175, 165, 150,
! 108158: /* 1200 */ 86, 87, 4, 180, 150, 248, 251, 93, 94, 95,
! 108159: /* 1210 */ 216, 180, 98, 251, 165, 221, 150, 149, 6, 165,
! 108160: /* 1220 */ 129, 130, 131, 132, 133, 134, 193, 150, 174, 175,
! 108161: /* 1230 */ 116, 165, 19, 20, 150, 22, 149, 151, 150, 26,
! 108162: /* 1240 */ 27, 149, 165, 129, 130, 131, 132, 133, 134, 165,
! 108163: /* 1250 */ 37, 174, 175, 165, 149, 19, 20, 13, 22, 150,
! 108164: /* 1260 */ 150, 150, 26, 27, 146, 147, 151, 150, 25, 56,
! 108165: /* 1270 */ 152, 159, 154, 37, 165, 165, 165, 193, 160, 66,
! 108166: /* 1280 */ 116, 193, 165, 174, 175, 174, 175, 194, 199, 150,
! 108167: /* 1290 */ 200, 126, 56, 124, 123, 150, 201, 122, 150, 86,
! 108168: /* 1300 */ 87, 150, 66, 193, 165, 202, 93, 94, 95, 150,
! 108169: /* 1310 */ 165, 98, 194, 165, 125, 22, 165, 150, 150, 26,
! 108170: /* 1320 */ 27, 135, 86, 87, 165, 174, 175, 203, 226, 93,
! 108171: /* 1330 */ 94, 95, 165, 165, 98, 150, 218, 150, 193, 157,
! 108172: /* 1340 */ 118, 157, 129, 130, 131, 132, 133, 134, 5, 104,
! 108173: /* 1350 */ 165, 211, 165, 10, 11, 12, 13, 14, 150, 66,
! 108174: /* 1360 */ 17, 174, 175, 210, 246, 129, 130, 131, 132, 133,
! 108175: /* 1370 */ 134, 150, 210, 165, 31, 121, 33, 150, 150, 86,
! 108176: /* 1380 */ 87, 176, 174, 175, 150, 42, 165, 94, 211, 210,
! 108177: /* 1390 */ 150, 98, 165, 165, 211, 174, 175, 150, 55, 165,
! 108178: /* 1400 */ 57, 150, 174, 175, 61, 165, 150, 64, 174, 175,
! 108179: /* 1410 */ 150, 150, 165, 150, 174, 175, 165, 104, 150, 184,
! 108180: /* 1420 */ 150, 165, 129, 130, 131, 165, 165, 150, 165, 150,
! 108181: /* 1430 */ 150, 176, 150, 165, 47, 165, 150, 150, 176, 103,
! 108182: /* 1440 */ 150, 22, 165, 178, 165, 165, 179, 165, 105, 106,
! 108183: /* 1450 */ 107, 165, 165, 229, 111, 165, 92, 176, 229, 116,
! 108184: /* 1460 */ 184, 176, 179, 156, 176, 176, 18, 157, 156, 237,
! 108185: /* 1470 */ 45, 157, 156, 135, 157, 157, 238, 156, 68, 157,
! 108186: /* 1480 */ 189, 189, 139, 219, 22, 157, 18, 192, 192, 192,
! 108187: /* 1490 */ 192, 189, 219, 199, 157, 242, 40, 157, 199, 242,
! 108188: /* 1500 */ 153, 157, 38, 245, 196, 166, 232, 198, 177, 177,
! 108189: /* 1510 */ 232, 227, 209, 178, 166, 182, 166, 148, 177, 177,
! 108190: /* 1520 */ 209, 196, 177, 199, 209, 199, 166, 208, 92, 195,
! 108191: /* 1530 */ 174, 174, 183, 252, 183, 183, 252, 191, 252, 235,
! 108192: /* 1540 */ 186, 241, 241, 252, 186, 252, 252, 252, 252, 252,
! 108193: /* 1550 */ 252, 252, 252, 252, 252, 252, 236,
! 108194: };
! 108195: #define YY_SHIFT_USE_DFLT (-74)
! 108196: #define YY_SHIFT_COUNT (418)
! 108197: #define YY_SHIFT_MIN (-73)
! 108198: #define YY_SHIFT_MAX (1468)
! 108199: static const short yy_shift_ofst[] = {
! 108200: /* 0 */ 975, 1114, 1343, 1114, 1213, 1213, 90, 90, 0, -19,
! 108201: /* 10 */ 1213, 1213, 1213, 1213, 1213, 345, 445, 721, 1091, 1213,
! 108202: /* 20 */ 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
! 108203: /* 30 */ 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
! 108204: /* 40 */ 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1236, 1213, 1213,
! 108205: /* 50 */ 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
! 108206: /* 60 */ 1213, 199, 445, 445, 835, 835, 365, 1164, 55, 647,
! 108207: /* 70 */ 573, 499, 425, 351, 277, 203, 129, 795, 795, 795,
! 108208: /* 80 */ 795, 795, 795, 795, 795, 795, 795, 795, 795, 795,
! 108209: /* 90 */ 795, 795, 795, 795, 795, 869, 795, 943, 1017, 1017,
! 108210: /* 100 */ -69, -45, -45, -45, -45, -45, -1, 58, 138, 100,
! 108211: /* 110 */ 445, 445, 445, 445, 445, 445, 445, 445, 445, 445,
! 108212: /* 120 */ 445, 445, 445, 445, 445, 445, 537, 438, 445, 445,
! 108213: /* 130 */ 445, 445, 445, 365, 807, 1436, -74, -74, -74, 1293,
! 108214: /* 140 */ 73, 434, 434, 311, 314, 290, 283, 286, 540, 467,
! 108215: /* 150 */ 445, 445, 445, 445, 445, 445, 445, 445, 445, 445,
! 108216: /* 160 */ 445, 445, 445, 445, 445, 445, 445, 445, 445, 445,
! 108217: /* 170 */ 445, 445, 445, 445, 445, 445, 445, 445, 445, 445,
! 108218: /* 180 */ 445, 445, 65, 722, 722, 722, 688, 266, 1164, 1164,
! 108219: /* 190 */ 1164, -74, -74, -74, 136, 168, 168, 234, 360, 360,
! 108220: /* 200 */ 360, 430, 372, 435, 352, 278, 126, -36, -36, -36,
! 108221: /* 210 */ -36, 421, 651, -36, -36, 592, 292, 212, 623, 158,
! 108222: /* 220 */ 204, 204, 505, 158, 505, 144, 365, 154, 365, 154,
! 108223: /* 230 */ 645, 154, 204, 154, 154, 535, 548, 548, 365, 387,
! 108224: /* 240 */ 508, 233, 1464, 1222, 1222, 1456, 1456, 1222, 1462, 1410,
! 108225: /* 250 */ 1165, 1468, 1468, 1468, 1468, 1222, 1165, 1462, 1410, 1410,
! 108226: /* 260 */ 1222, 1448, 1338, 1425, 1222, 1222, 1448, 1222, 1448, 1222,
! 108227: /* 270 */ 1448, 1419, 1313, 1313, 1313, 1387, 1364, 1364, 1419, 1313,
! 108228: /* 280 */ 1336, 1313, 1387, 1313, 1313, 1254, 1245, 1254, 1245, 1254,
! 108229: /* 290 */ 1245, 1222, 1222, 1186, 1189, 1175, 1169, 1171, 1165, 1164,
! 108230: /* 300 */ 1243, 1244, 1244, 1212, 1212, 1212, 1212, -74, -74, -74,
! 108231: /* 310 */ -74, -74, -74, 939, 104, 680, 571, 327, 1, 980,
! 108232: /* 320 */ 26, 972, 971, 946, 901, 870, 830, 806, 54, 21,
! 108233: /* 330 */ -73, 510, 242, 1198, 1190, 1170, 1042, 1161, 1108, 1146,
! 108234: /* 340 */ 1141, 1132, 1015, 1127, 1026, 1034, 1020, 1107, 1004, 1116,
! 108235: /* 350 */ 1121, 1005, 1099, 951, 1043, 1003, 969, 1045, 1035, 950,
! 108236: /* 360 */ 1053, 1047, 1025, 942, 913, 992, 1019, 945, 984, 940,
! 108237: /* 370 */ 876, 904, 953, 896, 748, 804, 880, 786, 868, 819,
! 108238: /* 380 */ 805, 810, 773, 751, 766, 706, 716, 691, 681, 568,
! 108239: /* 390 */ 655, 638, 676, 516, 541, 594, 599, 567, 541, 534,
! 108240: /* 400 */ 507, 527, 498, 523, 466, 382, 409, 384, 357, 6,
! 108241: /* 410 */ 240, 224, 143, 62, 18, 71, 39, 9, 5,
! 108242: };
! 108243: #define YY_REDUCE_USE_DFLT (-142)
! 108244: #define YY_REDUCE_COUNT (312)
! 108245: #define YY_REDUCE_MIN (-141)
! 108246: #define YY_REDUCE_MAX (1369)
! 108247: static const short yy_reduce_ofst[] = {
! 108248: /* 0 */ -141, 994, 1118, 223, 157, -53, 93, 89, 83, 375,
! 108249: /* 10 */ 386, 381, 379, 308, 295, 325, -47, 27, 1240, 1234,
! 108250: /* 20 */ 1228, 1221, 1208, 1187, 1151, 1111, 1109, 1077, 1054, 1022,
! 108251: /* 30 */ 1016, 1000, 911, 908, 906, 890, 888, 874, 834, 816,
! 108252: /* 40 */ 800, 760, 758, 755, 742, 739, 726, 685, 672, 668,
! 108253: /* 50 */ 665, 652, 611, 609, 607, 604, 591, 578, 526, 519,
! 108254: /* 60 */ 453, 474, 454, 461, 443, 245, 442, 473, 484, 484,
! 108255: /* 70 */ 484, 484, 484, 484, 484, 484, 484, 484, 484, 484,
! 108256: /* 80 */ 484, 484, 484, 484, 484, 484, 484, 484, 484, 484,
! 108257: /* 90 */ 484, 484, 484, 484, 484, 484, 484, 484, 484, 484,
! 108258: /* 100 */ 484, 484, 484, 484, 484, 484, 484, 130, 484, 484,
! 108259: /* 110 */ 1145, 909, 1110, 1088, 1084, 1033, 1002, 965, 820, 837,
! 108260: /* 120 */ 746, 686, 612, 817, 610, 919, 221, 563, 814, 813,
! 108261: /* 130 */ 744, 669, 470, 543, 484, 484, 484, 484, 484, 291,
! 108262: /* 140 */ 569, 671, 658, 970, 1290, 1287, 1286, 1282, 518, 518,
! 108263: /* 150 */ 1280, 1279, 1277, 1270, 1268, 1263, 1261, 1260, 1256, 1251,
! 108264: /* 160 */ 1247, 1227, 1185, 1168, 1167, 1159, 1148, 1139, 1117, 1066,
! 108265: /* 170 */ 1049, 1006, 998, 996, 995, 973, 970, 966, 964, 892,
! 108266: /* 180 */ 762, -52, 881, 932, 802, 731, 619, 812, 664, 660,
! 108267: /* 190 */ 627, 392, 331, 124, 1358, 1357, 1356, 1354, 1352, 1351,
! 108268: /* 200 */ 1349, 1319, 1334, 1346, 1334, 1334, 1334, 1334, 1334, 1334,
! 108269: /* 210 */ 1334, 1320, 1304, 1334, 1334, 1319, 1360, 1325, 1369, 1326,
! 108270: /* 220 */ 1315, 1311, 1301, 1324, 1300, 1335, 1350, 1345, 1348, 1342,
! 108271: /* 230 */ 1333, 1341, 1303, 1332, 1331, 1284, 1278, 1274, 1339, 1309,
! 108272: /* 240 */ 1308, 1347, 1258, 1344, 1340, 1257, 1253, 1337, 1273, 1302,
! 108273: /* 250 */ 1299, 1298, 1297, 1296, 1295, 1328, 1294, 1264, 1292, 1291,
! 108274: /* 260 */ 1322, 1321, 1238, 1232, 1318, 1317, 1316, 1314, 1312, 1310,
! 108275: /* 270 */ 1307, 1283, 1289, 1288, 1285, 1276, 1229, 1224, 1267, 1281,
! 108276: /* 280 */ 1265, 1262, 1235, 1255, 1205, 1183, 1179, 1177, 1162, 1140,
! 108277: /* 290 */ 1153, 1184, 1182, 1102, 1124, 1103, 1095, 1090, 1089, 1093,
! 108278: /* 300 */ 1112, 1115, 1086, 1105, 1092, 1087, 1068, 962, 955, 957,
! 108279: /* 310 */ 1031, 1023, 1030,
! 108280: };
! 108281: static const YYACTIONTYPE yy_default[] = {
! 108282: /* 0 */ 635, 870, 959, 959, 959, 870, 899, 899, 959, 759,
! 108283: /* 10 */ 959, 959, 959, 959, 868, 959, 959, 933, 959, 959,
! 108284: /* 20 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
! 108285: /* 30 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
! 108286: /* 40 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
! 108287: /* 50 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
! 108288: /* 60 */ 959, 959, 959, 959, 899, 899, 674, 763, 794, 959,
! 108289: /* 70 */ 959, 959, 959, 959, 959, 959, 959, 932, 934, 809,
! 108290: /* 80 */ 808, 802, 801, 912, 774, 799, 792, 785, 796, 871,
! 108291: /* 90 */ 864, 865, 863, 867, 872, 959, 795, 831, 848, 830,
! 108292: /* 100 */ 842, 847, 854, 846, 843, 833, 832, 666, 834, 835,
! 108293: /* 110 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
! 108294: /* 120 */ 959, 959, 959, 959, 959, 959, 661, 728, 959, 959,
! 108295: /* 130 */ 959, 959, 959, 959, 836, 837, 851, 850, 849, 959,
! 108296: /* 140 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
! 108297: /* 150 */ 959, 939, 937, 959, 883, 959, 959, 959, 959, 959,
! 108298: /* 160 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
! 108299: /* 170 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
! 108300: /* 180 */ 959, 641, 959, 759, 759, 759, 635, 959, 959, 959,
! 108301: /* 190 */ 959, 951, 763, 753, 719, 959, 959, 959, 959, 959,
! 108302: /* 200 */ 959, 959, 959, 959, 959, 959, 959, 804, 742, 922,
! 108303: /* 210 */ 924, 959, 905, 740, 663, 761, 676, 751, 643, 798,
! 108304: /* 220 */ 776, 776, 917, 798, 917, 700, 959, 788, 959, 788,
! 108305: /* 230 */ 697, 788, 776, 788, 788, 866, 959, 959, 959, 760,
! 108306: /* 240 */ 751, 959, 944, 767, 767, 936, 936, 767, 810, 732,
! 108307: /* 250 */ 798, 739, 739, 739, 739, 767, 798, 810, 732, 732,
! 108308: /* 260 */ 767, 658, 911, 909, 767, 767, 658, 767, 658, 767,
! 108309: /* 270 */ 658, 876, 730, 730, 730, 715, 880, 880, 876, 730,
! 108310: /* 280 */ 700, 730, 715, 730, 730, 780, 775, 780, 775, 780,
! 108311: /* 290 */ 775, 767, 767, 959, 793, 781, 791, 789, 798, 959,
! 108312: /* 300 */ 718, 651, 651, 640, 640, 640, 640, 956, 956, 951,
! 108313: /* 310 */ 702, 702, 684, 959, 959, 959, 959, 959, 959, 959,
! 108314: /* 320 */ 885, 959, 959, 959, 959, 959, 959, 959, 959, 959,
! 108315: /* 330 */ 959, 959, 959, 959, 636, 946, 959, 959, 943, 959,
! 108316: /* 340 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
! 108317: /* 350 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 915,
! 108318: /* 360 */ 959, 959, 959, 959, 959, 959, 908, 907, 959, 959,
! 108319: /* 370 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
! 108320: /* 380 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
! 108321: /* 390 */ 959, 959, 959, 959, 790, 959, 782, 959, 869, 959,
! 108322: /* 400 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 745,
! 108323: /* 410 */ 819, 959, 818, 822, 817, 668, 959, 649, 959, 632,
! 108324: /* 420 */ 637, 955, 958, 957, 954, 953, 952, 947, 945, 942,
! 108325: /* 430 */ 941, 940, 938, 935, 931, 889, 887, 894, 893, 892,
! 108326: /* 440 */ 891, 890, 888, 886, 884, 805, 803, 800, 797, 930,
! 108327: /* 450 */ 882, 741, 738, 737, 657, 948, 914, 923, 921, 811,
! 108328: /* 460 */ 920, 919, 918, 916, 913, 900, 807, 806, 733, 874,
! 108329: /* 470 */ 873, 660, 904, 903, 902, 906, 910, 901, 769, 659,
! 108330: /* 480 */ 656, 665, 722, 721, 729, 727, 726, 725, 724, 723,
! 108331: /* 490 */ 720, 667, 675, 686, 714, 699, 698, 879, 881, 878,
! 108332: /* 500 */ 877, 707, 706, 712, 711, 710, 709, 708, 705, 704,
! 108333: /* 510 */ 703, 696, 695, 701, 694, 717, 716, 713, 693, 736,
! 108334: /* 520 */ 735, 734, 731, 692, 691, 690, 822, 689, 688, 828,
! 108335: /* 530 */ 827, 815, 858, 756, 755, 754, 766, 765, 778, 777,
! 108336: /* 540 */ 813, 812, 779, 764, 758, 757, 773, 772, 771, 770,
! 108337: /* 550 */ 762, 752, 784, 787, 786, 783, 860, 768, 857, 929,
! 108338: /* 560 */ 928, 927, 926, 925, 862, 861, 829, 826, 679, 680,
! 108339: /* 570 */ 898, 896, 897, 895, 682, 681, 678, 677, 859, 747,
! 108340: /* 580 */ 746, 855, 852, 844, 840, 856, 853, 845, 841, 839,
! 108341: /* 590 */ 838, 824, 823, 821, 820, 816, 825, 670, 748, 744,
! 108342: /* 600 */ 743, 814, 750, 749, 687, 685, 683, 664, 662, 655,
! 108343: /* 610 */ 653, 652, 654, 650, 648, 647, 646, 645, 644, 673,
! 108344: /* 620 */ 672, 671, 669, 668, 642, 639, 638, 634, 633, 631,
! 108345: };
! 108346:
! 108347: /* The next table maps tokens into fallback tokens. If a construct
! 108348: ** like the following:
! 108349: **
! 108350: ** %fallback ID X Y Z.
! 108351: **
! 108352: ** appears in the grammar, then ID becomes a fallback token for X, Y,
! 108353: ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
! 108354: ** but it does not parse, the type of the token is changed to ID and
! 108355: ** the parse is retried before an error is thrown.
! 108356: */
! 108357: #ifdef YYFALLBACK
! 108358: static const YYCODETYPE yyFallback[] = {
! 108359: 0, /* $ => nothing */
! 108360: 0, /* SEMI => nothing */
! 108361: 26, /* EXPLAIN => ID */
! 108362: 26, /* QUERY => ID */
! 108363: 26, /* PLAN => ID */
! 108364: 26, /* BEGIN => ID */
! 108365: 0, /* TRANSACTION => nothing */
! 108366: 26, /* DEFERRED => ID */
! 108367: 26, /* IMMEDIATE => ID */
! 108368: 26, /* EXCLUSIVE => ID */
! 108369: 0, /* COMMIT => nothing */
! 108370: 26, /* END => ID */
! 108371: 26, /* ROLLBACK => ID */
! 108372: 26, /* SAVEPOINT => ID */
! 108373: 26, /* RELEASE => ID */
! 108374: 0, /* TO => nothing */
! 108375: 0, /* TABLE => nothing */
! 108376: 0, /* CREATE => nothing */
! 108377: 26, /* IF => ID */
! 108378: 0, /* NOT => nothing */
! 108379: 0, /* EXISTS => nothing */
! 108380: 26, /* TEMP => ID */
! 108381: 0, /* LP => nothing */
! 108382: 0, /* RP => nothing */
! 108383: 0, /* AS => nothing */
! 108384: 0, /* COMMA => nothing */
! 108385: 0, /* ID => nothing */
! 108386: 0, /* INDEXED => nothing */
! 108387: 26, /* ABORT => ID */
! 108388: 26, /* ACTION => ID */
! 108389: 26, /* AFTER => ID */
! 108390: 26, /* ANALYZE => ID */
! 108391: 26, /* ASC => ID */
! 108392: 26, /* ATTACH => ID */
! 108393: 26, /* BEFORE => ID */
! 108394: 26, /* BY => ID */
! 108395: 26, /* CASCADE => ID */
! 108396: 26, /* CAST => ID */
! 108397: 26, /* COLUMNKW => ID */
! 108398: 26, /* CONFLICT => ID */
! 108399: 26, /* DATABASE => ID */
! 108400: 26, /* DESC => ID */
! 108401: 26, /* DETACH => ID */
! 108402: 26, /* EACH => ID */
! 108403: 26, /* FAIL => ID */
! 108404: 26, /* FOR => ID */
! 108405: 26, /* IGNORE => ID */
! 108406: 26, /* INITIALLY => ID */
! 108407: 26, /* INSTEAD => ID */
! 108408: 26, /* LIKE_KW => ID */
! 108409: 26, /* MATCH => ID */
! 108410: 26, /* NO => ID */
! 108411: 26, /* KEY => ID */
! 108412: 26, /* OF => ID */
! 108413: 26, /* OFFSET => ID */
! 108414: 26, /* PRAGMA => ID */
! 108415: 26, /* RAISE => ID */
! 108416: 26, /* REPLACE => ID */
! 108417: 26, /* RESTRICT => ID */
! 108418: 26, /* ROW => ID */
! 108419: 26, /* TRIGGER => ID */
! 108420: 26, /* VACUUM => ID */
! 108421: 26, /* VIEW => ID */
! 108422: 26, /* VIRTUAL => ID */
! 108423: 26, /* REINDEX => ID */
! 108424: 26, /* RENAME => ID */
! 108425: 26, /* CTIME_KW => ID */
! 108426: };
! 108427: #endif /* YYFALLBACK */
! 108428:
! 108429: /* The following structure represents a single element of the
! 108430: ** parser's stack. Information stored includes:
! 108431: **
! 108432: ** + The state number for the parser at this level of the stack.
! 108433: **
! 108434: ** + The value of the token stored at this level of the stack.
! 108435: ** (In other words, the "major" token.)
! 108436: **
! 108437: ** + The semantic value stored at this level of the stack. This is
! 108438: ** the information used by the action routines in the grammar.
! 108439: ** It is sometimes called the "minor" token.
! 108440: */
! 108441: struct yyStackEntry {
! 108442: YYACTIONTYPE stateno; /* The state-number */
! 108443: YYCODETYPE major; /* The major token value. This is the code
! 108444: ** number for the token at this stack level */
! 108445: YYMINORTYPE minor; /* The user-supplied minor token value. This
! 108446: ** is the value of the token */
! 108447: };
! 108448: typedef struct yyStackEntry yyStackEntry;
! 108449:
! 108450: /* The state of the parser is completely contained in an instance of
! 108451: ** the following structure */
! 108452: struct yyParser {
! 108453: int yyidx; /* Index of top element in stack */
! 108454: #ifdef YYTRACKMAXSTACKDEPTH
! 108455: int yyidxMax; /* Maximum value of yyidx */
! 108456: #endif
! 108457: int yyerrcnt; /* Shifts left before out of the error */
! 108458: sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
! 108459: #if YYSTACKDEPTH<=0
! 108460: int yystksz; /* Current side of the stack */
! 108461: yyStackEntry *yystack; /* The parser's stack */
! 108462: #else
! 108463: yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
! 108464: #endif
! 108465: };
! 108466: typedef struct yyParser yyParser;
! 108467:
! 108468: #ifndef NDEBUG
! 108469: /* #include <stdio.h> */
! 108470: static FILE *yyTraceFILE = 0;
! 108471: static char *yyTracePrompt = 0;
! 108472: #endif /* NDEBUG */
! 108473:
! 108474: #ifndef NDEBUG
! 108475: /*
! 108476: ** Turn parser tracing on by giving a stream to which to write the trace
! 108477: ** and a prompt to preface each trace message. Tracing is turned off
! 108478: ** by making either argument NULL
! 108479: **
! 108480: ** Inputs:
! 108481: ** <ul>
! 108482: ** <li> A FILE* to which trace output should be written.
! 108483: ** If NULL, then tracing is turned off.
! 108484: ** <li> A prefix string written at the beginning of every
! 108485: ** line of trace output. If NULL, then tracing is
! 108486: ** turned off.
! 108487: ** </ul>
! 108488: **
! 108489: ** Outputs:
! 108490: ** None.
! 108491: */
! 108492: SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
! 108493: yyTraceFILE = TraceFILE;
! 108494: yyTracePrompt = zTracePrompt;
! 108495: if( yyTraceFILE==0 ) yyTracePrompt = 0;
! 108496: else if( yyTracePrompt==0 ) yyTraceFILE = 0;
! 108497: }
! 108498: #endif /* NDEBUG */
! 108499:
! 108500: #ifndef NDEBUG
! 108501: /* For tracing shifts, the names of all terminals and nonterminals
! 108502: ** are required. The following table supplies these names */
! 108503: static const char *const yyTokenName[] = {
! 108504: "$", "SEMI", "EXPLAIN", "QUERY",
! 108505: "PLAN", "BEGIN", "TRANSACTION", "DEFERRED",
! 108506: "IMMEDIATE", "EXCLUSIVE", "COMMIT", "END",
! 108507: "ROLLBACK", "SAVEPOINT", "RELEASE", "TO",
! 108508: "TABLE", "CREATE", "IF", "NOT",
! 108509: "EXISTS", "TEMP", "LP", "RP",
! 108510: "AS", "COMMA", "ID", "INDEXED",
! 108511: "ABORT", "ACTION", "AFTER", "ANALYZE",
! 108512: "ASC", "ATTACH", "BEFORE", "BY",
! 108513: "CASCADE", "CAST", "COLUMNKW", "CONFLICT",
! 108514: "DATABASE", "DESC", "DETACH", "EACH",
! 108515: "FAIL", "FOR", "IGNORE", "INITIALLY",
! 108516: "INSTEAD", "LIKE_KW", "MATCH", "NO",
! 108517: "KEY", "OF", "OFFSET", "PRAGMA",
! 108518: "RAISE", "REPLACE", "RESTRICT", "ROW",
! 108519: "TRIGGER", "VACUUM", "VIEW", "VIRTUAL",
! 108520: "REINDEX", "RENAME", "CTIME_KW", "ANY",
! 108521: "OR", "AND", "IS", "BETWEEN",
! 108522: "IN", "ISNULL", "NOTNULL", "NE",
! 108523: "EQ", "GT", "LE", "LT",
! 108524: "GE", "ESCAPE", "BITAND", "BITOR",
! 108525: "LSHIFT", "RSHIFT", "PLUS", "MINUS",
! 108526: "STAR", "SLASH", "REM", "CONCAT",
! 108527: "COLLATE", "BITNOT", "STRING", "JOIN_KW",
! 108528: "CONSTRAINT", "DEFAULT", "NULL", "PRIMARY",
! 108529: "UNIQUE", "CHECK", "REFERENCES", "AUTOINCR",
! 108530: "ON", "INSERT", "DELETE", "UPDATE",
! 108531: "SET", "DEFERRABLE", "FOREIGN", "DROP",
! 108532: "UNION", "ALL", "EXCEPT", "INTERSECT",
! 108533: "SELECT", "DISTINCT", "DOT", "FROM",
! 108534: "JOIN", "USING", "ORDER", "GROUP",
! 108535: "HAVING", "LIMIT", "WHERE", "INTO",
! 108536: "VALUES", "INTEGER", "FLOAT", "BLOB",
! 108537: "REGISTER", "VARIABLE", "CASE", "WHEN",
! 108538: "THEN", "ELSE", "INDEX", "ALTER",
! 108539: "ADD", "error", "input", "cmdlist",
! 108540: "ecmd", "explain", "cmdx", "cmd",
! 108541: "transtype", "trans_opt", "nm", "savepoint_opt",
! 108542: "create_table", "create_table_args", "createkw", "temp",
! 108543: "ifnotexists", "dbnm", "columnlist", "conslist_opt",
! 108544: "select", "column", "columnid", "type",
! 108545: "carglist", "id", "ids", "typetoken",
! 108546: "typename", "signed", "plus_num", "minus_num",
! 108547: "carg", "ccons", "term", "expr",
! 108548: "onconf", "sortorder", "autoinc", "idxlist_opt",
! 108549: "refargs", "defer_subclause", "refarg", "refact",
! 108550: "init_deferred_pred_opt", "conslist", "tcons", "idxlist",
! 108551: "defer_subclause_opt", "orconf", "resolvetype", "raisetype",
! 108552: "ifexists", "fullname", "oneselect", "multiselect_op",
! 108553: "distinct", "selcollist", "from", "where_opt",
! 108554: "groupby_opt", "having_opt", "orderby_opt", "limit_opt",
! 108555: "sclp", "as", "seltablist", "stl_prefix",
! 108556: "joinop", "indexed_opt", "on_opt", "using_opt",
! 108557: "joinop2", "inscollist", "sortlist", "sortitem",
! 108558: "nexprlist", "setlist", "insert_cmd", "inscollist_opt",
! 108559: "itemlist", "exprlist", "likeop", "between_op",
! 108560: "in_op", "case_operand", "case_exprlist", "case_else",
! 108561: "uniqueflag", "collate", "nmnum", "plus_opt",
! 108562: "number", "trigger_decl", "trigger_cmd_list", "trigger_time",
! 108563: "trigger_event", "foreach_clause", "when_clause", "trigger_cmd",
! 108564: "trnm", "tridxby", "database_kw_opt", "key_opt",
! 108565: "add_column_fullname", "kwcolumn_opt", "create_vtab", "vtabarglist",
! 108566: "vtabarg", "vtabargtoken", "lp", "anylist",
! 108567: };
! 108568: #endif /* NDEBUG */
! 108569:
! 108570: #ifndef NDEBUG
! 108571: /* For tracing reduce actions, the names of all rules are required.
! 108572: */
! 108573: static const char *const yyRuleName[] = {
! 108574: /* 0 */ "input ::= cmdlist",
! 108575: /* 1 */ "cmdlist ::= cmdlist ecmd",
! 108576: /* 2 */ "cmdlist ::= ecmd",
! 108577: /* 3 */ "ecmd ::= SEMI",
! 108578: /* 4 */ "ecmd ::= explain cmdx SEMI",
! 108579: /* 5 */ "explain ::=",
! 108580: /* 6 */ "explain ::= EXPLAIN",
! 108581: /* 7 */ "explain ::= EXPLAIN QUERY PLAN",
! 108582: /* 8 */ "cmdx ::= cmd",
! 108583: /* 9 */ "cmd ::= BEGIN transtype trans_opt",
! 108584: /* 10 */ "trans_opt ::=",
! 108585: /* 11 */ "trans_opt ::= TRANSACTION",
! 108586: /* 12 */ "trans_opt ::= TRANSACTION nm",
! 108587: /* 13 */ "transtype ::=",
! 108588: /* 14 */ "transtype ::= DEFERRED",
! 108589: /* 15 */ "transtype ::= IMMEDIATE",
! 108590: /* 16 */ "transtype ::= EXCLUSIVE",
! 108591: /* 17 */ "cmd ::= COMMIT trans_opt",
! 108592: /* 18 */ "cmd ::= END trans_opt",
! 108593: /* 19 */ "cmd ::= ROLLBACK trans_opt",
! 108594: /* 20 */ "savepoint_opt ::= SAVEPOINT",
! 108595: /* 21 */ "savepoint_opt ::=",
! 108596: /* 22 */ "cmd ::= SAVEPOINT nm",
! 108597: /* 23 */ "cmd ::= RELEASE savepoint_opt nm",
! 108598: /* 24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
! 108599: /* 25 */ "cmd ::= create_table create_table_args",
! 108600: /* 26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
! 108601: /* 27 */ "createkw ::= CREATE",
! 108602: /* 28 */ "ifnotexists ::=",
! 108603: /* 29 */ "ifnotexists ::= IF NOT EXISTS",
! 108604: /* 30 */ "temp ::= TEMP",
! 108605: /* 31 */ "temp ::=",
! 108606: /* 32 */ "create_table_args ::= LP columnlist conslist_opt RP",
! 108607: /* 33 */ "create_table_args ::= AS select",
! 108608: /* 34 */ "columnlist ::= columnlist COMMA column",
! 108609: /* 35 */ "columnlist ::= column",
! 108610: /* 36 */ "column ::= columnid type carglist",
! 108611: /* 37 */ "columnid ::= nm",
! 108612: /* 38 */ "id ::= ID",
! 108613: /* 39 */ "id ::= INDEXED",
! 108614: /* 40 */ "ids ::= ID|STRING",
! 108615: /* 41 */ "nm ::= id",
! 108616: /* 42 */ "nm ::= STRING",
! 108617: /* 43 */ "nm ::= JOIN_KW",
! 108618: /* 44 */ "type ::=",
! 108619: /* 45 */ "type ::= typetoken",
! 108620: /* 46 */ "typetoken ::= typename",
! 108621: /* 47 */ "typetoken ::= typename LP signed RP",
! 108622: /* 48 */ "typetoken ::= typename LP signed COMMA signed RP",
! 108623: /* 49 */ "typename ::= ids",
! 108624: /* 50 */ "typename ::= typename ids",
! 108625: /* 51 */ "signed ::= plus_num",
! 108626: /* 52 */ "signed ::= minus_num",
! 108627: /* 53 */ "carglist ::= carglist carg",
! 108628: /* 54 */ "carglist ::=",
! 108629: /* 55 */ "carg ::= CONSTRAINT nm ccons",
! 108630: /* 56 */ "carg ::= ccons",
! 108631: /* 57 */ "ccons ::= DEFAULT term",
! 108632: /* 58 */ "ccons ::= DEFAULT LP expr RP",
! 108633: /* 59 */ "ccons ::= DEFAULT PLUS term",
! 108634: /* 60 */ "ccons ::= DEFAULT MINUS term",
! 108635: /* 61 */ "ccons ::= DEFAULT id",
! 108636: /* 62 */ "ccons ::= NULL onconf",
! 108637: /* 63 */ "ccons ::= NOT NULL onconf",
! 108638: /* 64 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
! 108639: /* 65 */ "ccons ::= UNIQUE onconf",
! 108640: /* 66 */ "ccons ::= CHECK LP expr RP",
! 108641: /* 67 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
! 108642: /* 68 */ "ccons ::= defer_subclause",
! 108643: /* 69 */ "ccons ::= COLLATE ids",
! 108644: /* 70 */ "autoinc ::=",
! 108645: /* 71 */ "autoinc ::= AUTOINCR",
! 108646: /* 72 */ "refargs ::=",
! 108647: /* 73 */ "refargs ::= refargs refarg",
! 108648: /* 74 */ "refarg ::= MATCH nm",
! 108649: /* 75 */ "refarg ::= ON INSERT refact",
! 108650: /* 76 */ "refarg ::= ON DELETE refact",
! 108651: /* 77 */ "refarg ::= ON UPDATE refact",
! 108652: /* 78 */ "refact ::= SET NULL",
! 108653: /* 79 */ "refact ::= SET DEFAULT",
! 108654: /* 80 */ "refact ::= CASCADE",
! 108655: /* 81 */ "refact ::= RESTRICT",
! 108656: /* 82 */ "refact ::= NO ACTION",
! 108657: /* 83 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
! 108658: /* 84 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
! 108659: /* 85 */ "init_deferred_pred_opt ::=",
! 108660: /* 86 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
! 108661: /* 87 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
! 108662: /* 88 */ "conslist_opt ::=",
! 108663: /* 89 */ "conslist_opt ::= COMMA conslist",
! 108664: /* 90 */ "conslist ::= conslist COMMA tcons",
! 108665: /* 91 */ "conslist ::= conslist tcons",
! 108666: /* 92 */ "conslist ::= tcons",
! 108667: /* 93 */ "tcons ::= CONSTRAINT nm",
! 108668: /* 94 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
! 108669: /* 95 */ "tcons ::= UNIQUE LP idxlist RP onconf",
! 108670: /* 96 */ "tcons ::= CHECK LP expr RP onconf",
! 108671: /* 97 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
! 108672: /* 98 */ "defer_subclause_opt ::=",
! 108673: /* 99 */ "defer_subclause_opt ::= defer_subclause",
! 108674: /* 100 */ "onconf ::=",
! 108675: /* 101 */ "onconf ::= ON CONFLICT resolvetype",
! 108676: /* 102 */ "orconf ::=",
! 108677: /* 103 */ "orconf ::= OR resolvetype",
! 108678: /* 104 */ "resolvetype ::= raisetype",
! 108679: /* 105 */ "resolvetype ::= IGNORE",
! 108680: /* 106 */ "resolvetype ::= REPLACE",
! 108681: /* 107 */ "cmd ::= DROP TABLE ifexists fullname",
! 108682: /* 108 */ "ifexists ::= IF EXISTS",
! 108683: /* 109 */ "ifexists ::=",
! 108684: /* 110 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
! 108685: /* 111 */ "cmd ::= DROP VIEW ifexists fullname",
! 108686: /* 112 */ "cmd ::= select",
! 108687: /* 113 */ "select ::= oneselect",
! 108688: /* 114 */ "select ::= select multiselect_op oneselect",
! 108689: /* 115 */ "multiselect_op ::= UNION",
! 108690: /* 116 */ "multiselect_op ::= UNION ALL",
! 108691: /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
! 108692: /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
! 108693: /* 119 */ "distinct ::= DISTINCT",
! 108694: /* 120 */ "distinct ::= ALL",
! 108695: /* 121 */ "distinct ::=",
! 108696: /* 122 */ "sclp ::= selcollist COMMA",
! 108697: /* 123 */ "sclp ::=",
! 108698: /* 124 */ "selcollist ::= sclp expr as",
! 108699: /* 125 */ "selcollist ::= sclp STAR",
! 108700: /* 126 */ "selcollist ::= sclp nm DOT STAR",
! 108701: /* 127 */ "as ::= AS nm",
! 108702: /* 128 */ "as ::= ids",
! 108703: /* 129 */ "as ::=",
! 108704: /* 130 */ "from ::=",
! 108705: /* 131 */ "from ::= FROM seltablist",
! 108706: /* 132 */ "stl_prefix ::= seltablist joinop",
! 108707: /* 133 */ "stl_prefix ::=",
! 108708: /* 134 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
! 108709: /* 135 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
! 108710: /* 136 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
! 108711: /* 137 */ "dbnm ::=",
! 108712: /* 138 */ "dbnm ::= DOT nm",
! 108713: /* 139 */ "fullname ::= nm dbnm",
! 108714: /* 140 */ "joinop ::= COMMA|JOIN",
! 108715: /* 141 */ "joinop ::= JOIN_KW JOIN",
! 108716: /* 142 */ "joinop ::= JOIN_KW nm JOIN",
! 108717: /* 143 */ "joinop ::= JOIN_KW nm nm JOIN",
! 108718: /* 144 */ "on_opt ::= ON expr",
! 108719: /* 145 */ "on_opt ::=",
! 108720: /* 146 */ "indexed_opt ::=",
! 108721: /* 147 */ "indexed_opt ::= INDEXED BY nm",
! 108722: /* 148 */ "indexed_opt ::= NOT INDEXED",
! 108723: /* 149 */ "using_opt ::= USING LP inscollist RP",
! 108724: /* 150 */ "using_opt ::=",
! 108725: /* 151 */ "orderby_opt ::=",
! 108726: /* 152 */ "orderby_opt ::= ORDER BY sortlist",
! 108727: /* 153 */ "sortlist ::= sortlist COMMA sortitem sortorder",
! 108728: /* 154 */ "sortlist ::= sortitem sortorder",
! 108729: /* 155 */ "sortitem ::= expr",
! 108730: /* 156 */ "sortorder ::= ASC",
! 108731: /* 157 */ "sortorder ::= DESC",
! 108732: /* 158 */ "sortorder ::=",
! 108733: /* 159 */ "groupby_opt ::=",
! 108734: /* 160 */ "groupby_opt ::= GROUP BY nexprlist",
! 108735: /* 161 */ "having_opt ::=",
! 108736: /* 162 */ "having_opt ::= HAVING expr",
! 108737: /* 163 */ "limit_opt ::=",
! 108738: /* 164 */ "limit_opt ::= LIMIT expr",
! 108739: /* 165 */ "limit_opt ::= LIMIT expr OFFSET expr",
! 108740: /* 166 */ "limit_opt ::= LIMIT expr COMMA expr",
! 108741: /* 167 */ "cmd ::= DELETE FROM fullname indexed_opt where_opt",
! 108742: /* 168 */ "where_opt ::=",
! 108743: /* 169 */ "where_opt ::= WHERE expr",
! 108744: /* 170 */ "cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt",
! 108745: /* 171 */ "setlist ::= setlist COMMA nm EQ expr",
! 108746: /* 172 */ "setlist ::= nm EQ expr",
! 108747: /* 173 */ "cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP",
! 108748: /* 174 */ "cmd ::= insert_cmd INTO fullname inscollist_opt select",
! 108749: /* 175 */ "cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
! 108750: /* 176 */ "insert_cmd ::= INSERT orconf",
! 108751: /* 177 */ "insert_cmd ::= REPLACE",
! 108752: /* 178 */ "itemlist ::= itemlist COMMA expr",
! 108753: /* 179 */ "itemlist ::= expr",
! 108754: /* 180 */ "inscollist_opt ::=",
! 108755: /* 181 */ "inscollist_opt ::= LP inscollist RP",
! 108756: /* 182 */ "inscollist ::= inscollist COMMA nm",
! 108757: /* 183 */ "inscollist ::= nm",
! 108758: /* 184 */ "expr ::= term",
! 108759: /* 185 */ "expr ::= LP expr RP",
! 108760: /* 186 */ "term ::= NULL",
! 108761: /* 187 */ "expr ::= id",
! 108762: /* 188 */ "expr ::= JOIN_KW",
! 108763: /* 189 */ "expr ::= nm DOT nm",
! 108764: /* 190 */ "expr ::= nm DOT nm DOT nm",
! 108765: /* 191 */ "term ::= INTEGER|FLOAT|BLOB",
! 108766: /* 192 */ "term ::= STRING",
! 108767: /* 193 */ "expr ::= REGISTER",
! 108768: /* 194 */ "expr ::= VARIABLE",
! 108769: /* 195 */ "expr ::= expr COLLATE ids",
! 108770: /* 196 */ "expr ::= CAST LP expr AS typetoken RP",
! 108771: /* 197 */ "expr ::= ID LP distinct exprlist RP",
! 108772: /* 198 */ "expr ::= ID LP STAR RP",
! 108773: /* 199 */ "term ::= CTIME_KW",
! 108774: /* 200 */ "expr ::= expr AND expr",
! 108775: /* 201 */ "expr ::= expr OR expr",
! 108776: /* 202 */ "expr ::= expr LT|GT|GE|LE expr",
! 108777: /* 203 */ "expr ::= expr EQ|NE expr",
! 108778: /* 204 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
! 108779: /* 205 */ "expr ::= expr PLUS|MINUS expr",
! 108780: /* 206 */ "expr ::= expr STAR|SLASH|REM expr",
! 108781: /* 207 */ "expr ::= expr CONCAT expr",
! 108782: /* 208 */ "likeop ::= LIKE_KW",
! 108783: /* 209 */ "likeop ::= NOT LIKE_KW",
! 108784: /* 210 */ "likeop ::= MATCH",
! 108785: /* 211 */ "likeop ::= NOT MATCH",
! 108786: /* 212 */ "expr ::= expr likeop expr",
! 108787: /* 213 */ "expr ::= expr likeop expr ESCAPE expr",
! 108788: /* 214 */ "expr ::= expr ISNULL|NOTNULL",
! 108789: /* 215 */ "expr ::= expr NOT NULL",
! 108790: /* 216 */ "expr ::= expr IS expr",
! 108791: /* 217 */ "expr ::= expr IS NOT expr",
! 108792: /* 218 */ "expr ::= NOT expr",
! 108793: /* 219 */ "expr ::= BITNOT expr",
! 108794: /* 220 */ "expr ::= MINUS expr",
! 108795: /* 221 */ "expr ::= PLUS expr",
! 108796: /* 222 */ "between_op ::= BETWEEN",
! 108797: /* 223 */ "between_op ::= NOT BETWEEN",
! 108798: /* 224 */ "expr ::= expr between_op expr AND expr",
! 108799: /* 225 */ "in_op ::= IN",
! 108800: /* 226 */ "in_op ::= NOT IN",
! 108801: /* 227 */ "expr ::= expr in_op LP exprlist RP",
! 108802: /* 228 */ "expr ::= LP select RP",
! 108803: /* 229 */ "expr ::= expr in_op LP select RP",
! 108804: /* 230 */ "expr ::= expr in_op nm dbnm",
! 108805: /* 231 */ "expr ::= EXISTS LP select RP",
! 108806: /* 232 */ "expr ::= CASE case_operand case_exprlist case_else END",
! 108807: /* 233 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
! 108808: /* 234 */ "case_exprlist ::= WHEN expr THEN expr",
! 108809: /* 235 */ "case_else ::= ELSE expr",
! 108810: /* 236 */ "case_else ::=",
! 108811: /* 237 */ "case_operand ::= expr",
! 108812: /* 238 */ "case_operand ::=",
! 108813: /* 239 */ "exprlist ::= nexprlist",
! 108814: /* 240 */ "exprlist ::=",
! 108815: /* 241 */ "nexprlist ::= nexprlist COMMA expr",
! 108816: /* 242 */ "nexprlist ::= expr",
! 108817: /* 243 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP",
! 108818: /* 244 */ "uniqueflag ::= UNIQUE",
! 108819: /* 245 */ "uniqueflag ::=",
! 108820: /* 246 */ "idxlist_opt ::=",
! 108821: /* 247 */ "idxlist_opt ::= LP idxlist RP",
! 108822: /* 248 */ "idxlist ::= idxlist COMMA nm collate sortorder",
! 108823: /* 249 */ "idxlist ::= nm collate sortorder",
! 108824: /* 250 */ "collate ::=",
! 108825: /* 251 */ "collate ::= COLLATE ids",
! 108826: /* 252 */ "cmd ::= DROP INDEX ifexists fullname",
! 108827: /* 253 */ "cmd ::= VACUUM",
! 108828: /* 254 */ "cmd ::= VACUUM nm",
! 108829: /* 255 */ "cmd ::= PRAGMA nm dbnm",
! 108830: /* 256 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
! 108831: /* 257 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
! 108832: /* 258 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
! 108833: /* 259 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
! 108834: /* 260 */ "nmnum ::= plus_num",
! 108835: /* 261 */ "nmnum ::= nm",
! 108836: /* 262 */ "nmnum ::= ON",
! 108837: /* 263 */ "nmnum ::= DELETE",
! 108838: /* 264 */ "nmnum ::= DEFAULT",
! 108839: /* 265 */ "plus_num ::= plus_opt number",
! 108840: /* 266 */ "minus_num ::= MINUS number",
! 108841: /* 267 */ "number ::= INTEGER|FLOAT",
! 108842: /* 268 */ "plus_opt ::= PLUS",
! 108843: /* 269 */ "plus_opt ::=",
! 108844: /* 270 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
! 108845: /* 271 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
! 108846: /* 272 */ "trigger_time ::= BEFORE",
! 108847: /* 273 */ "trigger_time ::= AFTER",
! 108848: /* 274 */ "trigger_time ::= INSTEAD OF",
! 108849: /* 275 */ "trigger_time ::=",
! 108850: /* 276 */ "trigger_event ::= DELETE|INSERT",
! 108851: /* 277 */ "trigger_event ::= UPDATE",
! 108852: /* 278 */ "trigger_event ::= UPDATE OF inscollist",
! 108853: /* 279 */ "foreach_clause ::=",
! 108854: /* 280 */ "foreach_clause ::= FOR EACH ROW",
! 108855: /* 281 */ "when_clause ::=",
! 108856: /* 282 */ "when_clause ::= WHEN expr",
! 108857: /* 283 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
! 108858: /* 284 */ "trigger_cmd_list ::= trigger_cmd SEMI",
! 108859: /* 285 */ "trnm ::= nm",
! 108860: /* 286 */ "trnm ::= nm DOT nm",
! 108861: /* 287 */ "tridxby ::=",
! 108862: /* 288 */ "tridxby ::= INDEXED BY nm",
! 108863: /* 289 */ "tridxby ::= NOT INDEXED",
! 108864: /* 290 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
! 108865: /* 291 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP",
! 108866: /* 292 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
! 108867: /* 293 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
! 108868: /* 294 */ "trigger_cmd ::= select",
! 108869: /* 295 */ "expr ::= RAISE LP IGNORE RP",
! 108870: /* 296 */ "expr ::= RAISE LP raisetype COMMA nm RP",
! 108871: /* 297 */ "raisetype ::= ROLLBACK",
! 108872: /* 298 */ "raisetype ::= ABORT",
! 108873: /* 299 */ "raisetype ::= FAIL",
! 108874: /* 300 */ "cmd ::= DROP TRIGGER ifexists fullname",
! 108875: /* 301 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
! 108876: /* 302 */ "cmd ::= DETACH database_kw_opt expr",
! 108877: /* 303 */ "key_opt ::=",
! 108878: /* 304 */ "key_opt ::= KEY expr",
! 108879: /* 305 */ "database_kw_opt ::= DATABASE",
! 108880: /* 306 */ "database_kw_opt ::=",
! 108881: /* 307 */ "cmd ::= REINDEX",
! 108882: /* 308 */ "cmd ::= REINDEX nm dbnm",
! 108883: /* 309 */ "cmd ::= ANALYZE",
! 108884: /* 310 */ "cmd ::= ANALYZE nm dbnm",
! 108885: /* 311 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
! 108886: /* 312 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
! 108887: /* 313 */ "add_column_fullname ::= fullname",
! 108888: /* 314 */ "kwcolumn_opt ::=",
! 108889: /* 315 */ "kwcolumn_opt ::= COLUMNKW",
! 108890: /* 316 */ "cmd ::= create_vtab",
! 108891: /* 317 */ "cmd ::= create_vtab LP vtabarglist RP",
! 108892: /* 318 */ "create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm",
! 108893: /* 319 */ "vtabarglist ::= vtabarg",
! 108894: /* 320 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
! 108895: /* 321 */ "vtabarg ::=",
! 108896: /* 322 */ "vtabarg ::= vtabarg vtabargtoken",
! 108897: /* 323 */ "vtabargtoken ::= ANY",
! 108898: /* 324 */ "vtabargtoken ::= lp anylist RP",
! 108899: /* 325 */ "lp ::= LP",
! 108900: /* 326 */ "anylist ::=",
! 108901: /* 327 */ "anylist ::= anylist LP anylist RP",
! 108902: /* 328 */ "anylist ::= anylist ANY",
! 108903: };
! 108904: #endif /* NDEBUG */
! 108905:
! 108906:
! 108907: #if YYSTACKDEPTH<=0
! 108908: /*
! 108909: ** Try to increase the size of the parser stack.
! 108910: */
! 108911: static void yyGrowStack(yyParser *p){
! 108912: int newSize;
! 108913: yyStackEntry *pNew;
! 108914:
! 108915: newSize = p->yystksz*2 + 100;
! 108916: pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
! 108917: if( pNew ){
! 108918: p->yystack = pNew;
! 108919: p->yystksz = newSize;
! 108920: #ifndef NDEBUG
! 108921: if( yyTraceFILE ){
! 108922: fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
! 108923: yyTracePrompt, p->yystksz);
! 108924: }
! 108925: #endif
! 108926: }
! 108927: }
! 108928: #endif
! 108929:
! 108930: /*
! 108931: ** This function allocates a new parser.
! 108932: ** The only argument is a pointer to a function which works like
! 108933: ** malloc.
! 108934: **
! 108935: ** Inputs:
! 108936: ** A pointer to the function used to allocate memory.
! 108937: **
! 108938: ** Outputs:
! 108939: ** A pointer to a parser. This pointer is used in subsequent calls
! 108940: ** to sqlite3Parser and sqlite3ParserFree.
! 108941: */
! 108942: SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
! 108943: yyParser *pParser;
! 108944: pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
! 108945: if( pParser ){
! 108946: pParser->yyidx = -1;
! 108947: #ifdef YYTRACKMAXSTACKDEPTH
! 108948: pParser->yyidxMax = 0;
! 108949: #endif
! 108950: #if YYSTACKDEPTH<=0
! 108951: pParser->yystack = NULL;
! 108952: pParser->yystksz = 0;
! 108953: yyGrowStack(pParser);
! 108954: #endif
! 108955: }
! 108956: return pParser;
! 108957: }
! 108958:
! 108959: /* The following function deletes the value associated with a
! 108960: ** symbol. The symbol can be either a terminal or nonterminal.
! 108961: ** "yymajor" is the symbol code, and "yypminor" is a pointer to
! 108962: ** the value.
! 108963: */
! 108964: static void yy_destructor(
! 108965: yyParser *yypParser, /* The parser */
! 108966: YYCODETYPE yymajor, /* Type code for object to destroy */
! 108967: YYMINORTYPE *yypminor /* The object to be destroyed */
! 108968: ){
! 108969: sqlite3ParserARG_FETCH;
! 108970: switch( yymajor ){
! 108971: /* Here is inserted the actions which take place when a
! 108972: ** terminal or non-terminal is destroyed. This can happen
! 108973: ** when the symbol is popped from the stack during a
! 108974: ** reduce or during error processing or when a parser is
! 108975: ** being destroyed before it is finished parsing.
! 108976: **
! 108977: ** Note: during a reduce, the only symbols destroyed are those
! 108978: ** which appear on the RHS of the rule, but which are not used
! 108979: ** inside the C code.
! 108980: */
! 108981: case 160: /* select */
! 108982: case 194: /* oneselect */
! 108983: {
! 108984: sqlite3SelectDelete(pParse->db, (yypminor->yy387));
! 108985: }
! 108986: break;
! 108987: case 174: /* term */
! 108988: case 175: /* expr */
! 108989: {
! 108990: sqlite3ExprDelete(pParse->db, (yypminor->yy118).pExpr);
! 108991: }
! 108992: break;
! 108993: case 179: /* idxlist_opt */
! 108994: case 187: /* idxlist */
! 108995: case 197: /* selcollist */
! 108996: case 200: /* groupby_opt */
! 108997: case 202: /* orderby_opt */
! 108998: case 204: /* sclp */
! 108999: case 214: /* sortlist */
! 109000: case 216: /* nexprlist */
! 109001: case 217: /* setlist */
! 109002: case 220: /* itemlist */
! 109003: case 221: /* exprlist */
! 109004: case 226: /* case_exprlist */
! 109005: {
! 109006: sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
! 109007: }
! 109008: break;
! 109009: case 193: /* fullname */
! 109010: case 198: /* from */
! 109011: case 206: /* seltablist */
! 109012: case 207: /* stl_prefix */
! 109013: {
! 109014: sqlite3SrcListDelete(pParse->db, (yypminor->yy259));
! 109015: }
! 109016: break;
! 109017: case 199: /* where_opt */
! 109018: case 201: /* having_opt */
! 109019: case 210: /* on_opt */
! 109020: case 215: /* sortitem */
! 109021: case 225: /* case_operand */
! 109022: case 227: /* case_else */
! 109023: case 238: /* when_clause */
! 109024: case 243: /* key_opt */
! 109025: {
! 109026: sqlite3ExprDelete(pParse->db, (yypminor->yy314));
! 109027: }
! 109028: break;
! 109029: case 211: /* using_opt */
! 109030: case 213: /* inscollist */
! 109031: case 219: /* inscollist_opt */
! 109032: {
! 109033: sqlite3IdListDelete(pParse->db, (yypminor->yy384));
! 109034: }
! 109035: break;
! 109036: case 234: /* trigger_cmd_list */
! 109037: case 239: /* trigger_cmd */
! 109038: {
! 109039: sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy203));
! 109040: }
! 109041: break;
! 109042: case 236: /* trigger_event */
! 109043: {
! 109044: sqlite3IdListDelete(pParse->db, (yypminor->yy90).b);
! 109045: }
! 109046: break;
! 109047: default: break; /* If no destructor action specified: do nothing */
! 109048: }
! 109049: }
! 109050:
! 109051: /*
! 109052: ** Pop the parser's stack once.
! 109053: **
! 109054: ** If there is a destructor routine associated with the token which
! 109055: ** is popped from the stack, then call it.
! 109056: **
! 109057: ** Return the major token number for the symbol popped.
! 109058: */
! 109059: static int yy_pop_parser_stack(yyParser *pParser){
! 109060: YYCODETYPE yymajor;
! 109061: yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
! 109062:
! 109063: /* There is no mechanism by which the parser stack can be popped below
! 109064: ** empty in SQLite. */
! 109065: if( NEVER(pParser->yyidx<0) ) return 0;
! 109066: #ifndef NDEBUG
! 109067: if( yyTraceFILE && pParser->yyidx>=0 ){
! 109068: fprintf(yyTraceFILE,"%sPopping %s\n",
! 109069: yyTracePrompt,
! 109070: yyTokenName[yytos->major]);
! 109071: }
! 109072: #endif
! 109073: yymajor = yytos->major;
! 109074: yy_destructor(pParser, yymajor, &yytos->minor);
! 109075: pParser->yyidx--;
! 109076: return yymajor;
! 109077: }
! 109078:
! 109079: /*
! 109080: ** Deallocate and destroy a parser. Destructors are all called for
! 109081: ** all stack elements before shutting the parser down.
! 109082: **
! 109083: ** Inputs:
! 109084: ** <ul>
! 109085: ** <li> A pointer to the parser. This should be a pointer
! 109086: ** obtained from sqlite3ParserAlloc.
! 109087: ** <li> A pointer to a function used to reclaim memory obtained
! 109088: ** from malloc.
! 109089: ** </ul>
! 109090: */
! 109091: SQLITE_PRIVATE void sqlite3ParserFree(
! 109092: void *p, /* The parser to be deleted */
! 109093: void (*freeProc)(void*) /* Function used to reclaim memory */
! 109094: ){
! 109095: yyParser *pParser = (yyParser*)p;
! 109096: /* In SQLite, we never try to destroy a parser that was not successfully
! 109097: ** created in the first place. */
! 109098: if( NEVER(pParser==0) ) return;
! 109099: while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
! 109100: #if YYSTACKDEPTH<=0
! 109101: free(pParser->yystack);
! 109102: #endif
! 109103: (*freeProc)((void*)pParser);
! 109104: }
! 109105:
! 109106: /*
! 109107: ** Return the peak depth of the stack for a parser.
! 109108: */
! 109109: #ifdef YYTRACKMAXSTACKDEPTH
! 109110: SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
! 109111: yyParser *pParser = (yyParser*)p;
! 109112: return pParser->yyidxMax;
! 109113: }
! 109114: #endif
! 109115:
! 109116: /*
! 109117: ** Find the appropriate action for a parser given the terminal
! 109118: ** look-ahead token iLookAhead.
! 109119: **
! 109120: ** If the look-ahead token is YYNOCODE, then check to see if the action is
! 109121: ** independent of the look-ahead. If it is, return the action, otherwise
! 109122: ** return YY_NO_ACTION.
! 109123: */
! 109124: static int yy_find_shift_action(
! 109125: yyParser *pParser, /* The parser */
! 109126: YYCODETYPE iLookAhead /* The look-ahead token */
! 109127: ){
! 109128: int i;
! 109129: int stateno = pParser->yystack[pParser->yyidx].stateno;
! 109130:
! 109131: if( stateno>YY_SHIFT_COUNT
! 109132: || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
! 109133: return yy_default[stateno];
! 109134: }
! 109135: assert( iLookAhead!=YYNOCODE );
! 109136: i += iLookAhead;
! 109137: if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
! 109138: if( iLookAhead>0 ){
! 109139: #ifdef YYFALLBACK
! 109140: YYCODETYPE iFallback; /* Fallback token */
! 109141: if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
! 109142: && (iFallback = yyFallback[iLookAhead])!=0 ){
! 109143: #ifndef NDEBUG
! 109144: if( yyTraceFILE ){
! 109145: fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
! 109146: yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
! 109147: }
! 109148: #endif
! 109149: return yy_find_shift_action(pParser, iFallback);
! 109150: }
! 109151: #endif
! 109152: #ifdef YYWILDCARD
! 109153: {
! 109154: int j = i - iLookAhead + YYWILDCARD;
! 109155: if(
! 109156: #if YY_SHIFT_MIN+YYWILDCARD<0
! 109157: j>=0 &&
! 109158: #endif
! 109159: #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
! 109160: j<YY_ACTTAB_COUNT &&
! 109161: #endif
! 109162: yy_lookahead[j]==YYWILDCARD
! 109163: ){
! 109164: #ifndef NDEBUG
! 109165: if( yyTraceFILE ){
! 109166: fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
! 109167: yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
! 109168: }
! 109169: #endif /* NDEBUG */
! 109170: return yy_action[j];
! 109171: }
! 109172: }
! 109173: #endif /* YYWILDCARD */
! 109174: }
! 109175: return yy_default[stateno];
! 109176: }else{
! 109177: return yy_action[i];
! 109178: }
! 109179: }
! 109180:
! 109181: /*
! 109182: ** Find the appropriate action for a parser given the non-terminal
! 109183: ** look-ahead token iLookAhead.
! 109184: **
! 109185: ** If the look-ahead token is YYNOCODE, then check to see if the action is
! 109186: ** independent of the look-ahead. If it is, return the action, otherwise
! 109187: ** return YY_NO_ACTION.
! 109188: */
! 109189: static int yy_find_reduce_action(
! 109190: int stateno, /* Current state number */
! 109191: YYCODETYPE iLookAhead /* The look-ahead token */
! 109192: ){
! 109193: int i;
! 109194: #ifdef YYERRORSYMBOL
! 109195: if( stateno>YY_REDUCE_COUNT ){
! 109196: return yy_default[stateno];
! 109197: }
! 109198: #else
! 109199: assert( stateno<=YY_REDUCE_COUNT );
! 109200: #endif
! 109201: i = yy_reduce_ofst[stateno];
! 109202: assert( i!=YY_REDUCE_USE_DFLT );
! 109203: assert( iLookAhead!=YYNOCODE );
! 109204: i += iLookAhead;
! 109205: #ifdef YYERRORSYMBOL
! 109206: if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
! 109207: return yy_default[stateno];
! 109208: }
! 109209: #else
! 109210: assert( i>=0 && i<YY_ACTTAB_COUNT );
! 109211: assert( yy_lookahead[i]==iLookAhead );
! 109212: #endif
! 109213: return yy_action[i];
! 109214: }
! 109215:
! 109216: /*
! 109217: ** The following routine is called if the stack overflows.
! 109218: */
! 109219: static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
! 109220: sqlite3ParserARG_FETCH;
! 109221: yypParser->yyidx--;
! 109222: #ifndef NDEBUG
! 109223: if( yyTraceFILE ){
! 109224: fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
! 109225: }
! 109226: #endif
! 109227: while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
! 109228: /* Here code is inserted which will execute if the parser
! 109229: ** stack every overflows */
! 109230:
! 109231: UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
! 109232: sqlite3ErrorMsg(pParse, "parser stack overflow");
! 109233: sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
! 109234: }
! 109235:
! 109236: /*
! 109237: ** Perform a shift action.
! 109238: */
! 109239: static void yy_shift(
! 109240: yyParser *yypParser, /* The parser to be shifted */
! 109241: int yyNewState, /* The new state to shift in */
! 109242: int yyMajor, /* The major token to shift in */
! 109243: YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */
! 109244: ){
! 109245: yyStackEntry *yytos;
! 109246: yypParser->yyidx++;
! 109247: #ifdef YYTRACKMAXSTACKDEPTH
! 109248: if( yypParser->yyidx>yypParser->yyidxMax ){
! 109249: yypParser->yyidxMax = yypParser->yyidx;
! 109250: }
! 109251: #endif
! 109252: #if YYSTACKDEPTH>0
! 109253: if( yypParser->yyidx>=YYSTACKDEPTH ){
! 109254: yyStackOverflow(yypParser, yypMinor);
! 109255: return;
! 109256: }
! 109257: #else
! 109258: if( yypParser->yyidx>=yypParser->yystksz ){
! 109259: yyGrowStack(yypParser);
! 109260: if( yypParser->yyidx>=yypParser->yystksz ){
! 109261: yyStackOverflow(yypParser, yypMinor);
! 109262: return;
! 109263: }
! 109264: }
! 109265: #endif
! 109266: yytos = &yypParser->yystack[yypParser->yyidx];
! 109267: yytos->stateno = (YYACTIONTYPE)yyNewState;
! 109268: yytos->major = (YYCODETYPE)yyMajor;
! 109269: yytos->minor = *yypMinor;
! 109270: #ifndef NDEBUG
! 109271: if( yyTraceFILE && yypParser->yyidx>0 ){
! 109272: int i;
! 109273: fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
! 109274: fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
! 109275: for(i=1; i<=yypParser->yyidx; i++)
! 109276: fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
! 109277: fprintf(yyTraceFILE,"\n");
! 109278: }
! 109279: #endif
! 109280: }
! 109281:
! 109282: /* The following table contains information about every rule that
! 109283: ** is used during the reduce.
! 109284: */
! 109285: static const struct {
! 109286: YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
! 109287: unsigned char nrhs; /* Number of right-hand side symbols in the rule */
! 109288: } yyRuleInfo[] = {
! 109289: { 142, 1 },
! 109290: { 143, 2 },
! 109291: { 143, 1 },
! 109292: { 144, 1 },
! 109293: { 144, 3 },
! 109294: { 145, 0 },
! 109295: { 145, 1 },
! 109296: { 145, 3 },
! 109297: { 146, 1 },
! 109298: { 147, 3 },
! 109299: { 149, 0 },
! 109300: { 149, 1 },
! 109301: { 149, 2 },
! 109302: { 148, 0 },
! 109303: { 148, 1 },
! 109304: { 148, 1 },
! 109305: { 148, 1 },
! 109306: { 147, 2 },
! 109307: { 147, 2 },
! 109308: { 147, 2 },
! 109309: { 151, 1 },
! 109310: { 151, 0 },
! 109311: { 147, 2 },
! 109312: { 147, 3 },
! 109313: { 147, 5 },
! 109314: { 147, 2 },
! 109315: { 152, 6 },
! 109316: { 154, 1 },
! 109317: { 156, 0 },
! 109318: { 156, 3 },
! 109319: { 155, 1 },
! 109320: { 155, 0 },
! 109321: { 153, 4 },
! 109322: { 153, 2 },
! 109323: { 158, 3 },
! 109324: { 158, 1 },
! 109325: { 161, 3 },
! 109326: { 162, 1 },
! 109327: { 165, 1 },
! 109328: { 165, 1 },
! 109329: { 166, 1 },
! 109330: { 150, 1 },
! 109331: { 150, 1 },
! 109332: { 150, 1 },
! 109333: { 163, 0 },
! 109334: { 163, 1 },
! 109335: { 167, 1 },
! 109336: { 167, 4 },
! 109337: { 167, 6 },
! 109338: { 168, 1 },
! 109339: { 168, 2 },
! 109340: { 169, 1 },
! 109341: { 169, 1 },
! 109342: { 164, 2 },
! 109343: { 164, 0 },
! 109344: { 172, 3 },
! 109345: { 172, 1 },
! 109346: { 173, 2 },
! 109347: { 173, 4 },
! 109348: { 173, 3 },
! 109349: { 173, 3 },
! 109350: { 173, 2 },
! 109351: { 173, 2 },
! 109352: { 173, 3 },
! 109353: { 173, 5 },
! 109354: { 173, 2 },
! 109355: { 173, 4 },
! 109356: { 173, 4 },
! 109357: { 173, 1 },
! 109358: { 173, 2 },
! 109359: { 178, 0 },
! 109360: { 178, 1 },
! 109361: { 180, 0 },
! 109362: { 180, 2 },
! 109363: { 182, 2 },
! 109364: { 182, 3 },
! 109365: { 182, 3 },
! 109366: { 182, 3 },
! 109367: { 183, 2 },
! 109368: { 183, 2 },
! 109369: { 183, 1 },
! 109370: { 183, 1 },
! 109371: { 183, 2 },
! 109372: { 181, 3 },
! 109373: { 181, 2 },
! 109374: { 184, 0 },
! 109375: { 184, 2 },
! 109376: { 184, 2 },
! 109377: { 159, 0 },
! 109378: { 159, 2 },
! 109379: { 185, 3 },
! 109380: { 185, 2 },
! 109381: { 185, 1 },
! 109382: { 186, 2 },
! 109383: { 186, 7 },
! 109384: { 186, 5 },
! 109385: { 186, 5 },
! 109386: { 186, 10 },
! 109387: { 188, 0 },
! 109388: { 188, 1 },
! 109389: { 176, 0 },
! 109390: { 176, 3 },
! 109391: { 189, 0 },
! 109392: { 189, 2 },
! 109393: { 190, 1 },
! 109394: { 190, 1 },
! 109395: { 190, 1 },
! 109396: { 147, 4 },
! 109397: { 192, 2 },
! 109398: { 192, 0 },
! 109399: { 147, 8 },
! 109400: { 147, 4 },
! 109401: { 147, 1 },
! 109402: { 160, 1 },
! 109403: { 160, 3 },
! 109404: { 195, 1 },
! 109405: { 195, 2 },
! 109406: { 195, 1 },
! 109407: { 194, 9 },
! 109408: { 196, 1 },
! 109409: { 196, 1 },
! 109410: { 196, 0 },
! 109411: { 204, 2 },
! 109412: { 204, 0 },
! 109413: { 197, 3 },
! 109414: { 197, 2 },
! 109415: { 197, 4 },
! 109416: { 205, 2 },
! 109417: { 205, 1 },
! 109418: { 205, 0 },
! 109419: { 198, 0 },
! 109420: { 198, 2 },
! 109421: { 207, 2 },
! 109422: { 207, 0 },
! 109423: { 206, 7 },
! 109424: { 206, 7 },
! 109425: { 206, 7 },
! 109426: { 157, 0 },
! 109427: { 157, 2 },
! 109428: { 193, 2 },
! 109429: { 208, 1 },
! 109430: { 208, 2 },
! 109431: { 208, 3 },
! 109432: { 208, 4 },
! 109433: { 210, 2 },
! 109434: { 210, 0 },
! 109435: { 209, 0 },
! 109436: { 209, 3 },
! 109437: { 209, 2 },
! 109438: { 211, 4 },
! 109439: { 211, 0 },
! 109440: { 202, 0 },
! 109441: { 202, 3 },
! 109442: { 214, 4 },
! 109443: { 214, 2 },
! 109444: { 215, 1 },
! 109445: { 177, 1 },
! 109446: { 177, 1 },
! 109447: { 177, 0 },
! 109448: { 200, 0 },
! 109449: { 200, 3 },
! 109450: { 201, 0 },
! 109451: { 201, 2 },
! 109452: { 203, 0 },
! 109453: { 203, 2 },
! 109454: { 203, 4 },
! 109455: { 203, 4 },
! 109456: { 147, 5 },
! 109457: { 199, 0 },
! 109458: { 199, 2 },
! 109459: { 147, 7 },
! 109460: { 217, 5 },
! 109461: { 217, 3 },
! 109462: { 147, 8 },
! 109463: { 147, 5 },
! 109464: { 147, 6 },
! 109465: { 218, 2 },
! 109466: { 218, 1 },
! 109467: { 220, 3 },
! 109468: { 220, 1 },
! 109469: { 219, 0 },
! 109470: { 219, 3 },
! 109471: { 213, 3 },
! 109472: { 213, 1 },
! 109473: { 175, 1 },
! 109474: { 175, 3 },
! 109475: { 174, 1 },
! 109476: { 175, 1 },
! 109477: { 175, 1 },
! 109478: { 175, 3 },
! 109479: { 175, 5 },
! 109480: { 174, 1 },
! 109481: { 174, 1 },
! 109482: { 175, 1 },
! 109483: { 175, 1 },
! 109484: { 175, 3 },
! 109485: { 175, 6 },
! 109486: { 175, 5 },
! 109487: { 175, 4 },
! 109488: { 174, 1 },
! 109489: { 175, 3 },
! 109490: { 175, 3 },
! 109491: { 175, 3 },
! 109492: { 175, 3 },
! 109493: { 175, 3 },
! 109494: { 175, 3 },
! 109495: { 175, 3 },
! 109496: { 175, 3 },
! 109497: { 222, 1 },
! 109498: { 222, 2 },
! 109499: { 222, 1 },
! 109500: { 222, 2 },
! 109501: { 175, 3 },
! 109502: { 175, 5 },
! 109503: { 175, 2 },
! 109504: { 175, 3 },
! 109505: { 175, 3 },
! 109506: { 175, 4 },
! 109507: { 175, 2 },
! 109508: { 175, 2 },
! 109509: { 175, 2 },
! 109510: { 175, 2 },
! 109511: { 223, 1 },
! 109512: { 223, 2 },
! 109513: { 175, 5 },
! 109514: { 224, 1 },
! 109515: { 224, 2 },
! 109516: { 175, 5 },
! 109517: { 175, 3 },
! 109518: { 175, 5 },
! 109519: { 175, 4 },
! 109520: { 175, 4 },
! 109521: { 175, 5 },
! 109522: { 226, 5 },
! 109523: { 226, 4 },
! 109524: { 227, 2 },
! 109525: { 227, 0 },
! 109526: { 225, 1 },
! 109527: { 225, 0 },
! 109528: { 221, 1 },
! 109529: { 221, 0 },
! 109530: { 216, 3 },
! 109531: { 216, 1 },
! 109532: { 147, 11 },
! 109533: { 228, 1 },
! 109534: { 228, 0 },
! 109535: { 179, 0 },
! 109536: { 179, 3 },
! 109537: { 187, 5 },
! 109538: { 187, 3 },
! 109539: { 229, 0 },
! 109540: { 229, 2 },
! 109541: { 147, 4 },
! 109542: { 147, 1 },
! 109543: { 147, 2 },
! 109544: { 147, 3 },
! 109545: { 147, 5 },
! 109546: { 147, 6 },
! 109547: { 147, 5 },
! 109548: { 147, 6 },
! 109549: { 230, 1 },
! 109550: { 230, 1 },
! 109551: { 230, 1 },
! 109552: { 230, 1 },
! 109553: { 230, 1 },
! 109554: { 170, 2 },
! 109555: { 171, 2 },
! 109556: { 232, 1 },
! 109557: { 231, 1 },
! 109558: { 231, 0 },
! 109559: { 147, 5 },
! 109560: { 233, 11 },
! 109561: { 235, 1 },
! 109562: { 235, 1 },
! 109563: { 235, 2 },
! 109564: { 235, 0 },
! 109565: { 236, 1 },
! 109566: { 236, 1 },
! 109567: { 236, 3 },
! 109568: { 237, 0 },
! 109569: { 237, 3 },
! 109570: { 238, 0 },
! 109571: { 238, 2 },
! 109572: { 234, 3 },
! 109573: { 234, 2 },
! 109574: { 240, 1 },
! 109575: { 240, 3 },
! 109576: { 241, 0 },
! 109577: { 241, 3 },
! 109578: { 241, 2 },
! 109579: { 239, 7 },
! 109580: { 239, 8 },
! 109581: { 239, 5 },
! 109582: { 239, 5 },
! 109583: { 239, 1 },
! 109584: { 175, 4 },
! 109585: { 175, 6 },
! 109586: { 191, 1 },
! 109587: { 191, 1 },
! 109588: { 191, 1 },
! 109589: { 147, 4 },
! 109590: { 147, 6 },
! 109591: { 147, 3 },
! 109592: { 243, 0 },
! 109593: { 243, 2 },
! 109594: { 242, 1 },
! 109595: { 242, 0 },
! 109596: { 147, 1 },
! 109597: { 147, 3 },
! 109598: { 147, 1 },
! 109599: { 147, 3 },
! 109600: { 147, 6 },
! 109601: { 147, 6 },
! 109602: { 244, 1 },
! 109603: { 245, 0 },
! 109604: { 245, 1 },
! 109605: { 147, 1 },
! 109606: { 147, 4 },
! 109607: { 246, 7 },
! 109608: { 247, 1 },
! 109609: { 247, 3 },
! 109610: { 248, 0 },
! 109611: { 248, 2 },
! 109612: { 249, 1 },
! 109613: { 249, 3 },
! 109614: { 250, 1 },
! 109615: { 251, 0 },
! 109616: { 251, 4 },
! 109617: { 251, 2 },
! 109618: };
! 109619:
! 109620: static void yy_accept(yyParser*); /* Forward Declaration */
! 109621:
! 109622: /*
! 109623: ** Perform a reduce action and the shift that must immediately
! 109624: ** follow the reduce.
! 109625: */
! 109626: static void yy_reduce(
! 109627: yyParser *yypParser, /* The parser */
! 109628: int yyruleno /* Number of the rule by which to reduce */
! 109629: ){
! 109630: int yygoto; /* The next state */
! 109631: int yyact; /* The next action */
! 109632: YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
! 109633: yyStackEntry *yymsp; /* The top of the parser's stack */
! 109634: int yysize; /* Amount to pop the stack */
! 109635: sqlite3ParserARG_FETCH;
! 109636: yymsp = &yypParser->yystack[yypParser->yyidx];
! 109637: #ifndef NDEBUG
! 109638: if( yyTraceFILE && yyruleno>=0
! 109639: && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
! 109640: fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
! 109641: yyRuleName[yyruleno]);
! 109642: }
! 109643: #endif /* NDEBUG */
! 109644:
! 109645: /* Silence complaints from purify about yygotominor being uninitialized
! 109646: ** in some cases when it is copied into the stack after the following
! 109647: ** switch. yygotominor is uninitialized when a rule reduces that does
! 109648: ** not set the value of its left-hand side nonterminal. Leaving the
! 109649: ** value of the nonterminal uninitialized is utterly harmless as long
! 109650: ** as the value is never used. So really the only thing this code
! 109651: ** accomplishes is to quieten purify.
! 109652: **
! 109653: ** 2007-01-16: The wireshark project (www.wireshark.org) reports that
! 109654: ** without this code, their parser segfaults. I'm not sure what there
! 109655: ** parser is doing to make this happen. This is the second bug report
! 109656: ** from wireshark this week. Clearly they are stressing Lemon in ways
! 109657: ** that it has not been previously stressed... (SQLite ticket #2172)
! 109658: */
! 109659: /*memset(&yygotominor, 0, sizeof(yygotominor));*/
! 109660: yygotominor = yyzerominor;
! 109661:
! 109662:
! 109663: switch( yyruleno ){
! 109664: /* Beginning here are the reduction cases. A typical example
! 109665: ** follows:
! 109666: ** case 0:
! 109667: ** #line <lineno> <grammarfile>
! 109668: ** { ... } // User supplied code
! 109669: ** #line <lineno> <thisfile>
! 109670: ** break;
! 109671: */
! 109672: case 5: /* explain ::= */
! 109673: { sqlite3BeginParse(pParse, 0); }
! 109674: break;
! 109675: case 6: /* explain ::= EXPLAIN */
! 109676: { sqlite3BeginParse(pParse, 1); }
! 109677: break;
! 109678: case 7: /* explain ::= EXPLAIN QUERY PLAN */
! 109679: { sqlite3BeginParse(pParse, 2); }
! 109680: break;
! 109681: case 8: /* cmdx ::= cmd */
! 109682: { sqlite3FinishCoding(pParse); }
! 109683: break;
! 109684: case 9: /* cmd ::= BEGIN transtype trans_opt */
! 109685: {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy4);}
! 109686: break;
! 109687: case 13: /* transtype ::= */
! 109688: {yygotominor.yy4 = TK_DEFERRED;}
! 109689: break;
! 109690: case 14: /* transtype ::= DEFERRED */
! 109691: case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
! 109692: case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
! 109693: case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
! 109694: case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117);
! 109695: {yygotominor.yy4 = yymsp[0].major;}
! 109696: break;
! 109697: case 17: /* cmd ::= COMMIT trans_opt */
! 109698: case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
! 109699: {sqlite3CommitTransaction(pParse);}
! 109700: break;
! 109701: case 19: /* cmd ::= ROLLBACK trans_opt */
! 109702: {sqlite3RollbackTransaction(pParse);}
! 109703: break;
! 109704: case 22: /* cmd ::= SAVEPOINT nm */
! 109705: {
! 109706: sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
! 109707: }
! 109708: break;
! 109709: case 23: /* cmd ::= RELEASE savepoint_opt nm */
! 109710: {
! 109711: sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
! 109712: }
! 109713: break;
! 109714: case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
! 109715: {
! 109716: sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
! 109717: }
! 109718: break;
! 109719: case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
! 109720: {
! 109721: sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy4,0,0,yymsp[-2].minor.yy4);
! 109722: }
! 109723: break;
! 109724: case 27: /* createkw ::= CREATE */
! 109725: {
! 109726: pParse->db->lookaside.bEnabled = 0;
! 109727: yygotominor.yy0 = yymsp[0].minor.yy0;
! 109728: }
! 109729: break;
! 109730: case 28: /* ifnotexists ::= */
! 109731: case 31: /* temp ::= */ yytestcase(yyruleno==31);
! 109732: case 70: /* autoinc ::= */ yytestcase(yyruleno==70);
! 109733: case 83: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==83);
! 109734: case 85: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==85);
! 109735: case 87: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==87);
! 109736: case 98: /* defer_subclause_opt ::= */ yytestcase(yyruleno==98);
! 109737: case 109: /* ifexists ::= */ yytestcase(yyruleno==109);
! 109738: case 120: /* distinct ::= ALL */ yytestcase(yyruleno==120);
! 109739: case 121: /* distinct ::= */ yytestcase(yyruleno==121);
! 109740: case 222: /* between_op ::= BETWEEN */ yytestcase(yyruleno==222);
! 109741: case 225: /* in_op ::= IN */ yytestcase(yyruleno==225);
! 109742: {yygotominor.yy4 = 0;}
! 109743: break;
! 109744: case 29: /* ifnotexists ::= IF NOT EXISTS */
! 109745: case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
! 109746: case 71: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==71);
! 109747: case 86: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==86);
! 109748: case 108: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==108);
! 109749: case 119: /* distinct ::= DISTINCT */ yytestcase(yyruleno==119);
! 109750: case 223: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==223);
! 109751: case 226: /* in_op ::= NOT IN */ yytestcase(yyruleno==226);
! 109752: {yygotominor.yy4 = 1;}
! 109753: break;
! 109754: case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
! 109755: {
! 109756: sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0);
! 109757: }
! 109758: break;
! 109759: case 33: /* create_table_args ::= AS select */
! 109760: {
! 109761: sqlite3EndTable(pParse,0,0,yymsp[0].minor.yy387);
! 109762: sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
! 109763: }
! 109764: break;
! 109765: case 36: /* column ::= columnid type carglist */
! 109766: {
! 109767: yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
! 109768: yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
! 109769: }
! 109770: break;
! 109771: case 37: /* columnid ::= nm */
! 109772: {
! 109773: sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
! 109774: yygotominor.yy0 = yymsp[0].minor.yy0;
! 109775: }
! 109776: break;
! 109777: case 38: /* id ::= ID */
! 109778: case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39);
! 109779: case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40);
! 109780: case 41: /* nm ::= id */ yytestcase(yyruleno==41);
! 109781: case 42: /* nm ::= STRING */ yytestcase(yyruleno==42);
! 109782: case 43: /* nm ::= JOIN_KW */ yytestcase(yyruleno==43);
! 109783: case 46: /* typetoken ::= typename */ yytestcase(yyruleno==46);
! 109784: case 49: /* typename ::= ids */ yytestcase(yyruleno==49);
! 109785: case 127: /* as ::= AS nm */ yytestcase(yyruleno==127);
! 109786: case 128: /* as ::= ids */ yytestcase(yyruleno==128);
! 109787: case 138: /* dbnm ::= DOT nm */ yytestcase(yyruleno==138);
! 109788: case 147: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==147);
! 109789: case 251: /* collate ::= COLLATE ids */ yytestcase(yyruleno==251);
! 109790: case 260: /* nmnum ::= plus_num */ yytestcase(yyruleno==260);
! 109791: case 261: /* nmnum ::= nm */ yytestcase(yyruleno==261);
! 109792: case 262: /* nmnum ::= ON */ yytestcase(yyruleno==262);
! 109793: case 263: /* nmnum ::= DELETE */ yytestcase(yyruleno==263);
! 109794: case 264: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==264);
! 109795: case 265: /* plus_num ::= plus_opt number */ yytestcase(yyruleno==265);
! 109796: case 266: /* minus_num ::= MINUS number */ yytestcase(yyruleno==266);
! 109797: case 267: /* number ::= INTEGER|FLOAT */ yytestcase(yyruleno==267);
! 109798: case 285: /* trnm ::= nm */ yytestcase(yyruleno==285);
! 109799: {yygotominor.yy0 = yymsp[0].minor.yy0;}
! 109800: break;
! 109801: case 45: /* type ::= typetoken */
! 109802: {sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
! 109803: break;
! 109804: case 47: /* typetoken ::= typename LP signed RP */
! 109805: {
! 109806: yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
! 109807: yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
! 109808: }
! 109809: break;
! 109810: case 48: /* typetoken ::= typename LP signed COMMA signed RP */
! 109811: {
! 109812: yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
! 109813: yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
! 109814: }
! 109815: break;
! 109816: case 50: /* typename ::= typename ids */
! 109817: {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);}
! 109818: break;
! 109819: case 57: /* ccons ::= DEFAULT term */
! 109820: case 59: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==59);
! 109821: {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy118);}
! 109822: break;
! 109823: case 58: /* ccons ::= DEFAULT LP expr RP */
! 109824: {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy118);}
! 109825: break;
! 109826: case 60: /* ccons ::= DEFAULT MINUS term */
! 109827: {
! 109828: ExprSpan v;
! 109829: v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy118.pExpr, 0, 0);
! 109830: v.zStart = yymsp[-1].minor.yy0.z;
! 109831: v.zEnd = yymsp[0].minor.yy118.zEnd;
! 109832: sqlite3AddDefaultValue(pParse,&v);
! 109833: }
! 109834: break;
! 109835: case 61: /* ccons ::= DEFAULT id */
! 109836: {
! 109837: ExprSpan v;
! 109838: spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
! 109839: sqlite3AddDefaultValue(pParse,&v);
! 109840: }
! 109841: break;
! 109842: case 63: /* ccons ::= NOT NULL onconf */
! 109843: {sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);}
! 109844: break;
! 109845: case 64: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
! 109846: {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy4,yymsp[0].minor.yy4,yymsp[-2].minor.yy4);}
! 109847: break;
! 109848: case 65: /* ccons ::= UNIQUE onconf */
! 109849: {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy4,0,0,0,0);}
! 109850: break;
! 109851: case 66: /* ccons ::= CHECK LP expr RP */
! 109852: {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy118.pExpr);}
! 109853: break;
! 109854: case 67: /* ccons ::= REFERENCES nm idxlist_opt refargs */
! 109855: {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy4);}
! 109856: break;
! 109857: case 68: /* ccons ::= defer_subclause */
! 109858: {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy4);}
! 109859: break;
! 109860: case 69: /* ccons ::= COLLATE ids */
! 109861: {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
! 109862: break;
! 109863: case 72: /* refargs ::= */
! 109864: { yygotominor.yy4 = OE_None*0x0101; /* EV: R-19803-45884 */}
! 109865: break;
! 109866: case 73: /* refargs ::= refargs refarg */
! 109867: { yygotominor.yy4 = (yymsp[-1].minor.yy4 & ~yymsp[0].minor.yy215.mask) | yymsp[0].minor.yy215.value; }
! 109868: break;
! 109869: case 74: /* refarg ::= MATCH nm */
! 109870: case 75: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==75);
! 109871: { yygotominor.yy215.value = 0; yygotominor.yy215.mask = 0x000000; }
! 109872: break;
! 109873: case 76: /* refarg ::= ON DELETE refact */
! 109874: { yygotominor.yy215.value = yymsp[0].minor.yy4; yygotominor.yy215.mask = 0x0000ff; }
! 109875: break;
! 109876: case 77: /* refarg ::= ON UPDATE refact */
! 109877: { yygotominor.yy215.value = yymsp[0].minor.yy4<<8; yygotominor.yy215.mask = 0x00ff00; }
! 109878: break;
! 109879: case 78: /* refact ::= SET NULL */
! 109880: { yygotominor.yy4 = OE_SetNull; /* EV: R-33326-45252 */}
! 109881: break;
! 109882: case 79: /* refact ::= SET DEFAULT */
! 109883: { yygotominor.yy4 = OE_SetDflt; /* EV: R-33326-45252 */}
! 109884: break;
! 109885: case 80: /* refact ::= CASCADE */
! 109886: { yygotominor.yy4 = OE_Cascade; /* EV: R-33326-45252 */}
! 109887: break;
! 109888: case 81: /* refact ::= RESTRICT */
! 109889: { yygotominor.yy4 = OE_Restrict; /* EV: R-33326-45252 */}
! 109890: break;
! 109891: case 82: /* refact ::= NO ACTION */
! 109892: { yygotominor.yy4 = OE_None; /* EV: R-33326-45252 */}
! 109893: break;
! 109894: case 84: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
! 109895: case 99: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==99);
! 109896: case 101: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==101);
! 109897: case 104: /* resolvetype ::= raisetype */ yytestcase(yyruleno==104);
! 109898: {yygotominor.yy4 = yymsp[0].minor.yy4;}
! 109899: break;
! 109900: case 88: /* conslist_opt ::= */
! 109901: {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
! 109902: break;
! 109903: case 89: /* conslist_opt ::= COMMA conslist */
! 109904: {yygotominor.yy0 = yymsp[-1].minor.yy0;}
! 109905: break;
! 109906: case 94: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
! 109907: {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy4,yymsp[-2].minor.yy4,0);}
! 109908: break;
! 109909: case 95: /* tcons ::= UNIQUE LP idxlist RP onconf */
! 109910: {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy4,0,0,0,0);}
! 109911: break;
! 109912: case 96: /* tcons ::= CHECK LP expr RP onconf */
! 109913: {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy118.pExpr);}
! 109914: break;
! 109915: case 97: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
! 109916: {
! 109917: sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[-1].minor.yy4);
! 109918: sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy4);
! 109919: }
! 109920: break;
! 109921: case 100: /* onconf ::= */
! 109922: {yygotominor.yy4 = OE_Default;}
! 109923: break;
! 109924: case 102: /* orconf ::= */
! 109925: {yygotominor.yy210 = OE_Default;}
! 109926: break;
! 109927: case 103: /* orconf ::= OR resolvetype */
! 109928: {yygotominor.yy210 = (u8)yymsp[0].minor.yy4;}
! 109929: break;
! 109930: case 105: /* resolvetype ::= IGNORE */
! 109931: {yygotominor.yy4 = OE_Ignore;}
! 109932: break;
! 109933: case 106: /* resolvetype ::= REPLACE */
! 109934: {yygotominor.yy4 = OE_Replace;}
! 109935: break;
! 109936: case 107: /* cmd ::= DROP TABLE ifexists fullname */
! 109937: {
! 109938: sqlite3DropTable(pParse, yymsp[0].minor.yy259, 0, yymsp[-1].minor.yy4);
! 109939: }
! 109940: break;
! 109941: case 110: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
! 109942: {
! 109943: 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);
! 109944: }
! 109945: break;
! 109946: case 111: /* cmd ::= DROP VIEW ifexists fullname */
! 109947: {
! 109948: sqlite3DropTable(pParse, yymsp[0].minor.yy259, 1, yymsp[-1].minor.yy4);
! 109949: }
! 109950: break;
! 109951: case 112: /* cmd ::= select */
! 109952: {
! 109953: SelectDest dest = {SRT_Output, 0, 0, 0, 0};
! 109954: sqlite3Select(pParse, yymsp[0].minor.yy387, &dest);
! 109955: sqlite3ExplainBegin(pParse->pVdbe);
! 109956: sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy387);
! 109957: sqlite3ExplainFinish(pParse->pVdbe);
! 109958: sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
! 109959: }
! 109960: break;
! 109961: case 113: /* select ::= oneselect */
! 109962: {yygotominor.yy387 = yymsp[0].minor.yy387;}
! 109963: break;
! 109964: case 114: /* select ::= select multiselect_op oneselect */
! 109965: {
! 109966: if( yymsp[0].minor.yy387 ){
! 109967: yymsp[0].minor.yy387->op = (u8)yymsp[-1].minor.yy4;
! 109968: yymsp[0].minor.yy387->pPrior = yymsp[-2].minor.yy387;
! 109969: }else{
! 109970: sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy387);
! 109971: }
! 109972: yygotominor.yy387 = yymsp[0].minor.yy387;
! 109973: }
! 109974: break;
! 109975: case 116: /* multiselect_op ::= UNION ALL */
! 109976: {yygotominor.yy4 = TK_ALL;}
! 109977: break;
! 109978: case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
! 109979: {
! 109980: 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);
! 109981: }
! 109982: break;
! 109983: case 122: /* sclp ::= selcollist COMMA */
! 109984: case 247: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==247);
! 109985: {yygotominor.yy322 = yymsp[-1].minor.yy322;}
! 109986: break;
! 109987: case 123: /* sclp ::= */
! 109988: case 151: /* orderby_opt ::= */ yytestcase(yyruleno==151);
! 109989: case 159: /* groupby_opt ::= */ yytestcase(yyruleno==159);
! 109990: case 240: /* exprlist ::= */ yytestcase(yyruleno==240);
! 109991: case 246: /* idxlist_opt ::= */ yytestcase(yyruleno==246);
! 109992: {yygotominor.yy322 = 0;}
! 109993: break;
! 109994: case 124: /* selcollist ::= sclp expr as */
! 109995: {
! 109996: yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, yymsp[-1].minor.yy118.pExpr);
! 109997: if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[0].minor.yy0, 1);
! 109998: sqlite3ExprListSetSpan(pParse,yygotominor.yy322,&yymsp[-1].minor.yy118);
! 109999: }
! 110000: break;
! 110001: case 125: /* selcollist ::= sclp STAR */
! 110002: {
! 110003: Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
! 110004: yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy322, p);
! 110005: }
! 110006: break;
! 110007: case 126: /* selcollist ::= sclp nm DOT STAR */
! 110008: {
! 110009: Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
! 110010: Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
! 110011: Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
! 110012: yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, pDot);
! 110013: }
! 110014: break;
! 110015: case 129: /* as ::= */
! 110016: {yygotominor.yy0.n = 0;}
! 110017: break;
! 110018: case 130: /* from ::= */
! 110019: {yygotominor.yy259 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy259));}
! 110020: break;
! 110021: case 131: /* from ::= FROM seltablist */
! 110022: {
! 110023: yygotominor.yy259 = yymsp[0].minor.yy259;
! 110024: sqlite3SrcListShiftJoinType(yygotominor.yy259);
! 110025: }
! 110026: break;
! 110027: case 132: /* stl_prefix ::= seltablist joinop */
! 110028: {
! 110029: yygotominor.yy259 = yymsp[-1].minor.yy259;
! 110030: if( ALWAYS(yygotominor.yy259 && yygotominor.yy259->nSrc>0) ) yygotominor.yy259->a[yygotominor.yy259->nSrc-1].jointype = (u8)yymsp[0].minor.yy4;
! 110031: }
! 110032: break;
! 110033: case 133: /* stl_prefix ::= */
! 110034: {yygotominor.yy259 = 0;}
! 110035: break;
! 110036: case 134: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
! 110037: {
! 110038: 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);
! 110039: sqlite3SrcListIndexedBy(pParse, yygotominor.yy259, &yymsp[-2].minor.yy0);
! 110040: }
! 110041: break;
! 110042: case 135: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
! 110043: {
! 110044: 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);
! 110045: }
! 110046: break;
! 110047: case 136: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
! 110048: {
! 110049: if( yymsp[-6].minor.yy259==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy314==0 && yymsp[0].minor.yy384==0 ){
! 110050: yygotominor.yy259 = yymsp[-4].minor.yy259;
! 110051: }else{
! 110052: Select *pSubquery;
! 110053: sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy259);
! 110054: pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy259,0,0,0,0,0,0,0);
! 110055: yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy314,yymsp[0].minor.yy384);
! 110056: }
! 110057: }
! 110058: break;
! 110059: case 137: /* dbnm ::= */
! 110060: case 146: /* indexed_opt ::= */ yytestcase(yyruleno==146);
! 110061: {yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
! 110062: break;
! 110063: case 139: /* fullname ::= nm dbnm */
! 110064: {yygotominor.yy259 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
! 110065: break;
! 110066: case 140: /* joinop ::= COMMA|JOIN */
! 110067: { yygotominor.yy4 = JT_INNER; }
! 110068: break;
! 110069: case 141: /* joinop ::= JOIN_KW JOIN */
! 110070: { yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
! 110071: break;
! 110072: case 142: /* joinop ::= JOIN_KW nm JOIN */
! 110073: { yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
! 110074: break;
! 110075: case 143: /* joinop ::= JOIN_KW nm nm JOIN */
! 110076: { yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
! 110077: break;
! 110078: case 144: /* on_opt ::= ON expr */
! 110079: case 155: /* sortitem ::= expr */ yytestcase(yyruleno==155);
! 110080: case 162: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==162);
! 110081: case 169: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==169);
! 110082: case 235: /* case_else ::= ELSE expr */ yytestcase(yyruleno==235);
! 110083: case 237: /* case_operand ::= expr */ yytestcase(yyruleno==237);
! 110084: {yygotominor.yy314 = yymsp[0].minor.yy118.pExpr;}
! 110085: break;
! 110086: case 145: /* on_opt ::= */
! 110087: case 161: /* having_opt ::= */ yytestcase(yyruleno==161);
! 110088: case 168: /* where_opt ::= */ yytestcase(yyruleno==168);
! 110089: case 236: /* case_else ::= */ yytestcase(yyruleno==236);
! 110090: case 238: /* case_operand ::= */ yytestcase(yyruleno==238);
! 110091: {yygotominor.yy314 = 0;}
! 110092: break;
! 110093: case 148: /* indexed_opt ::= NOT INDEXED */
! 110094: {yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
! 110095: break;
! 110096: case 149: /* using_opt ::= USING LP inscollist RP */
! 110097: case 181: /* inscollist_opt ::= LP inscollist RP */ yytestcase(yyruleno==181);
! 110098: {yygotominor.yy384 = yymsp[-1].minor.yy384;}
! 110099: break;
! 110100: case 150: /* using_opt ::= */
! 110101: case 180: /* inscollist_opt ::= */ yytestcase(yyruleno==180);
! 110102: {yygotominor.yy384 = 0;}
! 110103: break;
! 110104: case 152: /* orderby_opt ::= ORDER BY sortlist */
! 110105: case 160: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==160);
! 110106: case 239: /* exprlist ::= nexprlist */ yytestcase(yyruleno==239);
! 110107: {yygotominor.yy322 = yymsp[0].minor.yy322;}
! 110108: break;
! 110109: case 153: /* sortlist ::= sortlist COMMA sortitem sortorder */
! 110110: {
! 110111: yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322,yymsp[-1].minor.yy314);
! 110112: if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
! 110113: }
! 110114: break;
! 110115: case 154: /* sortlist ::= sortitem sortorder */
! 110116: {
! 110117: yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy314);
! 110118: if( yygotominor.yy322 && ALWAYS(yygotominor.yy322->a) ) yygotominor.yy322->a[0].sortOrder = (u8)yymsp[0].minor.yy4;
! 110119: }
! 110120: break;
! 110121: case 156: /* sortorder ::= ASC */
! 110122: case 158: /* sortorder ::= */ yytestcase(yyruleno==158);
! 110123: {yygotominor.yy4 = SQLITE_SO_ASC;}
! 110124: break;
! 110125: case 157: /* sortorder ::= DESC */
! 110126: {yygotominor.yy4 = SQLITE_SO_DESC;}
! 110127: break;
! 110128: case 163: /* limit_opt ::= */
! 110129: {yygotominor.yy292.pLimit = 0; yygotominor.yy292.pOffset = 0;}
! 110130: break;
! 110131: case 164: /* limit_opt ::= LIMIT expr */
! 110132: {yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr; yygotominor.yy292.pOffset = 0;}
! 110133: break;
! 110134: case 165: /* limit_opt ::= LIMIT expr OFFSET expr */
! 110135: {yygotominor.yy292.pLimit = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pOffset = yymsp[0].minor.yy118.pExpr;}
! 110136: break;
! 110137: case 166: /* limit_opt ::= LIMIT expr COMMA expr */
! 110138: {yygotominor.yy292.pOffset = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr;}
! 110139: break;
! 110140: case 167: /* cmd ::= DELETE FROM fullname indexed_opt where_opt */
! 110141: {
! 110142: sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy259, &yymsp[-1].minor.yy0);
! 110143: sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy259,yymsp[0].minor.yy314);
! 110144: }
! 110145: break;
! 110146: case 170: /* cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt */
! 110147: {
! 110148: sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy259, &yymsp[-3].minor.yy0);
! 110149: sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy322,"set list");
! 110150: sqlite3Update(pParse,yymsp[-4].minor.yy259,yymsp[-1].minor.yy322,yymsp[0].minor.yy314,yymsp[-5].minor.yy210);
! 110151: }
! 110152: break;
! 110153: case 171: /* setlist ::= setlist COMMA nm EQ expr */
! 110154: {
! 110155: yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy118.pExpr);
! 110156: sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
! 110157: }
! 110158: break;
! 110159: case 172: /* setlist ::= nm EQ expr */
! 110160: {
! 110161: yygotominor.yy322 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy118.pExpr);
! 110162: sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
! 110163: }
! 110164: break;
! 110165: case 173: /* cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP */
! 110166: {sqlite3Insert(pParse, yymsp[-5].minor.yy259, yymsp[-1].minor.yy322, 0, yymsp[-4].minor.yy384, yymsp[-7].minor.yy210);}
! 110167: break;
! 110168: case 174: /* cmd ::= insert_cmd INTO fullname inscollist_opt select */
! 110169: {sqlite3Insert(pParse, yymsp[-2].minor.yy259, 0, yymsp[0].minor.yy387, yymsp[-1].minor.yy384, yymsp[-4].minor.yy210);}
! 110170: break;
! 110171: case 175: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
! 110172: {sqlite3Insert(pParse, yymsp[-3].minor.yy259, 0, 0, yymsp[-2].minor.yy384, yymsp[-5].minor.yy210);}
! 110173: break;
! 110174: case 176: /* insert_cmd ::= INSERT orconf */
! 110175: {yygotominor.yy210 = yymsp[0].minor.yy210;}
! 110176: break;
! 110177: case 177: /* insert_cmd ::= REPLACE */
! 110178: {yygotominor.yy210 = OE_Replace;}
! 110179: break;
! 110180: case 178: /* itemlist ::= itemlist COMMA expr */
! 110181: case 241: /* nexprlist ::= nexprlist COMMA expr */ yytestcase(yyruleno==241);
! 110182: {yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy118.pExpr);}
! 110183: break;
! 110184: case 179: /* itemlist ::= expr */
! 110185: case 242: /* nexprlist ::= expr */ yytestcase(yyruleno==242);
! 110186: {yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy118.pExpr);}
! 110187: break;
! 110188: case 182: /* inscollist ::= inscollist COMMA nm */
! 110189: {yygotominor.yy384 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy384,&yymsp[0].minor.yy0);}
! 110190: break;
! 110191: case 183: /* inscollist ::= nm */
! 110192: {yygotominor.yy384 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
! 110193: break;
! 110194: case 184: /* expr ::= term */
! 110195: {yygotominor.yy118 = yymsp[0].minor.yy118;}
! 110196: break;
! 110197: case 185: /* expr ::= LP expr RP */
! 110198: {yygotominor.yy118.pExpr = yymsp[-1].minor.yy118.pExpr; spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
! 110199: break;
! 110200: case 186: /* term ::= NULL */
! 110201: case 191: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==191);
! 110202: case 192: /* term ::= STRING */ yytestcase(yyruleno==192);
! 110203: {spanExpr(&yygotominor.yy118, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
! 110204: break;
! 110205: case 187: /* expr ::= id */
! 110206: case 188: /* expr ::= JOIN_KW */ yytestcase(yyruleno==188);
! 110207: {spanExpr(&yygotominor.yy118, pParse, TK_ID, &yymsp[0].minor.yy0);}
! 110208: break;
! 110209: case 189: /* expr ::= nm DOT nm */
! 110210: {
! 110211: Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
! 110212: Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
! 110213: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
! 110214: spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
! 110215: }
! 110216: break;
! 110217: case 190: /* expr ::= nm DOT nm DOT nm */
! 110218: {
! 110219: Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
! 110220: Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
! 110221: Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
! 110222: Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
! 110223: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
! 110224: spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
! 110225: }
! 110226: break;
! 110227: case 193: /* expr ::= REGISTER */
! 110228: {
! 110229: /* When doing a nested parse, one can include terms in an expression
! 110230: ** that look like this: #1 #2 ... These terms refer to registers
! 110231: ** in the virtual machine. #N is the N-th register. */
! 110232: if( pParse->nested==0 ){
! 110233: sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
! 110234: yygotominor.yy118.pExpr = 0;
! 110235: }else{
! 110236: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
! 110237: if( yygotominor.yy118.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy118.pExpr->iTable);
! 110238: }
! 110239: spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
! 110240: }
! 110241: break;
! 110242: case 194: /* expr ::= VARIABLE */
! 110243: {
! 110244: spanExpr(&yygotominor.yy118, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
! 110245: sqlite3ExprAssignVarNumber(pParse, yygotominor.yy118.pExpr);
! 110246: spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
! 110247: }
! 110248: break;
! 110249: case 195: /* expr ::= expr COLLATE ids */
! 110250: {
! 110251: yygotominor.yy118.pExpr = sqlite3ExprSetCollByToken(pParse, yymsp[-2].minor.yy118.pExpr, &yymsp[0].minor.yy0);
! 110252: yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart;
! 110253: yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
! 110254: }
! 110255: break;
! 110256: case 196: /* expr ::= CAST LP expr AS typetoken RP */
! 110257: {
! 110258: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy118.pExpr, 0, &yymsp[-1].minor.yy0);
! 110259: spanSet(&yygotominor.yy118,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
! 110260: }
! 110261: break;
! 110262: case 197: /* expr ::= ID LP distinct exprlist RP */
! 110263: {
! 110264: if( yymsp[-1].minor.yy322 && yymsp[-1].minor.yy322->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
! 110265: sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
! 110266: }
! 110267: yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0);
! 110268: spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
! 110269: if( yymsp[-2].minor.yy4 && yygotominor.yy118.pExpr ){
! 110270: yygotominor.yy118.pExpr->flags |= EP_Distinct;
! 110271: }
! 110272: }
! 110273: break;
! 110274: case 198: /* expr ::= ID LP STAR RP */
! 110275: {
! 110276: yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
! 110277: spanSet(&yygotominor.yy118,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
! 110278: }
! 110279: break;
! 110280: case 199: /* term ::= CTIME_KW */
! 110281: {
! 110282: /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
! 110283: ** treated as functions that return constants */
! 110284: yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0,&yymsp[0].minor.yy0);
! 110285: if( yygotominor.yy118.pExpr ){
! 110286: yygotominor.yy118.pExpr->op = TK_CONST_FUNC;
! 110287: }
! 110288: spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
! 110289: }
! 110290: break;
! 110291: case 200: /* expr ::= expr AND expr */
! 110292: case 201: /* expr ::= expr OR expr */ yytestcase(yyruleno==201);
! 110293: case 202: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==202);
! 110294: case 203: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==203);
! 110295: case 204: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==204);
! 110296: case 205: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==205);
! 110297: case 206: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==206);
! 110298: case 207: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==207);
! 110299: {spanBinaryExpr(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy118);}
! 110300: break;
! 110301: case 208: /* likeop ::= LIKE_KW */
! 110302: case 210: /* likeop ::= MATCH */ yytestcase(yyruleno==210);
! 110303: {yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.not = 0;}
! 110304: break;
! 110305: case 209: /* likeop ::= NOT LIKE_KW */
! 110306: case 211: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==211);
! 110307: {yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.not = 1;}
! 110308: break;
! 110309: case 212: /* expr ::= expr likeop expr */
! 110310: {
! 110311: ExprList *pList;
! 110312: pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy118.pExpr);
! 110313: pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy118.pExpr);
! 110314: yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy342.eOperator);
! 110315: if( yymsp[-1].minor.yy342.not ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
! 110316: yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart;
! 110317: yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
! 110318: if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc;
! 110319: }
! 110320: break;
! 110321: case 213: /* expr ::= expr likeop expr ESCAPE expr */
! 110322: {
! 110323: ExprList *pList;
! 110324: pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
! 110325: pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy118.pExpr);
! 110326: pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr);
! 110327: yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy342.eOperator);
! 110328: if( yymsp[-3].minor.yy342.not ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
! 110329: yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
! 110330: yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
! 110331: if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc;
! 110332: }
! 110333: break;
! 110334: case 214: /* expr ::= expr ISNULL|NOTNULL */
! 110335: {spanUnaryPostfix(&yygotominor.yy118,pParse,yymsp[0].major,&yymsp[-1].minor.yy118,&yymsp[0].minor.yy0);}
! 110336: break;
! 110337: case 215: /* expr ::= expr NOT NULL */
! 110338: {spanUnaryPostfix(&yygotominor.yy118,pParse,TK_NOTNULL,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy0);}
! 110339: break;
! 110340: case 216: /* expr ::= expr IS expr */
! 110341: {
! 110342: spanBinaryExpr(&yygotominor.yy118,pParse,TK_IS,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy118);
! 110343: binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExpr, TK_ISNULL);
! 110344: }
! 110345: break;
! 110346: case 217: /* expr ::= expr IS NOT expr */
! 110347: {
! 110348: spanBinaryExpr(&yygotominor.yy118,pParse,TK_ISNOT,&yymsp[-3].minor.yy118,&yymsp[0].minor.yy118);
! 110349: binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExpr, TK_NOTNULL);
! 110350: }
! 110351: break;
! 110352: case 218: /* expr ::= NOT expr */
! 110353: case 219: /* expr ::= BITNOT expr */ yytestcase(yyruleno==219);
! 110354: {spanUnaryPrefix(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
! 110355: break;
! 110356: case 220: /* expr ::= MINUS expr */
! 110357: {spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UMINUS,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
! 110358: break;
! 110359: case 221: /* expr ::= PLUS expr */
! 110360: {spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UPLUS,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
! 110361: break;
! 110362: case 224: /* expr ::= expr between_op expr AND expr */
! 110363: {
! 110364: ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
! 110365: pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr);
! 110366: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy118.pExpr, 0, 0);
! 110367: if( yygotominor.yy118.pExpr ){
! 110368: yygotominor.yy118.pExpr->x.pList = pList;
! 110369: }else{
! 110370: sqlite3ExprListDelete(pParse->db, pList);
! 110371: }
! 110372: if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
! 110373: yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
! 110374: yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
! 110375: }
! 110376: break;
! 110377: case 227: /* expr ::= expr in_op LP exprlist RP */
! 110378: {
! 110379: if( yymsp[-1].minor.yy322==0 ){
! 110380: /* Expressions of the form
! 110381: **
! 110382: ** expr1 IN ()
! 110383: ** expr1 NOT IN ()
! 110384: **
! 110385: ** simplify to constants 0 (false) and 1 (true), respectively,
! 110386: ** regardless of the value of expr1.
! 110387: */
! 110388: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy4]);
! 110389: sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy118.pExpr);
! 110390: }else{
! 110391: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118.pExpr, 0, 0);
! 110392: if( yygotominor.yy118.pExpr ){
! 110393: yygotominor.yy118.pExpr->x.pList = yymsp[-1].minor.yy322;
! 110394: sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
! 110395: }else{
! 110396: sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
! 110397: }
! 110398: if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
! 110399: }
! 110400: yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
! 110401: yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
! 110402: }
! 110403: break;
! 110404: case 228: /* expr ::= LP select RP */
! 110405: {
! 110406: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
! 110407: if( yygotominor.yy118.pExpr ){
! 110408: yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387;
! 110409: ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
! 110410: sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
! 110411: }else{
! 110412: sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
! 110413: }
! 110414: yygotominor.yy118.zStart = yymsp[-2].minor.yy0.z;
! 110415: yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
! 110416: }
! 110417: break;
! 110418: case 229: /* expr ::= expr in_op LP select RP */
! 110419: {
! 110420: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118.pExpr, 0, 0);
! 110421: if( yygotominor.yy118.pExpr ){
! 110422: yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387;
! 110423: ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
! 110424: sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
! 110425: }else{
! 110426: sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
! 110427: }
! 110428: if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
! 110429: yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
! 110430: yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
! 110431: }
! 110432: break;
! 110433: case 230: /* expr ::= expr in_op nm dbnm */
! 110434: {
! 110435: SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
! 110436: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy118.pExpr, 0, 0);
! 110437: if( yygotominor.yy118.pExpr ){
! 110438: yygotominor.yy118.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
! 110439: ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
! 110440: sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
! 110441: }else{
! 110442: sqlite3SrcListDelete(pParse->db, pSrc);
! 110443: }
! 110444: if( yymsp[-2].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
! 110445: yygotominor.yy118.zStart = yymsp[-3].minor.yy118.zStart;
! 110446: 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];
! 110447: }
! 110448: break;
! 110449: case 231: /* expr ::= EXISTS LP select RP */
! 110450: {
! 110451: Expr *p = yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
! 110452: if( p ){
! 110453: p->x.pSelect = yymsp[-1].minor.yy387;
! 110454: ExprSetProperty(p, EP_xIsSelect);
! 110455: sqlite3ExprSetHeight(pParse, p);
! 110456: }else{
! 110457: sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
! 110458: }
! 110459: yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z;
! 110460: yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
! 110461: }
! 110462: break;
! 110463: case 232: /* expr ::= CASE case_operand case_exprlist case_else END */
! 110464: {
! 110465: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy314, yymsp[-1].minor.yy314, 0);
! 110466: if( yygotominor.yy118.pExpr ){
! 110467: yygotominor.yy118.pExpr->x.pList = yymsp[-2].minor.yy322;
! 110468: sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
! 110469: }else{
! 110470: sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
! 110471: }
! 110472: yygotominor.yy118.zStart = yymsp[-4].minor.yy0.z;
! 110473: yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
! 110474: }
! 110475: break;
! 110476: case 233: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
! 110477: {
! 110478: yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy118.pExpr);
! 110479: yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].minor.yy118.pExpr);
! 110480: }
! 110481: break;
! 110482: case 234: /* case_exprlist ::= WHEN expr THEN expr */
! 110483: {
! 110484: yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
! 110485: yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].minor.yy118.pExpr);
! 110486: }
! 110487: break;
! 110488: case 243: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP */
! 110489: {
! 110490: sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy0, &yymsp[-5].minor.yy0,
! 110491: sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy0,0), yymsp[-1].minor.yy322, yymsp[-9].minor.yy4,
! 110492: &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy4);
! 110493: }
! 110494: break;
! 110495: case 244: /* uniqueflag ::= UNIQUE */
! 110496: case 298: /* raisetype ::= ABORT */ yytestcase(yyruleno==298);
! 110497: {yygotominor.yy4 = OE_Abort;}
! 110498: break;
! 110499: case 245: /* uniqueflag ::= */
! 110500: {yygotominor.yy4 = OE_None;}
! 110501: break;
! 110502: case 248: /* idxlist ::= idxlist COMMA nm collate sortorder */
! 110503: {
! 110504: Expr *p = 0;
! 110505: if( yymsp[-1].minor.yy0.n>0 ){
! 110506: p = sqlite3Expr(pParse->db, TK_COLUMN, 0);
! 110507: sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
! 110508: }
! 110509: yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, p);
! 110510: sqlite3ExprListSetName(pParse,yygotominor.yy322,&yymsp[-2].minor.yy0,1);
! 110511: sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index");
! 110512: if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
! 110513: }
! 110514: break;
! 110515: case 249: /* idxlist ::= nm collate sortorder */
! 110516: {
! 110517: Expr *p = 0;
! 110518: if( yymsp[-1].minor.yy0.n>0 ){
! 110519: p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
! 110520: sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
! 110521: }
! 110522: yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, p);
! 110523: sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
! 110524: sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index");
! 110525: if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
! 110526: }
! 110527: break;
! 110528: case 250: /* collate ::= */
! 110529: {yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
! 110530: break;
! 110531: case 252: /* cmd ::= DROP INDEX ifexists fullname */
! 110532: {sqlite3DropIndex(pParse, yymsp[0].minor.yy259, yymsp[-1].minor.yy4);}
! 110533: break;
! 110534: case 253: /* cmd ::= VACUUM */
! 110535: case 254: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==254);
! 110536: {sqlite3Vacuum(pParse);}
! 110537: break;
! 110538: case 255: /* cmd ::= PRAGMA nm dbnm */
! 110539: {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
! 110540: break;
! 110541: case 256: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
! 110542: {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
! 110543: break;
! 110544: case 257: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
! 110545: {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
! 110546: break;
! 110547: case 258: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
! 110548: {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
! 110549: break;
! 110550: case 259: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
! 110551: {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
! 110552: break;
! 110553: case 270: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
! 110554: {
! 110555: Token all;
! 110556: all.z = yymsp[-3].minor.yy0.z;
! 110557: all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
! 110558: sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy203, &all);
! 110559: }
! 110560: break;
! 110561: case 271: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
! 110562: {
! 110563: 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);
! 110564: yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
! 110565: }
! 110566: break;
! 110567: case 272: /* trigger_time ::= BEFORE */
! 110568: case 275: /* trigger_time ::= */ yytestcase(yyruleno==275);
! 110569: { yygotominor.yy4 = TK_BEFORE; }
! 110570: break;
! 110571: case 273: /* trigger_time ::= AFTER */
! 110572: { yygotominor.yy4 = TK_AFTER; }
! 110573: break;
! 110574: case 274: /* trigger_time ::= INSTEAD OF */
! 110575: { yygotominor.yy4 = TK_INSTEAD;}
! 110576: break;
! 110577: case 276: /* trigger_event ::= DELETE|INSERT */
! 110578: case 277: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==277);
! 110579: {yygotominor.yy90.a = yymsp[0].major; yygotominor.yy90.b = 0;}
! 110580: break;
! 110581: case 278: /* trigger_event ::= UPDATE OF inscollist */
! 110582: {yygotominor.yy90.a = TK_UPDATE; yygotominor.yy90.b = yymsp[0].minor.yy384;}
! 110583: break;
! 110584: case 281: /* when_clause ::= */
! 110585: case 303: /* key_opt ::= */ yytestcase(yyruleno==303);
! 110586: { yygotominor.yy314 = 0; }
! 110587: break;
! 110588: case 282: /* when_clause ::= WHEN expr */
! 110589: case 304: /* key_opt ::= KEY expr */ yytestcase(yyruleno==304);
! 110590: { yygotominor.yy314 = yymsp[0].minor.yy118.pExpr; }
! 110591: break;
! 110592: case 283: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
! 110593: {
! 110594: assert( yymsp[-2].minor.yy203!=0 );
! 110595: yymsp[-2].minor.yy203->pLast->pNext = yymsp[-1].minor.yy203;
! 110596: yymsp[-2].minor.yy203->pLast = yymsp[-1].minor.yy203;
! 110597: yygotominor.yy203 = yymsp[-2].minor.yy203;
! 110598: }
! 110599: break;
! 110600: case 284: /* trigger_cmd_list ::= trigger_cmd SEMI */
! 110601: {
! 110602: assert( yymsp[-1].minor.yy203!=0 );
! 110603: yymsp[-1].minor.yy203->pLast = yymsp[-1].minor.yy203;
! 110604: yygotominor.yy203 = yymsp[-1].minor.yy203;
! 110605: }
! 110606: break;
! 110607: case 286: /* trnm ::= nm DOT nm */
! 110608: {
! 110609: yygotominor.yy0 = yymsp[0].minor.yy0;
! 110610: sqlite3ErrorMsg(pParse,
! 110611: "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
! 110612: "statements within triggers");
! 110613: }
! 110614: break;
! 110615: case 288: /* tridxby ::= INDEXED BY nm */
! 110616: {
! 110617: sqlite3ErrorMsg(pParse,
! 110618: "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
! 110619: "within triggers");
! 110620: }
! 110621: break;
! 110622: case 289: /* tridxby ::= NOT INDEXED */
! 110623: {
! 110624: sqlite3ErrorMsg(pParse,
! 110625: "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
! 110626: "within triggers");
! 110627: }
! 110628: break;
! 110629: case 290: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
! 110630: { yygotominor.yy203 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy322, yymsp[0].minor.yy314, yymsp[-5].minor.yy210); }
! 110631: break;
! 110632: case 291: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP */
! 110633: {yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy384, yymsp[-1].minor.yy322, 0, yymsp[-7].minor.yy210);}
! 110634: break;
! 110635: case 292: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
! 110636: {yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy384, 0, yymsp[0].minor.yy387, yymsp[-4].minor.yy210);}
! 110637: break;
! 110638: case 293: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
! 110639: {yygotominor.yy203 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy314);}
! 110640: break;
! 110641: case 294: /* trigger_cmd ::= select */
! 110642: {yygotominor.yy203 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy387); }
! 110643: break;
! 110644: case 295: /* expr ::= RAISE LP IGNORE RP */
! 110645: {
! 110646: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
! 110647: if( yygotominor.yy118.pExpr ){
! 110648: yygotominor.yy118.pExpr->affinity = OE_Ignore;
! 110649: }
! 110650: yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z;
! 110651: yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
! 110652: }
! 110653: break;
! 110654: case 296: /* expr ::= RAISE LP raisetype COMMA nm RP */
! 110655: {
! 110656: yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0);
! 110657: if( yygotominor.yy118.pExpr ) {
! 110658: yygotominor.yy118.pExpr->affinity = (char)yymsp[-3].minor.yy4;
! 110659: }
! 110660: yygotominor.yy118.zStart = yymsp[-5].minor.yy0.z;
! 110661: yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
! 110662: }
! 110663: break;
! 110664: case 297: /* raisetype ::= ROLLBACK */
! 110665: {yygotominor.yy4 = OE_Rollback;}
! 110666: break;
! 110667: case 299: /* raisetype ::= FAIL */
! 110668: {yygotominor.yy4 = OE_Fail;}
! 110669: break;
! 110670: case 300: /* cmd ::= DROP TRIGGER ifexists fullname */
! 110671: {
! 110672: sqlite3DropTrigger(pParse,yymsp[0].minor.yy259,yymsp[-1].minor.yy4);
! 110673: }
! 110674: break;
! 110675: case 301: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
! 110676: {
! 110677: sqlite3Attach(pParse, yymsp[-3].minor.yy118.pExpr, yymsp[-1].minor.yy118.pExpr, yymsp[0].minor.yy314);
! 110678: }
! 110679: break;
! 110680: case 302: /* cmd ::= DETACH database_kw_opt expr */
! 110681: {
! 110682: sqlite3Detach(pParse, yymsp[0].minor.yy118.pExpr);
! 110683: }
! 110684: break;
! 110685: case 307: /* cmd ::= REINDEX */
! 110686: {sqlite3Reindex(pParse, 0, 0);}
! 110687: break;
! 110688: case 308: /* cmd ::= REINDEX nm dbnm */
! 110689: {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
! 110690: break;
! 110691: case 309: /* cmd ::= ANALYZE */
! 110692: {sqlite3Analyze(pParse, 0, 0);}
! 110693: break;
! 110694: case 310: /* cmd ::= ANALYZE nm dbnm */
! 110695: {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
! 110696: break;
! 110697: case 311: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
! 110698: {
! 110699: sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy259,&yymsp[0].minor.yy0);
! 110700: }
! 110701: break;
! 110702: case 312: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
! 110703: {
! 110704: sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
! 110705: }
! 110706: break;
! 110707: case 313: /* add_column_fullname ::= fullname */
! 110708: {
! 110709: pParse->db->lookaside.bEnabled = 0;
! 110710: sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy259);
! 110711: }
! 110712: break;
! 110713: case 316: /* cmd ::= create_vtab */
! 110714: {sqlite3VtabFinishParse(pParse,0);}
! 110715: break;
! 110716: case 317: /* cmd ::= create_vtab LP vtabarglist RP */
! 110717: {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
! 110718: break;
! 110719: case 318: /* create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm */
! 110720: {
! 110721: sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
! 110722: }
! 110723: break;
! 110724: case 321: /* vtabarg ::= */
! 110725: {sqlite3VtabArgInit(pParse);}
! 110726: break;
! 110727: case 323: /* vtabargtoken ::= ANY */
! 110728: case 324: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==324);
! 110729: case 325: /* lp ::= LP */ yytestcase(yyruleno==325);
! 110730: {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
! 110731: break;
! 110732: default:
! 110733: /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
! 110734: /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
! 110735: /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
! 110736: /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
! 110737: /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
! 110738: /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
! 110739: /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
! 110740: /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
! 110741: /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
! 110742: /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
! 110743: /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
! 110744: /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34);
! 110745: /* (35) columnlist ::= column */ yytestcase(yyruleno==35);
! 110746: /* (44) type ::= */ yytestcase(yyruleno==44);
! 110747: /* (51) signed ::= plus_num */ yytestcase(yyruleno==51);
! 110748: /* (52) signed ::= minus_num */ yytestcase(yyruleno==52);
! 110749: /* (53) carglist ::= carglist carg */ yytestcase(yyruleno==53);
! 110750: /* (54) carglist ::= */ yytestcase(yyruleno==54);
! 110751: /* (55) carg ::= CONSTRAINT nm ccons */ yytestcase(yyruleno==55);
! 110752: /* (56) carg ::= ccons */ yytestcase(yyruleno==56);
! 110753: /* (62) ccons ::= NULL onconf */ yytestcase(yyruleno==62);
! 110754: /* (90) conslist ::= conslist COMMA tcons */ yytestcase(yyruleno==90);
! 110755: /* (91) conslist ::= conslist tcons */ yytestcase(yyruleno==91);
! 110756: /* (92) conslist ::= tcons */ yytestcase(yyruleno==92);
! 110757: /* (93) tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==93);
! 110758: /* (268) plus_opt ::= PLUS */ yytestcase(yyruleno==268);
! 110759: /* (269) plus_opt ::= */ yytestcase(yyruleno==269);
! 110760: /* (279) foreach_clause ::= */ yytestcase(yyruleno==279);
! 110761: /* (280) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==280);
! 110762: /* (287) tridxby ::= */ yytestcase(yyruleno==287);
! 110763: /* (305) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==305);
! 110764: /* (306) database_kw_opt ::= */ yytestcase(yyruleno==306);
! 110765: /* (314) kwcolumn_opt ::= */ yytestcase(yyruleno==314);
! 110766: /* (315) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==315);
! 110767: /* (319) vtabarglist ::= vtabarg */ yytestcase(yyruleno==319);
! 110768: /* (320) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==320);
! 110769: /* (322) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==322);
! 110770: /* (326) anylist ::= */ yytestcase(yyruleno==326);
! 110771: /* (327) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==327);
! 110772: /* (328) anylist ::= anylist ANY */ yytestcase(yyruleno==328);
! 110773: break;
! 110774: };
! 110775: yygoto = yyRuleInfo[yyruleno].lhs;
! 110776: yysize = yyRuleInfo[yyruleno].nrhs;
! 110777: yypParser->yyidx -= yysize;
! 110778: yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
! 110779: if( yyact < YYNSTATE ){
! 110780: #ifdef NDEBUG
! 110781: /* If we are not debugging and the reduce action popped at least
! 110782: ** one element off the stack, then we can push the new element back
! 110783: ** onto the stack here, and skip the stack overflow test in yy_shift().
! 110784: ** That gives a significant speed improvement. */
! 110785: if( yysize ){
! 110786: yypParser->yyidx++;
! 110787: yymsp -= yysize-1;
! 110788: yymsp->stateno = (YYACTIONTYPE)yyact;
! 110789: yymsp->major = (YYCODETYPE)yygoto;
! 110790: yymsp->minor = yygotominor;
! 110791: }else
! 110792: #endif
! 110793: {
! 110794: yy_shift(yypParser,yyact,yygoto,&yygotominor);
! 110795: }
! 110796: }else{
! 110797: assert( yyact == YYNSTATE + YYNRULE + 1 );
! 110798: yy_accept(yypParser);
! 110799: }
! 110800: }
! 110801:
! 110802: /*
! 110803: ** The following code executes when the parse fails
! 110804: */
! 110805: #ifndef YYNOERRORRECOVERY
! 110806: static void yy_parse_failed(
! 110807: yyParser *yypParser /* The parser */
! 110808: ){
! 110809: sqlite3ParserARG_FETCH;
! 110810: #ifndef NDEBUG
! 110811: if( yyTraceFILE ){
! 110812: fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
! 110813: }
! 110814: #endif
! 110815: while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
! 110816: /* Here code is inserted which will be executed whenever the
! 110817: ** parser fails */
! 110818: sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
! 110819: }
! 110820: #endif /* YYNOERRORRECOVERY */
! 110821:
! 110822: /*
! 110823: ** The following code executes when a syntax error first occurs.
! 110824: */
! 110825: static void yy_syntax_error(
! 110826: yyParser *yypParser, /* The parser */
! 110827: int yymajor, /* The major type of the error token */
! 110828: YYMINORTYPE yyminor /* The minor type of the error token */
! 110829: ){
! 110830: sqlite3ParserARG_FETCH;
! 110831: #define TOKEN (yyminor.yy0)
! 110832:
! 110833: UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
! 110834: assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */
! 110835: sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
! 110836: sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
! 110837: }
! 110838:
! 110839: /*
! 110840: ** The following is executed when the parser accepts
! 110841: */
! 110842: static void yy_accept(
! 110843: yyParser *yypParser /* The parser */
! 110844: ){
! 110845: sqlite3ParserARG_FETCH;
! 110846: #ifndef NDEBUG
! 110847: if( yyTraceFILE ){
! 110848: fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
! 110849: }
! 110850: #endif
! 110851: while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
! 110852: /* Here code is inserted which will be executed whenever the
! 110853: ** parser accepts */
! 110854: sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
! 110855: }
! 110856:
! 110857: /* The main parser program.
! 110858: ** The first argument is a pointer to a structure obtained from
! 110859: ** "sqlite3ParserAlloc" which describes the current state of the parser.
! 110860: ** The second argument is the major token number. The third is
! 110861: ** the minor token. The fourth optional argument is whatever the
! 110862: ** user wants (and specified in the grammar) and is available for
! 110863: ** use by the action routines.
! 110864: **
! 110865: ** Inputs:
! 110866: ** <ul>
! 110867: ** <li> A pointer to the parser (an opaque structure.)
! 110868: ** <li> The major token number.
! 110869: ** <li> The minor token number.
! 110870: ** <li> An option argument of a grammar-specified type.
! 110871: ** </ul>
! 110872: **
! 110873: ** Outputs:
! 110874: ** None.
! 110875: */
! 110876: SQLITE_PRIVATE void sqlite3Parser(
! 110877: void *yyp, /* The parser */
! 110878: int yymajor, /* The major token code number */
! 110879: sqlite3ParserTOKENTYPE yyminor /* The value for the token */
! 110880: sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
! 110881: ){
! 110882: YYMINORTYPE yyminorunion;
! 110883: int yyact; /* The parser action. */
! 110884: #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
! 110885: int yyendofinput; /* True if we are at the end of input */
! 110886: #endif
! 110887: #ifdef YYERRORSYMBOL
! 110888: int yyerrorhit = 0; /* True if yymajor has invoked an error */
! 110889: #endif
! 110890: yyParser *yypParser; /* The parser */
! 110891:
! 110892: /* (re)initialize the parser, if necessary */
! 110893: yypParser = (yyParser*)yyp;
! 110894: if( yypParser->yyidx<0 ){
! 110895: #if YYSTACKDEPTH<=0
! 110896: if( yypParser->yystksz <=0 ){
! 110897: /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
! 110898: yyminorunion = yyzerominor;
! 110899: yyStackOverflow(yypParser, &yyminorunion);
! 110900: return;
! 110901: }
! 110902: #endif
! 110903: yypParser->yyidx = 0;
! 110904: yypParser->yyerrcnt = -1;
! 110905: yypParser->yystack[0].stateno = 0;
! 110906: yypParser->yystack[0].major = 0;
! 110907: }
! 110908: yyminorunion.yy0 = yyminor;
! 110909: #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
! 110910: yyendofinput = (yymajor==0);
! 110911: #endif
! 110912: sqlite3ParserARG_STORE;
! 110913:
! 110914: #ifndef NDEBUG
! 110915: if( yyTraceFILE ){
! 110916: fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
! 110917: }
! 110918: #endif
! 110919:
! 110920: do{
! 110921: yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
! 110922: if( yyact<YYNSTATE ){
! 110923: yy_shift(yypParser,yyact,yymajor,&yyminorunion);
! 110924: yypParser->yyerrcnt--;
! 110925: yymajor = YYNOCODE;
! 110926: }else if( yyact < YYNSTATE + YYNRULE ){
! 110927: yy_reduce(yypParser,yyact-YYNSTATE);
! 110928: }else{
! 110929: assert( yyact == YY_ERROR_ACTION );
! 110930: #ifdef YYERRORSYMBOL
! 110931: int yymx;
! 110932: #endif
! 110933: #ifndef NDEBUG
! 110934: if( yyTraceFILE ){
! 110935: fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
! 110936: }
! 110937: #endif
! 110938: #ifdef YYERRORSYMBOL
! 110939: /* A syntax error has occurred.
! 110940: ** The response to an error depends upon whether or not the
! 110941: ** grammar defines an error token "ERROR".
! 110942: **
! 110943: ** This is what we do if the grammar does define ERROR:
! 110944: **
! 110945: ** * Call the %syntax_error function.
! 110946: **
! 110947: ** * Begin popping the stack until we enter a state where
! 110948: ** it is legal to shift the error symbol, then shift
! 110949: ** the error symbol.
! 110950: **
! 110951: ** * Set the error count to three.
! 110952: **
! 110953: ** * Begin accepting and shifting new tokens. No new error
! 110954: ** processing will occur until three tokens have been
! 110955: ** shifted successfully.
! 110956: **
! 110957: */
! 110958: if( yypParser->yyerrcnt<0 ){
! 110959: yy_syntax_error(yypParser,yymajor,yyminorunion);
! 110960: }
! 110961: yymx = yypParser->yystack[yypParser->yyidx].major;
! 110962: if( yymx==YYERRORSYMBOL || yyerrorhit ){
! 110963: #ifndef NDEBUG
! 110964: if( yyTraceFILE ){
! 110965: fprintf(yyTraceFILE,"%sDiscard input token %s\n",
! 110966: yyTracePrompt,yyTokenName[yymajor]);
! 110967: }
! 110968: #endif
! 110969: yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
! 110970: yymajor = YYNOCODE;
! 110971: }else{
! 110972: while(
! 110973: yypParser->yyidx >= 0 &&
! 110974: yymx != YYERRORSYMBOL &&
! 110975: (yyact = yy_find_reduce_action(
! 110976: yypParser->yystack[yypParser->yyidx].stateno,
! 110977: YYERRORSYMBOL)) >= YYNSTATE
! 110978: ){
! 110979: yy_pop_parser_stack(yypParser);
! 110980: }
! 110981: if( yypParser->yyidx < 0 || yymajor==0 ){
! 110982: yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
! 110983: yy_parse_failed(yypParser);
! 110984: yymajor = YYNOCODE;
! 110985: }else if( yymx!=YYERRORSYMBOL ){
! 110986: YYMINORTYPE u2;
! 110987: u2.YYERRSYMDT = 0;
! 110988: yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
! 110989: }
! 110990: }
! 110991: yypParser->yyerrcnt = 3;
! 110992: yyerrorhit = 1;
! 110993: #elif defined(YYNOERRORRECOVERY)
! 110994: /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
! 110995: ** do any kind of error recovery. Instead, simply invoke the syntax
! 110996: ** error routine and continue going as if nothing had happened.
! 110997: **
! 110998: ** Applications can set this macro (for example inside %include) if
! 110999: ** they intend to abandon the parse upon the first syntax error seen.
! 111000: */
! 111001: yy_syntax_error(yypParser,yymajor,yyminorunion);
! 111002: yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
! 111003: yymajor = YYNOCODE;
! 111004:
! 111005: #else /* YYERRORSYMBOL is not defined */
! 111006: /* This is what we do if the grammar does not define ERROR:
! 111007: **
! 111008: ** * Report an error message, and throw away the input token.
! 111009: **
! 111010: ** * If the input token is $, then fail the parse.
! 111011: **
! 111012: ** As before, subsequent error messages are suppressed until
! 111013: ** three input tokens have been successfully shifted.
! 111014: */
! 111015: if( yypParser->yyerrcnt<=0 ){
! 111016: yy_syntax_error(yypParser,yymajor,yyminorunion);
! 111017: }
! 111018: yypParser->yyerrcnt = 3;
! 111019: yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
! 111020: if( yyendofinput ){
! 111021: yy_parse_failed(yypParser);
! 111022: }
! 111023: yymajor = YYNOCODE;
! 111024: #endif
! 111025: }
! 111026: }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
! 111027: return;
! 111028: }
! 111029:
! 111030: /************** End of parse.c ***********************************************/
! 111031: /************** Begin file tokenize.c ****************************************/
! 111032: /*
! 111033: ** 2001 September 15
! 111034: **
! 111035: ** The author disclaims copyright to this source code. In place of
! 111036: ** a legal notice, here is a blessing:
! 111037: **
! 111038: ** May you do good and not evil.
! 111039: ** May you find forgiveness for yourself and forgive others.
! 111040: ** May you share freely, never taking more than you give.
! 111041: **
! 111042: *************************************************************************
! 111043: ** An tokenizer for SQL
! 111044: **
! 111045: ** This file contains C code that splits an SQL input string up into
! 111046: ** individual tokens and sends those tokens one-by-one over to the
! 111047: ** parser for analysis.
! 111048: */
! 111049: /* #include <stdlib.h> */
! 111050:
! 111051: /*
! 111052: ** The charMap() macro maps alphabetic characters into their
! 111053: ** lower-case ASCII equivalent. On ASCII machines, this is just
! 111054: ** an upper-to-lower case map. On EBCDIC machines we also need
! 111055: ** to adjust the encoding. Only alphabetic characters and underscores
! 111056: ** need to be translated.
! 111057: */
! 111058: #ifdef SQLITE_ASCII
! 111059: # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
! 111060: #endif
! 111061: #ifdef SQLITE_EBCDIC
! 111062: # define charMap(X) ebcdicToAscii[(unsigned char)X]
! 111063: const unsigned char ebcdicToAscii[] = {
! 111064: /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
! 111065: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
! 111066: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
! 111067: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
! 111068: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
! 111069: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
! 111070: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
! 111071: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
! 111072: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
! 111073: 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
! 111074: 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
! 111075: 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
! 111076: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
! 111077: 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
! 111078: 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
! 111079: 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
! 111080: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
! 111081: };
! 111082: #endif
! 111083:
! 111084: /*
! 111085: ** The sqlite3KeywordCode function looks up an identifier to determine if
! 111086: ** it is a keyword. If it is a keyword, the token code of that keyword is
! 111087: ** returned. If the input is not a keyword, TK_ID is returned.
! 111088: **
! 111089: ** The implementation of this routine was generated by a program,
! 111090: ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
! 111091: ** The output of the mkkeywordhash.c program is written into a file
! 111092: ** named keywordhash.h and then included into this source file by
! 111093: ** the #include below.
! 111094: */
! 111095: /************** Include keywordhash.h in the middle of tokenize.c ************/
! 111096: /************** Begin file keywordhash.h *************************************/
! 111097: /***** This file contains automatically generated code ******
! 111098: **
! 111099: ** The code in this file has been automatically generated by
! 111100: **
! 111101: ** sqlite/tool/mkkeywordhash.c
! 111102: **
! 111103: ** The code in this file implements a function that determines whether
! 111104: ** or not a given identifier is really an SQL keyword. The same thing
! 111105: ** might be implemented more directly using a hand-written hash table.
! 111106: ** But by using this automatically generated code, the size of the code
! 111107: ** is substantially reduced. This is important for embedded applications
! 111108: ** on platforms with limited memory.
! 111109: */
! 111110: /* Hash score: 175 */
! 111111: static int keywordCode(const char *z, int n){
! 111112: /* zText[] encodes 811 bytes of keywords in 541 bytes */
! 111113: /* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */
! 111114: /* ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE */
! 111115: /* XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY */
! 111116: /* UNIQUERYATTACHAVINGROUPDATEBEGINNERELEASEBETWEENOTNULLIKE */
! 111117: /* CASCADELETECASECOLLATECREATECURRENT_DATEDETACHIMMEDIATEJOIN */
! 111118: /* SERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHENWHERENAME */
! 111119: /* AFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSS */
! 111120: /* CURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAILFROMFULLGLOBYIF */
! 111121: /* ISNULLORDERESTRICTOUTERIGHTROLLBACKROWUNIONUSINGVACUUMVIEW */
! 111122: /* INITIALLY */
! 111123: static const char zText[540] = {
! 111124: 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
! 111125: 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
! 111126: 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
! 111127: 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
! 111128: 'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
! 111129: 'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
! 111130: 'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
! 111131: 'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
! 111132: 'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
! 111133: 'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
! 111134: 'U','E','R','Y','A','T','T','A','C','H','A','V','I','N','G','R','O','U',
! 111135: 'P','D','A','T','E','B','E','G','I','N','N','E','R','E','L','E','A','S',
! 111136: 'E','B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C',
! 111137: 'A','S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L',
! 111138: 'A','T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D',
! 111139: 'A','T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E',
! 111140: 'J','O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A',
! 111141: 'L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U',
! 111142: 'E','S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W',
! 111143: 'H','E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C',
! 111144: 'E','A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R',
! 111145: 'E','M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M',
! 111146: 'M','I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U',
! 111147: 'R','R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M',
! 111148: 'A','R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T',
! 111149: 'D','R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L',
! 111150: 'O','B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S',
! 111151: 'T','R','I','C','T','O','U','T','E','R','I','G','H','T','R','O','L','L',
! 111152: 'B','A','C','K','R','O','W','U','N','I','O','N','U','S','I','N','G','V',
! 111153: 'A','C','U','U','M','V','I','E','W','I','N','I','T','I','A','L','L','Y',
! 111154: };
! 111155: static const unsigned char aHash[127] = {
! 111156: 72, 101, 114, 70, 0, 45, 0, 0, 78, 0, 73, 0, 0,
! 111157: 42, 12, 74, 15, 0, 113, 81, 50, 108, 0, 19, 0, 0,
! 111158: 118, 0, 116, 111, 0, 22, 89, 0, 9, 0, 0, 66, 67,
! 111159: 0, 65, 6, 0, 48, 86, 98, 0, 115, 97, 0, 0, 44,
! 111160: 0, 99, 24, 0, 17, 0, 119, 49, 23, 0, 5, 106, 25,
! 111161: 92, 0, 0, 121, 102, 56, 120, 53, 28, 51, 0, 87, 0,
! 111162: 96, 26, 0, 95, 0, 0, 0, 91, 88, 93, 84, 105, 14,
! 111163: 39, 104, 0, 77, 0, 18, 85, 107, 32, 0, 117, 76, 109,
! 111164: 58, 46, 80, 0, 0, 90, 40, 0, 112, 0, 36, 0, 0,
! 111165: 29, 0, 82, 59, 60, 0, 20, 57, 0, 52,
! 111166: };
! 111167: static const unsigned char aNext[121] = {
! 111168: 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0,
! 111169: 0, 2, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0,
! 111170: 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
! 111171: 0, 0, 0, 0, 33, 0, 21, 0, 0, 0, 43, 3, 47,
! 111172: 0, 0, 0, 0, 30, 0, 54, 0, 38, 0, 0, 0, 1,
! 111173: 62, 0, 0, 63, 0, 41, 0, 0, 0, 0, 0, 0, 0,
! 111174: 61, 0, 0, 0, 0, 31, 55, 16, 34, 10, 0, 0, 0,
! 111175: 0, 0, 0, 0, 11, 68, 75, 0, 8, 0, 100, 94, 0,
! 111176: 103, 0, 83, 0, 71, 0, 0, 110, 27, 37, 69, 79, 0,
! 111177: 35, 64, 0, 0,
! 111178: };
! 111179: static const unsigned char aLen[121] = {
! 111180: 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6,
! 111181: 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 6,
! 111182: 11, 6, 2, 7, 5, 5, 9, 6, 9, 9, 7, 10, 10,
! 111183: 4, 6, 2, 3, 9, 4, 2, 6, 5, 6, 6, 5, 6,
! 111184: 5, 5, 7, 7, 7, 3, 2, 4, 4, 7, 3, 6, 4,
! 111185: 7, 6, 12, 6, 9, 4, 6, 5, 4, 7, 6, 5, 6,
! 111186: 7, 5, 4, 5, 6, 5, 7, 3, 7, 13, 2, 2, 4,
! 111187: 6, 6, 8, 5, 17, 12, 7, 8, 8, 2, 4, 4, 4,
! 111188: 4, 4, 2, 2, 6, 5, 8, 5, 5, 8, 3, 5, 5,
! 111189: 6, 4, 9, 3,
! 111190: };
! 111191: static const unsigned short int aOffset[121] = {
! 111192: 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33,
! 111193: 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81,
! 111194: 86, 91, 95, 96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
! 111195: 159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 189, 194, 197,
! 111196: 203, 206, 210, 217, 223, 223, 223, 226, 229, 233, 234, 238, 244,
! 111197: 248, 255, 261, 273, 279, 288, 290, 296, 301, 303, 310, 315, 320,
! 111198: 326, 332, 337, 341, 344, 350, 354, 361, 363, 370, 372, 374, 383,
! 111199: 387, 393, 399, 407, 412, 412, 428, 435, 442, 443, 450, 454, 458,
! 111200: 462, 466, 469, 471, 473, 479, 483, 491, 495, 500, 508, 511, 516,
! 111201: 521, 527, 531, 536,
! 111202: };
! 111203: static const unsigned char aCode[121] = {
! 111204: TK_REINDEX, TK_INDEXED, TK_INDEX, TK_DESC, TK_ESCAPE,
! 111205: TK_EACH, TK_CHECK, TK_KEY, TK_BEFORE, TK_FOREIGN,
! 111206: TK_FOR, TK_IGNORE, TK_LIKE_KW, TK_EXPLAIN, TK_INSTEAD,
! 111207: TK_ADD, TK_DATABASE, TK_AS, TK_SELECT, TK_TABLE,
! 111208: TK_JOIN_KW, TK_THEN, TK_END, TK_DEFERRABLE, TK_ELSE,
! 111209: TK_EXCEPT, TK_TRANSACTION,TK_ACTION, TK_ON, TK_JOIN_KW,
! 111210: TK_ALTER, TK_RAISE, TK_EXCLUSIVE, TK_EXISTS, TK_SAVEPOINT,
! 111211: TK_INTERSECT, TK_TRIGGER, TK_REFERENCES, TK_CONSTRAINT, TK_INTO,
! 111212: TK_OFFSET, TK_OF, TK_SET, TK_TEMP, TK_TEMP,
! 111213: TK_OR, TK_UNIQUE, TK_QUERY, TK_ATTACH, TK_HAVING,
! 111214: TK_GROUP, TK_UPDATE, TK_BEGIN, TK_JOIN_KW, TK_RELEASE,
! 111215: TK_BETWEEN, TK_NOTNULL, TK_NOT, TK_NO, TK_NULL,
! 111216: TK_LIKE_KW, TK_CASCADE, TK_ASC, TK_DELETE, TK_CASE,
! 111217: TK_COLLATE, TK_CREATE, TK_CTIME_KW, TK_DETACH, TK_IMMEDIATE,
! 111218: TK_JOIN, TK_INSERT, TK_MATCH, TK_PLAN, TK_ANALYZE,
! 111219: TK_PRAGMA, TK_ABORT, TK_VALUES, TK_VIRTUAL, TK_LIMIT,
! 111220: TK_WHEN, TK_WHERE, TK_RENAME, TK_AFTER, TK_REPLACE,
! 111221: TK_AND, TK_DEFAULT, TK_AUTOINCR, TK_TO, TK_IN,
! 111222: TK_CAST, TK_COLUMNKW, TK_COMMIT, TK_CONFLICT, TK_JOIN_KW,
! 111223: TK_CTIME_KW, TK_CTIME_KW, TK_PRIMARY, TK_DEFERRED, TK_DISTINCT,
! 111224: TK_IS, TK_DROP, TK_FAIL, TK_FROM, TK_JOIN_KW,
! 111225: TK_LIKE_KW, TK_BY, TK_IF, TK_ISNULL, TK_ORDER,
! 111226: TK_RESTRICT, TK_JOIN_KW, TK_JOIN_KW, TK_ROLLBACK, TK_ROW,
! 111227: TK_UNION, TK_USING, TK_VACUUM, TK_VIEW, TK_INITIALLY,
! 111228: TK_ALL,
! 111229: };
! 111230: int h, i;
! 111231: if( n<2 ) return TK_ID;
! 111232: h = ((charMap(z[0])*4) ^
! 111233: (charMap(z[n-1])*3) ^
! 111234: n) % 127;
! 111235: for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
! 111236: if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
! 111237: testcase( i==0 ); /* REINDEX */
! 111238: testcase( i==1 ); /* INDEXED */
! 111239: testcase( i==2 ); /* INDEX */
! 111240: testcase( i==3 ); /* DESC */
! 111241: testcase( i==4 ); /* ESCAPE */
! 111242: testcase( i==5 ); /* EACH */
! 111243: testcase( i==6 ); /* CHECK */
! 111244: testcase( i==7 ); /* KEY */
! 111245: testcase( i==8 ); /* BEFORE */
! 111246: testcase( i==9 ); /* FOREIGN */
! 111247: testcase( i==10 ); /* FOR */
! 111248: testcase( i==11 ); /* IGNORE */
! 111249: testcase( i==12 ); /* REGEXP */
! 111250: testcase( i==13 ); /* EXPLAIN */
! 111251: testcase( i==14 ); /* INSTEAD */
! 111252: testcase( i==15 ); /* ADD */
! 111253: testcase( i==16 ); /* DATABASE */
! 111254: testcase( i==17 ); /* AS */
! 111255: testcase( i==18 ); /* SELECT */
! 111256: testcase( i==19 ); /* TABLE */
! 111257: testcase( i==20 ); /* LEFT */
! 111258: testcase( i==21 ); /* THEN */
! 111259: testcase( i==22 ); /* END */
! 111260: testcase( i==23 ); /* DEFERRABLE */
! 111261: testcase( i==24 ); /* ELSE */
! 111262: testcase( i==25 ); /* EXCEPT */
! 111263: testcase( i==26 ); /* TRANSACTION */
! 111264: testcase( i==27 ); /* ACTION */
! 111265: testcase( i==28 ); /* ON */
! 111266: testcase( i==29 ); /* NATURAL */
! 111267: testcase( i==30 ); /* ALTER */
! 111268: testcase( i==31 ); /* RAISE */
! 111269: testcase( i==32 ); /* EXCLUSIVE */
! 111270: testcase( i==33 ); /* EXISTS */
! 111271: testcase( i==34 ); /* SAVEPOINT */
! 111272: testcase( i==35 ); /* INTERSECT */
! 111273: testcase( i==36 ); /* TRIGGER */
! 111274: testcase( i==37 ); /* REFERENCES */
! 111275: testcase( i==38 ); /* CONSTRAINT */
! 111276: testcase( i==39 ); /* INTO */
! 111277: testcase( i==40 ); /* OFFSET */
! 111278: testcase( i==41 ); /* OF */
! 111279: testcase( i==42 ); /* SET */
! 111280: testcase( i==43 ); /* TEMPORARY */
! 111281: testcase( i==44 ); /* TEMP */
! 111282: testcase( i==45 ); /* OR */
! 111283: testcase( i==46 ); /* UNIQUE */
! 111284: testcase( i==47 ); /* QUERY */
! 111285: testcase( i==48 ); /* ATTACH */
! 111286: testcase( i==49 ); /* HAVING */
! 111287: testcase( i==50 ); /* GROUP */
! 111288: testcase( i==51 ); /* UPDATE */
! 111289: testcase( i==52 ); /* BEGIN */
! 111290: testcase( i==53 ); /* INNER */
! 111291: testcase( i==54 ); /* RELEASE */
! 111292: testcase( i==55 ); /* BETWEEN */
! 111293: testcase( i==56 ); /* NOTNULL */
! 111294: testcase( i==57 ); /* NOT */
! 111295: testcase( i==58 ); /* NO */
! 111296: testcase( i==59 ); /* NULL */
! 111297: testcase( i==60 ); /* LIKE */
! 111298: testcase( i==61 ); /* CASCADE */
! 111299: testcase( i==62 ); /* ASC */
! 111300: testcase( i==63 ); /* DELETE */
! 111301: testcase( i==64 ); /* CASE */
! 111302: testcase( i==65 ); /* COLLATE */
! 111303: testcase( i==66 ); /* CREATE */
! 111304: testcase( i==67 ); /* CURRENT_DATE */
! 111305: testcase( i==68 ); /* DETACH */
! 111306: testcase( i==69 ); /* IMMEDIATE */
! 111307: testcase( i==70 ); /* JOIN */
! 111308: testcase( i==71 ); /* INSERT */
! 111309: testcase( i==72 ); /* MATCH */
! 111310: testcase( i==73 ); /* PLAN */
! 111311: testcase( i==74 ); /* ANALYZE */
! 111312: testcase( i==75 ); /* PRAGMA */
! 111313: testcase( i==76 ); /* ABORT */
! 111314: testcase( i==77 ); /* VALUES */
! 111315: testcase( i==78 ); /* VIRTUAL */
! 111316: testcase( i==79 ); /* LIMIT */
! 111317: testcase( i==80 ); /* WHEN */
! 111318: testcase( i==81 ); /* WHERE */
! 111319: testcase( i==82 ); /* RENAME */
! 111320: testcase( i==83 ); /* AFTER */
! 111321: testcase( i==84 ); /* REPLACE */
! 111322: testcase( i==85 ); /* AND */
! 111323: testcase( i==86 ); /* DEFAULT */
! 111324: testcase( i==87 ); /* AUTOINCREMENT */
! 111325: testcase( i==88 ); /* TO */
! 111326: testcase( i==89 ); /* IN */
! 111327: testcase( i==90 ); /* CAST */
! 111328: testcase( i==91 ); /* COLUMN */
! 111329: testcase( i==92 ); /* COMMIT */
! 111330: testcase( i==93 ); /* CONFLICT */
! 111331: testcase( i==94 ); /* CROSS */
! 111332: testcase( i==95 ); /* CURRENT_TIMESTAMP */
! 111333: testcase( i==96 ); /* CURRENT_TIME */
! 111334: testcase( i==97 ); /* PRIMARY */
! 111335: testcase( i==98 ); /* DEFERRED */
! 111336: testcase( i==99 ); /* DISTINCT */
! 111337: testcase( i==100 ); /* IS */
! 111338: testcase( i==101 ); /* DROP */
! 111339: testcase( i==102 ); /* FAIL */
! 111340: testcase( i==103 ); /* FROM */
! 111341: testcase( i==104 ); /* FULL */
! 111342: testcase( i==105 ); /* GLOB */
! 111343: testcase( i==106 ); /* BY */
! 111344: testcase( i==107 ); /* IF */
! 111345: testcase( i==108 ); /* ISNULL */
! 111346: testcase( i==109 ); /* ORDER */
! 111347: testcase( i==110 ); /* RESTRICT */
! 111348: testcase( i==111 ); /* OUTER */
! 111349: testcase( i==112 ); /* RIGHT */
! 111350: testcase( i==113 ); /* ROLLBACK */
! 111351: testcase( i==114 ); /* ROW */
! 111352: testcase( i==115 ); /* UNION */
! 111353: testcase( i==116 ); /* USING */
! 111354: testcase( i==117 ); /* VACUUM */
! 111355: testcase( i==118 ); /* VIEW */
! 111356: testcase( i==119 ); /* INITIALLY */
! 111357: testcase( i==120 ); /* ALL */
! 111358: return aCode[i];
! 111359: }
! 111360: }
! 111361: return TK_ID;
! 111362: }
! 111363: SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
! 111364: return keywordCode((char*)z, n);
! 111365: }
! 111366: #define SQLITE_N_KEYWORD 121
! 111367:
! 111368: /************** End of keywordhash.h *****************************************/
! 111369: /************** Continuing where we left off in tokenize.c *******************/
! 111370:
! 111371:
! 111372: /*
! 111373: ** If X is a character that can be used in an identifier then
! 111374: ** IdChar(X) will be true. Otherwise it is false.
! 111375: **
! 111376: ** For ASCII, any character with the high-order bit set is
! 111377: ** allowed in an identifier. For 7-bit characters,
! 111378: ** sqlite3IsIdChar[X] must be 1.
! 111379: **
! 111380: ** For EBCDIC, the rules are more complex but have the same
! 111381: ** end result.
! 111382: **
! 111383: ** Ticket #1066. the SQL standard does not allow '$' in the
! 111384: ** middle of identfiers. But many SQL implementations do.
! 111385: ** SQLite will allow '$' in identifiers for compatibility.
! 111386: ** But the feature is undocumented.
! 111387: */
! 111388: #ifdef SQLITE_ASCII
! 111389: #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
! 111390: #endif
! 111391: #ifdef SQLITE_EBCDIC
! 111392: SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
! 111393: /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
! 111394: 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
! 111395: 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
! 111396: 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
! 111397: 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
! 111398: 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
! 111399: 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
! 111400: 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
! 111401: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
! 111402: 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
! 111403: 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
! 111404: 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
! 111405: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
! 111406: };
! 111407: #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
! 111408: #endif
! 111409:
! 111410:
! 111411: /*
! 111412: ** Return the length of the token that begins at z[0].
! 111413: ** Store the token type in *tokenType before returning.
! 111414: */
! 111415: SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
! 111416: int i, c;
! 111417: switch( *z ){
! 111418: case ' ': case '\t': case '\n': case '\f': case '\r': {
! 111419: testcase( z[0]==' ' );
! 111420: testcase( z[0]=='\t' );
! 111421: testcase( z[0]=='\n' );
! 111422: testcase( z[0]=='\f' );
! 111423: testcase( z[0]=='\r' );
! 111424: for(i=1; sqlite3Isspace(z[i]); i++){}
! 111425: *tokenType = TK_SPACE;
! 111426: return i;
! 111427: }
! 111428: case '-': {
! 111429: if( z[1]=='-' ){
! 111430: /* IMP: R-50417-27976 -- syntax diagram for comments */
! 111431: for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
! 111432: *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
! 111433: return i;
! 111434: }
! 111435: *tokenType = TK_MINUS;
! 111436: return 1;
! 111437: }
! 111438: case '(': {
! 111439: *tokenType = TK_LP;
! 111440: return 1;
! 111441: }
! 111442: case ')': {
! 111443: *tokenType = TK_RP;
! 111444: return 1;
! 111445: }
! 111446: case ';': {
! 111447: *tokenType = TK_SEMI;
! 111448: return 1;
! 111449: }
! 111450: case '+': {
! 111451: *tokenType = TK_PLUS;
! 111452: return 1;
! 111453: }
! 111454: case '*': {
! 111455: *tokenType = TK_STAR;
! 111456: return 1;
! 111457: }
! 111458: case '/': {
! 111459: if( z[1]!='*' || z[2]==0 ){
! 111460: *tokenType = TK_SLASH;
! 111461: return 1;
! 111462: }
! 111463: /* IMP: R-50417-27976 -- syntax diagram for comments */
! 111464: for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
! 111465: if( c ) i++;
! 111466: *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
! 111467: return i;
! 111468: }
! 111469: case '%': {
! 111470: *tokenType = TK_REM;
! 111471: return 1;
! 111472: }
! 111473: case '=': {
! 111474: *tokenType = TK_EQ;
! 111475: return 1 + (z[1]=='=');
! 111476: }
! 111477: case '<': {
! 111478: if( (c=z[1])=='=' ){
! 111479: *tokenType = TK_LE;
! 111480: return 2;
! 111481: }else if( c=='>' ){
! 111482: *tokenType = TK_NE;
! 111483: return 2;
! 111484: }else if( c=='<' ){
! 111485: *tokenType = TK_LSHIFT;
! 111486: return 2;
! 111487: }else{
! 111488: *tokenType = TK_LT;
! 111489: return 1;
! 111490: }
! 111491: }
! 111492: case '>': {
! 111493: if( (c=z[1])=='=' ){
! 111494: *tokenType = TK_GE;
! 111495: return 2;
! 111496: }else if( c=='>' ){
! 111497: *tokenType = TK_RSHIFT;
! 111498: return 2;
! 111499: }else{
! 111500: *tokenType = TK_GT;
! 111501: return 1;
! 111502: }
! 111503: }
! 111504: case '!': {
! 111505: if( z[1]!='=' ){
! 111506: *tokenType = TK_ILLEGAL;
! 111507: return 2;
! 111508: }else{
! 111509: *tokenType = TK_NE;
! 111510: return 2;
! 111511: }
! 111512: }
! 111513: case '|': {
! 111514: if( z[1]!='|' ){
! 111515: *tokenType = TK_BITOR;
! 111516: return 1;
! 111517: }else{
! 111518: *tokenType = TK_CONCAT;
! 111519: return 2;
! 111520: }
! 111521: }
! 111522: case ',': {
! 111523: *tokenType = TK_COMMA;
! 111524: return 1;
! 111525: }
! 111526: case '&': {
! 111527: *tokenType = TK_BITAND;
! 111528: return 1;
! 111529: }
! 111530: case '~': {
! 111531: *tokenType = TK_BITNOT;
! 111532: return 1;
! 111533: }
! 111534: case '`':
! 111535: case '\'':
! 111536: case '"': {
! 111537: int delim = z[0];
! 111538: testcase( delim=='`' );
! 111539: testcase( delim=='\'' );
! 111540: testcase( delim=='"' );
! 111541: for(i=1; (c=z[i])!=0; i++){
! 111542: if( c==delim ){
! 111543: if( z[i+1]==delim ){
! 111544: i++;
! 111545: }else{
! 111546: break;
! 111547: }
! 111548: }
! 111549: }
! 111550: if( c=='\'' ){
! 111551: *tokenType = TK_STRING;
! 111552: return i+1;
! 111553: }else if( c!=0 ){
! 111554: *tokenType = TK_ID;
! 111555: return i+1;
! 111556: }else{
! 111557: *tokenType = TK_ILLEGAL;
! 111558: return i;
! 111559: }
! 111560: }
! 111561: case '.': {
! 111562: #ifndef SQLITE_OMIT_FLOATING_POINT
! 111563: if( !sqlite3Isdigit(z[1]) )
! 111564: #endif
! 111565: {
! 111566: *tokenType = TK_DOT;
! 111567: return 1;
! 111568: }
! 111569: /* If the next character is a digit, this is a floating point
! 111570: ** number that begins with ".". Fall thru into the next case */
! 111571: }
! 111572: case '0': case '1': case '2': case '3': case '4':
! 111573: case '5': case '6': case '7': case '8': case '9': {
! 111574: testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
! 111575: testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
! 111576: testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
! 111577: testcase( z[0]=='9' );
! 111578: *tokenType = TK_INTEGER;
! 111579: for(i=0; sqlite3Isdigit(z[i]); i++){}
! 111580: #ifndef SQLITE_OMIT_FLOATING_POINT
! 111581: if( z[i]=='.' ){
! 111582: i++;
! 111583: while( sqlite3Isdigit(z[i]) ){ i++; }
! 111584: *tokenType = TK_FLOAT;
! 111585: }
! 111586: if( (z[i]=='e' || z[i]=='E') &&
! 111587: ( sqlite3Isdigit(z[i+1])
! 111588: || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
! 111589: )
! 111590: ){
! 111591: i += 2;
! 111592: while( sqlite3Isdigit(z[i]) ){ i++; }
! 111593: *tokenType = TK_FLOAT;
! 111594: }
! 111595: #endif
! 111596: while( IdChar(z[i]) ){
! 111597: *tokenType = TK_ILLEGAL;
! 111598: i++;
! 111599: }
! 111600: return i;
! 111601: }
! 111602: case '[': {
! 111603: for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
! 111604: *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
! 111605: return i;
! 111606: }
! 111607: case '?': {
! 111608: *tokenType = TK_VARIABLE;
! 111609: for(i=1; sqlite3Isdigit(z[i]); i++){}
! 111610: return i;
! 111611: }
! 111612: case '#': {
! 111613: for(i=1; sqlite3Isdigit(z[i]); i++){}
! 111614: if( i>1 ){
! 111615: /* Parameters of the form #NNN (where NNN is a number) are used
! 111616: ** internally by sqlite3NestedParse. */
! 111617: *tokenType = TK_REGISTER;
! 111618: return i;
! 111619: }
! 111620: /* Fall through into the next case if the '#' is not followed by
! 111621: ** a digit. Try to match #AAAA where AAAA is a parameter name. */
! 111622: }
! 111623: #ifndef SQLITE_OMIT_TCL_VARIABLE
! 111624: case '$':
! 111625: #endif
! 111626: case '@': /* For compatibility with MS SQL Server */
! 111627: case ':': {
! 111628: int n = 0;
! 111629: testcase( z[0]=='$' ); testcase( z[0]=='@' ); testcase( z[0]==':' );
! 111630: *tokenType = TK_VARIABLE;
! 111631: for(i=1; (c=z[i])!=0; i++){
! 111632: if( IdChar(c) ){
! 111633: n++;
! 111634: #ifndef SQLITE_OMIT_TCL_VARIABLE
! 111635: }else if( c=='(' && n>0 ){
! 111636: do{
! 111637: i++;
! 111638: }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
! 111639: if( c==')' ){
! 111640: i++;
! 111641: }else{
! 111642: *tokenType = TK_ILLEGAL;
! 111643: }
! 111644: break;
! 111645: }else if( c==':' && z[i+1]==':' ){
! 111646: i++;
! 111647: #endif
! 111648: }else{
! 111649: break;
! 111650: }
! 111651: }
! 111652: if( n==0 ) *tokenType = TK_ILLEGAL;
! 111653: return i;
! 111654: }
! 111655: #ifndef SQLITE_OMIT_BLOB_LITERAL
! 111656: case 'x': case 'X': {
! 111657: testcase( z[0]=='x' ); testcase( z[0]=='X' );
! 111658: if( z[1]=='\'' ){
! 111659: *tokenType = TK_BLOB;
! 111660: for(i=2; sqlite3Isxdigit(z[i]); i++){}
! 111661: if( z[i]!='\'' || i%2 ){
! 111662: *tokenType = TK_ILLEGAL;
! 111663: while( z[i] && z[i]!='\'' ){ i++; }
! 111664: }
! 111665: if( z[i] ) i++;
! 111666: return i;
! 111667: }
! 111668: /* Otherwise fall through to the next case */
! 111669: }
! 111670: #endif
! 111671: default: {
! 111672: if( !IdChar(*z) ){
! 111673: break;
! 111674: }
! 111675: for(i=1; IdChar(z[i]); i++){}
! 111676: *tokenType = keywordCode((char*)z, i);
! 111677: return i;
! 111678: }
! 111679: }
! 111680: *tokenType = TK_ILLEGAL;
! 111681: return 1;
! 111682: }
! 111683:
! 111684: /*
! 111685: ** Run the parser on the given SQL string. The parser structure is
! 111686: ** passed in. An SQLITE_ status code is returned. If an error occurs
! 111687: ** then an and attempt is made to write an error message into
! 111688: ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
! 111689: ** error message.
! 111690: */
! 111691: SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
! 111692: int nErr = 0; /* Number of errors encountered */
! 111693: int i; /* Loop counter */
! 111694: void *pEngine; /* The LEMON-generated LALR(1) parser */
! 111695: int tokenType; /* type of the next token */
! 111696: int lastTokenParsed = -1; /* type of the previous token */
! 111697: u8 enableLookaside; /* Saved value of db->lookaside.bEnabled */
! 111698: sqlite3 *db = pParse->db; /* The database connection */
! 111699: int mxSqlLen; /* Max length of an SQL string */
! 111700:
! 111701:
! 111702: mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
! 111703: if( db->activeVdbeCnt==0 ){
! 111704: db->u1.isInterrupted = 0;
! 111705: }
! 111706: pParse->rc = SQLITE_OK;
! 111707: pParse->zTail = zSql;
! 111708: i = 0;
! 111709: assert( pzErrMsg!=0 );
! 111710: pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
! 111711: if( pEngine==0 ){
! 111712: db->mallocFailed = 1;
! 111713: return SQLITE_NOMEM;
! 111714: }
! 111715: assert( pParse->pNewTable==0 );
! 111716: assert( pParse->pNewTrigger==0 );
! 111717: assert( pParse->nVar==0 );
! 111718: assert( pParse->nzVar==0 );
! 111719: assert( pParse->azVar==0 );
! 111720: enableLookaside = db->lookaside.bEnabled;
! 111721: if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
! 111722: while( !db->mallocFailed && zSql[i]!=0 ){
! 111723: assert( i>=0 );
! 111724: pParse->sLastToken.z = &zSql[i];
! 111725: pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
! 111726: i += pParse->sLastToken.n;
! 111727: if( i>mxSqlLen ){
! 111728: pParse->rc = SQLITE_TOOBIG;
! 111729: break;
! 111730: }
! 111731: switch( tokenType ){
! 111732: case TK_SPACE: {
! 111733: if( db->u1.isInterrupted ){
! 111734: sqlite3ErrorMsg(pParse, "interrupt");
! 111735: pParse->rc = SQLITE_INTERRUPT;
! 111736: goto abort_parse;
! 111737: }
! 111738: break;
! 111739: }
! 111740: case TK_ILLEGAL: {
! 111741: sqlite3DbFree(db, *pzErrMsg);
! 111742: *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
! 111743: &pParse->sLastToken);
! 111744: nErr++;
! 111745: goto abort_parse;
! 111746: }
! 111747: case TK_SEMI: {
! 111748: pParse->zTail = &zSql[i];
! 111749: /* Fall thru into the default case */
! 111750: }
! 111751: default: {
! 111752: sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
! 111753: lastTokenParsed = tokenType;
! 111754: if( pParse->rc!=SQLITE_OK ){
! 111755: goto abort_parse;
! 111756: }
! 111757: break;
! 111758: }
! 111759: }
! 111760: }
! 111761: abort_parse:
! 111762: if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
! 111763: if( lastTokenParsed!=TK_SEMI ){
! 111764: sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
! 111765: pParse->zTail = &zSql[i];
! 111766: }
! 111767: sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
! 111768: }
! 111769: #ifdef YYTRACKMAXSTACKDEPTH
! 111770: sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
! 111771: sqlite3ParserStackPeak(pEngine)
! 111772: );
! 111773: #endif /* YYDEBUG */
! 111774: sqlite3ParserFree(pEngine, sqlite3_free);
! 111775: db->lookaside.bEnabled = enableLookaside;
! 111776: if( db->mallocFailed ){
! 111777: pParse->rc = SQLITE_NOMEM;
! 111778: }
! 111779: if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
! 111780: sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
! 111781: }
! 111782: assert( pzErrMsg!=0 );
! 111783: if( pParse->zErrMsg ){
! 111784: *pzErrMsg = pParse->zErrMsg;
! 111785: sqlite3_log(pParse->rc, "%s", *pzErrMsg);
! 111786: pParse->zErrMsg = 0;
! 111787: nErr++;
! 111788: }
! 111789: if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
! 111790: sqlite3VdbeDelete(pParse->pVdbe);
! 111791: pParse->pVdbe = 0;
! 111792: }
! 111793: #ifndef SQLITE_OMIT_SHARED_CACHE
! 111794: if( pParse->nested==0 ){
! 111795: sqlite3DbFree(db, pParse->aTableLock);
! 111796: pParse->aTableLock = 0;
! 111797: pParse->nTableLock = 0;
! 111798: }
! 111799: #endif
! 111800: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 111801: sqlite3_free(pParse->apVtabLock);
! 111802: #endif
! 111803:
! 111804: if( !IN_DECLARE_VTAB ){
! 111805: /* If the pParse->declareVtab flag is set, do not delete any table
! 111806: ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
! 111807: ** will take responsibility for freeing the Table structure.
! 111808: */
! 111809: sqlite3DeleteTable(db, pParse->pNewTable);
! 111810: }
! 111811:
! 111812: sqlite3DeleteTrigger(db, pParse->pNewTrigger);
! 111813: for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
! 111814: sqlite3DbFree(db, pParse->azVar);
! 111815: sqlite3DbFree(db, pParse->aAlias);
! 111816: while( pParse->pAinc ){
! 111817: AutoincInfo *p = pParse->pAinc;
! 111818: pParse->pAinc = p->pNext;
! 111819: sqlite3DbFree(db, p);
! 111820: }
! 111821: while( pParse->pZombieTab ){
! 111822: Table *p = pParse->pZombieTab;
! 111823: pParse->pZombieTab = p->pNextZombie;
! 111824: sqlite3DeleteTable(db, p);
! 111825: }
! 111826: if( nErr>0 && pParse->rc==SQLITE_OK ){
! 111827: pParse->rc = SQLITE_ERROR;
! 111828: }
! 111829: return nErr;
! 111830: }
! 111831:
! 111832: /************** End of tokenize.c ********************************************/
! 111833: /************** Begin file complete.c ****************************************/
! 111834: /*
! 111835: ** 2001 September 15
! 111836: **
! 111837: ** The author disclaims copyright to this source code. In place of
! 111838: ** a legal notice, here is a blessing:
! 111839: **
! 111840: ** May you do good and not evil.
! 111841: ** May you find forgiveness for yourself and forgive others.
! 111842: ** May you share freely, never taking more than you give.
! 111843: **
! 111844: *************************************************************************
! 111845: ** An tokenizer for SQL
! 111846: **
! 111847: ** This file contains C code that implements the sqlite3_complete() API.
! 111848: ** This code used to be part of the tokenizer.c source file. But by
! 111849: ** separating it out, the code will be automatically omitted from
! 111850: ** static links that do not use it.
! 111851: */
! 111852: #ifndef SQLITE_OMIT_COMPLETE
! 111853:
! 111854: /*
! 111855: ** This is defined in tokenize.c. We just have to import the definition.
! 111856: */
! 111857: #ifndef SQLITE_AMALGAMATION
! 111858: #ifdef SQLITE_ASCII
! 111859: #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
! 111860: #endif
! 111861: #ifdef SQLITE_EBCDIC
! 111862: SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
! 111863: #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
! 111864: #endif
! 111865: #endif /* SQLITE_AMALGAMATION */
! 111866:
! 111867:
! 111868: /*
! 111869: ** Token types used by the sqlite3_complete() routine. See the header
! 111870: ** comments on that procedure for additional information.
! 111871: */
! 111872: #define tkSEMI 0
! 111873: #define tkWS 1
! 111874: #define tkOTHER 2
! 111875: #ifndef SQLITE_OMIT_TRIGGER
! 111876: #define tkEXPLAIN 3
! 111877: #define tkCREATE 4
! 111878: #define tkTEMP 5
! 111879: #define tkTRIGGER 6
! 111880: #define tkEND 7
! 111881: #endif
! 111882:
! 111883: /*
! 111884: ** Return TRUE if the given SQL string ends in a semicolon.
! 111885: **
! 111886: ** Special handling is require for CREATE TRIGGER statements.
! 111887: ** Whenever the CREATE TRIGGER keywords are seen, the statement
! 111888: ** must end with ";END;".
! 111889: **
! 111890: ** This implementation uses a state machine with 8 states:
! 111891: **
! 111892: ** (0) INVALID We have not yet seen a non-whitespace character.
! 111893: **
! 111894: ** (1) START At the beginning or end of an SQL statement. This routine
! 111895: ** returns 1 if it ends in the START state and 0 if it ends
! 111896: ** in any other state.
! 111897: **
! 111898: ** (2) NORMAL We are in the middle of statement which ends with a single
! 111899: ** semicolon.
! 111900: **
! 111901: ** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
! 111902: ** a statement.
! 111903: **
! 111904: ** (4) CREATE The keyword CREATE has been seen at the beginning of a
! 111905: ** statement, possibly preceeded by EXPLAIN and/or followed by
! 111906: ** TEMP or TEMPORARY
! 111907: **
! 111908: ** (5) TRIGGER We are in the middle of a trigger definition that must be
! 111909: ** ended by a semicolon, the keyword END, and another semicolon.
! 111910: **
! 111911: ** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
! 111912: ** the end of a trigger definition.
! 111913: **
! 111914: ** (7) END We've seen the ";END" of the ";END;" that occurs at the end
! 111915: ** of a trigger difinition.
! 111916: **
! 111917: ** Transitions between states above are determined by tokens extracted
! 111918: ** from the input. The following tokens are significant:
! 111919: **
! 111920: ** (0) tkSEMI A semicolon.
! 111921: ** (1) tkWS Whitespace.
! 111922: ** (2) tkOTHER Any other SQL token.
! 111923: ** (3) tkEXPLAIN The "explain" keyword.
! 111924: ** (4) tkCREATE The "create" keyword.
! 111925: ** (5) tkTEMP The "temp" or "temporary" keyword.
! 111926: ** (6) tkTRIGGER The "trigger" keyword.
! 111927: ** (7) tkEND The "end" keyword.
! 111928: **
! 111929: ** Whitespace never causes a state transition and is always ignored.
! 111930: ** This means that a SQL string of all whitespace is invalid.
! 111931: **
! 111932: ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
! 111933: ** to recognize the end of a trigger can be omitted. All we have to do
! 111934: ** is look for a semicolon that is not part of an string or comment.
! 111935: */
! 111936: SQLITE_API int sqlite3_complete(const char *zSql){
! 111937: u8 state = 0; /* Current state, using numbers defined in header comment */
! 111938: u8 token; /* Value of the next token */
! 111939:
! 111940: #ifndef SQLITE_OMIT_TRIGGER
! 111941: /* A complex statement machine used to detect the end of a CREATE TRIGGER
! 111942: ** statement. This is the normal case.
! 111943: */
! 111944: static const u8 trans[8][8] = {
! 111945: /* Token: */
! 111946: /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
! 111947: /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
! 111948: /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
! 111949: /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
! 111950: /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
! 111951: /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
! 111952: /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
! 111953: /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
! 111954: /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
! 111955: };
! 111956: #else
! 111957: /* If triggers are not supported by this compile then the statement machine
! 111958: ** used to detect the end of a statement is much simplier
! 111959: */
! 111960: static const u8 trans[3][3] = {
! 111961: /* Token: */
! 111962: /* State: ** SEMI WS OTHER */
! 111963: /* 0 INVALID: */ { 1, 0, 2, },
! 111964: /* 1 START: */ { 1, 1, 2, },
! 111965: /* 2 NORMAL: */ { 1, 2, 2, },
! 111966: };
! 111967: #endif /* SQLITE_OMIT_TRIGGER */
! 111968:
! 111969: while( *zSql ){
! 111970: switch( *zSql ){
! 111971: case ';': { /* A semicolon */
! 111972: token = tkSEMI;
! 111973: break;
! 111974: }
! 111975: case ' ':
! 111976: case '\r':
! 111977: case '\t':
! 111978: case '\n':
! 111979: case '\f': { /* White space is ignored */
! 111980: token = tkWS;
! 111981: break;
! 111982: }
! 111983: case '/': { /* C-style comments */
! 111984: if( zSql[1]!='*' ){
! 111985: token = tkOTHER;
! 111986: break;
! 111987: }
! 111988: zSql += 2;
! 111989: while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
! 111990: if( zSql[0]==0 ) return 0;
! 111991: zSql++;
! 111992: token = tkWS;
! 111993: break;
! 111994: }
! 111995: case '-': { /* SQL-style comments from "--" to end of line */
! 111996: if( zSql[1]!='-' ){
! 111997: token = tkOTHER;
! 111998: break;
! 111999: }
! 112000: while( *zSql && *zSql!='\n' ){ zSql++; }
! 112001: if( *zSql==0 ) return state==1;
! 112002: token = tkWS;
! 112003: break;
! 112004: }
! 112005: case '[': { /* Microsoft-style identifiers in [...] */
! 112006: zSql++;
! 112007: while( *zSql && *zSql!=']' ){ zSql++; }
! 112008: if( *zSql==0 ) return 0;
! 112009: token = tkOTHER;
! 112010: break;
! 112011: }
! 112012: case '`': /* Grave-accent quoted symbols used by MySQL */
! 112013: case '"': /* single- and double-quoted strings */
! 112014: case '\'': {
! 112015: int c = *zSql;
! 112016: zSql++;
! 112017: while( *zSql && *zSql!=c ){ zSql++; }
! 112018: if( *zSql==0 ) return 0;
! 112019: token = tkOTHER;
! 112020: break;
! 112021: }
! 112022: default: {
! 112023: #ifdef SQLITE_EBCDIC
! 112024: unsigned char c;
! 112025: #endif
! 112026: if( IdChar((u8)*zSql) ){
! 112027: /* Keywords and unquoted identifiers */
! 112028: int nId;
! 112029: for(nId=1; IdChar(zSql[nId]); nId++){}
! 112030: #ifdef SQLITE_OMIT_TRIGGER
! 112031: token = tkOTHER;
! 112032: #else
! 112033: switch( *zSql ){
! 112034: case 'c': case 'C': {
! 112035: if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
! 112036: token = tkCREATE;
! 112037: }else{
! 112038: token = tkOTHER;
! 112039: }
! 112040: break;
! 112041: }
! 112042: case 't': case 'T': {
! 112043: if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
! 112044: token = tkTRIGGER;
! 112045: }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
! 112046: token = tkTEMP;
! 112047: }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
! 112048: token = tkTEMP;
! 112049: }else{
! 112050: token = tkOTHER;
! 112051: }
! 112052: break;
! 112053: }
! 112054: case 'e': case 'E': {
! 112055: if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
! 112056: token = tkEND;
! 112057: }else
! 112058: #ifndef SQLITE_OMIT_EXPLAIN
! 112059: if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
! 112060: token = tkEXPLAIN;
! 112061: }else
! 112062: #endif
! 112063: {
! 112064: token = tkOTHER;
! 112065: }
! 112066: break;
! 112067: }
! 112068: default: {
! 112069: token = tkOTHER;
! 112070: break;
! 112071: }
! 112072: }
! 112073: #endif /* SQLITE_OMIT_TRIGGER */
! 112074: zSql += nId-1;
! 112075: }else{
! 112076: /* Operators and special symbols */
! 112077: token = tkOTHER;
! 112078: }
! 112079: break;
! 112080: }
! 112081: }
! 112082: state = trans[state][token];
! 112083: zSql++;
! 112084: }
! 112085: return state==1;
! 112086: }
! 112087:
! 112088: #ifndef SQLITE_OMIT_UTF16
! 112089: /*
! 112090: ** This routine is the same as the sqlite3_complete() routine described
! 112091: ** above, except that the parameter is required to be UTF-16 encoded, not
! 112092: ** UTF-8.
! 112093: */
! 112094: SQLITE_API int sqlite3_complete16(const void *zSql){
! 112095: sqlite3_value *pVal;
! 112096: char const *zSql8;
! 112097: int rc = SQLITE_NOMEM;
! 112098:
! 112099: #ifndef SQLITE_OMIT_AUTOINIT
! 112100: rc = sqlite3_initialize();
! 112101: if( rc ) return rc;
! 112102: #endif
! 112103: pVal = sqlite3ValueNew(0);
! 112104: sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
! 112105: zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
! 112106: if( zSql8 ){
! 112107: rc = sqlite3_complete(zSql8);
! 112108: }else{
! 112109: rc = SQLITE_NOMEM;
! 112110: }
! 112111: sqlite3ValueFree(pVal);
! 112112: return sqlite3ApiExit(0, rc);
! 112113: }
! 112114: #endif /* SQLITE_OMIT_UTF16 */
! 112115: #endif /* SQLITE_OMIT_COMPLETE */
! 112116:
! 112117: /************** End of complete.c ********************************************/
! 112118: /************** Begin file main.c ********************************************/
! 112119: /*
! 112120: ** 2001 September 15
! 112121: **
! 112122: ** The author disclaims copyright to this source code. In place of
! 112123: ** a legal notice, here is a blessing:
! 112124: **
! 112125: ** May you do good and not evil.
! 112126: ** May you find forgiveness for yourself and forgive others.
! 112127: ** May you share freely, never taking more than you give.
! 112128: **
! 112129: *************************************************************************
! 112130: ** Main file for the SQLite library. The routines in this file
! 112131: ** implement the programmer interface to the library. Routines in
! 112132: ** other files are for internal use by SQLite and should not be
! 112133: ** accessed by users of the library.
! 112134: */
! 112135:
! 112136: #ifdef SQLITE_ENABLE_FTS3
! 112137: /************** Include fts3.h in the middle of main.c ***********************/
! 112138: /************** Begin file fts3.h ********************************************/
! 112139: /*
! 112140: ** 2006 Oct 10
! 112141: **
! 112142: ** The author disclaims copyright to this source code. In place of
! 112143: ** a legal notice, here is a blessing:
! 112144: **
! 112145: ** May you do good and not evil.
! 112146: ** May you find forgiveness for yourself and forgive others.
! 112147: ** May you share freely, never taking more than you give.
! 112148: **
! 112149: ******************************************************************************
! 112150: **
! 112151: ** This header file is used by programs that want to link against the
! 112152: ** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
! 112153: */
! 112154:
! 112155: #if 0
! 112156: extern "C" {
! 112157: #endif /* __cplusplus */
! 112158:
! 112159: SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
! 112160:
! 112161: #if 0
! 112162: } /* extern "C" */
! 112163: #endif /* __cplusplus */
! 112164:
! 112165: /************** End of fts3.h ************************************************/
! 112166: /************** Continuing where we left off in main.c ***********************/
! 112167: #endif
! 112168: #ifdef SQLITE_ENABLE_RTREE
! 112169: /************** Include rtree.h in the middle of main.c **********************/
! 112170: /************** Begin file rtree.h *******************************************/
! 112171: /*
! 112172: ** 2008 May 26
! 112173: **
! 112174: ** The author disclaims copyright to this source code. In place of
! 112175: ** a legal notice, here is a blessing:
! 112176: **
! 112177: ** May you do good and not evil.
! 112178: ** May you find forgiveness for yourself and forgive others.
! 112179: ** May you share freely, never taking more than you give.
! 112180: **
! 112181: ******************************************************************************
! 112182: **
! 112183: ** This header file is used by programs that want to link against the
! 112184: ** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
! 112185: */
! 112186:
! 112187: #if 0
! 112188: extern "C" {
! 112189: #endif /* __cplusplus */
! 112190:
! 112191: SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
! 112192:
! 112193: #if 0
! 112194: } /* extern "C" */
! 112195: #endif /* __cplusplus */
! 112196:
! 112197: /************** End of rtree.h ***********************************************/
! 112198: /************** Continuing where we left off in main.c ***********************/
! 112199: #endif
! 112200: #ifdef SQLITE_ENABLE_ICU
! 112201: /************** Include sqliteicu.h in the middle of main.c ******************/
! 112202: /************** Begin file sqliteicu.h ***************************************/
! 112203: /*
! 112204: ** 2008 May 26
! 112205: **
! 112206: ** The author disclaims copyright to this source code. In place of
! 112207: ** a legal notice, here is a blessing:
! 112208: **
! 112209: ** May you do good and not evil.
! 112210: ** May you find forgiveness for yourself and forgive others.
! 112211: ** May you share freely, never taking more than you give.
! 112212: **
! 112213: ******************************************************************************
! 112214: **
! 112215: ** This header file is used by programs that want to link against the
! 112216: ** ICU extension. All it does is declare the sqlite3IcuInit() interface.
! 112217: */
! 112218:
! 112219: #if 0
! 112220: extern "C" {
! 112221: #endif /* __cplusplus */
! 112222:
! 112223: SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
! 112224:
! 112225: #if 0
! 112226: } /* extern "C" */
! 112227: #endif /* __cplusplus */
! 112228:
! 112229:
! 112230: /************** End of sqliteicu.h *******************************************/
! 112231: /************** Continuing where we left off in main.c ***********************/
! 112232: #endif
! 112233:
! 112234: #ifndef SQLITE_AMALGAMATION
! 112235: /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
! 112236: ** contains the text of SQLITE_VERSION macro.
! 112237: */
! 112238: SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
! 112239: #endif
! 112240:
! 112241: /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
! 112242: ** a pointer to the to the sqlite3_version[] string constant.
! 112243: */
! 112244: SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
! 112245:
! 112246: /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
! 112247: ** pointer to a string constant whose value is the same as the
! 112248: ** SQLITE_SOURCE_ID C preprocessor macro.
! 112249: */
! 112250: SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
! 112251:
! 112252: /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
! 112253: ** returns an integer equal to SQLITE_VERSION_NUMBER.
! 112254: */
! 112255: SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
! 112256:
! 112257: /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
! 112258: ** zero if and only if SQLite was compiled with mutexing code omitted due to
! 112259: ** the SQLITE_THREADSAFE compile-time option being set to 0.
! 112260: */
! 112261: SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
! 112262:
! 112263: #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
! 112264: /*
! 112265: ** If the following function pointer is not NULL and if
! 112266: ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
! 112267: ** I/O active are written using this function. These messages
! 112268: ** are intended for debugging activity only.
! 112269: */
! 112270: SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0;
! 112271: #endif
! 112272:
! 112273: /*
! 112274: ** If the following global variable points to a string which is the
! 112275: ** name of a directory, then that directory will be used to store
! 112276: ** temporary files.
! 112277: **
! 112278: ** See also the "PRAGMA temp_store_directory" SQL command.
! 112279: */
! 112280: SQLITE_API char *sqlite3_temp_directory = 0;
! 112281:
! 112282: /*
! 112283: ** Initialize SQLite.
! 112284: **
! 112285: ** This routine must be called to initialize the memory allocation,
! 112286: ** VFS, and mutex subsystems prior to doing any serious work with
! 112287: ** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
! 112288: ** this routine will be called automatically by key routines such as
! 112289: ** sqlite3_open().
! 112290: **
! 112291: ** This routine is a no-op except on its very first call for the process,
! 112292: ** or for the first call after a call to sqlite3_shutdown.
! 112293: **
! 112294: ** The first thread to call this routine runs the initialization to
! 112295: ** completion. If subsequent threads call this routine before the first
! 112296: ** thread has finished the initialization process, then the subsequent
! 112297: ** threads must block until the first thread finishes with the initialization.
! 112298: **
! 112299: ** The first thread might call this routine recursively. Recursive
! 112300: ** calls to this routine should not block, of course. Otherwise the
! 112301: ** initialization process would never complete.
! 112302: **
! 112303: ** Let X be the first thread to enter this routine. Let Y be some other
! 112304: ** thread. Then while the initial invocation of this routine by X is
! 112305: ** incomplete, it is required that:
! 112306: **
! 112307: ** * Calls to this routine from Y must block until the outer-most
! 112308: ** call by X completes.
! 112309: **
! 112310: ** * Recursive calls to this routine from thread X return immediately
! 112311: ** without blocking.
! 112312: */
! 112313: SQLITE_API int sqlite3_initialize(void){
! 112314: MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
! 112315: int rc; /* Result code */
! 112316:
! 112317: #ifdef SQLITE_OMIT_WSD
! 112318: rc = sqlite3_wsd_init(4096, 24);
! 112319: if( rc!=SQLITE_OK ){
! 112320: return rc;
! 112321: }
! 112322: #endif
! 112323:
! 112324: /* If SQLite is already completely initialized, then this call
! 112325: ** to sqlite3_initialize() should be a no-op. But the initialization
! 112326: ** must be complete. So isInit must not be set until the very end
! 112327: ** of this routine.
! 112328: */
! 112329: if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
! 112330:
! 112331: /* Make sure the mutex subsystem is initialized. If unable to
! 112332: ** initialize the mutex subsystem, return early with the error.
! 112333: ** If the system is so sick that we are unable to allocate a mutex,
! 112334: ** there is not much SQLite is going to be able to do.
! 112335: **
! 112336: ** The mutex subsystem must take care of serializing its own
! 112337: ** initialization.
! 112338: */
! 112339: rc = sqlite3MutexInit();
! 112340: if( rc ) return rc;
! 112341:
! 112342: /* Initialize the malloc() system and the recursive pInitMutex mutex.
! 112343: ** This operation is protected by the STATIC_MASTER mutex. Note that
! 112344: ** MutexAlloc() is called for a static mutex prior to initializing the
! 112345: ** malloc subsystem - this implies that the allocation of a static
! 112346: ** mutex must not require support from the malloc subsystem.
! 112347: */
! 112348: MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
! 112349: sqlite3_mutex_enter(pMaster);
! 112350: sqlite3GlobalConfig.isMutexInit = 1;
! 112351: if( !sqlite3GlobalConfig.isMallocInit ){
! 112352: rc = sqlite3MallocInit();
! 112353: }
! 112354: if( rc==SQLITE_OK ){
! 112355: sqlite3GlobalConfig.isMallocInit = 1;
! 112356: if( !sqlite3GlobalConfig.pInitMutex ){
! 112357: sqlite3GlobalConfig.pInitMutex =
! 112358: sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
! 112359: if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
! 112360: rc = SQLITE_NOMEM;
! 112361: }
! 112362: }
! 112363: }
! 112364: if( rc==SQLITE_OK ){
! 112365: sqlite3GlobalConfig.nRefInitMutex++;
! 112366: }
! 112367: sqlite3_mutex_leave(pMaster);
! 112368:
! 112369: /* If rc is not SQLITE_OK at this point, then either the malloc
! 112370: ** subsystem could not be initialized or the system failed to allocate
! 112371: ** the pInitMutex mutex. Return an error in either case. */
! 112372: if( rc!=SQLITE_OK ){
! 112373: return rc;
! 112374: }
! 112375:
! 112376: /* Do the rest of the initialization under the recursive mutex so
! 112377: ** that we will be able to handle recursive calls into
! 112378: ** sqlite3_initialize(). The recursive calls normally come through
! 112379: ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
! 112380: ** recursive calls might also be possible.
! 112381: **
! 112382: ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
! 112383: ** to the xInit method, so the xInit method need not be threadsafe.
! 112384: **
! 112385: ** The following mutex is what serializes access to the appdef pcache xInit
! 112386: ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
! 112387: ** call to sqlite3PcacheInitialize().
! 112388: */
! 112389: sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
! 112390: if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
! 112391: FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
! 112392: sqlite3GlobalConfig.inProgress = 1;
! 112393: memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
! 112394: sqlite3RegisterGlobalFunctions();
! 112395: if( sqlite3GlobalConfig.isPCacheInit==0 ){
! 112396: rc = sqlite3PcacheInitialize();
! 112397: }
! 112398: if( rc==SQLITE_OK ){
! 112399: sqlite3GlobalConfig.isPCacheInit = 1;
! 112400: rc = sqlite3OsInit();
! 112401: }
! 112402: if( rc==SQLITE_OK ){
! 112403: sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
! 112404: sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
! 112405: sqlite3GlobalConfig.isInit = 1;
! 112406: }
! 112407: sqlite3GlobalConfig.inProgress = 0;
! 112408: }
! 112409: sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
! 112410:
! 112411: /* Go back under the static mutex and clean up the recursive
! 112412: ** mutex to prevent a resource leak.
! 112413: */
! 112414: sqlite3_mutex_enter(pMaster);
! 112415: sqlite3GlobalConfig.nRefInitMutex--;
! 112416: if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
! 112417: assert( sqlite3GlobalConfig.nRefInitMutex==0 );
! 112418: sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
! 112419: sqlite3GlobalConfig.pInitMutex = 0;
! 112420: }
! 112421: sqlite3_mutex_leave(pMaster);
! 112422:
! 112423: /* The following is just a sanity check to make sure SQLite has
! 112424: ** been compiled correctly. It is important to run this code, but
! 112425: ** we don't want to run it too often and soak up CPU cycles for no
! 112426: ** reason. So we run it once during initialization.
! 112427: */
! 112428: #ifndef NDEBUG
! 112429: #ifndef SQLITE_OMIT_FLOATING_POINT
! 112430: /* This section of code's only "output" is via assert() statements. */
! 112431: if ( rc==SQLITE_OK ){
! 112432: u64 x = (((u64)1)<<63)-1;
! 112433: double y;
! 112434: assert(sizeof(x)==8);
! 112435: assert(sizeof(x)==sizeof(y));
! 112436: memcpy(&y, &x, 8);
! 112437: assert( sqlite3IsNaN(y) );
! 112438: }
! 112439: #endif
! 112440: #endif
! 112441:
! 112442: /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
! 112443: ** compile-time option.
! 112444: */
! 112445: #ifdef SQLITE_EXTRA_INIT
! 112446: if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){
! 112447: int SQLITE_EXTRA_INIT(const char*);
! 112448: rc = SQLITE_EXTRA_INIT(0);
! 112449: }
! 112450: #endif
! 112451:
! 112452: return rc;
! 112453: }
! 112454:
! 112455: /*
! 112456: ** Undo the effects of sqlite3_initialize(). Must not be called while
! 112457: ** there are outstanding database connections or memory allocations or
! 112458: ** while any part of SQLite is otherwise in use in any thread. This
! 112459: ** routine is not threadsafe. But it is safe to invoke this routine
! 112460: ** on when SQLite is already shut down. If SQLite is already shut down
! 112461: ** when this routine is invoked, then this routine is a harmless no-op.
! 112462: */
! 112463: SQLITE_API int sqlite3_shutdown(void){
! 112464: if( sqlite3GlobalConfig.isInit ){
! 112465: #ifdef SQLITE_EXTRA_SHUTDOWN
! 112466: void SQLITE_EXTRA_SHUTDOWN(void);
! 112467: SQLITE_EXTRA_SHUTDOWN();
! 112468: #endif
! 112469: sqlite3_os_end();
! 112470: sqlite3_reset_auto_extension();
! 112471: sqlite3GlobalConfig.isInit = 0;
! 112472: }
! 112473: if( sqlite3GlobalConfig.isPCacheInit ){
! 112474: sqlite3PcacheShutdown();
! 112475: sqlite3GlobalConfig.isPCacheInit = 0;
! 112476: }
! 112477: if( sqlite3GlobalConfig.isMallocInit ){
! 112478: sqlite3MallocEnd();
! 112479: sqlite3GlobalConfig.isMallocInit = 0;
! 112480: }
! 112481: if( sqlite3GlobalConfig.isMutexInit ){
! 112482: sqlite3MutexEnd();
! 112483: sqlite3GlobalConfig.isMutexInit = 0;
! 112484: }
! 112485:
! 112486: return SQLITE_OK;
! 112487: }
! 112488:
! 112489: /*
! 112490: ** This API allows applications to modify the global configuration of
! 112491: ** the SQLite library at run-time.
! 112492: **
! 112493: ** This routine should only be called when there are no outstanding
! 112494: ** database connections or memory allocations. This routine is not
! 112495: ** threadsafe. Failure to heed these warnings can lead to unpredictable
! 112496: ** behavior.
! 112497: */
! 112498: SQLITE_API int sqlite3_config(int op, ...){
! 112499: va_list ap;
! 112500: int rc = SQLITE_OK;
! 112501:
! 112502: /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
! 112503: ** the SQLite library is in use. */
! 112504: if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
! 112505:
! 112506: va_start(ap, op);
! 112507: switch( op ){
! 112508:
! 112509: /* Mutex configuration options are only available in a threadsafe
! 112510: ** compile.
! 112511: */
! 112512: #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
! 112513: case SQLITE_CONFIG_SINGLETHREAD: {
! 112514: /* Disable all mutexing */
! 112515: sqlite3GlobalConfig.bCoreMutex = 0;
! 112516: sqlite3GlobalConfig.bFullMutex = 0;
! 112517: break;
! 112518: }
! 112519: case SQLITE_CONFIG_MULTITHREAD: {
! 112520: /* Disable mutexing of database connections */
! 112521: /* Enable mutexing of core data structures */
! 112522: sqlite3GlobalConfig.bCoreMutex = 1;
! 112523: sqlite3GlobalConfig.bFullMutex = 0;
! 112524: break;
! 112525: }
! 112526: case SQLITE_CONFIG_SERIALIZED: {
! 112527: /* Enable all mutexing */
! 112528: sqlite3GlobalConfig.bCoreMutex = 1;
! 112529: sqlite3GlobalConfig.bFullMutex = 1;
! 112530: break;
! 112531: }
! 112532: case SQLITE_CONFIG_MUTEX: {
! 112533: /* Specify an alternative mutex implementation */
! 112534: sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
! 112535: break;
! 112536: }
! 112537: case SQLITE_CONFIG_GETMUTEX: {
! 112538: /* Retrieve the current mutex implementation */
! 112539: *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
! 112540: break;
! 112541: }
! 112542: #endif
! 112543:
! 112544:
! 112545: case SQLITE_CONFIG_MALLOC: {
! 112546: /* Specify an alternative malloc implementation */
! 112547: sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
! 112548: break;
! 112549: }
! 112550: case SQLITE_CONFIG_GETMALLOC: {
! 112551: /* Retrieve the current malloc() implementation */
! 112552: if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
! 112553: *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
! 112554: break;
! 112555: }
! 112556: case SQLITE_CONFIG_MEMSTATUS: {
! 112557: /* Enable or disable the malloc status collection */
! 112558: sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
! 112559: break;
! 112560: }
! 112561: case SQLITE_CONFIG_SCRATCH: {
! 112562: /* Designate a buffer for scratch memory space */
! 112563: sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
! 112564: sqlite3GlobalConfig.szScratch = va_arg(ap, int);
! 112565: sqlite3GlobalConfig.nScratch = va_arg(ap, int);
! 112566: break;
! 112567: }
! 112568: case SQLITE_CONFIG_PAGECACHE: {
! 112569: /* Designate a buffer for page cache memory space */
! 112570: sqlite3GlobalConfig.pPage = va_arg(ap, void*);
! 112571: sqlite3GlobalConfig.szPage = va_arg(ap, int);
! 112572: sqlite3GlobalConfig.nPage = va_arg(ap, int);
! 112573: break;
! 112574: }
! 112575:
! 112576: case SQLITE_CONFIG_PCACHE: {
! 112577: /* no-op */
! 112578: break;
! 112579: }
! 112580: case SQLITE_CONFIG_GETPCACHE: {
! 112581: /* now an error */
! 112582: rc = SQLITE_ERROR;
! 112583: break;
! 112584: }
! 112585:
! 112586: case SQLITE_CONFIG_PCACHE2: {
! 112587: /* Specify an alternative page cache implementation */
! 112588: sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
! 112589: break;
! 112590: }
! 112591: case SQLITE_CONFIG_GETPCACHE2: {
! 112592: if( sqlite3GlobalConfig.pcache2.xInit==0 ){
! 112593: sqlite3PCacheSetDefault();
! 112594: }
! 112595: *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
! 112596: break;
! 112597: }
! 112598:
! 112599: #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
! 112600: case SQLITE_CONFIG_HEAP: {
! 112601: /* Designate a buffer for heap memory space */
! 112602: sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
! 112603: sqlite3GlobalConfig.nHeap = va_arg(ap, int);
! 112604: sqlite3GlobalConfig.mnReq = va_arg(ap, int);
! 112605:
! 112606: if( sqlite3GlobalConfig.mnReq<1 ){
! 112607: sqlite3GlobalConfig.mnReq = 1;
! 112608: }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
! 112609: /* cap min request size at 2^12 */
! 112610: sqlite3GlobalConfig.mnReq = (1<<12);
! 112611: }
! 112612:
! 112613: if( sqlite3GlobalConfig.pHeap==0 ){
! 112614: /* If the heap pointer is NULL, then restore the malloc implementation
! 112615: ** back to NULL pointers too. This will cause the malloc to go
! 112616: ** back to its default implementation when sqlite3_initialize() is
! 112617: ** run.
! 112618: */
! 112619: memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
! 112620: }else{
! 112621: /* The heap pointer is not NULL, then install one of the
! 112622: ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
! 112623: ** ENABLE_MEMSYS5 is defined, return an error.
! 112624: */
! 112625: #ifdef SQLITE_ENABLE_MEMSYS3
! 112626: sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
! 112627: #endif
! 112628: #ifdef SQLITE_ENABLE_MEMSYS5
! 112629: sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
! 112630: #endif
! 112631: }
! 112632: break;
! 112633: }
! 112634: #endif
! 112635:
! 112636: case SQLITE_CONFIG_LOOKASIDE: {
! 112637: sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
! 112638: sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
! 112639: break;
! 112640: }
! 112641:
! 112642: /* Record a pointer to the logger funcction and its first argument.
! 112643: ** The default is NULL. Logging is disabled if the function pointer is
! 112644: ** NULL.
! 112645: */
! 112646: case SQLITE_CONFIG_LOG: {
! 112647: /* MSVC is picky about pulling func ptrs from va lists.
! 112648: ** http://support.microsoft.com/kb/47961
! 112649: ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
! 112650: */
! 112651: typedef void(*LOGFUNC_t)(void*,int,const char*);
! 112652: sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
! 112653: sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
! 112654: break;
! 112655: }
! 112656:
! 112657: case SQLITE_CONFIG_URI: {
! 112658: sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
! 112659: break;
! 112660: }
! 112661:
! 112662: default: {
! 112663: rc = SQLITE_ERROR;
! 112664: break;
! 112665: }
! 112666: }
! 112667: va_end(ap);
! 112668: return rc;
! 112669: }
! 112670:
! 112671: /*
! 112672: ** Set up the lookaside buffers for a database connection.
! 112673: ** Return SQLITE_OK on success.
! 112674: ** If lookaside is already active, return SQLITE_BUSY.
! 112675: **
! 112676: ** The sz parameter is the number of bytes in each lookaside slot.
! 112677: ** The cnt parameter is the number of slots. If pStart is NULL the
! 112678: ** space for the lookaside memory is obtained from sqlite3_malloc().
! 112679: ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
! 112680: ** the lookaside memory.
! 112681: */
! 112682: static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
! 112683: void *pStart;
! 112684: if( db->lookaside.nOut ){
! 112685: return SQLITE_BUSY;
! 112686: }
! 112687: /* Free any existing lookaside buffer for this handle before
! 112688: ** allocating a new one so we don't have to have space for
! 112689: ** both at the same time.
! 112690: */
! 112691: if( db->lookaside.bMalloced ){
! 112692: sqlite3_free(db->lookaside.pStart);
! 112693: }
! 112694: /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
! 112695: ** than a pointer to be useful.
! 112696: */
! 112697: sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
! 112698: if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
! 112699: if( cnt<0 ) cnt = 0;
! 112700: if( sz==0 || cnt==0 ){
! 112701: sz = 0;
! 112702: pStart = 0;
! 112703: }else if( pBuf==0 ){
! 112704: sqlite3BeginBenignMalloc();
! 112705: pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */
! 112706: sqlite3EndBenignMalloc();
! 112707: if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
! 112708: }else{
! 112709: pStart = pBuf;
! 112710: }
! 112711: db->lookaside.pStart = pStart;
! 112712: db->lookaside.pFree = 0;
! 112713: db->lookaside.sz = (u16)sz;
! 112714: if( pStart ){
! 112715: int i;
! 112716: LookasideSlot *p;
! 112717: assert( sz > (int)sizeof(LookasideSlot*) );
! 112718: p = (LookasideSlot*)pStart;
! 112719: for(i=cnt-1; i>=0; i--){
! 112720: p->pNext = db->lookaside.pFree;
! 112721: db->lookaside.pFree = p;
! 112722: p = (LookasideSlot*)&((u8*)p)[sz];
! 112723: }
! 112724: db->lookaside.pEnd = p;
! 112725: db->lookaside.bEnabled = 1;
! 112726: db->lookaside.bMalloced = pBuf==0 ?1:0;
! 112727: }else{
! 112728: db->lookaside.pEnd = 0;
! 112729: db->lookaside.bEnabled = 0;
! 112730: db->lookaside.bMalloced = 0;
! 112731: }
! 112732: return SQLITE_OK;
! 112733: }
! 112734:
! 112735: /*
! 112736: ** Return the mutex associated with a database connection.
! 112737: */
! 112738: SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
! 112739: return db->mutex;
! 112740: }
! 112741:
! 112742: /*
! 112743: ** Free up as much memory as we can from the given database
! 112744: ** connection.
! 112745: */
! 112746: SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
! 112747: int i;
! 112748: sqlite3_mutex_enter(db->mutex);
! 112749: sqlite3BtreeEnterAll(db);
! 112750: for(i=0; i<db->nDb; i++){
! 112751: Btree *pBt = db->aDb[i].pBt;
! 112752: if( pBt ){
! 112753: Pager *pPager = sqlite3BtreePager(pBt);
! 112754: sqlite3PagerShrink(pPager);
! 112755: }
! 112756: }
! 112757: sqlite3BtreeLeaveAll(db);
! 112758: sqlite3_mutex_leave(db->mutex);
! 112759: return SQLITE_OK;
! 112760: }
! 112761:
! 112762: /*
! 112763: ** Configuration settings for an individual database connection
! 112764: */
! 112765: SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
! 112766: va_list ap;
! 112767: int rc;
! 112768: va_start(ap, op);
! 112769: switch( op ){
! 112770: case SQLITE_DBCONFIG_LOOKASIDE: {
! 112771: void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
! 112772: int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
! 112773: int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
! 112774: rc = setupLookaside(db, pBuf, sz, cnt);
! 112775: break;
! 112776: }
! 112777: default: {
! 112778: static const struct {
! 112779: int op; /* The opcode */
! 112780: u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */
! 112781: } aFlagOp[] = {
! 112782: { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys },
! 112783: { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger },
! 112784: };
! 112785: unsigned int i;
! 112786: rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
! 112787: for(i=0; i<ArraySize(aFlagOp); i++){
! 112788: if( aFlagOp[i].op==op ){
! 112789: int onoff = va_arg(ap, int);
! 112790: int *pRes = va_arg(ap, int*);
! 112791: int oldFlags = db->flags;
! 112792: if( onoff>0 ){
! 112793: db->flags |= aFlagOp[i].mask;
! 112794: }else if( onoff==0 ){
! 112795: db->flags &= ~aFlagOp[i].mask;
! 112796: }
! 112797: if( oldFlags!=db->flags ){
! 112798: sqlite3ExpirePreparedStatements(db);
! 112799: }
! 112800: if( pRes ){
! 112801: *pRes = (db->flags & aFlagOp[i].mask)!=0;
! 112802: }
! 112803: rc = SQLITE_OK;
! 112804: break;
! 112805: }
! 112806: }
! 112807: break;
! 112808: }
! 112809: }
! 112810: va_end(ap);
! 112811: return rc;
! 112812: }
! 112813:
! 112814:
! 112815: /*
! 112816: ** Return true if the buffer z[0..n-1] contains all spaces.
! 112817: */
! 112818: static int allSpaces(const char *z, int n){
! 112819: while( n>0 && z[n-1]==' ' ){ n--; }
! 112820: return n==0;
! 112821: }
! 112822:
! 112823: /*
! 112824: ** This is the default collating function named "BINARY" which is always
! 112825: ** available.
! 112826: **
! 112827: ** If the padFlag argument is not NULL then space padding at the end
! 112828: ** of strings is ignored. This implements the RTRIM collation.
! 112829: */
! 112830: static int binCollFunc(
! 112831: void *padFlag,
! 112832: int nKey1, const void *pKey1,
! 112833: int nKey2, const void *pKey2
! 112834: ){
! 112835: int rc, n;
! 112836: n = nKey1<nKey2 ? nKey1 : nKey2;
! 112837: rc = memcmp(pKey1, pKey2, n);
! 112838: if( rc==0 ){
! 112839: if( padFlag
! 112840: && allSpaces(((char*)pKey1)+n, nKey1-n)
! 112841: && allSpaces(((char*)pKey2)+n, nKey2-n)
! 112842: ){
! 112843: /* Leave rc unchanged at 0 */
! 112844: }else{
! 112845: rc = nKey1 - nKey2;
! 112846: }
! 112847: }
! 112848: return rc;
! 112849: }
! 112850:
! 112851: /*
! 112852: ** Another built-in collating sequence: NOCASE.
! 112853: **
! 112854: ** This collating sequence is intended to be used for "case independant
! 112855: ** comparison". SQLite's knowledge of upper and lower case equivalents
! 112856: ** extends only to the 26 characters used in the English language.
! 112857: **
! 112858: ** At the moment there is only a UTF-8 implementation.
! 112859: */
! 112860: static int nocaseCollatingFunc(
! 112861: void *NotUsed,
! 112862: int nKey1, const void *pKey1,
! 112863: int nKey2, const void *pKey2
! 112864: ){
! 112865: int r = sqlite3StrNICmp(
! 112866: (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
! 112867: UNUSED_PARAMETER(NotUsed);
! 112868: if( 0==r ){
! 112869: r = nKey1-nKey2;
! 112870: }
! 112871: return r;
! 112872: }
! 112873:
! 112874: /*
! 112875: ** Return the ROWID of the most recent insert
! 112876: */
! 112877: SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
! 112878: return db->lastRowid;
! 112879: }
! 112880:
! 112881: /*
! 112882: ** Return the number of changes in the most recent call to sqlite3_exec().
! 112883: */
! 112884: SQLITE_API int sqlite3_changes(sqlite3 *db){
! 112885: return db->nChange;
! 112886: }
! 112887:
! 112888: /*
! 112889: ** Return the number of changes since the database handle was opened.
! 112890: */
! 112891: SQLITE_API int sqlite3_total_changes(sqlite3 *db){
! 112892: return db->nTotalChange;
! 112893: }
! 112894:
! 112895: /*
! 112896: ** Close all open savepoints. This function only manipulates fields of the
! 112897: ** database handle object, it does not close any savepoints that may be open
! 112898: ** at the b-tree/pager level.
! 112899: */
! 112900: SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
! 112901: while( db->pSavepoint ){
! 112902: Savepoint *pTmp = db->pSavepoint;
! 112903: db->pSavepoint = pTmp->pNext;
! 112904: sqlite3DbFree(db, pTmp);
! 112905: }
! 112906: db->nSavepoint = 0;
! 112907: db->nStatement = 0;
! 112908: db->isTransactionSavepoint = 0;
! 112909: }
! 112910:
! 112911: /*
! 112912: ** Invoke the destructor function associated with FuncDef p, if any. Except,
! 112913: ** if this is not the last copy of the function, do not invoke it. Multiple
! 112914: ** copies of a single function are created when create_function() is called
! 112915: ** with SQLITE_ANY as the encoding.
! 112916: */
! 112917: static void functionDestroy(sqlite3 *db, FuncDef *p){
! 112918: FuncDestructor *pDestructor = p->pDestructor;
! 112919: if( pDestructor ){
! 112920: pDestructor->nRef--;
! 112921: if( pDestructor->nRef==0 ){
! 112922: pDestructor->xDestroy(pDestructor->pUserData);
! 112923: sqlite3DbFree(db, pDestructor);
! 112924: }
! 112925: }
! 112926: }
! 112927:
! 112928: /*
! 112929: ** Close an existing SQLite database
! 112930: */
! 112931: SQLITE_API int sqlite3_close(sqlite3 *db){
! 112932: HashElem *i; /* Hash table iterator */
! 112933: int j;
! 112934:
! 112935: if( !db ){
! 112936: return SQLITE_OK;
! 112937: }
! 112938: if( !sqlite3SafetyCheckSickOrOk(db) ){
! 112939: return SQLITE_MISUSE_BKPT;
! 112940: }
! 112941: sqlite3_mutex_enter(db->mutex);
! 112942:
! 112943: /* Force xDestroy calls on all virtual tables */
! 112944: sqlite3ResetInternalSchema(db, -1);
! 112945:
! 112946: /* If a transaction is open, the ResetInternalSchema() call above
! 112947: ** will not have called the xDisconnect() method on any virtual
! 112948: ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
! 112949: ** call will do so. We need to do this before the check for active
! 112950: ** SQL statements below, as the v-table implementation may be storing
! 112951: ** some prepared statements internally.
! 112952: */
! 112953: sqlite3VtabRollback(db);
! 112954:
! 112955: /* If there are any outstanding VMs, return SQLITE_BUSY. */
! 112956: if( db->pVdbe ){
! 112957: sqlite3Error(db, SQLITE_BUSY,
! 112958: "unable to close due to unfinalised statements");
! 112959: sqlite3_mutex_leave(db->mutex);
! 112960: return SQLITE_BUSY;
! 112961: }
! 112962: assert( sqlite3SafetyCheckSickOrOk(db) );
! 112963:
! 112964: for(j=0; j<db->nDb; j++){
! 112965: Btree *pBt = db->aDb[j].pBt;
! 112966: if( pBt && sqlite3BtreeIsInBackup(pBt) ){
! 112967: sqlite3Error(db, SQLITE_BUSY,
! 112968: "unable to close due to unfinished backup operation");
! 112969: sqlite3_mutex_leave(db->mutex);
! 112970: return SQLITE_BUSY;
! 112971: }
! 112972: }
! 112973:
! 112974: /* Free any outstanding Savepoint structures. */
! 112975: sqlite3CloseSavepoints(db);
! 112976:
! 112977: for(j=0; j<db->nDb; j++){
! 112978: struct Db *pDb = &db->aDb[j];
! 112979: if( pDb->pBt ){
! 112980: sqlite3BtreeClose(pDb->pBt);
! 112981: pDb->pBt = 0;
! 112982: if( j!=1 ){
! 112983: pDb->pSchema = 0;
! 112984: }
! 112985: }
! 112986: }
! 112987: sqlite3ResetInternalSchema(db, -1);
! 112988:
! 112989: /* Tell the code in notify.c that the connection no longer holds any
! 112990: ** locks and does not require any further unlock-notify callbacks.
! 112991: */
! 112992: sqlite3ConnectionClosed(db);
! 112993:
! 112994: assert( db->nDb<=2 );
! 112995: assert( db->aDb==db->aDbStatic );
! 112996: for(j=0; j<ArraySize(db->aFunc.a); j++){
! 112997: FuncDef *pNext, *pHash, *p;
! 112998: for(p=db->aFunc.a[j]; p; p=pHash){
! 112999: pHash = p->pHash;
! 113000: while( p ){
! 113001: functionDestroy(db, p);
! 113002: pNext = p->pNext;
! 113003: sqlite3DbFree(db, p);
! 113004: p = pNext;
! 113005: }
! 113006: }
! 113007: }
! 113008: for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
! 113009: CollSeq *pColl = (CollSeq *)sqliteHashData(i);
! 113010: /* Invoke any destructors registered for collation sequence user data. */
! 113011: for(j=0; j<3; j++){
! 113012: if( pColl[j].xDel ){
! 113013: pColl[j].xDel(pColl[j].pUser);
! 113014: }
! 113015: }
! 113016: sqlite3DbFree(db, pColl);
! 113017: }
! 113018: sqlite3HashClear(&db->aCollSeq);
! 113019: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 113020: for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
! 113021: Module *pMod = (Module *)sqliteHashData(i);
! 113022: if( pMod->xDestroy ){
! 113023: pMod->xDestroy(pMod->pAux);
! 113024: }
! 113025: sqlite3DbFree(db, pMod);
! 113026: }
! 113027: sqlite3HashClear(&db->aModule);
! 113028: #endif
! 113029:
! 113030: sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
! 113031: if( db->pErr ){
! 113032: sqlite3ValueFree(db->pErr);
! 113033: }
! 113034: sqlite3CloseExtensions(db);
! 113035:
! 113036: db->magic = SQLITE_MAGIC_ERROR;
! 113037:
! 113038: /* The temp-database schema is allocated differently from the other schema
! 113039: ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
! 113040: ** So it needs to be freed here. Todo: Why not roll the temp schema into
! 113041: ** the same sqliteMalloc() as the one that allocates the database
! 113042: ** structure?
! 113043: */
! 113044: sqlite3DbFree(db, db->aDb[1].pSchema);
! 113045: sqlite3_mutex_leave(db->mutex);
! 113046: db->magic = SQLITE_MAGIC_CLOSED;
! 113047: sqlite3_mutex_free(db->mutex);
! 113048: assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */
! 113049: if( db->lookaside.bMalloced ){
! 113050: sqlite3_free(db->lookaside.pStart);
! 113051: }
! 113052: sqlite3_free(db);
! 113053: return SQLITE_OK;
! 113054: }
! 113055:
! 113056: /*
! 113057: ** Rollback all database files.
! 113058: */
! 113059: SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db){
! 113060: int i;
! 113061: int inTrans = 0;
! 113062: assert( sqlite3_mutex_held(db->mutex) );
! 113063: sqlite3BeginBenignMalloc();
! 113064: for(i=0; i<db->nDb; i++){
! 113065: if( db->aDb[i].pBt ){
! 113066: if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
! 113067: inTrans = 1;
! 113068: }
! 113069: sqlite3BtreeRollback(db->aDb[i].pBt);
! 113070: db->aDb[i].inTrans = 0;
! 113071: }
! 113072: }
! 113073: sqlite3VtabRollback(db);
! 113074: sqlite3EndBenignMalloc();
! 113075:
! 113076: if( db->flags&SQLITE_InternChanges ){
! 113077: sqlite3ExpirePreparedStatements(db);
! 113078: sqlite3ResetInternalSchema(db, -1);
! 113079: }
! 113080:
! 113081: /* Any deferred constraint violations have now been resolved. */
! 113082: db->nDeferredCons = 0;
! 113083:
! 113084: /* If one has been configured, invoke the rollback-hook callback */
! 113085: if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
! 113086: db->xRollbackCallback(db->pRollbackArg);
! 113087: }
! 113088: }
! 113089:
! 113090: /*
! 113091: ** Return a static string that describes the kind of error specified in the
! 113092: ** argument.
! 113093: */
! 113094: SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
! 113095: static const char* const aMsg[] = {
! 113096: /* SQLITE_OK */ "not an error",
! 113097: /* SQLITE_ERROR */ "SQL logic error or missing database",
! 113098: /* SQLITE_INTERNAL */ 0,
! 113099: /* SQLITE_PERM */ "access permission denied",
! 113100: /* SQLITE_ABORT */ "callback requested query abort",
! 113101: /* SQLITE_BUSY */ "database is locked",
! 113102: /* SQLITE_LOCKED */ "database table is locked",
! 113103: /* SQLITE_NOMEM */ "out of memory",
! 113104: /* SQLITE_READONLY */ "attempt to write a readonly database",
! 113105: /* SQLITE_INTERRUPT */ "interrupted",
! 113106: /* SQLITE_IOERR */ "disk I/O error",
! 113107: /* SQLITE_CORRUPT */ "database disk image is malformed",
! 113108: /* SQLITE_NOTFOUND */ "unknown operation",
! 113109: /* SQLITE_FULL */ "database or disk is full",
! 113110: /* SQLITE_CANTOPEN */ "unable to open database file",
! 113111: /* SQLITE_PROTOCOL */ "locking protocol",
! 113112: /* SQLITE_EMPTY */ "table contains no data",
! 113113: /* SQLITE_SCHEMA */ "database schema has changed",
! 113114: /* SQLITE_TOOBIG */ "string or blob too big",
! 113115: /* SQLITE_CONSTRAINT */ "constraint failed",
! 113116: /* SQLITE_MISMATCH */ "datatype mismatch",
! 113117: /* SQLITE_MISUSE */ "library routine called out of sequence",
! 113118: /* SQLITE_NOLFS */ "large file support is disabled",
! 113119: /* SQLITE_AUTH */ "authorization denied",
! 113120: /* SQLITE_FORMAT */ "auxiliary database format error",
! 113121: /* SQLITE_RANGE */ "bind or column index out of range",
! 113122: /* SQLITE_NOTADB */ "file is encrypted or is not a database",
! 113123: };
! 113124: rc &= 0xff;
! 113125: if( ALWAYS(rc>=0) && rc<(int)(sizeof(aMsg)/sizeof(aMsg[0])) && aMsg[rc]!=0 ){
! 113126: return aMsg[rc];
! 113127: }else{
! 113128: return "unknown error";
! 113129: }
! 113130: }
! 113131:
! 113132: /*
! 113133: ** This routine implements a busy callback that sleeps and tries
! 113134: ** again until a timeout value is reached. The timeout value is
! 113135: ** an integer number of milliseconds passed in as the first
! 113136: ** argument.
! 113137: */
! 113138: static int sqliteDefaultBusyCallback(
! 113139: void *ptr, /* Database connection */
! 113140: int count /* Number of times table has been busy */
! 113141: ){
! 113142: #if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
! 113143: static const u8 delays[] =
! 113144: { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
! 113145: static const u8 totals[] =
! 113146: { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
! 113147: # define NDELAY ArraySize(delays)
! 113148: sqlite3 *db = (sqlite3 *)ptr;
! 113149: int timeout = db->busyTimeout;
! 113150: int delay, prior;
! 113151:
! 113152: assert( count>=0 );
! 113153: if( count < NDELAY ){
! 113154: delay = delays[count];
! 113155: prior = totals[count];
! 113156: }else{
! 113157: delay = delays[NDELAY-1];
! 113158: prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
! 113159: }
! 113160: if( prior + delay > timeout ){
! 113161: delay = timeout - prior;
! 113162: if( delay<=0 ) return 0;
! 113163: }
! 113164: sqlite3OsSleep(db->pVfs, delay*1000);
! 113165: return 1;
! 113166: #else
! 113167: sqlite3 *db = (sqlite3 *)ptr;
! 113168: int timeout = ((sqlite3 *)ptr)->busyTimeout;
! 113169: if( (count+1)*1000 > timeout ){
! 113170: return 0;
! 113171: }
! 113172: sqlite3OsSleep(db->pVfs, 1000000);
! 113173: return 1;
! 113174: #endif
! 113175: }
! 113176:
! 113177: /*
! 113178: ** Invoke the given busy handler.
! 113179: **
! 113180: ** This routine is called when an operation failed with a lock.
! 113181: ** If this routine returns non-zero, the lock is retried. If it
! 113182: ** returns 0, the operation aborts with an SQLITE_BUSY error.
! 113183: */
! 113184: SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
! 113185: int rc;
! 113186: if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
! 113187: rc = p->xFunc(p->pArg, p->nBusy);
! 113188: if( rc==0 ){
! 113189: p->nBusy = -1;
! 113190: }else{
! 113191: p->nBusy++;
! 113192: }
! 113193: return rc;
! 113194: }
! 113195:
! 113196: /*
! 113197: ** This routine sets the busy callback for an Sqlite database to the
! 113198: ** given callback function with the given argument.
! 113199: */
! 113200: SQLITE_API int sqlite3_busy_handler(
! 113201: sqlite3 *db,
! 113202: int (*xBusy)(void*,int),
! 113203: void *pArg
! 113204: ){
! 113205: sqlite3_mutex_enter(db->mutex);
! 113206: db->busyHandler.xFunc = xBusy;
! 113207: db->busyHandler.pArg = pArg;
! 113208: db->busyHandler.nBusy = 0;
! 113209: sqlite3_mutex_leave(db->mutex);
! 113210: return SQLITE_OK;
! 113211: }
! 113212:
! 113213: #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
! 113214: /*
! 113215: ** This routine sets the progress callback for an Sqlite database to the
! 113216: ** given callback function with the given argument. The progress callback will
! 113217: ** be invoked every nOps opcodes.
! 113218: */
! 113219: SQLITE_API void sqlite3_progress_handler(
! 113220: sqlite3 *db,
! 113221: int nOps,
! 113222: int (*xProgress)(void*),
! 113223: void *pArg
! 113224: ){
! 113225: sqlite3_mutex_enter(db->mutex);
! 113226: if( nOps>0 ){
! 113227: db->xProgress = xProgress;
! 113228: db->nProgressOps = nOps;
! 113229: db->pProgressArg = pArg;
! 113230: }else{
! 113231: db->xProgress = 0;
! 113232: db->nProgressOps = 0;
! 113233: db->pProgressArg = 0;
! 113234: }
! 113235: sqlite3_mutex_leave(db->mutex);
! 113236: }
! 113237: #endif
! 113238:
! 113239:
! 113240: /*
! 113241: ** This routine installs a default busy handler that waits for the
! 113242: ** specified number of milliseconds before returning 0.
! 113243: */
! 113244: SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
! 113245: if( ms>0 ){
! 113246: db->busyTimeout = ms;
! 113247: sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
! 113248: }else{
! 113249: sqlite3_busy_handler(db, 0, 0);
! 113250: }
! 113251: return SQLITE_OK;
! 113252: }
! 113253:
! 113254: /*
! 113255: ** Cause any pending operation to stop at its earliest opportunity.
! 113256: */
! 113257: SQLITE_API void sqlite3_interrupt(sqlite3 *db){
! 113258: db->u1.isInterrupted = 1;
! 113259: }
! 113260:
! 113261:
! 113262: /*
! 113263: ** This function is exactly the same as sqlite3_create_function(), except
! 113264: ** that it is designed to be called by internal code. The difference is
! 113265: ** that if a malloc() fails in sqlite3_create_function(), an error code
! 113266: ** is returned and the mallocFailed flag cleared.
! 113267: */
! 113268: SQLITE_PRIVATE int sqlite3CreateFunc(
! 113269: sqlite3 *db,
! 113270: const char *zFunctionName,
! 113271: int nArg,
! 113272: int enc,
! 113273: void *pUserData,
! 113274: void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
! 113275: void (*xStep)(sqlite3_context*,int,sqlite3_value **),
! 113276: void (*xFinal)(sqlite3_context*),
! 113277: FuncDestructor *pDestructor
! 113278: ){
! 113279: FuncDef *p;
! 113280: int nName;
! 113281:
! 113282: assert( sqlite3_mutex_held(db->mutex) );
! 113283: if( zFunctionName==0 ||
! 113284: (xFunc && (xFinal || xStep)) ||
! 113285: (!xFunc && (xFinal && !xStep)) ||
! 113286: (!xFunc && (!xFinal && xStep)) ||
! 113287: (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
! 113288: (255<(nName = sqlite3Strlen30( zFunctionName))) ){
! 113289: return SQLITE_MISUSE_BKPT;
! 113290: }
! 113291:
! 113292: #ifndef SQLITE_OMIT_UTF16
! 113293: /* If SQLITE_UTF16 is specified as the encoding type, transform this
! 113294: ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
! 113295: ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
! 113296: **
! 113297: ** If SQLITE_ANY is specified, add three versions of the function
! 113298: ** to the hash table.
! 113299: */
! 113300: if( enc==SQLITE_UTF16 ){
! 113301: enc = SQLITE_UTF16NATIVE;
! 113302: }else if( enc==SQLITE_ANY ){
! 113303: int rc;
! 113304: rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
! 113305: pUserData, xFunc, xStep, xFinal, pDestructor);
! 113306: if( rc==SQLITE_OK ){
! 113307: rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
! 113308: pUserData, xFunc, xStep, xFinal, pDestructor);
! 113309: }
! 113310: if( rc!=SQLITE_OK ){
! 113311: return rc;
! 113312: }
! 113313: enc = SQLITE_UTF16BE;
! 113314: }
! 113315: #else
! 113316: enc = SQLITE_UTF8;
! 113317: #endif
! 113318:
! 113319: /* Check if an existing function is being overridden or deleted. If so,
! 113320: ** and there are active VMs, then return SQLITE_BUSY. If a function
! 113321: ** is being overridden/deleted but there are no active VMs, allow the
! 113322: ** operation to continue but invalidate all precompiled statements.
! 113323: */
! 113324: p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
! 113325: if( p && p->iPrefEnc==enc && p->nArg==nArg ){
! 113326: if( db->activeVdbeCnt ){
! 113327: sqlite3Error(db, SQLITE_BUSY,
! 113328: "unable to delete/modify user-function due to active statements");
! 113329: assert( !db->mallocFailed );
! 113330: return SQLITE_BUSY;
! 113331: }else{
! 113332: sqlite3ExpirePreparedStatements(db);
! 113333: }
! 113334: }
! 113335:
! 113336: p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
! 113337: assert(p || db->mallocFailed);
! 113338: if( !p ){
! 113339: return SQLITE_NOMEM;
! 113340: }
! 113341:
! 113342: /* If an older version of the function with a configured destructor is
! 113343: ** being replaced invoke the destructor function here. */
! 113344: functionDestroy(db, p);
! 113345:
! 113346: if( pDestructor ){
! 113347: pDestructor->nRef++;
! 113348: }
! 113349: p->pDestructor = pDestructor;
! 113350: p->flags = 0;
! 113351: p->xFunc = xFunc;
! 113352: p->xStep = xStep;
! 113353: p->xFinalize = xFinal;
! 113354: p->pUserData = pUserData;
! 113355: p->nArg = (u16)nArg;
! 113356: return SQLITE_OK;
! 113357: }
! 113358:
! 113359: /*
! 113360: ** Create new user functions.
! 113361: */
! 113362: SQLITE_API int sqlite3_create_function(
! 113363: sqlite3 *db,
! 113364: const char *zFunc,
! 113365: int nArg,
! 113366: int enc,
! 113367: void *p,
! 113368: void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
! 113369: void (*xStep)(sqlite3_context*,int,sqlite3_value **),
! 113370: void (*xFinal)(sqlite3_context*)
! 113371: ){
! 113372: return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
! 113373: xFinal, 0);
! 113374: }
! 113375:
! 113376: SQLITE_API int sqlite3_create_function_v2(
! 113377: sqlite3 *db,
! 113378: const char *zFunc,
! 113379: int nArg,
! 113380: int enc,
! 113381: void *p,
! 113382: void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
! 113383: void (*xStep)(sqlite3_context*,int,sqlite3_value **),
! 113384: void (*xFinal)(sqlite3_context*),
! 113385: void (*xDestroy)(void *)
! 113386: ){
! 113387: int rc = SQLITE_ERROR;
! 113388: FuncDestructor *pArg = 0;
! 113389: sqlite3_mutex_enter(db->mutex);
! 113390: if( xDestroy ){
! 113391: pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
! 113392: if( !pArg ){
! 113393: xDestroy(p);
! 113394: goto out;
! 113395: }
! 113396: pArg->xDestroy = xDestroy;
! 113397: pArg->pUserData = p;
! 113398: }
! 113399: rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
! 113400: if( pArg && pArg->nRef==0 ){
! 113401: assert( rc!=SQLITE_OK );
! 113402: xDestroy(p);
! 113403: sqlite3DbFree(db, pArg);
! 113404: }
! 113405:
! 113406: out:
! 113407: rc = sqlite3ApiExit(db, rc);
! 113408: sqlite3_mutex_leave(db->mutex);
! 113409: return rc;
! 113410: }
! 113411:
! 113412: #ifndef SQLITE_OMIT_UTF16
! 113413: SQLITE_API int sqlite3_create_function16(
! 113414: sqlite3 *db,
! 113415: const void *zFunctionName,
! 113416: int nArg,
! 113417: int eTextRep,
! 113418: void *p,
! 113419: void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
! 113420: void (*xStep)(sqlite3_context*,int,sqlite3_value**),
! 113421: void (*xFinal)(sqlite3_context*)
! 113422: ){
! 113423: int rc;
! 113424: char *zFunc8;
! 113425: sqlite3_mutex_enter(db->mutex);
! 113426: assert( !db->mallocFailed );
! 113427: zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
! 113428: rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
! 113429: sqlite3DbFree(db, zFunc8);
! 113430: rc = sqlite3ApiExit(db, rc);
! 113431: sqlite3_mutex_leave(db->mutex);
! 113432: return rc;
! 113433: }
! 113434: #endif
! 113435:
! 113436:
! 113437: /*
! 113438: ** Declare that a function has been overloaded by a virtual table.
! 113439: **
! 113440: ** If the function already exists as a regular global function, then
! 113441: ** this routine is a no-op. If the function does not exist, then create
! 113442: ** a new one that always throws a run-time error.
! 113443: **
! 113444: ** When virtual tables intend to provide an overloaded function, they
! 113445: ** should call this routine to make sure the global function exists.
! 113446: ** A global function must exist in order for name resolution to work
! 113447: ** properly.
! 113448: */
! 113449: SQLITE_API int sqlite3_overload_function(
! 113450: sqlite3 *db,
! 113451: const char *zName,
! 113452: int nArg
! 113453: ){
! 113454: int nName = sqlite3Strlen30(zName);
! 113455: int rc = SQLITE_OK;
! 113456: sqlite3_mutex_enter(db->mutex);
! 113457: if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
! 113458: rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
! 113459: 0, sqlite3InvalidFunction, 0, 0, 0);
! 113460: }
! 113461: rc = sqlite3ApiExit(db, rc);
! 113462: sqlite3_mutex_leave(db->mutex);
! 113463: return rc;
! 113464: }
! 113465:
! 113466: #ifndef SQLITE_OMIT_TRACE
! 113467: /*
! 113468: ** Register a trace function. The pArg from the previously registered trace
! 113469: ** is returned.
! 113470: **
! 113471: ** A NULL trace function means that no tracing is executes. A non-NULL
! 113472: ** trace is a pointer to a function that is invoked at the start of each
! 113473: ** SQL statement.
! 113474: */
! 113475: SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
! 113476: void *pOld;
! 113477: sqlite3_mutex_enter(db->mutex);
! 113478: pOld = db->pTraceArg;
! 113479: db->xTrace = xTrace;
! 113480: db->pTraceArg = pArg;
! 113481: sqlite3_mutex_leave(db->mutex);
! 113482: return pOld;
! 113483: }
! 113484: /*
! 113485: ** Register a profile function. The pArg from the previously registered
! 113486: ** profile function is returned.
! 113487: **
! 113488: ** A NULL profile function means that no profiling is executes. A non-NULL
! 113489: ** profile is a pointer to a function that is invoked at the conclusion of
! 113490: ** each SQL statement that is run.
! 113491: */
! 113492: SQLITE_API void *sqlite3_profile(
! 113493: sqlite3 *db,
! 113494: void (*xProfile)(void*,const char*,sqlite_uint64),
! 113495: void *pArg
! 113496: ){
! 113497: void *pOld;
! 113498: sqlite3_mutex_enter(db->mutex);
! 113499: pOld = db->pProfileArg;
! 113500: db->xProfile = xProfile;
! 113501: db->pProfileArg = pArg;
! 113502: sqlite3_mutex_leave(db->mutex);
! 113503: return pOld;
! 113504: }
! 113505: #endif /* SQLITE_OMIT_TRACE */
! 113506:
! 113507: /*** EXPERIMENTAL ***
! 113508: **
! 113509: ** Register a function to be invoked when a transaction comments.
! 113510: ** If the invoked function returns non-zero, then the commit becomes a
! 113511: ** rollback.
! 113512: */
! 113513: SQLITE_API void *sqlite3_commit_hook(
! 113514: sqlite3 *db, /* Attach the hook to this database */
! 113515: int (*xCallback)(void*), /* Function to invoke on each commit */
! 113516: void *pArg /* Argument to the function */
! 113517: ){
! 113518: void *pOld;
! 113519: sqlite3_mutex_enter(db->mutex);
! 113520: pOld = db->pCommitArg;
! 113521: db->xCommitCallback = xCallback;
! 113522: db->pCommitArg = pArg;
! 113523: sqlite3_mutex_leave(db->mutex);
! 113524: return pOld;
! 113525: }
! 113526:
! 113527: /*
! 113528: ** Register a callback to be invoked each time a row is updated,
! 113529: ** inserted or deleted using this database connection.
! 113530: */
! 113531: SQLITE_API void *sqlite3_update_hook(
! 113532: sqlite3 *db, /* Attach the hook to this database */
! 113533: void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
! 113534: void *pArg /* Argument to the function */
! 113535: ){
! 113536: void *pRet;
! 113537: sqlite3_mutex_enter(db->mutex);
! 113538: pRet = db->pUpdateArg;
! 113539: db->xUpdateCallback = xCallback;
! 113540: db->pUpdateArg = pArg;
! 113541: sqlite3_mutex_leave(db->mutex);
! 113542: return pRet;
! 113543: }
! 113544:
! 113545: /*
! 113546: ** Register a callback to be invoked each time a transaction is rolled
! 113547: ** back by this database connection.
! 113548: */
! 113549: SQLITE_API void *sqlite3_rollback_hook(
! 113550: sqlite3 *db, /* Attach the hook to this database */
! 113551: void (*xCallback)(void*), /* Callback function */
! 113552: void *pArg /* Argument to the function */
! 113553: ){
! 113554: void *pRet;
! 113555: sqlite3_mutex_enter(db->mutex);
! 113556: pRet = db->pRollbackArg;
! 113557: db->xRollbackCallback = xCallback;
! 113558: db->pRollbackArg = pArg;
! 113559: sqlite3_mutex_leave(db->mutex);
! 113560: return pRet;
! 113561: }
! 113562:
! 113563: #ifndef SQLITE_OMIT_WAL
! 113564: /*
! 113565: ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
! 113566: ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
! 113567: ** is greater than sqlite3.pWalArg cast to an integer (the value configured by
! 113568: ** wal_autocheckpoint()).
! 113569: */
! 113570: SQLITE_PRIVATE int sqlite3WalDefaultHook(
! 113571: void *pClientData, /* Argument */
! 113572: sqlite3 *db, /* Connection */
! 113573: const char *zDb, /* Database */
! 113574: int nFrame /* Size of WAL */
! 113575: ){
! 113576: if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
! 113577: sqlite3BeginBenignMalloc();
! 113578: sqlite3_wal_checkpoint(db, zDb);
! 113579: sqlite3EndBenignMalloc();
! 113580: }
! 113581: return SQLITE_OK;
! 113582: }
! 113583: #endif /* SQLITE_OMIT_WAL */
! 113584:
! 113585: /*
! 113586: ** Configure an sqlite3_wal_hook() callback to automatically checkpoint
! 113587: ** a database after committing a transaction if there are nFrame or
! 113588: ** more frames in the log file. Passing zero or a negative value as the
! 113589: ** nFrame parameter disables automatic checkpoints entirely.
! 113590: **
! 113591: ** The callback registered by this function replaces any existing callback
! 113592: ** registered using sqlite3_wal_hook(). Likewise, registering a callback
! 113593: ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
! 113594: ** configured by this function.
! 113595: */
! 113596: SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
! 113597: #ifdef SQLITE_OMIT_WAL
! 113598: UNUSED_PARAMETER(db);
! 113599: UNUSED_PARAMETER(nFrame);
! 113600: #else
! 113601: if( nFrame>0 ){
! 113602: sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
! 113603: }else{
! 113604: sqlite3_wal_hook(db, 0, 0);
! 113605: }
! 113606: #endif
! 113607: return SQLITE_OK;
! 113608: }
! 113609:
! 113610: /*
! 113611: ** Register a callback to be invoked each time a transaction is written
! 113612: ** into the write-ahead-log by this database connection.
! 113613: */
! 113614: SQLITE_API void *sqlite3_wal_hook(
! 113615: sqlite3 *db, /* Attach the hook to this db handle */
! 113616: int(*xCallback)(void *, sqlite3*, const char*, int),
! 113617: void *pArg /* First argument passed to xCallback() */
! 113618: ){
! 113619: #ifndef SQLITE_OMIT_WAL
! 113620: void *pRet;
! 113621: sqlite3_mutex_enter(db->mutex);
! 113622: pRet = db->pWalArg;
! 113623: db->xWalCallback = xCallback;
! 113624: db->pWalArg = pArg;
! 113625: sqlite3_mutex_leave(db->mutex);
! 113626: return pRet;
! 113627: #else
! 113628: return 0;
! 113629: #endif
! 113630: }
! 113631:
! 113632: /*
! 113633: ** Checkpoint database zDb.
! 113634: */
! 113635: SQLITE_API int sqlite3_wal_checkpoint_v2(
! 113636: sqlite3 *db, /* Database handle */
! 113637: const char *zDb, /* Name of attached database (or NULL) */
! 113638: int eMode, /* SQLITE_CHECKPOINT_* value */
! 113639: int *pnLog, /* OUT: Size of WAL log in frames */
! 113640: int *pnCkpt /* OUT: Total number of frames checkpointed */
! 113641: ){
! 113642: #ifdef SQLITE_OMIT_WAL
! 113643: return SQLITE_OK;
! 113644: #else
! 113645: int rc; /* Return code */
! 113646: int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
! 113647:
! 113648: /* Initialize the output variables to -1 in case an error occurs. */
! 113649: if( pnLog ) *pnLog = -1;
! 113650: if( pnCkpt ) *pnCkpt = -1;
! 113651:
! 113652: assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
! 113653: assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
! 113654: assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
! 113655: if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
! 113656: return SQLITE_MISUSE;
! 113657: }
! 113658:
! 113659: sqlite3_mutex_enter(db->mutex);
! 113660: if( zDb && zDb[0] ){
! 113661: iDb = sqlite3FindDbName(db, zDb);
! 113662: }
! 113663: if( iDb<0 ){
! 113664: rc = SQLITE_ERROR;
! 113665: sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
! 113666: }else{
! 113667: rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
! 113668: sqlite3Error(db, rc, 0);
! 113669: }
! 113670: rc = sqlite3ApiExit(db, rc);
! 113671: sqlite3_mutex_leave(db->mutex);
! 113672: return rc;
! 113673: #endif
! 113674: }
! 113675:
! 113676:
! 113677: /*
! 113678: ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
! 113679: ** to contains a zero-length string, all attached databases are
! 113680: ** checkpointed.
! 113681: */
! 113682: SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
! 113683: return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
! 113684: }
! 113685:
! 113686: #ifndef SQLITE_OMIT_WAL
! 113687: /*
! 113688: ** Run a checkpoint on database iDb. This is a no-op if database iDb is
! 113689: ** not currently open in WAL mode.
! 113690: **
! 113691: ** If a transaction is open on the database being checkpointed, this
! 113692: ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
! 113693: ** an error occurs while running the checkpoint, an SQLite error code is
! 113694: ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
! 113695: **
! 113696: ** The mutex on database handle db should be held by the caller. The mutex
! 113697: ** associated with the specific b-tree being checkpointed is taken by
! 113698: ** this function while the checkpoint is running.
! 113699: **
! 113700: ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
! 113701: ** checkpointed. If an error is encountered it is returned immediately -
! 113702: ** no attempt is made to checkpoint any remaining databases.
! 113703: **
! 113704: ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
! 113705: */
! 113706: SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
! 113707: int rc = SQLITE_OK; /* Return code */
! 113708: int i; /* Used to iterate through attached dbs */
! 113709: int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
! 113710:
! 113711: assert( sqlite3_mutex_held(db->mutex) );
! 113712: assert( !pnLog || *pnLog==-1 );
! 113713: assert( !pnCkpt || *pnCkpt==-1 );
! 113714:
! 113715: for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
! 113716: if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
! 113717: rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
! 113718: pnLog = 0;
! 113719: pnCkpt = 0;
! 113720: if( rc==SQLITE_BUSY ){
! 113721: bBusy = 1;
! 113722: rc = SQLITE_OK;
! 113723: }
! 113724: }
! 113725: }
! 113726:
! 113727: return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
! 113728: }
! 113729: #endif /* SQLITE_OMIT_WAL */
! 113730:
! 113731: /*
! 113732: ** This function returns true if main-memory should be used instead of
! 113733: ** a temporary file for transient pager files and statement journals.
! 113734: ** The value returned depends on the value of db->temp_store (runtime
! 113735: ** parameter) and the compile time value of SQLITE_TEMP_STORE. The
! 113736: ** following table describes the relationship between these two values
! 113737: ** and this functions return value.
! 113738: **
! 113739: ** SQLITE_TEMP_STORE db->temp_store Location of temporary database
! 113740: ** ----------------- -------------- ------------------------------
! 113741: ** 0 any file (return 0)
! 113742: ** 1 1 file (return 0)
! 113743: ** 1 2 memory (return 1)
! 113744: ** 1 0 file (return 0)
! 113745: ** 2 1 file (return 0)
! 113746: ** 2 2 memory (return 1)
! 113747: ** 2 0 memory (return 1)
! 113748: ** 3 any memory (return 1)
! 113749: */
! 113750: SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
! 113751: #if SQLITE_TEMP_STORE==1
! 113752: return ( db->temp_store==2 );
! 113753: #endif
! 113754: #if SQLITE_TEMP_STORE==2
! 113755: return ( db->temp_store!=1 );
! 113756: #endif
! 113757: #if SQLITE_TEMP_STORE==3
! 113758: return 1;
! 113759: #endif
! 113760: #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
! 113761: return 0;
! 113762: #endif
! 113763: }
! 113764:
! 113765: /*
! 113766: ** Return UTF-8 encoded English language explanation of the most recent
! 113767: ** error.
! 113768: */
! 113769: SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
! 113770: const char *z;
! 113771: if( !db ){
! 113772: return sqlite3ErrStr(SQLITE_NOMEM);
! 113773: }
! 113774: if( !sqlite3SafetyCheckSickOrOk(db) ){
! 113775: return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
! 113776: }
! 113777: sqlite3_mutex_enter(db->mutex);
! 113778: if( db->mallocFailed ){
! 113779: z = sqlite3ErrStr(SQLITE_NOMEM);
! 113780: }else{
! 113781: z = (char*)sqlite3_value_text(db->pErr);
! 113782: assert( !db->mallocFailed );
! 113783: if( z==0 ){
! 113784: z = sqlite3ErrStr(db->errCode);
! 113785: }
! 113786: }
! 113787: sqlite3_mutex_leave(db->mutex);
! 113788: return z;
! 113789: }
! 113790:
! 113791: #ifndef SQLITE_OMIT_UTF16
! 113792: /*
! 113793: ** Return UTF-16 encoded English language explanation of the most recent
! 113794: ** error.
! 113795: */
! 113796: SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
! 113797: static const u16 outOfMem[] = {
! 113798: 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
! 113799: };
! 113800: static const u16 misuse[] = {
! 113801: 'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ',
! 113802: 'r', 'o', 'u', 't', 'i', 'n', 'e', ' ',
! 113803: 'c', 'a', 'l', 'l', 'e', 'd', ' ',
! 113804: 'o', 'u', 't', ' ',
! 113805: 'o', 'f', ' ',
! 113806: 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
! 113807: };
! 113808:
! 113809: const void *z;
! 113810: if( !db ){
! 113811: return (void *)outOfMem;
! 113812: }
! 113813: if( !sqlite3SafetyCheckSickOrOk(db) ){
! 113814: return (void *)misuse;
! 113815: }
! 113816: sqlite3_mutex_enter(db->mutex);
! 113817: if( db->mallocFailed ){
! 113818: z = (void *)outOfMem;
! 113819: }else{
! 113820: z = sqlite3_value_text16(db->pErr);
! 113821: if( z==0 ){
! 113822: sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
! 113823: SQLITE_UTF8, SQLITE_STATIC);
! 113824: z = sqlite3_value_text16(db->pErr);
! 113825: }
! 113826: /* A malloc() may have failed within the call to sqlite3_value_text16()
! 113827: ** above. If this is the case, then the db->mallocFailed flag needs to
! 113828: ** be cleared before returning. Do this directly, instead of via
! 113829: ** sqlite3ApiExit(), to avoid setting the database handle error message.
! 113830: */
! 113831: db->mallocFailed = 0;
! 113832: }
! 113833: sqlite3_mutex_leave(db->mutex);
! 113834: return z;
! 113835: }
! 113836: #endif /* SQLITE_OMIT_UTF16 */
! 113837:
! 113838: /*
! 113839: ** Return the most recent error code generated by an SQLite routine. If NULL is
! 113840: ** passed to this function, we assume a malloc() failed during sqlite3_open().
! 113841: */
! 113842: SQLITE_API int sqlite3_errcode(sqlite3 *db){
! 113843: if( db && !sqlite3SafetyCheckSickOrOk(db) ){
! 113844: return SQLITE_MISUSE_BKPT;
! 113845: }
! 113846: if( !db || db->mallocFailed ){
! 113847: return SQLITE_NOMEM;
! 113848: }
! 113849: return db->errCode & db->errMask;
! 113850: }
! 113851: SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
! 113852: if( db && !sqlite3SafetyCheckSickOrOk(db) ){
! 113853: return SQLITE_MISUSE_BKPT;
! 113854: }
! 113855: if( !db || db->mallocFailed ){
! 113856: return SQLITE_NOMEM;
! 113857: }
! 113858: return db->errCode;
! 113859: }
! 113860:
! 113861: /*
! 113862: ** Create a new collating function for database "db". The name is zName
! 113863: ** and the encoding is enc.
! 113864: */
! 113865: static int createCollation(
! 113866: sqlite3* db,
! 113867: const char *zName,
! 113868: u8 enc,
! 113869: void* pCtx,
! 113870: int(*xCompare)(void*,int,const void*,int,const void*),
! 113871: void(*xDel)(void*)
! 113872: ){
! 113873: CollSeq *pColl;
! 113874: int enc2;
! 113875: int nName = sqlite3Strlen30(zName);
! 113876:
! 113877: assert( sqlite3_mutex_held(db->mutex) );
! 113878:
! 113879: /* If SQLITE_UTF16 is specified as the encoding type, transform this
! 113880: ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
! 113881: ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
! 113882: */
! 113883: enc2 = enc;
! 113884: testcase( enc2==SQLITE_UTF16 );
! 113885: testcase( enc2==SQLITE_UTF16_ALIGNED );
! 113886: if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
! 113887: enc2 = SQLITE_UTF16NATIVE;
! 113888: }
! 113889: if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
! 113890: return SQLITE_MISUSE_BKPT;
! 113891: }
! 113892:
! 113893: /* Check if this call is removing or replacing an existing collation
! 113894: ** sequence. If so, and there are active VMs, return busy. If there
! 113895: ** are no active VMs, invalidate any pre-compiled statements.
! 113896: */
! 113897: pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
! 113898: if( pColl && pColl->xCmp ){
! 113899: if( db->activeVdbeCnt ){
! 113900: sqlite3Error(db, SQLITE_BUSY,
! 113901: "unable to delete/modify collation sequence due to active statements");
! 113902: return SQLITE_BUSY;
! 113903: }
! 113904: sqlite3ExpirePreparedStatements(db);
! 113905:
! 113906: /* If collation sequence pColl was created directly by a call to
! 113907: ** sqlite3_create_collation, and not generated by synthCollSeq(),
! 113908: ** then any copies made by synthCollSeq() need to be invalidated.
! 113909: ** Also, collation destructor - CollSeq.xDel() - function may need
! 113910: ** to be called.
! 113911: */
! 113912: if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
! 113913: CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
! 113914: int j;
! 113915: for(j=0; j<3; j++){
! 113916: CollSeq *p = &aColl[j];
! 113917: if( p->enc==pColl->enc ){
! 113918: if( p->xDel ){
! 113919: p->xDel(p->pUser);
! 113920: }
! 113921: p->xCmp = 0;
! 113922: }
! 113923: }
! 113924: }
! 113925: }
! 113926:
! 113927: pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
! 113928: if( pColl==0 ) return SQLITE_NOMEM;
! 113929: pColl->xCmp = xCompare;
! 113930: pColl->pUser = pCtx;
! 113931: pColl->xDel = xDel;
! 113932: pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
! 113933: sqlite3Error(db, SQLITE_OK, 0);
! 113934: return SQLITE_OK;
! 113935: }
! 113936:
! 113937:
! 113938: /*
! 113939: ** This array defines hard upper bounds on limit values. The
! 113940: ** initializer must be kept in sync with the SQLITE_LIMIT_*
! 113941: ** #defines in sqlite3.h.
! 113942: */
! 113943: static const int aHardLimit[] = {
! 113944: SQLITE_MAX_LENGTH,
! 113945: SQLITE_MAX_SQL_LENGTH,
! 113946: SQLITE_MAX_COLUMN,
! 113947: SQLITE_MAX_EXPR_DEPTH,
! 113948: SQLITE_MAX_COMPOUND_SELECT,
! 113949: SQLITE_MAX_VDBE_OP,
! 113950: SQLITE_MAX_FUNCTION_ARG,
! 113951: SQLITE_MAX_ATTACHED,
! 113952: SQLITE_MAX_LIKE_PATTERN_LENGTH,
! 113953: SQLITE_MAX_VARIABLE_NUMBER,
! 113954: SQLITE_MAX_TRIGGER_DEPTH,
! 113955: };
! 113956:
! 113957: /*
! 113958: ** Make sure the hard limits are set to reasonable values
! 113959: */
! 113960: #if SQLITE_MAX_LENGTH<100
! 113961: # error SQLITE_MAX_LENGTH must be at least 100
! 113962: #endif
! 113963: #if SQLITE_MAX_SQL_LENGTH<100
! 113964: # error SQLITE_MAX_SQL_LENGTH must be at least 100
! 113965: #endif
! 113966: #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
! 113967: # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
! 113968: #endif
! 113969: #if SQLITE_MAX_COMPOUND_SELECT<2
! 113970: # error SQLITE_MAX_COMPOUND_SELECT must be at least 2
! 113971: #endif
! 113972: #if SQLITE_MAX_VDBE_OP<40
! 113973: # error SQLITE_MAX_VDBE_OP must be at least 40
! 113974: #endif
! 113975: #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
! 113976: # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
! 113977: #endif
! 113978: #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
! 113979: # error SQLITE_MAX_ATTACHED must be between 0 and 62
! 113980: #endif
! 113981: #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
! 113982: # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
! 113983: #endif
! 113984: #if SQLITE_MAX_COLUMN>32767
! 113985: # error SQLITE_MAX_COLUMN must not exceed 32767
! 113986: #endif
! 113987: #if SQLITE_MAX_TRIGGER_DEPTH<1
! 113988: # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
! 113989: #endif
! 113990:
! 113991:
! 113992: /*
! 113993: ** Change the value of a limit. Report the old value.
! 113994: ** If an invalid limit index is supplied, report -1.
! 113995: ** Make no changes but still report the old value if the
! 113996: ** new limit is negative.
! 113997: **
! 113998: ** A new lower limit does not shrink existing constructs.
! 113999: ** It merely prevents new constructs that exceed the limit
! 114000: ** from forming.
! 114001: */
! 114002: SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
! 114003: int oldLimit;
! 114004:
! 114005:
! 114006: /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
! 114007: ** there is a hard upper bound set at compile-time by a C preprocessor
! 114008: ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
! 114009: ** "_MAX_".)
! 114010: */
! 114011: assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
! 114012: assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
! 114013: assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
! 114014: assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
! 114015: assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
! 114016: assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
! 114017: assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
! 114018: assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
! 114019: assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
! 114020: SQLITE_MAX_LIKE_PATTERN_LENGTH );
! 114021: assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
! 114022: assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
! 114023: assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) );
! 114024:
! 114025:
! 114026: if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
! 114027: return -1;
! 114028: }
! 114029: oldLimit = db->aLimit[limitId];
! 114030: if( newLimit>=0 ){ /* IMP: R-52476-28732 */
! 114031: if( newLimit>aHardLimit[limitId] ){
! 114032: newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
! 114033: }
! 114034: db->aLimit[limitId] = newLimit;
! 114035: }
! 114036: return oldLimit; /* IMP: R-53341-35419 */
! 114037: }
! 114038:
! 114039: /*
! 114040: ** This function is used to parse both URIs and non-URI filenames passed by the
! 114041: ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
! 114042: ** URIs specified as part of ATTACH statements.
! 114043: **
! 114044: ** The first argument to this function is the name of the VFS to use (or
! 114045: ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
! 114046: ** query parameter. The second argument contains the URI (or non-URI filename)
! 114047: ** itself. When this function is called the *pFlags variable should contain
! 114048: ** the default flags to open the database handle with. The value stored in
! 114049: ** *pFlags may be updated before returning if the URI filename contains
! 114050: ** "cache=xxx" or "mode=xxx" query parameters.
! 114051: **
! 114052: ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
! 114053: ** the VFS that should be used to open the database file. *pzFile is set to
! 114054: ** point to a buffer containing the name of the file to open. It is the
! 114055: ** responsibility of the caller to eventually call sqlite3_free() to release
! 114056: ** this buffer.
! 114057: **
! 114058: ** If an error occurs, then an SQLite error code is returned and *pzErrMsg
! 114059: ** may be set to point to a buffer containing an English language error
! 114060: ** message. It is the responsibility of the caller to eventually release
! 114061: ** this buffer by calling sqlite3_free().
! 114062: */
! 114063: SQLITE_PRIVATE int sqlite3ParseUri(
! 114064: const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
! 114065: const char *zUri, /* Nul-terminated URI to parse */
! 114066: unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
! 114067: sqlite3_vfs **ppVfs, /* OUT: VFS to use */
! 114068: char **pzFile, /* OUT: Filename component of URI */
! 114069: char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
! 114070: ){
! 114071: int rc = SQLITE_OK;
! 114072: unsigned int flags = *pFlags;
! 114073: const char *zVfs = zDefaultVfs;
! 114074: char *zFile;
! 114075: char c;
! 114076: int nUri = sqlite3Strlen30(zUri);
! 114077:
! 114078: assert( *pzErrMsg==0 );
! 114079:
! 114080: if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri)
! 114081: && nUri>=5 && memcmp(zUri, "file:", 5)==0
! 114082: ){
! 114083: char *zOpt;
! 114084: int eState; /* Parser state when parsing URI */
! 114085: int iIn; /* Input character index */
! 114086: int iOut = 0; /* Output character index */
! 114087: int nByte = nUri+2; /* Bytes of space to allocate */
! 114088:
! 114089: /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
! 114090: ** method that there may be extra parameters following the file-name. */
! 114091: flags |= SQLITE_OPEN_URI;
! 114092:
! 114093: for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
! 114094: zFile = sqlite3_malloc(nByte);
! 114095: if( !zFile ) return SQLITE_NOMEM;
! 114096:
! 114097: /* Discard the scheme and authority segments of the URI. */
! 114098: if( zUri[5]=='/' && zUri[6]=='/' ){
! 114099: iIn = 7;
! 114100: while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
! 114101:
! 114102: if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
! 114103: *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
! 114104: iIn-7, &zUri[7]);
! 114105: rc = SQLITE_ERROR;
! 114106: goto parse_uri_out;
! 114107: }
! 114108: }else{
! 114109: iIn = 5;
! 114110: }
! 114111:
! 114112: /* Copy the filename and any query parameters into the zFile buffer.
! 114113: ** Decode %HH escape codes along the way.
! 114114: **
! 114115: ** Within this loop, variable eState may be set to 0, 1 or 2, depending
! 114116: ** on the parsing context. As follows:
! 114117: **
! 114118: ** 0: Parsing file-name.
! 114119: ** 1: Parsing name section of a name=value query parameter.
! 114120: ** 2: Parsing value section of a name=value query parameter.
! 114121: */
! 114122: eState = 0;
! 114123: while( (c = zUri[iIn])!=0 && c!='#' ){
! 114124: iIn++;
! 114125: if( c=='%'
! 114126: && sqlite3Isxdigit(zUri[iIn])
! 114127: && sqlite3Isxdigit(zUri[iIn+1])
! 114128: ){
! 114129: int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
! 114130: octet += sqlite3HexToInt(zUri[iIn++]);
! 114131:
! 114132: assert( octet>=0 && octet<256 );
! 114133: if( octet==0 ){
! 114134: /* This branch is taken when "%00" appears within the URI. In this
! 114135: ** case we ignore all text in the remainder of the path, name or
! 114136: ** value currently being parsed. So ignore the current character
! 114137: ** and skip to the next "?", "=" or "&", as appropriate. */
! 114138: while( (c = zUri[iIn])!=0 && c!='#'
! 114139: && (eState!=0 || c!='?')
! 114140: && (eState!=1 || (c!='=' && c!='&'))
! 114141: && (eState!=2 || c!='&')
! 114142: ){
! 114143: iIn++;
! 114144: }
! 114145: continue;
! 114146: }
! 114147: c = octet;
! 114148: }else if( eState==1 && (c=='&' || c=='=') ){
! 114149: if( zFile[iOut-1]==0 ){
! 114150: /* An empty option name. Ignore this option altogether. */
! 114151: while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
! 114152: continue;
! 114153: }
! 114154: if( c=='&' ){
! 114155: zFile[iOut++] = '\0';
! 114156: }else{
! 114157: eState = 2;
! 114158: }
! 114159: c = 0;
! 114160: }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
! 114161: c = 0;
! 114162: eState = 1;
! 114163: }
! 114164: zFile[iOut++] = c;
! 114165: }
! 114166: if( eState==1 ) zFile[iOut++] = '\0';
! 114167: zFile[iOut++] = '\0';
! 114168: zFile[iOut++] = '\0';
! 114169:
! 114170: /* Check if there were any options specified that should be interpreted
! 114171: ** here. Options that are interpreted here include "vfs" and those that
! 114172: ** correspond to flags that may be passed to the sqlite3_open_v2()
! 114173: ** method. */
! 114174: zOpt = &zFile[sqlite3Strlen30(zFile)+1];
! 114175: while( zOpt[0] ){
! 114176: int nOpt = sqlite3Strlen30(zOpt);
! 114177: char *zVal = &zOpt[nOpt+1];
! 114178: int nVal = sqlite3Strlen30(zVal);
! 114179:
! 114180: if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
! 114181: zVfs = zVal;
! 114182: }else{
! 114183: struct OpenMode {
! 114184: const char *z;
! 114185: int mode;
! 114186: } *aMode = 0;
! 114187: char *zModeType = 0;
! 114188: int mask = 0;
! 114189: int limit = 0;
! 114190:
! 114191: if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
! 114192: static struct OpenMode aCacheMode[] = {
! 114193: { "shared", SQLITE_OPEN_SHAREDCACHE },
! 114194: { "private", SQLITE_OPEN_PRIVATECACHE },
! 114195: { 0, 0 }
! 114196: };
! 114197:
! 114198: mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
! 114199: aMode = aCacheMode;
! 114200: limit = mask;
! 114201: zModeType = "cache";
! 114202: }
! 114203: if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
! 114204: static struct OpenMode aOpenMode[] = {
! 114205: { "ro", SQLITE_OPEN_READONLY },
! 114206: { "rw", SQLITE_OPEN_READWRITE },
! 114207: { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
! 114208: { 0, 0 }
! 114209: };
! 114210:
! 114211: mask = SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
! 114212: aMode = aOpenMode;
! 114213: limit = mask & flags;
! 114214: zModeType = "access";
! 114215: }
! 114216:
! 114217: if( aMode ){
! 114218: int i;
! 114219: int mode = 0;
! 114220: for(i=0; aMode[i].z; i++){
! 114221: const char *z = aMode[i].z;
! 114222: if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
! 114223: mode = aMode[i].mode;
! 114224: break;
! 114225: }
! 114226: }
! 114227: if( mode==0 ){
! 114228: *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
! 114229: rc = SQLITE_ERROR;
! 114230: goto parse_uri_out;
! 114231: }
! 114232: if( mode>limit ){
! 114233: *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
! 114234: zModeType, zVal);
! 114235: rc = SQLITE_PERM;
! 114236: goto parse_uri_out;
! 114237: }
! 114238: flags = (flags & ~mask) | mode;
! 114239: }
! 114240: }
! 114241:
! 114242: zOpt = &zVal[nVal+1];
! 114243: }
! 114244:
! 114245: }else{
! 114246: zFile = sqlite3_malloc(nUri+2);
! 114247: if( !zFile ) return SQLITE_NOMEM;
! 114248: memcpy(zFile, zUri, nUri);
! 114249: zFile[nUri] = '\0';
! 114250: zFile[nUri+1] = '\0';
! 114251: }
! 114252:
! 114253: *ppVfs = sqlite3_vfs_find(zVfs);
! 114254: if( *ppVfs==0 ){
! 114255: *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
! 114256: rc = SQLITE_ERROR;
! 114257: }
! 114258: parse_uri_out:
! 114259: if( rc!=SQLITE_OK ){
! 114260: sqlite3_free(zFile);
! 114261: zFile = 0;
! 114262: }
! 114263: *pFlags = flags;
! 114264: *pzFile = zFile;
! 114265: return rc;
! 114266: }
! 114267:
! 114268:
! 114269: /*
! 114270: ** This routine does the work of opening a database on behalf of
! 114271: ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
! 114272: ** is UTF-8 encoded.
! 114273: */
! 114274: static int openDatabase(
! 114275: const char *zFilename, /* Database filename UTF-8 encoded */
! 114276: sqlite3 **ppDb, /* OUT: Returned database handle */
! 114277: unsigned int flags, /* Operational flags */
! 114278: const char *zVfs /* Name of the VFS to use */
! 114279: ){
! 114280: sqlite3 *db; /* Store allocated handle here */
! 114281: int rc; /* Return code */
! 114282: int isThreadsafe; /* True for threadsafe connections */
! 114283: char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
! 114284: char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
! 114285:
! 114286: *ppDb = 0;
! 114287: #ifndef SQLITE_OMIT_AUTOINIT
! 114288: rc = sqlite3_initialize();
! 114289: if( rc ) return rc;
! 114290: #endif
! 114291:
! 114292: /* Only allow sensible combinations of bits in the flags argument.
! 114293: ** Throw an error if any non-sense combination is used. If we
! 114294: ** do not block illegal combinations here, it could trigger
! 114295: ** assert() statements in deeper layers. Sensible combinations
! 114296: ** are:
! 114297: **
! 114298: ** 1: SQLITE_OPEN_READONLY
! 114299: ** 2: SQLITE_OPEN_READWRITE
! 114300: ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
! 114301: */
! 114302: assert( SQLITE_OPEN_READONLY == 0x01 );
! 114303: assert( SQLITE_OPEN_READWRITE == 0x02 );
! 114304: assert( SQLITE_OPEN_CREATE == 0x04 );
! 114305: testcase( (1<<(flags&7))==0x02 ); /* READONLY */
! 114306: testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
! 114307: testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
! 114308: if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT;
! 114309:
! 114310: if( sqlite3GlobalConfig.bCoreMutex==0 ){
! 114311: isThreadsafe = 0;
! 114312: }else if( flags & SQLITE_OPEN_NOMUTEX ){
! 114313: isThreadsafe = 0;
! 114314: }else if( flags & SQLITE_OPEN_FULLMUTEX ){
! 114315: isThreadsafe = 1;
! 114316: }else{
! 114317: isThreadsafe = sqlite3GlobalConfig.bFullMutex;
! 114318: }
! 114319: if( flags & SQLITE_OPEN_PRIVATECACHE ){
! 114320: flags &= ~SQLITE_OPEN_SHAREDCACHE;
! 114321: }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
! 114322: flags |= SQLITE_OPEN_SHAREDCACHE;
! 114323: }
! 114324:
! 114325: /* Remove harmful bits from the flags parameter
! 114326: **
! 114327: ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
! 114328: ** dealt with in the previous code block. Besides these, the only
! 114329: ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
! 114330: ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
! 114331: ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask
! 114332: ** off all other flags.
! 114333: */
! 114334: flags &= ~( SQLITE_OPEN_DELETEONCLOSE |
! 114335: SQLITE_OPEN_EXCLUSIVE |
! 114336: SQLITE_OPEN_MAIN_DB |
! 114337: SQLITE_OPEN_TEMP_DB |
! 114338: SQLITE_OPEN_TRANSIENT_DB |
! 114339: SQLITE_OPEN_MAIN_JOURNAL |
! 114340: SQLITE_OPEN_TEMP_JOURNAL |
! 114341: SQLITE_OPEN_SUBJOURNAL |
! 114342: SQLITE_OPEN_MASTER_JOURNAL |
! 114343: SQLITE_OPEN_NOMUTEX |
! 114344: SQLITE_OPEN_FULLMUTEX |
! 114345: SQLITE_OPEN_WAL
! 114346: );
! 114347:
! 114348: /* Allocate the sqlite data structure */
! 114349: db = sqlite3MallocZero( sizeof(sqlite3) );
! 114350: if( db==0 ) goto opendb_out;
! 114351: if( isThreadsafe ){
! 114352: db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
! 114353: if( db->mutex==0 ){
! 114354: sqlite3_free(db);
! 114355: db = 0;
! 114356: goto opendb_out;
! 114357: }
! 114358: }
! 114359: sqlite3_mutex_enter(db->mutex);
! 114360: db->errMask = 0xff;
! 114361: db->nDb = 2;
! 114362: db->magic = SQLITE_MAGIC_BUSY;
! 114363: db->aDb = db->aDbStatic;
! 114364:
! 114365: assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
! 114366: memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
! 114367: db->autoCommit = 1;
! 114368: db->nextAutovac = -1;
! 114369: db->nextPagesize = 0;
! 114370: db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger
! 114371: #if SQLITE_DEFAULT_FILE_FORMAT<4
! 114372: | SQLITE_LegacyFileFmt
! 114373: #endif
! 114374: #ifdef SQLITE_ENABLE_LOAD_EXTENSION
! 114375: | SQLITE_LoadExtension
! 114376: #endif
! 114377: #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
! 114378: | SQLITE_RecTriggers
! 114379: #endif
! 114380: #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
! 114381: | SQLITE_ForeignKeys
! 114382: #endif
! 114383: ;
! 114384: sqlite3HashInit(&db->aCollSeq);
! 114385: #ifndef SQLITE_OMIT_VIRTUALTABLE
! 114386: sqlite3HashInit(&db->aModule);
! 114387: #endif
! 114388:
! 114389: /* Add the default collation sequence BINARY. BINARY works for both UTF-8
! 114390: ** and UTF-16, so add a version for each to avoid any unnecessary
! 114391: ** conversions. The only error that can occur here is a malloc() failure.
! 114392: */
! 114393: createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
! 114394: createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
! 114395: createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
! 114396: createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
! 114397: if( db->mallocFailed ){
! 114398: goto opendb_out;
! 114399: }
! 114400: db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
! 114401: assert( db->pDfltColl!=0 );
! 114402:
! 114403: /* Also add a UTF-8 case-insensitive collation sequence. */
! 114404: createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
! 114405:
! 114406: /* Parse the filename/URI argument. */
! 114407: db->openFlags = flags;
! 114408: rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
! 114409: if( rc!=SQLITE_OK ){
! 114410: if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
! 114411: sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
! 114412: sqlite3_free(zErrMsg);
! 114413: goto opendb_out;
! 114414: }
! 114415:
! 114416: /* Open the backend database driver */
! 114417: rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
! 114418: flags | SQLITE_OPEN_MAIN_DB);
! 114419: if( rc!=SQLITE_OK ){
! 114420: if( rc==SQLITE_IOERR_NOMEM ){
! 114421: rc = SQLITE_NOMEM;
! 114422: }
! 114423: sqlite3Error(db, rc, 0);
! 114424: goto opendb_out;
! 114425: }
! 114426: db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
! 114427: db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
! 114428:
! 114429:
! 114430: /* The default safety_level for the main database is 'full'; for the temp
! 114431: ** database it is 'NONE'. This matches the pager layer defaults.
! 114432: */
! 114433: db->aDb[0].zName = "main";
! 114434: db->aDb[0].safety_level = 3;
! 114435: db->aDb[1].zName = "temp";
! 114436: db->aDb[1].safety_level = 1;
! 114437:
! 114438: db->magic = SQLITE_MAGIC_OPEN;
! 114439: if( db->mallocFailed ){
! 114440: goto opendb_out;
! 114441: }
! 114442:
! 114443: /* Register all built-in functions, but do not attempt to read the
! 114444: ** database schema yet. This is delayed until the first time the database
! 114445: ** is accessed.
! 114446: */
! 114447: sqlite3Error(db, SQLITE_OK, 0);
! 114448: sqlite3RegisterBuiltinFunctions(db);
! 114449:
! 114450: /* Load automatic extensions - extensions that have been registered
! 114451: ** using the sqlite3_automatic_extension() API.
! 114452: */
! 114453: rc = sqlite3_errcode(db);
! 114454: if( rc==SQLITE_OK ){
! 114455: sqlite3AutoLoadExtensions(db);
! 114456: rc = sqlite3_errcode(db);
! 114457: if( rc!=SQLITE_OK ){
! 114458: goto opendb_out;
! 114459: }
! 114460: }
! 114461:
! 114462: #ifdef SQLITE_ENABLE_FTS1
! 114463: if( !db->mallocFailed ){
! 114464: extern int sqlite3Fts1Init(sqlite3*);
! 114465: rc = sqlite3Fts1Init(db);
! 114466: }
! 114467: #endif
! 114468:
! 114469: #ifdef SQLITE_ENABLE_FTS2
! 114470: if( !db->mallocFailed && rc==SQLITE_OK ){
! 114471: extern int sqlite3Fts2Init(sqlite3*);
! 114472: rc = sqlite3Fts2Init(db);
! 114473: }
! 114474: #endif
! 114475:
! 114476: #ifdef SQLITE_ENABLE_FTS3
! 114477: if( !db->mallocFailed && rc==SQLITE_OK ){
! 114478: rc = sqlite3Fts3Init(db);
! 114479: }
! 114480: #endif
! 114481:
! 114482: #ifdef SQLITE_ENABLE_ICU
! 114483: if( !db->mallocFailed && rc==SQLITE_OK ){
! 114484: rc = sqlite3IcuInit(db);
! 114485: }
! 114486: #endif
! 114487:
! 114488: #ifdef SQLITE_ENABLE_RTREE
! 114489: if( !db->mallocFailed && rc==SQLITE_OK){
! 114490: rc = sqlite3RtreeInit(db);
! 114491: }
! 114492: #endif
! 114493:
! 114494: sqlite3Error(db, rc, 0);
! 114495:
! 114496: /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
! 114497: ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
! 114498: ** mode. Doing nothing at all also makes NORMAL the default.
! 114499: */
! 114500: #ifdef SQLITE_DEFAULT_LOCKING_MODE
! 114501: db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
! 114502: sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
! 114503: SQLITE_DEFAULT_LOCKING_MODE);
! 114504: #endif
! 114505:
! 114506: /* Enable the lookaside-malloc subsystem */
! 114507: setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
! 114508: sqlite3GlobalConfig.nLookaside);
! 114509:
! 114510: sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
! 114511:
! 114512: opendb_out:
! 114513: sqlite3_free(zOpen);
! 114514: if( db ){
! 114515: assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
! 114516: sqlite3_mutex_leave(db->mutex);
! 114517: }
! 114518: rc = sqlite3_errcode(db);
! 114519: assert( db!=0 || rc==SQLITE_NOMEM );
! 114520: if( rc==SQLITE_NOMEM ){
! 114521: sqlite3_close(db);
! 114522: db = 0;
! 114523: }else if( rc!=SQLITE_OK ){
! 114524: db->magic = SQLITE_MAGIC_SICK;
! 114525: }
! 114526: *ppDb = db;
! 114527: return sqlite3ApiExit(0, rc);
! 114528: }
! 114529:
! 114530: /*
! 114531: ** Open a new database handle.
! 114532: */
! 114533: SQLITE_API int sqlite3_open(
! 114534: const char *zFilename,
! 114535: sqlite3 **ppDb
! 114536: ){
! 114537: return openDatabase(zFilename, ppDb,
! 114538: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
! 114539: }
! 114540: SQLITE_API int sqlite3_open_v2(
! 114541: const char *filename, /* Database filename (UTF-8) */
! 114542: sqlite3 **ppDb, /* OUT: SQLite db handle */
! 114543: int flags, /* Flags */
! 114544: const char *zVfs /* Name of VFS module to use */
! 114545: ){
! 114546: return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
! 114547: }
! 114548:
! 114549: #ifndef SQLITE_OMIT_UTF16
! 114550: /*
! 114551: ** Open a new database handle.
! 114552: */
! 114553: SQLITE_API int sqlite3_open16(
! 114554: const void *zFilename,
! 114555: sqlite3 **ppDb
! 114556: ){
! 114557: char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
! 114558: sqlite3_value *pVal;
! 114559: int rc;
! 114560:
! 114561: assert( zFilename );
! 114562: assert( ppDb );
! 114563: *ppDb = 0;
! 114564: #ifndef SQLITE_OMIT_AUTOINIT
! 114565: rc = sqlite3_initialize();
! 114566: if( rc ) return rc;
! 114567: #endif
! 114568: pVal = sqlite3ValueNew(0);
! 114569: sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
! 114570: zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
! 114571: if( zFilename8 ){
! 114572: rc = openDatabase(zFilename8, ppDb,
! 114573: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
! 114574: assert( *ppDb || rc==SQLITE_NOMEM );
! 114575: if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
! 114576: ENC(*ppDb) = SQLITE_UTF16NATIVE;
! 114577: }
! 114578: }else{
! 114579: rc = SQLITE_NOMEM;
! 114580: }
! 114581: sqlite3ValueFree(pVal);
! 114582:
! 114583: return sqlite3ApiExit(0, rc);
! 114584: }
! 114585: #endif /* SQLITE_OMIT_UTF16 */
! 114586:
! 114587: /*
! 114588: ** Register a new collation sequence with the database handle db.
! 114589: */
! 114590: SQLITE_API int sqlite3_create_collation(
! 114591: sqlite3* db,
! 114592: const char *zName,
! 114593: int enc,
! 114594: void* pCtx,
! 114595: int(*xCompare)(void*,int,const void*,int,const void*)
! 114596: ){
! 114597: int rc;
! 114598: sqlite3_mutex_enter(db->mutex);
! 114599: assert( !db->mallocFailed );
! 114600: rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0);
! 114601: rc = sqlite3ApiExit(db, rc);
! 114602: sqlite3_mutex_leave(db->mutex);
! 114603: return rc;
! 114604: }
! 114605:
! 114606: /*
! 114607: ** Register a new collation sequence with the database handle db.
! 114608: */
! 114609: SQLITE_API int sqlite3_create_collation_v2(
! 114610: sqlite3* db,
! 114611: const char *zName,
! 114612: int enc,
! 114613: void* pCtx,
! 114614: int(*xCompare)(void*,int,const void*,int,const void*),
! 114615: void(*xDel)(void*)
! 114616: ){
! 114617: int rc;
! 114618: sqlite3_mutex_enter(db->mutex);
! 114619: assert( !db->mallocFailed );
! 114620: rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
! 114621: rc = sqlite3ApiExit(db, rc);
! 114622: sqlite3_mutex_leave(db->mutex);
! 114623: return rc;
! 114624: }
! 114625:
! 114626: #ifndef SQLITE_OMIT_UTF16
! 114627: /*
! 114628: ** Register a new collation sequence with the database handle db.
! 114629: */
! 114630: SQLITE_API int sqlite3_create_collation16(
! 114631: sqlite3* db,
! 114632: const void *zName,
! 114633: int enc,
! 114634: void* pCtx,
! 114635: int(*xCompare)(void*,int,const void*,int,const void*)
! 114636: ){
! 114637: int rc = SQLITE_OK;
! 114638: char *zName8;
! 114639: sqlite3_mutex_enter(db->mutex);
! 114640: assert( !db->mallocFailed );
! 114641: zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
! 114642: if( zName8 ){
! 114643: rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
! 114644: sqlite3DbFree(db, zName8);
! 114645: }
! 114646: rc = sqlite3ApiExit(db, rc);
! 114647: sqlite3_mutex_leave(db->mutex);
! 114648: return rc;
! 114649: }
! 114650: #endif /* SQLITE_OMIT_UTF16 */
! 114651:
! 114652: /*
! 114653: ** Register a collation sequence factory callback with the database handle
! 114654: ** db. Replace any previously installed collation sequence factory.
! 114655: */
! 114656: SQLITE_API int sqlite3_collation_needed(
! 114657: sqlite3 *db,
! 114658: void *pCollNeededArg,
! 114659: void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
! 114660: ){
! 114661: sqlite3_mutex_enter(db->mutex);
! 114662: db->xCollNeeded = xCollNeeded;
! 114663: db->xCollNeeded16 = 0;
! 114664: db->pCollNeededArg = pCollNeededArg;
! 114665: sqlite3_mutex_leave(db->mutex);
! 114666: return SQLITE_OK;
! 114667: }
! 114668:
! 114669: #ifndef SQLITE_OMIT_UTF16
! 114670: /*
! 114671: ** Register a collation sequence factory callback with the database handle
! 114672: ** db. Replace any previously installed collation sequence factory.
! 114673: */
! 114674: SQLITE_API int sqlite3_collation_needed16(
! 114675: sqlite3 *db,
! 114676: void *pCollNeededArg,
! 114677: void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
! 114678: ){
! 114679: sqlite3_mutex_enter(db->mutex);
! 114680: db->xCollNeeded = 0;
! 114681: db->xCollNeeded16 = xCollNeeded16;
! 114682: db->pCollNeededArg = pCollNeededArg;
! 114683: sqlite3_mutex_leave(db->mutex);
! 114684: return SQLITE_OK;
! 114685: }
! 114686: #endif /* SQLITE_OMIT_UTF16 */
! 114687:
! 114688: #ifndef SQLITE_OMIT_DEPRECATED
! 114689: /*
! 114690: ** This function is now an anachronism. It used to be used to recover from a
! 114691: ** malloc() failure, but SQLite now does this automatically.
! 114692: */
! 114693: SQLITE_API int sqlite3_global_recover(void){
! 114694: return SQLITE_OK;
! 114695: }
! 114696: #endif
! 114697:
! 114698: /*
! 114699: ** Test to see whether or not the database connection is in autocommit
! 114700: ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
! 114701: ** by default. Autocommit is disabled by a BEGIN statement and reenabled
! 114702: ** by the next COMMIT or ROLLBACK.
! 114703: **
! 114704: ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
! 114705: */
! 114706: SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
! 114707: return db->autoCommit;
! 114708: }
! 114709:
! 114710: /*
! 114711: ** The following routines are subtitutes for constants SQLITE_CORRUPT,
! 114712: ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
! 114713: ** constants. They server two purposes:
! 114714: **
! 114715: ** 1. Serve as a convenient place to set a breakpoint in a debugger
! 114716: ** to detect when version error conditions occurs.
! 114717: **
! 114718: ** 2. Invoke sqlite3_log() to provide the source code location where
! 114719: ** a low-level error is first detected.
! 114720: */
! 114721: SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
! 114722: testcase( sqlite3GlobalConfig.xLog!=0 );
! 114723: sqlite3_log(SQLITE_CORRUPT,
! 114724: "database corruption at line %d of [%.10s]",
! 114725: lineno, 20+sqlite3_sourceid());
! 114726: return SQLITE_CORRUPT;
! 114727: }
! 114728: SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
! 114729: testcase( sqlite3GlobalConfig.xLog!=0 );
! 114730: sqlite3_log(SQLITE_MISUSE,
! 114731: "misuse at line %d of [%.10s]",
! 114732: lineno, 20+sqlite3_sourceid());
! 114733: return SQLITE_MISUSE;
! 114734: }
! 114735: SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
! 114736: testcase( sqlite3GlobalConfig.xLog!=0 );
! 114737: sqlite3_log(SQLITE_CANTOPEN,
! 114738: "cannot open file at line %d of [%.10s]",
! 114739: lineno, 20+sqlite3_sourceid());
! 114740: return SQLITE_CANTOPEN;
! 114741: }
! 114742:
! 114743:
! 114744: #ifndef SQLITE_OMIT_DEPRECATED
! 114745: /*
! 114746: ** This is a convenience routine that makes sure that all thread-specific
! 114747: ** data for this thread has been deallocated.
! 114748: **
! 114749: ** SQLite no longer uses thread-specific data so this routine is now a
! 114750: ** no-op. It is retained for historical compatibility.
! 114751: */
! 114752: SQLITE_API void sqlite3_thread_cleanup(void){
! 114753: }
! 114754: #endif
! 114755:
! 114756: /*
! 114757: ** Return meta information about a specific column of a database table.
! 114758: ** See comment in sqlite3.h (sqlite.h.in) for details.
! 114759: */
! 114760: #ifdef SQLITE_ENABLE_COLUMN_METADATA
! 114761: SQLITE_API int sqlite3_table_column_metadata(
! 114762: sqlite3 *db, /* Connection handle */
! 114763: const char *zDbName, /* Database name or NULL */
! 114764: const char *zTableName, /* Table name */
! 114765: const char *zColumnName, /* Column name */
! 114766: char const **pzDataType, /* OUTPUT: Declared data type */
! 114767: char const **pzCollSeq, /* OUTPUT: Collation sequence name */
! 114768: int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
! 114769: int *pPrimaryKey, /* OUTPUT: True if column part of PK */
! 114770: int *pAutoinc /* OUTPUT: True if column is auto-increment */
! 114771: ){
! 114772: int rc;
! 114773: char *zErrMsg = 0;
! 114774: Table *pTab = 0;
! 114775: Column *pCol = 0;
! 114776: int iCol;
! 114777:
! 114778: char const *zDataType = 0;
! 114779: char const *zCollSeq = 0;
! 114780: int notnull = 0;
! 114781: int primarykey = 0;
! 114782: int autoinc = 0;
! 114783:
! 114784: /* Ensure the database schema has been loaded */
! 114785: sqlite3_mutex_enter(db->mutex);
! 114786: sqlite3BtreeEnterAll(db);
! 114787: rc = sqlite3Init(db, &zErrMsg);
! 114788: if( SQLITE_OK!=rc ){
! 114789: goto error_out;
! 114790: }
! 114791:
! 114792: /* Locate the table in question */
! 114793: pTab = sqlite3FindTable(db, zTableName, zDbName);
! 114794: if( !pTab || pTab->pSelect ){
! 114795: pTab = 0;
! 114796: goto error_out;
! 114797: }
! 114798:
! 114799: /* Find the column for which info is requested */
! 114800: if( sqlite3IsRowid(zColumnName) ){
! 114801: iCol = pTab->iPKey;
! 114802: if( iCol>=0 ){
! 114803: pCol = &pTab->aCol[iCol];
! 114804: }
! 114805: }else{
! 114806: for(iCol=0; iCol<pTab->nCol; iCol++){
! 114807: pCol = &pTab->aCol[iCol];
! 114808: if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
! 114809: break;
! 114810: }
! 114811: }
! 114812: if( iCol==pTab->nCol ){
! 114813: pTab = 0;
! 114814: goto error_out;
! 114815: }
! 114816: }
! 114817:
! 114818: /* The following block stores the meta information that will be returned
! 114819: ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
! 114820: ** and autoinc. At this point there are two possibilities:
! 114821: **
! 114822: ** 1. The specified column name was rowid", "oid" or "_rowid_"
! 114823: ** and there is no explicitly declared IPK column.
! 114824: **
! 114825: ** 2. The table is not a view and the column name identified an
! 114826: ** explicitly declared column. Copy meta information from *pCol.
! 114827: */
! 114828: if( pCol ){
! 114829: zDataType = pCol->zType;
! 114830: zCollSeq = pCol->zColl;
! 114831: notnull = pCol->notNull!=0;
! 114832: primarykey = pCol->isPrimKey!=0;
! 114833: autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
! 114834: }else{
! 114835: zDataType = "INTEGER";
! 114836: primarykey = 1;
! 114837: }
! 114838: if( !zCollSeq ){
! 114839: zCollSeq = "BINARY";
! 114840: }
! 114841:
! 114842: error_out:
! 114843: sqlite3BtreeLeaveAll(db);
! 114844:
! 114845: /* Whether the function call succeeded or failed, set the output parameters
! 114846: ** to whatever their local counterparts contain. If an error did occur,
! 114847: ** this has the effect of zeroing all output parameters.
! 114848: */
! 114849: if( pzDataType ) *pzDataType = zDataType;
! 114850: if( pzCollSeq ) *pzCollSeq = zCollSeq;
! 114851: if( pNotNull ) *pNotNull = notnull;
! 114852: if( pPrimaryKey ) *pPrimaryKey = primarykey;
! 114853: if( pAutoinc ) *pAutoinc = autoinc;
! 114854:
! 114855: if( SQLITE_OK==rc && !pTab ){
! 114856: sqlite3DbFree(db, zErrMsg);
! 114857: zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
! 114858: zColumnName);
! 114859: rc = SQLITE_ERROR;
! 114860: }
! 114861: sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
! 114862: sqlite3DbFree(db, zErrMsg);
! 114863: rc = sqlite3ApiExit(db, rc);
! 114864: sqlite3_mutex_leave(db->mutex);
! 114865: return rc;
! 114866: }
! 114867: #endif
! 114868:
! 114869: /*
! 114870: ** Sleep for a little while. Return the amount of time slept.
! 114871: */
! 114872: SQLITE_API int sqlite3_sleep(int ms){
! 114873: sqlite3_vfs *pVfs;
! 114874: int rc;
! 114875: pVfs = sqlite3_vfs_find(0);
! 114876: if( pVfs==0 ) return 0;
! 114877:
! 114878: /* This function works in milliseconds, but the underlying OsSleep()
! 114879: ** API uses microseconds. Hence the 1000's.
! 114880: */
! 114881: rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
! 114882: return rc;
! 114883: }
! 114884:
! 114885: /*
! 114886: ** Enable or disable the extended result codes.
! 114887: */
! 114888: SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
! 114889: sqlite3_mutex_enter(db->mutex);
! 114890: db->errMask = onoff ? 0xffffffff : 0xff;
! 114891: sqlite3_mutex_leave(db->mutex);
! 114892: return SQLITE_OK;
! 114893: }
! 114894:
! 114895: /*
! 114896: ** Invoke the xFileControl method on a particular database.
! 114897: */
! 114898: SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
! 114899: int rc = SQLITE_ERROR;
! 114900: int iDb;
! 114901: sqlite3_mutex_enter(db->mutex);
! 114902: if( zDbName==0 ){
! 114903: iDb = 0;
! 114904: }else{
! 114905: for(iDb=0; iDb<db->nDb; iDb++){
! 114906: if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
! 114907: }
! 114908: }
! 114909: if( iDb<db->nDb ){
! 114910: Btree *pBtree = db->aDb[iDb].pBt;
! 114911: if( pBtree ){
! 114912: Pager *pPager;
! 114913: sqlite3_file *fd;
! 114914: sqlite3BtreeEnter(pBtree);
! 114915: pPager = sqlite3BtreePager(pBtree);
! 114916: assert( pPager!=0 );
! 114917: fd = sqlite3PagerFile(pPager);
! 114918: assert( fd!=0 );
! 114919: if( op==SQLITE_FCNTL_FILE_POINTER ){
! 114920: *(sqlite3_file**)pArg = fd;
! 114921: rc = SQLITE_OK;
! 114922: }else if( fd->pMethods ){
! 114923: rc = sqlite3OsFileControl(fd, op, pArg);
! 114924: }else{
! 114925: rc = SQLITE_NOTFOUND;
! 114926: }
! 114927: sqlite3BtreeLeave(pBtree);
! 114928: }
! 114929: }
! 114930: sqlite3_mutex_leave(db->mutex);
! 114931: return rc;
! 114932: }
! 114933:
! 114934: /*
! 114935: ** Interface to the testing logic.
! 114936: */
! 114937: SQLITE_API int sqlite3_test_control(int op, ...){
! 114938: int rc = 0;
! 114939: #ifndef SQLITE_OMIT_BUILTIN_TEST
! 114940: va_list ap;
! 114941: va_start(ap, op);
! 114942: switch( op ){
! 114943:
! 114944: /*
! 114945: ** Save the current state of the PRNG.
! 114946: */
! 114947: case SQLITE_TESTCTRL_PRNG_SAVE: {
! 114948: sqlite3PrngSaveState();
! 114949: break;
! 114950: }
! 114951:
! 114952: /*
! 114953: ** Restore the state of the PRNG to the last state saved using
! 114954: ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
! 114955: ** this verb acts like PRNG_RESET.
! 114956: */
! 114957: case SQLITE_TESTCTRL_PRNG_RESTORE: {
! 114958: sqlite3PrngRestoreState();
! 114959: break;
! 114960: }
! 114961:
! 114962: /*
! 114963: ** Reset the PRNG back to its uninitialized state. The next call
! 114964: ** to sqlite3_randomness() will reseed the PRNG using a single call
! 114965: ** to the xRandomness method of the default VFS.
! 114966: */
! 114967: case SQLITE_TESTCTRL_PRNG_RESET: {
! 114968: sqlite3PrngResetState();
! 114969: break;
! 114970: }
! 114971:
! 114972: /*
! 114973: ** sqlite3_test_control(BITVEC_TEST, size, program)
! 114974: **
! 114975: ** Run a test against a Bitvec object of size. The program argument
! 114976: ** is an array of integers that defines the test. Return -1 on a
! 114977: ** memory allocation error, 0 on success, or non-zero for an error.
! 114978: ** See the sqlite3BitvecBuiltinTest() for additional information.
! 114979: */
! 114980: case SQLITE_TESTCTRL_BITVEC_TEST: {
! 114981: int sz = va_arg(ap, int);
! 114982: int *aProg = va_arg(ap, int*);
! 114983: rc = sqlite3BitvecBuiltinTest(sz, aProg);
! 114984: break;
! 114985: }
! 114986:
! 114987: /*
! 114988: ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
! 114989: **
! 114990: ** Register hooks to call to indicate which malloc() failures
! 114991: ** are benign.
! 114992: */
! 114993: case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
! 114994: typedef void (*void_function)(void);
! 114995: void_function xBenignBegin;
! 114996: void_function xBenignEnd;
! 114997: xBenignBegin = va_arg(ap, void_function);
! 114998: xBenignEnd = va_arg(ap, void_function);
! 114999: sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
! 115000: break;
! 115001: }
! 115002:
! 115003: /*
! 115004: ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
! 115005: **
! 115006: ** Set the PENDING byte to the value in the argument, if X>0.
! 115007: ** Make no changes if X==0. Return the value of the pending byte
! 115008: ** as it existing before this routine was called.
! 115009: **
! 115010: ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
! 115011: ** an incompatible database file format. Changing the PENDING byte
! 115012: ** while any database connection is open results in undefined and
! 115013: ** dileterious behavior.
! 115014: */
! 115015: case SQLITE_TESTCTRL_PENDING_BYTE: {
! 115016: rc = PENDING_BYTE;
! 115017: #ifndef SQLITE_OMIT_WSD
! 115018: {
! 115019: unsigned int newVal = va_arg(ap, unsigned int);
! 115020: if( newVal ) sqlite3PendingByte = newVal;
! 115021: }
! 115022: #endif
! 115023: break;
! 115024: }
! 115025:
! 115026: /*
! 115027: ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
! 115028: **
! 115029: ** This action provides a run-time test to see whether or not
! 115030: ** assert() was enabled at compile-time. If X is true and assert()
! 115031: ** is enabled, then the return value is true. If X is true and
! 115032: ** assert() is disabled, then the return value is zero. If X is
! 115033: ** false and assert() is enabled, then the assertion fires and the
! 115034: ** process aborts. If X is false and assert() is disabled, then the
! 115035: ** return value is zero.
! 115036: */
! 115037: case SQLITE_TESTCTRL_ASSERT: {
! 115038: volatile int x = 0;
! 115039: assert( (x = va_arg(ap,int))!=0 );
! 115040: rc = x;
! 115041: break;
! 115042: }
! 115043:
! 115044:
! 115045: /*
! 115046: ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
! 115047: **
! 115048: ** This action provides a run-time test to see how the ALWAYS and
! 115049: ** NEVER macros were defined at compile-time.
! 115050: **
! 115051: ** The return value is ALWAYS(X).
! 115052: **
! 115053: ** The recommended test is X==2. If the return value is 2, that means
! 115054: ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
! 115055: ** default setting. If the return value is 1, then ALWAYS() is either
! 115056: ** hard-coded to true or else it asserts if its argument is false.
! 115057: ** The first behavior (hard-coded to true) is the case if
! 115058: ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
! 115059: ** behavior (assert if the argument to ALWAYS() is false) is the case if
! 115060: ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
! 115061: **
! 115062: ** The run-time test procedure might look something like this:
! 115063: **
! 115064: ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
! 115065: ** // ALWAYS() and NEVER() are no-op pass-through macros
! 115066: ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
! 115067: ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
! 115068: ** }else{
! 115069: ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
! 115070: ** }
! 115071: */
! 115072: case SQLITE_TESTCTRL_ALWAYS: {
! 115073: int x = va_arg(ap,int);
! 115074: rc = ALWAYS(x);
! 115075: break;
! 115076: }
! 115077:
! 115078: /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
! 115079: **
! 115080: ** Set the nReserve size to N for the main database on the database
! 115081: ** connection db.
! 115082: */
! 115083: case SQLITE_TESTCTRL_RESERVE: {
! 115084: sqlite3 *db = va_arg(ap, sqlite3*);
! 115085: int x = va_arg(ap,int);
! 115086: sqlite3_mutex_enter(db->mutex);
! 115087: sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
! 115088: sqlite3_mutex_leave(db->mutex);
! 115089: break;
! 115090: }
! 115091:
! 115092: /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
! 115093: **
! 115094: ** Enable or disable various optimizations for testing purposes. The
! 115095: ** argument N is a bitmask of optimizations to be disabled. For normal
! 115096: ** operation N should be 0. The idea is that a test program (like the
! 115097: ** SQL Logic Test or SLT test module) can run the same SQL multiple times
! 115098: ** with various optimizations disabled to verify that the same answer
! 115099: ** is obtained in every case.
! 115100: */
! 115101: case SQLITE_TESTCTRL_OPTIMIZATIONS: {
! 115102: sqlite3 *db = va_arg(ap, sqlite3*);
! 115103: int x = va_arg(ap,int);
! 115104: db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask);
! 115105: break;
! 115106: }
! 115107:
! 115108: #ifdef SQLITE_N_KEYWORD
! 115109: /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
! 115110: **
! 115111: ** If zWord is a keyword recognized by the parser, then return the
! 115112: ** number of keywords. Or if zWord is not a keyword, return 0.
! 115113: **
! 115114: ** This test feature is only available in the amalgamation since
! 115115: ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
! 115116: ** is built using separate source files.
! 115117: */
! 115118: case SQLITE_TESTCTRL_ISKEYWORD: {
! 115119: const char *zWord = va_arg(ap, const char*);
! 115120: int n = sqlite3Strlen30(zWord);
! 115121: rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
! 115122: break;
! 115123: }
! 115124: #endif
! 115125:
! 115126: /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
! 115127: **
! 115128: ** Pass pFree into sqlite3ScratchFree().
! 115129: ** If sz>0 then allocate a scratch buffer into pNew.
! 115130: */
! 115131: case SQLITE_TESTCTRL_SCRATCHMALLOC: {
! 115132: void *pFree, **ppNew;
! 115133: int sz;
! 115134: sz = va_arg(ap, int);
! 115135: ppNew = va_arg(ap, void**);
! 115136: pFree = va_arg(ap, void*);
! 115137: if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
! 115138: sqlite3ScratchFree(pFree);
! 115139: break;
! 115140: }
! 115141:
! 115142: /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
! 115143: **
! 115144: ** If parameter onoff is non-zero, configure the wrappers so that all
! 115145: ** subsequent calls to localtime() and variants fail. If onoff is zero,
! 115146: ** undo this setting.
! 115147: */
! 115148: case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
! 115149: sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
! 115150: break;
! 115151: }
! 115152:
! 115153: #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
! 115154: /* sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT,
! 115155: ** sqlite3_stmt*,const char**);
! 115156: **
! 115157: ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds
! 115158: ** a string that describes the optimized parse tree. This test-control
! 115159: ** returns a pointer to that string.
! 115160: */
! 115161: case SQLITE_TESTCTRL_EXPLAIN_STMT: {
! 115162: sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*);
! 115163: const char **pzRet = va_arg(ap, const char**);
! 115164: *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt);
! 115165: break;
! 115166: }
! 115167: #endif
! 115168:
! 115169: }
! 115170: va_end(ap);
! 115171: #endif /* SQLITE_OMIT_BUILTIN_TEST */
! 115172: return rc;
! 115173: }
! 115174:
! 115175: /*
! 115176: ** This is a utility routine, useful to VFS implementations, that checks
! 115177: ** to see if a database file was a URI that contained a specific query
! 115178: ** parameter, and if so obtains the value of the query parameter.
! 115179: **
! 115180: ** The zFilename argument is the filename pointer passed into the xOpen()
! 115181: ** method of a VFS implementation. The zParam argument is the name of the
! 115182: ** query parameter we seek. This routine returns the value of the zParam
! 115183: ** parameter if it exists. If the parameter does not exist, this routine
! 115184: ** returns a NULL pointer.
! 115185: */
! 115186: SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
! 115187: if( zFilename==0 ) return 0;
! 115188: zFilename += sqlite3Strlen30(zFilename) + 1;
! 115189: while( zFilename[0] ){
! 115190: int x = strcmp(zFilename, zParam);
! 115191: zFilename += sqlite3Strlen30(zFilename) + 1;
! 115192: if( x==0 ) return zFilename;
! 115193: zFilename += sqlite3Strlen30(zFilename) + 1;
! 115194: }
! 115195: return 0;
! 115196: }
! 115197:
! 115198: /*
! 115199: ** Return a boolean value for a query parameter.
! 115200: */
! 115201: SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
! 115202: const char *z = sqlite3_uri_parameter(zFilename, zParam);
! 115203: return z ? sqlite3GetBoolean(z) : (bDflt!=0);
! 115204: }
! 115205:
! 115206: /*
! 115207: ** Return a 64-bit integer value for a query parameter.
! 115208: */
! 115209: SQLITE_API sqlite3_int64 sqlite3_uri_int64(
! 115210: const char *zFilename, /* Filename as passed to xOpen */
! 115211: const char *zParam, /* URI parameter sought */
! 115212: sqlite3_int64 bDflt /* return if parameter is missing */
! 115213: ){
! 115214: const char *z = sqlite3_uri_parameter(zFilename, zParam);
! 115215: sqlite3_int64 v;
! 115216: if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){
! 115217: bDflt = v;
! 115218: }
! 115219: return bDflt;
! 115220: }
! 115221:
! 115222: /*
! 115223: ** Return the filename of the database associated with a database
! 115224: ** connection.
! 115225: */
! 115226: SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
! 115227: int i;
! 115228: for(i=0; i<db->nDb; i++){
! 115229: if( db->aDb[i].pBt && sqlite3StrICmp(zDbName, db->aDb[i].zName)==0 ){
! 115230: return sqlite3BtreeGetFilename(db->aDb[i].pBt);
! 115231: }
! 115232: }
! 115233: return 0;
! 115234: }
! 115235:
! 115236: /************** End of main.c ************************************************/
! 115237: /************** Begin file notify.c ******************************************/
! 115238: /*
! 115239: ** 2009 March 3
! 115240: **
! 115241: ** The author disclaims copyright to this source code. In place of
! 115242: ** a legal notice, here is a blessing:
! 115243: **
! 115244: ** May you do good and not evil.
! 115245: ** May you find forgiveness for yourself and forgive others.
! 115246: ** May you share freely, never taking more than you give.
! 115247: **
! 115248: *************************************************************************
! 115249: **
! 115250: ** This file contains the implementation of the sqlite3_unlock_notify()
! 115251: ** API method and its associated functionality.
! 115252: */
! 115253:
! 115254: /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
! 115255: #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
! 115256:
! 115257: /*
! 115258: ** Public interfaces:
! 115259: **
! 115260: ** sqlite3ConnectionBlocked()
! 115261: ** sqlite3ConnectionUnlocked()
! 115262: ** sqlite3ConnectionClosed()
! 115263: ** sqlite3_unlock_notify()
! 115264: */
! 115265:
! 115266: #define assertMutexHeld() \
! 115267: assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
! 115268:
! 115269: /*
! 115270: ** Head of a linked list of all sqlite3 objects created by this process
! 115271: ** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
! 115272: ** is not NULL. This variable may only accessed while the STATIC_MASTER
! 115273: ** mutex is held.
! 115274: */
! 115275: static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
! 115276:
! 115277: #ifndef NDEBUG
! 115278: /*
! 115279: ** This function is a complex assert() that verifies the following
! 115280: ** properties of the blocked connections list:
! 115281: **
! 115282: ** 1) Each entry in the list has a non-NULL value for either
! 115283: ** pUnlockConnection or pBlockingConnection, or both.
! 115284: **
! 115285: ** 2) All entries in the list that share a common value for
! 115286: ** xUnlockNotify are grouped together.
! 115287: **
! 115288: ** 3) If the argument db is not NULL, then none of the entries in the
! 115289: ** blocked connections list have pUnlockConnection or pBlockingConnection
! 115290: ** set to db. This is used when closing connection db.
! 115291: */
! 115292: static void checkListProperties(sqlite3 *db){
! 115293: sqlite3 *p;
! 115294: for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
! 115295: int seen = 0;
! 115296: sqlite3 *p2;
! 115297:
! 115298: /* Verify property (1) */
! 115299: assert( p->pUnlockConnection || p->pBlockingConnection );
! 115300:
! 115301: /* Verify property (2) */
! 115302: for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
! 115303: if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
! 115304: assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
! 115305: assert( db==0 || p->pUnlockConnection!=db );
! 115306: assert( db==0 || p->pBlockingConnection!=db );
! 115307: }
! 115308: }
! 115309: }
! 115310: #else
! 115311: # define checkListProperties(x)
! 115312: #endif
! 115313:
! 115314: /*
! 115315: ** Remove connection db from the blocked connections list. If connection
! 115316: ** db is not currently a part of the list, this function is a no-op.
! 115317: */
! 115318: static void removeFromBlockedList(sqlite3 *db){
! 115319: sqlite3 **pp;
! 115320: assertMutexHeld();
! 115321: for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
! 115322: if( *pp==db ){
! 115323: *pp = (*pp)->pNextBlocked;
! 115324: break;
! 115325: }
! 115326: }
! 115327: }
! 115328:
! 115329: /*
! 115330: ** Add connection db to the blocked connections list. It is assumed
! 115331: ** that it is not already a part of the list.
! 115332: */
! 115333: static void addToBlockedList(sqlite3 *db){
! 115334: sqlite3 **pp;
! 115335: assertMutexHeld();
! 115336: for(
! 115337: pp=&sqlite3BlockedList;
! 115338: *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
! 115339: pp=&(*pp)->pNextBlocked
! 115340: );
! 115341: db->pNextBlocked = *pp;
! 115342: *pp = db;
! 115343: }
! 115344:
! 115345: /*
! 115346: ** Obtain the STATIC_MASTER mutex.
! 115347: */
! 115348: static void enterMutex(void){
! 115349: sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 115350: checkListProperties(0);
! 115351: }
! 115352:
! 115353: /*
! 115354: ** Release the STATIC_MASTER mutex.
! 115355: */
! 115356: static void leaveMutex(void){
! 115357: assertMutexHeld();
! 115358: checkListProperties(0);
! 115359: sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
! 115360: }
! 115361:
! 115362: /*
! 115363: ** Register an unlock-notify callback.
! 115364: **
! 115365: ** This is called after connection "db" has attempted some operation
! 115366: ** but has received an SQLITE_LOCKED error because another connection
! 115367: ** (call it pOther) in the same process was busy using the same shared
! 115368: ** cache. pOther is found by looking at db->pBlockingConnection.
! 115369: **
! 115370: ** If there is no blocking connection, the callback is invoked immediately,
! 115371: ** before this routine returns.
! 115372: **
! 115373: ** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
! 115374: ** a deadlock.
! 115375: **
! 115376: ** Otherwise, make arrangements to invoke xNotify when pOther drops
! 115377: ** its locks.
! 115378: **
! 115379: ** Each call to this routine overrides any prior callbacks registered
! 115380: ** on the same "db". If xNotify==0 then any prior callbacks are immediately
! 115381: ** cancelled.
! 115382: */
! 115383: SQLITE_API int sqlite3_unlock_notify(
! 115384: sqlite3 *db,
! 115385: void (*xNotify)(void **, int),
! 115386: void *pArg
! 115387: ){
! 115388: int rc = SQLITE_OK;
! 115389:
! 115390: sqlite3_mutex_enter(db->mutex);
! 115391: enterMutex();
! 115392:
! 115393: if( xNotify==0 ){
! 115394: removeFromBlockedList(db);
! 115395: db->pBlockingConnection = 0;
! 115396: db->pUnlockConnection = 0;
! 115397: db->xUnlockNotify = 0;
! 115398: db->pUnlockArg = 0;
! 115399: }else if( 0==db->pBlockingConnection ){
! 115400: /* The blocking transaction has been concluded. Or there never was a
! 115401: ** blocking transaction. In either case, invoke the notify callback
! 115402: ** immediately.
! 115403: */
! 115404: xNotify(&pArg, 1);
! 115405: }else{
! 115406: sqlite3 *p;
! 115407:
! 115408: for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
! 115409: if( p ){
! 115410: rc = SQLITE_LOCKED; /* Deadlock detected. */
! 115411: }else{
! 115412: db->pUnlockConnection = db->pBlockingConnection;
! 115413: db->xUnlockNotify = xNotify;
! 115414: db->pUnlockArg = pArg;
! 115415: removeFromBlockedList(db);
! 115416: addToBlockedList(db);
! 115417: }
! 115418: }
! 115419:
! 115420: leaveMutex();
! 115421: assert( !db->mallocFailed );
! 115422: sqlite3Error(db, rc, (rc?"database is deadlocked":0));
! 115423: sqlite3_mutex_leave(db->mutex);
! 115424: return rc;
! 115425: }
! 115426:
! 115427: /*
! 115428: ** This function is called while stepping or preparing a statement
! 115429: ** associated with connection db. The operation will return SQLITE_LOCKED
! 115430: ** to the user because it requires a lock that will not be available
! 115431: ** until connection pBlocker concludes its current transaction.
! 115432: */
! 115433: SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
! 115434: enterMutex();
! 115435: if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
! 115436: addToBlockedList(db);
! 115437: }
! 115438: db->pBlockingConnection = pBlocker;
! 115439: leaveMutex();
! 115440: }
! 115441:
! 115442: /*
! 115443: ** This function is called when
! 115444: ** the transaction opened by database db has just finished. Locks held
! 115445: ** by database connection db have been released.
! 115446: **
! 115447: ** This function loops through each entry in the blocked connections
! 115448: ** list and does the following:
! 115449: **
! 115450: ** 1) If the sqlite3.pBlockingConnection member of a list entry is
! 115451: ** set to db, then set pBlockingConnection=0.
! 115452: **
! 115453: ** 2) If the sqlite3.pUnlockConnection member of a list entry is
! 115454: ** set to db, then invoke the configured unlock-notify callback and
! 115455: ** set pUnlockConnection=0.
! 115456: **
! 115457: ** 3) If the two steps above mean that pBlockingConnection==0 and
! 115458: ** pUnlockConnection==0, remove the entry from the blocked connections
! 115459: ** list.
! 115460: */
! 115461: SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
! 115462: void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
! 115463: int nArg = 0; /* Number of entries in aArg[] */
! 115464: sqlite3 **pp; /* Iterator variable */
! 115465: void **aArg; /* Arguments to the unlock callback */
! 115466: void **aDyn = 0; /* Dynamically allocated space for aArg[] */
! 115467: void *aStatic[16]; /* Starter space for aArg[]. No malloc required */
! 115468:
! 115469: aArg = aStatic;
! 115470: enterMutex(); /* Enter STATIC_MASTER mutex */
! 115471:
! 115472: /* This loop runs once for each entry in the blocked-connections list. */
! 115473: for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
! 115474: sqlite3 *p = *pp;
! 115475:
! 115476: /* Step 1. */
! 115477: if( p->pBlockingConnection==db ){
! 115478: p->pBlockingConnection = 0;
! 115479: }
! 115480:
! 115481: /* Step 2. */
! 115482: if( p->pUnlockConnection==db ){
! 115483: assert( p->xUnlockNotify );
! 115484: if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
! 115485: xUnlockNotify(aArg, nArg);
! 115486: nArg = 0;
! 115487: }
! 115488:
! 115489: sqlite3BeginBenignMalloc();
! 115490: assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
! 115491: assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
! 115492: if( (!aDyn && nArg==(int)ArraySize(aStatic))
! 115493: || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
! 115494: ){
! 115495: /* The aArg[] array needs to grow. */
! 115496: void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
! 115497: if( pNew ){
! 115498: memcpy(pNew, aArg, nArg*sizeof(void *));
! 115499: sqlite3_free(aDyn);
! 115500: aDyn = aArg = pNew;
! 115501: }else{
! 115502: /* This occurs when the array of context pointers that need to
! 115503: ** be passed to the unlock-notify callback is larger than the
! 115504: ** aStatic[] array allocated on the stack and the attempt to
! 115505: ** allocate a larger array from the heap has failed.
! 115506: **
! 115507: ** This is a difficult situation to handle. Returning an error
! 115508: ** code to the caller is insufficient, as even if an error code
! 115509: ** is returned the transaction on connection db will still be
! 115510: ** closed and the unlock-notify callbacks on blocked connections
! 115511: ** will go unissued. This might cause the application to wait
! 115512: ** indefinitely for an unlock-notify callback that will never
! 115513: ** arrive.
! 115514: **
! 115515: ** Instead, invoke the unlock-notify callback with the context
! 115516: ** array already accumulated. We can then clear the array and
! 115517: ** begin accumulating any further context pointers without
! 115518: ** requiring any dynamic allocation. This is sub-optimal because
! 115519: ** it means that instead of one callback with a large array of
! 115520: ** context pointers the application will receive two or more
! 115521: ** callbacks with smaller arrays of context pointers, which will
! 115522: ** reduce the applications ability to prioritize multiple
! 115523: ** connections. But it is the best that can be done under the
! 115524: ** circumstances.
! 115525: */
! 115526: xUnlockNotify(aArg, nArg);
! 115527: nArg = 0;
! 115528: }
! 115529: }
! 115530: sqlite3EndBenignMalloc();
! 115531:
! 115532: aArg[nArg++] = p->pUnlockArg;
! 115533: xUnlockNotify = p->xUnlockNotify;
! 115534: p->pUnlockConnection = 0;
! 115535: p->xUnlockNotify = 0;
! 115536: p->pUnlockArg = 0;
! 115537: }
! 115538:
! 115539: /* Step 3. */
! 115540: if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
! 115541: /* Remove connection p from the blocked connections list. */
! 115542: *pp = p->pNextBlocked;
! 115543: p->pNextBlocked = 0;
! 115544: }else{
! 115545: pp = &p->pNextBlocked;
! 115546: }
! 115547: }
! 115548:
! 115549: if( nArg!=0 ){
! 115550: xUnlockNotify(aArg, nArg);
! 115551: }
! 115552: sqlite3_free(aDyn);
! 115553: leaveMutex(); /* Leave STATIC_MASTER mutex */
! 115554: }
! 115555:
! 115556: /*
! 115557: ** This is called when the database connection passed as an argument is
! 115558: ** being closed. The connection is removed from the blocked list.
! 115559: */
! 115560: SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
! 115561: sqlite3ConnectionUnlocked(db);
! 115562: enterMutex();
! 115563: removeFromBlockedList(db);
! 115564: checkListProperties(db);
! 115565: leaveMutex();
! 115566: }
! 115567: #endif
! 115568:
! 115569: /************** End of notify.c **********************************************/
! 115570: /************** Begin file fts3.c ********************************************/
! 115571: /*
! 115572: ** 2006 Oct 10
! 115573: **
! 115574: ** The author disclaims copyright to this source code. In place of
! 115575: ** a legal notice, here is a blessing:
! 115576: **
! 115577: ** May you do good and not evil.
! 115578: ** May you find forgiveness for yourself and forgive others.
! 115579: ** May you share freely, never taking more than you give.
! 115580: **
! 115581: ******************************************************************************
! 115582: **
! 115583: ** This is an SQLite module implementing full-text search.
! 115584: */
! 115585:
! 115586: /*
! 115587: ** The code in this file is only compiled if:
! 115588: **
! 115589: ** * The FTS3 module is being built as an extension
! 115590: ** (in which case SQLITE_CORE is not defined), or
! 115591: **
! 115592: ** * The FTS3 module is being built into the core of
! 115593: ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
! 115594: */
! 115595:
! 115596: /* The full-text index is stored in a series of b+tree (-like)
! 115597: ** structures called segments which map terms to doclists. The
! 115598: ** structures are like b+trees in layout, but are constructed from the
! 115599: ** bottom up in optimal fashion and are not updatable. Since trees
! 115600: ** are built from the bottom up, things will be described from the
! 115601: ** bottom up.
! 115602: **
! 115603: **
! 115604: **** Varints ****
! 115605: ** The basic unit of encoding is a variable-length integer called a
! 115606: ** varint. We encode variable-length integers in little-endian order
! 115607: ** using seven bits * per byte as follows:
! 115608: **
! 115609: ** KEY:
! 115610: ** A = 0xxxxxxx 7 bits of data and one flag bit
! 115611: ** B = 1xxxxxxx 7 bits of data and one flag bit
! 115612: **
! 115613: ** 7 bits - A
! 115614: ** 14 bits - BA
! 115615: ** 21 bits - BBA
! 115616: ** and so on.
! 115617: **
! 115618: ** This is similar in concept to how sqlite encodes "varints" but
! 115619: ** the encoding is not the same. SQLite varints are big-endian
! 115620: ** are are limited to 9 bytes in length whereas FTS3 varints are
! 115621: ** little-endian and can be up to 10 bytes in length (in theory).
! 115622: **
! 115623: ** Example encodings:
! 115624: **
! 115625: ** 1: 0x01
! 115626: ** 127: 0x7f
! 115627: ** 128: 0x81 0x00
! 115628: **
! 115629: **
! 115630: **** Document lists ****
! 115631: ** A doclist (document list) holds a docid-sorted list of hits for a
! 115632: ** given term. Doclists hold docids and associated token positions.
! 115633: ** A docid is the unique integer identifier for a single document.
! 115634: ** A position is the index of a word within the document. The first
! 115635: ** word of the document has a position of 0.
! 115636: **
! 115637: ** FTS3 used to optionally store character offsets using a compile-time
! 115638: ** option. But that functionality is no longer supported.
! 115639: **
! 115640: ** A doclist is stored like this:
! 115641: **
! 115642: ** array {
! 115643: ** varint docid;
! 115644: ** array { (position list for column 0)
! 115645: ** varint position; (2 more than the delta from previous position)
! 115646: ** }
! 115647: ** array {
! 115648: ** varint POS_COLUMN; (marks start of position list for new column)
! 115649: ** varint column; (index of new column)
! 115650: ** array {
! 115651: ** varint position; (2 more than the delta from previous position)
! 115652: ** }
! 115653: ** }
! 115654: ** varint POS_END; (marks end of positions for this document.
! 115655: ** }
! 115656: **
! 115657: ** Here, array { X } means zero or more occurrences of X, adjacent in
! 115658: ** memory. A "position" is an index of a token in the token stream
! 115659: ** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
! 115660: ** in the same logical place as the position element, and act as sentinals
! 115661: ** ending a position list array. POS_END is 0. POS_COLUMN is 1.
! 115662: ** The positions numbers are not stored literally but rather as two more
! 115663: ** than the difference from the prior position, or the just the position plus
! 115664: ** 2 for the first position. Example:
! 115665: **
! 115666: ** label: A B C D E F G H I J K
! 115667: ** value: 123 5 9 1 1 14 35 0 234 72 0
! 115668: **
! 115669: ** The 123 value is the first docid. For column zero in this document
! 115670: ** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
! 115671: ** at D signals the start of a new column; the 1 at E indicates that the
! 115672: ** new column is column number 1. There are two positions at 12 and 45
! 115673: ** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
! 115674: ** 234 at I is the next docid. It has one position 72 (72-2) and then
! 115675: ** terminates with the 0 at K.
! 115676: **
! 115677: ** A "position-list" is the list of positions for multiple columns for
! 115678: ** a single docid. A "column-list" is the set of positions for a single
! 115679: ** column. Hence, a position-list consists of one or more column-lists,
! 115680: ** a document record consists of a docid followed by a position-list and
! 115681: ** a doclist consists of one or more document records.
! 115682: **
! 115683: ** A bare doclist omits the position information, becoming an
! 115684: ** array of varint-encoded docids.
! 115685: **
! 115686: **** Segment leaf nodes ****
! 115687: ** Segment leaf nodes store terms and doclists, ordered by term. Leaf
! 115688: ** nodes are written using LeafWriter, and read using LeafReader (to
! 115689: ** iterate through a single leaf node's data) and LeavesReader (to
! 115690: ** iterate through a segment's entire leaf layer). Leaf nodes have
! 115691: ** the format:
! 115692: **
! 115693: ** varint iHeight; (height from leaf level, always 0)
! 115694: ** varint nTerm; (length of first term)
! 115695: ** char pTerm[nTerm]; (content of first term)
! 115696: ** varint nDoclist; (length of term's associated doclist)
! 115697: ** char pDoclist[nDoclist]; (content of doclist)
! 115698: ** array {
! 115699: ** (further terms are delta-encoded)
! 115700: ** varint nPrefix; (length of prefix shared with previous term)
! 115701: ** varint nSuffix; (length of unshared suffix)
! 115702: ** char pTermSuffix[nSuffix];(unshared suffix of next term)
! 115703: ** varint nDoclist; (length of term's associated doclist)
! 115704: ** char pDoclist[nDoclist]; (content of doclist)
! 115705: ** }
! 115706: **
! 115707: ** Here, array { X } means zero or more occurrences of X, adjacent in
! 115708: ** memory.
! 115709: **
! 115710: ** Leaf nodes are broken into blocks which are stored contiguously in
! 115711: ** the %_segments table in sorted order. This means that when the end
! 115712: ** of a node is reached, the next term is in the node with the next
! 115713: ** greater node id.
! 115714: **
! 115715: ** New data is spilled to a new leaf node when the current node
! 115716: ** exceeds LEAF_MAX bytes (default 2048). New data which itself is
! 115717: ** larger than STANDALONE_MIN (default 1024) is placed in a standalone
! 115718: ** node (a leaf node with a single term and doclist). The goal of
! 115719: ** these settings is to pack together groups of small doclists while
! 115720: ** making it efficient to directly access large doclists. The
! 115721: ** assumption is that large doclists represent terms which are more
! 115722: ** likely to be query targets.
! 115723: **
! 115724: ** TODO(shess) It may be useful for blocking decisions to be more
! 115725: ** dynamic. For instance, it may make more sense to have a 2.5k leaf
! 115726: ** node rather than splitting into 2k and .5k nodes. My intuition is
! 115727: ** that this might extend through 2x or 4x the pagesize.
! 115728: **
! 115729: **
! 115730: **** Segment interior nodes ****
! 115731: ** Segment interior nodes store blockids for subtree nodes and terms
! 115732: ** to describe what data is stored by the each subtree. Interior
! 115733: ** nodes are written using InteriorWriter, and read using
! 115734: ** InteriorReader. InteriorWriters are created as needed when
! 115735: ** SegmentWriter creates new leaf nodes, or when an interior node
! 115736: ** itself grows too big and must be split. The format of interior
! 115737: ** nodes:
! 115738: **
! 115739: ** varint iHeight; (height from leaf level, always >0)
! 115740: ** varint iBlockid; (block id of node's leftmost subtree)
! 115741: ** optional {
! 115742: ** varint nTerm; (length of first term)
! 115743: ** char pTerm[nTerm]; (content of first term)
! 115744: ** array {
! 115745: ** (further terms are delta-encoded)
! 115746: ** varint nPrefix; (length of shared prefix with previous term)
! 115747: ** varint nSuffix; (length of unshared suffix)
! 115748: ** char pTermSuffix[nSuffix]; (unshared suffix of next term)
! 115749: ** }
! 115750: ** }
! 115751: **
! 115752: ** Here, optional { X } means an optional element, while array { X }
! 115753: ** means zero or more occurrences of X, adjacent in memory.
! 115754: **
! 115755: ** An interior node encodes n terms separating n+1 subtrees. The
! 115756: ** subtree blocks are contiguous, so only the first subtree's blockid
! 115757: ** is encoded. The subtree at iBlockid will contain all terms less
! 115758: ** than the first term encoded (or all terms if no term is encoded).
! 115759: ** Otherwise, for terms greater than or equal to pTerm[i] but less
! 115760: ** than pTerm[i+1], the subtree for that term will be rooted at
! 115761: ** iBlockid+i. Interior nodes only store enough term data to
! 115762: ** distinguish adjacent children (if the rightmost term of the left
! 115763: ** child is "something", and the leftmost term of the right child is
! 115764: ** "wicked", only "w" is stored).
! 115765: **
! 115766: ** New data is spilled to a new interior node at the same height when
! 115767: ** the current node exceeds INTERIOR_MAX bytes (default 2048).
! 115768: ** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
! 115769: ** interior nodes and making the tree too skinny. The interior nodes
! 115770: ** at a given height are naturally tracked by interior nodes at
! 115771: ** height+1, and so on.
! 115772: **
! 115773: **
! 115774: **** Segment directory ****
! 115775: ** The segment directory in table %_segdir stores meta-information for
! 115776: ** merging and deleting segments, and also the root node of the
! 115777: ** segment's tree.
! 115778: **
! 115779: ** The root node is the top node of the segment's tree after encoding
! 115780: ** the entire segment, restricted to ROOT_MAX bytes (default 1024).
! 115781: ** This could be either a leaf node or an interior node. If the top
! 115782: ** node requires more than ROOT_MAX bytes, it is flushed to %_segments
! 115783: ** and a new root interior node is generated (which should always fit
! 115784: ** within ROOT_MAX because it only needs space for 2 varints, the
! 115785: ** height and the blockid of the previous root).
! 115786: **
! 115787: ** The meta-information in the segment directory is:
! 115788: ** level - segment level (see below)
! 115789: ** idx - index within level
! 115790: ** - (level,idx uniquely identify a segment)
! 115791: ** start_block - first leaf node
! 115792: ** leaves_end_block - last leaf node
! 115793: ** end_block - last block (including interior nodes)
! 115794: ** root - contents of root node
! 115795: **
! 115796: ** If the root node is a leaf node, then start_block,
! 115797: ** leaves_end_block, and end_block are all 0.
! 115798: **
! 115799: **
! 115800: **** Segment merging ****
! 115801: ** To amortize update costs, segments are grouped into levels and
! 115802: ** merged in batches. Each increase in level represents exponentially
! 115803: ** more documents.
! 115804: **
! 115805: ** New documents (actually, document updates) are tokenized and
! 115806: ** written individually (using LeafWriter) to a level 0 segment, with
! 115807: ** incrementing idx. When idx reaches MERGE_COUNT (default 16), all
! 115808: ** level 0 segments are merged into a single level 1 segment. Level 1
! 115809: ** is populated like level 0, and eventually MERGE_COUNT level 1
! 115810: ** segments are merged to a single level 2 segment (representing
! 115811: ** MERGE_COUNT^2 updates), and so on.
! 115812: **
! 115813: ** A segment merge traverses all segments at a given level in
! 115814: ** parallel, performing a straightforward sorted merge. Since segment
! 115815: ** leaf nodes are written in to the %_segments table in order, this
! 115816: ** merge traverses the underlying sqlite disk structures efficiently.
! 115817: ** After the merge, all segment blocks from the merged level are
! 115818: ** deleted.
! 115819: **
! 115820: ** MERGE_COUNT controls how often we merge segments. 16 seems to be
! 115821: ** somewhat of a sweet spot for insertion performance. 32 and 64 show
! 115822: ** very similar performance numbers to 16 on insertion, though they're
! 115823: ** a tiny bit slower (perhaps due to more overhead in merge-time
! 115824: ** sorting). 8 is about 20% slower than 16, 4 about 50% slower than
! 115825: ** 16, 2 about 66% slower than 16.
! 115826: **
! 115827: ** At query time, high MERGE_COUNT increases the number of segments
! 115828: ** which need to be scanned and merged. For instance, with 100k docs
! 115829: ** inserted:
! 115830: **
! 115831: ** MERGE_COUNT segments
! 115832: ** 16 25
! 115833: ** 8 12
! 115834: ** 4 10
! 115835: ** 2 6
! 115836: **
! 115837: ** This appears to have only a moderate impact on queries for very
! 115838: ** frequent terms (which are somewhat dominated by segment merge
! 115839: ** costs), and infrequent and non-existent terms still seem to be fast
! 115840: ** even with many segments.
! 115841: **
! 115842: ** TODO(shess) That said, it would be nice to have a better query-side
! 115843: ** argument for MERGE_COUNT of 16. Also, it is possible/likely that
! 115844: ** optimizations to things like doclist merging will swing the sweet
! 115845: ** spot around.
! 115846: **
! 115847: **
! 115848: **
! 115849: **** Handling of deletions and updates ****
! 115850: ** Since we're using a segmented structure, with no docid-oriented
! 115851: ** index into the term index, we clearly cannot simply update the term
! 115852: ** index when a document is deleted or updated. For deletions, we
! 115853: ** write an empty doclist (varint(docid) varint(POS_END)), for updates
! 115854: ** we simply write the new doclist. Segment merges overwrite older
! 115855: ** data for a particular docid with newer data, so deletes or updates
! 115856: ** will eventually overtake the earlier data and knock it out. The
! 115857: ** query logic likewise merges doclists so that newer data knocks out
! 115858: ** older data.
! 115859: **
! 115860: ** TODO(shess) Provide a VACUUM type operation to clear out all
! 115861: ** deletions and duplications. This would basically be a forced merge
! 115862: ** into a single segment.
! 115863: */
! 115864:
! 115865: /************** Include fts3Int.h in the middle of fts3.c ********************/
! 115866: /************** Begin file fts3Int.h *****************************************/
! 115867: /*
! 115868: ** 2009 Nov 12
! 115869: **
! 115870: ** The author disclaims copyright to this source code. In place of
! 115871: ** a legal notice, here is a blessing:
! 115872: **
! 115873: ** May you do good and not evil.
! 115874: ** May you find forgiveness for yourself and forgive others.
! 115875: ** May you share freely, never taking more than you give.
! 115876: **
! 115877: ******************************************************************************
! 115878: **
! 115879: */
! 115880: #ifndef _FTSINT_H
! 115881: #define _FTSINT_H
! 115882:
! 115883: #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
! 115884: # define NDEBUG 1
! 115885: #endif
! 115886:
! 115887: /*
! 115888: ** FTS4 is really an extension for FTS3. It is enabled using the
! 115889: ** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
! 115890: ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
! 115891: */
! 115892: #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
! 115893: # define SQLITE_ENABLE_FTS3
! 115894: #endif
! 115895:
! 115896: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
! 115897:
! 115898: /* If not building as part of the core, include sqlite3ext.h. */
! 115899: #ifndef SQLITE_CORE
! 115900: SQLITE_API extern const sqlite3_api_routines *sqlite3_api;
! 115901: #endif
! 115902:
! 115903: /************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
! 115904: /************** Begin file fts3_tokenizer.h **********************************/
! 115905: /*
! 115906: ** 2006 July 10
! 115907: **
! 115908: ** The author disclaims copyright to this source code.
! 115909: **
! 115910: *************************************************************************
! 115911: ** Defines the interface to tokenizers used by fulltext-search. There
! 115912: ** are three basic components:
! 115913: **
! 115914: ** sqlite3_tokenizer_module is a singleton defining the tokenizer
! 115915: ** interface functions. This is essentially the class structure for
! 115916: ** tokenizers.
! 115917: **
! 115918: ** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
! 115919: ** including customization information defined at creation time.
! 115920: **
! 115921: ** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
! 115922: ** tokens from a particular input.
! 115923: */
! 115924: #ifndef _FTS3_TOKENIZER_H_
! 115925: #define _FTS3_TOKENIZER_H_
! 115926:
! 115927: /* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
! 115928: ** If tokenizers are to be allowed to call sqlite3_*() functions, then
! 115929: ** we will need a way to register the API consistently.
! 115930: */
! 115931:
! 115932: /*
! 115933: ** Structures used by the tokenizer interface. When a new tokenizer
! 115934: ** implementation is registered, the caller provides a pointer to
! 115935: ** an sqlite3_tokenizer_module containing pointers to the callback
! 115936: ** functions that make up an implementation.
! 115937: **
! 115938: ** When an fts3 table is created, it passes any arguments passed to
! 115939: ** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
! 115940: ** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
! 115941: ** implementation. The xCreate() function in turn returns an
! 115942: ** sqlite3_tokenizer structure representing the specific tokenizer to
! 115943: ** be used for the fts3 table (customized by the tokenizer clause arguments).
! 115944: **
! 115945: ** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
! 115946: ** method is called. It returns an sqlite3_tokenizer_cursor object
! 115947: ** that may be used to tokenize a specific input buffer based on
! 115948: ** the tokenization rules supplied by a specific sqlite3_tokenizer
! 115949: ** object.
! 115950: */
! 115951: typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
! 115952: typedef struct sqlite3_tokenizer sqlite3_tokenizer;
! 115953: typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
! 115954:
! 115955: struct sqlite3_tokenizer_module {
! 115956:
! 115957: /*
! 115958: ** Structure version. Should always be set to 0.
! 115959: */
! 115960: int iVersion;
! 115961:
! 115962: /*
! 115963: ** Create a new tokenizer. The values in the argv[] array are the
! 115964: ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
! 115965: ** TABLE statement that created the fts3 table. For example, if
! 115966: ** the following SQL is executed:
! 115967: **
! 115968: ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
! 115969: **
! 115970: ** then argc is set to 2, and the argv[] array contains pointers
! 115971: ** to the strings "arg1" and "arg2".
! 115972: **
! 115973: ** This method should return either SQLITE_OK (0), or an SQLite error
! 115974: ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
! 115975: ** to point at the newly created tokenizer structure. The generic
! 115976: ** sqlite3_tokenizer.pModule variable should not be initialised by
! 115977: ** this callback. The caller will do so.
! 115978: */
! 115979: int (*xCreate)(
! 115980: int argc, /* Size of argv array */
! 115981: const char *const*argv, /* Tokenizer argument strings */
! 115982: sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
! 115983: );
! 115984:
! 115985: /*
! 115986: ** Destroy an existing tokenizer. The fts3 module calls this method
! 115987: ** exactly once for each successful call to xCreate().
! 115988: */
! 115989: int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
! 115990:
! 115991: /*
! 115992: ** Create a tokenizer cursor to tokenize an input buffer. The caller
! 115993: ** is responsible for ensuring that the input buffer remains valid
! 115994: ** until the cursor is closed (using the xClose() method).
! 115995: */
! 115996: int (*xOpen)(
! 115997: sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
! 115998: const char *pInput, int nBytes, /* Input buffer */
! 115999: sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
! 116000: );
! 116001:
! 116002: /*
! 116003: ** Destroy an existing tokenizer cursor. The fts3 module calls this
! 116004: ** method exactly once for each successful call to xOpen().
! 116005: */
! 116006: int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
! 116007:
! 116008: /*
! 116009: ** Retrieve the next token from the tokenizer cursor pCursor. This
! 116010: ** method should either return SQLITE_OK and set the values of the
! 116011: ** "OUT" variables identified below, or SQLITE_DONE to indicate that
! 116012: ** the end of the buffer has been reached, or an SQLite error code.
! 116013: **
! 116014: ** *ppToken should be set to point at a buffer containing the
! 116015: ** normalized version of the token (i.e. after any case-folding and/or
! 116016: ** stemming has been performed). *pnBytes should be set to the length
! 116017: ** of this buffer in bytes. The input text that generated the token is
! 116018: ** identified by the byte offsets returned in *piStartOffset and
! 116019: ** *piEndOffset. *piStartOffset should be set to the index of the first
! 116020: ** byte of the token in the input buffer. *piEndOffset should be set
! 116021: ** to the index of the first byte just past the end of the token in
! 116022: ** the input buffer.
! 116023: **
! 116024: ** The buffer *ppToken is set to point at is managed by the tokenizer
! 116025: ** implementation. It is only required to be valid until the next call
! 116026: ** to xNext() or xClose().
! 116027: */
! 116028: /* TODO(shess) current implementation requires pInput to be
! 116029: ** nul-terminated. This should either be fixed, or pInput/nBytes
! 116030: ** should be converted to zInput.
! 116031: */
! 116032: int (*xNext)(
! 116033: sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */
! 116034: const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */
! 116035: int *piStartOffset, /* OUT: Byte offset of token in input buffer */
! 116036: int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */
! 116037: int *piPosition /* OUT: Number of tokens returned before this one */
! 116038: );
! 116039: };
! 116040:
! 116041: struct sqlite3_tokenizer {
! 116042: const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
! 116043: /* Tokenizer implementations will typically add additional fields */
! 116044: };
! 116045:
! 116046: struct sqlite3_tokenizer_cursor {
! 116047: sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
! 116048: /* Tokenizer implementations will typically add additional fields */
! 116049: };
! 116050:
! 116051: int fts3_global_term_cnt(int iTerm, int iCol);
! 116052: int fts3_term_cnt(int iTerm, int iCol);
! 116053:
! 116054:
! 116055: #endif /* _FTS3_TOKENIZER_H_ */
! 116056:
! 116057: /************** End of fts3_tokenizer.h **************************************/
! 116058: /************** Continuing where we left off in fts3Int.h ********************/
! 116059: /************** Include fts3_hash.h in the middle of fts3Int.h ***************/
! 116060: /************** Begin file fts3_hash.h ***************************************/
! 116061: /*
! 116062: ** 2001 September 22
! 116063: **
! 116064: ** The author disclaims copyright to this source code. In place of
! 116065: ** a legal notice, here is a blessing:
! 116066: **
! 116067: ** May you do good and not evil.
! 116068: ** May you find forgiveness for yourself and forgive others.
! 116069: ** May you share freely, never taking more than you give.
! 116070: **
! 116071: *************************************************************************
! 116072: ** This is the header file for the generic hash-table implemenation
! 116073: ** used in SQLite. We've modified it slightly to serve as a standalone
! 116074: ** hash table implementation for the full-text indexing module.
! 116075: **
! 116076: */
! 116077: #ifndef _FTS3_HASH_H_
! 116078: #define _FTS3_HASH_H_
! 116079:
! 116080: /* Forward declarations of structures. */
! 116081: typedef struct Fts3Hash Fts3Hash;
! 116082: typedef struct Fts3HashElem Fts3HashElem;
! 116083:
! 116084: /* A complete hash table is an instance of the following structure.
! 116085: ** The internals of this structure are intended to be opaque -- client
! 116086: ** code should not attempt to access or modify the fields of this structure
! 116087: ** directly. Change this structure only by using the routines below.
! 116088: ** However, many of the "procedures" and "functions" for modifying and
! 116089: ** accessing this structure are really macros, so we can't really make
! 116090: ** this structure opaque.
! 116091: */
! 116092: struct Fts3Hash {
! 116093: char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */
! 116094: char copyKey; /* True if copy of key made on insert */
! 116095: int count; /* Number of entries in this table */
! 116096: Fts3HashElem *first; /* The first element of the array */
! 116097: int htsize; /* Number of buckets in the hash table */
! 116098: struct _fts3ht { /* the hash table */
! 116099: int count; /* Number of entries with this hash */
! 116100: Fts3HashElem *chain; /* Pointer to first entry with this hash */
! 116101: } *ht;
! 116102: };
! 116103:
! 116104: /* Each element in the hash table is an instance of the following
! 116105: ** structure. All elements are stored on a single doubly-linked list.
! 116106: **
! 116107: ** Again, this structure is intended to be opaque, but it can't really
! 116108: ** be opaque because it is used by macros.
! 116109: */
! 116110: struct Fts3HashElem {
! 116111: Fts3HashElem *next, *prev; /* Next and previous elements in the table */
! 116112: void *data; /* Data associated with this element */
! 116113: void *pKey; int nKey; /* Key associated with this element */
! 116114: };
! 116115:
! 116116: /*
! 116117: ** There are 2 different modes of operation for a hash table:
! 116118: **
! 116119: ** FTS3_HASH_STRING pKey points to a string that is nKey bytes long
! 116120: ** (including the null-terminator, if any). Case
! 116121: ** is respected in comparisons.
! 116122: **
! 116123: ** FTS3_HASH_BINARY pKey points to binary data nKey bytes long.
! 116124: ** memcmp() is used to compare keys.
! 116125: **
! 116126: ** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
! 116127: */
! 116128: #define FTS3_HASH_STRING 1
! 116129: #define FTS3_HASH_BINARY 2
! 116130:
! 116131: /*
! 116132: ** Access routines. To delete, insert a NULL pointer.
! 116133: */
! 116134: SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
! 116135: SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
! 116136: SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
! 116137: SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
! 116138: SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
! 116139:
! 116140: /*
! 116141: ** Shorthand for the functions above
! 116142: */
! 116143: #define fts3HashInit sqlite3Fts3HashInit
! 116144: #define fts3HashInsert sqlite3Fts3HashInsert
! 116145: #define fts3HashFind sqlite3Fts3HashFind
! 116146: #define fts3HashClear sqlite3Fts3HashClear
! 116147: #define fts3HashFindElem sqlite3Fts3HashFindElem
! 116148:
! 116149: /*
! 116150: ** Macros for looping over all elements of a hash table. The idiom is
! 116151: ** like this:
! 116152: **
! 116153: ** Fts3Hash h;
! 116154: ** Fts3HashElem *p;
! 116155: ** ...
! 116156: ** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
! 116157: ** SomeStructure *pData = fts3HashData(p);
! 116158: ** // do something with pData
! 116159: ** }
! 116160: */
! 116161: #define fts3HashFirst(H) ((H)->first)
! 116162: #define fts3HashNext(E) ((E)->next)
! 116163: #define fts3HashData(E) ((E)->data)
! 116164: #define fts3HashKey(E) ((E)->pKey)
! 116165: #define fts3HashKeysize(E) ((E)->nKey)
! 116166:
! 116167: /*
! 116168: ** Number of entries in a hash table
! 116169: */
! 116170: #define fts3HashCount(H) ((H)->count)
! 116171:
! 116172: #endif /* _FTS3_HASH_H_ */
! 116173:
! 116174: /************** End of fts3_hash.h *******************************************/
! 116175: /************** Continuing where we left off in fts3Int.h ********************/
! 116176:
! 116177: /*
! 116178: ** This constant controls how often segments are merged. Once there are
! 116179: ** FTS3_MERGE_COUNT segments of level N, they are merged into a single
! 116180: ** segment of level N+1.
! 116181: */
! 116182: #define FTS3_MERGE_COUNT 16
! 116183:
! 116184: /*
! 116185: ** This is the maximum amount of data (in bytes) to store in the
! 116186: ** Fts3Table.pendingTerms hash table. Normally, the hash table is
! 116187: ** populated as documents are inserted/updated/deleted in a transaction
! 116188: ** and used to create a new segment when the transaction is committed.
! 116189: ** However if this limit is reached midway through a transaction, a new
! 116190: ** segment is created and the hash table cleared immediately.
! 116191: */
! 116192: #define FTS3_MAX_PENDING_DATA (1*1024*1024)
! 116193:
! 116194: /*
! 116195: ** Macro to return the number of elements in an array. SQLite has a
! 116196: ** similar macro called ArraySize(). Use a different name to avoid
! 116197: ** a collision when building an amalgamation with built-in FTS3.
! 116198: */
! 116199: #define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
! 116200:
! 116201:
! 116202: #ifndef MIN
! 116203: # define MIN(x,y) ((x)<(y)?(x):(y))
! 116204: #endif
! 116205:
! 116206: /*
! 116207: ** Maximum length of a varint encoded integer. The varint format is different
! 116208: ** from that used by SQLite, so the maximum length is 10, not 9.
! 116209: */
! 116210: #define FTS3_VARINT_MAX 10
! 116211:
! 116212: /*
! 116213: ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
! 116214: ** in the document set and zero or more prefix indexes. All indexes are stored
! 116215: ** as one or more b+-trees in the %_segments and %_segdir tables.
! 116216: **
! 116217: ** It is possible to determine which index a b+-tree belongs to based on the
! 116218: ** value stored in the "%_segdir.level" column. Given this value L, the index
! 116219: ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
! 116220: ** level values between 0 and 1023 (inclusive) belong to index 0, all levels
! 116221: ** between 1024 and 2047 to index 1, and so on.
! 116222: **
! 116223: ** It is considered impossible for an index to use more than 1024 levels. In
! 116224: ** theory though this may happen, but only after at least
! 116225: ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
! 116226: */
! 116227: #define FTS3_SEGDIR_MAXLEVEL 1024
! 116228: #define FTS3_SEGDIR_MAXLEVEL_STR "1024"
! 116229:
! 116230: /*
! 116231: ** The testcase() macro is only used by the amalgamation. If undefined,
! 116232: ** make it a no-op.
! 116233: */
! 116234: #ifndef testcase
! 116235: # define testcase(X)
! 116236: #endif
! 116237:
! 116238: /*
! 116239: ** Terminator values for position-lists and column-lists.
! 116240: */
! 116241: #define POS_COLUMN (1) /* Column-list terminator */
! 116242: #define POS_END (0) /* Position-list terminator */
! 116243:
! 116244: /*
! 116245: ** This section provides definitions to allow the
! 116246: ** FTS3 extension to be compiled outside of the
! 116247: ** amalgamation.
! 116248: */
! 116249: #ifndef SQLITE_AMALGAMATION
! 116250: /*
! 116251: ** Macros indicating that conditional expressions are always true or
! 116252: ** false.
! 116253: */
! 116254: #ifdef SQLITE_COVERAGE_TEST
! 116255: # define ALWAYS(x) (1)
! 116256: # define NEVER(X) (0)
! 116257: #else
! 116258: # define ALWAYS(x) (x)
! 116259: # define NEVER(X) (x)
! 116260: #endif
! 116261:
! 116262: /*
! 116263: ** Internal types used by SQLite.
! 116264: */
! 116265: typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
! 116266: typedef short int i16; /* 2-byte (or larger) signed integer */
! 116267: typedef unsigned int u32; /* 4-byte unsigned integer */
! 116268: typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
! 116269:
! 116270: /*
! 116271: ** Macro used to suppress compiler warnings for unused parameters.
! 116272: */
! 116273: #define UNUSED_PARAMETER(x) (void)(x)
! 116274:
! 116275: /*
! 116276: ** Activate assert() only if SQLITE_TEST is enabled.
! 116277: */
! 116278: #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
! 116279: # define NDEBUG 1
! 116280: #endif
! 116281:
! 116282: /*
! 116283: ** The TESTONLY macro is used to enclose variable declarations or
! 116284: ** other bits of code that are needed to support the arguments
! 116285: ** within testcase() and assert() macros.
! 116286: */
! 116287: #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
! 116288: # define TESTONLY(X) X
! 116289: #else
! 116290: # define TESTONLY(X)
! 116291: #endif
! 116292:
! 116293: #endif /* SQLITE_AMALGAMATION */
! 116294:
! 116295: #ifdef SQLITE_DEBUG
! 116296: SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
! 116297: # define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
! 116298: #else
! 116299: # define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
! 116300: #endif
! 116301:
! 116302: typedef struct Fts3Table Fts3Table;
! 116303: typedef struct Fts3Cursor Fts3Cursor;
! 116304: typedef struct Fts3Expr Fts3Expr;
! 116305: typedef struct Fts3Phrase Fts3Phrase;
! 116306: typedef struct Fts3PhraseToken Fts3PhraseToken;
! 116307:
! 116308: typedef struct Fts3Doclist Fts3Doclist;
! 116309: typedef struct Fts3SegFilter Fts3SegFilter;
! 116310: typedef struct Fts3DeferredToken Fts3DeferredToken;
! 116311: typedef struct Fts3SegReader Fts3SegReader;
! 116312: typedef struct Fts3MultiSegReader Fts3MultiSegReader;
! 116313:
! 116314: /*
! 116315: ** A connection to a fulltext index is an instance of the following
! 116316: ** structure. The xCreate and xConnect methods create an instance
! 116317: ** of this structure and xDestroy and xDisconnect free that instance.
! 116318: ** All other methods receive a pointer to the structure as one of their
! 116319: ** arguments.
! 116320: */
! 116321: struct Fts3Table {
! 116322: sqlite3_vtab base; /* Base class used by SQLite core */
! 116323: sqlite3 *db; /* The database connection */
! 116324: const char *zDb; /* logical database name */
! 116325: const char *zName; /* virtual table name */
! 116326: int nColumn; /* number of named columns in virtual table */
! 116327: char **azColumn; /* column names. malloced */
! 116328: sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
! 116329: char *zContentTbl; /* content=xxx option, or NULL */
! 116330:
! 116331: /* Precompiled statements used by the implementation. Each of these
! 116332: ** statements is run and reset within a single virtual table API call.
! 116333: */
! 116334: sqlite3_stmt *aStmt[27];
! 116335:
! 116336: char *zReadExprlist;
! 116337: char *zWriteExprlist;
! 116338:
! 116339: int nNodeSize; /* Soft limit for node size */
! 116340: u8 bHasStat; /* True if %_stat table exists */
! 116341: u8 bHasDocsize; /* True if %_docsize table exists */
! 116342: u8 bDescIdx; /* True if doclists are in reverse order */
! 116343: int nPgsz; /* Page size for host database */
! 116344: char *zSegmentsTbl; /* Name of %_segments table */
! 116345: sqlite3_blob *pSegments; /* Blob handle open on %_segments table */
! 116346:
! 116347: /* TODO: Fix the first paragraph of this comment.
! 116348: **
! 116349: ** The following hash table is used to buffer pending index updates during
! 116350: ** transactions. Variable nPendingData estimates the memory size of the
! 116351: ** pending data, including hash table overhead, but not malloc overhead.
! 116352: ** When nPendingData exceeds nMaxPendingData, the buffer is flushed
! 116353: ** automatically. Variable iPrevDocid is the docid of the most recently
! 116354: ** inserted record.
! 116355: **
! 116356: ** A single FTS4 table may have multiple full-text indexes. For each index
! 116357: ** there is an entry in the aIndex[] array. Index 0 is an index of all the
! 116358: ** terms that appear in the document set. Each subsequent index in aIndex[]
! 116359: ** is an index of prefixes of a specific length.
! 116360: */
! 116361: int nIndex; /* Size of aIndex[] */
! 116362: struct Fts3Index {
! 116363: int nPrefix; /* Prefix length (0 for main terms index) */
! 116364: Fts3Hash hPending; /* Pending terms table for this index */
! 116365: } *aIndex;
! 116366: int nMaxPendingData; /* Max pending data before flush to disk */
! 116367: int nPendingData; /* Current bytes of pending data */
! 116368: sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */
! 116369:
! 116370: #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
! 116371: /* State variables used for validating that the transaction control
! 116372: ** methods of the virtual table are called at appropriate times. These
! 116373: ** values do not contribution to the FTS computation; they are used for
! 116374: ** verifying the SQLite core.
! 116375: */
! 116376: int inTransaction; /* True after xBegin but before xCommit/xRollback */
! 116377: int mxSavepoint; /* Largest valid xSavepoint integer */
! 116378: #endif
! 116379: };
! 116380:
! 116381: /*
! 116382: ** When the core wants to read from the virtual table, it creates a
! 116383: ** virtual table cursor (an instance of the following structure) using
! 116384: ** the xOpen method. Cursors are destroyed using the xClose method.
! 116385: */
! 116386: struct Fts3Cursor {
! 116387: sqlite3_vtab_cursor base; /* Base class used by SQLite core */
! 116388: i16 eSearch; /* Search strategy (see below) */
! 116389: u8 isEof; /* True if at End Of Results */
! 116390: u8 isRequireSeek; /* True if must seek pStmt to %_content row */
! 116391: sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
! 116392: Fts3Expr *pExpr; /* Parsed MATCH query string */
! 116393: int nPhrase; /* Number of matchable phrases in query */
! 116394: Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
! 116395: sqlite3_int64 iPrevId; /* Previous id read from aDoclist */
! 116396: char *pNextId; /* Pointer into the body of aDoclist */
! 116397: char *aDoclist; /* List of docids for full-text queries */
! 116398: int nDoclist; /* Size of buffer at aDoclist */
! 116399: u8 bDesc; /* True to sort in descending order */
! 116400: int eEvalmode; /* An FTS3_EVAL_XX constant */
! 116401: int nRowAvg; /* Average size of database rows, in pages */
! 116402: sqlite3_int64 nDoc; /* Documents in table */
! 116403:
! 116404: int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
! 116405: u32 *aMatchinfo; /* Information about most recent match */
! 116406: int nMatchinfo; /* Number of elements in aMatchinfo[] */
! 116407: char *zMatchinfo; /* Matchinfo specification */
! 116408: };
! 116409:
! 116410: #define FTS3_EVAL_FILTER 0
! 116411: #define FTS3_EVAL_NEXT 1
! 116412: #define FTS3_EVAL_MATCHINFO 2
! 116413:
! 116414: /*
! 116415: ** The Fts3Cursor.eSearch member is always set to one of the following.
! 116416: ** Actualy, Fts3Cursor.eSearch can be greater than or equal to
! 116417: ** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
! 116418: ** of the column to be searched. For example, in
! 116419: **
! 116420: ** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
! 116421: ** SELECT docid FROM ex1 WHERE b MATCH 'one two three';
! 116422: **
! 116423: ** Because the LHS of the MATCH operator is 2nd column "b",
! 116424: ** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a,
! 116425: ** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1"
! 116426: ** indicating that all columns should be searched,
! 116427: ** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
! 116428: */
! 116429: #define FTS3_FULLSCAN_SEARCH 0 /* Linear scan of %_content table */
! 116430: #define FTS3_DOCID_SEARCH 1 /* Lookup by rowid on %_content table */
! 116431: #define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
! 116432:
! 116433:
! 116434: struct Fts3Doclist {
! 116435: char *aAll; /* Array containing doclist (or NULL) */
! 116436: int nAll; /* Size of a[] in bytes */
! 116437: char *pNextDocid; /* Pointer to next docid */
! 116438:
! 116439: sqlite3_int64 iDocid; /* Current docid (if pList!=0) */
! 116440: int bFreeList; /* True if pList should be sqlite3_free()d */
! 116441: char *pList; /* Pointer to position list following iDocid */
! 116442: int nList; /* Length of position list */
! 116443: };
! 116444:
! 116445: /*
! 116446: ** A "phrase" is a sequence of one or more tokens that must match in
! 116447: ** sequence. A single token is the base case and the most common case.
! 116448: ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
! 116449: ** nToken will be the number of tokens in the string.
! 116450: */
! 116451: struct Fts3PhraseToken {
! 116452: char *z; /* Text of the token */
! 116453: int n; /* Number of bytes in buffer z */
! 116454: int isPrefix; /* True if token ends with a "*" character */
! 116455: int bFirst; /* True if token must appear at position 0 */
! 116456:
! 116457: /* Variables above this point are populated when the expression is
! 116458: ** parsed (by code in fts3_expr.c). Below this point the variables are
! 116459: ** used when evaluating the expression. */
! 116460: Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
! 116461: Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
! 116462: };
! 116463:
! 116464: struct Fts3Phrase {
! 116465: /* Cache of doclist for this phrase. */
! 116466: Fts3Doclist doclist;
! 116467: int bIncr; /* True if doclist is loaded incrementally */
! 116468: int iDoclistToken;
! 116469:
! 116470: /* Variables below this point are populated by fts3_expr.c when parsing
! 116471: ** a MATCH expression. Everything above is part of the evaluation phase.
! 116472: */
! 116473: int nToken; /* Number of tokens in the phrase */
! 116474: int iColumn; /* Index of column this phrase must match */
! 116475: Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
! 116476: };
! 116477:
! 116478: /*
! 116479: ** A tree of these objects forms the RHS of a MATCH operator.
! 116480: **
! 116481: ** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
! 116482: ** points to a malloced buffer, size nDoclist bytes, containing the results
! 116483: ** of this phrase query in FTS3 doclist format. As usual, the initial
! 116484: ** "Length" field found in doclists stored on disk is omitted from this
! 116485: ** buffer.
! 116486: **
! 116487: ** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
! 116488: ** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
! 116489: ** where nCol is the number of columns in the queried FTS table. The array
! 116490: ** is populated as follows:
! 116491: **
! 116492: ** aMI[iCol*3 + 0] = Undefined
! 116493: ** aMI[iCol*3 + 1] = Number of occurrences
! 116494: ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
! 116495: **
! 116496: ** The aMI array is allocated using sqlite3_malloc(). It should be freed
! 116497: ** when the expression node is.
! 116498: */
! 116499: struct Fts3Expr {
! 116500: int eType; /* One of the FTSQUERY_XXX values defined below */
! 116501: int nNear; /* Valid if eType==FTSQUERY_NEAR */
! 116502: Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
! 116503: Fts3Expr *pLeft; /* Left operand */
! 116504: Fts3Expr *pRight; /* Right operand */
! 116505: Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */
! 116506:
! 116507: /* The following are used by the fts3_eval.c module. */
! 116508: sqlite3_int64 iDocid; /* Current docid */
! 116509: u8 bEof; /* True this expression is at EOF already */
! 116510: u8 bStart; /* True if iDocid is valid */
! 116511: u8 bDeferred; /* True if this expression is entirely deferred */
! 116512:
! 116513: u32 *aMI;
! 116514: };
! 116515:
! 116516: /*
! 116517: ** Candidate values for Fts3Query.eType. Note that the order of the first
! 116518: ** four values is in order of precedence when parsing expressions. For
! 116519: ** example, the following:
! 116520: **
! 116521: ** "a OR b AND c NOT d NEAR e"
! 116522: **
! 116523: ** is equivalent to:
! 116524: **
! 116525: ** "a OR (b AND (c NOT (d NEAR e)))"
! 116526: */
! 116527: #define FTSQUERY_NEAR 1
! 116528: #define FTSQUERY_NOT 2
! 116529: #define FTSQUERY_AND 3
! 116530: #define FTSQUERY_OR 4
! 116531: #define FTSQUERY_PHRASE 5
! 116532:
! 116533:
! 116534: /* fts3_write.c */
! 116535: SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
! 116536: SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
! 116537: SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
! 116538: SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
! 116539: SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, sqlite3_int64,
! 116540: sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
! 116541: SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
! 116542: Fts3Table*,int,const char*,int,int,Fts3SegReader**);
! 116543: SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
! 116544: SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, sqlite3_stmt **);
! 116545: SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *);
! 116546: SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
! 116547:
! 116548: SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
! 116549: SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
! 116550:
! 116551: SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
! 116552: SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
! 116553: SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
! 116554: SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
! 116555: SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
! 116556:
! 116557: /* Special values interpreted by sqlite3SegReaderCursor() */
! 116558: #define FTS3_SEGCURSOR_PENDING -1
! 116559: #define FTS3_SEGCURSOR_ALL -2
! 116560:
! 116561: SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
! 116562: SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
! 116563: SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
! 116564:
! 116565: SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
! 116566: Fts3Table *, int, int, const char *, int, int, int, Fts3MultiSegReader *);
! 116567:
! 116568: /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
! 116569: #define FTS3_SEGMENT_REQUIRE_POS 0x00000001
! 116570: #define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002
! 116571: #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
! 116572: #define FTS3_SEGMENT_PREFIX 0x00000008
! 116573: #define FTS3_SEGMENT_SCAN 0x00000010
! 116574: #define FTS3_SEGMENT_FIRST 0x00000020
! 116575:
! 116576: /* Type passed as 4th argument to SegmentReaderIterate() */
! 116577: struct Fts3SegFilter {
! 116578: const char *zTerm;
! 116579: int nTerm;
! 116580: int iCol;
! 116581: int flags;
! 116582: };
! 116583:
! 116584: struct Fts3MultiSegReader {
! 116585: /* Used internally by sqlite3Fts3SegReaderXXX() calls */
! 116586: Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
! 116587: int nSegment; /* Size of apSegment array */
! 116588: int nAdvance; /* How many seg-readers to advance */
! 116589: Fts3SegFilter *pFilter; /* Pointer to filter object */
! 116590: char *aBuffer; /* Buffer to merge doclists in */
! 116591: int nBuffer; /* Allocated size of aBuffer[] in bytes */
! 116592:
! 116593: int iColFilter; /* If >=0, filter for this column */
! 116594: int bRestart;
! 116595:
! 116596: /* Used by fts3.c only. */
! 116597: int nCost; /* Cost of running iterator */
! 116598: int bLookup; /* True if a lookup of a single entry. */
! 116599:
! 116600: /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
! 116601: char *zTerm; /* Pointer to term buffer */
! 116602: int nTerm; /* Size of zTerm in bytes */
! 116603: char *aDoclist; /* Pointer to doclist buffer */
! 116604: int nDoclist; /* Size of aDoclist[] in bytes */
! 116605: };
! 116606:
! 116607: /* fts3.c */
! 116608: SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
! 116609: SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
! 116610: SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
! 116611: SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
! 116612: SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
! 116613: SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
! 116614: SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
! 116615: SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
! 116616:
! 116617: /* fts3_tokenizer.c */
! 116618: SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
! 116619: SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
! 116620: SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
! 116621: sqlite3_tokenizer **, char **
! 116622: );
! 116623: SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
! 116624:
! 116625: /* fts3_snippet.c */
! 116626: SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
! 116627: SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
! 116628: const char *, const char *, int, int
! 116629: );
! 116630: SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
! 116631:
! 116632: /* fts3_expr.c */
! 116633: SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *,
! 116634: char **, int, int, int, const char *, int, Fts3Expr **
! 116635: );
! 116636: SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
! 116637: #ifdef SQLITE_TEST
! 116638: SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
! 116639: SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
! 116640: #endif
! 116641:
! 116642: /* fts3_aux.c */
! 116643: SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
! 116644:
! 116645: SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
! 116646:
! 116647: SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
! 116648: Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
! 116649: SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
! 116650: Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
! 116651: SQLITE_PRIVATE char *sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol);
! 116652: SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
! 116653: SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
! 116654:
! 116655: SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
! 116656:
! 116657: #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
! 116658: #endif /* _FTSINT_H */
! 116659:
! 116660: /************** End of fts3Int.h *********************************************/
! 116661: /************** Continuing where we left off in fts3.c ***********************/
! 116662: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
! 116663:
! 116664: #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
! 116665: # define SQLITE_CORE 1
! 116666: #endif
! 116667:
! 116668: /* #include <assert.h> */
! 116669: /* #include <stdlib.h> */
! 116670: /* #include <stddef.h> */
! 116671: /* #include <stdio.h> */
! 116672: /* #include <string.h> */
! 116673: /* #include <stdarg.h> */
! 116674:
! 116675: #ifndef SQLITE_CORE
! 116676: SQLITE_EXTENSION_INIT1
! 116677: #endif
! 116678:
! 116679: static int fts3EvalNext(Fts3Cursor *pCsr);
! 116680: static int fts3EvalStart(Fts3Cursor *pCsr);
! 116681: static int fts3TermSegReaderCursor(
! 116682: Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
! 116683:
! 116684: /*
! 116685: ** Write a 64-bit variable-length integer to memory starting at p[0].
! 116686: ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
! 116687: ** The number of bytes written is returned.
! 116688: */
! 116689: SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
! 116690: unsigned char *q = (unsigned char *) p;
! 116691: sqlite_uint64 vu = v;
! 116692: do{
! 116693: *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
! 116694: vu >>= 7;
! 116695: }while( vu!=0 );
! 116696: q[-1] &= 0x7f; /* turn off high bit in final byte */
! 116697: assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
! 116698: return (int) (q - (unsigned char *)p);
! 116699: }
! 116700:
! 116701: /*
! 116702: ** Read a 64-bit variable-length integer from memory starting at p[0].
! 116703: ** Return the number of bytes read, or 0 on error.
! 116704: ** The value is stored in *v.
! 116705: */
! 116706: SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
! 116707: const unsigned char *q = (const unsigned char *) p;
! 116708: sqlite_uint64 x = 0, y = 1;
! 116709: while( (*q&0x80)==0x80 && q-(unsigned char *)p<FTS3_VARINT_MAX ){
! 116710: x += y * (*q++ & 0x7f);
! 116711: y <<= 7;
! 116712: }
! 116713: x += y * (*q++);
! 116714: *v = (sqlite_int64) x;
! 116715: return (int) (q - (unsigned char *)p);
! 116716: }
! 116717:
! 116718: /*
! 116719: ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
! 116720: ** 32-bit integer before it is returned.
! 116721: */
! 116722: SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
! 116723: sqlite_int64 i;
! 116724: int ret = sqlite3Fts3GetVarint(p, &i);
! 116725: *pi = (int) i;
! 116726: return ret;
! 116727: }
! 116728:
! 116729: /*
! 116730: ** Return the number of bytes required to encode v as a varint
! 116731: */
! 116732: SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
! 116733: int i = 0;
! 116734: do{
! 116735: i++;
! 116736: v >>= 7;
! 116737: }while( v!=0 );
! 116738: return i;
! 116739: }
! 116740:
! 116741: /*
! 116742: ** Convert an SQL-style quoted string into a normal string by removing
! 116743: ** the quote characters. The conversion is done in-place. If the
! 116744: ** input does not begin with a quote character, then this routine
! 116745: ** is a no-op.
! 116746: **
! 116747: ** Examples:
! 116748: **
! 116749: ** "abc" becomes abc
! 116750: ** 'xyz' becomes xyz
! 116751: ** [pqr] becomes pqr
! 116752: ** `mno` becomes mno
! 116753: **
! 116754: */
! 116755: SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
! 116756: char quote; /* Quote character (if any ) */
! 116757:
! 116758: quote = z[0];
! 116759: if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
! 116760: int iIn = 1; /* Index of next byte to read from input */
! 116761: int iOut = 0; /* Index of next byte to write to output */
! 116762:
! 116763: /* If the first byte was a '[', then the close-quote character is a ']' */
! 116764: if( quote=='[' ) quote = ']';
! 116765:
! 116766: while( ALWAYS(z[iIn]) ){
! 116767: if( z[iIn]==quote ){
! 116768: if( z[iIn+1]!=quote ) break;
! 116769: z[iOut++] = quote;
! 116770: iIn += 2;
! 116771: }else{
! 116772: z[iOut++] = z[iIn++];
! 116773: }
! 116774: }
! 116775: z[iOut] = '\0';
! 116776: }
! 116777: }
! 116778:
! 116779: /*
! 116780: ** Read a single varint from the doclist at *pp and advance *pp to point
! 116781: ** to the first byte past the end of the varint. Add the value of the varint
! 116782: ** to *pVal.
! 116783: */
! 116784: static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
! 116785: sqlite3_int64 iVal;
! 116786: *pp += sqlite3Fts3GetVarint(*pp, &iVal);
! 116787: *pVal += iVal;
! 116788: }
! 116789:
! 116790: /*
! 116791: ** When this function is called, *pp points to the first byte following a
! 116792: ** varint that is part of a doclist (or position-list, or any other list
! 116793: ** of varints). This function moves *pp to point to the start of that varint,
! 116794: ** and sets *pVal by the varint value.
! 116795: **
! 116796: ** Argument pStart points to the first byte of the doclist that the
! 116797: ** varint is part of.
! 116798: */
! 116799: static void fts3GetReverseVarint(
! 116800: char **pp,
! 116801: char *pStart,
! 116802: sqlite3_int64 *pVal
! 116803: ){
! 116804: sqlite3_int64 iVal;
! 116805: char *p;
! 116806:
! 116807: /* Pointer p now points at the first byte past the varint we are
! 116808: ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
! 116809: ** clear on character p[-1]. */
! 116810: for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
! 116811: p++;
! 116812: *pp = p;
! 116813:
! 116814: sqlite3Fts3GetVarint(p, &iVal);
! 116815: *pVal = iVal;
! 116816: }
! 116817:
! 116818: /*
! 116819: ** The xDisconnect() virtual table method.
! 116820: */
! 116821: static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
! 116822: Fts3Table *p = (Fts3Table *)pVtab;
! 116823: int i;
! 116824:
! 116825: assert( p->nPendingData==0 );
! 116826: assert( p->pSegments==0 );
! 116827:
! 116828: /* Free any prepared statements held */
! 116829: for(i=0; i<SizeofArray(p->aStmt); i++){
! 116830: sqlite3_finalize(p->aStmt[i]);
! 116831: }
! 116832: sqlite3_free(p->zSegmentsTbl);
! 116833: sqlite3_free(p->zReadExprlist);
! 116834: sqlite3_free(p->zWriteExprlist);
! 116835: sqlite3_free(p->zContentTbl);
! 116836:
! 116837: /* Invoke the tokenizer destructor to free the tokenizer. */
! 116838: p->pTokenizer->pModule->xDestroy(p->pTokenizer);
! 116839:
! 116840: sqlite3_free(p);
! 116841: return SQLITE_OK;
! 116842: }
! 116843:
! 116844: /*
! 116845: ** Construct one or more SQL statements from the format string given
! 116846: ** and then evaluate those statements. The success code is written
! 116847: ** into *pRc.
! 116848: **
! 116849: ** If *pRc is initially non-zero then this routine is a no-op.
! 116850: */
! 116851: static void fts3DbExec(
! 116852: int *pRc, /* Success code */
! 116853: sqlite3 *db, /* Database in which to run SQL */
! 116854: const char *zFormat, /* Format string for SQL */
! 116855: ... /* Arguments to the format string */
! 116856: ){
! 116857: va_list ap;
! 116858: char *zSql;
! 116859: if( *pRc ) return;
! 116860: va_start(ap, zFormat);
! 116861: zSql = sqlite3_vmprintf(zFormat, ap);
! 116862: va_end(ap);
! 116863: if( zSql==0 ){
! 116864: *pRc = SQLITE_NOMEM;
! 116865: }else{
! 116866: *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
! 116867: sqlite3_free(zSql);
! 116868: }
! 116869: }
! 116870:
! 116871: /*
! 116872: ** The xDestroy() virtual table method.
! 116873: */
! 116874: static int fts3DestroyMethod(sqlite3_vtab *pVtab){
! 116875: Fts3Table *p = (Fts3Table *)pVtab;
! 116876: int rc = SQLITE_OK; /* Return code */
! 116877: const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
! 116878: sqlite3 *db = p->db; /* Database handle */
! 116879:
! 116880: /* Drop the shadow tables */
! 116881: if( p->zContentTbl==0 ){
! 116882: fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
! 116883: }
! 116884: fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
! 116885: fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
! 116886: fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
! 116887: fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
! 116888:
! 116889: /* If everything has worked, invoke fts3DisconnectMethod() to free the
! 116890: ** memory associated with the Fts3Table structure and return SQLITE_OK.
! 116891: ** Otherwise, return an SQLite error code.
! 116892: */
! 116893: return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
! 116894: }
! 116895:
! 116896:
! 116897: /*
! 116898: ** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
! 116899: ** passed as the first argument. This is done as part of the xConnect()
! 116900: ** and xCreate() methods.
! 116901: **
! 116902: ** If *pRc is non-zero when this function is called, it is a no-op.
! 116903: ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
! 116904: ** before returning.
! 116905: */
! 116906: static void fts3DeclareVtab(int *pRc, Fts3Table *p){
! 116907: if( *pRc==SQLITE_OK ){
! 116908: int i; /* Iterator variable */
! 116909: int rc; /* Return code */
! 116910: char *zSql; /* SQL statement passed to declare_vtab() */
! 116911: char *zCols; /* List of user defined columns */
! 116912:
! 116913: sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
! 116914:
! 116915: /* Create a list of user columns for the virtual table */
! 116916: zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
! 116917: for(i=1; zCols && i<p->nColumn; i++){
! 116918: zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
! 116919: }
! 116920:
! 116921: /* Create the whole "CREATE TABLE" statement to pass to SQLite */
! 116922: zSql = sqlite3_mprintf(
! 116923: "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN)", zCols, p->zName
! 116924: );
! 116925: if( !zCols || !zSql ){
! 116926: rc = SQLITE_NOMEM;
! 116927: }else{
! 116928: rc = sqlite3_declare_vtab(p->db, zSql);
! 116929: }
! 116930:
! 116931: sqlite3_free(zSql);
! 116932: sqlite3_free(zCols);
! 116933: *pRc = rc;
! 116934: }
! 116935: }
! 116936:
! 116937: /*
! 116938: ** Create the backing store tables (%_content, %_segments and %_segdir)
! 116939: ** required by the FTS3 table passed as the only argument. This is done
! 116940: ** as part of the vtab xCreate() method.
! 116941: **
! 116942: ** If the p->bHasDocsize boolean is true (indicating that this is an
! 116943: ** FTS4 table, not an FTS3 table) then also create the %_docsize and
! 116944: ** %_stat tables required by FTS4.
! 116945: */
! 116946: static int fts3CreateTables(Fts3Table *p){
! 116947: int rc = SQLITE_OK; /* Return code */
! 116948: int i; /* Iterator variable */
! 116949: sqlite3 *db = p->db; /* The database connection */
! 116950:
! 116951: if( p->zContentTbl==0 ){
! 116952: char *zContentCols; /* Columns of %_content table */
! 116953:
! 116954: /* Create a list of user columns for the content table */
! 116955: zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
! 116956: for(i=0; zContentCols && i<p->nColumn; i++){
! 116957: char *z = p->azColumn[i];
! 116958: zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
! 116959: }
! 116960: if( zContentCols==0 ) rc = SQLITE_NOMEM;
! 116961:
! 116962: /* Create the content table */
! 116963: fts3DbExec(&rc, db,
! 116964: "CREATE TABLE %Q.'%q_content'(%s)",
! 116965: p->zDb, p->zName, zContentCols
! 116966: );
! 116967: sqlite3_free(zContentCols);
! 116968: }
! 116969:
! 116970: /* Create other tables */
! 116971: fts3DbExec(&rc, db,
! 116972: "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
! 116973: p->zDb, p->zName
! 116974: );
! 116975: fts3DbExec(&rc, db,
! 116976: "CREATE TABLE %Q.'%q_segdir'("
! 116977: "level INTEGER,"
! 116978: "idx INTEGER,"
! 116979: "start_block INTEGER,"
! 116980: "leaves_end_block INTEGER,"
! 116981: "end_block INTEGER,"
! 116982: "root BLOB,"
! 116983: "PRIMARY KEY(level, idx)"
! 116984: ");",
! 116985: p->zDb, p->zName
! 116986: );
! 116987: if( p->bHasDocsize ){
! 116988: fts3DbExec(&rc, db,
! 116989: "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
! 116990: p->zDb, p->zName
! 116991: );
! 116992: }
! 116993: if( p->bHasStat ){
! 116994: fts3DbExec(&rc, db,
! 116995: "CREATE TABLE %Q.'%q_stat'(id INTEGER PRIMARY KEY, value BLOB);",
! 116996: p->zDb, p->zName
! 116997: );
! 116998: }
! 116999: return rc;
! 117000: }
! 117001:
! 117002: /*
! 117003: ** Store the current database page-size in bytes in p->nPgsz.
! 117004: **
! 117005: ** If *pRc is non-zero when this function is called, it is a no-op.
! 117006: ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
! 117007: ** before returning.
! 117008: */
! 117009: static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
! 117010: if( *pRc==SQLITE_OK ){
! 117011: int rc; /* Return code */
! 117012: char *zSql; /* SQL text "PRAGMA %Q.page_size" */
! 117013: sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */
! 117014:
! 117015: zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
! 117016: if( !zSql ){
! 117017: rc = SQLITE_NOMEM;
! 117018: }else{
! 117019: rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
! 117020: if( rc==SQLITE_OK ){
! 117021: sqlite3_step(pStmt);
! 117022: p->nPgsz = sqlite3_column_int(pStmt, 0);
! 117023: rc = sqlite3_finalize(pStmt);
! 117024: }else if( rc==SQLITE_AUTH ){
! 117025: p->nPgsz = 1024;
! 117026: rc = SQLITE_OK;
! 117027: }
! 117028: }
! 117029: assert( p->nPgsz>0 || rc!=SQLITE_OK );
! 117030: sqlite3_free(zSql);
! 117031: *pRc = rc;
! 117032: }
! 117033: }
! 117034:
! 117035: /*
! 117036: ** "Special" FTS4 arguments are column specifications of the following form:
! 117037: **
! 117038: ** <key> = <value>
! 117039: **
! 117040: ** There may not be whitespace surrounding the "=" character. The <value>
! 117041: ** term may be quoted, but the <key> may not.
! 117042: */
! 117043: static int fts3IsSpecialColumn(
! 117044: const char *z,
! 117045: int *pnKey,
! 117046: char **pzValue
! 117047: ){
! 117048: char *zValue;
! 117049: const char *zCsr = z;
! 117050:
! 117051: while( *zCsr!='=' ){
! 117052: if( *zCsr=='\0' ) return 0;
! 117053: zCsr++;
! 117054: }
! 117055:
! 117056: *pnKey = (int)(zCsr-z);
! 117057: zValue = sqlite3_mprintf("%s", &zCsr[1]);
! 117058: if( zValue ){
! 117059: sqlite3Fts3Dequote(zValue);
! 117060: }
! 117061: *pzValue = zValue;
! 117062: return 1;
! 117063: }
! 117064:
! 117065: /*
! 117066: ** Append the output of a printf() style formatting to an existing string.
! 117067: */
! 117068: static void fts3Appendf(
! 117069: int *pRc, /* IN/OUT: Error code */
! 117070: char **pz, /* IN/OUT: Pointer to string buffer */
! 117071: const char *zFormat, /* Printf format string to append */
! 117072: ... /* Arguments for printf format string */
! 117073: ){
! 117074: if( *pRc==SQLITE_OK ){
! 117075: va_list ap;
! 117076: char *z;
! 117077: va_start(ap, zFormat);
! 117078: z = sqlite3_vmprintf(zFormat, ap);
! 117079: va_end(ap);
! 117080: if( z && *pz ){
! 117081: char *z2 = sqlite3_mprintf("%s%s", *pz, z);
! 117082: sqlite3_free(z);
! 117083: z = z2;
! 117084: }
! 117085: if( z==0 ) *pRc = SQLITE_NOMEM;
! 117086: sqlite3_free(*pz);
! 117087: *pz = z;
! 117088: }
! 117089: }
! 117090:
! 117091: /*
! 117092: ** Return a copy of input string zInput enclosed in double-quotes (") and
! 117093: ** with all double quote characters escaped. For example:
! 117094: **
! 117095: ** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
! 117096: **
! 117097: ** The pointer returned points to memory obtained from sqlite3_malloc(). It
! 117098: ** is the callers responsibility to call sqlite3_free() to release this
! 117099: ** memory.
! 117100: */
! 117101: static char *fts3QuoteId(char const *zInput){
! 117102: int nRet;
! 117103: char *zRet;
! 117104: nRet = 2 + strlen(zInput)*2 + 1;
! 117105: zRet = sqlite3_malloc(nRet);
! 117106: if( zRet ){
! 117107: int i;
! 117108: char *z = zRet;
! 117109: *(z++) = '"';
! 117110: for(i=0; zInput[i]; i++){
! 117111: if( zInput[i]=='"' ) *(z++) = '"';
! 117112: *(z++) = zInput[i];
! 117113: }
! 117114: *(z++) = '"';
! 117115: *(z++) = '\0';
! 117116: }
! 117117: return zRet;
! 117118: }
! 117119:
! 117120: /*
! 117121: ** Return a list of comma separated SQL expressions and a FROM clause that
! 117122: ** could be used in a SELECT statement such as the following:
! 117123: **
! 117124: ** SELECT <list of expressions> FROM %_content AS x ...
! 117125: **
! 117126: ** to return the docid, followed by each column of text data in order
! 117127: ** from left to write. If parameter zFunc is not NULL, then instead of
! 117128: ** being returned directly each column of text data is passed to an SQL
! 117129: ** function named zFunc first. For example, if zFunc is "unzip" and the
! 117130: ** table has the three user-defined columns "a", "b", and "c", the following
! 117131: ** string is returned:
! 117132: **
! 117133: ** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
! 117134: **
! 117135: ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
! 117136: ** is the responsibility of the caller to eventually free it.
! 117137: **
! 117138: ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
! 117139: ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
! 117140: ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
! 117141: ** no error occurs, *pRc is left unmodified.
! 117142: */
! 117143: static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
! 117144: char *zRet = 0;
! 117145: char *zFree = 0;
! 117146: char *zFunction;
! 117147: int i;
! 117148:
! 117149: if( p->zContentTbl==0 ){
! 117150: if( !zFunc ){
! 117151: zFunction = "";
! 117152: }else{
! 117153: zFree = zFunction = fts3QuoteId(zFunc);
! 117154: }
! 117155: fts3Appendf(pRc, &zRet, "docid");
! 117156: for(i=0; i<p->nColumn; i++){
! 117157: fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
! 117158: }
! 117159: sqlite3_free(zFree);
! 117160: }else{
! 117161: fts3Appendf(pRc, &zRet, "rowid");
! 117162: for(i=0; i<p->nColumn; i++){
! 117163: fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
! 117164: }
! 117165: }
! 117166: fts3Appendf(pRc, &zRet, "FROM '%q'.'%q%s' AS x",
! 117167: p->zDb,
! 117168: (p->zContentTbl ? p->zContentTbl : p->zName),
! 117169: (p->zContentTbl ? "" : "_content")
! 117170: );
! 117171: return zRet;
! 117172: }
! 117173:
! 117174: /*
! 117175: ** Return a list of N comma separated question marks, where N is the number
! 117176: ** of columns in the %_content table (one for the docid plus one for each
! 117177: ** user-defined text column).
! 117178: **
! 117179: ** If argument zFunc is not NULL, then all but the first question mark
! 117180: ** is preceded by zFunc and an open bracket, and followed by a closed
! 117181: ** bracket. For example, if zFunc is "zip" and the FTS3 table has three
! 117182: ** user-defined text columns, the following string is returned:
! 117183: **
! 117184: ** "?, zip(?), zip(?), zip(?)"
! 117185: **
! 117186: ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
! 117187: ** is the responsibility of the caller to eventually free it.
! 117188: **
! 117189: ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
! 117190: ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
! 117191: ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
! 117192: ** no error occurs, *pRc is left unmodified.
! 117193: */
! 117194: static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
! 117195: char *zRet = 0;
! 117196: char *zFree = 0;
! 117197: char *zFunction;
! 117198: int i;
! 117199:
! 117200: if( !zFunc ){
! 117201: zFunction = "";
! 117202: }else{
! 117203: zFree = zFunction = fts3QuoteId(zFunc);
! 117204: }
! 117205: fts3Appendf(pRc, &zRet, "?");
! 117206: for(i=0; i<p->nColumn; i++){
! 117207: fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
! 117208: }
! 117209: sqlite3_free(zFree);
! 117210: return zRet;
! 117211: }
! 117212:
! 117213: /*
! 117214: ** This function interprets the string at (*pp) as a non-negative integer
! 117215: ** value. It reads the integer and sets *pnOut to the value read, then
! 117216: ** sets *pp to point to the byte immediately following the last byte of
! 117217: ** the integer value.
! 117218: **
! 117219: ** Only decimal digits ('0'..'9') may be part of an integer value.
! 117220: **
! 117221: ** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
! 117222: ** the output value undefined. Otherwise SQLITE_OK is returned.
! 117223: **
! 117224: ** This function is used when parsing the "prefix=" FTS4 parameter.
! 117225: */
! 117226: static int fts3GobbleInt(const char **pp, int *pnOut){
! 117227: const char *p; /* Iterator pointer */
! 117228: int nInt = 0; /* Output value */
! 117229:
! 117230: for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
! 117231: nInt = nInt * 10 + (p[0] - '0');
! 117232: }
! 117233: if( p==*pp ) return SQLITE_ERROR;
! 117234: *pnOut = nInt;
! 117235: *pp = p;
! 117236: return SQLITE_OK;
! 117237: }
! 117238:
! 117239: /*
! 117240: ** This function is called to allocate an array of Fts3Index structures
! 117241: ** representing the indexes maintained by the current FTS table. FTS tables
! 117242: ** always maintain the main "terms" index, but may also maintain one or
! 117243: ** more "prefix" indexes, depending on the value of the "prefix=" parameter
! 117244: ** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
! 117245: **
! 117246: ** Argument zParam is passed the value of the "prefix=" option if one was
! 117247: ** specified, or NULL otherwise.
! 117248: **
! 117249: ** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
! 117250: ** the allocated array. *pnIndex is set to the number of elements in the
! 117251: ** array. If an error does occur, an SQLite error code is returned.
! 117252: **
! 117253: ** Regardless of whether or not an error is returned, it is the responsibility
! 117254: ** of the caller to call sqlite3_free() on the output array to free it.
! 117255: */
! 117256: static int fts3PrefixParameter(
! 117257: const char *zParam, /* ABC in prefix=ABC parameter to parse */
! 117258: int *pnIndex, /* OUT: size of *apIndex[] array */
! 117259: struct Fts3Index **apIndex /* OUT: Array of indexes for this table */
! 117260: ){
! 117261: struct Fts3Index *aIndex; /* Allocated array */
! 117262: int nIndex = 1; /* Number of entries in array */
! 117263:
! 117264: if( zParam && zParam[0] ){
! 117265: const char *p;
! 117266: nIndex++;
! 117267: for(p=zParam; *p; p++){
! 117268: if( *p==',' ) nIndex++;
! 117269: }
! 117270: }
! 117271:
! 117272: aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
! 117273: *apIndex = aIndex;
! 117274: *pnIndex = nIndex;
! 117275: if( !aIndex ){
! 117276: return SQLITE_NOMEM;
! 117277: }
! 117278:
! 117279: memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
! 117280: if( zParam ){
! 117281: const char *p = zParam;
! 117282: int i;
! 117283: for(i=1; i<nIndex; i++){
! 117284: int nPrefix;
! 117285: if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
! 117286: aIndex[i].nPrefix = nPrefix;
! 117287: p++;
! 117288: }
! 117289: }
! 117290:
! 117291: return SQLITE_OK;
! 117292: }
! 117293:
! 117294: /*
! 117295: ** This function is called when initializing an FTS4 table that uses the
! 117296: ** content=xxx option. It determines the number of and names of the columns
! 117297: ** of the new FTS4 table.
! 117298: **
! 117299: ** The third argument passed to this function is the value passed to the
! 117300: ** config=xxx option (i.e. "xxx"). This function queries the database for
! 117301: ** a table of that name. If found, the output variables are populated
! 117302: ** as follows:
! 117303: **
! 117304: ** *pnCol: Set to the number of columns table xxx has,
! 117305: **
! 117306: ** *pnStr: Set to the total amount of space required to store a copy
! 117307: ** of each columns name, including the nul-terminator.
! 117308: **
! 117309: ** *pazCol: Set to point to an array of *pnCol strings. Each string is
! 117310: ** the name of the corresponding column in table xxx. The array
! 117311: ** and its contents are allocated using a single allocation. It
! 117312: ** is the responsibility of the caller to free this allocation
! 117313: ** by eventually passing the *pazCol value to sqlite3_free().
! 117314: **
! 117315: ** If the table cannot be found, an error code is returned and the output
! 117316: ** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
! 117317: ** returned (and the output variables are undefined).
! 117318: */
! 117319: static int fts3ContentColumns(
! 117320: sqlite3 *db, /* Database handle */
! 117321: const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */
! 117322: const char *zTbl, /* Name of content table */
! 117323: const char ***pazCol, /* OUT: Malloc'd array of column names */
! 117324: int *pnCol, /* OUT: Size of array *pazCol */
! 117325: int *pnStr /* OUT: Bytes of string content */
! 117326: ){
! 117327: int rc = SQLITE_OK; /* Return code */
! 117328: char *zSql; /* "SELECT *" statement on zTbl */
! 117329: sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */
! 117330:
! 117331: zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
! 117332: if( !zSql ){
! 117333: rc = SQLITE_NOMEM;
! 117334: }else{
! 117335: rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
! 117336: }
! 117337: sqlite3_free(zSql);
! 117338:
! 117339: if( rc==SQLITE_OK ){
! 117340: const char **azCol; /* Output array */
! 117341: int nStr = 0; /* Size of all column names (incl. 0x00) */
! 117342: int nCol; /* Number of table columns */
! 117343: int i; /* Used to iterate through columns */
! 117344:
! 117345: /* Loop through the returned columns. Set nStr to the number of bytes of
! 117346: ** space required to store a copy of each column name, including the
! 117347: ** nul-terminator byte. */
! 117348: nCol = sqlite3_column_count(pStmt);
! 117349: for(i=0; i<nCol; i++){
! 117350: const char *zCol = sqlite3_column_name(pStmt, i);
! 117351: nStr += strlen(zCol) + 1;
! 117352: }
! 117353:
! 117354: /* Allocate and populate the array to return. */
! 117355: azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr);
! 117356: if( azCol==0 ){
! 117357: rc = SQLITE_NOMEM;
! 117358: }else{
! 117359: char *p = (char *)&azCol[nCol];
! 117360: for(i=0; i<nCol; i++){
! 117361: const char *zCol = sqlite3_column_name(pStmt, i);
! 117362: int n = strlen(zCol)+1;
! 117363: memcpy(p, zCol, n);
! 117364: azCol[i] = p;
! 117365: p += n;
! 117366: }
! 117367: }
! 117368: sqlite3_finalize(pStmt);
! 117369:
! 117370: /* Set the output variables. */
! 117371: *pnCol = nCol;
! 117372: *pnStr = nStr;
! 117373: *pazCol = azCol;
! 117374: }
! 117375:
! 117376: return rc;
! 117377: }
! 117378:
! 117379: /*
! 117380: ** This function is the implementation of both the xConnect and xCreate
! 117381: ** methods of the FTS3 virtual table.
! 117382: **
! 117383: ** The argv[] array contains the following:
! 117384: **
! 117385: ** argv[0] -> module name ("fts3" or "fts4")
! 117386: ** argv[1] -> database name
! 117387: ** argv[2] -> table name
! 117388: ** argv[...] -> "column name" and other module argument fields.
! 117389: */
! 117390: static int fts3InitVtab(
! 117391: int isCreate, /* True for xCreate, false for xConnect */
! 117392: sqlite3 *db, /* The SQLite database connection */
! 117393: void *pAux, /* Hash table containing tokenizers */
! 117394: int argc, /* Number of elements in argv array */
! 117395: const char * const *argv, /* xCreate/xConnect argument array */
! 117396: sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
! 117397: char **pzErr /* Write any error message here */
! 117398: ){
! 117399: Fts3Hash *pHash = (Fts3Hash *)pAux;
! 117400: Fts3Table *p = 0; /* Pointer to allocated vtab */
! 117401: int rc = SQLITE_OK; /* Return code */
! 117402: int i; /* Iterator variable */
! 117403: int nByte; /* Size of allocation used for *p */
! 117404: int iCol; /* Column index */
! 117405: int nString = 0; /* Bytes required to hold all column names */
! 117406: int nCol = 0; /* Number of columns in the FTS table */
! 117407: char *zCsr; /* Space for holding column names */
! 117408: int nDb; /* Bytes required to hold database name */
! 117409: int nName; /* Bytes required to hold table name */
! 117410: int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
! 117411: const char **aCol; /* Array of column names */
! 117412: sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
! 117413:
! 117414: int nIndex; /* Size of aIndex[] array */
! 117415: struct Fts3Index *aIndex = 0; /* Array of indexes for this table */
! 117416:
! 117417: /* The results of parsing supported FTS4 key=value options: */
! 117418: int bNoDocsize = 0; /* True to omit %_docsize table */
! 117419: int bDescIdx = 0; /* True to store descending indexes */
! 117420: char *zPrefix = 0; /* Prefix parameter value (or NULL) */
! 117421: char *zCompress = 0; /* compress=? parameter (or NULL) */
! 117422: char *zUncompress = 0; /* uncompress=? parameter (or NULL) */
! 117423: char *zContent = 0; /* content=? parameter (or NULL) */
! 117424:
! 117425: assert( strlen(argv[0])==4 );
! 117426: assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
! 117427: || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
! 117428: );
! 117429:
! 117430: nDb = (int)strlen(argv[1]) + 1;
! 117431: nName = (int)strlen(argv[2]) + 1;
! 117432:
! 117433: aCol = (const char **)sqlite3_malloc(sizeof(const char *) * (argc-2) );
! 117434: if( !aCol ) return SQLITE_NOMEM;
! 117435: memset((void *)aCol, 0, sizeof(const char *) * (argc-2));
! 117436:
! 117437: /* Loop through all of the arguments passed by the user to the FTS3/4
! 117438: ** module (i.e. all the column names and special arguments). This loop
! 117439: ** does the following:
! 117440: **
! 117441: ** + Figures out the number of columns the FTSX table will have, and
! 117442: ** the number of bytes of space that must be allocated to store copies
! 117443: ** of the column names.
! 117444: **
! 117445: ** + If there is a tokenizer specification included in the arguments,
! 117446: ** initializes the tokenizer pTokenizer.
! 117447: */
! 117448: for(i=3; rc==SQLITE_OK && i<argc; i++){
! 117449: char const *z = argv[i];
! 117450: int nKey;
! 117451: char *zVal;
! 117452:
! 117453: /* Check if this is a tokenizer specification */
! 117454: if( !pTokenizer
! 117455: && strlen(z)>8
! 117456: && 0==sqlite3_strnicmp(z, "tokenize", 8)
! 117457: && 0==sqlite3Fts3IsIdChar(z[8])
! 117458: ){
! 117459: rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
! 117460: }
! 117461:
! 117462: /* Check if it is an FTS4 special argument. */
! 117463: else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
! 117464: struct Fts4Option {
! 117465: const char *zOpt;
! 117466: int nOpt;
! 117467: } aFts4Opt[] = {
! 117468: { "matchinfo", 9 }, /* 0 -> MATCHINFO */
! 117469: { "prefix", 6 }, /* 1 -> PREFIX */
! 117470: { "compress", 8 }, /* 2 -> COMPRESS */
! 117471: { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
! 117472: { "order", 5 }, /* 4 -> ORDER */
! 117473: { "content", 7 } /* 5 -> CONTENT */
! 117474: };
! 117475:
! 117476: int iOpt;
! 117477: if( !zVal ){
! 117478: rc = SQLITE_NOMEM;
! 117479: }else{
! 117480: for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
! 117481: struct Fts4Option *pOp = &aFts4Opt[iOpt];
! 117482: if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
! 117483: break;
! 117484: }
! 117485: }
! 117486: if( iOpt==SizeofArray(aFts4Opt) ){
! 117487: *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
! 117488: rc = SQLITE_ERROR;
! 117489: }else{
! 117490: switch( iOpt ){
! 117491: case 0: /* MATCHINFO */
! 117492: if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
! 117493: *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
! 117494: rc = SQLITE_ERROR;
! 117495: }
! 117496: bNoDocsize = 1;
! 117497: break;
! 117498:
! 117499: case 1: /* PREFIX */
! 117500: sqlite3_free(zPrefix);
! 117501: zPrefix = zVal;
! 117502: zVal = 0;
! 117503: break;
! 117504:
! 117505: case 2: /* COMPRESS */
! 117506: sqlite3_free(zCompress);
! 117507: zCompress = zVal;
! 117508: zVal = 0;
! 117509: break;
! 117510:
! 117511: case 3: /* UNCOMPRESS */
! 117512: sqlite3_free(zUncompress);
! 117513: zUncompress = zVal;
! 117514: zVal = 0;
! 117515: break;
! 117516:
! 117517: case 4: /* ORDER */
! 117518: if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
! 117519: && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
! 117520: ){
! 117521: *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal);
! 117522: rc = SQLITE_ERROR;
! 117523: }
! 117524: bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
! 117525: break;
! 117526:
! 117527: default: /* CONTENT */
! 117528: assert( iOpt==5 );
! 117529: sqlite3_free(zUncompress);
! 117530: zContent = zVal;
! 117531: zVal = 0;
! 117532: break;
! 117533: }
! 117534: }
! 117535: sqlite3_free(zVal);
! 117536: }
! 117537: }
! 117538:
! 117539: /* Otherwise, the argument is a column name. */
! 117540: else {
! 117541: nString += (int)(strlen(z) + 1);
! 117542: aCol[nCol++] = z;
! 117543: }
! 117544: }
! 117545:
! 117546: /* If a content=xxx option was specified, the following:
! 117547: **
! 117548: ** 1. Ignore any compress= and uncompress= options.
! 117549: **
! 117550: ** 2. If no column names were specified as part of the CREATE VIRTUAL
! 117551: ** TABLE statement, use all columns from the content table.
! 117552: */
! 117553: if( rc==SQLITE_OK && zContent ){
! 117554: sqlite3_free(zCompress);
! 117555: sqlite3_free(zUncompress);
! 117556: zCompress = 0;
! 117557: zUncompress = 0;
! 117558: if( nCol==0 ){
! 117559: sqlite3_free((void*)aCol);
! 117560: aCol = 0;
! 117561: rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString);
! 117562: }
! 117563: assert( rc!=SQLITE_OK || nCol>0 );
! 117564: }
! 117565: if( rc!=SQLITE_OK ) goto fts3_init_out;
! 117566:
! 117567: if( nCol==0 ){
! 117568: assert( nString==0 );
! 117569: aCol[0] = "content";
! 117570: nString = 8;
! 117571: nCol = 1;
! 117572: }
! 117573:
! 117574: if( pTokenizer==0 ){
! 117575: rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
! 117576: if( rc!=SQLITE_OK ) goto fts3_init_out;
! 117577: }
! 117578: assert( pTokenizer );
! 117579:
! 117580: rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
! 117581: if( rc==SQLITE_ERROR ){
! 117582: assert( zPrefix );
! 117583: *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix);
! 117584: }
! 117585: if( rc!=SQLITE_OK ) goto fts3_init_out;
! 117586:
! 117587: /* Allocate and populate the Fts3Table structure. */
! 117588: nByte = sizeof(Fts3Table) + /* Fts3Table */
! 117589: nCol * sizeof(char *) + /* azColumn */
! 117590: nIndex * sizeof(struct Fts3Index) + /* aIndex */
! 117591: nName + /* zName */
! 117592: nDb + /* zDb */
! 117593: nString; /* Space for azColumn strings */
! 117594: p = (Fts3Table*)sqlite3_malloc(nByte);
! 117595: if( p==0 ){
! 117596: rc = SQLITE_NOMEM;
! 117597: goto fts3_init_out;
! 117598: }
! 117599: memset(p, 0, nByte);
! 117600: p->db = db;
! 117601: p->nColumn = nCol;
! 117602: p->nPendingData = 0;
! 117603: p->azColumn = (char **)&p[1];
! 117604: p->pTokenizer = pTokenizer;
! 117605: p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
! 117606: p->bHasDocsize = (isFts4 && bNoDocsize==0);
! 117607: p->bHasStat = isFts4;
! 117608: p->bDescIdx = bDescIdx;
! 117609: p->zContentTbl = zContent;
! 117610: zContent = 0;
! 117611: TESTONLY( p->inTransaction = -1 );
! 117612: TESTONLY( p->mxSavepoint = -1 );
! 117613:
! 117614: p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
! 117615: memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
! 117616: p->nIndex = nIndex;
! 117617: for(i=0; i<nIndex; i++){
! 117618: fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
! 117619: }
! 117620:
! 117621: /* Fill in the zName and zDb fields of the vtab structure. */
! 117622: zCsr = (char *)&p->aIndex[nIndex];
! 117623: p->zName = zCsr;
! 117624: memcpy(zCsr, argv[2], nName);
! 117625: zCsr += nName;
! 117626: p->zDb = zCsr;
! 117627: memcpy(zCsr, argv[1], nDb);
! 117628: zCsr += nDb;
! 117629:
! 117630: /* Fill in the azColumn array */
! 117631: for(iCol=0; iCol<nCol; iCol++){
! 117632: char *z;
! 117633: int n = 0;
! 117634: z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
! 117635: memcpy(zCsr, z, n);
! 117636: zCsr[n] = '\0';
! 117637: sqlite3Fts3Dequote(zCsr);
! 117638: p->azColumn[iCol] = zCsr;
! 117639: zCsr += n+1;
! 117640: assert( zCsr <= &((char *)p)[nByte] );
! 117641: }
! 117642:
! 117643: if( (zCompress==0)!=(zUncompress==0) ){
! 117644: char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
! 117645: rc = SQLITE_ERROR;
! 117646: *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
! 117647: }
! 117648: p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
! 117649: p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
! 117650: if( rc!=SQLITE_OK ) goto fts3_init_out;
! 117651:
! 117652: /* If this is an xCreate call, create the underlying tables in the
! 117653: ** database. TODO: For xConnect(), it could verify that said tables exist.
! 117654: */
! 117655: if( isCreate ){
! 117656: rc = fts3CreateTables(p);
! 117657: }
! 117658:
! 117659: /* Figure out the page-size for the database. This is required in order to
! 117660: ** estimate the cost of loading large doclists from the database. */
! 117661: fts3DatabasePageSize(&rc, p);
! 117662: p->nNodeSize = p->nPgsz-35;
! 117663:
! 117664: /* Declare the table schema to SQLite. */
! 117665: fts3DeclareVtab(&rc, p);
! 117666:
! 117667: fts3_init_out:
! 117668: sqlite3_free(zPrefix);
! 117669: sqlite3_free(aIndex);
! 117670: sqlite3_free(zCompress);
! 117671: sqlite3_free(zUncompress);
! 117672: sqlite3_free(zContent);
! 117673: sqlite3_free((void *)aCol);
! 117674: if( rc!=SQLITE_OK ){
! 117675: if( p ){
! 117676: fts3DisconnectMethod((sqlite3_vtab *)p);
! 117677: }else if( pTokenizer ){
! 117678: pTokenizer->pModule->xDestroy(pTokenizer);
! 117679: }
! 117680: }else{
! 117681: assert( p->pSegments==0 );
! 117682: *ppVTab = &p->base;
! 117683: }
! 117684: return rc;
! 117685: }
! 117686:
! 117687: /*
! 117688: ** The xConnect() and xCreate() methods for the virtual table. All the
! 117689: ** work is done in function fts3InitVtab().
! 117690: */
! 117691: static int fts3ConnectMethod(
! 117692: sqlite3 *db, /* Database connection */
! 117693: void *pAux, /* Pointer to tokenizer hash table */
! 117694: int argc, /* Number of elements in argv array */
! 117695: const char * const *argv, /* xCreate/xConnect argument array */
! 117696: sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
! 117697: char **pzErr /* OUT: sqlite3_malloc'd error message */
! 117698: ){
! 117699: return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
! 117700: }
! 117701: static int fts3CreateMethod(
! 117702: sqlite3 *db, /* Database connection */
! 117703: void *pAux, /* Pointer to tokenizer hash table */
! 117704: int argc, /* Number of elements in argv array */
! 117705: const char * const *argv, /* xCreate/xConnect argument array */
! 117706: sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
! 117707: char **pzErr /* OUT: sqlite3_malloc'd error message */
! 117708: ){
! 117709: return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
! 117710: }
! 117711:
! 117712: /*
! 117713: ** Implementation of the xBestIndex method for FTS3 tables. There
! 117714: ** are three possible strategies, in order of preference:
! 117715: **
! 117716: ** 1. Direct lookup by rowid or docid.
! 117717: ** 2. Full-text search using a MATCH operator on a non-docid column.
! 117718: ** 3. Linear scan of %_content table.
! 117719: */
! 117720: static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
! 117721: Fts3Table *p = (Fts3Table *)pVTab;
! 117722: int i; /* Iterator variable */
! 117723: int iCons = -1; /* Index of constraint to use */
! 117724:
! 117725: /* By default use a full table scan. This is an expensive option,
! 117726: ** so search through the constraints to see if a more efficient
! 117727: ** strategy is possible.
! 117728: */
! 117729: pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
! 117730: pInfo->estimatedCost = 500000;
! 117731: for(i=0; i<pInfo->nConstraint; i++){
! 117732: struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
! 117733: if( pCons->usable==0 ) continue;
! 117734:
! 117735: /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
! 117736: if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
! 117737: && (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1 )
! 117738: ){
! 117739: pInfo->idxNum = FTS3_DOCID_SEARCH;
! 117740: pInfo->estimatedCost = 1.0;
! 117741: iCons = i;
! 117742: }
! 117743:
! 117744: /* A MATCH constraint. Use a full-text search.
! 117745: **
! 117746: ** If there is more than one MATCH constraint available, use the first
! 117747: ** one encountered. If there is both a MATCH constraint and a direct
! 117748: ** rowid/docid lookup, prefer the MATCH strategy. This is done even
! 117749: ** though the rowid/docid lookup is faster than a MATCH query, selecting
! 117750: ** it would lead to an "unable to use function MATCH in the requested
! 117751: ** context" error.
! 117752: */
! 117753: if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
! 117754: && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
! 117755: ){
! 117756: pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
! 117757: pInfo->estimatedCost = 2.0;
! 117758: iCons = i;
! 117759: break;
! 117760: }
! 117761: }
! 117762:
! 117763: if( iCons>=0 ){
! 117764: pInfo->aConstraintUsage[iCons].argvIndex = 1;
! 117765: pInfo->aConstraintUsage[iCons].omit = 1;
! 117766: }
! 117767:
! 117768: /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
! 117769: ** docid) order. Both ascending and descending are possible.
! 117770: */
! 117771: if( pInfo->nOrderBy==1 ){
! 117772: struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
! 117773: if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
! 117774: if( pOrder->desc ){
! 117775: pInfo->idxStr = "DESC";
! 117776: }else{
! 117777: pInfo->idxStr = "ASC";
! 117778: }
! 117779: pInfo->orderByConsumed = 1;
! 117780: }
! 117781: }
! 117782:
! 117783: assert( p->pSegments==0 );
! 117784: return SQLITE_OK;
! 117785: }
! 117786:
! 117787: /*
! 117788: ** Implementation of xOpen method.
! 117789: */
! 117790: static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
! 117791: sqlite3_vtab_cursor *pCsr; /* Allocated cursor */
! 117792:
! 117793: UNUSED_PARAMETER(pVTab);
! 117794:
! 117795: /* Allocate a buffer large enough for an Fts3Cursor structure. If the
! 117796: ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
! 117797: ** if the allocation fails, return SQLITE_NOMEM.
! 117798: */
! 117799: *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
! 117800: if( !pCsr ){
! 117801: return SQLITE_NOMEM;
! 117802: }
! 117803: memset(pCsr, 0, sizeof(Fts3Cursor));
! 117804: return SQLITE_OK;
! 117805: }
! 117806:
! 117807: /*
! 117808: ** Close the cursor. For additional information see the documentation
! 117809: ** on the xClose method of the virtual table interface.
! 117810: */
! 117811: static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
! 117812: Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
! 117813: assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
! 117814: sqlite3_finalize(pCsr->pStmt);
! 117815: sqlite3Fts3ExprFree(pCsr->pExpr);
! 117816: sqlite3Fts3FreeDeferredTokens(pCsr);
! 117817: sqlite3_free(pCsr->aDoclist);
! 117818: sqlite3_free(pCsr->aMatchinfo);
! 117819: assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
! 117820: sqlite3_free(pCsr);
! 117821: return SQLITE_OK;
! 117822: }
! 117823:
! 117824: /*
! 117825: ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
! 117826: ** compose and prepare an SQL statement of the form:
! 117827: **
! 117828: ** "SELECT <columns> FROM %_content WHERE rowid = ?"
! 117829: **
! 117830: ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
! 117831: ** it. If an error occurs, return an SQLite error code.
! 117832: **
! 117833: ** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
! 117834: */
! 117835: static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
! 117836: int rc = SQLITE_OK;
! 117837: if( pCsr->pStmt==0 ){
! 117838: Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
! 117839: char *zSql;
! 117840: zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
! 117841: if( !zSql ) return SQLITE_NOMEM;
! 117842: rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
! 117843: sqlite3_free(zSql);
! 117844: }
! 117845: *ppStmt = pCsr->pStmt;
! 117846: return rc;
! 117847: }
! 117848:
! 117849: /*
! 117850: ** Position the pCsr->pStmt statement so that it is on the row
! 117851: ** of the %_content table that contains the last match. Return
! 117852: ** SQLITE_OK on success.
! 117853: */
! 117854: static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
! 117855: int rc = SQLITE_OK;
! 117856: if( pCsr->isRequireSeek ){
! 117857: sqlite3_stmt *pStmt = 0;
! 117858:
! 117859: rc = fts3CursorSeekStmt(pCsr, &pStmt);
! 117860: if( rc==SQLITE_OK ){
! 117861: sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
! 117862: pCsr->isRequireSeek = 0;
! 117863: if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
! 117864: return SQLITE_OK;
! 117865: }else{
! 117866: rc = sqlite3_reset(pCsr->pStmt);
! 117867: if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
! 117868: /* If no row was found and no error has occured, then the %_content
! 117869: ** table is missing a row that is present in the full-text index.
! 117870: ** The data structures are corrupt. */
! 117871: rc = FTS_CORRUPT_VTAB;
! 117872: pCsr->isEof = 1;
! 117873: }
! 117874: }
! 117875: }
! 117876: }
! 117877:
! 117878: if( rc!=SQLITE_OK && pContext ){
! 117879: sqlite3_result_error_code(pContext, rc);
! 117880: }
! 117881: return rc;
! 117882: }
! 117883:
! 117884: /*
! 117885: ** This function is used to process a single interior node when searching
! 117886: ** a b-tree for a term or term prefix. The node data is passed to this
! 117887: ** function via the zNode/nNode parameters. The term to search for is
! 117888: ** passed in zTerm/nTerm.
! 117889: **
! 117890: ** If piFirst is not NULL, then this function sets *piFirst to the blockid
! 117891: ** of the child node that heads the sub-tree that may contain the term.
! 117892: **
! 117893: ** If piLast is not NULL, then *piLast is set to the right-most child node
! 117894: ** that heads a sub-tree that may contain a term for which zTerm/nTerm is
! 117895: ** a prefix.
! 117896: **
! 117897: ** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
! 117898: */
! 117899: static int fts3ScanInteriorNode(
! 117900: const char *zTerm, /* Term to select leaves for */
! 117901: int nTerm, /* Size of term zTerm in bytes */
! 117902: const char *zNode, /* Buffer containing segment interior node */
! 117903: int nNode, /* Size of buffer at zNode */
! 117904: sqlite3_int64 *piFirst, /* OUT: Selected child node */
! 117905: sqlite3_int64 *piLast /* OUT: Selected child node */
! 117906: ){
! 117907: int rc = SQLITE_OK; /* Return code */
! 117908: const char *zCsr = zNode; /* Cursor to iterate through node */
! 117909: const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
! 117910: char *zBuffer = 0; /* Buffer to load terms into */
! 117911: int nAlloc = 0; /* Size of allocated buffer */
! 117912: int isFirstTerm = 1; /* True when processing first term on page */
! 117913: sqlite3_int64 iChild; /* Block id of child node to descend to */
! 117914:
! 117915: /* Skip over the 'height' varint that occurs at the start of every
! 117916: ** interior node. Then load the blockid of the left-child of the b-tree
! 117917: ** node into variable iChild.
! 117918: **
! 117919: ** Even if the data structure on disk is corrupted, this (reading two
! 117920: ** varints from the buffer) does not risk an overread. If zNode is a
! 117921: ** root node, then the buffer comes from a SELECT statement. SQLite does
! 117922: ** not make this guarantee explicitly, but in practice there are always
! 117923: ** either more than 20 bytes of allocated space following the nNode bytes of
! 117924: ** contents, or two zero bytes. Or, if the node is read from the %_segments
! 117925: ** table, then there are always 20 bytes of zeroed padding following the
! 117926: ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
! 117927: */
! 117928: zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
! 117929: zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
! 117930: if( zCsr>zEnd ){
! 117931: return FTS_CORRUPT_VTAB;
! 117932: }
! 117933:
! 117934: while( zCsr<zEnd && (piFirst || piLast) ){
! 117935: int cmp; /* memcmp() result */
! 117936: int nSuffix; /* Size of term suffix */
! 117937: int nPrefix = 0; /* Size of term prefix */
! 117938: int nBuffer; /* Total term size */
! 117939:
! 117940: /* Load the next term on the node into zBuffer. Use realloc() to expand
! 117941: ** the size of zBuffer if required. */
! 117942: if( !isFirstTerm ){
! 117943: zCsr += sqlite3Fts3GetVarint32(zCsr, &nPrefix);
! 117944: }
! 117945: isFirstTerm = 0;
! 117946: zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix);
! 117947:
! 117948: if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
! 117949: rc = FTS_CORRUPT_VTAB;
! 117950: goto finish_scan;
! 117951: }
! 117952: if( nPrefix+nSuffix>nAlloc ){
! 117953: char *zNew;
! 117954: nAlloc = (nPrefix+nSuffix) * 2;
! 117955: zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
! 117956: if( !zNew ){
! 117957: rc = SQLITE_NOMEM;
! 117958: goto finish_scan;
! 117959: }
! 117960: zBuffer = zNew;
! 117961: }
! 117962: assert( zBuffer );
! 117963: memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
! 117964: nBuffer = nPrefix + nSuffix;
! 117965: zCsr += nSuffix;
! 117966:
! 117967: /* Compare the term we are searching for with the term just loaded from
! 117968: ** the interior node. If the specified term is greater than or equal
! 117969: ** to the term from the interior node, then all terms on the sub-tree
! 117970: ** headed by node iChild are smaller than zTerm. No need to search
! 117971: ** iChild.
! 117972: **
! 117973: ** If the interior node term is larger than the specified term, then
! 117974: ** the tree headed by iChild may contain the specified term.
! 117975: */
! 117976: cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
! 117977: if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
! 117978: *piFirst = iChild;
! 117979: piFirst = 0;
! 117980: }
! 117981:
! 117982: if( piLast && cmp<0 ){
! 117983: *piLast = iChild;
! 117984: piLast = 0;
! 117985: }
! 117986:
! 117987: iChild++;
! 117988: };
! 117989:
! 117990: if( piFirst ) *piFirst = iChild;
! 117991: if( piLast ) *piLast = iChild;
! 117992:
! 117993: finish_scan:
! 117994: sqlite3_free(zBuffer);
! 117995: return rc;
! 117996: }
! 117997:
! 117998:
! 117999: /*
! 118000: ** The buffer pointed to by argument zNode (size nNode bytes) contains an
! 118001: ** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
! 118002: ** contains a term. This function searches the sub-tree headed by the zNode
! 118003: ** node for the range of leaf nodes that may contain the specified term
! 118004: ** or terms for which the specified term is a prefix.
! 118005: **
! 118006: ** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
! 118007: ** left-most leaf node in the tree that may contain the specified term.
! 118008: ** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
! 118009: ** right-most leaf node that may contain a term for which the specified
! 118010: ** term is a prefix.
! 118011: **
! 118012: ** It is possible that the range of returned leaf nodes does not contain
! 118013: ** the specified term or any terms for which it is a prefix. However, if the
! 118014: ** segment does contain any such terms, they are stored within the identified
! 118015: ** range. Because this function only inspects interior segment nodes (and
! 118016: ** never loads leaf nodes into memory), it is not possible to be sure.
! 118017: **
! 118018: ** If an error occurs, an error code other than SQLITE_OK is returned.
! 118019: */
! 118020: static int fts3SelectLeaf(
! 118021: Fts3Table *p, /* Virtual table handle */
! 118022: const char *zTerm, /* Term to select leaves for */
! 118023: int nTerm, /* Size of term zTerm in bytes */
! 118024: const char *zNode, /* Buffer containing segment interior node */
! 118025: int nNode, /* Size of buffer at zNode */
! 118026: sqlite3_int64 *piLeaf, /* Selected leaf node */
! 118027: sqlite3_int64 *piLeaf2 /* Selected leaf node */
! 118028: ){
! 118029: int rc; /* Return code */
! 118030: int iHeight; /* Height of this node in tree */
! 118031:
! 118032: assert( piLeaf || piLeaf2 );
! 118033:
! 118034: sqlite3Fts3GetVarint32(zNode, &iHeight);
! 118035: rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
! 118036: assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
! 118037:
! 118038: if( rc==SQLITE_OK && iHeight>1 ){
! 118039: char *zBlob = 0; /* Blob read from %_segments table */
! 118040: int nBlob; /* Size of zBlob in bytes */
! 118041:
! 118042: if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
! 118043: rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
! 118044: if( rc==SQLITE_OK ){
! 118045: rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
! 118046: }
! 118047: sqlite3_free(zBlob);
! 118048: piLeaf = 0;
! 118049: zBlob = 0;
! 118050: }
! 118051:
! 118052: if( rc==SQLITE_OK ){
! 118053: rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
! 118054: }
! 118055: if( rc==SQLITE_OK ){
! 118056: rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
! 118057: }
! 118058: sqlite3_free(zBlob);
! 118059: }
! 118060:
! 118061: return rc;
! 118062: }
! 118063:
! 118064: /*
! 118065: ** This function is used to create delta-encoded serialized lists of FTS3
! 118066: ** varints. Each call to this function appends a single varint to a list.
! 118067: */
! 118068: static void fts3PutDeltaVarint(
! 118069: char **pp, /* IN/OUT: Output pointer */
! 118070: sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
! 118071: sqlite3_int64 iVal /* Write this value to the list */
! 118072: ){
! 118073: assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
! 118074: *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
! 118075: *piPrev = iVal;
! 118076: }
! 118077:
! 118078: /*
! 118079: ** When this function is called, *ppPoslist is assumed to point to the
! 118080: ** start of a position-list. After it returns, *ppPoslist points to the
! 118081: ** first byte after the position-list.
! 118082: **
! 118083: ** A position list is list of positions (delta encoded) and columns for
! 118084: ** a single document record of a doclist. So, in other words, this
! 118085: ** routine advances *ppPoslist so that it points to the next docid in
! 118086: ** the doclist, or to the first byte past the end of the doclist.
! 118087: **
! 118088: ** If pp is not NULL, then the contents of the position list are copied
! 118089: ** to *pp. *pp is set to point to the first byte past the last byte copied
! 118090: ** before this function returns.
! 118091: */
! 118092: static void fts3PoslistCopy(char **pp, char **ppPoslist){
! 118093: char *pEnd = *ppPoslist;
! 118094: char c = 0;
! 118095:
! 118096: /* The end of a position list is marked by a zero encoded as an FTS3
! 118097: ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
! 118098: ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
! 118099: ** of some other, multi-byte, value.
! 118100: **
! 118101: ** The following while-loop moves pEnd to point to the first byte that is not
! 118102: ** immediately preceded by a byte with the 0x80 bit set. Then increments
! 118103: ** pEnd once more so that it points to the byte immediately following the
! 118104: ** last byte in the position-list.
! 118105: */
! 118106: while( *pEnd | c ){
! 118107: c = *pEnd++ & 0x80;
! 118108: testcase( c!=0 && (*pEnd)==0 );
! 118109: }
! 118110: pEnd++; /* Advance past the POS_END terminator byte */
! 118111:
! 118112: if( pp ){
! 118113: int n = (int)(pEnd - *ppPoslist);
! 118114: char *p = *pp;
! 118115: memcpy(p, *ppPoslist, n);
! 118116: p += n;
! 118117: *pp = p;
! 118118: }
! 118119: *ppPoslist = pEnd;
! 118120: }
! 118121:
! 118122: /*
! 118123: ** When this function is called, *ppPoslist is assumed to point to the
! 118124: ** start of a column-list. After it returns, *ppPoslist points to the
! 118125: ** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
! 118126: **
! 118127: ** A column-list is list of delta-encoded positions for a single column
! 118128: ** within a single document within a doclist.
! 118129: **
! 118130: ** The column-list is terminated either by a POS_COLUMN varint (1) or
! 118131: ** a POS_END varint (0). This routine leaves *ppPoslist pointing to
! 118132: ** the POS_COLUMN or POS_END that terminates the column-list.
! 118133: **
! 118134: ** If pp is not NULL, then the contents of the column-list are copied
! 118135: ** to *pp. *pp is set to point to the first byte past the last byte copied
! 118136: ** before this function returns. The POS_COLUMN or POS_END terminator
! 118137: ** is not copied into *pp.
! 118138: */
! 118139: static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
! 118140: char *pEnd = *ppPoslist;
! 118141: char c = 0;
! 118142:
! 118143: /* A column-list is terminated by either a 0x01 or 0x00 byte that is
! 118144: ** not part of a multi-byte varint.
! 118145: */
! 118146: while( 0xFE & (*pEnd | c) ){
! 118147: c = *pEnd++ & 0x80;
! 118148: testcase( c!=0 && ((*pEnd)&0xfe)==0 );
! 118149: }
! 118150: if( pp ){
! 118151: int n = (int)(pEnd - *ppPoslist);
! 118152: char *p = *pp;
! 118153: memcpy(p, *ppPoslist, n);
! 118154: p += n;
! 118155: *pp = p;
! 118156: }
! 118157: *ppPoslist = pEnd;
! 118158: }
! 118159:
! 118160: /*
! 118161: ** Value used to signify the end of an position-list. This is safe because
! 118162: ** it is not possible to have a document with 2^31 terms.
! 118163: */
! 118164: #define POSITION_LIST_END 0x7fffffff
! 118165:
! 118166: /*
! 118167: ** This function is used to help parse position-lists. When this function is
! 118168: ** called, *pp may point to the start of the next varint in the position-list
! 118169: ** being parsed, or it may point to 1 byte past the end of the position-list
! 118170: ** (in which case **pp will be a terminator bytes POS_END (0) or
! 118171: ** (1)).
! 118172: **
! 118173: ** If *pp points past the end of the current position-list, set *pi to
! 118174: ** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
! 118175: ** increment the current value of *pi by the value read, and set *pp to
! 118176: ** point to the next value before returning.
! 118177: **
! 118178: ** Before calling this routine *pi must be initialized to the value of
! 118179: ** the previous position, or zero if we are reading the first position
! 118180: ** in the position-list. Because positions are delta-encoded, the value
! 118181: ** of the previous position is needed in order to compute the value of
! 118182: ** the next position.
! 118183: */
! 118184: static void fts3ReadNextPos(
! 118185: char **pp, /* IN/OUT: Pointer into position-list buffer */
! 118186: sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
! 118187: ){
! 118188: if( (**pp)&0xFE ){
! 118189: fts3GetDeltaVarint(pp, pi);
! 118190: *pi -= 2;
! 118191: }else{
! 118192: *pi = POSITION_LIST_END;
! 118193: }
! 118194: }
! 118195:
! 118196: /*
! 118197: ** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
! 118198: ** the value of iCol encoded as a varint to *pp. This will start a new
! 118199: ** column list.
! 118200: **
! 118201: ** Set *pp to point to the byte just after the last byte written before
! 118202: ** returning (do not modify it if iCol==0). Return the total number of bytes
! 118203: ** written (0 if iCol==0).
! 118204: */
! 118205: static int fts3PutColNumber(char **pp, int iCol){
! 118206: int n = 0; /* Number of bytes written */
! 118207: if( iCol ){
! 118208: char *p = *pp; /* Output pointer */
! 118209: n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
! 118210: *p = 0x01;
! 118211: *pp = &p[n];
! 118212: }
! 118213: return n;
! 118214: }
! 118215:
! 118216: /*
! 118217: ** Compute the union of two position lists. The output written
! 118218: ** into *pp contains all positions of both *pp1 and *pp2 in sorted
! 118219: ** order and with any duplicates removed. All pointers are
! 118220: ** updated appropriately. The caller is responsible for insuring
! 118221: ** that there is enough space in *pp to hold the complete output.
! 118222: */
! 118223: static void fts3PoslistMerge(
! 118224: char **pp, /* Output buffer */
! 118225: char **pp1, /* Left input list */
! 118226: char **pp2 /* Right input list */
! 118227: ){
! 118228: char *p = *pp;
! 118229: char *p1 = *pp1;
! 118230: char *p2 = *pp2;
! 118231:
! 118232: while( *p1 || *p2 ){
! 118233: int iCol1; /* The current column index in pp1 */
! 118234: int iCol2; /* The current column index in pp2 */
! 118235:
! 118236: if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1);
! 118237: else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
! 118238: else iCol1 = 0;
! 118239:
! 118240: if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2);
! 118241: else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
! 118242: else iCol2 = 0;
! 118243:
! 118244: if( iCol1==iCol2 ){
! 118245: sqlite3_int64 i1 = 0; /* Last position from pp1 */
! 118246: sqlite3_int64 i2 = 0; /* Last position from pp2 */
! 118247: sqlite3_int64 iPrev = 0;
! 118248: int n = fts3PutColNumber(&p, iCol1);
! 118249: p1 += n;
! 118250: p2 += n;
! 118251:
! 118252: /* At this point, both p1 and p2 point to the start of column-lists
! 118253: ** for the same column (the column with index iCol1 and iCol2).
! 118254: ** A column-list is a list of non-negative delta-encoded varints, each
! 118255: ** incremented by 2 before being stored. Each list is terminated by a
! 118256: ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
! 118257: ** and writes the results to buffer p. p is left pointing to the byte
! 118258: ** after the list written. No terminator (POS_END or POS_COLUMN) is
! 118259: ** written to the output.
! 118260: */
! 118261: fts3GetDeltaVarint(&p1, &i1);
! 118262: fts3GetDeltaVarint(&p2, &i2);
! 118263: do {
! 118264: fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
! 118265: iPrev -= 2;
! 118266: if( i1==i2 ){
! 118267: fts3ReadNextPos(&p1, &i1);
! 118268: fts3ReadNextPos(&p2, &i2);
! 118269: }else if( i1<i2 ){
! 118270: fts3ReadNextPos(&p1, &i1);
! 118271: }else{
! 118272: fts3ReadNextPos(&p2, &i2);
! 118273: }
! 118274: }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
! 118275: }else if( iCol1<iCol2 ){
! 118276: p1 += fts3PutColNumber(&p, iCol1);
! 118277: fts3ColumnlistCopy(&p, &p1);
! 118278: }else{
! 118279: p2 += fts3PutColNumber(&p, iCol2);
! 118280: fts3ColumnlistCopy(&p, &p2);
! 118281: }
! 118282: }
! 118283:
! 118284: *p++ = POS_END;
! 118285: *pp = p;
! 118286: *pp1 = p1 + 1;
! 118287: *pp2 = p2 + 1;
! 118288: }
! 118289:
! 118290: /*
! 118291: ** This function is used to merge two position lists into one. When it is
! 118292: ** called, *pp1 and *pp2 must both point to position lists. A position-list is
! 118293: ** the part of a doclist that follows each document id. For example, if a row
! 118294: ** contains:
! 118295: **
! 118296: ** 'a b c'|'x y z'|'a b b a'
! 118297: **
! 118298: ** Then the position list for this row for token 'b' would consist of:
! 118299: **
! 118300: ** 0x02 0x01 0x02 0x03 0x03 0x00
! 118301: **
! 118302: ** When this function returns, both *pp1 and *pp2 are left pointing to the
! 118303: ** byte following the 0x00 terminator of their respective position lists.
! 118304: **
! 118305: ** If isSaveLeft is 0, an entry is added to the output position list for
! 118306: ** each position in *pp2 for which there exists one or more positions in
! 118307: ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
! 118308: ** when the *pp1 token appears before the *pp2 token, but not more than nToken
! 118309: ** slots before it.
! 118310: **
! 118311: ** e.g. nToken==1 searches for adjacent positions.
! 118312: */
! 118313: static int fts3PoslistPhraseMerge(
! 118314: char **pp, /* IN/OUT: Preallocated output buffer */
! 118315: int nToken, /* Maximum difference in token positions */
! 118316: int isSaveLeft, /* Save the left position */
! 118317: int isExact, /* If *pp1 is exactly nTokens before *pp2 */
! 118318: char **pp1, /* IN/OUT: Left input list */
! 118319: char **pp2 /* IN/OUT: Right input list */
! 118320: ){
! 118321: char *p = *pp;
! 118322: char *p1 = *pp1;
! 118323: char *p2 = *pp2;
! 118324: int iCol1 = 0;
! 118325: int iCol2 = 0;
! 118326:
! 118327: /* Never set both isSaveLeft and isExact for the same invocation. */
! 118328: assert( isSaveLeft==0 || isExact==0 );
! 118329:
! 118330: assert( p!=0 && *p1!=0 && *p2!=0 );
! 118331: if( *p1==POS_COLUMN ){
! 118332: p1++;
! 118333: p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
! 118334: }
! 118335: if( *p2==POS_COLUMN ){
! 118336: p2++;
! 118337: p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
! 118338: }
! 118339:
! 118340: while( 1 ){
! 118341: if( iCol1==iCol2 ){
! 118342: char *pSave = p;
! 118343: sqlite3_int64 iPrev = 0;
! 118344: sqlite3_int64 iPos1 = 0;
! 118345: sqlite3_int64 iPos2 = 0;
! 118346:
! 118347: if( iCol1 ){
! 118348: *p++ = POS_COLUMN;
! 118349: p += sqlite3Fts3PutVarint(p, iCol1);
! 118350: }
! 118351:
! 118352: assert( *p1!=POS_END && *p1!=POS_COLUMN );
! 118353: assert( *p2!=POS_END && *p2!=POS_COLUMN );
! 118354: fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
! 118355: fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
! 118356:
! 118357: while( 1 ){
! 118358: if( iPos2==iPos1+nToken
! 118359: || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
! 118360: ){
! 118361: sqlite3_int64 iSave;
! 118362: iSave = isSaveLeft ? iPos1 : iPos2;
! 118363: fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
! 118364: pSave = 0;
! 118365: assert( p );
! 118366: }
! 118367: if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
! 118368: if( (*p2&0xFE)==0 ) break;
! 118369: fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
! 118370: }else{
! 118371: if( (*p1&0xFE)==0 ) break;
! 118372: fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
! 118373: }
! 118374: }
! 118375:
! 118376: if( pSave ){
! 118377: assert( pp && p );
! 118378: p = pSave;
! 118379: }
! 118380:
! 118381: fts3ColumnlistCopy(0, &p1);
! 118382: fts3ColumnlistCopy(0, &p2);
! 118383: assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
! 118384: if( 0==*p1 || 0==*p2 ) break;
! 118385:
! 118386: p1++;
! 118387: p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
! 118388: p2++;
! 118389: p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
! 118390: }
! 118391:
! 118392: /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
! 118393: ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
! 118394: ** end of the position list, or the 0x01 that precedes the next
! 118395: ** column-number in the position list.
! 118396: */
! 118397: else if( iCol1<iCol2 ){
! 118398: fts3ColumnlistCopy(0, &p1);
! 118399: if( 0==*p1 ) break;
! 118400: p1++;
! 118401: p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
! 118402: }else{
! 118403: fts3ColumnlistCopy(0, &p2);
! 118404: if( 0==*p2 ) break;
! 118405: p2++;
! 118406: p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
! 118407: }
! 118408: }
! 118409:
! 118410: fts3PoslistCopy(0, &p2);
! 118411: fts3PoslistCopy(0, &p1);
! 118412: *pp1 = p1;
! 118413: *pp2 = p2;
! 118414: if( *pp==p ){
! 118415: return 0;
! 118416: }
! 118417: *p++ = 0x00;
! 118418: *pp = p;
! 118419: return 1;
! 118420: }
! 118421:
! 118422: /*
! 118423: ** Merge two position-lists as required by the NEAR operator. The argument
! 118424: ** position lists correspond to the left and right phrases of an expression
! 118425: ** like:
! 118426: **
! 118427: ** "phrase 1" NEAR "phrase number 2"
! 118428: **
! 118429: ** Position list *pp1 corresponds to the left-hand side of the NEAR
! 118430: ** expression and *pp2 to the right. As usual, the indexes in the position
! 118431: ** lists are the offsets of the last token in each phrase (tokens "1" and "2"
! 118432: ** in the example above).
! 118433: **
! 118434: ** The output position list - written to *pp - is a copy of *pp2 with those
! 118435: ** entries that are not sufficiently NEAR entries in *pp1 removed.
! 118436: */
! 118437: static int fts3PoslistNearMerge(
! 118438: char **pp, /* Output buffer */
! 118439: char *aTmp, /* Temporary buffer space */
! 118440: int nRight, /* Maximum difference in token positions */
! 118441: int nLeft, /* Maximum difference in token positions */
! 118442: char **pp1, /* IN/OUT: Left input list */
! 118443: char **pp2 /* IN/OUT: Right input list */
! 118444: ){
! 118445: char *p1 = *pp1;
! 118446: char *p2 = *pp2;
! 118447:
! 118448: char *pTmp1 = aTmp;
! 118449: char *pTmp2;
! 118450: char *aTmp2;
! 118451: int res = 1;
! 118452:
! 118453: fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
! 118454: aTmp2 = pTmp2 = pTmp1;
! 118455: *pp1 = p1;
! 118456: *pp2 = p2;
! 118457: fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
! 118458: if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
! 118459: fts3PoslistMerge(pp, &aTmp, &aTmp2);
! 118460: }else if( pTmp1!=aTmp ){
! 118461: fts3PoslistCopy(pp, &aTmp);
! 118462: }else if( pTmp2!=aTmp2 ){
! 118463: fts3PoslistCopy(pp, &aTmp2);
! 118464: }else{
! 118465: res = 0;
! 118466: }
! 118467:
! 118468: return res;
! 118469: }
! 118470:
! 118471: /*
! 118472: ** An instance of this function is used to merge together the (potentially
! 118473: ** large number of) doclists for each term that matches a prefix query.
! 118474: ** See function fts3TermSelectMerge() for details.
! 118475: */
! 118476: typedef struct TermSelect TermSelect;
! 118477: struct TermSelect {
! 118478: char *aaOutput[16]; /* Malloc'd output buffers */
! 118479: int anOutput[16]; /* Size each output buffer in bytes */
! 118480: };
! 118481:
! 118482: /*
! 118483: ** This function is used to read a single varint from a buffer. Parameter
! 118484: ** pEnd points 1 byte past the end of the buffer. When this function is
! 118485: ** called, if *pp points to pEnd or greater, then the end of the buffer
! 118486: ** has been reached. In this case *pp is set to 0 and the function returns.
! 118487: **
! 118488: ** If *pp does not point to or past pEnd, then a single varint is read
! 118489: ** from *pp. *pp is then set to point 1 byte past the end of the read varint.
! 118490: **
! 118491: ** If bDescIdx is false, the value read is added to *pVal before returning.
! 118492: ** If it is true, the value read is subtracted from *pVal before this
! 118493: ** function returns.
! 118494: */
! 118495: static void fts3GetDeltaVarint3(
! 118496: char **pp, /* IN/OUT: Point to read varint from */
! 118497: char *pEnd, /* End of buffer */
! 118498: int bDescIdx, /* True if docids are descending */
! 118499: sqlite3_int64 *pVal /* IN/OUT: Integer value */
! 118500: ){
! 118501: if( *pp>=pEnd ){
! 118502: *pp = 0;
! 118503: }else{
! 118504: sqlite3_int64 iVal;
! 118505: *pp += sqlite3Fts3GetVarint(*pp, &iVal);
! 118506: if( bDescIdx ){
! 118507: *pVal -= iVal;
! 118508: }else{
! 118509: *pVal += iVal;
! 118510: }
! 118511: }
! 118512: }
! 118513:
! 118514: /*
! 118515: ** This function is used to write a single varint to a buffer. The varint
! 118516: ** is written to *pp. Before returning, *pp is set to point 1 byte past the
! 118517: ** end of the value written.
! 118518: **
! 118519: ** If *pbFirst is zero when this function is called, the value written to
! 118520: ** the buffer is that of parameter iVal.
! 118521: **
! 118522: ** If *pbFirst is non-zero when this function is called, then the value
! 118523: ** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
! 118524: ** (if bDescIdx is non-zero).
! 118525: **
! 118526: ** Before returning, this function always sets *pbFirst to 1 and *piPrev
! 118527: ** to the value of parameter iVal.
! 118528: */
! 118529: static void fts3PutDeltaVarint3(
! 118530: char **pp, /* IN/OUT: Output pointer */
! 118531: int bDescIdx, /* True for descending docids */
! 118532: sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
! 118533: int *pbFirst, /* IN/OUT: True after first int written */
! 118534: sqlite3_int64 iVal /* Write this value to the list */
! 118535: ){
! 118536: sqlite3_int64 iWrite;
! 118537: if( bDescIdx==0 || *pbFirst==0 ){
! 118538: iWrite = iVal - *piPrev;
! 118539: }else{
! 118540: iWrite = *piPrev - iVal;
! 118541: }
! 118542: assert( *pbFirst || *piPrev==0 );
! 118543: assert( *pbFirst==0 || iWrite>0 );
! 118544: *pp += sqlite3Fts3PutVarint(*pp, iWrite);
! 118545: *piPrev = iVal;
! 118546: *pbFirst = 1;
! 118547: }
! 118548:
! 118549:
! 118550: /*
! 118551: ** This macro is used by various functions that merge doclists. The two
! 118552: ** arguments are 64-bit docid values. If the value of the stack variable
! 118553: ** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
! 118554: ** Otherwise, (i2-i1).
! 118555: **
! 118556: ** Using this makes it easier to write code that can merge doclists that are
! 118557: ** sorted in either ascending or descending order.
! 118558: */
! 118559: #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
! 118560:
! 118561: /*
! 118562: ** This function does an "OR" merge of two doclists (output contains all
! 118563: ** positions contained in either argument doclist). If the docids in the
! 118564: ** input doclists are sorted in ascending order, parameter bDescDoclist
! 118565: ** should be false. If they are sorted in ascending order, it should be
! 118566: ** passed a non-zero value.
! 118567: **
! 118568: ** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
! 118569: ** containing the output doclist and SQLITE_OK is returned. In this case
! 118570: ** *pnOut is set to the number of bytes in the output doclist.
! 118571: **
! 118572: ** If an error occurs, an SQLite error code is returned. The output values
! 118573: ** are undefined in this case.
! 118574: */
! 118575: static int fts3DoclistOrMerge(
! 118576: int bDescDoclist, /* True if arguments are desc */
! 118577: char *a1, int n1, /* First doclist */
! 118578: char *a2, int n2, /* Second doclist */
! 118579: char **paOut, int *pnOut /* OUT: Malloc'd doclist */
! 118580: ){
! 118581: sqlite3_int64 i1 = 0;
! 118582: sqlite3_int64 i2 = 0;
! 118583: sqlite3_int64 iPrev = 0;
! 118584: char *pEnd1 = &a1[n1];
! 118585: char *pEnd2 = &a2[n2];
! 118586: char *p1 = a1;
! 118587: char *p2 = a2;
! 118588: char *p;
! 118589: char *aOut;
! 118590: int bFirstOut = 0;
! 118591:
! 118592: *paOut = 0;
! 118593: *pnOut = 0;
! 118594:
! 118595: /* Allocate space for the output. Both the input and output doclists
! 118596: ** are delta encoded. If they are in ascending order (bDescDoclist==0),
! 118597: ** then the first docid in each list is simply encoded as a varint. For
! 118598: ** each subsequent docid, the varint stored is the difference between the
! 118599: ** current and previous docid (a positive number - since the list is in
! 118600: ** ascending order).
! 118601: **
! 118602: ** The first docid written to the output is therefore encoded using the
! 118603: ** same number of bytes as it is in whichever of the input lists it is
! 118604: ** read from. And each subsequent docid read from the same input list
! 118605: ** consumes either the same or less bytes as it did in the input (since
! 118606: ** the difference between it and the previous value in the output must
! 118607: ** be a positive value less than or equal to the delta value read from
! 118608: ** the input list). The same argument applies to all but the first docid
! 118609: ** read from the 'other' list. And to the contents of all position lists
! 118610: ** that will be copied and merged from the input to the output.
! 118611: **
! 118612: ** However, if the first docid copied to the output is a negative number,
! 118613: ** then the encoding of the first docid from the 'other' input list may
! 118614: ** be larger in the output than it was in the input (since the delta value
! 118615: ** may be a larger positive integer than the actual docid).
! 118616: **
! 118617: ** The space required to store the output is therefore the sum of the
! 118618: ** sizes of the two inputs, plus enough space for exactly one of the input
! 118619: ** docids to grow.
! 118620: **
! 118621: ** A symetric argument may be made if the doclists are in descending
! 118622: ** order.
! 118623: */
! 118624: aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
! 118625: if( !aOut ) return SQLITE_NOMEM;
! 118626:
! 118627: p = aOut;
! 118628: fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
! 118629: fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
! 118630: while( p1 || p2 ){
! 118631: sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
! 118632:
! 118633: if( p2 && p1 && iDiff==0 ){
! 118634: fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
! 118635: fts3PoslistMerge(&p, &p1, &p2);
! 118636: fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
! 118637: fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
! 118638: }else if( !p2 || (p1 && iDiff<0) ){
! 118639: fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
! 118640: fts3PoslistCopy(&p, &p1);
! 118641: fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
! 118642: }else{
! 118643: fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
! 118644: fts3PoslistCopy(&p, &p2);
! 118645: fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
! 118646: }
! 118647: }
! 118648:
! 118649: *paOut = aOut;
! 118650: *pnOut = (p-aOut);
! 118651: assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
! 118652: return SQLITE_OK;
! 118653: }
! 118654:
! 118655: /*
! 118656: ** This function does a "phrase" merge of two doclists. In a phrase merge,
! 118657: ** the output contains a copy of each position from the right-hand input
! 118658: ** doclist for which there is a position in the left-hand input doclist
! 118659: ** exactly nDist tokens before it.
! 118660: **
! 118661: ** If the docids in the input doclists are sorted in ascending order,
! 118662: ** parameter bDescDoclist should be false. If they are sorted in ascending
! 118663: ** order, it should be passed a non-zero value.
! 118664: **
! 118665: ** The right-hand input doclist is overwritten by this function.
! 118666: */
! 118667: static void fts3DoclistPhraseMerge(
! 118668: int bDescDoclist, /* True if arguments are desc */
! 118669: int nDist, /* Distance from left to right (1=adjacent) */
! 118670: char *aLeft, int nLeft, /* Left doclist */
! 118671: char *aRight, int *pnRight /* IN/OUT: Right/output doclist */
! 118672: ){
! 118673: sqlite3_int64 i1 = 0;
! 118674: sqlite3_int64 i2 = 0;
! 118675: sqlite3_int64 iPrev = 0;
! 118676: char *pEnd1 = &aLeft[nLeft];
! 118677: char *pEnd2 = &aRight[*pnRight];
! 118678: char *p1 = aLeft;
! 118679: char *p2 = aRight;
! 118680: char *p;
! 118681: int bFirstOut = 0;
! 118682: char *aOut = aRight;
! 118683:
! 118684: assert( nDist>0 );
! 118685:
! 118686: p = aOut;
! 118687: fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
! 118688: fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
! 118689:
! 118690: while( p1 && p2 ){
! 118691: sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
! 118692: if( iDiff==0 ){
! 118693: char *pSave = p;
! 118694: sqlite3_int64 iPrevSave = iPrev;
! 118695: int bFirstOutSave = bFirstOut;
! 118696:
! 118697: fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
! 118698: if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
! 118699: p = pSave;
! 118700: iPrev = iPrevSave;
! 118701: bFirstOut = bFirstOutSave;
! 118702: }
! 118703: fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
! 118704: fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
! 118705: }else if( iDiff<0 ){
! 118706: fts3PoslistCopy(0, &p1);
! 118707: fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
! 118708: }else{
! 118709: fts3PoslistCopy(0, &p2);
! 118710: fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
! 118711: }
! 118712: }
! 118713:
! 118714: *pnRight = p - aOut;
! 118715: }
! 118716:
! 118717: /*
! 118718: ** Argument pList points to a position list nList bytes in size. This
! 118719: ** function checks to see if the position list contains any entries for
! 118720: ** a token in position 0 (of any column). If so, it writes argument iDelta
! 118721: ** to the output buffer pOut, followed by a position list consisting only
! 118722: ** of the entries from pList at position 0, and terminated by an 0x00 byte.
! 118723: ** The value returned is the number of bytes written to pOut (if any).
! 118724: */
! 118725: SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
! 118726: sqlite3_int64 iDelta, /* Varint that may be written to pOut */
! 118727: char *pList, /* Position list (no 0x00 term) */
! 118728: int nList, /* Size of pList in bytes */
! 118729: char *pOut /* Write output here */
! 118730: ){
! 118731: int nOut = 0;
! 118732: int bWritten = 0; /* True once iDelta has been written */
! 118733: char *p = pList;
! 118734: char *pEnd = &pList[nList];
! 118735:
! 118736: if( *p!=0x01 ){
! 118737: if( *p==0x02 ){
! 118738: nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
! 118739: pOut[nOut++] = 0x02;
! 118740: bWritten = 1;
! 118741: }
! 118742: fts3ColumnlistCopy(0, &p);
! 118743: }
! 118744:
! 118745: while( p<pEnd && *p==0x01 ){
! 118746: sqlite3_int64 iCol;
! 118747: p++;
! 118748: p += sqlite3Fts3GetVarint(p, &iCol);
! 118749: if( *p==0x02 ){
! 118750: if( bWritten==0 ){
! 118751: nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
! 118752: bWritten = 1;
! 118753: }
! 118754: pOut[nOut++] = 0x01;
! 118755: nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
! 118756: pOut[nOut++] = 0x02;
! 118757: }
! 118758: fts3ColumnlistCopy(0, &p);
! 118759: }
! 118760: if( bWritten ){
! 118761: pOut[nOut++] = 0x00;
! 118762: }
! 118763:
! 118764: return nOut;
! 118765: }
! 118766:
! 118767:
! 118768: /*
! 118769: ** Merge all doclists in the TermSelect.aaOutput[] array into a single
! 118770: ** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
! 118771: ** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
! 118772: **
! 118773: ** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
! 118774: ** the responsibility of the caller to free any doclists left in the
! 118775: ** TermSelect.aaOutput[] array.
! 118776: */
! 118777: static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
! 118778: char *aOut = 0;
! 118779: int nOut = 0;
! 118780: int i;
! 118781:
! 118782: /* Loop through the doclists in the aaOutput[] array. Merge them all
! 118783: ** into a single doclist.
! 118784: */
! 118785: for(i=0; i<SizeofArray(pTS->aaOutput); i++){
! 118786: if( pTS->aaOutput[i] ){
! 118787: if( !aOut ){
! 118788: aOut = pTS->aaOutput[i];
! 118789: nOut = pTS->anOutput[i];
! 118790: pTS->aaOutput[i] = 0;
! 118791: }else{
! 118792: int nNew;
! 118793: char *aNew;
! 118794:
! 118795: int rc = fts3DoclistOrMerge(p->bDescIdx,
! 118796: pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
! 118797: );
! 118798: if( rc!=SQLITE_OK ){
! 118799: sqlite3_free(aOut);
! 118800: return rc;
! 118801: }
! 118802:
! 118803: sqlite3_free(pTS->aaOutput[i]);
! 118804: sqlite3_free(aOut);
! 118805: pTS->aaOutput[i] = 0;
! 118806: aOut = aNew;
! 118807: nOut = nNew;
! 118808: }
! 118809: }
! 118810: }
! 118811:
! 118812: pTS->aaOutput[0] = aOut;
! 118813: pTS->anOutput[0] = nOut;
! 118814: return SQLITE_OK;
! 118815: }
! 118816:
! 118817: /*
! 118818: ** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
! 118819: ** as the first argument. The merge is an "OR" merge (see function
! 118820: ** fts3DoclistOrMerge() for details).
! 118821: **
! 118822: ** This function is called with the doclist for each term that matches
! 118823: ** a queried prefix. It merges all these doclists into one, the doclist
! 118824: ** for the specified prefix. Since there can be a very large number of
! 118825: ** doclists to merge, the merging is done pair-wise using the TermSelect
! 118826: ** object.
! 118827: **
! 118828: ** This function returns SQLITE_OK if the merge is successful, or an
! 118829: ** SQLite error code (SQLITE_NOMEM) if an error occurs.
! 118830: */
! 118831: static int fts3TermSelectMerge(
! 118832: Fts3Table *p, /* FTS table handle */
! 118833: TermSelect *pTS, /* TermSelect object to merge into */
! 118834: char *aDoclist, /* Pointer to doclist */
! 118835: int nDoclist /* Size of aDoclist in bytes */
! 118836: ){
! 118837: if( pTS->aaOutput[0]==0 ){
! 118838: /* If this is the first term selected, copy the doclist to the output
! 118839: ** buffer using memcpy(). */
! 118840: pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
! 118841: pTS->anOutput[0] = nDoclist;
! 118842: if( pTS->aaOutput[0] ){
! 118843: memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
! 118844: }else{
! 118845: return SQLITE_NOMEM;
! 118846: }
! 118847: }else{
! 118848: char *aMerge = aDoclist;
! 118849: int nMerge = nDoclist;
! 118850: int iOut;
! 118851:
! 118852: for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
! 118853: if( pTS->aaOutput[iOut]==0 ){
! 118854: assert( iOut>0 );
! 118855: pTS->aaOutput[iOut] = aMerge;
! 118856: pTS->anOutput[iOut] = nMerge;
! 118857: break;
! 118858: }else{
! 118859: char *aNew;
! 118860: int nNew;
! 118861:
! 118862: int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
! 118863: pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
! 118864: );
! 118865: if( rc!=SQLITE_OK ){
! 118866: if( aMerge!=aDoclist ) sqlite3_free(aMerge);
! 118867: return rc;
! 118868: }
! 118869:
! 118870: if( aMerge!=aDoclist ) sqlite3_free(aMerge);
! 118871: sqlite3_free(pTS->aaOutput[iOut]);
! 118872: pTS->aaOutput[iOut] = 0;
! 118873:
! 118874: aMerge = aNew;
! 118875: nMerge = nNew;
! 118876: if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
! 118877: pTS->aaOutput[iOut] = aMerge;
! 118878: pTS->anOutput[iOut] = nMerge;
! 118879: }
! 118880: }
! 118881: }
! 118882: }
! 118883: return SQLITE_OK;
! 118884: }
! 118885:
! 118886: /*
! 118887: ** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
! 118888: */
! 118889: static int fts3SegReaderCursorAppend(
! 118890: Fts3MultiSegReader *pCsr,
! 118891: Fts3SegReader *pNew
! 118892: ){
! 118893: if( (pCsr->nSegment%16)==0 ){
! 118894: Fts3SegReader **apNew;
! 118895: int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
! 118896: apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
! 118897: if( !apNew ){
! 118898: sqlite3Fts3SegReaderFree(pNew);
! 118899: return SQLITE_NOMEM;
! 118900: }
! 118901: pCsr->apSegment = apNew;
! 118902: }
! 118903: pCsr->apSegment[pCsr->nSegment++] = pNew;
! 118904: return SQLITE_OK;
! 118905: }
! 118906:
! 118907: /*
! 118908: ** Add seg-reader objects to the Fts3MultiSegReader object passed as the
! 118909: ** 8th argument.
! 118910: **
! 118911: ** This function returns SQLITE_OK if successful, or an SQLite error code
! 118912: ** otherwise.
! 118913: */
! 118914: static int fts3SegReaderCursor(
! 118915: Fts3Table *p, /* FTS3 table handle */
! 118916: int iIndex, /* Index to search (from 0 to p->nIndex-1) */
! 118917: int iLevel, /* Level of segments to scan */
! 118918: const char *zTerm, /* Term to query for */
! 118919: int nTerm, /* Size of zTerm in bytes */
! 118920: int isPrefix, /* True for a prefix search */
! 118921: int isScan, /* True to scan from zTerm to EOF */
! 118922: Fts3MultiSegReader *pCsr /* Cursor object to populate */
! 118923: ){
! 118924: int rc = SQLITE_OK; /* Error code */
! 118925: sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */
! 118926: int rc2; /* Result of sqlite3_reset() */
! 118927:
! 118928: /* If iLevel is less than 0 and this is not a scan, include a seg-reader
! 118929: ** for the pending-terms. If this is a scan, then this call must be being
! 118930: ** made by an fts4aux module, not an FTS table. In this case calling
! 118931: ** Fts3SegReaderPending might segfault, as the data structures used by
! 118932: ** fts4aux are not completely populated. So it's easiest to filter these
! 118933: ** calls out here. */
! 118934: if( iLevel<0 && p->aIndex ){
! 118935: Fts3SegReader *pSeg = 0;
! 118936: rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix, &pSeg);
! 118937: if( rc==SQLITE_OK && pSeg ){
! 118938: rc = fts3SegReaderCursorAppend(pCsr, pSeg);
! 118939: }
! 118940: }
! 118941:
! 118942: if( iLevel!=FTS3_SEGCURSOR_PENDING ){
! 118943: if( rc==SQLITE_OK ){
! 118944: rc = sqlite3Fts3AllSegdirs(p, iIndex, iLevel, &pStmt);
! 118945: }
! 118946:
! 118947: while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
! 118948: Fts3SegReader *pSeg = 0;
! 118949:
! 118950: /* Read the values returned by the SELECT into local variables. */
! 118951: sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
! 118952: sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
! 118953: sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
! 118954: int nRoot = sqlite3_column_bytes(pStmt, 4);
! 118955: char const *zRoot = sqlite3_column_blob(pStmt, 4);
! 118956:
! 118957: /* If zTerm is not NULL, and this segment is not stored entirely on its
! 118958: ** root node, the range of leaves scanned can be reduced. Do this. */
! 118959: if( iStartBlock && zTerm ){
! 118960: sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
! 118961: rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
! 118962: if( rc!=SQLITE_OK ) goto finished;
! 118963: if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
! 118964: }
! 118965:
! 118966: rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
! 118967: iStartBlock, iLeavesEndBlock, iEndBlock, zRoot, nRoot, &pSeg
! 118968: );
! 118969: if( rc!=SQLITE_OK ) goto finished;
! 118970: rc = fts3SegReaderCursorAppend(pCsr, pSeg);
! 118971: }
! 118972: }
! 118973:
! 118974: finished:
! 118975: rc2 = sqlite3_reset(pStmt);
! 118976: if( rc==SQLITE_DONE ) rc = rc2;
! 118977:
! 118978: return rc;
! 118979: }
! 118980:
! 118981: /*
! 118982: ** Set up a cursor object for iterating through a full-text index or a
! 118983: ** single level therein.
! 118984: */
! 118985: SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
! 118986: Fts3Table *p, /* FTS3 table handle */
! 118987: int iIndex, /* Index to search (from 0 to p->nIndex-1) */
! 118988: int iLevel, /* Level of segments to scan */
! 118989: const char *zTerm, /* Term to query for */
! 118990: int nTerm, /* Size of zTerm in bytes */
! 118991: int isPrefix, /* True for a prefix search */
! 118992: int isScan, /* True to scan from zTerm to EOF */
! 118993: Fts3MultiSegReader *pCsr /* Cursor object to populate */
! 118994: ){
! 118995: assert( iIndex>=0 && iIndex<p->nIndex );
! 118996: assert( iLevel==FTS3_SEGCURSOR_ALL
! 118997: || iLevel==FTS3_SEGCURSOR_PENDING
! 118998: || iLevel>=0
! 118999: );
! 119000: assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
! 119001: assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
! 119002: assert( isPrefix==0 || isScan==0 );
! 119003:
! 119004: /* "isScan" is only set to true by the ft4aux module, an ordinary
! 119005: ** full-text tables. */
! 119006: assert( isScan==0 || p->aIndex==0 );
! 119007:
! 119008: memset(pCsr, 0, sizeof(Fts3MultiSegReader));
! 119009:
! 119010: return fts3SegReaderCursor(
! 119011: p, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
! 119012: );
! 119013: }
! 119014:
! 119015: /*
! 119016: ** In addition to its current configuration, have the Fts3MultiSegReader
! 119017: ** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
! 119018: **
! 119019: ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
! 119020: */
! 119021: static int fts3SegReaderCursorAddZero(
! 119022: Fts3Table *p, /* FTS virtual table handle */
! 119023: const char *zTerm, /* Term to scan doclist of */
! 119024: int nTerm, /* Number of bytes in zTerm */
! 119025: Fts3MultiSegReader *pCsr /* Fts3MultiSegReader to modify */
! 119026: ){
! 119027: return fts3SegReaderCursor(p, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr);
! 119028: }
! 119029:
! 119030: /*
! 119031: ** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
! 119032: ** if isPrefix is true, to scan the doclist for all terms for which
! 119033: ** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
! 119034: ** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
! 119035: ** an SQLite error code.
! 119036: **
! 119037: ** It is the responsibility of the caller to free this object by eventually
! 119038: ** passing it to fts3SegReaderCursorFree()
! 119039: **
! 119040: ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
! 119041: ** Output parameter *ppSegcsr is set to 0 if an error occurs.
! 119042: */
! 119043: static int fts3TermSegReaderCursor(
! 119044: Fts3Cursor *pCsr, /* Virtual table cursor handle */
! 119045: const char *zTerm, /* Term to query for */
! 119046: int nTerm, /* Size of zTerm in bytes */
! 119047: int isPrefix, /* True for a prefix search */
! 119048: Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
! 119049: ){
! 119050: Fts3MultiSegReader *pSegcsr; /* Object to allocate and return */
! 119051: int rc = SQLITE_NOMEM; /* Return code */
! 119052:
! 119053: pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
! 119054: if( pSegcsr ){
! 119055: int i;
! 119056: int bFound = 0; /* True once an index has been found */
! 119057: Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
! 119058:
! 119059: if( isPrefix ){
! 119060: for(i=1; bFound==0 && i<p->nIndex; i++){
! 119061: if( p->aIndex[i].nPrefix==nTerm ){
! 119062: bFound = 1;
! 119063: rc = sqlite3Fts3SegReaderCursor(
! 119064: p, i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr);
! 119065: pSegcsr->bLookup = 1;
! 119066: }
! 119067: }
! 119068:
! 119069: for(i=1; bFound==0 && i<p->nIndex; i++){
! 119070: if( p->aIndex[i].nPrefix==nTerm+1 ){
! 119071: bFound = 1;
! 119072: rc = sqlite3Fts3SegReaderCursor(
! 119073: p, i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
! 119074: );
! 119075: if( rc==SQLITE_OK ){
! 119076: rc = fts3SegReaderCursorAddZero(p, zTerm, nTerm, pSegcsr);
! 119077: }
! 119078: }
! 119079: }
! 119080: }
! 119081:
! 119082: if( bFound==0 ){
! 119083: rc = sqlite3Fts3SegReaderCursor(
! 119084: p, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
! 119085: );
! 119086: pSegcsr->bLookup = !isPrefix;
! 119087: }
! 119088: }
! 119089:
! 119090: *ppSegcsr = pSegcsr;
! 119091: return rc;
! 119092: }
! 119093:
! 119094: /*
! 119095: ** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
! 119096: */
! 119097: static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
! 119098: sqlite3Fts3SegReaderFinish(pSegcsr);
! 119099: sqlite3_free(pSegcsr);
! 119100: }
! 119101:
! 119102: /*
! 119103: ** This function retreives the doclist for the specified term (or term
! 119104: ** prefix) from the database.
! 119105: */
! 119106: static int fts3TermSelect(
! 119107: Fts3Table *p, /* Virtual table handle */
! 119108: Fts3PhraseToken *pTok, /* Token to query for */
! 119109: int iColumn, /* Column to query (or -ve for all columns) */
! 119110: int *pnOut, /* OUT: Size of buffer at *ppOut */
! 119111: char **ppOut /* OUT: Malloced result buffer */
! 119112: ){
! 119113: int rc; /* Return code */
! 119114: Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
! 119115: TermSelect tsc; /* Object for pair-wise doclist merging */
! 119116: Fts3SegFilter filter; /* Segment term filter configuration */
! 119117:
! 119118: pSegcsr = pTok->pSegcsr;
! 119119: memset(&tsc, 0, sizeof(TermSelect));
! 119120:
! 119121: filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
! 119122: | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
! 119123: | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
! 119124: | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
! 119125: filter.iCol = iColumn;
! 119126: filter.zTerm = pTok->z;
! 119127: filter.nTerm = pTok->n;
! 119128:
! 119129: rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
! 119130: while( SQLITE_OK==rc
! 119131: && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
! 119132: ){
! 119133: rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
! 119134: }
! 119135:
! 119136: if( rc==SQLITE_OK ){
! 119137: rc = fts3TermSelectFinishMerge(p, &tsc);
! 119138: }
! 119139: if( rc==SQLITE_OK ){
! 119140: *ppOut = tsc.aaOutput[0];
! 119141: *pnOut = tsc.anOutput[0];
! 119142: }else{
! 119143: int i;
! 119144: for(i=0; i<SizeofArray(tsc.aaOutput); i++){
! 119145: sqlite3_free(tsc.aaOutput[i]);
! 119146: }
! 119147: }
! 119148:
! 119149: fts3SegReaderCursorFree(pSegcsr);
! 119150: pTok->pSegcsr = 0;
! 119151: return rc;
! 119152: }
! 119153:
! 119154: /*
! 119155: ** This function counts the total number of docids in the doclist stored
! 119156: ** in buffer aList[], size nList bytes.
! 119157: **
! 119158: ** If the isPoslist argument is true, then it is assumed that the doclist
! 119159: ** contains a position-list following each docid. Otherwise, it is assumed
! 119160: ** that the doclist is simply a list of docids stored as delta encoded
! 119161: ** varints.
! 119162: */
! 119163: static int fts3DoclistCountDocids(char *aList, int nList){
! 119164: int nDoc = 0; /* Return value */
! 119165: if( aList ){
! 119166: char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */
! 119167: char *p = aList; /* Cursor */
! 119168: while( p<aEnd ){
! 119169: nDoc++;
! 119170: while( (*p++)&0x80 ); /* Skip docid varint */
! 119171: fts3PoslistCopy(0, &p); /* Skip over position list */
! 119172: }
! 119173: }
! 119174:
! 119175: return nDoc;
! 119176: }
! 119177:
! 119178: /*
! 119179: ** Advance the cursor to the next row in the %_content table that
! 119180: ** matches the search criteria. For a MATCH search, this will be
! 119181: ** the next row that matches. For a full-table scan, this will be
! 119182: ** simply the next row in the %_content table. For a docid lookup,
! 119183: ** this routine simply sets the EOF flag.
! 119184: **
! 119185: ** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
! 119186: ** even if we reach end-of-file. The fts3EofMethod() will be called
! 119187: ** subsequently to determine whether or not an EOF was hit.
! 119188: */
! 119189: static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
! 119190: int rc;
! 119191: Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
! 119192: if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
! 119193: if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
! 119194: pCsr->isEof = 1;
! 119195: rc = sqlite3_reset(pCsr->pStmt);
! 119196: }else{
! 119197: pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
! 119198: rc = SQLITE_OK;
! 119199: }
! 119200: }else{
! 119201: rc = fts3EvalNext((Fts3Cursor *)pCursor);
! 119202: }
! 119203: assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
! 119204: return rc;
! 119205: }
! 119206:
! 119207: /*
! 119208: ** This is the xFilter interface for the virtual table. See
! 119209: ** the virtual table xFilter method documentation for additional
! 119210: ** information.
! 119211: **
! 119212: ** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
! 119213: ** the %_content table.
! 119214: **
! 119215: ** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
! 119216: ** in the %_content table.
! 119217: **
! 119218: ** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The
! 119219: ** column on the left-hand side of the MATCH operator is column
! 119220: ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
! 119221: ** side of the MATCH operator.
! 119222: */
! 119223: static int fts3FilterMethod(
! 119224: sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
! 119225: int idxNum, /* Strategy index */
! 119226: const char *idxStr, /* Unused */
! 119227: int nVal, /* Number of elements in apVal */
! 119228: sqlite3_value **apVal /* Arguments for the indexing scheme */
! 119229: ){
! 119230: int rc;
! 119231: char *zSql; /* SQL statement used to access %_content */
! 119232: Fts3Table *p = (Fts3Table *)pCursor->pVtab;
! 119233: Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
! 119234:
! 119235: UNUSED_PARAMETER(idxStr);
! 119236: UNUSED_PARAMETER(nVal);
! 119237:
! 119238: assert( idxNum>=0 && idxNum<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
! 119239: assert( nVal==0 || nVal==1 );
! 119240: assert( (nVal==0)==(idxNum==FTS3_FULLSCAN_SEARCH) );
! 119241: assert( p->pSegments==0 );
! 119242:
! 119243: /* In case the cursor has been used before, clear it now. */
! 119244: sqlite3_finalize(pCsr->pStmt);
! 119245: sqlite3_free(pCsr->aDoclist);
! 119246: sqlite3Fts3ExprFree(pCsr->pExpr);
! 119247: memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
! 119248:
! 119249: if( idxStr ){
! 119250: pCsr->bDesc = (idxStr[0]=='D');
! 119251: }else{
! 119252: pCsr->bDesc = p->bDescIdx;
! 119253: }
! 119254: pCsr->eSearch = (i16)idxNum;
! 119255:
! 119256: if( idxNum!=FTS3_DOCID_SEARCH && idxNum!=FTS3_FULLSCAN_SEARCH ){
! 119257: int iCol = idxNum-FTS3_FULLTEXT_SEARCH;
! 119258: const char *zQuery = (const char *)sqlite3_value_text(apVal[0]);
! 119259:
! 119260: if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
! 119261: return SQLITE_NOMEM;
! 119262: }
! 119263:
! 119264: rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->bHasStat,
! 119265: p->nColumn, iCol, zQuery, -1, &pCsr->pExpr
! 119266: );
! 119267: if( rc!=SQLITE_OK ){
! 119268: if( rc==SQLITE_ERROR ){
! 119269: static const char *zErr = "malformed MATCH expression: [%s]";
! 119270: p->base.zErrMsg = sqlite3_mprintf(zErr, zQuery);
! 119271: }
! 119272: return rc;
! 119273: }
! 119274:
! 119275: rc = sqlite3Fts3ReadLock(p);
! 119276: if( rc!=SQLITE_OK ) return rc;
! 119277:
! 119278: rc = fts3EvalStart(pCsr);
! 119279:
! 119280: sqlite3Fts3SegmentsClose(p);
! 119281: if( rc!=SQLITE_OK ) return rc;
! 119282: pCsr->pNextId = pCsr->aDoclist;
! 119283: pCsr->iPrevId = 0;
! 119284: }
! 119285:
! 119286: /* Compile a SELECT statement for this cursor. For a full-table-scan, the
! 119287: ** statement loops through all rows of the %_content table. For a
! 119288: ** full-text query or docid lookup, the statement retrieves a single
! 119289: ** row by docid.
! 119290: */
! 119291: if( idxNum==FTS3_FULLSCAN_SEARCH ){
! 119292: zSql = sqlite3_mprintf(
! 119293: "SELECT %s ORDER BY rowid %s",
! 119294: p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
! 119295: );
! 119296: if( zSql ){
! 119297: rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
! 119298: sqlite3_free(zSql);
! 119299: }else{
! 119300: rc = SQLITE_NOMEM;
! 119301: }
! 119302: }else if( idxNum==FTS3_DOCID_SEARCH ){
! 119303: rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
! 119304: if( rc==SQLITE_OK ){
! 119305: rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
! 119306: }
! 119307: }
! 119308: if( rc!=SQLITE_OK ) return rc;
! 119309:
! 119310: return fts3NextMethod(pCursor);
! 119311: }
! 119312:
! 119313: /*
! 119314: ** This is the xEof method of the virtual table. SQLite calls this
! 119315: ** routine to find out if it has reached the end of a result set.
! 119316: */
! 119317: static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
! 119318: return ((Fts3Cursor *)pCursor)->isEof;
! 119319: }
! 119320:
! 119321: /*
! 119322: ** This is the xRowid method. The SQLite core calls this routine to
! 119323: ** retrieve the rowid for the current row of the result set. fts3
! 119324: ** exposes %_content.docid as the rowid for the virtual table. The
! 119325: ** rowid should be written to *pRowid.
! 119326: */
! 119327: static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
! 119328: Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
! 119329: *pRowid = pCsr->iPrevId;
! 119330: return SQLITE_OK;
! 119331: }
! 119332:
! 119333: /*
! 119334: ** This is the xColumn method, called by SQLite to request a value from
! 119335: ** the row that the supplied cursor currently points to.
! 119336: */
! 119337: static int fts3ColumnMethod(
! 119338: sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
! 119339: sqlite3_context *pContext, /* Context for sqlite3_result_xxx() calls */
! 119340: int iCol /* Index of column to read value from */
! 119341: ){
! 119342: int rc = SQLITE_OK; /* Return Code */
! 119343: Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
! 119344: Fts3Table *p = (Fts3Table *)pCursor->pVtab;
! 119345:
! 119346: /* The column value supplied by SQLite must be in range. */
! 119347: assert( iCol>=0 && iCol<=p->nColumn+1 );
! 119348:
! 119349: if( iCol==p->nColumn+1 ){
! 119350: /* This call is a request for the "docid" column. Since "docid" is an
! 119351: ** alias for "rowid", use the xRowid() method to obtain the value.
! 119352: */
! 119353: sqlite3_result_int64(pContext, pCsr->iPrevId);
! 119354: }else if( iCol==p->nColumn ){
! 119355: /* The extra column whose name is the same as the table.
! 119356: ** Return a blob which is a pointer to the cursor.
! 119357: */
! 119358: sqlite3_result_blob(pContext, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
! 119359: }else{
! 119360: rc = fts3CursorSeek(0, pCsr);
! 119361: if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){
! 119362: sqlite3_result_value(pContext, sqlite3_column_value(pCsr->pStmt, iCol+1));
! 119363: }
! 119364: }
! 119365:
! 119366: assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
! 119367: return rc;
! 119368: }
! 119369:
! 119370: /*
! 119371: ** This function is the implementation of the xUpdate callback used by
! 119372: ** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
! 119373: ** inserted, updated or deleted.
! 119374: */
! 119375: static int fts3UpdateMethod(
! 119376: sqlite3_vtab *pVtab, /* Virtual table handle */
! 119377: int nArg, /* Size of argument array */
! 119378: sqlite3_value **apVal, /* Array of arguments */
! 119379: sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
! 119380: ){
! 119381: return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
! 119382: }
! 119383:
! 119384: /*
! 119385: ** Implementation of xSync() method. Flush the contents of the pending-terms
! 119386: ** hash-table to the database.
! 119387: */
! 119388: static int fts3SyncMethod(sqlite3_vtab *pVtab){
! 119389: int rc = sqlite3Fts3PendingTermsFlush((Fts3Table *)pVtab);
! 119390: sqlite3Fts3SegmentsClose((Fts3Table *)pVtab);
! 119391: return rc;
! 119392: }
! 119393:
! 119394: /*
! 119395: ** Implementation of xBegin() method. This is a no-op.
! 119396: */
! 119397: static int fts3BeginMethod(sqlite3_vtab *pVtab){
! 119398: TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
! 119399: UNUSED_PARAMETER(pVtab);
! 119400: assert( p->pSegments==0 );
! 119401: assert( p->nPendingData==0 );
! 119402: assert( p->inTransaction!=1 );
! 119403: TESTONLY( p->inTransaction = 1 );
! 119404: TESTONLY( p->mxSavepoint = -1; );
! 119405: return SQLITE_OK;
! 119406: }
! 119407:
! 119408: /*
! 119409: ** Implementation of xCommit() method. This is a no-op. The contents of
! 119410: ** the pending-terms hash-table have already been flushed into the database
! 119411: ** by fts3SyncMethod().
! 119412: */
! 119413: static int fts3CommitMethod(sqlite3_vtab *pVtab){
! 119414: TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
! 119415: UNUSED_PARAMETER(pVtab);
! 119416: assert( p->nPendingData==0 );
! 119417: assert( p->inTransaction!=0 );
! 119418: assert( p->pSegments==0 );
! 119419: TESTONLY( p->inTransaction = 0 );
! 119420: TESTONLY( p->mxSavepoint = -1; );
! 119421: return SQLITE_OK;
! 119422: }
! 119423:
! 119424: /*
! 119425: ** Implementation of xRollback(). Discard the contents of the pending-terms
! 119426: ** hash-table. Any changes made to the database are reverted by SQLite.
! 119427: */
! 119428: static int fts3RollbackMethod(sqlite3_vtab *pVtab){
! 119429: Fts3Table *p = (Fts3Table*)pVtab;
! 119430: sqlite3Fts3PendingTermsClear(p);
! 119431: assert( p->inTransaction!=0 );
! 119432: TESTONLY( p->inTransaction = 0 );
! 119433: TESTONLY( p->mxSavepoint = -1; );
! 119434: return SQLITE_OK;
! 119435: }
! 119436:
! 119437: /*
! 119438: ** When called, *ppPoslist must point to the byte immediately following the
! 119439: ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
! 119440: ** moves *ppPoslist so that it instead points to the first byte of the
! 119441: ** same position list.
! 119442: */
! 119443: static void fts3ReversePoslist(char *pStart, char **ppPoslist){
! 119444: char *p = &(*ppPoslist)[-2];
! 119445: char c = 0;
! 119446:
! 119447: while( p>pStart && (c=*p--)==0 );
! 119448: while( p>pStart && (*p & 0x80) | c ){
! 119449: c = *p--;
! 119450: }
! 119451: if( p>pStart ){ p = &p[2]; }
! 119452: while( *p++&0x80 );
! 119453: *ppPoslist = p;
! 119454: }
! 119455:
! 119456: /*
! 119457: ** Helper function used by the implementation of the overloaded snippet(),
! 119458: ** offsets() and optimize() SQL functions.
! 119459: **
! 119460: ** If the value passed as the third argument is a blob of size
! 119461: ** sizeof(Fts3Cursor*), then the blob contents are copied to the
! 119462: ** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
! 119463: ** message is written to context pContext and SQLITE_ERROR returned. The
! 119464: ** string passed via zFunc is used as part of the error message.
! 119465: */
! 119466: static int fts3FunctionArg(
! 119467: sqlite3_context *pContext, /* SQL function call context */
! 119468: const char *zFunc, /* Function name */
! 119469: sqlite3_value *pVal, /* argv[0] passed to function */
! 119470: Fts3Cursor **ppCsr /* OUT: Store cursor handle here */
! 119471: ){
! 119472: Fts3Cursor *pRet;
! 119473: if( sqlite3_value_type(pVal)!=SQLITE_BLOB
! 119474: || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
! 119475: ){
! 119476: char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
! 119477: sqlite3_result_error(pContext, zErr, -1);
! 119478: sqlite3_free(zErr);
! 119479: return SQLITE_ERROR;
! 119480: }
! 119481: memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
! 119482: *ppCsr = pRet;
! 119483: return SQLITE_OK;
! 119484: }
! 119485:
! 119486: /*
! 119487: ** Implementation of the snippet() function for FTS3
! 119488: */
! 119489: static void fts3SnippetFunc(
! 119490: sqlite3_context *pContext, /* SQLite function call context */
! 119491: int nVal, /* Size of apVal[] array */
! 119492: sqlite3_value **apVal /* Array of arguments */
! 119493: ){
! 119494: Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
! 119495: const char *zStart = "<b>";
! 119496: const char *zEnd = "</b>";
! 119497: const char *zEllipsis = "<b>...</b>";
! 119498: int iCol = -1;
! 119499: int nToken = 15; /* Default number of tokens in snippet */
! 119500:
! 119501: /* There must be at least one argument passed to this function (otherwise
! 119502: ** the non-overloaded version would have been called instead of this one).
! 119503: */
! 119504: assert( nVal>=1 );
! 119505:
! 119506: if( nVal>6 ){
! 119507: sqlite3_result_error(pContext,
! 119508: "wrong number of arguments to function snippet()", -1);
! 119509: return;
! 119510: }
! 119511: if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
! 119512:
! 119513: switch( nVal ){
! 119514: case 6: nToken = sqlite3_value_int(apVal[5]);
! 119515: case 5: iCol = sqlite3_value_int(apVal[4]);
! 119516: case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
! 119517: case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
! 119518: case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
! 119519: }
! 119520: if( !zEllipsis || !zEnd || !zStart ){
! 119521: sqlite3_result_error_nomem(pContext);
! 119522: }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
! 119523: sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
! 119524: }
! 119525: }
! 119526:
! 119527: /*
! 119528: ** Implementation of the offsets() function for FTS3
! 119529: */
! 119530: static void fts3OffsetsFunc(
! 119531: sqlite3_context *pContext, /* SQLite function call context */
! 119532: int nVal, /* Size of argument array */
! 119533: sqlite3_value **apVal /* Array of arguments */
! 119534: ){
! 119535: Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
! 119536:
! 119537: UNUSED_PARAMETER(nVal);
! 119538:
! 119539: assert( nVal==1 );
! 119540: if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
! 119541: assert( pCsr );
! 119542: if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
! 119543: sqlite3Fts3Offsets(pContext, pCsr);
! 119544: }
! 119545: }
! 119546:
! 119547: /*
! 119548: ** Implementation of the special optimize() function for FTS3. This
! 119549: ** function merges all segments in the database to a single segment.
! 119550: ** Example usage is:
! 119551: **
! 119552: ** SELECT optimize(t) FROM t LIMIT 1;
! 119553: **
! 119554: ** where 't' is the name of an FTS3 table.
! 119555: */
! 119556: static void fts3OptimizeFunc(
! 119557: sqlite3_context *pContext, /* SQLite function call context */
! 119558: int nVal, /* Size of argument array */
! 119559: sqlite3_value **apVal /* Array of arguments */
! 119560: ){
! 119561: int rc; /* Return code */
! 119562: Fts3Table *p; /* Virtual table handle */
! 119563: Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */
! 119564:
! 119565: UNUSED_PARAMETER(nVal);
! 119566:
! 119567: assert( nVal==1 );
! 119568: if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
! 119569: p = (Fts3Table *)pCursor->base.pVtab;
! 119570: assert( p );
! 119571:
! 119572: rc = sqlite3Fts3Optimize(p);
! 119573:
! 119574: switch( rc ){
! 119575: case SQLITE_OK:
! 119576: sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
! 119577: break;
! 119578: case SQLITE_DONE:
! 119579: sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
! 119580: break;
! 119581: default:
! 119582: sqlite3_result_error_code(pContext, rc);
! 119583: break;
! 119584: }
! 119585: }
! 119586:
! 119587: /*
! 119588: ** Implementation of the matchinfo() function for FTS3
! 119589: */
! 119590: static void fts3MatchinfoFunc(
! 119591: sqlite3_context *pContext, /* SQLite function call context */
! 119592: int nVal, /* Size of argument array */
! 119593: sqlite3_value **apVal /* Array of arguments */
! 119594: ){
! 119595: Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
! 119596: assert( nVal==1 || nVal==2 );
! 119597: if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
! 119598: const char *zArg = 0;
! 119599: if( nVal>1 ){
! 119600: zArg = (const char *)sqlite3_value_text(apVal[1]);
! 119601: }
! 119602: sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
! 119603: }
! 119604: }
! 119605:
! 119606: /*
! 119607: ** This routine implements the xFindFunction method for the FTS3
! 119608: ** virtual table.
! 119609: */
! 119610: static int fts3FindFunctionMethod(
! 119611: sqlite3_vtab *pVtab, /* Virtual table handle */
! 119612: int nArg, /* Number of SQL function arguments */
! 119613: const char *zName, /* Name of SQL function */
! 119614: void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
! 119615: void **ppArg /* Unused */
! 119616: ){
! 119617: struct Overloaded {
! 119618: const char *zName;
! 119619: void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
! 119620: } aOverload[] = {
! 119621: { "snippet", fts3SnippetFunc },
! 119622: { "offsets", fts3OffsetsFunc },
! 119623: { "optimize", fts3OptimizeFunc },
! 119624: { "matchinfo", fts3MatchinfoFunc },
! 119625: };
! 119626: int i; /* Iterator variable */
! 119627:
! 119628: UNUSED_PARAMETER(pVtab);
! 119629: UNUSED_PARAMETER(nArg);
! 119630: UNUSED_PARAMETER(ppArg);
! 119631:
! 119632: for(i=0; i<SizeofArray(aOverload); i++){
! 119633: if( strcmp(zName, aOverload[i].zName)==0 ){
! 119634: *pxFunc = aOverload[i].xFunc;
! 119635: return 1;
! 119636: }
! 119637: }
! 119638:
! 119639: /* No function of the specified name was found. Return 0. */
! 119640: return 0;
! 119641: }
! 119642:
! 119643: /*
! 119644: ** Implementation of FTS3 xRename method. Rename an fts3 table.
! 119645: */
! 119646: static int fts3RenameMethod(
! 119647: sqlite3_vtab *pVtab, /* Virtual table handle */
! 119648: const char *zName /* New name of table */
! 119649: ){
! 119650: Fts3Table *p = (Fts3Table *)pVtab;
! 119651: sqlite3 *db = p->db; /* Database connection */
! 119652: int rc; /* Return Code */
! 119653:
! 119654: /* As it happens, the pending terms table is always empty here. This is
! 119655: ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
! 119656: ** always opens a savepoint transaction. And the xSavepoint() method
! 119657: ** flushes the pending terms table. But leave the (no-op) call to
! 119658: ** PendingTermsFlush() in in case that changes.
! 119659: */
! 119660: assert( p->nPendingData==0 );
! 119661: rc = sqlite3Fts3PendingTermsFlush(p);
! 119662:
! 119663: if( p->zContentTbl==0 ){
! 119664: fts3DbExec(&rc, db,
! 119665: "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';",
! 119666: p->zDb, p->zName, zName
! 119667: );
! 119668: }
! 119669:
! 119670: if( p->bHasDocsize ){
! 119671: fts3DbExec(&rc, db,
! 119672: "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';",
! 119673: p->zDb, p->zName, zName
! 119674: );
! 119675: }
! 119676: if( p->bHasStat ){
! 119677: fts3DbExec(&rc, db,
! 119678: "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';",
! 119679: p->zDb, p->zName, zName
! 119680: );
! 119681: }
! 119682: fts3DbExec(&rc, db,
! 119683: "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
! 119684: p->zDb, p->zName, zName
! 119685: );
! 119686: fts3DbExec(&rc, db,
! 119687: "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';",
! 119688: p->zDb, p->zName, zName
! 119689: );
! 119690: return rc;
! 119691: }
! 119692:
! 119693: /*
! 119694: ** The xSavepoint() method.
! 119695: **
! 119696: ** Flush the contents of the pending-terms table to disk.
! 119697: */
! 119698: static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
! 119699: UNUSED_PARAMETER(iSavepoint);
! 119700: assert( ((Fts3Table *)pVtab)->inTransaction );
! 119701: assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
! 119702: TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
! 119703: return fts3SyncMethod(pVtab);
! 119704: }
! 119705:
! 119706: /*
! 119707: ** The xRelease() method.
! 119708: **
! 119709: ** This is a no-op.
! 119710: */
! 119711: static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
! 119712: TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
! 119713: UNUSED_PARAMETER(iSavepoint);
! 119714: UNUSED_PARAMETER(pVtab);
! 119715: assert( p->inTransaction );
! 119716: assert( p->mxSavepoint >= iSavepoint );
! 119717: TESTONLY( p->mxSavepoint = iSavepoint-1 );
! 119718: return SQLITE_OK;
! 119719: }
! 119720:
! 119721: /*
! 119722: ** The xRollbackTo() method.
! 119723: **
! 119724: ** Discard the contents of the pending terms table.
! 119725: */
! 119726: static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
! 119727: Fts3Table *p = (Fts3Table*)pVtab;
! 119728: UNUSED_PARAMETER(iSavepoint);
! 119729: assert( p->inTransaction );
! 119730: assert( p->mxSavepoint >= iSavepoint );
! 119731: TESTONLY( p->mxSavepoint = iSavepoint );
! 119732: sqlite3Fts3PendingTermsClear(p);
! 119733: return SQLITE_OK;
! 119734: }
! 119735:
! 119736: static const sqlite3_module fts3Module = {
! 119737: /* iVersion */ 2,
! 119738: /* xCreate */ fts3CreateMethod,
! 119739: /* xConnect */ fts3ConnectMethod,
! 119740: /* xBestIndex */ fts3BestIndexMethod,
! 119741: /* xDisconnect */ fts3DisconnectMethod,
! 119742: /* xDestroy */ fts3DestroyMethod,
! 119743: /* xOpen */ fts3OpenMethod,
! 119744: /* xClose */ fts3CloseMethod,
! 119745: /* xFilter */ fts3FilterMethod,
! 119746: /* xNext */ fts3NextMethod,
! 119747: /* xEof */ fts3EofMethod,
! 119748: /* xColumn */ fts3ColumnMethod,
! 119749: /* xRowid */ fts3RowidMethod,
! 119750: /* xUpdate */ fts3UpdateMethod,
! 119751: /* xBegin */ fts3BeginMethod,
! 119752: /* xSync */ fts3SyncMethod,
! 119753: /* xCommit */ fts3CommitMethod,
! 119754: /* xRollback */ fts3RollbackMethod,
! 119755: /* xFindFunction */ fts3FindFunctionMethod,
! 119756: /* xRename */ fts3RenameMethod,
! 119757: /* xSavepoint */ fts3SavepointMethod,
! 119758: /* xRelease */ fts3ReleaseMethod,
! 119759: /* xRollbackTo */ fts3RollbackToMethod,
! 119760: };
! 119761:
! 119762: /*
! 119763: ** This function is registered as the module destructor (called when an
! 119764: ** FTS3 enabled database connection is closed). It frees the memory
! 119765: ** allocated for the tokenizer hash table.
! 119766: */
! 119767: static void hashDestroy(void *p){
! 119768: Fts3Hash *pHash = (Fts3Hash *)p;
! 119769: sqlite3Fts3HashClear(pHash);
! 119770: sqlite3_free(pHash);
! 119771: }
! 119772:
! 119773: /*
! 119774: ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
! 119775: ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
! 119776: ** respectively. The following three forward declarations are for functions
! 119777: ** declared in these files used to retrieve the respective implementations.
! 119778: **
! 119779: ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
! 119780: ** to by the argument to point to the "simple" tokenizer implementation.
! 119781: ** And so on.
! 119782: */
! 119783: SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
! 119784: SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
! 119785: #ifdef SQLITE_ENABLE_ICU
! 119786: SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
! 119787: #endif
! 119788:
! 119789: /*
! 119790: ** Initialise the fts3 extension. If this extension is built as part
! 119791: ** of the sqlite library, then this function is called directly by
! 119792: ** SQLite. If fts3 is built as a dynamically loadable extension, this
! 119793: ** function is called by the sqlite3_extension_init() entry point.
! 119794: */
! 119795: SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
! 119796: int rc = SQLITE_OK;
! 119797: Fts3Hash *pHash = 0;
! 119798: const sqlite3_tokenizer_module *pSimple = 0;
! 119799: const sqlite3_tokenizer_module *pPorter = 0;
! 119800:
! 119801: #ifdef SQLITE_ENABLE_ICU
! 119802: const sqlite3_tokenizer_module *pIcu = 0;
! 119803: sqlite3Fts3IcuTokenizerModule(&pIcu);
! 119804: #endif
! 119805:
! 119806: #ifdef SQLITE_TEST
! 119807: rc = sqlite3Fts3InitTerm(db);
! 119808: if( rc!=SQLITE_OK ) return rc;
! 119809: #endif
! 119810:
! 119811: rc = sqlite3Fts3InitAux(db);
! 119812: if( rc!=SQLITE_OK ) return rc;
! 119813:
! 119814: sqlite3Fts3SimpleTokenizerModule(&pSimple);
! 119815: sqlite3Fts3PorterTokenizerModule(&pPorter);
! 119816:
! 119817: /* Allocate and initialise the hash-table used to store tokenizers. */
! 119818: pHash = sqlite3_malloc(sizeof(Fts3Hash));
! 119819: if( !pHash ){
! 119820: rc = SQLITE_NOMEM;
! 119821: }else{
! 119822: sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
! 119823: }
! 119824:
! 119825: /* Load the built-in tokenizers into the hash table */
! 119826: if( rc==SQLITE_OK ){
! 119827: if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
! 119828: || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
! 119829: #ifdef SQLITE_ENABLE_ICU
! 119830: || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
! 119831: #endif
! 119832: ){
! 119833: rc = SQLITE_NOMEM;
! 119834: }
! 119835: }
! 119836:
! 119837: #ifdef SQLITE_TEST
! 119838: if( rc==SQLITE_OK ){
! 119839: rc = sqlite3Fts3ExprInitTestInterface(db);
! 119840: }
! 119841: #endif
! 119842:
! 119843: /* Create the virtual table wrapper around the hash-table and overload
! 119844: ** the two scalar functions. If this is successful, register the
! 119845: ** module with sqlite.
! 119846: */
! 119847: if( SQLITE_OK==rc
! 119848: && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
! 119849: && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
! 119850: && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
! 119851: && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
! 119852: && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
! 119853: && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
! 119854: ){
! 119855: rc = sqlite3_create_module_v2(
! 119856: db, "fts3", &fts3Module, (void *)pHash, hashDestroy
! 119857: );
! 119858: if( rc==SQLITE_OK ){
! 119859: rc = sqlite3_create_module_v2(
! 119860: db, "fts4", &fts3Module, (void *)pHash, 0
! 119861: );
! 119862: }
! 119863: return rc;
! 119864: }
! 119865:
! 119866: /* An error has occurred. Delete the hash table and return the error code. */
! 119867: assert( rc!=SQLITE_OK );
! 119868: if( pHash ){
! 119869: sqlite3Fts3HashClear(pHash);
! 119870: sqlite3_free(pHash);
! 119871: }
! 119872: return rc;
! 119873: }
! 119874:
! 119875: /*
! 119876: ** Allocate an Fts3MultiSegReader for each token in the expression headed
! 119877: ** by pExpr.
! 119878: **
! 119879: ** An Fts3SegReader object is a cursor that can seek or scan a range of
! 119880: ** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
! 119881: ** Fts3SegReader objects internally to provide an interface to seek or scan
! 119882: ** within the union of all segments of a b-tree. Hence the name.
! 119883: **
! 119884: ** If the allocated Fts3MultiSegReader just seeks to a single entry in a
! 119885: ** segment b-tree (if the term is not a prefix or it is a prefix for which
! 119886: ** there exists prefix b-tree of the right length) then it may be traversed
! 119887: ** and merged incrementally. Otherwise, it has to be merged into an in-memory
! 119888: ** doclist and then traversed.
! 119889: */
! 119890: static void fts3EvalAllocateReaders(
! 119891: Fts3Cursor *pCsr, /* FTS cursor handle */
! 119892: Fts3Expr *pExpr, /* Allocate readers for this expression */
! 119893: int *pnToken, /* OUT: Total number of tokens in phrase. */
! 119894: int *pnOr, /* OUT: Total number of OR nodes in expr. */
! 119895: int *pRc /* IN/OUT: Error code */
! 119896: ){
! 119897: if( pExpr && SQLITE_OK==*pRc ){
! 119898: if( pExpr->eType==FTSQUERY_PHRASE ){
! 119899: int i;
! 119900: int nToken = pExpr->pPhrase->nToken;
! 119901: *pnToken += nToken;
! 119902: for(i=0; i<nToken; i++){
! 119903: Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
! 119904: int rc = fts3TermSegReaderCursor(pCsr,
! 119905: pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
! 119906: );
! 119907: if( rc!=SQLITE_OK ){
! 119908: *pRc = rc;
! 119909: return;
! 119910: }
! 119911: }
! 119912: assert( pExpr->pPhrase->iDoclistToken==0 );
! 119913: pExpr->pPhrase->iDoclistToken = -1;
! 119914: }else{
! 119915: *pnOr += (pExpr->eType==FTSQUERY_OR);
! 119916: fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
! 119917: fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
! 119918: }
! 119919: }
! 119920: }
! 119921:
! 119922: /*
! 119923: ** Arguments pList/nList contain the doclist for token iToken of phrase p.
! 119924: ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
! 119925: **
! 119926: ** This function assumes that pList points to a buffer allocated using
! 119927: ** sqlite3_malloc(). This function takes responsibility for eventually
! 119928: ** freeing the buffer.
! 119929: */
! 119930: static void fts3EvalPhraseMergeToken(
! 119931: Fts3Table *pTab, /* FTS Table pointer */
! 119932: Fts3Phrase *p, /* Phrase to merge pList/nList into */
! 119933: int iToken, /* Token pList/nList corresponds to */
! 119934: char *pList, /* Pointer to doclist */
! 119935: int nList /* Number of bytes in pList */
! 119936: ){
! 119937: assert( iToken!=p->iDoclistToken );
! 119938:
! 119939: if( pList==0 ){
! 119940: sqlite3_free(p->doclist.aAll);
! 119941: p->doclist.aAll = 0;
! 119942: p->doclist.nAll = 0;
! 119943: }
! 119944:
! 119945: else if( p->iDoclistToken<0 ){
! 119946: p->doclist.aAll = pList;
! 119947: p->doclist.nAll = nList;
! 119948: }
! 119949:
! 119950: else if( p->doclist.aAll==0 ){
! 119951: sqlite3_free(pList);
! 119952: }
! 119953:
! 119954: else {
! 119955: char *pLeft;
! 119956: char *pRight;
! 119957: int nLeft;
! 119958: int nRight;
! 119959: int nDiff;
! 119960:
! 119961: if( p->iDoclistToken<iToken ){
! 119962: pLeft = p->doclist.aAll;
! 119963: nLeft = p->doclist.nAll;
! 119964: pRight = pList;
! 119965: nRight = nList;
! 119966: nDiff = iToken - p->iDoclistToken;
! 119967: }else{
! 119968: pRight = p->doclist.aAll;
! 119969: nRight = p->doclist.nAll;
! 119970: pLeft = pList;
! 119971: nLeft = nList;
! 119972: nDiff = p->iDoclistToken - iToken;
! 119973: }
! 119974:
! 119975: fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
! 119976: sqlite3_free(pLeft);
! 119977: p->doclist.aAll = pRight;
! 119978: p->doclist.nAll = nRight;
! 119979: }
! 119980:
! 119981: if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
! 119982: }
! 119983:
! 119984: /*
! 119985: ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
! 119986: ** does not take deferred tokens into account.
! 119987: **
! 119988: ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
! 119989: */
! 119990: static int fts3EvalPhraseLoad(
! 119991: Fts3Cursor *pCsr, /* FTS Cursor handle */
! 119992: Fts3Phrase *p /* Phrase object */
! 119993: ){
! 119994: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 119995: int iToken;
! 119996: int rc = SQLITE_OK;
! 119997:
! 119998: for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
! 119999: Fts3PhraseToken *pToken = &p->aToken[iToken];
! 120000: assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
! 120001:
! 120002: if( pToken->pSegcsr ){
! 120003: int nThis = 0;
! 120004: char *pThis = 0;
! 120005: rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
! 120006: if( rc==SQLITE_OK ){
! 120007: fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
! 120008: }
! 120009: }
! 120010: assert( pToken->pSegcsr==0 );
! 120011: }
! 120012:
! 120013: return rc;
! 120014: }
! 120015:
! 120016: /*
! 120017: ** This function is called on each phrase after the position lists for
! 120018: ** any deferred tokens have been loaded into memory. It updates the phrases
! 120019: ** current position list to include only those positions that are really
! 120020: ** instances of the phrase (after considering deferred tokens). If this
! 120021: ** means that the phrase does not appear in the current row, doclist.pList
! 120022: ** and doclist.nList are both zeroed.
! 120023: **
! 120024: ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
! 120025: */
! 120026: static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
! 120027: int iToken; /* Used to iterate through phrase tokens */
! 120028: char *aPoslist = 0; /* Position list for deferred tokens */
! 120029: int nPoslist = 0; /* Number of bytes in aPoslist */
! 120030: int iPrev = -1; /* Token number of previous deferred token */
! 120031:
! 120032: assert( pPhrase->doclist.bFreeList==0 );
! 120033:
! 120034: for(iToken=0; iToken<pPhrase->nToken; iToken++){
! 120035: Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
! 120036: Fts3DeferredToken *pDeferred = pToken->pDeferred;
! 120037:
! 120038: if( pDeferred ){
! 120039: char *pList;
! 120040: int nList;
! 120041: int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
! 120042: if( rc!=SQLITE_OK ) return rc;
! 120043:
! 120044: if( pList==0 ){
! 120045: sqlite3_free(aPoslist);
! 120046: pPhrase->doclist.pList = 0;
! 120047: pPhrase->doclist.nList = 0;
! 120048: return SQLITE_OK;
! 120049:
! 120050: }else if( aPoslist==0 ){
! 120051: aPoslist = pList;
! 120052: nPoslist = nList;
! 120053:
! 120054: }else{
! 120055: char *aOut = pList;
! 120056: char *p1 = aPoslist;
! 120057: char *p2 = aOut;
! 120058:
! 120059: assert( iPrev>=0 );
! 120060: fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
! 120061: sqlite3_free(aPoslist);
! 120062: aPoslist = pList;
! 120063: nPoslist = aOut - aPoslist;
! 120064: if( nPoslist==0 ){
! 120065: sqlite3_free(aPoslist);
! 120066: pPhrase->doclist.pList = 0;
! 120067: pPhrase->doclist.nList = 0;
! 120068: return SQLITE_OK;
! 120069: }
! 120070: }
! 120071: iPrev = iToken;
! 120072: }
! 120073: }
! 120074:
! 120075: if( iPrev>=0 ){
! 120076: int nMaxUndeferred = pPhrase->iDoclistToken;
! 120077: if( nMaxUndeferred<0 ){
! 120078: pPhrase->doclist.pList = aPoslist;
! 120079: pPhrase->doclist.nList = nPoslist;
! 120080: pPhrase->doclist.iDocid = pCsr->iPrevId;
! 120081: pPhrase->doclist.bFreeList = 1;
! 120082: }else{
! 120083: int nDistance;
! 120084: char *p1;
! 120085: char *p2;
! 120086: char *aOut;
! 120087:
! 120088: if( nMaxUndeferred>iPrev ){
! 120089: p1 = aPoslist;
! 120090: p2 = pPhrase->doclist.pList;
! 120091: nDistance = nMaxUndeferred - iPrev;
! 120092: }else{
! 120093: p1 = pPhrase->doclist.pList;
! 120094: p2 = aPoslist;
! 120095: nDistance = iPrev - nMaxUndeferred;
! 120096: }
! 120097:
! 120098: aOut = (char *)sqlite3_malloc(nPoslist+8);
! 120099: if( !aOut ){
! 120100: sqlite3_free(aPoslist);
! 120101: return SQLITE_NOMEM;
! 120102: }
! 120103:
! 120104: pPhrase->doclist.pList = aOut;
! 120105: if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
! 120106: pPhrase->doclist.bFreeList = 1;
! 120107: pPhrase->doclist.nList = (aOut - pPhrase->doclist.pList);
! 120108: }else{
! 120109: sqlite3_free(aOut);
! 120110: pPhrase->doclist.pList = 0;
! 120111: pPhrase->doclist.nList = 0;
! 120112: }
! 120113: sqlite3_free(aPoslist);
! 120114: }
! 120115: }
! 120116:
! 120117: return SQLITE_OK;
! 120118: }
! 120119:
! 120120: /*
! 120121: ** This function is called for each Fts3Phrase in a full-text query
! 120122: ** expression to initialize the mechanism for returning rows. Once this
! 120123: ** function has been called successfully on an Fts3Phrase, it may be
! 120124: ** used with fts3EvalPhraseNext() to iterate through the matching docids.
! 120125: **
! 120126: ** If parameter bOptOk is true, then the phrase may (or may not) use the
! 120127: ** incremental loading strategy. Otherwise, the entire doclist is loaded into
! 120128: ** memory within this call.
! 120129: **
! 120130: ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
! 120131: */
! 120132: static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
! 120133: int rc; /* Error code */
! 120134: Fts3PhraseToken *pFirst = &p->aToken[0];
! 120135: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 120136:
! 120137: if( pCsr->bDesc==pTab->bDescIdx
! 120138: && bOptOk==1
! 120139: && p->nToken==1
! 120140: && pFirst->pSegcsr
! 120141: && pFirst->pSegcsr->bLookup
! 120142: && pFirst->bFirst==0
! 120143: ){
! 120144: /* Use the incremental approach. */
! 120145: int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
! 120146: rc = sqlite3Fts3MsrIncrStart(
! 120147: pTab, pFirst->pSegcsr, iCol, pFirst->z, pFirst->n);
! 120148: p->bIncr = 1;
! 120149:
! 120150: }else{
! 120151: /* Load the full doclist for the phrase into memory. */
! 120152: rc = fts3EvalPhraseLoad(pCsr, p);
! 120153: p->bIncr = 0;
! 120154: }
! 120155:
! 120156: assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
! 120157: return rc;
! 120158: }
! 120159:
! 120160: /*
! 120161: ** This function is used to iterate backwards (from the end to start)
! 120162: ** through doclists. It is used by this module to iterate through phrase
! 120163: ** doclists in reverse and by the fts3_write.c module to iterate through
! 120164: ** pending-terms lists when writing to databases with "order=desc".
! 120165: **
! 120166: ** The doclist may be sorted in ascending (parameter bDescIdx==0) or
! 120167: ** descending (parameter bDescIdx==1) order of docid. Regardless, this
! 120168: ** function iterates from the end of the doclist to the beginning.
! 120169: */
! 120170: SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
! 120171: int bDescIdx, /* True if the doclist is desc */
! 120172: char *aDoclist, /* Pointer to entire doclist */
! 120173: int nDoclist, /* Length of aDoclist in bytes */
! 120174: char **ppIter, /* IN/OUT: Iterator pointer */
! 120175: sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
! 120176: int *pnList, /* IN/OUT: List length pointer */
! 120177: u8 *pbEof /* OUT: End-of-file flag */
! 120178: ){
! 120179: char *p = *ppIter;
! 120180:
! 120181: assert( nDoclist>0 );
! 120182: assert( *pbEof==0 );
! 120183: assert( p || *piDocid==0 );
! 120184: assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
! 120185:
! 120186: if( p==0 ){
! 120187: sqlite3_int64 iDocid = 0;
! 120188: char *pNext = 0;
! 120189: char *pDocid = aDoclist;
! 120190: char *pEnd = &aDoclist[nDoclist];
! 120191: int iMul = 1;
! 120192:
! 120193: while( pDocid<pEnd ){
! 120194: sqlite3_int64 iDelta;
! 120195: pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
! 120196: iDocid += (iMul * iDelta);
! 120197: pNext = pDocid;
! 120198: fts3PoslistCopy(0, &pDocid);
! 120199: while( pDocid<pEnd && *pDocid==0 ) pDocid++;
! 120200: iMul = (bDescIdx ? -1 : 1);
! 120201: }
! 120202:
! 120203: *pnList = pEnd - pNext;
! 120204: *ppIter = pNext;
! 120205: *piDocid = iDocid;
! 120206: }else{
! 120207: int iMul = (bDescIdx ? -1 : 1);
! 120208: sqlite3_int64 iDelta;
! 120209: fts3GetReverseVarint(&p, aDoclist, &iDelta);
! 120210: *piDocid -= (iMul * iDelta);
! 120211:
! 120212: if( p==aDoclist ){
! 120213: *pbEof = 1;
! 120214: }else{
! 120215: char *pSave = p;
! 120216: fts3ReversePoslist(aDoclist, &p);
! 120217: *pnList = (pSave - p);
! 120218: }
! 120219: *ppIter = p;
! 120220: }
! 120221: }
! 120222:
! 120223: /*
! 120224: ** Attempt to move the phrase iterator to point to the next matching docid.
! 120225: ** If an error occurs, return an SQLite error code. Otherwise, return
! 120226: ** SQLITE_OK.
! 120227: **
! 120228: ** If there is no "next" entry and no error occurs, then *pbEof is set to
! 120229: ** 1 before returning. Otherwise, if no error occurs and the iterator is
! 120230: ** successfully advanced, *pbEof is set to 0.
! 120231: */
! 120232: static int fts3EvalPhraseNext(
! 120233: Fts3Cursor *pCsr, /* FTS Cursor handle */
! 120234: Fts3Phrase *p, /* Phrase object to advance to next docid */
! 120235: u8 *pbEof /* OUT: Set to 1 if EOF */
! 120236: ){
! 120237: int rc = SQLITE_OK;
! 120238: Fts3Doclist *pDL = &p->doclist;
! 120239: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 120240:
! 120241: if( p->bIncr ){
! 120242: assert( p->nToken==1 );
! 120243: assert( pDL->pNextDocid==0 );
! 120244: rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
! 120245: &pDL->iDocid, &pDL->pList, &pDL->nList
! 120246: );
! 120247: if( rc==SQLITE_OK && !pDL->pList ){
! 120248: *pbEof = 1;
! 120249: }
! 120250: }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
! 120251: sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
! 120252: &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
! 120253: );
! 120254: pDL->pList = pDL->pNextDocid;
! 120255: }else{
! 120256: char *pIter; /* Used to iterate through aAll */
! 120257: char *pEnd = &pDL->aAll[pDL->nAll]; /* 1 byte past end of aAll */
! 120258: if( pDL->pNextDocid ){
! 120259: pIter = pDL->pNextDocid;
! 120260: }else{
! 120261: pIter = pDL->aAll;
! 120262: }
! 120263:
! 120264: if( pIter>=pEnd ){
! 120265: /* We have already reached the end of this doclist. EOF. */
! 120266: *pbEof = 1;
! 120267: }else{
! 120268: sqlite3_int64 iDelta;
! 120269: pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
! 120270: if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
! 120271: pDL->iDocid += iDelta;
! 120272: }else{
! 120273: pDL->iDocid -= iDelta;
! 120274: }
! 120275: pDL->pList = pIter;
! 120276: fts3PoslistCopy(0, &pIter);
! 120277: pDL->nList = (pIter - pDL->pList);
! 120278:
! 120279: /* pIter now points just past the 0x00 that terminates the position-
! 120280: ** list for document pDL->iDocid. However, if this position-list was
! 120281: ** edited in place by fts3EvalNearTrim(), then pIter may not actually
! 120282: ** point to the start of the next docid value. The following line deals
! 120283: ** with this case by advancing pIter past the zero-padding added by
! 120284: ** fts3EvalNearTrim(). */
! 120285: while( pIter<pEnd && *pIter==0 ) pIter++;
! 120286:
! 120287: pDL->pNextDocid = pIter;
! 120288: assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
! 120289: *pbEof = 0;
! 120290: }
! 120291: }
! 120292:
! 120293: return rc;
! 120294: }
! 120295:
! 120296: /*
! 120297: **
! 120298: ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
! 120299: ** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
! 120300: ** expression. Also the Fts3Expr.bDeferred variable is set to true for any
! 120301: ** expressions for which all descendent tokens are deferred.
! 120302: **
! 120303: ** If parameter bOptOk is zero, then it is guaranteed that the
! 120304: ** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
! 120305: ** each phrase in the expression (subject to deferred token processing).
! 120306: ** Or, if bOptOk is non-zero, then one or more tokens within the expression
! 120307: ** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
! 120308: **
! 120309: ** If an error occurs within this function, *pRc is set to an SQLite error
! 120310: ** code before returning.
! 120311: */
! 120312: static void fts3EvalStartReaders(
! 120313: Fts3Cursor *pCsr, /* FTS Cursor handle */
! 120314: Fts3Expr *pExpr, /* Expression to initialize phrases in */
! 120315: int bOptOk, /* True to enable incremental loading */
! 120316: int *pRc /* IN/OUT: Error code */
! 120317: ){
! 120318: if( pExpr && SQLITE_OK==*pRc ){
! 120319: if( pExpr->eType==FTSQUERY_PHRASE ){
! 120320: int i;
! 120321: int nToken = pExpr->pPhrase->nToken;
! 120322: for(i=0; i<nToken; i++){
! 120323: if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
! 120324: }
! 120325: pExpr->bDeferred = (i==nToken);
! 120326: *pRc = fts3EvalPhraseStart(pCsr, bOptOk, pExpr->pPhrase);
! 120327: }else{
! 120328: fts3EvalStartReaders(pCsr, pExpr->pLeft, bOptOk, pRc);
! 120329: fts3EvalStartReaders(pCsr, pExpr->pRight, bOptOk, pRc);
! 120330: pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
! 120331: }
! 120332: }
! 120333: }
! 120334:
! 120335: /*
! 120336: ** An array of the following structures is assembled as part of the process
! 120337: ** of selecting tokens to defer before the query starts executing (as part
! 120338: ** of the xFilter() method). There is one element in the array for each
! 120339: ** token in the FTS expression.
! 120340: **
! 120341: ** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
! 120342: ** to phrases that are connected only by AND and NEAR operators (not OR or
! 120343: ** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
! 120344: ** separately. The root of a tokens AND/NEAR cluster is stored in
! 120345: ** Fts3TokenAndCost.pRoot.
! 120346: */
! 120347: typedef struct Fts3TokenAndCost Fts3TokenAndCost;
! 120348: struct Fts3TokenAndCost {
! 120349: Fts3Phrase *pPhrase; /* The phrase the token belongs to */
! 120350: int iToken; /* Position of token in phrase */
! 120351: Fts3PhraseToken *pToken; /* The token itself */
! 120352: Fts3Expr *pRoot; /* Root of NEAR/AND cluster */
! 120353: int nOvfl; /* Number of overflow pages to load doclist */
! 120354: int iCol; /* The column the token must match */
! 120355: };
! 120356:
! 120357: /*
! 120358: ** This function is used to populate an allocated Fts3TokenAndCost array.
! 120359: **
! 120360: ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
! 120361: ** Otherwise, if an error occurs during execution, *pRc is set to an
! 120362: ** SQLite error code.
! 120363: */
! 120364: static void fts3EvalTokenCosts(
! 120365: Fts3Cursor *pCsr, /* FTS Cursor handle */
! 120366: Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */
! 120367: Fts3Expr *pExpr, /* Expression to consider */
! 120368: Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */
! 120369: Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */
! 120370: int *pRc /* IN/OUT: Error code */
! 120371: ){
! 120372: if( *pRc==SQLITE_OK ){
! 120373: if( pExpr->eType==FTSQUERY_PHRASE ){
! 120374: Fts3Phrase *pPhrase = pExpr->pPhrase;
! 120375: int i;
! 120376: for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
! 120377: Fts3TokenAndCost *pTC = (*ppTC)++;
! 120378: pTC->pPhrase = pPhrase;
! 120379: pTC->iToken = i;
! 120380: pTC->pRoot = pRoot;
! 120381: pTC->pToken = &pPhrase->aToken[i];
! 120382: pTC->iCol = pPhrase->iColumn;
! 120383: *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
! 120384: }
! 120385: }else if( pExpr->eType!=FTSQUERY_NOT ){
! 120386: assert( pExpr->eType==FTSQUERY_OR
! 120387: || pExpr->eType==FTSQUERY_AND
! 120388: || pExpr->eType==FTSQUERY_NEAR
! 120389: );
! 120390: assert( pExpr->pLeft && pExpr->pRight );
! 120391: if( pExpr->eType==FTSQUERY_OR ){
! 120392: pRoot = pExpr->pLeft;
! 120393: **ppOr = pRoot;
! 120394: (*ppOr)++;
! 120395: }
! 120396: fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
! 120397: if( pExpr->eType==FTSQUERY_OR ){
! 120398: pRoot = pExpr->pRight;
! 120399: **ppOr = pRoot;
! 120400: (*ppOr)++;
! 120401: }
! 120402: fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
! 120403: }
! 120404: }
! 120405: }
! 120406:
! 120407: /*
! 120408: ** Determine the average document (row) size in pages. If successful,
! 120409: ** write this value to *pnPage and return SQLITE_OK. Otherwise, return
! 120410: ** an SQLite error code.
! 120411: **
! 120412: ** The average document size in pages is calculated by first calculating
! 120413: ** determining the average size in bytes, B. If B is less than the amount
! 120414: ** of data that will fit on a single leaf page of an intkey table in
! 120415: ** this database, then the average docsize is 1. Otherwise, it is 1 plus
! 120416: ** the number of overflow pages consumed by a record B bytes in size.
! 120417: */
! 120418: static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
! 120419: if( pCsr->nRowAvg==0 ){
! 120420: /* The average document size, which is required to calculate the cost
! 120421: ** of each doclist, has not yet been determined. Read the required
! 120422: ** data from the %_stat table to calculate it.
! 120423: **
! 120424: ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
! 120425: ** varints, where nCol is the number of columns in the FTS3 table.
! 120426: ** The first varint is the number of documents currently stored in
! 120427: ** the table. The following nCol varints contain the total amount of
! 120428: ** data stored in all rows of each column of the table, from left
! 120429: ** to right.
! 120430: */
! 120431: int rc;
! 120432: Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
! 120433: sqlite3_stmt *pStmt;
! 120434: sqlite3_int64 nDoc = 0;
! 120435: sqlite3_int64 nByte = 0;
! 120436: const char *pEnd;
! 120437: const char *a;
! 120438:
! 120439: rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
! 120440: if( rc!=SQLITE_OK ) return rc;
! 120441: a = sqlite3_column_blob(pStmt, 0);
! 120442: assert( a );
! 120443:
! 120444: pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
! 120445: a += sqlite3Fts3GetVarint(a, &nDoc);
! 120446: while( a<pEnd ){
! 120447: a += sqlite3Fts3GetVarint(a, &nByte);
! 120448: }
! 120449: if( nDoc==0 || nByte==0 ){
! 120450: sqlite3_reset(pStmt);
! 120451: return FTS_CORRUPT_VTAB;
! 120452: }
! 120453:
! 120454: pCsr->nDoc = nDoc;
! 120455: pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
! 120456: assert( pCsr->nRowAvg>0 );
! 120457: rc = sqlite3_reset(pStmt);
! 120458: if( rc!=SQLITE_OK ) return rc;
! 120459: }
! 120460:
! 120461: *pnPage = pCsr->nRowAvg;
! 120462: return SQLITE_OK;
! 120463: }
! 120464:
! 120465: /*
! 120466: ** This function is called to select the tokens (if any) that will be
! 120467: ** deferred. The array aTC[] has already been populated when this is
! 120468: ** called.
! 120469: **
! 120470: ** This function is called once for each AND/NEAR cluster in the
! 120471: ** expression. Each invocation determines which tokens to defer within
! 120472: ** the cluster with root node pRoot. See comments above the definition
! 120473: ** of struct Fts3TokenAndCost for more details.
! 120474: **
! 120475: ** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
! 120476: ** called on each token to defer. Otherwise, an SQLite error code is
! 120477: ** returned.
! 120478: */
! 120479: static int fts3EvalSelectDeferred(
! 120480: Fts3Cursor *pCsr, /* FTS Cursor handle */
! 120481: Fts3Expr *pRoot, /* Consider tokens with this root node */
! 120482: Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */
! 120483: int nTC /* Number of entries in aTC[] */
! 120484: ){
! 120485: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 120486: int nDocSize = 0; /* Number of pages per doc loaded */
! 120487: int rc = SQLITE_OK; /* Return code */
! 120488: int ii; /* Iterator variable for various purposes */
! 120489: int nOvfl = 0; /* Total overflow pages used by doclists */
! 120490: int nToken = 0; /* Total number of tokens in cluster */
! 120491:
! 120492: int nMinEst = 0; /* The minimum count for any phrase so far. */
! 120493: int nLoad4 = 1; /* (Phrases that will be loaded)^4. */
! 120494:
! 120495: /* Tokens are never deferred for FTS tables created using the content=xxx
! 120496: ** option. The reason being that it is not guaranteed that the content
! 120497: ** table actually contains the same data as the index. To prevent this from
! 120498: ** causing any problems, the deferred token optimization is completely
! 120499: ** disabled for content=xxx tables. */
! 120500: if( pTab->zContentTbl ){
! 120501: return SQLITE_OK;
! 120502: }
! 120503:
! 120504: /* Count the tokens in this AND/NEAR cluster. If none of the doclists
! 120505: ** associated with the tokens spill onto overflow pages, or if there is
! 120506: ** only 1 token, exit early. No tokens to defer in this case. */
! 120507: for(ii=0; ii<nTC; ii++){
! 120508: if( aTC[ii].pRoot==pRoot ){
! 120509: nOvfl += aTC[ii].nOvfl;
! 120510: nToken++;
! 120511: }
! 120512: }
! 120513: if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
! 120514:
! 120515: /* Obtain the average docsize (in pages). */
! 120516: rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
! 120517: assert( rc!=SQLITE_OK || nDocSize>0 );
! 120518:
! 120519:
! 120520: /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
! 120521: ** of the number of overflow pages that will be loaded by the pager layer
! 120522: ** to retrieve the entire doclist for the token from the full-text index.
! 120523: ** Load the doclists for tokens that are either:
! 120524: **
! 120525: ** a. The cheapest token in the entire query (i.e. the one visited by the
! 120526: ** first iteration of this loop), or
! 120527: **
! 120528: ** b. Part of a multi-token phrase.
! 120529: **
! 120530: ** After each token doclist is loaded, merge it with the others from the
! 120531: ** same phrase and count the number of documents that the merged doclist
! 120532: ** contains. Set variable "nMinEst" to the smallest number of documents in
! 120533: ** any phrase doclist for which 1 or more token doclists have been loaded.
! 120534: ** Let nOther be the number of other phrases for which it is certain that
! 120535: ** one or more tokens will not be deferred.
! 120536: **
! 120537: ** Then, for each token, defer it if loading the doclist would result in
! 120538: ** loading N or more overflow pages into memory, where N is computed as:
! 120539: **
! 120540: ** (nMinEst + 4^nOther - 1) / (4^nOther)
! 120541: */
! 120542: for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
! 120543: int iTC; /* Used to iterate through aTC[] array. */
! 120544: Fts3TokenAndCost *pTC = 0; /* Set to cheapest remaining token. */
! 120545:
! 120546: /* Set pTC to point to the cheapest remaining token. */
! 120547: for(iTC=0; iTC<nTC; iTC++){
! 120548: if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
! 120549: && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
! 120550: ){
! 120551: pTC = &aTC[iTC];
! 120552: }
! 120553: }
! 120554: assert( pTC );
! 120555:
! 120556: if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
! 120557: /* The number of overflow pages to load for this (and therefore all
! 120558: ** subsequent) tokens is greater than the estimated number of pages
! 120559: ** that will be loaded if all subsequent tokens are deferred.
! 120560: */
! 120561: Fts3PhraseToken *pToken = pTC->pToken;
! 120562: rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
! 120563: fts3SegReaderCursorFree(pToken->pSegcsr);
! 120564: pToken->pSegcsr = 0;
! 120565: }else{
! 120566: /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
! 120567: ** for-loop. Except, limit the value to 2^24 to prevent it from
! 120568: ** overflowing the 32-bit integer it is stored in. */
! 120569: if( ii<12 ) nLoad4 = nLoad4*4;
! 120570:
! 120571: if( ii==0 || pTC->pPhrase->nToken>1 ){
! 120572: /* Either this is the cheapest token in the entire query, or it is
! 120573: ** part of a multi-token phrase. Either way, the entire doclist will
! 120574: ** (eventually) be loaded into memory. It may as well be now. */
! 120575: Fts3PhraseToken *pToken = pTC->pToken;
! 120576: int nList = 0;
! 120577: char *pList = 0;
! 120578: rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
! 120579: assert( rc==SQLITE_OK || pList==0 );
! 120580: if( rc==SQLITE_OK ){
! 120581: int nCount;
! 120582: fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
! 120583: nCount = fts3DoclistCountDocids(
! 120584: pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
! 120585: );
! 120586: if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
! 120587: }
! 120588: }
! 120589: }
! 120590: pTC->pToken = 0;
! 120591: }
! 120592:
! 120593: return rc;
! 120594: }
! 120595:
! 120596: /*
! 120597: ** This function is called from within the xFilter method. It initializes
! 120598: ** the full-text query currently stored in pCsr->pExpr. To iterate through
! 120599: ** the results of a query, the caller does:
! 120600: **
! 120601: ** fts3EvalStart(pCsr);
! 120602: ** while( 1 ){
! 120603: ** fts3EvalNext(pCsr);
! 120604: ** if( pCsr->bEof ) break;
! 120605: ** ... return row pCsr->iPrevId to the caller ...
! 120606: ** }
! 120607: */
! 120608: static int fts3EvalStart(Fts3Cursor *pCsr){
! 120609: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 120610: int rc = SQLITE_OK;
! 120611: int nToken = 0;
! 120612: int nOr = 0;
! 120613:
! 120614: /* Allocate a MultiSegReader for each token in the expression. */
! 120615: fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
! 120616:
! 120617: /* Determine which, if any, tokens in the expression should be deferred. */
! 120618: if( rc==SQLITE_OK && nToken>1 && pTab->bHasStat ){
! 120619: Fts3TokenAndCost *aTC;
! 120620: Fts3Expr **apOr;
! 120621: aTC = (Fts3TokenAndCost *)sqlite3_malloc(
! 120622: sizeof(Fts3TokenAndCost) * nToken
! 120623: + sizeof(Fts3Expr *) * nOr * 2
! 120624: );
! 120625: apOr = (Fts3Expr **)&aTC[nToken];
! 120626:
! 120627: if( !aTC ){
! 120628: rc = SQLITE_NOMEM;
! 120629: }else{
! 120630: int ii;
! 120631: Fts3TokenAndCost *pTC = aTC;
! 120632: Fts3Expr **ppOr = apOr;
! 120633:
! 120634: fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
! 120635: nToken = pTC-aTC;
! 120636: nOr = ppOr-apOr;
! 120637:
! 120638: if( rc==SQLITE_OK ){
! 120639: rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
! 120640: for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
! 120641: rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
! 120642: }
! 120643: }
! 120644:
! 120645: sqlite3_free(aTC);
! 120646: }
! 120647: }
! 120648:
! 120649: fts3EvalStartReaders(pCsr, pCsr->pExpr, 1, &rc);
! 120650: return rc;
! 120651: }
! 120652:
! 120653: /*
! 120654: ** Invalidate the current position list for phrase pPhrase.
! 120655: */
! 120656: static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
! 120657: if( pPhrase->doclist.bFreeList ){
! 120658: sqlite3_free(pPhrase->doclist.pList);
! 120659: }
! 120660: pPhrase->doclist.pList = 0;
! 120661: pPhrase->doclist.nList = 0;
! 120662: pPhrase->doclist.bFreeList = 0;
! 120663: }
! 120664:
! 120665: /*
! 120666: ** This function is called to edit the position list associated with
! 120667: ** the phrase object passed as the fifth argument according to a NEAR
! 120668: ** condition. For example:
! 120669: **
! 120670: ** abc NEAR/5 "def ghi"
! 120671: **
! 120672: ** Parameter nNear is passed the NEAR distance of the expression (5 in
! 120673: ** the example above). When this function is called, *paPoslist points to
! 120674: ** the position list, and *pnToken is the number of phrase tokens in, the
! 120675: ** phrase on the other side of the NEAR operator to pPhrase. For example,
! 120676: ** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
! 120677: ** the position list associated with phrase "abc".
! 120678: **
! 120679: ** All positions in the pPhrase position list that are not sufficiently
! 120680: ** close to a position in the *paPoslist position list are removed. If this
! 120681: ** leaves 0 positions, zero is returned. Otherwise, non-zero.
! 120682: **
! 120683: ** Before returning, *paPoslist is set to point to the position lsit
! 120684: ** associated with pPhrase. And *pnToken is set to the number of tokens in
! 120685: ** pPhrase.
! 120686: */
! 120687: static int fts3EvalNearTrim(
! 120688: int nNear, /* NEAR distance. As in "NEAR/nNear". */
! 120689: char *aTmp, /* Temporary space to use */
! 120690: char **paPoslist, /* IN/OUT: Position list */
! 120691: int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */
! 120692: Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */
! 120693: ){
! 120694: int nParam1 = nNear + pPhrase->nToken;
! 120695: int nParam2 = nNear + *pnToken;
! 120696: int nNew;
! 120697: char *p2;
! 120698: char *pOut;
! 120699: int res;
! 120700:
! 120701: assert( pPhrase->doclist.pList );
! 120702:
! 120703: p2 = pOut = pPhrase->doclist.pList;
! 120704: res = fts3PoslistNearMerge(
! 120705: &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
! 120706: );
! 120707: if( res ){
! 120708: nNew = (pOut - pPhrase->doclist.pList) - 1;
! 120709: assert( pPhrase->doclist.pList[nNew]=='\0' );
! 120710: assert( nNew<=pPhrase->doclist.nList && nNew>0 );
! 120711: memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
! 120712: pPhrase->doclist.nList = nNew;
! 120713: *paPoslist = pPhrase->doclist.pList;
! 120714: *pnToken = pPhrase->nToken;
! 120715: }
! 120716:
! 120717: return res;
! 120718: }
! 120719:
! 120720: /*
! 120721: ** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
! 120722: ** Otherwise, it advances the expression passed as the second argument to
! 120723: ** point to the next matching row in the database. Expressions iterate through
! 120724: ** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
! 120725: ** or descending if it is non-zero.
! 120726: **
! 120727: ** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
! 120728: ** successful, the following variables in pExpr are set:
! 120729: **
! 120730: ** Fts3Expr.bEof (non-zero if EOF - there is no next row)
! 120731: ** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row)
! 120732: **
! 120733: ** If the expression is of type FTSQUERY_PHRASE, and the expression is not
! 120734: ** at EOF, then the following variables are populated with the position list
! 120735: ** for the phrase for the visited row:
! 120736: **
! 120737: ** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
! 120738: ** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
! 120739: **
! 120740: ** It says above that this function advances the expression to the next
! 120741: ** matching row. This is usually true, but there are the following exceptions:
! 120742: **
! 120743: ** 1. Deferred tokens are not taken into account. If a phrase consists
! 120744: ** entirely of deferred tokens, it is assumed to match every row in
! 120745: ** the db. In this case the position-list is not populated at all.
! 120746: **
! 120747: ** Or, if a phrase contains one or more deferred tokens and one or
! 120748: ** more non-deferred tokens, then the expression is advanced to the
! 120749: ** next possible match, considering only non-deferred tokens. In other
! 120750: ** words, if the phrase is "A B C", and "B" is deferred, the expression
! 120751: ** is advanced to the next row that contains an instance of "A * C",
! 120752: ** where "*" may match any single token. The position list in this case
! 120753: ** is populated as for "A * C" before returning.
! 120754: **
! 120755: ** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
! 120756: ** advanced to point to the next row that matches "x AND y".
! 120757: **
! 120758: ** See fts3EvalTestDeferredAndNear() for details on testing if a row is
! 120759: ** really a match, taking into account deferred tokens and NEAR operators.
! 120760: */
! 120761: static void fts3EvalNextRow(
! 120762: Fts3Cursor *pCsr, /* FTS Cursor handle */
! 120763: Fts3Expr *pExpr, /* Expr. to advance to next matching row */
! 120764: int *pRc /* IN/OUT: Error code */
! 120765: ){
! 120766: if( *pRc==SQLITE_OK ){
! 120767: int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
! 120768: assert( pExpr->bEof==0 );
! 120769: pExpr->bStart = 1;
! 120770:
! 120771: switch( pExpr->eType ){
! 120772: case FTSQUERY_NEAR:
! 120773: case FTSQUERY_AND: {
! 120774: Fts3Expr *pLeft = pExpr->pLeft;
! 120775: Fts3Expr *pRight = pExpr->pRight;
! 120776: assert( !pLeft->bDeferred || !pRight->bDeferred );
! 120777:
! 120778: if( pLeft->bDeferred ){
! 120779: /* LHS is entirely deferred. So we assume it matches every row.
! 120780: ** Advance the RHS iterator to find the next row visited. */
! 120781: fts3EvalNextRow(pCsr, pRight, pRc);
! 120782: pExpr->iDocid = pRight->iDocid;
! 120783: pExpr->bEof = pRight->bEof;
! 120784: }else if( pRight->bDeferred ){
! 120785: /* RHS is entirely deferred. So we assume it matches every row.
! 120786: ** Advance the LHS iterator to find the next row visited. */
! 120787: fts3EvalNextRow(pCsr, pLeft, pRc);
! 120788: pExpr->iDocid = pLeft->iDocid;
! 120789: pExpr->bEof = pLeft->bEof;
! 120790: }else{
! 120791: /* Neither the RHS or LHS are deferred. */
! 120792: fts3EvalNextRow(pCsr, pLeft, pRc);
! 120793: fts3EvalNextRow(pCsr, pRight, pRc);
! 120794: while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
! 120795: sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
! 120796: if( iDiff==0 ) break;
! 120797: if( iDiff<0 ){
! 120798: fts3EvalNextRow(pCsr, pLeft, pRc);
! 120799: }else{
! 120800: fts3EvalNextRow(pCsr, pRight, pRc);
! 120801: }
! 120802: }
! 120803: pExpr->iDocid = pLeft->iDocid;
! 120804: pExpr->bEof = (pLeft->bEof || pRight->bEof);
! 120805: }
! 120806: break;
! 120807: }
! 120808:
! 120809: case FTSQUERY_OR: {
! 120810: Fts3Expr *pLeft = pExpr->pLeft;
! 120811: Fts3Expr *pRight = pExpr->pRight;
! 120812: sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
! 120813:
! 120814: assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
! 120815: assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
! 120816:
! 120817: if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
! 120818: fts3EvalNextRow(pCsr, pLeft, pRc);
! 120819: }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){
! 120820: fts3EvalNextRow(pCsr, pRight, pRc);
! 120821: }else{
! 120822: fts3EvalNextRow(pCsr, pLeft, pRc);
! 120823: fts3EvalNextRow(pCsr, pRight, pRc);
! 120824: }
! 120825:
! 120826: pExpr->bEof = (pLeft->bEof && pRight->bEof);
! 120827: iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
! 120828: if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
! 120829: pExpr->iDocid = pLeft->iDocid;
! 120830: }else{
! 120831: pExpr->iDocid = pRight->iDocid;
! 120832: }
! 120833:
! 120834: break;
! 120835: }
! 120836:
! 120837: case FTSQUERY_NOT: {
! 120838: Fts3Expr *pLeft = pExpr->pLeft;
! 120839: Fts3Expr *pRight = pExpr->pRight;
! 120840:
! 120841: if( pRight->bStart==0 ){
! 120842: fts3EvalNextRow(pCsr, pRight, pRc);
! 120843: assert( *pRc!=SQLITE_OK || pRight->bStart );
! 120844: }
! 120845:
! 120846: fts3EvalNextRow(pCsr, pLeft, pRc);
! 120847: if( pLeft->bEof==0 ){
! 120848: while( !*pRc
! 120849: && !pRight->bEof
! 120850: && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
! 120851: ){
! 120852: fts3EvalNextRow(pCsr, pRight, pRc);
! 120853: }
! 120854: }
! 120855: pExpr->iDocid = pLeft->iDocid;
! 120856: pExpr->bEof = pLeft->bEof;
! 120857: break;
! 120858: }
! 120859:
! 120860: default: {
! 120861: Fts3Phrase *pPhrase = pExpr->pPhrase;
! 120862: fts3EvalInvalidatePoslist(pPhrase);
! 120863: *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
! 120864: pExpr->iDocid = pPhrase->doclist.iDocid;
! 120865: break;
! 120866: }
! 120867: }
! 120868: }
! 120869: }
! 120870:
! 120871: /*
! 120872: ** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
! 120873: ** cluster, then this function returns 1 immediately.
! 120874: **
! 120875: ** Otherwise, it checks if the current row really does match the NEAR
! 120876: ** expression, using the data currently stored in the position lists
! 120877: ** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
! 120878: **
! 120879: ** If the current row is a match, the position list associated with each
! 120880: ** phrase in the NEAR expression is edited in place to contain only those
! 120881: ** phrase instances sufficiently close to their peers to satisfy all NEAR
! 120882: ** constraints. In this case it returns 1. If the NEAR expression does not
! 120883: ** match the current row, 0 is returned. The position lists may or may not
! 120884: ** be edited if 0 is returned.
! 120885: */
! 120886: static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
! 120887: int res = 1;
! 120888:
! 120889: /* The following block runs if pExpr is the root of a NEAR query.
! 120890: ** For example, the query:
! 120891: **
! 120892: ** "w" NEAR "x" NEAR "y" NEAR "z"
! 120893: **
! 120894: ** which is represented in tree form as:
! 120895: **
! 120896: ** |
! 120897: ** +--NEAR--+ <-- root of NEAR query
! 120898: ** | |
! 120899: ** +--NEAR--+ "z"
! 120900: ** | |
! 120901: ** +--NEAR--+ "y"
! 120902: ** | |
! 120903: ** "w" "x"
! 120904: **
! 120905: ** The right-hand child of a NEAR node is always a phrase. The
! 120906: ** left-hand child may be either a phrase or a NEAR node. There are
! 120907: ** no exceptions to this - it's the way the parser in fts3_expr.c works.
! 120908: */
! 120909: if( *pRc==SQLITE_OK
! 120910: && pExpr->eType==FTSQUERY_NEAR
! 120911: && pExpr->bEof==0
! 120912: && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
! 120913: ){
! 120914: Fts3Expr *p;
! 120915: int nTmp = 0; /* Bytes of temp space */
! 120916: char *aTmp; /* Temp space for PoslistNearMerge() */
! 120917:
! 120918: /* Allocate temporary working space. */
! 120919: for(p=pExpr; p->pLeft; p=p->pLeft){
! 120920: nTmp += p->pRight->pPhrase->doclist.nList;
! 120921: }
! 120922: nTmp += p->pPhrase->doclist.nList;
! 120923: aTmp = sqlite3_malloc(nTmp*2);
! 120924: if( !aTmp ){
! 120925: *pRc = SQLITE_NOMEM;
! 120926: res = 0;
! 120927: }else{
! 120928: char *aPoslist = p->pPhrase->doclist.pList;
! 120929: int nToken = p->pPhrase->nToken;
! 120930:
! 120931: for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
! 120932: Fts3Phrase *pPhrase = p->pRight->pPhrase;
! 120933: int nNear = p->nNear;
! 120934: res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
! 120935: }
! 120936:
! 120937: aPoslist = pExpr->pRight->pPhrase->doclist.pList;
! 120938: nToken = pExpr->pRight->pPhrase->nToken;
! 120939: for(p=pExpr->pLeft; p && res; p=p->pLeft){
! 120940: int nNear;
! 120941: Fts3Phrase *pPhrase;
! 120942: assert( p->pParent && p->pParent->pLeft==p );
! 120943: nNear = p->pParent->nNear;
! 120944: pPhrase = (
! 120945: p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
! 120946: );
! 120947: res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
! 120948: }
! 120949: }
! 120950:
! 120951: sqlite3_free(aTmp);
! 120952: }
! 120953:
! 120954: return res;
! 120955: }
! 120956:
! 120957: /*
! 120958: ** This function is a helper function for fts3EvalTestDeferredAndNear().
! 120959: ** Assuming no error occurs or has occurred, It returns non-zero if the
! 120960: ** expression passed as the second argument matches the row that pCsr
! 120961: ** currently points to, or zero if it does not.
! 120962: **
! 120963: ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
! 120964: ** If an error occurs during execution of this function, *pRc is set to
! 120965: ** the appropriate SQLite error code. In this case the returned value is
! 120966: ** undefined.
! 120967: */
! 120968: static int fts3EvalTestExpr(
! 120969: Fts3Cursor *pCsr, /* FTS cursor handle */
! 120970: Fts3Expr *pExpr, /* Expr to test. May or may not be root. */
! 120971: int *pRc /* IN/OUT: Error code */
! 120972: ){
! 120973: int bHit = 1; /* Return value */
! 120974: if( *pRc==SQLITE_OK ){
! 120975: switch( pExpr->eType ){
! 120976: case FTSQUERY_NEAR:
! 120977: case FTSQUERY_AND:
! 120978: bHit = (
! 120979: fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
! 120980: && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
! 120981: && fts3EvalNearTest(pExpr, pRc)
! 120982: );
! 120983:
! 120984: /* If the NEAR expression does not match any rows, zero the doclist for
! 120985: ** all phrases involved in the NEAR. This is because the snippet(),
! 120986: ** offsets() and matchinfo() functions are not supposed to recognize
! 120987: ** any instances of phrases that are part of unmatched NEAR queries.
! 120988: ** For example if this expression:
! 120989: **
! 120990: ** ... MATCH 'a OR (b NEAR c)'
! 120991: **
! 120992: ** is matched against a row containing:
! 120993: **
! 120994: ** 'a b d e'
! 120995: **
! 120996: ** then any snippet() should ony highlight the "a" term, not the "b"
! 120997: ** (as "b" is part of a non-matching NEAR clause).
! 120998: */
! 120999: if( bHit==0
! 121000: && pExpr->eType==FTSQUERY_NEAR
! 121001: && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
! 121002: ){
! 121003: Fts3Expr *p;
! 121004: for(p=pExpr; p->pPhrase==0; p=p->pLeft){
! 121005: if( p->pRight->iDocid==pCsr->iPrevId ){
! 121006: fts3EvalInvalidatePoslist(p->pRight->pPhrase);
! 121007: }
! 121008: }
! 121009: if( p->iDocid==pCsr->iPrevId ){
! 121010: fts3EvalInvalidatePoslist(p->pPhrase);
! 121011: }
! 121012: }
! 121013:
! 121014: break;
! 121015:
! 121016: case FTSQUERY_OR: {
! 121017: int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
! 121018: int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
! 121019: bHit = bHit1 || bHit2;
! 121020: break;
! 121021: }
! 121022:
! 121023: case FTSQUERY_NOT:
! 121024: bHit = (
! 121025: fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
! 121026: && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
! 121027: );
! 121028: break;
! 121029:
! 121030: default: {
! 121031: if( pCsr->pDeferred
! 121032: && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
! 121033: ){
! 121034: Fts3Phrase *pPhrase = pExpr->pPhrase;
! 121035: assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
! 121036: if( pExpr->bDeferred ){
! 121037: fts3EvalInvalidatePoslist(pPhrase);
! 121038: }
! 121039: *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
! 121040: bHit = (pPhrase->doclist.pList!=0);
! 121041: pExpr->iDocid = pCsr->iPrevId;
! 121042: }else{
! 121043: bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
! 121044: }
! 121045: break;
! 121046: }
! 121047: }
! 121048: }
! 121049: return bHit;
! 121050: }
! 121051:
! 121052: /*
! 121053: ** This function is called as the second part of each xNext operation when
! 121054: ** iterating through the results of a full-text query. At this point the
! 121055: ** cursor points to a row that matches the query expression, with the
! 121056: ** following caveats:
! 121057: **
! 121058: ** * Up until this point, "NEAR" operators in the expression have been
! 121059: ** treated as "AND".
! 121060: **
! 121061: ** * Deferred tokens have not yet been considered.
! 121062: **
! 121063: ** If *pRc is not SQLITE_OK when this function is called, it immediately
! 121064: ** returns 0. Otherwise, it tests whether or not after considering NEAR
! 121065: ** operators and deferred tokens the current row is still a match for the
! 121066: ** expression. It returns 1 if both of the following are true:
! 121067: **
! 121068: ** 1. *pRc is SQLITE_OK when this function returns, and
! 121069: **
! 121070: ** 2. After scanning the current FTS table row for the deferred tokens,
! 121071: ** it is determined that the row does *not* match the query.
! 121072: **
! 121073: ** Or, if no error occurs and it seems the current row does match the FTS
! 121074: ** query, return 0.
! 121075: */
! 121076: static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){
! 121077: int rc = *pRc;
! 121078: int bMiss = 0;
! 121079: if( rc==SQLITE_OK ){
! 121080:
! 121081: /* If there are one or more deferred tokens, load the current row into
! 121082: ** memory and scan it to determine the position list for each deferred
! 121083: ** token. Then, see if this row is really a match, considering deferred
! 121084: ** tokens and NEAR operators (neither of which were taken into account
! 121085: ** earlier, by fts3EvalNextRow()).
! 121086: */
! 121087: if( pCsr->pDeferred ){
! 121088: rc = fts3CursorSeek(0, pCsr);
! 121089: if( rc==SQLITE_OK ){
! 121090: rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
! 121091: }
! 121092: }
! 121093: bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
! 121094:
! 121095: /* Free the position-lists accumulated for each deferred token above. */
! 121096: sqlite3Fts3FreeDeferredDoclists(pCsr);
! 121097: *pRc = rc;
! 121098: }
! 121099: return (rc==SQLITE_OK && bMiss);
! 121100: }
! 121101:
! 121102: /*
! 121103: ** Advance to the next document that matches the FTS expression in
! 121104: ** Fts3Cursor.pExpr.
! 121105: */
! 121106: static int fts3EvalNext(Fts3Cursor *pCsr){
! 121107: int rc = SQLITE_OK; /* Return Code */
! 121108: Fts3Expr *pExpr = pCsr->pExpr;
! 121109: assert( pCsr->isEof==0 );
! 121110: if( pExpr==0 ){
! 121111: pCsr->isEof = 1;
! 121112: }else{
! 121113: do {
! 121114: if( pCsr->isRequireSeek==0 ){
! 121115: sqlite3_reset(pCsr->pStmt);
! 121116: }
! 121117: assert( sqlite3_data_count(pCsr->pStmt)==0 );
! 121118: fts3EvalNextRow(pCsr, pExpr, &rc);
! 121119: pCsr->isEof = pExpr->bEof;
! 121120: pCsr->isRequireSeek = 1;
! 121121: pCsr->isMatchinfoNeeded = 1;
! 121122: pCsr->iPrevId = pExpr->iDocid;
! 121123: }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) );
! 121124: }
! 121125: return rc;
! 121126: }
! 121127:
! 121128: /*
! 121129: ** Restart interation for expression pExpr so that the next call to
! 121130: ** fts3EvalNext() visits the first row. Do not allow incremental
! 121131: ** loading or merging of phrase doclists for this iteration.
! 121132: **
! 121133: ** If *pRc is other than SQLITE_OK when this function is called, it is
! 121134: ** a no-op. If an error occurs within this function, *pRc is set to an
! 121135: ** SQLite error code before returning.
! 121136: */
! 121137: static void fts3EvalRestart(
! 121138: Fts3Cursor *pCsr,
! 121139: Fts3Expr *pExpr,
! 121140: int *pRc
! 121141: ){
! 121142: if( pExpr && *pRc==SQLITE_OK ){
! 121143: Fts3Phrase *pPhrase = pExpr->pPhrase;
! 121144:
! 121145: if( pPhrase ){
! 121146: fts3EvalInvalidatePoslist(pPhrase);
! 121147: if( pPhrase->bIncr ){
! 121148: assert( pPhrase->nToken==1 );
! 121149: assert( pPhrase->aToken[0].pSegcsr );
! 121150: sqlite3Fts3MsrIncrRestart(pPhrase->aToken[0].pSegcsr);
! 121151: *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
! 121152: }
! 121153:
! 121154: pPhrase->doclist.pNextDocid = 0;
! 121155: pPhrase->doclist.iDocid = 0;
! 121156: }
! 121157:
! 121158: pExpr->iDocid = 0;
! 121159: pExpr->bEof = 0;
! 121160: pExpr->bStart = 0;
! 121161:
! 121162: fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
! 121163: fts3EvalRestart(pCsr, pExpr->pRight, pRc);
! 121164: }
! 121165: }
! 121166:
! 121167: /*
! 121168: ** After allocating the Fts3Expr.aMI[] array for each phrase in the
! 121169: ** expression rooted at pExpr, the cursor iterates through all rows matched
! 121170: ** by pExpr, calling this function for each row. This function increments
! 121171: ** the values in Fts3Expr.aMI[] according to the position-list currently
! 121172: ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
! 121173: ** expression nodes.
! 121174: */
! 121175: static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
! 121176: if( pExpr ){
! 121177: Fts3Phrase *pPhrase = pExpr->pPhrase;
! 121178: if( pPhrase && pPhrase->doclist.pList ){
! 121179: int iCol = 0;
! 121180: char *p = pPhrase->doclist.pList;
! 121181:
! 121182: assert( *p );
! 121183: while( 1 ){
! 121184: u8 c = 0;
! 121185: int iCnt = 0;
! 121186: while( 0xFE & (*p | c) ){
! 121187: if( (c&0x80)==0 ) iCnt++;
! 121188: c = *p++ & 0x80;
! 121189: }
! 121190:
! 121191: /* aMI[iCol*3 + 1] = Number of occurrences
! 121192: ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
! 121193: */
! 121194: pExpr->aMI[iCol*3 + 1] += iCnt;
! 121195: pExpr->aMI[iCol*3 + 2] += (iCnt>0);
! 121196: if( *p==0x00 ) break;
! 121197: p++;
! 121198: p += sqlite3Fts3GetVarint32(p, &iCol);
! 121199: }
! 121200: }
! 121201:
! 121202: fts3EvalUpdateCounts(pExpr->pLeft);
! 121203: fts3EvalUpdateCounts(pExpr->pRight);
! 121204: }
! 121205: }
! 121206:
! 121207: /*
! 121208: ** Expression pExpr must be of type FTSQUERY_PHRASE.
! 121209: **
! 121210: ** If it is not already allocated and populated, this function allocates and
! 121211: ** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
! 121212: ** of a NEAR expression, then it also allocates and populates the same array
! 121213: ** for all other phrases that are part of the NEAR expression.
! 121214: **
! 121215: ** SQLITE_OK is returned if the aMI[] array is successfully allocated and
! 121216: ** populated. Otherwise, if an error occurs, an SQLite error code is returned.
! 121217: */
! 121218: static int fts3EvalGatherStats(
! 121219: Fts3Cursor *pCsr, /* Cursor object */
! 121220: Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */
! 121221: ){
! 121222: int rc = SQLITE_OK; /* Return code */
! 121223:
! 121224: assert( pExpr->eType==FTSQUERY_PHRASE );
! 121225: if( pExpr->aMI==0 ){
! 121226: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 121227: Fts3Expr *pRoot; /* Root of NEAR expression */
! 121228: Fts3Expr *p; /* Iterator used for several purposes */
! 121229:
! 121230: sqlite3_int64 iPrevId = pCsr->iPrevId;
! 121231: sqlite3_int64 iDocid;
! 121232: u8 bEof;
! 121233:
! 121234: /* Find the root of the NEAR expression */
! 121235: pRoot = pExpr;
! 121236: while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
! 121237: pRoot = pRoot->pParent;
! 121238: }
! 121239: iDocid = pRoot->iDocid;
! 121240: bEof = pRoot->bEof;
! 121241: assert( pRoot->bStart );
! 121242:
! 121243: /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
! 121244: for(p=pRoot; p; p=p->pLeft){
! 121245: Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
! 121246: assert( pE->aMI==0 );
! 121247: pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
! 121248: if( !pE->aMI ) return SQLITE_NOMEM;
! 121249: memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
! 121250: }
! 121251:
! 121252: fts3EvalRestart(pCsr, pRoot, &rc);
! 121253:
! 121254: while( pCsr->isEof==0 && rc==SQLITE_OK ){
! 121255:
! 121256: do {
! 121257: /* Ensure the %_content statement is reset. */
! 121258: if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
! 121259: assert( sqlite3_data_count(pCsr->pStmt)==0 );
! 121260:
! 121261: /* Advance to the next document */
! 121262: fts3EvalNextRow(pCsr, pRoot, &rc);
! 121263: pCsr->isEof = pRoot->bEof;
! 121264: pCsr->isRequireSeek = 1;
! 121265: pCsr->isMatchinfoNeeded = 1;
! 121266: pCsr->iPrevId = pRoot->iDocid;
! 121267: }while( pCsr->isEof==0
! 121268: && pRoot->eType==FTSQUERY_NEAR
! 121269: && fts3EvalTestDeferredAndNear(pCsr, &rc)
! 121270: );
! 121271:
! 121272: if( rc==SQLITE_OK && pCsr->isEof==0 ){
! 121273: fts3EvalUpdateCounts(pRoot);
! 121274: }
! 121275: }
! 121276:
! 121277: pCsr->isEof = 0;
! 121278: pCsr->iPrevId = iPrevId;
! 121279:
! 121280: if( bEof ){
! 121281: pRoot->bEof = bEof;
! 121282: }else{
! 121283: /* Caution: pRoot may iterate through docids in ascending or descending
! 121284: ** order. For this reason, even though it seems more defensive, the
! 121285: ** do loop can not be written:
! 121286: **
! 121287: ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
! 121288: */
! 121289: fts3EvalRestart(pCsr, pRoot, &rc);
! 121290: do {
! 121291: fts3EvalNextRow(pCsr, pRoot, &rc);
! 121292: assert( pRoot->bEof==0 );
! 121293: }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
! 121294: fts3EvalTestDeferredAndNear(pCsr, &rc);
! 121295: }
! 121296: }
! 121297: return rc;
! 121298: }
! 121299:
! 121300: /*
! 121301: ** This function is used by the matchinfo() module to query a phrase
! 121302: ** expression node for the following information:
! 121303: **
! 121304: ** 1. The total number of occurrences of the phrase in each column of
! 121305: ** the FTS table (considering all rows), and
! 121306: **
! 121307: ** 2. For each column, the number of rows in the table for which the
! 121308: ** column contains at least one instance of the phrase.
! 121309: **
! 121310: ** If no error occurs, SQLITE_OK is returned and the values for each column
! 121311: ** written into the array aiOut as follows:
! 121312: **
! 121313: ** aiOut[iCol*3 + 1] = Number of occurrences
! 121314: ** aiOut[iCol*3 + 2] = Number of rows containing at least one instance
! 121315: **
! 121316: ** Caveats:
! 121317: **
! 121318: ** * If a phrase consists entirely of deferred tokens, then all output
! 121319: ** values are set to the number of documents in the table. In other
! 121320: ** words we assume that very common tokens occur exactly once in each
! 121321: ** column of each row of the table.
! 121322: **
! 121323: ** * If a phrase contains some deferred tokens (and some non-deferred
! 121324: ** tokens), count the potential occurrence identified by considering
! 121325: ** the non-deferred tokens instead of actual phrase occurrences.
! 121326: **
! 121327: ** * If the phrase is part of a NEAR expression, then only phrase instances
! 121328: ** that meet the NEAR constraint are included in the counts.
! 121329: */
! 121330: SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
! 121331: Fts3Cursor *pCsr, /* FTS cursor handle */
! 121332: Fts3Expr *pExpr, /* Phrase expression */
! 121333: u32 *aiOut /* Array to write results into (see above) */
! 121334: ){
! 121335: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 121336: int rc = SQLITE_OK;
! 121337: int iCol;
! 121338:
! 121339: if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
! 121340: assert( pCsr->nDoc>0 );
! 121341: for(iCol=0; iCol<pTab->nColumn; iCol++){
! 121342: aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
! 121343: aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
! 121344: }
! 121345: }else{
! 121346: rc = fts3EvalGatherStats(pCsr, pExpr);
! 121347: if( rc==SQLITE_OK ){
! 121348: assert( pExpr->aMI );
! 121349: for(iCol=0; iCol<pTab->nColumn; iCol++){
! 121350: aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
! 121351: aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
! 121352: }
! 121353: }
! 121354: }
! 121355:
! 121356: return rc;
! 121357: }
! 121358:
! 121359: /*
! 121360: ** The expression pExpr passed as the second argument to this function
! 121361: ** must be of type FTSQUERY_PHRASE.
! 121362: **
! 121363: ** The returned value is either NULL or a pointer to a buffer containing
! 121364: ** a position-list indicating the occurrences of the phrase in column iCol
! 121365: ** of the current row.
! 121366: **
! 121367: ** More specifically, the returned buffer contains 1 varint for each
! 121368: ** occurence of the phrase in the column, stored using the normal (delta+2)
! 121369: ** compression and is terminated by either an 0x01 or 0x00 byte. For example,
! 121370: ** if the requested column contains "a b X c d X X" and the position-list
! 121371: ** for 'X' is requested, the buffer returned may contain:
! 121372: **
! 121373: ** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00
! 121374: **
! 121375: ** This function works regardless of whether or not the phrase is deferred,
! 121376: ** incremental, or neither.
! 121377: */
! 121378: SQLITE_PRIVATE char *sqlite3Fts3EvalPhrasePoslist(
! 121379: Fts3Cursor *pCsr, /* FTS3 cursor object */
! 121380: Fts3Expr *pExpr, /* Phrase to return doclist for */
! 121381: int iCol /* Column to return position list for */
! 121382: ){
! 121383: Fts3Phrase *pPhrase = pExpr->pPhrase;
! 121384: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 121385: char *pIter = pPhrase->doclist.pList;
! 121386: int iThis;
! 121387:
! 121388: assert( iCol>=0 && iCol<pTab->nColumn );
! 121389: if( !pIter
! 121390: || pExpr->bEof
! 121391: || pExpr->iDocid!=pCsr->iPrevId
! 121392: || (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol)
! 121393: ){
! 121394: return 0;
! 121395: }
! 121396:
! 121397: assert( pPhrase->doclist.nList>0 );
! 121398: if( *pIter==0x01 ){
! 121399: pIter++;
! 121400: pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
! 121401: }else{
! 121402: iThis = 0;
! 121403: }
! 121404: while( iThis<iCol ){
! 121405: fts3ColumnlistCopy(0, &pIter);
! 121406: if( *pIter==0x00 ) return 0;
! 121407: pIter++;
! 121408: pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
! 121409: }
! 121410:
! 121411: return ((iCol==iThis)?pIter:0);
! 121412: }
! 121413:
! 121414: /*
! 121415: ** Free all components of the Fts3Phrase structure that were allocated by
! 121416: ** the eval module. Specifically, this means to free:
! 121417: **
! 121418: ** * the contents of pPhrase->doclist, and
! 121419: ** * any Fts3MultiSegReader objects held by phrase tokens.
! 121420: */
! 121421: SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
! 121422: if( pPhrase ){
! 121423: int i;
! 121424: sqlite3_free(pPhrase->doclist.aAll);
! 121425: fts3EvalInvalidatePoslist(pPhrase);
! 121426: memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
! 121427: for(i=0; i<pPhrase->nToken; i++){
! 121428: fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
! 121429: pPhrase->aToken[i].pSegcsr = 0;
! 121430: }
! 121431: }
! 121432: }
! 121433:
! 121434: /*
! 121435: ** Return SQLITE_CORRUPT_VTAB.
! 121436: */
! 121437: #ifdef SQLITE_DEBUG
! 121438: SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
! 121439: return SQLITE_CORRUPT_VTAB;
! 121440: }
! 121441: #endif
! 121442:
! 121443: #if !SQLITE_CORE
! 121444: /*
! 121445: ** Initialize API pointer table, if required.
! 121446: */
! 121447: SQLITE_API int sqlite3_extension_init(
! 121448: sqlite3 *db,
! 121449: char **pzErrMsg,
! 121450: const sqlite3_api_routines *pApi
! 121451: ){
! 121452: SQLITE_EXTENSION_INIT2(pApi)
! 121453: return sqlite3Fts3Init(db);
! 121454: }
! 121455: #endif
! 121456:
! 121457: #endif
! 121458:
! 121459: /************** End of fts3.c ************************************************/
! 121460: /************** Begin file fts3_aux.c ****************************************/
! 121461: /*
! 121462: ** 2011 Jan 27
! 121463: **
! 121464: ** The author disclaims copyright to this source code. In place of
! 121465: ** a legal notice, here is a blessing:
! 121466: **
! 121467: ** May you do good and not evil.
! 121468: ** May you find forgiveness for yourself and forgive others.
! 121469: ** May you share freely, never taking more than you give.
! 121470: **
! 121471: ******************************************************************************
! 121472: **
! 121473: */
! 121474: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
! 121475:
! 121476: /* #include <string.h> */
! 121477: /* #include <assert.h> */
! 121478:
! 121479: typedef struct Fts3auxTable Fts3auxTable;
! 121480: typedef struct Fts3auxCursor Fts3auxCursor;
! 121481:
! 121482: struct Fts3auxTable {
! 121483: sqlite3_vtab base; /* Base class used by SQLite core */
! 121484: Fts3Table *pFts3Tab;
! 121485: };
! 121486:
! 121487: struct Fts3auxCursor {
! 121488: sqlite3_vtab_cursor base; /* Base class used by SQLite core */
! 121489: Fts3MultiSegReader csr; /* Must be right after "base" */
! 121490: Fts3SegFilter filter;
! 121491: char *zStop;
! 121492: int nStop; /* Byte-length of string zStop */
! 121493: int isEof; /* True if cursor is at EOF */
! 121494: sqlite3_int64 iRowid; /* Current rowid */
! 121495:
! 121496: int iCol; /* Current value of 'col' column */
! 121497: int nStat; /* Size of aStat[] array */
! 121498: struct Fts3auxColstats {
! 121499: sqlite3_int64 nDoc; /* 'documents' values for current csr row */
! 121500: sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
! 121501: } *aStat;
! 121502: };
! 121503:
! 121504: /*
! 121505: ** Schema of the terms table.
! 121506: */
! 121507: #define FTS3_TERMS_SCHEMA "CREATE TABLE x(term, col, documents, occurrences)"
! 121508:
! 121509: /*
! 121510: ** This function does all the work for both the xConnect and xCreate methods.
! 121511: ** These tables have no persistent representation of their own, so xConnect
! 121512: ** and xCreate are identical operations.
! 121513: */
! 121514: static int fts3auxConnectMethod(
! 121515: sqlite3 *db, /* Database connection */
! 121516: void *pUnused, /* Unused */
! 121517: int argc, /* Number of elements in argv array */
! 121518: const char * const *argv, /* xCreate/xConnect argument array */
! 121519: sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
! 121520: char **pzErr /* OUT: sqlite3_malloc'd error message */
! 121521: ){
! 121522: char const *zDb; /* Name of database (e.g. "main") */
! 121523: char const *zFts3; /* Name of fts3 table */
! 121524: int nDb; /* Result of strlen(zDb) */
! 121525: int nFts3; /* Result of strlen(zFts3) */
! 121526: int nByte; /* Bytes of space to allocate here */
! 121527: int rc; /* value returned by declare_vtab() */
! 121528: Fts3auxTable *p; /* Virtual table object to return */
! 121529:
! 121530: UNUSED_PARAMETER(pUnused);
! 121531:
! 121532: /* The user should specify a single argument - the name of an fts3 table. */
! 121533: if( argc!=4 ){
! 121534: *pzErr = sqlite3_mprintf(
! 121535: "wrong number of arguments to fts4aux constructor"
! 121536: );
! 121537: return SQLITE_ERROR;
! 121538: }
! 121539:
! 121540: zDb = argv[1];
! 121541: nDb = strlen(zDb);
! 121542: zFts3 = argv[3];
! 121543: nFts3 = strlen(zFts3);
! 121544:
! 121545: rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
! 121546: if( rc!=SQLITE_OK ) return rc;
! 121547:
! 121548: nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
! 121549: p = (Fts3auxTable *)sqlite3_malloc(nByte);
! 121550: if( !p ) return SQLITE_NOMEM;
! 121551: memset(p, 0, nByte);
! 121552:
! 121553: p->pFts3Tab = (Fts3Table *)&p[1];
! 121554: p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
! 121555: p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
! 121556: p->pFts3Tab->db = db;
! 121557: p->pFts3Tab->nIndex = 1;
! 121558:
! 121559: memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
! 121560: memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
! 121561: sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
! 121562:
! 121563: *ppVtab = (sqlite3_vtab *)p;
! 121564: return SQLITE_OK;
! 121565: }
! 121566:
! 121567: /*
! 121568: ** This function does the work for both the xDisconnect and xDestroy methods.
! 121569: ** These tables have no persistent representation of their own, so xDisconnect
! 121570: ** and xDestroy are identical operations.
! 121571: */
! 121572: static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
! 121573: Fts3auxTable *p = (Fts3auxTable *)pVtab;
! 121574: Fts3Table *pFts3 = p->pFts3Tab;
! 121575: int i;
! 121576:
! 121577: /* Free any prepared statements held */
! 121578: for(i=0; i<SizeofArray(pFts3->aStmt); i++){
! 121579: sqlite3_finalize(pFts3->aStmt[i]);
! 121580: }
! 121581: sqlite3_free(pFts3->zSegmentsTbl);
! 121582: sqlite3_free(p);
! 121583: return SQLITE_OK;
! 121584: }
! 121585:
! 121586: #define FTS4AUX_EQ_CONSTRAINT 1
! 121587: #define FTS4AUX_GE_CONSTRAINT 2
! 121588: #define FTS4AUX_LE_CONSTRAINT 4
! 121589:
! 121590: /*
! 121591: ** xBestIndex - Analyze a WHERE and ORDER BY clause.
! 121592: */
! 121593: static int fts3auxBestIndexMethod(
! 121594: sqlite3_vtab *pVTab,
! 121595: sqlite3_index_info *pInfo
! 121596: ){
! 121597: int i;
! 121598: int iEq = -1;
! 121599: int iGe = -1;
! 121600: int iLe = -1;
! 121601:
! 121602: UNUSED_PARAMETER(pVTab);
! 121603:
! 121604: /* This vtab delivers always results in "ORDER BY term ASC" order. */
! 121605: if( pInfo->nOrderBy==1
! 121606: && pInfo->aOrderBy[0].iColumn==0
! 121607: && pInfo->aOrderBy[0].desc==0
! 121608: ){
! 121609: pInfo->orderByConsumed = 1;
! 121610: }
! 121611:
! 121612: /* Search for equality and range constraints on the "term" column. */
! 121613: for(i=0; i<pInfo->nConstraint; i++){
! 121614: if( pInfo->aConstraint[i].usable && pInfo->aConstraint[i].iColumn==0 ){
! 121615: int op = pInfo->aConstraint[i].op;
! 121616: if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
! 121617: if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
! 121618: if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
! 121619: if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
! 121620: if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
! 121621: }
! 121622: }
! 121623:
! 121624: if( iEq>=0 ){
! 121625: pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
! 121626: pInfo->aConstraintUsage[iEq].argvIndex = 1;
! 121627: pInfo->estimatedCost = 5;
! 121628: }else{
! 121629: pInfo->idxNum = 0;
! 121630: pInfo->estimatedCost = 20000;
! 121631: if( iGe>=0 ){
! 121632: pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
! 121633: pInfo->aConstraintUsage[iGe].argvIndex = 1;
! 121634: pInfo->estimatedCost /= 2;
! 121635: }
! 121636: if( iLe>=0 ){
! 121637: pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
! 121638: pInfo->aConstraintUsage[iLe].argvIndex = 1 + (iGe>=0);
! 121639: pInfo->estimatedCost /= 2;
! 121640: }
! 121641: }
! 121642:
! 121643: return SQLITE_OK;
! 121644: }
! 121645:
! 121646: /*
! 121647: ** xOpen - Open a cursor.
! 121648: */
! 121649: static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
! 121650: Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
! 121651:
! 121652: UNUSED_PARAMETER(pVTab);
! 121653:
! 121654: pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
! 121655: if( !pCsr ) return SQLITE_NOMEM;
! 121656: memset(pCsr, 0, sizeof(Fts3auxCursor));
! 121657:
! 121658: *ppCsr = (sqlite3_vtab_cursor *)pCsr;
! 121659: return SQLITE_OK;
! 121660: }
! 121661:
! 121662: /*
! 121663: ** xClose - Close a cursor.
! 121664: */
! 121665: static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
! 121666: Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
! 121667: Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
! 121668:
! 121669: sqlite3Fts3SegmentsClose(pFts3);
! 121670: sqlite3Fts3SegReaderFinish(&pCsr->csr);
! 121671: sqlite3_free((void *)pCsr->filter.zTerm);
! 121672: sqlite3_free(pCsr->zStop);
! 121673: sqlite3_free(pCsr->aStat);
! 121674: sqlite3_free(pCsr);
! 121675: return SQLITE_OK;
! 121676: }
! 121677:
! 121678: static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
! 121679: if( nSize>pCsr->nStat ){
! 121680: struct Fts3auxColstats *aNew;
! 121681: aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat,
! 121682: sizeof(struct Fts3auxColstats) * nSize
! 121683: );
! 121684: if( aNew==0 ) return SQLITE_NOMEM;
! 121685: memset(&aNew[pCsr->nStat], 0,
! 121686: sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
! 121687: );
! 121688: pCsr->aStat = aNew;
! 121689: pCsr->nStat = nSize;
! 121690: }
! 121691: return SQLITE_OK;
! 121692: }
! 121693:
! 121694: /*
! 121695: ** xNext - Advance the cursor to the next row, if any.
! 121696: */
! 121697: static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
! 121698: Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
! 121699: Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
! 121700: int rc;
! 121701:
! 121702: /* Increment our pretend rowid value. */
! 121703: pCsr->iRowid++;
! 121704:
! 121705: for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
! 121706: if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
! 121707: }
! 121708:
! 121709: rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
! 121710: if( rc==SQLITE_ROW ){
! 121711: int i = 0;
! 121712: int nDoclist = pCsr->csr.nDoclist;
! 121713: char *aDoclist = pCsr->csr.aDoclist;
! 121714: int iCol;
! 121715:
! 121716: int eState = 0;
! 121717:
! 121718: if( pCsr->zStop ){
! 121719: int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
! 121720: int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
! 121721: if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
! 121722: pCsr->isEof = 1;
! 121723: return SQLITE_OK;
! 121724: }
! 121725: }
! 121726:
! 121727: if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
! 121728: memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
! 121729: iCol = 0;
! 121730:
! 121731: while( i<nDoclist ){
! 121732: sqlite3_int64 v = 0;
! 121733:
! 121734: i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
! 121735: switch( eState ){
! 121736: /* State 0. In this state the integer just read was a docid. */
! 121737: case 0:
! 121738: pCsr->aStat[0].nDoc++;
! 121739: eState = 1;
! 121740: iCol = 0;
! 121741: break;
! 121742:
! 121743: /* State 1. In this state we are expecting either a 1, indicating
! 121744: ** that the following integer will be a column number, or the
! 121745: ** start of a position list for column 0.
! 121746: **
! 121747: ** The only difference between state 1 and state 2 is that if the
! 121748: ** integer encountered in state 1 is not 0 or 1, then we need to
! 121749: ** increment the column 0 "nDoc" count for this term.
! 121750: */
! 121751: case 1:
! 121752: assert( iCol==0 );
! 121753: if( v>1 ){
! 121754: pCsr->aStat[1].nDoc++;
! 121755: }
! 121756: eState = 2;
! 121757: /* fall through */
! 121758:
! 121759: case 2:
! 121760: if( v==0 ){ /* 0x00. Next integer will be a docid. */
! 121761: eState = 0;
! 121762: }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
! 121763: eState = 3;
! 121764: }else{ /* 2 or greater. A position. */
! 121765: pCsr->aStat[iCol+1].nOcc++;
! 121766: pCsr->aStat[0].nOcc++;
! 121767: }
! 121768: break;
! 121769:
! 121770: /* State 3. The integer just read is a column number. */
! 121771: default: assert( eState==3 );
! 121772: iCol = (int)v;
! 121773: if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
! 121774: pCsr->aStat[iCol+1].nDoc++;
! 121775: eState = 2;
! 121776: break;
! 121777: }
! 121778: }
! 121779:
! 121780: pCsr->iCol = 0;
! 121781: rc = SQLITE_OK;
! 121782: }else{
! 121783: pCsr->isEof = 1;
! 121784: }
! 121785: return rc;
! 121786: }
! 121787:
! 121788: /*
! 121789: ** xFilter - Initialize a cursor to point at the start of its data.
! 121790: */
! 121791: static int fts3auxFilterMethod(
! 121792: sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
! 121793: int idxNum, /* Strategy index */
! 121794: const char *idxStr, /* Unused */
! 121795: int nVal, /* Number of elements in apVal */
! 121796: sqlite3_value **apVal /* Arguments for the indexing scheme */
! 121797: ){
! 121798: Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
! 121799: Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
! 121800: int rc;
! 121801: int isScan;
! 121802:
! 121803: UNUSED_PARAMETER(nVal);
! 121804: UNUSED_PARAMETER(idxStr);
! 121805:
! 121806: assert( idxStr==0 );
! 121807: assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
! 121808: || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
! 121809: || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
! 121810: );
! 121811: isScan = (idxNum!=FTS4AUX_EQ_CONSTRAINT);
! 121812:
! 121813: /* In case this cursor is being reused, close and zero it. */
! 121814: testcase(pCsr->filter.zTerm);
! 121815: sqlite3Fts3SegReaderFinish(&pCsr->csr);
! 121816: sqlite3_free((void *)pCsr->filter.zTerm);
! 121817: sqlite3_free(pCsr->aStat);
! 121818: memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
! 121819:
! 121820: pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
! 121821: if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
! 121822:
! 121823: if( idxNum&(FTS4AUX_EQ_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) ){
! 121824: const unsigned char *zStr = sqlite3_value_text(apVal[0]);
! 121825: if( zStr ){
! 121826: pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
! 121827: pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
! 121828: if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
! 121829: }
! 121830: }
! 121831: if( idxNum&FTS4AUX_LE_CONSTRAINT ){
! 121832: int iIdx = (idxNum&FTS4AUX_GE_CONSTRAINT) ? 1 : 0;
! 121833: pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iIdx]));
! 121834: pCsr->nStop = sqlite3_value_bytes(apVal[iIdx]);
! 121835: if( pCsr->zStop==0 ) return SQLITE_NOMEM;
! 121836: }
! 121837:
! 121838: rc = sqlite3Fts3SegReaderCursor(pFts3, 0, FTS3_SEGCURSOR_ALL,
! 121839: pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
! 121840: );
! 121841: if( rc==SQLITE_OK ){
! 121842: rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
! 121843: }
! 121844:
! 121845: if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
! 121846: return rc;
! 121847: }
! 121848:
! 121849: /*
! 121850: ** xEof - Return true if the cursor is at EOF, or false otherwise.
! 121851: */
! 121852: static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
! 121853: Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
! 121854: return pCsr->isEof;
! 121855: }
! 121856:
! 121857: /*
! 121858: ** xColumn - Return a column value.
! 121859: */
! 121860: static int fts3auxColumnMethod(
! 121861: sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
! 121862: sqlite3_context *pContext, /* Context for sqlite3_result_xxx() calls */
! 121863: int iCol /* Index of column to read value from */
! 121864: ){
! 121865: Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
! 121866:
! 121867: assert( p->isEof==0 );
! 121868: if( iCol==0 ){ /* Column "term" */
! 121869: sqlite3_result_text(pContext, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
! 121870: }else if( iCol==1 ){ /* Column "col" */
! 121871: if( p->iCol ){
! 121872: sqlite3_result_int(pContext, p->iCol-1);
! 121873: }else{
! 121874: sqlite3_result_text(pContext, "*", -1, SQLITE_STATIC);
! 121875: }
! 121876: }else if( iCol==2 ){ /* Column "documents" */
! 121877: sqlite3_result_int64(pContext, p->aStat[p->iCol].nDoc);
! 121878: }else{ /* Column "occurrences" */
! 121879: sqlite3_result_int64(pContext, p->aStat[p->iCol].nOcc);
! 121880: }
! 121881:
! 121882: return SQLITE_OK;
! 121883: }
! 121884:
! 121885: /*
! 121886: ** xRowid - Return the current rowid for the cursor.
! 121887: */
! 121888: static int fts3auxRowidMethod(
! 121889: sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
! 121890: sqlite_int64 *pRowid /* OUT: Rowid value */
! 121891: ){
! 121892: Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
! 121893: *pRowid = pCsr->iRowid;
! 121894: return SQLITE_OK;
! 121895: }
! 121896:
! 121897: /*
! 121898: ** Register the fts3aux module with database connection db. Return SQLITE_OK
! 121899: ** if successful or an error code if sqlite3_create_module() fails.
! 121900: */
! 121901: SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
! 121902: static const sqlite3_module fts3aux_module = {
! 121903: 0, /* iVersion */
! 121904: fts3auxConnectMethod, /* xCreate */
! 121905: fts3auxConnectMethod, /* xConnect */
! 121906: fts3auxBestIndexMethod, /* xBestIndex */
! 121907: fts3auxDisconnectMethod, /* xDisconnect */
! 121908: fts3auxDisconnectMethod, /* xDestroy */
! 121909: fts3auxOpenMethod, /* xOpen */
! 121910: fts3auxCloseMethod, /* xClose */
! 121911: fts3auxFilterMethod, /* xFilter */
! 121912: fts3auxNextMethod, /* xNext */
! 121913: fts3auxEofMethod, /* xEof */
! 121914: fts3auxColumnMethod, /* xColumn */
! 121915: fts3auxRowidMethod, /* xRowid */
! 121916: 0, /* xUpdate */
! 121917: 0, /* xBegin */
! 121918: 0, /* xSync */
! 121919: 0, /* xCommit */
! 121920: 0, /* xRollback */
! 121921: 0, /* xFindFunction */
! 121922: 0, /* xRename */
! 121923: 0, /* xSavepoint */
! 121924: 0, /* xRelease */
! 121925: 0 /* xRollbackTo */
! 121926: };
! 121927: int rc; /* Return code */
! 121928:
! 121929: rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
! 121930: return rc;
! 121931: }
! 121932:
! 121933: #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
! 121934:
! 121935: /************** End of fts3_aux.c ********************************************/
! 121936: /************** Begin file fts3_expr.c ***************************************/
! 121937: /*
! 121938: ** 2008 Nov 28
! 121939: **
! 121940: ** The author disclaims copyright to this source code. In place of
! 121941: ** a legal notice, here is a blessing:
! 121942: **
! 121943: ** May you do good and not evil.
! 121944: ** May you find forgiveness for yourself and forgive others.
! 121945: ** May you share freely, never taking more than you give.
! 121946: **
! 121947: ******************************************************************************
! 121948: **
! 121949: ** This module contains code that implements a parser for fts3 query strings
! 121950: ** (the right-hand argument to the MATCH operator). Because the supported
! 121951: ** syntax is relatively simple, the whole tokenizer/parser system is
! 121952: ** hand-coded.
! 121953: */
! 121954: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
! 121955:
! 121956: /*
! 121957: ** By default, this module parses the legacy syntax that has been
! 121958: ** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
! 121959: ** is defined, then it uses the new syntax. The differences between
! 121960: ** the new and the old syntaxes are:
! 121961: **
! 121962: ** a) The new syntax supports parenthesis. The old does not.
! 121963: **
! 121964: ** b) The new syntax supports the AND and NOT operators. The old does not.
! 121965: **
! 121966: ** c) The old syntax supports the "-" token qualifier. This is not
! 121967: ** supported by the new syntax (it is replaced by the NOT operator).
! 121968: **
! 121969: ** d) When using the old syntax, the OR operator has a greater precedence
! 121970: ** than an implicit AND. When using the new, both implicity and explicit
! 121971: ** AND operators have a higher precedence than OR.
! 121972: **
! 121973: ** If compiled with SQLITE_TEST defined, then this module exports the
! 121974: ** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
! 121975: ** to zero causes the module to use the old syntax. If it is set to
! 121976: ** non-zero the new syntax is activated. This is so both syntaxes can
! 121977: ** be tested using a single build of testfixture.
! 121978: **
! 121979: ** The following describes the syntax supported by the fts3 MATCH
! 121980: ** operator in a similar format to that used by the lemon parser
! 121981: ** generator. This module does not use actually lemon, it uses a
! 121982: ** custom parser.
! 121983: **
! 121984: ** query ::= andexpr (OR andexpr)*.
! 121985: **
! 121986: ** andexpr ::= notexpr (AND? notexpr)*.
! 121987: **
! 121988: ** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
! 121989: ** notexpr ::= LP query RP.
! 121990: **
! 121991: ** nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
! 121992: **
! 121993: ** distance_opt ::= .
! 121994: ** distance_opt ::= / INTEGER.
! 121995: **
! 121996: ** phrase ::= TOKEN.
! 121997: ** phrase ::= COLUMN:TOKEN.
! 121998: ** phrase ::= "TOKEN TOKEN TOKEN...".
! 121999: */
! 122000:
! 122001: #ifdef SQLITE_TEST
! 122002: SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
! 122003: #else
! 122004: # ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
! 122005: # define sqlite3_fts3_enable_parentheses 1
! 122006: # else
! 122007: # define sqlite3_fts3_enable_parentheses 0
! 122008: # endif
! 122009: #endif
! 122010:
! 122011: /*
! 122012: ** Default span for NEAR operators.
! 122013: */
! 122014: #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
! 122015:
! 122016: /* #include <string.h> */
! 122017: /* #include <assert.h> */
! 122018:
! 122019: /*
! 122020: ** isNot:
! 122021: ** This variable is used by function getNextNode(). When getNextNode() is
! 122022: ** called, it sets ParseContext.isNot to true if the 'next node' is a
! 122023: ** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
! 122024: ** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
! 122025: ** zero.
! 122026: */
! 122027: typedef struct ParseContext ParseContext;
! 122028: struct ParseContext {
! 122029: sqlite3_tokenizer *pTokenizer; /* Tokenizer module */
! 122030: const char **azCol; /* Array of column names for fts3 table */
! 122031: int bFts4; /* True to allow FTS4-only syntax */
! 122032: int nCol; /* Number of entries in azCol[] */
! 122033: int iDefaultCol; /* Default column to query */
! 122034: int isNot; /* True if getNextNode() sees a unary - */
! 122035: sqlite3_context *pCtx; /* Write error message here */
! 122036: int nNest; /* Number of nested brackets */
! 122037: };
! 122038:
! 122039: /*
! 122040: ** This function is equivalent to the standard isspace() function.
! 122041: **
! 122042: ** The standard isspace() can be awkward to use safely, because although it
! 122043: ** is defined to accept an argument of type int, its behaviour when passed
! 122044: ** an integer that falls outside of the range of the unsigned char type
! 122045: ** is undefined (and sometimes, "undefined" means segfault). This wrapper
! 122046: ** is defined to accept an argument of type char, and always returns 0 for
! 122047: ** any values that fall outside of the range of the unsigned char type (i.e.
! 122048: ** negative values).
! 122049: */
! 122050: static int fts3isspace(char c){
! 122051: return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
! 122052: }
! 122053:
! 122054: /*
! 122055: ** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
! 122056: ** zero the memory before returning a pointer to it. If unsuccessful,
! 122057: ** return NULL.
! 122058: */
! 122059: static void *fts3MallocZero(int nByte){
! 122060: void *pRet = sqlite3_malloc(nByte);
! 122061: if( pRet ) memset(pRet, 0, nByte);
! 122062: return pRet;
! 122063: }
! 122064:
! 122065:
! 122066: /*
! 122067: ** Extract the next token from buffer z (length n) using the tokenizer
! 122068: ** and other information (column names etc.) in pParse. Create an Fts3Expr
! 122069: ** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
! 122070: ** single token and set *ppExpr to point to it. If the end of the buffer is
! 122071: ** reached before a token is found, set *ppExpr to zero. It is the
! 122072: ** responsibility of the caller to eventually deallocate the allocated
! 122073: ** Fts3Expr structure (if any) by passing it to sqlite3_free().
! 122074: **
! 122075: ** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
! 122076: ** fails.
! 122077: */
! 122078: static int getNextToken(
! 122079: ParseContext *pParse, /* fts3 query parse context */
! 122080: int iCol, /* Value for Fts3Phrase.iColumn */
! 122081: const char *z, int n, /* Input string */
! 122082: Fts3Expr **ppExpr, /* OUT: expression */
! 122083: int *pnConsumed /* OUT: Number of bytes consumed */
! 122084: ){
! 122085: sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
! 122086: sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
! 122087: int rc;
! 122088: sqlite3_tokenizer_cursor *pCursor;
! 122089: Fts3Expr *pRet = 0;
! 122090: int nConsumed = 0;
! 122091:
! 122092: rc = pModule->xOpen(pTokenizer, z, n, &pCursor);
! 122093: if( rc==SQLITE_OK ){
! 122094: const char *zToken;
! 122095: int nToken, iStart, iEnd, iPosition;
! 122096: int nByte; /* total space to allocate */
! 122097:
! 122098: pCursor->pTokenizer = pTokenizer;
! 122099: rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
! 122100:
! 122101: if( rc==SQLITE_OK ){
! 122102: nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
! 122103: pRet = (Fts3Expr *)fts3MallocZero(nByte);
! 122104: if( !pRet ){
! 122105: rc = SQLITE_NOMEM;
! 122106: }else{
! 122107: pRet->eType = FTSQUERY_PHRASE;
! 122108: pRet->pPhrase = (Fts3Phrase *)&pRet[1];
! 122109: pRet->pPhrase->nToken = 1;
! 122110: pRet->pPhrase->iColumn = iCol;
! 122111: pRet->pPhrase->aToken[0].n = nToken;
! 122112: pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
! 122113: memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
! 122114:
! 122115: if( iEnd<n && z[iEnd]=='*' ){
! 122116: pRet->pPhrase->aToken[0].isPrefix = 1;
! 122117: iEnd++;
! 122118: }
! 122119:
! 122120: while( 1 ){
! 122121: if( !sqlite3_fts3_enable_parentheses
! 122122: && iStart>0 && z[iStart-1]=='-'
! 122123: ){
! 122124: pParse->isNot = 1;
! 122125: iStart--;
! 122126: }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
! 122127: pRet->pPhrase->aToken[0].bFirst = 1;
! 122128: iStart--;
! 122129: }else{
! 122130: break;
! 122131: }
! 122132: }
! 122133:
! 122134: }
! 122135: nConsumed = iEnd;
! 122136: }
! 122137:
! 122138: pModule->xClose(pCursor);
! 122139: }
! 122140:
! 122141: *pnConsumed = nConsumed;
! 122142: *ppExpr = pRet;
! 122143: return rc;
! 122144: }
! 122145:
! 122146:
! 122147: /*
! 122148: ** Enlarge a memory allocation. If an out-of-memory allocation occurs,
! 122149: ** then free the old allocation.
! 122150: */
! 122151: static void *fts3ReallocOrFree(void *pOrig, int nNew){
! 122152: void *pRet = sqlite3_realloc(pOrig, nNew);
! 122153: if( !pRet ){
! 122154: sqlite3_free(pOrig);
! 122155: }
! 122156: return pRet;
! 122157: }
! 122158:
! 122159: /*
! 122160: ** Buffer zInput, length nInput, contains the contents of a quoted string
! 122161: ** that appeared as part of an fts3 query expression. Neither quote character
! 122162: ** is included in the buffer. This function attempts to tokenize the entire
! 122163: ** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
! 122164: ** containing the results.
! 122165: **
! 122166: ** If successful, SQLITE_OK is returned and *ppExpr set to point at the
! 122167: ** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
! 122168: ** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
! 122169: ** to 0.
! 122170: */
! 122171: static int getNextString(
! 122172: ParseContext *pParse, /* fts3 query parse context */
! 122173: const char *zInput, int nInput, /* Input string */
! 122174: Fts3Expr **ppExpr /* OUT: expression */
! 122175: ){
! 122176: sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
! 122177: sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
! 122178: int rc;
! 122179: Fts3Expr *p = 0;
! 122180: sqlite3_tokenizer_cursor *pCursor = 0;
! 122181: char *zTemp = 0;
! 122182: int nTemp = 0;
! 122183:
! 122184: const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
! 122185: int nToken = 0;
! 122186:
! 122187: /* The final Fts3Expr data structure, including the Fts3Phrase,
! 122188: ** Fts3PhraseToken structures token buffers are all stored as a single
! 122189: ** allocation so that the expression can be freed with a single call to
! 122190: ** sqlite3_free(). Setting this up requires a two pass approach.
! 122191: **
! 122192: ** The first pass, in the block below, uses a tokenizer cursor to iterate
! 122193: ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
! 122194: ** to assemble data in two dynamic buffers:
! 122195: **
! 122196: ** Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
! 122197: ** structure, followed by the array of Fts3PhraseToken
! 122198: ** structures. This pass only populates the Fts3PhraseToken array.
! 122199: **
! 122200: ** Buffer zTemp: Contains copies of all tokens.
! 122201: **
! 122202: ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
! 122203: ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
! 122204: ** structures.
! 122205: */
! 122206: rc = pModule->xOpen(pTokenizer, zInput, nInput, &pCursor);
! 122207: if( rc==SQLITE_OK ){
! 122208: int ii;
! 122209: pCursor->pTokenizer = pTokenizer;
! 122210: for(ii=0; rc==SQLITE_OK; ii++){
! 122211: const char *zByte;
! 122212: int nByte, iBegin, iEnd, iPos;
! 122213: rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
! 122214: if( rc==SQLITE_OK ){
! 122215: Fts3PhraseToken *pToken;
! 122216:
! 122217: p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
! 122218: if( !p ) goto no_mem;
! 122219:
! 122220: zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
! 122221: if( !zTemp ) goto no_mem;
! 122222:
! 122223: assert( nToken==ii );
! 122224: pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
! 122225: memset(pToken, 0, sizeof(Fts3PhraseToken));
! 122226:
! 122227: memcpy(&zTemp[nTemp], zByte, nByte);
! 122228: nTemp += nByte;
! 122229:
! 122230: pToken->n = nByte;
! 122231: pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
! 122232: pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
! 122233: nToken = ii+1;
! 122234: }
! 122235: }
! 122236:
! 122237: pModule->xClose(pCursor);
! 122238: pCursor = 0;
! 122239: }
! 122240:
! 122241: if( rc==SQLITE_DONE ){
! 122242: int jj;
! 122243: char *zBuf = 0;
! 122244:
! 122245: p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
! 122246: if( !p ) goto no_mem;
! 122247: memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
! 122248: p->eType = FTSQUERY_PHRASE;
! 122249: p->pPhrase = (Fts3Phrase *)&p[1];
! 122250: p->pPhrase->iColumn = pParse->iDefaultCol;
! 122251: p->pPhrase->nToken = nToken;
! 122252:
! 122253: zBuf = (char *)&p->pPhrase->aToken[nToken];
! 122254: if( zTemp ){
! 122255: memcpy(zBuf, zTemp, nTemp);
! 122256: sqlite3_free(zTemp);
! 122257: }else{
! 122258: assert( nTemp==0 );
! 122259: }
! 122260:
! 122261: for(jj=0; jj<p->pPhrase->nToken; jj++){
! 122262: p->pPhrase->aToken[jj].z = zBuf;
! 122263: zBuf += p->pPhrase->aToken[jj].n;
! 122264: }
! 122265: rc = SQLITE_OK;
! 122266: }
! 122267:
! 122268: *ppExpr = p;
! 122269: return rc;
! 122270: no_mem:
! 122271:
! 122272: if( pCursor ){
! 122273: pModule->xClose(pCursor);
! 122274: }
! 122275: sqlite3_free(zTemp);
! 122276: sqlite3_free(p);
! 122277: *ppExpr = 0;
! 122278: return SQLITE_NOMEM;
! 122279: }
! 122280:
! 122281: /*
! 122282: ** Function getNextNode(), which is called by fts3ExprParse(), may itself
! 122283: ** call fts3ExprParse(). So this forward declaration is required.
! 122284: */
! 122285: static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
! 122286:
! 122287: /*
! 122288: ** The output variable *ppExpr is populated with an allocated Fts3Expr
! 122289: ** structure, or set to 0 if the end of the input buffer is reached.
! 122290: **
! 122291: ** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
! 122292: ** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
! 122293: ** If SQLITE_ERROR is returned, pContext is populated with an error message.
! 122294: */
! 122295: static int getNextNode(
! 122296: ParseContext *pParse, /* fts3 query parse context */
! 122297: const char *z, int n, /* Input string */
! 122298: Fts3Expr **ppExpr, /* OUT: expression */
! 122299: int *pnConsumed /* OUT: Number of bytes consumed */
! 122300: ){
! 122301: static const struct Fts3Keyword {
! 122302: char *z; /* Keyword text */
! 122303: unsigned char n; /* Length of the keyword */
! 122304: unsigned char parenOnly; /* Only valid in paren mode */
! 122305: unsigned char eType; /* Keyword code */
! 122306: } aKeyword[] = {
! 122307: { "OR" , 2, 0, FTSQUERY_OR },
! 122308: { "AND", 3, 1, FTSQUERY_AND },
! 122309: { "NOT", 3, 1, FTSQUERY_NOT },
! 122310: { "NEAR", 4, 0, FTSQUERY_NEAR }
! 122311: };
! 122312: int ii;
! 122313: int iCol;
! 122314: int iColLen;
! 122315: int rc;
! 122316: Fts3Expr *pRet = 0;
! 122317:
! 122318: const char *zInput = z;
! 122319: int nInput = n;
! 122320:
! 122321: pParse->isNot = 0;
! 122322:
! 122323: /* Skip over any whitespace before checking for a keyword, an open or
! 122324: ** close bracket, or a quoted string.
! 122325: */
! 122326: while( nInput>0 && fts3isspace(*zInput) ){
! 122327: nInput--;
! 122328: zInput++;
! 122329: }
! 122330: if( nInput==0 ){
! 122331: return SQLITE_DONE;
! 122332: }
! 122333:
! 122334: /* See if we are dealing with a keyword. */
! 122335: for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
! 122336: const struct Fts3Keyword *pKey = &aKeyword[ii];
! 122337:
! 122338: if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
! 122339: continue;
! 122340: }
! 122341:
! 122342: if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
! 122343: int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
! 122344: int nKey = pKey->n;
! 122345: char cNext;
! 122346:
! 122347: /* If this is a "NEAR" keyword, check for an explicit nearness. */
! 122348: if( pKey->eType==FTSQUERY_NEAR ){
! 122349: assert( nKey==4 );
! 122350: if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
! 122351: nNear = 0;
! 122352: for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
! 122353: nNear = nNear * 10 + (zInput[nKey] - '0');
! 122354: }
! 122355: }
! 122356: }
! 122357:
! 122358: /* At this point this is probably a keyword. But for that to be true,
! 122359: ** the next byte must contain either whitespace, an open or close
! 122360: ** parenthesis, a quote character, or EOF.
! 122361: */
! 122362: cNext = zInput[nKey];
! 122363: if( fts3isspace(cNext)
! 122364: || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
! 122365: ){
! 122366: pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
! 122367: if( !pRet ){
! 122368: return SQLITE_NOMEM;
! 122369: }
! 122370: pRet->eType = pKey->eType;
! 122371: pRet->nNear = nNear;
! 122372: *ppExpr = pRet;
! 122373: *pnConsumed = (int)((zInput - z) + nKey);
! 122374: return SQLITE_OK;
! 122375: }
! 122376:
! 122377: /* Turns out that wasn't a keyword after all. This happens if the
! 122378: ** user has supplied a token such as "ORacle". Continue.
! 122379: */
! 122380: }
! 122381: }
! 122382:
! 122383: /* Check for an open bracket. */
! 122384: if( sqlite3_fts3_enable_parentheses ){
! 122385: if( *zInput=='(' ){
! 122386: int nConsumed;
! 122387: pParse->nNest++;
! 122388: rc = fts3ExprParse(pParse, &zInput[1], nInput-1, ppExpr, &nConsumed);
! 122389: if( rc==SQLITE_OK && !*ppExpr ){
! 122390: rc = SQLITE_DONE;
! 122391: }
! 122392: *pnConsumed = (int)((zInput - z) + 1 + nConsumed);
! 122393: return rc;
! 122394: }
! 122395:
! 122396: /* Check for a close bracket. */
! 122397: if( *zInput==')' ){
! 122398: pParse->nNest--;
! 122399: *pnConsumed = (int)((zInput - z) + 1);
! 122400: return SQLITE_DONE;
! 122401: }
! 122402: }
! 122403:
! 122404: /* See if we are dealing with a quoted phrase. If this is the case, then
! 122405: ** search for the closing quote and pass the whole string to getNextString()
! 122406: ** for processing. This is easy to do, as fts3 has no syntax for escaping
! 122407: ** a quote character embedded in a string.
! 122408: */
! 122409: if( *zInput=='"' ){
! 122410: for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
! 122411: *pnConsumed = (int)((zInput - z) + ii + 1);
! 122412: if( ii==nInput ){
! 122413: return SQLITE_ERROR;
! 122414: }
! 122415: return getNextString(pParse, &zInput[1], ii-1, ppExpr);
! 122416: }
! 122417:
! 122418:
! 122419: /* If control flows to this point, this must be a regular token, or
! 122420: ** the end of the input. Read a regular token using the sqlite3_tokenizer
! 122421: ** interface. Before doing so, figure out if there is an explicit
! 122422: ** column specifier for the token.
! 122423: **
! 122424: ** TODO: Strangely, it is not possible to associate a column specifier
! 122425: ** with a quoted phrase, only with a single token. Not sure if this was
! 122426: ** an implementation artifact or an intentional decision when fts3 was
! 122427: ** first implemented. Whichever it was, this module duplicates the
! 122428: ** limitation.
! 122429: */
! 122430: iCol = pParse->iDefaultCol;
! 122431: iColLen = 0;
! 122432: for(ii=0; ii<pParse->nCol; ii++){
! 122433: const char *zStr = pParse->azCol[ii];
! 122434: int nStr = (int)strlen(zStr);
! 122435: if( nInput>nStr && zInput[nStr]==':'
! 122436: && sqlite3_strnicmp(zStr, zInput, nStr)==0
! 122437: ){
! 122438: iCol = ii;
! 122439: iColLen = (int)((zInput - z) + nStr + 1);
! 122440: break;
! 122441: }
! 122442: }
! 122443: rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
! 122444: *pnConsumed += iColLen;
! 122445: return rc;
! 122446: }
! 122447:
! 122448: /*
! 122449: ** The argument is an Fts3Expr structure for a binary operator (any type
! 122450: ** except an FTSQUERY_PHRASE). Return an integer value representing the
! 122451: ** precedence of the operator. Lower values have a higher precedence (i.e.
! 122452: ** group more tightly). For example, in the C language, the == operator
! 122453: ** groups more tightly than ||, and would therefore have a higher precedence.
! 122454: **
! 122455: ** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
! 122456: ** is defined), the order of the operators in precedence from highest to
! 122457: ** lowest is:
! 122458: **
! 122459: ** NEAR
! 122460: ** NOT
! 122461: ** AND (including implicit ANDs)
! 122462: ** OR
! 122463: **
! 122464: ** Note that when using the old query syntax, the OR operator has a higher
! 122465: ** precedence than the AND operator.
! 122466: */
! 122467: static int opPrecedence(Fts3Expr *p){
! 122468: assert( p->eType!=FTSQUERY_PHRASE );
! 122469: if( sqlite3_fts3_enable_parentheses ){
! 122470: return p->eType;
! 122471: }else if( p->eType==FTSQUERY_NEAR ){
! 122472: return 1;
! 122473: }else if( p->eType==FTSQUERY_OR ){
! 122474: return 2;
! 122475: }
! 122476: assert( p->eType==FTSQUERY_AND );
! 122477: return 3;
! 122478: }
! 122479:
! 122480: /*
! 122481: ** Argument ppHead contains a pointer to the current head of a query
! 122482: ** expression tree being parsed. pPrev is the expression node most recently
! 122483: ** inserted into the tree. This function adds pNew, which is always a binary
! 122484: ** operator node, into the expression tree based on the relative precedence
! 122485: ** of pNew and the existing nodes of the tree. This may result in the head
! 122486: ** of the tree changing, in which case *ppHead is set to the new root node.
! 122487: */
! 122488: static void insertBinaryOperator(
! 122489: Fts3Expr **ppHead, /* Pointer to the root node of a tree */
! 122490: Fts3Expr *pPrev, /* Node most recently inserted into the tree */
! 122491: Fts3Expr *pNew /* New binary node to insert into expression tree */
! 122492: ){
! 122493: Fts3Expr *pSplit = pPrev;
! 122494: while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
! 122495: pSplit = pSplit->pParent;
! 122496: }
! 122497:
! 122498: if( pSplit->pParent ){
! 122499: assert( pSplit->pParent->pRight==pSplit );
! 122500: pSplit->pParent->pRight = pNew;
! 122501: pNew->pParent = pSplit->pParent;
! 122502: }else{
! 122503: *ppHead = pNew;
! 122504: }
! 122505: pNew->pLeft = pSplit;
! 122506: pSplit->pParent = pNew;
! 122507: }
! 122508:
! 122509: /*
! 122510: ** Parse the fts3 query expression found in buffer z, length n. This function
! 122511: ** returns either when the end of the buffer is reached or an unmatched
! 122512: ** closing bracket - ')' - is encountered.
! 122513: **
! 122514: ** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
! 122515: ** parsed form of the expression and *pnConsumed is set to the number of
! 122516: ** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
! 122517: ** (out of memory error) or SQLITE_ERROR (parse error) is returned.
! 122518: */
! 122519: static int fts3ExprParse(
! 122520: ParseContext *pParse, /* fts3 query parse context */
! 122521: const char *z, int n, /* Text of MATCH query */
! 122522: Fts3Expr **ppExpr, /* OUT: Parsed query structure */
! 122523: int *pnConsumed /* OUT: Number of bytes consumed */
! 122524: ){
! 122525: Fts3Expr *pRet = 0;
! 122526: Fts3Expr *pPrev = 0;
! 122527: Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */
! 122528: int nIn = n;
! 122529: const char *zIn = z;
! 122530: int rc = SQLITE_OK;
! 122531: int isRequirePhrase = 1;
! 122532:
! 122533: while( rc==SQLITE_OK ){
! 122534: Fts3Expr *p = 0;
! 122535: int nByte = 0;
! 122536: rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
! 122537: if( rc==SQLITE_OK ){
! 122538: int isPhrase;
! 122539:
! 122540: if( !sqlite3_fts3_enable_parentheses
! 122541: && p->eType==FTSQUERY_PHRASE && pParse->isNot
! 122542: ){
! 122543: /* Create an implicit NOT operator. */
! 122544: Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
! 122545: if( !pNot ){
! 122546: sqlite3Fts3ExprFree(p);
! 122547: rc = SQLITE_NOMEM;
! 122548: goto exprparse_out;
! 122549: }
! 122550: pNot->eType = FTSQUERY_NOT;
! 122551: pNot->pRight = p;
! 122552: if( pNotBranch ){
! 122553: pNot->pLeft = pNotBranch;
! 122554: }
! 122555: pNotBranch = pNot;
! 122556: p = pPrev;
! 122557: }else{
! 122558: int eType = p->eType;
! 122559: isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
! 122560:
! 122561: /* The isRequirePhrase variable is set to true if a phrase or
! 122562: ** an expression contained in parenthesis is required. If a
! 122563: ** binary operator (AND, OR, NOT or NEAR) is encounted when
! 122564: ** isRequirePhrase is set, this is a syntax error.
! 122565: */
! 122566: if( !isPhrase && isRequirePhrase ){
! 122567: sqlite3Fts3ExprFree(p);
! 122568: rc = SQLITE_ERROR;
! 122569: goto exprparse_out;
! 122570: }
! 122571:
! 122572: if( isPhrase && !isRequirePhrase ){
! 122573: /* Insert an implicit AND operator. */
! 122574: Fts3Expr *pAnd;
! 122575: assert( pRet && pPrev );
! 122576: pAnd = fts3MallocZero(sizeof(Fts3Expr));
! 122577: if( !pAnd ){
! 122578: sqlite3Fts3ExprFree(p);
! 122579: rc = SQLITE_NOMEM;
! 122580: goto exprparse_out;
! 122581: }
! 122582: pAnd->eType = FTSQUERY_AND;
! 122583: insertBinaryOperator(&pRet, pPrev, pAnd);
! 122584: pPrev = pAnd;
! 122585: }
! 122586:
! 122587: /* This test catches attempts to make either operand of a NEAR
! 122588: ** operator something other than a phrase. For example, either of
! 122589: ** the following:
! 122590: **
! 122591: ** (bracketed expression) NEAR phrase
! 122592: ** phrase NEAR (bracketed expression)
! 122593: **
! 122594: ** Return an error in either case.
! 122595: */
! 122596: if( pPrev && (
! 122597: (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
! 122598: || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
! 122599: )){
! 122600: sqlite3Fts3ExprFree(p);
! 122601: rc = SQLITE_ERROR;
! 122602: goto exprparse_out;
! 122603: }
! 122604:
! 122605: if( isPhrase ){
! 122606: if( pRet ){
! 122607: assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
! 122608: pPrev->pRight = p;
! 122609: p->pParent = pPrev;
! 122610: }else{
! 122611: pRet = p;
! 122612: }
! 122613: }else{
! 122614: insertBinaryOperator(&pRet, pPrev, p);
! 122615: }
! 122616: isRequirePhrase = !isPhrase;
! 122617: }
! 122618: assert( nByte>0 );
! 122619: }
! 122620: assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
! 122621: nIn -= nByte;
! 122622: zIn += nByte;
! 122623: pPrev = p;
! 122624: }
! 122625:
! 122626: if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
! 122627: rc = SQLITE_ERROR;
! 122628: }
! 122629:
! 122630: if( rc==SQLITE_DONE ){
! 122631: rc = SQLITE_OK;
! 122632: if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
! 122633: if( !pRet ){
! 122634: rc = SQLITE_ERROR;
! 122635: }else{
! 122636: Fts3Expr *pIter = pNotBranch;
! 122637: while( pIter->pLeft ){
! 122638: pIter = pIter->pLeft;
! 122639: }
! 122640: pIter->pLeft = pRet;
! 122641: pRet = pNotBranch;
! 122642: }
! 122643: }
! 122644: }
! 122645: *pnConsumed = n - nIn;
! 122646:
! 122647: exprparse_out:
! 122648: if( rc!=SQLITE_OK ){
! 122649: sqlite3Fts3ExprFree(pRet);
! 122650: sqlite3Fts3ExprFree(pNotBranch);
! 122651: pRet = 0;
! 122652: }
! 122653: *ppExpr = pRet;
! 122654: return rc;
! 122655: }
! 122656:
! 122657: /*
! 122658: ** Parameters z and n contain a pointer to and length of a buffer containing
! 122659: ** an fts3 query expression, respectively. This function attempts to parse the
! 122660: ** query expression and create a tree of Fts3Expr structures representing the
! 122661: ** parsed expression. If successful, *ppExpr is set to point to the head
! 122662: ** of the parsed expression tree and SQLITE_OK is returned. If an error
! 122663: ** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
! 122664: ** error) is returned and *ppExpr is set to 0.
! 122665: **
! 122666: ** If parameter n is a negative number, then z is assumed to point to a
! 122667: ** nul-terminated string and the length is determined using strlen().
! 122668: **
! 122669: ** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
! 122670: ** use to normalize query tokens while parsing the expression. The azCol[]
! 122671: ** array, which is assumed to contain nCol entries, should contain the names
! 122672: ** of each column in the target fts3 table, in order from left to right.
! 122673: ** Column names must be nul-terminated strings.
! 122674: **
! 122675: ** The iDefaultCol parameter should be passed the index of the table column
! 122676: ** that appears on the left-hand-side of the MATCH operator (the default
! 122677: ** column to match against for tokens for which a column name is not explicitly
! 122678: ** specified as part of the query string), or -1 if tokens may by default
! 122679: ** match any table column.
! 122680: */
! 122681: SQLITE_PRIVATE int sqlite3Fts3ExprParse(
! 122682: sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
! 122683: char **azCol, /* Array of column names for fts3 table */
! 122684: int bFts4, /* True to allow FTS4-only syntax */
! 122685: int nCol, /* Number of entries in azCol[] */
! 122686: int iDefaultCol, /* Default column to query */
! 122687: const char *z, int n, /* Text of MATCH query */
! 122688: Fts3Expr **ppExpr /* OUT: Parsed query structure */
! 122689: ){
! 122690: int nParsed;
! 122691: int rc;
! 122692: ParseContext sParse;
! 122693: sParse.pTokenizer = pTokenizer;
! 122694: sParse.azCol = (const char **)azCol;
! 122695: sParse.nCol = nCol;
! 122696: sParse.iDefaultCol = iDefaultCol;
! 122697: sParse.nNest = 0;
! 122698: sParse.bFts4 = bFts4;
! 122699: if( z==0 ){
! 122700: *ppExpr = 0;
! 122701: return SQLITE_OK;
! 122702: }
! 122703: if( n<0 ){
! 122704: n = (int)strlen(z);
! 122705: }
! 122706: rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
! 122707:
! 122708: /* Check for mismatched parenthesis */
! 122709: if( rc==SQLITE_OK && sParse.nNest ){
! 122710: rc = SQLITE_ERROR;
! 122711: sqlite3Fts3ExprFree(*ppExpr);
! 122712: *ppExpr = 0;
! 122713: }
! 122714:
! 122715: return rc;
! 122716: }
! 122717:
! 122718: /*
! 122719: ** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
! 122720: */
! 122721: SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *p){
! 122722: if( p ){
! 122723: assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
! 122724: sqlite3Fts3ExprFree(p->pLeft);
! 122725: sqlite3Fts3ExprFree(p->pRight);
! 122726: sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
! 122727: sqlite3_free(p->aMI);
! 122728: sqlite3_free(p);
! 122729: }
! 122730: }
! 122731:
! 122732: /****************************************************************************
! 122733: *****************************************************************************
! 122734: ** Everything after this point is just test code.
! 122735: */
! 122736:
! 122737: #ifdef SQLITE_TEST
! 122738:
! 122739: /* #include <stdio.h> */
! 122740:
! 122741: /*
! 122742: ** Function to query the hash-table of tokenizers (see README.tokenizers).
! 122743: */
! 122744: static int queryTestTokenizer(
! 122745: sqlite3 *db,
! 122746: const char *zName,
! 122747: const sqlite3_tokenizer_module **pp
! 122748: ){
! 122749: int rc;
! 122750: sqlite3_stmt *pStmt;
! 122751: const char zSql[] = "SELECT fts3_tokenizer(?)";
! 122752:
! 122753: *pp = 0;
! 122754: rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
! 122755: if( rc!=SQLITE_OK ){
! 122756: return rc;
! 122757: }
! 122758:
! 122759: sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
! 122760: if( SQLITE_ROW==sqlite3_step(pStmt) ){
! 122761: if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
! 122762: memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
! 122763: }
! 122764: }
! 122765:
! 122766: return sqlite3_finalize(pStmt);
! 122767: }
! 122768:
! 122769: /*
! 122770: ** Return a pointer to a buffer containing a text representation of the
! 122771: ** expression passed as the first argument. The buffer is obtained from
! 122772: ** sqlite3_malloc(). It is the responsibility of the caller to use
! 122773: ** sqlite3_free() to release the memory. If an OOM condition is encountered,
! 122774: ** NULL is returned.
! 122775: **
! 122776: ** If the second argument is not NULL, then its contents are prepended to
! 122777: ** the returned expression text and then freed using sqlite3_free().
! 122778: */
! 122779: static char *exprToString(Fts3Expr *pExpr, char *zBuf){
! 122780: switch( pExpr->eType ){
! 122781: case FTSQUERY_PHRASE: {
! 122782: Fts3Phrase *pPhrase = pExpr->pPhrase;
! 122783: int i;
! 122784: zBuf = sqlite3_mprintf(
! 122785: "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
! 122786: for(i=0; zBuf && i<pPhrase->nToken; i++){
! 122787: zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
! 122788: pPhrase->aToken[i].n, pPhrase->aToken[i].z,
! 122789: (pPhrase->aToken[i].isPrefix?"+":"")
! 122790: );
! 122791: }
! 122792: return zBuf;
! 122793: }
! 122794:
! 122795: case FTSQUERY_NEAR:
! 122796: zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
! 122797: break;
! 122798: case FTSQUERY_NOT:
! 122799: zBuf = sqlite3_mprintf("%zNOT ", zBuf);
! 122800: break;
! 122801: case FTSQUERY_AND:
! 122802: zBuf = sqlite3_mprintf("%zAND ", zBuf);
! 122803: break;
! 122804: case FTSQUERY_OR:
! 122805: zBuf = sqlite3_mprintf("%zOR ", zBuf);
! 122806: break;
! 122807: }
! 122808:
! 122809: if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
! 122810: if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
! 122811: if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
! 122812:
! 122813: if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
! 122814: if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
! 122815:
! 122816: return zBuf;
! 122817: }
! 122818:
! 122819: /*
! 122820: ** This is the implementation of a scalar SQL function used to test the
! 122821: ** expression parser. It should be called as follows:
! 122822: **
! 122823: ** fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
! 122824: **
! 122825: ** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
! 122826: ** to parse the query expression (see README.tokenizers). The second argument
! 122827: ** is the query expression to parse. Each subsequent argument is the name
! 122828: ** of a column of the fts3 table that the query expression may refer to.
! 122829: ** For example:
! 122830: **
! 122831: ** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
! 122832: */
! 122833: static void fts3ExprTest(
! 122834: sqlite3_context *context,
! 122835: int argc,
! 122836: sqlite3_value **argv
! 122837: ){
! 122838: sqlite3_tokenizer_module const *pModule = 0;
! 122839: sqlite3_tokenizer *pTokenizer = 0;
! 122840: int rc;
! 122841: char **azCol = 0;
! 122842: const char *zExpr;
! 122843: int nExpr;
! 122844: int nCol;
! 122845: int ii;
! 122846: Fts3Expr *pExpr;
! 122847: char *zBuf = 0;
! 122848: sqlite3 *db = sqlite3_context_db_handle(context);
! 122849:
! 122850: if( argc<3 ){
! 122851: sqlite3_result_error(context,
! 122852: "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
! 122853: );
! 122854: return;
! 122855: }
! 122856:
! 122857: rc = queryTestTokenizer(db,
! 122858: (const char *)sqlite3_value_text(argv[0]), &pModule);
! 122859: if( rc==SQLITE_NOMEM ){
! 122860: sqlite3_result_error_nomem(context);
! 122861: goto exprtest_out;
! 122862: }else if( !pModule ){
! 122863: sqlite3_result_error(context, "No such tokenizer module", -1);
! 122864: goto exprtest_out;
! 122865: }
! 122866:
! 122867: rc = pModule->xCreate(0, 0, &pTokenizer);
! 122868: assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
! 122869: if( rc==SQLITE_NOMEM ){
! 122870: sqlite3_result_error_nomem(context);
! 122871: goto exprtest_out;
! 122872: }
! 122873: pTokenizer->pModule = pModule;
! 122874:
! 122875: zExpr = (const char *)sqlite3_value_text(argv[1]);
! 122876: nExpr = sqlite3_value_bytes(argv[1]);
! 122877: nCol = argc-2;
! 122878: azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
! 122879: if( !azCol ){
! 122880: sqlite3_result_error_nomem(context);
! 122881: goto exprtest_out;
! 122882: }
! 122883: for(ii=0; ii<nCol; ii++){
! 122884: azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
! 122885: }
! 122886:
! 122887: rc = sqlite3Fts3ExprParse(
! 122888: pTokenizer, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
! 122889: );
! 122890: if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
! 122891: sqlite3_result_error(context, "Error parsing expression", -1);
! 122892: }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
! 122893: sqlite3_result_error_nomem(context);
! 122894: }else{
! 122895: sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
! 122896: sqlite3_free(zBuf);
! 122897: }
! 122898:
! 122899: sqlite3Fts3ExprFree(pExpr);
! 122900:
! 122901: exprtest_out:
! 122902: if( pModule && pTokenizer ){
! 122903: rc = pModule->xDestroy(pTokenizer);
! 122904: }
! 122905: sqlite3_free(azCol);
! 122906: }
! 122907:
! 122908: /*
! 122909: ** Register the query expression parser test function fts3_exprtest()
! 122910: ** with database connection db.
! 122911: */
! 122912: SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
! 122913: return sqlite3_create_function(
! 122914: db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
! 122915: );
! 122916: }
! 122917:
! 122918: #endif
! 122919: #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
! 122920:
! 122921: /************** End of fts3_expr.c *******************************************/
! 122922: /************** Begin file fts3_hash.c ***************************************/
! 122923: /*
! 122924: ** 2001 September 22
! 122925: **
! 122926: ** The author disclaims copyright to this source code. In place of
! 122927: ** a legal notice, here is a blessing:
! 122928: **
! 122929: ** May you do good and not evil.
! 122930: ** May you find forgiveness for yourself and forgive others.
! 122931: ** May you share freely, never taking more than you give.
! 122932: **
! 122933: *************************************************************************
! 122934: ** This is the implementation of generic hash-tables used in SQLite.
! 122935: ** We've modified it slightly to serve as a standalone hash table
! 122936: ** implementation for the full-text indexing module.
! 122937: */
! 122938:
! 122939: /*
! 122940: ** The code in this file is only compiled if:
! 122941: **
! 122942: ** * The FTS3 module is being built as an extension
! 122943: ** (in which case SQLITE_CORE is not defined), or
! 122944: **
! 122945: ** * The FTS3 module is being built into the core of
! 122946: ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
! 122947: */
! 122948: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
! 122949:
! 122950: /* #include <assert.h> */
! 122951: /* #include <stdlib.h> */
! 122952: /* #include <string.h> */
! 122953:
! 122954:
! 122955: /*
! 122956: ** Malloc and Free functions
! 122957: */
! 122958: static void *fts3HashMalloc(int n){
! 122959: void *p = sqlite3_malloc(n);
! 122960: if( p ){
! 122961: memset(p, 0, n);
! 122962: }
! 122963: return p;
! 122964: }
! 122965: static void fts3HashFree(void *p){
! 122966: sqlite3_free(p);
! 122967: }
! 122968:
! 122969: /* Turn bulk memory into a hash table object by initializing the
! 122970: ** fields of the Hash structure.
! 122971: **
! 122972: ** "pNew" is a pointer to the hash table that is to be initialized.
! 122973: ** keyClass is one of the constants
! 122974: ** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass
! 122975: ** determines what kind of key the hash table will use. "copyKey" is
! 122976: ** true if the hash table should make its own private copy of keys and
! 122977: ** false if it should just use the supplied pointer.
! 122978: */
! 122979: SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
! 122980: assert( pNew!=0 );
! 122981: assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
! 122982: pNew->keyClass = keyClass;
! 122983: pNew->copyKey = copyKey;
! 122984: pNew->first = 0;
! 122985: pNew->count = 0;
! 122986: pNew->htsize = 0;
! 122987: pNew->ht = 0;
! 122988: }
! 122989:
! 122990: /* Remove all entries from a hash table. Reclaim all memory.
! 122991: ** Call this routine to delete a hash table or to reset a hash table
! 122992: ** to the empty state.
! 122993: */
! 122994: SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
! 122995: Fts3HashElem *elem; /* For looping over all elements of the table */
! 122996:
! 122997: assert( pH!=0 );
! 122998: elem = pH->first;
! 122999: pH->first = 0;
! 123000: fts3HashFree(pH->ht);
! 123001: pH->ht = 0;
! 123002: pH->htsize = 0;
! 123003: while( elem ){
! 123004: Fts3HashElem *next_elem = elem->next;
! 123005: if( pH->copyKey && elem->pKey ){
! 123006: fts3HashFree(elem->pKey);
! 123007: }
! 123008: fts3HashFree(elem);
! 123009: elem = next_elem;
! 123010: }
! 123011: pH->count = 0;
! 123012: }
! 123013:
! 123014: /*
! 123015: ** Hash and comparison functions when the mode is FTS3_HASH_STRING
! 123016: */
! 123017: static int fts3StrHash(const void *pKey, int nKey){
! 123018: const char *z = (const char *)pKey;
! 123019: int h = 0;
! 123020: if( nKey<=0 ) nKey = (int) strlen(z);
! 123021: while( nKey > 0 ){
! 123022: h = (h<<3) ^ h ^ *z++;
! 123023: nKey--;
! 123024: }
! 123025: return h & 0x7fffffff;
! 123026: }
! 123027: static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
! 123028: if( n1!=n2 ) return 1;
! 123029: return strncmp((const char*)pKey1,(const char*)pKey2,n1);
! 123030: }
! 123031:
! 123032: /*
! 123033: ** Hash and comparison functions when the mode is FTS3_HASH_BINARY
! 123034: */
! 123035: static int fts3BinHash(const void *pKey, int nKey){
! 123036: int h = 0;
! 123037: const char *z = (const char *)pKey;
! 123038: while( nKey-- > 0 ){
! 123039: h = (h<<3) ^ h ^ *(z++);
! 123040: }
! 123041: return h & 0x7fffffff;
! 123042: }
! 123043: static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
! 123044: if( n1!=n2 ) return 1;
! 123045: return memcmp(pKey1,pKey2,n1);
! 123046: }
! 123047:
! 123048: /*
! 123049: ** Return a pointer to the appropriate hash function given the key class.
! 123050: **
! 123051: ** The C syntax in this function definition may be unfamilar to some
! 123052: ** programmers, so we provide the following additional explanation:
! 123053: **
! 123054: ** The name of the function is "ftsHashFunction". The function takes a
! 123055: ** single parameter "keyClass". The return value of ftsHashFunction()
! 123056: ** is a pointer to another function. Specifically, the return value
! 123057: ** of ftsHashFunction() is a pointer to a function that takes two parameters
! 123058: ** with types "const void*" and "int" and returns an "int".
! 123059: */
! 123060: static int (*ftsHashFunction(int keyClass))(const void*,int){
! 123061: if( keyClass==FTS3_HASH_STRING ){
! 123062: return &fts3StrHash;
! 123063: }else{
! 123064: assert( keyClass==FTS3_HASH_BINARY );
! 123065: return &fts3BinHash;
! 123066: }
! 123067: }
! 123068:
! 123069: /*
! 123070: ** Return a pointer to the appropriate hash function given the key class.
! 123071: **
! 123072: ** For help in interpreted the obscure C code in the function definition,
! 123073: ** see the header comment on the previous function.
! 123074: */
! 123075: static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
! 123076: if( keyClass==FTS3_HASH_STRING ){
! 123077: return &fts3StrCompare;
! 123078: }else{
! 123079: assert( keyClass==FTS3_HASH_BINARY );
! 123080: return &fts3BinCompare;
! 123081: }
! 123082: }
! 123083:
! 123084: /* Link an element into the hash table
! 123085: */
! 123086: static void fts3HashInsertElement(
! 123087: Fts3Hash *pH, /* The complete hash table */
! 123088: struct _fts3ht *pEntry, /* The entry into which pNew is inserted */
! 123089: Fts3HashElem *pNew /* The element to be inserted */
! 123090: ){
! 123091: Fts3HashElem *pHead; /* First element already in pEntry */
! 123092: pHead = pEntry->chain;
! 123093: if( pHead ){
! 123094: pNew->next = pHead;
! 123095: pNew->prev = pHead->prev;
! 123096: if( pHead->prev ){ pHead->prev->next = pNew; }
! 123097: else { pH->first = pNew; }
! 123098: pHead->prev = pNew;
! 123099: }else{
! 123100: pNew->next = pH->first;
! 123101: if( pH->first ){ pH->first->prev = pNew; }
! 123102: pNew->prev = 0;
! 123103: pH->first = pNew;
! 123104: }
! 123105: pEntry->count++;
! 123106: pEntry->chain = pNew;
! 123107: }
! 123108:
! 123109:
! 123110: /* Resize the hash table so that it cantains "new_size" buckets.
! 123111: ** "new_size" must be a power of 2. The hash table might fail
! 123112: ** to resize if sqliteMalloc() fails.
! 123113: **
! 123114: ** Return non-zero if a memory allocation error occurs.
! 123115: */
! 123116: static int fts3Rehash(Fts3Hash *pH, int new_size){
! 123117: struct _fts3ht *new_ht; /* The new hash table */
! 123118: Fts3HashElem *elem, *next_elem; /* For looping over existing elements */
! 123119: int (*xHash)(const void*,int); /* The hash function */
! 123120:
! 123121: assert( (new_size & (new_size-1))==0 );
! 123122: new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
! 123123: if( new_ht==0 ) return 1;
! 123124: fts3HashFree(pH->ht);
! 123125: pH->ht = new_ht;
! 123126: pH->htsize = new_size;
! 123127: xHash = ftsHashFunction(pH->keyClass);
! 123128: for(elem=pH->first, pH->first=0; elem; elem = next_elem){
! 123129: int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
! 123130: next_elem = elem->next;
! 123131: fts3HashInsertElement(pH, &new_ht[h], elem);
! 123132: }
! 123133: return 0;
! 123134: }
! 123135:
! 123136: /* This function (for internal use only) locates an element in an
! 123137: ** hash table that matches the given key. The hash for this key has
! 123138: ** already been computed and is passed as the 4th parameter.
! 123139: */
! 123140: static Fts3HashElem *fts3FindElementByHash(
! 123141: const Fts3Hash *pH, /* The pH to be searched */
! 123142: const void *pKey, /* The key we are searching for */
! 123143: int nKey,
! 123144: int h /* The hash for this key. */
! 123145: ){
! 123146: Fts3HashElem *elem; /* Used to loop thru the element list */
! 123147: int count; /* Number of elements left to test */
! 123148: int (*xCompare)(const void*,int,const void*,int); /* comparison function */
! 123149:
! 123150: if( pH->ht ){
! 123151: struct _fts3ht *pEntry = &pH->ht[h];
! 123152: elem = pEntry->chain;
! 123153: count = pEntry->count;
! 123154: xCompare = ftsCompareFunction(pH->keyClass);
! 123155: while( count-- && elem ){
! 123156: if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
! 123157: return elem;
! 123158: }
! 123159: elem = elem->next;
! 123160: }
! 123161: }
! 123162: return 0;
! 123163: }
! 123164:
! 123165: /* Remove a single entry from the hash table given a pointer to that
! 123166: ** element and a hash on the element's key.
! 123167: */
! 123168: static void fts3RemoveElementByHash(
! 123169: Fts3Hash *pH, /* The pH containing "elem" */
! 123170: Fts3HashElem* elem, /* The element to be removed from the pH */
! 123171: int h /* Hash value for the element */
! 123172: ){
! 123173: struct _fts3ht *pEntry;
! 123174: if( elem->prev ){
! 123175: elem->prev->next = elem->next;
! 123176: }else{
! 123177: pH->first = elem->next;
! 123178: }
! 123179: if( elem->next ){
! 123180: elem->next->prev = elem->prev;
! 123181: }
! 123182: pEntry = &pH->ht[h];
! 123183: if( pEntry->chain==elem ){
! 123184: pEntry->chain = elem->next;
! 123185: }
! 123186: pEntry->count--;
! 123187: if( pEntry->count<=0 ){
! 123188: pEntry->chain = 0;
! 123189: }
! 123190: if( pH->copyKey && elem->pKey ){
! 123191: fts3HashFree(elem->pKey);
! 123192: }
! 123193: fts3HashFree( elem );
! 123194: pH->count--;
! 123195: if( pH->count<=0 ){
! 123196: assert( pH->first==0 );
! 123197: assert( pH->count==0 );
! 123198: fts3HashClear(pH);
! 123199: }
! 123200: }
! 123201:
! 123202: SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
! 123203: const Fts3Hash *pH,
! 123204: const void *pKey,
! 123205: int nKey
! 123206: ){
! 123207: int h; /* A hash on key */
! 123208: int (*xHash)(const void*,int); /* The hash function */
! 123209:
! 123210: if( pH==0 || pH->ht==0 ) return 0;
! 123211: xHash = ftsHashFunction(pH->keyClass);
! 123212: assert( xHash!=0 );
! 123213: h = (*xHash)(pKey,nKey);
! 123214: assert( (pH->htsize & (pH->htsize-1))==0 );
! 123215: return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
! 123216: }
! 123217:
! 123218: /*
! 123219: ** Attempt to locate an element of the hash table pH with a key
! 123220: ** that matches pKey,nKey. Return the data for this element if it is
! 123221: ** found, or NULL if there is no match.
! 123222: */
! 123223: SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
! 123224: Fts3HashElem *pElem; /* The element that matches key (if any) */
! 123225:
! 123226: pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
! 123227: return pElem ? pElem->data : 0;
! 123228: }
! 123229:
! 123230: /* Insert an element into the hash table pH. The key is pKey,nKey
! 123231: ** and the data is "data".
! 123232: **
! 123233: ** If no element exists with a matching key, then a new
! 123234: ** element is created. A copy of the key is made if the copyKey
! 123235: ** flag is set. NULL is returned.
! 123236: **
! 123237: ** If another element already exists with the same key, then the
! 123238: ** new data replaces the old data and the old data is returned.
! 123239: ** The key is not copied in this instance. If a malloc fails, then
! 123240: ** the new data is returned and the hash table is unchanged.
! 123241: **
! 123242: ** If the "data" parameter to this function is NULL, then the
! 123243: ** element corresponding to "key" is removed from the hash table.
! 123244: */
! 123245: SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
! 123246: Fts3Hash *pH, /* The hash table to insert into */
! 123247: const void *pKey, /* The key */
! 123248: int nKey, /* Number of bytes in the key */
! 123249: void *data /* The data */
! 123250: ){
! 123251: int hraw; /* Raw hash value of the key */
! 123252: int h; /* the hash of the key modulo hash table size */
! 123253: Fts3HashElem *elem; /* Used to loop thru the element list */
! 123254: Fts3HashElem *new_elem; /* New element added to the pH */
! 123255: int (*xHash)(const void*,int); /* The hash function */
! 123256:
! 123257: assert( pH!=0 );
! 123258: xHash = ftsHashFunction(pH->keyClass);
! 123259: assert( xHash!=0 );
! 123260: hraw = (*xHash)(pKey, nKey);
! 123261: assert( (pH->htsize & (pH->htsize-1))==0 );
! 123262: h = hraw & (pH->htsize-1);
! 123263: elem = fts3FindElementByHash(pH,pKey,nKey,h);
! 123264: if( elem ){
! 123265: void *old_data = elem->data;
! 123266: if( data==0 ){
! 123267: fts3RemoveElementByHash(pH,elem,h);
! 123268: }else{
! 123269: elem->data = data;
! 123270: }
! 123271: return old_data;
! 123272: }
! 123273: if( data==0 ) return 0;
! 123274: if( (pH->htsize==0 && fts3Rehash(pH,8))
! 123275: || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
! 123276: ){
! 123277: pH->count = 0;
! 123278: return data;
! 123279: }
! 123280: assert( pH->htsize>0 );
! 123281: new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
! 123282: if( new_elem==0 ) return data;
! 123283: if( pH->copyKey && pKey!=0 ){
! 123284: new_elem->pKey = fts3HashMalloc( nKey );
! 123285: if( new_elem->pKey==0 ){
! 123286: fts3HashFree(new_elem);
! 123287: return data;
! 123288: }
! 123289: memcpy((void*)new_elem->pKey, pKey, nKey);
! 123290: }else{
! 123291: new_elem->pKey = (void*)pKey;
! 123292: }
! 123293: new_elem->nKey = nKey;
! 123294: pH->count++;
! 123295: assert( pH->htsize>0 );
! 123296: assert( (pH->htsize & (pH->htsize-1))==0 );
! 123297: h = hraw & (pH->htsize-1);
! 123298: fts3HashInsertElement(pH, &pH->ht[h], new_elem);
! 123299: new_elem->data = data;
! 123300: return 0;
! 123301: }
! 123302:
! 123303: #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
! 123304:
! 123305: /************** End of fts3_hash.c *******************************************/
! 123306: /************** Begin file fts3_porter.c *************************************/
! 123307: /*
! 123308: ** 2006 September 30
! 123309: **
! 123310: ** The author disclaims copyright to this source code. In place of
! 123311: ** a legal notice, here is a blessing:
! 123312: **
! 123313: ** May you do good and not evil.
! 123314: ** May you find forgiveness for yourself and forgive others.
! 123315: ** May you share freely, never taking more than you give.
! 123316: **
! 123317: *************************************************************************
! 123318: ** Implementation of the full-text-search tokenizer that implements
! 123319: ** a Porter stemmer.
! 123320: */
! 123321:
! 123322: /*
! 123323: ** The code in this file is only compiled if:
! 123324: **
! 123325: ** * The FTS3 module is being built as an extension
! 123326: ** (in which case SQLITE_CORE is not defined), or
! 123327: **
! 123328: ** * The FTS3 module is being built into the core of
! 123329: ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
! 123330: */
! 123331: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
! 123332:
! 123333: /* #include <assert.h> */
! 123334: /* #include <stdlib.h> */
! 123335: /* #include <stdio.h> */
! 123336: /* #include <string.h> */
! 123337:
! 123338:
! 123339: /*
! 123340: ** Class derived from sqlite3_tokenizer
! 123341: */
! 123342: typedef struct porter_tokenizer {
! 123343: sqlite3_tokenizer base; /* Base class */
! 123344: } porter_tokenizer;
! 123345:
! 123346: /*
! 123347: ** Class derived from sqlite3_tokenizer_cursor
! 123348: */
! 123349: typedef struct porter_tokenizer_cursor {
! 123350: sqlite3_tokenizer_cursor base;
! 123351: const char *zInput; /* input we are tokenizing */
! 123352: int nInput; /* size of the input */
! 123353: int iOffset; /* current position in zInput */
! 123354: int iToken; /* index of next token to be returned */
! 123355: char *zToken; /* storage for current token */
! 123356: int nAllocated; /* space allocated to zToken buffer */
! 123357: } porter_tokenizer_cursor;
! 123358:
! 123359:
! 123360: /*
! 123361: ** Create a new tokenizer instance.
! 123362: */
! 123363: static int porterCreate(
! 123364: int argc, const char * const *argv,
! 123365: sqlite3_tokenizer **ppTokenizer
! 123366: ){
! 123367: porter_tokenizer *t;
! 123368:
! 123369: UNUSED_PARAMETER(argc);
! 123370: UNUSED_PARAMETER(argv);
! 123371:
! 123372: t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
! 123373: if( t==NULL ) return SQLITE_NOMEM;
! 123374: memset(t, 0, sizeof(*t));
! 123375: *ppTokenizer = &t->base;
! 123376: return SQLITE_OK;
! 123377: }
! 123378:
! 123379: /*
! 123380: ** Destroy a tokenizer
! 123381: */
! 123382: static int porterDestroy(sqlite3_tokenizer *pTokenizer){
! 123383: sqlite3_free(pTokenizer);
! 123384: return SQLITE_OK;
! 123385: }
! 123386:
! 123387: /*
! 123388: ** Prepare to begin tokenizing a particular string. The input
! 123389: ** string to be tokenized is zInput[0..nInput-1]. A cursor
! 123390: ** used to incrementally tokenize this string is returned in
! 123391: ** *ppCursor.
! 123392: */
! 123393: static int porterOpen(
! 123394: sqlite3_tokenizer *pTokenizer, /* The tokenizer */
! 123395: const char *zInput, int nInput, /* String to be tokenized */
! 123396: sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
! 123397: ){
! 123398: porter_tokenizer_cursor *c;
! 123399:
! 123400: UNUSED_PARAMETER(pTokenizer);
! 123401:
! 123402: c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
! 123403: if( c==NULL ) return SQLITE_NOMEM;
! 123404:
! 123405: c->zInput = zInput;
! 123406: if( zInput==0 ){
! 123407: c->nInput = 0;
! 123408: }else if( nInput<0 ){
! 123409: c->nInput = (int)strlen(zInput);
! 123410: }else{
! 123411: c->nInput = nInput;
! 123412: }
! 123413: c->iOffset = 0; /* start tokenizing at the beginning */
! 123414: c->iToken = 0;
! 123415: c->zToken = NULL; /* no space allocated, yet. */
! 123416: c->nAllocated = 0;
! 123417:
! 123418: *ppCursor = &c->base;
! 123419: return SQLITE_OK;
! 123420: }
! 123421:
! 123422: /*
! 123423: ** Close a tokenization cursor previously opened by a call to
! 123424: ** porterOpen() above.
! 123425: */
! 123426: static int porterClose(sqlite3_tokenizer_cursor *pCursor){
! 123427: porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
! 123428: sqlite3_free(c->zToken);
! 123429: sqlite3_free(c);
! 123430: return SQLITE_OK;
! 123431: }
! 123432: /*
! 123433: ** Vowel or consonant
! 123434: */
! 123435: static const char cType[] = {
! 123436: 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
! 123437: 1, 1, 1, 2, 1
! 123438: };
! 123439:
! 123440: /*
! 123441: ** isConsonant() and isVowel() determine if their first character in
! 123442: ** the string they point to is a consonant or a vowel, according
! 123443: ** to Porter ruls.
! 123444: **
! 123445: ** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
! 123446: ** 'Y' is a consonant unless it follows another consonant,
! 123447: ** in which case it is a vowel.
! 123448: **
! 123449: ** In these routine, the letters are in reverse order. So the 'y' rule
! 123450: ** is that 'y' is a consonant unless it is followed by another
! 123451: ** consonent.
! 123452: */
! 123453: static int isVowel(const char*);
! 123454: static int isConsonant(const char *z){
! 123455: int j;
! 123456: char x = *z;
! 123457: if( x==0 ) return 0;
! 123458: assert( x>='a' && x<='z' );
! 123459: j = cType[x-'a'];
! 123460: if( j<2 ) return j;
! 123461: return z[1]==0 || isVowel(z + 1);
! 123462: }
! 123463: static int isVowel(const char *z){
! 123464: int j;
! 123465: char x = *z;
! 123466: if( x==0 ) return 0;
! 123467: assert( x>='a' && x<='z' );
! 123468: j = cType[x-'a'];
! 123469: if( j<2 ) return 1-j;
! 123470: return isConsonant(z + 1);
! 123471: }
! 123472:
! 123473: /*
! 123474: ** Let any sequence of one or more vowels be represented by V and let
! 123475: ** C be sequence of one or more consonants. Then every word can be
! 123476: ** represented as:
! 123477: **
! 123478: ** [C] (VC){m} [V]
! 123479: **
! 123480: ** In prose: A word is an optional consonant followed by zero or
! 123481: ** vowel-consonant pairs followed by an optional vowel. "m" is the
! 123482: ** number of vowel consonant pairs. This routine computes the value
! 123483: ** of m for the first i bytes of a word.
! 123484: **
! 123485: ** Return true if the m-value for z is 1 or more. In other words,
! 123486: ** return true if z contains at least one vowel that is followed
! 123487: ** by a consonant.
! 123488: **
! 123489: ** In this routine z[] is in reverse order. So we are really looking
! 123490: ** for an instance of of a consonant followed by a vowel.
! 123491: */
! 123492: static int m_gt_0(const char *z){
! 123493: while( isVowel(z) ){ z++; }
! 123494: if( *z==0 ) return 0;
! 123495: while( isConsonant(z) ){ z++; }
! 123496: return *z!=0;
! 123497: }
! 123498:
! 123499: /* Like mgt0 above except we are looking for a value of m which is
! 123500: ** exactly 1
! 123501: */
! 123502: static int m_eq_1(const char *z){
! 123503: while( isVowel(z) ){ z++; }
! 123504: if( *z==0 ) return 0;
! 123505: while( isConsonant(z) ){ z++; }
! 123506: if( *z==0 ) return 0;
! 123507: while( isVowel(z) ){ z++; }
! 123508: if( *z==0 ) return 1;
! 123509: while( isConsonant(z) ){ z++; }
! 123510: return *z==0;
! 123511: }
! 123512:
! 123513: /* Like mgt0 above except we are looking for a value of m>1 instead
! 123514: ** or m>0
! 123515: */
! 123516: static int m_gt_1(const char *z){
! 123517: while( isVowel(z) ){ z++; }
! 123518: if( *z==0 ) return 0;
! 123519: while( isConsonant(z) ){ z++; }
! 123520: if( *z==0 ) return 0;
! 123521: while( isVowel(z) ){ z++; }
! 123522: if( *z==0 ) return 0;
! 123523: while( isConsonant(z) ){ z++; }
! 123524: return *z!=0;
! 123525: }
! 123526:
! 123527: /*
! 123528: ** Return TRUE if there is a vowel anywhere within z[0..n-1]
! 123529: */
! 123530: static int hasVowel(const char *z){
! 123531: while( isConsonant(z) ){ z++; }
! 123532: return *z!=0;
! 123533: }
! 123534:
! 123535: /*
! 123536: ** Return TRUE if the word ends in a double consonant.
! 123537: **
! 123538: ** The text is reversed here. So we are really looking at
! 123539: ** the first two characters of z[].
! 123540: */
! 123541: static int doubleConsonant(const char *z){
! 123542: return isConsonant(z) && z[0]==z[1];
! 123543: }
! 123544:
! 123545: /*
! 123546: ** Return TRUE if the word ends with three letters which
! 123547: ** are consonant-vowel-consonent and where the final consonant
! 123548: ** is not 'w', 'x', or 'y'.
! 123549: **
! 123550: ** The word is reversed here. So we are really checking the
! 123551: ** first three letters and the first one cannot be in [wxy].
! 123552: */
! 123553: static int star_oh(const char *z){
! 123554: return
! 123555: isConsonant(z) &&
! 123556: z[0]!='w' && z[0]!='x' && z[0]!='y' &&
! 123557: isVowel(z+1) &&
! 123558: isConsonant(z+2);
! 123559: }
! 123560:
! 123561: /*
! 123562: ** If the word ends with zFrom and xCond() is true for the stem
! 123563: ** of the word that preceeds the zFrom ending, then change the
! 123564: ** ending to zTo.
! 123565: **
! 123566: ** The input word *pz and zFrom are both in reverse order. zTo
! 123567: ** is in normal order.
! 123568: **
! 123569: ** Return TRUE if zFrom matches. Return FALSE if zFrom does not
! 123570: ** match. Not that TRUE is returned even if xCond() fails and
! 123571: ** no substitution occurs.
! 123572: */
! 123573: static int stem(
! 123574: char **pz, /* The word being stemmed (Reversed) */
! 123575: const char *zFrom, /* If the ending matches this... (Reversed) */
! 123576: const char *zTo, /* ... change the ending to this (not reversed) */
! 123577: int (*xCond)(const char*) /* Condition that must be true */
! 123578: ){
! 123579: char *z = *pz;
! 123580: while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
! 123581: if( *zFrom!=0 ) return 0;
! 123582: if( xCond && !xCond(z) ) return 1;
! 123583: while( *zTo ){
! 123584: *(--z) = *(zTo++);
! 123585: }
! 123586: *pz = z;
! 123587: return 1;
! 123588: }
! 123589:
! 123590: /*
! 123591: ** This is the fallback stemmer used when the porter stemmer is
! 123592: ** inappropriate. The input word is copied into the output with
! 123593: ** US-ASCII case folding. If the input word is too long (more
! 123594: ** than 20 bytes if it contains no digits or more than 6 bytes if
! 123595: ** it contains digits) then word is truncated to 20 or 6 bytes
! 123596: ** by taking 10 or 3 bytes from the beginning and end.
! 123597: */
! 123598: static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
! 123599: int i, mx, j;
! 123600: int hasDigit = 0;
! 123601: for(i=0; i<nIn; i++){
! 123602: char c = zIn[i];
! 123603: if( c>='A' && c<='Z' ){
! 123604: zOut[i] = c - 'A' + 'a';
! 123605: }else{
! 123606: if( c>='0' && c<='9' ) hasDigit = 1;
! 123607: zOut[i] = c;
! 123608: }
! 123609: }
! 123610: mx = hasDigit ? 3 : 10;
! 123611: if( nIn>mx*2 ){
! 123612: for(j=mx, i=nIn-mx; i<nIn; i++, j++){
! 123613: zOut[j] = zOut[i];
! 123614: }
! 123615: i = j;
! 123616: }
! 123617: zOut[i] = 0;
! 123618: *pnOut = i;
! 123619: }
! 123620:
! 123621:
! 123622: /*
! 123623: ** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
! 123624: ** zOut is at least big enough to hold nIn bytes. Write the actual
! 123625: ** size of the output word (exclusive of the '\0' terminator) into *pnOut.
! 123626: **
! 123627: ** Any upper-case characters in the US-ASCII character set ([A-Z])
! 123628: ** are converted to lower case. Upper-case UTF characters are
! 123629: ** unchanged.
! 123630: **
! 123631: ** Words that are longer than about 20 bytes are stemmed by retaining
! 123632: ** a few bytes from the beginning and the end of the word. If the
! 123633: ** word contains digits, 3 bytes are taken from the beginning and
! 123634: ** 3 bytes from the end. For long words without digits, 10 bytes
! 123635: ** are taken from each end. US-ASCII case folding still applies.
! 123636: **
! 123637: ** If the input word contains not digits but does characters not
! 123638: ** in [a-zA-Z] then no stemming is attempted and this routine just
! 123639: ** copies the input into the input into the output with US-ASCII
! 123640: ** case folding.
! 123641: **
! 123642: ** Stemming never increases the length of the word. So there is
! 123643: ** no chance of overflowing the zOut buffer.
! 123644: */
! 123645: static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
! 123646: int i, j;
! 123647: char zReverse[28];
! 123648: char *z, *z2;
! 123649: if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
! 123650: /* The word is too big or too small for the porter stemmer.
! 123651: ** Fallback to the copy stemmer */
! 123652: copy_stemmer(zIn, nIn, zOut, pnOut);
! 123653: return;
! 123654: }
! 123655: for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
! 123656: char c = zIn[i];
! 123657: if( c>='A' && c<='Z' ){
! 123658: zReverse[j] = c + 'a' - 'A';
! 123659: }else if( c>='a' && c<='z' ){
! 123660: zReverse[j] = c;
! 123661: }else{
! 123662: /* The use of a character not in [a-zA-Z] means that we fallback
! 123663: ** to the copy stemmer */
! 123664: copy_stemmer(zIn, nIn, zOut, pnOut);
! 123665: return;
! 123666: }
! 123667: }
! 123668: memset(&zReverse[sizeof(zReverse)-5], 0, 5);
! 123669: z = &zReverse[j+1];
! 123670:
! 123671:
! 123672: /* Step 1a */
! 123673: if( z[0]=='s' ){
! 123674: if(
! 123675: !stem(&z, "sess", "ss", 0) &&
! 123676: !stem(&z, "sei", "i", 0) &&
! 123677: !stem(&z, "ss", "ss", 0)
! 123678: ){
! 123679: z++;
! 123680: }
! 123681: }
! 123682:
! 123683: /* Step 1b */
! 123684: z2 = z;
! 123685: if( stem(&z, "dee", "ee", m_gt_0) ){
! 123686: /* Do nothing. The work was all in the test */
! 123687: }else if(
! 123688: (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
! 123689: && z!=z2
! 123690: ){
! 123691: if( stem(&z, "ta", "ate", 0) ||
! 123692: stem(&z, "lb", "ble", 0) ||
! 123693: stem(&z, "zi", "ize", 0) ){
! 123694: /* Do nothing. The work was all in the test */
! 123695: }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
! 123696: z++;
! 123697: }else if( m_eq_1(z) && star_oh(z) ){
! 123698: *(--z) = 'e';
! 123699: }
! 123700: }
! 123701:
! 123702: /* Step 1c */
! 123703: if( z[0]=='y' && hasVowel(z+1) ){
! 123704: z[0] = 'i';
! 123705: }
! 123706:
! 123707: /* Step 2 */
! 123708: switch( z[1] ){
! 123709: case 'a':
! 123710: stem(&z, "lanoita", "ate", m_gt_0) ||
! 123711: stem(&z, "lanoit", "tion", m_gt_0);
! 123712: break;
! 123713: case 'c':
! 123714: stem(&z, "icne", "ence", m_gt_0) ||
! 123715: stem(&z, "icna", "ance", m_gt_0);
! 123716: break;
! 123717: case 'e':
! 123718: stem(&z, "rezi", "ize", m_gt_0);
! 123719: break;
! 123720: case 'g':
! 123721: stem(&z, "igol", "log", m_gt_0);
! 123722: break;
! 123723: case 'l':
! 123724: stem(&z, "ilb", "ble", m_gt_0) ||
! 123725: stem(&z, "illa", "al", m_gt_0) ||
! 123726: stem(&z, "iltne", "ent", m_gt_0) ||
! 123727: stem(&z, "ile", "e", m_gt_0) ||
! 123728: stem(&z, "ilsuo", "ous", m_gt_0);
! 123729: break;
! 123730: case 'o':
! 123731: stem(&z, "noitazi", "ize", m_gt_0) ||
! 123732: stem(&z, "noita", "ate", m_gt_0) ||
! 123733: stem(&z, "rota", "ate", m_gt_0);
! 123734: break;
! 123735: case 's':
! 123736: stem(&z, "msila", "al", m_gt_0) ||
! 123737: stem(&z, "ssenevi", "ive", m_gt_0) ||
! 123738: stem(&z, "ssenluf", "ful", m_gt_0) ||
! 123739: stem(&z, "ssensuo", "ous", m_gt_0);
! 123740: break;
! 123741: case 't':
! 123742: stem(&z, "itila", "al", m_gt_0) ||
! 123743: stem(&z, "itivi", "ive", m_gt_0) ||
! 123744: stem(&z, "itilib", "ble", m_gt_0);
! 123745: break;
! 123746: }
! 123747:
! 123748: /* Step 3 */
! 123749: switch( z[0] ){
! 123750: case 'e':
! 123751: stem(&z, "etaci", "ic", m_gt_0) ||
! 123752: stem(&z, "evita", "", m_gt_0) ||
! 123753: stem(&z, "ezila", "al", m_gt_0);
! 123754: break;
! 123755: case 'i':
! 123756: stem(&z, "itici", "ic", m_gt_0);
! 123757: break;
! 123758: case 'l':
! 123759: stem(&z, "laci", "ic", m_gt_0) ||
! 123760: stem(&z, "luf", "", m_gt_0);
! 123761: break;
! 123762: case 's':
! 123763: stem(&z, "ssen", "", m_gt_0);
! 123764: break;
! 123765: }
! 123766:
! 123767: /* Step 4 */
! 123768: switch( z[1] ){
! 123769: case 'a':
! 123770: if( z[0]=='l' && m_gt_1(z+2) ){
! 123771: z += 2;
! 123772: }
! 123773: break;
! 123774: case 'c':
! 123775: if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){
! 123776: z += 4;
! 123777: }
! 123778: break;
! 123779: case 'e':
! 123780: if( z[0]=='r' && m_gt_1(z+2) ){
! 123781: z += 2;
! 123782: }
! 123783: break;
! 123784: case 'i':
! 123785: if( z[0]=='c' && m_gt_1(z+2) ){
! 123786: z += 2;
! 123787: }
! 123788: break;
! 123789: case 'l':
! 123790: if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
! 123791: z += 4;
! 123792: }
! 123793: break;
! 123794: case 'n':
! 123795: if( z[0]=='t' ){
! 123796: if( z[2]=='a' ){
! 123797: if( m_gt_1(z+3) ){
! 123798: z += 3;
! 123799: }
! 123800: }else if( z[2]=='e' ){
! 123801: stem(&z, "tneme", "", m_gt_1) ||
! 123802: stem(&z, "tnem", "", m_gt_1) ||
! 123803: stem(&z, "tne", "", m_gt_1);
! 123804: }
! 123805: }
! 123806: break;
! 123807: case 'o':
! 123808: if( z[0]=='u' ){
! 123809: if( m_gt_1(z+2) ){
! 123810: z += 2;
! 123811: }
! 123812: }else if( z[3]=='s' || z[3]=='t' ){
! 123813: stem(&z, "noi", "", m_gt_1);
! 123814: }
! 123815: break;
! 123816: case 's':
! 123817: if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
! 123818: z += 3;
! 123819: }
! 123820: break;
! 123821: case 't':
! 123822: stem(&z, "eta", "", m_gt_1) ||
! 123823: stem(&z, "iti", "", m_gt_1);
! 123824: break;
! 123825: case 'u':
! 123826: if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
! 123827: z += 3;
! 123828: }
! 123829: break;
! 123830: case 'v':
! 123831: case 'z':
! 123832: if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
! 123833: z += 3;
! 123834: }
! 123835: break;
! 123836: }
! 123837:
! 123838: /* Step 5a */
! 123839: if( z[0]=='e' ){
! 123840: if( m_gt_1(z+1) ){
! 123841: z++;
! 123842: }else if( m_eq_1(z+1) && !star_oh(z+1) ){
! 123843: z++;
! 123844: }
! 123845: }
! 123846:
! 123847: /* Step 5b */
! 123848: if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
! 123849: z++;
! 123850: }
! 123851:
! 123852: /* z[] is now the stemmed word in reverse order. Flip it back
! 123853: ** around into forward order and return.
! 123854: */
! 123855: *pnOut = i = (int)strlen(z);
! 123856: zOut[i] = 0;
! 123857: while( *z ){
! 123858: zOut[--i] = *(z++);
! 123859: }
! 123860: }
! 123861:
! 123862: /*
! 123863: ** Characters that can be part of a token. We assume any character
! 123864: ** whose value is greater than 0x80 (any UTF character) can be
! 123865: ** part of a token. In other words, delimiters all must have
! 123866: ** values of 0x7f or lower.
! 123867: */
! 123868: static const char porterIdChar[] = {
! 123869: /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
! 123870: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
! 123871: 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
! 123872: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
! 123873: 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
! 123874: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
! 123875: };
! 123876: #define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
! 123877:
! 123878: /*
! 123879: ** Extract the next token from a tokenization cursor. The cursor must
! 123880: ** have been opened by a prior call to porterOpen().
! 123881: */
! 123882: static int porterNext(
! 123883: sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */
! 123884: const char **pzToken, /* OUT: *pzToken is the token text */
! 123885: int *pnBytes, /* OUT: Number of bytes in token */
! 123886: int *piStartOffset, /* OUT: Starting offset of token */
! 123887: int *piEndOffset, /* OUT: Ending offset of token */
! 123888: int *piPosition /* OUT: Position integer of token */
! 123889: ){
! 123890: porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
! 123891: const char *z = c->zInput;
! 123892:
! 123893: while( c->iOffset<c->nInput ){
! 123894: int iStartOffset, ch;
! 123895:
! 123896: /* Scan past delimiter characters */
! 123897: while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
! 123898: c->iOffset++;
! 123899: }
! 123900:
! 123901: /* Count non-delimiter characters. */
! 123902: iStartOffset = c->iOffset;
! 123903: while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
! 123904: c->iOffset++;
! 123905: }
! 123906:
! 123907: if( c->iOffset>iStartOffset ){
! 123908: int n = c->iOffset-iStartOffset;
! 123909: if( n>c->nAllocated ){
! 123910: char *pNew;
! 123911: c->nAllocated = n+20;
! 123912: pNew = sqlite3_realloc(c->zToken, c->nAllocated);
! 123913: if( !pNew ) return SQLITE_NOMEM;
! 123914: c->zToken = pNew;
! 123915: }
! 123916: porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
! 123917: *pzToken = c->zToken;
! 123918: *piStartOffset = iStartOffset;
! 123919: *piEndOffset = c->iOffset;
! 123920: *piPosition = c->iToken++;
! 123921: return SQLITE_OK;
! 123922: }
! 123923: }
! 123924: return SQLITE_DONE;
! 123925: }
! 123926:
! 123927: /*
! 123928: ** The set of routines that implement the porter-stemmer tokenizer
! 123929: */
! 123930: static const sqlite3_tokenizer_module porterTokenizerModule = {
! 123931: 0,
! 123932: porterCreate,
! 123933: porterDestroy,
! 123934: porterOpen,
! 123935: porterClose,
! 123936: porterNext,
! 123937: };
! 123938:
! 123939: /*
! 123940: ** Allocate a new porter tokenizer. Return a pointer to the new
! 123941: ** tokenizer in *ppModule
! 123942: */
! 123943: SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
! 123944: sqlite3_tokenizer_module const**ppModule
! 123945: ){
! 123946: *ppModule = &porterTokenizerModule;
! 123947: }
! 123948:
! 123949: #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
! 123950:
! 123951: /************** End of fts3_porter.c *****************************************/
! 123952: /************** Begin file fts3_tokenizer.c **********************************/
! 123953: /*
! 123954: ** 2007 June 22
! 123955: **
! 123956: ** The author disclaims copyright to this source code. In place of
! 123957: ** a legal notice, here is a blessing:
! 123958: **
! 123959: ** May you do good and not evil.
! 123960: ** May you find forgiveness for yourself and forgive others.
! 123961: ** May you share freely, never taking more than you give.
! 123962: **
! 123963: ******************************************************************************
! 123964: **
! 123965: ** This is part of an SQLite module implementing full-text search.
! 123966: ** This particular file implements the generic tokenizer interface.
! 123967: */
! 123968:
! 123969: /*
! 123970: ** The code in this file is only compiled if:
! 123971: **
! 123972: ** * The FTS3 module is being built as an extension
! 123973: ** (in which case SQLITE_CORE is not defined), or
! 123974: **
! 123975: ** * The FTS3 module is being built into the core of
! 123976: ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
! 123977: */
! 123978: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
! 123979:
! 123980: /* #include <assert.h> */
! 123981: /* #include <string.h> */
! 123982:
! 123983: /*
! 123984: ** Implementation of the SQL scalar function for accessing the underlying
! 123985: ** hash table. This function may be called as follows:
! 123986: **
! 123987: ** SELECT <function-name>(<key-name>);
! 123988: ** SELECT <function-name>(<key-name>, <pointer>);
! 123989: **
! 123990: ** where <function-name> is the name passed as the second argument
! 123991: ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
! 123992: **
! 123993: ** If the <pointer> argument is specified, it must be a blob value
! 123994: ** containing a pointer to be stored as the hash data corresponding
! 123995: ** to the string <key-name>. If <pointer> is not specified, then
! 123996: ** the string <key-name> must already exist in the has table. Otherwise,
! 123997: ** an error is returned.
! 123998: **
! 123999: ** Whether or not the <pointer> argument is specified, the value returned
! 124000: ** is a blob containing the pointer stored as the hash data corresponding
! 124001: ** to string <key-name> (after the hash-table is updated, if applicable).
! 124002: */
! 124003: static void scalarFunc(
! 124004: sqlite3_context *context,
! 124005: int argc,
! 124006: sqlite3_value **argv
! 124007: ){
! 124008: Fts3Hash *pHash;
! 124009: void *pPtr = 0;
! 124010: const unsigned char *zName;
! 124011: int nName;
! 124012:
! 124013: assert( argc==1 || argc==2 );
! 124014:
! 124015: pHash = (Fts3Hash *)sqlite3_user_data(context);
! 124016:
! 124017: zName = sqlite3_value_text(argv[0]);
! 124018: nName = sqlite3_value_bytes(argv[0])+1;
! 124019:
! 124020: if( argc==2 ){
! 124021: void *pOld;
! 124022: int n = sqlite3_value_bytes(argv[1]);
! 124023: if( n!=sizeof(pPtr) ){
! 124024: sqlite3_result_error(context, "argument type mismatch", -1);
! 124025: return;
! 124026: }
! 124027: pPtr = *(void **)sqlite3_value_blob(argv[1]);
! 124028: pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
! 124029: if( pOld==pPtr ){
! 124030: sqlite3_result_error(context, "out of memory", -1);
! 124031: return;
! 124032: }
! 124033: }else{
! 124034: pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
! 124035: if( !pPtr ){
! 124036: char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
! 124037: sqlite3_result_error(context, zErr, -1);
! 124038: sqlite3_free(zErr);
! 124039: return;
! 124040: }
! 124041: }
! 124042:
! 124043: sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
! 124044: }
! 124045:
! 124046: SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
! 124047: static const char isFtsIdChar[] = {
! 124048: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
! 124049: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
! 124050: 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
! 124051: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
! 124052: 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
! 124053: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
! 124054: 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
! 124055: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
! 124056: };
! 124057: return (c&0x80 || isFtsIdChar[(int)(c)]);
! 124058: }
! 124059:
! 124060: SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
! 124061: const char *z1;
! 124062: const char *z2 = 0;
! 124063:
! 124064: /* Find the start of the next token. */
! 124065: z1 = zStr;
! 124066: while( z2==0 ){
! 124067: char c = *z1;
! 124068: switch( c ){
! 124069: case '\0': return 0; /* No more tokens here */
! 124070: case '\'':
! 124071: case '"':
! 124072: case '`': {
! 124073: z2 = z1;
! 124074: while( *++z2 && (*z2!=c || *++z2==c) );
! 124075: break;
! 124076: }
! 124077: case '[':
! 124078: z2 = &z1[1];
! 124079: while( *z2 && z2[0]!=']' ) z2++;
! 124080: if( *z2 ) z2++;
! 124081: break;
! 124082:
! 124083: default:
! 124084: if( sqlite3Fts3IsIdChar(*z1) ){
! 124085: z2 = &z1[1];
! 124086: while( sqlite3Fts3IsIdChar(*z2) ) z2++;
! 124087: }else{
! 124088: z1++;
! 124089: }
! 124090: }
! 124091: }
! 124092:
! 124093: *pn = (int)(z2-z1);
! 124094: return z1;
! 124095: }
! 124096:
! 124097: SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
! 124098: Fts3Hash *pHash, /* Tokenizer hash table */
! 124099: const char *zArg, /* Tokenizer name */
! 124100: sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */
! 124101: char **pzErr /* OUT: Set to malloced error message */
! 124102: ){
! 124103: int rc;
! 124104: char *z = (char *)zArg;
! 124105: int n = 0;
! 124106: char *zCopy;
! 124107: char *zEnd; /* Pointer to nul-term of zCopy */
! 124108: sqlite3_tokenizer_module *m;
! 124109:
! 124110: zCopy = sqlite3_mprintf("%s", zArg);
! 124111: if( !zCopy ) return SQLITE_NOMEM;
! 124112: zEnd = &zCopy[strlen(zCopy)];
! 124113:
! 124114: z = (char *)sqlite3Fts3NextToken(zCopy, &n);
! 124115: z[n] = '\0';
! 124116: sqlite3Fts3Dequote(z);
! 124117:
! 124118: m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
! 124119: if( !m ){
! 124120: *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
! 124121: rc = SQLITE_ERROR;
! 124122: }else{
! 124123: char const **aArg = 0;
! 124124: int iArg = 0;
! 124125: z = &z[n+1];
! 124126: while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
! 124127: int nNew = sizeof(char *)*(iArg+1);
! 124128: char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
! 124129: if( !aNew ){
! 124130: sqlite3_free(zCopy);
! 124131: sqlite3_free((void *)aArg);
! 124132: return SQLITE_NOMEM;
! 124133: }
! 124134: aArg = aNew;
! 124135: aArg[iArg++] = z;
! 124136: z[n] = '\0';
! 124137: sqlite3Fts3Dequote(z);
! 124138: z = &z[n+1];
! 124139: }
! 124140: rc = m->xCreate(iArg, aArg, ppTok);
! 124141: assert( rc!=SQLITE_OK || *ppTok );
! 124142: if( rc!=SQLITE_OK ){
! 124143: *pzErr = sqlite3_mprintf("unknown tokenizer");
! 124144: }else{
! 124145: (*ppTok)->pModule = m;
! 124146: }
! 124147: sqlite3_free((void *)aArg);
! 124148: }
! 124149:
! 124150: sqlite3_free(zCopy);
! 124151: return rc;
! 124152: }
! 124153:
! 124154:
! 124155: #ifdef SQLITE_TEST
! 124156:
! 124157: /* #include <tcl.h> */
! 124158: /* #include <string.h> */
! 124159:
! 124160: /*
! 124161: ** Implementation of a special SQL scalar function for testing tokenizers
! 124162: ** designed to be used in concert with the Tcl testing framework. This
! 124163: ** function must be called with two arguments:
! 124164: **
! 124165: ** SELECT <function-name>(<key-name>, <input-string>);
! 124166: ** SELECT <function-name>(<key-name>, <pointer>);
! 124167: **
! 124168: ** where <function-name> is the name passed as the second argument
! 124169: ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
! 124170: ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
! 124171: **
! 124172: ** The return value is a string that may be interpreted as a Tcl
! 124173: ** list. For each token in the <input-string>, three elements are
! 124174: ** added to the returned list. The first is the token position, the
! 124175: ** second is the token text (folded, stemmed, etc.) and the third is the
! 124176: ** substring of <input-string> associated with the token. For example,
! 124177: ** using the built-in "simple" tokenizer:
! 124178: **
! 124179: ** SELECT fts_tokenizer_test('simple', 'I don't see how');
! 124180: **
! 124181: ** will return the string:
! 124182: **
! 124183: ** "{0 i I 1 dont don't 2 see see 3 how how}"
! 124184: **
! 124185: */
! 124186: static void testFunc(
! 124187: sqlite3_context *context,
! 124188: int argc,
! 124189: sqlite3_value **argv
! 124190: ){
! 124191: Fts3Hash *pHash;
! 124192: sqlite3_tokenizer_module *p;
! 124193: sqlite3_tokenizer *pTokenizer = 0;
! 124194: sqlite3_tokenizer_cursor *pCsr = 0;
! 124195:
! 124196: const char *zErr = 0;
! 124197:
! 124198: const char *zName;
! 124199: int nName;
! 124200: const char *zInput;
! 124201: int nInput;
! 124202:
! 124203: const char *zArg = 0;
! 124204:
! 124205: const char *zToken;
! 124206: int nToken;
! 124207: int iStart;
! 124208: int iEnd;
! 124209: int iPos;
! 124210:
! 124211: Tcl_Obj *pRet;
! 124212:
! 124213: assert( argc==2 || argc==3 );
! 124214:
! 124215: nName = sqlite3_value_bytes(argv[0]);
! 124216: zName = (const char *)sqlite3_value_text(argv[0]);
! 124217: nInput = sqlite3_value_bytes(argv[argc-1]);
! 124218: zInput = (const char *)sqlite3_value_text(argv[argc-1]);
! 124219:
! 124220: if( argc==3 ){
! 124221: zArg = (const char *)sqlite3_value_text(argv[1]);
! 124222: }
! 124223:
! 124224: pHash = (Fts3Hash *)sqlite3_user_data(context);
! 124225: p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
! 124226:
! 124227: if( !p ){
! 124228: char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
! 124229: sqlite3_result_error(context, zErr, -1);
! 124230: sqlite3_free(zErr);
! 124231: return;
! 124232: }
! 124233:
! 124234: pRet = Tcl_NewObj();
! 124235: Tcl_IncrRefCount(pRet);
! 124236:
! 124237: if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){
! 124238: zErr = "error in xCreate()";
! 124239: goto finish;
! 124240: }
! 124241: pTokenizer->pModule = p;
! 124242: if( SQLITE_OK!=p->xOpen(pTokenizer, zInput, nInput, &pCsr) ){
! 124243: zErr = "error in xOpen()";
! 124244: goto finish;
! 124245: }
! 124246: pCsr->pTokenizer = pTokenizer;
! 124247:
! 124248: while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
! 124249: Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
! 124250: Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
! 124251: zToken = &zInput[iStart];
! 124252: nToken = iEnd-iStart;
! 124253: Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
! 124254: }
! 124255:
! 124256: if( SQLITE_OK!=p->xClose(pCsr) ){
! 124257: zErr = "error in xClose()";
! 124258: goto finish;
! 124259: }
! 124260: if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
! 124261: zErr = "error in xDestroy()";
! 124262: goto finish;
! 124263: }
! 124264:
! 124265: finish:
! 124266: if( zErr ){
! 124267: sqlite3_result_error(context, zErr, -1);
! 124268: }else{
! 124269: sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
! 124270: }
! 124271: Tcl_DecrRefCount(pRet);
! 124272: }
! 124273:
! 124274: static
! 124275: int registerTokenizer(
! 124276: sqlite3 *db,
! 124277: char *zName,
! 124278: const sqlite3_tokenizer_module *p
! 124279: ){
! 124280: int rc;
! 124281: sqlite3_stmt *pStmt;
! 124282: const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
! 124283:
! 124284: rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
! 124285: if( rc!=SQLITE_OK ){
! 124286: return rc;
! 124287: }
! 124288:
! 124289: sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
! 124290: sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
! 124291: sqlite3_step(pStmt);
! 124292:
! 124293: return sqlite3_finalize(pStmt);
! 124294: }
! 124295:
! 124296: static
! 124297: int queryTokenizer(
! 124298: sqlite3 *db,
! 124299: char *zName,
! 124300: const sqlite3_tokenizer_module **pp
! 124301: ){
! 124302: int rc;
! 124303: sqlite3_stmt *pStmt;
! 124304: const char zSql[] = "SELECT fts3_tokenizer(?)";
! 124305:
! 124306: *pp = 0;
! 124307: rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
! 124308: if( rc!=SQLITE_OK ){
! 124309: return rc;
! 124310: }
! 124311:
! 124312: sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
! 124313: if( SQLITE_ROW==sqlite3_step(pStmt) ){
! 124314: if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
! 124315: memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
! 124316: }
! 124317: }
! 124318:
! 124319: return sqlite3_finalize(pStmt);
! 124320: }
! 124321:
! 124322: SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
! 124323:
! 124324: /*
! 124325: ** Implementation of the scalar function fts3_tokenizer_internal_test().
! 124326: ** This function is used for testing only, it is not included in the
! 124327: ** build unless SQLITE_TEST is defined.
! 124328: **
! 124329: ** The purpose of this is to test that the fts3_tokenizer() function
! 124330: ** can be used as designed by the C-code in the queryTokenizer and
! 124331: ** registerTokenizer() functions above. These two functions are repeated
! 124332: ** in the README.tokenizer file as an example, so it is important to
! 124333: ** test them.
! 124334: **
! 124335: ** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
! 124336: ** function with no arguments. An assert() will fail if a problem is
! 124337: ** detected. i.e.:
! 124338: **
! 124339: ** SELECT fts3_tokenizer_internal_test();
! 124340: **
! 124341: */
! 124342: static void intTestFunc(
! 124343: sqlite3_context *context,
! 124344: int argc,
! 124345: sqlite3_value **argv
! 124346: ){
! 124347: int rc;
! 124348: const sqlite3_tokenizer_module *p1;
! 124349: const sqlite3_tokenizer_module *p2;
! 124350: sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
! 124351:
! 124352: UNUSED_PARAMETER(argc);
! 124353: UNUSED_PARAMETER(argv);
! 124354:
! 124355: /* Test the query function */
! 124356: sqlite3Fts3SimpleTokenizerModule(&p1);
! 124357: rc = queryTokenizer(db, "simple", &p2);
! 124358: assert( rc==SQLITE_OK );
! 124359: assert( p1==p2 );
! 124360: rc = queryTokenizer(db, "nosuchtokenizer", &p2);
! 124361: assert( rc==SQLITE_ERROR );
! 124362: assert( p2==0 );
! 124363: assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
! 124364:
! 124365: /* Test the storage function */
! 124366: rc = registerTokenizer(db, "nosuchtokenizer", p1);
! 124367: assert( rc==SQLITE_OK );
! 124368: rc = queryTokenizer(db, "nosuchtokenizer", &p2);
! 124369: assert( rc==SQLITE_OK );
! 124370: assert( p2==p1 );
! 124371:
! 124372: sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
! 124373: }
! 124374:
! 124375: #endif
! 124376:
! 124377: /*
! 124378: ** Set up SQL objects in database db used to access the contents of
! 124379: ** the hash table pointed to by argument pHash. The hash table must
! 124380: ** been initialised to use string keys, and to take a private copy
! 124381: ** of the key when a value is inserted. i.e. by a call similar to:
! 124382: **
! 124383: ** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
! 124384: **
! 124385: ** This function adds a scalar function (see header comment above
! 124386: ** scalarFunc() in this file for details) and, if ENABLE_TABLE is
! 124387: ** defined at compilation time, a temporary virtual table (see header
! 124388: ** comment above struct HashTableVtab) to the database schema. Both
! 124389: ** provide read/write access to the contents of *pHash.
! 124390: **
! 124391: ** The third argument to this function, zName, is used as the name
! 124392: ** of both the scalar and, if created, the virtual table.
! 124393: */
! 124394: SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
! 124395: sqlite3 *db,
! 124396: Fts3Hash *pHash,
! 124397: const char *zName
! 124398: ){
! 124399: int rc = SQLITE_OK;
! 124400: void *p = (void *)pHash;
! 124401: const int any = SQLITE_ANY;
! 124402:
! 124403: #ifdef SQLITE_TEST
! 124404: char *zTest = 0;
! 124405: char *zTest2 = 0;
! 124406: void *pdb = (void *)db;
! 124407: zTest = sqlite3_mprintf("%s_test", zName);
! 124408: zTest2 = sqlite3_mprintf("%s_internal_test", zName);
! 124409: if( !zTest || !zTest2 ){
! 124410: rc = SQLITE_NOMEM;
! 124411: }
! 124412: #endif
! 124413:
! 124414: if( SQLITE_OK==rc ){
! 124415: rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
! 124416: }
! 124417: if( SQLITE_OK==rc ){
! 124418: rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
! 124419: }
! 124420: #ifdef SQLITE_TEST
! 124421: if( SQLITE_OK==rc ){
! 124422: rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0);
! 124423: }
! 124424: if( SQLITE_OK==rc ){
! 124425: rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0);
! 124426: }
! 124427: if( SQLITE_OK==rc ){
! 124428: rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
! 124429: }
! 124430: #endif
! 124431:
! 124432: #ifdef SQLITE_TEST
! 124433: sqlite3_free(zTest);
! 124434: sqlite3_free(zTest2);
! 124435: #endif
! 124436:
! 124437: return rc;
! 124438: }
! 124439:
! 124440: #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
! 124441:
! 124442: /************** End of fts3_tokenizer.c **************************************/
! 124443: /************** Begin file fts3_tokenizer1.c *********************************/
! 124444: /*
! 124445: ** 2006 Oct 10
! 124446: **
! 124447: ** The author disclaims copyright to this source code. In place of
! 124448: ** a legal notice, here is a blessing:
! 124449: **
! 124450: ** May you do good and not evil.
! 124451: ** May you find forgiveness for yourself and forgive others.
! 124452: ** May you share freely, never taking more than you give.
! 124453: **
! 124454: ******************************************************************************
! 124455: **
! 124456: ** Implementation of the "simple" full-text-search tokenizer.
! 124457: */
! 124458:
! 124459: /*
! 124460: ** The code in this file is only compiled if:
! 124461: **
! 124462: ** * The FTS3 module is being built as an extension
! 124463: ** (in which case SQLITE_CORE is not defined), or
! 124464: **
! 124465: ** * The FTS3 module is being built into the core of
! 124466: ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
! 124467: */
! 124468: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
! 124469:
! 124470: /* #include <assert.h> */
! 124471: /* #include <stdlib.h> */
! 124472: /* #include <stdio.h> */
! 124473: /* #include <string.h> */
! 124474:
! 124475:
! 124476: typedef struct simple_tokenizer {
! 124477: sqlite3_tokenizer base;
! 124478: char delim[128]; /* flag ASCII delimiters */
! 124479: } simple_tokenizer;
! 124480:
! 124481: typedef struct simple_tokenizer_cursor {
! 124482: sqlite3_tokenizer_cursor base;
! 124483: const char *pInput; /* input we are tokenizing */
! 124484: int nBytes; /* size of the input */
! 124485: int iOffset; /* current position in pInput */
! 124486: int iToken; /* index of next token to be returned */
! 124487: char *pToken; /* storage for current token */
! 124488: int nTokenAllocated; /* space allocated to zToken buffer */
! 124489: } simple_tokenizer_cursor;
! 124490:
! 124491:
! 124492: static int simpleDelim(simple_tokenizer *t, unsigned char c){
! 124493: return c<0x80 && t->delim[c];
! 124494: }
! 124495: static int fts3_isalnum(int x){
! 124496: return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
! 124497: }
! 124498:
! 124499: /*
! 124500: ** Create a new tokenizer instance.
! 124501: */
! 124502: static int simpleCreate(
! 124503: int argc, const char * const *argv,
! 124504: sqlite3_tokenizer **ppTokenizer
! 124505: ){
! 124506: simple_tokenizer *t;
! 124507:
! 124508: t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
! 124509: if( t==NULL ) return SQLITE_NOMEM;
! 124510: memset(t, 0, sizeof(*t));
! 124511:
! 124512: /* TODO(shess) Delimiters need to remain the same from run to run,
! 124513: ** else we need to reindex. One solution would be a meta-table to
! 124514: ** track such information in the database, then we'd only want this
! 124515: ** information on the initial create.
! 124516: */
! 124517: if( argc>1 ){
! 124518: int i, n = (int)strlen(argv[1]);
! 124519: for(i=0; i<n; i++){
! 124520: unsigned char ch = argv[1][i];
! 124521: /* We explicitly don't support UTF-8 delimiters for now. */
! 124522: if( ch>=0x80 ){
! 124523: sqlite3_free(t);
! 124524: return SQLITE_ERROR;
! 124525: }
! 124526: t->delim[ch] = 1;
! 124527: }
! 124528: } else {
! 124529: /* Mark non-alphanumeric ASCII characters as delimiters */
! 124530: int i;
! 124531: for(i=1; i<0x80; i++){
! 124532: t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
! 124533: }
! 124534: }
! 124535:
! 124536: *ppTokenizer = &t->base;
! 124537: return SQLITE_OK;
! 124538: }
! 124539:
! 124540: /*
! 124541: ** Destroy a tokenizer
! 124542: */
! 124543: static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
! 124544: sqlite3_free(pTokenizer);
! 124545: return SQLITE_OK;
! 124546: }
! 124547:
! 124548: /*
! 124549: ** Prepare to begin tokenizing a particular string. The input
! 124550: ** string to be tokenized is pInput[0..nBytes-1]. A cursor
! 124551: ** used to incrementally tokenize this string is returned in
! 124552: ** *ppCursor.
! 124553: */
! 124554: static int simpleOpen(
! 124555: sqlite3_tokenizer *pTokenizer, /* The tokenizer */
! 124556: const char *pInput, int nBytes, /* String to be tokenized */
! 124557: sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
! 124558: ){
! 124559: simple_tokenizer_cursor *c;
! 124560:
! 124561: UNUSED_PARAMETER(pTokenizer);
! 124562:
! 124563: c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
! 124564: if( c==NULL ) return SQLITE_NOMEM;
! 124565:
! 124566: c->pInput = pInput;
! 124567: if( pInput==0 ){
! 124568: c->nBytes = 0;
! 124569: }else if( nBytes<0 ){
! 124570: c->nBytes = (int)strlen(pInput);
! 124571: }else{
! 124572: c->nBytes = nBytes;
! 124573: }
! 124574: c->iOffset = 0; /* start tokenizing at the beginning */
! 124575: c->iToken = 0;
! 124576: c->pToken = NULL; /* no space allocated, yet. */
! 124577: c->nTokenAllocated = 0;
! 124578:
! 124579: *ppCursor = &c->base;
! 124580: return SQLITE_OK;
! 124581: }
! 124582:
! 124583: /*
! 124584: ** Close a tokenization cursor previously opened by a call to
! 124585: ** simpleOpen() above.
! 124586: */
! 124587: static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
! 124588: simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
! 124589: sqlite3_free(c->pToken);
! 124590: sqlite3_free(c);
! 124591: return SQLITE_OK;
! 124592: }
! 124593:
! 124594: /*
! 124595: ** Extract the next token from a tokenization cursor. The cursor must
! 124596: ** have been opened by a prior call to simpleOpen().
! 124597: */
! 124598: static int simpleNext(
! 124599: sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
! 124600: const char **ppToken, /* OUT: *ppToken is the token text */
! 124601: int *pnBytes, /* OUT: Number of bytes in token */
! 124602: int *piStartOffset, /* OUT: Starting offset of token */
! 124603: int *piEndOffset, /* OUT: Ending offset of token */
! 124604: int *piPosition /* OUT: Position integer of token */
! 124605: ){
! 124606: simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
! 124607: simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
! 124608: unsigned char *p = (unsigned char *)c->pInput;
! 124609:
! 124610: while( c->iOffset<c->nBytes ){
! 124611: int iStartOffset;
! 124612:
! 124613: /* Scan past delimiter characters */
! 124614: while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
! 124615: c->iOffset++;
! 124616: }
! 124617:
! 124618: /* Count non-delimiter characters. */
! 124619: iStartOffset = c->iOffset;
! 124620: while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
! 124621: c->iOffset++;
! 124622: }
! 124623:
! 124624: if( c->iOffset>iStartOffset ){
! 124625: int i, n = c->iOffset-iStartOffset;
! 124626: if( n>c->nTokenAllocated ){
! 124627: char *pNew;
! 124628: c->nTokenAllocated = n+20;
! 124629: pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
! 124630: if( !pNew ) return SQLITE_NOMEM;
! 124631: c->pToken = pNew;
! 124632: }
! 124633: for(i=0; i<n; i++){
! 124634: /* TODO(shess) This needs expansion to handle UTF-8
! 124635: ** case-insensitivity.
! 124636: */
! 124637: unsigned char ch = p[iStartOffset+i];
! 124638: c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
! 124639: }
! 124640: *ppToken = c->pToken;
! 124641: *pnBytes = n;
! 124642: *piStartOffset = iStartOffset;
! 124643: *piEndOffset = c->iOffset;
! 124644: *piPosition = c->iToken++;
! 124645:
! 124646: return SQLITE_OK;
! 124647: }
! 124648: }
! 124649: return SQLITE_DONE;
! 124650: }
! 124651:
! 124652: /*
! 124653: ** The set of routines that implement the simple tokenizer
! 124654: */
! 124655: static const sqlite3_tokenizer_module simpleTokenizerModule = {
! 124656: 0,
! 124657: simpleCreate,
! 124658: simpleDestroy,
! 124659: simpleOpen,
! 124660: simpleClose,
! 124661: simpleNext,
! 124662: };
! 124663:
! 124664: /*
! 124665: ** Allocate a new simple tokenizer. Return a pointer to the new
! 124666: ** tokenizer in *ppModule
! 124667: */
! 124668: SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
! 124669: sqlite3_tokenizer_module const**ppModule
! 124670: ){
! 124671: *ppModule = &simpleTokenizerModule;
! 124672: }
! 124673:
! 124674: #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
! 124675:
! 124676: /************** End of fts3_tokenizer1.c *************************************/
! 124677: /************** Begin file fts3_write.c **************************************/
! 124678: /*
! 124679: ** 2009 Oct 23
! 124680: **
! 124681: ** The author disclaims copyright to this source code. In place of
! 124682: ** a legal notice, here is a blessing:
! 124683: **
! 124684: ** May you do good and not evil.
! 124685: ** May you find forgiveness for yourself and forgive others.
! 124686: ** May you share freely, never taking more than you give.
! 124687: **
! 124688: ******************************************************************************
! 124689: **
! 124690: ** This file is part of the SQLite FTS3 extension module. Specifically,
! 124691: ** this file contains code to insert, update and delete rows from FTS3
! 124692: ** tables. It also contains code to merge FTS3 b-tree segments. Some
! 124693: ** of the sub-routines used to merge segments are also used by the query
! 124694: ** code in fts3.c.
! 124695: */
! 124696:
! 124697: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
! 124698:
! 124699: /* #include <string.h> */
! 124700: /* #include <assert.h> */
! 124701: /* #include <stdlib.h> */
! 124702:
! 124703: /*
! 124704: ** When full-text index nodes are loaded from disk, the buffer that they
! 124705: ** are loaded into has the following number of bytes of padding at the end
! 124706: ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
! 124707: ** of 920 bytes is allocated for it.
! 124708: **
! 124709: ** This means that if we have a pointer into a buffer containing node data,
! 124710: ** it is always safe to read up to two varints from it without risking an
! 124711: ** overread, even if the node data is corrupted.
! 124712: */
! 124713: #define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
! 124714:
! 124715: /*
! 124716: ** Under certain circumstances, b-tree nodes (doclists) can be loaded into
! 124717: ** memory incrementally instead of all at once. This can be a big performance
! 124718: ** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
! 124719: ** method before retrieving all query results (as may happen, for example,
! 124720: ** if a query has a LIMIT clause).
! 124721: **
! 124722: ** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
! 124723: ** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
! 124724: ** The code is written so that the hard lower-limit for each of these values
! 124725: ** is 1. Clearly such small values would be inefficient, but can be useful
! 124726: ** for testing purposes.
! 124727: **
! 124728: ** If this module is built with SQLITE_TEST defined, these constants may
! 124729: ** be overridden at runtime for testing purposes. File fts3_test.c contains
! 124730: ** a Tcl interface to read and write the values.
! 124731: */
! 124732: #ifdef SQLITE_TEST
! 124733: int test_fts3_node_chunksize = (4*1024);
! 124734: int test_fts3_node_chunk_threshold = (4*1024)*4;
! 124735: # define FTS3_NODE_CHUNKSIZE test_fts3_node_chunksize
! 124736: # define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
! 124737: #else
! 124738: # define FTS3_NODE_CHUNKSIZE (4*1024)
! 124739: # define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
! 124740: #endif
! 124741:
! 124742: typedef struct PendingList PendingList;
! 124743: typedef struct SegmentNode SegmentNode;
! 124744: typedef struct SegmentWriter SegmentWriter;
! 124745:
! 124746: /*
! 124747: ** An instance of the following data structure is used to build doclists
! 124748: ** incrementally. See function fts3PendingListAppend() for details.
! 124749: */
! 124750: struct PendingList {
! 124751: int nData;
! 124752: char *aData;
! 124753: int nSpace;
! 124754: sqlite3_int64 iLastDocid;
! 124755: sqlite3_int64 iLastCol;
! 124756: sqlite3_int64 iLastPos;
! 124757: };
! 124758:
! 124759:
! 124760: /*
! 124761: ** Each cursor has a (possibly empty) linked list of the following objects.
! 124762: */
! 124763: struct Fts3DeferredToken {
! 124764: Fts3PhraseToken *pToken; /* Pointer to corresponding expr token */
! 124765: int iCol; /* Column token must occur in */
! 124766: Fts3DeferredToken *pNext; /* Next in list of deferred tokens */
! 124767: PendingList *pList; /* Doclist is assembled here */
! 124768: };
! 124769:
! 124770: /*
! 124771: ** An instance of this structure is used to iterate through the terms on
! 124772: ** a contiguous set of segment b-tree leaf nodes. Although the details of
! 124773: ** this structure are only manipulated by code in this file, opaque handles
! 124774: ** of type Fts3SegReader* are also used by code in fts3.c to iterate through
! 124775: ** terms when querying the full-text index. See functions:
! 124776: **
! 124777: ** sqlite3Fts3SegReaderNew()
! 124778: ** sqlite3Fts3SegReaderFree()
! 124779: ** sqlite3Fts3SegReaderIterate()
! 124780: **
! 124781: ** Methods used to manipulate Fts3SegReader structures:
! 124782: **
! 124783: ** fts3SegReaderNext()
! 124784: ** fts3SegReaderFirstDocid()
! 124785: ** fts3SegReaderNextDocid()
! 124786: */
! 124787: struct Fts3SegReader {
! 124788: int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
! 124789:
! 124790: sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
! 124791: sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
! 124792: sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
! 124793: sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
! 124794:
! 124795: char *aNode; /* Pointer to node data (or NULL) */
! 124796: int nNode; /* Size of buffer at aNode (or 0) */
! 124797: int nPopulate; /* If >0, bytes of buffer aNode[] loaded */
! 124798: sqlite3_blob *pBlob; /* If not NULL, blob handle to read node */
! 124799:
! 124800: Fts3HashElem **ppNextElem;
! 124801:
! 124802: /* Variables set by fts3SegReaderNext(). These may be read directly
! 124803: ** by the caller. They are valid from the time SegmentReaderNew() returns
! 124804: ** until SegmentReaderNext() returns something other than SQLITE_OK
! 124805: ** (i.e. SQLITE_DONE).
! 124806: */
! 124807: int nTerm; /* Number of bytes in current term */
! 124808: char *zTerm; /* Pointer to current term */
! 124809: int nTermAlloc; /* Allocated size of zTerm buffer */
! 124810: char *aDoclist; /* Pointer to doclist of current entry */
! 124811: int nDoclist; /* Size of doclist in current entry */
! 124812:
! 124813: /* The following variables are used by fts3SegReaderNextDocid() to iterate
! 124814: ** through the current doclist (aDoclist/nDoclist).
! 124815: */
! 124816: char *pOffsetList;
! 124817: int nOffsetList; /* For descending pending seg-readers only */
! 124818: sqlite3_int64 iDocid;
! 124819: };
! 124820:
! 124821: #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
! 124822: #define fts3SegReaderIsRootOnly(p) ((p)->aNode==(char *)&(p)[1])
! 124823:
! 124824: /*
! 124825: ** An instance of this structure is used to create a segment b-tree in the
! 124826: ** database. The internal details of this type are only accessed by the
! 124827: ** following functions:
! 124828: **
! 124829: ** fts3SegWriterAdd()
! 124830: ** fts3SegWriterFlush()
! 124831: ** fts3SegWriterFree()
! 124832: */
! 124833: struct SegmentWriter {
! 124834: SegmentNode *pTree; /* Pointer to interior tree structure */
! 124835: sqlite3_int64 iFirst; /* First slot in %_segments written */
! 124836: sqlite3_int64 iFree; /* Next free slot in %_segments */
! 124837: char *zTerm; /* Pointer to previous term buffer */
! 124838: int nTerm; /* Number of bytes in zTerm */
! 124839: int nMalloc; /* Size of malloc'd buffer at zMalloc */
! 124840: char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
! 124841: int nSize; /* Size of allocation at aData */
! 124842: int nData; /* Bytes of data in aData */
! 124843: char *aData; /* Pointer to block from malloc() */
! 124844: };
! 124845:
! 124846: /*
! 124847: ** Type SegmentNode is used by the following three functions to create
! 124848: ** the interior part of the segment b+-tree structures (everything except
! 124849: ** the leaf nodes). These functions and type are only ever used by code
! 124850: ** within the fts3SegWriterXXX() family of functions described above.
! 124851: **
! 124852: ** fts3NodeAddTerm()
! 124853: ** fts3NodeWrite()
! 124854: ** fts3NodeFree()
! 124855: **
! 124856: ** When a b+tree is written to the database (either as a result of a merge
! 124857: ** or the pending-terms table being flushed), leaves are written into the
! 124858: ** database file as soon as they are completely populated. The interior of
! 124859: ** the tree is assembled in memory and written out only once all leaves have
! 124860: ** been populated and stored. This is Ok, as the b+-tree fanout is usually
! 124861: ** very large, meaning that the interior of the tree consumes relatively
! 124862: ** little memory.
! 124863: */
! 124864: struct SegmentNode {
! 124865: SegmentNode *pParent; /* Parent node (or NULL for root node) */
! 124866: SegmentNode *pRight; /* Pointer to right-sibling */
! 124867: SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
! 124868: int nEntry; /* Number of terms written to node so far */
! 124869: char *zTerm; /* Pointer to previous term buffer */
! 124870: int nTerm; /* Number of bytes in zTerm */
! 124871: int nMalloc; /* Size of malloc'd buffer at zMalloc */
! 124872: char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
! 124873: int nData; /* Bytes of valid data so far */
! 124874: char *aData; /* Node data */
! 124875: };
! 124876:
! 124877: /*
! 124878: ** Valid values for the second argument to fts3SqlStmt().
! 124879: */
! 124880: #define SQL_DELETE_CONTENT 0
! 124881: #define SQL_IS_EMPTY 1
! 124882: #define SQL_DELETE_ALL_CONTENT 2
! 124883: #define SQL_DELETE_ALL_SEGMENTS 3
! 124884: #define SQL_DELETE_ALL_SEGDIR 4
! 124885: #define SQL_DELETE_ALL_DOCSIZE 5
! 124886: #define SQL_DELETE_ALL_STAT 6
! 124887: #define SQL_SELECT_CONTENT_BY_ROWID 7
! 124888: #define SQL_NEXT_SEGMENT_INDEX 8
! 124889: #define SQL_INSERT_SEGMENTS 9
! 124890: #define SQL_NEXT_SEGMENTS_ID 10
! 124891: #define SQL_INSERT_SEGDIR 11
! 124892: #define SQL_SELECT_LEVEL 12
! 124893: #define SQL_SELECT_LEVEL_RANGE 13
! 124894: #define SQL_SELECT_LEVEL_COUNT 14
! 124895: #define SQL_SELECT_SEGDIR_MAX_LEVEL 15
! 124896: #define SQL_DELETE_SEGDIR_LEVEL 16
! 124897: #define SQL_DELETE_SEGMENTS_RANGE 17
! 124898: #define SQL_CONTENT_INSERT 18
! 124899: #define SQL_DELETE_DOCSIZE 19
! 124900: #define SQL_REPLACE_DOCSIZE 20
! 124901: #define SQL_SELECT_DOCSIZE 21
! 124902: #define SQL_SELECT_DOCTOTAL 22
! 124903: #define SQL_REPLACE_DOCTOTAL 23
! 124904:
! 124905: #define SQL_SELECT_ALL_PREFIX_LEVEL 24
! 124906: #define SQL_DELETE_ALL_TERMS_SEGDIR 25
! 124907:
! 124908: #define SQL_DELETE_SEGDIR_RANGE 26
! 124909:
! 124910: /*
! 124911: ** This function is used to obtain an SQLite prepared statement handle
! 124912: ** for the statement identified by the second argument. If successful,
! 124913: ** *pp is set to the requested statement handle and SQLITE_OK returned.
! 124914: ** Otherwise, an SQLite error code is returned and *pp is set to 0.
! 124915: **
! 124916: ** If argument apVal is not NULL, then it must point to an array with
! 124917: ** at least as many entries as the requested statement has bound
! 124918: ** parameters. The values are bound to the statements parameters before
! 124919: ** returning.
! 124920: */
! 124921: static int fts3SqlStmt(
! 124922: Fts3Table *p, /* Virtual table handle */
! 124923: int eStmt, /* One of the SQL_XXX constants above */
! 124924: sqlite3_stmt **pp, /* OUT: Statement handle */
! 124925: sqlite3_value **apVal /* Values to bind to statement */
! 124926: ){
! 124927: const char *azSql[] = {
! 124928: /* 0 */ "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
! 124929: /* 1 */ "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
! 124930: /* 2 */ "DELETE FROM %Q.'%q_content'",
! 124931: /* 3 */ "DELETE FROM %Q.'%q_segments'",
! 124932: /* 4 */ "DELETE FROM %Q.'%q_segdir'",
! 124933: /* 5 */ "DELETE FROM %Q.'%q_docsize'",
! 124934: /* 6 */ "DELETE FROM %Q.'%q_stat'",
! 124935: /* 7 */ "SELECT %s WHERE rowid=?",
! 124936: /* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
! 124937: /* 9 */ "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
! 124938: /* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
! 124939: /* 11 */ "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
! 124940:
! 124941: /* Return segments in order from oldest to newest.*/
! 124942: /* 12 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
! 124943: "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
! 124944: /* 13 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
! 124945: "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
! 124946: "ORDER BY level DESC, idx ASC",
! 124947:
! 124948: /* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
! 124949: /* 15 */ "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
! 124950:
! 124951: /* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
! 124952: /* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
! 124953: /* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)",
! 124954: /* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
! 124955: /* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
! 124956: /* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
! 124957: /* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=0",
! 124958: /* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(0,?)",
! 124959: /* 24 */ "",
! 124960: /* 25 */ "",
! 124961:
! 124962: /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
! 124963:
! 124964: };
! 124965: int rc = SQLITE_OK;
! 124966: sqlite3_stmt *pStmt;
! 124967:
! 124968: assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
! 124969: assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
! 124970:
! 124971: pStmt = p->aStmt[eStmt];
! 124972: if( !pStmt ){
! 124973: char *zSql;
! 124974: if( eStmt==SQL_CONTENT_INSERT ){
! 124975: zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
! 124976: }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
! 124977: zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
! 124978: }else{
! 124979: zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
! 124980: }
! 124981: if( !zSql ){
! 124982: rc = SQLITE_NOMEM;
! 124983: }else{
! 124984: rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
! 124985: sqlite3_free(zSql);
! 124986: assert( rc==SQLITE_OK || pStmt==0 );
! 124987: p->aStmt[eStmt] = pStmt;
! 124988: }
! 124989: }
! 124990: if( apVal ){
! 124991: int i;
! 124992: int nParam = sqlite3_bind_parameter_count(pStmt);
! 124993: for(i=0; rc==SQLITE_OK && i<nParam; i++){
! 124994: rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
! 124995: }
! 124996: }
! 124997: *pp = pStmt;
! 124998: return rc;
! 124999: }
! 125000:
! 125001: static int fts3SelectDocsize(
! 125002: Fts3Table *pTab, /* FTS3 table handle */
! 125003: int eStmt, /* Either SQL_SELECT_DOCSIZE or DOCTOTAL */
! 125004: sqlite3_int64 iDocid, /* Docid to bind for SQL_SELECT_DOCSIZE */
! 125005: sqlite3_stmt **ppStmt /* OUT: Statement handle */
! 125006: ){
! 125007: sqlite3_stmt *pStmt = 0; /* Statement requested from fts3SqlStmt() */
! 125008: int rc; /* Return code */
! 125009:
! 125010: assert( eStmt==SQL_SELECT_DOCSIZE || eStmt==SQL_SELECT_DOCTOTAL );
! 125011:
! 125012: rc = fts3SqlStmt(pTab, eStmt, &pStmt, 0);
! 125013: if( rc==SQLITE_OK ){
! 125014: if( eStmt==SQL_SELECT_DOCSIZE ){
! 125015: sqlite3_bind_int64(pStmt, 1, iDocid);
! 125016: }
! 125017: rc = sqlite3_step(pStmt);
! 125018: if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
! 125019: rc = sqlite3_reset(pStmt);
! 125020: if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
! 125021: pStmt = 0;
! 125022: }else{
! 125023: rc = SQLITE_OK;
! 125024: }
! 125025: }
! 125026:
! 125027: *ppStmt = pStmt;
! 125028: return rc;
! 125029: }
! 125030:
! 125031: SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
! 125032: Fts3Table *pTab, /* Fts3 table handle */
! 125033: sqlite3_stmt **ppStmt /* OUT: Statement handle */
! 125034: ){
! 125035: return fts3SelectDocsize(pTab, SQL_SELECT_DOCTOTAL, 0, ppStmt);
! 125036: }
! 125037:
! 125038: SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
! 125039: Fts3Table *pTab, /* Fts3 table handle */
! 125040: sqlite3_int64 iDocid, /* Docid to read size data for */
! 125041: sqlite3_stmt **ppStmt /* OUT: Statement handle */
! 125042: ){
! 125043: return fts3SelectDocsize(pTab, SQL_SELECT_DOCSIZE, iDocid, ppStmt);
! 125044: }
! 125045:
! 125046: /*
! 125047: ** Similar to fts3SqlStmt(). Except, after binding the parameters in
! 125048: ** array apVal[] to the SQL statement identified by eStmt, the statement
! 125049: ** is executed.
! 125050: **
! 125051: ** Returns SQLITE_OK if the statement is successfully executed, or an
! 125052: ** SQLite error code otherwise.
! 125053: */
! 125054: static void fts3SqlExec(
! 125055: int *pRC, /* Result code */
! 125056: Fts3Table *p, /* The FTS3 table */
! 125057: int eStmt, /* Index of statement to evaluate */
! 125058: sqlite3_value **apVal /* Parameters to bind */
! 125059: ){
! 125060: sqlite3_stmt *pStmt;
! 125061: int rc;
! 125062: if( *pRC ) return;
! 125063: rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
! 125064: if( rc==SQLITE_OK ){
! 125065: sqlite3_step(pStmt);
! 125066: rc = sqlite3_reset(pStmt);
! 125067: }
! 125068: *pRC = rc;
! 125069: }
! 125070:
! 125071:
! 125072: /*
! 125073: ** This function ensures that the caller has obtained a shared-cache
! 125074: ** table-lock on the %_content table. This is required before reading
! 125075: ** data from the fts3 table. If this lock is not acquired first, then
! 125076: ** the caller may end up holding read-locks on the %_segments and %_segdir
! 125077: ** tables, but no read-lock on the %_content table. If this happens
! 125078: ** a second connection will be able to write to the fts3 table, but
! 125079: ** attempting to commit those writes might return SQLITE_LOCKED or
! 125080: ** SQLITE_LOCKED_SHAREDCACHE (because the commit attempts to obtain
! 125081: ** write-locks on the %_segments and %_segdir ** tables).
! 125082: **
! 125083: ** We try to avoid this because if FTS3 returns any error when committing
! 125084: ** a transaction, the whole transaction will be rolled back. And this is
! 125085: ** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can
! 125086: ** still happen if the user reads data directly from the %_segments or
! 125087: ** %_segdir tables instead of going through FTS3 though.
! 125088: **
! 125089: ** This reasoning does not apply to a content=xxx table.
! 125090: */
! 125091: SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){
! 125092: int rc; /* Return code */
! 125093: sqlite3_stmt *pStmt; /* Statement used to obtain lock */
! 125094:
! 125095: if( p->zContentTbl==0 ){
! 125096: rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0);
! 125097: if( rc==SQLITE_OK ){
! 125098: sqlite3_bind_null(pStmt, 1);
! 125099: sqlite3_step(pStmt);
! 125100: rc = sqlite3_reset(pStmt);
! 125101: }
! 125102: }else{
! 125103: rc = SQLITE_OK;
! 125104: }
! 125105:
! 125106: return rc;
! 125107: }
! 125108:
! 125109: /*
! 125110: ** Set *ppStmt to a statement handle that may be used to iterate through
! 125111: ** all rows in the %_segdir table, from oldest to newest. If successful,
! 125112: ** return SQLITE_OK. If an error occurs while preparing the statement,
! 125113: ** return an SQLite error code.
! 125114: **
! 125115: ** There is only ever one instance of this SQL statement compiled for
! 125116: ** each FTS3 table.
! 125117: **
! 125118: ** The statement returns the following columns from the %_segdir table:
! 125119: **
! 125120: ** 0: idx
! 125121: ** 1: start_block
! 125122: ** 2: leaves_end_block
! 125123: ** 3: end_block
! 125124: ** 4: root
! 125125: */
! 125126: SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
! 125127: Fts3Table *p, /* FTS3 table */
! 125128: int iIndex, /* Index for p->aIndex[] */
! 125129: int iLevel, /* Level to select */
! 125130: sqlite3_stmt **ppStmt /* OUT: Compiled statement */
! 125131: ){
! 125132: int rc;
! 125133: sqlite3_stmt *pStmt = 0;
! 125134:
! 125135: assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
! 125136: assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
! 125137: assert( iIndex>=0 && iIndex<p->nIndex );
! 125138:
! 125139: if( iLevel<0 ){
! 125140: /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
! 125141: rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
! 125142: if( rc==SQLITE_OK ){
! 125143: sqlite3_bind_int(pStmt, 1, iIndex*FTS3_SEGDIR_MAXLEVEL);
! 125144: sqlite3_bind_int(pStmt, 2, (iIndex+1)*FTS3_SEGDIR_MAXLEVEL-1);
! 125145: }
! 125146: }else{
! 125147: /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
! 125148: rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
! 125149: if( rc==SQLITE_OK ){
! 125150: sqlite3_bind_int(pStmt, 1, iLevel+iIndex*FTS3_SEGDIR_MAXLEVEL);
! 125151: }
! 125152: }
! 125153: *ppStmt = pStmt;
! 125154: return rc;
! 125155: }
! 125156:
! 125157:
! 125158: /*
! 125159: ** Append a single varint to a PendingList buffer. SQLITE_OK is returned
! 125160: ** if successful, or an SQLite error code otherwise.
! 125161: **
! 125162: ** This function also serves to allocate the PendingList structure itself.
! 125163: ** For example, to create a new PendingList structure containing two
! 125164: ** varints:
! 125165: **
! 125166: ** PendingList *p = 0;
! 125167: ** fts3PendingListAppendVarint(&p, 1);
! 125168: ** fts3PendingListAppendVarint(&p, 2);
! 125169: */
! 125170: static int fts3PendingListAppendVarint(
! 125171: PendingList **pp, /* IN/OUT: Pointer to PendingList struct */
! 125172: sqlite3_int64 i /* Value to append to data */
! 125173: ){
! 125174: PendingList *p = *pp;
! 125175:
! 125176: /* Allocate or grow the PendingList as required. */
! 125177: if( !p ){
! 125178: p = sqlite3_malloc(sizeof(*p) + 100);
! 125179: if( !p ){
! 125180: return SQLITE_NOMEM;
! 125181: }
! 125182: p->nSpace = 100;
! 125183: p->aData = (char *)&p[1];
! 125184: p->nData = 0;
! 125185: }
! 125186: else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
! 125187: int nNew = p->nSpace * 2;
! 125188: p = sqlite3_realloc(p, sizeof(*p) + nNew);
! 125189: if( !p ){
! 125190: sqlite3_free(*pp);
! 125191: *pp = 0;
! 125192: return SQLITE_NOMEM;
! 125193: }
! 125194: p->nSpace = nNew;
! 125195: p->aData = (char *)&p[1];
! 125196: }
! 125197:
! 125198: /* Append the new serialized varint to the end of the list. */
! 125199: p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
! 125200: p->aData[p->nData] = '\0';
! 125201: *pp = p;
! 125202: return SQLITE_OK;
! 125203: }
! 125204:
! 125205: /*
! 125206: ** Add a docid/column/position entry to a PendingList structure. Non-zero
! 125207: ** is returned if the structure is sqlite3_realloced as part of adding
! 125208: ** the entry. Otherwise, zero.
! 125209: **
! 125210: ** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
! 125211: ** Zero is always returned in this case. Otherwise, if no OOM error occurs,
! 125212: ** it is set to SQLITE_OK.
! 125213: */
! 125214: static int fts3PendingListAppend(
! 125215: PendingList **pp, /* IN/OUT: PendingList structure */
! 125216: sqlite3_int64 iDocid, /* Docid for entry to add */
! 125217: sqlite3_int64 iCol, /* Column for entry to add */
! 125218: sqlite3_int64 iPos, /* Position of term for entry to add */
! 125219: int *pRc /* OUT: Return code */
! 125220: ){
! 125221: PendingList *p = *pp;
! 125222: int rc = SQLITE_OK;
! 125223:
! 125224: assert( !p || p->iLastDocid<=iDocid );
! 125225:
! 125226: if( !p || p->iLastDocid!=iDocid ){
! 125227: sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
! 125228: if( p ){
! 125229: assert( p->nData<p->nSpace );
! 125230: assert( p->aData[p->nData]==0 );
! 125231: p->nData++;
! 125232: }
! 125233: if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
! 125234: goto pendinglistappend_out;
! 125235: }
! 125236: p->iLastCol = -1;
! 125237: p->iLastPos = 0;
! 125238: p->iLastDocid = iDocid;
! 125239: }
! 125240: if( iCol>0 && p->iLastCol!=iCol ){
! 125241: if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
! 125242: || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
! 125243: ){
! 125244: goto pendinglistappend_out;
! 125245: }
! 125246: p->iLastCol = iCol;
! 125247: p->iLastPos = 0;
! 125248: }
! 125249: if( iCol>=0 ){
! 125250: assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
! 125251: rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
! 125252: if( rc==SQLITE_OK ){
! 125253: p->iLastPos = iPos;
! 125254: }
! 125255: }
! 125256:
! 125257: pendinglistappend_out:
! 125258: *pRc = rc;
! 125259: if( p!=*pp ){
! 125260: *pp = p;
! 125261: return 1;
! 125262: }
! 125263: return 0;
! 125264: }
! 125265:
! 125266: /*
! 125267: ** Free a PendingList object allocated by fts3PendingListAppend().
! 125268: */
! 125269: static void fts3PendingListDelete(PendingList *pList){
! 125270: sqlite3_free(pList);
! 125271: }
! 125272:
! 125273: /*
! 125274: ** Add an entry to one of the pending-terms hash tables.
! 125275: */
! 125276: static int fts3PendingTermsAddOne(
! 125277: Fts3Table *p,
! 125278: int iCol,
! 125279: int iPos,
! 125280: Fts3Hash *pHash, /* Pending terms hash table to add entry to */
! 125281: const char *zToken,
! 125282: int nToken
! 125283: ){
! 125284: PendingList *pList;
! 125285: int rc = SQLITE_OK;
! 125286:
! 125287: pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
! 125288: if( pList ){
! 125289: p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
! 125290: }
! 125291: if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
! 125292: if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
! 125293: /* Malloc failed while inserting the new entry. This can only
! 125294: ** happen if there was no previous entry for this token.
! 125295: */
! 125296: assert( 0==fts3HashFind(pHash, zToken, nToken) );
! 125297: sqlite3_free(pList);
! 125298: rc = SQLITE_NOMEM;
! 125299: }
! 125300: }
! 125301: if( rc==SQLITE_OK ){
! 125302: p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
! 125303: }
! 125304: return rc;
! 125305: }
! 125306:
! 125307: /*
! 125308: ** Tokenize the nul-terminated string zText and add all tokens to the
! 125309: ** pending-terms hash-table. The docid used is that currently stored in
! 125310: ** p->iPrevDocid, and the column is specified by argument iCol.
! 125311: **
! 125312: ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
! 125313: */
! 125314: static int fts3PendingTermsAdd(
! 125315: Fts3Table *p, /* Table into which text will be inserted */
! 125316: const char *zText, /* Text of document to be inserted */
! 125317: int iCol, /* Column into which text is being inserted */
! 125318: u32 *pnWord /* OUT: Number of tokens inserted */
! 125319: ){
! 125320: int rc;
! 125321: int iStart;
! 125322: int iEnd;
! 125323: int iPos;
! 125324: int nWord = 0;
! 125325:
! 125326: char const *zToken;
! 125327: int nToken;
! 125328:
! 125329: sqlite3_tokenizer *pTokenizer = p->pTokenizer;
! 125330: sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
! 125331: sqlite3_tokenizer_cursor *pCsr;
! 125332: int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
! 125333: const char**,int*,int*,int*,int*);
! 125334:
! 125335: assert( pTokenizer && pModule );
! 125336:
! 125337: /* If the user has inserted a NULL value, this function may be called with
! 125338: ** zText==0. In this case, add zero token entries to the hash table and
! 125339: ** return early. */
! 125340: if( zText==0 ){
! 125341: *pnWord = 0;
! 125342: return SQLITE_OK;
! 125343: }
! 125344:
! 125345: rc = pModule->xOpen(pTokenizer, zText, -1, &pCsr);
! 125346: if( rc!=SQLITE_OK ){
! 125347: return rc;
! 125348: }
! 125349: pCsr->pTokenizer = pTokenizer;
! 125350:
! 125351: xNext = pModule->xNext;
! 125352: while( SQLITE_OK==rc
! 125353: && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
! 125354: ){
! 125355: int i;
! 125356: if( iPos>=nWord ) nWord = iPos+1;
! 125357:
! 125358: /* Positions cannot be negative; we use -1 as a terminator internally.
! 125359: ** Tokens must have a non-zero length.
! 125360: */
! 125361: if( iPos<0 || !zToken || nToken<=0 ){
! 125362: rc = SQLITE_ERROR;
! 125363: break;
! 125364: }
! 125365:
! 125366: /* Add the term to the terms index */
! 125367: rc = fts3PendingTermsAddOne(
! 125368: p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
! 125369: );
! 125370:
! 125371: /* Add the term to each of the prefix indexes that it is not too
! 125372: ** short for. */
! 125373: for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
! 125374: struct Fts3Index *pIndex = &p->aIndex[i];
! 125375: if( nToken<pIndex->nPrefix ) continue;
! 125376: rc = fts3PendingTermsAddOne(
! 125377: p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
! 125378: );
! 125379: }
! 125380: }
! 125381:
! 125382: pModule->xClose(pCsr);
! 125383: *pnWord = nWord;
! 125384: return (rc==SQLITE_DONE ? SQLITE_OK : rc);
! 125385: }
! 125386:
! 125387: /*
! 125388: ** Calling this function indicates that subsequent calls to
! 125389: ** fts3PendingTermsAdd() are to add term/position-list pairs for the
! 125390: ** contents of the document with docid iDocid.
! 125391: */
! 125392: static int fts3PendingTermsDocid(Fts3Table *p, sqlite_int64 iDocid){
! 125393: /* TODO(shess) Explore whether partially flushing the buffer on
! 125394: ** forced-flush would provide better performance. I suspect that if
! 125395: ** we ordered the doclists by size and flushed the largest until the
! 125396: ** buffer was half empty, that would let the less frequent terms
! 125397: ** generate longer doclists.
! 125398: */
! 125399: if( iDocid<=p->iPrevDocid || p->nPendingData>p->nMaxPendingData ){
! 125400: int rc = sqlite3Fts3PendingTermsFlush(p);
! 125401: if( rc!=SQLITE_OK ) return rc;
! 125402: }
! 125403: p->iPrevDocid = iDocid;
! 125404: return SQLITE_OK;
! 125405: }
! 125406:
! 125407: /*
! 125408: ** Discard the contents of the pending-terms hash tables.
! 125409: */
! 125410: SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
! 125411: int i;
! 125412: for(i=0; i<p->nIndex; i++){
! 125413: Fts3HashElem *pElem;
! 125414: Fts3Hash *pHash = &p->aIndex[i].hPending;
! 125415: for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
! 125416: PendingList *pList = (PendingList *)fts3HashData(pElem);
! 125417: fts3PendingListDelete(pList);
! 125418: }
! 125419: fts3HashClear(pHash);
! 125420: }
! 125421: p->nPendingData = 0;
! 125422: }
! 125423:
! 125424: /*
! 125425: ** This function is called by the xUpdate() method as part of an INSERT
! 125426: ** operation. It adds entries for each term in the new record to the
! 125427: ** pendingTerms hash table.
! 125428: **
! 125429: ** Argument apVal is the same as the similarly named argument passed to
! 125430: ** fts3InsertData(). Parameter iDocid is the docid of the new row.
! 125431: */
! 125432: static int fts3InsertTerms(Fts3Table *p, sqlite3_value **apVal, u32 *aSz){
! 125433: int i; /* Iterator variable */
! 125434: for(i=2; i<p->nColumn+2; i++){
! 125435: const char *zText = (const char *)sqlite3_value_text(apVal[i]);
! 125436: int rc = fts3PendingTermsAdd(p, zText, i-2, &aSz[i-2]);
! 125437: if( rc!=SQLITE_OK ){
! 125438: return rc;
! 125439: }
! 125440: aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
! 125441: }
! 125442: return SQLITE_OK;
! 125443: }
! 125444:
! 125445: /*
! 125446: ** This function is called by the xUpdate() method for an INSERT operation.
! 125447: ** The apVal parameter is passed a copy of the apVal argument passed by
! 125448: ** SQLite to the xUpdate() method. i.e:
! 125449: **
! 125450: ** apVal[0] Not used for INSERT.
! 125451: ** apVal[1] rowid
! 125452: ** apVal[2] Left-most user-defined column
! 125453: ** ...
! 125454: ** apVal[p->nColumn+1] Right-most user-defined column
! 125455: ** apVal[p->nColumn+2] Hidden column with same name as table
! 125456: ** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
! 125457: */
! 125458: static int fts3InsertData(
! 125459: Fts3Table *p, /* Full-text table */
! 125460: sqlite3_value **apVal, /* Array of values to insert */
! 125461: sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */
! 125462: ){
! 125463: int rc; /* Return code */
! 125464: sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */
! 125465:
! 125466: if( p->zContentTbl ){
! 125467: sqlite3_value *pRowid = apVal[p->nColumn+3];
! 125468: if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
! 125469: pRowid = apVal[1];
! 125470: }
! 125471: if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
! 125472: return SQLITE_CONSTRAINT;
! 125473: }
! 125474: *piDocid = sqlite3_value_int64(pRowid);
! 125475: return SQLITE_OK;
! 125476: }
! 125477:
! 125478: /* Locate the statement handle used to insert data into the %_content
! 125479: ** table. The SQL for this statement is:
! 125480: **
! 125481: ** INSERT INTO %_content VALUES(?, ?, ?, ...)
! 125482: **
! 125483: ** The statement features N '?' variables, where N is the number of user
! 125484: ** defined columns in the FTS3 table, plus one for the docid field.
! 125485: */
! 125486: rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
! 125487: if( rc!=SQLITE_OK ){
! 125488: return rc;
! 125489: }
! 125490:
! 125491: /* There is a quirk here. The users INSERT statement may have specified
! 125492: ** a value for the "rowid" field, for the "docid" field, or for both.
! 125493: ** Which is a problem, since "rowid" and "docid" are aliases for the
! 125494: ** same value. For example:
! 125495: **
! 125496: ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
! 125497: **
! 125498: ** In FTS3, this is an error. It is an error to specify non-NULL values
! 125499: ** for both docid and some other rowid alias.
! 125500: */
! 125501: if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
! 125502: if( SQLITE_NULL==sqlite3_value_type(apVal[0])
! 125503: && SQLITE_NULL!=sqlite3_value_type(apVal[1])
! 125504: ){
! 125505: /* A rowid/docid conflict. */
! 125506: return SQLITE_ERROR;
! 125507: }
! 125508: rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
! 125509: if( rc!=SQLITE_OK ) return rc;
! 125510: }
! 125511:
! 125512: /* Execute the statement to insert the record. Set *piDocid to the
! 125513: ** new docid value.
! 125514: */
! 125515: sqlite3_step(pContentInsert);
! 125516: rc = sqlite3_reset(pContentInsert);
! 125517:
! 125518: *piDocid = sqlite3_last_insert_rowid(p->db);
! 125519: return rc;
! 125520: }
! 125521:
! 125522:
! 125523:
! 125524: /*
! 125525: ** Remove all data from the FTS3 table. Clear the hash table containing
! 125526: ** pending terms.
! 125527: */
! 125528: static int fts3DeleteAll(Fts3Table *p, int bContent){
! 125529: int rc = SQLITE_OK; /* Return code */
! 125530:
! 125531: /* Discard the contents of the pending-terms hash table. */
! 125532: sqlite3Fts3PendingTermsClear(p);
! 125533:
! 125534: /* Delete everything from the shadow tables. Except, leave %_content as
! 125535: ** is if bContent is false. */
! 125536: assert( p->zContentTbl==0 || bContent==0 );
! 125537: if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
! 125538: fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
! 125539: fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
! 125540: if( p->bHasDocsize ){
! 125541: fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
! 125542: }
! 125543: if( p->bHasStat ){
! 125544: fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
! 125545: }
! 125546: return rc;
! 125547: }
! 125548:
! 125549: /*
! 125550: ** The first element in the apVal[] array is assumed to contain the docid
! 125551: ** (an integer) of a row about to be deleted. Remove all terms from the
! 125552: ** full-text index.
! 125553: */
! 125554: static void fts3DeleteTerms(
! 125555: int *pRC, /* Result code */
! 125556: Fts3Table *p, /* The FTS table to delete from */
! 125557: sqlite3_value *pRowid, /* The docid to be deleted */
! 125558: u32 *aSz /* Sizes of deleted document written here */
! 125559: ){
! 125560: int rc;
! 125561: sqlite3_stmt *pSelect;
! 125562:
! 125563: if( *pRC ) return;
! 125564: rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
! 125565: if( rc==SQLITE_OK ){
! 125566: if( SQLITE_ROW==sqlite3_step(pSelect) ){
! 125567: int i;
! 125568: for(i=1; i<=p->nColumn; i++){
! 125569: const char *zText = (const char *)sqlite3_column_text(pSelect, i);
! 125570: rc = fts3PendingTermsAdd(p, zText, -1, &aSz[i-1]);
! 125571: if( rc!=SQLITE_OK ){
! 125572: sqlite3_reset(pSelect);
! 125573: *pRC = rc;
! 125574: return;
! 125575: }
! 125576: aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
! 125577: }
! 125578: }
! 125579: rc = sqlite3_reset(pSelect);
! 125580: }else{
! 125581: sqlite3_reset(pSelect);
! 125582: }
! 125583: *pRC = rc;
! 125584: }
! 125585:
! 125586: /*
! 125587: ** Forward declaration to account for the circular dependency between
! 125588: ** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
! 125589: */
! 125590: static int fts3SegmentMerge(Fts3Table *, int, int);
! 125591:
! 125592: /*
! 125593: ** This function allocates a new level iLevel index in the segdir table.
! 125594: ** Usually, indexes are allocated within a level sequentially starting
! 125595: ** with 0, so the allocated index is one greater than the value returned
! 125596: ** by:
! 125597: **
! 125598: ** SELECT max(idx) FROM %_segdir WHERE level = :iLevel
! 125599: **
! 125600: ** However, if there are already FTS3_MERGE_COUNT indexes at the requested
! 125601: ** level, they are merged into a single level (iLevel+1) segment and the
! 125602: ** allocated index is 0.
! 125603: **
! 125604: ** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
! 125605: ** returned. Otherwise, an SQLite error code is returned.
! 125606: */
! 125607: static int fts3AllocateSegdirIdx(
! 125608: Fts3Table *p,
! 125609: int iIndex, /* Index for p->aIndex */
! 125610: int iLevel,
! 125611: int *piIdx
! 125612: ){
! 125613: int rc; /* Return Code */
! 125614: sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */
! 125615: int iNext = 0; /* Result of query pNextIdx */
! 125616:
! 125617: /* Set variable iNext to the next available segdir index at level iLevel. */
! 125618: rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
! 125619: if( rc==SQLITE_OK ){
! 125620: sqlite3_bind_int(pNextIdx, 1, iIndex*FTS3_SEGDIR_MAXLEVEL + iLevel);
! 125621: if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
! 125622: iNext = sqlite3_column_int(pNextIdx, 0);
! 125623: }
! 125624: rc = sqlite3_reset(pNextIdx);
! 125625: }
! 125626:
! 125627: if( rc==SQLITE_OK ){
! 125628: /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
! 125629: ** full, merge all segments in level iLevel into a single iLevel+1
! 125630: ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
! 125631: ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
! 125632: */
! 125633: if( iNext>=FTS3_MERGE_COUNT ){
! 125634: rc = fts3SegmentMerge(p, iIndex, iLevel);
! 125635: *piIdx = 0;
! 125636: }else{
! 125637: *piIdx = iNext;
! 125638: }
! 125639: }
! 125640:
! 125641: return rc;
! 125642: }
! 125643:
! 125644: /*
! 125645: ** The %_segments table is declared as follows:
! 125646: **
! 125647: ** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
! 125648: **
! 125649: ** This function reads data from a single row of the %_segments table. The
! 125650: ** specific row is identified by the iBlockid parameter. If paBlob is not
! 125651: ** NULL, then a buffer is allocated using sqlite3_malloc() and populated
! 125652: ** with the contents of the blob stored in the "block" column of the
! 125653: ** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
! 125654: ** to the size of the blob in bytes before returning.
! 125655: **
! 125656: ** If an error occurs, or the table does not contain the specified row,
! 125657: ** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
! 125658: ** paBlob is non-NULL, then it is the responsibility of the caller to
! 125659: ** eventually free the returned buffer.
! 125660: **
! 125661: ** This function may leave an open sqlite3_blob* handle in the
! 125662: ** Fts3Table.pSegments variable. This handle is reused by subsequent calls
! 125663: ** to this function. The handle may be closed by calling the
! 125664: ** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
! 125665: ** performance improvement, but the blob handle should always be closed
! 125666: ** before control is returned to the user (to prevent a lock being held
! 125667: ** on the database file for longer than necessary). Thus, any virtual table
! 125668: ** method (xFilter etc.) that may directly or indirectly call this function
! 125669: ** must call sqlite3Fts3SegmentsClose() before returning.
! 125670: */
! 125671: SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
! 125672: Fts3Table *p, /* FTS3 table handle */
! 125673: sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */
! 125674: char **paBlob, /* OUT: Blob data in malloc'd buffer */
! 125675: int *pnBlob, /* OUT: Size of blob data */
! 125676: int *pnLoad /* OUT: Bytes actually loaded */
! 125677: ){
! 125678: int rc; /* Return code */
! 125679:
! 125680: /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
! 125681: assert( pnBlob);
! 125682:
! 125683: if( p->pSegments ){
! 125684: rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
! 125685: }else{
! 125686: if( 0==p->zSegmentsTbl ){
! 125687: p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
! 125688: if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
! 125689: }
! 125690: rc = sqlite3_blob_open(
! 125691: p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
! 125692: );
! 125693: }
! 125694:
! 125695: if( rc==SQLITE_OK ){
! 125696: int nByte = sqlite3_blob_bytes(p->pSegments);
! 125697: *pnBlob = nByte;
! 125698: if( paBlob ){
! 125699: char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
! 125700: if( !aByte ){
! 125701: rc = SQLITE_NOMEM;
! 125702: }else{
! 125703: if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
! 125704: nByte = FTS3_NODE_CHUNKSIZE;
! 125705: *pnLoad = nByte;
! 125706: }
! 125707: rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
! 125708: memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
! 125709: if( rc!=SQLITE_OK ){
! 125710: sqlite3_free(aByte);
! 125711: aByte = 0;
! 125712: }
! 125713: }
! 125714: *paBlob = aByte;
! 125715: }
! 125716: }
! 125717:
! 125718: return rc;
! 125719: }
! 125720:
! 125721: /*
! 125722: ** Close the blob handle at p->pSegments, if it is open. See comments above
! 125723: ** the sqlite3Fts3ReadBlock() function for details.
! 125724: */
! 125725: SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
! 125726: sqlite3_blob_close(p->pSegments);
! 125727: p->pSegments = 0;
! 125728: }
! 125729:
! 125730: static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
! 125731: int nRead; /* Number of bytes to read */
! 125732: int rc; /* Return code */
! 125733:
! 125734: nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
! 125735: rc = sqlite3_blob_read(
! 125736: pReader->pBlob,
! 125737: &pReader->aNode[pReader->nPopulate],
! 125738: nRead,
! 125739: pReader->nPopulate
! 125740: );
! 125741:
! 125742: if( rc==SQLITE_OK ){
! 125743: pReader->nPopulate += nRead;
! 125744: memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
! 125745: if( pReader->nPopulate==pReader->nNode ){
! 125746: sqlite3_blob_close(pReader->pBlob);
! 125747: pReader->pBlob = 0;
! 125748: pReader->nPopulate = 0;
! 125749: }
! 125750: }
! 125751: return rc;
! 125752: }
! 125753:
! 125754: static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
! 125755: int rc = SQLITE_OK;
! 125756: assert( !pReader->pBlob
! 125757: || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
! 125758: );
! 125759: while( pReader->pBlob && rc==SQLITE_OK
! 125760: && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
! 125761: ){
! 125762: rc = fts3SegReaderIncrRead(pReader);
! 125763: }
! 125764: return rc;
! 125765: }
! 125766:
! 125767: /*
! 125768: ** Move the iterator passed as the first argument to the next term in the
! 125769: ** segment. If successful, SQLITE_OK is returned. If there is no next term,
! 125770: ** SQLITE_DONE. Otherwise, an SQLite error code.
! 125771: */
! 125772: static int fts3SegReaderNext(
! 125773: Fts3Table *p,
! 125774: Fts3SegReader *pReader,
! 125775: int bIncr
! 125776: ){
! 125777: int rc; /* Return code of various sub-routines */
! 125778: char *pNext; /* Cursor variable */
! 125779: int nPrefix; /* Number of bytes in term prefix */
! 125780: int nSuffix; /* Number of bytes in term suffix */
! 125781:
! 125782: if( !pReader->aDoclist ){
! 125783: pNext = pReader->aNode;
! 125784: }else{
! 125785: pNext = &pReader->aDoclist[pReader->nDoclist];
! 125786: }
! 125787:
! 125788: if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
! 125789:
! 125790: if( fts3SegReaderIsPending(pReader) ){
! 125791: Fts3HashElem *pElem = *(pReader->ppNextElem);
! 125792: if( pElem==0 ){
! 125793: pReader->aNode = 0;
! 125794: }else{
! 125795: PendingList *pList = (PendingList *)fts3HashData(pElem);
! 125796: pReader->zTerm = (char *)fts3HashKey(pElem);
! 125797: pReader->nTerm = fts3HashKeysize(pElem);
! 125798: pReader->nNode = pReader->nDoclist = pList->nData + 1;
! 125799: pReader->aNode = pReader->aDoclist = pList->aData;
! 125800: pReader->ppNextElem++;
! 125801: assert( pReader->aNode );
! 125802: }
! 125803: return SQLITE_OK;
! 125804: }
! 125805:
! 125806: if( !fts3SegReaderIsRootOnly(pReader) ){
! 125807: sqlite3_free(pReader->aNode);
! 125808: sqlite3_blob_close(pReader->pBlob);
! 125809: pReader->pBlob = 0;
! 125810: }
! 125811: pReader->aNode = 0;
! 125812:
! 125813: /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
! 125814: ** blocks have already been traversed. */
! 125815: assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
! 125816: if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
! 125817: return SQLITE_OK;
! 125818: }
! 125819:
! 125820: rc = sqlite3Fts3ReadBlock(
! 125821: p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
! 125822: (bIncr ? &pReader->nPopulate : 0)
! 125823: );
! 125824: if( rc!=SQLITE_OK ) return rc;
! 125825: assert( pReader->pBlob==0 );
! 125826: if( bIncr && pReader->nPopulate<pReader->nNode ){
! 125827: pReader->pBlob = p->pSegments;
! 125828: p->pSegments = 0;
! 125829: }
! 125830: pNext = pReader->aNode;
! 125831: }
! 125832:
! 125833: assert( !fts3SegReaderIsPending(pReader) );
! 125834:
! 125835: rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
! 125836: if( rc!=SQLITE_OK ) return rc;
! 125837:
! 125838: /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
! 125839: ** safe (no risk of overread) even if the node data is corrupted. */
! 125840: pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix);
! 125841: pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix);
! 125842: if( nPrefix<0 || nSuffix<=0
! 125843: || &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
! 125844: ){
! 125845: return FTS_CORRUPT_VTAB;
! 125846: }
! 125847:
! 125848: if( nPrefix+nSuffix>pReader->nTermAlloc ){
! 125849: int nNew = (nPrefix+nSuffix)*2;
! 125850: char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
! 125851: if( !zNew ){
! 125852: return SQLITE_NOMEM;
! 125853: }
! 125854: pReader->zTerm = zNew;
! 125855: pReader->nTermAlloc = nNew;
! 125856: }
! 125857:
! 125858: rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
! 125859: if( rc!=SQLITE_OK ) return rc;
! 125860:
! 125861: memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
! 125862: pReader->nTerm = nPrefix+nSuffix;
! 125863: pNext += nSuffix;
! 125864: pNext += sqlite3Fts3GetVarint32(pNext, &pReader->nDoclist);
! 125865: pReader->aDoclist = pNext;
! 125866: pReader->pOffsetList = 0;
! 125867:
! 125868: /* Check that the doclist does not appear to extend past the end of the
! 125869: ** b-tree node. And that the final byte of the doclist is 0x00. If either
! 125870: ** of these statements is untrue, then the data structure is corrupt.
! 125871: */
! 125872: if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
! 125873: || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
! 125874: ){
! 125875: return FTS_CORRUPT_VTAB;
! 125876: }
! 125877: return SQLITE_OK;
! 125878: }
! 125879:
! 125880: /*
! 125881: ** Set the SegReader to point to the first docid in the doclist associated
! 125882: ** with the current term.
! 125883: */
! 125884: static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
! 125885: int rc = SQLITE_OK;
! 125886: assert( pReader->aDoclist );
! 125887: assert( !pReader->pOffsetList );
! 125888: if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
! 125889: u8 bEof = 0;
! 125890: pReader->iDocid = 0;
! 125891: pReader->nOffsetList = 0;
! 125892: sqlite3Fts3DoclistPrev(0,
! 125893: pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
! 125894: &pReader->iDocid, &pReader->nOffsetList, &bEof
! 125895: );
! 125896: }else{
! 125897: rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
! 125898: if( rc==SQLITE_OK ){
! 125899: int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
! 125900: pReader->pOffsetList = &pReader->aDoclist[n];
! 125901: }
! 125902: }
! 125903: return rc;
! 125904: }
! 125905:
! 125906: /*
! 125907: ** Advance the SegReader to point to the next docid in the doclist
! 125908: ** associated with the current term.
! 125909: **
! 125910: ** If arguments ppOffsetList and pnOffsetList are not NULL, then
! 125911: ** *ppOffsetList is set to point to the first column-offset list
! 125912: ** in the doclist entry (i.e. immediately past the docid varint).
! 125913: ** *pnOffsetList is set to the length of the set of column-offset
! 125914: ** lists, not including the nul-terminator byte. For example:
! 125915: */
! 125916: static int fts3SegReaderNextDocid(
! 125917: Fts3Table *pTab,
! 125918: Fts3SegReader *pReader, /* Reader to advance to next docid */
! 125919: char **ppOffsetList, /* OUT: Pointer to current position-list */
! 125920: int *pnOffsetList /* OUT: Length of *ppOffsetList in bytes */
! 125921: ){
! 125922: int rc = SQLITE_OK;
! 125923: char *p = pReader->pOffsetList;
! 125924: char c = 0;
! 125925:
! 125926: assert( p );
! 125927:
! 125928: if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
! 125929: /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
! 125930: ** Pending-terms doclists are always built up in ascending order, so
! 125931: ** we have to iterate through them backwards here. */
! 125932: u8 bEof = 0;
! 125933: if( ppOffsetList ){
! 125934: *ppOffsetList = pReader->pOffsetList;
! 125935: *pnOffsetList = pReader->nOffsetList - 1;
! 125936: }
! 125937: sqlite3Fts3DoclistPrev(0,
! 125938: pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
! 125939: &pReader->nOffsetList, &bEof
! 125940: );
! 125941: if( bEof ){
! 125942: pReader->pOffsetList = 0;
! 125943: }else{
! 125944: pReader->pOffsetList = p;
! 125945: }
! 125946: }else{
! 125947: char *pEnd = &pReader->aDoclist[pReader->nDoclist];
! 125948:
! 125949: /* Pointer p currently points at the first byte of an offset list. The
! 125950: ** following block advances it to point one byte past the end of
! 125951: ** the same offset list. */
! 125952: while( 1 ){
! 125953:
! 125954: /* The following line of code (and the "p++" below the while() loop) is
! 125955: ** normally all that is required to move pointer p to the desired
! 125956: ** position. The exception is if this node is being loaded from disk
! 125957: ** incrementally and pointer "p" now points to the first byte passed
! 125958: ** the populated part of pReader->aNode[].
! 125959: */
! 125960: while( *p | c ) c = *p++ & 0x80;
! 125961: assert( *p==0 );
! 125962:
! 125963: if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
! 125964: rc = fts3SegReaderIncrRead(pReader);
! 125965: if( rc!=SQLITE_OK ) return rc;
! 125966: }
! 125967: p++;
! 125968:
! 125969: /* If required, populate the output variables with a pointer to and the
! 125970: ** size of the previous offset-list.
! 125971: */
! 125972: if( ppOffsetList ){
! 125973: *ppOffsetList = pReader->pOffsetList;
! 125974: *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
! 125975: }
! 125976:
! 125977: while( p<pEnd && *p==0 ) p++;
! 125978:
! 125979: /* If there are no more entries in the doclist, set pOffsetList to
! 125980: ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
! 125981: ** Fts3SegReader.pOffsetList to point to the next offset list before
! 125982: ** returning.
! 125983: */
! 125984: if( p>=pEnd ){
! 125985: pReader->pOffsetList = 0;
! 125986: }else{
! 125987: rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
! 125988: if( rc==SQLITE_OK ){
! 125989: sqlite3_int64 iDelta;
! 125990: pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
! 125991: if( pTab->bDescIdx ){
! 125992: pReader->iDocid -= iDelta;
! 125993: }else{
! 125994: pReader->iDocid += iDelta;
! 125995: }
! 125996: }
! 125997: }
! 125998: }
! 125999:
! 126000: return SQLITE_OK;
! 126001: }
! 126002:
! 126003:
! 126004: SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
! 126005: Fts3Cursor *pCsr,
! 126006: Fts3MultiSegReader *pMsr,
! 126007: int *pnOvfl
! 126008: ){
! 126009: Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
! 126010: int nOvfl = 0;
! 126011: int ii;
! 126012: int rc = SQLITE_OK;
! 126013: int pgsz = p->nPgsz;
! 126014:
! 126015: assert( p->bHasStat );
! 126016: assert( pgsz>0 );
! 126017:
! 126018: for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
! 126019: Fts3SegReader *pReader = pMsr->apSegment[ii];
! 126020: if( !fts3SegReaderIsPending(pReader)
! 126021: && !fts3SegReaderIsRootOnly(pReader)
! 126022: ){
! 126023: sqlite3_int64 jj;
! 126024: for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
! 126025: int nBlob;
! 126026: rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
! 126027: if( rc!=SQLITE_OK ) break;
! 126028: if( (nBlob+35)>pgsz ){
! 126029: nOvfl += (nBlob + 34)/pgsz;
! 126030: }
! 126031: }
! 126032: }
! 126033: }
! 126034: *pnOvfl = nOvfl;
! 126035: return rc;
! 126036: }
! 126037:
! 126038: /*
! 126039: ** Free all allocations associated with the iterator passed as the
! 126040: ** second argument.
! 126041: */
! 126042: SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
! 126043: if( pReader && !fts3SegReaderIsPending(pReader) ){
! 126044: sqlite3_free(pReader->zTerm);
! 126045: if( !fts3SegReaderIsRootOnly(pReader) ){
! 126046: sqlite3_free(pReader->aNode);
! 126047: sqlite3_blob_close(pReader->pBlob);
! 126048: }
! 126049: }
! 126050: sqlite3_free(pReader);
! 126051: }
! 126052:
! 126053: /*
! 126054: ** Allocate a new SegReader object.
! 126055: */
! 126056: SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
! 126057: int iAge, /* Segment "age". */
! 126058: sqlite3_int64 iStartLeaf, /* First leaf to traverse */
! 126059: sqlite3_int64 iEndLeaf, /* Final leaf to traverse */
! 126060: sqlite3_int64 iEndBlock, /* Final block of segment */
! 126061: const char *zRoot, /* Buffer containing root node */
! 126062: int nRoot, /* Size of buffer containing root node */
! 126063: Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
! 126064: ){
! 126065: Fts3SegReader *pReader; /* Newly allocated SegReader object */
! 126066: int nExtra = 0; /* Bytes to allocate segment root node */
! 126067:
! 126068: assert( iStartLeaf<=iEndLeaf );
! 126069: if( iStartLeaf==0 ){
! 126070: nExtra = nRoot + FTS3_NODE_PADDING;
! 126071: }
! 126072:
! 126073: pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
! 126074: if( !pReader ){
! 126075: return SQLITE_NOMEM;
! 126076: }
! 126077: memset(pReader, 0, sizeof(Fts3SegReader));
! 126078: pReader->iIdx = iAge;
! 126079: pReader->iStartBlock = iStartLeaf;
! 126080: pReader->iLeafEndBlock = iEndLeaf;
! 126081: pReader->iEndBlock = iEndBlock;
! 126082:
! 126083: if( nExtra ){
! 126084: /* The entire segment is stored in the root node. */
! 126085: pReader->aNode = (char *)&pReader[1];
! 126086: pReader->nNode = nRoot;
! 126087: memcpy(pReader->aNode, zRoot, nRoot);
! 126088: memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
! 126089: }else{
! 126090: pReader->iCurrentBlock = iStartLeaf-1;
! 126091: }
! 126092: *ppReader = pReader;
! 126093: return SQLITE_OK;
! 126094: }
! 126095:
! 126096: /*
! 126097: ** This is a comparison function used as a qsort() callback when sorting
! 126098: ** an array of pending terms by term. This occurs as part of flushing
! 126099: ** the contents of the pending-terms hash table to the database.
! 126100: */
! 126101: static int fts3CompareElemByTerm(const void *lhs, const void *rhs){
! 126102: char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
! 126103: char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
! 126104: int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
! 126105: int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
! 126106:
! 126107: int n = (n1<n2 ? n1 : n2);
! 126108: int c = memcmp(z1, z2, n);
! 126109: if( c==0 ){
! 126110: c = n1 - n2;
! 126111: }
! 126112: return c;
! 126113: }
! 126114:
! 126115: /*
! 126116: ** This function is used to allocate an Fts3SegReader that iterates through
! 126117: ** a subset of the terms stored in the Fts3Table.pendingTerms array.
! 126118: **
! 126119: ** If the isPrefixIter parameter is zero, then the returned SegReader iterates
! 126120: ** through each term in the pending-terms table. Or, if isPrefixIter is
! 126121: ** non-zero, it iterates through each term and its prefixes. For example, if
! 126122: ** the pending terms hash table contains the terms "sqlite", "mysql" and
! 126123: ** "firebird", then the iterator visits the following 'terms' (in the order
! 126124: ** shown):
! 126125: **
! 126126: ** f fi fir fire fireb firebi firebir firebird
! 126127: ** m my mys mysq mysql
! 126128: ** s sq sql sqli sqlit sqlite
! 126129: **
! 126130: ** Whereas if isPrefixIter is zero, the terms visited are:
! 126131: **
! 126132: ** firebird mysql sqlite
! 126133: */
! 126134: SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
! 126135: Fts3Table *p, /* Virtual table handle */
! 126136: int iIndex, /* Index for p->aIndex */
! 126137: const char *zTerm, /* Term to search for */
! 126138: int nTerm, /* Size of buffer zTerm */
! 126139: int bPrefix, /* True for a prefix iterator */
! 126140: Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
! 126141: ){
! 126142: Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
! 126143: Fts3HashElem *pE; /* Iterator variable */
! 126144: Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
! 126145: int nElem = 0; /* Size of array at aElem */
! 126146: int rc = SQLITE_OK; /* Return Code */
! 126147: Fts3Hash *pHash;
! 126148:
! 126149: pHash = &p->aIndex[iIndex].hPending;
! 126150: if( bPrefix ){
! 126151: int nAlloc = 0; /* Size of allocated array at aElem */
! 126152:
! 126153: for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
! 126154: char *zKey = (char *)fts3HashKey(pE);
! 126155: int nKey = fts3HashKeysize(pE);
! 126156: if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
! 126157: if( nElem==nAlloc ){
! 126158: Fts3HashElem **aElem2;
! 126159: nAlloc += 16;
! 126160: aElem2 = (Fts3HashElem **)sqlite3_realloc(
! 126161: aElem, nAlloc*sizeof(Fts3HashElem *)
! 126162: );
! 126163: if( !aElem2 ){
! 126164: rc = SQLITE_NOMEM;
! 126165: nElem = 0;
! 126166: break;
! 126167: }
! 126168: aElem = aElem2;
! 126169: }
! 126170:
! 126171: aElem[nElem++] = pE;
! 126172: }
! 126173: }
! 126174:
! 126175: /* If more than one term matches the prefix, sort the Fts3HashElem
! 126176: ** objects in term order using qsort(). This uses the same comparison
! 126177: ** callback as is used when flushing terms to disk.
! 126178: */
! 126179: if( nElem>1 ){
! 126180: qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
! 126181: }
! 126182:
! 126183: }else{
! 126184: /* The query is a simple term lookup that matches at most one term in
! 126185: ** the index. All that is required is a straight hash-lookup.
! 126186: **
! 126187: ** Because the stack address of pE may be accessed via the aElem pointer
! 126188: ** below, the "Fts3HashElem *pE" must be declared so that it is valid
! 126189: ** within this entire function, not just this "else{...}" block.
! 126190: */
! 126191: pE = fts3HashFindElem(pHash, zTerm, nTerm);
! 126192: if( pE ){
! 126193: aElem = &pE;
! 126194: nElem = 1;
! 126195: }
! 126196: }
! 126197:
! 126198: if( nElem>0 ){
! 126199: int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
! 126200: pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
! 126201: if( !pReader ){
! 126202: rc = SQLITE_NOMEM;
! 126203: }else{
! 126204: memset(pReader, 0, nByte);
! 126205: pReader->iIdx = 0x7FFFFFFF;
! 126206: pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
! 126207: memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
! 126208: }
! 126209: }
! 126210:
! 126211: if( bPrefix ){
! 126212: sqlite3_free(aElem);
! 126213: }
! 126214: *ppReader = pReader;
! 126215: return rc;
! 126216: }
! 126217:
! 126218: /*
! 126219: ** Compare the entries pointed to by two Fts3SegReader structures.
! 126220: ** Comparison is as follows:
! 126221: **
! 126222: ** 1) EOF is greater than not EOF.
! 126223: **
! 126224: ** 2) The current terms (if any) are compared using memcmp(). If one
! 126225: ** term is a prefix of another, the longer term is considered the
! 126226: ** larger.
! 126227: **
! 126228: ** 3) By segment age. An older segment is considered larger.
! 126229: */
! 126230: static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
! 126231: int rc;
! 126232: if( pLhs->aNode && pRhs->aNode ){
! 126233: int rc2 = pLhs->nTerm - pRhs->nTerm;
! 126234: if( rc2<0 ){
! 126235: rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
! 126236: }else{
! 126237: rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
! 126238: }
! 126239: if( rc==0 ){
! 126240: rc = rc2;
! 126241: }
! 126242: }else{
! 126243: rc = (pLhs->aNode==0) - (pRhs->aNode==0);
! 126244: }
! 126245: if( rc==0 ){
! 126246: rc = pRhs->iIdx - pLhs->iIdx;
! 126247: }
! 126248: assert( rc!=0 );
! 126249: return rc;
! 126250: }
! 126251:
! 126252: /*
! 126253: ** A different comparison function for SegReader structures. In this
! 126254: ** version, it is assumed that each SegReader points to an entry in
! 126255: ** a doclist for identical terms. Comparison is made as follows:
! 126256: **
! 126257: ** 1) EOF (end of doclist in this case) is greater than not EOF.
! 126258: **
! 126259: ** 2) By current docid.
! 126260: **
! 126261: ** 3) By segment age. An older segment is considered larger.
! 126262: */
! 126263: static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
! 126264: int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
! 126265: if( rc==0 ){
! 126266: if( pLhs->iDocid==pRhs->iDocid ){
! 126267: rc = pRhs->iIdx - pLhs->iIdx;
! 126268: }else{
! 126269: rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
! 126270: }
! 126271: }
! 126272: assert( pLhs->aNode && pRhs->aNode );
! 126273: return rc;
! 126274: }
! 126275: static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
! 126276: int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
! 126277: if( rc==0 ){
! 126278: if( pLhs->iDocid==pRhs->iDocid ){
! 126279: rc = pRhs->iIdx - pLhs->iIdx;
! 126280: }else{
! 126281: rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
! 126282: }
! 126283: }
! 126284: assert( pLhs->aNode && pRhs->aNode );
! 126285: return rc;
! 126286: }
! 126287:
! 126288: /*
! 126289: ** Compare the term that the Fts3SegReader object passed as the first argument
! 126290: ** points to with the term specified by arguments zTerm and nTerm.
! 126291: **
! 126292: ** If the pSeg iterator is already at EOF, return 0. Otherwise, return
! 126293: ** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
! 126294: ** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
! 126295: */
! 126296: static int fts3SegReaderTermCmp(
! 126297: Fts3SegReader *pSeg, /* Segment reader object */
! 126298: const char *zTerm, /* Term to compare to */
! 126299: int nTerm /* Size of term zTerm in bytes */
! 126300: ){
! 126301: int res = 0;
! 126302: if( pSeg->aNode ){
! 126303: if( pSeg->nTerm>nTerm ){
! 126304: res = memcmp(pSeg->zTerm, zTerm, nTerm);
! 126305: }else{
! 126306: res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
! 126307: }
! 126308: if( res==0 ){
! 126309: res = pSeg->nTerm-nTerm;
! 126310: }
! 126311: }
! 126312: return res;
! 126313: }
! 126314:
! 126315: /*
! 126316: ** Argument apSegment is an array of nSegment elements. It is known that
! 126317: ** the final (nSegment-nSuspect) members are already in sorted order
! 126318: ** (according to the comparison function provided). This function shuffles
! 126319: ** the array around until all entries are in sorted order.
! 126320: */
! 126321: static void fts3SegReaderSort(
! 126322: Fts3SegReader **apSegment, /* Array to sort entries of */
! 126323: int nSegment, /* Size of apSegment array */
! 126324: int nSuspect, /* Unsorted entry count */
! 126325: int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */
! 126326: ){
! 126327: int i; /* Iterator variable */
! 126328:
! 126329: assert( nSuspect<=nSegment );
! 126330:
! 126331: if( nSuspect==nSegment ) nSuspect--;
! 126332: for(i=nSuspect-1; i>=0; i--){
! 126333: int j;
! 126334: for(j=i; j<(nSegment-1); j++){
! 126335: Fts3SegReader *pTmp;
! 126336: if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
! 126337: pTmp = apSegment[j+1];
! 126338: apSegment[j+1] = apSegment[j];
! 126339: apSegment[j] = pTmp;
! 126340: }
! 126341: }
! 126342:
! 126343: #ifndef NDEBUG
! 126344: /* Check that the list really is sorted now. */
! 126345: for(i=0; i<(nSuspect-1); i++){
! 126346: assert( xCmp(apSegment[i], apSegment[i+1])<0 );
! 126347: }
! 126348: #endif
! 126349: }
! 126350:
! 126351: /*
! 126352: ** Insert a record into the %_segments table.
! 126353: */
! 126354: static int fts3WriteSegment(
! 126355: Fts3Table *p, /* Virtual table handle */
! 126356: sqlite3_int64 iBlock, /* Block id for new block */
! 126357: char *z, /* Pointer to buffer containing block data */
! 126358: int n /* Size of buffer z in bytes */
! 126359: ){
! 126360: sqlite3_stmt *pStmt;
! 126361: int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
! 126362: if( rc==SQLITE_OK ){
! 126363: sqlite3_bind_int64(pStmt, 1, iBlock);
! 126364: sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
! 126365: sqlite3_step(pStmt);
! 126366: rc = sqlite3_reset(pStmt);
! 126367: }
! 126368: return rc;
! 126369: }
! 126370:
! 126371: /*
! 126372: ** Insert a record into the %_segdir table.
! 126373: */
! 126374: static int fts3WriteSegdir(
! 126375: Fts3Table *p, /* Virtual table handle */
! 126376: int iLevel, /* Value for "level" field */
! 126377: int iIdx, /* Value for "idx" field */
! 126378: sqlite3_int64 iStartBlock, /* Value for "start_block" field */
! 126379: sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
! 126380: sqlite3_int64 iEndBlock, /* Value for "end_block" field */
! 126381: char *zRoot, /* Blob value for "root" field */
! 126382: int nRoot /* Number of bytes in buffer zRoot */
! 126383: ){
! 126384: sqlite3_stmt *pStmt;
! 126385: int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
! 126386: if( rc==SQLITE_OK ){
! 126387: sqlite3_bind_int(pStmt, 1, iLevel);
! 126388: sqlite3_bind_int(pStmt, 2, iIdx);
! 126389: sqlite3_bind_int64(pStmt, 3, iStartBlock);
! 126390: sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
! 126391: sqlite3_bind_int64(pStmt, 5, iEndBlock);
! 126392: sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
! 126393: sqlite3_step(pStmt);
! 126394: rc = sqlite3_reset(pStmt);
! 126395: }
! 126396: return rc;
! 126397: }
! 126398:
! 126399: /*
! 126400: ** Return the size of the common prefix (if any) shared by zPrev and
! 126401: ** zNext, in bytes. For example,
! 126402: **
! 126403: ** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3
! 126404: ** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2
! 126405: ** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0
! 126406: */
! 126407: static int fts3PrefixCompress(
! 126408: const char *zPrev, /* Buffer containing previous term */
! 126409: int nPrev, /* Size of buffer zPrev in bytes */
! 126410: const char *zNext, /* Buffer containing next term */
! 126411: int nNext /* Size of buffer zNext in bytes */
! 126412: ){
! 126413: int n;
! 126414: UNUSED_PARAMETER(nNext);
! 126415: for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
! 126416: return n;
! 126417: }
! 126418:
! 126419: /*
! 126420: ** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
! 126421: ** (according to memcmp) than the previous term.
! 126422: */
! 126423: static int fts3NodeAddTerm(
! 126424: Fts3Table *p, /* Virtual table handle */
! 126425: SegmentNode **ppTree, /* IN/OUT: SegmentNode handle */
! 126426: int isCopyTerm, /* True if zTerm/nTerm is transient */
! 126427: const char *zTerm, /* Pointer to buffer containing term */
! 126428: int nTerm /* Size of term in bytes */
! 126429: ){
! 126430: SegmentNode *pTree = *ppTree;
! 126431: int rc;
! 126432: SegmentNode *pNew;
! 126433:
! 126434: /* First try to append the term to the current node. Return early if
! 126435: ** this is possible.
! 126436: */
! 126437: if( pTree ){
! 126438: int nData = pTree->nData; /* Current size of node in bytes */
! 126439: int nReq = nData; /* Required space after adding zTerm */
! 126440: int nPrefix; /* Number of bytes of prefix compression */
! 126441: int nSuffix; /* Suffix length */
! 126442:
! 126443: nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
! 126444: nSuffix = nTerm-nPrefix;
! 126445:
! 126446: nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
! 126447: if( nReq<=p->nNodeSize || !pTree->zTerm ){
! 126448:
! 126449: if( nReq>p->nNodeSize ){
! 126450: /* An unusual case: this is the first term to be added to the node
! 126451: ** and the static node buffer (p->nNodeSize bytes) is not large
! 126452: ** enough. Use a separately malloced buffer instead This wastes
! 126453: ** p->nNodeSize bytes, but since this scenario only comes about when
! 126454: ** the database contain two terms that share a prefix of almost 2KB,
! 126455: ** this is not expected to be a serious problem.
! 126456: */
! 126457: assert( pTree->aData==(char *)&pTree[1] );
! 126458: pTree->aData = (char *)sqlite3_malloc(nReq);
! 126459: if( !pTree->aData ){
! 126460: return SQLITE_NOMEM;
! 126461: }
! 126462: }
! 126463:
! 126464: if( pTree->zTerm ){
! 126465: /* There is no prefix-length field for first term in a node */
! 126466: nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
! 126467: }
! 126468:
! 126469: nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
! 126470: memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
! 126471: pTree->nData = nData + nSuffix;
! 126472: pTree->nEntry++;
! 126473:
! 126474: if( isCopyTerm ){
! 126475: if( pTree->nMalloc<nTerm ){
! 126476: char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
! 126477: if( !zNew ){
! 126478: return SQLITE_NOMEM;
! 126479: }
! 126480: pTree->nMalloc = nTerm*2;
! 126481: pTree->zMalloc = zNew;
! 126482: }
! 126483: pTree->zTerm = pTree->zMalloc;
! 126484: memcpy(pTree->zTerm, zTerm, nTerm);
! 126485: pTree->nTerm = nTerm;
! 126486: }else{
! 126487: pTree->zTerm = (char *)zTerm;
! 126488: pTree->nTerm = nTerm;
! 126489: }
! 126490: return SQLITE_OK;
! 126491: }
! 126492: }
! 126493:
! 126494: /* If control flows to here, it was not possible to append zTerm to the
! 126495: ** current node. Create a new node (a right-sibling of the current node).
! 126496: ** If this is the first node in the tree, the term is added to it.
! 126497: **
! 126498: ** Otherwise, the term is not added to the new node, it is left empty for
! 126499: ** now. Instead, the term is inserted into the parent of pTree. If pTree
! 126500: ** has no parent, one is created here.
! 126501: */
! 126502: pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
! 126503: if( !pNew ){
! 126504: return SQLITE_NOMEM;
! 126505: }
! 126506: memset(pNew, 0, sizeof(SegmentNode));
! 126507: pNew->nData = 1 + FTS3_VARINT_MAX;
! 126508: pNew->aData = (char *)&pNew[1];
! 126509:
! 126510: if( pTree ){
! 126511: SegmentNode *pParent = pTree->pParent;
! 126512: rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
! 126513: if( pTree->pParent==0 ){
! 126514: pTree->pParent = pParent;
! 126515: }
! 126516: pTree->pRight = pNew;
! 126517: pNew->pLeftmost = pTree->pLeftmost;
! 126518: pNew->pParent = pParent;
! 126519: pNew->zMalloc = pTree->zMalloc;
! 126520: pNew->nMalloc = pTree->nMalloc;
! 126521: pTree->zMalloc = 0;
! 126522: }else{
! 126523: pNew->pLeftmost = pNew;
! 126524: rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
! 126525: }
! 126526:
! 126527: *ppTree = pNew;
! 126528: return rc;
! 126529: }
! 126530:
! 126531: /*
! 126532: ** Helper function for fts3NodeWrite().
! 126533: */
! 126534: static int fts3TreeFinishNode(
! 126535: SegmentNode *pTree,
! 126536: int iHeight,
! 126537: sqlite3_int64 iLeftChild
! 126538: ){
! 126539: int nStart;
! 126540: assert( iHeight>=1 && iHeight<128 );
! 126541: nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
! 126542: pTree->aData[nStart] = (char)iHeight;
! 126543: sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
! 126544: return nStart;
! 126545: }
! 126546:
! 126547: /*
! 126548: ** Write the buffer for the segment node pTree and all of its peers to the
! 126549: ** database. Then call this function recursively to write the parent of
! 126550: ** pTree and its peers to the database.
! 126551: **
! 126552: ** Except, if pTree is a root node, do not write it to the database. Instead,
! 126553: ** set output variables *paRoot and *pnRoot to contain the root node.
! 126554: **
! 126555: ** If successful, SQLITE_OK is returned and output variable *piLast is
! 126556: ** set to the largest blockid written to the database (or zero if no
! 126557: ** blocks were written to the db). Otherwise, an SQLite error code is
! 126558: ** returned.
! 126559: */
! 126560: static int fts3NodeWrite(
! 126561: Fts3Table *p, /* Virtual table handle */
! 126562: SegmentNode *pTree, /* SegmentNode handle */
! 126563: int iHeight, /* Height of this node in tree */
! 126564: sqlite3_int64 iLeaf, /* Block id of first leaf node */
! 126565: sqlite3_int64 iFree, /* Block id of next free slot in %_segments */
! 126566: sqlite3_int64 *piLast, /* OUT: Block id of last entry written */
! 126567: char **paRoot, /* OUT: Data for root node */
! 126568: int *pnRoot /* OUT: Size of root node in bytes */
! 126569: ){
! 126570: int rc = SQLITE_OK;
! 126571:
! 126572: if( !pTree->pParent ){
! 126573: /* Root node of the tree. */
! 126574: int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
! 126575: *piLast = iFree-1;
! 126576: *pnRoot = pTree->nData - nStart;
! 126577: *paRoot = &pTree->aData[nStart];
! 126578: }else{
! 126579: SegmentNode *pIter;
! 126580: sqlite3_int64 iNextFree = iFree;
! 126581: sqlite3_int64 iNextLeaf = iLeaf;
! 126582: for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
! 126583: int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
! 126584: int nWrite = pIter->nData - nStart;
! 126585:
! 126586: rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
! 126587: iNextFree++;
! 126588: iNextLeaf += (pIter->nEntry+1);
! 126589: }
! 126590: if( rc==SQLITE_OK ){
! 126591: assert( iNextLeaf==iFree );
! 126592: rc = fts3NodeWrite(
! 126593: p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
! 126594: );
! 126595: }
! 126596: }
! 126597:
! 126598: return rc;
! 126599: }
! 126600:
! 126601: /*
! 126602: ** Free all memory allocations associated with the tree pTree.
! 126603: */
! 126604: static void fts3NodeFree(SegmentNode *pTree){
! 126605: if( pTree ){
! 126606: SegmentNode *p = pTree->pLeftmost;
! 126607: fts3NodeFree(p->pParent);
! 126608: while( p ){
! 126609: SegmentNode *pRight = p->pRight;
! 126610: if( p->aData!=(char *)&p[1] ){
! 126611: sqlite3_free(p->aData);
! 126612: }
! 126613: assert( pRight==0 || p->zMalloc==0 );
! 126614: sqlite3_free(p->zMalloc);
! 126615: sqlite3_free(p);
! 126616: p = pRight;
! 126617: }
! 126618: }
! 126619: }
! 126620:
! 126621: /*
! 126622: ** Add a term to the segment being constructed by the SegmentWriter object
! 126623: ** *ppWriter. When adding the first term to a segment, *ppWriter should
! 126624: ** be passed NULL. This function will allocate a new SegmentWriter object
! 126625: ** and return it via the input/output variable *ppWriter in this case.
! 126626: **
! 126627: ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
! 126628: */
! 126629: static int fts3SegWriterAdd(
! 126630: Fts3Table *p, /* Virtual table handle */
! 126631: SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */
! 126632: int isCopyTerm, /* True if buffer zTerm must be copied */
! 126633: const char *zTerm, /* Pointer to buffer containing term */
! 126634: int nTerm, /* Size of term in bytes */
! 126635: const char *aDoclist, /* Pointer to buffer containing doclist */
! 126636: int nDoclist /* Size of doclist in bytes */
! 126637: ){
! 126638: int nPrefix; /* Size of term prefix in bytes */
! 126639: int nSuffix; /* Size of term suffix in bytes */
! 126640: int nReq; /* Number of bytes required on leaf page */
! 126641: int nData;
! 126642: SegmentWriter *pWriter = *ppWriter;
! 126643:
! 126644: if( !pWriter ){
! 126645: int rc;
! 126646: sqlite3_stmt *pStmt;
! 126647:
! 126648: /* Allocate the SegmentWriter structure */
! 126649: pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
! 126650: if( !pWriter ) return SQLITE_NOMEM;
! 126651: memset(pWriter, 0, sizeof(SegmentWriter));
! 126652: *ppWriter = pWriter;
! 126653:
! 126654: /* Allocate a buffer in which to accumulate data */
! 126655: pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
! 126656: if( !pWriter->aData ) return SQLITE_NOMEM;
! 126657: pWriter->nSize = p->nNodeSize;
! 126658:
! 126659: /* Find the next free blockid in the %_segments table */
! 126660: rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
! 126661: if( rc!=SQLITE_OK ) return rc;
! 126662: if( SQLITE_ROW==sqlite3_step(pStmt) ){
! 126663: pWriter->iFree = sqlite3_column_int64(pStmt, 0);
! 126664: pWriter->iFirst = pWriter->iFree;
! 126665: }
! 126666: rc = sqlite3_reset(pStmt);
! 126667: if( rc!=SQLITE_OK ) return rc;
! 126668: }
! 126669: nData = pWriter->nData;
! 126670:
! 126671: nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
! 126672: nSuffix = nTerm-nPrefix;
! 126673:
! 126674: /* Figure out how many bytes are required by this new entry */
! 126675: nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */
! 126676: sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */
! 126677: nSuffix + /* Term suffix */
! 126678: sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
! 126679: nDoclist; /* Doclist data */
! 126680:
! 126681: if( nData>0 && nData+nReq>p->nNodeSize ){
! 126682: int rc;
! 126683:
! 126684: /* The current leaf node is full. Write it out to the database. */
! 126685: rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
! 126686: if( rc!=SQLITE_OK ) return rc;
! 126687:
! 126688: /* Add the current term to the interior node tree. The term added to
! 126689: ** the interior tree must:
! 126690: **
! 126691: ** a) be greater than the largest term on the leaf node just written
! 126692: ** to the database (still available in pWriter->zTerm), and
! 126693: **
! 126694: ** b) be less than or equal to the term about to be added to the new
! 126695: ** leaf node (zTerm/nTerm).
! 126696: **
! 126697: ** In other words, it must be the prefix of zTerm 1 byte longer than
! 126698: ** the common prefix (if any) of zTerm and pWriter->zTerm.
! 126699: */
! 126700: assert( nPrefix<nTerm );
! 126701: rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
! 126702: if( rc!=SQLITE_OK ) return rc;
! 126703:
! 126704: nData = 0;
! 126705: pWriter->nTerm = 0;
! 126706:
! 126707: nPrefix = 0;
! 126708: nSuffix = nTerm;
! 126709: nReq = 1 + /* varint containing prefix size */
! 126710: sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */
! 126711: nTerm + /* Term suffix */
! 126712: sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
! 126713: nDoclist; /* Doclist data */
! 126714: }
! 126715:
! 126716: /* If the buffer currently allocated is too small for this entry, realloc
! 126717: ** the buffer to make it large enough.
! 126718: */
! 126719: if( nReq>pWriter->nSize ){
! 126720: char *aNew = sqlite3_realloc(pWriter->aData, nReq);
! 126721: if( !aNew ) return SQLITE_NOMEM;
! 126722: pWriter->aData = aNew;
! 126723: pWriter->nSize = nReq;
! 126724: }
! 126725: assert( nData+nReq<=pWriter->nSize );
! 126726:
! 126727: /* Append the prefix-compressed term and doclist to the buffer. */
! 126728: nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
! 126729: nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
! 126730: memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
! 126731: nData += nSuffix;
! 126732: nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
! 126733: memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
! 126734: pWriter->nData = nData + nDoclist;
! 126735:
! 126736: /* Save the current term so that it can be used to prefix-compress the next.
! 126737: ** If the isCopyTerm parameter is true, then the buffer pointed to by
! 126738: ** zTerm is transient, so take a copy of the term data. Otherwise, just
! 126739: ** store a copy of the pointer.
! 126740: */
! 126741: if( isCopyTerm ){
! 126742: if( nTerm>pWriter->nMalloc ){
! 126743: char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
! 126744: if( !zNew ){
! 126745: return SQLITE_NOMEM;
! 126746: }
! 126747: pWriter->nMalloc = nTerm*2;
! 126748: pWriter->zMalloc = zNew;
! 126749: pWriter->zTerm = zNew;
! 126750: }
! 126751: assert( pWriter->zTerm==pWriter->zMalloc );
! 126752: memcpy(pWriter->zTerm, zTerm, nTerm);
! 126753: }else{
! 126754: pWriter->zTerm = (char *)zTerm;
! 126755: }
! 126756: pWriter->nTerm = nTerm;
! 126757:
! 126758: return SQLITE_OK;
! 126759: }
! 126760:
! 126761: /*
! 126762: ** Flush all data associated with the SegmentWriter object pWriter to the
! 126763: ** database. This function must be called after all terms have been added
! 126764: ** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
! 126765: ** returned. Otherwise, an SQLite error code.
! 126766: */
! 126767: static int fts3SegWriterFlush(
! 126768: Fts3Table *p, /* Virtual table handle */
! 126769: SegmentWriter *pWriter, /* SegmentWriter to flush to the db */
! 126770: int iLevel, /* Value for 'level' column of %_segdir */
! 126771: int iIdx /* Value for 'idx' column of %_segdir */
! 126772: ){
! 126773: int rc; /* Return code */
! 126774: if( pWriter->pTree ){
! 126775: sqlite3_int64 iLast = 0; /* Largest block id written to database */
! 126776: sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */
! 126777: char *zRoot = NULL; /* Pointer to buffer containing root node */
! 126778: int nRoot = 0; /* Size of buffer zRoot */
! 126779:
! 126780: iLastLeaf = pWriter->iFree;
! 126781: rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
! 126782: if( rc==SQLITE_OK ){
! 126783: rc = fts3NodeWrite(p, pWriter->pTree, 1,
! 126784: pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
! 126785: }
! 126786: if( rc==SQLITE_OK ){
! 126787: rc = fts3WriteSegdir(
! 126788: p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
! 126789: }
! 126790: }else{
! 126791: /* The entire tree fits on the root node. Write it to the segdir table. */
! 126792: rc = fts3WriteSegdir(
! 126793: p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
! 126794: }
! 126795: return rc;
! 126796: }
! 126797:
! 126798: /*
! 126799: ** Release all memory held by the SegmentWriter object passed as the
! 126800: ** first argument.
! 126801: */
! 126802: static void fts3SegWriterFree(SegmentWriter *pWriter){
! 126803: if( pWriter ){
! 126804: sqlite3_free(pWriter->aData);
! 126805: sqlite3_free(pWriter->zMalloc);
! 126806: fts3NodeFree(pWriter->pTree);
! 126807: sqlite3_free(pWriter);
! 126808: }
! 126809: }
! 126810:
! 126811: /*
! 126812: ** The first value in the apVal[] array is assumed to contain an integer.
! 126813: ** This function tests if there exist any documents with docid values that
! 126814: ** are different from that integer. i.e. if deleting the document with docid
! 126815: ** pRowid would mean the FTS3 table were empty.
! 126816: **
! 126817: ** If successful, *pisEmpty is set to true if the table is empty except for
! 126818: ** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
! 126819: ** error occurs, an SQLite error code is returned.
! 126820: */
! 126821: static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
! 126822: sqlite3_stmt *pStmt;
! 126823: int rc;
! 126824: if( p->zContentTbl ){
! 126825: /* If using the content=xxx option, assume the table is never empty */
! 126826: *pisEmpty = 0;
! 126827: rc = SQLITE_OK;
! 126828: }else{
! 126829: rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
! 126830: if( rc==SQLITE_OK ){
! 126831: if( SQLITE_ROW==sqlite3_step(pStmt) ){
! 126832: *pisEmpty = sqlite3_column_int(pStmt, 0);
! 126833: }
! 126834: rc = sqlite3_reset(pStmt);
! 126835: }
! 126836: }
! 126837: return rc;
! 126838: }
! 126839:
! 126840: /*
! 126841: ** Set *pnMax to the largest segment level in the database for the index
! 126842: ** iIndex.
! 126843: **
! 126844: ** Segment levels are stored in the 'level' column of the %_segdir table.
! 126845: **
! 126846: ** Return SQLITE_OK if successful, or an SQLite error code if not.
! 126847: */
! 126848: static int fts3SegmentMaxLevel(Fts3Table *p, int iIndex, int *pnMax){
! 126849: sqlite3_stmt *pStmt;
! 126850: int rc;
! 126851: assert( iIndex>=0 && iIndex<p->nIndex );
! 126852:
! 126853: /* Set pStmt to the compiled version of:
! 126854: **
! 126855: ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
! 126856: **
! 126857: ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
! 126858: */
! 126859: rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
! 126860: if( rc!=SQLITE_OK ) return rc;
! 126861: sqlite3_bind_int(pStmt, 1, iIndex*FTS3_SEGDIR_MAXLEVEL);
! 126862: sqlite3_bind_int(pStmt, 2, (iIndex+1)*FTS3_SEGDIR_MAXLEVEL - 1);
! 126863: if( SQLITE_ROW==sqlite3_step(pStmt) ){
! 126864: *pnMax = sqlite3_column_int(pStmt, 0);
! 126865: }
! 126866: return sqlite3_reset(pStmt);
! 126867: }
! 126868:
! 126869: /*
! 126870: ** This function is used after merging multiple segments into a single large
! 126871: ** segment to delete the old, now redundant, segment b-trees. Specifically,
! 126872: ** it:
! 126873: **
! 126874: ** 1) Deletes all %_segments entries for the segments associated with
! 126875: ** each of the SegReader objects in the array passed as the third
! 126876: ** argument, and
! 126877: **
! 126878: ** 2) deletes all %_segdir entries with level iLevel, or all %_segdir
! 126879: ** entries regardless of level if (iLevel<0).
! 126880: **
! 126881: ** SQLITE_OK is returned if successful, otherwise an SQLite error code.
! 126882: */
! 126883: static int fts3DeleteSegdir(
! 126884: Fts3Table *p, /* Virtual table handle */
! 126885: int iIndex, /* Index for p->aIndex */
! 126886: int iLevel, /* Level of %_segdir entries to delete */
! 126887: Fts3SegReader **apSegment, /* Array of SegReader objects */
! 126888: int nReader /* Size of array apSegment */
! 126889: ){
! 126890: int rc; /* Return Code */
! 126891: int i; /* Iterator variable */
! 126892: sqlite3_stmt *pDelete; /* SQL statement to delete rows */
! 126893:
! 126894: rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
! 126895: for(i=0; rc==SQLITE_OK && i<nReader; i++){
! 126896: Fts3SegReader *pSegment = apSegment[i];
! 126897: if( pSegment->iStartBlock ){
! 126898: sqlite3_bind_int64(pDelete, 1, pSegment->iStartBlock);
! 126899: sqlite3_bind_int64(pDelete, 2, pSegment->iEndBlock);
! 126900: sqlite3_step(pDelete);
! 126901: rc = sqlite3_reset(pDelete);
! 126902: }
! 126903: }
! 126904: if( rc!=SQLITE_OK ){
! 126905: return rc;
! 126906: }
! 126907:
! 126908: assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
! 126909: if( iLevel==FTS3_SEGCURSOR_ALL ){
! 126910: rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
! 126911: if( rc==SQLITE_OK ){
! 126912: sqlite3_bind_int(pDelete, 1, iIndex*FTS3_SEGDIR_MAXLEVEL);
! 126913: sqlite3_bind_int(pDelete, 2, (iIndex+1) * FTS3_SEGDIR_MAXLEVEL - 1);
! 126914: }
! 126915: }else{
! 126916: rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
! 126917: if( rc==SQLITE_OK ){
! 126918: sqlite3_bind_int(pDelete, 1, iIndex*FTS3_SEGDIR_MAXLEVEL + iLevel);
! 126919: }
! 126920: }
! 126921:
! 126922: if( rc==SQLITE_OK ){
! 126923: sqlite3_step(pDelete);
! 126924: rc = sqlite3_reset(pDelete);
! 126925: }
! 126926:
! 126927: return rc;
! 126928: }
! 126929:
! 126930: /*
! 126931: ** When this function is called, buffer *ppList (size *pnList bytes) contains
! 126932: ** a position list that may (or may not) feature multiple columns. This
! 126933: ** function adjusts the pointer *ppList and the length *pnList so that they
! 126934: ** identify the subset of the position list that corresponds to column iCol.
! 126935: **
! 126936: ** If there are no entries in the input position list for column iCol, then
! 126937: ** *pnList is set to zero before returning.
! 126938: */
! 126939: static void fts3ColumnFilter(
! 126940: int iCol, /* Column to filter on */
! 126941: char **ppList, /* IN/OUT: Pointer to position list */
! 126942: int *pnList /* IN/OUT: Size of buffer *ppList in bytes */
! 126943: ){
! 126944: char *pList = *ppList;
! 126945: int nList = *pnList;
! 126946: char *pEnd = &pList[nList];
! 126947: int iCurrent = 0;
! 126948: char *p = pList;
! 126949:
! 126950: assert( iCol>=0 );
! 126951: while( 1 ){
! 126952: char c = 0;
! 126953: while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
! 126954:
! 126955: if( iCol==iCurrent ){
! 126956: nList = (int)(p - pList);
! 126957: break;
! 126958: }
! 126959:
! 126960: nList -= (int)(p - pList);
! 126961: pList = p;
! 126962: if( nList==0 ){
! 126963: break;
! 126964: }
! 126965: p = &pList[1];
! 126966: p += sqlite3Fts3GetVarint32(p, &iCurrent);
! 126967: }
! 126968:
! 126969: *ppList = pList;
! 126970: *pnList = nList;
! 126971: }
! 126972:
! 126973: /*
! 126974: ** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
! 126975: ** existing data). Grow the buffer if required.
! 126976: **
! 126977: ** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
! 126978: ** trying to resize the buffer, return SQLITE_NOMEM.
! 126979: */
! 126980: static int fts3MsrBufferData(
! 126981: Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
! 126982: char *pList,
! 126983: int nList
! 126984: ){
! 126985: if( nList>pMsr->nBuffer ){
! 126986: char *pNew;
! 126987: pMsr->nBuffer = nList*2;
! 126988: pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
! 126989: if( !pNew ) return SQLITE_NOMEM;
! 126990: pMsr->aBuffer = pNew;
! 126991: }
! 126992:
! 126993: memcpy(pMsr->aBuffer, pList, nList);
! 126994: return SQLITE_OK;
! 126995: }
! 126996:
! 126997: SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
! 126998: Fts3Table *p, /* Virtual table handle */
! 126999: Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
! 127000: sqlite3_int64 *piDocid, /* OUT: Docid value */
! 127001: char **paPoslist, /* OUT: Pointer to position list */
! 127002: int *pnPoslist /* OUT: Size of position list in bytes */
! 127003: ){
! 127004: int nMerge = pMsr->nAdvance;
! 127005: Fts3SegReader **apSegment = pMsr->apSegment;
! 127006: int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
! 127007: p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
! 127008: );
! 127009:
! 127010: if( nMerge==0 ){
! 127011: *paPoslist = 0;
! 127012: return SQLITE_OK;
! 127013: }
! 127014:
! 127015: while( 1 ){
! 127016: Fts3SegReader *pSeg;
! 127017: pSeg = pMsr->apSegment[0];
! 127018:
! 127019: if( pSeg->pOffsetList==0 ){
! 127020: *paPoslist = 0;
! 127021: break;
! 127022: }else{
! 127023: int rc;
! 127024: char *pList;
! 127025: int nList;
! 127026: int j;
! 127027: sqlite3_int64 iDocid = apSegment[0]->iDocid;
! 127028:
! 127029: rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
! 127030: j = 1;
! 127031: while( rc==SQLITE_OK
! 127032: && j<nMerge
! 127033: && apSegment[j]->pOffsetList
! 127034: && apSegment[j]->iDocid==iDocid
! 127035: ){
! 127036: rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
! 127037: j++;
! 127038: }
! 127039: if( rc!=SQLITE_OK ) return rc;
! 127040: fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
! 127041:
! 127042: if( pMsr->iColFilter>=0 ){
! 127043: fts3ColumnFilter(pMsr->iColFilter, &pList, &nList);
! 127044: }
! 127045:
! 127046: if( nList>0 ){
! 127047: if( fts3SegReaderIsPending(apSegment[0]) ){
! 127048: rc = fts3MsrBufferData(pMsr, pList, nList+1);
! 127049: if( rc!=SQLITE_OK ) return rc;
! 127050: *paPoslist = pMsr->aBuffer;
! 127051: assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
! 127052: }else{
! 127053: *paPoslist = pList;
! 127054: }
! 127055: *piDocid = iDocid;
! 127056: *pnPoslist = nList;
! 127057: break;
! 127058: }
! 127059: }
! 127060: }
! 127061:
! 127062: return SQLITE_OK;
! 127063: }
! 127064:
! 127065: static int fts3SegReaderStart(
! 127066: Fts3Table *p, /* Virtual table handle */
! 127067: Fts3MultiSegReader *pCsr, /* Cursor object */
! 127068: const char *zTerm, /* Term searched for (or NULL) */
! 127069: int nTerm /* Length of zTerm in bytes */
! 127070: ){
! 127071: int i;
! 127072: int nSeg = pCsr->nSegment;
! 127073:
! 127074: /* If the Fts3SegFilter defines a specific term (or term prefix) to search
! 127075: ** for, then advance each segment iterator until it points to a term of
! 127076: ** equal or greater value than the specified term. This prevents many
! 127077: ** unnecessary merge/sort operations for the case where single segment
! 127078: ** b-tree leaf nodes contain more than one term.
! 127079: */
! 127080: for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
! 127081: Fts3SegReader *pSeg = pCsr->apSegment[i];
! 127082: do {
! 127083: int rc = fts3SegReaderNext(p, pSeg, 0);
! 127084: if( rc!=SQLITE_OK ) return rc;
! 127085: }while( zTerm && fts3SegReaderTermCmp(pSeg, zTerm, nTerm)<0 );
! 127086: }
! 127087: fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
! 127088:
! 127089: return SQLITE_OK;
! 127090: }
! 127091:
! 127092: SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
! 127093: Fts3Table *p, /* Virtual table handle */
! 127094: Fts3MultiSegReader *pCsr, /* Cursor object */
! 127095: Fts3SegFilter *pFilter /* Restrictions on range of iteration */
! 127096: ){
! 127097: pCsr->pFilter = pFilter;
! 127098: return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
! 127099: }
! 127100:
! 127101: SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
! 127102: Fts3Table *p, /* Virtual table handle */
! 127103: Fts3MultiSegReader *pCsr, /* Cursor object */
! 127104: int iCol, /* Column to match on. */
! 127105: const char *zTerm, /* Term to iterate through a doclist for */
! 127106: int nTerm /* Number of bytes in zTerm */
! 127107: ){
! 127108: int i;
! 127109: int rc;
! 127110: int nSegment = pCsr->nSegment;
! 127111: int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
! 127112: p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
! 127113: );
! 127114:
! 127115: assert( pCsr->pFilter==0 );
! 127116: assert( zTerm && nTerm>0 );
! 127117:
! 127118: /* Advance each segment iterator until it points to the term zTerm/nTerm. */
! 127119: rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
! 127120: if( rc!=SQLITE_OK ) return rc;
! 127121:
! 127122: /* Determine how many of the segments actually point to zTerm/nTerm. */
! 127123: for(i=0; i<nSegment; i++){
! 127124: Fts3SegReader *pSeg = pCsr->apSegment[i];
! 127125: if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
! 127126: break;
! 127127: }
! 127128: }
! 127129: pCsr->nAdvance = i;
! 127130:
! 127131: /* Advance each of the segments to point to the first docid. */
! 127132: for(i=0; i<pCsr->nAdvance; i++){
! 127133: rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
! 127134: if( rc!=SQLITE_OK ) return rc;
! 127135: }
! 127136: fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
! 127137:
! 127138: assert( iCol<0 || iCol<p->nColumn );
! 127139: pCsr->iColFilter = iCol;
! 127140:
! 127141: return SQLITE_OK;
! 127142: }
! 127143:
! 127144: /*
! 127145: ** This function is called on a MultiSegReader that has been started using
! 127146: ** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
! 127147: ** have been made. Calling this function puts the MultiSegReader in such
! 127148: ** a state that if the next two calls are:
! 127149: **
! 127150: ** sqlite3Fts3SegReaderStart()
! 127151: ** sqlite3Fts3SegReaderStep()
! 127152: **
! 127153: ** then the entire doclist for the term is available in
! 127154: ** MultiSegReader.aDoclist/nDoclist.
! 127155: */
! 127156: SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
! 127157: int i; /* Used to iterate through segment-readers */
! 127158:
! 127159: assert( pCsr->zTerm==0 );
! 127160: assert( pCsr->nTerm==0 );
! 127161: assert( pCsr->aDoclist==0 );
! 127162: assert( pCsr->nDoclist==0 );
! 127163:
! 127164: pCsr->nAdvance = 0;
! 127165: pCsr->bRestart = 1;
! 127166: for(i=0; i<pCsr->nSegment; i++){
! 127167: pCsr->apSegment[i]->pOffsetList = 0;
! 127168: pCsr->apSegment[i]->nOffsetList = 0;
! 127169: pCsr->apSegment[i]->iDocid = 0;
! 127170: }
! 127171:
! 127172: return SQLITE_OK;
! 127173: }
! 127174:
! 127175:
! 127176: SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
! 127177: Fts3Table *p, /* Virtual table handle */
! 127178: Fts3MultiSegReader *pCsr /* Cursor object */
! 127179: ){
! 127180: int rc = SQLITE_OK;
! 127181:
! 127182: int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
! 127183: int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
! 127184: int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
! 127185: int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
! 127186: int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
! 127187: int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
! 127188:
! 127189: Fts3SegReader **apSegment = pCsr->apSegment;
! 127190: int nSegment = pCsr->nSegment;
! 127191: Fts3SegFilter *pFilter = pCsr->pFilter;
! 127192: int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
! 127193: p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
! 127194: );
! 127195:
! 127196: if( pCsr->nSegment==0 ) return SQLITE_OK;
! 127197:
! 127198: do {
! 127199: int nMerge;
! 127200: int i;
! 127201:
! 127202: /* Advance the first pCsr->nAdvance entries in the apSegment[] array
! 127203: ** forward. Then sort the list in order of current term again.
! 127204: */
! 127205: for(i=0; i<pCsr->nAdvance; i++){
! 127206: rc = fts3SegReaderNext(p, apSegment[i], 0);
! 127207: if( rc!=SQLITE_OK ) return rc;
! 127208: }
! 127209: fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
! 127210: pCsr->nAdvance = 0;
! 127211:
! 127212: /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
! 127213: assert( rc==SQLITE_OK );
! 127214: if( apSegment[0]->aNode==0 ) break;
! 127215:
! 127216: pCsr->nTerm = apSegment[0]->nTerm;
! 127217: pCsr->zTerm = apSegment[0]->zTerm;
! 127218:
! 127219: /* If this is a prefix-search, and if the term that apSegment[0] points
! 127220: ** to does not share a suffix with pFilter->zTerm/nTerm, then all
! 127221: ** required callbacks have been made. In this case exit early.
! 127222: **
! 127223: ** Similarly, if this is a search for an exact match, and the first term
! 127224: ** of segment apSegment[0] is not a match, exit early.
! 127225: */
! 127226: if( pFilter->zTerm && !isScan ){
! 127227: if( pCsr->nTerm<pFilter->nTerm
! 127228: || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
! 127229: || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
! 127230: ){
! 127231: break;
! 127232: }
! 127233: }
! 127234:
! 127235: nMerge = 1;
! 127236: while( nMerge<nSegment
! 127237: && apSegment[nMerge]->aNode
! 127238: && apSegment[nMerge]->nTerm==pCsr->nTerm
! 127239: && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
! 127240: ){
! 127241: nMerge++;
! 127242: }
! 127243:
! 127244: assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
! 127245: if( nMerge==1
! 127246: && !isIgnoreEmpty
! 127247: && !isFirst
! 127248: && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
! 127249: ){
! 127250: pCsr->nDoclist = apSegment[0]->nDoclist;
! 127251: if( fts3SegReaderIsPending(apSegment[0]) ){
! 127252: rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
! 127253: pCsr->aDoclist = pCsr->aBuffer;
! 127254: }else{
! 127255: pCsr->aDoclist = apSegment[0]->aDoclist;
! 127256: }
! 127257: if( rc==SQLITE_OK ) rc = SQLITE_ROW;
! 127258: }else{
! 127259: int nDoclist = 0; /* Size of doclist */
! 127260: sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
! 127261:
! 127262: /* The current term of the first nMerge entries in the array
! 127263: ** of Fts3SegReader objects is the same. The doclists must be merged
! 127264: ** and a single term returned with the merged doclist.
! 127265: */
! 127266: for(i=0; i<nMerge; i++){
! 127267: fts3SegReaderFirstDocid(p, apSegment[i]);
! 127268: }
! 127269: fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
! 127270: while( apSegment[0]->pOffsetList ){
! 127271: int j; /* Number of segments that share a docid */
! 127272: char *pList;
! 127273: int nList;
! 127274: int nByte;
! 127275: sqlite3_int64 iDocid = apSegment[0]->iDocid;
! 127276: fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
! 127277: j = 1;
! 127278: while( j<nMerge
! 127279: && apSegment[j]->pOffsetList
! 127280: && apSegment[j]->iDocid==iDocid
! 127281: ){
! 127282: fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
! 127283: j++;
! 127284: }
! 127285:
! 127286: if( isColFilter ){
! 127287: fts3ColumnFilter(pFilter->iCol, &pList, &nList);
! 127288: }
! 127289:
! 127290: if( !isIgnoreEmpty || nList>0 ){
! 127291:
! 127292: /* Calculate the 'docid' delta value to write into the merged
! 127293: ** doclist. */
! 127294: sqlite3_int64 iDelta;
! 127295: if( p->bDescIdx && nDoclist>0 ){
! 127296: iDelta = iPrev - iDocid;
! 127297: }else{
! 127298: iDelta = iDocid - iPrev;
! 127299: }
! 127300: assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
! 127301: assert( nDoclist>0 || iDelta==iDocid );
! 127302:
! 127303: nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
! 127304: if( nDoclist+nByte>pCsr->nBuffer ){
! 127305: char *aNew;
! 127306: pCsr->nBuffer = (nDoclist+nByte)*2;
! 127307: aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
! 127308: if( !aNew ){
! 127309: return SQLITE_NOMEM;
! 127310: }
! 127311: pCsr->aBuffer = aNew;
! 127312: }
! 127313:
! 127314: if( isFirst ){
! 127315: char *a = &pCsr->aBuffer[nDoclist];
! 127316: int nWrite;
! 127317:
! 127318: nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
! 127319: if( nWrite ){
! 127320: iPrev = iDocid;
! 127321: nDoclist += nWrite;
! 127322: }
! 127323: }else{
! 127324: nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
! 127325: iPrev = iDocid;
! 127326: if( isRequirePos ){
! 127327: memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
! 127328: nDoclist += nList;
! 127329: pCsr->aBuffer[nDoclist++] = '\0';
! 127330: }
! 127331: }
! 127332: }
! 127333:
! 127334: fts3SegReaderSort(apSegment, nMerge, j, xCmp);
! 127335: }
! 127336: if( nDoclist>0 ){
! 127337: pCsr->aDoclist = pCsr->aBuffer;
! 127338: pCsr->nDoclist = nDoclist;
! 127339: rc = SQLITE_ROW;
! 127340: }
! 127341: }
! 127342: pCsr->nAdvance = nMerge;
! 127343: }while( rc==SQLITE_OK );
! 127344:
! 127345: return rc;
! 127346: }
! 127347:
! 127348:
! 127349: SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
! 127350: Fts3MultiSegReader *pCsr /* Cursor object */
! 127351: ){
! 127352: if( pCsr ){
! 127353: int i;
! 127354: for(i=0; i<pCsr->nSegment; i++){
! 127355: sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
! 127356: }
! 127357: sqlite3_free(pCsr->apSegment);
! 127358: sqlite3_free(pCsr->aBuffer);
! 127359:
! 127360: pCsr->nSegment = 0;
! 127361: pCsr->apSegment = 0;
! 127362: pCsr->aBuffer = 0;
! 127363: }
! 127364: }
! 127365:
! 127366: /*
! 127367: ** Merge all level iLevel segments in the database into a single
! 127368: ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
! 127369: ** single segment with a level equal to the numerically largest level
! 127370: ** currently present in the database.
! 127371: **
! 127372: ** If this function is called with iLevel<0, but there is only one
! 127373: ** segment in the database, SQLITE_DONE is returned immediately.
! 127374: ** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
! 127375: ** an SQLite error code is returned.
! 127376: */
! 127377: static int fts3SegmentMerge(Fts3Table *p, int iIndex, int iLevel){
! 127378: int rc; /* Return code */
! 127379: int iIdx = 0; /* Index of new segment */
! 127380: int iNewLevel = 0; /* Level/index to create new segment at */
! 127381: SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
! 127382: Fts3SegFilter filter; /* Segment term filter condition */
! 127383: Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
! 127384: int bIgnoreEmpty = 0; /* True to ignore empty segments */
! 127385:
! 127386: assert( iLevel==FTS3_SEGCURSOR_ALL
! 127387: || iLevel==FTS3_SEGCURSOR_PENDING
! 127388: || iLevel>=0
! 127389: );
! 127390: assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
! 127391: assert( iIndex>=0 && iIndex<p->nIndex );
! 127392:
! 127393: rc = sqlite3Fts3SegReaderCursor(p, iIndex, iLevel, 0, 0, 1, 0, &csr);
! 127394: if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
! 127395:
! 127396: if( iLevel==FTS3_SEGCURSOR_ALL ){
! 127397: /* This call is to merge all segments in the database to a single
! 127398: ** segment. The level of the new segment is equal to the the numerically
! 127399: ** greatest segment level currently present in the database for this
! 127400: ** index. The idx of the new segment is always 0. */
! 127401: if( csr.nSegment==1 ){
! 127402: rc = SQLITE_DONE;
! 127403: goto finished;
! 127404: }
! 127405: rc = fts3SegmentMaxLevel(p, iIndex, &iNewLevel);
! 127406: bIgnoreEmpty = 1;
! 127407:
! 127408: }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
! 127409: iNewLevel = iIndex * FTS3_SEGDIR_MAXLEVEL;
! 127410: rc = fts3AllocateSegdirIdx(p, iIndex, 0, &iIdx);
! 127411: }else{
! 127412: /* This call is to merge all segments at level iLevel. find the next
! 127413: ** available segment index at level iLevel+1. The call to
! 127414: ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
! 127415: ** a single iLevel+2 segment if necessary. */
! 127416: rc = fts3AllocateSegdirIdx(p, iIndex, iLevel+1, &iIdx);
! 127417: iNewLevel = iIndex * FTS3_SEGDIR_MAXLEVEL + iLevel+1;
! 127418: }
! 127419: if( rc!=SQLITE_OK ) goto finished;
! 127420: assert( csr.nSegment>0 );
! 127421: assert( iNewLevel>=(iIndex*FTS3_SEGDIR_MAXLEVEL) );
! 127422: assert( iNewLevel<((iIndex+1)*FTS3_SEGDIR_MAXLEVEL) );
! 127423:
! 127424: memset(&filter, 0, sizeof(Fts3SegFilter));
! 127425: filter.flags = FTS3_SEGMENT_REQUIRE_POS;
! 127426: filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
! 127427:
! 127428: rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
! 127429: while( SQLITE_OK==rc ){
! 127430: rc = sqlite3Fts3SegReaderStep(p, &csr);
! 127431: if( rc!=SQLITE_ROW ) break;
! 127432: rc = fts3SegWriterAdd(p, &pWriter, 1,
! 127433: csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
! 127434: }
! 127435: if( rc!=SQLITE_OK ) goto finished;
! 127436: assert( pWriter );
! 127437:
! 127438: if( iLevel!=FTS3_SEGCURSOR_PENDING ){
! 127439: rc = fts3DeleteSegdir(p, iIndex, iLevel, csr.apSegment, csr.nSegment);
! 127440: if( rc!=SQLITE_OK ) goto finished;
! 127441: }
! 127442: rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
! 127443:
! 127444: finished:
! 127445: fts3SegWriterFree(pWriter);
! 127446: sqlite3Fts3SegReaderFinish(&csr);
! 127447: return rc;
! 127448: }
! 127449:
! 127450:
! 127451: /*
! 127452: ** Flush the contents of pendingTerms to level 0 segments.
! 127453: */
! 127454: SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
! 127455: int rc = SQLITE_OK;
! 127456: int i;
! 127457: for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
! 127458: rc = fts3SegmentMerge(p, i, FTS3_SEGCURSOR_PENDING);
! 127459: if( rc==SQLITE_DONE ) rc = SQLITE_OK;
! 127460: }
! 127461: sqlite3Fts3PendingTermsClear(p);
! 127462: return rc;
! 127463: }
! 127464:
! 127465: /*
! 127466: ** Encode N integers as varints into a blob.
! 127467: */
! 127468: static void fts3EncodeIntArray(
! 127469: int N, /* The number of integers to encode */
! 127470: u32 *a, /* The integer values */
! 127471: char *zBuf, /* Write the BLOB here */
! 127472: int *pNBuf /* Write number of bytes if zBuf[] used here */
! 127473: ){
! 127474: int i, j;
! 127475: for(i=j=0; i<N; i++){
! 127476: j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
! 127477: }
! 127478: *pNBuf = j;
! 127479: }
! 127480:
! 127481: /*
! 127482: ** Decode a blob of varints into N integers
! 127483: */
! 127484: static void fts3DecodeIntArray(
! 127485: int N, /* The number of integers to decode */
! 127486: u32 *a, /* Write the integer values */
! 127487: const char *zBuf, /* The BLOB containing the varints */
! 127488: int nBuf /* size of the BLOB */
! 127489: ){
! 127490: int i, j;
! 127491: UNUSED_PARAMETER(nBuf);
! 127492: for(i=j=0; i<N; i++){
! 127493: sqlite3_int64 x;
! 127494: j += sqlite3Fts3GetVarint(&zBuf[j], &x);
! 127495: assert(j<=nBuf);
! 127496: a[i] = (u32)(x & 0xffffffff);
! 127497: }
! 127498: }
! 127499:
! 127500: /*
! 127501: ** Insert the sizes (in tokens) for each column of the document
! 127502: ** with docid equal to p->iPrevDocid. The sizes are encoded as
! 127503: ** a blob of varints.
! 127504: */
! 127505: static void fts3InsertDocsize(
! 127506: int *pRC, /* Result code */
! 127507: Fts3Table *p, /* Table into which to insert */
! 127508: u32 *aSz /* Sizes of each column, in tokens */
! 127509: ){
! 127510: char *pBlob; /* The BLOB encoding of the document size */
! 127511: int nBlob; /* Number of bytes in the BLOB */
! 127512: sqlite3_stmt *pStmt; /* Statement used to insert the encoding */
! 127513: int rc; /* Result code from subfunctions */
! 127514:
! 127515: if( *pRC ) return;
! 127516: pBlob = sqlite3_malloc( 10*p->nColumn );
! 127517: if( pBlob==0 ){
! 127518: *pRC = SQLITE_NOMEM;
! 127519: return;
! 127520: }
! 127521: fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
! 127522: rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
! 127523: if( rc ){
! 127524: sqlite3_free(pBlob);
! 127525: *pRC = rc;
! 127526: return;
! 127527: }
! 127528: sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
! 127529: sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
! 127530: sqlite3_step(pStmt);
! 127531: *pRC = sqlite3_reset(pStmt);
! 127532: }
! 127533:
! 127534: /*
! 127535: ** Record 0 of the %_stat table contains a blob consisting of N varints,
! 127536: ** where N is the number of user defined columns in the fts3 table plus
! 127537: ** two. If nCol is the number of user defined columns, then values of the
! 127538: ** varints are set as follows:
! 127539: **
! 127540: ** Varint 0: Total number of rows in the table.
! 127541: **
! 127542: ** Varint 1..nCol: For each column, the total number of tokens stored in
! 127543: ** the column for all rows of the table.
! 127544: **
! 127545: ** Varint 1+nCol: The total size, in bytes, of all text values in all
! 127546: ** columns of all rows of the table.
! 127547: **
! 127548: */
! 127549: static void fts3UpdateDocTotals(
! 127550: int *pRC, /* The result code */
! 127551: Fts3Table *p, /* Table being updated */
! 127552: u32 *aSzIns, /* Size increases */
! 127553: u32 *aSzDel, /* Size decreases */
! 127554: int nChng /* Change in the number of documents */
! 127555: ){
! 127556: char *pBlob; /* Storage for BLOB written into %_stat */
! 127557: int nBlob; /* Size of BLOB written into %_stat */
! 127558: u32 *a; /* Array of integers that becomes the BLOB */
! 127559: sqlite3_stmt *pStmt; /* Statement for reading and writing */
! 127560: int i; /* Loop counter */
! 127561: int rc; /* Result code from subfunctions */
! 127562:
! 127563: const int nStat = p->nColumn+2;
! 127564:
! 127565: if( *pRC ) return;
! 127566: a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
! 127567: if( a==0 ){
! 127568: *pRC = SQLITE_NOMEM;
! 127569: return;
! 127570: }
! 127571: pBlob = (char*)&a[nStat];
! 127572: rc = fts3SqlStmt(p, SQL_SELECT_DOCTOTAL, &pStmt, 0);
! 127573: if( rc ){
! 127574: sqlite3_free(a);
! 127575: *pRC = rc;
! 127576: return;
! 127577: }
! 127578: if( sqlite3_step(pStmt)==SQLITE_ROW ){
! 127579: fts3DecodeIntArray(nStat, a,
! 127580: sqlite3_column_blob(pStmt, 0),
! 127581: sqlite3_column_bytes(pStmt, 0));
! 127582: }else{
! 127583: memset(a, 0, sizeof(u32)*(nStat) );
! 127584: }
! 127585: sqlite3_reset(pStmt);
! 127586: if( nChng<0 && a[0]<(u32)(-nChng) ){
! 127587: a[0] = 0;
! 127588: }else{
! 127589: a[0] += nChng;
! 127590: }
! 127591: for(i=0; i<p->nColumn+1; i++){
! 127592: u32 x = a[i+1];
! 127593: if( x+aSzIns[i] < aSzDel[i] ){
! 127594: x = 0;
! 127595: }else{
! 127596: x = x + aSzIns[i] - aSzDel[i];
! 127597: }
! 127598: a[i+1] = x;
! 127599: }
! 127600: fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
! 127601: rc = fts3SqlStmt(p, SQL_REPLACE_DOCTOTAL, &pStmt, 0);
! 127602: if( rc ){
! 127603: sqlite3_free(a);
! 127604: *pRC = rc;
! 127605: return;
! 127606: }
! 127607: sqlite3_bind_blob(pStmt, 1, pBlob, nBlob, SQLITE_STATIC);
! 127608: sqlite3_step(pStmt);
! 127609: *pRC = sqlite3_reset(pStmt);
! 127610: sqlite3_free(a);
! 127611: }
! 127612:
! 127613: static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
! 127614: int i;
! 127615: int bSeenDone = 0;
! 127616: int rc = SQLITE_OK;
! 127617: for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
! 127618: rc = fts3SegmentMerge(p, i, FTS3_SEGCURSOR_ALL);
! 127619: if( rc==SQLITE_DONE ){
! 127620: bSeenDone = 1;
! 127621: rc = SQLITE_OK;
! 127622: }
! 127623: }
! 127624: sqlite3Fts3SegmentsClose(p);
! 127625: sqlite3Fts3PendingTermsClear(p);
! 127626:
! 127627: return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
! 127628: }
! 127629:
! 127630: /*
! 127631: ** This function is called when the user executes the following statement:
! 127632: **
! 127633: ** INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
! 127634: **
! 127635: ** The entire FTS index is discarded and rebuilt. If the table is one
! 127636: ** created using the content=xxx option, then the new index is based on
! 127637: ** the current contents of the xxx table. Otherwise, it is rebuilt based
! 127638: ** on the contents of the %_content table.
! 127639: */
! 127640: static int fts3DoRebuild(Fts3Table *p){
! 127641: int rc; /* Return Code */
! 127642:
! 127643: rc = fts3DeleteAll(p, 0);
! 127644: if( rc==SQLITE_OK ){
! 127645: u32 *aSz = 0;
! 127646: u32 *aSzIns = 0;
! 127647: u32 *aSzDel = 0;
! 127648: sqlite3_stmt *pStmt = 0;
! 127649: int nEntry = 0;
! 127650:
! 127651: /* Compose and prepare an SQL statement to loop through the content table */
! 127652: char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
! 127653: if( !zSql ){
! 127654: rc = SQLITE_NOMEM;
! 127655: }else{
! 127656: rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
! 127657: sqlite3_free(zSql);
! 127658: }
! 127659:
! 127660: if( rc==SQLITE_OK ){
! 127661: int nByte = sizeof(u32) * (p->nColumn+1)*3;
! 127662: aSz = (u32 *)sqlite3_malloc(nByte);
! 127663: if( aSz==0 ){
! 127664: rc = SQLITE_NOMEM;
! 127665: }else{
! 127666: memset(aSz, 0, nByte);
! 127667: aSzIns = &aSz[p->nColumn+1];
! 127668: aSzDel = &aSzIns[p->nColumn+1];
! 127669: }
! 127670: }
! 127671:
! 127672: while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
! 127673: int iCol;
! 127674: rc = fts3PendingTermsDocid(p, sqlite3_column_int64(pStmt, 0));
! 127675: aSz[p->nColumn] = 0;
! 127676: for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
! 127677: const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
! 127678: rc = fts3PendingTermsAdd(p, z, iCol, &aSz[iCol]);
! 127679: aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
! 127680: }
! 127681: if( p->bHasDocsize ){
! 127682: fts3InsertDocsize(&rc, p, aSz);
! 127683: }
! 127684: if( rc!=SQLITE_OK ){
! 127685: sqlite3_finalize(pStmt);
! 127686: pStmt = 0;
! 127687: }else{
! 127688: nEntry++;
! 127689: for(iCol=0; iCol<=p->nColumn; iCol++){
! 127690: aSzIns[iCol] += aSz[iCol];
! 127691: }
! 127692: }
! 127693: }
! 127694: if( p->bHasStat ){
! 127695: fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
! 127696: }
! 127697: sqlite3_free(aSz);
! 127698:
! 127699: if( pStmt ){
! 127700: int rc2 = sqlite3_finalize(pStmt);
! 127701: if( rc==SQLITE_OK ){
! 127702: rc = rc2;
! 127703: }
! 127704: }
! 127705: }
! 127706:
! 127707: return rc;
! 127708: }
! 127709:
! 127710: /*
! 127711: ** Handle a 'special' INSERT of the form:
! 127712: **
! 127713: ** "INSERT INTO tbl(tbl) VALUES(<expr>)"
! 127714: **
! 127715: ** Argument pVal contains the result of <expr>. Currently the only
! 127716: ** meaningful value to insert is the text 'optimize'.
! 127717: */
! 127718: static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
! 127719: int rc; /* Return Code */
! 127720: const char *zVal = (const char *)sqlite3_value_text(pVal);
! 127721: int nVal = sqlite3_value_bytes(pVal);
! 127722:
! 127723: if( !zVal ){
! 127724: return SQLITE_NOMEM;
! 127725: }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
! 127726: rc = fts3DoOptimize(p, 0);
! 127727: }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
! 127728: rc = fts3DoRebuild(p);
! 127729: #ifdef SQLITE_TEST
! 127730: }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
! 127731: p->nNodeSize = atoi(&zVal[9]);
! 127732: rc = SQLITE_OK;
! 127733: }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
! 127734: p->nMaxPendingData = atoi(&zVal[11]);
! 127735: rc = SQLITE_OK;
! 127736: #endif
! 127737: }else{
! 127738: rc = SQLITE_ERROR;
! 127739: }
! 127740:
! 127741: return rc;
! 127742: }
! 127743:
! 127744: /*
! 127745: ** Delete all cached deferred doclists. Deferred doclists are cached
! 127746: ** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
! 127747: */
! 127748: SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
! 127749: Fts3DeferredToken *pDef;
! 127750: for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
! 127751: fts3PendingListDelete(pDef->pList);
! 127752: pDef->pList = 0;
! 127753: }
! 127754: }
! 127755:
! 127756: /*
! 127757: ** Free all entries in the pCsr->pDeffered list. Entries are added to
! 127758: ** this list using sqlite3Fts3DeferToken().
! 127759: */
! 127760: SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
! 127761: Fts3DeferredToken *pDef;
! 127762: Fts3DeferredToken *pNext;
! 127763: for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
! 127764: pNext = pDef->pNext;
! 127765: fts3PendingListDelete(pDef->pList);
! 127766: sqlite3_free(pDef);
! 127767: }
! 127768: pCsr->pDeferred = 0;
! 127769: }
! 127770:
! 127771: /*
! 127772: ** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
! 127773: ** based on the row that pCsr currently points to.
! 127774: **
! 127775: ** A deferred-doclist is like any other doclist with position information
! 127776: ** included, except that it only contains entries for a single row of the
! 127777: ** table, not for all rows.
! 127778: */
! 127779: SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
! 127780: int rc = SQLITE_OK; /* Return code */
! 127781: if( pCsr->pDeferred ){
! 127782: int i; /* Used to iterate through table columns */
! 127783: sqlite3_int64 iDocid; /* Docid of the row pCsr points to */
! 127784: Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */
! 127785:
! 127786: Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
! 127787: sqlite3_tokenizer *pT = p->pTokenizer;
! 127788: sqlite3_tokenizer_module const *pModule = pT->pModule;
! 127789:
! 127790: assert( pCsr->isRequireSeek==0 );
! 127791: iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
! 127792:
! 127793: for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
! 127794: const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
! 127795: sqlite3_tokenizer_cursor *pTC = 0;
! 127796:
! 127797: rc = pModule->xOpen(pT, zText, -1, &pTC);
! 127798: while( rc==SQLITE_OK ){
! 127799: char const *zToken; /* Buffer containing token */
! 127800: int nToken; /* Number of bytes in token */
! 127801: int iDum1, iDum2; /* Dummy variables */
! 127802: int iPos; /* Position of token in zText */
! 127803:
! 127804: pTC->pTokenizer = pT;
! 127805: rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
! 127806: for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
! 127807: Fts3PhraseToken *pPT = pDef->pToken;
! 127808: if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
! 127809: && (pPT->bFirst==0 || iPos==0)
! 127810: && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
! 127811: && (0==memcmp(zToken, pPT->z, pPT->n))
! 127812: ){
! 127813: fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
! 127814: }
! 127815: }
! 127816: }
! 127817: if( pTC ) pModule->xClose(pTC);
! 127818: if( rc==SQLITE_DONE ) rc = SQLITE_OK;
! 127819: }
! 127820:
! 127821: for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
! 127822: if( pDef->pList ){
! 127823: rc = fts3PendingListAppendVarint(&pDef->pList, 0);
! 127824: }
! 127825: }
! 127826: }
! 127827:
! 127828: return rc;
! 127829: }
! 127830:
! 127831: SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
! 127832: Fts3DeferredToken *p,
! 127833: char **ppData,
! 127834: int *pnData
! 127835: ){
! 127836: char *pRet;
! 127837: int nSkip;
! 127838: sqlite3_int64 dummy;
! 127839:
! 127840: *ppData = 0;
! 127841: *pnData = 0;
! 127842:
! 127843: if( p->pList==0 ){
! 127844: return SQLITE_OK;
! 127845: }
! 127846:
! 127847: pRet = (char *)sqlite3_malloc(p->pList->nData);
! 127848: if( !pRet ) return SQLITE_NOMEM;
! 127849:
! 127850: nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
! 127851: *pnData = p->pList->nData - nSkip;
! 127852: *ppData = pRet;
! 127853:
! 127854: memcpy(pRet, &p->pList->aData[nSkip], *pnData);
! 127855: return SQLITE_OK;
! 127856: }
! 127857:
! 127858: /*
! 127859: ** Add an entry for token pToken to the pCsr->pDeferred list.
! 127860: */
! 127861: SQLITE_PRIVATE int sqlite3Fts3DeferToken(
! 127862: Fts3Cursor *pCsr, /* Fts3 table cursor */
! 127863: Fts3PhraseToken *pToken, /* Token to defer */
! 127864: int iCol /* Column that token must appear in (or -1) */
! 127865: ){
! 127866: Fts3DeferredToken *pDeferred;
! 127867: pDeferred = sqlite3_malloc(sizeof(*pDeferred));
! 127868: if( !pDeferred ){
! 127869: return SQLITE_NOMEM;
! 127870: }
! 127871: memset(pDeferred, 0, sizeof(*pDeferred));
! 127872: pDeferred->pToken = pToken;
! 127873: pDeferred->pNext = pCsr->pDeferred;
! 127874: pDeferred->iCol = iCol;
! 127875: pCsr->pDeferred = pDeferred;
! 127876:
! 127877: assert( pToken->pDeferred==0 );
! 127878: pToken->pDeferred = pDeferred;
! 127879:
! 127880: return SQLITE_OK;
! 127881: }
! 127882:
! 127883: /*
! 127884: ** SQLite value pRowid contains the rowid of a row that may or may not be
! 127885: ** present in the FTS3 table. If it is, delete it and adjust the contents
! 127886: ** of subsiduary data structures accordingly.
! 127887: */
! 127888: static int fts3DeleteByRowid(
! 127889: Fts3Table *p,
! 127890: sqlite3_value *pRowid,
! 127891: int *pnDoc,
! 127892: u32 *aSzDel
! 127893: ){
! 127894: int isEmpty = 0;
! 127895: int rc = fts3IsEmpty(p, pRowid, &isEmpty);
! 127896: if( rc==SQLITE_OK ){
! 127897: if( isEmpty ){
! 127898: /* Deleting this row means the whole table is empty. In this case
! 127899: ** delete the contents of all three tables and throw away any
! 127900: ** data in the pendingTerms hash table. */
! 127901: rc = fts3DeleteAll(p, 1);
! 127902: *pnDoc = *pnDoc - 1;
! 127903: }else{
! 127904: sqlite3_int64 iRemove = sqlite3_value_int64(pRowid);
! 127905: rc = fts3PendingTermsDocid(p, iRemove);
! 127906: fts3DeleteTerms(&rc, p, pRowid, aSzDel);
! 127907: if( p->zContentTbl==0 ){
! 127908: fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
! 127909: if( sqlite3_changes(p->db) ) *pnDoc = *pnDoc - 1;
! 127910: }else{
! 127911: *pnDoc = *pnDoc - 1;
! 127912: }
! 127913: if( p->bHasDocsize ){
! 127914: fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
! 127915: }
! 127916: }
! 127917: }
! 127918:
! 127919: return rc;
! 127920: }
! 127921:
! 127922: /*
! 127923: ** This function does the work for the xUpdate method of FTS3 virtual
! 127924: ** tables.
! 127925: */
! 127926: SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
! 127927: sqlite3_vtab *pVtab, /* FTS3 vtab object */
! 127928: int nArg, /* Size of argument array */
! 127929: sqlite3_value **apVal, /* Array of arguments */
! 127930: sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
! 127931: ){
! 127932: Fts3Table *p = (Fts3Table *)pVtab;
! 127933: int rc = SQLITE_OK; /* Return Code */
! 127934: int isRemove = 0; /* True for an UPDATE or DELETE */
! 127935: u32 *aSzIns = 0; /* Sizes of inserted documents */
! 127936: u32 *aSzDel; /* Sizes of deleted documents */
! 127937: int nChng = 0; /* Net change in number of documents */
! 127938: int bInsertDone = 0;
! 127939:
! 127940: assert( p->pSegments==0 );
! 127941:
! 127942: /* Check for a "special" INSERT operation. One of the form:
! 127943: **
! 127944: ** INSERT INTO xyz(xyz) VALUES('command');
! 127945: */
! 127946: if( nArg>1
! 127947: && sqlite3_value_type(apVal[0])==SQLITE_NULL
! 127948: && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
! 127949: ){
! 127950: rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
! 127951: goto update_out;
! 127952: }
! 127953:
! 127954: /* Allocate space to hold the change in document sizes */
! 127955: aSzIns = sqlite3_malloc( sizeof(aSzIns[0])*(p->nColumn+1)*2 );
! 127956: if( aSzIns==0 ){
! 127957: rc = SQLITE_NOMEM;
! 127958: goto update_out;
! 127959: }
! 127960: aSzDel = &aSzIns[p->nColumn+1];
! 127961: memset(aSzIns, 0, sizeof(aSzIns[0])*(p->nColumn+1)*2);
! 127962:
! 127963: /* If this is an INSERT operation, or an UPDATE that modifies the rowid
! 127964: ** value, then this operation requires constraint handling.
! 127965: **
! 127966: ** If the on-conflict mode is REPLACE, this means that the existing row
! 127967: ** should be deleted from the database before inserting the new row. Or,
! 127968: ** if the on-conflict mode is other than REPLACE, then this method must
! 127969: ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
! 127970: ** modify the database file.
! 127971: */
! 127972: if( nArg>1 && p->zContentTbl==0 ){
! 127973: /* Find the value object that holds the new rowid value. */
! 127974: sqlite3_value *pNewRowid = apVal[3+p->nColumn];
! 127975: if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
! 127976: pNewRowid = apVal[1];
! 127977: }
! 127978:
! 127979: if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && (
! 127980: sqlite3_value_type(apVal[0])==SQLITE_NULL
! 127981: || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
! 127982: )){
! 127983: /* The new rowid is not NULL (in this case the rowid will be
! 127984: ** automatically assigned and there is no chance of a conflict), and
! 127985: ** the statement is either an INSERT or an UPDATE that modifies the
! 127986: ** rowid column. So if the conflict mode is REPLACE, then delete any
! 127987: ** existing row with rowid=pNewRowid.
! 127988: **
! 127989: ** Or, if the conflict mode is not REPLACE, insert the new record into
! 127990: ** the %_content table. If we hit the duplicate rowid constraint (or any
! 127991: ** other error) while doing so, return immediately.
! 127992: **
! 127993: ** This branch may also run if pNewRowid contains a value that cannot
! 127994: ** be losslessly converted to an integer. In this case, the eventual
! 127995: ** call to fts3InsertData() (either just below or further on in this
! 127996: ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
! 127997: ** invoked, it will delete zero rows (since no row will have
! 127998: ** docid=$pNewRowid if $pNewRowid is not an integer value).
! 127999: */
! 128000: if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
! 128001: rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
! 128002: }else{
! 128003: rc = fts3InsertData(p, apVal, pRowid);
! 128004: bInsertDone = 1;
! 128005: }
! 128006: }
! 128007: }
! 128008: if( rc!=SQLITE_OK ){
! 128009: goto update_out;
! 128010: }
! 128011:
! 128012: /* If this is a DELETE or UPDATE operation, remove the old record. */
! 128013: if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
! 128014: assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
! 128015: rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
! 128016: isRemove = 1;
! 128017: }
! 128018:
! 128019: /* If this is an INSERT or UPDATE operation, insert the new record. */
! 128020: if( nArg>1 && rc==SQLITE_OK ){
! 128021: if( bInsertDone==0 ){
! 128022: rc = fts3InsertData(p, apVal, pRowid);
! 128023: if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
! 128024: rc = FTS_CORRUPT_VTAB;
! 128025: }
! 128026: }
! 128027: if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
! 128028: rc = fts3PendingTermsDocid(p, *pRowid);
! 128029: }
! 128030: if( rc==SQLITE_OK ){
! 128031: assert( p->iPrevDocid==*pRowid );
! 128032: rc = fts3InsertTerms(p, apVal, aSzIns);
! 128033: }
! 128034: if( p->bHasDocsize ){
! 128035: fts3InsertDocsize(&rc, p, aSzIns);
! 128036: }
! 128037: nChng++;
! 128038: }
! 128039:
! 128040: if( p->bHasStat ){
! 128041: fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
! 128042: }
! 128043:
! 128044: update_out:
! 128045: sqlite3_free(aSzIns);
! 128046: sqlite3Fts3SegmentsClose(p);
! 128047: return rc;
! 128048: }
! 128049:
! 128050: /*
! 128051: ** Flush any data in the pending-terms hash table to disk. If successful,
! 128052: ** merge all segments in the database (including the new segment, if
! 128053: ** there was any data to flush) into a single segment.
! 128054: */
! 128055: SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
! 128056: int rc;
! 128057: rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
! 128058: if( rc==SQLITE_OK ){
! 128059: rc = fts3DoOptimize(p, 1);
! 128060: if( rc==SQLITE_OK || rc==SQLITE_DONE ){
! 128061: int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
! 128062: if( rc2!=SQLITE_OK ) rc = rc2;
! 128063: }else{
! 128064: sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
! 128065: sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
! 128066: }
! 128067: }
! 128068: sqlite3Fts3SegmentsClose(p);
! 128069: return rc;
! 128070: }
! 128071:
! 128072: #endif
! 128073:
! 128074: /************** End of fts3_write.c ******************************************/
! 128075: /************** Begin file fts3_snippet.c ************************************/
! 128076: /*
! 128077: ** 2009 Oct 23
! 128078: **
! 128079: ** The author disclaims copyright to this source code. In place of
! 128080: ** a legal notice, here is a blessing:
! 128081: **
! 128082: ** May you do good and not evil.
! 128083: ** May you find forgiveness for yourself and forgive others.
! 128084: ** May you share freely, never taking more than you give.
! 128085: **
! 128086: ******************************************************************************
! 128087: */
! 128088:
! 128089: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
! 128090:
! 128091: /* #include <string.h> */
! 128092: /* #include <assert.h> */
! 128093:
! 128094: /*
! 128095: ** Characters that may appear in the second argument to matchinfo().
! 128096: */
! 128097: #define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
! 128098: #define FTS3_MATCHINFO_NCOL 'c' /* 1 value */
! 128099: #define FTS3_MATCHINFO_NDOC 'n' /* 1 value */
! 128100: #define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */
! 128101: #define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */
! 128102: #define FTS3_MATCHINFO_LCS 's' /* nCol values */
! 128103: #define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */
! 128104:
! 128105: /*
! 128106: ** The default value for the second argument to matchinfo().
! 128107: */
! 128108: #define FTS3_MATCHINFO_DEFAULT "pcx"
! 128109:
! 128110:
! 128111: /*
! 128112: ** Used as an fts3ExprIterate() context when loading phrase doclists to
! 128113: ** Fts3Expr.aDoclist[]/nDoclist.
! 128114: */
! 128115: typedef struct LoadDoclistCtx LoadDoclistCtx;
! 128116: struct LoadDoclistCtx {
! 128117: Fts3Cursor *pCsr; /* FTS3 Cursor */
! 128118: int nPhrase; /* Number of phrases seen so far */
! 128119: int nToken; /* Number of tokens seen so far */
! 128120: };
! 128121:
! 128122: /*
! 128123: ** The following types are used as part of the implementation of the
! 128124: ** fts3BestSnippet() routine.
! 128125: */
! 128126: typedef struct SnippetIter SnippetIter;
! 128127: typedef struct SnippetPhrase SnippetPhrase;
! 128128: typedef struct SnippetFragment SnippetFragment;
! 128129:
! 128130: struct SnippetIter {
! 128131: Fts3Cursor *pCsr; /* Cursor snippet is being generated from */
! 128132: int iCol; /* Extract snippet from this column */
! 128133: int nSnippet; /* Requested snippet length (in tokens) */
! 128134: int nPhrase; /* Number of phrases in query */
! 128135: SnippetPhrase *aPhrase; /* Array of size nPhrase */
! 128136: int iCurrent; /* First token of current snippet */
! 128137: };
! 128138:
! 128139: struct SnippetPhrase {
! 128140: int nToken; /* Number of tokens in phrase */
! 128141: char *pList; /* Pointer to start of phrase position list */
! 128142: int iHead; /* Next value in position list */
! 128143: char *pHead; /* Position list data following iHead */
! 128144: int iTail; /* Next value in trailing position list */
! 128145: char *pTail; /* Position list data following iTail */
! 128146: };
! 128147:
! 128148: struct SnippetFragment {
! 128149: int iCol; /* Column snippet is extracted from */
! 128150: int iPos; /* Index of first token in snippet */
! 128151: u64 covered; /* Mask of query phrases covered */
! 128152: u64 hlmask; /* Mask of snippet terms to highlight */
! 128153: };
! 128154:
! 128155: /*
! 128156: ** This type is used as an fts3ExprIterate() context object while
! 128157: ** accumulating the data returned by the matchinfo() function.
! 128158: */
! 128159: typedef struct MatchInfo MatchInfo;
! 128160: struct MatchInfo {
! 128161: Fts3Cursor *pCursor; /* FTS3 Cursor */
! 128162: int nCol; /* Number of columns in table */
! 128163: int nPhrase; /* Number of matchable phrases in query */
! 128164: sqlite3_int64 nDoc; /* Number of docs in database */
! 128165: u32 *aMatchinfo; /* Pre-allocated buffer */
! 128166: };
! 128167:
! 128168:
! 128169:
! 128170: /*
! 128171: ** The snippet() and offsets() functions both return text values. An instance
! 128172: ** of the following structure is used to accumulate those values while the
! 128173: ** functions are running. See fts3StringAppend() for details.
! 128174: */
! 128175: typedef struct StrBuffer StrBuffer;
! 128176: struct StrBuffer {
! 128177: char *z; /* Pointer to buffer containing string */
! 128178: int n; /* Length of z in bytes (excl. nul-term) */
! 128179: int nAlloc; /* Allocated size of buffer z in bytes */
! 128180: };
! 128181:
! 128182:
! 128183: /*
! 128184: ** This function is used to help iterate through a position-list. A position
! 128185: ** list is a list of unique integers, sorted from smallest to largest. Each
! 128186: ** element of the list is represented by an FTS3 varint that takes the value
! 128187: ** of the difference between the current element and the previous one plus
! 128188: ** two. For example, to store the position-list:
! 128189: **
! 128190: ** 4 9 113
! 128191: **
! 128192: ** the three varints:
! 128193: **
! 128194: ** 6 7 106
! 128195: **
! 128196: ** are encoded.
! 128197: **
! 128198: ** When this function is called, *pp points to the start of an element of
! 128199: ** the list. *piPos contains the value of the previous entry in the list.
! 128200: ** After it returns, *piPos contains the value of the next element of the
! 128201: ** list and *pp is advanced to the following varint.
! 128202: */
! 128203: static void fts3GetDeltaPosition(char **pp, int *piPos){
! 128204: int iVal;
! 128205: *pp += sqlite3Fts3GetVarint32(*pp, &iVal);
! 128206: *piPos += (iVal-2);
! 128207: }
! 128208:
! 128209: /*
! 128210: ** Helper function for fts3ExprIterate() (see below).
! 128211: */
! 128212: static int fts3ExprIterate2(
! 128213: Fts3Expr *pExpr, /* Expression to iterate phrases of */
! 128214: int *piPhrase, /* Pointer to phrase counter */
! 128215: int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
! 128216: void *pCtx /* Second argument to pass to callback */
! 128217: ){
! 128218: int rc; /* Return code */
! 128219: int eType = pExpr->eType; /* Type of expression node pExpr */
! 128220:
! 128221: if( eType!=FTSQUERY_PHRASE ){
! 128222: assert( pExpr->pLeft && pExpr->pRight );
! 128223: rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
! 128224: if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
! 128225: rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
! 128226: }
! 128227: }else{
! 128228: rc = x(pExpr, *piPhrase, pCtx);
! 128229: (*piPhrase)++;
! 128230: }
! 128231: return rc;
! 128232: }
! 128233:
! 128234: /*
! 128235: ** Iterate through all phrase nodes in an FTS3 query, except those that
! 128236: ** are part of a sub-tree that is the right-hand-side of a NOT operator.
! 128237: ** For each phrase node found, the supplied callback function is invoked.
! 128238: **
! 128239: ** If the callback function returns anything other than SQLITE_OK,
! 128240: ** the iteration is abandoned and the error code returned immediately.
! 128241: ** Otherwise, SQLITE_OK is returned after a callback has been made for
! 128242: ** all eligible phrase nodes.
! 128243: */
! 128244: static int fts3ExprIterate(
! 128245: Fts3Expr *pExpr, /* Expression to iterate phrases of */
! 128246: int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
! 128247: void *pCtx /* Second argument to pass to callback */
! 128248: ){
! 128249: int iPhrase = 0; /* Variable used as the phrase counter */
! 128250: return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
! 128251: }
! 128252:
! 128253: /*
! 128254: ** This is an fts3ExprIterate() callback used while loading the doclists
! 128255: ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
! 128256: ** fts3ExprLoadDoclists().
! 128257: */
! 128258: static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
! 128259: int rc = SQLITE_OK;
! 128260: Fts3Phrase *pPhrase = pExpr->pPhrase;
! 128261: LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
! 128262:
! 128263: UNUSED_PARAMETER(iPhrase);
! 128264:
! 128265: p->nPhrase++;
! 128266: p->nToken += pPhrase->nToken;
! 128267:
! 128268: return rc;
! 128269: }
! 128270:
! 128271: /*
! 128272: ** Load the doclists for each phrase in the query associated with FTS3 cursor
! 128273: ** pCsr.
! 128274: **
! 128275: ** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
! 128276: ** phrases in the expression (all phrases except those directly or
! 128277: ** indirectly descended from the right-hand-side of a NOT operator). If
! 128278: ** pnToken is not NULL, then it is set to the number of tokens in all
! 128279: ** matchable phrases of the expression.
! 128280: */
! 128281: static int fts3ExprLoadDoclists(
! 128282: Fts3Cursor *pCsr, /* Fts3 cursor for current query */
! 128283: int *pnPhrase, /* OUT: Number of phrases in query */
! 128284: int *pnToken /* OUT: Number of tokens in query */
! 128285: ){
! 128286: int rc; /* Return Code */
! 128287: LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */
! 128288: sCtx.pCsr = pCsr;
! 128289: rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
! 128290: if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
! 128291: if( pnToken ) *pnToken = sCtx.nToken;
! 128292: return rc;
! 128293: }
! 128294:
! 128295: static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
! 128296: (*(int *)ctx)++;
! 128297: UNUSED_PARAMETER(pExpr);
! 128298: UNUSED_PARAMETER(iPhrase);
! 128299: return SQLITE_OK;
! 128300: }
! 128301: static int fts3ExprPhraseCount(Fts3Expr *pExpr){
! 128302: int nPhrase = 0;
! 128303: (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
! 128304: return nPhrase;
! 128305: }
! 128306:
! 128307: /*
! 128308: ** Advance the position list iterator specified by the first two
! 128309: ** arguments so that it points to the first element with a value greater
! 128310: ** than or equal to parameter iNext.
! 128311: */
! 128312: static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
! 128313: char *pIter = *ppIter;
! 128314: if( pIter ){
! 128315: int iIter = *piIter;
! 128316:
! 128317: while( iIter<iNext ){
! 128318: if( 0==(*pIter & 0xFE) ){
! 128319: iIter = -1;
! 128320: pIter = 0;
! 128321: break;
! 128322: }
! 128323: fts3GetDeltaPosition(&pIter, &iIter);
! 128324: }
! 128325:
! 128326: *piIter = iIter;
! 128327: *ppIter = pIter;
! 128328: }
! 128329: }
! 128330:
! 128331: /*
! 128332: ** Advance the snippet iterator to the next candidate snippet.
! 128333: */
! 128334: static int fts3SnippetNextCandidate(SnippetIter *pIter){
! 128335: int i; /* Loop counter */
! 128336:
! 128337: if( pIter->iCurrent<0 ){
! 128338: /* The SnippetIter object has just been initialized. The first snippet
! 128339: ** candidate always starts at offset 0 (even if this candidate has a
! 128340: ** score of 0.0).
! 128341: */
! 128342: pIter->iCurrent = 0;
! 128343:
! 128344: /* Advance the 'head' iterator of each phrase to the first offset that
! 128345: ** is greater than or equal to (iNext+nSnippet).
! 128346: */
! 128347: for(i=0; i<pIter->nPhrase; i++){
! 128348: SnippetPhrase *pPhrase = &pIter->aPhrase[i];
! 128349: fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
! 128350: }
! 128351: }else{
! 128352: int iStart;
! 128353: int iEnd = 0x7FFFFFFF;
! 128354:
! 128355: for(i=0; i<pIter->nPhrase; i++){
! 128356: SnippetPhrase *pPhrase = &pIter->aPhrase[i];
! 128357: if( pPhrase->pHead && pPhrase->iHead<iEnd ){
! 128358: iEnd = pPhrase->iHead;
! 128359: }
! 128360: }
! 128361: if( iEnd==0x7FFFFFFF ){
! 128362: return 1;
! 128363: }
! 128364:
! 128365: pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
! 128366: for(i=0; i<pIter->nPhrase; i++){
! 128367: SnippetPhrase *pPhrase = &pIter->aPhrase[i];
! 128368: fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
! 128369: fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
! 128370: }
! 128371: }
! 128372:
! 128373: return 0;
! 128374: }
! 128375:
! 128376: /*
! 128377: ** Retrieve information about the current candidate snippet of snippet
! 128378: ** iterator pIter.
! 128379: */
! 128380: static void fts3SnippetDetails(
! 128381: SnippetIter *pIter, /* Snippet iterator */
! 128382: u64 mCovered, /* Bitmask of phrases already covered */
! 128383: int *piToken, /* OUT: First token of proposed snippet */
! 128384: int *piScore, /* OUT: "Score" for this snippet */
! 128385: u64 *pmCover, /* OUT: Bitmask of phrases covered */
! 128386: u64 *pmHighlight /* OUT: Bitmask of terms to highlight */
! 128387: ){
! 128388: int iStart = pIter->iCurrent; /* First token of snippet */
! 128389: int iScore = 0; /* Score of this snippet */
! 128390: int i; /* Loop counter */
! 128391: u64 mCover = 0; /* Mask of phrases covered by this snippet */
! 128392: u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */
! 128393:
! 128394: for(i=0; i<pIter->nPhrase; i++){
! 128395: SnippetPhrase *pPhrase = &pIter->aPhrase[i];
! 128396: if( pPhrase->pTail ){
! 128397: char *pCsr = pPhrase->pTail;
! 128398: int iCsr = pPhrase->iTail;
! 128399:
! 128400: while( iCsr<(iStart+pIter->nSnippet) ){
! 128401: int j;
! 128402: u64 mPhrase = (u64)1 << i;
! 128403: u64 mPos = (u64)1 << (iCsr - iStart);
! 128404: assert( iCsr>=iStart );
! 128405: if( (mCover|mCovered)&mPhrase ){
! 128406: iScore++;
! 128407: }else{
! 128408: iScore += 1000;
! 128409: }
! 128410: mCover |= mPhrase;
! 128411:
! 128412: for(j=0; j<pPhrase->nToken; j++){
! 128413: mHighlight |= (mPos>>j);
! 128414: }
! 128415:
! 128416: if( 0==(*pCsr & 0x0FE) ) break;
! 128417: fts3GetDeltaPosition(&pCsr, &iCsr);
! 128418: }
! 128419: }
! 128420: }
! 128421:
! 128422: /* Set the output variables before returning. */
! 128423: *piToken = iStart;
! 128424: *piScore = iScore;
! 128425: *pmCover = mCover;
! 128426: *pmHighlight = mHighlight;
! 128427: }
! 128428:
! 128429: /*
! 128430: ** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
! 128431: ** Each invocation populates an element of the SnippetIter.aPhrase[] array.
! 128432: */
! 128433: static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
! 128434: SnippetIter *p = (SnippetIter *)ctx;
! 128435: SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
! 128436: char *pCsr;
! 128437:
! 128438: pPhrase->nToken = pExpr->pPhrase->nToken;
! 128439:
! 128440: pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
! 128441: if( pCsr ){
! 128442: int iFirst = 0;
! 128443: pPhrase->pList = pCsr;
! 128444: fts3GetDeltaPosition(&pCsr, &iFirst);
! 128445: assert( iFirst>=0 );
! 128446: pPhrase->pHead = pCsr;
! 128447: pPhrase->pTail = pCsr;
! 128448: pPhrase->iHead = iFirst;
! 128449: pPhrase->iTail = iFirst;
! 128450: }else{
! 128451: assert( pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 );
! 128452: }
! 128453:
! 128454: return SQLITE_OK;
! 128455: }
! 128456:
! 128457: /*
! 128458: ** Select the fragment of text consisting of nFragment contiguous tokens
! 128459: ** from column iCol that represent the "best" snippet. The best snippet
! 128460: ** is the snippet with the highest score, where scores are calculated
! 128461: ** by adding:
! 128462: **
! 128463: ** (a) +1 point for each occurence of a matchable phrase in the snippet.
! 128464: **
! 128465: ** (b) +1000 points for the first occurence of each matchable phrase in
! 128466: ** the snippet for which the corresponding mCovered bit is not set.
! 128467: **
! 128468: ** The selected snippet parameters are stored in structure *pFragment before
! 128469: ** returning. The score of the selected snippet is stored in *piScore
! 128470: ** before returning.
! 128471: */
! 128472: static int fts3BestSnippet(
! 128473: int nSnippet, /* Desired snippet length */
! 128474: Fts3Cursor *pCsr, /* Cursor to create snippet for */
! 128475: int iCol, /* Index of column to create snippet from */
! 128476: u64 mCovered, /* Mask of phrases already covered */
! 128477: u64 *pmSeen, /* IN/OUT: Mask of phrases seen */
! 128478: SnippetFragment *pFragment, /* OUT: Best snippet found */
! 128479: int *piScore /* OUT: Score of snippet pFragment */
! 128480: ){
! 128481: int rc; /* Return Code */
! 128482: int nList; /* Number of phrases in expression */
! 128483: SnippetIter sIter; /* Iterates through snippet candidates */
! 128484: int nByte; /* Number of bytes of space to allocate */
! 128485: int iBestScore = -1; /* Best snippet score found so far */
! 128486: int i; /* Loop counter */
! 128487:
! 128488: memset(&sIter, 0, sizeof(sIter));
! 128489:
! 128490: /* Iterate through the phrases in the expression to count them. The same
! 128491: ** callback makes sure the doclists are loaded for each phrase.
! 128492: */
! 128493: rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
! 128494: if( rc!=SQLITE_OK ){
! 128495: return rc;
! 128496: }
! 128497:
! 128498: /* Now that it is known how many phrases there are, allocate and zero
! 128499: ** the required space using malloc().
! 128500: */
! 128501: nByte = sizeof(SnippetPhrase) * nList;
! 128502: sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
! 128503: if( !sIter.aPhrase ){
! 128504: return SQLITE_NOMEM;
! 128505: }
! 128506: memset(sIter.aPhrase, 0, nByte);
! 128507:
! 128508: /* Initialize the contents of the SnippetIter object. Then iterate through
! 128509: ** the set of phrases in the expression to populate the aPhrase[] array.
! 128510: */
! 128511: sIter.pCsr = pCsr;
! 128512: sIter.iCol = iCol;
! 128513: sIter.nSnippet = nSnippet;
! 128514: sIter.nPhrase = nList;
! 128515: sIter.iCurrent = -1;
! 128516: (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
! 128517:
! 128518: /* Set the *pmSeen output variable. */
! 128519: for(i=0; i<nList; i++){
! 128520: if( sIter.aPhrase[i].pHead ){
! 128521: *pmSeen |= (u64)1 << i;
! 128522: }
! 128523: }
! 128524:
! 128525: /* Loop through all candidate snippets. Store the best snippet in
! 128526: ** *pFragment. Store its associated 'score' in iBestScore.
! 128527: */
! 128528: pFragment->iCol = iCol;
! 128529: while( !fts3SnippetNextCandidate(&sIter) ){
! 128530: int iPos;
! 128531: int iScore;
! 128532: u64 mCover;
! 128533: u64 mHighlight;
! 128534: fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight);
! 128535: assert( iScore>=0 );
! 128536: if( iScore>iBestScore ){
! 128537: pFragment->iPos = iPos;
! 128538: pFragment->hlmask = mHighlight;
! 128539: pFragment->covered = mCover;
! 128540: iBestScore = iScore;
! 128541: }
! 128542: }
! 128543:
! 128544: sqlite3_free(sIter.aPhrase);
! 128545: *piScore = iBestScore;
! 128546: return SQLITE_OK;
! 128547: }
! 128548:
! 128549:
! 128550: /*
! 128551: ** Append a string to the string-buffer passed as the first argument.
! 128552: **
! 128553: ** If nAppend is negative, then the length of the string zAppend is
! 128554: ** determined using strlen().
! 128555: */
! 128556: static int fts3StringAppend(
! 128557: StrBuffer *pStr, /* Buffer to append to */
! 128558: const char *zAppend, /* Pointer to data to append to buffer */
! 128559: int nAppend /* Size of zAppend in bytes (or -1) */
! 128560: ){
! 128561: if( nAppend<0 ){
! 128562: nAppend = (int)strlen(zAppend);
! 128563: }
! 128564:
! 128565: /* If there is insufficient space allocated at StrBuffer.z, use realloc()
! 128566: ** to grow the buffer until so that it is big enough to accomadate the
! 128567: ** appended data.
! 128568: */
! 128569: if( pStr->n+nAppend+1>=pStr->nAlloc ){
! 128570: int nAlloc = pStr->nAlloc+nAppend+100;
! 128571: char *zNew = sqlite3_realloc(pStr->z, nAlloc);
! 128572: if( !zNew ){
! 128573: return SQLITE_NOMEM;
! 128574: }
! 128575: pStr->z = zNew;
! 128576: pStr->nAlloc = nAlloc;
! 128577: }
! 128578:
! 128579: /* Append the data to the string buffer. */
! 128580: memcpy(&pStr->z[pStr->n], zAppend, nAppend);
! 128581: pStr->n += nAppend;
! 128582: pStr->z[pStr->n] = '\0';
! 128583:
! 128584: return SQLITE_OK;
! 128585: }
! 128586:
! 128587: /*
! 128588: ** The fts3BestSnippet() function often selects snippets that end with a
! 128589: ** query term. That is, the final term of the snippet is always a term
! 128590: ** that requires highlighting. For example, if 'X' is a highlighted term
! 128591: ** and '.' is a non-highlighted term, BestSnippet() may select:
! 128592: **
! 128593: ** ........X.....X
! 128594: **
! 128595: ** This function "shifts" the beginning of the snippet forward in the
! 128596: ** document so that there are approximately the same number of
! 128597: ** non-highlighted terms to the right of the final highlighted term as there
! 128598: ** are to the left of the first highlighted term. For example, to this:
! 128599: **
! 128600: ** ....X.....X....
! 128601: **
! 128602: ** This is done as part of extracting the snippet text, not when selecting
! 128603: ** the snippet. Snippet selection is done based on doclists only, so there
! 128604: ** is no way for fts3BestSnippet() to know whether or not the document
! 128605: ** actually contains terms that follow the final highlighted term.
! 128606: */
! 128607: static int fts3SnippetShift(
! 128608: Fts3Table *pTab, /* FTS3 table snippet comes from */
! 128609: int nSnippet, /* Number of tokens desired for snippet */
! 128610: const char *zDoc, /* Document text to extract snippet from */
! 128611: int nDoc, /* Size of buffer zDoc in bytes */
! 128612: int *piPos, /* IN/OUT: First token of snippet */
! 128613: u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */
! 128614: ){
! 128615: u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */
! 128616:
! 128617: if( hlmask ){
! 128618: int nLeft; /* Tokens to the left of first highlight */
! 128619: int nRight; /* Tokens to the right of last highlight */
! 128620: int nDesired; /* Ideal number of tokens to shift forward */
! 128621:
! 128622: for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
! 128623: for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
! 128624: nDesired = (nLeft-nRight)/2;
! 128625:
! 128626: /* Ideally, the start of the snippet should be pushed forward in the
! 128627: ** document nDesired tokens. This block checks if there are actually
! 128628: ** nDesired tokens to the right of the snippet. If so, *piPos and
! 128629: ** *pHlMask are updated to shift the snippet nDesired tokens to the
! 128630: ** right. Otherwise, the snippet is shifted by the number of tokens
! 128631: ** available.
! 128632: */
! 128633: if( nDesired>0 ){
! 128634: int nShift; /* Number of tokens to shift snippet by */
! 128635: int iCurrent = 0; /* Token counter */
! 128636: int rc; /* Return Code */
! 128637: sqlite3_tokenizer_module *pMod;
! 128638: sqlite3_tokenizer_cursor *pC;
! 128639: pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
! 128640:
! 128641: /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
! 128642: ** or more tokens in zDoc/nDoc.
! 128643: */
! 128644: rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
! 128645: if( rc!=SQLITE_OK ){
! 128646: return rc;
! 128647: }
! 128648: pC->pTokenizer = pTab->pTokenizer;
! 128649: while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
! 128650: const char *ZDUMMY; int DUMMY1, DUMMY2, DUMMY3;
! 128651: rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
! 128652: }
! 128653: pMod->xClose(pC);
! 128654: if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
! 128655:
! 128656: nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
! 128657: assert( nShift<=nDesired );
! 128658: if( nShift>0 ){
! 128659: *piPos += nShift;
! 128660: *pHlmask = hlmask >> nShift;
! 128661: }
! 128662: }
! 128663: }
! 128664: return SQLITE_OK;
! 128665: }
! 128666:
! 128667: /*
! 128668: ** Extract the snippet text for fragment pFragment from cursor pCsr and
! 128669: ** append it to string buffer pOut.
! 128670: */
! 128671: static int fts3SnippetText(
! 128672: Fts3Cursor *pCsr, /* FTS3 Cursor */
! 128673: SnippetFragment *pFragment, /* Snippet to extract */
! 128674: int iFragment, /* Fragment number */
! 128675: int isLast, /* True for final fragment in snippet */
! 128676: int nSnippet, /* Number of tokens in extracted snippet */
! 128677: const char *zOpen, /* String inserted before highlighted term */
! 128678: const char *zClose, /* String inserted after highlighted term */
! 128679: const char *zEllipsis, /* String inserted between snippets */
! 128680: StrBuffer *pOut /* Write output here */
! 128681: ){
! 128682: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 128683: int rc; /* Return code */
! 128684: const char *zDoc; /* Document text to extract snippet from */
! 128685: int nDoc; /* Size of zDoc in bytes */
! 128686: int iCurrent = 0; /* Current token number of document */
! 128687: int iEnd = 0; /* Byte offset of end of current token */
! 128688: int isShiftDone = 0; /* True after snippet is shifted */
! 128689: int iPos = pFragment->iPos; /* First token of snippet */
! 128690: u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
! 128691: int iCol = pFragment->iCol+1; /* Query column to extract text from */
! 128692: sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
! 128693: sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */
! 128694: const char *ZDUMMY; /* Dummy argument used with tokenizer */
! 128695: int DUMMY1; /* Dummy argument used with tokenizer */
! 128696:
! 128697: zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
! 128698: if( zDoc==0 ){
! 128699: if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
! 128700: return SQLITE_NOMEM;
! 128701: }
! 128702: return SQLITE_OK;
! 128703: }
! 128704: nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
! 128705:
! 128706: /* Open a token cursor on the document. */
! 128707: pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
! 128708: rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
! 128709: if( rc!=SQLITE_OK ){
! 128710: return rc;
! 128711: }
! 128712: pC->pTokenizer = pTab->pTokenizer;
! 128713:
! 128714: while( rc==SQLITE_OK ){
! 128715: int iBegin; /* Offset in zDoc of start of token */
! 128716: int iFin; /* Offset in zDoc of end of token */
! 128717: int isHighlight; /* True for highlighted terms */
! 128718:
! 128719: rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
! 128720: if( rc!=SQLITE_OK ){
! 128721: if( rc==SQLITE_DONE ){
! 128722: /* Special case - the last token of the snippet is also the last token
! 128723: ** of the column. Append any punctuation that occurred between the end
! 128724: ** of the previous token and the end of the document to the output.
! 128725: ** Then break out of the loop. */
! 128726: rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
! 128727: }
! 128728: break;
! 128729: }
! 128730: if( iCurrent<iPos ){ continue; }
! 128731:
! 128732: if( !isShiftDone ){
! 128733: int n = nDoc - iBegin;
! 128734: rc = fts3SnippetShift(pTab, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask);
! 128735: isShiftDone = 1;
! 128736:
! 128737: /* Now that the shift has been done, check if the initial "..." are
! 128738: ** required. They are required if (a) this is not the first fragment,
! 128739: ** or (b) this fragment does not begin at position 0 of its column.
! 128740: */
! 128741: if( rc==SQLITE_OK && (iPos>0 || iFragment>0) ){
! 128742: rc = fts3StringAppend(pOut, zEllipsis, -1);
! 128743: }
! 128744: if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
! 128745: }
! 128746:
! 128747: if( iCurrent>=(iPos+nSnippet) ){
! 128748: if( isLast ){
! 128749: rc = fts3StringAppend(pOut, zEllipsis, -1);
! 128750: }
! 128751: break;
! 128752: }
! 128753:
! 128754: /* Set isHighlight to true if this term should be highlighted. */
! 128755: isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
! 128756:
! 128757: if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
! 128758: if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
! 128759: if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
! 128760: if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
! 128761:
! 128762: iEnd = iFin;
! 128763: }
! 128764:
! 128765: pMod->xClose(pC);
! 128766: return rc;
! 128767: }
! 128768:
! 128769:
! 128770: /*
! 128771: ** This function is used to count the entries in a column-list (a
! 128772: ** delta-encoded list of term offsets within a single column of a single
! 128773: ** row). When this function is called, *ppCollist should point to the
! 128774: ** beginning of the first varint in the column-list (the varint that
! 128775: ** contains the position of the first matching term in the column data).
! 128776: ** Before returning, *ppCollist is set to point to the first byte after
! 128777: ** the last varint in the column-list (either the 0x00 signifying the end
! 128778: ** of the position-list, or the 0x01 that precedes the column number of
! 128779: ** the next column in the position-list).
! 128780: **
! 128781: ** The number of elements in the column-list is returned.
! 128782: */
! 128783: static int fts3ColumnlistCount(char **ppCollist){
! 128784: char *pEnd = *ppCollist;
! 128785: char c = 0;
! 128786: int nEntry = 0;
! 128787:
! 128788: /* A column-list is terminated by either a 0x01 or 0x00. */
! 128789: while( 0xFE & (*pEnd | c) ){
! 128790: c = *pEnd++ & 0x80;
! 128791: if( !c ) nEntry++;
! 128792: }
! 128793:
! 128794: *ppCollist = pEnd;
! 128795: return nEntry;
! 128796: }
! 128797:
! 128798: /*
! 128799: ** fts3ExprIterate() callback used to collect the "global" matchinfo stats
! 128800: ** for a single query.
! 128801: **
! 128802: ** fts3ExprIterate() callback to load the 'global' elements of a
! 128803: ** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
! 128804: ** of the matchinfo array that are constant for all rows returned by the
! 128805: ** current query.
! 128806: **
! 128807: ** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
! 128808: ** function populates Matchinfo.aMatchinfo[] as follows:
! 128809: **
! 128810: ** for(iCol=0; iCol<nCol; iCol++){
! 128811: ** aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
! 128812: ** aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
! 128813: ** }
! 128814: **
! 128815: ** where X is the number of matches for phrase iPhrase is column iCol of all
! 128816: ** rows of the table. Y is the number of rows for which column iCol contains
! 128817: ** at least one instance of phrase iPhrase.
! 128818: **
! 128819: ** If the phrase pExpr consists entirely of deferred tokens, then all X and
! 128820: ** Y values are set to nDoc, where nDoc is the number of documents in the
! 128821: ** file system. This is done because the full-text index doclist is required
! 128822: ** to calculate these values properly, and the full-text index doclist is
! 128823: ** not available for deferred tokens.
! 128824: */
! 128825: static int fts3ExprGlobalHitsCb(
! 128826: Fts3Expr *pExpr, /* Phrase expression node */
! 128827: int iPhrase, /* Phrase number (numbered from zero) */
! 128828: void *pCtx /* Pointer to MatchInfo structure */
! 128829: ){
! 128830: MatchInfo *p = (MatchInfo *)pCtx;
! 128831: return sqlite3Fts3EvalPhraseStats(
! 128832: p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
! 128833: );
! 128834: }
! 128835:
! 128836: /*
! 128837: ** fts3ExprIterate() callback used to collect the "local" part of the
! 128838: ** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
! 128839: ** array that are different for each row returned by the query.
! 128840: */
! 128841: static int fts3ExprLocalHitsCb(
! 128842: Fts3Expr *pExpr, /* Phrase expression node */
! 128843: int iPhrase, /* Phrase number */
! 128844: void *pCtx /* Pointer to MatchInfo structure */
! 128845: ){
! 128846: MatchInfo *p = (MatchInfo *)pCtx;
! 128847: int iStart = iPhrase * p->nCol * 3;
! 128848: int i;
! 128849:
! 128850: for(i=0; i<p->nCol; i++){
! 128851: char *pCsr;
! 128852: pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i);
! 128853: if( pCsr ){
! 128854: p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
! 128855: }else{
! 128856: p->aMatchinfo[iStart+i*3] = 0;
! 128857: }
! 128858: }
! 128859:
! 128860: return SQLITE_OK;
! 128861: }
! 128862:
! 128863: static int fts3MatchinfoCheck(
! 128864: Fts3Table *pTab,
! 128865: char cArg,
! 128866: char **pzErr
! 128867: ){
! 128868: if( (cArg==FTS3_MATCHINFO_NPHRASE)
! 128869: || (cArg==FTS3_MATCHINFO_NCOL)
! 128870: || (cArg==FTS3_MATCHINFO_NDOC && pTab->bHasStat)
! 128871: || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bHasStat)
! 128872: || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
! 128873: || (cArg==FTS3_MATCHINFO_LCS)
! 128874: || (cArg==FTS3_MATCHINFO_HITS)
! 128875: ){
! 128876: return SQLITE_OK;
! 128877: }
! 128878: *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
! 128879: return SQLITE_ERROR;
! 128880: }
! 128881:
! 128882: static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
! 128883: int nVal; /* Number of integers output by cArg */
! 128884:
! 128885: switch( cArg ){
! 128886: case FTS3_MATCHINFO_NDOC:
! 128887: case FTS3_MATCHINFO_NPHRASE:
! 128888: case FTS3_MATCHINFO_NCOL:
! 128889: nVal = 1;
! 128890: break;
! 128891:
! 128892: case FTS3_MATCHINFO_AVGLENGTH:
! 128893: case FTS3_MATCHINFO_LENGTH:
! 128894: case FTS3_MATCHINFO_LCS:
! 128895: nVal = pInfo->nCol;
! 128896: break;
! 128897:
! 128898: default:
! 128899: assert( cArg==FTS3_MATCHINFO_HITS );
! 128900: nVal = pInfo->nCol * pInfo->nPhrase * 3;
! 128901: break;
! 128902: }
! 128903:
! 128904: return nVal;
! 128905: }
! 128906:
! 128907: static int fts3MatchinfoSelectDoctotal(
! 128908: Fts3Table *pTab,
! 128909: sqlite3_stmt **ppStmt,
! 128910: sqlite3_int64 *pnDoc,
! 128911: const char **paLen
! 128912: ){
! 128913: sqlite3_stmt *pStmt;
! 128914: const char *a;
! 128915: sqlite3_int64 nDoc;
! 128916:
! 128917: if( !*ppStmt ){
! 128918: int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
! 128919: if( rc!=SQLITE_OK ) return rc;
! 128920: }
! 128921: pStmt = *ppStmt;
! 128922: assert( sqlite3_data_count(pStmt)==1 );
! 128923:
! 128924: a = sqlite3_column_blob(pStmt, 0);
! 128925: a += sqlite3Fts3GetVarint(a, &nDoc);
! 128926: if( nDoc==0 ) return FTS_CORRUPT_VTAB;
! 128927: *pnDoc = (u32)nDoc;
! 128928:
! 128929: if( paLen ) *paLen = a;
! 128930: return SQLITE_OK;
! 128931: }
! 128932:
! 128933: /*
! 128934: ** An instance of the following structure is used to store state while
! 128935: ** iterating through a multi-column position-list corresponding to the
! 128936: ** hits for a single phrase on a single row in order to calculate the
! 128937: ** values for a matchinfo() FTS3_MATCHINFO_LCS request.
! 128938: */
! 128939: typedef struct LcsIterator LcsIterator;
! 128940: struct LcsIterator {
! 128941: Fts3Expr *pExpr; /* Pointer to phrase expression */
! 128942: int iPosOffset; /* Tokens count up to end of this phrase */
! 128943: char *pRead; /* Cursor used to iterate through aDoclist */
! 128944: int iPos; /* Current position */
! 128945: };
! 128946:
! 128947: /*
! 128948: ** If LcsIterator.iCol is set to the following value, the iterator has
! 128949: ** finished iterating through all offsets for all columns.
! 128950: */
! 128951: #define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
! 128952:
! 128953: static int fts3MatchinfoLcsCb(
! 128954: Fts3Expr *pExpr, /* Phrase expression node */
! 128955: int iPhrase, /* Phrase number (numbered from zero) */
! 128956: void *pCtx /* Pointer to MatchInfo structure */
! 128957: ){
! 128958: LcsIterator *aIter = (LcsIterator *)pCtx;
! 128959: aIter[iPhrase].pExpr = pExpr;
! 128960: return SQLITE_OK;
! 128961: }
! 128962:
! 128963: /*
! 128964: ** Advance the iterator passed as an argument to the next position. Return
! 128965: ** 1 if the iterator is at EOF or if it now points to the start of the
! 128966: ** position list for the next column.
! 128967: */
! 128968: static int fts3LcsIteratorAdvance(LcsIterator *pIter){
! 128969: char *pRead = pIter->pRead;
! 128970: sqlite3_int64 iRead;
! 128971: int rc = 0;
! 128972:
! 128973: pRead += sqlite3Fts3GetVarint(pRead, &iRead);
! 128974: if( iRead==0 || iRead==1 ){
! 128975: pRead = 0;
! 128976: rc = 1;
! 128977: }else{
! 128978: pIter->iPos += (int)(iRead-2);
! 128979: }
! 128980:
! 128981: pIter->pRead = pRead;
! 128982: return rc;
! 128983: }
! 128984:
! 128985: /*
! 128986: ** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
! 128987: **
! 128988: ** If the call is successful, the longest-common-substring lengths for each
! 128989: ** column are written into the first nCol elements of the pInfo->aMatchinfo[]
! 128990: ** array before returning. SQLITE_OK is returned in this case.
! 128991: **
! 128992: ** Otherwise, if an error occurs, an SQLite error code is returned and the
! 128993: ** data written to the first nCol elements of pInfo->aMatchinfo[] is
! 128994: ** undefined.
! 128995: */
! 128996: static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
! 128997: LcsIterator *aIter;
! 128998: int i;
! 128999: int iCol;
! 129000: int nToken = 0;
! 129001:
! 129002: /* Allocate and populate the array of LcsIterator objects. The array
! 129003: ** contains one element for each matchable phrase in the query.
! 129004: **/
! 129005: aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
! 129006: if( !aIter ) return SQLITE_NOMEM;
! 129007: memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
! 129008: (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
! 129009:
! 129010: for(i=0; i<pInfo->nPhrase; i++){
! 129011: LcsIterator *pIter = &aIter[i];
! 129012: nToken -= pIter->pExpr->pPhrase->nToken;
! 129013: pIter->iPosOffset = nToken;
! 129014: }
! 129015:
! 129016: for(iCol=0; iCol<pInfo->nCol; iCol++){
! 129017: int nLcs = 0; /* LCS value for this column */
! 129018: int nLive = 0; /* Number of iterators in aIter not at EOF */
! 129019:
! 129020: for(i=0; i<pInfo->nPhrase; i++){
! 129021: LcsIterator *pIt = &aIter[i];
! 129022: pIt->pRead = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol);
! 129023: if( pIt->pRead ){
! 129024: pIt->iPos = pIt->iPosOffset;
! 129025: fts3LcsIteratorAdvance(&aIter[i]);
! 129026: nLive++;
! 129027: }
! 129028: }
! 129029:
! 129030: while( nLive>0 ){
! 129031: LcsIterator *pAdv = 0; /* The iterator to advance by one position */
! 129032: int nThisLcs = 0; /* LCS for the current iterator positions */
! 129033:
! 129034: for(i=0; i<pInfo->nPhrase; i++){
! 129035: LcsIterator *pIter = &aIter[i];
! 129036: if( pIter->pRead==0 ){
! 129037: /* This iterator is already at EOF for this column. */
! 129038: nThisLcs = 0;
! 129039: }else{
! 129040: if( pAdv==0 || pIter->iPos<pAdv->iPos ){
! 129041: pAdv = pIter;
! 129042: }
! 129043: if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
! 129044: nThisLcs++;
! 129045: }else{
! 129046: nThisLcs = 1;
! 129047: }
! 129048: if( nThisLcs>nLcs ) nLcs = nThisLcs;
! 129049: }
! 129050: }
! 129051: if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
! 129052: }
! 129053:
! 129054: pInfo->aMatchinfo[iCol] = nLcs;
! 129055: }
! 129056:
! 129057: sqlite3_free(aIter);
! 129058: return SQLITE_OK;
! 129059: }
! 129060:
! 129061: /*
! 129062: ** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
! 129063: ** be returned by the matchinfo() function. Argument zArg contains the
! 129064: ** format string passed as the second argument to matchinfo (or the
! 129065: ** default value "pcx" if no second argument was specified). The format
! 129066: ** string has already been validated and the pInfo->aMatchinfo[] array
! 129067: ** is guaranteed to be large enough for the output.
! 129068: **
! 129069: ** If bGlobal is true, then populate all fields of the matchinfo() output.
! 129070: ** If it is false, then assume that those fields that do not change between
! 129071: ** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
! 129072: ** have already been populated.
! 129073: **
! 129074: ** Return SQLITE_OK if successful, or an SQLite error code if an error
! 129075: ** occurs. If a value other than SQLITE_OK is returned, the state the
! 129076: ** pInfo->aMatchinfo[] buffer is left in is undefined.
! 129077: */
! 129078: static int fts3MatchinfoValues(
! 129079: Fts3Cursor *pCsr, /* FTS3 cursor object */
! 129080: int bGlobal, /* True to grab the global stats */
! 129081: MatchInfo *pInfo, /* Matchinfo context object */
! 129082: const char *zArg /* Matchinfo format string */
! 129083: ){
! 129084: int rc = SQLITE_OK;
! 129085: int i;
! 129086: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 129087: sqlite3_stmt *pSelect = 0;
! 129088:
! 129089: for(i=0; rc==SQLITE_OK && zArg[i]; i++){
! 129090:
! 129091: switch( zArg[i] ){
! 129092: case FTS3_MATCHINFO_NPHRASE:
! 129093: if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
! 129094: break;
! 129095:
! 129096: case FTS3_MATCHINFO_NCOL:
! 129097: if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
! 129098: break;
! 129099:
! 129100: case FTS3_MATCHINFO_NDOC:
! 129101: if( bGlobal ){
! 129102: sqlite3_int64 nDoc = 0;
! 129103: rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
! 129104: pInfo->aMatchinfo[0] = (u32)nDoc;
! 129105: }
! 129106: break;
! 129107:
! 129108: case FTS3_MATCHINFO_AVGLENGTH:
! 129109: if( bGlobal ){
! 129110: sqlite3_int64 nDoc; /* Number of rows in table */
! 129111: const char *a; /* Aggregate column length array */
! 129112:
! 129113: rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
! 129114: if( rc==SQLITE_OK ){
! 129115: int iCol;
! 129116: for(iCol=0; iCol<pInfo->nCol; iCol++){
! 129117: u32 iVal;
! 129118: sqlite3_int64 nToken;
! 129119: a += sqlite3Fts3GetVarint(a, &nToken);
! 129120: iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
! 129121: pInfo->aMatchinfo[iCol] = iVal;
! 129122: }
! 129123: }
! 129124: }
! 129125: break;
! 129126:
! 129127: case FTS3_MATCHINFO_LENGTH: {
! 129128: sqlite3_stmt *pSelectDocsize = 0;
! 129129: rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
! 129130: if( rc==SQLITE_OK ){
! 129131: int iCol;
! 129132: const char *a = sqlite3_column_blob(pSelectDocsize, 0);
! 129133: for(iCol=0; iCol<pInfo->nCol; iCol++){
! 129134: sqlite3_int64 nToken;
! 129135: a += sqlite3Fts3GetVarint(a, &nToken);
! 129136: pInfo->aMatchinfo[iCol] = (u32)nToken;
! 129137: }
! 129138: }
! 129139: sqlite3_reset(pSelectDocsize);
! 129140: break;
! 129141: }
! 129142:
! 129143: case FTS3_MATCHINFO_LCS:
! 129144: rc = fts3ExprLoadDoclists(pCsr, 0, 0);
! 129145: if( rc==SQLITE_OK ){
! 129146: rc = fts3MatchinfoLcs(pCsr, pInfo);
! 129147: }
! 129148: break;
! 129149:
! 129150: default: {
! 129151: Fts3Expr *pExpr;
! 129152: assert( zArg[i]==FTS3_MATCHINFO_HITS );
! 129153: pExpr = pCsr->pExpr;
! 129154: rc = fts3ExprLoadDoclists(pCsr, 0, 0);
! 129155: if( rc!=SQLITE_OK ) break;
! 129156: if( bGlobal ){
! 129157: if( pCsr->pDeferred ){
! 129158: rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
! 129159: if( rc!=SQLITE_OK ) break;
! 129160: }
! 129161: rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
! 129162: if( rc!=SQLITE_OK ) break;
! 129163: }
! 129164: (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
! 129165: break;
! 129166: }
! 129167: }
! 129168:
! 129169: pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
! 129170: }
! 129171:
! 129172: sqlite3_reset(pSelect);
! 129173: return rc;
! 129174: }
! 129175:
! 129176:
! 129177: /*
! 129178: ** Populate pCsr->aMatchinfo[] with data for the current row. The
! 129179: ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
! 129180: */
! 129181: static int fts3GetMatchinfo(
! 129182: Fts3Cursor *pCsr, /* FTS3 Cursor object */
! 129183: const char *zArg /* Second argument to matchinfo() function */
! 129184: ){
! 129185: MatchInfo sInfo;
! 129186: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 129187: int rc = SQLITE_OK;
! 129188: int bGlobal = 0; /* Collect 'global' stats as well as local */
! 129189:
! 129190: memset(&sInfo, 0, sizeof(MatchInfo));
! 129191: sInfo.pCursor = pCsr;
! 129192: sInfo.nCol = pTab->nColumn;
! 129193:
! 129194: /* If there is cached matchinfo() data, but the format string for the
! 129195: ** cache does not match the format string for this request, discard
! 129196: ** the cached data. */
! 129197: if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
! 129198: assert( pCsr->aMatchinfo );
! 129199: sqlite3_free(pCsr->aMatchinfo);
! 129200: pCsr->zMatchinfo = 0;
! 129201: pCsr->aMatchinfo = 0;
! 129202: }
! 129203:
! 129204: /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
! 129205: ** matchinfo function has been called for this query. In this case
! 129206: ** allocate the array used to accumulate the matchinfo data and
! 129207: ** initialize those elements that are constant for every row.
! 129208: */
! 129209: if( pCsr->aMatchinfo==0 ){
! 129210: int nMatchinfo = 0; /* Number of u32 elements in match-info */
! 129211: int nArg; /* Bytes in zArg */
! 129212: int i; /* Used to iterate through zArg */
! 129213:
! 129214: /* Determine the number of phrases in the query */
! 129215: pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
! 129216: sInfo.nPhrase = pCsr->nPhrase;
! 129217:
! 129218: /* Determine the number of integers in the buffer returned by this call. */
! 129219: for(i=0; zArg[i]; i++){
! 129220: nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
! 129221: }
! 129222:
! 129223: /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
! 129224: nArg = (int)strlen(zArg);
! 129225: pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
! 129226: if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
! 129227:
! 129228: pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
! 129229: pCsr->nMatchinfo = nMatchinfo;
! 129230: memcpy(pCsr->zMatchinfo, zArg, nArg+1);
! 129231: memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
! 129232: pCsr->isMatchinfoNeeded = 1;
! 129233: bGlobal = 1;
! 129234: }
! 129235:
! 129236: sInfo.aMatchinfo = pCsr->aMatchinfo;
! 129237: sInfo.nPhrase = pCsr->nPhrase;
! 129238: if( pCsr->isMatchinfoNeeded ){
! 129239: rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
! 129240: pCsr->isMatchinfoNeeded = 0;
! 129241: }
! 129242:
! 129243: return rc;
! 129244: }
! 129245:
! 129246: /*
! 129247: ** Implementation of snippet() function.
! 129248: */
! 129249: SQLITE_PRIVATE void sqlite3Fts3Snippet(
! 129250: sqlite3_context *pCtx, /* SQLite function call context */
! 129251: Fts3Cursor *pCsr, /* Cursor object */
! 129252: const char *zStart, /* Snippet start text - "<b>" */
! 129253: const char *zEnd, /* Snippet end text - "</b>" */
! 129254: const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */
! 129255: int iCol, /* Extract snippet from this column */
! 129256: int nToken /* Approximate number of tokens in snippet */
! 129257: ){
! 129258: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 129259: int rc = SQLITE_OK;
! 129260: int i;
! 129261: StrBuffer res = {0, 0, 0};
! 129262:
! 129263: /* The returned text includes up to four fragments of text extracted from
! 129264: ** the data in the current row. The first iteration of the for(...) loop
! 129265: ** below attempts to locate a single fragment of text nToken tokens in
! 129266: ** size that contains at least one instance of all phrases in the query
! 129267: ** expression that appear in the current row. If such a fragment of text
! 129268: ** cannot be found, the second iteration of the loop attempts to locate
! 129269: ** a pair of fragments, and so on.
! 129270: */
! 129271: int nSnippet = 0; /* Number of fragments in this snippet */
! 129272: SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */
! 129273: int nFToken = -1; /* Number of tokens in each fragment */
! 129274:
! 129275: if( !pCsr->pExpr ){
! 129276: sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
! 129277: return;
! 129278: }
! 129279:
! 129280: for(nSnippet=1; 1; nSnippet++){
! 129281:
! 129282: int iSnip; /* Loop counter 0..nSnippet-1 */
! 129283: u64 mCovered = 0; /* Bitmask of phrases covered by snippet */
! 129284: u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */
! 129285:
! 129286: if( nToken>=0 ){
! 129287: nFToken = (nToken+nSnippet-1) / nSnippet;
! 129288: }else{
! 129289: nFToken = -1 * nToken;
! 129290: }
! 129291:
! 129292: for(iSnip=0; iSnip<nSnippet; iSnip++){
! 129293: int iBestScore = -1; /* Best score of columns checked so far */
! 129294: int iRead; /* Used to iterate through columns */
! 129295: SnippetFragment *pFragment = &aSnippet[iSnip];
! 129296:
! 129297: memset(pFragment, 0, sizeof(*pFragment));
! 129298:
! 129299: /* Loop through all columns of the table being considered for snippets.
! 129300: ** If the iCol argument to this function was negative, this means all
! 129301: ** columns of the FTS3 table. Otherwise, only column iCol is considered.
! 129302: */
! 129303: for(iRead=0; iRead<pTab->nColumn; iRead++){
! 129304: SnippetFragment sF = {0, 0, 0, 0};
! 129305: int iS;
! 129306: if( iCol>=0 && iRead!=iCol ) continue;
! 129307:
! 129308: /* Find the best snippet of nFToken tokens in column iRead. */
! 129309: rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
! 129310: if( rc!=SQLITE_OK ){
! 129311: goto snippet_out;
! 129312: }
! 129313: if( iS>iBestScore ){
! 129314: *pFragment = sF;
! 129315: iBestScore = iS;
! 129316: }
! 129317: }
! 129318:
! 129319: mCovered |= pFragment->covered;
! 129320: }
! 129321:
! 129322: /* If all query phrases seen by fts3BestSnippet() are present in at least
! 129323: ** one of the nSnippet snippet fragments, break out of the loop.
! 129324: */
! 129325: assert( (mCovered&mSeen)==mCovered );
! 129326: if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
! 129327: }
! 129328:
! 129329: assert( nFToken>0 );
! 129330:
! 129331: for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
! 129332: rc = fts3SnippetText(pCsr, &aSnippet[i],
! 129333: i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
! 129334: );
! 129335: }
! 129336:
! 129337: snippet_out:
! 129338: sqlite3Fts3SegmentsClose(pTab);
! 129339: if( rc!=SQLITE_OK ){
! 129340: sqlite3_result_error_code(pCtx, rc);
! 129341: sqlite3_free(res.z);
! 129342: }else{
! 129343: sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
! 129344: }
! 129345: }
! 129346:
! 129347:
! 129348: typedef struct TermOffset TermOffset;
! 129349: typedef struct TermOffsetCtx TermOffsetCtx;
! 129350:
! 129351: struct TermOffset {
! 129352: char *pList; /* Position-list */
! 129353: int iPos; /* Position just read from pList */
! 129354: int iOff; /* Offset of this term from read positions */
! 129355: };
! 129356:
! 129357: struct TermOffsetCtx {
! 129358: Fts3Cursor *pCsr;
! 129359: int iCol; /* Column of table to populate aTerm for */
! 129360: int iTerm;
! 129361: sqlite3_int64 iDocid;
! 129362: TermOffset *aTerm;
! 129363: };
! 129364:
! 129365: /*
! 129366: ** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
! 129367: */
! 129368: static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
! 129369: TermOffsetCtx *p = (TermOffsetCtx *)ctx;
! 129370: int nTerm; /* Number of tokens in phrase */
! 129371: int iTerm; /* For looping through nTerm phrase terms */
! 129372: char *pList; /* Pointer to position list for phrase */
! 129373: int iPos = 0; /* First position in position-list */
! 129374:
! 129375: UNUSED_PARAMETER(iPhrase);
! 129376: pList = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
! 129377: nTerm = pExpr->pPhrase->nToken;
! 129378: if( pList ){
! 129379: fts3GetDeltaPosition(&pList, &iPos);
! 129380: assert( iPos>=0 );
! 129381: }
! 129382:
! 129383: for(iTerm=0; iTerm<nTerm; iTerm++){
! 129384: TermOffset *pT = &p->aTerm[p->iTerm++];
! 129385: pT->iOff = nTerm-iTerm-1;
! 129386: pT->pList = pList;
! 129387: pT->iPos = iPos;
! 129388: }
! 129389:
! 129390: return SQLITE_OK;
! 129391: }
! 129392:
! 129393: /*
! 129394: ** Implementation of offsets() function.
! 129395: */
! 129396: SQLITE_PRIVATE void sqlite3Fts3Offsets(
! 129397: sqlite3_context *pCtx, /* SQLite function call context */
! 129398: Fts3Cursor *pCsr /* Cursor object */
! 129399: ){
! 129400: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 129401: sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
! 129402: const char *ZDUMMY; /* Dummy argument used with xNext() */
! 129403: int NDUMMY; /* Dummy argument used with xNext() */
! 129404: int rc; /* Return Code */
! 129405: int nToken; /* Number of tokens in query */
! 129406: int iCol; /* Column currently being processed */
! 129407: StrBuffer res = {0, 0, 0}; /* Result string */
! 129408: TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */
! 129409:
! 129410: if( !pCsr->pExpr ){
! 129411: sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
! 129412: return;
! 129413: }
! 129414:
! 129415: memset(&sCtx, 0, sizeof(sCtx));
! 129416: assert( pCsr->isRequireSeek==0 );
! 129417:
! 129418: /* Count the number of terms in the query */
! 129419: rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
! 129420: if( rc!=SQLITE_OK ) goto offsets_out;
! 129421:
! 129422: /* Allocate the array of TermOffset iterators. */
! 129423: sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
! 129424: if( 0==sCtx.aTerm ){
! 129425: rc = SQLITE_NOMEM;
! 129426: goto offsets_out;
! 129427: }
! 129428: sCtx.iDocid = pCsr->iPrevId;
! 129429: sCtx.pCsr = pCsr;
! 129430:
! 129431: /* Loop through the table columns, appending offset information to
! 129432: ** string-buffer res for each column.
! 129433: */
! 129434: for(iCol=0; iCol<pTab->nColumn; iCol++){
! 129435: sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
! 129436: int iStart;
! 129437: int iEnd;
! 129438: int iCurrent;
! 129439: const char *zDoc;
! 129440: int nDoc;
! 129441:
! 129442: /* Initialize the contents of sCtx.aTerm[] for column iCol. There is
! 129443: ** no way that this operation can fail, so the return code from
! 129444: ** fts3ExprIterate() can be discarded.
! 129445: */
! 129446: sCtx.iCol = iCol;
! 129447: sCtx.iTerm = 0;
! 129448: (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
! 129449:
! 129450: /* Retreive the text stored in column iCol. If an SQL NULL is stored
! 129451: ** in column iCol, jump immediately to the next iteration of the loop.
! 129452: ** If an OOM occurs while retrieving the data (this can happen if SQLite
! 129453: ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
! 129454: ** to the caller.
! 129455: */
! 129456: zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
! 129457: nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
! 129458: if( zDoc==0 ){
! 129459: if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
! 129460: continue;
! 129461: }
! 129462: rc = SQLITE_NOMEM;
! 129463: goto offsets_out;
! 129464: }
! 129465:
! 129466: /* Initialize a tokenizer iterator to iterate through column iCol. */
! 129467: rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
! 129468: if( rc!=SQLITE_OK ) goto offsets_out;
! 129469: pC->pTokenizer = pTab->pTokenizer;
! 129470:
! 129471: rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
! 129472: while( rc==SQLITE_OK ){
! 129473: int i; /* Used to loop through terms */
! 129474: int iMinPos = 0x7FFFFFFF; /* Position of next token */
! 129475: TermOffset *pTerm = 0; /* TermOffset associated with next token */
! 129476:
! 129477: for(i=0; i<nToken; i++){
! 129478: TermOffset *pT = &sCtx.aTerm[i];
! 129479: if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
! 129480: iMinPos = pT->iPos-pT->iOff;
! 129481: pTerm = pT;
! 129482: }
! 129483: }
! 129484:
! 129485: if( !pTerm ){
! 129486: /* All offsets for this column have been gathered. */
! 129487: rc = SQLITE_DONE;
! 129488: }else{
! 129489: assert( iCurrent<=iMinPos );
! 129490: if( 0==(0xFE&*pTerm->pList) ){
! 129491: pTerm->pList = 0;
! 129492: }else{
! 129493: fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
! 129494: }
! 129495: while( rc==SQLITE_OK && iCurrent<iMinPos ){
! 129496: rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
! 129497: }
! 129498: if( rc==SQLITE_OK ){
! 129499: char aBuffer[64];
! 129500: sqlite3_snprintf(sizeof(aBuffer), aBuffer,
! 129501: "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
! 129502: );
! 129503: rc = fts3StringAppend(&res, aBuffer, -1);
! 129504: }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
! 129505: rc = FTS_CORRUPT_VTAB;
! 129506: }
! 129507: }
! 129508: }
! 129509: if( rc==SQLITE_DONE ){
! 129510: rc = SQLITE_OK;
! 129511: }
! 129512:
! 129513: pMod->xClose(pC);
! 129514: if( rc!=SQLITE_OK ) goto offsets_out;
! 129515: }
! 129516:
! 129517: offsets_out:
! 129518: sqlite3_free(sCtx.aTerm);
! 129519: assert( rc!=SQLITE_DONE );
! 129520: sqlite3Fts3SegmentsClose(pTab);
! 129521: if( rc!=SQLITE_OK ){
! 129522: sqlite3_result_error_code(pCtx, rc);
! 129523: sqlite3_free(res.z);
! 129524: }else{
! 129525: sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
! 129526: }
! 129527: return;
! 129528: }
! 129529:
! 129530: /*
! 129531: ** Implementation of matchinfo() function.
! 129532: */
! 129533: SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
! 129534: sqlite3_context *pContext, /* Function call context */
! 129535: Fts3Cursor *pCsr, /* FTS3 table cursor */
! 129536: const char *zArg /* Second arg to matchinfo() function */
! 129537: ){
! 129538: Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
! 129539: int rc;
! 129540: int i;
! 129541: const char *zFormat;
! 129542:
! 129543: if( zArg ){
! 129544: for(i=0; zArg[i]; i++){
! 129545: char *zErr = 0;
! 129546: if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
! 129547: sqlite3_result_error(pContext, zErr, -1);
! 129548: sqlite3_free(zErr);
! 129549: return;
! 129550: }
! 129551: }
! 129552: zFormat = zArg;
! 129553: }else{
! 129554: zFormat = FTS3_MATCHINFO_DEFAULT;
! 129555: }
! 129556:
! 129557: if( !pCsr->pExpr ){
! 129558: sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
! 129559: return;
! 129560: }
! 129561:
! 129562: /* Retrieve matchinfo() data. */
! 129563: rc = fts3GetMatchinfo(pCsr, zFormat);
! 129564: sqlite3Fts3SegmentsClose(pTab);
! 129565:
! 129566: if( rc!=SQLITE_OK ){
! 129567: sqlite3_result_error_code(pContext, rc);
! 129568: }else{
! 129569: int n = pCsr->nMatchinfo * sizeof(u32);
! 129570: sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
! 129571: }
! 129572: }
! 129573:
! 129574: #endif
! 129575:
! 129576: /************** End of fts3_snippet.c ****************************************/
! 129577: /************** Begin file rtree.c *******************************************/
! 129578: /*
! 129579: ** 2001 September 15
! 129580: **
! 129581: ** The author disclaims copyright to this source code. In place of
! 129582: ** a legal notice, here is a blessing:
! 129583: **
! 129584: ** May you do good and not evil.
! 129585: ** May you find forgiveness for yourself and forgive others.
! 129586: ** May you share freely, never taking more than you give.
! 129587: **
! 129588: *************************************************************************
! 129589: ** This file contains code for implementations of the r-tree and r*-tree
! 129590: ** algorithms packaged as an SQLite virtual table module.
! 129591: */
! 129592:
! 129593: /*
! 129594: ** Database Format of R-Tree Tables
! 129595: ** --------------------------------
! 129596: **
! 129597: ** The data structure for a single virtual r-tree table is stored in three
! 129598: ** native SQLite tables declared as follows. In each case, the '%' character
! 129599: ** in the table name is replaced with the user-supplied name of the r-tree
! 129600: ** table.
! 129601: **
! 129602: ** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
! 129603: ** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
! 129604: ** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
! 129605: **
! 129606: ** The data for each node of the r-tree structure is stored in the %_node
! 129607: ** table. For each node that is not the root node of the r-tree, there is
! 129608: ** an entry in the %_parent table associating the node with its parent.
! 129609: ** And for each row of data in the table, there is an entry in the %_rowid
! 129610: ** table that maps from the entries rowid to the id of the node that it
! 129611: ** is stored on.
! 129612: **
! 129613: ** The root node of an r-tree always exists, even if the r-tree table is
! 129614: ** empty. The nodeno of the root node is always 1. All other nodes in the
! 129615: ** table must be the same size as the root node. The content of each node
! 129616: ** is formatted as follows:
! 129617: **
! 129618: ** 1. If the node is the root node (node 1), then the first 2 bytes
! 129619: ** of the node contain the tree depth as a big-endian integer.
! 129620: ** For non-root nodes, the first 2 bytes are left unused.
! 129621: **
! 129622: ** 2. The next 2 bytes contain the number of entries currently
! 129623: ** stored in the node.
! 129624: **
! 129625: ** 3. The remainder of the node contains the node entries. Each entry
! 129626: ** consists of a single 8-byte integer followed by an even number
! 129627: ** of 4-byte coordinates. For leaf nodes the integer is the rowid
! 129628: ** of a record. For internal nodes it is the node number of a
! 129629: ** child page.
! 129630: */
! 129631:
! 129632: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
! 129633:
! 129634: /*
! 129635: ** This file contains an implementation of a couple of different variants
! 129636: ** of the r-tree algorithm. See the README file for further details. The
! 129637: ** same data-structure is used for all, but the algorithms for insert and
! 129638: ** delete operations vary. The variants used are selected at compile time
! 129639: ** by defining the following symbols:
! 129640: */
! 129641:
! 129642: /* Either, both or none of the following may be set to activate
! 129643: ** r*tree variant algorithms.
! 129644: */
! 129645: #define VARIANT_RSTARTREE_CHOOSESUBTREE 0
! 129646: #define VARIANT_RSTARTREE_REINSERT 1
! 129647:
! 129648: /*
! 129649: ** Exactly one of the following must be set to 1.
! 129650: */
! 129651: #define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
! 129652: #define VARIANT_GUTTMAN_LINEAR_SPLIT 0
! 129653: #define VARIANT_RSTARTREE_SPLIT 1
! 129654:
! 129655: #define VARIANT_GUTTMAN_SPLIT \
! 129656: (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
! 129657:
! 129658: #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
! 129659: #define PickNext QuadraticPickNext
! 129660: #define PickSeeds QuadraticPickSeeds
! 129661: #define AssignCells splitNodeGuttman
! 129662: #endif
! 129663: #if VARIANT_GUTTMAN_LINEAR_SPLIT
! 129664: #define PickNext LinearPickNext
! 129665: #define PickSeeds LinearPickSeeds
! 129666: #define AssignCells splitNodeGuttman
! 129667: #endif
! 129668: #if VARIANT_RSTARTREE_SPLIT
! 129669: #define AssignCells splitNodeStartree
! 129670: #endif
! 129671:
! 129672: #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
! 129673: # define NDEBUG 1
! 129674: #endif
! 129675:
! 129676: #ifndef SQLITE_CORE
! 129677: SQLITE_EXTENSION_INIT1
! 129678: #else
! 129679: #endif
! 129680:
! 129681: /* #include <string.h> */
! 129682: /* #include <assert.h> */
! 129683:
! 129684: #ifndef SQLITE_AMALGAMATION
! 129685: #include "sqlite3rtree.h"
! 129686: typedef sqlite3_int64 i64;
! 129687: typedef unsigned char u8;
! 129688: typedef unsigned int u32;
! 129689: #endif
! 129690:
! 129691: /* The following macro is used to suppress compiler warnings.
! 129692: */
! 129693: #ifndef UNUSED_PARAMETER
! 129694: # define UNUSED_PARAMETER(x) (void)(x)
! 129695: #endif
! 129696:
! 129697: typedef struct Rtree Rtree;
! 129698: typedef struct RtreeCursor RtreeCursor;
! 129699: typedef struct RtreeNode RtreeNode;
! 129700: typedef struct RtreeCell RtreeCell;
! 129701: typedef struct RtreeConstraint RtreeConstraint;
! 129702: typedef struct RtreeMatchArg RtreeMatchArg;
! 129703: typedef struct RtreeGeomCallback RtreeGeomCallback;
! 129704: typedef union RtreeCoord RtreeCoord;
! 129705:
! 129706: /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
! 129707: #define RTREE_MAX_DIMENSIONS 5
! 129708:
! 129709: /* Size of hash table Rtree.aHash. This hash table is not expected to
! 129710: ** ever contain very many entries, so a fixed number of buckets is
! 129711: ** used.
! 129712: */
! 129713: #define HASHSIZE 128
! 129714:
! 129715: /*
! 129716: ** An rtree virtual-table object.
! 129717: */
! 129718: struct Rtree {
! 129719: sqlite3_vtab base;
! 129720: sqlite3 *db; /* Host database connection */
! 129721: int iNodeSize; /* Size in bytes of each node in the node table */
! 129722: int nDim; /* Number of dimensions */
! 129723: int nBytesPerCell; /* Bytes consumed per cell */
! 129724: int iDepth; /* Current depth of the r-tree structure */
! 129725: char *zDb; /* Name of database containing r-tree table */
! 129726: char *zName; /* Name of r-tree table */
! 129727: RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
! 129728: int nBusy; /* Current number of users of this structure */
! 129729:
! 129730: /* List of nodes removed during a CondenseTree operation. List is
! 129731: ** linked together via the pointer normally used for hash chains -
! 129732: ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
! 129733: ** headed by the node (leaf nodes have RtreeNode.iNode==0).
! 129734: */
! 129735: RtreeNode *pDeleted;
! 129736: int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
! 129737:
! 129738: /* Statements to read/write/delete a record from xxx_node */
! 129739: sqlite3_stmt *pReadNode;
! 129740: sqlite3_stmt *pWriteNode;
! 129741: sqlite3_stmt *pDeleteNode;
! 129742:
! 129743: /* Statements to read/write/delete a record from xxx_rowid */
! 129744: sqlite3_stmt *pReadRowid;
! 129745: sqlite3_stmt *pWriteRowid;
! 129746: sqlite3_stmt *pDeleteRowid;
! 129747:
! 129748: /* Statements to read/write/delete a record from xxx_parent */
! 129749: sqlite3_stmt *pReadParent;
! 129750: sqlite3_stmt *pWriteParent;
! 129751: sqlite3_stmt *pDeleteParent;
! 129752:
! 129753: int eCoordType;
! 129754: };
! 129755:
! 129756: /* Possible values for eCoordType: */
! 129757: #define RTREE_COORD_REAL32 0
! 129758: #define RTREE_COORD_INT32 1
! 129759:
! 129760: /*
! 129761: ** The minimum number of cells allowed for a node is a third of the
! 129762: ** maximum. In Gutman's notation:
! 129763: **
! 129764: ** m = M/3
! 129765: **
! 129766: ** If an R*-tree "Reinsert" operation is required, the same number of
! 129767: ** cells are removed from the overfull node and reinserted into the tree.
! 129768: */
! 129769: #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
! 129770: #define RTREE_REINSERT(p) RTREE_MINCELLS(p)
! 129771: #define RTREE_MAXCELLS 51
! 129772:
! 129773: /*
! 129774: ** The smallest possible node-size is (512-64)==448 bytes. And the largest
! 129775: ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
! 129776: ** Therefore all non-root nodes must contain at least 3 entries. Since
! 129777: ** 2^40 is greater than 2^64, an r-tree structure always has a depth of
! 129778: ** 40 or less.
! 129779: */
! 129780: #define RTREE_MAX_DEPTH 40
! 129781:
! 129782: /*
! 129783: ** An rtree cursor object.
! 129784: */
! 129785: struct RtreeCursor {
! 129786: sqlite3_vtab_cursor base;
! 129787: RtreeNode *pNode; /* Node cursor is currently pointing at */
! 129788: int iCell; /* Index of current cell in pNode */
! 129789: int iStrategy; /* Copy of idxNum search parameter */
! 129790: int nConstraint; /* Number of entries in aConstraint */
! 129791: RtreeConstraint *aConstraint; /* Search constraints. */
! 129792: };
! 129793:
! 129794: union RtreeCoord {
! 129795: float f;
! 129796: int i;
! 129797: };
! 129798:
! 129799: /*
! 129800: ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
! 129801: ** formatted as a double. This macro assumes that local variable pRtree points
! 129802: ** to the Rtree structure associated with the RtreeCoord.
! 129803: */
! 129804: #define DCOORD(coord) ( \
! 129805: (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
! 129806: ((double)coord.f) : \
! 129807: ((double)coord.i) \
! 129808: )
! 129809:
! 129810: /*
! 129811: ** A search constraint.
! 129812: */
! 129813: struct RtreeConstraint {
! 129814: int iCoord; /* Index of constrained coordinate */
! 129815: int op; /* Constraining operation */
! 129816: double rValue; /* Constraint value. */
! 129817: int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
! 129818: sqlite3_rtree_geometry *pGeom; /* Constraint callback argument for a MATCH */
! 129819: };
! 129820:
! 129821: /* Possible values for RtreeConstraint.op */
! 129822: #define RTREE_EQ 0x41
! 129823: #define RTREE_LE 0x42
! 129824: #define RTREE_LT 0x43
! 129825: #define RTREE_GE 0x44
! 129826: #define RTREE_GT 0x45
! 129827: #define RTREE_MATCH 0x46
! 129828:
! 129829: /*
! 129830: ** An rtree structure node.
! 129831: */
! 129832: struct RtreeNode {
! 129833: RtreeNode *pParent; /* Parent node */
! 129834: i64 iNode;
! 129835: int nRef;
! 129836: int isDirty;
! 129837: u8 *zData;
! 129838: RtreeNode *pNext; /* Next node in this hash chain */
! 129839: };
! 129840: #define NCELL(pNode) readInt16(&(pNode)->zData[2])
! 129841:
! 129842: /*
! 129843: ** Structure to store a deserialized rtree record.
! 129844: */
! 129845: struct RtreeCell {
! 129846: i64 iRowid;
! 129847: RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
! 129848: };
! 129849:
! 129850:
! 129851: /*
! 129852: ** Value for the first field of every RtreeMatchArg object. The MATCH
! 129853: ** operator tests that the first field of a blob operand matches this
! 129854: ** value to avoid operating on invalid blobs (which could cause a segfault).
! 129855: */
! 129856: #define RTREE_GEOMETRY_MAGIC 0x891245AB
! 129857:
! 129858: /*
! 129859: ** An instance of this structure must be supplied as a blob argument to
! 129860: ** the right-hand-side of an SQL MATCH operator used to constrain an
! 129861: ** r-tree query.
! 129862: */
! 129863: struct RtreeMatchArg {
! 129864: u32 magic; /* Always RTREE_GEOMETRY_MAGIC */
! 129865: int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
! 129866: void *pContext;
! 129867: int nParam;
! 129868: double aParam[1];
! 129869: };
! 129870:
! 129871: /*
! 129872: ** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
! 129873: ** a single instance of the following structure is allocated. It is used
! 129874: ** as the context for the user-function created by by s_r_g_c(). The object
! 129875: ** is eventually deleted by the destructor mechanism provided by
! 129876: ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
! 129877: ** the geometry callback function).
! 129878: */
! 129879: struct RtreeGeomCallback {
! 129880: int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
! 129881: void *pContext;
! 129882: };
! 129883:
! 129884: #ifndef MAX
! 129885: # define MAX(x,y) ((x) < (y) ? (y) : (x))
! 129886: #endif
! 129887: #ifndef MIN
! 129888: # define MIN(x,y) ((x) > (y) ? (y) : (x))
! 129889: #endif
! 129890:
! 129891: /*
! 129892: ** Functions to deserialize a 16 bit integer, 32 bit real number and
! 129893: ** 64 bit integer. The deserialized value is returned.
! 129894: */
! 129895: static int readInt16(u8 *p){
! 129896: return (p[0]<<8) + p[1];
! 129897: }
! 129898: static void readCoord(u8 *p, RtreeCoord *pCoord){
! 129899: u32 i = (
! 129900: (((u32)p[0]) << 24) +
! 129901: (((u32)p[1]) << 16) +
! 129902: (((u32)p[2]) << 8) +
! 129903: (((u32)p[3]) << 0)
! 129904: );
! 129905: *(u32 *)pCoord = i;
! 129906: }
! 129907: static i64 readInt64(u8 *p){
! 129908: return (
! 129909: (((i64)p[0]) << 56) +
! 129910: (((i64)p[1]) << 48) +
! 129911: (((i64)p[2]) << 40) +
! 129912: (((i64)p[3]) << 32) +
! 129913: (((i64)p[4]) << 24) +
! 129914: (((i64)p[5]) << 16) +
! 129915: (((i64)p[6]) << 8) +
! 129916: (((i64)p[7]) << 0)
! 129917: );
! 129918: }
! 129919:
! 129920: /*
! 129921: ** Functions to serialize a 16 bit integer, 32 bit real number and
! 129922: ** 64 bit integer. The value returned is the number of bytes written
! 129923: ** to the argument buffer (always 2, 4 and 8 respectively).
! 129924: */
! 129925: static int writeInt16(u8 *p, int i){
! 129926: p[0] = (i>> 8)&0xFF;
! 129927: p[1] = (i>> 0)&0xFF;
! 129928: return 2;
! 129929: }
! 129930: static int writeCoord(u8 *p, RtreeCoord *pCoord){
! 129931: u32 i;
! 129932: assert( sizeof(RtreeCoord)==4 );
! 129933: assert( sizeof(u32)==4 );
! 129934: i = *(u32 *)pCoord;
! 129935: p[0] = (i>>24)&0xFF;
! 129936: p[1] = (i>>16)&0xFF;
! 129937: p[2] = (i>> 8)&0xFF;
! 129938: p[3] = (i>> 0)&0xFF;
! 129939: return 4;
! 129940: }
! 129941: static int writeInt64(u8 *p, i64 i){
! 129942: p[0] = (i>>56)&0xFF;
! 129943: p[1] = (i>>48)&0xFF;
! 129944: p[2] = (i>>40)&0xFF;
! 129945: p[3] = (i>>32)&0xFF;
! 129946: p[4] = (i>>24)&0xFF;
! 129947: p[5] = (i>>16)&0xFF;
! 129948: p[6] = (i>> 8)&0xFF;
! 129949: p[7] = (i>> 0)&0xFF;
! 129950: return 8;
! 129951: }
! 129952:
! 129953: /*
! 129954: ** Increment the reference count of node p.
! 129955: */
! 129956: static void nodeReference(RtreeNode *p){
! 129957: if( p ){
! 129958: p->nRef++;
! 129959: }
! 129960: }
! 129961:
! 129962: /*
! 129963: ** Clear the content of node p (set all bytes to 0x00).
! 129964: */
! 129965: static void nodeZero(Rtree *pRtree, RtreeNode *p){
! 129966: memset(&p->zData[2], 0, pRtree->iNodeSize-2);
! 129967: p->isDirty = 1;
! 129968: }
! 129969:
! 129970: /*
! 129971: ** Given a node number iNode, return the corresponding key to use
! 129972: ** in the Rtree.aHash table.
! 129973: */
! 129974: static int nodeHash(i64 iNode){
! 129975: return (
! 129976: (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^
! 129977: (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
! 129978: ) % HASHSIZE;
! 129979: }
! 129980:
! 129981: /*
! 129982: ** Search the node hash table for node iNode. If found, return a pointer
! 129983: ** to it. Otherwise, return 0.
! 129984: */
! 129985: static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
! 129986: RtreeNode *p;
! 129987: for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
! 129988: return p;
! 129989: }
! 129990:
! 129991: /*
! 129992: ** Add node pNode to the node hash table.
! 129993: */
! 129994: static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
! 129995: int iHash;
! 129996: assert( pNode->pNext==0 );
! 129997: iHash = nodeHash(pNode->iNode);
! 129998: pNode->pNext = pRtree->aHash[iHash];
! 129999: pRtree->aHash[iHash] = pNode;
! 130000: }
! 130001:
! 130002: /*
! 130003: ** Remove node pNode from the node hash table.
! 130004: */
! 130005: static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
! 130006: RtreeNode **pp;
! 130007: if( pNode->iNode!=0 ){
! 130008: pp = &pRtree->aHash[nodeHash(pNode->iNode)];
! 130009: for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
! 130010: *pp = pNode->pNext;
! 130011: pNode->pNext = 0;
! 130012: }
! 130013: }
! 130014:
! 130015: /*
! 130016: ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
! 130017: ** indicating that node has not yet been assigned a node number. It is
! 130018: ** assigned a node number when nodeWrite() is called to write the
! 130019: ** node contents out to the database.
! 130020: */
! 130021: static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
! 130022: RtreeNode *pNode;
! 130023: pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
! 130024: if( pNode ){
! 130025: memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
! 130026: pNode->zData = (u8 *)&pNode[1];
! 130027: pNode->nRef = 1;
! 130028: pNode->pParent = pParent;
! 130029: pNode->isDirty = 1;
! 130030: nodeReference(pParent);
! 130031: }
! 130032: return pNode;
! 130033: }
! 130034:
! 130035: /*
! 130036: ** Obtain a reference to an r-tree node.
! 130037: */
! 130038: static int
! 130039: nodeAcquire(
! 130040: Rtree *pRtree, /* R-tree structure */
! 130041: i64 iNode, /* Node number to load */
! 130042: RtreeNode *pParent, /* Either the parent node or NULL */
! 130043: RtreeNode **ppNode /* OUT: Acquired node */
! 130044: ){
! 130045: int rc;
! 130046: int rc2 = SQLITE_OK;
! 130047: RtreeNode *pNode;
! 130048:
! 130049: /* Check if the requested node is already in the hash table. If so,
! 130050: ** increase its reference count and return it.
! 130051: */
! 130052: if( (pNode = nodeHashLookup(pRtree, iNode)) ){
! 130053: assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
! 130054: if( pParent && !pNode->pParent ){
! 130055: nodeReference(pParent);
! 130056: pNode->pParent = pParent;
! 130057: }
! 130058: pNode->nRef++;
! 130059: *ppNode = pNode;
! 130060: return SQLITE_OK;
! 130061: }
! 130062:
! 130063: sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
! 130064: rc = sqlite3_step(pRtree->pReadNode);
! 130065: if( rc==SQLITE_ROW ){
! 130066: const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
! 130067: if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
! 130068: pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
! 130069: if( !pNode ){
! 130070: rc2 = SQLITE_NOMEM;
! 130071: }else{
! 130072: pNode->pParent = pParent;
! 130073: pNode->zData = (u8 *)&pNode[1];
! 130074: pNode->nRef = 1;
! 130075: pNode->iNode = iNode;
! 130076: pNode->isDirty = 0;
! 130077: pNode->pNext = 0;
! 130078: memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
! 130079: nodeReference(pParent);
! 130080: }
! 130081: }
! 130082: }
! 130083: rc = sqlite3_reset(pRtree->pReadNode);
! 130084: if( rc==SQLITE_OK ) rc = rc2;
! 130085:
! 130086: /* If the root node was just loaded, set pRtree->iDepth to the height
! 130087: ** of the r-tree structure. A height of zero means all data is stored on
! 130088: ** the root node. A height of one means the children of the root node
! 130089: ** are the leaves, and so on. If the depth as specified on the root node
! 130090: ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
! 130091: */
! 130092: if( pNode && iNode==1 ){
! 130093: pRtree->iDepth = readInt16(pNode->zData);
! 130094: if( pRtree->iDepth>RTREE_MAX_DEPTH ){
! 130095: rc = SQLITE_CORRUPT_VTAB;
! 130096: }
! 130097: }
! 130098:
! 130099: /* If no error has occurred so far, check if the "number of entries"
! 130100: ** field on the node is too large. If so, set the return code to
! 130101: ** SQLITE_CORRUPT_VTAB.
! 130102: */
! 130103: if( pNode && rc==SQLITE_OK ){
! 130104: if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
! 130105: rc = SQLITE_CORRUPT_VTAB;
! 130106: }
! 130107: }
! 130108:
! 130109: if( rc==SQLITE_OK ){
! 130110: if( pNode!=0 ){
! 130111: nodeHashInsert(pRtree, pNode);
! 130112: }else{
! 130113: rc = SQLITE_CORRUPT_VTAB;
! 130114: }
! 130115: *ppNode = pNode;
! 130116: }else{
! 130117: sqlite3_free(pNode);
! 130118: *ppNode = 0;
! 130119: }
! 130120:
! 130121: return rc;
! 130122: }
! 130123:
! 130124: /*
! 130125: ** Overwrite cell iCell of node pNode with the contents of pCell.
! 130126: */
! 130127: static void nodeOverwriteCell(
! 130128: Rtree *pRtree,
! 130129: RtreeNode *pNode,
! 130130: RtreeCell *pCell,
! 130131: int iCell
! 130132: ){
! 130133: int ii;
! 130134: u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
! 130135: p += writeInt64(p, pCell->iRowid);
! 130136: for(ii=0; ii<(pRtree->nDim*2); ii++){
! 130137: p += writeCoord(p, &pCell->aCoord[ii]);
! 130138: }
! 130139: pNode->isDirty = 1;
! 130140: }
! 130141:
! 130142: /*
! 130143: ** Remove cell the cell with index iCell from node pNode.
! 130144: */
! 130145: static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
! 130146: u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
! 130147: u8 *pSrc = &pDst[pRtree->nBytesPerCell];
! 130148: int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
! 130149: memmove(pDst, pSrc, nByte);
! 130150: writeInt16(&pNode->zData[2], NCELL(pNode)-1);
! 130151: pNode->isDirty = 1;
! 130152: }
! 130153:
! 130154: /*
! 130155: ** Insert the contents of cell pCell into node pNode. If the insert
! 130156: ** is successful, return SQLITE_OK.
! 130157: **
! 130158: ** If there is not enough free space in pNode, return SQLITE_FULL.
! 130159: */
! 130160: static int
! 130161: nodeInsertCell(
! 130162: Rtree *pRtree,
! 130163: RtreeNode *pNode,
! 130164: RtreeCell *pCell
! 130165: ){
! 130166: int nCell; /* Current number of cells in pNode */
! 130167: int nMaxCell; /* Maximum number of cells for pNode */
! 130168:
! 130169: nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
! 130170: nCell = NCELL(pNode);
! 130171:
! 130172: assert( nCell<=nMaxCell );
! 130173: if( nCell<nMaxCell ){
! 130174: nodeOverwriteCell(pRtree, pNode, pCell, nCell);
! 130175: writeInt16(&pNode->zData[2], nCell+1);
! 130176: pNode->isDirty = 1;
! 130177: }
! 130178:
! 130179: return (nCell==nMaxCell);
! 130180: }
! 130181:
! 130182: /*
! 130183: ** If the node is dirty, write it out to the database.
! 130184: */
! 130185: static int
! 130186: nodeWrite(Rtree *pRtree, RtreeNode *pNode){
! 130187: int rc = SQLITE_OK;
! 130188: if( pNode->isDirty ){
! 130189: sqlite3_stmt *p = pRtree->pWriteNode;
! 130190: if( pNode->iNode ){
! 130191: sqlite3_bind_int64(p, 1, pNode->iNode);
! 130192: }else{
! 130193: sqlite3_bind_null(p, 1);
! 130194: }
! 130195: sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
! 130196: sqlite3_step(p);
! 130197: pNode->isDirty = 0;
! 130198: rc = sqlite3_reset(p);
! 130199: if( pNode->iNode==0 && rc==SQLITE_OK ){
! 130200: pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
! 130201: nodeHashInsert(pRtree, pNode);
! 130202: }
! 130203: }
! 130204: return rc;
! 130205: }
! 130206:
! 130207: /*
! 130208: ** Release a reference to a node. If the node is dirty and the reference
! 130209: ** count drops to zero, the node data is written to the database.
! 130210: */
! 130211: static int
! 130212: nodeRelease(Rtree *pRtree, RtreeNode *pNode){
! 130213: int rc = SQLITE_OK;
! 130214: if( pNode ){
! 130215: assert( pNode->nRef>0 );
! 130216: pNode->nRef--;
! 130217: if( pNode->nRef==0 ){
! 130218: if( pNode->iNode==1 ){
! 130219: pRtree->iDepth = -1;
! 130220: }
! 130221: if( pNode->pParent ){
! 130222: rc = nodeRelease(pRtree, pNode->pParent);
! 130223: }
! 130224: if( rc==SQLITE_OK ){
! 130225: rc = nodeWrite(pRtree, pNode);
! 130226: }
! 130227: nodeHashDelete(pRtree, pNode);
! 130228: sqlite3_free(pNode);
! 130229: }
! 130230: }
! 130231: return rc;
! 130232: }
! 130233:
! 130234: /*
! 130235: ** Return the 64-bit integer value associated with cell iCell of
! 130236: ** node pNode. If pNode is a leaf node, this is a rowid. If it is
! 130237: ** an internal node, then the 64-bit integer is a child page number.
! 130238: */
! 130239: static i64 nodeGetRowid(
! 130240: Rtree *pRtree,
! 130241: RtreeNode *pNode,
! 130242: int iCell
! 130243: ){
! 130244: assert( iCell<NCELL(pNode) );
! 130245: return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
! 130246: }
! 130247:
! 130248: /*
! 130249: ** Return coordinate iCoord from cell iCell in node pNode.
! 130250: */
! 130251: static void nodeGetCoord(
! 130252: Rtree *pRtree,
! 130253: RtreeNode *pNode,
! 130254: int iCell,
! 130255: int iCoord,
! 130256: RtreeCoord *pCoord /* Space to write result to */
! 130257: ){
! 130258: readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
! 130259: }
! 130260:
! 130261: /*
! 130262: ** Deserialize cell iCell of node pNode. Populate the structure pointed
! 130263: ** to by pCell with the results.
! 130264: */
! 130265: static void nodeGetCell(
! 130266: Rtree *pRtree,
! 130267: RtreeNode *pNode,
! 130268: int iCell,
! 130269: RtreeCell *pCell
! 130270: ){
! 130271: int ii;
! 130272: pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
! 130273: for(ii=0; ii<pRtree->nDim*2; ii++){
! 130274: nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
! 130275: }
! 130276: }
! 130277:
! 130278:
! 130279: /* Forward declaration for the function that does the work of
! 130280: ** the virtual table module xCreate() and xConnect() methods.
! 130281: */
! 130282: static int rtreeInit(
! 130283: sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
! 130284: );
! 130285:
! 130286: /*
! 130287: ** Rtree virtual table module xCreate method.
! 130288: */
! 130289: static int rtreeCreate(
! 130290: sqlite3 *db,
! 130291: void *pAux,
! 130292: int argc, const char *const*argv,
! 130293: sqlite3_vtab **ppVtab,
! 130294: char **pzErr
! 130295: ){
! 130296: return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
! 130297: }
! 130298:
! 130299: /*
! 130300: ** Rtree virtual table module xConnect method.
! 130301: */
! 130302: static int rtreeConnect(
! 130303: sqlite3 *db,
! 130304: void *pAux,
! 130305: int argc, const char *const*argv,
! 130306: sqlite3_vtab **ppVtab,
! 130307: char **pzErr
! 130308: ){
! 130309: return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
! 130310: }
! 130311:
! 130312: /*
! 130313: ** Increment the r-tree reference count.
! 130314: */
! 130315: static void rtreeReference(Rtree *pRtree){
! 130316: pRtree->nBusy++;
! 130317: }
! 130318:
! 130319: /*
! 130320: ** Decrement the r-tree reference count. When the reference count reaches
! 130321: ** zero the structure is deleted.
! 130322: */
! 130323: static void rtreeRelease(Rtree *pRtree){
! 130324: pRtree->nBusy--;
! 130325: if( pRtree->nBusy==0 ){
! 130326: sqlite3_finalize(pRtree->pReadNode);
! 130327: sqlite3_finalize(pRtree->pWriteNode);
! 130328: sqlite3_finalize(pRtree->pDeleteNode);
! 130329: sqlite3_finalize(pRtree->pReadRowid);
! 130330: sqlite3_finalize(pRtree->pWriteRowid);
! 130331: sqlite3_finalize(pRtree->pDeleteRowid);
! 130332: sqlite3_finalize(pRtree->pReadParent);
! 130333: sqlite3_finalize(pRtree->pWriteParent);
! 130334: sqlite3_finalize(pRtree->pDeleteParent);
! 130335: sqlite3_free(pRtree);
! 130336: }
! 130337: }
! 130338:
! 130339: /*
! 130340: ** Rtree virtual table module xDisconnect method.
! 130341: */
! 130342: static int rtreeDisconnect(sqlite3_vtab *pVtab){
! 130343: rtreeRelease((Rtree *)pVtab);
! 130344: return SQLITE_OK;
! 130345: }
! 130346:
! 130347: /*
! 130348: ** Rtree virtual table module xDestroy method.
! 130349: */
! 130350: static int rtreeDestroy(sqlite3_vtab *pVtab){
! 130351: Rtree *pRtree = (Rtree *)pVtab;
! 130352: int rc;
! 130353: char *zCreate = sqlite3_mprintf(
! 130354: "DROP TABLE '%q'.'%q_node';"
! 130355: "DROP TABLE '%q'.'%q_rowid';"
! 130356: "DROP TABLE '%q'.'%q_parent';",
! 130357: pRtree->zDb, pRtree->zName,
! 130358: pRtree->zDb, pRtree->zName,
! 130359: pRtree->zDb, pRtree->zName
! 130360: );
! 130361: if( !zCreate ){
! 130362: rc = SQLITE_NOMEM;
! 130363: }else{
! 130364: rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
! 130365: sqlite3_free(zCreate);
! 130366: }
! 130367: if( rc==SQLITE_OK ){
! 130368: rtreeRelease(pRtree);
! 130369: }
! 130370:
! 130371: return rc;
! 130372: }
! 130373:
! 130374: /*
! 130375: ** Rtree virtual table module xOpen method.
! 130376: */
! 130377: static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
! 130378: int rc = SQLITE_NOMEM;
! 130379: RtreeCursor *pCsr;
! 130380:
! 130381: pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
! 130382: if( pCsr ){
! 130383: memset(pCsr, 0, sizeof(RtreeCursor));
! 130384: pCsr->base.pVtab = pVTab;
! 130385: rc = SQLITE_OK;
! 130386: }
! 130387: *ppCursor = (sqlite3_vtab_cursor *)pCsr;
! 130388:
! 130389: return rc;
! 130390: }
! 130391:
! 130392:
! 130393: /*
! 130394: ** Free the RtreeCursor.aConstraint[] array and its contents.
! 130395: */
! 130396: static void freeCursorConstraints(RtreeCursor *pCsr){
! 130397: if( pCsr->aConstraint ){
! 130398: int i; /* Used to iterate through constraint array */
! 130399: for(i=0; i<pCsr->nConstraint; i++){
! 130400: sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
! 130401: if( pGeom ){
! 130402: if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
! 130403: sqlite3_free(pGeom);
! 130404: }
! 130405: }
! 130406: sqlite3_free(pCsr->aConstraint);
! 130407: pCsr->aConstraint = 0;
! 130408: }
! 130409: }
! 130410:
! 130411: /*
! 130412: ** Rtree virtual table module xClose method.
! 130413: */
! 130414: static int rtreeClose(sqlite3_vtab_cursor *cur){
! 130415: Rtree *pRtree = (Rtree *)(cur->pVtab);
! 130416: int rc;
! 130417: RtreeCursor *pCsr = (RtreeCursor *)cur;
! 130418: freeCursorConstraints(pCsr);
! 130419: rc = nodeRelease(pRtree, pCsr->pNode);
! 130420: sqlite3_free(pCsr);
! 130421: return rc;
! 130422: }
! 130423:
! 130424: /*
! 130425: ** Rtree virtual table module xEof method.
! 130426: **
! 130427: ** Return non-zero if the cursor does not currently point to a valid
! 130428: ** record (i.e if the scan has finished), or zero otherwise.
! 130429: */
! 130430: static int rtreeEof(sqlite3_vtab_cursor *cur){
! 130431: RtreeCursor *pCsr = (RtreeCursor *)cur;
! 130432: return (pCsr->pNode==0);
! 130433: }
! 130434:
! 130435: /*
! 130436: ** The r-tree constraint passed as the second argument to this function is
! 130437: ** guaranteed to be a MATCH constraint.
! 130438: */
! 130439: static int testRtreeGeom(
! 130440: Rtree *pRtree, /* R-Tree object */
! 130441: RtreeConstraint *pConstraint, /* MATCH constraint to test */
! 130442: RtreeCell *pCell, /* Cell to test */
! 130443: int *pbRes /* OUT: Test result */
! 130444: ){
! 130445: int i;
! 130446: double aCoord[RTREE_MAX_DIMENSIONS*2];
! 130447: int nCoord = pRtree->nDim*2;
! 130448:
! 130449: assert( pConstraint->op==RTREE_MATCH );
! 130450: assert( pConstraint->pGeom );
! 130451:
! 130452: for(i=0; i<nCoord; i++){
! 130453: aCoord[i] = DCOORD(pCell->aCoord[i]);
! 130454: }
! 130455: return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
! 130456: }
! 130457:
! 130458: /*
! 130459: ** Cursor pCursor currently points to a cell in a non-leaf page.
! 130460: ** Set *pbEof to true if the sub-tree headed by the cell is filtered
! 130461: ** (excluded) by the constraints in the pCursor->aConstraint[]
! 130462: ** array, or false otherwise.
! 130463: **
! 130464: ** Return SQLITE_OK if successful or an SQLite error code if an error
! 130465: ** occurs within a geometry callback.
! 130466: */
! 130467: static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
! 130468: RtreeCell cell;
! 130469: int ii;
! 130470: int bRes = 0;
! 130471: int rc = SQLITE_OK;
! 130472:
! 130473: nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
! 130474: for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
! 130475: RtreeConstraint *p = &pCursor->aConstraint[ii];
! 130476: double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
! 130477: double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
! 130478:
! 130479: assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
! 130480: || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
! 130481: );
! 130482:
! 130483: switch( p->op ){
! 130484: case RTREE_LE: case RTREE_LT:
! 130485: bRes = p->rValue<cell_min;
! 130486: break;
! 130487:
! 130488: case RTREE_GE: case RTREE_GT:
! 130489: bRes = p->rValue>cell_max;
! 130490: break;
! 130491:
! 130492: case RTREE_EQ:
! 130493: bRes = (p->rValue>cell_max || p->rValue<cell_min);
! 130494: break;
! 130495:
! 130496: default: {
! 130497: assert( p->op==RTREE_MATCH );
! 130498: rc = testRtreeGeom(pRtree, p, &cell, &bRes);
! 130499: bRes = !bRes;
! 130500: break;
! 130501: }
! 130502: }
! 130503: }
! 130504:
! 130505: *pbEof = bRes;
! 130506: return rc;
! 130507: }
! 130508:
! 130509: /*
! 130510: ** Test if the cell that cursor pCursor currently points to
! 130511: ** would be filtered (excluded) by the constraints in the
! 130512: ** pCursor->aConstraint[] array. If so, set *pbEof to true before
! 130513: ** returning. If the cell is not filtered (excluded) by the constraints,
! 130514: ** set pbEof to zero.
! 130515: **
! 130516: ** Return SQLITE_OK if successful or an SQLite error code if an error
! 130517: ** occurs within a geometry callback.
! 130518: **
! 130519: ** This function assumes that the cell is part of a leaf node.
! 130520: */
! 130521: static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
! 130522: RtreeCell cell;
! 130523: int ii;
! 130524: *pbEof = 0;
! 130525:
! 130526: nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
! 130527: for(ii=0; ii<pCursor->nConstraint; ii++){
! 130528: RtreeConstraint *p = &pCursor->aConstraint[ii];
! 130529: double coord = DCOORD(cell.aCoord[p->iCoord]);
! 130530: int res;
! 130531: assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
! 130532: || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
! 130533: );
! 130534: switch( p->op ){
! 130535: case RTREE_LE: res = (coord<=p->rValue); break;
! 130536: case RTREE_LT: res = (coord<p->rValue); break;
! 130537: case RTREE_GE: res = (coord>=p->rValue); break;
! 130538: case RTREE_GT: res = (coord>p->rValue); break;
! 130539: case RTREE_EQ: res = (coord==p->rValue); break;
! 130540: default: {
! 130541: int rc;
! 130542: assert( p->op==RTREE_MATCH );
! 130543: rc = testRtreeGeom(pRtree, p, &cell, &res);
! 130544: if( rc!=SQLITE_OK ){
! 130545: return rc;
! 130546: }
! 130547: break;
! 130548: }
! 130549: }
! 130550:
! 130551: if( !res ){
! 130552: *pbEof = 1;
! 130553: return SQLITE_OK;
! 130554: }
! 130555: }
! 130556:
! 130557: return SQLITE_OK;
! 130558: }
! 130559:
! 130560: /*
! 130561: ** Cursor pCursor currently points at a node that heads a sub-tree of
! 130562: ** height iHeight (if iHeight==0, then the node is a leaf). Descend
! 130563: ** to point to the left-most cell of the sub-tree that matches the
! 130564: ** configured constraints.
! 130565: */
! 130566: static int descendToCell(
! 130567: Rtree *pRtree,
! 130568: RtreeCursor *pCursor,
! 130569: int iHeight,
! 130570: int *pEof /* OUT: Set to true if cannot descend */
! 130571: ){
! 130572: int isEof;
! 130573: int rc;
! 130574: int ii;
! 130575: RtreeNode *pChild;
! 130576: sqlite3_int64 iRowid;
! 130577:
! 130578: RtreeNode *pSavedNode = pCursor->pNode;
! 130579: int iSavedCell = pCursor->iCell;
! 130580:
! 130581: assert( iHeight>=0 );
! 130582:
! 130583: if( iHeight==0 ){
! 130584: rc = testRtreeEntry(pRtree, pCursor, &isEof);
! 130585: }else{
! 130586: rc = testRtreeCell(pRtree, pCursor, &isEof);
! 130587: }
! 130588: if( rc!=SQLITE_OK || isEof || iHeight==0 ){
! 130589: goto descend_to_cell_out;
! 130590: }
! 130591:
! 130592: iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
! 130593: rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
! 130594: if( rc!=SQLITE_OK ){
! 130595: goto descend_to_cell_out;
! 130596: }
! 130597:
! 130598: nodeRelease(pRtree, pCursor->pNode);
! 130599: pCursor->pNode = pChild;
! 130600: isEof = 1;
! 130601: for(ii=0; isEof && ii<NCELL(pChild); ii++){
! 130602: pCursor->iCell = ii;
! 130603: rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
! 130604: if( rc!=SQLITE_OK ){
! 130605: goto descend_to_cell_out;
! 130606: }
! 130607: }
! 130608:
! 130609: if( isEof ){
! 130610: assert( pCursor->pNode==pChild );
! 130611: nodeReference(pSavedNode);
! 130612: nodeRelease(pRtree, pChild);
! 130613: pCursor->pNode = pSavedNode;
! 130614: pCursor->iCell = iSavedCell;
! 130615: }
! 130616:
! 130617: descend_to_cell_out:
! 130618: *pEof = isEof;
! 130619: return rc;
! 130620: }
! 130621:
! 130622: /*
! 130623: ** One of the cells in node pNode is guaranteed to have a 64-bit
! 130624: ** integer value equal to iRowid. Return the index of this cell.
! 130625: */
! 130626: static int nodeRowidIndex(
! 130627: Rtree *pRtree,
! 130628: RtreeNode *pNode,
! 130629: i64 iRowid,
! 130630: int *piIndex
! 130631: ){
! 130632: int ii;
! 130633: int nCell = NCELL(pNode);
! 130634: for(ii=0; ii<nCell; ii++){
! 130635: if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
! 130636: *piIndex = ii;
! 130637: return SQLITE_OK;
! 130638: }
! 130639: }
! 130640: return SQLITE_CORRUPT_VTAB;
! 130641: }
! 130642:
! 130643: /*
! 130644: ** Return the index of the cell containing a pointer to node pNode
! 130645: ** in its parent. If pNode is the root node, return -1.
! 130646: */
! 130647: static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
! 130648: RtreeNode *pParent = pNode->pParent;
! 130649: if( pParent ){
! 130650: return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
! 130651: }
! 130652: *piIndex = -1;
! 130653: return SQLITE_OK;
! 130654: }
! 130655:
! 130656: /*
! 130657: ** Rtree virtual table module xNext method.
! 130658: */
! 130659: static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
! 130660: Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
! 130661: RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
! 130662: int rc = SQLITE_OK;
! 130663:
! 130664: /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is
! 130665: ** already at EOF. It is against the rules to call the xNext() method of
! 130666: ** a cursor that has already reached EOF.
! 130667: */
! 130668: assert( pCsr->pNode );
! 130669:
! 130670: if( pCsr->iStrategy==1 ){
! 130671: /* This "scan" is a direct lookup by rowid. There is no next entry. */
! 130672: nodeRelease(pRtree, pCsr->pNode);
! 130673: pCsr->pNode = 0;
! 130674: }else{
! 130675: /* Move to the next entry that matches the configured constraints. */
! 130676: int iHeight = 0;
! 130677: while( pCsr->pNode ){
! 130678: RtreeNode *pNode = pCsr->pNode;
! 130679: int nCell = NCELL(pNode);
! 130680: for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
! 130681: int isEof;
! 130682: rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
! 130683: if( rc!=SQLITE_OK || !isEof ){
! 130684: return rc;
! 130685: }
! 130686: }
! 130687: pCsr->pNode = pNode->pParent;
! 130688: rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell);
! 130689: if( rc!=SQLITE_OK ){
! 130690: return rc;
! 130691: }
! 130692: nodeReference(pCsr->pNode);
! 130693: nodeRelease(pRtree, pNode);
! 130694: iHeight++;
! 130695: }
! 130696: }
! 130697:
! 130698: return rc;
! 130699: }
! 130700:
! 130701: /*
! 130702: ** Rtree virtual table module xRowid method.
! 130703: */
! 130704: static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
! 130705: Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
! 130706: RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
! 130707:
! 130708: assert(pCsr->pNode);
! 130709: *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
! 130710:
! 130711: return SQLITE_OK;
! 130712: }
! 130713:
! 130714: /*
! 130715: ** Rtree virtual table module xColumn method.
! 130716: */
! 130717: static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
! 130718: Rtree *pRtree = (Rtree *)cur->pVtab;
! 130719: RtreeCursor *pCsr = (RtreeCursor *)cur;
! 130720:
! 130721: if( i==0 ){
! 130722: i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
! 130723: sqlite3_result_int64(ctx, iRowid);
! 130724: }else{
! 130725: RtreeCoord c;
! 130726: nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
! 130727: if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
! 130728: sqlite3_result_double(ctx, c.f);
! 130729: }else{
! 130730: assert( pRtree->eCoordType==RTREE_COORD_INT32 );
! 130731: sqlite3_result_int(ctx, c.i);
! 130732: }
! 130733: }
! 130734:
! 130735: return SQLITE_OK;
! 130736: }
! 130737:
! 130738: /*
! 130739: ** Use nodeAcquire() to obtain the leaf node containing the record with
! 130740: ** rowid iRowid. If successful, set *ppLeaf to point to the node and
! 130741: ** return SQLITE_OK. If there is no such record in the table, set
! 130742: ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
! 130743: ** to zero and return an SQLite error code.
! 130744: */
! 130745: static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
! 130746: int rc;
! 130747: *ppLeaf = 0;
! 130748: sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
! 130749: if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
! 130750: i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
! 130751: rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
! 130752: sqlite3_reset(pRtree->pReadRowid);
! 130753: }else{
! 130754: rc = sqlite3_reset(pRtree->pReadRowid);
! 130755: }
! 130756: return rc;
! 130757: }
! 130758:
! 130759: /*
! 130760: ** This function is called to configure the RtreeConstraint object passed
! 130761: ** as the second argument for a MATCH constraint. The value passed as the
! 130762: ** first argument to this function is the right-hand operand to the MATCH
! 130763: ** operator.
! 130764: */
! 130765: static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
! 130766: RtreeMatchArg *p;
! 130767: sqlite3_rtree_geometry *pGeom;
! 130768: int nBlob;
! 130769:
! 130770: /* Check that value is actually a blob. */
! 130771: if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
! 130772:
! 130773: /* Check that the blob is roughly the right size. */
! 130774: nBlob = sqlite3_value_bytes(pValue);
! 130775: if( nBlob<(int)sizeof(RtreeMatchArg)
! 130776: || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0
! 130777: ){
! 130778: return SQLITE_ERROR;
! 130779: }
! 130780:
! 130781: pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
! 130782: sizeof(sqlite3_rtree_geometry) + nBlob
! 130783: );
! 130784: if( !pGeom ) return SQLITE_NOMEM;
! 130785: memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
! 130786: p = (RtreeMatchArg *)&pGeom[1];
! 130787:
! 130788: memcpy(p, sqlite3_value_blob(pValue), nBlob);
! 130789: if( p->magic!=RTREE_GEOMETRY_MAGIC
! 130790: || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double))
! 130791: ){
! 130792: sqlite3_free(pGeom);
! 130793: return SQLITE_ERROR;
! 130794: }
! 130795:
! 130796: pGeom->pContext = p->pContext;
! 130797: pGeom->nParam = p->nParam;
! 130798: pGeom->aParam = p->aParam;
! 130799:
! 130800: pCons->xGeom = p->xGeom;
! 130801: pCons->pGeom = pGeom;
! 130802: return SQLITE_OK;
! 130803: }
! 130804:
! 130805: /*
! 130806: ** Rtree virtual table module xFilter method.
! 130807: */
! 130808: static int rtreeFilter(
! 130809: sqlite3_vtab_cursor *pVtabCursor,
! 130810: int idxNum, const char *idxStr,
! 130811: int argc, sqlite3_value **argv
! 130812: ){
! 130813: Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
! 130814: RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
! 130815:
! 130816: RtreeNode *pRoot = 0;
! 130817: int ii;
! 130818: int rc = SQLITE_OK;
! 130819:
! 130820: rtreeReference(pRtree);
! 130821:
! 130822: freeCursorConstraints(pCsr);
! 130823: pCsr->iStrategy = idxNum;
! 130824:
! 130825: if( idxNum==1 ){
! 130826: /* Special case - lookup by rowid. */
! 130827: RtreeNode *pLeaf; /* Leaf on which the required cell resides */
! 130828: i64 iRowid = sqlite3_value_int64(argv[0]);
! 130829: rc = findLeafNode(pRtree, iRowid, &pLeaf);
! 130830: pCsr->pNode = pLeaf;
! 130831: if( pLeaf ){
! 130832: assert( rc==SQLITE_OK );
! 130833: rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell);
! 130834: }
! 130835: }else{
! 130836: /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
! 130837: ** with the configured constraints.
! 130838: */
! 130839: if( argc>0 ){
! 130840: pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
! 130841: pCsr->nConstraint = argc;
! 130842: if( !pCsr->aConstraint ){
! 130843: rc = SQLITE_NOMEM;
! 130844: }else{
! 130845: memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
! 130846: assert( (idxStr==0 && argc==0)
! 130847: || (idxStr && (int)strlen(idxStr)==argc*2) );
! 130848: for(ii=0; ii<argc; ii++){
! 130849: RtreeConstraint *p = &pCsr->aConstraint[ii];
! 130850: p->op = idxStr[ii*2];
! 130851: p->iCoord = idxStr[ii*2+1]-'a';
! 130852: if( p->op==RTREE_MATCH ){
! 130853: /* A MATCH operator. The right-hand-side must be a blob that
! 130854: ** can be cast into an RtreeMatchArg object. One created using
! 130855: ** an sqlite3_rtree_geometry_callback() SQL user function.
! 130856: */
! 130857: rc = deserializeGeometry(argv[ii], p);
! 130858: if( rc!=SQLITE_OK ){
! 130859: break;
! 130860: }
! 130861: }else{
! 130862: p->rValue = sqlite3_value_double(argv[ii]);
! 130863: }
! 130864: }
! 130865: }
! 130866: }
! 130867:
! 130868: if( rc==SQLITE_OK ){
! 130869: pCsr->pNode = 0;
! 130870: rc = nodeAcquire(pRtree, 1, 0, &pRoot);
! 130871: }
! 130872: if( rc==SQLITE_OK ){
! 130873: int isEof = 1;
! 130874: int nCell = NCELL(pRoot);
! 130875: pCsr->pNode = pRoot;
! 130876: for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
! 130877: assert( pCsr->pNode==pRoot );
! 130878: rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
! 130879: if( !isEof ){
! 130880: break;
! 130881: }
! 130882: }
! 130883: if( rc==SQLITE_OK && isEof ){
! 130884: assert( pCsr->pNode==pRoot );
! 130885: nodeRelease(pRtree, pRoot);
! 130886: pCsr->pNode = 0;
! 130887: }
! 130888: assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
! 130889: }
! 130890: }
! 130891:
! 130892: rtreeRelease(pRtree);
! 130893: return rc;
! 130894: }
! 130895:
! 130896: /*
! 130897: ** Rtree virtual table module xBestIndex method. There are three
! 130898: ** table scan strategies to choose from (in order from most to
! 130899: ** least desirable):
! 130900: **
! 130901: ** idxNum idxStr Strategy
! 130902: ** ------------------------------------------------
! 130903: ** 1 Unused Direct lookup by rowid.
! 130904: ** 2 See below R-tree query or full-table scan.
! 130905: ** ------------------------------------------------
! 130906: **
! 130907: ** If strategy 1 is used, then idxStr is not meaningful. If strategy
! 130908: ** 2 is used, idxStr is formatted to contain 2 bytes for each
! 130909: ** constraint used. The first two bytes of idxStr correspond to
! 130910: ** the constraint in sqlite3_index_info.aConstraintUsage[] with
! 130911: ** (argvIndex==1) etc.
! 130912: **
! 130913: ** The first of each pair of bytes in idxStr identifies the constraint
! 130914: ** operator as follows:
! 130915: **
! 130916: ** Operator Byte Value
! 130917: ** ----------------------
! 130918: ** = 0x41 ('A')
! 130919: ** <= 0x42 ('B')
! 130920: ** < 0x43 ('C')
! 130921: ** >= 0x44 ('D')
! 130922: ** > 0x45 ('E')
! 130923: ** MATCH 0x46 ('F')
! 130924: ** ----------------------
! 130925: **
! 130926: ** The second of each pair of bytes identifies the coordinate column
! 130927: ** to which the constraint applies. The leftmost coordinate column
! 130928: ** is 'a', the second from the left 'b' etc.
! 130929: */
! 130930: static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
! 130931: int rc = SQLITE_OK;
! 130932: int ii;
! 130933:
! 130934: int iIdx = 0;
! 130935: char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
! 130936: memset(zIdxStr, 0, sizeof(zIdxStr));
! 130937: UNUSED_PARAMETER(tab);
! 130938:
! 130939: assert( pIdxInfo->idxStr==0 );
! 130940: for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
! 130941: struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
! 130942:
! 130943: if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
! 130944: /* We have an equality constraint on the rowid. Use strategy 1. */
! 130945: int jj;
! 130946: for(jj=0; jj<ii; jj++){
! 130947: pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
! 130948: pIdxInfo->aConstraintUsage[jj].omit = 0;
! 130949: }
! 130950: pIdxInfo->idxNum = 1;
! 130951: pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
! 130952: pIdxInfo->aConstraintUsage[jj].omit = 1;
! 130953:
! 130954: /* This strategy involves a two rowid lookups on an B-Tree structures
! 130955: ** and then a linear search of an R-Tree node. This should be
! 130956: ** considered almost as quick as a direct rowid lookup (for which
! 130957: ** sqlite uses an internal cost of 0.0).
! 130958: */
! 130959: pIdxInfo->estimatedCost = 10.0;
! 130960: return SQLITE_OK;
! 130961: }
! 130962:
! 130963: if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
! 130964: u8 op;
! 130965: switch( p->op ){
! 130966: case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
! 130967: case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
! 130968: case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
! 130969: case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
! 130970: case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
! 130971: default:
! 130972: assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
! 130973: op = RTREE_MATCH;
! 130974: break;
! 130975: }
! 130976: zIdxStr[iIdx++] = op;
! 130977: zIdxStr[iIdx++] = p->iColumn - 1 + 'a';
! 130978: pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
! 130979: pIdxInfo->aConstraintUsage[ii].omit = 1;
! 130980: }
! 130981: }
! 130982:
! 130983: pIdxInfo->idxNum = 2;
! 130984: pIdxInfo->needToFreeIdxStr = 1;
! 130985: if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
! 130986: return SQLITE_NOMEM;
! 130987: }
! 130988: assert( iIdx>=0 );
! 130989: pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1));
! 130990: return rc;
! 130991: }
! 130992:
! 130993: /*
! 130994: ** Return the N-dimensional volumn of the cell stored in *p.
! 130995: */
! 130996: static float cellArea(Rtree *pRtree, RtreeCell *p){
! 130997: float area = 1.0;
! 130998: int ii;
! 130999: for(ii=0; ii<(pRtree->nDim*2); ii+=2){
! 131000: area = (float)(area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
! 131001: }
! 131002: return area;
! 131003: }
! 131004:
! 131005: /*
! 131006: ** Return the margin length of cell p. The margin length is the sum
! 131007: ** of the objects size in each dimension.
! 131008: */
! 131009: static float cellMargin(Rtree *pRtree, RtreeCell *p){
! 131010: float margin = 0.0;
! 131011: int ii;
! 131012: for(ii=0; ii<(pRtree->nDim*2); ii+=2){
! 131013: margin += (float)(DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
! 131014: }
! 131015: return margin;
! 131016: }
! 131017:
! 131018: /*
! 131019: ** Store the union of cells p1 and p2 in p1.
! 131020: */
! 131021: static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
! 131022: int ii;
! 131023: if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
! 131024: for(ii=0; ii<(pRtree->nDim*2); ii+=2){
! 131025: p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
! 131026: p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
! 131027: }
! 131028: }else{
! 131029: for(ii=0; ii<(pRtree->nDim*2); ii+=2){
! 131030: p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
! 131031: p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
! 131032: }
! 131033: }
! 131034: }
! 131035:
! 131036: /*
! 131037: ** Return true if the area covered by p2 is a subset of the area covered
! 131038: ** by p1. False otherwise.
! 131039: */
! 131040: static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
! 131041: int ii;
! 131042: int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
! 131043: for(ii=0; ii<(pRtree->nDim*2); ii+=2){
! 131044: RtreeCoord *a1 = &p1->aCoord[ii];
! 131045: RtreeCoord *a2 = &p2->aCoord[ii];
! 131046: if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
! 131047: || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
! 131048: ){
! 131049: return 0;
! 131050: }
! 131051: }
! 131052: return 1;
! 131053: }
! 131054:
! 131055: /*
! 131056: ** Return the amount cell p would grow by if it were unioned with pCell.
! 131057: */
! 131058: static float cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
! 131059: float area;
! 131060: RtreeCell cell;
! 131061: memcpy(&cell, p, sizeof(RtreeCell));
! 131062: area = cellArea(pRtree, &cell);
! 131063: cellUnion(pRtree, &cell, pCell);
! 131064: return (cellArea(pRtree, &cell)-area);
! 131065: }
! 131066:
! 131067: #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
! 131068: static float cellOverlap(
! 131069: Rtree *pRtree,
! 131070: RtreeCell *p,
! 131071: RtreeCell *aCell,
! 131072: int nCell,
! 131073: int iExclude
! 131074: ){
! 131075: int ii;
! 131076: float overlap = 0.0;
! 131077: for(ii=0; ii<nCell; ii++){
! 131078: #if VARIANT_RSTARTREE_CHOOSESUBTREE
! 131079: if( ii!=iExclude )
! 131080: #else
! 131081: assert( iExclude==-1 );
! 131082: UNUSED_PARAMETER(iExclude);
! 131083: #endif
! 131084: {
! 131085: int jj;
! 131086: float o = 1.0;
! 131087: for(jj=0; jj<(pRtree->nDim*2); jj+=2){
! 131088: double x1;
! 131089: double x2;
! 131090:
! 131091: x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
! 131092: x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
! 131093:
! 131094: if( x2<x1 ){
! 131095: o = 0.0;
! 131096: break;
! 131097: }else{
! 131098: o = o * (float)(x2-x1);
! 131099: }
! 131100: }
! 131101: overlap += o;
! 131102: }
! 131103: }
! 131104: return overlap;
! 131105: }
! 131106: #endif
! 131107:
! 131108: #if VARIANT_RSTARTREE_CHOOSESUBTREE
! 131109: static float cellOverlapEnlargement(
! 131110: Rtree *pRtree,
! 131111: RtreeCell *p,
! 131112: RtreeCell *pInsert,
! 131113: RtreeCell *aCell,
! 131114: int nCell,
! 131115: int iExclude
! 131116: ){
! 131117: double before;
! 131118: double after;
! 131119: before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
! 131120: cellUnion(pRtree, p, pInsert);
! 131121: after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
! 131122: return (float)(after-before);
! 131123: }
! 131124: #endif
! 131125:
! 131126:
! 131127: /*
! 131128: ** This function implements the ChooseLeaf algorithm from Gutman[84].
! 131129: ** ChooseSubTree in r*tree terminology.
! 131130: */
! 131131: static int ChooseLeaf(
! 131132: Rtree *pRtree, /* Rtree table */
! 131133: RtreeCell *pCell, /* Cell to insert into rtree */
! 131134: int iHeight, /* Height of sub-tree rooted at pCell */
! 131135: RtreeNode **ppLeaf /* OUT: Selected leaf page */
! 131136: ){
! 131137: int rc;
! 131138: int ii;
! 131139: RtreeNode *pNode;
! 131140: rc = nodeAcquire(pRtree, 1, 0, &pNode);
! 131141:
! 131142: for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
! 131143: int iCell;
! 131144: sqlite3_int64 iBest = 0;
! 131145:
! 131146: float fMinGrowth = 0.0;
! 131147: float fMinArea = 0.0;
! 131148: #if VARIANT_RSTARTREE_CHOOSESUBTREE
! 131149: float fMinOverlap = 0.0;
! 131150: float overlap;
! 131151: #endif
! 131152:
! 131153: int nCell = NCELL(pNode);
! 131154: RtreeCell cell;
! 131155: RtreeNode *pChild;
! 131156:
! 131157: RtreeCell *aCell = 0;
! 131158:
! 131159: #if VARIANT_RSTARTREE_CHOOSESUBTREE
! 131160: if( ii==(pRtree->iDepth-1) ){
! 131161: int jj;
! 131162: aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
! 131163: if( !aCell ){
! 131164: rc = SQLITE_NOMEM;
! 131165: nodeRelease(pRtree, pNode);
! 131166: pNode = 0;
! 131167: continue;
! 131168: }
! 131169: for(jj=0; jj<nCell; jj++){
! 131170: nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
! 131171: }
! 131172: }
! 131173: #endif
! 131174:
! 131175: /* Select the child node which will be enlarged the least if pCell
! 131176: ** is inserted into it. Resolve ties by choosing the entry with
! 131177: ** the smallest area.
! 131178: */
! 131179: for(iCell=0; iCell<nCell; iCell++){
! 131180: int bBest = 0;
! 131181: float growth;
! 131182: float area;
! 131183: nodeGetCell(pRtree, pNode, iCell, &cell);
! 131184: growth = cellGrowth(pRtree, &cell, pCell);
! 131185: area = cellArea(pRtree, &cell);
! 131186:
! 131187: #if VARIANT_RSTARTREE_CHOOSESUBTREE
! 131188: if( ii==(pRtree->iDepth-1) ){
! 131189: overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
! 131190: }else{
! 131191: overlap = 0.0;
! 131192: }
! 131193: if( (iCell==0)
! 131194: || (overlap<fMinOverlap)
! 131195: || (overlap==fMinOverlap && growth<fMinGrowth)
! 131196: || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
! 131197: ){
! 131198: bBest = 1;
! 131199: fMinOverlap = overlap;
! 131200: }
! 131201: #else
! 131202: if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
! 131203: bBest = 1;
! 131204: }
! 131205: #endif
! 131206: if( bBest ){
! 131207: fMinGrowth = growth;
! 131208: fMinArea = area;
! 131209: iBest = cell.iRowid;
! 131210: }
! 131211: }
! 131212:
! 131213: sqlite3_free(aCell);
! 131214: rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
! 131215: nodeRelease(pRtree, pNode);
! 131216: pNode = pChild;
! 131217: }
! 131218:
! 131219: *ppLeaf = pNode;
! 131220: return rc;
! 131221: }
! 131222:
! 131223: /*
! 131224: ** A cell with the same content as pCell has just been inserted into
! 131225: ** the node pNode. This function updates the bounding box cells in
! 131226: ** all ancestor elements.
! 131227: */
! 131228: static int AdjustTree(
! 131229: Rtree *pRtree, /* Rtree table */
! 131230: RtreeNode *pNode, /* Adjust ancestry of this node. */
! 131231: RtreeCell *pCell /* This cell was just inserted */
! 131232: ){
! 131233: RtreeNode *p = pNode;
! 131234: while( p->pParent ){
! 131235: RtreeNode *pParent = p->pParent;
! 131236: RtreeCell cell;
! 131237: int iCell;
! 131238:
! 131239: if( nodeParentIndex(pRtree, p, &iCell) ){
! 131240: return SQLITE_CORRUPT_VTAB;
! 131241: }
! 131242:
! 131243: nodeGetCell(pRtree, pParent, iCell, &cell);
! 131244: if( !cellContains(pRtree, &cell, pCell) ){
! 131245: cellUnion(pRtree, &cell, pCell);
! 131246: nodeOverwriteCell(pRtree, pParent, &cell, iCell);
! 131247: }
! 131248:
! 131249: p = pParent;
! 131250: }
! 131251: return SQLITE_OK;
! 131252: }
! 131253:
! 131254: /*
! 131255: ** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
! 131256: */
! 131257: static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
! 131258: sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
! 131259: sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
! 131260: sqlite3_step(pRtree->pWriteRowid);
! 131261: return sqlite3_reset(pRtree->pWriteRowid);
! 131262: }
! 131263:
! 131264: /*
! 131265: ** Write mapping (iNode->iPar) to the <rtree>_parent table.
! 131266: */
! 131267: static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
! 131268: sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
! 131269: sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
! 131270: sqlite3_step(pRtree->pWriteParent);
! 131271: return sqlite3_reset(pRtree->pWriteParent);
! 131272: }
! 131273:
! 131274: static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
! 131275:
! 131276: #if VARIANT_GUTTMAN_LINEAR_SPLIT
! 131277: /*
! 131278: ** Implementation of the linear variant of the PickNext() function from
! 131279: ** Guttman[84].
! 131280: */
! 131281: static RtreeCell *LinearPickNext(
! 131282: Rtree *pRtree,
! 131283: RtreeCell *aCell,
! 131284: int nCell,
! 131285: RtreeCell *pLeftBox,
! 131286: RtreeCell *pRightBox,
! 131287: int *aiUsed
! 131288: ){
! 131289: int ii;
! 131290: for(ii=0; aiUsed[ii]; ii++);
! 131291: aiUsed[ii] = 1;
! 131292: return &aCell[ii];
! 131293: }
! 131294:
! 131295: /*
! 131296: ** Implementation of the linear variant of the PickSeeds() function from
! 131297: ** Guttman[84].
! 131298: */
! 131299: static void LinearPickSeeds(
! 131300: Rtree *pRtree,
! 131301: RtreeCell *aCell,
! 131302: int nCell,
! 131303: int *piLeftSeed,
! 131304: int *piRightSeed
! 131305: ){
! 131306: int i;
! 131307: int iLeftSeed = 0;
! 131308: int iRightSeed = 1;
! 131309: float maxNormalInnerWidth = 0.0;
! 131310:
! 131311: /* Pick two "seed" cells from the array of cells. The algorithm used
! 131312: ** here is the LinearPickSeeds algorithm from Gutman[1984]. The
! 131313: ** indices of the two seed cells in the array are stored in local
! 131314: ** variables iLeftSeek and iRightSeed.
! 131315: */
! 131316: for(i=0; i<pRtree->nDim; i++){
! 131317: float x1 = DCOORD(aCell[0].aCoord[i*2]);
! 131318: float x2 = DCOORD(aCell[0].aCoord[i*2+1]);
! 131319: float x3 = x1;
! 131320: float x4 = x2;
! 131321: int jj;
! 131322:
! 131323: int iCellLeft = 0;
! 131324: int iCellRight = 0;
! 131325:
! 131326: for(jj=1; jj<nCell; jj++){
! 131327: float left = DCOORD(aCell[jj].aCoord[i*2]);
! 131328: float right = DCOORD(aCell[jj].aCoord[i*2+1]);
! 131329:
! 131330: if( left<x1 ) x1 = left;
! 131331: if( right>x4 ) x4 = right;
! 131332: if( left>x3 ){
! 131333: x3 = left;
! 131334: iCellRight = jj;
! 131335: }
! 131336: if( right<x2 ){
! 131337: x2 = right;
! 131338: iCellLeft = jj;
! 131339: }
! 131340: }
! 131341:
! 131342: if( x4!=x1 ){
! 131343: float normalwidth = (x3 - x2) / (x4 - x1);
! 131344: if( normalwidth>maxNormalInnerWidth ){
! 131345: iLeftSeed = iCellLeft;
! 131346: iRightSeed = iCellRight;
! 131347: }
! 131348: }
! 131349: }
! 131350:
! 131351: *piLeftSeed = iLeftSeed;
! 131352: *piRightSeed = iRightSeed;
! 131353: }
! 131354: #endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
! 131355:
! 131356: #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
! 131357: /*
! 131358: ** Implementation of the quadratic variant of the PickNext() function from
! 131359: ** Guttman[84].
! 131360: */
! 131361: static RtreeCell *QuadraticPickNext(
! 131362: Rtree *pRtree,
! 131363: RtreeCell *aCell,
! 131364: int nCell,
! 131365: RtreeCell *pLeftBox,
! 131366: RtreeCell *pRightBox,
! 131367: int *aiUsed
! 131368: ){
! 131369: #define FABS(a) ((a)<0.0?-1.0*(a):(a))
! 131370:
! 131371: int iSelect = -1;
! 131372: float fDiff;
! 131373: int ii;
! 131374: for(ii=0; ii<nCell; ii++){
! 131375: if( aiUsed[ii]==0 ){
! 131376: float left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
! 131377: float right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
! 131378: float diff = FABS(right-left);
! 131379: if( iSelect<0 || diff>fDiff ){
! 131380: fDiff = diff;
! 131381: iSelect = ii;
! 131382: }
! 131383: }
! 131384: }
! 131385: aiUsed[iSelect] = 1;
! 131386: return &aCell[iSelect];
! 131387: }
! 131388:
! 131389: /*
! 131390: ** Implementation of the quadratic variant of the PickSeeds() function from
! 131391: ** Guttman[84].
! 131392: */
! 131393: static void QuadraticPickSeeds(
! 131394: Rtree *pRtree,
! 131395: RtreeCell *aCell,
! 131396: int nCell,
! 131397: int *piLeftSeed,
! 131398: int *piRightSeed
! 131399: ){
! 131400: int ii;
! 131401: int jj;
! 131402:
! 131403: int iLeftSeed = 0;
! 131404: int iRightSeed = 1;
! 131405: float fWaste = 0.0;
! 131406:
! 131407: for(ii=0; ii<nCell; ii++){
! 131408: for(jj=ii+1; jj<nCell; jj++){
! 131409: float right = cellArea(pRtree, &aCell[jj]);
! 131410: float growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
! 131411: float waste = growth - right;
! 131412:
! 131413: if( waste>fWaste ){
! 131414: iLeftSeed = ii;
! 131415: iRightSeed = jj;
! 131416: fWaste = waste;
! 131417: }
! 131418: }
! 131419: }
! 131420:
! 131421: *piLeftSeed = iLeftSeed;
! 131422: *piRightSeed = iRightSeed;
! 131423: }
! 131424: #endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
! 131425:
! 131426: /*
! 131427: ** Arguments aIdx, aDistance and aSpare all point to arrays of size
! 131428: ** nIdx. The aIdx array contains the set of integers from 0 to
! 131429: ** (nIdx-1) in no particular order. This function sorts the values
! 131430: ** in aIdx according to the indexed values in aDistance. For
! 131431: ** example, assuming the inputs:
! 131432: **
! 131433: ** aIdx = { 0, 1, 2, 3 }
! 131434: ** aDistance = { 5.0, 2.0, 7.0, 6.0 }
! 131435: **
! 131436: ** this function sets the aIdx array to contain:
! 131437: **
! 131438: ** aIdx = { 0, 1, 2, 3 }
! 131439: **
! 131440: ** The aSpare array is used as temporary working space by the
! 131441: ** sorting algorithm.
! 131442: */
! 131443: static void SortByDistance(
! 131444: int *aIdx,
! 131445: int nIdx,
! 131446: float *aDistance,
! 131447: int *aSpare
! 131448: ){
! 131449: if( nIdx>1 ){
! 131450: int iLeft = 0;
! 131451: int iRight = 0;
! 131452:
! 131453: int nLeft = nIdx/2;
! 131454: int nRight = nIdx-nLeft;
! 131455: int *aLeft = aIdx;
! 131456: int *aRight = &aIdx[nLeft];
! 131457:
! 131458: SortByDistance(aLeft, nLeft, aDistance, aSpare);
! 131459: SortByDistance(aRight, nRight, aDistance, aSpare);
! 131460:
! 131461: memcpy(aSpare, aLeft, sizeof(int)*nLeft);
! 131462: aLeft = aSpare;
! 131463:
! 131464: while( iLeft<nLeft || iRight<nRight ){
! 131465: if( iLeft==nLeft ){
! 131466: aIdx[iLeft+iRight] = aRight[iRight];
! 131467: iRight++;
! 131468: }else if( iRight==nRight ){
! 131469: aIdx[iLeft+iRight] = aLeft[iLeft];
! 131470: iLeft++;
! 131471: }else{
! 131472: float fLeft = aDistance[aLeft[iLeft]];
! 131473: float fRight = aDistance[aRight[iRight]];
! 131474: if( fLeft<fRight ){
! 131475: aIdx[iLeft+iRight] = aLeft[iLeft];
! 131476: iLeft++;
! 131477: }else{
! 131478: aIdx[iLeft+iRight] = aRight[iRight];
! 131479: iRight++;
! 131480: }
! 131481: }
! 131482: }
! 131483:
! 131484: #if 0
! 131485: /* Check that the sort worked */
! 131486: {
! 131487: int jj;
! 131488: for(jj=1; jj<nIdx; jj++){
! 131489: float left = aDistance[aIdx[jj-1]];
! 131490: float right = aDistance[aIdx[jj]];
! 131491: assert( left<=right );
! 131492: }
! 131493: }
! 131494: #endif
! 131495: }
! 131496: }
! 131497:
! 131498: /*
! 131499: ** Arguments aIdx, aCell and aSpare all point to arrays of size
! 131500: ** nIdx. The aIdx array contains the set of integers from 0 to
! 131501: ** (nIdx-1) in no particular order. This function sorts the values
! 131502: ** in aIdx according to dimension iDim of the cells in aCell. The
! 131503: ** minimum value of dimension iDim is considered first, the
! 131504: ** maximum used to break ties.
! 131505: **
! 131506: ** The aSpare array is used as temporary working space by the
! 131507: ** sorting algorithm.
! 131508: */
! 131509: static void SortByDimension(
! 131510: Rtree *pRtree,
! 131511: int *aIdx,
! 131512: int nIdx,
! 131513: int iDim,
! 131514: RtreeCell *aCell,
! 131515: int *aSpare
! 131516: ){
! 131517: if( nIdx>1 ){
! 131518:
! 131519: int iLeft = 0;
! 131520: int iRight = 0;
! 131521:
! 131522: int nLeft = nIdx/2;
! 131523: int nRight = nIdx-nLeft;
! 131524: int *aLeft = aIdx;
! 131525: int *aRight = &aIdx[nLeft];
! 131526:
! 131527: SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
! 131528: SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
! 131529:
! 131530: memcpy(aSpare, aLeft, sizeof(int)*nLeft);
! 131531: aLeft = aSpare;
! 131532: while( iLeft<nLeft || iRight<nRight ){
! 131533: double xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
! 131534: double xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
! 131535: double xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
! 131536: double xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
! 131537: if( (iLeft!=nLeft) && ((iRight==nRight)
! 131538: || (xleft1<xright1)
! 131539: || (xleft1==xright1 && xleft2<xright2)
! 131540: )){
! 131541: aIdx[iLeft+iRight] = aLeft[iLeft];
! 131542: iLeft++;
! 131543: }else{
! 131544: aIdx[iLeft+iRight] = aRight[iRight];
! 131545: iRight++;
! 131546: }
! 131547: }
! 131548:
! 131549: #if 0
! 131550: /* Check that the sort worked */
! 131551: {
! 131552: int jj;
! 131553: for(jj=1; jj<nIdx; jj++){
! 131554: float xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
! 131555: float xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
! 131556: float xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
! 131557: float xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
! 131558: assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
! 131559: }
! 131560: }
! 131561: #endif
! 131562: }
! 131563: }
! 131564:
! 131565: #if VARIANT_RSTARTREE_SPLIT
! 131566: /*
! 131567: ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
! 131568: */
! 131569: static int splitNodeStartree(
! 131570: Rtree *pRtree,
! 131571: RtreeCell *aCell,
! 131572: int nCell,
! 131573: RtreeNode *pLeft,
! 131574: RtreeNode *pRight,
! 131575: RtreeCell *pBboxLeft,
! 131576: RtreeCell *pBboxRight
! 131577: ){
! 131578: int **aaSorted;
! 131579: int *aSpare;
! 131580: int ii;
! 131581:
! 131582: int iBestDim = 0;
! 131583: int iBestSplit = 0;
! 131584: float fBestMargin = 0.0;
! 131585:
! 131586: int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
! 131587:
! 131588: aaSorted = (int **)sqlite3_malloc(nByte);
! 131589: if( !aaSorted ){
! 131590: return SQLITE_NOMEM;
! 131591: }
! 131592:
! 131593: aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
! 131594: memset(aaSorted, 0, nByte);
! 131595: for(ii=0; ii<pRtree->nDim; ii++){
! 131596: int jj;
! 131597: aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
! 131598: for(jj=0; jj<nCell; jj++){
! 131599: aaSorted[ii][jj] = jj;
! 131600: }
! 131601: SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
! 131602: }
! 131603:
! 131604: for(ii=0; ii<pRtree->nDim; ii++){
! 131605: float margin = 0.0;
! 131606: float fBestOverlap = 0.0;
! 131607: float fBestArea = 0.0;
! 131608: int iBestLeft = 0;
! 131609: int nLeft;
! 131610:
! 131611: for(
! 131612: nLeft=RTREE_MINCELLS(pRtree);
! 131613: nLeft<=(nCell-RTREE_MINCELLS(pRtree));
! 131614: nLeft++
! 131615: ){
! 131616: RtreeCell left;
! 131617: RtreeCell right;
! 131618: int kk;
! 131619: float overlap;
! 131620: float area;
! 131621:
! 131622: memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
! 131623: memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
! 131624: for(kk=1; kk<(nCell-1); kk++){
! 131625: if( kk<nLeft ){
! 131626: cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
! 131627: }else{
! 131628: cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
! 131629: }
! 131630: }
! 131631: margin += cellMargin(pRtree, &left);
! 131632: margin += cellMargin(pRtree, &right);
! 131633: overlap = cellOverlap(pRtree, &left, &right, 1, -1);
! 131634: area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
! 131635: if( (nLeft==RTREE_MINCELLS(pRtree))
! 131636: || (overlap<fBestOverlap)
! 131637: || (overlap==fBestOverlap && area<fBestArea)
! 131638: ){
! 131639: iBestLeft = nLeft;
! 131640: fBestOverlap = overlap;
! 131641: fBestArea = area;
! 131642: }
! 131643: }
! 131644:
! 131645: if( ii==0 || margin<fBestMargin ){
! 131646: iBestDim = ii;
! 131647: fBestMargin = margin;
! 131648: iBestSplit = iBestLeft;
! 131649: }
! 131650: }
! 131651:
! 131652: memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
! 131653: memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
! 131654: for(ii=0; ii<nCell; ii++){
! 131655: RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
! 131656: RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
! 131657: RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
! 131658: nodeInsertCell(pRtree, pTarget, pCell);
! 131659: cellUnion(pRtree, pBbox, pCell);
! 131660: }
! 131661:
! 131662: sqlite3_free(aaSorted);
! 131663: return SQLITE_OK;
! 131664: }
! 131665: #endif
! 131666:
! 131667: #if VARIANT_GUTTMAN_SPLIT
! 131668: /*
! 131669: ** Implementation of the regular R-tree SplitNode from Guttman[1984].
! 131670: */
! 131671: static int splitNodeGuttman(
! 131672: Rtree *pRtree,
! 131673: RtreeCell *aCell,
! 131674: int nCell,
! 131675: RtreeNode *pLeft,
! 131676: RtreeNode *pRight,
! 131677: RtreeCell *pBboxLeft,
! 131678: RtreeCell *pBboxRight
! 131679: ){
! 131680: int iLeftSeed = 0;
! 131681: int iRightSeed = 1;
! 131682: int *aiUsed;
! 131683: int i;
! 131684:
! 131685: aiUsed = sqlite3_malloc(sizeof(int)*nCell);
! 131686: if( !aiUsed ){
! 131687: return SQLITE_NOMEM;
! 131688: }
! 131689: memset(aiUsed, 0, sizeof(int)*nCell);
! 131690:
! 131691: PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
! 131692:
! 131693: memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
! 131694: memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
! 131695: nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
! 131696: nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
! 131697: aiUsed[iLeftSeed] = 1;
! 131698: aiUsed[iRightSeed] = 1;
! 131699:
! 131700: for(i=nCell-2; i>0; i--){
! 131701: RtreeCell *pNext;
! 131702: pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
! 131703: float diff =
! 131704: cellGrowth(pRtree, pBboxLeft, pNext) -
! 131705: cellGrowth(pRtree, pBboxRight, pNext)
! 131706: ;
! 131707: if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
! 131708: || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
! 131709: ){
! 131710: nodeInsertCell(pRtree, pRight, pNext);
! 131711: cellUnion(pRtree, pBboxRight, pNext);
! 131712: }else{
! 131713: nodeInsertCell(pRtree, pLeft, pNext);
! 131714: cellUnion(pRtree, pBboxLeft, pNext);
! 131715: }
! 131716: }
! 131717:
! 131718: sqlite3_free(aiUsed);
! 131719: return SQLITE_OK;
! 131720: }
! 131721: #endif
! 131722:
! 131723: static int updateMapping(
! 131724: Rtree *pRtree,
! 131725: i64 iRowid,
! 131726: RtreeNode *pNode,
! 131727: int iHeight
! 131728: ){
! 131729: int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
! 131730: xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
! 131731: if( iHeight>0 ){
! 131732: RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
! 131733: if( pChild ){
! 131734: nodeRelease(pRtree, pChild->pParent);
! 131735: nodeReference(pNode);
! 131736: pChild->pParent = pNode;
! 131737: }
! 131738: }
! 131739: return xSetMapping(pRtree, iRowid, pNode->iNode);
! 131740: }
! 131741:
! 131742: static int SplitNode(
! 131743: Rtree *pRtree,
! 131744: RtreeNode *pNode,
! 131745: RtreeCell *pCell,
! 131746: int iHeight
! 131747: ){
! 131748: int i;
! 131749: int newCellIsRight = 0;
! 131750:
! 131751: int rc = SQLITE_OK;
! 131752: int nCell = NCELL(pNode);
! 131753: RtreeCell *aCell;
! 131754: int *aiUsed;
! 131755:
! 131756: RtreeNode *pLeft = 0;
! 131757: RtreeNode *pRight = 0;
! 131758:
! 131759: RtreeCell leftbbox;
! 131760: RtreeCell rightbbox;
! 131761:
! 131762: /* Allocate an array and populate it with a copy of pCell and
! 131763: ** all cells from node pLeft. Then zero the original node.
! 131764: */
! 131765: aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
! 131766: if( !aCell ){
! 131767: rc = SQLITE_NOMEM;
! 131768: goto splitnode_out;
! 131769: }
! 131770: aiUsed = (int *)&aCell[nCell+1];
! 131771: memset(aiUsed, 0, sizeof(int)*(nCell+1));
! 131772: for(i=0; i<nCell; i++){
! 131773: nodeGetCell(pRtree, pNode, i, &aCell[i]);
! 131774: }
! 131775: nodeZero(pRtree, pNode);
! 131776: memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
! 131777: nCell++;
! 131778:
! 131779: if( pNode->iNode==1 ){
! 131780: pRight = nodeNew(pRtree, pNode);
! 131781: pLeft = nodeNew(pRtree, pNode);
! 131782: pRtree->iDepth++;
! 131783: pNode->isDirty = 1;
! 131784: writeInt16(pNode->zData, pRtree->iDepth);
! 131785: }else{
! 131786: pLeft = pNode;
! 131787: pRight = nodeNew(pRtree, pLeft->pParent);
! 131788: nodeReference(pLeft);
! 131789: }
! 131790:
! 131791: if( !pLeft || !pRight ){
! 131792: rc = SQLITE_NOMEM;
! 131793: goto splitnode_out;
! 131794: }
! 131795:
! 131796: memset(pLeft->zData, 0, pRtree->iNodeSize);
! 131797: memset(pRight->zData, 0, pRtree->iNodeSize);
! 131798:
! 131799: rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
! 131800: if( rc!=SQLITE_OK ){
! 131801: goto splitnode_out;
! 131802: }
! 131803:
! 131804: /* Ensure both child nodes have node numbers assigned to them by calling
! 131805: ** nodeWrite(). Node pRight always needs a node number, as it was created
! 131806: ** by nodeNew() above. But node pLeft sometimes already has a node number.
! 131807: ** In this case avoid the all to nodeWrite().
! 131808: */
! 131809: if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
! 131810: || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
! 131811: ){
! 131812: goto splitnode_out;
! 131813: }
! 131814:
! 131815: rightbbox.iRowid = pRight->iNode;
! 131816: leftbbox.iRowid = pLeft->iNode;
! 131817:
! 131818: if( pNode->iNode==1 ){
! 131819: rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
! 131820: if( rc!=SQLITE_OK ){
! 131821: goto splitnode_out;
! 131822: }
! 131823: }else{
! 131824: RtreeNode *pParent = pLeft->pParent;
! 131825: int iCell;
! 131826: rc = nodeParentIndex(pRtree, pLeft, &iCell);
! 131827: if( rc==SQLITE_OK ){
! 131828: nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
! 131829: rc = AdjustTree(pRtree, pParent, &leftbbox);
! 131830: }
! 131831: if( rc!=SQLITE_OK ){
! 131832: goto splitnode_out;
! 131833: }
! 131834: }
! 131835: if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
! 131836: goto splitnode_out;
! 131837: }
! 131838:
! 131839: for(i=0; i<NCELL(pRight); i++){
! 131840: i64 iRowid = nodeGetRowid(pRtree, pRight, i);
! 131841: rc = updateMapping(pRtree, iRowid, pRight, iHeight);
! 131842: if( iRowid==pCell->iRowid ){
! 131843: newCellIsRight = 1;
! 131844: }
! 131845: if( rc!=SQLITE_OK ){
! 131846: goto splitnode_out;
! 131847: }
! 131848: }
! 131849: if( pNode->iNode==1 ){
! 131850: for(i=0; i<NCELL(pLeft); i++){
! 131851: i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
! 131852: rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
! 131853: if( rc!=SQLITE_OK ){
! 131854: goto splitnode_out;
! 131855: }
! 131856: }
! 131857: }else if( newCellIsRight==0 ){
! 131858: rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
! 131859: }
! 131860:
! 131861: if( rc==SQLITE_OK ){
! 131862: rc = nodeRelease(pRtree, pRight);
! 131863: pRight = 0;
! 131864: }
! 131865: if( rc==SQLITE_OK ){
! 131866: rc = nodeRelease(pRtree, pLeft);
! 131867: pLeft = 0;
! 131868: }
! 131869:
! 131870: splitnode_out:
! 131871: nodeRelease(pRtree, pRight);
! 131872: nodeRelease(pRtree, pLeft);
! 131873: sqlite3_free(aCell);
! 131874: return rc;
! 131875: }
! 131876:
! 131877: /*
! 131878: ** If node pLeaf is not the root of the r-tree and its pParent pointer is
! 131879: ** still NULL, load all ancestor nodes of pLeaf into memory and populate
! 131880: ** the pLeaf->pParent chain all the way up to the root node.
! 131881: **
! 131882: ** This operation is required when a row is deleted (or updated - an update
! 131883: ** is implemented as a delete followed by an insert). SQLite provides the
! 131884: ** rowid of the row to delete, which can be used to find the leaf on which
! 131885: ** the entry resides (argument pLeaf). Once the leaf is located, this
! 131886: ** function is called to determine its ancestry.
! 131887: */
! 131888: static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
! 131889: int rc = SQLITE_OK;
! 131890: RtreeNode *pChild = pLeaf;
! 131891: while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
! 131892: int rc2 = SQLITE_OK; /* sqlite3_reset() return code */
! 131893: sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
! 131894: rc = sqlite3_step(pRtree->pReadParent);
! 131895: if( rc==SQLITE_ROW ){
! 131896: RtreeNode *pTest; /* Used to test for reference loops */
! 131897: i64 iNode; /* Node number of parent node */
! 131898:
! 131899: /* Before setting pChild->pParent, test that we are not creating a
! 131900: ** loop of references (as we would if, say, pChild==pParent). We don't
! 131901: ** want to do this as it leads to a memory leak when trying to delete
! 131902: ** the referenced counted node structures.
! 131903: */
! 131904: iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
! 131905: for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
! 131906: if( !pTest ){
! 131907: rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
! 131908: }
! 131909: }
! 131910: rc = sqlite3_reset(pRtree->pReadParent);
! 131911: if( rc==SQLITE_OK ) rc = rc2;
! 131912: if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
! 131913: pChild = pChild->pParent;
! 131914: }
! 131915: return rc;
! 131916: }
! 131917:
! 131918: static int deleteCell(Rtree *, RtreeNode *, int, int);
! 131919:
! 131920: static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
! 131921: int rc;
! 131922: int rc2;
! 131923: RtreeNode *pParent = 0;
! 131924: int iCell;
! 131925:
! 131926: assert( pNode->nRef==1 );
! 131927:
! 131928: /* Remove the entry in the parent cell. */
! 131929: rc = nodeParentIndex(pRtree, pNode, &iCell);
! 131930: if( rc==SQLITE_OK ){
! 131931: pParent = pNode->pParent;
! 131932: pNode->pParent = 0;
! 131933: rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
! 131934: }
! 131935: rc2 = nodeRelease(pRtree, pParent);
! 131936: if( rc==SQLITE_OK ){
! 131937: rc = rc2;
! 131938: }
! 131939: if( rc!=SQLITE_OK ){
! 131940: return rc;
! 131941: }
! 131942:
! 131943: /* Remove the xxx_node entry. */
! 131944: sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
! 131945: sqlite3_step(pRtree->pDeleteNode);
! 131946: if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
! 131947: return rc;
! 131948: }
! 131949:
! 131950: /* Remove the xxx_parent entry. */
! 131951: sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
! 131952: sqlite3_step(pRtree->pDeleteParent);
! 131953: if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
! 131954: return rc;
! 131955: }
! 131956:
! 131957: /* Remove the node from the in-memory hash table and link it into
! 131958: ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
! 131959: */
! 131960: nodeHashDelete(pRtree, pNode);
! 131961: pNode->iNode = iHeight;
! 131962: pNode->pNext = pRtree->pDeleted;
! 131963: pNode->nRef++;
! 131964: pRtree->pDeleted = pNode;
! 131965:
! 131966: return SQLITE_OK;
! 131967: }
! 131968:
! 131969: static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
! 131970: RtreeNode *pParent = pNode->pParent;
! 131971: int rc = SQLITE_OK;
! 131972: if( pParent ){
! 131973: int ii;
! 131974: int nCell = NCELL(pNode);
! 131975: RtreeCell box; /* Bounding box for pNode */
! 131976: nodeGetCell(pRtree, pNode, 0, &box);
! 131977: for(ii=1; ii<nCell; ii++){
! 131978: RtreeCell cell;
! 131979: nodeGetCell(pRtree, pNode, ii, &cell);
! 131980: cellUnion(pRtree, &box, &cell);
! 131981: }
! 131982: box.iRowid = pNode->iNode;
! 131983: rc = nodeParentIndex(pRtree, pNode, &ii);
! 131984: if( rc==SQLITE_OK ){
! 131985: nodeOverwriteCell(pRtree, pParent, &box, ii);
! 131986: rc = fixBoundingBox(pRtree, pParent);
! 131987: }
! 131988: }
! 131989: return rc;
! 131990: }
! 131991:
! 131992: /*
! 131993: ** Delete the cell at index iCell of node pNode. After removing the
! 131994: ** cell, adjust the r-tree data structure if required.
! 131995: */
! 131996: static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
! 131997: RtreeNode *pParent;
! 131998: int rc;
! 131999:
! 132000: if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
! 132001: return rc;
! 132002: }
! 132003:
! 132004: /* Remove the cell from the node. This call just moves bytes around
! 132005: ** the in-memory node image, so it cannot fail.
! 132006: */
! 132007: nodeDeleteCell(pRtree, pNode, iCell);
! 132008:
! 132009: /* If the node is not the tree root and now has less than the minimum
! 132010: ** number of cells, remove it from the tree. Otherwise, update the
! 132011: ** cell in the parent node so that it tightly contains the updated
! 132012: ** node.
! 132013: */
! 132014: pParent = pNode->pParent;
! 132015: assert( pParent || pNode->iNode==1 );
! 132016: if( pParent ){
! 132017: if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
! 132018: rc = removeNode(pRtree, pNode, iHeight);
! 132019: }else{
! 132020: rc = fixBoundingBox(pRtree, pNode);
! 132021: }
! 132022: }
! 132023:
! 132024: return rc;
! 132025: }
! 132026:
! 132027: static int Reinsert(
! 132028: Rtree *pRtree,
! 132029: RtreeNode *pNode,
! 132030: RtreeCell *pCell,
! 132031: int iHeight
! 132032: ){
! 132033: int *aOrder;
! 132034: int *aSpare;
! 132035: RtreeCell *aCell;
! 132036: float *aDistance;
! 132037: int nCell;
! 132038: float aCenterCoord[RTREE_MAX_DIMENSIONS];
! 132039: int iDim;
! 132040: int ii;
! 132041: int rc = SQLITE_OK;
! 132042:
! 132043: memset(aCenterCoord, 0, sizeof(float)*RTREE_MAX_DIMENSIONS);
! 132044:
! 132045: nCell = NCELL(pNode)+1;
! 132046:
! 132047: /* Allocate the buffers used by this operation. The allocation is
! 132048: ** relinquished before this function returns.
! 132049: */
! 132050: aCell = (RtreeCell *)sqlite3_malloc(nCell * (
! 132051: sizeof(RtreeCell) + /* aCell array */
! 132052: sizeof(int) + /* aOrder array */
! 132053: sizeof(int) + /* aSpare array */
! 132054: sizeof(float) /* aDistance array */
! 132055: ));
! 132056: if( !aCell ){
! 132057: return SQLITE_NOMEM;
! 132058: }
! 132059: aOrder = (int *)&aCell[nCell];
! 132060: aSpare = (int *)&aOrder[nCell];
! 132061: aDistance = (float *)&aSpare[nCell];
! 132062:
! 132063: for(ii=0; ii<nCell; ii++){
! 132064: if( ii==(nCell-1) ){
! 132065: memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
! 132066: }else{
! 132067: nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
! 132068: }
! 132069: aOrder[ii] = ii;
! 132070: for(iDim=0; iDim<pRtree->nDim; iDim++){
! 132071: aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2]);
! 132072: aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2+1]);
! 132073: }
! 132074: }
! 132075: for(iDim=0; iDim<pRtree->nDim; iDim++){
! 132076: aCenterCoord[iDim] = (float)(aCenterCoord[iDim]/((float)nCell*2.0));
! 132077: }
! 132078:
! 132079: for(ii=0; ii<nCell; ii++){
! 132080: aDistance[ii] = 0.0;
! 132081: for(iDim=0; iDim<pRtree->nDim; iDim++){
! 132082: float coord = (float)(DCOORD(aCell[ii].aCoord[iDim*2+1]) -
! 132083: DCOORD(aCell[ii].aCoord[iDim*2]));
! 132084: aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
! 132085: }
! 132086: }
! 132087:
! 132088: SortByDistance(aOrder, nCell, aDistance, aSpare);
! 132089: nodeZero(pRtree, pNode);
! 132090:
! 132091: for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
! 132092: RtreeCell *p = &aCell[aOrder[ii]];
! 132093: nodeInsertCell(pRtree, pNode, p);
! 132094: if( p->iRowid==pCell->iRowid ){
! 132095: if( iHeight==0 ){
! 132096: rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
! 132097: }else{
! 132098: rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
! 132099: }
! 132100: }
! 132101: }
! 132102: if( rc==SQLITE_OK ){
! 132103: rc = fixBoundingBox(pRtree, pNode);
! 132104: }
! 132105: for(; rc==SQLITE_OK && ii<nCell; ii++){
! 132106: /* Find a node to store this cell in. pNode->iNode currently contains
! 132107: ** the height of the sub-tree headed by the cell.
! 132108: */
! 132109: RtreeNode *pInsert;
! 132110: RtreeCell *p = &aCell[aOrder[ii]];
! 132111: rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
! 132112: if( rc==SQLITE_OK ){
! 132113: int rc2;
! 132114: rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
! 132115: rc2 = nodeRelease(pRtree, pInsert);
! 132116: if( rc==SQLITE_OK ){
! 132117: rc = rc2;
! 132118: }
! 132119: }
! 132120: }
! 132121:
! 132122: sqlite3_free(aCell);
! 132123: return rc;
! 132124: }
! 132125:
! 132126: /*
! 132127: ** Insert cell pCell into node pNode. Node pNode is the head of a
! 132128: ** subtree iHeight high (leaf nodes have iHeight==0).
! 132129: */
! 132130: static int rtreeInsertCell(
! 132131: Rtree *pRtree,
! 132132: RtreeNode *pNode,
! 132133: RtreeCell *pCell,
! 132134: int iHeight
! 132135: ){
! 132136: int rc = SQLITE_OK;
! 132137: if( iHeight>0 ){
! 132138: RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
! 132139: if( pChild ){
! 132140: nodeRelease(pRtree, pChild->pParent);
! 132141: nodeReference(pNode);
! 132142: pChild->pParent = pNode;
! 132143: }
! 132144: }
! 132145: if( nodeInsertCell(pRtree, pNode, pCell) ){
! 132146: #if VARIANT_RSTARTREE_REINSERT
! 132147: if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
! 132148: rc = SplitNode(pRtree, pNode, pCell, iHeight);
! 132149: }else{
! 132150: pRtree->iReinsertHeight = iHeight;
! 132151: rc = Reinsert(pRtree, pNode, pCell, iHeight);
! 132152: }
! 132153: #else
! 132154: rc = SplitNode(pRtree, pNode, pCell, iHeight);
! 132155: #endif
! 132156: }else{
! 132157: rc = AdjustTree(pRtree, pNode, pCell);
! 132158: if( rc==SQLITE_OK ){
! 132159: if( iHeight==0 ){
! 132160: rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
! 132161: }else{
! 132162: rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
! 132163: }
! 132164: }
! 132165: }
! 132166: return rc;
! 132167: }
! 132168:
! 132169: static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
! 132170: int ii;
! 132171: int rc = SQLITE_OK;
! 132172: int nCell = NCELL(pNode);
! 132173:
! 132174: for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
! 132175: RtreeNode *pInsert;
! 132176: RtreeCell cell;
! 132177: nodeGetCell(pRtree, pNode, ii, &cell);
! 132178:
! 132179: /* Find a node to store this cell in. pNode->iNode currently contains
! 132180: ** the height of the sub-tree headed by the cell.
! 132181: */
! 132182: rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
! 132183: if( rc==SQLITE_OK ){
! 132184: int rc2;
! 132185: rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
! 132186: rc2 = nodeRelease(pRtree, pInsert);
! 132187: if( rc==SQLITE_OK ){
! 132188: rc = rc2;
! 132189: }
! 132190: }
! 132191: }
! 132192: return rc;
! 132193: }
! 132194:
! 132195: /*
! 132196: ** Select a currently unused rowid for a new r-tree record.
! 132197: */
! 132198: static int newRowid(Rtree *pRtree, i64 *piRowid){
! 132199: int rc;
! 132200: sqlite3_bind_null(pRtree->pWriteRowid, 1);
! 132201: sqlite3_bind_null(pRtree->pWriteRowid, 2);
! 132202: sqlite3_step(pRtree->pWriteRowid);
! 132203: rc = sqlite3_reset(pRtree->pWriteRowid);
! 132204: *piRowid = sqlite3_last_insert_rowid(pRtree->db);
! 132205: return rc;
! 132206: }
! 132207:
! 132208: /*
! 132209: ** Remove the entry with rowid=iDelete from the r-tree structure.
! 132210: */
! 132211: static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
! 132212: int rc; /* Return code */
! 132213: RtreeNode *pLeaf; /* Leaf node containing record iDelete */
! 132214: int iCell; /* Index of iDelete cell in pLeaf */
! 132215: RtreeNode *pRoot; /* Root node of rtree structure */
! 132216:
! 132217:
! 132218: /* Obtain a reference to the root node to initialise Rtree.iDepth */
! 132219: rc = nodeAcquire(pRtree, 1, 0, &pRoot);
! 132220:
! 132221: /* Obtain a reference to the leaf node that contains the entry
! 132222: ** about to be deleted.
! 132223: */
! 132224: if( rc==SQLITE_OK ){
! 132225: rc = findLeafNode(pRtree, iDelete, &pLeaf);
! 132226: }
! 132227:
! 132228: /* Delete the cell in question from the leaf node. */
! 132229: if( rc==SQLITE_OK ){
! 132230: int rc2;
! 132231: rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
! 132232: if( rc==SQLITE_OK ){
! 132233: rc = deleteCell(pRtree, pLeaf, iCell, 0);
! 132234: }
! 132235: rc2 = nodeRelease(pRtree, pLeaf);
! 132236: if( rc==SQLITE_OK ){
! 132237: rc = rc2;
! 132238: }
! 132239: }
! 132240:
! 132241: /* Delete the corresponding entry in the <rtree>_rowid table. */
! 132242: if( rc==SQLITE_OK ){
! 132243: sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
! 132244: sqlite3_step(pRtree->pDeleteRowid);
! 132245: rc = sqlite3_reset(pRtree->pDeleteRowid);
! 132246: }
! 132247:
! 132248: /* Check if the root node now has exactly one child. If so, remove
! 132249: ** it, schedule the contents of the child for reinsertion and
! 132250: ** reduce the tree height by one.
! 132251: **
! 132252: ** This is equivalent to copying the contents of the child into
! 132253: ** the root node (the operation that Gutman's paper says to perform
! 132254: ** in this scenario).
! 132255: */
! 132256: if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
! 132257: int rc2;
! 132258: RtreeNode *pChild;
! 132259: i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
! 132260: rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
! 132261: if( rc==SQLITE_OK ){
! 132262: rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
! 132263: }
! 132264: rc2 = nodeRelease(pRtree, pChild);
! 132265: if( rc==SQLITE_OK ) rc = rc2;
! 132266: if( rc==SQLITE_OK ){
! 132267: pRtree->iDepth--;
! 132268: writeInt16(pRoot->zData, pRtree->iDepth);
! 132269: pRoot->isDirty = 1;
! 132270: }
! 132271: }
! 132272:
! 132273: /* Re-insert the contents of any underfull nodes removed from the tree. */
! 132274: for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
! 132275: if( rc==SQLITE_OK ){
! 132276: rc = reinsertNodeContent(pRtree, pLeaf);
! 132277: }
! 132278: pRtree->pDeleted = pLeaf->pNext;
! 132279: sqlite3_free(pLeaf);
! 132280: }
! 132281:
! 132282: /* Release the reference to the root node. */
! 132283: if( rc==SQLITE_OK ){
! 132284: rc = nodeRelease(pRtree, pRoot);
! 132285: }else{
! 132286: nodeRelease(pRtree, pRoot);
! 132287: }
! 132288:
! 132289: return rc;
! 132290: }
! 132291:
! 132292: /*
! 132293: ** The xUpdate method for rtree module virtual tables.
! 132294: */
! 132295: static int rtreeUpdate(
! 132296: sqlite3_vtab *pVtab,
! 132297: int nData,
! 132298: sqlite3_value **azData,
! 132299: sqlite_int64 *pRowid
! 132300: ){
! 132301: Rtree *pRtree = (Rtree *)pVtab;
! 132302: int rc = SQLITE_OK;
! 132303: RtreeCell cell; /* New cell to insert if nData>1 */
! 132304: int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
! 132305:
! 132306: rtreeReference(pRtree);
! 132307: assert(nData>=1);
! 132308:
! 132309: /* Constraint handling. A write operation on an r-tree table may return
! 132310: ** SQLITE_CONSTRAINT for two reasons:
! 132311: **
! 132312: ** 1. A duplicate rowid value, or
! 132313: ** 2. The supplied data violates the "x2>=x1" constraint.
! 132314: **
! 132315: ** In the first case, if the conflict-handling mode is REPLACE, then
! 132316: ** the conflicting row can be removed before proceeding. In the second
! 132317: ** case, SQLITE_CONSTRAINT must be returned regardless of the
! 132318: ** conflict-handling mode specified by the user.
! 132319: */
! 132320: if( nData>1 ){
! 132321: int ii;
! 132322:
! 132323: /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
! 132324: assert( nData==(pRtree->nDim*2 + 3) );
! 132325: if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
! 132326: for(ii=0; ii<(pRtree->nDim*2); ii+=2){
! 132327: cell.aCoord[ii].f = (float)sqlite3_value_double(azData[ii+3]);
! 132328: cell.aCoord[ii+1].f = (float)sqlite3_value_double(azData[ii+4]);
! 132329: if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
! 132330: rc = SQLITE_CONSTRAINT;
! 132331: goto constraint;
! 132332: }
! 132333: }
! 132334: }else{
! 132335: for(ii=0; ii<(pRtree->nDim*2); ii+=2){
! 132336: cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
! 132337: cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
! 132338: if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
! 132339: rc = SQLITE_CONSTRAINT;
! 132340: goto constraint;
! 132341: }
! 132342: }
! 132343: }
! 132344:
! 132345: /* If a rowid value was supplied, check if it is already present in
! 132346: ** the table. If so, the constraint has failed. */
! 132347: if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
! 132348: cell.iRowid = sqlite3_value_int64(azData[2]);
! 132349: if( sqlite3_value_type(azData[0])==SQLITE_NULL
! 132350: || sqlite3_value_int64(azData[0])!=cell.iRowid
! 132351: ){
! 132352: int steprc;
! 132353: sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
! 132354: steprc = sqlite3_step(pRtree->pReadRowid);
! 132355: rc = sqlite3_reset(pRtree->pReadRowid);
! 132356: if( SQLITE_ROW==steprc ){
! 132357: if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
! 132358: rc = rtreeDeleteRowid(pRtree, cell.iRowid);
! 132359: }else{
! 132360: rc = SQLITE_CONSTRAINT;
! 132361: goto constraint;
! 132362: }
! 132363: }
! 132364: }
! 132365: bHaveRowid = 1;
! 132366: }
! 132367: }
! 132368:
! 132369: /* If azData[0] is not an SQL NULL value, it is the rowid of a
! 132370: ** record to delete from the r-tree table. The following block does
! 132371: ** just that.
! 132372: */
! 132373: if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
! 132374: rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
! 132375: }
! 132376:
! 132377: /* If the azData[] array contains more than one element, elements
! 132378: ** (azData[2]..azData[argc-1]) contain a new record to insert into
! 132379: ** the r-tree structure.
! 132380: */
! 132381: if( rc==SQLITE_OK && nData>1 ){
! 132382: /* Insert the new record into the r-tree */
! 132383: RtreeNode *pLeaf;
! 132384:
! 132385: /* Figure out the rowid of the new row. */
! 132386: if( bHaveRowid==0 ){
! 132387: rc = newRowid(pRtree, &cell.iRowid);
! 132388: }
! 132389: *pRowid = cell.iRowid;
! 132390:
! 132391: if( rc==SQLITE_OK ){
! 132392: rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
! 132393: }
! 132394: if( rc==SQLITE_OK ){
! 132395: int rc2;
! 132396: pRtree->iReinsertHeight = -1;
! 132397: rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
! 132398: rc2 = nodeRelease(pRtree, pLeaf);
! 132399: if( rc==SQLITE_OK ){
! 132400: rc = rc2;
! 132401: }
! 132402: }
! 132403: }
! 132404:
! 132405: constraint:
! 132406: rtreeRelease(pRtree);
! 132407: return rc;
! 132408: }
! 132409:
! 132410: /*
! 132411: ** The xRename method for rtree module virtual tables.
! 132412: */
! 132413: static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
! 132414: Rtree *pRtree = (Rtree *)pVtab;
! 132415: int rc = SQLITE_NOMEM;
! 132416: char *zSql = sqlite3_mprintf(
! 132417: "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";"
! 132418: "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
! 132419: "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";"
! 132420: , pRtree->zDb, pRtree->zName, zNewName
! 132421: , pRtree->zDb, pRtree->zName, zNewName
! 132422: , pRtree->zDb, pRtree->zName, zNewName
! 132423: );
! 132424: if( zSql ){
! 132425: rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
! 132426: sqlite3_free(zSql);
! 132427: }
! 132428: return rc;
! 132429: }
! 132430:
! 132431: static sqlite3_module rtreeModule = {
! 132432: 0, /* iVersion */
! 132433: rtreeCreate, /* xCreate - create a table */
! 132434: rtreeConnect, /* xConnect - connect to an existing table */
! 132435: rtreeBestIndex, /* xBestIndex - Determine search strategy */
! 132436: rtreeDisconnect, /* xDisconnect - Disconnect from a table */
! 132437: rtreeDestroy, /* xDestroy - Drop a table */
! 132438: rtreeOpen, /* xOpen - open a cursor */
! 132439: rtreeClose, /* xClose - close a cursor */
! 132440: rtreeFilter, /* xFilter - configure scan constraints */
! 132441: rtreeNext, /* xNext - advance a cursor */
! 132442: rtreeEof, /* xEof */
! 132443: rtreeColumn, /* xColumn - read data */
! 132444: rtreeRowid, /* xRowid - read data */
! 132445: rtreeUpdate, /* xUpdate - write data */
! 132446: 0, /* xBegin - begin transaction */
! 132447: 0, /* xSync - sync transaction */
! 132448: 0, /* xCommit - commit transaction */
! 132449: 0, /* xRollback - rollback transaction */
! 132450: 0, /* xFindFunction - function overloading */
! 132451: rtreeRename, /* xRename - rename the table */
! 132452: 0, /* xSavepoint */
! 132453: 0, /* xRelease */
! 132454: 0 /* xRollbackTo */
! 132455: };
! 132456:
! 132457: static int rtreeSqlInit(
! 132458: Rtree *pRtree,
! 132459: sqlite3 *db,
! 132460: const char *zDb,
! 132461: const char *zPrefix,
! 132462: int isCreate
! 132463: ){
! 132464: int rc = SQLITE_OK;
! 132465:
! 132466: #define N_STATEMENT 9
! 132467: static const char *azSql[N_STATEMENT] = {
! 132468: /* Read and write the xxx_node table */
! 132469: "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
! 132470: "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
! 132471: "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
! 132472:
! 132473: /* Read and write the xxx_rowid table */
! 132474: "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
! 132475: "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
! 132476: "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
! 132477:
! 132478: /* Read and write the xxx_parent table */
! 132479: "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
! 132480: "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
! 132481: "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
! 132482: };
! 132483: sqlite3_stmt **appStmt[N_STATEMENT];
! 132484: int i;
! 132485:
! 132486: pRtree->db = db;
! 132487:
! 132488: if( isCreate ){
! 132489: char *zCreate = sqlite3_mprintf(
! 132490: "CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
! 132491: "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
! 132492: "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
! 132493: "INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
! 132494: zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
! 132495: );
! 132496: if( !zCreate ){
! 132497: return SQLITE_NOMEM;
! 132498: }
! 132499: rc = sqlite3_exec(db, zCreate, 0, 0, 0);
! 132500: sqlite3_free(zCreate);
! 132501: if( rc!=SQLITE_OK ){
! 132502: return rc;
! 132503: }
! 132504: }
! 132505:
! 132506: appStmt[0] = &pRtree->pReadNode;
! 132507: appStmt[1] = &pRtree->pWriteNode;
! 132508: appStmt[2] = &pRtree->pDeleteNode;
! 132509: appStmt[3] = &pRtree->pReadRowid;
! 132510: appStmt[4] = &pRtree->pWriteRowid;
! 132511: appStmt[5] = &pRtree->pDeleteRowid;
! 132512: appStmt[6] = &pRtree->pReadParent;
! 132513: appStmt[7] = &pRtree->pWriteParent;
! 132514: appStmt[8] = &pRtree->pDeleteParent;
! 132515:
! 132516: for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
! 132517: char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
! 132518: if( zSql ){
! 132519: rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0);
! 132520: }else{
! 132521: rc = SQLITE_NOMEM;
! 132522: }
! 132523: sqlite3_free(zSql);
! 132524: }
! 132525:
! 132526: return rc;
! 132527: }
! 132528:
! 132529: /*
! 132530: ** The second argument to this function contains the text of an SQL statement
! 132531: ** that returns a single integer value. The statement is compiled and executed
! 132532: ** using database connection db. If successful, the integer value returned
! 132533: ** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
! 132534: ** code is returned and the value of *piVal after returning is not defined.
! 132535: */
! 132536: static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
! 132537: int rc = SQLITE_NOMEM;
! 132538: if( zSql ){
! 132539: sqlite3_stmt *pStmt = 0;
! 132540: rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
! 132541: if( rc==SQLITE_OK ){
! 132542: if( SQLITE_ROW==sqlite3_step(pStmt) ){
! 132543: *piVal = sqlite3_column_int(pStmt, 0);
! 132544: }
! 132545: rc = sqlite3_finalize(pStmt);
! 132546: }
! 132547: }
! 132548: return rc;
! 132549: }
! 132550:
! 132551: /*
! 132552: ** This function is called from within the xConnect() or xCreate() method to
! 132553: ** determine the node-size used by the rtree table being created or connected
! 132554: ** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
! 132555: ** Otherwise, an SQLite error code is returned.
! 132556: **
! 132557: ** If this function is being called as part of an xConnect(), then the rtree
! 132558: ** table already exists. In this case the node-size is determined by inspecting
! 132559: ** the root node of the tree.
! 132560: **
! 132561: ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
! 132562: ** This ensures that each node is stored on a single database page. If the
! 132563: ** database page-size is so large that more than RTREE_MAXCELLS entries
! 132564: ** would fit in a single node, use a smaller node-size.
! 132565: */
! 132566: static int getNodeSize(
! 132567: sqlite3 *db, /* Database handle */
! 132568: Rtree *pRtree, /* Rtree handle */
! 132569: int isCreate /* True for xCreate, false for xConnect */
! 132570: ){
! 132571: int rc;
! 132572: char *zSql;
! 132573: if( isCreate ){
! 132574: int iPageSize = 0;
! 132575: zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
! 132576: rc = getIntFromStmt(db, zSql, &iPageSize);
! 132577: if( rc==SQLITE_OK ){
! 132578: pRtree->iNodeSize = iPageSize-64;
! 132579: if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
! 132580: pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
! 132581: }
! 132582: }
! 132583: }else{
! 132584: zSql = sqlite3_mprintf(
! 132585: "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
! 132586: pRtree->zDb, pRtree->zName
! 132587: );
! 132588: rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
! 132589: }
! 132590:
! 132591: sqlite3_free(zSql);
! 132592: return rc;
! 132593: }
! 132594:
! 132595: /*
! 132596: ** This function is the implementation of both the xConnect and xCreate
! 132597: ** methods of the r-tree virtual table.
! 132598: **
! 132599: ** argv[0] -> module name
! 132600: ** argv[1] -> database name
! 132601: ** argv[2] -> table name
! 132602: ** argv[...] -> column names...
! 132603: */
! 132604: static int rtreeInit(
! 132605: sqlite3 *db, /* Database connection */
! 132606: void *pAux, /* One of the RTREE_COORD_* constants */
! 132607: int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
! 132608: sqlite3_vtab **ppVtab, /* OUT: New virtual table */
! 132609: char **pzErr, /* OUT: Error message, if any */
! 132610: int isCreate /* True for xCreate, false for xConnect */
! 132611: ){
! 132612: int rc = SQLITE_OK;
! 132613: Rtree *pRtree;
! 132614: int nDb; /* Length of string argv[1] */
! 132615: int nName; /* Length of string argv[2] */
! 132616: int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
! 132617:
! 132618: const char *aErrMsg[] = {
! 132619: 0, /* 0 */
! 132620: "Wrong number of columns for an rtree table", /* 1 */
! 132621: "Too few columns for an rtree table", /* 2 */
! 132622: "Too many columns for an rtree table" /* 3 */
! 132623: };
! 132624:
! 132625: int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
! 132626: if( aErrMsg[iErr] ){
! 132627: *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
! 132628: return SQLITE_ERROR;
! 132629: }
! 132630:
! 132631: sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
! 132632:
! 132633: /* Allocate the sqlite3_vtab structure */
! 132634: nDb = strlen(argv[1]);
! 132635: nName = strlen(argv[2]);
! 132636: pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
! 132637: if( !pRtree ){
! 132638: return SQLITE_NOMEM;
! 132639: }
! 132640: memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
! 132641: pRtree->nBusy = 1;
! 132642: pRtree->base.pModule = &rtreeModule;
! 132643: pRtree->zDb = (char *)&pRtree[1];
! 132644: pRtree->zName = &pRtree->zDb[nDb+1];
! 132645: pRtree->nDim = (argc-4)/2;
! 132646: pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
! 132647: pRtree->eCoordType = eCoordType;
! 132648: memcpy(pRtree->zDb, argv[1], nDb);
! 132649: memcpy(pRtree->zName, argv[2], nName);
! 132650:
! 132651: /* Figure out the node size to use. */
! 132652: rc = getNodeSize(db, pRtree, isCreate);
! 132653:
! 132654: /* Create/Connect to the underlying relational database schema. If
! 132655: ** that is successful, call sqlite3_declare_vtab() to configure
! 132656: ** the r-tree table schema.
! 132657: */
! 132658: if( rc==SQLITE_OK ){
! 132659: if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
! 132660: *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
! 132661: }else{
! 132662: char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
! 132663: char *zTmp;
! 132664: int ii;
! 132665: for(ii=4; zSql && ii<argc; ii++){
! 132666: zTmp = zSql;
! 132667: zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
! 132668: sqlite3_free(zTmp);
! 132669: }
! 132670: if( zSql ){
! 132671: zTmp = zSql;
! 132672: zSql = sqlite3_mprintf("%s);", zTmp);
! 132673: sqlite3_free(zTmp);
! 132674: }
! 132675: if( !zSql ){
! 132676: rc = SQLITE_NOMEM;
! 132677: }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
! 132678: *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
! 132679: }
! 132680: sqlite3_free(zSql);
! 132681: }
! 132682: }
! 132683:
! 132684: if( rc==SQLITE_OK ){
! 132685: *ppVtab = (sqlite3_vtab *)pRtree;
! 132686: }else{
! 132687: rtreeRelease(pRtree);
! 132688: }
! 132689: return rc;
! 132690: }
! 132691:
! 132692:
! 132693: /*
! 132694: ** Implementation of a scalar function that decodes r-tree nodes to
! 132695: ** human readable strings. This can be used for debugging and analysis.
! 132696: **
! 132697: ** The scalar function takes two arguments, a blob of data containing
! 132698: ** an r-tree node, and the number of dimensions the r-tree indexes.
! 132699: ** For a two-dimensional r-tree structure called "rt", to deserialize
! 132700: ** all nodes, a statement like:
! 132701: **
! 132702: ** SELECT rtreenode(2, data) FROM rt_node;
! 132703: **
! 132704: ** The human readable string takes the form of a Tcl list with one
! 132705: ** entry for each cell in the r-tree node. Each entry is itself a
! 132706: ** list, containing the 8-byte rowid/pageno followed by the
! 132707: ** <num-dimension>*2 coordinates.
! 132708: */
! 132709: static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
! 132710: char *zText = 0;
! 132711: RtreeNode node;
! 132712: Rtree tree;
! 132713: int ii;
! 132714:
! 132715: UNUSED_PARAMETER(nArg);
! 132716: memset(&node, 0, sizeof(RtreeNode));
! 132717: memset(&tree, 0, sizeof(Rtree));
! 132718: tree.nDim = sqlite3_value_int(apArg[0]);
! 132719: tree.nBytesPerCell = 8 + 8 * tree.nDim;
! 132720: node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
! 132721:
! 132722: for(ii=0; ii<NCELL(&node); ii++){
! 132723: char zCell[512];
! 132724: int nCell = 0;
! 132725: RtreeCell cell;
! 132726: int jj;
! 132727:
! 132728: nodeGetCell(&tree, &node, ii, &cell);
! 132729: sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
! 132730: nCell = strlen(zCell);
! 132731: for(jj=0; jj<tree.nDim*2; jj++){
! 132732: sqlite3_snprintf(512-nCell,&zCell[nCell]," %f",(double)cell.aCoord[jj].f);
! 132733: nCell = strlen(zCell);
! 132734: }
! 132735:
! 132736: if( zText ){
! 132737: char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
! 132738: sqlite3_free(zText);
! 132739: zText = zTextNew;
! 132740: }else{
! 132741: zText = sqlite3_mprintf("{%s}", zCell);
! 132742: }
! 132743: }
! 132744:
! 132745: sqlite3_result_text(ctx, zText, -1, sqlite3_free);
! 132746: }
! 132747:
! 132748: static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
! 132749: UNUSED_PARAMETER(nArg);
! 132750: if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
! 132751: || sqlite3_value_bytes(apArg[0])<2
! 132752: ){
! 132753: sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
! 132754: }else{
! 132755: u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
! 132756: sqlite3_result_int(ctx, readInt16(zBlob));
! 132757: }
! 132758: }
! 132759:
! 132760: /*
! 132761: ** Register the r-tree module with database handle db. This creates the
! 132762: ** virtual table module "rtree" and the debugging/analysis scalar
! 132763: ** function "rtreenode".
! 132764: */
! 132765: SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
! 132766: const int utf8 = SQLITE_UTF8;
! 132767: int rc;
! 132768:
! 132769: rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
! 132770: if( rc==SQLITE_OK ){
! 132771: rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
! 132772: }
! 132773: if( rc==SQLITE_OK ){
! 132774: void *c = (void *)RTREE_COORD_REAL32;
! 132775: rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
! 132776: }
! 132777: if( rc==SQLITE_OK ){
! 132778: void *c = (void *)RTREE_COORD_INT32;
! 132779: rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
! 132780: }
! 132781:
! 132782: return rc;
! 132783: }
! 132784:
! 132785: /*
! 132786: ** A version of sqlite3_free() that can be used as a callback. This is used
! 132787: ** in two places - as the destructor for the blob value returned by the
! 132788: ** invocation of a geometry function, and as the destructor for the geometry
! 132789: ** functions themselves.
! 132790: */
! 132791: static void doSqlite3Free(void *p){
! 132792: sqlite3_free(p);
! 132793: }
! 132794:
! 132795: /*
! 132796: ** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite
! 132797: ** scalar user function. This C function is the callback used for all such
! 132798: ** registered SQL functions.
! 132799: **
! 132800: ** The scalar user functions return a blob that is interpreted by r-tree
! 132801: ** table MATCH operators.
! 132802: */
! 132803: static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
! 132804: RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
! 132805: RtreeMatchArg *pBlob;
! 132806: int nBlob;
! 132807:
! 132808: nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(double);
! 132809: pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
! 132810: if( !pBlob ){
! 132811: sqlite3_result_error_nomem(ctx);
! 132812: }else{
! 132813: int i;
! 132814: pBlob->magic = RTREE_GEOMETRY_MAGIC;
! 132815: pBlob->xGeom = pGeomCtx->xGeom;
! 132816: pBlob->pContext = pGeomCtx->pContext;
! 132817: pBlob->nParam = nArg;
! 132818: for(i=0; i<nArg; i++){
! 132819: pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
! 132820: }
! 132821: sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
! 132822: }
! 132823: }
! 132824:
! 132825: /*
! 132826: ** Register a new geometry function for use with the r-tree MATCH operator.
! 132827: */
! 132828: SQLITE_API int sqlite3_rtree_geometry_callback(
! 132829: sqlite3 *db,
! 132830: const char *zGeom,
! 132831: int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *),
! 132832: void *pContext
! 132833: ){
! 132834: RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
! 132835:
! 132836: /* Allocate and populate the context object. */
! 132837: pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
! 132838: if( !pGeomCtx ) return SQLITE_NOMEM;
! 132839: pGeomCtx->xGeom = xGeom;
! 132840: pGeomCtx->pContext = pContext;
! 132841:
! 132842: /* Create the new user-function. Register a destructor function to delete
! 132843: ** the context object when it is no longer required. */
! 132844: return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
! 132845: (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free
! 132846: );
! 132847: }
! 132848:
! 132849: #if !SQLITE_CORE
! 132850: SQLITE_API int sqlite3_extension_init(
! 132851: sqlite3 *db,
! 132852: char **pzErrMsg,
! 132853: const sqlite3_api_routines *pApi
! 132854: ){
! 132855: SQLITE_EXTENSION_INIT2(pApi)
! 132856: return sqlite3RtreeInit(db);
! 132857: }
! 132858: #endif
! 132859:
! 132860: #endif
! 132861:
! 132862: /************** End of rtree.c ***********************************************/
! 132863: /************** Begin file icu.c *********************************************/
! 132864: /*
! 132865: ** 2007 May 6
! 132866: **
! 132867: ** The author disclaims copyright to this source code. In place of
! 132868: ** a legal notice, here is a blessing:
! 132869: **
! 132870: ** May you do good and not evil.
! 132871: ** May you find forgiveness for yourself and forgive others.
! 132872: ** May you share freely, never taking more than you give.
! 132873: **
! 132874: *************************************************************************
! 132875: ** $Id: sqlite3.c,v 1.1.2.1 2012/04/08 16:31:40 misho Exp $
! 132876: **
! 132877: ** This file implements an integration between the ICU library
! 132878: ** ("International Components for Unicode", an open-source library
! 132879: ** for handling unicode data) and SQLite. The integration uses
! 132880: ** ICU to provide the following to SQLite:
! 132881: **
! 132882: ** * An implementation of the SQL regexp() function (and hence REGEXP
! 132883: ** operator) using the ICU uregex_XX() APIs.
! 132884: **
! 132885: ** * Implementations of the SQL scalar upper() and lower() functions
! 132886: ** for case mapping.
! 132887: **
! 132888: ** * Integration of ICU and SQLite collation seqences.
! 132889: **
! 132890: ** * An implementation of the LIKE operator that uses ICU to
! 132891: ** provide case-independent matching.
! 132892: */
! 132893:
! 132894: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
! 132895:
! 132896: /* Include ICU headers */
! 132897: #include <unicode/utypes.h>
! 132898: #include <unicode/uregex.h>
! 132899: #include <unicode/ustring.h>
! 132900: #include <unicode/ucol.h>
! 132901:
! 132902: /* #include <assert.h> */
! 132903:
! 132904: #ifndef SQLITE_CORE
! 132905: SQLITE_EXTENSION_INIT1
! 132906: #else
! 132907: #endif
! 132908:
! 132909: /*
! 132910: ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
! 132911: ** operator.
! 132912: */
! 132913: #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
! 132914: # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
! 132915: #endif
! 132916:
! 132917: /*
! 132918: ** Version of sqlite3_free() that is always a function, never a macro.
! 132919: */
! 132920: static void xFree(void *p){
! 132921: sqlite3_free(p);
! 132922: }
! 132923:
! 132924: /*
! 132925: ** Compare two UTF-8 strings for equality where the first string is
! 132926: ** a "LIKE" expression. Return true (1) if they are the same and
! 132927: ** false (0) if they are different.
! 132928: */
! 132929: static int icuLikeCompare(
! 132930: const uint8_t *zPattern, /* LIKE pattern */
! 132931: const uint8_t *zString, /* The UTF-8 string to compare against */
! 132932: const UChar32 uEsc /* The escape character */
! 132933: ){
! 132934: static const int MATCH_ONE = (UChar32)'_';
! 132935: static const int MATCH_ALL = (UChar32)'%';
! 132936:
! 132937: int iPattern = 0; /* Current byte index in zPattern */
! 132938: int iString = 0; /* Current byte index in zString */
! 132939:
! 132940: int prevEscape = 0; /* True if the previous character was uEsc */
! 132941:
! 132942: while( zPattern[iPattern]!=0 ){
! 132943:
! 132944: /* Read (and consume) the next character from the input pattern. */
! 132945: UChar32 uPattern;
! 132946: U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
! 132947: assert(uPattern!=0);
! 132948:
! 132949: /* There are now 4 possibilities:
! 132950: **
! 132951: ** 1. uPattern is an unescaped match-all character "%",
! 132952: ** 2. uPattern is an unescaped match-one character "_",
! 132953: ** 3. uPattern is an unescaped escape character, or
! 132954: ** 4. uPattern is to be handled as an ordinary character
! 132955: */
! 132956: if( !prevEscape && uPattern==MATCH_ALL ){
! 132957: /* Case 1. */
! 132958: uint8_t c;
! 132959:
! 132960: /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
! 132961: ** MATCH_ALL. For each MATCH_ONE, skip one character in the
! 132962: ** test string.
! 132963: */
! 132964: while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
! 132965: if( c==MATCH_ONE ){
! 132966: if( zString[iString]==0 ) return 0;
! 132967: U8_FWD_1_UNSAFE(zString, iString);
! 132968: }
! 132969: iPattern++;
! 132970: }
! 132971:
! 132972: if( zPattern[iPattern]==0 ) return 1;
! 132973:
! 132974: while( zString[iString] ){
! 132975: if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
! 132976: return 1;
! 132977: }
! 132978: U8_FWD_1_UNSAFE(zString, iString);
! 132979: }
! 132980: return 0;
! 132981:
! 132982: }else if( !prevEscape && uPattern==MATCH_ONE ){
! 132983: /* Case 2. */
! 132984: if( zString[iString]==0 ) return 0;
! 132985: U8_FWD_1_UNSAFE(zString, iString);
! 132986:
! 132987: }else if( !prevEscape && uPattern==uEsc){
! 132988: /* Case 3. */
! 132989: prevEscape = 1;
! 132990:
! 132991: }else{
! 132992: /* Case 4. */
! 132993: UChar32 uString;
! 132994: U8_NEXT_UNSAFE(zString, iString, uString);
! 132995: uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
! 132996: uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
! 132997: if( uString!=uPattern ){
! 132998: return 0;
! 132999: }
! 133000: prevEscape = 0;
! 133001: }
! 133002: }
! 133003:
! 133004: return zString[iString]==0;
! 133005: }
! 133006:
! 133007: /*
! 133008: ** Implementation of the like() SQL function. This function implements
! 133009: ** the build-in LIKE operator. The first argument to the function is the
! 133010: ** pattern and the second argument is the string. So, the SQL statements:
! 133011: **
! 133012: ** A LIKE B
! 133013: **
! 133014: ** is implemented as like(B, A). If there is an escape character E,
! 133015: **
! 133016: ** A LIKE B ESCAPE E
! 133017: **
! 133018: ** is mapped to like(B, A, E).
! 133019: */
! 133020: static void icuLikeFunc(
! 133021: sqlite3_context *context,
! 133022: int argc,
! 133023: sqlite3_value **argv
! 133024: ){
! 133025: const unsigned char *zA = sqlite3_value_text(argv[0]);
! 133026: const unsigned char *zB = sqlite3_value_text(argv[1]);
! 133027: UChar32 uEsc = 0;
! 133028:
! 133029: /* Limit the length of the LIKE or GLOB pattern to avoid problems
! 133030: ** of deep recursion and N*N behavior in patternCompare().
! 133031: */
! 133032: if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
! 133033: sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
! 133034: return;
! 133035: }
! 133036:
! 133037:
! 133038: if( argc==3 ){
! 133039: /* The escape character string must consist of a single UTF-8 character.
! 133040: ** Otherwise, return an error.
! 133041: */
! 133042: int nE= sqlite3_value_bytes(argv[2]);
! 133043: const unsigned char *zE = sqlite3_value_text(argv[2]);
! 133044: int i = 0;
! 133045: if( zE==0 ) return;
! 133046: U8_NEXT(zE, i, nE, uEsc);
! 133047: if( i!=nE){
! 133048: sqlite3_result_error(context,
! 133049: "ESCAPE expression must be a single character", -1);
! 133050: return;
! 133051: }
! 133052: }
! 133053:
! 133054: if( zA && zB ){
! 133055: sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
! 133056: }
! 133057: }
! 133058:
! 133059: /*
! 133060: ** This function is called when an ICU function called from within
! 133061: ** the implementation of an SQL scalar function returns an error.
! 133062: **
! 133063: ** The scalar function context passed as the first argument is
! 133064: ** loaded with an error message based on the following two args.
! 133065: */
! 133066: static void icuFunctionError(
! 133067: sqlite3_context *pCtx, /* SQLite scalar function context */
! 133068: const char *zName, /* Name of ICU function that failed */
! 133069: UErrorCode e /* Error code returned by ICU function */
! 133070: ){
! 133071: char zBuf[128];
! 133072: sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
! 133073: zBuf[127] = '\0';
! 133074: sqlite3_result_error(pCtx, zBuf, -1);
! 133075: }
! 133076:
! 133077: /*
! 133078: ** Function to delete compiled regexp objects. Registered as
! 133079: ** a destructor function with sqlite3_set_auxdata().
! 133080: */
! 133081: static void icuRegexpDelete(void *p){
! 133082: URegularExpression *pExpr = (URegularExpression *)p;
! 133083: uregex_close(pExpr);
! 133084: }
! 133085:
! 133086: /*
! 133087: ** Implementation of SQLite REGEXP operator. This scalar function takes
! 133088: ** two arguments. The first is a regular expression pattern to compile
! 133089: ** the second is a string to match against that pattern. If either
! 133090: ** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
! 133091: ** is 1 if the string matches the pattern, or 0 otherwise.
! 133092: **
! 133093: ** SQLite maps the regexp() function to the regexp() operator such
! 133094: ** that the following two are equivalent:
! 133095: **
! 133096: ** zString REGEXP zPattern
! 133097: ** regexp(zPattern, zString)
! 133098: **
! 133099: ** Uses the following ICU regexp APIs:
! 133100: **
! 133101: ** uregex_open()
! 133102: ** uregex_matches()
! 133103: ** uregex_close()
! 133104: */
! 133105: static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
! 133106: UErrorCode status = U_ZERO_ERROR;
! 133107: URegularExpression *pExpr;
! 133108: UBool res;
! 133109: const UChar *zString = sqlite3_value_text16(apArg[1]);
! 133110:
! 133111: (void)nArg; /* Unused parameter */
! 133112:
! 133113: /* If the left hand side of the regexp operator is NULL,
! 133114: ** then the result is also NULL.
! 133115: */
! 133116: if( !zString ){
! 133117: return;
! 133118: }
! 133119:
! 133120: pExpr = sqlite3_get_auxdata(p, 0);
! 133121: if( !pExpr ){
! 133122: const UChar *zPattern = sqlite3_value_text16(apArg[0]);
! 133123: if( !zPattern ){
! 133124: return;
! 133125: }
! 133126: pExpr = uregex_open(zPattern, -1, 0, 0, &status);
! 133127:
! 133128: if( U_SUCCESS(status) ){
! 133129: sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
! 133130: }else{
! 133131: assert(!pExpr);
! 133132: icuFunctionError(p, "uregex_open", status);
! 133133: return;
! 133134: }
! 133135: }
! 133136:
! 133137: /* Configure the text that the regular expression operates on. */
! 133138: uregex_setText(pExpr, zString, -1, &status);
! 133139: if( !U_SUCCESS(status) ){
! 133140: icuFunctionError(p, "uregex_setText", status);
! 133141: return;
! 133142: }
! 133143:
! 133144: /* Attempt the match */
! 133145: res = uregex_matches(pExpr, 0, &status);
! 133146: if( !U_SUCCESS(status) ){
! 133147: icuFunctionError(p, "uregex_matches", status);
! 133148: return;
! 133149: }
! 133150:
! 133151: /* Set the text that the regular expression operates on to a NULL
! 133152: ** pointer. This is not really necessary, but it is tidier than
! 133153: ** leaving the regular expression object configured with an invalid
! 133154: ** pointer after this function returns.
! 133155: */
! 133156: uregex_setText(pExpr, 0, 0, &status);
! 133157:
! 133158: /* Return 1 or 0. */
! 133159: sqlite3_result_int(p, res ? 1 : 0);
! 133160: }
! 133161:
! 133162: /*
! 133163: ** Implementations of scalar functions for case mapping - upper() and
! 133164: ** lower(). Function upper() converts its input to upper-case (ABC).
! 133165: ** Function lower() converts to lower-case (abc).
! 133166: **
! 133167: ** ICU provides two types of case mapping, "general" case mapping and
! 133168: ** "language specific". Refer to ICU documentation for the differences
! 133169: ** between the two.
! 133170: **
! 133171: ** To utilise "general" case mapping, the upper() or lower() scalar
! 133172: ** functions are invoked with one argument:
! 133173: **
! 133174: ** upper('ABC') -> 'abc'
! 133175: ** lower('abc') -> 'ABC'
! 133176: **
! 133177: ** To access ICU "language specific" case mapping, upper() or lower()
! 133178: ** should be invoked with two arguments. The second argument is the name
! 133179: ** of the locale to use. Passing an empty string ("") or SQL NULL value
! 133180: ** as the second argument is the same as invoking the 1 argument version
! 133181: ** of upper() or lower().
! 133182: **
! 133183: ** lower('I', 'en_us') -> 'i'
! 133184: ** lower('I', 'tr_tr') -> 'ı' (small dotless i)
! 133185: **
! 133186: ** http://www.icu-project.org/userguide/posix.html#case_mappings
! 133187: */
! 133188: static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
! 133189: const UChar *zInput;
! 133190: UChar *zOutput;
! 133191: int nInput;
! 133192: int nOutput;
! 133193:
! 133194: UErrorCode status = U_ZERO_ERROR;
! 133195: const char *zLocale = 0;
! 133196:
! 133197: assert(nArg==1 || nArg==2);
! 133198: if( nArg==2 ){
! 133199: zLocale = (const char *)sqlite3_value_text(apArg[1]);
! 133200: }
! 133201:
! 133202: zInput = sqlite3_value_text16(apArg[0]);
! 133203: if( !zInput ){
! 133204: return;
! 133205: }
! 133206: nInput = sqlite3_value_bytes16(apArg[0]);
! 133207:
! 133208: nOutput = nInput * 2 + 2;
! 133209: zOutput = sqlite3_malloc(nOutput);
! 133210: if( !zOutput ){
! 133211: return;
! 133212: }
! 133213:
! 133214: if( sqlite3_user_data(p) ){
! 133215: u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
! 133216: }else{
! 133217: u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
! 133218: }
! 133219:
! 133220: if( !U_SUCCESS(status) ){
! 133221: icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
! 133222: return;
! 133223: }
! 133224:
! 133225: sqlite3_result_text16(p, zOutput, -1, xFree);
! 133226: }
! 133227:
! 133228: /*
! 133229: ** Collation sequence destructor function. The pCtx argument points to
! 133230: ** a UCollator structure previously allocated using ucol_open().
! 133231: */
! 133232: static void icuCollationDel(void *pCtx){
! 133233: UCollator *p = (UCollator *)pCtx;
! 133234: ucol_close(p);
! 133235: }
! 133236:
! 133237: /*
! 133238: ** Collation sequence comparison function. The pCtx argument points to
! 133239: ** a UCollator structure previously allocated using ucol_open().
! 133240: */
! 133241: static int icuCollationColl(
! 133242: void *pCtx,
! 133243: int nLeft,
! 133244: const void *zLeft,
! 133245: int nRight,
! 133246: const void *zRight
! 133247: ){
! 133248: UCollationResult res;
! 133249: UCollator *p = (UCollator *)pCtx;
! 133250: res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
! 133251: switch( res ){
! 133252: case UCOL_LESS: return -1;
! 133253: case UCOL_GREATER: return +1;
! 133254: case UCOL_EQUAL: return 0;
! 133255: }
! 133256: assert(!"Unexpected return value from ucol_strcoll()");
! 133257: return 0;
! 133258: }
! 133259:
! 133260: /*
! 133261: ** Implementation of the scalar function icu_load_collation().
! 133262: **
! 133263: ** This scalar function is used to add ICU collation based collation
! 133264: ** types to an SQLite database connection. It is intended to be called
! 133265: ** as follows:
! 133266: **
! 133267: ** SELECT icu_load_collation(<locale>, <collation-name>);
! 133268: **
! 133269: ** Where <locale> is a string containing an ICU locale identifier (i.e.
! 133270: ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
! 133271: ** collation sequence to create.
! 133272: */
! 133273: static void icuLoadCollation(
! 133274: sqlite3_context *p,
! 133275: int nArg,
! 133276: sqlite3_value **apArg
! 133277: ){
! 133278: sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
! 133279: UErrorCode status = U_ZERO_ERROR;
! 133280: const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
! 133281: const char *zName; /* SQL Collation sequence name (eg. "japanese") */
! 133282: UCollator *pUCollator; /* ICU library collation object */
! 133283: int rc; /* Return code from sqlite3_create_collation_x() */
! 133284:
! 133285: assert(nArg==2);
! 133286: zLocale = (const char *)sqlite3_value_text(apArg[0]);
! 133287: zName = (const char *)sqlite3_value_text(apArg[1]);
! 133288:
! 133289: if( !zLocale || !zName ){
! 133290: return;
! 133291: }
! 133292:
! 133293: pUCollator = ucol_open(zLocale, &status);
! 133294: if( !U_SUCCESS(status) ){
! 133295: icuFunctionError(p, "ucol_open", status);
! 133296: return;
! 133297: }
! 133298: assert(p);
! 133299:
! 133300: rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
! 133301: icuCollationColl, icuCollationDel
! 133302: );
! 133303: if( rc!=SQLITE_OK ){
! 133304: ucol_close(pUCollator);
! 133305: sqlite3_result_error(p, "Error registering collation function", -1);
! 133306: }
! 133307: }
! 133308:
! 133309: /*
! 133310: ** Register the ICU extension functions with database db.
! 133311: */
! 133312: SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
! 133313: struct IcuScalar {
! 133314: const char *zName; /* Function name */
! 133315: int nArg; /* Number of arguments */
! 133316: int enc; /* Optimal text encoding */
! 133317: void *pContext; /* sqlite3_user_data() context */
! 133318: void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
! 133319: } scalars[] = {
! 133320: {"regexp", 2, SQLITE_ANY, 0, icuRegexpFunc},
! 133321:
! 133322: {"lower", 1, SQLITE_UTF16, 0, icuCaseFunc16},
! 133323: {"lower", 2, SQLITE_UTF16, 0, icuCaseFunc16},
! 133324: {"upper", 1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
! 133325: {"upper", 2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
! 133326:
! 133327: {"lower", 1, SQLITE_UTF8, 0, icuCaseFunc16},
! 133328: {"lower", 2, SQLITE_UTF8, 0, icuCaseFunc16},
! 133329: {"upper", 1, SQLITE_UTF8, (void*)1, icuCaseFunc16},
! 133330: {"upper", 2, SQLITE_UTF8, (void*)1, icuCaseFunc16},
! 133331:
! 133332: {"like", 2, SQLITE_UTF8, 0, icuLikeFunc},
! 133333: {"like", 3, SQLITE_UTF8, 0, icuLikeFunc},
! 133334:
! 133335: {"icu_load_collation", 2, SQLITE_UTF8, (void*)db, icuLoadCollation},
! 133336: };
! 133337:
! 133338: int rc = SQLITE_OK;
! 133339: int i;
! 133340:
! 133341: for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
! 133342: struct IcuScalar *p = &scalars[i];
! 133343: rc = sqlite3_create_function(
! 133344: db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
! 133345: );
! 133346: }
! 133347:
! 133348: return rc;
! 133349: }
! 133350:
! 133351: #if !SQLITE_CORE
! 133352: SQLITE_API int sqlite3_extension_init(
! 133353: sqlite3 *db,
! 133354: char **pzErrMsg,
! 133355: const sqlite3_api_routines *pApi
! 133356: ){
! 133357: SQLITE_EXTENSION_INIT2(pApi)
! 133358: return sqlite3IcuInit(db);
! 133359: }
! 133360: #endif
! 133361:
! 133362: #endif
! 133363:
! 133364: /************** End of icu.c *************************************************/
! 133365: /************** Begin file fts3_icu.c ****************************************/
! 133366: /*
! 133367: ** 2007 June 22
! 133368: **
! 133369: ** The author disclaims copyright to this source code. In place of
! 133370: ** a legal notice, here is a blessing:
! 133371: **
! 133372: ** May you do good and not evil.
! 133373: ** May you find forgiveness for yourself and forgive others.
! 133374: ** May you share freely, never taking more than you give.
! 133375: **
! 133376: *************************************************************************
! 133377: ** This file implements a tokenizer for fts3 based on the ICU library.
! 133378: */
! 133379: #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
! 133380: #ifdef SQLITE_ENABLE_ICU
! 133381:
! 133382: /* #include <assert.h> */
! 133383: /* #include <string.h> */
! 133384:
! 133385: #include <unicode/ubrk.h>
! 133386: /* #include <unicode/ucol.h> */
! 133387: /* #include <unicode/ustring.h> */
! 133388: #include <unicode/utf16.h>
! 133389:
! 133390: typedef struct IcuTokenizer IcuTokenizer;
! 133391: typedef struct IcuCursor IcuCursor;
! 133392:
! 133393: struct IcuTokenizer {
! 133394: sqlite3_tokenizer base;
! 133395: char *zLocale;
! 133396: };
! 133397:
! 133398: struct IcuCursor {
! 133399: sqlite3_tokenizer_cursor base;
! 133400:
! 133401: UBreakIterator *pIter; /* ICU break-iterator object */
! 133402: int nChar; /* Number of UChar elements in pInput */
! 133403: UChar *aChar; /* Copy of input using utf-16 encoding */
! 133404: int *aOffset; /* Offsets of each character in utf-8 input */
! 133405:
! 133406: int nBuffer;
! 133407: char *zBuffer;
! 133408:
! 133409: int iToken;
! 133410: };
! 133411:
! 133412: /*
! 133413: ** Create a new tokenizer instance.
! 133414: */
! 133415: static int icuCreate(
! 133416: int argc, /* Number of entries in argv[] */
! 133417: const char * const *argv, /* Tokenizer creation arguments */
! 133418: sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
! 133419: ){
! 133420: IcuTokenizer *p;
! 133421: int n = 0;
! 133422:
! 133423: if( argc>0 ){
! 133424: n = strlen(argv[0])+1;
! 133425: }
! 133426: p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
! 133427: if( !p ){
! 133428: return SQLITE_NOMEM;
! 133429: }
! 133430: memset(p, 0, sizeof(IcuTokenizer));
! 133431:
! 133432: if( n ){
! 133433: p->zLocale = (char *)&p[1];
! 133434: memcpy(p->zLocale, argv[0], n);
! 133435: }
! 133436:
! 133437: *ppTokenizer = (sqlite3_tokenizer *)p;
! 133438:
! 133439: return SQLITE_OK;
! 133440: }
! 133441:
! 133442: /*
! 133443: ** Destroy a tokenizer
! 133444: */
! 133445: static int icuDestroy(sqlite3_tokenizer *pTokenizer){
! 133446: IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
! 133447: sqlite3_free(p);
! 133448: return SQLITE_OK;
! 133449: }
! 133450:
! 133451: /*
! 133452: ** Prepare to begin tokenizing a particular string. The input
! 133453: ** string to be tokenized is pInput[0..nBytes-1]. A cursor
! 133454: ** used to incrementally tokenize this string is returned in
! 133455: ** *ppCursor.
! 133456: */
! 133457: static int icuOpen(
! 133458: sqlite3_tokenizer *pTokenizer, /* The tokenizer */
! 133459: const char *zInput, /* Input string */
! 133460: int nInput, /* Length of zInput in bytes */
! 133461: sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
! 133462: ){
! 133463: IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
! 133464: IcuCursor *pCsr;
! 133465:
! 133466: const int32_t opt = U_FOLD_CASE_DEFAULT;
! 133467: UErrorCode status = U_ZERO_ERROR;
! 133468: int nChar;
! 133469:
! 133470: UChar32 c;
! 133471: int iInput = 0;
! 133472: int iOut = 0;
! 133473:
! 133474: *ppCursor = 0;
! 133475:
! 133476: if( nInput<0 ){
! 133477: nInput = strlen(zInput);
! 133478: }
! 133479: nChar = nInput+1;
! 133480: pCsr = (IcuCursor *)sqlite3_malloc(
! 133481: sizeof(IcuCursor) + /* IcuCursor */
! 133482: nChar * sizeof(UChar) + /* IcuCursor.aChar[] */
! 133483: (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */
! 133484: );
! 133485: if( !pCsr ){
! 133486: return SQLITE_NOMEM;
! 133487: }
! 133488: memset(pCsr, 0, sizeof(IcuCursor));
! 133489: pCsr->aChar = (UChar *)&pCsr[1];
! 133490: pCsr->aOffset = (int *)&pCsr->aChar[nChar];
! 133491:
! 133492: pCsr->aOffset[iOut] = iInput;
! 133493: U8_NEXT(zInput, iInput, nInput, c);
! 133494: while( c>0 ){
! 133495: int isError = 0;
! 133496: c = u_foldCase(c, opt);
! 133497: U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
! 133498: if( isError ){
! 133499: sqlite3_free(pCsr);
! 133500: return SQLITE_ERROR;
! 133501: }
! 133502: pCsr->aOffset[iOut] = iInput;
! 133503:
! 133504: if( iInput<nInput ){
! 133505: U8_NEXT(zInput, iInput, nInput, c);
! 133506: }else{
! 133507: c = 0;
! 133508: }
! 133509: }
! 133510:
! 133511: pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
! 133512: if( !U_SUCCESS(status) ){
! 133513: sqlite3_free(pCsr);
! 133514: return SQLITE_ERROR;
! 133515: }
! 133516: pCsr->nChar = iOut;
! 133517:
! 133518: ubrk_first(pCsr->pIter);
! 133519: *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
! 133520: return SQLITE_OK;
! 133521: }
! 133522:
! 133523: /*
! 133524: ** Close a tokenization cursor previously opened by a call to icuOpen().
! 133525: */
! 133526: static int icuClose(sqlite3_tokenizer_cursor *pCursor){
! 133527: IcuCursor *pCsr = (IcuCursor *)pCursor;
! 133528: ubrk_close(pCsr->pIter);
! 133529: sqlite3_free(pCsr->zBuffer);
! 133530: sqlite3_free(pCsr);
! 133531: return SQLITE_OK;
! 133532: }
! 133533:
! 133534: /*
! 133535: ** Extract the next token from a tokenization cursor.
! 133536: */
! 133537: static int icuNext(
! 133538: sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
! 133539: const char **ppToken, /* OUT: *ppToken is the token text */
! 133540: int *pnBytes, /* OUT: Number of bytes in token */
! 133541: int *piStartOffset, /* OUT: Starting offset of token */
! 133542: int *piEndOffset, /* OUT: Ending offset of token */
! 133543: int *piPosition /* OUT: Position integer of token */
! 133544: ){
! 133545: IcuCursor *pCsr = (IcuCursor *)pCursor;
! 133546:
! 133547: int iStart = 0;
! 133548: int iEnd = 0;
! 133549: int nByte = 0;
! 133550:
! 133551: while( iStart==iEnd ){
! 133552: UChar32 c;
! 133553:
! 133554: iStart = ubrk_current(pCsr->pIter);
! 133555: iEnd = ubrk_next(pCsr->pIter);
! 133556: if( iEnd==UBRK_DONE ){
! 133557: return SQLITE_DONE;
! 133558: }
! 133559:
! 133560: while( iStart<iEnd ){
! 133561: int iWhite = iStart;
! 133562: U8_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
! 133563: if( u_isspace(c) ){
! 133564: iStart = iWhite;
! 133565: }else{
! 133566: break;
! 133567: }
! 133568: }
! 133569: assert(iStart<=iEnd);
! 133570: }
! 133571:
! 133572: do {
! 133573: UErrorCode status = U_ZERO_ERROR;
! 133574: if( nByte ){
! 133575: char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
! 133576: if( !zNew ){
! 133577: return SQLITE_NOMEM;
! 133578: }
! 133579: pCsr->zBuffer = zNew;
! 133580: pCsr->nBuffer = nByte;
! 133581: }
! 133582:
! 133583: u_strToUTF8(
! 133584: pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */
! 133585: &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */
! 133586: &status /* Output success/failure */
! 133587: );
! 133588: } while( nByte>pCsr->nBuffer );
! 133589:
! 133590: *ppToken = pCsr->zBuffer;
! 133591: *pnBytes = nByte;
! 133592: *piStartOffset = pCsr->aOffset[iStart];
! 133593: *piEndOffset = pCsr->aOffset[iEnd];
! 133594: *piPosition = pCsr->iToken++;
! 133595:
! 133596: return SQLITE_OK;
! 133597: }
! 133598:
! 133599: /*
! 133600: ** The set of routines that implement the simple tokenizer
! 133601: */
! 133602: static const sqlite3_tokenizer_module icuTokenizerModule = {
! 133603: 0, /* iVersion */
! 133604: icuCreate, /* xCreate */
! 133605: icuDestroy, /* xCreate */
! 133606: icuOpen, /* xOpen */
! 133607: icuClose, /* xClose */
! 133608: icuNext, /* xNext */
! 133609: };
! 133610:
! 133611: /*
! 133612: ** Set *ppModule to point at the implementation of the ICU tokenizer.
! 133613: */
! 133614: SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
! 133615: sqlite3_tokenizer_module const**ppModule
! 133616: ){
! 133617: *ppModule = &icuTokenizerModule;
! 133618: }
! 133619:
! 133620: #endif /* defined(SQLITE_ENABLE_ICU) */
! 133621: #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
! 133622:
! 133623: /************** End of fts3_icu.c ********************************************/
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>