Fossil SCM
Go back to using SQLite 3.8.8 after fixing the corruption bug in the b-tree balancer.
Commit
eaefb1809c1cf3f2bf97a739b1a592776b06a242
Parent
b543373febb6b35…
2 files changed
+2208
-885
+353
-227
+2208
-885
| --- src/sqlite3.c | ||
| +++ src/sqlite3.c | ||
| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | -** version 3.8.7.2. By combining all the individual C code files into this | |
| 3 | +** version 3.8.8. By combining all the individual C code files into this | |
| 4 | 4 | ** single large file, the entire code can be compiled as a single 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% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| @@ -179,11 +179,11 @@ | ||
| 179 | 179 | |
| 180 | 180 | |
| 181 | 181 | /* |
| 182 | 182 | ** These no-op macros are used in front of interfaces to mark those |
| 183 | 183 | ** interfaces as either deprecated or experimental. New applications |
| 184 | -** should not use deprecated interfaces - they are support for backwards | |
| 184 | +** should not use deprecated interfaces - they are supported for backwards | |
| 185 | 185 | ** compatibility only. Application writers should be aware that |
| 186 | 186 | ** experimental interfaces are subject to change in point releases. |
| 187 | 187 | ** |
| 188 | 188 | ** These macros used to resolve to various kinds of compiler magic that |
| 189 | 189 | ** would generate warning messages when they were used. But that |
| @@ -229,13 +229,13 @@ | ||
| 229 | 229 | ** |
| 230 | 230 | ** See also: [sqlite3_libversion()], |
| 231 | 231 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 232 | 232 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 233 | 233 | */ |
| 234 | -#define SQLITE_VERSION "3.8.7.2" | |
| 235 | -#define SQLITE_VERSION_NUMBER 3008007 | |
| 236 | -#define SQLITE_SOURCE_ID "2014-11-18 20:57:56 2ab564bf9655b7c7b97ab85cafc8a48329b27f93" | |
| 234 | +#define SQLITE_VERSION "3.8.8" | |
| 235 | +#define SQLITE_VERSION_NUMBER 3008008 | |
| 236 | +#define SQLITE_SOURCE_ID "2014-11-27 11:36:36 f095cde579e7417306e11b5c1d2dd90b6bb547d5" | |
| 237 | 237 | |
| 238 | 238 | /* |
| 239 | 239 | ** CAPI3REF: Run-Time Library Version Numbers |
| 240 | 240 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 241 | 241 | ** |
| @@ -1626,29 +1626,31 @@ | ||
| 1626 | 1626 | ** it is not possible to set the Serialized [threading mode] and |
| 1627 | 1627 | ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the |
| 1628 | 1628 | ** SQLITE_CONFIG_SERIALIZED configuration option.</dd> |
| 1629 | 1629 | ** |
| 1630 | 1630 | ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt> |
| 1631 | -** <dd> ^(This option takes a single argument which is a pointer to an | |
| 1632 | -** instance of the [sqlite3_mem_methods] structure. The argument specifies | |
| 1631 | +** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is | |
| 1632 | +** a pointer to an instance of the [sqlite3_mem_methods] structure. | |
| 1633 | +** The argument specifies | |
| 1633 | 1634 | ** alternative low-level memory allocation routines to be used in place of |
| 1634 | 1635 | ** the memory allocation routines built into SQLite.)^ ^SQLite makes |
| 1635 | 1636 | ** its own private copy of the content of the [sqlite3_mem_methods] structure |
| 1636 | 1637 | ** before the [sqlite3_config()] call returns.</dd> |
| 1637 | 1638 | ** |
| 1638 | 1639 | ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt> |
| 1639 | -** <dd> ^(This option takes a single argument which is a pointer to an | |
| 1640 | -** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods] | |
| 1640 | +** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which | |
| 1641 | +** is a pointer to an instance of the [sqlite3_mem_methods] structure. | |
| 1642 | +** The [sqlite3_mem_methods] | |
| 1641 | 1643 | ** structure is filled with the currently defined memory allocation routines.)^ |
| 1642 | 1644 | ** This option can be used to overload the default memory allocation |
| 1643 | 1645 | ** routines with a wrapper that simulations memory allocation failure or |
| 1644 | 1646 | ** tracks memory usage, for example. </dd> |
| 1645 | 1647 | ** |
| 1646 | 1648 | ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt> |
| 1647 | -** <dd> ^This option takes single argument of type int, interpreted as a | |
| 1648 | -** boolean, which enables or disables the collection of memory allocation | |
| 1649 | -** statistics. ^(When memory allocation statistics are disabled, the | |
| 1649 | +** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int, | |
| 1650 | +** interpreted as a boolean, which enables or disables the collection of | |
| 1651 | +** memory allocation statistics. ^(When memory allocation statistics are disabled, the | |
| 1650 | 1652 | ** following SQLite interfaces become non-operational: |
| 1651 | 1653 | ** <ul> |
| 1652 | 1654 | ** <li> [sqlite3_memory_used()] |
| 1653 | 1655 | ** <li> [sqlite3_memory_highwater()] |
| 1654 | 1656 | ** <li> [sqlite3_soft_heap_limit64()] |
| @@ -1658,78 +1660,90 @@ | ||
| 1658 | 1660 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1659 | 1661 | ** allocation statistics are disabled by default. |
| 1660 | 1662 | ** </dd> |
| 1661 | 1663 | ** |
| 1662 | 1664 | ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt> |
| 1663 | -** <dd> ^This option specifies a static memory buffer that SQLite can use for | |
| 1664 | -** scratch memory. There are three arguments: A pointer an 8-byte | |
| 1665 | +** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer | |
| 1666 | +** that SQLite can use for scratch memory. ^(There are three arguments | |
| 1667 | +** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte | |
| 1665 | 1668 | ** aligned memory buffer from which the scratch allocations will be |
| 1666 | 1669 | ** drawn, the size of each scratch allocation (sz), |
| 1667 | -** and the maximum number of scratch allocations (N). The sz | |
| 1668 | -** argument must be a multiple of 16. | |
| 1670 | +** and the maximum number of scratch allocations (N).)^ | |
| 1669 | 1671 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1670 | 1672 | ** of at least sz*N bytes of memory. |
| 1671 | -** ^SQLite will use no more than two scratch buffers per thread. So | |
| 1672 | -** N should be set to twice the expected maximum number of threads. | |
| 1673 | -** ^SQLite will never require a scratch buffer that is more than 6 | |
| 1674 | -** times the database page size. ^If SQLite needs needs additional | |
| 1673 | +** ^SQLite will not use more than one scratch buffers per thread. | |
| 1674 | +** ^SQLite will never request a scratch buffer that is more than 6 | |
| 1675 | +** times the database page size. | |
| 1676 | +** ^If SQLite needs needs additional | |
| 1675 | 1677 | ** scratch memory beyond what is provided by this configuration option, then |
| 1676 | -** [sqlite3_malloc()] will be used to obtain the memory needed.</dd> | |
| 1678 | +** [sqlite3_malloc()] will be used to obtain the memory needed.<p> | |
| 1679 | +** ^When the application provides any amount of scratch memory using | |
| 1680 | +** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large | |
| 1681 | +** [sqlite3_malloc|heap allocations]. | |
| 1682 | +** This can help [Robson proof|prevent memory allocation failures] due to heap | |
| 1683 | +** fragmentation in low-memory embedded systems. | |
| 1684 | +** </dd> | |
| 1677 | 1685 | ** |
| 1678 | 1686 | ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1679 | -** <dd> ^This option specifies a static memory buffer that SQLite can use for | |
| 1680 | -** the database page cache with the default page cache implementation. | |
| 1687 | +** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer | |
| 1688 | +** that SQLite can use for the database page cache with the default page | |
| 1689 | +** cache implementation. | |
| 1681 | 1690 | ** This configuration should not be used if an application-define page |
| 1682 | -** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option. | |
| 1683 | -** There are three arguments to this option: A pointer to 8-byte aligned | |
| 1691 | +** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2] | |
| 1692 | +** configuration option. | |
| 1693 | +** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned | |
| 1684 | 1694 | ** memory, the size of each page buffer (sz), and the number of pages (N). |
| 1685 | 1695 | ** The sz argument should be the size of the largest database page |
| 1686 | -** (a power of two between 512 and 32768) plus a little extra for each | |
| 1687 | -** page header. ^The page header size is 20 to 40 bytes depending on | |
| 1688 | -** the host architecture. ^It is harmless, apart from the wasted memory, | |
| 1689 | -** to make sz a little too large. The first | |
| 1690 | -** argument should point to an allocation of at least sz*N bytes of memory. | |
| 1696 | +** (a power of two between 512 and 32768) plus some extra bytes for each | |
| 1697 | +** page header. ^The number of extra bytes needed by the page header | |
| 1698 | +** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option | |
| 1699 | +** to [sqlite3_config()]. | |
| 1700 | +** ^It is harmless, apart from the wasted memory, | |
| 1701 | +** for the sz parameter to be larger than necessary. The first | |
| 1702 | +** argument should pointer to an 8-byte aligned block of memory that | |
| 1703 | +** is at least sz*N bytes of memory, otherwise subsequent behavior is | |
| 1704 | +** undefined. | |
| 1691 | 1705 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1692 | 1706 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1693 | 1707 | ** page cache memory is needed beyond what is provided by this option, then |
| 1694 | -** SQLite goes to [sqlite3_malloc()] for the additional storage space. | |
| 1695 | -** The pointer in the first argument must | |
| 1696 | -** be aligned to an 8-byte boundary or subsequent behavior of SQLite | |
| 1697 | -** will be undefined.</dd> | |
| 1708 | +** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd> | |
| 1698 | 1709 | ** |
| 1699 | 1710 | ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt> |
| 1700 | -** <dd> ^This option specifies a static memory buffer that SQLite will use | |
| 1701 | -** for all of its dynamic memory allocation needs beyond those provided | |
| 1702 | -** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE]. | |
| 1703 | -** There are three arguments: An 8-byte aligned pointer to the memory, | |
| 1711 | +** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer | |
| 1712 | +** that SQLite will use for all of its dynamic memory allocation needs | |
| 1713 | +** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE]. | |
| 1714 | +** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled | |
| 1715 | +** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns | |
| 1716 | +** [SQLITE_ERROR] if invoked otherwise. | |
| 1717 | +** ^There are three arguments to SQLITE_CONFIG_HEAP: | |
| 1718 | +** An 8-byte aligned pointer to the memory, | |
| 1704 | 1719 | ** the number of bytes in the memory buffer, and the minimum allocation size. |
| 1705 | 1720 | ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts |
| 1706 | 1721 | ** to using its default memory allocator (the system malloc() implementation), |
| 1707 | 1722 | ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the |
| 1708 | -** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or | |
| 1709 | -** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory | |
| 1723 | +** memory pointer is not NULL then the alternative memory | |
| 1710 | 1724 | ** allocator is engaged to handle all of SQLites memory allocation needs. |
| 1711 | 1725 | ** The first pointer (the memory pointer) must be aligned to an 8-byte |
| 1712 | 1726 | ** boundary or subsequent behavior of SQLite will be undefined. |
| 1713 | 1727 | ** The minimum allocation size is capped at 2**12. Reasonable values |
| 1714 | 1728 | ** for the minimum allocation size are 2**5 through 2**8.</dd> |
| 1715 | 1729 | ** |
| 1716 | 1730 | ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt> |
| 1717 | -** <dd> ^(This option takes a single argument which is a pointer to an | |
| 1718 | -** instance of the [sqlite3_mutex_methods] structure. The argument specifies | |
| 1719 | -** alternative low-level mutex routines to be used in place | |
| 1731 | +** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a | |
| 1732 | +** pointer to an instance of the [sqlite3_mutex_methods] structure. | |
| 1733 | +** The argument specifies alternative low-level mutex routines to be used in place | |
| 1720 | 1734 | ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the |
| 1721 | 1735 | ** content of the [sqlite3_mutex_methods] structure before the call to |
| 1722 | 1736 | ** [sqlite3_config()] returns. ^If SQLite is compiled with |
| 1723 | 1737 | ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then |
| 1724 | 1738 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1725 | 1739 | ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will |
| 1726 | 1740 | ** return [SQLITE_ERROR].</dd> |
| 1727 | 1741 | ** |
| 1728 | 1742 | ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt> |
| 1729 | -** <dd> ^(This option takes a single argument which is a pointer to an | |
| 1730 | -** instance of the [sqlite3_mutex_methods] structure. The | |
| 1743 | +** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which | |
| 1744 | +** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The | |
| 1731 | 1745 | ** [sqlite3_mutex_methods] |
| 1732 | 1746 | ** structure is filled with the currently defined mutex routines.)^ |
| 1733 | 1747 | ** This option can be used to overload the default mutex allocation |
| 1734 | 1748 | ** routines with a wrapper used to track mutex usage for performance |
| 1735 | 1749 | ** profiling or testing, for example. ^If SQLite is compiled with |
| @@ -1737,28 +1751,28 @@ | ||
| 1737 | 1751 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1738 | 1752 | ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will |
| 1739 | 1753 | ** return [SQLITE_ERROR].</dd> |
| 1740 | 1754 | ** |
| 1741 | 1755 | ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt> |
| 1742 | -** <dd> ^(This option takes two arguments that determine the default | |
| 1743 | -** memory allocation for the lookaside memory allocator on each | |
| 1744 | -** [database connection]. The first argument is the | |
| 1756 | +** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine | |
| 1757 | +** the default size of lookaside memory on each [database connection]. | |
| 1758 | +** The first argument is the | |
| 1745 | 1759 | ** size of each lookaside buffer slot and the second is the number of |
| 1746 | -** slots allocated to each database connection.)^ ^(This option sets the | |
| 1747 | -** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] | |
| 1748 | -** verb to [sqlite3_db_config()] can be used to change the lookaside | |
| 1760 | +** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE | |
| 1761 | +** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] | |
| 1762 | +** option to [sqlite3_db_config()] can be used to change the lookaside | |
| 1749 | 1763 | ** configuration on individual connections.)^ </dd> |
| 1750 | 1764 | ** |
| 1751 | 1765 | ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt> |
| 1752 | -** <dd> ^(This option takes a single argument which is a pointer to | |
| 1753 | -** an [sqlite3_pcache_methods2] object. This object specifies the interface | |
| 1754 | -** to a custom page cache implementation.)^ ^SQLite makes a copy of the | |
| 1755 | -** object and uses it for page cache memory allocations.</dd> | |
| 1766 | +** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is | |
| 1767 | +** a pointer to an [sqlite3_pcache_methods2] object. This object specifies | |
| 1768 | +** the interface to a custom page cache implementation.)^ | |
| 1769 | +** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd> | |
| 1756 | 1770 | ** |
| 1757 | 1771 | ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt> |
| 1758 | -** <dd> ^(This option takes a single argument which is a pointer to an | |
| 1759 | -** [sqlite3_pcache_methods2] object. SQLite copies of the current | |
| 1772 | +** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which | |
| 1773 | +** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of the current | |
| 1760 | 1774 | ** page cache implementation into that object.)^ </dd> |
| 1761 | 1775 | ** |
| 1762 | 1776 | ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt> |
| 1763 | 1777 | ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite |
| 1764 | 1778 | ** global [error log]. |
| @@ -1778,26 +1792,27 @@ | ||
| 1778 | 1792 | ** supplied by the application must not invoke any SQLite interface. |
| 1779 | 1793 | ** In a multi-threaded application, the application-defined logger |
| 1780 | 1794 | ** function must be threadsafe. </dd> |
| 1781 | 1795 | ** |
| 1782 | 1796 | ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI |
| 1783 | -** <dd>^(This option takes a single argument of type int. If non-zero, then | |
| 1784 | -** URI handling is globally enabled. If the parameter is zero, then URI handling | |
| 1785 | -** is globally disabled.)^ ^If URI handling is globally enabled, all filenames | |
| 1786 | -** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or | |
| 1797 | +** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int. | |
| 1798 | +** If non-zero, then URI handling is globally enabled. If the parameter is zero, | |
| 1799 | +** then URI handling is globally disabled.)^ ^If URI handling is globally enabled, | |
| 1800 | +** all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or | |
| 1787 | 1801 | ** specified as part of [ATTACH] commands are interpreted as URIs, regardless |
| 1788 | 1802 | ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database |
| 1789 | 1803 | ** connection is opened. ^If it is globally disabled, filenames are |
| 1790 | 1804 | ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the |
| 1791 | 1805 | ** database connection is opened. ^(By default, URI handling is globally |
| 1792 | 1806 | ** disabled. The default value may be changed by compiling with the |
| 1793 | 1807 | ** [SQLITE_USE_URI] symbol defined.)^ |
| 1794 | 1808 | ** |
| 1795 | 1809 | ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN |
| 1796 | -** <dd>^This option takes a single integer argument which is interpreted as | |
| 1797 | -** a boolean in order to enable or disable the use of covering indices for | |
| 1798 | -** full table scans in the query optimizer. ^The default setting is determined | |
| 1810 | +** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer | |
| 1811 | +** argument which is interpreted as a boolean in order to enable or disable | |
| 1812 | +** the use of covering indices for full table scans in the query optimizer. | |
| 1813 | +** ^The default setting is determined | |
| 1799 | 1814 | ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on" |
| 1800 | 1815 | ** if that compile-time option is omitted. |
| 1801 | 1816 | ** The ability to disable the use of covering indices for full table scans |
| 1802 | 1817 | ** is because some incorrectly coded legacy applications might malfunction |
| 1803 | 1818 | ** when the optimization is enabled. Providing the ability to |
| @@ -1833,23 +1848,32 @@ | ||
| 1833 | 1848 | ** that are the default mmap size limit (the default setting for |
| 1834 | 1849 | ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit. |
| 1835 | 1850 | ** ^The default setting can be overridden by each database connection using |
| 1836 | 1851 | ** either the [PRAGMA mmap_size] command, or by using the |
| 1837 | 1852 | ** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size |
| 1838 | -** cannot be changed at run-time. Nor may the maximum allowed mmap size | |
| 1839 | -** exceed the compile-time maximum mmap size set by the | |
| 1853 | +** will be silently truncated if necessary so that it does not exceed the | |
| 1854 | +** compile-time maximum mmap size set by the | |
| 1840 | 1855 | ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^ |
| 1841 | 1856 | ** ^If either argument to this option is negative, then that argument is |
| 1842 | 1857 | ** changed to its compile-time default. |
| 1843 | 1858 | ** |
| 1844 | 1859 | ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]] |
| 1845 | 1860 | ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE |
| 1846 | -** <dd>^This option is only available if SQLite is compiled for Windows | |
| 1847 | -** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined. | |
| 1848 | -** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value | |
| 1861 | +** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is | |
| 1862 | +** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro defined. | |
| 1863 | +** ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value | |
| 1849 | 1864 | ** that specifies the maximum size of the created heap. |
| 1850 | 1865 | ** </dl> |
| 1866 | +** | |
| 1867 | +** [[SQLITE_CONFIG_PCACHE_HDRSZ]] | |
| 1868 | +** <dt>SQLITE_CONFIG_PCACHE_HDRSZ | |
| 1869 | +** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which | |
| 1870 | +** is a pointer to an integer and writes into that integer the number of extra | |
| 1871 | +** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. The amount of | |
| 1872 | +** extra space required can change depending on the compiler, | |
| 1873 | +** target platform, and SQLite version. | |
| 1874 | +** </dl> | |
| 1851 | 1875 | */ |
| 1852 | 1876 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 1853 | 1877 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 1854 | 1878 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| 1855 | 1879 | #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ |
| @@ -1870,10 +1894,11 @@ | ||
| 1870 | 1894 | #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ |
| 1871 | 1895 | #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ |
| 1872 | 1896 | #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ |
| 1873 | 1897 | #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ |
| 1874 | 1898 | #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ |
| 1899 | +#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */ | |
| 1875 | 1900 | |
| 1876 | 1901 | /* |
| 1877 | 1902 | ** CAPI3REF: Database Connection Configuration Options |
| 1878 | 1903 | ** |
| 1879 | 1904 | ** These constants are the available integer configuration options that |
| @@ -1997,51 +2022,49 @@ | ||
| 1997 | 2022 | SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); |
| 1998 | 2023 | |
| 1999 | 2024 | /* |
| 2000 | 2025 | ** CAPI3REF: Count The Number Of Rows Modified |
| 2001 | 2026 | ** |
| 2002 | -** ^This function returns the number of database rows that were changed | |
| 2003 | -** or inserted or deleted by the most recently completed SQL statement | |
| 2004 | -** on the [database connection] specified by the first parameter. | |
| 2005 | -** ^(Only changes that are directly specified by the [INSERT], [UPDATE], | |
| 2006 | -** or [DELETE] statement are counted. Auxiliary changes caused by | |
| 2007 | -** triggers or [foreign key actions] are not counted.)^ Use the | |
| 2008 | -** [sqlite3_total_changes()] function to find the total number of changes | |
| 2009 | -** including changes caused by triggers and foreign key actions. | |
| 2010 | -** | |
| 2011 | -** ^Changes to a view that are simulated by an [INSTEAD OF trigger] | |
| 2012 | -** are not counted. Only real table changes are counted. | |
| 2013 | -** | |
| 2014 | -** ^(A "row change" is a change to a single row of a single table | |
| 2015 | -** caused by an INSERT, DELETE, or UPDATE statement. Rows that | |
| 2016 | -** are changed as side effects of [REPLACE] constraint resolution, | |
| 2017 | -** rollback, ABORT processing, [DROP TABLE], or by any other | |
| 2018 | -** mechanisms do not count as direct row changes.)^ | |
| 2019 | -** | |
| 2020 | -** A "trigger context" is a scope of execution that begins and | |
| 2021 | -** ends with the script of a [CREATE TRIGGER | trigger]. | |
| 2022 | -** Most SQL statements are | |
| 2023 | -** evaluated outside of any trigger. This is the "top level" | |
| 2024 | -** trigger context. If a trigger fires from the top level, a | |
| 2025 | -** new trigger context is entered for the duration of that one | |
| 2026 | -** trigger. Subtriggers create subcontexts for their duration. | |
| 2027 | -** | |
| 2028 | -** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does | |
| 2029 | -** not create a new trigger context. | |
| 2030 | -** | |
| 2031 | -** ^This function returns the number of direct row changes in the | |
| 2032 | -** most recent INSERT, UPDATE, or DELETE statement within the same | |
| 2033 | -** trigger context. | |
| 2034 | -** | |
| 2035 | -** ^Thus, when called from the top level, this function returns the | |
| 2036 | -** number of changes in the most recent INSERT, UPDATE, or DELETE | |
| 2037 | -** that also occurred at the top level. ^(Within the body of a trigger, | |
| 2038 | -** the sqlite3_changes() interface can be called to find the number of | |
| 2039 | -** changes in the most recently completed INSERT, UPDATE, or DELETE | |
| 2040 | -** statement within the body of the same trigger. | |
| 2041 | -** However, the number returned does not include changes | |
| 2042 | -** caused by subtriggers since those have their own context.)^ | |
| 2027 | +** ^This function returns the number of rows modified, inserted or | |
| 2028 | +** deleted by the most recently completed INSERT, UPDATE or DELETE | |
| 2029 | +** statement on the database connection specified by the only parameter. | |
| 2030 | +** ^Executing any other type of SQL statement does not modify the value | |
| 2031 | +** returned by this function. | |
| 2032 | +** | |
| 2033 | +** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are | |
| 2034 | +** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], | |
| 2035 | +** [foreign key actions] or [REPLACE] constraint resolution are not counted. | |
| 2036 | +** | |
| 2037 | +** Changes to a view that are intercepted by | |
| 2038 | +** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value | |
| 2039 | +** returned by sqlite3_changes() immediately after an INSERT, UPDATE or | |
| 2040 | +** DELETE statement run on a view is always zero. Only changes made to real | |
| 2041 | +** tables are counted. | |
| 2042 | +** | |
| 2043 | +** Things are more complicated if the sqlite3_changes() function is | |
| 2044 | +** executed while a trigger program is running. This may happen if the | |
| 2045 | +** program uses the [changes() SQL function], or if some other callback | |
| 2046 | +** function invokes sqlite3_changes() directly. Essentially: | |
| 2047 | +** | |
| 2048 | +** <ul> | |
| 2049 | +** <li> ^(Before entering a trigger program the value returned by | |
| 2050 | +** sqlite3_changes() function is saved. After the trigger program | |
| 2051 | +** has finished, the original value is restored.)^ | |
| 2052 | +** | |
| 2053 | +** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE | |
| 2054 | +** statement sets the value returned by sqlite3_changes() | |
| 2055 | +** upon completion as normal. Of course, this value will not include | |
| 2056 | +** any changes performed by sub-triggers, as the sqlite3_changes() | |
| 2057 | +** value will be saved and restored after each sub-trigger has run.)^ | |
| 2058 | +** </ul> | |
| 2059 | +** | |
| 2060 | +** ^This means that if the changes() SQL function (or similar) is used | |
| 2061 | +** by the first INSERT, UPDATE or DELETE statement within a trigger, it | |
| 2062 | +** returns the value as set when the calling statement began executing. | |
| 2063 | +** ^If it is used by the second or subsequent such statement within a trigger | |
| 2064 | +** program, the value returned reflects the number of rows modified by the | |
| 2065 | +** previous INSERT, UPDATE or DELETE statement within the same trigger. | |
| 2043 | 2066 | ** |
| 2044 | 2067 | ** See also the [sqlite3_total_changes()] interface, the |
| 2045 | 2068 | ** [count_changes pragma], and the [changes() SQL function]. |
| 2046 | 2069 | ** |
| 2047 | 2070 | ** If a separate thread makes changes on the same database connection |
| @@ -2051,24 +2074,21 @@ | ||
| 2051 | 2074 | SQLITE_API int sqlite3_changes(sqlite3*); |
| 2052 | 2075 | |
| 2053 | 2076 | /* |
| 2054 | 2077 | ** CAPI3REF: Total Number Of Rows Modified |
| 2055 | 2078 | ** |
| 2056 | -** ^This function returns the number of row changes caused by [INSERT], | |
| 2057 | -** [UPDATE] or [DELETE] statements since the [database connection] was opened. | |
| 2058 | -** ^(The count returned by sqlite3_total_changes() includes all changes | |
| 2059 | -** from all [CREATE TRIGGER | trigger] contexts and changes made by | |
| 2060 | -** [foreign key actions]. However, | |
| 2061 | -** the count does not include changes used to implement [REPLACE] constraints, | |
| 2062 | -** do rollbacks or ABORT processing, or [DROP TABLE] processing. The | |
| 2063 | -** count does not include rows of views that fire an [INSTEAD OF trigger], | |
| 2064 | -** though if the INSTEAD OF trigger makes changes of its own, those changes | |
| 2065 | -** are counted.)^ | |
| 2066 | -** ^The sqlite3_total_changes() function counts the changes as soon as | |
| 2067 | -** the statement that makes them is completed (when the statement handle | |
| 2068 | -** is passed to [sqlite3_reset()] or [sqlite3_finalize()]). | |
| 2069 | -** | |
| 2079 | +** ^This function returns the total number of rows inserted, modified or | |
| 2080 | +** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed | |
| 2081 | +** since the database connection was opened, including those executed as | |
| 2082 | +** part of trigger programs. ^Executing any other type of SQL statement | |
| 2083 | +** does not affect the value returned by sqlite3_total_changes(). | |
| 2084 | +** | |
| 2085 | +** ^Changes made as part of [foreign key actions] are included in the | |
| 2086 | +** count, but those made as part of REPLACE constraint resolution are | |
| 2087 | +** not. ^Changes to a view that are intercepted by INSTEAD OF triggers | |
| 2088 | +** are not counted. | |
| 2089 | +** | |
| 2070 | 2090 | ** See also the [sqlite3_changes()] interface, the |
| 2071 | 2091 | ** [count_changes pragma], and the [total_changes() SQL function]. |
| 2072 | 2092 | ** |
| 2073 | 2093 | ** If a separate thread makes changes on the same database connection |
| 2074 | 2094 | ** while [sqlite3_total_changes()] is running then the value |
| @@ -2542,17 +2562,18 @@ | ||
| 2542 | 2562 | ** already uses the largest possible [ROWID]. The PRNG is also used for |
| 2543 | 2563 | ** the build-in random() and randomblob() SQL functions. This interface allows |
| 2544 | 2564 | ** applications to access the same PRNG for other purposes. |
| 2545 | 2565 | ** |
| 2546 | 2566 | ** ^A call to this routine stores N bytes of randomness into buffer P. |
| 2547 | -** ^If N is less than one, then P can be a NULL pointer. | |
| 2567 | +** ^The P parameter can be a NULL pointer. | |
| 2548 | 2568 | ** |
| 2549 | 2569 | ** ^If this routine has not been previously called or if the previous |
| 2550 | -** call had N less than one, then the PRNG is seeded using randomness | |
| 2551 | -** obtained from the xRandomness method of the default [sqlite3_vfs] object. | |
| 2552 | -** ^If the previous call to this routine had an N of 1 or more then | |
| 2553 | -** the pseudo-randomness is generated | |
| 2570 | +** call had N less than one or a NULL pointer for P, then the PRNG is | |
| 2571 | +** seeded using randomness obtained from the xRandomness method of | |
| 2572 | +** the default [sqlite3_vfs] object. | |
| 2573 | +** ^If the previous call to this routine had an N of 1 or more and a | |
| 2574 | +** non-NULL P then the pseudo-randomness is generated | |
| 2554 | 2575 | ** internally and without recourse to the [sqlite3_vfs] xRandomness |
| 2555 | 2576 | ** method. |
| 2556 | 2577 | */ |
| 2557 | 2578 | SQLITE_API void sqlite3_randomness(int N, void *P); |
| 2558 | 2579 | |
| @@ -4270,13 +4291,13 @@ | ||
| 4270 | 4291 | ** CAPI3REF: Text Encodings |
| 4271 | 4292 | ** |
| 4272 | 4293 | ** These constant define integer codes that represent the various |
| 4273 | 4294 | ** text encodings supported by SQLite. |
| 4274 | 4295 | */ |
| 4275 | -#define SQLITE_UTF8 1 | |
| 4276 | -#define SQLITE_UTF16LE 2 | |
| 4277 | -#define SQLITE_UTF16BE 3 | |
| 4296 | +#define SQLITE_UTF8 1 /* IMP: R-37514-35566 */ | |
| 4297 | +#define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */ | |
| 4298 | +#define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */ | |
| 4278 | 4299 | #define SQLITE_UTF16 4 /* Use native byte order */ |
| 4279 | 4300 | #define SQLITE_ANY 5 /* Deprecated */ |
| 4280 | 4301 | #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ |
| 4281 | 4302 | |
| 4282 | 4303 | /* |
| @@ -5762,31 +5783,47 @@ | ||
| 5762 | 5783 | ** in other words, the same BLOB that would be selected by: |
| 5763 | 5784 | ** |
| 5764 | 5785 | ** <pre> |
| 5765 | 5786 | ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow; |
| 5766 | 5787 | ** </pre>)^ |
| 5788 | +** | |
| 5789 | +** ^(Parameter zDb is not the filename that contains the database, but | |
| 5790 | +** rather the symbolic name of the database. For attached databases, this is | |
| 5791 | +** the name that appears after the AS keyword in the [ATTACH] statement. | |
| 5792 | +** For the main database file, the database name is "main". For TEMP | |
| 5793 | +** tables, the database name is "temp".)^ | |
| 5767 | 5794 | ** |
| 5768 | 5795 | ** ^If the flags parameter is non-zero, then the BLOB is opened for read |
| 5769 | -** and write access. ^If it is zero, the BLOB is opened for read access. | |
| 5770 | -** ^It is not possible to open a column that is part of an index or primary | |
| 5771 | -** key for writing. ^If [foreign key constraints] are enabled, it is | |
| 5772 | -** not possible to open a column that is part of a [child key] for writing. | |
| 5773 | -** | |
| 5774 | -** ^Note that the database name is not the filename that contains | |
| 5775 | -** the database but rather the symbolic name of the database that | |
| 5776 | -** appears after the AS keyword when the database is connected using [ATTACH]. | |
| 5777 | -** ^For the main database file, the database name is "main". | |
| 5778 | -** ^For TEMP tables, the database name is "temp". | |
| 5779 | -** | |
| 5780 | -** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written | |
| 5781 | -** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set | |
| 5782 | -** to be a null pointer.)^ | |
| 5783 | -** ^This function sets the [database connection] error code and message | |
| 5784 | -** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related | |
| 5785 | -** functions. ^Note that the *ppBlob variable is always initialized in a | |
| 5786 | -** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob | |
| 5787 | -** regardless of the success or failure of this routine. | |
| 5796 | +** and write access. ^If the flags parameter is zero, the BLOB is opened for | |
| 5797 | +** read-only access. | |
| 5798 | +** | |
| 5799 | +** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored | |
| 5800 | +** in *ppBlob. Otherwise an [error code] is returned and, unless the error | |
| 5801 | +** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided | |
| 5802 | +** the API is not misused, it is always safe to call [sqlite3_blob_close()] | |
| 5803 | +** on *ppBlob after this function it returns. | |
| 5804 | +** | |
| 5805 | +** This function fails with SQLITE_ERROR if any of the following are true: | |
| 5806 | +** <ul> | |
| 5807 | +** <li> ^(Database zDb does not exist)^, | |
| 5808 | +** <li> ^(Table zTable does not exist within database zDb)^, | |
| 5809 | +** <li> ^(Table zTable is a WITHOUT ROWID table)^, | |
| 5810 | +** <li> ^(Column zColumn does not exist)^, | |
| 5811 | +** <li> ^(Row iRow is not present in the table)^, | |
| 5812 | +** <li> ^(The specified column of row iRow contains a value that is not | |
| 5813 | +** a TEXT or BLOB value)^, | |
| 5814 | +** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE | |
| 5815 | +** constraint and the blob is being opened for read/write access)^, | |
| 5816 | +** <li> ^([foreign key constraints | Foreign key constraints] are enabled, | |
| 5817 | +** column zColumn is part of a [child key] definition and the blob is | |
| 5818 | +** being opened for read/write access)^. | |
| 5819 | +** </ul> | |
| 5820 | +** | |
| 5821 | +** ^Unless it returns SQLITE_MISUSE, this function sets the | |
| 5822 | +** [database connection] error code and message accessible via | |
| 5823 | +** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. | |
| 5824 | +** | |
| 5788 | 5825 | ** |
| 5789 | 5826 | ** ^(If the row that a BLOB handle points to is modified by an |
| 5790 | 5827 | ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects |
| 5791 | 5828 | ** then the BLOB handle is marked as "expired". |
| 5792 | 5829 | ** This is true if any column of the row is changed, even a column |
| @@ -5800,17 +5837,13 @@ | ||
| 5800 | 5837 | ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of |
| 5801 | 5838 | ** the opened blob. ^The size of a blob may not be changed by this |
| 5802 | 5839 | ** interface. Use the [UPDATE] SQL command to change the size of a |
| 5803 | 5840 | ** blob. |
| 5804 | 5841 | ** |
| 5805 | -** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID] | |
| 5806 | -** table. Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables. | |
| 5807 | -** | |
| 5808 | 5842 | ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces |
| 5809 | -** and the built-in [zeroblob] SQL function can be used, if desired, | |
| 5810 | -** to create an empty, zero-filled blob in which to read or write using | |
| 5811 | -** this interface. | |
| 5843 | +** and the built-in [zeroblob] SQL function may be used to create a | |
| 5844 | +** zero-filled blob to read or write using the incremental-blob interface. | |
| 5812 | 5845 | ** |
| 5813 | 5846 | ** To avoid a resource leak, every open [BLOB handle] should eventually |
| 5814 | 5847 | ** be released by a call to [sqlite3_blob_close()]. |
| 5815 | 5848 | */ |
| 5816 | 5849 | SQLITE_API int sqlite3_blob_open( |
| @@ -5848,28 +5881,26 @@ | ||
| 5848 | 5881 | SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); |
| 5849 | 5882 | |
| 5850 | 5883 | /* |
| 5851 | 5884 | ** CAPI3REF: Close A BLOB Handle |
| 5852 | 5885 | ** |
| 5853 | -** ^Closes an open [BLOB handle]. | |
| 5854 | -** | |
| 5855 | -** ^Closing a BLOB shall cause the current transaction to commit | |
| 5856 | -** if there are no other BLOBs, no pending prepared statements, and the | |
| 5857 | -** database connection is in [autocommit mode]. | |
| 5858 | -** ^If any writes were made to the BLOB, they might be held in cache | |
| 5859 | -** until the close operation if they will fit. | |
| 5860 | -** | |
| 5861 | -** ^(Closing the BLOB often forces the changes | |
| 5862 | -** out to disk and so if any I/O errors occur, they will likely occur | |
| 5863 | -** at the time when the BLOB is closed. Any errors that occur during | |
| 5864 | -** closing are reported as a non-zero return value.)^ | |
| 5865 | -** | |
| 5866 | -** ^(The BLOB is closed unconditionally. Even if this routine returns | |
| 5867 | -** an error code, the BLOB is still closed.)^ | |
| 5868 | -** | |
| 5869 | -** ^Calling this routine with a null pointer (such as would be returned | |
| 5870 | -** by a failed call to [sqlite3_blob_open()]) is a harmless no-op. | |
| 5886 | +** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed | |
| 5887 | +** unconditionally. Even if this routine returns an error code, the | |
| 5888 | +** handle is still closed.)^ | |
| 5889 | +** | |
| 5890 | +** ^If the blob handle being closed was opened for read-write access, and if | |
| 5891 | +** the database is in auto-commit mode and there are no other open read-write | |
| 5892 | +** blob handles or active write statements, the current transaction is | |
| 5893 | +** committed. ^If an error occurs while committing the transaction, an error | |
| 5894 | +** code is returned and the transaction rolled back. | |
| 5895 | +** | |
| 5896 | +** Calling this function with an argument that is not a NULL pointer or an | |
| 5897 | +** open blob handle results in undefined behaviour. ^Calling this routine | |
| 5898 | +** with a null pointer (such as would be returned by a failed call to | |
| 5899 | +** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function | |
| 5900 | +** is passed a valid open blob handle, the values returned by the | |
| 5901 | +** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning. | |
| 5871 | 5902 | */ |
| 5872 | 5903 | SQLITE_API int sqlite3_blob_close(sqlite3_blob *); |
| 5873 | 5904 | |
| 5874 | 5905 | /* |
| 5875 | 5906 | ** CAPI3REF: Return The Size Of An Open BLOB |
| @@ -5915,36 +5946,39 @@ | ||
| 5915 | 5946 | SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); |
| 5916 | 5947 | |
| 5917 | 5948 | /* |
| 5918 | 5949 | ** CAPI3REF: Write Data Into A BLOB Incrementally |
| 5919 | 5950 | ** |
| 5920 | -** ^This function is used to write data into an open [BLOB handle] from a | |
| 5921 | -** caller-supplied buffer. ^N bytes of data are copied from the buffer Z | |
| 5922 | -** into the open BLOB, starting at offset iOffset. | |
| 5951 | +** ^(This function is used to write data into an open [BLOB handle] from a | |
| 5952 | +** caller-supplied buffer. N bytes of data are copied from the buffer Z | |
| 5953 | +** into the open BLOB, starting at offset iOffset.)^ | |
| 5954 | +** | |
| 5955 | +** ^(On success, sqlite3_blob_write() returns SQLITE_OK. | |
| 5956 | +** Otherwise, an [error code] or an [extended error code] is returned.)^ | |
| 5957 | +** ^Unless SQLITE_MISUSE is returned, this function sets the | |
| 5958 | +** [database connection] error code and message accessible via | |
| 5959 | +** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. | |
| 5923 | 5960 | ** |
| 5924 | 5961 | ** ^If the [BLOB handle] passed as the first argument was not opened for |
| 5925 | 5962 | ** writing (the flags parameter to [sqlite3_blob_open()] was zero), |
| 5926 | 5963 | ** this function returns [SQLITE_READONLY]. |
| 5927 | 5964 | ** |
| 5928 | -** ^This function may only modify the contents of the BLOB; it is | |
| 5965 | +** This function may only modify the contents of the BLOB; it is | |
| 5929 | 5966 | ** not possible to increase the size of a BLOB using this API. |
| 5930 | 5967 | ** ^If offset iOffset is less than N bytes from the end of the BLOB, |
| 5931 | -** [SQLITE_ERROR] is returned and no data is written. ^If N is | |
| 5932 | -** less than zero [SQLITE_ERROR] is returned and no data is written. | |
| 5933 | -** The size of the BLOB (and hence the maximum value of N+iOffset) | |
| 5934 | -** can be determined using the [sqlite3_blob_bytes()] interface. | |
| 5968 | +** [SQLITE_ERROR] is returned and no data is written. The size of the | |
| 5969 | +** BLOB (and hence the maximum value of N+iOffset) can be determined | |
| 5970 | +** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less | |
| 5971 | +** than zero [SQLITE_ERROR] is returned and no data is written. | |
| 5935 | 5972 | ** |
| 5936 | 5973 | ** ^An attempt to write to an expired [BLOB handle] fails with an |
| 5937 | 5974 | ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred |
| 5938 | 5975 | ** before the [BLOB handle] expired are not rolled back by the |
| 5939 | 5976 | ** expiration of the handle, though of course those changes might |
| 5940 | 5977 | ** have been overwritten by the statement that expired the BLOB handle |
| 5941 | 5978 | ** or by other independent statements. |
| 5942 | 5979 | ** |
| 5943 | -** ^(On success, sqlite3_blob_write() returns SQLITE_OK. | |
| 5944 | -** Otherwise, an [error code] or an [extended error code] is returned.)^ | |
| 5945 | -** | |
| 5946 | 5980 | ** This routine only works on a [BLOB handle] which has been created |
| 5947 | 5981 | ** by a prior successful call to [sqlite3_blob_open()] and which has not |
| 5948 | 5982 | ** been closed by [sqlite3_blob_close()]. Passing any other pointer in |
| 5949 | 5983 | ** to this routine results in undefined and probably undesirable behavior. |
| 5950 | 5984 | ** |
| @@ -5993,38 +6027,38 @@ | ||
| 5993 | 6027 | ** use by SQLite, code that links against SQLite is |
| 5994 | 6028 | ** permitted to use any of these routines. |
| 5995 | 6029 | ** |
| 5996 | 6030 | ** The SQLite source code contains multiple implementations |
| 5997 | 6031 | ** of these mutex routines. An appropriate implementation |
| 5998 | -** is selected automatically at compile-time. ^(The following | |
| 6032 | +** is selected automatically at compile-time. The following | |
| 5999 | 6033 | ** implementations are available in the SQLite core: |
| 6000 | 6034 | ** |
| 6001 | 6035 | ** <ul> |
| 6002 | 6036 | ** <li> SQLITE_MUTEX_PTHREADS |
| 6003 | 6037 | ** <li> SQLITE_MUTEX_W32 |
| 6004 | 6038 | ** <li> SQLITE_MUTEX_NOOP |
| 6005 | -** </ul>)^ | |
| 6039 | +** </ul> | |
| 6006 | 6040 | ** |
| 6007 | -** ^The SQLITE_MUTEX_NOOP implementation is a set of routines | |
| 6041 | +** The SQLITE_MUTEX_NOOP implementation is a set of routines | |
| 6008 | 6042 | ** that does no real locking and is appropriate for use in |
| 6009 | -** a single-threaded application. ^The SQLITE_MUTEX_PTHREADS and | |
| 6043 | +** a single-threaded application. The SQLITE_MUTEX_PTHREADS and | |
| 6010 | 6044 | ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix |
| 6011 | 6045 | ** and Windows. |
| 6012 | 6046 | ** |
| 6013 | -** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor | |
| 6047 | +** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor | |
| 6014 | 6048 | ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex |
| 6015 | 6049 | ** implementation is included with the library. In this case the |
| 6016 | 6050 | ** application must supply a custom mutex implementation using the |
| 6017 | 6051 | ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function |
| 6018 | 6052 | ** before calling sqlite3_initialize() or any other public sqlite3_ |
| 6019 | -** function that calls sqlite3_initialize().)^ | |
| 6053 | +** function that calls sqlite3_initialize(). | |
| 6020 | 6054 | ** |
| 6021 | 6055 | ** ^The sqlite3_mutex_alloc() routine allocates a new |
| 6022 | -** mutex and returns a pointer to it. ^If it returns NULL | |
| 6023 | -** that means that a mutex could not be allocated. ^SQLite | |
| 6024 | -** will unwind its stack and return an error. ^(The argument | |
| 6025 | -** to sqlite3_mutex_alloc() is one of these integer constants: | |
| 6056 | +** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc() | |
| 6057 | +** routine returns NULL if it is unable to allocate the requested | |
| 6058 | +** mutex. The argument to sqlite3_mutex_alloc() must one of these | |
| 6059 | +** integer constants: | |
| 6026 | 6060 | ** |
| 6027 | 6061 | ** <ul> |
| 6028 | 6062 | ** <li> SQLITE_MUTEX_FAST |
| 6029 | 6063 | ** <li> SQLITE_MUTEX_RECURSIVE |
| 6030 | 6064 | ** <li> SQLITE_MUTEX_STATIC_MASTER |
| @@ -6033,68 +6067,64 @@ | ||
| 6033 | 6067 | ** <li> SQLITE_MUTEX_STATIC_PRNG |
| 6034 | 6068 | ** <li> SQLITE_MUTEX_STATIC_LRU |
| 6035 | 6069 | ** <li> SQLITE_MUTEX_STATIC_PMEM |
| 6036 | 6070 | ** <li> SQLITE_MUTEX_STATIC_APP1 |
| 6037 | 6071 | ** <li> SQLITE_MUTEX_STATIC_APP2 |
| 6038 | -** </ul>)^ | |
| 6072 | +** <li> SQLITE_MUTEX_STATIC_APP3 | |
| 6073 | +** </ul> | |
| 6039 | 6074 | ** |
| 6040 | 6075 | ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) |
| 6041 | 6076 | ** cause sqlite3_mutex_alloc() to create |
| 6042 | 6077 | ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE |
| 6043 | 6078 | ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. |
| 6044 | 6079 | ** The mutex implementation does not need to make a distinction |
| 6045 | 6080 | ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does |
| 6046 | -** not want to. ^SQLite will only request a recursive mutex in | |
| 6047 | -** cases where it really needs one. ^If a faster non-recursive mutex | |
| 6081 | +** not want to. SQLite will only request a recursive mutex in | |
| 6082 | +** cases where it really needs one. If a faster non-recursive mutex | |
| 6048 | 6083 | ** implementation is available on the host platform, the mutex subsystem |
| 6049 | 6084 | ** might return such a mutex in response to SQLITE_MUTEX_FAST. |
| 6050 | 6085 | ** |
| 6051 | 6086 | ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other |
| 6052 | 6087 | ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return |
| 6053 | -** a pointer to a static preexisting mutex. ^Six static mutexes are | |
| 6088 | +** a pointer to a static preexisting mutex. ^Nine static mutexes are | |
| 6054 | 6089 | ** used by the current version of SQLite. Future versions of SQLite |
| 6055 | 6090 | ** may add additional static mutexes. Static mutexes are for internal |
| 6056 | 6091 | ** use by SQLite only. Applications that use SQLite mutexes should |
| 6057 | 6092 | ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or |
| 6058 | 6093 | ** SQLITE_MUTEX_RECURSIVE. |
| 6059 | 6094 | ** |
| 6060 | 6095 | ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST |
| 6061 | 6096 | ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() |
| 6062 | -** returns a different mutex on every call. ^But for the static | |
| 6097 | +** returns a different mutex on every call. ^For the static | |
| 6063 | 6098 | ** mutex types, the same mutex is returned on every call that has |
| 6064 | 6099 | ** the same type number. |
| 6065 | 6100 | ** |
| 6066 | 6101 | ** ^The sqlite3_mutex_free() routine deallocates a previously |
| 6067 | -** allocated dynamic mutex. ^SQLite is careful to deallocate every | |
| 6068 | -** dynamic mutex that it allocates. The dynamic mutexes must not be in | |
| 6069 | -** use when they are deallocated. Attempting to deallocate a static | |
| 6070 | -** mutex results in undefined behavior. ^SQLite never deallocates | |
| 6071 | -** a static mutex. | |
| 6102 | +** allocated dynamic mutex. Attempting to deallocate a static | |
| 6103 | +** mutex results in undefined behavior. | |
| 6072 | 6104 | ** |
| 6073 | 6105 | ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt |
| 6074 | 6106 | ** to enter a mutex. ^If another thread is already within the mutex, |
| 6075 | 6107 | ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return |
| 6076 | 6108 | ** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK] |
| 6077 | 6109 | ** upon successful entry. ^(Mutexes created using |
| 6078 | 6110 | ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. |
| 6079 | -** In such cases the, | |
| 6111 | +** In such cases, the | |
| 6080 | 6112 | ** mutex must be exited an equal number of times before another thread |
| 6081 | -** can enter.)^ ^(If the same thread tries to enter any other | |
| 6082 | -** kind of mutex more than once, the behavior is undefined. | |
| 6083 | -** SQLite will never exhibit | |
| 6084 | -** such behavior in its own use of mutexes.)^ | |
| 6113 | +** can enter.)^ If the same thread tries to enter any mutex other | |
| 6114 | +** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined. | |
| 6085 | 6115 | ** |
| 6086 | 6116 | ** ^(Some systems (for example, Windows 95) do not support the operation |
| 6087 | 6117 | ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() |
| 6088 | -** will always return SQLITE_BUSY. The SQLite core only ever uses | |
| 6089 | -** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^ | |
| 6118 | +** will always return SQLITE_BUSY. The SQLite core only ever uses | |
| 6119 | +** sqlite3_mutex_try() as an optimization so this is acceptable | |
| 6120 | +** behavior.)^ | |
| 6090 | 6121 | ** |
| 6091 | 6122 | ** ^The sqlite3_mutex_leave() routine exits a mutex that was |
| 6092 | -** previously entered by the same thread. ^(The behavior | |
| 6123 | +** previously entered by the same thread. The behavior | |
| 6093 | 6124 | ** is undefined if the mutex is not currently entered by the |
| 6094 | -** calling thread or is not currently allocated. SQLite will | |
| 6095 | -** never do either.)^ | |
| 6125 | +** calling thread or is not currently allocated. | |
| 6096 | 6126 | ** |
| 6097 | 6127 | ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or |
| 6098 | 6128 | ** sqlite3_mutex_leave() is a NULL pointer, then all three routines |
| 6099 | 6129 | ** behave as no-ops. |
| 6100 | 6130 | ** |
| @@ -6111,13 +6141,13 @@ | ||
| 6111 | 6141 | ** |
| 6112 | 6142 | ** An instance of this structure defines the low-level routines |
| 6113 | 6143 | ** used to allocate and use mutexes. |
| 6114 | 6144 | ** |
| 6115 | 6145 | ** Usually, the default mutex implementations provided by SQLite are |
| 6116 | -** sufficient, however the user has the option of substituting a custom | |
| 6146 | +** sufficient, however the application has the option of substituting a custom | |
| 6117 | 6147 | ** implementation for specialized deployments or systems for which SQLite |
| 6118 | -** does not provide a suitable implementation. In this case, the user | |
| 6148 | +** does not provide a suitable implementation. In this case, the application | |
| 6119 | 6149 | ** creates and populates an instance of this structure to pass |
| 6120 | 6150 | ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option. |
| 6121 | 6151 | ** Additionally, an instance of this structure can be used as an |
| 6122 | 6152 | ** output variable when querying the system for the current mutex |
| 6123 | 6153 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| @@ -6154,17 +6184,17 @@ | ||
| 6154 | 6184 | ** by this structure are not required to handle this case, the results |
| 6155 | 6185 | ** of passing a NULL pointer instead of a valid mutex handle are undefined |
| 6156 | 6186 | ** (i.e. it is acceptable to provide an implementation that segfaults if |
| 6157 | 6187 | ** it is passed a NULL pointer). |
| 6158 | 6188 | ** |
| 6159 | -** The xMutexInit() method must be threadsafe. ^It must be harmless to | |
| 6189 | +** The xMutexInit() method must be threadsafe. It must be harmless to | |
| 6160 | 6190 | ** invoke xMutexInit() multiple times within the same process and without |
| 6161 | 6191 | ** intervening calls to xMutexEnd(). Second and subsequent calls to |
| 6162 | 6192 | ** xMutexInit() must be no-ops. |
| 6163 | 6193 | ** |
| 6164 | -** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] | |
| 6165 | -** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory | |
| 6194 | +** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] | |
| 6195 | +** and its associates). Similarly, xMutexAlloc() must not use SQLite memory | |
| 6166 | 6196 | ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite |
| 6167 | 6197 | ** memory allocation for a fast or recursive mutex. |
| 6168 | 6198 | ** |
| 6169 | 6199 | ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is |
| 6170 | 6200 | ** called, but only if the prior call to xMutexInit returned SQLITE_OK. |
| @@ -6186,33 +6216,33 @@ | ||
| 6186 | 6216 | |
| 6187 | 6217 | /* |
| 6188 | 6218 | ** CAPI3REF: Mutex Verification Routines |
| 6189 | 6219 | ** |
| 6190 | 6220 | ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines |
| 6191 | -** are intended for use inside assert() statements. ^The SQLite core | |
| 6221 | +** are intended for use inside assert() statements. The SQLite core | |
| 6192 | 6222 | ** never uses these routines except inside an assert() and applications |
| 6193 | -** are advised to follow the lead of the core. ^The SQLite core only | |
| 6223 | +** are advised to follow the lead of the core. The SQLite core only | |
| 6194 | 6224 | ** provides implementations for these routines when it is compiled |
| 6195 | -** with the SQLITE_DEBUG flag. ^External mutex implementations | |
| 6225 | +** with the SQLITE_DEBUG flag. External mutex implementations | |
| 6196 | 6226 | ** are only required to provide these routines if SQLITE_DEBUG is |
| 6197 | 6227 | ** defined and if NDEBUG is not defined. |
| 6198 | 6228 | ** |
| 6199 | -** ^These routines should return true if the mutex in their argument | |
| 6229 | +** These routines should return true if the mutex in their argument | |
| 6200 | 6230 | ** is held or not held, respectively, by the calling thread. |
| 6201 | 6231 | ** |
| 6202 | -** ^The implementation is not required to provide versions of these | |
| 6232 | +** The implementation is not required to provide versions of these | |
| 6203 | 6233 | ** routines that actually work. If the implementation does not provide working |
| 6204 | 6234 | ** versions of these routines, it should at least provide stubs that always |
| 6205 | 6235 | ** return true so that one does not get spurious assertion failures. |
| 6206 | 6236 | ** |
| 6207 | -** ^If the argument to sqlite3_mutex_held() is a NULL pointer then | |
| 6237 | +** If the argument to sqlite3_mutex_held() is a NULL pointer then | |
| 6208 | 6238 | ** the routine should return 1. This seems counter-intuitive since |
| 6209 | 6239 | ** clearly the mutex cannot be held if it does not exist. But |
| 6210 | 6240 | ** the reason the mutex does not exist is because the build is not |
| 6211 | 6241 | ** using mutexes. And we do not want the assert() containing the |
| 6212 | 6242 | ** call to sqlite3_mutex_held() to fail, so a non-zero return is |
| 6213 | -** the appropriate thing to do. ^The sqlite3_mutex_notheld() | |
| 6243 | +** the appropriate thing to do. The sqlite3_mutex_notheld() | |
| 6214 | 6244 | ** interface should also return 1 when given a NULL pointer. |
| 6215 | 6245 | */ |
| 6216 | 6246 | #ifndef NDEBUG |
| 6217 | 6247 | SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*); |
| 6218 | 6248 | SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); |
| @@ -6940,10 +6970,14 @@ | ||
| 6940 | 6970 | ** sqlite3_backup_init(D,N,S,M) identify the [database connection] |
| 6941 | 6971 | ** and database name of the source database, respectively. |
| 6942 | 6972 | ** ^The source and destination [database connections] (parameters S and D) |
| 6943 | 6973 | ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with |
| 6944 | 6974 | ** an error. |
| 6975 | +** | |
| 6976 | +** ^A call to sqlite3_backup_init() will fail, returning SQLITE_ERROR, if | |
| 6977 | +** there is already a read or read-write transaction open on the | |
| 6978 | +** destination database. | |
| 6945 | 6979 | ** |
| 6946 | 6980 | ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is |
| 6947 | 6981 | ** returned and an error code and error message are stored in the |
| 6948 | 6982 | ** destination [database connection] D. |
| 6949 | 6983 | ** ^The error code and message for the failed call to sqlite3_backup_init() |
| @@ -7533,10 +7567,102 @@ | ||
| 7533 | 7567 | /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */ |
| 7534 | 7568 | #define SQLITE_FAIL 3 |
| 7535 | 7569 | /* #define SQLITE_ABORT 4 // Also an error code */ |
| 7536 | 7570 | #define SQLITE_REPLACE 5 |
| 7537 | 7571 | |
| 7572 | +/* | |
| 7573 | +** CAPI3REF: Prepared Statement Scan Status Opcodes | |
| 7574 | +** KEYWORDS: {scanstatus options} | |
| 7575 | +** | |
| 7576 | +** The following constants can be used for the T parameter to the | |
| 7577 | +** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a | |
| 7578 | +** different metric for sqlite3_stmt_scanstatus() to return. | |
| 7579 | +** | |
| 7580 | +** <dl> | |
| 7581 | +** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt> | |
| 7582 | +** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the | |
| 7583 | +** total number of times that the X-th loop has run.</dd> | |
| 7584 | +** | |
| 7585 | +** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt> | |
| 7586 | +** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the | |
| 7587 | +** total number of rows examined by all iterations of the X-th loop.</dd> | |
| 7588 | +** | |
| 7589 | +** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt> | |
| 7590 | +** <dd>^The "double" variable pointed to by the T parameter will be set to the | |
| 7591 | +** query planner's estimate for the average number of rows output from each | |
| 7592 | +** iteration of the X-th loop. If the query planner's estimates was accurate, | |
| 7593 | +** then this value will approximate the quotient NVISIT/NLOOP and the | |
| 7594 | +** product of this value for all prior loops with the same SELECTID will | |
| 7595 | +** be the NLOOP value for the current loop. | |
| 7596 | +** | |
| 7597 | +** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt> | |
| 7598 | +** <dd>^The "const char *" variable pointed to by the T parameter will be set to | |
| 7599 | +** a zero-terminated UTF-8 string containing the name of the index or table used | |
| 7600 | +** for the X-th loop. | |
| 7601 | +** | |
| 7602 | +** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt> | |
| 7603 | +** <dd>^The "const char *" variable pointed to by the T parameter will be set to | |
| 7604 | +** a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] description | |
| 7605 | +** for the X-th loop. | |
| 7606 | +** | |
| 7607 | +** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt> | |
| 7608 | +** <dd>^The "int" variable pointed to by the T parameter will be set to the | |
| 7609 | +** "select-id" for the X-th loop. The select-id identifies which query or | |
| 7610 | +** subquery the loop is part of. The main query has a select-id of zero. | |
| 7611 | +** The select-id is the same value as is output in the first column | |
| 7612 | +** of an [EXPLAIN QUERY PLAN] query. | |
| 7613 | +** </dl> | |
| 7614 | +*/ | |
| 7615 | +#define SQLITE_SCANSTAT_NLOOP 0 | |
| 7616 | +#define SQLITE_SCANSTAT_NVISIT 1 | |
| 7617 | +#define SQLITE_SCANSTAT_EST 2 | |
| 7618 | +#define SQLITE_SCANSTAT_NAME 3 | |
| 7619 | +#define SQLITE_SCANSTAT_EXPLAIN 4 | |
| 7620 | +#define SQLITE_SCANSTAT_SELECTID 5 | |
| 7621 | + | |
| 7622 | +/* | |
| 7623 | +** CAPI3REF: Prepared Statement Scan Status | |
| 7624 | +** | |
| 7625 | +** Return status data for a single loop within query pStmt. | |
| 7626 | +** | |
| 7627 | +** The "iScanStatusOp" parameter determines which status information to return. | |
| 7628 | +** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior of | |
| 7629 | +** this interface is undefined. | |
| 7630 | +** ^The requested measurement is written into a variable pointed to by | |
| 7631 | +** the "pOut" parameter. | |
| 7632 | +** Parameter "idx" identifies the specific loop to retrieve statistics for. | |
| 7633 | +** Loops are numbered starting from zero. ^If idx is out of range - less than | |
| 7634 | +** zero or greater than or equal to the total number of loops used to implement | |
| 7635 | +** the statement - a non-zero value is returned and the variable that pOut | |
| 7636 | +** points to is unchanged. | |
| 7637 | +** | |
| 7638 | +** ^Statistics might not be available for all loops in all statements. ^In cases | |
| 7639 | +** where there exist loops with no available statistics, this function behaves | |
| 7640 | +** as if the loop did not exist - it returns non-zero and leave the variable | |
| 7641 | +** that pOut points to unchanged. | |
| 7642 | +** | |
| 7643 | +** This API is only available if the library is built with pre-processor | |
| 7644 | +** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. | |
| 7645 | +** | |
| 7646 | +** See also: [sqlite3_stmt_scanstatus_reset()] | |
| 7647 | +*/ | |
| 7648 | +SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus( | |
| 7649 | + sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ | |
| 7650 | + int idx, /* Index of loop to report on */ | |
| 7651 | + int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ | |
| 7652 | + void *pOut /* Result written here */ | |
| 7653 | +); | |
| 7654 | + | |
| 7655 | +/* | |
| 7656 | +** CAPI3REF: Zero Scan-Status Counters | |
| 7657 | +** | |
| 7658 | +** ^Zero all [sqlite3_stmt_scanstatus()] related event counters. | |
| 7659 | +** | |
| 7660 | +** This API is only available if the library is built with pre-processor | |
| 7661 | +** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. | |
| 7662 | +*/ | |
| 7663 | +SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); | |
| 7538 | 7664 | |
| 7539 | 7665 | |
| 7540 | 7666 | /* |
| 7541 | 7667 | ** Undo the hack that converts floating point types to integer for |
| 7542 | 7668 | ** builds on processors without floating point support. |
| @@ -7978,14 +8104,13 @@ | ||
| 7978 | 8104 | #ifndef SQLITE_POWERSAFE_OVERWRITE |
| 7979 | 8105 | # define SQLITE_POWERSAFE_OVERWRITE 1 |
| 7980 | 8106 | #endif |
| 7981 | 8107 | |
| 7982 | 8108 | /* |
| 7983 | -** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1. | |
| 7984 | -** It determines whether or not the features related to | |
| 7985 | -** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can | |
| 7986 | -** be overridden at runtime using the sqlite3_config() API. | |
| 8109 | +** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by | |
| 8110 | +** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in | |
| 8111 | +** which case memory allocation statistics are disabled by default. | |
| 7987 | 8112 | */ |
| 7988 | 8113 | #if !defined(SQLITE_DEFAULT_MEMSTATUS) |
| 7989 | 8114 | # define SQLITE_DEFAULT_MEMSTATUS 1 |
| 7990 | 8115 | #endif |
| 7991 | 8116 | |
| @@ -8611,11 +8736,11 @@ | ||
| 8611 | 8736 | ** Estimated quantities used for query planning are stored as 16-bit |
| 8612 | 8737 | ** logarithms. For quantity X, the value stored is 10*log2(X). This |
| 8613 | 8738 | ** gives a possible range of values of approximately 1.0e986 to 1e-986. |
| 8614 | 8739 | ** But the allowed values are "grainy". Not every value is representable. |
| 8615 | 8740 | ** For example, quantities 16 and 17 are both represented by a LogEst |
| 8616 | -** of 40. However, since LogEst quantaties are suppose to be estimates, | |
| 8741 | +** of 40. However, since LogEst quantities are suppose to be estimates, | |
| 8617 | 8742 | ** not exact values, this imprecision is not a problem. |
| 8618 | 8743 | ** |
| 8619 | 8744 | ** "LogEst" is short for "Logarithmic Estimate". |
| 8620 | 8745 | ** |
| 8621 | 8746 | ** Examples: |
| @@ -9124,10 +9249,11 @@ | ||
| 9124 | 9249 | SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *); |
| 9125 | 9250 | SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *); |
| 9126 | 9251 | SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); |
| 9127 | 9252 | SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask); |
| 9128 | 9253 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt); |
| 9254 | +SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void); | |
| 9129 | 9255 | |
| 9130 | 9256 | #ifndef NDEBUG |
| 9131 | 9257 | SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*); |
| 9132 | 9258 | #endif |
| 9133 | 9259 | |
| @@ -9666,10 +9792,16 @@ | ||
| 9666 | 9792 | # define VdbeCoverageAlwaysTaken(v) |
| 9667 | 9793 | # define VdbeCoverageNeverTaken(v) |
| 9668 | 9794 | # define VDBE_OFFSET_LINENO(x) 0 |
| 9669 | 9795 | #endif |
| 9670 | 9796 | |
| 9797 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
| 9798 | +SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*); | |
| 9799 | +#else | |
| 9800 | +# define sqlite3VdbeScanStatus(a,b,c,d,e) | |
| 9801 | +#endif | |
| 9802 | + | |
| 9671 | 9803 | #endif |
| 9672 | 9804 | |
| 9673 | 9805 | /************** End of vdbe.h ************************************************/ |
| 9674 | 9806 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 9675 | 9807 | /************** Include pager.h in the middle of sqliteInt.h *****************/ |
| @@ -9862,10 +9994,12 @@ | ||
| 9862 | 9994 | SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *); |
| 9863 | 9995 | |
| 9864 | 9996 | /* Functions used to truncate the database file. */ |
| 9865 | 9997 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); |
| 9866 | 9998 | |
| 9999 | +SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16); | |
| 10000 | + | |
| 9867 | 10001 | #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL) |
| 9868 | 10002 | SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *); |
| 9869 | 10003 | #endif |
| 9870 | 10004 | |
| 9871 | 10005 | /* Functions to support testing and debugging. */ |
| @@ -10049,10 +10183,14 @@ | ||
| 10049 | 10183 | SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*); |
| 10050 | 10184 | #endif |
| 10051 | 10185 | |
| 10052 | 10186 | SQLITE_PRIVATE void sqlite3PCacheSetDefault(void); |
| 10053 | 10187 | |
| 10188 | +/* Return the header size */ | |
| 10189 | +SQLITE_PRIVATE int sqlite3HeaderSizePcache(void); | |
| 10190 | +SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void); | |
| 10191 | + | |
| 10054 | 10192 | #endif /* _PCACHE_H_ */ |
| 10055 | 10193 | |
| 10056 | 10194 | /************** End of pcache.h **********************************************/ |
| 10057 | 10195 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 10058 | 10196 | |
| @@ -10735,11 +10873,11 @@ | ||
| 10735 | 10873 | #define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */ |
| 10736 | 10874 | #define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */ |
| 10737 | 10875 | #define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */ |
| 10738 | 10876 | #define SQLITE_Transitive 0x0200 /* Transitive constraints */ |
| 10739 | 10877 | #define SQLITE_OmitNoopJoin 0x0400 /* Omit unused tables in joins */ |
| 10740 | -#define SQLITE_Stat3 0x0800 /* Use the SQLITE_STAT3 table */ | |
| 10878 | +#define SQLITE_Stat34 0x0800 /* Use STAT3 or STAT4 data */ | |
| 10741 | 10879 | #define SQLITE_AllOpts 0xffff /* All optimizations */ |
| 10742 | 10880 | |
| 10743 | 10881 | /* |
| 10744 | 10882 | ** Macros for testing whether or not optimizations are enabled or disabled. |
| 10745 | 10883 | */ |
| @@ -11317,16 +11455,18 @@ | ||
| 11317 | 11455 | unsigned idxType:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */ |
| 11318 | 11456 | unsigned bUnordered:1; /* Use this index for == or IN queries only */ |
| 11319 | 11457 | unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */ |
| 11320 | 11458 | unsigned isResized:1; /* True if resizeIndexObject() has been called */ |
| 11321 | 11459 | unsigned isCovering:1; /* True if this is a covering index */ |
| 11460 | + unsigned noSkipScan:1; /* Do not try to use skip-scan if true */ | |
| 11322 | 11461 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 11323 | 11462 | int nSample; /* Number of elements in aSample[] */ |
| 11324 | 11463 | int nSampleCol; /* Size of IndexSample.anEq[] and so on */ |
| 11325 | 11464 | tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */ |
| 11326 | 11465 | IndexSample *aSample; /* Samples of the left-most key */ |
| 11327 | - tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this table */ | |
| 11466 | + tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */ | |
| 11467 | + tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */ | |
| 11328 | 11468 | #endif |
| 11329 | 11469 | }; |
| 11330 | 11470 | |
| 11331 | 11471 | /* |
| 11332 | 11472 | ** Allowed values for Index.idxType |
| @@ -11520,11 +11660,11 @@ | ||
| 11520 | 11660 | int nHeight; /* Height of the tree headed by this node */ |
| 11521 | 11661 | #endif |
| 11522 | 11662 | int iTable; /* TK_COLUMN: cursor number of table holding column |
| 11523 | 11663 | ** TK_REGISTER: register number |
| 11524 | 11664 | ** TK_TRIGGER: 1 -> new, 0 -> old |
| 11525 | - ** EP_Unlikely: 1000 times likelihood */ | |
| 11665 | + ** EP_Unlikely: 134217728 times likelihood */ | |
| 11526 | 11666 | ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid. |
| 11527 | 11667 | ** TK_VARIABLE: variable number (always >= 1). */ |
| 11528 | 11668 | i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */ |
| 11529 | 11669 | i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */ |
| 11530 | 11670 | u8 op2; /* TK_REGISTER: original value of Expr.op |
| @@ -12412,13 +12552,15 @@ | ||
| 12412 | 12552 | int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */ |
| 12413 | 12553 | int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */ |
| 12414 | 12554 | void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */ |
| 12415 | 12555 | Parse *pParse; /* Parser context. */ |
| 12416 | 12556 | int walkerDepth; /* Number of subqueries */ |
| 12557 | + u8 eCode; /* A small processing code */ | |
| 12417 | 12558 | union { /* Extra data for callback */ |
| 12418 | 12559 | NameContext *pNC; /* Naming context */ |
| 12419 | - int i; /* Integer value */ | |
| 12560 | + int n; /* A counter */ | |
| 12561 | + int iCur; /* A cursor number */ | |
| 12420 | 12562 | SrcList *pSrcList; /* FROM clause */ |
| 12421 | 12563 | struct SrcCount *pSrcCount; /* Counting column references */ |
| 12422 | 12564 | } u; |
| 12423 | 12565 | }; |
| 12424 | 12566 | |
| @@ -12815,10 +12957,11 @@ | ||
| 12815 | 12957 | SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *); |
| 12816 | 12958 | SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*); |
| 12817 | 12959 | SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*); |
| 12818 | 12960 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*); |
| 12819 | 12961 | SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8); |
| 12962 | +SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int); | |
| 12820 | 12963 | SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*); |
| 12821 | 12964 | SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*); |
| 12822 | 12965 | SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); |
| 12823 | 12966 | SQLITE_PRIVATE int sqlite3IsRowid(const char*); |
| 12824 | 12967 | SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8); |
| @@ -13472,15 +13615,23 @@ | ||
| 13472 | 13615 | ** compatibility for legacy applications, the URI filename capability is |
| 13473 | 13616 | ** disabled by default. |
| 13474 | 13617 | ** |
| 13475 | 13618 | ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled |
| 13476 | 13619 | ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options. |
| 13620 | +** | |
| 13621 | +** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally | |
| 13622 | +** disabled. The default value may be changed by compiling with the | |
| 13623 | +** SQLITE_USE_URI symbol defined. | |
| 13477 | 13624 | */ |
| 13478 | 13625 | #ifndef SQLITE_USE_URI |
| 13479 | 13626 | # define SQLITE_USE_URI 0 |
| 13480 | 13627 | #endif |
| 13481 | 13628 | |
| 13629 | +/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the | |
| 13630 | +** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if | |
| 13631 | +** that compile-time option is omitted. | |
| 13632 | +*/ | |
| 13482 | 13633 | #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN |
| 13483 | 13634 | # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1 |
| 13484 | 13635 | #endif |
| 13485 | 13636 | |
| 13486 | 13637 | /* |
| @@ -13566,12 +13717,12 @@ | ||
| 13566 | 13717 | ** than 1 GiB. The sqlite3_test_control() interface can be used to |
| 13567 | 13718 | ** move the pending byte. |
| 13568 | 13719 | ** |
| 13569 | 13720 | ** IMPORTANT: Changing the pending byte to any value other than |
| 13570 | 13721 | ** 0x40000000 results in an incompatible database file format! |
| 13571 | -** Changing the pending byte during operating results in undefined | |
| 13572 | -** and dileterious behavior. | |
| 13722 | +** Changing the pending byte during operation will result in undefined | |
| 13723 | +** and incorrect behavior. | |
| 13573 | 13724 | */ |
| 13574 | 13725 | #ifndef SQLITE_OMIT_WSD |
| 13575 | 13726 | SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000; |
| 13576 | 13727 | #endif |
| 13577 | 13728 | |
| @@ -13646,10 +13797,13 @@ | ||
| 13646 | 13797 | #ifdef SQLITE_DISABLE_DIRSYNC |
| 13647 | 13798 | "DISABLE_DIRSYNC", |
| 13648 | 13799 | #endif |
| 13649 | 13800 | #ifdef SQLITE_DISABLE_LFS |
| 13650 | 13801 | "DISABLE_LFS", |
| 13802 | +#endif | |
| 13803 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 13804 | + "ENABLE_API_ARMOR", | |
| 13651 | 13805 | #endif |
| 13652 | 13806 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 13653 | 13807 | "ENABLE_ATOMIC_WRITE", |
| 13654 | 13808 | #endif |
| 13655 | 13809 | #ifdef SQLITE_ENABLE_CEROD |
| @@ -13972,10 +14126,17 @@ | ||
| 13972 | 14126 | ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix |
| 13973 | 14127 | ** is not required for a match. |
| 13974 | 14128 | */ |
| 13975 | 14129 | SQLITE_API int sqlite3_compileoption_used(const char *zOptName){ |
| 13976 | 14130 | int i, n; |
| 14131 | + | |
| 14132 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 14133 | + if( zOptName==0 ){ | |
| 14134 | + (void)SQLITE_MISUSE_BKPT; | |
| 14135 | + return 0; | |
| 14136 | + } | |
| 14137 | +#endif | |
| 13977 | 14138 | if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7; |
| 13978 | 14139 | n = sqlite3Strlen30(zOptName); |
| 13979 | 14140 | |
| 13980 | 14141 | /* Since ArraySize(azCompileOpt) is normally in single digits, a |
| 13981 | 14142 | ** linear search is adequate. No need for a binary search. */ |
| @@ -14153,10 +14314,11 @@ | ||
| 14153 | 14314 | typedef struct VdbeFrame VdbeFrame; |
| 14154 | 14315 | struct VdbeFrame { |
| 14155 | 14316 | Vdbe *v; /* VM this frame belongs to */ |
| 14156 | 14317 | VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */ |
| 14157 | 14318 | Op *aOp; /* Program instructions for parent frame */ |
| 14319 | + i64 *anExec; /* Event counters from parent frame */ | |
| 14158 | 14320 | Mem *aMem; /* Array of memory cells for parent frame */ |
| 14159 | 14321 | u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */ |
| 14160 | 14322 | VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */ |
| 14161 | 14323 | void *token; /* Copy of SubProgram.token */ |
| 14162 | 14324 | i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */ |
| @@ -14165,11 +14327,12 @@ | ||
| 14165 | 14327 | int nOp; /* Size of aOp array */ |
| 14166 | 14328 | int nMem; /* Number of entries in aMem */ |
| 14167 | 14329 | int nOnceFlag; /* Number of entries in aOnceFlag */ |
| 14168 | 14330 | int nChildMem; /* Number of memory cells for child frame */ |
| 14169 | 14331 | int nChildCsr; /* Number of cursors for child frame */ |
| 14170 | - int nChange; /* Statement changes (Vdbe.nChanges) */ | |
| 14332 | + int nChange; /* Statement changes (Vdbe.nChange) */ | |
| 14333 | + int nDbChange; /* Value of db->nChange */ | |
| 14171 | 14334 | }; |
| 14172 | 14335 | |
| 14173 | 14336 | #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) |
| 14174 | 14337 | |
| 14175 | 14338 | /* |
| @@ -14316,10 +14479,20 @@ | ||
| 14316 | 14479 | /* A bitfield type for use inside of structures. Always follow with :N where |
| 14317 | 14480 | ** N is the number of bits. |
| 14318 | 14481 | */ |
| 14319 | 14482 | typedef unsigned bft; /* Bit Field Type */ |
| 14320 | 14483 | |
| 14484 | +typedef struct ScanStatus ScanStatus; | |
| 14485 | +struct ScanStatus { | |
| 14486 | + int addrExplain; /* OP_Explain for loop */ | |
| 14487 | + int addrLoop; /* Address of "loops" counter */ | |
| 14488 | + int addrVisit; /* Address of "rows visited" counter */ | |
| 14489 | + int iSelectID; /* The "Select-ID" for this loop */ | |
| 14490 | + LogEst nEst; /* Estimated output rows per loop */ | |
| 14491 | + char *zName; /* Name of table or index */ | |
| 14492 | +}; | |
| 14493 | + | |
| 14321 | 14494 | /* |
| 14322 | 14495 | ** An instance of the virtual machine. This structure contains the complete |
| 14323 | 14496 | ** state of the virtual machine. |
| 14324 | 14497 | ** |
| 14325 | 14498 | ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare() |
| @@ -14388,10 +14561,15 @@ | ||
| 14388 | 14561 | u32 expmask; /* Binding to these vars invalidates VM */ |
| 14389 | 14562 | SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ |
| 14390 | 14563 | int nOnceFlag; /* Size of array aOnceFlag[] */ |
| 14391 | 14564 | u8 *aOnceFlag; /* Flags for OP_Once */ |
| 14392 | 14565 | AuxData *pAuxData; /* Linked list of auxdata allocations */ |
| 14566 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
| 14567 | + i64 *anExec; /* Number of times each op has been executed */ | |
| 14568 | + int nScan; /* Entries in aScan[] */ | |
| 14569 | + ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */ | |
| 14570 | +#endif | |
| 14393 | 14571 | }; |
| 14394 | 14572 | |
| 14395 | 14573 | /* |
| 14396 | 14574 | ** The following are allowed values for Vdbe.magic |
| 14397 | 14575 | */ |
| @@ -14577,10 +14755,13 @@ | ||
| 14577 | 14755 | SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ |
| 14578 | 14756 | wsdStatInit; |
| 14579 | 14757 | if( op<0 || op>=ArraySize(wsdStat.nowValue) ){ |
| 14580 | 14758 | return SQLITE_MISUSE_BKPT; |
| 14581 | 14759 | } |
| 14760 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 14761 | + if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; | |
| 14762 | +#endif | |
| 14582 | 14763 | *pCurrent = wsdStat.nowValue[op]; |
| 14583 | 14764 | *pHighwater = wsdStat.mxValue[op]; |
| 14584 | 14765 | if( resetFlag ){ |
| 14585 | 14766 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
| 14586 | 14767 | } |
| @@ -14596,10 +14777,15 @@ | ||
| 14596 | 14777 | int *pCurrent, /* Write current value here */ |
| 14597 | 14778 | int *pHighwater, /* Write high-water mark here */ |
| 14598 | 14779 | int resetFlag /* Reset high-water mark if true */ |
| 14599 | 14780 | ){ |
| 14600 | 14781 | int rc = SQLITE_OK; /* Return code */ |
| 14782 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 14783 | + if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){ | |
| 14784 | + return SQLITE_MISUSE_BKPT; | |
| 14785 | + } | |
| 14786 | +#endif | |
| 14601 | 14787 | sqlite3_mutex_enter(db->mutex); |
| 14602 | 14788 | switch( op ){ |
| 14603 | 14789 | case SQLITE_DBSTATUS_LOOKASIDE_USED: { |
| 14604 | 14790 | *pCurrent = db->lookaside.nOut; |
| 14605 | 14791 | *pHighwater = db->lookaside.mxOut; |
| @@ -14774,11 +14960,11 @@ | ||
| 14774 | 14960 | ** |
| 14775 | 14961 | ** There is only one exported symbol in this file - the function |
| 14776 | 14962 | ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. |
| 14777 | 14963 | ** All other code has file scope. |
| 14778 | 14964 | ** |
| 14779 | -** SQLite processes all times and dates as Julian Day numbers. The | |
| 14965 | +** SQLite processes all times and dates as julian day numbers. The | |
| 14780 | 14966 | ** dates and times are stored as the number of days since noon |
| 14781 | 14967 | ** in Greenwich on November 24, 4714 B.C. according to the Gregorian |
| 14782 | 14968 | ** calendar system. |
| 14783 | 14969 | ** |
| 14784 | 14970 | ** 1970-01-01 00:00:00 is JD 2440587.5 |
| @@ -14789,11 +14975,11 @@ | ||
| 14789 | 14975 | ** be represented, even though julian day numbers allow a much wider |
| 14790 | 14976 | ** range of dates. |
| 14791 | 14977 | ** |
| 14792 | 14978 | ** The Gregorian calendar system is used for all dates and times, |
| 14793 | 14979 | ** even those that predate the Gregorian calendar. Historians usually |
| 14794 | -** use the Julian calendar for dates prior to 1582-10-15 and for some | |
| 14980 | +** use the julian calendar for dates prior to 1582-10-15 and for some | |
| 14795 | 14981 | ** dates afterwards, depending on locale. Beware of this difference. |
| 14796 | 14982 | ** |
| 14797 | 14983 | ** The conversion algorithms are implemented based on descriptions |
| 14798 | 14984 | ** in the following text: |
| 14799 | 14985 | ** |
| @@ -15061,11 +15247,11 @@ | ||
| 15061 | 15247 | return 1; |
| 15062 | 15248 | } |
| 15063 | 15249 | } |
| 15064 | 15250 | |
| 15065 | 15251 | /* |
| 15066 | -** Attempt to parse the given string into a Julian Day Number. Return | |
| 15252 | +** Attempt to parse the given string into a julian day number. Return | |
| 15067 | 15253 | ** the number of errors. |
| 15068 | 15254 | ** |
| 15069 | 15255 | ** The following are acceptable forms for the input string: |
| 15070 | 15256 | ** |
| 15071 | 15257 | ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM |
| @@ -15632,11 +15818,11 @@ | ||
| 15632 | 15818 | ** |
| 15633 | 15819 | ** %d day of month |
| 15634 | 15820 | ** %f ** fractional seconds SS.SSS |
| 15635 | 15821 | ** %H hour 00-24 |
| 15636 | 15822 | ** %j day of year 000-366 |
| 15637 | -** %J ** Julian day number | |
| 15823 | +** %J ** julian day number | |
| 15638 | 15824 | ** %m month 01-12 |
| 15639 | 15825 | ** %M minute 00-59 |
| 15640 | 15826 | ** %s seconds since 1970-01-01 |
| 15641 | 15827 | ** %S seconds 00-59 |
| 15642 | 15828 | ** %w day of week 0-6 sunday==0 |
| @@ -16257,10 +16443,14 @@ | ||
| 16257 | 16443 | MUTEX_LOGIC(sqlite3_mutex *mutex;) |
| 16258 | 16444 | #ifndef SQLITE_OMIT_AUTOINIT |
| 16259 | 16445 | int rc = sqlite3_initialize(); |
| 16260 | 16446 | if( rc ) return rc; |
| 16261 | 16447 | #endif |
| 16448 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 16449 | + if( pVfs==0 ) return SQLITE_MISUSE_BKPT; | |
| 16450 | +#endif | |
| 16451 | + | |
| 16262 | 16452 | MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 16263 | 16453 | sqlite3_mutex_enter(mutex); |
| 16264 | 16454 | vfsUnlink(pVfs); |
| 16265 | 16455 | if( makeDflt || vfsList==0 ){ |
| 16266 | 16456 | pVfs->pNext = vfsList; |
| @@ -18614,10 +18804,11 @@ | ||
| 18614 | 18804 | ** Retrieve a pointer to a static mutex or allocate a new dynamic one. |
| 18615 | 18805 | */ |
| 18616 | 18806 | SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){ |
| 18617 | 18807 | #ifndef SQLITE_OMIT_AUTOINIT |
| 18618 | 18808 | if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0; |
| 18809 | + if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0; | |
| 18619 | 18810 | #endif |
| 18620 | 18811 | return sqlite3GlobalConfig.mutex.xMutexAlloc(id); |
| 18621 | 18812 | } |
| 18622 | 18813 | |
| 18623 | 18814 | SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){ |
| @@ -19070,12 +19261,16 @@ | ||
| 19070 | 19261 | pthread_mutex_init(&p->mutex, 0); |
| 19071 | 19262 | } |
| 19072 | 19263 | break; |
| 19073 | 19264 | } |
| 19074 | 19265 | default: { |
| 19075 | - assert( iType-2 >= 0 ); | |
| 19076 | - assert( iType-2 < ArraySize(staticMutexes) ); | |
| 19266 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 19267 | + if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ | |
| 19268 | + (void)SQLITE_MISUSE_BKPT; | |
| 19269 | + return 0; | |
| 19270 | + } | |
| 19271 | +#endif | |
| 19077 | 19272 | p = &staticMutexes[iType-2]; |
| 19078 | 19273 | #if SQLITE_MUTEX_NREF |
| 19079 | 19274 | p->id = iType; |
| 19080 | 19275 | #endif |
| 19081 | 19276 | break; |
| @@ -20293,15 +20488,16 @@ | ||
| 20293 | 20488 | } |
| 20294 | 20489 | assert( sqlite3_mutex_notheld(mem0.mutex) ); |
| 20295 | 20490 | |
| 20296 | 20491 | |
| 20297 | 20492 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 20298 | - /* Verify that no more than two scratch allocations per thread | |
| 20299 | - ** are outstanding at one time. (This is only checked in the | |
| 20300 | - ** single-threaded case since checking in the multi-threaded case | |
| 20301 | - ** would be much more complicated.) */ | |
| 20302 | - assert( scratchAllocOut<=1 ); | |
| 20493 | + /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch | |
| 20494 | + ** buffers per thread. | |
| 20495 | + ** | |
| 20496 | + ** This can only be checked in single-threaded mode. | |
| 20497 | + */ | |
| 20498 | + assert( scratchAllocOut==0 ); | |
| 20303 | 20499 | if( p ) scratchAllocOut++; |
| 20304 | 20500 | #endif |
| 20305 | 20501 | |
| 20306 | 20502 | return p; |
| 20307 | 20503 | } |
| @@ -20956,10 +21152,17 @@ | ||
| 20956 | 21152 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
| 20957 | 21153 | #endif |
| 20958 | 21154 | PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ |
| 20959 | 21155 | char buf[etBUFSIZE]; /* Conversion buffer */ |
| 20960 | 21156 | |
| 21157 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 21158 | + if( ap==0 ){ | |
| 21159 | + (void)SQLITE_MISUSE_BKPT; | |
| 21160 | + sqlite3StrAccumReset(pAccum); | |
| 21161 | + return; | |
| 21162 | + } | |
| 21163 | +#endif | |
| 20961 | 21164 | bufpt = 0; |
| 20962 | 21165 | if( bFlags ){ |
| 20963 | 21166 | if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ |
| 20964 | 21167 | pArgList = va_arg(ap, PrintfArguments*); |
| 20965 | 21168 | } |
| @@ -21496,10 +21699,15 @@ | ||
| 21496 | 21699 | return N; |
| 21497 | 21700 | }else{ |
| 21498 | 21701 | char *zOld = (p->zText==p->zBase ? 0 : p->zText); |
| 21499 | 21702 | i64 szNew = p->nChar; |
| 21500 | 21703 | szNew += N + 1; |
| 21704 | + if( szNew+p->nChar<=p->mxAlloc ){ | |
| 21705 | + /* Force exponential buffer size growth as long as it does not overflow, | |
| 21706 | + ** to avoid having to call this routine too often */ | |
| 21707 | + szNew += p->nChar; | |
| 21708 | + } | |
| 21501 | 21709 | if( szNew > p->mxAlloc ){ |
| 21502 | 21710 | sqlite3StrAccumReset(p); |
| 21503 | 21711 | setStrAccumError(p, STRACCUM_TOOBIG); |
| 21504 | 21712 | return 0; |
| 21505 | 21713 | }else{ |
| @@ -21512,10 +21720,11 @@ | ||
| 21512 | 21720 | } |
| 21513 | 21721 | if( zNew ){ |
| 21514 | 21722 | assert( p->zText!=0 || p->nChar==0 ); |
| 21515 | 21723 | if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 21516 | 21724 | p->zText = zNew; |
| 21725 | + p->nAlloc = sqlite3DbMallocSize(p->db, zNew); | |
| 21517 | 21726 | }else{ |
| 21518 | 21727 | sqlite3StrAccumReset(p); |
| 21519 | 21728 | setStrAccumError(p, STRACCUM_NOMEM); |
| 21520 | 21729 | return 0; |
| 21521 | 21730 | } |
| @@ -21681,10 +21890,17 @@ | ||
| 21681 | 21890 | */ |
| 21682 | 21891 | SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){ |
| 21683 | 21892 | char *z; |
| 21684 | 21893 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
| 21685 | 21894 | StrAccum acc; |
| 21895 | + | |
| 21896 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 21897 | + if( zFormat==0 ){ | |
| 21898 | + (void)SQLITE_MISUSE_BKPT; | |
| 21899 | + return 0; | |
| 21900 | + } | |
| 21901 | +#endif | |
| 21686 | 21902 | #ifndef SQLITE_OMIT_AUTOINIT |
| 21687 | 21903 | if( sqlite3_initialize() ) return 0; |
| 21688 | 21904 | #endif |
| 21689 | 21905 | sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
| 21690 | 21906 | acc.useMalloc = 2; |
| @@ -21723,10 +21939,17 @@ | ||
| 21723 | 21939 | ** sqlite3_vsnprintf() is the varargs version. |
| 21724 | 21940 | */ |
| 21725 | 21941 | SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ |
| 21726 | 21942 | StrAccum acc; |
| 21727 | 21943 | if( n<=0 ) return zBuf; |
| 21944 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 21945 | + if( zBuf==0 || zFormat==0 ) { | |
| 21946 | + (void)SQLITE_MISUSE_BKPT; | |
| 21947 | + if( zBuf && n>0 ) zBuf[0] = 0; | |
| 21948 | + return zBuf; | |
| 21949 | + } | |
| 21950 | +#endif | |
| 21728 | 21951 | sqlite3StrAccumInit(&acc, zBuf, n, 0); |
| 21729 | 21952 | acc.useMalloc = 0; |
| 21730 | 21953 | sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 21731 | 21954 | return sqlite3StrAccumFinish(&acc); |
| 21732 | 21955 | } |
| @@ -21914,15 +22137,23 @@ | ||
| 21914 | 22137 | #else |
| 21915 | 22138 | # define wsdPrng sqlite3Prng |
| 21916 | 22139 | #endif |
| 21917 | 22140 | |
| 21918 | 22141 | #if SQLITE_THREADSAFE |
| 21919 | - sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); | |
| 22142 | + sqlite3_mutex *mutex; | |
| 22143 | +#endif | |
| 22144 | + | |
| 22145 | +#ifndef SQLITE_OMIT_AUTOINIT | |
| 22146 | + if( sqlite3_initialize() ) return; | |
| 22147 | +#endif | |
| 22148 | + | |
| 22149 | +#if SQLITE_THREADSAFE | |
| 22150 | + mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); | |
| 22151 | +#endif | |
| 22152 | + | |
| 21920 | 22153 | sqlite3_mutex_enter(mutex); |
| 21921 | -#endif | |
| 21922 | - | |
| 21923 | - if( N<=0 ){ | |
| 22154 | + if( N<=0 || pBuf==0 ){ | |
| 21924 | 22155 | wsdPrng.isInit = 0; |
| 21925 | 22156 | sqlite3_mutex_leave(mutex); |
| 21926 | 22157 | return; |
| 21927 | 22158 | } |
| 21928 | 22159 | |
| @@ -23040,17 +23271,27 @@ | ||
| 23040 | 23271 | ** case-independent fashion, using the same definition of "case |
| 23041 | 23272 | ** independence" that SQLite uses internally when comparing identifiers. |
| 23042 | 23273 | */ |
| 23043 | 23274 | SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){ |
| 23044 | 23275 | register unsigned char *a, *b; |
| 23276 | + if( zLeft==0 ){ | |
| 23277 | + return zRight ? -1 : 0; | |
| 23278 | + }else if( zRight==0 ){ | |
| 23279 | + return 1; | |
| 23280 | + } | |
| 23045 | 23281 | a = (unsigned char *)zLeft; |
| 23046 | 23282 | b = (unsigned char *)zRight; |
| 23047 | 23283 | while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 23048 | 23284 | return UpperToLower[*a] - UpperToLower[*b]; |
| 23049 | 23285 | } |
| 23050 | 23286 | SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ |
| 23051 | 23287 | register unsigned char *a, *b; |
| 23288 | + if( zLeft==0 ){ | |
| 23289 | + return zRight ? -1 : 0; | |
| 23290 | + }else if( zRight==0 ){ | |
| 23291 | + return 1; | |
| 23292 | + } | |
| 23052 | 23293 | a = (unsigned char *)zLeft; |
| 23053 | 23294 | b = (unsigned char *)zRight; |
| 23054 | 23295 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 23055 | 23296 | return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
| 23056 | 23297 | } |
| @@ -32579,10 +32820,15 @@ | ||
| 32579 | 32820 | #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL) |
| 32580 | 32821 | # error "WAL mode requires support from the Windows NT kernel, compile\ |
| 32581 | 32822 | with SQLITE_OMIT_WAL." |
| 32582 | 32823 | #endif |
| 32583 | 32824 | |
| 32825 | +#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0 | |
| 32826 | +# error "Memory mapped files require support from the Windows NT kernel,\ | |
| 32827 | + compile with SQLITE_MAX_MMAP_SIZE=0." | |
| 32828 | +#endif | |
| 32829 | + | |
| 32584 | 32830 | /* |
| 32585 | 32831 | ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions |
| 32586 | 32832 | ** based on the sub-platform)? |
| 32587 | 32833 | */ |
| 32588 | 32834 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI) |
| @@ -32708,14 +32954,15 @@ | ||
| 32708 | 32954 | # define winGetDirSep() '\\' |
| 32709 | 32955 | #endif |
| 32710 | 32956 | |
| 32711 | 32957 | /* |
| 32712 | 32958 | ** Do we need to manually define the Win32 file mapping APIs for use with WAL |
| 32713 | -** mode (e.g. these APIs are available in the Windows CE SDK; however, they | |
| 32714 | -** are not present in the header file)? | |
| 32959 | +** mode or memory mapped files (e.g. these APIs are available in the Windows | |
| 32960 | +** CE SDK; however, they are not present in the header file)? | |
| 32715 | 32961 | */ |
| 32716 | -#if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) | |
| 32962 | +#if SQLITE_WIN32_FILEMAPPING_API && \ | |
| 32963 | + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) | |
| 32717 | 32964 | /* |
| 32718 | 32965 | ** Two of the file mapping APIs are different under WinRT. Figure out which |
| 32719 | 32966 | ** set we need. |
| 32720 | 32967 | */ |
| 32721 | 32968 | #if SQLITE_OS_WINRT |
| @@ -32739,11 +32986,11 @@ | ||
| 32739 | 32986 | |
| 32740 | 32987 | /* |
| 32741 | 32988 | ** This file mapping API is common to both Win32 and WinRT. |
| 32742 | 32989 | */ |
| 32743 | 32990 | WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID); |
| 32744 | -#endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */ | |
| 32991 | +#endif /* SQLITE_WIN32_FILEMAPPING_API */ | |
| 32745 | 32992 | |
| 32746 | 32993 | /* |
| 32747 | 32994 | ** Some Microsoft compilers lack this definition. |
| 32748 | 32995 | */ |
| 32749 | 32996 | #ifndef INVALID_FILE_ATTRIBUTES |
| @@ -33032,21 +33279,21 @@ | ||
| 33032 | 33279 | |
| 33033 | 33280 | #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \ |
| 33034 | 33281 | LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent) |
| 33035 | 33282 | |
| 33036 | 33283 | #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \ |
| 33037 | - !defined(SQLITE_OMIT_WAL)) | |
| 33284 | + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) | |
| 33038 | 33285 | { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 }, |
| 33039 | 33286 | #else |
| 33040 | 33287 | { "CreateFileMappingA", (SYSCALL)0, 0 }, |
| 33041 | 33288 | #endif |
| 33042 | 33289 | |
| 33043 | 33290 | #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \ |
| 33044 | 33291 | DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent) |
| 33045 | 33292 | |
| 33046 | 33293 | #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ |
| 33047 | - !defined(SQLITE_OMIT_WAL)) | |
| 33294 | + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) | |
| 33048 | 33295 | { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 }, |
| 33049 | 33296 | #else |
| 33050 | 33297 | { "CreateFileMappingW", (SYSCALL)0, 0 }, |
| 33051 | 33298 | #endif |
| 33052 | 33299 | |
| @@ -33382,11 +33629,12 @@ | ||
| 33382 | 33629 | #ifndef osLockFileEx |
| 33383 | 33630 | #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \ |
| 33384 | 33631 | LPOVERLAPPED))aSyscall[48].pCurrent) |
| 33385 | 33632 | #endif |
| 33386 | 33633 | |
| 33387 | -#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)) | |
| 33634 | +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \ | |
| 33635 | + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) | |
| 33388 | 33636 | { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 }, |
| 33389 | 33637 | #else |
| 33390 | 33638 | { "MapViewOfFile", (SYSCALL)0, 0 }, |
| 33391 | 33639 | #endif |
| 33392 | 33640 | |
| @@ -33452,11 +33700,11 @@ | ||
| 33452 | 33700 | #endif |
| 33453 | 33701 | |
| 33454 | 33702 | #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
| 33455 | 33703 | LPOVERLAPPED))aSyscall[58].pCurrent) |
| 33456 | 33704 | |
| 33457 | -#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) | |
| 33705 | +#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 | |
| 33458 | 33706 | { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 }, |
| 33459 | 33707 | #else |
| 33460 | 33708 | { "UnmapViewOfFile", (SYSCALL)0, 0 }, |
| 33461 | 33709 | #endif |
| 33462 | 33710 | |
| @@ -33515,11 +33763,11 @@ | ||
| 33515 | 33763 | #endif |
| 33516 | 33764 | |
| 33517 | 33765 | #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \ |
| 33518 | 33766 | FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent) |
| 33519 | 33767 | |
| 33520 | -#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL) | |
| 33768 | +#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) | |
| 33521 | 33769 | { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 }, |
| 33522 | 33770 | #else |
| 33523 | 33771 | { "MapViewOfFileFromApp", (SYSCALL)0, 0 }, |
| 33524 | 33772 | #endif |
| 33525 | 33773 | |
| @@ -33579,11 +33827,11 @@ | ||
| 33579 | 33827 | |
| 33580 | 33828 | { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 }, |
| 33581 | 33829 | |
| 33582 | 33830 | #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent) |
| 33583 | 33831 | |
| 33584 | -#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL) | |
| 33832 | +#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) | |
| 33585 | 33833 | { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 }, |
| 33586 | 33834 | #else |
| 33587 | 33835 | { "CreateFileMappingFromApp", (SYSCALL)0, 0 }, |
| 33588 | 33836 | #endif |
| 33589 | 33837 | |
| @@ -39155,10 +39403,17 @@ | ||
| 39155 | 39403 | */ |
| 39156 | 39404 | SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){ |
| 39157 | 39405 | assert( pCache->pCache!=0 ); |
| 39158 | 39406 | sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache); |
| 39159 | 39407 | } |
| 39408 | + | |
| 39409 | +/* | |
| 39410 | +** Return the size of the header added by this middleware layer | |
| 39411 | +** in the page-cache hierarchy. | |
| 39412 | +*/ | |
| 39413 | +SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return sizeof(PgHdr); } | |
| 39414 | + | |
| 39160 | 39415 | |
| 39161 | 39416 | #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) |
| 39162 | 39417 | /* |
| 39163 | 39418 | ** For all dirty pages currently in the cache, invoke the specified |
| 39164 | 39419 | ** callback. This is only used if the SQLITE_CHECK_PAGES macro is |
| @@ -40154,10 +40409,15 @@ | ||
| 40154 | 40409 | pcache1Shrink /* xShrink */ |
| 40155 | 40410 | }; |
| 40156 | 40411 | sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods); |
| 40157 | 40412 | } |
| 40158 | 40413 | |
| 40414 | +/* | |
| 40415 | +** Return the size of the header on each page of this PCACHE implementation. | |
| 40416 | +*/ | |
| 40417 | +SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return sizeof(PgHdr1); } | |
| 40418 | + | |
| 40159 | 40419 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 40160 | 40420 | /* |
| 40161 | 40421 | ** This function is called to free superfluous dynamically allocated memory |
| 40162 | 40422 | ** held by the pager system. Memory in use by any SQLite pager allocated |
| 40163 | 40423 | ** by the current thread may be sqlite3_free()ed. |
| @@ -43763,11 +44023,11 @@ | ||
| 43763 | 44023 | ** should be page numbers which are never 0xffffffff. So filling |
| 43764 | 44024 | ** pPager->dbFileVers[] with all 0xff bytes should suffice. |
| 43765 | 44025 | ** |
| 43766 | 44026 | ** For an encrypted database, the situation is more complex: bytes |
| 43767 | 44027 | ** 24..39 of the database are white noise. But the probability of |
| 43768 | - ** white noising equaling 16 bytes of 0xff is vanishingly small so | |
| 44028 | + ** white noise equaling 16 bytes of 0xff is vanishingly small so | |
| 43769 | 44029 | ** we should still be ok. |
| 43770 | 44030 | */ |
| 43771 | 44031 | memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers)); |
| 43772 | 44032 | }else{ |
| 43773 | 44033 | u8 *dbFileVers = &((u8*)pPg->pData)[24]; |
| @@ -47710,10 +47970,22 @@ | ||
| 47710 | 47970 | } |
| 47711 | 47971 | |
| 47712 | 47972 | return SQLITE_OK; |
| 47713 | 47973 | } |
| 47714 | 47974 | #endif |
| 47975 | + | |
| 47976 | +/* | |
| 47977 | +** The page handle passed as the first argument refers to a dirty page | |
| 47978 | +** with a page number other than iNew. This function changes the page's | |
| 47979 | +** page number to iNew and sets the value of the PgHdr.flags field to | |
| 47980 | +** the value passed as the third parameter. | |
| 47981 | +*/ | |
| 47982 | +SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){ | |
| 47983 | + assert( pPg->pgno!=iNew ); | |
| 47984 | + pPg->flags = flags; | |
| 47985 | + sqlite3PcacheMove(pPg, iNew); | |
| 47986 | +} | |
| 47715 | 47987 | |
| 47716 | 47988 | /* |
| 47717 | 47989 | ** Return a pointer to the data for the specified page. |
| 47718 | 47990 | */ |
| 47719 | 47991 | SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){ |
| @@ -48108,10 +48380,11 @@ | ||
| 48108 | 48380 | SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){ |
| 48109 | 48381 | assert( pPager->eState>=PAGER_READER ); |
| 48110 | 48382 | return sqlite3WalFramesize(pPager->pWal); |
| 48111 | 48383 | } |
| 48112 | 48384 | #endif |
| 48385 | + | |
| 48113 | 48386 | |
| 48114 | 48387 | #endif /* SQLITE_OMIT_DISKIO */ |
| 48115 | 48388 | |
| 48116 | 48389 | /************** End of pager.c ***********************************************/ |
| 48117 | 48390 | /************** Begin file wal.c *********************************************/ |
| @@ -49618,11 +49891,11 @@ | ||
| 49618 | 49891 | |
| 49619 | 49892 | /* |
| 49620 | 49893 | ** Free an iterator allocated by walIteratorInit(). |
| 49621 | 49894 | */ |
| 49622 | 49895 | static void walIteratorFree(WalIterator *p){ |
| 49623 | - sqlite3ScratchFree(p); | |
| 49896 | + sqlite3_free(p); | |
| 49624 | 49897 | } |
| 49625 | 49898 | |
| 49626 | 49899 | /* |
| 49627 | 49900 | ** Construct a WalInterator object that can be used to loop over all |
| 49628 | 49901 | ** pages in the WAL in ascending order. The caller must hold the checkpoint |
| @@ -49653,21 +49926,21 @@ | ||
| 49653 | 49926 | /* Allocate space for the WalIterator object. */ |
| 49654 | 49927 | nSegment = walFramePage(iLast) + 1; |
| 49655 | 49928 | nByte = sizeof(WalIterator) |
| 49656 | 49929 | + (nSegment-1)*sizeof(struct WalSegment) |
| 49657 | 49930 | + iLast*sizeof(ht_slot); |
| 49658 | - p = (WalIterator *)sqlite3ScratchMalloc(nByte); | |
| 49931 | + p = (WalIterator *)sqlite3_malloc(nByte); | |
| 49659 | 49932 | if( !p ){ |
| 49660 | 49933 | return SQLITE_NOMEM; |
| 49661 | 49934 | } |
| 49662 | 49935 | memset(p, 0, nByte); |
| 49663 | 49936 | p->nSegment = nSegment; |
| 49664 | 49937 | |
| 49665 | 49938 | /* Allocate temporary space used by the merge-sort routine. This block |
| 49666 | 49939 | ** of memory will be freed before this function returns. |
| 49667 | 49940 | */ |
| 49668 | - aTmp = (ht_slot *)sqlite3ScratchMalloc( | |
| 49941 | + aTmp = (ht_slot *)sqlite3_malloc( | |
| 49669 | 49942 | sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast) |
| 49670 | 49943 | ); |
| 49671 | 49944 | if( !aTmp ){ |
| 49672 | 49945 | rc = SQLITE_NOMEM; |
| 49673 | 49946 | } |
| @@ -49700,11 +49973,11 @@ | ||
| 49700 | 49973 | p->aSegment[i].nEntry = nEntry; |
| 49701 | 49974 | p->aSegment[i].aIndex = aIndex; |
| 49702 | 49975 | p->aSegment[i].aPgno = (u32 *)aPgno; |
| 49703 | 49976 | } |
| 49704 | 49977 | } |
| 49705 | - sqlite3ScratchFree(aTmp); | |
| 49978 | + sqlite3_free(aTmp); | |
| 49706 | 49979 | |
| 49707 | 49980 | if( rc!=SQLITE_OK ){ |
| 49708 | 49981 | walIteratorFree(p); |
| 49709 | 49982 | } |
| 49710 | 49983 | *pp = p; |
| @@ -53331,10 +53604,15 @@ | ||
| 53331 | 53604 | /* |
| 53332 | 53605 | ** Defragment the page given. All Cells are moved to the |
| 53333 | 53606 | ** end of the page and all free space is collected into one |
| 53334 | 53607 | ** big FreeBlk that occurs in between the header and cell |
| 53335 | 53608 | ** pointer array and the cell content area. |
| 53609 | +** | |
| 53610 | +** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a | |
| 53611 | +** b-tree page so that there are no freeblocks or fragment bytes, all | |
| 53612 | +** unused bytes are contained in the unallocated space region, and all | |
| 53613 | +** cells are packed tightly at the end of the page. | |
| 53336 | 53614 | */ |
| 53337 | 53615 | static int defragmentPage(MemPage *pPage){ |
| 53338 | 53616 | int i; /* Loop counter */ |
| 53339 | 53617 | int pc; /* Address of the i-th cell */ |
| 53340 | 53618 | int hdr; /* Offset to the page header */ |
| @@ -53343,28 +53621,27 @@ | ||
| 53343 | 53621 | int cellOffset; /* Offset to the cell pointer array */ |
| 53344 | 53622 | int cbrk; /* Offset to the cell content area */ |
| 53345 | 53623 | int nCell; /* Number of cells on the page */ |
| 53346 | 53624 | unsigned char *data; /* The page data */ |
| 53347 | 53625 | unsigned char *temp; /* Temp area for cell content */ |
| 53626 | + unsigned char *src; /* Source of content */ | |
| 53348 | 53627 | int iCellFirst; /* First allowable cell index */ |
| 53349 | 53628 | int iCellLast; /* Last possible cell index */ |
| 53350 | 53629 | |
| 53351 | 53630 | |
| 53352 | 53631 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53353 | 53632 | assert( pPage->pBt!=0 ); |
| 53354 | 53633 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
| 53355 | 53634 | assert( pPage->nOverflow==0 ); |
| 53356 | 53635 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53357 | - temp = sqlite3PagerTempSpace(pPage->pBt->pPager); | |
| 53358 | - data = pPage->aData; | |
| 53636 | + temp = 0; | |
| 53637 | + src = data = pPage->aData; | |
| 53359 | 53638 | hdr = pPage->hdrOffset; |
| 53360 | 53639 | cellOffset = pPage->cellOffset; |
| 53361 | 53640 | nCell = pPage->nCell; |
| 53362 | 53641 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 53363 | 53642 | usableSize = pPage->pBt->usableSize; |
| 53364 | - cbrk = get2byte(&data[hdr+5]); | |
| 53365 | - memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); | |
| 53366 | 53643 | cbrk = usableSize; |
| 53367 | 53644 | iCellFirst = cellOffset + 2*nCell; |
| 53368 | 53645 | iCellLast = usableSize - 4; |
| 53369 | 53646 | for(i=0; i<nCell; i++){ |
| 53370 | 53647 | u8 *pAddr; /* The i-th cell pointer */ |
| @@ -53379,11 +53656,11 @@ | ||
| 53379 | 53656 | if( pc<iCellFirst || pc>iCellLast ){ |
| 53380 | 53657 | return SQLITE_CORRUPT_BKPT; |
| 53381 | 53658 | } |
| 53382 | 53659 | #endif |
| 53383 | 53660 | assert( pc>=iCellFirst && pc<=iCellLast ); |
| 53384 | - size = cellSizePtr(pPage, &temp[pc]); | |
| 53661 | + size = cellSizePtr(pPage, &src[pc]); | |
| 53385 | 53662 | cbrk -= size; |
| 53386 | 53663 | #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 53387 | 53664 | if( cbrk<iCellFirst ){ |
| 53388 | 53665 | return SQLITE_CORRUPT_BKPT; |
| 53389 | 53666 | } |
| @@ -53393,12 +53670,20 @@ | ||
| 53393 | 53670 | } |
| 53394 | 53671 | #endif |
| 53395 | 53672 | assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); |
| 53396 | 53673 | testcase( cbrk+size==usableSize ); |
| 53397 | 53674 | testcase( pc+size==usableSize ); |
| 53398 | - memcpy(&data[cbrk], &temp[pc], size); | |
| 53399 | 53675 | put2byte(pAddr, cbrk); |
| 53676 | + if( temp==0 ){ | |
| 53677 | + int x; | |
| 53678 | + if( cbrk==pc ) continue; | |
| 53679 | + temp = sqlite3PagerTempSpace(pPage->pBt->pPager); | |
| 53680 | + x = get2byte(&data[hdr+5]); | |
| 53681 | + memcpy(&temp[x], &data[x], (cbrk+size) - x); | |
| 53682 | + src = temp; | |
| 53683 | + } | |
| 53684 | + memcpy(&data[cbrk], &src[pc], size); | |
| 53400 | 53685 | } |
| 53401 | 53686 | assert( cbrk>=iCellFirst ); |
| 53402 | 53687 | put2byte(&data[hdr+5], cbrk); |
| 53403 | 53688 | data[hdr+1] = 0; |
| 53404 | 53689 | data[hdr+2] = 0; |
| @@ -53408,10 +53693,73 @@ | ||
| 53408 | 53693 | if( cbrk-iCellFirst!=pPage->nFree ){ |
| 53409 | 53694 | return SQLITE_CORRUPT_BKPT; |
| 53410 | 53695 | } |
| 53411 | 53696 | return SQLITE_OK; |
| 53412 | 53697 | } |
| 53698 | + | |
| 53699 | +/* | |
| 53700 | +** Search the free-list on page pPg for space to store a cell nByte bytes in | |
| 53701 | +** size. If one can be found, return a pointer to the space and remove it | |
| 53702 | +** from the free-list. | |
| 53703 | +** | |
| 53704 | +** If no suitable space can be found on the free-list, return NULL. | |
| 53705 | +** | |
| 53706 | +** This function may detect corruption within pPg. If corruption is | |
| 53707 | +** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned. | |
| 53708 | +** | |
| 53709 | +** If a slot of at least nByte bytes is found but cannot be used because | |
| 53710 | +** there are already at least 60 fragmented bytes on the page, return NULL. | |
| 53711 | +** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true. | |
| 53712 | +*/ | |
| 53713 | +static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){ | |
| 53714 | + const int hdr = pPg->hdrOffset; | |
| 53715 | + u8 * const aData = pPg->aData; | |
| 53716 | + int iAddr; | |
| 53717 | + int pc; | |
| 53718 | + int usableSize = pPg->pBt->usableSize; | |
| 53719 | + | |
| 53720 | + for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){ | |
| 53721 | + int size; /* Size of the free slot */ | |
| 53722 | + /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of | |
| 53723 | + ** increasing offset. */ | |
| 53724 | + if( pc>usableSize-4 || pc<iAddr+4 ){ | |
| 53725 | + *pRc = SQLITE_CORRUPT_BKPT; | |
| 53726 | + return 0; | |
| 53727 | + } | |
| 53728 | + /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each | |
| 53729 | + ** freeblock form a big-endian integer which is the size of the freeblock | |
| 53730 | + ** in bytes, including the 4-byte header. */ | |
| 53731 | + size = get2byte(&aData[pc+2]); | |
| 53732 | + if( size>=nByte ){ | |
| 53733 | + int x = size - nByte; | |
| 53734 | + testcase( x==4 ); | |
| 53735 | + testcase( x==3 ); | |
| 53736 | + if( x<4 ){ | |
| 53737 | + /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total | |
| 53738 | + ** number of bytes in fragments may not exceed 60. */ | |
| 53739 | + if( aData[hdr+7]>=60 ){ | |
| 53740 | + if( pbDefrag ) *pbDefrag = 1; | |
| 53741 | + return 0; | |
| 53742 | + } | |
| 53743 | + /* Remove the slot from the free-list. Update the number of | |
| 53744 | + ** fragmented bytes within the page. */ | |
| 53745 | + memcpy(&aData[iAddr], &aData[pc], 2); | |
| 53746 | + aData[hdr+7] += (u8)x; | |
| 53747 | + }else if( size+pc > usableSize ){ | |
| 53748 | + *pRc = SQLITE_CORRUPT_BKPT; | |
| 53749 | + return 0; | |
| 53750 | + }else{ | |
| 53751 | + /* The slot remains on the free-list. Reduce its size to account | |
| 53752 | + ** for the portion used by the new allocation. */ | |
| 53753 | + put2byte(&aData[pc+2], x); | |
| 53754 | + } | |
| 53755 | + return &aData[pc + x]; | |
| 53756 | + } | |
| 53757 | + } | |
| 53758 | + | |
| 53759 | + return 0; | |
| 53760 | +} | |
| 53413 | 53761 | |
| 53414 | 53762 | /* |
| 53415 | 53763 | ** Allocate nByte bytes of space from within the B-Tree page passed |
| 53416 | 53764 | ** as the first argument. Write into *pIdx the index into pPage->aData[] |
| 53417 | 53765 | ** of the first byte of allocated space. Return either SQLITE_OK or |
| @@ -53426,79 +53774,57 @@ | ||
| 53426 | 53774 | */ |
| 53427 | 53775 | static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
| 53428 | 53776 | const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ |
| 53429 | 53777 | u8 * const data = pPage->aData; /* Local cache of pPage->aData */ |
| 53430 | 53778 | int top; /* First byte of cell content area */ |
| 53779 | + int rc = SQLITE_OK; /* Integer return code */ | |
| 53431 | 53780 | int gap; /* First byte of gap between cell pointers and cell content */ |
| 53432 | - int rc; /* Integer return code */ | |
| 53433 | - int usableSize; /* Usable size of the page */ | |
| 53434 | 53781 | |
| 53435 | 53782 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53436 | 53783 | assert( pPage->pBt ); |
| 53437 | 53784 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53438 | 53785 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 53439 | 53786 | assert( pPage->nFree>=nByte ); |
| 53440 | 53787 | assert( pPage->nOverflow==0 ); |
| 53441 | - usableSize = pPage->pBt->usableSize; | |
| 53442 | - assert( nByte < usableSize-8 ); | |
| 53788 | + assert( nByte < (int)(pPage->pBt->usableSize-8) ); | |
| 53443 | 53789 | |
| 53444 | 53790 | assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); |
| 53445 | 53791 | gap = pPage->cellOffset + 2*pPage->nCell; |
| 53446 | 53792 | assert( gap<=65536 ); |
| 53447 | - top = get2byte(&data[hdr+5]); | |
| 53448 | - if( gap>top ){ | |
| 53449 | - if( top==0 ){ | |
| 53450 | - top = 65536; | |
| 53451 | - }else{ | |
| 53452 | - return SQLITE_CORRUPT_BKPT; | |
| 53453 | - } | |
| 53454 | - } | |
| 53793 | + /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size | |
| 53794 | + ** and the reserved space is zero (the usual value for reserved space) | |
| 53795 | + ** then the cell content offset of an empty page wants to be 65536. | |
| 53796 | + ** However, that integer is too large to be stored in a 2-byte unsigned | |
| 53797 | + ** integer, so a value of 0 is used in its place. */ | |
| 53798 | + top = get2byteNotZero(&data[hdr+5]); | |
| 53799 | + if( gap>top ) return SQLITE_CORRUPT_BKPT; | |
| 53455 | 53800 | |
| 53456 | 53801 | /* If there is enough space between gap and top for one more cell pointer |
| 53457 | 53802 | ** array entry offset, and if the freelist is not empty, then search the |
| 53458 | 53803 | ** freelist looking for a free slot big enough to satisfy the request. |
| 53459 | 53804 | */ |
| 53460 | 53805 | testcase( gap+2==top ); |
| 53461 | 53806 | testcase( gap+1==top ); |
| 53462 | 53807 | testcase( gap==top ); |
| 53463 | 53808 | if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){ |
| 53464 | - int pc, addr; | |
| 53465 | - for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){ | |
| 53466 | - int size; /* Size of the free slot */ | |
| 53467 | - if( pc>usableSize-4 || pc<addr+4 ){ | |
| 53468 | - return SQLITE_CORRUPT_BKPT; | |
| 53469 | - } | |
| 53470 | - size = get2byte(&data[pc+2]); | |
| 53471 | - if( size>=nByte ){ | |
| 53472 | - int x = size - nByte; | |
| 53473 | - testcase( x==4 ); | |
| 53474 | - testcase( x==3 ); | |
| 53475 | - if( x<4 ){ | |
| 53476 | - if( data[hdr+7]>=60 ) goto defragment_page; | |
| 53477 | - /* Remove the slot from the free-list. Update the number of | |
| 53478 | - ** fragmented bytes within the page. */ | |
| 53479 | - memcpy(&data[addr], &data[pc], 2); | |
| 53480 | - data[hdr+7] += (u8)x; | |
| 53481 | - }else if( size+pc > usableSize ){ | |
| 53482 | - return SQLITE_CORRUPT_BKPT; | |
| 53483 | - }else{ | |
| 53484 | - /* The slot remains on the free-list. Reduce its size to account | |
| 53485 | - ** for the portion used by the new allocation. */ | |
| 53486 | - put2byte(&data[pc+2], x); | |
| 53487 | - } | |
| 53488 | - *pIdx = pc + x; | |
| 53489 | - return SQLITE_OK; | |
| 53490 | - } | |
| 53809 | + int bDefrag = 0; | |
| 53810 | + u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag); | |
| 53811 | + if( rc ) return rc; | |
| 53812 | + if( bDefrag ) goto defragment_page; | |
| 53813 | + if( pSpace ){ | |
| 53814 | + assert( pSpace>=data && (pSpace - data)<65536 ); | |
| 53815 | + *pIdx = (int)(pSpace - data); | |
| 53816 | + return SQLITE_OK; | |
| 53491 | 53817 | } |
| 53492 | 53818 | } |
| 53493 | 53819 | |
| 53494 | 53820 | /* The request could not be fulfilled using a freelist slot. Check |
| 53495 | 53821 | ** to see if defragmentation is necessary. |
| 53496 | 53822 | */ |
| 53497 | 53823 | testcase( gap+2+nByte==top ); |
| 53498 | 53824 | if( gap+2+nByte>top ){ |
| 53499 | -defragment_page: | |
| 53825 | + defragment_page: | |
| 53500 | 53826 | testcase( pPage->nCell==0 ); |
| 53501 | 53827 | rc = defragmentPage(pPage); |
| 53502 | 53828 | if( rc ) return rc; |
| 53503 | 53829 | top = get2byteNotZero(&data[hdr+5]); |
| 53504 | 53830 | assert( gap+nByte<=top ); |
| @@ -53542,11 +53868,11 @@ | ||
| 53542 | 53868 | unsigned char *data = pPage->aData; /* Page content */ |
| 53543 | 53869 | |
| 53544 | 53870 | assert( pPage->pBt!=0 ); |
| 53545 | 53871 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53546 | 53872 | assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
| 53547 | - assert( iEnd <= pPage->pBt->usableSize ); | |
| 53873 | + assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); | |
| 53548 | 53874 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53549 | 53875 | assert( iSize>=4 ); /* Minimum cell size is 4 */ |
| 53550 | 53876 | assert( iStart<=iLast ); |
| 53551 | 53877 | |
| 53552 | 53878 | /* Overwrite deleted information with zeros when the secure_delete |
| @@ -53637,22 +53963,36 @@ | ||
| 53637 | 53963 | pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
| 53638 | 53964 | flagByte &= ~PTF_LEAF; |
| 53639 | 53965 | pPage->childPtrSize = 4-4*pPage->leaf; |
| 53640 | 53966 | pBt = pPage->pBt; |
| 53641 | 53967 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 53968 | + /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior | |
| 53969 | + ** table b-tree page. */ | |
| 53970 | + assert( (PTF_LEAFDATA|PTF_INTKEY)==5 ); | |
| 53971 | + /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf | |
| 53972 | + ** table b-tree page. */ | |
| 53973 | + assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 ); | |
| 53642 | 53974 | pPage->intKey = 1; |
| 53643 | 53975 | pPage->intKeyLeaf = pPage->leaf; |
| 53644 | 53976 | pPage->noPayload = !pPage->leaf; |
| 53645 | 53977 | pPage->maxLocal = pBt->maxLeaf; |
| 53646 | 53978 | pPage->minLocal = pBt->minLeaf; |
| 53647 | 53979 | }else if( flagByte==PTF_ZERODATA ){ |
| 53980 | + /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior | |
| 53981 | + ** index b-tree page. */ | |
| 53982 | + assert( (PTF_ZERODATA)==2 ); | |
| 53983 | + /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf | |
| 53984 | + ** index b-tree page. */ | |
| 53985 | + assert( (PTF_ZERODATA|PTF_LEAF)==10 ); | |
| 53648 | 53986 | pPage->intKey = 0; |
| 53649 | 53987 | pPage->intKeyLeaf = 0; |
| 53650 | 53988 | pPage->noPayload = 0; |
| 53651 | 53989 | pPage->maxLocal = pBt->maxLocal; |
| 53652 | 53990 | pPage->minLocal = pBt->minLocal; |
| 53653 | 53991 | }else{ |
| 53992 | + /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is | |
| 53993 | + ** an error. */ | |
| 53654 | 53994 | return SQLITE_CORRUPT_BKPT; |
| 53655 | 53995 | } |
| 53656 | 53996 | pPage->max1bytePayload = pBt->max1bytePayload; |
| 53657 | 53997 | return SQLITE_OK; |
| 53658 | 53998 | } |
| @@ -53688,25 +54028,37 @@ | ||
| 53688 | 54028 | |
| 53689 | 54029 | pBt = pPage->pBt; |
| 53690 | 54030 | |
| 53691 | 54031 | hdr = pPage->hdrOffset; |
| 53692 | 54032 | data = pPage->aData; |
| 54033 | + /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating | |
| 54034 | + ** the b-tree page type. */ | |
| 53693 | 54035 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
| 53694 | 54036 | assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 53695 | 54037 | pPage->maskPage = (u16)(pBt->pageSize - 1); |
| 53696 | 54038 | pPage->nOverflow = 0; |
| 53697 | 54039 | usableSize = pBt->usableSize; |
| 53698 | - pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; | |
| 54040 | + pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize; | |
| 53699 | 54041 | pPage->aDataEnd = &data[usableSize]; |
| 53700 | 54042 | pPage->aCellIdx = &data[cellOffset]; |
| 54043 | + /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates | |
| 54044 | + ** the start of the cell content area. A zero value for this integer is | |
| 54045 | + ** interpreted as 65536. */ | |
| 53701 | 54046 | top = get2byteNotZero(&data[hdr+5]); |
| 54047 | + /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the | |
| 54048 | + ** number of cells on the page. */ | |
| 53702 | 54049 | pPage->nCell = get2byte(&data[hdr+3]); |
| 53703 | 54050 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 53704 | 54051 | /* To many cells for a single page. The page must be corrupt */ |
| 53705 | 54052 | return SQLITE_CORRUPT_BKPT; |
| 53706 | 54053 | } |
| 53707 | 54054 | testcase( pPage->nCell==MX_CELL(pBt) ); |
| 54055 | + /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only | |
| 54056 | + ** possible for a root page of a table that contains no rows) then the | |
| 54057 | + ** offset to the cell content area will equal the page size minus the | |
| 54058 | + ** bytes of reserved space. */ | |
| 54059 | + assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB ); | |
| 53708 | 54060 | |
| 53709 | 54061 | /* A malformed database page might cause us to read past the end |
| 53710 | 54062 | ** of page when parsing a cell. |
| 53711 | 54063 | ** |
| 53712 | 54064 | ** The following block of code checks early to see if a cell extends |
| @@ -53736,17 +54088,24 @@ | ||
| 53736 | 54088 | } |
| 53737 | 54089 | if( !pPage->leaf ) iCellLast++; |
| 53738 | 54090 | } |
| 53739 | 54091 | #endif |
| 53740 | 54092 | |
| 53741 | - /* Compute the total free space on the page */ | |
| 54093 | + /* Compute the total free space on the page | |
| 54094 | + ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the | |
| 54095 | + ** start of the first freeblock on the page, or is zero if there are no | |
| 54096 | + ** freeblocks. */ | |
| 53742 | 54097 | pc = get2byte(&data[hdr+1]); |
| 53743 | - nFree = data[hdr+7] + top; | |
| 54098 | + nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */ | |
| 53744 | 54099 | while( pc>0 ){ |
| 53745 | 54100 | u16 next, size; |
| 53746 | 54101 | if( pc<iCellFirst || pc>iCellLast ){ |
| 53747 | - /* Start of free block is off the page */ | |
| 54102 | + /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will | |
| 54103 | + ** always be at least one cell before the first freeblock. | |
| 54104 | + ** | |
| 54105 | + ** Or, the freeblock is off the end of the page | |
| 54106 | + */ | |
| 53748 | 54107 | return SQLITE_CORRUPT_BKPT; |
| 53749 | 54108 | } |
| 53750 | 54109 | next = get2byte(&data[pc]); |
| 53751 | 54110 | size = get2byte(&data[pc+2]); |
| 53752 | 54111 | if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){ |
| @@ -54148,10 +54507,13 @@ | ||
| 54148 | 54507 | pBt->pPage1 = 0; |
| 54149 | 54508 | if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY; |
| 54150 | 54509 | #ifdef SQLITE_SECURE_DELETE |
| 54151 | 54510 | pBt->btsFlags |= BTS_SECURE_DELETE; |
| 54152 | 54511 | #endif |
| 54512 | + /* EVIDENCE-OF: R-51873-39618 The page size for a database file is | |
| 54513 | + ** determined by the 2-byte integer located at an offset of 16 bytes from | |
| 54514 | + ** the beginning of the database file. */ | |
| 54153 | 54515 | pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); |
| 54154 | 54516 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 54155 | 54517 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
| 54156 | 54518 | pBt->pageSize = 0; |
| 54157 | 54519 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| @@ -54166,10 +54528,13 @@ | ||
| 54166 | 54528 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 54167 | 54529 | } |
| 54168 | 54530 | #endif |
| 54169 | 54531 | nReserve = 0; |
| 54170 | 54532 | }else{ |
| 54533 | + /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is | |
| 54534 | + ** determined by the one-byte unsigned integer found at an offset of 20 | |
| 54535 | + ** into the database file header. */ | |
| 54171 | 54536 | nReserve = zDbHeader[20]; |
| 54172 | 54537 | pBt->btsFlags |= BTS_PAGESIZE_FIXED; |
| 54173 | 54538 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 54174 | 54539 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 54175 | 54540 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| @@ -54675,10 +55040,13 @@ | ||
| 54675 | 55040 | if( nPage>0 ){ |
| 54676 | 55041 | u32 pageSize; |
| 54677 | 55042 | u32 usableSize; |
| 54678 | 55043 | u8 *page1 = pPage1->aData; |
| 54679 | 55044 | rc = SQLITE_NOTADB; |
| 55045 | + /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins | |
| 55046 | + ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d | |
| 55047 | + ** 61 74 20 33 00. */ | |
| 54680 | 55048 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
| 54681 | 55049 | goto page1_init_failed; |
| 54682 | 55050 | } |
| 54683 | 55051 | |
| 54684 | 55052 | #ifdef SQLITE_OMIT_WAL |
| @@ -54715,26 +55083,39 @@ | ||
| 54715 | 55083 | } |
| 54716 | 55084 | rc = SQLITE_NOTADB; |
| 54717 | 55085 | } |
| 54718 | 55086 | #endif |
| 54719 | 55087 | |
| 54720 | - /* The maximum embedded fraction must be exactly 25%. And the minimum | |
| 54721 | - ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. | |
| 55088 | + /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload | |
| 55089 | + ** fractions and the leaf payload fraction values must be 64, 32, and 32. | |
| 55090 | + ** | |
| 54722 | 55091 | ** The original design allowed these amounts to vary, but as of |
| 54723 | 55092 | ** version 3.6.0, we require them to be fixed. |
| 54724 | 55093 | */ |
| 54725 | 55094 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 54726 | 55095 | goto page1_init_failed; |
| 54727 | 55096 | } |
| 55097 | + /* EVIDENCE-OF: R-51873-39618 The page size for a database file is | |
| 55098 | + ** determined by the 2-byte integer located at an offset of 16 bytes from | |
| 55099 | + ** the beginning of the database file. */ | |
| 54728 | 55100 | pageSize = (page1[16]<<8) | (page1[17]<<16); |
| 55101 | + /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two | |
| 55102 | + ** between 512 and 65536 inclusive. */ | |
| 54729 | 55103 | if( ((pageSize-1)&pageSize)!=0 |
| 54730 | 55104 | || pageSize>SQLITE_MAX_PAGE_SIZE |
| 54731 | 55105 | || pageSize<=256 |
| 54732 | 55106 | ){ |
| 54733 | 55107 | goto page1_init_failed; |
| 54734 | 55108 | } |
| 54735 | 55109 | assert( (pageSize & 7)==0 ); |
| 55110 | + /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte | |
| 55111 | + ** integer at offset 20 is the number of bytes of space at the end of | |
| 55112 | + ** each page to reserve for extensions. | |
| 55113 | + ** | |
| 55114 | + ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is | |
| 55115 | + ** determined by the one-byte unsigned integer found at an offset of 20 | |
| 55116 | + ** into the database file header. */ | |
| 54736 | 55117 | usableSize = pageSize - page1[20]; |
| 54737 | 55118 | if( (u32)pageSize!=pBt->pageSize ){ |
| 54738 | 55119 | /* After reading the first page of the database assuming a page size |
| 54739 | 55120 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 54740 | 55121 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| @@ -54751,10 +55132,13 @@ | ||
| 54751 | 55132 | } |
| 54752 | 55133 | if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){ |
| 54753 | 55134 | rc = SQLITE_CORRUPT_BKPT; |
| 54754 | 55135 | goto page1_init_failed; |
| 54755 | 55136 | } |
| 55137 | + /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to | |
| 55138 | + ** be less than 480. In other words, if the page size is 512, then the | |
| 55139 | + ** reserved space size cannot exceed 32. */ | |
| 54756 | 55140 | if( usableSize<480 ){ |
| 54757 | 55141 | goto page1_init_failed; |
| 54758 | 55142 | } |
| 54759 | 55143 | pBt->pageSize = pageSize; |
| 54760 | 55144 | pBt->usableSize = usableSize; |
| @@ -57328,10 +57712,12 @@ | ||
| 57328 | 57712 | |
| 57329 | 57713 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 57330 | 57714 | assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) ); |
| 57331 | 57715 | pPage1 = pBt->pPage1; |
| 57332 | 57716 | mxPage = btreePagecount(pBt); |
| 57717 | + /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36 | |
| 57718 | + ** stores stores the total number of pages on the freelist. */ | |
| 57333 | 57719 | n = get4byte(&pPage1->aData[36]); |
| 57334 | 57720 | testcase( n==mxPage-1 ); |
| 57335 | 57721 | if( n>=mxPage ){ |
| 57336 | 57722 | return SQLITE_CORRUPT_BKPT; |
| 57337 | 57723 | } |
| @@ -57374,12 +57760,18 @@ | ||
| 57374 | 57760 | ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT) |
| 57375 | 57761 | */ |
| 57376 | 57762 | do { |
| 57377 | 57763 | pPrevTrunk = pTrunk; |
| 57378 | 57764 | if( pPrevTrunk ){ |
| 57765 | + /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page | |
| 57766 | + ** is the page number of the next freelist trunk page in the list or | |
| 57767 | + ** zero if this is the last freelist trunk page. */ | |
| 57379 | 57768 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
| 57380 | 57769 | }else{ |
| 57770 | + /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32 | |
| 57771 | + ** stores the page number of the first page of the freelist, or zero if | |
| 57772 | + ** the freelist is empty. */ | |
| 57381 | 57773 | iTrunk = get4byte(&pPage1->aData[32]); |
| 57382 | 57774 | } |
| 57383 | 57775 | testcase( iTrunk==mxPage ); |
| 57384 | 57776 | if( iTrunk>mxPage ){ |
| 57385 | 57777 | rc = SQLITE_CORRUPT_BKPT; |
| @@ -57390,12 +57782,13 @@ | ||
| 57390 | 57782 | pTrunk = 0; |
| 57391 | 57783 | goto end_allocate_page; |
| 57392 | 57784 | } |
| 57393 | 57785 | assert( pTrunk!=0 ); |
| 57394 | 57786 | assert( pTrunk->aData!=0 ); |
| 57395 | - | |
| 57396 | - k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */ | |
| 57787 | + /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page | |
| 57788 | + ** is the number of leaf page pointers to follow. */ | |
| 57789 | + k = get4byte(&pTrunk->aData[4]); | |
| 57397 | 57790 | if( k==0 && !searchList ){ |
| 57398 | 57791 | /* The trunk has no leaves and the list is not being searched. |
| 57399 | 57792 | ** So extract the trunk page itself and use it as the newly |
| 57400 | 57793 | ** allocated page */ |
| 57401 | 57794 | assert( pPrevTrunk==0 ); |
| @@ -57709,10 +58102,15 @@ | ||
| 57709 | 58102 | ** to maintain backwards compatibility with older versions of SQLite, |
| 57710 | 58103 | ** we will continue to restrict the number of entries to usableSize/4 - 8 |
| 57711 | 58104 | ** for now. At some point in the future (once everyone has upgraded |
| 57712 | 58105 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 57713 | 58106 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
| 58107 | + ** | |
| 58108 | + ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still | |
| 58109 | + ** avoid using the last six entries in the freelist trunk page array in | |
| 58110 | + ** order that database files created by newer versions of SQLite can be | |
| 58111 | + ** read by older versions of SQLite. | |
| 57714 | 58112 | */ |
| 57715 | 58113 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
| 57716 | 58114 | if( rc==SQLITE_OK ){ |
| 57717 | 58115 | put4byte(&pTrunk->aData[4], nLeaf+1); |
| 57718 | 58116 | put4byte(&pTrunk->aData[8+nLeaf*4], iPage); |
| @@ -58060,13 +58458,21 @@ | ||
| 58060 | 58458 | if( rc ){ |
| 58061 | 58459 | *pRC = rc; |
| 58062 | 58460 | return; |
| 58063 | 58461 | } |
| 58064 | 58462 | pPage->nCell--; |
| 58065 | - memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); | |
| 58066 | - put2byte(&data[hdr+3], pPage->nCell); | |
| 58067 | - pPage->nFree += 2; | |
| 58463 | + if( pPage->nCell==0 ){ | |
| 58464 | + memset(&data[hdr+1], 0, 4); | |
| 58465 | + data[hdr+7] = 0; | |
| 58466 | + put2byte(&data[hdr+5], pPage->pBt->usableSize); | |
| 58467 | + pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset | |
| 58468 | + - pPage->childPtrSize - 8; | |
| 58469 | + }else{ | |
| 58470 | + memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); | |
| 58471 | + put2byte(&data[hdr+3], pPage->nCell); | |
| 58472 | + pPage->nFree += 2; | |
| 58473 | + } | |
| 58068 | 58474 | } |
| 58069 | 58475 | |
| 58070 | 58476 | /* |
| 58071 | 58477 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 58072 | 58478 | ** content of the cell. |
| @@ -58157,49 +58563,275 @@ | ||
| 58157 | 58563 | #endif |
| 58158 | 58564 | } |
| 58159 | 58565 | } |
| 58160 | 58566 | |
| 58161 | 58567 | /* |
| 58162 | -** Add a list of cells to a page. The page should be initially empty. | |
| 58163 | -** The cells are guaranteed to fit on the page. | |
| 58164 | -*/ | |
| 58165 | -static void assemblePage( | |
| 58166 | - MemPage *pPage, /* The page to be assembled */ | |
| 58167 | - int nCell, /* The number of cells to add to this page */ | |
| 58168 | - u8 **apCell, /* Pointers to cell bodies */ | |
| 58169 | - u16 *aSize /* Sizes of the cells */ | |
| 58170 | -){ | |
| 58171 | - int i; /* Loop counter */ | |
| 58172 | - u8 *pCellptr; /* Address of next cell pointer */ | |
| 58173 | - int cellbody; /* Address of next cell body */ | |
| 58174 | - u8 * const data = pPage->aData; /* Pointer to data for pPage */ | |
| 58175 | - const int hdr = pPage->hdrOffset; /* Offset of header on pPage */ | |
| 58176 | - const int nUsable = pPage->pBt->usableSize; /* Usable size of page */ | |
| 58177 | - | |
| 58178 | - assert( pPage->nOverflow==0 ); | |
| 58179 | - assert( sqlite3_mutex_held(pPage->pBt->mutex) ); | |
| 58180 | - assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt) | |
| 58181 | - && (int)MX_CELL(pPage->pBt)<=10921); | |
| 58182 | - assert( sqlite3PagerIswriteable(pPage->pDbPage) ); | |
| 58183 | - | |
| 58184 | - /* Check that the page has just been zeroed by zeroPage() */ | |
| 58185 | - assert( pPage->nCell==0 ); | |
| 58186 | - assert( get2byteNotZero(&data[hdr+5])==nUsable ); | |
| 58187 | - | |
| 58188 | - pCellptr = &pPage->aCellIdx[nCell*2]; | |
| 58189 | - cellbody = nUsable; | |
| 58190 | - for(i=nCell-1; i>=0; i--){ | |
| 58191 | - u16 sz = aSize[i]; | |
| 58192 | - pCellptr -= 2; | |
| 58193 | - cellbody -= sz; | |
| 58194 | - put2byte(pCellptr, cellbody); | |
| 58195 | - memcpy(&data[cellbody], apCell[i], sz); | |
| 58196 | - } | |
| 58197 | - put2byte(&data[hdr+3], nCell); | |
| 58198 | - put2byte(&data[hdr+5], cellbody); | |
| 58199 | - pPage->nFree -= (nCell*2 + nUsable - cellbody); | |
| 58200 | - pPage->nCell = (u16)nCell; | |
| 58568 | +** Array apCell[] contains pointers to nCell b-tree page cells. The | |
| 58569 | +** szCell[] array contains the size in bytes of each cell. This function | |
| 58570 | +** replaces the current contents of page pPg with the contents of the cell | |
| 58571 | +** array. | |
| 58572 | +** | |
| 58573 | +** Some of the cells in apCell[] may currently be stored in pPg. This | |
| 58574 | +** function works around problems caused by this by making a copy of any | |
| 58575 | +** such cells before overwriting the page data. | |
| 58576 | +** | |
| 58577 | +** The MemPage.nFree field is invalidated by this function. It is the | |
| 58578 | +** responsibility of the caller to set it correctly. | |
| 58579 | +*/ | |
| 58580 | +static void rebuildPage( | |
| 58581 | + MemPage *pPg, /* Edit this page */ | |
| 58582 | + int nCell, /* Final number of cells on page */ | |
| 58583 | + u8 **apCell, /* Array of cells */ | |
| 58584 | + u16 *szCell /* Array of cell sizes */ | |
| 58585 | +){ | |
| 58586 | + const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ | |
| 58587 | + u8 * const aData = pPg->aData; /* Pointer to data for pPg */ | |
| 58588 | + const int usableSize = pPg->pBt->usableSize; | |
| 58589 | + u8 * const pEnd = &aData[usableSize]; | |
| 58590 | + int i; | |
| 58591 | + u8 *pCellptr = pPg->aCellIdx; | |
| 58592 | + u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); | |
| 58593 | + u8 *pData; | |
| 58594 | + | |
| 58595 | + i = get2byte(&aData[hdr+5]); | |
| 58596 | + memcpy(&pTmp[i], &aData[i], usableSize - i); | |
| 58597 | + | |
| 58598 | + pData = pEnd; | |
| 58599 | + for(i=0; i<nCell; i++){ | |
| 58600 | + u8 *pCell = apCell[i]; | |
| 58601 | + if( pCell>aData && pCell<pEnd ){ | |
| 58602 | + pCell = &pTmp[pCell - aData]; | |
| 58603 | + } | |
| 58604 | + pData -= szCell[i]; | |
| 58605 | + memcpy(pData, pCell, szCell[i]); | |
| 58606 | + put2byte(pCellptr, (pData - aData)); | |
| 58607 | + pCellptr += 2; | |
| 58608 | + assert( szCell[i]==cellSizePtr(pPg, pCell) ); | |
| 58609 | + } | |
| 58610 | + | |
| 58611 | + /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ | |
| 58612 | + pPg->nCell = nCell; | |
| 58613 | + pPg->nOverflow = 0; | |
| 58614 | + | |
| 58615 | + put2byte(&aData[hdr+1], 0); | |
| 58616 | + put2byte(&aData[hdr+3], pPg->nCell); | |
| 58617 | + put2byte(&aData[hdr+5], pData - aData); | |
| 58618 | + aData[hdr+7] = 0x00; | |
| 58619 | +} | |
| 58620 | + | |
| 58621 | +/* | |
| 58622 | +** Array apCell[] contains nCell pointers to b-tree cells. Array szCell | |
| 58623 | +** contains the size in bytes of each such cell. This function attempts to | |
| 58624 | +** add the cells stored in the array to page pPg. If it cannot (because | |
| 58625 | +** the page needs to be defragmented before the cells will fit), non-zero | |
| 58626 | +** is returned. Otherwise, if the cells are added successfully, zero is | |
| 58627 | +** returned. | |
| 58628 | +** | |
| 58629 | +** Argument pCellptr points to the first entry in the cell-pointer array | |
| 58630 | +** (part of page pPg) to populate. After cell apCell[0] is written to the | |
| 58631 | +** page body, a 16-bit offset is written to pCellptr. And so on, for each | |
| 58632 | +** cell in the array. It is the responsibility of the caller to ensure | |
| 58633 | +** that it is safe to overwrite this part of the cell-pointer array. | |
| 58634 | +** | |
| 58635 | +** When this function is called, *ppData points to the start of the | |
| 58636 | +** content area on page pPg. If the size of the content area is extended, | |
| 58637 | +** *ppData is updated to point to the new start of the content area | |
| 58638 | +** before returning. | |
| 58639 | +** | |
| 58640 | +** Finally, argument pBegin points to the byte immediately following the | |
| 58641 | +** end of the space required by this page for the cell-pointer area (for | |
| 58642 | +** all cells - not just those inserted by the current call). If the content | |
| 58643 | +** area must be extended to before this point in order to accomodate all | |
| 58644 | +** cells in apCell[], then the cells do not fit and non-zero is returned. | |
| 58645 | +*/ | |
| 58646 | +static int pageInsertArray( | |
| 58647 | + MemPage *pPg, /* Page to add cells to */ | |
| 58648 | + u8 *pBegin, /* End of cell-pointer array */ | |
| 58649 | + u8 **ppData, /* IN/OUT: Page content -area pointer */ | |
| 58650 | + u8 *pCellptr, /* Pointer to cell-pointer area */ | |
| 58651 | + int nCell, /* Number of cells to add to pPg */ | |
| 58652 | + u8 **apCell, /* Array of cells */ | |
| 58653 | + u16 *szCell /* Array of cell sizes */ | |
| 58654 | +){ | |
| 58655 | + int i; | |
| 58656 | + u8 *aData = pPg->aData; | |
| 58657 | + u8 *pData = *ppData; | |
| 58658 | + const int bFreelist = aData[1] || aData[2]; | |
| 58659 | + assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ | |
| 58660 | + for(i=0; i<nCell; i++){ | |
| 58661 | + int sz = szCell[i]; | |
| 58662 | + int rc; | |
| 58663 | + u8 *pSlot; | |
| 58664 | + if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){ | |
| 58665 | + pData -= sz; | |
| 58666 | + if( pData<pBegin ) return 1; | |
| 58667 | + pSlot = pData; | |
| 58668 | + } | |
| 58669 | + memcpy(pSlot, apCell[i], sz); | |
| 58670 | + put2byte(pCellptr, (pSlot - aData)); | |
| 58671 | + pCellptr += 2; | |
| 58672 | + } | |
| 58673 | + *ppData = pData; | |
| 58674 | + return 0; | |
| 58675 | +} | |
| 58676 | + | |
| 58677 | +/* | |
| 58678 | +** Array apCell[] contains nCell pointers to b-tree cells. Array szCell | |
| 58679 | +** contains the size in bytes of each such cell. This function adds the | |
| 58680 | +** space associated with each cell in the array that is currently stored | |
| 58681 | +** within the body of pPg to the pPg free-list. The cell-pointers and other | |
| 58682 | +** fields of the page are not updated. | |
| 58683 | +** | |
| 58684 | +** This function returns the total number of cells added to the free-list. | |
| 58685 | +*/ | |
| 58686 | +static int pageFreeArray( | |
| 58687 | + MemPage *pPg, /* Page to edit */ | |
| 58688 | + int nCell, /* Cells to delete */ | |
| 58689 | + u8 **apCell, /* Array of cells */ | |
| 58690 | + u16 *szCell /* Array of cell sizes */ | |
| 58691 | +){ | |
| 58692 | + u8 * const aData = pPg->aData; | |
| 58693 | + u8 * const pEnd = &aData[pPg->pBt->usableSize]; | |
| 58694 | + u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; | |
| 58695 | + int nRet = 0; | |
| 58696 | + int i; | |
| 58697 | + u8 *pFree = 0; | |
| 58698 | + int szFree = 0; | |
| 58699 | + | |
| 58700 | + for(i=0; i<nCell; i++){ | |
| 58701 | + u8 *pCell = apCell[i]; | |
| 58702 | + if( pCell>=pStart && pCell<pEnd ){ | |
| 58703 | + int sz = szCell[i]; | |
| 58704 | + if( pFree!=(pCell + sz) ){ | |
| 58705 | + if( pFree ){ | |
| 58706 | + assert( pFree>aData && (pFree - aData)<65536 ); | |
| 58707 | + freeSpace(pPg, (u16)(pFree - aData), szFree); | |
| 58708 | + } | |
| 58709 | + pFree = pCell; | |
| 58710 | + szFree = sz; | |
| 58711 | + if( pFree+sz>pEnd ) return 0; | |
| 58712 | + }else{ | |
| 58713 | + pFree = pCell; | |
| 58714 | + szFree += sz; | |
| 58715 | + } | |
| 58716 | + nRet++; | |
| 58717 | + } | |
| 58718 | + } | |
| 58719 | + if( pFree ){ | |
| 58720 | + assert( pFree>aData && (pFree - aData)<65536 ); | |
| 58721 | + freeSpace(pPg, (u16)(pFree - aData), szFree); | |
| 58722 | + } | |
| 58723 | + return nRet; | |
| 58724 | +} | |
| 58725 | + | |
| 58726 | +/* | |
| 58727 | +** apCell[] and szCell[] contains pointers to and sizes of all cells in the | |
| 58728 | +** pages being balanced. The current page, pPg, has pPg->nCell cells starting | |
| 58729 | +** with apCell[iOld]. After balancing, this page should hold nNew cells | |
| 58730 | +** starting at apCell[iNew]. | |
| 58731 | +** | |
| 58732 | +** This routine makes the necessary adjustments to pPg so that it contains | |
| 58733 | +** the correct cells after being balanced. | |
| 58734 | +** | |
| 58735 | +** The pPg->nFree field is invalid when this function returns. It is the | |
| 58736 | +** responsibility of the caller to set it correctly. | |
| 58737 | +*/ | |
| 58738 | +static void editPage( | |
| 58739 | + MemPage *pPg, /* Edit this page */ | |
| 58740 | + int iOld, /* Index of first cell currently on page */ | |
| 58741 | + int iNew, /* Index of new first cell on page */ | |
| 58742 | + int nNew, /* Final number of cells on page */ | |
| 58743 | + u8 **apCell, /* Array of cells */ | |
| 58744 | + u16 *szCell /* Array of cell sizes */ | |
| 58745 | +){ | |
| 58746 | + u8 * const aData = pPg->aData; | |
| 58747 | + const int hdr = pPg->hdrOffset; | |
| 58748 | + u8 *pBegin = &pPg->aCellIdx[nNew * 2]; | |
| 58749 | + int nCell = pPg->nCell; /* Cells stored on pPg */ | |
| 58750 | + u8 *pData; | |
| 58751 | + u8 *pCellptr; | |
| 58752 | + int i; | |
| 58753 | + int iOldEnd = iOld + pPg->nCell + pPg->nOverflow; | |
| 58754 | + int iNewEnd = iNew + nNew; | |
| 58755 | + | |
| 58756 | +#ifdef SQLITE_DEBUG | |
| 58757 | + u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); | |
| 58758 | + memcpy(pTmp, aData, pPg->pBt->usableSize); | |
| 58759 | +#endif | |
| 58760 | + | |
| 58761 | + /* Remove cells from the start and end of the page */ | |
| 58762 | + if( iOld<iNew ){ | |
| 58763 | + int nShift = pageFreeArray( | |
| 58764 | + pPg, iNew-iOld, &apCell[iOld], &szCell[iOld] | |
| 58765 | + ); | |
| 58766 | + memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); | |
| 58767 | + nCell -= nShift; | |
| 58768 | + } | |
| 58769 | + if( iNewEnd < iOldEnd ){ | |
| 58770 | + nCell -= pageFreeArray( | |
| 58771 | + pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd] | |
| 58772 | + ); | |
| 58773 | + } | |
| 58774 | + | |
| 58775 | + pData = &aData[get2byteNotZero(&aData[hdr+5])]; | |
| 58776 | + if( pData<pBegin ) goto editpage_fail; | |
| 58777 | + | |
| 58778 | + /* Add cells to the start of the page */ | |
| 58779 | + if( iNew<iOld ){ | |
| 58780 | + int nAdd = MIN(nNew,iOld-iNew); | |
| 58781 | + assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB ); | |
| 58782 | + pCellptr = pPg->aCellIdx; | |
| 58783 | + memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); | |
| 58784 | + if( pageInsertArray( | |
| 58785 | + pPg, pBegin, &pData, pCellptr, | |
| 58786 | + nAdd, &apCell[iNew], &szCell[iNew] | |
| 58787 | + ) ) goto editpage_fail; | |
| 58788 | + nCell += nAdd; | |
| 58789 | + } | |
| 58790 | + | |
| 58791 | + /* Add any overflow cells */ | |
| 58792 | + for(i=0; i<pPg->nOverflow; i++){ | |
| 58793 | + int iCell = (iOld + pPg->aiOvfl[i]) - iNew; | |
| 58794 | + if( iCell>=0 && iCell<nNew ){ | |
| 58795 | + pCellptr = &pPg->aCellIdx[iCell * 2]; | |
| 58796 | + memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); | |
| 58797 | + nCell++; | |
| 58798 | + if( pageInsertArray( | |
| 58799 | + pPg, pBegin, &pData, pCellptr, | |
| 58800 | + 1, &apCell[iCell + iNew], &szCell[iCell + iNew] | |
| 58801 | + ) ) goto editpage_fail; | |
| 58802 | + } | |
| 58803 | + } | |
| 58804 | + | |
| 58805 | + /* Append cells to the end of the page */ | |
| 58806 | + pCellptr = &pPg->aCellIdx[nCell*2]; | |
| 58807 | + if( pageInsertArray( | |
| 58808 | + pPg, pBegin, &pData, pCellptr, | |
| 58809 | + nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell] | |
| 58810 | + ) ) goto editpage_fail; | |
| 58811 | + | |
| 58812 | + pPg->nCell = nNew; | |
| 58813 | + pPg->nOverflow = 0; | |
| 58814 | + | |
| 58815 | + put2byte(&aData[hdr+3], pPg->nCell); | |
| 58816 | + put2byte(&aData[hdr+5], pData - aData); | |
| 58817 | + | |
| 58818 | +#ifdef SQLITE_DEBUG | |
| 58819 | + for(i=0; i<nNew && !CORRUPT_DB; i++){ | |
| 58820 | + u8 *pCell = apCell[i+iNew]; | |
| 58821 | + int iOff = get2byte(&pPg->aCellIdx[i*2]); | |
| 58822 | + if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){ | |
| 58823 | + pCell = &pTmp[pCell - aData]; | |
| 58824 | + } | |
| 58825 | + assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) ); | |
| 58826 | + } | |
| 58827 | +#endif | |
| 58828 | + | |
| 58829 | + return; | |
| 58830 | + editpage_fail: | |
| 58831 | + /* Unable to edit this page. Rebuild it from scratch instead. */ | |
| 58832 | + rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]); | |
| 58201 | 58833 | } |
| 58202 | 58834 | |
| 58203 | 58835 | /* |
| 58204 | 58836 | ** The following parameters determine how many adjacent pages get involved |
| 58205 | 58837 | ** in a balancing operation. NN is the number of neighbors on either side |
| @@ -58267,11 +58899,12 @@ | ||
| 58267 | 58899 | u8 *pStop; |
| 58268 | 58900 | |
| 58269 | 58901 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
| 58270 | 58902 | assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); |
| 58271 | 58903 | zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); |
| 58272 | - assemblePage(pNew, 1, &pCell, &szCell); | |
| 58904 | + rebuildPage(pNew, 1, &pCell, &szCell); | |
| 58905 | + pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; | |
| 58273 | 58906 | |
| 58274 | 58907 | /* If this is an auto-vacuum database, update the pointer map |
| 58275 | 58908 | ** with entries for the new page, and any pointer from the |
| 58276 | 58909 | ** cell on the page to an overflow page. If either of these |
| 58277 | 58910 | ** operations fails, the return code is set, but the contents |
| @@ -58486,21 +59119,26 @@ | ||
| 58486 | 59119 | int subtotal; /* Subtotal of bytes in cells on one page */ |
| 58487 | 59120 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
| 58488 | 59121 | int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ |
| 58489 | 59122 | int szScratch; /* Size of scratch memory requested */ |
| 58490 | 59123 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 58491 | - MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ | |
| 58492 | 59124 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 58493 | 59125 | u8 *pRight; /* Location in parent of right-sibling pointer */ |
| 58494 | 59126 | u8 *apDiv[NB-1]; /* Divider cells in pParent */ |
| 58495 | 59127 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 58496 | - int szNew[NB+2]; /* Combined size of cells place on i-th page */ | |
| 59128 | + int cntOld[NB+2]; /* Old index in aCell[] after i-th page */ | |
| 59129 | + int szNew[NB+2]; /* Combined size of cells placed on i-th page */ | |
| 58497 | 59130 | u8 **apCell = 0; /* All cells begin balanced */ |
| 58498 | 59131 | u16 *szCell; /* Local size of all cells in apCell[] */ |
| 58499 | 59132 | u8 *aSpace1; /* Space for copies of dividers cells */ |
| 58500 | 59133 | Pgno pgno; /* Temp var to store a page number in */ |
| 59134 | + u8 abDone[NB+2]; /* True after i'th new page is populated */ | |
| 59135 | + Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ | |
| 59136 | + Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ | |
| 59137 | + u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ | |
| 58501 | 59138 | |
| 59139 | + memset(abDone, 0, sizeof(abDone)); | |
| 58502 | 59140 | pBt = pParent->pBt; |
| 58503 | 59141 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 58504 | 59142 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 58505 | 59143 | |
| 58506 | 59144 | #if 0 |
| @@ -58605,16 +59243,18 @@ | ||
| 58605 | 59243 | nMaxCells = (nMaxCells + 3)&~3; |
| 58606 | 59244 | |
| 58607 | 59245 | /* |
| 58608 | 59246 | ** Allocate space for memory structures |
| 58609 | 59247 | */ |
| 58610 | - k = pBt->pageSize + ROUND8(sizeof(MemPage)); | |
| 58611 | 59248 | szScratch = |
| 58612 | 59249 | nMaxCells*sizeof(u8*) /* apCell */ |
| 58613 | 59250 | + nMaxCells*sizeof(u16) /* szCell */ |
| 58614 | - + pBt->pageSize /* aSpace1 */ | |
| 58615 | - + k*nOld; /* Page copies (apCopy) */ | |
| 59251 | + + pBt->pageSize; /* aSpace1 */ | |
| 59252 | + | |
| 59253 | + /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer | |
| 59254 | + ** that is more than 6 times the database page size. */ | |
| 59255 | + assert( szScratch<=6*pBt->pageSize ); | |
| 58616 | 59256 | apCell = sqlite3ScratchMalloc( szScratch ); |
| 58617 | 59257 | if( apCell==0 ){ |
| 58618 | 59258 | rc = SQLITE_NOMEM; |
| 58619 | 59259 | goto balance_cleanup; |
| 58620 | 59260 | } |
| @@ -58623,12 +59263,12 @@ | ||
| 58623 | 59263 | assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); |
| 58624 | 59264 | |
| 58625 | 59265 | /* |
| 58626 | 59266 | ** Load pointers to all cells on sibling pages and the divider cells |
| 58627 | 59267 | ** into the local apCell[] array. Make copies of the divider cells |
| 58628 | - ** into space obtained from aSpace1[] and remove the divider cells | |
| 58629 | - ** from pParent. | |
| 59268 | + ** into space obtained from aSpace1[]. The divider cells have already | |
| 59269 | + ** been removed from pParent. | |
| 58630 | 59270 | ** |
| 58631 | 59271 | ** If the siblings are on leaf pages, then the child pointers of the |
| 58632 | 59272 | ** divider cells are stripped from the cells before they are copied |
| 58633 | 59273 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
| 58634 | 59274 | ** child pointers. If siblings are not leaves, then all cell in |
| @@ -58640,19 +59280,11 @@ | ||
| 58640 | 59280 | */ |
| 58641 | 59281 | leafCorrection = apOld[0]->leaf*4; |
| 58642 | 59282 | leafData = apOld[0]->intKeyLeaf; |
| 58643 | 59283 | for(i=0; i<nOld; i++){ |
| 58644 | 59284 | int limit; |
| 58645 | - | |
| 58646 | - /* Before doing anything else, take a copy of the i'th original sibling | |
| 58647 | - ** The rest of this function will use data from the copies rather | |
| 58648 | - ** that the original pages since the original pages will be in the | |
| 58649 | - ** process of being overwritten. */ | |
| 58650 | - MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i]; | |
| 58651 | - memcpy(pOld, apOld[i], sizeof(MemPage)); | |
| 58652 | - pOld->aData = (void*)&pOld[1]; | |
| 58653 | - memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize); | |
| 59285 | + MemPage *pOld = apOld[i]; | |
| 58654 | 59286 | |
| 58655 | 59287 | limit = pOld->nCell+pOld->nOverflow; |
| 58656 | 59288 | if( pOld->nOverflow>0 ){ |
| 58657 | 59289 | for(j=0; j<limit; j++){ |
| 58658 | 59290 | assert( nCell<nMaxCells ); |
| @@ -58669,10 +59301,11 @@ | ||
| 58669 | 59301 | apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j); |
| 58670 | 59302 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
| 58671 | 59303 | nCell++; |
| 58672 | 59304 | } |
| 58673 | 59305 | } |
| 59306 | + cntOld[i] = nCell; | |
| 58674 | 59307 | if( i<nOld-1 && !leafData){ |
| 58675 | 59308 | u16 sz = (u16)szNew[i]; |
| 58676 | 59309 | u8 *pTemp; |
| 58677 | 59310 | assert( nCell<nMaxCells ); |
| 58678 | 59311 | szCell[nCell] = sz; |
| @@ -58720,11 +59353,11 @@ | ||
| 58720 | 59353 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
| 58721 | 59354 | for(subtotal=k=i=0; i<nCell; i++){ |
| 58722 | 59355 | assert( i<nMaxCells ); |
| 58723 | 59356 | subtotal += szCell[i] + 2; |
| 58724 | 59357 | if( subtotal > usableSpace ){ |
| 58725 | - szNew[k] = subtotal - szCell[i]; | |
| 59358 | + szNew[k] = subtotal - szCell[i] - 2; | |
| 58726 | 59359 | cntNew[k] = i; |
| 58727 | 59360 | if( leafData ){ i--; } |
| 58728 | 59361 | subtotal = 0; |
| 58729 | 59362 | k++; |
| 58730 | 59363 | if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
| @@ -58734,13 +59367,14 @@ | ||
| 58734 | 59367 | cntNew[k] = nCell; |
| 58735 | 59368 | k++; |
| 58736 | 59369 | |
| 58737 | 59370 | /* |
| 58738 | 59371 | ** The packing computed by the previous block is biased toward the siblings |
| 58739 | - ** on the left side. The left siblings are always nearly full, while the | |
| 58740 | - ** right-most sibling might be nearly empty. This block of code attempts | |
| 58741 | - ** to adjust the packing of siblings to get a better balance. | |
| 59372 | + ** on the left side (siblings with smaller keys). The left siblings are | |
| 59373 | + ** always nearly full, while the right-most sibling might be nearly empty. | |
| 59374 | + ** The next block of code attempts to adjust the packing of siblings to | |
| 59375 | + ** get a better balance. | |
| 58742 | 59376 | ** |
| 58743 | 59377 | ** This adjustment is more than an optimization. The packing above might |
| 58744 | 59378 | ** be so out of balance as to be illegal. For example, the right-most |
| 58745 | 59379 | ** sibling might be completely empty. This adjustment is not optional. |
| 58746 | 59380 | */ |
| @@ -58765,26 +59399,22 @@ | ||
| 58765 | 59399 | } |
| 58766 | 59400 | szNew[i] = szRight; |
| 58767 | 59401 | szNew[i-1] = szLeft; |
| 58768 | 59402 | } |
| 58769 | 59403 | |
| 58770 | - /* Either we found one or more cells (cntnew[0])>0) or pPage is | |
| 58771 | - ** a virtual root page. A virtual root page is when the real root | |
| 58772 | - ** page is page 1 and we are the only child of that page. | |
| 58773 | - ** | |
| 58774 | - ** UPDATE: The assert() below is not necessarily true if the database | |
| 58775 | - ** file is corrupt. The corruption will be detected and reported later | |
| 58776 | - ** in this procedure so there is no need to act upon it now. | |
| 59404 | + /* Sanity check: For a non-corrupt database file one of the follwing | |
| 59405 | + ** must be true: | |
| 59406 | + ** (1) We found one or more cells (cntNew[0])>0), or | |
| 59407 | + ** (2) pPage is a virtual root page. A virtual root page is when | |
| 59408 | + ** the real root page is page 1 and we are the only child of | |
| 59409 | + ** that page. | |
| 58777 | 59410 | */ |
| 58778 | -#if 0 | |
| 58779 | - assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); | |
| 58780 | -#endif | |
| 58781 | - | |
| 58782 | - TRACE(("BALANCE: old: %d %d %d ", | |
| 58783 | - apOld[0]->pgno, | |
| 58784 | - nOld>=2 ? apOld[1]->pgno : 0, | |
| 58785 | - nOld>=3 ? apOld[2]->pgno : 0 | |
| 59411 | + assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB); | |
| 59412 | + TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n", | |
| 59413 | + apOld[0]->pgno, apOld[0]->nCell, | |
| 59414 | + nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0, | |
| 59415 | + nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0 | |
| 58786 | 59416 | )); |
| 58787 | 59417 | |
| 58788 | 59418 | /* |
| 58789 | 59419 | ** Allocate k new pages. Reuse old pages where possible. |
| 58790 | 59420 | */ |
| @@ -58803,12 +59433,14 @@ | ||
| 58803 | 59433 | if( rc ) goto balance_cleanup; |
| 58804 | 59434 | }else{ |
| 58805 | 59435 | assert( i>0 ); |
| 58806 | 59436 | rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0); |
| 58807 | 59437 | if( rc ) goto balance_cleanup; |
| 59438 | + zeroPage(pNew, pageFlags); | |
| 58808 | 59439 | apNew[i] = pNew; |
| 58809 | 59440 | nNew++; |
| 59441 | + cntOld[i] = nCell; | |
| 58810 | 59442 | |
| 58811 | 59443 | /* Set the pointer-map entry for the new sibling page. */ |
| 58812 | 59444 | if( ISAUTOVACUUM ){ |
| 58813 | 59445 | ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); |
| 58814 | 59446 | if( rc!=SQLITE_OK ){ |
| @@ -58816,139 +59448,247 @@ | ||
| 58816 | 59448 | } |
| 58817 | 59449 | } |
| 58818 | 59450 | } |
| 58819 | 59451 | } |
| 58820 | 59452 | |
| 58821 | - /* Free any old pages that were not reused as new pages. | |
| 58822 | - */ | |
| 58823 | - while( i<nOld ){ | |
| 58824 | - freePage(apOld[i], &rc); | |
| 58825 | - if( rc ) goto balance_cleanup; | |
| 58826 | - releasePage(apOld[i]); | |
| 58827 | - apOld[i] = 0; | |
| 58828 | - i++; | |
| 58829 | - } | |
| 58830 | - | |
| 58831 | 59453 | /* |
| 58832 | - ** Put the new pages in ascending order. This helps to | |
| 58833 | - ** keep entries in the disk file in order so that a scan | |
| 58834 | - ** of the table is a linear scan through the file. That | |
| 58835 | - ** in turn helps the operating system to deliver pages | |
| 58836 | - ** from the disk more rapidly. | |
| 58837 | - ** | |
| 58838 | - ** An O(n^2) insertion sort algorithm is used, but since | |
| 58839 | - ** n is never more than NB (a small constant), that should | |
| 58840 | - ** not be a problem. | |
| 58841 | - ** | |
| 58842 | - ** When NB==3, this one optimization makes the database | |
| 58843 | - ** about 25% faster for large insertions and deletions. | |
| 58844 | - */ | |
| 58845 | - for(i=0; i<k-1; i++){ | |
| 58846 | - int minV = apNew[i]->pgno; | |
| 58847 | - int minI = i; | |
| 58848 | - for(j=i+1; j<k; j++){ | |
| 58849 | - if( apNew[j]->pgno<(unsigned)minV ){ | |
| 58850 | - minI = j; | |
| 58851 | - minV = apNew[j]->pgno; | |
| 58852 | - } | |
| 58853 | - } | |
| 58854 | - if( minI>i ){ | |
| 58855 | - MemPage *pT; | |
| 58856 | - pT = apNew[i]; | |
| 58857 | - apNew[i] = apNew[minI]; | |
| 58858 | - apNew[minI] = pT; | |
| 58859 | - } | |
| 58860 | - } | |
| 58861 | - TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n", | |
| 58862 | - apNew[0]->pgno, szNew[0], | |
| 59454 | + ** Reassign page numbers so that the new pages are in ascending order. | |
| 59455 | + ** This helps to keep entries in the disk file in order so that a scan | |
| 59456 | + ** of the table is closer to a linear scan through the file. That in turn | |
| 59457 | + ** helps the operating system to deliver pages from the disk more rapidly. | |
| 59458 | + ** | |
| 59459 | + ** An O(n^2) insertion sort algorithm is used, but since n is never more | |
| 59460 | + ** than (NB+2) (a small constant), that should not be a problem. | |
| 59461 | + ** | |
| 59462 | + ** When NB==3, this one optimization makes the database about 25% faster | |
| 59463 | + ** for large insertions and deletions. | |
| 59464 | + */ | |
| 59465 | + for(i=0; i<nNew; i++){ | |
| 59466 | + aPgOrder[i] = aPgno[i] = apNew[i]->pgno; | |
| 59467 | + aPgFlags[i] = apNew[i]->pDbPage->flags; | |
| 59468 | + for(j=0; j<i; j++){ | |
| 59469 | + if( aPgno[j]==aPgno[i] ){ | |
| 59470 | + /* This branch is taken if the set of sibling pages somehow contains | |
| 59471 | + ** duplicate entries. This can happen if the database is corrupt. | |
| 59472 | + ** It would be simpler to detect this as part of the loop below, but | |
| 59473 | + ** we do the detection here in order to avoid populating the pager | |
| 59474 | + ** cache with two separate objects associated with the same | |
| 59475 | + ** page number. */ | |
| 59476 | + assert( CORRUPT_DB ); | |
| 59477 | + rc = SQLITE_CORRUPT_BKPT; | |
| 59478 | + goto balance_cleanup; | |
| 59479 | + } | |
| 59480 | + } | |
| 59481 | + } | |
| 59482 | + for(i=0; i<nNew; i++){ | |
| 59483 | + int iBest = 0; /* aPgno[] index of page number to use */ | |
| 59484 | + for(j=1; j<nNew; j++){ | |
| 59485 | + if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j; | |
| 59486 | + } | |
| 59487 | + pgno = aPgOrder[iBest]; | |
| 59488 | + aPgOrder[iBest] = 0xffffffff; | |
| 59489 | + if( iBest!=i ){ | |
| 59490 | + if( iBest>i ){ | |
| 59491 | + sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0); | |
| 59492 | + } | |
| 59493 | + sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]); | |
| 59494 | + apNew[i]->pgno = pgno; | |
| 59495 | + } | |
| 59496 | + } | |
| 59497 | + | |
| 59498 | + TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) " | |
| 59499 | + "%d(%d nc=%d) %d(%d nc=%d)\n", | |
| 59500 | + apNew[0]->pgno, szNew[0], cntNew[0], | |
| 58863 | 59501 | nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, |
| 59502 | + nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0, | |
| 58864 | 59503 | nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, |
| 59504 | + nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0, | |
| 58865 | 59505 | nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, |
| 58866 | - nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0)); | |
| 59506 | + nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0, | |
| 59507 | + nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0, | |
| 59508 | + nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0 | |
| 59509 | + )); | |
| 58867 | 59510 | |
| 58868 | 59511 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 58869 | 59512 | put4byte(pRight, apNew[nNew-1]->pgno); |
| 58870 | 59513 | |
| 58871 | - /* | |
| 58872 | - ** Evenly distribute the data in apCell[] across the new pages. | |
| 58873 | - ** Insert divider cells into pParent as necessary. | |
| 59514 | + /* If the sibling pages are not leaves, ensure that the right-child pointer | |
| 59515 | + ** of the right-most new sibling page is set to the value that was | |
| 59516 | + ** originally in the same field of the right-most old sibling page. */ | |
| 59517 | + if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){ | |
| 59518 | + MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1]; | |
| 59519 | + memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4); | |
| 59520 | + } | |
| 59521 | + | |
| 59522 | + /* Make any required updates to pointer map entries associated with | |
| 59523 | + ** cells stored on sibling pages following the balance operation. Pointer | |
| 59524 | + ** map entries associated with divider cells are set by the insertCell() | |
| 59525 | + ** routine. The associated pointer map entries are: | |
| 59526 | + ** | |
| 59527 | + ** a) if the cell contains a reference to an overflow chain, the | |
| 59528 | + ** entry associated with the first page in the overflow chain, and | |
| 59529 | + ** | |
| 59530 | + ** b) if the sibling pages are not leaves, the child page associated | |
| 59531 | + ** with the cell. | |
| 59532 | + ** | |
| 59533 | + ** If the sibling pages are not leaves, then the pointer map entry | |
| 59534 | + ** associated with the right-child of each sibling may also need to be | |
| 59535 | + ** updated. This happens below, after the sibling pages have been | |
| 59536 | + ** populated, not here. | |
| 58874 | 59537 | */ |
| 58875 | - j = 0; | |
| 58876 | - for(i=0; i<nNew; i++){ | |
| 58877 | - /* Assemble the new sibling page. */ | |
| 59538 | + if( ISAUTOVACUUM ){ | |
| 59539 | + MemPage *pNew = apNew[0]; | |
| 59540 | + u8 *aOld = pNew->aData; | |
| 59541 | + int cntOldNext = pNew->nCell + pNew->nOverflow; | |
| 59542 | + int usableSize = pBt->usableSize; | |
| 59543 | + int iNew = 0; | |
| 59544 | + int iOld = 0; | |
| 59545 | + | |
| 59546 | + for(i=0; i<nCell; i++){ | |
| 59547 | + u8 *pCell = apCell[i]; | |
| 59548 | + if( i==cntOldNext ){ | |
| 59549 | + MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld]; | |
| 59550 | + cntOldNext += pOld->nCell + pOld->nOverflow + !leafData; | |
| 59551 | + aOld = pOld->aData; | |
| 59552 | + } | |
| 59553 | + if( i==cntNew[iNew] ){ | |
| 59554 | + pNew = apNew[++iNew]; | |
| 59555 | + if( !leafData ) continue; | |
| 59556 | + } | |
| 59557 | + | |
| 59558 | + /* Cell pCell is destined for new sibling page pNew. Originally, it | |
| 59559 | + ** was either part of sibling page iOld (possibly an overflow cell), | |
| 59560 | + ** or else the divider cell to the left of sibling page iOld. So, | |
| 59561 | + ** if sibling page iOld had the same page number as pNew, and if | |
| 59562 | + ** pCell really was a part of sibling page iOld (not a divider or | |
| 59563 | + ** overflow cell), we can skip updating the pointer map entries. */ | |
| 59564 | + if( pNew->pgno!=aPgno[iOld] || pCell<aOld || pCell>=&aOld[usableSize] ){ | |
| 59565 | + if( !leafCorrection ){ | |
| 59566 | + ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); | |
| 59567 | + } | |
| 59568 | + if( szCell[i]>pNew->minLocal ){ | |
| 59569 | + ptrmapPutOvflPtr(pNew, pCell, &rc); | |
| 59570 | + } | |
| 59571 | + } | |
| 59572 | + } | |
| 59573 | + } | |
| 59574 | + | |
| 59575 | + /* Insert new divider cells into pParent. */ | |
| 59576 | + for(i=0; i<nNew-1; i++){ | |
| 59577 | + u8 *pCell; | |
| 59578 | + u8 *pTemp; | |
| 59579 | + int sz; | |
| 58878 | 59580 | MemPage *pNew = apNew[i]; |
| 58879 | - assert( j<nMaxCells ); | |
| 58880 | - zeroPage(pNew, pageFlags); | |
| 58881 | - assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); | |
| 58882 | - assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); | |
| 58883 | - assert( pNew->nOverflow==0 ); | |
| 58884 | - | |
| 58885 | 59581 | j = cntNew[i]; |
| 58886 | 59582 | |
| 58887 | - /* If the sibling page assembled above was not the right-most sibling, | |
| 58888 | - ** insert a divider cell into the parent page. | |
| 58889 | - */ | |
| 58890 | - assert( i<nNew-1 || j==nCell ); | |
| 58891 | - if( j<nCell ){ | |
| 58892 | - u8 *pCell; | |
| 58893 | - u8 *pTemp; | |
| 58894 | - int sz; | |
| 58895 | - | |
| 58896 | - assert( j<nMaxCells ); | |
| 58897 | - pCell = apCell[j]; | |
| 58898 | - sz = szCell[j] + leafCorrection; | |
| 58899 | - pTemp = &aOvflSpace[iOvflSpace]; | |
| 58900 | - if( !pNew->leaf ){ | |
| 58901 | - memcpy(&pNew->aData[8], pCell, 4); | |
| 58902 | - }else if( leafData ){ | |
| 58903 | - /* If the tree is a leaf-data tree, and the siblings are leaves, | |
| 58904 | - ** then there is no divider cell in apCell[]. Instead, the divider | |
| 58905 | - ** cell consists of the integer key for the right-most cell of | |
| 58906 | - ** the sibling-page assembled above only. | |
| 58907 | - */ | |
| 58908 | - CellInfo info; | |
| 58909 | - j--; | |
| 58910 | - btreeParseCellPtr(pNew, apCell[j], &info); | |
| 58911 | - pCell = pTemp; | |
| 58912 | - sz = 4 + putVarint(&pCell[4], info.nKey); | |
| 58913 | - pTemp = 0; | |
| 58914 | - }else{ | |
| 58915 | - pCell -= 4; | |
| 58916 | - /* Obscure case for non-leaf-data trees: If the cell at pCell was | |
| 58917 | - ** previously stored on a leaf node, and its reported size was 4 | |
| 58918 | - ** bytes, then it may actually be smaller than this | |
| 58919 | - ** (see btreeParseCellPtr(), 4 bytes is the minimum size of | |
| 58920 | - ** any cell). But it is important to pass the correct size to | |
| 58921 | - ** insertCell(), so reparse the cell now. | |
| 58922 | - ** | |
| 58923 | - ** Note that this can never happen in an SQLite data file, as all | |
| 58924 | - ** cells are at least 4 bytes. It only happens in b-trees used | |
| 58925 | - ** to evaluate "IN (SELECT ...)" and similar clauses. | |
| 58926 | - */ | |
| 58927 | - if( szCell[j]==4 ){ | |
| 58928 | - assert(leafCorrection==4); | |
| 58929 | - sz = cellSizePtr(pParent, pCell); | |
| 58930 | - } | |
| 58931 | - } | |
| 58932 | - iOvflSpace += sz; | |
| 58933 | - assert( sz<=pBt->maxLocal+23 ); | |
| 58934 | - assert( iOvflSpace <= (int)pBt->pageSize ); | |
| 58935 | - insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc); | |
| 58936 | - if( rc!=SQLITE_OK ) goto balance_cleanup; | |
| 58937 | - assert( sqlite3PagerIswriteable(pParent->pDbPage) ); | |
| 58938 | - | |
| 58939 | - j++; | |
| 58940 | - nxDiv++; | |
| 58941 | - } | |
| 58942 | - } | |
| 58943 | - assert( j==nCell ); | |
| 59583 | + assert( j<nMaxCells ); | |
| 59584 | + pCell = apCell[j]; | |
| 59585 | + sz = szCell[j] + leafCorrection; | |
| 59586 | + pTemp = &aOvflSpace[iOvflSpace]; | |
| 59587 | + if( !pNew->leaf ){ | |
| 59588 | + memcpy(&pNew->aData[8], pCell, 4); | |
| 59589 | + }else if( leafData ){ | |
| 59590 | + /* If the tree is a leaf-data tree, and the siblings are leaves, | |
| 59591 | + ** then there is no divider cell in apCell[]. Instead, the divider | |
| 59592 | + ** cell consists of the integer key for the right-most cell of | |
| 59593 | + ** the sibling-page assembled above only. | |
| 59594 | + */ | |
| 59595 | + CellInfo info; | |
| 59596 | + j--; | |
| 59597 | + btreeParseCellPtr(pNew, apCell[j], &info); | |
| 59598 | + pCell = pTemp; | |
| 59599 | + sz = 4 + putVarint(&pCell[4], info.nKey); | |
| 59600 | + pTemp = 0; | |
| 59601 | + }else{ | |
| 59602 | + pCell -= 4; | |
| 59603 | + /* Obscure case for non-leaf-data trees: If the cell at pCell was | |
| 59604 | + ** previously stored on a leaf node, and its reported size was 4 | |
| 59605 | + ** bytes, then it may actually be smaller than this | |
| 59606 | + ** (see btreeParseCellPtr(), 4 bytes is the minimum size of | |
| 59607 | + ** any cell). But it is important to pass the correct size to | |
| 59608 | + ** insertCell(), so reparse the cell now. | |
| 59609 | + ** | |
| 59610 | + ** Note that this can never happen in an SQLite data file, as all | |
| 59611 | + ** cells are at least 4 bytes. It only happens in b-trees used | |
| 59612 | + ** to evaluate "IN (SELECT ...)" and similar clauses. | |
| 59613 | + */ | |
| 59614 | + if( szCell[j]==4 ){ | |
| 59615 | + assert(leafCorrection==4); | |
| 59616 | + sz = cellSizePtr(pParent, pCell); | |
| 59617 | + } | |
| 59618 | + } | |
| 59619 | + iOvflSpace += sz; | |
| 59620 | + assert( sz<=pBt->maxLocal+23 ); | |
| 59621 | + assert( iOvflSpace <= (int)pBt->pageSize ); | |
| 59622 | + insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc); | |
| 59623 | + if( rc!=SQLITE_OK ) goto balance_cleanup; | |
| 59624 | + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); | |
| 59625 | + } | |
| 59626 | + | |
| 59627 | + /* Now update the actual sibling pages. The order in which they are updated | |
| 59628 | + ** is important, as this code needs to avoid disrupting any page from which | |
| 59629 | + ** cells may still to be read. In practice, this means: | |
| 59630 | + ** | |
| 59631 | + ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1]) | |
| 59632 | + ** then it is not safe to update page apNew[iPg] until after | |
| 59633 | + ** the left-hand sibling apNew[iPg-1] has been updated. | |
| 59634 | + ** | |
| 59635 | + ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1]) | |
| 59636 | + ** then it is not safe to update page apNew[iPg] until after | |
| 59637 | + ** the right-hand sibling apNew[iPg+1] has been updated. | |
| 59638 | + ** | |
| 59639 | + ** If neither of the above apply, the page is safe to update. | |
| 59640 | + ** | |
| 59641 | + ** The iPg value in the following loop starts at nNew-1 goes down | |
| 59642 | + ** to 0, then back up to nNew-1 again, thus making two passes over | |
| 59643 | + ** the pages. On the initial downward pass, only condition (1) above | |
| 59644 | + ** needs to be tested because (2) will always be true from the previous | |
| 59645 | + ** step. On the upward pass, both conditions are always true, so the | |
| 59646 | + ** upwards pass simply processes pages that were missed on the downward | |
| 59647 | + ** pass. | |
| 59648 | + */ | |
| 59649 | + for(i=1-nNew; i<nNew; i++){ | |
| 59650 | + int iPg = i<0 ? -i : i; | |
| 59651 | + assert( iPg>=0 && iPg<nNew ); | |
| 59652 | + if( abDone[iPg] ) continue; /* Skip pages already processed */ | |
| 59653 | + if( i>=0 /* On the upwards pass, or... */ | |
| 59654 | + || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */ | |
| 59655 | + ){ | |
| 59656 | + int iNew; | |
| 59657 | + int iOld; | |
| 59658 | + int nNewCell; | |
| 59659 | + | |
| 59660 | + /* Verify condition (1): If cells are moving left, update iPg | |
| 59661 | + ** only after iPg-1 has already been updated. */ | |
| 59662 | + assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] ); | |
| 59663 | + | |
| 59664 | + /* Verify condition (2): If cells are moving right, update iPg | |
| 59665 | + ** only after iPg+1 has already been updated. */ | |
| 59666 | + assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] ); | |
| 59667 | + | |
| 59668 | + if( iPg==0 ){ | |
| 59669 | + iNew = iOld = 0; | |
| 59670 | + nNewCell = cntNew[0]; | |
| 59671 | + }else{ | |
| 59672 | + iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : nCell; | |
| 59673 | + iNew = cntNew[iPg-1] + !leafData; | |
| 59674 | + nNewCell = cntNew[iPg] - iNew; | |
| 59675 | + } | |
| 59676 | + | |
| 59677 | + editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell); | |
| 59678 | + abDone[iPg]++; | |
| 59679 | + apNew[iPg]->nFree = usableSpace-szNew[iPg]; | |
| 59680 | + assert( apNew[iPg]->nOverflow==0 ); | |
| 59681 | + assert( apNew[iPg]->nCell==nNewCell ); | |
| 59682 | + } | |
| 59683 | + } | |
| 59684 | + | |
| 59685 | + /* All pages have been processed exactly once */ | |
| 59686 | + assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 ); | |
| 59687 | + | |
| 58944 | 59688 | assert( nOld>0 ); |
| 58945 | 59689 | assert( nNew>0 ); |
| 58946 | - if( (pageFlags & PTF_LEAF)==0 ){ | |
| 58947 | - u8 *zChild = &apCopy[nOld-1]->aData[8]; | |
| 58948 | - memcpy(&apNew[nNew-1]->aData[8], zChild, 4); | |
| 58949 | - } | |
| 58950 | 59690 | |
| 58951 | 59691 | if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ |
| 58952 | 59692 | /* The root page of the b-tree now contains no cells. The only sibling |
| 58953 | 59693 | ** page is the right-child of the parent. Copy the contents of the |
| 58954 | 59694 | ** child page into the parent, decreasing the overall height of the |
| @@ -58957,130 +59697,54 @@ | ||
| 58957 | 59697 | ** |
| 58958 | 59698 | ** If this is an auto-vacuum database, the call to copyNodeContent() |
| 58959 | 59699 | ** sets all pointer-map entries corresponding to database image pages |
| 58960 | 59700 | ** for which the pointer is stored within the content being copied. |
| 58961 | 59701 | ** |
| 58962 | - ** The second assert below verifies that the child page is defragmented | |
| 58963 | - ** (it must be, as it was just reconstructed using assemblePage()). This | |
| 58964 | - ** is important if the parent page happens to be page 1 of the database | |
| 58965 | - ** image. */ | |
| 59702 | + ** It is critical that the child page be defragmented before being | |
| 59703 | + ** copied into the parent, because if the parent is page 1 then it will | |
| 59704 | + ** by smaller than the child due to the database header, and so all the | |
| 59705 | + ** free space needs to be up front. | |
| 59706 | + */ | |
| 58966 | 59707 | assert( nNew==1 ); |
| 59708 | + rc = defragmentPage(apNew[0]); | |
| 59709 | + testcase( rc!=SQLITE_OK ); | |
| 58967 | 59710 | assert( apNew[0]->nFree == |
| 58968 | - (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) | |
| 59711 | + (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) | |
| 59712 | + || rc!=SQLITE_OK | |
| 58969 | 59713 | ); |
| 58970 | 59714 | copyNodeContent(apNew[0], pParent, &rc); |
| 58971 | 59715 | freePage(apNew[0], &rc); |
| 58972 | - }else if( ISAUTOVACUUM ){ | |
| 58973 | - /* Fix the pointer-map entries for all the cells that were shifted around. | |
| 58974 | - ** There are several different types of pointer-map entries that need to | |
| 58975 | - ** be dealt with by this routine. Some of these have been set already, but | |
| 58976 | - ** many have not. The following is a summary: | |
| 58977 | - ** | |
| 58978 | - ** 1) The entries associated with new sibling pages that were not | |
| 58979 | - ** siblings when this function was called. These have already | |
| 58980 | - ** been set. We don't need to worry about old siblings that were | |
| 58981 | - ** moved to the free-list - the freePage() code has taken care | |
| 58982 | - ** of those. | |
| 58983 | - ** | |
| 58984 | - ** 2) The pointer-map entries associated with the first overflow | |
| 58985 | - ** page in any overflow chains used by new divider cells. These | |
| 58986 | - ** have also already been taken care of by the insertCell() code. | |
| 58987 | - ** | |
| 58988 | - ** 3) If the sibling pages are not leaves, then the child pages of | |
| 58989 | - ** cells stored on the sibling pages may need to be updated. | |
| 58990 | - ** | |
| 58991 | - ** 4) If the sibling pages are not internal intkey nodes, then any | |
| 58992 | - ** overflow pages used by these cells may need to be updated | |
| 58993 | - ** (internal intkey nodes never contain pointers to overflow pages). | |
| 58994 | - ** | |
| 58995 | - ** 5) If the sibling pages are not leaves, then the pointer-map | |
| 58996 | - ** entries for the right-child pages of each sibling may need | |
| 58997 | - ** to be updated. | |
| 58998 | - ** | |
| 58999 | - ** Cases 1 and 2 are dealt with above by other code. The next | |
| 59000 | - ** block deals with cases 3 and 4 and the one after that, case 5. Since | |
| 59001 | - ** setting a pointer map entry is a relatively expensive operation, this | |
| 59002 | - ** code only sets pointer map entries for child or overflow pages that have | |
| 59003 | - ** actually moved between pages. */ | |
| 59004 | - MemPage *pNew = apNew[0]; | |
| 59005 | - MemPage *pOld = apCopy[0]; | |
| 59006 | - int nOverflow = pOld->nOverflow; | |
| 59007 | - int iNextOld = pOld->nCell + nOverflow; | |
| 59008 | - int iOverflow = (nOverflow ? pOld->aiOvfl[0] : -1); | |
| 59009 | - j = 0; /* Current 'old' sibling page */ | |
| 59010 | - k = 0; /* Current 'new' sibling page */ | |
| 59011 | - for(i=0; i<nCell; i++){ | |
| 59012 | - int isDivider = 0; | |
| 59013 | - while( i==iNextOld ){ | |
| 59014 | - /* Cell i is the cell immediately following the last cell on old | |
| 59015 | - ** sibling page j. If the siblings are not leaf pages of an | |
| 59016 | - ** intkey b-tree, then cell i was a divider cell. */ | |
| 59017 | - assert( j+1 < ArraySize(apCopy) ); | |
| 59018 | - assert( j+1 < nOld ); | |
| 59019 | - pOld = apCopy[++j]; | |
| 59020 | - iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow; | |
| 59021 | - if( pOld->nOverflow ){ | |
| 59022 | - nOverflow = pOld->nOverflow; | |
| 59023 | - iOverflow = i + !leafData + pOld->aiOvfl[0]; | |
| 59024 | - } | |
| 59025 | - isDivider = !leafData; | |
| 59026 | - } | |
| 59027 | - | |
| 59028 | - assert(nOverflow>0 || iOverflow<i ); | |
| 59029 | - assert(nOverflow<2 || pOld->aiOvfl[0]==pOld->aiOvfl[1]-1); | |
| 59030 | - assert(nOverflow<3 || pOld->aiOvfl[1]==pOld->aiOvfl[2]-1); | |
| 59031 | - if( i==iOverflow ){ | |
| 59032 | - isDivider = 1; | |
| 59033 | - if( (--nOverflow)>0 ){ | |
| 59034 | - iOverflow++; | |
| 59035 | - } | |
| 59036 | - } | |
| 59037 | - | |
| 59038 | - if( i==cntNew[k] ){ | |
| 59039 | - /* Cell i is the cell immediately following the last cell on new | |
| 59040 | - ** sibling page k. If the siblings are not leaf pages of an | |
| 59041 | - ** intkey b-tree, then cell i is a divider cell. */ | |
| 59042 | - pNew = apNew[++k]; | |
| 59043 | - if( !leafData ) continue; | |
| 59044 | - } | |
| 59045 | - assert( j<nOld ); | |
| 59046 | - assert( k<nNew ); | |
| 59047 | - | |
| 59048 | - /* If the cell was originally divider cell (and is not now) or | |
| 59049 | - ** an overflow cell, or if the cell was located on a different sibling | |
| 59050 | - ** page before the balancing, then the pointer map entries associated | |
| 59051 | - ** with any child or overflow pages need to be updated. */ | |
| 59052 | - if( isDivider || pOld->pgno!=pNew->pgno ){ | |
| 59053 | - if( !leafCorrection ){ | |
| 59054 | - ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc); | |
| 59055 | - } | |
| 59056 | - if( szCell[i]>pNew->minLocal ){ | |
| 59057 | - ptrmapPutOvflPtr(pNew, apCell[i], &rc); | |
| 59058 | - } | |
| 59059 | - } | |
| 59060 | - } | |
| 59061 | - | |
| 59062 | - if( !leafCorrection ){ | |
| 59063 | - for(i=0; i<nNew; i++){ | |
| 59064 | - u32 key = get4byte(&apNew[i]->aData[8]); | |
| 59065 | - ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); | |
| 59066 | - } | |
| 59067 | - } | |
| 59716 | + }else if( ISAUTOVACUUM && !leafCorrection ){ | |
| 59717 | + /* Fix the pointer map entries associated with the right-child of each | |
| 59718 | + ** sibling page. All other pointer map entries have already been taken | |
| 59719 | + ** care of. */ | |
| 59720 | + for(i=0; i<nNew; i++){ | |
| 59721 | + u32 key = get4byte(&apNew[i]->aData[8]); | |
| 59722 | + ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); | |
| 59723 | + } | |
| 59724 | + } | |
| 59725 | + | |
| 59726 | + assert( pParent->isInit ); | |
| 59727 | + TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", | |
| 59728 | + nOld, nNew, nCell)); | |
| 59729 | + | |
| 59730 | + /* Free any old pages that were not reused as new pages. | |
| 59731 | + */ | |
| 59732 | + for(i=nNew; i<nOld; i++){ | |
| 59733 | + freePage(apOld[i], &rc); | |
| 59734 | + } | |
| 59068 | 59735 | |
| 59069 | 59736 | #if 0 |
| 59737 | + if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){ | |
| 59070 | 59738 | /* The ptrmapCheckPages() contains assert() statements that verify that |
| 59071 | 59739 | ** all pointer map pages are set correctly. This is helpful while |
| 59072 | 59740 | ** debugging. This is usually disabled because a corrupt database may |
| 59073 | 59741 | ** cause an assert() statement to fail. */ |
| 59074 | 59742 | ptrmapCheckPages(apNew, nNew); |
| 59075 | 59743 | ptrmapCheckPages(&pParent, 1); |
| 59744 | + } | |
| 59076 | 59745 | #endif |
| 59077 | - } | |
| 59078 | - | |
| 59079 | - assert( pParent->isInit ); | |
| 59080 | - TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", | |
| 59081 | - nOld, nNew, nCell)); | |
| 59082 | 59746 | |
| 59083 | 59747 | /* |
| 59084 | 59748 | ** Cleanup before returning. |
| 59085 | 59749 | */ |
| 59086 | 59750 | balance_cleanup: |
| @@ -60461,12 +61125,18 @@ | ||
| 60461 | 61125 | }else{ |
| 60462 | 61126 | int contentOffset = get2byteNotZero(&data[hdr+5]); |
| 60463 | 61127 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
| 60464 | 61128 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 60465 | 61129 | memset(hit, 1, contentOffset); |
| 61130 | + /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the | |
| 61131 | + ** number of cells on the page. */ | |
| 60466 | 61132 | nCell = get2byte(&data[hdr+3]); |
| 61133 | + /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page | |
| 61134 | + ** immediately follows the b-tree page header. */ | |
| 60467 | 61135 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 61136 | + /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte | |
| 61137 | + ** integer offsets to the cell contents. */ | |
| 60468 | 61138 | for(i=0; i<nCell; i++){ |
| 60469 | 61139 | int pc = get2byte(&data[cellStart+i*2]); |
| 60470 | 61140 | u32 size = 65536; |
| 60471 | 61141 | int j; |
| 60472 | 61142 | if( pc<=usableSize-4 ){ |
| @@ -60478,18 +61148,27 @@ | ||
| 60478 | 61148 | "Corruption detected in cell %d on page %d",i,iPage); |
| 60479 | 61149 | }else{ |
| 60480 | 61150 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 60481 | 61151 | } |
| 60482 | 61152 | } |
| 61153 | + /* EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header | |
| 61154 | + ** is the offset of the first freeblock, or zero if there are no | |
| 61155 | + ** freeblocks on the page. */ | |
| 60483 | 61156 | i = get2byte(&data[hdr+1]); |
| 60484 | 61157 | while( i>0 ){ |
| 60485 | 61158 | int size, j; |
| 60486 | 61159 | assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 60487 | 61160 | size = get2byte(&data[i+2]); |
| 60488 | 61161 | assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */ |
| 60489 | 61162 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 61163 | + /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a | |
| 61164 | + ** big-endian integer which is the offset in the b-tree page of the next | |
| 61165 | + ** freeblock in the chain, or zero if the freeblock is the last on the | |
| 61166 | + ** chain. */ | |
| 60490 | 61167 | j = get2byte(&data[i]); |
| 61168 | + /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of | |
| 61169 | + ** increasing offset. */ | |
| 60491 | 61170 | assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ |
| 60492 | 61171 | assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 60493 | 61172 | i = j; |
| 60494 | 61173 | } |
| 60495 | 61174 | for(i=cnt=0; i<usableSize; i++){ |
| @@ -60499,10 +61178,15 @@ | ||
| 60499 | 61178 | checkAppendMsg(pCheck, |
| 60500 | 61179 | "Multiple uses for byte %d of page %d", i, iPage); |
| 60501 | 61180 | break; |
| 60502 | 61181 | } |
| 60503 | 61182 | } |
| 61183 | + /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments | |
| 61184 | + ** is stored in the fifth field of the b-tree page header. | |
| 61185 | + ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the | |
| 61186 | + ** number of fragmented free bytes within the cell content area. | |
| 61187 | + */ | |
| 60504 | 61188 | if( cnt!=data[hdr+7] ){ |
| 60505 | 61189 | checkAppendMsg(pCheck, |
| 60506 | 61190 | "Fragmentation of %d bytes reported as %d on page %d", |
| 60507 | 61191 | cnt, data[hdr+7], iPage); |
| 60508 | 61192 | } |
| @@ -60902,10 +61586,15 @@ | ||
| 60902 | 61586 | */ |
| 60903 | 61587 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){ |
| 60904 | 61588 | return (p->pBt->btsFlags & BTS_READ_ONLY)!=0; |
| 60905 | 61589 | } |
| 60906 | 61590 | |
| 61591 | +/* | |
| 61592 | +** Return the size of the header added to each page by this module. | |
| 61593 | +*/ | |
| 61594 | +SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return sizeof(MemPage); } | |
| 61595 | + | |
| 60907 | 61596 | /************** End of btree.c ***********************************************/ |
| 60908 | 61597 | /************** Begin file backup.c ******************************************/ |
| 60909 | 61598 | /* |
| 60910 | 61599 | ** 2009 January 28 |
| 60911 | 61600 | ** |
| @@ -61025,10 +61714,24 @@ | ||
| 61025 | 61714 | static int setDestPgsz(sqlite3_backup *p){ |
| 61026 | 61715 | int rc; |
| 61027 | 61716 | rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0); |
| 61028 | 61717 | return rc; |
| 61029 | 61718 | } |
| 61719 | + | |
| 61720 | +/* | |
| 61721 | +** Check that there is no open read-transaction on the b-tree passed as the | |
| 61722 | +** second argument. If there is not, return SQLITE_OK. Otherwise, if there | |
| 61723 | +** is an open read-transaction, return SQLITE_ERROR and leave an error | |
| 61724 | +** message in database handle db. | |
| 61725 | +*/ | |
| 61726 | +static int checkReadTransaction(sqlite3 *db, Btree *p){ | |
| 61727 | + if( sqlite3BtreeIsInReadTrans(p) ){ | |
| 61728 | + sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use"); | |
| 61729 | + return SQLITE_ERROR; | |
| 61730 | + } | |
| 61731 | + return SQLITE_OK; | |
| 61732 | +} | |
| 61030 | 61733 | |
| 61031 | 61734 | /* |
| 61032 | 61735 | ** Create an sqlite3_backup process to copy the contents of zSrcDb from |
| 61033 | 61736 | ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return |
| 61034 | 61737 | ** a pointer to the new sqlite3_backup object. |
| @@ -61041,10 +61744,17 @@ | ||
| 61041 | 61744 | const char *zDestDb, /* Name of database within pDestDb */ |
| 61042 | 61745 | sqlite3* pSrcDb, /* Database connection to read from */ |
| 61043 | 61746 | const char *zSrcDb /* Name of database within pSrcDb */ |
| 61044 | 61747 | ){ |
| 61045 | 61748 | sqlite3_backup *p; /* Value to return */ |
| 61749 | + | |
| 61750 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 61751 | + if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){ | |
| 61752 | + (void)SQLITE_MISUSE_BKPT; | |
| 61753 | + return 0; | |
| 61754 | + } | |
| 61755 | +#endif | |
| 61046 | 61756 | |
| 61047 | 61757 | /* Lock the source database handle. The destination database |
| 61048 | 61758 | ** handle is not locked in this routine, but it is locked in |
| 61049 | 61759 | ** sqlite3_backup_step(). The user is required to ensure that no |
| 61050 | 61760 | ** other thread accesses the destination handle for the duration |
| @@ -61078,16 +61788,19 @@ | ||
| 61078 | 61788 | p->pDestDb = pDestDb; |
| 61079 | 61789 | p->pSrcDb = pSrcDb; |
| 61080 | 61790 | p->iNext = 1; |
| 61081 | 61791 | p->isAttached = 0; |
| 61082 | 61792 | |
| 61083 | - if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){ | |
| 61793 | + if( 0==p->pSrc || 0==p->pDest | |
| 61794 | + || setDestPgsz(p)==SQLITE_NOMEM | |
| 61795 | + || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK | |
| 61796 | + ){ | |
| 61084 | 61797 | /* One (or both) of the named databases did not exist or an OOM |
| 61085 | - ** error was hit. The error has already been written into the | |
| 61086 | - ** pDestDb handle. All that is left to do here is free the | |
| 61087 | - ** sqlite3_backup structure. | |
| 61088 | - */ | |
| 61798 | + ** error was hit. Or there is a transaction open on the destination | |
| 61799 | + ** database. The error has already been written into the pDestDb | |
| 61800 | + ** handle. All that is left to do here is free the sqlite3_backup | |
| 61801 | + ** structure. */ | |
| 61089 | 61802 | sqlite3_free(p); |
| 61090 | 61803 | p = 0; |
| 61091 | 61804 | } |
| 61092 | 61805 | } |
| 61093 | 61806 | if( p ){ |
| @@ -61238,10 +61951,13 @@ | ||
| 61238 | 61951 | int rc; |
| 61239 | 61952 | int destMode; /* Destination journal mode */ |
| 61240 | 61953 | int pgszSrc = 0; /* Source page size */ |
| 61241 | 61954 | int pgszDest = 0; /* Destination page size */ |
| 61242 | 61955 | |
| 61956 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 61957 | + if( p==0 ) return SQLITE_MISUSE_BKPT; | |
| 61958 | +#endif | |
| 61243 | 61959 | sqlite3_mutex_enter(p->pSrcDb->mutex); |
| 61244 | 61960 | sqlite3BtreeEnter(p->pSrc); |
| 61245 | 61961 | if( p->pDestDb ){ |
| 61246 | 61962 | sqlite3_mutex_enter(p->pDestDb->mutex); |
| 61247 | 61963 | } |
| @@ -61527,18 +62243,30 @@ | ||
| 61527 | 62243 | /* |
| 61528 | 62244 | ** Return the number of pages still to be backed up as of the most recent |
| 61529 | 62245 | ** call to sqlite3_backup_step(). |
| 61530 | 62246 | */ |
| 61531 | 62247 | SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){ |
| 62248 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 62249 | + if( p==0 ){ | |
| 62250 | + (void)SQLITE_MISUSE_BKPT; | |
| 62251 | + return 0; | |
| 62252 | + } | |
| 62253 | +#endif | |
| 61532 | 62254 | return p->nRemaining; |
| 61533 | 62255 | } |
| 61534 | 62256 | |
| 61535 | 62257 | /* |
| 61536 | 62258 | ** Return the total number of pages in the source database as of the most |
| 61537 | 62259 | ** recent call to sqlite3_backup_step(). |
| 61538 | 62260 | */ |
| 61539 | 62261 | SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){ |
| 62262 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 62263 | + if( p==0 ){ | |
| 62264 | + (void)SQLITE_MISUSE_BKPT; | |
| 62265 | + return 0; | |
| 62266 | + } | |
| 62267 | +#endif | |
| 61540 | 62268 | return p->nPagecount; |
| 61541 | 62269 | } |
| 61542 | 62270 | |
| 61543 | 62271 | /* |
| 61544 | 62272 | ** This function is called after the contents of page iPage of the |
| @@ -63825,10 +64553,38 @@ | ||
| 63825 | 64553 | } |
| 63826 | 64554 | p->nOp += nOp; |
| 63827 | 64555 | } |
| 63828 | 64556 | return addr; |
| 63829 | 64557 | } |
| 64558 | + | |
| 64559 | +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) | |
| 64560 | +/* | |
| 64561 | +** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus(). | |
| 64562 | +*/ | |
| 64563 | +SQLITE_PRIVATE void sqlite3VdbeScanStatus( | |
| 64564 | + Vdbe *p, /* VM to add scanstatus() to */ | |
| 64565 | + int addrExplain, /* Address of OP_Explain (or 0) */ | |
| 64566 | + int addrLoop, /* Address of loop counter */ | |
| 64567 | + int addrVisit, /* Address of rows visited counter */ | |
| 64568 | + LogEst nEst, /* Estimated number of output rows */ | |
| 64569 | + const char *zName /* Name of table or index being scanned */ | |
| 64570 | +){ | |
| 64571 | + int nByte = (p->nScan+1) * sizeof(ScanStatus); | |
| 64572 | + ScanStatus *aNew; | |
| 64573 | + aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte); | |
| 64574 | + if( aNew ){ | |
| 64575 | + ScanStatus *pNew = &aNew[p->nScan++]; | |
| 64576 | + pNew->addrExplain = addrExplain; | |
| 64577 | + pNew->addrLoop = addrLoop; | |
| 64578 | + pNew->addrVisit = addrVisit; | |
| 64579 | + pNew->nEst = nEst; | |
| 64580 | + pNew->zName = sqlite3DbStrDup(p->db, zName); | |
| 64581 | + p->aScan = aNew; | |
| 64582 | + } | |
| 64583 | +} | |
| 64584 | +#endif | |
| 64585 | + | |
| 63830 | 64586 | |
| 63831 | 64587 | /* |
| 63832 | 64588 | ** Change the value of the P1 operand for a specific instruction. |
| 63833 | 64589 | ** This routine is useful when a large program is loaded from a |
| 63834 | 64590 | ** static array using sqlite3VdbeAddOpList but we want to make a |
| @@ -64924,10 +65680,13 @@ | ||
| 64924 | 65680 | p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); |
| 64925 | 65681 | p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); |
| 64926 | 65682 | p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), |
| 64927 | 65683 | &zCsr, zEnd, &nByte); |
| 64928 | 65684 | p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte); |
| 65685 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
| 65686 | + p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), &zCsr, zEnd, &nByte); | |
| 65687 | +#endif | |
| 64929 | 65688 | if( nByte ){ |
| 64930 | 65689 | p->pFree = sqlite3DbMallocZero(db, nByte); |
| 64931 | 65690 | } |
| 64932 | 65691 | zCsr = p->pFree; |
| 64933 | 65692 | zEnd = &zCsr[nByte]; |
| @@ -64991,10 +65750,13 @@ | ||
| 64991 | 65750 | ** is used, for example, when a trigger sub-program is halted to restore |
| 64992 | 65751 | ** control to the main program. |
| 64993 | 65752 | */ |
| 64994 | 65753 | SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ |
| 64995 | 65754 | Vdbe *v = pFrame->v; |
| 65755 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
| 65756 | + v->anExec = pFrame->anExec; | |
| 65757 | +#endif | |
| 64996 | 65758 | v->aOnceFlag = pFrame->aOnceFlag; |
| 64997 | 65759 | v->nOnceFlag = pFrame->nOnceFlag; |
| 64998 | 65760 | v->aOp = pFrame->aOp; |
| 64999 | 65761 | v->nOp = pFrame->nOp; |
| 65000 | 65762 | v->aMem = pFrame->aMem; |
| @@ -65001,10 +65763,11 @@ | ||
| 65001 | 65763 | v->nMem = pFrame->nMem; |
| 65002 | 65764 | v->apCsr = pFrame->apCsr; |
| 65003 | 65765 | v->nCursor = pFrame->nCursor; |
| 65004 | 65766 | v->db->lastRowid = pFrame->lastRowid; |
| 65005 | 65767 | v->nChange = pFrame->nChange; |
| 65768 | + v->db->nChange = pFrame->nDbChange; | |
| 65006 | 65769 | return pFrame->pc; |
| 65007 | 65770 | } |
| 65008 | 65771 | |
| 65009 | 65772 | /* |
| 65010 | 65773 | ** Close all cursors. |
| @@ -65568,10 +66331,11 @@ | ||
| 65568 | 66331 | ** so, abort any other statements this handle currently has active. |
| 65569 | 66332 | */ |
| 65570 | 66333 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 65571 | 66334 | sqlite3CloseSavepoints(db); |
| 65572 | 66335 | db->autoCommit = 1; |
| 66336 | + p->nChange = 0; | |
| 65573 | 66337 | } |
| 65574 | 66338 | } |
| 65575 | 66339 | } |
| 65576 | 66340 | |
| 65577 | 66341 | /* Check for immediate foreign key violations. */ |
| @@ -65608,18 +66372,20 @@ | ||
| 65608 | 66372 | sqlite3VdbeLeave(p); |
| 65609 | 66373 | return SQLITE_BUSY; |
| 65610 | 66374 | }else if( rc!=SQLITE_OK ){ |
| 65611 | 66375 | p->rc = rc; |
| 65612 | 66376 | sqlite3RollbackAll(db, SQLITE_OK); |
| 66377 | + p->nChange = 0; | |
| 65613 | 66378 | }else{ |
| 65614 | 66379 | db->nDeferredCons = 0; |
| 65615 | 66380 | db->nDeferredImmCons = 0; |
| 65616 | 66381 | db->flags &= ~SQLITE_DeferFKs; |
| 65617 | 66382 | sqlite3CommitInternalChanges(db); |
| 65618 | 66383 | } |
| 65619 | 66384 | }else{ |
| 65620 | 66385 | sqlite3RollbackAll(db, SQLITE_OK); |
| 66386 | + p->nChange = 0; | |
| 65621 | 66387 | } |
| 65622 | 66388 | db->nStatement = 0; |
| 65623 | 66389 | }else if( eStatementOp==0 ){ |
| 65624 | 66390 | if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ |
| 65625 | 66391 | eStatementOp = SAVEPOINT_RELEASE; |
| @@ -65627,10 +66393,11 @@ | ||
| 65627 | 66393 | eStatementOp = SAVEPOINT_ROLLBACK; |
| 65628 | 66394 | }else{ |
| 65629 | 66395 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 65630 | 66396 | sqlite3CloseSavepoints(db); |
| 65631 | 66397 | db->autoCommit = 1; |
| 66398 | + p->nChange = 0; | |
| 65632 | 66399 | } |
| 65633 | 66400 | } |
| 65634 | 66401 | |
| 65635 | 66402 | /* If eStatementOp is non-zero, then a statement transaction needs to |
| 65636 | 66403 | ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to |
| @@ -65647,10 +66414,11 @@ | ||
| 65647 | 66414 | p->zErrMsg = 0; |
| 65648 | 66415 | } |
| 65649 | 66416 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 65650 | 66417 | sqlite3CloseSavepoints(db); |
| 65651 | 66418 | db->autoCommit = 1; |
| 66419 | + p->nChange = 0; | |
| 65652 | 66420 | } |
| 65653 | 66421 | } |
| 65654 | 66422 | |
| 65655 | 66423 | /* If this was an INSERT, UPDATE or DELETE and no statement transaction |
| 65656 | 66424 | ** has been rolled back, update the database connection change-counter. |
| @@ -65908,10 +66676,16 @@ | ||
| 65908 | 66676 | for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); |
| 65909 | 66677 | vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 65910 | 66678 | sqlite3DbFree(db, p->aColName); |
| 65911 | 66679 | sqlite3DbFree(db, p->zSql); |
| 65912 | 66680 | sqlite3DbFree(db, p->pFree); |
| 66681 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
| 66682 | + for(i=0; i<p->nScan; i++){ | |
| 66683 | + sqlite3DbFree(db, p->aScan[i].zName); | |
| 66684 | + } | |
| 66685 | + sqlite3DbFree(db, p->aScan); | |
| 66686 | +#endif | |
| 65913 | 66687 | } |
| 65914 | 66688 | |
| 65915 | 66689 | /* |
| 65916 | 66690 | ** Delete an entire VDBE. |
| 65917 | 66691 | */ |
| @@ -66066,13 +66840,11 @@ | ||
| 66066 | 66840 | /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
| 66067 | 66841 | # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) |
| 66068 | 66842 | i64 i = pMem->u.i; |
| 66069 | 66843 | u64 u; |
| 66070 | 66844 | if( i<0 ){ |
| 66071 | - if( i<(-MAX_6BYTE) ) return 6; | |
| 66072 | - /* Previous test prevents: u = -(-9223372036854775808) */ | |
| 66073 | - u = -i; | |
| 66845 | + u = ~i; | |
| 66074 | 66846 | }else{ |
| 66075 | 66847 | u = i; |
| 66076 | 66848 | } |
| 66077 | 66849 | if( u<=127 ){ |
| 66078 | 66850 | return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1; |
| @@ -66234,14 +67006,18 @@ | ||
| 66234 | 67006 | ){ |
| 66235 | 67007 | u64 x = FOUR_BYTE_UINT(buf); |
| 66236 | 67008 | u32 y = FOUR_BYTE_UINT(buf+4); |
| 66237 | 67009 | x = (x<<32) + y; |
| 66238 | 67010 | if( serial_type==6 ){ |
| 67011 | + /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit | |
| 67012 | + ** twos-complement integer. */ | |
| 66239 | 67013 | pMem->u.i = *(i64*)&x; |
| 66240 | 67014 | pMem->flags = MEM_Int; |
| 66241 | 67015 | testcase( pMem->u.i<0 ); |
| 66242 | 67016 | }else{ |
| 67017 | + /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit | |
| 67018 | + ** floating point number. */ | |
| 66243 | 67019 | #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) |
| 66244 | 67020 | /* Verify that integers and floating point values use the same |
| 66245 | 67021 | ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is |
| 66246 | 67022 | ** defined that 64-bit floating point values really are mixed |
| 66247 | 67023 | ** endian. |
| @@ -66265,39 +67041,50 @@ | ||
| 66265 | 67041 | Mem *pMem /* Memory cell to write value into */ |
| 66266 | 67042 | ){ |
| 66267 | 67043 | switch( serial_type ){ |
| 66268 | 67044 | case 10: /* Reserved for future use */ |
| 66269 | 67045 | case 11: /* Reserved for future use */ |
| 66270 | - case 0: { /* NULL */ | |
| 67046 | + case 0: { /* Null */ | |
| 67047 | + /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */ | |
| 66271 | 67048 | pMem->flags = MEM_Null; |
| 66272 | 67049 | break; |
| 66273 | 67050 | } |
| 66274 | - case 1: { /* 1-byte signed integer */ | |
| 67051 | + case 1: { | |
| 67052 | + /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement | |
| 67053 | + ** integer. */ | |
| 66275 | 67054 | pMem->u.i = ONE_BYTE_INT(buf); |
| 66276 | 67055 | pMem->flags = MEM_Int; |
| 66277 | 67056 | testcase( pMem->u.i<0 ); |
| 66278 | 67057 | return 1; |
| 66279 | 67058 | } |
| 66280 | 67059 | case 2: { /* 2-byte signed integer */ |
| 67060 | + /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit | |
| 67061 | + ** twos-complement integer. */ | |
| 66281 | 67062 | pMem->u.i = TWO_BYTE_INT(buf); |
| 66282 | 67063 | pMem->flags = MEM_Int; |
| 66283 | 67064 | testcase( pMem->u.i<0 ); |
| 66284 | 67065 | return 2; |
| 66285 | 67066 | } |
| 66286 | 67067 | case 3: { /* 3-byte signed integer */ |
| 67068 | + /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit | |
| 67069 | + ** twos-complement integer. */ | |
| 66287 | 67070 | pMem->u.i = THREE_BYTE_INT(buf); |
| 66288 | 67071 | pMem->flags = MEM_Int; |
| 66289 | 67072 | testcase( pMem->u.i<0 ); |
| 66290 | 67073 | return 3; |
| 66291 | 67074 | } |
| 66292 | 67075 | case 4: { /* 4-byte signed integer */ |
| 67076 | + /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit | |
| 67077 | + ** twos-complement integer. */ | |
| 66293 | 67078 | pMem->u.i = FOUR_BYTE_INT(buf); |
| 66294 | 67079 | pMem->flags = MEM_Int; |
| 66295 | 67080 | testcase( pMem->u.i<0 ); |
| 66296 | 67081 | return 4; |
| 66297 | 67082 | } |
| 66298 | 67083 | case 5: { /* 6-byte signed integer */ |
| 67084 | + /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit | |
| 67085 | + ** twos-complement integer. */ | |
| 66299 | 67086 | pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); |
| 66300 | 67087 | pMem->flags = MEM_Int; |
| 66301 | 67088 | testcase( pMem->u.i<0 ); |
| 66302 | 67089 | return 6; |
| 66303 | 67090 | } |
| @@ -66307,15 +67094,21 @@ | ||
| 66307 | 67094 | ** to avoid having to move the frame pointer in the common case */ |
| 66308 | 67095 | return serialGet(buf,serial_type,pMem); |
| 66309 | 67096 | } |
| 66310 | 67097 | case 8: /* Integer 0 */ |
| 66311 | 67098 | case 9: { /* Integer 1 */ |
| 67099 | + /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */ | |
| 67100 | + /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */ | |
| 66312 | 67101 | pMem->u.i = serial_type-8; |
| 66313 | 67102 | pMem->flags = MEM_Int; |
| 66314 | 67103 | return 0; |
| 66315 | 67104 | } |
| 66316 | 67105 | default: { |
| 67106 | + /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in | |
| 67107 | + ** length. | |
| 67108 | + ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and | |
| 67109 | + ** (N-13)/2 bytes in length. */ | |
| 66317 | 67110 | static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; |
| 66318 | 67111 | pMem->z = (char *)buf; |
| 66319 | 67112 | pMem->n = (serial_type-12)/2; |
| 66320 | 67113 | pMem->flags = aFlag[serial_type&1]; |
| 66321 | 67114 | return pMem->n; |
| @@ -68275,15 +69068,23 @@ | ||
| 68275 | 69068 | sqlite3_stmt *pStmt, |
| 68276 | 69069 | int N, |
| 68277 | 69070 | const void *(*xFunc)(Mem*), |
| 68278 | 69071 | int useType |
| 68279 | 69072 | ){ |
| 68280 | - const void *ret = 0; | |
| 68281 | - Vdbe *p = (Vdbe *)pStmt; | |
| 69073 | + const void *ret; | |
| 69074 | + Vdbe *p; | |
| 68282 | 69075 | int n; |
| 68283 | - sqlite3 *db = p->db; | |
| 68284 | - | |
| 69076 | + sqlite3 *db; | |
| 69077 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 69078 | + if( pStmt==0 ){ | |
| 69079 | + (void)SQLITE_MISUSE_BKPT; | |
| 69080 | + return 0; | |
| 69081 | + } | |
| 69082 | +#endif | |
| 69083 | + ret = 0; | |
| 69084 | + p = (Vdbe *)pStmt; | |
| 69085 | + db = p->db; | |
| 68285 | 69086 | assert( db!=0 ); |
| 68286 | 69087 | n = sqlite3_column_count(pStmt); |
| 68287 | 69088 | if( N<n && N>=0 ){ |
| 68288 | 69089 | N += useType*n; |
| 68289 | 69090 | sqlite3_mutex_enter(db->mutex); |
| @@ -68744,10 +69545,16 @@ | ||
| 68744 | 69545 | ** prepared statement for the database connection. Return NULL if there |
| 68745 | 69546 | ** are no more. |
| 68746 | 69547 | */ |
| 68747 | 69548 | SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ |
| 68748 | 69549 | sqlite3_stmt *pNext; |
| 69550 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 69551 | + if( !sqlite3SafetyCheckOk(pDb) ){ | |
| 69552 | + (void)SQLITE_MISUSE_BKPT; | |
| 69553 | + return 0; | |
| 69554 | + } | |
| 69555 | +#endif | |
| 68749 | 69556 | sqlite3_mutex_enter(pDb->mutex); |
| 68750 | 69557 | if( pStmt==0 ){ |
| 68751 | 69558 | pNext = (sqlite3_stmt*)pDb->pVdbe; |
| 68752 | 69559 | }else{ |
| 68753 | 69560 | pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; |
| @@ -68759,15 +69566,91 @@ | ||
| 68759 | 69566 | /* |
| 68760 | 69567 | ** Return the value of a status counter for a prepared statement |
| 68761 | 69568 | */ |
| 68762 | 69569 | SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ |
| 68763 | 69570 | Vdbe *pVdbe = (Vdbe*)pStmt; |
| 68764 | - u32 v = pVdbe->aCounter[op]; | |
| 69571 | + u32 v; | |
| 69572 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 69573 | + if( !pStmt ){ | |
| 69574 | + (void)SQLITE_MISUSE_BKPT; | |
| 69575 | + return 0; | |
| 69576 | + } | |
| 69577 | +#endif | |
| 69578 | + v = pVdbe->aCounter[op]; | |
| 68765 | 69579 | if( resetFlag ) pVdbe->aCounter[op] = 0; |
| 68766 | 69580 | return (int)v; |
| 68767 | 69581 | } |
| 68768 | 69582 | |
| 69583 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
| 69584 | +/* | |
| 69585 | +** Return status data for a single loop within query pStmt. | |
| 69586 | +*/ | |
| 69587 | +SQLITE_API int sqlite3_stmt_scanstatus( | |
| 69588 | + sqlite3_stmt *pStmt, /* Prepared statement being queried */ | |
| 69589 | + int idx, /* Index of loop to report on */ | |
| 69590 | + int iScanStatusOp, /* Which metric to return */ | |
| 69591 | + void *pOut /* OUT: Write the answer here */ | |
| 69592 | +){ | |
| 69593 | + Vdbe *p = (Vdbe*)pStmt; | |
| 69594 | + ScanStatus *pScan; | |
| 69595 | + if( idx<0 || idx>=p->nScan ) return 1; | |
| 69596 | + pScan = &p->aScan[idx]; | |
| 69597 | + switch( iScanStatusOp ){ | |
| 69598 | + case SQLITE_SCANSTAT_NLOOP: { | |
| 69599 | + *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; | |
| 69600 | + break; | |
| 69601 | + } | |
| 69602 | + case SQLITE_SCANSTAT_NVISIT: { | |
| 69603 | + *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; | |
| 69604 | + break; | |
| 69605 | + } | |
| 69606 | + case SQLITE_SCANSTAT_EST: { | |
| 69607 | + double r = 1.0; | |
| 69608 | + LogEst x = pScan->nEst; | |
| 69609 | + while( x<100 ){ | |
| 69610 | + x += 10; | |
| 69611 | + r *= 0.5; | |
| 69612 | + } | |
| 69613 | + *(double*)pOut = r*sqlite3LogEstToInt(x); | |
| 69614 | + break; | |
| 69615 | + } | |
| 69616 | + case SQLITE_SCANSTAT_NAME: { | |
| 69617 | + *(const char**)pOut = pScan->zName; | |
| 69618 | + break; | |
| 69619 | + } | |
| 69620 | + case SQLITE_SCANSTAT_EXPLAIN: { | |
| 69621 | + if( pScan->addrExplain ){ | |
| 69622 | + *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; | |
| 69623 | + }else{ | |
| 69624 | + *(const char**)pOut = 0; | |
| 69625 | + } | |
| 69626 | + break; | |
| 69627 | + } | |
| 69628 | + case SQLITE_SCANSTAT_SELECTID: { | |
| 69629 | + if( pScan->addrExplain ){ | |
| 69630 | + *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; | |
| 69631 | + }else{ | |
| 69632 | + *(int*)pOut = -1; | |
| 69633 | + } | |
| 69634 | + break; | |
| 69635 | + } | |
| 69636 | + default: { | |
| 69637 | + return 1; | |
| 69638 | + } | |
| 69639 | + } | |
| 69640 | + return 0; | |
| 69641 | +} | |
| 69642 | + | |
| 69643 | +/* | |
| 69644 | +** Zero all counters associated with the sqlite3_stmt_scanstatus() data. | |
| 69645 | +*/ | |
| 69646 | +SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ | |
| 69647 | + Vdbe *p = (Vdbe*)pStmt; | |
| 69648 | + memset(p->anExec, 0, p->nOp * sizeof(i64)); | |
| 69649 | +} | |
| 69650 | +#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ | |
| 69651 | + | |
| 68769 | 69652 | /************** End of vdbeapi.c *********************************************/ |
| 68770 | 69653 | /************** Begin file vdbetrace.c ***************************************/ |
| 68771 | 69654 | /* |
| 68772 | 69655 | ** 2009 November 25 |
| 68773 | 69656 | ** |
| @@ -69649,10 +70532,13 @@ | ||
| 69649 | 70532 | #ifdef VDBE_PROFILE |
| 69650 | 70533 | start = sqlite3Hwtime(); |
| 69651 | 70534 | #endif |
| 69652 | 70535 | nVmStep++; |
| 69653 | 70536 | pOp = &aOp[pc]; |
| 70537 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
| 70538 | + if( p->anExec ) p->anExec[pc]++; | |
| 70539 | +#endif | |
| 69654 | 70540 | |
| 69655 | 70541 | /* Only allow tracing if SQLITE_DEBUG is defined. |
| 69656 | 70542 | */ |
| 69657 | 70543 | #ifdef SQLITE_DEBUG |
| 69658 | 70544 | if( db->flags & SQLITE_VdbeTrace ){ |
| @@ -71674,11 +72560,14 @@ | ||
| 71674 | 72560 | testcase( serial_type==127 ); |
| 71675 | 72561 | testcase( serial_type==128 ); |
| 71676 | 72562 | nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); |
| 71677 | 72563 | }while( (--pRec)>=pData0 ); |
| 71678 | 72564 | |
| 71679 | - /* Add the initial header varint and total the size */ | |
| 72565 | + /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint | |
| 72566 | + ** which determines the total number of bytes in the header. The varint | |
| 72567 | + ** value is the size of the header in bytes including the size varint | |
| 72568 | + ** itself. */ | |
| 71680 | 72569 | testcase( nHdr==126 ); |
| 71681 | 72570 | testcase( nHdr==127 ); |
| 71682 | 72571 | if( nHdr<=126 ){ |
| 71683 | 72572 | /* The common case */ |
| 71684 | 72573 | nHdr += 1; |
| @@ -71708,11 +72597,15 @@ | ||
| 71708 | 72597 | j = nHdr; |
| 71709 | 72598 | assert( pData0<=pLast ); |
| 71710 | 72599 | pRec = pData0; |
| 71711 | 72600 | do{ |
| 71712 | 72601 | serial_type = pRec->uTemp; |
| 72602 | + /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more | |
| 72603 | + ** additional varints, one per column. */ | |
| 71713 | 72604 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
| 72605 | + /* EVIDENCE-OF: R-64536-51728 The values for each column in the record | |
| 72606 | + ** immediately follow the header. */ | |
| 71714 | 72607 | j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ |
| 71715 | 72608 | }while( (++pRec)<=pLast ); |
| 71716 | 72609 | assert( i==nHdr ); |
| 71717 | 72610 | assert( j==nByte ); |
| 71718 | 72611 | |
| @@ -72843,14 +73736,14 @@ | ||
| 72843 | 73736 | } |
| 72844 | 73737 | pIdxKey = &r; |
| 72845 | 73738 | }else{ |
| 72846 | 73739 | pIdxKey = sqlite3VdbeAllocUnpackedRecord( |
| 72847 | 73740 | pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree |
| 72848 | - ); | |
| 73741 | + ); | |
| 72849 | 73742 | if( pIdxKey==0 ) goto no_mem; |
| 72850 | 73743 | assert( pIn3->flags & MEM_Blob ); |
| 72851 | - assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */ | |
| 73744 | + ExpandBlob(pIn3); | |
| 72852 | 73745 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
| 72853 | 73746 | } |
| 72854 | 73747 | pIdxKey->default_rc = 0; |
| 72855 | 73748 | if( pOp->opcode==OP_NoConflict ){ |
| 72856 | 73749 | /* For the OP_NoConflict opcode, take the jump if any of the |
| @@ -73540,13 +74433,13 @@ | ||
| 73540 | 74433 | } |
| 73541 | 74434 | /* Opcode: Rewind P1 P2 * * * |
| 73542 | 74435 | ** |
| 73543 | 74436 | ** The next use of the Rowid or Column or Next instruction for P1 |
| 73544 | 74437 | ** will refer to the first entry in the database table or index. |
| 73545 | -** If the table or index is empty and P2>0, then jump immediately to P2. | |
| 73546 | -** If P2 is 0 or if the table or index is not empty, fall through | |
| 73547 | -** to the following instruction. | |
| 74438 | +** If the table or index is empty, jump immediately to P2. | |
| 74439 | +** If the table or index is not empty, fall through to the following | |
| 74440 | +** instruction. | |
| 73548 | 74441 | ** |
| 73549 | 74442 | ** This opcode leaves the cursor configured to move in forward order, |
| 73550 | 74443 | ** from the beginning toward the end. In other words, the cursor is |
| 73551 | 74444 | ** configured to use Next, not Prev. |
| 73552 | 74445 | */ |
| @@ -74458,10 +75351,13 @@ | ||
| 74458 | 75351 | pFrame->aOp = p->aOp; |
| 74459 | 75352 | pFrame->nOp = p->nOp; |
| 74460 | 75353 | pFrame->token = pProgram->token; |
| 74461 | 75354 | pFrame->aOnceFlag = p->aOnceFlag; |
| 74462 | 75355 | pFrame->nOnceFlag = p->nOnceFlag; |
| 75356 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
| 75357 | + pFrame->anExec = p->anExec; | |
| 75358 | +#endif | |
| 74463 | 75359 | |
| 74464 | 75360 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 74465 | 75361 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
| 74466 | 75362 | pMem->flags = MEM_Undefined; |
| 74467 | 75363 | pMem->db = db; |
| @@ -74475,10 +75371,11 @@ | ||
| 74475 | 75371 | |
| 74476 | 75372 | p->nFrame++; |
| 74477 | 75373 | pFrame->pParent = p->pFrame; |
| 74478 | 75374 | pFrame->lastRowid = lastRowid; |
| 74479 | 75375 | pFrame->nChange = p->nChange; |
| 75376 | + pFrame->nDbChange = p->db->nChange; | |
| 74480 | 75377 | p->nChange = 0; |
| 74481 | 75378 | p->pFrame = pFrame; |
| 74482 | 75379 | p->aMem = aMem = &VdbeFrameMem(pFrame)[-1]; |
| 74483 | 75380 | p->nMem = pFrame->nChildMem; |
| 74484 | 75381 | p->nCursor = (u16)pFrame->nChildCsr; |
| @@ -74485,10 +75382,13 @@ | ||
| 74485 | 75382 | p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; |
| 74486 | 75383 | p->aOp = aOp = pProgram->aOp; |
| 74487 | 75384 | p->nOp = pProgram->nOp; |
| 74488 | 75385 | p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor]; |
| 74489 | 75386 | p->nOnceFlag = pProgram->nOnce; |
| 75387 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
| 75388 | + p->anExec = 0; | |
| 75389 | +#endif | |
| 74490 | 75390 | pc = -1; |
| 74491 | 75391 | memset(p->aOnceFlag, 0, p->nOnceFlag); |
| 74492 | 75392 | |
| 74493 | 75393 | break; |
| 74494 | 75394 | } |
| @@ -75673,10 +76573,15 @@ | ||
| 75673 | 76573 | char *zErr = 0; |
| 75674 | 76574 | Table *pTab; |
| 75675 | 76575 | Parse *pParse = 0; |
| 75676 | 76576 | Incrblob *pBlob = 0; |
| 75677 | 76577 | |
| 76578 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 76579 | + if( !sqlite3SafetyCheckOk(db) || ppBlob==0 || zTable==0 ){ | |
| 76580 | + return SQLITE_MISUSE_BKPT; | |
| 76581 | + } | |
| 76582 | +#endif | |
| 75678 | 76583 | flags = !!flags; /* flags = (flags ? 1 : 0); */ |
| 75679 | 76584 | *ppBlob = 0; |
| 75680 | 76585 | |
| 75681 | 76586 | sqlite3_mutex_enter(db->mutex); |
| 75682 | 76587 | |
| @@ -75891,11 +76796,10 @@ | ||
| 75891 | 76796 | v = (Vdbe*)p->pStmt; |
| 75892 | 76797 | |
| 75893 | 76798 | if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){ |
| 75894 | 76799 | /* Request is out of range. Return a transient error. */ |
| 75895 | 76800 | rc = SQLITE_ERROR; |
| 75896 | - sqlite3Error(db, SQLITE_ERROR); | |
| 75897 | 76801 | }else if( v==0 ){ |
| 75898 | 76802 | /* If there is no statement handle, then the blob-handle has |
| 75899 | 76803 | ** already been invalidated. Return SQLITE_ABORT in this case. |
| 75900 | 76804 | */ |
| 75901 | 76805 | rc = SQLITE_ABORT; |
| @@ -75909,14 +76813,14 @@ | ||
| 75909 | 76813 | sqlite3BtreeLeaveCursor(p->pCsr); |
| 75910 | 76814 | if( rc==SQLITE_ABORT ){ |
| 75911 | 76815 | sqlite3VdbeFinalize(v); |
| 75912 | 76816 | p->pStmt = 0; |
| 75913 | 76817 | }else{ |
| 75914 | - db->errCode = rc; | |
| 75915 | 76818 | v->rc = rc; |
| 75916 | 76819 | } |
| 75917 | 76820 | } |
| 76821 | + sqlite3Error(db, rc); | |
| 75918 | 76822 | rc = sqlite3ApiExit(db, rc); |
| 75919 | 76823 | sqlite3_mutex_leave(db->mutex); |
| 75920 | 76824 | return rc; |
| 75921 | 76825 | } |
| 75922 | 76826 | |
| @@ -76089,11 +76993,11 @@ | ||
| 76089 | 76993 | ** itself. |
| 76090 | 76994 | ** |
| 76091 | 76995 | ** The sorter is running in multi-threaded mode if (a) the library was built |
| 76092 | 76996 | ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater |
| 76093 | 76997 | ** than zero, and (b) worker threads have been enabled at runtime by calling |
| 76094 | -** sqlite3_config(SQLITE_CONFIG_WORKER_THREADS, ...). | |
| 76998 | +** "PRAGMA threads=N" with some value of N greater than 0. | |
| 76095 | 76999 | ** |
| 76096 | 77000 | ** When Rewind() is called, any data remaining in memory is flushed to a |
| 76097 | 77001 | ** final PMA. So at this point the data is stored in some number of sorted |
| 76098 | 77002 | ** PMAs within temporary files on disk. |
| 76099 | 77003 | ** |
| @@ -76134,10 +77038,17 @@ | ||
| 76134 | 77038 | */ |
| 76135 | 77039 | #if 0 |
| 76136 | 77040 | # define SQLITE_DEBUG_SORTER_THREADS 1 |
| 76137 | 77041 | #endif |
| 76138 | 77042 | |
| 77043 | +/* | |
| 77044 | +** Hard-coded maximum amount of data to accumulate in memory before flushing | |
| 77045 | +** to a level 0 PMA. The purpose of this limit is to prevent various integer | |
| 77046 | +** overflows. 512MiB. | |
| 77047 | +*/ | |
| 77048 | +#define SQLITE_MAX_MXPMASIZE (1<<29) | |
| 77049 | + | |
| 76139 | 77050 | /* |
| 76140 | 77051 | ** Private objects used by the sorter |
| 76141 | 77052 | */ |
| 76142 | 77053 | typedef struct MergeEngine MergeEngine; /* Merge PMAs together */ |
| 76143 | 77054 | typedef struct PmaReader PmaReader; /* Incrementally read one PMA */ |
| @@ -76832,17 +77743,15 @@ | ||
| 76832 | 77743 | |
| 76833 | 77744 | if( !sqlite3TempInMemory(db) ){ |
| 76834 | 77745 | pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz; |
| 76835 | 77746 | mxCache = db->aDb[0].pSchema->cache_size; |
| 76836 | 77747 | if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING; |
| 76837 | - pSorter->mxPmaSize = mxCache * pgsz; | |
| 77748 | + pSorter->mxPmaSize = MIN((i64)mxCache*pgsz, SQLITE_MAX_MXPMASIZE); | |
| 76838 | 77749 | |
| 76839 | - /* If the application has not configure scratch memory using | |
| 76840 | - ** SQLITE_CONFIG_SCRATCH then we assume it is OK to do large memory | |
| 76841 | - ** allocations. If scratch memory has been configured, then assume | |
| 76842 | - ** large memory allocations should be avoided to prevent heap | |
| 76843 | - ** fragmentation. | |
| 77750 | + /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of | |
| 77751 | + ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary | |
| 77752 | + ** large heap allocations. | |
| 76844 | 77753 | */ |
| 76845 | 77754 | if( sqlite3GlobalConfig.pScratch==0 ){ |
| 76846 | 77755 | assert( pSorter->iMemory==0 ); |
| 76847 | 77756 | pSorter->nMemory = pgsz; |
| 76848 | 77757 | pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz); |
| @@ -79210,19 +80119,19 @@ | ||
| 79210 | 80119 | ** |
| 79211 | 80120 | ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) |
| 79212 | 80121 | ** is a helper function - a callback for the tree walker. |
| 79213 | 80122 | */ |
| 79214 | 80123 | static int incrAggDepth(Walker *pWalker, Expr *pExpr){ |
| 79215 | - if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i; | |
| 80124 | + if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n; | |
| 79216 | 80125 | return WRC_Continue; |
| 79217 | 80126 | } |
| 79218 | 80127 | static void incrAggFunctionDepth(Expr *pExpr, int N){ |
| 79219 | 80128 | if( N>0 ){ |
| 79220 | 80129 | Walker w; |
| 79221 | 80130 | memset(&w, 0, sizeof(w)); |
| 79222 | 80131 | w.xExprCallback = incrAggDepth; |
| 79223 | - w.u.i = N; | |
| 80132 | + w.u.n = N; | |
| 79224 | 80133 | sqlite3WalkExpr(&w, pExpr); |
| 79225 | 80134 | } |
| 79226 | 80135 | } |
| 79227 | 80136 | |
| 79228 | 80137 | /* |
| @@ -79766,11 +80675,11 @@ | ||
| 79766 | 80675 | double r = -1.0; |
| 79767 | 80676 | if( p->op!=TK_FLOAT ) return -1; |
| 79768 | 80677 | sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8); |
| 79769 | 80678 | assert( r>=0.0 ); |
| 79770 | 80679 | if( r>1.0 ) return -1; |
| 79771 | - return (int)(r*1000.0); | |
| 80680 | + return (int)(r*134217728.0); | |
| 79772 | 80681 | } |
| 79773 | 80682 | |
| 79774 | 80683 | /* |
| 79775 | 80684 | ** This routine is callback for sqlite3WalkExpr(). |
| 79776 | 80685 | ** |
| @@ -79898,11 +80807,11 @@ | ||
| 79898 | 80807 | ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand for |
| 79899 | 80808 | ** likelihood(X,0.9375). |
| 79900 | 80809 | ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to |
| 79901 | 80810 | ** likelihood(X,0.9375). */ |
| 79902 | 80811 | /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */ |
| 79903 | - pExpr->iTable = pDef->zName[0]=='u' ? 62 : 938; | |
| 80812 | + pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120; | |
| 79904 | 80813 | } |
| 79905 | 80814 | } |
| 79906 | 80815 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 79907 | 80816 | auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); |
| 79908 | 80817 | if( auth!=SQLITE_OK ){ |
| @@ -81855,69 +82764,79 @@ | ||
| 81855 | 82764 | sqlite3DbFree(db, pList->a); |
| 81856 | 82765 | sqlite3DbFree(db, pList); |
| 81857 | 82766 | } |
| 81858 | 82767 | |
| 81859 | 82768 | /* |
| 81860 | -** These routines are Walker callbacks. Walker.u.pi is a pointer | |
| 81861 | -** to an integer. These routines are checking an expression to see | |
| 81862 | -** if it is a constant. Set *Walker.u.i to 0 if the expression is | |
| 81863 | -** not constant. | |
| 82769 | +** These routines are Walker callbacks used to check expressions to | |
| 82770 | +** see if they are "constant" for some definition of constant. The | |
| 82771 | +** Walker.eCode value determines the type of "constant" we are looking | |
| 82772 | +** for. | |
| 81864 | 82773 | ** |
| 81865 | 82774 | ** These callback routines are used to implement the following: |
| 81866 | 82775 | ** |
| 81867 | -** sqlite3ExprIsConstant() pWalker->u.i==1 | |
| 81868 | -** sqlite3ExprIsConstantNotJoin() pWalker->u.i==2 | |
| 81869 | -** sqlite3ExprIsConstantOrFunction() pWalker->u.i==3 or 4 | |
| 82776 | +** sqlite3ExprIsConstant() pWalker->eCode==1 | |
| 82777 | +** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 | |
| 82778 | +** sqlite3ExprRefOneTableOnly() pWalker->eCode==3 | |
| 82779 | +** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 | |
| 82780 | +** | |
| 82781 | +** In all cases, the callbacks set Walker.eCode=0 and abort if the expression | |
| 82782 | +** is found to not be a constant. | |
| 81870 | 82783 | ** |
| 81871 | 82784 | ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions |
| 81872 | -** in a CREATE TABLE statement. The Walker.u.i value is 4 when parsing | |
| 81873 | -** an existing schema and 3 when processing a new statement. A bound | |
| 82785 | +** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing | |
| 82786 | +** an existing schema and 4 when processing a new statement. A bound | |
| 81874 | 82787 | ** parameter raises an error for new statements, but is silently converted |
| 81875 | 82788 | ** to NULL for existing schemas. This allows sqlite_master tables that |
| 81876 | 82789 | ** contain a bound parameter because they were generated by older versions |
| 81877 | 82790 | ** of SQLite to be parsed by newer versions of SQLite without raising a |
| 81878 | 82791 | ** malformed schema error. |
| 81879 | 82792 | */ |
| 81880 | 82793 | static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ |
| 81881 | 82794 | |
| 81882 | - /* If pWalker->u.i is 2 then any term of the expression that comes from | |
| 81883 | - ** the ON or USING clauses of a join disqualifies the expression | |
| 82795 | + /* If pWalker->eCode is 2 then any term of the expression that comes from | |
| 82796 | + ** the ON or USING clauses of a left join disqualifies the expression | |
| 81884 | 82797 | ** from being considered constant. */ |
| 81885 | - if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ | |
| 81886 | - pWalker->u.i = 0; | |
| 82798 | + if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ | |
| 82799 | + pWalker->eCode = 0; | |
| 81887 | 82800 | return WRC_Abort; |
| 81888 | 82801 | } |
| 81889 | 82802 | |
| 81890 | 82803 | switch( pExpr->op ){ |
| 81891 | 82804 | /* Consider functions to be constant if all their arguments are constant |
| 81892 | - ** and either pWalker->u.i==3 or 4 or the function as the SQLITE_FUNC_CONST | |
| 81893 | - ** flag. */ | |
| 82805 | + ** and either pWalker->eCode==4 or 5 or the function has the | |
| 82806 | + ** SQLITE_FUNC_CONST flag. */ | |
| 81894 | 82807 | case TK_FUNCTION: |
| 81895 | - if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){ | |
| 82808 | + if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_Constant) ){ | |
| 81896 | 82809 | return WRC_Continue; |
| 82810 | + }else{ | |
| 82811 | + pWalker->eCode = 0; | |
| 82812 | + return WRC_Abort; | |
| 81897 | 82813 | } |
| 81898 | - /* Fall through */ | |
| 81899 | 82814 | case TK_ID: |
| 81900 | 82815 | case TK_COLUMN: |
| 81901 | 82816 | case TK_AGG_FUNCTION: |
| 81902 | 82817 | case TK_AGG_COLUMN: |
| 81903 | 82818 | testcase( pExpr->op==TK_ID ); |
| 81904 | 82819 | testcase( pExpr->op==TK_COLUMN ); |
| 81905 | 82820 | testcase( pExpr->op==TK_AGG_FUNCTION ); |
| 81906 | 82821 | testcase( pExpr->op==TK_AGG_COLUMN ); |
| 81907 | - pWalker->u.i = 0; | |
| 81908 | - return WRC_Abort; | |
| 82822 | + if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ | |
| 82823 | + return WRC_Continue; | |
| 82824 | + }else{ | |
| 82825 | + pWalker->eCode = 0; | |
| 82826 | + return WRC_Abort; | |
| 82827 | + } | |
| 81909 | 82828 | case TK_VARIABLE: |
| 81910 | - if( pWalker->u.i==4 ){ | |
| 82829 | + if( pWalker->eCode==5 ){ | |
| 81911 | 82830 | /* Silently convert bound parameters that appear inside of CREATE |
| 81912 | 82831 | ** statements into a NULL when parsing the CREATE statement text out |
| 81913 | 82832 | ** of the sqlite_master table */ |
| 81914 | 82833 | pExpr->op = TK_NULL; |
| 81915 | - }else if( pWalker->u.i==3 ){ | |
| 82834 | + }else if( pWalker->eCode==4 ){ | |
| 81916 | 82835 | /* A bound parameter in a CREATE statement that originates from |
| 81917 | 82836 | ** sqlite3_prepare() causes an error */ |
| 81918 | - pWalker->u.i = 0; | |
| 82837 | + pWalker->eCode = 0; | |
| 81919 | 82838 | return WRC_Abort; |
| 81920 | 82839 | } |
| 81921 | 82840 | /* Fall through */ |
| 81922 | 82841 | default: |
| 81923 | 82842 | testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ |
| @@ -81925,57 +82844,68 @@ | ||
| 81925 | 82844 | return WRC_Continue; |
| 81926 | 82845 | } |
| 81927 | 82846 | } |
| 81928 | 82847 | static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ |
| 81929 | 82848 | UNUSED_PARAMETER(NotUsed); |
| 81930 | - pWalker->u.i = 0; | |
| 82849 | + pWalker->eCode = 0; | |
| 81931 | 82850 | return WRC_Abort; |
| 81932 | 82851 | } |
| 81933 | -static int exprIsConst(Expr *p, int initFlag){ | |
| 82852 | +static int exprIsConst(Expr *p, int initFlag, int iCur){ | |
| 81934 | 82853 | Walker w; |
| 81935 | 82854 | memset(&w, 0, sizeof(w)); |
| 81936 | - w.u.i = initFlag; | |
| 82855 | + w.eCode = initFlag; | |
| 81937 | 82856 | w.xExprCallback = exprNodeIsConstant; |
| 81938 | 82857 | w.xSelectCallback = selectNodeIsConstant; |
| 82858 | + w.u.iCur = iCur; | |
| 81939 | 82859 | sqlite3WalkExpr(&w, p); |
| 81940 | - return w.u.i; | |
| 82860 | + return w.eCode; | |
| 81941 | 82861 | } |
| 81942 | 82862 | |
| 81943 | 82863 | /* |
| 81944 | -** Walk an expression tree. Return 1 if the expression is constant | |
| 82864 | +** Walk an expression tree. Return non-zero if the expression is constant | |
| 81945 | 82865 | ** and 0 if it involves variables or function calls. |
| 81946 | 82866 | ** |
| 81947 | 82867 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 81948 | 82868 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 81949 | 82869 | ** a constant. |
| 81950 | 82870 | */ |
| 81951 | 82871 | SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){ |
| 81952 | - return exprIsConst(p, 1); | |
| 82872 | + return exprIsConst(p, 1, 0); | |
| 81953 | 82873 | } |
| 81954 | 82874 | |
| 81955 | 82875 | /* |
| 81956 | -** Walk an expression tree. Return 1 if the expression is constant | |
| 82876 | +** Walk an expression tree. Return non-zero if the expression is constant | |
| 81957 | 82877 | ** that does no originate from the ON or USING clauses of a join. |
| 81958 | 82878 | ** Return 0 if it involves variables or function calls or terms from |
| 81959 | 82879 | ** an ON or USING clause. |
| 81960 | 82880 | */ |
| 81961 | 82881 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){ |
| 81962 | - return exprIsConst(p, 2); | |
| 82882 | + return exprIsConst(p, 2, 0); | |
| 81963 | 82883 | } |
| 81964 | 82884 | |
| 81965 | 82885 | /* |
| 81966 | -** Walk an expression tree. Return 1 if the expression is constant | |
| 82886 | +** Walk an expression tree. Return non-zero if the expression constant | |
| 82887 | +** for any single row of the table with cursor iCur. In other words, the | |
| 82888 | +** expression must not refer to any non-deterministic function nor any | |
| 82889 | +** table other than iCur. | |
| 82890 | +*/ | |
| 82891 | +SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){ | |
| 82892 | + return exprIsConst(p, 3, iCur); | |
| 82893 | +} | |
| 82894 | + | |
| 82895 | +/* | |
| 82896 | +** Walk an expression tree. Return non-zero if the expression is constant | |
| 81967 | 82897 | ** or a function call with constant arguments. Return and 0 if there |
| 81968 | 82898 | ** are any variables. |
| 81969 | 82899 | ** |
| 81970 | 82900 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 81971 | 82901 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 81972 | 82902 | ** a constant. |
| 81973 | 82903 | */ |
| 81974 | 82904 | SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ |
| 81975 | 82905 | assert( isInit==0 || isInit==1 ); |
| 81976 | - return exprIsConst(p, 3+isInit); | |
| 82906 | + return exprIsConst(p, 4+isInit, 0); | |
| 81977 | 82907 | } |
| 81978 | 82908 | |
| 81979 | 82909 | /* |
| 81980 | 82910 | ** If the expression p codes a constant integer that is small enough |
| 81981 | 82911 | ** to fit in a 32-bit integer, return 1 and put the value of the integer |
| @@ -83627,11 +84557,14 @@ | ||
| 83627 | 84557 | target |
| 83628 | 84558 | )); |
| 83629 | 84559 | |
| 83630 | 84560 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 83631 | 84561 | /* If the column has REAL affinity, it may currently be stored as an |
| 83632 | - ** integer. Use OP_RealAffinity to make sure it is really real. */ | |
| 84562 | + ** integer. Use OP_RealAffinity to make sure it is really real. | |
| 84563 | + ** | |
| 84564 | + ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to | |
| 84565 | + ** floating point when extracting it from the record. */ | |
| 83633 | 84566 | if( pExpr->iColumn>=0 |
| 83634 | 84567 | && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL |
| 83635 | 84568 | ){ |
| 83636 | 84569 | sqlite3VdbeAddOp1(v, OP_RealAffinity, target); |
| 83637 | 84570 | } |
| @@ -87279,27 +88212,32 @@ | ||
| 87279 | 88212 | aLog[i] = sqlite3LogEst(v); |
| 87280 | 88213 | #endif |
| 87281 | 88214 | if( *z==' ' ) z++; |
| 87282 | 88215 | } |
| 87283 | 88216 | #ifndef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87284 | - assert( pIndex!=0 ); | |
| 88217 | + assert( pIndex!=0 ); { | |
| 87285 | 88218 | #else |
| 87286 | - if( pIndex ) | |
| 88219 | + if( pIndex ){ | |
| 87287 | 88220 | #endif |
| 87288 | - while( z[0] ){ | |
| 87289 | - if( sqlite3_strglob("unordered*", z)==0 ){ | |
| 87290 | - pIndex->bUnordered = 1; | |
| 87291 | - }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){ | |
| 87292 | - pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3)); | |
| 87293 | - } | |
| 88221 | + pIndex->bUnordered = 0; | |
| 88222 | + pIndex->noSkipScan = 0; | |
| 88223 | + while( z[0] ){ | |
| 88224 | + if( sqlite3_strglob("unordered*", z)==0 ){ | |
| 88225 | + pIndex->bUnordered = 1; | |
| 88226 | + }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){ | |
| 88227 | + pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3)); | |
| 88228 | + }else if( sqlite3_strglob("noskipscan*", z)==0 ){ | |
| 88229 | + pIndex->noSkipScan = 1; | |
| 88230 | + } | |
| 87294 | 88231 | #ifdef SQLITE_ENABLE_COSTMULT |
| 87295 | - else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){ | |
| 87296 | - pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9)); | |
| 88232 | + else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){ | |
| 88233 | + pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9)); | |
| 88234 | + } | |
| 88235 | +#endif | |
| 88236 | + while( z[0]!=0 && z[0]!=' ' ) z++; | |
| 88237 | + while( z[0]==' ' ) z++; | |
| 87297 | 88238 | } |
| 87298 | -#endif | |
| 87299 | - while( z[0]!=0 && z[0]!=' ' ) z++; | |
| 87300 | - while( z[0]==' ' ) z++; | |
| 87301 | 88239 | } |
| 87302 | 88240 | } |
| 87303 | 88241 | |
| 87304 | 88242 | /* |
| 87305 | 88243 | ** This callback is invoked once for each index when reading the |
| @@ -87421,10 +88359,11 @@ | ||
| 87421 | 88359 | nSample--; |
| 87422 | 88360 | }else{ |
| 87423 | 88361 | nRow = pIdx->aiRowEst[0]; |
| 87424 | 88362 | nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1]; |
| 87425 | 88363 | } |
| 88364 | + pIdx->nRowEst0 = nRow; | |
| 87426 | 88365 | |
| 87427 | 88366 | /* Set nSum to the number of distinct (iCol+1) field prefixes that |
| 87428 | 88367 | ** occur in the stat4 table for this index. Set sumEq to the sum of |
| 87429 | 88368 | ** the nEq values for column iCol for the same set (adding the value |
| 87430 | 88369 | ** only once where there exist duplicate prefixes). */ |
| @@ -87682,11 +88621,11 @@ | ||
| 87682 | 88621 | } |
| 87683 | 88622 | |
| 87684 | 88623 | |
| 87685 | 88624 | /* Load the statistics from the sqlite_stat4 table. */ |
| 87686 | 88625 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87687 | - if( rc==SQLITE_OK ){ | |
| 88626 | + if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){ | |
| 87688 | 88627 | int lookasideEnabled = db->lookaside.bEnabled; |
| 87689 | 88628 | db->lookaside.bEnabled = 0; |
| 87690 | 88629 | rc = loadStat4(db, sInfo.zDatabase); |
| 87691 | 88630 | db->lookaside.bEnabled = lookasideEnabled; |
| 87692 | 88631 | } |
| @@ -88364,10 +89303,13 @@ | ||
| 88364 | 89303 | SQLITE_API int sqlite3_set_authorizer( |
| 88365 | 89304 | sqlite3 *db, |
| 88366 | 89305 | int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), |
| 88367 | 89306 | void *pArg |
| 88368 | 89307 | ){ |
| 89308 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 89309 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; | |
| 89310 | +#endif | |
| 88369 | 89311 | sqlite3_mutex_enter(db->mutex); |
| 88370 | 89312 | db->xAuth = (sqlite3_xauth)xAuth; |
| 88371 | 89313 | db->pAuthArg = pArg; |
| 88372 | 89314 | sqlite3ExpirePreparedStatements(db); |
| 88373 | 89315 | sqlite3_mutex_leave(db->mutex); |
| @@ -88858,11 +89800,15 @@ | ||
| 88858 | 89800 | ** See also sqlite3LocateTable(). |
| 88859 | 89801 | */ |
| 88860 | 89802 | SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
| 88861 | 89803 | Table *p = 0; |
| 88862 | 89804 | int i; |
| 88863 | - assert( zName!=0 ); | |
| 89805 | + | |
| 89806 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 89807 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return 0; | |
| 89808 | +#endif | |
| 89809 | + | |
| 88864 | 89810 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 88865 | 89811 | assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
| 88866 | 89812 | #if SQLITE_USER_AUTHENTICATION |
| 88867 | 89813 | /* Only the admin user is allowed to know that the sqlite_user table |
| 88868 | 89814 | ** exists */ |
| @@ -94322,12 +95268,12 @@ | ||
| 94322 | 95268 | break; |
| 94323 | 95269 | } |
| 94324 | 95270 | default: { |
| 94325 | 95271 | /* Because sqlite3_value_double() returns 0.0 if the argument is not |
| 94326 | 95272 | ** something that can be converted into a number, we have: |
| 94327 | - ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that | |
| 94328 | - ** cannot be converted to a numeric value. | |
| 95273 | + ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob | |
| 95274 | + ** that cannot be converted to a numeric value. | |
| 94329 | 95275 | */ |
| 94330 | 95276 | double rVal = sqlite3_value_double(argv[0]); |
| 94331 | 95277 | if( rVal<0 ) rVal = -rVal; |
| 94332 | 95278 | sqlite3_result_double(context, rVal); |
| 94333 | 95279 | break; |
| @@ -103881,13 +104827,16 @@ | ||
| 103881 | 104827 | Vdbe *pOld, /* VM being reprepared */ |
| 103882 | 104828 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 103883 | 104829 | const char **pzTail /* OUT: End of parsed string */ |
| 103884 | 104830 | ){ |
| 103885 | 104831 | int rc; |
| 103886 | - assert( ppStmt!=0 ); | |
| 104832 | + | |
| 104833 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 104834 | + if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; | |
| 104835 | +#endif | |
| 103887 | 104836 | *ppStmt = 0; |
| 103888 | - if( !sqlite3SafetyCheckOk(db) ){ | |
| 104837 | + if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ | |
| 103889 | 104838 | return SQLITE_MISUSE_BKPT; |
| 103890 | 104839 | } |
| 103891 | 104840 | sqlite3_mutex_enter(db->mutex); |
| 103892 | 104841 | sqlite3BtreeEnterAll(db); |
| 103893 | 104842 | rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); |
| @@ -103990,13 +104939,15 @@ | ||
| 103990 | 104939 | */ |
| 103991 | 104940 | char *zSql8; |
| 103992 | 104941 | const char *zTail8 = 0; |
| 103993 | 104942 | int rc = SQLITE_OK; |
| 103994 | 104943 | |
| 103995 | - assert( ppStmt ); | |
| 104944 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 104945 | + if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; | |
| 104946 | +#endif | |
| 103996 | 104947 | *ppStmt = 0; |
| 103997 | - if( !sqlite3SafetyCheckOk(db) ){ | |
| 104948 | + if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ | |
| 103998 | 104949 | return SQLITE_MISUSE_BKPT; |
| 103999 | 104950 | } |
| 104000 | 104951 | if( nBytes>=0 ){ |
| 104001 | 104952 | int sz; |
| 104002 | 104953 | const char *z = (const char*)zSql; |
| @@ -109705,10 +110656,13 @@ | ||
| 109705 | 110656 | char **pzErrMsg /* Write error messages here */ |
| 109706 | 110657 | ){ |
| 109707 | 110658 | int rc; |
| 109708 | 110659 | TabResult res; |
| 109709 | 110660 | |
| 110661 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 110662 | + if( pazResult==0 ) return SQLITE_MISUSE_BKPT; | |
| 110663 | +#endif | |
| 109710 | 110664 | *pazResult = 0; |
| 109711 | 110665 | if( pnColumn ) *pnColumn = 0; |
| 109712 | 110666 | if( pnRow ) *pnRow = 0; |
| 109713 | 110667 | if( pzErrMsg ) *pzErrMsg = 0; |
| 109714 | 110668 | res.zErrMsg = 0; |
| @@ -111768,11 +112722,11 @@ | ||
| 111768 | 112722 | ** Two writes per page are required in step (3) because the original |
| 111769 | 112723 | ** database content must be written into the rollback journal prior to |
| 111770 | 112724 | ** overwriting the database with the vacuumed content. |
| 111771 | 112725 | ** |
| 111772 | 112726 | ** Only 1x temporary space and only 1x writes would be required if |
| 111773 | -** the copy of step (3) were replace by deleting the original database | |
| 112727 | +** the copy of step (3) were replaced by deleting the original database | |
| 111774 | 112728 | ** and renaming the transient database as the original. But that will |
| 111775 | 112729 | ** not work if other processes are attached to the original database. |
| 111776 | 112730 | ** And a power loss in between deleting the original and renaming the |
| 111777 | 112731 | ** transient would cause the database file to appear to be deleted |
| 111778 | 112732 | ** following reboot. |
| @@ -112126,10 +113080,13 @@ | ||
| 112126 | 113080 | sqlite3 *db, /* Database in which module is registered */ |
| 112127 | 113081 | const char *zName, /* Name assigned to this module */ |
| 112128 | 113082 | const sqlite3_module *pModule, /* The definition of the module */ |
| 112129 | 113083 | void *pAux /* Context pointer for xCreate/xConnect */ |
| 112130 | 113084 | ){ |
| 113085 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 113086 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; | |
| 113087 | +#endif | |
| 112131 | 113088 | return createModule(db, zName, pModule, pAux, 0); |
| 112132 | 113089 | } |
| 112133 | 113090 | |
| 112134 | 113091 | /* |
| 112135 | 113092 | ** External API function used to create a new virtual-table module. |
| @@ -112139,10 +113096,13 @@ | ||
| 112139 | 113096 | const char *zName, /* Name assigned to this module */ |
| 112140 | 113097 | const sqlite3_module *pModule, /* The definition of the module */ |
| 112141 | 113098 | void *pAux, /* Context pointer for xCreate/xConnect */ |
| 112142 | 113099 | void (*xDestroy)(void *) /* Module destructor function */ |
| 112143 | 113100 | ){ |
| 113101 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 113102 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; | |
| 113103 | +#endif | |
| 112144 | 113104 | return createModule(db, zName, pModule, pAux, xDestroy); |
| 112145 | 113105 | } |
| 112146 | 113106 | |
| 112147 | 113107 | /* |
| 112148 | 113108 | ** Lock the virtual table so that it cannot be disconnected. |
| @@ -112371,11 +113331,16 @@ | ||
| 112371 | 113331 | pTable->tabFlags |= TF_Virtual; |
| 112372 | 113332 | pTable->nModuleArg = 0; |
| 112373 | 113333 | addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); |
| 112374 | 113334 | addModuleArgument(db, pTable, 0); |
| 112375 | 113335 | addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); |
| 112376 | - pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z); | |
| 113336 | + assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0) | |
| 113337 | + || (pParse->sNameToken.z==pName1->z && pName2->z==0) | |
| 113338 | + ); | |
| 113339 | + pParse->sNameToken.n = (int)( | |
| 113340 | + &pModuleName->z[pModuleName->n] - pParse->sNameToken.z | |
| 113341 | + ); | |
| 112377 | 113342 | |
| 112378 | 113343 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 112379 | 113344 | /* Creating a virtual table invokes the authorization callback twice. |
| 112380 | 113345 | ** The first invocation, to obtain permission to INSERT a row into the |
| 112381 | 113346 | ** sqlite_master table, has already been made by sqlite3StartTable(). |
| @@ -112743,10 +113708,13 @@ | ||
| 112743 | 113708 | |
| 112744 | 113709 | int rc = SQLITE_OK; |
| 112745 | 113710 | Table *pTab; |
| 112746 | 113711 | char *zErr = 0; |
| 112747 | 113712 | |
| 113713 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 113714 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; | |
| 113715 | +#endif | |
| 112748 | 113716 | sqlite3_mutex_enter(db->mutex); |
| 112749 | 113717 | if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){ |
| 112750 | 113718 | sqlite3Error(db, SQLITE_MISUSE); |
| 112751 | 113719 | sqlite3_mutex_leave(db->mutex); |
| 112752 | 113720 | return SQLITE_MISUSE_BKPT; |
| @@ -113099,10 +114067,13 @@ | ||
| 113099 | 114067 | */ |
| 113100 | 114068 | SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){ |
| 113101 | 114069 | static const unsigned char aMap[] = { |
| 113102 | 114070 | SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE |
| 113103 | 114071 | }; |
| 114072 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 114073 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; | |
| 114074 | +#endif | |
| 113104 | 114075 | assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 ); |
| 113105 | 114076 | assert( OE_Ignore==4 && OE_Replace==5 ); |
| 113106 | 114077 | assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 ); |
| 113107 | 114078 | return (int)aMap[db->vtabOnConflict-1]; |
| 113108 | 114079 | } |
| @@ -113114,12 +114085,14 @@ | ||
| 113114 | 114085 | */ |
| 113115 | 114086 | SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){ |
| 113116 | 114087 | va_list ap; |
| 113117 | 114088 | int rc = SQLITE_OK; |
| 113118 | 114089 | |
| 114090 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 114091 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; | |
| 114092 | +#endif | |
| 113119 | 114093 | sqlite3_mutex_enter(db->mutex); |
| 113120 | - | |
| 113121 | 114094 | va_start(ap, op); |
| 113122 | 114095 | switch( op ){ |
| 113123 | 114096 | case SQLITE_VTAB_CONSTRAINT_SUPPORT: { |
| 113124 | 114097 | VtabCtx *p = db->pVtabCtx; |
| 113125 | 114098 | if( !p ){ |
| @@ -113250,10 +114223,13 @@ | ||
| 113250 | 114223 | } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
| 113251 | 114224 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 113252 | 114225 | } u; |
| 113253 | 114226 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 113254 | 114227 | Bitmask notReady; /* FROM entries not usable at this level */ |
| 114228 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
| 114229 | + int addrVisit; /* Address at which row is visited */ | |
| 114230 | +#endif | |
| 113255 | 114231 | }; |
| 113256 | 114232 | |
| 113257 | 114233 | /* |
| 113258 | 114234 | ** Each instance of this object represents an algorithm for evaluating one |
| 113259 | 114235 | ** term of a join. Every term of the FROM clause will have at least |
| @@ -113280,11 +114256,10 @@ | ||
| 113280 | 114256 | LogEst rRun; /* Cost of running each loop */ |
| 113281 | 114257 | LogEst nOut; /* Estimated number of output rows */ |
| 113282 | 114258 | union { |
| 113283 | 114259 | struct { /* Information for internal btree tables */ |
| 113284 | 114260 | u16 nEq; /* Number of equality constraints */ |
| 113285 | - u16 nSkip; /* Number of initial index columns to skip */ | |
| 113286 | 114261 | Index *pIndex; /* Index used, or NULL */ |
| 113287 | 114262 | } btree; |
| 113288 | 114263 | struct { /* Information for virtual tables */ |
| 113289 | 114264 | int idxNum; /* Index number */ |
| 113290 | 114265 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| @@ -113293,16 +114268,17 @@ | ||
| 113293 | 114268 | char *idxStr; /* Index identifier string */ |
| 113294 | 114269 | } vtab; |
| 113295 | 114270 | } u; |
| 113296 | 114271 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 113297 | 114272 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 114273 | + u16 nSkip; /* Number of NULL aLTerm[] entries */ | |
| 113298 | 114274 | /**** whereLoopXfer() copies fields above ***********************/ |
| 113299 | 114275 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 113300 | 114276 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
| 113301 | 114277 | WhereTerm **aLTerm; /* WhereTerms used */ |
| 113302 | 114278 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
| 113303 | - WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ | |
| 114279 | + WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */ | |
| 113304 | 114280 | }; |
| 113305 | 114281 | |
| 113306 | 114282 | /* This object holds the prerequisites and the cost of running a |
| 113307 | 114283 | ** subquery on one operand of an OR operator in the WHERE clause. |
| 113308 | 114284 | ** See WhereOrSet for additional information |
| @@ -113624,10 +114600,11 @@ | ||
| 113624 | 114600 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
| 113625 | 114601 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 113626 | 114602 | #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 113627 | 114603 | #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ |
| 113628 | 114604 | #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ |
| 114605 | +#define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */ | |
| 113629 | 114606 | |
| 113630 | 114607 | /************** End of whereInt.h ********************************************/ |
| 113631 | 114608 | /************** Continuing where we left off in where.c **********************/ |
| 113632 | 114609 | |
| 113633 | 114610 | /* |
| @@ -113834,11 +114811,11 @@ | ||
| 113834 | 114811 | } |
| 113835 | 114812 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
| 113836 | 114813 | } |
| 113837 | 114814 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
| 113838 | 114815 | if( p && ExprHasProperty(p, EP_Unlikely) ){ |
| 113839 | - pTerm->truthProb = sqlite3LogEst(p->iTable) - 99; | |
| 114816 | + pTerm->truthProb = sqlite3LogEst(p->iTable) - 270; | |
| 113840 | 114817 | }else{ |
| 113841 | 114818 | pTerm->truthProb = 1; |
| 113842 | 114819 | } |
| 113843 | 114820 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
| 113844 | 114821 | pTerm->wtFlags = wtFlags; |
| @@ -114364,10 +115341,19 @@ | ||
| 114364 | 115341 | if( pDerived ){ |
| 114365 | 115342 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 114366 | 115343 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 114367 | 115344 | } |
| 114368 | 115345 | } |
| 115346 | + | |
| 115347 | +/* | |
| 115348 | +** Mark term iChild as being a child of term iParent | |
| 115349 | +*/ | |
| 115350 | +static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ | |
| 115351 | + pWC->a[iChild].iParent = iParent; | |
| 115352 | + pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; | |
| 115353 | + pWC->a[iParent].nChild++; | |
| 115354 | +} | |
| 114369 | 115355 | |
| 114370 | 115356 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 114371 | 115357 | /* |
| 114372 | 115358 | ** Analyze a term that consists of two or more OR-connected |
| 114373 | 115359 | ** subterms. So in: |
| @@ -114662,12 +115648,11 @@ | ||
| 114662 | 115648 | pNew->x.pList = pList; |
| 114663 | 115649 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114664 | 115650 | testcase( idxNew==0 ); |
| 114665 | 115651 | exprAnalyze(pSrc, pWC, idxNew); |
| 114666 | 115652 | pTerm = &pWC->a[idxTerm]; |
| 114667 | - pWC->a[idxNew].iParent = idxTerm; | |
| 114668 | - pTerm->nChild = 1; | |
| 115653 | + markTermAsChild(pWC, idxNew, idxTerm); | |
| 114669 | 115654 | }else{ |
| 114670 | 115655 | sqlite3ExprListDelete(db, pList); |
| 114671 | 115656 | } |
| 114672 | 115657 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
| 114673 | 115658 | } |
| @@ -114765,13 +115750,12 @@ | ||
| 114765 | 115750 | return; |
| 114766 | 115751 | } |
| 114767 | 115752 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114768 | 115753 | if( idxNew==0 ) return; |
| 114769 | 115754 | pNew = &pWC->a[idxNew]; |
| 114770 | - pNew->iParent = idxTerm; | |
| 115755 | + markTermAsChild(pWC, idxNew, idxTerm); | |
| 114771 | 115756 | pTerm = &pWC->a[idxTerm]; |
| 114772 | - pTerm->nChild = 1; | |
| 114773 | 115757 | pTerm->wtFlags |= TERM_COPIED; |
| 114774 | 115758 | if( pExpr->op==TK_EQ |
| 114775 | 115759 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 114776 | 115760 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 114777 | 115761 | ){ |
| @@ -114824,13 +115808,12 @@ | ||
| 114824 | 115808 | transferJoinMarkings(pNewExpr, pExpr); |
| 114825 | 115809 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114826 | 115810 | testcase( idxNew==0 ); |
| 114827 | 115811 | exprAnalyze(pSrc, pWC, idxNew); |
| 114828 | 115812 | pTerm = &pWC->a[idxTerm]; |
| 114829 | - pWC->a[idxNew].iParent = idxTerm; | |
| 115813 | + markTermAsChild(pWC, idxNew, idxTerm); | |
| 114830 | 115814 | } |
| 114831 | - pTerm->nChild = 2; | |
| 114832 | 115815 | } |
| 114833 | 115816 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
| 114834 | 115817 | |
| 114835 | 115818 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 114836 | 115819 | /* Analyze a term that is composed of two or more subterms connected by |
| @@ -114901,13 +115884,12 @@ | ||
| 114901 | 115884 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114902 | 115885 | testcase( idxNew2==0 ); |
| 114903 | 115886 | exprAnalyze(pSrc, pWC, idxNew2); |
| 114904 | 115887 | pTerm = &pWC->a[idxTerm]; |
| 114905 | 115888 | if( isComplete ){ |
| 114906 | - pWC->a[idxNew1].iParent = idxTerm; | |
| 114907 | - pWC->a[idxNew2].iParent = idxTerm; | |
| 114908 | - pTerm->nChild = 2; | |
| 115889 | + markTermAsChild(pWC, idxNew1, idxTerm); | |
| 115890 | + markTermAsChild(pWC, idxNew2, idxTerm); | |
| 114909 | 115891 | } |
| 114910 | 115892 | } |
| 114911 | 115893 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 114912 | 115894 | |
| 114913 | 115895 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| @@ -114936,13 +115918,12 @@ | ||
| 114936 | 115918 | pNewTerm = &pWC->a[idxNew]; |
| 114937 | 115919 | pNewTerm->prereqRight = prereqExpr; |
| 114938 | 115920 | pNewTerm->leftCursor = pLeft->iTable; |
| 114939 | 115921 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 114940 | 115922 | pNewTerm->eOperator = WO_MATCH; |
| 114941 | - pNewTerm->iParent = idxTerm; | |
| 115923 | + markTermAsChild(pWC, idxNew, idxTerm); | |
| 114942 | 115924 | pTerm = &pWC->a[idxTerm]; |
| 114943 | - pTerm->nChild = 1; | |
| 114944 | 115925 | pTerm->wtFlags |= TERM_COPIED; |
| 114945 | 115926 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 114946 | 115927 | } |
| 114947 | 115928 | } |
| 114948 | 115929 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| @@ -114959,11 +115940,11 @@ | ||
| 114959 | 115940 | ** the start of the loop will prevent any results from being returned. |
| 114960 | 115941 | */ |
| 114961 | 115942 | if( pExpr->op==TK_NOTNULL |
| 114962 | 115943 | && pExpr->pLeft->op==TK_COLUMN |
| 114963 | 115944 | && pExpr->pLeft->iColumn>=0 |
| 114964 | - && OptimizationEnabled(db, SQLITE_Stat3) | |
| 115945 | + && OptimizationEnabled(db, SQLITE_Stat34) | |
| 114965 | 115946 | ){ |
| 114966 | 115947 | Expr *pNewExpr; |
| 114967 | 115948 | Expr *pLeft = pExpr->pLeft; |
| 114968 | 115949 | int idxNew; |
| 114969 | 115950 | WhereTerm *pNewTerm; |
| @@ -114978,13 +115959,12 @@ | ||
| 114978 | 115959 | pNewTerm = &pWC->a[idxNew]; |
| 114979 | 115960 | pNewTerm->prereqRight = 0; |
| 114980 | 115961 | pNewTerm->leftCursor = pLeft->iTable; |
| 114981 | 115962 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 114982 | 115963 | pNewTerm->eOperator = WO_GT; |
| 114983 | - pNewTerm->iParent = idxTerm; | |
| 115964 | + markTermAsChild(pWC, idxNew, idxTerm); | |
| 114984 | 115965 | pTerm = &pWC->a[idxTerm]; |
| 114985 | - pTerm->nChild = 1; | |
| 114986 | 115966 | pTerm->wtFlags |= TERM_COPIED; |
| 114987 | 115967 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 114988 | 115968 | } |
| 114989 | 115969 | } |
| 114990 | 115970 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| @@ -115200,10 +116180,12 @@ | ||
| 115200 | 116180 | WhereLoop *pLoop; /* The Loop object */ |
| 115201 | 116181 | char *zNotUsed; /* Extra space on the end of pIdx */ |
| 115202 | 116182 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 115203 | 116183 | Bitmask extraCols; /* Bitmap of additional columns */ |
| 115204 | 116184 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
| 116185 | + Expr *pPartial = 0; /* Partial Index Expression */ | |
| 116186 | + int iContinue = 0; /* Jump here to skip excluded rows */ | |
| 115205 | 116187 | |
| 115206 | 116188 | /* Generate code to skip over the creation and initialization of the |
| 115207 | 116189 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 115208 | 116190 | v = pParse->pVdbe; |
| 115209 | 116191 | assert( v!=0 ); |
| @@ -115215,10 +116197,16 @@ | ||
| 115215 | 116197 | pTable = pSrc->pTab; |
| 115216 | 116198 | pWCEnd = &pWC->a[pWC->nTerm]; |
| 115217 | 116199 | pLoop = pLevel->pWLoop; |
| 115218 | 116200 | idxCols = 0; |
| 115219 | 116201 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 116202 | + if( pLoop->prereq==0 | |
| 116203 | + && (pTerm->wtFlags & TERM_VIRTUAL)==0 | |
| 116204 | + && sqlite3ExprIsTableConstant(pTerm->pExpr, pSrc->iCursor) ){ | |
| 116205 | + pPartial = sqlite3ExprAnd(pParse->db, pPartial, | |
| 116206 | + sqlite3ExprDup(pParse->db, pTerm->pExpr, 0)); | |
| 116207 | + } | |
| 115220 | 116208 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 115221 | 116209 | int iCol = pTerm->u.leftColumn; |
| 115222 | 116210 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
| 115223 | 116211 | testcase( iCol==BMS ); |
| 115224 | 116212 | testcase( iCol==BMS-1 ); |
| @@ -115227,11 +116215,13 @@ | ||
| 115227 | 116215 | "automatic index on %s(%s)", pTable->zName, |
| 115228 | 116216 | pTable->aCol[iCol].zName); |
| 115229 | 116217 | sentWarning = 1; |
| 115230 | 116218 | } |
| 115231 | 116219 | if( (idxCols & cMask)==0 ){ |
| 115232 | - if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return; | |
| 116220 | + if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){ | |
| 116221 | + goto end_auto_index_create; | |
| 116222 | + } | |
| 115233 | 116223 | pLoop->aLTerm[nKeyCol++] = pTerm; |
| 115234 | 116224 | idxCols |= cMask; |
| 115235 | 116225 | } |
| 115236 | 116226 | } |
| 115237 | 116227 | } |
| @@ -115247,24 +116237,23 @@ | ||
| 115247 | 116237 | ** be a covering index because the index will not be updated if the |
| 115248 | 116238 | ** original table changes and the index and table cannot both be used |
| 115249 | 116239 | ** if they go out of sync. |
| 115250 | 116240 | */ |
| 115251 | 116241 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
| 115252 | - mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; | |
| 116242 | + mxBitCol = MIN(BMS-1,pTable->nCol); | |
| 115253 | 116243 | testcase( pTable->nCol==BMS-1 ); |
| 115254 | 116244 | testcase( pTable->nCol==BMS-2 ); |
| 115255 | 116245 | for(i=0; i<mxBitCol; i++){ |
| 115256 | 116246 | if( extraCols & MASKBIT(i) ) nKeyCol++; |
| 115257 | 116247 | } |
| 115258 | 116248 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 115259 | 116249 | nKeyCol += pTable->nCol - BMS + 1; |
| 115260 | 116250 | } |
| 115261 | - pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; | |
| 115262 | 116251 | |
| 115263 | 116252 | /* Construct the Index object to describe this index */ |
| 115264 | 116253 | pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); |
| 115265 | - if( pIdx==0 ) return; | |
| 116254 | + if( pIdx==0 ) goto end_auto_index_create; | |
| 115266 | 116255 | pLoop->u.btree.pIndex = pIdx; |
| 115267 | 116256 | pIdx->zName = "auto-index"; |
| 115268 | 116257 | pIdx->pTable = pTable; |
| 115269 | 116258 | n = 0; |
| 115270 | 116259 | idxCols = 0; |
| @@ -115312,22 +116301,33 @@ | ||
| 115312 | 116301 | sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); |
| 115313 | 116302 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 115314 | 116303 | VdbeComment((v, "for %s", pTable->zName)); |
| 115315 | 116304 | |
| 115316 | 116305 | /* Fill the automatic index with content */ |
| 116306 | + sqlite3ExprCachePush(pParse); | |
| 115317 | 116307 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); |
| 116308 | + if( pPartial ){ | |
| 116309 | + iContinue = sqlite3VdbeMakeLabel(v); | |
| 116310 | + sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); | |
| 116311 | + pLoop->wsFlags |= WHERE_PARTIALIDX; | |
| 116312 | + } | |
| 115318 | 116313 | regRecord = sqlite3GetTempReg(pParse); |
| 115319 | 116314 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0); |
| 115320 | 116315 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 115321 | 116316 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 116317 | + if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); | |
| 115322 | 116318 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); |
| 115323 | 116319 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
| 115324 | 116320 | sqlite3VdbeJumpHere(v, addrTop); |
| 115325 | 116321 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 116322 | + sqlite3ExprCachePop(pParse); | |
| 115326 | 116323 | |
| 115327 | 116324 | /* Jump here when skipping the initialization */ |
| 115328 | 116325 | sqlite3VdbeJumpHere(v, addrInit); |
| 116326 | + | |
| 116327 | +end_auto_index_create: | |
| 116328 | + sqlite3ExprDelete(pParse->db, pPartial); | |
| 115329 | 116329 | } |
| 115330 | 116330 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 115331 | 116331 | |
| 115332 | 116332 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 115333 | 116333 | /* |
| @@ -115483,22 +116483,22 @@ | ||
| 115483 | 116483 | |
| 115484 | 116484 | return pParse->nErr; |
| 115485 | 116485 | } |
| 115486 | 116486 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 115487 | 116487 | |
| 115488 | - | |
| 115489 | 116488 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 115490 | 116489 | /* |
| 115491 | 116490 | ** Estimate the location of a particular key among all keys in an |
| 115492 | 116491 | ** index. Store the results in aStat as follows: |
| 115493 | 116492 | ** |
| 115494 | 116493 | ** aStat[0] Est. number of rows less than pVal |
| 115495 | 116494 | ** aStat[1] Est. number of rows equal to pVal |
| 115496 | 116495 | ** |
| 115497 | -** Return SQLITE_OK on success. | |
| 116496 | +** Return the index of the sample that is the smallest sample that | |
| 116497 | +** is greater than or equal to pRec. | |
| 115498 | 116498 | */ |
| 115499 | -static void whereKeyStats( | |
| 116499 | +static int whereKeyStats( | |
| 115500 | 116500 | Parse *pParse, /* Database connection */ |
| 115501 | 116501 | Index *pIdx, /* Index to consider domain of */ |
| 115502 | 116502 | UnpackedRecord *pRec, /* Vector of values to consider */ |
| 115503 | 116503 | int roundUp, /* Round up if true. Round down if false */ |
| 115504 | 116504 | tRowcnt *aStat /* OUT: stats written here */ |
| @@ -115576,10 +116576,11 @@ | ||
| 115576 | 116576 | }else{ |
| 115577 | 116577 | iGap = iGap/3; |
| 115578 | 116578 | } |
| 115579 | 116579 | aStat[0] = iLower + iGap; |
| 115580 | 116580 | } |
| 116581 | + return i; | |
| 115581 | 116582 | } |
| 115582 | 116583 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 115583 | 116584 | |
| 115584 | 116585 | /* |
| 115585 | 116586 | ** If it is not NULL, pTerm is a term that provides an upper or lower |
| @@ -115726,11 +116727,11 @@ | ||
| 115726 | 116727 | ** pLower pUpper |
| 115727 | 116728 | ** |
| 115728 | 116729 | ** If either of the upper or lower bound is not present, then NULL is passed in |
| 115729 | 116730 | ** place of the corresponding WhereTerm. |
| 115730 | 116731 | ** |
| 115731 | -** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index | |
| 116732 | +** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index | |
| 115732 | 116733 | ** column subject to the range constraint. Or, equivalently, the number of |
| 115733 | 116734 | ** equality constraints optimized by the proposed index scan. For example, |
| 115734 | 116735 | ** assuming index p is on t1(a, b), and the SQL query is: |
| 115735 | 116736 | ** |
| 115736 | 116737 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| @@ -115742,11 +116743,11 @@ | ||
| 115742 | 116743 | ** |
| 115743 | 116744 | ** then nEq is set to 0. |
| 115744 | 116745 | ** |
| 115745 | 116746 | ** When this function is called, *pnOut is set to the sqlite3LogEst() of the |
| 115746 | 116747 | ** number of rows that the index scan is expected to visit without |
| 115747 | -** considering the range constraints. If nEq is 0, this is the number of | |
| 116748 | +** considering the range constraints. If nEq is 0, then *pnOut is the number of | |
| 115748 | 116749 | ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) |
| 115749 | 116750 | ** to account for the range constraints pLower and pUpper. |
| 115750 | 116751 | ** |
| 115751 | 116752 | ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be |
| 115752 | 116753 | ** used, a single range inequality reduces the search space by a factor of 4. |
| @@ -115766,14 +116767,11 @@ | ||
| 115766 | 116767 | |
| 115767 | 116768 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 115768 | 116769 | Index *p = pLoop->u.btree.pIndex; |
| 115769 | 116770 | int nEq = pLoop->u.btree.nEq; |
| 115770 | 116771 | |
| 115771 | - if( p->nSample>0 | |
| 115772 | - && nEq<p->nSampleCol | |
| 115773 | - && OptimizationEnabled(pParse->db, SQLITE_Stat3) | |
| 115774 | - ){ | |
| 116772 | + if( p->nSample>0 && nEq<p->nSampleCol ){ | |
| 115775 | 116773 | if( nEq==pBuilder->nRecValid ){ |
| 115776 | 116774 | UnpackedRecord *pRec = pBuilder->pRec; |
| 115777 | 116775 | tRowcnt a[2]; |
| 115778 | 116776 | u8 aff; |
| 115779 | 116777 | |
| @@ -115785,19 +116783,23 @@ | ||
| 115785 | 116783 | ** |
| 115786 | 116784 | ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 115787 | 116785 | ** is not a simple variable or literal value), the lower bound of the |
| 115788 | 116786 | ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 115789 | 116787 | ** if $L is available, whereKeyStats() is called for both ($P) and |
| 115790 | - ** ($P:$L) and the larger of the two returned values used. | |
| 116788 | + ** ($P:$L) and the larger of the two returned values is used. | |
| 115791 | 116789 | ** |
| 115792 | 116790 | ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 115793 | 116791 | ** less than the upper bound of the range query. Where the upper bound |
| 115794 | 116792 | ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 115795 | 116793 | ** of iUpper are requested of whereKeyStats() and the smaller used. |
| 116794 | + ** | |
| 116795 | + ** The number of rows between the two bounds is then just iUpper-iLower. | |
| 115796 | 116796 | */ |
| 115797 | - tRowcnt iLower; | |
| 115798 | - tRowcnt iUpper; | |
| 116797 | + tRowcnt iLower; /* Rows less than the lower bound */ | |
| 116798 | + tRowcnt iUpper; /* Rows less than the upper bound */ | |
| 116799 | + int iLwrIdx = -2; /* aSample[] for the lower bound */ | |
| 116800 | + int iUprIdx = -1; /* aSample[] for the upper bound */ | |
| 115799 | 116801 | |
| 115800 | 116802 | if( pRec ){ |
| 115801 | 116803 | testcase( pRec->nField!=pBuilder->nRecValid ); |
| 115802 | 116804 | pRec->nField = pBuilder->nRecValid; |
| 115803 | 116805 | } |
| @@ -115807,11 +116809,11 @@ | ||
| 115807 | 116809 | aff = p->pTable->aCol[p->aiColumn[nEq]].affinity; |
| 115808 | 116810 | } |
| 115809 | 116811 | /* Determine iLower and iUpper using ($P) only. */ |
| 115810 | 116812 | if( nEq==0 ){ |
| 115811 | 116813 | iLower = 0; |
| 115812 | - iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]); | |
| 116814 | + iUpper = p->nRowEst0; | |
| 115813 | 116815 | }else{ |
| 115814 | 116816 | /* Note: this call could be optimized away - since the same values must |
| 115815 | 116817 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 115816 | 116818 | whereKeyStats(pParse, p, pRec, 0, a); |
| 115817 | 116819 | iLower = a[0]; |
| @@ -115831,11 +116833,11 @@ | ||
| 115831 | 116833 | int bOk; /* True if value is extracted from pExpr */ |
| 115832 | 116834 | Expr *pExpr = pLower->pExpr->pRight; |
| 115833 | 116835 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 115834 | 116836 | if( rc==SQLITE_OK && bOk ){ |
| 115835 | 116837 | tRowcnt iNew; |
| 115836 | - whereKeyStats(pParse, p, pRec, 0, a); | |
| 116838 | + iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a); | |
| 115837 | 116839 | iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 115838 | 116840 | if( iNew>iLower ) iLower = iNew; |
| 115839 | 116841 | nOut--; |
| 115840 | 116842 | pLower = 0; |
| 115841 | 116843 | } |
| @@ -115846,11 +116848,11 @@ | ||
| 115846 | 116848 | int bOk; /* True if value is extracted from pExpr */ |
| 115847 | 116849 | Expr *pExpr = pUpper->pExpr->pRight; |
| 115848 | 116850 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 115849 | 116851 | if( rc==SQLITE_OK && bOk ){ |
| 115850 | 116852 | tRowcnt iNew; |
| 115851 | - whereKeyStats(pParse, p, pRec, 1, a); | |
| 116853 | + iUprIdx = whereKeyStats(pParse, p, pRec, 1, a); | |
| 115852 | 116854 | iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 115853 | 116855 | if( iNew<iUpper ) iUpper = iNew; |
| 115854 | 116856 | nOut--; |
| 115855 | 116857 | pUpper = 0; |
| 115856 | 116858 | } |
| @@ -115858,10 +116860,15 @@ | ||
| 115858 | 116860 | |
| 115859 | 116861 | pBuilder->pRec = pRec; |
| 115860 | 116862 | if( rc==SQLITE_OK ){ |
| 115861 | 116863 | if( iUpper>iLower ){ |
| 115862 | 116864 | nNew = sqlite3LogEst(iUpper - iLower); |
| 116865 | + /* TUNING: If both iUpper and iLower are derived from the same | |
| 116866 | + ** sample, then assume they are 4x more selective. This brings | |
| 116867 | + ** the estimated selectivity more in line with what it would be | |
| 116868 | + ** if estimated without the use of STAT3/4 tables. */ | |
| 116869 | + if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) ); | |
| 115863 | 116870 | }else{ |
| 115864 | 116871 | nNew = 10; assert( 10==sqlite3LogEst(2) ); |
| 115865 | 116872 | } |
| 115866 | 116873 | if( nNew<nOut ){ |
| 115867 | 116874 | nOut = nNew; |
| @@ -115882,16 +116889,19 @@ | ||
| 115882 | 116889 | #endif |
| 115883 | 116890 | assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 ); |
| 115884 | 116891 | nNew = whereRangeAdjust(pLower, nOut); |
| 115885 | 116892 | nNew = whereRangeAdjust(pUpper, nNew); |
| 115886 | 116893 | |
| 115887 | - /* TUNING: If there is both an upper and lower limit, assume the range is | |
| 116894 | + /* TUNING: If there is both an upper and lower limit and neither limit | |
| 116895 | + ** has an application-defined likelihood(), assume the range is | |
| 115888 | 116896 | ** reduced by an additional 75%. This means that, by default, an open-ended |
| 115889 | 116897 | ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the |
| 115890 | 116898 | ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to |
| 115891 | 116899 | ** match 1/64 of the index. */ |
| 115892 | - if( pLower && pUpper ) nNew -= 20; | |
| 116900 | + if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){ | |
| 116901 | + nNew -= 20; | |
| 116902 | + } | |
| 115893 | 116903 | |
| 115894 | 116904 | nOut -= (pLower!=0) + (pUpper!=0); |
| 115895 | 116905 | if( nNew<10 ) nNew = 10; |
| 115896 | 116906 | if( nNew<nOut ) nOut = nNew; |
| 115897 | 116907 | #if defined(WHERETRACE_ENABLED) |
| @@ -116247,11 +117257,11 @@ | ||
| 116247 | 117257 | |
| 116248 | 117258 | /* This module is only called on query plans that use an index. */ |
| 116249 | 117259 | pLoop = pLevel->pWLoop; |
| 116250 | 117260 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 116251 | 117261 | nEq = pLoop->u.btree.nEq; |
| 116252 | - nSkip = pLoop->u.btree.nSkip; | |
| 117262 | + nSkip = pLoop->nSkip; | |
| 116253 | 117263 | pIdx = pLoop->u.btree.pIndex; |
| 116254 | 117264 | assert( pIdx!=0 ); |
| 116255 | 117265 | |
| 116256 | 117266 | /* Figure out how many memory cells we will need then allocate them. |
| 116257 | 117267 | */ |
| @@ -116361,11 +117371,11 @@ | ||
| 116361 | 117371 | ** "a=? AND b>?" |
| 116362 | 117372 | */ |
| 116363 | 117373 | static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){ |
| 116364 | 117374 | Index *pIndex = pLoop->u.btree.pIndex; |
| 116365 | 117375 | u16 nEq = pLoop->u.btree.nEq; |
| 116366 | - u16 nSkip = pLoop->u.btree.nSkip; | |
| 117376 | + u16 nSkip = pLoop->nSkip; | |
| 116367 | 117377 | int i, j; |
| 116368 | 117378 | Column *aCol = pTab->aCol; |
| 116369 | 117379 | i16 *aiColumn = pIndex->aiColumn; |
| 116370 | 117380 | |
| 116371 | 117381 | if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
| @@ -116392,23 +117402,27 @@ | ||
| 116392 | 117402 | sqlite3StrAccumAppend(pStr, ")", 1); |
| 116393 | 117403 | } |
| 116394 | 117404 | |
| 116395 | 117405 | /* |
| 116396 | 117406 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 116397 | -** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single | |
| 116398 | -** record is added to the output to describe the table scan strategy in | |
| 116399 | -** pLevel. | |
| 117407 | +** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was | |
| 117408 | +** defined at compile-time. If it is not a no-op, a single OP_Explain opcode | |
| 117409 | +** is added to the output to describe the table scan strategy in pLevel. | |
| 117410 | +** | |
| 117411 | +** If an OP_Explain opcode is added to the VM, its address is returned. | |
| 117412 | +** Otherwise, if no OP_Explain is coded, zero is returned. | |
| 116400 | 117413 | */ |
| 116401 | -static void explainOneScan( | |
| 117414 | +static int explainOneScan( | |
| 116402 | 117415 | Parse *pParse, /* Parse context */ |
| 116403 | 117416 | SrcList *pTabList, /* Table list this loop refers to */ |
| 116404 | 117417 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 116405 | 117418 | int iLevel, /* Value for "level" column of output */ |
| 116406 | 117419 | int iFrom, /* Value for "from" column of output */ |
| 116407 | 117420 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 116408 | 117421 | ){ |
| 116409 | -#ifndef SQLITE_DEBUG | |
| 117422 | + int ret = 0; | |
| 117423 | +#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) | |
| 116410 | 117424 | if( pParse->explain==2 ) |
| 116411 | 117425 | #endif |
| 116412 | 117426 | { |
| 116413 | 117427 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 116414 | 117428 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| @@ -116421,11 +117435,11 @@ | ||
| 116421 | 117435 | StrAccum str; /* EQP output string */ |
| 116422 | 117436 | char zBuf[100]; /* Initial space for EQP output string */ |
| 116423 | 117437 | |
| 116424 | 117438 | pLoop = pLevel->pWLoop; |
| 116425 | 117439 | flags = pLoop->wsFlags; |
| 116426 | - if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; | |
| 117440 | + if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0; | |
| 116427 | 117441 | |
| 116428 | 117442 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 116429 | 117443 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 116430 | 117444 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 116431 | 117445 | |
| @@ -116450,10 +117464,12 @@ | ||
| 116450 | 117464 | assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
| 116451 | 117465 | if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
| 116452 | 117466 | if( isSearch ){ |
| 116453 | 117467 | zFmt = "PRIMARY KEY"; |
| 116454 | 117468 | } |
| 117469 | + }else if( flags & WHERE_PARTIALIDX ){ | |
| 117470 | + zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; | |
| 116455 | 117471 | }else if( flags & WHERE_AUTO_INDEX ){ |
| 116456 | 117472 | zFmt = "AUTOMATIC COVERING INDEX"; |
| 116457 | 117473 | }else if( flags & WHERE_IDX_ONLY ){ |
| 116458 | 117474 | zFmt = "COVERING INDEX %s"; |
| 116459 | 117475 | }else{ |
| @@ -116491,16 +117507,49 @@ | ||
| 116491 | 117507 | }else{ |
| 116492 | 117508 | sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
| 116493 | 117509 | } |
| 116494 | 117510 | #endif |
| 116495 | 117511 | zMsg = sqlite3StrAccumFinish(&str); |
| 116496 | - sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); | |
| 117512 | + ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC); | |
| 116497 | 117513 | } |
| 117514 | + return ret; | |
| 116498 | 117515 | } |
| 116499 | 117516 | #else |
| 116500 | -# define explainOneScan(u,v,w,x,y,z) | |
| 117517 | +# define explainOneScan(u,v,w,x,y,z) 0 | |
| 116501 | 117518 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 117519 | + | |
| 117520 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
| 117521 | +/* | |
| 117522 | +** Configure the VM passed as the first argument with an | |
| 117523 | +** sqlite3_stmt_scanstatus() entry corresponding to the scan used to | |
| 117524 | +** implement level pLvl. Argument pSrclist is a pointer to the FROM | |
| 117525 | +** clause that the scan reads data from. | |
| 117526 | +** | |
| 117527 | +** If argument addrExplain is not 0, it must be the address of an | |
| 117528 | +** OP_Explain instruction that describes the same loop. | |
| 117529 | +*/ | |
| 117530 | +static void addScanStatus( | |
| 117531 | + Vdbe *v, /* Vdbe to add scanstatus entry to */ | |
| 117532 | + SrcList *pSrclist, /* FROM clause pLvl reads data from */ | |
| 117533 | + WhereLevel *pLvl, /* Level to add scanstatus() entry for */ | |
| 117534 | + int addrExplain /* Address of OP_Explain (or 0) */ | |
| 117535 | +){ | |
| 117536 | + const char *zObj = 0; | |
| 117537 | + WhereLoop *pLoop = pLvl->pWLoop; | |
| 117538 | + if( (pLoop->wsFlags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ | |
| 117539 | + zObj = pLoop->u.btree.pIndex->zName; | |
| 117540 | + }else{ | |
| 117541 | + zObj = pSrclist->a[pLvl->iFrom].zName; | |
| 117542 | + } | |
| 117543 | + sqlite3VdbeScanStatus( | |
| 117544 | + v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj | |
| 117545 | + ); | |
| 117546 | +} | |
| 117547 | +#else | |
| 117548 | +# define addScanStatus(a, b, c, d) ((void)d) | |
| 117549 | +#endif | |
| 117550 | + | |
| 116502 | 117551 | |
| 116503 | 117552 | |
| 116504 | 117553 | /* |
| 116505 | 117554 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 116506 | 117555 | ** implementation described by pWInfo. |
| @@ -116798,11 +117847,11 @@ | ||
| 116798 | 117847 | u8 bSeekPastNull = 0; /* True to seek past initial nulls */ |
| 116799 | 117848 | u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ |
| 116800 | 117849 | |
| 116801 | 117850 | pIdx = pLoop->u.btree.pIndex; |
| 116802 | 117851 | iIdxCur = pLevel->iIdxCur; |
| 116803 | - assert( nEq>=pLoop->u.btree.nSkip ); | |
| 117852 | + assert( nEq>=pLoop->nSkip ); | |
| 116804 | 117853 | |
| 116805 | 117854 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 116806 | 117855 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 116807 | 117856 | ** query, then the caller will only allow the loop to run for |
| 116808 | 117857 | ** a single iteration. This means that the first row returned |
| @@ -116815,11 +117864,11 @@ | ||
| 116815 | 117864 | || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); |
| 116816 | 117865 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 116817 | 117866 | && pWInfo->nOBSat>0 |
| 116818 | 117867 | && (pIdx->nKeyCol>nEq) |
| 116819 | 117868 | ){ |
| 116820 | - assert( pLoop->u.btree.nSkip==0 ); | |
| 117869 | + assert( pLoop->nSkip==0 ); | |
| 116821 | 117870 | bSeekPastNull = 1; |
| 116822 | 117871 | nExtraReg = 1; |
| 116823 | 117872 | } |
| 116824 | 117873 | |
| 116825 | 117874 | /* Find any inequality constraint terms for the start and end |
| @@ -117164,13 +118213,15 @@ | ||
| 117164 | 118213 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 117165 | 118214 | wctrlFlags, iCovCur); |
| 117166 | 118215 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
| 117167 | 118216 | if( pSubWInfo ){ |
| 117168 | 118217 | WhereLoop *pSubLoop; |
| 117169 | - explainOneScan( | |
| 118218 | + int addrExplain = explainOneScan( | |
| 117170 | 118219 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 117171 | 118220 | ); |
| 118221 | + addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain); | |
| 118222 | + | |
| 117172 | 118223 | /* This is the sub-WHERE clause body. First skip over |
| 117173 | 118224 | ** duplicate rows from prior sub-WHERE clauses, and record the |
| 117174 | 118225 | ** rowid (or PRIMARY KEY) for the current row so that the same |
| 117175 | 118226 | ** row will be skipped in subsequent sub-WHERE clauses. |
| 117176 | 118227 | */ |
| @@ -117296,10 +118347,14 @@ | ||
| 117296 | 118347 | VdbeCoverageIf(v, bRev==0); |
| 117297 | 118348 | VdbeCoverageIf(v, bRev!=0); |
| 117298 | 118349 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 117299 | 118350 | } |
| 117300 | 118351 | } |
| 118352 | + | |
| 118353 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
| 118354 | + pLevel->addrVisit = sqlite3VdbeCurrentAddr(v); | |
| 118355 | +#endif | |
| 117301 | 118356 | |
| 117302 | 118357 | /* Insert code to test every subexpression that can be completely |
| 117303 | 118358 | ** computed using the current set of tables. |
| 117304 | 118359 | */ |
| 117305 | 118360 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| @@ -117436,11 +118491,11 @@ | ||
| 117436 | 118491 | } |
| 117437 | 118492 | sqlite3DebugPrintf(" %-19s", z); |
| 117438 | 118493 | sqlite3_free(z); |
| 117439 | 118494 | } |
| 117440 | 118495 | if( p->wsFlags & WHERE_SKIPSCAN ){ |
| 117441 | - sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip); | |
| 118496 | + sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip); | |
| 117442 | 118497 | }else{ |
| 117443 | 118498 | sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); |
| 117444 | 118499 | } |
| 117445 | 118500 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
| 117446 | 118501 | if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ |
| @@ -117547,34 +118602,41 @@ | ||
| 117547 | 118602 | sqlite3DbFree(db, pWInfo); |
| 117548 | 118603 | } |
| 117549 | 118604 | } |
| 117550 | 118605 | |
| 117551 | 118606 | /* |
| 117552 | -** Return TRUE if both of the following are true: | |
| 118607 | +** Return TRUE if all of the following are true: | |
| 117553 | 118608 | ** |
| 117554 | 118609 | ** (1) X has the same or lower cost that Y |
| 117555 | 118610 | ** (2) X is a proper subset of Y |
| 118611 | +** (3) X skips at least as many columns as Y | |
| 117556 | 118612 | ** |
| 117557 | 118613 | ** By "proper subset" we mean that X uses fewer WHERE clause terms |
| 117558 | 118614 | ** than Y and that every WHERE clause term used by X is also used |
| 117559 | 118615 | ** by Y. |
| 117560 | 118616 | ** |
| 117561 | 118617 | ** If X is a proper subset of Y then Y is a better choice and ought |
| 117562 | 118618 | ** to have a lower cost. This routine returns TRUE when that cost |
| 117563 | -** relationship is inverted and needs to be adjusted. | |
| 118619 | +** relationship is inverted and needs to be adjusted. The third rule | |
| 118620 | +** was added because if X uses skip-scan less than Y it still might | |
| 118621 | +** deserve a lower cost even if it is a proper subset of Y. | |
| 117564 | 118622 | */ |
| 117565 | 118623 | static int whereLoopCheaperProperSubset( |
| 117566 | 118624 | const WhereLoop *pX, /* First WhereLoop to compare */ |
| 117567 | 118625 | const WhereLoop *pY /* Compare against this WhereLoop */ |
| 117568 | 118626 | ){ |
| 117569 | 118627 | int i, j; |
| 117570 | - if( pX->nLTerm >= pY->nLTerm ) return 0; /* X is not a subset of Y */ | |
| 118628 | + if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){ | |
| 118629 | + return 0; /* X is not a subset of Y */ | |
| 118630 | + } | |
| 118631 | + if( pY->nSkip > pX->nSkip ) return 0; | |
| 117571 | 118632 | if( pX->rRun >= pY->rRun ){ |
| 117572 | 118633 | if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ |
| 117573 | 118634 | if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ |
| 117574 | 118635 | } |
| 117575 | 118636 | for(i=pX->nLTerm-1; i>=0; i--){ |
| 118637 | + if( pX->aLTerm[i]==0 ) continue; | |
| 117576 | 118638 | for(j=pY->nLTerm-1; j>=0; j--){ |
| 117577 | 118639 | if( pY->aLTerm[j]==pX->aLTerm[i] ) break; |
| 117578 | 118640 | } |
| 117579 | 118641 | if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ |
| 117580 | 118642 | } |
| @@ -117592,37 +118654,28 @@ | ||
| 117592 | 118654 | ** is a proper subset. |
| 117593 | 118655 | ** |
| 117594 | 118656 | ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer |
| 117595 | 118657 | ** WHERE clause terms than Y and that every WHERE clause term used by X is |
| 117596 | 118658 | ** also used by Y. |
| 117597 | -** | |
| 117598 | -** This adjustment is omitted for SKIPSCAN loops. In a SKIPSCAN loop, the | |
| 117599 | -** WhereLoop.nLTerm field is not an accurate measure of the number of WHERE | |
| 117600 | -** clause terms covered, since some of the first nLTerm entries in aLTerm[] | |
| 117601 | -** will be NULL (because they are skipped). That makes it more difficult | |
| 117602 | -** to compare the loops. We could add extra code to do the comparison, and | |
| 117603 | -** perhaps we will someday. But SKIPSCAN is sufficiently uncommon, and this | |
| 117604 | -** adjustment is sufficient minor, that it is very difficult to construct | |
| 117605 | -** a test case where the extra code would improve the query plan. Better | |
| 117606 | -** to avoid the added complexity and just omit cost adjustments to SKIPSCAN | |
| 117607 | -** loops. | |
| 117608 | 118659 | */ |
| 117609 | 118660 | static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ |
| 117610 | 118661 | if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return; |
| 117611 | - if( (pTemplate->wsFlags & WHERE_SKIPSCAN)!=0 ) return; | |
| 117612 | 118662 | for(; p; p=p->pNextLoop){ |
| 117613 | 118663 | if( p->iTab!=pTemplate->iTab ) continue; |
| 117614 | 118664 | if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; |
| 117615 | - if( (p->wsFlags & WHERE_SKIPSCAN)!=0 ) continue; | |
| 117616 | 118665 | if( whereLoopCheaperProperSubset(p, pTemplate) ){ |
| 117617 | 118666 | /* Adjust pTemplate cost downward so that it is cheaper than its |
| 117618 | - ** subset p */ | |
| 118667 | + ** subset p. */ | |
| 118668 | + WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", | |
| 118669 | + pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1)); | |
| 117619 | 118670 | pTemplate->rRun = p->rRun; |
| 117620 | 118671 | pTemplate->nOut = p->nOut - 1; |
| 117621 | 118672 | }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ |
| 117622 | 118673 | /* Adjust pTemplate cost upward so that it is costlier than p since |
| 117623 | 118674 | ** pTemplate is a proper subset of p */ |
| 118675 | + WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", | |
| 118676 | + pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1)); | |
| 117624 | 118677 | pTemplate->rRun = p->rRun; |
| 117625 | 118678 | pTemplate->nOut = p->nOut + 1; |
| 117626 | 118679 | } |
| 117627 | 118680 | } |
| 117628 | 118681 | } |
| @@ -117663,12 +118716,13 @@ | ||
| 117663 | 118716 | ** rSetup. Call this SETUP-INVARIANT */ |
| 117664 | 118717 | assert( p->rSetup>=pTemplate->rSetup ); |
| 117665 | 118718 | |
| 117666 | 118719 | /* Any loop using an appliation-defined index (or PRIMARY KEY or |
| 117667 | 118720 | ** UNIQUE constraint) with one or more == constraints is better |
| 117668 | - ** than an automatic index. */ | |
| 118721 | + ** than an automatic index. Unless it is a skip-scan. */ | |
| 117669 | 118722 | if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 |
| 118723 | + && (pTemplate->nSkip)==0 | |
| 117670 | 118724 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 117671 | 118725 | && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0 |
| 117672 | 118726 | && (p->prereq & pTemplate->prereq)==pTemplate->prereq |
| 117673 | 118727 | ){ |
| 117674 | 118728 | break; |
| @@ -117823,25 +118877,46 @@ | ||
| 117823 | 118877 | |
| 117824 | 118878 | /* |
| 117825 | 118879 | ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 117826 | 118880 | ** WHERE clause that reference the loop but which are not used by an |
| 117827 | 118881 | ** index. |
| 118882 | +* | |
| 118883 | +** For every WHERE clause term that is not used by the index | |
| 118884 | +** and which has a truth probability assigned by one of the likelihood(), | |
| 118885 | +** likely(), or unlikely() SQL functions, reduce the estimated number | |
| 118886 | +** of output rows by the probability specified. | |
| 117828 | 118887 | ** |
| 117829 | -** In the current implementation, the first extra WHERE clause term reduces | |
| 117830 | -** the number of output rows by a factor of 10 and each additional term | |
| 117831 | -** reduces the number of output rows by sqrt(2). | |
| 118888 | +** TUNING: For every WHERE clause term that is not used by the index | |
| 118889 | +** and which does not have an assigned truth probability, heuristics | |
| 118890 | +** described below are used to try to estimate the truth probability. | |
| 118891 | +** TODO --> Perhaps this is something that could be improved by better | |
| 118892 | +** table statistics. | |
| 118893 | +** | |
| 118894 | +** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75% | |
| 118895 | +** value corresponds to -1 in LogEst notation, so this means decrement | |
| 118896 | +** the WhereLoop.nOut field for every such WHERE clause term. | |
| 118897 | +** | |
| 118898 | +** Heuristic 2: If there exists one or more WHERE clause terms of the | |
| 118899 | +** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the | |
| 118900 | +** final output row estimate is no greater than 1/4 of the total number | |
| 118901 | +** of rows in the table. In other words, assume that x==EXPR will filter | |
| 118902 | +** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the | |
| 118903 | +** "x" column is boolean or else -1 or 0 or 1 is a common default value | |
| 118904 | +** on the "x" column and so in that case only cap the output row estimate | |
| 118905 | +** at 1/2 instead of 1/4. | |
| 117832 | 118906 | */ |
| 117833 | 118907 | static void whereLoopOutputAdjust( |
| 117834 | 118908 | WhereClause *pWC, /* The WHERE clause */ |
| 117835 | 118909 | WhereLoop *pLoop, /* The loop to adjust downward */ |
| 117836 | 118910 | LogEst nRow /* Number of rows in the entire table */ |
| 117837 | 118911 | ){ |
| 117838 | 118912 | WhereTerm *pTerm, *pX; |
| 117839 | 118913 | Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); |
| 117840 | - int i, j; | |
| 117841 | - int nEq = 0; /* Number of = constraints not within likely()/unlikely() */ | |
| 118914 | + int i, j, k; | |
| 118915 | + LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */ | |
| 117842 | 118916 | |
| 118917 | + assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); | |
| 117843 | 118918 | for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ |
| 117844 | 118919 | if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; |
| 117845 | 118920 | if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; |
| 117846 | 118921 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
| 117847 | 118922 | for(j=pLoop->nLTerm-1; j>=0; j--){ |
| @@ -117850,24 +118925,30 @@ | ||
| 117850 | 118925 | if( pX==pTerm ) break; |
| 117851 | 118926 | if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; |
| 117852 | 118927 | } |
| 117853 | 118928 | if( j<0 ){ |
| 117854 | 118929 | if( pTerm->truthProb<=0 ){ |
| 118930 | + /* If a truth probability is specified using the likelihood() hints, | |
| 118931 | + ** then use the probability provided by the application. */ | |
| 117855 | 118932 | pLoop->nOut += pTerm->truthProb; |
| 117856 | 118933 | }else{ |
| 118934 | + /* In the absence of explicit truth probabilities, use heuristics to | |
| 118935 | + ** guess a reasonable truth probability. */ | |
| 117857 | 118936 | pLoop->nOut--; |
| 117858 | - if( pTerm->eOperator&WO_EQ ) nEq++; | |
| 118937 | + if( pTerm->eOperator&WO_EQ ){ | |
| 118938 | + Expr *pRight = pTerm->pExpr->pRight; | |
| 118939 | + if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){ | |
| 118940 | + k = 10; | |
| 118941 | + }else{ | |
| 118942 | + k = 20; | |
| 118943 | + } | |
| 118944 | + if( iReduce<k ) iReduce = k; | |
| 118945 | + } | |
| 117859 | 118946 | } |
| 117860 | 118947 | } |
| 117861 | 118948 | } |
| 117862 | - /* TUNING: If there is at least one equality constraint in the WHERE | |
| 117863 | - ** clause that does not have a likelihood() explicitly assigned to it | |
| 117864 | - ** then do not let the estimated number of output rows exceed half | |
| 117865 | - ** the number of rows in the table. */ | |
| 117866 | - if( nEq && pLoop->nOut>nRow-10 ){ | |
| 117867 | - pLoop->nOut = nRow - 10; | |
| 117868 | - } | |
| 118949 | + if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce; | |
| 117869 | 118950 | } |
| 117870 | 118951 | |
| 117871 | 118952 | /* |
| 117872 | 118953 | ** Adjust the cost C by the costMult facter T. This only occurs if |
| 117873 | 118954 | ** compiled with -DSQLITE_ENABLE_COSTMULT |
| @@ -117904,11 +118985,11 @@ | ||
| 117904 | 118985 | int opMask; /* Valid operators for constraints */ |
| 117905 | 118986 | WhereScan scan; /* Iterator for WHERE terms */ |
| 117906 | 118987 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 117907 | 118988 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 117908 | 118989 | u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 117909 | - u16 saved_nSkip; /* Original value of pNew->u.btree.nSkip */ | |
| 118990 | + u16 saved_nSkip; /* Original value of pNew->nSkip */ | |
| 117910 | 118991 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 117911 | 118992 | LogEst saved_nOut; /* Original value of pNew->nOut */ |
| 117912 | 118993 | int iCol; /* Index of the column in the table */ |
| 117913 | 118994 | int rc = SQLITE_OK; /* Return code */ |
| 117914 | 118995 | LogEst rSize; /* Number of rows in the table */ |
| @@ -117933,56 +119014,18 @@ | ||
| 117933 | 119014 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| 117934 | 119015 | |
| 117935 | 119016 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
| 117936 | 119017 | opMask, pProbe); |
| 117937 | 119018 | saved_nEq = pNew->u.btree.nEq; |
| 117938 | - saved_nSkip = pNew->u.btree.nSkip; | |
| 119019 | + saved_nSkip = pNew->nSkip; | |
| 117939 | 119020 | saved_nLTerm = pNew->nLTerm; |
| 117940 | 119021 | saved_wsFlags = pNew->wsFlags; |
| 117941 | 119022 | saved_prereq = pNew->prereq; |
| 117942 | 119023 | saved_nOut = pNew->nOut; |
| 117943 | 119024 | pNew->rSetup = 0; |
| 117944 | 119025 | rSize = pProbe->aiRowLogEst[0]; |
| 117945 | 119026 | rLogSize = estLog(rSize); |
| 117946 | - | |
| 117947 | - /* Consider using a skip-scan if there are no WHERE clause constraints | |
| 117948 | - ** available for the left-most terms of the index, and if the average | |
| 117949 | - ** number of repeats in the left-most terms is at least 18. | |
| 117950 | - ** | |
| 117951 | - ** The magic number 18 is selected on the basis that scanning 17 rows | |
| 117952 | - ** is almost always quicker than an index seek (even though if the index | |
| 117953 | - ** contains fewer than 2^17 rows we assume otherwise in other parts of | |
| 117954 | - ** the code). And, even if it is not, it should not be too much slower. | |
| 117955 | - ** On the other hand, the extra seeks could end up being significantly | |
| 117956 | - ** more expensive. */ | |
| 117957 | - assert( 42==sqlite3LogEst(18) ); | |
| 117958 | - if( saved_nEq==saved_nSkip | |
| 117959 | - && saved_nEq+1<pProbe->nKeyCol | |
| 117960 | - && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ | |
| 117961 | - && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK | |
| 117962 | - ){ | |
| 117963 | - LogEst nIter; | |
| 117964 | - pNew->u.btree.nEq++; | |
| 117965 | - pNew->u.btree.nSkip++; | |
| 117966 | - pNew->aLTerm[pNew->nLTerm++] = 0; | |
| 117967 | - pNew->wsFlags |= WHERE_SKIPSCAN; | |
| 117968 | - nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; | |
| 117969 | - if( pTerm ){ | |
| 117970 | - /* TUNING: When estimating skip-scan for a term that is also indexable, | |
| 117971 | - ** multiply the cost of the skip-scan by 2.0, to make it a little less | |
| 117972 | - ** desirable than the regular index lookup. */ | |
| 117973 | - nIter += 10; assert( 10==sqlite3LogEst(2) ); | |
| 117974 | - } | |
| 117975 | - pNew->nOut -= nIter; | |
| 117976 | - /* TUNING: Because uncertainties in the estimates for skip-scan queries, | |
| 117977 | - ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ | |
| 117978 | - nIter += 5; | |
| 117979 | - whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); | |
| 117980 | - pNew->nOut = saved_nOut; | |
| 117981 | - pNew->u.btree.nEq = saved_nEq; | |
| 117982 | - pNew->u.btree.nSkip = saved_nSkip; | |
| 117983 | - } | |
| 117984 | 119027 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
| 117985 | 119028 | u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ |
| 117986 | 119029 | LogEst rCostIdx; |
| 117987 | 119030 | LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ |
| 117988 | 119031 | int nIn = 0; |
| @@ -118073,11 +119116,10 @@ | ||
| 118073 | 119116 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 118074 | 119117 | tRowcnt nOut = 0; |
| 118075 | 119118 | if( nInMul==0 |
| 118076 | 119119 | && pProbe->nSample |
| 118077 | 119120 | && pNew->u.btree.nEq<=pProbe->nSampleCol |
| 118078 | - && OptimizationEnabled(db, SQLITE_Stat3) | |
| 118079 | 119121 | && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) |
| 118080 | 119122 | ){ |
| 118081 | 119123 | Expr *pExpr = pTerm->pExpr; |
| 118082 | 119124 | if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){ |
| 118083 | 119125 | testcase( eOp & WO_EQ ); |
| @@ -118141,14 +119183,49 @@ | ||
| 118141 | 119183 | pBuilder->nRecValid = nRecValid; |
| 118142 | 119184 | #endif |
| 118143 | 119185 | } |
| 118144 | 119186 | pNew->prereq = saved_prereq; |
| 118145 | 119187 | pNew->u.btree.nEq = saved_nEq; |
| 118146 | - pNew->u.btree.nSkip = saved_nSkip; | |
| 119188 | + pNew->nSkip = saved_nSkip; | |
| 118147 | 119189 | pNew->wsFlags = saved_wsFlags; |
| 118148 | 119190 | pNew->nOut = saved_nOut; |
| 118149 | 119191 | pNew->nLTerm = saved_nLTerm; |
| 119192 | + | |
| 119193 | + /* Consider using a skip-scan if there are no WHERE clause constraints | |
| 119194 | + ** available for the left-most terms of the index, and if the average | |
| 119195 | + ** number of repeats in the left-most terms is at least 18. | |
| 119196 | + ** | |
| 119197 | + ** The magic number 18 is selected on the basis that scanning 17 rows | |
| 119198 | + ** is almost always quicker than an index seek (even though if the index | |
| 119199 | + ** contains fewer than 2^17 rows we assume otherwise in other parts of | |
| 119200 | + ** the code). And, even if it is not, it should not be too much slower. | |
| 119201 | + ** On the other hand, the extra seeks could end up being significantly | |
| 119202 | + ** more expensive. */ | |
| 119203 | + assert( 42==sqlite3LogEst(18) ); | |
| 119204 | + if( saved_nEq==saved_nSkip | |
| 119205 | + && saved_nEq+1<pProbe->nKeyCol | |
| 119206 | + && pProbe->noSkipScan==0 | |
| 119207 | + && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ | |
| 119208 | + && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK | |
| 119209 | + ){ | |
| 119210 | + LogEst nIter; | |
| 119211 | + pNew->u.btree.nEq++; | |
| 119212 | + pNew->nSkip++; | |
| 119213 | + pNew->aLTerm[pNew->nLTerm++] = 0; | |
| 119214 | + pNew->wsFlags |= WHERE_SKIPSCAN; | |
| 119215 | + nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; | |
| 119216 | + pNew->nOut -= nIter; | |
| 119217 | + /* TUNING: Because uncertainties in the estimates for skip-scan queries, | |
| 119218 | + ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ | |
| 119219 | + nIter += 5; | |
| 119220 | + whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); | |
| 119221 | + pNew->nOut = saved_nOut; | |
| 119222 | + pNew->u.btree.nEq = saved_nEq; | |
| 119223 | + pNew->nSkip = saved_nSkip; | |
| 119224 | + pNew->wsFlags = saved_wsFlags; | |
| 119225 | + } | |
| 119226 | + | |
| 118150 | 119227 | return rc; |
| 118151 | 119228 | } |
| 118152 | 119229 | |
| 118153 | 119230 | /* |
| 118154 | 119231 | ** Return True if it is possible that pIndex might be useful in |
| @@ -118323,11 +119400,11 @@ | ||
| 118323 | 119400 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 118324 | 119401 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
| 118325 | 119402 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 118326 | 119403 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 118327 | 119404 | pNew->u.btree.nEq = 1; |
| 118328 | - pNew->u.btree.nSkip = 0; | |
| 119405 | + pNew->nSkip = 0; | |
| 118329 | 119406 | pNew->u.btree.pIndex = 0; |
| 118330 | 119407 | pNew->nLTerm = 1; |
| 118331 | 119408 | pNew->aLTerm[0] = pTerm; |
| 118332 | 119409 | /* TUNING: One-time cost for computing the automatic index is |
| 118333 | 119410 | ** estimated to be X*N*log2(N) where N is the number of rows in |
| @@ -118364,11 +119441,11 @@ | ||
| 118364 | 119441 | testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ |
| 118365 | 119442 | continue; /* Partial index inappropriate for this query */ |
| 118366 | 119443 | } |
| 118367 | 119444 | rSize = pProbe->aiRowLogEst[0]; |
| 118368 | 119445 | pNew->u.btree.nEq = 0; |
| 118369 | - pNew->u.btree.nSkip = 0; | |
| 119446 | + pNew->nSkip = 0; | |
| 118370 | 119447 | pNew->nLTerm = 0; |
| 118371 | 119448 | pNew->iSortIdx = 0; |
| 118372 | 119449 | pNew->rSetup = 0; |
| 118373 | 119450 | pNew->prereq = mExtra; |
| 118374 | 119451 | pNew->nOut = rSize; |
| @@ -118914,11 +119991,11 @@ | ||
| 118914 | 119991 | for(j=0; j<nColumn; j++){ |
| 118915 | 119992 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 118916 | 119993 | |
| 118917 | 119994 | /* Skip over == and IS NULL terms */ |
| 118918 | 119995 | if( j<pLoop->u.btree.nEq |
| 118919 | - && pLoop->u.btree.nSkip==0 | |
| 119996 | + && pLoop->nSkip==0 | |
| 118920 | 119997 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
| 118921 | 119998 | ){ |
| 118922 | 119999 | if( i & WO_ISNULL ){ |
| 118923 | 120000 | testcase( isOrderDistinct ); |
| 118924 | 120001 | isOrderDistinct = 0; |
| @@ -119368,11 +120445,11 @@ | ||
| 119368 | 120445 | } |
| 119369 | 120446 | } |
| 119370 | 120447 | } |
| 119371 | 120448 | |
| 119372 | 120449 | #ifdef WHERETRACE_ENABLED /* >=2 */ |
| 119373 | - if( sqlite3WhereTrace>=2 ){ | |
| 120450 | + if( sqlite3WhereTrace & 0x02 ){ | |
| 119374 | 120451 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
| 119375 | 120452 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
| 119376 | 120453 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
| 119377 | 120454 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
| 119378 | 120455 | pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); |
| @@ -119487,11 +120564,11 @@ | ||
| 119487 | 120564 | if( pItem->zIndex ) return 0; |
| 119488 | 120565 | iCur = pItem->iCursor; |
| 119489 | 120566 | pWC = &pWInfo->sWC; |
| 119490 | 120567 | pLoop = pBuilder->pNew; |
| 119491 | 120568 | pLoop->wsFlags = 0; |
| 119492 | - pLoop->u.btree.nSkip = 0; | |
| 120569 | + pLoop->nSkip = 0; | |
| 119493 | 120570 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
| 119494 | 120571 | if( pTerm ){ |
| 119495 | 120572 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 119496 | 120573 | pLoop->aLTerm[0] = pTerm; |
| 119497 | 120574 | pLoop->nLTerm = 1; |
| @@ -119499,11 +120576,10 @@ | ||
| 119499 | 120576 | /* TUNING: Cost of a rowid lookup is 10 */ |
| 119500 | 120577 | pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ |
| 119501 | 120578 | }else{ |
| 119502 | 120579 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 119503 | 120580 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
| 119504 | - assert( ArraySize(pLoop->aLTermSpace)==4 ); | |
| 119505 | 120581 | if( !IsUniqueIndex(pIdx) |
| 119506 | 120582 | || pIdx->pPartIdxWhere!=0 |
| 119507 | 120583 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
| 119508 | 120584 | ) continue; |
| 119509 | 120585 | for(j=0; j<pIdx->nKeyCol; j++){ |
| @@ -120008,22 +121084,30 @@ | ||
| 120008 | 121084 | ** loop below generates code for a single nested loop of the VM |
| 120009 | 121085 | ** program. |
| 120010 | 121086 | */ |
| 120011 | 121087 | notReady = ~(Bitmask)0; |
| 120012 | 121088 | for(ii=0; ii<nTabList; ii++){ |
| 121089 | + int addrExplain; | |
| 121090 | + int wsFlags; | |
| 120013 | 121091 | pLevel = &pWInfo->a[ii]; |
| 121092 | + wsFlags = pLevel->pWLoop->wsFlags; | |
| 120014 | 121093 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 120015 | 121094 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 120016 | 121095 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 120017 | 121096 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 120018 | 121097 | if( db->mallocFailed ) goto whereBeginError; |
| 120019 | 121098 | } |
| 120020 | 121099 | #endif |
| 120021 | - explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); | |
| 121100 | + addrExplain = explainOneScan( | |
| 121101 | + pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags | |
| 121102 | + ); | |
| 120022 | 121103 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
| 120023 | 121104 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
| 120024 | 121105 | pWInfo->iContinue = pLevel->addrCont; |
| 121106 | + if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){ | |
| 121107 | + addScanStatus(v, pTabList, pLevel, addrExplain); | |
| 121108 | + } | |
| 120025 | 121109 | } |
| 120026 | 121110 | |
| 120027 | 121111 | /* Done. */ |
| 120028 | 121112 | VdbeModuleComment((v, "Begin WHERE-core")); |
| 120029 | 121113 | return pWInfo; |
| @@ -124687,10 +125771,17 @@ | ||
| 124687 | 125771 | ** is look for a semicolon that is not part of an string or comment. |
| 124688 | 125772 | */ |
| 124689 | 125773 | SQLITE_API int sqlite3_complete(const char *zSql){ |
| 124690 | 125774 | u8 state = 0; /* Current state, using numbers defined in header comment */ |
| 124691 | 125775 | u8 token; /* Value of the next token */ |
| 125776 | + | |
| 125777 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 125778 | + if( zSql==0 ){ | |
| 125779 | + (void)SQLITE_MISUSE_BKPT; | |
| 125780 | + return 0; | |
| 125781 | + } | |
| 125782 | +#endif | |
| 124692 | 125783 | |
| 124693 | 125784 | #ifndef SQLITE_OMIT_TRIGGER |
| 124694 | 125785 | /* A complex statement machine used to detect the end of a CREATE TRIGGER |
| 124695 | 125786 | ** statement. This is the normal case. |
| 124696 | 125787 | */ |
| @@ -125285,74 +126376,106 @@ | ||
| 125285 | 126376 | |
| 125286 | 126377 | va_start(ap, op); |
| 125287 | 126378 | switch( op ){ |
| 125288 | 126379 | |
| 125289 | 126380 | /* Mutex configuration options are only available in a threadsafe |
| 125290 | - ** compile. | |
| 126381 | + ** compile. | |
| 125291 | 126382 | */ |
| 125292 | -#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 | |
| 126383 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */ | |
| 125293 | 126384 | case SQLITE_CONFIG_SINGLETHREAD: { |
| 125294 | 126385 | /* Disable all mutexing */ |
| 125295 | 126386 | sqlite3GlobalConfig.bCoreMutex = 0; |
| 125296 | 126387 | sqlite3GlobalConfig.bFullMutex = 0; |
| 125297 | 126388 | break; |
| 125298 | 126389 | } |
| 126390 | +#endif | |
| 126391 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */ | |
| 125299 | 126392 | case SQLITE_CONFIG_MULTITHREAD: { |
| 125300 | 126393 | /* Disable mutexing of database connections */ |
| 125301 | 126394 | /* Enable mutexing of core data structures */ |
| 125302 | 126395 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 125303 | 126396 | sqlite3GlobalConfig.bFullMutex = 0; |
| 125304 | 126397 | break; |
| 125305 | 126398 | } |
| 126399 | +#endif | |
| 126400 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */ | |
| 125306 | 126401 | case SQLITE_CONFIG_SERIALIZED: { |
| 125307 | 126402 | /* Enable all mutexing */ |
| 125308 | 126403 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 125309 | 126404 | sqlite3GlobalConfig.bFullMutex = 1; |
| 125310 | 126405 | break; |
| 125311 | 126406 | } |
| 126407 | +#endif | |
| 126408 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */ | |
| 125312 | 126409 | case SQLITE_CONFIG_MUTEX: { |
| 125313 | 126410 | /* Specify an alternative mutex implementation */ |
| 125314 | 126411 | sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); |
| 125315 | 126412 | break; |
| 125316 | 126413 | } |
| 126414 | +#endif | |
| 126415 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */ | |
| 125317 | 126416 | case SQLITE_CONFIG_GETMUTEX: { |
| 125318 | 126417 | /* Retrieve the current mutex implementation */ |
| 125319 | 126418 | *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; |
| 125320 | 126419 | break; |
| 125321 | 126420 | } |
| 125322 | 126421 | #endif |
| 125323 | 126422 | |
| 125324 | - | |
| 125325 | 126423 | case SQLITE_CONFIG_MALLOC: { |
| 125326 | - /* Specify an alternative malloc implementation */ | |
| 126424 | + /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a | |
| 126425 | + ** single argument which is a pointer to an instance of the | |
| 126426 | + ** sqlite3_mem_methods structure. The argument specifies alternative | |
| 126427 | + ** low-level memory allocation routines to be used in place of the memory | |
| 126428 | + ** allocation routines built into SQLite. */ | |
| 125327 | 126429 | sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); |
| 125328 | 126430 | break; |
| 125329 | 126431 | } |
| 125330 | 126432 | case SQLITE_CONFIG_GETMALLOC: { |
| 125331 | - /* Retrieve the current malloc() implementation */ | |
| 126433 | + /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a | |
| 126434 | + ** single argument which is a pointer to an instance of the | |
| 126435 | + ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is | |
| 126436 | + ** filled with the currently defined memory allocation routines. */ | |
| 125332 | 126437 | if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); |
| 125333 | 126438 | *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; |
| 125334 | 126439 | break; |
| 125335 | 126440 | } |
| 125336 | 126441 | case SQLITE_CONFIG_MEMSTATUS: { |
| 125337 | - /* Enable or disable the malloc status collection */ | |
| 126442 | + /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes | |
| 126443 | + ** single argument of type int, interpreted as a boolean, which enables | |
| 126444 | + ** or disables the collection of memory allocation statistics. */ | |
| 125338 | 126445 | sqlite3GlobalConfig.bMemstat = va_arg(ap, int); |
| 125339 | 126446 | break; |
| 125340 | 126447 | } |
| 125341 | 126448 | case SQLITE_CONFIG_SCRATCH: { |
| 125342 | - /* Designate a buffer for scratch memory space */ | |
| 126449 | + /* EVIDENCE-OF: R-08404-60887 There are three arguments to | |
| 126450 | + ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from | |
| 126451 | + ** which the scratch allocations will be drawn, the size of each scratch | |
| 126452 | + ** allocation (sz), and the maximum number of scratch allocations (N). */ | |
| 125343 | 126453 | sqlite3GlobalConfig.pScratch = va_arg(ap, void*); |
| 125344 | 126454 | sqlite3GlobalConfig.szScratch = va_arg(ap, int); |
| 125345 | 126455 | sqlite3GlobalConfig.nScratch = va_arg(ap, int); |
| 125346 | 126456 | break; |
| 125347 | 126457 | } |
| 125348 | 126458 | case SQLITE_CONFIG_PAGECACHE: { |
| 125349 | - /* Designate a buffer for page cache memory space */ | |
| 126459 | + /* EVIDENCE-OF: R-31408-40510 There are three arguments to | |
| 126460 | + ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory, the size | |
| 126461 | + ** of each page buffer (sz), and the number of pages (N). */ | |
| 125350 | 126462 | sqlite3GlobalConfig.pPage = va_arg(ap, void*); |
| 125351 | 126463 | sqlite3GlobalConfig.szPage = va_arg(ap, int); |
| 125352 | 126464 | sqlite3GlobalConfig.nPage = va_arg(ap, int); |
| 125353 | 126465 | break; |
| 126466 | + } | |
| 126467 | + case SQLITE_CONFIG_PCACHE_HDRSZ: { | |
| 126468 | + /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes | |
| 126469 | + ** a single parameter which is a pointer to an integer and writes into | |
| 126470 | + ** that integer the number of extra bytes per page required for each page | |
| 126471 | + ** in SQLITE_CONFIG_PAGECACHE. */ | |
| 126472 | + *va_arg(ap, int*) = | |
| 126473 | + sqlite3HeaderSizeBtree() + | |
| 126474 | + sqlite3HeaderSizePcache() + | |
| 126475 | + sqlite3HeaderSizePcache1(); | |
| 126476 | + break; | |
| 125354 | 126477 | } |
| 125355 | 126478 | |
| 125356 | 126479 | case SQLITE_CONFIG_PCACHE: { |
| 125357 | 126480 | /* no-op */ |
| 125358 | 126481 | break; |
| @@ -125362,25 +126485,37 @@ | ||
| 125362 | 126485 | rc = SQLITE_ERROR; |
| 125363 | 126486 | break; |
| 125364 | 126487 | } |
| 125365 | 126488 | |
| 125366 | 126489 | case SQLITE_CONFIG_PCACHE2: { |
| 125367 | - /* Specify an alternative page cache implementation */ | |
| 126490 | + /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a | |
| 126491 | + ** single argument which is a pointer to an sqlite3_pcache_methods2 | |
| 126492 | + ** object. This object specifies the interface to a custom page cache | |
| 126493 | + ** implementation. */ | |
| 125368 | 126494 | sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*); |
| 125369 | 126495 | break; |
| 125370 | 126496 | } |
| 125371 | 126497 | case SQLITE_CONFIG_GETPCACHE2: { |
| 126498 | + /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a | |
| 126499 | + ** single argument which is a pointer to an sqlite3_pcache_methods2 | |
| 126500 | + ** object. SQLite copies of the current page cache implementation into | |
| 126501 | + ** that object. */ | |
| 125372 | 126502 | if( sqlite3GlobalConfig.pcache2.xInit==0 ){ |
| 125373 | 126503 | sqlite3PCacheSetDefault(); |
| 125374 | 126504 | } |
| 125375 | 126505 | *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2; |
| 125376 | 126506 | break; |
| 125377 | 126507 | } |
| 125378 | 126508 | |
| 126509 | +/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only | |
| 126510 | +** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or | |
| 126511 | +** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */ | |
| 125379 | 126512 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 125380 | 126513 | case SQLITE_CONFIG_HEAP: { |
| 125381 | - /* Designate a buffer for heap memory space */ | |
| 126514 | + /* EVIDENCE-OF: R-19854-42126 There are three arguments to | |
| 126515 | + ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the | |
| 126516 | + ** number of bytes in the memory buffer, and the minimum allocation size. */ | |
| 125382 | 126517 | sqlite3GlobalConfig.pHeap = va_arg(ap, void*); |
| 125383 | 126518 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 125384 | 126519 | sqlite3GlobalConfig.mnReq = va_arg(ap, int); |
| 125385 | 126520 | |
| 125386 | 126521 | if( sqlite3GlobalConfig.mnReq<1 ){ |
| @@ -125389,21 +126524,23 @@ | ||
| 125389 | 126524 | /* cap min request size at 2^12 */ |
| 125390 | 126525 | sqlite3GlobalConfig.mnReq = (1<<12); |
| 125391 | 126526 | } |
| 125392 | 126527 | |
| 125393 | 126528 | if( sqlite3GlobalConfig.pHeap==0 ){ |
| 125394 | - /* If the heap pointer is NULL, then restore the malloc implementation | |
| 125395 | - ** back to NULL pointers too. This will cause the malloc to go | |
| 125396 | - ** back to its default implementation when sqlite3_initialize() is | |
| 125397 | - ** run. | |
| 126529 | + /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer) | |
| 126530 | + ** is NULL, then SQLite reverts to using its default memory allocator | |
| 126531 | + ** (the system malloc() implementation), undoing any prior invocation of | |
| 126532 | + ** SQLITE_CONFIG_MALLOC. | |
| 126533 | + ** | |
| 126534 | + ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to | |
| 126535 | + ** revert to its default implementation when sqlite3_initialize() is run | |
| 125398 | 126536 | */ |
| 125399 | 126537 | memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); |
| 125400 | 126538 | }else{ |
| 125401 | - /* The heap pointer is not NULL, then install one of the | |
| 125402 | - ** mem5.c/mem3.c methods. The enclosing #if guarantees at | |
| 125403 | - ** least one of these methods is currently enabled. | |
| 125404 | - */ | |
| 126539 | + /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the | |
| 126540 | + ** alternative memory allocator is engaged to handle all of SQLites | |
| 126541 | + ** memory allocation needs. */ | |
| 125405 | 126542 | #ifdef SQLITE_ENABLE_MEMSYS3 |
| 125406 | 126543 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); |
| 125407 | 126544 | #endif |
| 125408 | 126545 | #ifdef SQLITE_ENABLE_MEMSYS5 |
| 125409 | 126546 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); |
| @@ -125438,15 +126575,23 @@ | ||
| 125438 | 126575 | ** can be changed at start-time using the |
| 125439 | 126576 | ** sqlite3_config(SQLITE_CONFIG_URI,1) or |
| 125440 | 126577 | ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. |
| 125441 | 126578 | */ |
| 125442 | 126579 | case SQLITE_CONFIG_URI: { |
| 126580 | + /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single | |
| 126581 | + ** argument of type int. If non-zero, then URI handling is globally | |
| 126582 | + ** enabled. If the parameter is zero, then URI handling is globally | |
| 126583 | + ** disabled. */ | |
| 125443 | 126584 | sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); |
| 125444 | 126585 | break; |
| 125445 | 126586 | } |
| 125446 | 126587 | |
| 125447 | 126588 | case SQLITE_CONFIG_COVERING_INDEX_SCAN: { |
| 126589 | + /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN | |
| 126590 | + ** option takes a single integer argument which is interpreted as a | |
| 126591 | + ** boolean in order to enable or disable the use of covering indices for | |
| 126592 | + ** full table scans in the query optimizer. */ | |
| 125448 | 126593 | sqlite3GlobalConfig.bUseCis = va_arg(ap, int); |
| 125449 | 126594 | break; |
| 125450 | 126595 | } |
| 125451 | 126596 | |
| 125452 | 126597 | #ifdef SQLITE_ENABLE_SQLLOG |
| @@ -125457,24 +126602,37 @@ | ||
| 125457 | 126602 | break; |
| 125458 | 126603 | } |
| 125459 | 126604 | #endif |
| 125460 | 126605 | |
| 125461 | 126606 | case SQLITE_CONFIG_MMAP_SIZE: { |
| 126607 | + /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit | |
| 126608 | + ** integer (sqlite3_int64) values that are the default mmap size limit | |
| 126609 | + ** (the default setting for PRAGMA mmap_size) and the maximum allowed | |
| 126610 | + ** mmap size limit. */ | |
| 125462 | 126611 | sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64); |
| 125463 | 126612 | sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64); |
| 125464 | - if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){ | |
| 125465 | - mxMmap = SQLITE_MAX_MMAP_SIZE; | |
| 125466 | - } | |
| 125467 | - sqlite3GlobalConfig.mxMmap = mxMmap; | |
| 126613 | + /* EVIDENCE-OF: R-53367-43190 If either argument to this option is | |
| 126614 | + ** negative, then that argument is changed to its compile-time default. | |
| 126615 | + ** | |
| 126616 | + ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be | |
| 126617 | + ** silently truncated if necessary so that it does not exceed the | |
| 126618 | + ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE | |
| 126619 | + ** compile-time option. | |
| 126620 | + */ | |
| 126621 | + if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ) mxMmap = SQLITE_MAX_MMAP_SIZE; | |
| 125468 | 126622 | if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE; |
| 125469 | 126623 | if( szMmap>mxMmap) szMmap = mxMmap; |
| 126624 | + sqlite3GlobalConfig.mxMmap = mxMmap; | |
| 125470 | 126625 | sqlite3GlobalConfig.szMmap = szMmap; |
| 125471 | 126626 | break; |
| 125472 | 126627 | } |
| 125473 | 126628 | |
| 125474 | -#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) | |
| 126629 | +#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */ | |
| 125475 | 126630 | case SQLITE_CONFIG_WIN32_HEAPSIZE: { |
| 126631 | + /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit | |
| 126632 | + ** unsigned integer value that specifies the maximum size of the created | |
| 126633 | + ** heap. */ | |
| 125476 | 126634 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 125477 | 126635 | break; |
| 125478 | 126636 | } |
| 125479 | 126637 | #endif |
| 125480 | 126638 | |
| @@ -125554,19 +126712,29 @@ | ||
| 125554 | 126712 | |
| 125555 | 126713 | /* |
| 125556 | 126714 | ** Return the mutex associated with a database connection. |
| 125557 | 126715 | */ |
| 125558 | 126716 | SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ |
| 126717 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 126718 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 126719 | + (void)SQLITE_MISUSE_BKPT; | |
| 126720 | + return 0; | |
| 126721 | + } | |
| 126722 | +#endif | |
| 125559 | 126723 | return db->mutex; |
| 125560 | 126724 | } |
| 125561 | 126725 | |
| 125562 | 126726 | /* |
| 125563 | 126727 | ** Free up as much memory as we can from the given database |
| 125564 | 126728 | ** connection. |
| 125565 | 126729 | */ |
| 125566 | 126730 | SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){ |
| 125567 | 126731 | int i; |
| 126732 | + | |
| 126733 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 126734 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; | |
| 126735 | +#endif | |
| 125568 | 126736 | sqlite3_mutex_enter(db->mutex); |
| 125569 | 126737 | sqlite3BtreeEnterAll(db); |
| 125570 | 126738 | for(i=0; i<db->nDb; i++){ |
| 125571 | 126739 | Btree *pBt = db->aDb[i].pBt; |
| 125572 | 126740 | if( pBt ){ |
| @@ -125652,17 +126820,24 @@ | ||
| 125652 | 126820 | int nKey1, const void *pKey1, |
| 125653 | 126821 | int nKey2, const void *pKey2 |
| 125654 | 126822 | ){ |
| 125655 | 126823 | int rc, n; |
| 125656 | 126824 | n = nKey1<nKey2 ? nKey1 : nKey2; |
| 126825 | + /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares | |
| 126826 | + ** strings byte by byte using the memcmp() function from the standard C | |
| 126827 | + ** library. */ | |
| 125657 | 126828 | rc = memcmp(pKey1, pKey2, n); |
| 125658 | 126829 | if( rc==0 ){ |
| 125659 | 126830 | if( padFlag |
| 125660 | 126831 | && allSpaces(((char*)pKey1)+n, nKey1-n) |
| 125661 | 126832 | && allSpaces(((char*)pKey2)+n, nKey2-n) |
| 125662 | 126833 | ){ |
| 125663 | - /* Leave rc unchanged at 0 */ | |
| 126834 | + /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra | |
| 126835 | + ** spaces at the end of either string do not change the result. In other | |
| 126836 | + ** words, strings will compare equal to one another as long as they | |
| 126837 | + ** differ only in the number of spaces at the end. | |
| 126838 | + */ | |
| 125664 | 126839 | }else{ |
| 125665 | 126840 | rc = nKey1 - nKey2; |
| 125666 | 126841 | } |
| 125667 | 126842 | } |
| 125668 | 126843 | return rc; |
| @@ -125693,24 +126868,42 @@ | ||
| 125693 | 126868 | |
| 125694 | 126869 | /* |
| 125695 | 126870 | ** Return the ROWID of the most recent insert |
| 125696 | 126871 | */ |
| 125697 | 126872 | SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ |
| 126873 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 126874 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 126875 | + (void)SQLITE_MISUSE_BKPT; | |
| 126876 | + return 0; | |
| 126877 | + } | |
| 126878 | +#endif | |
| 125698 | 126879 | return db->lastRowid; |
| 125699 | 126880 | } |
| 125700 | 126881 | |
| 125701 | 126882 | /* |
| 125702 | 126883 | ** Return the number of changes in the most recent call to sqlite3_exec(). |
| 125703 | 126884 | */ |
| 125704 | 126885 | SQLITE_API int sqlite3_changes(sqlite3 *db){ |
| 126886 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 126887 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 126888 | + (void)SQLITE_MISUSE_BKPT; | |
| 126889 | + return 0; | |
| 126890 | + } | |
| 126891 | +#endif | |
| 125705 | 126892 | return db->nChange; |
| 125706 | 126893 | } |
| 125707 | 126894 | |
| 125708 | 126895 | /* |
| 125709 | 126896 | ** Return the number of changes since the database handle was opened. |
| 125710 | 126897 | */ |
| 125711 | 126898 | SQLITE_API int sqlite3_total_changes(sqlite3 *db){ |
| 126899 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 126900 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 126901 | + (void)SQLITE_MISUSE_BKPT; | |
| 126902 | + return 0; | |
| 126903 | + } | |
| 126904 | +#endif | |
| 125712 | 126905 | return db->nTotalChange; |
| 125713 | 126906 | } |
| 125714 | 126907 | |
| 125715 | 126908 | /* |
| 125716 | 126909 | ** Close all open savepoints. This function only manipulates fields of the |
| @@ -126255,10 +127448,13 @@ | ||
| 126255 | 127448 | SQLITE_API int sqlite3_busy_handler( |
| 126256 | 127449 | sqlite3 *db, |
| 126257 | 127450 | int (*xBusy)(void*,int), |
| 126258 | 127451 | void *pArg |
| 126259 | 127452 | ){ |
| 127453 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127454 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE; | |
| 127455 | +#endif | |
| 126260 | 127456 | sqlite3_mutex_enter(db->mutex); |
| 126261 | 127457 | db->busyHandler.xFunc = xBusy; |
| 126262 | 127458 | db->busyHandler.pArg = pArg; |
| 126263 | 127459 | db->busyHandler.nBusy = 0; |
| 126264 | 127460 | db->busyTimeout = 0; |
| @@ -126276,10 +127472,16 @@ | ||
| 126276 | 127472 | sqlite3 *db, |
| 126277 | 127473 | int nOps, |
| 126278 | 127474 | int (*xProgress)(void*), |
| 126279 | 127475 | void *pArg |
| 126280 | 127476 | ){ |
| 127477 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127478 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 127479 | + (void)SQLITE_MISUSE_BKPT; | |
| 127480 | + return; | |
| 127481 | + } | |
| 127482 | +#endif | |
| 126281 | 127483 | sqlite3_mutex_enter(db->mutex); |
| 126282 | 127484 | if( nOps>0 ){ |
| 126283 | 127485 | db->xProgress = xProgress; |
| 126284 | 127486 | db->nProgressOps = (unsigned)nOps; |
| 126285 | 127487 | db->pProgressArg = pArg; |
| @@ -126296,10 +127498,13 @@ | ||
| 126296 | 127498 | /* |
| 126297 | 127499 | ** This routine installs a default busy handler that waits for the |
| 126298 | 127500 | ** specified number of milliseconds before returning 0. |
| 126299 | 127501 | */ |
| 126300 | 127502 | SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
| 127503 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127504 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; | |
| 127505 | +#endif | |
| 126301 | 127506 | if( ms>0 ){ |
| 126302 | 127507 | sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); |
| 126303 | 127508 | db->busyTimeout = ms; |
| 126304 | 127509 | }else{ |
| 126305 | 127510 | sqlite3_busy_handler(db, 0, 0); |
| @@ -126309,10 +127514,16 @@ | ||
| 126309 | 127514 | |
| 126310 | 127515 | /* |
| 126311 | 127516 | ** Cause any pending operation to stop at its earliest opportunity. |
| 126312 | 127517 | */ |
| 126313 | 127518 | SQLITE_API void sqlite3_interrupt(sqlite3 *db){ |
| 127519 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127520 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 127521 | + (void)SQLITE_MISUSE_BKPT; | |
| 127522 | + return; | |
| 127523 | + } | |
| 127524 | +#endif | |
| 126314 | 127525 | db->u1.isInterrupted = 1; |
| 126315 | 127526 | } |
| 126316 | 127527 | |
| 126317 | 127528 | |
| 126318 | 127529 | /* |
| @@ -126446,10 +127657,16 @@ | ||
| 126446 | 127657 | void (*xFinal)(sqlite3_context*), |
| 126447 | 127658 | void (*xDestroy)(void *) |
| 126448 | 127659 | ){ |
| 126449 | 127660 | int rc = SQLITE_ERROR; |
| 126450 | 127661 | FuncDestructor *pArg = 0; |
| 127662 | + | |
| 127663 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127664 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 127665 | + return SQLITE_MISUSE_BKPT; | |
| 127666 | + } | |
| 127667 | +#endif | |
| 126451 | 127668 | sqlite3_mutex_enter(db->mutex); |
| 126452 | 127669 | if( xDestroy ){ |
| 126453 | 127670 | pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); |
| 126454 | 127671 | if( !pArg ){ |
| 126455 | 127672 | xDestroy(p); |
| @@ -126482,10 +127699,14 @@ | ||
| 126482 | 127699 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 126483 | 127700 | void (*xFinal)(sqlite3_context*) |
| 126484 | 127701 | ){ |
| 126485 | 127702 | int rc; |
| 126486 | 127703 | char *zFunc8; |
| 127704 | + | |
| 127705 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127706 | + if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT; | |
| 127707 | +#endif | |
| 126487 | 127708 | sqlite3_mutex_enter(db->mutex); |
| 126488 | 127709 | assert( !db->mallocFailed ); |
| 126489 | 127710 | zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); |
| 126490 | 127711 | rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0); |
| 126491 | 127712 | sqlite3DbFree(db, zFunc8); |
| @@ -126513,10 +127734,16 @@ | ||
| 126513 | 127734 | const char *zName, |
| 126514 | 127735 | int nArg |
| 126515 | 127736 | ){ |
| 126516 | 127737 | int nName = sqlite3Strlen30(zName); |
| 126517 | 127738 | int rc = SQLITE_OK; |
| 127739 | + | |
| 127740 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127741 | + if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){ | |
| 127742 | + return SQLITE_MISUSE_BKPT; | |
| 127743 | + } | |
| 127744 | +#endif | |
| 126518 | 127745 | sqlite3_mutex_enter(db->mutex); |
| 126519 | 127746 | if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 126520 | 127747 | rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 126521 | 127748 | 0, sqlite3InvalidFunction, 0, 0, 0); |
| 126522 | 127749 | } |
| @@ -126534,10 +127761,17 @@ | ||
| 126534 | 127761 | ** trace is a pointer to a function that is invoked at the start of each |
| 126535 | 127762 | ** SQL statement. |
| 126536 | 127763 | */ |
| 126537 | 127764 | SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ |
| 126538 | 127765 | void *pOld; |
| 127766 | + | |
| 127767 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127768 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 127769 | + (void)SQLITE_MISUSE_BKPT; | |
| 127770 | + return 0; | |
| 127771 | + } | |
| 127772 | +#endif | |
| 126539 | 127773 | sqlite3_mutex_enter(db->mutex); |
| 126540 | 127774 | pOld = db->pTraceArg; |
| 126541 | 127775 | db->xTrace = xTrace; |
| 126542 | 127776 | db->pTraceArg = pArg; |
| 126543 | 127777 | sqlite3_mutex_leave(db->mutex); |
| @@ -126555,10 +127789,17 @@ | ||
| 126555 | 127789 | sqlite3 *db, |
| 126556 | 127790 | void (*xProfile)(void*,const char*,sqlite_uint64), |
| 126557 | 127791 | void *pArg |
| 126558 | 127792 | ){ |
| 126559 | 127793 | void *pOld; |
| 127794 | + | |
| 127795 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127796 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 127797 | + (void)SQLITE_MISUSE_BKPT; | |
| 127798 | + return 0; | |
| 127799 | + } | |
| 127800 | +#endif | |
| 126560 | 127801 | sqlite3_mutex_enter(db->mutex); |
| 126561 | 127802 | pOld = db->pProfileArg; |
| 126562 | 127803 | db->xProfile = xProfile; |
| 126563 | 127804 | db->pProfileArg = pArg; |
| 126564 | 127805 | sqlite3_mutex_leave(db->mutex); |
| @@ -126575,10 +127816,17 @@ | ||
| 126575 | 127816 | sqlite3 *db, /* Attach the hook to this database */ |
| 126576 | 127817 | int (*xCallback)(void*), /* Function to invoke on each commit */ |
| 126577 | 127818 | void *pArg /* Argument to the function */ |
| 126578 | 127819 | ){ |
| 126579 | 127820 | void *pOld; |
| 127821 | + | |
| 127822 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127823 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 127824 | + (void)SQLITE_MISUSE_BKPT; | |
| 127825 | + return 0; | |
| 127826 | + } | |
| 127827 | +#endif | |
| 126580 | 127828 | sqlite3_mutex_enter(db->mutex); |
| 126581 | 127829 | pOld = db->pCommitArg; |
| 126582 | 127830 | db->xCommitCallback = xCallback; |
| 126583 | 127831 | db->pCommitArg = pArg; |
| 126584 | 127832 | sqlite3_mutex_leave(db->mutex); |
| @@ -126593,10 +127841,17 @@ | ||
| 126593 | 127841 | sqlite3 *db, /* Attach the hook to this database */ |
| 126594 | 127842 | void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), |
| 126595 | 127843 | void *pArg /* Argument to the function */ |
| 126596 | 127844 | ){ |
| 126597 | 127845 | void *pRet; |
| 127846 | + | |
| 127847 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127848 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 127849 | + (void)SQLITE_MISUSE_BKPT; | |
| 127850 | + return 0; | |
| 127851 | + } | |
| 127852 | +#endif | |
| 126598 | 127853 | sqlite3_mutex_enter(db->mutex); |
| 126599 | 127854 | pRet = db->pUpdateArg; |
| 126600 | 127855 | db->xUpdateCallback = xCallback; |
| 126601 | 127856 | db->pUpdateArg = pArg; |
| 126602 | 127857 | sqlite3_mutex_leave(db->mutex); |
| @@ -126611,10 +127866,17 @@ | ||
| 126611 | 127866 | sqlite3 *db, /* Attach the hook to this database */ |
| 126612 | 127867 | void (*xCallback)(void*), /* Callback function */ |
| 126613 | 127868 | void *pArg /* Argument to the function */ |
| 126614 | 127869 | ){ |
| 126615 | 127870 | void *pRet; |
| 127871 | + | |
| 127872 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127873 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 127874 | + (void)SQLITE_MISUSE_BKPT; | |
| 127875 | + return 0; | |
| 127876 | + } | |
| 127877 | +#endif | |
| 126616 | 127878 | sqlite3_mutex_enter(db->mutex); |
| 126617 | 127879 | pRet = db->pRollbackArg; |
| 126618 | 127880 | db->xRollbackCallback = xCallback; |
| 126619 | 127881 | db->pRollbackArg = pArg; |
| 126620 | 127882 | sqlite3_mutex_leave(db->mutex); |
| @@ -126657,10 +127919,13 @@ | ||
| 126657 | 127919 | SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ |
| 126658 | 127920 | #ifdef SQLITE_OMIT_WAL |
| 126659 | 127921 | UNUSED_PARAMETER(db); |
| 126660 | 127922 | UNUSED_PARAMETER(nFrame); |
| 126661 | 127923 | #else |
| 127924 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127925 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; | |
| 127926 | +#endif | |
| 126662 | 127927 | if( nFrame>0 ){ |
| 126663 | 127928 | sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); |
| 126664 | 127929 | }else{ |
| 126665 | 127930 | sqlite3_wal_hook(db, 0, 0); |
| 126666 | 127931 | } |
| @@ -126677,10 +127942,16 @@ | ||
| 126677 | 127942 | int(*xCallback)(void *, sqlite3*, const char*, int), |
| 126678 | 127943 | void *pArg /* First argument passed to xCallback() */ |
| 126679 | 127944 | ){ |
| 126680 | 127945 | #ifndef SQLITE_OMIT_WAL |
| 126681 | 127946 | void *pRet; |
| 127947 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127948 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 127949 | + (void)SQLITE_MISUSE_BKPT; | |
| 127950 | + return 0; | |
| 127951 | + } | |
| 127952 | +#endif | |
| 126682 | 127953 | sqlite3_mutex_enter(db->mutex); |
| 126683 | 127954 | pRet = db->pWalArg; |
| 126684 | 127955 | db->xWalCallback = xCallback; |
| 126685 | 127956 | db->pWalArg = pArg; |
| 126686 | 127957 | sqlite3_mutex_leave(db->mutex); |
| @@ -126703,10 +127974,14 @@ | ||
| 126703 | 127974 | #ifdef SQLITE_OMIT_WAL |
| 126704 | 127975 | return SQLITE_OK; |
| 126705 | 127976 | #else |
| 126706 | 127977 | int rc; /* Return code */ |
| 126707 | 127978 | int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */ |
| 127979 | + | |
| 127980 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 127981 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; | |
| 127982 | +#endif | |
| 126708 | 127983 | |
| 126709 | 127984 | /* Initialize the output variables to -1 in case an error occurs. */ |
| 126710 | 127985 | if( pnLog ) *pnLog = -1; |
| 126711 | 127986 | if( pnCkpt ) *pnCkpt = -1; |
| 126712 | 127987 | |
| @@ -127100,10 +128375,16 @@ | ||
| 127100 | 128375 | ** from forming. |
| 127101 | 128376 | */ |
| 127102 | 128377 | SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ |
| 127103 | 128378 | int oldLimit; |
| 127104 | 128379 | |
| 128380 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 128381 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 128382 | + (void)SQLITE_MISUSE_BKPT; | |
| 128383 | + return -1; | |
| 128384 | + } | |
| 128385 | +#endif | |
| 127105 | 128386 | |
| 127106 | 128387 | /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME |
| 127107 | 128388 | ** there is a hard upper bound set at compile-time by a C preprocessor |
| 127108 | 128389 | ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to |
| 127109 | 128390 | ** "_MAX_".) |
| @@ -127176,11 +128457,12 @@ | ||
| 127176 | 128457 | char c; |
| 127177 | 128458 | int nUri = sqlite3Strlen30(zUri); |
| 127178 | 128459 | |
| 127179 | 128460 | assert( *pzErrMsg==0 ); |
| 127180 | 128461 | |
| 127181 | - if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) | |
| 128462 | + if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */ | |
| 128463 | + || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */ | |
| 127182 | 128464 | && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ |
| 127183 | 128465 | ){ |
| 127184 | 128466 | char *zOpt; |
| 127185 | 128467 | int eState; /* Parser state when parsing URI */ |
| 127186 | 128468 | int iIn; /* Input character index */ |
| @@ -127385,10 +128667,13 @@ | ||
| 127385 | 128667 | int rc; /* Return code */ |
| 127386 | 128668 | int isThreadsafe; /* True for threadsafe connections */ |
| 127387 | 128669 | char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ |
| 127388 | 128670 | char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ |
| 127389 | 128671 | |
| 128672 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 128673 | + if( ppDb==0 ) return SQLITE_MISUSE_BKPT; | |
| 128674 | +#endif | |
| 127390 | 128675 | *ppDb = 0; |
| 127391 | 128676 | #ifndef SQLITE_OMIT_AUTOINIT |
| 127392 | 128677 | rc = sqlite3_initialize(); |
| 127393 | 128678 | if( rc ) return rc; |
| 127394 | 128679 | #endif |
| @@ -127499,24 +128784,28 @@ | ||
| 127499 | 128784 | #endif |
| 127500 | 128785 | |
| 127501 | 128786 | /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
| 127502 | 128787 | ** and UTF-16, so add a version for each to avoid any unnecessary |
| 127503 | 128788 | ** conversions. The only error that can occur here is a malloc() failure. |
| 128789 | + ** | |
| 128790 | + ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating | |
| 128791 | + ** functions: | |
| 127504 | 128792 | */ |
| 127505 | 128793 | createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0); |
| 127506 | 128794 | createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0); |
| 127507 | 128795 | createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0); |
| 128796 | + createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); | |
| 127508 | 128797 | createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); |
| 127509 | 128798 | if( db->mallocFailed ){ |
| 127510 | 128799 | goto opendb_out; |
| 127511 | 128800 | } |
| 128801 | + /* EVIDENCE-OF: R-08308-17224 The default collating function for all | |
| 128802 | + ** strings is BINARY. | |
| 128803 | + */ | |
| 127512 | 128804 | db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); |
| 127513 | 128805 | assert( db->pDfltColl!=0 ); |
| 127514 | 128806 | |
| 127515 | - /* Also add a UTF-8 case-insensitive collation sequence. */ | |
| 127516 | - createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); | |
| 127517 | - | |
| 127518 | 128807 | /* Parse the filename/URI argument. */ |
| 127519 | 128808 | db->openFlags = flags; |
| 127520 | 128809 | rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); |
| 127521 | 128810 | if( rc!=SQLITE_OK ){ |
| 127522 | 128811 | if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; |
| @@ -127674,17 +128963,19 @@ | ||
| 127674 | 128963 | ){ |
| 127675 | 128964 | char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
| 127676 | 128965 | sqlite3_value *pVal; |
| 127677 | 128966 | int rc; |
| 127678 | 128967 | |
| 127679 | - assert( zFilename ); | |
| 127680 | - assert( ppDb ); | |
| 128968 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 128969 | + if( ppDb==0 ) return SQLITE_MISUSE_BKPT; | |
| 128970 | +#endif | |
| 127681 | 128971 | *ppDb = 0; |
| 127682 | 128972 | #ifndef SQLITE_OMIT_AUTOINIT |
| 127683 | 128973 | rc = sqlite3_initialize(); |
| 127684 | 128974 | if( rc ) return rc; |
| 127685 | 128975 | #endif |
| 128976 | + if( zFilename==0 ) zFilename = "\000\000"; | |
| 127686 | 128977 | pVal = sqlite3ValueNew(0); |
| 127687 | 128978 | sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 127688 | 128979 | zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
| 127689 | 128980 | if( zFilename8 ){ |
| 127690 | 128981 | rc = openDatabase(zFilename8, ppDb, |
| @@ -127710,17 +129001,11 @@ | ||
| 127710 | 129001 | const char *zName, |
| 127711 | 129002 | int enc, |
| 127712 | 129003 | void* pCtx, |
| 127713 | 129004 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 127714 | 129005 | ){ |
| 127715 | - int rc; | |
| 127716 | - sqlite3_mutex_enter(db->mutex); | |
| 127717 | - assert( !db->mallocFailed ); | |
| 127718 | - rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0); | |
| 127719 | - rc = sqlite3ApiExit(db, rc); | |
| 127720 | - sqlite3_mutex_leave(db->mutex); | |
| 127721 | - return rc; | |
| 129006 | + return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0); | |
| 127722 | 129007 | } |
| 127723 | 129008 | |
| 127724 | 129009 | /* |
| 127725 | 129010 | ** Register a new collation sequence with the database handle db. |
| 127726 | 129011 | */ |
| @@ -127731,10 +129016,14 @@ | ||
| 127731 | 129016 | void* pCtx, |
| 127732 | 129017 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 127733 | 129018 | void(*xDel)(void*) |
| 127734 | 129019 | ){ |
| 127735 | 129020 | int rc; |
| 129021 | + | |
| 129022 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 129023 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; | |
| 129024 | +#endif | |
| 127736 | 129025 | sqlite3_mutex_enter(db->mutex); |
| 127737 | 129026 | assert( !db->mallocFailed ); |
| 127738 | 129027 | rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel); |
| 127739 | 129028 | rc = sqlite3ApiExit(db, rc); |
| 127740 | 129029 | sqlite3_mutex_leave(db->mutex); |
| @@ -127752,10 +129041,14 @@ | ||
| 127752 | 129041 | void* pCtx, |
| 127753 | 129042 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 127754 | 129043 | ){ |
| 127755 | 129044 | int rc = SQLITE_OK; |
| 127756 | 129045 | char *zName8; |
| 129046 | + | |
| 129047 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 129048 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; | |
| 129049 | +#endif | |
| 127757 | 129050 | sqlite3_mutex_enter(db->mutex); |
| 127758 | 129051 | assert( !db->mallocFailed ); |
| 127759 | 129052 | zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); |
| 127760 | 129053 | if( zName8 ){ |
| 127761 | 129054 | rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0); |
| @@ -127774,10 +129067,13 @@ | ||
| 127774 | 129067 | SQLITE_API int sqlite3_collation_needed( |
| 127775 | 129068 | sqlite3 *db, |
| 127776 | 129069 | void *pCollNeededArg, |
| 127777 | 129070 | void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) |
| 127778 | 129071 | ){ |
| 129072 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 129073 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; | |
| 129074 | +#endif | |
| 127779 | 129075 | sqlite3_mutex_enter(db->mutex); |
| 127780 | 129076 | db->xCollNeeded = xCollNeeded; |
| 127781 | 129077 | db->xCollNeeded16 = 0; |
| 127782 | 129078 | db->pCollNeededArg = pCollNeededArg; |
| 127783 | 129079 | sqlite3_mutex_leave(db->mutex); |
| @@ -127792,10 +129088,13 @@ | ||
| 127792 | 129088 | SQLITE_API int sqlite3_collation_needed16( |
| 127793 | 129089 | sqlite3 *db, |
| 127794 | 129090 | void *pCollNeededArg, |
| 127795 | 129091 | void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) |
| 127796 | 129092 | ){ |
| 129093 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 129094 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; | |
| 129095 | +#endif | |
| 127797 | 129096 | sqlite3_mutex_enter(db->mutex); |
| 127798 | 129097 | db->xCollNeeded = 0; |
| 127799 | 129098 | db->xCollNeeded16 = xCollNeeded16; |
| 127800 | 129099 | db->pCollNeededArg = pCollNeededArg; |
| 127801 | 129100 | sqlite3_mutex_leave(db->mutex); |
| @@ -127818,10 +129117,16 @@ | ||
| 127818 | 129117 | ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 127819 | 129118 | ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 127820 | 129119 | ** by the next COMMIT or ROLLBACK. |
| 127821 | 129120 | */ |
| 127822 | 129121 | SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){ |
| 129122 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 129123 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 129124 | + (void)SQLITE_MISUSE_BKPT; | |
| 129125 | + return 0; | |
| 129126 | + } | |
| 129127 | +#endif | |
| 127823 | 129128 | return db->autoCommit; |
| 127824 | 129129 | } |
| 127825 | 129130 | |
| 127826 | 129131 | /* |
| 127827 | 129132 | ** The following routines are substitutes for constants SQLITE_CORRUPT, |
| @@ -128000,10 +129305,13 @@ | ||
| 128000 | 129305 | |
| 128001 | 129306 | /* |
| 128002 | 129307 | ** Enable or disable the extended result codes. |
| 128003 | 129308 | */ |
| 128004 | 129309 | SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ |
| 129310 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 129311 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; | |
| 129312 | +#endif | |
| 128005 | 129313 | sqlite3_mutex_enter(db->mutex); |
| 128006 | 129314 | db->errMask = onoff ? 0xffffffff : 0xff; |
| 128007 | 129315 | sqlite3_mutex_leave(db->mutex); |
| 128008 | 129316 | return SQLITE_OK; |
| 128009 | 129317 | } |
| @@ -128013,10 +129321,13 @@ | ||
| 128013 | 129321 | */ |
| 128014 | 129322 | SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ |
| 128015 | 129323 | int rc = SQLITE_ERROR; |
| 128016 | 129324 | Btree *pBtree; |
| 128017 | 129325 | |
| 129326 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 129327 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; | |
| 129328 | +#endif | |
| 128018 | 129329 | sqlite3_mutex_enter(db->mutex); |
| 128019 | 129330 | pBtree = sqlite3DbNameToBtree(db, zDbName); |
| 128020 | 129331 | if( pBtree ){ |
| 128021 | 129332 | Pager *pPager; |
| 128022 | 129333 | sqlite3_file *fd; |
| @@ -128355,11 +129666,11 @@ | ||
| 128355 | 129666 | ** query parameter we seek. This routine returns the value of the zParam |
| 128356 | 129667 | ** parameter if it exists. If the parameter does not exist, this routine |
| 128357 | 129668 | ** returns a NULL pointer. |
| 128358 | 129669 | */ |
| 128359 | 129670 | SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){ |
| 128360 | - if( zFilename==0 ) return 0; | |
| 129671 | + if( zFilename==0 || zParam==0 ) return 0; | |
| 128361 | 129672 | zFilename += sqlite3Strlen30(zFilename) + 1; |
| 128362 | 129673 | while( zFilename[0] ){ |
| 128363 | 129674 | int x = strcmp(zFilename, zParam); |
| 128364 | 129675 | zFilename += sqlite3Strlen30(zFilename) + 1; |
| 128365 | 129676 | if( x==0 ) return zFilename; |
| @@ -128411,19 +129722,31 @@ | ||
| 128411 | 129722 | /* |
| 128412 | 129723 | ** Return the filename of the database associated with a database |
| 128413 | 129724 | ** connection. |
| 128414 | 129725 | */ |
| 128415 | 129726 | SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){ |
| 129727 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 129728 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 129729 | + (void)SQLITE_MISUSE_BKPT; | |
| 129730 | + return 0; | |
| 129731 | + } | |
| 129732 | +#endif | |
| 128416 | 129733 | Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 128417 | 129734 | return pBt ? sqlite3BtreeGetFilename(pBt) : 0; |
| 128418 | 129735 | } |
| 128419 | 129736 | |
| 128420 | 129737 | /* |
| 128421 | 129738 | ** Return 1 if database is read-only or 0 if read/write. Return -1 if |
| 128422 | 129739 | ** no such database exists. |
| 128423 | 129740 | */ |
| 128424 | 129741 | SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){ |
| 129742 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 129743 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 129744 | + (void)SQLITE_MISUSE_BKPT; | |
| 129745 | + return -1; | |
| 129746 | + } | |
| 129747 | +#endif | |
| 128425 | 129748 | Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 128426 | 129749 | return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; |
| 128427 | 129750 | } |
| 128428 | 129751 | |
| 128429 | 129752 | /************** End of main.c ************************************************/ |
| 128430 | 129753 |
| --- src/sqlite3.c | |
| +++ src/sqlite3.c | |
| @@ -1,8 +1,8 @@ | |
| 1 | /****************************************************************************** |
| 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | ** version 3.8.7.2. By combining all the individual C code files into this |
| 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | ** translation unit. |
| @@ -179,11 +179,11 @@ | |
| 179 | |
| 180 | |
| 181 | /* |
| 182 | ** These no-op macros are used in front of interfaces to mark those |
| 183 | ** interfaces as either deprecated or experimental. New applications |
| 184 | ** should not use deprecated interfaces - they are support for backwards |
| 185 | ** compatibility only. Application writers should be aware that |
| 186 | ** experimental interfaces are subject to change in point releases. |
| 187 | ** |
| 188 | ** These macros used to resolve to various kinds of compiler magic that |
| 189 | ** would generate warning messages when they were used. But that |
| @@ -229,13 +229,13 @@ | |
| 229 | ** |
| 230 | ** See also: [sqlite3_libversion()], |
| 231 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 232 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 233 | */ |
| 234 | #define SQLITE_VERSION "3.8.7.2" |
| 235 | #define SQLITE_VERSION_NUMBER 3008007 |
| 236 | #define SQLITE_SOURCE_ID "2014-11-18 20:57:56 2ab564bf9655b7c7b97ab85cafc8a48329b27f93" |
| 237 | |
| 238 | /* |
| 239 | ** CAPI3REF: Run-Time Library Version Numbers |
| 240 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 241 | ** |
| @@ -1626,29 +1626,31 @@ | |
| 1626 | ** it is not possible to set the Serialized [threading mode] and |
| 1627 | ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the |
| 1628 | ** SQLITE_CONFIG_SERIALIZED configuration option.</dd> |
| 1629 | ** |
| 1630 | ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt> |
| 1631 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1632 | ** instance of the [sqlite3_mem_methods] structure. The argument specifies |
| 1633 | ** alternative low-level memory allocation routines to be used in place of |
| 1634 | ** the memory allocation routines built into SQLite.)^ ^SQLite makes |
| 1635 | ** its own private copy of the content of the [sqlite3_mem_methods] structure |
| 1636 | ** before the [sqlite3_config()] call returns.</dd> |
| 1637 | ** |
| 1638 | ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt> |
| 1639 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1640 | ** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods] |
| 1641 | ** structure is filled with the currently defined memory allocation routines.)^ |
| 1642 | ** This option can be used to overload the default memory allocation |
| 1643 | ** routines with a wrapper that simulations memory allocation failure or |
| 1644 | ** tracks memory usage, for example. </dd> |
| 1645 | ** |
| 1646 | ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt> |
| 1647 | ** <dd> ^This option takes single argument of type int, interpreted as a |
| 1648 | ** boolean, which enables or disables the collection of memory allocation |
| 1649 | ** statistics. ^(When memory allocation statistics are disabled, the |
| 1650 | ** following SQLite interfaces become non-operational: |
| 1651 | ** <ul> |
| 1652 | ** <li> [sqlite3_memory_used()] |
| 1653 | ** <li> [sqlite3_memory_highwater()] |
| 1654 | ** <li> [sqlite3_soft_heap_limit64()] |
| @@ -1658,78 +1660,90 @@ | |
| 1658 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1659 | ** allocation statistics are disabled by default. |
| 1660 | ** </dd> |
| 1661 | ** |
| 1662 | ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt> |
| 1663 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1664 | ** scratch memory. There are three arguments: A pointer an 8-byte |
| 1665 | ** aligned memory buffer from which the scratch allocations will be |
| 1666 | ** drawn, the size of each scratch allocation (sz), |
| 1667 | ** and the maximum number of scratch allocations (N). The sz |
| 1668 | ** argument must be a multiple of 16. |
| 1669 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1670 | ** of at least sz*N bytes of memory. |
| 1671 | ** ^SQLite will use no more than two scratch buffers per thread. So |
| 1672 | ** N should be set to twice the expected maximum number of threads. |
| 1673 | ** ^SQLite will never require a scratch buffer that is more than 6 |
| 1674 | ** times the database page size. ^If SQLite needs needs additional |
| 1675 | ** scratch memory beyond what is provided by this configuration option, then |
| 1676 | ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd> |
| 1677 | ** |
| 1678 | ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1679 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1680 | ** the database page cache with the default page cache implementation. |
| 1681 | ** This configuration should not be used if an application-define page |
| 1682 | ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option. |
| 1683 | ** There are three arguments to this option: A pointer to 8-byte aligned |
| 1684 | ** memory, the size of each page buffer (sz), and the number of pages (N). |
| 1685 | ** The sz argument should be the size of the largest database page |
| 1686 | ** (a power of two between 512 and 32768) plus a little extra for each |
| 1687 | ** page header. ^The page header size is 20 to 40 bytes depending on |
| 1688 | ** the host architecture. ^It is harmless, apart from the wasted memory, |
| 1689 | ** to make sz a little too large. The first |
| 1690 | ** argument should point to an allocation of at least sz*N bytes of memory. |
| 1691 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1692 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1693 | ** page cache memory is needed beyond what is provided by this option, then |
| 1694 | ** SQLite goes to [sqlite3_malloc()] for the additional storage space. |
| 1695 | ** The pointer in the first argument must |
| 1696 | ** be aligned to an 8-byte boundary or subsequent behavior of SQLite |
| 1697 | ** will be undefined.</dd> |
| 1698 | ** |
| 1699 | ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt> |
| 1700 | ** <dd> ^This option specifies a static memory buffer that SQLite will use |
| 1701 | ** for all of its dynamic memory allocation needs beyond those provided |
| 1702 | ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE]. |
| 1703 | ** There are three arguments: An 8-byte aligned pointer to the memory, |
| 1704 | ** the number of bytes in the memory buffer, and the minimum allocation size. |
| 1705 | ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts |
| 1706 | ** to using its default memory allocator (the system malloc() implementation), |
| 1707 | ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the |
| 1708 | ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or |
| 1709 | ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory |
| 1710 | ** allocator is engaged to handle all of SQLites memory allocation needs. |
| 1711 | ** The first pointer (the memory pointer) must be aligned to an 8-byte |
| 1712 | ** boundary or subsequent behavior of SQLite will be undefined. |
| 1713 | ** The minimum allocation size is capped at 2**12. Reasonable values |
| 1714 | ** for the minimum allocation size are 2**5 through 2**8.</dd> |
| 1715 | ** |
| 1716 | ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt> |
| 1717 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1718 | ** instance of the [sqlite3_mutex_methods] structure. The argument specifies |
| 1719 | ** alternative low-level mutex routines to be used in place |
| 1720 | ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the |
| 1721 | ** content of the [sqlite3_mutex_methods] structure before the call to |
| 1722 | ** [sqlite3_config()] returns. ^If SQLite is compiled with |
| 1723 | ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then |
| 1724 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1725 | ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will |
| 1726 | ** return [SQLITE_ERROR].</dd> |
| 1727 | ** |
| 1728 | ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt> |
| 1729 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1730 | ** instance of the [sqlite3_mutex_methods] structure. The |
| 1731 | ** [sqlite3_mutex_methods] |
| 1732 | ** structure is filled with the currently defined mutex routines.)^ |
| 1733 | ** This option can be used to overload the default mutex allocation |
| 1734 | ** routines with a wrapper used to track mutex usage for performance |
| 1735 | ** profiling or testing, for example. ^If SQLite is compiled with |
| @@ -1737,28 +1751,28 @@ | |
| 1737 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1738 | ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will |
| 1739 | ** return [SQLITE_ERROR].</dd> |
| 1740 | ** |
| 1741 | ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt> |
| 1742 | ** <dd> ^(This option takes two arguments that determine the default |
| 1743 | ** memory allocation for the lookaside memory allocator on each |
| 1744 | ** [database connection]. The first argument is the |
| 1745 | ** size of each lookaside buffer slot and the second is the number of |
| 1746 | ** slots allocated to each database connection.)^ ^(This option sets the |
| 1747 | ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] |
| 1748 | ** verb to [sqlite3_db_config()] can be used to change the lookaside |
| 1749 | ** configuration on individual connections.)^ </dd> |
| 1750 | ** |
| 1751 | ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt> |
| 1752 | ** <dd> ^(This option takes a single argument which is a pointer to |
| 1753 | ** an [sqlite3_pcache_methods2] object. This object specifies the interface |
| 1754 | ** to a custom page cache implementation.)^ ^SQLite makes a copy of the |
| 1755 | ** object and uses it for page cache memory allocations.</dd> |
| 1756 | ** |
| 1757 | ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt> |
| 1758 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1759 | ** [sqlite3_pcache_methods2] object. SQLite copies of the current |
| 1760 | ** page cache implementation into that object.)^ </dd> |
| 1761 | ** |
| 1762 | ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt> |
| 1763 | ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite |
| 1764 | ** global [error log]. |
| @@ -1778,26 +1792,27 @@ | |
| 1778 | ** supplied by the application must not invoke any SQLite interface. |
| 1779 | ** In a multi-threaded application, the application-defined logger |
| 1780 | ** function must be threadsafe. </dd> |
| 1781 | ** |
| 1782 | ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI |
| 1783 | ** <dd>^(This option takes a single argument of type int. If non-zero, then |
| 1784 | ** URI handling is globally enabled. If the parameter is zero, then URI handling |
| 1785 | ** is globally disabled.)^ ^If URI handling is globally enabled, all filenames |
| 1786 | ** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or |
| 1787 | ** specified as part of [ATTACH] commands are interpreted as URIs, regardless |
| 1788 | ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database |
| 1789 | ** connection is opened. ^If it is globally disabled, filenames are |
| 1790 | ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the |
| 1791 | ** database connection is opened. ^(By default, URI handling is globally |
| 1792 | ** disabled. The default value may be changed by compiling with the |
| 1793 | ** [SQLITE_USE_URI] symbol defined.)^ |
| 1794 | ** |
| 1795 | ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN |
| 1796 | ** <dd>^This option takes a single integer argument which is interpreted as |
| 1797 | ** a boolean in order to enable or disable the use of covering indices for |
| 1798 | ** full table scans in the query optimizer. ^The default setting is determined |
| 1799 | ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on" |
| 1800 | ** if that compile-time option is omitted. |
| 1801 | ** The ability to disable the use of covering indices for full table scans |
| 1802 | ** is because some incorrectly coded legacy applications might malfunction |
| 1803 | ** when the optimization is enabled. Providing the ability to |
| @@ -1833,23 +1848,32 @@ | |
| 1833 | ** that are the default mmap size limit (the default setting for |
| 1834 | ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit. |
| 1835 | ** ^The default setting can be overridden by each database connection using |
| 1836 | ** either the [PRAGMA mmap_size] command, or by using the |
| 1837 | ** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size |
| 1838 | ** cannot be changed at run-time. Nor may the maximum allowed mmap size |
| 1839 | ** exceed the compile-time maximum mmap size set by the |
| 1840 | ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^ |
| 1841 | ** ^If either argument to this option is negative, then that argument is |
| 1842 | ** changed to its compile-time default. |
| 1843 | ** |
| 1844 | ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]] |
| 1845 | ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE |
| 1846 | ** <dd>^This option is only available if SQLite is compiled for Windows |
| 1847 | ** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined. |
| 1848 | ** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value |
| 1849 | ** that specifies the maximum size of the created heap. |
| 1850 | ** </dl> |
| 1851 | */ |
| 1852 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 1853 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 1854 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| 1855 | #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ |
| @@ -1870,10 +1894,11 @@ | |
| 1870 | #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ |
| 1871 | #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ |
| 1872 | #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ |
| 1873 | #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ |
| 1874 | #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ |
| 1875 | |
| 1876 | /* |
| 1877 | ** CAPI3REF: Database Connection Configuration Options |
| 1878 | ** |
| 1879 | ** These constants are the available integer configuration options that |
| @@ -1997,51 +2022,49 @@ | |
| 1997 | SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); |
| 1998 | |
| 1999 | /* |
| 2000 | ** CAPI3REF: Count The Number Of Rows Modified |
| 2001 | ** |
| 2002 | ** ^This function returns the number of database rows that were changed |
| 2003 | ** or inserted or deleted by the most recently completed SQL statement |
| 2004 | ** on the [database connection] specified by the first parameter. |
| 2005 | ** ^(Only changes that are directly specified by the [INSERT], [UPDATE], |
| 2006 | ** or [DELETE] statement are counted. Auxiliary changes caused by |
| 2007 | ** triggers or [foreign key actions] are not counted.)^ Use the |
| 2008 | ** [sqlite3_total_changes()] function to find the total number of changes |
| 2009 | ** including changes caused by triggers and foreign key actions. |
| 2010 | ** |
| 2011 | ** ^Changes to a view that are simulated by an [INSTEAD OF trigger] |
| 2012 | ** are not counted. Only real table changes are counted. |
| 2013 | ** |
| 2014 | ** ^(A "row change" is a change to a single row of a single table |
| 2015 | ** caused by an INSERT, DELETE, or UPDATE statement. Rows that |
| 2016 | ** are changed as side effects of [REPLACE] constraint resolution, |
| 2017 | ** rollback, ABORT processing, [DROP TABLE], or by any other |
| 2018 | ** mechanisms do not count as direct row changes.)^ |
| 2019 | ** |
| 2020 | ** A "trigger context" is a scope of execution that begins and |
| 2021 | ** ends with the script of a [CREATE TRIGGER | trigger]. |
| 2022 | ** Most SQL statements are |
| 2023 | ** evaluated outside of any trigger. This is the "top level" |
| 2024 | ** trigger context. If a trigger fires from the top level, a |
| 2025 | ** new trigger context is entered for the duration of that one |
| 2026 | ** trigger. Subtriggers create subcontexts for their duration. |
| 2027 | ** |
| 2028 | ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does |
| 2029 | ** not create a new trigger context. |
| 2030 | ** |
| 2031 | ** ^This function returns the number of direct row changes in the |
| 2032 | ** most recent INSERT, UPDATE, or DELETE statement within the same |
| 2033 | ** trigger context. |
| 2034 | ** |
| 2035 | ** ^Thus, when called from the top level, this function returns the |
| 2036 | ** number of changes in the most recent INSERT, UPDATE, or DELETE |
| 2037 | ** that also occurred at the top level. ^(Within the body of a trigger, |
| 2038 | ** the sqlite3_changes() interface can be called to find the number of |
| 2039 | ** changes in the most recently completed INSERT, UPDATE, or DELETE |
| 2040 | ** statement within the body of the same trigger. |
| 2041 | ** However, the number returned does not include changes |
| 2042 | ** caused by subtriggers since those have their own context.)^ |
| 2043 | ** |
| 2044 | ** See also the [sqlite3_total_changes()] interface, the |
| 2045 | ** [count_changes pragma], and the [changes() SQL function]. |
| 2046 | ** |
| 2047 | ** If a separate thread makes changes on the same database connection |
| @@ -2051,24 +2074,21 @@ | |
| 2051 | SQLITE_API int sqlite3_changes(sqlite3*); |
| 2052 | |
| 2053 | /* |
| 2054 | ** CAPI3REF: Total Number Of Rows Modified |
| 2055 | ** |
| 2056 | ** ^This function returns the number of row changes caused by [INSERT], |
| 2057 | ** [UPDATE] or [DELETE] statements since the [database connection] was opened. |
| 2058 | ** ^(The count returned by sqlite3_total_changes() includes all changes |
| 2059 | ** from all [CREATE TRIGGER | trigger] contexts and changes made by |
| 2060 | ** [foreign key actions]. However, |
| 2061 | ** the count does not include changes used to implement [REPLACE] constraints, |
| 2062 | ** do rollbacks or ABORT processing, or [DROP TABLE] processing. The |
| 2063 | ** count does not include rows of views that fire an [INSTEAD OF trigger], |
| 2064 | ** though if the INSTEAD OF trigger makes changes of its own, those changes |
| 2065 | ** are counted.)^ |
| 2066 | ** ^The sqlite3_total_changes() function counts the changes as soon as |
| 2067 | ** the statement that makes them is completed (when the statement handle |
| 2068 | ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]). |
| 2069 | ** |
| 2070 | ** See also the [sqlite3_changes()] interface, the |
| 2071 | ** [count_changes pragma], and the [total_changes() SQL function]. |
| 2072 | ** |
| 2073 | ** If a separate thread makes changes on the same database connection |
| 2074 | ** while [sqlite3_total_changes()] is running then the value |
| @@ -2542,17 +2562,18 @@ | |
| 2542 | ** already uses the largest possible [ROWID]. The PRNG is also used for |
| 2543 | ** the build-in random() and randomblob() SQL functions. This interface allows |
| 2544 | ** applications to access the same PRNG for other purposes. |
| 2545 | ** |
| 2546 | ** ^A call to this routine stores N bytes of randomness into buffer P. |
| 2547 | ** ^If N is less than one, then P can be a NULL pointer. |
| 2548 | ** |
| 2549 | ** ^If this routine has not been previously called or if the previous |
| 2550 | ** call had N less than one, then the PRNG is seeded using randomness |
| 2551 | ** obtained from the xRandomness method of the default [sqlite3_vfs] object. |
| 2552 | ** ^If the previous call to this routine had an N of 1 or more then |
| 2553 | ** the pseudo-randomness is generated |
| 2554 | ** internally and without recourse to the [sqlite3_vfs] xRandomness |
| 2555 | ** method. |
| 2556 | */ |
| 2557 | SQLITE_API void sqlite3_randomness(int N, void *P); |
| 2558 | |
| @@ -4270,13 +4291,13 @@ | |
| 4270 | ** CAPI3REF: Text Encodings |
| 4271 | ** |
| 4272 | ** These constant define integer codes that represent the various |
| 4273 | ** text encodings supported by SQLite. |
| 4274 | */ |
| 4275 | #define SQLITE_UTF8 1 |
| 4276 | #define SQLITE_UTF16LE 2 |
| 4277 | #define SQLITE_UTF16BE 3 |
| 4278 | #define SQLITE_UTF16 4 /* Use native byte order */ |
| 4279 | #define SQLITE_ANY 5 /* Deprecated */ |
| 4280 | #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ |
| 4281 | |
| 4282 | /* |
| @@ -5762,31 +5783,47 @@ | |
| 5762 | ** in other words, the same BLOB that would be selected by: |
| 5763 | ** |
| 5764 | ** <pre> |
| 5765 | ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow; |
| 5766 | ** </pre>)^ |
| 5767 | ** |
| 5768 | ** ^If the flags parameter is non-zero, then the BLOB is opened for read |
| 5769 | ** and write access. ^If it is zero, the BLOB is opened for read access. |
| 5770 | ** ^It is not possible to open a column that is part of an index or primary |
| 5771 | ** key for writing. ^If [foreign key constraints] are enabled, it is |
| 5772 | ** not possible to open a column that is part of a [child key] for writing. |
| 5773 | ** |
| 5774 | ** ^Note that the database name is not the filename that contains |
| 5775 | ** the database but rather the symbolic name of the database that |
| 5776 | ** appears after the AS keyword when the database is connected using [ATTACH]. |
| 5777 | ** ^For the main database file, the database name is "main". |
| 5778 | ** ^For TEMP tables, the database name is "temp". |
| 5779 | ** |
| 5780 | ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written |
| 5781 | ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set |
| 5782 | ** to be a null pointer.)^ |
| 5783 | ** ^This function sets the [database connection] error code and message |
| 5784 | ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related |
| 5785 | ** functions. ^Note that the *ppBlob variable is always initialized in a |
| 5786 | ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob |
| 5787 | ** regardless of the success or failure of this routine. |
| 5788 | ** |
| 5789 | ** ^(If the row that a BLOB handle points to is modified by an |
| 5790 | ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects |
| 5791 | ** then the BLOB handle is marked as "expired". |
| 5792 | ** This is true if any column of the row is changed, even a column |
| @@ -5800,17 +5837,13 @@ | |
| 5800 | ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of |
| 5801 | ** the opened blob. ^The size of a blob may not be changed by this |
| 5802 | ** interface. Use the [UPDATE] SQL command to change the size of a |
| 5803 | ** blob. |
| 5804 | ** |
| 5805 | ** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID] |
| 5806 | ** table. Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables. |
| 5807 | ** |
| 5808 | ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces |
| 5809 | ** and the built-in [zeroblob] SQL function can be used, if desired, |
| 5810 | ** to create an empty, zero-filled blob in which to read or write using |
| 5811 | ** this interface. |
| 5812 | ** |
| 5813 | ** To avoid a resource leak, every open [BLOB handle] should eventually |
| 5814 | ** be released by a call to [sqlite3_blob_close()]. |
| 5815 | */ |
| 5816 | SQLITE_API int sqlite3_blob_open( |
| @@ -5848,28 +5881,26 @@ | |
| 5848 | SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); |
| 5849 | |
| 5850 | /* |
| 5851 | ** CAPI3REF: Close A BLOB Handle |
| 5852 | ** |
| 5853 | ** ^Closes an open [BLOB handle]. |
| 5854 | ** |
| 5855 | ** ^Closing a BLOB shall cause the current transaction to commit |
| 5856 | ** if there are no other BLOBs, no pending prepared statements, and the |
| 5857 | ** database connection is in [autocommit mode]. |
| 5858 | ** ^If any writes were made to the BLOB, they might be held in cache |
| 5859 | ** until the close operation if they will fit. |
| 5860 | ** |
| 5861 | ** ^(Closing the BLOB often forces the changes |
| 5862 | ** out to disk and so if any I/O errors occur, they will likely occur |
| 5863 | ** at the time when the BLOB is closed. Any errors that occur during |
| 5864 | ** closing are reported as a non-zero return value.)^ |
| 5865 | ** |
| 5866 | ** ^(The BLOB is closed unconditionally. Even if this routine returns |
| 5867 | ** an error code, the BLOB is still closed.)^ |
| 5868 | ** |
| 5869 | ** ^Calling this routine with a null pointer (such as would be returned |
| 5870 | ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op. |
| 5871 | */ |
| 5872 | SQLITE_API int sqlite3_blob_close(sqlite3_blob *); |
| 5873 | |
| 5874 | /* |
| 5875 | ** CAPI3REF: Return The Size Of An Open BLOB |
| @@ -5915,36 +5946,39 @@ | |
| 5915 | SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); |
| 5916 | |
| 5917 | /* |
| 5918 | ** CAPI3REF: Write Data Into A BLOB Incrementally |
| 5919 | ** |
| 5920 | ** ^This function is used to write data into an open [BLOB handle] from a |
| 5921 | ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z |
| 5922 | ** into the open BLOB, starting at offset iOffset. |
| 5923 | ** |
| 5924 | ** ^If the [BLOB handle] passed as the first argument was not opened for |
| 5925 | ** writing (the flags parameter to [sqlite3_blob_open()] was zero), |
| 5926 | ** this function returns [SQLITE_READONLY]. |
| 5927 | ** |
| 5928 | ** ^This function may only modify the contents of the BLOB; it is |
| 5929 | ** not possible to increase the size of a BLOB using this API. |
| 5930 | ** ^If offset iOffset is less than N bytes from the end of the BLOB, |
| 5931 | ** [SQLITE_ERROR] is returned and no data is written. ^If N is |
| 5932 | ** less than zero [SQLITE_ERROR] is returned and no data is written. |
| 5933 | ** The size of the BLOB (and hence the maximum value of N+iOffset) |
| 5934 | ** can be determined using the [sqlite3_blob_bytes()] interface. |
| 5935 | ** |
| 5936 | ** ^An attempt to write to an expired [BLOB handle] fails with an |
| 5937 | ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred |
| 5938 | ** before the [BLOB handle] expired are not rolled back by the |
| 5939 | ** expiration of the handle, though of course those changes might |
| 5940 | ** have been overwritten by the statement that expired the BLOB handle |
| 5941 | ** or by other independent statements. |
| 5942 | ** |
| 5943 | ** ^(On success, sqlite3_blob_write() returns SQLITE_OK. |
| 5944 | ** Otherwise, an [error code] or an [extended error code] is returned.)^ |
| 5945 | ** |
| 5946 | ** This routine only works on a [BLOB handle] which has been created |
| 5947 | ** by a prior successful call to [sqlite3_blob_open()] and which has not |
| 5948 | ** been closed by [sqlite3_blob_close()]. Passing any other pointer in |
| 5949 | ** to this routine results in undefined and probably undesirable behavior. |
| 5950 | ** |
| @@ -5993,38 +6027,38 @@ | |
| 5993 | ** use by SQLite, code that links against SQLite is |
| 5994 | ** permitted to use any of these routines. |
| 5995 | ** |
| 5996 | ** The SQLite source code contains multiple implementations |
| 5997 | ** of these mutex routines. An appropriate implementation |
| 5998 | ** is selected automatically at compile-time. ^(The following |
| 5999 | ** implementations are available in the SQLite core: |
| 6000 | ** |
| 6001 | ** <ul> |
| 6002 | ** <li> SQLITE_MUTEX_PTHREADS |
| 6003 | ** <li> SQLITE_MUTEX_W32 |
| 6004 | ** <li> SQLITE_MUTEX_NOOP |
| 6005 | ** </ul>)^ |
| 6006 | ** |
| 6007 | ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines |
| 6008 | ** that does no real locking and is appropriate for use in |
| 6009 | ** a single-threaded application. ^The SQLITE_MUTEX_PTHREADS and |
| 6010 | ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix |
| 6011 | ** and Windows. |
| 6012 | ** |
| 6013 | ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor |
| 6014 | ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex |
| 6015 | ** implementation is included with the library. In this case the |
| 6016 | ** application must supply a custom mutex implementation using the |
| 6017 | ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function |
| 6018 | ** before calling sqlite3_initialize() or any other public sqlite3_ |
| 6019 | ** function that calls sqlite3_initialize().)^ |
| 6020 | ** |
| 6021 | ** ^The sqlite3_mutex_alloc() routine allocates a new |
| 6022 | ** mutex and returns a pointer to it. ^If it returns NULL |
| 6023 | ** that means that a mutex could not be allocated. ^SQLite |
| 6024 | ** will unwind its stack and return an error. ^(The argument |
| 6025 | ** to sqlite3_mutex_alloc() is one of these integer constants: |
| 6026 | ** |
| 6027 | ** <ul> |
| 6028 | ** <li> SQLITE_MUTEX_FAST |
| 6029 | ** <li> SQLITE_MUTEX_RECURSIVE |
| 6030 | ** <li> SQLITE_MUTEX_STATIC_MASTER |
| @@ -6033,68 +6067,64 @@ | |
| 6033 | ** <li> SQLITE_MUTEX_STATIC_PRNG |
| 6034 | ** <li> SQLITE_MUTEX_STATIC_LRU |
| 6035 | ** <li> SQLITE_MUTEX_STATIC_PMEM |
| 6036 | ** <li> SQLITE_MUTEX_STATIC_APP1 |
| 6037 | ** <li> SQLITE_MUTEX_STATIC_APP2 |
| 6038 | ** </ul>)^ |
| 6039 | ** |
| 6040 | ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) |
| 6041 | ** cause sqlite3_mutex_alloc() to create |
| 6042 | ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE |
| 6043 | ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. |
| 6044 | ** The mutex implementation does not need to make a distinction |
| 6045 | ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does |
| 6046 | ** not want to. ^SQLite will only request a recursive mutex in |
| 6047 | ** cases where it really needs one. ^If a faster non-recursive mutex |
| 6048 | ** implementation is available on the host platform, the mutex subsystem |
| 6049 | ** might return such a mutex in response to SQLITE_MUTEX_FAST. |
| 6050 | ** |
| 6051 | ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other |
| 6052 | ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return |
| 6053 | ** a pointer to a static preexisting mutex. ^Six static mutexes are |
| 6054 | ** used by the current version of SQLite. Future versions of SQLite |
| 6055 | ** may add additional static mutexes. Static mutexes are for internal |
| 6056 | ** use by SQLite only. Applications that use SQLite mutexes should |
| 6057 | ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or |
| 6058 | ** SQLITE_MUTEX_RECURSIVE. |
| 6059 | ** |
| 6060 | ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST |
| 6061 | ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() |
| 6062 | ** returns a different mutex on every call. ^But for the static |
| 6063 | ** mutex types, the same mutex is returned on every call that has |
| 6064 | ** the same type number. |
| 6065 | ** |
| 6066 | ** ^The sqlite3_mutex_free() routine deallocates a previously |
| 6067 | ** allocated dynamic mutex. ^SQLite is careful to deallocate every |
| 6068 | ** dynamic mutex that it allocates. The dynamic mutexes must not be in |
| 6069 | ** use when they are deallocated. Attempting to deallocate a static |
| 6070 | ** mutex results in undefined behavior. ^SQLite never deallocates |
| 6071 | ** a static mutex. |
| 6072 | ** |
| 6073 | ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt |
| 6074 | ** to enter a mutex. ^If another thread is already within the mutex, |
| 6075 | ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return |
| 6076 | ** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK] |
| 6077 | ** upon successful entry. ^(Mutexes created using |
| 6078 | ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. |
| 6079 | ** In such cases the, |
| 6080 | ** mutex must be exited an equal number of times before another thread |
| 6081 | ** can enter.)^ ^(If the same thread tries to enter any other |
| 6082 | ** kind of mutex more than once, the behavior is undefined. |
| 6083 | ** SQLite will never exhibit |
| 6084 | ** such behavior in its own use of mutexes.)^ |
| 6085 | ** |
| 6086 | ** ^(Some systems (for example, Windows 95) do not support the operation |
| 6087 | ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() |
| 6088 | ** will always return SQLITE_BUSY. The SQLite core only ever uses |
| 6089 | ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^ |
| 6090 | ** |
| 6091 | ** ^The sqlite3_mutex_leave() routine exits a mutex that was |
| 6092 | ** previously entered by the same thread. ^(The behavior |
| 6093 | ** is undefined if the mutex is not currently entered by the |
| 6094 | ** calling thread or is not currently allocated. SQLite will |
| 6095 | ** never do either.)^ |
| 6096 | ** |
| 6097 | ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or |
| 6098 | ** sqlite3_mutex_leave() is a NULL pointer, then all three routines |
| 6099 | ** behave as no-ops. |
| 6100 | ** |
| @@ -6111,13 +6141,13 @@ | |
| 6111 | ** |
| 6112 | ** An instance of this structure defines the low-level routines |
| 6113 | ** used to allocate and use mutexes. |
| 6114 | ** |
| 6115 | ** Usually, the default mutex implementations provided by SQLite are |
| 6116 | ** sufficient, however the user has the option of substituting a custom |
| 6117 | ** implementation for specialized deployments or systems for which SQLite |
| 6118 | ** does not provide a suitable implementation. In this case, the user |
| 6119 | ** creates and populates an instance of this structure to pass |
| 6120 | ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option. |
| 6121 | ** Additionally, an instance of this structure can be used as an |
| 6122 | ** output variable when querying the system for the current mutex |
| 6123 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| @@ -6154,17 +6184,17 @@ | |
| 6154 | ** by this structure are not required to handle this case, the results |
| 6155 | ** of passing a NULL pointer instead of a valid mutex handle are undefined |
| 6156 | ** (i.e. it is acceptable to provide an implementation that segfaults if |
| 6157 | ** it is passed a NULL pointer). |
| 6158 | ** |
| 6159 | ** The xMutexInit() method must be threadsafe. ^It must be harmless to |
| 6160 | ** invoke xMutexInit() multiple times within the same process and without |
| 6161 | ** intervening calls to xMutexEnd(). Second and subsequent calls to |
| 6162 | ** xMutexInit() must be no-ops. |
| 6163 | ** |
| 6164 | ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] |
| 6165 | ** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory |
| 6166 | ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite |
| 6167 | ** memory allocation for a fast or recursive mutex. |
| 6168 | ** |
| 6169 | ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is |
| 6170 | ** called, but only if the prior call to xMutexInit returned SQLITE_OK. |
| @@ -6186,33 +6216,33 @@ | |
| 6186 | |
| 6187 | /* |
| 6188 | ** CAPI3REF: Mutex Verification Routines |
| 6189 | ** |
| 6190 | ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines |
| 6191 | ** are intended for use inside assert() statements. ^The SQLite core |
| 6192 | ** never uses these routines except inside an assert() and applications |
| 6193 | ** are advised to follow the lead of the core. ^The SQLite core only |
| 6194 | ** provides implementations for these routines when it is compiled |
| 6195 | ** with the SQLITE_DEBUG flag. ^External mutex implementations |
| 6196 | ** are only required to provide these routines if SQLITE_DEBUG is |
| 6197 | ** defined and if NDEBUG is not defined. |
| 6198 | ** |
| 6199 | ** ^These routines should return true if the mutex in their argument |
| 6200 | ** is held or not held, respectively, by the calling thread. |
| 6201 | ** |
| 6202 | ** ^The implementation is not required to provide versions of these |
| 6203 | ** routines that actually work. If the implementation does not provide working |
| 6204 | ** versions of these routines, it should at least provide stubs that always |
| 6205 | ** return true so that one does not get spurious assertion failures. |
| 6206 | ** |
| 6207 | ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then |
| 6208 | ** the routine should return 1. This seems counter-intuitive since |
| 6209 | ** clearly the mutex cannot be held if it does not exist. But |
| 6210 | ** the reason the mutex does not exist is because the build is not |
| 6211 | ** using mutexes. And we do not want the assert() containing the |
| 6212 | ** call to sqlite3_mutex_held() to fail, so a non-zero return is |
| 6213 | ** the appropriate thing to do. ^The sqlite3_mutex_notheld() |
| 6214 | ** interface should also return 1 when given a NULL pointer. |
| 6215 | */ |
| 6216 | #ifndef NDEBUG |
| 6217 | SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*); |
| 6218 | SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); |
| @@ -6940,10 +6970,14 @@ | |
| 6940 | ** sqlite3_backup_init(D,N,S,M) identify the [database connection] |
| 6941 | ** and database name of the source database, respectively. |
| 6942 | ** ^The source and destination [database connections] (parameters S and D) |
| 6943 | ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with |
| 6944 | ** an error. |
| 6945 | ** |
| 6946 | ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is |
| 6947 | ** returned and an error code and error message are stored in the |
| 6948 | ** destination [database connection] D. |
| 6949 | ** ^The error code and message for the failed call to sqlite3_backup_init() |
| @@ -7533,10 +7567,102 @@ | |
| 7533 | /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */ |
| 7534 | #define SQLITE_FAIL 3 |
| 7535 | /* #define SQLITE_ABORT 4 // Also an error code */ |
| 7536 | #define SQLITE_REPLACE 5 |
| 7537 | |
| 7538 | |
| 7539 | |
| 7540 | /* |
| 7541 | ** Undo the hack that converts floating point types to integer for |
| 7542 | ** builds on processors without floating point support. |
| @@ -7978,14 +8104,13 @@ | |
| 7978 | #ifndef SQLITE_POWERSAFE_OVERWRITE |
| 7979 | # define SQLITE_POWERSAFE_OVERWRITE 1 |
| 7980 | #endif |
| 7981 | |
| 7982 | /* |
| 7983 | ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1. |
| 7984 | ** It determines whether or not the features related to |
| 7985 | ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can |
| 7986 | ** be overridden at runtime using the sqlite3_config() API. |
| 7987 | */ |
| 7988 | #if !defined(SQLITE_DEFAULT_MEMSTATUS) |
| 7989 | # define SQLITE_DEFAULT_MEMSTATUS 1 |
| 7990 | #endif |
| 7991 | |
| @@ -8611,11 +8736,11 @@ | |
| 8611 | ** Estimated quantities used for query planning are stored as 16-bit |
| 8612 | ** logarithms. For quantity X, the value stored is 10*log2(X). This |
| 8613 | ** gives a possible range of values of approximately 1.0e986 to 1e-986. |
| 8614 | ** But the allowed values are "grainy". Not every value is representable. |
| 8615 | ** For example, quantities 16 and 17 are both represented by a LogEst |
| 8616 | ** of 40. However, since LogEst quantaties are suppose to be estimates, |
| 8617 | ** not exact values, this imprecision is not a problem. |
| 8618 | ** |
| 8619 | ** "LogEst" is short for "Logarithmic Estimate". |
| 8620 | ** |
| 8621 | ** Examples: |
| @@ -9124,10 +9249,11 @@ | |
| 9124 | SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *); |
| 9125 | SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *); |
| 9126 | SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); |
| 9127 | SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask); |
| 9128 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt); |
| 9129 | |
| 9130 | #ifndef NDEBUG |
| 9131 | SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*); |
| 9132 | #endif |
| 9133 | |
| @@ -9666,10 +9792,16 @@ | |
| 9666 | # define VdbeCoverageAlwaysTaken(v) |
| 9667 | # define VdbeCoverageNeverTaken(v) |
| 9668 | # define VDBE_OFFSET_LINENO(x) 0 |
| 9669 | #endif |
| 9670 | |
| 9671 | #endif |
| 9672 | |
| 9673 | /************** End of vdbe.h ************************************************/ |
| 9674 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 9675 | /************** Include pager.h in the middle of sqliteInt.h *****************/ |
| @@ -9862,10 +9994,12 @@ | |
| 9862 | SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *); |
| 9863 | |
| 9864 | /* Functions used to truncate the database file. */ |
| 9865 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); |
| 9866 | |
| 9867 | #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL) |
| 9868 | SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *); |
| 9869 | #endif |
| 9870 | |
| 9871 | /* Functions to support testing and debugging. */ |
| @@ -10049,10 +10183,14 @@ | |
| 10049 | SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*); |
| 10050 | #endif |
| 10051 | |
| 10052 | SQLITE_PRIVATE void sqlite3PCacheSetDefault(void); |
| 10053 | |
| 10054 | #endif /* _PCACHE_H_ */ |
| 10055 | |
| 10056 | /************** End of pcache.h **********************************************/ |
| 10057 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 10058 | |
| @@ -10735,11 +10873,11 @@ | |
| 10735 | #define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */ |
| 10736 | #define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */ |
| 10737 | #define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */ |
| 10738 | #define SQLITE_Transitive 0x0200 /* Transitive constraints */ |
| 10739 | #define SQLITE_OmitNoopJoin 0x0400 /* Omit unused tables in joins */ |
| 10740 | #define SQLITE_Stat3 0x0800 /* Use the SQLITE_STAT3 table */ |
| 10741 | #define SQLITE_AllOpts 0xffff /* All optimizations */ |
| 10742 | |
| 10743 | /* |
| 10744 | ** Macros for testing whether or not optimizations are enabled or disabled. |
| 10745 | */ |
| @@ -11317,16 +11455,18 @@ | |
| 11317 | unsigned idxType:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */ |
| 11318 | unsigned bUnordered:1; /* Use this index for == or IN queries only */ |
| 11319 | unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */ |
| 11320 | unsigned isResized:1; /* True if resizeIndexObject() has been called */ |
| 11321 | unsigned isCovering:1; /* True if this is a covering index */ |
| 11322 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 11323 | int nSample; /* Number of elements in aSample[] */ |
| 11324 | int nSampleCol; /* Size of IndexSample.anEq[] and so on */ |
| 11325 | tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */ |
| 11326 | IndexSample *aSample; /* Samples of the left-most key */ |
| 11327 | tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this table */ |
| 11328 | #endif |
| 11329 | }; |
| 11330 | |
| 11331 | /* |
| 11332 | ** Allowed values for Index.idxType |
| @@ -11520,11 +11660,11 @@ | |
| 11520 | int nHeight; /* Height of the tree headed by this node */ |
| 11521 | #endif |
| 11522 | int iTable; /* TK_COLUMN: cursor number of table holding column |
| 11523 | ** TK_REGISTER: register number |
| 11524 | ** TK_TRIGGER: 1 -> new, 0 -> old |
| 11525 | ** EP_Unlikely: 1000 times likelihood */ |
| 11526 | ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid. |
| 11527 | ** TK_VARIABLE: variable number (always >= 1). */ |
| 11528 | i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */ |
| 11529 | i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */ |
| 11530 | u8 op2; /* TK_REGISTER: original value of Expr.op |
| @@ -12412,13 +12552,15 @@ | |
| 12412 | int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */ |
| 12413 | int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */ |
| 12414 | void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */ |
| 12415 | Parse *pParse; /* Parser context. */ |
| 12416 | int walkerDepth; /* Number of subqueries */ |
| 12417 | union { /* Extra data for callback */ |
| 12418 | NameContext *pNC; /* Naming context */ |
| 12419 | int i; /* Integer value */ |
| 12420 | SrcList *pSrcList; /* FROM clause */ |
| 12421 | struct SrcCount *pSrcCount; /* Counting column references */ |
| 12422 | } u; |
| 12423 | }; |
| 12424 | |
| @@ -12815,10 +12957,11 @@ | |
| 12815 | SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *); |
| 12816 | SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*); |
| 12817 | SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*); |
| 12818 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*); |
| 12819 | SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8); |
| 12820 | SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*); |
| 12821 | SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*); |
| 12822 | SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); |
| 12823 | SQLITE_PRIVATE int sqlite3IsRowid(const char*); |
| 12824 | SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8); |
| @@ -13472,15 +13615,23 @@ | |
| 13472 | ** compatibility for legacy applications, the URI filename capability is |
| 13473 | ** disabled by default. |
| 13474 | ** |
| 13475 | ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled |
| 13476 | ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options. |
| 13477 | */ |
| 13478 | #ifndef SQLITE_USE_URI |
| 13479 | # define SQLITE_USE_URI 0 |
| 13480 | #endif |
| 13481 | |
| 13482 | #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN |
| 13483 | # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1 |
| 13484 | #endif |
| 13485 | |
| 13486 | /* |
| @@ -13566,12 +13717,12 @@ | |
| 13566 | ** than 1 GiB. The sqlite3_test_control() interface can be used to |
| 13567 | ** move the pending byte. |
| 13568 | ** |
| 13569 | ** IMPORTANT: Changing the pending byte to any value other than |
| 13570 | ** 0x40000000 results in an incompatible database file format! |
| 13571 | ** Changing the pending byte during operating results in undefined |
| 13572 | ** and dileterious behavior. |
| 13573 | */ |
| 13574 | #ifndef SQLITE_OMIT_WSD |
| 13575 | SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000; |
| 13576 | #endif |
| 13577 | |
| @@ -13646,10 +13797,13 @@ | |
| 13646 | #ifdef SQLITE_DISABLE_DIRSYNC |
| 13647 | "DISABLE_DIRSYNC", |
| 13648 | #endif |
| 13649 | #ifdef SQLITE_DISABLE_LFS |
| 13650 | "DISABLE_LFS", |
| 13651 | #endif |
| 13652 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 13653 | "ENABLE_ATOMIC_WRITE", |
| 13654 | #endif |
| 13655 | #ifdef SQLITE_ENABLE_CEROD |
| @@ -13972,10 +14126,17 @@ | |
| 13972 | ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix |
| 13973 | ** is not required for a match. |
| 13974 | */ |
| 13975 | SQLITE_API int sqlite3_compileoption_used(const char *zOptName){ |
| 13976 | int i, n; |
| 13977 | if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7; |
| 13978 | n = sqlite3Strlen30(zOptName); |
| 13979 | |
| 13980 | /* Since ArraySize(azCompileOpt) is normally in single digits, a |
| 13981 | ** linear search is adequate. No need for a binary search. */ |
| @@ -14153,10 +14314,11 @@ | |
| 14153 | typedef struct VdbeFrame VdbeFrame; |
| 14154 | struct VdbeFrame { |
| 14155 | Vdbe *v; /* VM this frame belongs to */ |
| 14156 | VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */ |
| 14157 | Op *aOp; /* Program instructions for parent frame */ |
| 14158 | Mem *aMem; /* Array of memory cells for parent frame */ |
| 14159 | u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */ |
| 14160 | VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */ |
| 14161 | void *token; /* Copy of SubProgram.token */ |
| 14162 | i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */ |
| @@ -14165,11 +14327,12 @@ | |
| 14165 | int nOp; /* Size of aOp array */ |
| 14166 | int nMem; /* Number of entries in aMem */ |
| 14167 | int nOnceFlag; /* Number of entries in aOnceFlag */ |
| 14168 | int nChildMem; /* Number of memory cells for child frame */ |
| 14169 | int nChildCsr; /* Number of cursors for child frame */ |
| 14170 | int nChange; /* Statement changes (Vdbe.nChanges) */ |
| 14171 | }; |
| 14172 | |
| 14173 | #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) |
| 14174 | |
| 14175 | /* |
| @@ -14316,10 +14479,20 @@ | |
| 14316 | /* A bitfield type for use inside of structures. Always follow with :N where |
| 14317 | ** N is the number of bits. |
| 14318 | */ |
| 14319 | typedef unsigned bft; /* Bit Field Type */ |
| 14320 | |
| 14321 | /* |
| 14322 | ** An instance of the virtual machine. This structure contains the complete |
| 14323 | ** state of the virtual machine. |
| 14324 | ** |
| 14325 | ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare() |
| @@ -14388,10 +14561,15 @@ | |
| 14388 | u32 expmask; /* Binding to these vars invalidates VM */ |
| 14389 | SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ |
| 14390 | int nOnceFlag; /* Size of array aOnceFlag[] */ |
| 14391 | u8 *aOnceFlag; /* Flags for OP_Once */ |
| 14392 | AuxData *pAuxData; /* Linked list of auxdata allocations */ |
| 14393 | }; |
| 14394 | |
| 14395 | /* |
| 14396 | ** The following are allowed values for Vdbe.magic |
| 14397 | */ |
| @@ -14577,10 +14755,13 @@ | |
| 14577 | SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ |
| 14578 | wsdStatInit; |
| 14579 | if( op<0 || op>=ArraySize(wsdStat.nowValue) ){ |
| 14580 | return SQLITE_MISUSE_BKPT; |
| 14581 | } |
| 14582 | *pCurrent = wsdStat.nowValue[op]; |
| 14583 | *pHighwater = wsdStat.mxValue[op]; |
| 14584 | if( resetFlag ){ |
| 14585 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
| 14586 | } |
| @@ -14596,10 +14777,15 @@ | |
| 14596 | int *pCurrent, /* Write current value here */ |
| 14597 | int *pHighwater, /* Write high-water mark here */ |
| 14598 | int resetFlag /* Reset high-water mark if true */ |
| 14599 | ){ |
| 14600 | int rc = SQLITE_OK; /* Return code */ |
| 14601 | sqlite3_mutex_enter(db->mutex); |
| 14602 | switch( op ){ |
| 14603 | case SQLITE_DBSTATUS_LOOKASIDE_USED: { |
| 14604 | *pCurrent = db->lookaside.nOut; |
| 14605 | *pHighwater = db->lookaside.mxOut; |
| @@ -14774,11 +14960,11 @@ | |
| 14774 | ** |
| 14775 | ** There is only one exported symbol in this file - the function |
| 14776 | ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. |
| 14777 | ** All other code has file scope. |
| 14778 | ** |
| 14779 | ** SQLite processes all times and dates as Julian Day numbers. The |
| 14780 | ** dates and times are stored as the number of days since noon |
| 14781 | ** in Greenwich on November 24, 4714 B.C. according to the Gregorian |
| 14782 | ** calendar system. |
| 14783 | ** |
| 14784 | ** 1970-01-01 00:00:00 is JD 2440587.5 |
| @@ -14789,11 +14975,11 @@ | |
| 14789 | ** be represented, even though julian day numbers allow a much wider |
| 14790 | ** range of dates. |
| 14791 | ** |
| 14792 | ** The Gregorian calendar system is used for all dates and times, |
| 14793 | ** even those that predate the Gregorian calendar. Historians usually |
| 14794 | ** use the Julian calendar for dates prior to 1582-10-15 and for some |
| 14795 | ** dates afterwards, depending on locale. Beware of this difference. |
| 14796 | ** |
| 14797 | ** The conversion algorithms are implemented based on descriptions |
| 14798 | ** in the following text: |
| 14799 | ** |
| @@ -15061,11 +15247,11 @@ | |
| 15061 | return 1; |
| 15062 | } |
| 15063 | } |
| 15064 | |
| 15065 | /* |
| 15066 | ** Attempt to parse the given string into a Julian Day Number. Return |
| 15067 | ** the number of errors. |
| 15068 | ** |
| 15069 | ** The following are acceptable forms for the input string: |
| 15070 | ** |
| 15071 | ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM |
| @@ -15632,11 +15818,11 @@ | |
| 15632 | ** |
| 15633 | ** %d day of month |
| 15634 | ** %f ** fractional seconds SS.SSS |
| 15635 | ** %H hour 00-24 |
| 15636 | ** %j day of year 000-366 |
| 15637 | ** %J ** Julian day number |
| 15638 | ** %m month 01-12 |
| 15639 | ** %M minute 00-59 |
| 15640 | ** %s seconds since 1970-01-01 |
| 15641 | ** %S seconds 00-59 |
| 15642 | ** %w day of week 0-6 sunday==0 |
| @@ -16257,10 +16443,14 @@ | |
| 16257 | MUTEX_LOGIC(sqlite3_mutex *mutex;) |
| 16258 | #ifndef SQLITE_OMIT_AUTOINIT |
| 16259 | int rc = sqlite3_initialize(); |
| 16260 | if( rc ) return rc; |
| 16261 | #endif |
| 16262 | MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 16263 | sqlite3_mutex_enter(mutex); |
| 16264 | vfsUnlink(pVfs); |
| 16265 | if( makeDflt || vfsList==0 ){ |
| 16266 | pVfs->pNext = vfsList; |
| @@ -18614,10 +18804,11 @@ | |
| 18614 | ** Retrieve a pointer to a static mutex or allocate a new dynamic one. |
| 18615 | */ |
| 18616 | SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){ |
| 18617 | #ifndef SQLITE_OMIT_AUTOINIT |
| 18618 | if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0; |
| 18619 | #endif |
| 18620 | return sqlite3GlobalConfig.mutex.xMutexAlloc(id); |
| 18621 | } |
| 18622 | |
| 18623 | SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){ |
| @@ -19070,12 +19261,16 @@ | |
| 19070 | pthread_mutex_init(&p->mutex, 0); |
| 19071 | } |
| 19072 | break; |
| 19073 | } |
| 19074 | default: { |
| 19075 | assert( iType-2 >= 0 ); |
| 19076 | assert( iType-2 < ArraySize(staticMutexes) ); |
| 19077 | p = &staticMutexes[iType-2]; |
| 19078 | #if SQLITE_MUTEX_NREF |
| 19079 | p->id = iType; |
| 19080 | #endif |
| 19081 | break; |
| @@ -20293,15 +20488,16 @@ | |
| 20293 | } |
| 20294 | assert( sqlite3_mutex_notheld(mem0.mutex) ); |
| 20295 | |
| 20296 | |
| 20297 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 20298 | /* Verify that no more than two scratch allocations per thread |
| 20299 | ** are outstanding at one time. (This is only checked in the |
| 20300 | ** single-threaded case since checking in the multi-threaded case |
| 20301 | ** would be much more complicated.) */ |
| 20302 | assert( scratchAllocOut<=1 ); |
| 20303 | if( p ) scratchAllocOut++; |
| 20304 | #endif |
| 20305 | |
| 20306 | return p; |
| 20307 | } |
| @@ -20956,10 +21152,17 @@ | |
| 20956 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
| 20957 | #endif |
| 20958 | PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ |
| 20959 | char buf[etBUFSIZE]; /* Conversion buffer */ |
| 20960 | |
| 20961 | bufpt = 0; |
| 20962 | if( bFlags ){ |
| 20963 | if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ |
| 20964 | pArgList = va_arg(ap, PrintfArguments*); |
| 20965 | } |
| @@ -21496,10 +21699,15 @@ | |
| 21496 | return N; |
| 21497 | }else{ |
| 21498 | char *zOld = (p->zText==p->zBase ? 0 : p->zText); |
| 21499 | i64 szNew = p->nChar; |
| 21500 | szNew += N + 1; |
| 21501 | if( szNew > p->mxAlloc ){ |
| 21502 | sqlite3StrAccumReset(p); |
| 21503 | setStrAccumError(p, STRACCUM_TOOBIG); |
| 21504 | return 0; |
| 21505 | }else{ |
| @@ -21512,10 +21720,11 @@ | |
| 21512 | } |
| 21513 | if( zNew ){ |
| 21514 | assert( p->zText!=0 || p->nChar==0 ); |
| 21515 | if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 21516 | p->zText = zNew; |
| 21517 | }else{ |
| 21518 | sqlite3StrAccumReset(p); |
| 21519 | setStrAccumError(p, STRACCUM_NOMEM); |
| 21520 | return 0; |
| 21521 | } |
| @@ -21681,10 +21890,17 @@ | |
| 21681 | */ |
| 21682 | SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){ |
| 21683 | char *z; |
| 21684 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
| 21685 | StrAccum acc; |
| 21686 | #ifndef SQLITE_OMIT_AUTOINIT |
| 21687 | if( sqlite3_initialize() ) return 0; |
| 21688 | #endif |
| 21689 | sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
| 21690 | acc.useMalloc = 2; |
| @@ -21723,10 +21939,17 @@ | |
| 21723 | ** sqlite3_vsnprintf() is the varargs version. |
| 21724 | */ |
| 21725 | SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ |
| 21726 | StrAccum acc; |
| 21727 | if( n<=0 ) return zBuf; |
| 21728 | sqlite3StrAccumInit(&acc, zBuf, n, 0); |
| 21729 | acc.useMalloc = 0; |
| 21730 | sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 21731 | return sqlite3StrAccumFinish(&acc); |
| 21732 | } |
| @@ -21914,15 +22137,23 @@ | |
| 21914 | #else |
| 21915 | # define wsdPrng sqlite3Prng |
| 21916 | #endif |
| 21917 | |
| 21918 | #if SQLITE_THREADSAFE |
| 21919 | sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); |
| 21920 | sqlite3_mutex_enter(mutex); |
| 21921 | #endif |
| 21922 | |
| 21923 | if( N<=0 ){ |
| 21924 | wsdPrng.isInit = 0; |
| 21925 | sqlite3_mutex_leave(mutex); |
| 21926 | return; |
| 21927 | } |
| 21928 | |
| @@ -23040,17 +23271,27 @@ | |
| 23040 | ** case-independent fashion, using the same definition of "case |
| 23041 | ** independence" that SQLite uses internally when comparing identifiers. |
| 23042 | */ |
| 23043 | SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){ |
| 23044 | register unsigned char *a, *b; |
| 23045 | a = (unsigned char *)zLeft; |
| 23046 | b = (unsigned char *)zRight; |
| 23047 | while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 23048 | return UpperToLower[*a] - UpperToLower[*b]; |
| 23049 | } |
| 23050 | SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ |
| 23051 | register unsigned char *a, *b; |
| 23052 | a = (unsigned char *)zLeft; |
| 23053 | b = (unsigned char *)zRight; |
| 23054 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 23055 | return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
| 23056 | } |
| @@ -32579,10 +32820,15 @@ | |
| 32579 | #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL) |
| 32580 | # error "WAL mode requires support from the Windows NT kernel, compile\ |
| 32581 | with SQLITE_OMIT_WAL." |
| 32582 | #endif |
| 32583 | |
| 32584 | /* |
| 32585 | ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions |
| 32586 | ** based on the sub-platform)? |
| 32587 | */ |
| 32588 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI) |
| @@ -32708,14 +32954,15 @@ | |
| 32708 | # define winGetDirSep() '\\' |
| 32709 | #endif |
| 32710 | |
| 32711 | /* |
| 32712 | ** Do we need to manually define the Win32 file mapping APIs for use with WAL |
| 32713 | ** mode (e.g. these APIs are available in the Windows CE SDK; however, they |
| 32714 | ** are not present in the header file)? |
| 32715 | */ |
| 32716 | #if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) |
| 32717 | /* |
| 32718 | ** Two of the file mapping APIs are different under WinRT. Figure out which |
| 32719 | ** set we need. |
| 32720 | */ |
| 32721 | #if SQLITE_OS_WINRT |
| @@ -32739,11 +32986,11 @@ | |
| 32739 | |
| 32740 | /* |
| 32741 | ** This file mapping API is common to both Win32 and WinRT. |
| 32742 | */ |
| 32743 | WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID); |
| 32744 | #endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */ |
| 32745 | |
| 32746 | /* |
| 32747 | ** Some Microsoft compilers lack this definition. |
| 32748 | */ |
| 32749 | #ifndef INVALID_FILE_ATTRIBUTES |
| @@ -33032,21 +33279,21 @@ | |
| 33032 | |
| 33033 | #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \ |
| 33034 | LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent) |
| 33035 | |
| 33036 | #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \ |
| 33037 | !defined(SQLITE_OMIT_WAL)) |
| 33038 | { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 }, |
| 33039 | #else |
| 33040 | { "CreateFileMappingA", (SYSCALL)0, 0 }, |
| 33041 | #endif |
| 33042 | |
| 33043 | #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \ |
| 33044 | DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent) |
| 33045 | |
| 33046 | #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ |
| 33047 | !defined(SQLITE_OMIT_WAL)) |
| 33048 | { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 }, |
| 33049 | #else |
| 33050 | { "CreateFileMappingW", (SYSCALL)0, 0 }, |
| 33051 | #endif |
| 33052 | |
| @@ -33382,11 +33629,12 @@ | |
| 33382 | #ifndef osLockFileEx |
| 33383 | #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \ |
| 33384 | LPOVERLAPPED))aSyscall[48].pCurrent) |
| 33385 | #endif |
| 33386 | |
| 33387 | #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)) |
| 33388 | { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 }, |
| 33389 | #else |
| 33390 | { "MapViewOfFile", (SYSCALL)0, 0 }, |
| 33391 | #endif |
| 33392 | |
| @@ -33452,11 +33700,11 @@ | |
| 33452 | #endif |
| 33453 | |
| 33454 | #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
| 33455 | LPOVERLAPPED))aSyscall[58].pCurrent) |
| 33456 | |
| 33457 | #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) |
| 33458 | { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 }, |
| 33459 | #else |
| 33460 | { "UnmapViewOfFile", (SYSCALL)0, 0 }, |
| 33461 | #endif |
| 33462 | |
| @@ -33515,11 +33763,11 @@ | |
| 33515 | #endif |
| 33516 | |
| 33517 | #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \ |
| 33518 | FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent) |
| 33519 | |
| 33520 | #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL) |
| 33521 | { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 }, |
| 33522 | #else |
| 33523 | { "MapViewOfFileFromApp", (SYSCALL)0, 0 }, |
| 33524 | #endif |
| 33525 | |
| @@ -33579,11 +33827,11 @@ | |
| 33579 | |
| 33580 | { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 }, |
| 33581 | |
| 33582 | #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent) |
| 33583 | |
| 33584 | #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL) |
| 33585 | { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 }, |
| 33586 | #else |
| 33587 | { "CreateFileMappingFromApp", (SYSCALL)0, 0 }, |
| 33588 | #endif |
| 33589 | |
| @@ -39155,10 +39403,17 @@ | |
| 39155 | */ |
| 39156 | SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){ |
| 39157 | assert( pCache->pCache!=0 ); |
| 39158 | sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache); |
| 39159 | } |
| 39160 | |
| 39161 | #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) |
| 39162 | /* |
| 39163 | ** For all dirty pages currently in the cache, invoke the specified |
| 39164 | ** callback. This is only used if the SQLITE_CHECK_PAGES macro is |
| @@ -40154,10 +40409,15 @@ | |
| 40154 | pcache1Shrink /* xShrink */ |
| 40155 | }; |
| 40156 | sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods); |
| 40157 | } |
| 40158 | |
| 40159 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 40160 | /* |
| 40161 | ** This function is called to free superfluous dynamically allocated memory |
| 40162 | ** held by the pager system. Memory in use by any SQLite pager allocated |
| 40163 | ** by the current thread may be sqlite3_free()ed. |
| @@ -43763,11 +44023,11 @@ | |
| 43763 | ** should be page numbers which are never 0xffffffff. So filling |
| 43764 | ** pPager->dbFileVers[] with all 0xff bytes should suffice. |
| 43765 | ** |
| 43766 | ** For an encrypted database, the situation is more complex: bytes |
| 43767 | ** 24..39 of the database are white noise. But the probability of |
| 43768 | ** white noising equaling 16 bytes of 0xff is vanishingly small so |
| 43769 | ** we should still be ok. |
| 43770 | */ |
| 43771 | memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers)); |
| 43772 | }else{ |
| 43773 | u8 *dbFileVers = &((u8*)pPg->pData)[24]; |
| @@ -47710,10 +47970,22 @@ | |
| 47710 | } |
| 47711 | |
| 47712 | return SQLITE_OK; |
| 47713 | } |
| 47714 | #endif |
| 47715 | |
| 47716 | /* |
| 47717 | ** Return a pointer to the data for the specified page. |
| 47718 | */ |
| 47719 | SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){ |
| @@ -48108,10 +48380,11 @@ | |
| 48108 | SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){ |
| 48109 | assert( pPager->eState>=PAGER_READER ); |
| 48110 | return sqlite3WalFramesize(pPager->pWal); |
| 48111 | } |
| 48112 | #endif |
| 48113 | |
| 48114 | #endif /* SQLITE_OMIT_DISKIO */ |
| 48115 | |
| 48116 | /************** End of pager.c ***********************************************/ |
| 48117 | /************** Begin file wal.c *********************************************/ |
| @@ -49618,11 +49891,11 @@ | |
| 49618 | |
| 49619 | /* |
| 49620 | ** Free an iterator allocated by walIteratorInit(). |
| 49621 | */ |
| 49622 | static void walIteratorFree(WalIterator *p){ |
| 49623 | sqlite3ScratchFree(p); |
| 49624 | } |
| 49625 | |
| 49626 | /* |
| 49627 | ** Construct a WalInterator object that can be used to loop over all |
| 49628 | ** pages in the WAL in ascending order. The caller must hold the checkpoint |
| @@ -49653,21 +49926,21 @@ | |
| 49653 | /* Allocate space for the WalIterator object. */ |
| 49654 | nSegment = walFramePage(iLast) + 1; |
| 49655 | nByte = sizeof(WalIterator) |
| 49656 | + (nSegment-1)*sizeof(struct WalSegment) |
| 49657 | + iLast*sizeof(ht_slot); |
| 49658 | p = (WalIterator *)sqlite3ScratchMalloc(nByte); |
| 49659 | if( !p ){ |
| 49660 | return SQLITE_NOMEM; |
| 49661 | } |
| 49662 | memset(p, 0, nByte); |
| 49663 | p->nSegment = nSegment; |
| 49664 | |
| 49665 | /* Allocate temporary space used by the merge-sort routine. This block |
| 49666 | ** of memory will be freed before this function returns. |
| 49667 | */ |
| 49668 | aTmp = (ht_slot *)sqlite3ScratchMalloc( |
| 49669 | sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast) |
| 49670 | ); |
| 49671 | if( !aTmp ){ |
| 49672 | rc = SQLITE_NOMEM; |
| 49673 | } |
| @@ -49700,11 +49973,11 @@ | |
| 49700 | p->aSegment[i].nEntry = nEntry; |
| 49701 | p->aSegment[i].aIndex = aIndex; |
| 49702 | p->aSegment[i].aPgno = (u32 *)aPgno; |
| 49703 | } |
| 49704 | } |
| 49705 | sqlite3ScratchFree(aTmp); |
| 49706 | |
| 49707 | if( rc!=SQLITE_OK ){ |
| 49708 | walIteratorFree(p); |
| 49709 | } |
| 49710 | *pp = p; |
| @@ -53331,10 +53604,15 @@ | |
| 53331 | /* |
| 53332 | ** Defragment the page given. All Cells are moved to the |
| 53333 | ** end of the page and all free space is collected into one |
| 53334 | ** big FreeBlk that occurs in between the header and cell |
| 53335 | ** pointer array and the cell content area. |
| 53336 | */ |
| 53337 | static int defragmentPage(MemPage *pPage){ |
| 53338 | int i; /* Loop counter */ |
| 53339 | int pc; /* Address of the i-th cell */ |
| 53340 | int hdr; /* Offset to the page header */ |
| @@ -53343,28 +53621,27 @@ | |
| 53343 | int cellOffset; /* Offset to the cell pointer array */ |
| 53344 | int cbrk; /* Offset to the cell content area */ |
| 53345 | int nCell; /* Number of cells on the page */ |
| 53346 | unsigned char *data; /* The page data */ |
| 53347 | unsigned char *temp; /* Temp area for cell content */ |
| 53348 | int iCellFirst; /* First allowable cell index */ |
| 53349 | int iCellLast; /* Last possible cell index */ |
| 53350 | |
| 53351 | |
| 53352 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53353 | assert( pPage->pBt!=0 ); |
| 53354 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
| 53355 | assert( pPage->nOverflow==0 ); |
| 53356 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53357 | temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
| 53358 | data = pPage->aData; |
| 53359 | hdr = pPage->hdrOffset; |
| 53360 | cellOffset = pPage->cellOffset; |
| 53361 | nCell = pPage->nCell; |
| 53362 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 53363 | usableSize = pPage->pBt->usableSize; |
| 53364 | cbrk = get2byte(&data[hdr+5]); |
| 53365 | memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); |
| 53366 | cbrk = usableSize; |
| 53367 | iCellFirst = cellOffset + 2*nCell; |
| 53368 | iCellLast = usableSize - 4; |
| 53369 | for(i=0; i<nCell; i++){ |
| 53370 | u8 *pAddr; /* The i-th cell pointer */ |
| @@ -53379,11 +53656,11 @@ | |
| 53379 | if( pc<iCellFirst || pc>iCellLast ){ |
| 53380 | return SQLITE_CORRUPT_BKPT; |
| 53381 | } |
| 53382 | #endif |
| 53383 | assert( pc>=iCellFirst && pc<=iCellLast ); |
| 53384 | size = cellSizePtr(pPage, &temp[pc]); |
| 53385 | cbrk -= size; |
| 53386 | #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 53387 | if( cbrk<iCellFirst ){ |
| 53388 | return SQLITE_CORRUPT_BKPT; |
| 53389 | } |
| @@ -53393,12 +53670,20 @@ | |
| 53393 | } |
| 53394 | #endif |
| 53395 | assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); |
| 53396 | testcase( cbrk+size==usableSize ); |
| 53397 | testcase( pc+size==usableSize ); |
| 53398 | memcpy(&data[cbrk], &temp[pc], size); |
| 53399 | put2byte(pAddr, cbrk); |
| 53400 | } |
| 53401 | assert( cbrk>=iCellFirst ); |
| 53402 | put2byte(&data[hdr+5], cbrk); |
| 53403 | data[hdr+1] = 0; |
| 53404 | data[hdr+2] = 0; |
| @@ -53408,10 +53693,73 @@ | |
| 53408 | if( cbrk-iCellFirst!=pPage->nFree ){ |
| 53409 | return SQLITE_CORRUPT_BKPT; |
| 53410 | } |
| 53411 | return SQLITE_OK; |
| 53412 | } |
| 53413 | |
| 53414 | /* |
| 53415 | ** Allocate nByte bytes of space from within the B-Tree page passed |
| 53416 | ** as the first argument. Write into *pIdx the index into pPage->aData[] |
| 53417 | ** of the first byte of allocated space. Return either SQLITE_OK or |
| @@ -53426,79 +53774,57 @@ | |
| 53426 | */ |
| 53427 | static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
| 53428 | const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ |
| 53429 | u8 * const data = pPage->aData; /* Local cache of pPage->aData */ |
| 53430 | int top; /* First byte of cell content area */ |
| 53431 | int gap; /* First byte of gap between cell pointers and cell content */ |
| 53432 | int rc; /* Integer return code */ |
| 53433 | int usableSize; /* Usable size of the page */ |
| 53434 | |
| 53435 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53436 | assert( pPage->pBt ); |
| 53437 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53438 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 53439 | assert( pPage->nFree>=nByte ); |
| 53440 | assert( pPage->nOverflow==0 ); |
| 53441 | usableSize = pPage->pBt->usableSize; |
| 53442 | assert( nByte < usableSize-8 ); |
| 53443 | |
| 53444 | assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); |
| 53445 | gap = pPage->cellOffset + 2*pPage->nCell; |
| 53446 | assert( gap<=65536 ); |
| 53447 | top = get2byte(&data[hdr+5]); |
| 53448 | if( gap>top ){ |
| 53449 | if( top==0 ){ |
| 53450 | top = 65536; |
| 53451 | }else{ |
| 53452 | return SQLITE_CORRUPT_BKPT; |
| 53453 | } |
| 53454 | } |
| 53455 | |
| 53456 | /* If there is enough space between gap and top for one more cell pointer |
| 53457 | ** array entry offset, and if the freelist is not empty, then search the |
| 53458 | ** freelist looking for a free slot big enough to satisfy the request. |
| 53459 | */ |
| 53460 | testcase( gap+2==top ); |
| 53461 | testcase( gap+1==top ); |
| 53462 | testcase( gap==top ); |
| 53463 | if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){ |
| 53464 | int pc, addr; |
| 53465 | for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){ |
| 53466 | int size; /* Size of the free slot */ |
| 53467 | if( pc>usableSize-4 || pc<addr+4 ){ |
| 53468 | return SQLITE_CORRUPT_BKPT; |
| 53469 | } |
| 53470 | size = get2byte(&data[pc+2]); |
| 53471 | if( size>=nByte ){ |
| 53472 | int x = size - nByte; |
| 53473 | testcase( x==4 ); |
| 53474 | testcase( x==3 ); |
| 53475 | if( x<4 ){ |
| 53476 | if( data[hdr+7]>=60 ) goto defragment_page; |
| 53477 | /* Remove the slot from the free-list. Update the number of |
| 53478 | ** fragmented bytes within the page. */ |
| 53479 | memcpy(&data[addr], &data[pc], 2); |
| 53480 | data[hdr+7] += (u8)x; |
| 53481 | }else if( size+pc > usableSize ){ |
| 53482 | return SQLITE_CORRUPT_BKPT; |
| 53483 | }else{ |
| 53484 | /* The slot remains on the free-list. Reduce its size to account |
| 53485 | ** for the portion used by the new allocation. */ |
| 53486 | put2byte(&data[pc+2], x); |
| 53487 | } |
| 53488 | *pIdx = pc + x; |
| 53489 | return SQLITE_OK; |
| 53490 | } |
| 53491 | } |
| 53492 | } |
| 53493 | |
| 53494 | /* The request could not be fulfilled using a freelist slot. Check |
| 53495 | ** to see if defragmentation is necessary. |
| 53496 | */ |
| 53497 | testcase( gap+2+nByte==top ); |
| 53498 | if( gap+2+nByte>top ){ |
| 53499 | defragment_page: |
| 53500 | testcase( pPage->nCell==0 ); |
| 53501 | rc = defragmentPage(pPage); |
| 53502 | if( rc ) return rc; |
| 53503 | top = get2byteNotZero(&data[hdr+5]); |
| 53504 | assert( gap+nByte<=top ); |
| @@ -53542,11 +53868,11 @@ | |
| 53542 | unsigned char *data = pPage->aData; /* Page content */ |
| 53543 | |
| 53544 | assert( pPage->pBt!=0 ); |
| 53545 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53546 | assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
| 53547 | assert( iEnd <= pPage->pBt->usableSize ); |
| 53548 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53549 | assert( iSize>=4 ); /* Minimum cell size is 4 */ |
| 53550 | assert( iStart<=iLast ); |
| 53551 | |
| 53552 | /* Overwrite deleted information with zeros when the secure_delete |
| @@ -53637,22 +53963,36 @@ | |
| 53637 | pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
| 53638 | flagByte &= ~PTF_LEAF; |
| 53639 | pPage->childPtrSize = 4-4*pPage->leaf; |
| 53640 | pBt = pPage->pBt; |
| 53641 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 53642 | pPage->intKey = 1; |
| 53643 | pPage->intKeyLeaf = pPage->leaf; |
| 53644 | pPage->noPayload = !pPage->leaf; |
| 53645 | pPage->maxLocal = pBt->maxLeaf; |
| 53646 | pPage->minLocal = pBt->minLeaf; |
| 53647 | }else if( flagByte==PTF_ZERODATA ){ |
| 53648 | pPage->intKey = 0; |
| 53649 | pPage->intKeyLeaf = 0; |
| 53650 | pPage->noPayload = 0; |
| 53651 | pPage->maxLocal = pBt->maxLocal; |
| 53652 | pPage->minLocal = pBt->minLocal; |
| 53653 | }else{ |
| 53654 | return SQLITE_CORRUPT_BKPT; |
| 53655 | } |
| 53656 | pPage->max1bytePayload = pBt->max1bytePayload; |
| 53657 | return SQLITE_OK; |
| 53658 | } |
| @@ -53688,25 +54028,37 @@ | |
| 53688 | |
| 53689 | pBt = pPage->pBt; |
| 53690 | |
| 53691 | hdr = pPage->hdrOffset; |
| 53692 | data = pPage->aData; |
| 53693 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
| 53694 | assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 53695 | pPage->maskPage = (u16)(pBt->pageSize - 1); |
| 53696 | pPage->nOverflow = 0; |
| 53697 | usableSize = pBt->usableSize; |
| 53698 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 53699 | pPage->aDataEnd = &data[usableSize]; |
| 53700 | pPage->aCellIdx = &data[cellOffset]; |
| 53701 | top = get2byteNotZero(&data[hdr+5]); |
| 53702 | pPage->nCell = get2byte(&data[hdr+3]); |
| 53703 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 53704 | /* To many cells for a single page. The page must be corrupt */ |
| 53705 | return SQLITE_CORRUPT_BKPT; |
| 53706 | } |
| 53707 | testcase( pPage->nCell==MX_CELL(pBt) ); |
| 53708 | |
| 53709 | /* A malformed database page might cause us to read past the end |
| 53710 | ** of page when parsing a cell. |
| 53711 | ** |
| 53712 | ** The following block of code checks early to see if a cell extends |
| @@ -53736,17 +54088,24 @@ | |
| 53736 | } |
| 53737 | if( !pPage->leaf ) iCellLast++; |
| 53738 | } |
| 53739 | #endif |
| 53740 | |
| 53741 | /* Compute the total free space on the page */ |
| 53742 | pc = get2byte(&data[hdr+1]); |
| 53743 | nFree = data[hdr+7] + top; |
| 53744 | while( pc>0 ){ |
| 53745 | u16 next, size; |
| 53746 | if( pc<iCellFirst || pc>iCellLast ){ |
| 53747 | /* Start of free block is off the page */ |
| 53748 | return SQLITE_CORRUPT_BKPT; |
| 53749 | } |
| 53750 | next = get2byte(&data[pc]); |
| 53751 | size = get2byte(&data[pc+2]); |
| 53752 | if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){ |
| @@ -54148,10 +54507,13 @@ | |
| 54148 | pBt->pPage1 = 0; |
| 54149 | if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY; |
| 54150 | #ifdef SQLITE_SECURE_DELETE |
| 54151 | pBt->btsFlags |= BTS_SECURE_DELETE; |
| 54152 | #endif |
| 54153 | pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); |
| 54154 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 54155 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
| 54156 | pBt->pageSize = 0; |
| 54157 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| @@ -54166,10 +54528,13 @@ | |
| 54166 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 54167 | } |
| 54168 | #endif |
| 54169 | nReserve = 0; |
| 54170 | }else{ |
| 54171 | nReserve = zDbHeader[20]; |
| 54172 | pBt->btsFlags |= BTS_PAGESIZE_FIXED; |
| 54173 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 54174 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 54175 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| @@ -54675,10 +55040,13 @@ | |
| 54675 | if( nPage>0 ){ |
| 54676 | u32 pageSize; |
| 54677 | u32 usableSize; |
| 54678 | u8 *page1 = pPage1->aData; |
| 54679 | rc = SQLITE_NOTADB; |
| 54680 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
| 54681 | goto page1_init_failed; |
| 54682 | } |
| 54683 | |
| 54684 | #ifdef SQLITE_OMIT_WAL |
| @@ -54715,26 +55083,39 @@ | |
| 54715 | } |
| 54716 | rc = SQLITE_NOTADB; |
| 54717 | } |
| 54718 | #endif |
| 54719 | |
| 54720 | /* The maximum embedded fraction must be exactly 25%. And the minimum |
| 54721 | ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. |
| 54722 | ** The original design allowed these amounts to vary, but as of |
| 54723 | ** version 3.6.0, we require them to be fixed. |
| 54724 | */ |
| 54725 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 54726 | goto page1_init_failed; |
| 54727 | } |
| 54728 | pageSize = (page1[16]<<8) | (page1[17]<<16); |
| 54729 | if( ((pageSize-1)&pageSize)!=0 |
| 54730 | || pageSize>SQLITE_MAX_PAGE_SIZE |
| 54731 | || pageSize<=256 |
| 54732 | ){ |
| 54733 | goto page1_init_failed; |
| 54734 | } |
| 54735 | assert( (pageSize & 7)==0 ); |
| 54736 | usableSize = pageSize - page1[20]; |
| 54737 | if( (u32)pageSize!=pBt->pageSize ){ |
| 54738 | /* After reading the first page of the database assuming a page size |
| 54739 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 54740 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| @@ -54751,10 +55132,13 @@ | |
| 54751 | } |
| 54752 | if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){ |
| 54753 | rc = SQLITE_CORRUPT_BKPT; |
| 54754 | goto page1_init_failed; |
| 54755 | } |
| 54756 | if( usableSize<480 ){ |
| 54757 | goto page1_init_failed; |
| 54758 | } |
| 54759 | pBt->pageSize = pageSize; |
| 54760 | pBt->usableSize = usableSize; |
| @@ -57328,10 +57712,12 @@ | |
| 57328 | |
| 57329 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 57330 | assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) ); |
| 57331 | pPage1 = pBt->pPage1; |
| 57332 | mxPage = btreePagecount(pBt); |
| 57333 | n = get4byte(&pPage1->aData[36]); |
| 57334 | testcase( n==mxPage-1 ); |
| 57335 | if( n>=mxPage ){ |
| 57336 | return SQLITE_CORRUPT_BKPT; |
| 57337 | } |
| @@ -57374,12 +57760,18 @@ | |
| 57374 | ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT) |
| 57375 | */ |
| 57376 | do { |
| 57377 | pPrevTrunk = pTrunk; |
| 57378 | if( pPrevTrunk ){ |
| 57379 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
| 57380 | }else{ |
| 57381 | iTrunk = get4byte(&pPage1->aData[32]); |
| 57382 | } |
| 57383 | testcase( iTrunk==mxPage ); |
| 57384 | if( iTrunk>mxPage ){ |
| 57385 | rc = SQLITE_CORRUPT_BKPT; |
| @@ -57390,12 +57782,13 @@ | |
| 57390 | pTrunk = 0; |
| 57391 | goto end_allocate_page; |
| 57392 | } |
| 57393 | assert( pTrunk!=0 ); |
| 57394 | assert( pTrunk->aData!=0 ); |
| 57395 | |
| 57396 | k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */ |
| 57397 | if( k==0 && !searchList ){ |
| 57398 | /* The trunk has no leaves and the list is not being searched. |
| 57399 | ** So extract the trunk page itself and use it as the newly |
| 57400 | ** allocated page */ |
| 57401 | assert( pPrevTrunk==0 ); |
| @@ -57709,10 +58102,15 @@ | |
| 57709 | ** to maintain backwards compatibility with older versions of SQLite, |
| 57710 | ** we will continue to restrict the number of entries to usableSize/4 - 8 |
| 57711 | ** for now. At some point in the future (once everyone has upgraded |
| 57712 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 57713 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
| 57714 | */ |
| 57715 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
| 57716 | if( rc==SQLITE_OK ){ |
| 57717 | put4byte(&pTrunk->aData[4], nLeaf+1); |
| 57718 | put4byte(&pTrunk->aData[8+nLeaf*4], iPage); |
| @@ -58060,13 +58458,21 @@ | |
| 58060 | if( rc ){ |
| 58061 | *pRC = rc; |
| 58062 | return; |
| 58063 | } |
| 58064 | pPage->nCell--; |
| 58065 | memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); |
| 58066 | put2byte(&data[hdr+3], pPage->nCell); |
| 58067 | pPage->nFree += 2; |
| 58068 | } |
| 58069 | |
| 58070 | /* |
| 58071 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 58072 | ** content of the cell. |
| @@ -58157,49 +58563,275 @@ | |
| 58157 | #endif |
| 58158 | } |
| 58159 | } |
| 58160 | |
| 58161 | /* |
| 58162 | ** Add a list of cells to a page. The page should be initially empty. |
| 58163 | ** The cells are guaranteed to fit on the page. |
| 58164 | */ |
| 58165 | static void assemblePage( |
| 58166 | MemPage *pPage, /* The page to be assembled */ |
| 58167 | int nCell, /* The number of cells to add to this page */ |
| 58168 | u8 **apCell, /* Pointers to cell bodies */ |
| 58169 | u16 *aSize /* Sizes of the cells */ |
| 58170 | ){ |
| 58171 | int i; /* Loop counter */ |
| 58172 | u8 *pCellptr; /* Address of next cell pointer */ |
| 58173 | int cellbody; /* Address of next cell body */ |
| 58174 | u8 * const data = pPage->aData; /* Pointer to data for pPage */ |
| 58175 | const int hdr = pPage->hdrOffset; /* Offset of header on pPage */ |
| 58176 | const int nUsable = pPage->pBt->usableSize; /* Usable size of page */ |
| 58177 | |
| 58178 | assert( pPage->nOverflow==0 ); |
| 58179 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 58180 | assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt) |
| 58181 | && (int)MX_CELL(pPage->pBt)<=10921); |
| 58182 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 58183 | |
| 58184 | /* Check that the page has just been zeroed by zeroPage() */ |
| 58185 | assert( pPage->nCell==0 ); |
| 58186 | assert( get2byteNotZero(&data[hdr+5])==nUsable ); |
| 58187 | |
| 58188 | pCellptr = &pPage->aCellIdx[nCell*2]; |
| 58189 | cellbody = nUsable; |
| 58190 | for(i=nCell-1; i>=0; i--){ |
| 58191 | u16 sz = aSize[i]; |
| 58192 | pCellptr -= 2; |
| 58193 | cellbody -= sz; |
| 58194 | put2byte(pCellptr, cellbody); |
| 58195 | memcpy(&data[cellbody], apCell[i], sz); |
| 58196 | } |
| 58197 | put2byte(&data[hdr+3], nCell); |
| 58198 | put2byte(&data[hdr+5], cellbody); |
| 58199 | pPage->nFree -= (nCell*2 + nUsable - cellbody); |
| 58200 | pPage->nCell = (u16)nCell; |
| 58201 | } |
| 58202 | |
| 58203 | /* |
| 58204 | ** The following parameters determine how many adjacent pages get involved |
| 58205 | ** in a balancing operation. NN is the number of neighbors on either side |
| @@ -58267,11 +58899,12 @@ | |
| 58267 | u8 *pStop; |
| 58268 | |
| 58269 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
| 58270 | assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); |
| 58271 | zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); |
| 58272 | assemblePage(pNew, 1, &pCell, &szCell); |
| 58273 | |
| 58274 | /* If this is an auto-vacuum database, update the pointer map |
| 58275 | ** with entries for the new page, and any pointer from the |
| 58276 | ** cell on the page to an overflow page. If either of these |
| 58277 | ** operations fails, the return code is set, but the contents |
| @@ -58486,21 +59119,26 @@ | |
| 58486 | int subtotal; /* Subtotal of bytes in cells on one page */ |
| 58487 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
| 58488 | int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ |
| 58489 | int szScratch; /* Size of scratch memory requested */ |
| 58490 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 58491 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
| 58492 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 58493 | u8 *pRight; /* Location in parent of right-sibling pointer */ |
| 58494 | u8 *apDiv[NB-1]; /* Divider cells in pParent */ |
| 58495 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 58496 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
| 58497 | u8 **apCell = 0; /* All cells begin balanced */ |
| 58498 | u16 *szCell; /* Local size of all cells in apCell[] */ |
| 58499 | u8 *aSpace1; /* Space for copies of dividers cells */ |
| 58500 | Pgno pgno; /* Temp var to store a page number in */ |
| 58501 | |
| 58502 | pBt = pParent->pBt; |
| 58503 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 58504 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 58505 | |
| 58506 | #if 0 |
| @@ -58605,16 +59243,18 @@ | |
| 58605 | nMaxCells = (nMaxCells + 3)&~3; |
| 58606 | |
| 58607 | /* |
| 58608 | ** Allocate space for memory structures |
| 58609 | */ |
| 58610 | k = pBt->pageSize + ROUND8(sizeof(MemPage)); |
| 58611 | szScratch = |
| 58612 | nMaxCells*sizeof(u8*) /* apCell */ |
| 58613 | + nMaxCells*sizeof(u16) /* szCell */ |
| 58614 | + pBt->pageSize /* aSpace1 */ |
| 58615 | + k*nOld; /* Page copies (apCopy) */ |
| 58616 | apCell = sqlite3ScratchMalloc( szScratch ); |
| 58617 | if( apCell==0 ){ |
| 58618 | rc = SQLITE_NOMEM; |
| 58619 | goto balance_cleanup; |
| 58620 | } |
| @@ -58623,12 +59263,12 @@ | |
| 58623 | assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); |
| 58624 | |
| 58625 | /* |
| 58626 | ** Load pointers to all cells on sibling pages and the divider cells |
| 58627 | ** into the local apCell[] array. Make copies of the divider cells |
| 58628 | ** into space obtained from aSpace1[] and remove the divider cells |
| 58629 | ** from pParent. |
| 58630 | ** |
| 58631 | ** If the siblings are on leaf pages, then the child pointers of the |
| 58632 | ** divider cells are stripped from the cells before they are copied |
| 58633 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
| 58634 | ** child pointers. If siblings are not leaves, then all cell in |
| @@ -58640,19 +59280,11 @@ | |
| 58640 | */ |
| 58641 | leafCorrection = apOld[0]->leaf*4; |
| 58642 | leafData = apOld[0]->intKeyLeaf; |
| 58643 | for(i=0; i<nOld; i++){ |
| 58644 | int limit; |
| 58645 | |
| 58646 | /* Before doing anything else, take a copy of the i'th original sibling |
| 58647 | ** The rest of this function will use data from the copies rather |
| 58648 | ** that the original pages since the original pages will be in the |
| 58649 | ** process of being overwritten. */ |
| 58650 | MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i]; |
| 58651 | memcpy(pOld, apOld[i], sizeof(MemPage)); |
| 58652 | pOld->aData = (void*)&pOld[1]; |
| 58653 | memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize); |
| 58654 | |
| 58655 | limit = pOld->nCell+pOld->nOverflow; |
| 58656 | if( pOld->nOverflow>0 ){ |
| 58657 | for(j=0; j<limit; j++){ |
| 58658 | assert( nCell<nMaxCells ); |
| @@ -58669,10 +59301,11 @@ | |
| 58669 | apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j); |
| 58670 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
| 58671 | nCell++; |
| 58672 | } |
| 58673 | } |
| 58674 | if( i<nOld-1 && !leafData){ |
| 58675 | u16 sz = (u16)szNew[i]; |
| 58676 | u8 *pTemp; |
| 58677 | assert( nCell<nMaxCells ); |
| 58678 | szCell[nCell] = sz; |
| @@ -58720,11 +59353,11 @@ | |
| 58720 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
| 58721 | for(subtotal=k=i=0; i<nCell; i++){ |
| 58722 | assert( i<nMaxCells ); |
| 58723 | subtotal += szCell[i] + 2; |
| 58724 | if( subtotal > usableSpace ){ |
| 58725 | szNew[k] = subtotal - szCell[i]; |
| 58726 | cntNew[k] = i; |
| 58727 | if( leafData ){ i--; } |
| 58728 | subtotal = 0; |
| 58729 | k++; |
| 58730 | if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
| @@ -58734,13 +59367,14 @@ | |
| 58734 | cntNew[k] = nCell; |
| 58735 | k++; |
| 58736 | |
| 58737 | /* |
| 58738 | ** The packing computed by the previous block is biased toward the siblings |
| 58739 | ** on the left side. The left siblings are always nearly full, while the |
| 58740 | ** right-most sibling might be nearly empty. This block of code attempts |
| 58741 | ** to adjust the packing of siblings to get a better balance. |
| 58742 | ** |
| 58743 | ** This adjustment is more than an optimization. The packing above might |
| 58744 | ** be so out of balance as to be illegal. For example, the right-most |
| 58745 | ** sibling might be completely empty. This adjustment is not optional. |
| 58746 | */ |
| @@ -58765,26 +59399,22 @@ | |
| 58765 | } |
| 58766 | szNew[i] = szRight; |
| 58767 | szNew[i-1] = szLeft; |
| 58768 | } |
| 58769 | |
| 58770 | /* Either we found one or more cells (cntnew[0])>0) or pPage is |
| 58771 | ** a virtual root page. A virtual root page is when the real root |
| 58772 | ** page is page 1 and we are the only child of that page. |
| 58773 | ** |
| 58774 | ** UPDATE: The assert() below is not necessarily true if the database |
| 58775 | ** file is corrupt. The corruption will be detected and reported later |
| 58776 | ** in this procedure so there is no need to act upon it now. |
| 58777 | */ |
| 58778 | #if 0 |
| 58779 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
| 58780 | #endif |
| 58781 | |
| 58782 | TRACE(("BALANCE: old: %d %d %d ", |
| 58783 | apOld[0]->pgno, |
| 58784 | nOld>=2 ? apOld[1]->pgno : 0, |
| 58785 | nOld>=3 ? apOld[2]->pgno : 0 |
| 58786 | )); |
| 58787 | |
| 58788 | /* |
| 58789 | ** Allocate k new pages. Reuse old pages where possible. |
| 58790 | */ |
| @@ -58803,12 +59433,14 @@ | |
| 58803 | if( rc ) goto balance_cleanup; |
| 58804 | }else{ |
| 58805 | assert( i>0 ); |
| 58806 | rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0); |
| 58807 | if( rc ) goto balance_cleanup; |
| 58808 | apNew[i] = pNew; |
| 58809 | nNew++; |
| 58810 | |
| 58811 | /* Set the pointer-map entry for the new sibling page. */ |
| 58812 | if( ISAUTOVACUUM ){ |
| 58813 | ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); |
| 58814 | if( rc!=SQLITE_OK ){ |
| @@ -58816,139 +59448,247 @@ | |
| 58816 | } |
| 58817 | } |
| 58818 | } |
| 58819 | } |
| 58820 | |
| 58821 | /* Free any old pages that were not reused as new pages. |
| 58822 | */ |
| 58823 | while( i<nOld ){ |
| 58824 | freePage(apOld[i], &rc); |
| 58825 | if( rc ) goto balance_cleanup; |
| 58826 | releasePage(apOld[i]); |
| 58827 | apOld[i] = 0; |
| 58828 | i++; |
| 58829 | } |
| 58830 | |
| 58831 | /* |
| 58832 | ** Put the new pages in ascending order. This helps to |
| 58833 | ** keep entries in the disk file in order so that a scan |
| 58834 | ** of the table is a linear scan through the file. That |
| 58835 | ** in turn helps the operating system to deliver pages |
| 58836 | ** from the disk more rapidly. |
| 58837 | ** |
| 58838 | ** An O(n^2) insertion sort algorithm is used, but since |
| 58839 | ** n is never more than NB (a small constant), that should |
| 58840 | ** not be a problem. |
| 58841 | ** |
| 58842 | ** When NB==3, this one optimization makes the database |
| 58843 | ** about 25% faster for large insertions and deletions. |
| 58844 | */ |
| 58845 | for(i=0; i<k-1; i++){ |
| 58846 | int minV = apNew[i]->pgno; |
| 58847 | int minI = i; |
| 58848 | for(j=i+1; j<k; j++){ |
| 58849 | if( apNew[j]->pgno<(unsigned)minV ){ |
| 58850 | minI = j; |
| 58851 | minV = apNew[j]->pgno; |
| 58852 | } |
| 58853 | } |
| 58854 | if( minI>i ){ |
| 58855 | MemPage *pT; |
| 58856 | pT = apNew[i]; |
| 58857 | apNew[i] = apNew[minI]; |
| 58858 | apNew[minI] = pT; |
| 58859 | } |
| 58860 | } |
| 58861 | TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n", |
| 58862 | apNew[0]->pgno, szNew[0], |
| 58863 | nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, |
| 58864 | nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, |
| 58865 | nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, |
| 58866 | nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0)); |
| 58867 | |
| 58868 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 58869 | put4byte(pRight, apNew[nNew-1]->pgno); |
| 58870 | |
| 58871 | /* |
| 58872 | ** Evenly distribute the data in apCell[] across the new pages. |
| 58873 | ** Insert divider cells into pParent as necessary. |
| 58874 | */ |
| 58875 | j = 0; |
| 58876 | for(i=0; i<nNew; i++){ |
| 58877 | /* Assemble the new sibling page. */ |
| 58878 | MemPage *pNew = apNew[i]; |
| 58879 | assert( j<nMaxCells ); |
| 58880 | zeroPage(pNew, pageFlags); |
| 58881 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
| 58882 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
| 58883 | assert( pNew->nOverflow==0 ); |
| 58884 | |
| 58885 | j = cntNew[i]; |
| 58886 | |
| 58887 | /* If the sibling page assembled above was not the right-most sibling, |
| 58888 | ** insert a divider cell into the parent page. |
| 58889 | */ |
| 58890 | assert( i<nNew-1 || j==nCell ); |
| 58891 | if( j<nCell ){ |
| 58892 | u8 *pCell; |
| 58893 | u8 *pTemp; |
| 58894 | int sz; |
| 58895 | |
| 58896 | assert( j<nMaxCells ); |
| 58897 | pCell = apCell[j]; |
| 58898 | sz = szCell[j] + leafCorrection; |
| 58899 | pTemp = &aOvflSpace[iOvflSpace]; |
| 58900 | if( !pNew->leaf ){ |
| 58901 | memcpy(&pNew->aData[8], pCell, 4); |
| 58902 | }else if( leafData ){ |
| 58903 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
| 58904 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 58905 | ** cell consists of the integer key for the right-most cell of |
| 58906 | ** the sibling-page assembled above only. |
| 58907 | */ |
| 58908 | CellInfo info; |
| 58909 | j--; |
| 58910 | btreeParseCellPtr(pNew, apCell[j], &info); |
| 58911 | pCell = pTemp; |
| 58912 | sz = 4 + putVarint(&pCell[4], info.nKey); |
| 58913 | pTemp = 0; |
| 58914 | }else{ |
| 58915 | pCell -= 4; |
| 58916 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
| 58917 | ** previously stored on a leaf node, and its reported size was 4 |
| 58918 | ** bytes, then it may actually be smaller than this |
| 58919 | ** (see btreeParseCellPtr(), 4 bytes is the minimum size of |
| 58920 | ** any cell). But it is important to pass the correct size to |
| 58921 | ** insertCell(), so reparse the cell now. |
| 58922 | ** |
| 58923 | ** Note that this can never happen in an SQLite data file, as all |
| 58924 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 58925 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 58926 | */ |
| 58927 | if( szCell[j]==4 ){ |
| 58928 | assert(leafCorrection==4); |
| 58929 | sz = cellSizePtr(pParent, pCell); |
| 58930 | } |
| 58931 | } |
| 58932 | iOvflSpace += sz; |
| 58933 | assert( sz<=pBt->maxLocal+23 ); |
| 58934 | assert( iOvflSpace <= (int)pBt->pageSize ); |
| 58935 | insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc); |
| 58936 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
| 58937 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 58938 | |
| 58939 | j++; |
| 58940 | nxDiv++; |
| 58941 | } |
| 58942 | } |
| 58943 | assert( j==nCell ); |
| 58944 | assert( nOld>0 ); |
| 58945 | assert( nNew>0 ); |
| 58946 | if( (pageFlags & PTF_LEAF)==0 ){ |
| 58947 | u8 *zChild = &apCopy[nOld-1]->aData[8]; |
| 58948 | memcpy(&apNew[nNew-1]->aData[8], zChild, 4); |
| 58949 | } |
| 58950 | |
| 58951 | if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ |
| 58952 | /* The root page of the b-tree now contains no cells. The only sibling |
| 58953 | ** page is the right-child of the parent. Copy the contents of the |
| 58954 | ** child page into the parent, decreasing the overall height of the |
| @@ -58957,130 +59697,54 @@ | |
| 58957 | ** |
| 58958 | ** If this is an auto-vacuum database, the call to copyNodeContent() |
| 58959 | ** sets all pointer-map entries corresponding to database image pages |
| 58960 | ** for which the pointer is stored within the content being copied. |
| 58961 | ** |
| 58962 | ** The second assert below verifies that the child page is defragmented |
| 58963 | ** (it must be, as it was just reconstructed using assemblePage()). This |
| 58964 | ** is important if the parent page happens to be page 1 of the database |
| 58965 | ** image. */ |
| 58966 | assert( nNew==1 ); |
| 58967 | assert( apNew[0]->nFree == |
| 58968 | (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) |
| 58969 | ); |
| 58970 | copyNodeContent(apNew[0], pParent, &rc); |
| 58971 | freePage(apNew[0], &rc); |
| 58972 | }else if( ISAUTOVACUUM ){ |
| 58973 | /* Fix the pointer-map entries for all the cells that were shifted around. |
| 58974 | ** There are several different types of pointer-map entries that need to |
| 58975 | ** be dealt with by this routine. Some of these have been set already, but |
| 58976 | ** many have not. The following is a summary: |
| 58977 | ** |
| 58978 | ** 1) The entries associated with new sibling pages that were not |
| 58979 | ** siblings when this function was called. These have already |
| 58980 | ** been set. We don't need to worry about old siblings that were |
| 58981 | ** moved to the free-list - the freePage() code has taken care |
| 58982 | ** of those. |
| 58983 | ** |
| 58984 | ** 2) The pointer-map entries associated with the first overflow |
| 58985 | ** page in any overflow chains used by new divider cells. These |
| 58986 | ** have also already been taken care of by the insertCell() code. |
| 58987 | ** |
| 58988 | ** 3) If the sibling pages are not leaves, then the child pages of |
| 58989 | ** cells stored on the sibling pages may need to be updated. |
| 58990 | ** |
| 58991 | ** 4) If the sibling pages are not internal intkey nodes, then any |
| 58992 | ** overflow pages used by these cells may need to be updated |
| 58993 | ** (internal intkey nodes never contain pointers to overflow pages). |
| 58994 | ** |
| 58995 | ** 5) If the sibling pages are not leaves, then the pointer-map |
| 58996 | ** entries for the right-child pages of each sibling may need |
| 58997 | ** to be updated. |
| 58998 | ** |
| 58999 | ** Cases 1 and 2 are dealt with above by other code. The next |
| 59000 | ** block deals with cases 3 and 4 and the one after that, case 5. Since |
| 59001 | ** setting a pointer map entry is a relatively expensive operation, this |
| 59002 | ** code only sets pointer map entries for child or overflow pages that have |
| 59003 | ** actually moved between pages. */ |
| 59004 | MemPage *pNew = apNew[0]; |
| 59005 | MemPage *pOld = apCopy[0]; |
| 59006 | int nOverflow = pOld->nOverflow; |
| 59007 | int iNextOld = pOld->nCell + nOverflow; |
| 59008 | int iOverflow = (nOverflow ? pOld->aiOvfl[0] : -1); |
| 59009 | j = 0; /* Current 'old' sibling page */ |
| 59010 | k = 0; /* Current 'new' sibling page */ |
| 59011 | for(i=0; i<nCell; i++){ |
| 59012 | int isDivider = 0; |
| 59013 | while( i==iNextOld ){ |
| 59014 | /* Cell i is the cell immediately following the last cell on old |
| 59015 | ** sibling page j. If the siblings are not leaf pages of an |
| 59016 | ** intkey b-tree, then cell i was a divider cell. */ |
| 59017 | assert( j+1 < ArraySize(apCopy) ); |
| 59018 | assert( j+1 < nOld ); |
| 59019 | pOld = apCopy[++j]; |
| 59020 | iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow; |
| 59021 | if( pOld->nOverflow ){ |
| 59022 | nOverflow = pOld->nOverflow; |
| 59023 | iOverflow = i + !leafData + pOld->aiOvfl[0]; |
| 59024 | } |
| 59025 | isDivider = !leafData; |
| 59026 | } |
| 59027 | |
| 59028 | assert(nOverflow>0 || iOverflow<i ); |
| 59029 | assert(nOverflow<2 || pOld->aiOvfl[0]==pOld->aiOvfl[1]-1); |
| 59030 | assert(nOverflow<3 || pOld->aiOvfl[1]==pOld->aiOvfl[2]-1); |
| 59031 | if( i==iOverflow ){ |
| 59032 | isDivider = 1; |
| 59033 | if( (--nOverflow)>0 ){ |
| 59034 | iOverflow++; |
| 59035 | } |
| 59036 | } |
| 59037 | |
| 59038 | if( i==cntNew[k] ){ |
| 59039 | /* Cell i is the cell immediately following the last cell on new |
| 59040 | ** sibling page k. If the siblings are not leaf pages of an |
| 59041 | ** intkey b-tree, then cell i is a divider cell. */ |
| 59042 | pNew = apNew[++k]; |
| 59043 | if( !leafData ) continue; |
| 59044 | } |
| 59045 | assert( j<nOld ); |
| 59046 | assert( k<nNew ); |
| 59047 | |
| 59048 | /* If the cell was originally divider cell (and is not now) or |
| 59049 | ** an overflow cell, or if the cell was located on a different sibling |
| 59050 | ** page before the balancing, then the pointer map entries associated |
| 59051 | ** with any child or overflow pages need to be updated. */ |
| 59052 | if( isDivider || pOld->pgno!=pNew->pgno ){ |
| 59053 | if( !leafCorrection ){ |
| 59054 | ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc); |
| 59055 | } |
| 59056 | if( szCell[i]>pNew->minLocal ){ |
| 59057 | ptrmapPutOvflPtr(pNew, apCell[i], &rc); |
| 59058 | } |
| 59059 | } |
| 59060 | } |
| 59061 | |
| 59062 | if( !leafCorrection ){ |
| 59063 | for(i=0; i<nNew; i++){ |
| 59064 | u32 key = get4byte(&apNew[i]->aData[8]); |
| 59065 | ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); |
| 59066 | } |
| 59067 | } |
| 59068 | |
| 59069 | #if 0 |
| 59070 | /* The ptrmapCheckPages() contains assert() statements that verify that |
| 59071 | ** all pointer map pages are set correctly. This is helpful while |
| 59072 | ** debugging. This is usually disabled because a corrupt database may |
| 59073 | ** cause an assert() statement to fail. */ |
| 59074 | ptrmapCheckPages(apNew, nNew); |
| 59075 | ptrmapCheckPages(&pParent, 1); |
| 59076 | #endif |
| 59077 | } |
| 59078 | |
| 59079 | assert( pParent->isInit ); |
| 59080 | TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
| 59081 | nOld, nNew, nCell)); |
| 59082 | |
| 59083 | /* |
| 59084 | ** Cleanup before returning. |
| 59085 | */ |
| 59086 | balance_cleanup: |
| @@ -60461,12 +61125,18 @@ | |
| 60461 | }else{ |
| 60462 | int contentOffset = get2byteNotZero(&data[hdr+5]); |
| 60463 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
| 60464 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 60465 | memset(hit, 1, contentOffset); |
| 60466 | nCell = get2byte(&data[hdr+3]); |
| 60467 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 60468 | for(i=0; i<nCell; i++){ |
| 60469 | int pc = get2byte(&data[cellStart+i*2]); |
| 60470 | u32 size = 65536; |
| 60471 | int j; |
| 60472 | if( pc<=usableSize-4 ){ |
| @@ -60478,18 +61148,27 @@ | |
| 60478 | "Corruption detected in cell %d on page %d",i,iPage); |
| 60479 | }else{ |
| 60480 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 60481 | } |
| 60482 | } |
| 60483 | i = get2byte(&data[hdr+1]); |
| 60484 | while( i>0 ){ |
| 60485 | int size, j; |
| 60486 | assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 60487 | size = get2byte(&data[i+2]); |
| 60488 | assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */ |
| 60489 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 60490 | j = get2byte(&data[i]); |
| 60491 | assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ |
| 60492 | assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 60493 | i = j; |
| 60494 | } |
| 60495 | for(i=cnt=0; i<usableSize; i++){ |
| @@ -60499,10 +61178,15 @@ | |
| 60499 | checkAppendMsg(pCheck, |
| 60500 | "Multiple uses for byte %d of page %d", i, iPage); |
| 60501 | break; |
| 60502 | } |
| 60503 | } |
| 60504 | if( cnt!=data[hdr+7] ){ |
| 60505 | checkAppendMsg(pCheck, |
| 60506 | "Fragmentation of %d bytes reported as %d on page %d", |
| 60507 | cnt, data[hdr+7], iPage); |
| 60508 | } |
| @@ -60902,10 +61586,15 @@ | |
| 60902 | */ |
| 60903 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){ |
| 60904 | return (p->pBt->btsFlags & BTS_READ_ONLY)!=0; |
| 60905 | } |
| 60906 | |
| 60907 | /************** End of btree.c ***********************************************/ |
| 60908 | /************** Begin file backup.c ******************************************/ |
| 60909 | /* |
| 60910 | ** 2009 January 28 |
| 60911 | ** |
| @@ -61025,10 +61714,24 @@ | |
| 61025 | static int setDestPgsz(sqlite3_backup *p){ |
| 61026 | int rc; |
| 61027 | rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0); |
| 61028 | return rc; |
| 61029 | } |
| 61030 | |
| 61031 | /* |
| 61032 | ** Create an sqlite3_backup process to copy the contents of zSrcDb from |
| 61033 | ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return |
| 61034 | ** a pointer to the new sqlite3_backup object. |
| @@ -61041,10 +61744,17 @@ | |
| 61041 | const char *zDestDb, /* Name of database within pDestDb */ |
| 61042 | sqlite3* pSrcDb, /* Database connection to read from */ |
| 61043 | const char *zSrcDb /* Name of database within pSrcDb */ |
| 61044 | ){ |
| 61045 | sqlite3_backup *p; /* Value to return */ |
| 61046 | |
| 61047 | /* Lock the source database handle. The destination database |
| 61048 | ** handle is not locked in this routine, but it is locked in |
| 61049 | ** sqlite3_backup_step(). The user is required to ensure that no |
| 61050 | ** other thread accesses the destination handle for the duration |
| @@ -61078,16 +61788,19 @@ | |
| 61078 | p->pDestDb = pDestDb; |
| 61079 | p->pSrcDb = pSrcDb; |
| 61080 | p->iNext = 1; |
| 61081 | p->isAttached = 0; |
| 61082 | |
| 61083 | if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){ |
| 61084 | /* One (or both) of the named databases did not exist or an OOM |
| 61085 | ** error was hit. The error has already been written into the |
| 61086 | ** pDestDb handle. All that is left to do here is free the |
| 61087 | ** sqlite3_backup structure. |
| 61088 | */ |
| 61089 | sqlite3_free(p); |
| 61090 | p = 0; |
| 61091 | } |
| 61092 | } |
| 61093 | if( p ){ |
| @@ -61238,10 +61951,13 @@ | |
| 61238 | int rc; |
| 61239 | int destMode; /* Destination journal mode */ |
| 61240 | int pgszSrc = 0; /* Source page size */ |
| 61241 | int pgszDest = 0; /* Destination page size */ |
| 61242 | |
| 61243 | sqlite3_mutex_enter(p->pSrcDb->mutex); |
| 61244 | sqlite3BtreeEnter(p->pSrc); |
| 61245 | if( p->pDestDb ){ |
| 61246 | sqlite3_mutex_enter(p->pDestDb->mutex); |
| 61247 | } |
| @@ -61527,18 +62243,30 @@ | |
| 61527 | /* |
| 61528 | ** Return the number of pages still to be backed up as of the most recent |
| 61529 | ** call to sqlite3_backup_step(). |
| 61530 | */ |
| 61531 | SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){ |
| 61532 | return p->nRemaining; |
| 61533 | } |
| 61534 | |
| 61535 | /* |
| 61536 | ** Return the total number of pages in the source database as of the most |
| 61537 | ** recent call to sqlite3_backup_step(). |
| 61538 | */ |
| 61539 | SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){ |
| 61540 | return p->nPagecount; |
| 61541 | } |
| 61542 | |
| 61543 | /* |
| 61544 | ** This function is called after the contents of page iPage of the |
| @@ -63825,10 +64553,38 @@ | |
| 63825 | } |
| 63826 | p->nOp += nOp; |
| 63827 | } |
| 63828 | return addr; |
| 63829 | } |
| 63830 | |
| 63831 | /* |
| 63832 | ** Change the value of the P1 operand for a specific instruction. |
| 63833 | ** This routine is useful when a large program is loaded from a |
| 63834 | ** static array using sqlite3VdbeAddOpList but we want to make a |
| @@ -64924,10 +65680,13 @@ | |
| 64924 | p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); |
| 64925 | p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); |
| 64926 | p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), |
| 64927 | &zCsr, zEnd, &nByte); |
| 64928 | p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte); |
| 64929 | if( nByte ){ |
| 64930 | p->pFree = sqlite3DbMallocZero(db, nByte); |
| 64931 | } |
| 64932 | zCsr = p->pFree; |
| 64933 | zEnd = &zCsr[nByte]; |
| @@ -64991,10 +65750,13 @@ | |
| 64991 | ** is used, for example, when a trigger sub-program is halted to restore |
| 64992 | ** control to the main program. |
| 64993 | */ |
| 64994 | SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ |
| 64995 | Vdbe *v = pFrame->v; |
| 64996 | v->aOnceFlag = pFrame->aOnceFlag; |
| 64997 | v->nOnceFlag = pFrame->nOnceFlag; |
| 64998 | v->aOp = pFrame->aOp; |
| 64999 | v->nOp = pFrame->nOp; |
| 65000 | v->aMem = pFrame->aMem; |
| @@ -65001,10 +65763,11 @@ | |
| 65001 | v->nMem = pFrame->nMem; |
| 65002 | v->apCsr = pFrame->apCsr; |
| 65003 | v->nCursor = pFrame->nCursor; |
| 65004 | v->db->lastRowid = pFrame->lastRowid; |
| 65005 | v->nChange = pFrame->nChange; |
| 65006 | return pFrame->pc; |
| 65007 | } |
| 65008 | |
| 65009 | /* |
| 65010 | ** Close all cursors. |
| @@ -65568,10 +66331,11 @@ | |
| 65568 | ** so, abort any other statements this handle currently has active. |
| 65569 | */ |
| 65570 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 65571 | sqlite3CloseSavepoints(db); |
| 65572 | db->autoCommit = 1; |
| 65573 | } |
| 65574 | } |
| 65575 | } |
| 65576 | |
| 65577 | /* Check for immediate foreign key violations. */ |
| @@ -65608,18 +66372,20 @@ | |
| 65608 | sqlite3VdbeLeave(p); |
| 65609 | return SQLITE_BUSY; |
| 65610 | }else if( rc!=SQLITE_OK ){ |
| 65611 | p->rc = rc; |
| 65612 | sqlite3RollbackAll(db, SQLITE_OK); |
| 65613 | }else{ |
| 65614 | db->nDeferredCons = 0; |
| 65615 | db->nDeferredImmCons = 0; |
| 65616 | db->flags &= ~SQLITE_DeferFKs; |
| 65617 | sqlite3CommitInternalChanges(db); |
| 65618 | } |
| 65619 | }else{ |
| 65620 | sqlite3RollbackAll(db, SQLITE_OK); |
| 65621 | } |
| 65622 | db->nStatement = 0; |
| 65623 | }else if( eStatementOp==0 ){ |
| 65624 | if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ |
| 65625 | eStatementOp = SAVEPOINT_RELEASE; |
| @@ -65627,10 +66393,11 @@ | |
| 65627 | eStatementOp = SAVEPOINT_ROLLBACK; |
| 65628 | }else{ |
| 65629 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 65630 | sqlite3CloseSavepoints(db); |
| 65631 | db->autoCommit = 1; |
| 65632 | } |
| 65633 | } |
| 65634 | |
| 65635 | /* If eStatementOp is non-zero, then a statement transaction needs to |
| 65636 | ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to |
| @@ -65647,10 +66414,11 @@ | |
| 65647 | p->zErrMsg = 0; |
| 65648 | } |
| 65649 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 65650 | sqlite3CloseSavepoints(db); |
| 65651 | db->autoCommit = 1; |
| 65652 | } |
| 65653 | } |
| 65654 | |
| 65655 | /* If this was an INSERT, UPDATE or DELETE and no statement transaction |
| 65656 | ** has been rolled back, update the database connection change-counter. |
| @@ -65908,10 +66676,16 @@ | |
| 65908 | for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); |
| 65909 | vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 65910 | sqlite3DbFree(db, p->aColName); |
| 65911 | sqlite3DbFree(db, p->zSql); |
| 65912 | sqlite3DbFree(db, p->pFree); |
| 65913 | } |
| 65914 | |
| 65915 | /* |
| 65916 | ** Delete an entire VDBE. |
| 65917 | */ |
| @@ -66066,13 +66840,11 @@ | |
| 66066 | /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
| 66067 | # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) |
| 66068 | i64 i = pMem->u.i; |
| 66069 | u64 u; |
| 66070 | if( i<0 ){ |
| 66071 | if( i<(-MAX_6BYTE) ) return 6; |
| 66072 | /* Previous test prevents: u = -(-9223372036854775808) */ |
| 66073 | u = -i; |
| 66074 | }else{ |
| 66075 | u = i; |
| 66076 | } |
| 66077 | if( u<=127 ){ |
| 66078 | return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1; |
| @@ -66234,14 +67006,18 @@ | |
| 66234 | ){ |
| 66235 | u64 x = FOUR_BYTE_UINT(buf); |
| 66236 | u32 y = FOUR_BYTE_UINT(buf+4); |
| 66237 | x = (x<<32) + y; |
| 66238 | if( serial_type==6 ){ |
| 66239 | pMem->u.i = *(i64*)&x; |
| 66240 | pMem->flags = MEM_Int; |
| 66241 | testcase( pMem->u.i<0 ); |
| 66242 | }else{ |
| 66243 | #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) |
| 66244 | /* Verify that integers and floating point values use the same |
| 66245 | ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is |
| 66246 | ** defined that 64-bit floating point values really are mixed |
| 66247 | ** endian. |
| @@ -66265,39 +67041,50 @@ | |
| 66265 | Mem *pMem /* Memory cell to write value into */ |
| 66266 | ){ |
| 66267 | switch( serial_type ){ |
| 66268 | case 10: /* Reserved for future use */ |
| 66269 | case 11: /* Reserved for future use */ |
| 66270 | case 0: { /* NULL */ |
| 66271 | pMem->flags = MEM_Null; |
| 66272 | break; |
| 66273 | } |
| 66274 | case 1: { /* 1-byte signed integer */ |
| 66275 | pMem->u.i = ONE_BYTE_INT(buf); |
| 66276 | pMem->flags = MEM_Int; |
| 66277 | testcase( pMem->u.i<0 ); |
| 66278 | return 1; |
| 66279 | } |
| 66280 | case 2: { /* 2-byte signed integer */ |
| 66281 | pMem->u.i = TWO_BYTE_INT(buf); |
| 66282 | pMem->flags = MEM_Int; |
| 66283 | testcase( pMem->u.i<0 ); |
| 66284 | return 2; |
| 66285 | } |
| 66286 | case 3: { /* 3-byte signed integer */ |
| 66287 | pMem->u.i = THREE_BYTE_INT(buf); |
| 66288 | pMem->flags = MEM_Int; |
| 66289 | testcase( pMem->u.i<0 ); |
| 66290 | return 3; |
| 66291 | } |
| 66292 | case 4: { /* 4-byte signed integer */ |
| 66293 | pMem->u.i = FOUR_BYTE_INT(buf); |
| 66294 | pMem->flags = MEM_Int; |
| 66295 | testcase( pMem->u.i<0 ); |
| 66296 | return 4; |
| 66297 | } |
| 66298 | case 5: { /* 6-byte signed integer */ |
| 66299 | pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); |
| 66300 | pMem->flags = MEM_Int; |
| 66301 | testcase( pMem->u.i<0 ); |
| 66302 | return 6; |
| 66303 | } |
| @@ -66307,15 +67094,21 @@ | |
| 66307 | ** to avoid having to move the frame pointer in the common case */ |
| 66308 | return serialGet(buf,serial_type,pMem); |
| 66309 | } |
| 66310 | case 8: /* Integer 0 */ |
| 66311 | case 9: { /* Integer 1 */ |
| 66312 | pMem->u.i = serial_type-8; |
| 66313 | pMem->flags = MEM_Int; |
| 66314 | return 0; |
| 66315 | } |
| 66316 | default: { |
| 66317 | static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; |
| 66318 | pMem->z = (char *)buf; |
| 66319 | pMem->n = (serial_type-12)/2; |
| 66320 | pMem->flags = aFlag[serial_type&1]; |
| 66321 | return pMem->n; |
| @@ -68275,15 +69068,23 @@ | |
| 68275 | sqlite3_stmt *pStmt, |
| 68276 | int N, |
| 68277 | const void *(*xFunc)(Mem*), |
| 68278 | int useType |
| 68279 | ){ |
| 68280 | const void *ret = 0; |
| 68281 | Vdbe *p = (Vdbe *)pStmt; |
| 68282 | int n; |
| 68283 | sqlite3 *db = p->db; |
| 68284 | |
| 68285 | assert( db!=0 ); |
| 68286 | n = sqlite3_column_count(pStmt); |
| 68287 | if( N<n && N>=0 ){ |
| 68288 | N += useType*n; |
| 68289 | sqlite3_mutex_enter(db->mutex); |
| @@ -68744,10 +69545,16 @@ | |
| 68744 | ** prepared statement for the database connection. Return NULL if there |
| 68745 | ** are no more. |
| 68746 | */ |
| 68747 | SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ |
| 68748 | sqlite3_stmt *pNext; |
| 68749 | sqlite3_mutex_enter(pDb->mutex); |
| 68750 | if( pStmt==0 ){ |
| 68751 | pNext = (sqlite3_stmt*)pDb->pVdbe; |
| 68752 | }else{ |
| 68753 | pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; |
| @@ -68759,15 +69566,91 @@ | |
| 68759 | /* |
| 68760 | ** Return the value of a status counter for a prepared statement |
| 68761 | */ |
| 68762 | SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ |
| 68763 | Vdbe *pVdbe = (Vdbe*)pStmt; |
| 68764 | u32 v = pVdbe->aCounter[op]; |
| 68765 | if( resetFlag ) pVdbe->aCounter[op] = 0; |
| 68766 | return (int)v; |
| 68767 | } |
| 68768 | |
| 68769 | /************** End of vdbeapi.c *********************************************/ |
| 68770 | /************** Begin file vdbetrace.c ***************************************/ |
| 68771 | /* |
| 68772 | ** 2009 November 25 |
| 68773 | ** |
| @@ -69649,10 +70532,13 @@ | |
| 69649 | #ifdef VDBE_PROFILE |
| 69650 | start = sqlite3Hwtime(); |
| 69651 | #endif |
| 69652 | nVmStep++; |
| 69653 | pOp = &aOp[pc]; |
| 69654 | |
| 69655 | /* Only allow tracing if SQLITE_DEBUG is defined. |
| 69656 | */ |
| 69657 | #ifdef SQLITE_DEBUG |
| 69658 | if( db->flags & SQLITE_VdbeTrace ){ |
| @@ -71674,11 +72560,14 @@ | |
| 71674 | testcase( serial_type==127 ); |
| 71675 | testcase( serial_type==128 ); |
| 71676 | nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); |
| 71677 | }while( (--pRec)>=pData0 ); |
| 71678 | |
| 71679 | /* Add the initial header varint and total the size */ |
| 71680 | testcase( nHdr==126 ); |
| 71681 | testcase( nHdr==127 ); |
| 71682 | if( nHdr<=126 ){ |
| 71683 | /* The common case */ |
| 71684 | nHdr += 1; |
| @@ -71708,11 +72597,15 @@ | |
| 71708 | j = nHdr; |
| 71709 | assert( pData0<=pLast ); |
| 71710 | pRec = pData0; |
| 71711 | do{ |
| 71712 | serial_type = pRec->uTemp; |
| 71713 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
| 71714 | j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ |
| 71715 | }while( (++pRec)<=pLast ); |
| 71716 | assert( i==nHdr ); |
| 71717 | assert( j==nByte ); |
| 71718 | |
| @@ -72843,14 +73736,14 @@ | |
| 72843 | } |
| 72844 | pIdxKey = &r; |
| 72845 | }else{ |
| 72846 | pIdxKey = sqlite3VdbeAllocUnpackedRecord( |
| 72847 | pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree |
| 72848 | ); |
| 72849 | if( pIdxKey==0 ) goto no_mem; |
| 72850 | assert( pIn3->flags & MEM_Blob ); |
| 72851 | assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */ |
| 72852 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
| 72853 | } |
| 72854 | pIdxKey->default_rc = 0; |
| 72855 | if( pOp->opcode==OP_NoConflict ){ |
| 72856 | /* For the OP_NoConflict opcode, take the jump if any of the |
| @@ -73540,13 +74433,13 @@ | |
| 73540 | } |
| 73541 | /* Opcode: Rewind P1 P2 * * * |
| 73542 | ** |
| 73543 | ** The next use of the Rowid or Column or Next instruction for P1 |
| 73544 | ** will refer to the first entry in the database table or index. |
| 73545 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 73546 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 73547 | ** to the following instruction. |
| 73548 | ** |
| 73549 | ** This opcode leaves the cursor configured to move in forward order, |
| 73550 | ** from the beginning toward the end. In other words, the cursor is |
| 73551 | ** configured to use Next, not Prev. |
| 73552 | */ |
| @@ -74458,10 +75351,13 @@ | |
| 74458 | pFrame->aOp = p->aOp; |
| 74459 | pFrame->nOp = p->nOp; |
| 74460 | pFrame->token = pProgram->token; |
| 74461 | pFrame->aOnceFlag = p->aOnceFlag; |
| 74462 | pFrame->nOnceFlag = p->nOnceFlag; |
| 74463 | |
| 74464 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 74465 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
| 74466 | pMem->flags = MEM_Undefined; |
| 74467 | pMem->db = db; |
| @@ -74475,10 +75371,11 @@ | |
| 74475 | |
| 74476 | p->nFrame++; |
| 74477 | pFrame->pParent = p->pFrame; |
| 74478 | pFrame->lastRowid = lastRowid; |
| 74479 | pFrame->nChange = p->nChange; |
| 74480 | p->nChange = 0; |
| 74481 | p->pFrame = pFrame; |
| 74482 | p->aMem = aMem = &VdbeFrameMem(pFrame)[-1]; |
| 74483 | p->nMem = pFrame->nChildMem; |
| 74484 | p->nCursor = (u16)pFrame->nChildCsr; |
| @@ -74485,10 +75382,13 @@ | |
| 74485 | p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; |
| 74486 | p->aOp = aOp = pProgram->aOp; |
| 74487 | p->nOp = pProgram->nOp; |
| 74488 | p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor]; |
| 74489 | p->nOnceFlag = pProgram->nOnce; |
| 74490 | pc = -1; |
| 74491 | memset(p->aOnceFlag, 0, p->nOnceFlag); |
| 74492 | |
| 74493 | break; |
| 74494 | } |
| @@ -75673,10 +76573,15 @@ | |
| 75673 | char *zErr = 0; |
| 75674 | Table *pTab; |
| 75675 | Parse *pParse = 0; |
| 75676 | Incrblob *pBlob = 0; |
| 75677 | |
| 75678 | flags = !!flags; /* flags = (flags ? 1 : 0); */ |
| 75679 | *ppBlob = 0; |
| 75680 | |
| 75681 | sqlite3_mutex_enter(db->mutex); |
| 75682 | |
| @@ -75891,11 +76796,10 @@ | |
| 75891 | v = (Vdbe*)p->pStmt; |
| 75892 | |
| 75893 | if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){ |
| 75894 | /* Request is out of range. Return a transient error. */ |
| 75895 | rc = SQLITE_ERROR; |
| 75896 | sqlite3Error(db, SQLITE_ERROR); |
| 75897 | }else if( v==0 ){ |
| 75898 | /* If there is no statement handle, then the blob-handle has |
| 75899 | ** already been invalidated. Return SQLITE_ABORT in this case. |
| 75900 | */ |
| 75901 | rc = SQLITE_ABORT; |
| @@ -75909,14 +76813,14 @@ | |
| 75909 | sqlite3BtreeLeaveCursor(p->pCsr); |
| 75910 | if( rc==SQLITE_ABORT ){ |
| 75911 | sqlite3VdbeFinalize(v); |
| 75912 | p->pStmt = 0; |
| 75913 | }else{ |
| 75914 | db->errCode = rc; |
| 75915 | v->rc = rc; |
| 75916 | } |
| 75917 | } |
| 75918 | rc = sqlite3ApiExit(db, rc); |
| 75919 | sqlite3_mutex_leave(db->mutex); |
| 75920 | return rc; |
| 75921 | } |
| 75922 | |
| @@ -76089,11 +76993,11 @@ | |
| 76089 | ** itself. |
| 76090 | ** |
| 76091 | ** The sorter is running in multi-threaded mode if (a) the library was built |
| 76092 | ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater |
| 76093 | ** than zero, and (b) worker threads have been enabled at runtime by calling |
| 76094 | ** sqlite3_config(SQLITE_CONFIG_WORKER_THREADS, ...). |
| 76095 | ** |
| 76096 | ** When Rewind() is called, any data remaining in memory is flushed to a |
| 76097 | ** final PMA. So at this point the data is stored in some number of sorted |
| 76098 | ** PMAs within temporary files on disk. |
| 76099 | ** |
| @@ -76134,10 +77038,17 @@ | |
| 76134 | */ |
| 76135 | #if 0 |
| 76136 | # define SQLITE_DEBUG_SORTER_THREADS 1 |
| 76137 | #endif |
| 76138 | |
| 76139 | /* |
| 76140 | ** Private objects used by the sorter |
| 76141 | */ |
| 76142 | typedef struct MergeEngine MergeEngine; /* Merge PMAs together */ |
| 76143 | typedef struct PmaReader PmaReader; /* Incrementally read one PMA */ |
| @@ -76832,17 +77743,15 @@ | |
| 76832 | |
| 76833 | if( !sqlite3TempInMemory(db) ){ |
| 76834 | pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz; |
| 76835 | mxCache = db->aDb[0].pSchema->cache_size; |
| 76836 | if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING; |
| 76837 | pSorter->mxPmaSize = mxCache * pgsz; |
| 76838 | |
| 76839 | /* If the application has not configure scratch memory using |
| 76840 | ** SQLITE_CONFIG_SCRATCH then we assume it is OK to do large memory |
| 76841 | ** allocations. If scratch memory has been configured, then assume |
| 76842 | ** large memory allocations should be avoided to prevent heap |
| 76843 | ** fragmentation. |
| 76844 | */ |
| 76845 | if( sqlite3GlobalConfig.pScratch==0 ){ |
| 76846 | assert( pSorter->iMemory==0 ); |
| 76847 | pSorter->nMemory = pgsz; |
| 76848 | pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz); |
| @@ -79210,19 +80119,19 @@ | |
| 79210 | ** |
| 79211 | ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) |
| 79212 | ** is a helper function - a callback for the tree walker. |
| 79213 | */ |
| 79214 | static int incrAggDepth(Walker *pWalker, Expr *pExpr){ |
| 79215 | if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i; |
| 79216 | return WRC_Continue; |
| 79217 | } |
| 79218 | static void incrAggFunctionDepth(Expr *pExpr, int N){ |
| 79219 | if( N>0 ){ |
| 79220 | Walker w; |
| 79221 | memset(&w, 0, sizeof(w)); |
| 79222 | w.xExprCallback = incrAggDepth; |
| 79223 | w.u.i = N; |
| 79224 | sqlite3WalkExpr(&w, pExpr); |
| 79225 | } |
| 79226 | } |
| 79227 | |
| 79228 | /* |
| @@ -79766,11 +80675,11 @@ | |
| 79766 | double r = -1.0; |
| 79767 | if( p->op!=TK_FLOAT ) return -1; |
| 79768 | sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8); |
| 79769 | assert( r>=0.0 ); |
| 79770 | if( r>1.0 ) return -1; |
| 79771 | return (int)(r*1000.0); |
| 79772 | } |
| 79773 | |
| 79774 | /* |
| 79775 | ** This routine is callback for sqlite3WalkExpr(). |
| 79776 | ** |
| @@ -79898,11 +80807,11 @@ | |
| 79898 | ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand for |
| 79899 | ** likelihood(X,0.9375). |
| 79900 | ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to |
| 79901 | ** likelihood(X,0.9375). */ |
| 79902 | /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */ |
| 79903 | pExpr->iTable = pDef->zName[0]=='u' ? 62 : 938; |
| 79904 | } |
| 79905 | } |
| 79906 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 79907 | auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); |
| 79908 | if( auth!=SQLITE_OK ){ |
| @@ -81855,69 +82764,79 @@ | |
| 81855 | sqlite3DbFree(db, pList->a); |
| 81856 | sqlite3DbFree(db, pList); |
| 81857 | } |
| 81858 | |
| 81859 | /* |
| 81860 | ** These routines are Walker callbacks. Walker.u.pi is a pointer |
| 81861 | ** to an integer. These routines are checking an expression to see |
| 81862 | ** if it is a constant. Set *Walker.u.i to 0 if the expression is |
| 81863 | ** not constant. |
| 81864 | ** |
| 81865 | ** These callback routines are used to implement the following: |
| 81866 | ** |
| 81867 | ** sqlite3ExprIsConstant() pWalker->u.i==1 |
| 81868 | ** sqlite3ExprIsConstantNotJoin() pWalker->u.i==2 |
| 81869 | ** sqlite3ExprIsConstantOrFunction() pWalker->u.i==3 or 4 |
| 81870 | ** |
| 81871 | ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions |
| 81872 | ** in a CREATE TABLE statement. The Walker.u.i value is 4 when parsing |
| 81873 | ** an existing schema and 3 when processing a new statement. A bound |
| 81874 | ** parameter raises an error for new statements, but is silently converted |
| 81875 | ** to NULL for existing schemas. This allows sqlite_master tables that |
| 81876 | ** contain a bound parameter because they were generated by older versions |
| 81877 | ** of SQLite to be parsed by newer versions of SQLite without raising a |
| 81878 | ** malformed schema error. |
| 81879 | */ |
| 81880 | static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ |
| 81881 | |
| 81882 | /* If pWalker->u.i is 2 then any term of the expression that comes from |
| 81883 | ** the ON or USING clauses of a join disqualifies the expression |
| 81884 | ** from being considered constant. */ |
| 81885 | if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 81886 | pWalker->u.i = 0; |
| 81887 | return WRC_Abort; |
| 81888 | } |
| 81889 | |
| 81890 | switch( pExpr->op ){ |
| 81891 | /* Consider functions to be constant if all their arguments are constant |
| 81892 | ** and either pWalker->u.i==3 or 4 or the function as the SQLITE_FUNC_CONST |
| 81893 | ** flag. */ |
| 81894 | case TK_FUNCTION: |
| 81895 | if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){ |
| 81896 | return WRC_Continue; |
| 81897 | } |
| 81898 | /* Fall through */ |
| 81899 | case TK_ID: |
| 81900 | case TK_COLUMN: |
| 81901 | case TK_AGG_FUNCTION: |
| 81902 | case TK_AGG_COLUMN: |
| 81903 | testcase( pExpr->op==TK_ID ); |
| 81904 | testcase( pExpr->op==TK_COLUMN ); |
| 81905 | testcase( pExpr->op==TK_AGG_FUNCTION ); |
| 81906 | testcase( pExpr->op==TK_AGG_COLUMN ); |
| 81907 | pWalker->u.i = 0; |
| 81908 | return WRC_Abort; |
| 81909 | case TK_VARIABLE: |
| 81910 | if( pWalker->u.i==4 ){ |
| 81911 | /* Silently convert bound parameters that appear inside of CREATE |
| 81912 | ** statements into a NULL when parsing the CREATE statement text out |
| 81913 | ** of the sqlite_master table */ |
| 81914 | pExpr->op = TK_NULL; |
| 81915 | }else if( pWalker->u.i==3 ){ |
| 81916 | /* A bound parameter in a CREATE statement that originates from |
| 81917 | ** sqlite3_prepare() causes an error */ |
| 81918 | pWalker->u.i = 0; |
| 81919 | return WRC_Abort; |
| 81920 | } |
| 81921 | /* Fall through */ |
| 81922 | default: |
| 81923 | testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ |
| @@ -81925,57 +82844,68 @@ | |
| 81925 | return WRC_Continue; |
| 81926 | } |
| 81927 | } |
| 81928 | static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ |
| 81929 | UNUSED_PARAMETER(NotUsed); |
| 81930 | pWalker->u.i = 0; |
| 81931 | return WRC_Abort; |
| 81932 | } |
| 81933 | static int exprIsConst(Expr *p, int initFlag){ |
| 81934 | Walker w; |
| 81935 | memset(&w, 0, sizeof(w)); |
| 81936 | w.u.i = initFlag; |
| 81937 | w.xExprCallback = exprNodeIsConstant; |
| 81938 | w.xSelectCallback = selectNodeIsConstant; |
| 81939 | sqlite3WalkExpr(&w, p); |
| 81940 | return w.u.i; |
| 81941 | } |
| 81942 | |
| 81943 | /* |
| 81944 | ** Walk an expression tree. Return 1 if the expression is constant |
| 81945 | ** and 0 if it involves variables or function calls. |
| 81946 | ** |
| 81947 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 81948 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 81949 | ** a constant. |
| 81950 | */ |
| 81951 | SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){ |
| 81952 | return exprIsConst(p, 1); |
| 81953 | } |
| 81954 | |
| 81955 | /* |
| 81956 | ** Walk an expression tree. Return 1 if the expression is constant |
| 81957 | ** that does no originate from the ON or USING clauses of a join. |
| 81958 | ** Return 0 if it involves variables or function calls or terms from |
| 81959 | ** an ON or USING clause. |
| 81960 | */ |
| 81961 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){ |
| 81962 | return exprIsConst(p, 2); |
| 81963 | } |
| 81964 | |
| 81965 | /* |
| 81966 | ** Walk an expression tree. Return 1 if the expression is constant |
| 81967 | ** or a function call with constant arguments. Return and 0 if there |
| 81968 | ** are any variables. |
| 81969 | ** |
| 81970 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 81971 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 81972 | ** a constant. |
| 81973 | */ |
| 81974 | SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ |
| 81975 | assert( isInit==0 || isInit==1 ); |
| 81976 | return exprIsConst(p, 3+isInit); |
| 81977 | } |
| 81978 | |
| 81979 | /* |
| 81980 | ** If the expression p codes a constant integer that is small enough |
| 81981 | ** to fit in a 32-bit integer, return 1 and put the value of the integer |
| @@ -83627,11 +84557,14 @@ | |
| 83627 | target |
| 83628 | )); |
| 83629 | |
| 83630 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 83631 | /* If the column has REAL affinity, it may currently be stored as an |
| 83632 | ** integer. Use OP_RealAffinity to make sure it is really real. */ |
| 83633 | if( pExpr->iColumn>=0 |
| 83634 | && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL |
| 83635 | ){ |
| 83636 | sqlite3VdbeAddOp1(v, OP_RealAffinity, target); |
| 83637 | } |
| @@ -87279,27 +88212,32 @@ | |
| 87279 | aLog[i] = sqlite3LogEst(v); |
| 87280 | #endif |
| 87281 | if( *z==' ' ) z++; |
| 87282 | } |
| 87283 | #ifndef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87284 | assert( pIndex!=0 ); |
| 87285 | #else |
| 87286 | if( pIndex ) |
| 87287 | #endif |
| 87288 | while( z[0] ){ |
| 87289 | if( sqlite3_strglob("unordered*", z)==0 ){ |
| 87290 | pIndex->bUnordered = 1; |
| 87291 | }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){ |
| 87292 | pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3)); |
| 87293 | } |
| 87294 | #ifdef SQLITE_ENABLE_COSTMULT |
| 87295 | else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){ |
| 87296 | pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9)); |
| 87297 | } |
| 87298 | #endif |
| 87299 | while( z[0]!=0 && z[0]!=' ' ) z++; |
| 87300 | while( z[0]==' ' ) z++; |
| 87301 | } |
| 87302 | } |
| 87303 | |
| 87304 | /* |
| 87305 | ** This callback is invoked once for each index when reading the |
| @@ -87421,10 +88359,11 @@ | |
| 87421 | nSample--; |
| 87422 | }else{ |
| 87423 | nRow = pIdx->aiRowEst[0]; |
| 87424 | nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1]; |
| 87425 | } |
| 87426 | |
| 87427 | /* Set nSum to the number of distinct (iCol+1) field prefixes that |
| 87428 | ** occur in the stat4 table for this index. Set sumEq to the sum of |
| 87429 | ** the nEq values for column iCol for the same set (adding the value |
| 87430 | ** only once where there exist duplicate prefixes). */ |
| @@ -87682,11 +88621,11 @@ | |
| 87682 | } |
| 87683 | |
| 87684 | |
| 87685 | /* Load the statistics from the sqlite_stat4 table. */ |
| 87686 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87687 | if( rc==SQLITE_OK ){ |
| 87688 | int lookasideEnabled = db->lookaside.bEnabled; |
| 87689 | db->lookaside.bEnabled = 0; |
| 87690 | rc = loadStat4(db, sInfo.zDatabase); |
| 87691 | db->lookaside.bEnabled = lookasideEnabled; |
| 87692 | } |
| @@ -88364,10 +89303,13 @@ | |
| 88364 | SQLITE_API int sqlite3_set_authorizer( |
| 88365 | sqlite3 *db, |
| 88366 | int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), |
| 88367 | void *pArg |
| 88368 | ){ |
| 88369 | sqlite3_mutex_enter(db->mutex); |
| 88370 | db->xAuth = (sqlite3_xauth)xAuth; |
| 88371 | db->pAuthArg = pArg; |
| 88372 | sqlite3ExpirePreparedStatements(db); |
| 88373 | sqlite3_mutex_leave(db->mutex); |
| @@ -88858,11 +89800,15 @@ | |
| 88858 | ** See also sqlite3LocateTable(). |
| 88859 | */ |
| 88860 | SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
| 88861 | Table *p = 0; |
| 88862 | int i; |
| 88863 | assert( zName!=0 ); |
| 88864 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 88865 | assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
| 88866 | #if SQLITE_USER_AUTHENTICATION |
| 88867 | /* Only the admin user is allowed to know that the sqlite_user table |
| 88868 | ** exists */ |
| @@ -94322,12 +95268,12 @@ | |
| 94322 | break; |
| 94323 | } |
| 94324 | default: { |
| 94325 | /* Because sqlite3_value_double() returns 0.0 if the argument is not |
| 94326 | ** something that can be converted into a number, we have: |
| 94327 | ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that |
| 94328 | ** cannot be converted to a numeric value. |
| 94329 | */ |
| 94330 | double rVal = sqlite3_value_double(argv[0]); |
| 94331 | if( rVal<0 ) rVal = -rVal; |
| 94332 | sqlite3_result_double(context, rVal); |
| 94333 | break; |
| @@ -103881,13 +104827,16 @@ | |
| 103881 | Vdbe *pOld, /* VM being reprepared */ |
| 103882 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 103883 | const char **pzTail /* OUT: End of parsed string */ |
| 103884 | ){ |
| 103885 | int rc; |
| 103886 | assert( ppStmt!=0 ); |
| 103887 | *ppStmt = 0; |
| 103888 | if( !sqlite3SafetyCheckOk(db) ){ |
| 103889 | return SQLITE_MISUSE_BKPT; |
| 103890 | } |
| 103891 | sqlite3_mutex_enter(db->mutex); |
| 103892 | sqlite3BtreeEnterAll(db); |
| 103893 | rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); |
| @@ -103990,13 +104939,15 @@ | |
| 103990 | */ |
| 103991 | char *zSql8; |
| 103992 | const char *zTail8 = 0; |
| 103993 | int rc = SQLITE_OK; |
| 103994 | |
| 103995 | assert( ppStmt ); |
| 103996 | *ppStmt = 0; |
| 103997 | if( !sqlite3SafetyCheckOk(db) ){ |
| 103998 | return SQLITE_MISUSE_BKPT; |
| 103999 | } |
| 104000 | if( nBytes>=0 ){ |
| 104001 | int sz; |
| 104002 | const char *z = (const char*)zSql; |
| @@ -109705,10 +110656,13 @@ | |
| 109705 | char **pzErrMsg /* Write error messages here */ |
| 109706 | ){ |
| 109707 | int rc; |
| 109708 | TabResult res; |
| 109709 | |
| 109710 | *pazResult = 0; |
| 109711 | if( pnColumn ) *pnColumn = 0; |
| 109712 | if( pnRow ) *pnRow = 0; |
| 109713 | if( pzErrMsg ) *pzErrMsg = 0; |
| 109714 | res.zErrMsg = 0; |
| @@ -111768,11 +112722,11 @@ | |
| 111768 | ** Two writes per page are required in step (3) because the original |
| 111769 | ** database content must be written into the rollback journal prior to |
| 111770 | ** overwriting the database with the vacuumed content. |
| 111771 | ** |
| 111772 | ** Only 1x temporary space and only 1x writes would be required if |
| 111773 | ** the copy of step (3) were replace by deleting the original database |
| 111774 | ** and renaming the transient database as the original. But that will |
| 111775 | ** not work if other processes are attached to the original database. |
| 111776 | ** And a power loss in between deleting the original and renaming the |
| 111777 | ** transient would cause the database file to appear to be deleted |
| 111778 | ** following reboot. |
| @@ -112126,10 +113080,13 @@ | |
| 112126 | sqlite3 *db, /* Database in which module is registered */ |
| 112127 | const char *zName, /* Name assigned to this module */ |
| 112128 | const sqlite3_module *pModule, /* The definition of the module */ |
| 112129 | void *pAux /* Context pointer for xCreate/xConnect */ |
| 112130 | ){ |
| 112131 | return createModule(db, zName, pModule, pAux, 0); |
| 112132 | } |
| 112133 | |
| 112134 | /* |
| 112135 | ** External API function used to create a new virtual-table module. |
| @@ -112139,10 +113096,13 @@ | |
| 112139 | const char *zName, /* Name assigned to this module */ |
| 112140 | const sqlite3_module *pModule, /* The definition of the module */ |
| 112141 | void *pAux, /* Context pointer for xCreate/xConnect */ |
| 112142 | void (*xDestroy)(void *) /* Module destructor function */ |
| 112143 | ){ |
| 112144 | return createModule(db, zName, pModule, pAux, xDestroy); |
| 112145 | } |
| 112146 | |
| 112147 | /* |
| 112148 | ** Lock the virtual table so that it cannot be disconnected. |
| @@ -112371,11 +113331,16 @@ | |
| 112371 | pTable->tabFlags |= TF_Virtual; |
| 112372 | pTable->nModuleArg = 0; |
| 112373 | addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); |
| 112374 | addModuleArgument(db, pTable, 0); |
| 112375 | addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); |
| 112376 | pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z); |
| 112377 | |
| 112378 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 112379 | /* Creating a virtual table invokes the authorization callback twice. |
| 112380 | ** The first invocation, to obtain permission to INSERT a row into the |
| 112381 | ** sqlite_master table, has already been made by sqlite3StartTable(). |
| @@ -112743,10 +113708,13 @@ | |
| 112743 | |
| 112744 | int rc = SQLITE_OK; |
| 112745 | Table *pTab; |
| 112746 | char *zErr = 0; |
| 112747 | |
| 112748 | sqlite3_mutex_enter(db->mutex); |
| 112749 | if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){ |
| 112750 | sqlite3Error(db, SQLITE_MISUSE); |
| 112751 | sqlite3_mutex_leave(db->mutex); |
| 112752 | return SQLITE_MISUSE_BKPT; |
| @@ -113099,10 +114067,13 @@ | |
| 113099 | */ |
| 113100 | SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){ |
| 113101 | static const unsigned char aMap[] = { |
| 113102 | SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE |
| 113103 | }; |
| 113104 | assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 ); |
| 113105 | assert( OE_Ignore==4 && OE_Replace==5 ); |
| 113106 | assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 ); |
| 113107 | return (int)aMap[db->vtabOnConflict-1]; |
| 113108 | } |
| @@ -113114,12 +114085,14 @@ | |
| 113114 | */ |
| 113115 | SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){ |
| 113116 | va_list ap; |
| 113117 | int rc = SQLITE_OK; |
| 113118 | |
| 113119 | sqlite3_mutex_enter(db->mutex); |
| 113120 | |
| 113121 | va_start(ap, op); |
| 113122 | switch( op ){ |
| 113123 | case SQLITE_VTAB_CONSTRAINT_SUPPORT: { |
| 113124 | VtabCtx *p = db->pVtabCtx; |
| 113125 | if( !p ){ |
| @@ -113250,10 +114223,13 @@ | |
| 113250 | } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
| 113251 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 113252 | } u; |
| 113253 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 113254 | Bitmask notReady; /* FROM entries not usable at this level */ |
| 113255 | }; |
| 113256 | |
| 113257 | /* |
| 113258 | ** Each instance of this object represents an algorithm for evaluating one |
| 113259 | ** term of a join. Every term of the FROM clause will have at least |
| @@ -113280,11 +114256,10 @@ | |
| 113280 | LogEst rRun; /* Cost of running each loop */ |
| 113281 | LogEst nOut; /* Estimated number of output rows */ |
| 113282 | union { |
| 113283 | struct { /* Information for internal btree tables */ |
| 113284 | u16 nEq; /* Number of equality constraints */ |
| 113285 | u16 nSkip; /* Number of initial index columns to skip */ |
| 113286 | Index *pIndex; /* Index used, or NULL */ |
| 113287 | } btree; |
| 113288 | struct { /* Information for virtual tables */ |
| 113289 | int idxNum; /* Index number */ |
| 113290 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| @@ -113293,16 +114268,17 @@ | |
| 113293 | char *idxStr; /* Index identifier string */ |
| 113294 | } vtab; |
| 113295 | } u; |
| 113296 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 113297 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 113298 | /**** whereLoopXfer() copies fields above ***********************/ |
| 113299 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 113300 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
| 113301 | WhereTerm **aLTerm; /* WhereTerms used */ |
| 113302 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
| 113303 | WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ |
| 113304 | }; |
| 113305 | |
| 113306 | /* This object holds the prerequisites and the cost of running a |
| 113307 | ** subquery on one operand of an OR operator in the WHERE clause. |
| 113308 | ** See WhereOrSet for additional information |
| @@ -113624,10 +114600,11 @@ | |
| 113624 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
| 113625 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 113626 | #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 113627 | #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ |
| 113628 | #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ |
| 113629 | |
| 113630 | /************** End of whereInt.h ********************************************/ |
| 113631 | /************** Continuing where we left off in where.c **********************/ |
| 113632 | |
| 113633 | /* |
| @@ -113834,11 +114811,11 @@ | |
| 113834 | } |
| 113835 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
| 113836 | } |
| 113837 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
| 113838 | if( p && ExprHasProperty(p, EP_Unlikely) ){ |
| 113839 | pTerm->truthProb = sqlite3LogEst(p->iTable) - 99; |
| 113840 | }else{ |
| 113841 | pTerm->truthProb = 1; |
| 113842 | } |
| 113843 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
| 113844 | pTerm->wtFlags = wtFlags; |
| @@ -114364,10 +115341,19 @@ | |
| 114364 | if( pDerived ){ |
| 114365 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 114366 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 114367 | } |
| 114368 | } |
| 114369 | |
| 114370 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 114371 | /* |
| 114372 | ** Analyze a term that consists of two or more OR-connected |
| 114373 | ** subterms. So in: |
| @@ -114662,12 +115648,11 @@ | |
| 114662 | pNew->x.pList = pList; |
| 114663 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114664 | testcase( idxNew==0 ); |
| 114665 | exprAnalyze(pSrc, pWC, idxNew); |
| 114666 | pTerm = &pWC->a[idxTerm]; |
| 114667 | pWC->a[idxNew].iParent = idxTerm; |
| 114668 | pTerm->nChild = 1; |
| 114669 | }else{ |
| 114670 | sqlite3ExprListDelete(db, pList); |
| 114671 | } |
| 114672 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
| 114673 | } |
| @@ -114765,13 +115750,12 @@ | |
| 114765 | return; |
| 114766 | } |
| 114767 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114768 | if( idxNew==0 ) return; |
| 114769 | pNew = &pWC->a[idxNew]; |
| 114770 | pNew->iParent = idxTerm; |
| 114771 | pTerm = &pWC->a[idxTerm]; |
| 114772 | pTerm->nChild = 1; |
| 114773 | pTerm->wtFlags |= TERM_COPIED; |
| 114774 | if( pExpr->op==TK_EQ |
| 114775 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 114776 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 114777 | ){ |
| @@ -114824,13 +115808,12 @@ | |
| 114824 | transferJoinMarkings(pNewExpr, pExpr); |
| 114825 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114826 | testcase( idxNew==0 ); |
| 114827 | exprAnalyze(pSrc, pWC, idxNew); |
| 114828 | pTerm = &pWC->a[idxTerm]; |
| 114829 | pWC->a[idxNew].iParent = idxTerm; |
| 114830 | } |
| 114831 | pTerm->nChild = 2; |
| 114832 | } |
| 114833 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
| 114834 | |
| 114835 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 114836 | /* Analyze a term that is composed of two or more subterms connected by |
| @@ -114901,13 +115884,12 @@ | |
| 114901 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114902 | testcase( idxNew2==0 ); |
| 114903 | exprAnalyze(pSrc, pWC, idxNew2); |
| 114904 | pTerm = &pWC->a[idxTerm]; |
| 114905 | if( isComplete ){ |
| 114906 | pWC->a[idxNew1].iParent = idxTerm; |
| 114907 | pWC->a[idxNew2].iParent = idxTerm; |
| 114908 | pTerm->nChild = 2; |
| 114909 | } |
| 114910 | } |
| 114911 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 114912 | |
| 114913 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| @@ -114936,13 +115918,12 @@ | |
| 114936 | pNewTerm = &pWC->a[idxNew]; |
| 114937 | pNewTerm->prereqRight = prereqExpr; |
| 114938 | pNewTerm->leftCursor = pLeft->iTable; |
| 114939 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 114940 | pNewTerm->eOperator = WO_MATCH; |
| 114941 | pNewTerm->iParent = idxTerm; |
| 114942 | pTerm = &pWC->a[idxTerm]; |
| 114943 | pTerm->nChild = 1; |
| 114944 | pTerm->wtFlags |= TERM_COPIED; |
| 114945 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 114946 | } |
| 114947 | } |
| 114948 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| @@ -114959,11 +115940,11 @@ | |
| 114959 | ** the start of the loop will prevent any results from being returned. |
| 114960 | */ |
| 114961 | if( pExpr->op==TK_NOTNULL |
| 114962 | && pExpr->pLeft->op==TK_COLUMN |
| 114963 | && pExpr->pLeft->iColumn>=0 |
| 114964 | && OptimizationEnabled(db, SQLITE_Stat3) |
| 114965 | ){ |
| 114966 | Expr *pNewExpr; |
| 114967 | Expr *pLeft = pExpr->pLeft; |
| 114968 | int idxNew; |
| 114969 | WhereTerm *pNewTerm; |
| @@ -114978,13 +115959,12 @@ | |
| 114978 | pNewTerm = &pWC->a[idxNew]; |
| 114979 | pNewTerm->prereqRight = 0; |
| 114980 | pNewTerm->leftCursor = pLeft->iTable; |
| 114981 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 114982 | pNewTerm->eOperator = WO_GT; |
| 114983 | pNewTerm->iParent = idxTerm; |
| 114984 | pTerm = &pWC->a[idxTerm]; |
| 114985 | pTerm->nChild = 1; |
| 114986 | pTerm->wtFlags |= TERM_COPIED; |
| 114987 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 114988 | } |
| 114989 | } |
| 114990 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| @@ -115200,10 +116180,12 @@ | |
| 115200 | WhereLoop *pLoop; /* The Loop object */ |
| 115201 | char *zNotUsed; /* Extra space on the end of pIdx */ |
| 115202 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 115203 | Bitmask extraCols; /* Bitmap of additional columns */ |
| 115204 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
| 115205 | |
| 115206 | /* Generate code to skip over the creation and initialization of the |
| 115207 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 115208 | v = pParse->pVdbe; |
| 115209 | assert( v!=0 ); |
| @@ -115215,10 +116197,16 @@ | |
| 115215 | pTable = pSrc->pTab; |
| 115216 | pWCEnd = &pWC->a[pWC->nTerm]; |
| 115217 | pLoop = pLevel->pWLoop; |
| 115218 | idxCols = 0; |
| 115219 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 115220 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 115221 | int iCol = pTerm->u.leftColumn; |
| 115222 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
| 115223 | testcase( iCol==BMS ); |
| 115224 | testcase( iCol==BMS-1 ); |
| @@ -115227,11 +116215,13 @@ | |
| 115227 | "automatic index on %s(%s)", pTable->zName, |
| 115228 | pTable->aCol[iCol].zName); |
| 115229 | sentWarning = 1; |
| 115230 | } |
| 115231 | if( (idxCols & cMask)==0 ){ |
| 115232 | if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return; |
| 115233 | pLoop->aLTerm[nKeyCol++] = pTerm; |
| 115234 | idxCols |= cMask; |
| 115235 | } |
| 115236 | } |
| 115237 | } |
| @@ -115247,24 +116237,23 @@ | |
| 115247 | ** be a covering index because the index will not be updated if the |
| 115248 | ** original table changes and the index and table cannot both be used |
| 115249 | ** if they go out of sync. |
| 115250 | */ |
| 115251 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
| 115252 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
| 115253 | testcase( pTable->nCol==BMS-1 ); |
| 115254 | testcase( pTable->nCol==BMS-2 ); |
| 115255 | for(i=0; i<mxBitCol; i++){ |
| 115256 | if( extraCols & MASKBIT(i) ) nKeyCol++; |
| 115257 | } |
| 115258 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 115259 | nKeyCol += pTable->nCol - BMS + 1; |
| 115260 | } |
| 115261 | pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
| 115262 | |
| 115263 | /* Construct the Index object to describe this index */ |
| 115264 | pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); |
| 115265 | if( pIdx==0 ) return; |
| 115266 | pLoop->u.btree.pIndex = pIdx; |
| 115267 | pIdx->zName = "auto-index"; |
| 115268 | pIdx->pTable = pTable; |
| 115269 | n = 0; |
| 115270 | idxCols = 0; |
| @@ -115312,22 +116301,33 @@ | |
| 115312 | sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); |
| 115313 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 115314 | VdbeComment((v, "for %s", pTable->zName)); |
| 115315 | |
| 115316 | /* Fill the automatic index with content */ |
| 115317 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); |
| 115318 | regRecord = sqlite3GetTempReg(pParse); |
| 115319 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0); |
| 115320 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 115321 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 115322 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); |
| 115323 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
| 115324 | sqlite3VdbeJumpHere(v, addrTop); |
| 115325 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 115326 | |
| 115327 | /* Jump here when skipping the initialization */ |
| 115328 | sqlite3VdbeJumpHere(v, addrInit); |
| 115329 | } |
| 115330 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 115331 | |
| 115332 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 115333 | /* |
| @@ -115483,22 +116483,22 @@ | |
| 115483 | |
| 115484 | return pParse->nErr; |
| 115485 | } |
| 115486 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 115487 | |
| 115488 | |
| 115489 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 115490 | /* |
| 115491 | ** Estimate the location of a particular key among all keys in an |
| 115492 | ** index. Store the results in aStat as follows: |
| 115493 | ** |
| 115494 | ** aStat[0] Est. number of rows less than pVal |
| 115495 | ** aStat[1] Est. number of rows equal to pVal |
| 115496 | ** |
| 115497 | ** Return SQLITE_OK on success. |
| 115498 | */ |
| 115499 | static void whereKeyStats( |
| 115500 | Parse *pParse, /* Database connection */ |
| 115501 | Index *pIdx, /* Index to consider domain of */ |
| 115502 | UnpackedRecord *pRec, /* Vector of values to consider */ |
| 115503 | int roundUp, /* Round up if true. Round down if false */ |
| 115504 | tRowcnt *aStat /* OUT: stats written here */ |
| @@ -115576,10 +116576,11 @@ | |
| 115576 | }else{ |
| 115577 | iGap = iGap/3; |
| 115578 | } |
| 115579 | aStat[0] = iLower + iGap; |
| 115580 | } |
| 115581 | } |
| 115582 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 115583 | |
| 115584 | /* |
| 115585 | ** If it is not NULL, pTerm is a term that provides an upper or lower |
| @@ -115726,11 +116727,11 @@ | |
| 115726 | ** pLower pUpper |
| 115727 | ** |
| 115728 | ** If either of the upper or lower bound is not present, then NULL is passed in |
| 115729 | ** place of the corresponding WhereTerm. |
| 115730 | ** |
| 115731 | ** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index |
| 115732 | ** column subject to the range constraint. Or, equivalently, the number of |
| 115733 | ** equality constraints optimized by the proposed index scan. For example, |
| 115734 | ** assuming index p is on t1(a, b), and the SQL query is: |
| 115735 | ** |
| 115736 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| @@ -115742,11 +116743,11 @@ | |
| 115742 | ** |
| 115743 | ** then nEq is set to 0. |
| 115744 | ** |
| 115745 | ** When this function is called, *pnOut is set to the sqlite3LogEst() of the |
| 115746 | ** number of rows that the index scan is expected to visit without |
| 115747 | ** considering the range constraints. If nEq is 0, this is the number of |
| 115748 | ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) |
| 115749 | ** to account for the range constraints pLower and pUpper. |
| 115750 | ** |
| 115751 | ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be |
| 115752 | ** used, a single range inequality reduces the search space by a factor of 4. |
| @@ -115766,14 +116767,11 @@ | |
| 115766 | |
| 115767 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 115768 | Index *p = pLoop->u.btree.pIndex; |
| 115769 | int nEq = pLoop->u.btree.nEq; |
| 115770 | |
| 115771 | if( p->nSample>0 |
| 115772 | && nEq<p->nSampleCol |
| 115773 | && OptimizationEnabled(pParse->db, SQLITE_Stat3) |
| 115774 | ){ |
| 115775 | if( nEq==pBuilder->nRecValid ){ |
| 115776 | UnpackedRecord *pRec = pBuilder->pRec; |
| 115777 | tRowcnt a[2]; |
| 115778 | u8 aff; |
| 115779 | |
| @@ -115785,19 +116783,23 @@ | |
| 115785 | ** |
| 115786 | ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 115787 | ** is not a simple variable or literal value), the lower bound of the |
| 115788 | ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 115789 | ** if $L is available, whereKeyStats() is called for both ($P) and |
| 115790 | ** ($P:$L) and the larger of the two returned values used. |
| 115791 | ** |
| 115792 | ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 115793 | ** less than the upper bound of the range query. Where the upper bound |
| 115794 | ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 115795 | ** of iUpper are requested of whereKeyStats() and the smaller used. |
| 115796 | */ |
| 115797 | tRowcnt iLower; |
| 115798 | tRowcnt iUpper; |
| 115799 | |
| 115800 | if( pRec ){ |
| 115801 | testcase( pRec->nField!=pBuilder->nRecValid ); |
| 115802 | pRec->nField = pBuilder->nRecValid; |
| 115803 | } |
| @@ -115807,11 +116809,11 @@ | |
| 115807 | aff = p->pTable->aCol[p->aiColumn[nEq]].affinity; |
| 115808 | } |
| 115809 | /* Determine iLower and iUpper using ($P) only. */ |
| 115810 | if( nEq==0 ){ |
| 115811 | iLower = 0; |
| 115812 | iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]); |
| 115813 | }else{ |
| 115814 | /* Note: this call could be optimized away - since the same values must |
| 115815 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 115816 | whereKeyStats(pParse, p, pRec, 0, a); |
| 115817 | iLower = a[0]; |
| @@ -115831,11 +116833,11 @@ | |
| 115831 | int bOk; /* True if value is extracted from pExpr */ |
| 115832 | Expr *pExpr = pLower->pExpr->pRight; |
| 115833 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 115834 | if( rc==SQLITE_OK && bOk ){ |
| 115835 | tRowcnt iNew; |
| 115836 | whereKeyStats(pParse, p, pRec, 0, a); |
| 115837 | iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 115838 | if( iNew>iLower ) iLower = iNew; |
| 115839 | nOut--; |
| 115840 | pLower = 0; |
| 115841 | } |
| @@ -115846,11 +116848,11 @@ | |
| 115846 | int bOk; /* True if value is extracted from pExpr */ |
| 115847 | Expr *pExpr = pUpper->pExpr->pRight; |
| 115848 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 115849 | if( rc==SQLITE_OK && bOk ){ |
| 115850 | tRowcnt iNew; |
| 115851 | whereKeyStats(pParse, p, pRec, 1, a); |
| 115852 | iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 115853 | if( iNew<iUpper ) iUpper = iNew; |
| 115854 | nOut--; |
| 115855 | pUpper = 0; |
| 115856 | } |
| @@ -115858,10 +116860,15 @@ | |
| 115858 | |
| 115859 | pBuilder->pRec = pRec; |
| 115860 | if( rc==SQLITE_OK ){ |
| 115861 | if( iUpper>iLower ){ |
| 115862 | nNew = sqlite3LogEst(iUpper - iLower); |
| 115863 | }else{ |
| 115864 | nNew = 10; assert( 10==sqlite3LogEst(2) ); |
| 115865 | } |
| 115866 | if( nNew<nOut ){ |
| 115867 | nOut = nNew; |
| @@ -115882,16 +116889,19 @@ | |
| 115882 | #endif |
| 115883 | assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 ); |
| 115884 | nNew = whereRangeAdjust(pLower, nOut); |
| 115885 | nNew = whereRangeAdjust(pUpper, nNew); |
| 115886 | |
| 115887 | /* TUNING: If there is both an upper and lower limit, assume the range is |
| 115888 | ** reduced by an additional 75%. This means that, by default, an open-ended |
| 115889 | ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the |
| 115890 | ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to |
| 115891 | ** match 1/64 of the index. */ |
| 115892 | if( pLower && pUpper ) nNew -= 20; |
| 115893 | |
| 115894 | nOut -= (pLower!=0) + (pUpper!=0); |
| 115895 | if( nNew<10 ) nNew = 10; |
| 115896 | if( nNew<nOut ) nOut = nNew; |
| 115897 | #if defined(WHERETRACE_ENABLED) |
| @@ -116247,11 +117257,11 @@ | |
| 116247 | |
| 116248 | /* This module is only called on query plans that use an index. */ |
| 116249 | pLoop = pLevel->pWLoop; |
| 116250 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 116251 | nEq = pLoop->u.btree.nEq; |
| 116252 | nSkip = pLoop->u.btree.nSkip; |
| 116253 | pIdx = pLoop->u.btree.pIndex; |
| 116254 | assert( pIdx!=0 ); |
| 116255 | |
| 116256 | /* Figure out how many memory cells we will need then allocate them. |
| 116257 | */ |
| @@ -116361,11 +117371,11 @@ | |
| 116361 | ** "a=? AND b>?" |
| 116362 | */ |
| 116363 | static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){ |
| 116364 | Index *pIndex = pLoop->u.btree.pIndex; |
| 116365 | u16 nEq = pLoop->u.btree.nEq; |
| 116366 | u16 nSkip = pLoop->u.btree.nSkip; |
| 116367 | int i, j; |
| 116368 | Column *aCol = pTab->aCol; |
| 116369 | i16 *aiColumn = pIndex->aiColumn; |
| 116370 | |
| 116371 | if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
| @@ -116392,23 +117402,27 @@ | |
| 116392 | sqlite3StrAccumAppend(pStr, ")", 1); |
| 116393 | } |
| 116394 | |
| 116395 | /* |
| 116396 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 116397 | ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| 116398 | ** record is added to the output to describe the table scan strategy in |
| 116399 | ** pLevel. |
| 116400 | */ |
| 116401 | static void explainOneScan( |
| 116402 | Parse *pParse, /* Parse context */ |
| 116403 | SrcList *pTabList, /* Table list this loop refers to */ |
| 116404 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 116405 | int iLevel, /* Value for "level" column of output */ |
| 116406 | int iFrom, /* Value for "from" column of output */ |
| 116407 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 116408 | ){ |
| 116409 | #ifndef SQLITE_DEBUG |
| 116410 | if( pParse->explain==2 ) |
| 116411 | #endif |
| 116412 | { |
| 116413 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 116414 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| @@ -116421,11 +117435,11 @@ | |
| 116421 | StrAccum str; /* EQP output string */ |
| 116422 | char zBuf[100]; /* Initial space for EQP output string */ |
| 116423 | |
| 116424 | pLoop = pLevel->pWLoop; |
| 116425 | flags = pLoop->wsFlags; |
| 116426 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
| 116427 | |
| 116428 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 116429 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 116430 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 116431 | |
| @@ -116450,10 +117464,12 @@ | |
| 116450 | assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
| 116451 | if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
| 116452 | if( isSearch ){ |
| 116453 | zFmt = "PRIMARY KEY"; |
| 116454 | } |
| 116455 | }else if( flags & WHERE_AUTO_INDEX ){ |
| 116456 | zFmt = "AUTOMATIC COVERING INDEX"; |
| 116457 | }else if( flags & WHERE_IDX_ONLY ){ |
| 116458 | zFmt = "COVERING INDEX %s"; |
| 116459 | }else{ |
| @@ -116491,16 +117507,49 @@ | |
| 116491 | }else{ |
| 116492 | sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
| 116493 | } |
| 116494 | #endif |
| 116495 | zMsg = sqlite3StrAccumFinish(&str); |
| 116496 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
| 116497 | } |
| 116498 | } |
| 116499 | #else |
| 116500 | # define explainOneScan(u,v,w,x,y,z) |
| 116501 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 116502 | |
| 116503 | |
| 116504 | /* |
| 116505 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 116506 | ** implementation described by pWInfo. |
| @@ -116798,11 +117847,11 @@ | |
| 116798 | u8 bSeekPastNull = 0; /* True to seek past initial nulls */ |
| 116799 | u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ |
| 116800 | |
| 116801 | pIdx = pLoop->u.btree.pIndex; |
| 116802 | iIdxCur = pLevel->iIdxCur; |
| 116803 | assert( nEq>=pLoop->u.btree.nSkip ); |
| 116804 | |
| 116805 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 116806 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 116807 | ** query, then the caller will only allow the loop to run for |
| 116808 | ** a single iteration. This means that the first row returned |
| @@ -116815,11 +117864,11 @@ | |
| 116815 | || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); |
| 116816 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 116817 | && pWInfo->nOBSat>0 |
| 116818 | && (pIdx->nKeyCol>nEq) |
| 116819 | ){ |
| 116820 | assert( pLoop->u.btree.nSkip==0 ); |
| 116821 | bSeekPastNull = 1; |
| 116822 | nExtraReg = 1; |
| 116823 | } |
| 116824 | |
| 116825 | /* Find any inequality constraint terms for the start and end |
| @@ -117164,13 +118213,15 @@ | |
| 117164 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 117165 | wctrlFlags, iCovCur); |
| 117166 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
| 117167 | if( pSubWInfo ){ |
| 117168 | WhereLoop *pSubLoop; |
| 117169 | explainOneScan( |
| 117170 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 117171 | ); |
| 117172 | /* This is the sub-WHERE clause body. First skip over |
| 117173 | ** duplicate rows from prior sub-WHERE clauses, and record the |
| 117174 | ** rowid (or PRIMARY KEY) for the current row so that the same |
| 117175 | ** row will be skipped in subsequent sub-WHERE clauses. |
| 117176 | */ |
| @@ -117296,10 +118347,14 @@ | |
| 117296 | VdbeCoverageIf(v, bRev==0); |
| 117297 | VdbeCoverageIf(v, bRev!=0); |
| 117298 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 117299 | } |
| 117300 | } |
| 117301 | |
| 117302 | /* Insert code to test every subexpression that can be completely |
| 117303 | ** computed using the current set of tables. |
| 117304 | */ |
| 117305 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| @@ -117436,11 +118491,11 @@ | |
| 117436 | } |
| 117437 | sqlite3DebugPrintf(" %-19s", z); |
| 117438 | sqlite3_free(z); |
| 117439 | } |
| 117440 | if( p->wsFlags & WHERE_SKIPSCAN ){ |
| 117441 | sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip); |
| 117442 | }else{ |
| 117443 | sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); |
| 117444 | } |
| 117445 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
| 117446 | if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ |
| @@ -117547,34 +118602,41 @@ | |
| 117547 | sqlite3DbFree(db, pWInfo); |
| 117548 | } |
| 117549 | } |
| 117550 | |
| 117551 | /* |
| 117552 | ** Return TRUE if both of the following are true: |
| 117553 | ** |
| 117554 | ** (1) X has the same or lower cost that Y |
| 117555 | ** (2) X is a proper subset of Y |
| 117556 | ** |
| 117557 | ** By "proper subset" we mean that X uses fewer WHERE clause terms |
| 117558 | ** than Y and that every WHERE clause term used by X is also used |
| 117559 | ** by Y. |
| 117560 | ** |
| 117561 | ** If X is a proper subset of Y then Y is a better choice and ought |
| 117562 | ** to have a lower cost. This routine returns TRUE when that cost |
| 117563 | ** relationship is inverted and needs to be adjusted. |
| 117564 | */ |
| 117565 | static int whereLoopCheaperProperSubset( |
| 117566 | const WhereLoop *pX, /* First WhereLoop to compare */ |
| 117567 | const WhereLoop *pY /* Compare against this WhereLoop */ |
| 117568 | ){ |
| 117569 | int i, j; |
| 117570 | if( pX->nLTerm >= pY->nLTerm ) return 0; /* X is not a subset of Y */ |
| 117571 | if( pX->rRun >= pY->rRun ){ |
| 117572 | if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ |
| 117573 | if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ |
| 117574 | } |
| 117575 | for(i=pX->nLTerm-1; i>=0; i--){ |
| 117576 | for(j=pY->nLTerm-1; j>=0; j--){ |
| 117577 | if( pY->aLTerm[j]==pX->aLTerm[i] ) break; |
| 117578 | } |
| 117579 | if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ |
| 117580 | } |
| @@ -117592,37 +118654,28 @@ | |
| 117592 | ** is a proper subset. |
| 117593 | ** |
| 117594 | ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer |
| 117595 | ** WHERE clause terms than Y and that every WHERE clause term used by X is |
| 117596 | ** also used by Y. |
| 117597 | ** |
| 117598 | ** This adjustment is omitted for SKIPSCAN loops. In a SKIPSCAN loop, the |
| 117599 | ** WhereLoop.nLTerm field is not an accurate measure of the number of WHERE |
| 117600 | ** clause terms covered, since some of the first nLTerm entries in aLTerm[] |
| 117601 | ** will be NULL (because they are skipped). That makes it more difficult |
| 117602 | ** to compare the loops. We could add extra code to do the comparison, and |
| 117603 | ** perhaps we will someday. But SKIPSCAN is sufficiently uncommon, and this |
| 117604 | ** adjustment is sufficient minor, that it is very difficult to construct |
| 117605 | ** a test case where the extra code would improve the query plan. Better |
| 117606 | ** to avoid the added complexity and just omit cost adjustments to SKIPSCAN |
| 117607 | ** loops. |
| 117608 | */ |
| 117609 | static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ |
| 117610 | if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return; |
| 117611 | if( (pTemplate->wsFlags & WHERE_SKIPSCAN)!=0 ) return; |
| 117612 | for(; p; p=p->pNextLoop){ |
| 117613 | if( p->iTab!=pTemplate->iTab ) continue; |
| 117614 | if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; |
| 117615 | if( (p->wsFlags & WHERE_SKIPSCAN)!=0 ) continue; |
| 117616 | if( whereLoopCheaperProperSubset(p, pTemplate) ){ |
| 117617 | /* Adjust pTemplate cost downward so that it is cheaper than its |
| 117618 | ** subset p */ |
| 117619 | pTemplate->rRun = p->rRun; |
| 117620 | pTemplate->nOut = p->nOut - 1; |
| 117621 | }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ |
| 117622 | /* Adjust pTemplate cost upward so that it is costlier than p since |
| 117623 | ** pTemplate is a proper subset of p */ |
| 117624 | pTemplate->rRun = p->rRun; |
| 117625 | pTemplate->nOut = p->nOut + 1; |
| 117626 | } |
| 117627 | } |
| 117628 | } |
| @@ -117663,12 +118716,13 @@ | |
| 117663 | ** rSetup. Call this SETUP-INVARIANT */ |
| 117664 | assert( p->rSetup>=pTemplate->rSetup ); |
| 117665 | |
| 117666 | /* Any loop using an appliation-defined index (or PRIMARY KEY or |
| 117667 | ** UNIQUE constraint) with one or more == constraints is better |
| 117668 | ** than an automatic index. */ |
| 117669 | if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 |
| 117670 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 117671 | && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0 |
| 117672 | && (p->prereq & pTemplate->prereq)==pTemplate->prereq |
| 117673 | ){ |
| 117674 | break; |
| @@ -117823,25 +118877,46 @@ | |
| 117823 | |
| 117824 | /* |
| 117825 | ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 117826 | ** WHERE clause that reference the loop but which are not used by an |
| 117827 | ** index. |
| 117828 | ** |
| 117829 | ** In the current implementation, the first extra WHERE clause term reduces |
| 117830 | ** the number of output rows by a factor of 10 and each additional term |
| 117831 | ** reduces the number of output rows by sqrt(2). |
| 117832 | */ |
| 117833 | static void whereLoopOutputAdjust( |
| 117834 | WhereClause *pWC, /* The WHERE clause */ |
| 117835 | WhereLoop *pLoop, /* The loop to adjust downward */ |
| 117836 | LogEst nRow /* Number of rows in the entire table */ |
| 117837 | ){ |
| 117838 | WhereTerm *pTerm, *pX; |
| 117839 | Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); |
| 117840 | int i, j; |
| 117841 | int nEq = 0; /* Number of = constraints not within likely()/unlikely() */ |
| 117842 | |
| 117843 | for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ |
| 117844 | if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; |
| 117845 | if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; |
| 117846 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
| 117847 | for(j=pLoop->nLTerm-1; j>=0; j--){ |
| @@ -117850,24 +118925,30 @@ | |
| 117850 | if( pX==pTerm ) break; |
| 117851 | if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; |
| 117852 | } |
| 117853 | if( j<0 ){ |
| 117854 | if( pTerm->truthProb<=0 ){ |
| 117855 | pLoop->nOut += pTerm->truthProb; |
| 117856 | }else{ |
| 117857 | pLoop->nOut--; |
| 117858 | if( pTerm->eOperator&WO_EQ ) nEq++; |
| 117859 | } |
| 117860 | } |
| 117861 | } |
| 117862 | /* TUNING: If there is at least one equality constraint in the WHERE |
| 117863 | ** clause that does not have a likelihood() explicitly assigned to it |
| 117864 | ** then do not let the estimated number of output rows exceed half |
| 117865 | ** the number of rows in the table. */ |
| 117866 | if( nEq && pLoop->nOut>nRow-10 ){ |
| 117867 | pLoop->nOut = nRow - 10; |
| 117868 | } |
| 117869 | } |
| 117870 | |
| 117871 | /* |
| 117872 | ** Adjust the cost C by the costMult facter T. This only occurs if |
| 117873 | ** compiled with -DSQLITE_ENABLE_COSTMULT |
| @@ -117904,11 +118985,11 @@ | |
| 117904 | int opMask; /* Valid operators for constraints */ |
| 117905 | WhereScan scan; /* Iterator for WHERE terms */ |
| 117906 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 117907 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 117908 | u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 117909 | u16 saved_nSkip; /* Original value of pNew->u.btree.nSkip */ |
| 117910 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 117911 | LogEst saved_nOut; /* Original value of pNew->nOut */ |
| 117912 | int iCol; /* Index of the column in the table */ |
| 117913 | int rc = SQLITE_OK; /* Return code */ |
| 117914 | LogEst rSize; /* Number of rows in the table */ |
| @@ -117933,56 +119014,18 @@ | |
| 117933 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| 117934 | |
| 117935 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
| 117936 | opMask, pProbe); |
| 117937 | saved_nEq = pNew->u.btree.nEq; |
| 117938 | saved_nSkip = pNew->u.btree.nSkip; |
| 117939 | saved_nLTerm = pNew->nLTerm; |
| 117940 | saved_wsFlags = pNew->wsFlags; |
| 117941 | saved_prereq = pNew->prereq; |
| 117942 | saved_nOut = pNew->nOut; |
| 117943 | pNew->rSetup = 0; |
| 117944 | rSize = pProbe->aiRowLogEst[0]; |
| 117945 | rLogSize = estLog(rSize); |
| 117946 | |
| 117947 | /* Consider using a skip-scan if there are no WHERE clause constraints |
| 117948 | ** available for the left-most terms of the index, and if the average |
| 117949 | ** number of repeats in the left-most terms is at least 18. |
| 117950 | ** |
| 117951 | ** The magic number 18 is selected on the basis that scanning 17 rows |
| 117952 | ** is almost always quicker than an index seek (even though if the index |
| 117953 | ** contains fewer than 2^17 rows we assume otherwise in other parts of |
| 117954 | ** the code). And, even if it is not, it should not be too much slower. |
| 117955 | ** On the other hand, the extra seeks could end up being significantly |
| 117956 | ** more expensive. */ |
| 117957 | assert( 42==sqlite3LogEst(18) ); |
| 117958 | if( saved_nEq==saved_nSkip |
| 117959 | && saved_nEq+1<pProbe->nKeyCol |
| 117960 | && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ |
| 117961 | && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK |
| 117962 | ){ |
| 117963 | LogEst nIter; |
| 117964 | pNew->u.btree.nEq++; |
| 117965 | pNew->u.btree.nSkip++; |
| 117966 | pNew->aLTerm[pNew->nLTerm++] = 0; |
| 117967 | pNew->wsFlags |= WHERE_SKIPSCAN; |
| 117968 | nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; |
| 117969 | if( pTerm ){ |
| 117970 | /* TUNING: When estimating skip-scan for a term that is also indexable, |
| 117971 | ** multiply the cost of the skip-scan by 2.0, to make it a little less |
| 117972 | ** desirable than the regular index lookup. */ |
| 117973 | nIter += 10; assert( 10==sqlite3LogEst(2) ); |
| 117974 | } |
| 117975 | pNew->nOut -= nIter; |
| 117976 | /* TUNING: Because uncertainties in the estimates for skip-scan queries, |
| 117977 | ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ |
| 117978 | nIter += 5; |
| 117979 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
| 117980 | pNew->nOut = saved_nOut; |
| 117981 | pNew->u.btree.nEq = saved_nEq; |
| 117982 | pNew->u.btree.nSkip = saved_nSkip; |
| 117983 | } |
| 117984 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
| 117985 | u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ |
| 117986 | LogEst rCostIdx; |
| 117987 | LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ |
| 117988 | int nIn = 0; |
| @@ -118073,11 +119116,10 @@ | |
| 118073 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 118074 | tRowcnt nOut = 0; |
| 118075 | if( nInMul==0 |
| 118076 | && pProbe->nSample |
| 118077 | && pNew->u.btree.nEq<=pProbe->nSampleCol |
| 118078 | && OptimizationEnabled(db, SQLITE_Stat3) |
| 118079 | && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) |
| 118080 | ){ |
| 118081 | Expr *pExpr = pTerm->pExpr; |
| 118082 | if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){ |
| 118083 | testcase( eOp & WO_EQ ); |
| @@ -118141,14 +119183,49 @@ | |
| 118141 | pBuilder->nRecValid = nRecValid; |
| 118142 | #endif |
| 118143 | } |
| 118144 | pNew->prereq = saved_prereq; |
| 118145 | pNew->u.btree.nEq = saved_nEq; |
| 118146 | pNew->u.btree.nSkip = saved_nSkip; |
| 118147 | pNew->wsFlags = saved_wsFlags; |
| 118148 | pNew->nOut = saved_nOut; |
| 118149 | pNew->nLTerm = saved_nLTerm; |
| 118150 | return rc; |
| 118151 | } |
| 118152 | |
| 118153 | /* |
| 118154 | ** Return True if it is possible that pIndex might be useful in |
| @@ -118323,11 +119400,11 @@ | |
| 118323 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 118324 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
| 118325 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 118326 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 118327 | pNew->u.btree.nEq = 1; |
| 118328 | pNew->u.btree.nSkip = 0; |
| 118329 | pNew->u.btree.pIndex = 0; |
| 118330 | pNew->nLTerm = 1; |
| 118331 | pNew->aLTerm[0] = pTerm; |
| 118332 | /* TUNING: One-time cost for computing the automatic index is |
| 118333 | ** estimated to be X*N*log2(N) where N is the number of rows in |
| @@ -118364,11 +119441,11 @@ | |
| 118364 | testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ |
| 118365 | continue; /* Partial index inappropriate for this query */ |
| 118366 | } |
| 118367 | rSize = pProbe->aiRowLogEst[0]; |
| 118368 | pNew->u.btree.nEq = 0; |
| 118369 | pNew->u.btree.nSkip = 0; |
| 118370 | pNew->nLTerm = 0; |
| 118371 | pNew->iSortIdx = 0; |
| 118372 | pNew->rSetup = 0; |
| 118373 | pNew->prereq = mExtra; |
| 118374 | pNew->nOut = rSize; |
| @@ -118914,11 +119991,11 @@ | |
| 118914 | for(j=0; j<nColumn; j++){ |
| 118915 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 118916 | |
| 118917 | /* Skip over == and IS NULL terms */ |
| 118918 | if( j<pLoop->u.btree.nEq |
| 118919 | && pLoop->u.btree.nSkip==0 |
| 118920 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
| 118921 | ){ |
| 118922 | if( i & WO_ISNULL ){ |
| 118923 | testcase( isOrderDistinct ); |
| 118924 | isOrderDistinct = 0; |
| @@ -119368,11 +120445,11 @@ | |
| 119368 | } |
| 119369 | } |
| 119370 | } |
| 119371 | |
| 119372 | #ifdef WHERETRACE_ENABLED /* >=2 */ |
| 119373 | if( sqlite3WhereTrace>=2 ){ |
| 119374 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
| 119375 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
| 119376 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
| 119377 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
| 119378 | pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); |
| @@ -119487,11 +120564,11 @@ | |
| 119487 | if( pItem->zIndex ) return 0; |
| 119488 | iCur = pItem->iCursor; |
| 119489 | pWC = &pWInfo->sWC; |
| 119490 | pLoop = pBuilder->pNew; |
| 119491 | pLoop->wsFlags = 0; |
| 119492 | pLoop->u.btree.nSkip = 0; |
| 119493 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
| 119494 | if( pTerm ){ |
| 119495 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 119496 | pLoop->aLTerm[0] = pTerm; |
| 119497 | pLoop->nLTerm = 1; |
| @@ -119499,11 +120576,10 @@ | |
| 119499 | /* TUNING: Cost of a rowid lookup is 10 */ |
| 119500 | pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ |
| 119501 | }else{ |
| 119502 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 119503 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
| 119504 | assert( ArraySize(pLoop->aLTermSpace)==4 ); |
| 119505 | if( !IsUniqueIndex(pIdx) |
| 119506 | || pIdx->pPartIdxWhere!=0 |
| 119507 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
| 119508 | ) continue; |
| 119509 | for(j=0; j<pIdx->nKeyCol; j++){ |
| @@ -120008,22 +121084,30 @@ | |
| 120008 | ** loop below generates code for a single nested loop of the VM |
| 120009 | ** program. |
| 120010 | */ |
| 120011 | notReady = ~(Bitmask)0; |
| 120012 | for(ii=0; ii<nTabList; ii++){ |
| 120013 | pLevel = &pWInfo->a[ii]; |
| 120014 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 120015 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 120016 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 120017 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 120018 | if( db->mallocFailed ) goto whereBeginError; |
| 120019 | } |
| 120020 | #endif |
| 120021 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
| 120022 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
| 120023 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
| 120024 | pWInfo->iContinue = pLevel->addrCont; |
| 120025 | } |
| 120026 | |
| 120027 | /* Done. */ |
| 120028 | VdbeModuleComment((v, "Begin WHERE-core")); |
| 120029 | return pWInfo; |
| @@ -124687,10 +125771,17 @@ | |
| 124687 | ** is look for a semicolon that is not part of an string or comment. |
| 124688 | */ |
| 124689 | SQLITE_API int sqlite3_complete(const char *zSql){ |
| 124690 | u8 state = 0; /* Current state, using numbers defined in header comment */ |
| 124691 | u8 token; /* Value of the next token */ |
| 124692 | |
| 124693 | #ifndef SQLITE_OMIT_TRIGGER |
| 124694 | /* A complex statement machine used to detect the end of a CREATE TRIGGER |
| 124695 | ** statement. This is the normal case. |
| 124696 | */ |
| @@ -125285,74 +126376,106 @@ | |
| 125285 | |
| 125286 | va_start(ap, op); |
| 125287 | switch( op ){ |
| 125288 | |
| 125289 | /* Mutex configuration options are only available in a threadsafe |
| 125290 | ** compile. |
| 125291 | */ |
| 125292 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 |
| 125293 | case SQLITE_CONFIG_SINGLETHREAD: { |
| 125294 | /* Disable all mutexing */ |
| 125295 | sqlite3GlobalConfig.bCoreMutex = 0; |
| 125296 | sqlite3GlobalConfig.bFullMutex = 0; |
| 125297 | break; |
| 125298 | } |
| 125299 | case SQLITE_CONFIG_MULTITHREAD: { |
| 125300 | /* Disable mutexing of database connections */ |
| 125301 | /* Enable mutexing of core data structures */ |
| 125302 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 125303 | sqlite3GlobalConfig.bFullMutex = 0; |
| 125304 | break; |
| 125305 | } |
| 125306 | case SQLITE_CONFIG_SERIALIZED: { |
| 125307 | /* Enable all mutexing */ |
| 125308 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 125309 | sqlite3GlobalConfig.bFullMutex = 1; |
| 125310 | break; |
| 125311 | } |
| 125312 | case SQLITE_CONFIG_MUTEX: { |
| 125313 | /* Specify an alternative mutex implementation */ |
| 125314 | sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); |
| 125315 | break; |
| 125316 | } |
| 125317 | case SQLITE_CONFIG_GETMUTEX: { |
| 125318 | /* Retrieve the current mutex implementation */ |
| 125319 | *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; |
| 125320 | break; |
| 125321 | } |
| 125322 | #endif |
| 125323 | |
| 125324 | |
| 125325 | case SQLITE_CONFIG_MALLOC: { |
| 125326 | /* Specify an alternative malloc implementation */ |
| 125327 | sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); |
| 125328 | break; |
| 125329 | } |
| 125330 | case SQLITE_CONFIG_GETMALLOC: { |
| 125331 | /* Retrieve the current malloc() implementation */ |
| 125332 | if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); |
| 125333 | *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; |
| 125334 | break; |
| 125335 | } |
| 125336 | case SQLITE_CONFIG_MEMSTATUS: { |
| 125337 | /* Enable or disable the malloc status collection */ |
| 125338 | sqlite3GlobalConfig.bMemstat = va_arg(ap, int); |
| 125339 | break; |
| 125340 | } |
| 125341 | case SQLITE_CONFIG_SCRATCH: { |
| 125342 | /* Designate a buffer for scratch memory space */ |
| 125343 | sqlite3GlobalConfig.pScratch = va_arg(ap, void*); |
| 125344 | sqlite3GlobalConfig.szScratch = va_arg(ap, int); |
| 125345 | sqlite3GlobalConfig.nScratch = va_arg(ap, int); |
| 125346 | break; |
| 125347 | } |
| 125348 | case SQLITE_CONFIG_PAGECACHE: { |
| 125349 | /* Designate a buffer for page cache memory space */ |
| 125350 | sqlite3GlobalConfig.pPage = va_arg(ap, void*); |
| 125351 | sqlite3GlobalConfig.szPage = va_arg(ap, int); |
| 125352 | sqlite3GlobalConfig.nPage = va_arg(ap, int); |
| 125353 | break; |
| 125354 | } |
| 125355 | |
| 125356 | case SQLITE_CONFIG_PCACHE: { |
| 125357 | /* no-op */ |
| 125358 | break; |
| @@ -125362,25 +126485,37 @@ | |
| 125362 | rc = SQLITE_ERROR; |
| 125363 | break; |
| 125364 | } |
| 125365 | |
| 125366 | case SQLITE_CONFIG_PCACHE2: { |
| 125367 | /* Specify an alternative page cache implementation */ |
| 125368 | sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*); |
| 125369 | break; |
| 125370 | } |
| 125371 | case SQLITE_CONFIG_GETPCACHE2: { |
| 125372 | if( sqlite3GlobalConfig.pcache2.xInit==0 ){ |
| 125373 | sqlite3PCacheSetDefault(); |
| 125374 | } |
| 125375 | *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2; |
| 125376 | break; |
| 125377 | } |
| 125378 | |
| 125379 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 125380 | case SQLITE_CONFIG_HEAP: { |
| 125381 | /* Designate a buffer for heap memory space */ |
| 125382 | sqlite3GlobalConfig.pHeap = va_arg(ap, void*); |
| 125383 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 125384 | sqlite3GlobalConfig.mnReq = va_arg(ap, int); |
| 125385 | |
| 125386 | if( sqlite3GlobalConfig.mnReq<1 ){ |
| @@ -125389,21 +126524,23 @@ | |
| 125389 | /* cap min request size at 2^12 */ |
| 125390 | sqlite3GlobalConfig.mnReq = (1<<12); |
| 125391 | } |
| 125392 | |
| 125393 | if( sqlite3GlobalConfig.pHeap==0 ){ |
| 125394 | /* If the heap pointer is NULL, then restore the malloc implementation |
| 125395 | ** back to NULL pointers too. This will cause the malloc to go |
| 125396 | ** back to its default implementation when sqlite3_initialize() is |
| 125397 | ** run. |
| 125398 | */ |
| 125399 | memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); |
| 125400 | }else{ |
| 125401 | /* The heap pointer is not NULL, then install one of the |
| 125402 | ** mem5.c/mem3.c methods. The enclosing #if guarantees at |
| 125403 | ** least one of these methods is currently enabled. |
| 125404 | */ |
| 125405 | #ifdef SQLITE_ENABLE_MEMSYS3 |
| 125406 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); |
| 125407 | #endif |
| 125408 | #ifdef SQLITE_ENABLE_MEMSYS5 |
| 125409 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); |
| @@ -125438,15 +126575,23 @@ | |
| 125438 | ** can be changed at start-time using the |
| 125439 | ** sqlite3_config(SQLITE_CONFIG_URI,1) or |
| 125440 | ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. |
| 125441 | */ |
| 125442 | case SQLITE_CONFIG_URI: { |
| 125443 | sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); |
| 125444 | break; |
| 125445 | } |
| 125446 | |
| 125447 | case SQLITE_CONFIG_COVERING_INDEX_SCAN: { |
| 125448 | sqlite3GlobalConfig.bUseCis = va_arg(ap, int); |
| 125449 | break; |
| 125450 | } |
| 125451 | |
| 125452 | #ifdef SQLITE_ENABLE_SQLLOG |
| @@ -125457,24 +126602,37 @@ | |
| 125457 | break; |
| 125458 | } |
| 125459 | #endif |
| 125460 | |
| 125461 | case SQLITE_CONFIG_MMAP_SIZE: { |
| 125462 | sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64); |
| 125463 | sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64); |
| 125464 | if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){ |
| 125465 | mxMmap = SQLITE_MAX_MMAP_SIZE; |
| 125466 | } |
| 125467 | sqlite3GlobalConfig.mxMmap = mxMmap; |
| 125468 | if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE; |
| 125469 | if( szMmap>mxMmap) szMmap = mxMmap; |
| 125470 | sqlite3GlobalConfig.szMmap = szMmap; |
| 125471 | break; |
| 125472 | } |
| 125473 | |
| 125474 | #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) |
| 125475 | case SQLITE_CONFIG_WIN32_HEAPSIZE: { |
| 125476 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 125477 | break; |
| 125478 | } |
| 125479 | #endif |
| 125480 | |
| @@ -125554,19 +126712,29 @@ | |
| 125554 | |
| 125555 | /* |
| 125556 | ** Return the mutex associated with a database connection. |
| 125557 | */ |
| 125558 | SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ |
| 125559 | return db->mutex; |
| 125560 | } |
| 125561 | |
| 125562 | /* |
| 125563 | ** Free up as much memory as we can from the given database |
| 125564 | ** connection. |
| 125565 | */ |
| 125566 | SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){ |
| 125567 | int i; |
| 125568 | sqlite3_mutex_enter(db->mutex); |
| 125569 | sqlite3BtreeEnterAll(db); |
| 125570 | for(i=0; i<db->nDb; i++){ |
| 125571 | Btree *pBt = db->aDb[i].pBt; |
| 125572 | if( pBt ){ |
| @@ -125652,17 +126820,24 @@ | |
| 125652 | int nKey1, const void *pKey1, |
| 125653 | int nKey2, const void *pKey2 |
| 125654 | ){ |
| 125655 | int rc, n; |
| 125656 | n = nKey1<nKey2 ? nKey1 : nKey2; |
| 125657 | rc = memcmp(pKey1, pKey2, n); |
| 125658 | if( rc==0 ){ |
| 125659 | if( padFlag |
| 125660 | && allSpaces(((char*)pKey1)+n, nKey1-n) |
| 125661 | && allSpaces(((char*)pKey2)+n, nKey2-n) |
| 125662 | ){ |
| 125663 | /* Leave rc unchanged at 0 */ |
| 125664 | }else{ |
| 125665 | rc = nKey1 - nKey2; |
| 125666 | } |
| 125667 | } |
| 125668 | return rc; |
| @@ -125693,24 +126868,42 @@ | |
| 125693 | |
| 125694 | /* |
| 125695 | ** Return the ROWID of the most recent insert |
| 125696 | */ |
| 125697 | SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ |
| 125698 | return db->lastRowid; |
| 125699 | } |
| 125700 | |
| 125701 | /* |
| 125702 | ** Return the number of changes in the most recent call to sqlite3_exec(). |
| 125703 | */ |
| 125704 | SQLITE_API int sqlite3_changes(sqlite3 *db){ |
| 125705 | return db->nChange; |
| 125706 | } |
| 125707 | |
| 125708 | /* |
| 125709 | ** Return the number of changes since the database handle was opened. |
| 125710 | */ |
| 125711 | SQLITE_API int sqlite3_total_changes(sqlite3 *db){ |
| 125712 | return db->nTotalChange; |
| 125713 | } |
| 125714 | |
| 125715 | /* |
| 125716 | ** Close all open savepoints. This function only manipulates fields of the |
| @@ -126255,10 +127448,13 @@ | |
| 126255 | SQLITE_API int sqlite3_busy_handler( |
| 126256 | sqlite3 *db, |
| 126257 | int (*xBusy)(void*,int), |
| 126258 | void *pArg |
| 126259 | ){ |
| 126260 | sqlite3_mutex_enter(db->mutex); |
| 126261 | db->busyHandler.xFunc = xBusy; |
| 126262 | db->busyHandler.pArg = pArg; |
| 126263 | db->busyHandler.nBusy = 0; |
| 126264 | db->busyTimeout = 0; |
| @@ -126276,10 +127472,16 @@ | |
| 126276 | sqlite3 *db, |
| 126277 | int nOps, |
| 126278 | int (*xProgress)(void*), |
| 126279 | void *pArg |
| 126280 | ){ |
| 126281 | sqlite3_mutex_enter(db->mutex); |
| 126282 | if( nOps>0 ){ |
| 126283 | db->xProgress = xProgress; |
| 126284 | db->nProgressOps = (unsigned)nOps; |
| 126285 | db->pProgressArg = pArg; |
| @@ -126296,10 +127498,13 @@ | |
| 126296 | /* |
| 126297 | ** This routine installs a default busy handler that waits for the |
| 126298 | ** specified number of milliseconds before returning 0. |
| 126299 | */ |
| 126300 | SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
| 126301 | if( ms>0 ){ |
| 126302 | sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); |
| 126303 | db->busyTimeout = ms; |
| 126304 | }else{ |
| 126305 | sqlite3_busy_handler(db, 0, 0); |
| @@ -126309,10 +127514,16 @@ | |
| 126309 | |
| 126310 | /* |
| 126311 | ** Cause any pending operation to stop at its earliest opportunity. |
| 126312 | */ |
| 126313 | SQLITE_API void sqlite3_interrupt(sqlite3 *db){ |
| 126314 | db->u1.isInterrupted = 1; |
| 126315 | } |
| 126316 | |
| 126317 | |
| 126318 | /* |
| @@ -126446,10 +127657,16 @@ | |
| 126446 | void (*xFinal)(sqlite3_context*), |
| 126447 | void (*xDestroy)(void *) |
| 126448 | ){ |
| 126449 | int rc = SQLITE_ERROR; |
| 126450 | FuncDestructor *pArg = 0; |
| 126451 | sqlite3_mutex_enter(db->mutex); |
| 126452 | if( xDestroy ){ |
| 126453 | pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); |
| 126454 | if( !pArg ){ |
| 126455 | xDestroy(p); |
| @@ -126482,10 +127699,14 @@ | |
| 126482 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 126483 | void (*xFinal)(sqlite3_context*) |
| 126484 | ){ |
| 126485 | int rc; |
| 126486 | char *zFunc8; |
| 126487 | sqlite3_mutex_enter(db->mutex); |
| 126488 | assert( !db->mallocFailed ); |
| 126489 | zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); |
| 126490 | rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0); |
| 126491 | sqlite3DbFree(db, zFunc8); |
| @@ -126513,10 +127734,16 @@ | |
| 126513 | const char *zName, |
| 126514 | int nArg |
| 126515 | ){ |
| 126516 | int nName = sqlite3Strlen30(zName); |
| 126517 | int rc = SQLITE_OK; |
| 126518 | sqlite3_mutex_enter(db->mutex); |
| 126519 | if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 126520 | rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 126521 | 0, sqlite3InvalidFunction, 0, 0, 0); |
| 126522 | } |
| @@ -126534,10 +127761,17 @@ | |
| 126534 | ** trace is a pointer to a function that is invoked at the start of each |
| 126535 | ** SQL statement. |
| 126536 | */ |
| 126537 | SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ |
| 126538 | void *pOld; |
| 126539 | sqlite3_mutex_enter(db->mutex); |
| 126540 | pOld = db->pTraceArg; |
| 126541 | db->xTrace = xTrace; |
| 126542 | db->pTraceArg = pArg; |
| 126543 | sqlite3_mutex_leave(db->mutex); |
| @@ -126555,10 +127789,17 @@ | |
| 126555 | sqlite3 *db, |
| 126556 | void (*xProfile)(void*,const char*,sqlite_uint64), |
| 126557 | void *pArg |
| 126558 | ){ |
| 126559 | void *pOld; |
| 126560 | sqlite3_mutex_enter(db->mutex); |
| 126561 | pOld = db->pProfileArg; |
| 126562 | db->xProfile = xProfile; |
| 126563 | db->pProfileArg = pArg; |
| 126564 | sqlite3_mutex_leave(db->mutex); |
| @@ -126575,10 +127816,17 @@ | |
| 126575 | sqlite3 *db, /* Attach the hook to this database */ |
| 126576 | int (*xCallback)(void*), /* Function to invoke on each commit */ |
| 126577 | void *pArg /* Argument to the function */ |
| 126578 | ){ |
| 126579 | void *pOld; |
| 126580 | sqlite3_mutex_enter(db->mutex); |
| 126581 | pOld = db->pCommitArg; |
| 126582 | db->xCommitCallback = xCallback; |
| 126583 | db->pCommitArg = pArg; |
| 126584 | sqlite3_mutex_leave(db->mutex); |
| @@ -126593,10 +127841,17 @@ | |
| 126593 | sqlite3 *db, /* Attach the hook to this database */ |
| 126594 | void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), |
| 126595 | void *pArg /* Argument to the function */ |
| 126596 | ){ |
| 126597 | void *pRet; |
| 126598 | sqlite3_mutex_enter(db->mutex); |
| 126599 | pRet = db->pUpdateArg; |
| 126600 | db->xUpdateCallback = xCallback; |
| 126601 | db->pUpdateArg = pArg; |
| 126602 | sqlite3_mutex_leave(db->mutex); |
| @@ -126611,10 +127866,17 @@ | |
| 126611 | sqlite3 *db, /* Attach the hook to this database */ |
| 126612 | void (*xCallback)(void*), /* Callback function */ |
| 126613 | void *pArg /* Argument to the function */ |
| 126614 | ){ |
| 126615 | void *pRet; |
| 126616 | sqlite3_mutex_enter(db->mutex); |
| 126617 | pRet = db->pRollbackArg; |
| 126618 | db->xRollbackCallback = xCallback; |
| 126619 | db->pRollbackArg = pArg; |
| 126620 | sqlite3_mutex_leave(db->mutex); |
| @@ -126657,10 +127919,13 @@ | |
| 126657 | SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ |
| 126658 | #ifdef SQLITE_OMIT_WAL |
| 126659 | UNUSED_PARAMETER(db); |
| 126660 | UNUSED_PARAMETER(nFrame); |
| 126661 | #else |
| 126662 | if( nFrame>0 ){ |
| 126663 | sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); |
| 126664 | }else{ |
| 126665 | sqlite3_wal_hook(db, 0, 0); |
| 126666 | } |
| @@ -126677,10 +127942,16 @@ | |
| 126677 | int(*xCallback)(void *, sqlite3*, const char*, int), |
| 126678 | void *pArg /* First argument passed to xCallback() */ |
| 126679 | ){ |
| 126680 | #ifndef SQLITE_OMIT_WAL |
| 126681 | void *pRet; |
| 126682 | sqlite3_mutex_enter(db->mutex); |
| 126683 | pRet = db->pWalArg; |
| 126684 | db->xWalCallback = xCallback; |
| 126685 | db->pWalArg = pArg; |
| 126686 | sqlite3_mutex_leave(db->mutex); |
| @@ -126703,10 +127974,14 @@ | |
| 126703 | #ifdef SQLITE_OMIT_WAL |
| 126704 | return SQLITE_OK; |
| 126705 | #else |
| 126706 | int rc; /* Return code */ |
| 126707 | int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */ |
| 126708 | |
| 126709 | /* Initialize the output variables to -1 in case an error occurs. */ |
| 126710 | if( pnLog ) *pnLog = -1; |
| 126711 | if( pnCkpt ) *pnCkpt = -1; |
| 126712 | |
| @@ -127100,10 +128375,16 @@ | |
| 127100 | ** from forming. |
| 127101 | */ |
| 127102 | SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ |
| 127103 | int oldLimit; |
| 127104 | |
| 127105 | |
| 127106 | /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME |
| 127107 | ** there is a hard upper bound set at compile-time by a C preprocessor |
| 127108 | ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to |
| 127109 | ** "_MAX_".) |
| @@ -127176,11 +128457,12 @@ | |
| 127176 | char c; |
| 127177 | int nUri = sqlite3Strlen30(zUri); |
| 127178 | |
| 127179 | assert( *pzErrMsg==0 ); |
| 127180 | |
| 127181 | if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) |
| 127182 | && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ |
| 127183 | ){ |
| 127184 | char *zOpt; |
| 127185 | int eState; /* Parser state when parsing URI */ |
| 127186 | int iIn; /* Input character index */ |
| @@ -127385,10 +128667,13 @@ | |
| 127385 | int rc; /* Return code */ |
| 127386 | int isThreadsafe; /* True for threadsafe connections */ |
| 127387 | char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ |
| 127388 | char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ |
| 127389 | |
| 127390 | *ppDb = 0; |
| 127391 | #ifndef SQLITE_OMIT_AUTOINIT |
| 127392 | rc = sqlite3_initialize(); |
| 127393 | if( rc ) return rc; |
| 127394 | #endif |
| @@ -127499,24 +128784,28 @@ | |
| 127499 | #endif |
| 127500 | |
| 127501 | /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
| 127502 | ** and UTF-16, so add a version for each to avoid any unnecessary |
| 127503 | ** conversions. The only error that can occur here is a malloc() failure. |
| 127504 | */ |
| 127505 | createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0); |
| 127506 | createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0); |
| 127507 | createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0); |
| 127508 | createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); |
| 127509 | if( db->mallocFailed ){ |
| 127510 | goto opendb_out; |
| 127511 | } |
| 127512 | db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); |
| 127513 | assert( db->pDfltColl!=0 ); |
| 127514 | |
| 127515 | /* Also add a UTF-8 case-insensitive collation sequence. */ |
| 127516 | createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); |
| 127517 | |
| 127518 | /* Parse the filename/URI argument. */ |
| 127519 | db->openFlags = flags; |
| 127520 | rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); |
| 127521 | if( rc!=SQLITE_OK ){ |
| 127522 | if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; |
| @@ -127674,17 +128963,19 @@ | |
| 127674 | ){ |
| 127675 | char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
| 127676 | sqlite3_value *pVal; |
| 127677 | int rc; |
| 127678 | |
| 127679 | assert( zFilename ); |
| 127680 | assert( ppDb ); |
| 127681 | *ppDb = 0; |
| 127682 | #ifndef SQLITE_OMIT_AUTOINIT |
| 127683 | rc = sqlite3_initialize(); |
| 127684 | if( rc ) return rc; |
| 127685 | #endif |
| 127686 | pVal = sqlite3ValueNew(0); |
| 127687 | sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 127688 | zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
| 127689 | if( zFilename8 ){ |
| 127690 | rc = openDatabase(zFilename8, ppDb, |
| @@ -127710,17 +129001,11 @@ | |
| 127710 | const char *zName, |
| 127711 | int enc, |
| 127712 | void* pCtx, |
| 127713 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 127714 | ){ |
| 127715 | int rc; |
| 127716 | sqlite3_mutex_enter(db->mutex); |
| 127717 | assert( !db->mallocFailed ); |
| 127718 | rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0); |
| 127719 | rc = sqlite3ApiExit(db, rc); |
| 127720 | sqlite3_mutex_leave(db->mutex); |
| 127721 | return rc; |
| 127722 | } |
| 127723 | |
| 127724 | /* |
| 127725 | ** Register a new collation sequence with the database handle db. |
| 127726 | */ |
| @@ -127731,10 +129016,14 @@ | |
| 127731 | void* pCtx, |
| 127732 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 127733 | void(*xDel)(void*) |
| 127734 | ){ |
| 127735 | int rc; |
| 127736 | sqlite3_mutex_enter(db->mutex); |
| 127737 | assert( !db->mallocFailed ); |
| 127738 | rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel); |
| 127739 | rc = sqlite3ApiExit(db, rc); |
| 127740 | sqlite3_mutex_leave(db->mutex); |
| @@ -127752,10 +129041,14 @@ | |
| 127752 | void* pCtx, |
| 127753 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 127754 | ){ |
| 127755 | int rc = SQLITE_OK; |
| 127756 | char *zName8; |
| 127757 | sqlite3_mutex_enter(db->mutex); |
| 127758 | assert( !db->mallocFailed ); |
| 127759 | zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); |
| 127760 | if( zName8 ){ |
| 127761 | rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0); |
| @@ -127774,10 +129067,13 @@ | |
| 127774 | SQLITE_API int sqlite3_collation_needed( |
| 127775 | sqlite3 *db, |
| 127776 | void *pCollNeededArg, |
| 127777 | void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) |
| 127778 | ){ |
| 127779 | sqlite3_mutex_enter(db->mutex); |
| 127780 | db->xCollNeeded = xCollNeeded; |
| 127781 | db->xCollNeeded16 = 0; |
| 127782 | db->pCollNeededArg = pCollNeededArg; |
| 127783 | sqlite3_mutex_leave(db->mutex); |
| @@ -127792,10 +129088,13 @@ | |
| 127792 | SQLITE_API int sqlite3_collation_needed16( |
| 127793 | sqlite3 *db, |
| 127794 | void *pCollNeededArg, |
| 127795 | void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) |
| 127796 | ){ |
| 127797 | sqlite3_mutex_enter(db->mutex); |
| 127798 | db->xCollNeeded = 0; |
| 127799 | db->xCollNeeded16 = xCollNeeded16; |
| 127800 | db->pCollNeededArg = pCollNeededArg; |
| 127801 | sqlite3_mutex_leave(db->mutex); |
| @@ -127818,10 +129117,16 @@ | |
| 127818 | ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 127819 | ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 127820 | ** by the next COMMIT or ROLLBACK. |
| 127821 | */ |
| 127822 | SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){ |
| 127823 | return db->autoCommit; |
| 127824 | } |
| 127825 | |
| 127826 | /* |
| 127827 | ** The following routines are substitutes for constants SQLITE_CORRUPT, |
| @@ -128000,10 +129305,13 @@ | |
| 128000 | |
| 128001 | /* |
| 128002 | ** Enable or disable the extended result codes. |
| 128003 | */ |
| 128004 | SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ |
| 128005 | sqlite3_mutex_enter(db->mutex); |
| 128006 | db->errMask = onoff ? 0xffffffff : 0xff; |
| 128007 | sqlite3_mutex_leave(db->mutex); |
| 128008 | return SQLITE_OK; |
| 128009 | } |
| @@ -128013,10 +129321,13 @@ | |
| 128013 | */ |
| 128014 | SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ |
| 128015 | int rc = SQLITE_ERROR; |
| 128016 | Btree *pBtree; |
| 128017 | |
| 128018 | sqlite3_mutex_enter(db->mutex); |
| 128019 | pBtree = sqlite3DbNameToBtree(db, zDbName); |
| 128020 | if( pBtree ){ |
| 128021 | Pager *pPager; |
| 128022 | sqlite3_file *fd; |
| @@ -128355,11 +129666,11 @@ | |
| 128355 | ** query parameter we seek. This routine returns the value of the zParam |
| 128356 | ** parameter if it exists. If the parameter does not exist, this routine |
| 128357 | ** returns a NULL pointer. |
| 128358 | */ |
| 128359 | SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){ |
| 128360 | if( zFilename==0 ) return 0; |
| 128361 | zFilename += sqlite3Strlen30(zFilename) + 1; |
| 128362 | while( zFilename[0] ){ |
| 128363 | int x = strcmp(zFilename, zParam); |
| 128364 | zFilename += sqlite3Strlen30(zFilename) + 1; |
| 128365 | if( x==0 ) return zFilename; |
| @@ -128411,19 +129722,31 @@ | |
| 128411 | /* |
| 128412 | ** Return the filename of the database associated with a database |
| 128413 | ** connection. |
| 128414 | */ |
| 128415 | SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){ |
| 128416 | Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 128417 | return pBt ? sqlite3BtreeGetFilename(pBt) : 0; |
| 128418 | } |
| 128419 | |
| 128420 | /* |
| 128421 | ** Return 1 if database is read-only or 0 if read/write. Return -1 if |
| 128422 | ** no such database exists. |
| 128423 | */ |
| 128424 | SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){ |
| 128425 | Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 128426 | return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; |
| 128427 | } |
| 128428 | |
| 128429 | /************** End of main.c ************************************************/ |
| 128430 |
| --- src/sqlite3.c | |
| +++ src/sqlite3.c | |
| @@ -1,8 +1,8 @@ | |
| 1 | /****************************************************************************** |
| 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | ** version 3.8.8. By combining all the individual C code files into this |
| 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | ** translation unit. |
| @@ -179,11 +179,11 @@ | |
| 179 | |
| 180 | |
| 181 | /* |
| 182 | ** These no-op macros are used in front of interfaces to mark those |
| 183 | ** interfaces as either deprecated or experimental. New applications |
| 184 | ** should not use deprecated interfaces - they are supported for backwards |
| 185 | ** compatibility only. Application writers should be aware that |
| 186 | ** experimental interfaces are subject to change in point releases. |
| 187 | ** |
| 188 | ** These macros used to resolve to various kinds of compiler magic that |
| 189 | ** would generate warning messages when they were used. But that |
| @@ -229,13 +229,13 @@ | |
| 229 | ** |
| 230 | ** See also: [sqlite3_libversion()], |
| 231 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 232 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 233 | */ |
| 234 | #define SQLITE_VERSION "3.8.8" |
| 235 | #define SQLITE_VERSION_NUMBER 3008008 |
| 236 | #define SQLITE_SOURCE_ID "2014-11-27 11:36:36 f095cde579e7417306e11b5c1d2dd90b6bb547d5" |
| 237 | |
| 238 | /* |
| 239 | ** CAPI3REF: Run-Time Library Version Numbers |
| 240 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 241 | ** |
| @@ -1626,29 +1626,31 @@ | |
| 1626 | ** it is not possible to set the Serialized [threading mode] and |
| 1627 | ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the |
| 1628 | ** SQLITE_CONFIG_SERIALIZED configuration option.</dd> |
| 1629 | ** |
| 1630 | ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt> |
| 1631 | ** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is |
| 1632 | ** a pointer to an instance of the [sqlite3_mem_methods] structure. |
| 1633 | ** The argument specifies |
| 1634 | ** alternative low-level memory allocation routines to be used in place of |
| 1635 | ** the memory allocation routines built into SQLite.)^ ^SQLite makes |
| 1636 | ** its own private copy of the content of the [sqlite3_mem_methods] structure |
| 1637 | ** before the [sqlite3_config()] call returns.</dd> |
| 1638 | ** |
| 1639 | ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt> |
| 1640 | ** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which |
| 1641 | ** is a pointer to an instance of the [sqlite3_mem_methods] structure. |
| 1642 | ** The [sqlite3_mem_methods] |
| 1643 | ** structure is filled with the currently defined memory allocation routines.)^ |
| 1644 | ** This option can be used to overload the default memory allocation |
| 1645 | ** routines with a wrapper that simulations memory allocation failure or |
| 1646 | ** tracks memory usage, for example. </dd> |
| 1647 | ** |
| 1648 | ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt> |
| 1649 | ** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int, |
| 1650 | ** interpreted as a boolean, which enables or disables the collection of |
| 1651 | ** memory allocation statistics. ^(When memory allocation statistics are disabled, the |
| 1652 | ** following SQLite interfaces become non-operational: |
| 1653 | ** <ul> |
| 1654 | ** <li> [sqlite3_memory_used()] |
| 1655 | ** <li> [sqlite3_memory_highwater()] |
| 1656 | ** <li> [sqlite3_soft_heap_limit64()] |
| @@ -1658,78 +1660,90 @@ | |
| 1660 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1661 | ** allocation statistics are disabled by default. |
| 1662 | ** </dd> |
| 1663 | ** |
| 1664 | ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt> |
| 1665 | ** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer |
| 1666 | ** that SQLite can use for scratch memory. ^(There are three arguments |
| 1667 | ** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte |
| 1668 | ** aligned memory buffer from which the scratch allocations will be |
| 1669 | ** drawn, the size of each scratch allocation (sz), |
| 1670 | ** and the maximum number of scratch allocations (N).)^ |
| 1671 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1672 | ** of at least sz*N bytes of memory. |
| 1673 | ** ^SQLite will not use more than one scratch buffers per thread. |
| 1674 | ** ^SQLite will never request a scratch buffer that is more than 6 |
| 1675 | ** times the database page size. |
| 1676 | ** ^If SQLite needs needs additional |
| 1677 | ** scratch memory beyond what is provided by this configuration option, then |
| 1678 | ** [sqlite3_malloc()] will be used to obtain the memory needed.<p> |
| 1679 | ** ^When the application provides any amount of scratch memory using |
| 1680 | ** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large |
| 1681 | ** [sqlite3_malloc|heap allocations]. |
| 1682 | ** This can help [Robson proof|prevent memory allocation failures] due to heap |
| 1683 | ** fragmentation in low-memory embedded systems. |
| 1684 | ** </dd> |
| 1685 | ** |
| 1686 | ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1687 | ** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer |
| 1688 | ** that SQLite can use for the database page cache with the default page |
| 1689 | ** cache implementation. |
| 1690 | ** This configuration should not be used if an application-define page |
| 1691 | ** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2] |
| 1692 | ** configuration option. |
| 1693 | ** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned |
| 1694 | ** memory, the size of each page buffer (sz), and the number of pages (N). |
| 1695 | ** The sz argument should be the size of the largest database page |
| 1696 | ** (a power of two between 512 and 32768) plus some extra bytes for each |
| 1697 | ** page header. ^The number of extra bytes needed by the page header |
| 1698 | ** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option |
| 1699 | ** to [sqlite3_config()]. |
| 1700 | ** ^It is harmless, apart from the wasted memory, |
| 1701 | ** for the sz parameter to be larger than necessary. The first |
| 1702 | ** argument should pointer to an 8-byte aligned block of memory that |
| 1703 | ** is at least sz*N bytes of memory, otherwise subsequent behavior is |
| 1704 | ** undefined. |
| 1705 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1706 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1707 | ** page cache memory is needed beyond what is provided by this option, then |
| 1708 | ** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd> |
| 1709 | ** |
| 1710 | ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt> |
| 1711 | ** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer |
| 1712 | ** that SQLite will use for all of its dynamic memory allocation needs |
| 1713 | ** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE]. |
| 1714 | ** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled |
| 1715 | ** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns |
| 1716 | ** [SQLITE_ERROR] if invoked otherwise. |
| 1717 | ** ^There are three arguments to SQLITE_CONFIG_HEAP: |
| 1718 | ** An 8-byte aligned pointer to the memory, |
| 1719 | ** the number of bytes in the memory buffer, and the minimum allocation size. |
| 1720 | ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts |
| 1721 | ** to using its default memory allocator (the system malloc() implementation), |
| 1722 | ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the |
| 1723 | ** memory pointer is not NULL then the alternative memory |
| 1724 | ** allocator is engaged to handle all of SQLites memory allocation needs. |
| 1725 | ** The first pointer (the memory pointer) must be aligned to an 8-byte |
| 1726 | ** boundary or subsequent behavior of SQLite will be undefined. |
| 1727 | ** The minimum allocation size is capped at 2**12. Reasonable values |
| 1728 | ** for the minimum allocation size are 2**5 through 2**8.</dd> |
| 1729 | ** |
| 1730 | ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt> |
| 1731 | ** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a |
| 1732 | ** pointer to an instance of the [sqlite3_mutex_methods] structure. |
| 1733 | ** The argument specifies alternative low-level mutex routines to be used in place |
| 1734 | ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the |
| 1735 | ** content of the [sqlite3_mutex_methods] structure before the call to |
| 1736 | ** [sqlite3_config()] returns. ^If SQLite is compiled with |
| 1737 | ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then |
| 1738 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1739 | ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will |
| 1740 | ** return [SQLITE_ERROR].</dd> |
| 1741 | ** |
| 1742 | ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt> |
| 1743 | ** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which |
| 1744 | ** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The |
| 1745 | ** [sqlite3_mutex_methods] |
| 1746 | ** structure is filled with the currently defined mutex routines.)^ |
| 1747 | ** This option can be used to overload the default mutex allocation |
| 1748 | ** routines with a wrapper used to track mutex usage for performance |
| 1749 | ** profiling or testing, for example. ^If SQLite is compiled with |
| @@ -1737,28 +1751,28 @@ | |
| 1751 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1752 | ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will |
| 1753 | ** return [SQLITE_ERROR].</dd> |
| 1754 | ** |
| 1755 | ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt> |
| 1756 | ** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine |
| 1757 | ** the default size of lookaside memory on each [database connection]. |
| 1758 | ** The first argument is the |
| 1759 | ** size of each lookaside buffer slot and the second is the number of |
| 1760 | ** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE |
| 1761 | ** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] |
| 1762 | ** option to [sqlite3_db_config()] can be used to change the lookaside |
| 1763 | ** configuration on individual connections.)^ </dd> |
| 1764 | ** |
| 1765 | ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt> |
| 1766 | ** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is |
| 1767 | ** a pointer to an [sqlite3_pcache_methods2] object. This object specifies |
| 1768 | ** the interface to a custom page cache implementation.)^ |
| 1769 | ** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd> |
| 1770 | ** |
| 1771 | ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt> |
| 1772 | ** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which |
| 1773 | ** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of the current |
| 1774 | ** page cache implementation into that object.)^ </dd> |
| 1775 | ** |
| 1776 | ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt> |
| 1777 | ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite |
| 1778 | ** global [error log]. |
| @@ -1778,26 +1792,27 @@ | |
| 1792 | ** supplied by the application must not invoke any SQLite interface. |
| 1793 | ** In a multi-threaded application, the application-defined logger |
| 1794 | ** function must be threadsafe. </dd> |
| 1795 | ** |
| 1796 | ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI |
| 1797 | ** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int. |
| 1798 | ** If non-zero, then URI handling is globally enabled. If the parameter is zero, |
| 1799 | ** then URI handling is globally disabled.)^ ^If URI handling is globally enabled, |
| 1800 | ** all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or |
| 1801 | ** specified as part of [ATTACH] commands are interpreted as URIs, regardless |
| 1802 | ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database |
| 1803 | ** connection is opened. ^If it is globally disabled, filenames are |
| 1804 | ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the |
| 1805 | ** database connection is opened. ^(By default, URI handling is globally |
| 1806 | ** disabled. The default value may be changed by compiling with the |
| 1807 | ** [SQLITE_USE_URI] symbol defined.)^ |
| 1808 | ** |
| 1809 | ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN |
| 1810 | ** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer |
| 1811 | ** argument which is interpreted as a boolean in order to enable or disable |
| 1812 | ** the use of covering indices for full table scans in the query optimizer. |
| 1813 | ** ^The default setting is determined |
| 1814 | ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on" |
| 1815 | ** if that compile-time option is omitted. |
| 1816 | ** The ability to disable the use of covering indices for full table scans |
| 1817 | ** is because some incorrectly coded legacy applications might malfunction |
| 1818 | ** when the optimization is enabled. Providing the ability to |
| @@ -1833,23 +1848,32 @@ | |
| 1848 | ** that are the default mmap size limit (the default setting for |
| 1849 | ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit. |
| 1850 | ** ^The default setting can be overridden by each database connection using |
| 1851 | ** either the [PRAGMA mmap_size] command, or by using the |
| 1852 | ** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size |
| 1853 | ** will be silently truncated if necessary so that it does not exceed the |
| 1854 | ** compile-time maximum mmap size set by the |
| 1855 | ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^ |
| 1856 | ** ^If either argument to this option is negative, then that argument is |
| 1857 | ** changed to its compile-time default. |
| 1858 | ** |
| 1859 | ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]] |
| 1860 | ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE |
| 1861 | ** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is |
| 1862 | ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro defined. |
| 1863 | ** ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value |
| 1864 | ** that specifies the maximum size of the created heap. |
| 1865 | ** </dl> |
| 1866 | ** |
| 1867 | ** [[SQLITE_CONFIG_PCACHE_HDRSZ]] |
| 1868 | ** <dt>SQLITE_CONFIG_PCACHE_HDRSZ |
| 1869 | ** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which |
| 1870 | ** is a pointer to an integer and writes into that integer the number of extra |
| 1871 | ** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. The amount of |
| 1872 | ** extra space required can change depending on the compiler, |
| 1873 | ** target platform, and SQLite version. |
| 1874 | ** </dl> |
| 1875 | */ |
| 1876 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 1877 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 1878 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| 1879 | #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ |
| @@ -1870,10 +1894,11 @@ | |
| 1894 | #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ |
| 1895 | #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ |
| 1896 | #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ |
| 1897 | #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ |
| 1898 | #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ |
| 1899 | #define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */ |
| 1900 | |
| 1901 | /* |
| 1902 | ** CAPI3REF: Database Connection Configuration Options |
| 1903 | ** |
| 1904 | ** These constants are the available integer configuration options that |
| @@ -1997,51 +2022,49 @@ | |
| 2022 | SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); |
| 2023 | |
| 2024 | /* |
| 2025 | ** CAPI3REF: Count The Number Of Rows Modified |
| 2026 | ** |
| 2027 | ** ^This function returns the number of rows modified, inserted or |
| 2028 | ** deleted by the most recently completed INSERT, UPDATE or DELETE |
| 2029 | ** statement on the database connection specified by the only parameter. |
| 2030 | ** ^Executing any other type of SQL statement does not modify the value |
| 2031 | ** returned by this function. |
| 2032 | ** |
| 2033 | ** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are |
| 2034 | ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], |
| 2035 | ** [foreign key actions] or [REPLACE] constraint resolution are not counted. |
| 2036 | ** |
| 2037 | ** Changes to a view that are intercepted by |
| 2038 | ** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value |
| 2039 | ** returned by sqlite3_changes() immediately after an INSERT, UPDATE or |
| 2040 | ** DELETE statement run on a view is always zero. Only changes made to real |
| 2041 | ** tables are counted. |
| 2042 | ** |
| 2043 | ** Things are more complicated if the sqlite3_changes() function is |
| 2044 | ** executed while a trigger program is running. This may happen if the |
| 2045 | ** program uses the [changes() SQL function], or if some other callback |
| 2046 | ** function invokes sqlite3_changes() directly. Essentially: |
| 2047 | ** |
| 2048 | ** <ul> |
| 2049 | ** <li> ^(Before entering a trigger program the value returned by |
| 2050 | ** sqlite3_changes() function is saved. After the trigger program |
| 2051 | ** has finished, the original value is restored.)^ |
| 2052 | ** |
| 2053 | ** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE |
| 2054 | ** statement sets the value returned by sqlite3_changes() |
| 2055 | ** upon completion as normal. Of course, this value will not include |
| 2056 | ** any changes performed by sub-triggers, as the sqlite3_changes() |
| 2057 | ** value will be saved and restored after each sub-trigger has run.)^ |
| 2058 | ** </ul> |
| 2059 | ** |
| 2060 | ** ^This means that if the changes() SQL function (or similar) is used |
| 2061 | ** by the first INSERT, UPDATE or DELETE statement within a trigger, it |
| 2062 | ** returns the value as set when the calling statement began executing. |
| 2063 | ** ^If it is used by the second or subsequent such statement within a trigger |
| 2064 | ** program, the value returned reflects the number of rows modified by the |
| 2065 | ** previous INSERT, UPDATE or DELETE statement within the same trigger. |
| 2066 | ** |
| 2067 | ** See also the [sqlite3_total_changes()] interface, the |
| 2068 | ** [count_changes pragma], and the [changes() SQL function]. |
| 2069 | ** |
| 2070 | ** If a separate thread makes changes on the same database connection |
| @@ -2051,24 +2074,21 @@ | |
| 2074 | SQLITE_API int sqlite3_changes(sqlite3*); |
| 2075 | |
| 2076 | /* |
| 2077 | ** CAPI3REF: Total Number Of Rows Modified |
| 2078 | ** |
| 2079 | ** ^This function returns the total number of rows inserted, modified or |
| 2080 | ** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed |
| 2081 | ** since the database connection was opened, including those executed as |
| 2082 | ** part of trigger programs. ^Executing any other type of SQL statement |
| 2083 | ** does not affect the value returned by sqlite3_total_changes(). |
| 2084 | ** |
| 2085 | ** ^Changes made as part of [foreign key actions] are included in the |
| 2086 | ** count, but those made as part of REPLACE constraint resolution are |
| 2087 | ** not. ^Changes to a view that are intercepted by INSTEAD OF triggers |
| 2088 | ** are not counted. |
| 2089 | ** |
| 2090 | ** See also the [sqlite3_changes()] interface, the |
| 2091 | ** [count_changes pragma], and the [total_changes() SQL function]. |
| 2092 | ** |
| 2093 | ** If a separate thread makes changes on the same database connection |
| 2094 | ** while [sqlite3_total_changes()] is running then the value |
| @@ -2542,17 +2562,18 @@ | |
| 2562 | ** already uses the largest possible [ROWID]. The PRNG is also used for |
| 2563 | ** the build-in random() and randomblob() SQL functions. This interface allows |
| 2564 | ** applications to access the same PRNG for other purposes. |
| 2565 | ** |
| 2566 | ** ^A call to this routine stores N bytes of randomness into buffer P. |
| 2567 | ** ^The P parameter can be a NULL pointer. |
| 2568 | ** |
| 2569 | ** ^If this routine has not been previously called or if the previous |
| 2570 | ** call had N less than one or a NULL pointer for P, then the PRNG is |
| 2571 | ** seeded using randomness obtained from the xRandomness method of |
| 2572 | ** the default [sqlite3_vfs] object. |
| 2573 | ** ^If the previous call to this routine had an N of 1 or more and a |
| 2574 | ** non-NULL P then the pseudo-randomness is generated |
| 2575 | ** internally and without recourse to the [sqlite3_vfs] xRandomness |
| 2576 | ** method. |
| 2577 | */ |
| 2578 | SQLITE_API void sqlite3_randomness(int N, void *P); |
| 2579 | |
| @@ -4270,13 +4291,13 @@ | |
| 4291 | ** CAPI3REF: Text Encodings |
| 4292 | ** |
| 4293 | ** These constant define integer codes that represent the various |
| 4294 | ** text encodings supported by SQLite. |
| 4295 | */ |
| 4296 | #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */ |
| 4297 | #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */ |
| 4298 | #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */ |
| 4299 | #define SQLITE_UTF16 4 /* Use native byte order */ |
| 4300 | #define SQLITE_ANY 5 /* Deprecated */ |
| 4301 | #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ |
| 4302 | |
| 4303 | /* |
| @@ -5762,31 +5783,47 @@ | |
| 5783 | ** in other words, the same BLOB that would be selected by: |
| 5784 | ** |
| 5785 | ** <pre> |
| 5786 | ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow; |
| 5787 | ** </pre>)^ |
| 5788 | ** |
| 5789 | ** ^(Parameter zDb is not the filename that contains the database, but |
| 5790 | ** rather the symbolic name of the database. For attached databases, this is |
| 5791 | ** the name that appears after the AS keyword in the [ATTACH] statement. |
| 5792 | ** For the main database file, the database name is "main". For TEMP |
| 5793 | ** tables, the database name is "temp".)^ |
| 5794 | ** |
| 5795 | ** ^If the flags parameter is non-zero, then the BLOB is opened for read |
| 5796 | ** and write access. ^If the flags parameter is zero, the BLOB is opened for |
| 5797 | ** read-only access. |
| 5798 | ** |
| 5799 | ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored |
| 5800 | ** in *ppBlob. Otherwise an [error code] is returned and, unless the error |
| 5801 | ** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided |
| 5802 | ** the API is not misused, it is always safe to call [sqlite3_blob_close()] |
| 5803 | ** on *ppBlob after this function it returns. |
| 5804 | ** |
| 5805 | ** This function fails with SQLITE_ERROR if any of the following are true: |
| 5806 | ** <ul> |
| 5807 | ** <li> ^(Database zDb does not exist)^, |
| 5808 | ** <li> ^(Table zTable does not exist within database zDb)^, |
| 5809 | ** <li> ^(Table zTable is a WITHOUT ROWID table)^, |
| 5810 | ** <li> ^(Column zColumn does not exist)^, |
| 5811 | ** <li> ^(Row iRow is not present in the table)^, |
| 5812 | ** <li> ^(The specified column of row iRow contains a value that is not |
| 5813 | ** a TEXT or BLOB value)^, |
| 5814 | ** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE |
| 5815 | ** constraint and the blob is being opened for read/write access)^, |
| 5816 | ** <li> ^([foreign key constraints | Foreign key constraints] are enabled, |
| 5817 | ** column zColumn is part of a [child key] definition and the blob is |
| 5818 | ** being opened for read/write access)^. |
| 5819 | ** </ul> |
| 5820 | ** |
| 5821 | ** ^Unless it returns SQLITE_MISUSE, this function sets the |
| 5822 | ** [database connection] error code and message accessible via |
| 5823 | ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. |
| 5824 | ** |
| 5825 | ** |
| 5826 | ** ^(If the row that a BLOB handle points to is modified by an |
| 5827 | ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects |
| 5828 | ** then the BLOB handle is marked as "expired". |
| 5829 | ** This is true if any column of the row is changed, even a column |
| @@ -5800,17 +5837,13 @@ | |
| 5837 | ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of |
| 5838 | ** the opened blob. ^The size of a blob may not be changed by this |
| 5839 | ** interface. Use the [UPDATE] SQL command to change the size of a |
| 5840 | ** blob. |
| 5841 | ** |
| 5842 | ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces |
| 5843 | ** and the built-in [zeroblob] SQL function may be used to create a |
| 5844 | ** zero-filled blob to read or write using the incremental-blob interface. |
| 5845 | ** |
| 5846 | ** To avoid a resource leak, every open [BLOB handle] should eventually |
| 5847 | ** be released by a call to [sqlite3_blob_close()]. |
| 5848 | */ |
| 5849 | SQLITE_API int sqlite3_blob_open( |
| @@ -5848,28 +5881,26 @@ | |
| 5881 | SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); |
| 5882 | |
| 5883 | /* |
| 5884 | ** CAPI3REF: Close A BLOB Handle |
| 5885 | ** |
| 5886 | ** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed |
| 5887 | ** unconditionally. Even if this routine returns an error code, the |
| 5888 | ** handle is still closed.)^ |
| 5889 | ** |
| 5890 | ** ^If the blob handle being closed was opened for read-write access, and if |
| 5891 | ** the database is in auto-commit mode and there are no other open read-write |
| 5892 | ** blob handles or active write statements, the current transaction is |
| 5893 | ** committed. ^If an error occurs while committing the transaction, an error |
| 5894 | ** code is returned and the transaction rolled back. |
| 5895 | ** |
| 5896 | ** Calling this function with an argument that is not a NULL pointer or an |
| 5897 | ** open blob handle results in undefined behaviour. ^Calling this routine |
| 5898 | ** with a null pointer (such as would be returned by a failed call to |
| 5899 | ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function |
| 5900 | ** is passed a valid open blob handle, the values returned by the |
| 5901 | ** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning. |
| 5902 | */ |
| 5903 | SQLITE_API int sqlite3_blob_close(sqlite3_blob *); |
| 5904 | |
| 5905 | /* |
| 5906 | ** CAPI3REF: Return The Size Of An Open BLOB |
| @@ -5915,36 +5946,39 @@ | |
| 5946 | SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); |
| 5947 | |
| 5948 | /* |
| 5949 | ** CAPI3REF: Write Data Into A BLOB Incrementally |
| 5950 | ** |
| 5951 | ** ^(This function is used to write data into an open [BLOB handle] from a |
| 5952 | ** caller-supplied buffer. N bytes of data are copied from the buffer Z |
| 5953 | ** into the open BLOB, starting at offset iOffset.)^ |
| 5954 | ** |
| 5955 | ** ^(On success, sqlite3_blob_write() returns SQLITE_OK. |
| 5956 | ** Otherwise, an [error code] or an [extended error code] is returned.)^ |
| 5957 | ** ^Unless SQLITE_MISUSE is returned, this function sets the |
| 5958 | ** [database connection] error code and message accessible via |
| 5959 | ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. |
| 5960 | ** |
| 5961 | ** ^If the [BLOB handle] passed as the first argument was not opened for |
| 5962 | ** writing (the flags parameter to [sqlite3_blob_open()] was zero), |
| 5963 | ** this function returns [SQLITE_READONLY]. |
| 5964 | ** |
| 5965 | ** This function may only modify the contents of the BLOB; it is |
| 5966 | ** not possible to increase the size of a BLOB using this API. |
| 5967 | ** ^If offset iOffset is less than N bytes from the end of the BLOB, |
| 5968 | ** [SQLITE_ERROR] is returned and no data is written. The size of the |
| 5969 | ** BLOB (and hence the maximum value of N+iOffset) can be determined |
| 5970 | ** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less |
| 5971 | ** than zero [SQLITE_ERROR] is returned and no data is written. |
| 5972 | ** |
| 5973 | ** ^An attempt to write to an expired [BLOB handle] fails with an |
| 5974 | ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred |
| 5975 | ** before the [BLOB handle] expired are not rolled back by the |
| 5976 | ** expiration of the handle, though of course those changes might |
| 5977 | ** have been overwritten by the statement that expired the BLOB handle |
| 5978 | ** or by other independent statements. |
| 5979 | ** |
| 5980 | ** This routine only works on a [BLOB handle] which has been created |
| 5981 | ** by a prior successful call to [sqlite3_blob_open()] and which has not |
| 5982 | ** been closed by [sqlite3_blob_close()]. Passing any other pointer in |
| 5983 | ** to this routine results in undefined and probably undesirable behavior. |
| 5984 | ** |
| @@ -5993,38 +6027,38 @@ | |
| 6027 | ** use by SQLite, code that links against SQLite is |
| 6028 | ** permitted to use any of these routines. |
| 6029 | ** |
| 6030 | ** The SQLite source code contains multiple implementations |
| 6031 | ** of these mutex routines. An appropriate implementation |
| 6032 | ** is selected automatically at compile-time. The following |
| 6033 | ** implementations are available in the SQLite core: |
| 6034 | ** |
| 6035 | ** <ul> |
| 6036 | ** <li> SQLITE_MUTEX_PTHREADS |
| 6037 | ** <li> SQLITE_MUTEX_W32 |
| 6038 | ** <li> SQLITE_MUTEX_NOOP |
| 6039 | ** </ul> |
| 6040 | ** |
| 6041 | ** The SQLITE_MUTEX_NOOP implementation is a set of routines |
| 6042 | ** that does no real locking and is appropriate for use in |
| 6043 | ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and |
| 6044 | ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix |
| 6045 | ** and Windows. |
| 6046 | ** |
| 6047 | ** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor |
| 6048 | ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex |
| 6049 | ** implementation is included with the library. In this case the |
| 6050 | ** application must supply a custom mutex implementation using the |
| 6051 | ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function |
| 6052 | ** before calling sqlite3_initialize() or any other public sqlite3_ |
| 6053 | ** function that calls sqlite3_initialize(). |
| 6054 | ** |
| 6055 | ** ^The sqlite3_mutex_alloc() routine allocates a new |
| 6056 | ** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc() |
| 6057 | ** routine returns NULL if it is unable to allocate the requested |
| 6058 | ** mutex. The argument to sqlite3_mutex_alloc() must one of these |
| 6059 | ** integer constants: |
| 6060 | ** |
| 6061 | ** <ul> |
| 6062 | ** <li> SQLITE_MUTEX_FAST |
| 6063 | ** <li> SQLITE_MUTEX_RECURSIVE |
| 6064 | ** <li> SQLITE_MUTEX_STATIC_MASTER |
| @@ -6033,68 +6067,64 @@ | |
| 6067 | ** <li> SQLITE_MUTEX_STATIC_PRNG |
| 6068 | ** <li> SQLITE_MUTEX_STATIC_LRU |
| 6069 | ** <li> SQLITE_MUTEX_STATIC_PMEM |
| 6070 | ** <li> SQLITE_MUTEX_STATIC_APP1 |
| 6071 | ** <li> SQLITE_MUTEX_STATIC_APP2 |
| 6072 | ** <li> SQLITE_MUTEX_STATIC_APP3 |
| 6073 | ** </ul> |
| 6074 | ** |
| 6075 | ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) |
| 6076 | ** cause sqlite3_mutex_alloc() to create |
| 6077 | ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE |
| 6078 | ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. |
| 6079 | ** The mutex implementation does not need to make a distinction |
| 6080 | ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does |
| 6081 | ** not want to. SQLite will only request a recursive mutex in |
| 6082 | ** cases where it really needs one. If a faster non-recursive mutex |
| 6083 | ** implementation is available on the host platform, the mutex subsystem |
| 6084 | ** might return such a mutex in response to SQLITE_MUTEX_FAST. |
| 6085 | ** |
| 6086 | ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other |
| 6087 | ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return |
| 6088 | ** a pointer to a static preexisting mutex. ^Nine static mutexes are |
| 6089 | ** used by the current version of SQLite. Future versions of SQLite |
| 6090 | ** may add additional static mutexes. Static mutexes are for internal |
| 6091 | ** use by SQLite only. Applications that use SQLite mutexes should |
| 6092 | ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or |
| 6093 | ** SQLITE_MUTEX_RECURSIVE. |
| 6094 | ** |
| 6095 | ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST |
| 6096 | ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() |
| 6097 | ** returns a different mutex on every call. ^For the static |
| 6098 | ** mutex types, the same mutex is returned on every call that has |
| 6099 | ** the same type number. |
| 6100 | ** |
| 6101 | ** ^The sqlite3_mutex_free() routine deallocates a previously |
| 6102 | ** allocated dynamic mutex. Attempting to deallocate a static |
| 6103 | ** mutex results in undefined behavior. |
| 6104 | ** |
| 6105 | ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt |
| 6106 | ** to enter a mutex. ^If another thread is already within the mutex, |
| 6107 | ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return |
| 6108 | ** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK] |
| 6109 | ** upon successful entry. ^(Mutexes created using |
| 6110 | ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. |
| 6111 | ** In such cases, the |
| 6112 | ** mutex must be exited an equal number of times before another thread |
| 6113 | ** can enter.)^ If the same thread tries to enter any mutex other |
| 6114 | ** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined. |
| 6115 | ** |
| 6116 | ** ^(Some systems (for example, Windows 95) do not support the operation |
| 6117 | ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() |
| 6118 | ** will always return SQLITE_BUSY. The SQLite core only ever uses |
| 6119 | ** sqlite3_mutex_try() as an optimization so this is acceptable |
| 6120 | ** behavior.)^ |
| 6121 | ** |
| 6122 | ** ^The sqlite3_mutex_leave() routine exits a mutex that was |
| 6123 | ** previously entered by the same thread. The behavior |
| 6124 | ** is undefined if the mutex is not currently entered by the |
| 6125 | ** calling thread or is not currently allocated. |
| 6126 | ** |
| 6127 | ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or |
| 6128 | ** sqlite3_mutex_leave() is a NULL pointer, then all three routines |
| 6129 | ** behave as no-ops. |
| 6130 | ** |
| @@ -6111,13 +6141,13 @@ | |
| 6141 | ** |
| 6142 | ** An instance of this structure defines the low-level routines |
| 6143 | ** used to allocate and use mutexes. |
| 6144 | ** |
| 6145 | ** Usually, the default mutex implementations provided by SQLite are |
| 6146 | ** sufficient, however the application has the option of substituting a custom |
| 6147 | ** implementation for specialized deployments or systems for which SQLite |
| 6148 | ** does not provide a suitable implementation. In this case, the application |
| 6149 | ** creates and populates an instance of this structure to pass |
| 6150 | ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option. |
| 6151 | ** Additionally, an instance of this structure can be used as an |
| 6152 | ** output variable when querying the system for the current mutex |
| 6153 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| @@ -6154,17 +6184,17 @@ | |
| 6184 | ** by this structure are not required to handle this case, the results |
| 6185 | ** of passing a NULL pointer instead of a valid mutex handle are undefined |
| 6186 | ** (i.e. it is acceptable to provide an implementation that segfaults if |
| 6187 | ** it is passed a NULL pointer). |
| 6188 | ** |
| 6189 | ** The xMutexInit() method must be threadsafe. It must be harmless to |
| 6190 | ** invoke xMutexInit() multiple times within the same process and without |
| 6191 | ** intervening calls to xMutexEnd(). Second and subsequent calls to |
| 6192 | ** xMutexInit() must be no-ops. |
| 6193 | ** |
| 6194 | ** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] |
| 6195 | ** and its associates). Similarly, xMutexAlloc() must not use SQLite memory |
| 6196 | ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite |
| 6197 | ** memory allocation for a fast or recursive mutex. |
| 6198 | ** |
| 6199 | ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is |
| 6200 | ** called, but only if the prior call to xMutexInit returned SQLITE_OK. |
| @@ -6186,33 +6216,33 @@ | |
| 6216 | |
| 6217 | /* |
| 6218 | ** CAPI3REF: Mutex Verification Routines |
| 6219 | ** |
| 6220 | ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines |
| 6221 | ** are intended for use inside assert() statements. The SQLite core |
| 6222 | ** never uses these routines except inside an assert() and applications |
| 6223 | ** are advised to follow the lead of the core. The SQLite core only |
| 6224 | ** provides implementations for these routines when it is compiled |
| 6225 | ** with the SQLITE_DEBUG flag. External mutex implementations |
| 6226 | ** are only required to provide these routines if SQLITE_DEBUG is |
| 6227 | ** defined and if NDEBUG is not defined. |
| 6228 | ** |
| 6229 | ** These routines should return true if the mutex in their argument |
| 6230 | ** is held or not held, respectively, by the calling thread. |
| 6231 | ** |
| 6232 | ** The implementation is not required to provide versions of these |
| 6233 | ** routines that actually work. If the implementation does not provide working |
| 6234 | ** versions of these routines, it should at least provide stubs that always |
| 6235 | ** return true so that one does not get spurious assertion failures. |
| 6236 | ** |
| 6237 | ** If the argument to sqlite3_mutex_held() is a NULL pointer then |
| 6238 | ** the routine should return 1. This seems counter-intuitive since |
| 6239 | ** clearly the mutex cannot be held if it does not exist. But |
| 6240 | ** the reason the mutex does not exist is because the build is not |
| 6241 | ** using mutexes. And we do not want the assert() containing the |
| 6242 | ** call to sqlite3_mutex_held() to fail, so a non-zero return is |
| 6243 | ** the appropriate thing to do. The sqlite3_mutex_notheld() |
| 6244 | ** interface should also return 1 when given a NULL pointer. |
| 6245 | */ |
| 6246 | #ifndef NDEBUG |
| 6247 | SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*); |
| 6248 | SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); |
| @@ -6940,10 +6970,14 @@ | |
| 6970 | ** sqlite3_backup_init(D,N,S,M) identify the [database connection] |
| 6971 | ** and database name of the source database, respectively. |
| 6972 | ** ^The source and destination [database connections] (parameters S and D) |
| 6973 | ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with |
| 6974 | ** an error. |
| 6975 | ** |
| 6976 | ** ^A call to sqlite3_backup_init() will fail, returning SQLITE_ERROR, if |
| 6977 | ** there is already a read or read-write transaction open on the |
| 6978 | ** destination database. |
| 6979 | ** |
| 6980 | ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is |
| 6981 | ** returned and an error code and error message are stored in the |
| 6982 | ** destination [database connection] D. |
| 6983 | ** ^The error code and message for the failed call to sqlite3_backup_init() |
| @@ -7533,10 +7567,102 @@ | |
| 7567 | /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */ |
| 7568 | #define SQLITE_FAIL 3 |
| 7569 | /* #define SQLITE_ABORT 4 // Also an error code */ |
| 7570 | #define SQLITE_REPLACE 5 |
| 7571 | |
| 7572 | /* |
| 7573 | ** CAPI3REF: Prepared Statement Scan Status Opcodes |
| 7574 | ** KEYWORDS: {scanstatus options} |
| 7575 | ** |
| 7576 | ** The following constants can be used for the T parameter to the |
| 7577 | ** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a |
| 7578 | ** different metric for sqlite3_stmt_scanstatus() to return. |
| 7579 | ** |
| 7580 | ** <dl> |
| 7581 | ** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt> |
| 7582 | ** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the |
| 7583 | ** total number of times that the X-th loop has run.</dd> |
| 7584 | ** |
| 7585 | ** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt> |
| 7586 | ** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the |
| 7587 | ** total number of rows examined by all iterations of the X-th loop.</dd> |
| 7588 | ** |
| 7589 | ** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt> |
| 7590 | ** <dd>^The "double" variable pointed to by the T parameter will be set to the |
| 7591 | ** query planner's estimate for the average number of rows output from each |
| 7592 | ** iteration of the X-th loop. If the query planner's estimates was accurate, |
| 7593 | ** then this value will approximate the quotient NVISIT/NLOOP and the |
| 7594 | ** product of this value for all prior loops with the same SELECTID will |
| 7595 | ** be the NLOOP value for the current loop. |
| 7596 | ** |
| 7597 | ** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt> |
| 7598 | ** <dd>^The "const char *" variable pointed to by the T parameter will be set to |
| 7599 | ** a zero-terminated UTF-8 string containing the name of the index or table used |
| 7600 | ** for the X-th loop. |
| 7601 | ** |
| 7602 | ** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt> |
| 7603 | ** <dd>^The "const char *" variable pointed to by the T parameter will be set to |
| 7604 | ** a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] description |
| 7605 | ** for the X-th loop. |
| 7606 | ** |
| 7607 | ** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt> |
| 7608 | ** <dd>^The "int" variable pointed to by the T parameter will be set to the |
| 7609 | ** "select-id" for the X-th loop. The select-id identifies which query or |
| 7610 | ** subquery the loop is part of. The main query has a select-id of zero. |
| 7611 | ** The select-id is the same value as is output in the first column |
| 7612 | ** of an [EXPLAIN QUERY PLAN] query. |
| 7613 | ** </dl> |
| 7614 | */ |
| 7615 | #define SQLITE_SCANSTAT_NLOOP 0 |
| 7616 | #define SQLITE_SCANSTAT_NVISIT 1 |
| 7617 | #define SQLITE_SCANSTAT_EST 2 |
| 7618 | #define SQLITE_SCANSTAT_NAME 3 |
| 7619 | #define SQLITE_SCANSTAT_EXPLAIN 4 |
| 7620 | #define SQLITE_SCANSTAT_SELECTID 5 |
| 7621 | |
| 7622 | /* |
| 7623 | ** CAPI3REF: Prepared Statement Scan Status |
| 7624 | ** |
| 7625 | ** Return status data for a single loop within query pStmt. |
| 7626 | ** |
| 7627 | ** The "iScanStatusOp" parameter determines which status information to return. |
| 7628 | ** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior of |
| 7629 | ** this interface is undefined. |
| 7630 | ** ^The requested measurement is written into a variable pointed to by |
| 7631 | ** the "pOut" parameter. |
| 7632 | ** Parameter "idx" identifies the specific loop to retrieve statistics for. |
| 7633 | ** Loops are numbered starting from zero. ^If idx is out of range - less than |
| 7634 | ** zero or greater than or equal to the total number of loops used to implement |
| 7635 | ** the statement - a non-zero value is returned and the variable that pOut |
| 7636 | ** points to is unchanged. |
| 7637 | ** |
| 7638 | ** ^Statistics might not be available for all loops in all statements. ^In cases |
| 7639 | ** where there exist loops with no available statistics, this function behaves |
| 7640 | ** as if the loop did not exist - it returns non-zero and leave the variable |
| 7641 | ** that pOut points to unchanged. |
| 7642 | ** |
| 7643 | ** This API is only available if the library is built with pre-processor |
| 7644 | ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. |
| 7645 | ** |
| 7646 | ** See also: [sqlite3_stmt_scanstatus_reset()] |
| 7647 | */ |
| 7648 | SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus( |
| 7649 | sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ |
| 7650 | int idx, /* Index of loop to report on */ |
| 7651 | int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ |
| 7652 | void *pOut /* Result written here */ |
| 7653 | ); |
| 7654 | |
| 7655 | /* |
| 7656 | ** CAPI3REF: Zero Scan-Status Counters |
| 7657 | ** |
| 7658 | ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters. |
| 7659 | ** |
| 7660 | ** This API is only available if the library is built with pre-processor |
| 7661 | ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. |
| 7662 | */ |
| 7663 | SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); |
| 7664 | |
| 7665 | |
| 7666 | /* |
| 7667 | ** Undo the hack that converts floating point types to integer for |
| 7668 | ** builds on processors without floating point support. |
| @@ -7978,14 +8104,13 @@ | |
| 8104 | #ifndef SQLITE_POWERSAFE_OVERWRITE |
| 8105 | # define SQLITE_POWERSAFE_OVERWRITE 1 |
| 8106 | #endif |
| 8107 | |
| 8108 | /* |
| 8109 | ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by |
| 8110 | ** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in |
| 8111 | ** which case memory allocation statistics are disabled by default. |
| 8112 | */ |
| 8113 | #if !defined(SQLITE_DEFAULT_MEMSTATUS) |
| 8114 | # define SQLITE_DEFAULT_MEMSTATUS 1 |
| 8115 | #endif |
| 8116 | |
| @@ -8611,11 +8736,11 @@ | |
| 8736 | ** Estimated quantities used for query planning are stored as 16-bit |
| 8737 | ** logarithms. For quantity X, the value stored is 10*log2(X). This |
| 8738 | ** gives a possible range of values of approximately 1.0e986 to 1e-986. |
| 8739 | ** But the allowed values are "grainy". Not every value is representable. |
| 8740 | ** For example, quantities 16 and 17 are both represented by a LogEst |
| 8741 | ** of 40. However, since LogEst quantities are suppose to be estimates, |
| 8742 | ** not exact values, this imprecision is not a problem. |
| 8743 | ** |
| 8744 | ** "LogEst" is short for "Logarithmic Estimate". |
| 8745 | ** |
| 8746 | ** Examples: |
| @@ -9124,10 +9249,11 @@ | |
| 9249 | SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *); |
| 9250 | SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *); |
| 9251 | SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); |
| 9252 | SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask); |
| 9253 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt); |
| 9254 | SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void); |
| 9255 | |
| 9256 | #ifndef NDEBUG |
| 9257 | SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*); |
| 9258 | #endif |
| 9259 | |
| @@ -9666,10 +9792,16 @@ | |
| 9792 | # define VdbeCoverageAlwaysTaken(v) |
| 9793 | # define VdbeCoverageNeverTaken(v) |
| 9794 | # define VDBE_OFFSET_LINENO(x) 0 |
| 9795 | #endif |
| 9796 | |
| 9797 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 9798 | SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*); |
| 9799 | #else |
| 9800 | # define sqlite3VdbeScanStatus(a,b,c,d,e) |
| 9801 | #endif |
| 9802 | |
| 9803 | #endif |
| 9804 | |
| 9805 | /************** End of vdbe.h ************************************************/ |
| 9806 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 9807 | /************** Include pager.h in the middle of sqliteInt.h *****************/ |
| @@ -9862,10 +9994,12 @@ | |
| 9994 | SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *); |
| 9995 | |
| 9996 | /* Functions used to truncate the database file. */ |
| 9997 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); |
| 9998 | |
| 9999 | SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16); |
| 10000 | |
| 10001 | #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL) |
| 10002 | SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *); |
| 10003 | #endif |
| 10004 | |
| 10005 | /* Functions to support testing and debugging. */ |
| @@ -10049,10 +10183,14 @@ | |
| 10183 | SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*); |
| 10184 | #endif |
| 10185 | |
| 10186 | SQLITE_PRIVATE void sqlite3PCacheSetDefault(void); |
| 10187 | |
| 10188 | /* Return the header size */ |
| 10189 | SQLITE_PRIVATE int sqlite3HeaderSizePcache(void); |
| 10190 | SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void); |
| 10191 | |
| 10192 | #endif /* _PCACHE_H_ */ |
| 10193 | |
| 10194 | /************** End of pcache.h **********************************************/ |
| 10195 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 10196 | |
| @@ -10735,11 +10873,11 @@ | |
| 10873 | #define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */ |
| 10874 | #define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */ |
| 10875 | #define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */ |
| 10876 | #define SQLITE_Transitive 0x0200 /* Transitive constraints */ |
| 10877 | #define SQLITE_OmitNoopJoin 0x0400 /* Omit unused tables in joins */ |
| 10878 | #define SQLITE_Stat34 0x0800 /* Use STAT3 or STAT4 data */ |
| 10879 | #define SQLITE_AllOpts 0xffff /* All optimizations */ |
| 10880 | |
| 10881 | /* |
| 10882 | ** Macros for testing whether or not optimizations are enabled or disabled. |
| 10883 | */ |
| @@ -11317,16 +11455,18 @@ | |
| 11455 | unsigned idxType:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */ |
| 11456 | unsigned bUnordered:1; /* Use this index for == or IN queries only */ |
| 11457 | unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */ |
| 11458 | unsigned isResized:1; /* True if resizeIndexObject() has been called */ |
| 11459 | unsigned isCovering:1; /* True if this is a covering index */ |
| 11460 | unsigned noSkipScan:1; /* Do not try to use skip-scan if true */ |
| 11461 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 11462 | int nSample; /* Number of elements in aSample[] */ |
| 11463 | int nSampleCol; /* Size of IndexSample.anEq[] and so on */ |
| 11464 | tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */ |
| 11465 | IndexSample *aSample; /* Samples of the left-most key */ |
| 11466 | tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */ |
| 11467 | tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */ |
| 11468 | #endif |
| 11469 | }; |
| 11470 | |
| 11471 | /* |
| 11472 | ** Allowed values for Index.idxType |
| @@ -11520,11 +11660,11 @@ | |
| 11660 | int nHeight; /* Height of the tree headed by this node */ |
| 11661 | #endif |
| 11662 | int iTable; /* TK_COLUMN: cursor number of table holding column |
| 11663 | ** TK_REGISTER: register number |
| 11664 | ** TK_TRIGGER: 1 -> new, 0 -> old |
| 11665 | ** EP_Unlikely: 134217728 times likelihood */ |
| 11666 | ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid. |
| 11667 | ** TK_VARIABLE: variable number (always >= 1). */ |
| 11668 | i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */ |
| 11669 | i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */ |
| 11670 | u8 op2; /* TK_REGISTER: original value of Expr.op |
| @@ -12412,13 +12552,15 @@ | |
| 12552 | int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */ |
| 12553 | int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */ |
| 12554 | void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */ |
| 12555 | Parse *pParse; /* Parser context. */ |
| 12556 | int walkerDepth; /* Number of subqueries */ |
| 12557 | u8 eCode; /* A small processing code */ |
| 12558 | union { /* Extra data for callback */ |
| 12559 | NameContext *pNC; /* Naming context */ |
| 12560 | int n; /* A counter */ |
| 12561 | int iCur; /* A cursor number */ |
| 12562 | SrcList *pSrcList; /* FROM clause */ |
| 12563 | struct SrcCount *pSrcCount; /* Counting column references */ |
| 12564 | } u; |
| 12565 | }; |
| 12566 | |
| @@ -12815,10 +12957,11 @@ | |
| 12957 | SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *); |
| 12958 | SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*); |
| 12959 | SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*); |
| 12960 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*); |
| 12961 | SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8); |
| 12962 | SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int); |
| 12963 | SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*); |
| 12964 | SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*); |
| 12965 | SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); |
| 12966 | SQLITE_PRIVATE int sqlite3IsRowid(const char*); |
| 12967 | SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8); |
| @@ -13472,15 +13615,23 @@ | |
| 13615 | ** compatibility for legacy applications, the URI filename capability is |
| 13616 | ** disabled by default. |
| 13617 | ** |
| 13618 | ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled |
| 13619 | ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options. |
| 13620 | ** |
| 13621 | ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally |
| 13622 | ** disabled. The default value may be changed by compiling with the |
| 13623 | ** SQLITE_USE_URI symbol defined. |
| 13624 | */ |
| 13625 | #ifndef SQLITE_USE_URI |
| 13626 | # define SQLITE_USE_URI 0 |
| 13627 | #endif |
| 13628 | |
| 13629 | /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the |
| 13630 | ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if |
| 13631 | ** that compile-time option is omitted. |
| 13632 | */ |
| 13633 | #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN |
| 13634 | # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1 |
| 13635 | #endif |
| 13636 | |
| 13637 | /* |
| @@ -13566,12 +13717,12 @@ | |
| 13717 | ** than 1 GiB. The sqlite3_test_control() interface can be used to |
| 13718 | ** move the pending byte. |
| 13719 | ** |
| 13720 | ** IMPORTANT: Changing the pending byte to any value other than |
| 13721 | ** 0x40000000 results in an incompatible database file format! |
| 13722 | ** Changing the pending byte during operation will result in undefined |
| 13723 | ** and incorrect behavior. |
| 13724 | */ |
| 13725 | #ifndef SQLITE_OMIT_WSD |
| 13726 | SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000; |
| 13727 | #endif |
| 13728 | |
| @@ -13646,10 +13797,13 @@ | |
| 13797 | #ifdef SQLITE_DISABLE_DIRSYNC |
| 13798 | "DISABLE_DIRSYNC", |
| 13799 | #endif |
| 13800 | #ifdef SQLITE_DISABLE_LFS |
| 13801 | "DISABLE_LFS", |
| 13802 | #endif |
| 13803 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 13804 | "ENABLE_API_ARMOR", |
| 13805 | #endif |
| 13806 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 13807 | "ENABLE_ATOMIC_WRITE", |
| 13808 | #endif |
| 13809 | #ifdef SQLITE_ENABLE_CEROD |
| @@ -13972,10 +14126,17 @@ | |
| 14126 | ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix |
| 14127 | ** is not required for a match. |
| 14128 | */ |
| 14129 | SQLITE_API int sqlite3_compileoption_used(const char *zOptName){ |
| 14130 | int i, n; |
| 14131 | |
| 14132 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 14133 | if( zOptName==0 ){ |
| 14134 | (void)SQLITE_MISUSE_BKPT; |
| 14135 | return 0; |
| 14136 | } |
| 14137 | #endif |
| 14138 | if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7; |
| 14139 | n = sqlite3Strlen30(zOptName); |
| 14140 | |
| 14141 | /* Since ArraySize(azCompileOpt) is normally in single digits, a |
| 14142 | ** linear search is adequate. No need for a binary search. */ |
| @@ -14153,10 +14314,11 @@ | |
| 14314 | typedef struct VdbeFrame VdbeFrame; |
| 14315 | struct VdbeFrame { |
| 14316 | Vdbe *v; /* VM this frame belongs to */ |
| 14317 | VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */ |
| 14318 | Op *aOp; /* Program instructions for parent frame */ |
| 14319 | i64 *anExec; /* Event counters from parent frame */ |
| 14320 | Mem *aMem; /* Array of memory cells for parent frame */ |
| 14321 | u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */ |
| 14322 | VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */ |
| 14323 | void *token; /* Copy of SubProgram.token */ |
| 14324 | i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */ |
| @@ -14165,11 +14327,12 @@ | |
| 14327 | int nOp; /* Size of aOp array */ |
| 14328 | int nMem; /* Number of entries in aMem */ |
| 14329 | int nOnceFlag; /* Number of entries in aOnceFlag */ |
| 14330 | int nChildMem; /* Number of memory cells for child frame */ |
| 14331 | int nChildCsr; /* Number of cursors for child frame */ |
| 14332 | int nChange; /* Statement changes (Vdbe.nChange) */ |
| 14333 | int nDbChange; /* Value of db->nChange */ |
| 14334 | }; |
| 14335 | |
| 14336 | #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) |
| 14337 | |
| 14338 | /* |
| @@ -14316,10 +14479,20 @@ | |
| 14479 | /* A bitfield type for use inside of structures. Always follow with :N where |
| 14480 | ** N is the number of bits. |
| 14481 | */ |
| 14482 | typedef unsigned bft; /* Bit Field Type */ |
| 14483 | |
| 14484 | typedef struct ScanStatus ScanStatus; |
| 14485 | struct ScanStatus { |
| 14486 | int addrExplain; /* OP_Explain for loop */ |
| 14487 | int addrLoop; /* Address of "loops" counter */ |
| 14488 | int addrVisit; /* Address of "rows visited" counter */ |
| 14489 | int iSelectID; /* The "Select-ID" for this loop */ |
| 14490 | LogEst nEst; /* Estimated output rows per loop */ |
| 14491 | char *zName; /* Name of table or index */ |
| 14492 | }; |
| 14493 | |
| 14494 | /* |
| 14495 | ** An instance of the virtual machine. This structure contains the complete |
| 14496 | ** state of the virtual machine. |
| 14497 | ** |
| 14498 | ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare() |
| @@ -14388,10 +14561,15 @@ | |
| 14561 | u32 expmask; /* Binding to these vars invalidates VM */ |
| 14562 | SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ |
| 14563 | int nOnceFlag; /* Size of array aOnceFlag[] */ |
| 14564 | u8 *aOnceFlag; /* Flags for OP_Once */ |
| 14565 | AuxData *pAuxData; /* Linked list of auxdata allocations */ |
| 14566 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 14567 | i64 *anExec; /* Number of times each op has been executed */ |
| 14568 | int nScan; /* Entries in aScan[] */ |
| 14569 | ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */ |
| 14570 | #endif |
| 14571 | }; |
| 14572 | |
| 14573 | /* |
| 14574 | ** The following are allowed values for Vdbe.magic |
| 14575 | */ |
| @@ -14577,10 +14755,13 @@ | |
| 14755 | SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ |
| 14756 | wsdStatInit; |
| 14757 | if( op<0 || op>=ArraySize(wsdStat.nowValue) ){ |
| 14758 | return SQLITE_MISUSE_BKPT; |
| 14759 | } |
| 14760 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 14761 | if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; |
| 14762 | #endif |
| 14763 | *pCurrent = wsdStat.nowValue[op]; |
| 14764 | *pHighwater = wsdStat.mxValue[op]; |
| 14765 | if( resetFlag ){ |
| 14766 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
| 14767 | } |
| @@ -14596,10 +14777,15 @@ | |
| 14777 | int *pCurrent, /* Write current value here */ |
| 14778 | int *pHighwater, /* Write high-water mark here */ |
| 14779 | int resetFlag /* Reset high-water mark if true */ |
| 14780 | ){ |
| 14781 | int rc = SQLITE_OK; /* Return code */ |
| 14782 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 14783 | if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){ |
| 14784 | return SQLITE_MISUSE_BKPT; |
| 14785 | } |
| 14786 | #endif |
| 14787 | sqlite3_mutex_enter(db->mutex); |
| 14788 | switch( op ){ |
| 14789 | case SQLITE_DBSTATUS_LOOKASIDE_USED: { |
| 14790 | *pCurrent = db->lookaside.nOut; |
| 14791 | *pHighwater = db->lookaside.mxOut; |
| @@ -14774,11 +14960,11 @@ | |
| 14960 | ** |
| 14961 | ** There is only one exported symbol in this file - the function |
| 14962 | ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. |
| 14963 | ** All other code has file scope. |
| 14964 | ** |
| 14965 | ** SQLite processes all times and dates as julian day numbers. The |
| 14966 | ** dates and times are stored as the number of days since noon |
| 14967 | ** in Greenwich on November 24, 4714 B.C. according to the Gregorian |
| 14968 | ** calendar system. |
| 14969 | ** |
| 14970 | ** 1970-01-01 00:00:00 is JD 2440587.5 |
| @@ -14789,11 +14975,11 @@ | |
| 14975 | ** be represented, even though julian day numbers allow a much wider |
| 14976 | ** range of dates. |
| 14977 | ** |
| 14978 | ** The Gregorian calendar system is used for all dates and times, |
| 14979 | ** even those that predate the Gregorian calendar. Historians usually |
| 14980 | ** use the julian calendar for dates prior to 1582-10-15 and for some |
| 14981 | ** dates afterwards, depending on locale. Beware of this difference. |
| 14982 | ** |
| 14983 | ** The conversion algorithms are implemented based on descriptions |
| 14984 | ** in the following text: |
| 14985 | ** |
| @@ -15061,11 +15247,11 @@ | |
| 15247 | return 1; |
| 15248 | } |
| 15249 | } |
| 15250 | |
| 15251 | /* |
| 15252 | ** Attempt to parse the given string into a julian day number. Return |
| 15253 | ** the number of errors. |
| 15254 | ** |
| 15255 | ** The following are acceptable forms for the input string: |
| 15256 | ** |
| 15257 | ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM |
| @@ -15632,11 +15818,11 @@ | |
| 15818 | ** |
| 15819 | ** %d day of month |
| 15820 | ** %f ** fractional seconds SS.SSS |
| 15821 | ** %H hour 00-24 |
| 15822 | ** %j day of year 000-366 |
| 15823 | ** %J ** julian day number |
| 15824 | ** %m month 01-12 |
| 15825 | ** %M minute 00-59 |
| 15826 | ** %s seconds since 1970-01-01 |
| 15827 | ** %S seconds 00-59 |
| 15828 | ** %w day of week 0-6 sunday==0 |
| @@ -16257,10 +16443,14 @@ | |
| 16443 | MUTEX_LOGIC(sqlite3_mutex *mutex;) |
| 16444 | #ifndef SQLITE_OMIT_AUTOINIT |
| 16445 | int rc = sqlite3_initialize(); |
| 16446 | if( rc ) return rc; |
| 16447 | #endif |
| 16448 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 16449 | if( pVfs==0 ) return SQLITE_MISUSE_BKPT; |
| 16450 | #endif |
| 16451 | |
| 16452 | MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 16453 | sqlite3_mutex_enter(mutex); |
| 16454 | vfsUnlink(pVfs); |
| 16455 | if( makeDflt || vfsList==0 ){ |
| 16456 | pVfs->pNext = vfsList; |
| @@ -18614,10 +18804,11 @@ | |
| 18804 | ** Retrieve a pointer to a static mutex or allocate a new dynamic one. |
| 18805 | */ |
| 18806 | SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){ |
| 18807 | #ifndef SQLITE_OMIT_AUTOINIT |
| 18808 | if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0; |
| 18809 | if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0; |
| 18810 | #endif |
| 18811 | return sqlite3GlobalConfig.mutex.xMutexAlloc(id); |
| 18812 | } |
| 18813 | |
| 18814 | SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){ |
| @@ -19070,12 +19261,16 @@ | |
| 19261 | pthread_mutex_init(&p->mutex, 0); |
| 19262 | } |
| 19263 | break; |
| 19264 | } |
| 19265 | default: { |
| 19266 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 19267 | if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ |
| 19268 | (void)SQLITE_MISUSE_BKPT; |
| 19269 | return 0; |
| 19270 | } |
| 19271 | #endif |
| 19272 | p = &staticMutexes[iType-2]; |
| 19273 | #if SQLITE_MUTEX_NREF |
| 19274 | p->id = iType; |
| 19275 | #endif |
| 19276 | break; |
| @@ -20293,15 +20488,16 @@ | |
| 20488 | } |
| 20489 | assert( sqlite3_mutex_notheld(mem0.mutex) ); |
| 20490 | |
| 20491 | |
| 20492 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 20493 | /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch |
| 20494 | ** buffers per thread. |
| 20495 | ** |
| 20496 | ** This can only be checked in single-threaded mode. |
| 20497 | */ |
| 20498 | assert( scratchAllocOut==0 ); |
| 20499 | if( p ) scratchAllocOut++; |
| 20500 | #endif |
| 20501 | |
| 20502 | return p; |
| 20503 | } |
| @@ -20956,10 +21152,17 @@ | |
| 21152 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
| 21153 | #endif |
| 21154 | PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ |
| 21155 | char buf[etBUFSIZE]; /* Conversion buffer */ |
| 21156 | |
| 21157 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 21158 | if( ap==0 ){ |
| 21159 | (void)SQLITE_MISUSE_BKPT; |
| 21160 | sqlite3StrAccumReset(pAccum); |
| 21161 | return; |
| 21162 | } |
| 21163 | #endif |
| 21164 | bufpt = 0; |
| 21165 | if( bFlags ){ |
| 21166 | if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ |
| 21167 | pArgList = va_arg(ap, PrintfArguments*); |
| 21168 | } |
| @@ -21496,10 +21699,15 @@ | |
| 21699 | return N; |
| 21700 | }else{ |
| 21701 | char *zOld = (p->zText==p->zBase ? 0 : p->zText); |
| 21702 | i64 szNew = p->nChar; |
| 21703 | szNew += N + 1; |
| 21704 | if( szNew+p->nChar<=p->mxAlloc ){ |
| 21705 | /* Force exponential buffer size growth as long as it does not overflow, |
| 21706 | ** to avoid having to call this routine too often */ |
| 21707 | szNew += p->nChar; |
| 21708 | } |
| 21709 | if( szNew > p->mxAlloc ){ |
| 21710 | sqlite3StrAccumReset(p); |
| 21711 | setStrAccumError(p, STRACCUM_TOOBIG); |
| 21712 | return 0; |
| 21713 | }else{ |
| @@ -21512,10 +21720,11 @@ | |
| 21720 | } |
| 21721 | if( zNew ){ |
| 21722 | assert( p->zText!=0 || p->nChar==0 ); |
| 21723 | if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 21724 | p->zText = zNew; |
| 21725 | p->nAlloc = sqlite3DbMallocSize(p->db, zNew); |
| 21726 | }else{ |
| 21727 | sqlite3StrAccumReset(p); |
| 21728 | setStrAccumError(p, STRACCUM_NOMEM); |
| 21729 | return 0; |
| 21730 | } |
| @@ -21681,10 +21890,17 @@ | |
| 21890 | */ |
| 21891 | SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){ |
| 21892 | char *z; |
| 21893 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
| 21894 | StrAccum acc; |
| 21895 | |
| 21896 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 21897 | if( zFormat==0 ){ |
| 21898 | (void)SQLITE_MISUSE_BKPT; |
| 21899 | return 0; |
| 21900 | } |
| 21901 | #endif |
| 21902 | #ifndef SQLITE_OMIT_AUTOINIT |
| 21903 | if( sqlite3_initialize() ) return 0; |
| 21904 | #endif |
| 21905 | sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
| 21906 | acc.useMalloc = 2; |
| @@ -21723,10 +21939,17 @@ | |
| 21939 | ** sqlite3_vsnprintf() is the varargs version. |
| 21940 | */ |
| 21941 | SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ |
| 21942 | StrAccum acc; |
| 21943 | if( n<=0 ) return zBuf; |
| 21944 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 21945 | if( zBuf==0 || zFormat==0 ) { |
| 21946 | (void)SQLITE_MISUSE_BKPT; |
| 21947 | if( zBuf && n>0 ) zBuf[0] = 0; |
| 21948 | return zBuf; |
| 21949 | } |
| 21950 | #endif |
| 21951 | sqlite3StrAccumInit(&acc, zBuf, n, 0); |
| 21952 | acc.useMalloc = 0; |
| 21953 | sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 21954 | return sqlite3StrAccumFinish(&acc); |
| 21955 | } |
| @@ -21914,15 +22137,23 @@ | |
| 22137 | #else |
| 22138 | # define wsdPrng sqlite3Prng |
| 22139 | #endif |
| 22140 | |
| 22141 | #if SQLITE_THREADSAFE |
| 22142 | sqlite3_mutex *mutex; |
| 22143 | #endif |
| 22144 | |
| 22145 | #ifndef SQLITE_OMIT_AUTOINIT |
| 22146 | if( sqlite3_initialize() ) return; |
| 22147 | #endif |
| 22148 | |
| 22149 | #if SQLITE_THREADSAFE |
| 22150 | mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); |
| 22151 | #endif |
| 22152 | |
| 22153 | sqlite3_mutex_enter(mutex); |
| 22154 | if( N<=0 || pBuf==0 ){ |
| 22155 | wsdPrng.isInit = 0; |
| 22156 | sqlite3_mutex_leave(mutex); |
| 22157 | return; |
| 22158 | } |
| 22159 | |
| @@ -23040,17 +23271,27 @@ | |
| 23271 | ** case-independent fashion, using the same definition of "case |
| 23272 | ** independence" that SQLite uses internally when comparing identifiers. |
| 23273 | */ |
| 23274 | SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){ |
| 23275 | register unsigned char *a, *b; |
| 23276 | if( zLeft==0 ){ |
| 23277 | return zRight ? -1 : 0; |
| 23278 | }else if( zRight==0 ){ |
| 23279 | return 1; |
| 23280 | } |
| 23281 | a = (unsigned char *)zLeft; |
| 23282 | b = (unsigned char *)zRight; |
| 23283 | while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 23284 | return UpperToLower[*a] - UpperToLower[*b]; |
| 23285 | } |
| 23286 | SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ |
| 23287 | register unsigned char *a, *b; |
| 23288 | if( zLeft==0 ){ |
| 23289 | return zRight ? -1 : 0; |
| 23290 | }else if( zRight==0 ){ |
| 23291 | return 1; |
| 23292 | } |
| 23293 | a = (unsigned char *)zLeft; |
| 23294 | b = (unsigned char *)zRight; |
| 23295 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 23296 | return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
| 23297 | } |
| @@ -32579,10 +32820,15 @@ | |
| 32820 | #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL) |
| 32821 | # error "WAL mode requires support from the Windows NT kernel, compile\ |
| 32822 | with SQLITE_OMIT_WAL." |
| 32823 | #endif |
| 32824 | |
| 32825 | #if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0 |
| 32826 | # error "Memory mapped files require support from the Windows NT kernel,\ |
| 32827 | compile with SQLITE_MAX_MMAP_SIZE=0." |
| 32828 | #endif |
| 32829 | |
| 32830 | /* |
| 32831 | ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions |
| 32832 | ** based on the sub-platform)? |
| 32833 | */ |
| 32834 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI) |
| @@ -32708,14 +32954,15 @@ | |
| 32954 | # define winGetDirSep() '\\' |
| 32955 | #endif |
| 32956 | |
| 32957 | /* |
| 32958 | ** Do we need to manually define the Win32 file mapping APIs for use with WAL |
| 32959 | ** mode or memory mapped files (e.g. these APIs are available in the Windows |
| 32960 | ** CE SDK; however, they are not present in the header file)? |
| 32961 | */ |
| 32962 | #if SQLITE_WIN32_FILEMAPPING_API && \ |
| 32963 | (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
| 32964 | /* |
| 32965 | ** Two of the file mapping APIs are different under WinRT. Figure out which |
| 32966 | ** set we need. |
| 32967 | */ |
| 32968 | #if SQLITE_OS_WINRT |
| @@ -32739,11 +32986,11 @@ | |
| 32986 | |
| 32987 | /* |
| 32988 | ** This file mapping API is common to both Win32 and WinRT. |
| 32989 | */ |
| 32990 | WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID); |
| 32991 | #endif /* SQLITE_WIN32_FILEMAPPING_API */ |
| 32992 | |
| 32993 | /* |
| 32994 | ** Some Microsoft compilers lack this definition. |
| 32995 | */ |
| 32996 | #ifndef INVALID_FILE_ATTRIBUTES |
| @@ -33032,21 +33279,21 @@ | |
| 33279 | |
| 33280 | #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \ |
| 33281 | LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent) |
| 33282 | |
| 33283 | #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \ |
| 33284 | (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
| 33285 | { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 }, |
| 33286 | #else |
| 33287 | { "CreateFileMappingA", (SYSCALL)0, 0 }, |
| 33288 | #endif |
| 33289 | |
| 33290 | #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \ |
| 33291 | DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent) |
| 33292 | |
| 33293 | #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ |
| 33294 | (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
| 33295 | { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 }, |
| 33296 | #else |
| 33297 | { "CreateFileMappingW", (SYSCALL)0, 0 }, |
| 33298 | #endif |
| 33299 | |
| @@ -33382,11 +33629,12 @@ | |
| 33629 | #ifndef osLockFileEx |
| 33630 | #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \ |
| 33631 | LPOVERLAPPED))aSyscall[48].pCurrent) |
| 33632 | #endif |
| 33633 | |
| 33634 | #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \ |
| 33635 | (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
| 33636 | { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 }, |
| 33637 | #else |
| 33638 | { "MapViewOfFile", (SYSCALL)0, 0 }, |
| 33639 | #endif |
| 33640 | |
| @@ -33452,11 +33700,11 @@ | |
| 33700 | #endif |
| 33701 | |
| 33702 | #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
| 33703 | LPOVERLAPPED))aSyscall[58].pCurrent) |
| 33704 | |
| 33705 | #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
| 33706 | { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 }, |
| 33707 | #else |
| 33708 | { "UnmapViewOfFile", (SYSCALL)0, 0 }, |
| 33709 | #endif |
| 33710 | |
| @@ -33515,11 +33763,11 @@ | |
| 33763 | #endif |
| 33764 | |
| 33765 | #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \ |
| 33766 | FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent) |
| 33767 | |
| 33768 | #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
| 33769 | { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 }, |
| 33770 | #else |
| 33771 | { "MapViewOfFileFromApp", (SYSCALL)0, 0 }, |
| 33772 | #endif |
| 33773 | |
| @@ -33579,11 +33827,11 @@ | |
| 33827 | |
| 33828 | { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 }, |
| 33829 | |
| 33830 | #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent) |
| 33831 | |
| 33832 | #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
| 33833 | { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 }, |
| 33834 | #else |
| 33835 | { "CreateFileMappingFromApp", (SYSCALL)0, 0 }, |
| 33836 | #endif |
| 33837 | |
| @@ -39155,10 +39403,17 @@ | |
| 39403 | */ |
| 39404 | SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){ |
| 39405 | assert( pCache->pCache!=0 ); |
| 39406 | sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache); |
| 39407 | } |
| 39408 | |
| 39409 | /* |
| 39410 | ** Return the size of the header added by this middleware layer |
| 39411 | ** in the page-cache hierarchy. |
| 39412 | */ |
| 39413 | SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return sizeof(PgHdr); } |
| 39414 | |
| 39415 | |
| 39416 | #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) |
| 39417 | /* |
| 39418 | ** For all dirty pages currently in the cache, invoke the specified |
| 39419 | ** callback. This is only used if the SQLITE_CHECK_PAGES macro is |
| @@ -40154,10 +40409,15 @@ | |
| 40409 | pcache1Shrink /* xShrink */ |
| 40410 | }; |
| 40411 | sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods); |
| 40412 | } |
| 40413 | |
| 40414 | /* |
| 40415 | ** Return the size of the header on each page of this PCACHE implementation. |
| 40416 | */ |
| 40417 | SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return sizeof(PgHdr1); } |
| 40418 | |
| 40419 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 40420 | /* |
| 40421 | ** This function is called to free superfluous dynamically allocated memory |
| 40422 | ** held by the pager system. Memory in use by any SQLite pager allocated |
| 40423 | ** by the current thread may be sqlite3_free()ed. |
| @@ -43763,11 +44023,11 @@ | |
| 44023 | ** should be page numbers which are never 0xffffffff. So filling |
| 44024 | ** pPager->dbFileVers[] with all 0xff bytes should suffice. |
| 44025 | ** |
| 44026 | ** For an encrypted database, the situation is more complex: bytes |
| 44027 | ** 24..39 of the database are white noise. But the probability of |
| 44028 | ** white noise equaling 16 bytes of 0xff is vanishingly small so |
| 44029 | ** we should still be ok. |
| 44030 | */ |
| 44031 | memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers)); |
| 44032 | }else{ |
| 44033 | u8 *dbFileVers = &((u8*)pPg->pData)[24]; |
| @@ -47710,10 +47970,22 @@ | |
| 47970 | } |
| 47971 | |
| 47972 | return SQLITE_OK; |
| 47973 | } |
| 47974 | #endif |
| 47975 | |
| 47976 | /* |
| 47977 | ** The page handle passed as the first argument refers to a dirty page |
| 47978 | ** with a page number other than iNew. This function changes the page's |
| 47979 | ** page number to iNew and sets the value of the PgHdr.flags field to |
| 47980 | ** the value passed as the third parameter. |
| 47981 | */ |
| 47982 | SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){ |
| 47983 | assert( pPg->pgno!=iNew ); |
| 47984 | pPg->flags = flags; |
| 47985 | sqlite3PcacheMove(pPg, iNew); |
| 47986 | } |
| 47987 | |
| 47988 | /* |
| 47989 | ** Return a pointer to the data for the specified page. |
| 47990 | */ |
| 47991 | SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){ |
| @@ -48108,10 +48380,11 @@ | |
| 48380 | SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){ |
| 48381 | assert( pPager->eState>=PAGER_READER ); |
| 48382 | return sqlite3WalFramesize(pPager->pWal); |
| 48383 | } |
| 48384 | #endif |
| 48385 | |
| 48386 | |
| 48387 | #endif /* SQLITE_OMIT_DISKIO */ |
| 48388 | |
| 48389 | /************** End of pager.c ***********************************************/ |
| 48390 | /************** Begin file wal.c *********************************************/ |
| @@ -49618,11 +49891,11 @@ | |
| 49891 | |
| 49892 | /* |
| 49893 | ** Free an iterator allocated by walIteratorInit(). |
| 49894 | */ |
| 49895 | static void walIteratorFree(WalIterator *p){ |
| 49896 | sqlite3_free(p); |
| 49897 | } |
| 49898 | |
| 49899 | /* |
| 49900 | ** Construct a WalInterator object that can be used to loop over all |
| 49901 | ** pages in the WAL in ascending order. The caller must hold the checkpoint |
| @@ -49653,21 +49926,21 @@ | |
| 49926 | /* Allocate space for the WalIterator object. */ |
| 49927 | nSegment = walFramePage(iLast) + 1; |
| 49928 | nByte = sizeof(WalIterator) |
| 49929 | + (nSegment-1)*sizeof(struct WalSegment) |
| 49930 | + iLast*sizeof(ht_slot); |
| 49931 | p = (WalIterator *)sqlite3_malloc(nByte); |
| 49932 | if( !p ){ |
| 49933 | return SQLITE_NOMEM; |
| 49934 | } |
| 49935 | memset(p, 0, nByte); |
| 49936 | p->nSegment = nSegment; |
| 49937 | |
| 49938 | /* Allocate temporary space used by the merge-sort routine. This block |
| 49939 | ** of memory will be freed before this function returns. |
| 49940 | */ |
| 49941 | aTmp = (ht_slot *)sqlite3_malloc( |
| 49942 | sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast) |
| 49943 | ); |
| 49944 | if( !aTmp ){ |
| 49945 | rc = SQLITE_NOMEM; |
| 49946 | } |
| @@ -49700,11 +49973,11 @@ | |
| 49973 | p->aSegment[i].nEntry = nEntry; |
| 49974 | p->aSegment[i].aIndex = aIndex; |
| 49975 | p->aSegment[i].aPgno = (u32 *)aPgno; |
| 49976 | } |
| 49977 | } |
| 49978 | sqlite3_free(aTmp); |
| 49979 | |
| 49980 | if( rc!=SQLITE_OK ){ |
| 49981 | walIteratorFree(p); |
| 49982 | } |
| 49983 | *pp = p; |
| @@ -53331,10 +53604,15 @@ | |
| 53604 | /* |
| 53605 | ** Defragment the page given. All Cells are moved to the |
| 53606 | ** end of the page and all free space is collected into one |
| 53607 | ** big FreeBlk that occurs in between the header and cell |
| 53608 | ** pointer array and the cell content area. |
| 53609 | ** |
| 53610 | ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a |
| 53611 | ** b-tree page so that there are no freeblocks or fragment bytes, all |
| 53612 | ** unused bytes are contained in the unallocated space region, and all |
| 53613 | ** cells are packed tightly at the end of the page. |
| 53614 | */ |
| 53615 | static int defragmentPage(MemPage *pPage){ |
| 53616 | int i; /* Loop counter */ |
| 53617 | int pc; /* Address of the i-th cell */ |
| 53618 | int hdr; /* Offset to the page header */ |
| @@ -53343,28 +53621,27 @@ | |
| 53621 | int cellOffset; /* Offset to the cell pointer array */ |
| 53622 | int cbrk; /* Offset to the cell content area */ |
| 53623 | int nCell; /* Number of cells on the page */ |
| 53624 | unsigned char *data; /* The page data */ |
| 53625 | unsigned char *temp; /* Temp area for cell content */ |
| 53626 | unsigned char *src; /* Source of content */ |
| 53627 | int iCellFirst; /* First allowable cell index */ |
| 53628 | int iCellLast; /* Last possible cell index */ |
| 53629 | |
| 53630 | |
| 53631 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53632 | assert( pPage->pBt!=0 ); |
| 53633 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
| 53634 | assert( pPage->nOverflow==0 ); |
| 53635 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53636 | temp = 0; |
| 53637 | src = data = pPage->aData; |
| 53638 | hdr = pPage->hdrOffset; |
| 53639 | cellOffset = pPage->cellOffset; |
| 53640 | nCell = pPage->nCell; |
| 53641 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 53642 | usableSize = pPage->pBt->usableSize; |
| 53643 | cbrk = usableSize; |
| 53644 | iCellFirst = cellOffset + 2*nCell; |
| 53645 | iCellLast = usableSize - 4; |
| 53646 | for(i=0; i<nCell; i++){ |
| 53647 | u8 *pAddr; /* The i-th cell pointer */ |
| @@ -53379,11 +53656,11 @@ | |
| 53656 | if( pc<iCellFirst || pc>iCellLast ){ |
| 53657 | return SQLITE_CORRUPT_BKPT; |
| 53658 | } |
| 53659 | #endif |
| 53660 | assert( pc>=iCellFirst && pc<=iCellLast ); |
| 53661 | size = cellSizePtr(pPage, &src[pc]); |
| 53662 | cbrk -= size; |
| 53663 | #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 53664 | if( cbrk<iCellFirst ){ |
| 53665 | return SQLITE_CORRUPT_BKPT; |
| 53666 | } |
| @@ -53393,12 +53670,20 @@ | |
| 53670 | } |
| 53671 | #endif |
| 53672 | assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); |
| 53673 | testcase( cbrk+size==usableSize ); |
| 53674 | testcase( pc+size==usableSize ); |
| 53675 | put2byte(pAddr, cbrk); |
| 53676 | if( temp==0 ){ |
| 53677 | int x; |
| 53678 | if( cbrk==pc ) continue; |
| 53679 | temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
| 53680 | x = get2byte(&data[hdr+5]); |
| 53681 | memcpy(&temp[x], &data[x], (cbrk+size) - x); |
| 53682 | src = temp; |
| 53683 | } |
| 53684 | memcpy(&data[cbrk], &src[pc], size); |
| 53685 | } |
| 53686 | assert( cbrk>=iCellFirst ); |
| 53687 | put2byte(&data[hdr+5], cbrk); |
| 53688 | data[hdr+1] = 0; |
| 53689 | data[hdr+2] = 0; |
| @@ -53408,10 +53693,73 @@ | |
| 53693 | if( cbrk-iCellFirst!=pPage->nFree ){ |
| 53694 | return SQLITE_CORRUPT_BKPT; |
| 53695 | } |
| 53696 | return SQLITE_OK; |
| 53697 | } |
| 53698 | |
| 53699 | /* |
| 53700 | ** Search the free-list on page pPg for space to store a cell nByte bytes in |
| 53701 | ** size. If one can be found, return a pointer to the space and remove it |
| 53702 | ** from the free-list. |
| 53703 | ** |
| 53704 | ** If no suitable space can be found on the free-list, return NULL. |
| 53705 | ** |
| 53706 | ** This function may detect corruption within pPg. If corruption is |
| 53707 | ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned. |
| 53708 | ** |
| 53709 | ** If a slot of at least nByte bytes is found but cannot be used because |
| 53710 | ** there are already at least 60 fragmented bytes on the page, return NULL. |
| 53711 | ** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true. |
| 53712 | */ |
| 53713 | static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){ |
| 53714 | const int hdr = pPg->hdrOffset; |
| 53715 | u8 * const aData = pPg->aData; |
| 53716 | int iAddr; |
| 53717 | int pc; |
| 53718 | int usableSize = pPg->pBt->usableSize; |
| 53719 | |
| 53720 | for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){ |
| 53721 | int size; /* Size of the free slot */ |
| 53722 | /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of |
| 53723 | ** increasing offset. */ |
| 53724 | if( pc>usableSize-4 || pc<iAddr+4 ){ |
| 53725 | *pRc = SQLITE_CORRUPT_BKPT; |
| 53726 | return 0; |
| 53727 | } |
| 53728 | /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each |
| 53729 | ** freeblock form a big-endian integer which is the size of the freeblock |
| 53730 | ** in bytes, including the 4-byte header. */ |
| 53731 | size = get2byte(&aData[pc+2]); |
| 53732 | if( size>=nByte ){ |
| 53733 | int x = size - nByte; |
| 53734 | testcase( x==4 ); |
| 53735 | testcase( x==3 ); |
| 53736 | if( x<4 ){ |
| 53737 | /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total |
| 53738 | ** number of bytes in fragments may not exceed 60. */ |
| 53739 | if( aData[hdr+7]>=60 ){ |
| 53740 | if( pbDefrag ) *pbDefrag = 1; |
| 53741 | return 0; |
| 53742 | } |
| 53743 | /* Remove the slot from the free-list. Update the number of |
| 53744 | ** fragmented bytes within the page. */ |
| 53745 | memcpy(&aData[iAddr], &aData[pc], 2); |
| 53746 | aData[hdr+7] += (u8)x; |
| 53747 | }else if( size+pc > usableSize ){ |
| 53748 | *pRc = SQLITE_CORRUPT_BKPT; |
| 53749 | return 0; |
| 53750 | }else{ |
| 53751 | /* The slot remains on the free-list. Reduce its size to account |
| 53752 | ** for the portion used by the new allocation. */ |
| 53753 | put2byte(&aData[pc+2], x); |
| 53754 | } |
| 53755 | return &aData[pc + x]; |
| 53756 | } |
| 53757 | } |
| 53758 | |
| 53759 | return 0; |
| 53760 | } |
| 53761 | |
| 53762 | /* |
| 53763 | ** Allocate nByte bytes of space from within the B-Tree page passed |
| 53764 | ** as the first argument. Write into *pIdx the index into pPage->aData[] |
| 53765 | ** of the first byte of allocated space. Return either SQLITE_OK or |
| @@ -53426,79 +53774,57 @@ | |
| 53774 | */ |
| 53775 | static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
| 53776 | const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ |
| 53777 | u8 * const data = pPage->aData; /* Local cache of pPage->aData */ |
| 53778 | int top; /* First byte of cell content area */ |
| 53779 | int rc = SQLITE_OK; /* Integer return code */ |
| 53780 | int gap; /* First byte of gap between cell pointers and cell content */ |
| 53781 | |
| 53782 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53783 | assert( pPage->pBt ); |
| 53784 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53785 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 53786 | assert( pPage->nFree>=nByte ); |
| 53787 | assert( pPage->nOverflow==0 ); |
| 53788 | assert( nByte < (int)(pPage->pBt->usableSize-8) ); |
| 53789 | |
| 53790 | assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); |
| 53791 | gap = pPage->cellOffset + 2*pPage->nCell; |
| 53792 | assert( gap<=65536 ); |
| 53793 | /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size |
| 53794 | ** and the reserved space is zero (the usual value for reserved space) |
| 53795 | ** then the cell content offset of an empty page wants to be 65536. |
| 53796 | ** However, that integer is too large to be stored in a 2-byte unsigned |
| 53797 | ** integer, so a value of 0 is used in its place. */ |
| 53798 | top = get2byteNotZero(&data[hdr+5]); |
| 53799 | if( gap>top ) return SQLITE_CORRUPT_BKPT; |
| 53800 | |
| 53801 | /* If there is enough space between gap and top for one more cell pointer |
| 53802 | ** array entry offset, and if the freelist is not empty, then search the |
| 53803 | ** freelist looking for a free slot big enough to satisfy the request. |
| 53804 | */ |
| 53805 | testcase( gap+2==top ); |
| 53806 | testcase( gap+1==top ); |
| 53807 | testcase( gap==top ); |
| 53808 | if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){ |
| 53809 | int bDefrag = 0; |
| 53810 | u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag); |
| 53811 | if( rc ) return rc; |
| 53812 | if( bDefrag ) goto defragment_page; |
| 53813 | if( pSpace ){ |
| 53814 | assert( pSpace>=data && (pSpace - data)<65536 ); |
| 53815 | *pIdx = (int)(pSpace - data); |
| 53816 | return SQLITE_OK; |
| 53817 | } |
| 53818 | } |
| 53819 | |
| 53820 | /* The request could not be fulfilled using a freelist slot. Check |
| 53821 | ** to see if defragmentation is necessary. |
| 53822 | */ |
| 53823 | testcase( gap+2+nByte==top ); |
| 53824 | if( gap+2+nByte>top ){ |
| 53825 | defragment_page: |
| 53826 | testcase( pPage->nCell==0 ); |
| 53827 | rc = defragmentPage(pPage); |
| 53828 | if( rc ) return rc; |
| 53829 | top = get2byteNotZero(&data[hdr+5]); |
| 53830 | assert( gap+nByte<=top ); |
| @@ -53542,11 +53868,11 @@ | |
| 53868 | unsigned char *data = pPage->aData; /* Page content */ |
| 53869 | |
| 53870 | assert( pPage->pBt!=0 ); |
| 53871 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53872 | assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
| 53873 | assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); |
| 53874 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53875 | assert( iSize>=4 ); /* Minimum cell size is 4 */ |
| 53876 | assert( iStart<=iLast ); |
| 53877 | |
| 53878 | /* Overwrite deleted information with zeros when the secure_delete |
| @@ -53637,22 +53963,36 @@ | |
| 53963 | pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
| 53964 | flagByte &= ~PTF_LEAF; |
| 53965 | pPage->childPtrSize = 4-4*pPage->leaf; |
| 53966 | pBt = pPage->pBt; |
| 53967 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 53968 | /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior |
| 53969 | ** table b-tree page. */ |
| 53970 | assert( (PTF_LEAFDATA|PTF_INTKEY)==5 ); |
| 53971 | /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf |
| 53972 | ** table b-tree page. */ |
| 53973 | assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 ); |
| 53974 | pPage->intKey = 1; |
| 53975 | pPage->intKeyLeaf = pPage->leaf; |
| 53976 | pPage->noPayload = !pPage->leaf; |
| 53977 | pPage->maxLocal = pBt->maxLeaf; |
| 53978 | pPage->minLocal = pBt->minLeaf; |
| 53979 | }else if( flagByte==PTF_ZERODATA ){ |
| 53980 | /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior |
| 53981 | ** index b-tree page. */ |
| 53982 | assert( (PTF_ZERODATA)==2 ); |
| 53983 | /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf |
| 53984 | ** index b-tree page. */ |
| 53985 | assert( (PTF_ZERODATA|PTF_LEAF)==10 ); |
| 53986 | pPage->intKey = 0; |
| 53987 | pPage->intKeyLeaf = 0; |
| 53988 | pPage->noPayload = 0; |
| 53989 | pPage->maxLocal = pBt->maxLocal; |
| 53990 | pPage->minLocal = pBt->minLocal; |
| 53991 | }else{ |
| 53992 | /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is |
| 53993 | ** an error. */ |
| 53994 | return SQLITE_CORRUPT_BKPT; |
| 53995 | } |
| 53996 | pPage->max1bytePayload = pBt->max1bytePayload; |
| 53997 | return SQLITE_OK; |
| 53998 | } |
| @@ -53688,25 +54028,37 @@ | |
| 54028 | |
| 54029 | pBt = pPage->pBt; |
| 54030 | |
| 54031 | hdr = pPage->hdrOffset; |
| 54032 | data = pPage->aData; |
| 54033 | /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating |
| 54034 | ** the b-tree page type. */ |
| 54035 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
| 54036 | assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 54037 | pPage->maskPage = (u16)(pBt->pageSize - 1); |
| 54038 | pPage->nOverflow = 0; |
| 54039 | usableSize = pBt->usableSize; |
| 54040 | pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize; |
| 54041 | pPage->aDataEnd = &data[usableSize]; |
| 54042 | pPage->aCellIdx = &data[cellOffset]; |
| 54043 | /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates |
| 54044 | ** the start of the cell content area. A zero value for this integer is |
| 54045 | ** interpreted as 65536. */ |
| 54046 | top = get2byteNotZero(&data[hdr+5]); |
| 54047 | /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
| 54048 | ** number of cells on the page. */ |
| 54049 | pPage->nCell = get2byte(&data[hdr+3]); |
| 54050 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 54051 | /* To many cells for a single page. The page must be corrupt */ |
| 54052 | return SQLITE_CORRUPT_BKPT; |
| 54053 | } |
| 54054 | testcase( pPage->nCell==MX_CELL(pBt) ); |
| 54055 | /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only |
| 54056 | ** possible for a root page of a table that contains no rows) then the |
| 54057 | ** offset to the cell content area will equal the page size minus the |
| 54058 | ** bytes of reserved space. */ |
| 54059 | assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB ); |
| 54060 | |
| 54061 | /* A malformed database page might cause us to read past the end |
| 54062 | ** of page when parsing a cell. |
| 54063 | ** |
| 54064 | ** The following block of code checks early to see if a cell extends |
| @@ -53736,17 +54088,24 @@ | |
| 54088 | } |
| 54089 | if( !pPage->leaf ) iCellLast++; |
| 54090 | } |
| 54091 | #endif |
| 54092 | |
| 54093 | /* Compute the total free space on the page |
| 54094 | ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the |
| 54095 | ** start of the first freeblock on the page, or is zero if there are no |
| 54096 | ** freeblocks. */ |
| 54097 | pc = get2byte(&data[hdr+1]); |
| 54098 | nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */ |
| 54099 | while( pc>0 ){ |
| 54100 | u16 next, size; |
| 54101 | if( pc<iCellFirst || pc>iCellLast ){ |
| 54102 | /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will |
| 54103 | ** always be at least one cell before the first freeblock. |
| 54104 | ** |
| 54105 | ** Or, the freeblock is off the end of the page |
| 54106 | */ |
| 54107 | return SQLITE_CORRUPT_BKPT; |
| 54108 | } |
| 54109 | next = get2byte(&data[pc]); |
| 54110 | size = get2byte(&data[pc+2]); |
| 54111 | if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){ |
| @@ -54148,10 +54507,13 @@ | |
| 54507 | pBt->pPage1 = 0; |
| 54508 | if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY; |
| 54509 | #ifdef SQLITE_SECURE_DELETE |
| 54510 | pBt->btsFlags |= BTS_SECURE_DELETE; |
| 54511 | #endif |
| 54512 | /* EVIDENCE-OF: R-51873-39618 The page size for a database file is |
| 54513 | ** determined by the 2-byte integer located at an offset of 16 bytes from |
| 54514 | ** the beginning of the database file. */ |
| 54515 | pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); |
| 54516 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 54517 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
| 54518 | pBt->pageSize = 0; |
| 54519 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| @@ -54166,10 +54528,13 @@ | |
| 54528 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 54529 | } |
| 54530 | #endif |
| 54531 | nReserve = 0; |
| 54532 | }else{ |
| 54533 | /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is |
| 54534 | ** determined by the one-byte unsigned integer found at an offset of 20 |
| 54535 | ** into the database file header. */ |
| 54536 | nReserve = zDbHeader[20]; |
| 54537 | pBt->btsFlags |= BTS_PAGESIZE_FIXED; |
| 54538 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 54539 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 54540 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| @@ -54675,10 +55040,13 @@ | |
| 55040 | if( nPage>0 ){ |
| 55041 | u32 pageSize; |
| 55042 | u32 usableSize; |
| 55043 | u8 *page1 = pPage1->aData; |
| 55044 | rc = SQLITE_NOTADB; |
| 55045 | /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins |
| 55046 | ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d |
| 55047 | ** 61 74 20 33 00. */ |
| 55048 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
| 55049 | goto page1_init_failed; |
| 55050 | } |
| 55051 | |
| 55052 | #ifdef SQLITE_OMIT_WAL |
| @@ -54715,26 +55083,39 @@ | |
| 55083 | } |
| 55084 | rc = SQLITE_NOTADB; |
| 55085 | } |
| 55086 | #endif |
| 55087 | |
| 55088 | /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload |
| 55089 | ** fractions and the leaf payload fraction values must be 64, 32, and 32. |
| 55090 | ** |
| 55091 | ** The original design allowed these amounts to vary, but as of |
| 55092 | ** version 3.6.0, we require them to be fixed. |
| 55093 | */ |
| 55094 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 55095 | goto page1_init_failed; |
| 55096 | } |
| 55097 | /* EVIDENCE-OF: R-51873-39618 The page size for a database file is |
| 55098 | ** determined by the 2-byte integer located at an offset of 16 bytes from |
| 55099 | ** the beginning of the database file. */ |
| 55100 | pageSize = (page1[16]<<8) | (page1[17]<<16); |
| 55101 | /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two |
| 55102 | ** between 512 and 65536 inclusive. */ |
| 55103 | if( ((pageSize-1)&pageSize)!=0 |
| 55104 | || pageSize>SQLITE_MAX_PAGE_SIZE |
| 55105 | || pageSize<=256 |
| 55106 | ){ |
| 55107 | goto page1_init_failed; |
| 55108 | } |
| 55109 | assert( (pageSize & 7)==0 ); |
| 55110 | /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte |
| 55111 | ** integer at offset 20 is the number of bytes of space at the end of |
| 55112 | ** each page to reserve for extensions. |
| 55113 | ** |
| 55114 | ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is |
| 55115 | ** determined by the one-byte unsigned integer found at an offset of 20 |
| 55116 | ** into the database file header. */ |
| 55117 | usableSize = pageSize - page1[20]; |
| 55118 | if( (u32)pageSize!=pBt->pageSize ){ |
| 55119 | /* After reading the first page of the database assuming a page size |
| 55120 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 55121 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| @@ -54751,10 +55132,13 @@ | |
| 55132 | } |
| 55133 | if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){ |
| 55134 | rc = SQLITE_CORRUPT_BKPT; |
| 55135 | goto page1_init_failed; |
| 55136 | } |
| 55137 | /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to |
| 55138 | ** be less than 480. In other words, if the page size is 512, then the |
| 55139 | ** reserved space size cannot exceed 32. */ |
| 55140 | if( usableSize<480 ){ |
| 55141 | goto page1_init_failed; |
| 55142 | } |
| 55143 | pBt->pageSize = pageSize; |
| 55144 | pBt->usableSize = usableSize; |
| @@ -57328,10 +57712,12 @@ | |
| 57712 | |
| 57713 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 57714 | assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) ); |
| 57715 | pPage1 = pBt->pPage1; |
| 57716 | mxPage = btreePagecount(pBt); |
| 57717 | /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36 |
| 57718 | ** stores stores the total number of pages on the freelist. */ |
| 57719 | n = get4byte(&pPage1->aData[36]); |
| 57720 | testcase( n==mxPage-1 ); |
| 57721 | if( n>=mxPage ){ |
| 57722 | return SQLITE_CORRUPT_BKPT; |
| 57723 | } |
| @@ -57374,12 +57760,18 @@ | |
| 57760 | ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT) |
| 57761 | */ |
| 57762 | do { |
| 57763 | pPrevTrunk = pTrunk; |
| 57764 | if( pPrevTrunk ){ |
| 57765 | /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page |
| 57766 | ** is the page number of the next freelist trunk page in the list or |
| 57767 | ** zero if this is the last freelist trunk page. */ |
| 57768 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
| 57769 | }else{ |
| 57770 | /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32 |
| 57771 | ** stores the page number of the first page of the freelist, or zero if |
| 57772 | ** the freelist is empty. */ |
| 57773 | iTrunk = get4byte(&pPage1->aData[32]); |
| 57774 | } |
| 57775 | testcase( iTrunk==mxPage ); |
| 57776 | if( iTrunk>mxPage ){ |
| 57777 | rc = SQLITE_CORRUPT_BKPT; |
| @@ -57390,12 +57782,13 @@ | |
| 57782 | pTrunk = 0; |
| 57783 | goto end_allocate_page; |
| 57784 | } |
| 57785 | assert( pTrunk!=0 ); |
| 57786 | assert( pTrunk->aData!=0 ); |
| 57787 | /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page |
| 57788 | ** is the number of leaf page pointers to follow. */ |
| 57789 | k = get4byte(&pTrunk->aData[4]); |
| 57790 | if( k==0 && !searchList ){ |
| 57791 | /* The trunk has no leaves and the list is not being searched. |
| 57792 | ** So extract the trunk page itself and use it as the newly |
| 57793 | ** allocated page */ |
| 57794 | assert( pPrevTrunk==0 ); |
| @@ -57709,10 +58102,15 @@ | |
| 58102 | ** to maintain backwards compatibility with older versions of SQLite, |
| 58103 | ** we will continue to restrict the number of entries to usableSize/4 - 8 |
| 58104 | ** for now. At some point in the future (once everyone has upgraded |
| 58105 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 58106 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
| 58107 | ** |
| 58108 | ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still |
| 58109 | ** avoid using the last six entries in the freelist trunk page array in |
| 58110 | ** order that database files created by newer versions of SQLite can be |
| 58111 | ** read by older versions of SQLite. |
| 58112 | */ |
| 58113 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
| 58114 | if( rc==SQLITE_OK ){ |
| 58115 | put4byte(&pTrunk->aData[4], nLeaf+1); |
| 58116 | put4byte(&pTrunk->aData[8+nLeaf*4], iPage); |
| @@ -58060,13 +58458,21 @@ | |
| 58458 | if( rc ){ |
| 58459 | *pRC = rc; |
| 58460 | return; |
| 58461 | } |
| 58462 | pPage->nCell--; |
| 58463 | if( pPage->nCell==0 ){ |
| 58464 | memset(&data[hdr+1], 0, 4); |
| 58465 | data[hdr+7] = 0; |
| 58466 | put2byte(&data[hdr+5], pPage->pBt->usableSize); |
| 58467 | pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset |
| 58468 | - pPage->childPtrSize - 8; |
| 58469 | }else{ |
| 58470 | memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); |
| 58471 | put2byte(&data[hdr+3], pPage->nCell); |
| 58472 | pPage->nFree += 2; |
| 58473 | } |
| 58474 | } |
| 58475 | |
| 58476 | /* |
| 58477 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 58478 | ** content of the cell. |
| @@ -58157,49 +58563,275 @@ | |
| 58563 | #endif |
| 58564 | } |
| 58565 | } |
| 58566 | |
| 58567 | /* |
| 58568 | ** Array apCell[] contains pointers to nCell b-tree page cells. The |
| 58569 | ** szCell[] array contains the size in bytes of each cell. This function |
| 58570 | ** replaces the current contents of page pPg with the contents of the cell |
| 58571 | ** array. |
| 58572 | ** |
| 58573 | ** Some of the cells in apCell[] may currently be stored in pPg. This |
| 58574 | ** function works around problems caused by this by making a copy of any |
| 58575 | ** such cells before overwriting the page data. |
| 58576 | ** |
| 58577 | ** The MemPage.nFree field is invalidated by this function. It is the |
| 58578 | ** responsibility of the caller to set it correctly. |
| 58579 | */ |
| 58580 | static void rebuildPage( |
| 58581 | MemPage *pPg, /* Edit this page */ |
| 58582 | int nCell, /* Final number of cells on page */ |
| 58583 | u8 **apCell, /* Array of cells */ |
| 58584 | u16 *szCell /* Array of cell sizes */ |
| 58585 | ){ |
| 58586 | const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ |
| 58587 | u8 * const aData = pPg->aData; /* Pointer to data for pPg */ |
| 58588 | const int usableSize = pPg->pBt->usableSize; |
| 58589 | u8 * const pEnd = &aData[usableSize]; |
| 58590 | int i; |
| 58591 | u8 *pCellptr = pPg->aCellIdx; |
| 58592 | u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
| 58593 | u8 *pData; |
| 58594 | |
| 58595 | i = get2byte(&aData[hdr+5]); |
| 58596 | memcpy(&pTmp[i], &aData[i], usableSize - i); |
| 58597 | |
| 58598 | pData = pEnd; |
| 58599 | for(i=0; i<nCell; i++){ |
| 58600 | u8 *pCell = apCell[i]; |
| 58601 | if( pCell>aData && pCell<pEnd ){ |
| 58602 | pCell = &pTmp[pCell - aData]; |
| 58603 | } |
| 58604 | pData -= szCell[i]; |
| 58605 | memcpy(pData, pCell, szCell[i]); |
| 58606 | put2byte(pCellptr, (pData - aData)); |
| 58607 | pCellptr += 2; |
| 58608 | assert( szCell[i]==cellSizePtr(pPg, pCell) ); |
| 58609 | } |
| 58610 | |
| 58611 | /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ |
| 58612 | pPg->nCell = nCell; |
| 58613 | pPg->nOverflow = 0; |
| 58614 | |
| 58615 | put2byte(&aData[hdr+1], 0); |
| 58616 | put2byte(&aData[hdr+3], pPg->nCell); |
| 58617 | put2byte(&aData[hdr+5], pData - aData); |
| 58618 | aData[hdr+7] = 0x00; |
| 58619 | } |
| 58620 | |
| 58621 | /* |
| 58622 | ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
| 58623 | ** contains the size in bytes of each such cell. This function attempts to |
| 58624 | ** add the cells stored in the array to page pPg. If it cannot (because |
| 58625 | ** the page needs to be defragmented before the cells will fit), non-zero |
| 58626 | ** is returned. Otherwise, if the cells are added successfully, zero is |
| 58627 | ** returned. |
| 58628 | ** |
| 58629 | ** Argument pCellptr points to the first entry in the cell-pointer array |
| 58630 | ** (part of page pPg) to populate. After cell apCell[0] is written to the |
| 58631 | ** page body, a 16-bit offset is written to pCellptr. And so on, for each |
| 58632 | ** cell in the array. It is the responsibility of the caller to ensure |
| 58633 | ** that it is safe to overwrite this part of the cell-pointer array. |
| 58634 | ** |
| 58635 | ** When this function is called, *ppData points to the start of the |
| 58636 | ** content area on page pPg. If the size of the content area is extended, |
| 58637 | ** *ppData is updated to point to the new start of the content area |
| 58638 | ** before returning. |
| 58639 | ** |
| 58640 | ** Finally, argument pBegin points to the byte immediately following the |
| 58641 | ** end of the space required by this page for the cell-pointer area (for |
| 58642 | ** all cells - not just those inserted by the current call). If the content |
| 58643 | ** area must be extended to before this point in order to accomodate all |
| 58644 | ** cells in apCell[], then the cells do not fit and non-zero is returned. |
| 58645 | */ |
| 58646 | static int pageInsertArray( |
| 58647 | MemPage *pPg, /* Page to add cells to */ |
| 58648 | u8 *pBegin, /* End of cell-pointer array */ |
| 58649 | u8 **ppData, /* IN/OUT: Page content -area pointer */ |
| 58650 | u8 *pCellptr, /* Pointer to cell-pointer area */ |
| 58651 | int nCell, /* Number of cells to add to pPg */ |
| 58652 | u8 **apCell, /* Array of cells */ |
| 58653 | u16 *szCell /* Array of cell sizes */ |
| 58654 | ){ |
| 58655 | int i; |
| 58656 | u8 *aData = pPg->aData; |
| 58657 | u8 *pData = *ppData; |
| 58658 | const int bFreelist = aData[1] || aData[2]; |
| 58659 | assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ |
| 58660 | for(i=0; i<nCell; i++){ |
| 58661 | int sz = szCell[i]; |
| 58662 | int rc; |
| 58663 | u8 *pSlot; |
| 58664 | if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){ |
| 58665 | pData -= sz; |
| 58666 | if( pData<pBegin ) return 1; |
| 58667 | pSlot = pData; |
| 58668 | } |
| 58669 | memcpy(pSlot, apCell[i], sz); |
| 58670 | put2byte(pCellptr, (pSlot - aData)); |
| 58671 | pCellptr += 2; |
| 58672 | } |
| 58673 | *ppData = pData; |
| 58674 | return 0; |
| 58675 | } |
| 58676 | |
| 58677 | /* |
| 58678 | ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
| 58679 | ** contains the size in bytes of each such cell. This function adds the |
| 58680 | ** space associated with each cell in the array that is currently stored |
| 58681 | ** within the body of pPg to the pPg free-list. The cell-pointers and other |
| 58682 | ** fields of the page are not updated. |
| 58683 | ** |
| 58684 | ** This function returns the total number of cells added to the free-list. |
| 58685 | */ |
| 58686 | static int pageFreeArray( |
| 58687 | MemPage *pPg, /* Page to edit */ |
| 58688 | int nCell, /* Cells to delete */ |
| 58689 | u8 **apCell, /* Array of cells */ |
| 58690 | u16 *szCell /* Array of cell sizes */ |
| 58691 | ){ |
| 58692 | u8 * const aData = pPg->aData; |
| 58693 | u8 * const pEnd = &aData[pPg->pBt->usableSize]; |
| 58694 | u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; |
| 58695 | int nRet = 0; |
| 58696 | int i; |
| 58697 | u8 *pFree = 0; |
| 58698 | int szFree = 0; |
| 58699 | |
| 58700 | for(i=0; i<nCell; i++){ |
| 58701 | u8 *pCell = apCell[i]; |
| 58702 | if( pCell>=pStart && pCell<pEnd ){ |
| 58703 | int sz = szCell[i]; |
| 58704 | if( pFree!=(pCell + sz) ){ |
| 58705 | if( pFree ){ |
| 58706 | assert( pFree>aData && (pFree - aData)<65536 ); |
| 58707 | freeSpace(pPg, (u16)(pFree - aData), szFree); |
| 58708 | } |
| 58709 | pFree = pCell; |
| 58710 | szFree = sz; |
| 58711 | if( pFree+sz>pEnd ) return 0; |
| 58712 | }else{ |
| 58713 | pFree = pCell; |
| 58714 | szFree += sz; |
| 58715 | } |
| 58716 | nRet++; |
| 58717 | } |
| 58718 | } |
| 58719 | if( pFree ){ |
| 58720 | assert( pFree>aData && (pFree - aData)<65536 ); |
| 58721 | freeSpace(pPg, (u16)(pFree - aData), szFree); |
| 58722 | } |
| 58723 | return nRet; |
| 58724 | } |
| 58725 | |
| 58726 | /* |
| 58727 | ** apCell[] and szCell[] contains pointers to and sizes of all cells in the |
| 58728 | ** pages being balanced. The current page, pPg, has pPg->nCell cells starting |
| 58729 | ** with apCell[iOld]. After balancing, this page should hold nNew cells |
| 58730 | ** starting at apCell[iNew]. |
| 58731 | ** |
| 58732 | ** This routine makes the necessary adjustments to pPg so that it contains |
| 58733 | ** the correct cells after being balanced. |
| 58734 | ** |
| 58735 | ** The pPg->nFree field is invalid when this function returns. It is the |
| 58736 | ** responsibility of the caller to set it correctly. |
| 58737 | */ |
| 58738 | static void editPage( |
| 58739 | MemPage *pPg, /* Edit this page */ |
| 58740 | int iOld, /* Index of first cell currently on page */ |
| 58741 | int iNew, /* Index of new first cell on page */ |
| 58742 | int nNew, /* Final number of cells on page */ |
| 58743 | u8 **apCell, /* Array of cells */ |
| 58744 | u16 *szCell /* Array of cell sizes */ |
| 58745 | ){ |
| 58746 | u8 * const aData = pPg->aData; |
| 58747 | const int hdr = pPg->hdrOffset; |
| 58748 | u8 *pBegin = &pPg->aCellIdx[nNew * 2]; |
| 58749 | int nCell = pPg->nCell; /* Cells stored on pPg */ |
| 58750 | u8 *pData; |
| 58751 | u8 *pCellptr; |
| 58752 | int i; |
| 58753 | int iOldEnd = iOld + pPg->nCell + pPg->nOverflow; |
| 58754 | int iNewEnd = iNew + nNew; |
| 58755 | |
| 58756 | #ifdef SQLITE_DEBUG |
| 58757 | u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
| 58758 | memcpy(pTmp, aData, pPg->pBt->usableSize); |
| 58759 | #endif |
| 58760 | |
| 58761 | /* Remove cells from the start and end of the page */ |
| 58762 | if( iOld<iNew ){ |
| 58763 | int nShift = pageFreeArray( |
| 58764 | pPg, iNew-iOld, &apCell[iOld], &szCell[iOld] |
| 58765 | ); |
| 58766 | memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); |
| 58767 | nCell -= nShift; |
| 58768 | } |
| 58769 | if( iNewEnd < iOldEnd ){ |
| 58770 | nCell -= pageFreeArray( |
| 58771 | pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd] |
| 58772 | ); |
| 58773 | } |
| 58774 | |
| 58775 | pData = &aData[get2byteNotZero(&aData[hdr+5])]; |
| 58776 | if( pData<pBegin ) goto editpage_fail; |
| 58777 | |
| 58778 | /* Add cells to the start of the page */ |
| 58779 | if( iNew<iOld ){ |
| 58780 | int nAdd = MIN(nNew,iOld-iNew); |
| 58781 | assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB ); |
| 58782 | pCellptr = pPg->aCellIdx; |
| 58783 | memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); |
| 58784 | if( pageInsertArray( |
| 58785 | pPg, pBegin, &pData, pCellptr, |
| 58786 | nAdd, &apCell[iNew], &szCell[iNew] |
| 58787 | ) ) goto editpage_fail; |
| 58788 | nCell += nAdd; |
| 58789 | } |
| 58790 | |
| 58791 | /* Add any overflow cells */ |
| 58792 | for(i=0; i<pPg->nOverflow; i++){ |
| 58793 | int iCell = (iOld + pPg->aiOvfl[i]) - iNew; |
| 58794 | if( iCell>=0 && iCell<nNew ){ |
| 58795 | pCellptr = &pPg->aCellIdx[iCell * 2]; |
| 58796 | memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); |
| 58797 | nCell++; |
| 58798 | if( pageInsertArray( |
| 58799 | pPg, pBegin, &pData, pCellptr, |
| 58800 | 1, &apCell[iCell + iNew], &szCell[iCell + iNew] |
| 58801 | ) ) goto editpage_fail; |
| 58802 | } |
| 58803 | } |
| 58804 | |
| 58805 | /* Append cells to the end of the page */ |
| 58806 | pCellptr = &pPg->aCellIdx[nCell*2]; |
| 58807 | if( pageInsertArray( |
| 58808 | pPg, pBegin, &pData, pCellptr, |
| 58809 | nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell] |
| 58810 | ) ) goto editpage_fail; |
| 58811 | |
| 58812 | pPg->nCell = nNew; |
| 58813 | pPg->nOverflow = 0; |
| 58814 | |
| 58815 | put2byte(&aData[hdr+3], pPg->nCell); |
| 58816 | put2byte(&aData[hdr+5], pData - aData); |
| 58817 | |
| 58818 | #ifdef SQLITE_DEBUG |
| 58819 | for(i=0; i<nNew && !CORRUPT_DB; i++){ |
| 58820 | u8 *pCell = apCell[i+iNew]; |
| 58821 | int iOff = get2byte(&pPg->aCellIdx[i*2]); |
| 58822 | if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){ |
| 58823 | pCell = &pTmp[pCell - aData]; |
| 58824 | } |
| 58825 | assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) ); |
| 58826 | } |
| 58827 | #endif |
| 58828 | |
| 58829 | return; |
| 58830 | editpage_fail: |
| 58831 | /* Unable to edit this page. Rebuild it from scratch instead. */ |
| 58832 | rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]); |
| 58833 | } |
| 58834 | |
| 58835 | /* |
| 58836 | ** The following parameters determine how many adjacent pages get involved |
| 58837 | ** in a balancing operation. NN is the number of neighbors on either side |
| @@ -58267,11 +58899,12 @@ | |
| 58899 | u8 *pStop; |
| 58900 | |
| 58901 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
| 58902 | assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); |
| 58903 | zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); |
| 58904 | rebuildPage(pNew, 1, &pCell, &szCell); |
| 58905 | pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; |
| 58906 | |
| 58907 | /* If this is an auto-vacuum database, update the pointer map |
| 58908 | ** with entries for the new page, and any pointer from the |
| 58909 | ** cell on the page to an overflow page. If either of these |
| 58910 | ** operations fails, the return code is set, but the contents |
| @@ -58486,21 +59119,26 @@ | |
| 59119 | int subtotal; /* Subtotal of bytes in cells on one page */ |
| 59120 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
| 59121 | int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ |
| 59122 | int szScratch; /* Size of scratch memory requested */ |
| 59123 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 59124 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 59125 | u8 *pRight; /* Location in parent of right-sibling pointer */ |
| 59126 | u8 *apDiv[NB-1]; /* Divider cells in pParent */ |
| 59127 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 59128 | int cntOld[NB+2]; /* Old index in aCell[] after i-th page */ |
| 59129 | int szNew[NB+2]; /* Combined size of cells placed on i-th page */ |
| 59130 | u8 **apCell = 0; /* All cells begin balanced */ |
| 59131 | u16 *szCell; /* Local size of all cells in apCell[] */ |
| 59132 | u8 *aSpace1; /* Space for copies of dividers cells */ |
| 59133 | Pgno pgno; /* Temp var to store a page number in */ |
| 59134 | u8 abDone[NB+2]; /* True after i'th new page is populated */ |
| 59135 | Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ |
| 59136 | Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ |
| 59137 | u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ |
| 59138 | |
| 59139 | memset(abDone, 0, sizeof(abDone)); |
| 59140 | pBt = pParent->pBt; |
| 59141 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 59142 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 59143 | |
| 59144 | #if 0 |
| @@ -58605,16 +59243,18 @@ | |
| 59243 | nMaxCells = (nMaxCells + 3)&~3; |
| 59244 | |
| 59245 | /* |
| 59246 | ** Allocate space for memory structures |
| 59247 | */ |
| 59248 | szScratch = |
| 59249 | nMaxCells*sizeof(u8*) /* apCell */ |
| 59250 | + nMaxCells*sizeof(u16) /* szCell */ |
| 59251 | + pBt->pageSize; /* aSpace1 */ |
| 59252 | |
| 59253 | /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer |
| 59254 | ** that is more than 6 times the database page size. */ |
| 59255 | assert( szScratch<=6*pBt->pageSize ); |
| 59256 | apCell = sqlite3ScratchMalloc( szScratch ); |
| 59257 | if( apCell==0 ){ |
| 59258 | rc = SQLITE_NOMEM; |
| 59259 | goto balance_cleanup; |
| 59260 | } |
| @@ -58623,12 +59263,12 @@ | |
| 59263 | assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); |
| 59264 | |
| 59265 | /* |
| 59266 | ** Load pointers to all cells on sibling pages and the divider cells |
| 59267 | ** into the local apCell[] array. Make copies of the divider cells |
| 59268 | ** into space obtained from aSpace1[]. The divider cells have already |
| 59269 | ** been removed from pParent. |
| 59270 | ** |
| 59271 | ** If the siblings are on leaf pages, then the child pointers of the |
| 59272 | ** divider cells are stripped from the cells before they are copied |
| 59273 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
| 59274 | ** child pointers. If siblings are not leaves, then all cell in |
| @@ -58640,19 +59280,11 @@ | |
| 59280 | */ |
| 59281 | leafCorrection = apOld[0]->leaf*4; |
| 59282 | leafData = apOld[0]->intKeyLeaf; |
| 59283 | for(i=0; i<nOld; i++){ |
| 59284 | int limit; |
| 59285 | MemPage *pOld = apOld[i]; |
| 59286 | |
| 59287 | limit = pOld->nCell+pOld->nOverflow; |
| 59288 | if( pOld->nOverflow>0 ){ |
| 59289 | for(j=0; j<limit; j++){ |
| 59290 | assert( nCell<nMaxCells ); |
| @@ -58669,10 +59301,11 @@ | |
| 59301 | apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j); |
| 59302 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
| 59303 | nCell++; |
| 59304 | } |
| 59305 | } |
| 59306 | cntOld[i] = nCell; |
| 59307 | if( i<nOld-1 && !leafData){ |
| 59308 | u16 sz = (u16)szNew[i]; |
| 59309 | u8 *pTemp; |
| 59310 | assert( nCell<nMaxCells ); |
| 59311 | szCell[nCell] = sz; |
| @@ -58720,11 +59353,11 @@ | |
| 59353 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
| 59354 | for(subtotal=k=i=0; i<nCell; i++){ |
| 59355 | assert( i<nMaxCells ); |
| 59356 | subtotal += szCell[i] + 2; |
| 59357 | if( subtotal > usableSpace ){ |
| 59358 | szNew[k] = subtotal - szCell[i] - 2; |
| 59359 | cntNew[k] = i; |
| 59360 | if( leafData ){ i--; } |
| 59361 | subtotal = 0; |
| 59362 | k++; |
| 59363 | if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
| @@ -58734,13 +59367,14 @@ | |
| 59367 | cntNew[k] = nCell; |
| 59368 | k++; |
| 59369 | |
| 59370 | /* |
| 59371 | ** The packing computed by the previous block is biased toward the siblings |
| 59372 | ** on the left side (siblings with smaller keys). The left siblings are |
| 59373 | ** always nearly full, while the right-most sibling might be nearly empty. |
| 59374 | ** The next block of code attempts to adjust the packing of siblings to |
| 59375 | ** get a better balance. |
| 59376 | ** |
| 59377 | ** This adjustment is more than an optimization. The packing above might |
| 59378 | ** be so out of balance as to be illegal. For example, the right-most |
| 59379 | ** sibling might be completely empty. This adjustment is not optional. |
| 59380 | */ |
| @@ -58765,26 +59399,22 @@ | |
| 59399 | } |
| 59400 | szNew[i] = szRight; |
| 59401 | szNew[i-1] = szLeft; |
| 59402 | } |
| 59403 | |
| 59404 | /* Sanity check: For a non-corrupt database file one of the follwing |
| 59405 | ** must be true: |
| 59406 | ** (1) We found one or more cells (cntNew[0])>0), or |
| 59407 | ** (2) pPage is a virtual root page. A virtual root page is when |
| 59408 | ** the real root page is page 1 and we are the only child of |
| 59409 | ** that page. |
| 59410 | */ |
| 59411 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB); |
| 59412 | TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n", |
| 59413 | apOld[0]->pgno, apOld[0]->nCell, |
| 59414 | nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0, |
| 59415 | nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0 |
| 59416 | )); |
| 59417 | |
| 59418 | /* |
| 59419 | ** Allocate k new pages. Reuse old pages where possible. |
| 59420 | */ |
| @@ -58803,12 +59433,14 @@ | |
| 59433 | if( rc ) goto balance_cleanup; |
| 59434 | }else{ |
| 59435 | assert( i>0 ); |
| 59436 | rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0); |
| 59437 | if( rc ) goto balance_cleanup; |
| 59438 | zeroPage(pNew, pageFlags); |
| 59439 | apNew[i] = pNew; |
| 59440 | nNew++; |
| 59441 | cntOld[i] = nCell; |
| 59442 | |
| 59443 | /* Set the pointer-map entry for the new sibling page. */ |
| 59444 | if( ISAUTOVACUUM ){ |
| 59445 | ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); |
| 59446 | if( rc!=SQLITE_OK ){ |
| @@ -58816,139 +59448,247 @@ | |
| 59448 | } |
| 59449 | } |
| 59450 | } |
| 59451 | } |
| 59452 | |
| 59453 | /* |
| 59454 | ** Reassign page numbers so that the new pages are in ascending order. |
| 59455 | ** This helps to keep entries in the disk file in order so that a scan |
| 59456 | ** of the table is closer to a linear scan through the file. That in turn |
| 59457 | ** helps the operating system to deliver pages from the disk more rapidly. |
| 59458 | ** |
| 59459 | ** An O(n^2) insertion sort algorithm is used, but since n is never more |
| 59460 | ** than (NB+2) (a small constant), that should not be a problem. |
| 59461 | ** |
| 59462 | ** When NB==3, this one optimization makes the database about 25% faster |
| 59463 | ** for large insertions and deletions. |
| 59464 | */ |
| 59465 | for(i=0; i<nNew; i++){ |
| 59466 | aPgOrder[i] = aPgno[i] = apNew[i]->pgno; |
| 59467 | aPgFlags[i] = apNew[i]->pDbPage->flags; |
| 59468 | for(j=0; j<i; j++){ |
| 59469 | if( aPgno[j]==aPgno[i] ){ |
| 59470 | /* This branch is taken if the set of sibling pages somehow contains |
| 59471 | ** duplicate entries. This can happen if the database is corrupt. |
| 59472 | ** It would be simpler to detect this as part of the loop below, but |
| 59473 | ** we do the detection here in order to avoid populating the pager |
| 59474 | ** cache with two separate objects associated with the same |
| 59475 | ** page number. */ |
| 59476 | assert( CORRUPT_DB ); |
| 59477 | rc = SQLITE_CORRUPT_BKPT; |
| 59478 | goto balance_cleanup; |
| 59479 | } |
| 59480 | } |
| 59481 | } |
| 59482 | for(i=0; i<nNew; i++){ |
| 59483 | int iBest = 0; /* aPgno[] index of page number to use */ |
| 59484 | for(j=1; j<nNew; j++){ |
| 59485 | if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j; |
| 59486 | } |
| 59487 | pgno = aPgOrder[iBest]; |
| 59488 | aPgOrder[iBest] = 0xffffffff; |
| 59489 | if( iBest!=i ){ |
| 59490 | if( iBest>i ){ |
| 59491 | sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0); |
| 59492 | } |
| 59493 | sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]); |
| 59494 | apNew[i]->pgno = pgno; |
| 59495 | } |
| 59496 | } |
| 59497 | |
| 59498 | TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) " |
| 59499 | "%d(%d nc=%d) %d(%d nc=%d)\n", |
| 59500 | apNew[0]->pgno, szNew[0], cntNew[0], |
| 59501 | nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, |
| 59502 | nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0, |
| 59503 | nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, |
| 59504 | nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0, |
| 59505 | nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, |
| 59506 | nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0, |
| 59507 | nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0, |
| 59508 | nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0 |
| 59509 | )); |
| 59510 | |
| 59511 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 59512 | put4byte(pRight, apNew[nNew-1]->pgno); |
| 59513 | |
| 59514 | /* If the sibling pages are not leaves, ensure that the right-child pointer |
| 59515 | ** of the right-most new sibling page is set to the value that was |
| 59516 | ** originally in the same field of the right-most old sibling page. */ |
| 59517 | if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){ |
| 59518 | MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1]; |
| 59519 | memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4); |
| 59520 | } |
| 59521 | |
| 59522 | /* Make any required updates to pointer map entries associated with |
| 59523 | ** cells stored on sibling pages following the balance operation. Pointer |
| 59524 | ** map entries associated with divider cells are set by the insertCell() |
| 59525 | ** routine. The associated pointer map entries are: |
| 59526 | ** |
| 59527 | ** a) if the cell contains a reference to an overflow chain, the |
| 59528 | ** entry associated with the first page in the overflow chain, and |
| 59529 | ** |
| 59530 | ** b) if the sibling pages are not leaves, the child page associated |
| 59531 | ** with the cell. |
| 59532 | ** |
| 59533 | ** If the sibling pages are not leaves, then the pointer map entry |
| 59534 | ** associated with the right-child of each sibling may also need to be |
| 59535 | ** updated. This happens below, after the sibling pages have been |
| 59536 | ** populated, not here. |
| 59537 | */ |
| 59538 | if( ISAUTOVACUUM ){ |
| 59539 | MemPage *pNew = apNew[0]; |
| 59540 | u8 *aOld = pNew->aData; |
| 59541 | int cntOldNext = pNew->nCell + pNew->nOverflow; |
| 59542 | int usableSize = pBt->usableSize; |
| 59543 | int iNew = 0; |
| 59544 | int iOld = 0; |
| 59545 | |
| 59546 | for(i=0; i<nCell; i++){ |
| 59547 | u8 *pCell = apCell[i]; |
| 59548 | if( i==cntOldNext ){ |
| 59549 | MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld]; |
| 59550 | cntOldNext += pOld->nCell + pOld->nOverflow + !leafData; |
| 59551 | aOld = pOld->aData; |
| 59552 | } |
| 59553 | if( i==cntNew[iNew] ){ |
| 59554 | pNew = apNew[++iNew]; |
| 59555 | if( !leafData ) continue; |
| 59556 | } |
| 59557 | |
| 59558 | /* Cell pCell is destined for new sibling page pNew. Originally, it |
| 59559 | ** was either part of sibling page iOld (possibly an overflow cell), |
| 59560 | ** or else the divider cell to the left of sibling page iOld. So, |
| 59561 | ** if sibling page iOld had the same page number as pNew, and if |
| 59562 | ** pCell really was a part of sibling page iOld (not a divider or |
| 59563 | ** overflow cell), we can skip updating the pointer map entries. */ |
| 59564 | if( pNew->pgno!=aPgno[iOld] || pCell<aOld || pCell>=&aOld[usableSize] ){ |
| 59565 | if( !leafCorrection ){ |
| 59566 | ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); |
| 59567 | } |
| 59568 | if( szCell[i]>pNew->minLocal ){ |
| 59569 | ptrmapPutOvflPtr(pNew, pCell, &rc); |
| 59570 | } |
| 59571 | } |
| 59572 | } |
| 59573 | } |
| 59574 | |
| 59575 | /* Insert new divider cells into pParent. */ |
| 59576 | for(i=0; i<nNew-1; i++){ |
| 59577 | u8 *pCell; |
| 59578 | u8 *pTemp; |
| 59579 | int sz; |
| 59580 | MemPage *pNew = apNew[i]; |
| 59581 | j = cntNew[i]; |
| 59582 | |
| 59583 | assert( j<nMaxCells ); |
| 59584 | pCell = apCell[j]; |
| 59585 | sz = szCell[j] + leafCorrection; |
| 59586 | pTemp = &aOvflSpace[iOvflSpace]; |
| 59587 | if( !pNew->leaf ){ |
| 59588 | memcpy(&pNew->aData[8], pCell, 4); |
| 59589 | }else if( leafData ){ |
| 59590 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
| 59591 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 59592 | ** cell consists of the integer key for the right-most cell of |
| 59593 | ** the sibling-page assembled above only. |
| 59594 | */ |
| 59595 | CellInfo info; |
| 59596 | j--; |
| 59597 | btreeParseCellPtr(pNew, apCell[j], &info); |
| 59598 | pCell = pTemp; |
| 59599 | sz = 4 + putVarint(&pCell[4], info.nKey); |
| 59600 | pTemp = 0; |
| 59601 | }else{ |
| 59602 | pCell -= 4; |
| 59603 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
| 59604 | ** previously stored on a leaf node, and its reported size was 4 |
| 59605 | ** bytes, then it may actually be smaller than this |
| 59606 | ** (see btreeParseCellPtr(), 4 bytes is the minimum size of |
| 59607 | ** any cell). But it is important to pass the correct size to |
| 59608 | ** insertCell(), so reparse the cell now. |
| 59609 | ** |
| 59610 | ** Note that this can never happen in an SQLite data file, as all |
| 59611 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 59612 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 59613 | */ |
| 59614 | if( szCell[j]==4 ){ |
| 59615 | assert(leafCorrection==4); |
| 59616 | sz = cellSizePtr(pParent, pCell); |
| 59617 | } |
| 59618 | } |
| 59619 | iOvflSpace += sz; |
| 59620 | assert( sz<=pBt->maxLocal+23 ); |
| 59621 | assert( iOvflSpace <= (int)pBt->pageSize ); |
| 59622 | insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc); |
| 59623 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
| 59624 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 59625 | } |
| 59626 | |
| 59627 | /* Now update the actual sibling pages. The order in which they are updated |
| 59628 | ** is important, as this code needs to avoid disrupting any page from which |
| 59629 | ** cells may still to be read. In practice, this means: |
| 59630 | ** |
| 59631 | ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1]) |
| 59632 | ** then it is not safe to update page apNew[iPg] until after |
| 59633 | ** the left-hand sibling apNew[iPg-1] has been updated. |
| 59634 | ** |
| 59635 | ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1]) |
| 59636 | ** then it is not safe to update page apNew[iPg] until after |
| 59637 | ** the right-hand sibling apNew[iPg+1] has been updated. |
| 59638 | ** |
| 59639 | ** If neither of the above apply, the page is safe to update. |
| 59640 | ** |
| 59641 | ** The iPg value in the following loop starts at nNew-1 goes down |
| 59642 | ** to 0, then back up to nNew-1 again, thus making two passes over |
| 59643 | ** the pages. On the initial downward pass, only condition (1) above |
| 59644 | ** needs to be tested because (2) will always be true from the previous |
| 59645 | ** step. On the upward pass, both conditions are always true, so the |
| 59646 | ** upwards pass simply processes pages that were missed on the downward |
| 59647 | ** pass. |
| 59648 | */ |
| 59649 | for(i=1-nNew; i<nNew; i++){ |
| 59650 | int iPg = i<0 ? -i : i; |
| 59651 | assert( iPg>=0 && iPg<nNew ); |
| 59652 | if( abDone[iPg] ) continue; /* Skip pages already processed */ |
| 59653 | if( i>=0 /* On the upwards pass, or... */ |
| 59654 | || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */ |
| 59655 | ){ |
| 59656 | int iNew; |
| 59657 | int iOld; |
| 59658 | int nNewCell; |
| 59659 | |
| 59660 | /* Verify condition (1): If cells are moving left, update iPg |
| 59661 | ** only after iPg-1 has already been updated. */ |
| 59662 | assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] ); |
| 59663 | |
| 59664 | /* Verify condition (2): If cells are moving right, update iPg |
| 59665 | ** only after iPg+1 has already been updated. */ |
| 59666 | assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] ); |
| 59667 | |
| 59668 | if( iPg==0 ){ |
| 59669 | iNew = iOld = 0; |
| 59670 | nNewCell = cntNew[0]; |
| 59671 | }else{ |
| 59672 | iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : nCell; |
| 59673 | iNew = cntNew[iPg-1] + !leafData; |
| 59674 | nNewCell = cntNew[iPg] - iNew; |
| 59675 | } |
| 59676 | |
| 59677 | editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell); |
| 59678 | abDone[iPg]++; |
| 59679 | apNew[iPg]->nFree = usableSpace-szNew[iPg]; |
| 59680 | assert( apNew[iPg]->nOverflow==0 ); |
| 59681 | assert( apNew[iPg]->nCell==nNewCell ); |
| 59682 | } |
| 59683 | } |
| 59684 | |
| 59685 | /* All pages have been processed exactly once */ |
| 59686 | assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 ); |
| 59687 | |
| 59688 | assert( nOld>0 ); |
| 59689 | assert( nNew>0 ); |
| 59690 | |
| 59691 | if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ |
| 59692 | /* The root page of the b-tree now contains no cells. The only sibling |
| 59693 | ** page is the right-child of the parent. Copy the contents of the |
| 59694 | ** child page into the parent, decreasing the overall height of the |
| @@ -58957,130 +59697,54 @@ | |
| 59697 | ** |
| 59698 | ** If this is an auto-vacuum database, the call to copyNodeContent() |
| 59699 | ** sets all pointer-map entries corresponding to database image pages |
| 59700 | ** for which the pointer is stored within the content being copied. |
| 59701 | ** |
| 59702 | ** It is critical that the child page be defragmented before being |
| 59703 | ** copied into the parent, because if the parent is page 1 then it will |
| 59704 | ** by smaller than the child due to the database header, and so all the |
| 59705 | ** free space needs to be up front. |
| 59706 | */ |
| 59707 | assert( nNew==1 ); |
| 59708 | rc = defragmentPage(apNew[0]); |
| 59709 | testcase( rc!=SQLITE_OK ); |
| 59710 | assert( apNew[0]->nFree == |
| 59711 | (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) |
| 59712 | || rc!=SQLITE_OK |
| 59713 | ); |
| 59714 | copyNodeContent(apNew[0], pParent, &rc); |
| 59715 | freePage(apNew[0], &rc); |
| 59716 | }else if( ISAUTOVACUUM && !leafCorrection ){ |
| 59717 | /* Fix the pointer map entries associated with the right-child of each |
| 59718 | ** sibling page. All other pointer map entries have already been taken |
| 59719 | ** care of. */ |
| 59720 | for(i=0; i<nNew; i++){ |
| 59721 | u32 key = get4byte(&apNew[i]->aData[8]); |
| 59722 | ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); |
| 59723 | } |
| 59724 | } |
| 59725 | |
| 59726 | assert( pParent->isInit ); |
| 59727 | TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
| 59728 | nOld, nNew, nCell)); |
| 59729 | |
| 59730 | /* Free any old pages that were not reused as new pages. |
| 59731 | */ |
| 59732 | for(i=nNew; i<nOld; i++){ |
| 59733 | freePage(apOld[i], &rc); |
| 59734 | } |
| 59735 | |
| 59736 | #if 0 |
| 59737 | if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){ |
| 59738 | /* The ptrmapCheckPages() contains assert() statements that verify that |
| 59739 | ** all pointer map pages are set correctly. This is helpful while |
| 59740 | ** debugging. This is usually disabled because a corrupt database may |
| 59741 | ** cause an assert() statement to fail. */ |
| 59742 | ptrmapCheckPages(apNew, nNew); |
| 59743 | ptrmapCheckPages(&pParent, 1); |
| 59744 | } |
| 59745 | #endif |
| 59746 | |
| 59747 | /* |
| 59748 | ** Cleanup before returning. |
| 59749 | */ |
| 59750 | balance_cleanup: |
| @@ -60461,12 +61125,18 @@ | |
| 61125 | }else{ |
| 61126 | int contentOffset = get2byteNotZero(&data[hdr+5]); |
| 61127 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
| 61128 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 61129 | memset(hit, 1, contentOffset); |
| 61130 | /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
| 61131 | ** number of cells on the page. */ |
| 61132 | nCell = get2byte(&data[hdr+3]); |
| 61133 | /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page |
| 61134 | ** immediately follows the b-tree page header. */ |
| 61135 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 61136 | /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte |
| 61137 | ** integer offsets to the cell contents. */ |
| 61138 | for(i=0; i<nCell; i++){ |
| 61139 | int pc = get2byte(&data[cellStart+i*2]); |
| 61140 | u32 size = 65536; |
| 61141 | int j; |
| 61142 | if( pc<=usableSize-4 ){ |
| @@ -60478,18 +61148,27 @@ | |
| 61148 | "Corruption detected in cell %d on page %d",i,iPage); |
| 61149 | }else{ |
| 61150 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 61151 | } |
| 61152 | } |
| 61153 | /* EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header |
| 61154 | ** is the offset of the first freeblock, or zero if there are no |
| 61155 | ** freeblocks on the page. */ |
| 61156 | i = get2byte(&data[hdr+1]); |
| 61157 | while( i>0 ){ |
| 61158 | int size, j; |
| 61159 | assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 61160 | size = get2byte(&data[i+2]); |
| 61161 | assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */ |
| 61162 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 61163 | /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a |
| 61164 | ** big-endian integer which is the offset in the b-tree page of the next |
| 61165 | ** freeblock in the chain, or zero if the freeblock is the last on the |
| 61166 | ** chain. */ |
| 61167 | j = get2byte(&data[i]); |
| 61168 | /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of |
| 61169 | ** increasing offset. */ |
| 61170 | assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ |
| 61171 | assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 61172 | i = j; |
| 61173 | } |
| 61174 | for(i=cnt=0; i<usableSize; i++){ |
| @@ -60499,10 +61178,15 @@ | |
| 61178 | checkAppendMsg(pCheck, |
| 61179 | "Multiple uses for byte %d of page %d", i, iPage); |
| 61180 | break; |
| 61181 | } |
| 61182 | } |
| 61183 | /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments |
| 61184 | ** is stored in the fifth field of the b-tree page header. |
| 61185 | ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the |
| 61186 | ** number of fragmented free bytes within the cell content area. |
| 61187 | */ |
| 61188 | if( cnt!=data[hdr+7] ){ |
| 61189 | checkAppendMsg(pCheck, |
| 61190 | "Fragmentation of %d bytes reported as %d on page %d", |
| 61191 | cnt, data[hdr+7], iPage); |
| 61192 | } |
| @@ -60902,10 +61586,15 @@ | |
| 61586 | */ |
| 61587 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){ |
| 61588 | return (p->pBt->btsFlags & BTS_READ_ONLY)!=0; |
| 61589 | } |
| 61590 | |
| 61591 | /* |
| 61592 | ** Return the size of the header added to each page by this module. |
| 61593 | */ |
| 61594 | SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return sizeof(MemPage); } |
| 61595 | |
| 61596 | /************** End of btree.c ***********************************************/ |
| 61597 | /************** Begin file backup.c ******************************************/ |
| 61598 | /* |
| 61599 | ** 2009 January 28 |
| 61600 | ** |
| @@ -61025,10 +61714,24 @@ | |
| 61714 | static int setDestPgsz(sqlite3_backup *p){ |
| 61715 | int rc; |
| 61716 | rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0); |
| 61717 | return rc; |
| 61718 | } |
| 61719 | |
| 61720 | /* |
| 61721 | ** Check that there is no open read-transaction on the b-tree passed as the |
| 61722 | ** second argument. If there is not, return SQLITE_OK. Otherwise, if there |
| 61723 | ** is an open read-transaction, return SQLITE_ERROR and leave an error |
| 61724 | ** message in database handle db. |
| 61725 | */ |
| 61726 | static int checkReadTransaction(sqlite3 *db, Btree *p){ |
| 61727 | if( sqlite3BtreeIsInReadTrans(p) ){ |
| 61728 | sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use"); |
| 61729 | return SQLITE_ERROR; |
| 61730 | } |
| 61731 | return SQLITE_OK; |
| 61732 | } |
| 61733 | |
| 61734 | /* |
| 61735 | ** Create an sqlite3_backup process to copy the contents of zSrcDb from |
| 61736 | ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return |
| 61737 | ** a pointer to the new sqlite3_backup object. |
| @@ -61041,10 +61744,17 @@ | |
| 61744 | const char *zDestDb, /* Name of database within pDestDb */ |
| 61745 | sqlite3* pSrcDb, /* Database connection to read from */ |
| 61746 | const char *zSrcDb /* Name of database within pSrcDb */ |
| 61747 | ){ |
| 61748 | sqlite3_backup *p; /* Value to return */ |
| 61749 | |
| 61750 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 61751 | if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){ |
| 61752 | (void)SQLITE_MISUSE_BKPT; |
| 61753 | return 0; |
| 61754 | } |
| 61755 | #endif |
| 61756 | |
| 61757 | /* Lock the source database handle. The destination database |
| 61758 | ** handle is not locked in this routine, but it is locked in |
| 61759 | ** sqlite3_backup_step(). The user is required to ensure that no |
| 61760 | ** other thread accesses the destination handle for the duration |
| @@ -61078,16 +61788,19 @@ | |
| 61788 | p->pDestDb = pDestDb; |
| 61789 | p->pSrcDb = pSrcDb; |
| 61790 | p->iNext = 1; |
| 61791 | p->isAttached = 0; |
| 61792 | |
| 61793 | if( 0==p->pSrc || 0==p->pDest |
| 61794 | || setDestPgsz(p)==SQLITE_NOMEM |
| 61795 | || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK |
| 61796 | ){ |
| 61797 | /* One (or both) of the named databases did not exist or an OOM |
| 61798 | ** error was hit. Or there is a transaction open on the destination |
| 61799 | ** database. The error has already been written into the pDestDb |
| 61800 | ** handle. All that is left to do here is free the sqlite3_backup |
| 61801 | ** structure. */ |
| 61802 | sqlite3_free(p); |
| 61803 | p = 0; |
| 61804 | } |
| 61805 | } |
| 61806 | if( p ){ |
| @@ -61238,10 +61951,13 @@ | |
| 61951 | int rc; |
| 61952 | int destMode; /* Destination journal mode */ |
| 61953 | int pgszSrc = 0; /* Source page size */ |
| 61954 | int pgszDest = 0; /* Destination page size */ |
| 61955 | |
| 61956 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 61957 | if( p==0 ) return SQLITE_MISUSE_BKPT; |
| 61958 | #endif |
| 61959 | sqlite3_mutex_enter(p->pSrcDb->mutex); |
| 61960 | sqlite3BtreeEnter(p->pSrc); |
| 61961 | if( p->pDestDb ){ |
| 61962 | sqlite3_mutex_enter(p->pDestDb->mutex); |
| 61963 | } |
| @@ -61527,18 +62243,30 @@ | |
| 62243 | /* |
| 62244 | ** Return the number of pages still to be backed up as of the most recent |
| 62245 | ** call to sqlite3_backup_step(). |
| 62246 | */ |
| 62247 | SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){ |
| 62248 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 62249 | if( p==0 ){ |
| 62250 | (void)SQLITE_MISUSE_BKPT; |
| 62251 | return 0; |
| 62252 | } |
| 62253 | #endif |
| 62254 | return p->nRemaining; |
| 62255 | } |
| 62256 | |
| 62257 | /* |
| 62258 | ** Return the total number of pages in the source database as of the most |
| 62259 | ** recent call to sqlite3_backup_step(). |
| 62260 | */ |
| 62261 | SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){ |
| 62262 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 62263 | if( p==0 ){ |
| 62264 | (void)SQLITE_MISUSE_BKPT; |
| 62265 | return 0; |
| 62266 | } |
| 62267 | #endif |
| 62268 | return p->nPagecount; |
| 62269 | } |
| 62270 | |
| 62271 | /* |
| 62272 | ** This function is called after the contents of page iPage of the |
| @@ -63825,10 +64553,38 @@ | |
| 64553 | } |
| 64554 | p->nOp += nOp; |
| 64555 | } |
| 64556 | return addr; |
| 64557 | } |
| 64558 | |
| 64559 | #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 64560 | /* |
| 64561 | ** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus(). |
| 64562 | */ |
| 64563 | SQLITE_PRIVATE void sqlite3VdbeScanStatus( |
| 64564 | Vdbe *p, /* VM to add scanstatus() to */ |
| 64565 | int addrExplain, /* Address of OP_Explain (or 0) */ |
| 64566 | int addrLoop, /* Address of loop counter */ |
| 64567 | int addrVisit, /* Address of rows visited counter */ |
| 64568 | LogEst nEst, /* Estimated number of output rows */ |
| 64569 | const char *zName /* Name of table or index being scanned */ |
| 64570 | ){ |
| 64571 | int nByte = (p->nScan+1) * sizeof(ScanStatus); |
| 64572 | ScanStatus *aNew; |
| 64573 | aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte); |
| 64574 | if( aNew ){ |
| 64575 | ScanStatus *pNew = &aNew[p->nScan++]; |
| 64576 | pNew->addrExplain = addrExplain; |
| 64577 | pNew->addrLoop = addrLoop; |
| 64578 | pNew->addrVisit = addrVisit; |
| 64579 | pNew->nEst = nEst; |
| 64580 | pNew->zName = sqlite3DbStrDup(p->db, zName); |
| 64581 | p->aScan = aNew; |
| 64582 | } |
| 64583 | } |
| 64584 | #endif |
| 64585 | |
| 64586 | |
| 64587 | /* |
| 64588 | ** Change the value of the P1 operand for a specific instruction. |
| 64589 | ** This routine is useful when a large program is loaded from a |
| 64590 | ** static array using sqlite3VdbeAddOpList but we want to make a |
| @@ -64924,10 +65680,13 @@ | |
| 65680 | p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); |
| 65681 | p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); |
| 65682 | p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), |
| 65683 | &zCsr, zEnd, &nByte); |
| 65684 | p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte); |
| 65685 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 65686 | p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), &zCsr, zEnd, &nByte); |
| 65687 | #endif |
| 65688 | if( nByte ){ |
| 65689 | p->pFree = sqlite3DbMallocZero(db, nByte); |
| 65690 | } |
| 65691 | zCsr = p->pFree; |
| 65692 | zEnd = &zCsr[nByte]; |
| @@ -64991,10 +65750,13 @@ | |
| 65750 | ** is used, for example, when a trigger sub-program is halted to restore |
| 65751 | ** control to the main program. |
| 65752 | */ |
| 65753 | SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ |
| 65754 | Vdbe *v = pFrame->v; |
| 65755 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 65756 | v->anExec = pFrame->anExec; |
| 65757 | #endif |
| 65758 | v->aOnceFlag = pFrame->aOnceFlag; |
| 65759 | v->nOnceFlag = pFrame->nOnceFlag; |
| 65760 | v->aOp = pFrame->aOp; |
| 65761 | v->nOp = pFrame->nOp; |
| 65762 | v->aMem = pFrame->aMem; |
| @@ -65001,10 +65763,11 @@ | |
| 65763 | v->nMem = pFrame->nMem; |
| 65764 | v->apCsr = pFrame->apCsr; |
| 65765 | v->nCursor = pFrame->nCursor; |
| 65766 | v->db->lastRowid = pFrame->lastRowid; |
| 65767 | v->nChange = pFrame->nChange; |
| 65768 | v->db->nChange = pFrame->nDbChange; |
| 65769 | return pFrame->pc; |
| 65770 | } |
| 65771 | |
| 65772 | /* |
| 65773 | ** Close all cursors. |
| @@ -65568,10 +66331,11 @@ | |
| 66331 | ** so, abort any other statements this handle currently has active. |
| 66332 | */ |
| 66333 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 66334 | sqlite3CloseSavepoints(db); |
| 66335 | db->autoCommit = 1; |
| 66336 | p->nChange = 0; |
| 66337 | } |
| 66338 | } |
| 66339 | } |
| 66340 | |
| 66341 | /* Check for immediate foreign key violations. */ |
| @@ -65608,18 +66372,20 @@ | |
| 66372 | sqlite3VdbeLeave(p); |
| 66373 | return SQLITE_BUSY; |
| 66374 | }else if( rc!=SQLITE_OK ){ |
| 66375 | p->rc = rc; |
| 66376 | sqlite3RollbackAll(db, SQLITE_OK); |
| 66377 | p->nChange = 0; |
| 66378 | }else{ |
| 66379 | db->nDeferredCons = 0; |
| 66380 | db->nDeferredImmCons = 0; |
| 66381 | db->flags &= ~SQLITE_DeferFKs; |
| 66382 | sqlite3CommitInternalChanges(db); |
| 66383 | } |
| 66384 | }else{ |
| 66385 | sqlite3RollbackAll(db, SQLITE_OK); |
| 66386 | p->nChange = 0; |
| 66387 | } |
| 66388 | db->nStatement = 0; |
| 66389 | }else if( eStatementOp==0 ){ |
| 66390 | if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ |
| 66391 | eStatementOp = SAVEPOINT_RELEASE; |
| @@ -65627,10 +66393,11 @@ | |
| 66393 | eStatementOp = SAVEPOINT_ROLLBACK; |
| 66394 | }else{ |
| 66395 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 66396 | sqlite3CloseSavepoints(db); |
| 66397 | db->autoCommit = 1; |
| 66398 | p->nChange = 0; |
| 66399 | } |
| 66400 | } |
| 66401 | |
| 66402 | /* If eStatementOp is non-zero, then a statement transaction needs to |
| 66403 | ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to |
| @@ -65647,10 +66414,11 @@ | |
| 66414 | p->zErrMsg = 0; |
| 66415 | } |
| 66416 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 66417 | sqlite3CloseSavepoints(db); |
| 66418 | db->autoCommit = 1; |
| 66419 | p->nChange = 0; |
| 66420 | } |
| 66421 | } |
| 66422 | |
| 66423 | /* If this was an INSERT, UPDATE or DELETE and no statement transaction |
| 66424 | ** has been rolled back, update the database connection change-counter. |
| @@ -65908,10 +66676,16 @@ | |
| 66676 | for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); |
| 66677 | vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 66678 | sqlite3DbFree(db, p->aColName); |
| 66679 | sqlite3DbFree(db, p->zSql); |
| 66680 | sqlite3DbFree(db, p->pFree); |
| 66681 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 66682 | for(i=0; i<p->nScan; i++){ |
| 66683 | sqlite3DbFree(db, p->aScan[i].zName); |
| 66684 | } |
| 66685 | sqlite3DbFree(db, p->aScan); |
| 66686 | #endif |
| 66687 | } |
| 66688 | |
| 66689 | /* |
| 66690 | ** Delete an entire VDBE. |
| 66691 | */ |
| @@ -66066,13 +66840,11 @@ | |
| 66840 | /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
| 66841 | # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) |
| 66842 | i64 i = pMem->u.i; |
| 66843 | u64 u; |
| 66844 | if( i<0 ){ |
| 66845 | u = ~i; |
| 66846 | }else{ |
| 66847 | u = i; |
| 66848 | } |
| 66849 | if( u<=127 ){ |
| 66850 | return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1; |
| @@ -66234,14 +67006,18 @@ | |
| 67006 | ){ |
| 67007 | u64 x = FOUR_BYTE_UINT(buf); |
| 67008 | u32 y = FOUR_BYTE_UINT(buf+4); |
| 67009 | x = (x<<32) + y; |
| 67010 | if( serial_type==6 ){ |
| 67011 | /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit |
| 67012 | ** twos-complement integer. */ |
| 67013 | pMem->u.i = *(i64*)&x; |
| 67014 | pMem->flags = MEM_Int; |
| 67015 | testcase( pMem->u.i<0 ); |
| 67016 | }else{ |
| 67017 | /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit |
| 67018 | ** floating point number. */ |
| 67019 | #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) |
| 67020 | /* Verify that integers and floating point values use the same |
| 67021 | ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is |
| 67022 | ** defined that 64-bit floating point values really are mixed |
| 67023 | ** endian. |
| @@ -66265,39 +67041,50 @@ | |
| 67041 | Mem *pMem /* Memory cell to write value into */ |
| 67042 | ){ |
| 67043 | switch( serial_type ){ |
| 67044 | case 10: /* Reserved for future use */ |
| 67045 | case 11: /* Reserved for future use */ |
| 67046 | case 0: { /* Null */ |
| 67047 | /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */ |
| 67048 | pMem->flags = MEM_Null; |
| 67049 | break; |
| 67050 | } |
| 67051 | case 1: { |
| 67052 | /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement |
| 67053 | ** integer. */ |
| 67054 | pMem->u.i = ONE_BYTE_INT(buf); |
| 67055 | pMem->flags = MEM_Int; |
| 67056 | testcase( pMem->u.i<0 ); |
| 67057 | return 1; |
| 67058 | } |
| 67059 | case 2: { /* 2-byte signed integer */ |
| 67060 | /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit |
| 67061 | ** twos-complement integer. */ |
| 67062 | pMem->u.i = TWO_BYTE_INT(buf); |
| 67063 | pMem->flags = MEM_Int; |
| 67064 | testcase( pMem->u.i<0 ); |
| 67065 | return 2; |
| 67066 | } |
| 67067 | case 3: { /* 3-byte signed integer */ |
| 67068 | /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit |
| 67069 | ** twos-complement integer. */ |
| 67070 | pMem->u.i = THREE_BYTE_INT(buf); |
| 67071 | pMem->flags = MEM_Int; |
| 67072 | testcase( pMem->u.i<0 ); |
| 67073 | return 3; |
| 67074 | } |
| 67075 | case 4: { /* 4-byte signed integer */ |
| 67076 | /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit |
| 67077 | ** twos-complement integer. */ |
| 67078 | pMem->u.i = FOUR_BYTE_INT(buf); |
| 67079 | pMem->flags = MEM_Int; |
| 67080 | testcase( pMem->u.i<0 ); |
| 67081 | return 4; |
| 67082 | } |
| 67083 | case 5: { /* 6-byte signed integer */ |
| 67084 | /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit |
| 67085 | ** twos-complement integer. */ |
| 67086 | pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); |
| 67087 | pMem->flags = MEM_Int; |
| 67088 | testcase( pMem->u.i<0 ); |
| 67089 | return 6; |
| 67090 | } |
| @@ -66307,15 +67094,21 @@ | |
| 67094 | ** to avoid having to move the frame pointer in the common case */ |
| 67095 | return serialGet(buf,serial_type,pMem); |
| 67096 | } |
| 67097 | case 8: /* Integer 0 */ |
| 67098 | case 9: { /* Integer 1 */ |
| 67099 | /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */ |
| 67100 | /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */ |
| 67101 | pMem->u.i = serial_type-8; |
| 67102 | pMem->flags = MEM_Int; |
| 67103 | return 0; |
| 67104 | } |
| 67105 | default: { |
| 67106 | /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in |
| 67107 | ** length. |
| 67108 | ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and |
| 67109 | ** (N-13)/2 bytes in length. */ |
| 67110 | static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; |
| 67111 | pMem->z = (char *)buf; |
| 67112 | pMem->n = (serial_type-12)/2; |
| 67113 | pMem->flags = aFlag[serial_type&1]; |
| 67114 | return pMem->n; |
| @@ -68275,15 +69068,23 @@ | |
| 69068 | sqlite3_stmt *pStmt, |
| 69069 | int N, |
| 69070 | const void *(*xFunc)(Mem*), |
| 69071 | int useType |
| 69072 | ){ |
| 69073 | const void *ret; |
| 69074 | Vdbe *p; |
| 69075 | int n; |
| 69076 | sqlite3 *db; |
| 69077 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 69078 | if( pStmt==0 ){ |
| 69079 | (void)SQLITE_MISUSE_BKPT; |
| 69080 | return 0; |
| 69081 | } |
| 69082 | #endif |
| 69083 | ret = 0; |
| 69084 | p = (Vdbe *)pStmt; |
| 69085 | db = p->db; |
| 69086 | assert( db!=0 ); |
| 69087 | n = sqlite3_column_count(pStmt); |
| 69088 | if( N<n && N>=0 ){ |
| 69089 | N += useType*n; |
| 69090 | sqlite3_mutex_enter(db->mutex); |
| @@ -68744,10 +69545,16 @@ | |
| 69545 | ** prepared statement for the database connection. Return NULL if there |
| 69546 | ** are no more. |
| 69547 | */ |
| 69548 | SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ |
| 69549 | sqlite3_stmt *pNext; |
| 69550 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 69551 | if( !sqlite3SafetyCheckOk(pDb) ){ |
| 69552 | (void)SQLITE_MISUSE_BKPT; |
| 69553 | return 0; |
| 69554 | } |
| 69555 | #endif |
| 69556 | sqlite3_mutex_enter(pDb->mutex); |
| 69557 | if( pStmt==0 ){ |
| 69558 | pNext = (sqlite3_stmt*)pDb->pVdbe; |
| 69559 | }else{ |
| 69560 | pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; |
| @@ -68759,15 +69566,91 @@ | |
| 69566 | /* |
| 69567 | ** Return the value of a status counter for a prepared statement |
| 69568 | */ |
| 69569 | SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ |
| 69570 | Vdbe *pVdbe = (Vdbe*)pStmt; |
| 69571 | u32 v; |
| 69572 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 69573 | if( !pStmt ){ |
| 69574 | (void)SQLITE_MISUSE_BKPT; |
| 69575 | return 0; |
| 69576 | } |
| 69577 | #endif |
| 69578 | v = pVdbe->aCounter[op]; |
| 69579 | if( resetFlag ) pVdbe->aCounter[op] = 0; |
| 69580 | return (int)v; |
| 69581 | } |
| 69582 | |
| 69583 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 69584 | /* |
| 69585 | ** Return status data for a single loop within query pStmt. |
| 69586 | */ |
| 69587 | SQLITE_API int sqlite3_stmt_scanstatus( |
| 69588 | sqlite3_stmt *pStmt, /* Prepared statement being queried */ |
| 69589 | int idx, /* Index of loop to report on */ |
| 69590 | int iScanStatusOp, /* Which metric to return */ |
| 69591 | void *pOut /* OUT: Write the answer here */ |
| 69592 | ){ |
| 69593 | Vdbe *p = (Vdbe*)pStmt; |
| 69594 | ScanStatus *pScan; |
| 69595 | if( idx<0 || idx>=p->nScan ) return 1; |
| 69596 | pScan = &p->aScan[idx]; |
| 69597 | switch( iScanStatusOp ){ |
| 69598 | case SQLITE_SCANSTAT_NLOOP: { |
| 69599 | *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; |
| 69600 | break; |
| 69601 | } |
| 69602 | case SQLITE_SCANSTAT_NVISIT: { |
| 69603 | *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; |
| 69604 | break; |
| 69605 | } |
| 69606 | case SQLITE_SCANSTAT_EST: { |
| 69607 | double r = 1.0; |
| 69608 | LogEst x = pScan->nEst; |
| 69609 | while( x<100 ){ |
| 69610 | x += 10; |
| 69611 | r *= 0.5; |
| 69612 | } |
| 69613 | *(double*)pOut = r*sqlite3LogEstToInt(x); |
| 69614 | break; |
| 69615 | } |
| 69616 | case SQLITE_SCANSTAT_NAME: { |
| 69617 | *(const char**)pOut = pScan->zName; |
| 69618 | break; |
| 69619 | } |
| 69620 | case SQLITE_SCANSTAT_EXPLAIN: { |
| 69621 | if( pScan->addrExplain ){ |
| 69622 | *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; |
| 69623 | }else{ |
| 69624 | *(const char**)pOut = 0; |
| 69625 | } |
| 69626 | break; |
| 69627 | } |
| 69628 | case SQLITE_SCANSTAT_SELECTID: { |
| 69629 | if( pScan->addrExplain ){ |
| 69630 | *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; |
| 69631 | }else{ |
| 69632 | *(int*)pOut = -1; |
| 69633 | } |
| 69634 | break; |
| 69635 | } |
| 69636 | default: { |
| 69637 | return 1; |
| 69638 | } |
| 69639 | } |
| 69640 | return 0; |
| 69641 | } |
| 69642 | |
| 69643 | /* |
| 69644 | ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. |
| 69645 | */ |
| 69646 | SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ |
| 69647 | Vdbe *p = (Vdbe*)pStmt; |
| 69648 | memset(p->anExec, 0, p->nOp * sizeof(i64)); |
| 69649 | } |
| 69650 | #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ |
| 69651 | |
| 69652 | /************** End of vdbeapi.c *********************************************/ |
| 69653 | /************** Begin file vdbetrace.c ***************************************/ |
| 69654 | /* |
| 69655 | ** 2009 November 25 |
| 69656 | ** |
| @@ -69649,10 +70532,13 @@ | |
| 70532 | #ifdef VDBE_PROFILE |
| 70533 | start = sqlite3Hwtime(); |
| 70534 | #endif |
| 70535 | nVmStep++; |
| 70536 | pOp = &aOp[pc]; |
| 70537 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 70538 | if( p->anExec ) p->anExec[pc]++; |
| 70539 | #endif |
| 70540 | |
| 70541 | /* Only allow tracing if SQLITE_DEBUG is defined. |
| 70542 | */ |
| 70543 | #ifdef SQLITE_DEBUG |
| 70544 | if( db->flags & SQLITE_VdbeTrace ){ |
| @@ -71674,11 +72560,14 @@ | |
| 72560 | testcase( serial_type==127 ); |
| 72561 | testcase( serial_type==128 ); |
| 72562 | nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); |
| 72563 | }while( (--pRec)>=pData0 ); |
| 72564 | |
| 72565 | /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint |
| 72566 | ** which determines the total number of bytes in the header. The varint |
| 72567 | ** value is the size of the header in bytes including the size varint |
| 72568 | ** itself. */ |
| 72569 | testcase( nHdr==126 ); |
| 72570 | testcase( nHdr==127 ); |
| 72571 | if( nHdr<=126 ){ |
| 72572 | /* The common case */ |
| 72573 | nHdr += 1; |
| @@ -71708,11 +72597,15 @@ | |
| 72597 | j = nHdr; |
| 72598 | assert( pData0<=pLast ); |
| 72599 | pRec = pData0; |
| 72600 | do{ |
| 72601 | serial_type = pRec->uTemp; |
| 72602 | /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more |
| 72603 | ** additional varints, one per column. */ |
| 72604 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
| 72605 | /* EVIDENCE-OF: R-64536-51728 The values for each column in the record |
| 72606 | ** immediately follow the header. */ |
| 72607 | j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ |
| 72608 | }while( (++pRec)<=pLast ); |
| 72609 | assert( i==nHdr ); |
| 72610 | assert( j==nByte ); |
| 72611 | |
| @@ -72843,14 +73736,14 @@ | |
| 73736 | } |
| 73737 | pIdxKey = &r; |
| 73738 | }else{ |
| 73739 | pIdxKey = sqlite3VdbeAllocUnpackedRecord( |
| 73740 | pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree |
| 73741 | ); |
| 73742 | if( pIdxKey==0 ) goto no_mem; |
| 73743 | assert( pIn3->flags & MEM_Blob ); |
| 73744 | ExpandBlob(pIn3); |
| 73745 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
| 73746 | } |
| 73747 | pIdxKey->default_rc = 0; |
| 73748 | if( pOp->opcode==OP_NoConflict ){ |
| 73749 | /* For the OP_NoConflict opcode, take the jump if any of the |
| @@ -73540,13 +74433,13 @@ | |
| 74433 | } |
| 74434 | /* Opcode: Rewind P1 P2 * * * |
| 74435 | ** |
| 74436 | ** The next use of the Rowid or Column or Next instruction for P1 |
| 74437 | ** will refer to the first entry in the database table or index. |
| 74438 | ** If the table or index is empty, jump immediately to P2. |
| 74439 | ** If the table or index is not empty, fall through to the following |
| 74440 | ** instruction. |
| 74441 | ** |
| 74442 | ** This opcode leaves the cursor configured to move in forward order, |
| 74443 | ** from the beginning toward the end. In other words, the cursor is |
| 74444 | ** configured to use Next, not Prev. |
| 74445 | */ |
| @@ -74458,10 +75351,13 @@ | |
| 75351 | pFrame->aOp = p->aOp; |
| 75352 | pFrame->nOp = p->nOp; |
| 75353 | pFrame->token = pProgram->token; |
| 75354 | pFrame->aOnceFlag = p->aOnceFlag; |
| 75355 | pFrame->nOnceFlag = p->nOnceFlag; |
| 75356 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 75357 | pFrame->anExec = p->anExec; |
| 75358 | #endif |
| 75359 | |
| 75360 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 75361 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
| 75362 | pMem->flags = MEM_Undefined; |
| 75363 | pMem->db = db; |
| @@ -74475,10 +75371,11 @@ | |
| 75371 | |
| 75372 | p->nFrame++; |
| 75373 | pFrame->pParent = p->pFrame; |
| 75374 | pFrame->lastRowid = lastRowid; |
| 75375 | pFrame->nChange = p->nChange; |
| 75376 | pFrame->nDbChange = p->db->nChange; |
| 75377 | p->nChange = 0; |
| 75378 | p->pFrame = pFrame; |
| 75379 | p->aMem = aMem = &VdbeFrameMem(pFrame)[-1]; |
| 75380 | p->nMem = pFrame->nChildMem; |
| 75381 | p->nCursor = (u16)pFrame->nChildCsr; |
| @@ -74485,10 +75382,13 @@ | |
| 75382 | p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; |
| 75383 | p->aOp = aOp = pProgram->aOp; |
| 75384 | p->nOp = pProgram->nOp; |
| 75385 | p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor]; |
| 75386 | p->nOnceFlag = pProgram->nOnce; |
| 75387 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 75388 | p->anExec = 0; |
| 75389 | #endif |
| 75390 | pc = -1; |
| 75391 | memset(p->aOnceFlag, 0, p->nOnceFlag); |
| 75392 | |
| 75393 | break; |
| 75394 | } |
| @@ -75673,10 +76573,15 @@ | |
| 76573 | char *zErr = 0; |
| 76574 | Table *pTab; |
| 76575 | Parse *pParse = 0; |
| 76576 | Incrblob *pBlob = 0; |
| 76577 | |
| 76578 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 76579 | if( !sqlite3SafetyCheckOk(db) || ppBlob==0 || zTable==0 ){ |
| 76580 | return SQLITE_MISUSE_BKPT; |
| 76581 | } |
| 76582 | #endif |
| 76583 | flags = !!flags; /* flags = (flags ? 1 : 0); */ |
| 76584 | *ppBlob = 0; |
| 76585 | |
| 76586 | sqlite3_mutex_enter(db->mutex); |
| 76587 | |
| @@ -75891,11 +76796,10 @@ | |
| 76796 | v = (Vdbe*)p->pStmt; |
| 76797 | |
| 76798 | if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){ |
| 76799 | /* Request is out of range. Return a transient error. */ |
| 76800 | rc = SQLITE_ERROR; |
| 76801 | }else if( v==0 ){ |
| 76802 | /* If there is no statement handle, then the blob-handle has |
| 76803 | ** already been invalidated. Return SQLITE_ABORT in this case. |
| 76804 | */ |
| 76805 | rc = SQLITE_ABORT; |
| @@ -75909,14 +76813,14 @@ | |
| 76813 | sqlite3BtreeLeaveCursor(p->pCsr); |
| 76814 | if( rc==SQLITE_ABORT ){ |
| 76815 | sqlite3VdbeFinalize(v); |
| 76816 | p->pStmt = 0; |
| 76817 | }else{ |
| 76818 | v->rc = rc; |
| 76819 | } |
| 76820 | } |
| 76821 | sqlite3Error(db, rc); |
| 76822 | rc = sqlite3ApiExit(db, rc); |
| 76823 | sqlite3_mutex_leave(db->mutex); |
| 76824 | return rc; |
| 76825 | } |
| 76826 | |
| @@ -76089,11 +76993,11 @@ | |
| 76993 | ** itself. |
| 76994 | ** |
| 76995 | ** The sorter is running in multi-threaded mode if (a) the library was built |
| 76996 | ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater |
| 76997 | ** than zero, and (b) worker threads have been enabled at runtime by calling |
| 76998 | ** "PRAGMA threads=N" with some value of N greater than 0. |
| 76999 | ** |
| 77000 | ** When Rewind() is called, any data remaining in memory is flushed to a |
| 77001 | ** final PMA. So at this point the data is stored in some number of sorted |
| 77002 | ** PMAs within temporary files on disk. |
| 77003 | ** |
| @@ -76134,10 +77038,17 @@ | |
| 77038 | */ |
| 77039 | #if 0 |
| 77040 | # define SQLITE_DEBUG_SORTER_THREADS 1 |
| 77041 | #endif |
| 77042 | |
| 77043 | /* |
| 77044 | ** Hard-coded maximum amount of data to accumulate in memory before flushing |
| 77045 | ** to a level 0 PMA. The purpose of this limit is to prevent various integer |
| 77046 | ** overflows. 512MiB. |
| 77047 | */ |
| 77048 | #define SQLITE_MAX_MXPMASIZE (1<<29) |
| 77049 | |
| 77050 | /* |
| 77051 | ** Private objects used by the sorter |
| 77052 | */ |
| 77053 | typedef struct MergeEngine MergeEngine; /* Merge PMAs together */ |
| 77054 | typedef struct PmaReader PmaReader; /* Incrementally read one PMA */ |
| @@ -76832,17 +77743,15 @@ | |
| 77743 | |
| 77744 | if( !sqlite3TempInMemory(db) ){ |
| 77745 | pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz; |
| 77746 | mxCache = db->aDb[0].pSchema->cache_size; |
| 77747 | if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING; |
| 77748 | pSorter->mxPmaSize = MIN((i64)mxCache*pgsz, SQLITE_MAX_MXPMASIZE); |
| 77749 | |
| 77750 | /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of |
| 77751 | ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary |
| 77752 | ** large heap allocations. |
| 77753 | */ |
| 77754 | if( sqlite3GlobalConfig.pScratch==0 ){ |
| 77755 | assert( pSorter->iMemory==0 ); |
| 77756 | pSorter->nMemory = pgsz; |
| 77757 | pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz); |
| @@ -79210,19 +80119,19 @@ | |
| 80119 | ** |
| 80120 | ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) |
| 80121 | ** is a helper function - a callback for the tree walker. |
| 80122 | */ |
| 80123 | static int incrAggDepth(Walker *pWalker, Expr *pExpr){ |
| 80124 | if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n; |
| 80125 | return WRC_Continue; |
| 80126 | } |
| 80127 | static void incrAggFunctionDepth(Expr *pExpr, int N){ |
| 80128 | if( N>0 ){ |
| 80129 | Walker w; |
| 80130 | memset(&w, 0, sizeof(w)); |
| 80131 | w.xExprCallback = incrAggDepth; |
| 80132 | w.u.n = N; |
| 80133 | sqlite3WalkExpr(&w, pExpr); |
| 80134 | } |
| 80135 | } |
| 80136 | |
| 80137 | /* |
| @@ -79766,11 +80675,11 @@ | |
| 80675 | double r = -1.0; |
| 80676 | if( p->op!=TK_FLOAT ) return -1; |
| 80677 | sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8); |
| 80678 | assert( r>=0.0 ); |
| 80679 | if( r>1.0 ) return -1; |
| 80680 | return (int)(r*134217728.0); |
| 80681 | } |
| 80682 | |
| 80683 | /* |
| 80684 | ** This routine is callback for sqlite3WalkExpr(). |
| 80685 | ** |
| @@ -79898,11 +80807,11 @@ | |
| 80807 | ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand for |
| 80808 | ** likelihood(X,0.9375). |
| 80809 | ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to |
| 80810 | ** likelihood(X,0.9375). */ |
| 80811 | /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */ |
| 80812 | pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120; |
| 80813 | } |
| 80814 | } |
| 80815 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 80816 | auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); |
| 80817 | if( auth!=SQLITE_OK ){ |
| @@ -81855,69 +82764,79 @@ | |
| 82764 | sqlite3DbFree(db, pList->a); |
| 82765 | sqlite3DbFree(db, pList); |
| 82766 | } |
| 82767 | |
| 82768 | /* |
| 82769 | ** These routines are Walker callbacks used to check expressions to |
| 82770 | ** see if they are "constant" for some definition of constant. The |
| 82771 | ** Walker.eCode value determines the type of "constant" we are looking |
| 82772 | ** for. |
| 82773 | ** |
| 82774 | ** These callback routines are used to implement the following: |
| 82775 | ** |
| 82776 | ** sqlite3ExprIsConstant() pWalker->eCode==1 |
| 82777 | ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 |
| 82778 | ** sqlite3ExprRefOneTableOnly() pWalker->eCode==3 |
| 82779 | ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 |
| 82780 | ** |
| 82781 | ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression |
| 82782 | ** is found to not be a constant. |
| 82783 | ** |
| 82784 | ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions |
| 82785 | ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing |
| 82786 | ** an existing schema and 4 when processing a new statement. A bound |
| 82787 | ** parameter raises an error for new statements, but is silently converted |
| 82788 | ** to NULL for existing schemas. This allows sqlite_master tables that |
| 82789 | ** contain a bound parameter because they were generated by older versions |
| 82790 | ** of SQLite to be parsed by newer versions of SQLite without raising a |
| 82791 | ** malformed schema error. |
| 82792 | */ |
| 82793 | static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ |
| 82794 | |
| 82795 | /* If pWalker->eCode is 2 then any term of the expression that comes from |
| 82796 | ** the ON or USING clauses of a left join disqualifies the expression |
| 82797 | ** from being considered constant. */ |
| 82798 | if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 82799 | pWalker->eCode = 0; |
| 82800 | return WRC_Abort; |
| 82801 | } |
| 82802 | |
| 82803 | switch( pExpr->op ){ |
| 82804 | /* Consider functions to be constant if all their arguments are constant |
| 82805 | ** and either pWalker->eCode==4 or 5 or the function has the |
| 82806 | ** SQLITE_FUNC_CONST flag. */ |
| 82807 | case TK_FUNCTION: |
| 82808 | if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_Constant) ){ |
| 82809 | return WRC_Continue; |
| 82810 | }else{ |
| 82811 | pWalker->eCode = 0; |
| 82812 | return WRC_Abort; |
| 82813 | } |
| 82814 | case TK_ID: |
| 82815 | case TK_COLUMN: |
| 82816 | case TK_AGG_FUNCTION: |
| 82817 | case TK_AGG_COLUMN: |
| 82818 | testcase( pExpr->op==TK_ID ); |
| 82819 | testcase( pExpr->op==TK_COLUMN ); |
| 82820 | testcase( pExpr->op==TK_AGG_FUNCTION ); |
| 82821 | testcase( pExpr->op==TK_AGG_COLUMN ); |
| 82822 | if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ |
| 82823 | return WRC_Continue; |
| 82824 | }else{ |
| 82825 | pWalker->eCode = 0; |
| 82826 | return WRC_Abort; |
| 82827 | } |
| 82828 | case TK_VARIABLE: |
| 82829 | if( pWalker->eCode==5 ){ |
| 82830 | /* Silently convert bound parameters that appear inside of CREATE |
| 82831 | ** statements into a NULL when parsing the CREATE statement text out |
| 82832 | ** of the sqlite_master table */ |
| 82833 | pExpr->op = TK_NULL; |
| 82834 | }else if( pWalker->eCode==4 ){ |
| 82835 | /* A bound parameter in a CREATE statement that originates from |
| 82836 | ** sqlite3_prepare() causes an error */ |
| 82837 | pWalker->eCode = 0; |
| 82838 | return WRC_Abort; |
| 82839 | } |
| 82840 | /* Fall through */ |
| 82841 | default: |
| 82842 | testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ |
| @@ -81925,57 +82844,68 @@ | |
| 82844 | return WRC_Continue; |
| 82845 | } |
| 82846 | } |
| 82847 | static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ |
| 82848 | UNUSED_PARAMETER(NotUsed); |
| 82849 | pWalker->eCode = 0; |
| 82850 | return WRC_Abort; |
| 82851 | } |
| 82852 | static int exprIsConst(Expr *p, int initFlag, int iCur){ |
| 82853 | Walker w; |
| 82854 | memset(&w, 0, sizeof(w)); |
| 82855 | w.eCode = initFlag; |
| 82856 | w.xExprCallback = exprNodeIsConstant; |
| 82857 | w.xSelectCallback = selectNodeIsConstant; |
| 82858 | w.u.iCur = iCur; |
| 82859 | sqlite3WalkExpr(&w, p); |
| 82860 | return w.eCode; |
| 82861 | } |
| 82862 | |
| 82863 | /* |
| 82864 | ** Walk an expression tree. Return non-zero if the expression is constant |
| 82865 | ** and 0 if it involves variables or function calls. |
| 82866 | ** |
| 82867 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 82868 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 82869 | ** a constant. |
| 82870 | */ |
| 82871 | SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){ |
| 82872 | return exprIsConst(p, 1, 0); |
| 82873 | } |
| 82874 | |
| 82875 | /* |
| 82876 | ** Walk an expression tree. Return non-zero if the expression is constant |
| 82877 | ** that does no originate from the ON or USING clauses of a join. |
| 82878 | ** Return 0 if it involves variables or function calls or terms from |
| 82879 | ** an ON or USING clause. |
| 82880 | */ |
| 82881 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){ |
| 82882 | return exprIsConst(p, 2, 0); |
| 82883 | } |
| 82884 | |
| 82885 | /* |
| 82886 | ** Walk an expression tree. Return non-zero if the expression constant |
| 82887 | ** for any single row of the table with cursor iCur. In other words, the |
| 82888 | ** expression must not refer to any non-deterministic function nor any |
| 82889 | ** table other than iCur. |
| 82890 | */ |
| 82891 | SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){ |
| 82892 | return exprIsConst(p, 3, iCur); |
| 82893 | } |
| 82894 | |
| 82895 | /* |
| 82896 | ** Walk an expression tree. Return non-zero if the expression is constant |
| 82897 | ** or a function call with constant arguments. Return and 0 if there |
| 82898 | ** are any variables. |
| 82899 | ** |
| 82900 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 82901 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 82902 | ** a constant. |
| 82903 | */ |
| 82904 | SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ |
| 82905 | assert( isInit==0 || isInit==1 ); |
| 82906 | return exprIsConst(p, 4+isInit, 0); |
| 82907 | } |
| 82908 | |
| 82909 | /* |
| 82910 | ** If the expression p codes a constant integer that is small enough |
| 82911 | ** to fit in a 32-bit integer, return 1 and put the value of the integer |
| @@ -83627,11 +84557,14 @@ | |
| 84557 | target |
| 84558 | )); |
| 84559 | |
| 84560 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 84561 | /* If the column has REAL affinity, it may currently be stored as an |
| 84562 | ** integer. Use OP_RealAffinity to make sure it is really real. |
| 84563 | ** |
| 84564 | ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to |
| 84565 | ** floating point when extracting it from the record. */ |
| 84566 | if( pExpr->iColumn>=0 |
| 84567 | && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL |
| 84568 | ){ |
| 84569 | sqlite3VdbeAddOp1(v, OP_RealAffinity, target); |
| 84570 | } |
| @@ -87279,27 +88212,32 @@ | |
| 88212 | aLog[i] = sqlite3LogEst(v); |
| 88213 | #endif |
| 88214 | if( *z==' ' ) z++; |
| 88215 | } |
| 88216 | #ifndef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 88217 | assert( pIndex!=0 ); { |
| 88218 | #else |
| 88219 | if( pIndex ){ |
| 88220 | #endif |
| 88221 | pIndex->bUnordered = 0; |
| 88222 | pIndex->noSkipScan = 0; |
| 88223 | while( z[0] ){ |
| 88224 | if( sqlite3_strglob("unordered*", z)==0 ){ |
| 88225 | pIndex->bUnordered = 1; |
| 88226 | }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){ |
| 88227 | pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3)); |
| 88228 | }else if( sqlite3_strglob("noskipscan*", z)==0 ){ |
| 88229 | pIndex->noSkipScan = 1; |
| 88230 | } |
| 88231 | #ifdef SQLITE_ENABLE_COSTMULT |
| 88232 | else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){ |
| 88233 | pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9)); |
| 88234 | } |
| 88235 | #endif |
| 88236 | while( z[0]!=0 && z[0]!=' ' ) z++; |
| 88237 | while( z[0]==' ' ) z++; |
| 88238 | } |
| 88239 | } |
| 88240 | } |
| 88241 | |
| 88242 | /* |
| 88243 | ** This callback is invoked once for each index when reading the |
| @@ -87421,10 +88359,11 @@ | |
| 88359 | nSample--; |
| 88360 | }else{ |
| 88361 | nRow = pIdx->aiRowEst[0]; |
| 88362 | nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1]; |
| 88363 | } |
| 88364 | pIdx->nRowEst0 = nRow; |
| 88365 | |
| 88366 | /* Set nSum to the number of distinct (iCol+1) field prefixes that |
| 88367 | ** occur in the stat4 table for this index. Set sumEq to the sum of |
| 88368 | ** the nEq values for column iCol for the same set (adding the value |
| 88369 | ** only once where there exist duplicate prefixes). */ |
| @@ -87682,11 +88621,11 @@ | |
| 88621 | } |
| 88622 | |
| 88623 | |
| 88624 | /* Load the statistics from the sqlite_stat4 table. */ |
| 88625 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 88626 | if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){ |
| 88627 | int lookasideEnabled = db->lookaside.bEnabled; |
| 88628 | db->lookaside.bEnabled = 0; |
| 88629 | rc = loadStat4(db, sInfo.zDatabase); |
| 88630 | db->lookaside.bEnabled = lookasideEnabled; |
| 88631 | } |
| @@ -88364,10 +89303,13 @@ | |
| 89303 | SQLITE_API int sqlite3_set_authorizer( |
| 89304 | sqlite3 *db, |
| 89305 | int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), |
| 89306 | void *pArg |
| 89307 | ){ |
| 89308 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 89309 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 89310 | #endif |
| 89311 | sqlite3_mutex_enter(db->mutex); |
| 89312 | db->xAuth = (sqlite3_xauth)xAuth; |
| 89313 | db->pAuthArg = pArg; |
| 89314 | sqlite3ExpirePreparedStatements(db); |
| 89315 | sqlite3_mutex_leave(db->mutex); |
| @@ -88858,11 +89800,15 @@ | |
| 89800 | ** See also sqlite3LocateTable(). |
| 89801 | */ |
| 89802 | SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
| 89803 | Table *p = 0; |
| 89804 | int i; |
| 89805 | |
| 89806 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 89807 | if( !sqlite3SafetyCheckOk(db) || zName==0 ) return 0; |
| 89808 | #endif |
| 89809 | |
| 89810 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 89811 | assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
| 89812 | #if SQLITE_USER_AUTHENTICATION |
| 89813 | /* Only the admin user is allowed to know that the sqlite_user table |
| 89814 | ** exists */ |
| @@ -94322,12 +95268,12 @@ | |
| 95268 | break; |
| 95269 | } |
| 95270 | default: { |
| 95271 | /* Because sqlite3_value_double() returns 0.0 if the argument is not |
| 95272 | ** something that can be converted into a number, we have: |
| 95273 | ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob |
| 95274 | ** that cannot be converted to a numeric value. |
| 95275 | */ |
| 95276 | double rVal = sqlite3_value_double(argv[0]); |
| 95277 | if( rVal<0 ) rVal = -rVal; |
| 95278 | sqlite3_result_double(context, rVal); |
| 95279 | break; |
| @@ -103881,13 +104827,16 @@ | |
| 104827 | Vdbe *pOld, /* VM being reprepared */ |
| 104828 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 104829 | const char **pzTail /* OUT: End of parsed string */ |
| 104830 | ){ |
| 104831 | int rc; |
| 104832 | |
| 104833 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 104834 | if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 104835 | #endif |
| 104836 | *ppStmt = 0; |
| 104837 | if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ |
| 104838 | return SQLITE_MISUSE_BKPT; |
| 104839 | } |
| 104840 | sqlite3_mutex_enter(db->mutex); |
| 104841 | sqlite3BtreeEnterAll(db); |
| 104842 | rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); |
| @@ -103990,13 +104939,15 @@ | |
| 104939 | */ |
| 104940 | char *zSql8; |
| 104941 | const char *zTail8 = 0; |
| 104942 | int rc = SQLITE_OK; |
| 104943 | |
| 104944 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 104945 | if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 104946 | #endif |
| 104947 | *ppStmt = 0; |
| 104948 | if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ |
| 104949 | return SQLITE_MISUSE_BKPT; |
| 104950 | } |
| 104951 | if( nBytes>=0 ){ |
| 104952 | int sz; |
| 104953 | const char *z = (const char*)zSql; |
| @@ -109705,10 +110656,13 @@ | |
| 110656 | char **pzErrMsg /* Write error messages here */ |
| 110657 | ){ |
| 110658 | int rc; |
| 110659 | TabResult res; |
| 110660 | |
| 110661 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 110662 | if( pazResult==0 ) return SQLITE_MISUSE_BKPT; |
| 110663 | #endif |
| 110664 | *pazResult = 0; |
| 110665 | if( pnColumn ) *pnColumn = 0; |
| 110666 | if( pnRow ) *pnRow = 0; |
| 110667 | if( pzErrMsg ) *pzErrMsg = 0; |
| 110668 | res.zErrMsg = 0; |
| @@ -111768,11 +112722,11 @@ | |
| 112722 | ** Two writes per page are required in step (3) because the original |
| 112723 | ** database content must be written into the rollback journal prior to |
| 112724 | ** overwriting the database with the vacuumed content. |
| 112725 | ** |
| 112726 | ** Only 1x temporary space and only 1x writes would be required if |
| 112727 | ** the copy of step (3) were replaced by deleting the original database |
| 112728 | ** and renaming the transient database as the original. But that will |
| 112729 | ** not work if other processes are attached to the original database. |
| 112730 | ** And a power loss in between deleting the original and renaming the |
| 112731 | ** transient would cause the database file to appear to be deleted |
| 112732 | ** following reboot. |
| @@ -112126,10 +113080,13 @@ | |
| 113080 | sqlite3 *db, /* Database in which module is registered */ |
| 113081 | const char *zName, /* Name assigned to this module */ |
| 113082 | const sqlite3_module *pModule, /* The definition of the module */ |
| 113083 | void *pAux /* Context pointer for xCreate/xConnect */ |
| 113084 | ){ |
| 113085 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 113086 | if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 113087 | #endif |
| 113088 | return createModule(db, zName, pModule, pAux, 0); |
| 113089 | } |
| 113090 | |
| 113091 | /* |
| 113092 | ** External API function used to create a new virtual-table module. |
| @@ -112139,10 +113096,13 @@ | |
| 113096 | const char *zName, /* Name assigned to this module */ |
| 113097 | const sqlite3_module *pModule, /* The definition of the module */ |
| 113098 | void *pAux, /* Context pointer for xCreate/xConnect */ |
| 113099 | void (*xDestroy)(void *) /* Module destructor function */ |
| 113100 | ){ |
| 113101 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 113102 | if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 113103 | #endif |
| 113104 | return createModule(db, zName, pModule, pAux, xDestroy); |
| 113105 | } |
| 113106 | |
| 113107 | /* |
| 113108 | ** Lock the virtual table so that it cannot be disconnected. |
| @@ -112371,11 +113331,16 @@ | |
| 113331 | pTable->tabFlags |= TF_Virtual; |
| 113332 | pTable->nModuleArg = 0; |
| 113333 | addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); |
| 113334 | addModuleArgument(db, pTable, 0); |
| 113335 | addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); |
| 113336 | assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0) |
| 113337 | || (pParse->sNameToken.z==pName1->z && pName2->z==0) |
| 113338 | ); |
| 113339 | pParse->sNameToken.n = (int)( |
| 113340 | &pModuleName->z[pModuleName->n] - pParse->sNameToken.z |
| 113341 | ); |
| 113342 | |
| 113343 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 113344 | /* Creating a virtual table invokes the authorization callback twice. |
| 113345 | ** The first invocation, to obtain permission to INSERT a row into the |
| 113346 | ** sqlite_master table, has already been made by sqlite3StartTable(). |
| @@ -112743,10 +113708,13 @@ | |
| 113708 | |
| 113709 | int rc = SQLITE_OK; |
| 113710 | Table *pTab; |
| 113711 | char *zErr = 0; |
| 113712 | |
| 113713 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 113714 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 113715 | #endif |
| 113716 | sqlite3_mutex_enter(db->mutex); |
| 113717 | if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){ |
| 113718 | sqlite3Error(db, SQLITE_MISUSE); |
| 113719 | sqlite3_mutex_leave(db->mutex); |
| 113720 | return SQLITE_MISUSE_BKPT; |
| @@ -113099,10 +114067,13 @@ | |
| 114067 | */ |
| 114068 | SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){ |
| 114069 | static const unsigned char aMap[] = { |
| 114070 | SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE |
| 114071 | }; |
| 114072 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 114073 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 114074 | #endif |
| 114075 | assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 ); |
| 114076 | assert( OE_Ignore==4 && OE_Replace==5 ); |
| 114077 | assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 ); |
| 114078 | return (int)aMap[db->vtabOnConflict-1]; |
| 114079 | } |
| @@ -113114,12 +114085,14 @@ | |
| 114085 | */ |
| 114086 | SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){ |
| 114087 | va_list ap; |
| 114088 | int rc = SQLITE_OK; |
| 114089 | |
| 114090 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 114091 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 114092 | #endif |
| 114093 | sqlite3_mutex_enter(db->mutex); |
| 114094 | va_start(ap, op); |
| 114095 | switch( op ){ |
| 114096 | case SQLITE_VTAB_CONSTRAINT_SUPPORT: { |
| 114097 | VtabCtx *p = db->pVtabCtx; |
| 114098 | if( !p ){ |
| @@ -113250,10 +114223,13 @@ | |
| 114223 | } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
| 114224 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 114225 | } u; |
| 114226 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 114227 | Bitmask notReady; /* FROM entries not usable at this level */ |
| 114228 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 114229 | int addrVisit; /* Address at which row is visited */ |
| 114230 | #endif |
| 114231 | }; |
| 114232 | |
| 114233 | /* |
| 114234 | ** Each instance of this object represents an algorithm for evaluating one |
| 114235 | ** term of a join. Every term of the FROM clause will have at least |
| @@ -113280,11 +114256,10 @@ | |
| 114256 | LogEst rRun; /* Cost of running each loop */ |
| 114257 | LogEst nOut; /* Estimated number of output rows */ |
| 114258 | union { |
| 114259 | struct { /* Information for internal btree tables */ |
| 114260 | u16 nEq; /* Number of equality constraints */ |
| 114261 | Index *pIndex; /* Index used, or NULL */ |
| 114262 | } btree; |
| 114263 | struct { /* Information for virtual tables */ |
| 114264 | int idxNum; /* Index number */ |
| 114265 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| @@ -113293,16 +114268,17 @@ | |
| 114268 | char *idxStr; /* Index identifier string */ |
| 114269 | } vtab; |
| 114270 | } u; |
| 114271 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 114272 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 114273 | u16 nSkip; /* Number of NULL aLTerm[] entries */ |
| 114274 | /**** whereLoopXfer() copies fields above ***********************/ |
| 114275 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 114276 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
| 114277 | WhereTerm **aLTerm; /* WhereTerms used */ |
| 114278 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
| 114279 | WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */ |
| 114280 | }; |
| 114281 | |
| 114282 | /* This object holds the prerequisites and the cost of running a |
| 114283 | ** subquery on one operand of an OR operator in the WHERE clause. |
| 114284 | ** See WhereOrSet for additional information |
| @@ -113624,10 +114600,11 @@ | |
| 114600 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
| 114601 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 114602 | #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 114603 | #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ |
| 114604 | #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ |
| 114605 | #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */ |
| 114606 | |
| 114607 | /************** End of whereInt.h ********************************************/ |
| 114608 | /************** Continuing where we left off in where.c **********************/ |
| 114609 | |
| 114610 | /* |
| @@ -113834,11 +114811,11 @@ | |
| 114811 | } |
| 114812 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
| 114813 | } |
| 114814 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
| 114815 | if( p && ExprHasProperty(p, EP_Unlikely) ){ |
| 114816 | pTerm->truthProb = sqlite3LogEst(p->iTable) - 270; |
| 114817 | }else{ |
| 114818 | pTerm->truthProb = 1; |
| 114819 | } |
| 114820 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
| 114821 | pTerm->wtFlags = wtFlags; |
| @@ -114364,10 +115341,19 @@ | |
| 115341 | if( pDerived ){ |
| 115342 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 115343 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 115344 | } |
| 115345 | } |
| 115346 | |
| 115347 | /* |
| 115348 | ** Mark term iChild as being a child of term iParent |
| 115349 | */ |
| 115350 | static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ |
| 115351 | pWC->a[iChild].iParent = iParent; |
| 115352 | pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; |
| 115353 | pWC->a[iParent].nChild++; |
| 115354 | } |
| 115355 | |
| 115356 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 115357 | /* |
| 115358 | ** Analyze a term that consists of two or more OR-connected |
| 115359 | ** subterms. So in: |
| @@ -114662,12 +115648,11 @@ | |
| 115648 | pNew->x.pList = pList; |
| 115649 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 115650 | testcase( idxNew==0 ); |
| 115651 | exprAnalyze(pSrc, pWC, idxNew); |
| 115652 | pTerm = &pWC->a[idxTerm]; |
| 115653 | markTermAsChild(pWC, idxNew, idxTerm); |
| 115654 | }else{ |
| 115655 | sqlite3ExprListDelete(db, pList); |
| 115656 | } |
| 115657 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
| 115658 | } |
| @@ -114765,13 +115750,12 @@ | |
| 115750 | return; |
| 115751 | } |
| 115752 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 115753 | if( idxNew==0 ) return; |
| 115754 | pNew = &pWC->a[idxNew]; |
| 115755 | markTermAsChild(pWC, idxNew, idxTerm); |
| 115756 | pTerm = &pWC->a[idxTerm]; |
| 115757 | pTerm->wtFlags |= TERM_COPIED; |
| 115758 | if( pExpr->op==TK_EQ |
| 115759 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 115760 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 115761 | ){ |
| @@ -114824,13 +115808,12 @@ | |
| 115808 | transferJoinMarkings(pNewExpr, pExpr); |
| 115809 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
| 115810 | testcase( idxNew==0 ); |
| 115811 | exprAnalyze(pSrc, pWC, idxNew); |
| 115812 | pTerm = &pWC->a[idxTerm]; |
| 115813 | markTermAsChild(pWC, idxNew, idxTerm); |
| 115814 | } |
| 115815 | } |
| 115816 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
| 115817 | |
| 115818 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 115819 | /* Analyze a term that is composed of two or more subterms connected by |
| @@ -114901,13 +115884,12 @@ | |
| 115884 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
| 115885 | testcase( idxNew2==0 ); |
| 115886 | exprAnalyze(pSrc, pWC, idxNew2); |
| 115887 | pTerm = &pWC->a[idxTerm]; |
| 115888 | if( isComplete ){ |
| 115889 | markTermAsChild(pWC, idxNew1, idxTerm); |
| 115890 | markTermAsChild(pWC, idxNew2, idxTerm); |
| 115891 | } |
| 115892 | } |
| 115893 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 115894 | |
| 115895 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| @@ -114936,13 +115918,12 @@ | |
| 115918 | pNewTerm = &pWC->a[idxNew]; |
| 115919 | pNewTerm->prereqRight = prereqExpr; |
| 115920 | pNewTerm->leftCursor = pLeft->iTable; |
| 115921 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 115922 | pNewTerm->eOperator = WO_MATCH; |
| 115923 | markTermAsChild(pWC, idxNew, idxTerm); |
| 115924 | pTerm = &pWC->a[idxTerm]; |
| 115925 | pTerm->wtFlags |= TERM_COPIED; |
| 115926 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 115927 | } |
| 115928 | } |
| 115929 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| @@ -114959,11 +115940,11 @@ | |
| 115940 | ** the start of the loop will prevent any results from being returned. |
| 115941 | */ |
| 115942 | if( pExpr->op==TK_NOTNULL |
| 115943 | && pExpr->pLeft->op==TK_COLUMN |
| 115944 | && pExpr->pLeft->iColumn>=0 |
| 115945 | && OptimizationEnabled(db, SQLITE_Stat34) |
| 115946 | ){ |
| 115947 | Expr *pNewExpr; |
| 115948 | Expr *pLeft = pExpr->pLeft; |
| 115949 | int idxNew; |
| 115950 | WhereTerm *pNewTerm; |
| @@ -114978,13 +115959,12 @@ | |
| 115959 | pNewTerm = &pWC->a[idxNew]; |
| 115960 | pNewTerm->prereqRight = 0; |
| 115961 | pNewTerm->leftCursor = pLeft->iTable; |
| 115962 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 115963 | pNewTerm->eOperator = WO_GT; |
| 115964 | markTermAsChild(pWC, idxNew, idxTerm); |
| 115965 | pTerm = &pWC->a[idxTerm]; |
| 115966 | pTerm->wtFlags |= TERM_COPIED; |
| 115967 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 115968 | } |
| 115969 | } |
| 115970 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| @@ -115200,10 +116180,12 @@ | |
| 116180 | WhereLoop *pLoop; /* The Loop object */ |
| 116181 | char *zNotUsed; /* Extra space on the end of pIdx */ |
| 116182 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 116183 | Bitmask extraCols; /* Bitmap of additional columns */ |
| 116184 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
| 116185 | Expr *pPartial = 0; /* Partial Index Expression */ |
| 116186 | int iContinue = 0; /* Jump here to skip excluded rows */ |
| 116187 | |
| 116188 | /* Generate code to skip over the creation and initialization of the |
| 116189 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 116190 | v = pParse->pVdbe; |
| 116191 | assert( v!=0 ); |
| @@ -115215,10 +116197,16 @@ | |
| 116197 | pTable = pSrc->pTab; |
| 116198 | pWCEnd = &pWC->a[pWC->nTerm]; |
| 116199 | pLoop = pLevel->pWLoop; |
| 116200 | idxCols = 0; |
| 116201 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 116202 | if( pLoop->prereq==0 |
| 116203 | && (pTerm->wtFlags & TERM_VIRTUAL)==0 |
| 116204 | && sqlite3ExprIsTableConstant(pTerm->pExpr, pSrc->iCursor) ){ |
| 116205 | pPartial = sqlite3ExprAnd(pParse->db, pPartial, |
| 116206 | sqlite3ExprDup(pParse->db, pTerm->pExpr, 0)); |
| 116207 | } |
| 116208 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 116209 | int iCol = pTerm->u.leftColumn; |
| 116210 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
| 116211 | testcase( iCol==BMS ); |
| 116212 | testcase( iCol==BMS-1 ); |
| @@ -115227,11 +116215,13 @@ | |
| 116215 | "automatic index on %s(%s)", pTable->zName, |
| 116216 | pTable->aCol[iCol].zName); |
| 116217 | sentWarning = 1; |
| 116218 | } |
| 116219 | if( (idxCols & cMask)==0 ){ |
| 116220 | if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){ |
| 116221 | goto end_auto_index_create; |
| 116222 | } |
| 116223 | pLoop->aLTerm[nKeyCol++] = pTerm; |
| 116224 | idxCols |= cMask; |
| 116225 | } |
| 116226 | } |
| 116227 | } |
| @@ -115247,24 +116237,23 @@ | |
| 116237 | ** be a covering index because the index will not be updated if the |
| 116238 | ** original table changes and the index and table cannot both be used |
| 116239 | ** if they go out of sync. |
| 116240 | */ |
| 116241 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
| 116242 | mxBitCol = MIN(BMS-1,pTable->nCol); |
| 116243 | testcase( pTable->nCol==BMS-1 ); |
| 116244 | testcase( pTable->nCol==BMS-2 ); |
| 116245 | for(i=0; i<mxBitCol; i++){ |
| 116246 | if( extraCols & MASKBIT(i) ) nKeyCol++; |
| 116247 | } |
| 116248 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 116249 | nKeyCol += pTable->nCol - BMS + 1; |
| 116250 | } |
| 116251 | |
| 116252 | /* Construct the Index object to describe this index */ |
| 116253 | pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); |
| 116254 | if( pIdx==0 ) goto end_auto_index_create; |
| 116255 | pLoop->u.btree.pIndex = pIdx; |
| 116256 | pIdx->zName = "auto-index"; |
| 116257 | pIdx->pTable = pTable; |
| 116258 | n = 0; |
| 116259 | idxCols = 0; |
| @@ -115312,22 +116301,33 @@ | |
| 116301 | sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); |
| 116302 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 116303 | VdbeComment((v, "for %s", pTable->zName)); |
| 116304 | |
| 116305 | /* Fill the automatic index with content */ |
| 116306 | sqlite3ExprCachePush(pParse); |
| 116307 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); |
| 116308 | if( pPartial ){ |
| 116309 | iContinue = sqlite3VdbeMakeLabel(v); |
| 116310 | sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); |
| 116311 | pLoop->wsFlags |= WHERE_PARTIALIDX; |
| 116312 | } |
| 116313 | regRecord = sqlite3GetTempReg(pParse); |
| 116314 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0); |
| 116315 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 116316 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 116317 | if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); |
| 116318 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); |
| 116319 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
| 116320 | sqlite3VdbeJumpHere(v, addrTop); |
| 116321 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 116322 | sqlite3ExprCachePop(pParse); |
| 116323 | |
| 116324 | /* Jump here when skipping the initialization */ |
| 116325 | sqlite3VdbeJumpHere(v, addrInit); |
| 116326 | |
| 116327 | end_auto_index_create: |
| 116328 | sqlite3ExprDelete(pParse->db, pPartial); |
| 116329 | } |
| 116330 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 116331 | |
| 116332 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 116333 | /* |
| @@ -115483,22 +116483,22 @@ | |
| 116483 | |
| 116484 | return pParse->nErr; |
| 116485 | } |
| 116486 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 116487 | |
| 116488 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 116489 | /* |
| 116490 | ** Estimate the location of a particular key among all keys in an |
| 116491 | ** index. Store the results in aStat as follows: |
| 116492 | ** |
| 116493 | ** aStat[0] Est. number of rows less than pVal |
| 116494 | ** aStat[1] Est. number of rows equal to pVal |
| 116495 | ** |
| 116496 | ** Return the index of the sample that is the smallest sample that |
| 116497 | ** is greater than or equal to pRec. |
| 116498 | */ |
| 116499 | static int whereKeyStats( |
| 116500 | Parse *pParse, /* Database connection */ |
| 116501 | Index *pIdx, /* Index to consider domain of */ |
| 116502 | UnpackedRecord *pRec, /* Vector of values to consider */ |
| 116503 | int roundUp, /* Round up if true. Round down if false */ |
| 116504 | tRowcnt *aStat /* OUT: stats written here */ |
| @@ -115576,10 +116576,11 @@ | |
| 116576 | }else{ |
| 116577 | iGap = iGap/3; |
| 116578 | } |
| 116579 | aStat[0] = iLower + iGap; |
| 116580 | } |
| 116581 | return i; |
| 116582 | } |
| 116583 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 116584 | |
| 116585 | /* |
| 116586 | ** If it is not NULL, pTerm is a term that provides an upper or lower |
| @@ -115726,11 +116727,11 @@ | |
| 116727 | ** pLower pUpper |
| 116728 | ** |
| 116729 | ** If either of the upper or lower bound is not present, then NULL is passed in |
| 116730 | ** place of the corresponding WhereTerm. |
| 116731 | ** |
| 116732 | ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index |
| 116733 | ** column subject to the range constraint. Or, equivalently, the number of |
| 116734 | ** equality constraints optimized by the proposed index scan. For example, |
| 116735 | ** assuming index p is on t1(a, b), and the SQL query is: |
| 116736 | ** |
| 116737 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| @@ -115742,11 +116743,11 @@ | |
| 116743 | ** |
| 116744 | ** then nEq is set to 0. |
| 116745 | ** |
| 116746 | ** When this function is called, *pnOut is set to the sqlite3LogEst() of the |
| 116747 | ** number of rows that the index scan is expected to visit without |
| 116748 | ** considering the range constraints. If nEq is 0, then *pnOut is the number of |
| 116749 | ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) |
| 116750 | ** to account for the range constraints pLower and pUpper. |
| 116751 | ** |
| 116752 | ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be |
| 116753 | ** used, a single range inequality reduces the search space by a factor of 4. |
| @@ -115766,14 +116767,11 @@ | |
| 116767 | |
| 116768 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 116769 | Index *p = pLoop->u.btree.pIndex; |
| 116770 | int nEq = pLoop->u.btree.nEq; |
| 116771 | |
| 116772 | if( p->nSample>0 && nEq<p->nSampleCol ){ |
| 116773 | if( nEq==pBuilder->nRecValid ){ |
| 116774 | UnpackedRecord *pRec = pBuilder->pRec; |
| 116775 | tRowcnt a[2]; |
| 116776 | u8 aff; |
| 116777 | |
| @@ -115785,19 +116783,23 @@ | |
| 116783 | ** |
| 116784 | ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 116785 | ** is not a simple variable or literal value), the lower bound of the |
| 116786 | ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 116787 | ** if $L is available, whereKeyStats() is called for both ($P) and |
| 116788 | ** ($P:$L) and the larger of the two returned values is used. |
| 116789 | ** |
| 116790 | ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 116791 | ** less than the upper bound of the range query. Where the upper bound |
| 116792 | ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 116793 | ** of iUpper are requested of whereKeyStats() and the smaller used. |
| 116794 | ** |
| 116795 | ** The number of rows between the two bounds is then just iUpper-iLower. |
| 116796 | */ |
| 116797 | tRowcnt iLower; /* Rows less than the lower bound */ |
| 116798 | tRowcnt iUpper; /* Rows less than the upper bound */ |
| 116799 | int iLwrIdx = -2; /* aSample[] for the lower bound */ |
| 116800 | int iUprIdx = -1; /* aSample[] for the upper bound */ |
| 116801 | |
| 116802 | if( pRec ){ |
| 116803 | testcase( pRec->nField!=pBuilder->nRecValid ); |
| 116804 | pRec->nField = pBuilder->nRecValid; |
| 116805 | } |
| @@ -115807,11 +116809,11 @@ | |
| 116809 | aff = p->pTable->aCol[p->aiColumn[nEq]].affinity; |
| 116810 | } |
| 116811 | /* Determine iLower and iUpper using ($P) only. */ |
| 116812 | if( nEq==0 ){ |
| 116813 | iLower = 0; |
| 116814 | iUpper = p->nRowEst0; |
| 116815 | }else{ |
| 116816 | /* Note: this call could be optimized away - since the same values must |
| 116817 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 116818 | whereKeyStats(pParse, p, pRec, 0, a); |
| 116819 | iLower = a[0]; |
| @@ -115831,11 +116833,11 @@ | |
| 116833 | int bOk; /* True if value is extracted from pExpr */ |
| 116834 | Expr *pExpr = pLower->pExpr->pRight; |
| 116835 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 116836 | if( rc==SQLITE_OK && bOk ){ |
| 116837 | tRowcnt iNew; |
| 116838 | iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a); |
| 116839 | iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 116840 | if( iNew>iLower ) iLower = iNew; |
| 116841 | nOut--; |
| 116842 | pLower = 0; |
| 116843 | } |
| @@ -115846,11 +116848,11 @@ | |
| 116848 | int bOk; /* True if value is extracted from pExpr */ |
| 116849 | Expr *pExpr = pUpper->pExpr->pRight; |
| 116850 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 116851 | if( rc==SQLITE_OK && bOk ){ |
| 116852 | tRowcnt iNew; |
| 116853 | iUprIdx = whereKeyStats(pParse, p, pRec, 1, a); |
| 116854 | iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 116855 | if( iNew<iUpper ) iUpper = iNew; |
| 116856 | nOut--; |
| 116857 | pUpper = 0; |
| 116858 | } |
| @@ -115858,10 +116860,15 @@ | |
| 116860 | |
| 116861 | pBuilder->pRec = pRec; |
| 116862 | if( rc==SQLITE_OK ){ |
| 116863 | if( iUpper>iLower ){ |
| 116864 | nNew = sqlite3LogEst(iUpper - iLower); |
| 116865 | /* TUNING: If both iUpper and iLower are derived from the same |
| 116866 | ** sample, then assume they are 4x more selective. This brings |
| 116867 | ** the estimated selectivity more in line with what it would be |
| 116868 | ** if estimated without the use of STAT3/4 tables. */ |
| 116869 | if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) ); |
| 116870 | }else{ |
| 116871 | nNew = 10; assert( 10==sqlite3LogEst(2) ); |
| 116872 | } |
| 116873 | if( nNew<nOut ){ |
| 116874 | nOut = nNew; |
| @@ -115882,16 +116889,19 @@ | |
| 116889 | #endif |
| 116890 | assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 ); |
| 116891 | nNew = whereRangeAdjust(pLower, nOut); |
| 116892 | nNew = whereRangeAdjust(pUpper, nNew); |
| 116893 | |
| 116894 | /* TUNING: If there is both an upper and lower limit and neither limit |
| 116895 | ** has an application-defined likelihood(), assume the range is |
| 116896 | ** reduced by an additional 75%. This means that, by default, an open-ended |
| 116897 | ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the |
| 116898 | ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to |
| 116899 | ** match 1/64 of the index. */ |
| 116900 | if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){ |
| 116901 | nNew -= 20; |
| 116902 | } |
| 116903 | |
| 116904 | nOut -= (pLower!=0) + (pUpper!=0); |
| 116905 | if( nNew<10 ) nNew = 10; |
| 116906 | if( nNew<nOut ) nOut = nNew; |
| 116907 | #if defined(WHERETRACE_ENABLED) |
| @@ -116247,11 +117257,11 @@ | |
| 117257 | |
| 117258 | /* This module is only called on query plans that use an index. */ |
| 117259 | pLoop = pLevel->pWLoop; |
| 117260 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 117261 | nEq = pLoop->u.btree.nEq; |
| 117262 | nSkip = pLoop->nSkip; |
| 117263 | pIdx = pLoop->u.btree.pIndex; |
| 117264 | assert( pIdx!=0 ); |
| 117265 | |
| 117266 | /* Figure out how many memory cells we will need then allocate them. |
| 117267 | */ |
| @@ -116361,11 +117371,11 @@ | |
| 117371 | ** "a=? AND b>?" |
| 117372 | */ |
| 117373 | static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){ |
| 117374 | Index *pIndex = pLoop->u.btree.pIndex; |
| 117375 | u16 nEq = pLoop->u.btree.nEq; |
| 117376 | u16 nSkip = pLoop->nSkip; |
| 117377 | int i, j; |
| 117378 | Column *aCol = pTab->aCol; |
| 117379 | i16 *aiColumn = pIndex->aiColumn; |
| 117380 | |
| 117381 | if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
| @@ -116392,23 +117402,27 @@ | |
| 117402 | sqlite3StrAccumAppend(pStr, ")", 1); |
| 117403 | } |
| 117404 | |
| 117405 | /* |
| 117406 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 117407 | ** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was |
| 117408 | ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode |
| 117409 | ** is added to the output to describe the table scan strategy in pLevel. |
| 117410 | ** |
| 117411 | ** If an OP_Explain opcode is added to the VM, its address is returned. |
| 117412 | ** Otherwise, if no OP_Explain is coded, zero is returned. |
| 117413 | */ |
| 117414 | static int explainOneScan( |
| 117415 | Parse *pParse, /* Parse context */ |
| 117416 | SrcList *pTabList, /* Table list this loop refers to */ |
| 117417 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 117418 | int iLevel, /* Value for "level" column of output */ |
| 117419 | int iFrom, /* Value for "from" column of output */ |
| 117420 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 117421 | ){ |
| 117422 | int ret = 0; |
| 117423 | #if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 117424 | if( pParse->explain==2 ) |
| 117425 | #endif |
| 117426 | { |
| 117427 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 117428 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| @@ -116421,11 +117435,11 @@ | |
| 117435 | StrAccum str; /* EQP output string */ |
| 117436 | char zBuf[100]; /* Initial space for EQP output string */ |
| 117437 | |
| 117438 | pLoop = pLevel->pWLoop; |
| 117439 | flags = pLoop->wsFlags; |
| 117440 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0; |
| 117441 | |
| 117442 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 117443 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 117444 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 117445 | |
| @@ -116450,10 +117464,12 @@ | |
| 117464 | assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
| 117465 | if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
| 117466 | if( isSearch ){ |
| 117467 | zFmt = "PRIMARY KEY"; |
| 117468 | } |
| 117469 | }else if( flags & WHERE_PARTIALIDX ){ |
| 117470 | zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; |
| 117471 | }else if( flags & WHERE_AUTO_INDEX ){ |
| 117472 | zFmt = "AUTOMATIC COVERING INDEX"; |
| 117473 | }else if( flags & WHERE_IDX_ONLY ){ |
| 117474 | zFmt = "COVERING INDEX %s"; |
| 117475 | }else{ |
| @@ -116491,16 +117507,49 @@ | |
| 117507 | }else{ |
| 117508 | sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
| 117509 | } |
| 117510 | #endif |
| 117511 | zMsg = sqlite3StrAccumFinish(&str); |
| 117512 | ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC); |
| 117513 | } |
| 117514 | return ret; |
| 117515 | } |
| 117516 | #else |
| 117517 | # define explainOneScan(u,v,w,x,y,z) 0 |
| 117518 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 117519 | |
| 117520 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 117521 | /* |
| 117522 | ** Configure the VM passed as the first argument with an |
| 117523 | ** sqlite3_stmt_scanstatus() entry corresponding to the scan used to |
| 117524 | ** implement level pLvl. Argument pSrclist is a pointer to the FROM |
| 117525 | ** clause that the scan reads data from. |
| 117526 | ** |
| 117527 | ** If argument addrExplain is not 0, it must be the address of an |
| 117528 | ** OP_Explain instruction that describes the same loop. |
| 117529 | */ |
| 117530 | static void addScanStatus( |
| 117531 | Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 117532 | SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 117533 | WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 117534 | int addrExplain /* Address of OP_Explain (or 0) */ |
| 117535 | ){ |
| 117536 | const char *zObj = 0; |
| 117537 | WhereLoop *pLoop = pLvl->pWLoop; |
| 117538 | if( (pLoop->wsFlags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ |
| 117539 | zObj = pLoop->u.btree.pIndex->zName; |
| 117540 | }else{ |
| 117541 | zObj = pSrclist->a[pLvl->iFrom].zName; |
| 117542 | } |
| 117543 | sqlite3VdbeScanStatus( |
| 117544 | v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj |
| 117545 | ); |
| 117546 | } |
| 117547 | #else |
| 117548 | # define addScanStatus(a, b, c, d) ((void)d) |
| 117549 | #endif |
| 117550 | |
| 117551 | |
| 117552 | |
| 117553 | /* |
| 117554 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 117555 | ** implementation described by pWInfo. |
| @@ -116798,11 +117847,11 @@ | |
| 117847 | u8 bSeekPastNull = 0; /* True to seek past initial nulls */ |
| 117848 | u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ |
| 117849 | |
| 117850 | pIdx = pLoop->u.btree.pIndex; |
| 117851 | iIdxCur = pLevel->iIdxCur; |
| 117852 | assert( nEq>=pLoop->nSkip ); |
| 117853 | |
| 117854 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 117855 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 117856 | ** query, then the caller will only allow the loop to run for |
| 117857 | ** a single iteration. This means that the first row returned |
| @@ -116815,11 +117864,11 @@ | |
| 117864 | || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); |
| 117865 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 117866 | && pWInfo->nOBSat>0 |
| 117867 | && (pIdx->nKeyCol>nEq) |
| 117868 | ){ |
| 117869 | assert( pLoop->nSkip==0 ); |
| 117870 | bSeekPastNull = 1; |
| 117871 | nExtraReg = 1; |
| 117872 | } |
| 117873 | |
| 117874 | /* Find any inequality constraint terms for the start and end |
| @@ -117164,13 +118213,15 @@ | |
| 118213 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 118214 | wctrlFlags, iCovCur); |
| 118215 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
| 118216 | if( pSubWInfo ){ |
| 118217 | WhereLoop *pSubLoop; |
| 118218 | int addrExplain = explainOneScan( |
| 118219 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 118220 | ); |
| 118221 | addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain); |
| 118222 | |
| 118223 | /* This is the sub-WHERE clause body. First skip over |
| 118224 | ** duplicate rows from prior sub-WHERE clauses, and record the |
| 118225 | ** rowid (or PRIMARY KEY) for the current row so that the same |
| 118226 | ** row will be skipped in subsequent sub-WHERE clauses. |
| 118227 | */ |
| @@ -117296,10 +118347,14 @@ | |
| 118347 | VdbeCoverageIf(v, bRev==0); |
| 118348 | VdbeCoverageIf(v, bRev!=0); |
| 118349 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 118350 | } |
| 118351 | } |
| 118352 | |
| 118353 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 118354 | pLevel->addrVisit = sqlite3VdbeCurrentAddr(v); |
| 118355 | #endif |
| 118356 | |
| 118357 | /* Insert code to test every subexpression that can be completely |
| 118358 | ** computed using the current set of tables. |
| 118359 | */ |
| 118360 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| @@ -117436,11 +118491,11 @@ | |
| 118491 | } |
| 118492 | sqlite3DebugPrintf(" %-19s", z); |
| 118493 | sqlite3_free(z); |
| 118494 | } |
| 118495 | if( p->wsFlags & WHERE_SKIPSCAN ){ |
| 118496 | sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip); |
| 118497 | }else{ |
| 118498 | sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); |
| 118499 | } |
| 118500 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
| 118501 | if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ |
| @@ -117547,34 +118602,41 @@ | |
| 118602 | sqlite3DbFree(db, pWInfo); |
| 118603 | } |
| 118604 | } |
| 118605 | |
| 118606 | /* |
| 118607 | ** Return TRUE if all of the following are true: |
| 118608 | ** |
| 118609 | ** (1) X has the same or lower cost that Y |
| 118610 | ** (2) X is a proper subset of Y |
| 118611 | ** (3) X skips at least as many columns as Y |
| 118612 | ** |
| 118613 | ** By "proper subset" we mean that X uses fewer WHERE clause terms |
| 118614 | ** than Y and that every WHERE clause term used by X is also used |
| 118615 | ** by Y. |
| 118616 | ** |
| 118617 | ** If X is a proper subset of Y then Y is a better choice and ought |
| 118618 | ** to have a lower cost. This routine returns TRUE when that cost |
| 118619 | ** relationship is inverted and needs to be adjusted. The third rule |
| 118620 | ** was added because if X uses skip-scan less than Y it still might |
| 118621 | ** deserve a lower cost even if it is a proper subset of Y. |
| 118622 | */ |
| 118623 | static int whereLoopCheaperProperSubset( |
| 118624 | const WhereLoop *pX, /* First WhereLoop to compare */ |
| 118625 | const WhereLoop *pY /* Compare against this WhereLoop */ |
| 118626 | ){ |
| 118627 | int i, j; |
| 118628 | if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){ |
| 118629 | return 0; /* X is not a subset of Y */ |
| 118630 | } |
| 118631 | if( pY->nSkip > pX->nSkip ) return 0; |
| 118632 | if( pX->rRun >= pY->rRun ){ |
| 118633 | if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ |
| 118634 | if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ |
| 118635 | } |
| 118636 | for(i=pX->nLTerm-1; i>=0; i--){ |
| 118637 | if( pX->aLTerm[i]==0 ) continue; |
| 118638 | for(j=pY->nLTerm-1; j>=0; j--){ |
| 118639 | if( pY->aLTerm[j]==pX->aLTerm[i] ) break; |
| 118640 | } |
| 118641 | if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ |
| 118642 | } |
| @@ -117592,37 +118654,28 @@ | |
| 118654 | ** is a proper subset. |
| 118655 | ** |
| 118656 | ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer |
| 118657 | ** WHERE clause terms than Y and that every WHERE clause term used by X is |
| 118658 | ** also used by Y. |
| 118659 | */ |
| 118660 | static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ |
| 118661 | if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return; |
| 118662 | for(; p; p=p->pNextLoop){ |
| 118663 | if( p->iTab!=pTemplate->iTab ) continue; |
| 118664 | if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; |
| 118665 | if( whereLoopCheaperProperSubset(p, pTemplate) ){ |
| 118666 | /* Adjust pTemplate cost downward so that it is cheaper than its |
| 118667 | ** subset p. */ |
| 118668 | WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 118669 | pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1)); |
| 118670 | pTemplate->rRun = p->rRun; |
| 118671 | pTemplate->nOut = p->nOut - 1; |
| 118672 | }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ |
| 118673 | /* Adjust pTemplate cost upward so that it is costlier than p since |
| 118674 | ** pTemplate is a proper subset of p */ |
| 118675 | WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 118676 | pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1)); |
| 118677 | pTemplate->rRun = p->rRun; |
| 118678 | pTemplate->nOut = p->nOut + 1; |
| 118679 | } |
| 118680 | } |
| 118681 | } |
| @@ -117663,12 +118716,13 @@ | |
| 118716 | ** rSetup. Call this SETUP-INVARIANT */ |
| 118717 | assert( p->rSetup>=pTemplate->rSetup ); |
| 118718 | |
| 118719 | /* Any loop using an appliation-defined index (or PRIMARY KEY or |
| 118720 | ** UNIQUE constraint) with one or more == constraints is better |
| 118721 | ** than an automatic index. Unless it is a skip-scan. */ |
| 118722 | if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 |
| 118723 | && (pTemplate->nSkip)==0 |
| 118724 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 118725 | && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0 |
| 118726 | && (p->prereq & pTemplate->prereq)==pTemplate->prereq |
| 118727 | ){ |
| 118728 | break; |
| @@ -117823,25 +118877,46 @@ | |
| 118877 | |
| 118878 | /* |
| 118879 | ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 118880 | ** WHERE clause that reference the loop but which are not used by an |
| 118881 | ** index. |
| 118882 | * |
| 118883 | ** For every WHERE clause term that is not used by the index |
| 118884 | ** and which has a truth probability assigned by one of the likelihood(), |
| 118885 | ** likely(), or unlikely() SQL functions, reduce the estimated number |
| 118886 | ** of output rows by the probability specified. |
| 118887 | ** |
| 118888 | ** TUNING: For every WHERE clause term that is not used by the index |
| 118889 | ** and which does not have an assigned truth probability, heuristics |
| 118890 | ** described below are used to try to estimate the truth probability. |
| 118891 | ** TODO --> Perhaps this is something that could be improved by better |
| 118892 | ** table statistics. |
| 118893 | ** |
| 118894 | ** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75% |
| 118895 | ** value corresponds to -1 in LogEst notation, so this means decrement |
| 118896 | ** the WhereLoop.nOut field for every such WHERE clause term. |
| 118897 | ** |
| 118898 | ** Heuristic 2: If there exists one or more WHERE clause terms of the |
| 118899 | ** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the |
| 118900 | ** final output row estimate is no greater than 1/4 of the total number |
| 118901 | ** of rows in the table. In other words, assume that x==EXPR will filter |
| 118902 | ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the |
| 118903 | ** "x" column is boolean or else -1 or 0 or 1 is a common default value |
| 118904 | ** on the "x" column and so in that case only cap the output row estimate |
| 118905 | ** at 1/2 instead of 1/4. |
| 118906 | */ |
| 118907 | static void whereLoopOutputAdjust( |
| 118908 | WhereClause *pWC, /* The WHERE clause */ |
| 118909 | WhereLoop *pLoop, /* The loop to adjust downward */ |
| 118910 | LogEst nRow /* Number of rows in the entire table */ |
| 118911 | ){ |
| 118912 | WhereTerm *pTerm, *pX; |
| 118913 | Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); |
| 118914 | int i, j, k; |
| 118915 | LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */ |
| 118916 | |
| 118917 | assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
| 118918 | for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ |
| 118919 | if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; |
| 118920 | if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; |
| 118921 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
| 118922 | for(j=pLoop->nLTerm-1; j>=0; j--){ |
| @@ -117850,24 +118925,30 @@ | |
| 118925 | if( pX==pTerm ) break; |
| 118926 | if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; |
| 118927 | } |
| 118928 | if( j<0 ){ |
| 118929 | if( pTerm->truthProb<=0 ){ |
| 118930 | /* If a truth probability is specified using the likelihood() hints, |
| 118931 | ** then use the probability provided by the application. */ |
| 118932 | pLoop->nOut += pTerm->truthProb; |
| 118933 | }else{ |
| 118934 | /* In the absence of explicit truth probabilities, use heuristics to |
| 118935 | ** guess a reasonable truth probability. */ |
| 118936 | pLoop->nOut--; |
| 118937 | if( pTerm->eOperator&WO_EQ ){ |
| 118938 | Expr *pRight = pTerm->pExpr->pRight; |
| 118939 | if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){ |
| 118940 | k = 10; |
| 118941 | }else{ |
| 118942 | k = 20; |
| 118943 | } |
| 118944 | if( iReduce<k ) iReduce = k; |
| 118945 | } |
| 118946 | } |
| 118947 | } |
| 118948 | } |
| 118949 | if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce; |
| 118950 | } |
| 118951 | |
| 118952 | /* |
| 118953 | ** Adjust the cost C by the costMult facter T. This only occurs if |
| 118954 | ** compiled with -DSQLITE_ENABLE_COSTMULT |
| @@ -117904,11 +118985,11 @@ | |
| 118985 | int opMask; /* Valid operators for constraints */ |
| 118986 | WhereScan scan; /* Iterator for WHERE terms */ |
| 118987 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 118988 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 118989 | u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 118990 | u16 saved_nSkip; /* Original value of pNew->nSkip */ |
| 118991 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 118992 | LogEst saved_nOut; /* Original value of pNew->nOut */ |
| 118993 | int iCol; /* Index of the column in the table */ |
| 118994 | int rc = SQLITE_OK; /* Return code */ |
| 118995 | LogEst rSize; /* Number of rows in the table */ |
| @@ -117933,56 +119014,18 @@ | |
| 119014 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| 119015 | |
| 119016 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
| 119017 | opMask, pProbe); |
| 119018 | saved_nEq = pNew->u.btree.nEq; |
| 119019 | saved_nSkip = pNew->nSkip; |
| 119020 | saved_nLTerm = pNew->nLTerm; |
| 119021 | saved_wsFlags = pNew->wsFlags; |
| 119022 | saved_prereq = pNew->prereq; |
| 119023 | saved_nOut = pNew->nOut; |
| 119024 | pNew->rSetup = 0; |
| 119025 | rSize = pProbe->aiRowLogEst[0]; |
| 119026 | rLogSize = estLog(rSize); |
| 119027 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
| 119028 | u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ |
| 119029 | LogEst rCostIdx; |
| 119030 | LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ |
| 119031 | int nIn = 0; |
| @@ -118073,11 +119116,10 @@ | |
| 119116 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 119117 | tRowcnt nOut = 0; |
| 119118 | if( nInMul==0 |
| 119119 | && pProbe->nSample |
| 119120 | && pNew->u.btree.nEq<=pProbe->nSampleCol |
| 119121 | && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) |
| 119122 | ){ |
| 119123 | Expr *pExpr = pTerm->pExpr; |
| 119124 | if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){ |
| 119125 | testcase( eOp & WO_EQ ); |
| @@ -118141,14 +119183,49 @@ | |
| 119183 | pBuilder->nRecValid = nRecValid; |
| 119184 | #endif |
| 119185 | } |
| 119186 | pNew->prereq = saved_prereq; |
| 119187 | pNew->u.btree.nEq = saved_nEq; |
| 119188 | pNew->nSkip = saved_nSkip; |
| 119189 | pNew->wsFlags = saved_wsFlags; |
| 119190 | pNew->nOut = saved_nOut; |
| 119191 | pNew->nLTerm = saved_nLTerm; |
| 119192 | |
| 119193 | /* Consider using a skip-scan if there are no WHERE clause constraints |
| 119194 | ** available for the left-most terms of the index, and if the average |
| 119195 | ** number of repeats in the left-most terms is at least 18. |
| 119196 | ** |
| 119197 | ** The magic number 18 is selected on the basis that scanning 17 rows |
| 119198 | ** is almost always quicker than an index seek (even though if the index |
| 119199 | ** contains fewer than 2^17 rows we assume otherwise in other parts of |
| 119200 | ** the code). And, even if it is not, it should not be too much slower. |
| 119201 | ** On the other hand, the extra seeks could end up being significantly |
| 119202 | ** more expensive. */ |
| 119203 | assert( 42==sqlite3LogEst(18) ); |
| 119204 | if( saved_nEq==saved_nSkip |
| 119205 | && saved_nEq+1<pProbe->nKeyCol |
| 119206 | && pProbe->noSkipScan==0 |
| 119207 | && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ |
| 119208 | && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK |
| 119209 | ){ |
| 119210 | LogEst nIter; |
| 119211 | pNew->u.btree.nEq++; |
| 119212 | pNew->nSkip++; |
| 119213 | pNew->aLTerm[pNew->nLTerm++] = 0; |
| 119214 | pNew->wsFlags |= WHERE_SKIPSCAN; |
| 119215 | nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; |
| 119216 | pNew->nOut -= nIter; |
| 119217 | /* TUNING: Because uncertainties in the estimates for skip-scan queries, |
| 119218 | ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ |
| 119219 | nIter += 5; |
| 119220 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
| 119221 | pNew->nOut = saved_nOut; |
| 119222 | pNew->u.btree.nEq = saved_nEq; |
| 119223 | pNew->nSkip = saved_nSkip; |
| 119224 | pNew->wsFlags = saved_wsFlags; |
| 119225 | } |
| 119226 | |
| 119227 | return rc; |
| 119228 | } |
| 119229 | |
| 119230 | /* |
| 119231 | ** Return True if it is possible that pIndex might be useful in |
| @@ -118323,11 +119400,11 @@ | |
| 119400 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 119401 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
| 119402 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 119403 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 119404 | pNew->u.btree.nEq = 1; |
| 119405 | pNew->nSkip = 0; |
| 119406 | pNew->u.btree.pIndex = 0; |
| 119407 | pNew->nLTerm = 1; |
| 119408 | pNew->aLTerm[0] = pTerm; |
| 119409 | /* TUNING: One-time cost for computing the automatic index is |
| 119410 | ** estimated to be X*N*log2(N) where N is the number of rows in |
| @@ -118364,11 +119441,11 @@ | |
| 119441 | testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ |
| 119442 | continue; /* Partial index inappropriate for this query */ |
| 119443 | } |
| 119444 | rSize = pProbe->aiRowLogEst[0]; |
| 119445 | pNew->u.btree.nEq = 0; |
| 119446 | pNew->nSkip = 0; |
| 119447 | pNew->nLTerm = 0; |
| 119448 | pNew->iSortIdx = 0; |
| 119449 | pNew->rSetup = 0; |
| 119450 | pNew->prereq = mExtra; |
| 119451 | pNew->nOut = rSize; |
| @@ -118914,11 +119991,11 @@ | |
| 119991 | for(j=0; j<nColumn; j++){ |
| 119992 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 119993 | |
| 119994 | /* Skip over == and IS NULL terms */ |
| 119995 | if( j<pLoop->u.btree.nEq |
| 119996 | && pLoop->nSkip==0 |
| 119997 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
| 119998 | ){ |
| 119999 | if( i & WO_ISNULL ){ |
| 120000 | testcase( isOrderDistinct ); |
| 120001 | isOrderDistinct = 0; |
| @@ -119368,11 +120445,11 @@ | |
| 120445 | } |
| 120446 | } |
| 120447 | } |
| 120448 | |
| 120449 | #ifdef WHERETRACE_ENABLED /* >=2 */ |
| 120450 | if( sqlite3WhereTrace & 0x02 ){ |
| 120451 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
| 120452 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
| 120453 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
| 120454 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
| 120455 | pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); |
| @@ -119487,11 +120564,11 @@ | |
| 120564 | if( pItem->zIndex ) return 0; |
| 120565 | iCur = pItem->iCursor; |
| 120566 | pWC = &pWInfo->sWC; |
| 120567 | pLoop = pBuilder->pNew; |
| 120568 | pLoop->wsFlags = 0; |
| 120569 | pLoop->nSkip = 0; |
| 120570 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
| 120571 | if( pTerm ){ |
| 120572 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 120573 | pLoop->aLTerm[0] = pTerm; |
| 120574 | pLoop->nLTerm = 1; |
| @@ -119499,11 +120576,10 @@ | |
| 120576 | /* TUNING: Cost of a rowid lookup is 10 */ |
| 120577 | pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ |
| 120578 | }else{ |
| 120579 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 120580 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
| 120581 | if( !IsUniqueIndex(pIdx) |
| 120582 | || pIdx->pPartIdxWhere!=0 |
| 120583 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
| 120584 | ) continue; |
| 120585 | for(j=0; j<pIdx->nKeyCol; j++){ |
| @@ -120008,22 +121084,30 @@ | |
| 121084 | ** loop below generates code for a single nested loop of the VM |
| 121085 | ** program. |
| 121086 | */ |
| 121087 | notReady = ~(Bitmask)0; |
| 121088 | for(ii=0; ii<nTabList; ii++){ |
| 121089 | int addrExplain; |
| 121090 | int wsFlags; |
| 121091 | pLevel = &pWInfo->a[ii]; |
| 121092 | wsFlags = pLevel->pWLoop->wsFlags; |
| 121093 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 121094 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 121095 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 121096 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 121097 | if( db->mallocFailed ) goto whereBeginError; |
| 121098 | } |
| 121099 | #endif |
| 121100 | addrExplain = explainOneScan( |
| 121101 | pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags |
| 121102 | ); |
| 121103 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
| 121104 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
| 121105 | pWInfo->iContinue = pLevel->addrCont; |
| 121106 | if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){ |
| 121107 | addScanStatus(v, pTabList, pLevel, addrExplain); |
| 121108 | } |
| 121109 | } |
| 121110 | |
| 121111 | /* Done. */ |
| 121112 | VdbeModuleComment((v, "Begin WHERE-core")); |
| 121113 | return pWInfo; |
| @@ -124687,10 +125771,17 @@ | |
| 125771 | ** is look for a semicolon that is not part of an string or comment. |
| 125772 | */ |
| 125773 | SQLITE_API int sqlite3_complete(const char *zSql){ |
| 125774 | u8 state = 0; /* Current state, using numbers defined in header comment */ |
| 125775 | u8 token; /* Value of the next token */ |
| 125776 | |
| 125777 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 125778 | if( zSql==0 ){ |
| 125779 | (void)SQLITE_MISUSE_BKPT; |
| 125780 | return 0; |
| 125781 | } |
| 125782 | #endif |
| 125783 | |
| 125784 | #ifndef SQLITE_OMIT_TRIGGER |
| 125785 | /* A complex statement machine used to detect the end of a CREATE TRIGGER |
| 125786 | ** statement. This is the normal case. |
| 125787 | */ |
| @@ -125285,74 +126376,106 @@ | |
| 126376 | |
| 126377 | va_start(ap, op); |
| 126378 | switch( op ){ |
| 126379 | |
| 126380 | /* Mutex configuration options are only available in a threadsafe |
| 126381 | ** compile. |
| 126382 | */ |
| 126383 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */ |
| 126384 | case SQLITE_CONFIG_SINGLETHREAD: { |
| 126385 | /* Disable all mutexing */ |
| 126386 | sqlite3GlobalConfig.bCoreMutex = 0; |
| 126387 | sqlite3GlobalConfig.bFullMutex = 0; |
| 126388 | break; |
| 126389 | } |
| 126390 | #endif |
| 126391 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */ |
| 126392 | case SQLITE_CONFIG_MULTITHREAD: { |
| 126393 | /* Disable mutexing of database connections */ |
| 126394 | /* Enable mutexing of core data structures */ |
| 126395 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 126396 | sqlite3GlobalConfig.bFullMutex = 0; |
| 126397 | break; |
| 126398 | } |
| 126399 | #endif |
| 126400 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */ |
| 126401 | case SQLITE_CONFIG_SERIALIZED: { |
| 126402 | /* Enable all mutexing */ |
| 126403 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 126404 | sqlite3GlobalConfig.bFullMutex = 1; |
| 126405 | break; |
| 126406 | } |
| 126407 | #endif |
| 126408 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */ |
| 126409 | case SQLITE_CONFIG_MUTEX: { |
| 126410 | /* Specify an alternative mutex implementation */ |
| 126411 | sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); |
| 126412 | break; |
| 126413 | } |
| 126414 | #endif |
| 126415 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */ |
| 126416 | case SQLITE_CONFIG_GETMUTEX: { |
| 126417 | /* Retrieve the current mutex implementation */ |
| 126418 | *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; |
| 126419 | break; |
| 126420 | } |
| 126421 | #endif |
| 126422 | |
| 126423 | case SQLITE_CONFIG_MALLOC: { |
| 126424 | /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a |
| 126425 | ** single argument which is a pointer to an instance of the |
| 126426 | ** sqlite3_mem_methods structure. The argument specifies alternative |
| 126427 | ** low-level memory allocation routines to be used in place of the memory |
| 126428 | ** allocation routines built into SQLite. */ |
| 126429 | sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); |
| 126430 | break; |
| 126431 | } |
| 126432 | case SQLITE_CONFIG_GETMALLOC: { |
| 126433 | /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a |
| 126434 | ** single argument which is a pointer to an instance of the |
| 126435 | ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is |
| 126436 | ** filled with the currently defined memory allocation routines. */ |
| 126437 | if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); |
| 126438 | *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; |
| 126439 | break; |
| 126440 | } |
| 126441 | case SQLITE_CONFIG_MEMSTATUS: { |
| 126442 | /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes |
| 126443 | ** single argument of type int, interpreted as a boolean, which enables |
| 126444 | ** or disables the collection of memory allocation statistics. */ |
| 126445 | sqlite3GlobalConfig.bMemstat = va_arg(ap, int); |
| 126446 | break; |
| 126447 | } |
| 126448 | case SQLITE_CONFIG_SCRATCH: { |
| 126449 | /* EVIDENCE-OF: R-08404-60887 There are three arguments to |
| 126450 | ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from |
| 126451 | ** which the scratch allocations will be drawn, the size of each scratch |
| 126452 | ** allocation (sz), and the maximum number of scratch allocations (N). */ |
| 126453 | sqlite3GlobalConfig.pScratch = va_arg(ap, void*); |
| 126454 | sqlite3GlobalConfig.szScratch = va_arg(ap, int); |
| 126455 | sqlite3GlobalConfig.nScratch = va_arg(ap, int); |
| 126456 | break; |
| 126457 | } |
| 126458 | case SQLITE_CONFIG_PAGECACHE: { |
| 126459 | /* EVIDENCE-OF: R-31408-40510 There are three arguments to |
| 126460 | ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory, the size |
| 126461 | ** of each page buffer (sz), and the number of pages (N). */ |
| 126462 | sqlite3GlobalConfig.pPage = va_arg(ap, void*); |
| 126463 | sqlite3GlobalConfig.szPage = va_arg(ap, int); |
| 126464 | sqlite3GlobalConfig.nPage = va_arg(ap, int); |
| 126465 | break; |
| 126466 | } |
| 126467 | case SQLITE_CONFIG_PCACHE_HDRSZ: { |
| 126468 | /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes |
| 126469 | ** a single parameter which is a pointer to an integer and writes into |
| 126470 | ** that integer the number of extra bytes per page required for each page |
| 126471 | ** in SQLITE_CONFIG_PAGECACHE. */ |
| 126472 | *va_arg(ap, int*) = |
| 126473 | sqlite3HeaderSizeBtree() + |
| 126474 | sqlite3HeaderSizePcache() + |
| 126475 | sqlite3HeaderSizePcache1(); |
| 126476 | break; |
| 126477 | } |
| 126478 | |
| 126479 | case SQLITE_CONFIG_PCACHE: { |
| 126480 | /* no-op */ |
| 126481 | break; |
| @@ -125362,25 +126485,37 @@ | |
| 126485 | rc = SQLITE_ERROR; |
| 126486 | break; |
| 126487 | } |
| 126488 | |
| 126489 | case SQLITE_CONFIG_PCACHE2: { |
| 126490 | /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a |
| 126491 | ** single argument which is a pointer to an sqlite3_pcache_methods2 |
| 126492 | ** object. This object specifies the interface to a custom page cache |
| 126493 | ** implementation. */ |
| 126494 | sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*); |
| 126495 | break; |
| 126496 | } |
| 126497 | case SQLITE_CONFIG_GETPCACHE2: { |
| 126498 | /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a |
| 126499 | ** single argument which is a pointer to an sqlite3_pcache_methods2 |
| 126500 | ** object. SQLite copies of the current page cache implementation into |
| 126501 | ** that object. */ |
| 126502 | if( sqlite3GlobalConfig.pcache2.xInit==0 ){ |
| 126503 | sqlite3PCacheSetDefault(); |
| 126504 | } |
| 126505 | *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2; |
| 126506 | break; |
| 126507 | } |
| 126508 | |
| 126509 | /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only |
| 126510 | ** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or |
| 126511 | ** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */ |
| 126512 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 126513 | case SQLITE_CONFIG_HEAP: { |
| 126514 | /* EVIDENCE-OF: R-19854-42126 There are three arguments to |
| 126515 | ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the |
| 126516 | ** number of bytes in the memory buffer, and the minimum allocation size. */ |
| 126517 | sqlite3GlobalConfig.pHeap = va_arg(ap, void*); |
| 126518 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 126519 | sqlite3GlobalConfig.mnReq = va_arg(ap, int); |
| 126520 | |
| 126521 | if( sqlite3GlobalConfig.mnReq<1 ){ |
| @@ -125389,21 +126524,23 @@ | |
| 126524 | /* cap min request size at 2^12 */ |
| 126525 | sqlite3GlobalConfig.mnReq = (1<<12); |
| 126526 | } |
| 126527 | |
| 126528 | if( sqlite3GlobalConfig.pHeap==0 ){ |
| 126529 | /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer) |
| 126530 | ** is NULL, then SQLite reverts to using its default memory allocator |
| 126531 | ** (the system malloc() implementation), undoing any prior invocation of |
| 126532 | ** SQLITE_CONFIG_MALLOC. |
| 126533 | ** |
| 126534 | ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to |
| 126535 | ** revert to its default implementation when sqlite3_initialize() is run |
| 126536 | */ |
| 126537 | memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); |
| 126538 | }else{ |
| 126539 | /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the |
| 126540 | ** alternative memory allocator is engaged to handle all of SQLites |
| 126541 | ** memory allocation needs. */ |
| 126542 | #ifdef SQLITE_ENABLE_MEMSYS3 |
| 126543 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); |
| 126544 | #endif |
| 126545 | #ifdef SQLITE_ENABLE_MEMSYS5 |
| 126546 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); |
| @@ -125438,15 +126575,23 @@ | |
| 126575 | ** can be changed at start-time using the |
| 126576 | ** sqlite3_config(SQLITE_CONFIG_URI,1) or |
| 126577 | ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. |
| 126578 | */ |
| 126579 | case SQLITE_CONFIG_URI: { |
| 126580 | /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single |
| 126581 | ** argument of type int. If non-zero, then URI handling is globally |
| 126582 | ** enabled. If the parameter is zero, then URI handling is globally |
| 126583 | ** disabled. */ |
| 126584 | sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); |
| 126585 | break; |
| 126586 | } |
| 126587 | |
| 126588 | case SQLITE_CONFIG_COVERING_INDEX_SCAN: { |
| 126589 | /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN |
| 126590 | ** option takes a single integer argument which is interpreted as a |
| 126591 | ** boolean in order to enable or disable the use of covering indices for |
| 126592 | ** full table scans in the query optimizer. */ |
| 126593 | sqlite3GlobalConfig.bUseCis = va_arg(ap, int); |
| 126594 | break; |
| 126595 | } |
| 126596 | |
| 126597 | #ifdef SQLITE_ENABLE_SQLLOG |
| @@ -125457,24 +126602,37 @@ | |
| 126602 | break; |
| 126603 | } |
| 126604 | #endif |
| 126605 | |
| 126606 | case SQLITE_CONFIG_MMAP_SIZE: { |
| 126607 | /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit |
| 126608 | ** integer (sqlite3_int64) values that are the default mmap size limit |
| 126609 | ** (the default setting for PRAGMA mmap_size) and the maximum allowed |
| 126610 | ** mmap size limit. */ |
| 126611 | sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64); |
| 126612 | sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64); |
| 126613 | /* EVIDENCE-OF: R-53367-43190 If either argument to this option is |
| 126614 | ** negative, then that argument is changed to its compile-time default. |
| 126615 | ** |
| 126616 | ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be |
| 126617 | ** silently truncated if necessary so that it does not exceed the |
| 126618 | ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE |
| 126619 | ** compile-time option. |
| 126620 | */ |
| 126621 | if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ) mxMmap = SQLITE_MAX_MMAP_SIZE; |
| 126622 | if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE; |
| 126623 | if( szMmap>mxMmap) szMmap = mxMmap; |
| 126624 | sqlite3GlobalConfig.mxMmap = mxMmap; |
| 126625 | sqlite3GlobalConfig.szMmap = szMmap; |
| 126626 | break; |
| 126627 | } |
| 126628 | |
| 126629 | #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */ |
| 126630 | case SQLITE_CONFIG_WIN32_HEAPSIZE: { |
| 126631 | /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit |
| 126632 | ** unsigned integer value that specifies the maximum size of the created |
| 126633 | ** heap. */ |
| 126634 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 126635 | break; |
| 126636 | } |
| 126637 | #endif |
| 126638 | |
| @@ -125554,19 +126712,29 @@ | |
| 126712 | |
| 126713 | /* |
| 126714 | ** Return the mutex associated with a database connection. |
| 126715 | */ |
| 126716 | SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ |
| 126717 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 126718 | if( !sqlite3SafetyCheckOk(db) ){ |
| 126719 | (void)SQLITE_MISUSE_BKPT; |
| 126720 | return 0; |
| 126721 | } |
| 126722 | #endif |
| 126723 | return db->mutex; |
| 126724 | } |
| 126725 | |
| 126726 | /* |
| 126727 | ** Free up as much memory as we can from the given database |
| 126728 | ** connection. |
| 126729 | */ |
| 126730 | SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){ |
| 126731 | int i; |
| 126732 | |
| 126733 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 126734 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 126735 | #endif |
| 126736 | sqlite3_mutex_enter(db->mutex); |
| 126737 | sqlite3BtreeEnterAll(db); |
| 126738 | for(i=0; i<db->nDb; i++){ |
| 126739 | Btree *pBt = db->aDb[i].pBt; |
| 126740 | if( pBt ){ |
| @@ -125652,17 +126820,24 @@ | |
| 126820 | int nKey1, const void *pKey1, |
| 126821 | int nKey2, const void *pKey2 |
| 126822 | ){ |
| 126823 | int rc, n; |
| 126824 | n = nKey1<nKey2 ? nKey1 : nKey2; |
| 126825 | /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares |
| 126826 | ** strings byte by byte using the memcmp() function from the standard C |
| 126827 | ** library. */ |
| 126828 | rc = memcmp(pKey1, pKey2, n); |
| 126829 | if( rc==0 ){ |
| 126830 | if( padFlag |
| 126831 | && allSpaces(((char*)pKey1)+n, nKey1-n) |
| 126832 | && allSpaces(((char*)pKey2)+n, nKey2-n) |
| 126833 | ){ |
| 126834 | /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra |
| 126835 | ** spaces at the end of either string do not change the result. In other |
| 126836 | ** words, strings will compare equal to one another as long as they |
| 126837 | ** differ only in the number of spaces at the end. |
| 126838 | */ |
| 126839 | }else{ |
| 126840 | rc = nKey1 - nKey2; |
| 126841 | } |
| 126842 | } |
| 126843 | return rc; |
| @@ -125693,24 +126868,42 @@ | |
| 126868 | |
| 126869 | /* |
| 126870 | ** Return the ROWID of the most recent insert |
| 126871 | */ |
| 126872 | SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ |
| 126873 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 126874 | if( !sqlite3SafetyCheckOk(db) ){ |
| 126875 | (void)SQLITE_MISUSE_BKPT; |
| 126876 | return 0; |
| 126877 | } |
| 126878 | #endif |
| 126879 | return db->lastRowid; |
| 126880 | } |
| 126881 | |
| 126882 | /* |
| 126883 | ** Return the number of changes in the most recent call to sqlite3_exec(). |
| 126884 | */ |
| 126885 | SQLITE_API int sqlite3_changes(sqlite3 *db){ |
| 126886 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 126887 | if( !sqlite3SafetyCheckOk(db) ){ |
| 126888 | (void)SQLITE_MISUSE_BKPT; |
| 126889 | return 0; |
| 126890 | } |
| 126891 | #endif |
| 126892 | return db->nChange; |
| 126893 | } |
| 126894 | |
| 126895 | /* |
| 126896 | ** Return the number of changes since the database handle was opened. |
| 126897 | */ |
| 126898 | SQLITE_API int sqlite3_total_changes(sqlite3 *db){ |
| 126899 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 126900 | if( !sqlite3SafetyCheckOk(db) ){ |
| 126901 | (void)SQLITE_MISUSE_BKPT; |
| 126902 | return 0; |
| 126903 | } |
| 126904 | #endif |
| 126905 | return db->nTotalChange; |
| 126906 | } |
| 126907 | |
| 126908 | /* |
| 126909 | ** Close all open savepoints. This function only manipulates fields of the |
| @@ -126255,10 +127448,13 @@ | |
| 127448 | SQLITE_API int sqlite3_busy_handler( |
| 127449 | sqlite3 *db, |
| 127450 | int (*xBusy)(void*,int), |
| 127451 | void *pArg |
| 127452 | ){ |
| 127453 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127454 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE; |
| 127455 | #endif |
| 127456 | sqlite3_mutex_enter(db->mutex); |
| 127457 | db->busyHandler.xFunc = xBusy; |
| 127458 | db->busyHandler.pArg = pArg; |
| 127459 | db->busyHandler.nBusy = 0; |
| 127460 | db->busyTimeout = 0; |
| @@ -126276,10 +127472,16 @@ | |
| 127472 | sqlite3 *db, |
| 127473 | int nOps, |
| 127474 | int (*xProgress)(void*), |
| 127475 | void *pArg |
| 127476 | ){ |
| 127477 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127478 | if( !sqlite3SafetyCheckOk(db) ){ |
| 127479 | (void)SQLITE_MISUSE_BKPT; |
| 127480 | return; |
| 127481 | } |
| 127482 | #endif |
| 127483 | sqlite3_mutex_enter(db->mutex); |
| 127484 | if( nOps>0 ){ |
| 127485 | db->xProgress = xProgress; |
| 127486 | db->nProgressOps = (unsigned)nOps; |
| 127487 | db->pProgressArg = pArg; |
| @@ -126296,10 +127498,13 @@ | |
| 127498 | /* |
| 127499 | ** This routine installs a default busy handler that waits for the |
| 127500 | ** specified number of milliseconds before returning 0. |
| 127501 | */ |
| 127502 | SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
| 127503 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127504 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 127505 | #endif |
| 127506 | if( ms>0 ){ |
| 127507 | sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); |
| 127508 | db->busyTimeout = ms; |
| 127509 | }else{ |
| 127510 | sqlite3_busy_handler(db, 0, 0); |
| @@ -126309,10 +127514,16 @@ | |
| 127514 | |
| 127515 | /* |
| 127516 | ** Cause any pending operation to stop at its earliest opportunity. |
| 127517 | */ |
| 127518 | SQLITE_API void sqlite3_interrupt(sqlite3 *db){ |
| 127519 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127520 | if( !sqlite3SafetyCheckOk(db) ){ |
| 127521 | (void)SQLITE_MISUSE_BKPT; |
| 127522 | return; |
| 127523 | } |
| 127524 | #endif |
| 127525 | db->u1.isInterrupted = 1; |
| 127526 | } |
| 127527 | |
| 127528 | |
| 127529 | /* |
| @@ -126446,10 +127657,16 @@ | |
| 127657 | void (*xFinal)(sqlite3_context*), |
| 127658 | void (*xDestroy)(void *) |
| 127659 | ){ |
| 127660 | int rc = SQLITE_ERROR; |
| 127661 | FuncDestructor *pArg = 0; |
| 127662 | |
| 127663 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127664 | if( !sqlite3SafetyCheckOk(db) ){ |
| 127665 | return SQLITE_MISUSE_BKPT; |
| 127666 | } |
| 127667 | #endif |
| 127668 | sqlite3_mutex_enter(db->mutex); |
| 127669 | if( xDestroy ){ |
| 127670 | pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); |
| 127671 | if( !pArg ){ |
| 127672 | xDestroy(p); |
| @@ -126482,10 +127699,14 @@ | |
| 127699 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 127700 | void (*xFinal)(sqlite3_context*) |
| 127701 | ){ |
| 127702 | int rc; |
| 127703 | char *zFunc8; |
| 127704 | |
| 127705 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127706 | if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT; |
| 127707 | #endif |
| 127708 | sqlite3_mutex_enter(db->mutex); |
| 127709 | assert( !db->mallocFailed ); |
| 127710 | zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); |
| 127711 | rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0); |
| 127712 | sqlite3DbFree(db, zFunc8); |
| @@ -126513,10 +127734,16 @@ | |
| 127734 | const char *zName, |
| 127735 | int nArg |
| 127736 | ){ |
| 127737 | int nName = sqlite3Strlen30(zName); |
| 127738 | int rc = SQLITE_OK; |
| 127739 | |
| 127740 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127741 | if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){ |
| 127742 | return SQLITE_MISUSE_BKPT; |
| 127743 | } |
| 127744 | #endif |
| 127745 | sqlite3_mutex_enter(db->mutex); |
| 127746 | if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 127747 | rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 127748 | 0, sqlite3InvalidFunction, 0, 0, 0); |
| 127749 | } |
| @@ -126534,10 +127761,17 @@ | |
| 127761 | ** trace is a pointer to a function that is invoked at the start of each |
| 127762 | ** SQL statement. |
| 127763 | */ |
| 127764 | SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ |
| 127765 | void *pOld; |
| 127766 | |
| 127767 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127768 | if( !sqlite3SafetyCheckOk(db) ){ |
| 127769 | (void)SQLITE_MISUSE_BKPT; |
| 127770 | return 0; |
| 127771 | } |
| 127772 | #endif |
| 127773 | sqlite3_mutex_enter(db->mutex); |
| 127774 | pOld = db->pTraceArg; |
| 127775 | db->xTrace = xTrace; |
| 127776 | db->pTraceArg = pArg; |
| 127777 | sqlite3_mutex_leave(db->mutex); |
| @@ -126555,10 +127789,17 @@ | |
| 127789 | sqlite3 *db, |
| 127790 | void (*xProfile)(void*,const char*,sqlite_uint64), |
| 127791 | void *pArg |
| 127792 | ){ |
| 127793 | void *pOld; |
| 127794 | |
| 127795 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127796 | if( !sqlite3SafetyCheckOk(db) ){ |
| 127797 | (void)SQLITE_MISUSE_BKPT; |
| 127798 | return 0; |
| 127799 | } |
| 127800 | #endif |
| 127801 | sqlite3_mutex_enter(db->mutex); |
| 127802 | pOld = db->pProfileArg; |
| 127803 | db->xProfile = xProfile; |
| 127804 | db->pProfileArg = pArg; |
| 127805 | sqlite3_mutex_leave(db->mutex); |
| @@ -126575,10 +127816,17 @@ | |
| 127816 | sqlite3 *db, /* Attach the hook to this database */ |
| 127817 | int (*xCallback)(void*), /* Function to invoke on each commit */ |
| 127818 | void *pArg /* Argument to the function */ |
| 127819 | ){ |
| 127820 | void *pOld; |
| 127821 | |
| 127822 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127823 | if( !sqlite3SafetyCheckOk(db) ){ |
| 127824 | (void)SQLITE_MISUSE_BKPT; |
| 127825 | return 0; |
| 127826 | } |
| 127827 | #endif |
| 127828 | sqlite3_mutex_enter(db->mutex); |
| 127829 | pOld = db->pCommitArg; |
| 127830 | db->xCommitCallback = xCallback; |
| 127831 | db->pCommitArg = pArg; |
| 127832 | sqlite3_mutex_leave(db->mutex); |
| @@ -126593,10 +127841,17 @@ | |
| 127841 | sqlite3 *db, /* Attach the hook to this database */ |
| 127842 | void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), |
| 127843 | void *pArg /* Argument to the function */ |
| 127844 | ){ |
| 127845 | void *pRet; |
| 127846 | |
| 127847 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127848 | if( !sqlite3SafetyCheckOk(db) ){ |
| 127849 | (void)SQLITE_MISUSE_BKPT; |
| 127850 | return 0; |
| 127851 | } |
| 127852 | #endif |
| 127853 | sqlite3_mutex_enter(db->mutex); |
| 127854 | pRet = db->pUpdateArg; |
| 127855 | db->xUpdateCallback = xCallback; |
| 127856 | db->pUpdateArg = pArg; |
| 127857 | sqlite3_mutex_leave(db->mutex); |
| @@ -126611,10 +127866,17 @@ | |
| 127866 | sqlite3 *db, /* Attach the hook to this database */ |
| 127867 | void (*xCallback)(void*), /* Callback function */ |
| 127868 | void *pArg /* Argument to the function */ |
| 127869 | ){ |
| 127870 | void *pRet; |
| 127871 | |
| 127872 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127873 | if( !sqlite3SafetyCheckOk(db) ){ |
| 127874 | (void)SQLITE_MISUSE_BKPT; |
| 127875 | return 0; |
| 127876 | } |
| 127877 | #endif |
| 127878 | sqlite3_mutex_enter(db->mutex); |
| 127879 | pRet = db->pRollbackArg; |
| 127880 | db->xRollbackCallback = xCallback; |
| 127881 | db->pRollbackArg = pArg; |
| 127882 | sqlite3_mutex_leave(db->mutex); |
| @@ -126657,10 +127919,13 @@ | |
| 127919 | SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ |
| 127920 | #ifdef SQLITE_OMIT_WAL |
| 127921 | UNUSED_PARAMETER(db); |
| 127922 | UNUSED_PARAMETER(nFrame); |
| 127923 | #else |
| 127924 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127925 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 127926 | #endif |
| 127927 | if( nFrame>0 ){ |
| 127928 | sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); |
| 127929 | }else{ |
| 127930 | sqlite3_wal_hook(db, 0, 0); |
| 127931 | } |
| @@ -126677,10 +127942,16 @@ | |
| 127942 | int(*xCallback)(void *, sqlite3*, const char*, int), |
| 127943 | void *pArg /* First argument passed to xCallback() */ |
| 127944 | ){ |
| 127945 | #ifndef SQLITE_OMIT_WAL |
| 127946 | void *pRet; |
| 127947 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127948 | if( !sqlite3SafetyCheckOk(db) ){ |
| 127949 | (void)SQLITE_MISUSE_BKPT; |
| 127950 | return 0; |
| 127951 | } |
| 127952 | #endif |
| 127953 | sqlite3_mutex_enter(db->mutex); |
| 127954 | pRet = db->pWalArg; |
| 127955 | db->xWalCallback = xCallback; |
| 127956 | db->pWalArg = pArg; |
| 127957 | sqlite3_mutex_leave(db->mutex); |
| @@ -126703,10 +127974,14 @@ | |
| 127974 | #ifdef SQLITE_OMIT_WAL |
| 127975 | return SQLITE_OK; |
| 127976 | #else |
| 127977 | int rc; /* Return code */ |
| 127978 | int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */ |
| 127979 | |
| 127980 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 127981 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 127982 | #endif |
| 127983 | |
| 127984 | /* Initialize the output variables to -1 in case an error occurs. */ |
| 127985 | if( pnLog ) *pnLog = -1; |
| 127986 | if( pnCkpt ) *pnCkpt = -1; |
| 127987 | |
| @@ -127100,10 +128375,16 @@ | |
| 128375 | ** from forming. |
| 128376 | */ |
| 128377 | SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ |
| 128378 | int oldLimit; |
| 128379 | |
| 128380 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 128381 | if( !sqlite3SafetyCheckOk(db) ){ |
| 128382 | (void)SQLITE_MISUSE_BKPT; |
| 128383 | return -1; |
| 128384 | } |
| 128385 | #endif |
| 128386 | |
| 128387 | /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME |
| 128388 | ** there is a hard upper bound set at compile-time by a C preprocessor |
| 128389 | ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to |
| 128390 | ** "_MAX_".) |
| @@ -127176,11 +128457,12 @@ | |
| 128457 | char c; |
| 128458 | int nUri = sqlite3Strlen30(zUri); |
| 128459 | |
| 128460 | assert( *pzErrMsg==0 ); |
| 128461 | |
| 128462 | if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */ |
| 128463 | || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */ |
| 128464 | && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ |
| 128465 | ){ |
| 128466 | char *zOpt; |
| 128467 | int eState; /* Parser state when parsing URI */ |
| 128468 | int iIn; /* Input character index */ |
| @@ -127385,10 +128667,13 @@ | |
| 128667 | int rc; /* Return code */ |
| 128668 | int isThreadsafe; /* True for threadsafe connections */ |
| 128669 | char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ |
| 128670 | char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ |
| 128671 | |
| 128672 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 128673 | if( ppDb==0 ) return SQLITE_MISUSE_BKPT; |
| 128674 | #endif |
| 128675 | *ppDb = 0; |
| 128676 | #ifndef SQLITE_OMIT_AUTOINIT |
| 128677 | rc = sqlite3_initialize(); |
| 128678 | if( rc ) return rc; |
| 128679 | #endif |
| @@ -127499,24 +128784,28 @@ | |
| 128784 | #endif |
| 128785 | |
| 128786 | /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
| 128787 | ** and UTF-16, so add a version for each to avoid any unnecessary |
| 128788 | ** conversions. The only error that can occur here is a malloc() failure. |
| 128789 | ** |
| 128790 | ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating |
| 128791 | ** functions: |
| 128792 | */ |
| 128793 | createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0); |
| 128794 | createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0); |
| 128795 | createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0); |
| 128796 | createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); |
| 128797 | createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); |
| 128798 | if( db->mallocFailed ){ |
| 128799 | goto opendb_out; |
| 128800 | } |
| 128801 | /* EVIDENCE-OF: R-08308-17224 The default collating function for all |
| 128802 | ** strings is BINARY. |
| 128803 | */ |
| 128804 | db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); |
| 128805 | assert( db->pDfltColl!=0 ); |
| 128806 | |
| 128807 | /* Parse the filename/URI argument. */ |
| 128808 | db->openFlags = flags; |
| 128809 | rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); |
| 128810 | if( rc!=SQLITE_OK ){ |
| 128811 | if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; |
| @@ -127674,17 +128963,19 @@ | |
| 128963 | ){ |
| 128964 | char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
| 128965 | sqlite3_value *pVal; |
| 128966 | int rc; |
| 128967 | |
| 128968 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 128969 | if( ppDb==0 ) return SQLITE_MISUSE_BKPT; |
| 128970 | #endif |
| 128971 | *ppDb = 0; |
| 128972 | #ifndef SQLITE_OMIT_AUTOINIT |
| 128973 | rc = sqlite3_initialize(); |
| 128974 | if( rc ) return rc; |
| 128975 | #endif |
| 128976 | if( zFilename==0 ) zFilename = "\000\000"; |
| 128977 | pVal = sqlite3ValueNew(0); |
| 128978 | sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 128979 | zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
| 128980 | if( zFilename8 ){ |
| 128981 | rc = openDatabase(zFilename8, ppDb, |
| @@ -127710,17 +129001,11 @@ | |
| 129001 | const char *zName, |
| 129002 | int enc, |
| 129003 | void* pCtx, |
| 129004 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 129005 | ){ |
| 129006 | return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0); |
| 129007 | } |
| 129008 | |
| 129009 | /* |
| 129010 | ** Register a new collation sequence with the database handle db. |
| 129011 | */ |
| @@ -127731,10 +129016,14 @@ | |
| 129016 | void* pCtx, |
| 129017 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 129018 | void(*xDel)(void*) |
| 129019 | ){ |
| 129020 | int rc; |
| 129021 | |
| 129022 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 129023 | if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 129024 | #endif |
| 129025 | sqlite3_mutex_enter(db->mutex); |
| 129026 | assert( !db->mallocFailed ); |
| 129027 | rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel); |
| 129028 | rc = sqlite3ApiExit(db, rc); |
| 129029 | sqlite3_mutex_leave(db->mutex); |
| @@ -127752,10 +129041,14 @@ | |
| 129041 | void* pCtx, |
| 129042 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 129043 | ){ |
| 129044 | int rc = SQLITE_OK; |
| 129045 | char *zName8; |
| 129046 | |
| 129047 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 129048 | if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 129049 | #endif |
| 129050 | sqlite3_mutex_enter(db->mutex); |
| 129051 | assert( !db->mallocFailed ); |
| 129052 | zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); |
| 129053 | if( zName8 ){ |
| 129054 | rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0); |
| @@ -127774,10 +129067,13 @@ | |
| 129067 | SQLITE_API int sqlite3_collation_needed( |
| 129068 | sqlite3 *db, |
| 129069 | void *pCollNeededArg, |
| 129070 | void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) |
| 129071 | ){ |
| 129072 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 129073 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 129074 | #endif |
| 129075 | sqlite3_mutex_enter(db->mutex); |
| 129076 | db->xCollNeeded = xCollNeeded; |
| 129077 | db->xCollNeeded16 = 0; |
| 129078 | db->pCollNeededArg = pCollNeededArg; |
| 129079 | sqlite3_mutex_leave(db->mutex); |
| @@ -127792,10 +129088,13 @@ | |
| 129088 | SQLITE_API int sqlite3_collation_needed16( |
| 129089 | sqlite3 *db, |
| 129090 | void *pCollNeededArg, |
| 129091 | void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) |
| 129092 | ){ |
| 129093 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 129094 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 129095 | #endif |
| 129096 | sqlite3_mutex_enter(db->mutex); |
| 129097 | db->xCollNeeded = 0; |
| 129098 | db->xCollNeeded16 = xCollNeeded16; |
| 129099 | db->pCollNeededArg = pCollNeededArg; |
| 129100 | sqlite3_mutex_leave(db->mutex); |
| @@ -127818,10 +129117,16 @@ | |
| 129117 | ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 129118 | ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 129119 | ** by the next COMMIT or ROLLBACK. |
| 129120 | */ |
| 129121 | SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){ |
| 129122 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 129123 | if( !sqlite3SafetyCheckOk(db) ){ |
| 129124 | (void)SQLITE_MISUSE_BKPT; |
| 129125 | return 0; |
| 129126 | } |
| 129127 | #endif |
| 129128 | return db->autoCommit; |
| 129129 | } |
| 129130 | |
| 129131 | /* |
| 129132 | ** The following routines are substitutes for constants SQLITE_CORRUPT, |
| @@ -128000,10 +129305,13 @@ | |
| 129305 | |
| 129306 | /* |
| 129307 | ** Enable or disable the extended result codes. |
| 129308 | */ |
| 129309 | SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ |
| 129310 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 129311 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 129312 | #endif |
| 129313 | sqlite3_mutex_enter(db->mutex); |
| 129314 | db->errMask = onoff ? 0xffffffff : 0xff; |
| 129315 | sqlite3_mutex_leave(db->mutex); |
| 129316 | return SQLITE_OK; |
| 129317 | } |
| @@ -128013,10 +129321,13 @@ | |
| 129321 | */ |
| 129322 | SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ |
| 129323 | int rc = SQLITE_ERROR; |
| 129324 | Btree *pBtree; |
| 129325 | |
| 129326 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 129327 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 129328 | #endif |
| 129329 | sqlite3_mutex_enter(db->mutex); |
| 129330 | pBtree = sqlite3DbNameToBtree(db, zDbName); |
| 129331 | if( pBtree ){ |
| 129332 | Pager *pPager; |
| 129333 | sqlite3_file *fd; |
| @@ -128355,11 +129666,11 @@ | |
| 129666 | ** query parameter we seek. This routine returns the value of the zParam |
| 129667 | ** parameter if it exists. If the parameter does not exist, this routine |
| 129668 | ** returns a NULL pointer. |
| 129669 | */ |
| 129670 | SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){ |
| 129671 | if( zFilename==0 || zParam==0 ) return 0; |
| 129672 | zFilename += sqlite3Strlen30(zFilename) + 1; |
| 129673 | while( zFilename[0] ){ |
| 129674 | int x = strcmp(zFilename, zParam); |
| 129675 | zFilename += sqlite3Strlen30(zFilename) + 1; |
| 129676 | if( x==0 ) return zFilename; |
| @@ -128411,19 +129722,31 @@ | |
| 129722 | /* |
| 129723 | ** Return the filename of the database associated with a database |
| 129724 | ** connection. |
| 129725 | */ |
| 129726 | SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){ |
| 129727 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 129728 | if( !sqlite3SafetyCheckOk(db) ){ |
| 129729 | (void)SQLITE_MISUSE_BKPT; |
| 129730 | return 0; |
| 129731 | } |
| 129732 | #endif |
| 129733 | Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 129734 | return pBt ? sqlite3BtreeGetFilename(pBt) : 0; |
| 129735 | } |
| 129736 | |
| 129737 | /* |
| 129738 | ** Return 1 if database is read-only or 0 if read/write. Return -1 if |
| 129739 | ** no such database exists. |
| 129740 | */ |
| 129741 | SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){ |
| 129742 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 129743 | if( !sqlite3SafetyCheckOk(db) ){ |
| 129744 | (void)SQLITE_MISUSE_BKPT; |
| 129745 | return -1; |
| 129746 | } |
| 129747 | #endif |
| 129748 | Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 129749 | return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; |
| 129750 | } |
| 129751 | |
| 129752 | /************** End of main.c ************************************************/ |
| 129753 |
+353
-227
| --- src/sqlite3.h | ||
| +++ src/sqlite3.h | ||
| @@ -55,11 +55,11 @@ | ||
| 55 | 55 | |
| 56 | 56 | |
| 57 | 57 | /* |
| 58 | 58 | ** These no-op macros are used in front of interfaces to mark those |
| 59 | 59 | ** interfaces as either deprecated or experimental. New applications |
| 60 | -** should not use deprecated interfaces - they are support for backwards | |
| 60 | +** should not use deprecated interfaces - they are supported for backwards | |
| 61 | 61 | ** compatibility only. Application writers should be aware that |
| 62 | 62 | ** experimental interfaces are subject to change in point releases. |
| 63 | 63 | ** |
| 64 | 64 | ** These macros used to resolve to various kinds of compiler magic that |
| 65 | 65 | ** would generate warning messages when they were used. But that |
| @@ -105,13 +105,13 @@ | ||
| 105 | 105 | ** |
| 106 | 106 | ** See also: [sqlite3_libversion()], |
| 107 | 107 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 108 | 108 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 109 | 109 | */ |
| 110 | -#define SQLITE_VERSION "3.8.7.2" | |
| 111 | -#define SQLITE_VERSION_NUMBER 3008007 | |
| 112 | -#define SQLITE_SOURCE_ID "2014-11-18 20:57:56 2ab564bf9655b7c7b97ab85cafc8a48329b27f93" | |
| 110 | +#define SQLITE_VERSION "3.8.8" | |
| 111 | +#define SQLITE_VERSION_NUMBER 3008008 | |
| 112 | +#define SQLITE_SOURCE_ID "2014-11-27 11:36:36 f095cde579e7417306e11b5c1d2dd90b6bb547d5" | |
| 113 | 113 | |
| 114 | 114 | /* |
| 115 | 115 | ** CAPI3REF: Run-Time Library Version Numbers |
| 116 | 116 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 117 | 117 | ** |
| @@ -1502,29 +1502,31 @@ | ||
| 1502 | 1502 | ** it is not possible to set the Serialized [threading mode] and |
| 1503 | 1503 | ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the |
| 1504 | 1504 | ** SQLITE_CONFIG_SERIALIZED configuration option.</dd> |
| 1505 | 1505 | ** |
| 1506 | 1506 | ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt> |
| 1507 | -** <dd> ^(This option takes a single argument which is a pointer to an | |
| 1508 | -** instance of the [sqlite3_mem_methods] structure. The argument specifies | |
| 1507 | +** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is | |
| 1508 | +** a pointer to an instance of the [sqlite3_mem_methods] structure. | |
| 1509 | +** The argument specifies | |
| 1509 | 1510 | ** alternative low-level memory allocation routines to be used in place of |
| 1510 | 1511 | ** the memory allocation routines built into SQLite.)^ ^SQLite makes |
| 1511 | 1512 | ** its own private copy of the content of the [sqlite3_mem_methods] structure |
| 1512 | 1513 | ** before the [sqlite3_config()] call returns.</dd> |
| 1513 | 1514 | ** |
| 1514 | 1515 | ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt> |
| 1515 | -** <dd> ^(This option takes a single argument which is a pointer to an | |
| 1516 | -** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods] | |
| 1516 | +** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which | |
| 1517 | +** is a pointer to an instance of the [sqlite3_mem_methods] structure. | |
| 1518 | +** The [sqlite3_mem_methods] | |
| 1517 | 1519 | ** structure is filled with the currently defined memory allocation routines.)^ |
| 1518 | 1520 | ** This option can be used to overload the default memory allocation |
| 1519 | 1521 | ** routines with a wrapper that simulations memory allocation failure or |
| 1520 | 1522 | ** tracks memory usage, for example. </dd> |
| 1521 | 1523 | ** |
| 1522 | 1524 | ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt> |
| 1523 | -** <dd> ^This option takes single argument of type int, interpreted as a | |
| 1524 | -** boolean, which enables or disables the collection of memory allocation | |
| 1525 | -** statistics. ^(When memory allocation statistics are disabled, the | |
| 1525 | +** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int, | |
| 1526 | +** interpreted as a boolean, which enables or disables the collection of | |
| 1527 | +** memory allocation statistics. ^(When memory allocation statistics are disabled, the | |
| 1526 | 1528 | ** following SQLite interfaces become non-operational: |
| 1527 | 1529 | ** <ul> |
| 1528 | 1530 | ** <li> [sqlite3_memory_used()] |
| 1529 | 1531 | ** <li> [sqlite3_memory_highwater()] |
| 1530 | 1532 | ** <li> [sqlite3_soft_heap_limit64()] |
| @@ -1534,78 +1536,90 @@ | ||
| 1534 | 1536 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1535 | 1537 | ** allocation statistics are disabled by default. |
| 1536 | 1538 | ** </dd> |
| 1537 | 1539 | ** |
| 1538 | 1540 | ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt> |
| 1539 | -** <dd> ^This option specifies a static memory buffer that SQLite can use for | |
| 1540 | -** scratch memory. There are three arguments: A pointer an 8-byte | |
| 1541 | +** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer | |
| 1542 | +** that SQLite can use for scratch memory. ^(There are three arguments | |
| 1543 | +** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte | |
| 1541 | 1544 | ** aligned memory buffer from which the scratch allocations will be |
| 1542 | 1545 | ** drawn, the size of each scratch allocation (sz), |
| 1543 | -** and the maximum number of scratch allocations (N). The sz | |
| 1544 | -** argument must be a multiple of 16. | |
| 1546 | +** and the maximum number of scratch allocations (N).)^ | |
| 1545 | 1547 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1546 | 1548 | ** of at least sz*N bytes of memory. |
| 1547 | -** ^SQLite will use no more than two scratch buffers per thread. So | |
| 1548 | -** N should be set to twice the expected maximum number of threads. | |
| 1549 | -** ^SQLite will never require a scratch buffer that is more than 6 | |
| 1550 | -** times the database page size. ^If SQLite needs needs additional | |
| 1549 | +** ^SQLite will not use more than one scratch buffers per thread. | |
| 1550 | +** ^SQLite will never request a scratch buffer that is more than 6 | |
| 1551 | +** times the database page size. | |
| 1552 | +** ^If SQLite needs needs additional | |
| 1551 | 1553 | ** scratch memory beyond what is provided by this configuration option, then |
| 1552 | -** [sqlite3_malloc()] will be used to obtain the memory needed.</dd> | |
| 1554 | +** [sqlite3_malloc()] will be used to obtain the memory needed.<p> | |
| 1555 | +** ^When the application provides any amount of scratch memory using | |
| 1556 | +** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large | |
| 1557 | +** [sqlite3_malloc|heap allocations]. | |
| 1558 | +** This can help [Robson proof|prevent memory allocation failures] due to heap | |
| 1559 | +** fragmentation in low-memory embedded systems. | |
| 1560 | +** </dd> | |
| 1553 | 1561 | ** |
| 1554 | 1562 | ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1555 | -** <dd> ^This option specifies a static memory buffer that SQLite can use for | |
| 1556 | -** the database page cache with the default page cache implementation. | |
| 1563 | +** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer | |
| 1564 | +** that SQLite can use for the database page cache with the default page | |
| 1565 | +** cache implementation. | |
| 1557 | 1566 | ** This configuration should not be used if an application-define page |
| 1558 | -** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option. | |
| 1559 | -** There are three arguments to this option: A pointer to 8-byte aligned | |
| 1567 | +** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2] | |
| 1568 | +** configuration option. | |
| 1569 | +** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned | |
| 1560 | 1570 | ** memory, the size of each page buffer (sz), and the number of pages (N). |
| 1561 | 1571 | ** The sz argument should be the size of the largest database page |
| 1562 | -** (a power of two between 512 and 32768) plus a little extra for each | |
| 1563 | -** page header. ^The page header size is 20 to 40 bytes depending on | |
| 1564 | -** the host architecture. ^It is harmless, apart from the wasted memory, | |
| 1565 | -** to make sz a little too large. The first | |
| 1566 | -** argument should point to an allocation of at least sz*N bytes of memory. | |
| 1572 | +** (a power of two between 512 and 32768) plus some extra bytes for each | |
| 1573 | +** page header. ^The number of extra bytes needed by the page header | |
| 1574 | +** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option | |
| 1575 | +** to [sqlite3_config()]. | |
| 1576 | +** ^It is harmless, apart from the wasted memory, | |
| 1577 | +** for the sz parameter to be larger than necessary. The first | |
| 1578 | +** argument should pointer to an 8-byte aligned block of memory that | |
| 1579 | +** is at least sz*N bytes of memory, otherwise subsequent behavior is | |
| 1580 | +** undefined. | |
| 1567 | 1581 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1568 | 1582 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1569 | 1583 | ** page cache memory is needed beyond what is provided by this option, then |
| 1570 | -** SQLite goes to [sqlite3_malloc()] for the additional storage space. | |
| 1571 | -** The pointer in the first argument must | |
| 1572 | -** be aligned to an 8-byte boundary or subsequent behavior of SQLite | |
| 1573 | -** will be undefined.</dd> | |
| 1584 | +** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd> | |
| 1574 | 1585 | ** |
| 1575 | 1586 | ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt> |
| 1576 | -** <dd> ^This option specifies a static memory buffer that SQLite will use | |
| 1577 | -** for all of its dynamic memory allocation needs beyond those provided | |
| 1578 | -** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE]. | |
| 1579 | -** There are three arguments: An 8-byte aligned pointer to the memory, | |
| 1587 | +** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer | |
| 1588 | +** that SQLite will use for all of its dynamic memory allocation needs | |
| 1589 | +** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE]. | |
| 1590 | +** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled | |
| 1591 | +** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns | |
| 1592 | +** [SQLITE_ERROR] if invoked otherwise. | |
| 1593 | +** ^There are three arguments to SQLITE_CONFIG_HEAP: | |
| 1594 | +** An 8-byte aligned pointer to the memory, | |
| 1580 | 1595 | ** the number of bytes in the memory buffer, and the minimum allocation size. |
| 1581 | 1596 | ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts |
| 1582 | 1597 | ** to using its default memory allocator (the system malloc() implementation), |
| 1583 | 1598 | ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the |
| 1584 | -** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or | |
| 1585 | -** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory | |
| 1599 | +** memory pointer is not NULL then the alternative memory | |
| 1586 | 1600 | ** allocator is engaged to handle all of SQLites memory allocation needs. |
| 1587 | 1601 | ** The first pointer (the memory pointer) must be aligned to an 8-byte |
| 1588 | 1602 | ** boundary or subsequent behavior of SQLite will be undefined. |
| 1589 | 1603 | ** The minimum allocation size is capped at 2**12. Reasonable values |
| 1590 | 1604 | ** for the minimum allocation size are 2**5 through 2**8.</dd> |
| 1591 | 1605 | ** |
| 1592 | 1606 | ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt> |
| 1593 | -** <dd> ^(This option takes a single argument which is a pointer to an | |
| 1594 | -** instance of the [sqlite3_mutex_methods] structure. The argument specifies | |
| 1595 | -** alternative low-level mutex routines to be used in place | |
| 1607 | +** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a | |
| 1608 | +** pointer to an instance of the [sqlite3_mutex_methods] structure. | |
| 1609 | +** The argument specifies alternative low-level mutex routines to be used in place | |
| 1596 | 1610 | ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the |
| 1597 | 1611 | ** content of the [sqlite3_mutex_methods] structure before the call to |
| 1598 | 1612 | ** [sqlite3_config()] returns. ^If SQLite is compiled with |
| 1599 | 1613 | ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then |
| 1600 | 1614 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1601 | 1615 | ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will |
| 1602 | 1616 | ** return [SQLITE_ERROR].</dd> |
| 1603 | 1617 | ** |
| 1604 | 1618 | ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt> |
| 1605 | -** <dd> ^(This option takes a single argument which is a pointer to an | |
| 1606 | -** instance of the [sqlite3_mutex_methods] structure. The | |
| 1619 | +** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which | |
| 1620 | +** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The | |
| 1607 | 1621 | ** [sqlite3_mutex_methods] |
| 1608 | 1622 | ** structure is filled with the currently defined mutex routines.)^ |
| 1609 | 1623 | ** This option can be used to overload the default mutex allocation |
| 1610 | 1624 | ** routines with a wrapper used to track mutex usage for performance |
| 1611 | 1625 | ** profiling or testing, for example. ^If SQLite is compiled with |
| @@ -1613,28 +1627,28 @@ | ||
| 1613 | 1627 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1614 | 1628 | ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will |
| 1615 | 1629 | ** return [SQLITE_ERROR].</dd> |
| 1616 | 1630 | ** |
| 1617 | 1631 | ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt> |
| 1618 | -** <dd> ^(This option takes two arguments that determine the default | |
| 1619 | -** memory allocation for the lookaside memory allocator on each | |
| 1620 | -** [database connection]. The first argument is the | |
| 1632 | +** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine | |
| 1633 | +** the default size of lookaside memory on each [database connection]. | |
| 1634 | +** The first argument is the | |
| 1621 | 1635 | ** size of each lookaside buffer slot and the second is the number of |
| 1622 | -** slots allocated to each database connection.)^ ^(This option sets the | |
| 1623 | -** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] | |
| 1624 | -** verb to [sqlite3_db_config()] can be used to change the lookaside | |
| 1636 | +** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE | |
| 1637 | +** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] | |
| 1638 | +** option to [sqlite3_db_config()] can be used to change the lookaside | |
| 1625 | 1639 | ** configuration on individual connections.)^ </dd> |
| 1626 | 1640 | ** |
| 1627 | 1641 | ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt> |
| 1628 | -** <dd> ^(This option takes a single argument which is a pointer to | |
| 1629 | -** an [sqlite3_pcache_methods2] object. This object specifies the interface | |
| 1630 | -** to a custom page cache implementation.)^ ^SQLite makes a copy of the | |
| 1631 | -** object and uses it for page cache memory allocations.</dd> | |
| 1642 | +** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is | |
| 1643 | +** a pointer to an [sqlite3_pcache_methods2] object. This object specifies | |
| 1644 | +** the interface to a custom page cache implementation.)^ | |
| 1645 | +** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd> | |
| 1632 | 1646 | ** |
| 1633 | 1647 | ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt> |
| 1634 | -** <dd> ^(This option takes a single argument which is a pointer to an | |
| 1635 | -** [sqlite3_pcache_methods2] object. SQLite copies of the current | |
| 1648 | +** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which | |
| 1649 | +** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of the current | |
| 1636 | 1650 | ** page cache implementation into that object.)^ </dd> |
| 1637 | 1651 | ** |
| 1638 | 1652 | ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt> |
| 1639 | 1653 | ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite |
| 1640 | 1654 | ** global [error log]. |
| @@ -1654,26 +1668,27 @@ | ||
| 1654 | 1668 | ** supplied by the application must not invoke any SQLite interface. |
| 1655 | 1669 | ** In a multi-threaded application, the application-defined logger |
| 1656 | 1670 | ** function must be threadsafe. </dd> |
| 1657 | 1671 | ** |
| 1658 | 1672 | ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI |
| 1659 | -** <dd>^(This option takes a single argument of type int. If non-zero, then | |
| 1660 | -** URI handling is globally enabled. If the parameter is zero, then URI handling | |
| 1661 | -** is globally disabled.)^ ^If URI handling is globally enabled, all filenames | |
| 1662 | -** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or | |
| 1673 | +** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int. | |
| 1674 | +** If non-zero, then URI handling is globally enabled. If the parameter is zero, | |
| 1675 | +** then URI handling is globally disabled.)^ ^If URI handling is globally enabled, | |
| 1676 | +** all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or | |
| 1663 | 1677 | ** specified as part of [ATTACH] commands are interpreted as URIs, regardless |
| 1664 | 1678 | ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database |
| 1665 | 1679 | ** connection is opened. ^If it is globally disabled, filenames are |
| 1666 | 1680 | ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the |
| 1667 | 1681 | ** database connection is opened. ^(By default, URI handling is globally |
| 1668 | 1682 | ** disabled. The default value may be changed by compiling with the |
| 1669 | 1683 | ** [SQLITE_USE_URI] symbol defined.)^ |
| 1670 | 1684 | ** |
| 1671 | 1685 | ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN |
| 1672 | -** <dd>^This option takes a single integer argument which is interpreted as | |
| 1673 | -** a boolean in order to enable or disable the use of covering indices for | |
| 1674 | -** full table scans in the query optimizer. ^The default setting is determined | |
| 1686 | +** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer | |
| 1687 | +** argument which is interpreted as a boolean in order to enable or disable | |
| 1688 | +** the use of covering indices for full table scans in the query optimizer. | |
| 1689 | +** ^The default setting is determined | |
| 1675 | 1690 | ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on" |
| 1676 | 1691 | ** if that compile-time option is omitted. |
| 1677 | 1692 | ** The ability to disable the use of covering indices for full table scans |
| 1678 | 1693 | ** is because some incorrectly coded legacy applications might malfunction |
| 1679 | 1694 | ** when the optimization is enabled. Providing the ability to |
| @@ -1709,23 +1724,32 @@ | ||
| 1709 | 1724 | ** that are the default mmap size limit (the default setting for |
| 1710 | 1725 | ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit. |
| 1711 | 1726 | ** ^The default setting can be overridden by each database connection using |
| 1712 | 1727 | ** either the [PRAGMA mmap_size] command, or by using the |
| 1713 | 1728 | ** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size |
| 1714 | -** cannot be changed at run-time. Nor may the maximum allowed mmap size | |
| 1715 | -** exceed the compile-time maximum mmap size set by the | |
| 1729 | +** will be silently truncated if necessary so that it does not exceed the | |
| 1730 | +** compile-time maximum mmap size set by the | |
| 1716 | 1731 | ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^ |
| 1717 | 1732 | ** ^If either argument to this option is negative, then that argument is |
| 1718 | 1733 | ** changed to its compile-time default. |
| 1719 | 1734 | ** |
| 1720 | 1735 | ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]] |
| 1721 | 1736 | ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE |
| 1722 | -** <dd>^This option is only available if SQLite is compiled for Windows | |
| 1723 | -** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined. | |
| 1724 | -** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value | |
| 1737 | +** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is | |
| 1738 | +** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro defined. | |
| 1739 | +** ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value | |
| 1725 | 1740 | ** that specifies the maximum size of the created heap. |
| 1726 | 1741 | ** </dl> |
| 1742 | +** | |
| 1743 | +** [[SQLITE_CONFIG_PCACHE_HDRSZ]] | |
| 1744 | +** <dt>SQLITE_CONFIG_PCACHE_HDRSZ | |
| 1745 | +** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which | |
| 1746 | +** is a pointer to an integer and writes into that integer the number of extra | |
| 1747 | +** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. The amount of | |
| 1748 | +** extra space required can change depending on the compiler, | |
| 1749 | +** target platform, and SQLite version. | |
| 1750 | +** </dl> | |
| 1727 | 1751 | */ |
| 1728 | 1752 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 1729 | 1753 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 1730 | 1754 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| 1731 | 1755 | #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ |
| @@ -1746,10 +1770,11 @@ | ||
| 1746 | 1770 | #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ |
| 1747 | 1771 | #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ |
| 1748 | 1772 | #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ |
| 1749 | 1773 | #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ |
| 1750 | 1774 | #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ |
| 1775 | +#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */ | |
| 1751 | 1776 | |
| 1752 | 1777 | /* |
| 1753 | 1778 | ** CAPI3REF: Database Connection Configuration Options |
| 1754 | 1779 | ** |
| 1755 | 1780 | ** These constants are the available integer configuration options that |
| @@ -1873,51 +1898,49 @@ | ||
| 1873 | 1898 | SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); |
| 1874 | 1899 | |
| 1875 | 1900 | /* |
| 1876 | 1901 | ** CAPI3REF: Count The Number Of Rows Modified |
| 1877 | 1902 | ** |
| 1878 | -** ^This function returns the number of database rows that were changed | |
| 1879 | -** or inserted or deleted by the most recently completed SQL statement | |
| 1880 | -** on the [database connection] specified by the first parameter. | |
| 1881 | -** ^(Only changes that are directly specified by the [INSERT], [UPDATE], | |
| 1882 | -** or [DELETE] statement are counted. Auxiliary changes caused by | |
| 1883 | -** triggers or [foreign key actions] are not counted.)^ Use the | |
| 1884 | -** [sqlite3_total_changes()] function to find the total number of changes | |
| 1885 | -** including changes caused by triggers and foreign key actions. | |
| 1886 | -** | |
| 1887 | -** ^Changes to a view that are simulated by an [INSTEAD OF trigger] | |
| 1888 | -** are not counted. Only real table changes are counted. | |
| 1889 | -** | |
| 1890 | -** ^(A "row change" is a change to a single row of a single table | |
| 1891 | -** caused by an INSERT, DELETE, or UPDATE statement. Rows that | |
| 1892 | -** are changed as side effects of [REPLACE] constraint resolution, | |
| 1893 | -** rollback, ABORT processing, [DROP TABLE], or by any other | |
| 1894 | -** mechanisms do not count as direct row changes.)^ | |
| 1895 | -** | |
| 1896 | -** A "trigger context" is a scope of execution that begins and | |
| 1897 | -** ends with the script of a [CREATE TRIGGER | trigger]. | |
| 1898 | -** Most SQL statements are | |
| 1899 | -** evaluated outside of any trigger. This is the "top level" | |
| 1900 | -** trigger context. If a trigger fires from the top level, a | |
| 1901 | -** new trigger context is entered for the duration of that one | |
| 1902 | -** trigger. Subtriggers create subcontexts for their duration. | |
| 1903 | -** | |
| 1904 | -** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does | |
| 1905 | -** not create a new trigger context. | |
| 1906 | -** | |
| 1907 | -** ^This function returns the number of direct row changes in the | |
| 1908 | -** most recent INSERT, UPDATE, or DELETE statement within the same | |
| 1909 | -** trigger context. | |
| 1910 | -** | |
| 1911 | -** ^Thus, when called from the top level, this function returns the | |
| 1912 | -** number of changes in the most recent INSERT, UPDATE, or DELETE | |
| 1913 | -** that also occurred at the top level. ^(Within the body of a trigger, | |
| 1914 | -** the sqlite3_changes() interface can be called to find the number of | |
| 1915 | -** changes in the most recently completed INSERT, UPDATE, or DELETE | |
| 1916 | -** statement within the body of the same trigger. | |
| 1917 | -** However, the number returned does not include changes | |
| 1918 | -** caused by subtriggers since those have their own context.)^ | |
| 1903 | +** ^This function returns the number of rows modified, inserted or | |
| 1904 | +** deleted by the most recently completed INSERT, UPDATE or DELETE | |
| 1905 | +** statement on the database connection specified by the only parameter. | |
| 1906 | +** ^Executing any other type of SQL statement does not modify the value | |
| 1907 | +** returned by this function. | |
| 1908 | +** | |
| 1909 | +** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are | |
| 1910 | +** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], | |
| 1911 | +** [foreign key actions] or [REPLACE] constraint resolution are not counted. | |
| 1912 | +** | |
| 1913 | +** Changes to a view that are intercepted by | |
| 1914 | +** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value | |
| 1915 | +** returned by sqlite3_changes() immediately after an INSERT, UPDATE or | |
| 1916 | +** DELETE statement run on a view is always zero. Only changes made to real | |
| 1917 | +** tables are counted. | |
| 1918 | +** | |
| 1919 | +** Things are more complicated if the sqlite3_changes() function is | |
| 1920 | +** executed while a trigger program is running. This may happen if the | |
| 1921 | +** program uses the [changes() SQL function], or if some other callback | |
| 1922 | +** function invokes sqlite3_changes() directly. Essentially: | |
| 1923 | +** | |
| 1924 | +** <ul> | |
| 1925 | +** <li> ^(Before entering a trigger program the value returned by | |
| 1926 | +** sqlite3_changes() function is saved. After the trigger program | |
| 1927 | +** has finished, the original value is restored.)^ | |
| 1928 | +** | |
| 1929 | +** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE | |
| 1930 | +** statement sets the value returned by sqlite3_changes() | |
| 1931 | +** upon completion as normal. Of course, this value will not include | |
| 1932 | +** any changes performed by sub-triggers, as the sqlite3_changes() | |
| 1933 | +** value will be saved and restored after each sub-trigger has run.)^ | |
| 1934 | +** </ul> | |
| 1935 | +** | |
| 1936 | +** ^This means that if the changes() SQL function (or similar) is used | |
| 1937 | +** by the first INSERT, UPDATE or DELETE statement within a trigger, it | |
| 1938 | +** returns the value as set when the calling statement began executing. | |
| 1939 | +** ^If it is used by the second or subsequent such statement within a trigger | |
| 1940 | +** program, the value returned reflects the number of rows modified by the | |
| 1941 | +** previous INSERT, UPDATE or DELETE statement within the same trigger. | |
| 1919 | 1942 | ** |
| 1920 | 1943 | ** See also the [sqlite3_total_changes()] interface, the |
| 1921 | 1944 | ** [count_changes pragma], and the [changes() SQL function]. |
| 1922 | 1945 | ** |
| 1923 | 1946 | ** If a separate thread makes changes on the same database connection |
| @@ -1927,24 +1950,21 @@ | ||
| 1927 | 1950 | SQLITE_API int sqlite3_changes(sqlite3*); |
| 1928 | 1951 | |
| 1929 | 1952 | /* |
| 1930 | 1953 | ** CAPI3REF: Total Number Of Rows Modified |
| 1931 | 1954 | ** |
| 1932 | -** ^This function returns the number of row changes caused by [INSERT], | |
| 1933 | -** [UPDATE] or [DELETE] statements since the [database connection] was opened. | |
| 1934 | -** ^(The count returned by sqlite3_total_changes() includes all changes | |
| 1935 | -** from all [CREATE TRIGGER | trigger] contexts and changes made by | |
| 1936 | -** [foreign key actions]. However, | |
| 1937 | -** the count does not include changes used to implement [REPLACE] constraints, | |
| 1938 | -** do rollbacks or ABORT processing, or [DROP TABLE] processing. The | |
| 1939 | -** count does not include rows of views that fire an [INSTEAD OF trigger], | |
| 1940 | -** though if the INSTEAD OF trigger makes changes of its own, those changes | |
| 1941 | -** are counted.)^ | |
| 1942 | -** ^The sqlite3_total_changes() function counts the changes as soon as | |
| 1943 | -** the statement that makes them is completed (when the statement handle | |
| 1944 | -** is passed to [sqlite3_reset()] or [sqlite3_finalize()]). | |
| 1945 | -** | |
| 1955 | +** ^This function returns the total number of rows inserted, modified or | |
| 1956 | +** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed | |
| 1957 | +** since the database connection was opened, including those executed as | |
| 1958 | +** part of trigger programs. ^Executing any other type of SQL statement | |
| 1959 | +** does not affect the value returned by sqlite3_total_changes(). | |
| 1960 | +** | |
| 1961 | +** ^Changes made as part of [foreign key actions] are included in the | |
| 1962 | +** count, but those made as part of REPLACE constraint resolution are | |
| 1963 | +** not. ^Changes to a view that are intercepted by INSTEAD OF triggers | |
| 1964 | +** are not counted. | |
| 1965 | +** | |
| 1946 | 1966 | ** See also the [sqlite3_changes()] interface, the |
| 1947 | 1967 | ** [count_changes pragma], and the [total_changes() SQL function]. |
| 1948 | 1968 | ** |
| 1949 | 1969 | ** If a separate thread makes changes on the same database connection |
| 1950 | 1970 | ** while [sqlite3_total_changes()] is running then the value |
| @@ -2418,17 +2438,18 @@ | ||
| 2418 | 2438 | ** already uses the largest possible [ROWID]. The PRNG is also used for |
| 2419 | 2439 | ** the build-in random() and randomblob() SQL functions. This interface allows |
| 2420 | 2440 | ** applications to access the same PRNG for other purposes. |
| 2421 | 2441 | ** |
| 2422 | 2442 | ** ^A call to this routine stores N bytes of randomness into buffer P. |
| 2423 | -** ^If N is less than one, then P can be a NULL pointer. | |
| 2443 | +** ^The P parameter can be a NULL pointer. | |
| 2424 | 2444 | ** |
| 2425 | 2445 | ** ^If this routine has not been previously called or if the previous |
| 2426 | -** call had N less than one, then the PRNG is seeded using randomness | |
| 2427 | -** obtained from the xRandomness method of the default [sqlite3_vfs] object. | |
| 2428 | -** ^If the previous call to this routine had an N of 1 or more then | |
| 2429 | -** the pseudo-randomness is generated | |
| 2446 | +** call had N less than one or a NULL pointer for P, then the PRNG is | |
| 2447 | +** seeded using randomness obtained from the xRandomness method of | |
| 2448 | +** the default [sqlite3_vfs] object. | |
| 2449 | +** ^If the previous call to this routine had an N of 1 or more and a | |
| 2450 | +** non-NULL P then the pseudo-randomness is generated | |
| 2430 | 2451 | ** internally and without recourse to the [sqlite3_vfs] xRandomness |
| 2431 | 2452 | ** method. |
| 2432 | 2453 | */ |
| 2433 | 2454 | SQLITE_API void sqlite3_randomness(int N, void *P); |
| 2434 | 2455 | |
| @@ -4146,13 +4167,13 @@ | ||
| 4146 | 4167 | ** CAPI3REF: Text Encodings |
| 4147 | 4168 | ** |
| 4148 | 4169 | ** These constant define integer codes that represent the various |
| 4149 | 4170 | ** text encodings supported by SQLite. |
| 4150 | 4171 | */ |
| 4151 | -#define SQLITE_UTF8 1 | |
| 4152 | -#define SQLITE_UTF16LE 2 | |
| 4153 | -#define SQLITE_UTF16BE 3 | |
| 4172 | +#define SQLITE_UTF8 1 /* IMP: R-37514-35566 */ | |
| 4173 | +#define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */ | |
| 4174 | +#define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */ | |
| 4154 | 4175 | #define SQLITE_UTF16 4 /* Use native byte order */ |
| 4155 | 4176 | #define SQLITE_ANY 5 /* Deprecated */ |
| 4156 | 4177 | #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ |
| 4157 | 4178 | |
| 4158 | 4179 | /* |
| @@ -5638,31 +5659,47 @@ | ||
| 5638 | 5659 | ** in other words, the same BLOB that would be selected by: |
| 5639 | 5660 | ** |
| 5640 | 5661 | ** <pre> |
| 5641 | 5662 | ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow; |
| 5642 | 5663 | ** </pre>)^ |
| 5664 | +** | |
| 5665 | +** ^(Parameter zDb is not the filename that contains the database, but | |
| 5666 | +** rather the symbolic name of the database. For attached databases, this is | |
| 5667 | +** the name that appears after the AS keyword in the [ATTACH] statement. | |
| 5668 | +** For the main database file, the database name is "main". For TEMP | |
| 5669 | +** tables, the database name is "temp".)^ | |
| 5643 | 5670 | ** |
| 5644 | 5671 | ** ^If the flags parameter is non-zero, then the BLOB is opened for read |
| 5645 | -** and write access. ^If it is zero, the BLOB is opened for read access. | |
| 5646 | -** ^It is not possible to open a column that is part of an index or primary | |
| 5647 | -** key for writing. ^If [foreign key constraints] are enabled, it is | |
| 5648 | -** not possible to open a column that is part of a [child key] for writing. | |
| 5649 | -** | |
| 5650 | -** ^Note that the database name is not the filename that contains | |
| 5651 | -** the database but rather the symbolic name of the database that | |
| 5652 | -** appears after the AS keyword when the database is connected using [ATTACH]. | |
| 5653 | -** ^For the main database file, the database name is "main". | |
| 5654 | -** ^For TEMP tables, the database name is "temp". | |
| 5655 | -** | |
| 5656 | -** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written | |
| 5657 | -** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set | |
| 5658 | -** to be a null pointer.)^ | |
| 5659 | -** ^This function sets the [database connection] error code and message | |
| 5660 | -** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related | |
| 5661 | -** functions. ^Note that the *ppBlob variable is always initialized in a | |
| 5662 | -** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob | |
| 5663 | -** regardless of the success or failure of this routine. | |
| 5672 | +** and write access. ^If the flags parameter is zero, the BLOB is opened for | |
| 5673 | +** read-only access. | |
| 5674 | +** | |
| 5675 | +** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored | |
| 5676 | +** in *ppBlob. Otherwise an [error code] is returned and, unless the error | |
| 5677 | +** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided | |
| 5678 | +** the API is not misused, it is always safe to call [sqlite3_blob_close()] | |
| 5679 | +** on *ppBlob after this function it returns. | |
| 5680 | +** | |
| 5681 | +** This function fails with SQLITE_ERROR if any of the following are true: | |
| 5682 | +** <ul> | |
| 5683 | +** <li> ^(Database zDb does not exist)^, | |
| 5684 | +** <li> ^(Table zTable does not exist within database zDb)^, | |
| 5685 | +** <li> ^(Table zTable is a WITHOUT ROWID table)^, | |
| 5686 | +** <li> ^(Column zColumn does not exist)^, | |
| 5687 | +** <li> ^(Row iRow is not present in the table)^, | |
| 5688 | +** <li> ^(The specified column of row iRow contains a value that is not | |
| 5689 | +** a TEXT or BLOB value)^, | |
| 5690 | +** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE | |
| 5691 | +** constraint and the blob is being opened for read/write access)^, | |
| 5692 | +** <li> ^([foreign key constraints | Foreign key constraints] are enabled, | |
| 5693 | +** column zColumn is part of a [child key] definition and the blob is | |
| 5694 | +** being opened for read/write access)^. | |
| 5695 | +** </ul> | |
| 5696 | +** | |
| 5697 | +** ^Unless it returns SQLITE_MISUSE, this function sets the | |
| 5698 | +** [database connection] error code and message accessible via | |
| 5699 | +** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. | |
| 5700 | +** | |
| 5664 | 5701 | ** |
| 5665 | 5702 | ** ^(If the row that a BLOB handle points to is modified by an |
| 5666 | 5703 | ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects |
| 5667 | 5704 | ** then the BLOB handle is marked as "expired". |
| 5668 | 5705 | ** This is true if any column of the row is changed, even a column |
| @@ -5676,17 +5713,13 @@ | ||
| 5676 | 5713 | ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of |
| 5677 | 5714 | ** the opened blob. ^The size of a blob may not be changed by this |
| 5678 | 5715 | ** interface. Use the [UPDATE] SQL command to change the size of a |
| 5679 | 5716 | ** blob. |
| 5680 | 5717 | ** |
| 5681 | -** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID] | |
| 5682 | -** table. Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables. | |
| 5683 | -** | |
| 5684 | 5718 | ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces |
| 5685 | -** and the built-in [zeroblob] SQL function can be used, if desired, | |
| 5686 | -** to create an empty, zero-filled blob in which to read or write using | |
| 5687 | -** this interface. | |
| 5719 | +** and the built-in [zeroblob] SQL function may be used to create a | |
| 5720 | +** zero-filled blob to read or write using the incremental-blob interface. | |
| 5688 | 5721 | ** |
| 5689 | 5722 | ** To avoid a resource leak, every open [BLOB handle] should eventually |
| 5690 | 5723 | ** be released by a call to [sqlite3_blob_close()]. |
| 5691 | 5724 | */ |
| 5692 | 5725 | SQLITE_API int sqlite3_blob_open( |
| @@ -5724,28 +5757,26 @@ | ||
| 5724 | 5757 | SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); |
| 5725 | 5758 | |
| 5726 | 5759 | /* |
| 5727 | 5760 | ** CAPI3REF: Close A BLOB Handle |
| 5728 | 5761 | ** |
| 5729 | -** ^Closes an open [BLOB handle]. | |
| 5730 | -** | |
| 5731 | -** ^Closing a BLOB shall cause the current transaction to commit | |
| 5732 | -** if there are no other BLOBs, no pending prepared statements, and the | |
| 5733 | -** database connection is in [autocommit mode]. | |
| 5734 | -** ^If any writes were made to the BLOB, they might be held in cache | |
| 5735 | -** until the close operation if they will fit. | |
| 5736 | -** | |
| 5737 | -** ^(Closing the BLOB often forces the changes | |
| 5738 | -** out to disk and so if any I/O errors occur, they will likely occur | |
| 5739 | -** at the time when the BLOB is closed. Any errors that occur during | |
| 5740 | -** closing are reported as a non-zero return value.)^ | |
| 5741 | -** | |
| 5742 | -** ^(The BLOB is closed unconditionally. Even if this routine returns | |
| 5743 | -** an error code, the BLOB is still closed.)^ | |
| 5744 | -** | |
| 5745 | -** ^Calling this routine with a null pointer (such as would be returned | |
| 5746 | -** by a failed call to [sqlite3_blob_open()]) is a harmless no-op. | |
| 5762 | +** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed | |
| 5763 | +** unconditionally. Even if this routine returns an error code, the | |
| 5764 | +** handle is still closed.)^ | |
| 5765 | +** | |
| 5766 | +** ^If the blob handle being closed was opened for read-write access, and if | |
| 5767 | +** the database is in auto-commit mode and there are no other open read-write | |
| 5768 | +** blob handles or active write statements, the current transaction is | |
| 5769 | +** committed. ^If an error occurs while committing the transaction, an error | |
| 5770 | +** code is returned and the transaction rolled back. | |
| 5771 | +** | |
| 5772 | +** Calling this function with an argument that is not a NULL pointer or an | |
| 5773 | +** open blob handle results in undefined behaviour. ^Calling this routine | |
| 5774 | +** with a null pointer (such as would be returned by a failed call to | |
| 5775 | +** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function | |
| 5776 | +** is passed a valid open blob handle, the values returned by the | |
| 5777 | +** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning. | |
| 5747 | 5778 | */ |
| 5748 | 5779 | SQLITE_API int sqlite3_blob_close(sqlite3_blob *); |
| 5749 | 5780 | |
| 5750 | 5781 | /* |
| 5751 | 5782 | ** CAPI3REF: Return The Size Of An Open BLOB |
| @@ -5791,36 +5822,39 @@ | ||
| 5791 | 5822 | SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); |
| 5792 | 5823 | |
| 5793 | 5824 | /* |
| 5794 | 5825 | ** CAPI3REF: Write Data Into A BLOB Incrementally |
| 5795 | 5826 | ** |
| 5796 | -** ^This function is used to write data into an open [BLOB handle] from a | |
| 5797 | -** caller-supplied buffer. ^N bytes of data are copied from the buffer Z | |
| 5798 | -** into the open BLOB, starting at offset iOffset. | |
| 5827 | +** ^(This function is used to write data into an open [BLOB handle] from a | |
| 5828 | +** caller-supplied buffer. N bytes of data are copied from the buffer Z | |
| 5829 | +** into the open BLOB, starting at offset iOffset.)^ | |
| 5830 | +** | |
| 5831 | +** ^(On success, sqlite3_blob_write() returns SQLITE_OK. | |
| 5832 | +** Otherwise, an [error code] or an [extended error code] is returned.)^ | |
| 5833 | +** ^Unless SQLITE_MISUSE is returned, this function sets the | |
| 5834 | +** [database connection] error code and message accessible via | |
| 5835 | +** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. | |
| 5799 | 5836 | ** |
| 5800 | 5837 | ** ^If the [BLOB handle] passed as the first argument was not opened for |
| 5801 | 5838 | ** writing (the flags parameter to [sqlite3_blob_open()] was zero), |
| 5802 | 5839 | ** this function returns [SQLITE_READONLY]. |
| 5803 | 5840 | ** |
| 5804 | -** ^This function may only modify the contents of the BLOB; it is | |
| 5841 | +** This function may only modify the contents of the BLOB; it is | |
| 5805 | 5842 | ** not possible to increase the size of a BLOB using this API. |
| 5806 | 5843 | ** ^If offset iOffset is less than N bytes from the end of the BLOB, |
| 5807 | -** [SQLITE_ERROR] is returned and no data is written. ^If N is | |
| 5808 | -** less than zero [SQLITE_ERROR] is returned and no data is written. | |
| 5809 | -** The size of the BLOB (and hence the maximum value of N+iOffset) | |
| 5810 | -** can be determined using the [sqlite3_blob_bytes()] interface. | |
| 5844 | +** [SQLITE_ERROR] is returned and no data is written. The size of the | |
| 5845 | +** BLOB (and hence the maximum value of N+iOffset) can be determined | |
| 5846 | +** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less | |
| 5847 | +** than zero [SQLITE_ERROR] is returned and no data is written. | |
| 5811 | 5848 | ** |
| 5812 | 5849 | ** ^An attempt to write to an expired [BLOB handle] fails with an |
| 5813 | 5850 | ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred |
| 5814 | 5851 | ** before the [BLOB handle] expired are not rolled back by the |
| 5815 | 5852 | ** expiration of the handle, though of course those changes might |
| 5816 | 5853 | ** have been overwritten by the statement that expired the BLOB handle |
| 5817 | 5854 | ** or by other independent statements. |
| 5818 | 5855 | ** |
| 5819 | -** ^(On success, sqlite3_blob_write() returns SQLITE_OK. | |
| 5820 | -** Otherwise, an [error code] or an [extended error code] is returned.)^ | |
| 5821 | -** | |
| 5822 | 5856 | ** This routine only works on a [BLOB handle] which has been created |
| 5823 | 5857 | ** by a prior successful call to [sqlite3_blob_open()] and which has not |
| 5824 | 5858 | ** been closed by [sqlite3_blob_close()]. Passing any other pointer in |
| 5825 | 5859 | ** to this routine results in undefined and probably undesirable behavior. |
| 5826 | 5860 | ** |
| @@ -5869,38 +5903,38 @@ | ||
| 5869 | 5903 | ** use by SQLite, code that links against SQLite is |
| 5870 | 5904 | ** permitted to use any of these routines. |
| 5871 | 5905 | ** |
| 5872 | 5906 | ** The SQLite source code contains multiple implementations |
| 5873 | 5907 | ** of these mutex routines. An appropriate implementation |
| 5874 | -** is selected automatically at compile-time. ^(The following | |
| 5908 | +** is selected automatically at compile-time. The following | |
| 5875 | 5909 | ** implementations are available in the SQLite core: |
| 5876 | 5910 | ** |
| 5877 | 5911 | ** <ul> |
| 5878 | 5912 | ** <li> SQLITE_MUTEX_PTHREADS |
| 5879 | 5913 | ** <li> SQLITE_MUTEX_W32 |
| 5880 | 5914 | ** <li> SQLITE_MUTEX_NOOP |
| 5881 | -** </ul>)^ | |
| 5915 | +** </ul> | |
| 5882 | 5916 | ** |
| 5883 | -** ^The SQLITE_MUTEX_NOOP implementation is a set of routines | |
| 5917 | +** The SQLITE_MUTEX_NOOP implementation is a set of routines | |
| 5884 | 5918 | ** that does no real locking and is appropriate for use in |
| 5885 | -** a single-threaded application. ^The SQLITE_MUTEX_PTHREADS and | |
| 5919 | +** a single-threaded application. The SQLITE_MUTEX_PTHREADS and | |
| 5886 | 5920 | ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix |
| 5887 | 5921 | ** and Windows. |
| 5888 | 5922 | ** |
| 5889 | -** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor | |
| 5923 | +** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor | |
| 5890 | 5924 | ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex |
| 5891 | 5925 | ** implementation is included with the library. In this case the |
| 5892 | 5926 | ** application must supply a custom mutex implementation using the |
| 5893 | 5927 | ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function |
| 5894 | 5928 | ** before calling sqlite3_initialize() or any other public sqlite3_ |
| 5895 | -** function that calls sqlite3_initialize().)^ | |
| 5929 | +** function that calls sqlite3_initialize(). | |
| 5896 | 5930 | ** |
| 5897 | 5931 | ** ^The sqlite3_mutex_alloc() routine allocates a new |
| 5898 | -** mutex and returns a pointer to it. ^If it returns NULL | |
| 5899 | -** that means that a mutex could not be allocated. ^SQLite | |
| 5900 | -** will unwind its stack and return an error. ^(The argument | |
| 5901 | -** to sqlite3_mutex_alloc() is one of these integer constants: | |
| 5932 | +** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc() | |
| 5933 | +** routine returns NULL if it is unable to allocate the requested | |
| 5934 | +** mutex. The argument to sqlite3_mutex_alloc() must one of these | |
| 5935 | +** integer constants: | |
| 5902 | 5936 | ** |
| 5903 | 5937 | ** <ul> |
| 5904 | 5938 | ** <li> SQLITE_MUTEX_FAST |
| 5905 | 5939 | ** <li> SQLITE_MUTEX_RECURSIVE |
| 5906 | 5940 | ** <li> SQLITE_MUTEX_STATIC_MASTER |
| @@ -5909,68 +5943,64 @@ | ||
| 5909 | 5943 | ** <li> SQLITE_MUTEX_STATIC_PRNG |
| 5910 | 5944 | ** <li> SQLITE_MUTEX_STATIC_LRU |
| 5911 | 5945 | ** <li> SQLITE_MUTEX_STATIC_PMEM |
| 5912 | 5946 | ** <li> SQLITE_MUTEX_STATIC_APP1 |
| 5913 | 5947 | ** <li> SQLITE_MUTEX_STATIC_APP2 |
| 5914 | -** </ul>)^ | |
| 5948 | +** <li> SQLITE_MUTEX_STATIC_APP3 | |
| 5949 | +** </ul> | |
| 5915 | 5950 | ** |
| 5916 | 5951 | ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) |
| 5917 | 5952 | ** cause sqlite3_mutex_alloc() to create |
| 5918 | 5953 | ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE |
| 5919 | 5954 | ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. |
| 5920 | 5955 | ** The mutex implementation does not need to make a distinction |
| 5921 | 5956 | ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does |
| 5922 | -** not want to. ^SQLite will only request a recursive mutex in | |
| 5923 | -** cases where it really needs one. ^If a faster non-recursive mutex | |
| 5957 | +** not want to. SQLite will only request a recursive mutex in | |
| 5958 | +** cases where it really needs one. If a faster non-recursive mutex | |
| 5924 | 5959 | ** implementation is available on the host platform, the mutex subsystem |
| 5925 | 5960 | ** might return such a mutex in response to SQLITE_MUTEX_FAST. |
| 5926 | 5961 | ** |
| 5927 | 5962 | ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other |
| 5928 | 5963 | ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return |
| 5929 | -** a pointer to a static preexisting mutex. ^Six static mutexes are | |
| 5964 | +** a pointer to a static preexisting mutex. ^Nine static mutexes are | |
| 5930 | 5965 | ** used by the current version of SQLite. Future versions of SQLite |
| 5931 | 5966 | ** may add additional static mutexes. Static mutexes are for internal |
| 5932 | 5967 | ** use by SQLite only. Applications that use SQLite mutexes should |
| 5933 | 5968 | ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or |
| 5934 | 5969 | ** SQLITE_MUTEX_RECURSIVE. |
| 5935 | 5970 | ** |
| 5936 | 5971 | ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST |
| 5937 | 5972 | ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() |
| 5938 | -** returns a different mutex on every call. ^But for the static | |
| 5973 | +** returns a different mutex on every call. ^For the static | |
| 5939 | 5974 | ** mutex types, the same mutex is returned on every call that has |
| 5940 | 5975 | ** the same type number. |
| 5941 | 5976 | ** |
| 5942 | 5977 | ** ^The sqlite3_mutex_free() routine deallocates a previously |
| 5943 | -** allocated dynamic mutex. ^SQLite is careful to deallocate every | |
| 5944 | -** dynamic mutex that it allocates. The dynamic mutexes must not be in | |
| 5945 | -** use when they are deallocated. Attempting to deallocate a static | |
| 5946 | -** mutex results in undefined behavior. ^SQLite never deallocates | |
| 5947 | -** a static mutex. | |
| 5978 | +** allocated dynamic mutex. Attempting to deallocate a static | |
| 5979 | +** mutex results in undefined behavior. | |
| 5948 | 5980 | ** |
| 5949 | 5981 | ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt |
| 5950 | 5982 | ** to enter a mutex. ^If another thread is already within the mutex, |
| 5951 | 5983 | ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return |
| 5952 | 5984 | ** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK] |
| 5953 | 5985 | ** upon successful entry. ^(Mutexes created using |
| 5954 | 5986 | ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. |
| 5955 | -** In such cases the, | |
| 5987 | +** In such cases, the | |
| 5956 | 5988 | ** mutex must be exited an equal number of times before another thread |
| 5957 | -** can enter.)^ ^(If the same thread tries to enter any other | |
| 5958 | -** kind of mutex more than once, the behavior is undefined. | |
| 5959 | -** SQLite will never exhibit | |
| 5960 | -** such behavior in its own use of mutexes.)^ | |
| 5989 | +** can enter.)^ If the same thread tries to enter any mutex other | |
| 5990 | +** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined. | |
| 5961 | 5991 | ** |
| 5962 | 5992 | ** ^(Some systems (for example, Windows 95) do not support the operation |
| 5963 | 5993 | ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() |
| 5964 | -** will always return SQLITE_BUSY. The SQLite core only ever uses | |
| 5965 | -** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^ | |
| 5994 | +** will always return SQLITE_BUSY. The SQLite core only ever uses | |
| 5995 | +** sqlite3_mutex_try() as an optimization so this is acceptable | |
| 5996 | +** behavior.)^ | |
| 5966 | 5997 | ** |
| 5967 | 5998 | ** ^The sqlite3_mutex_leave() routine exits a mutex that was |
| 5968 | -** previously entered by the same thread. ^(The behavior | |
| 5999 | +** previously entered by the same thread. The behavior | |
| 5969 | 6000 | ** is undefined if the mutex is not currently entered by the |
| 5970 | -** calling thread or is not currently allocated. SQLite will | |
| 5971 | -** never do either.)^ | |
| 6001 | +** calling thread or is not currently allocated. | |
| 5972 | 6002 | ** |
| 5973 | 6003 | ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or |
| 5974 | 6004 | ** sqlite3_mutex_leave() is a NULL pointer, then all three routines |
| 5975 | 6005 | ** behave as no-ops. |
| 5976 | 6006 | ** |
| @@ -5987,13 +6017,13 @@ | ||
| 5987 | 6017 | ** |
| 5988 | 6018 | ** An instance of this structure defines the low-level routines |
| 5989 | 6019 | ** used to allocate and use mutexes. |
| 5990 | 6020 | ** |
| 5991 | 6021 | ** Usually, the default mutex implementations provided by SQLite are |
| 5992 | -** sufficient, however the user has the option of substituting a custom | |
| 6022 | +** sufficient, however the application has the option of substituting a custom | |
| 5993 | 6023 | ** implementation for specialized deployments or systems for which SQLite |
| 5994 | -** does not provide a suitable implementation. In this case, the user | |
| 6024 | +** does not provide a suitable implementation. In this case, the application | |
| 5995 | 6025 | ** creates and populates an instance of this structure to pass |
| 5996 | 6026 | ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option. |
| 5997 | 6027 | ** Additionally, an instance of this structure can be used as an |
| 5998 | 6028 | ** output variable when querying the system for the current mutex |
| 5999 | 6029 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| @@ -6030,17 +6060,17 @@ | ||
| 6030 | 6060 | ** by this structure are not required to handle this case, the results |
| 6031 | 6061 | ** of passing a NULL pointer instead of a valid mutex handle are undefined |
| 6032 | 6062 | ** (i.e. it is acceptable to provide an implementation that segfaults if |
| 6033 | 6063 | ** it is passed a NULL pointer). |
| 6034 | 6064 | ** |
| 6035 | -** The xMutexInit() method must be threadsafe. ^It must be harmless to | |
| 6065 | +** The xMutexInit() method must be threadsafe. It must be harmless to | |
| 6036 | 6066 | ** invoke xMutexInit() multiple times within the same process and without |
| 6037 | 6067 | ** intervening calls to xMutexEnd(). Second and subsequent calls to |
| 6038 | 6068 | ** xMutexInit() must be no-ops. |
| 6039 | 6069 | ** |
| 6040 | -** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] | |
| 6041 | -** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory | |
| 6070 | +** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] | |
| 6071 | +** and its associates). Similarly, xMutexAlloc() must not use SQLite memory | |
| 6042 | 6072 | ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite |
| 6043 | 6073 | ** memory allocation for a fast or recursive mutex. |
| 6044 | 6074 | ** |
| 6045 | 6075 | ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is |
| 6046 | 6076 | ** called, but only if the prior call to xMutexInit returned SQLITE_OK. |
| @@ -6062,33 +6092,33 @@ | ||
| 6062 | 6092 | |
| 6063 | 6093 | /* |
| 6064 | 6094 | ** CAPI3REF: Mutex Verification Routines |
| 6065 | 6095 | ** |
| 6066 | 6096 | ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines |
| 6067 | -** are intended for use inside assert() statements. ^The SQLite core | |
| 6097 | +** are intended for use inside assert() statements. The SQLite core | |
| 6068 | 6098 | ** never uses these routines except inside an assert() and applications |
| 6069 | -** are advised to follow the lead of the core. ^The SQLite core only | |
| 6099 | +** are advised to follow the lead of the core. The SQLite core only | |
| 6070 | 6100 | ** provides implementations for these routines when it is compiled |
| 6071 | -** with the SQLITE_DEBUG flag. ^External mutex implementations | |
| 6101 | +** with the SQLITE_DEBUG flag. External mutex implementations | |
| 6072 | 6102 | ** are only required to provide these routines if SQLITE_DEBUG is |
| 6073 | 6103 | ** defined and if NDEBUG is not defined. |
| 6074 | 6104 | ** |
| 6075 | -** ^These routines should return true if the mutex in their argument | |
| 6105 | +** These routines should return true if the mutex in their argument | |
| 6076 | 6106 | ** is held or not held, respectively, by the calling thread. |
| 6077 | 6107 | ** |
| 6078 | -** ^The implementation is not required to provide versions of these | |
| 6108 | +** The implementation is not required to provide versions of these | |
| 6079 | 6109 | ** routines that actually work. If the implementation does not provide working |
| 6080 | 6110 | ** versions of these routines, it should at least provide stubs that always |
| 6081 | 6111 | ** return true so that one does not get spurious assertion failures. |
| 6082 | 6112 | ** |
| 6083 | -** ^If the argument to sqlite3_mutex_held() is a NULL pointer then | |
| 6113 | +** If the argument to sqlite3_mutex_held() is a NULL pointer then | |
| 6084 | 6114 | ** the routine should return 1. This seems counter-intuitive since |
| 6085 | 6115 | ** clearly the mutex cannot be held if it does not exist. But |
| 6086 | 6116 | ** the reason the mutex does not exist is because the build is not |
| 6087 | 6117 | ** using mutexes. And we do not want the assert() containing the |
| 6088 | 6118 | ** call to sqlite3_mutex_held() to fail, so a non-zero return is |
| 6089 | -** the appropriate thing to do. ^The sqlite3_mutex_notheld() | |
| 6119 | +** the appropriate thing to do. The sqlite3_mutex_notheld() | |
| 6090 | 6120 | ** interface should also return 1 when given a NULL pointer. |
| 6091 | 6121 | */ |
| 6092 | 6122 | #ifndef NDEBUG |
| 6093 | 6123 | SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*); |
| 6094 | 6124 | SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); |
| @@ -6816,10 +6846,14 @@ | ||
| 6816 | 6846 | ** sqlite3_backup_init(D,N,S,M) identify the [database connection] |
| 6817 | 6847 | ** and database name of the source database, respectively. |
| 6818 | 6848 | ** ^The source and destination [database connections] (parameters S and D) |
| 6819 | 6849 | ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with |
| 6820 | 6850 | ** an error. |
| 6851 | +** | |
| 6852 | +** ^A call to sqlite3_backup_init() will fail, returning SQLITE_ERROR, if | |
| 6853 | +** there is already a read or read-write transaction open on the | |
| 6854 | +** destination database. | |
| 6821 | 6855 | ** |
| 6822 | 6856 | ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is |
| 6823 | 6857 | ** returned and an error code and error message are stored in the |
| 6824 | 6858 | ** destination [database connection] D. |
| 6825 | 6859 | ** ^The error code and message for the failed call to sqlite3_backup_init() |
| @@ -7409,10 +7443,102 @@ | ||
| 7409 | 7443 | /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */ |
| 7410 | 7444 | #define SQLITE_FAIL 3 |
| 7411 | 7445 | /* #define SQLITE_ABORT 4 // Also an error code */ |
| 7412 | 7446 | #define SQLITE_REPLACE 5 |
| 7413 | 7447 | |
| 7448 | +/* | |
| 7449 | +** CAPI3REF: Prepared Statement Scan Status Opcodes | |
| 7450 | +** KEYWORDS: {scanstatus options} | |
| 7451 | +** | |
| 7452 | +** The following constants can be used for the T parameter to the | |
| 7453 | +** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a | |
| 7454 | +** different metric for sqlite3_stmt_scanstatus() to return. | |
| 7455 | +** | |
| 7456 | +** <dl> | |
| 7457 | +** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt> | |
| 7458 | +** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the | |
| 7459 | +** total number of times that the X-th loop has run.</dd> | |
| 7460 | +** | |
| 7461 | +** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt> | |
| 7462 | +** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the | |
| 7463 | +** total number of rows examined by all iterations of the X-th loop.</dd> | |
| 7464 | +** | |
| 7465 | +** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt> | |
| 7466 | +** <dd>^The "double" variable pointed to by the T parameter will be set to the | |
| 7467 | +** query planner's estimate for the average number of rows output from each | |
| 7468 | +** iteration of the X-th loop. If the query planner's estimates was accurate, | |
| 7469 | +** then this value will approximate the quotient NVISIT/NLOOP and the | |
| 7470 | +** product of this value for all prior loops with the same SELECTID will | |
| 7471 | +** be the NLOOP value for the current loop. | |
| 7472 | +** | |
| 7473 | +** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt> | |
| 7474 | +** <dd>^The "const char *" variable pointed to by the T parameter will be set to | |
| 7475 | +** a zero-terminated UTF-8 string containing the name of the index or table used | |
| 7476 | +** for the X-th loop. | |
| 7477 | +** | |
| 7478 | +** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt> | |
| 7479 | +** <dd>^The "const char *" variable pointed to by the T parameter will be set to | |
| 7480 | +** a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] description | |
| 7481 | +** for the X-th loop. | |
| 7482 | +** | |
| 7483 | +** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt> | |
| 7484 | +** <dd>^The "int" variable pointed to by the T parameter will be set to the | |
| 7485 | +** "select-id" for the X-th loop. The select-id identifies which query or | |
| 7486 | +** subquery the loop is part of. The main query has a select-id of zero. | |
| 7487 | +** The select-id is the same value as is output in the first column | |
| 7488 | +** of an [EXPLAIN QUERY PLAN] query. | |
| 7489 | +** </dl> | |
| 7490 | +*/ | |
| 7491 | +#define SQLITE_SCANSTAT_NLOOP 0 | |
| 7492 | +#define SQLITE_SCANSTAT_NVISIT 1 | |
| 7493 | +#define SQLITE_SCANSTAT_EST 2 | |
| 7494 | +#define SQLITE_SCANSTAT_NAME 3 | |
| 7495 | +#define SQLITE_SCANSTAT_EXPLAIN 4 | |
| 7496 | +#define SQLITE_SCANSTAT_SELECTID 5 | |
| 7497 | + | |
| 7498 | +/* | |
| 7499 | +** CAPI3REF: Prepared Statement Scan Status | |
| 7500 | +** | |
| 7501 | +** Return status data for a single loop within query pStmt. | |
| 7502 | +** | |
| 7503 | +** The "iScanStatusOp" parameter determines which status information to return. | |
| 7504 | +** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior of | |
| 7505 | +** this interface is undefined. | |
| 7506 | +** ^The requested measurement is written into a variable pointed to by | |
| 7507 | +** the "pOut" parameter. | |
| 7508 | +** Parameter "idx" identifies the specific loop to retrieve statistics for. | |
| 7509 | +** Loops are numbered starting from zero. ^If idx is out of range - less than | |
| 7510 | +** zero or greater than or equal to the total number of loops used to implement | |
| 7511 | +** the statement - a non-zero value is returned and the variable that pOut | |
| 7512 | +** points to is unchanged. | |
| 7513 | +** | |
| 7514 | +** ^Statistics might not be available for all loops in all statements. ^In cases | |
| 7515 | +** where there exist loops with no available statistics, this function behaves | |
| 7516 | +** as if the loop did not exist - it returns non-zero and leave the variable | |
| 7517 | +** that pOut points to unchanged. | |
| 7518 | +** | |
| 7519 | +** This API is only available if the library is built with pre-processor | |
| 7520 | +** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. | |
| 7521 | +** | |
| 7522 | +** See also: [sqlite3_stmt_scanstatus_reset()] | |
| 7523 | +*/ | |
| 7524 | +SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus( | |
| 7525 | + sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ | |
| 7526 | + int idx, /* Index of loop to report on */ | |
| 7527 | + int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ | |
| 7528 | + void *pOut /* Result written here */ | |
| 7529 | +); | |
| 7530 | + | |
| 7531 | +/* | |
| 7532 | +** CAPI3REF: Zero Scan-Status Counters | |
| 7533 | +** | |
| 7534 | +** ^Zero all [sqlite3_stmt_scanstatus()] related event counters. | |
| 7535 | +** | |
| 7536 | +** This API is only available if the library is built with pre-processor | |
| 7537 | +** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. | |
| 7538 | +*/ | |
| 7539 | +SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); | |
| 7414 | 7540 | |
| 7415 | 7541 | |
| 7416 | 7542 | /* |
| 7417 | 7543 | ** Undo the hack that converts floating point types to integer for |
| 7418 | 7544 | ** builds on processors without floating point support. |
| 7419 | 7545 |
| --- src/sqlite3.h | |
| +++ src/sqlite3.h | |
| @@ -55,11 +55,11 @@ | |
| 55 | |
| 56 | |
| 57 | /* |
| 58 | ** These no-op macros are used in front of interfaces to mark those |
| 59 | ** interfaces as either deprecated or experimental. New applications |
| 60 | ** should not use deprecated interfaces - they are support for backwards |
| 61 | ** compatibility only. Application writers should be aware that |
| 62 | ** experimental interfaces are subject to change in point releases. |
| 63 | ** |
| 64 | ** These macros used to resolve to various kinds of compiler magic that |
| 65 | ** would generate warning messages when they were used. But that |
| @@ -105,13 +105,13 @@ | |
| 105 | ** |
| 106 | ** See also: [sqlite3_libversion()], |
| 107 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 108 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 109 | */ |
| 110 | #define SQLITE_VERSION "3.8.7.2" |
| 111 | #define SQLITE_VERSION_NUMBER 3008007 |
| 112 | #define SQLITE_SOURCE_ID "2014-11-18 20:57:56 2ab564bf9655b7c7b97ab85cafc8a48329b27f93" |
| 113 | |
| 114 | /* |
| 115 | ** CAPI3REF: Run-Time Library Version Numbers |
| 116 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 117 | ** |
| @@ -1502,29 +1502,31 @@ | |
| 1502 | ** it is not possible to set the Serialized [threading mode] and |
| 1503 | ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the |
| 1504 | ** SQLITE_CONFIG_SERIALIZED configuration option.</dd> |
| 1505 | ** |
| 1506 | ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt> |
| 1507 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1508 | ** instance of the [sqlite3_mem_methods] structure. The argument specifies |
| 1509 | ** alternative low-level memory allocation routines to be used in place of |
| 1510 | ** the memory allocation routines built into SQLite.)^ ^SQLite makes |
| 1511 | ** its own private copy of the content of the [sqlite3_mem_methods] structure |
| 1512 | ** before the [sqlite3_config()] call returns.</dd> |
| 1513 | ** |
| 1514 | ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt> |
| 1515 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1516 | ** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods] |
| 1517 | ** structure is filled with the currently defined memory allocation routines.)^ |
| 1518 | ** This option can be used to overload the default memory allocation |
| 1519 | ** routines with a wrapper that simulations memory allocation failure or |
| 1520 | ** tracks memory usage, for example. </dd> |
| 1521 | ** |
| 1522 | ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt> |
| 1523 | ** <dd> ^This option takes single argument of type int, interpreted as a |
| 1524 | ** boolean, which enables or disables the collection of memory allocation |
| 1525 | ** statistics. ^(When memory allocation statistics are disabled, the |
| 1526 | ** following SQLite interfaces become non-operational: |
| 1527 | ** <ul> |
| 1528 | ** <li> [sqlite3_memory_used()] |
| 1529 | ** <li> [sqlite3_memory_highwater()] |
| 1530 | ** <li> [sqlite3_soft_heap_limit64()] |
| @@ -1534,78 +1536,90 @@ | |
| 1534 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1535 | ** allocation statistics are disabled by default. |
| 1536 | ** </dd> |
| 1537 | ** |
| 1538 | ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt> |
| 1539 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1540 | ** scratch memory. There are three arguments: A pointer an 8-byte |
| 1541 | ** aligned memory buffer from which the scratch allocations will be |
| 1542 | ** drawn, the size of each scratch allocation (sz), |
| 1543 | ** and the maximum number of scratch allocations (N). The sz |
| 1544 | ** argument must be a multiple of 16. |
| 1545 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1546 | ** of at least sz*N bytes of memory. |
| 1547 | ** ^SQLite will use no more than two scratch buffers per thread. So |
| 1548 | ** N should be set to twice the expected maximum number of threads. |
| 1549 | ** ^SQLite will never require a scratch buffer that is more than 6 |
| 1550 | ** times the database page size. ^If SQLite needs needs additional |
| 1551 | ** scratch memory beyond what is provided by this configuration option, then |
| 1552 | ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd> |
| 1553 | ** |
| 1554 | ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1555 | ** <dd> ^This option specifies a static memory buffer that SQLite can use for |
| 1556 | ** the database page cache with the default page cache implementation. |
| 1557 | ** This configuration should not be used if an application-define page |
| 1558 | ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option. |
| 1559 | ** There are three arguments to this option: A pointer to 8-byte aligned |
| 1560 | ** memory, the size of each page buffer (sz), and the number of pages (N). |
| 1561 | ** The sz argument should be the size of the largest database page |
| 1562 | ** (a power of two between 512 and 32768) plus a little extra for each |
| 1563 | ** page header. ^The page header size is 20 to 40 bytes depending on |
| 1564 | ** the host architecture. ^It is harmless, apart from the wasted memory, |
| 1565 | ** to make sz a little too large. The first |
| 1566 | ** argument should point to an allocation of at least sz*N bytes of memory. |
| 1567 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1568 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1569 | ** page cache memory is needed beyond what is provided by this option, then |
| 1570 | ** SQLite goes to [sqlite3_malloc()] for the additional storage space. |
| 1571 | ** The pointer in the first argument must |
| 1572 | ** be aligned to an 8-byte boundary or subsequent behavior of SQLite |
| 1573 | ** will be undefined.</dd> |
| 1574 | ** |
| 1575 | ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt> |
| 1576 | ** <dd> ^This option specifies a static memory buffer that SQLite will use |
| 1577 | ** for all of its dynamic memory allocation needs beyond those provided |
| 1578 | ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE]. |
| 1579 | ** There are three arguments: An 8-byte aligned pointer to the memory, |
| 1580 | ** the number of bytes in the memory buffer, and the minimum allocation size. |
| 1581 | ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts |
| 1582 | ** to using its default memory allocator (the system malloc() implementation), |
| 1583 | ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the |
| 1584 | ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or |
| 1585 | ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory |
| 1586 | ** allocator is engaged to handle all of SQLites memory allocation needs. |
| 1587 | ** The first pointer (the memory pointer) must be aligned to an 8-byte |
| 1588 | ** boundary or subsequent behavior of SQLite will be undefined. |
| 1589 | ** The minimum allocation size is capped at 2**12. Reasonable values |
| 1590 | ** for the minimum allocation size are 2**5 through 2**8.</dd> |
| 1591 | ** |
| 1592 | ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt> |
| 1593 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1594 | ** instance of the [sqlite3_mutex_methods] structure. The argument specifies |
| 1595 | ** alternative low-level mutex routines to be used in place |
| 1596 | ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the |
| 1597 | ** content of the [sqlite3_mutex_methods] structure before the call to |
| 1598 | ** [sqlite3_config()] returns. ^If SQLite is compiled with |
| 1599 | ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then |
| 1600 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1601 | ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will |
| 1602 | ** return [SQLITE_ERROR].</dd> |
| 1603 | ** |
| 1604 | ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt> |
| 1605 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1606 | ** instance of the [sqlite3_mutex_methods] structure. The |
| 1607 | ** [sqlite3_mutex_methods] |
| 1608 | ** structure is filled with the currently defined mutex routines.)^ |
| 1609 | ** This option can be used to overload the default mutex allocation |
| 1610 | ** routines with a wrapper used to track mutex usage for performance |
| 1611 | ** profiling or testing, for example. ^If SQLite is compiled with |
| @@ -1613,28 +1627,28 @@ | |
| 1613 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1614 | ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will |
| 1615 | ** return [SQLITE_ERROR].</dd> |
| 1616 | ** |
| 1617 | ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt> |
| 1618 | ** <dd> ^(This option takes two arguments that determine the default |
| 1619 | ** memory allocation for the lookaside memory allocator on each |
| 1620 | ** [database connection]. The first argument is the |
| 1621 | ** size of each lookaside buffer slot and the second is the number of |
| 1622 | ** slots allocated to each database connection.)^ ^(This option sets the |
| 1623 | ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] |
| 1624 | ** verb to [sqlite3_db_config()] can be used to change the lookaside |
| 1625 | ** configuration on individual connections.)^ </dd> |
| 1626 | ** |
| 1627 | ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt> |
| 1628 | ** <dd> ^(This option takes a single argument which is a pointer to |
| 1629 | ** an [sqlite3_pcache_methods2] object. This object specifies the interface |
| 1630 | ** to a custom page cache implementation.)^ ^SQLite makes a copy of the |
| 1631 | ** object and uses it for page cache memory allocations.</dd> |
| 1632 | ** |
| 1633 | ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt> |
| 1634 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1635 | ** [sqlite3_pcache_methods2] object. SQLite copies of the current |
| 1636 | ** page cache implementation into that object.)^ </dd> |
| 1637 | ** |
| 1638 | ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt> |
| 1639 | ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite |
| 1640 | ** global [error log]. |
| @@ -1654,26 +1668,27 @@ | |
| 1654 | ** supplied by the application must not invoke any SQLite interface. |
| 1655 | ** In a multi-threaded application, the application-defined logger |
| 1656 | ** function must be threadsafe. </dd> |
| 1657 | ** |
| 1658 | ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI |
| 1659 | ** <dd>^(This option takes a single argument of type int. If non-zero, then |
| 1660 | ** URI handling is globally enabled. If the parameter is zero, then URI handling |
| 1661 | ** is globally disabled.)^ ^If URI handling is globally enabled, all filenames |
| 1662 | ** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or |
| 1663 | ** specified as part of [ATTACH] commands are interpreted as URIs, regardless |
| 1664 | ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database |
| 1665 | ** connection is opened. ^If it is globally disabled, filenames are |
| 1666 | ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the |
| 1667 | ** database connection is opened. ^(By default, URI handling is globally |
| 1668 | ** disabled. The default value may be changed by compiling with the |
| 1669 | ** [SQLITE_USE_URI] symbol defined.)^ |
| 1670 | ** |
| 1671 | ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN |
| 1672 | ** <dd>^This option takes a single integer argument which is interpreted as |
| 1673 | ** a boolean in order to enable or disable the use of covering indices for |
| 1674 | ** full table scans in the query optimizer. ^The default setting is determined |
| 1675 | ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on" |
| 1676 | ** if that compile-time option is omitted. |
| 1677 | ** The ability to disable the use of covering indices for full table scans |
| 1678 | ** is because some incorrectly coded legacy applications might malfunction |
| 1679 | ** when the optimization is enabled. Providing the ability to |
| @@ -1709,23 +1724,32 @@ | |
| 1709 | ** that are the default mmap size limit (the default setting for |
| 1710 | ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit. |
| 1711 | ** ^The default setting can be overridden by each database connection using |
| 1712 | ** either the [PRAGMA mmap_size] command, or by using the |
| 1713 | ** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size |
| 1714 | ** cannot be changed at run-time. Nor may the maximum allowed mmap size |
| 1715 | ** exceed the compile-time maximum mmap size set by the |
| 1716 | ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^ |
| 1717 | ** ^If either argument to this option is negative, then that argument is |
| 1718 | ** changed to its compile-time default. |
| 1719 | ** |
| 1720 | ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]] |
| 1721 | ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE |
| 1722 | ** <dd>^This option is only available if SQLite is compiled for Windows |
| 1723 | ** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined. |
| 1724 | ** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value |
| 1725 | ** that specifies the maximum size of the created heap. |
| 1726 | ** </dl> |
| 1727 | */ |
| 1728 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 1729 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 1730 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| 1731 | #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ |
| @@ -1746,10 +1770,11 @@ | |
| 1746 | #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ |
| 1747 | #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ |
| 1748 | #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ |
| 1749 | #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ |
| 1750 | #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ |
| 1751 | |
| 1752 | /* |
| 1753 | ** CAPI3REF: Database Connection Configuration Options |
| 1754 | ** |
| 1755 | ** These constants are the available integer configuration options that |
| @@ -1873,51 +1898,49 @@ | |
| 1873 | SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); |
| 1874 | |
| 1875 | /* |
| 1876 | ** CAPI3REF: Count The Number Of Rows Modified |
| 1877 | ** |
| 1878 | ** ^This function returns the number of database rows that were changed |
| 1879 | ** or inserted or deleted by the most recently completed SQL statement |
| 1880 | ** on the [database connection] specified by the first parameter. |
| 1881 | ** ^(Only changes that are directly specified by the [INSERT], [UPDATE], |
| 1882 | ** or [DELETE] statement are counted. Auxiliary changes caused by |
| 1883 | ** triggers or [foreign key actions] are not counted.)^ Use the |
| 1884 | ** [sqlite3_total_changes()] function to find the total number of changes |
| 1885 | ** including changes caused by triggers and foreign key actions. |
| 1886 | ** |
| 1887 | ** ^Changes to a view that are simulated by an [INSTEAD OF trigger] |
| 1888 | ** are not counted. Only real table changes are counted. |
| 1889 | ** |
| 1890 | ** ^(A "row change" is a change to a single row of a single table |
| 1891 | ** caused by an INSERT, DELETE, or UPDATE statement. Rows that |
| 1892 | ** are changed as side effects of [REPLACE] constraint resolution, |
| 1893 | ** rollback, ABORT processing, [DROP TABLE], or by any other |
| 1894 | ** mechanisms do not count as direct row changes.)^ |
| 1895 | ** |
| 1896 | ** A "trigger context" is a scope of execution that begins and |
| 1897 | ** ends with the script of a [CREATE TRIGGER | trigger]. |
| 1898 | ** Most SQL statements are |
| 1899 | ** evaluated outside of any trigger. This is the "top level" |
| 1900 | ** trigger context. If a trigger fires from the top level, a |
| 1901 | ** new trigger context is entered for the duration of that one |
| 1902 | ** trigger. Subtriggers create subcontexts for their duration. |
| 1903 | ** |
| 1904 | ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does |
| 1905 | ** not create a new trigger context. |
| 1906 | ** |
| 1907 | ** ^This function returns the number of direct row changes in the |
| 1908 | ** most recent INSERT, UPDATE, or DELETE statement within the same |
| 1909 | ** trigger context. |
| 1910 | ** |
| 1911 | ** ^Thus, when called from the top level, this function returns the |
| 1912 | ** number of changes in the most recent INSERT, UPDATE, or DELETE |
| 1913 | ** that also occurred at the top level. ^(Within the body of a trigger, |
| 1914 | ** the sqlite3_changes() interface can be called to find the number of |
| 1915 | ** changes in the most recently completed INSERT, UPDATE, or DELETE |
| 1916 | ** statement within the body of the same trigger. |
| 1917 | ** However, the number returned does not include changes |
| 1918 | ** caused by subtriggers since those have their own context.)^ |
| 1919 | ** |
| 1920 | ** See also the [sqlite3_total_changes()] interface, the |
| 1921 | ** [count_changes pragma], and the [changes() SQL function]. |
| 1922 | ** |
| 1923 | ** If a separate thread makes changes on the same database connection |
| @@ -1927,24 +1950,21 @@ | |
| 1927 | SQLITE_API int sqlite3_changes(sqlite3*); |
| 1928 | |
| 1929 | /* |
| 1930 | ** CAPI3REF: Total Number Of Rows Modified |
| 1931 | ** |
| 1932 | ** ^This function returns the number of row changes caused by [INSERT], |
| 1933 | ** [UPDATE] or [DELETE] statements since the [database connection] was opened. |
| 1934 | ** ^(The count returned by sqlite3_total_changes() includes all changes |
| 1935 | ** from all [CREATE TRIGGER | trigger] contexts and changes made by |
| 1936 | ** [foreign key actions]. However, |
| 1937 | ** the count does not include changes used to implement [REPLACE] constraints, |
| 1938 | ** do rollbacks or ABORT processing, or [DROP TABLE] processing. The |
| 1939 | ** count does not include rows of views that fire an [INSTEAD OF trigger], |
| 1940 | ** though if the INSTEAD OF trigger makes changes of its own, those changes |
| 1941 | ** are counted.)^ |
| 1942 | ** ^The sqlite3_total_changes() function counts the changes as soon as |
| 1943 | ** the statement that makes them is completed (when the statement handle |
| 1944 | ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]). |
| 1945 | ** |
| 1946 | ** See also the [sqlite3_changes()] interface, the |
| 1947 | ** [count_changes pragma], and the [total_changes() SQL function]. |
| 1948 | ** |
| 1949 | ** If a separate thread makes changes on the same database connection |
| 1950 | ** while [sqlite3_total_changes()] is running then the value |
| @@ -2418,17 +2438,18 @@ | |
| 2418 | ** already uses the largest possible [ROWID]. The PRNG is also used for |
| 2419 | ** the build-in random() and randomblob() SQL functions. This interface allows |
| 2420 | ** applications to access the same PRNG for other purposes. |
| 2421 | ** |
| 2422 | ** ^A call to this routine stores N bytes of randomness into buffer P. |
| 2423 | ** ^If N is less than one, then P can be a NULL pointer. |
| 2424 | ** |
| 2425 | ** ^If this routine has not been previously called or if the previous |
| 2426 | ** call had N less than one, then the PRNG is seeded using randomness |
| 2427 | ** obtained from the xRandomness method of the default [sqlite3_vfs] object. |
| 2428 | ** ^If the previous call to this routine had an N of 1 or more then |
| 2429 | ** the pseudo-randomness is generated |
| 2430 | ** internally and without recourse to the [sqlite3_vfs] xRandomness |
| 2431 | ** method. |
| 2432 | */ |
| 2433 | SQLITE_API void sqlite3_randomness(int N, void *P); |
| 2434 | |
| @@ -4146,13 +4167,13 @@ | |
| 4146 | ** CAPI3REF: Text Encodings |
| 4147 | ** |
| 4148 | ** These constant define integer codes that represent the various |
| 4149 | ** text encodings supported by SQLite. |
| 4150 | */ |
| 4151 | #define SQLITE_UTF8 1 |
| 4152 | #define SQLITE_UTF16LE 2 |
| 4153 | #define SQLITE_UTF16BE 3 |
| 4154 | #define SQLITE_UTF16 4 /* Use native byte order */ |
| 4155 | #define SQLITE_ANY 5 /* Deprecated */ |
| 4156 | #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ |
| 4157 | |
| 4158 | /* |
| @@ -5638,31 +5659,47 @@ | |
| 5638 | ** in other words, the same BLOB that would be selected by: |
| 5639 | ** |
| 5640 | ** <pre> |
| 5641 | ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow; |
| 5642 | ** </pre>)^ |
| 5643 | ** |
| 5644 | ** ^If the flags parameter is non-zero, then the BLOB is opened for read |
| 5645 | ** and write access. ^If it is zero, the BLOB is opened for read access. |
| 5646 | ** ^It is not possible to open a column that is part of an index or primary |
| 5647 | ** key for writing. ^If [foreign key constraints] are enabled, it is |
| 5648 | ** not possible to open a column that is part of a [child key] for writing. |
| 5649 | ** |
| 5650 | ** ^Note that the database name is not the filename that contains |
| 5651 | ** the database but rather the symbolic name of the database that |
| 5652 | ** appears after the AS keyword when the database is connected using [ATTACH]. |
| 5653 | ** ^For the main database file, the database name is "main". |
| 5654 | ** ^For TEMP tables, the database name is "temp". |
| 5655 | ** |
| 5656 | ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written |
| 5657 | ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set |
| 5658 | ** to be a null pointer.)^ |
| 5659 | ** ^This function sets the [database connection] error code and message |
| 5660 | ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related |
| 5661 | ** functions. ^Note that the *ppBlob variable is always initialized in a |
| 5662 | ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob |
| 5663 | ** regardless of the success or failure of this routine. |
| 5664 | ** |
| 5665 | ** ^(If the row that a BLOB handle points to is modified by an |
| 5666 | ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects |
| 5667 | ** then the BLOB handle is marked as "expired". |
| 5668 | ** This is true if any column of the row is changed, even a column |
| @@ -5676,17 +5713,13 @@ | |
| 5676 | ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of |
| 5677 | ** the opened blob. ^The size of a blob may not be changed by this |
| 5678 | ** interface. Use the [UPDATE] SQL command to change the size of a |
| 5679 | ** blob. |
| 5680 | ** |
| 5681 | ** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID] |
| 5682 | ** table. Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables. |
| 5683 | ** |
| 5684 | ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces |
| 5685 | ** and the built-in [zeroblob] SQL function can be used, if desired, |
| 5686 | ** to create an empty, zero-filled blob in which to read or write using |
| 5687 | ** this interface. |
| 5688 | ** |
| 5689 | ** To avoid a resource leak, every open [BLOB handle] should eventually |
| 5690 | ** be released by a call to [sqlite3_blob_close()]. |
| 5691 | */ |
| 5692 | SQLITE_API int sqlite3_blob_open( |
| @@ -5724,28 +5757,26 @@ | |
| 5724 | SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); |
| 5725 | |
| 5726 | /* |
| 5727 | ** CAPI3REF: Close A BLOB Handle |
| 5728 | ** |
| 5729 | ** ^Closes an open [BLOB handle]. |
| 5730 | ** |
| 5731 | ** ^Closing a BLOB shall cause the current transaction to commit |
| 5732 | ** if there are no other BLOBs, no pending prepared statements, and the |
| 5733 | ** database connection is in [autocommit mode]. |
| 5734 | ** ^If any writes were made to the BLOB, they might be held in cache |
| 5735 | ** until the close operation if they will fit. |
| 5736 | ** |
| 5737 | ** ^(Closing the BLOB often forces the changes |
| 5738 | ** out to disk and so if any I/O errors occur, they will likely occur |
| 5739 | ** at the time when the BLOB is closed. Any errors that occur during |
| 5740 | ** closing are reported as a non-zero return value.)^ |
| 5741 | ** |
| 5742 | ** ^(The BLOB is closed unconditionally. Even if this routine returns |
| 5743 | ** an error code, the BLOB is still closed.)^ |
| 5744 | ** |
| 5745 | ** ^Calling this routine with a null pointer (such as would be returned |
| 5746 | ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op. |
| 5747 | */ |
| 5748 | SQLITE_API int sqlite3_blob_close(sqlite3_blob *); |
| 5749 | |
| 5750 | /* |
| 5751 | ** CAPI3REF: Return The Size Of An Open BLOB |
| @@ -5791,36 +5822,39 @@ | |
| 5791 | SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); |
| 5792 | |
| 5793 | /* |
| 5794 | ** CAPI3REF: Write Data Into A BLOB Incrementally |
| 5795 | ** |
| 5796 | ** ^This function is used to write data into an open [BLOB handle] from a |
| 5797 | ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z |
| 5798 | ** into the open BLOB, starting at offset iOffset. |
| 5799 | ** |
| 5800 | ** ^If the [BLOB handle] passed as the first argument was not opened for |
| 5801 | ** writing (the flags parameter to [sqlite3_blob_open()] was zero), |
| 5802 | ** this function returns [SQLITE_READONLY]. |
| 5803 | ** |
| 5804 | ** ^This function may only modify the contents of the BLOB; it is |
| 5805 | ** not possible to increase the size of a BLOB using this API. |
| 5806 | ** ^If offset iOffset is less than N bytes from the end of the BLOB, |
| 5807 | ** [SQLITE_ERROR] is returned and no data is written. ^If N is |
| 5808 | ** less than zero [SQLITE_ERROR] is returned and no data is written. |
| 5809 | ** The size of the BLOB (and hence the maximum value of N+iOffset) |
| 5810 | ** can be determined using the [sqlite3_blob_bytes()] interface. |
| 5811 | ** |
| 5812 | ** ^An attempt to write to an expired [BLOB handle] fails with an |
| 5813 | ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred |
| 5814 | ** before the [BLOB handle] expired are not rolled back by the |
| 5815 | ** expiration of the handle, though of course those changes might |
| 5816 | ** have been overwritten by the statement that expired the BLOB handle |
| 5817 | ** or by other independent statements. |
| 5818 | ** |
| 5819 | ** ^(On success, sqlite3_blob_write() returns SQLITE_OK. |
| 5820 | ** Otherwise, an [error code] or an [extended error code] is returned.)^ |
| 5821 | ** |
| 5822 | ** This routine only works on a [BLOB handle] which has been created |
| 5823 | ** by a prior successful call to [sqlite3_blob_open()] and which has not |
| 5824 | ** been closed by [sqlite3_blob_close()]. Passing any other pointer in |
| 5825 | ** to this routine results in undefined and probably undesirable behavior. |
| 5826 | ** |
| @@ -5869,38 +5903,38 @@ | |
| 5869 | ** use by SQLite, code that links against SQLite is |
| 5870 | ** permitted to use any of these routines. |
| 5871 | ** |
| 5872 | ** The SQLite source code contains multiple implementations |
| 5873 | ** of these mutex routines. An appropriate implementation |
| 5874 | ** is selected automatically at compile-time. ^(The following |
| 5875 | ** implementations are available in the SQLite core: |
| 5876 | ** |
| 5877 | ** <ul> |
| 5878 | ** <li> SQLITE_MUTEX_PTHREADS |
| 5879 | ** <li> SQLITE_MUTEX_W32 |
| 5880 | ** <li> SQLITE_MUTEX_NOOP |
| 5881 | ** </ul>)^ |
| 5882 | ** |
| 5883 | ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines |
| 5884 | ** that does no real locking and is appropriate for use in |
| 5885 | ** a single-threaded application. ^The SQLITE_MUTEX_PTHREADS and |
| 5886 | ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix |
| 5887 | ** and Windows. |
| 5888 | ** |
| 5889 | ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor |
| 5890 | ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex |
| 5891 | ** implementation is included with the library. In this case the |
| 5892 | ** application must supply a custom mutex implementation using the |
| 5893 | ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function |
| 5894 | ** before calling sqlite3_initialize() or any other public sqlite3_ |
| 5895 | ** function that calls sqlite3_initialize().)^ |
| 5896 | ** |
| 5897 | ** ^The sqlite3_mutex_alloc() routine allocates a new |
| 5898 | ** mutex and returns a pointer to it. ^If it returns NULL |
| 5899 | ** that means that a mutex could not be allocated. ^SQLite |
| 5900 | ** will unwind its stack and return an error. ^(The argument |
| 5901 | ** to sqlite3_mutex_alloc() is one of these integer constants: |
| 5902 | ** |
| 5903 | ** <ul> |
| 5904 | ** <li> SQLITE_MUTEX_FAST |
| 5905 | ** <li> SQLITE_MUTEX_RECURSIVE |
| 5906 | ** <li> SQLITE_MUTEX_STATIC_MASTER |
| @@ -5909,68 +5943,64 @@ | |
| 5909 | ** <li> SQLITE_MUTEX_STATIC_PRNG |
| 5910 | ** <li> SQLITE_MUTEX_STATIC_LRU |
| 5911 | ** <li> SQLITE_MUTEX_STATIC_PMEM |
| 5912 | ** <li> SQLITE_MUTEX_STATIC_APP1 |
| 5913 | ** <li> SQLITE_MUTEX_STATIC_APP2 |
| 5914 | ** </ul>)^ |
| 5915 | ** |
| 5916 | ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) |
| 5917 | ** cause sqlite3_mutex_alloc() to create |
| 5918 | ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE |
| 5919 | ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. |
| 5920 | ** The mutex implementation does not need to make a distinction |
| 5921 | ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does |
| 5922 | ** not want to. ^SQLite will only request a recursive mutex in |
| 5923 | ** cases where it really needs one. ^If a faster non-recursive mutex |
| 5924 | ** implementation is available on the host platform, the mutex subsystem |
| 5925 | ** might return such a mutex in response to SQLITE_MUTEX_FAST. |
| 5926 | ** |
| 5927 | ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other |
| 5928 | ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return |
| 5929 | ** a pointer to a static preexisting mutex. ^Six static mutexes are |
| 5930 | ** used by the current version of SQLite. Future versions of SQLite |
| 5931 | ** may add additional static mutexes. Static mutexes are for internal |
| 5932 | ** use by SQLite only. Applications that use SQLite mutexes should |
| 5933 | ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or |
| 5934 | ** SQLITE_MUTEX_RECURSIVE. |
| 5935 | ** |
| 5936 | ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST |
| 5937 | ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() |
| 5938 | ** returns a different mutex on every call. ^But for the static |
| 5939 | ** mutex types, the same mutex is returned on every call that has |
| 5940 | ** the same type number. |
| 5941 | ** |
| 5942 | ** ^The sqlite3_mutex_free() routine deallocates a previously |
| 5943 | ** allocated dynamic mutex. ^SQLite is careful to deallocate every |
| 5944 | ** dynamic mutex that it allocates. The dynamic mutexes must not be in |
| 5945 | ** use when they are deallocated. Attempting to deallocate a static |
| 5946 | ** mutex results in undefined behavior. ^SQLite never deallocates |
| 5947 | ** a static mutex. |
| 5948 | ** |
| 5949 | ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt |
| 5950 | ** to enter a mutex. ^If another thread is already within the mutex, |
| 5951 | ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return |
| 5952 | ** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK] |
| 5953 | ** upon successful entry. ^(Mutexes created using |
| 5954 | ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. |
| 5955 | ** In such cases the, |
| 5956 | ** mutex must be exited an equal number of times before another thread |
| 5957 | ** can enter.)^ ^(If the same thread tries to enter any other |
| 5958 | ** kind of mutex more than once, the behavior is undefined. |
| 5959 | ** SQLite will never exhibit |
| 5960 | ** such behavior in its own use of mutexes.)^ |
| 5961 | ** |
| 5962 | ** ^(Some systems (for example, Windows 95) do not support the operation |
| 5963 | ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() |
| 5964 | ** will always return SQLITE_BUSY. The SQLite core only ever uses |
| 5965 | ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^ |
| 5966 | ** |
| 5967 | ** ^The sqlite3_mutex_leave() routine exits a mutex that was |
| 5968 | ** previously entered by the same thread. ^(The behavior |
| 5969 | ** is undefined if the mutex is not currently entered by the |
| 5970 | ** calling thread or is not currently allocated. SQLite will |
| 5971 | ** never do either.)^ |
| 5972 | ** |
| 5973 | ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or |
| 5974 | ** sqlite3_mutex_leave() is a NULL pointer, then all three routines |
| 5975 | ** behave as no-ops. |
| 5976 | ** |
| @@ -5987,13 +6017,13 @@ | |
| 5987 | ** |
| 5988 | ** An instance of this structure defines the low-level routines |
| 5989 | ** used to allocate and use mutexes. |
| 5990 | ** |
| 5991 | ** Usually, the default mutex implementations provided by SQLite are |
| 5992 | ** sufficient, however the user has the option of substituting a custom |
| 5993 | ** implementation for specialized deployments or systems for which SQLite |
| 5994 | ** does not provide a suitable implementation. In this case, the user |
| 5995 | ** creates and populates an instance of this structure to pass |
| 5996 | ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option. |
| 5997 | ** Additionally, an instance of this structure can be used as an |
| 5998 | ** output variable when querying the system for the current mutex |
| 5999 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| @@ -6030,17 +6060,17 @@ | |
| 6030 | ** by this structure are not required to handle this case, the results |
| 6031 | ** of passing a NULL pointer instead of a valid mutex handle are undefined |
| 6032 | ** (i.e. it is acceptable to provide an implementation that segfaults if |
| 6033 | ** it is passed a NULL pointer). |
| 6034 | ** |
| 6035 | ** The xMutexInit() method must be threadsafe. ^It must be harmless to |
| 6036 | ** invoke xMutexInit() multiple times within the same process and without |
| 6037 | ** intervening calls to xMutexEnd(). Second and subsequent calls to |
| 6038 | ** xMutexInit() must be no-ops. |
| 6039 | ** |
| 6040 | ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] |
| 6041 | ** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory |
| 6042 | ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite |
| 6043 | ** memory allocation for a fast or recursive mutex. |
| 6044 | ** |
| 6045 | ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is |
| 6046 | ** called, but only if the prior call to xMutexInit returned SQLITE_OK. |
| @@ -6062,33 +6092,33 @@ | |
| 6062 | |
| 6063 | /* |
| 6064 | ** CAPI3REF: Mutex Verification Routines |
| 6065 | ** |
| 6066 | ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines |
| 6067 | ** are intended for use inside assert() statements. ^The SQLite core |
| 6068 | ** never uses these routines except inside an assert() and applications |
| 6069 | ** are advised to follow the lead of the core. ^The SQLite core only |
| 6070 | ** provides implementations for these routines when it is compiled |
| 6071 | ** with the SQLITE_DEBUG flag. ^External mutex implementations |
| 6072 | ** are only required to provide these routines if SQLITE_DEBUG is |
| 6073 | ** defined and if NDEBUG is not defined. |
| 6074 | ** |
| 6075 | ** ^These routines should return true if the mutex in their argument |
| 6076 | ** is held or not held, respectively, by the calling thread. |
| 6077 | ** |
| 6078 | ** ^The implementation is not required to provide versions of these |
| 6079 | ** routines that actually work. If the implementation does not provide working |
| 6080 | ** versions of these routines, it should at least provide stubs that always |
| 6081 | ** return true so that one does not get spurious assertion failures. |
| 6082 | ** |
| 6083 | ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then |
| 6084 | ** the routine should return 1. This seems counter-intuitive since |
| 6085 | ** clearly the mutex cannot be held if it does not exist. But |
| 6086 | ** the reason the mutex does not exist is because the build is not |
| 6087 | ** using mutexes. And we do not want the assert() containing the |
| 6088 | ** call to sqlite3_mutex_held() to fail, so a non-zero return is |
| 6089 | ** the appropriate thing to do. ^The sqlite3_mutex_notheld() |
| 6090 | ** interface should also return 1 when given a NULL pointer. |
| 6091 | */ |
| 6092 | #ifndef NDEBUG |
| 6093 | SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*); |
| 6094 | SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); |
| @@ -6816,10 +6846,14 @@ | |
| 6816 | ** sqlite3_backup_init(D,N,S,M) identify the [database connection] |
| 6817 | ** and database name of the source database, respectively. |
| 6818 | ** ^The source and destination [database connections] (parameters S and D) |
| 6819 | ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with |
| 6820 | ** an error. |
| 6821 | ** |
| 6822 | ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is |
| 6823 | ** returned and an error code and error message are stored in the |
| 6824 | ** destination [database connection] D. |
| 6825 | ** ^The error code and message for the failed call to sqlite3_backup_init() |
| @@ -7409,10 +7443,102 @@ | |
| 7409 | /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */ |
| 7410 | #define SQLITE_FAIL 3 |
| 7411 | /* #define SQLITE_ABORT 4 // Also an error code */ |
| 7412 | #define SQLITE_REPLACE 5 |
| 7413 | |
| 7414 | |
| 7415 | |
| 7416 | /* |
| 7417 | ** Undo the hack that converts floating point types to integer for |
| 7418 | ** builds on processors without floating point support. |
| 7419 |
| --- src/sqlite3.h | |
| +++ src/sqlite3.h | |
| @@ -55,11 +55,11 @@ | |
| 55 | |
| 56 | |
| 57 | /* |
| 58 | ** These no-op macros are used in front of interfaces to mark those |
| 59 | ** interfaces as either deprecated or experimental. New applications |
| 60 | ** should not use deprecated interfaces - they are supported for backwards |
| 61 | ** compatibility only. Application writers should be aware that |
| 62 | ** experimental interfaces are subject to change in point releases. |
| 63 | ** |
| 64 | ** These macros used to resolve to various kinds of compiler magic that |
| 65 | ** would generate warning messages when they were used. But that |
| @@ -105,13 +105,13 @@ | |
| 105 | ** |
| 106 | ** See also: [sqlite3_libversion()], |
| 107 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 108 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 109 | */ |
| 110 | #define SQLITE_VERSION "3.8.8" |
| 111 | #define SQLITE_VERSION_NUMBER 3008008 |
| 112 | #define SQLITE_SOURCE_ID "2014-11-27 11:36:36 f095cde579e7417306e11b5c1d2dd90b6bb547d5" |
| 113 | |
| 114 | /* |
| 115 | ** CAPI3REF: Run-Time Library Version Numbers |
| 116 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 117 | ** |
| @@ -1502,29 +1502,31 @@ | |
| 1502 | ** it is not possible to set the Serialized [threading mode] and |
| 1503 | ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the |
| 1504 | ** SQLITE_CONFIG_SERIALIZED configuration option.</dd> |
| 1505 | ** |
| 1506 | ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt> |
| 1507 | ** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is |
| 1508 | ** a pointer to an instance of the [sqlite3_mem_methods] structure. |
| 1509 | ** The argument specifies |
| 1510 | ** alternative low-level memory allocation routines to be used in place of |
| 1511 | ** the memory allocation routines built into SQLite.)^ ^SQLite makes |
| 1512 | ** its own private copy of the content of the [sqlite3_mem_methods] structure |
| 1513 | ** before the [sqlite3_config()] call returns.</dd> |
| 1514 | ** |
| 1515 | ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt> |
| 1516 | ** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which |
| 1517 | ** is a pointer to an instance of the [sqlite3_mem_methods] structure. |
| 1518 | ** The [sqlite3_mem_methods] |
| 1519 | ** structure is filled with the currently defined memory allocation routines.)^ |
| 1520 | ** This option can be used to overload the default memory allocation |
| 1521 | ** routines with a wrapper that simulations memory allocation failure or |
| 1522 | ** tracks memory usage, for example. </dd> |
| 1523 | ** |
| 1524 | ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt> |
| 1525 | ** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int, |
| 1526 | ** interpreted as a boolean, which enables or disables the collection of |
| 1527 | ** memory allocation statistics. ^(When memory allocation statistics are disabled, the |
| 1528 | ** following SQLite interfaces become non-operational: |
| 1529 | ** <ul> |
| 1530 | ** <li> [sqlite3_memory_used()] |
| 1531 | ** <li> [sqlite3_memory_highwater()] |
| 1532 | ** <li> [sqlite3_soft_heap_limit64()] |
| @@ -1534,78 +1536,90 @@ | |
| 1536 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1537 | ** allocation statistics are disabled by default. |
| 1538 | ** </dd> |
| 1539 | ** |
| 1540 | ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt> |
| 1541 | ** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer |
| 1542 | ** that SQLite can use for scratch memory. ^(There are three arguments |
| 1543 | ** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte |
| 1544 | ** aligned memory buffer from which the scratch allocations will be |
| 1545 | ** drawn, the size of each scratch allocation (sz), |
| 1546 | ** and the maximum number of scratch allocations (N).)^ |
| 1547 | ** The first argument must be a pointer to an 8-byte aligned buffer |
| 1548 | ** of at least sz*N bytes of memory. |
| 1549 | ** ^SQLite will not use more than one scratch buffers per thread. |
| 1550 | ** ^SQLite will never request a scratch buffer that is more than 6 |
| 1551 | ** times the database page size. |
| 1552 | ** ^If SQLite needs needs additional |
| 1553 | ** scratch memory beyond what is provided by this configuration option, then |
| 1554 | ** [sqlite3_malloc()] will be used to obtain the memory needed.<p> |
| 1555 | ** ^When the application provides any amount of scratch memory using |
| 1556 | ** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large |
| 1557 | ** [sqlite3_malloc|heap allocations]. |
| 1558 | ** This can help [Robson proof|prevent memory allocation failures] due to heap |
| 1559 | ** fragmentation in low-memory embedded systems. |
| 1560 | ** </dd> |
| 1561 | ** |
| 1562 | ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1563 | ** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer |
| 1564 | ** that SQLite can use for the database page cache with the default page |
| 1565 | ** cache implementation. |
| 1566 | ** This configuration should not be used if an application-define page |
| 1567 | ** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2] |
| 1568 | ** configuration option. |
| 1569 | ** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned |
| 1570 | ** memory, the size of each page buffer (sz), and the number of pages (N). |
| 1571 | ** The sz argument should be the size of the largest database page |
| 1572 | ** (a power of two between 512 and 32768) plus some extra bytes for each |
| 1573 | ** page header. ^The number of extra bytes needed by the page header |
| 1574 | ** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option |
| 1575 | ** to [sqlite3_config()]. |
| 1576 | ** ^It is harmless, apart from the wasted memory, |
| 1577 | ** for the sz parameter to be larger than necessary. The first |
| 1578 | ** argument should pointer to an 8-byte aligned block of memory that |
| 1579 | ** is at least sz*N bytes of memory, otherwise subsequent behavior is |
| 1580 | ** undefined. |
| 1581 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1582 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1583 | ** page cache memory is needed beyond what is provided by this option, then |
| 1584 | ** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd> |
| 1585 | ** |
| 1586 | ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt> |
| 1587 | ** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer |
| 1588 | ** that SQLite will use for all of its dynamic memory allocation needs |
| 1589 | ** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE]. |
| 1590 | ** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled |
| 1591 | ** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns |
| 1592 | ** [SQLITE_ERROR] if invoked otherwise. |
| 1593 | ** ^There are three arguments to SQLITE_CONFIG_HEAP: |
| 1594 | ** An 8-byte aligned pointer to the memory, |
| 1595 | ** the number of bytes in the memory buffer, and the minimum allocation size. |
| 1596 | ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts |
| 1597 | ** to using its default memory allocator (the system malloc() implementation), |
| 1598 | ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the |
| 1599 | ** memory pointer is not NULL then the alternative memory |
| 1600 | ** allocator is engaged to handle all of SQLites memory allocation needs. |
| 1601 | ** The first pointer (the memory pointer) must be aligned to an 8-byte |
| 1602 | ** boundary or subsequent behavior of SQLite will be undefined. |
| 1603 | ** The minimum allocation size is capped at 2**12. Reasonable values |
| 1604 | ** for the minimum allocation size are 2**5 through 2**8.</dd> |
| 1605 | ** |
| 1606 | ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt> |
| 1607 | ** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a |
| 1608 | ** pointer to an instance of the [sqlite3_mutex_methods] structure. |
| 1609 | ** The argument specifies alternative low-level mutex routines to be used in place |
| 1610 | ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the |
| 1611 | ** content of the [sqlite3_mutex_methods] structure before the call to |
| 1612 | ** [sqlite3_config()] returns. ^If SQLite is compiled with |
| 1613 | ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then |
| 1614 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1615 | ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will |
| 1616 | ** return [SQLITE_ERROR].</dd> |
| 1617 | ** |
| 1618 | ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt> |
| 1619 | ** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which |
| 1620 | ** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The |
| 1621 | ** [sqlite3_mutex_methods] |
| 1622 | ** structure is filled with the currently defined mutex routines.)^ |
| 1623 | ** This option can be used to overload the default mutex allocation |
| 1624 | ** routines with a wrapper used to track mutex usage for performance |
| 1625 | ** profiling or testing, for example. ^If SQLite is compiled with |
| @@ -1613,28 +1627,28 @@ | |
| 1627 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1628 | ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will |
| 1629 | ** return [SQLITE_ERROR].</dd> |
| 1630 | ** |
| 1631 | ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt> |
| 1632 | ** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine |
| 1633 | ** the default size of lookaside memory on each [database connection]. |
| 1634 | ** The first argument is the |
| 1635 | ** size of each lookaside buffer slot and the second is the number of |
| 1636 | ** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE |
| 1637 | ** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] |
| 1638 | ** option to [sqlite3_db_config()] can be used to change the lookaside |
| 1639 | ** configuration on individual connections.)^ </dd> |
| 1640 | ** |
| 1641 | ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt> |
| 1642 | ** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is |
| 1643 | ** a pointer to an [sqlite3_pcache_methods2] object. This object specifies |
| 1644 | ** the interface to a custom page cache implementation.)^ |
| 1645 | ** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd> |
| 1646 | ** |
| 1647 | ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt> |
| 1648 | ** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which |
| 1649 | ** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of the current |
| 1650 | ** page cache implementation into that object.)^ </dd> |
| 1651 | ** |
| 1652 | ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt> |
| 1653 | ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite |
| 1654 | ** global [error log]. |
| @@ -1654,26 +1668,27 @@ | |
| 1668 | ** supplied by the application must not invoke any SQLite interface. |
| 1669 | ** In a multi-threaded application, the application-defined logger |
| 1670 | ** function must be threadsafe. </dd> |
| 1671 | ** |
| 1672 | ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI |
| 1673 | ** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int. |
| 1674 | ** If non-zero, then URI handling is globally enabled. If the parameter is zero, |
| 1675 | ** then URI handling is globally disabled.)^ ^If URI handling is globally enabled, |
| 1676 | ** all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or |
| 1677 | ** specified as part of [ATTACH] commands are interpreted as URIs, regardless |
| 1678 | ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database |
| 1679 | ** connection is opened. ^If it is globally disabled, filenames are |
| 1680 | ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the |
| 1681 | ** database connection is opened. ^(By default, URI handling is globally |
| 1682 | ** disabled. The default value may be changed by compiling with the |
| 1683 | ** [SQLITE_USE_URI] symbol defined.)^ |
| 1684 | ** |
| 1685 | ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN |
| 1686 | ** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer |
| 1687 | ** argument which is interpreted as a boolean in order to enable or disable |
| 1688 | ** the use of covering indices for full table scans in the query optimizer. |
| 1689 | ** ^The default setting is determined |
| 1690 | ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on" |
| 1691 | ** if that compile-time option is omitted. |
| 1692 | ** The ability to disable the use of covering indices for full table scans |
| 1693 | ** is because some incorrectly coded legacy applications might malfunction |
| 1694 | ** when the optimization is enabled. Providing the ability to |
| @@ -1709,23 +1724,32 @@ | |
| 1724 | ** that are the default mmap size limit (the default setting for |
| 1725 | ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit. |
| 1726 | ** ^The default setting can be overridden by each database connection using |
| 1727 | ** either the [PRAGMA mmap_size] command, or by using the |
| 1728 | ** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size |
| 1729 | ** will be silently truncated if necessary so that it does not exceed the |
| 1730 | ** compile-time maximum mmap size set by the |
| 1731 | ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^ |
| 1732 | ** ^If either argument to this option is negative, then that argument is |
| 1733 | ** changed to its compile-time default. |
| 1734 | ** |
| 1735 | ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]] |
| 1736 | ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE |
| 1737 | ** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is |
| 1738 | ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro defined. |
| 1739 | ** ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value |
| 1740 | ** that specifies the maximum size of the created heap. |
| 1741 | ** </dl> |
| 1742 | ** |
| 1743 | ** [[SQLITE_CONFIG_PCACHE_HDRSZ]] |
| 1744 | ** <dt>SQLITE_CONFIG_PCACHE_HDRSZ |
| 1745 | ** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which |
| 1746 | ** is a pointer to an integer and writes into that integer the number of extra |
| 1747 | ** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. The amount of |
| 1748 | ** extra space required can change depending on the compiler, |
| 1749 | ** target platform, and SQLite version. |
| 1750 | ** </dl> |
| 1751 | */ |
| 1752 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 1753 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 1754 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| 1755 | #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ |
| @@ -1746,10 +1770,11 @@ | |
| 1770 | #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ |
| 1771 | #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ |
| 1772 | #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ |
| 1773 | #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ |
| 1774 | #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ |
| 1775 | #define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */ |
| 1776 | |
| 1777 | /* |
| 1778 | ** CAPI3REF: Database Connection Configuration Options |
| 1779 | ** |
| 1780 | ** These constants are the available integer configuration options that |
| @@ -1873,51 +1898,49 @@ | |
| 1898 | SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); |
| 1899 | |
| 1900 | /* |
| 1901 | ** CAPI3REF: Count The Number Of Rows Modified |
| 1902 | ** |
| 1903 | ** ^This function returns the number of rows modified, inserted or |
| 1904 | ** deleted by the most recently completed INSERT, UPDATE or DELETE |
| 1905 | ** statement on the database connection specified by the only parameter. |
| 1906 | ** ^Executing any other type of SQL statement does not modify the value |
| 1907 | ** returned by this function. |
| 1908 | ** |
| 1909 | ** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are |
| 1910 | ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], |
| 1911 | ** [foreign key actions] or [REPLACE] constraint resolution are not counted. |
| 1912 | ** |
| 1913 | ** Changes to a view that are intercepted by |
| 1914 | ** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value |
| 1915 | ** returned by sqlite3_changes() immediately after an INSERT, UPDATE or |
| 1916 | ** DELETE statement run on a view is always zero. Only changes made to real |
| 1917 | ** tables are counted. |
| 1918 | ** |
| 1919 | ** Things are more complicated if the sqlite3_changes() function is |
| 1920 | ** executed while a trigger program is running. This may happen if the |
| 1921 | ** program uses the [changes() SQL function], or if some other callback |
| 1922 | ** function invokes sqlite3_changes() directly. Essentially: |
| 1923 | ** |
| 1924 | ** <ul> |
| 1925 | ** <li> ^(Before entering a trigger program the value returned by |
| 1926 | ** sqlite3_changes() function is saved. After the trigger program |
| 1927 | ** has finished, the original value is restored.)^ |
| 1928 | ** |
| 1929 | ** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE |
| 1930 | ** statement sets the value returned by sqlite3_changes() |
| 1931 | ** upon completion as normal. Of course, this value will not include |
| 1932 | ** any changes performed by sub-triggers, as the sqlite3_changes() |
| 1933 | ** value will be saved and restored after each sub-trigger has run.)^ |
| 1934 | ** </ul> |
| 1935 | ** |
| 1936 | ** ^This means that if the changes() SQL function (or similar) is used |
| 1937 | ** by the first INSERT, UPDATE or DELETE statement within a trigger, it |
| 1938 | ** returns the value as set when the calling statement began executing. |
| 1939 | ** ^If it is used by the second or subsequent such statement within a trigger |
| 1940 | ** program, the value returned reflects the number of rows modified by the |
| 1941 | ** previous INSERT, UPDATE or DELETE statement within the same trigger. |
| 1942 | ** |
| 1943 | ** See also the [sqlite3_total_changes()] interface, the |
| 1944 | ** [count_changes pragma], and the [changes() SQL function]. |
| 1945 | ** |
| 1946 | ** If a separate thread makes changes on the same database connection |
| @@ -1927,24 +1950,21 @@ | |
| 1950 | SQLITE_API int sqlite3_changes(sqlite3*); |
| 1951 | |
| 1952 | /* |
| 1953 | ** CAPI3REF: Total Number Of Rows Modified |
| 1954 | ** |
| 1955 | ** ^This function returns the total number of rows inserted, modified or |
| 1956 | ** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed |
| 1957 | ** since the database connection was opened, including those executed as |
| 1958 | ** part of trigger programs. ^Executing any other type of SQL statement |
| 1959 | ** does not affect the value returned by sqlite3_total_changes(). |
| 1960 | ** |
| 1961 | ** ^Changes made as part of [foreign key actions] are included in the |
| 1962 | ** count, but those made as part of REPLACE constraint resolution are |
| 1963 | ** not. ^Changes to a view that are intercepted by INSTEAD OF triggers |
| 1964 | ** are not counted. |
| 1965 | ** |
| 1966 | ** See also the [sqlite3_changes()] interface, the |
| 1967 | ** [count_changes pragma], and the [total_changes() SQL function]. |
| 1968 | ** |
| 1969 | ** If a separate thread makes changes on the same database connection |
| 1970 | ** while [sqlite3_total_changes()] is running then the value |
| @@ -2418,17 +2438,18 @@ | |
| 2438 | ** already uses the largest possible [ROWID]. The PRNG is also used for |
| 2439 | ** the build-in random() and randomblob() SQL functions. This interface allows |
| 2440 | ** applications to access the same PRNG for other purposes. |
| 2441 | ** |
| 2442 | ** ^A call to this routine stores N bytes of randomness into buffer P. |
| 2443 | ** ^The P parameter can be a NULL pointer. |
| 2444 | ** |
| 2445 | ** ^If this routine has not been previously called or if the previous |
| 2446 | ** call had N less than one or a NULL pointer for P, then the PRNG is |
| 2447 | ** seeded using randomness obtained from the xRandomness method of |
| 2448 | ** the default [sqlite3_vfs] object. |
| 2449 | ** ^If the previous call to this routine had an N of 1 or more and a |
| 2450 | ** non-NULL P then the pseudo-randomness is generated |
| 2451 | ** internally and without recourse to the [sqlite3_vfs] xRandomness |
| 2452 | ** method. |
| 2453 | */ |
| 2454 | SQLITE_API void sqlite3_randomness(int N, void *P); |
| 2455 | |
| @@ -4146,13 +4167,13 @@ | |
| 4167 | ** CAPI3REF: Text Encodings |
| 4168 | ** |
| 4169 | ** These constant define integer codes that represent the various |
| 4170 | ** text encodings supported by SQLite. |
| 4171 | */ |
| 4172 | #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */ |
| 4173 | #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */ |
| 4174 | #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */ |
| 4175 | #define SQLITE_UTF16 4 /* Use native byte order */ |
| 4176 | #define SQLITE_ANY 5 /* Deprecated */ |
| 4177 | #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ |
| 4178 | |
| 4179 | /* |
| @@ -5638,31 +5659,47 @@ | |
| 5659 | ** in other words, the same BLOB that would be selected by: |
| 5660 | ** |
| 5661 | ** <pre> |
| 5662 | ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow; |
| 5663 | ** </pre>)^ |
| 5664 | ** |
| 5665 | ** ^(Parameter zDb is not the filename that contains the database, but |
| 5666 | ** rather the symbolic name of the database. For attached databases, this is |
| 5667 | ** the name that appears after the AS keyword in the [ATTACH] statement. |
| 5668 | ** For the main database file, the database name is "main". For TEMP |
| 5669 | ** tables, the database name is "temp".)^ |
| 5670 | ** |
| 5671 | ** ^If the flags parameter is non-zero, then the BLOB is opened for read |
| 5672 | ** and write access. ^If the flags parameter is zero, the BLOB is opened for |
| 5673 | ** read-only access. |
| 5674 | ** |
| 5675 | ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored |
| 5676 | ** in *ppBlob. Otherwise an [error code] is returned and, unless the error |
| 5677 | ** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided |
| 5678 | ** the API is not misused, it is always safe to call [sqlite3_blob_close()] |
| 5679 | ** on *ppBlob after this function it returns. |
| 5680 | ** |
| 5681 | ** This function fails with SQLITE_ERROR if any of the following are true: |
| 5682 | ** <ul> |
| 5683 | ** <li> ^(Database zDb does not exist)^, |
| 5684 | ** <li> ^(Table zTable does not exist within database zDb)^, |
| 5685 | ** <li> ^(Table zTable is a WITHOUT ROWID table)^, |
| 5686 | ** <li> ^(Column zColumn does not exist)^, |
| 5687 | ** <li> ^(Row iRow is not present in the table)^, |
| 5688 | ** <li> ^(The specified column of row iRow contains a value that is not |
| 5689 | ** a TEXT or BLOB value)^, |
| 5690 | ** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE |
| 5691 | ** constraint and the blob is being opened for read/write access)^, |
| 5692 | ** <li> ^([foreign key constraints | Foreign key constraints] are enabled, |
| 5693 | ** column zColumn is part of a [child key] definition and the blob is |
| 5694 | ** being opened for read/write access)^. |
| 5695 | ** </ul> |
| 5696 | ** |
| 5697 | ** ^Unless it returns SQLITE_MISUSE, this function sets the |
| 5698 | ** [database connection] error code and message accessible via |
| 5699 | ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. |
| 5700 | ** |
| 5701 | ** |
| 5702 | ** ^(If the row that a BLOB handle points to is modified by an |
| 5703 | ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects |
| 5704 | ** then the BLOB handle is marked as "expired". |
| 5705 | ** This is true if any column of the row is changed, even a column |
| @@ -5676,17 +5713,13 @@ | |
| 5713 | ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of |
| 5714 | ** the opened blob. ^The size of a blob may not be changed by this |
| 5715 | ** interface. Use the [UPDATE] SQL command to change the size of a |
| 5716 | ** blob. |
| 5717 | ** |
| 5718 | ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces |
| 5719 | ** and the built-in [zeroblob] SQL function may be used to create a |
| 5720 | ** zero-filled blob to read or write using the incremental-blob interface. |
| 5721 | ** |
| 5722 | ** To avoid a resource leak, every open [BLOB handle] should eventually |
| 5723 | ** be released by a call to [sqlite3_blob_close()]. |
| 5724 | */ |
| 5725 | SQLITE_API int sqlite3_blob_open( |
| @@ -5724,28 +5757,26 @@ | |
| 5757 | SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); |
| 5758 | |
| 5759 | /* |
| 5760 | ** CAPI3REF: Close A BLOB Handle |
| 5761 | ** |
| 5762 | ** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed |
| 5763 | ** unconditionally. Even if this routine returns an error code, the |
| 5764 | ** handle is still closed.)^ |
| 5765 | ** |
| 5766 | ** ^If the blob handle being closed was opened for read-write access, and if |
| 5767 | ** the database is in auto-commit mode and there are no other open read-write |
| 5768 | ** blob handles or active write statements, the current transaction is |
| 5769 | ** committed. ^If an error occurs while committing the transaction, an error |
| 5770 | ** code is returned and the transaction rolled back. |
| 5771 | ** |
| 5772 | ** Calling this function with an argument that is not a NULL pointer or an |
| 5773 | ** open blob handle results in undefined behaviour. ^Calling this routine |
| 5774 | ** with a null pointer (such as would be returned by a failed call to |
| 5775 | ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function |
| 5776 | ** is passed a valid open blob handle, the values returned by the |
| 5777 | ** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning. |
| 5778 | */ |
| 5779 | SQLITE_API int sqlite3_blob_close(sqlite3_blob *); |
| 5780 | |
| 5781 | /* |
| 5782 | ** CAPI3REF: Return The Size Of An Open BLOB |
| @@ -5791,36 +5822,39 @@ | |
| 5822 | SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); |
| 5823 | |
| 5824 | /* |
| 5825 | ** CAPI3REF: Write Data Into A BLOB Incrementally |
| 5826 | ** |
| 5827 | ** ^(This function is used to write data into an open [BLOB handle] from a |
| 5828 | ** caller-supplied buffer. N bytes of data are copied from the buffer Z |
| 5829 | ** into the open BLOB, starting at offset iOffset.)^ |
| 5830 | ** |
| 5831 | ** ^(On success, sqlite3_blob_write() returns SQLITE_OK. |
| 5832 | ** Otherwise, an [error code] or an [extended error code] is returned.)^ |
| 5833 | ** ^Unless SQLITE_MISUSE is returned, this function sets the |
| 5834 | ** [database connection] error code and message accessible via |
| 5835 | ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. |
| 5836 | ** |
| 5837 | ** ^If the [BLOB handle] passed as the first argument was not opened for |
| 5838 | ** writing (the flags parameter to [sqlite3_blob_open()] was zero), |
| 5839 | ** this function returns [SQLITE_READONLY]. |
| 5840 | ** |
| 5841 | ** This function may only modify the contents of the BLOB; it is |
| 5842 | ** not possible to increase the size of a BLOB using this API. |
| 5843 | ** ^If offset iOffset is less than N bytes from the end of the BLOB, |
| 5844 | ** [SQLITE_ERROR] is returned and no data is written. The size of the |
| 5845 | ** BLOB (and hence the maximum value of N+iOffset) can be determined |
| 5846 | ** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less |
| 5847 | ** than zero [SQLITE_ERROR] is returned and no data is written. |
| 5848 | ** |
| 5849 | ** ^An attempt to write to an expired [BLOB handle] fails with an |
| 5850 | ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred |
| 5851 | ** before the [BLOB handle] expired are not rolled back by the |
| 5852 | ** expiration of the handle, though of course those changes might |
| 5853 | ** have been overwritten by the statement that expired the BLOB handle |
| 5854 | ** or by other independent statements. |
| 5855 | ** |
| 5856 | ** This routine only works on a [BLOB handle] which has been created |
| 5857 | ** by a prior successful call to [sqlite3_blob_open()] and which has not |
| 5858 | ** been closed by [sqlite3_blob_close()]. Passing any other pointer in |
| 5859 | ** to this routine results in undefined and probably undesirable behavior. |
| 5860 | ** |
| @@ -5869,38 +5903,38 @@ | |
| 5903 | ** use by SQLite, code that links against SQLite is |
| 5904 | ** permitted to use any of these routines. |
| 5905 | ** |
| 5906 | ** The SQLite source code contains multiple implementations |
| 5907 | ** of these mutex routines. An appropriate implementation |
| 5908 | ** is selected automatically at compile-time. The following |
| 5909 | ** implementations are available in the SQLite core: |
| 5910 | ** |
| 5911 | ** <ul> |
| 5912 | ** <li> SQLITE_MUTEX_PTHREADS |
| 5913 | ** <li> SQLITE_MUTEX_W32 |
| 5914 | ** <li> SQLITE_MUTEX_NOOP |
| 5915 | ** </ul> |
| 5916 | ** |
| 5917 | ** The SQLITE_MUTEX_NOOP implementation is a set of routines |
| 5918 | ** that does no real locking and is appropriate for use in |
| 5919 | ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and |
| 5920 | ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix |
| 5921 | ** and Windows. |
| 5922 | ** |
| 5923 | ** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor |
| 5924 | ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex |
| 5925 | ** implementation is included with the library. In this case the |
| 5926 | ** application must supply a custom mutex implementation using the |
| 5927 | ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function |
| 5928 | ** before calling sqlite3_initialize() or any other public sqlite3_ |
| 5929 | ** function that calls sqlite3_initialize(). |
| 5930 | ** |
| 5931 | ** ^The sqlite3_mutex_alloc() routine allocates a new |
| 5932 | ** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc() |
| 5933 | ** routine returns NULL if it is unable to allocate the requested |
| 5934 | ** mutex. The argument to sqlite3_mutex_alloc() must one of these |
| 5935 | ** integer constants: |
| 5936 | ** |
| 5937 | ** <ul> |
| 5938 | ** <li> SQLITE_MUTEX_FAST |
| 5939 | ** <li> SQLITE_MUTEX_RECURSIVE |
| 5940 | ** <li> SQLITE_MUTEX_STATIC_MASTER |
| @@ -5909,68 +5943,64 @@ | |
| 5943 | ** <li> SQLITE_MUTEX_STATIC_PRNG |
| 5944 | ** <li> SQLITE_MUTEX_STATIC_LRU |
| 5945 | ** <li> SQLITE_MUTEX_STATIC_PMEM |
| 5946 | ** <li> SQLITE_MUTEX_STATIC_APP1 |
| 5947 | ** <li> SQLITE_MUTEX_STATIC_APP2 |
| 5948 | ** <li> SQLITE_MUTEX_STATIC_APP3 |
| 5949 | ** </ul> |
| 5950 | ** |
| 5951 | ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) |
| 5952 | ** cause sqlite3_mutex_alloc() to create |
| 5953 | ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE |
| 5954 | ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. |
| 5955 | ** The mutex implementation does not need to make a distinction |
| 5956 | ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does |
| 5957 | ** not want to. SQLite will only request a recursive mutex in |
| 5958 | ** cases where it really needs one. If a faster non-recursive mutex |
| 5959 | ** implementation is available on the host platform, the mutex subsystem |
| 5960 | ** might return such a mutex in response to SQLITE_MUTEX_FAST. |
| 5961 | ** |
| 5962 | ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other |
| 5963 | ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return |
| 5964 | ** a pointer to a static preexisting mutex. ^Nine static mutexes are |
| 5965 | ** used by the current version of SQLite. Future versions of SQLite |
| 5966 | ** may add additional static mutexes. Static mutexes are for internal |
| 5967 | ** use by SQLite only. Applications that use SQLite mutexes should |
| 5968 | ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or |
| 5969 | ** SQLITE_MUTEX_RECURSIVE. |
| 5970 | ** |
| 5971 | ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST |
| 5972 | ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() |
| 5973 | ** returns a different mutex on every call. ^For the static |
| 5974 | ** mutex types, the same mutex is returned on every call that has |
| 5975 | ** the same type number. |
| 5976 | ** |
| 5977 | ** ^The sqlite3_mutex_free() routine deallocates a previously |
| 5978 | ** allocated dynamic mutex. Attempting to deallocate a static |
| 5979 | ** mutex results in undefined behavior. |
| 5980 | ** |
| 5981 | ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt |
| 5982 | ** to enter a mutex. ^If another thread is already within the mutex, |
| 5983 | ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return |
| 5984 | ** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK] |
| 5985 | ** upon successful entry. ^(Mutexes created using |
| 5986 | ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. |
| 5987 | ** In such cases, the |
| 5988 | ** mutex must be exited an equal number of times before another thread |
| 5989 | ** can enter.)^ If the same thread tries to enter any mutex other |
| 5990 | ** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined. |
| 5991 | ** |
| 5992 | ** ^(Some systems (for example, Windows 95) do not support the operation |
| 5993 | ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() |
| 5994 | ** will always return SQLITE_BUSY. The SQLite core only ever uses |
| 5995 | ** sqlite3_mutex_try() as an optimization so this is acceptable |
| 5996 | ** behavior.)^ |
| 5997 | ** |
| 5998 | ** ^The sqlite3_mutex_leave() routine exits a mutex that was |
| 5999 | ** previously entered by the same thread. The behavior |
| 6000 | ** is undefined if the mutex is not currently entered by the |
| 6001 | ** calling thread or is not currently allocated. |
| 6002 | ** |
| 6003 | ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or |
| 6004 | ** sqlite3_mutex_leave() is a NULL pointer, then all three routines |
| 6005 | ** behave as no-ops. |
| 6006 | ** |
| @@ -5987,13 +6017,13 @@ | |
| 6017 | ** |
| 6018 | ** An instance of this structure defines the low-level routines |
| 6019 | ** used to allocate and use mutexes. |
| 6020 | ** |
| 6021 | ** Usually, the default mutex implementations provided by SQLite are |
| 6022 | ** sufficient, however the application has the option of substituting a custom |
| 6023 | ** implementation for specialized deployments or systems for which SQLite |
| 6024 | ** does not provide a suitable implementation. In this case, the application |
| 6025 | ** creates and populates an instance of this structure to pass |
| 6026 | ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option. |
| 6027 | ** Additionally, an instance of this structure can be used as an |
| 6028 | ** output variable when querying the system for the current mutex |
| 6029 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| @@ -6030,17 +6060,17 @@ | |
| 6060 | ** by this structure are not required to handle this case, the results |
| 6061 | ** of passing a NULL pointer instead of a valid mutex handle are undefined |
| 6062 | ** (i.e. it is acceptable to provide an implementation that segfaults if |
| 6063 | ** it is passed a NULL pointer). |
| 6064 | ** |
| 6065 | ** The xMutexInit() method must be threadsafe. It must be harmless to |
| 6066 | ** invoke xMutexInit() multiple times within the same process and without |
| 6067 | ** intervening calls to xMutexEnd(). Second and subsequent calls to |
| 6068 | ** xMutexInit() must be no-ops. |
| 6069 | ** |
| 6070 | ** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] |
| 6071 | ** and its associates). Similarly, xMutexAlloc() must not use SQLite memory |
| 6072 | ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite |
| 6073 | ** memory allocation for a fast or recursive mutex. |
| 6074 | ** |
| 6075 | ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is |
| 6076 | ** called, but only if the prior call to xMutexInit returned SQLITE_OK. |
| @@ -6062,33 +6092,33 @@ | |
| 6092 | |
| 6093 | /* |
| 6094 | ** CAPI3REF: Mutex Verification Routines |
| 6095 | ** |
| 6096 | ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines |
| 6097 | ** are intended for use inside assert() statements. The SQLite core |
| 6098 | ** never uses these routines except inside an assert() and applications |
| 6099 | ** are advised to follow the lead of the core. The SQLite core only |
| 6100 | ** provides implementations for these routines when it is compiled |
| 6101 | ** with the SQLITE_DEBUG flag. External mutex implementations |
| 6102 | ** are only required to provide these routines if SQLITE_DEBUG is |
| 6103 | ** defined and if NDEBUG is not defined. |
| 6104 | ** |
| 6105 | ** These routines should return true if the mutex in their argument |
| 6106 | ** is held or not held, respectively, by the calling thread. |
| 6107 | ** |
| 6108 | ** The implementation is not required to provide versions of these |
| 6109 | ** routines that actually work. If the implementation does not provide working |
| 6110 | ** versions of these routines, it should at least provide stubs that always |
| 6111 | ** return true so that one does not get spurious assertion failures. |
| 6112 | ** |
| 6113 | ** If the argument to sqlite3_mutex_held() is a NULL pointer then |
| 6114 | ** the routine should return 1. This seems counter-intuitive since |
| 6115 | ** clearly the mutex cannot be held if it does not exist. But |
| 6116 | ** the reason the mutex does not exist is because the build is not |
| 6117 | ** using mutexes. And we do not want the assert() containing the |
| 6118 | ** call to sqlite3_mutex_held() to fail, so a non-zero return is |
| 6119 | ** the appropriate thing to do. The sqlite3_mutex_notheld() |
| 6120 | ** interface should also return 1 when given a NULL pointer. |
| 6121 | */ |
| 6122 | #ifndef NDEBUG |
| 6123 | SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*); |
| 6124 | SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); |
| @@ -6816,10 +6846,14 @@ | |
| 6846 | ** sqlite3_backup_init(D,N,S,M) identify the [database connection] |
| 6847 | ** and database name of the source database, respectively. |
| 6848 | ** ^The source and destination [database connections] (parameters S and D) |
| 6849 | ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with |
| 6850 | ** an error. |
| 6851 | ** |
| 6852 | ** ^A call to sqlite3_backup_init() will fail, returning SQLITE_ERROR, if |
| 6853 | ** there is already a read or read-write transaction open on the |
| 6854 | ** destination database. |
| 6855 | ** |
| 6856 | ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is |
| 6857 | ** returned and an error code and error message are stored in the |
| 6858 | ** destination [database connection] D. |
| 6859 | ** ^The error code and message for the failed call to sqlite3_backup_init() |
| @@ -7409,10 +7443,102 @@ | |
| 7443 | /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */ |
| 7444 | #define SQLITE_FAIL 3 |
| 7445 | /* #define SQLITE_ABORT 4 // Also an error code */ |
| 7446 | #define SQLITE_REPLACE 5 |
| 7447 | |
| 7448 | /* |
| 7449 | ** CAPI3REF: Prepared Statement Scan Status Opcodes |
| 7450 | ** KEYWORDS: {scanstatus options} |
| 7451 | ** |
| 7452 | ** The following constants can be used for the T parameter to the |
| 7453 | ** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a |
| 7454 | ** different metric for sqlite3_stmt_scanstatus() to return. |
| 7455 | ** |
| 7456 | ** <dl> |
| 7457 | ** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt> |
| 7458 | ** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the |
| 7459 | ** total number of times that the X-th loop has run.</dd> |
| 7460 | ** |
| 7461 | ** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt> |
| 7462 | ** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set to the |
| 7463 | ** total number of rows examined by all iterations of the X-th loop.</dd> |
| 7464 | ** |
| 7465 | ** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt> |
| 7466 | ** <dd>^The "double" variable pointed to by the T parameter will be set to the |
| 7467 | ** query planner's estimate for the average number of rows output from each |
| 7468 | ** iteration of the X-th loop. If the query planner's estimates was accurate, |
| 7469 | ** then this value will approximate the quotient NVISIT/NLOOP and the |
| 7470 | ** product of this value for all prior loops with the same SELECTID will |
| 7471 | ** be the NLOOP value for the current loop. |
| 7472 | ** |
| 7473 | ** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt> |
| 7474 | ** <dd>^The "const char *" variable pointed to by the T parameter will be set to |
| 7475 | ** a zero-terminated UTF-8 string containing the name of the index or table used |
| 7476 | ** for the X-th loop. |
| 7477 | ** |
| 7478 | ** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt> |
| 7479 | ** <dd>^The "const char *" variable pointed to by the T parameter will be set to |
| 7480 | ** a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] description |
| 7481 | ** for the X-th loop. |
| 7482 | ** |
| 7483 | ** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt> |
| 7484 | ** <dd>^The "int" variable pointed to by the T parameter will be set to the |
| 7485 | ** "select-id" for the X-th loop. The select-id identifies which query or |
| 7486 | ** subquery the loop is part of. The main query has a select-id of zero. |
| 7487 | ** The select-id is the same value as is output in the first column |
| 7488 | ** of an [EXPLAIN QUERY PLAN] query. |
| 7489 | ** </dl> |
| 7490 | */ |
| 7491 | #define SQLITE_SCANSTAT_NLOOP 0 |
| 7492 | #define SQLITE_SCANSTAT_NVISIT 1 |
| 7493 | #define SQLITE_SCANSTAT_EST 2 |
| 7494 | #define SQLITE_SCANSTAT_NAME 3 |
| 7495 | #define SQLITE_SCANSTAT_EXPLAIN 4 |
| 7496 | #define SQLITE_SCANSTAT_SELECTID 5 |
| 7497 | |
| 7498 | /* |
| 7499 | ** CAPI3REF: Prepared Statement Scan Status |
| 7500 | ** |
| 7501 | ** Return status data for a single loop within query pStmt. |
| 7502 | ** |
| 7503 | ** The "iScanStatusOp" parameter determines which status information to return. |
| 7504 | ** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior of |
| 7505 | ** this interface is undefined. |
| 7506 | ** ^The requested measurement is written into a variable pointed to by |
| 7507 | ** the "pOut" parameter. |
| 7508 | ** Parameter "idx" identifies the specific loop to retrieve statistics for. |
| 7509 | ** Loops are numbered starting from zero. ^If idx is out of range - less than |
| 7510 | ** zero or greater than or equal to the total number of loops used to implement |
| 7511 | ** the statement - a non-zero value is returned and the variable that pOut |
| 7512 | ** points to is unchanged. |
| 7513 | ** |
| 7514 | ** ^Statistics might not be available for all loops in all statements. ^In cases |
| 7515 | ** where there exist loops with no available statistics, this function behaves |
| 7516 | ** as if the loop did not exist - it returns non-zero and leave the variable |
| 7517 | ** that pOut points to unchanged. |
| 7518 | ** |
| 7519 | ** This API is only available if the library is built with pre-processor |
| 7520 | ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. |
| 7521 | ** |
| 7522 | ** See also: [sqlite3_stmt_scanstatus_reset()] |
| 7523 | */ |
| 7524 | SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus( |
| 7525 | sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ |
| 7526 | int idx, /* Index of loop to report on */ |
| 7527 | int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ |
| 7528 | void *pOut /* Result written here */ |
| 7529 | ); |
| 7530 | |
| 7531 | /* |
| 7532 | ** CAPI3REF: Zero Scan-Status Counters |
| 7533 | ** |
| 7534 | ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters. |
| 7535 | ** |
| 7536 | ** This API is only available if the library is built with pre-processor |
| 7537 | ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. |
| 7538 | */ |
| 7539 | SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); |
| 7540 | |
| 7541 | |
| 7542 | /* |
| 7543 | ** Undo the hack that converts floating point types to integer for |
| 7544 | ** builds on processors without floating point support. |
| 7545 |