| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.6.23.1. By combining all the individual C code files into this |
| 3 | +** version 3.6.23. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a one translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% are more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -321,11 +321,11 @@ |
| 321 | 321 | ** to generate appropriate macros for a wide range of compilers. |
| 322 | 322 | ** |
| 323 | 323 | ** The correct "ANSI" way to do this is to use the intptr_t type. |
| 324 | 324 | ** Unfortunately, that typedef is not available on all compilers, or |
| 325 | 325 | ** if it is available, it requires an #include of specific headers |
| 326 | | -** that very from one machine to the next. |
| 326 | +** that vary from one machine to the next. |
| 327 | 327 | ** |
| 328 | 328 | ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on |
| 329 | 329 | ** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)). |
| 330 | 330 | ** So we have to define the macros in different ways depending on the |
| 331 | 331 | ** compiler. |
| | @@ -626,13 +626,13 @@ |
| 626 | 626 | ** |
| 627 | 627 | ** See also: [sqlite3_libversion()], |
| 628 | 628 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 629 | 629 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 630 | 630 | */ |
| 631 | | -#define SQLITE_VERSION "3.6.23.1" |
| 631 | +#define SQLITE_VERSION "3.6.23" |
| 632 | 632 | #define SQLITE_VERSION_NUMBER 3006023 |
| 633 | | -#define SQLITE_SOURCE_ID "2010-03-26 22:28:06 ex-b078b588d617e07886ad156e9f54ade6d823568e" |
| 633 | +#define SQLITE_SOURCE_ID "2010-04-07 19:32:00 1f40441204d9a912b1d6b67ff6ff9e17146c7abd" |
| 634 | 634 | |
| 635 | 635 | /* |
| 636 | 636 | ** CAPI3REF: Run-Time Library Version Numbers |
| 637 | 637 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 638 | 638 | ** |
| | @@ -665,11 +665,10 @@ |
| 665 | 665 | SQLITE_API const char sqlite3_version[] = SQLITE_VERSION; |
| 666 | 666 | SQLITE_API const char *sqlite3_libversion(void); |
| 667 | 667 | SQLITE_API const char *sqlite3_sourceid(void); |
| 668 | 668 | SQLITE_API int sqlite3_libversion_number(void); |
| 669 | 669 | |
| 670 | | -#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 671 | 670 | /* |
| 672 | 671 | ** CAPI3REF: Run-Time Library Compilation Options Diagnostics |
| 673 | 672 | ** |
| 674 | 673 | ** ^The sqlite3_compileoption_used() function returns 0 or 1 |
| 675 | 674 | ** indicating whether the specified option was defined at |
| | @@ -688,13 +687,14 @@ |
| 688 | 687 | ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time. |
| 689 | 688 | ** |
| 690 | 689 | ** See also: SQL functions [sqlite_compileoption_used()] and |
| 691 | 690 | ** [sqlite_compileoption_get()] and the [compile_options pragma]. |
| 692 | 691 | */ |
| 692 | +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 693 | 693 | SQLITE_API int sqlite3_compileoption_used(const char *zOptName); |
| 694 | 694 | SQLITE_API const char *sqlite3_compileoption_get(int N); |
| 695 | | -#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 695 | +#endif |
| 696 | 696 | |
| 697 | 697 | /* |
| 698 | 698 | ** CAPI3REF: Test To See If The Library Is Threadsafe |
| 699 | 699 | ** |
| 700 | 700 | ** ^The sqlite3_threadsafe() function returns zero if and only if |
| | @@ -1492,15 +1492,14 @@ |
| 1492 | 1492 | ** |
| 1493 | 1493 | ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK]. |
| 1494 | 1494 | ** ^If the option is unknown or SQLite is unable to set the option |
| 1495 | 1495 | ** then this routine returns a non-zero [error code]. |
| 1496 | 1496 | */ |
| 1497 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_config(int, ...); |
| 1497 | +SQLITE_API int sqlite3_config(int, ...); |
| 1498 | 1498 | |
| 1499 | 1499 | /* |
| 1500 | 1500 | ** CAPI3REF: Configure database connections |
| 1501 | | -** EXPERIMENTAL |
| 1502 | 1501 | ** |
| 1503 | 1502 | ** The sqlite3_db_config() interface is used to make configuration |
| 1504 | 1503 | ** changes to a [database connection]. The interface is similar to |
| 1505 | 1504 | ** [sqlite3_config()] except that the changes apply to a single |
| 1506 | 1505 | ** [database connection] (specified in the first argument). The |
| | @@ -1516,15 +1515,14 @@ |
| 1516 | 1515 | ** Additional arguments depend on the verb. |
| 1517 | 1516 | ** |
| 1518 | 1517 | ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if |
| 1519 | 1518 | ** the call is considered successful. |
| 1520 | 1519 | */ |
| 1521 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_db_config(sqlite3*, int op, ...); |
| 1520 | +SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...); |
| 1522 | 1521 | |
| 1523 | 1522 | /* |
| 1524 | 1523 | ** CAPI3REF: Memory Allocation Routines |
| 1525 | | -** EXPERIMENTAL |
| 1526 | 1524 | ** |
| 1527 | 1525 | ** An instance of this object defines the interface between SQLite |
| 1528 | 1526 | ** and low-level memory allocation routines. |
| 1529 | 1527 | ** |
| 1530 | 1528 | ** This object is used in only one place in the SQLite interface. |
| | @@ -1602,11 +1600,10 @@ |
| 1602 | 1600 | void *pAppData; /* Argument to xInit() and xShutdown() */ |
| 1603 | 1601 | }; |
| 1604 | 1602 | |
| 1605 | 1603 | /* |
| 1606 | 1604 | ** CAPI3REF: Configuration Options |
| 1607 | | -** EXPERIMENTAL |
| 1608 | 1605 | ** |
| 1609 | 1606 | ** These constants are the available integer configuration options that |
| 1610 | 1607 | ** can be passed as the first argument to the [sqlite3_config()] interface. |
| 1611 | 1608 | ** |
| 1612 | 1609 | ** New configuration options may be added in future releases of SQLite. |
| | @@ -1788,10 +1785,28 @@ |
| 1788 | 1785 | ** <dt>SQLITE_CONFIG_GETPCACHE</dt> |
| 1789 | 1786 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1790 | 1787 | ** [sqlite3_pcache_methods] object. SQLite copies of the current |
| 1791 | 1788 | ** page cache implementation into that object.)^ </dd> |
| 1792 | 1789 | ** |
| 1790 | +** <dt>SQLITE_CONFIG_LOG</dt> |
| 1791 | +** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a |
| 1792 | +** function with a call signature of void(*)(void*,int,const char*), |
| 1793 | +** and a pointer to void. ^If the function pointer is not NULL, it is |
| 1794 | +** invoked by [sqlite3_log()] to process each logging event. ^If the |
| 1795 | +** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op. |
| 1796 | +** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is |
| 1797 | +** passed through as the first parameter to the application-defined logger |
| 1798 | +** function whenever that function is invoked. ^The second parameter to |
| 1799 | +** the logger function is a copy of the first parameter to the corresponding |
| 1800 | +** [sqlite3_log()] call and is intended to be a [result code] or an |
| 1801 | +** [extended result code]. ^The third parameter passed to the logger is |
| 1802 | +** log message after formatting via [sqlite3_snprintf()]. |
| 1803 | +** The SQLite logging interface is not reentrant; the logger function |
| 1804 | +** supplied by the application must not invoke any SQLite interface. |
| 1805 | +** In a multi-threaded application, the application-defined logger |
| 1806 | +** function must be threadsafe. </dd> |
| 1807 | +** |
| 1793 | 1808 | ** </dl> |
| 1794 | 1809 | */ |
| 1795 | 1810 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 1796 | 1811 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 1797 | 1812 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| | @@ -1808,12 +1823,11 @@ |
| 1808 | 1823 | #define SQLITE_CONFIG_PCACHE 14 /* sqlite3_pcache_methods* */ |
| 1809 | 1824 | #define SQLITE_CONFIG_GETPCACHE 15 /* sqlite3_pcache_methods* */ |
| 1810 | 1825 | #define SQLITE_CONFIG_LOG 16 /* xFunc, void* */ |
| 1811 | 1826 | |
| 1812 | 1827 | /* |
| 1813 | | -** CAPI3REF: Configuration Options |
| 1814 | | -** EXPERIMENTAL |
| 1828 | +** CAPI3REF: Database Connection Configuration Options |
| 1815 | 1829 | ** |
| 1816 | 1830 | ** These constants are the available integer configuration options that |
| 1817 | 1831 | ** can be passed as the second argument to the [sqlite3_db_config()] interface. |
| 1818 | 1832 | ** |
| 1819 | 1833 | ** New configuration options may be added in future releases of SQLite. |
| | @@ -2585,11 +2599,10 @@ |
| 2585 | 2599 | #define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */ |
| 2586 | 2600 | #define SQLITE_COPY 0 /* No longer used */ |
| 2587 | 2601 | |
| 2588 | 2602 | /* |
| 2589 | 2603 | ** CAPI3REF: Tracing And Profiling Functions |
| 2590 | | -** EXPERIMENTAL |
| 2591 | 2604 | ** |
| 2592 | 2605 | ** These routines register callback functions that can be used for |
| 2593 | 2606 | ** tracing and profiling the execution of SQL statements. |
| 2594 | 2607 | ** |
| 2595 | 2608 | ** ^The callback function registered by sqlite3_trace() is invoked at |
| | @@ -2603,11 +2616,11 @@ |
| 2603 | 2616 | ** ^The callback function registered by sqlite3_profile() is invoked |
| 2604 | 2617 | ** as each SQL statement finishes. ^The profile callback contains |
| 2605 | 2618 | ** the original statement text and an estimate of wall-clock time |
| 2606 | 2619 | ** of how long that statement took to run. |
| 2607 | 2620 | */ |
| 2608 | | -SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); |
| 2621 | +SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); |
| 2609 | 2622 | SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*, |
| 2610 | 2623 | void(*xProfile)(void*,const char*,sqlite3_uint64), void*); |
| 2611 | 2624 | |
| 2612 | 2625 | /* |
| 2613 | 2626 | ** CAPI3REF: Query Progress Callbacks |
| | @@ -4208,11 +4221,11 @@ |
| 4208 | 4221 | sqlite3*, |
| 4209 | 4222 | void*, |
| 4210 | 4223 | void(*)(void*,sqlite3*,int eTextRep,const void*) |
| 4211 | 4224 | ); |
| 4212 | 4225 | |
| 4213 | | -#if SQLITE_HAS_CODEC |
| 4226 | +#ifdef SQLITE_HAS_CODEC |
| 4214 | 4227 | /* |
| 4215 | 4228 | ** Specify the key for an encrypted database. This routine should be |
| 4216 | 4229 | ** called right after sqlite3_open(). |
| 4217 | 4230 | ** |
| 4218 | 4231 | ** The code to implement this API is not available in the public release |
| | @@ -4678,12 +4691,10 @@ |
| 4678 | 4691 | ** ^This function disables automatic extensions in all threads. |
| 4679 | 4692 | */ |
| 4680 | 4693 | SQLITE_API void sqlite3_reset_auto_extension(void); |
| 4681 | 4694 | |
| 4682 | 4695 | /* |
| 4683 | | -****** EXPERIMENTAL - subject to change without notice ************** |
| 4684 | | -** |
| 4685 | 4696 | ** The interface to the virtual-table mechanism is currently considered |
| 4686 | 4697 | ** to be experimental. The interface might change in incompatible ways. |
| 4687 | 4698 | ** If this is a problem for you, do not use the interface at this time. |
| 4688 | 4699 | ** |
| 4689 | 4700 | ** When the virtual-table mechanism stabilizes, we will declare the |
| | @@ -4699,11 +4710,10 @@ |
| 4699 | 4710 | typedef struct sqlite3_module sqlite3_module; |
| 4700 | 4711 | |
| 4701 | 4712 | /* |
| 4702 | 4713 | ** CAPI3REF: Virtual Table Object |
| 4703 | 4714 | ** KEYWORDS: sqlite3_module {virtual table module} |
| 4704 | | -** EXPERIMENTAL |
| 4705 | 4715 | ** |
| 4706 | 4716 | ** This structure, sometimes called a a "virtual table module", |
| 4707 | 4717 | ** defines the implementation of a [virtual tables]. |
| 4708 | 4718 | ** This structure consists mostly of methods for the module. |
| 4709 | 4719 | ** |
| | @@ -4746,11 +4756,10 @@ |
| 4746 | 4756 | }; |
| 4747 | 4757 | |
| 4748 | 4758 | /* |
| 4749 | 4759 | ** CAPI3REF: Virtual Table Indexing Information |
| 4750 | 4760 | ** KEYWORDS: sqlite3_index_info |
| 4751 | | -** EXPERIMENTAL |
| 4752 | 4761 | ** |
| 4753 | 4762 | ** The sqlite3_index_info structure and its substructures is used to |
| 4754 | 4763 | ** pass information into and receive the reply from the [xBestIndex] |
| 4755 | 4764 | ** method of a [virtual table module]. The fields under **Inputs** are the |
| 4756 | 4765 | ** inputs to xBestIndex and are read-only. xBestIndex inserts its |
| | @@ -4828,11 +4837,10 @@ |
| 4828 | 4837 | #define SQLITE_INDEX_CONSTRAINT_GE 32 |
| 4829 | 4838 | #define SQLITE_INDEX_CONSTRAINT_MATCH 64 |
| 4830 | 4839 | |
| 4831 | 4840 | /* |
| 4832 | 4841 | ** CAPI3REF: Register A Virtual Table Implementation |
| 4833 | | -** EXPERIMENTAL |
| 4834 | 4842 | ** |
| 4835 | 4843 | ** ^These routines are used to register a new [virtual table module] name. |
| 4836 | 4844 | ** ^Module names must be registered before |
| 4837 | 4845 | ** creating a new [virtual table] using the module and before using a |
| 4838 | 4846 | ** preexisting [virtual table] for the module. |
| | @@ -4850,17 +4858,17 @@ |
| 4850 | 4858 | ** invoke the destructor function (if it is not NULL) when SQLite |
| 4851 | 4859 | ** no longer needs the pClientData pointer. ^The sqlite3_create_module() |
| 4852 | 4860 | ** interface is equivalent to sqlite3_create_module_v2() with a NULL |
| 4853 | 4861 | ** destructor. |
| 4854 | 4862 | */ |
| 4855 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_create_module( |
| 4863 | +SQLITE_API int sqlite3_create_module( |
| 4856 | 4864 | sqlite3 *db, /* SQLite connection to register module with */ |
| 4857 | 4865 | const char *zName, /* Name of the module */ |
| 4858 | 4866 | const sqlite3_module *p, /* Methods for the module */ |
| 4859 | 4867 | void *pClientData /* Client data for xCreate/xConnect */ |
| 4860 | 4868 | ); |
| 4861 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_create_module_v2( |
| 4869 | +SQLITE_API int sqlite3_create_module_v2( |
| 4862 | 4870 | sqlite3 *db, /* SQLite connection to register module with */ |
| 4863 | 4871 | const char *zName, /* Name of the module */ |
| 4864 | 4872 | const sqlite3_module *p, /* Methods for the module */ |
| 4865 | 4873 | void *pClientData, /* Client data for xCreate/xConnect */ |
| 4866 | 4874 | void(*xDestroy)(void*) /* Module destructor function */ |
| | @@ -4867,11 +4875,10 @@ |
| 4867 | 4875 | ); |
| 4868 | 4876 | |
| 4869 | 4877 | /* |
| 4870 | 4878 | ** CAPI3REF: Virtual Table Instance Object |
| 4871 | 4879 | ** KEYWORDS: sqlite3_vtab |
| 4872 | | -** EXPERIMENTAL |
| 4873 | 4880 | ** |
| 4874 | 4881 | ** Every [virtual table module] implementation uses a subclass |
| 4875 | 4882 | ** of this object to describe a particular instance |
| 4876 | 4883 | ** of the [virtual table]. Each subclass will |
| 4877 | 4884 | ** be tailored to the specific needs of the module implementation. |
| | @@ -4893,11 +4900,10 @@ |
| 4893 | 4900 | }; |
| 4894 | 4901 | |
| 4895 | 4902 | /* |
| 4896 | 4903 | ** CAPI3REF: Virtual Table Cursor Object |
| 4897 | 4904 | ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor} |
| 4898 | | -** EXPERIMENTAL |
| 4899 | 4905 | ** |
| 4900 | 4906 | ** Every [virtual table module] implementation uses a subclass of the |
| 4901 | 4907 | ** following structure to describe cursors that point into the |
| 4902 | 4908 | ** [virtual table] and are used |
| 4903 | 4909 | ** to loop through the virtual table. Cursors are created using the |
| | @@ -4915,22 +4921,20 @@ |
| 4915 | 4921 | /* Virtual table implementations will typically add additional fields */ |
| 4916 | 4922 | }; |
| 4917 | 4923 | |
| 4918 | 4924 | /* |
| 4919 | 4925 | ** CAPI3REF: Declare The Schema Of A Virtual Table |
| 4920 | | -** EXPERIMENTAL |
| 4921 | 4926 | ** |
| 4922 | 4927 | ** ^The [xCreate] and [xConnect] methods of a |
| 4923 | 4928 | ** [virtual table module] call this interface |
| 4924 | 4929 | ** to declare the format (the names and datatypes of the columns) of |
| 4925 | 4930 | ** the virtual tables they implement. |
| 4926 | 4931 | */ |
| 4927 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_declare_vtab(sqlite3*, const char *zSQL); |
| 4932 | +SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL); |
| 4928 | 4933 | |
| 4929 | 4934 | /* |
| 4930 | 4935 | ** CAPI3REF: Overload A Function For A Virtual Table |
| 4931 | | -** EXPERIMENTAL |
| 4932 | 4936 | ** |
| 4933 | 4937 | ** ^(Virtual tables can provide alternative implementations of functions |
| 4934 | 4938 | ** using the [xFindFunction] method of the [virtual table module]. |
| 4935 | 4939 | ** But global versions of those functions |
| 4936 | 4940 | ** must exist in order to be overloaded.)^ |
| | @@ -4941,22 +4945,20 @@ |
| 4941 | 4945 | ** of the new function always causes an exception to be thrown. So |
| 4942 | 4946 | ** the new function is not good for anything by itself. Its only |
| 4943 | 4947 | ** purpose is to be a placeholder function that can be overloaded |
| 4944 | 4948 | ** by a [virtual table]. |
| 4945 | 4949 | */ |
| 4946 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); |
| 4950 | +SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); |
| 4947 | 4951 | |
| 4948 | 4952 | /* |
| 4949 | 4953 | ** The interface to the virtual-table mechanism defined above (back up |
| 4950 | 4954 | ** to a comment remarkably similar to this one) is currently considered |
| 4951 | 4955 | ** to be experimental. The interface might change in incompatible ways. |
| 4952 | 4956 | ** If this is a problem for you, do not use the interface at this time. |
| 4953 | 4957 | ** |
| 4954 | 4958 | ** When the virtual-table mechanism stabilizes, we will declare the |
| 4955 | 4959 | ** interface fixed, support it indefinitely, and remove this comment. |
| 4956 | | -** |
| 4957 | | -****** EXPERIMENTAL - subject to change without notice ************** |
| 4958 | 4960 | */ |
| 4959 | 4961 | |
| 4960 | 4962 | /* |
| 4961 | 4963 | ** CAPI3REF: A Handle To An Open BLOB |
| 4962 | 4964 | ** KEYWORDS: {BLOB handle} {BLOB handles} |
| | @@ -5295,11 +5297,10 @@ |
| 5295 | 5297 | SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*); |
| 5296 | 5298 | SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*); |
| 5297 | 5299 | |
| 5298 | 5300 | /* |
| 5299 | 5301 | ** CAPI3REF: Mutex Methods Object |
| 5300 | | -** EXPERIMENTAL |
| 5301 | 5302 | ** |
| 5302 | 5303 | ** An instance of this structure defines the low-level routines |
| 5303 | 5304 | ** used to allocate and use mutexes. |
| 5304 | 5305 | ** |
| 5305 | 5306 | ** Usually, the default mutex implementations provided by SQLite are |
| | @@ -5512,11 +5513,10 @@ |
| 5512 | 5513 | #define SQLITE_TESTCTRL_ISKEYWORD 16 |
| 5513 | 5514 | #define SQLITE_TESTCTRL_LAST 16 |
| 5514 | 5515 | |
| 5515 | 5516 | /* |
| 5516 | 5517 | ** CAPI3REF: SQLite Runtime Status |
| 5517 | | -** EXPERIMENTAL |
| 5518 | 5518 | ** |
| 5519 | 5519 | ** ^This interface is used to retrieve runtime status information |
| 5520 | 5520 | ** about the preformance of SQLite, and optionally to reset various |
| 5521 | 5521 | ** highwater marks. ^The first argument is an integer code for |
| 5522 | 5522 | ** the specific parameter to measure. ^(Recognized integer codes |
| | @@ -5540,16 +5540,15 @@ |
| 5540 | 5540 | ** and it is possible that another thread might change the parameter |
| 5541 | 5541 | ** in between the times when *pCurrent and *pHighwater are written. |
| 5542 | 5542 | ** |
| 5543 | 5543 | ** See also: [sqlite3_db_status()] |
| 5544 | 5544 | */ |
| 5545 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag); |
| 5545 | +SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag); |
| 5546 | 5546 | |
| 5547 | 5547 | |
| 5548 | 5548 | /* |
| 5549 | 5549 | ** CAPI3REF: Status Parameters |
| 5550 | | -** EXPERIMENTAL |
| 5551 | 5550 | ** |
| 5552 | 5551 | ** These integer constants designate various run-time status parameters |
| 5553 | 5552 | ** that can be returned by [sqlite3_status()]. |
| 5554 | 5553 | ** |
| 5555 | 5554 | ** <dl> |
| | @@ -5632,31 +5631,31 @@ |
| 5632 | 5631 | #define SQLITE_STATUS_PAGECACHE_SIZE 7 |
| 5633 | 5632 | #define SQLITE_STATUS_SCRATCH_SIZE 8 |
| 5634 | 5633 | |
| 5635 | 5634 | /* |
| 5636 | 5635 | ** CAPI3REF: Database Connection Status |
| 5637 | | -** EXPERIMENTAL |
| 5638 | 5636 | ** |
| 5639 | 5637 | ** ^This interface is used to retrieve runtime status information |
| 5640 | 5638 | ** about a single [database connection]. ^The first argument is the |
| 5641 | 5639 | ** database connection object to be interrogated. ^The second argument |
| 5642 | | -** is the parameter to interrogate. ^Currently, the only allowed value |
| 5643 | | -** for the second parameter is [SQLITE_DBSTATUS_LOOKASIDE_USED]. |
| 5644 | | -** Additional options will likely appear in future releases of SQLite. |
| 5640 | +** is an integer constant, taken from the set of |
| 5641 | +** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros, that |
| 5642 | +** determiness the parameter to interrogate. The set of |
| 5643 | +** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely |
| 5644 | +** to grow in future releases of SQLite. |
| 5645 | 5645 | ** |
| 5646 | 5646 | ** ^The current value of the requested parameter is written into *pCur |
| 5647 | 5647 | ** and the highest instantaneous value is written into *pHiwtr. ^If |
| 5648 | 5648 | ** the resetFlg is true, then the highest instantaneous value is |
| 5649 | 5649 | ** reset back down to the current value. |
| 5650 | 5650 | ** |
| 5651 | 5651 | ** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. |
| 5652 | 5652 | */ |
| 5653 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); |
| 5653 | +SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); |
| 5654 | 5654 | |
| 5655 | 5655 | /* |
| 5656 | 5656 | ** CAPI3REF: Status Parameters for database connections |
| 5657 | | -** EXPERIMENTAL |
| 5658 | 5657 | ** |
| 5659 | 5658 | ** These constants are the available integer "verbs" that can be passed as |
| 5660 | 5659 | ** the second argument to the [sqlite3_db_status()] interface. |
| 5661 | 5660 | ** |
| 5662 | 5661 | ** New verbs may be added in future releases of SQLite. Existing verbs |
| | @@ -5667,18 +5666,25 @@ |
| 5667 | 5666 | ** |
| 5668 | 5667 | ** <dl> |
| 5669 | 5668 | ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt> |
| 5670 | 5669 | ** <dd>This parameter returns the number of lookaside memory slots currently |
| 5671 | 5670 | ** checked out.</dd>)^ |
| 5671 | +** |
| 5672 | +** <dt>SQLITE_DBSTATUS_CACHE_USED</dt> |
| 5673 | +** <dd>^This parameter returns the approximate number of of bytes of heap |
| 5674 | +** memory used by all pager caches associated with the database connection. |
| 5675 | +** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0. |
| 5676 | +** checked out.</dd>)^ |
| 5672 | 5677 | ** </dl> |
| 5673 | 5678 | */ |
| 5674 | 5679 | #define SQLITE_DBSTATUS_LOOKASIDE_USED 0 |
| 5680 | +#define SQLITE_DBSTATUS_CACHE_USED 1 |
| 5681 | +#define SQLITE_DBSTATUS_MAX 1 /* Largest defined DBSTATUS */ |
| 5675 | 5682 | |
| 5676 | 5683 | |
| 5677 | 5684 | /* |
| 5678 | 5685 | ** CAPI3REF: Prepared Statement Status |
| 5679 | | -** EXPERIMENTAL |
| 5680 | 5686 | ** |
| 5681 | 5687 | ** ^(Each prepared statement maintains various |
| 5682 | 5688 | ** [SQLITE_STMTSTATUS_SORT | counters] that measure the number |
| 5683 | 5689 | ** of times it has performed specific operations.)^ These counters can |
| 5684 | 5690 | ** be used to monitor the performance characteristics of the prepared |
| | @@ -5696,15 +5702,14 @@ |
| 5696 | 5702 | ** ^If the resetFlg is true, then the counter is reset to zero after this |
| 5697 | 5703 | ** interface call returns. |
| 5698 | 5704 | ** |
| 5699 | 5705 | ** See also: [sqlite3_status()] and [sqlite3_db_status()]. |
| 5700 | 5706 | */ |
| 5701 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); |
| 5707 | +SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); |
| 5702 | 5708 | |
| 5703 | 5709 | /* |
| 5704 | 5710 | ** CAPI3REF: Status Parameters for prepared statements |
| 5705 | | -** EXPERIMENTAL |
| 5706 | 5711 | ** |
| 5707 | 5712 | ** These preprocessor macros define integer codes that name counter |
| 5708 | 5713 | ** values associated with the [sqlite3_stmt_status()] interface. |
| 5709 | 5714 | ** The meanings of the various counters are as follows: |
| 5710 | 5715 | ** |
| | @@ -5718,18 +5723,25 @@ |
| 5718 | 5723 | ** <dt>SQLITE_STMTSTATUS_SORT</dt> |
| 5719 | 5724 | ** <dd>^This is the number of sort operations that have occurred. |
| 5720 | 5725 | ** A non-zero value in this counter may indicate an opportunity to |
| 5721 | 5726 | ** improvement performance through careful use of indices.</dd> |
| 5722 | 5727 | ** |
| 5728 | +** <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt> |
| 5729 | +** <dd>^This is the number of rows inserted into transient indices that |
| 5730 | +** were created automatically in order to help joins run faster. |
| 5731 | +** A non-zero value in this counter may indicate an opportunity to |
| 5732 | +** improvement performance by adding permanent indices that do not |
| 5733 | +** need to be reinitialized each time the statement is run.</dd> |
| 5734 | +** |
| 5723 | 5735 | ** </dl> |
| 5724 | 5736 | */ |
| 5725 | 5737 | #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1 |
| 5726 | 5738 | #define SQLITE_STMTSTATUS_SORT 2 |
| 5739 | +#define SQLITE_STMTSTATUS_AUTOINDEX 3 |
| 5727 | 5740 | |
| 5728 | 5741 | /* |
| 5729 | 5742 | ** CAPI3REF: Custom Page Cache Object |
| 5730 | | -** EXPERIMENTAL |
| 5731 | 5743 | ** |
| 5732 | 5744 | ** The sqlite3_pcache type is opaque. It is implemented by |
| 5733 | 5745 | ** the pluggable module. The SQLite core has no knowledge of |
| 5734 | 5746 | ** its size or internal structure and never deals with the |
| 5735 | 5747 | ** sqlite3_pcache object except by holding and passing pointers |
| | @@ -5740,11 +5752,10 @@ |
| 5740 | 5752 | typedef struct sqlite3_pcache sqlite3_pcache; |
| 5741 | 5753 | |
| 5742 | 5754 | /* |
| 5743 | 5755 | ** CAPI3REF: Application Defined Page Cache. |
| 5744 | 5756 | ** KEYWORDS: {page cache} |
| 5745 | | -** EXPERIMENTAL |
| 5746 | 5757 | ** |
| 5747 | 5758 | ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can |
| 5748 | 5759 | ** register an alternative page cache implementation by passing in an |
| 5749 | 5760 | ** instance of the sqlite3_pcache_methods structure.)^ The majority of the |
| 5750 | 5761 | ** heap memory used by SQLite is used by the page cache to cache data read |
| | @@ -5882,11 +5893,10 @@ |
| 5882 | 5893 | void (*xDestroy)(sqlite3_pcache*); |
| 5883 | 5894 | }; |
| 5884 | 5895 | |
| 5885 | 5896 | /* |
| 5886 | 5897 | ** CAPI3REF: Online Backup Object |
| 5887 | | -** EXPERIMENTAL |
| 5888 | 5898 | ** |
| 5889 | 5899 | ** The sqlite3_backup object records state information about an ongoing |
| 5890 | 5900 | ** online backup operation. ^The sqlite3_backup object is created by |
| 5891 | 5901 | ** a call to [sqlite3_backup_init()] and is destroyed by a call to |
| 5892 | 5902 | ** [sqlite3_backup_finish()]. |
| | @@ -5895,11 +5905,10 @@ |
| 5895 | 5905 | */ |
| 5896 | 5906 | typedef struct sqlite3_backup sqlite3_backup; |
| 5897 | 5907 | |
| 5898 | 5908 | /* |
| 5899 | 5909 | ** CAPI3REF: Online Backup API. |
| 5900 | | -** EXPERIMENTAL |
| 5901 | 5910 | ** |
| 5902 | 5911 | ** The backup API copies the content of one database into another. |
| 5903 | 5912 | ** It is useful either for creating backups of databases or |
| 5904 | 5913 | ** for copying in-memory databases to or from persistent files. |
| 5905 | 5914 | ** |
| | @@ -6083,11 +6092,10 @@ |
| 6083 | 6092 | SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p); |
| 6084 | 6093 | SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p); |
| 6085 | 6094 | |
| 6086 | 6095 | /* |
| 6087 | 6096 | ** CAPI3REF: Unlock Notification |
| 6088 | | -** EXPERIMENTAL |
| 6089 | 6097 | ** |
| 6090 | 6098 | ** ^When running in shared-cache mode, a database operation may fail with |
| 6091 | 6099 | ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or |
| 6092 | 6100 | ** individual tables within the shared-cache cannot be obtained. See |
| 6093 | 6101 | ** [SQLite Shared-Cache Mode] for a description of shared-cache locking. |
| | @@ -6205,11 +6213,10 @@ |
| 6205 | 6213 | ); |
| 6206 | 6214 | |
| 6207 | 6215 | |
| 6208 | 6216 | /* |
| 6209 | 6217 | ** CAPI3REF: String Comparison |
| 6210 | | -** EXPERIMENTAL |
| 6211 | 6218 | ** |
| 6212 | 6219 | ** ^The [sqlite3_strnicmp()] API allows applications and extensions to |
| 6213 | 6220 | ** compare the contents of two buffers containing UTF-8 strings in a |
| 6214 | 6221 | ** case-indendent fashion, using the same definition of case independence |
| 6215 | 6222 | ** that SQLite uses internally when comparing identifiers. |
| | @@ -6216,16 +6223,15 @@ |
| 6216 | 6223 | */ |
| 6217 | 6224 | SQLITE_API int sqlite3_strnicmp(const char *, const char *, int); |
| 6218 | 6225 | |
| 6219 | 6226 | /* |
| 6220 | 6227 | ** CAPI3REF: Error Logging Interface |
| 6221 | | -** EXPERIMENTAL |
| 6222 | 6228 | ** |
| 6223 | 6229 | ** ^The [sqlite3_log()] interface writes a message into the error log |
| 6224 | 6230 | ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()]. |
| 6225 | 6231 | ** ^If logging is enabled, the zFormat string and subsequent arguments are |
| 6226 | | -** passed through to [sqlite3_vmprintf()] to generate the final output string. |
| 6232 | +** used with [sqlite3_snprintf()] to generate the final output string. |
| 6227 | 6233 | ** |
| 6228 | 6234 | ** The sqlite3_log() interface is intended for use by extensions such as |
| 6229 | 6235 | ** virtual tables, collating functions, and SQL functions. While there is |
| 6230 | 6236 | ** nothing to prevent an application from calling sqlite3_log(), doing so |
| 6231 | 6237 | ** is considered bad form. |
| | @@ -6529,10 +6535,11 @@ |
| 6529 | 6535 | ** If compiling for a processor that lacks floating point support, |
| 6530 | 6536 | ** substitute integer for floating-point |
| 6531 | 6537 | */ |
| 6532 | 6538 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 6533 | 6539 | # define double sqlite_int64 |
| 6540 | +# define float sqlite_int64 |
| 6534 | 6541 | # define LONGDOUBLE_TYPE sqlite_int64 |
| 6535 | 6542 | # ifndef SQLITE_BIG_DBL |
| 6536 | 6543 | # define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50) |
| 6537 | 6544 | # endif |
| 6538 | 6545 | # define SQLITE_OMIT_DATETIME_FUNCS 1 |
| | @@ -6940,10 +6947,11 @@ |
| 6940 | 6947 | SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int); |
| 6941 | 6948 | SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*); |
| 6942 | 6949 | SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix); |
| 6943 | 6950 | SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*); |
| 6944 | 6951 | SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int); |
| 6952 | +SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*); |
| 6945 | 6953 | SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int); |
| 6946 | 6954 | SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*); |
| 6947 | 6955 | SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int); |
| 6948 | 6956 | SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *); |
| 6949 | 6957 | SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int); |
| | @@ -7334,85 +7342,85 @@ |
| 7334 | 7342 | #define OP_ReadCookie 35 |
| 7335 | 7343 | #define OP_SetCookie 36 |
| 7336 | 7344 | #define OP_VerifyCookie 37 |
| 7337 | 7345 | #define OP_OpenRead 38 |
| 7338 | 7346 | #define OP_OpenWrite 39 |
| 7339 | | -#define OP_OpenEphemeral 40 |
| 7340 | | -#define OP_OpenPseudo 41 |
| 7341 | | -#define OP_Close 42 |
| 7342 | | -#define OP_SeekLt 43 |
| 7343 | | -#define OP_SeekLe 44 |
| 7344 | | -#define OP_SeekGe 45 |
| 7345 | | -#define OP_SeekGt 46 |
| 7346 | | -#define OP_Seek 47 |
| 7347 | | -#define OP_NotFound 48 |
| 7348 | | -#define OP_Found 49 |
| 7349 | | -#define OP_IsUnique 50 |
| 7350 | | -#define OP_NotExists 51 |
| 7351 | | -#define OP_Sequence 52 |
| 7352 | | -#define OP_NewRowid 53 |
| 7353 | | -#define OP_Insert 54 |
| 7354 | | -#define OP_InsertInt 55 |
| 7355 | | -#define OP_Delete 56 |
| 7356 | | -#define OP_ResetCount 57 |
| 7357 | | -#define OP_RowKey 58 |
| 7358 | | -#define OP_RowData 59 |
| 7359 | | -#define OP_Rowid 60 |
| 7360 | | -#define OP_NullRow 61 |
| 7361 | | -#define OP_Last 62 |
| 7362 | | -#define OP_Sort 63 |
| 7363 | | -#define OP_Rewind 64 |
| 7364 | | -#define OP_Prev 65 |
| 7365 | | -#define OP_Next 66 |
| 7366 | | -#define OP_IdxInsert 67 |
| 7367 | | -#define OP_IdxDelete 70 |
| 7368 | | -#define OP_IdxRowid 71 |
| 7369 | | -#define OP_IdxLT 72 |
| 7370 | | -#define OP_IdxGE 81 |
| 7371 | | -#define OP_Destroy 92 |
| 7372 | | -#define OP_Clear 95 |
| 7373 | | -#define OP_CreateIndex 96 |
| 7374 | | -#define OP_CreateTable 97 |
| 7375 | | -#define OP_ParseSchema 98 |
| 7376 | | -#define OP_LoadAnalysis 99 |
| 7377 | | -#define OP_DropTable 100 |
| 7378 | | -#define OP_DropIndex 101 |
| 7379 | | -#define OP_DropTrigger 102 |
| 7380 | | -#define OP_IntegrityCk 103 |
| 7381 | | -#define OP_RowSetAdd 104 |
| 7382 | | -#define OP_RowSetRead 105 |
| 7383 | | -#define OP_RowSetTest 106 |
| 7384 | | -#define OP_Program 107 |
| 7385 | | -#define OP_Param 108 |
| 7386 | | -#define OP_FkCounter 109 |
| 7387 | | -#define OP_FkIfZero 110 |
| 7388 | | -#define OP_MemMax 111 |
| 7389 | | -#define OP_IfPos 112 |
| 7390 | | -#define OP_IfNeg 113 |
| 7391 | | -#define OP_IfZero 114 |
| 7392 | | -#define OP_AggStep 115 |
| 7393 | | -#define OP_AggFinal 116 |
| 7394 | | -#define OP_Vacuum 117 |
| 7395 | | -#define OP_IncrVacuum 118 |
| 7396 | | -#define OP_Expire 119 |
| 7397 | | -#define OP_TableLock 120 |
| 7398 | | -#define OP_VBegin 121 |
| 7399 | | -#define OP_VCreate 122 |
| 7400 | | -#define OP_VDestroy 123 |
| 7401 | | -#define OP_VOpen 124 |
| 7402 | | -#define OP_VFilter 125 |
| 7403 | | -#define OP_VColumn 126 |
| 7404 | | -#define OP_VNext 127 |
| 7405 | | -#define OP_VRename 128 |
| 7406 | | -#define OP_VUpdate 129 |
| 7407 | | -#define OP_Pagecount 131 |
| 7408 | | -#define OP_Trace 132 |
| 7409 | | -#define OP_Noop 133 |
| 7410 | | -#define OP_Explain 134 |
| 7411 | | - |
| 7412 | | -/* The following opcode values are never used */ |
| 7413 | | -#define OP_NotUsed_135 135 |
| 7347 | +#define OP_OpenAutoindex 40 |
| 7348 | +#define OP_OpenEphemeral 41 |
| 7349 | +#define OP_OpenPseudo 42 |
| 7350 | +#define OP_Close 43 |
| 7351 | +#define OP_SeekLt 44 |
| 7352 | +#define OP_SeekLe 45 |
| 7353 | +#define OP_SeekGe 46 |
| 7354 | +#define OP_SeekGt 47 |
| 7355 | +#define OP_Seek 48 |
| 7356 | +#define OP_NotFound 49 |
| 7357 | +#define OP_Found 50 |
| 7358 | +#define OP_IsUnique 51 |
| 7359 | +#define OP_NotExists 52 |
| 7360 | +#define OP_Sequence 53 |
| 7361 | +#define OP_NewRowid 54 |
| 7362 | +#define OP_Insert 55 |
| 7363 | +#define OP_InsertInt 56 |
| 7364 | +#define OP_Delete 57 |
| 7365 | +#define OP_ResetCount 58 |
| 7366 | +#define OP_RowKey 59 |
| 7367 | +#define OP_RowData 60 |
| 7368 | +#define OP_Rowid 61 |
| 7369 | +#define OP_NullRow 62 |
| 7370 | +#define OP_Last 63 |
| 7371 | +#define OP_Sort 64 |
| 7372 | +#define OP_Rewind 65 |
| 7373 | +#define OP_Prev 66 |
| 7374 | +#define OP_Next 67 |
| 7375 | +#define OP_IdxInsert 70 |
| 7376 | +#define OP_IdxDelete 71 |
| 7377 | +#define OP_IdxRowid 72 |
| 7378 | +#define OP_IdxLT 81 |
| 7379 | +#define OP_IdxGE 92 |
| 7380 | +#define OP_Destroy 95 |
| 7381 | +#define OP_Clear 96 |
| 7382 | +#define OP_CreateIndex 97 |
| 7383 | +#define OP_CreateTable 98 |
| 7384 | +#define OP_ParseSchema 99 |
| 7385 | +#define OP_LoadAnalysis 100 |
| 7386 | +#define OP_DropTable 101 |
| 7387 | +#define OP_DropIndex 102 |
| 7388 | +#define OP_DropTrigger 103 |
| 7389 | +#define OP_IntegrityCk 104 |
| 7390 | +#define OP_RowSetAdd 105 |
| 7391 | +#define OP_RowSetRead 106 |
| 7392 | +#define OP_RowSetTest 107 |
| 7393 | +#define OP_Program 108 |
| 7394 | +#define OP_Param 109 |
| 7395 | +#define OP_FkCounter 110 |
| 7396 | +#define OP_FkIfZero 111 |
| 7397 | +#define OP_MemMax 112 |
| 7398 | +#define OP_IfPos 113 |
| 7399 | +#define OP_IfNeg 114 |
| 7400 | +#define OP_IfZero 115 |
| 7401 | +#define OP_AggStep 116 |
| 7402 | +#define OP_AggFinal 117 |
| 7403 | +#define OP_Vacuum 118 |
| 7404 | +#define OP_IncrVacuum 119 |
| 7405 | +#define OP_Expire 120 |
| 7406 | +#define OP_TableLock 121 |
| 7407 | +#define OP_VBegin 122 |
| 7408 | +#define OP_VCreate 123 |
| 7409 | +#define OP_VDestroy 124 |
| 7410 | +#define OP_VOpen 125 |
| 7411 | +#define OP_VFilter 126 |
| 7412 | +#define OP_VColumn 127 |
| 7413 | +#define OP_VNext 128 |
| 7414 | +#define OP_VRename 129 |
| 7415 | +#define OP_VUpdate 131 |
| 7416 | +#define OP_Pagecount 132 |
| 7417 | +#define OP_Trace 133 |
| 7418 | +#define OP_Noop 134 |
| 7419 | +#define OP_Explain 135 |
| 7420 | + |
| 7421 | +/* The following opcode values are never used */ |
| 7414 | 7422 | #define OP_NotUsed_136 136 |
| 7415 | 7423 | #define OP_NotUsed_137 137 |
| 7416 | 7424 | #define OP_NotUsed_138 138 |
| 7417 | 7425 | #define OP_NotUsed_139 139 |
| 7418 | 7426 | #define OP_NotUsed_140 140 |
| | @@ -7433,22 +7441,22 @@ |
| 7433 | 7441 | /* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\ |
| 7434 | 7442 | /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x24, 0x24,\ |
| 7435 | 7443 | /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\ |
| 7436 | 7444 | /* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\ |
| 7437 | 7445 | /* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\ |
| 7438 | | -/* 40 */ 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x08,\ |
| 7439 | | -/* 48 */ 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00, 0x00,\ |
| 7440 | | -/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x01,\ |
| 7441 | | -/* 64 */ 0x01, 0x01, 0x01, 0x08, 0x4c, 0x4c, 0x00, 0x02,\ |
| 7442 | | -/* 72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\ |
| 7446 | +/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\ |
| 7447 | +/* 48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\ |
| 7448 | +/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\ |
| 7449 | +/* 64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x08, 0x00,\ |
| 7450 | +/* 72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\ |
| 7443 | 7451 | /* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\ |
| 7444 | | -/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x02, 0x24, 0x02, 0x00,\ |
| 7445 | | -/* 96 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 7446 | | -/* 104 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\ |
| 7447 | | -/* 112 */ 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00,\ |
| 7448 | | -/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,\ |
| 7449 | | -/* 128 */ 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,\ |
| 7452 | +/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\ |
| 7453 | +/* 96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 7454 | +/* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\ |
| 7455 | +/* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x01,\ |
| 7456 | +/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,\ |
| 7457 | +/* 128 */ 0x01, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00,\ |
| 7450 | 7458 | /* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\ |
| 7451 | 7459 | /* 144 */ 0x04, 0x04,} |
| 7452 | 7460 | |
| 7453 | 7461 | /************** End of opcodes.h *********************************************/ |
| 7454 | 7462 | /************** Continuing where we left off in vdbe.h ***********************/ |
| | @@ -7658,10 +7666,11 @@ |
| 7658 | 7666 | SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager); |
| 7659 | 7667 | |
| 7660 | 7668 | /* Functions used to query pager state and configuration. */ |
| 7661 | 7669 | SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*); |
| 7662 | 7670 | SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*); |
| 7671 | +SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*); |
| 7663 | 7672 | SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*); |
| 7664 | 7673 | SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*); |
| 7665 | 7674 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*); |
| 7666 | 7675 | SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*); |
| 7667 | 7676 | SQLITE_PRIVATE int sqlite3PagerNosync(Pager*); |
| | @@ -8478,10 +8487,11 @@ |
| 8478 | 8487 | #define SQLITE_LoadExtension 0x00400000 /* Enable load_extension */ |
| 8479 | 8488 | #define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */ |
| 8480 | 8489 | #define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */ |
| 8481 | 8490 | #define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */ |
| 8482 | 8491 | #define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */ |
| 8492 | +#define SQLITE_AutoIndex 0x08000000 /* Enable automatic indexes */ |
| 8483 | 8493 | |
| 8484 | 8494 | /* |
| 8485 | 8495 | ** Bits of the sqlite3.flags field that are used by the |
| 8486 | 8496 | ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface. |
| 8487 | 8497 | ** These must be the low-order bits of the flags field. |
| | @@ -9340,10 +9350,13 @@ |
| 9340 | 9350 | ** |
| 9341 | 9351 | ** The jointype starts out showing the join type between the current table |
| 9342 | 9352 | ** and the next table on the list. The parser builds the list this way. |
| 9343 | 9353 | ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each |
| 9344 | 9354 | ** jointype expresses the join between the table and the previous table. |
| 9355 | +** |
| 9356 | +** In the colUsed field, the high-order bit (bit 63) is set if the table |
| 9357 | +** contains more than 63 columns and the 64-th or later column is used. |
| 9345 | 9358 | */ |
| 9346 | 9359 | struct SrcList { |
| 9347 | 9360 | i16 nSrc; /* Number of tables or subqueries in the FROM clause */ |
| 9348 | 9361 | i16 nAlloc; /* Number of entries allocated in a[] below */ |
| 9349 | 9362 | struct SrcList_item { |
| | @@ -9451,11 +9464,11 @@ |
| 9451 | 9464 | #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */ |
| 9452 | 9465 | #define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */ |
| 9453 | 9466 | #define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */ |
| 9454 | 9467 | #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */ |
| 9455 | 9468 | #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */ |
| 9456 | | -#define WHERE_OMIT_OPEN 0x0010 /* Table cursor are already open */ |
| 9469 | +#define WHERE_OMIT_OPEN 0x0010 /* Table cursors are already open */ |
| 9457 | 9470 | #define WHERE_OMIT_CLOSE 0x0020 /* Omit close of table & index cursors */ |
| 9458 | 9471 | #define WHERE_FORCE_TABLE 0x0040 /* Do not use an index-only search */ |
| 9459 | 9472 | #define WHERE_ONETABLE_ONLY 0x0080 /* Only code the 1st table in pTabList */ |
| 9460 | 9473 | |
| 9461 | 9474 | /* |
| | @@ -9474,10 +9487,11 @@ |
| 9474 | 9487 | int iTop; /* The very beginning of the WHERE loop */ |
| 9475 | 9488 | int iContinue; /* Jump here to continue with next record */ |
| 9476 | 9489 | int iBreak; /* Jump here to break out of the loop */ |
| 9477 | 9490 | int nLevel; /* Number of nested loop */ |
| 9478 | 9491 | struct WhereClause *pWC; /* Decomposition of the WHERE clause */ |
| 9492 | + double savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
| 9479 | 9493 | WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 9480 | 9494 | }; |
| 9481 | 9495 | |
| 9482 | 9496 | /* |
| 9483 | 9497 | ** A NameContext defines a context in which to resolve table and column |
| | @@ -9715,10 +9729,11 @@ |
| 9715 | 9729 | u32 oldmask; /* Mask of old.* columns referenced */ |
| 9716 | 9730 | u32 newmask; /* Mask of new.* columns referenced */ |
| 9717 | 9731 | u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */ |
| 9718 | 9732 | u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ |
| 9719 | 9733 | u8 disableTriggers; /* True to disable triggers */ |
| 9734 | + double nQueryLoop; /* Estimated number of iterations of a query */ |
| 9720 | 9735 | |
| 9721 | 9736 | /* Above is constant between recursions. Below is reset before and after |
| 9722 | 9737 | ** each recursion */ |
| 9723 | 9738 | |
| 9724 | 9739 | int nVar; /* Number of '?' variables seen in the SQL so far */ |
| | @@ -10656,11 +10671,50 @@ |
| 10656 | 10671 | #else |
| 10657 | 10672 | # define IOTRACE(A) |
| 10658 | 10673 | # define sqlite3VdbeIOTraceSql(X) |
| 10659 | 10674 | #endif |
| 10660 | 10675 | |
| 10676 | +/* |
| 10677 | +** These routines are available for the mem2.c debugging memory allocator |
| 10678 | +** only. They are used to verify that different "types" of memory |
| 10679 | +** allocations are properly tracked by the system. |
| 10680 | +** |
| 10681 | +** sqlite3MemdebugSetType() sets the "type" of an allocation to one of |
| 10682 | +** the MEMTYPE_* macros defined below. The type must be a bitmask with |
| 10683 | +** a single bit set. |
| 10684 | +** |
| 10685 | +** sqlite3MemdebugHasType() returns true if any of the bits in its second |
| 10686 | +** argument match the type set by the previous sqlite3MemdebugSetType(). |
| 10687 | +** sqlite3MemdebugHasType() is intended for use inside assert() statements. |
| 10688 | +** For example: |
| 10689 | +** |
| 10690 | +** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 10691 | +** |
| 10692 | +** Perhaps the most important point is the difference between MEMTYPE_HEAP |
| 10693 | +** and MEMTYPE_DB. If an allocation is MEMTYPE_DB, that means it might have |
| 10694 | +** been allocated by lookaside, except the allocation was too large or |
| 10695 | +** lookaside was already full. It is important to verify that allocations |
| 10696 | +** that might have been satisfied by lookaside are not passed back to |
| 10697 | +** non-lookaside free() routines. Asserts such as the example above are |
| 10698 | +** placed on the non-lookaside free() routines to verify this constraint. |
| 10699 | +** |
| 10700 | +** All of this is no-op for a production build. It only comes into |
| 10701 | +** play when the SQLITE_MEMDEBUG compile-time option is used. |
| 10702 | +*/ |
| 10703 | +#ifdef SQLITE_MEMDEBUG |
| 10704 | +SQLITE_PRIVATE void sqlite3MemdebugSetType(void*,u8); |
| 10705 | +SQLITE_PRIVATE int sqlite3MemdebugHasType(void*,u8); |
| 10706 | +#else |
| 10707 | +# define sqlite3MemdebugSetType(X,Y) /* no-op */ |
| 10708 | +# define sqlite3MemdebugHasType(X,Y) 1 |
| 10661 | 10709 | #endif |
| 10710 | +#define MEMTYPE_HEAP 0x01 /* General heap allocations */ |
| 10711 | +#define MEMTYPE_DB 0x02 /* Associated with a database connection */ |
| 10712 | +#define MEMTYPE_SCRATCH 0x04 /* Scratch allocations */ |
| 10713 | +#define MEMTYPE_PCACHE 0x08 /* Page cache allocations */ |
| 10714 | + |
| 10715 | +#endif /* _SQLITEINT_H_ */ |
| 10662 | 10716 | |
| 10663 | 10717 | /************** End of sqliteInt.h *******************************************/ |
| 10664 | 10718 | /************** Begin file global.c ******************************************/ |
| 10665 | 10719 | /* |
| 10666 | 10720 | ** 2008 June 13 |
| | @@ -11038,10 +11092,13 @@ |
| 11038 | 11092 | #ifdef SQLITE_OMIT_AUTOINCREMENT |
| 11039 | 11093 | "OMIT_AUTOINCREMENT", |
| 11040 | 11094 | #endif |
| 11041 | 11095 | #ifdef SQLITE_OMIT_AUTOINIT |
| 11042 | 11096 | "OMIT_AUTOINIT", |
| 11097 | +#endif |
| 11098 | +#ifdef SQLITE_OMIT_AUTOMATIC_INDEX |
| 11099 | + "OMIT_AUTOMATIC_INDEX", |
| 11043 | 11100 | #endif |
| 11044 | 11101 | #ifdef SQLITE_OMIT_AUTOVACUUM |
| 11045 | 11102 | "OMIT_AUTOVACUUM", |
| 11046 | 11103 | #endif |
| 11047 | 11104 | #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
| | @@ -11367,10 +11424,30 @@ |
| 11367 | 11424 | if( resetFlag ){ |
| 11368 | 11425 | db->lookaside.mxOut = db->lookaside.nOut; |
| 11369 | 11426 | } |
| 11370 | 11427 | break; |
| 11371 | 11428 | } |
| 11429 | + |
| 11430 | + /* |
| 11431 | + ** Return an approximation for the amount of memory currently used |
| 11432 | + ** by all pagers associated with the given database connection. The |
| 11433 | + ** highwater mark is meaningless and is returned as zero. |
| 11434 | + */ |
| 11435 | + case SQLITE_DBSTATUS_CACHE_USED: { |
| 11436 | + int totalUsed = 0; |
| 11437 | + int i; |
| 11438 | + for(i=0; i<db->nDb; i++){ |
| 11439 | + Btree *pBt = db->aDb[i].pBt; |
| 11440 | + if( pBt ){ |
| 11441 | + Pager *pPager = sqlite3BtreePager(pBt); |
| 11442 | + totalUsed += sqlite3PagerMemUsed(pPager); |
| 11443 | + } |
| 11444 | + } |
| 11445 | + *pCurrent = totalUsed; |
| 11446 | + *pHighwater = 0; |
| 11447 | + break; |
| 11448 | + } |
| 11372 | 11449 | default: { |
| 11373 | 11450 | return SQLITE_ERROR; |
| 11374 | 11451 | } |
| 11375 | 11452 | } |
| 11376 | 11453 | return SQLITE_OK; |
| | @@ -13140,11 +13217,12 @@ |
| 13140 | 13217 | struct MemBlockHdr { |
| 13141 | 13218 | i64 iSize; /* Size of this allocation */ |
| 13142 | 13219 | struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */ |
| 13143 | 13220 | char nBacktrace; /* Number of backtraces on this alloc */ |
| 13144 | 13221 | char nBacktraceSlots; /* Available backtrace slots */ |
| 13145 | | - short nTitle; /* Bytes of title; includes '\0' */ |
| 13222 | + u8 nTitle; /* Bytes of title; includes '\0' */ |
| 13223 | + u8 eType; /* Allocation type code */ |
| 13146 | 13224 | int iForeGuard; /* Guard word for sanity */ |
| 13147 | 13225 | }; |
| 13148 | 13226 | |
| 13149 | 13227 | /* |
| 13150 | 13228 | ** Guard words |
| | @@ -13348,10 +13426,11 @@ |
| 13348 | 13426 | }else{ |
| 13349 | 13427 | mem.pFirst = pHdr; |
| 13350 | 13428 | } |
| 13351 | 13429 | mem.pLast = pHdr; |
| 13352 | 13430 | pHdr->iForeGuard = FOREGUARD; |
| 13431 | + pHdr->eType = MEMTYPE_HEAP; |
| 13353 | 13432 | pHdr->nBacktraceSlots = mem.nBacktrace; |
| 13354 | 13433 | pHdr->nTitle = mem.nTitle; |
| 13355 | 13434 | if( mem.nBacktrace ){ |
| 13356 | 13435 | void *aAddr[40]; |
| 13357 | 13436 | pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1; |
| | @@ -13454,10 +13533,51 @@ |
| 13454 | 13533 | sqlite3MemShutdown, |
| 13455 | 13534 | 0 |
| 13456 | 13535 | }; |
| 13457 | 13536 | sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); |
| 13458 | 13537 | } |
| 13538 | + |
| 13539 | +/* |
| 13540 | +** Set the "type" of an allocation. |
| 13541 | +*/ |
| 13542 | +SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){ |
| 13543 | + if( p ){ |
| 13544 | + struct MemBlockHdr *pHdr; |
| 13545 | + pHdr = sqlite3MemsysGetHeader(p); |
| 13546 | + assert( pHdr->iForeGuard==FOREGUARD ); |
| 13547 | + pHdr->eType = eType; |
| 13548 | + } |
| 13549 | +} |
| 13550 | + |
| 13551 | +/* |
| 13552 | +** Return TRUE if the mask of type in eType matches the type of the |
| 13553 | +** allocation p. Also return true if p==NULL. |
| 13554 | +** |
| 13555 | +** This routine is designed for use within an assert() statement, to |
| 13556 | +** verify the type of an allocation. For example: |
| 13557 | +** |
| 13558 | +** assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 13559 | +*/ |
| 13560 | +SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){ |
| 13561 | + int rc = 1; |
| 13562 | + if( p ){ |
| 13563 | + struct MemBlockHdr *pHdr; |
| 13564 | + pHdr = sqlite3MemsysGetHeader(p); |
| 13565 | + assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ |
| 13566 | + assert( (pHdr->eType & (pHdr->eType-1))==0 ); /* Only one type bit set */ |
| 13567 | + if( (pHdr->eType&eType)==0 ){ |
| 13568 | + void **pBt; |
| 13569 | + pBt = (void**)pHdr; |
| 13570 | + pBt -= pHdr->nBacktraceSlots; |
| 13571 | + backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(stderr)); |
| 13572 | + fprintf(stderr, "\n"); |
| 13573 | + rc = 0; |
| 13574 | + } |
| 13575 | + } |
| 13576 | + return rc; |
| 13577 | +} |
| 13578 | + |
| 13459 | 13579 | |
| 13460 | 13580 | /* |
| 13461 | 13581 | ** Set the number of backtrace levels kept for each allocation. |
| 13462 | 13582 | ** A value of zero turns off backtracing. The number is always rounded |
| 13463 | 13583 | ** up to a multiple of 2. |
| | @@ -16446,10 +16566,11 @@ |
| 16446 | 16566 | if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); |
| 16447 | 16567 | sqlite3_mutex_leave(mem0.mutex); |
| 16448 | 16568 | }else{ |
| 16449 | 16569 | p = sqlite3GlobalConfig.m.xMalloc(n); |
| 16450 | 16570 | } |
| 16571 | + sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); |
| 16451 | 16572 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 16452 | 16573 | scratchAllocOut = p!=0; |
| 16453 | 16574 | #endif |
| 16454 | 16575 | return p; |
| 16455 | 16576 | } |
| | @@ -16466,10 +16587,12 @@ |
| 16466 | 16587 | #endif |
| 16467 | 16588 | |
| 16468 | 16589 | if( sqlite3GlobalConfig.pScratch==0 |
| 16469 | 16590 | || p<sqlite3GlobalConfig.pScratch |
| 16470 | 16591 | || p>=(void*)mem0.aScratchFree ){ |
| 16592 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); |
| 16593 | + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 16471 | 16594 | if( sqlite3GlobalConfig.bMemstat ){ |
| 16472 | 16595 | int iSize = sqlite3MallocSize(p); |
| 16473 | 16596 | sqlite3_mutex_enter(mem0.mutex); |
| 16474 | 16597 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); |
| 16475 | 16598 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); |
| | @@ -16506,26 +16629,30 @@ |
| 16506 | 16629 | /* |
| 16507 | 16630 | ** Return the size of a memory allocation previously obtained from |
| 16508 | 16631 | ** sqlite3Malloc() or sqlite3_malloc(). |
| 16509 | 16632 | */ |
| 16510 | 16633 | SQLITE_PRIVATE int sqlite3MallocSize(void *p){ |
| 16634 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 16511 | 16635 | return sqlite3GlobalConfig.m.xSize(p); |
| 16512 | 16636 | } |
| 16513 | 16637 | SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){ |
| 16514 | 16638 | assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
| 16515 | 16639 | if( isLookaside(db, p) ){ |
| 16516 | 16640 | return db->lookaside.sz; |
| 16517 | 16641 | }else{ |
| 16642 | + assert( sqlite3MemdebugHasType(p, |
| 16643 | + db ? (MEMTYPE_DB|MEMTYPE_HEAP) : MEMTYPE_HEAP) ); |
| 16518 | 16644 | return sqlite3GlobalConfig.m.xSize(p); |
| 16519 | 16645 | } |
| 16520 | 16646 | } |
| 16521 | 16647 | |
| 16522 | 16648 | /* |
| 16523 | 16649 | ** Free memory previously obtained from sqlite3Malloc(). |
| 16524 | 16650 | */ |
| 16525 | 16651 | SQLITE_API void sqlite3_free(void *p){ |
| 16526 | 16652 | if( p==0 ) return; |
| 16653 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 16527 | 16654 | if( sqlite3GlobalConfig.bMemstat ){ |
| 16528 | 16655 | sqlite3_mutex_enter(mem0.mutex); |
| 16529 | 16656 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
| 16530 | 16657 | sqlite3GlobalConfig.m.xFree(p); |
| 16531 | 16658 | sqlite3_mutex_leave(mem0.mutex); |
| | @@ -16544,10 +16671,12 @@ |
| 16544 | 16671 | LookasideSlot *pBuf = (LookasideSlot*)p; |
| 16545 | 16672 | pBuf->pNext = db->lookaside.pFree; |
| 16546 | 16673 | db->lookaside.pFree = pBuf; |
| 16547 | 16674 | db->lookaside.nOut--; |
| 16548 | 16675 | }else{ |
| 16676 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_DB|MEMTYPE_HEAP) ); |
| 16677 | + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 16549 | 16678 | sqlite3_free(p); |
| 16550 | 16679 | } |
| 16551 | 16680 | } |
| 16552 | 16681 | |
| 16553 | 16682 | /* |
| | @@ -16576,10 +16705,11 @@ |
| 16576 | 16705 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); |
| 16577 | 16706 | if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= |
| 16578 | 16707 | mem0.alarmThreshold ){ |
| 16579 | 16708 | sqlite3MallocAlarm(nNew-nOld); |
| 16580 | 16709 | } |
| 16710 | + assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
| 16581 | 16711 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 16582 | 16712 | if( pNew==0 && mem0.alarmCallback ){ |
| 16583 | 16713 | sqlite3MallocAlarm(nBytes); |
| 16584 | 16714 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 16585 | 16715 | } |
| | @@ -16673,10 +16803,12 @@ |
| 16673 | 16803 | #endif |
| 16674 | 16804 | p = sqlite3Malloc(n); |
| 16675 | 16805 | if( !p && db ){ |
| 16676 | 16806 | db->mallocFailed = 1; |
| 16677 | 16807 | } |
| 16808 | + sqlite3MemdebugSetType(p, |
| 16809 | + (db && db->lookaside.bEnabled) ? MEMTYPE_DB : MEMTYPE_HEAP); |
| 16678 | 16810 | return p; |
| 16679 | 16811 | } |
| 16680 | 16812 | |
| 16681 | 16813 | /* |
| 16682 | 16814 | ** Resize the block of memory pointed to by p to n bytes. If the |
| | @@ -16698,14 +16830,18 @@ |
| 16698 | 16830 | if( pNew ){ |
| 16699 | 16831 | memcpy(pNew, p, db->lookaside.sz); |
| 16700 | 16832 | sqlite3DbFree(db, p); |
| 16701 | 16833 | } |
| 16702 | 16834 | }else{ |
| 16835 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_DB|MEMTYPE_HEAP) ); |
| 16836 | + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 16703 | 16837 | pNew = sqlite3_realloc(p, n); |
| 16704 | 16838 | if( !pNew ){ |
| 16705 | 16839 | db->mallocFailed = 1; |
| 16706 | 16840 | } |
| 16841 | + sqlite3MemdebugSetType(pNew, |
| 16842 | + db->lookaside.bEnabled ? MEMTYPE_DB : MEMTYPE_HEAP); |
| 16707 | 16843 | } |
| 16708 | 16844 | } |
| 16709 | 16845 | return pNew; |
| 16710 | 16846 | } |
| 16711 | 16847 | |
| | @@ -18305,11 +18441,11 @@ |
| 18305 | 18441 | u8 isPrepareV2; /* True if prepared with prepare_v2() */ |
| 18306 | 18442 | int nChange; /* Number of db changes made since last reset */ |
| 18307 | 18443 | int btreeMask; /* Bitmask of db->aDb[] entries referenced */ |
| 18308 | 18444 | i64 startTime; /* Time when query started - used for profiling */ |
| 18309 | 18445 | BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */ |
| 18310 | | - int aCounter[2]; /* Counters used by sqlite3_stmt_status() */ |
| 18446 | + int aCounter[3]; /* Counters used by sqlite3_stmt_status() */ |
| 18311 | 18447 | char *zSql; /* Text of the SQL statement that generated this */ |
| 18312 | 18448 | void *pFree; /* Free this when deleting the vdbe */ |
| 18313 | 18449 | i64 nFkConstraint; /* Number of imm. FK constraints this VM */ |
| 18314 | 18450 | i64 nStmtDefCons; /* Number of def. constraints when stmt started */ |
| 18315 | 18451 | int iStatement; /* Statement number (or 0 if has not opened stmt) */ |
| | @@ -20345,52 +20481,52 @@ |
| 20345 | 20481 | /* 35 */ "ReadCookie", |
| 20346 | 20482 | /* 36 */ "SetCookie", |
| 20347 | 20483 | /* 37 */ "VerifyCookie", |
| 20348 | 20484 | /* 38 */ "OpenRead", |
| 20349 | 20485 | /* 39 */ "OpenWrite", |
| 20350 | | - /* 40 */ "OpenEphemeral", |
| 20351 | | - /* 41 */ "OpenPseudo", |
| 20352 | | - /* 42 */ "Close", |
| 20353 | | - /* 43 */ "SeekLt", |
| 20354 | | - /* 44 */ "SeekLe", |
| 20355 | | - /* 45 */ "SeekGe", |
| 20356 | | - /* 46 */ "SeekGt", |
| 20357 | | - /* 47 */ "Seek", |
| 20358 | | - /* 48 */ "NotFound", |
| 20359 | | - /* 49 */ "Found", |
| 20360 | | - /* 50 */ "IsUnique", |
| 20361 | | - /* 51 */ "NotExists", |
| 20362 | | - /* 52 */ "Sequence", |
| 20363 | | - /* 53 */ "NewRowid", |
| 20364 | | - /* 54 */ "Insert", |
| 20365 | | - /* 55 */ "InsertInt", |
| 20366 | | - /* 56 */ "Delete", |
| 20367 | | - /* 57 */ "ResetCount", |
| 20368 | | - /* 58 */ "RowKey", |
| 20369 | | - /* 59 */ "RowData", |
| 20370 | | - /* 60 */ "Rowid", |
| 20371 | | - /* 61 */ "NullRow", |
| 20372 | | - /* 62 */ "Last", |
| 20373 | | - /* 63 */ "Sort", |
| 20374 | | - /* 64 */ "Rewind", |
| 20375 | | - /* 65 */ "Prev", |
| 20376 | | - /* 66 */ "Next", |
| 20377 | | - /* 67 */ "IdxInsert", |
| 20486 | + /* 40 */ "OpenAutoindex", |
| 20487 | + /* 41 */ "OpenEphemeral", |
| 20488 | + /* 42 */ "OpenPseudo", |
| 20489 | + /* 43 */ "Close", |
| 20490 | + /* 44 */ "SeekLt", |
| 20491 | + /* 45 */ "SeekLe", |
| 20492 | + /* 46 */ "SeekGe", |
| 20493 | + /* 47 */ "SeekGt", |
| 20494 | + /* 48 */ "Seek", |
| 20495 | + /* 49 */ "NotFound", |
| 20496 | + /* 50 */ "Found", |
| 20497 | + /* 51 */ "IsUnique", |
| 20498 | + /* 52 */ "NotExists", |
| 20499 | + /* 53 */ "Sequence", |
| 20500 | + /* 54 */ "NewRowid", |
| 20501 | + /* 55 */ "Insert", |
| 20502 | + /* 56 */ "InsertInt", |
| 20503 | + /* 57 */ "Delete", |
| 20504 | + /* 58 */ "ResetCount", |
| 20505 | + /* 59 */ "RowKey", |
| 20506 | + /* 60 */ "RowData", |
| 20507 | + /* 61 */ "Rowid", |
| 20508 | + /* 62 */ "NullRow", |
| 20509 | + /* 63 */ "Last", |
| 20510 | + /* 64 */ "Sort", |
| 20511 | + /* 65 */ "Rewind", |
| 20512 | + /* 66 */ "Prev", |
| 20513 | + /* 67 */ "Next", |
| 20378 | 20514 | /* 68 */ "Or", |
| 20379 | 20515 | /* 69 */ "And", |
| 20380 | | - /* 70 */ "IdxDelete", |
| 20381 | | - /* 71 */ "IdxRowid", |
| 20382 | | - /* 72 */ "IdxLT", |
| 20516 | + /* 70 */ "IdxInsert", |
| 20517 | + /* 71 */ "IdxDelete", |
| 20518 | + /* 72 */ "IdxRowid", |
| 20383 | 20519 | /* 73 */ "IsNull", |
| 20384 | 20520 | /* 74 */ "NotNull", |
| 20385 | 20521 | /* 75 */ "Ne", |
| 20386 | 20522 | /* 76 */ "Eq", |
| 20387 | 20523 | /* 77 */ "Gt", |
| 20388 | 20524 | /* 78 */ "Le", |
| 20389 | 20525 | /* 79 */ "Lt", |
| 20390 | 20526 | /* 80 */ "Ge", |
| 20391 | | - /* 81 */ "IdxGE", |
| 20527 | + /* 81 */ "IdxLT", |
| 20392 | 20528 | /* 82 */ "BitAnd", |
| 20393 | 20529 | /* 83 */ "BitOr", |
| 20394 | 20530 | /* 84 */ "ShiftLeft", |
| 20395 | 20531 | /* 85 */ "ShiftRight", |
| 20396 | 20532 | /* 86 */ "Add", |
| | @@ -20397,54 +20533,54 @@ |
| 20397 | 20533 | /* 87 */ "Subtract", |
| 20398 | 20534 | /* 88 */ "Multiply", |
| 20399 | 20535 | /* 89 */ "Divide", |
| 20400 | 20536 | /* 90 */ "Remainder", |
| 20401 | 20537 | /* 91 */ "Concat", |
| 20402 | | - /* 92 */ "Destroy", |
| 20538 | + /* 92 */ "IdxGE", |
| 20403 | 20539 | /* 93 */ "BitNot", |
| 20404 | 20540 | /* 94 */ "String8", |
| 20405 | | - /* 95 */ "Clear", |
| 20406 | | - /* 96 */ "CreateIndex", |
| 20407 | | - /* 97 */ "CreateTable", |
| 20408 | | - /* 98 */ "ParseSchema", |
| 20409 | | - /* 99 */ "LoadAnalysis", |
| 20410 | | - /* 100 */ "DropTable", |
| 20411 | | - /* 101 */ "DropIndex", |
| 20412 | | - /* 102 */ "DropTrigger", |
| 20413 | | - /* 103 */ "IntegrityCk", |
| 20414 | | - /* 104 */ "RowSetAdd", |
| 20415 | | - /* 105 */ "RowSetRead", |
| 20416 | | - /* 106 */ "RowSetTest", |
| 20417 | | - /* 107 */ "Program", |
| 20418 | | - /* 108 */ "Param", |
| 20419 | | - /* 109 */ "FkCounter", |
| 20420 | | - /* 110 */ "FkIfZero", |
| 20421 | | - /* 111 */ "MemMax", |
| 20422 | | - /* 112 */ "IfPos", |
| 20423 | | - /* 113 */ "IfNeg", |
| 20424 | | - /* 114 */ "IfZero", |
| 20425 | | - /* 115 */ "AggStep", |
| 20426 | | - /* 116 */ "AggFinal", |
| 20427 | | - /* 117 */ "Vacuum", |
| 20428 | | - /* 118 */ "IncrVacuum", |
| 20429 | | - /* 119 */ "Expire", |
| 20430 | | - /* 120 */ "TableLock", |
| 20431 | | - /* 121 */ "VBegin", |
| 20432 | | - /* 122 */ "VCreate", |
| 20433 | | - /* 123 */ "VDestroy", |
| 20434 | | - /* 124 */ "VOpen", |
| 20435 | | - /* 125 */ "VFilter", |
| 20436 | | - /* 126 */ "VColumn", |
| 20437 | | - /* 127 */ "VNext", |
| 20438 | | - /* 128 */ "VRename", |
| 20439 | | - /* 129 */ "VUpdate", |
| 20541 | + /* 95 */ "Destroy", |
| 20542 | + /* 96 */ "Clear", |
| 20543 | + /* 97 */ "CreateIndex", |
| 20544 | + /* 98 */ "CreateTable", |
| 20545 | + /* 99 */ "ParseSchema", |
| 20546 | + /* 100 */ "LoadAnalysis", |
| 20547 | + /* 101 */ "DropTable", |
| 20548 | + /* 102 */ "DropIndex", |
| 20549 | + /* 103 */ "DropTrigger", |
| 20550 | + /* 104 */ "IntegrityCk", |
| 20551 | + /* 105 */ "RowSetAdd", |
| 20552 | + /* 106 */ "RowSetRead", |
| 20553 | + /* 107 */ "RowSetTest", |
| 20554 | + /* 108 */ "Program", |
| 20555 | + /* 109 */ "Param", |
| 20556 | + /* 110 */ "FkCounter", |
| 20557 | + /* 111 */ "FkIfZero", |
| 20558 | + /* 112 */ "MemMax", |
| 20559 | + /* 113 */ "IfPos", |
| 20560 | + /* 114 */ "IfNeg", |
| 20561 | + /* 115 */ "IfZero", |
| 20562 | + /* 116 */ "AggStep", |
| 20563 | + /* 117 */ "AggFinal", |
| 20564 | + /* 118 */ "Vacuum", |
| 20565 | + /* 119 */ "IncrVacuum", |
| 20566 | + /* 120 */ "Expire", |
| 20567 | + /* 121 */ "TableLock", |
| 20568 | + /* 122 */ "VBegin", |
| 20569 | + /* 123 */ "VCreate", |
| 20570 | + /* 124 */ "VDestroy", |
| 20571 | + /* 125 */ "VOpen", |
| 20572 | + /* 126 */ "VFilter", |
| 20573 | + /* 127 */ "VColumn", |
| 20574 | + /* 128 */ "VNext", |
| 20575 | + /* 129 */ "VRename", |
| 20440 | 20576 | /* 130 */ "Real", |
| 20441 | | - /* 131 */ "Pagecount", |
| 20442 | | - /* 132 */ "Trace", |
| 20443 | | - /* 133 */ "Noop", |
| 20444 | | - /* 134 */ "Explain", |
| 20445 | | - /* 135 */ "NotUsed_135", |
| 20577 | + /* 131 */ "VUpdate", |
| 20578 | + /* 132 */ "Pagecount", |
| 20579 | + /* 133 */ "Trace", |
| 20580 | + /* 134 */ "Noop", |
| 20581 | + /* 135 */ "Explain", |
| 20446 | 20582 | /* 136 */ "NotUsed_136", |
| 20447 | 20583 | /* 137 */ "NotUsed_137", |
| 20448 | 20584 | /* 138 */ "NotUsed_138", |
| 20449 | 20585 | /* 139 */ "NotUsed_139", |
| 20450 | 20586 | /* 140 */ "NotUsed_140", |
| | @@ -26155,13 +26291,11 @@ |
| 26155 | 26291 | flags |= SQLITE_OPEN_READONLY; |
| 26156 | 26292 | openFlags |= O_RDONLY; |
| 26157 | 26293 | fd = open(zName, openFlags, openMode); |
| 26158 | 26294 | } |
| 26159 | 26295 | if( fd<0 ){ |
| 26160 | | - sqlite3_log(SQLITE_CANTOPEN, "cannot open file [%s]: %s", zName, |
| 26161 | | - strerror(errno)); |
| 26162 | | - rc = SQLITE_CANTOPEN; |
| 26296 | + rc = SQLITE_CANTOPEN_BKPT; |
| 26163 | 26297 | goto open_finished; |
| 26164 | 26298 | } |
| 26165 | 26299 | } |
| 26166 | 26300 | assert( fd>=0 ); |
| 26167 | 26301 | if( pOutFlags ){ |
| | @@ -30816,11 +30950,16 @@ |
| 30816 | 30950 | if( pCache->pCache ){ |
| 30817 | 30951 | PgHdr *p; |
| 30818 | 30952 | PgHdr *pNext; |
| 30819 | 30953 | for(p=pCache->pDirty; p; p=pNext){ |
| 30820 | 30954 | pNext = p->pDirtyNext; |
| 30821 | | - if( p->pgno>pgno ){ |
| 30955 | + /* This routine never gets call with a positive pgno except right |
| 30956 | + ** after sqlite3PcacheCleanAll(). So if there are dirty pages, |
| 30957 | + ** it must be that pgno==0. |
| 30958 | + */ |
| 30959 | + assert( p->pgno>0 ); |
| 30960 | + if( ALWAYS(p->pgno>pgno) ){ |
| 30822 | 30961 | assert( p->flags&PGHDR_DIRTY ); |
| 30823 | 30962 | sqlite3PcacheMakeClean(p); |
| 30824 | 30963 | } |
| 30825 | 30964 | } |
| 30826 | 30965 | if( pgno==0 && pCache->pPage1 ){ |
| | @@ -31160,10 +31299,11 @@ |
| 31160 | 31299 | pcache1EnterMutex(); |
| 31161 | 31300 | if( p ){ |
| 31162 | 31301 | int sz = sqlite3MallocSize(p); |
| 31163 | 31302 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); |
| 31164 | 31303 | } |
| 31304 | + sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); |
| 31165 | 31305 | } |
| 31166 | 31306 | return p; |
| 31167 | 31307 | } |
| 31168 | 31308 | |
| 31169 | 31309 | /* |
| | @@ -31177,11 +31317,14 @@ |
| 31177 | 31317 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); |
| 31178 | 31318 | pSlot = (PgFreeslot*)p; |
| 31179 | 31319 | pSlot->pNext = pcache1.pFree; |
| 31180 | 31320 | pcache1.pFree = pSlot; |
| 31181 | 31321 | }else{ |
| 31182 | | - int iSize = sqlite3MallocSize(p); |
| 31322 | + int iSize; |
| 31323 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 31324 | + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 31325 | + iSize = sqlite3MallocSize(p); |
| 31183 | 31326 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize); |
| 31184 | 31327 | sqlite3_free(p); |
| 31185 | 31328 | } |
| 31186 | 31329 | } |
| 31187 | 31330 | |
| | @@ -33315,12 +33458,13 @@ |
| 33315 | 33458 | pPager->pInJournal = 0; |
| 33316 | 33459 | releaseAllSavepoints(pPager); |
| 33317 | 33460 | |
| 33318 | 33461 | /* If the file is unlocked, somebody else might change it. The |
| 33319 | 33462 | ** values stored in Pager.dbSize etc. might become invalid if |
| 33320 | | - ** this happens. TODO: Really, this doesn't need to be cleared |
| 33463 | + ** this happens. One can argue that this doesn't need to be cleared |
| 33321 | 33464 | ** until the change-counter check fails in PagerSharedLock(). |
| 33465 | + ** Clearing the page size cache here is being conservative. |
| 33322 | 33466 | */ |
| 33323 | 33467 | pPager->dbSizeValid = 0; |
| 33324 | 33468 | |
| 33325 | 33469 | rc = osUnlock(pPager->fd, NO_LOCK); |
| 33326 | 33470 | if( rc ){ |
| | @@ -33506,16 +33650,15 @@ |
| 33506 | 33650 | } |
| 33507 | 33651 | |
| 33508 | 33652 | #ifdef SQLITE_CHECK_PAGES |
| 33509 | 33653 | sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); |
| 33510 | 33654 | #endif |
| 33511 | | - |
| 33512 | | - sqlite3PcacheCleanAll(pPager->pPCache); |
| 33513 | | - sqlite3BitvecDestroy(pPager->pInJournal); |
| 33514 | | - pPager->pInJournal = 0; |
| 33515 | | - pPager->nRec = 0; |
| 33516 | 33655 | } |
| 33656 | + sqlite3BitvecDestroy(pPager->pInJournal); |
| 33657 | + pPager->pInJournal = 0; |
| 33658 | + pPager->nRec = 0; |
| 33659 | + sqlite3PcacheCleanAll(pPager->pPCache); |
| 33517 | 33660 | |
| 33518 | 33661 | if( !pPager->exclusiveMode ){ |
| 33519 | 33662 | rc2 = osUnlock(pPager->fd, SHARED_LOCK); |
| 33520 | 33663 | pPager->state = PAGER_SHARED; |
| 33521 | 33664 | pPager->changeCountDone = 0; |
| | @@ -34170,10 +34313,18 @@ |
| 34170 | 34313 | if( rc!=SQLITE_OK ){ |
| 34171 | 34314 | if( rc==SQLITE_DONE ){ |
| 34172 | 34315 | rc = SQLITE_OK; |
| 34173 | 34316 | pPager->journalOff = szJ; |
| 34174 | 34317 | break; |
| 34318 | + }else if( rc==SQLITE_IOERR_SHORT_READ ){ |
| 34319 | + /* If the journal has been truncated, simply stop reading and |
| 34320 | + ** processing the journal. This might happen if the journal was |
| 34321 | + ** not completely written and synced prior to a crash. In that |
| 34322 | + ** case, the database should have never been written in the |
| 34323 | + ** first place so it is OK to simply abandon the rollback. */ |
| 34324 | + rc = SQLITE_OK; |
| 34325 | + goto end_playback; |
| 34175 | 34326 | }else{ |
| 34176 | 34327 | /* If we are unable to rollback, quit and return the error |
| 34177 | 34328 | ** code. This will cause the pager to enter the error state |
| 34178 | 34329 | ** so that no further harm will be done. Perhaps the next |
| 34179 | 34330 | ** process to come along will be able to rollback the database. |
| | @@ -34575,14 +34726,16 @@ |
| 34575 | 34726 | ** maximum page count below the current size of the database. |
| 34576 | 34727 | ** |
| 34577 | 34728 | ** Regardless of mxPage, return the current maximum page count. |
| 34578 | 34729 | */ |
| 34579 | 34730 | SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ |
| 34731 | + int nPage; |
| 34580 | 34732 | if( mxPage>0 ){ |
| 34581 | 34733 | pPager->mxPgno = mxPage; |
| 34582 | 34734 | } |
| 34583 | | - sqlite3PagerPagecount(pPager, 0); |
| 34735 | + sqlite3PagerPagecount(pPager, &nPage); |
| 34736 | + assert( pPager->mxPgno>=nPage ); |
| 34584 | 34737 | return pPager->mxPgno; |
| 34585 | 34738 | } |
| 34586 | 34739 | |
| 34587 | 34740 | /* |
| 34588 | 34741 | ** The following set of routines are used to disable the simulated |
| | @@ -34652,15 +34805,10 @@ |
| 34652 | 34805 | ** and *pnPage is set to the number of pages in the database. |
| 34653 | 34806 | */ |
| 34654 | 34807 | SQLITE_PRIVATE int sqlite3PagerPagecount(Pager *pPager, int *pnPage){ |
| 34655 | 34808 | Pgno nPage; /* Value to return via *pnPage */ |
| 34656 | 34809 | |
| 34657 | | - /* If the pager is already in the error state, return the error code. */ |
| 34658 | | - if( pPager->errCode ){ |
| 34659 | | - return pPager->errCode; |
| 34660 | | - } |
| 34661 | | - |
| 34662 | 34810 | /* Determine the number of pages in the file. Store this in nPage. */ |
| 34663 | 34811 | if( pPager->dbSizeValid ){ |
| 34664 | 34812 | nPage = pPager->dbSize; |
| 34665 | 34813 | }else{ |
| 34666 | 34814 | int rc; /* Error returned by OsFileSize() */ |
| | @@ -34690,13 +34838,11 @@ |
| 34690 | 34838 | if( nPage>pPager->mxPgno ){ |
| 34691 | 34839 | pPager->mxPgno = (Pgno)nPage; |
| 34692 | 34840 | } |
| 34693 | 34841 | |
| 34694 | 34842 | /* Set the output variable and return SQLITE_OK */ |
| 34695 | | - if( pnPage ){ |
| 34696 | | - *pnPage = nPage; |
| 34697 | | - } |
| 34843 | + *pnPage = nPage; |
| 34698 | 34844 | return SQLITE_OK; |
| 34699 | 34845 | } |
| 34700 | 34846 | |
| 34701 | 34847 | |
| 34702 | 34848 | /* |
| | @@ -35890,20 +36036,20 @@ |
| 35890 | 36036 | ** |
| 35891 | 36037 | ** There is a vanishingly small chance that a change will not be |
| 35892 | 36038 | ** detected. The chance of an undetected change is so small that |
| 35893 | 36039 | ** it can be neglected. |
| 35894 | 36040 | */ |
| 36041 | + int nPage; |
| 35895 | 36042 | char dbFileVers[sizeof(pPager->dbFileVers)]; |
| 35896 | | - sqlite3PagerPagecount(pPager, 0); |
| 36043 | + sqlite3PagerPagecount(pPager, &nPage); |
| 35897 | 36044 | |
| 35898 | 36045 | if( pPager->errCode ){ |
| 35899 | 36046 | rc = pPager->errCode; |
| 35900 | 36047 | goto failed; |
| 35901 | 36048 | } |
| 35902 | 36049 | |
| 35903 | | - assert( pPager->dbSizeValid ); |
| 35904 | | - if( pPager->dbSize>0 ){ |
| 36050 | + if( nPage>0 ){ |
| 35905 | 36051 | IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); |
| 35906 | 36052 | rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); |
| 35907 | 36053 | if( rc!=SQLITE_OK ){ |
| 35908 | 36054 | goto failed; |
| 35909 | 36055 | } |
| | @@ -36024,11 +36170,11 @@ |
| 36024 | 36170 | goto pager_acquire_err; |
| 36025 | 36171 | } |
| 36026 | 36172 | assert( (*ppPage)->pgno==pgno ); |
| 36027 | 36173 | assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 ); |
| 36028 | 36174 | |
| 36029 | | - if( (*ppPage)->pPager ){ |
| 36175 | + if( (*ppPage)->pPager && !noContent ){ |
| 36030 | 36176 | /* In this case the pcache already contains an initialized copy of |
| 36031 | 36177 | ** the page. Return without further ado. */ |
| 36032 | 36178 | assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) ); |
| 36033 | 36179 | PAGER_INCR(pPager->nHit); |
| 36034 | 36180 | return SQLITE_OK; |
| | @@ -36184,10 +36330,11 @@ |
| 36184 | 36330 | ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or |
| 36185 | 36331 | ** an IO error code if opening or writing the journal file fails. |
| 36186 | 36332 | */ |
| 36187 | 36333 | static int pager_open_journal(Pager *pPager){ |
| 36188 | 36334 | int rc = SQLITE_OK; /* Return code */ |
| 36335 | + int nPage; /* Size of database file */ |
| 36189 | 36336 | sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */ |
| 36190 | 36337 | |
| 36191 | 36338 | assert( pPager->state>=PAGER_RESERVED ); |
| 36192 | 36339 | assert( pPager->useJournal ); |
| 36193 | 36340 | assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF ); |
| | @@ -36196,17 +36343,14 @@ |
| 36196 | 36343 | /* If already in the error state, this function is a no-op. But on |
| 36197 | 36344 | ** the other hand, this routine is never called if we are already in |
| 36198 | 36345 | ** an error state. */ |
| 36199 | 36346 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 36200 | 36347 | |
| 36201 | | - /* TODO: Is it really possible to get here with dbSizeValid==0? If not, |
| 36202 | | - ** the call to PagerPagecount() can be removed. |
| 36203 | | - */ |
| 36204 | 36348 | testcase( pPager->dbSizeValid==0 ); |
| 36205 | | - sqlite3PagerPagecount(pPager, 0); |
| 36206 | | - |
| 36207 | | - pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); |
| 36349 | + rc = sqlite3PagerPagecount(pPager, &nPage); |
| 36350 | + if( rc ) return rc; |
| 36351 | + pPager->pInJournal = sqlite3BitvecCreate(nPage); |
| 36208 | 36352 | if( pPager->pInJournal==0 ){ |
| 36209 | 36353 | return SQLITE_NOMEM; |
| 36210 | 36354 | } |
| 36211 | 36355 | |
| 36212 | 36356 | /* Open the journal file if it is not already open. */ |
| | @@ -36301,16 +36445,15 @@ |
| 36301 | 36445 | if( exFlag ){ |
| 36302 | 36446 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
| 36303 | 36447 | } |
| 36304 | 36448 | } |
| 36305 | 36449 | |
| 36306 | | - /* If the required locks were successfully obtained, open the journal |
| 36307 | | - ** file and write the first journal-header to it. |
| 36450 | + /* No need to open the journal file at this time. It will be |
| 36451 | + ** opened before it is written to. If we defer opening the journal, |
| 36452 | + ** we might save the work of creating a file if the transaction |
| 36453 | + ** ends up being a no-op. |
| 36308 | 36454 | */ |
| 36309 | | - if( rc==SQLITE_OK && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
| 36310 | | - rc = pager_open_journal(pPager); |
| 36311 | | - } |
| 36312 | 36455 | }else if( isOpen(pPager->jfd) && pPager->journalOff==0 ){ |
| 36313 | 36456 | /* This happens when the pager was in exclusive-access mode the last |
| 36314 | 36457 | ** time a (read or write) transaction was successfully concluded |
| 36315 | 36458 | ** by this connection. Instead of deleting the journal file it was |
| 36316 | 36459 | ** kept open and either was truncated to 0 bytes or its header was |
| | @@ -36321,11 +36464,10 @@ |
| 36321 | 36464 | assert( pPager->pInJournal==0 ); |
| 36322 | 36465 | rc = pager_open_journal(pPager); |
| 36323 | 36466 | } |
| 36324 | 36467 | |
| 36325 | 36468 | PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager))); |
| 36326 | | - assert( !isOpen(pPager->jfd) || pPager->journalOff>0 || rc!=SQLITE_OK ); |
| 36327 | 36469 | if( rc!=SQLITE_OK ){ |
| 36328 | 36470 | assert( !pPager->dbModified ); |
| 36329 | 36471 | /* Ignore any IO error that occurs within pager_end_transaction(). The |
| 36330 | 36472 | ** purpose of this call is to reset the internal state of the pager |
| 36331 | 36473 | ** sub-system. It doesn't matter if the journal-file is not properly |
| | @@ -36351,12 +36493,12 @@ |
| 36351 | 36493 | /* This routine is not called unless a transaction has already been |
| 36352 | 36494 | ** started. |
| 36353 | 36495 | */ |
| 36354 | 36496 | assert( pPager->state>=PAGER_RESERVED ); |
| 36355 | 36497 | |
| 36356 | | - /* If an error has been previously detected, we should not be |
| 36357 | | - ** calling this routine. Repeat the error for robustness. |
| 36498 | + /* If an error has been previously detected, report the same error |
| 36499 | + ** again. |
| 36358 | 36500 | */ |
| 36359 | 36501 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 36360 | 36502 | |
| 36361 | 36503 | /* Higher-level routines never call this function if database is not |
| 36362 | 36504 | ** writable. But check anyway, just for robustness. */ |
| | @@ -36377,15 +36519,15 @@ |
| 36377 | 36519 | /* If we get this far, it means that the page needs to be |
| 36378 | 36520 | ** written to the transaction journal or the ckeckpoint journal |
| 36379 | 36521 | ** or both. |
| 36380 | 36522 | ** |
| 36381 | 36523 | ** Higher level routines should have already started a transaction, |
| 36382 | | - ** which means they have acquired the necessary locks and opened |
| 36383 | | - ** a rollback journal. Double-check to makes sure this is the case. |
| 36524 | + ** which means they have acquired the necessary locks but the rollback |
| 36525 | + ** journal might not yet be open. |
| 36384 | 36526 | */ |
| 36385 | 36527 | rc = sqlite3PagerBegin(pPager, 0, pPager->subjInMemory); |
| 36386 | | - if( NEVER(rc!=SQLITE_OK) ){ |
| 36528 | + if( rc!=SQLITE_OK ){ |
| 36387 | 36529 | return rc; |
| 36388 | 36530 | } |
| 36389 | 36531 | if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
| 36390 | 36532 | assert( pPager->useJournal ); |
| 36391 | 36533 | rc = pager_open_journal(pPager); |
| | @@ -36523,11 +36665,12 @@ |
| 36523 | 36665 | ** an integer power of 2. It sets variable pg1 to the identifier |
| 36524 | 36666 | ** of the first page of the sector pPg is located on. |
| 36525 | 36667 | */ |
| 36526 | 36668 | pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; |
| 36527 | 36669 | |
| 36528 | | - sqlite3PagerPagecount(pPager, (int *)&nPageCount); |
| 36670 | + rc = sqlite3PagerPagecount(pPager, (int *)&nPageCount); |
| 36671 | + if( rc ) return rc; |
| 36529 | 36672 | if( pPg->pgno>nPageCount ){ |
| 36530 | 36673 | nPage = (pPg->pgno - pg1)+1; |
| 36531 | 36674 | }else if( (pg1+nPagePerSector-1)>nPageCount ){ |
| 36532 | 36675 | nPage = nPageCount+1-pg1; |
| 36533 | 36676 | }else{ |
| | @@ -36684,10 +36827,13 @@ |
| 36684 | 36827 | /* Increment the value just read and write it back to byte 24. */ |
| 36685 | 36828 | change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers); |
| 36686 | 36829 | change_counter++; |
| 36687 | 36830 | put32bits(((char*)pPgHdr->pData)+24, change_counter); |
| 36688 | 36831 | |
| 36832 | + /* Also store the SQLite version number in bytes 96..99 */ |
| 36833 | + put32bits(((char*)pPgHdr->pData)+96, SQLITE_VERSION_NUMBER); |
| 36834 | + |
| 36689 | 36835 | /* If running in direct mode, write the contents of page 1 to the file. */ |
| 36690 | 36836 | if( DIRECT_MODE ){ |
| 36691 | 36837 | const void *zBuf = pPgHdr->pData; |
| 36692 | 36838 | assert( pPager->dbFileSize>0 ); |
| 36693 | 36839 | rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); |
| | @@ -36757,13 +36903,11 @@ |
| 36757 | 36903 | int rc = SQLITE_OK; /* Return code */ |
| 36758 | 36904 | |
| 36759 | 36905 | /* The dbOrigSize is never set if journal_mode=OFF */ |
| 36760 | 36906 | assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 ); |
| 36761 | 36907 | |
| 36762 | | - /* If a prior error occurred, this routine should not be called. ROLLBACK |
| 36763 | | - ** is the appropriate response to an error, not COMMIT. Guard against |
| 36764 | | - ** coding errors by repeating the prior error. */ |
| 36908 | + /* If a prior error occurred, report that error again. */ |
| 36765 | 36909 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 36766 | 36910 | |
| 36767 | 36911 | PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", |
| 36768 | 36912 | pPager->zFilename, zMaster, pPager->dbSize)); |
| 36769 | 36913 | |
| | @@ -37048,10 +37192,20 @@ |
| 37048 | 37192 | ** Return the number of references to the pager. |
| 37049 | 37193 | */ |
| 37050 | 37194 | SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){ |
| 37051 | 37195 | return sqlite3PcacheRefCount(pPager->pPCache); |
| 37052 | 37196 | } |
| 37197 | + |
| 37198 | +/* |
| 37199 | +** Return the approximate number of bytes of memory currently |
| 37200 | +** used by the pager and its associated cache. |
| 37201 | +*/ |
| 37202 | +SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){ |
| 37203 | + int perPageSize = pPager->pageSize + pPager->nExtra + 20; |
| 37204 | + return perPageSize*sqlite3PcachePagecount(pPager->pPCache) |
| 37205 | + + sqlite3MallocSize(pPager); |
| 37206 | +} |
| 37053 | 37207 | |
| 37054 | 37208 | /* |
| 37055 | 37209 | ** Return the number of references to the specified page. |
| 37056 | 37210 | */ |
| 37057 | 37211 | SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){ |
| | @@ -37101,15 +37255,14 @@ |
| 37101 | 37255 | int nCurrent = pPager->nSavepoint; /* Current number of savepoints */ |
| 37102 | 37256 | |
| 37103 | 37257 | if( nSavepoint>nCurrent && pPager->useJournal ){ |
| 37104 | 37258 | int ii; /* Iterator variable */ |
| 37105 | 37259 | PagerSavepoint *aNew; /* New Pager.aSavepoint array */ |
| 37260 | + int nPage; /* Size of database file */ |
| 37106 | 37261 | |
| 37107 | | - /* Either there is no active journal or the sub-journal is open or |
| 37108 | | - ** the journal is always stored in memory */ |
| 37109 | | - assert( pPager->nSavepoint==0 || isOpen(pPager->sjfd) || |
| 37110 | | - pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); |
| 37262 | + rc = sqlite3PagerPagecount(pPager, &nPage); |
| 37263 | + if( rc ) return rc; |
| 37111 | 37264 | |
| 37112 | 37265 | /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM |
| 37113 | 37266 | ** if the allocation fails. Otherwise, zero the new portion in case a |
| 37114 | 37267 | ** malloc failure occurs while populating it in the for(...) loop below. |
| 37115 | 37268 | */ |
| | @@ -37123,19 +37276,18 @@ |
| 37123 | 37276 | pPager->aSavepoint = aNew; |
| 37124 | 37277 | pPager->nSavepoint = nSavepoint; |
| 37125 | 37278 | |
| 37126 | 37279 | /* Populate the PagerSavepoint structures just allocated. */ |
| 37127 | 37280 | for(ii=nCurrent; ii<nSavepoint; ii++){ |
| 37128 | | - assert( pPager->dbSizeValid ); |
| 37129 | | - aNew[ii].nOrig = pPager->dbSize; |
| 37130 | | - if( isOpen(pPager->jfd) && ALWAYS(pPager->journalOff>0) ){ |
| 37281 | + aNew[ii].nOrig = nPage; |
| 37282 | + if( isOpen(pPager->jfd) && pPager->journalOff>0 ){ |
| 37131 | 37283 | aNew[ii].iOffset = pPager->journalOff; |
| 37132 | 37284 | }else{ |
| 37133 | 37285 | aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); |
| 37134 | 37286 | } |
| 37135 | 37287 | aNew[ii].iSubRec = pPager->nSubRec; |
| 37136 | | - aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); |
| 37288 | + aNew[ii].pInSavepoint = sqlite3BitvecCreate(nPage); |
| 37137 | 37289 | if( !aNew[ii].pInSavepoint ){ |
| 37138 | 37290 | return SQLITE_NOMEM; |
| 37139 | 37291 | } |
| 37140 | 37292 | } |
| 37141 | 37293 | |
| | @@ -37518,10 +37670,19 @@ |
| 37518 | 37670 | && (!isOpen(pPager->jfd) || 0==pPager->journalOff) |
| 37519 | 37671 | ){ |
| 37520 | 37672 | if( isOpen(pPager->jfd) ){ |
| 37521 | 37673 | sqlite3OsClose(pPager->jfd); |
| 37522 | 37674 | } |
| 37675 | + assert( (PAGER_JOURNALMODE_TRUNCATE & 1)==1 ); |
| 37676 | + assert( (PAGER_JOURNALMODE_PERSIST & 1)==1 ); |
| 37677 | + assert( (PAGER_JOURNALMODE_DELETE & 1)==0 ); |
| 37678 | + assert( (PAGER_JOURNALMODE_MEMORY & 1)==0 ); |
| 37679 | + assert( (PAGER_JOURNALMODE_OFF & 1)==0 ); |
| 37680 | + if( (pPager->journalMode & 1)==1 && (eMode & 1)==0 |
| 37681 | + && !pPager->exclusiveMode ){ |
| 37682 | + sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); |
| 37683 | + } |
| 37523 | 37684 | pPager->journalMode = (u8)eMode; |
| 37524 | 37685 | } |
| 37525 | 37686 | return (int)pPager->journalMode; |
| 37526 | 37687 | } |
| 37527 | 37688 | |
| | @@ -37978,10 +38139,11 @@ |
| 37978 | 38139 | BtCursor *pCursor; /* A list of all open cursors */ |
| 37979 | 38140 | MemPage *pPage1; /* First page of the database */ |
| 37980 | 38141 | u8 readOnly; /* True if the underlying file is readonly */ |
| 37981 | 38142 | u8 pageSizeFixed; /* True if the page size can no longer be changed */ |
| 37982 | 38143 | u8 secureDelete; /* True if secure_delete is enabled */ |
| 38144 | + u8 initiallyEmpty; /* Database is empty at start of transaction */ |
| 37983 | 38145 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 37984 | 38146 | u8 autoVacuum; /* True if auto-vacuum is enabled */ |
| 37985 | 38147 | u8 incrVacuum; /* True if incr-vacuum is enabled */ |
| 37986 | 38148 | #endif |
| 37987 | 38149 | u16 pageSize; /* Total number of bytes on a page */ |
| | @@ -37990,10 +38152,11 @@ |
| 37990 | 38152 | u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 37991 | 38153 | u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 37992 | 38154 | u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ |
| 37993 | 38155 | u8 inTransaction; /* Transaction state */ |
| 37994 | 38156 | int nTransaction; /* Number of open transactions (read + write) */ |
| 38157 | + u32 nPage; /* Number of pages in the database */ |
| 37995 | 38158 | void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ |
| 37996 | 38159 | void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ |
| 37997 | 38160 | sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */ |
| 37998 | 38161 | Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */ |
| 37999 | 38162 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| | @@ -39070,15 +39233,12 @@ |
| 39070 | 39233 | ** at the end of every transaction. |
| 39071 | 39234 | */ |
| 39072 | 39235 | static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ |
| 39073 | 39236 | int rc = SQLITE_OK; |
| 39074 | 39237 | if( !pBt->pHasContent ){ |
| 39075 | | - int nPage = 100; |
| 39076 | | - sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 39077 | | - /* If sqlite3PagerPagecount() fails there is no harm because the |
| 39078 | | - ** nPage variable is unchanged from its default value of 100 */ |
| 39079 | | - pBt->pHasContent = sqlite3BitvecCreate((u32)nPage); |
| 39238 | + assert( pgno<=pBt->nPage ); |
| 39239 | + pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage); |
| 39080 | 39240 | if( !pBt->pHasContent ){ |
| 39081 | 39241 | rc = SQLITE_NOMEM; |
| 39082 | 39242 | } |
| 39083 | 39243 | } |
| 39084 | 39244 | if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){ |
| | @@ -40117,17 +40277,17 @@ |
| 40117 | 40277 | |
| 40118 | 40278 | /* |
| 40119 | 40279 | ** Return the size of the database file in pages. If there is any kind of |
| 40120 | 40280 | ** error, return ((unsigned int)-1). |
| 40121 | 40281 | */ |
| 40122 | | -static Pgno pagerPagecount(BtShared *pBt){ |
| 40123 | | - int nPage = -1; |
| 40124 | | - int rc; |
| 40125 | | - assert( pBt->pPage1 ); |
| 40126 | | - rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 40127 | | - assert( rc==SQLITE_OK || nPage==-1 ); |
| 40128 | | - return (Pgno)nPage; |
| 40282 | +static Pgno btreePagecount(BtShared *pBt){ |
| 40283 | + return pBt->nPage; |
| 40284 | +} |
| 40285 | +SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){ |
| 40286 | + assert( sqlite3BtreeHoldsMutex(p) ); |
| 40287 | + assert( ((p->pBt->nPage)&0x8000000)==0 ); |
| 40288 | + return (int)btreePagecount(p->pBt); |
| 40129 | 40289 | } |
| 40130 | 40290 | |
| 40131 | 40291 | /* |
| 40132 | 40292 | ** Get a page from the pager and initialize it. This routine is just a |
| 40133 | 40293 | ** convenience wrapper around separate calls to btreeGetPage() and |
| | @@ -40140,29 +40300,22 @@ |
| 40140 | 40300 | BtShared *pBt, /* The database file */ |
| 40141 | 40301 | Pgno pgno, /* Number of the page to get */ |
| 40142 | 40302 | MemPage **ppPage /* Write the page pointer here */ |
| 40143 | 40303 | ){ |
| 40144 | 40304 | int rc; |
| 40145 | | - TESTONLY( Pgno iLastPg = pagerPagecount(pBt); ) |
| 40146 | 40305 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 40147 | 40306 | |
| 40307 | + if( pgno<=0 || pgno>btreePagecount(pBt) ){ |
| 40308 | + return SQLITE_CORRUPT_BKPT; |
| 40309 | + } |
| 40148 | 40310 | rc = btreeGetPage(pBt, pgno, ppPage, 0); |
| 40149 | 40311 | if( rc==SQLITE_OK ){ |
| 40150 | 40312 | rc = btreeInitPage(*ppPage); |
| 40151 | 40313 | if( rc!=SQLITE_OK ){ |
| 40152 | 40314 | releasePage(*ppPage); |
| 40153 | 40315 | } |
| 40154 | 40316 | } |
| 40155 | | - |
| 40156 | | - /* If the requested page number was either 0 or greater than the page |
| 40157 | | - ** number of the last page in the database, this function should return |
| 40158 | | - ** SQLITE_CORRUPT or some other error (i.e. SQLITE_FULL). Check that this |
| 40159 | | - ** is the case. */ |
| 40160 | | - assert( (pgno>0 && pgno<=iLastPg) || rc!=SQLITE_OK ); |
| 40161 | | - testcase( pgno==0 ); |
| 40162 | | - testcase( pgno==iLastPg ); |
| 40163 | | - |
| 40164 | 40317 | return rc; |
| 40165 | 40318 | } |
| 40166 | 40319 | |
| 40167 | 40320 | /* |
| 40168 | 40321 | ** Release a MemPage. This should be called once for each prior |
| | @@ -40794,13 +40947,15 @@ |
| 40794 | 40947 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 40795 | 40948 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
| 40796 | 40949 | ** is returned if we run out of memory. |
| 40797 | 40950 | */ |
| 40798 | 40951 | static int lockBtree(BtShared *pBt){ |
| 40799 | | - int rc; |
| 40800 | | - MemPage *pPage1; |
| 40801 | | - int nPage; |
| 40952 | + int rc; /* Result code from subfunctions */ |
| 40953 | + MemPage *pPage1; /* Page 1 of the database file */ |
| 40954 | + int nPage; /* Number of pages in the database */ |
| 40955 | + int nPageFile = 0; /* Number of pages in the database file */ |
| 40956 | + int nPageHeader; /* Number of pages in the database according to hdr */ |
| 40802 | 40957 | |
| 40803 | 40958 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 40804 | 40959 | assert( pBt->pPage1==0 ); |
| 40805 | 40960 | rc = sqlite3PagerSharedLock(pBt->pPager); |
| 40806 | 40961 | if( rc!=SQLITE_OK ) return rc; |
| | @@ -40808,14 +40963,18 @@ |
| 40808 | 40963 | if( rc!=SQLITE_OK ) return rc; |
| 40809 | 40964 | |
| 40810 | 40965 | /* Do some checking to help insure the file we opened really is |
| 40811 | 40966 | ** a valid database file. |
| 40812 | 40967 | */ |
| 40813 | | - rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 40814 | | - if( rc!=SQLITE_OK ){ |
| 40968 | + nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData); |
| 40969 | + if( (rc = sqlite3PagerPagecount(pBt->pPager, &nPageFile))!=SQLITE_OK ){; |
| 40815 | 40970 | goto page1_init_failed; |
| 40816 | | - }else if( nPage>0 ){ |
| 40971 | + } |
| 40972 | + if( nPage==0 ){ |
| 40973 | + nPage = nPageFile; |
| 40974 | + } |
| 40975 | + if( nPage>0 ){ |
| 40817 | 40976 | int pageSize; |
| 40818 | 40977 | int usableSize; |
| 40819 | 40978 | u8 *page1 = pPage1->aData; |
| 40820 | 40979 | rc = SQLITE_NOTADB; |
| 40821 | 40980 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
| | @@ -40857,10 +41016,14 @@ |
| 40857 | 41016 | freeTempSpace(pBt); |
| 40858 | 41017 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, |
| 40859 | 41018 | pageSize-usableSize); |
| 40860 | 41019 | return rc; |
| 40861 | 41020 | } |
| 41021 | + if( nPageHeader>nPageFile ){ |
| 41022 | + rc = SQLITE_CORRUPT_BKPT; |
| 41023 | + goto page1_init_failed; |
| 41024 | + } |
| 40862 | 41025 | if( usableSize<480 ){ |
| 40863 | 41026 | goto page1_init_failed; |
| 40864 | 41027 | } |
| 40865 | 41028 | pBt->pageSize = (u16)pageSize; |
| 40866 | 41029 | pBt->usableSize = (u16)usableSize; |
| | @@ -40887,10 +41050,11 @@ |
| 40887 | 41050 | pBt->minLocal = (pBt->usableSize-12)*32/255 - 23; |
| 40888 | 41051 | pBt->maxLeaf = pBt->usableSize - 35; |
| 40889 | 41052 | pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23; |
| 40890 | 41053 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
| 40891 | 41054 | pBt->pPage1 = pPage1; |
| 41055 | + pBt->nPage = nPage; |
| 40892 | 41056 | return SQLITE_OK; |
| 40893 | 41057 | |
| 40894 | 41058 | page1_init_failed: |
| 40895 | 41059 | releasePage(pPage1); |
| 40896 | 41060 | pBt->pPage1 = 0; |
| | @@ -40924,16 +41088,14 @@ |
| 40924 | 41088 | */ |
| 40925 | 41089 | static int newDatabase(BtShared *pBt){ |
| 40926 | 41090 | MemPage *pP1; |
| 40927 | 41091 | unsigned char *data; |
| 40928 | 41092 | int rc; |
| 40929 | | - int nPage; |
| 40930 | 41093 | |
| 40931 | 41094 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 40932 | | - rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 40933 | | - if( rc!=SQLITE_OK || nPage>0 ){ |
| 40934 | | - return rc; |
| 41095 | + if( pBt->nPage>0 ){ |
| 41096 | + return SQLITE_OK; |
| 40935 | 41097 | } |
| 40936 | 41098 | pP1 = pBt->pPage1; |
| 40937 | 41099 | assert( pP1!=0 ); |
| 40938 | 41100 | data = pP1->aData; |
| 40939 | 41101 | rc = sqlite3PagerWrite(pP1->pDbPage); |
| | @@ -40955,10 +41117,12 @@ |
| 40955 | 41117 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
| 40956 | 41118 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
| 40957 | 41119 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
| 40958 | 41120 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
| 40959 | 41121 | #endif |
| 41122 | + pBt->nPage = 1; |
| 41123 | + data[31] = 1; |
| 40960 | 41124 | return SQLITE_OK; |
| 40961 | 41125 | } |
| 40962 | 41126 | |
| 40963 | 41127 | /* |
| 40964 | 41128 | ** Attempt to start a new transaction. A write-transaction |
| | @@ -41044,10 +41208,11 @@ |
| 41044 | 41208 | ** page 1. So if some other shared-cache client already has a write-lock |
| 41045 | 41209 | ** on page 1, the transaction cannot be opened. */ |
| 41046 | 41210 | rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); |
| 41047 | 41211 | if( SQLITE_OK!=rc ) goto trans_begun; |
| 41048 | 41212 | |
| 41213 | + pBt->initiallyEmpty = pBt->nPage==0; |
| 41049 | 41214 | do { |
| 41050 | 41215 | /* Call lockBtree() until either pBt->pPage1 is populated or |
| 41051 | 41216 | ** lockBtree() returns something other than SQLITE_OK. lockBtree() |
| 41052 | 41217 | ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after |
| 41053 | 41218 | ** reading page 1 it discovers that the page-size of the database |
| | @@ -41323,16 +41488,16 @@ |
| 41323 | 41488 | ** which may or may not empty the freelist. A full autovacuum |
| 41324 | 41489 | ** has nFin>0. A "PRAGMA incremental_vacuum" has nFin==0. |
| 41325 | 41490 | */ |
| 41326 | 41491 | static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){ |
| 41327 | 41492 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 41493 | + int rc; |
| 41328 | 41494 | |
| 41329 | 41495 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 41330 | 41496 | assert( iLastPg>nFin ); |
| 41331 | 41497 | |
| 41332 | 41498 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 41333 | | - int rc; |
| 41334 | 41499 | u8 eType; |
| 41335 | 41500 | Pgno iPtrPage; |
| 41336 | 41501 | |
| 41337 | 41502 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 41338 | 41503 | if( nFreeList==0 ){ |
| | @@ -41404,11 +41569,11 @@ |
| 41404 | 41569 | if( nFin==0 ){ |
| 41405 | 41570 | iLastPg--; |
| 41406 | 41571 | while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){ |
| 41407 | 41572 | if( PTRMAP_ISPAGE(pBt, iLastPg) ){ |
| 41408 | 41573 | MemPage *pPg; |
| 41409 | | - int rc = btreeGetPage(pBt, iLastPg, &pPg, 0); |
| 41574 | + rc = btreeGetPage(pBt, iLastPg, &pPg, 0); |
| 41410 | 41575 | if( rc!=SQLITE_OK ){ |
| 41411 | 41576 | return rc; |
| 41412 | 41577 | } |
| 41413 | 41578 | rc = sqlite3PagerWrite(pPg->pDbPage); |
| 41414 | 41579 | releasePage(pPg); |
| | @@ -41417,10 +41582,11 @@ |
| 41417 | 41582 | } |
| 41418 | 41583 | } |
| 41419 | 41584 | iLastPg--; |
| 41420 | 41585 | } |
| 41421 | 41586 | sqlite3PagerTruncateImage(pBt->pPager, iLastPg); |
| 41587 | + pBt->nPage = iLastPg; |
| 41422 | 41588 | } |
| 41423 | 41589 | return SQLITE_OK; |
| 41424 | 41590 | } |
| 41425 | 41591 | |
| 41426 | 41592 | /* |
| | @@ -41439,11 +41605,15 @@ |
| 41439 | 41605 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 41440 | 41606 | if( !pBt->autoVacuum ){ |
| 41441 | 41607 | rc = SQLITE_DONE; |
| 41442 | 41608 | }else{ |
| 41443 | 41609 | invalidateAllOverflowCache(pBt); |
| 41444 | | - rc = incrVacuumStep(pBt, 0, pagerPagecount(pBt)); |
| 41610 | + rc = incrVacuumStep(pBt, 0, btreePagecount(pBt)); |
| 41611 | + if( rc==SQLITE_OK ){ |
| 41612 | + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 41613 | + put4byte(&pBt->pPage1->aData[28], pBt->nPage); |
| 41614 | + } |
| 41445 | 41615 | } |
| 41446 | 41616 | sqlite3BtreeLeave(p); |
| 41447 | 41617 | return rc; |
| 41448 | 41618 | } |
| 41449 | 41619 | |
| | @@ -41470,11 +41640,11 @@ |
| 41470 | 41640 | Pgno nPtrmap; /* Number of PtrMap pages to be freed */ |
| 41471 | 41641 | Pgno iFree; /* The next page to be freed */ |
| 41472 | 41642 | int nEntry; /* Number of entries on one ptrmap page */ |
| 41473 | 41643 | Pgno nOrig; /* Database size before freeing */ |
| 41474 | 41644 | |
| 41475 | | - nOrig = pagerPagecount(pBt); |
| 41645 | + nOrig = btreePagecount(pBt); |
| 41476 | 41646 | if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 41477 | 41647 | /* It is not possible to create a database for which the final page |
| 41478 | 41648 | ** is either a pointer-map page or the pending-byte page. If one |
| 41479 | 41649 | ** is encountered, this indicates corruption. |
| 41480 | 41650 | */ |
| | @@ -41495,15 +41665,16 @@ |
| 41495 | 41665 | |
| 41496 | 41666 | for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ |
| 41497 | 41667 | rc = incrVacuumStep(pBt, nFin, iFree); |
| 41498 | 41668 | } |
| 41499 | 41669 | if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ |
| 41500 | | - rc = SQLITE_OK; |
| 41501 | 41670 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 41502 | 41671 | put4byte(&pBt->pPage1->aData[32], 0); |
| 41503 | 41672 | put4byte(&pBt->pPage1->aData[36], 0); |
| 41673 | + put4byte(&pBt->pPage1->aData[28], nFin); |
| 41504 | 41674 | sqlite3PagerTruncateImage(pBt->pPager, nFin); |
| 41675 | + pBt->nPage = nFin; |
| 41505 | 41676 | } |
| 41506 | 41677 | if( rc!=SQLITE_OK ){ |
| 41507 | 41678 | sqlite3PagerRollback(pPager); |
| 41508 | 41679 | } |
| 41509 | 41680 | } |
| | @@ -41749,10 +41920,15 @@ |
| 41749 | 41920 | |
| 41750 | 41921 | /* The rollback may have destroyed the pPage1->aData value. So |
| 41751 | 41922 | ** call btreeGetPage() on page 1 again to make |
| 41752 | 41923 | ** sure pPage1->aData is set correctly. */ |
| 41753 | 41924 | if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
| 41925 | + int nPage = get4byte(28+(u8*)pPage1->aData); |
| 41926 | + testcase( nPage==0 ); |
| 41927 | + if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 41928 | + testcase( pBt->nPage!=nPage ); |
| 41929 | + pBt->nPage = nPage; |
| 41754 | 41930 | releasePage(pPage1); |
| 41755 | 41931 | } |
| 41756 | 41932 | assert( countWriteCursors(pBt)==0 ); |
| 41757 | 41933 | pBt->inTransaction = TRANS_READ; |
| 41758 | 41934 | } |
| | @@ -41786,21 +41962,17 @@ |
| 41786 | 41962 | sqlite3BtreeEnter(p); |
| 41787 | 41963 | assert( p->inTrans==TRANS_WRITE ); |
| 41788 | 41964 | assert( pBt->readOnly==0 ); |
| 41789 | 41965 | assert( iStatement>0 ); |
| 41790 | 41966 | assert( iStatement>p->db->nSavepoint ); |
| 41791 | | - if( NEVER(p->inTrans!=TRANS_WRITE || pBt->readOnly) ){ |
| 41792 | | - rc = SQLITE_INTERNAL; |
| 41793 | | - }else{ |
| 41794 | | - assert( pBt->inTransaction==TRANS_WRITE ); |
| 41795 | | - /* At the pager level, a statement transaction is a savepoint with |
| 41796 | | - ** an index greater than all savepoints created explicitly using |
| 41797 | | - ** SQL statements. It is illegal to open, release or rollback any |
| 41798 | | - ** such savepoints while the statement transaction savepoint is active. |
| 41799 | | - */ |
| 41800 | | - rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement); |
| 41801 | | - } |
| 41967 | + assert( pBt->inTransaction==TRANS_WRITE ); |
| 41968 | + /* At the pager level, a statement transaction is a savepoint with |
| 41969 | + ** an index greater than all savepoints created explicitly using |
| 41970 | + ** SQL statements. It is illegal to open, release or rollback any |
| 41971 | + ** such savepoints while the statement transaction savepoint is active. |
| 41972 | + */ |
| 41973 | + rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement); |
| 41802 | 41974 | sqlite3BtreeLeave(p); |
| 41803 | 41975 | return rc; |
| 41804 | 41976 | } |
| 41805 | 41977 | |
| 41806 | 41978 | /* |
| | @@ -41822,11 +41994,13 @@ |
| 41822 | 41994 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 41823 | 41995 | assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); |
| 41824 | 41996 | sqlite3BtreeEnter(p); |
| 41825 | 41997 | rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); |
| 41826 | 41998 | if( rc==SQLITE_OK ){ |
| 41999 | + if( iSavepoint<0 && pBt->initiallyEmpty ) pBt->nPage = 0; |
| 41827 | 42000 | rc = newDatabase(pBt); |
| 42001 | + pBt->nPage = get4byte(28 + pBt->pPage1->aData); |
| 41828 | 42002 | } |
| 41829 | 42003 | sqlite3BtreeLeave(p); |
| 41830 | 42004 | } |
| 41831 | 42005 | return rc; |
| 41832 | 42006 | } |
| | @@ -41888,11 +42062,11 @@ |
| 41888 | 42062 | assert( pBt->pPage1 && pBt->pPage1->aData ); |
| 41889 | 42063 | |
| 41890 | 42064 | if( NEVER(wrFlag && pBt->readOnly) ){ |
| 41891 | 42065 | return SQLITE_READONLY; |
| 41892 | 42066 | } |
| 41893 | | - if( iTable==1 && pagerPagecount(pBt)==0 ){ |
| 42067 | + if( iTable==1 && btreePagecount(pBt)==0 ){ |
| 41894 | 42068 | return SQLITE_EMPTY; |
| 41895 | 42069 | } |
| 41896 | 42070 | |
| 41897 | 42071 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 41898 | 42072 | ** variables and link the cursor into the BtShared list. */ |
| | @@ -42159,11 +42333,11 @@ |
| 42159 | 42333 | |
| 42160 | 42334 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 42161 | 42335 | iGuess++; |
| 42162 | 42336 | } |
| 42163 | 42337 | |
| 42164 | | - if( iGuess<=pagerPagecount(pBt) ){ |
| 42338 | + if( iGuess<=btreePagecount(pBt) ){ |
| 42165 | 42339 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 42166 | 42340 | if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| 42167 | 42341 | next = iGuess; |
| 42168 | 42342 | rc = SQLITE_DONE; |
| 42169 | 42343 | } |
| | @@ -43191,11 +43365,11 @@ |
| 43191 | 43365 | MemPage *pPrevTrunk = 0; |
| 43192 | 43366 | Pgno mxPage; /* Total size of the database file */ |
| 43193 | 43367 | |
| 43194 | 43368 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 43195 | 43369 | pPage1 = pBt->pPage1; |
| 43196 | | - mxPage = pagerPagecount(pBt); |
| 43370 | + mxPage = btreePagecount(pBt); |
| 43197 | 43371 | n = get4byte(&pPage1->aData[36]); |
| 43198 | 43372 | testcase( n==mxPage-1 ); |
| 43199 | 43373 | if( n>=mxPage ){ |
| 43200 | 43374 | return SQLITE_CORRUPT_BKPT; |
| 43201 | 43375 | } |
| | @@ -43387,39 +43561,39 @@ |
| 43387 | 43561 | pPrevTrunk = 0; |
| 43388 | 43562 | }while( searchList ); |
| 43389 | 43563 | }else{ |
| 43390 | 43564 | /* There are no pages on the freelist, so create a new page at the |
| 43391 | 43565 | ** end of the file */ |
| 43392 | | - int nPage = pagerPagecount(pBt); |
| 43393 | | - *pPgno = nPage + 1; |
| 43394 | | - |
| 43395 | | - if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 43396 | | - (*pPgno)++; |
| 43397 | | - } |
| 43566 | + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 43567 | + if( rc ) return rc; |
| 43568 | + pBt->nPage++; |
| 43569 | + if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++; |
| 43398 | 43570 | |
| 43399 | 43571 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 43400 | | - if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
| 43572 | + if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){ |
| 43401 | 43573 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 43402 | 43574 | ** at the end of the file instead of one. The first allocated page |
| 43403 | 43575 | ** becomes a new pointer-map page, the second is used by the caller. |
| 43404 | 43576 | */ |
| 43405 | 43577 | MemPage *pPg = 0; |
| 43406 | | - TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
| 43407 | | - assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 43408 | | - rc = btreeGetPage(pBt, *pPgno, &pPg, 0); |
| 43578 | + TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); |
| 43579 | + assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); |
| 43580 | + rc = btreeGetPage(pBt, pBt->nPage, &pPg, 1); |
| 43409 | 43581 | if( rc==SQLITE_OK ){ |
| 43410 | 43582 | rc = sqlite3PagerWrite(pPg->pDbPage); |
| 43411 | 43583 | releasePage(pPg); |
| 43412 | 43584 | } |
| 43413 | 43585 | if( rc ) return rc; |
| 43414 | | - (*pPgno)++; |
| 43415 | | - if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; } |
| 43586 | + pBt->nPage++; |
| 43587 | + if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; } |
| 43416 | 43588 | } |
| 43417 | 43589 | #endif |
| 43590 | + put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage); |
| 43591 | + *pPgno = pBt->nPage; |
| 43418 | 43592 | |
| 43419 | 43593 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 43420 | | - rc = btreeGetPage(pBt, *pPgno, ppPage, 0); |
| 43594 | + rc = btreeGetPage(pBt, *pPgno, ppPage, 1); |
| 43421 | 43595 | if( rc ) return rc; |
| 43422 | 43596 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
| 43423 | 43597 | if( rc!=SQLITE_OK ){ |
| 43424 | 43598 | releasePage(*ppPage); |
| 43425 | 43599 | } |
| | @@ -43605,11 +43779,11 @@ |
| 43605 | 43779 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 43606 | 43780 | assert( ovflPgno==0 || nOvfl>0 ); |
| 43607 | 43781 | while( nOvfl-- ){ |
| 43608 | 43782 | Pgno iNext = 0; |
| 43609 | 43783 | MemPage *pOvfl = 0; |
| 43610 | | - if( ovflPgno<2 || ovflPgno>pagerPagecount(pBt) ){ |
| 43784 | + if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){ |
| 43611 | 43785 | /* 0 is not a legal page number and page 1 cannot be an |
| 43612 | 43786 | ** overflow page. Therefore if ovflPgno<2 or past the end of the |
| 43613 | 43787 | ** file the database must be corrupt. */ |
| 43614 | 43788 | return SQLITE_CORRUPT_BKPT; |
| 43615 | 43789 | } |
| | @@ -45437,12 +45611,18 @@ |
| 45437 | 45611 | ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc); |
| 45438 | 45612 | if( rc ){ |
| 45439 | 45613 | releasePage(pRoot); |
| 45440 | 45614 | return rc; |
| 45441 | 45615 | } |
| 45616 | + |
| 45617 | + /* When the new root page was allocated, page 1 was made writable in |
| 45618 | + ** order either to increase the database filesize, or to decrement the |
| 45619 | + ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail. |
| 45620 | + */ |
| 45621 | + assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) ); |
| 45442 | 45622 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
| 45443 | | - if( rc ){ |
| 45623 | + if( NEVER(rc) ){ |
| 45444 | 45624 | releasePage(pRoot); |
| 45445 | 45625 | return rc; |
| 45446 | 45626 | } |
| 45447 | 45627 | |
| 45448 | 45628 | }else{ |
| | @@ -45478,11 +45658,11 @@ |
| 45478 | 45658 | int rc; |
| 45479 | 45659 | unsigned char *pCell; |
| 45480 | 45660 | int i; |
| 45481 | 45661 | |
| 45482 | 45662 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 45483 | | - if( pgno>pagerPagecount(pBt) ){ |
| 45663 | + if( pgno>btreePagecount(pBt) ){ |
| 45484 | 45664 | return SQLITE_CORRUPT_BKPT; |
| 45485 | 45665 | } |
| 45486 | 45666 | |
| 45487 | 45667 | rc = getAndInitPage(pBt, pgno, &pPage); |
| 45488 | 45668 | if( rc ) return rc; |
| | @@ -46229,11 +46409,11 @@ |
| 46229 | 46409 | sqlite3BtreeEnter(p); |
| 46230 | 46410 | assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE ); |
| 46231 | 46411 | nRef = sqlite3PagerRefcount(pBt->pPager); |
| 46232 | 46412 | sCheck.pBt = pBt; |
| 46233 | 46413 | sCheck.pPager = pBt->pPager; |
| 46234 | | - sCheck.nPage = pagerPagecount(sCheck.pBt); |
| 46414 | + sCheck.nPage = btreePagecount(sCheck.pBt); |
| 46235 | 46415 | sCheck.mxErr = mxErr; |
| 46236 | 46416 | sCheck.nErr = 0; |
| 46237 | 46417 | sCheck.mallocFailed = 0; |
| 46238 | 46418 | *pnErr = 0; |
| 46239 | 46419 | if( sCheck.nPage==0 ){ |
| | @@ -46831,13 +47011,12 @@ |
| 46831 | 47011 | } |
| 46832 | 47012 | |
| 46833 | 47013 | /* Now that there is a read-lock on the source database, query the |
| 46834 | 47014 | ** source pager for the number of pages in the database. |
| 46835 | 47015 | */ |
| 46836 | | - if( rc==SQLITE_OK ){ |
| 46837 | | - rc = sqlite3PagerPagecount(pSrcPager, &nSrcPage); |
| 46838 | | - } |
| 47016 | + nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc); |
| 47017 | + assert( nSrcPage>=0 ); |
| 46839 | 47018 | for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){ |
| 46840 | 47019 | const Pgno iSrcPg = p->iNext; /* Source page number */ |
| 46841 | 47020 | if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){ |
| 46842 | 47021 | DbPage *pSrcPg; /* Source page object */ |
| 46843 | 47022 | rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg); |
| | @@ -48986,11 +49165,11 @@ |
| 48986 | 49165 | nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; |
| 48987 | 49166 | pKeyInfo = sqlite3Malloc( nByte ); |
| 48988 | 49167 | pOp->p4.pKeyInfo = pKeyInfo; |
| 48989 | 49168 | if( pKeyInfo ){ |
| 48990 | 49169 | u8 *aSortOrder; |
| 48991 | | - memcpy(pKeyInfo, zP4, nByte); |
| 49170 | + memcpy((char*)pKeyInfo, zP4, nByte - nField); |
| 48992 | 49171 | aSortOrder = pKeyInfo->aSortOrder; |
| 48993 | 49172 | if( aSortOrder ){ |
| 48994 | 49173 | pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; |
| 48995 | 49174 | memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); |
| 48996 | 49175 | } |
| | @@ -53812,18 +53991,13 @@ |
| 53812 | 53991 | int i; |
| 53813 | 53992 | sqlite_int64 rowid; |
| 53814 | 53993 | Mem **apArg; |
| 53815 | 53994 | Mem *pX; |
| 53816 | 53995 | } ck; |
| 53817 | | - struct OP_Pagecount_stack_vars { |
| 53818 | | - int p1; |
| 53819 | | - int nPage; |
| 53820 | | - Pager *pPager; |
| 53821 | | - } cl; |
| 53822 | 53996 | struct OP_Trace_stack_vars { |
| 53823 | 53997 | char *zTrace; |
| 53824 | | - } cm; |
| 53998 | + } cl; |
| 53825 | 53999 | } u; |
| 53826 | 54000 | /* End automatically generated code |
| 53827 | 54001 | ********************************************************************/ |
| 53828 | 54002 | |
| 53829 | 54003 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
| | @@ -54646,11 +54820,11 @@ |
| 54646 | 54820 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ag.n ); |
| 54647 | 54821 | u.ag.pArg = &aMem[pOp->p2]; |
| 54648 | 54822 | for(u.ag.i=0; u.ag.i<u.ag.n; u.ag.i++, u.ag.pArg++){ |
| 54649 | 54823 | u.ag.apVal[u.ag.i] = u.ag.pArg; |
| 54650 | 54824 | sqlite3VdbeMemStoreType(u.ag.pArg); |
| 54651 | | - REGISTER_TRACE(pOp->p2, u.ag.pArg); |
| 54825 | + REGISTER_TRACE(pOp->p2+u.ag.i, u.ag.pArg); |
| 54652 | 54826 | } |
| 54653 | 54827 | |
| 54654 | 54828 | assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC ); |
| 54655 | 54829 | if( pOp->p4type==P4_FUNCDEF ){ |
| 54656 | 54830 | u.ag.ctx.pFunc = pOp->p4.pFunc; |
| | @@ -56363,14 +56537,14 @@ |
| 56363 | 56537 | |
| 56364 | 56538 | /* Opcode: OpenEphemeral P1 P2 * P4 * |
| 56365 | 56539 | ** |
| 56366 | 56540 | ** Open a new cursor P1 to a transient table. |
| 56367 | 56541 | ** The cursor is always opened read/write even if |
| 56368 | | -** the main database is read-only. The transient or virtual |
| 56542 | +** the main database is read-only. The ephemeral |
| 56369 | 56543 | ** table is deleted automatically when the cursor is closed. |
| 56370 | 56544 | ** |
| 56371 | | -** P2 is the number of columns in the virtual table. |
| 56545 | +** P2 is the number of columns in the ephemeral table. |
| 56372 | 56546 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 56373 | 56547 | ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure |
| 56374 | 56548 | ** that defines the format of keys in the index. |
| 56375 | 56549 | ** |
| 56376 | 56550 | ** This opcode was once called OpenTemp. But that created |
| | @@ -56377,10 +56551,18 @@ |
| 56377 | 56551 | ** confusion because the term "temp table", might refer either |
| 56378 | 56552 | ** to a TEMP table at the SQL level, or to a table opened by |
| 56379 | 56553 | ** this opcode. Then this opcode was call OpenVirtual. But |
| 56380 | 56554 | ** that created confusion with the whole virtual-table idea. |
| 56381 | 56555 | */ |
| 56556 | +/* Opcode: OpenAutoindex P1 P2 * P4 * |
| 56557 | +** |
| 56558 | +** This opcode works the same as OP_OpenEphemeral. It has a |
| 56559 | +** different name to distinguish its use. Tables created using |
| 56560 | +** by this opcode will be used for automatically created transient |
| 56561 | +** indices in joins. |
| 56562 | +*/ |
| 56563 | +case OP_OpenAutoindex: |
| 56382 | 56564 | case OP_OpenEphemeral: { |
| 56383 | 56565 | #if 0 /* local variables moved into u.ax */ |
| 56384 | 56566 | VdbeCursor *pCx; |
| 56385 | 56567 | #endif /* local variables moved into u.ax */ |
| 56386 | 56568 | static const int openFlags = |
| | @@ -57515,29 +57697,35 @@ |
| 57515 | 57697 | pc = pOp->p2 - 1; |
| 57516 | 57698 | } |
| 57517 | 57699 | break; |
| 57518 | 57700 | } |
| 57519 | 57701 | |
| 57520 | | -/* Opcode: Next P1 P2 * * * |
| 57702 | +/* Opcode: Next P1 P2 * * P5 |
| 57521 | 57703 | ** |
| 57522 | 57704 | ** Advance cursor P1 so that it points to the next key/data pair in its |
| 57523 | 57705 | ** table or index. If there are no more key/value pairs then fall through |
| 57524 | 57706 | ** to the following instruction. But if the cursor advance was successful, |
| 57525 | 57707 | ** jump immediately to P2. |
| 57526 | 57708 | ** |
| 57527 | 57709 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 57528 | 57710 | ** |
| 57711 | +** If P5 is positive and the jump is taken, then event counter |
| 57712 | +** number P5-1 in the prepared statement is incremented. |
| 57713 | +** |
| 57529 | 57714 | ** See also: Prev |
| 57530 | 57715 | */ |
| 57531 | | -/* Opcode: Prev P1 P2 * * * |
| 57716 | +/* Opcode: Prev P1 P2 * * P5 |
| 57532 | 57717 | ** |
| 57533 | 57718 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 57534 | 57719 | ** table or index. If there is no previous key/value pairs then fall through |
| 57535 | 57720 | ** to the following instruction. But if the cursor backup was successful, |
| 57536 | 57721 | ** jump immediately to P2. |
| 57537 | 57722 | ** |
| 57538 | 57723 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 57724 | +** |
| 57725 | +** If P5 is positive and the jump is taken, then event counter |
| 57726 | +** number P5-1 in the prepared statement is incremented. |
| 57539 | 57727 | */ |
| 57540 | 57728 | case OP_Prev: /* jump */ |
| 57541 | 57729 | case OP_Next: { /* jump */ |
| 57542 | 57730 | #if 0 /* local variables moved into u.bm */ |
| 57543 | 57731 | VdbeCursor *pC; |
| | @@ -57545,10 +57733,11 @@ |
| 57545 | 57733 | int res; |
| 57546 | 57734 | #endif /* local variables moved into u.bm */ |
| 57547 | 57735 | |
| 57548 | 57736 | CHECK_FOR_INTERRUPT; |
| 57549 | 57737 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 57738 | + assert( pOp->p5<=ArraySize(p->aCounter) ); |
| 57550 | 57739 | u.bm.pC = p->apCsr[pOp->p1]; |
| 57551 | 57740 | if( u.bm.pC==0 ){ |
| 57552 | 57741 | break; /* See ticket #2273 */ |
| 57553 | 57742 | } |
| 57554 | 57743 | u.bm.pCrsr = u.bm.pC->pCursor; |
| | @@ -58991,25 +59180,11 @@ |
| 58991 | 59180 | /* Opcode: Pagecount P1 P2 * * * |
| 58992 | 59181 | ** |
| 58993 | 59182 | ** Write the current number of pages in database P1 to memory cell P2. |
| 58994 | 59183 | */ |
| 58995 | 59184 | case OP_Pagecount: { /* out2-prerelease */ |
| 58996 | | -#if 0 /* local variables moved into u.cl */ |
| 58997 | | - int p1; |
| 58998 | | - int nPage; |
| 58999 | | - Pager *pPager; |
| 59000 | | -#endif /* local variables moved into u.cl */ |
| 59001 | | - |
| 59002 | | - u.cl.p1 = pOp->p1; |
| 59003 | | - u.cl.pPager = sqlite3BtreePager(db->aDb[u.cl.p1].pBt); |
| 59004 | | - rc = sqlite3PagerPagecount(u.cl.pPager, &u.cl.nPage); |
| 59005 | | - /* OP_Pagecount is always called from within a read transaction. The |
| 59006 | | - ** page count has already been successfully read and cached. So the |
| 59007 | | - ** sqlite3PagerPagecount() call above cannot fail. */ |
| 59008 | | - if( ALWAYS(rc==SQLITE_OK) ){ |
| 59009 | | - pOut->u.i = u.cl.nPage; |
| 59010 | | - } |
| 59185 | + pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); |
| 59011 | 59186 | break; |
| 59012 | 59187 | } |
| 59013 | 59188 | #endif |
| 59014 | 59189 | |
| 59015 | 59190 | #ifndef SQLITE_OMIT_TRACE |
| | @@ -59017,24 +59192,24 @@ |
| 59017 | 59192 | ** |
| 59018 | 59193 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 59019 | 59194 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
| 59020 | 59195 | */ |
| 59021 | 59196 | case OP_Trace: { |
| 59022 | | -#if 0 /* local variables moved into u.cm */ |
| 59197 | +#if 0 /* local variables moved into u.cl */ |
| 59023 | 59198 | char *zTrace; |
| 59024 | | -#endif /* local variables moved into u.cm */ |
| 59199 | +#endif /* local variables moved into u.cl */ |
| 59025 | 59200 | |
| 59026 | | - u.cm.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
| 59027 | | - if( u.cm.zTrace ){ |
| 59201 | + u.cl.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
| 59202 | + if( u.cl.zTrace ){ |
| 59028 | 59203 | if( db->xTrace ){ |
| 59029 | | - char *z = sqlite3VdbeExpandSql(p, u.cm.zTrace); |
| 59204 | + char *z = sqlite3VdbeExpandSql(p, u.cl.zTrace); |
| 59030 | 59205 | db->xTrace(db->pTraceArg, z); |
| 59031 | 59206 | sqlite3DbFree(db, z); |
| 59032 | 59207 | } |
| 59033 | 59208 | #ifdef SQLITE_DEBUG |
| 59034 | 59209 | if( (db->flags & SQLITE_SqlTrace)!=0 ){ |
| 59035 | | - sqlite3DebugPrintf("SQL-trace: %s\n", u.cm.zTrace); |
| 59210 | + sqlite3DebugPrintf("SQL-trace: %s\n", u.cl.zTrace); |
| 59036 | 59211 | } |
| 59037 | 59212 | #endif /* SQLITE_DEBUG */ |
| 59038 | 59213 | } |
| 59039 | 59214 | break; |
| 59040 | 59215 | } |
| | @@ -66537,16 +66712,20 @@ |
| 66537 | 66712 | int n = sqlite3_column_bytes(pStmt, 2); |
| 66538 | 66713 | if( n>24 ){ |
| 66539 | 66714 | n = 24; |
| 66540 | 66715 | } |
| 66541 | 66716 | pSample->nByte = (u8)n; |
| 66542 | | - pSample->u.z = sqlite3DbMallocRaw(dbMem, n); |
| 66543 | | - if( pSample->u.z ){ |
| 66544 | | - memcpy(pSample->u.z, z, n); |
| 66717 | + if( n < 1){ |
| 66718 | + pSample->u.z = 0; |
| 66545 | 66719 | }else{ |
| 66546 | | - db->mallocFailed = 1; |
| 66547 | | - break; |
| 66720 | + pSample->u.z = sqlite3DbMallocRaw(dbMem, n); |
| 66721 | + if( pSample->u.z ){ |
| 66722 | + memcpy(pSample->u.z, z, n); |
| 66723 | + }else{ |
| 66724 | + db->mallocFailed = 1; |
| 66725 | + break; |
| 66726 | + } |
| 66548 | 66727 | } |
| 66549 | 66728 | } |
| 66550 | 66729 | } |
| 66551 | 66730 | } |
| 66552 | 66731 | } |
| | @@ -75790,11 +75969,11 @@ |
| 75790 | 75969 | }else{ |
| 75791 | 75970 | for(j=0; j<pColumn->nId; j++){ |
| 75792 | 75971 | if( pColumn->a[j].idx==i ) break; |
| 75793 | 75972 | } |
| 75794 | 75973 | } |
| 75795 | | - if( pColumn && j>=pColumn->nId ){ |
| 75974 | + if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){ |
| 75796 | 75975 | sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); |
| 75797 | 75976 | }else if( useTempTable ){ |
| 75798 | 75977 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); |
| 75799 | 75978 | }else{ |
| 75800 | 75979 | assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
| | @@ -78086,10 +78265,13 @@ |
| 78086 | 78265 | { "count_changes", SQLITE_CountRows }, |
| 78087 | 78266 | { "empty_result_callbacks", SQLITE_NullCallback }, |
| 78088 | 78267 | { "legacy_file_format", SQLITE_LegacyFileFmt }, |
| 78089 | 78268 | { "fullfsync", SQLITE_FullFSync }, |
| 78090 | 78269 | { "reverse_unordered_selects", SQLITE_ReverseOrder }, |
| 78270 | +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 78271 | + { "automatic_index", SQLITE_AutoIndex }, |
| 78272 | +#endif |
| 78091 | 78273 | #ifdef SQLITE_DEBUG |
| 78092 | 78274 | { "sql_trace", SQLITE_SqlTrace }, |
| 78093 | 78275 | { "vdbe_listing", SQLITE_VdbeListing }, |
| 78094 | 78276 | { "vdbe_trace", SQLITE_VdbeTrace }, |
| 78095 | 78277 | #endif |
| | @@ -79967,10 +80149,11 @@ |
| 79967 | 80149 | } |
| 79968 | 80150 | |
| 79969 | 80151 | sqlite3VtabUnlockList(db); |
| 79970 | 80152 | |
| 79971 | 80153 | pParse->db = db; |
| 80154 | + pParse->nQueryLoop = (double)1; |
| 79972 | 80155 | if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ |
| 79973 | 80156 | char *zSqlCopy; |
| 79974 | 80157 | int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
| 79975 | 80158 | testcase( nBytes==mxLen ); |
| 79976 | 80159 | testcase( nBytes==mxLen+1 ); |
| | @@ -79988,10 +80171,11 @@ |
| 79988 | 80171 | pParse->zTail = &zSql[nBytes]; |
| 79989 | 80172 | } |
| 79990 | 80173 | }else{ |
| 79991 | 80174 | sqlite3RunParser(pParse, zSql, &zErrMsg); |
| 79992 | 80175 | } |
| 80176 | + assert( 1==(int)pParse->nQueryLoop ); |
| 79993 | 80177 | |
| 79994 | 80178 | if( db->mallocFailed ){ |
| 79995 | 80179 | pParse->rc = SQLITE_NOMEM; |
| 79996 | 80180 | } |
| 79997 | 80181 | if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK; |
| | @@ -83744,10 +83928,22 @@ |
| 83744 | 83928 | if( addrNext ){ |
| 83745 | 83929 | sqlite3VdbeResolveLabel(v, addrNext); |
| 83746 | 83930 | sqlite3ExprCacheClear(pParse); |
| 83747 | 83931 | } |
| 83748 | 83932 | } |
| 83933 | + |
| 83934 | + /* Before populating the accumulator registers, clear the column cache. |
| 83935 | + ** Otherwise, if any of the required column values are already present |
| 83936 | + ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value |
| 83937 | + ** to pC->iMem. But by the time the value is used, the original register |
| 83938 | + ** may have been used, invalidating the underlying buffer holding the |
| 83939 | + ** text or blob value. See ticket [883034dcb5]. |
| 83940 | + ** |
| 83941 | + ** Another solution would be to change the OP_SCopy used to copy cached |
| 83942 | + ** values to an OP_Copy. |
| 83943 | + */ |
| 83944 | + sqlite3ExprCacheClear(pParse); |
| 83749 | 83945 | for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){ |
| 83750 | 83946 | sqlite3ExprCode(pParse, pC->pExpr, pC->iMem); |
| 83751 | 83947 | } |
| 83752 | 83948 | pAggInfo->directMode = 0; |
| 83753 | 83949 | sqlite3ExprCacheClear(pParse); |
| | @@ -85557,10 +85753,11 @@ |
| 85557 | 85753 | pSubParse->db = db; |
| 85558 | 85754 | pSubParse->pTriggerTab = pTab; |
| 85559 | 85755 | pSubParse->pToplevel = pTop; |
| 85560 | 85756 | pSubParse->zAuthContext = pTrigger->zName; |
| 85561 | 85757 | pSubParse->eTriggerOp = pTrigger->op; |
| 85758 | + pSubParse->nQueryLoop = pParse->nQueryLoop; |
| 85562 | 85759 | |
| 85563 | 85760 | v = sqlite3GetVdbe(pSubParse); |
| 85564 | 85761 | if( v ){ |
| 85565 | 85762 | VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", |
| 85566 | 85763 | pTrigger->zName, onErrorText(orconf), |
| | @@ -87477,10 +87674,11 @@ |
| 87477 | 87674 | if( pParse==0 ){ |
| 87478 | 87675 | rc = SQLITE_NOMEM; |
| 87479 | 87676 | }else{ |
| 87480 | 87677 | pParse->declareVtab = 1; |
| 87481 | 87678 | pParse->db = db; |
| 87679 | + pParse->nQueryLoop = 1; |
| 87482 | 87680 | |
| 87483 | 87681 | if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) |
| 87484 | 87682 | && pParse->pNewTable |
| 87485 | 87683 | && !db->mallocFailed |
| 87486 | 87684 | && !pParse->pNewTable->pSelect |
| | @@ -87997,19 +88195,21 @@ |
| 87997 | 88195 | #define WHERE_COLUMN_EQ 0x00010000 /* x=EXPR or x IN (...) or x IS NULL */ |
| 87998 | 88196 | #define WHERE_COLUMN_RANGE 0x00020000 /* x<EXPR and/or x>EXPR */ |
| 87999 | 88197 | #define WHERE_COLUMN_IN 0x00040000 /* x IN (...) */ |
| 88000 | 88198 | #define WHERE_COLUMN_NULL 0x00080000 /* x IS NULL */ |
| 88001 | 88199 | #define WHERE_INDEXED 0x000f0000 /* Anything that uses an index */ |
| 88200 | +#define WHERE_NOT_FULLSCAN 0x000f3000 /* Does not do a full table scan */ |
| 88002 | 88201 | #define WHERE_IN_ABLE 0x000f1000 /* Able to support an IN operator */ |
| 88003 | 88202 | #define WHERE_TOP_LIMIT 0x00100000 /* x<EXPR or x<=EXPR constraint */ |
| 88004 | 88203 | #define WHERE_BTM_LIMIT 0x00200000 /* x>EXPR or x>=EXPR constraint */ |
| 88005 | 88204 | #define WHERE_IDX_ONLY 0x00800000 /* Use index only - omit table */ |
| 88006 | 88205 | #define WHERE_ORDERBY 0x01000000 /* Output will appear in correct order */ |
| 88007 | 88206 | #define WHERE_REVERSE 0x02000000 /* Scan in reverse order */ |
| 88008 | 88207 | #define WHERE_UNIQUE 0x04000000 /* Selects no more than one row */ |
| 88009 | 88208 | #define WHERE_VIRTUALTABLE 0x08000000 /* Use virtual-table processing */ |
| 88010 | 88209 | #define WHERE_MULTI_OR 0x10000000 /* OR using multiple indices */ |
| 88210 | +#define WHERE_TEMP_INDEX 0x20000000 /* Uses an ephemeral index */ |
| 88011 | 88211 | |
| 88012 | 88212 | /* |
| 88013 | 88213 | ** Initialize a preallocated WhereClause structure. |
| 88014 | 88214 | */ |
| 88015 | 88215 | static void whereClauseInit( |
| | @@ -89397,10 +89597,238 @@ |
| 89397 | 89597 | } |
| 89398 | 89598 | } |
| 89399 | 89599 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 89400 | 89600 | } |
| 89401 | 89601 | |
| 89602 | +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 89603 | +/* |
| 89604 | +** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 89605 | +** could be used with an index to access pSrc, assuming an appropriate |
| 89606 | +** index existed. |
| 89607 | +*/ |
| 89608 | +static int termCanDriveIndex( |
| 89609 | + WhereTerm *pTerm, /* WHERE clause term to check */ |
| 89610 | + struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 89611 | + Bitmask notReady /* Tables in outer loops of the join */ |
| 89612 | +){ |
| 89613 | + char aff; |
| 89614 | + if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
| 89615 | + if( pTerm->eOperator!=WO_EQ ) return 0; |
| 89616 | + if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
| 89617 | + aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 89618 | + if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 89619 | + return 1; |
| 89620 | +} |
| 89621 | +#endif |
| 89622 | + |
| 89623 | +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 89624 | +/* |
| 89625 | +** If the query plan for pSrc specified in pCost is a full table scan |
| 89626 | +** and indexing is allows (if there is no NOT INDEXED clause) and it |
| 89627 | +** possible to construct a transient index that would perform better |
| 89628 | +** than a full table scan even when the cost of constructing the index |
| 89629 | +** is taken into account, then alter the query plan to use the |
| 89630 | +** transient index. |
| 89631 | +*/ |
| 89632 | +static void bestAutomaticIndex( |
| 89633 | + Parse *pParse, /* The parsing context */ |
| 89634 | + WhereClause *pWC, /* The WHERE clause */ |
| 89635 | + struct SrcList_item *pSrc, /* The FROM clause term to search */ |
| 89636 | + Bitmask notReady, /* Mask of cursors that are not available */ |
| 89637 | + WhereCost *pCost /* Lowest cost query plan */ |
| 89638 | +){ |
| 89639 | + double nTableRow; /* Rows in the input table */ |
| 89640 | + double logN; /* log(nTableRow) */ |
| 89641 | + double costTempIdx; /* per-query cost of the transient index */ |
| 89642 | + WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 89643 | + WhereTerm *pWCEnd; /* End of pWC->a[] */ |
| 89644 | + Table *pTable; /* Table tht might be indexed */ |
| 89645 | + |
| 89646 | + if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){ |
| 89647 | + /* Automatic indices are disabled at run-time */ |
| 89648 | + return; |
| 89649 | + } |
| 89650 | + if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){ |
| 89651 | + /* We already have some kind of index in use for this query. */ |
| 89652 | + return; |
| 89653 | + } |
| 89654 | + if( pSrc->notIndexed ){ |
| 89655 | + /* The NOT INDEXED clause appears in the SQL. */ |
| 89656 | + return; |
| 89657 | + } |
| 89658 | + |
| 89659 | + assert( pParse->nQueryLoop >= (double)1 ); |
| 89660 | + nTableRow = pSrc->pIndex ? pSrc->pIndex->aiRowEst[0] : 1000000; |
| 89661 | + logN = estLog(nTableRow); |
| 89662 | + costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1); |
| 89663 | + if( costTempIdx>=pCost->rCost ){ |
| 89664 | + /* The cost of creating the transient table would be greater than |
| 89665 | + ** doing the full table scan */ |
| 89666 | + return; |
| 89667 | + } |
| 89668 | + |
| 89669 | + /* Search for any equality comparison term */ |
| 89670 | + pTable = pSrc->pTab; |
| 89671 | + pWCEnd = &pWC->a[pWC->nTerm]; |
| 89672 | + for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 89673 | + if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 89674 | + WHERETRACE(("auto-index reduces cost from %.2f to %.2f\n", |
| 89675 | + pCost->rCost, costTempIdx)); |
| 89676 | + pCost->rCost = costTempIdx; |
| 89677 | + pCost->nRow = logN + 1; |
| 89678 | + pCost->plan.wsFlags = WHERE_TEMP_INDEX; |
| 89679 | + pCost->used = pTerm->prereqRight; |
| 89680 | + break; |
| 89681 | + } |
| 89682 | + } |
| 89683 | +} |
| 89684 | +#else |
| 89685 | +# define bestAutomaticIndex(A,B,C,D,E) /* no-op */ |
| 89686 | +#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 89687 | + |
| 89688 | + |
| 89689 | +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 89690 | +/* |
| 89691 | +** Generate code to construct the Index object for an automatic index |
| 89692 | +** and to set up the WhereLevel object pLevel so that the code generator |
| 89693 | +** makes use of the automatic index. |
| 89694 | +*/ |
| 89695 | +static void constructAutomaticIndex( |
| 89696 | + Parse *pParse, /* The parsing context */ |
| 89697 | + WhereClause *pWC, /* The WHERE clause */ |
| 89698 | + struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 89699 | + Bitmask notReady, /* Mask of cursors that are not available */ |
| 89700 | + WhereLevel *pLevel /* Write new index here */ |
| 89701 | +){ |
| 89702 | + int nColumn; /* Number of columns in the constructed index */ |
| 89703 | + WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 89704 | + WhereTerm *pWCEnd; /* End of pWC->a[] */ |
| 89705 | + int nByte; /* Byte of memory needed for pIdx */ |
| 89706 | + Index *pIdx; /* Object describing the transient index */ |
| 89707 | + Vdbe *v; /* Prepared statement under construction */ |
| 89708 | + int regIsInit; /* Register set by initialization */ |
| 89709 | + int addrInit; /* Address of the initialization bypass jump */ |
| 89710 | + Table *pTable; /* The table being indexed */ |
| 89711 | + KeyInfo *pKeyinfo; /* Key information for the index */ |
| 89712 | + int addrTop; /* Top of the index fill loop */ |
| 89713 | + int regRecord; /* Register holding an index record */ |
| 89714 | + int n; /* Column counter */ |
| 89715 | + int i; /* Loop counter */ |
| 89716 | + int mxBitCol; /* Maximum column in pSrc->colUsed */ |
| 89717 | + CollSeq *pColl; /* Collating sequence to on a column */ |
| 89718 | + Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 89719 | + Bitmask extraCols; /* Bitmap of additional columns */ |
| 89720 | + |
| 89721 | + /* Generate code to skip over the creation and initialization of the |
| 89722 | + ** transient index on 2nd and subsequent iterations of the loop. */ |
| 89723 | + v = pParse->pVdbe; |
| 89724 | + assert( v!=0 ); |
| 89725 | + regIsInit = ++pParse->nMem; |
| 89726 | + addrInit = sqlite3VdbeAddOp1(v, OP_If, regIsInit); |
| 89727 | + sqlite3VdbeAddOp2(v, OP_Integer, 1, regIsInit); |
| 89728 | + |
| 89729 | + /* Count the number of columns that will be added to the index |
| 89730 | + ** and used to match WHERE clause constraints */ |
| 89731 | + nColumn = 0; |
| 89732 | + pTable = pSrc->pTab; |
| 89733 | + pWCEnd = &pWC->a[pWC->nTerm]; |
| 89734 | + idxCols = 0; |
| 89735 | + for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 89736 | + if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 89737 | + int iCol = pTerm->u.leftColumn; |
| 89738 | + if( iCol<BMS && iCol>=0 ) idxCols |= 1<<iCol; |
| 89739 | + nColumn++; |
| 89740 | + } |
| 89741 | + } |
| 89742 | + assert( nColumn>0 ); |
| 89743 | + pLevel->plan.nEq = nColumn; |
| 89744 | + |
| 89745 | + /* Count the number of additional columns needed to create a |
| 89746 | + ** covering index. A "covering index" is an index that contains all |
| 89747 | + ** columns that are needed by the query. With a covering index, the |
| 89748 | + ** original table never needs to be accessed. Automatic indices must |
| 89749 | + ** be a covering index because the index will not be updated if the |
| 89750 | + ** original table changes and the index and table cannot both be used |
| 89751 | + ** if they go out of sync. |
| 89752 | + */ |
| 89753 | + extraCols = pSrc->colUsed & ~idxCols; |
| 89754 | + mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
| 89755 | + for(i=0; i<mxBitCol; i++){ |
| 89756 | + if( extraCols & (1<<i) ) nColumn++; |
| 89757 | + } |
| 89758 | + if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){ |
| 89759 | + nColumn += pTable->nCol - BMS + 1; |
| 89760 | + } |
| 89761 | + pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ; |
| 89762 | + |
| 89763 | + /* Construct the Index object to describe this index */ |
| 89764 | + nByte = sizeof(Index); |
| 89765 | + nByte += nColumn*sizeof(int); /* Index.aiColumn */ |
| 89766 | + nByte += nColumn*sizeof(char*); /* Index.azColl */ |
| 89767 | + nByte += nColumn; /* Index.aSortOrder */ |
| 89768 | + pIdx = sqlite3DbMallocZero(pParse->db, nByte); |
| 89769 | + if( pIdx==0 ) return; |
| 89770 | + pLevel->plan.u.pIdx = pIdx; |
| 89771 | + pIdx->azColl = (char**)&pIdx[1]; |
| 89772 | + pIdx->aiColumn = (int*)&pIdx->azColl[nColumn]; |
| 89773 | + pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn]; |
| 89774 | + pIdx->zName = "auto-index"; |
| 89775 | + pIdx->nColumn = nColumn; |
| 89776 | + pIdx->pTable = pTable; |
| 89777 | + n = 0; |
| 89778 | + for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 89779 | + if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 89780 | + Expr *pX = pTerm->pExpr; |
| 89781 | + pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 89782 | + pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
| 89783 | + pIdx->azColl[n] = pColl->zName; |
| 89784 | + n++; |
| 89785 | + } |
| 89786 | + } |
| 89787 | + assert( n==pLevel->plan.nEq ); |
| 89788 | + |
| 89789 | + /* Add additional columns needed to make the automatic index into |
| 89790 | + ** a covering index */ |
| 89791 | + for(i=0; i<mxBitCol; i++){ |
| 89792 | + if( extraCols & (1<<i) ){ |
| 89793 | + pIdx->aiColumn[n] = i; |
| 89794 | + pIdx->azColl[n] = "BINARY"; |
| 89795 | + n++; |
| 89796 | + } |
| 89797 | + } |
| 89798 | + if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){ |
| 89799 | + for(i=BMS-1; i<pTable->nCol; i++){ |
| 89800 | + pIdx->aiColumn[n] = i; |
| 89801 | + pIdx->azColl[n] = "BINARY"; |
| 89802 | + n++; |
| 89803 | + } |
| 89804 | + } |
| 89805 | + assert( n==nColumn ); |
| 89806 | + |
| 89807 | + /* Create the automatic index */ |
| 89808 | + pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); |
| 89809 | + assert( pLevel->iIdxCur>=0 ); |
| 89810 | + sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, |
| 89811 | + (char*)pKeyinfo, P4_KEYINFO_HANDOFF); |
| 89812 | + VdbeComment((v, "for %s", pTable->zName)); |
| 89813 | + |
| 89814 | + /* Fill the automatic index with content */ |
| 89815 | + addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); |
| 89816 | + regRecord = sqlite3GetTempReg(pParse); |
| 89817 | + sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1); |
| 89818 | + sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 89819 | + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 89820 | + sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); |
| 89821 | + sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
| 89822 | + sqlite3VdbeJumpHere(v, addrTop); |
| 89823 | + sqlite3ReleaseTempReg(pParse, regRecord); |
| 89824 | + |
| 89825 | + /* Jump here when skipping the initialization */ |
| 89826 | + sqlite3VdbeJumpHere(v, addrInit); |
| 89827 | +} |
| 89828 | +#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 89829 | + |
| 89402 | 89830 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 89403 | 89831 | /* |
| 89404 | 89832 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 89405 | 89833 | ** responsibility of the caller to eventually release the structure |
| 89406 | 89834 | ** by passing the pointer returned by this function to sqlite3_free(). |
| | @@ -89581,10 +90009,11 @@ |
| 89581 | 90009 | struct sqlite3_index_constraint *pIdxCons; |
| 89582 | 90010 | struct sqlite3_index_constraint_usage *pUsage; |
| 89583 | 90011 | WhereTerm *pTerm; |
| 89584 | 90012 | int i, j; |
| 89585 | 90013 | int nOrderBy; |
| 90014 | + double rCost; |
| 89586 | 90015 | |
| 89587 | 90016 | /* Make sure wsFlags is initialized to some sane value. Otherwise, if the |
| 89588 | 90017 | ** malloc in allocateIndexInfo() fails and this function returns leaving |
| 89589 | 90018 | ** wsFlags in an uninitialized state, the caller may behave unpredictably. |
| 89590 | 90019 | */ |
| | @@ -89666,22 +90095,31 @@ |
| 89666 | 90095 | for(i=0; i<pIdxInfo->nConstraint; i++){ |
| 89667 | 90096 | if( pUsage[i].argvIndex>0 ){ |
| 89668 | 90097 | pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight; |
| 89669 | 90098 | } |
| 89670 | 90099 | } |
| 90100 | + |
| 90101 | + /* If there is an ORDER BY clause, and the selected virtual table index |
| 90102 | + ** does not satisfy it, increase the cost of the scan accordingly. This |
| 90103 | + ** matches the processing for non-virtual tables in bestBtreeIndex(). |
| 90104 | + */ |
| 90105 | + rCost = pIdxInfo->estimatedCost; |
| 90106 | + if( pOrderBy && pIdxInfo->orderByConsumed==0 ){ |
| 90107 | + rCost += estLog(rCost)*rCost; |
| 90108 | + } |
| 89671 | 90109 | |
| 89672 | 90110 | /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the |
| 89673 | 90111 | ** inital value of lowestCost in this loop. If it is, then the |
| 89674 | 90112 | ** (cost<lowestCost) test below will never be true. |
| 89675 | 90113 | ** |
| 89676 | 90114 | ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT |
| 89677 | 90115 | ** is defined. |
| 89678 | 90116 | */ |
| 89679 | | - if( (SQLITE_BIG_DBL/((double)2))<pIdxInfo->estimatedCost ){ |
| 90117 | + if( (SQLITE_BIG_DBL/((double)2))<rCost ){ |
| 89680 | 90118 | pCost->rCost = (SQLITE_BIG_DBL/((double)2)); |
| 89681 | 90119 | }else{ |
| 89682 | | - pCost->rCost = pIdxInfo->estimatedCost; |
| 90120 | + pCost->rCost = rCost; |
| 89683 | 90121 | } |
| 89684 | 90122 | pCost->plan.u.pVtabIdx = pIdxInfo; |
| 89685 | 90123 | if( pIdxInfo->orderByConsumed ){ |
| 89686 | 90124 | pCost->plan.wsFlags |= WHERE_ORDERBY; |
| 89687 | 90125 | } |
| | @@ -90175,11 +90613,11 @@ |
| 90175 | 90613 | } |
| 90176 | 90614 | } |
| 90177 | 90615 | |
| 90178 | 90616 | /* If currently calculating the cost of using an index (not the IPK |
| 90179 | 90617 | ** index), determine if all required column data may be obtained without |
| 90180 | | - ** seeking to entries in the main table (i.e. if the index is a covering |
| 90618 | + ** using the main table (i.e. if the index is a covering |
| 90181 | 90619 | ** index for this query). If it is, set the WHERE_IDX_ONLY flag in |
| 90182 | 90620 | ** wsFlags. Otherwise, set the bLookup variable to true. */ |
| 90183 | 90621 | if( pIdx && wsFlags ){ |
| 90184 | 90622 | Bitmask m = pSrc->colUsed; |
| 90185 | 90623 | int j; |
| | @@ -90233,14 +90671,14 @@ |
| 90233 | 90671 | cost /= (double)2; |
| 90234 | 90672 | } |
| 90235 | 90673 | /**** Cost of using this index has now been computed ****/ |
| 90236 | 90674 | |
| 90237 | 90675 | WHERETRACE(( |
| 90238 | | - "tbl=%s idx=%s nEq=%d nInMul=%d nBound=%d bSort=%d bLookup=%d" |
| 90239 | | - " wsFlags=%d (nRow=%.2f cost=%.2f)\n", |
| 90676 | + "%s(%s): nEq=%d nInMul=%d nBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n" |
| 90677 | + " notReady=0x%llx nRow=%.2f cost=%.2f used=0x%llx\n", |
| 90240 | 90678 | pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), |
| 90241 | | - nEq, nInMul, nBound, bSort, bLookup, wsFlags, nRow, cost |
| 90679 | + nEq, nInMul, nBound, bSort, bLookup, wsFlags, notReady, nRow, cost, used |
| 90242 | 90680 | )); |
| 90243 | 90681 | |
| 90244 | 90682 | /* If this index is the best we have seen so far, then record this |
| 90245 | 90683 | ** index and its cost in the pCost structure. |
| 90246 | 90684 | */ |
| | @@ -90281,10 +90719,11 @@ |
| 90281 | 90719 | WHERETRACE(("best index is: %s\n", |
| 90282 | 90720 | (pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk") |
| 90283 | 90721 | )); |
| 90284 | 90722 | |
| 90285 | 90723 | bestOrClauseIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost); |
| 90724 | + bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost); |
| 90286 | 90725 | pCost->plan.wsFlags |= eqTermMask; |
| 90287 | 90726 | } |
| 90288 | 90727 | |
| 90289 | 90728 | /* |
| 90290 | 90729 | ** Find the query plan for accessing table pSrc->pTab. Write the |
| | @@ -90750,11 +91189,15 @@ |
| 90750 | 91189 | } |
| 90751 | 91190 | start = sqlite3VdbeCurrentAddr(v); |
| 90752 | 91191 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 90753 | 91192 | pLevel->p1 = iCur; |
| 90754 | 91193 | pLevel->p2 = start; |
| 90755 | | - pLevel->p5 = (pStart==0 && pEnd==0) ?1:0; |
| 91194 | + if( pStart==0 && pEnd==0 ){ |
| 91195 | + pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 91196 | + }else{ |
| 91197 | + assert( pLevel->p5==0 ); |
| 91198 | + } |
| 90756 | 91199 | if( testOp!=OP_Noop ){ |
| 90757 | 91200 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 90758 | 91201 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
| 90759 | 91202 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 90760 | 91203 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| | @@ -91219,10 +91662,17 @@ |
| 91219 | 91662 | if( pInfo->needToFreeIdxStr ){ |
| 91220 | 91663 | sqlite3_free(pInfo->idxStr); |
| 91221 | 91664 | } |
| 91222 | 91665 | sqlite3DbFree(db, pInfo); |
| 91223 | 91666 | } |
| 91667 | + if( pWInfo->a[i].plan.wsFlags & WHERE_TEMP_INDEX ){ |
| 91668 | + Index *pIdx = pWInfo->a[i].plan.u.pIdx; |
| 91669 | + if( pIdx ){ |
| 91670 | + sqlite3DbFree(db, pIdx->zColAff); |
| 91671 | + sqlite3DbFree(db, pIdx); |
| 91672 | + } |
| 91673 | + } |
| 91224 | 91674 | } |
| 91225 | 91675 | whereClauseClear(pWInfo->pWC); |
| 91226 | 91676 | sqlite3DbFree(db, pWInfo); |
| 91227 | 91677 | } |
| 91228 | 91678 | } |
| | @@ -91365,18 +91815,21 @@ |
| 91365 | 91815 | nByteWInfo + |
| 91366 | 91816 | sizeof(WhereClause) + |
| 91367 | 91817 | sizeof(WhereMaskSet) |
| 91368 | 91818 | ); |
| 91369 | 91819 | if( db->mallocFailed ){ |
| 91820 | + sqlite3DbFree(db, pWInfo); |
| 91821 | + pWInfo = 0; |
| 91370 | 91822 | goto whereBeginError; |
| 91371 | 91823 | } |
| 91372 | 91824 | pWInfo->nLevel = nTabList; |
| 91373 | 91825 | pWInfo->pParse = pParse; |
| 91374 | 91826 | pWInfo->pTabList = pTabList; |
| 91375 | 91827 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
| 91376 | 91828 | pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo]; |
| 91377 | 91829 | pWInfo->wctrlFlags = wctrlFlags; |
| 91830 | + pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
| 91378 | 91831 | pMaskSet = (WhereMaskSet*)&pWC[1]; |
| 91379 | 91832 | |
| 91380 | 91833 | /* Split the WHERE clause into separate subexpressions where each |
| 91381 | 91834 | ** subexpression is separated by an AND operator. |
| 91382 | 91835 | */ |
| | @@ -91552,17 +92005,20 @@ |
| 91552 | 92005 | if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){ |
| 91553 | 92006 | *ppOrderBy = 0; |
| 91554 | 92007 | } |
| 91555 | 92008 | andFlags &= bestPlan.plan.wsFlags; |
| 91556 | 92009 | pLevel->plan = bestPlan.plan; |
| 91557 | | - if( bestPlan.plan.wsFlags & WHERE_INDEXED ){ |
| 92010 | + testcase( bestPlan.plan.wsFlags & WHERE_INDEXED ); |
| 92011 | + testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX ); |
| 92012 | + if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){ |
| 91558 | 92013 | pLevel->iIdxCur = pParse->nTab++; |
| 91559 | 92014 | }else{ |
| 91560 | 92015 | pLevel->iIdxCur = -1; |
| 91561 | 92016 | } |
| 91562 | 92017 | notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor); |
| 91563 | 92018 | pLevel->iFrom = (u8)bestJ; |
| 92019 | + if( bestPlan.nRow>=(double)1 ) pParse->nQueryLoop *= bestPlan.nRow; |
| 91564 | 92020 | |
| 91565 | 92021 | /* Check that if the table scanned by this loop iteration had an |
| 91566 | 92022 | ** INDEXED BY clause attached to it, that the named index is being |
| 91567 | 92023 | ** used for the scan. If not, then query compilation has failed. |
| 91568 | 92024 | ** Return an error. |
| | @@ -91605,10 +92061,11 @@ |
| 91605 | 92061 | |
| 91606 | 92062 | /* Open all tables in the pTabList and any indices selected for |
| 91607 | 92063 | ** searching those tables. |
| 91608 | 92064 | */ |
| 91609 | 92065 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
| 92066 | + notReady = ~(Bitmask)0; |
| 91610 | 92067 | for(i=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){ |
| 91611 | 92068 | Table *pTab; /* Table to open */ |
| 91612 | 92069 | int iDb; /* Index of database containing table/index */ |
| 91613 | 92070 | |
| 91614 | 92071 | #ifndef SQLITE_OMIT_EXPLAIN |
| | @@ -91617,11 +92074,13 @@ |
| 91617 | 92074 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 91618 | 92075 | zMsg = sqlite3MPrintf(db, "TABLE %s", pItem->zName); |
| 91619 | 92076 | if( pItem->zAlias ){ |
| 91620 | 92077 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 91621 | 92078 | } |
| 91622 | | - if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ |
| 92079 | + if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
| 92080 | + zMsg = sqlite3MAppendf(db, zMsg, "%s WITH AUTOMATIC INDEX", zMsg); |
| 92081 | + }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ |
| 91623 | 92082 | zMsg = sqlite3MAppendf(db, zMsg, "%s WITH INDEX %s", |
| 91624 | 92083 | zMsg, pLevel->plan.u.pIdx->zName); |
| 91625 | 92084 | }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){ |
| 91626 | 92085 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIA MULTI-INDEX UNION", zMsg); |
| 91627 | 92086 | }else if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){ |
| | @@ -91640,12 +92099,15 @@ |
| 91640 | 92099 | sqlite3VdbeAddOp4(v, OP_Explain, i, pLevel->iFrom, 0, zMsg, P4_DYNAMIC); |
| 91641 | 92100 | } |
| 91642 | 92101 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 91643 | 92102 | pTabItem = &pTabList->a[pLevel->iFrom]; |
| 91644 | 92103 | pTab = pTabItem->pTab; |
| 92104 | + pLevel->iTabCur = pTabItem->iCursor; |
| 91645 | 92105 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 91646 | | - if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ) continue; |
| 92106 | + if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
| 92107 | + /* Do nothing */ |
| 92108 | + }else |
| 91647 | 92109 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 91648 | 92110 | if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 91649 | 92111 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
| 91650 | 92112 | int iCur = pTabItem->iCursor; |
| 91651 | 92113 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
| | @@ -91664,11 +92126,15 @@ |
| 91664 | 92126 | assert( n<=pTab->nCol ); |
| 91665 | 92127 | } |
| 91666 | 92128 | }else{ |
| 91667 | 92129 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 91668 | 92130 | } |
| 91669 | | - pLevel->iTabCur = pTabItem->iCursor; |
| 92131 | +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 92132 | + if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
| 92133 | + constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel); |
| 92134 | + }else |
| 92135 | +#endif |
| 91670 | 92136 | if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ |
| 91671 | 92137 | Index *pIx = pLevel->plan.u.pIdx; |
| 91672 | 92138 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); |
| 91673 | 92139 | int iIdxCur = pLevel->iIdxCur; |
| 91674 | 92140 | assert( pIx->pSchema==pTab->pSchema ); |
| | @@ -91676,12 +92142,14 @@ |
| 91676 | 92142 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb, |
| 91677 | 92143 | (char*)pKey, P4_KEYINFO_HANDOFF); |
| 91678 | 92144 | VdbeComment((v, "%s", pIx->zName)); |
| 91679 | 92145 | } |
| 91680 | 92146 | sqlite3CodeVerifySchema(pParse, iDb); |
| 92147 | + notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor); |
| 91681 | 92148 | } |
| 91682 | 92149 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
| 92150 | + if( db->mallocFailed ) goto whereBeginError; |
| 91683 | 92151 | |
| 91684 | 92152 | /* Generate the code to do the search. Each iteration of the for |
| 91685 | 92153 | ** loop below generates code for a single nested loop of the VM |
| 91686 | 92154 | ** program. |
| 91687 | 92155 | */ |
| | @@ -91745,11 +92213,14 @@ |
| 91745 | 92213 | */ |
| 91746 | 92214 | return pWInfo; |
| 91747 | 92215 | |
| 91748 | 92216 | /* Jump here if malloc fails */ |
| 91749 | 92217 | whereBeginError: |
| 91750 | | - whereInfoFree(db, pWInfo); |
| 92218 | + if( pWInfo ){ |
| 92219 | + pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 92220 | + whereInfoFree(db, pWInfo); |
| 92221 | + } |
| 91751 | 92222 | return 0; |
| 91752 | 92223 | } |
| 91753 | 92224 | |
| 91754 | 92225 | /* |
| 91755 | 92226 | ** Generate the end of the WHERE loop. See comments on |
| | @@ -91815,16 +92286,19 @@ |
| 91815 | 92286 | assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc ); |
| 91816 | 92287 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
| 91817 | 92288 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
| 91818 | 92289 | Table *pTab = pTabItem->pTab; |
| 91819 | 92290 | assert( pTab!=0 ); |
| 91820 | | - if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ) continue; |
| 91821 | | - if( (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0 ){ |
| 91822 | | - if( !pWInfo->okOnePass && (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){ |
| 92291 | + if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 92292 | + && pTab->pSelect==0 |
| 92293 | + && (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0 |
| 92294 | + ){ |
| 92295 | + int ws = pLevel->plan.wsFlags; |
| 92296 | + if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
| 91823 | 92297 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 91824 | 92298 | } |
| 91825 | | - if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ |
| 92299 | + if( (ws & (WHERE_INDEXED|WHERE_TEMP_INDEX)) == WHERE_INDEXED ){ |
| 91826 | 92300 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 91827 | 92301 | } |
| 91828 | 92302 | } |
| 91829 | 92303 | |
| 91830 | 92304 | /* If this scan uses an index, make code substitutions to read data |
| | @@ -91868,11 +92342,14 @@ |
| 91868 | 92342 | } |
| 91869 | 92343 | } |
| 91870 | 92344 | |
| 91871 | 92345 | /* Final cleanup |
| 91872 | 92346 | */ |
| 91873 | | - whereInfoFree(db, pWInfo); |
| 92347 | + if( pWInfo ){ |
| 92348 | + pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 92349 | + whereInfoFree(db, pWInfo); |
| 92350 | + } |
| 91874 | 92351 | return; |
| 91875 | 92352 | } |
| 91876 | 92353 | |
| 91877 | 92354 | /************** End of where.c ***********************************************/ |
| 91878 | 92355 | /************** Begin file parse.c *******************************************/ |
| | @@ -98071,11 +98548,11 @@ |
| 98071 | 98548 | assert( sizeof(db->aLimit)==sizeof(aHardLimit) ); |
| 98072 | 98549 | memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit)); |
| 98073 | 98550 | db->autoCommit = 1; |
| 98074 | 98551 | db->nextAutovac = -1; |
| 98075 | 98552 | db->nextPagesize = 0; |
| 98076 | | - db->flags |= SQLITE_ShortColNames |
| 98553 | + db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex |
| 98077 | 98554 | #if SQLITE_DEFAULT_FILE_FORMAT<4 |
| 98078 | 98555 | | SQLITE_LegacyFileFmt |
| 98079 | 98556 | #endif |
| 98080 | 98557 | #ifdef SQLITE_ENABLE_LOAD_EXTENSION |
| 98081 | 98558 | | SQLITE_LoadExtension |
| | @@ -99199,11 +99676,11 @@ |
| 99199 | 99676 | ** and so on. |
| 99200 | 99677 | ** |
| 99201 | 99678 | ** This is similar in concept to how sqlite encodes "varints" but |
| 99202 | 99679 | ** the encoding is not the same. SQLite varints are big-endian |
| 99203 | 99680 | ** are are limited to 9 bytes in length whereas FTS3 varints are |
| 99204 | | -** little-endian and can be upt to 10 bytes in length (in theory). |
| 99681 | +** little-endian and can be up to 10 bytes in length (in theory). |
| 99205 | 99682 | ** |
| 99206 | 99683 | ** Example encodings: |
| 99207 | 99684 | ** |
| 99208 | 99685 | ** 1: 0x01 |
| 99209 | 99686 | ** 127: 0x7f |
| | @@ -99210,30 +99687,30 @@ |
| 99210 | 99687 | ** 128: 0x81 0x00 |
| 99211 | 99688 | ** |
| 99212 | 99689 | ** |
| 99213 | 99690 | **** Document lists **** |
| 99214 | 99691 | ** A doclist (document list) holds a docid-sorted list of hits for a |
| 99215 | | -** given term. Doclists hold docids, and can optionally associate |
| 99216 | | -** token positions and offsets with docids. A position is the index |
| 99217 | | -** of a word within the document. The first word of the document has |
| 99218 | | -** a position of 0. |
| 99692 | +** given term. Doclists hold docids and associated token positions. |
| 99693 | +** A docid is the unique integer identifier for a single document. |
| 99694 | +** A position is the index of a word within the document. The first |
| 99695 | +** word of the document has a position of 0. |
| 99219 | 99696 | ** |
| 99220 | 99697 | ** FTS3 used to optionally store character offsets using a compile-time |
| 99221 | 99698 | ** option. But that functionality is no longer supported. |
| 99222 | 99699 | ** |
| 99223 | | -** A DL_POSITIONS_OFFSETS doclist is stored like this: |
| 99700 | +** A doclist is stored like this: |
| 99224 | 99701 | ** |
| 99225 | 99702 | ** array { |
| 99226 | 99703 | ** varint docid; |
| 99227 | 99704 | ** array { (position list for column 0) |
| 99228 | | -** varint position; (delta from previous position plus POS_BASE) |
| 99705 | +** varint position; (2 more than the delta from previous position) |
| 99229 | 99706 | ** } |
| 99230 | 99707 | ** array { |
| 99231 | 99708 | ** varint POS_COLUMN; (marks start of position list for new column) |
| 99232 | 99709 | ** varint column; (index of new column) |
| 99233 | 99710 | ** array { |
| 99234 | | -** varint position; (delta from previous position plus POS_BASE) |
| 99711 | +** varint position; (2 more than the delta from previous position) |
| 99235 | 99712 | ** } |
| 99236 | 99713 | ** } |
| 99237 | 99714 | ** varint POS_END; (marks end of positions for this document. |
| 99238 | 99715 | ** } |
| 99239 | 99716 | ** |
| | @@ -99241,11 +99718,11 @@ |
| 99241 | 99718 | ** memory. A "position" is an index of a token in the token stream |
| 99242 | 99719 | ** generated by the tokenizer. Note that POS_END and POS_COLUMN occur |
| 99243 | 99720 | ** in the same logical place as the position element, and act as sentinals |
| 99244 | 99721 | ** ending a position list array. POS_END is 0. POS_COLUMN is 1. |
| 99245 | 99722 | ** The positions numbers are not stored literally but rather as two more |
| 99246 | | -** the difference from the prior position, or the just the position plus |
| 99723 | +** than the difference from the prior position, or the just the position plus |
| 99247 | 99724 | ** 2 for the first position. Example: |
| 99248 | 99725 | ** |
| 99249 | 99726 | ** label: A B C D E F G H I J K |
| 99250 | 99727 | ** value: 123 5 9 1 1 14 35 0 234 72 0 |
| 99251 | 99728 | ** |
| | @@ -99255,18 +99732,18 @@ |
| 99255 | 99732 | ** new column is column number 1. There are two positions at 12 and 45 |
| 99256 | 99733 | ** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The |
| 99257 | 99734 | ** 234 at I is the next docid. It has one position 72 (72-2) and then |
| 99258 | 99735 | ** terminates with the 0 at K. |
| 99259 | 99736 | ** |
| 99260 | | -** A DL_POSITIONS doclist omits the startOffset and endOffset |
| 99261 | | -** information. A DL_DOCIDS doclist omits both the position and |
| 99262 | | -** offset information, becoming an array of varint-encoded docids. |
| 99263 | | -** |
| 99264 | | -** On-disk data is stored as type DL_DEFAULT, so we don't serialize |
| 99265 | | -** the type. Due to how deletion is implemented in the segmentation |
| 99266 | | -** system, on-disk doclists MUST store at least positions. |
| 99267 | | -** |
| 99737 | +** A "position-list" is the list of positions for multiple columns for |
| 99738 | +** a single docid. A "column-list" is the set of positions for a single |
| 99739 | +** column. Hence, a position-list consists of one or more column-lists, |
| 99740 | +** a document record consists of a docid followed by a position-list and |
| 99741 | +** a doclist consists of one or more document records. |
| 99742 | +** |
| 99743 | +** A bare doclist omits the position information, becoming an |
| 99744 | +** array of varint-encoded docids. |
| 99268 | 99745 | ** |
| 99269 | 99746 | **** Segment leaf nodes **** |
| 99270 | 99747 | ** Segment leaf nodes store terms and doclists, ordered by term. Leaf |
| 99271 | 99748 | ** nodes are written using LeafWriter, and read using LeafReader (to |
| 99272 | 99749 | ** iterate through a single leaf node's data) and LeavesReader (to |
| | @@ -99776,10 +100253,24 @@ |
| 99776 | 100253 | ** Maximum length of a varint encoded integer. The varint format is different |
| 99777 | 100254 | ** from that used by SQLite, so the maximum length is 10, not 9. |
| 99778 | 100255 | */ |
| 99779 | 100256 | #define FTS3_VARINT_MAX 10 |
| 99780 | 100257 | |
| 100258 | +/* |
| 100259 | +** The testcase() macro is only used by the amalgamation. If undefined, |
| 100260 | +** make it a no-op. |
| 100261 | +*/ |
| 100262 | +#ifndef testcase |
| 100263 | +# define testcase(X) |
| 100264 | +#endif |
| 100265 | + |
| 100266 | +/* |
| 100267 | +** Terminator values for position-lists and column-lists. |
| 100268 | +*/ |
| 100269 | +#define POS_COLUMN (1) /* Column-list terminator */ |
| 100270 | +#define POS_END (0) /* Position-list terminator */ |
| 100271 | + |
| 99781 | 100272 | /* |
| 99782 | 100273 | ** This section provides definitions to allow the |
| 99783 | 100274 | ** FTS3 extension to be compiled outside of the |
| 99784 | 100275 | ** amalgamation. |
| 99785 | 100276 | */ |
| | @@ -100087,12 +100578,11 @@ |
| 100087 | 100578 | *pi = (int) i; |
| 100088 | 100579 | return ret; |
| 100089 | 100580 | } |
| 100090 | 100581 | |
| 100091 | 100582 | /* |
| 100092 | | -** Return the number of bytes required to store the value passed as the |
| 100093 | | -** first argument in varint form. |
| 100583 | +** Return the number of bytes required to encode v as a varint |
| 100094 | 100584 | */ |
| 100095 | 100585 | SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){ |
| 100096 | 100586 | int i = 0; |
| 100097 | 100587 | do{ |
| 100098 | 100588 | i++; |
| | @@ -100139,11 +100629,11 @@ |
| 100139 | 100629 | } |
| 100140 | 100630 | } |
| 100141 | 100631 | |
| 100142 | 100632 | /* |
| 100143 | 100633 | ** Read a single varint from the doclist at *pp and advance *pp to point |
| 100144 | | -** to the next element of the varlist. Add the value of the varint |
| 100634 | +** to the first byte past the end of the varint. Add the value of the varint |
| 100145 | 100635 | ** to *pVal. |
| 100146 | 100636 | */ |
| 100147 | 100637 | static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){ |
| 100148 | 100638 | sqlite3_int64 iVal; |
| 100149 | 100639 | *pp += sqlite3Fts3GetVarint(*pp, &iVal); |
| | @@ -100195,11 +100685,11 @@ |
| 100195 | 100685 | ** and then evaluate those statements. The success code is writting |
| 100196 | 100686 | ** into *pRc. |
| 100197 | 100687 | ** |
| 100198 | 100688 | ** If *pRc is initially non-zero then this routine is a no-op. |
| 100199 | 100689 | */ |
| 100200 | | -void fts3DbExec( |
| 100690 | +static void fts3DbExec( |
| 100201 | 100691 | int *pRc, /* Success code */ |
| 100202 | 100692 | sqlite3 *db, /* Database in which to run SQL */ |
| 100203 | 100693 | const char *zFormat, /* Format string for SQL */ |
| 100204 | 100694 | ... /* Arguments to the format string */ |
| 100205 | 100695 | ){ |
| | @@ -100275,10 +100765,14 @@ |
| 100275 | 100765 | |
| 100276 | 100766 | /* |
| 100277 | 100767 | ** Create the backing store tables (%_content, %_segments and %_segdir) |
| 100278 | 100768 | ** required by the FTS3 table passed as the only argument. This is done |
| 100279 | 100769 | ** as part of the vtab xCreate() method. |
| 100770 | +** |
| 100771 | +** If the p->bHasDocsize boolean is true (indicating that this is an |
| 100772 | +** FTS4 table, not an FTS3 table) then also create the %_docsize and |
| 100773 | +** %_stat tables required by FTS4. |
| 100280 | 100774 | */ |
| 100281 | 100775 | static int fts3CreateTables(Fts3Table *p){ |
| 100282 | 100776 | int rc = SQLITE_OK; /* Return code */ |
| 100283 | 100777 | int i; /* Iterator variable */ |
| 100284 | 100778 | char *zContentCols; /* Columns of %_content table */ |
| | @@ -100367,11 +100861,11 @@ |
| 100367 | 100861 | ** This function is the implementation of both the xConnect and xCreate |
| 100368 | 100862 | ** methods of the FTS3 virtual table. |
| 100369 | 100863 | ** |
| 100370 | 100864 | ** The argv[] array contains the following: |
| 100371 | 100865 | ** |
| 100372 | | -** argv[0] -> module name |
| 100866 | +** argv[0] -> module name ("fts3" or "fts4") |
| 100373 | 100867 | ** argv[1] -> database name |
| 100374 | 100868 | ** argv[2] -> table name |
| 100375 | 100869 | ** argv[...] -> "column name" and other module argument fields. |
| 100376 | 100870 | */ |
| 100377 | 100871 | static int fts3InitVtab( |
| | @@ -100386,16 +100880,16 @@ |
| 100386 | 100880 | Fts3Hash *pHash = (Fts3Hash *)pAux; |
| 100387 | 100881 | Fts3Table *p; /* Pointer to allocated vtab */ |
| 100388 | 100882 | int rc; /* Return code */ |
| 100389 | 100883 | int i; /* Iterator variable */ |
| 100390 | 100884 | int nByte; /* Size of allocation used for *p */ |
| 100391 | | - int iCol; |
| 100392 | | - int nString = 0; |
| 100393 | | - int nCol = 0; |
| 100394 | | - char *zCsr; |
| 100395 | | - int nDb; |
| 100396 | | - int nName; |
| 100885 | + int iCol; /* Column index */ |
| 100886 | + int nString = 0; /* Bytes required to hold all column names */ |
| 100887 | + int nCol = 0; /* Number of columns in the FTS table */ |
| 100888 | + char *zCsr; /* Space for holding column names */ |
| 100889 | + int nDb; /* Bytes required to hold database name */ |
| 100890 | + int nName; /* Bytes required to hold table name */ |
| 100397 | 100891 | |
| 100398 | 100892 | const char *zTokenizer = 0; /* Name of tokenizer to use */ |
| 100399 | 100893 | sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */ |
| 100400 | 100894 | |
| 100401 | 100895 | nDb = (int)strlen(argv[1]) + 1; |
| | @@ -100621,10 +101115,15 @@ |
| 100621 | 101115 | sqlite3_free(pCsr->aMatchinfo); |
| 100622 | 101116 | sqlite3_free(pCsr); |
| 100623 | 101117 | return SQLITE_OK; |
| 100624 | 101118 | } |
| 100625 | 101119 | |
| 101120 | +/* |
| 101121 | +** Position the pCsr->pStmt statement so that it is on the row |
| 101122 | +** of the %_content table that contains the last match. Return |
| 101123 | +** SQLITE_OK on success. |
| 101124 | +*/ |
| 100626 | 101125 | static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){ |
| 100627 | 101126 | if( pCsr->isRequireSeek ){ |
| 100628 | 101127 | pCsr->isRequireSeek = 0; |
| 100629 | 101128 | sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId); |
| 100630 | 101129 | if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){ |
| | @@ -100647,10 +101146,21 @@ |
| 100647 | 101146 | }else{ |
| 100648 | 101147 | return SQLITE_OK; |
| 100649 | 101148 | } |
| 100650 | 101149 | } |
| 100651 | 101150 | |
| 101151 | +/* |
| 101152 | +** Advance the cursor to the next row in the %_content table that |
| 101153 | +** matches the search criteria. For a MATCH search, this will be |
| 101154 | +** the next row that matches. For a full-table scan, this will be |
| 101155 | +** simply the next row in the %_content table. For a docid lookup, |
| 101156 | +** this routine simply sets the EOF flag. |
| 101157 | +** |
| 101158 | +** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned |
| 101159 | +** even if we reach end-of-file. The fts3EofMethod() will be called |
| 101160 | +** subsequently to determine whether or not an EOF was hit. |
| 101161 | +*/ |
| 100652 | 101162 | static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){ |
| 100653 | 101163 | int rc = SQLITE_OK; /* Return code */ |
| 100654 | 101164 | Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; |
| 100655 | 101165 | |
| 100656 | 101166 | if( pCsr->aDoclist==0 ){ |
| | @@ -100782,10 +101292,15 @@ |
| 100782 | 101292 | |
| 100783 | 101293 | /* |
| 100784 | 101294 | ** When this function is called, *ppPoslist is assumed to point to the |
| 100785 | 101295 | ** start of a position-list. After it returns, *ppPoslist points to the |
| 100786 | 101296 | ** first byte after the position-list. |
| 101297 | +** |
| 101298 | +** A position list is list of positions (delta encoded) and columns for |
| 101299 | +** a single document record of a doclist. So, in other words, this |
| 101300 | +** routine advances *ppPoslist so that it points to the next docid in |
| 101301 | +** the doclist, or to the first byte past the end of the doclist. |
| 100787 | 101302 | ** |
| 100788 | 101303 | ** If pp is not NULL, then the contents of the position list are copied |
| 100789 | 101304 | ** to *pp. *pp is set to point to the first byte past the last byte copied |
| 100790 | 101305 | ** before this function returns. |
| 100791 | 101306 | */ |
| | @@ -100792,21 +101307,24 @@ |
| 100792 | 101307 | static void fts3PoslistCopy(char **pp, char **ppPoslist){ |
| 100793 | 101308 | char *pEnd = *ppPoslist; |
| 100794 | 101309 | char c = 0; |
| 100795 | 101310 | |
| 100796 | 101311 | /* The end of a position list is marked by a zero encoded as an FTS3 |
| 100797 | | - ** varint. A single 0x00 byte. Except, if the 0x00 byte is preceded by |
| 101312 | + ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by |
| 100798 | 101313 | ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail |
| 100799 | 101314 | ** of some other, multi-byte, value. |
| 100800 | 101315 | ** |
| 100801 | | - ** The following block moves pEnd to point to the first byte that is not |
| 101316 | + ** The following while-loop moves pEnd to point to the first byte that is not |
| 100802 | 101317 | ** immediately preceded by a byte with the 0x80 bit set. Then increments |
| 100803 | 101318 | ** pEnd once more so that it points to the byte immediately following the |
| 100804 | 101319 | ** last byte in the position-list. |
| 100805 | 101320 | */ |
| 100806 | | - while( *pEnd | c ) c = *pEnd++ & 0x80; |
| 100807 | | - pEnd++; |
| 101321 | + while( *pEnd | c ){ |
| 101322 | + c = *pEnd++ & 0x80; |
| 101323 | + testcase( c!=0 && (*pEnd)==0 ); |
| 101324 | + } |
| 101325 | + pEnd++; /* Advance past the POS_END terminator byte */ |
| 100808 | 101326 | |
| 100809 | 101327 | if( pp ){ |
| 100810 | 101328 | int n = (int)(pEnd - *ppPoslist); |
| 100811 | 101329 | char *p = *pp; |
| 100812 | 101330 | memcpy(p, *ppPoslist, n); |
| | @@ -100814,16 +101332,38 @@ |
| 100814 | 101332 | *pp = p; |
| 100815 | 101333 | } |
| 100816 | 101334 | *ppPoslist = pEnd; |
| 100817 | 101335 | } |
| 100818 | 101336 | |
| 101337 | +/* |
| 101338 | +** When this function is called, *ppPoslist is assumed to point to the |
| 101339 | +** start of a column-list. After it returns, *ppPoslist points to the |
| 101340 | +** to the terminator (POS_COLUMN or POS_END) byte of the column-list. |
| 101341 | +** |
| 101342 | +** A column-list is list of delta-encoded positions for a single column |
| 101343 | +** within a single document within a doclist. |
| 101344 | +** |
| 101345 | +** The column-list is terminated either by a POS_COLUMN varint (1) or |
| 101346 | +** a POS_END varint (0). This routine leaves *ppPoslist pointing to |
| 101347 | +** the POS_COLUMN or POS_END that terminates the column-list. |
| 101348 | +** |
| 101349 | +** If pp is not NULL, then the contents of the column-list are copied |
| 101350 | +** to *pp. *pp is set to point to the first byte past the last byte copied |
| 101351 | +** before this function returns. The POS_COLUMN or POS_END terminator |
| 101352 | +** is not copied into *pp. |
| 101353 | +*/ |
| 100819 | 101354 | static void fts3ColumnlistCopy(char **pp, char **ppPoslist){ |
| 100820 | 101355 | char *pEnd = *ppPoslist; |
| 100821 | 101356 | char c = 0; |
| 100822 | 101357 | |
| 100823 | | - /* A column-list is terminated by either a 0x01 or 0x00. */ |
| 100824 | | - while( 0xFE & (*pEnd | c) ) c = *pEnd++ & 0x80; |
| 101358 | + /* A column-list is terminated by either a 0x01 or 0x00 byte that is |
| 101359 | + ** not part of a multi-byte varint. |
| 101360 | + */ |
| 101361 | + while( 0xFE & (*pEnd | c) ){ |
| 101362 | + c = *pEnd++ & 0x80; |
| 101363 | + testcase( c!=0 && ((*pEnd)&0xfe)==0 ); |
| 101364 | + } |
| 100825 | 101365 | if( pp ){ |
| 100826 | 101366 | int n = (int)(pEnd - *ppPoslist); |
| 100827 | 101367 | char *p = *pp; |
| 100828 | 101368 | memcpy(p, *ppPoslist, n); |
| 100829 | 101369 | p += n; |
| | @@ -100831,41 +101371,49 @@ |
| 100831 | 101371 | } |
| 100832 | 101372 | *ppPoslist = pEnd; |
| 100833 | 101373 | } |
| 100834 | 101374 | |
| 100835 | 101375 | /* |
| 100836 | | -** Value used to signify the end of an offset-list. This is safe because |
| 101376 | +** Value used to signify the end of an position-list. This is safe because |
| 100837 | 101377 | ** it is not possible to have a document with 2^31 terms. |
| 100838 | 101378 | */ |
| 100839 | | -#define OFFSET_LIST_END 0x7fffffff |
| 101379 | +#define POSITION_LIST_END 0x7fffffff |
| 100840 | 101380 | |
| 100841 | 101381 | /* |
| 100842 | | -** This function is used to help parse offset-lists. When this function is |
| 100843 | | -** called, *pp may point to the start of the next varint in the offset-list |
| 100844 | | -** being parsed, or it may point to 1 byte past the end of the offset-list |
| 100845 | | -** (in which case **pp will be 0x00 or 0x01). |
| 101382 | +** This function is used to help parse position-lists. When this function is |
| 101383 | +** called, *pp may point to the start of the next varint in the position-list |
| 101384 | +** being parsed, or it may point to 1 byte past the end of the position-list |
| 101385 | +** (in which case **pp will be a terminator bytes POS_END (0) or |
| 101386 | +** (1)). |
| 100846 | 101387 | ** |
| 100847 | | -** If *pp points past the end of the current offset list, set *pi to |
| 100848 | | -** OFFSET_LIST_END and return. Otherwise, read the next varint from *pp, |
| 101388 | +** If *pp points past the end of the current position-list, set *pi to |
| 101389 | +** POSITION_LIST_END and return. Otherwise, read the next varint from *pp, |
| 100849 | 101390 | ** increment the current value of *pi by the value read, and set *pp to |
| 100850 | 101391 | ** point to the next value before returning. |
| 101392 | +** |
| 101393 | +** Before calling this routine *pi must be initialized to the value of |
| 101394 | +** the previous position, or zero if we are reading the first position |
| 101395 | +** in the position-list. Because positions are delta-encoded, the value |
| 101396 | +** of the previous position is needed in order to compute the value of |
| 101397 | +** the next position. |
| 100851 | 101398 | */ |
| 100852 | 101399 | static void fts3ReadNextPos( |
| 100853 | | - char **pp, /* IN/OUT: Pointer into offset-list buffer */ |
| 100854 | | - sqlite3_int64 *pi /* IN/OUT: Value read from offset-list */ |
| 101400 | + char **pp, /* IN/OUT: Pointer into position-list buffer */ |
| 101401 | + sqlite3_int64 *pi /* IN/OUT: Value read from position-list */ |
| 100855 | 101402 | ){ |
| 100856 | | - if( **pp&0xFE ){ |
| 101403 | + if( (**pp)&0xFE ){ |
| 100857 | 101404 | fts3GetDeltaVarint(pp, pi); |
| 100858 | 101405 | *pi -= 2; |
| 100859 | 101406 | }else{ |
| 100860 | | - *pi = OFFSET_LIST_END; |
| 101407 | + *pi = POSITION_LIST_END; |
| 100861 | 101408 | } |
| 100862 | 101409 | } |
| 100863 | 101410 | |
| 100864 | 101411 | /* |
| 100865 | | -** If parameter iCol is not 0, write an 0x01 byte followed by the value of |
| 100866 | | -** iCol encoded as a varint to *pp. |
| 101412 | +** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by |
| 101413 | +** the value of iCol encoded as a varint to *pp. This will start a new |
| 101414 | +** column list. |
| 100867 | 101415 | ** |
| 100868 | 101416 | ** Set *pp to point to the byte just after the last byte written before |
| 100869 | 101417 | ** returning (do not modify it if iCol==0). Return the total number of bytes |
| 100870 | 101418 | ** written (0 if iCol==0). |
| 100871 | 101419 | */ |
| | @@ -100879,11 +101427,15 @@ |
| 100879 | 101427 | } |
| 100880 | 101428 | return n; |
| 100881 | 101429 | } |
| 100882 | 101430 | |
| 100883 | 101431 | /* |
| 100884 | | -** |
| 101432 | +** Compute the union of two position lists. The output written |
| 101433 | +** into *pp contains all positions of both *pp1 and *pp2 in sorted |
| 101434 | +** order and with any duplicates removed. All pointers are |
| 101435 | +** updated appropriately. The caller is responsible for insuring |
| 101436 | +** that there is enough space in *pp to hold the complete output. |
| 100885 | 101437 | */ |
| 100886 | 101438 | static void fts3PoslistMerge( |
| 100887 | 101439 | char **pp, /* Output buffer */ |
| 100888 | 101440 | char **pp1, /* Left input list */ |
| 100889 | 101441 | char **pp2 /* Right input list */ |
| | @@ -100891,36 +101443,37 @@ |
| 100891 | 101443 | char *p = *pp; |
| 100892 | 101444 | char *p1 = *pp1; |
| 100893 | 101445 | char *p2 = *pp2; |
| 100894 | 101446 | |
| 100895 | 101447 | while( *p1 || *p2 ){ |
| 100896 | | - int iCol1; |
| 100897 | | - int iCol2; |
| 101448 | + int iCol1; /* The current column index in pp1 */ |
| 101449 | + int iCol2; /* The current column index in pp2 */ |
| 100898 | 101450 | |
| 100899 | | - if( *p1==0x01 ) sqlite3Fts3GetVarint32(&p1[1], &iCol1); |
| 100900 | | - else if( *p1==0x00 ) iCol1 = OFFSET_LIST_END; |
| 101451 | + if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1); |
| 101452 | + else if( *p1==POS_END ) iCol1 = POSITION_LIST_END; |
| 100901 | 101453 | else iCol1 = 0; |
| 100902 | 101454 | |
| 100903 | | - if( *p2==0x01 ) sqlite3Fts3GetVarint32(&p2[1], &iCol2); |
| 100904 | | - else if( *p2==0x00 ) iCol2 = OFFSET_LIST_END; |
| 101455 | + if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2); |
| 101456 | + else if( *p2==POS_END ) iCol2 = POSITION_LIST_END; |
| 100905 | 101457 | else iCol2 = 0; |
| 100906 | 101458 | |
| 100907 | 101459 | if( iCol1==iCol2 ){ |
| 100908 | | - sqlite3_int64 i1 = 0; |
| 100909 | | - sqlite3_int64 i2 = 0; |
| 101460 | + sqlite3_int64 i1 = 0; /* Last position from pp1 */ |
| 101461 | + sqlite3_int64 i2 = 0; /* Last position from pp2 */ |
| 100910 | 101462 | sqlite3_int64 iPrev = 0; |
| 100911 | 101463 | int n = fts3PutColNumber(&p, iCol1); |
| 100912 | 101464 | p1 += n; |
| 100913 | 101465 | p2 += n; |
| 100914 | 101466 | |
| 100915 | | - /* At this point, both p1 and p2 point to the start of offset-lists. |
| 100916 | | - ** An offset-list is a list of non-negative delta-encoded varints, each |
| 100917 | | - ** incremented by 2 before being stored. Each list is terminated by a 0 |
| 100918 | | - ** or 1 value (0x00 or 0x01). The following block merges the two lists |
| 101467 | + /* At this point, both p1 and p2 point to the start of column-lists |
| 101468 | + ** for the same column (the column with index iCol1 and iCol2). |
| 101469 | + ** A column-list is a list of non-negative delta-encoded varints, each |
| 101470 | + ** incremented by 2 before being stored. Each list is terminated by a |
| 101471 | + ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists |
| 100919 | 101472 | ** and writes the results to buffer p. p is left pointing to the byte |
| 100920 | | - ** after the list written. No terminator (0x00 or 0x01) is written to |
| 100921 | | - ** the output. |
| 101473 | + ** after the list written. No terminator (POS_END or POS_COLUMN) is |
| 101474 | + ** written to the output. |
| 100922 | 101475 | */ |
| 100923 | 101476 | fts3GetDeltaVarint(&p1, &i1); |
| 100924 | 101477 | fts3GetDeltaVarint(&p2, &i2); |
| 100925 | 101478 | do { |
| 100926 | 101479 | fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2); |
| | @@ -100931,21 +101484,21 @@ |
| 100931 | 101484 | }else if( i1<i2 ){ |
| 100932 | 101485 | fts3ReadNextPos(&p1, &i1); |
| 100933 | 101486 | }else{ |
| 100934 | 101487 | fts3ReadNextPos(&p2, &i2); |
| 100935 | 101488 | } |
| 100936 | | - }while( i1!=OFFSET_LIST_END || i2!=OFFSET_LIST_END ); |
| 101489 | + }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END ); |
| 100937 | 101490 | }else if( iCol1<iCol2 ){ |
| 100938 | 101491 | p1 += fts3PutColNumber(&p, iCol1); |
| 100939 | 101492 | fts3ColumnlistCopy(&p, &p1); |
| 100940 | 101493 | }else{ |
| 100941 | 101494 | p2 += fts3PutColNumber(&p, iCol2); |
| 100942 | 101495 | fts3ColumnlistCopy(&p, &p2); |
| 100943 | 101496 | } |
| 100944 | 101497 | } |
| 100945 | 101498 | |
| 100946 | | - *p++ = '\0'; |
| 101499 | + *p++ = POS_END; |
| 100947 | 101500 | *pp = p; |
| 100948 | 101501 | *pp1 = p1 + 1; |
| 100949 | 101502 | *pp2 = p2 + 1; |
| 100950 | 101503 | } |
| 100951 | 101504 | |
| | @@ -100964,15 +101517,15 @@ |
| 100964 | 101517 | char *p2 = *pp2; |
| 100965 | 101518 | |
| 100966 | 101519 | int iCol1 = 0; |
| 100967 | 101520 | int iCol2 = 0; |
| 100968 | 101521 | assert( *p1!=0 && *p2!=0 ); |
| 100969 | | - if( *p1==0x01 ){ |
| 101522 | + if( *p1==POS_COLUMN ){ |
| 100970 | 101523 | p1++; |
| 100971 | 101524 | p1 += sqlite3Fts3GetVarint32(p1, &iCol1); |
| 100972 | 101525 | } |
| 100973 | | - if( *p2==0x01 ){ |
| 101526 | + if( *p2==POS_COLUMN ){ |
| 100974 | 101527 | p2++; |
| 100975 | 101528 | p2 += sqlite3Fts3GetVarint32(p2, &iCol2); |
| 100976 | 101529 | } |
| 100977 | 101530 | |
| 100978 | 101531 | while( 1 ){ |
| | @@ -100981,15 +101534,16 @@ |
| 100981 | 101534 | sqlite3_int64 iPrev = 0; |
| 100982 | 101535 | sqlite3_int64 iPos1 = 0; |
| 100983 | 101536 | sqlite3_int64 iPos2 = 0; |
| 100984 | 101537 | |
| 100985 | 101538 | if( pp && iCol1 ){ |
| 100986 | | - *p++ = 0x01; |
| 101539 | + *p++ = POS_COLUMN; |
| 100987 | 101540 | p += sqlite3Fts3PutVarint(p, iCol1); |
| 100988 | 101541 | } |
| 100989 | 101542 | |
| 100990 | | - assert( *p1!=0x00 && *p2!=0x00 && *p1!=0x01 && *p2!=0x01 ); |
| 101543 | + assert( *p1!=POS_END && *p1!=POS_COLUMN ); |
| 101544 | + assert( *p2!=POS_END && *p2!=POS_COLUMN ); |
| 100991 | 101545 | fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2; |
| 100992 | 101546 | fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2; |
| 100993 | 101547 | |
| 100994 | 101548 | while( 1 ){ |
| 100995 | 101549 | if( iPos2>iPos1 && iPos2<=iPos1+nToken ){ |
| | @@ -101237,10 +101791,11 @@ |
| 101237 | 101791 | } |
| 101238 | 101792 | |
| 101239 | 101793 | default: assert( mergetype==MERGE_POS_NEAR || mergetype==MERGE_NEAR ); { |
| 101240 | 101794 | char *aTmp = 0; |
| 101241 | 101795 | char **ppPos = 0; |
| 101796 | + |
| 101242 | 101797 | if( mergetype==MERGE_POS_NEAR ){ |
| 101243 | 101798 | ppPos = &p; |
| 101244 | 101799 | aTmp = sqlite3_malloc(2*(n1+n2+1)); |
| 101245 | 101800 | if( !aTmp ){ |
| 101246 | 101801 | return SQLITE_NOMEM; |
| | @@ -101341,13 +101896,13 @@ |
| 101341 | 101896 | ** This function retreives the doclist for the specified term (or term |
| 101342 | 101897 | ** prefix) from the database. |
| 101343 | 101898 | ** |
| 101344 | 101899 | ** The returned doclist may be in one of two formats, depending on the |
| 101345 | 101900 | ** value of parameter isReqPos. If isReqPos is zero, then the doclist is |
| 101346 | | -** a sorted list of delta-compressed docids. If isReqPos is non-zero, |
| 101347 | | -** then the returned list is in the same format as is stored in the |
| 101348 | | -** database without the found length specifier at the start of on-disk |
| 101901 | +** a sorted list of delta-compressed docids (a bare doclist). If isReqPos |
| 101902 | +** is non-zero, then the returned list is in the same format as is stored |
| 101903 | +** in the database without the found length specifier at the start of on-disk |
| 101349 | 101904 | ** doclists. |
| 101350 | 101905 | */ |
| 101351 | 101906 | static int fts3TermSelect( |
| 101352 | 101907 | Fts3Table *p, /* Virtual table handle */ |
| 101353 | 101908 | int iColumn, /* Column to query (or -ve for all columns) */ |
| | @@ -101603,11 +102158,13 @@ |
| 101603 | 102158 | return rc; |
| 101604 | 102159 | } |
| 101605 | 102160 | |
| 101606 | 102161 | /* |
| 101607 | 102162 | ** Evaluate the full-text expression pExpr against fts3 table pTab. Store |
| 101608 | | -** the resulting doclist in *paOut and *pnOut. |
| 102163 | +** the resulting doclist in *paOut and *pnOut. This routine mallocs for |
| 102164 | +** the space needed to store the output. The caller is responsible for |
| 102165 | +** freeing the space when it has finished. |
| 101609 | 102166 | */ |
| 101610 | 102167 | static int evalFts3Expr( |
| 101611 | 102168 | Fts3Table *p, /* Virtual table handle */ |
| 101612 | 102169 | Fts3Expr *pExpr, /* Parsed fts3 expression */ |
| 101613 | 102170 | char **paOut, /* OUT: Pointer to malloc'd result buffer */ |
| | @@ -108163,11 +108720,11 @@ |
| 108163 | 108720 | ** This is done as part of extracting the snippet text, not when selecting |
| 108164 | 108721 | ** the snippet. Snippet selection is done based on doclists only, so there |
| 108165 | 108722 | ** is no way for fts3BestSnippet() to know whether or not the document |
| 108166 | 108723 | ** actually contains terms that follow the final highlighted term. |
| 108167 | 108724 | */ |
| 108168 | | -int fts3SnippetShift( |
| 108725 | +static int fts3SnippetShift( |
| 108169 | 108726 | Fts3Table *pTab, /* FTS3 table snippet comes from */ |
| 108170 | 108727 | int nSnippet, /* Number of tokens desired for snippet */ |
| 108171 | 108728 | const char *zDoc, /* Document text to extract snippet from */ |
| 108172 | 108729 | int nDoc, /* Size of buffer zDoc in bytes */ |
| 108173 | 108730 | int *piPos, /* IN/OUT: First token of snippet */ |
| | @@ -111280,10 +111837,11 @@ |
| 111280 | 111837 | rc = SQLITE_CONSTRAINT; |
| 111281 | 111838 | goto constraint; |
| 111282 | 111839 | } |
| 111283 | 111840 | rc = sqlite3_reset(pRtree->pReadRowid); |
| 111284 | 111841 | } |
| 111842 | + *pRowid = cell.iRowid; |
| 111285 | 111843 | |
| 111286 | 111844 | if( rc==SQLITE_OK ){ |
| 111287 | 111845 | rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf); |
| 111288 | 111846 | } |
| 111289 | 111847 | if( rc==SQLITE_OK ){ |
| 111290 | 111848 | |