| | @@ -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.1. 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.1" |
| 235 | | -#define SQLITE_VERSION_NUMBER 3008007 |
| 236 | | -#define SQLITE_SOURCE_ID "2014-10-29 01:27:43 83afe23e553e802c0947c80d0ffdd120423e7c52" |
| 234 | +#define SQLITE_VERSION "3.8.8" |
| 235 | +#define SQLITE_VERSION_NUMBER 3008008 |
| 236 | +#define SQLITE_SOURCE_ID "2014-12-06 14:56:49 6aeece19a235344be2537e66a3fe08b1febfb5a0" |
| 237 | 237 | |
| 238 | 238 | /* |
| 239 | 239 | ** CAPI3REF: Run-Time Library Version Numbers |
| 240 | 240 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 241 | 241 | ** |
| | @@ -1343,11 +1343,11 @@ |
| 1343 | 1343 | ** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED |
| 1344 | 1344 | ** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE |
| 1345 | 1345 | ** </ul> |
| 1346 | 1346 | ** |
| 1347 | 1347 | ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as |
| 1348 | | -** was given no the corresponding lock. |
| 1348 | +** was given on the corresponding lock. |
| 1349 | 1349 | ** |
| 1350 | 1350 | ** The xShmLock method can transition between unlocked and SHARED or |
| 1351 | 1351 | ** between unlocked and EXCLUSIVE. It cannot transition between SHARED |
| 1352 | 1352 | ** and EXCLUSIVE. |
| 1353 | 1353 | */ |
| | @@ -1626,30 +1626,32 @@ |
| 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 |
| 1650 | | -** following SQLite interfaces become non-operational: |
| 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 |
| 1652 | +** disabled, the 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()] |
| 1655 | 1657 | ** <li> [sqlite3_status()] |
| | @@ -1658,78 +1660,92 @@ |
| 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 |
| 1694 | +** 8-byte aligned |
| 1684 | 1695 | ** memory, the size of each page buffer (sz), and the number of pages (N). |
| 1685 | 1696 | ** 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. |
| 1697 | +** (a power of two between 512 and 32768) plus some extra bytes for each |
| 1698 | +** page header. ^The number of extra bytes needed by the page header |
| 1699 | +** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option |
| 1700 | +** to [sqlite3_config()]. |
| 1701 | +** ^It is harmless, apart from the wasted memory, |
| 1702 | +** for the sz parameter to be larger than necessary. The first |
| 1703 | +** argument should pointer to an 8-byte aligned block of memory that |
| 1704 | +** is at least sz*N bytes of memory, otherwise subsequent behavior is |
| 1705 | +** undefined. |
| 1691 | 1706 | ** ^SQLite will use the memory provided by the first argument to satisfy its |
| 1692 | 1707 | ** memory needs for the first N pages that it adds to cache. ^If additional |
| 1693 | 1708 | ** 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> |
| 1709 | +** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd> |
| 1698 | 1710 | ** |
| 1699 | 1711 | ** [[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, |
| 1712 | +** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer |
| 1713 | +** that SQLite will use for all of its dynamic memory allocation needs |
| 1714 | +** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and |
| 1715 | +** [SQLITE_CONFIG_PAGECACHE]. |
| 1716 | +** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled |
| 1717 | +** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns |
| 1718 | +** [SQLITE_ERROR] if invoked otherwise. |
| 1719 | +** ^There are three arguments to SQLITE_CONFIG_HEAP: |
| 1720 | +** An 8-byte aligned pointer to the memory, |
| 1704 | 1721 | ** the number of bytes in the memory buffer, and the minimum allocation size. |
| 1705 | 1722 | ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts |
| 1706 | 1723 | ** to using its default memory allocator (the system malloc() implementation), |
| 1707 | 1724 | ** 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 |
| 1725 | +** memory pointer is not NULL then the alternative memory |
| 1710 | 1726 | ** allocator is engaged to handle all of SQLites memory allocation needs. |
| 1711 | 1727 | ** The first pointer (the memory pointer) must be aligned to an 8-byte |
| 1712 | 1728 | ** boundary or subsequent behavior of SQLite will be undefined. |
| 1713 | 1729 | ** The minimum allocation size is capped at 2**12. Reasonable values |
| 1714 | 1730 | ** for the minimum allocation size are 2**5 through 2**8.</dd> |
| 1715 | 1731 | ** |
| 1716 | 1732 | ** [[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 |
| 1733 | +** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a |
| 1734 | +** pointer to an instance of the [sqlite3_mutex_methods] structure. |
| 1735 | +** The argument specifies alternative low-level mutex routines to be used |
| 1736 | +** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of |
| 1737 | +** the content of the [sqlite3_mutex_methods] structure before the call to |
| 1722 | 1738 | ** [sqlite3_config()] returns. ^If SQLite is compiled with |
| 1723 | 1739 | ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then |
| 1724 | 1740 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1725 | 1741 | ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will |
| 1726 | 1742 | ** return [SQLITE_ERROR].</dd> |
| 1727 | 1743 | ** |
| 1728 | 1744 | ** [[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 |
| 1745 | +** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which |
| 1746 | +** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The |
| 1731 | 1747 | ** [sqlite3_mutex_methods] |
| 1732 | 1748 | ** structure is filled with the currently defined mutex routines.)^ |
| 1733 | 1749 | ** This option can be used to overload the default mutex allocation |
| 1734 | 1750 | ** routines with a wrapper used to track mutex usage for performance |
| 1735 | 1751 | ** profiling or testing, for example. ^If SQLite is compiled with |
| | @@ -1737,29 +1753,29 @@ |
| 1737 | 1753 | ** the entire mutexing subsystem is omitted from the build and hence calls to |
| 1738 | 1754 | ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will |
| 1739 | 1755 | ** return [SQLITE_ERROR].</dd> |
| 1740 | 1756 | ** |
| 1741 | 1757 | ** [[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 |
| 1758 | +** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine |
| 1759 | +** the default size of lookaside memory on each [database connection]. |
| 1760 | +** The first argument is the |
| 1745 | 1761 | ** 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 |
| 1762 | +** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE |
| 1763 | +** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] |
| 1764 | +** option to [sqlite3_db_config()] can be used to change the lookaside |
| 1749 | 1765 | ** configuration on individual connections.)^ </dd> |
| 1750 | 1766 | ** |
| 1751 | 1767 | ** [[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> |
| 1768 | +** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is |
| 1769 | +** a pointer to an [sqlite3_pcache_methods2] object. This object specifies |
| 1770 | +** the interface to a custom page cache implementation.)^ |
| 1771 | +** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd> |
| 1756 | 1772 | ** |
| 1757 | 1773 | ** [[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> |
| 1774 | +** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which |
| 1775 | +** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of |
| 1776 | +** the current page cache implementation into that object.)^ </dd> |
| 1761 | 1777 | ** |
| 1762 | 1778 | ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt> |
| 1763 | 1779 | ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite |
| 1764 | 1780 | ** global [error log]. |
| 1765 | 1781 | ** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a |
| | @@ -1778,26 +1794,28 @@ |
| 1778 | 1794 | ** supplied by the application must not invoke any SQLite interface. |
| 1779 | 1795 | ** In a multi-threaded application, the application-defined logger |
| 1780 | 1796 | ** function must be threadsafe. </dd> |
| 1781 | 1797 | ** |
| 1782 | 1798 | ** [[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 |
| 1799 | +** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int. |
| 1800 | +** If non-zero, then URI handling is globally enabled. If the parameter is zero, |
| 1801 | +** then URI handling is globally disabled.)^ ^If URI handling is globally |
| 1802 | +** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], |
| 1803 | +** [sqlite3_open16()] or |
| 1787 | 1804 | ** specified as part of [ATTACH] commands are interpreted as URIs, regardless |
| 1788 | 1805 | ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database |
| 1789 | 1806 | ** connection is opened. ^If it is globally disabled, filenames are |
| 1790 | 1807 | ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the |
| 1791 | 1808 | ** database connection is opened. ^(By default, URI handling is globally |
| 1792 | 1809 | ** disabled. The default value may be changed by compiling with the |
| 1793 | 1810 | ** [SQLITE_USE_URI] symbol defined.)^ |
| 1794 | 1811 | ** |
| 1795 | 1812 | ** [[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 |
| 1813 | +** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer |
| 1814 | +** argument which is interpreted as a boolean in order to enable or disable |
| 1815 | +** the use of covering indices for full table scans in the query optimizer. |
| 1816 | +** ^The default setting is determined |
| 1799 | 1817 | ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on" |
| 1800 | 1818 | ** if that compile-time option is omitted. |
| 1801 | 1819 | ** The ability to disable the use of covering indices for full table scans |
| 1802 | 1820 | ** is because some incorrectly coded legacy applications might malfunction |
| 1803 | 1821 | ** when the optimization is enabled. Providing the ability to |
| | @@ -1833,23 +1851,32 @@ |
| 1833 | 1851 | ** that are the default mmap size limit (the default setting for |
| 1834 | 1852 | ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit. |
| 1835 | 1853 | ** ^The default setting can be overridden by each database connection using |
| 1836 | 1854 | ** either the [PRAGMA mmap_size] command, or by using the |
| 1837 | 1855 | ** [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 |
| 1856 | +** will be silently truncated if necessary so that it does not exceed the |
| 1857 | +** compile-time maximum mmap size set by the |
| 1840 | 1858 | ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^ |
| 1841 | 1859 | ** ^If either argument to this option is negative, then that argument is |
| 1842 | 1860 | ** changed to its compile-time default. |
| 1843 | 1861 | ** |
| 1844 | 1862 | ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]] |
| 1845 | 1863 | ** <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 |
| 1864 | +** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is |
| 1865 | +** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro |
| 1866 | +** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value |
| 1849 | 1867 | ** that specifies the maximum size of the created heap. |
| 1850 | 1868 | ** </dl> |
| 1869 | +** |
| 1870 | +** [[SQLITE_CONFIG_PCACHE_HDRSZ]] |
| 1871 | +** <dt>SQLITE_CONFIG_PCACHE_HDRSZ |
| 1872 | +** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which |
| 1873 | +** is a pointer to an integer and writes into that integer the number of extra |
| 1874 | +** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. |
| 1875 | +** The amount of extra space required can change depending on the compiler, |
| 1876 | +** target platform, and SQLite version. |
| 1877 | +** </dl> |
| 1851 | 1878 | */ |
| 1852 | 1879 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 1853 | 1880 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 1854 | 1881 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| 1855 | 1882 | #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ |
| | @@ -1870,10 +1897,11 @@ |
| 1870 | 1897 | #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ |
| 1871 | 1898 | #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ |
| 1872 | 1899 | #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ |
| 1873 | 1900 | #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ |
| 1874 | 1901 | #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ |
| 1902 | +#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */ |
| 1875 | 1903 | |
| 1876 | 1904 | /* |
| 1877 | 1905 | ** CAPI3REF: Database Connection Configuration Options |
| 1878 | 1906 | ** |
| 1879 | 1907 | ** These constants are the available integer configuration options that |
| | @@ -1997,51 +2025,49 @@ |
| 1997 | 2025 | SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); |
| 1998 | 2026 | |
| 1999 | 2027 | /* |
| 2000 | 2028 | ** CAPI3REF: Count The Number Of Rows Modified |
| 2001 | 2029 | ** |
| 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.)^ |
| 2030 | +** ^This function returns the number of rows modified, inserted or |
| 2031 | +** deleted by the most recently completed INSERT, UPDATE or DELETE |
| 2032 | +** statement on the database connection specified by the only parameter. |
| 2033 | +** ^Executing any other type of SQL statement does not modify the value |
| 2034 | +** returned by this function. |
| 2035 | +** |
| 2036 | +** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are |
| 2037 | +** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], |
| 2038 | +** [foreign key actions] or [REPLACE] constraint resolution are not counted. |
| 2039 | +** |
| 2040 | +** Changes to a view that are intercepted by |
| 2041 | +** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value |
| 2042 | +** returned by sqlite3_changes() immediately after an INSERT, UPDATE or |
| 2043 | +** DELETE statement run on a view is always zero. Only changes made to real |
| 2044 | +** tables are counted. |
| 2045 | +** |
| 2046 | +** Things are more complicated if the sqlite3_changes() function is |
| 2047 | +** executed while a trigger program is running. This may happen if the |
| 2048 | +** program uses the [changes() SQL function], or if some other callback |
| 2049 | +** function invokes sqlite3_changes() directly. Essentially: |
| 2050 | +** |
| 2051 | +** <ul> |
| 2052 | +** <li> ^(Before entering a trigger program the value returned by |
| 2053 | +** sqlite3_changes() function is saved. After the trigger program |
| 2054 | +** has finished, the original value is restored.)^ |
| 2055 | +** |
| 2056 | +** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE |
| 2057 | +** statement sets the value returned by sqlite3_changes() |
| 2058 | +** upon completion as normal. Of course, this value will not include |
| 2059 | +** any changes performed by sub-triggers, as the sqlite3_changes() |
| 2060 | +** value will be saved and restored after each sub-trigger has run.)^ |
| 2061 | +** </ul> |
| 2062 | +** |
| 2063 | +** ^This means that if the changes() SQL function (or similar) is used |
| 2064 | +** by the first INSERT, UPDATE or DELETE statement within a trigger, it |
| 2065 | +** returns the value as set when the calling statement began executing. |
| 2066 | +** ^If it is used by the second or subsequent such statement within a trigger |
| 2067 | +** program, the value returned reflects the number of rows modified by the |
| 2068 | +** previous INSERT, UPDATE or DELETE statement within the same trigger. |
| 2043 | 2069 | ** |
| 2044 | 2070 | ** See also the [sqlite3_total_changes()] interface, the |
| 2045 | 2071 | ** [count_changes pragma], and the [changes() SQL function]. |
| 2046 | 2072 | ** |
| 2047 | 2073 | ** If a separate thread makes changes on the same database connection |
| | @@ -2051,24 +2077,21 @@ |
| 2051 | 2077 | SQLITE_API int sqlite3_changes(sqlite3*); |
| 2052 | 2078 | |
| 2053 | 2079 | /* |
| 2054 | 2080 | ** CAPI3REF: Total Number Of Rows Modified |
| 2055 | 2081 | ** |
| 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 | | -** |
| 2082 | +** ^This function returns the total number of rows inserted, modified or |
| 2083 | +** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed |
| 2084 | +** since the database connection was opened, including those executed as |
| 2085 | +** part of trigger programs. ^Executing any other type of SQL statement |
| 2086 | +** does not affect the value returned by sqlite3_total_changes(). |
| 2087 | +** |
| 2088 | +** ^Changes made as part of [foreign key actions] are included in the |
| 2089 | +** count, but those made as part of REPLACE constraint resolution are |
| 2090 | +** not. ^Changes to a view that are intercepted by INSTEAD OF triggers |
| 2091 | +** are not counted. |
| 2092 | +** |
| 2070 | 2093 | ** See also the [sqlite3_changes()] interface, the |
| 2071 | 2094 | ** [count_changes pragma], and the [total_changes() SQL function]. |
| 2072 | 2095 | ** |
| 2073 | 2096 | ** If a separate thread makes changes on the same database connection |
| 2074 | 2097 | ** while [sqlite3_total_changes()] is running then the value |
| | @@ -2151,10 +2174,11 @@ |
| 2151 | 2174 | SQLITE_API int sqlite3_complete(const char *sql); |
| 2152 | 2175 | SQLITE_API int sqlite3_complete16(const void *sql); |
| 2153 | 2176 | |
| 2154 | 2177 | /* |
| 2155 | 2178 | ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors |
| 2179 | +** KEYWORDS: {busy-handler callback} {busy handler} |
| 2156 | 2180 | ** |
| 2157 | 2181 | ** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X |
| 2158 | 2182 | ** that might be invoked with argument P whenever |
| 2159 | 2183 | ** an attempt is made to access a database table associated with |
| 2160 | 2184 | ** [database connection] D when another thread |
| | @@ -2167,11 +2191,11 @@ |
| 2167 | 2191 | ** is not NULL, then the callback might be invoked with two arguments. |
| 2168 | 2192 | ** |
| 2169 | 2193 | ** ^The first argument to the busy handler is a copy of the void* pointer which |
| 2170 | 2194 | ** is the third argument to sqlite3_busy_handler(). ^The second argument to |
| 2171 | 2195 | ** the busy handler callback is the number of times that the busy handler has |
| 2172 | | -** been invoked for the same locking event. ^If the |
| 2196 | +** been invoked previously for the same locking event. ^If the |
| 2173 | 2197 | ** busy callback returns 0, then no additional attempts are made to |
| 2174 | 2198 | ** access the database and [SQLITE_BUSY] is returned |
| 2175 | 2199 | ** to the application. |
| 2176 | 2200 | ** ^If the callback returns non-zero, then another attempt |
| 2177 | 2201 | ** is made to access the database and the cycle repeats. |
| | @@ -2542,17 +2566,18 @@ |
| 2542 | 2566 | ** already uses the largest possible [ROWID]. The PRNG is also used for |
| 2543 | 2567 | ** the build-in random() and randomblob() SQL functions. This interface allows |
| 2544 | 2568 | ** applications to access the same PRNG for other purposes. |
| 2545 | 2569 | ** |
| 2546 | 2570 | ** ^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. |
| 2571 | +** ^The P parameter can be a NULL pointer. |
| 2548 | 2572 | ** |
| 2549 | 2573 | ** ^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 |
| 2574 | +** call had N less than one or a NULL pointer for P, then the PRNG is |
| 2575 | +** seeded using randomness obtained from the xRandomness method of |
| 2576 | +** the default [sqlite3_vfs] object. |
| 2577 | +** ^If the previous call to this routine had an N of 1 or more and a |
| 2578 | +** non-NULL P then the pseudo-randomness is generated |
| 2554 | 2579 | ** internally and without recourse to the [sqlite3_vfs] xRandomness |
| 2555 | 2580 | ** method. |
| 2556 | 2581 | */ |
| 2557 | 2582 | SQLITE_API void sqlite3_randomness(int N, void *P); |
| 2558 | 2583 | |
| | @@ -4270,13 +4295,13 @@ |
| 4270 | 4295 | ** CAPI3REF: Text Encodings |
| 4271 | 4296 | ** |
| 4272 | 4297 | ** These constant define integer codes that represent the various |
| 4273 | 4298 | ** text encodings supported by SQLite. |
| 4274 | 4299 | */ |
| 4275 | | -#define SQLITE_UTF8 1 |
| 4276 | | -#define SQLITE_UTF16LE 2 |
| 4277 | | -#define SQLITE_UTF16BE 3 |
| 4300 | +#define SQLITE_UTF8 1 /* IMP: R-37514-35566 */ |
| 4301 | +#define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */ |
| 4302 | +#define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */ |
| 4278 | 4303 | #define SQLITE_UTF16 4 /* Use native byte order */ |
| 4279 | 4304 | #define SQLITE_ANY 5 /* Deprecated */ |
| 4280 | 4305 | #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ |
| 4281 | 4306 | |
| 4282 | 4307 | /* |
| | @@ -4621,11 +4646,12 @@ |
| 4621 | 4646 | ** If these routines are called from within the different thread |
| 4622 | 4647 | ** than the one containing the application-defined function that received |
| 4623 | 4648 | ** the [sqlite3_context] pointer, the results are undefined. |
| 4624 | 4649 | */ |
| 4625 | 4650 | SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); |
| 4626 | | -SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,sqlite3_uint64,void(*)(void*)); |
| 4651 | +SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*, |
| 4652 | + sqlite3_uint64,void(*)(void*)); |
| 4627 | 4653 | SQLITE_API void sqlite3_result_double(sqlite3_context*, double); |
| 4628 | 4654 | SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int); |
| 4629 | 4655 | SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int); |
| 4630 | 4656 | SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*); |
| 4631 | 4657 | SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*); |
| | @@ -5762,31 +5788,47 @@ |
| 5762 | 5788 | ** in other words, the same BLOB that would be selected by: |
| 5763 | 5789 | ** |
| 5764 | 5790 | ** <pre> |
| 5765 | 5791 | ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow; |
| 5766 | 5792 | ** </pre>)^ |
| 5793 | +** |
| 5794 | +** ^(Parameter zDb is not the filename that contains the database, but |
| 5795 | +** rather the symbolic name of the database. For attached databases, this is |
| 5796 | +** the name that appears after the AS keyword in the [ATTACH] statement. |
| 5797 | +** For the main database file, the database name is "main". For TEMP |
| 5798 | +** tables, the database name is "temp".)^ |
| 5767 | 5799 | ** |
| 5768 | 5800 | ** ^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. |
| 5801 | +** and write access. ^If the flags parameter is zero, the BLOB is opened for |
| 5802 | +** read-only access. |
| 5803 | +** |
| 5804 | +** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored |
| 5805 | +** in *ppBlob. Otherwise an [error code] is returned and, unless the error |
| 5806 | +** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided |
| 5807 | +** the API is not misused, it is always safe to call [sqlite3_blob_close()] |
| 5808 | +** on *ppBlob after this function it returns. |
| 5809 | +** |
| 5810 | +** This function fails with SQLITE_ERROR if any of the following are true: |
| 5811 | +** <ul> |
| 5812 | +** <li> ^(Database zDb does not exist)^, |
| 5813 | +** <li> ^(Table zTable does not exist within database zDb)^, |
| 5814 | +** <li> ^(Table zTable is a WITHOUT ROWID table)^, |
| 5815 | +** <li> ^(Column zColumn does not exist)^, |
| 5816 | +** <li> ^(Row iRow is not present in the table)^, |
| 5817 | +** <li> ^(The specified column of row iRow contains a value that is not |
| 5818 | +** a TEXT or BLOB value)^, |
| 5819 | +** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE |
| 5820 | +** constraint and the blob is being opened for read/write access)^, |
| 5821 | +** <li> ^([foreign key constraints | Foreign key constraints] are enabled, |
| 5822 | +** column zColumn is part of a [child key] definition and the blob is |
| 5823 | +** being opened for read/write access)^. |
| 5824 | +** </ul> |
| 5825 | +** |
| 5826 | +** ^Unless it returns SQLITE_MISUSE, this function sets the |
| 5827 | +** [database connection] error code and message accessible via |
| 5828 | +** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. |
| 5829 | +** |
| 5788 | 5830 | ** |
| 5789 | 5831 | ** ^(If the row that a BLOB handle points to is modified by an |
| 5790 | 5832 | ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects |
| 5791 | 5833 | ** then the BLOB handle is marked as "expired". |
| 5792 | 5834 | ** This is true if any column of the row is changed, even a column |
| | @@ -5800,17 +5842,13 @@ |
| 5800 | 5842 | ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of |
| 5801 | 5843 | ** the opened blob. ^The size of a blob may not be changed by this |
| 5802 | 5844 | ** interface. Use the [UPDATE] SQL command to change the size of a |
| 5803 | 5845 | ** blob. |
| 5804 | 5846 | ** |
| 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 | 5847 | ** ^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. |
| 5848 | +** and the built-in [zeroblob] SQL function may be used to create a |
| 5849 | +** zero-filled blob to read or write using the incremental-blob interface. |
| 5812 | 5850 | ** |
| 5813 | 5851 | ** To avoid a resource leak, every open [BLOB handle] should eventually |
| 5814 | 5852 | ** be released by a call to [sqlite3_blob_close()]. |
| 5815 | 5853 | */ |
| 5816 | 5854 | SQLITE_API int sqlite3_blob_open( |
| | @@ -5848,28 +5886,26 @@ |
| 5848 | 5886 | SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); |
| 5849 | 5887 | |
| 5850 | 5888 | /* |
| 5851 | 5889 | ** CAPI3REF: Close A BLOB Handle |
| 5852 | 5890 | ** |
| 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. |
| 5891 | +** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed |
| 5892 | +** unconditionally. Even if this routine returns an error code, the |
| 5893 | +** handle is still closed.)^ |
| 5894 | +** |
| 5895 | +** ^If the blob handle being closed was opened for read-write access, and if |
| 5896 | +** the database is in auto-commit mode and there are no other open read-write |
| 5897 | +** blob handles or active write statements, the current transaction is |
| 5898 | +** committed. ^If an error occurs while committing the transaction, an error |
| 5899 | +** code is returned and the transaction rolled back. |
| 5900 | +** |
| 5901 | +** Calling this function with an argument that is not a NULL pointer or an |
| 5902 | +** open blob handle results in undefined behaviour. ^Calling this routine |
| 5903 | +** with a null pointer (such as would be returned by a failed call to |
| 5904 | +** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function |
| 5905 | +** is passed a valid open blob handle, the values returned by the |
| 5906 | +** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning. |
| 5871 | 5907 | */ |
| 5872 | 5908 | SQLITE_API int sqlite3_blob_close(sqlite3_blob *); |
| 5873 | 5909 | |
| 5874 | 5910 | /* |
| 5875 | 5911 | ** CAPI3REF: Return The Size Of An Open BLOB |
| | @@ -5915,36 +5951,39 @@ |
| 5915 | 5951 | SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); |
| 5916 | 5952 | |
| 5917 | 5953 | /* |
| 5918 | 5954 | ** CAPI3REF: Write Data Into A BLOB Incrementally |
| 5919 | 5955 | ** |
| 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. |
| 5956 | +** ^(This function is used to write data into an open [BLOB handle] from a |
| 5957 | +** caller-supplied buffer. N bytes of data are copied from the buffer Z |
| 5958 | +** into the open BLOB, starting at offset iOffset.)^ |
| 5959 | +** |
| 5960 | +** ^(On success, sqlite3_blob_write() returns SQLITE_OK. |
| 5961 | +** Otherwise, an [error code] or an [extended error code] is returned.)^ |
| 5962 | +** ^Unless SQLITE_MISUSE is returned, this function sets the |
| 5963 | +** [database connection] error code and message accessible via |
| 5964 | +** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. |
| 5923 | 5965 | ** |
| 5924 | 5966 | ** ^If the [BLOB handle] passed as the first argument was not opened for |
| 5925 | 5967 | ** writing (the flags parameter to [sqlite3_blob_open()] was zero), |
| 5926 | 5968 | ** this function returns [SQLITE_READONLY]. |
| 5927 | 5969 | ** |
| 5928 | | -** ^This function may only modify the contents of the BLOB; it is |
| 5970 | +** This function may only modify the contents of the BLOB; it is |
| 5929 | 5971 | ** not possible to increase the size of a BLOB using this API. |
| 5930 | 5972 | ** ^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. |
| 5973 | +** [SQLITE_ERROR] is returned and no data is written. The size of the |
| 5974 | +** BLOB (and hence the maximum value of N+iOffset) can be determined |
| 5975 | +** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less |
| 5976 | +** than zero [SQLITE_ERROR] is returned and no data is written. |
| 5935 | 5977 | ** |
| 5936 | 5978 | ** ^An attempt to write to an expired [BLOB handle] fails with an |
| 5937 | 5979 | ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred |
| 5938 | 5980 | ** before the [BLOB handle] expired are not rolled back by the |
| 5939 | 5981 | ** expiration of the handle, though of course those changes might |
| 5940 | 5982 | ** have been overwritten by the statement that expired the BLOB handle |
| 5941 | 5983 | ** or by other independent statements. |
| 5942 | 5984 | ** |
| 5943 | | -** ^(On success, sqlite3_blob_write() returns SQLITE_OK. |
| 5944 | | -** Otherwise, an [error code] or an [extended error code] is returned.)^ |
| 5945 | | -** |
| 5946 | 5985 | ** This routine only works on a [BLOB handle] which has been created |
| 5947 | 5986 | ** by a prior successful call to [sqlite3_blob_open()] and which has not |
| 5948 | 5987 | ** been closed by [sqlite3_blob_close()]. Passing any other pointer in |
| 5949 | 5988 | ** to this routine results in undefined and probably undesirable behavior. |
| 5950 | 5989 | ** |
| | @@ -5993,38 +6032,38 @@ |
| 5993 | 6032 | ** use by SQLite, code that links against SQLite is |
| 5994 | 6033 | ** permitted to use any of these routines. |
| 5995 | 6034 | ** |
| 5996 | 6035 | ** The SQLite source code contains multiple implementations |
| 5997 | 6036 | ** of these mutex routines. An appropriate implementation |
| 5998 | | -** is selected automatically at compile-time. ^(The following |
| 6037 | +** is selected automatically at compile-time. The following |
| 5999 | 6038 | ** implementations are available in the SQLite core: |
| 6000 | 6039 | ** |
| 6001 | 6040 | ** <ul> |
| 6002 | 6041 | ** <li> SQLITE_MUTEX_PTHREADS |
| 6003 | 6042 | ** <li> SQLITE_MUTEX_W32 |
| 6004 | 6043 | ** <li> SQLITE_MUTEX_NOOP |
| 6005 | | -** </ul>)^ |
| 6044 | +** </ul> |
| 6006 | 6045 | ** |
| 6007 | | -** ^The SQLITE_MUTEX_NOOP implementation is a set of routines |
| 6046 | +** The SQLITE_MUTEX_NOOP implementation is a set of routines |
| 6008 | 6047 | ** that does no real locking and is appropriate for use in |
| 6009 | | -** a single-threaded application. ^The SQLITE_MUTEX_PTHREADS and |
| 6048 | +** a single-threaded application. The SQLITE_MUTEX_PTHREADS and |
| 6010 | 6049 | ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix |
| 6011 | 6050 | ** and Windows. |
| 6012 | 6051 | ** |
| 6013 | | -** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor |
| 6052 | +** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor |
| 6014 | 6053 | ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex |
| 6015 | 6054 | ** implementation is included with the library. In this case the |
| 6016 | 6055 | ** application must supply a custom mutex implementation using the |
| 6017 | 6056 | ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function |
| 6018 | 6057 | ** before calling sqlite3_initialize() or any other public sqlite3_ |
| 6019 | | -** function that calls sqlite3_initialize().)^ |
| 6058 | +** function that calls sqlite3_initialize(). |
| 6020 | 6059 | ** |
| 6021 | 6060 | ** ^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: |
| 6061 | +** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc() |
| 6062 | +** routine returns NULL if it is unable to allocate the requested |
| 6063 | +** mutex. The argument to sqlite3_mutex_alloc() must one of these |
| 6064 | +** integer constants: |
| 6026 | 6065 | ** |
| 6027 | 6066 | ** <ul> |
| 6028 | 6067 | ** <li> SQLITE_MUTEX_FAST |
| 6029 | 6068 | ** <li> SQLITE_MUTEX_RECURSIVE |
| 6030 | 6069 | ** <li> SQLITE_MUTEX_STATIC_MASTER |
| | @@ -6033,68 +6072,64 @@ |
| 6033 | 6072 | ** <li> SQLITE_MUTEX_STATIC_PRNG |
| 6034 | 6073 | ** <li> SQLITE_MUTEX_STATIC_LRU |
| 6035 | 6074 | ** <li> SQLITE_MUTEX_STATIC_PMEM |
| 6036 | 6075 | ** <li> SQLITE_MUTEX_STATIC_APP1 |
| 6037 | 6076 | ** <li> SQLITE_MUTEX_STATIC_APP2 |
| 6038 | | -** </ul>)^ |
| 6077 | +** <li> SQLITE_MUTEX_STATIC_APP3 |
| 6078 | +** </ul> |
| 6039 | 6079 | ** |
| 6040 | 6080 | ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) |
| 6041 | 6081 | ** cause sqlite3_mutex_alloc() to create |
| 6042 | 6082 | ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE |
| 6043 | 6083 | ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. |
| 6044 | 6084 | ** The mutex implementation does not need to make a distinction |
| 6045 | 6085 | ** 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 |
| 6086 | +** not want to. SQLite will only request a recursive mutex in |
| 6087 | +** cases where it really needs one. If a faster non-recursive mutex |
| 6048 | 6088 | ** implementation is available on the host platform, the mutex subsystem |
| 6049 | 6089 | ** might return such a mutex in response to SQLITE_MUTEX_FAST. |
| 6050 | 6090 | ** |
| 6051 | 6091 | ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other |
| 6052 | 6092 | ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return |
| 6053 | | -** a pointer to a static preexisting mutex. ^Six static mutexes are |
| 6093 | +** a pointer to a static preexisting mutex. ^Nine static mutexes are |
| 6054 | 6094 | ** used by the current version of SQLite. Future versions of SQLite |
| 6055 | 6095 | ** may add additional static mutexes. Static mutexes are for internal |
| 6056 | 6096 | ** use by SQLite only. Applications that use SQLite mutexes should |
| 6057 | 6097 | ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or |
| 6058 | 6098 | ** SQLITE_MUTEX_RECURSIVE. |
| 6059 | 6099 | ** |
| 6060 | 6100 | ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST |
| 6061 | 6101 | ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() |
| 6062 | | -** returns a different mutex on every call. ^But for the static |
| 6102 | +** returns a different mutex on every call. ^For the static |
| 6063 | 6103 | ** mutex types, the same mutex is returned on every call that has |
| 6064 | 6104 | ** the same type number. |
| 6065 | 6105 | ** |
| 6066 | 6106 | ** ^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. |
| 6107 | +** allocated dynamic mutex. Attempting to deallocate a static |
| 6108 | +** mutex results in undefined behavior. |
| 6072 | 6109 | ** |
| 6073 | 6110 | ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt |
| 6074 | 6111 | ** to enter a mutex. ^If another thread is already within the mutex, |
| 6075 | 6112 | ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return |
| 6076 | 6113 | ** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK] |
| 6077 | 6114 | ** upon successful entry. ^(Mutexes created using |
| 6078 | 6115 | ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. |
| 6079 | | -** In such cases the, |
| 6116 | +** In such cases, the |
| 6080 | 6117 | ** 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.)^ |
| 6118 | +** can enter.)^ If the same thread tries to enter any mutex other |
| 6119 | +** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined. |
| 6085 | 6120 | ** |
| 6086 | 6121 | ** ^(Some systems (for example, Windows 95) do not support the operation |
| 6087 | 6122 | ** 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.)^ |
| 6123 | +** will always return SQLITE_BUSY. The SQLite core only ever uses |
| 6124 | +** sqlite3_mutex_try() as an optimization so this is acceptable |
| 6125 | +** behavior.)^ |
| 6090 | 6126 | ** |
| 6091 | 6127 | ** ^The sqlite3_mutex_leave() routine exits a mutex that was |
| 6092 | | -** previously entered by the same thread. ^(The behavior |
| 6128 | +** previously entered by the same thread. The behavior |
| 6093 | 6129 | ** 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.)^ |
| 6130 | +** calling thread or is not currently allocated. |
| 6096 | 6131 | ** |
| 6097 | 6132 | ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or |
| 6098 | 6133 | ** sqlite3_mutex_leave() is a NULL pointer, then all three routines |
| 6099 | 6134 | ** behave as no-ops. |
| 6100 | 6135 | ** |
| | @@ -6111,13 +6146,13 @@ |
| 6111 | 6146 | ** |
| 6112 | 6147 | ** An instance of this structure defines the low-level routines |
| 6113 | 6148 | ** used to allocate and use mutexes. |
| 6114 | 6149 | ** |
| 6115 | 6150 | ** Usually, the default mutex implementations provided by SQLite are |
| 6116 | | -** sufficient, however the user has the option of substituting a custom |
| 6151 | +** sufficient, however the application has the option of substituting a custom |
| 6117 | 6152 | ** implementation for specialized deployments or systems for which SQLite |
| 6118 | | -** does not provide a suitable implementation. In this case, the user |
| 6153 | +** does not provide a suitable implementation. In this case, the application |
| 6119 | 6154 | ** creates and populates an instance of this structure to pass |
| 6120 | 6155 | ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option. |
| 6121 | 6156 | ** Additionally, an instance of this structure can be used as an |
| 6122 | 6157 | ** output variable when querying the system for the current mutex |
| 6123 | 6158 | ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. |
| | @@ -6154,17 +6189,17 @@ |
| 6154 | 6189 | ** by this structure are not required to handle this case, the results |
| 6155 | 6190 | ** of passing a NULL pointer instead of a valid mutex handle are undefined |
| 6156 | 6191 | ** (i.e. it is acceptable to provide an implementation that segfaults if |
| 6157 | 6192 | ** it is passed a NULL pointer). |
| 6158 | 6193 | ** |
| 6159 | | -** The xMutexInit() method must be threadsafe. ^It must be harmless to |
| 6194 | +** The xMutexInit() method must be threadsafe. It must be harmless to |
| 6160 | 6195 | ** invoke xMutexInit() multiple times within the same process and without |
| 6161 | 6196 | ** intervening calls to xMutexEnd(). Second and subsequent calls to |
| 6162 | 6197 | ** xMutexInit() must be no-ops. |
| 6163 | 6198 | ** |
| 6164 | | -** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] |
| 6165 | | -** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory |
| 6199 | +** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] |
| 6200 | +** and its associates). Similarly, xMutexAlloc() must not use SQLite memory |
| 6166 | 6201 | ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite |
| 6167 | 6202 | ** memory allocation for a fast or recursive mutex. |
| 6168 | 6203 | ** |
| 6169 | 6204 | ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is |
| 6170 | 6205 | ** called, but only if the prior call to xMutexInit returned SQLITE_OK. |
| | @@ -6186,33 +6221,33 @@ |
| 6186 | 6221 | |
| 6187 | 6222 | /* |
| 6188 | 6223 | ** CAPI3REF: Mutex Verification Routines |
| 6189 | 6224 | ** |
| 6190 | 6225 | ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines |
| 6191 | | -** are intended for use inside assert() statements. ^The SQLite core |
| 6226 | +** are intended for use inside assert() statements. The SQLite core |
| 6192 | 6227 | ** never uses these routines except inside an assert() and applications |
| 6193 | | -** are advised to follow the lead of the core. ^The SQLite core only |
| 6228 | +** are advised to follow the lead of the core. The SQLite core only |
| 6194 | 6229 | ** provides implementations for these routines when it is compiled |
| 6195 | | -** with the SQLITE_DEBUG flag. ^External mutex implementations |
| 6230 | +** with the SQLITE_DEBUG flag. External mutex implementations |
| 6196 | 6231 | ** are only required to provide these routines if SQLITE_DEBUG is |
| 6197 | 6232 | ** defined and if NDEBUG is not defined. |
| 6198 | 6233 | ** |
| 6199 | | -** ^These routines should return true if the mutex in their argument |
| 6234 | +** These routines should return true if the mutex in their argument |
| 6200 | 6235 | ** is held or not held, respectively, by the calling thread. |
| 6201 | 6236 | ** |
| 6202 | | -** ^The implementation is not required to provide versions of these |
| 6237 | +** The implementation is not required to provide versions of these |
| 6203 | 6238 | ** routines that actually work. If the implementation does not provide working |
| 6204 | 6239 | ** versions of these routines, it should at least provide stubs that always |
| 6205 | 6240 | ** return true so that one does not get spurious assertion failures. |
| 6206 | 6241 | ** |
| 6207 | | -** ^If the argument to sqlite3_mutex_held() is a NULL pointer then |
| 6242 | +** If the argument to sqlite3_mutex_held() is a NULL pointer then |
| 6208 | 6243 | ** the routine should return 1. This seems counter-intuitive since |
| 6209 | 6244 | ** clearly the mutex cannot be held if it does not exist. But |
| 6210 | 6245 | ** the reason the mutex does not exist is because the build is not |
| 6211 | 6246 | ** using mutexes. And we do not want the assert() containing the |
| 6212 | 6247 | ** call to sqlite3_mutex_held() to fail, so a non-zero return is |
| 6213 | | -** the appropriate thing to do. ^The sqlite3_mutex_notheld() |
| 6248 | +** the appropriate thing to do. The sqlite3_mutex_notheld() |
| 6214 | 6249 | ** interface should also return 1 when given a NULL pointer. |
| 6215 | 6250 | */ |
| 6216 | 6251 | #ifndef NDEBUG |
| 6217 | 6252 | SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*); |
| 6218 | 6253 | SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); |
| | @@ -6940,10 +6975,14 @@ |
| 6940 | 6975 | ** sqlite3_backup_init(D,N,S,M) identify the [database connection] |
| 6941 | 6976 | ** and database name of the source database, respectively. |
| 6942 | 6977 | ** ^The source and destination [database connections] (parameters S and D) |
| 6943 | 6978 | ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with |
| 6944 | 6979 | ** an error. |
| 6980 | +** |
| 6981 | +** ^A call to sqlite3_backup_init() will fail, returning SQLITE_ERROR, if |
| 6982 | +** there is already a read or read-write transaction open on the |
| 6983 | +** destination database. |
| 6945 | 6984 | ** |
| 6946 | 6985 | ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is |
| 6947 | 6986 | ** returned and an error code and error message are stored in the |
| 6948 | 6987 | ** destination [database connection] D. |
| 6949 | 6988 | ** ^The error code and message for the failed call to sqlite3_backup_init() |
| | @@ -7334,101 +7373,118 @@ |
| 7334 | 7373 | SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N); |
| 7335 | 7374 | |
| 7336 | 7375 | /* |
| 7337 | 7376 | ** CAPI3REF: Checkpoint a database |
| 7338 | 7377 | ** |
| 7339 | | -** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X |
| 7340 | | -** on [database connection] D to be [checkpointed]. ^If X is NULL or an |
| 7341 | | -** empty string, then a checkpoint is run on all databases of |
| 7342 | | -** connection D. ^If the database connection D is not in |
| 7343 | | -** [WAL | write-ahead log mode] then this interface is a harmless no-op. |
| 7344 | | -** ^The [sqlite3_wal_checkpoint(D,X)] interface initiates a |
| 7345 | | -** [sqlite3_wal_checkpoint_v2|PASSIVE] checkpoint. |
| 7346 | | -** Use the [sqlite3_wal_checkpoint_v2()] interface to get a FULL |
| 7347 | | -** or RESET checkpoint. |
| 7348 | | -** |
| 7349 | | -** ^The [wal_checkpoint pragma] can be used to invoke this interface |
| 7350 | | -** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the |
| 7351 | | -** [wal_autocheckpoint pragma] can be used to cause this interface to be |
| 7352 | | -** run whenever the WAL reaches a certain size threshold. |
| 7353 | | -** |
| 7354 | | -** See also: [sqlite3_wal_checkpoint_v2()] |
| 7378 | +** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to |
| 7379 | +** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^ |
| 7380 | +** |
| 7381 | +** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the |
| 7382 | +** [write-ahead log] for database X on [database connection] D to be |
| 7383 | +** transferred into the database file and for the write-ahead log to |
| 7384 | +** be reset. See the [checkpointing] documentation for addition |
| 7385 | +** information. |
| 7386 | +** |
| 7387 | +** This interface used to be the only way to cause a checkpoint to |
| 7388 | +** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()] |
| 7389 | +** interface was added. This interface is retained for backwards |
| 7390 | +** compatibility and as a convenience for applications that need to manually |
| 7391 | +** start a callback but which do not need the full power (and corresponding |
| 7392 | +** complication) of [sqlite3_wal_checkpoint_v2()]. |
| 7355 | 7393 | */ |
| 7356 | 7394 | SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); |
| 7357 | 7395 | |
| 7358 | 7396 | /* |
| 7359 | 7397 | ** CAPI3REF: Checkpoint a database |
| 7360 | 7398 | ** |
| 7361 | | -** Run a checkpoint operation on WAL database zDb attached to database |
| 7362 | | -** handle db. The specific operation is determined by the value of the |
| 7363 | | -** eMode parameter: |
| 7399 | +** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint |
| 7400 | +** operation on database X of [database connection] D in mode M. Status |
| 7401 | +** information is written back into integers pointed to by L and C.)^ |
| 7402 | +** ^(The M parameter must be a valid [checkpoint mode]:)^ |
| 7364 | 7403 | ** |
| 7365 | 7404 | ** <dl> |
| 7366 | 7405 | ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd> |
| 7367 | | -** Checkpoint as many frames as possible without waiting for any database |
| 7368 | | -** readers or writers to finish. Sync the db file if all frames in the log |
| 7369 | | -** are checkpointed. This mode is the same as calling |
| 7370 | | -** sqlite3_wal_checkpoint(). The [sqlite3_busy_handler|busy-handler callback] |
| 7371 | | -** is never invoked. |
| 7406 | +** ^Checkpoint as many frames as possible without waiting for any database |
| 7407 | +** readers or writers to finish, then sync the database file if all frames |
| 7408 | +** in the log were checkpointed. ^The [busy-handler callback] |
| 7409 | +** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode. |
| 7410 | +** ^On the other hand, passive mode might leave the checkpoint unfinished |
| 7411 | +** if there are concurrent readers or writers. |
| 7372 | 7412 | ** |
| 7373 | 7413 | ** <dt>SQLITE_CHECKPOINT_FULL<dd> |
| 7374 | | -** This mode blocks (it invokes the |
| 7414 | +** ^This mode blocks (it invokes the |
| 7375 | 7415 | ** [sqlite3_busy_handler|busy-handler callback]) until there is no |
| 7376 | 7416 | ** database writer and all readers are reading from the most recent database |
| 7377 | | -** snapshot. It then checkpoints all frames in the log file and syncs the |
| 7378 | | -** database file. This call blocks database writers while it is running, |
| 7379 | | -** but not database readers. |
| 7417 | +** snapshot. ^It then checkpoints all frames in the log file and syncs the |
| 7418 | +** database file. ^This mode blocks new database writers while it is pending, |
| 7419 | +** but new database readers are allowed to continue unimpeded. |
| 7380 | 7420 | ** |
| 7381 | 7421 | ** <dt>SQLITE_CHECKPOINT_RESTART<dd> |
| 7382 | | -** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after |
| 7383 | | -** checkpointing the log file it blocks (calls the |
| 7384 | | -** [sqlite3_busy_handler|busy-handler callback]) |
| 7385 | | -** until all readers are reading from the database file only. This ensures |
| 7386 | | -** that the next client to write to the database file restarts the log file |
| 7387 | | -** from the beginning. This call blocks database writers while it is running, |
| 7388 | | -** but not database readers. |
| 7422 | +** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition |
| 7423 | +** that after checkpointing the log file it blocks (calls the |
| 7424 | +** [busy-handler callback]) |
| 7425 | +** until all readers are reading from the database file only. ^This ensures |
| 7426 | +** that the next writer will restart the log file from the beginning. |
| 7427 | +** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new |
| 7428 | +** database writer attempts while it is pending, but does not impede readers. |
| 7429 | +** |
| 7430 | +** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd> |
| 7431 | +** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the |
| 7432 | +** addition that it also truncates the log file to zero bytes just prior |
| 7433 | +** to a successful return. |
| 7389 | 7434 | ** </dl> |
| 7390 | 7435 | ** |
| 7391 | | -** If pnLog is not NULL, then *pnLog is set to the total number of frames in |
| 7392 | | -** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to |
| 7393 | | -** the total number of checkpointed frames (including any that were already |
| 7394 | | -** checkpointed when this function is called). *pnLog and *pnCkpt may be |
| 7395 | | -** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK. |
| 7396 | | -** If no values are available because of an error, they are both set to -1 |
| 7397 | | -** before returning to communicate this to the caller. |
| 7436 | +** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in |
| 7437 | +** the log file or to -1 if the checkpoint could not run because |
| 7438 | +** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not |
| 7439 | +** NULL,then *pnCkpt is set to the total number of checkpointed frames in the |
| 7440 | +** log file (including any that were already checkpointed before the function |
| 7441 | +** was called) or to -1 if the checkpoint could not run due to an error or |
| 7442 | +** because the database is not in WAL mode. ^Note that upon successful |
| 7443 | +** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been |
| 7444 | +** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero. |
| 7398 | 7445 | ** |
| 7399 | | -** All calls obtain an exclusive "checkpoint" lock on the database file. If |
| 7446 | +** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If |
| 7400 | 7447 | ** any other process is running a checkpoint operation at the same time, the |
| 7401 | | -** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a |
| 7448 | +** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a |
| 7402 | 7449 | ** busy-handler configured, it will not be invoked in this case. |
| 7403 | 7450 | ** |
| 7404 | | -** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive |
| 7405 | | -** "writer" lock on the database file. If the writer lock cannot be obtained |
| 7406 | | -** immediately, and a busy-handler is configured, it is invoked and the writer |
| 7407 | | -** lock retried until either the busy-handler returns 0 or the lock is |
| 7408 | | -** successfully obtained. The busy-handler is also invoked while waiting for |
| 7409 | | -** database readers as described above. If the busy-handler returns 0 before |
| 7451 | +** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the |
| 7452 | +** exclusive "writer" lock on the database file. ^If the writer lock cannot be |
| 7453 | +** obtained immediately, and a busy-handler is configured, it is invoked and |
| 7454 | +** the writer lock retried until either the busy-handler returns 0 or the lock |
| 7455 | +** is successfully obtained. ^The busy-handler is also invoked while waiting for |
| 7456 | +** database readers as described above. ^If the busy-handler returns 0 before |
| 7410 | 7457 | ** the writer lock is obtained or while waiting for database readers, the |
| 7411 | 7458 | ** checkpoint operation proceeds from that point in the same way as |
| 7412 | 7459 | ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible |
| 7413 | | -** without blocking any further. SQLITE_BUSY is returned in this case. |
| 7460 | +** without blocking any further. ^SQLITE_BUSY is returned in this case. |
| 7414 | 7461 | ** |
| 7415 | | -** If parameter zDb is NULL or points to a zero length string, then the |
| 7416 | | -** specified operation is attempted on all WAL databases. In this case the |
| 7417 | | -** values written to output parameters *pnLog and *pnCkpt are undefined. If |
| 7462 | +** ^If parameter zDb is NULL or points to a zero length string, then the |
| 7463 | +** specified operation is attempted on all WAL databases [attached] to |
| 7464 | +** [database connection] db. In this case the |
| 7465 | +** values written to output parameters *pnLog and *pnCkpt are undefined. ^If |
| 7418 | 7466 | ** an SQLITE_BUSY error is encountered when processing one or more of the |
| 7419 | 7467 | ** attached WAL databases, the operation is still attempted on any remaining |
| 7420 | | -** attached databases and SQLITE_BUSY is returned to the caller. If any other |
| 7468 | +** attached databases and SQLITE_BUSY is returned at the end. ^If any other |
| 7421 | 7469 | ** error occurs while processing an attached database, processing is abandoned |
| 7422 | | -** and the error code returned to the caller immediately. If no error |
| 7470 | +** and the error code is returned to the caller immediately. ^If no error |
| 7423 | 7471 | ** (SQLITE_BUSY or otherwise) is encountered while processing the attached |
| 7424 | 7472 | ** databases, SQLITE_OK is returned. |
| 7425 | 7473 | ** |
| 7426 | | -** If database zDb is the name of an attached database that is not in WAL |
| 7427 | | -** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If |
| 7474 | +** ^If database zDb is the name of an attached database that is not in WAL |
| 7475 | +** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If |
| 7428 | 7476 | ** zDb is not NULL (or a zero length string) and is not the name of any |
| 7429 | 7477 | ** attached database, SQLITE_ERROR is returned to the caller. |
| 7478 | +** |
| 7479 | +** ^Unless it returns SQLITE_MISUSE, |
| 7480 | +** the sqlite3_wal_checkpoint_v2() interface |
| 7481 | +** sets the error information that is queried by |
| 7482 | +** [sqlite3_errcode()] and [sqlite3_errmsg()]. |
| 7483 | +** |
| 7484 | +** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface |
| 7485 | +** from SQL. |
| 7430 | 7486 | */ |
| 7431 | 7487 | SQLITE_API int sqlite3_wal_checkpoint_v2( |
| 7432 | 7488 | sqlite3 *db, /* Database handle */ |
| 7433 | 7489 | const char *zDb, /* Name of attached database (or NULL) */ |
| 7434 | 7490 | int eMode, /* SQLITE_CHECKPOINT_* value */ |
| | @@ -7435,20 +7491,22 @@ |
| 7435 | 7491 | int *pnLog, /* OUT: Size of WAL log in frames */ |
| 7436 | 7492 | int *pnCkpt /* OUT: Total number of frames checkpointed */ |
| 7437 | 7493 | ); |
| 7438 | 7494 | |
| 7439 | 7495 | /* |
| 7440 | | -** CAPI3REF: Checkpoint operation parameters |
| 7496 | +** CAPI3REF: Checkpoint Mode Values |
| 7497 | +** KEYWORDS: {checkpoint mode} |
| 7441 | 7498 | ** |
| 7442 | | -** These constants can be used as the 3rd parameter to |
| 7443 | | -** [sqlite3_wal_checkpoint_v2()]. See the [sqlite3_wal_checkpoint_v2()] |
| 7444 | | -** documentation for additional information about the meaning and use of |
| 7445 | | -** each of these values. |
| 7499 | +** These constants define all valid values for the "checkpoint mode" passed |
| 7500 | +** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface. |
| 7501 | +** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the |
| 7502 | +** meaning of each of these checkpoint modes. |
| 7446 | 7503 | */ |
| 7447 | | -#define SQLITE_CHECKPOINT_PASSIVE 0 |
| 7448 | | -#define SQLITE_CHECKPOINT_FULL 1 |
| 7449 | | -#define SQLITE_CHECKPOINT_RESTART 2 |
| 7504 | +#define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */ |
| 7505 | +#define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */ |
| 7506 | +#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */ |
| 7507 | +#define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */ |
| 7450 | 7508 | |
| 7451 | 7509 | /* |
| 7452 | 7510 | ** CAPI3REF: Virtual Table Interface Configuration |
| 7453 | 7511 | ** |
| 7454 | 7512 | ** This function may be called by either the [xConnect] or [xCreate] method |
| | @@ -7533,10 +7591,102 @@ |
| 7533 | 7591 | /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */ |
| 7534 | 7592 | #define SQLITE_FAIL 3 |
| 7535 | 7593 | /* #define SQLITE_ABORT 4 // Also an error code */ |
| 7536 | 7594 | #define SQLITE_REPLACE 5 |
| 7537 | 7595 | |
| 7596 | +/* |
| 7597 | +** CAPI3REF: Prepared Statement Scan Status Opcodes |
| 7598 | +** KEYWORDS: {scanstatus options} |
| 7599 | +** |
| 7600 | +** The following constants can be used for the T parameter to the |
| 7601 | +** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a |
| 7602 | +** different metric for sqlite3_stmt_scanstatus() to return. |
| 7603 | +** |
| 7604 | +** <dl> |
| 7605 | +** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt> |
| 7606 | +** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be |
| 7607 | +** set to the total number of times that the X-th loop has run.</dd> |
| 7608 | +** |
| 7609 | +** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt> |
| 7610 | +** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set |
| 7611 | +** to the total number of rows examined by all iterations of the X-th loop.</dd> |
| 7612 | +** |
| 7613 | +** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt> |
| 7614 | +** <dd>^The "double" variable pointed to by the T parameter will be set to the |
| 7615 | +** query planner's estimate for the average number of rows output from each |
| 7616 | +** iteration of the X-th loop. If the query planner's estimates was accurate, |
| 7617 | +** then this value will approximate the quotient NVISIT/NLOOP and the |
| 7618 | +** product of this value for all prior loops with the same SELECTID will |
| 7619 | +** be the NLOOP value for the current loop. |
| 7620 | +** |
| 7621 | +** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt> |
| 7622 | +** <dd>^The "const char *" variable pointed to by the T parameter will be set |
| 7623 | +** to a zero-terminated UTF-8 string containing the name of the index or table |
| 7624 | +** used for the X-th loop. |
| 7625 | +** |
| 7626 | +** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt> |
| 7627 | +** <dd>^The "const char *" variable pointed to by the T parameter will be set |
| 7628 | +** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] |
| 7629 | +** description for the X-th loop. |
| 7630 | +** |
| 7631 | +** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt> |
| 7632 | +** <dd>^The "int" variable pointed to by the T parameter will be set to the |
| 7633 | +** "select-id" for the X-th loop. The select-id identifies which query or |
| 7634 | +** subquery the loop is part of. The main query has a select-id of zero. |
| 7635 | +** The select-id is the same value as is output in the first column |
| 7636 | +** of an [EXPLAIN QUERY PLAN] query. |
| 7637 | +** </dl> |
| 7638 | +*/ |
| 7639 | +#define SQLITE_SCANSTAT_NLOOP 0 |
| 7640 | +#define SQLITE_SCANSTAT_NVISIT 1 |
| 7641 | +#define SQLITE_SCANSTAT_EST 2 |
| 7642 | +#define SQLITE_SCANSTAT_NAME 3 |
| 7643 | +#define SQLITE_SCANSTAT_EXPLAIN 4 |
| 7644 | +#define SQLITE_SCANSTAT_SELECTID 5 |
| 7645 | + |
| 7646 | +/* |
| 7647 | +** CAPI3REF: Prepared Statement Scan Status |
| 7648 | +** |
| 7649 | +** Return status data for a single loop within query pStmt. |
| 7650 | +** |
| 7651 | +** The "iScanStatusOp" parameter determines which status information to return. |
| 7652 | +** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior |
| 7653 | +** of this interface is undefined. |
| 7654 | +** ^The requested measurement is written into a variable pointed to by |
| 7655 | +** the "pOut" parameter. |
| 7656 | +** Parameter "idx" identifies the specific loop to retrieve statistics for. |
| 7657 | +** Loops are numbered starting from zero. ^If idx is out of range - less than |
| 7658 | +** zero or greater than or equal to the total number of loops used to implement |
| 7659 | +** the statement - a non-zero value is returned and the variable that pOut |
| 7660 | +** points to is unchanged. |
| 7661 | +** |
| 7662 | +** ^Statistics might not be available for all loops in all statements. ^In cases |
| 7663 | +** where there exist loops with no available statistics, this function behaves |
| 7664 | +** as if the loop did not exist - it returns non-zero and leave the variable |
| 7665 | +** that pOut points to unchanged. |
| 7666 | +** |
| 7667 | +** This API is only available if the library is built with pre-processor |
| 7668 | +** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. |
| 7669 | +** |
| 7670 | +** See also: [sqlite3_stmt_scanstatus_reset()] |
| 7671 | +*/ |
| 7672 | +SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus( |
| 7673 | + sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ |
| 7674 | + int idx, /* Index of loop to report on */ |
| 7675 | + int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ |
| 7676 | + void *pOut /* Result written here */ |
| 7677 | +); |
| 7678 | + |
| 7679 | +/* |
| 7680 | +** CAPI3REF: Zero Scan-Status Counters |
| 7681 | +** |
| 7682 | +** ^Zero all [sqlite3_stmt_scanstatus()] related event counters. |
| 7683 | +** |
| 7684 | +** This API is only available if the library is built with pre-processor |
| 7685 | +** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. |
| 7686 | +*/ |
| 7687 | +SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); |
| 7538 | 7688 | |
| 7539 | 7689 | |
| 7540 | 7690 | /* |
| 7541 | 7691 | ** Undo the hack that converts floating point types to integer for |
| 7542 | 7692 | ** builds on processors without floating point support. |
| | @@ -7978,14 +8128,13 @@ |
| 7978 | 8128 | #ifndef SQLITE_POWERSAFE_OVERWRITE |
| 7979 | 8129 | # define SQLITE_POWERSAFE_OVERWRITE 1 |
| 7980 | 8130 | #endif |
| 7981 | 8131 | |
| 7982 | 8132 | /* |
| 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. |
| 8133 | +** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by |
| 8134 | +** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in |
| 8135 | +** which case memory allocation statistics are disabled by default. |
| 7987 | 8136 | */ |
| 7988 | 8137 | #if !defined(SQLITE_DEFAULT_MEMSTATUS) |
| 7989 | 8138 | # define SQLITE_DEFAULT_MEMSTATUS 1 |
| 7990 | 8139 | #endif |
| 7991 | 8140 | |
| | @@ -8611,11 +8760,11 @@ |
| 8611 | 8760 | ** Estimated quantities used for query planning are stored as 16-bit |
| 8612 | 8761 | ** logarithms. For quantity X, the value stored is 10*log2(X). This |
| 8613 | 8762 | ** gives a possible range of values of approximately 1.0e986 to 1e-986. |
| 8614 | 8763 | ** But the allowed values are "grainy". Not every value is representable. |
| 8615 | 8764 | ** For example, quantities 16 and 17 are both represented by a LogEst |
| 8616 | | -** of 40. However, since LogEst quantaties are suppose to be estimates, |
| 8765 | +** of 40. However, since LogEst quantities are suppose to be estimates, |
| 8617 | 8766 | ** not exact values, this imprecision is not a problem. |
| 8618 | 8767 | ** |
| 8619 | 8768 | ** "LogEst" is short for "Logarithmic Estimate". |
| 8620 | 8769 | ** |
| 8621 | 8770 | ** Examples: |
| | @@ -9011,11 +9160,11 @@ |
| 9011 | 9160 | SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *); |
| 9012 | 9161 | SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int); |
| 9013 | 9162 | SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster); |
| 9014 | 9163 | SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int); |
| 9015 | 9164 | SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*); |
| 9016 | | -SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int); |
| 9165 | +SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int); |
| 9017 | 9166 | SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int); |
| 9018 | 9167 | SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags); |
| 9019 | 9168 | SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*); |
| 9020 | 9169 | SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*); |
| 9021 | 9170 | SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*); |
| | @@ -9044,11 +9193,11 @@ |
| 9044 | 9193 | #define BTREE_BLOBKEY 2 /* Table has keys only - no data */ |
| 9045 | 9194 | |
| 9046 | 9195 | SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*); |
| 9047 | 9196 | SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*); |
| 9048 | 9197 | SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*); |
| 9049 | | -SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int); |
| 9198 | +SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree*, int, int); |
| 9050 | 9199 | |
| 9051 | 9200 | SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue); |
| 9052 | 9201 | SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value); |
| 9053 | 9202 | |
| 9054 | 9203 | SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p); |
| | @@ -9124,10 +9273,11 @@ |
| 9124 | 9273 | SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *); |
| 9125 | 9274 | SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *); |
| 9126 | 9275 | SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); |
| 9127 | 9276 | SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask); |
| 9128 | 9277 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt); |
| 9278 | +SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void); |
| 9129 | 9279 | |
| 9130 | 9280 | #ifndef NDEBUG |
| 9131 | 9281 | SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*); |
| 9132 | 9282 | #endif |
| 9133 | 9283 | |
| | @@ -9666,10 +9816,16 @@ |
| 9666 | 9816 | # define VdbeCoverageAlwaysTaken(v) |
| 9667 | 9817 | # define VdbeCoverageNeverTaken(v) |
| 9668 | 9818 | # define VDBE_OFFSET_LINENO(x) 0 |
| 9669 | 9819 | #endif |
| 9670 | 9820 | |
| 9821 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 9822 | +SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*); |
| 9823 | +#else |
| 9824 | +# define sqlite3VdbeScanStatus(a,b,c,d,e) |
| 9825 | +#endif |
| 9826 | + |
| 9671 | 9827 | #endif |
| 9672 | 9828 | |
| 9673 | 9829 | /************** End of vdbe.h ************************************************/ |
| 9674 | 9830 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 9675 | 9831 | /************** Include pager.h in the middle of sqliteInt.h *****************/ |
| | @@ -9862,10 +10018,12 @@ |
| 9862 | 10018 | SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *); |
| 9863 | 10019 | |
| 9864 | 10020 | /* Functions used to truncate the database file. */ |
| 9865 | 10021 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); |
| 9866 | 10022 | |
| 10023 | +SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16); |
| 10024 | + |
| 9867 | 10025 | #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL) |
| 9868 | 10026 | SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *); |
| 9869 | 10027 | #endif |
| 9870 | 10028 | |
| 9871 | 10029 | /* Functions to support testing and debugging. */ |
| | @@ -10049,10 +10207,14 @@ |
| 10049 | 10207 | SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*); |
| 10050 | 10208 | #endif |
| 10051 | 10209 | |
| 10052 | 10210 | SQLITE_PRIVATE void sqlite3PCacheSetDefault(void); |
| 10053 | 10211 | |
| 10212 | +/* Return the header size */ |
| 10213 | +SQLITE_PRIVATE int sqlite3HeaderSizePcache(void); |
| 10214 | +SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void); |
| 10215 | + |
| 10054 | 10216 | #endif /* _PCACHE_H_ */ |
| 10055 | 10217 | |
| 10056 | 10218 | /************** End of pcache.h **********************************************/ |
| 10057 | 10219 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 10058 | 10220 | |
| | @@ -10735,11 +10897,11 @@ |
| 10735 | 10897 | #define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */ |
| 10736 | 10898 | #define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */ |
| 10737 | 10899 | #define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */ |
| 10738 | 10900 | #define SQLITE_Transitive 0x0200 /* Transitive constraints */ |
| 10739 | 10901 | #define SQLITE_OmitNoopJoin 0x0400 /* Omit unused tables in joins */ |
| 10740 | | -#define SQLITE_Stat3 0x0800 /* Use the SQLITE_STAT3 table */ |
| 10902 | +#define SQLITE_Stat34 0x0800 /* Use STAT3 or STAT4 data */ |
| 10741 | 10903 | #define SQLITE_AllOpts 0xffff /* All optimizations */ |
| 10742 | 10904 | |
| 10743 | 10905 | /* |
| 10744 | 10906 | ** Macros for testing whether or not optimizations are enabled or disabled. |
| 10745 | 10907 | */ |
| | @@ -11317,16 +11479,18 @@ |
| 11317 | 11479 | unsigned idxType:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */ |
| 11318 | 11480 | unsigned bUnordered:1; /* Use this index for == or IN queries only */ |
| 11319 | 11481 | unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */ |
| 11320 | 11482 | unsigned isResized:1; /* True if resizeIndexObject() has been called */ |
| 11321 | 11483 | unsigned isCovering:1; /* True if this is a covering index */ |
| 11484 | + unsigned noSkipScan:1; /* Do not try to use skip-scan if true */ |
| 11322 | 11485 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 11323 | 11486 | int nSample; /* Number of elements in aSample[] */ |
| 11324 | 11487 | int nSampleCol; /* Size of IndexSample.anEq[] and so on */ |
| 11325 | 11488 | tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */ |
| 11326 | 11489 | IndexSample *aSample; /* Samples of the left-most key */ |
| 11327 | | - tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this table */ |
| 11490 | + tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */ |
| 11491 | + tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */ |
| 11328 | 11492 | #endif |
| 11329 | 11493 | }; |
| 11330 | 11494 | |
| 11331 | 11495 | /* |
| 11332 | 11496 | ** Allowed values for Index.idxType |
| | @@ -11520,11 +11684,11 @@ |
| 11520 | 11684 | int nHeight; /* Height of the tree headed by this node */ |
| 11521 | 11685 | #endif |
| 11522 | 11686 | int iTable; /* TK_COLUMN: cursor number of table holding column |
| 11523 | 11687 | ** TK_REGISTER: register number |
| 11524 | 11688 | ** TK_TRIGGER: 1 -> new, 0 -> old |
| 11525 | | - ** EP_Unlikely: 1000 times likelihood */ |
| 11689 | + ** EP_Unlikely: 134217728 times likelihood */ |
| 11526 | 11690 | ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid. |
| 11527 | 11691 | ** TK_VARIABLE: variable number (always >= 1). */ |
| 11528 | 11692 | i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */ |
| 11529 | 11693 | i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */ |
| 11530 | 11694 | u8 op2; /* TK_REGISTER: original value of Expr.op |
| | @@ -11535,11 +11699,11 @@ |
| 11535 | 11699 | }; |
| 11536 | 11700 | |
| 11537 | 11701 | /* |
| 11538 | 11702 | ** The following are the meanings of bits in the Expr.flags field. |
| 11539 | 11703 | */ |
| 11540 | | -#define EP_FromJoin 0x000001 /* Originated in ON or USING clause of a join */ |
| 11704 | +#define EP_FromJoin 0x000001 /* Originates in ON/USING clause of outer join */ |
| 11541 | 11705 | #define EP_Agg 0x000002 /* Contains one or more aggregate functions */ |
| 11542 | 11706 | #define EP_Resolved 0x000004 /* IDs have been resolved to COLUMNs */ |
| 11543 | 11707 | #define EP_Error 0x000008 /* Expression contains one or more errors */ |
| 11544 | 11708 | #define EP_Distinct 0x000010 /* Aggregate function with DISTINCT keyword */ |
| 11545 | 11709 | #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */ |
| | @@ -11555,10 +11719,11 @@ |
| 11555 | 11719 | #define EP_Static 0x008000 /* Held in memory not obtained from malloc() */ |
| 11556 | 11720 | #define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */ |
| 11557 | 11721 | #define EP_NoReduce 0x020000 /* Cannot EXPRDUP_REDUCE this Expr */ |
| 11558 | 11722 | #define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */ |
| 11559 | 11723 | #define EP_Constant 0x080000 /* Node is a constant */ |
| 11724 | +#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */ |
| 11560 | 11725 | |
| 11561 | 11726 | /* |
| 11562 | 11727 | ** These macros can be used to test, set, or clear bits in the |
| 11563 | 11728 | ** Expr.flags field. |
| 11564 | 11729 | */ |
| | @@ -12412,13 +12577,15 @@ |
| 12412 | 12577 | int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */ |
| 12413 | 12578 | int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */ |
| 12414 | 12579 | void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */ |
| 12415 | 12580 | Parse *pParse; /* Parser context. */ |
| 12416 | 12581 | int walkerDepth; /* Number of subqueries */ |
| 12582 | + u8 eCode; /* A small processing code */ |
| 12417 | 12583 | union { /* Extra data for callback */ |
| 12418 | 12584 | NameContext *pNC; /* Naming context */ |
| 12419 | | - int i; /* Integer value */ |
| 12585 | + int n; /* A counter */ |
| 12586 | + int iCur; /* A cursor number */ |
| 12420 | 12587 | SrcList *pSrcList; /* FROM clause */ |
| 12421 | 12588 | struct SrcCount *pSrcCount; /* Counting column references */ |
| 12422 | 12589 | } u; |
| 12423 | 12590 | }; |
| 12424 | 12591 | |
| | @@ -12815,10 +12982,11 @@ |
| 12815 | 12982 | SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *); |
| 12816 | 12983 | SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*); |
| 12817 | 12984 | SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*); |
| 12818 | 12985 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*); |
| 12819 | 12986 | SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8); |
| 12987 | +SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int); |
| 12820 | 12988 | SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*); |
| 12821 | 12989 | SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*); |
| 12822 | 12990 | SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); |
| 12823 | 12991 | SQLITE_PRIVATE int sqlite3IsRowid(const char*); |
| 12824 | 12992 | SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8); |
| | @@ -13060,11 +13228,11 @@ |
| 13060 | 13228 | SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *); |
| 13061 | 13229 | |
| 13062 | 13230 | SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int); |
| 13063 | 13231 | SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int); |
| 13064 | 13232 | SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*); |
| 13065 | | -SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int); |
| 13233 | +SQLITE_PRIVATE void sqlite3AppendChar(StrAccum*,int,char); |
| 13066 | 13234 | SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*); |
| 13067 | 13235 | SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*); |
| 13068 | 13236 | SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int); |
| 13069 | 13237 | SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int); |
| 13070 | 13238 | |
| | @@ -13472,15 +13640,23 @@ |
| 13472 | 13640 | ** compatibility for legacy applications, the URI filename capability is |
| 13473 | 13641 | ** disabled by default. |
| 13474 | 13642 | ** |
| 13475 | 13643 | ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled |
| 13476 | 13644 | ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options. |
| 13645 | +** |
| 13646 | +** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally |
| 13647 | +** disabled. The default value may be changed by compiling with the |
| 13648 | +** SQLITE_USE_URI symbol defined. |
| 13477 | 13649 | */ |
| 13478 | 13650 | #ifndef SQLITE_USE_URI |
| 13479 | 13651 | # define SQLITE_USE_URI 0 |
| 13480 | 13652 | #endif |
| 13481 | 13653 | |
| 13654 | +/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the |
| 13655 | +** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if |
| 13656 | +** that compile-time option is omitted. |
| 13657 | +*/ |
| 13482 | 13658 | #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN |
| 13483 | 13659 | # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1 |
| 13484 | 13660 | #endif |
| 13485 | 13661 | |
| 13486 | 13662 | /* |
| | @@ -13566,12 +13742,12 @@ |
| 13566 | 13742 | ** than 1 GiB. The sqlite3_test_control() interface can be used to |
| 13567 | 13743 | ** move the pending byte. |
| 13568 | 13744 | ** |
| 13569 | 13745 | ** IMPORTANT: Changing the pending byte to any value other than |
| 13570 | 13746 | ** 0x40000000 results in an incompatible database file format! |
| 13571 | | -** Changing the pending byte during operating results in undefined |
| 13572 | | -** and dileterious behavior. |
| 13747 | +** Changing the pending byte during operation will result in undefined |
| 13748 | +** and incorrect behavior. |
| 13573 | 13749 | */ |
| 13574 | 13750 | #ifndef SQLITE_OMIT_WSD |
| 13575 | 13751 | SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000; |
| 13576 | 13752 | #endif |
| 13577 | 13753 | |
| | @@ -13646,10 +13822,13 @@ |
| 13646 | 13822 | #ifdef SQLITE_DISABLE_DIRSYNC |
| 13647 | 13823 | "DISABLE_DIRSYNC", |
| 13648 | 13824 | #endif |
| 13649 | 13825 | #ifdef SQLITE_DISABLE_LFS |
| 13650 | 13826 | "DISABLE_LFS", |
| 13827 | +#endif |
| 13828 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 13829 | + "ENABLE_API_ARMOR", |
| 13651 | 13830 | #endif |
| 13652 | 13831 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 13653 | 13832 | "ENABLE_ATOMIC_WRITE", |
| 13654 | 13833 | #endif |
| 13655 | 13834 | #ifdef SQLITE_ENABLE_CEROD |
| | @@ -13972,10 +14151,17 @@ |
| 13972 | 14151 | ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix |
| 13973 | 14152 | ** is not required for a match. |
| 13974 | 14153 | */ |
| 13975 | 14154 | SQLITE_API int sqlite3_compileoption_used(const char *zOptName){ |
| 13976 | 14155 | int i, n; |
| 14156 | + |
| 14157 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 14158 | + if( zOptName==0 ){ |
| 14159 | + (void)SQLITE_MISUSE_BKPT; |
| 14160 | + return 0; |
| 14161 | + } |
| 14162 | +#endif |
| 13977 | 14163 | if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7; |
| 13978 | 14164 | n = sqlite3Strlen30(zOptName); |
| 13979 | 14165 | |
| 13980 | 14166 | /* Since ArraySize(azCompileOpt) is normally in single digits, a |
| 13981 | 14167 | ** linear search is adequate. No need for a binary search. */ |
| | @@ -14153,10 +14339,11 @@ |
| 14153 | 14339 | typedef struct VdbeFrame VdbeFrame; |
| 14154 | 14340 | struct VdbeFrame { |
| 14155 | 14341 | Vdbe *v; /* VM this frame belongs to */ |
| 14156 | 14342 | VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */ |
| 14157 | 14343 | Op *aOp; /* Program instructions for parent frame */ |
| 14344 | + i64 *anExec; /* Event counters from parent frame */ |
| 14158 | 14345 | Mem *aMem; /* Array of memory cells for parent frame */ |
| 14159 | 14346 | u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */ |
| 14160 | 14347 | VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */ |
| 14161 | 14348 | void *token; /* Copy of SubProgram.token */ |
| 14162 | 14349 | i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */ |
| | @@ -14165,11 +14352,12 @@ |
| 14165 | 14352 | int nOp; /* Size of aOp array */ |
| 14166 | 14353 | int nMem; /* Number of entries in aMem */ |
| 14167 | 14354 | int nOnceFlag; /* Number of entries in aOnceFlag */ |
| 14168 | 14355 | int nChildMem; /* Number of memory cells for child frame */ |
| 14169 | 14356 | int nChildCsr; /* Number of cursors for child frame */ |
| 14170 | | - int nChange; /* Statement changes (Vdbe.nChanges) */ |
| 14357 | + int nChange; /* Statement changes (Vdbe.nChange) */ |
| 14358 | + int nDbChange; /* Value of db->nChange */ |
| 14171 | 14359 | }; |
| 14172 | 14360 | |
| 14173 | 14361 | #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) |
| 14174 | 14362 | |
| 14175 | 14363 | /* |
| | @@ -14316,10 +14504,20 @@ |
| 14316 | 14504 | /* A bitfield type for use inside of structures. Always follow with :N where |
| 14317 | 14505 | ** N is the number of bits. |
| 14318 | 14506 | */ |
| 14319 | 14507 | typedef unsigned bft; /* Bit Field Type */ |
| 14320 | 14508 | |
| 14509 | +typedef struct ScanStatus ScanStatus; |
| 14510 | +struct ScanStatus { |
| 14511 | + int addrExplain; /* OP_Explain for loop */ |
| 14512 | + int addrLoop; /* Address of "loops" counter */ |
| 14513 | + int addrVisit; /* Address of "rows visited" counter */ |
| 14514 | + int iSelectID; /* The "Select-ID" for this loop */ |
| 14515 | + LogEst nEst; /* Estimated output rows per loop */ |
| 14516 | + char *zName; /* Name of table or index */ |
| 14517 | +}; |
| 14518 | + |
| 14321 | 14519 | /* |
| 14322 | 14520 | ** An instance of the virtual machine. This structure contains the complete |
| 14323 | 14521 | ** state of the virtual machine. |
| 14324 | 14522 | ** |
| 14325 | 14523 | ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare() |
| | @@ -14388,10 +14586,15 @@ |
| 14388 | 14586 | u32 expmask; /* Binding to these vars invalidates VM */ |
| 14389 | 14587 | SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ |
| 14390 | 14588 | int nOnceFlag; /* Size of array aOnceFlag[] */ |
| 14391 | 14589 | u8 *aOnceFlag; /* Flags for OP_Once */ |
| 14392 | 14590 | AuxData *pAuxData; /* Linked list of auxdata allocations */ |
| 14591 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 14592 | + i64 *anExec; /* Number of times each op has been executed */ |
| 14593 | + int nScan; /* Entries in aScan[] */ |
| 14594 | + ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */ |
| 14595 | +#endif |
| 14393 | 14596 | }; |
| 14394 | 14597 | |
| 14395 | 14598 | /* |
| 14396 | 14599 | ** The following are allowed values for Vdbe.magic |
| 14397 | 14600 | */ |
| | @@ -14577,10 +14780,13 @@ |
| 14577 | 14780 | SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ |
| 14578 | 14781 | wsdStatInit; |
| 14579 | 14782 | if( op<0 || op>=ArraySize(wsdStat.nowValue) ){ |
| 14580 | 14783 | return SQLITE_MISUSE_BKPT; |
| 14581 | 14784 | } |
| 14785 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 14786 | + if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; |
| 14787 | +#endif |
| 14582 | 14788 | *pCurrent = wsdStat.nowValue[op]; |
| 14583 | 14789 | *pHighwater = wsdStat.mxValue[op]; |
| 14584 | 14790 | if( resetFlag ){ |
| 14585 | 14791 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
| 14586 | 14792 | } |
| | @@ -14596,10 +14802,15 @@ |
| 14596 | 14802 | int *pCurrent, /* Write current value here */ |
| 14597 | 14803 | int *pHighwater, /* Write high-water mark here */ |
| 14598 | 14804 | int resetFlag /* Reset high-water mark if true */ |
| 14599 | 14805 | ){ |
| 14600 | 14806 | int rc = SQLITE_OK; /* Return code */ |
| 14807 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 14808 | + if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){ |
| 14809 | + return SQLITE_MISUSE_BKPT; |
| 14810 | + } |
| 14811 | +#endif |
| 14601 | 14812 | sqlite3_mutex_enter(db->mutex); |
| 14602 | 14813 | switch( op ){ |
| 14603 | 14814 | case SQLITE_DBSTATUS_LOOKASIDE_USED: { |
| 14604 | 14815 | *pCurrent = db->lookaside.nOut; |
| 14605 | 14816 | *pHighwater = db->lookaside.mxOut; |
| | @@ -14774,11 +14985,11 @@ |
| 14774 | 14985 | ** |
| 14775 | 14986 | ** There is only one exported symbol in this file - the function |
| 14776 | 14987 | ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. |
| 14777 | 14988 | ** All other code has file scope. |
| 14778 | 14989 | ** |
| 14779 | | -** SQLite processes all times and dates as Julian Day numbers. The |
| 14990 | +** SQLite processes all times and dates as julian day numbers. The |
| 14780 | 14991 | ** dates and times are stored as the number of days since noon |
| 14781 | 14992 | ** in Greenwich on November 24, 4714 B.C. according to the Gregorian |
| 14782 | 14993 | ** calendar system. |
| 14783 | 14994 | ** |
| 14784 | 14995 | ** 1970-01-01 00:00:00 is JD 2440587.5 |
| | @@ -14789,11 +15000,11 @@ |
| 14789 | 15000 | ** be represented, even though julian day numbers allow a much wider |
| 14790 | 15001 | ** range of dates. |
| 14791 | 15002 | ** |
| 14792 | 15003 | ** The Gregorian calendar system is used for all dates and times, |
| 14793 | 15004 | ** even those that predate the Gregorian calendar. Historians usually |
| 14794 | | -** use the Julian calendar for dates prior to 1582-10-15 and for some |
| 15005 | +** use the julian calendar for dates prior to 1582-10-15 and for some |
| 14795 | 15006 | ** dates afterwards, depending on locale. Beware of this difference. |
| 14796 | 15007 | ** |
| 14797 | 15008 | ** The conversion algorithms are implemented based on descriptions |
| 14798 | 15009 | ** in the following text: |
| 14799 | 15010 | ** |
| | @@ -15061,11 +15272,11 @@ |
| 15061 | 15272 | return 1; |
| 15062 | 15273 | } |
| 15063 | 15274 | } |
| 15064 | 15275 | |
| 15065 | 15276 | /* |
| 15066 | | -** Attempt to parse the given string into a Julian Day Number. Return |
| 15277 | +** Attempt to parse the given string into a julian day number. Return |
| 15067 | 15278 | ** the number of errors. |
| 15068 | 15279 | ** |
| 15069 | 15280 | ** The following are acceptable forms for the input string: |
| 15070 | 15281 | ** |
| 15071 | 15282 | ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM |
| | @@ -15632,11 +15843,11 @@ |
| 15632 | 15843 | ** |
| 15633 | 15844 | ** %d day of month |
| 15634 | 15845 | ** %f ** fractional seconds SS.SSS |
| 15635 | 15846 | ** %H hour 00-24 |
| 15636 | 15847 | ** %j day of year 000-366 |
| 15637 | | -** %J ** Julian day number |
| 15848 | +** %J ** julian day number |
| 15638 | 15849 | ** %m month 01-12 |
| 15639 | 15850 | ** %M minute 00-59 |
| 15640 | 15851 | ** %s seconds since 1970-01-01 |
| 15641 | 15852 | ** %S seconds 00-59 |
| 15642 | 15853 | ** %w day of week 0-6 sunday==0 |
| | @@ -16257,10 +16468,14 @@ |
| 16257 | 16468 | MUTEX_LOGIC(sqlite3_mutex *mutex;) |
| 16258 | 16469 | #ifndef SQLITE_OMIT_AUTOINIT |
| 16259 | 16470 | int rc = sqlite3_initialize(); |
| 16260 | 16471 | if( rc ) return rc; |
| 16261 | 16472 | #endif |
| 16473 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 16474 | + if( pVfs==0 ) return SQLITE_MISUSE_BKPT; |
| 16475 | +#endif |
| 16476 | + |
| 16262 | 16477 | MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 16263 | 16478 | sqlite3_mutex_enter(mutex); |
| 16264 | 16479 | vfsUnlink(pVfs); |
| 16265 | 16480 | if( makeDflt || vfsList==0 ){ |
| 16266 | 16481 | pVfs->pNext = vfsList; |
| | @@ -18614,10 +18829,11 @@ |
| 18614 | 18829 | ** Retrieve a pointer to a static mutex or allocate a new dynamic one. |
| 18615 | 18830 | */ |
| 18616 | 18831 | SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){ |
| 18617 | 18832 | #ifndef SQLITE_OMIT_AUTOINIT |
| 18618 | 18833 | if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0; |
| 18834 | + if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0; |
| 18619 | 18835 | #endif |
| 18620 | 18836 | return sqlite3GlobalConfig.mutex.xMutexAlloc(id); |
| 18621 | 18837 | } |
| 18622 | 18838 | |
| 18623 | 18839 | SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){ |
| | @@ -19070,12 +19286,16 @@ |
| 19070 | 19286 | pthread_mutex_init(&p->mutex, 0); |
| 19071 | 19287 | } |
| 19072 | 19288 | break; |
| 19073 | 19289 | } |
| 19074 | 19290 | default: { |
| 19075 | | - assert( iType-2 >= 0 ); |
| 19076 | | - assert( iType-2 < ArraySize(staticMutexes) ); |
| 19291 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 19292 | + if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ |
| 19293 | + (void)SQLITE_MISUSE_BKPT; |
| 19294 | + return 0; |
| 19295 | + } |
| 19296 | +#endif |
| 19077 | 19297 | p = &staticMutexes[iType-2]; |
| 19078 | 19298 | #if SQLITE_MUTEX_NREF |
| 19079 | 19299 | p->id = iType; |
| 19080 | 19300 | #endif |
| 19081 | 19301 | break; |
| | @@ -20293,15 +20513,16 @@ |
| 20293 | 20513 | } |
| 20294 | 20514 | assert( sqlite3_mutex_notheld(mem0.mutex) ); |
| 20295 | 20515 | |
| 20296 | 20516 | |
| 20297 | 20517 | #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 ); |
| 20518 | + /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch |
| 20519 | + ** buffers per thread. |
| 20520 | + ** |
| 20521 | + ** This can only be checked in single-threaded mode. |
| 20522 | + */ |
| 20523 | + assert( scratchAllocOut==0 ); |
| 20303 | 20524 | if( p ) scratchAllocOut++; |
| 20304 | 20525 | #endif |
| 20305 | 20526 | |
| 20306 | 20527 | return p; |
| 20307 | 20528 | } |
| | @@ -20945,11 +21166,11 @@ |
| 20945 | 21166 | sqlite_uint64 longvalue; /* Value for integer types */ |
| 20946 | 21167 | LONGDOUBLE_TYPE realvalue; /* Value for real types */ |
| 20947 | 21168 | const et_info *infop; /* Pointer to the appropriate info structure */ |
| 20948 | 21169 | char *zOut; /* Rendering buffer */ |
| 20949 | 21170 | int nOut; /* Size of the rendering buffer */ |
| 20950 | | - char *zExtra; /* Malloced memory used by some conversion */ |
| 21171 | + char *zExtra = 0; /* Malloced memory used by some conversion */ |
| 20951 | 21172 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 20952 | 21173 | int exp, e2; /* exponent of real numbers */ |
| 20953 | 21174 | int nsd; /* Number of significant digits returned */ |
| 20954 | 21175 | double rounder; /* Used for rounding floating point values */ |
| 20955 | 21176 | etByte flag_dp; /* True if decimal point should be shown */ |
| | @@ -20956,10 +21177,17 @@ |
| 20956 | 21177 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
| 20957 | 21178 | #endif |
| 20958 | 21179 | PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ |
| 20959 | 21180 | char buf[etBUFSIZE]; /* Conversion buffer */ |
| 20960 | 21181 | |
| 21182 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 21183 | + if( ap==0 ){ |
| 21184 | + (void)SQLITE_MISUSE_BKPT; |
| 21185 | + sqlite3StrAccumReset(pAccum); |
| 21186 | + return; |
| 21187 | + } |
| 21188 | +#endif |
| 20961 | 21189 | bufpt = 0; |
| 20962 | 21190 | if( bFlags ){ |
| 20963 | 21191 | if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ |
| 20964 | 21192 | pArgList = va_arg(ap, PrintfArguments*); |
| 20965 | 21193 | } |
| | @@ -21062,11 +21290,10 @@ |
| 21062 | 21290 | return; |
| 21063 | 21291 | } |
| 21064 | 21292 | break; |
| 21065 | 21293 | } |
| 21066 | 21294 | } |
| 21067 | | - zExtra = 0; |
| 21068 | 21295 | |
| 21069 | 21296 | /* |
| 21070 | 21297 | ** At this point, variables are initialized as follows: |
| 21071 | 21298 | ** |
| 21072 | 21299 | ** flag_alternateform TRUE if a '#' is present. |
| | @@ -21353,17 +21580,20 @@ |
| 21353 | 21580 | bufpt = getTextArg(pArgList); |
| 21354 | 21581 | c = bufpt ? bufpt[0] : 0; |
| 21355 | 21582 | }else{ |
| 21356 | 21583 | c = va_arg(ap,int); |
| 21357 | 21584 | } |
| 21358 | | - buf[0] = (char)c; |
| 21359 | | - if( precision>=0 ){ |
| 21360 | | - for(idx=1; idx<precision; idx++) buf[idx] = (char)c; |
| 21361 | | - length = precision; |
| 21362 | | - }else{ |
| 21363 | | - length =1; |
| 21585 | + if( precision>1 ){ |
| 21586 | + width -= precision-1; |
| 21587 | + if( width>1 && !flag_leftjustify ){ |
| 21588 | + sqlite3AppendChar(pAccum, width-1, ' '); |
| 21589 | + width = 0; |
| 21590 | + } |
| 21591 | + sqlite3AppendChar(pAccum, precision-1, c); |
| 21364 | 21592 | } |
| 21593 | + length = 1; |
| 21594 | + buf[0] = c; |
| 21365 | 21595 | bufpt = buf; |
| 21366 | 21596 | break; |
| 21367 | 21597 | case etSTRING: |
| 21368 | 21598 | case etDYNSTRING: |
| 21369 | 21599 | if( bArgList ){ |
| | @@ -21460,15 +21690,18 @@ |
| 21460 | 21690 | ** The text of the conversion is pointed to by "bufpt" and is |
| 21461 | 21691 | ** "length" characters long. The field width is "width". Do |
| 21462 | 21692 | ** the output. |
| 21463 | 21693 | */ |
| 21464 | 21694 | width -= length; |
| 21465 | | - if( width>0 && !flag_leftjustify ) sqlite3AppendSpace(pAccum, width); |
| 21695 | + if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); |
| 21466 | 21696 | sqlite3StrAccumAppend(pAccum, bufpt, length); |
| 21467 | | - if( width>0 && flag_leftjustify ) sqlite3AppendSpace(pAccum, width); |
| 21697 | + if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); |
| 21468 | 21698 | |
| 21469 | | - if( zExtra ) sqlite3_free(zExtra); |
| 21699 | + if( zExtra ){ |
| 21700 | + sqlite3_free(zExtra); |
| 21701 | + zExtra = 0; |
| 21702 | + } |
| 21470 | 21703 | }/* End for loop over the format string */ |
| 21471 | 21704 | } /* End of function */ |
| 21472 | 21705 | |
| 21473 | 21706 | /* |
| 21474 | 21707 | ** Enlarge the memory allocation on a StrAccum object so that it is |
| | @@ -21491,10 +21724,15 @@ |
| 21491 | 21724 | return N; |
| 21492 | 21725 | }else{ |
| 21493 | 21726 | char *zOld = (p->zText==p->zBase ? 0 : p->zText); |
| 21494 | 21727 | i64 szNew = p->nChar; |
| 21495 | 21728 | szNew += N + 1; |
| 21729 | + if( szNew+p->nChar<=p->mxAlloc ){ |
| 21730 | + /* Force exponential buffer size growth as long as it does not overflow, |
| 21731 | + ** to avoid having to call this routine too often */ |
| 21732 | + szNew += p->nChar; |
| 21733 | + } |
| 21496 | 21734 | if( szNew > p->mxAlloc ){ |
| 21497 | 21735 | sqlite3StrAccumReset(p); |
| 21498 | 21736 | setStrAccumError(p, STRACCUM_TOOBIG); |
| 21499 | 21737 | return 0; |
| 21500 | 21738 | }else{ |
| | @@ -21507,10 +21745,11 @@ |
| 21507 | 21745 | } |
| 21508 | 21746 | if( zNew ){ |
| 21509 | 21747 | assert( p->zText!=0 || p->nChar==0 ); |
| 21510 | 21748 | if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 21511 | 21749 | p->zText = zNew; |
| 21750 | + p->nAlloc = sqlite3DbMallocSize(p->db, zNew); |
| 21512 | 21751 | }else{ |
| 21513 | 21752 | sqlite3StrAccumReset(p); |
| 21514 | 21753 | setStrAccumError(p, STRACCUM_NOMEM); |
| 21515 | 21754 | return 0; |
| 21516 | 21755 | } |
| | @@ -21517,15 +21756,15 @@ |
| 21517 | 21756 | } |
| 21518 | 21757 | return N; |
| 21519 | 21758 | } |
| 21520 | 21759 | |
| 21521 | 21760 | /* |
| 21522 | | -** Append N space characters to the given string buffer. |
| 21761 | +** Append N copies of character c to the given string buffer. |
| 21523 | 21762 | */ |
| 21524 | | -SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *p, int N){ |
| 21763 | +SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){ |
| 21525 | 21764 | if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return; |
| 21526 | | - while( (N--)>0 ) p->zText[p->nChar++] = ' '; |
| 21765 | + while( (N--)>0 ) p->zText[p->nChar++] = c; |
| 21527 | 21766 | } |
| 21528 | 21767 | |
| 21529 | 21768 | /* |
| 21530 | 21769 | ** The StrAccum "p" is not large enough to accept N new bytes of z[]. |
| 21531 | 21770 | ** So enlarge if first, then do the append. |
| | @@ -21676,10 +21915,17 @@ |
| 21676 | 21915 | */ |
| 21677 | 21916 | SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){ |
| 21678 | 21917 | char *z; |
| 21679 | 21918 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
| 21680 | 21919 | StrAccum acc; |
| 21920 | + |
| 21921 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 21922 | + if( zFormat==0 ){ |
| 21923 | + (void)SQLITE_MISUSE_BKPT; |
| 21924 | + return 0; |
| 21925 | + } |
| 21926 | +#endif |
| 21681 | 21927 | #ifndef SQLITE_OMIT_AUTOINIT |
| 21682 | 21928 | if( sqlite3_initialize() ) return 0; |
| 21683 | 21929 | #endif |
| 21684 | 21930 | sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
| 21685 | 21931 | acc.useMalloc = 2; |
| | @@ -21718,10 +21964,17 @@ |
| 21718 | 21964 | ** sqlite3_vsnprintf() is the varargs version. |
| 21719 | 21965 | */ |
| 21720 | 21966 | SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ |
| 21721 | 21967 | StrAccum acc; |
| 21722 | 21968 | if( n<=0 ) return zBuf; |
| 21969 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 21970 | + if( zBuf==0 || zFormat==0 ) { |
| 21971 | + (void)SQLITE_MISUSE_BKPT; |
| 21972 | + if( zBuf && n>0 ) zBuf[0] = 0; |
| 21973 | + return zBuf; |
| 21974 | + } |
| 21975 | +#endif |
| 21723 | 21976 | sqlite3StrAccumInit(&acc, zBuf, n, 0); |
| 21724 | 21977 | acc.useMalloc = 0; |
| 21725 | 21978 | sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 21726 | 21979 | return sqlite3StrAccumFinish(&acc); |
| 21727 | 21980 | } |
| | @@ -21909,15 +22162,23 @@ |
| 21909 | 22162 | #else |
| 21910 | 22163 | # define wsdPrng sqlite3Prng |
| 21911 | 22164 | #endif |
| 21912 | 22165 | |
| 21913 | 22166 | #if SQLITE_THREADSAFE |
| 21914 | | - sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); |
| 22167 | + sqlite3_mutex *mutex; |
| 22168 | +#endif |
| 22169 | + |
| 22170 | +#ifndef SQLITE_OMIT_AUTOINIT |
| 22171 | + if( sqlite3_initialize() ) return; |
| 22172 | +#endif |
| 22173 | + |
| 22174 | +#if SQLITE_THREADSAFE |
| 22175 | + mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); |
| 22176 | +#endif |
| 22177 | + |
| 21915 | 22178 | sqlite3_mutex_enter(mutex); |
| 21916 | | -#endif |
| 21917 | | - |
| 21918 | | - if( N<=0 ){ |
| 22179 | + if( N<=0 || pBuf==0 ){ |
| 21919 | 22180 | wsdPrng.isInit = 0; |
| 21920 | 22181 | sqlite3_mutex_leave(mutex); |
| 21921 | 22182 | return; |
| 21922 | 22183 | } |
| 21923 | 22184 | |
| | @@ -23035,17 +23296,27 @@ |
| 23035 | 23296 | ** case-independent fashion, using the same definition of "case |
| 23036 | 23297 | ** independence" that SQLite uses internally when comparing identifiers. |
| 23037 | 23298 | */ |
| 23038 | 23299 | SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){ |
| 23039 | 23300 | register unsigned char *a, *b; |
| 23301 | + if( zLeft==0 ){ |
| 23302 | + return zRight ? -1 : 0; |
| 23303 | + }else if( zRight==0 ){ |
| 23304 | + return 1; |
| 23305 | + } |
| 23040 | 23306 | a = (unsigned char *)zLeft; |
| 23041 | 23307 | b = (unsigned char *)zRight; |
| 23042 | 23308 | while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 23043 | 23309 | return UpperToLower[*a] - UpperToLower[*b]; |
| 23044 | 23310 | } |
| 23045 | 23311 | SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ |
| 23046 | 23312 | register unsigned char *a, *b; |
| 23313 | + if( zLeft==0 ){ |
| 23314 | + return zRight ? -1 : 0; |
| 23315 | + }else if( zRight==0 ){ |
| 23316 | + return 1; |
| 23317 | + } |
| 23047 | 23318 | a = (unsigned char *)zLeft; |
| 23048 | 23319 | b = (unsigned char *)zRight; |
| 23049 | 23320 | while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 23050 | 23321 | return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
| 23051 | 23322 | } |
| | @@ -32574,10 +32845,15 @@ |
| 32574 | 32845 | #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL) |
| 32575 | 32846 | # error "WAL mode requires support from the Windows NT kernel, compile\ |
| 32576 | 32847 | with SQLITE_OMIT_WAL." |
| 32577 | 32848 | #endif |
| 32578 | 32849 | |
| 32850 | +#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0 |
| 32851 | +# error "Memory mapped files require support from the Windows NT kernel,\ |
| 32852 | + compile with SQLITE_MAX_MMAP_SIZE=0." |
| 32853 | +#endif |
| 32854 | + |
| 32579 | 32855 | /* |
| 32580 | 32856 | ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions |
| 32581 | 32857 | ** based on the sub-platform)? |
| 32582 | 32858 | */ |
| 32583 | 32859 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI) |
| | @@ -32703,14 +32979,15 @@ |
| 32703 | 32979 | # define winGetDirSep() '\\' |
| 32704 | 32980 | #endif |
| 32705 | 32981 | |
| 32706 | 32982 | /* |
| 32707 | 32983 | ** Do we need to manually define the Win32 file mapping APIs for use with WAL |
| 32708 | | -** mode (e.g. these APIs are available in the Windows CE SDK; however, they |
| 32709 | | -** are not present in the header file)? |
| 32984 | +** mode or memory mapped files (e.g. these APIs are available in the Windows |
| 32985 | +** CE SDK; however, they are not present in the header file)? |
| 32710 | 32986 | */ |
| 32711 | | -#if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) |
| 32987 | +#if SQLITE_WIN32_FILEMAPPING_API && \ |
| 32988 | + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
| 32712 | 32989 | /* |
| 32713 | 32990 | ** Two of the file mapping APIs are different under WinRT. Figure out which |
| 32714 | 32991 | ** set we need. |
| 32715 | 32992 | */ |
| 32716 | 32993 | #if SQLITE_OS_WINRT |
| | @@ -32734,11 +33011,11 @@ |
| 32734 | 33011 | |
| 32735 | 33012 | /* |
| 32736 | 33013 | ** This file mapping API is common to both Win32 and WinRT. |
| 32737 | 33014 | */ |
| 32738 | 33015 | WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID); |
| 32739 | | -#endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */ |
| 33016 | +#endif /* SQLITE_WIN32_FILEMAPPING_API */ |
| 32740 | 33017 | |
| 32741 | 33018 | /* |
| 32742 | 33019 | ** Some Microsoft compilers lack this definition. |
| 32743 | 33020 | */ |
| 32744 | 33021 | #ifndef INVALID_FILE_ATTRIBUTES |
| | @@ -33027,21 +33304,21 @@ |
| 33027 | 33304 | |
| 33028 | 33305 | #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \ |
| 33029 | 33306 | LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent) |
| 33030 | 33307 | |
| 33031 | 33308 | #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \ |
| 33032 | | - !defined(SQLITE_OMIT_WAL)) |
| 33309 | + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
| 33033 | 33310 | { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 }, |
| 33034 | 33311 | #else |
| 33035 | 33312 | { "CreateFileMappingA", (SYSCALL)0, 0 }, |
| 33036 | 33313 | #endif |
| 33037 | 33314 | |
| 33038 | 33315 | #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \ |
| 33039 | 33316 | DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent) |
| 33040 | 33317 | |
| 33041 | 33318 | #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ |
| 33042 | | - !defined(SQLITE_OMIT_WAL)) |
| 33319 | + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
| 33043 | 33320 | { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 }, |
| 33044 | 33321 | #else |
| 33045 | 33322 | { "CreateFileMappingW", (SYSCALL)0, 0 }, |
| 33046 | 33323 | #endif |
| 33047 | 33324 | |
| | @@ -33377,11 +33654,12 @@ |
| 33377 | 33654 | #ifndef osLockFileEx |
| 33378 | 33655 | #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \ |
| 33379 | 33656 | LPOVERLAPPED))aSyscall[48].pCurrent) |
| 33380 | 33657 | #endif |
| 33381 | 33658 | |
| 33382 | | -#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)) |
| 33659 | +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \ |
| 33660 | + (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
| 33383 | 33661 | { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 }, |
| 33384 | 33662 | #else |
| 33385 | 33663 | { "MapViewOfFile", (SYSCALL)0, 0 }, |
| 33386 | 33664 | #endif |
| 33387 | 33665 | |
| | @@ -33447,11 +33725,11 @@ |
| 33447 | 33725 | #endif |
| 33448 | 33726 | |
| 33449 | 33727 | #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
| 33450 | 33728 | LPOVERLAPPED))aSyscall[58].pCurrent) |
| 33451 | 33729 | |
| 33452 | | -#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) |
| 33730 | +#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
| 33453 | 33731 | { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 }, |
| 33454 | 33732 | #else |
| 33455 | 33733 | { "UnmapViewOfFile", (SYSCALL)0, 0 }, |
| 33456 | 33734 | #endif |
| 33457 | 33735 | |
| | @@ -33510,11 +33788,11 @@ |
| 33510 | 33788 | #endif |
| 33511 | 33789 | |
| 33512 | 33790 | #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \ |
| 33513 | 33791 | FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent) |
| 33514 | 33792 | |
| 33515 | | -#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL) |
| 33793 | +#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
| 33516 | 33794 | { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 }, |
| 33517 | 33795 | #else |
| 33518 | 33796 | { "MapViewOfFileFromApp", (SYSCALL)0, 0 }, |
| 33519 | 33797 | #endif |
| 33520 | 33798 | |
| | @@ -33574,11 +33852,11 @@ |
| 33574 | 33852 | |
| 33575 | 33853 | { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 }, |
| 33576 | 33854 | |
| 33577 | 33855 | #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent) |
| 33578 | 33856 | |
| 33579 | | -#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL) |
| 33857 | +#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
| 33580 | 33858 | { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 }, |
| 33581 | 33859 | #else |
| 33582 | 33860 | { "CreateFileMappingFromApp", (SYSCALL)0, 0 }, |
| 33583 | 33861 | #endif |
| 33584 | 33862 | |
| | @@ -39150,10 +39428,17 @@ |
| 39150 | 39428 | */ |
| 39151 | 39429 | SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){ |
| 39152 | 39430 | assert( pCache->pCache!=0 ); |
| 39153 | 39431 | sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache); |
| 39154 | 39432 | } |
| 39433 | + |
| 39434 | +/* |
| 39435 | +** Return the size of the header added by this middleware layer |
| 39436 | +** in the page-cache hierarchy. |
| 39437 | +*/ |
| 39438 | +SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return sizeof(PgHdr); } |
| 39439 | + |
| 39155 | 39440 | |
| 39156 | 39441 | #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) |
| 39157 | 39442 | /* |
| 39158 | 39443 | ** For all dirty pages currently in the cache, invoke the specified |
| 39159 | 39444 | ** callback. This is only used if the SQLITE_CHECK_PAGES macro is |
| | @@ -40149,10 +40434,15 @@ |
| 40149 | 40434 | pcache1Shrink /* xShrink */ |
| 40150 | 40435 | }; |
| 40151 | 40436 | sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods); |
| 40152 | 40437 | } |
| 40153 | 40438 | |
| 40439 | +/* |
| 40440 | +** Return the size of the header on each page of this PCACHE implementation. |
| 40441 | +*/ |
| 40442 | +SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return sizeof(PgHdr1); } |
| 40443 | + |
| 40154 | 40444 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 40155 | 40445 | /* |
| 40156 | 40446 | ** This function is called to free superfluous dynamically allocated memory |
| 40157 | 40447 | ** held by the pager system. Memory in use by any SQLite pager allocated |
| 40158 | 40448 | ** by the current thread may be sqlite3_free()ed. |
| | @@ -43758,11 +44048,11 @@ |
| 43758 | 44048 | ** should be page numbers which are never 0xffffffff. So filling |
| 43759 | 44049 | ** pPager->dbFileVers[] with all 0xff bytes should suffice. |
| 43760 | 44050 | ** |
| 43761 | 44051 | ** For an encrypted database, the situation is more complex: bytes |
| 43762 | 44052 | ** 24..39 of the database are white noise. But the probability of |
| 43763 | | - ** white noising equaling 16 bytes of 0xff is vanishingly small so |
| 44053 | + ** white noise equaling 16 bytes of 0xff is vanishingly small so |
| 43764 | 44054 | ** we should still be ok. |
| 43765 | 44055 | */ |
| 43766 | 44056 | memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers)); |
| 43767 | 44057 | }else{ |
| 43768 | 44058 | u8 *dbFileVers = &((u8*)pPg->pData)[24]; |
| | @@ -47705,10 +47995,22 @@ |
| 47705 | 47995 | } |
| 47706 | 47996 | |
| 47707 | 47997 | return SQLITE_OK; |
| 47708 | 47998 | } |
| 47709 | 47999 | #endif |
| 48000 | + |
| 48001 | +/* |
| 48002 | +** The page handle passed as the first argument refers to a dirty page |
| 48003 | +** with a page number other than iNew. This function changes the page's |
| 48004 | +** page number to iNew and sets the value of the PgHdr.flags field to |
| 48005 | +** the value passed as the third parameter. |
| 48006 | +*/ |
| 48007 | +SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){ |
| 48008 | + assert( pPg->pgno!=iNew ); |
| 48009 | + pPg->flags = flags; |
| 48010 | + sqlite3PcacheMove(pPg, iNew); |
| 48011 | +} |
| 47710 | 48012 | |
| 47711 | 48013 | /* |
| 47712 | 48014 | ** Return a pointer to the data for the specified page. |
| 47713 | 48015 | */ |
| 47714 | 48016 | SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){ |
| | @@ -47922,11 +48224,12 @@ |
| 47922 | 48224 | */ |
| 47923 | 48225 | SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){ |
| 47924 | 48226 | int rc = SQLITE_OK; |
| 47925 | 48227 | if( pPager->pWal ){ |
| 47926 | 48228 | rc = sqlite3WalCheckpoint(pPager->pWal, eMode, |
| 47927 | | - pPager->xBusyHandler, pPager->pBusyHandlerArg, |
| 48229 | + (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler), |
| 48230 | + pPager->pBusyHandlerArg, |
| 47928 | 48231 | pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace, |
| 47929 | 48232 | pnLog, pnCkpt |
| 47930 | 48233 | ); |
| 47931 | 48234 | } |
| 47932 | 48235 | return rc; |
| | @@ -48103,10 +48406,11 @@ |
| 48103 | 48406 | SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){ |
| 48104 | 48407 | assert( pPager->eState>=PAGER_READER ); |
| 48105 | 48408 | return sqlite3WalFramesize(pPager->pWal); |
| 48106 | 48409 | } |
| 48107 | 48410 | #endif |
| 48411 | + |
| 48108 | 48412 | |
| 48109 | 48413 | #endif /* SQLITE_OMIT_DISKIO */ |
| 48110 | 48414 | |
| 48111 | 48415 | /************** End of pager.c ***********************************************/ |
| 48112 | 48416 | /************** Begin file wal.c *********************************************/ |
| | @@ -49613,11 +49917,11 @@ |
| 49613 | 49917 | |
| 49614 | 49918 | /* |
| 49615 | 49919 | ** Free an iterator allocated by walIteratorInit(). |
| 49616 | 49920 | */ |
| 49617 | 49921 | static void walIteratorFree(WalIterator *p){ |
| 49618 | | - sqlite3ScratchFree(p); |
| 49922 | + sqlite3_free(p); |
| 49619 | 49923 | } |
| 49620 | 49924 | |
| 49621 | 49925 | /* |
| 49622 | 49926 | ** Construct a WalInterator object that can be used to loop over all |
| 49623 | 49927 | ** pages in the WAL in ascending order. The caller must hold the checkpoint |
| | @@ -49648,21 +49952,21 @@ |
| 49648 | 49952 | /* Allocate space for the WalIterator object. */ |
| 49649 | 49953 | nSegment = walFramePage(iLast) + 1; |
| 49650 | 49954 | nByte = sizeof(WalIterator) |
| 49651 | 49955 | + (nSegment-1)*sizeof(struct WalSegment) |
| 49652 | 49956 | + iLast*sizeof(ht_slot); |
| 49653 | | - p = (WalIterator *)sqlite3ScratchMalloc(nByte); |
| 49957 | + p = (WalIterator *)sqlite3_malloc(nByte); |
| 49654 | 49958 | if( !p ){ |
| 49655 | 49959 | return SQLITE_NOMEM; |
| 49656 | 49960 | } |
| 49657 | 49961 | memset(p, 0, nByte); |
| 49658 | 49962 | p->nSegment = nSegment; |
| 49659 | 49963 | |
| 49660 | 49964 | /* Allocate temporary space used by the merge-sort routine. This block |
| 49661 | 49965 | ** of memory will be freed before this function returns. |
| 49662 | 49966 | */ |
| 49663 | | - aTmp = (ht_slot *)sqlite3ScratchMalloc( |
| 49967 | + aTmp = (ht_slot *)sqlite3_malloc( |
| 49664 | 49968 | sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast) |
| 49665 | 49969 | ); |
| 49666 | 49970 | if( !aTmp ){ |
| 49667 | 49971 | rc = SQLITE_NOMEM; |
| 49668 | 49972 | } |
| | @@ -49695,11 +49999,11 @@ |
| 49695 | 49999 | p->aSegment[i].nEntry = nEntry; |
| 49696 | 50000 | p->aSegment[i].aIndex = aIndex; |
| 49697 | 50001 | p->aSegment[i].aPgno = (u32 *)aPgno; |
| 49698 | 50002 | } |
| 49699 | 50003 | } |
| 49700 | | - sqlite3ScratchFree(aTmp); |
| 50004 | + sqlite3_free(aTmp); |
| 49701 | 50005 | |
| 49702 | 50006 | if( rc!=SQLITE_OK ){ |
| 49703 | 50007 | walIteratorFree(p); |
| 49704 | 50008 | } |
| 49705 | 50009 | *pp = p; |
| | @@ -49731,10 +50035,42 @@ |
| 49731 | 50035 | ** Return the page-size in bytes used by the database. |
| 49732 | 50036 | */ |
| 49733 | 50037 | static int walPagesize(Wal *pWal){ |
| 49734 | 50038 | return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16); |
| 49735 | 50039 | } |
| 50040 | + |
| 50041 | +/* |
| 50042 | +** The following is guaranteed when this function is called: |
| 50043 | +** |
| 50044 | +** a) the WRITER lock is held, |
| 50045 | +** b) the entire log file has been checkpointed, and |
| 50046 | +** c) any existing readers are reading exclusively from the database |
| 50047 | +** file - there are no readers that may attempt to read a frame from |
| 50048 | +** the log file. |
| 50049 | +** |
| 50050 | +** This function updates the shared-memory structures so that the next |
| 50051 | +** client to write to the database (which may be this one) does so by |
| 50052 | +** writing frames into the start of the log file. |
| 50053 | +** |
| 50054 | +** The value of parameter salt1 is used as the aSalt[1] value in the |
| 50055 | +** new wal-index header. It should be passed a pseudo-random value (i.e. |
| 50056 | +** one obtained from sqlite3_randomness()). |
| 50057 | +*/ |
| 50058 | +static void walRestartHdr(Wal *pWal, u32 salt1){ |
| 50059 | + volatile WalCkptInfo *pInfo = walCkptInfo(pWal); |
| 50060 | + int i; /* Loop counter */ |
| 50061 | + u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */ |
| 50062 | + pWal->nCkpt++; |
| 50063 | + pWal->hdr.mxFrame = 0; |
| 50064 | + sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0])); |
| 50065 | + memcpy(&pWal->hdr.aSalt[1], &salt1, 4); |
| 50066 | + walIndexWriteHdr(pWal); |
| 50067 | + pInfo->nBackfill = 0; |
| 50068 | + pInfo->aReadMark[1] = 0; |
| 50069 | + for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
| 50070 | + assert( pInfo->aReadMark[0]==0 ); |
| 50071 | +} |
| 49736 | 50072 | |
| 49737 | 50073 | /* |
| 49738 | 50074 | ** Copy as much content as we can from the WAL back into the database file |
| 49739 | 50075 | ** in response to an sqlite3_wal_checkpoint() request or the equivalent. |
| 49740 | 50076 | ** |
| | @@ -49766,11 +50102,11 @@ |
| 49766 | 50102 | ** time. |
| 49767 | 50103 | */ |
| 49768 | 50104 | static int walCheckpoint( |
| 49769 | 50105 | Wal *pWal, /* Wal connection */ |
| 49770 | 50106 | int eMode, /* One of PASSIVE, FULL or RESTART */ |
| 49771 | | - int (*xBusyCall)(void*), /* Function to call when busy */ |
| 50107 | + int (*xBusy)(void*), /* Function to call when busy */ |
| 49772 | 50108 | void *pBusyArg, /* Context argument for xBusyHandler */ |
| 49773 | 50109 | int sync_flags, /* Flags for OsSync() (or 0) */ |
| 49774 | 50110 | u8 *zBuf /* Temporary buffer to use */ |
| 49775 | 50111 | ){ |
| 49776 | 50112 | int rc; /* Return code */ |
| | @@ -49780,11 +50116,10 @@ |
| 49780 | 50116 | u32 iFrame = 0; /* Wal frame containing data for iDbpage */ |
| 49781 | 50117 | u32 mxSafeFrame; /* Max frame that can be backfilled */ |
| 49782 | 50118 | u32 mxPage; /* Max database page to write */ |
| 49783 | 50119 | int i; /* Loop counter */ |
| 49784 | 50120 | volatile WalCkptInfo *pInfo; /* The checkpoint status information */ |
| 49785 | | - int (*xBusy)(void*) = 0; /* Function to call when waiting for locks */ |
| 49786 | 50121 | |
| 49787 | 50122 | szPage = walPagesize(pWal); |
| 49788 | 50123 | testcase( szPage<=32768 ); |
| 49789 | 50124 | testcase( szPage>=65536 ); |
| 49790 | 50125 | pInfo = walCkptInfo(pWal); |
| | @@ -49795,11 +50130,13 @@ |
| 49795 | 50130 | if( rc!=SQLITE_OK ){ |
| 49796 | 50131 | return rc; |
| 49797 | 50132 | } |
| 49798 | 50133 | assert( pIter ); |
| 49799 | 50134 | |
| 49800 | | - if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall; |
| 50135 | + /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked |
| 50136 | + ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ |
| 50137 | + assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); |
| 49801 | 50138 | |
| 49802 | 50139 | /* Compute in mxSafeFrame the index of the last frame of the WAL that is |
| 49803 | 50140 | ** safe to write into the database. Frames beyond mxSafeFrame might |
| 49804 | 50141 | ** overwrite database pages that are in use by active readers and thus |
| 49805 | 50142 | ** cannot be backfilled from the WAL. |
| | @@ -49884,23 +50221,42 @@ |
| 49884 | 50221 | /* Reset the return code so as not to report a checkpoint failure |
| 49885 | 50222 | ** just because there are active readers. */ |
| 49886 | 50223 | rc = SQLITE_OK; |
| 49887 | 50224 | } |
| 49888 | 50225 | |
| 49889 | | - /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal |
| 49890 | | - ** file has been copied into the database file, then block until all |
| 49891 | | - ** readers have finished using the wal file. This ensures that the next |
| 49892 | | - ** process to write to the database restarts the wal file. |
| 50226 | + /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the |
| 50227 | + ** entire wal file has been copied into the database file, then block |
| 50228 | + ** until all readers have finished using the wal file. This ensures that |
| 50229 | + ** the next process to write to the database restarts the wal file. |
| 49893 | 50230 | */ |
| 49894 | 50231 | if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){ |
| 49895 | 50232 | assert( pWal->writeLock ); |
| 49896 | 50233 | if( pInfo->nBackfill<pWal->hdr.mxFrame ){ |
| 49897 | 50234 | rc = SQLITE_BUSY; |
| 49898 | | - }else if( eMode==SQLITE_CHECKPOINT_RESTART ){ |
| 50235 | + }else if( eMode>=SQLITE_CHECKPOINT_RESTART ){ |
| 50236 | + u32 salt1; |
| 50237 | + sqlite3_randomness(4, &salt1); |
| 49899 | 50238 | assert( mxSafeFrame==pWal->hdr.mxFrame ); |
| 49900 | 50239 | rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1); |
| 49901 | 50240 | if( rc==SQLITE_OK ){ |
| 50241 | + if( eMode==SQLITE_CHECKPOINT_TRUNCATE ){ |
| 50242 | + /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as |
| 50243 | + ** SQLITE_CHECKPOINT_RESTART with the addition that it also |
| 50244 | + ** truncates the log file to zero bytes just prior to a |
| 50245 | + ** successful return. |
| 50246 | + ** |
| 50247 | + ** In theory, it might be safe to do this without updating the |
| 50248 | + ** wal-index header in shared memory, as all subsequent reader or |
| 50249 | + ** writer clients should see that the entire log file has been |
| 50250 | + ** checkpointed and behave accordingly. This seems unsafe though, |
| 50251 | + ** as it would leave the system in a state where the contents of |
| 50252 | + ** the wal-index header do not match the contents of the |
| 50253 | + ** file-system. To avoid this, update the wal-index header to |
| 50254 | + ** indicate that the log file contains zero valid frames. */ |
| 50255 | + walRestartHdr(pWal, salt1); |
| 50256 | + rc = sqlite3OsTruncate(pWal->pWalFd, 0); |
| 50257 | + } |
| 49902 | 50258 | walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); |
| 49903 | 50259 | } |
| 49904 | 50260 | } |
| 49905 | 50261 | } |
| 49906 | 50262 | |
| | @@ -50634,11 +50990,10 @@ |
| 50634 | 50990 | assert( walFramePgno(pWal, iFrame)!=1 ); |
| 50635 | 50991 | rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame)); |
| 50636 | 50992 | } |
| 50637 | 50993 | if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal); |
| 50638 | 50994 | } |
| 50639 | | - assert( rc==SQLITE_OK ); |
| 50640 | 50995 | return rc; |
| 50641 | 50996 | } |
| 50642 | 50997 | |
| 50643 | 50998 | /* |
| 50644 | 50999 | ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32 |
| | @@ -50683,11 +51038,10 @@ |
| 50683 | 51038 | } |
| 50684 | 51039 | |
| 50685 | 51040 | return rc; |
| 50686 | 51041 | } |
| 50687 | 51042 | |
| 50688 | | - |
| 50689 | 51043 | /* |
| 50690 | 51044 | ** This function is called just before writing a set of frames to the log |
| 50691 | 51045 | ** file (see sqlite3WalFrames()). It checks to see if, instead of appending |
| 50692 | 51046 | ** to the current log file, it is possible to overwrite the start of the |
| 50693 | 51047 | ** existing log file with the new frames (i.e. "reset" the log). If so, |
| | @@ -50716,24 +51070,12 @@ |
| 50716 | 51070 | ** wal-index header to reflect this. |
| 50717 | 51071 | ** |
| 50718 | 51072 | ** In theory it would be Ok to update the cache of the header only |
| 50719 | 51073 | ** at this point. But updating the actual wal-index header is also |
| 50720 | 51074 | ** safe and means there is no special case for sqlite3WalUndo() |
| 50721 | | - ** to handle if this transaction is rolled back. |
| 50722 | | - */ |
| 50723 | | - int i; /* Loop counter */ |
| 50724 | | - u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */ |
| 50725 | | - |
| 50726 | | - pWal->nCkpt++; |
| 50727 | | - pWal->hdr.mxFrame = 0; |
| 50728 | | - sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0])); |
| 50729 | | - aSalt[1] = salt1; |
| 50730 | | - walIndexWriteHdr(pWal); |
| 50731 | | - pInfo->nBackfill = 0; |
| 50732 | | - pInfo->aReadMark[1] = 0; |
| 50733 | | - for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
| 50734 | | - assert( pInfo->aReadMark[0]==0 ); |
| 51075 | + ** to handle if this transaction is rolled back. */ |
| 51076 | + walRestartHdr(pWal, salt1); |
| 50735 | 51077 | walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); |
| 50736 | 51078 | }else if( rc!=SQLITE_BUSY ){ |
| 50737 | 51079 | return rc; |
| 50738 | 51080 | } |
| 50739 | 51081 | } |
| | @@ -51017,11 +51359,11 @@ |
| 51017 | 51359 | ** If parameter xBusy is not NULL, it is a pointer to a busy-handler |
| 51018 | 51360 | ** callback. In this case this function runs a blocking checkpoint. |
| 51019 | 51361 | */ |
| 51020 | 51362 | SQLITE_PRIVATE int sqlite3WalCheckpoint( |
| 51021 | 51363 | Wal *pWal, /* Wal connection */ |
| 51022 | | - int eMode, /* PASSIVE, FULL or RESTART */ |
| 51364 | + int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */ |
| 51023 | 51365 | int (*xBusy)(void*), /* Function to call when busy */ |
| 51024 | 51366 | void *pBusyArg, /* Context argument for xBusyHandler */ |
| 51025 | 51367 | int sync_flags, /* Flags to sync db file with (or 0) */ |
| 51026 | 51368 | int nBuf, /* Size of temporary buffer */ |
| 51027 | 51369 | u8 *zBuf, /* Temporary buffer to use */ |
| | @@ -51029,40 +51371,54 @@ |
| 51029 | 51371 | int *pnCkpt /* OUT: Number of backfilled frames in WAL */ |
| 51030 | 51372 | ){ |
| 51031 | 51373 | int rc; /* Return code */ |
| 51032 | 51374 | int isChanged = 0; /* True if a new wal-index header is loaded */ |
| 51033 | 51375 | int eMode2 = eMode; /* Mode to pass to walCheckpoint() */ |
| 51376 | + int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */ |
| 51034 | 51377 | |
| 51035 | 51378 | assert( pWal->ckptLock==0 ); |
| 51036 | 51379 | assert( pWal->writeLock==0 ); |
| 51037 | 51380 | |
| 51381 | + /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked |
| 51382 | + ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ |
| 51383 | + assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); |
| 51384 | + |
| 51038 | 51385 | if( pWal->readOnly ) return SQLITE_READONLY; |
| 51039 | 51386 | WALTRACE(("WAL%p: checkpoint begins\n", pWal)); |
| 51387 | + |
| 51388 | + /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive |
| 51389 | + ** "checkpoint" lock on the database file. */ |
| 51040 | 51390 | rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); |
| 51041 | 51391 | if( rc ){ |
| 51042 | | - /* Usually this is SQLITE_BUSY meaning that another thread or process |
| 51043 | | - ** is already running a checkpoint, or maybe a recovery. But it might |
| 51044 | | - ** also be SQLITE_IOERR. */ |
| 51392 | + /* EVIDENCE-OF: R-10421-19736 If any other process is running a |
| 51393 | + ** checkpoint operation at the same time, the lock cannot be obtained and |
| 51394 | + ** SQLITE_BUSY is returned. |
| 51395 | + ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured, |
| 51396 | + ** it will not be invoked in this case. |
| 51397 | + */ |
| 51398 | + testcase( rc==SQLITE_BUSY ); |
| 51399 | + testcase( xBusy!=0 ); |
| 51045 | 51400 | return rc; |
| 51046 | 51401 | } |
| 51047 | 51402 | pWal->ckptLock = 1; |
| 51048 | 51403 | |
| 51049 | | - /* If this is a blocking-checkpoint, then obtain the write-lock as well |
| 51050 | | - ** to prevent any writers from running while the checkpoint is underway. |
| 51051 | | - ** This has to be done before the call to walIndexReadHdr() below. |
| 51404 | + /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and |
| 51405 | + ** TRUNCATE modes also obtain the exclusive "writer" lock on the database |
| 51406 | + ** file. |
| 51052 | 51407 | ** |
| 51053 | | - ** If the writer lock cannot be obtained, then a passive checkpoint is |
| 51054 | | - ** run instead. Since the checkpointer is not holding the writer lock, |
| 51055 | | - ** there is no point in blocking waiting for any readers. Assuming no |
| 51056 | | - ** other error occurs, this function will return SQLITE_BUSY to the caller. |
| 51408 | + ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained |
| 51409 | + ** immediately, and a busy-handler is configured, it is invoked and the |
| 51410 | + ** writer lock retried until either the busy-handler returns 0 or the |
| 51411 | + ** lock is successfully obtained. |
| 51057 | 51412 | */ |
| 51058 | 51413 | if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){ |
| 51059 | 51414 | rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1); |
| 51060 | 51415 | if( rc==SQLITE_OK ){ |
| 51061 | 51416 | pWal->writeLock = 1; |
| 51062 | 51417 | }else if( rc==SQLITE_BUSY ){ |
| 51063 | 51418 | eMode2 = SQLITE_CHECKPOINT_PASSIVE; |
| 51419 | + xBusy2 = 0; |
| 51064 | 51420 | rc = SQLITE_OK; |
| 51065 | 51421 | } |
| 51066 | 51422 | } |
| 51067 | 51423 | |
| 51068 | 51424 | /* Read the wal-index header. */ |
| | @@ -51076,11 +51432,11 @@ |
| 51076 | 51432 | /* Copy data from the log to the database file. */ |
| 51077 | 51433 | if( rc==SQLITE_OK ){ |
| 51078 | 51434 | if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){ |
| 51079 | 51435 | rc = SQLITE_CORRUPT_BKPT; |
| 51080 | 51436 | }else{ |
| 51081 | | - rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf); |
| 51437 | + rc = walCheckpoint(pWal, eMode2, xBusy2, pBusyArg, sync_flags, zBuf); |
| 51082 | 51438 | } |
| 51083 | 51439 | |
| 51084 | 51440 | /* If no error occurred, set the output variables. */ |
| 51085 | 51441 | if( rc==SQLITE_OK || rc==SQLITE_BUSY ){ |
| 51086 | 51442 | if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame; |
| | @@ -51713,10 +52069,15 @@ |
| 51713 | 52069 | ** but cursors cannot be shared. Each cursor is associated with a |
| 51714 | 52070 | ** particular database connection identified BtCursor.pBtree.db. |
| 51715 | 52071 | ** |
| 51716 | 52072 | ** Fields in this structure are accessed under the BtShared.mutex |
| 51717 | 52073 | ** found at self->pBt->mutex. |
| 52074 | +** |
| 52075 | +** skipNext meaning: |
| 52076 | +** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op. |
| 52077 | +** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op. |
| 52078 | +** eState==FAULT: Cursor fault with skipNext as error code. |
| 51718 | 52079 | */ |
| 51719 | 52080 | struct BtCursor { |
| 51720 | 52081 | Btree *pBtree; /* The Btree to which this cursor belongs */ |
| 51721 | 52082 | BtShared *pBt; /* The BtShared this cursor points to */ |
| 51722 | 52083 | BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ |
| | @@ -51725,11 +52086,12 @@ |
| 51725 | 52086 | CellInfo info; /* A parse of the cell we are pointing at */ |
| 51726 | 52087 | i64 nKey; /* Size of pKey, or last integer key */ |
| 51727 | 52088 | void *pKey; /* Saved key that was cursor last known position */ |
| 51728 | 52089 | Pgno pgnoRoot; /* The root page of this tree */ |
| 51729 | 52090 | int nOvflAlloc; /* Allocated size of aOverflow[] array */ |
| 51730 | | - int skipNext; /* Prev() is noop if negative. Next() is noop if positive */ |
| 52091 | + int skipNext; /* Prev() is noop if negative. Next() is noop if positive. |
| 52092 | + ** Error code if eState==CURSOR_FAULT */ |
| 51731 | 52093 | u8 curFlags; /* zero or more BTCF_* flags defined below */ |
| 51732 | 52094 | u8 eState; /* One of the CURSOR_XXX constants (see below) */ |
| 51733 | 52095 | u8 hints; /* As configured by CursorSetHints() */ |
| 51734 | 52096 | i16 iPage; /* Index of current page in apPage */ |
| 51735 | 52097 | u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */ |
| | @@ -51771,11 +52133,11 @@ |
| 51771 | 52133 | ** CURSOR_FAULT: |
| 51772 | 52134 | ** An unrecoverable error (an I/O error or a malloc failure) has occurred |
| 51773 | 52135 | ** on a different connection that shares the BtShared cache with this |
| 51774 | 52136 | ** cursor. The error has left the cache in an inconsistent state. |
| 51775 | 52137 | ** Do nothing else with this cursor. Any attempt to use the cursor |
| 51776 | | -** should return the error code stored in BtCursor.skip |
| 52138 | +** should return the error code stored in BtCursor.skipNext |
| 51777 | 52139 | */ |
| 51778 | 52140 | #define CURSOR_INVALID 0 |
| 51779 | 52141 | #define CURSOR_VALID 1 |
| 51780 | 52142 | #define CURSOR_SKIPNEXT 2 |
| 51781 | 52143 | #define CURSOR_REQUIRESEEK 3 |
| | @@ -53321,10 +53683,15 @@ |
| 53321 | 53683 | /* |
| 53322 | 53684 | ** Defragment the page given. All Cells are moved to the |
| 53323 | 53685 | ** end of the page and all free space is collected into one |
| 53324 | 53686 | ** big FreeBlk that occurs in between the header and cell |
| 53325 | 53687 | ** pointer array and the cell content area. |
| 53688 | +** |
| 53689 | +** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a |
| 53690 | +** b-tree page so that there are no freeblocks or fragment bytes, all |
| 53691 | +** unused bytes are contained in the unallocated space region, and all |
| 53692 | +** cells are packed tightly at the end of the page. |
| 53326 | 53693 | */ |
| 53327 | 53694 | static int defragmentPage(MemPage *pPage){ |
| 53328 | 53695 | int i; /* Loop counter */ |
| 53329 | 53696 | int pc; /* Address of the i-th cell */ |
| 53330 | 53697 | int hdr; /* Offset to the page header */ |
| | @@ -53333,28 +53700,27 @@ |
| 53333 | 53700 | int cellOffset; /* Offset to the cell pointer array */ |
| 53334 | 53701 | int cbrk; /* Offset to the cell content area */ |
| 53335 | 53702 | int nCell; /* Number of cells on the page */ |
| 53336 | 53703 | unsigned char *data; /* The page data */ |
| 53337 | 53704 | unsigned char *temp; /* Temp area for cell content */ |
| 53705 | + unsigned char *src; /* Source of content */ |
| 53338 | 53706 | int iCellFirst; /* First allowable cell index */ |
| 53339 | 53707 | int iCellLast; /* Last possible cell index */ |
| 53340 | 53708 | |
| 53341 | 53709 | |
| 53342 | 53710 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53343 | 53711 | assert( pPage->pBt!=0 ); |
| 53344 | 53712 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
| 53345 | 53713 | assert( pPage->nOverflow==0 ); |
| 53346 | 53714 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53347 | | - temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
| 53348 | | - data = pPage->aData; |
| 53715 | + temp = 0; |
| 53716 | + src = data = pPage->aData; |
| 53349 | 53717 | hdr = pPage->hdrOffset; |
| 53350 | 53718 | cellOffset = pPage->cellOffset; |
| 53351 | 53719 | nCell = pPage->nCell; |
| 53352 | 53720 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 53353 | 53721 | usableSize = pPage->pBt->usableSize; |
| 53354 | | - cbrk = get2byte(&data[hdr+5]); |
| 53355 | | - memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); |
| 53356 | 53722 | cbrk = usableSize; |
| 53357 | 53723 | iCellFirst = cellOffset + 2*nCell; |
| 53358 | 53724 | iCellLast = usableSize - 4; |
| 53359 | 53725 | for(i=0; i<nCell; i++){ |
| 53360 | 53726 | u8 *pAddr; /* The i-th cell pointer */ |
| | @@ -53369,11 +53735,11 @@ |
| 53369 | 53735 | if( pc<iCellFirst || pc>iCellLast ){ |
| 53370 | 53736 | return SQLITE_CORRUPT_BKPT; |
| 53371 | 53737 | } |
| 53372 | 53738 | #endif |
| 53373 | 53739 | assert( pc>=iCellFirst && pc<=iCellLast ); |
| 53374 | | - size = cellSizePtr(pPage, &temp[pc]); |
| 53740 | + size = cellSizePtr(pPage, &src[pc]); |
| 53375 | 53741 | cbrk -= size; |
| 53376 | 53742 | #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 53377 | 53743 | if( cbrk<iCellFirst ){ |
| 53378 | 53744 | return SQLITE_CORRUPT_BKPT; |
| 53379 | 53745 | } |
| | @@ -53383,12 +53749,20 @@ |
| 53383 | 53749 | } |
| 53384 | 53750 | #endif |
| 53385 | 53751 | assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); |
| 53386 | 53752 | testcase( cbrk+size==usableSize ); |
| 53387 | 53753 | testcase( pc+size==usableSize ); |
| 53388 | | - memcpy(&data[cbrk], &temp[pc], size); |
| 53389 | 53754 | put2byte(pAddr, cbrk); |
| 53755 | + if( temp==0 ){ |
| 53756 | + int x; |
| 53757 | + if( cbrk==pc ) continue; |
| 53758 | + temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
| 53759 | + x = get2byte(&data[hdr+5]); |
| 53760 | + memcpy(&temp[x], &data[x], (cbrk+size) - x); |
| 53761 | + src = temp; |
| 53762 | + } |
| 53763 | + memcpy(&data[cbrk], &src[pc], size); |
| 53390 | 53764 | } |
| 53391 | 53765 | assert( cbrk>=iCellFirst ); |
| 53392 | 53766 | put2byte(&data[hdr+5], cbrk); |
| 53393 | 53767 | data[hdr+1] = 0; |
| 53394 | 53768 | data[hdr+2] = 0; |
| | @@ -53398,10 +53772,73 @@ |
| 53398 | 53772 | if( cbrk-iCellFirst!=pPage->nFree ){ |
| 53399 | 53773 | return SQLITE_CORRUPT_BKPT; |
| 53400 | 53774 | } |
| 53401 | 53775 | return SQLITE_OK; |
| 53402 | 53776 | } |
| 53777 | + |
| 53778 | +/* |
| 53779 | +** Search the free-list on page pPg for space to store a cell nByte bytes in |
| 53780 | +** size. If one can be found, return a pointer to the space and remove it |
| 53781 | +** from the free-list. |
| 53782 | +** |
| 53783 | +** If no suitable space can be found on the free-list, return NULL. |
| 53784 | +** |
| 53785 | +** This function may detect corruption within pPg. If corruption is |
| 53786 | +** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned. |
| 53787 | +** |
| 53788 | +** If a slot of at least nByte bytes is found but cannot be used because |
| 53789 | +** there are already at least 60 fragmented bytes on the page, return NULL. |
| 53790 | +** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true. |
| 53791 | +*/ |
| 53792 | +static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){ |
| 53793 | + const int hdr = pPg->hdrOffset; |
| 53794 | + u8 * const aData = pPg->aData; |
| 53795 | + int iAddr; |
| 53796 | + int pc; |
| 53797 | + int usableSize = pPg->pBt->usableSize; |
| 53798 | + |
| 53799 | + for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){ |
| 53800 | + int size; /* Size of the free slot */ |
| 53801 | + /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of |
| 53802 | + ** increasing offset. */ |
| 53803 | + if( pc>usableSize-4 || pc<iAddr+4 ){ |
| 53804 | + *pRc = SQLITE_CORRUPT_BKPT; |
| 53805 | + return 0; |
| 53806 | + } |
| 53807 | + /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each |
| 53808 | + ** freeblock form a big-endian integer which is the size of the freeblock |
| 53809 | + ** in bytes, including the 4-byte header. */ |
| 53810 | + size = get2byte(&aData[pc+2]); |
| 53811 | + if( size>=nByte ){ |
| 53812 | + int x = size - nByte; |
| 53813 | + testcase( x==4 ); |
| 53814 | + testcase( x==3 ); |
| 53815 | + if( x<4 ){ |
| 53816 | + /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total |
| 53817 | + ** number of bytes in fragments may not exceed 60. */ |
| 53818 | + if( aData[hdr+7]>=60 ){ |
| 53819 | + if( pbDefrag ) *pbDefrag = 1; |
| 53820 | + return 0; |
| 53821 | + } |
| 53822 | + /* Remove the slot from the free-list. Update the number of |
| 53823 | + ** fragmented bytes within the page. */ |
| 53824 | + memcpy(&aData[iAddr], &aData[pc], 2); |
| 53825 | + aData[hdr+7] += (u8)x; |
| 53826 | + }else if( size+pc > usableSize ){ |
| 53827 | + *pRc = SQLITE_CORRUPT_BKPT; |
| 53828 | + return 0; |
| 53829 | + }else{ |
| 53830 | + /* The slot remains on the free-list. Reduce its size to account |
| 53831 | + ** for the portion used by the new allocation. */ |
| 53832 | + put2byte(&aData[pc+2], x); |
| 53833 | + } |
| 53834 | + return &aData[pc + x]; |
| 53835 | + } |
| 53836 | + } |
| 53837 | + |
| 53838 | + return 0; |
| 53839 | +} |
| 53403 | 53840 | |
| 53404 | 53841 | /* |
| 53405 | 53842 | ** Allocate nByte bytes of space from within the B-Tree page passed |
| 53406 | 53843 | ** as the first argument. Write into *pIdx the index into pPage->aData[] |
| 53407 | 53844 | ** of the first byte of allocated space. Return either SQLITE_OK or |
| | @@ -53416,80 +53853,58 @@ |
| 53416 | 53853 | */ |
| 53417 | 53854 | static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
| 53418 | 53855 | const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ |
| 53419 | 53856 | u8 * const data = pPage->aData; /* Local cache of pPage->aData */ |
| 53420 | 53857 | int top; /* First byte of cell content area */ |
| 53858 | + int rc = SQLITE_OK; /* Integer return code */ |
| 53421 | 53859 | int gap; /* First byte of gap between cell pointers and cell content */ |
| 53422 | | - int rc; /* Integer return code */ |
| 53423 | | - int usableSize; /* Usable size of the page */ |
| 53424 | 53860 | |
| 53425 | 53861 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53426 | 53862 | assert( pPage->pBt ); |
| 53427 | 53863 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53428 | 53864 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 53429 | 53865 | assert( pPage->nFree>=nByte ); |
| 53430 | 53866 | assert( pPage->nOverflow==0 ); |
| 53431 | | - usableSize = pPage->pBt->usableSize; |
| 53432 | | - assert( nByte < usableSize-8 ); |
| 53867 | + assert( nByte < (int)(pPage->pBt->usableSize-8) ); |
| 53433 | 53868 | |
| 53434 | 53869 | assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); |
| 53435 | 53870 | gap = pPage->cellOffset + 2*pPage->nCell; |
| 53436 | 53871 | assert( gap<=65536 ); |
| 53437 | | - top = get2byte(&data[hdr+5]); |
| 53438 | | - if( gap>top ){ |
| 53439 | | - if( top==0 ){ |
| 53440 | | - top = 65536; |
| 53441 | | - }else{ |
| 53442 | | - return SQLITE_CORRUPT_BKPT; |
| 53443 | | - } |
| 53444 | | - } |
| 53872 | + /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size |
| 53873 | + ** and the reserved space is zero (the usual value for reserved space) |
| 53874 | + ** then the cell content offset of an empty page wants to be 65536. |
| 53875 | + ** However, that integer is too large to be stored in a 2-byte unsigned |
| 53876 | + ** integer, so a value of 0 is used in its place. */ |
| 53877 | + top = get2byteNotZero(&data[hdr+5]); |
| 53878 | + if( gap>top ) return SQLITE_CORRUPT_BKPT; |
| 53445 | 53879 | |
| 53446 | 53880 | /* If there is enough space between gap and top for one more cell pointer |
| 53447 | 53881 | ** array entry offset, and if the freelist is not empty, then search the |
| 53448 | 53882 | ** freelist looking for a free slot big enough to satisfy the request. |
| 53449 | 53883 | */ |
| 53450 | 53884 | testcase( gap+2==top ); |
| 53451 | 53885 | testcase( gap+1==top ); |
| 53452 | 53886 | testcase( gap==top ); |
| 53453 | 53887 | if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){ |
| 53454 | | - int pc, addr; |
| 53455 | | - for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){ |
| 53456 | | - int size; /* Size of the free slot */ |
| 53457 | | - if( pc>usableSize-4 || pc<addr+4 ){ |
| 53458 | | - return SQLITE_CORRUPT_BKPT; |
| 53459 | | - } |
| 53460 | | - size = get2byte(&data[pc+2]); |
| 53461 | | - if( size>=nByte ){ |
| 53462 | | - int x = size - nByte; |
| 53463 | | - testcase( x==4 ); |
| 53464 | | - testcase( x==3 ); |
| 53465 | | - if( x<4 ){ |
| 53466 | | - if( data[hdr+7]>=60 ) goto defragment_page; |
| 53467 | | - /* Remove the slot from the free-list. Update the number of |
| 53468 | | - ** fragmented bytes within the page. */ |
| 53469 | | - memcpy(&data[addr], &data[pc], 2); |
| 53470 | | - data[hdr+7] += (u8)x; |
| 53471 | | - }else if( size+pc > usableSize ){ |
| 53472 | | - return SQLITE_CORRUPT_BKPT; |
| 53473 | | - }else{ |
| 53474 | | - /* The slot remains on the free-list. Reduce its size to account |
| 53475 | | - ** for the portion used by the new allocation. */ |
| 53476 | | - put2byte(&data[pc+2], x); |
| 53477 | | - } |
| 53478 | | - *pIdx = pc + x; |
| 53479 | | - return SQLITE_OK; |
| 53480 | | - } |
| 53888 | + int bDefrag = 0; |
| 53889 | + u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag); |
| 53890 | + if( rc ) return rc; |
| 53891 | + if( bDefrag ) goto defragment_page; |
| 53892 | + if( pSpace ){ |
| 53893 | + assert( pSpace>=data && (pSpace - data)<65536 ); |
| 53894 | + *pIdx = (int)(pSpace - data); |
| 53895 | + return SQLITE_OK; |
| 53481 | 53896 | } |
| 53482 | 53897 | } |
| 53483 | 53898 | |
| 53484 | 53899 | /* The request could not be fulfilled using a freelist slot. Check |
| 53485 | 53900 | ** to see if defragmentation is necessary. |
| 53486 | 53901 | */ |
| 53487 | 53902 | testcase( gap+2+nByte==top ); |
| 53488 | 53903 | if( gap+2+nByte>top ){ |
| 53489 | | -defragment_page: |
| 53490 | | - testcase( pPage->nCell==0 ); |
| 53904 | + defragment_page: |
| 53905 | + assert( pPage->nCell>0 || CORRUPT_DB ); |
| 53491 | 53906 | rc = defragmentPage(pPage); |
| 53492 | 53907 | if( rc ) return rc; |
| 53493 | 53908 | top = get2byteNotZero(&data[hdr+5]); |
| 53494 | 53909 | assert( gap+nByte<=top ); |
| 53495 | 53910 | } |
| | @@ -53532,11 +53947,11 @@ |
| 53532 | 53947 | unsigned char *data = pPage->aData; /* Page content */ |
| 53533 | 53948 | |
| 53534 | 53949 | assert( pPage->pBt!=0 ); |
| 53535 | 53950 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 53536 | 53951 | assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
| 53537 | | - assert( iEnd <= pPage->pBt->usableSize ); |
| 53952 | + assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); |
| 53538 | 53953 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53539 | 53954 | assert( iSize>=4 ); /* Minimum cell size is 4 */ |
| 53540 | 53955 | assert( iStart<=iLast ); |
| 53541 | 53956 | |
| 53542 | 53957 | /* Overwrite deleted information with zeros when the secure_delete |
| | @@ -53627,22 +54042,36 @@ |
| 53627 | 54042 | pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
| 53628 | 54043 | flagByte &= ~PTF_LEAF; |
| 53629 | 54044 | pPage->childPtrSize = 4-4*pPage->leaf; |
| 53630 | 54045 | pBt = pPage->pBt; |
| 53631 | 54046 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 54047 | + /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior |
| 54048 | + ** table b-tree page. */ |
| 54049 | + assert( (PTF_LEAFDATA|PTF_INTKEY)==5 ); |
| 54050 | + /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf |
| 54051 | + ** table b-tree page. */ |
| 54052 | + assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 ); |
| 53632 | 54053 | pPage->intKey = 1; |
| 53633 | 54054 | pPage->intKeyLeaf = pPage->leaf; |
| 53634 | 54055 | pPage->noPayload = !pPage->leaf; |
| 53635 | 54056 | pPage->maxLocal = pBt->maxLeaf; |
| 53636 | 54057 | pPage->minLocal = pBt->minLeaf; |
| 53637 | 54058 | }else if( flagByte==PTF_ZERODATA ){ |
| 54059 | + /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior |
| 54060 | + ** index b-tree page. */ |
| 54061 | + assert( (PTF_ZERODATA)==2 ); |
| 54062 | + /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf |
| 54063 | + ** index b-tree page. */ |
| 54064 | + assert( (PTF_ZERODATA|PTF_LEAF)==10 ); |
| 53638 | 54065 | pPage->intKey = 0; |
| 53639 | 54066 | pPage->intKeyLeaf = 0; |
| 53640 | 54067 | pPage->noPayload = 0; |
| 53641 | 54068 | pPage->maxLocal = pBt->maxLocal; |
| 53642 | 54069 | pPage->minLocal = pBt->minLocal; |
| 53643 | 54070 | }else{ |
| 54071 | + /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is |
| 54072 | + ** an error. */ |
| 53644 | 54073 | return SQLITE_CORRUPT_BKPT; |
| 53645 | 54074 | } |
| 53646 | 54075 | pPage->max1bytePayload = pBt->max1bytePayload; |
| 53647 | 54076 | return SQLITE_OK; |
| 53648 | 54077 | } |
| | @@ -53678,25 +54107,37 @@ |
| 53678 | 54107 | |
| 53679 | 54108 | pBt = pPage->pBt; |
| 53680 | 54109 | |
| 53681 | 54110 | hdr = pPage->hdrOffset; |
| 53682 | 54111 | data = pPage->aData; |
| 54112 | + /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating |
| 54113 | + ** the b-tree page type. */ |
| 53683 | 54114 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
| 53684 | 54115 | assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 53685 | 54116 | pPage->maskPage = (u16)(pBt->pageSize - 1); |
| 53686 | 54117 | pPage->nOverflow = 0; |
| 53687 | 54118 | usableSize = pBt->usableSize; |
| 53688 | | - pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 54119 | + pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize; |
| 53689 | 54120 | pPage->aDataEnd = &data[usableSize]; |
| 53690 | 54121 | pPage->aCellIdx = &data[cellOffset]; |
| 54122 | + /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates |
| 54123 | + ** the start of the cell content area. A zero value for this integer is |
| 54124 | + ** interpreted as 65536. */ |
| 53691 | 54125 | top = get2byteNotZero(&data[hdr+5]); |
| 54126 | + /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
| 54127 | + ** number of cells on the page. */ |
| 53692 | 54128 | pPage->nCell = get2byte(&data[hdr+3]); |
| 53693 | 54129 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 53694 | 54130 | /* To many cells for a single page. The page must be corrupt */ |
| 53695 | 54131 | return SQLITE_CORRUPT_BKPT; |
| 53696 | 54132 | } |
| 53697 | 54133 | testcase( pPage->nCell==MX_CELL(pBt) ); |
| 54134 | + /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only |
| 54135 | + ** possible for a root page of a table that contains no rows) then the |
| 54136 | + ** offset to the cell content area will equal the page size minus the |
| 54137 | + ** bytes of reserved space. */ |
| 54138 | + assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB ); |
| 53698 | 54139 | |
| 53699 | 54140 | /* A malformed database page might cause us to read past the end |
| 53700 | 54141 | ** of page when parsing a cell. |
| 53701 | 54142 | ** |
| 53702 | 54143 | ** The following block of code checks early to see if a cell extends |
| | @@ -53726,17 +54167,24 @@ |
| 53726 | 54167 | } |
| 53727 | 54168 | if( !pPage->leaf ) iCellLast++; |
| 53728 | 54169 | } |
| 53729 | 54170 | #endif |
| 53730 | 54171 | |
| 53731 | | - /* Compute the total free space on the page */ |
| 54172 | + /* Compute the total free space on the page |
| 54173 | + ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the |
| 54174 | + ** start of the first freeblock on the page, or is zero if there are no |
| 54175 | + ** freeblocks. */ |
| 53732 | 54176 | pc = get2byte(&data[hdr+1]); |
| 53733 | | - nFree = data[hdr+7] + top; |
| 54177 | + nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */ |
| 53734 | 54178 | while( pc>0 ){ |
| 53735 | 54179 | u16 next, size; |
| 53736 | 54180 | if( pc<iCellFirst || pc>iCellLast ){ |
| 53737 | | - /* Start of free block is off the page */ |
| 54181 | + /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will |
| 54182 | + ** always be at least one cell before the first freeblock. |
| 54183 | + ** |
| 54184 | + ** Or, the freeblock is off the end of the page |
| 54185 | + */ |
| 53738 | 54186 | return SQLITE_CORRUPT_BKPT; |
| 53739 | 54187 | } |
| 53740 | 54188 | next = get2byte(&data[pc]); |
| 53741 | 54189 | size = get2byte(&data[pc+2]); |
| 53742 | 54190 | if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){ |
| | @@ -54138,10 +54586,13 @@ |
| 54138 | 54586 | pBt->pPage1 = 0; |
| 54139 | 54587 | if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY; |
| 54140 | 54588 | #ifdef SQLITE_SECURE_DELETE |
| 54141 | 54589 | pBt->btsFlags |= BTS_SECURE_DELETE; |
| 54142 | 54590 | #endif |
| 54591 | + /* EVIDENCE-OF: R-51873-39618 The page size for a database file is |
| 54592 | + ** determined by the 2-byte integer located at an offset of 16 bytes from |
| 54593 | + ** the beginning of the database file. */ |
| 54143 | 54594 | pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); |
| 54144 | 54595 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 54145 | 54596 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
| 54146 | 54597 | pBt->pageSize = 0; |
| 54147 | 54598 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| | @@ -54156,10 +54607,13 @@ |
| 54156 | 54607 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 54157 | 54608 | } |
| 54158 | 54609 | #endif |
| 54159 | 54610 | nReserve = 0; |
| 54160 | 54611 | }else{ |
| 54612 | + /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is |
| 54613 | + ** determined by the one-byte unsigned integer found at an offset of 20 |
| 54614 | + ** into the database file header. */ |
| 54161 | 54615 | nReserve = zDbHeader[20]; |
| 54162 | 54616 | pBt->btsFlags |= BTS_PAGESIZE_FIXED; |
| 54163 | 54617 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 54164 | 54618 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 54165 | 54619 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| | @@ -54353,11 +54807,11 @@ |
| 54353 | 54807 | |
| 54354 | 54808 | /* Rollback any active transaction and free the handle structure. |
| 54355 | 54809 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 54356 | 54810 | ** this handle. |
| 54357 | 54811 | */ |
| 54358 | | - sqlite3BtreeRollback(p, SQLITE_OK); |
| 54812 | + sqlite3BtreeRollback(p, SQLITE_OK, 0); |
| 54359 | 54813 | sqlite3BtreeLeave(p); |
| 54360 | 54814 | |
| 54361 | 54815 | /* If there are still other outstanding references to the shared-btree |
| 54362 | 54816 | ** structure, return now. The remainder of this procedure cleans |
| 54363 | 54817 | ** up the shared-btree. |
| | @@ -54665,10 +55119,13 @@ |
| 54665 | 55119 | if( nPage>0 ){ |
| 54666 | 55120 | u32 pageSize; |
| 54667 | 55121 | u32 usableSize; |
| 54668 | 55122 | u8 *page1 = pPage1->aData; |
| 54669 | 55123 | rc = SQLITE_NOTADB; |
| 55124 | + /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins |
| 55125 | + ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d |
| 55126 | + ** 61 74 20 33 00. */ |
| 54670 | 55127 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
| 54671 | 55128 | goto page1_init_failed; |
| 54672 | 55129 | } |
| 54673 | 55130 | |
| 54674 | 55131 | #ifdef SQLITE_OMIT_WAL |
| | @@ -54705,26 +55162,39 @@ |
| 54705 | 55162 | } |
| 54706 | 55163 | rc = SQLITE_NOTADB; |
| 54707 | 55164 | } |
| 54708 | 55165 | #endif |
| 54709 | 55166 | |
| 54710 | | - /* The maximum embedded fraction must be exactly 25%. And the minimum |
| 54711 | | - ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. |
| 55167 | + /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload |
| 55168 | + ** fractions and the leaf payload fraction values must be 64, 32, and 32. |
| 55169 | + ** |
| 54712 | 55170 | ** The original design allowed these amounts to vary, but as of |
| 54713 | 55171 | ** version 3.6.0, we require them to be fixed. |
| 54714 | 55172 | */ |
| 54715 | 55173 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 54716 | 55174 | goto page1_init_failed; |
| 54717 | 55175 | } |
| 55176 | + /* EVIDENCE-OF: R-51873-39618 The page size for a database file is |
| 55177 | + ** determined by the 2-byte integer located at an offset of 16 bytes from |
| 55178 | + ** the beginning of the database file. */ |
| 54718 | 55179 | pageSize = (page1[16]<<8) | (page1[17]<<16); |
| 55180 | + /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two |
| 55181 | + ** between 512 and 65536 inclusive. */ |
| 54719 | 55182 | if( ((pageSize-1)&pageSize)!=0 |
| 54720 | 55183 | || pageSize>SQLITE_MAX_PAGE_SIZE |
| 54721 | 55184 | || pageSize<=256 |
| 54722 | 55185 | ){ |
| 54723 | 55186 | goto page1_init_failed; |
| 54724 | 55187 | } |
| 54725 | 55188 | assert( (pageSize & 7)==0 ); |
| 55189 | + /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte |
| 55190 | + ** integer at offset 20 is the number of bytes of space at the end of |
| 55191 | + ** each page to reserve for extensions. |
| 55192 | + ** |
| 55193 | + ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is |
| 55194 | + ** determined by the one-byte unsigned integer found at an offset of 20 |
| 55195 | + ** into the database file header. */ |
| 54726 | 55196 | usableSize = pageSize - page1[20]; |
| 54727 | 55197 | if( (u32)pageSize!=pBt->pageSize ){ |
| 54728 | 55198 | /* After reading the first page of the database assuming a page size |
| 54729 | 55199 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 54730 | 55200 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| | @@ -54741,10 +55211,13 @@ |
| 54741 | 55211 | } |
| 54742 | 55212 | if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){ |
| 54743 | 55213 | rc = SQLITE_CORRUPT_BKPT; |
| 54744 | 55214 | goto page1_init_failed; |
| 54745 | 55215 | } |
| 55216 | + /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to |
| 55217 | + ** be less than 480. In other words, if the page size is 512, then the |
| 55218 | + ** reserved space size cannot exceed 32. */ |
| 54746 | 55219 | if( usableSize<480 ){ |
| 54747 | 55220 | goto page1_init_failed; |
| 54748 | 55221 | } |
| 54749 | 55222 | pBt->pageSize = pageSize; |
| 54750 | 55223 | pBt->usableSize = usableSize; |
| | @@ -55646,64 +56119,95 @@ |
| 55646 | 56119 | return rc; |
| 55647 | 56120 | } |
| 55648 | 56121 | |
| 55649 | 56122 | /* |
| 55650 | 56123 | ** This routine sets the state to CURSOR_FAULT and the error |
| 55651 | | -** code to errCode for every cursor on BtShared that pBtree |
| 55652 | | -** references. |
| 55653 | | -** |
| 55654 | | -** Every cursor is tripped, including cursors that belong |
| 55655 | | -** to other database connections that happen to be sharing |
| 55656 | | -** the cache with pBtree. |
| 55657 | | -** |
| 55658 | | -** This routine gets called when a rollback occurs. |
| 55659 | | -** All cursors using the same cache must be tripped |
| 55660 | | -** to prevent them from trying to use the btree after |
| 55661 | | -** the rollback. The rollback may have deleted tables |
| 55662 | | -** or moved root pages, so it is not sufficient to |
| 55663 | | -** save the state of the cursor. The cursor must be |
| 55664 | | -** invalidated. |
| 55665 | | -*/ |
| 55666 | | -SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 56124 | +** code to errCode for every cursor on any BtShared that pBtree |
| 56125 | +** references. Or if the writeOnly flag is set to 1, then only |
| 56126 | +** trip write cursors and leave read cursors unchanged. |
| 56127 | +** |
| 56128 | +** Every cursor is a candidate to be tripped, including cursors |
| 56129 | +** that belong to other database connections that happen to be |
| 56130 | +** sharing the cache with pBtree. |
| 56131 | +** |
| 56132 | +** This routine gets called when a rollback occurs. If the writeOnly |
| 56133 | +** flag is true, then only write-cursors need be tripped - read-only |
| 56134 | +** cursors save their current positions so that they may continue |
| 56135 | +** following the rollback. Or, if writeOnly is false, all cursors are |
| 56136 | +** tripped. In general, writeOnly is false if the transaction being |
| 56137 | +** rolled back modified the database schema. In this case b-tree root |
| 56138 | +** pages may be moved or deleted from the database altogether, making |
| 56139 | +** it unsafe for read cursors to continue. |
| 56140 | +** |
| 56141 | +** If the writeOnly flag is true and an error is encountered while |
| 56142 | +** saving the current position of a read-only cursor, all cursors, |
| 56143 | +** including all read-cursors are tripped. |
| 56144 | +** |
| 56145 | +** SQLITE_OK is returned if successful, or if an error occurs while |
| 56146 | +** saving a cursor position, an SQLite error code. |
| 56147 | +*/ |
| 56148 | +SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){ |
| 55667 | 56149 | BtCursor *p; |
| 55668 | | - if( pBtree==0 ) return; |
| 55669 | | - sqlite3BtreeEnter(pBtree); |
| 55670 | | - for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 55671 | | - int i; |
| 55672 | | - sqlite3BtreeClearCursor(p); |
| 55673 | | - p->eState = CURSOR_FAULT; |
| 55674 | | - p->skipNext = errCode; |
| 55675 | | - for(i=0; i<=p->iPage; i++){ |
| 55676 | | - releasePage(p->apPage[i]); |
| 55677 | | - p->apPage[i] = 0; |
| 55678 | | - } |
| 55679 | | - } |
| 55680 | | - sqlite3BtreeLeave(pBtree); |
| 56150 | + int rc = SQLITE_OK; |
| 56151 | + |
| 56152 | + assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 ); |
| 56153 | + if( pBtree ){ |
| 56154 | + sqlite3BtreeEnter(pBtree); |
| 56155 | + for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 56156 | + int i; |
| 56157 | + if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){ |
| 56158 | + if( p->eState==CURSOR_VALID ){ |
| 56159 | + rc = saveCursorPosition(p); |
| 56160 | + if( rc!=SQLITE_OK ){ |
| 56161 | + (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0); |
| 56162 | + break; |
| 56163 | + } |
| 56164 | + } |
| 56165 | + }else{ |
| 56166 | + sqlite3BtreeClearCursor(p); |
| 56167 | + p->eState = CURSOR_FAULT; |
| 56168 | + p->skipNext = errCode; |
| 56169 | + } |
| 56170 | + for(i=0; i<=p->iPage; i++){ |
| 56171 | + releasePage(p->apPage[i]); |
| 56172 | + p->apPage[i] = 0; |
| 56173 | + } |
| 56174 | + } |
| 56175 | + sqlite3BtreeLeave(pBtree); |
| 56176 | + } |
| 56177 | + return rc; |
| 55681 | 56178 | } |
| 55682 | 56179 | |
| 55683 | 56180 | /* |
| 55684 | | -** Rollback the transaction in progress. All cursors will be |
| 55685 | | -** invalided by this operation. Any attempt to use a cursor |
| 55686 | | -** that was open at the beginning of this operation will result |
| 55687 | | -** in an error. |
| 56181 | +** Rollback the transaction in progress. |
| 56182 | +** |
| 56183 | +** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped). |
| 56184 | +** Only write cursors are tripped if writeOnly is true but all cursors are |
| 56185 | +** tripped if writeOnly is false. Any attempt to use |
| 56186 | +** a tripped cursor will result in an error. |
| 55688 | 56187 | ** |
| 55689 | 56188 | ** This will release the write lock on the database file. If there |
| 55690 | 56189 | ** are no active cursors, it also releases the read lock. |
| 55691 | 56190 | */ |
| 55692 | | -SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode){ |
| 56191 | +SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){ |
| 55693 | 56192 | int rc; |
| 55694 | 56193 | BtShared *pBt = p->pBt; |
| 55695 | 56194 | MemPage *pPage1; |
| 55696 | 56195 | |
| 56196 | + assert( writeOnly==1 || writeOnly==0 ); |
| 56197 | + assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK ); |
| 55697 | 56198 | sqlite3BtreeEnter(p); |
| 55698 | 56199 | if( tripCode==SQLITE_OK ){ |
| 55699 | 56200 | rc = tripCode = saveAllCursors(pBt, 0, 0); |
| 56201 | + if( rc ) writeOnly = 0; |
| 55700 | 56202 | }else{ |
| 55701 | 56203 | rc = SQLITE_OK; |
| 55702 | 56204 | } |
| 55703 | 56205 | if( tripCode ){ |
| 55704 | | - sqlite3BtreeTripAllCursors(p, tripCode); |
| 56206 | + int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly); |
| 56207 | + assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) ); |
| 56208 | + if( rc2!=SQLITE_OK ) rc = rc2; |
| 55705 | 56209 | } |
| 55706 | 56210 | btreeIntegrity(p); |
| 55707 | 56211 | |
| 55708 | 56212 | if( p->inTrans==TRANS_WRITE ){ |
| 55709 | 56213 | int rc2; |
| | @@ -56034,17 +56538,13 @@ |
| 56034 | 56538 | ** |
| 56035 | 56539 | ** This routine cannot fail. It always returns SQLITE_OK. |
| 56036 | 56540 | */ |
| 56037 | 56541 | SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
| 56038 | 56542 | assert( cursorHoldsMutex(pCur) ); |
| 56039 | | - assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 56040 | | - if( pCur->eState!=CURSOR_VALID ){ |
| 56041 | | - *pSize = 0; |
| 56042 | | - }else{ |
| 56043 | | - getCellInfo(pCur); |
| 56044 | | - *pSize = pCur->info.nKey; |
| 56045 | | - } |
| 56543 | + assert( pCur->eState==CURSOR_VALID ); |
| 56544 | + getCellInfo(pCur); |
| 56545 | + *pSize = pCur->info.nKey; |
| 56046 | 56546 | return SQLITE_OK; |
| 56047 | 56547 | } |
| 56048 | 56548 | |
| 56049 | 56549 | /* |
| 56050 | 56550 | ** Set *pSize to the number of bytes of data in the entry the |
| | @@ -57291,10 +57791,12 @@ |
| 57291 | 57791 | |
| 57292 | 57792 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 57293 | 57793 | assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) ); |
| 57294 | 57794 | pPage1 = pBt->pPage1; |
| 57295 | 57795 | mxPage = btreePagecount(pBt); |
| 57796 | + /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36 |
| 57797 | + ** stores stores the total number of pages on the freelist. */ |
| 57296 | 57798 | n = get4byte(&pPage1->aData[36]); |
| 57297 | 57799 | testcase( n==mxPage-1 ); |
| 57298 | 57800 | if( n>=mxPage ){ |
| 57299 | 57801 | return SQLITE_CORRUPT_BKPT; |
| 57300 | 57802 | } |
| | @@ -57337,12 +57839,18 @@ |
| 57337 | 57839 | ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT) |
| 57338 | 57840 | */ |
| 57339 | 57841 | do { |
| 57340 | 57842 | pPrevTrunk = pTrunk; |
| 57341 | 57843 | if( pPrevTrunk ){ |
| 57844 | + /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page |
| 57845 | + ** is the page number of the next freelist trunk page in the list or |
| 57846 | + ** zero if this is the last freelist trunk page. */ |
| 57342 | 57847 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
| 57343 | 57848 | }else{ |
| 57849 | + /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32 |
| 57850 | + ** stores the page number of the first page of the freelist, or zero if |
| 57851 | + ** the freelist is empty. */ |
| 57344 | 57852 | iTrunk = get4byte(&pPage1->aData[32]); |
| 57345 | 57853 | } |
| 57346 | 57854 | testcase( iTrunk==mxPage ); |
| 57347 | 57855 | if( iTrunk>mxPage ){ |
| 57348 | 57856 | rc = SQLITE_CORRUPT_BKPT; |
| | @@ -57353,12 +57861,13 @@ |
| 57353 | 57861 | pTrunk = 0; |
| 57354 | 57862 | goto end_allocate_page; |
| 57355 | 57863 | } |
| 57356 | 57864 | assert( pTrunk!=0 ); |
| 57357 | 57865 | assert( pTrunk->aData!=0 ); |
| 57358 | | - |
| 57359 | | - k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */ |
| 57866 | + /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page |
| 57867 | + ** is the number of leaf page pointers to follow. */ |
| 57868 | + k = get4byte(&pTrunk->aData[4]); |
| 57360 | 57869 | if( k==0 && !searchList ){ |
| 57361 | 57870 | /* The trunk has no leaves and the list is not being searched. |
| 57362 | 57871 | ** So extract the trunk page itself and use it as the newly |
| 57363 | 57872 | ** allocated page */ |
| 57364 | 57873 | assert( pPrevTrunk==0 ); |
| | @@ -57672,10 +58181,15 @@ |
| 57672 | 58181 | ** to maintain backwards compatibility with older versions of SQLite, |
| 57673 | 58182 | ** we will continue to restrict the number of entries to usableSize/4 - 8 |
| 57674 | 58183 | ** for now. At some point in the future (once everyone has upgraded |
| 57675 | 58184 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 57676 | 58185 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
| 58186 | + ** |
| 58187 | + ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still |
| 58188 | + ** avoid using the last six entries in the freelist trunk page array in |
| 58189 | + ** order that database files created by newer versions of SQLite can be |
| 58190 | + ** read by older versions of SQLite. |
| 57677 | 58191 | */ |
| 57678 | 58192 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
| 57679 | 58193 | if( rc==SQLITE_OK ){ |
| 57680 | 58194 | put4byte(&pTrunk->aData[4], nLeaf+1); |
| 57681 | 58195 | put4byte(&pTrunk->aData[8+nLeaf*4], iPage); |
| | @@ -58023,13 +58537,21 @@ |
| 58023 | 58537 | if( rc ){ |
| 58024 | 58538 | *pRC = rc; |
| 58025 | 58539 | return; |
| 58026 | 58540 | } |
| 58027 | 58541 | pPage->nCell--; |
| 58028 | | - memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); |
| 58029 | | - put2byte(&data[hdr+3], pPage->nCell); |
| 58030 | | - pPage->nFree += 2; |
| 58542 | + if( pPage->nCell==0 ){ |
| 58543 | + memset(&data[hdr+1], 0, 4); |
| 58544 | + data[hdr+7] = 0; |
| 58545 | + put2byte(&data[hdr+5], pPage->pBt->usableSize); |
| 58546 | + pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset |
| 58547 | + - pPage->childPtrSize - 8; |
| 58548 | + }else{ |
| 58549 | + memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); |
| 58550 | + put2byte(&data[hdr+3], pPage->nCell); |
| 58551 | + pPage->nFree += 2; |
| 58552 | + } |
| 58031 | 58553 | } |
| 58032 | 58554 | |
| 58033 | 58555 | /* |
| 58034 | 58556 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 58035 | 58557 | ** content of the cell. |
| | @@ -58120,49 +58642,275 @@ |
| 58120 | 58642 | #endif |
| 58121 | 58643 | } |
| 58122 | 58644 | } |
| 58123 | 58645 | |
| 58124 | 58646 | /* |
| 58125 | | -** Add a list of cells to a page. The page should be initially empty. |
| 58126 | | -** The cells are guaranteed to fit on the page. |
| 58127 | | -*/ |
| 58128 | | -static void assemblePage( |
| 58129 | | - MemPage *pPage, /* The page to be assembled */ |
| 58130 | | - int nCell, /* The number of cells to add to this page */ |
| 58131 | | - u8 **apCell, /* Pointers to cell bodies */ |
| 58132 | | - u16 *aSize /* Sizes of the cells */ |
| 58133 | | -){ |
| 58134 | | - int i; /* Loop counter */ |
| 58135 | | - u8 *pCellptr; /* Address of next cell pointer */ |
| 58136 | | - int cellbody; /* Address of next cell body */ |
| 58137 | | - u8 * const data = pPage->aData; /* Pointer to data for pPage */ |
| 58138 | | - const int hdr = pPage->hdrOffset; /* Offset of header on pPage */ |
| 58139 | | - const int nUsable = pPage->pBt->usableSize; /* Usable size of page */ |
| 58140 | | - |
| 58141 | | - assert( pPage->nOverflow==0 ); |
| 58142 | | - assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 58143 | | - assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt) |
| 58144 | | - && (int)MX_CELL(pPage->pBt)<=10921); |
| 58145 | | - assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 58146 | | - |
| 58147 | | - /* Check that the page has just been zeroed by zeroPage() */ |
| 58148 | | - assert( pPage->nCell==0 ); |
| 58149 | | - assert( get2byteNotZero(&data[hdr+5])==nUsable ); |
| 58150 | | - |
| 58151 | | - pCellptr = &pPage->aCellIdx[nCell*2]; |
| 58152 | | - cellbody = nUsable; |
| 58153 | | - for(i=nCell-1; i>=0; i--){ |
| 58154 | | - u16 sz = aSize[i]; |
| 58155 | | - pCellptr -= 2; |
| 58156 | | - cellbody -= sz; |
| 58157 | | - put2byte(pCellptr, cellbody); |
| 58158 | | - memcpy(&data[cellbody], apCell[i], sz); |
| 58159 | | - } |
| 58160 | | - put2byte(&data[hdr+3], nCell); |
| 58161 | | - put2byte(&data[hdr+5], cellbody); |
| 58162 | | - pPage->nFree -= (nCell*2 + nUsable - cellbody); |
| 58163 | | - pPage->nCell = (u16)nCell; |
| 58647 | +** Array apCell[] contains pointers to nCell b-tree page cells. The |
| 58648 | +** szCell[] array contains the size in bytes of each cell. This function |
| 58649 | +** replaces the current contents of page pPg with the contents of the cell |
| 58650 | +** array. |
| 58651 | +** |
| 58652 | +** Some of the cells in apCell[] may currently be stored in pPg. This |
| 58653 | +** function works around problems caused by this by making a copy of any |
| 58654 | +** such cells before overwriting the page data. |
| 58655 | +** |
| 58656 | +** The MemPage.nFree field is invalidated by this function. It is the |
| 58657 | +** responsibility of the caller to set it correctly. |
| 58658 | +*/ |
| 58659 | +static void rebuildPage( |
| 58660 | + MemPage *pPg, /* Edit this page */ |
| 58661 | + int nCell, /* Final number of cells on page */ |
| 58662 | + u8 **apCell, /* Array of cells */ |
| 58663 | + u16 *szCell /* Array of cell sizes */ |
| 58664 | +){ |
| 58665 | + const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ |
| 58666 | + u8 * const aData = pPg->aData; /* Pointer to data for pPg */ |
| 58667 | + const int usableSize = pPg->pBt->usableSize; |
| 58668 | + u8 * const pEnd = &aData[usableSize]; |
| 58669 | + int i; |
| 58670 | + u8 *pCellptr = pPg->aCellIdx; |
| 58671 | + u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
| 58672 | + u8 *pData; |
| 58673 | + |
| 58674 | + i = get2byte(&aData[hdr+5]); |
| 58675 | + memcpy(&pTmp[i], &aData[i], usableSize - i); |
| 58676 | + |
| 58677 | + pData = pEnd; |
| 58678 | + for(i=0; i<nCell; i++){ |
| 58679 | + u8 *pCell = apCell[i]; |
| 58680 | + if( pCell>aData && pCell<pEnd ){ |
| 58681 | + pCell = &pTmp[pCell - aData]; |
| 58682 | + } |
| 58683 | + pData -= szCell[i]; |
| 58684 | + memcpy(pData, pCell, szCell[i]); |
| 58685 | + put2byte(pCellptr, (pData - aData)); |
| 58686 | + pCellptr += 2; |
| 58687 | + assert( szCell[i]==cellSizePtr(pPg, pCell) ); |
| 58688 | + } |
| 58689 | + |
| 58690 | + /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ |
| 58691 | + pPg->nCell = nCell; |
| 58692 | + pPg->nOverflow = 0; |
| 58693 | + |
| 58694 | + put2byte(&aData[hdr+1], 0); |
| 58695 | + put2byte(&aData[hdr+3], pPg->nCell); |
| 58696 | + put2byte(&aData[hdr+5], pData - aData); |
| 58697 | + aData[hdr+7] = 0x00; |
| 58698 | +} |
| 58699 | + |
| 58700 | +/* |
| 58701 | +** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
| 58702 | +** contains the size in bytes of each such cell. This function attempts to |
| 58703 | +** add the cells stored in the array to page pPg. If it cannot (because |
| 58704 | +** the page needs to be defragmented before the cells will fit), non-zero |
| 58705 | +** is returned. Otherwise, if the cells are added successfully, zero is |
| 58706 | +** returned. |
| 58707 | +** |
| 58708 | +** Argument pCellptr points to the first entry in the cell-pointer array |
| 58709 | +** (part of page pPg) to populate. After cell apCell[0] is written to the |
| 58710 | +** page body, a 16-bit offset is written to pCellptr. And so on, for each |
| 58711 | +** cell in the array. It is the responsibility of the caller to ensure |
| 58712 | +** that it is safe to overwrite this part of the cell-pointer array. |
| 58713 | +** |
| 58714 | +** When this function is called, *ppData points to the start of the |
| 58715 | +** content area on page pPg. If the size of the content area is extended, |
| 58716 | +** *ppData is updated to point to the new start of the content area |
| 58717 | +** before returning. |
| 58718 | +** |
| 58719 | +** Finally, argument pBegin points to the byte immediately following the |
| 58720 | +** end of the space required by this page for the cell-pointer area (for |
| 58721 | +** all cells - not just those inserted by the current call). If the content |
| 58722 | +** area must be extended to before this point in order to accomodate all |
| 58723 | +** cells in apCell[], then the cells do not fit and non-zero is returned. |
| 58724 | +*/ |
| 58725 | +static int pageInsertArray( |
| 58726 | + MemPage *pPg, /* Page to add cells to */ |
| 58727 | + u8 *pBegin, /* End of cell-pointer array */ |
| 58728 | + u8 **ppData, /* IN/OUT: Page content -area pointer */ |
| 58729 | + u8 *pCellptr, /* Pointer to cell-pointer area */ |
| 58730 | + int nCell, /* Number of cells to add to pPg */ |
| 58731 | + u8 **apCell, /* Array of cells */ |
| 58732 | + u16 *szCell /* Array of cell sizes */ |
| 58733 | +){ |
| 58734 | + int i; |
| 58735 | + u8 *aData = pPg->aData; |
| 58736 | + u8 *pData = *ppData; |
| 58737 | + const int bFreelist = aData[1] || aData[2]; |
| 58738 | + assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ |
| 58739 | + for(i=0; i<nCell; i++){ |
| 58740 | + int sz = szCell[i]; |
| 58741 | + int rc; |
| 58742 | + u8 *pSlot; |
| 58743 | + if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){ |
| 58744 | + pData -= sz; |
| 58745 | + if( pData<pBegin ) return 1; |
| 58746 | + pSlot = pData; |
| 58747 | + } |
| 58748 | + memcpy(pSlot, apCell[i], sz); |
| 58749 | + put2byte(pCellptr, (pSlot - aData)); |
| 58750 | + pCellptr += 2; |
| 58751 | + } |
| 58752 | + *ppData = pData; |
| 58753 | + return 0; |
| 58754 | +} |
| 58755 | + |
| 58756 | +/* |
| 58757 | +** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
| 58758 | +** contains the size in bytes of each such cell. This function adds the |
| 58759 | +** space associated with each cell in the array that is currently stored |
| 58760 | +** within the body of pPg to the pPg free-list. The cell-pointers and other |
| 58761 | +** fields of the page are not updated. |
| 58762 | +** |
| 58763 | +** This function returns the total number of cells added to the free-list. |
| 58764 | +*/ |
| 58765 | +static int pageFreeArray( |
| 58766 | + MemPage *pPg, /* Page to edit */ |
| 58767 | + int nCell, /* Cells to delete */ |
| 58768 | + u8 **apCell, /* Array of cells */ |
| 58769 | + u16 *szCell /* Array of cell sizes */ |
| 58770 | +){ |
| 58771 | + u8 * const aData = pPg->aData; |
| 58772 | + u8 * const pEnd = &aData[pPg->pBt->usableSize]; |
| 58773 | + u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; |
| 58774 | + int nRet = 0; |
| 58775 | + int i; |
| 58776 | + u8 *pFree = 0; |
| 58777 | + int szFree = 0; |
| 58778 | + |
| 58779 | + for(i=0; i<nCell; i++){ |
| 58780 | + u8 *pCell = apCell[i]; |
| 58781 | + if( pCell>=pStart && pCell<pEnd ){ |
| 58782 | + int sz = szCell[i]; |
| 58783 | + if( pFree!=(pCell + sz) ){ |
| 58784 | + if( pFree ){ |
| 58785 | + assert( pFree>aData && (pFree - aData)<65536 ); |
| 58786 | + freeSpace(pPg, (u16)(pFree - aData), szFree); |
| 58787 | + } |
| 58788 | + pFree = pCell; |
| 58789 | + szFree = sz; |
| 58790 | + if( pFree+sz>pEnd ) return 0; |
| 58791 | + }else{ |
| 58792 | + pFree = pCell; |
| 58793 | + szFree += sz; |
| 58794 | + } |
| 58795 | + nRet++; |
| 58796 | + } |
| 58797 | + } |
| 58798 | + if( pFree ){ |
| 58799 | + assert( pFree>aData && (pFree - aData)<65536 ); |
| 58800 | + freeSpace(pPg, (u16)(pFree - aData), szFree); |
| 58801 | + } |
| 58802 | + return nRet; |
| 58803 | +} |
| 58804 | + |
| 58805 | +/* |
| 58806 | +** apCell[] and szCell[] contains pointers to and sizes of all cells in the |
| 58807 | +** pages being balanced. The current page, pPg, has pPg->nCell cells starting |
| 58808 | +** with apCell[iOld]. After balancing, this page should hold nNew cells |
| 58809 | +** starting at apCell[iNew]. |
| 58810 | +** |
| 58811 | +** This routine makes the necessary adjustments to pPg so that it contains |
| 58812 | +** the correct cells after being balanced. |
| 58813 | +** |
| 58814 | +** The pPg->nFree field is invalid when this function returns. It is the |
| 58815 | +** responsibility of the caller to set it correctly. |
| 58816 | +*/ |
| 58817 | +static void editPage( |
| 58818 | + MemPage *pPg, /* Edit this page */ |
| 58819 | + int iOld, /* Index of first cell currently on page */ |
| 58820 | + int iNew, /* Index of new first cell on page */ |
| 58821 | + int nNew, /* Final number of cells on page */ |
| 58822 | + u8 **apCell, /* Array of cells */ |
| 58823 | + u16 *szCell /* Array of cell sizes */ |
| 58824 | +){ |
| 58825 | + u8 * const aData = pPg->aData; |
| 58826 | + const int hdr = pPg->hdrOffset; |
| 58827 | + u8 *pBegin = &pPg->aCellIdx[nNew * 2]; |
| 58828 | + int nCell = pPg->nCell; /* Cells stored on pPg */ |
| 58829 | + u8 *pData; |
| 58830 | + u8 *pCellptr; |
| 58831 | + int i; |
| 58832 | + int iOldEnd = iOld + pPg->nCell + pPg->nOverflow; |
| 58833 | + int iNewEnd = iNew + nNew; |
| 58834 | + |
| 58835 | +#ifdef SQLITE_DEBUG |
| 58836 | + u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
| 58837 | + memcpy(pTmp, aData, pPg->pBt->usableSize); |
| 58838 | +#endif |
| 58839 | + |
| 58840 | + /* Remove cells from the start and end of the page */ |
| 58841 | + if( iOld<iNew ){ |
| 58842 | + int nShift = pageFreeArray( |
| 58843 | + pPg, iNew-iOld, &apCell[iOld], &szCell[iOld] |
| 58844 | + ); |
| 58845 | + memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); |
| 58846 | + nCell -= nShift; |
| 58847 | + } |
| 58848 | + if( iNewEnd < iOldEnd ){ |
| 58849 | + nCell -= pageFreeArray( |
| 58850 | + pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd] |
| 58851 | + ); |
| 58852 | + } |
| 58853 | + |
| 58854 | + pData = &aData[get2byteNotZero(&aData[hdr+5])]; |
| 58855 | + if( pData<pBegin ) goto editpage_fail; |
| 58856 | + |
| 58857 | + /* Add cells to the start of the page */ |
| 58858 | + if( iNew<iOld ){ |
| 58859 | + int nAdd = MIN(nNew,iOld-iNew); |
| 58860 | + assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB ); |
| 58861 | + pCellptr = pPg->aCellIdx; |
| 58862 | + memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); |
| 58863 | + if( pageInsertArray( |
| 58864 | + pPg, pBegin, &pData, pCellptr, |
| 58865 | + nAdd, &apCell[iNew], &szCell[iNew] |
| 58866 | + ) ) goto editpage_fail; |
| 58867 | + nCell += nAdd; |
| 58868 | + } |
| 58869 | + |
| 58870 | + /* Add any overflow cells */ |
| 58871 | + for(i=0; i<pPg->nOverflow; i++){ |
| 58872 | + int iCell = (iOld + pPg->aiOvfl[i]) - iNew; |
| 58873 | + if( iCell>=0 && iCell<nNew ){ |
| 58874 | + pCellptr = &pPg->aCellIdx[iCell * 2]; |
| 58875 | + memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); |
| 58876 | + nCell++; |
| 58877 | + if( pageInsertArray( |
| 58878 | + pPg, pBegin, &pData, pCellptr, |
| 58879 | + 1, &apCell[iCell + iNew], &szCell[iCell + iNew] |
| 58880 | + ) ) goto editpage_fail; |
| 58881 | + } |
| 58882 | + } |
| 58883 | + |
| 58884 | + /* Append cells to the end of the page */ |
| 58885 | + pCellptr = &pPg->aCellIdx[nCell*2]; |
| 58886 | + if( pageInsertArray( |
| 58887 | + pPg, pBegin, &pData, pCellptr, |
| 58888 | + nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell] |
| 58889 | + ) ) goto editpage_fail; |
| 58890 | + |
| 58891 | + pPg->nCell = nNew; |
| 58892 | + pPg->nOverflow = 0; |
| 58893 | + |
| 58894 | + put2byte(&aData[hdr+3], pPg->nCell); |
| 58895 | + put2byte(&aData[hdr+5], pData - aData); |
| 58896 | + |
| 58897 | +#ifdef SQLITE_DEBUG |
| 58898 | + for(i=0; i<nNew && !CORRUPT_DB; i++){ |
| 58899 | + u8 *pCell = apCell[i+iNew]; |
| 58900 | + int iOff = get2byte(&pPg->aCellIdx[i*2]); |
| 58901 | + if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){ |
| 58902 | + pCell = &pTmp[pCell - aData]; |
| 58903 | + } |
| 58904 | + assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) ); |
| 58905 | + } |
| 58906 | +#endif |
| 58907 | + |
| 58908 | + return; |
| 58909 | + editpage_fail: |
| 58910 | + /* Unable to edit this page. Rebuild it from scratch instead. */ |
| 58911 | + rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]); |
| 58164 | 58912 | } |
| 58165 | 58913 | |
| 58166 | 58914 | /* |
| 58167 | 58915 | ** The following parameters determine how many adjacent pages get involved |
| 58168 | 58916 | ** in a balancing operation. NN is the number of neighbors on either side |
| | @@ -58212,11 +58960,11 @@ |
| 58212 | 58960 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 58213 | 58961 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 58214 | 58962 | assert( pPage->nOverflow==1 ); |
| 58215 | 58963 | |
| 58216 | 58964 | /* This error condition is now caught prior to reaching this function */ |
| 58217 | | - if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; |
| 58965 | + if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT; |
| 58218 | 58966 | |
| 58219 | 58967 | /* Allocate a new page. This page will become the right-sibling of |
| 58220 | 58968 | ** pPage. Make the parent page writable, so that the new divider cell |
| 58221 | 58969 | ** may be inserted. If both these operations are successful, proceed. |
| 58222 | 58970 | */ |
| | @@ -58230,11 +58978,12 @@ |
| 58230 | 58978 | u8 *pStop; |
| 58231 | 58979 | |
| 58232 | 58980 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
| 58233 | 58981 | assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); |
| 58234 | 58982 | zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); |
| 58235 | | - assemblePage(pNew, 1, &pCell, &szCell); |
| 58983 | + rebuildPage(pNew, 1, &pCell, &szCell); |
| 58984 | + pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; |
| 58236 | 58985 | |
| 58237 | 58986 | /* If this is an auto-vacuum database, update the pointer map |
| 58238 | 58987 | ** with entries for the new page, and any pointer from the |
| 58239 | 58988 | ** cell on the page to an overflow page. If either of these |
| 58240 | 58989 | ** operations fails, the return code is set, but the contents |
| | @@ -58449,21 +59198,26 @@ |
| 58449 | 59198 | int subtotal; /* Subtotal of bytes in cells on one page */ |
| 58450 | 59199 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
| 58451 | 59200 | int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ |
| 58452 | 59201 | int szScratch; /* Size of scratch memory requested */ |
| 58453 | 59202 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 58454 | | - MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
| 58455 | 59203 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 58456 | 59204 | u8 *pRight; /* Location in parent of right-sibling pointer */ |
| 58457 | 59205 | u8 *apDiv[NB-1]; /* Divider cells in pParent */ |
| 58458 | 59206 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 58459 | | - int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
| 59207 | + int cntOld[NB+2]; /* Old index in aCell[] after i-th page */ |
| 59208 | + int szNew[NB+2]; /* Combined size of cells placed on i-th page */ |
| 58460 | 59209 | u8 **apCell = 0; /* All cells begin balanced */ |
| 58461 | 59210 | u16 *szCell; /* Local size of all cells in apCell[] */ |
| 58462 | 59211 | u8 *aSpace1; /* Space for copies of dividers cells */ |
| 58463 | 59212 | Pgno pgno; /* Temp var to store a page number in */ |
| 59213 | + u8 abDone[NB+2]; /* True after i'th new page is populated */ |
| 59214 | + Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ |
| 59215 | + Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ |
| 59216 | + u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ |
| 58464 | 59217 | |
| 59218 | + memset(abDone, 0, sizeof(abDone)); |
| 58465 | 59219 | pBt = pParent->pBt; |
| 58466 | 59220 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 58467 | 59221 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 58468 | 59222 | |
| 58469 | 59223 | #if 0 |
| | @@ -58568,16 +59322,18 @@ |
| 58568 | 59322 | nMaxCells = (nMaxCells + 3)&~3; |
| 58569 | 59323 | |
| 58570 | 59324 | /* |
| 58571 | 59325 | ** Allocate space for memory structures |
| 58572 | 59326 | */ |
| 58573 | | - k = pBt->pageSize + ROUND8(sizeof(MemPage)); |
| 58574 | 59327 | szScratch = |
| 58575 | 59328 | nMaxCells*sizeof(u8*) /* apCell */ |
| 58576 | 59329 | + nMaxCells*sizeof(u16) /* szCell */ |
| 58577 | | - + pBt->pageSize /* aSpace1 */ |
| 58578 | | - + k*nOld; /* Page copies (apCopy) */ |
| 59330 | + + pBt->pageSize; /* aSpace1 */ |
| 59331 | + |
| 59332 | + /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer |
| 59333 | + ** that is more than 6 times the database page size. */ |
| 59334 | + assert( szScratch<=6*pBt->pageSize ); |
| 58579 | 59335 | apCell = sqlite3ScratchMalloc( szScratch ); |
| 58580 | 59336 | if( apCell==0 ){ |
| 58581 | 59337 | rc = SQLITE_NOMEM; |
| 58582 | 59338 | goto balance_cleanup; |
| 58583 | 59339 | } |
| | @@ -58586,12 +59342,12 @@ |
| 58586 | 59342 | assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); |
| 58587 | 59343 | |
| 58588 | 59344 | /* |
| 58589 | 59345 | ** Load pointers to all cells on sibling pages and the divider cells |
| 58590 | 59346 | ** into the local apCell[] array. Make copies of the divider cells |
| 58591 | | - ** into space obtained from aSpace1[] and remove the divider cells |
| 58592 | | - ** from pParent. |
| 59347 | + ** into space obtained from aSpace1[]. The divider cells have already |
| 59348 | + ** been removed from pParent. |
| 58593 | 59349 | ** |
| 58594 | 59350 | ** If the siblings are on leaf pages, then the child pointers of the |
| 58595 | 59351 | ** divider cells are stripped from the cells before they are copied |
| 58596 | 59352 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
| 58597 | 59353 | ** child pointers. If siblings are not leaves, then all cell in |
| | @@ -58603,19 +59359,11 @@ |
| 58603 | 59359 | */ |
| 58604 | 59360 | leafCorrection = apOld[0]->leaf*4; |
| 58605 | 59361 | leafData = apOld[0]->intKeyLeaf; |
| 58606 | 59362 | for(i=0; i<nOld; i++){ |
| 58607 | 59363 | int limit; |
| 58608 | | - |
| 58609 | | - /* Before doing anything else, take a copy of the i'th original sibling |
| 58610 | | - ** The rest of this function will use data from the copies rather |
| 58611 | | - ** that the original pages since the original pages will be in the |
| 58612 | | - ** process of being overwritten. */ |
| 58613 | | - MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i]; |
| 58614 | | - memcpy(pOld, apOld[i], sizeof(MemPage)); |
| 58615 | | - pOld->aData = (void*)&pOld[1]; |
| 58616 | | - memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize); |
| 59364 | + MemPage *pOld = apOld[i]; |
| 58617 | 59365 | |
| 58618 | 59366 | limit = pOld->nCell+pOld->nOverflow; |
| 58619 | 59367 | if( pOld->nOverflow>0 ){ |
| 58620 | 59368 | for(j=0; j<limit; j++){ |
| 58621 | 59369 | assert( nCell<nMaxCells ); |
| | @@ -58632,10 +59380,11 @@ |
| 58632 | 59380 | apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j); |
| 58633 | 59381 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
| 58634 | 59382 | nCell++; |
| 58635 | 59383 | } |
| 58636 | 59384 | } |
| 59385 | + cntOld[i] = nCell; |
| 58637 | 59386 | if( i<nOld-1 && !leafData){ |
| 58638 | 59387 | u16 sz = (u16)szNew[i]; |
| 58639 | 59388 | u8 *pTemp; |
| 58640 | 59389 | assert( nCell<nMaxCells ); |
| 58641 | 59390 | szCell[nCell] = sz; |
| | @@ -58654,11 +59403,15 @@ |
| 58654 | 59403 | ** pointer of the divider cell */ |
| 58655 | 59404 | memcpy(apCell[nCell], &pOld->aData[8], 4); |
| 58656 | 59405 | }else{ |
| 58657 | 59406 | assert( leafCorrection==4 ); |
| 58658 | 59407 | if( szCell[nCell]<4 ){ |
| 58659 | | - /* Do not allow any cells smaller than 4 bytes. */ |
| 59408 | + /* Do not allow any cells smaller than 4 bytes. If a smaller cell |
| 59409 | + ** does exist, pad it with 0x00 bytes. */ |
| 59410 | + assert( szCell[nCell]==3 ); |
| 59411 | + assert( apCell[nCell]==&pTemp[iSpace1-3] ); |
| 59412 | + pTemp[iSpace1++] = 0x00; |
| 58660 | 59413 | szCell[nCell] = 4; |
| 58661 | 59414 | } |
| 58662 | 59415 | } |
| 58663 | 59416 | nCell++; |
| 58664 | 59417 | } |
| | @@ -58683,11 +59436,11 @@ |
| 58683 | 59436 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
| 58684 | 59437 | for(subtotal=k=i=0; i<nCell; i++){ |
| 58685 | 59438 | assert( i<nMaxCells ); |
| 58686 | 59439 | subtotal += szCell[i] + 2; |
| 58687 | 59440 | if( subtotal > usableSpace ){ |
| 58688 | | - szNew[k] = subtotal - szCell[i]; |
| 59441 | + szNew[k] = subtotal - szCell[i] - 2; |
| 58689 | 59442 | cntNew[k] = i; |
| 58690 | 59443 | if( leafData ){ i--; } |
| 58691 | 59444 | subtotal = 0; |
| 58692 | 59445 | k++; |
| 58693 | 59446 | if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
| | @@ -58697,13 +59450,14 @@ |
| 58697 | 59450 | cntNew[k] = nCell; |
| 58698 | 59451 | k++; |
| 58699 | 59452 | |
| 58700 | 59453 | /* |
| 58701 | 59454 | ** The packing computed by the previous block is biased toward the siblings |
| 58702 | | - ** on the left side. The left siblings are always nearly full, while the |
| 58703 | | - ** right-most sibling might be nearly empty. This block of code attempts |
| 58704 | | - ** to adjust the packing of siblings to get a better balance. |
| 59455 | + ** on the left side (siblings with smaller keys). The left siblings are |
| 59456 | + ** always nearly full, while the right-most sibling might be nearly empty. |
| 59457 | + ** The next block of code attempts to adjust the packing of siblings to |
| 59458 | + ** get a better balance. |
| 58705 | 59459 | ** |
| 58706 | 59460 | ** This adjustment is more than an optimization. The packing above might |
| 58707 | 59461 | ** be so out of balance as to be illegal. For example, the right-most |
| 58708 | 59462 | ** sibling might be completely empty. This adjustment is not optional. |
| 58709 | 59463 | */ |
| | @@ -58728,26 +59482,22 @@ |
| 58728 | 59482 | } |
| 58729 | 59483 | szNew[i] = szRight; |
| 58730 | 59484 | szNew[i-1] = szLeft; |
| 58731 | 59485 | } |
| 58732 | 59486 | |
| 58733 | | - /* Either we found one or more cells (cntnew[0])>0) or pPage is |
| 58734 | | - ** a virtual root page. A virtual root page is when the real root |
| 58735 | | - ** page is page 1 and we are the only child of that page. |
| 58736 | | - ** |
| 58737 | | - ** UPDATE: The assert() below is not necessarily true if the database |
| 58738 | | - ** file is corrupt. The corruption will be detected and reported later |
| 58739 | | - ** in this procedure so there is no need to act upon it now. |
| 59487 | + /* Sanity check: For a non-corrupt database file one of the follwing |
| 59488 | + ** must be true: |
| 59489 | + ** (1) We found one or more cells (cntNew[0])>0), or |
| 59490 | + ** (2) pPage is a virtual root page. A virtual root page is when |
| 59491 | + ** the real root page is page 1 and we are the only child of |
| 59492 | + ** that page. |
| 58740 | 59493 | */ |
| 58741 | | -#if 0 |
| 58742 | | - assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
| 58743 | | -#endif |
| 58744 | | - |
| 58745 | | - TRACE(("BALANCE: old: %d %d %d ", |
| 58746 | | - apOld[0]->pgno, |
| 58747 | | - nOld>=2 ? apOld[1]->pgno : 0, |
| 58748 | | - nOld>=3 ? apOld[2]->pgno : 0 |
| 59494 | + assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB); |
| 59495 | + TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n", |
| 59496 | + apOld[0]->pgno, apOld[0]->nCell, |
| 59497 | + nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0, |
| 59498 | + nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0 |
| 58749 | 59499 | )); |
| 58750 | 59500 | |
| 58751 | 59501 | /* |
| 58752 | 59502 | ** Allocate k new pages. Reuse old pages where possible. |
| 58753 | 59503 | */ |
| | @@ -58766,12 +59516,14 @@ |
| 58766 | 59516 | if( rc ) goto balance_cleanup; |
| 58767 | 59517 | }else{ |
| 58768 | 59518 | assert( i>0 ); |
| 58769 | 59519 | rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0); |
| 58770 | 59520 | if( rc ) goto balance_cleanup; |
| 59521 | + zeroPage(pNew, pageFlags); |
| 58771 | 59522 | apNew[i] = pNew; |
| 58772 | 59523 | nNew++; |
| 59524 | + cntOld[i] = nCell; |
| 58773 | 59525 | |
| 58774 | 59526 | /* Set the pointer-map entry for the new sibling page. */ |
| 58775 | 59527 | if( ISAUTOVACUUM ){ |
| 58776 | 59528 | ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); |
| 58777 | 59529 | if( rc!=SQLITE_OK ){ |
| | @@ -58779,139 +59531,251 @@ |
| 58779 | 59531 | } |
| 58780 | 59532 | } |
| 58781 | 59533 | } |
| 58782 | 59534 | } |
| 58783 | 59535 | |
| 58784 | | - /* Free any old pages that were not reused as new pages. |
| 58785 | | - */ |
| 58786 | | - while( i<nOld ){ |
| 58787 | | - freePage(apOld[i], &rc); |
| 58788 | | - if( rc ) goto balance_cleanup; |
| 58789 | | - releasePage(apOld[i]); |
| 58790 | | - apOld[i] = 0; |
| 58791 | | - i++; |
| 58792 | | - } |
| 58793 | | - |
| 58794 | 59536 | /* |
| 58795 | | - ** Put the new pages in ascending order. This helps to |
| 58796 | | - ** keep entries in the disk file in order so that a scan |
| 58797 | | - ** of the table is a linear scan through the file. That |
| 58798 | | - ** in turn helps the operating system to deliver pages |
| 58799 | | - ** from the disk more rapidly. |
| 58800 | | - ** |
| 58801 | | - ** An O(n^2) insertion sort algorithm is used, but since |
| 58802 | | - ** n is never more than NB (a small constant), that should |
| 58803 | | - ** not be a problem. |
| 58804 | | - ** |
| 58805 | | - ** When NB==3, this one optimization makes the database |
| 58806 | | - ** about 25% faster for large insertions and deletions. |
| 58807 | | - */ |
| 58808 | | - for(i=0; i<k-1; i++){ |
| 58809 | | - int minV = apNew[i]->pgno; |
| 58810 | | - int minI = i; |
| 58811 | | - for(j=i+1; j<k; j++){ |
| 58812 | | - if( apNew[j]->pgno<(unsigned)minV ){ |
| 58813 | | - minI = j; |
| 58814 | | - minV = apNew[j]->pgno; |
| 58815 | | - } |
| 58816 | | - } |
| 58817 | | - if( minI>i ){ |
| 58818 | | - MemPage *pT; |
| 58819 | | - pT = apNew[i]; |
| 58820 | | - apNew[i] = apNew[minI]; |
| 58821 | | - apNew[minI] = pT; |
| 58822 | | - } |
| 58823 | | - } |
| 58824 | | - TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n", |
| 58825 | | - apNew[0]->pgno, szNew[0], |
| 59537 | + ** Reassign page numbers so that the new pages are in ascending order. |
| 59538 | + ** This helps to keep entries in the disk file in order so that a scan |
| 59539 | + ** of the table is closer to a linear scan through the file. That in turn |
| 59540 | + ** helps the operating system to deliver pages from the disk more rapidly. |
| 59541 | + ** |
| 59542 | + ** An O(n^2) insertion sort algorithm is used, but since n is never more |
| 59543 | + ** than (NB+2) (a small constant), that should not be a problem. |
| 59544 | + ** |
| 59545 | + ** When NB==3, this one optimization makes the database about 25% faster |
| 59546 | + ** for large insertions and deletions. |
| 59547 | + */ |
| 59548 | + for(i=0; i<nNew; i++){ |
| 59549 | + aPgOrder[i] = aPgno[i] = apNew[i]->pgno; |
| 59550 | + aPgFlags[i] = apNew[i]->pDbPage->flags; |
| 59551 | + for(j=0; j<i; j++){ |
| 59552 | + if( aPgno[j]==aPgno[i] ){ |
| 59553 | + /* This branch is taken if the set of sibling pages somehow contains |
| 59554 | + ** duplicate entries. This can happen if the database is corrupt. |
| 59555 | + ** It would be simpler to detect this as part of the loop below, but |
| 59556 | + ** we do the detection here in order to avoid populating the pager |
| 59557 | + ** cache with two separate objects associated with the same |
| 59558 | + ** page number. */ |
| 59559 | + assert( CORRUPT_DB ); |
| 59560 | + rc = SQLITE_CORRUPT_BKPT; |
| 59561 | + goto balance_cleanup; |
| 59562 | + } |
| 59563 | + } |
| 59564 | + } |
| 59565 | + for(i=0; i<nNew; i++){ |
| 59566 | + int iBest = 0; /* aPgno[] index of page number to use */ |
| 59567 | + for(j=1; j<nNew; j++){ |
| 59568 | + if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j; |
| 59569 | + } |
| 59570 | + pgno = aPgOrder[iBest]; |
| 59571 | + aPgOrder[iBest] = 0xffffffff; |
| 59572 | + if( iBest!=i ){ |
| 59573 | + if( iBest>i ){ |
| 59574 | + sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0); |
| 59575 | + } |
| 59576 | + sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]); |
| 59577 | + apNew[i]->pgno = pgno; |
| 59578 | + } |
| 59579 | + } |
| 59580 | + |
| 59581 | + TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) " |
| 59582 | + "%d(%d nc=%d) %d(%d nc=%d)\n", |
| 59583 | + apNew[0]->pgno, szNew[0], cntNew[0], |
| 58826 | 59584 | nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, |
| 59585 | + nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0, |
| 58827 | 59586 | nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, |
| 59587 | + nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0, |
| 58828 | 59588 | nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, |
| 58829 | | - nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0)); |
| 59589 | + nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0, |
| 59590 | + nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0, |
| 59591 | + nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0 |
| 59592 | + )); |
| 58830 | 59593 | |
| 58831 | 59594 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 58832 | 59595 | put4byte(pRight, apNew[nNew-1]->pgno); |
| 58833 | 59596 | |
| 58834 | | - /* |
| 58835 | | - ** Evenly distribute the data in apCell[] across the new pages. |
| 58836 | | - ** Insert divider cells into pParent as necessary. |
| 59597 | + /* If the sibling pages are not leaves, ensure that the right-child pointer |
| 59598 | + ** of the right-most new sibling page is set to the value that was |
| 59599 | + ** originally in the same field of the right-most old sibling page. */ |
| 59600 | + if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){ |
| 59601 | + MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1]; |
| 59602 | + memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4); |
| 59603 | + } |
| 59604 | + |
| 59605 | + /* Make any required updates to pointer map entries associated with |
| 59606 | + ** cells stored on sibling pages following the balance operation. Pointer |
| 59607 | + ** map entries associated with divider cells are set by the insertCell() |
| 59608 | + ** routine. The associated pointer map entries are: |
| 59609 | + ** |
| 59610 | + ** a) if the cell contains a reference to an overflow chain, the |
| 59611 | + ** entry associated with the first page in the overflow chain, and |
| 59612 | + ** |
| 59613 | + ** b) if the sibling pages are not leaves, the child page associated |
| 59614 | + ** with the cell. |
| 59615 | + ** |
| 59616 | + ** If the sibling pages are not leaves, then the pointer map entry |
| 59617 | + ** associated with the right-child of each sibling may also need to be |
| 59618 | + ** updated. This happens below, after the sibling pages have been |
| 59619 | + ** populated, not here. |
| 58837 | 59620 | */ |
| 58838 | | - j = 0; |
| 58839 | | - for(i=0; i<nNew; i++){ |
| 58840 | | - /* Assemble the new sibling page. */ |
| 59621 | + if( ISAUTOVACUUM ){ |
| 59622 | + MemPage *pNew = apNew[0]; |
| 59623 | + u8 *aOld = pNew->aData; |
| 59624 | + int cntOldNext = pNew->nCell + pNew->nOverflow; |
| 59625 | + int usableSize = pBt->usableSize; |
| 59626 | + int iNew = 0; |
| 59627 | + int iOld = 0; |
| 59628 | + |
| 59629 | + for(i=0; i<nCell; i++){ |
| 59630 | + u8 *pCell = apCell[i]; |
| 59631 | + if( i==cntOldNext ){ |
| 59632 | + MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld]; |
| 59633 | + cntOldNext += pOld->nCell + pOld->nOverflow + !leafData; |
| 59634 | + aOld = pOld->aData; |
| 59635 | + } |
| 59636 | + if( i==cntNew[iNew] ){ |
| 59637 | + pNew = apNew[++iNew]; |
| 59638 | + if( !leafData ) continue; |
| 59639 | + } |
| 59640 | + |
| 59641 | + /* Cell pCell is destined for new sibling page pNew. Originally, it |
| 59642 | + ** was either part of sibling page iOld (possibly an overflow cell), |
| 59643 | + ** or else the divider cell to the left of sibling page iOld. So, |
| 59644 | + ** if sibling page iOld had the same page number as pNew, and if |
| 59645 | + ** pCell really was a part of sibling page iOld (not a divider or |
| 59646 | + ** overflow cell), we can skip updating the pointer map entries. */ |
| 59647 | + if( iOld>=nNew |
| 59648 | + || pNew->pgno!=aPgno[iOld] |
| 59649 | + || pCell<aOld |
| 59650 | + || pCell>=&aOld[usableSize] |
| 59651 | + ){ |
| 59652 | + if( !leafCorrection ){ |
| 59653 | + ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); |
| 59654 | + } |
| 59655 | + if( szCell[i]>pNew->minLocal ){ |
| 59656 | + ptrmapPutOvflPtr(pNew, pCell, &rc); |
| 59657 | + } |
| 59658 | + } |
| 59659 | + } |
| 59660 | + } |
| 59661 | + |
| 59662 | + /* Insert new divider cells into pParent. */ |
| 59663 | + for(i=0; i<nNew-1; i++){ |
| 59664 | + u8 *pCell; |
| 59665 | + u8 *pTemp; |
| 59666 | + int sz; |
| 58841 | 59667 | MemPage *pNew = apNew[i]; |
| 58842 | | - assert( j<nMaxCells ); |
| 58843 | | - zeroPage(pNew, pageFlags); |
| 58844 | | - assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
| 58845 | | - assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
| 58846 | | - assert( pNew->nOverflow==0 ); |
| 58847 | | - |
| 58848 | 59668 | j = cntNew[i]; |
| 58849 | 59669 | |
| 58850 | | - /* If the sibling page assembled above was not the right-most sibling, |
| 58851 | | - ** insert a divider cell into the parent page. |
| 58852 | | - */ |
| 58853 | | - assert( i<nNew-1 || j==nCell ); |
| 58854 | | - if( j<nCell ){ |
| 58855 | | - u8 *pCell; |
| 58856 | | - u8 *pTemp; |
| 58857 | | - int sz; |
| 58858 | | - |
| 58859 | | - assert( j<nMaxCells ); |
| 58860 | | - pCell = apCell[j]; |
| 58861 | | - sz = szCell[j] + leafCorrection; |
| 58862 | | - pTemp = &aOvflSpace[iOvflSpace]; |
| 58863 | | - if( !pNew->leaf ){ |
| 58864 | | - memcpy(&pNew->aData[8], pCell, 4); |
| 58865 | | - }else if( leafData ){ |
| 58866 | | - /* If the tree is a leaf-data tree, and the siblings are leaves, |
| 58867 | | - ** then there is no divider cell in apCell[]. Instead, the divider |
| 58868 | | - ** cell consists of the integer key for the right-most cell of |
| 58869 | | - ** the sibling-page assembled above only. |
| 58870 | | - */ |
| 58871 | | - CellInfo info; |
| 58872 | | - j--; |
| 58873 | | - btreeParseCellPtr(pNew, apCell[j], &info); |
| 58874 | | - pCell = pTemp; |
| 58875 | | - sz = 4 + putVarint(&pCell[4], info.nKey); |
| 58876 | | - pTemp = 0; |
| 58877 | | - }else{ |
| 58878 | | - pCell -= 4; |
| 58879 | | - /* Obscure case for non-leaf-data trees: If the cell at pCell was |
| 58880 | | - ** previously stored on a leaf node, and its reported size was 4 |
| 58881 | | - ** bytes, then it may actually be smaller than this |
| 58882 | | - ** (see btreeParseCellPtr(), 4 bytes is the minimum size of |
| 58883 | | - ** any cell). But it is important to pass the correct size to |
| 58884 | | - ** insertCell(), so reparse the cell now. |
| 58885 | | - ** |
| 58886 | | - ** Note that this can never happen in an SQLite data file, as all |
| 58887 | | - ** cells are at least 4 bytes. It only happens in b-trees used |
| 58888 | | - ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 58889 | | - */ |
| 58890 | | - if( szCell[j]==4 ){ |
| 58891 | | - assert(leafCorrection==4); |
| 58892 | | - sz = cellSizePtr(pParent, pCell); |
| 58893 | | - } |
| 58894 | | - } |
| 58895 | | - iOvflSpace += sz; |
| 58896 | | - assert( sz<=pBt->maxLocal+23 ); |
| 58897 | | - assert( iOvflSpace <= (int)pBt->pageSize ); |
| 58898 | | - insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc); |
| 58899 | | - if( rc!=SQLITE_OK ) goto balance_cleanup; |
| 58900 | | - assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 58901 | | - |
| 58902 | | - j++; |
| 58903 | | - nxDiv++; |
| 58904 | | - } |
| 58905 | | - } |
| 58906 | | - assert( j==nCell ); |
| 59670 | + assert( j<nMaxCells ); |
| 59671 | + pCell = apCell[j]; |
| 59672 | + sz = szCell[j] + leafCorrection; |
| 59673 | + pTemp = &aOvflSpace[iOvflSpace]; |
| 59674 | + if( !pNew->leaf ){ |
| 59675 | + memcpy(&pNew->aData[8], pCell, 4); |
| 59676 | + }else if( leafData ){ |
| 59677 | + /* If the tree is a leaf-data tree, and the siblings are leaves, |
| 59678 | + ** then there is no divider cell in apCell[]. Instead, the divider |
| 59679 | + ** cell consists of the integer key for the right-most cell of |
| 59680 | + ** the sibling-page assembled above only. |
| 59681 | + */ |
| 59682 | + CellInfo info; |
| 59683 | + j--; |
| 59684 | + btreeParseCellPtr(pNew, apCell[j], &info); |
| 59685 | + pCell = pTemp; |
| 59686 | + sz = 4 + putVarint(&pCell[4], info.nKey); |
| 59687 | + pTemp = 0; |
| 59688 | + }else{ |
| 59689 | + pCell -= 4; |
| 59690 | + /* Obscure case for non-leaf-data trees: If the cell at pCell was |
| 59691 | + ** previously stored on a leaf node, and its reported size was 4 |
| 59692 | + ** bytes, then it may actually be smaller than this |
| 59693 | + ** (see btreeParseCellPtr(), 4 bytes is the minimum size of |
| 59694 | + ** any cell). But it is important to pass the correct size to |
| 59695 | + ** insertCell(), so reparse the cell now. |
| 59696 | + ** |
| 59697 | + ** Note that this can never happen in an SQLite data file, as all |
| 59698 | + ** cells are at least 4 bytes. It only happens in b-trees used |
| 59699 | + ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 59700 | + */ |
| 59701 | + if( szCell[j]==4 ){ |
| 59702 | + assert(leafCorrection==4); |
| 59703 | + sz = cellSizePtr(pParent, pCell); |
| 59704 | + } |
| 59705 | + } |
| 59706 | + iOvflSpace += sz; |
| 59707 | + assert( sz<=pBt->maxLocal+23 ); |
| 59708 | + assert( iOvflSpace <= (int)pBt->pageSize ); |
| 59709 | + insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc); |
| 59710 | + if( rc!=SQLITE_OK ) goto balance_cleanup; |
| 59711 | + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 59712 | + } |
| 59713 | + |
| 59714 | + /* Now update the actual sibling pages. The order in which they are updated |
| 59715 | + ** is important, as this code needs to avoid disrupting any page from which |
| 59716 | + ** cells may still to be read. In practice, this means: |
| 59717 | + ** |
| 59718 | + ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1]) |
| 59719 | + ** then it is not safe to update page apNew[iPg] until after |
| 59720 | + ** the left-hand sibling apNew[iPg-1] has been updated. |
| 59721 | + ** |
| 59722 | + ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1]) |
| 59723 | + ** then it is not safe to update page apNew[iPg] until after |
| 59724 | + ** the right-hand sibling apNew[iPg+1] has been updated. |
| 59725 | + ** |
| 59726 | + ** If neither of the above apply, the page is safe to update. |
| 59727 | + ** |
| 59728 | + ** The iPg value in the following loop starts at nNew-1 goes down |
| 59729 | + ** to 0, then back up to nNew-1 again, thus making two passes over |
| 59730 | + ** the pages. On the initial downward pass, only condition (1) above |
| 59731 | + ** needs to be tested because (2) will always be true from the previous |
| 59732 | + ** step. On the upward pass, both conditions are always true, so the |
| 59733 | + ** upwards pass simply processes pages that were missed on the downward |
| 59734 | + ** pass. |
| 59735 | + */ |
| 59736 | + for(i=1-nNew; i<nNew; i++){ |
| 59737 | + int iPg = i<0 ? -i : i; |
| 59738 | + assert( iPg>=0 && iPg<nNew ); |
| 59739 | + if( abDone[iPg] ) continue; /* Skip pages already processed */ |
| 59740 | + if( i>=0 /* On the upwards pass, or... */ |
| 59741 | + || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */ |
| 59742 | + ){ |
| 59743 | + int iNew; |
| 59744 | + int iOld; |
| 59745 | + int nNewCell; |
| 59746 | + |
| 59747 | + /* Verify condition (1): If cells are moving left, update iPg |
| 59748 | + ** only after iPg-1 has already been updated. */ |
| 59749 | + assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] ); |
| 59750 | + |
| 59751 | + /* Verify condition (2): If cells are moving right, update iPg |
| 59752 | + ** only after iPg+1 has already been updated. */ |
| 59753 | + assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] ); |
| 59754 | + |
| 59755 | + if( iPg==0 ){ |
| 59756 | + iNew = iOld = 0; |
| 59757 | + nNewCell = cntNew[0]; |
| 59758 | + }else{ |
| 59759 | + iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : nCell; |
| 59760 | + iNew = cntNew[iPg-1] + !leafData; |
| 59761 | + nNewCell = cntNew[iPg] - iNew; |
| 59762 | + } |
| 59763 | + |
| 59764 | + editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell); |
| 59765 | + abDone[iPg]++; |
| 59766 | + apNew[iPg]->nFree = usableSpace-szNew[iPg]; |
| 59767 | + assert( apNew[iPg]->nOverflow==0 ); |
| 59768 | + assert( apNew[iPg]->nCell==nNewCell ); |
| 59769 | + } |
| 59770 | + } |
| 59771 | + |
| 59772 | + /* All pages have been processed exactly once */ |
| 59773 | + assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 ); |
| 59774 | + |
| 58907 | 59775 | assert( nOld>0 ); |
| 58908 | 59776 | assert( nNew>0 ); |
| 58909 | | - if( (pageFlags & PTF_LEAF)==0 ){ |
| 58910 | | - u8 *zChild = &apCopy[nOld-1]->aData[8]; |
| 58911 | | - memcpy(&apNew[nNew-1]->aData[8], zChild, 4); |
| 58912 | | - } |
| 58913 | 59777 | |
| 58914 | 59778 | if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ |
| 58915 | 59779 | /* The root page of the b-tree now contains no cells. The only sibling |
| 58916 | 59780 | ** page is the right-child of the parent. Copy the contents of the |
| 58917 | 59781 | ** child page into the parent, decreasing the overall height of the |
| | @@ -58920,130 +59784,54 @@ |
| 58920 | 59784 | ** |
| 58921 | 59785 | ** If this is an auto-vacuum database, the call to copyNodeContent() |
| 58922 | 59786 | ** sets all pointer-map entries corresponding to database image pages |
| 58923 | 59787 | ** for which the pointer is stored within the content being copied. |
| 58924 | 59788 | ** |
| 58925 | | - ** The second assert below verifies that the child page is defragmented |
| 58926 | | - ** (it must be, as it was just reconstructed using assemblePage()). This |
| 58927 | | - ** is important if the parent page happens to be page 1 of the database |
| 58928 | | - ** image. */ |
| 59789 | + ** It is critical that the child page be defragmented before being |
| 59790 | + ** copied into the parent, because if the parent is page 1 then it will |
| 59791 | + ** by smaller than the child due to the database header, and so all the |
| 59792 | + ** free space needs to be up front. |
| 59793 | + */ |
| 58929 | 59794 | assert( nNew==1 ); |
| 59795 | + rc = defragmentPage(apNew[0]); |
| 59796 | + testcase( rc!=SQLITE_OK ); |
| 58930 | 59797 | assert( apNew[0]->nFree == |
| 58931 | | - (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) |
| 59798 | + (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) |
| 59799 | + || rc!=SQLITE_OK |
| 58932 | 59800 | ); |
| 58933 | 59801 | copyNodeContent(apNew[0], pParent, &rc); |
| 58934 | 59802 | freePage(apNew[0], &rc); |
| 58935 | | - }else if( ISAUTOVACUUM ){ |
| 58936 | | - /* Fix the pointer-map entries for all the cells that were shifted around. |
| 58937 | | - ** There are several different types of pointer-map entries that need to |
| 58938 | | - ** be dealt with by this routine. Some of these have been set already, but |
| 58939 | | - ** many have not. The following is a summary: |
| 58940 | | - ** |
| 58941 | | - ** 1) The entries associated with new sibling pages that were not |
| 58942 | | - ** siblings when this function was called. These have already |
| 58943 | | - ** been set. We don't need to worry about old siblings that were |
| 58944 | | - ** moved to the free-list - the freePage() code has taken care |
| 58945 | | - ** of those. |
| 58946 | | - ** |
| 58947 | | - ** 2) The pointer-map entries associated with the first overflow |
| 58948 | | - ** page in any overflow chains used by new divider cells. These |
| 58949 | | - ** have also already been taken care of by the insertCell() code. |
| 58950 | | - ** |
| 58951 | | - ** 3) If the sibling pages are not leaves, then the child pages of |
| 58952 | | - ** cells stored on the sibling pages may need to be updated. |
| 58953 | | - ** |
| 58954 | | - ** 4) If the sibling pages are not internal intkey nodes, then any |
| 58955 | | - ** overflow pages used by these cells may need to be updated |
| 58956 | | - ** (internal intkey nodes never contain pointers to overflow pages). |
| 58957 | | - ** |
| 58958 | | - ** 5) If the sibling pages are not leaves, then the pointer-map |
| 58959 | | - ** entries for the right-child pages of each sibling may need |
| 58960 | | - ** to be updated. |
| 58961 | | - ** |
| 58962 | | - ** Cases 1 and 2 are dealt with above by other code. The next |
| 58963 | | - ** block deals with cases 3 and 4 and the one after that, case 5. Since |
| 58964 | | - ** setting a pointer map entry is a relatively expensive operation, this |
| 58965 | | - ** code only sets pointer map entries for child or overflow pages that have |
| 58966 | | - ** actually moved between pages. */ |
| 58967 | | - MemPage *pNew = apNew[0]; |
| 58968 | | - MemPage *pOld = apCopy[0]; |
| 58969 | | - int nOverflow = pOld->nOverflow; |
| 58970 | | - int iNextOld = pOld->nCell + nOverflow; |
| 58971 | | - int iOverflow = (nOverflow ? pOld->aiOvfl[0] : -1); |
| 58972 | | - j = 0; /* Current 'old' sibling page */ |
| 58973 | | - k = 0; /* Current 'new' sibling page */ |
| 58974 | | - for(i=0; i<nCell; i++){ |
| 58975 | | - int isDivider = 0; |
| 58976 | | - while( i==iNextOld ){ |
| 58977 | | - /* Cell i is the cell immediately following the last cell on old |
| 58978 | | - ** sibling page j. If the siblings are not leaf pages of an |
| 58979 | | - ** intkey b-tree, then cell i was a divider cell. */ |
| 58980 | | - assert( j+1 < ArraySize(apCopy) ); |
| 58981 | | - assert( j+1 < nOld ); |
| 58982 | | - pOld = apCopy[++j]; |
| 58983 | | - iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow; |
| 58984 | | - if( pOld->nOverflow ){ |
| 58985 | | - nOverflow = pOld->nOverflow; |
| 58986 | | - iOverflow = i + !leafData + pOld->aiOvfl[0]; |
| 58987 | | - } |
| 58988 | | - isDivider = !leafData; |
| 58989 | | - } |
| 58990 | | - |
| 58991 | | - assert(nOverflow>0 || iOverflow<i ); |
| 58992 | | - assert(nOverflow<2 || pOld->aiOvfl[0]==pOld->aiOvfl[1]-1); |
| 58993 | | - assert(nOverflow<3 || pOld->aiOvfl[1]==pOld->aiOvfl[2]-1); |
| 58994 | | - if( i==iOverflow ){ |
| 58995 | | - isDivider = 1; |
| 58996 | | - if( (--nOverflow)>0 ){ |
| 58997 | | - iOverflow++; |
| 58998 | | - } |
| 58999 | | - } |
| 59000 | | - |
| 59001 | | - if( i==cntNew[k] ){ |
| 59002 | | - /* Cell i is the cell immediately following the last cell on new |
| 59003 | | - ** sibling page k. If the siblings are not leaf pages of an |
| 59004 | | - ** intkey b-tree, then cell i is a divider cell. */ |
| 59005 | | - pNew = apNew[++k]; |
| 59006 | | - if( !leafData ) continue; |
| 59007 | | - } |
| 59008 | | - assert( j<nOld ); |
| 59009 | | - assert( k<nNew ); |
| 59010 | | - |
| 59011 | | - /* If the cell was originally divider cell (and is not now) or |
| 59012 | | - ** an overflow cell, or if the cell was located on a different sibling |
| 59013 | | - ** page before the balancing, then the pointer map entries associated |
| 59014 | | - ** with any child or overflow pages need to be updated. */ |
| 59015 | | - if( isDivider || pOld->pgno!=pNew->pgno ){ |
| 59016 | | - if( !leafCorrection ){ |
| 59017 | | - ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc); |
| 59018 | | - } |
| 59019 | | - if( szCell[i]>pNew->minLocal ){ |
| 59020 | | - ptrmapPutOvflPtr(pNew, apCell[i], &rc); |
| 59021 | | - } |
| 59022 | | - } |
| 59023 | | - } |
| 59024 | | - |
| 59025 | | - if( !leafCorrection ){ |
| 59026 | | - for(i=0; i<nNew; i++){ |
| 59027 | | - u32 key = get4byte(&apNew[i]->aData[8]); |
| 59028 | | - ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); |
| 59029 | | - } |
| 59030 | | - } |
| 59803 | + }else if( ISAUTOVACUUM && !leafCorrection ){ |
| 59804 | + /* Fix the pointer map entries associated with the right-child of each |
| 59805 | + ** sibling page. All other pointer map entries have already been taken |
| 59806 | + ** care of. */ |
| 59807 | + for(i=0; i<nNew; i++){ |
| 59808 | + u32 key = get4byte(&apNew[i]->aData[8]); |
| 59809 | + ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); |
| 59810 | + } |
| 59811 | + } |
| 59812 | + |
| 59813 | + assert( pParent->isInit ); |
| 59814 | + TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
| 59815 | + nOld, nNew, nCell)); |
| 59816 | + |
| 59817 | + /* Free any old pages that were not reused as new pages. |
| 59818 | + */ |
| 59819 | + for(i=nNew; i<nOld; i++){ |
| 59820 | + freePage(apOld[i], &rc); |
| 59821 | + } |
| 59031 | 59822 | |
| 59032 | 59823 | #if 0 |
| 59824 | + if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){ |
| 59033 | 59825 | /* The ptrmapCheckPages() contains assert() statements that verify that |
| 59034 | 59826 | ** all pointer map pages are set correctly. This is helpful while |
| 59035 | 59827 | ** debugging. This is usually disabled because a corrupt database may |
| 59036 | 59828 | ** cause an assert() statement to fail. */ |
| 59037 | 59829 | ptrmapCheckPages(apNew, nNew); |
| 59038 | 59830 | ptrmapCheckPages(&pParent, 1); |
| 59831 | + } |
| 59039 | 59832 | #endif |
| 59040 | | - } |
| 59041 | | - |
| 59042 | | - assert( pParent->isInit ); |
| 59043 | | - TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
| 59044 | | - nOld, nNew, nCell)); |
| 59045 | 59833 | |
| 59046 | 59834 | /* |
| 59047 | 59835 | ** Cleanup before returning. |
| 59048 | 59836 | */ |
| 59049 | 59837 | balance_cleanup: |
| | @@ -60424,12 +61212,18 @@ |
| 60424 | 61212 | }else{ |
| 60425 | 61213 | int contentOffset = get2byteNotZero(&data[hdr+5]); |
| 60426 | 61214 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
| 60427 | 61215 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 60428 | 61216 | memset(hit, 1, contentOffset); |
| 61217 | + /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
| 61218 | + ** number of cells on the page. */ |
| 60429 | 61219 | nCell = get2byte(&data[hdr+3]); |
| 61220 | + /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page |
| 61221 | + ** immediately follows the b-tree page header. */ |
| 60430 | 61222 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 61223 | + /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte |
| 61224 | + ** integer offsets to the cell contents. */ |
| 60431 | 61225 | for(i=0; i<nCell; i++){ |
| 60432 | 61226 | int pc = get2byte(&data[cellStart+i*2]); |
| 60433 | 61227 | u32 size = 65536; |
| 60434 | 61228 | int j; |
| 60435 | 61229 | if( pc<=usableSize-4 ){ |
| | @@ -60441,18 +61235,27 @@ |
| 60441 | 61235 | "Corruption detected in cell %d on page %d",i,iPage); |
| 60442 | 61236 | }else{ |
| 60443 | 61237 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 60444 | 61238 | } |
| 60445 | 61239 | } |
| 61240 | + /* EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header |
| 61241 | + ** is the offset of the first freeblock, or zero if there are no |
| 61242 | + ** freeblocks on the page. */ |
| 60446 | 61243 | i = get2byte(&data[hdr+1]); |
| 60447 | 61244 | while( i>0 ){ |
| 60448 | 61245 | int size, j; |
| 60449 | 61246 | assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 60450 | 61247 | size = get2byte(&data[i+2]); |
| 60451 | 61248 | assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */ |
| 60452 | 61249 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 61250 | + /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a |
| 61251 | + ** big-endian integer which is the offset in the b-tree page of the next |
| 61252 | + ** freeblock in the chain, or zero if the freeblock is the last on the |
| 61253 | + ** chain. */ |
| 60453 | 61254 | j = get2byte(&data[i]); |
| 61255 | + /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of |
| 61256 | + ** increasing offset. */ |
| 60454 | 61257 | assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ |
| 60455 | 61258 | assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 60456 | 61259 | i = j; |
| 60457 | 61260 | } |
| 60458 | 61261 | for(i=cnt=0; i<usableSize; i++){ |
| | @@ -60462,10 +61265,15 @@ |
| 60462 | 61265 | checkAppendMsg(pCheck, |
| 60463 | 61266 | "Multiple uses for byte %d of page %d", i, iPage); |
| 60464 | 61267 | break; |
| 60465 | 61268 | } |
| 60466 | 61269 | } |
| 61270 | + /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments |
| 61271 | + ** is stored in the fifth field of the b-tree page header. |
| 61272 | + ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the |
| 61273 | + ** number of fragmented free bytes within the cell content area. |
| 61274 | + */ |
| 60467 | 61275 | if( cnt!=data[hdr+7] ){ |
| 60468 | 61276 | checkAppendMsg(pCheck, |
| 60469 | 61277 | "Fragmentation of %d bytes reported as %d on page %d", |
| 60470 | 61278 | cnt, data[hdr+7], iPage); |
| 60471 | 61279 | } |
| | @@ -60865,10 +61673,15 @@ |
| 60865 | 61673 | */ |
| 60866 | 61674 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){ |
| 60867 | 61675 | return (p->pBt->btsFlags & BTS_READ_ONLY)!=0; |
| 60868 | 61676 | } |
| 60869 | 61677 | |
| 61678 | +/* |
| 61679 | +** Return the size of the header added to each page by this module. |
| 61680 | +*/ |
| 61681 | +SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return sizeof(MemPage); } |
| 61682 | + |
| 60870 | 61683 | /************** End of btree.c ***********************************************/ |
| 60871 | 61684 | /************** Begin file backup.c ******************************************/ |
| 60872 | 61685 | /* |
| 60873 | 61686 | ** 2009 January 28 |
| 60874 | 61687 | ** |
| | @@ -60988,10 +61801,24 @@ |
| 60988 | 61801 | static int setDestPgsz(sqlite3_backup *p){ |
| 60989 | 61802 | int rc; |
| 60990 | 61803 | rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0); |
| 60991 | 61804 | return rc; |
| 60992 | 61805 | } |
| 61806 | + |
| 61807 | +/* |
| 61808 | +** Check that there is no open read-transaction on the b-tree passed as the |
| 61809 | +** second argument. If there is not, return SQLITE_OK. Otherwise, if there |
| 61810 | +** is an open read-transaction, return SQLITE_ERROR and leave an error |
| 61811 | +** message in database handle db. |
| 61812 | +*/ |
| 61813 | +static int checkReadTransaction(sqlite3 *db, Btree *p){ |
| 61814 | + if( sqlite3BtreeIsInReadTrans(p) ){ |
| 61815 | + sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use"); |
| 61816 | + return SQLITE_ERROR; |
| 61817 | + } |
| 61818 | + return SQLITE_OK; |
| 61819 | +} |
| 60993 | 61820 | |
| 60994 | 61821 | /* |
| 60995 | 61822 | ** Create an sqlite3_backup process to copy the contents of zSrcDb from |
| 60996 | 61823 | ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return |
| 60997 | 61824 | ** a pointer to the new sqlite3_backup object. |
| | @@ -61004,10 +61831,17 @@ |
| 61004 | 61831 | const char *zDestDb, /* Name of database within pDestDb */ |
| 61005 | 61832 | sqlite3* pSrcDb, /* Database connection to read from */ |
| 61006 | 61833 | const char *zSrcDb /* Name of database within pSrcDb */ |
| 61007 | 61834 | ){ |
| 61008 | 61835 | sqlite3_backup *p; /* Value to return */ |
| 61836 | + |
| 61837 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 61838 | + if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){ |
| 61839 | + (void)SQLITE_MISUSE_BKPT; |
| 61840 | + return 0; |
| 61841 | + } |
| 61842 | +#endif |
| 61009 | 61843 | |
| 61010 | 61844 | /* Lock the source database handle. The destination database |
| 61011 | 61845 | ** handle is not locked in this routine, but it is locked in |
| 61012 | 61846 | ** sqlite3_backup_step(). The user is required to ensure that no |
| 61013 | 61847 | ** other thread accesses the destination handle for the duration |
| | @@ -61041,16 +61875,19 @@ |
| 61041 | 61875 | p->pDestDb = pDestDb; |
| 61042 | 61876 | p->pSrcDb = pSrcDb; |
| 61043 | 61877 | p->iNext = 1; |
| 61044 | 61878 | p->isAttached = 0; |
| 61045 | 61879 | |
| 61046 | | - if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){ |
| 61880 | + if( 0==p->pSrc || 0==p->pDest |
| 61881 | + || setDestPgsz(p)==SQLITE_NOMEM |
| 61882 | + || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK |
| 61883 | + ){ |
| 61047 | 61884 | /* One (or both) of the named databases did not exist or an OOM |
| 61048 | | - ** error was hit. The error has already been written into the |
| 61049 | | - ** pDestDb handle. All that is left to do here is free the |
| 61050 | | - ** sqlite3_backup structure. |
| 61051 | | - */ |
| 61885 | + ** error was hit. Or there is a transaction open on the destination |
| 61886 | + ** database. The error has already been written into the pDestDb |
| 61887 | + ** handle. All that is left to do here is free the sqlite3_backup |
| 61888 | + ** structure. */ |
| 61052 | 61889 | sqlite3_free(p); |
| 61053 | 61890 | p = 0; |
| 61054 | 61891 | } |
| 61055 | 61892 | } |
| 61056 | 61893 | if( p ){ |
| | @@ -61201,10 +62038,13 @@ |
| 61201 | 62038 | int rc; |
| 61202 | 62039 | int destMode; /* Destination journal mode */ |
| 61203 | 62040 | int pgszSrc = 0; /* Source page size */ |
| 61204 | 62041 | int pgszDest = 0; /* Destination page size */ |
| 61205 | 62042 | |
| 62043 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 62044 | + if( p==0 ) return SQLITE_MISUSE_BKPT; |
| 62045 | +#endif |
| 61206 | 62046 | sqlite3_mutex_enter(p->pSrcDb->mutex); |
| 61207 | 62047 | sqlite3BtreeEnter(p->pSrc); |
| 61208 | 62048 | if( p->pDestDb ){ |
| 61209 | 62049 | sqlite3_mutex_enter(p->pDestDb->mutex); |
| 61210 | 62050 | } |
| | @@ -61464,11 +62304,11 @@ |
| 61464 | 62304 | } |
| 61465 | 62305 | *pp = p->pNext; |
| 61466 | 62306 | } |
| 61467 | 62307 | |
| 61468 | 62308 | /* If a transaction is still open on the Btree, roll it back. */ |
| 61469 | | - sqlite3BtreeRollback(p->pDest, SQLITE_OK); |
| 62309 | + sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0); |
| 61470 | 62310 | |
| 61471 | 62311 | /* Set the error code of the destination database handle. */ |
| 61472 | 62312 | rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc; |
| 61473 | 62313 | if( p->pDestDb ){ |
| 61474 | 62314 | sqlite3Error(p->pDestDb, rc); |
| | @@ -61490,18 +62330,30 @@ |
| 61490 | 62330 | /* |
| 61491 | 62331 | ** Return the number of pages still to be backed up as of the most recent |
| 61492 | 62332 | ** call to sqlite3_backup_step(). |
| 61493 | 62333 | */ |
| 61494 | 62334 | SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){ |
| 62335 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 62336 | + if( p==0 ){ |
| 62337 | + (void)SQLITE_MISUSE_BKPT; |
| 62338 | + return 0; |
| 62339 | + } |
| 62340 | +#endif |
| 61495 | 62341 | return p->nRemaining; |
| 61496 | 62342 | } |
| 61497 | 62343 | |
| 61498 | 62344 | /* |
| 61499 | 62345 | ** Return the total number of pages in the source database as of the most |
| 61500 | 62346 | ** recent call to sqlite3_backup_step(). |
| 61501 | 62347 | */ |
| 61502 | 62348 | SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){ |
| 62349 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 62350 | + if( p==0 ){ |
| 62351 | + (void)SQLITE_MISUSE_BKPT; |
| 62352 | + return 0; |
| 62353 | + } |
| 62354 | +#endif |
| 61503 | 62355 | return p->nPagecount; |
| 61504 | 62356 | } |
| 61505 | 62357 | |
| 61506 | 62358 | /* |
| 61507 | 62359 | ** This function is called after the contents of page iPage of the |
| | @@ -63788,10 +64640,38 @@ |
| 63788 | 64640 | } |
| 63789 | 64641 | p->nOp += nOp; |
| 63790 | 64642 | } |
| 63791 | 64643 | return addr; |
| 63792 | 64644 | } |
| 64645 | + |
| 64646 | +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 64647 | +/* |
| 64648 | +** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus(). |
| 64649 | +*/ |
| 64650 | +SQLITE_PRIVATE void sqlite3VdbeScanStatus( |
| 64651 | + Vdbe *p, /* VM to add scanstatus() to */ |
| 64652 | + int addrExplain, /* Address of OP_Explain (or 0) */ |
| 64653 | + int addrLoop, /* Address of loop counter */ |
| 64654 | + int addrVisit, /* Address of rows visited counter */ |
| 64655 | + LogEst nEst, /* Estimated number of output rows */ |
| 64656 | + const char *zName /* Name of table or index being scanned */ |
| 64657 | +){ |
| 64658 | + int nByte = (p->nScan+1) * sizeof(ScanStatus); |
| 64659 | + ScanStatus *aNew; |
| 64660 | + aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte); |
| 64661 | + if( aNew ){ |
| 64662 | + ScanStatus *pNew = &aNew[p->nScan++]; |
| 64663 | + pNew->addrExplain = addrExplain; |
| 64664 | + pNew->addrLoop = addrLoop; |
| 64665 | + pNew->addrVisit = addrVisit; |
| 64666 | + pNew->nEst = nEst; |
| 64667 | + pNew->zName = sqlite3DbStrDup(p->db, zName); |
| 64668 | + p->aScan = aNew; |
| 64669 | + } |
| 64670 | +} |
| 64671 | +#endif |
| 64672 | + |
| 63793 | 64673 | |
| 63794 | 64674 | /* |
| 63795 | 64675 | ** Change the value of the P1 operand for a specific instruction. |
| 63796 | 64676 | ** This routine is useful when a large program is loaded from a |
| 63797 | 64677 | ** static array using sqlite3VdbeAddOpList but we want to make a |
| | @@ -64887,10 +65767,13 @@ |
| 64887 | 65767 | p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); |
| 64888 | 65768 | p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); |
| 64889 | 65769 | p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), |
| 64890 | 65770 | &zCsr, zEnd, &nByte); |
| 64891 | 65771 | p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte); |
| 65772 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 65773 | + p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), &zCsr, zEnd, &nByte); |
| 65774 | +#endif |
| 64892 | 65775 | if( nByte ){ |
| 64893 | 65776 | p->pFree = sqlite3DbMallocZero(db, nByte); |
| 64894 | 65777 | } |
| 64895 | 65778 | zCsr = p->pFree; |
| 64896 | 65779 | zEnd = &zCsr[nByte]; |
| | @@ -64903,11 +65786,11 @@ |
| 64903 | 65786 | for(n=0; n<nVar; n++){ |
| 64904 | 65787 | p->aVar[n].flags = MEM_Null; |
| 64905 | 65788 | p->aVar[n].db = db; |
| 64906 | 65789 | } |
| 64907 | 65790 | } |
| 64908 | | - if( p->azVar ){ |
| 65791 | + if( p->azVar && pParse->nzVar>0 ){ |
| 64909 | 65792 | p->nzVar = pParse->nzVar; |
| 64910 | 65793 | memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0])); |
| 64911 | 65794 | memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0])); |
| 64912 | 65795 | } |
| 64913 | 65796 | if( p->aMem ){ |
| | @@ -64954,10 +65837,13 @@ |
| 64954 | 65837 | ** is used, for example, when a trigger sub-program is halted to restore |
| 64955 | 65838 | ** control to the main program. |
| 64956 | 65839 | */ |
| 64957 | 65840 | SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ |
| 64958 | 65841 | Vdbe *v = pFrame->v; |
| 65842 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 65843 | + v->anExec = pFrame->anExec; |
| 65844 | +#endif |
| 64959 | 65845 | v->aOnceFlag = pFrame->aOnceFlag; |
| 64960 | 65846 | v->nOnceFlag = pFrame->nOnceFlag; |
| 64961 | 65847 | v->aOp = pFrame->aOp; |
| 64962 | 65848 | v->nOp = pFrame->nOp; |
| 64963 | 65849 | v->aMem = pFrame->aMem; |
| | @@ -64964,10 +65850,11 @@ |
| 64964 | 65850 | v->nMem = pFrame->nMem; |
| 64965 | 65851 | v->apCsr = pFrame->apCsr; |
| 64966 | 65852 | v->nCursor = pFrame->nCursor; |
| 64967 | 65853 | v->db->lastRowid = pFrame->lastRowid; |
| 64968 | 65854 | v->nChange = pFrame->nChange; |
| 65855 | + v->db->nChange = pFrame->nDbChange; |
| 64969 | 65856 | return pFrame->pc; |
| 64970 | 65857 | } |
| 64971 | 65858 | |
| 64972 | 65859 | /* |
| 64973 | 65860 | ** Close all cursors. |
| | @@ -65531,10 +66418,11 @@ |
| 65531 | 66418 | ** so, abort any other statements this handle currently has active. |
| 65532 | 66419 | */ |
| 65533 | 66420 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 65534 | 66421 | sqlite3CloseSavepoints(db); |
| 65535 | 66422 | db->autoCommit = 1; |
| 66423 | + p->nChange = 0; |
| 65536 | 66424 | } |
| 65537 | 66425 | } |
| 65538 | 66426 | } |
| 65539 | 66427 | |
| 65540 | 66428 | /* Check for immediate foreign key violations. */ |
| | @@ -65571,18 +66459,20 @@ |
| 65571 | 66459 | sqlite3VdbeLeave(p); |
| 65572 | 66460 | return SQLITE_BUSY; |
| 65573 | 66461 | }else if( rc!=SQLITE_OK ){ |
| 65574 | 66462 | p->rc = rc; |
| 65575 | 66463 | sqlite3RollbackAll(db, SQLITE_OK); |
| 66464 | + p->nChange = 0; |
| 65576 | 66465 | }else{ |
| 65577 | 66466 | db->nDeferredCons = 0; |
| 65578 | 66467 | db->nDeferredImmCons = 0; |
| 65579 | 66468 | db->flags &= ~SQLITE_DeferFKs; |
| 65580 | 66469 | sqlite3CommitInternalChanges(db); |
| 65581 | 66470 | } |
| 65582 | 66471 | }else{ |
| 65583 | 66472 | sqlite3RollbackAll(db, SQLITE_OK); |
| 66473 | + p->nChange = 0; |
| 65584 | 66474 | } |
| 65585 | 66475 | db->nStatement = 0; |
| 65586 | 66476 | }else if( eStatementOp==0 ){ |
| 65587 | 66477 | if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ |
| 65588 | 66478 | eStatementOp = SAVEPOINT_RELEASE; |
| | @@ -65590,10 +66480,11 @@ |
| 65590 | 66480 | eStatementOp = SAVEPOINT_ROLLBACK; |
| 65591 | 66481 | }else{ |
| 65592 | 66482 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 65593 | 66483 | sqlite3CloseSavepoints(db); |
| 65594 | 66484 | db->autoCommit = 1; |
| 66485 | + p->nChange = 0; |
| 65595 | 66486 | } |
| 65596 | 66487 | } |
| 65597 | 66488 | |
| 65598 | 66489 | /* If eStatementOp is non-zero, then a statement transaction needs to |
| 65599 | 66490 | ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to |
| | @@ -65610,10 +66501,11 @@ |
| 65610 | 66501 | p->zErrMsg = 0; |
| 65611 | 66502 | } |
| 65612 | 66503 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 65613 | 66504 | sqlite3CloseSavepoints(db); |
| 65614 | 66505 | db->autoCommit = 1; |
| 66506 | + p->nChange = 0; |
| 65615 | 66507 | } |
| 65616 | 66508 | } |
| 65617 | 66509 | |
| 65618 | 66510 | /* If this was an INSERT, UPDATE or DELETE and no statement transaction |
| 65619 | 66511 | ** has been rolled back, update the database connection change-counter. |
| | @@ -65871,10 +66763,16 @@ |
| 65871 | 66763 | for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); |
| 65872 | 66764 | vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 65873 | 66765 | sqlite3DbFree(db, p->aColName); |
| 65874 | 66766 | sqlite3DbFree(db, p->zSql); |
| 65875 | 66767 | sqlite3DbFree(db, p->pFree); |
| 66768 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 66769 | + for(i=0; i<p->nScan; i++){ |
| 66770 | + sqlite3DbFree(db, p->aScan[i].zName); |
| 66771 | + } |
| 66772 | + sqlite3DbFree(db, p->aScan); |
| 66773 | +#endif |
| 65876 | 66774 | } |
| 65877 | 66775 | |
| 65878 | 66776 | /* |
| 65879 | 66777 | ** Delete an entire VDBE. |
| 65880 | 66778 | */ |
| | @@ -66029,13 +66927,11 @@ |
| 66029 | 66927 | /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
| 66030 | 66928 | # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) |
| 66031 | 66929 | i64 i = pMem->u.i; |
| 66032 | 66930 | u64 u; |
| 66033 | 66931 | if( i<0 ){ |
| 66034 | | - if( i<(-MAX_6BYTE) ) return 6; |
| 66035 | | - /* Previous test prevents: u = -(-9223372036854775808) */ |
| 66036 | | - u = -i; |
| 66932 | + u = ~i; |
| 66037 | 66933 | }else{ |
| 66038 | 66934 | u = i; |
| 66039 | 66935 | } |
| 66040 | 66936 | if( u<=127 ){ |
| 66041 | 66937 | return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1; |
| | @@ -66197,14 +67093,18 @@ |
| 66197 | 67093 | ){ |
| 66198 | 67094 | u64 x = FOUR_BYTE_UINT(buf); |
| 66199 | 67095 | u32 y = FOUR_BYTE_UINT(buf+4); |
| 66200 | 67096 | x = (x<<32) + y; |
| 66201 | 67097 | if( serial_type==6 ){ |
| 67098 | + /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit |
| 67099 | + ** twos-complement integer. */ |
| 66202 | 67100 | pMem->u.i = *(i64*)&x; |
| 66203 | 67101 | pMem->flags = MEM_Int; |
| 66204 | 67102 | testcase( pMem->u.i<0 ); |
| 66205 | 67103 | }else{ |
| 67104 | + /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit |
| 67105 | + ** floating point number. */ |
| 66206 | 67106 | #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) |
| 66207 | 67107 | /* Verify that integers and floating point values use the same |
| 66208 | 67108 | ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is |
| 66209 | 67109 | ** defined that 64-bit floating point values really are mixed |
| 66210 | 67110 | ** endian. |
| | @@ -66228,39 +67128,50 @@ |
| 66228 | 67128 | Mem *pMem /* Memory cell to write value into */ |
| 66229 | 67129 | ){ |
| 66230 | 67130 | switch( serial_type ){ |
| 66231 | 67131 | case 10: /* Reserved for future use */ |
| 66232 | 67132 | case 11: /* Reserved for future use */ |
| 66233 | | - case 0: { /* NULL */ |
| 67133 | + case 0: { /* Null */ |
| 67134 | + /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */ |
| 66234 | 67135 | pMem->flags = MEM_Null; |
| 66235 | 67136 | break; |
| 66236 | 67137 | } |
| 66237 | | - case 1: { /* 1-byte signed integer */ |
| 67138 | + case 1: { |
| 67139 | + /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement |
| 67140 | + ** integer. */ |
| 66238 | 67141 | pMem->u.i = ONE_BYTE_INT(buf); |
| 66239 | 67142 | pMem->flags = MEM_Int; |
| 66240 | 67143 | testcase( pMem->u.i<0 ); |
| 66241 | 67144 | return 1; |
| 66242 | 67145 | } |
| 66243 | 67146 | case 2: { /* 2-byte signed integer */ |
| 67147 | + /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit |
| 67148 | + ** twos-complement integer. */ |
| 66244 | 67149 | pMem->u.i = TWO_BYTE_INT(buf); |
| 66245 | 67150 | pMem->flags = MEM_Int; |
| 66246 | 67151 | testcase( pMem->u.i<0 ); |
| 66247 | 67152 | return 2; |
| 66248 | 67153 | } |
| 66249 | 67154 | case 3: { /* 3-byte signed integer */ |
| 67155 | + /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit |
| 67156 | + ** twos-complement integer. */ |
| 66250 | 67157 | pMem->u.i = THREE_BYTE_INT(buf); |
| 66251 | 67158 | pMem->flags = MEM_Int; |
| 66252 | 67159 | testcase( pMem->u.i<0 ); |
| 66253 | 67160 | return 3; |
| 66254 | 67161 | } |
| 66255 | 67162 | case 4: { /* 4-byte signed integer */ |
| 67163 | + /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit |
| 67164 | + ** twos-complement integer. */ |
| 66256 | 67165 | pMem->u.i = FOUR_BYTE_INT(buf); |
| 66257 | 67166 | pMem->flags = MEM_Int; |
| 66258 | 67167 | testcase( pMem->u.i<0 ); |
| 66259 | 67168 | return 4; |
| 66260 | 67169 | } |
| 66261 | 67170 | case 5: { /* 6-byte signed integer */ |
| 67171 | + /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit |
| 67172 | + ** twos-complement integer. */ |
| 66262 | 67173 | pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); |
| 66263 | 67174 | pMem->flags = MEM_Int; |
| 66264 | 67175 | testcase( pMem->u.i<0 ); |
| 66265 | 67176 | return 6; |
| 66266 | 67177 | } |
| | @@ -66270,15 +67181,21 @@ |
| 66270 | 67181 | ** to avoid having to move the frame pointer in the common case */ |
| 66271 | 67182 | return serialGet(buf,serial_type,pMem); |
| 66272 | 67183 | } |
| 66273 | 67184 | case 8: /* Integer 0 */ |
| 66274 | 67185 | case 9: { /* Integer 1 */ |
| 67186 | + /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */ |
| 67187 | + /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */ |
| 66275 | 67188 | pMem->u.i = serial_type-8; |
| 66276 | 67189 | pMem->flags = MEM_Int; |
| 66277 | 67190 | return 0; |
| 66278 | 67191 | } |
| 66279 | 67192 | default: { |
| 67193 | + /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in |
| 67194 | + ** length. |
| 67195 | + ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and |
| 67196 | + ** (N-13)/2 bytes in length. */ |
| 66280 | 67197 | static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; |
| 66281 | 67198 | pMem->z = (char *)buf; |
| 66282 | 67199 | pMem->n = (serial_type-12)/2; |
| 66283 | 67200 | pMem->flags = aFlag[serial_type&1]; |
| 66284 | 67201 | return pMem->n; |
| | @@ -68238,15 +69155,23 @@ |
| 68238 | 69155 | sqlite3_stmt *pStmt, |
| 68239 | 69156 | int N, |
| 68240 | 69157 | const void *(*xFunc)(Mem*), |
| 68241 | 69158 | int useType |
| 68242 | 69159 | ){ |
| 68243 | | - const void *ret = 0; |
| 68244 | | - Vdbe *p = (Vdbe *)pStmt; |
| 69160 | + const void *ret; |
| 69161 | + Vdbe *p; |
| 68245 | 69162 | int n; |
| 68246 | | - sqlite3 *db = p->db; |
| 68247 | | - |
| 69163 | + sqlite3 *db; |
| 69164 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 69165 | + if( pStmt==0 ){ |
| 69166 | + (void)SQLITE_MISUSE_BKPT; |
| 69167 | + return 0; |
| 69168 | + } |
| 69169 | +#endif |
| 69170 | + ret = 0; |
| 69171 | + p = (Vdbe *)pStmt; |
| 69172 | + db = p->db; |
| 68248 | 69173 | assert( db!=0 ); |
| 68249 | 69174 | n = sqlite3_column_count(pStmt); |
| 68250 | 69175 | if( N<n && N>=0 ){ |
| 68251 | 69176 | N += useType*n; |
| 68252 | 69177 | sqlite3_mutex_enter(db->mutex); |
| | @@ -68707,10 +69632,16 @@ |
| 68707 | 69632 | ** prepared statement for the database connection. Return NULL if there |
| 68708 | 69633 | ** are no more. |
| 68709 | 69634 | */ |
| 68710 | 69635 | SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ |
| 68711 | 69636 | sqlite3_stmt *pNext; |
| 69637 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 69638 | + if( !sqlite3SafetyCheckOk(pDb) ){ |
| 69639 | + (void)SQLITE_MISUSE_BKPT; |
| 69640 | + return 0; |
| 69641 | + } |
| 69642 | +#endif |
| 68712 | 69643 | sqlite3_mutex_enter(pDb->mutex); |
| 68713 | 69644 | if( pStmt==0 ){ |
| 68714 | 69645 | pNext = (sqlite3_stmt*)pDb->pVdbe; |
| 68715 | 69646 | }else{ |
| 68716 | 69647 | pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; |
| | @@ -68722,15 +69653,91 @@ |
| 68722 | 69653 | /* |
| 68723 | 69654 | ** Return the value of a status counter for a prepared statement |
| 68724 | 69655 | */ |
| 68725 | 69656 | SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ |
| 68726 | 69657 | Vdbe *pVdbe = (Vdbe*)pStmt; |
| 68727 | | - u32 v = pVdbe->aCounter[op]; |
| 69658 | + u32 v; |
| 69659 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 69660 | + if( !pStmt ){ |
| 69661 | + (void)SQLITE_MISUSE_BKPT; |
| 69662 | + return 0; |
| 69663 | + } |
| 69664 | +#endif |
| 69665 | + v = pVdbe->aCounter[op]; |
| 68728 | 69666 | if( resetFlag ) pVdbe->aCounter[op] = 0; |
| 68729 | 69667 | return (int)v; |
| 68730 | 69668 | } |
| 68731 | 69669 | |
| 69670 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 69671 | +/* |
| 69672 | +** Return status data for a single loop within query pStmt. |
| 69673 | +*/ |
| 69674 | +SQLITE_API int sqlite3_stmt_scanstatus( |
| 69675 | + sqlite3_stmt *pStmt, /* Prepared statement being queried */ |
| 69676 | + int idx, /* Index of loop to report on */ |
| 69677 | + int iScanStatusOp, /* Which metric to return */ |
| 69678 | + void *pOut /* OUT: Write the answer here */ |
| 69679 | +){ |
| 69680 | + Vdbe *p = (Vdbe*)pStmt; |
| 69681 | + ScanStatus *pScan; |
| 69682 | + if( idx<0 || idx>=p->nScan ) return 1; |
| 69683 | + pScan = &p->aScan[idx]; |
| 69684 | + switch( iScanStatusOp ){ |
| 69685 | + case SQLITE_SCANSTAT_NLOOP: { |
| 69686 | + *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; |
| 69687 | + break; |
| 69688 | + } |
| 69689 | + case SQLITE_SCANSTAT_NVISIT: { |
| 69690 | + *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; |
| 69691 | + break; |
| 69692 | + } |
| 69693 | + case SQLITE_SCANSTAT_EST: { |
| 69694 | + double r = 1.0; |
| 69695 | + LogEst x = pScan->nEst; |
| 69696 | + while( x<100 ){ |
| 69697 | + x += 10; |
| 69698 | + r *= 0.5; |
| 69699 | + } |
| 69700 | + *(double*)pOut = r*sqlite3LogEstToInt(x); |
| 69701 | + break; |
| 69702 | + } |
| 69703 | + case SQLITE_SCANSTAT_NAME: { |
| 69704 | + *(const char**)pOut = pScan->zName; |
| 69705 | + break; |
| 69706 | + } |
| 69707 | + case SQLITE_SCANSTAT_EXPLAIN: { |
| 69708 | + if( pScan->addrExplain ){ |
| 69709 | + *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; |
| 69710 | + }else{ |
| 69711 | + *(const char**)pOut = 0; |
| 69712 | + } |
| 69713 | + break; |
| 69714 | + } |
| 69715 | + case SQLITE_SCANSTAT_SELECTID: { |
| 69716 | + if( pScan->addrExplain ){ |
| 69717 | + *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; |
| 69718 | + }else{ |
| 69719 | + *(int*)pOut = -1; |
| 69720 | + } |
| 69721 | + break; |
| 69722 | + } |
| 69723 | + default: { |
| 69724 | + return 1; |
| 69725 | + } |
| 69726 | + } |
| 69727 | + return 0; |
| 69728 | +} |
| 69729 | + |
| 69730 | +/* |
| 69731 | +** Zero all counters associated with the sqlite3_stmt_scanstatus() data. |
| 69732 | +*/ |
| 69733 | +SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ |
| 69734 | + Vdbe *p = (Vdbe*)pStmt; |
| 69735 | + memset(p->anExec, 0, p->nOp * sizeof(i64)); |
| 69736 | +} |
| 69737 | +#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ |
| 69738 | + |
| 68732 | 69739 | /************** End of vdbeapi.c *********************************************/ |
| 68733 | 69740 | /************** Begin file vdbetrace.c ***************************************/ |
| 68734 | 69741 | /* |
| 68735 | 69742 | ** 2009 November 25 |
| 68736 | 69743 | ** |
| | @@ -69612,10 +70619,13 @@ |
| 69612 | 70619 | #ifdef VDBE_PROFILE |
| 69613 | 70620 | start = sqlite3Hwtime(); |
| 69614 | 70621 | #endif |
| 69615 | 70622 | nVmStep++; |
| 69616 | 70623 | pOp = &aOp[pc]; |
| 70624 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 70625 | + if( p->anExec ) p->anExec[pc]++; |
| 70626 | +#endif |
| 69617 | 70627 | |
| 69618 | 70628 | /* Only allow tracing if SQLITE_DEBUG is defined. |
| 69619 | 70629 | */ |
| 69620 | 70630 | #ifdef SQLITE_DEBUG |
| 69621 | 70631 | if( db->flags & SQLITE_VdbeTrace ){ |
| | @@ -71302,11 +72312,11 @@ |
| 71302 | 72312 | assert( pReg->flags & MEM_Blob ); |
| 71303 | 72313 | assert( memIsValid(pReg) ); |
| 71304 | 72314 | pC->payloadSize = pC->szRow = avail = pReg->n; |
| 71305 | 72315 | pC->aRow = (u8*)pReg->z; |
| 71306 | 72316 | }else{ |
| 71307 | | - MemSetTypeFlag(pDest, MEM_Null); |
| 72317 | + sqlite3VdbeMemSetNull(pDest); |
| 71308 | 72318 | goto op_column_out; |
| 71309 | 72319 | } |
| 71310 | 72320 | }else{ |
| 71311 | 72321 | assert( pCrsr ); |
| 71312 | 72322 | if( pC->isTable==0 ){ |
| | @@ -71637,11 +72647,14 @@ |
| 71637 | 72647 | testcase( serial_type==127 ); |
| 71638 | 72648 | testcase( serial_type==128 ); |
| 71639 | 72649 | nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); |
| 71640 | 72650 | }while( (--pRec)>=pData0 ); |
| 71641 | 72651 | |
| 71642 | | - /* Add the initial header varint and total the size */ |
| 72652 | + /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint |
| 72653 | + ** which determines the total number of bytes in the header. The varint |
| 72654 | + ** value is the size of the header in bytes including the size varint |
| 72655 | + ** itself. */ |
| 71643 | 72656 | testcase( nHdr==126 ); |
| 71644 | 72657 | testcase( nHdr==127 ); |
| 71645 | 72658 | if( nHdr<=126 ){ |
| 71646 | 72659 | /* The common case */ |
| 71647 | 72660 | nHdr += 1; |
| | @@ -71671,11 +72684,15 @@ |
| 71671 | 72684 | j = nHdr; |
| 71672 | 72685 | assert( pData0<=pLast ); |
| 71673 | 72686 | pRec = pData0; |
| 71674 | 72687 | do{ |
| 71675 | 72688 | serial_type = pRec->uTemp; |
| 72689 | + /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more |
| 72690 | + ** additional varints, one per column. */ |
| 71676 | 72691 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
| 72692 | + /* EVIDENCE-OF: R-64536-51728 The values for each column in the record |
| 72693 | + ** immediately follow the header. */ |
| 71677 | 72694 | j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ |
| 71678 | 72695 | }while( (++pRec)<=pLast ); |
| 71679 | 72696 | assert( i==nHdr ); |
| 71680 | 72697 | assert( j==nByte ); |
| 71681 | 72698 | |
| | @@ -71826,23 +72843,30 @@ |
| 71826 | 72843 | goto vdbe_return; |
| 71827 | 72844 | } |
| 71828 | 72845 | db->isTransactionSavepoint = 0; |
| 71829 | 72846 | rc = p->rc; |
| 71830 | 72847 | }else{ |
| 72848 | + int isSchemaChange; |
| 71831 | 72849 | iSavepoint = db->nSavepoint - iSavepoint - 1; |
| 71832 | 72850 | if( p1==SAVEPOINT_ROLLBACK ){ |
| 72851 | + isSchemaChange = (db->flags & SQLITE_InternChanges)!=0; |
| 71833 | 72852 | for(ii=0; ii<db->nDb; ii++){ |
| 71834 | | - sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT); |
| 72853 | + rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, |
| 72854 | + SQLITE_ABORT_ROLLBACK, |
| 72855 | + isSchemaChange==0); |
| 72856 | + if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 71835 | 72857 | } |
| 72858 | + }else{ |
| 72859 | + isSchemaChange = 0; |
| 71836 | 72860 | } |
| 71837 | 72861 | for(ii=0; ii<db->nDb; ii++){ |
| 71838 | 72862 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 71839 | 72863 | if( rc!=SQLITE_OK ){ |
| 71840 | 72864 | goto abort_due_to_error; |
| 71841 | 72865 | } |
| 71842 | 72866 | } |
| 71843 | | - if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){ |
| 72867 | + if( isSchemaChange ){ |
| 71844 | 72868 | sqlite3ExpirePreparedStatements(db); |
| 71845 | 72869 | sqlite3ResetAllSchemasOfConnection(db); |
| 71846 | 72870 | db->flags = (db->flags | SQLITE_InternChanges); |
| 71847 | 72871 | } |
| 71848 | 72872 | } |
| | @@ -72235,11 +73259,11 @@ |
| 72235 | 73259 | assert( p->bIsReader ); |
| 72236 | 73260 | assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx |
| 72237 | 73261 | || p->readOnly==0 ); |
| 72238 | 73262 | |
| 72239 | 73263 | if( p->expired ){ |
| 72240 | | - rc = SQLITE_ABORT; |
| 73264 | + rc = SQLITE_ABORT_ROLLBACK; |
| 72241 | 73265 | break; |
| 72242 | 73266 | } |
| 72243 | 73267 | |
| 72244 | 73268 | nField = 0; |
| 72245 | 73269 | pKeyInfo = 0; |
| | @@ -72799,14 +73823,14 @@ |
| 72799 | 73823 | } |
| 72800 | 73824 | pIdxKey = &r; |
| 72801 | 73825 | }else{ |
| 72802 | 73826 | pIdxKey = sqlite3VdbeAllocUnpackedRecord( |
| 72803 | 73827 | pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree |
| 72804 | | - ); |
| 73828 | + ); |
| 72805 | 73829 | if( pIdxKey==0 ) goto no_mem; |
| 72806 | 73830 | assert( pIn3->flags & MEM_Blob ); |
| 72807 | | - assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */ |
| 73831 | + ExpandBlob(pIn3); |
| 72808 | 73832 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
| 72809 | 73833 | } |
| 72810 | 73834 | pIdxKey->default_rc = 0; |
| 72811 | 73835 | if( pOp->opcode==OP_NoConflict ){ |
| 72812 | 73836 | /* For the OP_NoConflict opcode, take the jump if any of the |
| | @@ -73402,10 +74426,14 @@ |
| 73402 | 74426 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 73403 | 74427 | }else{ |
| 73404 | 74428 | assert( pC->pCursor!=0 ); |
| 73405 | 74429 | rc = sqlite3VdbeCursorRestore(pC); |
| 73406 | 74430 | if( rc ) goto abort_due_to_error; |
| 74431 | + if( pC->nullRow ){ |
| 74432 | + pOut->flags = MEM_Null; |
| 74433 | + break; |
| 74434 | + } |
| 73407 | 74435 | rc = sqlite3BtreeKeySize(pC->pCursor, &v); |
| 73408 | 74436 | assert( rc==SQLITE_OK ); /* Always so because of CursorRestore() above */ |
| 73409 | 74437 | } |
| 73410 | 74438 | pOut->u.i = v; |
| 73411 | 74439 | break; |
| | @@ -73492,13 +74520,13 @@ |
| 73492 | 74520 | } |
| 73493 | 74521 | /* Opcode: Rewind P1 P2 * * * |
| 73494 | 74522 | ** |
| 73495 | 74523 | ** The next use of the Rowid or Column or Next instruction for P1 |
| 73496 | 74524 | ** will refer to the first entry in the database table or index. |
| 73497 | | -** If the table or index is empty and P2>0, then jump immediately to P2. |
| 73498 | | -** If P2 is 0 or if the table or index is not empty, fall through |
| 73499 | | -** to the following instruction. |
| 74525 | +** If the table or index is empty, jump immediately to P2. |
| 74526 | +** If the table or index is not empty, fall through to the following |
| 74527 | +** instruction. |
| 73500 | 74528 | ** |
| 73501 | 74529 | ** This opcode leaves the cursor configured to move in forward order, |
| 73502 | 74530 | ** from the beginning toward the end. In other words, the cursor is |
| 73503 | 74531 | ** configured to use Next, not Prev. |
| 73504 | 74532 | */ |
| | @@ -74410,10 +75438,13 @@ |
| 74410 | 75438 | pFrame->aOp = p->aOp; |
| 74411 | 75439 | pFrame->nOp = p->nOp; |
| 74412 | 75440 | pFrame->token = pProgram->token; |
| 74413 | 75441 | pFrame->aOnceFlag = p->aOnceFlag; |
| 74414 | 75442 | pFrame->nOnceFlag = p->nOnceFlag; |
| 75443 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 75444 | + pFrame->anExec = p->anExec; |
| 75445 | +#endif |
| 74415 | 75446 | |
| 74416 | 75447 | pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
| 74417 | 75448 | for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
| 74418 | 75449 | pMem->flags = MEM_Undefined; |
| 74419 | 75450 | pMem->db = db; |
| | @@ -74427,10 +75458,11 @@ |
| 74427 | 75458 | |
| 74428 | 75459 | p->nFrame++; |
| 74429 | 75460 | pFrame->pParent = p->pFrame; |
| 74430 | 75461 | pFrame->lastRowid = lastRowid; |
| 74431 | 75462 | pFrame->nChange = p->nChange; |
| 75463 | + pFrame->nDbChange = p->db->nChange; |
| 74432 | 75464 | p->nChange = 0; |
| 74433 | 75465 | p->pFrame = pFrame; |
| 74434 | 75466 | p->aMem = aMem = &VdbeFrameMem(pFrame)[-1]; |
| 74435 | 75467 | p->nMem = pFrame->nChildMem; |
| 74436 | 75468 | p->nCursor = (u16)pFrame->nChildCsr; |
| | @@ -74437,10 +75469,13 @@ |
| 74437 | 75469 | p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; |
| 74438 | 75470 | p->aOp = aOp = pProgram->aOp; |
| 74439 | 75471 | p->nOp = pProgram->nOp; |
| 74440 | 75472 | p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor]; |
| 74441 | 75473 | p->nOnceFlag = pProgram->nOnce; |
| 75474 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 75475 | + p->anExec = 0; |
| 75476 | +#endif |
| 74442 | 75477 | pc = -1; |
| 74443 | 75478 | memset(p->aOnceFlag, 0, p->nOnceFlag); |
| 74444 | 75479 | |
| 74445 | 75480 | break; |
| 74446 | 75481 | } |
| | @@ -74681,12 +75716,12 @@ |
| 74681 | 75716 | |
| 74682 | 75717 | #ifndef SQLITE_OMIT_WAL |
| 74683 | 75718 | /* Opcode: Checkpoint P1 P2 P3 * * |
| 74684 | 75719 | ** |
| 74685 | 75720 | ** Checkpoint database P1. This is a no-op if P1 is not currently in |
| 74686 | | -** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL |
| 74687 | | -** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns |
| 75721 | +** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, |
| 75722 | +** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns |
| 74688 | 75723 | ** SQLITE_BUSY or not, respectively. Write the number of pages in the |
| 74689 | 75724 | ** WAL after the checkpoint into mem[P3+1] and the number of pages |
| 74690 | 75725 | ** in the WAL that have been checkpointed after the checkpoint |
| 74691 | 75726 | ** completes into mem[P3+2]. However on an error, mem[P3+1] and |
| 74692 | 75727 | ** mem[P3+2] are initialized to -1. |
| | @@ -74700,10 +75735,11 @@ |
| 74700 | 75735 | aRes[0] = 0; |
| 74701 | 75736 | aRes[1] = aRes[2] = -1; |
| 74702 | 75737 | assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE |
| 74703 | 75738 | || pOp->p2==SQLITE_CHECKPOINT_FULL |
| 74704 | 75739 | || pOp->p2==SQLITE_CHECKPOINT_RESTART |
| 75740 | + || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE |
| 74705 | 75741 | ); |
| 74706 | 75742 | rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); |
| 74707 | 75743 | if( rc==SQLITE_BUSY ){ |
| 74708 | 75744 | rc = SQLITE_OK; |
| 74709 | 75745 | aRes[0] = 1; |
| | @@ -75625,10 +76661,15 @@ |
| 75625 | 76661 | char *zErr = 0; |
| 75626 | 76662 | Table *pTab; |
| 75627 | 76663 | Parse *pParse = 0; |
| 75628 | 76664 | Incrblob *pBlob = 0; |
| 75629 | 76665 | |
| 76666 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 76667 | + if( !sqlite3SafetyCheckOk(db) || ppBlob==0 || zTable==0 ){ |
| 76668 | + return SQLITE_MISUSE_BKPT; |
| 76669 | + } |
| 76670 | +#endif |
| 75630 | 76671 | flags = !!flags; /* flags = (flags ? 1 : 0); */ |
| 75631 | 76672 | *ppBlob = 0; |
| 75632 | 76673 | |
| 75633 | 76674 | sqlite3_mutex_enter(db->mutex); |
| 75634 | 76675 | |
| | @@ -75843,11 +76884,10 @@ |
| 75843 | 76884 | v = (Vdbe*)p->pStmt; |
| 75844 | 76885 | |
| 75845 | 76886 | if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){ |
| 75846 | 76887 | /* Request is out of range. Return a transient error. */ |
| 75847 | 76888 | rc = SQLITE_ERROR; |
| 75848 | | - sqlite3Error(db, SQLITE_ERROR); |
| 75849 | 76889 | }else if( v==0 ){ |
| 75850 | 76890 | /* If there is no statement handle, then the blob-handle has |
| 75851 | 76891 | ** already been invalidated. Return SQLITE_ABORT in this case. |
| 75852 | 76892 | */ |
| 75853 | 76893 | rc = SQLITE_ABORT; |
| | @@ -75861,14 +76901,14 @@ |
| 75861 | 76901 | sqlite3BtreeLeaveCursor(p->pCsr); |
| 75862 | 76902 | if( rc==SQLITE_ABORT ){ |
| 75863 | 76903 | sqlite3VdbeFinalize(v); |
| 75864 | 76904 | p->pStmt = 0; |
| 75865 | 76905 | }else{ |
| 75866 | | - db->errCode = rc; |
| 75867 | 76906 | v->rc = rc; |
| 75868 | 76907 | } |
| 75869 | 76908 | } |
| 76909 | + sqlite3Error(db, rc); |
| 75870 | 76910 | rc = sqlite3ApiExit(db, rc); |
| 75871 | 76911 | sqlite3_mutex_leave(db->mutex); |
| 75872 | 76912 | return rc; |
| 75873 | 76913 | } |
| 75874 | 76914 | |
| | @@ -76041,11 +77081,11 @@ |
| 76041 | 77081 | ** itself. |
| 76042 | 77082 | ** |
| 76043 | 77083 | ** The sorter is running in multi-threaded mode if (a) the library was built |
| 76044 | 77084 | ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater |
| 76045 | 77085 | ** than zero, and (b) worker threads have been enabled at runtime by calling |
| 76046 | | -** sqlite3_config(SQLITE_CONFIG_WORKER_THREADS, ...). |
| 77086 | +** "PRAGMA threads=N" with some value of N greater than 0. |
| 76047 | 77087 | ** |
| 76048 | 77088 | ** When Rewind() is called, any data remaining in memory is flushed to a |
| 76049 | 77089 | ** final PMA. So at this point the data is stored in some number of sorted |
| 76050 | 77090 | ** PMAs within temporary files on disk. |
| 76051 | 77091 | ** |
| | @@ -76086,10 +77126,17 @@ |
| 76086 | 77126 | */ |
| 76087 | 77127 | #if 0 |
| 76088 | 77128 | # define SQLITE_DEBUG_SORTER_THREADS 1 |
| 76089 | 77129 | #endif |
| 76090 | 77130 | |
| 77131 | +/* |
| 77132 | +** Hard-coded maximum amount of data to accumulate in memory before flushing |
| 77133 | +** to a level 0 PMA. The purpose of this limit is to prevent various integer |
| 77134 | +** overflows. 512MiB. |
| 77135 | +*/ |
| 77136 | +#define SQLITE_MAX_MXPMASIZE (1<<29) |
| 77137 | + |
| 76091 | 77138 | /* |
| 76092 | 77139 | ** Private objects used by the sorter |
| 76093 | 77140 | */ |
| 76094 | 77141 | typedef struct MergeEngine MergeEngine; /* Merge PMAs together */ |
| 76095 | 77142 | typedef struct PmaReader PmaReader; /* Incrementally read one PMA */ |
| | @@ -76784,17 +77831,15 @@ |
| 76784 | 77831 | |
| 76785 | 77832 | if( !sqlite3TempInMemory(db) ){ |
| 76786 | 77833 | pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz; |
| 76787 | 77834 | mxCache = db->aDb[0].pSchema->cache_size; |
| 76788 | 77835 | if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING; |
| 76789 | | - pSorter->mxPmaSize = mxCache * pgsz; |
| 77836 | + pSorter->mxPmaSize = MIN((i64)mxCache*pgsz, SQLITE_MAX_MXPMASIZE); |
| 76790 | 77837 | |
| 76791 | | - /* If the application has not configure scratch memory using |
| 76792 | | - ** SQLITE_CONFIG_SCRATCH then we assume it is OK to do large memory |
| 76793 | | - ** allocations. If scratch memory has been configured, then assume |
| 76794 | | - ** large memory allocations should be avoided to prevent heap |
| 76795 | | - ** fragmentation. |
| 77838 | + /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of |
| 77839 | + ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary |
| 77840 | + ** large heap allocations. |
| 76796 | 77841 | */ |
| 76797 | 77842 | if( sqlite3GlobalConfig.pScratch==0 ){ |
| 76798 | 77843 | assert( pSorter->iMemory==0 ); |
| 76799 | 77844 | pSorter->nMemory = pgsz; |
| 76800 | 77845 | pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz); |
| | @@ -79162,19 +80207,19 @@ |
| 79162 | 80207 | ** |
| 79163 | 80208 | ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) |
| 79164 | 80209 | ** is a helper function - a callback for the tree walker. |
| 79165 | 80210 | */ |
| 79166 | 80211 | static int incrAggDepth(Walker *pWalker, Expr *pExpr){ |
| 79167 | | - if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i; |
| 80212 | + if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n; |
| 79168 | 80213 | return WRC_Continue; |
| 79169 | 80214 | } |
| 79170 | 80215 | static void incrAggFunctionDepth(Expr *pExpr, int N){ |
| 79171 | 80216 | if( N>0 ){ |
| 79172 | 80217 | Walker w; |
| 79173 | 80218 | memset(&w, 0, sizeof(w)); |
| 79174 | 80219 | w.xExprCallback = incrAggDepth; |
| 79175 | | - w.u.i = N; |
| 80220 | + w.u.n = N; |
| 79176 | 80221 | sqlite3WalkExpr(&w, pExpr); |
| 79177 | 80222 | } |
| 79178 | 80223 | } |
| 79179 | 80224 | |
| 79180 | 80225 | /* |
| | @@ -79454,10 +80499,14 @@ |
| 79454 | 80499 | } |
| 79455 | 80500 | } |
| 79456 | 80501 | if( pMatch ){ |
| 79457 | 80502 | pExpr->iTable = pMatch->iCursor; |
| 79458 | 80503 | pExpr->pTab = pMatch->pTab; |
| 80504 | + assert( (pMatch->jointype & JT_RIGHT)==0 ); /* RIGHT JOIN not (yet) supported */ |
| 80505 | + if( (pMatch->jointype & JT_LEFT)!=0 ){ |
| 80506 | + ExprSetProperty(pExpr, EP_CanBeNull); |
| 80507 | + } |
| 79459 | 80508 | pSchema = pExpr->pTab->pSchema; |
| 79460 | 80509 | } |
| 79461 | 80510 | } /* if( pSrcList ) */ |
| 79462 | 80511 | |
| 79463 | 80512 | #ifndef SQLITE_OMIT_TRIGGER |
| | @@ -79718,11 +80767,11 @@ |
| 79718 | 80767 | double r = -1.0; |
| 79719 | 80768 | if( p->op!=TK_FLOAT ) return -1; |
| 79720 | 80769 | sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8); |
| 79721 | 80770 | assert( r>=0.0 ); |
| 79722 | 80771 | if( r>1.0 ) return -1; |
| 79723 | | - return (int)(r*1000.0); |
| 80772 | + return (int)(r*134217728.0); |
| 79724 | 80773 | } |
| 79725 | 80774 | |
| 79726 | 80775 | /* |
| 79727 | 80776 | ** This routine is callback for sqlite3WalkExpr(). |
| 79728 | 80777 | ** |
| | @@ -79850,11 +80899,11 @@ |
| 79850 | 80899 | ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand for |
| 79851 | 80900 | ** likelihood(X,0.9375). |
| 79852 | 80901 | ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to |
| 79853 | 80902 | ** likelihood(X,0.9375). */ |
| 79854 | 80903 | /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */ |
| 79855 | | - pExpr->iTable = pDef->zName[0]=='u' ? 62 : 938; |
| 80904 | + pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120; |
| 79856 | 80905 | } |
| 79857 | 80906 | } |
| 79858 | 80907 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 79859 | 80908 | auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); |
| 79860 | 80909 | if( auth!=SQLITE_OK ){ |
| | @@ -81807,69 +82856,79 @@ |
| 81807 | 82856 | sqlite3DbFree(db, pList->a); |
| 81808 | 82857 | sqlite3DbFree(db, pList); |
| 81809 | 82858 | } |
| 81810 | 82859 | |
| 81811 | 82860 | /* |
| 81812 | | -** These routines are Walker callbacks. Walker.u.pi is a pointer |
| 81813 | | -** to an integer. These routines are checking an expression to see |
| 81814 | | -** if it is a constant. Set *Walker.u.i to 0 if the expression is |
| 81815 | | -** not constant. |
| 82861 | +** These routines are Walker callbacks used to check expressions to |
| 82862 | +** see if they are "constant" for some definition of constant. The |
| 82863 | +** Walker.eCode value determines the type of "constant" we are looking |
| 82864 | +** for. |
| 81816 | 82865 | ** |
| 81817 | 82866 | ** These callback routines are used to implement the following: |
| 81818 | 82867 | ** |
| 81819 | | -** sqlite3ExprIsConstant() pWalker->u.i==1 |
| 81820 | | -** sqlite3ExprIsConstantNotJoin() pWalker->u.i==2 |
| 81821 | | -** sqlite3ExprIsConstantOrFunction() pWalker->u.i==3 or 4 |
| 82868 | +** sqlite3ExprIsConstant() pWalker->eCode==1 |
| 82869 | +** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 |
| 82870 | +** sqlite3ExprRefOneTableOnly() pWalker->eCode==3 |
| 82871 | +** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 |
| 82872 | +** |
| 82873 | +** In all cases, the callbacks set Walker.eCode=0 and abort if the expression |
| 82874 | +** is found to not be a constant. |
| 81822 | 82875 | ** |
| 81823 | 82876 | ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions |
| 81824 | | -** in a CREATE TABLE statement. The Walker.u.i value is 4 when parsing |
| 81825 | | -** an existing schema and 3 when processing a new statement. A bound |
| 82877 | +** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing |
| 82878 | +** an existing schema and 4 when processing a new statement. A bound |
| 81826 | 82879 | ** parameter raises an error for new statements, but is silently converted |
| 81827 | 82880 | ** to NULL for existing schemas. This allows sqlite_master tables that |
| 81828 | 82881 | ** contain a bound parameter because they were generated by older versions |
| 81829 | 82882 | ** of SQLite to be parsed by newer versions of SQLite without raising a |
| 81830 | 82883 | ** malformed schema error. |
| 81831 | 82884 | */ |
| 81832 | 82885 | static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ |
| 81833 | 82886 | |
| 81834 | | - /* If pWalker->u.i is 2 then any term of the expression that comes from |
| 81835 | | - ** the ON or USING clauses of a join disqualifies the expression |
| 82887 | + /* If pWalker->eCode is 2 then any term of the expression that comes from |
| 82888 | + ** the ON or USING clauses of a left join disqualifies the expression |
| 81836 | 82889 | ** from being considered constant. */ |
| 81837 | | - if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 81838 | | - pWalker->u.i = 0; |
| 82890 | + if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 82891 | + pWalker->eCode = 0; |
| 81839 | 82892 | return WRC_Abort; |
| 81840 | 82893 | } |
| 81841 | 82894 | |
| 81842 | 82895 | switch( pExpr->op ){ |
| 81843 | 82896 | /* Consider functions to be constant if all their arguments are constant |
| 81844 | | - ** and either pWalker->u.i==3 or 4 or the function as the SQLITE_FUNC_CONST |
| 81845 | | - ** flag. */ |
| 82897 | + ** and either pWalker->eCode==4 or 5 or the function has the |
| 82898 | + ** SQLITE_FUNC_CONST flag. */ |
| 81846 | 82899 | case TK_FUNCTION: |
| 81847 | | - if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){ |
| 82900 | + if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_Constant) ){ |
| 81848 | 82901 | return WRC_Continue; |
| 82902 | + }else{ |
| 82903 | + pWalker->eCode = 0; |
| 82904 | + return WRC_Abort; |
| 81849 | 82905 | } |
| 81850 | | - /* Fall through */ |
| 81851 | 82906 | case TK_ID: |
| 81852 | 82907 | case TK_COLUMN: |
| 81853 | 82908 | case TK_AGG_FUNCTION: |
| 81854 | 82909 | case TK_AGG_COLUMN: |
| 81855 | 82910 | testcase( pExpr->op==TK_ID ); |
| 81856 | 82911 | testcase( pExpr->op==TK_COLUMN ); |
| 81857 | 82912 | testcase( pExpr->op==TK_AGG_FUNCTION ); |
| 81858 | 82913 | testcase( pExpr->op==TK_AGG_COLUMN ); |
| 81859 | | - pWalker->u.i = 0; |
| 81860 | | - return WRC_Abort; |
| 82914 | + if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ |
| 82915 | + return WRC_Continue; |
| 82916 | + }else{ |
| 82917 | + pWalker->eCode = 0; |
| 82918 | + return WRC_Abort; |
| 82919 | + } |
| 81861 | 82920 | case TK_VARIABLE: |
| 81862 | | - if( pWalker->u.i==4 ){ |
| 82921 | + if( pWalker->eCode==5 ){ |
| 81863 | 82922 | /* Silently convert bound parameters that appear inside of CREATE |
| 81864 | 82923 | ** statements into a NULL when parsing the CREATE statement text out |
| 81865 | 82924 | ** of the sqlite_master table */ |
| 81866 | 82925 | pExpr->op = TK_NULL; |
| 81867 | | - }else if( pWalker->u.i==3 ){ |
| 82926 | + }else if( pWalker->eCode==4 ){ |
| 81868 | 82927 | /* A bound parameter in a CREATE statement that originates from |
| 81869 | 82928 | ** sqlite3_prepare() causes an error */ |
| 81870 | | - pWalker->u.i = 0; |
| 82929 | + pWalker->eCode = 0; |
| 81871 | 82930 | return WRC_Abort; |
| 81872 | 82931 | } |
| 81873 | 82932 | /* Fall through */ |
| 81874 | 82933 | default: |
| 81875 | 82934 | testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ |
| | @@ -81877,57 +82936,68 @@ |
| 81877 | 82936 | return WRC_Continue; |
| 81878 | 82937 | } |
| 81879 | 82938 | } |
| 81880 | 82939 | static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ |
| 81881 | 82940 | UNUSED_PARAMETER(NotUsed); |
| 81882 | | - pWalker->u.i = 0; |
| 82941 | + pWalker->eCode = 0; |
| 81883 | 82942 | return WRC_Abort; |
| 81884 | 82943 | } |
| 81885 | | -static int exprIsConst(Expr *p, int initFlag){ |
| 82944 | +static int exprIsConst(Expr *p, int initFlag, int iCur){ |
| 81886 | 82945 | Walker w; |
| 81887 | 82946 | memset(&w, 0, sizeof(w)); |
| 81888 | | - w.u.i = initFlag; |
| 82947 | + w.eCode = initFlag; |
| 81889 | 82948 | w.xExprCallback = exprNodeIsConstant; |
| 81890 | 82949 | w.xSelectCallback = selectNodeIsConstant; |
| 82950 | + w.u.iCur = iCur; |
| 81891 | 82951 | sqlite3WalkExpr(&w, p); |
| 81892 | | - return w.u.i; |
| 82952 | + return w.eCode; |
| 81893 | 82953 | } |
| 81894 | 82954 | |
| 81895 | 82955 | /* |
| 81896 | | -** Walk an expression tree. Return 1 if the expression is constant |
| 82956 | +** Walk an expression tree. Return non-zero if the expression is constant |
| 81897 | 82957 | ** and 0 if it involves variables or function calls. |
| 81898 | 82958 | ** |
| 81899 | 82959 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 81900 | 82960 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 81901 | 82961 | ** a constant. |
| 81902 | 82962 | */ |
| 81903 | 82963 | SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){ |
| 81904 | | - return exprIsConst(p, 1); |
| 82964 | + return exprIsConst(p, 1, 0); |
| 81905 | 82965 | } |
| 81906 | 82966 | |
| 81907 | 82967 | /* |
| 81908 | | -** Walk an expression tree. Return 1 if the expression is constant |
| 82968 | +** Walk an expression tree. Return non-zero if the expression is constant |
| 81909 | 82969 | ** that does no originate from the ON or USING clauses of a join. |
| 81910 | 82970 | ** Return 0 if it involves variables or function calls or terms from |
| 81911 | 82971 | ** an ON or USING clause. |
| 81912 | 82972 | */ |
| 81913 | 82973 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){ |
| 81914 | | - return exprIsConst(p, 2); |
| 82974 | + return exprIsConst(p, 2, 0); |
| 81915 | 82975 | } |
| 81916 | 82976 | |
| 81917 | 82977 | /* |
| 81918 | | -** Walk an expression tree. Return 1 if the expression is constant |
| 82978 | +** Walk an expression tree. Return non-zero if the expression constant |
| 82979 | +** for any single row of the table with cursor iCur. In other words, the |
| 82980 | +** expression must not refer to any non-deterministic function nor any |
| 82981 | +** table other than iCur. |
| 82982 | +*/ |
| 82983 | +SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){ |
| 82984 | + return exprIsConst(p, 3, iCur); |
| 82985 | +} |
| 82986 | + |
| 82987 | +/* |
| 82988 | +** Walk an expression tree. Return non-zero if the expression is constant |
| 81919 | 82989 | ** or a function call with constant arguments. Return and 0 if there |
| 81920 | 82990 | ** are any variables. |
| 81921 | 82991 | ** |
| 81922 | 82992 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 81923 | 82993 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 81924 | 82994 | ** a constant. |
| 81925 | 82995 | */ |
| 81926 | 82996 | SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ |
| 81927 | 82997 | assert( isInit==0 || isInit==1 ); |
| 81928 | | - return exprIsConst(p, 3+isInit); |
| 82998 | + return exprIsConst(p, 4+isInit, 0); |
| 81929 | 82999 | } |
| 81930 | 83000 | |
| 81931 | 83001 | /* |
| 81932 | 83002 | ** If the expression p codes a constant integer that is small enough |
| 81933 | 83003 | ** to fit in a 32-bit integer, return 1 and put the value of the integer |
| | @@ -81990,11 +83060,12 @@ |
| 81990 | 83060 | case TK_FLOAT: |
| 81991 | 83061 | case TK_BLOB: |
| 81992 | 83062 | return 0; |
| 81993 | 83063 | case TK_COLUMN: |
| 81994 | 83064 | assert( p->pTab!=0 ); |
| 81995 | | - return p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0; |
| 83065 | + return ExprHasProperty(p, EP_CanBeNull) || |
| 83066 | + (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0); |
| 81996 | 83067 | default: |
| 81997 | 83068 | return 1; |
| 81998 | 83069 | } |
| 81999 | 83070 | } |
| 82000 | 83071 | |
| | @@ -82433,11 +83504,10 @@ |
| 82433 | 83504 | sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); |
| 82434 | 83505 | dest.affSdst = (u8)affinity; |
| 82435 | 83506 | assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); |
| 82436 | 83507 | pSelect->iLimit = 0; |
| 82437 | 83508 | testcase( pSelect->selFlags & SF_Distinct ); |
| 82438 | | - pSelect->selFlags &= ~SF_Distinct; |
| 82439 | 83509 | testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ |
| 82440 | 83510 | if( sqlite3Select(pParse, pSelect, &dest) ){ |
| 82441 | 83511 | sqlite3KeyInfoUnref(pKeyInfo); |
| 82442 | 83512 | return 0; |
| 82443 | 83513 | } |
| | @@ -83580,11 +84650,14 @@ |
| 83580 | 84650 | target |
| 83581 | 84651 | )); |
| 83582 | 84652 | |
| 83583 | 84653 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 83584 | 84654 | /* If the column has REAL affinity, it may currently be stored as an |
| 83585 | | - ** integer. Use OP_RealAffinity to make sure it is really real. */ |
| 84655 | + ** integer. Use OP_RealAffinity to make sure it is really real. |
| 84656 | + ** |
| 84657 | + ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to |
| 84658 | + ** floating point when extracting it from the record. */ |
| 83586 | 84659 | if( pExpr->iColumn>=0 |
| 83587 | 84660 | && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL |
| 83588 | 84661 | ){ |
| 83589 | 84662 | sqlite3VdbeAddOp1(v, OP_RealAffinity, target); |
| 83590 | 84663 | } |
| | @@ -87232,27 +88305,32 @@ |
| 87232 | 88305 | aLog[i] = sqlite3LogEst(v); |
| 87233 | 88306 | #endif |
| 87234 | 88307 | if( *z==' ' ) z++; |
| 87235 | 88308 | } |
| 87236 | 88309 | #ifndef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87237 | | - assert( pIndex!=0 ); |
| 88310 | + assert( pIndex!=0 ); { |
| 87238 | 88311 | #else |
| 87239 | | - if( pIndex ) |
| 88312 | + if( pIndex ){ |
| 87240 | 88313 | #endif |
| 87241 | | - while( z[0] ){ |
| 87242 | | - if( sqlite3_strglob("unordered*", z)==0 ){ |
| 87243 | | - pIndex->bUnordered = 1; |
| 87244 | | - }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){ |
| 87245 | | - pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3)); |
| 87246 | | - } |
| 88314 | + pIndex->bUnordered = 0; |
| 88315 | + pIndex->noSkipScan = 0; |
| 88316 | + while( z[0] ){ |
| 88317 | + if( sqlite3_strglob("unordered*", z)==0 ){ |
| 88318 | + pIndex->bUnordered = 1; |
| 88319 | + }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){ |
| 88320 | + pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3)); |
| 88321 | + }else if( sqlite3_strglob("noskipscan*", z)==0 ){ |
| 88322 | + pIndex->noSkipScan = 1; |
| 88323 | + } |
| 87247 | 88324 | #ifdef SQLITE_ENABLE_COSTMULT |
| 87248 | | - else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){ |
| 87249 | | - pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9)); |
| 88325 | + else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){ |
| 88326 | + pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9)); |
| 88327 | + } |
| 88328 | +#endif |
| 88329 | + while( z[0]!=0 && z[0]!=' ' ) z++; |
| 88330 | + while( z[0]==' ' ) z++; |
| 87250 | 88331 | } |
| 87251 | | -#endif |
| 87252 | | - while( z[0]!=0 && z[0]!=' ' ) z++; |
| 87253 | | - while( z[0]==' ' ) z++; |
| 87254 | 88332 | } |
| 87255 | 88333 | } |
| 87256 | 88334 | |
| 87257 | 88335 | /* |
| 87258 | 88336 | ** This callback is invoked once for each index when reading the |
| | @@ -87366,18 +88444,19 @@ |
| 87366 | 88444 | tRowcnt avgEq = 0; |
| 87367 | 88445 | tRowcnt nRow; /* Number of rows in index */ |
| 87368 | 88446 | i64 nSum100 = 0; /* Number of terms contributing to sumEq */ |
| 87369 | 88447 | i64 nDist100; /* Number of distinct values in index */ |
| 87370 | 88448 | |
| 87371 | | - if( pIdx->aiRowEst==0 || pIdx->aiRowEst[iCol+1]==0 ){ |
| 88449 | + if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){ |
| 87372 | 88450 | nRow = pFinal->anLt[iCol]; |
| 87373 | 88451 | nDist100 = (i64)100 * pFinal->anDLt[iCol]; |
| 87374 | 88452 | nSample--; |
| 87375 | 88453 | }else{ |
| 87376 | 88454 | nRow = pIdx->aiRowEst[0]; |
| 87377 | 88455 | nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1]; |
| 87378 | 88456 | } |
| 88457 | + pIdx->nRowEst0 = nRow; |
| 87379 | 88458 | |
| 87380 | 88459 | /* Set nSum to the number of distinct (iCol+1) field prefixes that |
| 87381 | 88460 | ** occur in the stat4 table for this index. Set sumEq to the sum of |
| 87382 | 88461 | ** the nEq values for column iCol for the same set (adding the value |
| 87383 | 88462 | ** only once where there exist duplicate prefixes). */ |
| | @@ -87635,11 +88714,11 @@ |
| 87635 | 88714 | } |
| 87636 | 88715 | |
| 87637 | 88716 | |
| 87638 | 88717 | /* Load the statistics from the sqlite_stat4 table. */ |
| 87639 | 88718 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87640 | | - if( rc==SQLITE_OK ){ |
| 88719 | + if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){ |
| 87641 | 88720 | int lookasideEnabled = db->lookaside.bEnabled; |
| 87642 | 88721 | db->lookaside.bEnabled = 0; |
| 87643 | 88722 | rc = loadStat4(db, sInfo.zDatabase); |
| 87644 | 88723 | db->lookaside.bEnabled = lookasideEnabled; |
| 87645 | 88724 | } |
| | @@ -88317,10 +89396,13 @@ |
| 88317 | 89396 | SQLITE_API int sqlite3_set_authorizer( |
| 88318 | 89397 | sqlite3 *db, |
| 88319 | 89398 | int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), |
| 88320 | 89399 | void *pArg |
| 88321 | 89400 | ){ |
| 89401 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 89402 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 89403 | +#endif |
| 88322 | 89404 | sqlite3_mutex_enter(db->mutex); |
| 88323 | 89405 | db->xAuth = (sqlite3_xauth)xAuth; |
| 88324 | 89406 | db->pAuthArg = pArg; |
| 88325 | 89407 | sqlite3ExpirePreparedStatements(db); |
| 88326 | 89408 | sqlite3_mutex_leave(db->mutex); |
| | @@ -88811,11 +89893,15 @@ |
| 88811 | 89893 | ** See also sqlite3LocateTable(). |
| 88812 | 89894 | */ |
| 88813 | 89895 | SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
| 88814 | 89896 | Table *p = 0; |
| 88815 | 89897 | int i; |
| 88816 | | - assert( zName!=0 ); |
| 89898 | + |
| 89899 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 89900 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return 0; |
| 89901 | +#endif |
| 89902 | + |
| 88817 | 89903 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 88818 | 89904 | assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
| 88819 | 89905 | #if SQLITE_USER_AUTHENTICATION |
| 88820 | 89906 | /* Only the admin user is allowed to know that the sqlite_user table |
| 88821 | 89907 | ** exists */ |
| | @@ -94275,12 +95361,12 @@ |
| 94275 | 95361 | break; |
| 94276 | 95362 | } |
| 94277 | 95363 | default: { |
| 94278 | 95364 | /* Because sqlite3_value_double() returns 0.0 if the argument is not |
| 94279 | 95365 | ** something that can be converted into a number, we have: |
| 94280 | | - ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that |
| 94281 | | - ** cannot be converted to a numeric value. |
| 95366 | + ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob |
| 95367 | + ** that cannot be converted to a numeric value. |
| 94282 | 95368 | */ |
| 94283 | 95369 | double rVal = sqlite3_value_double(argv[0]); |
| 94284 | 95370 | if( rVal<0 ) rVal = -rVal; |
| 94285 | 95371 | sqlite3_result_double(context, rVal); |
| 94286 | 95372 | break; |
| | @@ -102928,11 +104014,11 @@ |
| 102928 | 104014 | break; |
| 102929 | 104015 | #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 102930 | 104016 | |
| 102931 | 104017 | #ifndef SQLITE_OMIT_WAL |
| 102932 | 104018 | /* |
| 102933 | | - ** PRAGMA [database.]wal_checkpoint = passive|full|restart |
| 104019 | + ** PRAGMA [database.]wal_checkpoint = passive|full|restart|truncate |
| 102934 | 104020 | ** |
| 102935 | 104021 | ** Checkpoint the database. |
| 102936 | 104022 | */ |
| 102937 | 104023 | case PragTyp_WAL_CHECKPOINT: { |
| 102938 | 104024 | int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED); |
| | @@ -102940,10 +104026,12 @@ |
| 102940 | 104026 | if( zRight ){ |
| 102941 | 104027 | if( sqlite3StrICmp(zRight, "full")==0 ){ |
| 102942 | 104028 | eMode = SQLITE_CHECKPOINT_FULL; |
| 102943 | 104029 | }else if( sqlite3StrICmp(zRight, "restart")==0 ){ |
| 102944 | 104030 | eMode = SQLITE_CHECKPOINT_RESTART; |
| 104031 | + }else if( sqlite3StrICmp(zRight, "truncate")==0 ){ |
| 104032 | + eMode = SQLITE_CHECKPOINT_TRUNCATE; |
| 102945 | 104033 | } |
| 102946 | 104034 | } |
| 102947 | 104035 | sqlite3VdbeSetNumCols(v, 3); |
| 102948 | 104036 | pParse->nMem = 3; |
| 102949 | 104037 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC); |
| | @@ -103834,13 +104922,16 @@ |
| 103834 | 104922 | Vdbe *pOld, /* VM being reprepared */ |
| 103835 | 104923 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 103836 | 104924 | const char **pzTail /* OUT: End of parsed string */ |
| 103837 | 104925 | ){ |
| 103838 | 104926 | int rc; |
| 103839 | | - assert( ppStmt!=0 ); |
| 104927 | + |
| 104928 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 104929 | + if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 104930 | +#endif |
| 103840 | 104931 | *ppStmt = 0; |
| 103841 | | - if( !sqlite3SafetyCheckOk(db) ){ |
| 104932 | + if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ |
| 103842 | 104933 | return SQLITE_MISUSE_BKPT; |
| 103843 | 104934 | } |
| 103844 | 104935 | sqlite3_mutex_enter(db->mutex); |
| 103845 | 104936 | sqlite3BtreeEnterAll(db); |
| 103846 | 104937 | rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); |
| | @@ -103943,13 +105034,15 @@ |
| 103943 | 105034 | */ |
| 103944 | 105035 | char *zSql8; |
| 103945 | 105036 | const char *zTail8 = 0; |
| 103946 | 105037 | int rc = SQLITE_OK; |
| 103947 | 105038 | |
| 103948 | | - assert( ppStmt ); |
| 105039 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 105040 | + if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 105041 | +#endif |
| 103949 | 105042 | *ppStmt = 0; |
| 103950 | | - if( !sqlite3SafetyCheckOk(db) ){ |
| 105043 | + if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ |
| 103951 | 105044 | return SQLITE_MISUSE_BKPT; |
| 103952 | 105045 | } |
| 103953 | 105046 | if( nBytes>=0 ){ |
| 103954 | 105047 | int sz; |
| 103955 | 105048 | const char *z = (const char*)zSql; |
| | @@ -108842,11 +109935,11 @@ |
| 108842 | 109935 | ** |
| 108843 | 109936 | ** SELECT DISTINCT xyz FROM ... ORDER BY xyz |
| 108844 | 109937 | ** |
| 108845 | 109938 | ** is transformed to: |
| 108846 | 109939 | ** |
| 108847 | | - ** SELECT xyz FROM ... GROUP BY xyz |
| 109940 | + ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz |
| 108848 | 109941 | ** |
| 108849 | 109942 | ** The second form is preferred as a single index (or temp-table) may be |
| 108850 | 109943 | ** used for both the ORDER BY and DISTINCT processing. As originally |
| 108851 | 109944 | ** written the query must use a temp-table for at least one of the ORDER |
| 108852 | 109945 | ** BY and DISTINCT, and an index or separate temp-table for the other. |
| | @@ -108855,11 +109948,10 @@ |
| 108855 | 109948 | && sqlite3ExprListCompare(sSort.pOrderBy, p->pEList, -1)==0 |
| 108856 | 109949 | ){ |
| 108857 | 109950 | p->selFlags &= ~SF_Distinct; |
| 108858 | 109951 | p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0); |
| 108859 | 109952 | pGroupBy = p->pGroupBy; |
| 108860 | | - sSort.pOrderBy = 0; |
| 108861 | 109953 | /* Notice that even thought SF_Distinct has been cleared from p->selFlags, |
| 108862 | 109954 | ** the sDistinct.isTnct is still set. Hence, isTnct represents the |
| 108863 | 109955 | ** original setting of the SF_Distinct flag, not the current setting */ |
| 108864 | 109956 | assert( sDistinct.isTnct ); |
| 108865 | 109957 | } |
| | @@ -109658,10 +110750,13 @@ |
| 109658 | 110750 | char **pzErrMsg /* Write error messages here */ |
| 109659 | 110751 | ){ |
| 109660 | 110752 | int rc; |
| 109661 | 110753 | TabResult res; |
| 109662 | 110754 | |
| 110755 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 110756 | + if( pazResult==0 ) return SQLITE_MISUSE_BKPT; |
| 110757 | +#endif |
| 109663 | 110758 | *pazResult = 0; |
| 109664 | 110759 | if( pnColumn ) *pnColumn = 0; |
| 109665 | 110760 | if( pnRow ) *pnRow = 0; |
| 109666 | 110761 | if( pzErrMsg ) *pzErrMsg = 0; |
| 109667 | 110762 | res.zErrMsg = 0; |
| | @@ -111721,11 +112816,11 @@ |
| 111721 | 112816 | ** Two writes per page are required in step (3) because the original |
| 111722 | 112817 | ** database content must be written into the rollback journal prior to |
| 111723 | 112818 | ** overwriting the database with the vacuumed content. |
| 111724 | 112819 | ** |
| 111725 | 112820 | ** Only 1x temporary space and only 1x writes would be required if |
| 111726 | | -** the copy of step (3) were replace by deleting the original database |
| 112821 | +** the copy of step (3) were replaced by deleting the original database |
| 111727 | 112822 | ** and renaming the transient database as the original. But that will |
| 111728 | 112823 | ** not work if other processes are attached to the original database. |
| 111729 | 112824 | ** And a power loss in between deleting the original and renaming the |
| 111730 | 112825 | ** transient would cause the database file to appear to be deleted |
| 111731 | 112826 | ** following reboot. |
| | @@ -112079,10 +113174,13 @@ |
| 112079 | 113174 | sqlite3 *db, /* Database in which module is registered */ |
| 112080 | 113175 | const char *zName, /* Name assigned to this module */ |
| 112081 | 113176 | const sqlite3_module *pModule, /* The definition of the module */ |
| 112082 | 113177 | void *pAux /* Context pointer for xCreate/xConnect */ |
| 112083 | 113178 | ){ |
| 113179 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 113180 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 113181 | +#endif |
| 112084 | 113182 | return createModule(db, zName, pModule, pAux, 0); |
| 112085 | 113183 | } |
| 112086 | 113184 | |
| 112087 | 113185 | /* |
| 112088 | 113186 | ** External API function used to create a new virtual-table module. |
| | @@ -112092,10 +113190,13 @@ |
| 112092 | 113190 | const char *zName, /* Name assigned to this module */ |
| 112093 | 113191 | const sqlite3_module *pModule, /* The definition of the module */ |
| 112094 | 113192 | void *pAux, /* Context pointer for xCreate/xConnect */ |
| 112095 | 113193 | void (*xDestroy)(void *) /* Module destructor function */ |
| 112096 | 113194 | ){ |
| 113195 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 113196 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 113197 | +#endif |
| 112097 | 113198 | return createModule(db, zName, pModule, pAux, xDestroy); |
| 112098 | 113199 | } |
| 112099 | 113200 | |
| 112100 | 113201 | /* |
| 112101 | 113202 | ** Lock the virtual table so that it cannot be disconnected. |
| | @@ -112324,11 +113425,16 @@ |
| 112324 | 113425 | pTable->tabFlags |= TF_Virtual; |
| 112325 | 113426 | pTable->nModuleArg = 0; |
| 112326 | 113427 | addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); |
| 112327 | 113428 | addModuleArgument(db, pTable, 0); |
| 112328 | 113429 | addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); |
| 112329 | | - pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z); |
| 113430 | + assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0) |
| 113431 | + || (pParse->sNameToken.z==pName1->z && pName2->z==0) |
| 113432 | + ); |
| 113433 | + pParse->sNameToken.n = (int)( |
| 113434 | + &pModuleName->z[pModuleName->n] - pParse->sNameToken.z |
| 113435 | + ); |
| 112330 | 113436 | |
| 112331 | 113437 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 112332 | 113438 | /* Creating a virtual table invokes the authorization callback twice. |
| 112333 | 113439 | ** The first invocation, to obtain permission to INSERT a row into the |
| 112334 | 113440 | ** sqlite_master table, has already been made by sqlite3StartTable(). |
| | @@ -112696,10 +113802,13 @@ |
| 112696 | 113802 | |
| 112697 | 113803 | int rc = SQLITE_OK; |
| 112698 | 113804 | Table *pTab; |
| 112699 | 113805 | char *zErr = 0; |
| 112700 | 113806 | |
| 113807 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 113808 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 113809 | +#endif |
| 112701 | 113810 | sqlite3_mutex_enter(db->mutex); |
| 112702 | 113811 | if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){ |
| 112703 | 113812 | sqlite3Error(db, SQLITE_MISUSE); |
| 112704 | 113813 | sqlite3_mutex_leave(db->mutex); |
| 112705 | 113814 | return SQLITE_MISUSE_BKPT; |
| | @@ -113052,10 +114161,13 @@ |
| 113052 | 114161 | */ |
| 113053 | 114162 | SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){ |
| 113054 | 114163 | static const unsigned char aMap[] = { |
| 113055 | 114164 | SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE |
| 113056 | 114165 | }; |
| 114166 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 114167 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 114168 | +#endif |
| 113057 | 114169 | assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 ); |
| 113058 | 114170 | assert( OE_Ignore==4 && OE_Replace==5 ); |
| 113059 | 114171 | assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 ); |
| 113060 | 114172 | return (int)aMap[db->vtabOnConflict-1]; |
| 113061 | 114173 | } |
| | @@ -113067,12 +114179,14 @@ |
| 113067 | 114179 | */ |
| 113068 | 114180 | SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){ |
| 113069 | 114181 | va_list ap; |
| 113070 | 114182 | int rc = SQLITE_OK; |
| 113071 | 114183 | |
| 114184 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 114185 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 114186 | +#endif |
| 113072 | 114187 | sqlite3_mutex_enter(db->mutex); |
| 113073 | | - |
| 113074 | 114188 | va_start(ap, op); |
| 113075 | 114189 | switch( op ){ |
| 113076 | 114190 | case SQLITE_VTAB_CONSTRAINT_SUPPORT: { |
| 113077 | 114191 | VtabCtx *p = db->pVtabCtx; |
| 113078 | 114192 | if( !p ){ |
| | @@ -113203,10 +114317,13 @@ |
| 113203 | 114317 | } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
| 113204 | 114318 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 113205 | 114319 | } u; |
| 113206 | 114320 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 113207 | 114321 | Bitmask notReady; /* FROM entries not usable at this level */ |
| 114322 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 114323 | + int addrVisit; /* Address at which row is visited */ |
| 114324 | +#endif |
| 113208 | 114325 | }; |
| 113209 | 114326 | |
| 113210 | 114327 | /* |
| 113211 | 114328 | ** Each instance of this object represents an algorithm for evaluating one |
| 113212 | 114329 | ** term of a join. Every term of the FROM clause will have at least |
| | @@ -113233,11 +114350,10 @@ |
| 113233 | 114350 | LogEst rRun; /* Cost of running each loop */ |
| 113234 | 114351 | LogEst nOut; /* Estimated number of output rows */ |
| 113235 | 114352 | union { |
| 113236 | 114353 | struct { /* Information for internal btree tables */ |
| 113237 | 114354 | u16 nEq; /* Number of equality constraints */ |
| 113238 | | - u16 nSkip; /* Number of initial index columns to skip */ |
| 113239 | 114355 | Index *pIndex; /* Index used, or NULL */ |
| 113240 | 114356 | } btree; |
| 113241 | 114357 | struct { /* Information for virtual tables */ |
| 113242 | 114358 | int idxNum; /* Index number */ |
| 113243 | 114359 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| | @@ -113246,16 +114362,17 @@ |
| 113246 | 114362 | char *idxStr; /* Index identifier string */ |
| 113247 | 114363 | } vtab; |
| 113248 | 114364 | } u; |
| 113249 | 114365 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 113250 | 114366 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 114367 | + u16 nSkip; /* Number of NULL aLTerm[] entries */ |
| 113251 | 114368 | /**** whereLoopXfer() copies fields above ***********************/ |
| 113252 | 114369 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 113253 | 114370 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
| 113254 | 114371 | WhereTerm **aLTerm; /* WhereTerms used */ |
| 113255 | 114372 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
| 113256 | | - WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ |
| 114373 | + WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */ |
| 113257 | 114374 | }; |
| 113258 | 114375 | |
| 113259 | 114376 | /* This object holds the prerequisites and the cost of running a |
| 113260 | 114377 | ** subquery on one operand of an OR operator in the WHERE clause. |
| 113261 | 114378 | ** See WhereOrSet for additional information |
| | @@ -113577,10 +114694,11 @@ |
| 113577 | 114694 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
| 113578 | 114695 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 113579 | 114696 | #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 113580 | 114697 | #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ |
| 113581 | 114698 | #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ |
| 114699 | +#define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */ |
| 113582 | 114700 | |
| 113583 | 114701 | /************** End of whereInt.h ********************************************/ |
| 113584 | 114702 | /************** Continuing where we left off in where.c **********************/ |
| 113585 | 114703 | |
| 113586 | 114704 | /* |
| | @@ -113784,14 +114902,15 @@ |
| 113784 | 114902 | memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 113785 | 114903 | if( pOld!=pWC->aStatic ){ |
| 113786 | 114904 | sqlite3DbFree(db, pOld); |
| 113787 | 114905 | } |
| 113788 | 114906 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
| 114907 | + memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm)); |
| 113789 | 114908 | } |
| 113790 | 114909 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
| 113791 | 114910 | if( p && ExprHasProperty(p, EP_Unlikely) ){ |
| 113792 | | - pTerm->truthProb = sqlite3LogEst(p->iTable) - 99; |
| 114911 | + pTerm->truthProb = sqlite3LogEst(p->iTable) - 270; |
| 113793 | 114912 | }else{ |
| 113794 | 114913 | pTerm->truthProb = 1; |
| 113795 | 114914 | } |
| 113796 | 114915 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
| 113797 | 114916 | pTerm->wtFlags = wtFlags; |
| | @@ -114317,10 +115436,19 @@ |
| 114317 | 115436 | if( pDerived ){ |
| 114318 | 115437 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 114319 | 115438 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 114320 | 115439 | } |
| 114321 | 115440 | } |
| 115441 | + |
| 115442 | +/* |
| 115443 | +** Mark term iChild as being a child of term iParent |
| 115444 | +*/ |
| 115445 | +static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ |
| 115446 | + pWC->a[iChild].iParent = iParent; |
| 115447 | + pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; |
| 115448 | + pWC->a[iParent].nChild++; |
| 115449 | +} |
| 114322 | 115450 | |
| 114323 | 115451 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 114324 | 115452 | /* |
| 114325 | 115453 | ** Analyze a term that consists of two or more OR-connected |
| 114326 | 115454 | ** subterms. So in: |
| | @@ -114615,12 +115743,11 @@ |
| 114615 | 115743 | pNew->x.pList = pList; |
| 114616 | 115744 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114617 | 115745 | testcase( idxNew==0 ); |
| 114618 | 115746 | exprAnalyze(pSrc, pWC, idxNew); |
| 114619 | 115747 | pTerm = &pWC->a[idxTerm]; |
| 114620 | | - pWC->a[idxNew].iParent = idxTerm; |
| 114621 | | - pTerm->nChild = 1; |
| 115748 | + markTermAsChild(pWC, idxNew, idxTerm); |
| 114622 | 115749 | }else{ |
| 114623 | 115750 | sqlite3ExprListDelete(db, pList); |
| 114624 | 115751 | } |
| 114625 | 115752 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
| 114626 | 115753 | } |
| | @@ -114718,13 +115845,12 @@ |
| 114718 | 115845 | return; |
| 114719 | 115846 | } |
| 114720 | 115847 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114721 | 115848 | if( idxNew==0 ) return; |
| 114722 | 115849 | pNew = &pWC->a[idxNew]; |
| 114723 | | - pNew->iParent = idxTerm; |
| 115850 | + markTermAsChild(pWC, idxNew, idxTerm); |
| 114724 | 115851 | pTerm = &pWC->a[idxTerm]; |
| 114725 | | - pTerm->nChild = 1; |
| 114726 | 115852 | pTerm->wtFlags |= TERM_COPIED; |
| 114727 | 115853 | if( pExpr->op==TK_EQ |
| 114728 | 115854 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 114729 | 115855 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 114730 | 115856 | ){ |
| | @@ -114777,13 +115903,12 @@ |
| 114777 | 115903 | transferJoinMarkings(pNewExpr, pExpr); |
| 114778 | 115904 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114779 | 115905 | testcase( idxNew==0 ); |
| 114780 | 115906 | exprAnalyze(pSrc, pWC, idxNew); |
| 114781 | 115907 | pTerm = &pWC->a[idxTerm]; |
| 114782 | | - pWC->a[idxNew].iParent = idxTerm; |
| 115908 | + markTermAsChild(pWC, idxNew, idxTerm); |
| 114783 | 115909 | } |
| 114784 | | - pTerm->nChild = 2; |
| 114785 | 115910 | } |
| 114786 | 115911 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
| 114787 | 115912 | |
| 114788 | 115913 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 114789 | 115914 | /* Analyze a term that is composed of two or more subterms connected by |
| | @@ -114854,13 +115979,12 @@ |
| 114854 | 115979 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
| 114855 | 115980 | testcase( idxNew2==0 ); |
| 114856 | 115981 | exprAnalyze(pSrc, pWC, idxNew2); |
| 114857 | 115982 | pTerm = &pWC->a[idxTerm]; |
| 114858 | 115983 | if( isComplete ){ |
| 114859 | | - pWC->a[idxNew1].iParent = idxTerm; |
| 114860 | | - pWC->a[idxNew2].iParent = idxTerm; |
| 114861 | | - pTerm->nChild = 2; |
| 115984 | + markTermAsChild(pWC, idxNew1, idxTerm); |
| 115985 | + markTermAsChild(pWC, idxNew2, idxTerm); |
| 114862 | 115986 | } |
| 114863 | 115987 | } |
| 114864 | 115988 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 114865 | 115989 | |
| 114866 | 115990 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -114889,13 +116013,12 @@ |
| 114889 | 116013 | pNewTerm = &pWC->a[idxNew]; |
| 114890 | 116014 | pNewTerm->prereqRight = prereqExpr; |
| 114891 | 116015 | pNewTerm->leftCursor = pLeft->iTable; |
| 114892 | 116016 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 114893 | 116017 | pNewTerm->eOperator = WO_MATCH; |
| 114894 | | - pNewTerm->iParent = idxTerm; |
| 116018 | + markTermAsChild(pWC, idxNew, idxTerm); |
| 114895 | 116019 | pTerm = &pWC->a[idxTerm]; |
| 114896 | | - pTerm->nChild = 1; |
| 114897 | 116020 | pTerm->wtFlags |= TERM_COPIED; |
| 114898 | 116021 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 114899 | 116022 | } |
| 114900 | 116023 | } |
| 114901 | 116024 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| | @@ -114912,11 +116035,11 @@ |
| 114912 | 116035 | ** the start of the loop will prevent any results from being returned. |
| 114913 | 116036 | */ |
| 114914 | 116037 | if( pExpr->op==TK_NOTNULL |
| 114915 | 116038 | && pExpr->pLeft->op==TK_COLUMN |
| 114916 | 116039 | && pExpr->pLeft->iColumn>=0 |
| 114917 | | - && OptimizationEnabled(db, SQLITE_Stat3) |
| 116040 | + && OptimizationEnabled(db, SQLITE_Stat34) |
| 114918 | 116041 | ){ |
| 114919 | 116042 | Expr *pNewExpr; |
| 114920 | 116043 | Expr *pLeft = pExpr->pLeft; |
| 114921 | 116044 | int idxNew; |
| 114922 | 116045 | WhereTerm *pNewTerm; |
| | @@ -114931,13 +116054,12 @@ |
| 114931 | 116054 | pNewTerm = &pWC->a[idxNew]; |
| 114932 | 116055 | pNewTerm->prereqRight = 0; |
| 114933 | 116056 | pNewTerm->leftCursor = pLeft->iTable; |
| 114934 | 116057 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 114935 | 116058 | pNewTerm->eOperator = WO_GT; |
| 114936 | | - pNewTerm->iParent = idxTerm; |
| 116059 | + markTermAsChild(pWC, idxNew, idxTerm); |
| 114937 | 116060 | pTerm = &pWC->a[idxTerm]; |
| 114938 | | - pTerm->nChild = 1; |
| 114939 | 116061 | pTerm->wtFlags |= TERM_COPIED; |
| 114940 | 116062 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 114941 | 116063 | } |
| 114942 | 116064 | } |
| 114943 | 116065 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| | @@ -115153,10 +116275,12 @@ |
| 115153 | 116275 | WhereLoop *pLoop; /* The Loop object */ |
| 115154 | 116276 | char *zNotUsed; /* Extra space on the end of pIdx */ |
| 115155 | 116277 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 115156 | 116278 | Bitmask extraCols; /* Bitmap of additional columns */ |
| 115157 | 116279 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
| 116280 | + Expr *pPartial = 0; /* Partial Index Expression */ |
| 116281 | + int iContinue = 0; /* Jump here to skip excluded rows */ |
| 115158 | 116282 | |
| 115159 | 116283 | /* Generate code to skip over the creation and initialization of the |
| 115160 | 116284 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 115161 | 116285 | v = pParse->pVdbe; |
| 115162 | 116286 | assert( v!=0 ); |
| | @@ -115168,10 +116292,16 @@ |
| 115168 | 116292 | pTable = pSrc->pTab; |
| 115169 | 116293 | pWCEnd = &pWC->a[pWC->nTerm]; |
| 115170 | 116294 | pLoop = pLevel->pWLoop; |
| 115171 | 116295 | idxCols = 0; |
| 115172 | 116296 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 116297 | + if( pLoop->prereq==0 |
| 116298 | + && (pTerm->wtFlags & TERM_VIRTUAL)==0 |
| 116299 | + && sqlite3ExprIsTableConstant(pTerm->pExpr, pSrc->iCursor) ){ |
| 116300 | + pPartial = sqlite3ExprAnd(pParse->db, pPartial, |
| 116301 | + sqlite3ExprDup(pParse->db, pTerm->pExpr, 0)); |
| 116302 | + } |
| 115173 | 116303 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 115174 | 116304 | int iCol = pTerm->u.leftColumn; |
| 115175 | 116305 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
| 115176 | 116306 | testcase( iCol==BMS ); |
| 115177 | 116307 | testcase( iCol==BMS-1 ); |
| | @@ -115180,11 +116310,13 @@ |
| 115180 | 116310 | "automatic index on %s(%s)", pTable->zName, |
| 115181 | 116311 | pTable->aCol[iCol].zName); |
| 115182 | 116312 | sentWarning = 1; |
| 115183 | 116313 | } |
| 115184 | 116314 | if( (idxCols & cMask)==0 ){ |
| 115185 | | - if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return; |
| 116315 | + if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){ |
| 116316 | + goto end_auto_index_create; |
| 116317 | + } |
| 115186 | 116318 | pLoop->aLTerm[nKeyCol++] = pTerm; |
| 115187 | 116319 | idxCols |= cMask; |
| 115188 | 116320 | } |
| 115189 | 116321 | } |
| 115190 | 116322 | } |
| | @@ -115200,24 +116332,23 @@ |
| 115200 | 116332 | ** be a covering index because the index will not be updated if the |
| 115201 | 116333 | ** original table changes and the index and table cannot both be used |
| 115202 | 116334 | ** if they go out of sync. |
| 115203 | 116335 | */ |
| 115204 | 116336 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
| 115205 | | - mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
| 116337 | + mxBitCol = MIN(BMS-1,pTable->nCol); |
| 115206 | 116338 | testcase( pTable->nCol==BMS-1 ); |
| 115207 | 116339 | testcase( pTable->nCol==BMS-2 ); |
| 115208 | 116340 | for(i=0; i<mxBitCol; i++){ |
| 115209 | 116341 | if( extraCols & MASKBIT(i) ) nKeyCol++; |
| 115210 | 116342 | } |
| 115211 | 116343 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 115212 | 116344 | nKeyCol += pTable->nCol - BMS + 1; |
| 115213 | 116345 | } |
| 115214 | | - pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
| 115215 | 116346 | |
| 115216 | 116347 | /* Construct the Index object to describe this index */ |
| 115217 | 116348 | pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); |
| 115218 | | - if( pIdx==0 ) return; |
| 116349 | + if( pIdx==0 ) goto end_auto_index_create; |
| 115219 | 116350 | pLoop->u.btree.pIndex = pIdx; |
| 115220 | 116351 | pIdx->zName = "auto-index"; |
| 115221 | 116352 | pIdx->pTable = pTable; |
| 115222 | 116353 | n = 0; |
| 115223 | 116354 | idxCols = 0; |
| | @@ -115265,22 +116396,33 @@ |
| 115265 | 116396 | sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); |
| 115266 | 116397 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 115267 | 116398 | VdbeComment((v, "for %s", pTable->zName)); |
| 115268 | 116399 | |
| 115269 | 116400 | /* Fill the automatic index with content */ |
| 116401 | + sqlite3ExprCachePush(pParse); |
| 115270 | 116402 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); |
| 116403 | + if( pPartial ){ |
| 116404 | + iContinue = sqlite3VdbeMakeLabel(v); |
| 116405 | + sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); |
| 116406 | + pLoop->wsFlags |= WHERE_PARTIALIDX; |
| 116407 | + } |
| 115271 | 116408 | regRecord = sqlite3GetTempReg(pParse); |
| 115272 | 116409 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0); |
| 115273 | 116410 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 115274 | 116411 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 116412 | + if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); |
| 115275 | 116413 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); |
| 115276 | 116414 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
| 115277 | 116415 | sqlite3VdbeJumpHere(v, addrTop); |
| 115278 | 116416 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 116417 | + sqlite3ExprCachePop(pParse); |
| 115279 | 116418 | |
| 115280 | 116419 | /* Jump here when skipping the initialization */ |
| 115281 | 116420 | sqlite3VdbeJumpHere(v, addrInit); |
| 116421 | + |
| 116422 | +end_auto_index_create: |
| 116423 | + sqlite3ExprDelete(pParse->db, pPartial); |
| 115282 | 116424 | } |
| 115283 | 116425 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 115284 | 116426 | |
| 115285 | 116427 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 115286 | 116428 | /* |
| | @@ -115436,22 +116578,22 @@ |
| 115436 | 116578 | |
| 115437 | 116579 | return pParse->nErr; |
| 115438 | 116580 | } |
| 115439 | 116581 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 115440 | 116582 | |
| 115441 | | - |
| 115442 | 116583 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 115443 | 116584 | /* |
| 115444 | 116585 | ** Estimate the location of a particular key among all keys in an |
| 115445 | 116586 | ** index. Store the results in aStat as follows: |
| 115446 | 116587 | ** |
| 115447 | 116588 | ** aStat[0] Est. number of rows less than pVal |
| 115448 | 116589 | ** aStat[1] Est. number of rows equal to pVal |
| 115449 | 116590 | ** |
| 115450 | | -** Return SQLITE_OK on success. |
| 116591 | +** Return the index of the sample that is the smallest sample that |
| 116592 | +** is greater than or equal to pRec. |
| 115451 | 116593 | */ |
| 115452 | | -static void whereKeyStats( |
| 116594 | +static int whereKeyStats( |
| 115453 | 116595 | Parse *pParse, /* Database connection */ |
| 115454 | 116596 | Index *pIdx, /* Index to consider domain of */ |
| 115455 | 116597 | UnpackedRecord *pRec, /* Vector of values to consider */ |
| 115456 | 116598 | int roundUp, /* Round up if true. Round down if false */ |
| 115457 | 116599 | tRowcnt *aStat /* OUT: stats written here */ |
| | @@ -115529,10 +116671,11 @@ |
| 115529 | 116671 | }else{ |
| 115530 | 116672 | iGap = iGap/3; |
| 115531 | 116673 | } |
| 115532 | 116674 | aStat[0] = iLower + iGap; |
| 115533 | 116675 | } |
| 116676 | + return i; |
| 115534 | 116677 | } |
| 115535 | 116678 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 115536 | 116679 | |
| 115537 | 116680 | /* |
| 115538 | 116681 | ** If it is not NULL, pTerm is a term that provides an upper or lower |
| | @@ -115679,11 +116822,11 @@ |
| 115679 | 116822 | ** pLower pUpper |
| 115680 | 116823 | ** |
| 115681 | 116824 | ** If either of the upper or lower bound is not present, then NULL is passed in |
| 115682 | 116825 | ** place of the corresponding WhereTerm. |
| 115683 | 116826 | ** |
| 115684 | | -** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index |
| 116827 | +** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index |
| 115685 | 116828 | ** column subject to the range constraint. Or, equivalently, the number of |
| 115686 | 116829 | ** equality constraints optimized by the proposed index scan. For example, |
| 115687 | 116830 | ** assuming index p is on t1(a, b), and the SQL query is: |
| 115688 | 116831 | ** |
| 115689 | 116832 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| | @@ -115695,11 +116838,11 @@ |
| 115695 | 116838 | ** |
| 115696 | 116839 | ** then nEq is set to 0. |
| 115697 | 116840 | ** |
| 115698 | 116841 | ** When this function is called, *pnOut is set to the sqlite3LogEst() of the |
| 115699 | 116842 | ** number of rows that the index scan is expected to visit without |
| 115700 | | -** considering the range constraints. If nEq is 0, this is the number of |
| 116843 | +** considering the range constraints. If nEq is 0, then *pnOut is the number of |
| 115701 | 116844 | ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) |
| 115702 | 116845 | ** to account for the range constraints pLower and pUpper. |
| 115703 | 116846 | ** |
| 115704 | 116847 | ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be |
| 115705 | 116848 | ** used, a single range inequality reduces the search space by a factor of 4. |
| | @@ -115719,14 +116862,11 @@ |
| 115719 | 116862 | |
| 115720 | 116863 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 115721 | 116864 | Index *p = pLoop->u.btree.pIndex; |
| 115722 | 116865 | int nEq = pLoop->u.btree.nEq; |
| 115723 | 116866 | |
| 115724 | | - if( p->nSample>0 |
| 115725 | | - && nEq<p->nSampleCol |
| 115726 | | - && OptimizationEnabled(pParse->db, SQLITE_Stat3) |
| 115727 | | - ){ |
| 116867 | + if( p->nSample>0 && nEq<p->nSampleCol ){ |
| 115728 | 116868 | if( nEq==pBuilder->nRecValid ){ |
| 115729 | 116869 | UnpackedRecord *pRec = pBuilder->pRec; |
| 115730 | 116870 | tRowcnt a[2]; |
| 115731 | 116871 | u8 aff; |
| 115732 | 116872 | |
| | @@ -115738,19 +116878,23 @@ |
| 115738 | 116878 | ** |
| 115739 | 116879 | ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 115740 | 116880 | ** is not a simple variable or literal value), the lower bound of the |
| 115741 | 116881 | ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 115742 | 116882 | ** if $L is available, whereKeyStats() is called for both ($P) and |
| 115743 | | - ** ($P:$L) and the larger of the two returned values used. |
| 116883 | + ** ($P:$L) and the larger of the two returned values is used. |
| 115744 | 116884 | ** |
| 115745 | 116885 | ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 115746 | 116886 | ** less than the upper bound of the range query. Where the upper bound |
| 115747 | 116887 | ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 115748 | 116888 | ** of iUpper are requested of whereKeyStats() and the smaller used. |
| 116889 | + ** |
| 116890 | + ** The number of rows between the two bounds is then just iUpper-iLower. |
| 115749 | 116891 | */ |
| 115750 | | - tRowcnt iLower; |
| 115751 | | - tRowcnt iUpper; |
| 116892 | + tRowcnt iLower; /* Rows less than the lower bound */ |
| 116893 | + tRowcnt iUpper; /* Rows less than the upper bound */ |
| 116894 | + int iLwrIdx = -2; /* aSample[] for the lower bound */ |
| 116895 | + int iUprIdx = -1; /* aSample[] for the upper bound */ |
| 115752 | 116896 | |
| 115753 | 116897 | if( pRec ){ |
| 115754 | 116898 | testcase( pRec->nField!=pBuilder->nRecValid ); |
| 115755 | 116899 | pRec->nField = pBuilder->nRecValid; |
| 115756 | 116900 | } |
| | @@ -115760,11 +116904,11 @@ |
| 115760 | 116904 | aff = p->pTable->aCol[p->aiColumn[nEq]].affinity; |
| 115761 | 116905 | } |
| 115762 | 116906 | /* Determine iLower and iUpper using ($P) only. */ |
| 115763 | 116907 | if( nEq==0 ){ |
| 115764 | 116908 | iLower = 0; |
| 115765 | | - iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]); |
| 116909 | + iUpper = p->nRowEst0; |
| 115766 | 116910 | }else{ |
| 115767 | 116911 | /* Note: this call could be optimized away - since the same values must |
| 115768 | 116912 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 115769 | 116913 | whereKeyStats(pParse, p, pRec, 0, a); |
| 115770 | 116914 | iLower = a[0]; |
| | @@ -115784,11 +116928,11 @@ |
| 115784 | 116928 | int bOk; /* True if value is extracted from pExpr */ |
| 115785 | 116929 | Expr *pExpr = pLower->pExpr->pRight; |
| 115786 | 116930 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 115787 | 116931 | if( rc==SQLITE_OK && bOk ){ |
| 115788 | 116932 | tRowcnt iNew; |
| 115789 | | - whereKeyStats(pParse, p, pRec, 0, a); |
| 116933 | + iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a); |
| 115790 | 116934 | iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 115791 | 116935 | if( iNew>iLower ) iLower = iNew; |
| 115792 | 116936 | nOut--; |
| 115793 | 116937 | pLower = 0; |
| 115794 | 116938 | } |
| | @@ -115799,11 +116943,11 @@ |
| 115799 | 116943 | int bOk; /* True if value is extracted from pExpr */ |
| 115800 | 116944 | Expr *pExpr = pUpper->pExpr->pRight; |
| 115801 | 116945 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 115802 | 116946 | if( rc==SQLITE_OK && bOk ){ |
| 115803 | 116947 | tRowcnt iNew; |
| 115804 | | - whereKeyStats(pParse, p, pRec, 1, a); |
| 116948 | + iUprIdx = whereKeyStats(pParse, p, pRec, 1, a); |
| 115805 | 116949 | iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 115806 | 116950 | if( iNew<iUpper ) iUpper = iNew; |
| 115807 | 116951 | nOut--; |
| 115808 | 116952 | pUpper = 0; |
| 115809 | 116953 | } |
| | @@ -115811,10 +116955,15 @@ |
| 115811 | 116955 | |
| 115812 | 116956 | pBuilder->pRec = pRec; |
| 115813 | 116957 | if( rc==SQLITE_OK ){ |
| 115814 | 116958 | if( iUpper>iLower ){ |
| 115815 | 116959 | nNew = sqlite3LogEst(iUpper - iLower); |
| 116960 | + /* TUNING: If both iUpper and iLower are derived from the same |
| 116961 | + ** sample, then assume they are 4x more selective. This brings |
| 116962 | + ** the estimated selectivity more in line with what it would be |
| 116963 | + ** if estimated without the use of STAT3/4 tables. */ |
| 116964 | + if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) ); |
| 115816 | 116965 | }else{ |
| 115817 | 116966 | nNew = 10; assert( 10==sqlite3LogEst(2) ); |
| 115818 | 116967 | } |
| 115819 | 116968 | if( nNew<nOut ){ |
| 115820 | 116969 | nOut = nNew; |
| | @@ -115835,16 +116984,19 @@ |
| 115835 | 116984 | #endif |
| 115836 | 116985 | assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 ); |
| 115837 | 116986 | nNew = whereRangeAdjust(pLower, nOut); |
| 115838 | 116987 | nNew = whereRangeAdjust(pUpper, nNew); |
| 115839 | 116988 | |
| 115840 | | - /* TUNING: If there is both an upper and lower limit, assume the range is |
| 116989 | + /* TUNING: If there is both an upper and lower limit and neither limit |
| 116990 | + ** has an application-defined likelihood(), assume the range is |
| 115841 | 116991 | ** reduced by an additional 75%. This means that, by default, an open-ended |
| 115842 | 116992 | ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the |
| 115843 | 116993 | ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to |
| 115844 | 116994 | ** match 1/64 of the index. */ |
| 115845 | | - if( pLower && pUpper ) nNew -= 20; |
| 116995 | + if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){ |
| 116996 | + nNew -= 20; |
| 116997 | + } |
| 115846 | 116998 | |
| 115847 | 116999 | nOut -= (pLower!=0) + (pUpper!=0); |
| 115848 | 117000 | if( nNew<10 ) nNew = 10; |
| 115849 | 117001 | if( nNew<nOut ) nOut = nNew; |
| 115850 | 117002 | #if defined(WHERETRACE_ENABLED) |
| | @@ -116200,11 +117352,11 @@ |
| 116200 | 117352 | |
| 116201 | 117353 | /* This module is only called on query plans that use an index. */ |
| 116202 | 117354 | pLoop = pLevel->pWLoop; |
| 116203 | 117355 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 116204 | 117356 | nEq = pLoop->u.btree.nEq; |
| 116205 | | - nSkip = pLoop->u.btree.nSkip; |
| 117357 | + nSkip = pLoop->nSkip; |
| 116206 | 117358 | pIdx = pLoop->u.btree.pIndex; |
| 116207 | 117359 | assert( pIdx!=0 ); |
| 116208 | 117360 | |
| 116209 | 117361 | /* Figure out how many memory cells we will need then allocate them. |
| 116210 | 117362 | */ |
| | @@ -116314,11 +117466,11 @@ |
| 116314 | 117466 | ** "a=? AND b>?" |
| 116315 | 117467 | */ |
| 116316 | 117468 | static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){ |
| 116317 | 117469 | Index *pIndex = pLoop->u.btree.pIndex; |
| 116318 | 117470 | u16 nEq = pLoop->u.btree.nEq; |
| 116319 | | - u16 nSkip = pLoop->u.btree.nSkip; |
| 117471 | + u16 nSkip = pLoop->nSkip; |
| 116320 | 117472 | int i, j; |
| 116321 | 117473 | Column *aCol = pTab->aCol; |
| 116322 | 117474 | i16 *aiColumn = pIndex->aiColumn; |
| 116323 | 117475 | |
| 116324 | 117476 | if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
| | @@ -116345,23 +117497,27 @@ |
| 116345 | 117497 | sqlite3StrAccumAppend(pStr, ")", 1); |
| 116346 | 117498 | } |
| 116347 | 117499 | |
| 116348 | 117500 | /* |
| 116349 | 117501 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 116350 | | -** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| 116351 | | -** record is added to the output to describe the table scan strategy in |
| 116352 | | -** pLevel. |
| 117502 | +** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was |
| 117503 | +** defined at compile-time. If it is not a no-op, a single OP_Explain opcode |
| 117504 | +** is added to the output to describe the table scan strategy in pLevel. |
| 117505 | +** |
| 117506 | +** If an OP_Explain opcode is added to the VM, its address is returned. |
| 117507 | +** Otherwise, if no OP_Explain is coded, zero is returned. |
| 116353 | 117508 | */ |
| 116354 | | -static void explainOneScan( |
| 117509 | +static int explainOneScan( |
| 116355 | 117510 | Parse *pParse, /* Parse context */ |
| 116356 | 117511 | SrcList *pTabList, /* Table list this loop refers to */ |
| 116357 | 117512 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 116358 | 117513 | int iLevel, /* Value for "level" column of output */ |
| 116359 | 117514 | int iFrom, /* Value for "from" column of output */ |
| 116360 | 117515 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 116361 | 117516 | ){ |
| 116362 | | -#ifndef SQLITE_DEBUG |
| 117517 | + int ret = 0; |
| 117518 | +#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 116363 | 117519 | if( pParse->explain==2 ) |
| 116364 | 117520 | #endif |
| 116365 | 117521 | { |
| 116366 | 117522 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 116367 | 117523 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| | @@ -116374,11 +117530,11 @@ |
| 116374 | 117530 | StrAccum str; /* EQP output string */ |
| 116375 | 117531 | char zBuf[100]; /* Initial space for EQP output string */ |
| 116376 | 117532 | |
| 116377 | 117533 | pLoop = pLevel->pWLoop; |
| 116378 | 117534 | flags = pLoop->wsFlags; |
| 116379 | | - if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
| 117535 | + if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0; |
| 116380 | 117536 | |
| 116381 | 117537 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 116382 | 117538 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 116383 | 117539 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 116384 | 117540 | |
| | @@ -116403,10 +117559,12 @@ |
| 116403 | 117559 | assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
| 116404 | 117560 | if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
| 116405 | 117561 | if( isSearch ){ |
| 116406 | 117562 | zFmt = "PRIMARY KEY"; |
| 116407 | 117563 | } |
| 117564 | + }else if( flags & WHERE_PARTIALIDX ){ |
| 117565 | + zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; |
| 116408 | 117566 | }else if( flags & WHERE_AUTO_INDEX ){ |
| 116409 | 117567 | zFmt = "AUTOMATIC COVERING INDEX"; |
| 116410 | 117568 | }else if( flags & WHERE_IDX_ONLY ){ |
| 116411 | 117569 | zFmt = "COVERING INDEX %s"; |
| 116412 | 117570 | }else{ |
| | @@ -116444,16 +117602,49 @@ |
| 116444 | 117602 | }else{ |
| 116445 | 117603 | sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
| 116446 | 117604 | } |
| 116447 | 117605 | #endif |
| 116448 | 117606 | zMsg = sqlite3StrAccumFinish(&str); |
| 116449 | | - sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
| 117607 | + ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC); |
| 116450 | 117608 | } |
| 117609 | + return ret; |
| 116451 | 117610 | } |
| 116452 | 117611 | #else |
| 116453 | | -# define explainOneScan(u,v,w,x,y,z) |
| 117612 | +# define explainOneScan(u,v,w,x,y,z) 0 |
| 116454 | 117613 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 117614 | + |
| 117615 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 117616 | +/* |
| 117617 | +** Configure the VM passed as the first argument with an |
| 117618 | +** sqlite3_stmt_scanstatus() entry corresponding to the scan used to |
| 117619 | +** implement level pLvl. Argument pSrclist is a pointer to the FROM |
| 117620 | +** clause that the scan reads data from. |
| 117621 | +** |
| 117622 | +** If argument addrExplain is not 0, it must be the address of an |
| 117623 | +** OP_Explain instruction that describes the same loop. |
| 117624 | +*/ |
| 117625 | +static void addScanStatus( |
| 117626 | + Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 117627 | + SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 117628 | + WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 117629 | + int addrExplain /* Address of OP_Explain (or 0) */ |
| 117630 | +){ |
| 117631 | + const char *zObj = 0; |
| 117632 | + WhereLoop *pLoop = pLvl->pWLoop; |
| 117633 | + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){ |
| 117634 | + zObj = pLoop->u.btree.pIndex->zName; |
| 117635 | + }else{ |
| 117636 | + zObj = pSrclist->a[pLvl->iFrom].zName; |
| 117637 | + } |
| 117638 | + sqlite3VdbeScanStatus( |
| 117639 | + v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj |
| 117640 | + ); |
| 117641 | +} |
| 117642 | +#else |
| 117643 | +# define addScanStatus(a, b, c, d) ((void)d) |
| 117644 | +#endif |
| 117645 | + |
| 116455 | 117646 | |
| 116456 | 117647 | |
| 116457 | 117648 | /* |
| 116458 | 117649 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 116459 | 117650 | ** implementation described by pWInfo. |
| | @@ -116751,11 +117942,11 @@ |
| 116751 | 117942 | u8 bSeekPastNull = 0; /* True to seek past initial nulls */ |
| 116752 | 117943 | u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ |
| 116753 | 117944 | |
| 116754 | 117945 | pIdx = pLoop->u.btree.pIndex; |
| 116755 | 117946 | iIdxCur = pLevel->iIdxCur; |
| 116756 | | - assert( nEq>=pLoop->u.btree.nSkip ); |
| 117947 | + assert( nEq>=pLoop->nSkip ); |
| 116757 | 117948 | |
| 116758 | 117949 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 116759 | 117950 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 116760 | 117951 | ** query, then the caller will only allow the loop to run for |
| 116761 | 117952 | ** a single iteration. This means that the first row returned |
| | @@ -116768,11 +117959,11 @@ |
| 116768 | 117959 | || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); |
| 116769 | 117960 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 116770 | 117961 | && pWInfo->nOBSat>0 |
| 116771 | 117962 | && (pIdx->nKeyCol>nEq) |
| 116772 | 117963 | ){ |
| 116773 | | - assert( pLoop->u.btree.nSkip==0 ); |
| 117964 | + assert( pLoop->nSkip==0 ); |
| 116774 | 117965 | bSeekPastNull = 1; |
| 116775 | 117966 | nExtraReg = 1; |
| 116776 | 117967 | } |
| 116777 | 117968 | |
| 116778 | 117969 | /* Find any inequality constraint terms for the start and end |
| | @@ -117081,14 +118272,13 @@ |
| 117081 | 118272 | int iTerm; |
| 117082 | 118273 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 117083 | 118274 | Expr *pExpr = pWC->a[iTerm].pExpr; |
| 117084 | 118275 | if( &pWC->a[iTerm] == pTerm ) continue; |
| 117085 | 118276 | if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
| 117086 | | - testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); |
| 117087 | | - testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL ); |
| 117088 | | - if( pWC->a[iTerm].wtFlags & (TERM_ORINFO|TERM_VIRTUAL) ) continue; |
| 118277 | + if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue; |
| 117089 | 118278 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
| 118279 | + testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); |
| 117090 | 118280 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 117091 | 118281 | pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); |
| 117092 | 118282 | } |
| 117093 | 118283 | if( pAndExpr ){ |
| 117094 | 118284 | pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| | @@ -117117,13 +118307,15 @@ |
| 117117 | 118307 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 117118 | 118308 | wctrlFlags, iCovCur); |
| 117119 | 118309 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
| 117120 | 118310 | if( pSubWInfo ){ |
| 117121 | 118311 | WhereLoop *pSubLoop; |
| 117122 | | - explainOneScan( |
| 118312 | + int addrExplain = explainOneScan( |
| 117123 | 118313 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 117124 | 118314 | ); |
| 118315 | + addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain); |
| 118316 | + |
| 117125 | 118317 | /* This is the sub-WHERE clause body. First skip over |
| 117126 | 118318 | ** duplicate rows from prior sub-WHERE clauses, and record the |
| 117127 | 118319 | ** rowid (or PRIMARY KEY) for the current row so that the same |
| 117128 | 118320 | ** row will be skipped in subsequent sub-WHERE clauses. |
| 117129 | 118321 | */ |
| | @@ -117249,10 +118441,14 @@ |
| 117249 | 118441 | VdbeCoverageIf(v, bRev==0); |
| 117250 | 118442 | VdbeCoverageIf(v, bRev!=0); |
| 117251 | 118443 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 117252 | 118444 | } |
| 117253 | 118445 | } |
| 118446 | + |
| 118447 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 118448 | + pLevel->addrVisit = sqlite3VdbeCurrentAddr(v); |
| 118449 | +#endif |
| 117254 | 118450 | |
| 117255 | 118451 | /* Insert code to test every subexpression that can be completely |
| 117256 | 118452 | ** computed using the current set of tables. |
| 117257 | 118453 | */ |
| 117258 | 118454 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| | @@ -117389,11 +118585,11 @@ |
| 117389 | 118585 | } |
| 117390 | 118586 | sqlite3DebugPrintf(" %-19s", z); |
| 117391 | 118587 | sqlite3_free(z); |
| 117392 | 118588 | } |
| 117393 | 118589 | if( p->wsFlags & WHERE_SKIPSCAN ){ |
| 117394 | | - sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip); |
| 118590 | + sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip); |
| 117395 | 118591 | }else{ |
| 117396 | 118592 | sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); |
| 117397 | 118593 | } |
| 117398 | 118594 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
| 117399 | 118595 | if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ |
| | @@ -117500,34 +118696,41 @@ |
| 117500 | 118696 | sqlite3DbFree(db, pWInfo); |
| 117501 | 118697 | } |
| 117502 | 118698 | } |
| 117503 | 118699 | |
| 117504 | 118700 | /* |
| 117505 | | -** Return TRUE if both of the following are true: |
| 118701 | +** Return TRUE if all of the following are true: |
| 117506 | 118702 | ** |
| 117507 | 118703 | ** (1) X has the same or lower cost that Y |
| 117508 | 118704 | ** (2) X is a proper subset of Y |
| 118705 | +** (3) X skips at least as many columns as Y |
| 117509 | 118706 | ** |
| 117510 | 118707 | ** By "proper subset" we mean that X uses fewer WHERE clause terms |
| 117511 | 118708 | ** than Y and that every WHERE clause term used by X is also used |
| 117512 | 118709 | ** by Y. |
| 117513 | 118710 | ** |
| 117514 | 118711 | ** If X is a proper subset of Y then Y is a better choice and ought |
| 117515 | 118712 | ** to have a lower cost. This routine returns TRUE when that cost |
| 117516 | | -** relationship is inverted and needs to be adjusted. |
| 118713 | +** relationship is inverted and needs to be adjusted. The third rule |
| 118714 | +** was added because if X uses skip-scan less than Y it still might |
| 118715 | +** deserve a lower cost even if it is a proper subset of Y. |
| 117517 | 118716 | */ |
| 117518 | 118717 | static int whereLoopCheaperProperSubset( |
| 117519 | 118718 | const WhereLoop *pX, /* First WhereLoop to compare */ |
| 117520 | 118719 | const WhereLoop *pY /* Compare against this WhereLoop */ |
| 117521 | 118720 | ){ |
| 117522 | 118721 | int i, j; |
| 117523 | | - if( pX->nLTerm >= pY->nLTerm ) return 0; /* X is not a subset of Y */ |
| 118722 | + if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){ |
| 118723 | + return 0; /* X is not a subset of Y */ |
| 118724 | + } |
| 118725 | + if( pY->nSkip > pX->nSkip ) return 0; |
| 117524 | 118726 | if( pX->rRun >= pY->rRun ){ |
| 117525 | 118727 | if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ |
| 117526 | 118728 | if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ |
| 117527 | 118729 | } |
| 117528 | 118730 | for(i=pX->nLTerm-1; i>=0; i--){ |
| 118731 | + if( pX->aLTerm[i]==0 ) continue; |
| 117529 | 118732 | for(j=pY->nLTerm-1; j>=0; j--){ |
| 117530 | 118733 | if( pY->aLTerm[j]==pX->aLTerm[i] ) break; |
| 117531 | 118734 | } |
| 117532 | 118735 | if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ |
| 117533 | 118736 | } |
| | @@ -117545,37 +118748,28 @@ |
| 117545 | 118748 | ** is a proper subset. |
| 117546 | 118749 | ** |
| 117547 | 118750 | ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer |
| 117548 | 118751 | ** WHERE clause terms than Y and that every WHERE clause term used by X is |
| 117549 | 118752 | ** also used by Y. |
| 117550 | | -** |
| 117551 | | -** This adjustment is omitted for SKIPSCAN loops. In a SKIPSCAN loop, the |
| 117552 | | -** WhereLoop.nLTerm field is not an accurate measure of the number of WHERE |
| 117553 | | -** clause terms covered, since some of the first nLTerm entries in aLTerm[] |
| 117554 | | -** will be NULL (because they are skipped). That makes it more difficult |
| 117555 | | -** to compare the loops. We could add extra code to do the comparison, and |
| 117556 | | -** perhaps we will someday. But SKIPSCAN is sufficiently uncommon, and this |
| 117557 | | -** adjustment is sufficient minor, that it is very difficult to construct |
| 117558 | | -** a test case where the extra code would improve the query plan. Better |
| 117559 | | -** to avoid the added complexity and just omit cost adjustments to SKIPSCAN |
| 117560 | | -** loops. |
| 117561 | 118753 | */ |
| 117562 | 118754 | static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ |
| 117563 | 118755 | if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return; |
| 117564 | | - if( (pTemplate->wsFlags & WHERE_SKIPSCAN)!=0 ) return; |
| 117565 | 118756 | for(; p; p=p->pNextLoop){ |
| 117566 | 118757 | if( p->iTab!=pTemplate->iTab ) continue; |
| 117567 | 118758 | if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; |
| 117568 | | - if( (p->wsFlags & WHERE_SKIPSCAN)!=0 ) continue; |
| 117569 | 118759 | if( whereLoopCheaperProperSubset(p, pTemplate) ){ |
| 117570 | 118760 | /* Adjust pTemplate cost downward so that it is cheaper than its |
| 117571 | | - ** subset p */ |
| 118761 | + ** subset p. */ |
| 118762 | + WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 118763 | + pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1)); |
| 117572 | 118764 | pTemplate->rRun = p->rRun; |
| 117573 | 118765 | pTemplate->nOut = p->nOut - 1; |
| 117574 | 118766 | }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ |
| 117575 | 118767 | /* Adjust pTemplate cost upward so that it is costlier than p since |
| 117576 | 118768 | ** pTemplate is a proper subset of p */ |
| 118769 | + WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 118770 | + pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1)); |
| 117577 | 118771 | pTemplate->rRun = p->rRun; |
| 117578 | 118772 | pTemplate->nOut = p->nOut + 1; |
| 117579 | 118773 | } |
| 117580 | 118774 | } |
| 117581 | 118775 | } |
| | @@ -117616,12 +118810,13 @@ |
| 117616 | 118810 | ** rSetup. Call this SETUP-INVARIANT */ |
| 117617 | 118811 | assert( p->rSetup>=pTemplate->rSetup ); |
| 117618 | 118812 | |
| 117619 | 118813 | /* Any loop using an appliation-defined index (or PRIMARY KEY or |
| 117620 | 118814 | ** UNIQUE constraint) with one or more == constraints is better |
| 117621 | | - ** than an automatic index. */ |
| 118815 | + ** than an automatic index. Unless it is a skip-scan. */ |
| 117622 | 118816 | if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 |
| 118817 | + && (pTemplate->nSkip)==0 |
| 117623 | 118818 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 117624 | 118819 | && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0 |
| 117625 | 118820 | && (p->prereq & pTemplate->prereq)==pTemplate->prereq |
| 117626 | 118821 | ){ |
| 117627 | 118822 | break; |
| | @@ -117776,25 +118971,46 @@ |
| 117776 | 118971 | |
| 117777 | 118972 | /* |
| 117778 | 118973 | ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 117779 | 118974 | ** WHERE clause that reference the loop but which are not used by an |
| 117780 | 118975 | ** index. |
| 118976 | +* |
| 118977 | +** For every WHERE clause term that is not used by the index |
| 118978 | +** and which has a truth probability assigned by one of the likelihood(), |
| 118979 | +** likely(), or unlikely() SQL functions, reduce the estimated number |
| 118980 | +** of output rows by the probability specified. |
| 117781 | 118981 | ** |
| 117782 | | -** In the current implementation, the first extra WHERE clause term reduces |
| 117783 | | -** the number of output rows by a factor of 10 and each additional term |
| 117784 | | -** reduces the number of output rows by sqrt(2). |
| 118982 | +** TUNING: For every WHERE clause term that is not used by the index |
| 118983 | +** and which does not have an assigned truth probability, heuristics |
| 118984 | +** described below are used to try to estimate the truth probability. |
| 118985 | +** TODO --> Perhaps this is something that could be improved by better |
| 118986 | +** table statistics. |
| 118987 | +** |
| 118988 | +** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75% |
| 118989 | +** value corresponds to -1 in LogEst notation, so this means decrement |
| 118990 | +** the WhereLoop.nOut field for every such WHERE clause term. |
| 118991 | +** |
| 118992 | +** Heuristic 2: If there exists one or more WHERE clause terms of the |
| 118993 | +** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the |
| 118994 | +** final output row estimate is no greater than 1/4 of the total number |
| 118995 | +** of rows in the table. In other words, assume that x==EXPR will filter |
| 118996 | +** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the |
| 118997 | +** "x" column is boolean or else -1 or 0 or 1 is a common default value |
| 118998 | +** on the "x" column and so in that case only cap the output row estimate |
| 118999 | +** at 1/2 instead of 1/4. |
| 117785 | 119000 | */ |
| 117786 | 119001 | static void whereLoopOutputAdjust( |
| 117787 | 119002 | WhereClause *pWC, /* The WHERE clause */ |
| 117788 | 119003 | WhereLoop *pLoop, /* The loop to adjust downward */ |
| 117789 | 119004 | LogEst nRow /* Number of rows in the entire table */ |
| 117790 | 119005 | ){ |
| 117791 | 119006 | WhereTerm *pTerm, *pX; |
| 117792 | 119007 | Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); |
| 117793 | | - int i, j; |
| 117794 | | - int nEq = 0; /* Number of = constraints not within likely()/unlikely() */ |
| 119008 | + int i, j, k; |
| 119009 | + LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */ |
| 117795 | 119010 | |
| 119011 | + assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
| 117796 | 119012 | for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ |
| 117797 | 119013 | if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; |
| 117798 | 119014 | if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; |
| 117799 | 119015 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
| 117800 | 119016 | for(j=pLoop->nLTerm-1; j>=0; j--){ |
| | @@ -117803,24 +119019,30 @@ |
| 117803 | 119019 | if( pX==pTerm ) break; |
| 117804 | 119020 | if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; |
| 117805 | 119021 | } |
| 117806 | 119022 | if( j<0 ){ |
| 117807 | 119023 | if( pTerm->truthProb<=0 ){ |
| 119024 | + /* If a truth probability is specified using the likelihood() hints, |
| 119025 | + ** then use the probability provided by the application. */ |
| 117808 | 119026 | pLoop->nOut += pTerm->truthProb; |
| 117809 | 119027 | }else{ |
| 119028 | + /* In the absence of explicit truth probabilities, use heuristics to |
| 119029 | + ** guess a reasonable truth probability. */ |
| 117810 | 119030 | pLoop->nOut--; |
| 117811 | | - if( pTerm->eOperator&WO_EQ ) nEq++; |
| 119031 | + if( pTerm->eOperator&WO_EQ ){ |
| 119032 | + Expr *pRight = pTerm->pExpr->pRight; |
| 119033 | + if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){ |
| 119034 | + k = 10; |
| 119035 | + }else{ |
| 119036 | + k = 20; |
| 119037 | + } |
| 119038 | + if( iReduce<k ) iReduce = k; |
| 119039 | + } |
| 117812 | 119040 | } |
| 117813 | 119041 | } |
| 117814 | 119042 | } |
| 117815 | | - /* TUNING: If there is at least one equality constraint in the WHERE |
| 117816 | | - ** clause that does not have a likelihood() explicitly assigned to it |
| 117817 | | - ** then do not let the estimated number of output rows exceed half |
| 117818 | | - ** the number of rows in the table. */ |
| 117819 | | - if( nEq && pLoop->nOut>nRow-10 ){ |
| 117820 | | - pLoop->nOut = nRow - 10; |
| 117821 | | - } |
| 119043 | + if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce; |
| 117822 | 119044 | } |
| 117823 | 119045 | |
| 117824 | 119046 | /* |
| 117825 | 119047 | ** Adjust the cost C by the costMult facter T. This only occurs if |
| 117826 | 119048 | ** compiled with -DSQLITE_ENABLE_COSTMULT |
| | @@ -117857,11 +119079,11 @@ |
| 117857 | 119079 | int opMask; /* Valid operators for constraints */ |
| 117858 | 119080 | WhereScan scan; /* Iterator for WHERE terms */ |
| 117859 | 119081 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 117860 | 119082 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 117861 | 119083 | u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 117862 | | - u16 saved_nSkip; /* Original value of pNew->u.btree.nSkip */ |
| 119084 | + u16 saved_nSkip; /* Original value of pNew->nSkip */ |
| 117863 | 119085 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 117864 | 119086 | LogEst saved_nOut; /* Original value of pNew->nOut */ |
| 117865 | 119087 | int iCol; /* Index of the column in the table */ |
| 117866 | 119088 | int rc = SQLITE_OK; /* Return code */ |
| 117867 | 119089 | LogEst rSize; /* Number of rows in the table */ |
| | @@ -117886,56 +119108,18 @@ |
| 117886 | 119108 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| 117887 | 119109 | |
| 117888 | 119110 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
| 117889 | 119111 | opMask, pProbe); |
| 117890 | 119112 | saved_nEq = pNew->u.btree.nEq; |
| 117891 | | - saved_nSkip = pNew->u.btree.nSkip; |
| 119113 | + saved_nSkip = pNew->nSkip; |
| 117892 | 119114 | saved_nLTerm = pNew->nLTerm; |
| 117893 | 119115 | saved_wsFlags = pNew->wsFlags; |
| 117894 | 119116 | saved_prereq = pNew->prereq; |
| 117895 | 119117 | saved_nOut = pNew->nOut; |
| 117896 | 119118 | pNew->rSetup = 0; |
| 117897 | 119119 | rSize = pProbe->aiRowLogEst[0]; |
| 117898 | 119120 | rLogSize = estLog(rSize); |
| 117899 | | - |
| 117900 | | - /* Consider using a skip-scan if there are no WHERE clause constraints |
| 117901 | | - ** available for the left-most terms of the index, and if the average |
| 117902 | | - ** number of repeats in the left-most terms is at least 18. |
| 117903 | | - ** |
| 117904 | | - ** The magic number 18 is selected on the basis that scanning 17 rows |
| 117905 | | - ** is almost always quicker than an index seek (even though if the index |
| 117906 | | - ** contains fewer than 2^17 rows we assume otherwise in other parts of |
| 117907 | | - ** the code). And, even if it is not, it should not be too much slower. |
| 117908 | | - ** On the other hand, the extra seeks could end up being significantly |
| 117909 | | - ** more expensive. */ |
| 117910 | | - assert( 42==sqlite3LogEst(18) ); |
| 117911 | | - if( saved_nEq==saved_nSkip |
| 117912 | | - && saved_nEq+1<pProbe->nKeyCol |
| 117913 | | - && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ |
| 117914 | | - && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK |
| 117915 | | - ){ |
| 117916 | | - LogEst nIter; |
| 117917 | | - pNew->u.btree.nEq++; |
| 117918 | | - pNew->u.btree.nSkip++; |
| 117919 | | - pNew->aLTerm[pNew->nLTerm++] = 0; |
| 117920 | | - pNew->wsFlags |= WHERE_SKIPSCAN; |
| 117921 | | - nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; |
| 117922 | | - if( pTerm ){ |
| 117923 | | - /* TUNING: When estimating skip-scan for a term that is also indexable, |
| 117924 | | - ** multiply the cost of the skip-scan by 2.0, to make it a little less |
| 117925 | | - ** desirable than the regular index lookup. */ |
| 117926 | | - nIter += 10; assert( 10==sqlite3LogEst(2) ); |
| 117927 | | - } |
| 117928 | | - pNew->nOut -= nIter; |
| 117929 | | - /* TUNING: Because uncertainties in the estimates for skip-scan queries, |
| 117930 | | - ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ |
| 117931 | | - nIter += 5; |
| 117932 | | - whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
| 117933 | | - pNew->nOut = saved_nOut; |
| 117934 | | - pNew->u.btree.nEq = saved_nEq; |
| 117935 | | - pNew->u.btree.nSkip = saved_nSkip; |
| 117936 | | - } |
| 117937 | 119121 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
| 117938 | 119122 | u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ |
| 117939 | 119123 | LogEst rCostIdx; |
| 117940 | 119124 | LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ |
| 117941 | 119125 | int nIn = 0; |
| | @@ -118026,11 +119210,10 @@ |
| 118026 | 119210 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 118027 | 119211 | tRowcnt nOut = 0; |
| 118028 | 119212 | if( nInMul==0 |
| 118029 | 119213 | && pProbe->nSample |
| 118030 | 119214 | && pNew->u.btree.nEq<=pProbe->nSampleCol |
| 118031 | | - && OptimizationEnabled(db, SQLITE_Stat3) |
| 118032 | 119215 | && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) |
| 118033 | 119216 | ){ |
| 118034 | 119217 | Expr *pExpr = pTerm->pExpr; |
| 118035 | 119218 | if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){ |
| 118036 | 119219 | testcase( eOp & WO_EQ ); |
| | @@ -118094,14 +119277,49 @@ |
| 118094 | 119277 | pBuilder->nRecValid = nRecValid; |
| 118095 | 119278 | #endif |
| 118096 | 119279 | } |
| 118097 | 119280 | pNew->prereq = saved_prereq; |
| 118098 | 119281 | pNew->u.btree.nEq = saved_nEq; |
| 118099 | | - pNew->u.btree.nSkip = saved_nSkip; |
| 119282 | + pNew->nSkip = saved_nSkip; |
| 118100 | 119283 | pNew->wsFlags = saved_wsFlags; |
| 118101 | 119284 | pNew->nOut = saved_nOut; |
| 118102 | 119285 | pNew->nLTerm = saved_nLTerm; |
| 119286 | + |
| 119287 | + /* Consider using a skip-scan if there are no WHERE clause constraints |
| 119288 | + ** available for the left-most terms of the index, and if the average |
| 119289 | + ** number of repeats in the left-most terms is at least 18. |
| 119290 | + ** |
| 119291 | + ** The magic number 18 is selected on the basis that scanning 17 rows |
| 119292 | + ** is almost always quicker than an index seek (even though if the index |
| 119293 | + ** contains fewer than 2^17 rows we assume otherwise in other parts of |
| 119294 | + ** the code). And, even if it is not, it should not be too much slower. |
| 119295 | + ** On the other hand, the extra seeks could end up being significantly |
| 119296 | + ** more expensive. */ |
| 119297 | + assert( 42==sqlite3LogEst(18) ); |
| 119298 | + if( saved_nEq==saved_nSkip |
| 119299 | + && saved_nEq+1<pProbe->nKeyCol |
| 119300 | + && pProbe->noSkipScan==0 |
| 119301 | + && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ |
| 119302 | + && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK |
| 119303 | + ){ |
| 119304 | + LogEst nIter; |
| 119305 | + pNew->u.btree.nEq++; |
| 119306 | + pNew->nSkip++; |
| 119307 | + pNew->aLTerm[pNew->nLTerm++] = 0; |
| 119308 | + pNew->wsFlags |= WHERE_SKIPSCAN; |
| 119309 | + nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; |
| 119310 | + pNew->nOut -= nIter; |
| 119311 | + /* TUNING: Because uncertainties in the estimates for skip-scan queries, |
| 119312 | + ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ |
| 119313 | + nIter += 5; |
| 119314 | + whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
| 119315 | + pNew->nOut = saved_nOut; |
| 119316 | + pNew->u.btree.nEq = saved_nEq; |
| 119317 | + pNew->nSkip = saved_nSkip; |
| 119318 | + pNew->wsFlags = saved_wsFlags; |
| 119319 | + } |
| 119320 | + |
| 118103 | 119321 | return rc; |
| 118104 | 119322 | } |
| 118105 | 119323 | |
| 118106 | 119324 | /* |
| 118107 | 119325 | ** Return True if it is possible that pIndex might be useful in |
| | @@ -118276,11 +119494,11 @@ |
| 118276 | 119494 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 118277 | 119495 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
| 118278 | 119496 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 118279 | 119497 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 118280 | 119498 | pNew->u.btree.nEq = 1; |
| 118281 | | - pNew->u.btree.nSkip = 0; |
| 119499 | + pNew->nSkip = 0; |
| 118282 | 119500 | pNew->u.btree.pIndex = 0; |
| 118283 | 119501 | pNew->nLTerm = 1; |
| 118284 | 119502 | pNew->aLTerm[0] = pTerm; |
| 118285 | 119503 | /* TUNING: One-time cost for computing the automatic index is |
| 118286 | 119504 | ** estimated to be X*N*log2(N) where N is the number of rows in |
| | @@ -118317,11 +119535,11 @@ |
| 118317 | 119535 | testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ |
| 118318 | 119536 | continue; /* Partial index inappropriate for this query */ |
| 118319 | 119537 | } |
| 118320 | 119538 | rSize = pProbe->aiRowLogEst[0]; |
| 118321 | 119539 | pNew->u.btree.nEq = 0; |
| 118322 | | - pNew->u.btree.nSkip = 0; |
| 119540 | + pNew->nSkip = 0; |
| 118323 | 119541 | pNew->nLTerm = 0; |
| 118324 | 119542 | pNew->iSortIdx = 0; |
| 118325 | 119543 | pNew->rSetup = 0; |
| 118326 | 119544 | pNew->prereq = mExtra; |
| 118327 | 119545 | pNew->nOut = rSize; |
| | @@ -118867,11 +120085,11 @@ |
| 118867 | 120085 | for(j=0; j<nColumn; j++){ |
| 118868 | 120086 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 118869 | 120087 | |
| 118870 | 120088 | /* Skip over == and IS NULL terms */ |
| 118871 | 120089 | if( j<pLoop->u.btree.nEq |
| 118872 | | - && pLoop->u.btree.nSkip==0 |
| 120090 | + && pLoop->nSkip==0 |
| 118873 | 120091 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
| 118874 | 120092 | ){ |
| 118875 | 120093 | if( i & WO_ISNULL ){ |
| 118876 | 120094 | testcase( isOrderDistinct ); |
| 118877 | 120095 | isOrderDistinct = 0; |
| | @@ -119321,11 +120539,11 @@ |
| 119321 | 120539 | } |
| 119322 | 120540 | } |
| 119323 | 120541 | } |
| 119324 | 120542 | |
| 119325 | 120543 | #ifdef WHERETRACE_ENABLED /* >=2 */ |
| 119326 | | - if( sqlite3WhereTrace>=2 ){ |
| 120544 | + if( sqlite3WhereTrace & 0x02 ){ |
| 119327 | 120545 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
| 119328 | 120546 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
| 119329 | 120547 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
| 119330 | 120548 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
| 119331 | 120549 | pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); |
| | @@ -119440,11 +120658,11 @@ |
| 119440 | 120658 | if( pItem->zIndex ) return 0; |
| 119441 | 120659 | iCur = pItem->iCursor; |
| 119442 | 120660 | pWC = &pWInfo->sWC; |
| 119443 | 120661 | pLoop = pBuilder->pNew; |
| 119444 | 120662 | pLoop->wsFlags = 0; |
| 119445 | | - pLoop->u.btree.nSkip = 0; |
| 120663 | + pLoop->nSkip = 0; |
| 119446 | 120664 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
| 119447 | 120665 | if( pTerm ){ |
| 119448 | 120666 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 119449 | 120667 | pLoop->aLTerm[0] = pTerm; |
| 119450 | 120668 | pLoop->nLTerm = 1; |
| | @@ -119452,11 +120670,10 @@ |
| 119452 | 120670 | /* TUNING: Cost of a rowid lookup is 10 */ |
| 119453 | 120671 | pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ |
| 119454 | 120672 | }else{ |
| 119455 | 120673 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 119456 | 120674 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
| 119457 | | - assert( ArraySize(pLoop->aLTermSpace)==4 ); |
| 119458 | 120675 | if( !IsUniqueIndex(pIdx) |
| 119459 | 120676 | || pIdx->pPartIdxWhere!=0 |
| 119460 | 120677 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
| 119461 | 120678 | ) continue; |
| 119462 | 120679 | for(j=0; j<pIdx->nKeyCol; j++){ |
| | @@ -119961,22 +121178,30 @@ |
| 119961 | 121178 | ** loop below generates code for a single nested loop of the VM |
| 119962 | 121179 | ** program. |
| 119963 | 121180 | */ |
| 119964 | 121181 | notReady = ~(Bitmask)0; |
| 119965 | 121182 | for(ii=0; ii<nTabList; ii++){ |
| 121183 | + int addrExplain; |
| 121184 | + int wsFlags; |
| 119966 | 121185 | pLevel = &pWInfo->a[ii]; |
| 121186 | + wsFlags = pLevel->pWLoop->wsFlags; |
| 119967 | 121187 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 119968 | 121188 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 119969 | 121189 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 119970 | 121190 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 119971 | 121191 | if( db->mallocFailed ) goto whereBeginError; |
| 119972 | 121192 | } |
| 119973 | 121193 | #endif |
| 119974 | | - explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
| 121194 | + addrExplain = explainOneScan( |
| 121195 | + pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags |
| 121196 | + ); |
| 119975 | 121197 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
| 119976 | 121198 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
| 119977 | 121199 | pWInfo->iContinue = pLevel->addrCont; |
| 121200 | + if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){ |
| 121201 | + addScanStatus(v, pTabList, pLevel, addrExplain); |
| 121202 | + } |
| 119978 | 121203 | } |
| 119979 | 121204 | |
| 119980 | 121205 | /* Done. */ |
| 119981 | 121206 | VdbeModuleComment((v, "Begin WHERE-core")); |
| 119982 | 121207 | return pWInfo; |
| | @@ -124640,10 +125865,17 @@ |
| 124640 | 125865 | ** is look for a semicolon that is not part of an string or comment. |
| 124641 | 125866 | */ |
| 124642 | 125867 | SQLITE_API int sqlite3_complete(const char *zSql){ |
| 124643 | 125868 | u8 state = 0; /* Current state, using numbers defined in header comment */ |
| 124644 | 125869 | u8 token; /* Value of the next token */ |
| 125870 | + |
| 125871 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 125872 | + if( zSql==0 ){ |
| 125873 | + (void)SQLITE_MISUSE_BKPT; |
| 125874 | + return 0; |
| 125875 | + } |
| 125876 | +#endif |
| 124645 | 125877 | |
| 124646 | 125878 | #ifndef SQLITE_OMIT_TRIGGER |
| 124647 | 125879 | /* A complex statement machine used to detect the end of a CREATE TRIGGER |
| 124648 | 125880 | ** statement. This is the normal case. |
| 124649 | 125881 | */ |
| | @@ -125238,74 +126470,106 @@ |
| 125238 | 126470 | |
| 125239 | 126471 | va_start(ap, op); |
| 125240 | 126472 | switch( op ){ |
| 125241 | 126473 | |
| 125242 | 126474 | /* Mutex configuration options are only available in a threadsafe |
| 125243 | | - ** compile. |
| 126475 | + ** compile. |
| 125244 | 126476 | */ |
| 125245 | | -#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 |
| 126477 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */ |
| 125246 | 126478 | case SQLITE_CONFIG_SINGLETHREAD: { |
| 125247 | 126479 | /* Disable all mutexing */ |
| 125248 | 126480 | sqlite3GlobalConfig.bCoreMutex = 0; |
| 125249 | 126481 | sqlite3GlobalConfig.bFullMutex = 0; |
| 125250 | 126482 | break; |
| 125251 | 126483 | } |
| 126484 | +#endif |
| 126485 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */ |
| 125252 | 126486 | case SQLITE_CONFIG_MULTITHREAD: { |
| 125253 | 126487 | /* Disable mutexing of database connections */ |
| 125254 | 126488 | /* Enable mutexing of core data structures */ |
| 125255 | 126489 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 125256 | 126490 | sqlite3GlobalConfig.bFullMutex = 0; |
| 125257 | 126491 | break; |
| 125258 | 126492 | } |
| 126493 | +#endif |
| 126494 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */ |
| 125259 | 126495 | case SQLITE_CONFIG_SERIALIZED: { |
| 125260 | 126496 | /* Enable all mutexing */ |
| 125261 | 126497 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 125262 | 126498 | sqlite3GlobalConfig.bFullMutex = 1; |
| 125263 | 126499 | break; |
| 125264 | 126500 | } |
| 126501 | +#endif |
| 126502 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */ |
| 125265 | 126503 | case SQLITE_CONFIG_MUTEX: { |
| 125266 | 126504 | /* Specify an alternative mutex implementation */ |
| 125267 | 126505 | sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); |
| 125268 | 126506 | break; |
| 125269 | 126507 | } |
| 126508 | +#endif |
| 126509 | +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */ |
| 125270 | 126510 | case SQLITE_CONFIG_GETMUTEX: { |
| 125271 | 126511 | /* Retrieve the current mutex implementation */ |
| 125272 | 126512 | *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; |
| 125273 | 126513 | break; |
| 125274 | 126514 | } |
| 125275 | 126515 | #endif |
| 125276 | 126516 | |
| 125277 | | - |
| 125278 | 126517 | case SQLITE_CONFIG_MALLOC: { |
| 125279 | | - /* Specify an alternative malloc implementation */ |
| 126518 | + /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a |
| 126519 | + ** single argument which is a pointer to an instance of the |
| 126520 | + ** sqlite3_mem_methods structure. The argument specifies alternative |
| 126521 | + ** low-level memory allocation routines to be used in place of the memory |
| 126522 | + ** allocation routines built into SQLite. */ |
| 125280 | 126523 | sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); |
| 125281 | 126524 | break; |
| 125282 | 126525 | } |
| 125283 | 126526 | case SQLITE_CONFIG_GETMALLOC: { |
| 125284 | | - /* Retrieve the current malloc() implementation */ |
| 126527 | + /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a |
| 126528 | + ** single argument which is a pointer to an instance of the |
| 126529 | + ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is |
| 126530 | + ** filled with the currently defined memory allocation routines. */ |
| 125285 | 126531 | if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); |
| 125286 | 126532 | *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; |
| 125287 | 126533 | break; |
| 125288 | 126534 | } |
| 125289 | 126535 | case SQLITE_CONFIG_MEMSTATUS: { |
| 125290 | | - /* Enable or disable the malloc status collection */ |
| 126536 | + /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes |
| 126537 | + ** single argument of type int, interpreted as a boolean, which enables |
| 126538 | + ** or disables the collection of memory allocation statistics. */ |
| 125291 | 126539 | sqlite3GlobalConfig.bMemstat = va_arg(ap, int); |
| 125292 | 126540 | break; |
| 125293 | 126541 | } |
| 125294 | 126542 | case SQLITE_CONFIG_SCRATCH: { |
| 125295 | | - /* Designate a buffer for scratch memory space */ |
| 126543 | + /* EVIDENCE-OF: R-08404-60887 There are three arguments to |
| 126544 | + ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from |
| 126545 | + ** which the scratch allocations will be drawn, the size of each scratch |
| 126546 | + ** allocation (sz), and the maximum number of scratch allocations (N). */ |
| 125296 | 126547 | sqlite3GlobalConfig.pScratch = va_arg(ap, void*); |
| 125297 | 126548 | sqlite3GlobalConfig.szScratch = va_arg(ap, int); |
| 125298 | 126549 | sqlite3GlobalConfig.nScratch = va_arg(ap, int); |
| 125299 | 126550 | break; |
| 125300 | 126551 | } |
| 125301 | 126552 | case SQLITE_CONFIG_PAGECACHE: { |
| 125302 | | - /* Designate a buffer for page cache memory space */ |
| 126553 | + /* EVIDENCE-OF: R-31408-40510 There are three arguments to |
| 126554 | + ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory, the size |
| 126555 | + ** of each page buffer (sz), and the number of pages (N). */ |
| 125303 | 126556 | sqlite3GlobalConfig.pPage = va_arg(ap, void*); |
| 125304 | 126557 | sqlite3GlobalConfig.szPage = va_arg(ap, int); |
| 125305 | 126558 | sqlite3GlobalConfig.nPage = va_arg(ap, int); |
| 125306 | 126559 | break; |
| 126560 | + } |
| 126561 | + case SQLITE_CONFIG_PCACHE_HDRSZ: { |
| 126562 | + /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes |
| 126563 | + ** a single parameter which is a pointer to an integer and writes into |
| 126564 | + ** that integer the number of extra bytes per page required for each page |
| 126565 | + ** in SQLITE_CONFIG_PAGECACHE. */ |
| 126566 | + *va_arg(ap, int*) = |
| 126567 | + sqlite3HeaderSizeBtree() + |
| 126568 | + sqlite3HeaderSizePcache() + |
| 126569 | + sqlite3HeaderSizePcache1(); |
| 126570 | + break; |
| 125307 | 126571 | } |
| 125308 | 126572 | |
| 125309 | 126573 | case SQLITE_CONFIG_PCACHE: { |
| 125310 | 126574 | /* no-op */ |
| 125311 | 126575 | break; |
| | @@ -125315,25 +126579,37 @@ |
| 125315 | 126579 | rc = SQLITE_ERROR; |
| 125316 | 126580 | break; |
| 125317 | 126581 | } |
| 125318 | 126582 | |
| 125319 | 126583 | case SQLITE_CONFIG_PCACHE2: { |
| 125320 | | - /* Specify an alternative page cache implementation */ |
| 126584 | + /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a |
| 126585 | + ** single argument which is a pointer to an sqlite3_pcache_methods2 |
| 126586 | + ** object. This object specifies the interface to a custom page cache |
| 126587 | + ** implementation. */ |
| 125321 | 126588 | sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*); |
| 125322 | 126589 | break; |
| 125323 | 126590 | } |
| 125324 | 126591 | case SQLITE_CONFIG_GETPCACHE2: { |
| 126592 | + /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a |
| 126593 | + ** single argument which is a pointer to an sqlite3_pcache_methods2 |
| 126594 | + ** object. SQLite copies of the current page cache implementation into |
| 126595 | + ** that object. */ |
| 125325 | 126596 | if( sqlite3GlobalConfig.pcache2.xInit==0 ){ |
| 125326 | 126597 | sqlite3PCacheSetDefault(); |
| 125327 | 126598 | } |
| 125328 | 126599 | *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2; |
| 125329 | 126600 | break; |
| 125330 | 126601 | } |
| 125331 | 126602 | |
| 126603 | +/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only |
| 126604 | +** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or |
| 126605 | +** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */ |
| 125332 | 126606 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 125333 | 126607 | case SQLITE_CONFIG_HEAP: { |
| 125334 | | - /* Designate a buffer for heap memory space */ |
| 126608 | + /* EVIDENCE-OF: R-19854-42126 There are three arguments to |
| 126609 | + ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the |
| 126610 | + ** number of bytes in the memory buffer, and the minimum allocation size. */ |
| 125335 | 126611 | sqlite3GlobalConfig.pHeap = va_arg(ap, void*); |
| 125336 | 126612 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 125337 | 126613 | sqlite3GlobalConfig.mnReq = va_arg(ap, int); |
| 125338 | 126614 | |
| 125339 | 126615 | if( sqlite3GlobalConfig.mnReq<1 ){ |
| | @@ -125342,21 +126618,23 @@ |
| 125342 | 126618 | /* cap min request size at 2^12 */ |
| 125343 | 126619 | sqlite3GlobalConfig.mnReq = (1<<12); |
| 125344 | 126620 | } |
| 125345 | 126621 | |
| 125346 | 126622 | if( sqlite3GlobalConfig.pHeap==0 ){ |
| 125347 | | - /* If the heap pointer is NULL, then restore the malloc implementation |
| 125348 | | - ** back to NULL pointers too. This will cause the malloc to go |
| 125349 | | - ** back to its default implementation when sqlite3_initialize() is |
| 125350 | | - ** run. |
| 126623 | + /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer) |
| 126624 | + ** is NULL, then SQLite reverts to using its default memory allocator |
| 126625 | + ** (the system malloc() implementation), undoing any prior invocation of |
| 126626 | + ** SQLITE_CONFIG_MALLOC. |
| 126627 | + ** |
| 126628 | + ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to |
| 126629 | + ** revert to its default implementation when sqlite3_initialize() is run |
| 125351 | 126630 | */ |
| 125352 | 126631 | memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); |
| 125353 | 126632 | }else{ |
| 125354 | | - /* The heap pointer is not NULL, then install one of the |
| 125355 | | - ** mem5.c/mem3.c methods. The enclosing #if guarantees at |
| 125356 | | - ** least one of these methods is currently enabled. |
| 125357 | | - */ |
| 126633 | + /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the |
| 126634 | + ** alternative memory allocator is engaged to handle all of SQLites |
| 126635 | + ** memory allocation needs. */ |
| 125358 | 126636 | #ifdef SQLITE_ENABLE_MEMSYS3 |
| 125359 | 126637 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); |
| 125360 | 126638 | #endif |
| 125361 | 126639 | #ifdef SQLITE_ENABLE_MEMSYS5 |
| 125362 | 126640 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); |
| | @@ -125391,15 +126669,23 @@ |
| 125391 | 126669 | ** can be changed at start-time using the |
| 125392 | 126670 | ** sqlite3_config(SQLITE_CONFIG_URI,1) or |
| 125393 | 126671 | ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. |
| 125394 | 126672 | */ |
| 125395 | 126673 | case SQLITE_CONFIG_URI: { |
| 126674 | + /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single |
| 126675 | + ** argument of type int. If non-zero, then URI handling is globally |
| 126676 | + ** enabled. If the parameter is zero, then URI handling is globally |
| 126677 | + ** disabled. */ |
| 125396 | 126678 | sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); |
| 125397 | 126679 | break; |
| 125398 | 126680 | } |
| 125399 | 126681 | |
| 125400 | 126682 | case SQLITE_CONFIG_COVERING_INDEX_SCAN: { |
| 126683 | + /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN |
| 126684 | + ** option takes a single integer argument which is interpreted as a |
| 126685 | + ** boolean in order to enable or disable the use of covering indices for |
| 126686 | + ** full table scans in the query optimizer. */ |
| 125401 | 126687 | sqlite3GlobalConfig.bUseCis = va_arg(ap, int); |
| 125402 | 126688 | break; |
| 125403 | 126689 | } |
| 125404 | 126690 | |
| 125405 | 126691 | #ifdef SQLITE_ENABLE_SQLLOG |
| | @@ -125410,24 +126696,37 @@ |
| 125410 | 126696 | break; |
| 125411 | 126697 | } |
| 125412 | 126698 | #endif |
| 125413 | 126699 | |
| 125414 | 126700 | case SQLITE_CONFIG_MMAP_SIZE: { |
| 126701 | + /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit |
| 126702 | + ** integer (sqlite3_int64) values that are the default mmap size limit |
| 126703 | + ** (the default setting for PRAGMA mmap_size) and the maximum allowed |
| 126704 | + ** mmap size limit. */ |
| 125415 | 126705 | sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64); |
| 125416 | 126706 | sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64); |
| 125417 | | - if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){ |
| 125418 | | - mxMmap = SQLITE_MAX_MMAP_SIZE; |
| 125419 | | - } |
| 125420 | | - sqlite3GlobalConfig.mxMmap = mxMmap; |
| 126707 | + /* EVIDENCE-OF: R-53367-43190 If either argument to this option is |
| 126708 | + ** negative, then that argument is changed to its compile-time default. |
| 126709 | + ** |
| 126710 | + ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be |
| 126711 | + ** silently truncated if necessary so that it does not exceed the |
| 126712 | + ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE |
| 126713 | + ** compile-time option. |
| 126714 | + */ |
| 126715 | + if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ) mxMmap = SQLITE_MAX_MMAP_SIZE; |
| 125421 | 126716 | if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE; |
| 125422 | 126717 | if( szMmap>mxMmap) szMmap = mxMmap; |
| 126718 | + sqlite3GlobalConfig.mxMmap = mxMmap; |
| 125423 | 126719 | sqlite3GlobalConfig.szMmap = szMmap; |
| 125424 | 126720 | break; |
| 125425 | 126721 | } |
| 125426 | 126722 | |
| 125427 | | -#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) |
| 126723 | +#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */ |
| 125428 | 126724 | case SQLITE_CONFIG_WIN32_HEAPSIZE: { |
| 126725 | + /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit |
| 126726 | + ** unsigned integer value that specifies the maximum size of the created |
| 126727 | + ** heap. */ |
| 125429 | 126728 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 125430 | 126729 | break; |
| 125431 | 126730 | } |
| 125432 | 126731 | #endif |
| 125433 | 126732 | |
| | @@ -125507,19 +126806,29 @@ |
| 125507 | 126806 | |
| 125508 | 126807 | /* |
| 125509 | 126808 | ** Return the mutex associated with a database connection. |
| 125510 | 126809 | */ |
| 125511 | 126810 | SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ |
| 126811 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 126812 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 126813 | + (void)SQLITE_MISUSE_BKPT; |
| 126814 | + return 0; |
| 126815 | + } |
| 126816 | +#endif |
| 125512 | 126817 | return db->mutex; |
| 125513 | 126818 | } |
| 125514 | 126819 | |
| 125515 | 126820 | /* |
| 125516 | 126821 | ** Free up as much memory as we can from the given database |
| 125517 | 126822 | ** connection. |
| 125518 | 126823 | */ |
| 125519 | 126824 | SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){ |
| 125520 | 126825 | int i; |
| 126826 | + |
| 126827 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 126828 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 126829 | +#endif |
| 125521 | 126830 | sqlite3_mutex_enter(db->mutex); |
| 125522 | 126831 | sqlite3BtreeEnterAll(db); |
| 125523 | 126832 | for(i=0; i<db->nDb; i++){ |
| 125524 | 126833 | Btree *pBt = db->aDb[i].pBt; |
| 125525 | 126834 | if( pBt ){ |
| | @@ -125605,17 +126914,24 @@ |
| 125605 | 126914 | int nKey1, const void *pKey1, |
| 125606 | 126915 | int nKey2, const void *pKey2 |
| 125607 | 126916 | ){ |
| 125608 | 126917 | int rc, n; |
| 125609 | 126918 | n = nKey1<nKey2 ? nKey1 : nKey2; |
| 126919 | + /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares |
| 126920 | + ** strings byte by byte using the memcmp() function from the standard C |
| 126921 | + ** library. */ |
| 125610 | 126922 | rc = memcmp(pKey1, pKey2, n); |
| 125611 | 126923 | if( rc==0 ){ |
| 125612 | 126924 | if( padFlag |
| 125613 | 126925 | && allSpaces(((char*)pKey1)+n, nKey1-n) |
| 125614 | 126926 | && allSpaces(((char*)pKey2)+n, nKey2-n) |
| 125615 | 126927 | ){ |
| 125616 | | - /* Leave rc unchanged at 0 */ |
| 126928 | + /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra |
| 126929 | + ** spaces at the end of either string do not change the result. In other |
| 126930 | + ** words, strings will compare equal to one another as long as they |
| 126931 | + ** differ only in the number of spaces at the end. |
| 126932 | + */ |
| 125617 | 126933 | }else{ |
| 125618 | 126934 | rc = nKey1 - nKey2; |
| 125619 | 126935 | } |
| 125620 | 126936 | } |
| 125621 | 126937 | return rc; |
| | @@ -125646,24 +126962,42 @@ |
| 125646 | 126962 | |
| 125647 | 126963 | /* |
| 125648 | 126964 | ** Return the ROWID of the most recent insert |
| 125649 | 126965 | */ |
| 125650 | 126966 | SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ |
| 126967 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 126968 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 126969 | + (void)SQLITE_MISUSE_BKPT; |
| 126970 | + return 0; |
| 126971 | + } |
| 126972 | +#endif |
| 125651 | 126973 | return db->lastRowid; |
| 125652 | 126974 | } |
| 125653 | 126975 | |
| 125654 | 126976 | /* |
| 125655 | 126977 | ** Return the number of changes in the most recent call to sqlite3_exec(). |
| 125656 | 126978 | */ |
| 125657 | 126979 | SQLITE_API int sqlite3_changes(sqlite3 *db){ |
| 126980 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 126981 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 126982 | + (void)SQLITE_MISUSE_BKPT; |
| 126983 | + return 0; |
| 126984 | + } |
| 126985 | +#endif |
| 125658 | 126986 | return db->nChange; |
| 125659 | 126987 | } |
| 125660 | 126988 | |
| 125661 | 126989 | /* |
| 125662 | 126990 | ** Return the number of changes since the database handle was opened. |
| 125663 | 126991 | */ |
| 125664 | 126992 | SQLITE_API int sqlite3_total_changes(sqlite3 *db){ |
| 126993 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 126994 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 126995 | + (void)SQLITE_MISUSE_BKPT; |
| 126996 | + return 0; |
| 126997 | + } |
| 126998 | +#endif |
| 125665 | 126999 | return db->nTotalChange; |
| 125666 | 127000 | } |
| 125667 | 127001 | |
| 125668 | 127002 | /* |
| 125669 | 127003 | ** Close all open savepoints. This function only manipulates fields of the |
| | @@ -125839,10 +127173,18 @@ |
| 125839 | 127173 | |
| 125840 | 127174 | /* Close all database connections */ |
| 125841 | 127175 | for(j=0; j<db->nDb; j++){ |
| 125842 | 127176 | struct Db *pDb = &db->aDb[j]; |
| 125843 | 127177 | if( pDb->pBt ){ |
| 127178 | + if( pDb->pSchema ){ |
| 127179 | + /* Must clear the KeyInfo cache. See ticket [e4a18565a36884b00edf] */ |
| 127180 | + for(i=sqliteHashFirst(&pDb->pSchema->idxHash); i; i=sqliteHashNext(i)){ |
| 127181 | + Index *pIdx = sqliteHashData(i); |
| 127182 | + sqlite3KeyInfoUnref(pIdx->pKeyInfo); |
| 127183 | + pIdx->pKeyInfo = 0; |
| 127184 | + } |
| 127185 | + } |
| 125844 | 127186 | sqlite3BtreeClose(pDb->pBt); |
| 125845 | 127187 | pDb->pBt = 0; |
| 125846 | 127188 | if( j!=1 ){ |
| 125847 | 127189 | pDb->pSchema = 0; |
| 125848 | 127190 | } |
| | @@ -125925,17 +127267,19 @@ |
| 125925 | 127267 | sqlite3_free(db); |
| 125926 | 127268 | } |
| 125927 | 127269 | |
| 125928 | 127270 | /* |
| 125929 | 127271 | ** Rollback all database files. If tripCode is not SQLITE_OK, then |
| 125930 | | -** any open cursors are invalidated ("tripped" - as in "tripping a circuit |
| 127272 | +** any write cursors are invalidated ("tripped" - as in "tripping a circuit |
| 125931 | 127273 | ** breaker") and made to return tripCode if there are any further |
| 125932 | | -** attempts to use that cursor. |
| 127274 | +** attempts to use that cursor. Read cursors remain open and valid |
| 127275 | +** but are "saved" in case the table pages are moved around. |
| 125933 | 127276 | */ |
| 125934 | 127277 | SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){ |
| 125935 | 127278 | int i; |
| 125936 | 127279 | int inTrans = 0; |
| 127280 | + int schemaChange; |
| 125937 | 127281 | assert( sqlite3_mutex_held(db->mutex) ); |
| 125938 | 127282 | sqlite3BeginBenignMalloc(); |
| 125939 | 127283 | |
| 125940 | 127284 | /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). |
| 125941 | 127285 | ** This is important in case the transaction being rolled back has |
| | @@ -125942,18 +127286,19 @@ |
| 125942 | 127286 | ** modified the database schema. If the b-tree mutexes are not taken |
| 125943 | 127287 | ** here, then another shared-cache connection might sneak in between |
| 125944 | 127288 | ** the database rollback and schema reset, which can cause false |
| 125945 | 127289 | ** corruption reports in some cases. */ |
| 125946 | 127290 | sqlite3BtreeEnterAll(db); |
| 127291 | + schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0; |
| 125947 | 127292 | |
| 125948 | 127293 | for(i=0; i<db->nDb; i++){ |
| 125949 | 127294 | Btree *p = db->aDb[i].pBt; |
| 125950 | 127295 | if( p ){ |
| 125951 | 127296 | if( sqlite3BtreeIsInTrans(p) ){ |
| 125952 | 127297 | inTrans = 1; |
| 125953 | 127298 | } |
| 125954 | | - sqlite3BtreeRollback(p, tripCode); |
| 127299 | + sqlite3BtreeRollback(p, tripCode, !schemaChange); |
| 125955 | 127300 | } |
| 125956 | 127301 | } |
| 125957 | 127302 | sqlite3VtabRollback(db); |
| 125958 | 127303 | sqlite3EndBenignMalloc(); |
| 125959 | 127304 | |
| | @@ -126205,10 +127550,13 @@ |
| 126205 | 127550 | SQLITE_API int sqlite3_busy_handler( |
| 126206 | 127551 | sqlite3 *db, |
| 126207 | 127552 | int (*xBusy)(void*,int), |
| 126208 | 127553 | void *pArg |
| 126209 | 127554 | ){ |
| 127555 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127556 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE; |
| 127557 | +#endif |
| 126210 | 127558 | sqlite3_mutex_enter(db->mutex); |
| 126211 | 127559 | db->busyHandler.xFunc = xBusy; |
| 126212 | 127560 | db->busyHandler.pArg = pArg; |
| 126213 | 127561 | db->busyHandler.nBusy = 0; |
| 126214 | 127562 | db->busyTimeout = 0; |
| | @@ -126226,10 +127574,16 @@ |
| 126226 | 127574 | sqlite3 *db, |
| 126227 | 127575 | int nOps, |
| 126228 | 127576 | int (*xProgress)(void*), |
| 126229 | 127577 | void *pArg |
| 126230 | 127578 | ){ |
| 127579 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127580 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127581 | + (void)SQLITE_MISUSE_BKPT; |
| 127582 | + return; |
| 127583 | + } |
| 127584 | +#endif |
| 126231 | 127585 | sqlite3_mutex_enter(db->mutex); |
| 126232 | 127586 | if( nOps>0 ){ |
| 126233 | 127587 | db->xProgress = xProgress; |
| 126234 | 127588 | db->nProgressOps = (unsigned)nOps; |
| 126235 | 127589 | db->pProgressArg = pArg; |
| | @@ -126246,10 +127600,13 @@ |
| 126246 | 127600 | /* |
| 126247 | 127601 | ** This routine installs a default busy handler that waits for the |
| 126248 | 127602 | ** specified number of milliseconds before returning 0. |
| 126249 | 127603 | */ |
| 126250 | 127604 | SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
| 127605 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127606 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 127607 | +#endif |
| 126251 | 127608 | if( ms>0 ){ |
| 126252 | 127609 | sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); |
| 126253 | 127610 | db->busyTimeout = ms; |
| 126254 | 127611 | }else{ |
| 126255 | 127612 | sqlite3_busy_handler(db, 0, 0); |
| | @@ -126259,10 +127616,16 @@ |
| 126259 | 127616 | |
| 126260 | 127617 | /* |
| 126261 | 127618 | ** Cause any pending operation to stop at its earliest opportunity. |
| 126262 | 127619 | */ |
| 126263 | 127620 | SQLITE_API void sqlite3_interrupt(sqlite3 *db){ |
| 127621 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127622 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127623 | + (void)SQLITE_MISUSE_BKPT; |
| 127624 | + return; |
| 127625 | + } |
| 127626 | +#endif |
| 126264 | 127627 | db->u1.isInterrupted = 1; |
| 126265 | 127628 | } |
| 126266 | 127629 | |
| 126267 | 127630 | |
| 126268 | 127631 | /* |
| | @@ -126396,10 +127759,16 @@ |
| 126396 | 127759 | void (*xFinal)(sqlite3_context*), |
| 126397 | 127760 | void (*xDestroy)(void *) |
| 126398 | 127761 | ){ |
| 126399 | 127762 | int rc = SQLITE_ERROR; |
| 126400 | 127763 | FuncDestructor *pArg = 0; |
| 127764 | + |
| 127765 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127766 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127767 | + return SQLITE_MISUSE_BKPT; |
| 127768 | + } |
| 127769 | +#endif |
| 126401 | 127770 | sqlite3_mutex_enter(db->mutex); |
| 126402 | 127771 | if( xDestroy ){ |
| 126403 | 127772 | pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); |
| 126404 | 127773 | if( !pArg ){ |
| 126405 | 127774 | xDestroy(p); |
| | @@ -126432,10 +127801,14 @@ |
| 126432 | 127801 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 126433 | 127802 | void (*xFinal)(sqlite3_context*) |
| 126434 | 127803 | ){ |
| 126435 | 127804 | int rc; |
| 126436 | 127805 | char *zFunc8; |
| 127806 | + |
| 127807 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127808 | + if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT; |
| 127809 | +#endif |
| 126437 | 127810 | sqlite3_mutex_enter(db->mutex); |
| 126438 | 127811 | assert( !db->mallocFailed ); |
| 126439 | 127812 | zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); |
| 126440 | 127813 | rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0); |
| 126441 | 127814 | sqlite3DbFree(db, zFunc8); |
| | @@ -126463,10 +127836,16 @@ |
| 126463 | 127836 | const char *zName, |
| 126464 | 127837 | int nArg |
| 126465 | 127838 | ){ |
| 126466 | 127839 | int nName = sqlite3Strlen30(zName); |
| 126467 | 127840 | int rc = SQLITE_OK; |
| 127841 | + |
| 127842 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127843 | + if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){ |
| 127844 | + return SQLITE_MISUSE_BKPT; |
| 127845 | + } |
| 127846 | +#endif |
| 126468 | 127847 | sqlite3_mutex_enter(db->mutex); |
| 126469 | 127848 | if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 126470 | 127849 | rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 126471 | 127850 | 0, sqlite3InvalidFunction, 0, 0, 0); |
| 126472 | 127851 | } |
| | @@ -126484,10 +127863,17 @@ |
| 126484 | 127863 | ** trace is a pointer to a function that is invoked at the start of each |
| 126485 | 127864 | ** SQL statement. |
| 126486 | 127865 | */ |
| 126487 | 127866 | SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ |
| 126488 | 127867 | void *pOld; |
| 127868 | + |
| 127869 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127870 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127871 | + (void)SQLITE_MISUSE_BKPT; |
| 127872 | + return 0; |
| 127873 | + } |
| 127874 | +#endif |
| 126489 | 127875 | sqlite3_mutex_enter(db->mutex); |
| 126490 | 127876 | pOld = db->pTraceArg; |
| 126491 | 127877 | db->xTrace = xTrace; |
| 126492 | 127878 | db->pTraceArg = pArg; |
| 126493 | 127879 | sqlite3_mutex_leave(db->mutex); |
| | @@ -126505,10 +127891,17 @@ |
| 126505 | 127891 | sqlite3 *db, |
| 126506 | 127892 | void (*xProfile)(void*,const char*,sqlite_uint64), |
| 126507 | 127893 | void *pArg |
| 126508 | 127894 | ){ |
| 126509 | 127895 | void *pOld; |
| 127896 | + |
| 127897 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127898 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127899 | + (void)SQLITE_MISUSE_BKPT; |
| 127900 | + return 0; |
| 127901 | + } |
| 127902 | +#endif |
| 126510 | 127903 | sqlite3_mutex_enter(db->mutex); |
| 126511 | 127904 | pOld = db->pProfileArg; |
| 126512 | 127905 | db->xProfile = xProfile; |
| 126513 | 127906 | db->pProfileArg = pArg; |
| 126514 | 127907 | sqlite3_mutex_leave(db->mutex); |
| | @@ -126525,10 +127918,17 @@ |
| 126525 | 127918 | sqlite3 *db, /* Attach the hook to this database */ |
| 126526 | 127919 | int (*xCallback)(void*), /* Function to invoke on each commit */ |
| 126527 | 127920 | void *pArg /* Argument to the function */ |
| 126528 | 127921 | ){ |
| 126529 | 127922 | void *pOld; |
| 127923 | + |
| 127924 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127925 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127926 | + (void)SQLITE_MISUSE_BKPT; |
| 127927 | + return 0; |
| 127928 | + } |
| 127929 | +#endif |
| 126530 | 127930 | sqlite3_mutex_enter(db->mutex); |
| 126531 | 127931 | pOld = db->pCommitArg; |
| 126532 | 127932 | db->xCommitCallback = xCallback; |
| 126533 | 127933 | db->pCommitArg = pArg; |
| 126534 | 127934 | sqlite3_mutex_leave(db->mutex); |
| | @@ -126543,10 +127943,17 @@ |
| 126543 | 127943 | sqlite3 *db, /* Attach the hook to this database */ |
| 126544 | 127944 | void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), |
| 126545 | 127945 | void *pArg /* Argument to the function */ |
| 126546 | 127946 | ){ |
| 126547 | 127947 | void *pRet; |
| 127948 | + |
| 127949 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127950 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127951 | + (void)SQLITE_MISUSE_BKPT; |
| 127952 | + return 0; |
| 127953 | + } |
| 127954 | +#endif |
| 126548 | 127955 | sqlite3_mutex_enter(db->mutex); |
| 126549 | 127956 | pRet = db->pUpdateArg; |
| 126550 | 127957 | db->xUpdateCallback = xCallback; |
| 126551 | 127958 | db->pUpdateArg = pArg; |
| 126552 | 127959 | sqlite3_mutex_leave(db->mutex); |
| | @@ -126561,10 +127968,17 @@ |
| 126561 | 127968 | sqlite3 *db, /* Attach the hook to this database */ |
| 126562 | 127969 | void (*xCallback)(void*), /* Callback function */ |
| 126563 | 127970 | void *pArg /* Argument to the function */ |
| 126564 | 127971 | ){ |
| 126565 | 127972 | void *pRet; |
| 127973 | + |
| 127974 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 127975 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 127976 | + (void)SQLITE_MISUSE_BKPT; |
| 127977 | + return 0; |
| 127978 | + } |
| 127979 | +#endif |
| 126566 | 127980 | sqlite3_mutex_enter(db->mutex); |
| 126567 | 127981 | pRet = db->pRollbackArg; |
| 126568 | 127982 | db->xRollbackCallback = xCallback; |
| 126569 | 127983 | db->pRollbackArg = pArg; |
| 126570 | 127984 | sqlite3_mutex_leave(db->mutex); |
| | @@ -126607,10 +128021,13 @@ |
| 126607 | 128021 | SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ |
| 126608 | 128022 | #ifdef SQLITE_OMIT_WAL |
| 126609 | 128023 | UNUSED_PARAMETER(db); |
| 126610 | 128024 | UNUSED_PARAMETER(nFrame); |
| 126611 | 128025 | #else |
| 128026 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128027 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 128028 | +#endif |
| 126612 | 128029 | if( nFrame>0 ){ |
| 126613 | 128030 | sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); |
| 126614 | 128031 | }else{ |
| 126615 | 128032 | sqlite3_wal_hook(db, 0, 0); |
| 126616 | 128033 | } |
| | @@ -126627,10 +128044,16 @@ |
| 126627 | 128044 | int(*xCallback)(void *, sqlite3*, const char*, int), |
| 126628 | 128045 | void *pArg /* First argument passed to xCallback() */ |
| 126629 | 128046 | ){ |
| 126630 | 128047 | #ifndef SQLITE_OMIT_WAL |
| 126631 | 128048 | void *pRet; |
| 128049 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128050 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 128051 | + (void)SQLITE_MISUSE_BKPT; |
| 128052 | + return 0; |
| 128053 | + } |
| 128054 | +#endif |
| 126632 | 128055 | sqlite3_mutex_enter(db->mutex); |
| 126633 | 128056 | pRet = db->pWalArg; |
| 126634 | 128057 | db->xWalCallback = xCallback; |
| 126635 | 128058 | db->pWalArg = pArg; |
| 126636 | 128059 | sqlite3_mutex_leave(db->mutex); |
| | @@ -126653,19 +128076,26 @@ |
| 126653 | 128076 | #ifdef SQLITE_OMIT_WAL |
| 126654 | 128077 | return SQLITE_OK; |
| 126655 | 128078 | #else |
| 126656 | 128079 | int rc; /* Return code */ |
| 126657 | 128080 | int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */ |
| 128081 | + |
| 128082 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128083 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 128084 | +#endif |
| 126658 | 128085 | |
| 126659 | 128086 | /* Initialize the output variables to -1 in case an error occurs. */ |
| 126660 | 128087 | if( pnLog ) *pnLog = -1; |
| 126661 | 128088 | if( pnCkpt ) *pnCkpt = -1; |
| 126662 | 128089 | |
| 126663 | | - assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE ); |
| 126664 | | - assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART ); |
| 126665 | | - assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART ); |
| 126666 | | - if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){ |
| 128090 | + assert( SQLITE_CHECKPOINT_PASSIVE==0 ); |
| 128091 | + assert( SQLITE_CHECKPOINT_FULL==1 ); |
| 128092 | + assert( SQLITE_CHECKPOINT_RESTART==2 ); |
| 128093 | + assert( SQLITE_CHECKPOINT_TRUNCATE==3 ); |
| 128094 | + if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){ |
| 128095 | + /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint |
| 128096 | + ** mode: */ |
| 126667 | 128097 | return SQLITE_MISUSE; |
| 126668 | 128098 | } |
| 126669 | 128099 | |
| 126670 | 128100 | sqlite3_mutex_enter(db->mutex); |
| 126671 | 128101 | if( zDb && zDb[0] ){ |
| | @@ -126689,11 +128119,13 @@ |
| 126689 | 128119 | ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points |
| 126690 | 128120 | ** to contains a zero-length string, all attached databases are |
| 126691 | 128121 | ** checkpointed. |
| 126692 | 128122 | */ |
| 126693 | 128123 | SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){ |
| 126694 | | - return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0); |
| 128124 | + /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to |
| 128125 | + ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */ |
| 128126 | + return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0); |
| 126695 | 128127 | } |
| 126696 | 128128 | |
| 126697 | 128129 | #ifndef SQLITE_OMIT_WAL |
| 126698 | 128130 | /* |
| 126699 | 128131 | ** Run a checkpoint on database iDb. This is a no-op if database iDb is |
| | @@ -127050,10 +128482,16 @@ |
| 127050 | 128482 | ** from forming. |
| 127051 | 128483 | */ |
| 127052 | 128484 | SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ |
| 127053 | 128485 | int oldLimit; |
| 127054 | 128486 | |
| 128487 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128488 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 128489 | + (void)SQLITE_MISUSE_BKPT; |
| 128490 | + return -1; |
| 128491 | + } |
| 128492 | +#endif |
| 127055 | 128493 | |
| 127056 | 128494 | /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME |
| 127057 | 128495 | ** there is a hard upper bound set at compile-time by a C preprocessor |
| 127058 | 128496 | ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to |
| 127059 | 128497 | ** "_MAX_".) |
| | @@ -127126,11 +128564,12 @@ |
| 127126 | 128564 | char c; |
| 127127 | 128565 | int nUri = sqlite3Strlen30(zUri); |
| 127128 | 128566 | |
| 127129 | 128567 | assert( *pzErrMsg==0 ); |
| 127130 | 128568 | |
| 127131 | | - if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) |
| 128569 | + if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */ |
| 128570 | + || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */ |
| 127132 | 128571 | && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ |
| 127133 | 128572 | ){ |
| 127134 | 128573 | char *zOpt; |
| 127135 | 128574 | int eState; /* Parser state when parsing URI */ |
| 127136 | 128575 | int iIn; /* Input character index */ |
| | @@ -127335,10 +128774,13 @@ |
| 127335 | 128774 | int rc; /* Return code */ |
| 127336 | 128775 | int isThreadsafe; /* True for threadsafe connections */ |
| 127337 | 128776 | char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ |
| 127338 | 128777 | char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ |
| 127339 | 128778 | |
| 128779 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 128780 | + if( ppDb==0 ) return SQLITE_MISUSE_BKPT; |
| 128781 | +#endif |
| 127340 | 128782 | *ppDb = 0; |
| 127341 | 128783 | #ifndef SQLITE_OMIT_AUTOINIT |
| 127342 | 128784 | rc = sqlite3_initialize(); |
| 127343 | 128785 | if( rc ) return rc; |
| 127344 | 128786 | #endif |
| | @@ -127449,24 +128891,28 @@ |
| 127449 | 128891 | #endif |
| 127450 | 128892 | |
| 127451 | 128893 | /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
| 127452 | 128894 | ** and UTF-16, so add a version for each to avoid any unnecessary |
| 127453 | 128895 | ** conversions. The only error that can occur here is a malloc() failure. |
| 128896 | + ** |
| 128897 | + ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating |
| 128898 | + ** functions: |
| 127454 | 128899 | */ |
| 127455 | 128900 | createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0); |
| 127456 | 128901 | createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0); |
| 127457 | 128902 | createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0); |
| 128903 | + createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); |
| 127458 | 128904 | createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); |
| 127459 | 128905 | if( db->mallocFailed ){ |
| 127460 | 128906 | goto opendb_out; |
| 127461 | 128907 | } |
| 128908 | + /* EVIDENCE-OF: R-08308-17224 The default collating function for all |
| 128909 | + ** strings is BINARY. |
| 128910 | + */ |
| 127462 | 128911 | db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); |
| 127463 | 128912 | assert( db->pDfltColl!=0 ); |
| 127464 | 128913 | |
| 127465 | | - /* Also add a UTF-8 case-insensitive collation sequence. */ |
| 127466 | | - createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); |
| 127467 | | - |
| 127468 | 128914 | /* Parse the filename/URI argument. */ |
| 127469 | 128915 | db->openFlags = flags; |
| 127470 | 128916 | rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); |
| 127471 | 128917 | if( rc!=SQLITE_OK ){ |
| 127472 | 128918 | if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; |
| | @@ -127624,17 +129070,19 @@ |
| 127624 | 129070 | ){ |
| 127625 | 129071 | char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
| 127626 | 129072 | sqlite3_value *pVal; |
| 127627 | 129073 | int rc; |
| 127628 | 129074 | |
| 127629 | | - assert( zFilename ); |
| 127630 | | - assert( ppDb ); |
| 129075 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129076 | + if( ppDb==0 ) return SQLITE_MISUSE_BKPT; |
| 129077 | +#endif |
| 127631 | 129078 | *ppDb = 0; |
| 127632 | 129079 | #ifndef SQLITE_OMIT_AUTOINIT |
| 127633 | 129080 | rc = sqlite3_initialize(); |
| 127634 | 129081 | if( rc ) return rc; |
| 127635 | 129082 | #endif |
| 129083 | + if( zFilename==0 ) zFilename = "\000\000"; |
| 127636 | 129084 | pVal = sqlite3ValueNew(0); |
| 127637 | 129085 | sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 127638 | 129086 | zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
| 127639 | 129087 | if( zFilename8 ){ |
| 127640 | 129088 | rc = openDatabase(zFilename8, ppDb, |
| | @@ -127660,17 +129108,11 @@ |
| 127660 | 129108 | const char *zName, |
| 127661 | 129109 | int enc, |
| 127662 | 129110 | void* pCtx, |
| 127663 | 129111 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 127664 | 129112 | ){ |
| 127665 | | - int rc; |
| 127666 | | - sqlite3_mutex_enter(db->mutex); |
| 127667 | | - assert( !db->mallocFailed ); |
| 127668 | | - rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0); |
| 127669 | | - rc = sqlite3ApiExit(db, rc); |
| 127670 | | - sqlite3_mutex_leave(db->mutex); |
| 127671 | | - return rc; |
| 129113 | + return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0); |
| 127672 | 129114 | } |
| 127673 | 129115 | |
| 127674 | 129116 | /* |
| 127675 | 129117 | ** Register a new collation sequence with the database handle db. |
| 127676 | 129118 | */ |
| | @@ -127681,10 +129123,14 @@ |
| 127681 | 129123 | void* pCtx, |
| 127682 | 129124 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 127683 | 129125 | void(*xDel)(void*) |
| 127684 | 129126 | ){ |
| 127685 | 129127 | int rc; |
| 129128 | + |
| 129129 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129130 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 129131 | +#endif |
| 127686 | 129132 | sqlite3_mutex_enter(db->mutex); |
| 127687 | 129133 | assert( !db->mallocFailed ); |
| 127688 | 129134 | rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel); |
| 127689 | 129135 | rc = sqlite3ApiExit(db, rc); |
| 127690 | 129136 | sqlite3_mutex_leave(db->mutex); |
| | @@ -127702,10 +129148,14 @@ |
| 127702 | 129148 | void* pCtx, |
| 127703 | 129149 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 127704 | 129150 | ){ |
| 127705 | 129151 | int rc = SQLITE_OK; |
| 127706 | 129152 | char *zName8; |
| 129153 | + |
| 129154 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129155 | + if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 129156 | +#endif |
| 127707 | 129157 | sqlite3_mutex_enter(db->mutex); |
| 127708 | 129158 | assert( !db->mallocFailed ); |
| 127709 | 129159 | zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); |
| 127710 | 129160 | if( zName8 ){ |
| 127711 | 129161 | rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0); |
| | @@ -127724,10 +129174,13 @@ |
| 127724 | 129174 | SQLITE_API int sqlite3_collation_needed( |
| 127725 | 129175 | sqlite3 *db, |
| 127726 | 129176 | void *pCollNeededArg, |
| 127727 | 129177 | void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) |
| 127728 | 129178 | ){ |
| 129179 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129180 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 129181 | +#endif |
| 127729 | 129182 | sqlite3_mutex_enter(db->mutex); |
| 127730 | 129183 | db->xCollNeeded = xCollNeeded; |
| 127731 | 129184 | db->xCollNeeded16 = 0; |
| 127732 | 129185 | db->pCollNeededArg = pCollNeededArg; |
| 127733 | 129186 | sqlite3_mutex_leave(db->mutex); |
| | @@ -127742,10 +129195,13 @@ |
| 127742 | 129195 | SQLITE_API int sqlite3_collation_needed16( |
| 127743 | 129196 | sqlite3 *db, |
| 127744 | 129197 | void *pCollNeededArg, |
| 127745 | 129198 | void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) |
| 127746 | 129199 | ){ |
| 129200 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129201 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 129202 | +#endif |
| 127747 | 129203 | sqlite3_mutex_enter(db->mutex); |
| 127748 | 129204 | db->xCollNeeded = 0; |
| 127749 | 129205 | db->xCollNeeded16 = xCollNeeded16; |
| 127750 | 129206 | db->pCollNeededArg = pCollNeededArg; |
| 127751 | 129207 | sqlite3_mutex_leave(db->mutex); |
| | @@ -127768,10 +129224,16 @@ |
| 127768 | 129224 | ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 127769 | 129225 | ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 127770 | 129226 | ** by the next COMMIT or ROLLBACK. |
| 127771 | 129227 | */ |
| 127772 | 129228 | SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){ |
| 129229 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129230 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 129231 | + (void)SQLITE_MISUSE_BKPT; |
| 129232 | + return 0; |
| 129233 | + } |
| 129234 | +#endif |
| 127773 | 129235 | return db->autoCommit; |
| 127774 | 129236 | } |
| 127775 | 129237 | |
| 127776 | 129238 | /* |
| 127777 | 129239 | ** The following routines are substitutes for constants SQLITE_CORRUPT, |
| | @@ -127950,10 +129412,13 @@ |
| 127950 | 129412 | |
| 127951 | 129413 | /* |
| 127952 | 129414 | ** Enable or disable the extended result codes. |
| 127953 | 129415 | */ |
| 127954 | 129416 | SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ |
| 129417 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129418 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 129419 | +#endif |
| 127955 | 129420 | sqlite3_mutex_enter(db->mutex); |
| 127956 | 129421 | db->errMask = onoff ? 0xffffffff : 0xff; |
| 127957 | 129422 | sqlite3_mutex_leave(db->mutex); |
| 127958 | 129423 | return SQLITE_OK; |
| 127959 | 129424 | } |
| | @@ -127963,10 +129428,13 @@ |
| 127963 | 129428 | */ |
| 127964 | 129429 | SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ |
| 127965 | 129430 | int rc = SQLITE_ERROR; |
| 127966 | 129431 | Btree *pBtree; |
| 127967 | 129432 | |
| 129433 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129434 | + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 129435 | +#endif |
| 127968 | 129436 | sqlite3_mutex_enter(db->mutex); |
| 127969 | 129437 | pBtree = sqlite3DbNameToBtree(db, zDbName); |
| 127970 | 129438 | if( pBtree ){ |
| 127971 | 129439 | Pager *pPager; |
| 127972 | 129440 | sqlite3_file *fd; |
| | @@ -128305,11 +129773,11 @@ |
| 128305 | 129773 | ** query parameter we seek. This routine returns the value of the zParam |
| 128306 | 129774 | ** parameter if it exists. If the parameter does not exist, this routine |
| 128307 | 129775 | ** returns a NULL pointer. |
| 128308 | 129776 | */ |
| 128309 | 129777 | SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){ |
| 128310 | | - if( zFilename==0 ) return 0; |
| 129778 | + if( zFilename==0 || zParam==0 ) return 0; |
| 128311 | 129779 | zFilename += sqlite3Strlen30(zFilename) + 1; |
| 128312 | 129780 | while( zFilename[0] ){ |
| 128313 | 129781 | int x = strcmp(zFilename, zParam); |
| 128314 | 129782 | zFilename += sqlite3Strlen30(zFilename) + 1; |
| 128315 | 129783 | if( x==0 ) return zFilename; |
| | @@ -128361,19 +129829,31 @@ |
| 128361 | 129829 | /* |
| 128362 | 129830 | ** Return the filename of the database associated with a database |
| 128363 | 129831 | ** connection. |
| 128364 | 129832 | */ |
| 128365 | 129833 | SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){ |
| 129834 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129835 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 129836 | + (void)SQLITE_MISUSE_BKPT; |
| 129837 | + return 0; |
| 129838 | + } |
| 129839 | +#endif |
| 128366 | 129840 | Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 128367 | 129841 | return pBt ? sqlite3BtreeGetFilename(pBt) : 0; |
| 128368 | 129842 | } |
| 128369 | 129843 | |
| 128370 | 129844 | /* |
| 128371 | 129845 | ** Return 1 if database is read-only or 0 if read/write. Return -1 if |
| 128372 | 129846 | ** no such database exists. |
| 128373 | 129847 | */ |
| 128374 | 129848 | SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){ |
| 129849 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 129850 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 129851 | + (void)SQLITE_MISUSE_BKPT; |
| 129852 | + return -1; |
| 129853 | + } |
| 129854 | +#endif |
| 128375 | 129855 | Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 128376 | 129856 | return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; |
| 128377 | 129857 | } |
| 128378 | 129858 | |
| 128379 | 129859 | /************** End of main.c ************************************************/ |
| | @@ -137791,11 +139271,11 @@ |
| 137791 | 139271 | ** Return true if the m-value for z is 1 or more. In other words, |
| 137792 | 139272 | ** return true if z contains at least one vowel that is followed |
| 137793 | 139273 | ** by a consonant. |
| 137794 | 139274 | ** |
| 137795 | 139275 | ** In this routine z[] is in reverse order. So we are really looking |
| 137796 | | -** for an instance of of a consonant followed by a vowel. |
| 139276 | +** for an instance of a consonant followed by a vowel. |
| 137797 | 139277 | */ |
| 137798 | 139278 | static int m_gt_0(const char *z){ |
| 137799 | 139279 | while( isVowel(z) ){ z++; } |
| 137800 | 139280 | if( *z==0 ) return 0; |
| 137801 | 139281 | while( isConsonant(z) ){ z++; } |
| 137802 | 139282 | |